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 "common/sessiond-comm/payload.h"
11 #include "lttng/lttng-error.h"
12 #include "lttng/tracker.h"
16 #include <urcu/list.h>
17 #include <urcu/uatomic.h>
21 #include <common/defaults.h>
22 #include <common/common.h>
23 #include <common/sessiond-comm/sessiond-comm.h>
24 #include <common/relayd/relayd.h>
25 #include <common/utils.h>
26 #include <common/compat/string.h>
27 #include <common/kernel-ctl/kernel-ctl.h>
28 #include <common/dynamic-buffer.h>
29 #include <common/buffer-view.h>
30 #include <common/sessiond-comm/payload.h>
31 #include <common/sessiond-comm/payload-view.h>
32 #include <common/trace-chunk.h>
33 #include <lttng/location-internal.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <lttng/condition/condition.h>
36 #include <lttng/action/action.h>
37 #include <lttng/channel.h>
38 #include <lttng/channel-internal.h>
39 #include <lttng/rotate-internal.h>
40 #include <lttng/location-internal.h>
41 #include <lttng/session-internal.h>
42 #include <lttng/userspace-probe-internal.h>
43 #include <lttng/session-descriptor-internal.h>
44 #include <common/string-utils/string-utils.h>
49 #include "health-sessiond.h"
51 #include "kernel-consumer.h"
52 #include "lttng-sessiond.h"
54 #include "lttng-syscall.h"
56 #include "buffer-registry.h"
57 #include "notification-thread.h"
58 #include "notification-thread-commands.h"
60 #include "rotation-thread.h"
62 #include "agent-thread.h"
66 /* Sleep for 100ms between each check for the shm path's deletion. */
67 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
69 struct cmd_destroy_session_reply_context
{
71 bool implicit_rotation_on_destroy
;
73 * Indicates whether or not an error occurred while launching the
74 * destruction of a session.
76 enum lttng_error_code destruction_status
;
79 static enum lttng_error_code
wait_on_path(void *path
);
82 * Command completion handler that is used by the destroy command
83 * when a session that has a non-default shm_path is being destroyed.
85 * See comment in cmd_destroy_session() for the rationale.
87 static struct destroy_completion_handler
{
88 struct cmd_completion_handler handler
;
89 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
90 } destroy_completion_handler
= {
93 .data
= destroy_completion_handler
.shm_path
98 static struct cmd_completion_handler
*current_completion_handler
;
101 * Used to keep a unique index for each relayd socket created where this value
102 * is associated with streams on the consumer so it can match the right relayd
103 * to send to. It must be accessed with the relayd_net_seq_idx_lock
106 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
107 static uint64_t relayd_net_seq_idx
;
109 static int validate_ust_event_name(const char *);
110 static int cmd_enable_event_internal(struct ltt_session
*session
,
111 const struct lttng_domain
*domain
,
112 char *channel_name
, struct lttng_event
*event
,
113 char *filter_expression
,
114 struct lttng_filter_bytecode
*filter
,
115 struct lttng_event_exclusion
*exclusion
,
119 * Create a session path used by list_lttng_sessions for the case that the
120 * session consumer is on the network.
122 static int build_network_session_path(char *dst
, size_t size
,
123 struct ltt_session
*session
)
125 int ret
, kdata_port
, udata_port
;
126 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
127 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
132 memset(tmp_urls
, 0, sizeof(tmp_urls
));
133 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
135 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
137 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
138 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
139 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
142 if (session
->ust_session
&& session
->ust_session
->consumer
) {
143 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
144 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
147 if (uuri
== NULL
&& kuri
== NULL
) {
148 uri
= &session
->consumer
->dst
.net
.control
;
149 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
150 } else if (kuri
&& uuri
) {
151 ret
= uri_compare(kuri
, uuri
);
155 /* Build uuri URL string */
156 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
163 } else if (kuri
&& uuri
== NULL
) {
165 } else if (uuri
&& kuri
== NULL
) {
169 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
175 * Do we have a UST url set. If yes, this means we have both kernel and UST
178 if (*tmp_uurl
!= '\0') {
179 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
180 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
183 if (kuri
|| (!kuri
&& !uuri
)) {
186 /* No kernel URI, use the UST port. */
189 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
197 * Get run-time attributes if the session has been started (discarded events,
200 static int get_kernel_runtime_stats(struct ltt_session
*session
,
201 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
202 uint64_t *lost_packets
)
206 if (!session
->has_been_started
) {
208 *discarded_events
= 0;
213 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
214 session
->kernel_session
->consumer
,
220 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
221 session
->kernel_session
->consumer
,
232 * Get run-time attributes if the session has been started (discarded events,
235 static int get_ust_runtime_stats(struct ltt_session
*session
,
236 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
237 uint64_t *lost_packets
)
240 struct ltt_ust_session
*usess
;
242 if (!discarded_events
|| !lost_packets
) {
247 usess
= session
->ust_session
;
248 assert(discarded_events
);
249 assert(lost_packets
);
251 if (!usess
|| !session
->has_been_started
) {
252 *discarded_events
= 0;
258 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
259 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
260 &usess
->buffer_reg_uid_list
,
261 usess
->consumer
, uchan
->id
,
262 uchan
->attr
.overwrite
,
265 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
266 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
267 uchan
, usess
->consumer
,
268 uchan
->attr
.overwrite
,
274 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
275 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
277 ERR("Unsupported buffer type");
288 * Fill lttng_channel array of all channels.
290 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
291 struct ltt_session
*session
, struct lttng_channel
*channels
,
292 struct lttng_channel_extended
*chan_exts
)
295 struct ltt_kernel_channel
*kchan
;
297 DBG("Listing channels for session %s", session
->name
);
300 case LTTNG_DOMAIN_KERNEL
:
301 /* Kernel channels */
302 if (session
->kernel_session
!= NULL
) {
303 cds_list_for_each_entry(kchan
,
304 &session
->kernel_session
->channel_list
.head
, list
) {
305 uint64_t discarded_events
, lost_packets
;
306 struct lttng_channel_extended
*extended
;
308 extended
= (struct lttng_channel_extended
*)
309 kchan
->channel
->attr
.extended
.ptr
;
311 ret
= get_kernel_runtime_stats(session
, kchan
,
312 &discarded_events
, &lost_packets
);
316 /* Copy lttng_channel struct to array */
317 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
318 channels
[i
].enabled
= kchan
->enabled
;
319 chan_exts
[i
].discarded_events
=
321 chan_exts
[i
].lost_packets
= lost_packets
;
322 chan_exts
[i
].monitor_timer_interval
=
323 extended
->monitor_timer_interval
;
324 chan_exts
[i
].blocking_timeout
= 0;
329 case LTTNG_DOMAIN_UST
:
331 struct lttng_ht_iter iter
;
332 struct ltt_ust_channel
*uchan
;
335 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
336 &iter
.iter
, uchan
, node
.node
) {
337 uint64_t discarded_events
= 0, lost_packets
= 0;
339 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
340 LTTNG_SYMBOL_NAME_LEN
)) {
343 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
344 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
345 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
346 channels
[i
].attr
.switch_timer_interval
=
347 uchan
->attr
.switch_timer_interval
;
348 channels
[i
].attr
.read_timer_interval
=
349 uchan
->attr
.read_timer_interval
;
350 channels
[i
].enabled
= uchan
->enabled
;
351 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
352 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
355 * Map enum lttng_ust_output to enum lttng_event_output.
357 switch (uchan
->attr
.output
) {
359 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
363 * LTTNG_UST_MMAP is the only supported UST
370 chan_exts
[i
].monitor_timer_interval
=
371 uchan
->monitor_timer_interval
;
372 chan_exts
[i
].blocking_timeout
=
373 uchan
->attr
.u
.s
.blocking_timeout
;
375 ret
= get_ust_runtime_stats(session
, uchan
,
376 &discarded_events
, &lost_packets
);
380 chan_exts
[i
].discarded_events
= discarded_events
;
381 chan_exts
[i
].lost_packets
= lost_packets
;
393 return -LTTNG_ERR_FATAL
;
399 static int increment_extended_len(const char *filter_expression
,
400 struct lttng_event_exclusion
*exclusion
,
401 const struct lttng_userspace_probe_location
*probe_location
,
402 size_t *extended_len
)
406 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
408 if (filter_expression
) {
409 *extended_len
+= strlen(filter_expression
) + 1;
413 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
416 if (probe_location
) {
417 ret
= lttng_userspace_probe_location_serialize(probe_location
,
422 *extended_len
+= ret
;
429 static int append_extended_info(const char *filter_expression
,
430 struct lttng_event_exclusion
*exclusion
,
431 struct lttng_userspace_probe_location
*probe_location
,
435 size_t filter_len
= 0;
436 size_t nb_exclusions
= 0;
437 size_t userspace_probe_location_len
= 0;
438 struct lttng_dynamic_buffer location_buffer
;
439 struct lttcomm_event_extended_header extended_header
;
441 if (filter_expression
) {
442 filter_len
= strlen(filter_expression
) + 1;
446 nb_exclusions
= exclusion
->count
;
449 if (probe_location
) {
450 lttng_dynamic_buffer_init(&location_buffer
);
451 ret
= lttng_userspace_probe_location_serialize(probe_location
,
452 &location_buffer
, NULL
);
457 userspace_probe_location_len
= location_buffer
.size
;
460 /* Set header fields */
461 extended_header
.filter_len
= filter_len
;
462 extended_header
.nb_exclusions
= nb_exclusions
;
463 extended_header
.userspace_probe_location_len
= userspace_probe_location_len
;
466 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
467 *extended_at
+= sizeof(extended_header
);
469 /* Copy filter string */
470 if (filter_expression
) {
471 memcpy(*extended_at
, filter_expression
, filter_len
);
472 *extended_at
+= filter_len
;
475 /* Copy exclusion names */
477 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
479 memcpy(*extended_at
, &exclusion
->names
, len
);
483 if (probe_location
) {
484 memcpy(*extended_at
, location_buffer
.data
, location_buffer
.size
);
485 *extended_at
+= location_buffer
.size
;
486 lttng_dynamic_buffer_reset(&location_buffer
);
494 * Create a list of agent domain events.
496 * Return number of events in list on success or else a negative value.
498 static int list_lttng_agent_events(struct agent
*agt
,
499 struct lttng_event
**events
, size_t *total_size
)
502 unsigned int nb_event
= 0;
503 struct agent_event
*event
;
504 struct lttng_event
*tmp_events
= NULL
;
505 struct lttng_ht_iter iter
;
506 size_t extended_len
= 0;
512 DBG3("Listing agent events");
515 nb_event
= lttng_ht_get_count(agt
->events
);
523 /* Compute required extended infos size */
524 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
527 * This is only valid because the commands which add events are
528 * processed in the same thread as the listing.
531 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
532 ret
= increment_extended_len(event
->filter_expression
, NULL
, NULL
,
535 DBG("Error computing the length of extended info message");
536 ret
= -LTTNG_ERR_FATAL
;
542 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
543 tmp_events
= zmalloc(*total_size
);
545 PERROR("zmalloc agent events session");
546 ret
= -LTTNG_ERR_FATAL
;
550 extended_at
= ((uint8_t *) tmp_events
) +
551 nb_event
* sizeof(struct lttng_event
);
554 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
555 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
556 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
557 tmp_events
[i
].enabled
= event
->enabled
;
558 tmp_events
[i
].loglevel
= event
->loglevel_value
;
559 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
562 /* Append extended info */
563 ret
= append_extended_info(event
->filter_expression
, NULL
, NULL
,
566 DBG("Error appending extended info message");
567 ret
= -LTTNG_ERR_FATAL
;
572 *events
= tmp_events
;
574 assert(nb_event
== i
);
585 * Create a list of ust global domain events.
587 static int list_lttng_ust_global_events(char *channel_name
,
588 struct ltt_ust_domain_global
*ust_global
,
589 struct lttng_event
**events
, size_t *total_size
)
592 unsigned int nb_event
= 0;
593 struct lttng_ht_iter iter
;
594 struct lttng_ht_node_str
*node
;
595 struct ltt_ust_channel
*uchan
;
596 struct ltt_ust_event
*uevent
;
597 struct lttng_event
*tmp
;
598 size_t extended_len
= 0;
601 DBG("Listing UST global events for channel %s", channel_name
);
605 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
606 node
= lttng_ht_iter_get_node_str(&iter
);
608 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
612 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
614 nb_event
= lttng_ht_get_count(uchan
->events
);
621 DBG3("Listing UST global %d events", nb_event
);
623 /* Compute required extended infos size */
624 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
625 if (uevent
->internal
) {
630 ret
= increment_extended_len(uevent
->filter_expression
,
631 uevent
->exclusion
, NULL
, &extended_len
);
633 DBG("Error computing the length of extended info message");
634 ret
= -LTTNG_ERR_FATAL
;
639 /* All events are internal, skip. */
645 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
646 tmp
= zmalloc(*total_size
);
648 ret
= -LTTNG_ERR_FATAL
;
652 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
654 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
655 if (uevent
->internal
) {
656 /* This event should remain hidden from clients */
659 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
660 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
661 tmp
[i
].enabled
= uevent
->enabled
;
663 switch (uevent
->attr
.instrumentation
) {
664 case LTTNG_UST_TRACEPOINT
:
665 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
667 case LTTNG_UST_PROBE
:
668 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
670 case LTTNG_UST_FUNCTION
:
671 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
675 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
676 switch (uevent
->attr
.loglevel_type
) {
677 case LTTNG_UST_LOGLEVEL_ALL
:
678 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
680 case LTTNG_UST_LOGLEVEL_RANGE
:
681 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
683 case LTTNG_UST_LOGLEVEL_SINGLE
:
684 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
687 if (uevent
->filter
) {
690 if (uevent
->exclusion
) {
691 tmp
[i
].exclusion
= 1;
695 /* Append extended info */
696 ret
= append_extended_info(uevent
->filter_expression
,
697 uevent
->exclusion
, NULL
, &extended_at
);
699 DBG("Error appending extended info message");
700 ret
= -LTTNG_ERR_FATAL
;
713 * Fill lttng_event array of all kernel events in the channel.
715 static int list_lttng_kernel_events(char *channel_name
,
716 struct ltt_kernel_session
*kernel_session
,
717 struct lttng_event
**events
, size_t *total_size
)
720 unsigned int nb_event
;
721 struct ltt_kernel_event
*event
;
722 struct ltt_kernel_channel
*kchan
;
723 size_t extended_len
= 0;
726 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
728 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
732 nb_event
= kchan
->event_count
;
734 DBG("Listing events for channel %s", kchan
->channel
->name
);
742 /* Compute required extended infos size */
743 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
744 ret
= increment_extended_len(event
->filter_expression
, NULL
,
745 event
->userspace_probe_location
,
748 DBG("Error computing the length of extended info message");
749 ret
= -LTTNG_ERR_FATAL
;
754 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
755 *events
= zmalloc(*total_size
);
756 if (*events
== NULL
) {
757 ret
= -LTTNG_ERR_FATAL
;
761 extended_at
= ((void *) *events
) +
762 nb_event
* sizeof(struct lttng_event
);
764 /* Kernel channels */
765 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
766 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
767 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
768 (*events
)[i
].enabled
= event
->enabled
;
769 (*events
)[i
].filter
=
770 (unsigned char) !!event
->filter_expression
;
772 switch (event
->event
->instrumentation
) {
773 case LTTNG_KERNEL_TRACEPOINT
:
774 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
776 case LTTNG_KERNEL_KRETPROBE
:
777 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
778 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
779 sizeof(struct lttng_kernel_kprobe
));
781 case LTTNG_KERNEL_KPROBE
:
782 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
783 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
784 sizeof(struct lttng_kernel_kprobe
));
786 case LTTNG_KERNEL_UPROBE
:
787 (*events
)[i
].type
= LTTNG_EVENT_USERSPACE_PROBE
;
789 case LTTNG_KERNEL_FUNCTION
:
790 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
791 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
792 sizeof(struct lttng_kernel_function
));
794 case LTTNG_KERNEL_NOOP
:
795 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
797 case LTTNG_KERNEL_SYSCALL
:
798 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
800 case LTTNG_KERNEL_ALL
:
808 /* Append extended info */
809 ret
= append_extended_info(event
->filter_expression
, NULL
,
810 event
->userspace_probe_location
, &extended_at
);
812 DBG("Error appending extended info message");
813 ret
= -LTTNG_ERR_FATAL
;
826 * Add URI so the consumer output object. Set the correct path depending on the
827 * domain adding the default trace directory.
829 static enum lttng_error_code
add_uri_to_consumer(
830 const struct ltt_session
*session
,
831 struct consumer_output
*consumer
,
832 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
835 enum lttng_error_code ret_code
= LTTNG_OK
;
839 if (consumer
== NULL
) {
840 DBG("No consumer detected. Don't add URI. Stopping.");
841 ret_code
= LTTNG_ERR_NO_CONSUMER
;
846 case LTTNG_DOMAIN_KERNEL
:
847 ret
= lttng_strncpy(consumer
->domain_subdir
,
848 DEFAULT_KERNEL_TRACE_DIR
,
849 sizeof(consumer
->domain_subdir
));
851 case LTTNG_DOMAIN_UST
:
852 ret
= lttng_strncpy(consumer
->domain_subdir
,
853 DEFAULT_UST_TRACE_DIR
,
854 sizeof(consumer
->domain_subdir
));
858 * This case is possible is we try to add the URI to the global
859 * tracing session consumer object which in this case there is
862 memset(consumer
->domain_subdir
, 0,
863 sizeof(consumer
->domain_subdir
));
867 ERR("Failed to initialize consumer output domain subdirectory");
868 ret_code
= LTTNG_ERR_FATAL
;
872 switch (uri
->dtype
) {
875 DBG2("Setting network URI to consumer");
877 if (consumer
->type
== CONSUMER_DST_NET
) {
878 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
879 consumer
->dst
.net
.control_isset
) ||
880 (uri
->stype
== LTTNG_STREAM_DATA
&&
881 consumer
->dst
.net
.data_isset
)) {
882 ret_code
= LTTNG_ERR_URL_EXIST
;
886 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
889 /* Set URI into consumer output object */
890 ret
= consumer_set_network_uri(session
, consumer
, uri
);
894 } else if (ret
== 1) {
896 * URI was the same in the consumer so we do not append the subdir
897 * again so to not duplicate output dir.
904 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
905 ret_code
= LTTNG_ERR_INVALID
;
908 DBG2("Setting trace directory path from URI to %s",
910 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
912 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
914 sizeof(consumer
->dst
.session_root_path
));
916 ret_code
= LTTNG_ERR_FATAL
;
919 consumer
->type
= CONSUMER_DST_LOCAL
;
929 * Init tracing by creating trace directory and sending fds kernel consumer.
931 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
934 struct lttng_ht_iter iter
;
935 struct consumer_socket
*socket
;
941 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
942 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
944 pthread_mutex_lock(socket
->lock
);
945 ret
= kernel_consumer_send_session(socket
, session
);
946 pthread_mutex_unlock(socket
->lock
);
948 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
960 * Create a socket to the relayd using the URI.
962 * On success, the relayd_sock pointer is set to the created socket.
963 * Else, it remains untouched and an LTTng error code is returned.
965 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
966 struct lttcomm_relayd_sock
**relayd_sock
,
967 struct consumer_output
*consumer
)
970 enum lttng_error_code status
= LTTNG_OK
;
971 struct lttcomm_relayd_sock
*rsock
;
973 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
974 RELAYD_VERSION_COMM_MINOR
);
976 status
= LTTNG_ERR_FATAL
;
981 * Connect to relayd so we can proceed with a session creation. This call
982 * can possibly block for an arbitrary amount of time to set the health
983 * state to be in poll execution.
986 ret
= relayd_connect(rsock
);
989 ERR("Unable to reach lttng-relayd");
990 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
994 /* Create socket for control stream. */
995 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
996 uint64_t result_flags
;
998 DBG3("Creating relayd stream socket from URI");
1000 /* Check relayd version */
1001 ret
= relayd_version_check(rsock
);
1002 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
1003 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
1005 } else if (ret
< 0) {
1006 ERR("Unable to reach lttng-relayd");
1007 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1010 consumer
->relay_major_version
= rsock
->major
;
1011 consumer
->relay_minor_version
= rsock
->minor
;
1012 ret
= relayd_get_configuration(rsock
, 0,
1015 ERR("Unable to get relayd configuration");
1016 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1019 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
1020 consumer
->relay_allows_clear
= true;
1022 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1023 DBG3("Creating relayd data socket from URI");
1025 /* Command is not valid */
1026 ERR("Relayd invalid stream type: %d", uri
->stype
);
1027 status
= LTTNG_ERR_INVALID
;
1031 *relayd_sock
= rsock
;
1036 /* The returned value is not useful since we are on an error path. */
1037 (void) relayd_close(rsock
);
1045 * Connect to the relayd using URI and send the socket to the right consumer.
1047 * The consumer socket lock must be held by the caller.
1049 * Returns LTTNG_OK on success or an LTTng error code on failure.
1051 static enum lttng_error_code
send_consumer_relayd_socket(
1052 unsigned int session_id
,
1053 struct lttng_uri
*relayd_uri
,
1054 struct consumer_output
*consumer
,
1055 struct consumer_socket
*consumer_sock
,
1056 const char *session_name
, const char *hostname
,
1057 const char *base_path
, int session_live_timer
,
1058 const uint64_t *current_chunk_id
,
1059 time_t session_creation_time
,
1060 bool session_name_contains_creation_time
)
1063 struct lttcomm_relayd_sock
*rsock
= NULL
;
1064 enum lttng_error_code status
;
1066 /* Connect to relayd and make version check if uri is the control. */
1067 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1068 if (status
!= LTTNG_OK
) {
1069 goto relayd_comm_error
;
1073 /* Set the network sequence index if not set. */
1074 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1075 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1077 * Increment net_seq_idx because we are about to transfer the
1078 * new relayd socket to the consumer.
1079 * Assign unique key so the consumer can match streams.
1081 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1082 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1085 /* Send relayd socket to consumer. */
1086 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1087 relayd_uri
->stype
, session_id
,
1088 session_name
, hostname
, base_path
,
1089 session_live_timer
, current_chunk_id
,
1090 session_creation_time
, session_name_contains_creation_time
);
1092 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1096 /* Flag that the corresponding socket was sent. */
1097 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1098 consumer_sock
->control_sock_sent
= 1;
1099 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1100 consumer_sock
->data_sock_sent
= 1;
1104 * Close socket which was dup on the consumer side. The session daemon does
1105 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1109 if (status
!= LTTNG_OK
) {
1111 * The consumer output for this session should not be used anymore
1112 * since the relayd connection failed thus making any tracing or/and
1113 * streaming not usable.
1115 consumer
->enabled
= 0;
1117 (void) relayd_close(rsock
);
1125 * Send both relayd sockets to a specific consumer and domain. This is a
1126 * helper function to facilitate sending the information to the consumer for a
1129 * The consumer socket lock must be held by the caller.
1131 * Returns LTTNG_OK, or an LTTng error code on failure.
1133 static enum lttng_error_code
send_consumer_relayd_sockets(
1134 enum lttng_domain_type domain
,
1135 unsigned int session_id
, struct consumer_output
*consumer
,
1136 struct consumer_socket
*sock
, const char *session_name
,
1137 const char *hostname
, const char *base_path
, int session_live_timer
,
1138 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1139 bool session_name_contains_creation_time
)
1141 enum lttng_error_code status
= LTTNG_OK
;
1146 /* Sending control relayd socket. */
1147 if (!sock
->control_sock_sent
) {
1148 status
= send_consumer_relayd_socket(session_id
,
1149 &consumer
->dst
.net
.control
, consumer
, sock
,
1150 session_name
, hostname
, base_path
, session_live_timer
,
1151 current_chunk_id
, session_creation_time
,
1152 session_name_contains_creation_time
);
1153 if (status
!= LTTNG_OK
) {
1158 /* Sending data relayd socket. */
1159 if (!sock
->data_sock_sent
) {
1160 status
= send_consumer_relayd_socket(session_id
,
1161 &consumer
->dst
.net
.data
, consumer
, sock
,
1162 session_name
, hostname
, base_path
, session_live_timer
,
1163 current_chunk_id
, session_creation_time
,
1164 session_name_contains_creation_time
);
1165 if (status
!= LTTNG_OK
) {
1175 * Setup relayd connections for a tracing session. First creates the socket to
1176 * the relayd and send them to the right domain consumer. Consumer type MUST be
1179 int cmd_setup_relayd(struct ltt_session
*session
)
1182 struct ltt_ust_session
*usess
;
1183 struct ltt_kernel_session
*ksess
;
1184 struct consumer_socket
*socket
;
1185 struct lttng_ht_iter iter
;
1186 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1190 usess
= session
->ust_session
;
1191 ksess
= session
->kernel_session
;
1193 DBG("Setting relayd for session %s", session
->name
);
1196 if (session
->current_trace_chunk
) {
1197 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1198 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1200 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1201 current_chunk_id
.is_set
= true;
1203 ERR("Failed to get current trace chunk id");
1204 ret
= LTTNG_ERR_UNK
;
1209 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1210 && usess
->consumer
->enabled
) {
1211 /* For each consumer socket, send relayd sockets */
1212 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1213 socket
, node
.node
) {
1214 pthread_mutex_lock(socket
->lock
);
1215 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1216 usess
->consumer
, socket
,
1217 session
->name
, session
->hostname
,
1219 session
->live_timer
,
1220 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1221 session
->creation_time
,
1222 session
->name_contains_creation_time
);
1223 pthread_mutex_unlock(socket
->lock
);
1224 if (ret
!= LTTNG_OK
) {
1227 /* Session is now ready for network streaming. */
1228 session
->net_handle
= 1;
1230 session
->consumer
->relay_major_version
=
1231 usess
->consumer
->relay_major_version
;
1232 session
->consumer
->relay_minor_version
=
1233 usess
->consumer
->relay_minor_version
;
1234 session
->consumer
->relay_allows_clear
=
1235 usess
->consumer
->relay_allows_clear
;
1238 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1239 && ksess
->consumer
->enabled
) {
1240 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1241 socket
, node
.node
) {
1242 pthread_mutex_lock(socket
->lock
);
1243 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1244 ksess
->consumer
, socket
,
1245 session
->name
, session
->hostname
,
1247 session
->live_timer
,
1248 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1249 session
->creation_time
,
1250 session
->name_contains_creation_time
);
1251 pthread_mutex_unlock(socket
->lock
);
1252 if (ret
!= LTTNG_OK
) {
1255 /* Session is now ready for network streaming. */
1256 session
->net_handle
= 1;
1258 session
->consumer
->relay_major_version
=
1259 ksess
->consumer
->relay_major_version
;
1260 session
->consumer
->relay_minor_version
=
1261 ksess
->consumer
->relay_minor_version
;
1262 session
->consumer
->relay_allows_clear
=
1263 ksess
->consumer
->relay_allows_clear
;
1272 * Start a kernel session by opening all necessary streams.
1274 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1277 struct ltt_kernel_channel
*kchan
;
1279 /* Open kernel metadata */
1280 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1281 ret
= kernel_open_metadata(ksess
);
1283 ret
= LTTNG_ERR_KERN_META_FAIL
;
1288 /* Open kernel metadata stream */
1289 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1290 ret
= kernel_open_metadata_stream(ksess
);
1292 ERR("Kernel create metadata stream failed");
1293 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1298 /* For each channel */
1299 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1300 if (kchan
->stream_count
== 0) {
1301 ret
= kernel_open_channel_stream(kchan
);
1303 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1306 /* Update the stream global counter */
1307 ksess
->stream_count_global
+= ret
;
1311 /* Setup kernel consumer socket and send fds to it */
1312 ret
= init_kernel_tracing(ksess
);
1314 ret
= LTTNG_ERR_KERN_START_FAIL
;
1318 /* This start the kernel tracing */
1319 ret
= kernel_start_session(ksess
);
1321 ret
= LTTNG_ERR_KERN_START_FAIL
;
1325 /* Quiescent wait after starting trace */
1326 kernel_wait_quiescent();
1336 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1338 struct ltt_kernel_channel
*kchan
;
1339 bool error_occurred
= false;
1342 if (!ksess
|| !ksess
->active
) {
1345 DBG("Stopping kernel tracing");
1347 ret
= kernel_stop_session(ksess
);
1349 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1353 kernel_wait_quiescent();
1355 /* Flush metadata after stopping (if exists) */
1356 if (ksess
->metadata_stream_fd
>= 0) {
1357 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1359 ERR("Kernel metadata flush failed");
1360 error_occurred
= true;
1364 /* Flush all buffers after stopping */
1365 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1366 ret
= kernel_flush_buffer(kchan
);
1368 ERR("Kernel flush buffer error");
1369 error_occurred
= true;
1374 if (error_occurred
) {
1375 ret
= LTTNG_ERR_UNK
;
1384 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1386 int cmd_disable_channel(struct ltt_session
*session
,
1387 enum lttng_domain_type domain
, char *channel_name
)
1390 struct ltt_ust_session
*usess
;
1392 usess
= session
->ust_session
;
1397 case LTTNG_DOMAIN_KERNEL
:
1399 ret
= channel_kernel_disable(session
->kernel_session
,
1401 if (ret
!= LTTNG_OK
) {
1405 kernel_wait_quiescent();
1408 case LTTNG_DOMAIN_UST
:
1410 struct ltt_ust_channel
*uchan
;
1411 struct lttng_ht
*chan_ht
;
1413 chan_ht
= usess
->domain_global
.channels
;
1415 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1416 if (uchan
== NULL
) {
1417 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1421 ret
= channel_ust_disable(usess
, uchan
);
1422 if (ret
!= LTTNG_OK
) {
1428 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1440 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1442 * The wpipe arguments is used as a notifier for the kernel thread.
1444 int cmd_enable_channel(struct ltt_session
*session
,
1445 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1448 struct ltt_ust_session
*usess
= session
->ust_session
;
1449 struct lttng_ht
*chan_ht
;
1451 struct lttng_channel attr
;
1458 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1460 /* Validate channel name */
1461 if (attr
.name
[0] == '.' ||
1462 memchr(attr
.name
, '/', len
) != NULL
) {
1463 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1467 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1472 * Don't try to enable a channel if the session has been started at
1473 * some point in time before. The tracer does not allow it.
1475 if (session
->has_been_started
) {
1476 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1481 * If the session is a live session, remove the switch timer, the
1482 * live timer does the same thing but sends also synchronisation
1483 * beacons for inactive streams.
1485 if (session
->live_timer
> 0) {
1486 attr
.attr
.live_timer_interval
= session
->live_timer
;
1487 attr
.attr
.switch_timer_interval
= 0;
1490 /* Check for feature support */
1491 switch (domain
->type
) {
1492 case LTTNG_DOMAIN_KERNEL
:
1494 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1495 /* Sampling position of buffer is not supported */
1496 WARN("Kernel tracer does not support buffer monitoring. "
1497 "Setting the monitor interval timer to 0 "
1498 "(disabled) for channel '%s' of session '%s'",
1499 attr
.name
, session
->name
);
1500 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1504 case LTTNG_DOMAIN_UST
:
1506 case LTTNG_DOMAIN_JUL
:
1507 case LTTNG_DOMAIN_LOG4J
:
1508 case LTTNG_DOMAIN_PYTHON
:
1509 if (!agent_tracing_is_enabled()) {
1510 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1511 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1516 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1520 switch (domain
->type
) {
1521 case LTTNG_DOMAIN_KERNEL
:
1523 struct ltt_kernel_channel
*kchan
;
1525 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1526 session
->kernel_session
);
1527 if (kchan
== NULL
) {
1528 if (session
->snapshot
.nb_output
> 0 ||
1529 session
->snapshot_mode
) {
1530 /* Enforce mmap output for snapshot sessions. */
1531 attr
.attr
.output
= LTTNG_EVENT_MMAP
;
1533 ret
= channel_kernel_create(session
->kernel_session
, &attr
, wpipe
);
1534 if (attr
.name
[0] != '\0') {
1535 session
->kernel_session
->has_non_default_channel
= 1;
1538 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1541 if (ret
!= LTTNG_OK
) {
1545 kernel_wait_quiescent();
1548 case LTTNG_DOMAIN_UST
:
1549 case LTTNG_DOMAIN_JUL
:
1550 case LTTNG_DOMAIN_LOG4J
:
1551 case LTTNG_DOMAIN_PYTHON
:
1553 struct ltt_ust_channel
*uchan
;
1558 * Current agent implementation limitations force us to allow
1559 * only one channel at once in "agent" subdomains. Each
1560 * subdomain has a default channel name which must be strictly
1563 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1564 if (strncmp(attr
.name
, DEFAULT_JUL_CHANNEL_NAME
,
1565 LTTNG_SYMBOL_NAME_LEN
)) {
1566 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1569 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1570 if (strncmp(attr
.name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1571 LTTNG_SYMBOL_NAME_LEN
)) {
1572 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1575 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1576 if (strncmp(attr
.name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1577 LTTNG_SYMBOL_NAME_LEN
)) {
1578 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1583 chan_ht
= usess
->domain_global
.channels
;
1585 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1586 if (uchan
== NULL
) {
1587 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1588 if (attr
.name
[0] != '\0') {
1589 usess
->has_non_default_channel
= 1;
1592 ret
= channel_ust_enable(usess
, uchan
);
1597 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1601 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1602 session
->has_non_mmap_channel
= true;
1610 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1611 struct ltt_session
*session
,
1612 enum lttng_domain_type domain
,
1613 enum lttng_process_attr process_attr
,
1614 enum lttng_tracking_policy
*policy
)
1616 enum lttng_error_code ret_code
= LTTNG_OK
;
1617 const struct process_attr_tracker
*tracker
;
1620 case LTTNG_DOMAIN_KERNEL
:
1621 if (!session
->kernel_session
) {
1622 ret_code
= LTTNG_ERR_INVALID
;
1625 tracker
= kernel_get_process_attr_tracker(
1626 session
->kernel_session
, process_attr
);
1628 case LTTNG_DOMAIN_UST
:
1629 if (!session
->ust_session
) {
1630 ret_code
= LTTNG_ERR_INVALID
;
1633 tracker
= trace_ust_get_process_attr_tracker(
1634 session
->ust_session
, process_attr
);
1637 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1641 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1643 ret_code
= LTTNG_ERR_INVALID
;
1649 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1650 struct ltt_session
*session
,
1651 enum lttng_domain_type domain
,
1652 enum lttng_process_attr process_attr
,
1653 enum lttng_tracking_policy policy
)
1655 enum lttng_error_code ret_code
= LTTNG_OK
;
1658 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1659 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1660 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1663 ret_code
= LTTNG_ERR_INVALID
;
1668 case LTTNG_DOMAIN_KERNEL
:
1669 if (!session
->kernel_session
) {
1670 ret_code
= LTTNG_ERR_INVALID
;
1673 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1674 session
->kernel_session
, process_attr
, policy
);
1676 case LTTNG_DOMAIN_UST
:
1677 if (!session
->ust_session
) {
1678 ret_code
= LTTNG_ERR_INVALID
;
1681 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1682 session
->ust_session
, process_attr
, policy
);
1685 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1692 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1693 struct ltt_session
*session
,
1694 enum lttng_domain_type domain
,
1695 enum lttng_process_attr process_attr
,
1696 const struct process_attr_value
*value
)
1698 enum lttng_error_code ret_code
= LTTNG_OK
;
1701 case LTTNG_DOMAIN_KERNEL
:
1702 if (!session
->kernel_session
) {
1703 ret_code
= LTTNG_ERR_INVALID
;
1706 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1707 session
->kernel_session
, process_attr
, value
);
1709 case LTTNG_DOMAIN_UST
:
1710 if (!session
->ust_session
) {
1711 ret_code
= LTTNG_ERR_INVALID
;
1714 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1715 session
->ust_session
, process_attr
, value
);
1718 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1725 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1726 struct ltt_session
*session
,
1727 enum lttng_domain_type domain
,
1728 enum lttng_process_attr process_attr
,
1729 const struct process_attr_value
*value
)
1731 enum lttng_error_code ret_code
= LTTNG_OK
;
1734 case LTTNG_DOMAIN_KERNEL
:
1735 if (!session
->kernel_session
) {
1736 ret_code
= LTTNG_ERR_INVALID
;
1739 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1740 session
->kernel_session
, process_attr
, value
);
1742 case LTTNG_DOMAIN_UST
:
1743 if (!session
->ust_session
) {
1744 ret_code
= LTTNG_ERR_INVALID
;
1747 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1748 session
->ust_session
, process_attr
, value
);
1751 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1758 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1759 struct ltt_session
*session
,
1760 enum lttng_domain_type domain
,
1761 enum lttng_process_attr process_attr
,
1762 struct lttng_process_attr_values
**values
)
1764 enum lttng_error_code ret_code
= LTTNG_OK
;
1765 const struct process_attr_tracker
*tracker
;
1766 enum process_attr_tracker_status status
;
1769 case LTTNG_DOMAIN_KERNEL
:
1770 if (!session
->kernel_session
) {
1771 ret_code
= LTTNG_ERR_INVALID
;
1774 tracker
= kernel_get_process_attr_tracker(
1775 session
->kernel_session
, process_attr
);
1777 case LTTNG_DOMAIN_UST
:
1778 if (!session
->ust_session
) {
1779 ret_code
= LTTNG_ERR_INVALID
;
1782 tracker
= trace_ust_get_process_attr_tracker(
1783 session
->ust_session
, process_attr
);
1786 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1791 ret_code
= LTTNG_ERR_INVALID
;
1795 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1797 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1798 ret_code
= LTTNG_OK
;
1800 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1801 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1803 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1804 ret_code
= LTTNG_ERR_NOMEM
;
1807 ret_code
= LTTNG_ERR_UNK
;
1816 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1818 int cmd_disable_event(struct ltt_session
*session
,
1819 enum lttng_domain_type domain
, const char *channel_name
,
1820 const struct lttng_event
*event
)
1823 const char *event_name
;
1825 DBG("Disable event command for event \'%s\'", event
->name
);
1827 event_name
= event
->name
;
1829 /* Error out on unhandled search criteria */
1830 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1831 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1832 ret
= LTTNG_ERR_UNK
;
1839 case LTTNG_DOMAIN_KERNEL
:
1841 struct ltt_kernel_channel
*kchan
;
1842 struct ltt_kernel_session
*ksess
;
1844 ksess
= session
->kernel_session
;
1847 * If a non-default channel has been created in the
1848 * session, explicitely require that -c chan_name needs
1851 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1852 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1856 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1857 if (kchan
== NULL
) {
1858 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1862 switch (event
->type
) {
1863 case LTTNG_EVENT_ALL
:
1864 case LTTNG_EVENT_TRACEPOINT
:
1865 case LTTNG_EVENT_SYSCALL
:
1866 case LTTNG_EVENT_PROBE
:
1867 case LTTNG_EVENT_FUNCTION
:
1868 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1869 if (event_name
[0] == '\0') {
1870 ret
= event_kernel_disable_event(kchan
,
1873 ret
= event_kernel_disable_event(kchan
,
1874 event_name
, event
->type
);
1876 if (ret
!= LTTNG_OK
) {
1881 ret
= LTTNG_ERR_UNK
;
1885 kernel_wait_quiescent();
1888 case LTTNG_DOMAIN_UST
:
1890 struct ltt_ust_channel
*uchan
;
1891 struct ltt_ust_session
*usess
;
1893 usess
= session
->ust_session
;
1895 if (validate_ust_event_name(event_name
)) {
1896 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1901 * If a non-default channel has been created in the
1902 * session, explicitly require that -c chan_name needs
1905 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1906 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1910 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1912 if (uchan
== NULL
) {
1913 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1917 switch (event
->type
) {
1918 case LTTNG_EVENT_ALL
:
1920 * An empty event name means that everything
1921 * should be disabled.
1923 if (event
->name
[0] == '\0') {
1924 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1926 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1929 if (ret
!= LTTNG_OK
) {
1934 ret
= LTTNG_ERR_UNK
;
1938 DBG3("Disable UST event %s in channel %s completed", event_name
,
1942 case LTTNG_DOMAIN_LOG4J
:
1943 case LTTNG_DOMAIN_JUL
:
1944 case LTTNG_DOMAIN_PYTHON
:
1947 struct ltt_ust_session
*usess
= session
->ust_session
;
1951 switch (event
->type
) {
1952 case LTTNG_EVENT_ALL
:
1955 ret
= LTTNG_ERR_UNK
;
1959 agt
= trace_ust_find_agent(usess
, domain
);
1961 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1965 * An empty event name means that everything
1966 * should be disabled.
1968 if (event
->name
[0] == '\0') {
1969 ret
= event_agent_disable_all(usess
, agt
);
1971 ret
= event_agent_disable(usess
, agt
, event_name
);
1973 if (ret
!= LTTNG_OK
) {
1980 ret
= LTTNG_ERR_UND
;
1993 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1995 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1996 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1998 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1999 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
2002 * Don't try to add a context if the session has been started at
2003 * some point in time before. The tracer does not allow it and would
2004 * result in a corrupted trace.
2006 if (session
->has_been_started
) {
2007 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2011 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2012 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
2013 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
2017 case LTTNG_DOMAIN_KERNEL
:
2018 assert(session
->kernel_session
);
2020 if (session
->kernel_session
->channel_count
== 0) {
2021 /* Create default channel */
2022 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
2023 if (ret
!= LTTNG_OK
) {
2026 chan_kern_created
= 1;
2028 /* Add kernel context to kernel tracer */
2029 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
2030 if (ret
!= LTTNG_OK
) {
2034 case LTTNG_DOMAIN_JUL
:
2035 case LTTNG_DOMAIN_LOG4J
:
2038 * Validate channel name.
2039 * If no channel name is given and the domain is JUL or LOG4J,
2040 * set it to the appropriate domain-specific channel name. If
2041 * a name is provided but does not match the expexted channel
2042 * name, return an error.
2044 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
2045 strcmp(channel_name
,
2046 DEFAULT_JUL_CHANNEL_NAME
)) {
2047 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2049 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
2050 strcmp(channel_name
,
2051 DEFAULT_LOG4J_CHANNEL_NAME
)) {
2052 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2055 /* break is _not_ missing here. */
2057 case LTTNG_DOMAIN_UST
:
2059 struct ltt_ust_session
*usess
= session
->ust_session
;
2060 unsigned int chan_count
;
2064 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
2065 if (chan_count
== 0) {
2066 struct lttng_channel
*attr
;
2067 /* Create default channel */
2068 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
2070 ret
= LTTNG_ERR_FATAL
;
2074 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
2075 if (ret
!= LTTNG_OK
) {
2079 channel_attr_destroy(attr
);
2080 chan_ust_created
= 1;
2083 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2084 free(app_ctx_provider_name
);
2086 app_ctx_name
= NULL
;
2087 app_ctx_provider_name
= NULL
;
2088 if (ret
!= LTTNG_OK
) {
2094 ret
= LTTNG_ERR_UND
;
2102 if (chan_kern_created
) {
2103 struct ltt_kernel_channel
*kchan
=
2104 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2105 session
->kernel_session
);
2106 /* Created previously, this should NOT fail. */
2108 kernel_destroy_channel(kchan
);
2111 if (chan_ust_created
) {
2112 struct ltt_ust_channel
*uchan
=
2113 trace_ust_find_channel_by_name(
2114 session
->ust_session
->domain_global
.channels
,
2115 DEFAULT_CHANNEL_NAME
);
2116 /* Created previously, this should NOT fail. */
2118 /* Remove from the channel list of the session. */
2119 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2121 trace_ust_destroy_channel(uchan
);
2124 free(app_ctx_provider_name
);
2129 static inline bool name_starts_with(const char *name
, const char *prefix
)
2131 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2133 return !strncmp(name
, prefix
, max_cmp_len
);
2136 /* Perform userspace-specific event name validation */
2137 static int validate_ust_event_name(const char *name
)
2147 * Check name against all internal UST event component namespaces used
2150 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2151 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2152 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2161 * Internal version of cmd_enable_event() with a supplemental
2162 * "internal_event" flag which is used to enable internal events which should
2163 * be hidden from clients. Such events are used in the agent implementation to
2164 * enable the events through which all "agent" events are funeled.
2166 static int _cmd_enable_event(struct ltt_session
*session
,
2167 const struct lttng_domain
*domain
,
2168 char *channel_name
, struct lttng_event
*event
,
2169 char *filter_expression
,
2170 struct lttng_filter_bytecode
*filter
,
2171 struct lttng_event_exclusion
*exclusion
,
2172 int wpipe
, bool internal_event
)
2174 int ret
= 0, channel_created
= 0;
2175 struct lttng_channel
*attr
= NULL
;
2179 assert(channel_name
);
2181 /* If we have a filter, we must have its filter expression */
2182 assert(!(!!filter_expression
^ !!filter
));
2184 /* Normalize event name as a globbing pattern */
2185 strutils_normalize_star_glob_pattern(event
->name
);
2187 /* Normalize exclusion names as globbing patterns */
2191 for (i
= 0; i
< exclusion
->count
; i
++) {
2192 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2194 strutils_normalize_star_glob_pattern(name
);
2198 DBG("Enable event command for event \'%s\'", event
->name
);
2202 switch (domain
->type
) {
2203 case LTTNG_DOMAIN_KERNEL
:
2205 struct ltt_kernel_channel
*kchan
;
2208 * If a non-default channel has been created in the
2209 * session, explicitely require that -c chan_name needs
2212 if (session
->kernel_session
->has_non_default_channel
2213 && channel_name
[0] == '\0') {
2214 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2218 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2219 session
->kernel_session
);
2220 if (kchan
== NULL
) {
2221 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2222 LTTNG_BUFFER_GLOBAL
);
2224 ret
= LTTNG_ERR_FATAL
;
2227 if (lttng_strncpy(attr
->name
, channel_name
,
2228 sizeof(attr
->name
))) {
2229 ret
= LTTNG_ERR_INVALID
;
2233 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2234 if (ret
!= LTTNG_OK
) {
2237 channel_created
= 1;
2240 /* Get the newly created kernel channel pointer */
2241 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2242 session
->kernel_session
);
2243 if (kchan
== NULL
) {
2244 /* This sould not happen... */
2245 ret
= LTTNG_ERR_FATAL
;
2249 switch (event
->type
) {
2250 case LTTNG_EVENT_ALL
:
2252 char *filter_expression_a
= NULL
;
2253 struct lttng_filter_bytecode
*filter_a
= NULL
;
2256 * We need to duplicate filter_expression and filter,
2257 * because ownership is passed to first enable
2260 if (filter_expression
) {
2261 filter_expression_a
= strdup(filter_expression
);
2262 if (!filter_expression_a
) {
2263 ret
= LTTNG_ERR_FATAL
;
2268 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2270 free(filter_expression_a
);
2271 ret
= LTTNG_ERR_FATAL
;
2274 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2276 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2277 ret
= event_kernel_enable_event(kchan
, event
,
2278 filter_expression
, filter
);
2279 /* We have passed ownership */
2280 filter_expression
= NULL
;
2282 if (ret
!= LTTNG_OK
) {
2283 if (channel_created
) {
2284 /* Let's not leak a useless channel. */
2285 kernel_destroy_channel(kchan
);
2287 free(filter_expression_a
);
2291 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2292 ret
= event_kernel_enable_event(kchan
, event
,
2293 filter_expression_a
, filter_a
);
2294 /* We have passed ownership */
2295 filter_expression_a
= NULL
;
2297 if (ret
!= LTTNG_OK
) {
2302 case LTTNG_EVENT_PROBE
:
2303 case LTTNG_EVENT_USERSPACE_PROBE
:
2304 case LTTNG_EVENT_FUNCTION
:
2305 case LTTNG_EVENT_FUNCTION_ENTRY
:
2306 case LTTNG_EVENT_TRACEPOINT
:
2307 ret
= event_kernel_enable_event(kchan
, event
,
2308 filter_expression
, filter
);
2309 /* We have passed ownership */
2310 filter_expression
= NULL
;
2312 if (ret
!= LTTNG_OK
) {
2313 if (channel_created
) {
2314 /* Let's not leak a useless channel. */
2315 kernel_destroy_channel(kchan
);
2320 case LTTNG_EVENT_SYSCALL
:
2321 ret
= event_kernel_enable_event(kchan
, event
,
2322 filter_expression
, filter
);
2323 /* We have passed ownership */
2324 filter_expression
= NULL
;
2326 if (ret
!= LTTNG_OK
) {
2331 ret
= LTTNG_ERR_UNK
;
2335 kernel_wait_quiescent();
2338 case LTTNG_DOMAIN_UST
:
2340 struct ltt_ust_channel
*uchan
;
2341 struct ltt_ust_session
*usess
= session
->ust_session
;
2346 * If a non-default channel has been created in the
2347 * session, explicitely require that -c chan_name needs
2350 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2351 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2355 /* Get channel from global UST domain */
2356 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2358 if (uchan
== NULL
) {
2359 /* Create default channel */
2360 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2361 usess
->buffer_type
);
2363 ret
= LTTNG_ERR_FATAL
;
2366 if (lttng_strncpy(attr
->name
, channel_name
,
2367 sizeof(attr
->name
))) {
2368 ret
= LTTNG_ERR_INVALID
;
2372 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2373 if (ret
!= LTTNG_OK
) {
2377 /* Get the newly created channel reference back */
2378 uchan
= trace_ust_find_channel_by_name(
2379 usess
->domain_global
.channels
, channel_name
);
2383 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2385 * Don't allow users to add UST events to channels which
2386 * are assigned to a userspace subdomain (JUL, Log4J,
2389 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2393 if (!internal_event
) {
2395 * Ensure the event name is not reserved for internal
2398 ret
= validate_ust_event_name(event
->name
);
2400 WARN("Userspace event name %s failed validation.",
2402 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2407 /* At this point, the session and channel exist on the tracer */
2408 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2409 filter_expression
, filter
, exclusion
,
2411 /* We have passed ownership */
2412 filter_expression
= NULL
;
2415 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2416 goto already_enabled
;
2417 } else if (ret
!= LTTNG_OK
) {
2422 case LTTNG_DOMAIN_LOG4J
:
2423 case LTTNG_DOMAIN_JUL
:
2424 case LTTNG_DOMAIN_PYTHON
:
2426 const char *default_event_name
, *default_chan_name
;
2428 struct lttng_event uevent
;
2429 struct lttng_domain tmp_dom
;
2430 struct ltt_ust_session
*usess
= session
->ust_session
;
2434 if (!agent_tracing_is_enabled()) {
2435 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2436 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2440 agt
= trace_ust_find_agent(usess
, domain
->type
);
2442 agt
= agent_create(domain
->type
);
2444 ret
= LTTNG_ERR_NOMEM
;
2447 agent_add(agt
, usess
->agents
);
2450 /* Create the default tracepoint. */
2451 memset(&uevent
, 0, sizeof(uevent
));
2452 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2453 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2454 default_event_name
= event_get_default_agent_ust_name(
2456 if (!default_event_name
) {
2457 ret
= LTTNG_ERR_FATAL
;
2460 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2461 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2464 * The domain type is changed because we are about to enable the
2465 * default channel and event for the JUL domain that are hardcoded.
2466 * This happens in the UST domain.
2468 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2469 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2471 switch (domain
->type
) {
2472 case LTTNG_DOMAIN_LOG4J
:
2473 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2475 case LTTNG_DOMAIN_JUL
:
2476 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2478 case LTTNG_DOMAIN_PYTHON
:
2479 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2482 /* The switch/case we are in makes this impossible */
2487 char *filter_expression_copy
= NULL
;
2488 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2491 const size_t filter_size
= sizeof(
2492 struct lttng_filter_bytecode
)
2495 filter_copy
= zmalloc(filter_size
);
2497 ret
= LTTNG_ERR_NOMEM
;
2500 memcpy(filter_copy
, filter
, filter_size
);
2502 filter_expression_copy
=
2503 strdup(filter_expression
);
2504 if (!filter_expression
) {
2505 ret
= LTTNG_ERR_NOMEM
;
2508 if (!filter_expression_copy
|| !filter_copy
) {
2509 free(filter_expression_copy
);
2515 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2516 (char *) default_chan_name
,
2517 &uevent
, filter_expression_copy
,
2518 filter_copy
, NULL
, wpipe
);
2521 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2522 goto already_enabled
;
2523 } else if (ret
!= LTTNG_OK
) {
2527 /* The wild card * means that everything should be enabled. */
2528 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2529 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2532 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2536 filter_expression
= NULL
;
2537 if (ret
!= LTTNG_OK
) {
2544 ret
= LTTNG_ERR_UND
;
2552 free(filter_expression
);
2555 channel_attr_destroy(attr
);
2561 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2562 * We own filter, exclusion, and filter_expression.
2564 int cmd_enable_event(struct ltt_session
*session
,
2565 const struct lttng_domain
*domain
,
2566 char *channel_name
, struct lttng_event
*event
,
2567 char *filter_expression
,
2568 struct lttng_filter_bytecode
*filter
,
2569 struct lttng_event_exclusion
*exclusion
,
2572 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2573 filter_expression
, filter
, exclusion
, wpipe
, false);
2577 * Enable an event which is internal to LTTng. An internal should
2578 * never be made visible to clients and are immune to checks such as
2581 static int cmd_enable_event_internal(struct ltt_session
*session
,
2582 const struct lttng_domain
*domain
,
2583 char *channel_name
, struct lttng_event
*event
,
2584 char *filter_expression
,
2585 struct lttng_filter_bytecode
*filter
,
2586 struct lttng_event_exclusion
*exclusion
,
2589 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2590 filter_expression
, filter
, exclusion
, wpipe
, true);
2594 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2596 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2597 struct lttng_event
**events
)
2600 ssize_t nb_events
= 0;
2603 case LTTNG_DOMAIN_KERNEL
:
2604 nb_events
= kernel_list_events(events
);
2605 if (nb_events
< 0) {
2606 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2610 case LTTNG_DOMAIN_UST
:
2611 nb_events
= ust_app_list_events(events
);
2612 if (nb_events
< 0) {
2613 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2617 case LTTNG_DOMAIN_LOG4J
:
2618 case LTTNG_DOMAIN_JUL
:
2619 case LTTNG_DOMAIN_PYTHON
:
2620 nb_events
= agent_list_events(events
, domain
);
2621 if (nb_events
< 0) {
2622 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2627 ret
= LTTNG_ERR_UND
;
2634 /* Return negative value to differentiate return code */
2639 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2641 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2642 struct lttng_event_field
**fields
)
2645 ssize_t nb_fields
= 0;
2648 case LTTNG_DOMAIN_UST
:
2649 nb_fields
= ust_app_list_event_fields(fields
);
2650 if (nb_fields
< 0) {
2651 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2655 case LTTNG_DOMAIN_KERNEL
:
2656 default: /* fall-through */
2657 ret
= LTTNG_ERR_UND
;
2664 /* Return negative value to differentiate return code */
2668 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2670 return syscall_table_list(events
);
2674 * Command LTTNG_START_TRACE processed by the client thread.
2676 * Called with session mutex held.
2678 int cmd_start_trace(struct ltt_session
*session
)
2680 enum lttng_error_code ret
;
2681 unsigned long nb_chan
= 0;
2682 struct ltt_kernel_session
*ksession
;
2683 struct ltt_ust_session
*usess
;
2684 const bool session_rotated_after_last_stop
=
2685 session
->rotated_after_last_stop
;
2686 const bool session_cleared_after_last_stop
=
2687 session
->cleared_after_last_stop
;
2691 /* Ease our life a bit ;) */
2692 ksession
= session
->kernel_session
;
2693 usess
= session
->ust_session
;
2695 /* Is the session already started? */
2696 if (session
->active
) {
2697 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2698 /* Perform nothing */
2702 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2703 !session
->current_trace_chunk
) {
2705 * A rotation was launched while the session was stopped and
2706 * it has not been completed yet. It is not possible to start
2707 * the session since starting the session here would require a
2708 * rotation from "NULL" to a new trace chunk. That rotation
2709 * would overlap with the ongoing rotation, which is not
2712 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2714 ret
= LTTNG_ERR_ROTATION_PENDING
;
2719 * Starting a session without channel is useless since after that it's not
2720 * possible to enable channel thus inform the client.
2722 if (usess
&& usess
->domain_global
.channels
) {
2723 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2726 nb_chan
+= ksession
->channel_count
;
2729 ret
= LTTNG_ERR_NO_CHANNEL
;
2733 session
->active
= 1;
2734 session
->rotated_after_last_stop
= false;
2735 session
->cleared_after_last_stop
= false;
2736 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2737 if (!session
->has_been_started
) {
2738 struct lttng_trace_chunk
*trace_chunk
;
2740 DBG("Creating initial trace chunk of session \"%s\"",
2742 trace_chunk
= session_create_new_trace_chunk(
2743 session
, NULL
, NULL
, NULL
);
2745 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2748 assert(!session
->current_trace_chunk
);
2749 ret
= session_set_trace_chunk(session
, trace_chunk
,
2751 lttng_trace_chunk_put(trace_chunk
);
2753 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2757 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2760 * Rotate existing streams into the new chunk.
2761 * This is a "quiet" rotation has no client has
2762 * explicitly requested this operation.
2764 * There is also no need to wait for the rotation
2765 * to complete as it will happen immediately. No data
2766 * was produced as the session was stopped, so the
2767 * rotation should happen on reception of the command.
2769 ret
= cmd_rotate_session(session
, NULL
, true,
2770 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2771 if (ret
!= LTTNG_OK
) {
2777 /* Kernel tracing */
2778 if (ksession
!= NULL
) {
2779 DBG("Start kernel tracing session %s", session
->name
);
2780 ret
= start_kernel_session(ksession
);
2781 if (ret
!= LTTNG_OK
) {
2786 /* Flag session that trace should start automatically */
2788 int int_ret
= ust_app_start_trace_all(usess
);
2791 ret
= LTTNG_ERR_UST_START_FAIL
;
2797 * Open a packet in every stream of the session to ensure that viewers
2798 * can correctly identify the boundaries of the periods during which
2799 * tracing was active for this session.
2801 ret
= session_open_packets(session
);
2802 if (ret
!= LTTNG_OK
) {
2807 * Clear the flag that indicates that a rotation was done while the
2808 * session was stopped.
2810 session
->rotated_after_last_stop
= false;
2812 if (session
->rotate_timer_period
) {
2813 int int_ret
= timer_session_rotation_schedule_timer_start(
2814 session
, session
->rotate_timer_period
);
2817 ERR("Failed to enable rotate timer");
2818 ret
= LTTNG_ERR_UNK
;
2826 if (ret
== LTTNG_OK
) {
2827 /* Flag this after a successful start. */
2828 session
->has_been_started
|= 1;
2830 session
->active
= 0;
2831 /* Restore initial state on error. */
2832 session
->rotated_after_last_stop
=
2833 session_rotated_after_last_stop
;
2834 session
->cleared_after_last_stop
=
2835 session_cleared_after_last_stop
;
2842 * Command LTTNG_STOP_TRACE processed by the client thread.
2844 int cmd_stop_trace(struct ltt_session
*session
)
2847 struct ltt_kernel_session
*ksession
;
2848 struct ltt_ust_session
*usess
;
2852 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2854 ksession
= session
->kernel_session
;
2855 usess
= session
->ust_session
;
2857 /* Session is not active. Skip everythong and inform the client. */
2858 if (!session
->active
) {
2859 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2863 ret
= stop_kernel_session(ksession
);
2864 if (ret
!= LTTNG_OK
) {
2868 if (usess
&& usess
->active
) {
2869 ret
= ust_app_stop_trace_all(usess
);
2871 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2876 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2878 /* Flag inactive after a successful stop. */
2879 session
->active
= 0;
2887 * Set the base_path of the session only if subdir of a control uris is set.
2888 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2890 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2892 struct lttng_uri
*uris
)
2897 for (i
= 0; i
< nb_uri
; i
++) {
2898 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2899 uris
[i
].subdir
[0] == '\0') {
2900 /* Not interested in these URIs */
2904 if (session
->base_path
!= NULL
) {
2905 free(session
->base_path
);
2906 session
->base_path
= NULL
;
2909 /* Set session base_path */
2910 session
->base_path
= strdup(uris
[i
].subdir
);
2911 if (!session
->base_path
) {
2912 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2913 uris
[i
].subdir
, session
->name
);
2914 ret
= LTTNG_ERR_NOMEM
;
2917 DBG2("Setting base path \"%s\" for session \"%s\"",
2918 session
->base_path
, session
->name
);
2926 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2928 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2929 struct lttng_uri
*uris
)
2932 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2933 struct ltt_ust_session
*usess
= session
->ust_session
;
2939 /* Can't set consumer URI if the session is active. */
2940 if (session
->active
) {
2941 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2946 * Set the session base path if any. This is done inside
2947 * cmd_set_consumer_uri to preserve backward compatibility of the
2948 * previous session creation api vs the session descriptor api.
2950 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2951 if (ret
!= LTTNG_OK
) {
2955 /* Set the "global" consumer URIs */
2956 for (i
= 0; i
< nb_uri
; i
++) {
2957 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2959 if (ret
!= LTTNG_OK
) {
2964 /* Set UST session URIs */
2965 if (session
->ust_session
) {
2966 for (i
= 0; i
< nb_uri
; i
++) {
2967 ret
= add_uri_to_consumer(session
,
2968 session
->ust_session
->consumer
,
2969 &uris
[i
], LTTNG_DOMAIN_UST
);
2970 if (ret
!= LTTNG_OK
) {
2976 /* Set kernel session URIs */
2977 if (session
->kernel_session
) {
2978 for (i
= 0; i
< nb_uri
; i
++) {
2979 ret
= add_uri_to_consumer(session
,
2980 session
->kernel_session
->consumer
,
2981 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2982 if (ret
!= LTTNG_OK
) {
2989 * Make sure to set the session in output mode after we set URI since a
2990 * session can be created without URL (thus flagged in no output mode).
2992 session
->output_traces
= 1;
2994 ksess
->output_traces
= 1;
2998 usess
->output_traces
= 1;
3009 enum lttng_error_code
set_session_output_from_descriptor(
3010 struct ltt_session
*session
,
3011 const struct lttng_session_descriptor
*descriptor
)
3014 enum lttng_error_code ret_code
= LTTNG_OK
;
3015 enum lttng_session_descriptor_type session_type
=
3016 lttng_session_descriptor_get_type(descriptor
);
3017 enum lttng_session_descriptor_output_type output_type
=
3018 lttng_session_descriptor_get_output_type(descriptor
);
3019 struct lttng_uri uris
[2] = {};
3020 size_t uri_count
= 0;
3022 switch (output_type
) {
3023 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3025 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3026 lttng_session_descriptor_get_local_output_uri(descriptor
,
3030 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3031 lttng_session_descriptor_get_network_output_uris(descriptor
,
3032 &uris
[0], &uris
[1]);
3036 ret_code
= LTTNG_ERR_INVALID
;
3040 switch (session_type
) {
3041 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3043 struct snapshot_output
*new_output
= NULL
;
3045 new_output
= snapshot_output_alloc();
3047 ret_code
= LTTNG_ERR_NOMEM
;
3051 ret
= snapshot_output_init_with_uri(session
,
3052 DEFAULT_SNAPSHOT_MAX_SIZE
,
3053 NULL
, uris
, uri_count
, session
->consumer
,
3054 new_output
, &session
->snapshot
);
3056 ret_code
= (ret
== -ENOMEM
) ?
3057 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3058 snapshot_output_destroy(new_output
);
3061 snapshot_add_output(&session
->snapshot
, new_output
);
3064 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3065 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3067 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
3071 ret_code
= LTTNG_ERR_INVALID
;
3079 enum lttng_error_code
cmd_create_session_from_descriptor(
3080 struct lttng_session_descriptor
*descriptor
,
3081 const lttng_sock_cred
*creds
,
3082 const char *home_path
)
3085 enum lttng_error_code ret_code
;
3086 const char *session_name
;
3087 struct ltt_session
*new_session
= NULL
;
3088 enum lttng_session_descriptor_status descriptor_status
;
3090 session_lock_list();
3092 if (*home_path
!= '/') {
3093 ERR("Home path provided by client is not absolute");
3094 ret_code
= LTTNG_ERR_INVALID
;
3099 descriptor_status
= lttng_session_descriptor_get_session_name(
3100 descriptor
, &session_name
);
3101 switch (descriptor_status
) {
3102 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3104 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3105 session_name
= NULL
;
3108 ret_code
= LTTNG_ERR_INVALID
;
3112 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3114 if (ret_code
!= LTTNG_OK
) {
3118 if (!session_name
) {
3119 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3122 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3127 if (!lttng_session_descriptor_is_output_destination_initialized(
3130 * Only include the session's creation time in the output
3131 * destination if the name of the session itself was
3132 * not auto-generated.
3134 ret_code
= lttng_session_descriptor_set_default_output(
3136 session_name
? &new_session
->creation_time
: NULL
,
3138 if (ret_code
!= LTTNG_OK
) {
3142 new_session
->has_user_specified_directory
=
3143 lttng_session_descriptor_has_output_directory(
3147 switch (lttng_session_descriptor_get_type(descriptor
)) {
3148 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3149 new_session
->snapshot_mode
= 1;
3151 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3152 new_session
->live_timer
=
3153 lttng_session_descriptor_live_get_timer_interval(
3160 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3161 if (ret_code
!= LTTNG_OK
) {
3164 new_session
->consumer
->enabled
= 1;
3165 ret_code
= LTTNG_OK
;
3167 /* Release reference provided by the session_create function. */
3168 session_put(new_session
);
3169 if (ret_code
!= LTTNG_OK
&& new_session
) {
3170 /* Release the global reference on error. */
3171 session_destroy(new_session
);
3173 session_unlock_list();
3177 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3178 struct lttng_session_descriptor
**return_descriptor
)
3181 size_t payload_size
;
3182 struct lttng_dynamic_buffer payload
;
3183 struct lttng_buffer_view home_dir_view
;
3184 struct lttng_buffer_view session_descriptor_view
;
3185 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3186 enum lttng_error_code ret_code
;
3188 lttng_dynamic_buffer_init(&payload
);
3189 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
3191 ret_code
= LTTNG_ERR_INVALID
;
3194 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
3195 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3196 ret_code
= LTTNG_ERR_INVALID
;
3200 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
3201 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
3202 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3204 ret_code
= LTTNG_ERR_NOMEM
;
3208 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3210 ERR("Reception of session descriptor failed, aborting.");
3211 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3215 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3218 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3219 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3221 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3222 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3224 ret
= lttng_session_descriptor_create_from_buffer(
3225 &session_descriptor_view
, &session_descriptor
);
3227 ERR("Failed to create session descriptor from payload of \"create session\" command");
3228 ret_code
= LTTNG_ERR_INVALID
;
3233 * Sets the descriptor's auto-generated properties (name, output) if
3236 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3238 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3239 if (ret_code
!= LTTNG_OK
) {
3243 ret_code
= LTTNG_OK
;
3244 *return_descriptor
= session_descriptor
;
3245 session_descriptor
= NULL
;
3247 lttng_dynamic_buffer_reset(&payload
);
3248 lttng_session_descriptor_destroy(session_descriptor
);
3253 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3254 void *_reply_context
)
3258 const struct cmd_destroy_session_reply_context
*reply_context
=
3260 struct lttng_dynamic_buffer payload
;
3261 struct lttcomm_session_destroy_command_header cmd_header
;
3262 struct lttng_trace_archive_location
*location
= NULL
;
3263 struct lttcomm_lttng_msg llm
= {
3264 .cmd_type
= LTTNG_DESTROY_SESSION
,
3265 .ret_code
= reply_context
->destruction_status
,
3268 sizeof(struct lttcomm_session_destroy_command_header
),
3271 size_t payload_size_before_location
;
3273 lttng_dynamic_buffer_init(&payload
);
3275 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3277 ERR("Failed to append session destruction message");
3281 cmd_header
.rotation_state
=
3282 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3283 session
->rotation_state
:
3284 LTTNG_ROTATION_STATE_NO_ROTATION
);
3285 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3286 sizeof(cmd_header
));
3288 ERR("Failed to append session destruction command header");
3292 if (!reply_context
->implicit_rotation_on_destroy
) {
3293 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3297 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3298 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3303 location
= session_get_trace_archive_location(session
);
3305 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3310 payload_size_before_location
= payload
.size
;
3311 comm_ret
= lttng_trace_archive_location_serialize(location
,
3314 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3318 /* Update the message to indicate the location's length. */
3319 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3320 payload
.size
- payload_size_before_location
;
3322 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3323 payload
.data
, payload
.size
);
3324 if (comm_ret
!= (ssize_t
) payload
.size
) {
3325 ERR("Failed to send result of the destruction of session \"%s\" to client",
3329 ret
= close(reply_context
->reply_sock_fd
);
3331 PERROR("Failed to close client socket in deferred session destroy reply");
3333 lttng_dynamic_buffer_reset(&payload
);
3334 free(_reply_context
);
3338 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3340 * Called with session lock held.
3342 int cmd_destroy_session(struct ltt_session
*session
,
3343 struct notification_thread_handle
*notification_thread_handle
,
3347 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3348 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3351 reply_context
= zmalloc(sizeof(*reply_context
));
3352 if (!reply_context
) {
3353 ret
= LTTNG_ERR_NOMEM
;
3356 reply_context
->reply_sock_fd
= *sock_fd
;
3362 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3364 if (session
->active
) {
3365 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3367 ret
= cmd_stop_trace(session
);
3368 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3369 /* Carry on with the destruction of the session. */
3370 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3371 session
->name
, lttng_strerror(-ret
));
3372 destruction_last_error
= ret
;
3376 if (session
->rotation_schedule_timer_enabled
) {
3377 if (timer_session_rotation_schedule_timer_stop(
3379 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3381 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3385 if (session
->rotate_size
) {
3386 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3387 session
->rotate_size
= 0;
3390 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3392 * Perform a last rotation on destruction if rotations have
3393 * occurred during the session's lifetime.
3395 ret
= cmd_rotate_session(session
, NULL
, false,
3396 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3397 if (ret
!= LTTNG_OK
) {
3398 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3399 session
->name
, lttng_strerror(-ret
));
3400 destruction_last_error
= -ret
;
3402 if (reply_context
) {
3403 reply_context
->implicit_rotation_on_destroy
= true;
3405 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3407 * The user has not triggered a session rotation. However, to
3408 * ensure all data has been consumed, the session is rotated
3409 * to a 'null' trace chunk before it is destroyed.
3411 * This is a "quiet" rotation meaning that no notification is
3412 * emitted and no renaming of the current trace chunk takes
3415 ret
= cmd_rotate_session(session
, NULL
, true,
3416 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3418 * Rotation operations may not be supported by the kernel
3419 * tracer. Hence, do not consider this implicit rotation as
3420 * a session destruction error. The library has already stopped
3421 * the session and waited for pending data; there is nothing
3422 * left to do but complete the destruction of the session.
3424 if (ret
!= LTTNG_OK
&&
3425 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3426 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3427 session
->name
, lttng_strerror(ret
));
3428 destruction_last_error
= -ret
;
3432 if (session
->shm_path
[0]) {
3434 * When a session is created with an explicit shm_path,
3435 * the consumer daemon will create its shared memory files
3436 * at that location and will *not* unlink them. This is normal
3437 * as the intention of that feature is to make it possible
3438 * to retrieve the content of those files should a crash occur.
3440 * To ensure the content of those files can be used, the
3441 * sessiond daemon will replicate the content of the metadata
3442 * cache in a metadata file.
3444 * On clean-up, it is expected that the consumer daemon will
3445 * unlink the shared memory files and that the session daemon
3446 * will unlink the metadata file. Then, the session's directory
3447 * in the shm path can be removed.
3449 * Unfortunately, a flaw in the design of the sessiond's and
3450 * consumerd's tear down of channels makes it impossible to
3451 * determine when the sessiond _and_ the consumerd have both
3452 * destroyed their representation of a channel. For one, the
3453 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3454 * callbacks in both daemons.
3456 * However, it is also impossible for the sessiond to know when
3457 * the consumer daemon is done destroying its channel(s) since
3458 * it occurs as a reaction to the closing of the channel's file
3459 * descriptor. There is no resulting communication initiated
3460 * from the consumerd to the sessiond to confirm that the
3461 * operation is completed (and was successful).
3463 * Until this is all fixed, the session daemon checks for the
3464 * removal of the session's shm path which makes it possible
3465 * to safely advertise a session as having been destroyed.
3467 * Prior to this fix, it was not possible to reliably save
3468 * a session making use of the --shm-path option, destroy it,
3469 * and load it again. This is because the creation of the
3470 * session would fail upon seeing the session's shm path
3471 * already in existence.
3473 * Note that none of the error paths in the check for the
3474 * directory's existence return an error. This is normal
3475 * as there isn't much that can be done. The session will
3476 * be destroyed properly, except that we can't offer the
3477 * guarantee that the same session can be re-created.
3479 current_completion_handler
= &destroy_completion_handler
.handler
;
3480 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3482 sizeof(destroy_completion_handler
.shm_path
));
3487 * The session is destroyed. However, note that the command context
3488 * still holds a reference to the session, thus delaying its destruction
3489 * _at least_ up to the point when that reference is released.
3491 session_destroy(session
);
3492 if (reply_context
) {
3493 reply_context
->destruction_status
= destruction_last_error
;
3494 ret
= session_add_destroy_notifier(session
,
3495 cmd_destroy_session_reply
,
3496 (void *) reply_context
);
3498 ret
= LTTNG_ERR_FATAL
;
3510 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3512 int cmd_register_consumer(struct ltt_session
*session
,
3513 enum lttng_domain_type domain
, const char *sock_path
,
3514 struct consumer_data
*cdata
)
3517 struct consumer_socket
*socket
= NULL
;
3524 case LTTNG_DOMAIN_KERNEL
:
3526 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3530 /* Can't register a consumer if there is already one */
3531 if (ksess
->consumer_fds_sent
!= 0) {
3532 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3536 sock
= lttcomm_connect_unix_sock(sock_path
);
3538 ret
= LTTNG_ERR_CONNECT_FAIL
;
3541 cdata
->cmd_sock
= sock
;
3543 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3544 if (socket
== NULL
) {
3547 PERROR("close register consumer");
3549 cdata
->cmd_sock
= -1;
3550 ret
= LTTNG_ERR_FATAL
;
3554 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3555 if (socket
->lock
== NULL
) {
3556 PERROR("zmalloc pthread mutex");
3557 ret
= LTTNG_ERR_FATAL
;
3560 pthread_mutex_init(socket
->lock
, NULL
);
3561 socket
->registered
= 1;
3564 consumer_add_socket(socket
, ksess
->consumer
);
3567 pthread_mutex_lock(&cdata
->pid_mutex
);
3569 pthread_mutex_unlock(&cdata
->pid_mutex
);
3574 /* TODO: Userspace tracing */
3575 ret
= LTTNG_ERR_UND
;
3583 consumer_destroy_socket(socket
);
3589 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3591 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3592 struct lttng_domain
**domains
)
3597 struct lttng_ht_iter iter
;
3599 if (session
->kernel_session
!= NULL
) {
3600 DBG3("Listing domains found kernel domain");
3604 if (session
->ust_session
!= NULL
) {
3605 DBG3("Listing domains found UST global domain");
3609 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3611 if (agt
->being_used
) {
3622 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3623 if (*domains
== NULL
) {
3624 ret
= LTTNG_ERR_FATAL
;
3628 if (session
->kernel_session
!= NULL
) {
3629 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3631 /* Kernel session buffer type is always GLOBAL */
3632 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3637 if (session
->ust_session
!= NULL
) {
3638 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3639 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3643 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3645 if (agt
->being_used
) {
3646 (*domains
)[index
].type
= agt
->domain
;
3647 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3657 /* Return negative value to differentiate return code */
3663 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3665 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3666 struct ltt_session
*session
, struct lttng_channel
**channels
)
3668 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3671 case LTTNG_DOMAIN_KERNEL
:
3672 if (session
->kernel_session
!= NULL
) {
3673 nb_chan
= session
->kernel_session
->channel_count
;
3675 DBG3("Number of kernel channels %zd", nb_chan
);
3677 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3681 case LTTNG_DOMAIN_UST
:
3682 if (session
->ust_session
!= NULL
) {
3684 nb_chan
= lttng_ht_get_count(
3685 session
->ust_session
->domain_global
.channels
);
3688 DBG3("Number of UST global channels %zd", nb_chan
);
3690 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3695 ret
= -LTTNG_ERR_UND
;
3700 const size_t channel_size
= sizeof(struct lttng_channel
) +
3701 sizeof(struct lttng_channel_extended
);
3702 struct lttng_channel_extended
*channel_exts
;
3704 payload_size
= nb_chan
* channel_size
;
3705 *channels
= zmalloc(payload_size
);
3706 if (*channels
== NULL
) {
3707 ret
= -LTTNG_ERR_FATAL
;
3711 channel_exts
= ((void *) *channels
) +
3712 (nb_chan
* sizeof(struct lttng_channel
));
3713 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3714 if (ret
!= LTTNG_OK
) {
3729 * Command LTTNG_LIST_EVENTS processed by the client thread.
3731 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3732 struct ltt_session
*session
, char *channel_name
,
3733 struct lttng_event
**events
, size_t *total_size
)
3736 ssize_t nb_event
= 0;
3739 case LTTNG_DOMAIN_KERNEL
:
3740 if (session
->kernel_session
!= NULL
) {
3741 nb_event
= list_lttng_kernel_events(channel_name
,
3742 session
->kernel_session
, events
,
3746 case LTTNG_DOMAIN_UST
:
3748 if (session
->ust_session
!= NULL
) {
3749 nb_event
= list_lttng_ust_global_events(channel_name
,
3750 &session
->ust_session
->domain_global
, events
,
3755 case LTTNG_DOMAIN_LOG4J
:
3756 case LTTNG_DOMAIN_JUL
:
3757 case LTTNG_DOMAIN_PYTHON
:
3758 if (session
->ust_session
) {
3759 struct lttng_ht_iter iter
;
3763 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3764 &iter
.iter
, agt
, node
.node
) {
3765 if (agt
->domain
== domain
) {
3766 nb_event
= list_lttng_agent_events(
3776 ret
= LTTNG_ERR_UND
;
3783 /* Return negative value to differentiate return code */
3788 * Using the session list, filled a lttng_session array to send back to the
3789 * client for session listing.
3791 * The session list lock MUST be acquired before calling this function. Use
3792 * session_lock_list() and session_unlock_list().
3794 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3795 size_t session_count
, uid_t uid
, gid_t gid
)
3799 struct ltt_session
*session
;
3800 struct ltt_session_list
*list
= session_get_list();
3801 struct lttng_session_extended
*extended
=
3802 (typeof(extended
)) (&sessions
[session_count
]);
3804 DBG("Getting all available session for UID %d GID %d",
3807 * Iterate over session list and append data after the control struct in
3810 cds_list_for_each_entry(session
, &list
->head
, list
) {
3811 if (!session_get(session
)) {
3815 * Only list the sessions the user can control.
3817 if (!session_access_ok(session
, uid
, gid
) ||
3818 session
->destroyed
) {
3819 session_put(session
);
3823 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3824 struct ltt_ust_session
*usess
= session
->ust_session
;
3826 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3827 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3828 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3829 ret
= build_network_session_path(sessions
[i
].path
,
3830 sizeof(sessions
[i
].path
), session
);
3832 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3833 session
->consumer
->dst
.session_root_path
);
3836 PERROR("snprintf session path");
3837 session_put(session
);
3841 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3842 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3843 sessions
[i
].enabled
= session
->active
;
3844 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3845 sessions
[i
].live_timer_interval
= session
->live_timer
;
3846 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3847 extended
[i
].creation_time
.is_set
= 1;
3849 session_put(session
);
3854 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3855 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3857 int cmd_data_pending(struct ltt_session
*session
)
3860 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3861 struct ltt_ust_session
*usess
= session
->ust_session
;
3865 DBG("Data pending for session %s", session
->name
);
3867 /* Session MUST be stopped to ask for data availability. */
3868 if (session
->active
) {
3869 ret
= LTTNG_ERR_SESSION_STARTED
;
3873 * If stopped, just make sure we've started before else the above call
3874 * will always send that there is data pending.
3876 * The consumer assumes that when the data pending command is received,
3877 * the trace has been started before or else no output data is written
3878 * by the streams which is a condition for data pending. So, this is
3879 * *VERY* important that we don't ask the consumer before a start
3882 if (!session
->has_been_started
) {
3888 /* A rotation is still pending, we have to wait. */
3889 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3890 DBG("Rotate still pending for session %s", session
->name
);
3895 if (ksess
&& ksess
->consumer
) {
3896 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3898 /* Data is still being extracted for the kernel. */
3903 if (usess
&& usess
->consumer
) {
3904 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3906 /* Data is still being extracted for the kernel. */
3911 /* Data is ready to be read by a viewer */
3919 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3921 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3923 int cmd_snapshot_add_output(struct ltt_session
*session
,
3924 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3927 struct snapshot_output
*new_output
;
3932 DBG("Cmd snapshot add output for session %s", session
->name
);
3935 * Can't create an output if the session is not set in no-output mode.
3937 if (session
->output_traces
) {
3938 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3942 if (session
->has_non_mmap_channel
) {
3943 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3947 /* Only one output is allowed until we have the "tee" feature. */
3948 if (session
->snapshot
.nb_output
== 1) {
3949 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3953 new_output
= snapshot_output_alloc();
3955 ret
= LTTNG_ERR_NOMEM
;
3959 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3960 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3961 &session
->snapshot
);
3963 if (ret
== -ENOMEM
) {
3964 ret
= LTTNG_ERR_NOMEM
;
3966 ret
= LTTNG_ERR_INVALID
;
3972 snapshot_add_output(&session
->snapshot
, new_output
);
3974 *id
= new_output
->id
;
3981 snapshot_output_destroy(new_output
);
3987 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3989 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3991 int cmd_snapshot_del_output(struct ltt_session
*session
,
3992 const struct lttng_snapshot_output
*output
)
3995 struct snapshot_output
*sout
= NULL
;
4003 * Permission denied to create an output if the session is not
4004 * set in no output mode.
4006 if (session
->output_traces
) {
4007 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4012 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4014 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4015 } else if (*output
->name
!= '\0') {
4016 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4018 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4021 ret
= LTTNG_ERR_INVALID
;
4025 snapshot_delete_output(&session
->snapshot
, sout
);
4026 snapshot_output_destroy(sout
);
4035 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4037 * If no output is available, outputs is untouched and 0 is returned.
4039 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4041 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4042 struct lttng_snapshot_output
**outputs
)
4045 struct lttng_snapshot_output
*list
= NULL
;
4046 struct lttng_ht_iter iter
;
4047 struct snapshot_output
*output
;
4052 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4055 * Permission denied to create an output if the session is not
4056 * set in no output mode.
4058 if (session
->output_traces
) {
4059 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4063 if (session
->snapshot
.nb_output
== 0) {
4068 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4070 ret
= -LTTNG_ERR_NOMEM
;
4074 /* Copy list from session to the new list object. */
4076 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4077 output
, node
.node
) {
4078 assert(output
->consumer
);
4079 list
[idx
].id
= output
->id
;
4080 list
[idx
].max_size
= output
->max_size
;
4081 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4082 sizeof(list
[idx
].name
))) {
4083 ret
= -LTTNG_ERR_INVALID
;
4086 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4087 if (lttng_strncpy(list
[idx
].ctrl_url
,
4088 output
->consumer
->dst
.session_root_path
,
4089 sizeof(list
[idx
].ctrl_url
))) {
4090 ret
= -LTTNG_ERR_INVALID
;
4095 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4096 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4098 ret
= -LTTNG_ERR_NOMEM
;
4103 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4104 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4106 ret
= -LTTNG_ERR_NOMEM
;
4115 ret
= session
->snapshot
.nb_output
;
4124 * Check if we can regenerate the metadata for this session.
4125 * Only kernel, UST per-uid and non-live sessions are supported.
4127 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4130 int check_regenerate_metadata_support(struct ltt_session
*session
)
4136 if (session
->live_timer
!= 0) {
4137 ret
= LTTNG_ERR_LIVE_SESSION
;
4140 if (!session
->active
) {
4141 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4144 if (session
->ust_session
) {
4145 switch (session
->ust_session
->buffer_type
) {
4146 case LTTNG_BUFFER_PER_UID
:
4148 case LTTNG_BUFFER_PER_PID
:
4149 ret
= LTTNG_ERR_PER_PID_SESSION
;
4153 ret
= LTTNG_ERR_UNK
;
4157 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4158 session
->consumer
->relay_minor_version
< 8) {
4159 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4169 int clear_metadata_file(int fd
)
4174 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4175 if (lseek_ret
< 0) {
4181 ret
= ftruncate(fd
, 0);
4183 PERROR("ftruncate");
4192 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4195 struct buffer_reg_uid
*uid_reg
= NULL
;
4196 struct buffer_reg_session
*session_reg
= NULL
;
4199 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4200 struct ust_registry_session
*registry
;
4201 struct ust_registry_channel
*chan
;
4202 struct lttng_ht_iter iter_chan
;
4204 session_reg
= uid_reg
->registry
;
4205 registry
= session_reg
->reg
.ust
;
4207 pthread_mutex_lock(®istry
->lock
);
4208 registry
->metadata_len_sent
= 0;
4209 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4210 registry
->metadata_len
= 0;
4211 registry
->metadata_version
++;
4212 if (registry
->metadata_fd
> 0) {
4213 /* Clear the metadata file's content. */
4214 ret
= clear_metadata_file(registry
->metadata_fd
);
4216 pthread_mutex_unlock(®istry
->lock
);
4221 ret
= ust_metadata_session_statedump(registry
, NULL
,
4222 registry
->major
, registry
->minor
);
4224 pthread_mutex_unlock(®istry
->lock
);
4225 ERR("Failed to generate session metadata (err = %d)",
4229 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4231 struct ust_registry_event
*event
;
4232 struct lttng_ht_iter iter_event
;
4234 ret
= ust_metadata_channel_statedump(registry
, chan
);
4236 pthread_mutex_unlock(®istry
->lock
);
4237 ERR("Failed to generate channel metadata "
4241 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4243 ret
= ust_metadata_event_statedump(registry
,
4246 pthread_mutex_unlock(®istry
->lock
);
4247 ERR("Failed to generate event metadata "
4253 pthread_mutex_unlock(®istry
->lock
);
4262 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4264 * Ask the consumer to truncate the existing metadata file(s) and
4265 * then regenerate the metadata. Live and per-pid sessions are not
4266 * supported and return an error.
4268 * Return 0 on success or else a LTTNG_ERR code.
4270 int cmd_regenerate_metadata(struct ltt_session
*session
)
4276 ret
= check_regenerate_metadata_support(session
);
4281 if (session
->kernel_session
) {
4282 ret
= kernctl_session_regenerate_metadata(
4283 session
->kernel_session
->fd
);
4285 ERR("Failed to regenerate the kernel metadata");
4290 if (session
->ust_session
) {
4291 ret
= ust_regenerate_metadata(session
->ust_session
);
4293 ERR("Failed to regenerate the UST metadata");
4297 DBG("Cmd metadata regenerate for session %s", session
->name
);
4305 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4307 * Ask the tracer to regenerate a new statedump.
4309 * Return 0 on success or else a LTTNG_ERR code.
4311 int cmd_regenerate_statedump(struct ltt_session
*session
)
4317 if (!session
->active
) {
4318 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4322 if (session
->kernel_session
) {
4323 ret
= kernctl_session_regenerate_statedump(
4324 session
->kernel_session
->fd
);
4326 * Currently, the statedump in kernel can only fail if out
4330 if (ret
== -ENOMEM
) {
4331 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4333 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4335 ERR("Failed to regenerate the kernel statedump");
4340 if (session
->ust_session
) {
4341 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4343 * Currently, the statedump in UST always returns 0.
4346 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4347 ERR("Failed to regenerate the UST statedump");
4351 DBG("Cmd regenerate statedump for session %s", session
->name
);
4358 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4359 struct notification_thread_handle
*notification_thread
)
4363 ssize_t sock_recv_len
;
4364 struct lttng_trigger
*trigger
= NULL
;
4365 struct lttng_payload trigger_payload
;
4367 lttng_payload_init(&trigger_payload
);
4368 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4369 ret
= lttng_dynamic_buffer_set_size(
4370 &trigger_payload
.buffer
, trigger_len
);
4372 ret
= LTTNG_ERR_NOMEM
;
4376 sock_recv_len
= lttcomm_recv_unix_sock(
4377 sock
, trigger_payload
.buffer
.data
, trigger_len
);
4378 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4379 ERR("Failed to receive \"register trigger\" command payload");
4380 /* TODO: should this be a new error enum ? */
4381 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4386 struct lttng_payload_view view
=
4387 lttng_payload_view_from_payload(
4388 &trigger_payload
, 0, -1);
4390 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
4392 ERR("Invalid trigger payload received in \"register trigger\" command");
4393 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4398 ret
= notification_thread_command_register_trigger(notification_thread
,
4400 /* Ownership of trigger was transferred. */
4403 lttng_trigger_destroy(trigger
);
4404 lttng_payload_reset(&trigger_payload
);
4408 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4409 struct notification_thread_handle
*notification_thread
)
4413 ssize_t sock_recv_len
;
4414 struct lttng_trigger
*trigger
= NULL
;
4415 struct lttng_payload trigger_payload
;
4417 lttng_payload_init(&trigger_payload
);
4418 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4419 ret
= lttng_dynamic_buffer_set_size(
4420 &trigger_payload
.buffer
, trigger_len
);
4422 ret
= LTTNG_ERR_NOMEM
;
4426 sock_recv_len
= lttcomm_recv_unix_sock(
4427 sock
, trigger_payload
.buffer
.data
, trigger_len
);
4428 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4429 ERR("Failed to receive \"unregister trigger\" command payload");
4430 /* TODO: should this be a new error enum ? */
4431 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4436 struct lttng_payload_view view
=
4437 lttng_payload_view_from_payload(
4438 &trigger_payload
, 0, -1);
4440 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
4442 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4443 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4448 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4451 lttng_trigger_destroy(trigger
);
4452 lttng_payload_reset(&trigger_payload
);
4457 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4458 * snapshot output is *not* set with a remote destination.
4460 * Return LTTNG_OK on success or a LTTNG_ERR code.
4462 static enum lttng_error_code
set_relayd_for_snapshot(
4463 struct consumer_output
*output
,
4464 const struct ltt_session
*session
)
4466 enum lttng_error_code status
= LTTNG_OK
;
4467 struct lttng_ht_iter iter
;
4468 struct consumer_socket
*socket
;
4469 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4470 const char *base_path
;
4475 DBG2("Set relayd object from snapshot output");
4477 if (session
->current_trace_chunk
) {
4478 enum lttng_trace_chunk_status chunk_status
=
4479 lttng_trace_chunk_get_id(
4480 session
->current_trace_chunk
,
4481 ¤t_chunk_id
.value
);
4483 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4484 current_chunk_id
.is_set
= true;
4486 ERR("Failed to get current trace chunk id");
4487 status
= LTTNG_ERR_UNK
;
4492 /* Ignore if snapshot consumer output is not network. */
4493 if (output
->type
!= CONSUMER_DST_NET
) {
4498 * The snapshot record URI base path overrides the session
4501 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4502 base_path
= output
->dst
.net
.control
.subdir
;
4504 base_path
= session
->base_path
;
4508 * For each consumer socket, create and send the relayd object of the
4512 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4513 socket
, node
.node
) {
4514 pthread_mutex_lock(socket
->lock
);
4515 status
= send_consumer_relayd_sockets(0, session
->id
,
4517 session
->name
, session
->hostname
,
4519 session
->live_timer
,
4520 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4521 session
->creation_time
,
4522 session
->name_contains_creation_time
);
4523 pthread_mutex_unlock(socket
->lock
);
4524 if (status
!= LTTNG_OK
) {
4536 * Record a kernel snapshot.
4538 * Return LTTNG_OK on success or a LTTNG_ERR code.
4540 static enum lttng_error_code
record_kernel_snapshot(
4541 struct ltt_kernel_session
*ksess
,
4542 const struct consumer_output
*output
,
4543 const struct ltt_session
*session
,
4544 int wait
, uint64_t nb_packets_per_stream
)
4546 enum lttng_error_code status
;
4552 status
= kernel_snapshot_record(
4553 ksess
, output
, wait
, nb_packets_per_stream
);
4558 * Record a UST snapshot.
4560 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4562 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4563 const struct consumer_output
*output
,
4564 const struct ltt_session
*session
,
4565 int wait
, uint64_t nb_packets_per_stream
)
4567 enum lttng_error_code status
;
4573 status
= ust_app_snapshot_record(
4574 usess
, output
, wait
, nb_packets_per_stream
);
4579 uint64_t get_session_size_one_more_packet_per_stream(
4580 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4582 uint64_t tot_size
= 0;
4584 if (session
->kernel_session
) {
4585 struct ltt_kernel_channel
*chan
;
4586 const struct ltt_kernel_session
*ksess
=
4587 session
->kernel_session
;
4589 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4590 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4592 * Don't take channel into account if we
4593 * already grab all its packets.
4597 tot_size
+= chan
->channel
->attr
.subbuf_size
4598 * chan
->stream_count
;
4602 if (session
->ust_session
) {
4603 const struct ltt_ust_session
*usess
= session
->ust_session
;
4605 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4613 * Calculate the number of packets we can grab from each stream that
4614 * fits within the overall snapshot max size.
4616 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4617 * the number of packets per stream.
4619 * TODO: this approach is not perfect: we consider the worse case
4620 * (packet filling the sub-buffers) as an upper bound, but we could do
4621 * better if we do this calculation while we actually grab the packet
4622 * content: we would know how much padding we don't actually store into
4625 * This algorithm is currently bounded by the number of packets per
4628 * Since we call this algorithm before actually grabbing the data, it's
4629 * an approximation: for instance, applications could appear/disappear
4630 * in between this call and actually grabbing data.
4633 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4637 uint64_t cur_nb_packets
= 0;
4640 return 0; /* Infinite */
4643 size_left
= max_size
;
4645 uint64_t one_more_packet_tot_size
;
4647 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4648 session
, cur_nb_packets
);
4649 if (!one_more_packet_tot_size
) {
4650 /* We are already grabbing all packets. */
4653 size_left
-= one_more_packet_tot_size
;
4654 if (size_left
< 0) {
4659 if (!cur_nb_packets
&& size_left
!= max_size
) {
4660 /* Not enough room to grab one packet of each stream, error. */
4663 return cur_nb_packets
;
4667 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4668 const struct snapshot_output
*snapshot_output
, int wait
)
4670 int64_t nb_packets_per_stream
;
4671 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4673 enum lttng_error_code ret_code
= LTTNG_OK
;
4674 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4675 struct consumer_output
*original_ust_consumer_output
= NULL
;
4676 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4677 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4678 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4680 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4682 snapshot_output
->name
,
4683 snapshot_output
->datetime
,
4684 snapshot_output
->nb_snapshot
);
4685 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4686 ERR("Failed to format snapshot name");
4687 ret_code
= LTTNG_ERR_INVALID
;
4690 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4691 snapshot_output
->name
, session
->name
,
4692 snapshot_chunk_name
);
4693 if (!session
->kernel_session
&& !session
->ust_session
) {
4694 ERR("Failed to record snapshot as no channels exist");
4695 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4699 if (session
->kernel_session
) {
4700 original_kernel_consumer_output
=
4701 session
->kernel_session
->consumer
;
4702 snapshot_kernel_consumer_output
=
4703 consumer_copy_output(snapshot_output
->consumer
);
4704 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
4705 snapshot_chunk_name
);
4706 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4707 original_kernel_consumer_output
);
4709 ERR("Failed to copy consumer sockets from snapshot output configuration");
4710 ret_code
= LTTNG_ERR_NOMEM
;
4713 ret_code
= set_relayd_for_snapshot(
4714 snapshot_kernel_consumer_output
, session
);
4715 if (ret_code
!= LTTNG_OK
) {
4716 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4719 session
->kernel_session
->consumer
=
4720 snapshot_kernel_consumer_output
;
4722 if (session
->ust_session
) {
4723 original_ust_consumer_output
= session
->ust_session
->consumer
;
4724 snapshot_ust_consumer_output
=
4725 consumer_copy_output(snapshot_output
->consumer
);
4726 strcpy(snapshot_ust_consumer_output
->chunk_path
,
4727 snapshot_chunk_name
);
4728 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4729 original_ust_consumer_output
);
4731 ERR("Failed to copy consumer sockets from snapshot output configuration");
4732 ret_code
= LTTNG_ERR_NOMEM
;
4735 ret_code
= set_relayd_for_snapshot(
4736 snapshot_ust_consumer_output
, session
);
4737 if (ret_code
!= LTTNG_OK
) {
4738 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4741 session
->ust_session
->consumer
=
4742 snapshot_ust_consumer_output
;
4745 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4746 snapshot_kernel_consumer_output
?:
4747 snapshot_ust_consumer_output
,
4748 consumer_output_get_base_path(
4749 snapshot_output
->consumer
),
4750 snapshot_chunk_name
);
4751 if (!snapshot_trace_chunk
) {
4752 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4754 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4757 assert(!session
->current_trace_chunk
);
4758 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4759 lttng_trace_chunk_put(snapshot_trace_chunk
);
4760 snapshot_trace_chunk
= NULL
;
4762 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4764 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4768 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4769 snapshot_output
->max_size
);
4770 if (nb_packets_per_stream
< 0) {
4771 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4772 goto error_close_trace_chunk
;
4775 if (session
->kernel_session
) {
4776 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4777 snapshot_kernel_consumer_output
, session
,
4778 wait
, nb_packets_per_stream
);
4779 if (ret_code
!= LTTNG_OK
) {
4780 goto error_close_trace_chunk
;
4784 if (session
->ust_session
) {
4785 ret_code
= record_ust_snapshot(session
->ust_session
,
4786 snapshot_ust_consumer_output
, session
,
4787 wait
, nb_packets_per_stream
);
4788 if (ret_code
!= LTTNG_OK
) {
4789 goto error_close_trace_chunk
;
4793 error_close_trace_chunk
:
4794 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
4795 ERR("Failed to release the current trace chunk of session \"%s\"",
4797 ret_code
= LTTNG_ERR_UNK
;
4800 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
4801 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
4803 * Don't goto end; make sure the chunk is closed for the session
4804 * to allow future snapshots.
4806 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4808 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4811 if (original_ust_consumer_output
) {
4812 session
->ust_session
->consumer
= original_ust_consumer_output
;
4814 if (original_kernel_consumer_output
) {
4815 session
->kernel_session
->consumer
=
4816 original_kernel_consumer_output
;
4818 consumer_output_put(snapshot_ust_consumer_output
);
4819 consumer_output_put(snapshot_kernel_consumer_output
);
4824 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4826 * The wait parameter is ignored so this call always wait for the snapshot to
4827 * complete before returning.
4829 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4831 int cmd_snapshot_record(struct ltt_session
*session
,
4832 const struct lttng_snapshot_output
*output
, int wait
)
4834 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4836 unsigned int snapshot_success
= 0;
4838 struct snapshot_output
*tmp_output
= NULL
;
4843 DBG("Cmd snapshot record for session %s", session
->name
);
4845 /* Get the datetime for the snapshot output directory. */
4846 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4849 cmd_ret
= LTTNG_ERR_INVALID
;
4854 * Permission denied to create an output if the session is not
4855 * set in no output mode.
4857 if (session
->output_traces
) {
4858 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4862 /* The session needs to be started at least once. */
4863 if (!session
->has_been_started
) {
4864 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4868 /* Use temporary output for the session. */
4869 if (*output
->ctrl_url
!= '\0') {
4870 tmp_output
= snapshot_output_alloc();
4872 cmd_ret
= LTTNG_ERR_NOMEM
;
4876 ret
= snapshot_output_init(session
, output
->max_size
,
4878 output
->ctrl_url
, output
->data_url
,
4882 if (ret
== -ENOMEM
) {
4883 cmd_ret
= LTTNG_ERR_NOMEM
;
4885 cmd_ret
= LTTNG_ERR_INVALID
;
4889 /* Use the global session count for the temporary snapshot. */
4890 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
4892 /* Use the global datetime */
4893 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
4894 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
4895 if (cmd_ret
!= LTTNG_OK
) {
4898 snapshot_success
= 1;
4900 struct snapshot_output
*sout
;
4901 struct lttng_ht_iter iter
;
4904 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4905 &iter
.iter
, sout
, node
.node
) {
4906 struct snapshot_output output_copy
;
4909 * Make a local copy of the output and override output
4910 * parameters with those provided as part of the
4913 memcpy(&output_copy
, sout
, sizeof(output_copy
));
4915 if (output
->max_size
!= (uint64_t) -1ULL) {
4916 output_copy
.max_size
= output
->max_size
;
4919 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4920 memcpy(output_copy
.datetime
, datetime
,
4923 /* Use temporary name. */
4924 if (*output
->name
!= '\0') {
4925 if (lttng_strncpy(output_copy
.name
,
4927 sizeof(output_copy
.name
))) {
4928 cmd_ret
= LTTNG_ERR_INVALID
;
4934 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
4935 if (cmd_ret
!= LTTNG_OK
) {
4939 snapshot_success
= 1;
4944 if (snapshot_success
) {
4945 session
->snapshot
.nb_snapshot
++;
4947 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4952 snapshot_output_destroy(tmp_output
);
4958 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4960 int cmd_set_session_shm_path(struct ltt_session
*session
,
4961 const char *shm_path
)
4967 * Can only set shm path before session is started.
4969 if (session
->has_been_started
) {
4970 return LTTNG_ERR_SESSION_STARTED
;
4973 strncpy(session
->shm_path
, shm_path
,
4974 sizeof(session
->shm_path
));
4975 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4981 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4983 * Ask the consumer to rotate the session output directory.
4984 * The session lock must be held.
4986 * Returns LTTNG_OK on success or else a negative LTTng error code.
4988 int cmd_rotate_session(struct ltt_session
*session
,
4989 struct lttng_rotate_session_return
*rotate_return
,
4990 bool quiet_rotation
,
4991 enum lttng_trace_chunk_command_type command
)
4994 uint64_t ongoing_rotation_chunk_id
;
4995 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4996 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
4997 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
4998 enum lttng_trace_chunk_status chunk_status
;
4999 bool failed_to_rotate
= false;
5000 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5004 if (!session
->has_been_started
) {
5005 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5010 * Explicit rotation is not supported for live sessions.
5011 * However, live sessions can perform a quiet rotation on
5013 * Rotation is not supported for snapshot traces (no output).
5015 if ((!quiet_rotation
&& session
->live_timer
) ||
5016 !session
->output_traces
) {
5017 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5021 /* Unsupported feature in lttng-relayd before 2.11. */
5022 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5023 (session
->consumer
->relay_major_version
== 2 &&
5024 session
->consumer
->relay_minor_version
< 11)) {
5025 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5029 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5030 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5031 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5035 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5036 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5038 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5043 * After a stop, we only allow one rotation to occur, the other ones are
5044 * useless until a new start.
5046 if (session
->rotated_after_last_stop
) {
5047 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5049 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5054 * After a stop followed by a clear, disallow following rotations a they would
5055 * generate empty chunks.
5057 if (session
->cleared_after_last_stop
) {
5058 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5060 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5064 if (session
->active
) {
5065 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5067 if (!new_trace_chunk
) {
5068 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5074 * The current trace chunk becomes the chunk being archived.
5076 * After this point, "chunk_being_archived" must absolutely
5077 * be closed on the consumer(s), otherwise it will never be
5078 * cleaned-up, which will result in a leak.
5080 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5081 &chunk_being_archived
);
5083 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5087 if (session
->kernel_session
) {
5088 cmd_ret
= kernel_rotate_session(session
);
5089 if (cmd_ret
!= LTTNG_OK
) {
5090 failed_to_rotate
= true;
5091 rotation_fail_code
= cmd_ret
;
5094 if (session
->ust_session
) {
5095 cmd_ret
= ust_app_rotate_session(session
);
5096 if (cmd_ret
!= LTTNG_OK
) {
5097 failed_to_rotate
= true;
5098 rotation_fail_code
= cmd_ret
;
5102 if (!session
->active
) {
5103 session
->rotated_after_last_stop
= true;
5106 if (!chunk_being_archived
) {
5107 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5109 if (failed_to_rotate
) {
5110 cmd_ret
= rotation_fail_code
;
5117 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5118 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5119 &ongoing_rotation_chunk_id
);
5120 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5122 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5123 command
, session
->last_chunk_path
);
5125 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5129 if (failed_to_rotate
) {
5130 cmd_ret
= rotation_fail_code
;
5134 session
->quiet_rotation
= quiet_rotation
;
5135 ret
= timer_session_rotation_pending_check_start(session
,
5136 DEFAULT_ROTATE_PENDING_TIMER
);
5138 cmd_ret
= LTTNG_ERR_UNK
;
5142 if (rotate_return
) {
5143 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5146 session
->chunk_being_archived
= chunk_being_archived
;
5147 chunk_being_archived
= NULL
;
5148 if (!quiet_rotation
) {
5149 ret
= notification_thread_command_session_rotation_ongoing(
5150 notification_thread_handle
,
5151 session
->name
, session
->uid
, session
->gid
,
5152 ongoing_rotation_chunk_id
);
5153 if (ret
!= LTTNG_OK
) {
5154 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5160 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5161 session
->name
, ongoing_rotation_chunk_id
);
5163 lttng_trace_chunk_put(new_trace_chunk
);
5164 lttng_trace_chunk_put(chunk_being_archived
);
5165 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5168 if (session_reset_rotation_state(session
,
5169 LTTNG_ROTATION_STATE_ERROR
)) {
5170 ERR("Failed to reset rotation state of session \"%s\"",
5177 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5179 * Check if the session has finished its rotation.
5181 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5183 int cmd_rotate_get_info(struct ltt_session
*session
,
5184 struct lttng_rotation_get_info_return
*info_return
,
5185 uint64_t rotation_id
)
5187 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5188 enum lttng_rotation_state rotation_state
;
5190 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5191 session
->most_recent_chunk_id
.value
);
5193 if (session
->chunk_being_archived
) {
5194 enum lttng_trace_chunk_status chunk_status
;
5197 chunk_status
= lttng_trace_chunk_get_id(
5198 session
->chunk_being_archived
,
5200 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5202 rotation_state
= rotation_id
== chunk_id
?
5203 LTTNG_ROTATION_STATE_ONGOING
:
5204 LTTNG_ROTATION_STATE_EXPIRED
;
5206 if (session
->last_archived_chunk_id
.is_set
&&
5207 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5208 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5210 rotation_state
= session
->rotation_state
;
5214 switch (rotation_state
) {
5215 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5216 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5219 case LTTNG_ROTATION_STATE_EXPIRED
:
5220 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5221 rotation_id
, session
->name
);
5223 case LTTNG_ROTATION_STATE_ONGOING
:
5224 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5225 rotation_id
, session
->name
);
5227 case LTTNG_ROTATION_STATE_COMPLETED
:
5231 char *current_tracing_path_reply
;
5232 size_t current_tracing_path_reply_len
;
5234 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5235 rotation_id
, session
->name
);
5237 switch (session_get_consumer_destination_type(session
)) {
5238 case CONSUMER_DST_LOCAL
:
5239 current_tracing_path_reply
=
5240 info_return
->location
.local
.absolute_path
;
5241 current_tracing_path_reply_len
=
5242 sizeof(info_return
->location
.local
.absolute_path
);
5243 info_return
->location_type
=
5244 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5245 fmt_ret
= asprintf(&chunk_path
,
5246 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5247 session_get_base_path(session
),
5248 session
->last_archived_chunk_name
);
5249 if (fmt_ret
== -1) {
5250 PERROR("Failed to format the path of the last archived trace chunk");
5251 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5252 cmd_ret
= LTTNG_ERR_UNK
;
5256 case CONSUMER_DST_NET
:
5258 uint16_t ctrl_port
, data_port
;
5260 current_tracing_path_reply
=
5261 info_return
->location
.relay
.relative_path
;
5262 current_tracing_path_reply_len
=
5263 sizeof(info_return
->location
.relay
.relative_path
);
5264 /* Currently the only supported relay protocol. */
5265 info_return
->location
.relay
.protocol
=
5266 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5268 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5269 session_get_net_consumer_hostname(session
),
5270 sizeof(info_return
->location
.relay
.host
));
5272 ERR("Failed to copy host name to rotate_get_info reply");
5273 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5274 cmd_ret
= LTTNG_ERR_SET_URL
;
5278 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5279 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5280 info_return
->location
.relay
.ports
.data
= data_port
;
5281 info_return
->location_type
=
5282 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5283 chunk_path
= strdup(session
->last_chunk_path
);
5285 ERR("Failed to allocate the path of the last archived trace chunk");
5286 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5287 cmd_ret
= LTTNG_ERR_UNK
;
5296 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5297 chunk_path
, current_tracing_path_reply_len
);
5300 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5301 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5302 cmd_ret
= LTTNG_ERR_UNK
;
5308 case LTTNG_ROTATION_STATE_ERROR
:
5309 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5310 rotation_id
, session
->name
);
5318 info_return
->status
= (int32_t) rotation_state
;
5323 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5325 * Configure the automatic rotation parameters.
5326 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5327 * 'activate' to false means deactivate the rotation schedule and validate that
5328 * 'new_value' has the same value as the currently active value.
5330 * Return 0 on success or else a positive LTTNG_ERR code.
5332 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5333 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5335 struct notification_thread_handle
*notification_thread_handle
)
5338 uint64_t *parameter_value
;
5342 DBG("Cmd rotate set schedule session %s", session
->name
);
5344 if (session
->live_timer
|| !session
->output_traces
) {
5345 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5346 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5350 switch (schedule_type
) {
5351 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5352 parameter_value
= &session
->rotate_size
;
5354 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5355 parameter_value
= &session
->rotate_timer_period
;
5356 if (new_value
>= UINT_MAX
) {
5357 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5358 new_value
, UINT_MAX
);
5359 ret
= LTTNG_ERR_INVALID
;
5364 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5365 ret
= LTTNG_ERR_INVALID
;
5369 /* Improper use of the API. */
5370 if (new_value
== -1ULL) {
5371 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5372 ret
= LTTNG_ERR_INVALID
;
5377 * As indicated in struct ltt_session's comments, a value of == 0 means
5378 * this schedule rotation type is not in use.
5380 * Reject the command if we were asked to activate a schedule that was
5383 if (activate
&& *parameter_value
!= 0) {
5384 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5385 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5390 * Reject the command if we were asked to deactivate a schedule that was
5393 if (!activate
&& *parameter_value
== 0) {
5394 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5395 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5400 * Reject the command if we were asked to deactivate a schedule that
5403 if (!activate
&& *parameter_value
!= new_value
) {
5404 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5405 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5409 *parameter_value
= activate
? new_value
: 0;
5411 switch (schedule_type
) {
5412 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5413 if (activate
&& session
->active
) {
5415 * Only start the timer if the session is active,
5416 * otherwise it will be started when the session starts.
5418 ret
= timer_session_rotation_schedule_timer_start(
5419 session
, new_value
);
5421 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5422 ret
= LTTNG_ERR_UNK
;
5426 ret
= timer_session_rotation_schedule_timer_stop(
5429 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5430 ret
= LTTNG_ERR_UNK
;
5435 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5437 ret
= subscribe_session_consumed_size_rotation(session
,
5438 new_value
, notification_thread_handle
);
5440 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5441 ret
= LTTNG_ERR_UNK
;
5445 ret
= unsubscribe_session_consumed_size_rotation(session
,
5446 notification_thread_handle
);
5448 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5449 ret
= LTTNG_ERR_UNK
;
5456 /* Would have been caught before. */
5468 /* Wait for a given path to be removed before continuing. */
5469 static enum lttng_error_code
wait_on_path(void *path_data
)
5471 const char *shm_path
= path_data
;
5473 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5479 ret
= stat(shm_path
, &st
);
5481 if (errno
!= ENOENT
) {
5482 PERROR("stat() returned an error while checking for the existence of the shm path");
5484 DBG("shm path no longer exists, completing the destruction of session");
5488 if (!S_ISDIR(st
.st_mode
)) {
5489 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5494 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5500 * Returns a pointer to a handler to run on completion of a command.
5501 * Returns NULL if no handler has to be run for the last command executed.
5503 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5505 struct cmd_completion_handler
*handler
= current_completion_handler
;
5507 current_completion_handler
= NULL
;
5512 * Init command subsystem.
5517 * Set network sequence index to 1 for streams to match a relayd
5518 * socket on the consumer side.
5520 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5521 relayd_net_seq_idx
= 1;
5522 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5524 DBG("Command subsystem initialized");