2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <urcu/list.h>
23 #include <urcu/uatomic.h>
26 #include <common/defaults.h>
27 #include <common/common.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/relayd/relayd.h>
30 #include <common/utils.h>
31 #include <common/compat/string.h>
32 #include <common/kernel-ctl/kernel-ctl.h>
33 #include <common/dynamic-buffer.h>
34 #include <common/buffer-view.h>
35 #include <lttng/trigger/trigger-internal.h>
36 #include <lttng/condition/condition.h>
37 #include <lttng/action/action.h>
38 #include <lttng/channel.h>
39 #include <lttng/channel-internal.h>
40 #include <lttng/rotate-internal.h>
41 #include <lttng/location-internal.h>
42 #include <lttng/userspace-probe-internal.h>
43 #include <common/string-utils/string-utils.h>
48 #include "health-sessiond.h"
50 #include "kernel-consumer.h"
51 #include "lttng-sessiond.h"
53 #include "lttng-syscall.h"
55 #include "buffer-registry.h"
56 #include "notification-thread.h"
57 #include "notification-thread-commands.h"
59 #include "rotation-thread.h"
60 #include "sessiond-timer.h"
61 #include "agent-thread.h"
65 /* Sleep for 100ms between each check for the shm path's deletion. */
66 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
68 static enum lttng_error_code
wait_on_path(void *path
);
71 * Command completion handler that is used by the destroy command
72 * when a session that has a non-default shm_path is being destroyed.
74 * See comment in cmd_destroy_session() for the rationale.
76 static struct destroy_completion_handler
{
77 struct cmd_completion_handler handler
;
78 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
79 } destroy_completion_handler
= {
82 .data
= destroy_completion_handler
.shm_path
87 static struct cmd_completion_handler
*current_completion_handler
;
90 * Used to keep a unique index for each relayd socket created where this value
91 * is associated with streams on the consumer so it can match the right relayd
92 * to send to. It must be accessed with the relayd_net_seq_idx_lock
95 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
96 static uint64_t relayd_net_seq_idx
;
98 static int validate_ust_event_name(const char *);
99 static int cmd_enable_event_internal(struct ltt_session
*session
,
100 struct lttng_domain
*domain
,
101 char *channel_name
, struct lttng_event
*event
,
102 char *filter_expression
,
103 struct lttng_filter_bytecode
*filter
,
104 struct lttng_event_exclusion
*exclusion
,
108 * Create a session path used by list_lttng_sessions for the case that the
109 * session consumer is on the network.
111 static int build_network_session_path(char *dst
, size_t size
,
112 struct ltt_session
*session
)
114 int ret
, kdata_port
, udata_port
;
115 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
116 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
121 memset(tmp_urls
, 0, sizeof(tmp_urls
));
122 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
124 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
126 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
127 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
128 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
131 if (session
->ust_session
&& session
->ust_session
->consumer
) {
132 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
133 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
136 if (uuri
== NULL
&& kuri
== NULL
) {
137 uri
= &session
->consumer
->dst
.net
.control
;
138 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
139 } else if (kuri
&& uuri
) {
140 ret
= uri_compare(kuri
, uuri
);
144 /* Build uuri URL string */
145 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
152 } else if (kuri
&& uuri
== NULL
) {
154 } else if (uuri
&& kuri
== NULL
) {
158 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
164 * Do we have a UST url set. If yes, this means we have both kernel and UST
167 if (*tmp_uurl
!= '\0') {
168 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
169 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
172 if (kuri
|| (!kuri
&& !uuri
)) {
175 /* No kernel URI, use the UST port. */
178 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
186 * Get run-time attributes if the session has been started (discarded events,
189 static int get_kernel_runtime_stats(struct ltt_session
*session
,
190 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
191 uint64_t *lost_packets
)
195 if (!session
->has_been_started
) {
197 *discarded_events
= 0;
202 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
203 session
->kernel_session
->consumer
,
209 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
210 session
->kernel_session
->consumer
,
221 * Get run-time attributes if the session has been started (discarded events,
224 static int get_ust_runtime_stats(struct ltt_session
*session
,
225 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
226 uint64_t *lost_packets
)
229 struct ltt_ust_session
*usess
;
231 if (!discarded_events
|| !lost_packets
) {
236 usess
= session
->ust_session
;
237 assert(discarded_events
);
238 assert(lost_packets
);
240 if (!usess
|| !session
->has_been_started
) {
241 *discarded_events
= 0;
247 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
248 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
249 &usess
->buffer_reg_uid_list
,
250 usess
->consumer
, uchan
->id
,
251 uchan
->attr
.overwrite
,
254 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
255 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
256 uchan
, usess
->consumer
,
257 uchan
->attr
.overwrite
,
263 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
264 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
266 ERR("Unsupported buffer type");
277 * Fill lttng_channel array of all channels.
279 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
280 struct ltt_session
*session
, struct lttng_channel
*channels
,
281 struct lttng_channel_extended
*chan_exts
)
284 struct ltt_kernel_channel
*kchan
;
286 DBG("Listing channels for session %s", session
->name
);
289 case LTTNG_DOMAIN_KERNEL
:
290 /* Kernel channels */
291 if (session
->kernel_session
!= NULL
) {
292 cds_list_for_each_entry(kchan
,
293 &session
->kernel_session
->channel_list
.head
, list
) {
294 uint64_t discarded_events
, lost_packets
;
295 struct lttng_channel_extended
*extended
;
297 extended
= (struct lttng_channel_extended
*)
298 kchan
->channel
->attr
.extended
.ptr
;
300 ret
= get_kernel_runtime_stats(session
, kchan
,
301 &discarded_events
, &lost_packets
);
305 /* Copy lttng_channel struct to array */
306 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
307 channels
[i
].enabled
= kchan
->enabled
;
308 chan_exts
[i
].discarded_events
=
310 chan_exts
[i
].lost_packets
= lost_packets
;
311 chan_exts
[i
].monitor_timer_interval
=
312 extended
->monitor_timer_interval
;
313 chan_exts
[i
].blocking_timeout
= 0;
318 case LTTNG_DOMAIN_UST
:
320 struct lttng_ht_iter iter
;
321 struct ltt_ust_channel
*uchan
;
324 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
325 &iter
.iter
, uchan
, node
.node
) {
326 uint64_t discarded_events
= 0, lost_packets
= 0;
328 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
329 LTTNG_SYMBOL_NAME_LEN
)) {
332 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
333 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
334 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
335 channels
[i
].attr
.switch_timer_interval
=
336 uchan
->attr
.switch_timer_interval
;
337 channels
[i
].attr
.read_timer_interval
=
338 uchan
->attr
.read_timer_interval
;
339 channels
[i
].enabled
= uchan
->enabled
;
340 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
341 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
344 * Map enum lttng_ust_output to enum lttng_event_output.
346 switch (uchan
->attr
.output
) {
348 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
352 * LTTNG_UST_MMAP is the only supported UST
359 chan_exts
[i
].monitor_timer_interval
=
360 uchan
->monitor_timer_interval
;
361 chan_exts
[i
].blocking_timeout
=
362 uchan
->attr
.u
.s
.blocking_timeout
;
364 ret
= get_ust_runtime_stats(session
, uchan
,
365 &discarded_events
, &lost_packets
);
369 chan_exts
[i
].discarded_events
= discarded_events
;
370 chan_exts
[i
].lost_packets
= lost_packets
;
382 return -LTTNG_ERR_FATAL
;
388 static int increment_extended_len(const char *filter_expression
,
389 struct lttng_event_exclusion
*exclusion
,
390 const struct lttng_userspace_probe_location
*probe_location
,
391 size_t *extended_len
)
395 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
397 if (filter_expression
) {
398 *extended_len
+= strlen(filter_expression
) + 1;
402 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
405 if (probe_location
) {
406 ret
= lttng_userspace_probe_location_serialize(probe_location
,
411 *extended_len
+= ret
;
418 static int append_extended_info(const char *filter_expression
,
419 struct lttng_event_exclusion
*exclusion
,
420 struct lttng_userspace_probe_location
*probe_location
,
424 size_t filter_len
= 0;
425 size_t nb_exclusions
= 0;
426 size_t userspace_probe_location_len
= 0;
427 struct lttng_dynamic_buffer location_buffer
;
428 struct lttcomm_event_extended_header extended_header
;
430 if (filter_expression
) {
431 filter_len
= strlen(filter_expression
) + 1;
435 nb_exclusions
= exclusion
->count
;
438 if (probe_location
) {
439 lttng_dynamic_buffer_init(&location_buffer
);
440 ret
= lttng_userspace_probe_location_serialize(probe_location
,
441 &location_buffer
, NULL
);
446 userspace_probe_location_len
= location_buffer
.size
;
449 /* Set header fields */
450 extended_header
.filter_len
= filter_len
;
451 extended_header
.nb_exclusions
= nb_exclusions
;
452 extended_header
.userspace_probe_location_len
= userspace_probe_location_len
;
455 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
456 *extended_at
+= sizeof(extended_header
);
458 /* Copy filter string */
459 if (filter_expression
) {
460 memcpy(*extended_at
, filter_expression
, filter_len
);
461 *extended_at
+= filter_len
;
464 /* Copy exclusion names */
466 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
468 memcpy(*extended_at
, &exclusion
->names
, len
);
472 if (probe_location
) {
473 memcpy(*extended_at
, location_buffer
.data
, location_buffer
.size
);
474 *extended_at
+= location_buffer
.size
;
475 lttng_dynamic_buffer_reset(&location_buffer
);
483 * Create a list of agent domain events.
485 * Return number of events in list on success or else a negative value.
487 static int list_lttng_agent_events(struct agent
*agt
,
488 struct lttng_event
**events
, size_t *total_size
)
491 unsigned int nb_event
= 0;
492 struct agent_event
*event
;
493 struct lttng_event
*tmp_events
;
494 struct lttng_ht_iter iter
;
495 size_t extended_len
= 0;
501 DBG3("Listing agent events");
504 nb_event
= lttng_ht_get_count(agt
->events
);
512 /* Compute required extended infos size */
513 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
516 * This is only valid because the commands which add events are
517 * processed in the same thread as the listing.
520 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
521 ret
= increment_extended_len(event
->filter_expression
, NULL
, NULL
,
524 DBG("Error computing the length of extended info message");
525 ret
= -LTTNG_ERR_FATAL
;
531 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
532 tmp_events
= zmalloc(*total_size
);
534 PERROR("zmalloc agent events session");
535 ret
= -LTTNG_ERR_FATAL
;
539 extended_at
= ((uint8_t *) tmp_events
) +
540 nb_event
* sizeof(struct lttng_event
);
543 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
544 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
545 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
546 tmp_events
[i
].enabled
= event
->enabled
;
547 tmp_events
[i
].loglevel
= event
->loglevel_value
;
548 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
551 /* Append extended info */
552 ret
= append_extended_info(event
->filter_expression
, NULL
, NULL
,
555 DBG("Error appending extended info message");
556 ret
= -LTTNG_ERR_FATAL
;
561 *events
= tmp_events
;
563 assert(nb_event
== i
);
571 * Create a list of ust global domain events.
573 static int list_lttng_ust_global_events(char *channel_name
,
574 struct ltt_ust_domain_global
*ust_global
,
575 struct lttng_event
**events
, size_t *total_size
)
578 unsigned int nb_event
= 0;
579 struct lttng_ht_iter iter
;
580 struct lttng_ht_node_str
*node
;
581 struct ltt_ust_channel
*uchan
;
582 struct ltt_ust_event
*uevent
;
583 struct lttng_event
*tmp
;
584 size_t extended_len
= 0;
587 DBG("Listing UST global events for channel %s", channel_name
);
591 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
592 node
= lttng_ht_iter_get_node_str(&iter
);
594 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
598 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
600 nb_event
= lttng_ht_get_count(uchan
->events
);
607 DBG3("Listing UST global %d events", nb_event
);
609 /* Compute required extended infos size */
610 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
611 if (uevent
->internal
) {
616 ret
= increment_extended_len(uevent
->filter_expression
,
617 uevent
->exclusion
, NULL
, &extended_len
);
619 DBG("Error computing the length of extended info message");
620 ret
= -LTTNG_ERR_FATAL
;
625 /* All events are internal, skip. */
631 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
632 tmp
= zmalloc(*total_size
);
634 ret
= -LTTNG_ERR_FATAL
;
638 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
640 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
641 if (uevent
->internal
) {
642 /* This event should remain hidden from clients */
645 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
646 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
647 tmp
[i
].enabled
= uevent
->enabled
;
649 switch (uevent
->attr
.instrumentation
) {
650 case LTTNG_UST_TRACEPOINT
:
651 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
653 case LTTNG_UST_PROBE
:
654 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
656 case LTTNG_UST_FUNCTION
:
657 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
661 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
662 switch (uevent
->attr
.loglevel_type
) {
663 case LTTNG_UST_LOGLEVEL_ALL
:
664 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
666 case LTTNG_UST_LOGLEVEL_RANGE
:
667 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
669 case LTTNG_UST_LOGLEVEL_SINGLE
:
670 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
673 if (uevent
->filter
) {
676 if (uevent
->exclusion
) {
677 tmp
[i
].exclusion
= 1;
681 /* Append extended info */
682 ret
= append_extended_info(uevent
->filter_expression
,
683 uevent
->exclusion
, NULL
, &extended_at
);
685 DBG("Error appending extended info message");
686 ret
= -LTTNG_ERR_FATAL
;
699 * Fill lttng_event array of all kernel events in the channel.
701 static int list_lttng_kernel_events(char *channel_name
,
702 struct ltt_kernel_session
*kernel_session
,
703 struct lttng_event
**events
, size_t *total_size
)
706 unsigned int nb_event
;
707 struct ltt_kernel_event
*event
;
708 struct ltt_kernel_channel
*kchan
;
709 size_t extended_len
= 0;
712 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
714 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
718 nb_event
= kchan
->event_count
;
720 DBG("Listing events for channel %s", kchan
->channel
->name
);
728 /* Compute required extended infos size */
729 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
730 ret
= increment_extended_len(event
->filter_expression
, NULL
,
731 event
->userspace_probe_location
,
734 DBG("Error computing the length of extended info message");
735 ret
= -LTTNG_ERR_FATAL
;
740 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
741 *events
= zmalloc(*total_size
);
742 if (*events
== NULL
) {
743 ret
= -LTTNG_ERR_FATAL
;
747 extended_at
= ((void *) *events
) +
748 nb_event
* sizeof(struct lttng_event
);
750 /* Kernel channels */
751 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
752 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
753 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
754 (*events
)[i
].enabled
= event
->enabled
;
755 (*events
)[i
].filter
=
756 (unsigned char) !!event
->filter_expression
;
758 switch (event
->event
->instrumentation
) {
759 case LTTNG_KERNEL_TRACEPOINT
:
760 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
762 case LTTNG_KERNEL_KRETPROBE
:
763 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
764 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
765 sizeof(struct lttng_kernel_kprobe
));
767 case LTTNG_KERNEL_KPROBE
:
768 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
769 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
770 sizeof(struct lttng_kernel_kprobe
));
772 case LTTNG_KERNEL_FUNCTION
:
773 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
774 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
775 sizeof(struct lttng_kernel_function
));
777 case LTTNG_KERNEL_NOOP
:
778 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
780 case LTTNG_KERNEL_SYSCALL
:
781 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
783 case LTTNG_KERNEL_ALL
:
791 /* Append extended info */
792 ret
= append_extended_info(event
->filter_expression
, NULL
,
793 event
->userspace_probe_location
, &extended_at
);
795 DBG("Error appending extended info message");
796 ret
= -LTTNG_ERR_FATAL
;
805 /* Negate the error code to differentiate the size from an error */
810 * Add URI so the consumer output object. Set the correct path depending on the
811 * domain adding the default trace directory.
813 static int add_uri_to_consumer(struct consumer_output
*consumer
,
814 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
815 const char *session_name
)
818 const char *default_trace_dir
;
822 if (consumer
== NULL
) {
823 DBG("No consumer detected. Don't add URI. Stopping.");
824 ret
= LTTNG_ERR_NO_CONSUMER
;
829 case LTTNG_DOMAIN_KERNEL
:
830 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
832 case LTTNG_DOMAIN_UST
:
833 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
837 * This case is possible is we try to add the URI to the global tracing
838 * session consumer object which in this case there is no subdir.
840 default_trace_dir
= "";
843 switch (uri
->dtype
) {
846 DBG2("Setting network URI to consumer");
848 if (consumer
->type
== CONSUMER_DST_NET
) {
849 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
850 consumer
->dst
.net
.control_isset
) ||
851 (uri
->stype
== LTTNG_STREAM_DATA
&&
852 consumer
->dst
.net
.data_isset
)) {
853 ret
= LTTNG_ERR_URL_EXIST
;
857 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
860 consumer
->type
= CONSUMER_DST_NET
;
862 /* Set URI into consumer output object */
863 ret
= consumer_set_network_uri(consumer
, uri
);
867 } else if (ret
== 1) {
869 * URI was the same in the consumer so we do not append the subdir
870 * again so to not duplicate output dir.
876 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
877 ret
= consumer_set_subdir(consumer
, session_name
);
879 ret
= LTTNG_ERR_FATAL
;
884 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
885 /* On a new subdir, reappend the default trace dir. */
886 strncat(consumer
->subdir
, default_trace_dir
,
887 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
888 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
893 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
894 memset(consumer
->dst
.session_root_path
, 0,
895 sizeof(consumer
->dst
.session_root_path
));
896 /* Explicit length checks for strcpy and strcat. */
897 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
898 >= sizeof(consumer
->dst
.session_root_path
)) {
899 ret
= LTTNG_ERR_FATAL
;
902 strcpy(consumer
->dst
.session_root_path
, uri
->dst
.path
);
903 /* Append default trace dir */
904 strcat(consumer
->dst
.session_root_path
, default_trace_dir
);
905 /* Flag consumer as local. */
906 consumer
->type
= CONSUMER_DST_LOCAL
;
917 * Init tracing by creating trace directory and sending fds kernel consumer.
919 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
922 struct lttng_ht_iter iter
;
923 struct consumer_socket
*socket
;
929 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
930 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
932 pthread_mutex_lock(socket
->lock
);
933 ret
= kernel_consumer_send_session(socket
, session
);
934 pthread_mutex_unlock(socket
->lock
);
936 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
948 * Create a socket to the relayd using the URI.
950 * On success, the relayd_sock pointer is set to the created socket.
951 * Else, it's stays untouched and a lttcomm error code is returned.
953 static int create_connect_relayd(struct lttng_uri
*uri
,
954 struct lttcomm_relayd_sock
**relayd_sock
,
955 struct consumer_output
*consumer
)
958 struct lttcomm_relayd_sock
*rsock
;
960 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
961 RELAYD_VERSION_COMM_MINOR
);
963 ret
= LTTNG_ERR_FATAL
;
968 * Connect to relayd so we can proceed with a session creation. This call
969 * can possibly block for an arbitrary amount of time to set the health
970 * state to be in poll execution.
973 ret
= relayd_connect(rsock
);
976 ERR("Unable to reach lttng-relayd");
977 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
981 /* Create socket for control stream. */
982 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
983 DBG3("Creating relayd stream socket from URI");
985 /* Check relayd version */
986 ret
= relayd_version_check(rsock
);
987 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
989 } else if (ret
< 0) {
990 ERR("Unable to reach lttng-relayd");
991 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
994 consumer
->relay_major_version
= rsock
->major
;
995 consumer
->relay_minor_version
= rsock
->minor
;
996 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
997 DBG3("Creating relayd data socket from URI");
999 /* Command is not valid */
1000 ERR("Relayd invalid stream type: %d", uri
->stype
);
1001 ret
= LTTNG_ERR_INVALID
;
1005 *relayd_sock
= rsock
;
1010 /* The returned value is not useful since we are on an error path. */
1011 (void) relayd_close(rsock
);
1019 * Connect to the relayd using URI and send the socket to the right consumer.
1021 * The consumer socket lock must be held by the caller.
1023 static int send_consumer_relayd_socket(unsigned int session_id
,
1024 struct lttng_uri
*relayd_uri
,
1025 struct consumer_output
*consumer
,
1026 struct consumer_socket
*consumer_sock
,
1027 char *session_name
, char *hostname
, int session_live_timer
)
1030 struct lttcomm_relayd_sock
*rsock
= NULL
;
1032 /* Connect to relayd and make version check if uri is the control. */
1033 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1034 if (ret
!= LTTNG_OK
) {
1035 goto relayd_comm_error
;
1039 /* Set the network sequence index if not set. */
1040 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1041 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1043 * Increment net_seq_idx because we are about to transfer the
1044 * new relayd socket to the consumer.
1045 * Assign unique key so the consumer can match streams.
1047 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1048 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1051 /* Send relayd socket to consumer. */
1052 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1053 relayd_uri
->stype
, session_id
,
1054 session_name
, hostname
, session_live_timer
);
1056 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1060 /* Flag that the corresponding socket was sent. */
1061 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1062 consumer_sock
->control_sock_sent
= 1;
1063 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1064 consumer_sock
->data_sock_sent
= 1;
1070 * Close socket which was dup on the consumer side. The session daemon does
1071 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1075 if (ret
!= LTTNG_OK
) {
1077 * The consumer output for this session should not be used anymore
1078 * since the relayd connection failed thus making any tracing or/and
1079 * streaming not usable.
1081 consumer
->enabled
= 0;
1083 (void) relayd_close(rsock
);
1091 * Send both relayd sockets to a specific consumer and domain. This is a
1092 * helper function to facilitate sending the information to the consumer for a
1095 * The consumer socket lock must be held by the caller.
1097 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
1098 unsigned int session_id
, struct consumer_output
*consumer
,
1099 struct consumer_socket
*sock
, char *session_name
,
1100 char *hostname
, int session_live_timer
)
1107 /* Sending control relayd socket. */
1108 if (!sock
->control_sock_sent
) {
1109 ret
= send_consumer_relayd_socket(session_id
,
1110 &consumer
->dst
.net
.control
, consumer
, sock
,
1111 session_name
, hostname
, session_live_timer
);
1112 if (ret
!= LTTNG_OK
) {
1117 /* Sending data relayd socket. */
1118 if (!sock
->data_sock_sent
) {
1119 ret
= send_consumer_relayd_socket(session_id
,
1120 &consumer
->dst
.net
.data
, consumer
, sock
,
1121 session_name
, hostname
, session_live_timer
);
1122 if (ret
!= LTTNG_OK
) {
1132 * Setup relayd connections for a tracing session. First creates the socket to
1133 * the relayd and send them to the right domain consumer. Consumer type MUST be
1136 int cmd_setup_relayd(struct ltt_session
*session
)
1139 struct ltt_ust_session
*usess
;
1140 struct ltt_kernel_session
*ksess
;
1141 struct consumer_socket
*socket
;
1142 struct lttng_ht_iter iter
;
1146 usess
= session
->ust_session
;
1147 ksess
= session
->kernel_session
;
1149 DBG("Setting relayd for session %s", session
->name
);
1153 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1154 && usess
->consumer
->enabled
) {
1155 /* For each consumer socket, send relayd sockets */
1156 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1157 socket
, node
.node
) {
1158 pthread_mutex_lock(socket
->lock
);
1159 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1160 usess
->consumer
, socket
,
1161 session
->name
, session
->hostname
,
1162 session
->live_timer
);
1163 pthread_mutex_unlock(socket
->lock
);
1164 if (ret
!= LTTNG_OK
) {
1167 /* Session is now ready for network streaming. */
1168 session
->net_handle
= 1;
1170 session
->consumer
->relay_major_version
=
1171 usess
->consumer
->relay_major_version
;
1172 session
->consumer
->relay_minor_version
=
1173 usess
->consumer
->relay_minor_version
;
1176 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1177 && ksess
->consumer
->enabled
) {
1178 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1179 socket
, node
.node
) {
1180 pthread_mutex_lock(socket
->lock
);
1181 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1182 ksess
->consumer
, socket
,
1183 session
->name
, session
->hostname
,
1184 session
->live_timer
);
1185 pthread_mutex_unlock(socket
->lock
);
1186 if (ret
!= LTTNG_OK
) {
1189 /* Session is now ready for network streaming. */
1190 session
->net_handle
= 1;
1192 session
->consumer
->relay_major_version
=
1193 ksess
->consumer
->relay_major_version
;
1194 session
->consumer
->relay_minor_version
=
1195 ksess
->consumer
->relay_minor_version
;
1204 * Start a kernel session by opening all necessary streams.
1206 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1209 struct ltt_kernel_channel
*kchan
;
1211 /* Open kernel metadata */
1212 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1213 ret
= kernel_open_metadata(ksess
);
1215 ret
= LTTNG_ERR_KERN_META_FAIL
;
1220 /* Open kernel metadata stream */
1221 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1222 ret
= kernel_open_metadata_stream(ksess
);
1224 ERR("Kernel create metadata stream failed");
1225 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1230 /* For each channel */
1231 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1232 if (kchan
->stream_count
== 0) {
1233 ret
= kernel_open_channel_stream(kchan
);
1235 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1238 /* Update the stream global counter */
1239 ksess
->stream_count_global
+= ret
;
1243 /* Setup kernel consumer socket and send fds to it */
1244 ret
= init_kernel_tracing(ksess
);
1246 ret
= LTTNG_ERR_KERN_START_FAIL
;
1250 /* This start the kernel tracing */
1251 ret
= kernel_start_session(ksess
);
1253 ret
= LTTNG_ERR_KERN_START_FAIL
;
1257 /* Quiescent wait after starting trace */
1258 kernel_wait_quiescent(wpipe
);
1269 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1271 int cmd_disable_channel(struct ltt_session
*session
,
1272 enum lttng_domain_type domain
, char *channel_name
)
1275 struct ltt_ust_session
*usess
;
1277 usess
= session
->ust_session
;
1282 case LTTNG_DOMAIN_KERNEL
:
1284 ret
= channel_kernel_disable(session
->kernel_session
,
1286 if (ret
!= LTTNG_OK
) {
1290 kernel_wait_quiescent(kernel_tracer_fd
);
1293 case LTTNG_DOMAIN_UST
:
1295 struct ltt_ust_channel
*uchan
;
1296 struct lttng_ht
*chan_ht
;
1298 chan_ht
= usess
->domain_global
.channels
;
1300 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1301 if (uchan
== NULL
) {
1302 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1306 ret
= channel_ust_disable(usess
, uchan
);
1307 if (ret
!= LTTNG_OK
) {
1313 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1325 * Command LTTNG_TRACK_PID processed by the client thread.
1327 * Called with session lock held.
1329 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1337 case LTTNG_DOMAIN_KERNEL
:
1339 struct ltt_kernel_session
*ksess
;
1341 ksess
= session
->kernel_session
;
1343 ret
= kernel_track_pid(ksess
, pid
);
1344 if (ret
!= LTTNG_OK
) {
1348 kernel_wait_quiescent(kernel_tracer_fd
);
1351 case LTTNG_DOMAIN_UST
:
1353 struct ltt_ust_session
*usess
;
1355 usess
= session
->ust_session
;
1357 ret
= trace_ust_track_pid(usess
, pid
);
1358 if (ret
!= LTTNG_OK
) {
1364 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1376 * Command LTTNG_UNTRACK_PID processed by the client thread.
1378 * Called with session lock held.
1380 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1388 case LTTNG_DOMAIN_KERNEL
:
1390 struct ltt_kernel_session
*ksess
;
1392 ksess
= session
->kernel_session
;
1394 ret
= kernel_untrack_pid(ksess
, pid
);
1395 if (ret
!= LTTNG_OK
) {
1399 kernel_wait_quiescent(kernel_tracer_fd
);
1402 case LTTNG_DOMAIN_UST
:
1404 struct ltt_ust_session
*usess
;
1406 usess
= session
->ust_session
;
1408 ret
= trace_ust_untrack_pid(usess
, pid
);
1409 if (ret
!= LTTNG_OK
) {
1415 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1427 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1429 * The wpipe arguments is used as a notifier for the kernel thread.
1431 int cmd_enable_channel(struct ltt_session
*session
,
1432 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1435 struct ltt_ust_session
*usess
= session
->ust_session
;
1436 struct lttng_ht
*chan_ht
;
1443 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1445 /* Validate channel name */
1446 if (attr
->name
[0] == '.' ||
1447 memchr(attr
->name
, '/', len
) != NULL
) {
1448 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1452 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1457 * Don't try to enable a channel if the session has been started at
1458 * some point in time before. The tracer does not allow it.
1460 if (session
->has_been_started
) {
1461 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1466 * If the session is a live session, remove the switch timer, the
1467 * live timer does the same thing but sends also synchronisation
1468 * beacons for inactive streams.
1470 if (session
->live_timer
> 0) {
1471 attr
->attr
.live_timer_interval
= session
->live_timer
;
1472 attr
->attr
.switch_timer_interval
= 0;
1475 /* Check for feature support */
1476 switch (domain
->type
) {
1477 case LTTNG_DOMAIN_KERNEL
:
1479 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd
) != 1) {
1480 /* Sampling position of buffer is not supported */
1481 WARN("Kernel tracer does not support buffer monitoring. "
1482 "Setting the monitor interval timer to 0 "
1483 "(disabled) for channel '%s' of session '%s'",
1484 attr
-> name
, session
->name
);
1485 lttng_channel_set_monitor_timer_interval(attr
, 0);
1489 case LTTNG_DOMAIN_UST
:
1491 case LTTNG_DOMAIN_JUL
:
1492 case LTTNG_DOMAIN_LOG4J
:
1493 case LTTNG_DOMAIN_PYTHON
:
1494 if (!agent_tracing_is_enabled()) {
1495 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1496 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1501 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1505 switch (domain
->type
) {
1506 case LTTNG_DOMAIN_KERNEL
:
1508 struct ltt_kernel_channel
*kchan
;
1510 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1511 session
->kernel_session
);
1512 if (kchan
== NULL
) {
1513 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1514 if (attr
->name
[0] != '\0') {
1515 session
->kernel_session
->has_non_default_channel
= 1;
1518 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1521 if (ret
!= LTTNG_OK
) {
1525 kernel_wait_quiescent(kernel_tracer_fd
);
1528 case LTTNG_DOMAIN_UST
:
1529 case LTTNG_DOMAIN_JUL
:
1530 case LTTNG_DOMAIN_LOG4J
:
1531 case LTTNG_DOMAIN_PYTHON
:
1533 struct ltt_ust_channel
*uchan
;
1538 * Current agent implementation limitations force us to allow
1539 * only one channel at once in "agent" subdomains. Each
1540 * subdomain has a default channel name which must be strictly
1543 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1544 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1545 LTTNG_SYMBOL_NAME_LEN
)) {
1546 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1549 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1550 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1551 LTTNG_SYMBOL_NAME_LEN
)) {
1552 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1555 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1556 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1557 LTTNG_SYMBOL_NAME_LEN
)) {
1558 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1563 chan_ht
= usess
->domain_global
.channels
;
1565 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1566 if (uchan
== NULL
) {
1567 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1568 if (attr
->name
[0] != '\0') {
1569 usess
->has_non_default_channel
= 1;
1572 ret
= channel_ust_enable(usess
, uchan
);
1577 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1588 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1590 int cmd_disable_event(struct ltt_session
*session
,
1591 enum lttng_domain_type domain
, char *channel_name
,
1592 struct lttng_event
*event
)
1597 DBG("Disable event command for event \'%s\'", event
->name
);
1599 event_name
= event
->name
;
1601 /* Error out on unhandled search criteria */
1602 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1603 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1604 ret
= LTTNG_ERR_UNK
;
1611 case LTTNG_DOMAIN_KERNEL
:
1613 struct ltt_kernel_channel
*kchan
;
1614 struct ltt_kernel_session
*ksess
;
1616 ksess
= session
->kernel_session
;
1619 * If a non-default channel has been created in the
1620 * session, explicitely require that -c chan_name needs
1623 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1624 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1628 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1629 if (kchan
== NULL
) {
1630 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1634 switch (event
->type
) {
1635 case LTTNG_EVENT_ALL
:
1636 case LTTNG_EVENT_TRACEPOINT
:
1637 case LTTNG_EVENT_SYSCALL
:
1638 case LTTNG_EVENT_PROBE
:
1639 case LTTNG_EVENT_FUNCTION
:
1640 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1641 if (event_name
[0] == '\0') {
1642 ret
= event_kernel_disable_event(kchan
,
1645 ret
= event_kernel_disable_event(kchan
,
1646 event_name
, event
->type
);
1648 if (ret
!= LTTNG_OK
) {
1653 ret
= LTTNG_ERR_UNK
;
1657 kernel_wait_quiescent(kernel_tracer_fd
);
1660 case LTTNG_DOMAIN_UST
:
1662 struct ltt_ust_channel
*uchan
;
1663 struct ltt_ust_session
*usess
;
1665 usess
= session
->ust_session
;
1667 if (validate_ust_event_name(event_name
)) {
1668 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1673 * If a non-default channel has been created in the
1674 * session, explicitly require that -c chan_name needs
1677 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1678 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1682 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1684 if (uchan
== NULL
) {
1685 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1689 switch (event
->type
) {
1690 case LTTNG_EVENT_ALL
:
1692 * An empty event name means that everything
1693 * should be disabled.
1695 if (event
->name
[0] == '\0') {
1696 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1698 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1701 if (ret
!= LTTNG_OK
) {
1706 ret
= LTTNG_ERR_UNK
;
1710 DBG3("Disable UST event %s in channel %s completed", event_name
,
1714 case LTTNG_DOMAIN_LOG4J
:
1715 case LTTNG_DOMAIN_JUL
:
1716 case LTTNG_DOMAIN_PYTHON
:
1719 struct ltt_ust_session
*usess
= session
->ust_session
;
1723 switch (event
->type
) {
1724 case LTTNG_EVENT_ALL
:
1727 ret
= LTTNG_ERR_UNK
;
1731 agt
= trace_ust_find_agent(usess
, domain
);
1733 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1737 * An empty event name means that everything
1738 * should be disabled.
1740 if (event
->name
[0] == '\0') {
1741 ret
= event_agent_disable_all(usess
, agt
);
1743 ret
= event_agent_disable(usess
, agt
, event_name
);
1745 if (ret
!= LTTNG_OK
) {
1752 ret
= LTTNG_ERR_UND
;
1765 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1767 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1768 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1770 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1771 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1774 * Don't try to add a context if the session has been started at
1775 * some point in time before. The tracer does not allow it and would
1776 * result in a corrupted trace.
1778 if (session
->has_been_started
) {
1779 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1783 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1784 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1785 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1789 case LTTNG_DOMAIN_KERNEL
:
1790 assert(session
->kernel_session
);
1792 if (session
->kernel_session
->channel_count
== 0) {
1793 /* Create default channel */
1794 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1795 if (ret
!= LTTNG_OK
) {
1798 chan_kern_created
= 1;
1800 /* Add kernel context to kernel tracer */
1801 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1802 if (ret
!= LTTNG_OK
) {
1806 case LTTNG_DOMAIN_JUL
:
1807 case LTTNG_DOMAIN_LOG4J
:
1810 * Validate channel name.
1811 * If no channel name is given and the domain is JUL or LOG4J,
1812 * set it to the appropriate domain-specific channel name. If
1813 * a name is provided but does not match the expexted channel
1814 * name, return an error.
1816 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1817 strcmp(channel_name
,
1818 DEFAULT_JUL_CHANNEL_NAME
)) {
1819 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1821 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1822 strcmp(channel_name
,
1823 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1824 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1827 /* break is _not_ missing here. */
1829 case LTTNG_DOMAIN_UST
:
1831 struct ltt_ust_session
*usess
= session
->ust_session
;
1832 unsigned int chan_count
;
1836 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1837 if (chan_count
== 0) {
1838 struct lttng_channel
*attr
;
1839 /* Create default channel */
1840 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1842 ret
= LTTNG_ERR_FATAL
;
1846 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1847 if (ret
!= LTTNG_OK
) {
1851 channel_attr_destroy(attr
);
1852 chan_ust_created
= 1;
1855 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1856 free(app_ctx_provider_name
);
1858 app_ctx_name
= NULL
;
1859 app_ctx_provider_name
= NULL
;
1860 if (ret
!= LTTNG_OK
) {
1866 ret
= LTTNG_ERR_UND
;
1874 if (chan_kern_created
) {
1875 struct ltt_kernel_channel
*kchan
=
1876 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1877 session
->kernel_session
);
1878 /* Created previously, this should NOT fail. */
1880 kernel_destroy_channel(kchan
);
1883 if (chan_ust_created
) {
1884 struct ltt_ust_channel
*uchan
=
1885 trace_ust_find_channel_by_name(
1886 session
->ust_session
->domain_global
.channels
,
1887 DEFAULT_CHANNEL_NAME
);
1888 /* Created previously, this should NOT fail. */
1890 /* Remove from the channel list of the session. */
1891 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1893 trace_ust_destroy_channel(uchan
);
1896 free(app_ctx_provider_name
);
1901 static inline bool name_starts_with(const char *name
, const char *prefix
)
1903 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1905 return !strncmp(name
, prefix
, max_cmp_len
);
1908 /* Perform userspace-specific event name validation */
1909 static int validate_ust_event_name(const char *name
)
1919 * Check name against all internal UST event component namespaces used
1922 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1923 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1924 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1933 * Internal version of cmd_enable_event() with a supplemental
1934 * "internal_event" flag which is used to enable internal events which should
1935 * be hidden from clients. Such events are used in the agent implementation to
1936 * enable the events through which all "agent" events are funeled.
1938 static int _cmd_enable_event(struct ltt_session
*session
,
1939 struct lttng_domain
*domain
,
1940 char *channel_name
, struct lttng_event
*event
,
1941 char *filter_expression
,
1942 struct lttng_filter_bytecode
*filter
,
1943 struct lttng_event_exclusion
*exclusion
,
1944 int wpipe
, bool internal_event
)
1946 int ret
= 0, channel_created
= 0;
1947 struct lttng_channel
*attr
= NULL
;
1951 assert(channel_name
);
1953 /* If we have a filter, we must have its filter expression */
1954 assert(!(!!filter_expression
^ !!filter
));
1956 /* Normalize event name as a globbing pattern */
1957 strutils_normalize_star_glob_pattern(event
->name
);
1959 /* Normalize exclusion names as globbing patterns */
1963 for (i
= 0; i
< exclusion
->count
; i
++) {
1964 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
1966 strutils_normalize_star_glob_pattern(name
);
1970 DBG("Enable event command for event \'%s\'", event
->name
);
1974 switch (domain
->type
) {
1975 case LTTNG_DOMAIN_KERNEL
:
1977 struct ltt_kernel_channel
*kchan
;
1980 * If a non-default channel has been created in the
1981 * session, explicitely require that -c chan_name needs
1984 if (session
->kernel_session
->has_non_default_channel
1985 && channel_name
[0] == '\0') {
1986 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1990 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1991 session
->kernel_session
);
1992 if (kchan
== NULL
) {
1993 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1994 LTTNG_BUFFER_GLOBAL
);
1996 ret
= LTTNG_ERR_FATAL
;
1999 if (lttng_strncpy(attr
->name
, channel_name
,
2000 sizeof(attr
->name
))) {
2001 ret
= LTTNG_ERR_INVALID
;
2005 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2006 if (ret
!= LTTNG_OK
) {
2009 channel_created
= 1;
2012 /* Get the newly created kernel channel pointer */
2013 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2014 session
->kernel_session
);
2015 if (kchan
== NULL
) {
2016 /* This sould not happen... */
2017 ret
= LTTNG_ERR_FATAL
;
2021 switch (event
->type
) {
2022 case LTTNG_EVENT_ALL
:
2024 char *filter_expression_a
= NULL
;
2025 struct lttng_filter_bytecode
*filter_a
= NULL
;
2028 * We need to duplicate filter_expression and filter,
2029 * because ownership is passed to first enable
2032 if (filter_expression
) {
2033 filter_expression_a
= strdup(filter_expression
);
2034 if (!filter_expression_a
) {
2035 ret
= LTTNG_ERR_FATAL
;
2040 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2042 free(filter_expression_a
);
2043 ret
= LTTNG_ERR_FATAL
;
2046 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2048 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2049 ret
= event_kernel_enable_event(kchan
, event
,
2050 filter_expression
, filter
);
2051 /* We have passed ownership */
2052 filter_expression
= NULL
;
2054 if (ret
!= LTTNG_OK
) {
2055 if (channel_created
) {
2056 /* Let's not leak a useless channel. */
2057 kernel_destroy_channel(kchan
);
2059 free(filter_expression_a
);
2063 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2064 ret
= event_kernel_enable_event(kchan
, event
,
2065 filter_expression_a
, filter_a
);
2066 /* We have passed ownership */
2067 filter_expression_a
= NULL
;
2069 if (ret
!= LTTNG_OK
) {
2074 case LTTNG_EVENT_PROBE
:
2075 case LTTNG_EVENT_FUNCTION
:
2076 case LTTNG_EVENT_FUNCTION_ENTRY
:
2077 case LTTNG_EVENT_TRACEPOINT
:
2078 ret
= event_kernel_enable_event(kchan
, event
,
2079 filter_expression
, filter
);
2080 /* We have passed ownership */
2081 filter_expression
= NULL
;
2083 if (ret
!= LTTNG_OK
) {
2084 if (channel_created
) {
2085 /* Let's not leak a useless channel. */
2086 kernel_destroy_channel(kchan
);
2091 case LTTNG_EVENT_SYSCALL
:
2092 ret
= event_kernel_enable_event(kchan
, event
,
2093 filter_expression
, filter
);
2094 /* We have passed ownership */
2095 filter_expression
= NULL
;
2097 if (ret
!= LTTNG_OK
) {
2102 ret
= LTTNG_ERR_UNK
;
2106 kernel_wait_quiescent(kernel_tracer_fd
);
2109 case LTTNG_DOMAIN_UST
:
2111 struct ltt_ust_channel
*uchan
;
2112 struct ltt_ust_session
*usess
= session
->ust_session
;
2117 * If a non-default channel has been created in the
2118 * session, explicitely require that -c chan_name needs
2121 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2122 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2126 /* Get channel from global UST domain */
2127 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2129 if (uchan
== NULL
) {
2130 /* Create default channel */
2131 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2132 usess
->buffer_type
);
2134 ret
= LTTNG_ERR_FATAL
;
2137 if (lttng_strncpy(attr
->name
, channel_name
,
2138 sizeof(attr
->name
))) {
2139 ret
= LTTNG_ERR_INVALID
;
2143 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2144 if (ret
!= LTTNG_OK
) {
2148 /* Get the newly created channel reference back */
2149 uchan
= trace_ust_find_channel_by_name(
2150 usess
->domain_global
.channels
, channel_name
);
2154 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2156 * Don't allow users to add UST events to channels which
2157 * are assigned to a userspace subdomain (JUL, Log4J,
2160 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2164 if (!internal_event
) {
2166 * Ensure the event name is not reserved for internal
2169 ret
= validate_ust_event_name(event
->name
);
2171 WARN("Userspace event name %s failed validation.",
2173 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2178 /* At this point, the session and channel exist on the tracer */
2179 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2180 filter_expression
, filter
, exclusion
,
2182 /* We have passed ownership */
2183 filter_expression
= NULL
;
2186 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2187 goto already_enabled
;
2188 } else if (ret
!= LTTNG_OK
) {
2193 case LTTNG_DOMAIN_LOG4J
:
2194 case LTTNG_DOMAIN_JUL
:
2195 case LTTNG_DOMAIN_PYTHON
:
2197 const char *default_event_name
, *default_chan_name
;
2199 struct lttng_event uevent
;
2200 struct lttng_domain tmp_dom
;
2201 struct ltt_ust_session
*usess
= session
->ust_session
;
2205 if (!agent_tracing_is_enabled()) {
2206 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2207 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2211 agt
= trace_ust_find_agent(usess
, domain
->type
);
2213 agt
= agent_create(domain
->type
);
2215 ret
= LTTNG_ERR_NOMEM
;
2218 agent_add(agt
, usess
->agents
);
2221 /* Create the default tracepoint. */
2222 memset(&uevent
, 0, sizeof(uevent
));
2223 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2224 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2225 default_event_name
= event_get_default_agent_ust_name(
2227 if (!default_event_name
) {
2228 ret
= LTTNG_ERR_FATAL
;
2231 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2232 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2235 * The domain type is changed because we are about to enable the
2236 * default channel and event for the JUL domain that are hardcoded.
2237 * This happens in the UST domain.
2239 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2240 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2242 switch (domain
->type
) {
2243 case LTTNG_DOMAIN_LOG4J
:
2244 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2246 case LTTNG_DOMAIN_JUL
:
2247 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2249 case LTTNG_DOMAIN_PYTHON
:
2250 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2253 /* The switch/case we are in makes this impossible */
2258 char *filter_expression_copy
= NULL
;
2259 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2262 const size_t filter_size
= sizeof(
2263 struct lttng_filter_bytecode
)
2266 filter_copy
= zmalloc(filter_size
);
2268 ret
= LTTNG_ERR_NOMEM
;
2271 memcpy(filter_copy
, filter
, filter_size
);
2273 filter_expression_copy
=
2274 strdup(filter_expression
);
2275 if (!filter_expression
) {
2276 ret
= LTTNG_ERR_NOMEM
;
2279 if (!filter_expression_copy
|| !filter_copy
) {
2280 free(filter_expression_copy
);
2286 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2287 (char *) default_chan_name
,
2288 &uevent
, filter_expression_copy
,
2289 filter_copy
, NULL
, wpipe
);
2292 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2293 goto already_enabled
;
2294 } else if (ret
!= LTTNG_OK
) {
2298 /* The wild card * means that everything should be enabled. */
2299 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2300 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2303 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2307 filter_expression
= NULL
;
2308 if (ret
!= LTTNG_OK
) {
2315 ret
= LTTNG_ERR_UND
;
2323 free(filter_expression
);
2326 channel_attr_destroy(attr
);
2332 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2333 * We own filter, exclusion, and filter_expression.
2335 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2336 char *channel_name
, struct lttng_event
*event
,
2337 char *filter_expression
,
2338 struct lttng_filter_bytecode
*filter
,
2339 struct lttng_event_exclusion
*exclusion
,
2342 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2343 filter_expression
, filter
, exclusion
, wpipe
, false);
2347 * Enable an event which is internal to LTTng. An internal should
2348 * never be made visible to clients and are immune to checks such as
2351 static int cmd_enable_event_internal(struct ltt_session
*session
,
2352 struct lttng_domain
*domain
,
2353 char *channel_name
, struct lttng_event
*event
,
2354 char *filter_expression
,
2355 struct lttng_filter_bytecode
*filter
,
2356 struct lttng_event_exclusion
*exclusion
,
2359 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2360 filter_expression
, filter
, exclusion
, wpipe
, true);
2364 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2366 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2367 struct lttng_event
**events
)
2370 ssize_t nb_events
= 0;
2373 case LTTNG_DOMAIN_KERNEL
:
2374 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2375 if (nb_events
< 0) {
2376 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2380 case LTTNG_DOMAIN_UST
:
2381 nb_events
= ust_app_list_events(events
);
2382 if (nb_events
< 0) {
2383 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2387 case LTTNG_DOMAIN_LOG4J
:
2388 case LTTNG_DOMAIN_JUL
:
2389 case LTTNG_DOMAIN_PYTHON
:
2390 nb_events
= agent_list_events(events
, domain
);
2391 if (nb_events
< 0) {
2392 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2397 ret
= LTTNG_ERR_UND
;
2404 /* Return negative value to differentiate return code */
2409 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2411 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2412 struct lttng_event_field
**fields
)
2415 ssize_t nb_fields
= 0;
2418 case LTTNG_DOMAIN_UST
:
2419 nb_fields
= ust_app_list_event_fields(fields
);
2420 if (nb_fields
< 0) {
2421 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2425 case LTTNG_DOMAIN_KERNEL
:
2426 default: /* fall-through */
2427 ret
= LTTNG_ERR_UND
;
2434 /* Return negative value to differentiate return code */
2438 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2440 return syscall_table_list(events
);
2444 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2446 * Called with session lock held.
2448 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2449 enum lttng_domain_type domain
, int32_t **pids
)
2452 ssize_t nr_pids
= 0;
2455 case LTTNG_DOMAIN_KERNEL
:
2457 struct ltt_kernel_session
*ksess
;
2459 ksess
= session
->kernel_session
;
2460 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2462 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2467 case LTTNG_DOMAIN_UST
:
2469 struct ltt_ust_session
*usess
;
2471 usess
= session
->ust_session
;
2472 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2474 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2479 case LTTNG_DOMAIN_LOG4J
:
2480 case LTTNG_DOMAIN_JUL
:
2481 case LTTNG_DOMAIN_PYTHON
:
2483 ret
= LTTNG_ERR_UND
;
2490 /* Return negative value to differentiate return code */
2495 int domain_mkdir(const struct consumer_output
*output
,
2496 const struct ltt_session
*session
,
2497 uid_t uid
, gid_t gid
)
2499 struct consumer_socket
*socket
;
2500 struct lttng_ht_iter iter
;
2504 if (!output
|| !output
->socks
) {
2505 ERR("No consumer output found");
2510 path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
2512 ERR("Cannot allocate mkdir path");
2517 ret
= snprintf(path
, LTTNG_PATH_MAX
, "%s%s%s",
2518 session_get_base_path(session
),
2519 output
->chunk_path
, output
->subdir
);
2520 if (ret
< 0 || ret
>= LTTNG_PATH_MAX
) {
2526 DBG("Domain mkdir %s for session %" PRIu64
, path
, session
->id
);
2529 * We have to iterate to find a socket, but we only need to send the
2530 * rename command to one consumer, so we break after the first one.
2532 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
2533 pthread_mutex_lock(socket
->lock
);
2534 ret
= consumer_mkdir(socket
, session
->id
, output
, path
, uid
, gid
);
2535 pthread_mutex_unlock(socket
->lock
);
2537 ERR("Consumer mkdir");
2554 int session_mkdir(const struct ltt_session
*session
)
2557 struct consumer_output
*output
;
2562 * Unsupported feature in lttng-relayd before 2.11, not an error since it
2563 * is only needed for session rotation and the user will get an error
2566 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
2567 session
->consumer
->relay_major_version
== 2 &&
2568 session
->consumer
->relay_minor_version
< 11) {
2573 if (session
->kernel_session
) {
2574 output
= session
->kernel_session
->consumer
;
2575 uid
= session
->kernel_session
->uid
;
2576 gid
= session
->kernel_session
->gid
;
2577 ret
= domain_mkdir(output
, session
, uid
, gid
);
2579 ERR("Mkdir kernel");
2584 if (session
->ust_session
) {
2585 output
= session
->ust_session
->consumer
;
2586 uid
= session
->ust_session
->uid
;
2587 gid
= session
->ust_session
->gid
;
2588 ret
= domain_mkdir(output
, session
, uid
, gid
);
2602 * Command LTTNG_START_TRACE processed by the client thread.
2604 * Called with session mutex held.
2606 int cmd_start_trace(struct ltt_session
*session
)
2609 unsigned long nb_chan
= 0;
2610 struct ltt_kernel_session
*ksession
;
2611 struct ltt_ust_session
*usess
;
2615 /* Ease our life a bit ;) */
2616 ksession
= session
->kernel_session
;
2617 usess
= session
->ust_session
;
2619 /* Is the session already started? */
2620 if (session
->active
) {
2621 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2626 * Starting a session without channel is useless since after that it's not
2627 * possible to enable channel thus inform the client.
2629 if (usess
&& usess
->domain_global
.channels
) {
2630 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2633 nb_chan
+= ksession
->channel_count
;
2636 ret
= LTTNG_ERR_NO_CHANNEL
;
2641 * Record the timestamp of the first time the session is started for
2642 * an eventual session rotation call.
2644 if (!session
->has_been_started
) {
2645 session
->current_chunk_start_ts
= time(NULL
);
2646 if (session
->current_chunk_start_ts
== (time_t) -1) {
2647 PERROR("Failed to retrieve the \"%s\" session's start time",
2649 ret
= LTTNG_ERR_FATAL
;
2652 if (!session
->snapshot_mode
&& session
->output_traces
) {
2653 ret
= session_mkdir(session
);
2655 ERR("Failed to create the session directories");
2656 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2662 /* Kernel tracing */
2663 if (ksession
!= NULL
) {
2664 DBG("Start kernel tracing session %s", session
->name
);
2665 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2666 if (ret
!= LTTNG_OK
) {
2671 /* Flag session that trace should start automatically */
2674 * Even though the start trace might fail, flag this session active so
2675 * other application coming in are started by default.
2679 ret
= ust_app_start_trace_all(usess
);
2681 ret
= LTTNG_ERR_UST_START_FAIL
;
2686 /* Flag this after a successful start. */
2687 session
->has_been_started
= 1;
2688 session
->active
= 1;
2691 * Clear the flag that indicates that a rotation was done while the
2692 * session was stopped.
2694 session
->rotated_after_last_stop
= false;
2696 if (session
->rotate_timer_period
) {
2697 ret
= sessiond_rotate_timer_start(session
,
2698 session
->rotate_timer_period
);
2700 ERR("Failed to enable rotate timer");
2701 ret
= LTTNG_ERR_UNK
;
2713 int rename_active_chunk(struct ltt_session
*session
)
2717 session
->current_archive_id
++;
2720 * The currently active tracing path is now the folder we
2723 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
2724 session
->rotation_chunk
.active_tracing_path
,
2725 sizeof(session
->rotation_chunk
.current_rotate_path
));
2727 ERR("Failed to copy active tracing path");
2731 ret
= rename_complete_chunk(session
, time(NULL
));
2733 ERR("Failed to rename current rotate path");
2738 * We just renamed, the folder, we didn't do an actual rotation, so
2739 * the active tracing path is now the renamed folder and we have to
2740 * restore the rotate count.
2742 ret
= lttng_strncpy(session
->rotation_chunk
.active_tracing_path
,
2743 session
->rotation_chunk
.current_rotate_path
,
2744 sizeof(session
->rotation_chunk
.active_tracing_path
));
2746 ERR("Failed to rename active session chunk tracing path");
2750 session
->current_archive_id
--;
2755 * Command LTTNG_STOP_TRACE processed by the client thread.
2757 int cmd_stop_trace(struct ltt_session
*session
)
2760 struct ltt_kernel_channel
*kchan
;
2761 struct ltt_kernel_session
*ksession
;
2762 struct ltt_ust_session
*usess
;
2763 bool error_occured
= false;
2767 DBG("Begin stop session %s (id %" PRIu64
")", session
->name
, session
->id
);
2769 ksession
= session
->kernel_session
;
2770 usess
= session
->ust_session
;
2772 /* Session is not active. Skip everythong and inform the client. */
2773 if (!session
->active
) {
2774 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2778 if (session
->rotate_relay_pending_timer_enabled
) {
2779 sessiond_timer_rotate_pending_stop(session
);
2782 if (session
->rotate_timer_enabled
) {
2783 sessiond_rotate_timer_stop(session
);
2786 if (session
->current_archive_id
> 0 && !session
->rotate_pending
) {
2787 ret
= rename_active_chunk(session
);
2790 * This error should not prevent the user from stopping
2791 * the session. However, it will be reported at the end.
2793 error_occured
= true;
2798 if (ksession
&& ksession
->active
) {
2799 DBG("Stop kernel tracing");
2801 ret
= kernel_stop_session(ksession
);
2803 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2807 kernel_wait_quiescent(kernel_tracer_fd
);
2809 /* Flush metadata after stopping (if exists) */
2810 if (ksession
->metadata_stream_fd
>= 0) {
2811 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2813 ERR("Kernel metadata flush failed");
2817 /* Flush all buffers after stopping */
2818 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2819 ret
= kernel_flush_buffer(kchan
);
2821 ERR("Kernel flush buffer error");
2825 ksession
->active
= 0;
2826 DBG("Kernel session stopped %s (id %" PRIu64
")", session
->name
,
2830 if (usess
&& usess
->active
) {
2832 * Even though the stop trace might fail, flag this session inactive so
2833 * other application coming in are not started by default.
2837 ret
= ust_app_stop_trace_all(usess
);
2839 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2844 /* Flag inactive after a successful stop. */
2845 session
->active
= 0;
2846 ret
= !error_occured
? LTTNG_OK
: LTTNG_ERR_UNK
;
2853 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2855 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2856 struct lttng_uri
*uris
)
2859 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2860 struct ltt_ust_session
*usess
= session
->ust_session
;
2866 /* Can't set consumer URI if the session is active. */
2867 if (session
->active
) {
2868 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2872 /* Set the "global" consumer URIs */
2873 for (i
= 0; i
< nb_uri
; i
++) {
2874 ret
= add_uri_to_consumer(session
->consumer
,
2875 &uris
[i
], 0, session
->name
);
2876 if (ret
!= LTTNG_OK
) {
2881 /* Set UST session URIs */
2882 if (session
->ust_session
) {
2883 for (i
= 0; i
< nb_uri
; i
++) {
2884 ret
= add_uri_to_consumer(
2885 session
->ust_session
->consumer
,
2886 &uris
[i
], LTTNG_DOMAIN_UST
,
2888 if (ret
!= LTTNG_OK
) {
2894 /* Set kernel session URIs */
2895 if (session
->kernel_session
) {
2896 for (i
= 0; i
< nb_uri
; i
++) {
2897 ret
= add_uri_to_consumer(
2898 session
->kernel_session
->consumer
,
2899 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2901 if (ret
!= LTTNG_OK
) {
2908 * Make sure to set the session in output mode after we set URI since a
2909 * session can be created without URL (thus flagged in no output mode).
2911 session
->output_traces
= 1;
2913 ksess
->output_traces
= 1;
2917 usess
->output_traces
= 1;
2928 * Command LTTNG_CREATE_SESSION processed by the client thread.
2930 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2931 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2934 struct ltt_session
*session
;
2940 * Verify if the session already exist
2942 * XXX: There is no need for the session lock list here since the caller
2943 * (process_client_msg) is holding it. We might want to change that so a
2944 * single command does not lock the entire session list.
2946 session
= session_find_by_name(name
);
2947 if (session
!= NULL
) {
2948 ret
= LTTNG_ERR_EXIST_SESS
;
2952 /* Create tracing session in the registry */
2953 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2954 LTTNG_SOCK_GET_GID_CRED(creds
));
2955 if (ret
!= LTTNG_OK
) {
2960 * Get the newly created session pointer back
2962 * XXX: There is no need for the session lock list here since the caller
2963 * (process_client_msg) is holding it. We might want to change that so a
2964 * single command does not lock the entire session list.
2966 session
= session_find_by_name(name
);
2969 session
->live_timer
= live_timer
;
2970 /* Create default consumer output for the session not yet created. */
2971 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2972 if (session
->consumer
== NULL
) {
2973 ret
= LTTNG_ERR_FATAL
;
2974 goto consumer_error
;
2978 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2979 if (ret
!= LTTNG_OK
) {
2980 goto consumer_error
;
2982 session
->output_traces
= 1;
2984 session
->output_traces
= 0;
2985 DBG2("Session %s created with no output", session
->name
);
2988 session
->consumer
->enabled
= 1;
2993 session_destroy(session
);
3000 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
3002 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
3003 size_t nb_uri
, lttng_sock_cred
*creds
)
3006 struct ltt_session
*session
;
3007 struct snapshot_output
*new_output
= NULL
;
3013 * Create session in no output mode with URIs set to NULL. The uris we've
3014 * received are for a default snapshot output if one.
3016 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
3017 if (ret
!= LTTNG_OK
) {
3021 /* Get the newly created session pointer back. This should NEVER fail. */
3022 session
= session_find_by_name(name
);
3025 /* Flag session for snapshot mode. */
3026 session
->snapshot_mode
= 1;
3028 /* Skip snapshot output creation if no URI is given. */
3033 new_output
= snapshot_output_alloc();
3035 ret
= LTTNG_ERR_NOMEM
;
3036 goto error_snapshot_alloc
;
3039 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
3040 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
3042 if (ret
== -ENOMEM
) {
3043 ret
= LTTNG_ERR_NOMEM
;
3045 ret
= LTTNG_ERR_INVALID
;
3047 goto error_snapshot
;
3051 snapshot_add_output(&session
->snapshot
, new_output
);
3058 snapshot_output_destroy(new_output
);
3059 error_snapshot_alloc
:
3060 session_destroy(session
);
3066 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3068 * Called with session lock held.
3070 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
,
3071 struct notification_thread_handle
*notification_thread_handle
)
3074 struct ltt_ust_session
*usess
;
3075 struct ltt_kernel_session
*ksess
;
3080 usess
= session
->ust_session
;
3081 ksess
= session
->kernel_session
;
3083 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3085 if (session
->rotate_relay_pending_timer_enabled
) {
3086 sessiond_timer_rotate_pending_stop(session
);
3089 if (session
->rotate_timer_enabled
) {
3090 sessiond_rotate_timer_stop(session
);
3093 if (session
->rotate_size
) {
3094 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3095 session
->rotate_size
= 0;
3099 * The rename of the current chunk is performed at stop, but if we rotated
3100 * the session after the previous stop command, we need to rename the
3101 * new (and empty) chunk that was started in between.
3103 if (session
->rotated_after_last_stop
) {
3104 rename_active_chunk(session
);
3107 /* Clean kernel session teardown */
3108 kernel_destroy_session(ksess
);
3110 /* UST session teardown */
3112 /* Close any relayd session */
3113 consumer_output_send_destroy_relayd(usess
->consumer
);
3115 /* Destroy every UST application related to this session. */
3116 ret
= ust_app_destroy_trace_all(usess
);
3118 ERR("Error in ust_app_destroy_trace_all");
3121 /* Clean up the rest. */
3122 trace_ust_destroy_session(usess
);
3126 * Must notify the kernel thread here to update it's poll set in order to
3127 * remove the channel(s)' fd just destroyed.
3129 ret
= notify_thread_pipe(wpipe
);
3131 PERROR("write kernel poll pipe");
3134 if (session
->shm_path
[0]) {
3136 * When a session is created with an explicit shm_path,
3137 * the consumer daemon will create its shared memory files
3138 * at that location and will *not* unlink them. This is normal
3139 * as the intention of that feature is to make it possible
3140 * to retrieve the content of those files should a crash occur.
3142 * To ensure the content of those files can be used, the
3143 * sessiond daemon will replicate the content of the metadata
3144 * cache in a metadata file.
3146 * On clean-up, it is expected that the consumer daemon will
3147 * unlink the shared memory files and that the session daemon
3148 * will unlink the metadata file. Then, the session's directory
3149 * in the shm path can be removed.
3151 * Unfortunately, a flaw in the design of the sessiond's and
3152 * consumerd's tear down of channels makes it impossible to
3153 * determine when the sessiond _and_ the consumerd have both
3154 * destroyed their representation of a channel. For one, the
3155 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3156 * callbacks in both daemons.
3158 * However, it is also impossible for the sessiond to know when
3159 * the consumer daemon is done destroying its channel(s) since
3160 * it occurs as a reaction to the closing of the channel's file
3161 * descriptor. There is no resulting communication initiated
3162 * from the consumerd to the sessiond to confirm that the
3163 * operation is completed (and was successful).
3165 * Until this is all fixed, the session daemon checks for the
3166 * removal of the session's shm path which makes it possible
3167 * to safely advertise a session as having been destroyed.
3169 * Prior to this fix, it was not possible to reliably save
3170 * a session making use of the --shm-path option, destroy it,
3171 * and load it again. This is because the creation of the
3172 * session would fail upon seeing the session's shm path
3173 * already in existence.
3175 * Note that none of the error paths in the check for the
3176 * directory's existence return an error. This is normal
3177 * as there isn't much that can be done. The session will
3178 * be destroyed properly, except that we can't offer the
3179 * guarantee that the same session can be re-created.
3181 current_completion_handler
= &destroy_completion_handler
.handler
;
3182 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3184 sizeof(destroy_completion_handler
.shm_path
));
3187 ret
= session_destroy(session
);
3193 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3195 int cmd_register_consumer(struct ltt_session
*session
,
3196 enum lttng_domain_type domain
, const char *sock_path
,
3197 struct consumer_data
*cdata
)
3200 struct consumer_socket
*socket
= NULL
;
3207 case LTTNG_DOMAIN_KERNEL
:
3209 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3213 /* Can't register a consumer if there is already one */
3214 if (ksess
->consumer_fds_sent
!= 0) {
3215 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3219 sock
= lttcomm_connect_unix_sock(sock_path
);
3221 ret
= LTTNG_ERR_CONNECT_FAIL
;
3224 cdata
->cmd_sock
= sock
;
3226 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3227 if (socket
== NULL
) {
3230 PERROR("close register consumer");
3232 cdata
->cmd_sock
= -1;
3233 ret
= LTTNG_ERR_FATAL
;
3237 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3238 if (socket
->lock
== NULL
) {
3239 PERROR("zmalloc pthread mutex");
3240 ret
= LTTNG_ERR_FATAL
;
3243 pthread_mutex_init(socket
->lock
, NULL
);
3244 socket
->registered
= 1;
3247 consumer_add_socket(socket
, ksess
->consumer
);
3250 pthread_mutex_lock(&cdata
->pid_mutex
);
3252 pthread_mutex_unlock(&cdata
->pid_mutex
);
3257 /* TODO: Userspace tracing */
3258 ret
= LTTNG_ERR_UND
;
3266 consumer_destroy_socket(socket
);
3272 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3274 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3275 struct lttng_domain
**domains
)
3280 struct lttng_ht_iter iter
;
3282 if (session
->kernel_session
!= NULL
) {
3283 DBG3("Listing domains found kernel domain");
3287 if (session
->ust_session
!= NULL
) {
3288 DBG3("Listing domains found UST global domain");
3292 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3294 if (agt
->being_used
) {
3305 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3306 if (*domains
== NULL
) {
3307 ret
= LTTNG_ERR_FATAL
;
3311 if (session
->kernel_session
!= NULL
) {
3312 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3314 /* Kernel session buffer type is always GLOBAL */
3315 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3320 if (session
->ust_session
!= NULL
) {
3321 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3322 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3326 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3328 if (agt
->being_used
) {
3329 (*domains
)[index
].type
= agt
->domain
;
3330 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3340 /* Return negative value to differentiate return code */
3346 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3348 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3349 struct ltt_session
*session
, struct lttng_channel
**channels
)
3351 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3354 case LTTNG_DOMAIN_KERNEL
:
3355 if (session
->kernel_session
!= NULL
) {
3356 nb_chan
= session
->kernel_session
->channel_count
;
3358 DBG3("Number of kernel channels %zd", nb_chan
);
3360 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3364 case LTTNG_DOMAIN_UST
:
3365 if (session
->ust_session
!= NULL
) {
3367 nb_chan
= lttng_ht_get_count(
3368 session
->ust_session
->domain_global
.channels
);
3371 DBG3("Number of UST global channels %zd", nb_chan
);
3373 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3378 ret
= -LTTNG_ERR_UND
;
3383 const size_t channel_size
= sizeof(struct lttng_channel
) +
3384 sizeof(struct lttng_channel_extended
);
3385 struct lttng_channel_extended
*channel_exts
;
3387 payload_size
= nb_chan
* channel_size
;
3388 *channels
= zmalloc(payload_size
);
3389 if (*channels
== NULL
) {
3390 ret
= -LTTNG_ERR_FATAL
;
3394 channel_exts
= ((void *) *channels
) +
3395 (nb_chan
* sizeof(struct lttng_channel
));
3396 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3397 if (ret
!= LTTNG_OK
) {
3412 * Command LTTNG_LIST_EVENTS processed by the client thread.
3414 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3415 struct ltt_session
*session
, char *channel_name
,
3416 struct lttng_event
**events
, size_t *total_size
)
3419 ssize_t nb_event
= 0;
3422 case LTTNG_DOMAIN_KERNEL
:
3423 if (session
->kernel_session
!= NULL
) {
3424 nb_event
= list_lttng_kernel_events(channel_name
,
3425 session
->kernel_session
, events
,
3429 case LTTNG_DOMAIN_UST
:
3431 if (session
->ust_session
!= NULL
) {
3432 nb_event
= list_lttng_ust_global_events(channel_name
,
3433 &session
->ust_session
->domain_global
, events
,
3438 case LTTNG_DOMAIN_LOG4J
:
3439 case LTTNG_DOMAIN_JUL
:
3440 case LTTNG_DOMAIN_PYTHON
:
3441 if (session
->ust_session
) {
3442 struct lttng_ht_iter iter
;
3446 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3447 &iter
.iter
, agt
, node
.node
) {
3448 if (agt
->domain
== domain
) {
3449 nb_event
= list_lttng_agent_events(
3459 ret
= LTTNG_ERR_UND
;
3466 /* Return negative value to differentiate return code */
3471 * Using the session list, filled a lttng_session array to send back to the
3472 * client for session listing.
3474 * The session list lock MUST be acquired before calling this function. Use
3475 * session_lock_list() and session_unlock_list().
3477 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3482 struct ltt_session
*session
;
3483 struct ltt_session_list
*list
= session_get_list();
3485 DBG("Getting all available session for UID %d GID %d",
3488 * Iterate over session list and append data after the control struct in
3491 cds_list_for_each_entry(session
, &list
->head
, list
) {
3493 * Only list the sessions the user can control.
3495 if (!session_access_ok(session
, uid
, gid
)) {
3499 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3500 struct ltt_ust_session
*usess
= session
->ust_session
;
3502 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3503 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3504 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3505 ret
= build_network_session_path(sessions
[i
].path
,
3506 sizeof(sessions
[i
].path
), session
);
3508 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3509 session
->consumer
->dst
.session_root_path
);
3512 PERROR("snprintf session path");
3516 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3517 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3518 sessions
[i
].enabled
= session
->active
;
3519 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3520 sessions
[i
].live_timer_interval
= session
->live_timer
;
3526 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3527 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3529 int cmd_data_pending(struct ltt_session
*session
)
3532 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3533 struct ltt_ust_session
*usess
= session
->ust_session
;
3537 DBG("Data pending for session %s", session
->name
);
3539 /* Session MUST be stopped to ask for data availability. */
3540 if (session
->active
) {
3541 ret
= LTTNG_ERR_SESSION_STARTED
;
3545 * If stopped, just make sure we've started before else the above call
3546 * will always send that there is data pending.
3548 * The consumer assumes that when the data pending command is received,
3549 * the trace has been started before or else no output data is written
3550 * by the streams which is a condition for data pending. So, this is
3551 * *VERY* important that we don't ask the consumer before a start
3554 if (!session
->has_been_started
) {
3561 * A rotation is still pending, we have to wait.
3563 if (session
->rotate_pending
) {
3564 DBG("Rotate still pending for session %s", session
->name
);
3569 if (ksess
&& ksess
->consumer
) {
3570 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3572 /* Data is still being extracted for the kernel. */
3577 if (usess
&& usess
->consumer
) {
3578 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3580 /* Data is still being extracted for the kernel. */
3585 /* Data is ready to be read by a viewer */
3593 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3595 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3597 int cmd_snapshot_add_output(struct ltt_session
*session
,
3598 struct lttng_snapshot_output
*output
, uint32_t *id
)
3601 struct snapshot_output
*new_output
;
3606 DBG("Cmd snapshot add output for session %s", session
->name
);
3609 * Can't create an output if the session is not set in no-output mode.
3611 if (session
->output_traces
) {
3612 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3616 /* Only one output is allowed until we have the "tee" feature. */
3617 if (session
->snapshot
.nb_output
== 1) {
3618 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3622 new_output
= snapshot_output_alloc();
3624 ret
= LTTNG_ERR_NOMEM
;
3628 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3629 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3630 &session
->snapshot
);
3632 if (ret
== -ENOMEM
) {
3633 ret
= LTTNG_ERR_NOMEM
;
3635 ret
= LTTNG_ERR_INVALID
;
3641 snapshot_add_output(&session
->snapshot
, new_output
);
3643 *id
= new_output
->id
;
3650 snapshot_output_destroy(new_output
);
3656 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3658 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3660 int cmd_snapshot_del_output(struct ltt_session
*session
,
3661 struct lttng_snapshot_output
*output
)
3664 struct snapshot_output
*sout
= NULL
;
3672 * Permission denied to create an output if the session is not
3673 * set in no output mode.
3675 if (session
->output_traces
) {
3676 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3681 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3683 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3684 } else if (*output
->name
!= '\0') {
3685 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3687 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3690 ret
= LTTNG_ERR_INVALID
;
3694 snapshot_delete_output(&session
->snapshot
, sout
);
3695 snapshot_output_destroy(sout
);
3704 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3706 * If no output is available, outputs is untouched and 0 is returned.
3708 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3710 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3711 struct lttng_snapshot_output
**outputs
)
3714 struct lttng_snapshot_output
*list
= NULL
;
3715 struct lttng_ht_iter iter
;
3716 struct snapshot_output
*output
;
3721 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3724 * Permission denied to create an output if the session is not
3725 * set in no output mode.
3727 if (session
->output_traces
) {
3728 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3732 if (session
->snapshot
.nb_output
== 0) {
3737 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3739 ret
= -LTTNG_ERR_NOMEM
;
3743 /* Copy list from session to the new list object. */
3745 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3746 output
, node
.node
) {
3747 assert(output
->consumer
);
3748 list
[idx
].id
= output
->id
;
3749 list
[idx
].max_size
= output
->max_size
;
3750 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3751 sizeof(list
[idx
].name
))) {
3752 ret
= -LTTNG_ERR_INVALID
;
3755 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3756 if (lttng_strncpy(list
[idx
].ctrl_url
,
3757 output
->consumer
->dst
.session_root_path
,
3758 sizeof(list
[idx
].ctrl_url
))) {
3759 ret
= -LTTNG_ERR_INVALID
;
3764 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3765 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3767 ret
= -LTTNG_ERR_NOMEM
;
3772 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3773 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3775 ret
= -LTTNG_ERR_NOMEM
;
3784 ret
= session
->snapshot
.nb_output
;
3793 * Check if we can regenerate the metadata for this session.
3794 * Only kernel, UST per-uid and non-live sessions are supported.
3796 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3799 int check_regenerate_metadata_support(struct ltt_session
*session
)
3805 if (session
->live_timer
!= 0) {
3806 ret
= LTTNG_ERR_LIVE_SESSION
;
3809 if (!session
->active
) {
3810 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3813 if (session
->ust_session
) {
3814 switch (session
->ust_session
->buffer_type
) {
3815 case LTTNG_BUFFER_PER_UID
:
3817 case LTTNG_BUFFER_PER_PID
:
3818 ret
= LTTNG_ERR_PER_PID_SESSION
;
3822 ret
= LTTNG_ERR_UNK
;
3826 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3827 session
->consumer
->relay_minor_version
< 8) {
3828 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3838 int clear_metadata_file(int fd
)
3843 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
3844 if (lseek_ret
< 0) {
3850 ret
= ftruncate(fd
, 0);
3852 PERROR("ftruncate");
3861 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3864 struct buffer_reg_uid
*uid_reg
= NULL
;
3865 struct buffer_reg_session
*session_reg
= NULL
;
3868 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3869 struct ust_registry_session
*registry
;
3870 struct ust_registry_channel
*chan
;
3871 struct lttng_ht_iter iter_chan
;
3873 session_reg
= uid_reg
->registry
;
3874 registry
= session_reg
->reg
.ust
;
3876 pthread_mutex_lock(®istry
->lock
);
3877 registry
->metadata_len_sent
= 0;
3878 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3879 registry
->metadata_len
= 0;
3880 registry
->metadata_version
++;
3881 if (registry
->metadata_fd
> 0) {
3882 /* Clear the metadata file's content. */
3883 ret
= clear_metadata_file(registry
->metadata_fd
);
3885 pthread_mutex_unlock(®istry
->lock
);
3890 ret
= ust_metadata_session_statedump(registry
, NULL
,
3891 registry
->major
, registry
->minor
);
3893 pthread_mutex_unlock(®istry
->lock
);
3894 ERR("Failed to generate session metadata (err = %d)",
3898 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3900 struct ust_registry_event
*event
;
3901 struct lttng_ht_iter iter_event
;
3903 ret
= ust_metadata_channel_statedump(registry
, chan
);
3905 pthread_mutex_unlock(®istry
->lock
);
3906 ERR("Failed to generate channel metadata "
3910 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3912 ret
= ust_metadata_event_statedump(registry
,
3915 pthread_mutex_unlock(®istry
->lock
);
3916 ERR("Failed to generate event metadata "
3922 pthread_mutex_unlock(®istry
->lock
);
3931 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3933 * Ask the consumer to truncate the existing metadata file(s) and
3934 * then regenerate the metadata. Live and per-pid sessions are not
3935 * supported and return an error.
3937 * Return 0 on success or else a LTTNG_ERR code.
3939 int cmd_regenerate_metadata(struct ltt_session
*session
)
3945 ret
= check_regenerate_metadata_support(session
);
3950 if (session
->kernel_session
) {
3951 ret
= kernctl_session_regenerate_metadata(
3952 session
->kernel_session
->fd
);
3954 ERR("Failed to regenerate the kernel metadata");
3959 if (session
->ust_session
) {
3960 ret
= ust_regenerate_metadata(session
->ust_session
);
3962 ERR("Failed to regenerate the UST metadata");
3966 DBG("Cmd metadata regenerate for session %s", session
->name
);
3974 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3976 * Ask the tracer to regenerate a new statedump.
3978 * Return 0 on success or else a LTTNG_ERR code.
3980 int cmd_regenerate_statedump(struct ltt_session
*session
)
3986 if (!session
->active
) {
3987 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3991 if (session
->kernel_session
) {
3992 ret
= kernctl_session_regenerate_statedump(
3993 session
->kernel_session
->fd
);
3995 * Currently, the statedump in kernel can only fail if out
3999 if (ret
== -ENOMEM
) {
4000 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4002 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4004 ERR("Failed to regenerate the kernel statedump");
4009 if (session
->ust_session
) {
4010 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4012 * Currently, the statedump in UST always returns 0.
4015 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4016 ERR("Failed to regenerate the UST statedump");
4020 DBG("Cmd regenerate statedump for session %s", session
->name
);
4027 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4028 struct notification_thread_handle
*notification_thread
)
4032 ssize_t sock_recv_len
;
4033 struct lttng_trigger
*trigger
= NULL
;
4034 struct lttng_buffer_view view
;
4035 struct lttng_dynamic_buffer trigger_buffer
;
4037 lttng_dynamic_buffer_init(&trigger_buffer
);
4038 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4039 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4041 ret
= LTTNG_ERR_NOMEM
;
4045 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4047 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4048 ERR("Failed to receive \"register trigger\" command payload");
4049 /* TODO: should this be a new error enum ? */
4050 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4054 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4055 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4057 ERR("Invalid trigger payload received in \"register trigger\" command");
4058 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4062 ret
= notification_thread_command_register_trigger(notification_thread
,
4064 /* Ownership of trigger was transferred. */
4067 lttng_trigger_destroy(trigger
);
4068 lttng_dynamic_buffer_reset(&trigger_buffer
);
4072 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4073 struct notification_thread_handle
*notification_thread
)
4077 ssize_t sock_recv_len
;
4078 struct lttng_trigger
*trigger
= NULL
;
4079 struct lttng_buffer_view view
;
4080 struct lttng_dynamic_buffer trigger_buffer
;
4082 lttng_dynamic_buffer_init(&trigger_buffer
);
4083 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4084 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4086 ret
= LTTNG_ERR_NOMEM
;
4090 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4092 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4093 ERR("Failed to receive \"unregister trigger\" command payload");
4094 /* TODO: should this be a new error enum ? */
4095 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4099 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4100 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4102 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4103 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4107 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4110 lttng_trigger_destroy(trigger
);
4111 lttng_dynamic_buffer_reset(&trigger_buffer
);
4116 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4117 * snapshot output is *not* set with a remote destination.
4119 * Return 0 on success or a LTTNG_ERR code.
4121 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
4122 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
4125 struct lttng_ht_iter iter
;
4126 struct consumer_socket
*socket
;
4129 assert(snap_output
);
4132 DBG2("Set relayd object from snapshot output");
4134 /* Ignore if snapshot consumer output is not network. */
4135 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
4140 * For each consumer socket, create and send the relayd object of the
4144 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
4145 socket
, node
.node
) {
4146 pthread_mutex_lock(socket
->lock
);
4147 ret
= send_consumer_relayd_sockets(0, session
->id
,
4148 snap_output
->consumer
, socket
,
4149 session
->name
, session
->hostname
,
4150 session
->live_timer
);
4151 pthread_mutex_unlock(socket
->lock
);
4152 if (ret
!= LTTNG_OK
) {
4164 * Record a kernel snapshot.
4166 * Return LTTNG_OK on success or a LTTNG_ERR code.
4168 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
4169 struct snapshot_output
*output
, struct ltt_session
*session
,
4170 int wait
, uint64_t nb_packets_per_stream
)
4180 * Copy kernel session sockets so we can communicate with the right
4181 * consumer for the snapshot record command.
4183 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
4185 ret
= LTTNG_ERR_NOMEM
;
4189 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
4190 if (ret
!= LTTNG_OK
) {
4191 goto error_snapshot
;
4194 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
4195 if (ret
!= LTTNG_OK
) {
4196 goto error_snapshot
;
4203 /* Clean up copied sockets so this output can use some other later on. */
4204 consumer_destroy_output_sockets(output
->consumer
);
4211 * Record a UST snapshot.
4213 * Return 0 on success or a LTTNG_ERR error code.
4215 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
4216 struct snapshot_output
*output
, struct ltt_session
*session
,
4217 int wait
, uint64_t nb_packets_per_stream
)
4226 * Copy UST session sockets so we can communicate with the right
4227 * consumer for the snapshot record command.
4229 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
4231 ret
= LTTNG_ERR_NOMEM
;
4235 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
4236 if (ret
!= LTTNG_OK
) {
4237 goto error_snapshot
;
4240 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
4244 ret
= LTTNG_ERR_INVALID
;
4247 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4250 goto error_snapshot
;
4256 /* Clean up copied sockets so this output can use some other later on. */
4257 consumer_destroy_output_sockets(output
->consumer
);
4263 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
4264 uint64_t cur_nr_packets
)
4266 uint64_t tot_size
= 0;
4268 if (session
->kernel_session
) {
4269 struct ltt_kernel_channel
*chan
;
4270 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4272 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4273 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4275 * Don't take channel into account if we
4276 * already grab all its packets.
4280 tot_size
+= chan
->channel
->attr
.subbuf_size
4281 * chan
->stream_count
;
4285 if (session
->ust_session
) {
4286 struct ltt_ust_session
*usess
= session
->ust_session
;
4288 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4296 * Calculate the number of packets we can grab from each stream that
4297 * fits within the overall snapshot max size.
4299 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4300 * the number of packets per stream.
4302 * TODO: this approach is not perfect: we consider the worse case
4303 * (packet filling the sub-buffers) as an upper bound, but we could do
4304 * better if we do this calculation while we actually grab the packet
4305 * content: we would know how much padding we don't actually store into
4308 * This algorithm is currently bounded by the number of packets per
4311 * Since we call this algorithm before actually grabbing the data, it's
4312 * an approximation: for instance, applications could appear/disappear
4313 * in between this call and actually grabbing data.
4316 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
4319 uint64_t cur_nb_packets
= 0;
4322 return 0; /* Infinite */
4325 size_left
= max_size
;
4327 uint64_t one_more_packet_tot_size
;
4329 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
4331 if (!one_more_packet_tot_size
) {
4332 /* We are already grabbing all packets. */
4335 size_left
-= one_more_packet_tot_size
;
4336 if (size_left
< 0) {
4341 if (!cur_nb_packets
) {
4342 /* Not enough room to grab one packet of each stream, error. */
4345 return cur_nb_packets
;
4349 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4351 * The wait parameter is ignored so this call always wait for the snapshot to
4352 * complete before returning.
4354 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4356 int cmd_snapshot_record(struct ltt_session
*session
,
4357 struct lttng_snapshot_output
*output
, int wait
)
4360 unsigned int use_tmp_output
= 0;
4361 struct snapshot_output tmp_output
;
4362 unsigned int snapshot_success
= 0;
4368 DBG("Cmd snapshot record for session %s", session
->name
);
4370 /* Get the datetime for the snapshot output directory. */
4371 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4374 ret
= LTTNG_ERR_INVALID
;
4379 * Permission denied to create an output if the session is not
4380 * set in no output mode.
4382 if (session
->output_traces
) {
4383 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4387 /* The session needs to be started at least once. */
4388 if (!session
->has_been_started
) {
4389 ret
= LTTNG_ERR_START_SESSION_ONCE
;
4393 /* Use temporary output for the session. */
4394 if (*output
->ctrl_url
!= '\0') {
4395 ret
= snapshot_output_init(output
->max_size
, output
->name
,
4396 output
->ctrl_url
, output
->data_url
, session
->consumer
,
4399 if (ret
== -ENOMEM
) {
4400 ret
= LTTNG_ERR_NOMEM
;
4402 ret
= LTTNG_ERR_INVALID
;
4406 /* Use the global session count for the temporary snapshot. */
4407 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4409 /* Use the global datetime */
4410 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4414 if (use_tmp_output
) {
4415 int64_t nb_packets_per_stream
;
4417 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4418 tmp_output
.max_size
);
4419 if (nb_packets_per_stream
< 0) {
4420 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4424 if (session
->kernel_session
) {
4425 ret
= record_kernel_snapshot(session
->kernel_session
,
4426 &tmp_output
, session
,
4427 wait
, nb_packets_per_stream
);
4428 if (ret
!= LTTNG_OK
) {
4433 if (session
->ust_session
) {
4434 ret
= record_ust_snapshot(session
->ust_session
,
4435 &tmp_output
, session
,
4436 wait
, nb_packets_per_stream
);
4437 if (ret
!= LTTNG_OK
) {
4442 snapshot_success
= 1;
4444 struct snapshot_output
*sout
;
4445 struct lttng_ht_iter iter
;
4448 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4449 &iter
.iter
, sout
, node
.node
) {
4450 int64_t nb_packets_per_stream
;
4453 * Make a local copy of the output and assign the possible
4454 * temporary value given by the caller.
4456 memset(&tmp_output
, 0, sizeof(tmp_output
));
4457 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
4459 if (output
->max_size
!= (uint64_t) -1ULL) {
4460 tmp_output
.max_size
= output
->max_size
;
4463 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4464 tmp_output
.max_size
);
4465 if (nb_packets_per_stream
< 0) {
4466 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4471 /* Use temporary name. */
4472 if (*output
->name
!= '\0') {
4473 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4474 sizeof(tmp_output
.name
))) {
4475 ret
= LTTNG_ERR_INVALID
;
4481 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4482 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4484 if (session
->kernel_session
) {
4485 ret
= record_kernel_snapshot(session
->kernel_session
,
4486 &tmp_output
, session
,
4487 wait
, nb_packets_per_stream
);
4488 if (ret
!= LTTNG_OK
) {
4494 if (session
->ust_session
) {
4495 ret
= record_ust_snapshot(session
->ust_session
,
4496 &tmp_output
, session
,
4497 wait
, nb_packets_per_stream
);
4498 if (ret
!= LTTNG_OK
) {
4503 snapshot_success
= 1;
4508 if (snapshot_success
) {
4509 session
->snapshot
.nb_snapshot
++;
4511 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4519 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4521 int cmd_set_session_shm_path(struct ltt_session
*session
,
4522 const char *shm_path
)
4528 * Can only set shm path before session is started.
4530 if (session
->has_been_started
) {
4531 return LTTNG_ERR_SESSION_STARTED
;
4534 strncpy(session
->shm_path
, shm_path
,
4535 sizeof(session
->shm_path
));
4536 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4542 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4544 * Ask the consumer to rotate the session output directory.
4545 * The session lock must be held.
4547 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4549 int cmd_rotate_session(struct ltt_session
*session
,
4550 struct lttng_rotate_session_return
*rotate_return
)
4554 struct tm
*timeinfo
;
4557 bool ust_active
= false;
4561 if (!session
->has_been_started
) {
4562 ret
= -LTTNG_ERR_START_SESSION_ONCE
;
4566 if (session
->live_timer
|| session
->snapshot_mode
||
4567 !session
->output_traces
) {
4568 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4573 * Unsupported feature in lttng-relayd before 2.11.
4575 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4576 (session
->consumer
->relay_major_version
== 2 &&
4577 session
->consumer
->relay_minor_version
< 11)) {
4578 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
4582 if (session
->rotate_pending
|| session
->rotate_pending_relay
) {
4583 ret
= -LTTNG_ERR_ROTATION_PENDING
;
4584 DBG("Rotate already in progress");
4589 * After a stop, we only allow one rotation to occur, the other ones are
4590 * useless until a new start.
4592 if (session
->rotated_after_last_stop
) {
4593 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4595 ret
= -LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
4599 /* Special case for the first rotation. */
4600 if (session
->current_archive_id
== 0) {
4601 const char *base_path
= NULL
;
4603 /* Either one of the two sessions is enough to get the root path. */
4604 if (session
->kernel_session
) {
4605 base_path
= session_get_base_path(session
);
4606 } else if (session
->ust_session
) {
4607 base_path
= session_get_base_path(session
);
4612 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4614 sizeof(session
->rotation_chunk
.current_rotate_path
));
4616 ERR("Failed to copy session base path to current rotation chunk path");
4617 ret
= -LTTNG_ERR_UNK
;
4622 * The currently active tracing path is now the folder we
4625 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4626 session
->rotation_chunk
.active_tracing_path
,
4627 sizeof(session
->rotation_chunk
.current_rotate_path
));
4629 ERR("Failed to copy the active tracing path to the current rotate path");
4630 ret
= -LTTNG_ERR_UNK
;
4634 DBG("Current rotate path %s", session
->rotation_chunk
.current_rotate_path
);
4636 session
->current_archive_id
++;
4637 session
->rotate_pending
= true;
4638 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
4639 ret
= notification_thread_command_session_rotation_ongoing(
4640 notification_thread_handle
,
4641 session
->name
, session
->uid
, session
->gid
,
4642 session
->current_archive_id
);
4643 if (ret
!= LTTNG_OK
) {
4644 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
4649 * Create the path name for the next chunk.
4652 if (now
== (time_t) -1) {
4653 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4656 session
->last_chunk_start_ts
= session
->current_chunk_start_ts
;
4657 session
->current_chunk_start_ts
= now
;
4659 timeinfo
= localtime(&now
);
4661 PERROR("Failed to sample local time in rotate session command");
4662 ret
= -LTTNG_ERR_UNK
;
4665 strf_ret
= strftime(datetime
, sizeof(datetime
), "%Y%m%dT%H%M%S%z",
4668 ERR("Failed to format local time timestamp in rotate session command");
4669 ret
= -LTTNG_ERR_UNK
;
4672 if (session
->kernel_session
) {
4674 * The active path for the next rotation/destroy.
4675 * Ex: ~/lttng-traces/auto-20170922-111748/20170922-111754-42
4677 ret
= snprintf(session
->rotation_chunk
.active_tracing_path
,
4678 sizeof(session
->rotation_chunk
.active_tracing_path
),
4680 session_get_base_path(session
),
4681 datetime
, session
->current_archive_id
+ 1);
4682 if (ret
< 0 || ret
== sizeof(session
->rotation_chunk
.active_tracing_path
)) {
4683 ERR("Failed to format active kernel tracing path in rotate session command");
4684 ret
= -LTTNG_ERR_UNK
;
4688 * The sub-directory for the consumer
4689 * Ex: /20170922-111754-42/kernel
4691 ret
= snprintf(session
->kernel_session
->consumer
->chunk_path
,
4692 sizeof(session
->kernel_session
->consumer
->chunk_path
),
4693 "/%s-%" PRIu64
, datetime
,
4694 session
->current_archive_id
+ 1);
4695 if (ret
< 0 || ret
== sizeof(session
->kernel_session
->consumer
->chunk_path
)) {
4696 ERR("Failed to format the kernel consumer's sub-directory in rotate session command");
4697 ret
= -LTTNG_ERR_UNK
;
4701 * Create the new chunk folder, before the rotation begins so we don't
4702 * race with the consumer/tracer activity.
4704 ret
= domain_mkdir(session
->kernel_session
->consumer
, session
,
4705 session
->kernel_session
->uid
,
4706 session
->kernel_session
->gid
);
4708 ERR("Failed to create kernel session tracing path at %s",
4709 session
->kernel_session
->consumer
->chunk_path
);
4710 ret
= -LTTNG_ERR_CREATE_DIR_FAIL
;
4713 ret
= kernel_rotate_session(session
);
4714 if (ret
!= LTTNG_OK
) {
4719 if (session
->ust_session
) {
4720 ret
= snprintf(session
->rotation_chunk
.active_tracing_path
,
4721 PATH_MAX
, "%s/%s-%" PRIu64
,
4722 session_get_base_path(session
),
4723 datetime
, session
->current_archive_id
+ 1);
4725 ERR("Failed to format active UST tracing path in rotate session command");
4726 ret
= -LTTNG_ERR_UNK
;
4729 ret
= snprintf(session
->ust_session
->consumer
->chunk_path
,
4730 PATH_MAX
, "/%s-%" PRIu64
, datetime
,
4731 session
->current_archive_id
+ 1);
4733 ERR("Failed to format the UST consumer's sub-directory in rotate session command");
4734 ret
= -LTTNG_ERR_UNK
;
4738 * Create the new chunk folder, before the rotation begins so we don't
4739 * race with the consumer/tracer activity.
4741 ret
= domain_mkdir(session
->ust_session
->consumer
, session
,
4742 session
->ust_session
->uid
,
4743 session
->ust_session
->gid
);
4745 ret
= -LTTNG_ERR_CREATE_DIR_FAIL
;
4748 ret
= ust_app_rotate_session(session
, &ust_active
);
4749 if (ret
!= LTTNG_OK
) {
4753 * Handle the case where we did not start a rotation on any channel.
4754 * The consumer will never wake up the rotation thread to perform the
4755 * rename, so we have to do it here while we hold the session and
4756 * session_list locks.
4758 if (!session
->kernel_session
&& !ust_active
) {
4759 struct lttng_trace_archive_location
*location
;
4761 session
->rotate_pending
= false;
4762 session
->rotation_state
= LTTNG_ROTATION_STATE_COMPLETED
;
4763 ret
= rename_complete_chunk(session
, now
);
4765 ERR("Failed to rename completed rotation chunk");
4769 /* Ownership of location is transferred. */
4770 location
= session_get_trace_archive_location(session
);
4771 ret
= notification_thread_command_session_rotation_completed(
4772 notification_thread_handle
,
4776 session
->current_archive_id
,
4778 if (ret
!= LTTNG_OK
) {
4779 ERR("Failed to notify notification thread that rotation is complete for session %s",
4785 if (!session
->active
) {
4786 session
->rotated_after_last_stop
= true;
4789 if (rotate_return
) {
4790 rotate_return
->rotation_id
= session
->current_archive_id
;
4793 DBG("Cmd rotate session %s, current_archive_id %" PRIu64
" sent",
4794 session
->name
, session
->current_archive_id
);
4802 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
4804 * Check if the session has finished its rotation.
4806 * Return 0 on success or else a LTTNG_ERR code.
4808 int cmd_rotate_get_info(struct ltt_session
*session
,
4809 struct lttng_rotation_get_info_return
*info_return
,
4810 uint64_t rotation_id
)
4816 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
4817 session
->current_archive_id
);
4819 if (session
->current_archive_id
!= rotation_id
) {
4820 info_return
->status
= (int32_t) LTTNG_ROTATION_STATE_EXPIRED
;
4825 switch (session
->rotation_state
) {
4826 case LTTNG_ROTATION_STATE_ONGOING
:
4827 DBG("Reporting that rotation id %" PRIu64
" of session %s is still pending",
4828 rotation_id
, session
->name
);
4830 case LTTNG_ROTATION_STATE_COMPLETED
:
4832 char *current_tracing_path_reply
;
4833 size_t current_tracing_path_reply_len
;
4835 switch (session_get_consumer_destination_type(session
)) {
4836 case CONSUMER_DST_LOCAL
:
4837 current_tracing_path_reply
=
4838 info_return
->location
.local
.absolute_path
;
4839 current_tracing_path_reply_len
=
4840 sizeof(info_return
->location
.local
.absolute_path
);
4841 info_return
->location_type
=
4842 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
4844 case CONSUMER_DST_NET
:
4845 current_tracing_path_reply
=
4846 info_return
->location
.relay
.relative_path
;
4847 current_tracing_path_reply_len
=
4848 sizeof(info_return
->location
.relay
.relative_path
);
4849 /* Currently the only supported relay protocol. */
4850 info_return
->location
.relay
.protocol
=
4851 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
4853 ret
= lttng_strncpy(info_return
->location
.relay
.host
,
4854 session_get_net_consumer_hostname(session
),
4855 sizeof(info_return
->location
.relay
.host
));
4857 ERR("Failed to host name to rotate_get_info reply");
4858 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4859 ret
= -LTTNG_ERR_UNK
;
4863 session_get_net_consumer_ports(session
,
4864 &info_return
->location
.relay
.ports
.control
,
4865 &info_return
->location
.relay
.ports
.data
);
4866 info_return
->location_type
=
4867 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
4872 ret
= lttng_strncpy(current_tracing_path_reply
,
4873 session
->rotation_chunk
.current_rotate_path
,
4874 current_tracing_path_reply_len
);
4876 ERR("Failed to copy current tracing path to rotate_get_info reply");
4877 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4878 ret
= -LTTNG_ERR_UNK
;
4884 case LTTNG_ROTATION_STATE_ERROR
:
4885 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session %s",
4886 rotation_id
, session
->name
);
4892 info_return
->status
= (int32_t) session
->rotation_state
;
4899 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
4901 * Configure the automatic rotation parameters.
4902 * 'activate' to true means activate the rotation schedule type with 'new_value'.
4903 * 'activate' to false means deactivate the rotation schedule and validate that
4904 * 'new_value' has the same value as the currently active value.
4906 * Return 0 on success or else a positive LTTNG_ERR code.
4908 int cmd_rotation_set_schedule(struct ltt_session
*session
,
4909 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
4911 struct notification_thread_handle
*notification_thread_handle
)
4914 uint64_t *parameter_value
;
4918 DBG("Cmd rotate set schedule session %s", session
->name
);
4920 if (session
->live_timer
|| session
->snapshot_mode
||
4921 !session
->output_traces
) {
4922 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
4923 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4927 switch (schedule_type
) {
4928 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
4929 parameter_value
= &session
->rotate_size
;
4931 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
4932 parameter_value
= &session
->rotate_timer_period
;
4933 if (new_value
>= UINT_MAX
) {
4934 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
4935 new_value
, UINT_MAX
);
4936 ret
= LTTNG_ERR_INVALID
;
4941 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
4942 ret
= LTTNG_ERR_INVALID
;
4946 /* Improper use of the API. */
4947 if (new_value
== -1ULL) {
4948 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
4949 ret
= LTTNG_ERR_INVALID
;
4954 * As indicated in struct ltt_session's comments, a value of == 0 means
4955 * this schedule rotation type is not in use.
4957 * Reject the command if we were asked to activate a schedule that was
4960 if (activate
&& *parameter_value
!= 0) {
4961 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
4962 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
4967 * Reject the command if we were asked to deactivate a schedule that was
4970 if (!activate
&& *parameter_value
== 0) {
4971 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
4972 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
4977 * Reject the command if we were asked to deactivate a schedule that
4980 if (!activate
&& *parameter_value
!= new_value
) {
4981 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
4982 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
4986 *parameter_value
= activate
? new_value
: 0;
4988 switch (schedule_type
) {
4989 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
4990 if (activate
&& session
->active
) {
4992 * Only start the timer if the session is active,
4993 * otherwise it will be started when the session starts.
4995 ret
= sessiond_rotate_timer_start(session
, new_value
);
4997 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
4998 ret
= LTTNG_ERR_UNK
;
5002 ret
= sessiond_rotate_timer_stop(session
);
5004 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5005 ret
= LTTNG_ERR_UNK
;
5010 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5012 ret
= subscribe_session_consumed_size_rotation(session
,
5013 new_value
, notification_thread_handle
);
5015 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5016 ret
= LTTNG_ERR_UNK
;
5020 ret
= unsubscribe_session_consumed_size_rotation(session
,
5021 notification_thread_handle
);
5023 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5024 ret
= LTTNG_ERR_UNK
;
5031 /* Would have been caught before. */
5044 * Command ROTATE_GET_CURRENT_PATH from the lttng-ctl library.
5046 * Configure the automatic rotation parameters.
5047 * Set to -1ULL to disable them.
5049 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5051 int cmd_session_get_current_output(struct ltt_session
*session
,
5052 struct lttng_session_get_current_output_return
*output_return
)
5057 if (!session
->snapshot_mode
) {
5058 if (session
->current_archive_id
== 0) {
5059 if (session
->kernel_session
) {
5060 path
= session_get_base_path(session
);
5061 } else if (session
->ust_session
) {
5062 path
= session_get_base_path(session
);
5068 path
= session
->rotation_chunk
.active_tracing_path
;
5072 * A snapshot session does not have a "current" trace archive
5078 DBG("Cmd get current output for session %s, returning %s",
5079 session
->name
, path
);
5081 ret
= lttng_strncpy(output_return
->path
,
5083 sizeof(output_return
->path
));
5085 ERR("Failed to copy trace output path to session get current output command reply");
5086 ret
= -LTTNG_ERR_UNK
;
5095 /* Wait for a given path to be removed before continuing. */
5096 static enum lttng_error_code
wait_on_path(void *path_data
)
5098 const char *shm_path
= path_data
;
5100 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5106 ret
= stat(shm_path
, &st
);
5108 if (errno
!= ENOENT
) {
5109 PERROR("stat() returned an error while checking for the existence of the shm path");
5111 DBG("shm path no longer exists, completing the destruction of session");
5115 if (!S_ISDIR(st
.st_mode
)) {
5116 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5121 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5127 * Returns a pointer to a handler to run on completion of a command.
5128 * Returns NULL if no handler has to be run for the last command executed.
5130 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5132 struct cmd_completion_handler
*handler
= current_completion_handler
;
5134 current_completion_handler
= NULL
;
5139 * Init command subsystem.
5144 * Set network sequence index to 1 for streams to match a relayd
5145 * socket on the consumer side.
5147 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5148 relayd_net_seq_idx
= 1;
5149 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5151 DBG("Command subsystem initialized");