2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
9 #include "bin/lttng-sessiond/tracker.h"
10 #include "lttng/lttng-error.h"
11 #include "lttng/tracker.h"
15 #include <urcu/list.h>
16 #include <urcu/uatomic.h>
20 #include <common/defaults.h>
21 #include <common/common.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/relayd/relayd.h>
24 #include <common/utils.h>
25 #include <common/compat/string.h>
26 #include <common/kernel-ctl/kernel-ctl.h>
27 #include <common/dynamic-buffer.h>
28 #include <common/buffer-view.h>
29 #include <common/trace-chunk.h>
30 #include <lttng/location-internal.h>
31 #include <lttng/trigger/trigger-internal.h>
32 #include <lttng/condition/condition.h>
33 #include <lttng/action/action.h>
34 #include <lttng/channel.h>
35 #include <lttng/channel-internal.h>
36 #include <lttng/rotate-internal.h>
37 #include <lttng/location-internal.h>
38 #include <lttng/session-internal.h>
39 #include <lttng/userspace-probe-internal.h>
40 #include <lttng/session-descriptor-internal.h>
41 #include <common/string-utils/string-utils.h>
46 #include "health-sessiond.h"
48 #include "kernel-consumer.h"
49 #include "lttng-sessiond.h"
51 #include "lttng-syscall.h"
53 #include "buffer-registry.h"
54 #include "notification-thread.h"
55 #include "notification-thread-commands.h"
57 #include "rotation-thread.h"
59 #include "agent-thread.h"
63 /* Sleep for 100ms between each check for the shm path's deletion. */
64 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
66 struct cmd_destroy_session_reply_context
{
68 bool implicit_rotation_on_destroy
;
70 * Indicates whether or not an error occurred while launching the
71 * destruction of a session.
73 enum lttng_error_code destruction_status
;
76 static enum lttng_error_code
wait_on_path(void *path
);
79 * Command completion handler that is used by the destroy command
80 * when a session that has a non-default shm_path is being destroyed.
82 * See comment in cmd_destroy_session() for the rationale.
84 static struct destroy_completion_handler
{
85 struct cmd_completion_handler handler
;
86 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
87 } destroy_completion_handler
= {
90 .data
= destroy_completion_handler
.shm_path
95 static struct cmd_completion_handler
*current_completion_handler
;
98 * Used to keep a unique index for each relayd socket created where this value
99 * is associated with streams on the consumer so it can match the right relayd
100 * to send to. It must be accessed with the relayd_net_seq_idx_lock
103 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
104 static uint64_t relayd_net_seq_idx
;
106 static int validate_ust_event_name(const char *);
107 static int cmd_enable_event_internal(struct ltt_session
*session
,
108 const struct lttng_domain
*domain
,
109 char *channel_name
, struct lttng_event
*event
,
110 char *filter_expression
,
111 struct lttng_filter_bytecode
*filter
,
112 struct lttng_event_exclusion
*exclusion
,
114 static int cmd_enable_channel_internal(struct ltt_session
*session
,
115 const struct lttng_domain
*domain
,
116 const struct lttng_channel
*_attr
,
120 * Create a session path used by list_lttng_sessions for the case that the
121 * session consumer is on the network.
123 static int build_network_session_path(char *dst
, size_t size
,
124 struct ltt_session
*session
)
126 int ret
, kdata_port
, udata_port
;
127 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
128 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
133 memset(tmp_urls
, 0, sizeof(tmp_urls
));
134 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
136 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
138 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
139 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
140 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
143 if (session
->ust_session
&& session
->ust_session
->consumer
) {
144 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
145 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
148 if (uuri
== NULL
&& kuri
== NULL
) {
149 uri
= &session
->consumer
->dst
.net
.control
;
150 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
151 } else if (kuri
&& uuri
) {
152 ret
= uri_compare(kuri
, uuri
);
156 /* Build uuri URL string */
157 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
164 } else if (kuri
&& uuri
== NULL
) {
166 } else if (uuri
&& kuri
== NULL
) {
170 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
176 * Do we have a UST url set. If yes, this means we have both kernel and UST
179 if (*tmp_uurl
!= '\0') {
180 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
181 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
184 if (kuri
|| (!kuri
&& !uuri
)) {
187 /* No kernel URI, use the UST port. */
190 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
198 * Get run-time attributes if the session has been started (discarded events,
201 static int get_kernel_runtime_stats(struct ltt_session
*session
,
202 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
203 uint64_t *lost_packets
)
207 if (!session
->has_been_started
) {
209 *discarded_events
= 0;
214 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
215 session
->kernel_session
->consumer
,
221 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
222 session
->kernel_session
->consumer
,
233 * Get run-time attributes if the session has been started (discarded events,
236 static int get_ust_runtime_stats(struct ltt_session
*session
,
237 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
238 uint64_t *lost_packets
)
241 struct ltt_ust_session
*usess
;
243 if (!discarded_events
|| !lost_packets
) {
248 usess
= session
->ust_session
;
249 assert(discarded_events
);
250 assert(lost_packets
);
252 if (!usess
|| !session
->has_been_started
) {
253 *discarded_events
= 0;
259 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
260 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
261 &usess
->buffer_reg_uid_list
,
262 usess
->consumer
, uchan
->id
,
263 uchan
->attr
.overwrite
,
266 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
267 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
268 uchan
, usess
->consumer
,
269 uchan
->attr
.overwrite
,
275 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
276 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
278 ERR("Unsupported buffer type");
288 static int increment_extended_len(const char *filter_expression
,
289 struct lttng_event_exclusion
*exclusion
,
290 const struct lttng_userspace_probe_location
*probe_location
,
291 size_t *extended_len
)
295 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
297 if (filter_expression
) {
298 *extended_len
+= strlen(filter_expression
) + 1;
302 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
305 if (probe_location
) {
306 ret
= lttng_userspace_probe_location_serialize(probe_location
,
311 *extended_len
+= ret
;
318 static int append_extended_info(const char *filter_expression
,
319 struct lttng_event_exclusion
*exclusion
,
320 struct lttng_userspace_probe_location
*probe_location
,
324 size_t filter_len
= 0;
325 size_t nb_exclusions
= 0;
326 size_t userspace_probe_location_len
= 0;
327 struct lttng_dynamic_buffer location_buffer
;
328 struct lttcomm_event_extended_header extended_header
;
330 if (filter_expression
) {
331 filter_len
= strlen(filter_expression
) + 1;
335 nb_exclusions
= exclusion
->count
;
338 if (probe_location
) {
339 lttng_dynamic_buffer_init(&location_buffer
);
340 ret
= lttng_userspace_probe_location_serialize(probe_location
,
341 &location_buffer
, NULL
);
346 userspace_probe_location_len
= location_buffer
.size
;
349 /* Set header fields */
350 extended_header
.filter_len
= filter_len
;
351 extended_header
.nb_exclusions
= nb_exclusions
;
352 extended_header
.userspace_probe_location_len
= userspace_probe_location_len
;
355 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
356 *extended_at
+= sizeof(extended_header
);
358 /* Copy filter string */
359 if (filter_expression
) {
360 memcpy(*extended_at
, filter_expression
, filter_len
);
361 *extended_at
+= filter_len
;
364 /* Copy exclusion names */
366 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
368 memcpy(*extended_at
, &exclusion
->names
, len
);
372 if (probe_location
) {
373 memcpy(*extended_at
, location_buffer
.data
, location_buffer
.size
);
374 *extended_at
+= location_buffer
.size
;
375 lttng_dynamic_buffer_reset(&location_buffer
);
383 * Create a list of agent domain events.
385 * Return number of events in list on success or else a negative value.
387 static int list_lttng_agent_events(struct agent
*agt
,
388 struct lttng_event
**events
, size_t *total_size
)
391 unsigned int nb_event
= 0;
392 struct agent_event
*event
;
393 struct lttng_event
*tmp_events
= NULL
;
394 struct lttng_ht_iter iter
;
395 size_t extended_len
= 0;
401 DBG3("Listing agent events");
404 nb_event
= lttng_ht_get_count(agt
->events
);
411 /* Compute required extended infos size */
412 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
415 * This is only valid because the commands which add events are
416 * processed in the same thread as the listing.
418 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
419 ret
= increment_extended_len(event
->filter_expression
, NULL
, NULL
,
422 DBG("Error computing the length of extended info message");
423 ret
= -LTTNG_ERR_FATAL
;
428 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
429 tmp_events
= zmalloc(*total_size
);
431 PERROR("zmalloc agent events session");
432 ret
= -LTTNG_ERR_FATAL
;
436 extended_at
= ((uint8_t *) tmp_events
) +
437 nb_event
* sizeof(struct lttng_event
);
439 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
440 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
441 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
442 tmp_events
[i
].enabled
= event
->enabled
;
443 tmp_events
[i
].loglevel
= event
->loglevel_value
;
444 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
447 /* Append extended info */
448 ret
= append_extended_info(event
->filter_expression
, NULL
, NULL
,
451 DBG("Error appending extended info message");
452 ret
= -LTTNG_ERR_FATAL
;
457 *events
= tmp_events
;
459 assert(nb_event
== i
);
470 * Create a list of ust global domain events.
472 static int list_lttng_ust_global_events(char *channel_name
,
473 struct ltt_ust_domain_global
*ust_global
,
474 struct lttng_event
**events
, size_t *total_size
)
477 unsigned int nb_event
= 0;
478 struct lttng_ht_iter iter
;
479 struct lttng_ht_node_str
*node
;
480 struct ltt_ust_channel
*uchan
;
481 struct ltt_ust_event
*uevent
;
482 struct lttng_event
*tmp
;
483 size_t extended_len
= 0;
486 DBG("Listing UST global events for channel %s", channel_name
);
490 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
491 node
= lttng_ht_iter_get_node_str(&iter
);
493 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
497 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
499 nb_event
= lttng_ht_get_count(uchan
->events
);
506 DBG3("Listing UST global %d events", nb_event
);
508 /* Compute required extended infos size */
509 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
510 if (uevent
->internal
) {
515 ret
= increment_extended_len(uevent
->filter_expression
,
516 uevent
->exclusion
, NULL
, &extended_len
);
518 DBG("Error computing the length of extended info message");
519 ret
= -LTTNG_ERR_FATAL
;
524 /* All events are internal, skip. */
530 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
531 tmp
= zmalloc(*total_size
);
533 ret
= -LTTNG_ERR_FATAL
;
537 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
539 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
540 if (uevent
->internal
) {
541 /* This event should remain hidden from clients */
544 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
545 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
546 tmp
[i
].enabled
= uevent
->enabled
;
548 switch (uevent
->attr
.instrumentation
) {
549 case LTTNG_UST_TRACEPOINT
:
550 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
552 case LTTNG_UST_PROBE
:
553 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
555 case LTTNG_UST_FUNCTION
:
556 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
560 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
561 switch (uevent
->attr
.loglevel_type
) {
562 case LTTNG_UST_LOGLEVEL_ALL
:
563 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
565 case LTTNG_UST_LOGLEVEL_RANGE
:
566 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
568 case LTTNG_UST_LOGLEVEL_SINGLE
:
569 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
572 if (uevent
->filter
) {
575 if (uevent
->exclusion
) {
576 tmp
[i
].exclusion
= 1;
580 /* Append extended info */
581 ret
= append_extended_info(uevent
->filter_expression
,
582 uevent
->exclusion
, NULL
, &extended_at
);
584 DBG("Error appending extended info message");
585 ret
= -LTTNG_ERR_FATAL
;
598 * Fill lttng_event array of all kernel events in the channel.
600 static int list_lttng_kernel_events(char *channel_name
,
601 struct ltt_kernel_session
*kernel_session
,
602 struct lttng_event
**events
, size_t *total_size
)
605 unsigned int nb_event
;
606 struct ltt_kernel_event
*event
;
607 struct ltt_kernel_channel
*kchan
;
608 size_t extended_len
= 0;
611 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
613 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
617 nb_event
= kchan
->event_count
;
619 DBG("Listing events for channel %s", kchan
->channel
->name
);
627 /* Compute required extended infos size */
628 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
629 ret
= increment_extended_len(event
->filter_expression
, NULL
,
630 event
->userspace_probe_location
,
633 DBG("Error computing the length of extended info message");
634 ret
= -LTTNG_ERR_FATAL
;
639 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
640 *events
= zmalloc(*total_size
);
641 if (*events
== NULL
) {
642 ret
= -LTTNG_ERR_FATAL
;
646 extended_at
= ((void *) *events
) +
647 nb_event
* sizeof(struct lttng_event
);
649 /* Kernel channels */
650 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
651 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
652 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
653 (*events
)[i
].enabled
= event
->enabled
;
654 (*events
)[i
].filter
=
655 (unsigned char) !!event
->filter_expression
;
657 switch (event
->event
->instrumentation
) {
658 case LTTNG_KERNEL_TRACEPOINT
:
659 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
661 case LTTNG_KERNEL_KRETPROBE
:
662 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
663 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
664 sizeof(struct lttng_kernel_kprobe
));
666 case LTTNG_KERNEL_KPROBE
:
667 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
668 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
669 sizeof(struct lttng_kernel_kprobe
));
671 case LTTNG_KERNEL_UPROBE
:
672 (*events
)[i
].type
= LTTNG_EVENT_USERSPACE_PROBE
;
674 case LTTNG_KERNEL_FUNCTION
:
675 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
676 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
677 sizeof(struct lttng_kernel_function
));
679 case LTTNG_KERNEL_NOOP
:
680 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
682 case LTTNG_KERNEL_SYSCALL
:
683 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
685 case LTTNG_KERNEL_ALL
:
693 /* Append extended info */
694 ret
= append_extended_info(event
->filter_expression
, NULL
,
695 event
->userspace_probe_location
, &extended_at
);
697 DBG("Error appending extended info message");
698 ret
= -LTTNG_ERR_FATAL
;
711 * Add URI so the consumer output object. Set the correct path depending on the
712 * domain adding the default trace directory.
714 static enum lttng_error_code
add_uri_to_consumer(
715 const struct ltt_session
*session
,
716 struct consumer_output
*consumer
,
717 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
720 enum lttng_error_code ret_code
= LTTNG_OK
;
724 if (consumer
== NULL
) {
725 DBG("No consumer detected. Don't add URI. Stopping.");
726 ret_code
= LTTNG_ERR_NO_CONSUMER
;
731 case LTTNG_DOMAIN_KERNEL
:
732 ret
= lttng_strncpy(consumer
->domain_subdir
,
733 DEFAULT_KERNEL_TRACE_DIR
,
734 sizeof(consumer
->domain_subdir
));
736 case LTTNG_DOMAIN_UST
:
737 ret
= lttng_strncpy(consumer
->domain_subdir
,
738 DEFAULT_UST_TRACE_DIR
,
739 sizeof(consumer
->domain_subdir
));
743 * This case is possible is we try to add the URI to the global
744 * tracing session consumer object which in this case there is
747 memset(consumer
->domain_subdir
, 0,
748 sizeof(consumer
->domain_subdir
));
752 ERR("Failed to initialize consumer output domain subdirectory");
753 ret_code
= LTTNG_ERR_FATAL
;
757 switch (uri
->dtype
) {
760 DBG2("Setting network URI to consumer");
762 if (consumer
->type
== CONSUMER_DST_NET
) {
763 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
764 consumer
->dst
.net
.control_isset
) ||
765 (uri
->stype
== LTTNG_STREAM_DATA
&&
766 consumer
->dst
.net
.data_isset
)) {
767 ret_code
= LTTNG_ERR_URL_EXIST
;
771 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
774 /* Set URI into consumer output object */
775 ret
= consumer_set_network_uri(session
, consumer
, uri
);
779 } else if (ret
== 1) {
781 * URI was the same in the consumer so we do not append the subdir
782 * again so to not duplicate output dir.
789 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
790 ret_code
= LTTNG_ERR_INVALID
;
793 DBG2("Setting trace directory path from URI to %s",
795 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
797 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
799 sizeof(consumer
->dst
.session_root_path
));
801 ret_code
= LTTNG_ERR_FATAL
;
804 consumer
->type
= CONSUMER_DST_LOCAL
;
814 * Init tracing by creating trace directory and sending fds kernel consumer.
816 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
819 struct lttng_ht_iter iter
;
820 struct consumer_socket
*socket
;
826 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
827 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
829 pthread_mutex_lock(socket
->lock
);
830 ret
= kernel_consumer_send_session(socket
, session
);
831 pthread_mutex_unlock(socket
->lock
);
833 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
845 * Create a socket to the relayd using the URI.
847 * On success, the relayd_sock pointer is set to the created socket.
848 * Else, it remains untouched and an LTTng error code is returned.
850 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
851 struct lttcomm_relayd_sock
**relayd_sock
,
852 struct consumer_output
*consumer
)
855 enum lttng_error_code status
= LTTNG_OK
;
856 struct lttcomm_relayd_sock
*rsock
;
858 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
859 RELAYD_VERSION_COMM_MINOR
);
861 status
= LTTNG_ERR_FATAL
;
866 * Connect to relayd so we can proceed with a session creation. This call
867 * can possibly block for an arbitrary amount of time to set the health
868 * state to be in poll execution.
871 ret
= relayd_connect(rsock
);
874 ERR("Unable to reach lttng-relayd");
875 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
879 /* Create socket for control stream. */
880 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
881 uint64_t result_flags
;
883 DBG3("Creating relayd stream socket from URI");
885 /* Check relayd version */
886 ret
= relayd_version_check(rsock
);
887 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
888 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
890 } else if (ret
< 0) {
891 ERR("Unable to reach lttng-relayd");
892 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
895 consumer
->relay_major_version
= rsock
->major
;
896 consumer
->relay_minor_version
= rsock
->minor
;
897 ret
= relayd_get_configuration(rsock
, 0,
900 ERR("Unable to get relayd configuration");
901 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
904 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
905 consumer
->relay_allows_clear
= true;
907 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
908 DBG3("Creating relayd data socket from URI");
910 /* Command is not valid */
911 ERR("Relayd invalid stream type: %d", uri
->stype
);
912 status
= LTTNG_ERR_INVALID
;
916 *relayd_sock
= rsock
;
921 /* The returned value is not useful since we are on an error path. */
922 (void) relayd_close(rsock
);
930 * Connect to the relayd using URI and send the socket to the right consumer.
932 * The consumer socket lock must be held by the caller.
934 * Returns LTTNG_OK on success or an LTTng error code on failure.
936 static enum lttng_error_code
send_consumer_relayd_socket(
937 unsigned int session_id
,
938 struct lttng_uri
*relayd_uri
,
939 struct consumer_output
*consumer
,
940 struct consumer_socket
*consumer_sock
,
941 const char *session_name
, const char *hostname
,
942 const char *base_path
, int session_live_timer
,
943 const uint64_t *current_chunk_id
,
944 time_t session_creation_time
,
945 bool session_name_contains_creation_time
)
948 struct lttcomm_relayd_sock
*rsock
= NULL
;
949 enum lttng_error_code status
;
951 /* Connect to relayd and make version check if uri is the control. */
952 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
953 if (status
!= LTTNG_OK
) {
954 goto relayd_comm_error
;
958 /* Set the network sequence index if not set. */
959 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
960 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
962 * Increment net_seq_idx because we are about to transfer the
963 * new relayd socket to the consumer.
964 * Assign unique key so the consumer can match streams.
966 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
967 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
970 /* Send relayd socket to consumer. */
971 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
972 relayd_uri
->stype
, session_id
,
973 session_name
, hostname
, base_path
,
974 session_live_timer
, current_chunk_id
,
975 session_creation_time
, session_name_contains_creation_time
);
977 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
981 /* Flag that the corresponding socket was sent. */
982 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
983 consumer_sock
->control_sock_sent
= 1;
984 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
985 consumer_sock
->data_sock_sent
= 1;
989 * Close socket which was dup on the consumer side. The session daemon does
990 * NOT keep track of the relayd socket(s) once transfer to the consumer.
994 if (status
!= LTTNG_OK
) {
996 * The consumer output for this session should not be used anymore
997 * since the relayd connection failed thus making any tracing or/and
998 * streaming not usable.
1000 consumer
->enabled
= 0;
1002 (void) relayd_close(rsock
);
1010 * Send both relayd sockets to a specific consumer and domain. This is a
1011 * helper function to facilitate sending the information to the consumer for a
1014 * The consumer socket lock must be held by the caller.
1016 * Returns LTTNG_OK, or an LTTng error code on failure.
1018 static enum lttng_error_code
send_consumer_relayd_sockets(
1019 enum lttng_domain_type domain
,
1020 unsigned int session_id
, struct consumer_output
*consumer
,
1021 struct consumer_socket
*sock
, const char *session_name
,
1022 const char *hostname
, const char *base_path
, int session_live_timer
,
1023 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1024 bool session_name_contains_creation_time
)
1026 enum lttng_error_code status
= LTTNG_OK
;
1031 /* Sending control relayd socket. */
1032 if (!sock
->control_sock_sent
) {
1033 status
= send_consumer_relayd_socket(session_id
,
1034 &consumer
->dst
.net
.control
, consumer
, sock
,
1035 session_name
, hostname
, base_path
, session_live_timer
,
1036 current_chunk_id
, session_creation_time
,
1037 session_name_contains_creation_time
);
1038 if (status
!= LTTNG_OK
) {
1043 /* Sending data relayd socket. */
1044 if (!sock
->data_sock_sent
) {
1045 status
= send_consumer_relayd_socket(session_id
,
1046 &consumer
->dst
.net
.data
, consumer
, sock
,
1047 session_name
, hostname
, base_path
, session_live_timer
,
1048 current_chunk_id
, session_creation_time
,
1049 session_name_contains_creation_time
);
1050 if (status
!= LTTNG_OK
) {
1060 * Setup relayd connections for a tracing session. First creates the socket to
1061 * the relayd and send them to the right domain consumer. Consumer type MUST be
1064 int cmd_setup_relayd(struct ltt_session
*session
)
1067 struct ltt_ust_session
*usess
;
1068 struct ltt_kernel_session
*ksess
;
1069 struct consumer_socket
*socket
;
1070 struct lttng_ht_iter iter
;
1071 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1075 usess
= session
->ust_session
;
1076 ksess
= session
->kernel_session
;
1078 DBG("Setting relayd for session %s", session
->name
);
1081 if (session
->current_trace_chunk
) {
1082 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1083 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1085 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1086 current_chunk_id
.is_set
= true;
1088 ERR("Failed to get current trace chunk id");
1089 ret
= LTTNG_ERR_UNK
;
1094 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1095 && usess
->consumer
->enabled
) {
1096 /* For each consumer socket, send relayd sockets */
1097 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1098 socket
, node
.node
) {
1099 pthread_mutex_lock(socket
->lock
);
1100 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1101 usess
->consumer
, socket
,
1102 session
->name
, session
->hostname
,
1104 session
->live_timer
,
1105 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1106 session
->creation_time
,
1107 session
->name_contains_creation_time
);
1108 pthread_mutex_unlock(socket
->lock
);
1109 if (ret
!= LTTNG_OK
) {
1112 /* Session is now ready for network streaming. */
1113 session
->net_handle
= 1;
1115 session
->consumer
->relay_major_version
=
1116 usess
->consumer
->relay_major_version
;
1117 session
->consumer
->relay_minor_version
=
1118 usess
->consumer
->relay_minor_version
;
1119 session
->consumer
->relay_allows_clear
=
1120 usess
->consumer
->relay_allows_clear
;
1123 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1124 && ksess
->consumer
->enabled
) {
1125 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1126 socket
, node
.node
) {
1127 pthread_mutex_lock(socket
->lock
);
1128 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1129 ksess
->consumer
, socket
,
1130 session
->name
, session
->hostname
,
1132 session
->live_timer
,
1133 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1134 session
->creation_time
,
1135 session
->name_contains_creation_time
);
1136 pthread_mutex_unlock(socket
->lock
);
1137 if (ret
!= LTTNG_OK
) {
1140 /* Session is now ready for network streaming. */
1141 session
->net_handle
= 1;
1143 session
->consumer
->relay_major_version
=
1144 ksess
->consumer
->relay_major_version
;
1145 session
->consumer
->relay_minor_version
=
1146 ksess
->consumer
->relay_minor_version
;
1147 session
->consumer
->relay_allows_clear
=
1148 ksess
->consumer
->relay_allows_clear
;
1157 * Start a kernel session by opening all necessary streams.
1159 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1162 struct ltt_kernel_channel
*kchan
;
1164 /* Open kernel metadata */
1165 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1166 ret
= kernel_open_metadata(ksess
);
1168 ret
= LTTNG_ERR_KERN_META_FAIL
;
1173 /* Open kernel metadata stream */
1174 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1175 ret
= kernel_open_metadata_stream(ksess
);
1177 ERR("Kernel create metadata stream failed");
1178 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1183 /* For each channel */
1184 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1185 if (kchan
->stream_count
== 0) {
1186 ret
= kernel_open_channel_stream(kchan
);
1188 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1191 /* Update the stream global counter */
1192 ksess
->stream_count_global
+= ret
;
1196 /* Setup kernel consumer socket and send fds to it */
1197 ret
= init_kernel_tracing(ksess
);
1199 ret
= LTTNG_ERR_KERN_START_FAIL
;
1203 /* This start the kernel tracing */
1204 ret
= kernel_start_session(ksess
);
1206 ret
= LTTNG_ERR_KERN_START_FAIL
;
1210 /* Quiescent wait after starting trace */
1211 kernel_wait_quiescent();
1221 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1223 struct ltt_kernel_channel
*kchan
;
1224 bool error_occurred
= false;
1227 if (!ksess
|| !ksess
->active
) {
1230 DBG("Stopping kernel tracing");
1232 ret
= kernel_stop_session(ksess
);
1234 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1238 kernel_wait_quiescent();
1240 /* Flush metadata after stopping (if exists) */
1241 if (ksess
->metadata_stream_fd
>= 0) {
1242 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1244 ERR("Kernel metadata flush failed");
1245 error_occurred
= true;
1249 /* Flush all buffers after stopping */
1250 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1251 ret
= kernel_flush_buffer(kchan
);
1253 ERR("Kernel flush buffer error");
1254 error_occurred
= true;
1259 if (error_occurred
) {
1260 ret
= LTTNG_ERR_UNK
;
1269 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1271 int cmd_disable_channel(struct ltt_session
*session
,
1272 enum lttng_domain_type domain
, char *channel_name
)
1275 struct ltt_ust_session
*usess
;
1277 usess
= session
->ust_session
;
1282 case LTTNG_DOMAIN_KERNEL
:
1284 ret
= channel_kernel_disable(session
->kernel_session
,
1286 if (ret
!= LTTNG_OK
) {
1290 kernel_wait_quiescent();
1293 case LTTNG_DOMAIN_UST
:
1295 struct ltt_ust_channel
*uchan
;
1296 struct lttng_ht
*chan_ht
;
1298 chan_ht
= usess
->domain_global
.channels
;
1300 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1301 if (uchan
== NULL
) {
1302 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1306 ret
= channel_ust_disable(usess
, uchan
);
1307 if (ret
!= LTTNG_OK
) {
1313 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1325 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1327 * The wpipe arguments is used as a notifier for the kernel thread.
1329 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1333 ssize_t sock_recv_len
;
1334 struct lttng_channel
*channel
= NULL
;
1335 struct lttng_buffer_view view
;
1336 struct lttng_dynamic_buffer channel_buffer
;
1338 lttng_dynamic_buffer_init(&channel_buffer
);
1339 channel_len
= (size_t) cmd_ctx
->lsm
->u
.channel
.length
;
1340 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1342 ret
= LTTNG_ERR_NOMEM
;
1346 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1348 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1349 ERR("Failed to receive \"enable channel\" command payload");
1350 ret
= LTTNG_ERR_INVALID
;
1354 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1355 if (!lttng_buffer_view_is_valid(&view
)) {
1356 ret
= LTTNG_ERR_INVALID
;
1360 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1361 ERR("Invalid channel payload received in \"enable channel\" command");
1362 ret
= LTTNG_ERR_INVALID
;
1366 ret
= cmd_enable_channel_internal(cmd_ctx
->session
,
1367 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
), channel
,
1371 lttng_dynamic_buffer_reset(&channel_buffer
);
1372 lttng_channel_destroy(channel
);
1376 static int cmd_enable_channel_internal(struct ltt_session
*session
,
1377 const struct lttng_domain
*domain
,
1378 const struct lttng_channel
*_attr
,
1382 struct ltt_ust_session
*usess
= session
->ust_session
;
1383 struct lttng_ht
*chan_ht
;
1385 struct lttng_channel
*attr
= NULL
;
1391 attr
= lttng_channel_copy(_attr
);
1393 ret
= -LTTNG_ERR_NOMEM
;
1397 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1399 /* Validate channel name */
1400 if (attr
->name
[0] == '.' ||
1401 memchr(attr
->name
, '/', len
) != NULL
) {
1402 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1406 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1411 * If the session is a live session, remove the switch timer, the
1412 * live timer does the same thing but sends also synchronisation
1413 * beacons for inactive streams.
1415 if (session
->live_timer
> 0) {
1416 attr
->attr
.live_timer_interval
= session
->live_timer
;
1417 attr
->attr
.switch_timer_interval
= 0;
1420 /* Check for feature support */
1421 switch (domain
->type
) {
1422 case LTTNG_DOMAIN_KERNEL
:
1424 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1425 /* Sampling position of buffer is not supported */
1426 WARN("Kernel tracer does not support buffer monitoring. "
1427 "Setting the monitor interval timer to 0 "
1428 "(disabled) for channel '%s' of session '%s'",
1429 attr
->name
, session
->name
);
1430 lttng_channel_set_monitor_timer_interval(attr
, 0);
1434 case LTTNG_DOMAIN_UST
:
1436 case LTTNG_DOMAIN_JUL
:
1437 case LTTNG_DOMAIN_LOG4J
:
1438 case LTTNG_DOMAIN_PYTHON
:
1439 if (!agent_tracing_is_enabled()) {
1440 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1441 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1446 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1450 switch (domain
->type
) {
1451 case LTTNG_DOMAIN_KERNEL
:
1453 struct ltt_kernel_channel
*kchan
;
1455 kchan
= trace_kernel_get_channel_by_name(
1456 attr
->name
, session
->kernel_session
);
1457 if (kchan
== NULL
) {
1459 * Don't try to create a channel if the session has been started at
1460 * some point in time before. The tracer does not allow it.
1462 if (session
->has_been_started
) {
1463 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1467 if (session
->snapshot
.nb_output
> 0 ||
1468 session
->snapshot_mode
) {
1469 /* Enforce mmap output for snapshot sessions. */
1470 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1472 ret
= channel_kernel_create(
1473 session
->kernel_session
, attr
, wpipe
);
1474 if (attr
->name
[0] != '\0') {
1475 session
->kernel_session
->has_non_default_channel
= 1;
1478 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1481 if (ret
!= LTTNG_OK
) {
1485 kernel_wait_quiescent();
1488 case LTTNG_DOMAIN_UST
:
1489 case LTTNG_DOMAIN_JUL
:
1490 case LTTNG_DOMAIN_LOG4J
:
1491 case LTTNG_DOMAIN_PYTHON
:
1493 struct ltt_ust_channel
*uchan
;
1498 * Current agent implementation limitations force us to allow
1499 * only one channel at once in "agent" subdomains. Each
1500 * subdomain has a default channel name which must be strictly
1503 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1504 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1505 LTTNG_SYMBOL_NAME_LEN
)) {
1506 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1509 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1510 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1511 LTTNG_SYMBOL_NAME_LEN
)) {
1512 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1515 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1516 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1517 LTTNG_SYMBOL_NAME_LEN
)) {
1518 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1523 chan_ht
= usess
->domain_global
.channels
;
1525 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1526 if (uchan
== NULL
) {
1528 * Don't try to create a channel if the session has been started at
1529 * some point in time before. The tracer does not allow it.
1531 if (session
->has_been_started
) {
1532 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1536 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1537 if (attr
->name
[0] != '\0') {
1538 usess
->has_non_default_channel
= 1;
1541 ret
= channel_ust_enable(usess
, uchan
);
1546 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1550 if (ret
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1551 session
->has_non_mmap_channel
= true;
1556 lttng_channel_destroy(attr
);
1560 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1561 struct ltt_session
*session
,
1562 enum lttng_domain_type domain
,
1563 enum lttng_process_attr process_attr
,
1564 enum lttng_tracking_policy
*policy
)
1566 enum lttng_error_code ret_code
= LTTNG_OK
;
1567 const struct process_attr_tracker
*tracker
;
1570 case LTTNG_DOMAIN_KERNEL
:
1571 if (!session
->kernel_session
) {
1572 ret_code
= LTTNG_ERR_INVALID
;
1575 tracker
= kernel_get_process_attr_tracker(
1576 session
->kernel_session
, process_attr
);
1578 case LTTNG_DOMAIN_UST
:
1579 if (!session
->ust_session
) {
1580 ret_code
= LTTNG_ERR_INVALID
;
1583 tracker
= trace_ust_get_process_attr_tracker(
1584 session
->ust_session
, process_attr
);
1587 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1591 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1593 ret_code
= LTTNG_ERR_INVALID
;
1599 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1600 struct ltt_session
*session
,
1601 enum lttng_domain_type domain
,
1602 enum lttng_process_attr process_attr
,
1603 enum lttng_tracking_policy policy
)
1605 enum lttng_error_code ret_code
= LTTNG_OK
;
1608 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1609 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1610 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1613 ret_code
= LTTNG_ERR_INVALID
;
1618 case LTTNG_DOMAIN_KERNEL
:
1619 if (!session
->kernel_session
) {
1620 ret_code
= LTTNG_ERR_INVALID
;
1623 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1624 session
->kernel_session
, process_attr
, policy
);
1626 case LTTNG_DOMAIN_UST
:
1627 if (!session
->ust_session
) {
1628 ret_code
= LTTNG_ERR_INVALID
;
1631 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1632 session
->ust_session
, process_attr
, policy
);
1635 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1642 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1643 struct ltt_session
*session
,
1644 enum lttng_domain_type domain
,
1645 enum lttng_process_attr process_attr
,
1646 const struct process_attr_value
*value
)
1648 enum lttng_error_code ret_code
= LTTNG_OK
;
1651 case LTTNG_DOMAIN_KERNEL
:
1652 if (!session
->kernel_session
) {
1653 ret_code
= LTTNG_ERR_INVALID
;
1656 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1657 session
->kernel_session
, process_attr
, value
);
1659 case LTTNG_DOMAIN_UST
:
1660 if (!session
->ust_session
) {
1661 ret_code
= LTTNG_ERR_INVALID
;
1664 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1665 session
->ust_session
, process_attr
, value
);
1668 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1675 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1676 struct ltt_session
*session
,
1677 enum lttng_domain_type domain
,
1678 enum lttng_process_attr process_attr
,
1679 const struct process_attr_value
*value
)
1681 enum lttng_error_code ret_code
= LTTNG_OK
;
1684 case LTTNG_DOMAIN_KERNEL
:
1685 if (!session
->kernel_session
) {
1686 ret_code
= LTTNG_ERR_INVALID
;
1689 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1690 session
->kernel_session
, process_attr
, value
);
1692 case LTTNG_DOMAIN_UST
:
1693 if (!session
->ust_session
) {
1694 ret_code
= LTTNG_ERR_INVALID
;
1697 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1698 session
->ust_session
, process_attr
, value
);
1701 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1708 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1709 struct ltt_session
*session
,
1710 enum lttng_domain_type domain
,
1711 enum lttng_process_attr process_attr
,
1712 struct lttng_process_attr_values
**values
)
1714 enum lttng_error_code ret_code
= LTTNG_OK
;
1715 const struct process_attr_tracker
*tracker
;
1716 enum process_attr_tracker_status status
;
1719 case LTTNG_DOMAIN_KERNEL
:
1720 if (!session
->kernel_session
) {
1721 ret_code
= LTTNG_ERR_INVALID
;
1724 tracker
= kernel_get_process_attr_tracker(
1725 session
->kernel_session
, process_attr
);
1727 case LTTNG_DOMAIN_UST
:
1728 if (!session
->ust_session
) {
1729 ret_code
= LTTNG_ERR_INVALID
;
1732 tracker
= trace_ust_get_process_attr_tracker(
1733 session
->ust_session
, process_attr
);
1736 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1741 ret_code
= LTTNG_ERR_INVALID
;
1745 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1747 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1748 ret_code
= LTTNG_OK
;
1750 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1751 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1753 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1754 ret_code
= LTTNG_ERR_NOMEM
;
1757 ret_code
= LTTNG_ERR_UNK
;
1766 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1768 int cmd_disable_event(struct ltt_session
*session
,
1769 enum lttng_domain_type domain
, const char *channel_name
,
1770 const struct lttng_event
*event
)
1773 const char *event_name
;
1775 DBG("Disable event command for event \'%s\'", event
->name
);
1777 event_name
= event
->name
;
1779 /* Error out on unhandled search criteria */
1780 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1781 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1782 ret
= LTTNG_ERR_UNK
;
1789 case LTTNG_DOMAIN_KERNEL
:
1791 struct ltt_kernel_channel
*kchan
;
1792 struct ltt_kernel_session
*ksess
;
1794 ksess
= session
->kernel_session
;
1797 * If a non-default channel has been created in the
1798 * session, explicitely require that -c chan_name needs
1801 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1802 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1806 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1807 if (kchan
== NULL
) {
1808 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1812 switch (event
->type
) {
1813 case LTTNG_EVENT_ALL
:
1814 case LTTNG_EVENT_TRACEPOINT
:
1815 case LTTNG_EVENT_SYSCALL
:
1816 case LTTNG_EVENT_PROBE
:
1817 case LTTNG_EVENT_FUNCTION
:
1818 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1819 if (event_name
[0] == '\0') {
1820 ret
= event_kernel_disable_event(kchan
,
1823 ret
= event_kernel_disable_event(kchan
,
1824 event_name
, event
->type
);
1826 if (ret
!= LTTNG_OK
) {
1831 ret
= LTTNG_ERR_UNK
;
1835 kernel_wait_quiescent();
1838 case LTTNG_DOMAIN_UST
:
1840 struct ltt_ust_channel
*uchan
;
1841 struct ltt_ust_session
*usess
;
1843 usess
= session
->ust_session
;
1845 if (validate_ust_event_name(event_name
)) {
1846 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1851 * If a non-default channel has been created in the
1852 * session, explicitly require that -c chan_name needs
1855 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1856 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1860 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1862 if (uchan
== NULL
) {
1863 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1867 switch (event
->type
) {
1868 case LTTNG_EVENT_ALL
:
1870 * An empty event name means that everything
1871 * should be disabled.
1873 if (event
->name
[0] == '\0') {
1874 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1876 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1879 if (ret
!= LTTNG_OK
) {
1884 ret
= LTTNG_ERR_UNK
;
1888 DBG3("Disable UST event %s in channel %s completed", event_name
,
1892 case LTTNG_DOMAIN_LOG4J
:
1893 case LTTNG_DOMAIN_JUL
:
1894 case LTTNG_DOMAIN_PYTHON
:
1897 struct ltt_ust_session
*usess
= session
->ust_session
;
1901 switch (event
->type
) {
1902 case LTTNG_EVENT_ALL
:
1905 ret
= LTTNG_ERR_UNK
;
1909 agt
= trace_ust_find_agent(usess
, domain
);
1911 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1915 * An empty event name means that everything
1916 * should be disabled.
1918 if (event
->name
[0] == '\0') {
1919 ret
= event_agent_disable_all(usess
, agt
);
1921 ret
= event_agent_disable(usess
, agt
, event_name
);
1923 if (ret
!= LTTNG_OK
) {
1930 ret
= LTTNG_ERR_UND
;
1943 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1945 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1946 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1948 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1949 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1952 * Don't try to add a context if the session has been started at
1953 * some point in time before. The tracer does not allow it and would
1954 * result in a corrupted trace.
1956 if (session
->has_been_started
) {
1957 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1961 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1962 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1963 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1967 case LTTNG_DOMAIN_KERNEL
:
1968 assert(session
->kernel_session
);
1970 if (session
->kernel_session
->channel_count
== 0) {
1971 /* Create default channel */
1972 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1973 if (ret
!= LTTNG_OK
) {
1976 chan_kern_created
= 1;
1978 /* Add kernel context to kernel tracer */
1979 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1980 if (ret
!= LTTNG_OK
) {
1984 case LTTNG_DOMAIN_JUL
:
1985 case LTTNG_DOMAIN_LOG4J
:
1988 * Validate channel name.
1989 * If no channel name is given and the domain is JUL or LOG4J,
1990 * set it to the appropriate domain-specific channel name. If
1991 * a name is provided but does not match the expexted channel
1992 * name, return an error.
1994 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1995 strcmp(channel_name
,
1996 DEFAULT_JUL_CHANNEL_NAME
)) {
1997 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1999 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
2000 strcmp(channel_name
,
2001 DEFAULT_LOG4J_CHANNEL_NAME
)) {
2002 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2005 /* break is _not_ missing here. */
2007 case LTTNG_DOMAIN_UST
:
2009 struct ltt_ust_session
*usess
= session
->ust_session
;
2010 unsigned int chan_count
;
2014 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
2015 if (chan_count
== 0) {
2016 struct lttng_channel
*attr
;
2017 /* Create default channel */
2018 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
2020 ret
= LTTNG_ERR_FATAL
;
2024 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
2025 if (ret
!= LTTNG_OK
) {
2029 channel_attr_destroy(attr
);
2030 chan_ust_created
= 1;
2033 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2034 free(app_ctx_provider_name
);
2036 app_ctx_name
= NULL
;
2037 app_ctx_provider_name
= NULL
;
2038 if (ret
!= LTTNG_OK
) {
2044 ret
= LTTNG_ERR_UND
;
2052 if (chan_kern_created
) {
2053 struct ltt_kernel_channel
*kchan
=
2054 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2055 session
->kernel_session
);
2056 /* Created previously, this should NOT fail. */
2058 kernel_destroy_channel(kchan
);
2061 if (chan_ust_created
) {
2062 struct ltt_ust_channel
*uchan
=
2063 trace_ust_find_channel_by_name(
2064 session
->ust_session
->domain_global
.channels
,
2065 DEFAULT_CHANNEL_NAME
);
2066 /* Created previously, this should NOT fail. */
2068 /* Remove from the channel list of the session. */
2069 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2071 trace_ust_destroy_channel(uchan
);
2074 free(app_ctx_provider_name
);
2079 static inline bool name_starts_with(const char *name
, const char *prefix
)
2081 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2083 return !strncmp(name
, prefix
, max_cmp_len
);
2086 /* Perform userspace-specific event name validation */
2087 static int validate_ust_event_name(const char *name
)
2097 * Check name against all internal UST event component namespaces used
2100 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2101 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2102 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2111 * Internal version of cmd_enable_event() with a supplemental
2112 * "internal_event" flag which is used to enable internal events which should
2113 * be hidden from clients. Such events are used in the agent implementation to
2114 * enable the events through which all "agent" events are funeled.
2116 static int _cmd_enable_event(struct ltt_session
*session
,
2117 const struct lttng_domain
*domain
,
2118 char *channel_name
, struct lttng_event
*event
,
2119 char *filter_expression
,
2120 struct lttng_filter_bytecode
*filter
,
2121 struct lttng_event_exclusion
*exclusion
,
2122 int wpipe
, bool internal_event
)
2124 int ret
= 0, channel_created
= 0;
2125 struct lttng_channel
*attr
= NULL
;
2129 assert(channel_name
);
2131 /* If we have a filter, we must have its filter expression */
2132 assert(!(!!filter_expression
^ !!filter
));
2134 /* Normalize event name as a globbing pattern */
2135 strutils_normalize_star_glob_pattern(event
->name
);
2137 /* Normalize exclusion names as globbing patterns */
2141 for (i
= 0; i
< exclusion
->count
; i
++) {
2142 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2144 strutils_normalize_star_glob_pattern(name
);
2148 DBG("Enable event command for event \'%s\'", event
->name
);
2152 switch (domain
->type
) {
2153 case LTTNG_DOMAIN_KERNEL
:
2155 struct ltt_kernel_channel
*kchan
;
2158 * If a non-default channel has been created in the
2159 * session, explicitely require that -c chan_name needs
2162 if (session
->kernel_session
->has_non_default_channel
2163 && channel_name
[0] == '\0') {
2164 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2168 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2169 session
->kernel_session
);
2170 if (kchan
== NULL
) {
2171 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2172 LTTNG_BUFFER_GLOBAL
);
2174 ret
= LTTNG_ERR_FATAL
;
2177 if (lttng_strncpy(attr
->name
, channel_name
,
2178 sizeof(attr
->name
))) {
2179 ret
= LTTNG_ERR_INVALID
;
2183 ret
= cmd_enable_channel_internal(
2184 session
, domain
, attr
, wpipe
);
2185 if (ret
!= LTTNG_OK
) {
2188 channel_created
= 1;
2191 /* Get the newly created kernel channel pointer */
2192 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2193 session
->kernel_session
);
2194 if (kchan
== NULL
) {
2195 /* This sould not happen... */
2196 ret
= LTTNG_ERR_FATAL
;
2200 switch (event
->type
) {
2201 case LTTNG_EVENT_ALL
:
2203 char *filter_expression_a
= NULL
;
2204 struct lttng_filter_bytecode
*filter_a
= NULL
;
2207 * We need to duplicate filter_expression and filter,
2208 * because ownership is passed to first enable
2211 if (filter_expression
) {
2212 filter_expression_a
= strdup(filter_expression
);
2213 if (!filter_expression_a
) {
2214 ret
= LTTNG_ERR_FATAL
;
2219 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2221 free(filter_expression_a
);
2222 ret
= LTTNG_ERR_FATAL
;
2225 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2227 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2228 ret
= event_kernel_enable_event(kchan
, event
,
2229 filter_expression
, filter
);
2230 /* We have passed ownership */
2231 filter_expression
= NULL
;
2233 if (ret
!= LTTNG_OK
) {
2234 if (channel_created
) {
2235 /* Let's not leak a useless channel. */
2236 kernel_destroy_channel(kchan
);
2238 free(filter_expression_a
);
2242 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2243 ret
= event_kernel_enable_event(kchan
, event
,
2244 filter_expression_a
, filter_a
);
2245 /* We have passed ownership */
2246 filter_expression_a
= NULL
;
2248 if (ret
!= LTTNG_OK
) {
2253 case LTTNG_EVENT_PROBE
:
2254 case LTTNG_EVENT_USERSPACE_PROBE
:
2255 case LTTNG_EVENT_FUNCTION
:
2256 case LTTNG_EVENT_FUNCTION_ENTRY
:
2257 case LTTNG_EVENT_TRACEPOINT
:
2258 ret
= event_kernel_enable_event(kchan
, event
,
2259 filter_expression
, filter
);
2260 /* We have passed ownership */
2261 filter_expression
= NULL
;
2263 if (ret
!= LTTNG_OK
) {
2264 if (channel_created
) {
2265 /* Let's not leak a useless channel. */
2266 kernel_destroy_channel(kchan
);
2271 case LTTNG_EVENT_SYSCALL
:
2272 ret
= event_kernel_enable_event(kchan
, event
,
2273 filter_expression
, filter
);
2274 /* We have passed ownership */
2275 filter_expression
= NULL
;
2277 if (ret
!= LTTNG_OK
) {
2282 ret
= LTTNG_ERR_UNK
;
2286 kernel_wait_quiescent();
2289 case LTTNG_DOMAIN_UST
:
2291 struct ltt_ust_channel
*uchan
;
2292 struct ltt_ust_session
*usess
= session
->ust_session
;
2297 * If a non-default channel has been created in the
2298 * session, explicitely require that -c chan_name needs
2301 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2302 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2306 /* Get channel from global UST domain */
2307 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2309 if (uchan
== NULL
) {
2310 /* Create default channel */
2311 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2312 usess
->buffer_type
);
2314 ret
= LTTNG_ERR_FATAL
;
2317 if (lttng_strncpy(attr
->name
, channel_name
,
2318 sizeof(attr
->name
))) {
2319 ret
= LTTNG_ERR_INVALID
;
2323 ret
= cmd_enable_channel_internal(
2324 session
, domain
, attr
, wpipe
);
2325 if (ret
!= LTTNG_OK
) {
2329 /* Get the newly created channel reference back */
2330 uchan
= trace_ust_find_channel_by_name(
2331 usess
->domain_global
.channels
, channel_name
);
2335 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2337 * Don't allow users to add UST events to channels which
2338 * are assigned to a userspace subdomain (JUL, Log4J,
2341 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2345 if (!internal_event
) {
2347 * Ensure the event name is not reserved for internal
2350 ret
= validate_ust_event_name(event
->name
);
2352 WARN("Userspace event name %s failed validation.",
2354 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2359 /* At this point, the session and channel exist on the tracer */
2360 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2361 filter_expression
, filter
, exclusion
,
2363 /* We have passed ownership */
2364 filter_expression
= NULL
;
2367 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2368 goto already_enabled
;
2369 } else if (ret
!= LTTNG_OK
) {
2374 case LTTNG_DOMAIN_LOG4J
:
2375 case LTTNG_DOMAIN_JUL
:
2376 case LTTNG_DOMAIN_PYTHON
:
2378 const char *default_event_name
, *default_chan_name
;
2380 struct lttng_event uevent
;
2381 struct lttng_domain tmp_dom
;
2382 struct ltt_ust_session
*usess
= session
->ust_session
;
2386 if (!agent_tracing_is_enabled()) {
2387 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2388 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2392 agt
= trace_ust_find_agent(usess
, domain
->type
);
2394 agt
= agent_create(domain
->type
);
2396 ret
= LTTNG_ERR_NOMEM
;
2399 agent_add(agt
, usess
->agents
);
2402 /* Create the default tracepoint. */
2403 memset(&uevent
, 0, sizeof(uevent
));
2404 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2405 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2406 default_event_name
= event_get_default_agent_ust_name(
2408 if (!default_event_name
) {
2409 ret
= LTTNG_ERR_FATAL
;
2412 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2413 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2416 * The domain type is changed because we are about to enable the
2417 * default channel and event for the JUL domain that are hardcoded.
2418 * This happens in the UST domain.
2420 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2421 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2423 switch (domain
->type
) {
2424 case LTTNG_DOMAIN_LOG4J
:
2425 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2427 case LTTNG_DOMAIN_JUL
:
2428 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2430 case LTTNG_DOMAIN_PYTHON
:
2431 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2434 /* The switch/case we are in makes this impossible */
2439 char *filter_expression_copy
= NULL
;
2440 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2443 const size_t filter_size
= sizeof(
2444 struct lttng_filter_bytecode
)
2447 filter_copy
= zmalloc(filter_size
);
2449 ret
= LTTNG_ERR_NOMEM
;
2452 memcpy(filter_copy
, filter
, filter_size
);
2454 filter_expression_copy
=
2455 strdup(filter_expression
);
2456 if (!filter_expression
) {
2457 ret
= LTTNG_ERR_NOMEM
;
2460 if (!filter_expression_copy
|| !filter_copy
) {
2461 free(filter_expression_copy
);
2467 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2468 (char *) default_chan_name
,
2469 &uevent
, filter_expression_copy
,
2470 filter_copy
, NULL
, wpipe
);
2473 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2474 goto already_enabled
;
2475 } else if (ret
!= LTTNG_OK
) {
2479 /* The wild card * means that everything should be enabled. */
2480 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2481 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2484 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2488 filter_expression
= NULL
;
2489 if (ret
!= LTTNG_OK
) {
2496 ret
= LTTNG_ERR_UND
;
2504 free(filter_expression
);
2507 channel_attr_destroy(attr
);
2513 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2514 * We own filter, exclusion, and filter_expression.
2516 int cmd_enable_event(struct ltt_session
*session
,
2517 const struct lttng_domain
*domain
,
2518 char *channel_name
, struct lttng_event
*event
,
2519 char *filter_expression
,
2520 struct lttng_filter_bytecode
*filter
,
2521 struct lttng_event_exclusion
*exclusion
,
2524 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2525 filter_expression
, filter
, exclusion
, wpipe
, false);
2529 * Enable an event which is internal to LTTng. An internal should
2530 * never be made visible to clients and are immune to checks such as
2533 static int cmd_enable_event_internal(struct ltt_session
*session
,
2534 const struct lttng_domain
*domain
,
2535 char *channel_name
, struct lttng_event
*event
,
2536 char *filter_expression
,
2537 struct lttng_filter_bytecode
*filter
,
2538 struct lttng_event_exclusion
*exclusion
,
2541 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2542 filter_expression
, filter
, exclusion
, wpipe
, true);
2546 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2548 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2549 struct lttng_event
**events
)
2552 ssize_t nb_events
= 0;
2555 case LTTNG_DOMAIN_KERNEL
:
2556 nb_events
= kernel_list_events(events
);
2557 if (nb_events
< 0) {
2558 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2562 case LTTNG_DOMAIN_UST
:
2563 nb_events
= ust_app_list_events(events
);
2564 if (nb_events
< 0) {
2565 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2569 case LTTNG_DOMAIN_LOG4J
:
2570 case LTTNG_DOMAIN_JUL
:
2571 case LTTNG_DOMAIN_PYTHON
:
2572 nb_events
= agent_list_events(events
, domain
);
2573 if (nb_events
< 0) {
2574 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2579 ret
= LTTNG_ERR_UND
;
2586 /* Return negative value to differentiate return code */
2591 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2593 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2594 struct lttng_event_field
**fields
)
2597 ssize_t nb_fields
= 0;
2600 case LTTNG_DOMAIN_UST
:
2601 nb_fields
= ust_app_list_event_fields(fields
);
2602 if (nb_fields
< 0) {
2603 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2607 case LTTNG_DOMAIN_KERNEL
:
2608 default: /* fall-through */
2609 ret
= LTTNG_ERR_UND
;
2616 /* Return negative value to differentiate return code */
2620 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2622 return syscall_table_list(events
);
2626 * Command LTTNG_START_TRACE processed by the client thread.
2628 * Called with session mutex held.
2630 int cmd_start_trace(struct ltt_session
*session
)
2632 enum lttng_error_code ret
;
2633 unsigned long nb_chan
= 0;
2634 struct ltt_kernel_session
*ksession
;
2635 struct ltt_ust_session
*usess
;
2636 const bool session_rotated_after_last_stop
=
2637 session
->rotated_after_last_stop
;
2638 const bool session_cleared_after_last_stop
=
2639 session
->cleared_after_last_stop
;
2643 /* Ease our life a bit ;) */
2644 ksession
= session
->kernel_session
;
2645 usess
= session
->ust_session
;
2647 /* Is the session already started? */
2648 if (session
->active
) {
2649 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2650 /* Perform nothing */
2654 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2655 !session
->current_trace_chunk
) {
2657 * A rotation was launched while the session was stopped and
2658 * it has not been completed yet. It is not possible to start
2659 * the session since starting the session here would require a
2660 * rotation from "NULL" to a new trace chunk. That rotation
2661 * would overlap with the ongoing rotation, which is not
2664 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2666 ret
= LTTNG_ERR_ROTATION_PENDING
;
2671 * Starting a session without channel is useless since after that it's not
2672 * possible to enable channel thus inform the client.
2674 if (usess
&& usess
->domain_global
.channels
) {
2675 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2678 nb_chan
+= ksession
->channel_count
;
2681 ret
= LTTNG_ERR_NO_CHANNEL
;
2685 session
->active
= 1;
2686 session
->rotated_after_last_stop
= false;
2687 session
->cleared_after_last_stop
= false;
2688 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2689 if (!session
->has_been_started
) {
2690 struct lttng_trace_chunk
*trace_chunk
;
2692 DBG("Creating initial trace chunk of session \"%s\"",
2694 trace_chunk
= session_create_new_trace_chunk(
2695 session
, NULL
, NULL
, NULL
);
2697 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2700 assert(!session
->current_trace_chunk
);
2701 ret
= session_set_trace_chunk(session
, trace_chunk
,
2703 lttng_trace_chunk_put(trace_chunk
);
2705 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2709 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2712 * Rotate existing streams into the new chunk.
2713 * This is a "quiet" rotation has no client has
2714 * explicitly requested this operation.
2716 * There is also no need to wait for the rotation
2717 * to complete as it will happen immediately. No data
2718 * was produced as the session was stopped, so the
2719 * rotation should happen on reception of the command.
2721 ret
= cmd_rotate_session(session
, NULL
, true,
2722 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2723 if (ret
!= LTTNG_OK
) {
2729 /* Kernel tracing */
2730 if (ksession
!= NULL
) {
2731 DBG("Start kernel tracing session %s", session
->name
);
2732 ret
= start_kernel_session(ksession
);
2733 if (ret
!= LTTNG_OK
) {
2738 /* Flag session that trace should start automatically */
2740 int int_ret
= ust_app_start_trace_all(usess
);
2743 ret
= LTTNG_ERR_UST_START_FAIL
;
2749 * Open a packet in every stream of the session to ensure that viewers
2750 * can correctly identify the boundaries of the periods during which
2751 * tracing was active for this session.
2753 ret
= session_open_packets(session
);
2754 if (ret
!= LTTNG_OK
) {
2759 * Clear the flag that indicates that a rotation was done while the
2760 * session was stopped.
2762 session
->rotated_after_last_stop
= false;
2764 if (session
->rotate_timer_period
) {
2765 int int_ret
= timer_session_rotation_schedule_timer_start(
2766 session
, session
->rotate_timer_period
);
2769 ERR("Failed to enable rotate timer");
2770 ret
= LTTNG_ERR_UNK
;
2778 if (ret
== LTTNG_OK
) {
2779 /* Flag this after a successful start. */
2780 session
->has_been_started
|= 1;
2782 session
->active
= 0;
2783 /* Restore initial state on error. */
2784 session
->rotated_after_last_stop
=
2785 session_rotated_after_last_stop
;
2786 session
->cleared_after_last_stop
=
2787 session_cleared_after_last_stop
;
2794 * Command LTTNG_STOP_TRACE processed by the client thread.
2796 int cmd_stop_trace(struct ltt_session
*session
)
2799 struct ltt_kernel_session
*ksession
;
2800 struct ltt_ust_session
*usess
;
2804 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2806 ksession
= session
->kernel_session
;
2807 usess
= session
->ust_session
;
2809 /* Session is not active. Skip everythong and inform the client. */
2810 if (!session
->active
) {
2811 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2815 ret
= stop_kernel_session(ksession
);
2816 if (ret
!= LTTNG_OK
) {
2820 if (usess
&& usess
->active
) {
2821 ret
= ust_app_stop_trace_all(usess
);
2823 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2828 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2830 /* Flag inactive after a successful stop. */
2831 session
->active
= 0;
2839 * Set the base_path of the session only if subdir of a control uris is set.
2840 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2842 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2844 struct lttng_uri
*uris
)
2849 for (i
= 0; i
< nb_uri
; i
++) {
2850 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2851 uris
[i
].subdir
[0] == '\0') {
2852 /* Not interested in these URIs */
2856 if (session
->base_path
!= NULL
) {
2857 free(session
->base_path
);
2858 session
->base_path
= NULL
;
2861 /* Set session base_path */
2862 session
->base_path
= strdup(uris
[i
].subdir
);
2863 if (!session
->base_path
) {
2864 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2865 uris
[i
].subdir
, session
->name
);
2866 ret
= LTTNG_ERR_NOMEM
;
2869 DBG2("Setting base path \"%s\" for session \"%s\"",
2870 session
->base_path
, session
->name
);
2878 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2880 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2881 struct lttng_uri
*uris
)
2884 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2885 struct ltt_ust_session
*usess
= session
->ust_session
;
2891 /* Can't set consumer URI if the session is active. */
2892 if (session
->active
) {
2893 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2898 * Set the session base path if any. This is done inside
2899 * cmd_set_consumer_uri to preserve backward compatibility of the
2900 * previous session creation api vs the session descriptor api.
2902 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2903 if (ret
!= LTTNG_OK
) {
2907 /* Set the "global" consumer URIs */
2908 for (i
= 0; i
< nb_uri
; i
++) {
2909 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2911 if (ret
!= LTTNG_OK
) {
2916 /* Set UST session URIs */
2917 if (session
->ust_session
) {
2918 for (i
= 0; i
< nb_uri
; i
++) {
2919 ret
= add_uri_to_consumer(session
,
2920 session
->ust_session
->consumer
,
2921 &uris
[i
], LTTNG_DOMAIN_UST
);
2922 if (ret
!= LTTNG_OK
) {
2928 /* Set kernel session URIs */
2929 if (session
->kernel_session
) {
2930 for (i
= 0; i
< nb_uri
; i
++) {
2931 ret
= add_uri_to_consumer(session
,
2932 session
->kernel_session
->consumer
,
2933 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2934 if (ret
!= LTTNG_OK
) {
2941 * Make sure to set the session in output mode after we set URI since a
2942 * session can be created without URL (thus flagged in no output mode).
2944 session
->output_traces
= 1;
2946 ksess
->output_traces
= 1;
2950 usess
->output_traces
= 1;
2961 enum lttng_error_code
set_session_output_from_descriptor(
2962 struct ltt_session
*session
,
2963 const struct lttng_session_descriptor
*descriptor
)
2966 enum lttng_error_code ret_code
= LTTNG_OK
;
2967 enum lttng_session_descriptor_type session_type
=
2968 lttng_session_descriptor_get_type(descriptor
);
2969 enum lttng_session_descriptor_output_type output_type
=
2970 lttng_session_descriptor_get_output_type(descriptor
);
2971 struct lttng_uri uris
[2] = {};
2972 size_t uri_count
= 0;
2974 switch (output_type
) {
2975 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2977 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2978 lttng_session_descriptor_get_local_output_uri(descriptor
,
2982 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2983 lttng_session_descriptor_get_network_output_uris(descriptor
,
2984 &uris
[0], &uris
[1]);
2988 ret_code
= LTTNG_ERR_INVALID
;
2992 switch (session_type
) {
2993 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2995 struct snapshot_output
*new_output
= NULL
;
2997 new_output
= snapshot_output_alloc();
2999 ret_code
= LTTNG_ERR_NOMEM
;
3003 ret
= snapshot_output_init_with_uri(session
,
3004 DEFAULT_SNAPSHOT_MAX_SIZE
,
3005 NULL
, uris
, uri_count
, session
->consumer
,
3006 new_output
, &session
->snapshot
);
3008 ret_code
= (ret
== -ENOMEM
) ?
3009 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3010 snapshot_output_destroy(new_output
);
3013 snapshot_add_output(&session
->snapshot
, new_output
);
3016 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3017 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3019 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
3023 ret_code
= LTTNG_ERR_INVALID
;
3031 enum lttng_error_code
cmd_create_session_from_descriptor(
3032 struct lttng_session_descriptor
*descriptor
,
3033 const lttng_sock_cred
*creds
,
3034 const char *home_path
)
3037 enum lttng_error_code ret_code
;
3038 const char *session_name
;
3039 struct ltt_session
*new_session
= NULL
;
3040 enum lttng_session_descriptor_status descriptor_status
;
3042 session_lock_list();
3044 if (*home_path
!= '/') {
3045 ERR("Home path provided by client is not absolute");
3046 ret_code
= LTTNG_ERR_INVALID
;
3051 descriptor_status
= lttng_session_descriptor_get_session_name(
3052 descriptor
, &session_name
);
3053 switch (descriptor_status
) {
3054 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3056 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3057 session_name
= NULL
;
3060 ret_code
= LTTNG_ERR_INVALID
;
3064 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3066 if (ret_code
!= LTTNG_OK
) {
3070 if (!session_name
) {
3071 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3074 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3079 if (!lttng_session_descriptor_is_output_destination_initialized(
3082 * Only include the session's creation time in the output
3083 * destination if the name of the session itself was
3084 * not auto-generated.
3086 ret_code
= lttng_session_descriptor_set_default_output(
3088 session_name
? &new_session
->creation_time
: NULL
,
3090 if (ret_code
!= LTTNG_OK
) {
3094 new_session
->has_user_specified_directory
=
3095 lttng_session_descriptor_has_output_directory(
3099 switch (lttng_session_descriptor_get_type(descriptor
)) {
3100 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3101 new_session
->snapshot_mode
= 1;
3103 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3104 new_session
->live_timer
=
3105 lttng_session_descriptor_live_get_timer_interval(
3112 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3113 if (ret_code
!= LTTNG_OK
) {
3116 new_session
->consumer
->enabled
= 1;
3117 ret_code
= LTTNG_OK
;
3119 /* Release reference provided by the session_create function. */
3120 session_put(new_session
);
3121 if (ret_code
!= LTTNG_OK
&& new_session
) {
3122 /* Release the global reference on error. */
3123 session_destroy(new_session
);
3125 session_unlock_list();
3129 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3130 struct lttng_session_descriptor
**return_descriptor
)
3133 size_t payload_size
;
3134 struct lttng_dynamic_buffer payload
;
3135 struct lttng_buffer_view home_dir_view
;
3136 struct lttng_buffer_view session_descriptor_view
;
3137 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3138 enum lttng_error_code ret_code
;
3140 lttng_dynamic_buffer_init(&payload
);
3141 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
3143 ret_code
= LTTNG_ERR_INVALID
;
3146 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
3147 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3148 ret_code
= LTTNG_ERR_INVALID
;
3152 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
3153 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
3154 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3156 ret_code
= LTTNG_ERR_NOMEM
;
3160 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3162 ERR("Reception of session descriptor failed, aborting.");
3163 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3167 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3170 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3171 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3173 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3174 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3176 ret
= lttng_session_descriptor_create_from_buffer(
3177 &session_descriptor_view
, &session_descriptor
);
3179 ERR("Failed to create session descriptor from payload of \"create session\" command");
3180 ret_code
= LTTNG_ERR_INVALID
;
3185 * Sets the descriptor's auto-generated properties (name, output) if
3188 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3190 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3191 if (ret_code
!= LTTNG_OK
) {
3195 ret_code
= LTTNG_OK
;
3196 *return_descriptor
= session_descriptor
;
3197 session_descriptor
= NULL
;
3199 lttng_dynamic_buffer_reset(&payload
);
3200 lttng_session_descriptor_destroy(session_descriptor
);
3205 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3206 void *_reply_context
)
3210 const struct cmd_destroy_session_reply_context
*reply_context
=
3212 struct lttng_dynamic_buffer payload
;
3213 struct lttcomm_session_destroy_command_header cmd_header
;
3214 struct lttng_trace_archive_location
*location
= NULL
;
3215 struct lttcomm_lttng_msg llm
= {
3216 .cmd_type
= LTTNG_DESTROY_SESSION
,
3217 .ret_code
= reply_context
->destruction_status
,
3220 sizeof(struct lttcomm_session_destroy_command_header
),
3223 size_t payload_size_before_location
;
3225 lttng_dynamic_buffer_init(&payload
);
3227 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3229 ERR("Failed to append session destruction message");
3233 cmd_header
.rotation_state
=
3234 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3235 session
->rotation_state
:
3236 LTTNG_ROTATION_STATE_NO_ROTATION
);
3237 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3238 sizeof(cmd_header
));
3240 ERR("Failed to append session destruction command header");
3244 if (!reply_context
->implicit_rotation_on_destroy
) {
3245 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3249 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3250 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3255 location
= session_get_trace_archive_location(session
);
3257 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3262 payload_size_before_location
= payload
.size
;
3263 comm_ret
= lttng_trace_archive_location_serialize(location
,
3265 lttng_trace_archive_location_put(location
);
3267 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3271 /* Update the message to indicate the location's length. */
3272 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3273 payload
.size
- payload_size_before_location
;
3275 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3276 payload
.data
, payload
.size
);
3277 if (comm_ret
!= (ssize_t
) payload
.size
) {
3278 ERR("Failed to send result of the destruction of session \"%s\" to client",
3282 ret
= close(reply_context
->reply_sock_fd
);
3284 PERROR("Failed to close client socket in deferred session destroy reply");
3286 lttng_dynamic_buffer_reset(&payload
);
3287 free(_reply_context
);
3291 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3293 * Called with session lock held.
3295 int cmd_destroy_session(struct ltt_session
*session
,
3296 struct notification_thread_handle
*notification_thread_handle
,
3300 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3301 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3304 reply_context
= zmalloc(sizeof(*reply_context
));
3305 if (!reply_context
) {
3306 ret
= LTTNG_ERR_NOMEM
;
3309 reply_context
->reply_sock_fd
= *sock_fd
;
3315 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3317 if (session
->active
) {
3318 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3320 ret
= cmd_stop_trace(session
);
3321 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3322 /* Carry on with the destruction of the session. */
3323 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3324 session
->name
, lttng_strerror(-ret
));
3325 destruction_last_error
= ret
;
3329 if (session
->rotation_schedule_timer_enabled
) {
3330 if (timer_session_rotation_schedule_timer_stop(
3332 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3334 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3338 if (session
->rotate_size
) {
3339 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3340 session
->rotate_size
= 0;
3343 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3345 * Perform a last rotation on destruction if rotations have
3346 * occurred during the session's lifetime.
3348 ret
= cmd_rotate_session(session
, NULL
, false,
3349 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3350 if (ret
!= LTTNG_OK
) {
3351 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3352 session
->name
, lttng_strerror(-ret
));
3353 destruction_last_error
= -ret
;
3355 if (reply_context
) {
3356 reply_context
->implicit_rotation_on_destroy
= true;
3358 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3360 * The user has not triggered a session rotation. However, to
3361 * ensure all data has been consumed, the session is rotated
3362 * to a 'null' trace chunk before it is destroyed.
3364 * This is a "quiet" rotation meaning that no notification is
3365 * emitted and no renaming of the current trace chunk takes
3368 ret
= cmd_rotate_session(session
, NULL
, true,
3369 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3371 * Rotation operations may not be supported by the kernel
3372 * tracer. Hence, do not consider this implicit rotation as
3373 * a session destruction error. The library has already stopped
3374 * the session and waited for pending data; there is nothing
3375 * left to do but complete the destruction of the session.
3377 if (ret
!= LTTNG_OK
&&
3378 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3379 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3380 session
->name
, lttng_strerror(ret
));
3381 destruction_last_error
= -ret
;
3385 if (session
->shm_path
[0]) {
3387 * When a session is created with an explicit shm_path,
3388 * the consumer daemon will create its shared memory files
3389 * at that location and will *not* unlink them. This is normal
3390 * as the intention of that feature is to make it possible
3391 * to retrieve the content of those files should a crash occur.
3393 * To ensure the content of those files can be used, the
3394 * sessiond daemon will replicate the content of the metadata
3395 * cache in a metadata file.
3397 * On clean-up, it is expected that the consumer daemon will
3398 * unlink the shared memory files and that the session daemon
3399 * will unlink the metadata file. Then, the session's directory
3400 * in the shm path can be removed.
3402 * Unfortunately, a flaw in the design of the sessiond's and
3403 * consumerd's tear down of channels makes it impossible to
3404 * determine when the sessiond _and_ the consumerd have both
3405 * destroyed their representation of a channel. For one, the
3406 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3407 * callbacks in both daemons.
3409 * However, it is also impossible for the sessiond to know when
3410 * the consumer daemon is done destroying its channel(s) since
3411 * it occurs as a reaction to the closing of the channel's file
3412 * descriptor. There is no resulting communication initiated
3413 * from the consumerd to the sessiond to confirm that the
3414 * operation is completed (and was successful).
3416 * Until this is all fixed, the session daemon checks for the
3417 * removal of the session's shm path which makes it possible
3418 * to safely advertise a session as having been destroyed.
3420 * Prior to this fix, it was not possible to reliably save
3421 * a session making use of the --shm-path option, destroy it,
3422 * and load it again. This is because the creation of the
3423 * session would fail upon seeing the session's shm path
3424 * already in existence.
3426 * Note that none of the error paths in the check for the
3427 * directory's existence return an error. This is normal
3428 * as there isn't much that can be done. The session will
3429 * be destroyed properly, except that we can't offer the
3430 * guarantee that the same session can be re-created.
3432 current_completion_handler
= &destroy_completion_handler
.handler
;
3433 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3435 sizeof(destroy_completion_handler
.shm_path
));
3440 * The session is destroyed. However, note that the command context
3441 * still holds a reference to the session, thus delaying its destruction
3442 * _at least_ up to the point when that reference is released.
3444 session_destroy(session
);
3445 if (reply_context
) {
3446 reply_context
->destruction_status
= destruction_last_error
;
3447 ret
= session_add_destroy_notifier(session
,
3448 cmd_destroy_session_reply
,
3449 (void *) reply_context
);
3451 ret
= LTTNG_ERR_FATAL
;
3463 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3465 int cmd_register_consumer(struct ltt_session
*session
,
3466 enum lttng_domain_type domain
, const char *sock_path
,
3467 struct consumer_data
*cdata
)
3470 struct consumer_socket
*socket
= NULL
;
3477 case LTTNG_DOMAIN_KERNEL
:
3479 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3483 /* Can't register a consumer if there is already one */
3484 if (ksess
->consumer_fds_sent
!= 0) {
3485 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3489 sock
= lttcomm_connect_unix_sock(sock_path
);
3491 ret
= LTTNG_ERR_CONNECT_FAIL
;
3494 cdata
->cmd_sock
= sock
;
3496 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3497 if (socket
== NULL
) {
3500 PERROR("close register consumer");
3502 cdata
->cmd_sock
= -1;
3503 ret
= LTTNG_ERR_FATAL
;
3507 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3508 if (socket
->lock
== NULL
) {
3509 PERROR("zmalloc pthread mutex");
3510 ret
= LTTNG_ERR_FATAL
;
3513 pthread_mutex_init(socket
->lock
, NULL
);
3514 socket
->registered
= 1;
3517 consumer_add_socket(socket
, ksess
->consumer
);
3520 pthread_mutex_lock(&cdata
->pid_mutex
);
3522 pthread_mutex_unlock(&cdata
->pid_mutex
);
3527 /* TODO: Userspace tracing */
3528 ret
= LTTNG_ERR_UND
;
3536 consumer_destroy_socket(socket
);
3542 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3544 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3545 struct lttng_domain
**domains
)
3550 struct lttng_ht_iter iter
;
3552 if (session
->kernel_session
!= NULL
) {
3553 DBG3("Listing domains found kernel domain");
3557 if (session
->ust_session
!= NULL
) {
3558 DBG3("Listing domains found UST global domain");
3562 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3564 if (agt
->being_used
) {
3575 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3576 if (*domains
== NULL
) {
3577 ret
= LTTNG_ERR_FATAL
;
3581 if (session
->kernel_session
!= NULL
) {
3582 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3584 /* Kernel session buffer type is always GLOBAL */
3585 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3590 if (session
->ust_session
!= NULL
) {
3591 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3592 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3596 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3598 if (agt
->being_used
) {
3599 (*domains
)[index
].type
= agt
->domain
;
3600 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3610 /* Return negative value to differentiate return code */
3616 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3618 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3619 struct ltt_session
*session
,
3620 struct lttng_dynamic_buffer
*buffer
,
3621 uint32_t *nb_channel
)
3630 DBG("Listing channels for session %s", session
->name
);
3633 case LTTNG_DOMAIN_KERNEL
:
3635 /* Kernel channels */
3636 struct ltt_kernel_channel
*kchan
;
3637 if (session
->kernel_session
!= NULL
) {
3638 cds_list_for_each_entry(kchan
,
3639 &session
->kernel_session
->channel_list
.head
, list
) {
3640 uint64_t discarded_events
, lost_packets
;
3641 struct lttng_channel_extended
*extended
;
3643 extended
= (struct lttng_channel_extended
*)
3644 kchan
->channel
->attr
.extended
.ptr
;
3646 ret
= get_kernel_runtime_stats(session
, kchan
,
3647 &discarded_events
, &lost_packets
);
3653 * Update the discarded_events and lost_packets
3654 * count for the channel
3656 extended
->discarded_events
= discarded_events
;
3657 extended
->lost_packets
= lost_packets
;
3659 ret
= lttng_channel_serialize(
3660 kchan
->channel
, buffer
);
3671 case LTTNG_DOMAIN_UST
:
3673 struct lttng_ht_iter iter
;
3674 struct ltt_ust_channel
*uchan
;
3677 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3678 &iter
.iter
, uchan
, node
.node
) {
3679 uint64_t discarded_events
= 0, lost_packets
= 0;
3680 struct lttng_channel
*channel
= NULL
;
3681 struct lttng_channel_extended
*extended
;
3683 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3689 extended
= (struct lttng_channel_extended
*)
3690 channel
->attr
.extended
.ptr
;
3692 ret
= get_ust_runtime_stats(session
, uchan
,
3693 &discarded_events
, &lost_packets
);
3695 lttng_channel_destroy(channel
);
3699 extended
->discarded_events
= discarded_events
;
3700 extended
->lost_packets
= lost_packets
;
3702 ret
= lttng_channel_serialize(channel
, buffer
);
3720 return LTTNG_ERR_FATAL
;
3728 * Command LTTNG_LIST_EVENTS processed by the client thread.
3730 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3731 struct ltt_session
*session
, char *channel_name
,
3732 struct lttng_event
**events
, size_t *total_size
)
3735 ssize_t nb_event
= 0;
3738 case LTTNG_DOMAIN_KERNEL
:
3739 if (session
->kernel_session
!= NULL
) {
3740 nb_event
= list_lttng_kernel_events(channel_name
,
3741 session
->kernel_session
, events
,
3745 case LTTNG_DOMAIN_UST
:
3747 if (session
->ust_session
!= NULL
) {
3748 nb_event
= list_lttng_ust_global_events(channel_name
,
3749 &session
->ust_session
->domain_global
, events
,
3754 case LTTNG_DOMAIN_LOG4J
:
3755 case LTTNG_DOMAIN_JUL
:
3756 case LTTNG_DOMAIN_PYTHON
:
3757 if (session
->ust_session
) {
3758 struct lttng_ht_iter iter
;
3762 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3763 &iter
.iter
, agt
, node
.node
) {
3764 if (agt
->domain
== domain
) {
3765 nb_event
= list_lttng_agent_events(
3775 ret
= LTTNG_ERR_UND
;
3782 /* Return negative value to differentiate return code */
3787 * Using the session list, filled a lttng_session array to send back to the
3788 * client for session listing.
3790 * The session list lock MUST be acquired before calling this function. Use
3791 * session_lock_list() and session_unlock_list().
3793 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3794 size_t session_count
, uid_t uid
, gid_t gid
)
3798 struct ltt_session
*session
;
3799 struct ltt_session_list
*list
= session_get_list();
3800 struct lttng_session_extended
*extended
=
3801 (typeof(extended
)) (&sessions
[session_count
]);
3803 DBG("Getting all available session for UID %d GID %d",
3806 * Iterate over session list and append data after the control struct in
3809 cds_list_for_each_entry(session
, &list
->head
, list
) {
3810 if (!session_get(session
)) {
3814 * Only list the sessions the user can control.
3816 if (!session_access_ok(session
, uid
, gid
) ||
3817 session
->destroyed
) {
3818 session_put(session
);
3822 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3823 struct ltt_ust_session
*usess
= session
->ust_session
;
3825 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3826 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3827 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3828 ret
= build_network_session_path(sessions
[i
].path
,
3829 sizeof(sessions
[i
].path
), session
);
3831 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3832 session
->consumer
->dst
.session_root_path
);
3835 PERROR("snprintf session path");
3836 session_put(session
);
3840 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3841 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3842 sessions
[i
].enabled
= session
->active
;
3843 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3844 sessions
[i
].live_timer_interval
= session
->live_timer
;
3845 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3846 extended
[i
].creation_time
.is_set
= 1;
3848 session_put(session
);
3853 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3854 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3856 int cmd_data_pending(struct ltt_session
*session
)
3859 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3860 struct ltt_ust_session
*usess
= session
->ust_session
;
3864 DBG("Data pending for session %s", session
->name
);
3866 /* Session MUST be stopped to ask for data availability. */
3867 if (session
->active
) {
3868 ret
= LTTNG_ERR_SESSION_STARTED
;
3872 * If stopped, just make sure we've started before else the above call
3873 * will always send that there is data pending.
3875 * The consumer assumes that when the data pending command is received,
3876 * the trace has been started before or else no output data is written
3877 * by the streams which is a condition for data pending. So, this is
3878 * *VERY* important that we don't ask the consumer before a start
3881 if (!session
->has_been_started
) {
3887 /* A rotation is still pending, we have to wait. */
3888 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3889 DBG("Rotate still pending for session %s", session
->name
);
3894 if (ksess
&& ksess
->consumer
) {
3895 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3897 /* Data is still being extracted for the kernel. */
3902 if (usess
&& usess
->consumer
) {
3903 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3905 /* Data is still being extracted for the kernel. */
3910 /* Data is ready to be read by a viewer */
3918 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3920 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3922 int cmd_snapshot_add_output(struct ltt_session
*session
,
3923 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3926 struct snapshot_output
*new_output
;
3931 DBG("Cmd snapshot add output for session %s", session
->name
);
3934 * Can't create an output if the session is not set in no-output mode.
3936 if (session
->output_traces
) {
3937 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3941 if (session
->has_non_mmap_channel
) {
3942 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3946 /* Only one output is allowed until we have the "tee" feature. */
3947 if (session
->snapshot
.nb_output
== 1) {
3948 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3952 new_output
= snapshot_output_alloc();
3954 ret
= LTTNG_ERR_NOMEM
;
3958 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3959 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3960 &session
->snapshot
);
3962 if (ret
== -ENOMEM
) {
3963 ret
= LTTNG_ERR_NOMEM
;
3965 ret
= LTTNG_ERR_INVALID
;
3971 snapshot_add_output(&session
->snapshot
, new_output
);
3973 *id
= new_output
->id
;
3980 snapshot_output_destroy(new_output
);
3986 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3988 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3990 int cmd_snapshot_del_output(struct ltt_session
*session
,
3991 const struct lttng_snapshot_output
*output
)
3994 struct snapshot_output
*sout
= NULL
;
4002 * Permission denied to create an output if the session is not
4003 * set in no output mode.
4005 if (session
->output_traces
) {
4006 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4011 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4013 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4014 } else if (*output
->name
!= '\0') {
4015 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4017 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4020 ret
= LTTNG_ERR_INVALID
;
4024 snapshot_delete_output(&session
->snapshot
, sout
);
4025 snapshot_output_destroy(sout
);
4034 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4036 * If no output is available, outputs is untouched and 0 is returned.
4038 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4040 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4041 struct lttng_snapshot_output
**outputs
)
4044 struct lttng_snapshot_output
*list
= NULL
;
4045 struct lttng_ht_iter iter
;
4046 struct snapshot_output
*output
;
4051 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4054 * Permission denied to create an output if the session is not
4055 * set in no output mode.
4057 if (session
->output_traces
) {
4058 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4062 if (session
->snapshot
.nb_output
== 0) {
4067 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4069 ret
= -LTTNG_ERR_NOMEM
;
4073 /* Copy list from session to the new list object. */
4075 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4076 output
, node
.node
) {
4077 assert(output
->consumer
);
4078 list
[idx
].id
= output
->id
;
4079 list
[idx
].max_size
= output
->max_size
;
4080 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4081 sizeof(list
[idx
].name
))) {
4082 ret
= -LTTNG_ERR_INVALID
;
4085 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4086 if (lttng_strncpy(list
[idx
].ctrl_url
,
4087 output
->consumer
->dst
.session_root_path
,
4088 sizeof(list
[idx
].ctrl_url
))) {
4089 ret
= -LTTNG_ERR_INVALID
;
4094 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4095 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4097 ret
= -LTTNG_ERR_NOMEM
;
4102 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4103 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4105 ret
= -LTTNG_ERR_NOMEM
;
4114 ret
= session
->snapshot
.nb_output
;
4123 * Check if we can regenerate the metadata for this session.
4124 * Only kernel, UST per-uid and non-live sessions are supported.
4126 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4129 int check_regenerate_metadata_support(struct ltt_session
*session
)
4135 if (session
->live_timer
!= 0) {
4136 ret
= LTTNG_ERR_LIVE_SESSION
;
4139 if (!session
->active
) {
4140 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4143 if (session
->ust_session
) {
4144 switch (session
->ust_session
->buffer_type
) {
4145 case LTTNG_BUFFER_PER_UID
:
4147 case LTTNG_BUFFER_PER_PID
:
4148 ret
= LTTNG_ERR_PER_PID_SESSION
;
4152 ret
= LTTNG_ERR_UNK
;
4156 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4157 session
->consumer
->relay_minor_version
< 8) {
4158 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4168 int clear_metadata_file(int fd
)
4173 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4174 if (lseek_ret
< 0) {
4180 ret
= ftruncate(fd
, 0);
4182 PERROR("ftruncate");
4191 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4194 struct buffer_reg_uid
*uid_reg
= NULL
;
4195 struct buffer_reg_session
*session_reg
= NULL
;
4198 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4199 struct ust_registry_session
*registry
;
4200 struct ust_registry_channel
*chan
;
4201 struct lttng_ht_iter iter_chan
;
4203 session_reg
= uid_reg
->registry
;
4204 registry
= session_reg
->reg
.ust
;
4206 pthread_mutex_lock(®istry
->lock
);
4207 registry
->metadata_len_sent
= 0;
4208 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4209 registry
->metadata_len
= 0;
4210 registry
->metadata_version
++;
4211 if (registry
->metadata_fd
> 0) {
4212 /* Clear the metadata file's content. */
4213 ret
= clear_metadata_file(registry
->metadata_fd
);
4215 pthread_mutex_unlock(®istry
->lock
);
4220 ret
= ust_metadata_session_statedump(registry
, NULL
,
4221 registry
->major
, registry
->minor
);
4223 pthread_mutex_unlock(®istry
->lock
);
4224 ERR("Failed to generate session metadata (err = %d)",
4228 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4230 struct ust_registry_event
*event
;
4231 struct lttng_ht_iter iter_event
;
4233 ret
= ust_metadata_channel_statedump(registry
, chan
);
4235 pthread_mutex_unlock(®istry
->lock
);
4236 ERR("Failed to generate channel metadata "
4240 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4242 ret
= ust_metadata_event_statedump(registry
,
4245 pthread_mutex_unlock(®istry
->lock
);
4246 ERR("Failed to generate event metadata "
4252 pthread_mutex_unlock(®istry
->lock
);
4261 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4263 * Ask the consumer to truncate the existing metadata file(s) and
4264 * then regenerate the metadata. Live and per-pid sessions are not
4265 * supported and return an error.
4267 * Return 0 on success or else a LTTNG_ERR code.
4269 int cmd_regenerate_metadata(struct ltt_session
*session
)
4275 ret
= check_regenerate_metadata_support(session
);
4280 if (session
->kernel_session
) {
4281 ret
= kernctl_session_regenerate_metadata(
4282 session
->kernel_session
->fd
);
4284 ERR("Failed to regenerate the kernel metadata");
4289 if (session
->ust_session
) {
4290 ret
= ust_regenerate_metadata(session
->ust_session
);
4292 ERR("Failed to regenerate the UST metadata");
4296 DBG("Cmd metadata regenerate for session %s", session
->name
);
4304 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4306 * Ask the tracer to regenerate a new statedump.
4308 * Return 0 on success or else a LTTNG_ERR code.
4310 int cmd_regenerate_statedump(struct ltt_session
*session
)
4316 if (!session
->active
) {
4317 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4321 if (session
->kernel_session
) {
4322 ret
= kernctl_session_regenerate_statedump(
4323 session
->kernel_session
->fd
);
4325 * Currently, the statedump in kernel can only fail if out
4329 if (ret
== -ENOMEM
) {
4330 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4332 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4334 ERR("Failed to regenerate the kernel statedump");
4339 if (session
->ust_session
) {
4340 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4342 * Currently, the statedump in UST always returns 0.
4345 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4346 ERR("Failed to regenerate the UST statedump");
4350 DBG("Cmd regenerate statedump for session %s", session
->name
);
4357 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4358 struct notification_thread_handle
*notification_thread
)
4362 ssize_t sock_recv_len
;
4363 struct lttng_trigger
*trigger
= NULL
;
4364 struct lttng_buffer_view view
;
4365 struct lttng_dynamic_buffer trigger_buffer
;
4367 lttng_dynamic_buffer_init(&trigger_buffer
);
4368 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4369 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4371 ret
= LTTNG_ERR_NOMEM
;
4375 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4377 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4378 ERR("Failed to receive \"register trigger\" command payload");
4379 /* TODO: should this be a new error enum ? */
4380 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4384 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4385 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4387 ERR("Invalid trigger payload received in \"register trigger\" command");
4388 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4392 ret
= notification_thread_command_register_trigger(notification_thread
,
4394 /* Ownership of trigger was transferred. */
4397 lttng_trigger_destroy(trigger
);
4398 lttng_dynamic_buffer_reset(&trigger_buffer
);
4402 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4403 struct notification_thread_handle
*notification_thread
)
4407 ssize_t sock_recv_len
;
4408 struct lttng_trigger
*trigger
= NULL
;
4409 struct lttng_buffer_view view
;
4410 struct lttng_dynamic_buffer trigger_buffer
;
4412 lttng_dynamic_buffer_init(&trigger_buffer
);
4413 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4414 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4416 ret
= LTTNG_ERR_NOMEM
;
4420 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4422 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4423 ERR("Failed to receive \"unregister trigger\" command payload");
4424 /* TODO: should this be a new error enum ? */
4425 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4429 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4430 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4432 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4433 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4437 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4440 lttng_trigger_destroy(trigger
);
4441 lttng_dynamic_buffer_reset(&trigger_buffer
);
4446 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4447 * snapshot output is *not* set with a remote destination.
4449 * Return LTTNG_OK on success or a LTTNG_ERR code.
4451 static enum lttng_error_code
set_relayd_for_snapshot(
4452 struct consumer_output
*output
,
4453 const struct ltt_session
*session
)
4455 enum lttng_error_code status
= LTTNG_OK
;
4456 struct lttng_ht_iter iter
;
4457 struct consumer_socket
*socket
;
4458 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4459 const char *base_path
;
4464 DBG2("Set relayd object from snapshot output");
4466 if (session
->current_trace_chunk
) {
4467 enum lttng_trace_chunk_status chunk_status
=
4468 lttng_trace_chunk_get_id(
4469 session
->current_trace_chunk
,
4470 ¤t_chunk_id
.value
);
4472 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4473 current_chunk_id
.is_set
= true;
4475 ERR("Failed to get current trace chunk id");
4476 status
= LTTNG_ERR_UNK
;
4481 /* Ignore if snapshot consumer output is not network. */
4482 if (output
->type
!= CONSUMER_DST_NET
) {
4487 * The snapshot record URI base path overrides the session
4490 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4491 base_path
= output
->dst
.net
.control
.subdir
;
4493 base_path
= session
->base_path
;
4497 * For each consumer socket, create and send the relayd object of the
4501 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4502 socket
, node
.node
) {
4503 pthread_mutex_lock(socket
->lock
);
4504 status
= send_consumer_relayd_sockets(0, session
->id
,
4506 session
->name
, session
->hostname
,
4508 session
->live_timer
,
4509 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4510 session
->creation_time
,
4511 session
->name_contains_creation_time
);
4512 pthread_mutex_unlock(socket
->lock
);
4513 if (status
!= LTTNG_OK
) {
4525 * Record a kernel snapshot.
4527 * Return LTTNG_OK on success or a LTTNG_ERR code.
4529 static enum lttng_error_code
record_kernel_snapshot(
4530 struct ltt_kernel_session
*ksess
,
4531 const struct consumer_output
*output
,
4532 const struct ltt_session
*session
,
4533 int wait
, uint64_t nb_packets_per_stream
)
4535 enum lttng_error_code status
;
4541 status
= kernel_snapshot_record(
4542 ksess
, output
, wait
, nb_packets_per_stream
);
4547 * Record a UST snapshot.
4549 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4551 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4552 const struct consumer_output
*output
,
4553 const struct ltt_session
*session
,
4554 int wait
, uint64_t nb_packets_per_stream
)
4556 enum lttng_error_code status
;
4562 status
= ust_app_snapshot_record(
4563 usess
, output
, wait
, nb_packets_per_stream
);
4568 uint64_t get_session_size_one_more_packet_per_stream(
4569 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4571 uint64_t tot_size
= 0;
4573 if (session
->kernel_session
) {
4574 struct ltt_kernel_channel
*chan
;
4575 const struct ltt_kernel_session
*ksess
=
4576 session
->kernel_session
;
4578 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4579 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4581 * Don't take channel into account if we
4582 * already grab all its packets.
4586 tot_size
+= chan
->channel
->attr
.subbuf_size
4587 * chan
->stream_count
;
4591 if (session
->ust_session
) {
4592 const struct ltt_ust_session
*usess
= session
->ust_session
;
4594 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4602 * Calculate the number of packets we can grab from each stream that
4603 * fits within the overall snapshot max size.
4605 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4606 * the number of packets per stream.
4608 * TODO: this approach is not perfect: we consider the worse case
4609 * (packet filling the sub-buffers) as an upper bound, but we could do
4610 * better if we do this calculation while we actually grab the packet
4611 * content: we would know how much padding we don't actually store into
4614 * This algorithm is currently bounded by the number of packets per
4617 * Since we call this algorithm before actually grabbing the data, it's
4618 * an approximation: for instance, applications could appear/disappear
4619 * in between this call and actually grabbing data.
4622 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4626 uint64_t cur_nb_packets
= 0;
4629 return 0; /* Infinite */
4632 size_left
= max_size
;
4634 uint64_t one_more_packet_tot_size
;
4636 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4637 session
, cur_nb_packets
);
4638 if (!one_more_packet_tot_size
) {
4639 /* We are already grabbing all packets. */
4642 size_left
-= one_more_packet_tot_size
;
4643 if (size_left
< 0) {
4648 if (!cur_nb_packets
&& size_left
!= max_size
) {
4649 /* Not enough room to grab one packet of each stream, error. */
4652 return cur_nb_packets
;
4656 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4657 const struct snapshot_output
*snapshot_output
, int wait
)
4659 int64_t nb_packets_per_stream
;
4660 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4662 enum lttng_error_code ret_code
= LTTNG_OK
;
4663 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4664 struct consumer_output
*original_ust_consumer_output
= NULL
;
4665 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4666 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4667 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4669 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4671 snapshot_output
->name
,
4672 snapshot_output
->datetime
,
4673 snapshot_output
->nb_snapshot
);
4674 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4675 ERR("Failed to format snapshot name");
4676 ret_code
= LTTNG_ERR_INVALID
;
4679 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4680 snapshot_output
->name
, session
->name
,
4681 snapshot_chunk_name
);
4682 if (!session
->kernel_session
&& !session
->ust_session
) {
4683 ERR("Failed to record snapshot as no channels exist");
4684 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4688 if (session
->kernel_session
) {
4689 original_kernel_consumer_output
=
4690 session
->kernel_session
->consumer
;
4691 snapshot_kernel_consumer_output
=
4692 consumer_copy_output(snapshot_output
->consumer
);
4693 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
4694 snapshot_chunk_name
);
4696 /* Copy the original domain subdir. */
4697 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
4698 original_kernel_consumer_output
->domain_subdir
);
4700 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4701 original_kernel_consumer_output
);
4703 ERR("Failed to copy consumer sockets from snapshot output configuration");
4704 ret_code
= LTTNG_ERR_NOMEM
;
4707 ret_code
= set_relayd_for_snapshot(
4708 snapshot_kernel_consumer_output
, session
);
4709 if (ret_code
!= LTTNG_OK
) {
4710 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4713 session
->kernel_session
->consumer
=
4714 snapshot_kernel_consumer_output
;
4716 if (session
->ust_session
) {
4717 original_ust_consumer_output
= session
->ust_session
->consumer
;
4718 snapshot_ust_consumer_output
=
4719 consumer_copy_output(snapshot_output
->consumer
);
4720 strcpy(snapshot_ust_consumer_output
->chunk_path
,
4721 snapshot_chunk_name
);
4723 /* Copy the original domain subdir. */
4724 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
4725 original_ust_consumer_output
->domain_subdir
);
4727 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4728 original_ust_consumer_output
);
4730 ERR("Failed to copy consumer sockets from snapshot output configuration");
4731 ret_code
= LTTNG_ERR_NOMEM
;
4734 ret_code
= set_relayd_for_snapshot(
4735 snapshot_ust_consumer_output
, session
);
4736 if (ret_code
!= LTTNG_OK
) {
4737 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4740 session
->ust_session
->consumer
=
4741 snapshot_ust_consumer_output
;
4744 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4745 snapshot_kernel_consumer_output
?:
4746 snapshot_ust_consumer_output
,
4747 consumer_output_get_base_path(
4748 snapshot_output
->consumer
),
4749 snapshot_chunk_name
);
4750 if (!snapshot_trace_chunk
) {
4751 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4753 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4756 assert(!session
->current_trace_chunk
);
4757 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4758 lttng_trace_chunk_put(snapshot_trace_chunk
);
4759 snapshot_trace_chunk
= NULL
;
4761 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4763 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4767 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4768 snapshot_output
->max_size
);
4769 if (nb_packets_per_stream
< 0) {
4770 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4771 goto error_close_trace_chunk
;
4774 if (session
->kernel_session
) {
4775 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4776 snapshot_kernel_consumer_output
, session
,
4777 wait
, nb_packets_per_stream
);
4778 if (ret_code
!= LTTNG_OK
) {
4779 goto error_close_trace_chunk
;
4783 if (session
->ust_session
) {
4784 ret_code
= record_ust_snapshot(session
->ust_session
,
4785 snapshot_ust_consumer_output
, session
,
4786 wait
, nb_packets_per_stream
);
4787 if (ret_code
!= LTTNG_OK
) {
4788 goto error_close_trace_chunk
;
4792 error_close_trace_chunk
:
4793 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
4794 ERR("Failed to release the current trace chunk of session \"%s\"",
4796 ret_code
= LTTNG_ERR_UNK
;
4799 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
4800 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
4802 * Don't goto end; make sure the chunk is closed for the session
4803 * to allow future snapshots.
4805 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4807 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4810 lttng_trace_chunk_put(snapshot_trace_chunk
);
4811 snapshot_trace_chunk
= NULL
;
4813 if (original_ust_consumer_output
) {
4814 session
->ust_session
->consumer
= original_ust_consumer_output
;
4816 if (original_kernel_consumer_output
) {
4817 session
->kernel_session
->consumer
=
4818 original_kernel_consumer_output
;
4820 consumer_output_put(snapshot_ust_consumer_output
);
4821 consumer_output_put(snapshot_kernel_consumer_output
);
4826 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4828 * The wait parameter is ignored so this call always wait for the snapshot to
4829 * complete before returning.
4831 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4833 int cmd_snapshot_record(struct ltt_session
*session
,
4834 const struct lttng_snapshot_output
*output
, int wait
)
4836 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4838 unsigned int snapshot_success
= 0;
4840 struct snapshot_output
*tmp_output
= NULL
;
4845 DBG("Cmd snapshot record for session %s", session
->name
);
4847 /* Get the datetime for the snapshot output directory. */
4848 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4851 cmd_ret
= LTTNG_ERR_INVALID
;
4856 * Permission denied to create an output if the session is not
4857 * set in no output mode.
4859 if (session
->output_traces
) {
4860 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4864 /* The session needs to be started at least once. */
4865 if (!session
->has_been_started
) {
4866 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4870 /* Use temporary output for the session. */
4871 if (*output
->ctrl_url
!= '\0') {
4872 tmp_output
= snapshot_output_alloc();
4874 cmd_ret
= LTTNG_ERR_NOMEM
;
4878 ret
= snapshot_output_init(session
, output
->max_size
,
4880 output
->ctrl_url
, output
->data_url
,
4884 if (ret
== -ENOMEM
) {
4885 cmd_ret
= LTTNG_ERR_NOMEM
;
4887 cmd_ret
= LTTNG_ERR_INVALID
;
4891 /* Use the global session count for the temporary snapshot. */
4892 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
4894 /* Use the global datetime */
4895 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
4896 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
4897 if (cmd_ret
!= LTTNG_OK
) {
4900 snapshot_success
= 1;
4902 struct snapshot_output
*sout
;
4903 struct lttng_ht_iter iter
;
4906 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4907 &iter
.iter
, sout
, node
.node
) {
4908 struct snapshot_output output_copy
;
4911 * Make a local copy of the output and override output
4912 * parameters with those provided as part of the
4915 memcpy(&output_copy
, sout
, sizeof(output_copy
));
4917 if (output
->max_size
!= (uint64_t) -1ULL) {
4918 output_copy
.max_size
= output
->max_size
;
4921 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4922 memcpy(output_copy
.datetime
, datetime
,
4925 /* Use temporary name. */
4926 if (*output
->name
!= '\0') {
4927 if (lttng_strncpy(output_copy
.name
,
4929 sizeof(output_copy
.name
))) {
4930 cmd_ret
= LTTNG_ERR_INVALID
;
4936 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
4937 if (cmd_ret
!= LTTNG_OK
) {
4941 snapshot_success
= 1;
4946 if (snapshot_success
) {
4947 session
->snapshot
.nb_snapshot
++;
4949 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4954 snapshot_output_destroy(tmp_output
);
4960 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4962 int cmd_set_session_shm_path(struct ltt_session
*session
,
4963 const char *shm_path
)
4969 * Can only set shm path before session is started.
4971 if (session
->has_been_started
) {
4972 return LTTNG_ERR_SESSION_STARTED
;
4975 strncpy(session
->shm_path
, shm_path
,
4976 sizeof(session
->shm_path
));
4977 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4983 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4985 * Ask the consumer to rotate the session output directory.
4986 * The session lock must be held.
4988 * Returns LTTNG_OK on success or else a negative LTTng error code.
4990 int cmd_rotate_session(struct ltt_session
*session
,
4991 struct lttng_rotate_session_return
*rotate_return
,
4992 bool quiet_rotation
,
4993 enum lttng_trace_chunk_command_type command
)
4996 uint64_t ongoing_rotation_chunk_id
;
4997 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4998 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
4999 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5000 enum lttng_trace_chunk_status chunk_status
;
5001 bool failed_to_rotate
= false;
5002 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5006 if (!session
->has_been_started
) {
5007 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5012 * Explicit rotation is not supported for live sessions.
5013 * However, live sessions can perform a quiet rotation on
5015 * Rotation is not supported for snapshot traces (no output).
5017 if ((!quiet_rotation
&& session
->live_timer
) ||
5018 !session
->output_traces
) {
5019 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5023 /* Unsupported feature in lttng-relayd before 2.11. */
5024 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5025 (session
->consumer
->relay_major_version
== 2 &&
5026 session
->consumer
->relay_minor_version
< 11)) {
5027 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5031 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5032 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5033 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5037 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5038 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5040 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5045 * After a stop, we only allow one rotation to occur, the other ones are
5046 * useless until a new start.
5048 if (session
->rotated_after_last_stop
) {
5049 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5051 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5056 * After a stop followed by a clear, disallow following rotations a they would
5057 * generate empty chunks.
5059 if (session
->cleared_after_last_stop
) {
5060 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5062 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5066 if (session
->active
) {
5067 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5069 if (!new_trace_chunk
) {
5070 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5076 * The current trace chunk becomes the chunk being archived.
5078 * After this point, "chunk_being_archived" must absolutely
5079 * be closed on the consumer(s), otherwise it will never be
5080 * cleaned-up, which will result in a leak.
5082 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5083 &chunk_being_archived
);
5085 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5089 if (session
->kernel_session
) {
5090 cmd_ret
= kernel_rotate_session(session
);
5091 if (cmd_ret
!= LTTNG_OK
) {
5092 failed_to_rotate
= true;
5093 rotation_fail_code
= cmd_ret
;
5096 if (session
->ust_session
) {
5097 cmd_ret
= ust_app_rotate_session(session
);
5098 if (cmd_ret
!= LTTNG_OK
) {
5099 failed_to_rotate
= true;
5100 rotation_fail_code
= cmd_ret
;
5104 if (!session
->active
) {
5105 session
->rotated_after_last_stop
= true;
5108 if (!chunk_being_archived
) {
5109 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5111 if (failed_to_rotate
) {
5112 cmd_ret
= rotation_fail_code
;
5119 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5120 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5121 &ongoing_rotation_chunk_id
);
5122 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5124 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5125 command
, session
->last_chunk_path
);
5127 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5131 if (failed_to_rotate
) {
5132 cmd_ret
= rotation_fail_code
;
5136 session
->quiet_rotation
= quiet_rotation
;
5137 ret
= timer_session_rotation_pending_check_start(session
,
5138 DEFAULT_ROTATE_PENDING_TIMER
);
5140 cmd_ret
= LTTNG_ERR_UNK
;
5144 if (rotate_return
) {
5145 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5148 session
->chunk_being_archived
= chunk_being_archived
;
5149 chunk_being_archived
= NULL
;
5150 if (!quiet_rotation
) {
5151 ret
= notification_thread_command_session_rotation_ongoing(
5152 notification_thread_handle
,
5153 session
->name
, session
->uid
, session
->gid
,
5154 ongoing_rotation_chunk_id
);
5155 if (ret
!= LTTNG_OK
) {
5156 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5162 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5163 session
->name
, ongoing_rotation_chunk_id
);
5165 lttng_trace_chunk_put(new_trace_chunk
);
5166 lttng_trace_chunk_put(chunk_being_archived
);
5167 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5170 if (session_reset_rotation_state(session
,
5171 LTTNG_ROTATION_STATE_ERROR
)) {
5172 ERR("Failed to reset rotation state of session \"%s\"",
5179 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5181 * Check if the session has finished its rotation.
5183 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5185 int cmd_rotate_get_info(struct ltt_session
*session
,
5186 struct lttng_rotation_get_info_return
*info_return
,
5187 uint64_t rotation_id
)
5189 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5190 enum lttng_rotation_state rotation_state
;
5192 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5193 session
->most_recent_chunk_id
.value
);
5195 if (session
->chunk_being_archived
) {
5196 enum lttng_trace_chunk_status chunk_status
;
5199 chunk_status
= lttng_trace_chunk_get_id(
5200 session
->chunk_being_archived
,
5202 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5204 rotation_state
= rotation_id
== chunk_id
?
5205 LTTNG_ROTATION_STATE_ONGOING
:
5206 LTTNG_ROTATION_STATE_EXPIRED
;
5208 if (session
->last_archived_chunk_id
.is_set
&&
5209 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5210 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5212 rotation_state
= session
->rotation_state
;
5216 switch (rotation_state
) {
5217 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5218 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5221 case LTTNG_ROTATION_STATE_EXPIRED
:
5222 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5223 rotation_id
, session
->name
);
5225 case LTTNG_ROTATION_STATE_ONGOING
:
5226 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5227 rotation_id
, session
->name
);
5229 case LTTNG_ROTATION_STATE_COMPLETED
:
5233 char *current_tracing_path_reply
;
5234 size_t current_tracing_path_reply_len
;
5236 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5237 rotation_id
, session
->name
);
5239 switch (session_get_consumer_destination_type(session
)) {
5240 case CONSUMER_DST_LOCAL
:
5241 current_tracing_path_reply
=
5242 info_return
->location
.local
.absolute_path
;
5243 current_tracing_path_reply_len
=
5244 sizeof(info_return
->location
.local
.absolute_path
);
5245 info_return
->location_type
=
5246 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5247 fmt_ret
= asprintf(&chunk_path
,
5248 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5249 session_get_base_path(session
),
5250 session
->last_archived_chunk_name
);
5251 if (fmt_ret
== -1) {
5252 PERROR("Failed to format the path of the last archived trace chunk");
5253 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5254 cmd_ret
= LTTNG_ERR_UNK
;
5258 case CONSUMER_DST_NET
:
5260 uint16_t ctrl_port
, data_port
;
5262 current_tracing_path_reply
=
5263 info_return
->location
.relay
.relative_path
;
5264 current_tracing_path_reply_len
=
5265 sizeof(info_return
->location
.relay
.relative_path
);
5266 /* Currently the only supported relay protocol. */
5267 info_return
->location
.relay
.protocol
=
5268 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5270 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5271 session_get_net_consumer_hostname(session
),
5272 sizeof(info_return
->location
.relay
.host
));
5274 ERR("Failed to copy host name to rotate_get_info reply");
5275 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5276 cmd_ret
= LTTNG_ERR_SET_URL
;
5280 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5281 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5282 info_return
->location
.relay
.ports
.data
= data_port
;
5283 info_return
->location_type
=
5284 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5285 chunk_path
= strdup(session
->last_chunk_path
);
5287 ERR("Failed to allocate the path of the last archived trace chunk");
5288 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5289 cmd_ret
= LTTNG_ERR_UNK
;
5298 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5299 chunk_path
, current_tracing_path_reply_len
);
5302 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5303 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5304 cmd_ret
= LTTNG_ERR_UNK
;
5310 case LTTNG_ROTATION_STATE_ERROR
:
5311 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5312 rotation_id
, session
->name
);
5320 info_return
->status
= (int32_t) rotation_state
;
5325 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5327 * Configure the automatic rotation parameters.
5328 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5329 * 'activate' to false means deactivate the rotation schedule and validate that
5330 * 'new_value' has the same value as the currently active value.
5332 * Return 0 on success or else a positive LTTNG_ERR code.
5334 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5335 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5337 struct notification_thread_handle
*notification_thread_handle
)
5340 uint64_t *parameter_value
;
5344 DBG("Cmd rotate set schedule session %s", session
->name
);
5346 if (session
->live_timer
|| !session
->output_traces
) {
5347 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5348 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5352 switch (schedule_type
) {
5353 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5354 parameter_value
= &session
->rotate_size
;
5356 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5357 parameter_value
= &session
->rotate_timer_period
;
5358 if (new_value
>= UINT_MAX
) {
5359 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5360 new_value
, UINT_MAX
);
5361 ret
= LTTNG_ERR_INVALID
;
5366 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5367 ret
= LTTNG_ERR_INVALID
;
5371 /* Improper use of the API. */
5372 if (new_value
== -1ULL) {
5373 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5374 ret
= LTTNG_ERR_INVALID
;
5379 * As indicated in struct ltt_session's comments, a value of == 0 means
5380 * this schedule rotation type is not in use.
5382 * Reject the command if we were asked to activate a schedule that was
5385 if (activate
&& *parameter_value
!= 0) {
5386 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5387 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5392 * Reject the command if we were asked to deactivate a schedule that was
5395 if (!activate
&& *parameter_value
== 0) {
5396 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5397 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5402 * Reject the command if we were asked to deactivate a schedule that
5405 if (!activate
&& *parameter_value
!= new_value
) {
5406 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5407 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5411 *parameter_value
= activate
? new_value
: 0;
5413 switch (schedule_type
) {
5414 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5415 if (activate
&& session
->active
) {
5417 * Only start the timer if the session is active,
5418 * otherwise it will be started when the session starts.
5420 ret
= timer_session_rotation_schedule_timer_start(
5421 session
, new_value
);
5423 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5424 ret
= LTTNG_ERR_UNK
;
5428 ret
= timer_session_rotation_schedule_timer_stop(
5431 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5432 ret
= LTTNG_ERR_UNK
;
5437 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5439 ret
= subscribe_session_consumed_size_rotation(session
,
5440 new_value
, notification_thread_handle
);
5442 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5443 ret
= LTTNG_ERR_UNK
;
5447 ret
= unsubscribe_session_consumed_size_rotation(session
,
5448 notification_thread_handle
);
5450 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5451 ret
= LTTNG_ERR_UNK
;
5458 /* Would have been caught before. */
5470 /* Wait for a given path to be removed before continuing. */
5471 static enum lttng_error_code
wait_on_path(void *path_data
)
5473 const char *shm_path
= path_data
;
5475 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5481 ret
= stat(shm_path
, &st
);
5483 if (errno
!= ENOENT
) {
5484 PERROR("stat() returned an error while checking for the existence of the shm path");
5486 DBG("shm path no longer exists, completing the destruction of session");
5490 if (!S_ISDIR(st
.st_mode
)) {
5491 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5496 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5502 * Returns a pointer to a handler to run on completion of a command.
5503 * Returns NULL if no handler has to be run for the last command executed.
5505 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5507 struct cmd_completion_handler
*handler
= current_completion_handler
;
5509 current_completion_handler
= NULL
;
5514 * Init command subsystem.
5519 * Set network sequence index to 1 for streams to match a relayd
5520 * socket on the consumer side.
5522 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5523 relayd_net_seq_idx
= 1;
5524 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5526 DBG("Command subsystem initialized");