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"
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
= NULL
;
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
);
574 * Create a list of ust global domain events.
576 static int list_lttng_ust_global_events(char *channel_name
,
577 struct ltt_ust_domain_global
*ust_global
,
578 struct lttng_event
**events
, size_t *total_size
)
581 unsigned int nb_event
= 0;
582 struct lttng_ht_iter iter
;
583 struct lttng_ht_node_str
*node
;
584 struct ltt_ust_channel
*uchan
;
585 struct ltt_ust_event
*uevent
;
586 struct lttng_event
*tmp
;
587 size_t extended_len
= 0;
590 DBG("Listing UST global events for channel %s", channel_name
);
594 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
595 node
= lttng_ht_iter_get_node_str(&iter
);
597 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
601 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
603 nb_event
= lttng_ht_get_count(uchan
->events
);
610 DBG3("Listing UST global %d events", nb_event
);
612 /* Compute required extended infos size */
613 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
614 if (uevent
->internal
) {
619 ret
= increment_extended_len(uevent
->filter_expression
,
620 uevent
->exclusion
, NULL
, &extended_len
);
622 DBG("Error computing the length of extended info message");
623 ret
= -LTTNG_ERR_FATAL
;
628 /* All events are internal, skip. */
634 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
635 tmp
= zmalloc(*total_size
);
637 ret
= -LTTNG_ERR_FATAL
;
641 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
643 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
644 if (uevent
->internal
) {
645 /* This event should remain hidden from clients */
648 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
649 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
650 tmp
[i
].enabled
= uevent
->enabled
;
652 switch (uevent
->attr
.instrumentation
) {
653 case LTTNG_UST_TRACEPOINT
:
654 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
656 case LTTNG_UST_PROBE
:
657 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
659 case LTTNG_UST_FUNCTION
:
660 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
664 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
665 switch (uevent
->attr
.loglevel_type
) {
666 case LTTNG_UST_LOGLEVEL_ALL
:
667 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
669 case LTTNG_UST_LOGLEVEL_RANGE
:
670 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
672 case LTTNG_UST_LOGLEVEL_SINGLE
:
673 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
676 if (uevent
->filter
) {
679 if (uevent
->exclusion
) {
680 tmp
[i
].exclusion
= 1;
684 /* Append extended info */
685 ret
= append_extended_info(uevent
->filter_expression
,
686 uevent
->exclusion
, NULL
, &extended_at
);
688 DBG("Error appending extended info message");
689 ret
= -LTTNG_ERR_FATAL
;
702 * Fill lttng_event array of all kernel events in the channel.
704 static int list_lttng_kernel_events(char *channel_name
,
705 struct ltt_kernel_session
*kernel_session
,
706 struct lttng_event
**events
, size_t *total_size
)
709 unsigned int nb_event
;
710 struct ltt_kernel_event
*event
;
711 struct ltt_kernel_channel
*kchan
;
712 size_t extended_len
= 0;
715 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
717 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
721 nb_event
= kchan
->event_count
;
723 DBG("Listing events for channel %s", kchan
->channel
->name
);
731 /* Compute required extended infos size */
732 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
733 ret
= increment_extended_len(event
->filter_expression
, NULL
,
734 event
->userspace_probe_location
,
737 DBG("Error computing the length of extended info message");
738 ret
= -LTTNG_ERR_FATAL
;
743 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
744 *events
= zmalloc(*total_size
);
745 if (*events
== NULL
) {
746 ret
= -LTTNG_ERR_FATAL
;
750 extended_at
= ((void *) *events
) +
751 nb_event
* sizeof(struct lttng_event
);
753 /* Kernel channels */
754 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
755 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
756 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
757 (*events
)[i
].enabled
= event
->enabled
;
758 (*events
)[i
].filter
=
759 (unsigned char) !!event
->filter_expression
;
761 switch (event
->event
->instrumentation
) {
762 case LTTNG_KERNEL_TRACEPOINT
:
763 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
765 case LTTNG_KERNEL_KRETPROBE
:
766 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
767 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
768 sizeof(struct lttng_kernel_kprobe
));
770 case LTTNG_KERNEL_KPROBE
:
771 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
772 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
773 sizeof(struct lttng_kernel_kprobe
));
775 case LTTNG_KERNEL_UPROBE
:
776 (*events
)[i
].type
= LTTNG_EVENT_USERSPACE_PROBE
;
778 case LTTNG_KERNEL_FUNCTION
:
779 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
780 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
781 sizeof(struct lttng_kernel_function
));
783 case LTTNG_KERNEL_NOOP
:
784 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
786 case LTTNG_KERNEL_SYSCALL
:
787 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
789 case LTTNG_KERNEL_ALL
:
797 /* Append extended info */
798 ret
= append_extended_info(event
->filter_expression
, NULL
,
799 event
->userspace_probe_location
, &extended_at
);
801 DBG("Error appending extended info message");
802 ret
= -LTTNG_ERR_FATAL
;
811 /* Negate the error code to differentiate the size from an error */
816 * Add URI so the consumer output object. Set the correct path depending on the
817 * domain adding the default trace directory.
819 static int add_uri_to_consumer(struct consumer_output
*consumer
,
820 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
821 const char *session_name
)
824 const char *default_trace_dir
;
828 if (consumer
== NULL
) {
829 DBG("No consumer detected. Don't add URI. Stopping.");
830 ret
= LTTNG_ERR_NO_CONSUMER
;
835 case LTTNG_DOMAIN_KERNEL
:
836 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
838 case LTTNG_DOMAIN_UST
:
839 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
843 * This case is possible is we try to add the URI to the global tracing
844 * session consumer object which in this case there is no subdir.
846 default_trace_dir
= "";
849 switch (uri
->dtype
) {
852 DBG2("Setting network URI to consumer");
854 if (consumer
->type
== CONSUMER_DST_NET
) {
855 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
856 consumer
->dst
.net
.control_isset
) ||
857 (uri
->stype
== LTTNG_STREAM_DATA
&&
858 consumer
->dst
.net
.data_isset
)) {
859 ret
= LTTNG_ERR_URL_EXIST
;
863 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
866 consumer
->type
= CONSUMER_DST_NET
;
868 /* Set URI into consumer output object */
869 ret
= consumer_set_network_uri(consumer
, uri
);
873 } else if (ret
== 1) {
875 * URI was the same in the consumer so we do not append the subdir
876 * again so to not duplicate output dir.
882 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
883 ret
= consumer_set_subdir(consumer
, session_name
);
885 ret
= LTTNG_ERR_FATAL
;
890 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
891 /* On a new subdir, reappend the default trace dir. */
892 strncat(consumer
->subdir
, default_trace_dir
,
893 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
894 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
899 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
900 memset(consumer
->dst
.session_root_path
, 0,
901 sizeof(consumer
->dst
.session_root_path
));
902 /* Explicit length checks for strcpy and strcat. */
903 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
904 >= sizeof(consumer
->dst
.session_root_path
)) {
905 ret
= LTTNG_ERR_FATAL
;
908 strcpy(consumer
->dst
.session_root_path
, uri
->dst
.path
);
909 /* Append default trace dir */
910 strcat(consumer
->dst
.session_root_path
, default_trace_dir
);
911 /* Flag consumer as local. */
912 consumer
->type
= CONSUMER_DST_LOCAL
;
923 * Init tracing by creating trace directory and sending fds kernel consumer.
925 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
928 struct lttng_ht_iter iter
;
929 struct consumer_socket
*socket
;
935 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
936 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
938 pthread_mutex_lock(socket
->lock
);
939 ret
= kernel_consumer_send_session(socket
, session
);
940 pthread_mutex_unlock(socket
->lock
);
942 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
954 * Create a socket to the relayd using the URI.
956 * On success, the relayd_sock pointer is set to the created socket.
957 * Else, it's stays untouched and a lttcomm error code is returned.
959 static int create_connect_relayd(struct lttng_uri
*uri
,
960 struct lttcomm_relayd_sock
**relayd_sock
,
961 struct consumer_output
*consumer
)
964 struct lttcomm_relayd_sock
*rsock
;
966 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
967 RELAYD_VERSION_COMM_MINOR
);
969 ret
= LTTNG_ERR_FATAL
;
974 * Connect to relayd so we can proceed with a session creation. This call
975 * can possibly block for an arbitrary amount of time to set the health
976 * state to be in poll execution.
979 ret
= relayd_connect(rsock
);
982 ERR("Unable to reach lttng-relayd");
983 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
987 /* Create socket for control stream. */
988 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
989 DBG3("Creating relayd stream socket from URI");
991 /* Check relayd version */
992 ret
= relayd_version_check(rsock
);
993 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
995 } else if (ret
< 0) {
996 ERR("Unable to reach lttng-relayd");
997 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1000 consumer
->relay_major_version
= rsock
->major
;
1001 consumer
->relay_minor_version
= rsock
->minor
;
1002 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1003 DBG3("Creating relayd data socket from URI");
1005 /* Command is not valid */
1006 ERR("Relayd invalid stream type: %d", uri
->stype
);
1007 ret
= LTTNG_ERR_INVALID
;
1011 *relayd_sock
= rsock
;
1016 /* The returned value is not useful since we are on an error path. */
1017 (void) relayd_close(rsock
);
1025 * Connect to the relayd using URI and send the socket to the right consumer.
1027 * The consumer socket lock must be held by the caller.
1029 static int send_consumer_relayd_socket(unsigned int session_id
,
1030 struct lttng_uri
*relayd_uri
,
1031 struct consumer_output
*consumer
,
1032 struct consumer_socket
*consumer_sock
,
1033 char *session_name
, char *hostname
, int session_live_timer
)
1036 struct lttcomm_relayd_sock
*rsock
= NULL
;
1038 /* Connect to relayd and make version check if uri is the control. */
1039 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1040 if (ret
!= LTTNG_OK
) {
1041 goto relayd_comm_error
;
1045 /* Set the network sequence index if not set. */
1046 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1047 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1049 * Increment net_seq_idx because we are about to transfer the
1050 * new relayd socket to the consumer.
1051 * Assign unique key so the consumer can match streams.
1053 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1054 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1057 /* Send relayd socket to consumer. */
1058 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1059 relayd_uri
->stype
, session_id
,
1060 session_name
, hostname
, session_live_timer
);
1062 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1066 /* Flag that the corresponding socket was sent. */
1067 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1068 consumer_sock
->control_sock_sent
= 1;
1069 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1070 consumer_sock
->data_sock_sent
= 1;
1076 * Close socket which was dup on the consumer side. The session daemon does
1077 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1081 if (ret
!= LTTNG_OK
) {
1083 * The consumer output for this session should not be used anymore
1084 * since the relayd connection failed thus making any tracing or/and
1085 * streaming not usable.
1087 consumer
->enabled
= 0;
1089 (void) relayd_close(rsock
);
1097 * Send both relayd sockets to a specific consumer and domain. This is a
1098 * helper function to facilitate sending the information to the consumer for a
1101 * The consumer socket lock must be held by the caller.
1103 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
1104 unsigned int session_id
, struct consumer_output
*consumer
,
1105 struct consumer_socket
*sock
, char *session_name
,
1106 char *hostname
, int session_live_timer
)
1113 /* Sending control relayd socket. */
1114 if (!sock
->control_sock_sent
) {
1115 ret
= send_consumer_relayd_socket(session_id
,
1116 &consumer
->dst
.net
.control
, consumer
, sock
,
1117 session_name
, hostname
, session_live_timer
);
1118 if (ret
!= LTTNG_OK
) {
1123 /* Sending data relayd socket. */
1124 if (!sock
->data_sock_sent
) {
1125 ret
= send_consumer_relayd_socket(session_id
,
1126 &consumer
->dst
.net
.data
, consumer
, sock
,
1127 session_name
, hostname
, session_live_timer
);
1128 if (ret
!= LTTNG_OK
) {
1138 * Setup relayd connections for a tracing session. First creates the socket to
1139 * the relayd and send them to the right domain consumer. Consumer type MUST be
1142 int cmd_setup_relayd(struct ltt_session
*session
)
1145 struct ltt_ust_session
*usess
;
1146 struct ltt_kernel_session
*ksess
;
1147 struct consumer_socket
*socket
;
1148 struct lttng_ht_iter iter
;
1152 usess
= session
->ust_session
;
1153 ksess
= session
->kernel_session
;
1155 DBG("Setting relayd for session %s", session
->name
);
1159 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1160 && usess
->consumer
->enabled
) {
1161 /* For each consumer socket, send relayd sockets */
1162 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1163 socket
, node
.node
) {
1164 pthread_mutex_lock(socket
->lock
);
1165 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1166 usess
->consumer
, socket
,
1167 session
->name
, session
->hostname
,
1168 session
->live_timer
);
1169 pthread_mutex_unlock(socket
->lock
);
1170 if (ret
!= LTTNG_OK
) {
1173 /* Session is now ready for network streaming. */
1174 session
->net_handle
= 1;
1176 session
->consumer
->relay_major_version
=
1177 usess
->consumer
->relay_major_version
;
1178 session
->consumer
->relay_minor_version
=
1179 usess
->consumer
->relay_minor_version
;
1182 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1183 && ksess
->consumer
->enabled
) {
1184 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1185 socket
, node
.node
) {
1186 pthread_mutex_lock(socket
->lock
);
1187 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1188 ksess
->consumer
, socket
,
1189 session
->name
, session
->hostname
,
1190 session
->live_timer
);
1191 pthread_mutex_unlock(socket
->lock
);
1192 if (ret
!= LTTNG_OK
) {
1195 /* Session is now ready for network streaming. */
1196 session
->net_handle
= 1;
1198 session
->consumer
->relay_major_version
=
1199 ksess
->consumer
->relay_major_version
;
1200 session
->consumer
->relay_minor_version
=
1201 ksess
->consumer
->relay_minor_version
;
1210 * Start a kernel session by opening all necessary streams.
1212 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1215 struct ltt_kernel_channel
*kchan
;
1217 /* Open kernel metadata */
1218 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1219 ret
= kernel_open_metadata(ksess
);
1221 ret
= LTTNG_ERR_KERN_META_FAIL
;
1226 /* Open kernel metadata stream */
1227 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1228 ret
= kernel_open_metadata_stream(ksess
);
1230 ERR("Kernel create metadata stream failed");
1231 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1236 /* For each channel */
1237 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1238 if (kchan
->stream_count
== 0) {
1239 ret
= kernel_open_channel_stream(kchan
);
1241 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1244 /* Update the stream global counter */
1245 ksess
->stream_count_global
+= ret
;
1249 /* Setup kernel consumer socket and send fds to it */
1250 ret
= init_kernel_tracing(ksess
);
1252 ret
= LTTNG_ERR_KERN_START_FAIL
;
1256 /* This start the kernel tracing */
1257 ret
= kernel_start_session(ksess
);
1259 ret
= LTTNG_ERR_KERN_START_FAIL
;
1263 /* Quiescent wait after starting trace */
1264 kernel_wait_quiescent(wpipe
);
1275 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1277 int cmd_disable_channel(struct ltt_session
*session
,
1278 enum lttng_domain_type domain
, char *channel_name
)
1281 struct ltt_ust_session
*usess
;
1283 usess
= session
->ust_session
;
1288 case LTTNG_DOMAIN_KERNEL
:
1290 ret
= channel_kernel_disable(session
->kernel_session
,
1292 if (ret
!= LTTNG_OK
) {
1296 kernel_wait_quiescent(kernel_tracer_fd
);
1299 case LTTNG_DOMAIN_UST
:
1301 struct ltt_ust_channel
*uchan
;
1302 struct lttng_ht
*chan_ht
;
1304 chan_ht
= usess
->domain_global
.channels
;
1306 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1307 if (uchan
== NULL
) {
1308 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1312 ret
= channel_ust_disable(usess
, uchan
);
1313 if (ret
!= LTTNG_OK
) {
1319 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1331 * Command LTTNG_TRACK_PID processed by the client thread.
1333 * Called with session lock held.
1335 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1343 case LTTNG_DOMAIN_KERNEL
:
1345 struct ltt_kernel_session
*ksess
;
1347 ksess
= session
->kernel_session
;
1349 ret
= kernel_track_pid(ksess
, pid
);
1350 if (ret
!= LTTNG_OK
) {
1354 kernel_wait_quiescent(kernel_tracer_fd
);
1357 case LTTNG_DOMAIN_UST
:
1359 struct ltt_ust_session
*usess
;
1361 usess
= session
->ust_session
;
1363 ret
= trace_ust_track_pid(usess
, pid
);
1364 if (ret
!= LTTNG_OK
) {
1370 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1382 * Command LTTNG_UNTRACK_PID processed by the client thread.
1384 * Called with session lock held.
1386 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1394 case LTTNG_DOMAIN_KERNEL
:
1396 struct ltt_kernel_session
*ksess
;
1398 ksess
= session
->kernel_session
;
1400 ret
= kernel_untrack_pid(ksess
, pid
);
1401 if (ret
!= LTTNG_OK
) {
1405 kernel_wait_quiescent(kernel_tracer_fd
);
1408 case LTTNG_DOMAIN_UST
:
1410 struct ltt_ust_session
*usess
;
1412 usess
= session
->ust_session
;
1414 ret
= trace_ust_untrack_pid(usess
, pid
);
1415 if (ret
!= LTTNG_OK
) {
1421 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1433 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1435 * The wpipe arguments is used as a notifier for the kernel thread.
1437 int cmd_enable_channel(struct ltt_session
*session
,
1438 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1441 struct ltt_ust_session
*usess
= session
->ust_session
;
1442 struct lttng_ht
*chan_ht
;
1449 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1451 /* Validate channel name */
1452 if (attr
->name
[0] == '.' ||
1453 memchr(attr
->name
, '/', len
) != NULL
) {
1454 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1458 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1463 * Don't try to enable a channel if the session has been started at
1464 * some point in time before. The tracer does not allow it.
1466 if (session
->has_been_started
) {
1467 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1472 * If the session is a live session, remove the switch timer, the
1473 * live timer does the same thing but sends also synchronisation
1474 * beacons for inactive streams.
1476 if (session
->live_timer
> 0) {
1477 attr
->attr
.live_timer_interval
= session
->live_timer
;
1478 attr
->attr
.switch_timer_interval
= 0;
1481 /* Check for feature support */
1482 switch (domain
->type
) {
1483 case LTTNG_DOMAIN_KERNEL
:
1485 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd
) != 1) {
1486 /* Sampling position of buffer is not supported */
1487 WARN("Kernel tracer does not support buffer monitoring. "
1488 "Setting the monitor interval timer to 0 "
1489 "(disabled) for channel '%s' of session '%s'",
1490 attr
-> name
, session
->name
);
1491 lttng_channel_set_monitor_timer_interval(attr
, 0);
1495 case LTTNG_DOMAIN_UST
:
1497 case LTTNG_DOMAIN_JUL
:
1498 case LTTNG_DOMAIN_LOG4J
:
1499 case LTTNG_DOMAIN_PYTHON
:
1500 if (!agent_tracing_is_enabled()) {
1501 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1502 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1507 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1511 switch (domain
->type
) {
1512 case LTTNG_DOMAIN_KERNEL
:
1514 struct ltt_kernel_channel
*kchan
;
1516 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1517 session
->kernel_session
);
1518 if (kchan
== NULL
) {
1519 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1520 if (attr
->name
[0] != '\0') {
1521 session
->kernel_session
->has_non_default_channel
= 1;
1524 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1527 if (ret
!= LTTNG_OK
) {
1531 kernel_wait_quiescent(kernel_tracer_fd
);
1534 case LTTNG_DOMAIN_UST
:
1535 case LTTNG_DOMAIN_JUL
:
1536 case LTTNG_DOMAIN_LOG4J
:
1537 case LTTNG_DOMAIN_PYTHON
:
1539 struct ltt_ust_channel
*uchan
;
1544 * Current agent implementation limitations force us to allow
1545 * only one channel at once in "agent" subdomains. Each
1546 * subdomain has a default channel name which must be strictly
1549 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1550 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1551 LTTNG_SYMBOL_NAME_LEN
)) {
1552 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1555 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1556 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1557 LTTNG_SYMBOL_NAME_LEN
)) {
1558 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1561 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1562 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1563 LTTNG_SYMBOL_NAME_LEN
)) {
1564 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1569 chan_ht
= usess
->domain_global
.channels
;
1571 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1572 if (uchan
== NULL
) {
1573 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1574 if (attr
->name
[0] != '\0') {
1575 usess
->has_non_default_channel
= 1;
1578 ret
= channel_ust_enable(usess
, uchan
);
1583 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1594 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1596 int cmd_disable_event(struct ltt_session
*session
,
1597 enum lttng_domain_type domain
, char *channel_name
,
1598 struct lttng_event
*event
)
1603 DBG("Disable event command for event \'%s\'", event
->name
);
1605 event_name
= event
->name
;
1607 /* Error out on unhandled search criteria */
1608 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1609 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1610 ret
= LTTNG_ERR_UNK
;
1617 case LTTNG_DOMAIN_KERNEL
:
1619 struct ltt_kernel_channel
*kchan
;
1620 struct ltt_kernel_session
*ksess
;
1622 ksess
= session
->kernel_session
;
1625 * If a non-default channel has been created in the
1626 * session, explicitely require that -c chan_name needs
1629 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1630 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1634 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1635 if (kchan
== NULL
) {
1636 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1640 switch (event
->type
) {
1641 case LTTNG_EVENT_ALL
:
1642 case LTTNG_EVENT_TRACEPOINT
:
1643 case LTTNG_EVENT_SYSCALL
:
1644 case LTTNG_EVENT_PROBE
:
1645 case LTTNG_EVENT_FUNCTION
:
1646 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1647 if (event_name
[0] == '\0') {
1648 ret
= event_kernel_disable_event(kchan
,
1651 ret
= event_kernel_disable_event(kchan
,
1652 event_name
, event
->type
);
1654 if (ret
!= LTTNG_OK
) {
1659 ret
= LTTNG_ERR_UNK
;
1663 kernel_wait_quiescent(kernel_tracer_fd
);
1666 case LTTNG_DOMAIN_UST
:
1668 struct ltt_ust_channel
*uchan
;
1669 struct ltt_ust_session
*usess
;
1671 usess
= session
->ust_session
;
1673 if (validate_ust_event_name(event_name
)) {
1674 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1679 * If a non-default channel has been created in the
1680 * session, explicitly require that -c chan_name needs
1683 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1684 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1688 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1690 if (uchan
== NULL
) {
1691 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1695 switch (event
->type
) {
1696 case LTTNG_EVENT_ALL
:
1698 * An empty event name means that everything
1699 * should be disabled.
1701 if (event
->name
[0] == '\0') {
1702 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1704 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1707 if (ret
!= LTTNG_OK
) {
1712 ret
= LTTNG_ERR_UNK
;
1716 DBG3("Disable UST event %s in channel %s completed", event_name
,
1720 case LTTNG_DOMAIN_LOG4J
:
1721 case LTTNG_DOMAIN_JUL
:
1722 case LTTNG_DOMAIN_PYTHON
:
1725 struct ltt_ust_session
*usess
= session
->ust_session
;
1729 switch (event
->type
) {
1730 case LTTNG_EVENT_ALL
:
1733 ret
= LTTNG_ERR_UNK
;
1737 agt
= trace_ust_find_agent(usess
, domain
);
1739 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1743 * An empty event name means that everything
1744 * should be disabled.
1746 if (event
->name
[0] == '\0') {
1747 ret
= event_agent_disable_all(usess
, agt
);
1749 ret
= event_agent_disable(usess
, agt
, event_name
);
1751 if (ret
!= LTTNG_OK
) {
1758 ret
= LTTNG_ERR_UND
;
1771 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1773 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1774 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1776 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1777 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1780 * Don't try to add a context if the session has been started at
1781 * some point in time before. The tracer does not allow it and would
1782 * result in a corrupted trace.
1784 if (session
->has_been_started
) {
1785 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1789 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1790 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1791 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1795 case LTTNG_DOMAIN_KERNEL
:
1796 assert(session
->kernel_session
);
1798 if (session
->kernel_session
->channel_count
== 0) {
1799 /* Create default channel */
1800 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1801 if (ret
!= LTTNG_OK
) {
1804 chan_kern_created
= 1;
1806 /* Add kernel context to kernel tracer */
1807 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1808 if (ret
!= LTTNG_OK
) {
1812 case LTTNG_DOMAIN_JUL
:
1813 case LTTNG_DOMAIN_LOG4J
:
1816 * Validate channel name.
1817 * If no channel name is given and the domain is JUL or LOG4J,
1818 * set it to the appropriate domain-specific channel name. If
1819 * a name is provided but does not match the expexted channel
1820 * name, return an error.
1822 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1823 strcmp(channel_name
,
1824 DEFAULT_JUL_CHANNEL_NAME
)) {
1825 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1827 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1828 strcmp(channel_name
,
1829 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1830 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1833 /* break is _not_ missing here. */
1835 case LTTNG_DOMAIN_UST
:
1837 struct ltt_ust_session
*usess
= session
->ust_session
;
1838 unsigned int chan_count
;
1842 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1843 if (chan_count
== 0) {
1844 struct lttng_channel
*attr
;
1845 /* Create default channel */
1846 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1848 ret
= LTTNG_ERR_FATAL
;
1852 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1853 if (ret
!= LTTNG_OK
) {
1857 channel_attr_destroy(attr
);
1858 chan_ust_created
= 1;
1861 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1862 free(app_ctx_provider_name
);
1864 app_ctx_name
= NULL
;
1865 app_ctx_provider_name
= NULL
;
1866 if (ret
!= LTTNG_OK
) {
1872 ret
= LTTNG_ERR_UND
;
1880 if (chan_kern_created
) {
1881 struct ltt_kernel_channel
*kchan
=
1882 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1883 session
->kernel_session
);
1884 /* Created previously, this should NOT fail. */
1886 kernel_destroy_channel(kchan
);
1889 if (chan_ust_created
) {
1890 struct ltt_ust_channel
*uchan
=
1891 trace_ust_find_channel_by_name(
1892 session
->ust_session
->domain_global
.channels
,
1893 DEFAULT_CHANNEL_NAME
);
1894 /* Created previously, this should NOT fail. */
1896 /* Remove from the channel list of the session. */
1897 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1899 trace_ust_destroy_channel(uchan
);
1902 free(app_ctx_provider_name
);
1907 static inline bool name_starts_with(const char *name
, const char *prefix
)
1909 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1911 return !strncmp(name
, prefix
, max_cmp_len
);
1914 /* Perform userspace-specific event name validation */
1915 static int validate_ust_event_name(const char *name
)
1925 * Check name against all internal UST event component namespaces used
1928 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1929 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1930 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1939 * Internal version of cmd_enable_event() with a supplemental
1940 * "internal_event" flag which is used to enable internal events which should
1941 * be hidden from clients. Such events are used in the agent implementation to
1942 * enable the events through which all "agent" events are funeled.
1944 static int _cmd_enable_event(struct ltt_session
*session
,
1945 struct lttng_domain
*domain
,
1946 char *channel_name
, struct lttng_event
*event
,
1947 char *filter_expression
,
1948 struct lttng_filter_bytecode
*filter
,
1949 struct lttng_event_exclusion
*exclusion
,
1950 int wpipe
, bool internal_event
)
1952 int ret
= 0, channel_created
= 0;
1953 struct lttng_channel
*attr
= NULL
;
1957 assert(channel_name
);
1959 /* If we have a filter, we must have its filter expression */
1960 assert(!(!!filter_expression
^ !!filter
));
1962 /* Normalize event name as a globbing pattern */
1963 strutils_normalize_star_glob_pattern(event
->name
);
1965 /* Normalize exclusion names as globbing patterns */
1969 for (i
= 0; i
< exclusion
->count
; i
++) {
1970 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
1972 strutils_normalize_star_glob_pattern(name
);
1976 DBG("Enable event command for event \'%s\'", event
->name
);
1980 switch (domain
->type
) {
1981 case LTTNG_DOMAIN_KERNEL
:
1983 struct ltt_kernel_channel
*kchan
;
1986 * If a non-default channel has been created in the
1987 * session, explicitely require that -c chan_name needs
1990 if (session
->kernel_session
->has_non_default_channel
1991 && channel_name
[0] == '\0') {
1992 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1996 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1997 session
->kernel_session
);
1998 if (kchan
== NULL
) {
1999 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2000 LTTNG_BUFFER_GLOBAL
);
2002 ret
= LTTNG_ERR_FATAL
;
2005 if (lttng_strncpy(attr
->name
, channel_name
,
2006 sizeof(attr
->name
))) {
2007 ret
= LTTNG_ERR_INVALID
;
2011 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2012 if (ret
!= LTTNG_OK
) {
2015 channel_created
= 1;
2018 /* Get the newly created kernel channel pointer */
2019 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2020 session
->kernel_session
);
2021 if (kchan
== NULL
) {
2022 /* This sould not happen... */
2023 ret
= LTTNG_ERR_FATAL
;
2027 switch (event
->type
) {
2028 case LTTNG_EVENT_ALL
:
2030 char *filter_expression_a
= NULL
;
2031 struct lttng_filter_bytecode
*filter_a
= NULL
;
2034 * We need to duplicate filter_expression and filter,
2035 * because ownership is passed to first enable
2038 if (filter_expression
) {
2039 filter_expression_a
= strdup(filter_expression
);
2040 if (!filter_expression_a
) {
2041 ret
= LTTNG_ERR_FATAL
;
2046 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2048 free(filter_expression_a
);
2049 ret
= LTTNG_ERR_FATAL
;
2052 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2054 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2055 ret
= event_kernel_enable_event(kchan
, event
,
2056 filter_expression
, filter
);
2057 /* We have passed ownership */
2058 filter_expression
= NULL
;
2060 if (ret
!= LTTNG_OK
) {
2061 if (channel_created
) {
2062 /* Let's not leak a useless channel. */
2063 kernel_destroy_channel(kchan
);
2065 free(filter_expression_a
);
2069 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2070 ret
= event_kernel_enable_event(kchan
, event
,
2071 filter_expression_a
, filter_a
);
2072 /* We have passed ownership */
2073 filter_expression_a
= NULL
;
2075 if (ret
!= LTTNG_OK
) {
2080 case LTTNG_EVENT_PROBE
:
2081 case LTTNG_EVENT_USERSPACE_PROBE
:
2082 case LTTNG_EVENT_FUNCTION
:
2083 case LTTNG_EVENT_FUNCTION_ENTRY
:
2084 case LTTNG_EVENT_TRACEPOINT
:
2085 ret
= event_kernel_enable_event(kchan
, event
,
2086 filter_expression
, filter
);
2087 /* We have passed ownership */
2088 filter_expression
= NULL
;
2090 if (ret
!= LTTNG_OK
) {
2091 if (channel_created
) {
2092 /* Let's not leak a useless channel. */
2093 kernel_destroy_channel(kchan
);
2098 case LTTNG_EVENT_SYSCALL
:
2099 ret
= event_kernel_enable_event(kchan
, event
,
2100 filter_expression
, filter
);
2101 /* We have passed ownership */
2102 filter_expression
= NULL
;
2104 if (ret
!= LTTNG_OK
) {
2109 ret
= LTTNG_ERR_UNK
;
2113 kernel_wait_quiescent(kernel_tracer_fd
);
2116 case LTTNG_DOMAIN_UST
:
2118 struct ltt_ust_channel
*uchan
;
2119 struct ltt_ust_session
*usess
= session
->ust_session
;
2124 * If a non-default channel has been created in the
2125 * session, explicitely require that -c chan_name needs
2128 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2129 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2133 /* Get channel from global UST domain */
2134 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2136 if (uchan
== NULL
) {
2137 /* Create default channel */
2138 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2139 usess
->buffer_type
);
2141 ret
= LTTNG_ERR_FATAL
;
2144 if (lttng_strncpy(attr
->name
, channel_name
,
2145 sizeof(attr
->name
))) {
2146 ret
= LTTNG_ERR_INVALID
;
2150 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2151 if (ret
!= LTTNG_OK
) {
2155 /* Get the newly created channel reference back */
2156 uchan
= trace_ust_find_channel_by_name(
2157 usess
->domain_global
.channels
, channel_name
);
2161 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2163 * Don't allow users to add UST events to channels which
2164 * are assigned to a userspace subdomain (JUL, Log4J,
2167 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2171 if (!internal_event
) {
2173 * Ensure the event name is not reserved for internal
2176 ret
= validate_ust_event_name(event
->name
);
2178 WARN("Userspace event name %s failed validation.",
2180 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2185 /* At this point, the session and channel exist on the tracer */
2186 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2187 filter_expression
, filter
, exclusion
,
2189 /* We have passed ownership */
2190 filter_expression
= NULL
;
2193 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2194 goto already_enabled
;
2195 } else if (ret
!= LTTNG_OK
) {
2200 case LTTNG_DOMAIN_LOG4J
:
2201 case LTTNG_DOMAIN_JUL
:
2202 case LTTNG_DOMAIN_PYTHON
:
2204 const char *default_event_name
, *default_chan_name
;
2206 struct lttng_event uevent
;
2207 struct lttng_domain tmp_dom
;
2208 struct ltt_ust_session
*usess
= session
->ust_session
;
2212 if (!agent_tracing_is_enabled()) {
2213 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2214 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2218 agt
= trace_ust_find_agent(usess
, domain
->type
);
2220 agt
= agent_create(domain
->type
);
2222 ret
= LTTNG_ERR_NOMEM
;
2225 agent_add(agt
, usess
->agents
);
2228 /* Create the default tracepoint. */
2229 memset(&uevent
, 0, sizeof(uevent
));
2230 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2231 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2232 default_event_name
= event_get_default_agent_ust_name(
2234 if (!default_event_name
) {
2235 ret
= LTTNG_ERR_FATAL
;
2238 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2239 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2242 * The domain type is changed because we are about to enable the
2243 * default channel and event for the JUL domain that are hardcoded.
2244 * This happens in the UST domain.
2246 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2247 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2249 switch (domain
->type
) {
2250 case LTTNG_DOMAIN_LOG4J
:
2251 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2253 case LTTNG_DOMAIN_JUL
:
2254 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2256 case LTTNG_DOMAIN_PYTHON
:
2257 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2260 /* The switch/case we are in makes this impossible */
2265 char *filter_expression_copy
= NULL
;
2266 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2269 const size_t filter_size
= sizeof(
2270 struct lttng_filter_bytecode
)
2273 filter_copy
= zmalloc(filter_size
);
2275 ret
= LTTNG_ERR_NOMEM
;
2278 memcpy(filter_copy
, filter
, filter_size
);
2280 filter_expression_copy
=
2281 strdup(filter_expression
);
2282 if (!filter_expression
) {
2283 ret
= LTTNG_ERR_NOMEM
;
2286 if (!filter_expression_copy
|| !filter_copy
) {
2287 free(filter_expression_copy
);
2293 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2294 (char *) default_chan_name
,
2295 &uevent
, filter_expression_copy
,
2296 filter_copy
, NULL
, wpipe
);
2299 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2300 goto already_enabled
;
2301 } else if (ret
!= LTTNG_OK
) {
2305 /* The wild card * means that everything should be enabled. */
2306 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2307 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2310 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2314 filter_expression
= NULL
;
2315 if (ret
!= LTTNG_OK
) {
2322 ret
= LTTNG_ERR_UND
;
2330 free(filter_expression
);
2333 channel_attr_destroy(attr
);
2339 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2340 * We own filter, exclusion, and filter_expression.
2342 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2343 char *channel_name
, struct lttng_event
*event
,
2344 char *filter_expression
,
2345 struct lttng_filter_bytecode
*filter
,
2346 struct lttng_event_exclusion
*exclusion
,
2349 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2350 filter_expression
, filter
, exclusion
, wpipe
, false);
2354 * Enable an event which is internal to LTTng. An internal should
2355 * never be made visible to clients and are immune to checks such as
2358 static int cmd_enable_event_internal(struct ltt_session
*session
,
2359 struct lttng_domain
*domain
,
2360 char *channel_name
, struct lttng_event
*event
,
2361 char *filter_expression
,
2362 struct lttng_filter_bytecode
*filter
,
2363 struct lttng_event_exclusion
*exclusion
,
2366 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2367 filter_expression
, filter
, exclusion
, wpipe
, true);
2371 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2373 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2374 struct lttng_event
**events
)
2377 ssize_t nb_events
= 0;
2380 case LTTNG_DOMAIN_KERNEL
:
2381 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2382 if (nb_events
< 0) {
2383 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2387 case LTTNG_DOMAIN_UST
:
2388 nb_events
= ust_app_list_events(events
);
2389 if (nb_events
< 0) {
2390 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2394 case LTTNG_DOMAIN_LOG4J
:
2395 case LTTNG_DOMAIN_JUL
:
2396 case LTTNG_DOMAIN_PYTHON
:
2397 nb_events
= agent_list_events(events
, domain
);
2398 if (nb_events
< 0) {
2399 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2404 ret
= LTTNG_ERR_UND
;
2411 /* Return negative value to differentiate return code */
2416 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2418 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2419 struct lttng_event_field
**fields
)
2422 ssize_t nb_fields
= 0;
2425 case LTTNG_DOMAIN_UST
:
2426 nb_fields
= ust_app_list_event_fields(fields
);
2427 if (nb_fields
< 0) {
2428 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2432 case LTTNG_DOMAIN_KERNEL
:
2433 default: /* fall-through */
2434 ret
= LTTNG_ERR_UND
;
2441 /* Return negative value to differentiate return code */
2445 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2447 return syscall_table_list(events
);
2451 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2453 * Called with session lock held.
2455 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2456 enum lttng_domain_type domain
, int32_t **pids
)
2459 ssize_t nr_pids
= 0;
2462 case LTTNG_DOMAIN_KERNEL
:
2464 struct ltt_kernel_session
*ksess
;
2466 ksess
= session
->kernel_session
;
2467 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2469 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2474 case LTTNG_DOMAIN_UST
:
2476 struct ltt_ust_session
*usess
;
2478 usess
= session
->ust_session
;
2479 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2481 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2486 case LTTNG_DOMAIN_LOG4J
:
2487 case LTTNG_DOMAIN_JUL
:
2488 case LTTNG_DOMAIN_PYTHON
:
2490 ret
= LTTNG_ERR_UND
;
2497 /* Return negative value to differentiate return code */
2502 int domain_mkdir(const struct consumer_output
*output
,
2503 const struct ltt_session
*session
,
2504 uid_t uid
, gid_t gid
)
2506 struct consumer_socket
*socket
;
2507 struct lttng_ht_iter iter
;
2511 if (!output
|| !output
->socks
) {
2512 ERR("No consumer output found");
2517 path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
2519 ERR("Cannot allocate mkdir path");
2524 ret
= snprintf(path
, LTTNG_PATH_MAX
, "%s%s%s",
2525 session_get_base_path(session
),
2526 output
->chunk_path
, output
->subdir
);
2527 if (ret
< 0 || ret
>= LTTNG_PATH_MAX
) {
2533 DBG("Domain mkdir %s for session %" PRIu64
, path
, session
->id
);
2536 * We have to iterate to find a socket, but we only need to send the
2537 * rename command to one consumer, so we break after the first one.
2539 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
2540 pthread_mutex_lock(socket
->lock
);
2541 ret
= consumer_mkdir(socket
, session
->id
, output
, path
, uid
, gid
);
2542 pthread_mutex_unlock(socket
->lock
);
2544 ERR("Consumer mkdir");
2561 int session_mkdir(const struct ltt_session
*session
)
2564 struct consumer_output
*output
;
2569 * Unsupported feature in lttng-relayd before 2.11, not an error since it
2570 * is only needed for session rotation and the user will get an error
2573 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
2574 session
->consumer
->relay_major_version
== 2 &&
2575 session
->consumer
->relay_minor_version
< 11) {
2580 if (session
->kernel_session
) {
2581 output
= session
->kernel_session
->consumer
;
2582 uid
= session
->kernel_session
->uid
;
2583 gid
= session
->kernel_session
->gid
;
2584 ret
= domain_mkdir(output
, session
, uid
, gid
);
2586 ERR("Mkdir kernel");
2591 if (session
->ust_session
) {
2592 output
= session
->ust_session
->consumer
;
2593 uid
= session
->ust_session
->uid
;
2594 gid
= session
->ust_session
->gid
;
2595 ret
= domain_mkdir(output
, session
, uid
, gid
);
2609 * Command LTTNG_START_TRACE processed by the client thread.
2611 * Called with session mutex held.
2613 int cmd_start_trace(struct ltt_session
*session
)
2616 unsigned long nb_chan
= 0;
2617 struct ltt_kernel_session
*ksession
;
2618 struct ltt_ust_session
*usess
;
2622 /* Ease our life a bit ;) */
2623 ksession
= session
->kernel_session
;
2624 usess
= session
->ust_session
;
2626 /* Is the session already started? */
2627 if (session
->active
) {
2628 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2633 * Starting a session without channel is useless since after that it's not
2634 * possible to enable channel thus inform the client.
2636 if (usess
&& usess
->domain_global
.channels
) {
2637 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2640 nb_chan
+= ksession
->channel_count
;
2643 ret
= LTTNG_ERR_NO_CHANNEL
;
2648 * Record the timestamp of the first time the session is started for
2649 * an eventual session rotation call.
2651 if (!session
->has_been_started
) {
2652 session
->current_chunk_start_ts
= time(NULL
);
2653 if (session
->current_chunk_start_ts
== (time_t) -1) {
2654 PERROR("Failed to retrieve the \"%s\" session's start time",
2656 ret
= LTTNG_ERR_FATAL
;
2659 if (!session
->snapshot_mode
&& session
->output_traces
) {
2660 ret
= session_mkdir(session
);
2662 ERR("Failed to create the session directories");
2663 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2669 /* Kernel tracing */
2670 if (ksession
!= NULL
) {
2671 DBG("Start kernel tracing session %s", session
->name
);
2672 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2673 if (ret
!= LTTNG_OK
) {
2678 /* Flag session that trace should start automatically */
2681 * Even though the start trace might fail, flag this session active so
2682 * other application coming in are started by default.
2686 ret
= ust_app_start_trace_all(usess
);
2688 ret
= LTTNG_ERR_UST_START_FAIL
;
2693 /* Flag this after a successful start. */
2694 session
->has_been_started
= 1;
2695 session
->active
= 1;
2698 * Clear the flag that indicates that a rotation was done while the
2699 * session was stopped.
2701 session
->rotated_after_last_stop
= false;
2703 if (session
->rotate_timer_period
) {
2704 ret
= timer_session_rotation_schedule_timer_start(session
,
2705 session
->rotate_timer_period
);
2707 ERR("Failed to enable rotate timer");
2708 ret
= LTTNG_ERR_UNK
;
2720 int rename_active_chunk(struct ltt_session
*session
)
2724 session
->current_archive_id
++;
2727 * The currently active tracing path is now the folder we
2730 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
2731 session
->rotation_chunk
.active_tracing_path
,
2732 sizeof(session
->rotation_chunk
.current_rotate_path
));
2734 ERR("Failed to copy active tracing path");
2738 ret
= rename_completed_chunk(session
, time(NULL
));
2740 ERR("Failed to rename current rotation's path");
2745 * We just renamed, the folder, we didn't do an actual rotation, so
2746 * the active tracing path is now the renamed folder and we have to
2747 * restore the rotate count.
2749 ret
= lttng_strncpy(session
->rotation_chunk
.active_tracing_path
,
2750 session
->rotation_chunk
.current_rotate_path
,
2751 sizeof(session
->rotation_chunk
.active_tracing_path
));
2753 ERR("Failed to rename active session chunk tracing path");
2757 session
->current_archive_id
--;
2762 * Command LTTNG_STOP_TRACE processed by the client thread.
2764 int cmd_stop_trace(struct ltt_session
*session
)
2767 struct ltt_kernel_channel
*kchan
;
2768 struct ltt_kernel_session
*ksession
;
2769 struct ltt_ust_session
*usess
;
2770 bool error_occured
= false;
2774 DBG("Begin stop session %s (id %" PRIu64
")", session
->name
, session
->id
);
2776 ksession
= session
->kernel_session
;
2777 usess
= session
->ust_session
;
2779 /* Session is not active. Skip everythong and inform the client. */
2780 if (!session
->active
) {
2781 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2785 if (session
->rotation_pending_check_timer_enabled
) {
2786 if (timer_session_rotation_pending_check_stop(session
)) {
2787 ERR("Failed to stop the \"rotation pending check\" timer of session %s",
2792 if (session
->rotation_schedule_timer_enabled
) {
2793 if (timer_session_rotation_schedule_timer_stop(
2795 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
2800 if (session
->current_archive_id
> 0 &&
2801 session
->rotation_state
!= LTTNG_ROTATION_STATE_ONGOING
) {
2802 ret
= rename_active_chunk(session
);
2805 * This error should not prevent the user from stopping
2806 * the session. However, it will be reported at the end.
2808 error_occured
= true;
2813 if (ksession
&& ksession
->active
) {
2814 DBG("Stop kernel tracing");
2816 ret
= kernel_stop_session(ksession
);
2818 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2822 kernel_wait_quiescent(kernel_tracer_fd
);
2824 /* Flush metadata after stopping (if exists) */
2825 if (ksession
->metadata_stream_fd
>= 0) {
2826 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2828 ERR("Kernel metadata flush failed");
2832 /* Flush all buffers after stopping */
2833 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2834 ret
= kernel_flush_buffer(kchan
);
2836 ERR("Kernel flush buffer error");
2840 ksession
->active
= 0;
2841 DBG("Kernel session stopped %s (id %" PRIu64
")", session
->name
,
2845 if (usess
&& usess
->active
) {
2847 * Even though the stop trace might fail, flag this session inactive so
2848 * other application coming in are not started by default.
2852 ret
= ust_app_stop_trace_all(usess
);
2854 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2859 /* Flag inactive after a successful stop. */
2860 session
->active
= 0;
2861 ret
= !error_occured
? LTTNG_OK
: LTTNG_ERR_UNK
;
2868 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2870 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2871 struct lttng_uri
*uris
)
2874 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2875 struct ltt_ust_session
*usess
= session
->ust_session
;
2881 /* Can't set consumer URI if the session is active. */
2882 if (session
->active
) {
2883 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2887 /* Set the "global" consumer URIs */
2888 for (i
= 0; i
< nb_uri
; i
++) {
2889 ret
= add_uri_to_consumer(session
->consumer
,
2890 &uris
[i
], 0, session
->name
);
2891 if (ret
!= LTTNG_OK
) {
2896 /* Set UST session URIs */
2897 if (session
->ust_session
) {
2898 for (i
= 0; i
< nb_uri
; i
++) {
2899 ret
= add_uri_to_consumer(
2900 session
->ust_session
->consumer
,
2901 &uris
[i
], LTTNG_DOMAIN_UST
,
2903 if (ret
!= LTTNG_OK
) {
2909 /* Set kernel session URIs */
2910 if (session
->kernel_session
) {
2911 for (i
= 0; i
< nb_uri
; i
++) {
2912 ret
= add_uri_to_consumer(
2913 session
->kernel_session
->consumer
,
2914 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2916 if (ret
!= LTTNG_OK
) {
2923 * Make sure to set the session in output mode after we set URI since a
2924 * session can be created without URL (thus flagged in no output mode).
2926 session
->output_traces
= 1;
2928 ksess
->output_traces
= 1;
2932 usess
->output_traces
= 1;
2943 * Command LTTNG_CREATE_SESSION processed by the client thread.
2945 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2946 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2949 struct ltt_session
*session
;
2955 * Verify if the session already exist
2957 * XXX: There is no need for the session lock list here since the caller
2958 * (process_client_msg) is holding it. We might want to change that so a
2959 * single command does not lock the entire session list.
2961 session
= session_find_by_name(name
);
2962 if (session
!= NULL
) {
2963 ret
= LTTNG_ERR_EXIST_SESS
;
2967 /* Create tracing session in the registry */
2968 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2969 LTTNG_SOCK_GET_GID_CRED(creds
));
2970 if (ret
!= LTTNG_OK
) {
2975 * Get the newly created session pointer back
2977 * XXX: There is no need for the session lock list here since the caller
2978 * (process_client_msg) is holding it. We might want to change that so a
2979 * single command does not lock the entire session list.
2981 session
= session_find_by_name(name
);
2984 session
->live_timer
= live_timer
;
2985 /* Create default consumer output for the session not yet created. */
2986 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2987 if (session
->consumer
== NULL
) {
2988 ret
= LTTNG_ERR_FATAL
;
2989 goto consumer_error
;
2993 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2994 if (ret
!= LTTNG_OK
) {
2995 goto consumer_error
;
2997 session
->output_traces
= 1;
2999 session
->output_traces
= 0;
3000 DBG2("Session %s created with no output", session
->name
);
3003 session
->consumer
->enabled
= 1;
3008 session_destroy(session
);
3015 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
3017 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
3018 size_t nb_uri
, lttng_sock_cred
*creds
)
3021 struct ltt_session
*session
;
3022 struct snapshot_output
*new_output
= NULL
;
3028 * Create session in no output mode with URIs set to NULL. The uris we've
3029 * received are for a default snapshot output if one.
3031 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
3032 if (ret
!= LTTNG_OK
) {
3036 /* Get the newly created session pointer back. This should NEVER fail. */
3037 session
= session_find_by_name(name
);
3040 /* Flag session for snapshot mode. */
3041 session
->snapshot_mode
= 1;
3043 /* Skip snapshot output creation if no URI is given. */
3048 new_output
= snapshot_output_alloc();
3050 ret
= LTTNG_ERR_NOMEM
;
3051 goto error_snapshot_alloc
;
3054 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
3055 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
3057 if (ret
== -ENOMEM
) {
3058 ret
= LTTNG_ERR_NOMEM
;
3060 ret
= LTTNG_ERR_INVALID
;
3062 goto error_snapshot
;
3066 snapshot_add_output(&session
->snapshot
, new_output
);
3073 snapshot_output_destroy(new_output
);
3074 error_snapshot_alloc
:
3075 session_destroy(session
);
3081 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3083 * Called with session lock held.
3085 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
,
3086 struct notification_thread_handle
*notification_thread_handle
)
3089 struct ltt_ust_session
*usess
;
3090 struct ltt_kernel_session
*ksess
;
3095 usess
= session
->ust_session
;
3096 ksess
= session
->kernel_session
;
3098 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3100 if (session
->rotation_pending_check_timer_enabled
) {
3101 if (timer_session_rotation_pending_check_stop(session
)) {
3102 ERR("Failed to stop the \"rotation pending check\" timer of session %s",
3107 if (session
->rotation_schedule_timer_enabled
) {
3108 if (timer_session_rotation_schedule_timer_stop(
3110 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3115 if (session
->rotate_size
) {
3116 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3117 session
->rotate_size
= 0;
3121 * The rename of the current chunk is performed at stop, but if we rotated
3122 * the session after the previous stop command, we need to rename the
3123 * new (and empty) chunk that was started in between.
3125 if (session
->rotated_after_last_stop
) {
3126 rename_active_chunk(session
);
3129 /* Clean kernel session teardown */
3130 kernel_destroy_session(ksess
);
3132 /* UST session teardown */
3134 /* Close any relayd session */
3135 consumer_output_send_destroy_relayd(usess
->consumer
);
3137 /* Destroy every UST application related to this session. */
3138 ret
= ust_app_destroy_trace_all(usess
);
3140 ERR("Error in ust_app_destroy_trace_all");
3143 /* Clean up the rest. */
3144 trace_ust_destroy_session(usess
);
3148 * Must notify the kernel thread here to update it's poll set in order to
3149 * remove the channel(s)' fd just destroyed.
3151 ret
= notify_thread_pipe(wpipe
);
3153 PERROR("write kernel poll pipe");
3156 if (session
->shm_path
[0]) {
3158 * When a session is created with an explicit shm_path,
3159 * the consumer daemon will create its shared memory files
3160 * at that location and will *not* unlink them. This is normal
3161 * as the intention of that feature is to make it possible
3162 * to retrieve the content of those files should a crash occur.
3164 * To ensure the content of those files can be used, the
3165 * sessiond daemon will replicate the content of the metadata
3166 * cache in a metadata file.
3168 * On clean-up, it is expected that the consumer daemon will
3169 * unlink the shared memory files and that the session daemon
3170 * will unlink the metadata file. Then, the session's directory
3171 * in the shm path can be removed.
3173 * Unfortunately, a flaw in the design of the sessiond's and
3174 * consumerd's tear down of channels makes it impossible to
3175 * determine when the sessiond _and_ the consumerd have both
3176 * destroyed their representation of a channel. For one, the
3177 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3178 * callbacks in both daemons.
3180 * However, it is also impossible for the sessiond to know when
3181 * the consumer daemon is done destroying its channel(s) since
3182 * it occurs as a reaction to the closing of the channel's file
3183 * descriptor. There is no resulting communication initiated
3184 * from the consumerd to the sessiond to confirm that the
3185 * operation is completed (and was successful).
3187 * Until this is all fixed, the session daemon checks for the
3188 * removal of the session's shm path which makes it possible
3189 * to safely advertise a session as having been destroyed.
3191 * Prior to this fix, it was not possible to reliably save
3192 * a session making use of the --shm-path option, destroy it,
3193 * and load it again. This is because the creation of the
3194 * session would fail upon seeing the session's shm path
3195 * already in existence.
3197 * Note that none of the error paths in the check for the
3198 * directory's existence return an error. This is normal
3199 * as there isn't much that can be done. The session will
3200 * be destroyed properly, except that we can't offer the
3201 * guarantee that the same session can be re-created.
3203 current_completion_handler
= &destroy_completion_handler
.handler
;
3204 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3206 sizeof(destroy_completion_handler
.shm_path
));
3209 ret
= session_destroy(session
);
3215 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3217 int cmd_register_consumer(struct ltt_session
*session
,
3218 enum lttng_domain_type domain
, const char *sock_path
,
3219 struct consumer_data
*cdata
)
3222 struct consumer_socket
*socket
= NULL
;
3229 case LTTNG_DOMAIN_KERNEL
:
3231 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3235 /* Can't register a consumer if there is already one */
3236 if (ksess
->consumer_fds_sent
!= 0) {
3237 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3241 sock
= lttcomm_connect_unix_sock(sock_path
);
3243 ret
= LTTNG_ERR_CONNECT_FAIL
;
3246 cdata
->cmd_sock
= sock
;
3248 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3249 if (socket
== NULL
) {
3252 PERROR("close register consumer");
3254 cdata
->cmd_sock
= -1;
3255 ret
= LTTNG_ERR_FATAL
;
3259 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3260 if (socket
->lock
== NULL
) {
3261 PERROR("zmalloc pthread mutex");
3262 ret
= LTTNG_ERR_FATAL
;
3265 pthread_mutex_init(socket
->lock
, NULL
);
3266 socket
->registered
= 1;
3269 consumer_add_socket(socket
, ksess
->consumer
);
3272 pthread_mutex_lock(&cdata
->pid_mutex
);
3274 pthread_mutex_unlock(&cdata
->pid_mutex
);
3279 /* TODO: Userspace tracing */
3280 ret
= LTTNG_ERR_UND
;
3288 consumer_destroy_socket(socket
);
3294 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3296 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3297 struct lttng_domain
**domains
)
3302 struct lttng_ht_iter iter
;
3304 if (session
->kernel_session
!= NULL
) {
3305 DBG3("Listing domains found kernel domain");
3309 if (session
->ust_session
!= NULL
) {
3310 DBG3("Listing domains found UST global domain");
3314 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3316 if (agt
->being_used
) {
3327 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3328 if (*domains
== NULL
) {
3329 ret
= LTTNG_ERR_FATAL
;
3333 if (session
->kernel_session
!= NULL
) {
3334 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3336 /* Kernel session buffer type is always GLOBAL */
3337 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3342 if (session
->ust_session
!= NULL
) {
3343 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3344 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3348 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3350 if (agt
->being_used
) {
3351 (*domains
)[index
].type
= agt
->domain
;
3352 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3362 /* Return negative value to differentiate return code */
3368 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3370 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3371 struct ltt_session
*session
, struct lttng_channel
**channels
)
3373 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3376 case LTTNG_DOMAIN_KERNEL
:
3377 if (session
->kernel_session
!= NULL
) {
3378 nb_chan
= session
->kernel_session
->channel_count
;
3380 DBG3("Number of kernel channels %zd", nb_chan
);
3382 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3386 case LTTNG_DOMAIN_UST
:
3387 if (session
->ust_session
!= NULL
) {
3389 nb_chan
= lttng_ht_get_count(
3390 session
->ust_session
->domain_global
.channels
);
3393 DBG3("Number of UST global channels %zd", nb_chan
);
3395 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3400 ret
= -LTTNG_ERR_UND
;
3405 const size_t channel_size
= sizeof(struct lttng_channel
) +
3406 sizeof(struct lttng_channel_extended
);
3407 struct lttng_channel_extended
*channel_exts
;
3409 payload_size
= nb_chan
* channel_size
;
3410 *channels
= zmalloc(payload_size
);
3411 if (*channels
== NULL
) {
3412 ret
= -LTTNG_ERR_FATAL
;
3416 channel_exts
= ((void *) *channels
) +
3417 (nb_chan
* sizeof(struct lttng_channel
));
3418 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3419 if (ret
!= LTTNG_OK
) {
3434 * Command LTTNG_LIST_EVENTS processed by the client thread.
3436 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3437 struct ltt_session
*session
, char *channel_name
,
3438 struct lttng_event
**events
, size_t *total_size
)
3441 ssize_t nb_event
= 0;
3444 case LTTNG_DOMAIN_KERNEL
:
3445 if (session
->kernel_session
!= NULL
) {
3446 nb_event
= list_lttng_kernel_events(channel_name
,
3447 session
->kernel_session
, events
,
3451 case LTTNG_DOMAIN_UST
:
3453 if (session
->ust_session
!= NULL
) {
3454 nb_event
= list_lttng_ust_global_events(channel_name
,
3455 &session
->ust_session
->domain_global
, events
,
3460 case LTTNG_DOMAIN_LOG4J
:
3461 case LTTNG_DOMAIN_JUL
:
3462 case LTTNG_DOMAIN_PYTHON
:
3463 if (session
->ust_session
) {
3464 struct lttng_ht_iter iter
;
3468 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3469 &iter
.iter
, agt
, node
.node
) {
3470 if (agt
->domain
== domain
) {
3471 nb_event
= list_lttng_agent_events(
3481 ret
= LTTNG_ERR_UND
;
3488 /* Return negative value to differentiate return code */
3493 * Using the session list, filled a lttng_session array to send back to the
3494 * client for session listing.
3496 * The session list lock MUST be acquired before calling this function. Use
3497 * session_lock_list() and session_unlock_list().
3499 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3504 struct ltt_session
*session
;
3505 struct ltt_session_list
*list
= session_get_list();
3507 DBG("Getting all available session for UID %d GID %d",
3510 * Iterate over session list and append data after the control struct in
3513 cds_list_for_each_entry(session
, &list
->head
, list
) {
3515 * Only list the sessions the user can control.
3517 if (!session_access_ok(session
, uid
, gid
)) {
3521 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3522 struct ltt_ust_session
*usess
= session
->ust_session
;
3524 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3525 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3526 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3527 ret
= build_network_session_path(sessions
[i
].path
,
3528 sizeof(sessions
[i
].path
), session
);
3530 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3531 session
->consumer
->dst
.session_root_path
);
3534 PERROR("snprintf session path");
3538 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3539 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3540 sessions
[i
].enabled
= session
->active
;
3541 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3542 sessions
[i
].live_timer_interval
= session
->live_timer
;
3548 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3549 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3551 int cmd_data_pending(struct ltt_session
*session
)
3554 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3555 struct ltt_ust_session
*usess
= session
->ust_session
;
3559 DBG("Data pending for session %s", session
->name
);
3561 /* Session MUST be stopped to ask for data availability. */
3562 if (session
->active
) {
3563 ret
= LTTNG_ERR_SESSION_STARTED
;
3567 * If stopped, just make sure we've started before else the above call
3568 * will always send that there is data pending.
3570 * The consumer assumes that when the data pending command is received,
3571 * the trace has been started before or else no output data is written
3572 * by the streams which is a condition for data pending. So, this is
3573 * *VERY* important that we don't ask the consumer before a start
3576 if (!session
->has_been_started
) {
3582 /* A rotation is still pending, we have to wait. */
3583 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3584 DBG("Rotate still pending for session %s", session
->name
);
3589 if (ksess
&& ksess
->consumer
) {
3590 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3592 /* Data is still being extracted for the kernel. */
3597 if (usess
&& usess
->consumer
) {
3598 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3600 /* Data is still being extracted for the kernel. */
3605 /* Data is ready to be read by a viewer */
3613 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3615 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3617 int cmd_snapshot_add_output(struct ltt_session
*session
,
3618 struct lttng_snapshot_output
*output
, uint32_t *id
)
3621 struct snapshot_output
*new_output
;
3626 DBG("Cmd snapshot add output for session %s", session
->name
);
3629 * Can't create an output if the session is not set in no-output mode.
3631 if (session
->output_traces
) {
3632 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3636 /* Only one output is allowed until we have the "tee" feature. */
3637 if (session
->snapshot
.nb_output
== 1) {
3638 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3642 new_output
= snapshot_output_alloc();
3644 ret
= LTTNG_ERR_NOMEM
;
3648 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3649 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3650 &session
->snapshot
);
3652 if (ret
== -ENOMEM
) {
3653 ret
= LTTNG_ERR_NOMEM
;
3655 ret
= LTTNG_ERR_INVALID
;
3661 snapshot_add_output(&session
->snapshot
, new_output
);
3663 *id
= new_output
->id
;
3670 snapshot_output_destroy(new_output
);
3676 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3678 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3680 int cmd_snapshot_del_output(struct ltt_session
*session
,
3681 struct lttng_snapshot_output
*output
)
3684 struct snapshot_output
*sout
= NULL
;
3692 * Permission denied to create an output if the session is not
3693 * set in no output mode.
3695 if (session
->output_traces
) {
3696 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3701 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3703 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3704 } else if (*output
->name
!= '\0') {
3705 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3707 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3710 ret
= LTTNG_ERR_INVALID
;
3714 snapshot_delete_output(&session
->snapshot
, sout
);
3715 snapshot_output_destroy(sout
);
3724 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3726 * If no output is available, outputs is untouched and 0 is returned.
3728 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3730 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3731 struct lttng_snapshot_output
**outputs
)
3734 struct lttng_snapshot_output
*list
= NULL
;
3735 struct lttng_ht_iter iter
;
3736 struct snapshot_output
*output
;
3741 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3744 * Permission denied to create an output if the session is not
3745 * set in no output mode.
3747 if (session
->output_traces
) {
3748 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3752 if (session
->snapshot
.nb_output
== 0) {
3757 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3759 ret
= -LTTNG_ERR_NOMEM
;
3763 /* Copy list from session to the new list object. */
3765 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3766 output
, node
.node
) {
3767 assert(output
->consumer
);
3768 list
[idx
].id
= output
->id
;
3769 list
[idx
].max_size
= output
->max_size
;
3770 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3771 sizeof(list
[idx
].name
))) {
3772 ret
= -LTTNG_ERR_INVALID
;
3775 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3776 if (lttng_strncpy(list
[idx
].ctrl_url
,
3777 output
->consumer
->dst
.session_root_path
,
3778 sizeof(list
[idx
].ctrl_url
))) {
3779 ret
= -LTTNG_ERR_INVALID
;
3784 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3785 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3787 ret
= -LTTNG_ERR_NOMEM
;
3792 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3793 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3795 ret
= -LTTNG_ERR_NOMEM
;
3804 ret
= session
->snapshot
.nb_output
;
3813 * Check if we can regenerate the metadata for this session.
3814 * Only kernel, UST per-uid and non-live sessions are supported.
3816 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3819 int check_regenerate_metadata_support(struct ltt_session
*session
)
3825 if (session
->live_timer
!= 0) {
3826 ret
= LTTNG_ERR_LIVE_SESSION
;
3829 if (!session
->active
) {
3830 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3833 if (session
->ust_session
) {
3834 switch (session
->ust_session
->buffer_type
) {
3835 case LTTNG_BUFFER_PER_UID
:
3837 case LTTNG_BUFFER_PER_PID
:
3838 ret
= LTTNG_ERR_PER_PID_SESSION
;
3842 ret
= LTTNG_ERR_UNK
;
3846 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3847 session
->consumer
->relay_minor_version
< 8) {
3848 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3858 int clear_metadata_file(int fd
)
3863 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
3864 if (lseek_ret
< 0) {
3870 ret
= ftruncate(fd
, 0);
3872 PERROR("ftruncate");
3881 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3884 struct buffer_reg_uid
*uid_reg
= NULL
;
3885 struct buffer_reg_session
*session_reg
= NULL
;
3888 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3889 struct ust_registry_session
*registry
;
3890 struct ust_registry_channel
*chan
;
3891 struct lttng_ht_iter iter_chan
;
3893 session_reg
= uid_reg
->registry
;
3894 registry
= session_reg
->reg
.ust
;
3896 pthread_mutex_lock(®istry
->lock
);
3897 registry
->metadata_len_sent
= 0;
3898 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3899 registry
->metadata_len
= 0;
3900 registry
->metadata_version
++;
3901 if (registry
->metadata_fd
> 0) {
3902 /* Clear the metadata file's content. */
3903 ret
= clear_metadata_file(registry
->metadata_fd
);
3905 pthread_mutex_unlock(®istry
->lock
);
3910 ret
= ust_metadata_session_statedump(registry
, NULL
,
3911 registry
->major
, registry
->minor
);
3913 pthread_mutex_unlock(®istry
->lock
);
3914 ERR("Failed to generate session metadata (err = %d)",
3918 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3920 struct ust_registry_event
*event
;
3921 struct lttng_ht_iter iter_event
;
3923 ret
= ust_metadata_channel_statedump(registry
, chan
);
3925 pthread_mutex_unlock(®istry
->lock
);
3926 ERR("Failed to generate channel metadata "
3930 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3932 ret
= ust_metadata_event_statedump(registry
,
3935 pthread_mutex_unlock(®istry
->lock
);
3936 ERR("Failed to generate event metadata "
3942 pthread_mutex_unlock(®istry
->lock
);
3951 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3953 * Ask the consumer to truncate the existing metadata file(s) and
3954 * then regenerate the metadata. Live and per-pid sessions are not
3955 * supported and return an error.
3957 * Return 0 on success or else a LTTNG_ERR code.
3959 int cmd_regenerate_metadata(struct ltt_session
*session
)
3965 ret
= check_regenerate_metadata_support(session
);
3970 if (session
->kernel_session
) {
3971 ret
= kernctl_session_regenerate_metadata(
3972 session
->kernel_session
->fd
);
3974 ERR("Failed to regenerate the kernel metadata");
3979 if (session
->ust_session
) {
3980 ret
= ust_regenerate_metadata(session
->ust_session
);
3982 ERR("Failed to regenerate the UST metadata");
3986 DBG("Cmd metadata regenerate for session %s", session
->name
);
3994 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3996 * Ask the tracer to regenerate a new statedump.
3998 * Return 0 on success or else a LTTNG_ERR code.
4000 int cmd_regenerate_statedump(struct ltt_session
*session
)
4006 if (!session
->active
) {
4007 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4011 if (session
->kernel_session
) {
4012 ret
= kernctl_session_regenerate_statedump(
4013 session
->kernel_session
->fd
);
4015 * Currently, the statedump in kernel can only fail if out
4019 if (ret
== -ENOMEM
) {
4020 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4022 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4024 ERR("Failed to regenerate the kernel statedump");
4029 if (session
->ust_session
) {
4030 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4032 * Currently, the statedump in UST always returns 0.
4035 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4036 ERR("Failed to regenerate the UST statedump");
4040 DBG("Cmd regenerate statedump for session %s", session
->name
);
4047 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4048 struct notification_thread_handle
*notification_thread
)
4052 ssize_t sock_recv_len
;
4053 struct lttng_trigger
*trigger
= NULL
;
4054 struct lttng_buffer_view view
;
4055 struct lttng_dynamic_buffer trigger_buffer
;
4057 lttng_dynamic_buffer_init(&trigger_buffer
);
4058 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4059 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4061 ret
= LTTNG_ERR_NOMEM
;
4065 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4067 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4068 ERR("Failed to receive \"register trigger\" command payload");
4069 /* TODO: should this be a new error enum ? */
4070 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4074 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4075 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4077 ERR("Invalid trigger payload received in \"register trigger\" command");
4078 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4082 ret
= notification_thread_command_register_trigger(notification_thread
,
4084 /* Ownership of trigger was transferred. */
4087 lttng_trigger_destroy(trigger
);
4088 lttng_dynamic_buffer_reset(&trigger_buffer
);
4092 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4093 struct notification_thread_handle
*notification_thread
)
4097 ssize_t sock_recv_len
;
4098 struct lttng_trigger
*trigger
= NULL
;
4099 struct lttng_buffer_view view
;
4100 struct lttng_dynamic_buffer trigger_buffer
;
4102 lttng_dynamic_buffer_init(&trigger_buffer
);
4103 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4104 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4106 ret
= LTTNG_ERR_NOMEM
;
4110 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4112 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4113 ERR("Failed to receive \"unregister trigger\" command payload");
4114 /* TODO: should this be a new error enum ? */
4115 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4119 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4120 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4122 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4123 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4127 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4130 lttng_trigger_destroy(trigger
);
4131 lttng_dynamic_buffer_reset(&trigger_buffer
);
4136 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4137 * snapshot output is *not* set with a remote destination.
4139 * Return 0 on success or a LTTNG_ERR code.
4141 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
4142 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
4145 struct lttng_ht_iter iter
;
4146 struct consumer_socket
*socket
;
4149 assert(snap_output
);
4152 DBG2("Set relayd object from snapshot output");
4154 /* Ignore if snapshot consumer output is not network. */
4155 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
4160 * For each consumer socket, create and send the relayd object of the
4164 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
4165 socket
, node
.node
) {
4166 pthread_mutex_lock(socket
->lock
);
4167 ret
= send_consumer_relayd_sockets(0, session
->id
,
4168 snap_output
->consumer
, socket
,
4169 session
->name
, session
->hostname
,
4170 session
->live_timer
);
4171 pthread_mutex_unlock(socket
->lock
);
4172 if (ret
!= LTTNG_OK
) {
4184 * Record a kernel snapshot.
4186 * Return LTTNG_OK on success or a LTTNG_ERR code.
4188 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
4189 struct snapshot_output
*output
, struct ltt_session
*session
,
4190 int wait
, uint64_t nb_packets_per_stream
)
4200 * Copy kernel session sockets so we can communicate with the right
4201 * consumer for the snapshot record command.
4203 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
4205 ret
= LTTNG_ERR_NOMEM
;
4209 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
4210 if (ret
!= LTTNG_OK
) {
4211 goto error_snapshot
;
4214 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
4215 if (ret
!= LTTNG_OK
) {
4216 goto error_snapshot
;
4223 /* Clean up copied sockets so this output can use some other later on. */
4224 consumer_destroy_output_sockets(output
->consumer
);
4231 * Record a UST snapshot.
4233 * Return 0 on success or a LTTNG_ERR error code.
4235 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
4236 struct snapshot_output
*output
, struct ltt_session
*session
,
4237 int wait
, uint64_t nb_packets_per_stream
)
4246 * Copy UST session sockets so we can communicate with the right
4247 * consumer for the snapshot record command.
4249 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
4251 ret
= LTTNG_ERR_NOMEM
;
4255 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
4256 if (ret
!= LTTNG_OK
) {
4257 goto error_snapshot
;
4260 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
4264 ret
= LTTNG_ERR_INVALID
;
4267 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4270 goto error_snapshot
;
4276 /* Clean up copied sockets so this output can use some other later on. */
4277 consumer_destroy_output_sockets(output
->consumer
);
4283 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
4284 uint64_t cur_nr_packets
)
4286 uint64_t tot_size
= 0;
4288 if (session
->kernel_session
) {
4289 struct ltt_kernel_channel
*chan
;
4290 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4292 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4293 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4295 * Don't take channel into account if we
4296 * already grab all its packets.
4300 tot_size
+= chan
->channel
->attr
.subbuf_size
4301 * chan
->stream_count
;
4305 if (session
->ust_session
) {
4306 struct ltt_ust_session
*usess
= session
->ust_session
;
4308 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4316 * Calculate the number of packets we can grab from each stream that
4317 * fits within the overall snapshot max size.
4319 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4320 * the number of packets per stream.
4322 * TODO: this approach is not perfect: we consider the worse case
4323 * (packet filling the sub-buffers) as an upper bound, but we could do
4324 * better if we do this calculation while we actually grab the packet
4325 * content: we would know how much padding we don't actually store into
4328 * This algorithm is currently bounded by the number of packets per
4331 * Since we call this algorithm before actually grabbing the data, it's
4332 * an approximation: for instance, applications could appear/disappear
4333 * in between this call and actually grabbing data.
4336 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
4339 uint64_t cur_nb_packets
= 0;
4342 return 0; /* Infinite */
4345 size_left
= max_size
;
4347 uint64_t one_more_packet_tot_size
;
4349 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
4351 if (!one_more_packet_tot_size
) {
4352 /* We are already grabbing all packets. */
4355 size_left
-= one_more_packet_tot_size
;
4356 if (size_left
< 0) {
4361 if (!cur_nb_packets
) {
4362 /* Not enough room to grab one packet of each stream, error. */
4365 return cur_nb_packets
;
4369 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4371 * The wait parameter is ignored so this call always wait for the snapshot to
4372 * complete before returning.
4374 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4376 int cmd_snapshot_record(struct ltt_session
*session
,
4377 struct lttng_snapshot_output
*output
, int wait
)
4380 unsigned int use_tmp_output
= 0;
4381 struct snapshot_output tmp_output
;
4382 unsigned int snapshot_success
= 0;
4388 DBG("Cmd snapshot record for session %s", session
->name
);
4390 /* Get the datetime for the snapshot output directory. */
4391 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4394 ret
= LTTNG_ERR_INVALID
;
4399 * Permission denied to create an output if the session is not
4400 * set in no output mode.
4402 if (session
->output_traces
) {
4403 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4407 /* The session needs to be started at least once. */
4408 if (!session
->has_been_started
) {
4409 ret
= LTTNG_ERR_START_SESSION_ONCE
;
4413 /* Use temporary output for the session. */
4414 if (*output
->ctrl_url
!= '\0') {
4415 ret
= snapshot_output_init(output
->max_size
, output
->name
,
4416 output
->ctrl_url
, output
->data_url
, session
->consumer
,
4419 if (ret
== -ENOMEM
) {
4420 ret
= LTTNG_ERR_NOMEM
;
4422 ret
= LTTNG_ERR_INVALID
;
4426 /* Use the global session count for the temporary snapshot. */
4427 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4429 /* Use the global datetime */
4430 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4434 if (use_tmp_output
) {
4435 int64_t nb_packets_per_stream
;
4437 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4438 tmp_output
.max_size
);
4439 if (nb_packets_per_stream
< 0) {
4440 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4444 if (session
->kernel_session
) {
4445 ret
= record_kernel_snapshot(session
->kernel_session
,
4446 &tmp_output
, session
,
4447 wait
, nb_packets_per_stream
);
4448 if (ret
!= LTTNG_OK
) {
4453 if (session
->ust_session
) {
4454 ret
= record_ust_snapshot(session
->ust_session
,
4455 &tmp_output
, session
,
4456 wait
, nb_packets_per_stream
);
4457 if (ret
!= LTTNG_OK
) {
4462 snapshot_success
= 1;
4464 struct snapshot_output
*sout
;
4465 struct lttng_ht_iter iter
;
4468 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4469 &iter
.iter
, sout
, node
.node
) {
4470 int64_t nb_packets_per_stream
;
4473 * Make a local copy of the output and assign the possible
4474 * temporary value given by the caller.
4476 memset(&tmp_output
, 0, sizeof(tmp_output
));
4477 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
4479 if (output
->max_size
!= (uint64_t) -1ULL) {
4480 tmp_output
.max_size
= output
->max_size
;
4483 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4484 tmp_output
.max_size
);
4485 if (nb_packets_per_stream
< 0) {
4486 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4491 /* Use temporary name. */
4492 if (*output
->name
!= '\0') {
4493 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4494 sizeof(tmp_output
.name
))) {
4495 ret
= LTTNG_ERR_INVALID
;
4501 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4502 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4504 if (session
->kernel_session
) {
4505 ret
= record_kernel_snapshot(session
->kernel_session
,
4506 &tmp_output
, session
,
4507 wait
, nb_packets_per_stream
);
4508 if (ret
!= LTTNG_OK
) {
4514 if (session
->ust_session
) {
4515 ret
= record_ust_snapshot(session
->ust_session
,
4516 &tmp_output
, session
,
4517 wait
, nb_packets_per_stream
);
4518 if (ret
!= LTTNG_OK
) {
4523 snapshot_success
= 1;
4528 if (snapshot_success
) {
4529 session
->snapshot
.nb_snapshot
++;
4531 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4539 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4541 int cmd_set_session_shm_path(struct ltt_session
*session
,
4542 const char *shm_path
)
4548 * Can only set shm path before session is started.
4550 if (session
->has_been_started
) {
4551 return LTTNG_ERR_SESSION_STARTED
;
4554 strncpy(session
->shm_path
, shm_path
,
4555 sizeof(session
->shm_path
));
4556 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4562 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4564 * Ask the consumer to rotate the session output directory.
4565 * The session lock must be held.
4567 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4569 int cmd_rotate_session(struct ltt_session
*session
,
4570 struct lttng_rotate_session_return
*rotate_return
)
4574 struct tm
*timeinfo
;
4580 if (!session
->has_been_started
) {
4581 ret
= -LTTNG_ERR_START_SESSION_ONCE
;
4585 if (session
->live_timer
|| session
->snapshot_mode
||
4586 !session
->output_traces
) {
4587 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4592 * Unsupported feature in lttng-relayd before 2.11.
4594 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4595 (session
->consumer
->relay_major_version
== 2 &&
4596 session
->consumer
->relay_minor_version
< 11)) {
4597 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
4601 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4602 ret
= -LTTNG_ERR_ROTATION_PENDING
;
4603 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
4609 * After a stop, we only allow one rotation to occur, the other ones are
4610 * useless until a new start.
4612 if (session
->rotated_after_last_stop
) {
4613 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4615 ret
= -LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
4619 /* Special case for the first rotation. */
4620 if (session
->current_archive_id
== 0) {
4621 const char *base_path
= NULL
;
4623 assert(session
->kernel_session
|| session
->ust_session
);
4624 /* Either one of the two sessions is enough to get the root path. */
4625 base_path
= session_get_base_path(session
);
4628 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4630 sizeof(session
->rotation_chunk
.current_rotate_path
));
4632 ERR("Failed to copy session base path to current rotation chunk path");
4633 ret
= -LTTNG_ERR_UNK
;
4638 * The currently active tracing path is now the folder we
4641 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4642 session
->rotation_chunk
.active_tracing_path
,
4643 sizeof(session
->rotation_chunk
.current_rotate_path
));
4645 ERR("Failed to copy the active tracing path to the current rotate path");
4646 ret
= -LTTNG_ERR_UNK
;
4650 DBG("Current rotate path %s", session
->rotation_chunk
.current_rotate_path
);
4653 * Channels created after this point will belong to the next
4656 session
->current_archive_id
++;
4658 * A rotation has a local step even if the destination is a relay
4659 * daemon; the buffers must be consumed by the consumer daemon.
4661 session
->rotation_pending_local
= true;
4662 session
->rotation_pending_relay
=
4663 session_get_consumer_destination_type(session
) == CONSUMER_DST_NET
;
4664 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
4665 ret
= notification_thread_command_session_rotation_ongoing(
4666 notification_thread_handle
,
4667 session
->name
, session
->uid
, session
->gid
,
4668 session
->current_archive_id
- 1);
4669 if (ret
!= LTTNG_OK
) {
4670 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
4674 /* Create the path name for the next chunk. */
4676 if (now
== (time_t) -1) {
4677 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4680 session
->last_chunk_start_ts
= session
->current_chunk_start_ts
;
4681 session
->current_chunk_start_ts
= now
;
4683 timeinfo
= localtime(&now
);
4685 PERROR("Failed to sample local time in rotate session command");
4686 ret
= -LTTNG_ERR_UNK
;
4689 strf_ret
= strftime(datetime
, sizeof(datetime
), "%Y%m%dT%H%M%S%z",
4692 ERR("Failed to format local time timestamp in rotate session command");
4693 ret
= -LTTNG_ERR_UNK
;
4696 if (session
->kernel_session
) {
4698 * The active path for the next rotation/destroy.
4699 * Ex: ~/lttng-traces/auto-20170922-111748/20170922-111754-42
4701 ret
= snprintf(session
->rotation_chunk
.active_tracing_path
,
4702 sizeof(session
->rotation_chunk
.active_tracing_path
),
4704 session_get_base_path(session
),
4705 datetime
, session
->current_archive_id
+ 1);
4706 if (ret
< 0 || ret
== sizeof(session
->rotation_chunk
.active_tracing_path
)) {
4707 ERR("Failed to format active kernel tracing path in rotate session command");
4708 ret
= -LTTNG_ERR_UNK
;
4712 * The sub-directory for the consumer
4713 * Ex: /20170922-111754-42/kernel
4715 ret
= snprintf(session
->kernel_session
->consumer
->chunk_path
,
4716 sizeof(session
->kernel_session
->consumer
->chunk_path
),
4717 "/%s-%" PRIu64
, datetime
,
4718 session
->current_archive_id
+ 1);
4719 if (ret
< 0 || ret
== sizeof(session
->kernel_session
->consumer
->chunk_path
)) {
4720 ERR("Failed to format the kernel consumer's sub-directory in rotate session command");
4721 ret
= -LTTNG_ERR_UNK
;
4725 * Create the new chunk folder, before the rotation begins so we don't
4726 * race with the consumer/tracer activity.
4728 ret
= domain_mkdir(session
->kernel_session
->consumer
, session
,
4729 session
->kernel_session
->uid
,
4730 session
->kernel_session
->gid
);
4732 ERR("Failed to create kernel session tracing path at %s",
4733 session
->kernel_session
->consumer
->chunk_path
);
4734 ret
= -LTTNG_ERR_CREATE_DIR_FAIL
;
4737 ret
= kernel_rotate_session(session
);
4738 if (ret
!= LTTNG_OK
) {
4743 if (session
->ust_session
) {
4744 ret
= snprintf(session
->rotation_chunk
.active_tracing_path
,
4745 PATH_MAX
, "%s/%s-%" PRIu64
,
4746 session_get_base_path(session
),
4747 datetime
, session
->current_archive_id
+ 1);
4749 ERR("Failed to format active UST tracing path in rotate session command");
4750 ret
= -LTTNG_ERR_UNK
;
4753 ret
= snprintf(session
->ust_session
->consumer
->chunk_path
,
4754 PATH_MAX
, "/%s-%" PRIu64
, datetime
,
4755 session
->current_archive_id
+ 1);
4757 ERR("Failed to format the UST consumer's sub-directory in rotate session command");
4758 ret
= -LTTNG_ERR_UNK
;
4762 * Create the new chunk folder, before the rotation begins so we don't
4763 * race with the consumer/tracer activity.
4765 ret
= domain_mkdir(session
->ust_session
->consumer
, session
,
4766 session
->ust_session
->uid
,
4767 session
->ust_session
->gid
);
4769 ret
= -LTTNG_ERR_CREATE_DIR_FAIL
;
4772 ret
= ust_app_rotate_session(session
);
4773 if (ret
!= LTTNG_OK
) {
4778 ret
= timer_session_rotation_pending_check_start(session
,
4779 DEFAULT_ROTATE_PENDING_TIMER
);
4784 if (!session
->active
) {
4785 session
->rotated_after_last_stop
= true;
4788 if (rotate_return
) {
4789 rotate_return
->rotation_id
= session
->current_archive_id
;
4792 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
4793 session
->name
, session
->current_archive_id
- 1);
4801 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
4803 * Check if the session has finished its rotation.
4805 * Return 0 on success or else a LTTNG_ERR code.
4807 int cmd_rotate_get_info(struct ltt_session
*session
,
4808 struct lttng_rotation_get_info_return
*info_return
,
4809 uint64_t rotation_id
)
4815 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
4816 session
->current_archive_id
);
4818 if (session
->current_archive_id
!= rotation_id
) {
4819 info_return
->status
= (int32_t) LTTNG_ROTATION_STATE_EXPIRED
;
4824 switch (session
->rotation_state
) {
4825 case LTTNG_ROTATION_STATE_ONGOING
:
4826 DBG("Reporting that rotation id %" PRIu64
" of session %s is still pending",
4827 rotation_id
, session
->name
);
4829 case LTTNG_ROTATION_STATE_COMPLETED
:
4831 char *current_tracing_path_reply
;
4832 size_t current_tracing_path_reply_len
;
4834 switch (session_get_consumer_destination_type(session
)) {
4835 case CONSUMER_DST_LOCAL
:
4836 current_tracing_path_reply
=
4837 info_return
->location
.local
.absolute_path
;
4838 current_tracing_path_reply_len
=
4839 sizeof(info_return
->location
.local
.absolute_path
);
4840 info_return
->location_type
=
4841 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
4843 case CONSUMER_DST_NET
:
4844 current_tracing_path_reply
=
4845 info_return
->location
.relay
.relative_path
;
4846 current_tracing_path_reply_len
=
4847 sizeof(info_return
->location
.relay
.relative_path
);
4848 /* Currently the only supported relay protocol. */
4849 info_return
->location
.relay
.protocol
=
4850 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
4852 ret
= lttng_strncpy(info_return
->location
.relay
.host
,
4853 session_get_net_consumer_hostname(session
),
4854 sizeof(info_return
->location
.relay
.host
));
4856 ERR("Failed to host name to rotate_get_info reply");
4857 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4858 ret
= -LTTNG_ERR_UNK
;
4862 session_get_net_consumer_ports(session
,
4863 &info_return
->location
.relay
.ports
.control
,
4864 &info_return
->location
.relay
.ports
.data
);
4865 info_return
->location_type
=
4866 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
4871 ret
= lttng_strncpy(current_tracing_path_reply
,
4872 session
->rotation_chunk
.current_rotate_path
,
4873 current_tracing_path_reply_len
);
4875 ERR("Failed to copy current tracing path to rotate_get_info reply");
4876 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4877 ret
= -LTTNG_ERR_UNK
;
4883 case LTTNG_ROTATION_STATE_ERROR
:
4884 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session %s",
4885 rotation_id
, session
->name
);
4891 info_return
->status
= (int32_t) session
->rotation_state
;
4898 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
4900 * Configure the automatic rotation parameters.
4901 * 'activate' to true means activate the rotation schedule type with 'new_value'.
4902 * 'activate' to false means deactivate the rotation schedule and validate that
4903 * 'new_value' has the same value as the currently active value.
4905 * Return 0 on success or else a positive LTTNG_ERR code.
4907 int cmd_rotation_set_schedule(struct ltt_session
*session
,
4908 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
4910 struct notification_thread_handle
*notification_thread_handle
)
4913 uint64_t *parameter_value
;
4917 DBG("Cmd rotate set schedule session %s", session
->name
);
4919 if (session
->live_timer
|| session
->snapshot_mode
||
4920 !session
->output_traces
) {
4921 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
4922 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4926 switch (schedule_type
) {
4927 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
4928 parameter_value
= &session
->rotate_size
;
4930 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
4931 parameter_value
= &session
->rotate_timer_period
;
4932 if (new_value
>= UINT_MAX
) {
4933 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
4934 new_value
, UINT_MAX
);
4935 ret
= LTTNG_ERR_INVALID
;
4940 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
4941 ret
= LTTNG_ERR_INVALID
;
4945 /* Improper use of the API. */
4946 if (new_value
== -1ULL) {
4947 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
4948 ret
= LTTNG_ERR_INVALID
;
4953 * As indicated in struct ltt_session's comments, a value of == 0 means
4954 * this schedule rotation type is not in use.
4956 * Reject the command if we were asked to activate a schedule that was
4959 if (activate
&& *parameter_value
!= 0) {
4960 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
4961 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
4966 * Reject the command if we were asked to deactivate a schedule that was
4969 if (!activate
&& *parameter_value
== 0) {
4970 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
4971 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
4976 * Reject the command if we were asked to deactivate a schedule that
4979 if (!activate
&& *parameter_value
!= new_value
) {
4980 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
4981 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
4985 *parameter_value
= activate
? new_value
: 0;
4987 switch (schedule_type
) {
4988 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
4989 if (activate
&& session
->active
) {
4991 * Only start the timer if the session is active,
4992 * otherwise it will be started when the session starts.
4994 ret
= timer_session_rotation_schedule_timer_start(
4995 session
, new_value
);
4997 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
4998 ret
= LTTNG_ERR_UNK
;
5002 ret
= timer_session_rotation_schedule_timer_stop(
5005 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5006 ret
= LTTNG_ERR_UNK
;
5011 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5013 ret
= subscribe_session_consumed_size_rotation(session
,
5014 new_value
, notification_thread_handle
);
5016 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5017 ret
= LTTNG_ERR_UNK
;
5021 ret
= unsubscribe_session_consumed_size_rotation(session
,
5022 notification_thread_handle
);
5024 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5025 ret
= LTTNG_ERR_UNK
;
5032 /* Would have been caught before. */
5044 /* Wait for a given path to be removed before continuing. */
5045 static enum lttng_error_code
wait_on_path(void *path_data
)
5047 const char *shm_path
= path_data
;
5049 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5055 ret
= stat(shm_path
, &st
);
5057 if (errno
!= ENOENT
) {
5058 PERROR("stat() returned an error while checking for the existence of the shm path");
5060 DBG("shm path no longer exists, completing the destruction of session");
5064 if (!S_ISDIR(st
.st_mode
)) {
5065 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5070 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5076 * Returns a pointer to a handler to run on completion of a command.
5077 * Returns NULL if no handler has to be run for the last command executed.
5079 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5081 struct cmd_completion_handler
*handler
= current_completion_handler
;
5083 current_completion_handler
= NULL
;
5088 * Init command subsystem.
5093 * Set network sequence index to 1 for streams to match a relayd
5094 * socket on the consumer side.
5096 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5097 relayd_net_seq_idx
= 1;
5098 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5100 DBG("Command subsystem initialized");