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 <common/trace-chunk.h>
36 #include <lttng/trigger/trigger-internal.h>
37 #include <lttng/condition/condition.h>
38 #include <lttng/action/action.h>
39 #include <lttng/channel.h>
40 #include <lttng/channel-internal.h>
41 #include <lttng/rotate-internal.h>
42 #include <lttng/location-internal.h>
43 #include <lttng/session-internal.h>
44 #include <lttng/userspace-probe-internal.h>
45 #include <lttng/session-descriptor-internal.h>
46 #include <common/string-utils/string-utils.h>
51 #include "health-sessiond.h"
53 #include "kernel-consumer.h"
54 #include "lttng-sessiond.h"
56 #include "lttng-syscall.h"
58 #include "buffer-registry.h"
59 #include "notification-thread.h"
60 #include "notification-thread-commands.h"
62 #include "rotation-thread.h"
64 #include "agent-thread.h"
68 /* Sleep for 100ms between each check for the shm path's deletion. */
69 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
71 static enum lttng_error_code
wait_on_path(void *path
);
74 * Command completion handler that is used by the destroy command
75 * when a session that has a non-default shm_path is being destroyed.
77 * See comment in cmd_destroy_session() for the rationale.
79 static struct destroy_completion_handler
{
80 struct cmd_completion_handler handler
;
81 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
82 } destroy_completion_handler
= {
85 .data
= destroy_completion_handler
.shm_path
90 static struct cmd_completion_handler
*current_completion_handler
;
93 * Used to keep a unique index for each relayd socket created where this value
94 * is associated with streams on the consumer so it can match the right relayd
95 * to send to. It must be accessed with the relayd_net_seq_idx_lock
98 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
99 static uint64_t relayd_net_seq_idx
;
101 static int validate_ust_event_name(const char *);
102 static int cmd_enable_event_internal(struct ltt_session
*session
,
103 struct lttng_domain
*domain
,
104 char *channel_name
, struct lttng_event
*event
,
105 char *filter_expression
,
106 struct lttng_filter_bytecode
*filter
,
107 struct lttng_event_exclusion
*exclusion
,
111 * Create a session path used by list_lttng_sessions for the case that the
112 * session consumer is on the network.
114 static int build_network_session_path(char *dst
, size_t size
,
115 struct ltt_session
*session
)
117 int ret
, kdata_port
, udata_port
;
118 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
119 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
124 memset(tmp_urls
, 0, sizeof(tmp_urls
));
125 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
127 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
129 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
130 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
131 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
134 if (session
->ust_session
&& session
->ust_session
->consumer
) {
135 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
136 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
139 if (uuri
== NULL
&& kuri
== NULL
) {
140 uri
= &session
->consumer
->dst
.net
.control
;
141 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
142 } else if (kuri
&& uuri
) {
143 ret
= uri_compare(kuri
, uuri
);
147 /* Build uuri URL string */
148 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
155 } else if (kuri
&& uuri
== NULL
) {
157 } else if (uuri
&& kuri
== NULL
) {
161 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
167 * Do we have a UST url set. If yes, this means we have both kernel and UST
170 if (*tmp_uurl
!= '\0') {
171 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
172 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
175 if (kuri
|| (!kuri
&& !uuri
)) {
178 /* No kernel URI, use the UST port. */
181 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
189 * Get run-time attributes if the session has been started (discarded events,
192 static int get_kernel_runtime_stats(struct ltt_session
*session
,
193 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
194 uint64_t *lost_packets
)
198 if (!session
->has_been_started
) {
200 *discarded_events
= 0;
205 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
206 session
->kernel_session
->consumer
,
212 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
213 session
->kernel_session
->consumer
,
224 * Get run-time attributes if the session has been started (discarded events,
227 static int get_ust_runtime_stats(struct ltt_session
*session
,
228 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
229 uint64_t *lost_packets
)
232 struct ltt_ust_session
*usess
;
234 if (!discarded_events
|| !lost_packets
) {
239 usess
= session
->ust_session
;
240 assert(discarded_events
);
241 assert(lost_packets
);
243 if (!usess
|| !session
->has_been_started
) {
244 *discarded_events
= 0;
250 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
251 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
252 &usess
->buffer_reg_uid_list
,
253 usess
->consumer
, uchan
->id
,
254 uchan
->attr
.overwrite
,
257 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
258 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
259 uchan
, usess
->consumer
,
260 uchan
->attr
.overwrite
,
266 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
267 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
269 ERR("Unsupported buffer type");
280 * Fill lttng_channel array of all channels.
282 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
283 struct ltt_session
*session
, struct lttng_channel
*channels
,
284 struct lttng_channel_extended
*chan_exts
)
287 struct ltt_kernel_channel
*kchan
;
289 DBG("Listing channels for session %s", session
->name
);
292 case LTTNG_DOMAIN_KERNEL
:
293 /* Kernel channels */
294 if (session
->kernel_session
!= NULL
) {
295 cds_list_for_each_entry(kchan
,
296 &session
->kernel_session
->channel_list
.head
, list
) {
297 uint64_t discarded_events
, lost_packets
;
298 struct lttng_channel_extended
*extended
;
300 extended
= (struct lttng_channel_extended
*)
301 kchan
->channel
->attr
.extended
.ptr
;
303 ret
= get_kernel_runtime_stats(session
, kchan
,
304 &discarded_events
, &lost_packets
);
308 /* Copy lttng_channel struct to array */
309 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
310 channels
[i
].enabled
= kchan
->enabled
;
311 chan_exts
[i
].discarded_events
=
313 chan_exts
[i
].lost_packets
= lost_packets
;
314 chan_exts
[i
].monitor_timer_interval
=
315 extended
->monitor_timer_interval
;
316 chan_exts
[i
].blocking_timeout
= 0;
321 case LTTNG_DOMAIN_UST
:
323 struct lttng_ht_iter iter
;
324 struct ltt_ust_channel
*uchan
;
327 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
328 &iter
.iter
, uchan
, node
.node
) {
329 uint64_t discarded_events
= 0, lost_packets
= 0;
331 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
332 LTTNG_SYMBOL_NAME_LEN
)) {
335 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
336 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
337 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
338 channels
[i
].attr
.switch_timer_interval
=
339 uchan
->attr
.switch_timer_interval
;
340 channels
[i
].attr
.read_timer_interval
=
341 uchan
->attr
.read_timer_interval
;
342 channels
[i
].enabled
= uchan
->enabled
;
343 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
344 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
347 * Map enum lttng_ust_output to enum lttng_event_output.
349 switch (uchan
->attr
.output
) {
351 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
355 * LTTNG_UST_MMAP is the only supported UST
362 chan_exts
[i
].monitor_timer_interval
=
363 uchan
->monitor_timer_interval
;
364 chan_exts
[i
].blocking_timeout
=
365 uchan
->attr
.u
.s
.blocking_timeout
;
367 ret
= get_ust_runtime_stats(session
, uchan
,
368 &discarded_events
, &lost_packets
);
372 chan_exts
[i
].discarded_events
= discarded_events
;
373 chan_exts
[i
].lost_packets
= lost_packets
;
385 return -LTTNG_ERR_FATAL
;
391 static int increment_extended_len(const char *filter_expression
,
392 struct lttng_event_exclusion
*exclusion
,
393 const struct lttng_userspace_probe_location
*probe_location
,
394 size_t *extended_len
)
398 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
400 if (filter_expression
) {
401 *extended_len
+= strlen(filter_expression
) + 1;
405 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
408 if (probe_location
) {
409 ret
= lttng_userspace_probe_location_serialize(probe_location
,
414 *extended_len
+= ret
;
421 static int append_extended_info(const char *filter_expression
,
422 struct lttng_event_exclusion
*exclusion
,
423 struct lttng_userspace_probe_location
*probe_location
,
427 size_t filter_len
= 0;
428 size_t nb_exclusions
= 0;
429 size_t userspace_probe_location_len
= 0;
430 struct lttng_dynamic_buffer location_buffer
;
431 struct lttcomm_event_extended_header extended_header
;
433 if (filter_expression
) {
434 filter_len
= strlen(filter_expression
) + 1;
438 nb_exclusions
= exclusion
->count
;
441 if (probe_location
) {
442 lttng_dynamic_buffer_init(&location_buffer
);
443 ret
= lttng_userspace_probe_location_serialize(probe_location
,
444 &location_buffer
, NULL
);
449 userspace_probe_location_len
= location_buffer
.size
;
452 /* Set header fields */
453 extended_header
.filter_len
= filter_len
;
454 extended_header
.nb_exclusions
= nb_exclusions
;
455 extended_header
.userspace_probe_location_len
= userspace_probe_location_len
;
458 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
459 *extended_at
+= sizeof(extended_header
);
461 /* Copy filter string */
462 if (filter_expression
) {
463 memcpy(*extended_at
, filter_expression
, filter_len
);
464 *extended_at
+= filter_len
;
467 /* Copy exclusion names */
469 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
471 memcpy(*extended_at
, &exclusion
->names
, len
);
475 if (probe_location
) {
476 memcpy(*extended_at
, location_buffer
.data
, location_buffer
.size
);
477 *extended_at
+= location_buffer
.size
;
478 lttng_dynamic_buffer_reset(&location_buffer
);
486 * Create a list of agent domain events.
488 * Return number of events in list on success or else a negative value.
490 static int list_lttng_agent_events(struct agent
*agt
,
491 struct lttng_event
**events
, size_t *total_size
)
494 unsigned int nb_event
= 0;
495 struct agent_event
*event
;
496 struct lttng_event
*tmp_events
= NULL
;
497 struct lttng_ht_iter iter
;
498 size_t extended_len
= 0;
504 DBG3("Listing agent events");
507 nb_event
= lttng_ht_get_count(agt
->events
);
515 /* Compute required extended infos size */
516 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
519 * This is only valid because the commands which add events are
520 * processed in the same thread as the listing.
523 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
524 ret
= increment_extended_len(event
->filter_expression
, NULL
, NULL
,
527 DBG("Error computing the length of extended info message");
528 ret
= -LTTNG_ERR_FATAL
;
534 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
535 tmp_events
= zmalloc(*total_size
);
537 PERROR("zmalloc agent events session");
538 ret
= -LTTNG_ERR_FATAL
;
542 extended_at
= ((uint8_t *) tmp_events
) +
543 nb_event
* sizeof(struct lttng_event
);
546 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
547 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
548 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
549 tmp_events
[i
].enabled
= event
->enabled
;
550 tmp_events
[i
].loglevel
= event
->loglevel_value
;
551 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
554 /* Append extended info */
555 ret
= append_extended_info(event
->filter_expression
, NULL
, NULL
,
558 DBG("Error appending extended info message");
559 ret
= -LTTNG_ERR_FATAL
;
564 *events
= tmp_events
;
566 assert(nb_event
== i
);
577 * Create a list of ust global domain events.
579 static int list_lttng_ust_global_events(char *channel_name
,
580 struct ltt_ust_domain_global
*ust_global
,
581 struct lttng_event
**events
, size_t *total_size
)
584 unsigned int nb_event
= 0;
585 struct lttng_ht_iter iter
;
586 struct lttng_ht_node_str
*node
;
587 struct ltt_ust_channel
*uchan
;
588 struct ltt_ust_event
*uevent
;
589 struct lttng_event
*tmp
;
590 size_t extended_len
= 0;
593 DBG("Listing UST global events for channel %s", channel_name
);
597 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
598 node
= lttng_ht_iter_get_node_str(&iter
);
600 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
604 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
606 nb_event
= lttng_ht_get_count(uchan
->events
);
613 DBG3("Listing UST global %d events", nb_event
);
615 /* Compute required extended infos size */
616 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
617 if (uevent
->internal
) {
622 ret
= increment_extended_len(uevent
->filter_expression
,
623 uevent
->exclusion
, NULL
, &extended_len
);
625 DBG("Error computing the length of extended info message");
626 ret
= -LTTNG_ERR_FATAL
;
631 /* All events are internal, skip. */
637 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
638 tmp
= zmalloc(*total_size
);
640 ret
= -LTTNG_ERR_FATAL
;
644 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
646 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
647 if (uevent
->internal
) {
648 /* This event should remain hidden from clients */
651 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
652 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
653 tmp
[i
].enabled
= uevent
->enabled
;
655 switch (uevent
->attr
.instrumentation
) {
656 case LTTNG_UST_TRACEPOINT
:
657 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
659 case LTTNG_UST_PROBE
:
660 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
662 case LTTNG_UST_FUNCTION
:
663 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
667 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
668 switch (uevent
->attr
.loglevel_type
) {
669 case LTTNG_UST_LOGLEVEL_ALL
:
670 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
672 case LTTNG_UST_LOGLEVEL_RANGE
:
673 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
675 case LTTNG_UST_LOGLEVEL_SINGLE
:
676 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
679 if (uevent
->filter
) {
682 if (uevent
->exclusion
) {
683 tmp
[i
].exclusion
= 1;
687 /* Append extended info */
688 ret
= append_extended_info(uevent
->filter_expression
,
689 uevent
->exclusion
, NULL
, &extended_at
);
691 DBG("Error appending extended info message");
692 ret
= -LTTNG_ERR_FATAL
;
705 * Fill lttng_event array of all kernel events in the channel.
707 static int list_lttng_kernel_events(char *channel_name
,
708 struct ltt_kernel_session
*kernel_session
,
709 struct lttng_event
**events
, size_t *total_size
)
712 unsigned int nb_event
;
713 struct ltt_kernel_event
*event
;
714 struct ltt_kernel_channel
*kchan
;
715 size_t extended_len
= 0;
718 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
720 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
724 nb_event
= kchan
->event_count
;
726 DBG("Listing events for channel %s", kchan
->channel
->name
);
734 /* Compute required extended infos size */
735 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
736 ret
= increment_extended_len(event
->filter_expression
, NULL
,
737 event
->userspace_probe_location
,
740 DBG("Error computing the length of extended info message");
741 ret
= -LTTNG_ERR_FATAL
;
746 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
747 *events
= zmalloc(*total_size
);
748 if (*events
== NULL
) {
749 ret
= -LTTNG_ERR_FATAL
;
753 extended_at
= ((void *) *events
) +
754 nb_event
* sizeof(struct lttng_event
);
756 /* Kernel channels */
757 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
758 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
759 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
760 (*events
)[i
].enabled
= event
->enabled
;
761 (*events
)[i
].filter
=
762 (unsigned char) !!event
->filter_expression
;
764 switch (event
->event
->instrumentation
) {
765 case LTTNG_KERNEL_TRACEPOINT
:
766 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
768 case LTTNG_KERNEL_KRETPROBE
:
769 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
770 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
771 sizeof(struct lttng_kernel_kprobe
));
773 case LTTNG_KERNEL_KPROBE
:
774 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
775 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
776 sizeof(struct lttng_kernel_kprobe
));
778 case LTTNG_KERNEL_UPROBE
:
779 (*events
)[i
].type
= LTTNG_EVENT_USERSPACE_PROBE
;
781 case LTTNG_KERNEL_FUNCTION
:
782 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
783 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
784 sizeof(struct lttng_kernel_function
));
786 case LTTNG_KERNEL_NOOP
:
787 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
789 case LTTNG_KERNEL_SYSCALL
:
790 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
792 case LTTNG_KERNEL_ALL
:
800 /* Append extended info */
801 ret
= append_extended_info(event
->filter_expression
, NULL
,
802 event
->userspace_probe_location
, &extended_at
);
804 DBG("Error appending extended info message");
805 ret
= -LTTNG_ERR_FATAL
;
814 /* Negate the error code to differentiate the size from an error */
819 * Add URI so the consumer output object. Set the correct path depending on the
820 * domain adding the default trace directory.
822 static enum lttng_error_code
add_uri_to_consumer(
823 const struct ltt_session
*session
,
824 struct consumer_output
*consumer
,
825 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
828 enum lttng_error_code ret_code
= LTTNG_OK
;
832 if (consumer
== NULL
) {
833 DBG("No consumer detected. Don't add URI. Stopping.");
834 ret_code
= LTTNG_ERR_NO_CONSUMER
;
839 case LTTNG_DOMAIN_KERNEL
:
840 ret
= lttng_strncpy(consumer
->domain_subdir
,
841 DEFAULT_KERNEL_TRACE_DIR
,
842 sizeof(consumer
->domain_subdir
));
844 case LTTNG_DOMAIN_UST
:
845 ret
= lttng_strncpy(consumer
->domain_subdir
,
846 DEFAULT_UST_TRACE_DIR
,
847 sizeof(consumer
->domain_subdir
));
851 * This case is possible is we try to add the URI to the global
852 * tracing session consumer object which in this case there is
855 memset(consumer
->domain_subdir
, 0,
856 sizeof(consumer
->domain_subdir
));
860 ERR("Failed to initialize consumer output domain subdirectory");
861 ret_code
= LTTNG_ERR_FATAL
;
865 switch (uri
->dtype
) {
868 DBG2("Setting network URI to consumer");
870 if (consumer
->type
== CONSUMER_DST_NET
) {
871 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
872 consumer
->dst
.net
.control_isset
) ||
873 (uri
->stype
== LTTNG_STREAM_DATA
&&
874 consumer
->dst
.net
.data_isset
)) {
875 ret_code
= LTTNG_ERR_URL_EXIST
;
879 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
882 /* Set URI into consumer output object */
883 ret
= consumer_set_network_uri(session
, consumer
, uri
);
887 } else if (ret
== 1) {
889 * URI was the same in the consumer so we do not append the subdir
890 * again so to not duplicate output dir.
897 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
898 ret_code
= LTTNG_ERR_INVALID
;
901 DBG2("Setting trace directory path from URI to %s",
903 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
905 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
907 sizeof(consumer
->dst
.session_root_path
));
908 consumer
->type
= CONSUMER_DST_LOCAL
;
918 * Init tracing by creating trace directory and sending fds kernel consumer.
920 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
923 struct lttng_ht_iter iter
;
924 struct consumer_socket
*socket
;
930 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
931 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
933 pthread_mutex_lock(socket
->lock
);
934 ret
= kernel_consumer_send_session(socket
, session
);
935 pthread_mutex_unlock(socket
->lock
);
937 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
949 * Create a socket to the relayd using the URI.
951 * On success, the relayd_sock pointer is set to the created socket.
952 * Else, it remains untouched and an LTTng error code is returned.
954 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
955 struct lttcomm_relayd_sock
**relayd_sock
,
956 struct consumer_output
*consumer
)
959 enum lttng_error_code status
= LTTNG_OK
;
960 struct lttcomm_relayd_sock
*rsock
;
962 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
963 RELAYD_VERSION_COMM_MINOR
);
965 status
= LTTNG_ERR_FATAL
;
970 * Connect to relayd so we can proceed with a session creation. This call
971 * can possibly block for an arbitrary amount of time to set the health
972 * state to be in poll execution.
975 ret
= relayd_connect(rsock
);
978 ERR("Unable to reach lttng-relayd");
979 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
983 /* Create socket for control stream. */
984 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
985 DBG3("Creating relayd stream socket from URI");
987 /* Check relayd version */
988 ret
= relayd_version_check(rsock
);
989 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
990 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
992 } else if (ret
< 0) {
993 ERR("Unable to reach lttng-relayd");
994 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
997 consumer
->relay_major_version
= rsock
->major
;
998 consumer
->relay_minor_version
= rsock
->minor
;
999 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1000 DBG3("Creating relayd data socket from URI");
1002 /* Command is not valid */
1003 ERR("Relayd invalid stream type: %d", uri
->stype
);
1004 status
= LTTNG_ERR_INVALID
;
1008 *relayd_sock
= rsock
;
1013 /* The returned value is not useful since we are on an error path. */
1014 (void) relayd_close(rsock
);
1022 * Connect to the relayd using URI and send the socket to the right consumer.
1024 * The consumer socket lock must be held by the caller.
1026 * Returns LTTNG_OK on success or an LTTng error code on failure.
1028 static enum lttng_error_code
send_consumer_relayd_socket(
1029 unsigned int session_id
,
1030 struct lttng_uri
*relayd_uri
,
1031 struct consumer_output
*consumer
,
1032 struct consumer_socket
*consumer_sock
,
1033 const char *session_name
, const char *hostname
,
1034 int session_live_timer
)
1037 struct lttcomm_relayd_sock
*rsock
= NULL
;
1038 enum lttng_error_code status
;
1040 /* Connect to relayd and make version check if uri is the control. */
1041 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1042 if (status
!= LTTNG_OK
) {
1043 goto relayd_comm_error
;
1047 /* Set the network sequence index if not set. */
1048 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1049 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1051 * Increment net_seq_idx because we are about to transfer the
1052 * new relayd socket to the consumer.
1053 * Assign unique key so the consumer can match streams.
1055 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1056 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1059 /* Send relayd socket to consumer. */
1060 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1061 relayd_uri
->stype
, session_id
,
1062 session_name
, hostname
, session_live_timer
);
1064 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1068 /* Flag that the corresponding socket was sent. */
1069 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1070 consumer_sock
->control_sock_sent
= 1;
1071 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1072 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 (status
!= 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 * Returns LTTNG_OK, or an LTTng error code on failure.
1105 static enum lttng_error_code
send_consumer_relayd_sockets(
1106 enum lttng_domain_type domain
,
1107 unsigned int session_id
, struct consumer_output
*consumer
,
1108 struct consumer_socket
*sock
, const char *session_name
,
1109 const char *hostname
, int session_live_timer
)
1111 enum lttng_error_code status
= LTTNG_OK
;
1116 /* Sending control relayd socket. */
1117 if (!sock
->control_sock_sent
) {
1118 status
= send_consumer_relayd_socket(session_id
,
1119 &consumer
->dst
.net
.control
, consumer
, sock
,
1120 session_name
, hostname
, session_live_timer
);
1121 if (status
!= LTTNG_OK
) {
1126 /* Sending data relayd socket. */
1127 if (!sock
->data_sock_sent
) {
1128 status
= send_consumer_relayd_socket(session_id
,
1129 &consumer
->dst
.net
.data
, consumer
, sock
,
1130 session_name
, hostname
, session_live_timer
);
1131 if (status
!= LTTNG_OK
) {
1141 * Setup relayd connections for a tracing session. First creates the socket to
1142 * the relayd and send them to the right domain consumer. Consumer type MUST be
1145 int cmd_setup_relayd(struct ltt_session
*session
)
1148 struct ltt_ust_session
*usess
;
1149 struct ltt_kernel_session
*ksess
;
1150 struct consumer_socket
*socket
;
1151 struct lttng_ht_iter iter
;
1155 usess
= session
->ust_session
;
1156 ksess
= session
->kernel_session
;
1158 DBG("Setting relayd for session %s", session
->name
);
1162 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1163 && usess
->consumer
->enabled
) {
1164 /* For each consumer socket, send relayd sockets */
1165 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1166 socket
, node
.node
) {
1167 pthread_mutex_lock(socket
->lock
);
1168 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1169 usess
->consumer
, socket
,
1170 session
->name
, session
->hostname
,
1171 session
->live_timer
);
1172 pthread_mutex_unlock(socket
->lock
);
1173 if (ret
!= LTTNG_OK
) {
1176 /* Session is now ready for network streaming. */
1177 session
->net_handle
= 1;
1179 session
->consumer
->relay_major_version
=
1180 usess
->consumer
->relay_major_version
;
1181 session
->consumer
->relay_minor_version
=
1182 usess
->consumer
->relay_minor_version
;
1185 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1186 && ksess
->consumer
->enabled
) {
1187 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1188 socket
, node
.node
) {
1189 pthread_mutex_lock(socket
->lock
);
1190 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1191 ksess
->consumer
, socket
,
1192 session
->name
, session
->hostname
,
1193 session
->live_timer
);
1194 pthread_mutex_unlock(socket
->lock
);
1195 if (ret
!= LTTNG_OK
) {
1198 /* Session is now ready for network streaming. */
1199 session
->net_handle
= 1;
1201 session
->consumer
->relay_major_version
=
1202 ksess
->consumer
->relay_major_version
;
1203 session
->consumer
->relay_minor_version
=
1204 ksess
->consumer
->relay_minor_version
;
1213 * Start a kernel session by opening all necessary streams.
1215 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1218 struct ltt_kernel_channel
*kchan
;
1220 /* Open kernel metadata */
1221 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1222 ret
= kernel_open_metadata(ksess
);
1224 ret
= LTTNG_ERR_KERN_META_FAIL
;
1229 /* Open kernel metadata stream */
1230 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1231 ret
= kernel_open_metadata_stream(ksess
);
1233 ERR("Kernel create metadata stream failed");
1234 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1239 /* For each channel */
1240 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1241 if (kchan
->stream_count
== 0) {
1242 ret
= kernel_open_channel_stream(kchan
);
1244 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1247 /* Update the stream global counter */
1248 ksess
->stream_count_global
+= ret
;
1252 /* Setup kernel consumer socket and send fds to it */
1253 ret
= init_kernel_tracing(ksess
);
1255 ret
= LTTNG_ERR_KERN_START_FAIL
;
1259 /* This start the kernel tracing */
1260 ret
= kernel_start_session(ksess
);
1262 ret
= LTTNG_ERR_KERN_START_FAIL
;
1266 /* Quiescent wait after starting trace */
1267 kernel_wait_quiescent(wpipe
);
1278 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1280 int cmd_disable_channel(struct ltt_session
*session
,
1281 enum lttng_domain_type domain
, char *channel_name
)
1284 struct ltt_ust_session
*usess
;
1286 usess
= session
->ust_session
;
1291 case LTTNG_DOMAIN_KERNEL
:
1293 ret
= channel_kernel_disable(session
->kernel_session
,
1295 if (ret
!= LTTNG_OK
) {
1299 kernel_wait_quiescent(kernel_tracer_fd
);
1302 case LTTNG_DOMAIN_UST
:
1304 struct ltt_ust_channel
*uchan
;
1305 struct lttng_ht
*chan_ht
;
1307 chan_ht
= usess
->domain_global
.channels
;
1309 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1310 if (uchan
== NULL
) {
1311 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1315 ret
= channel_ust_disable(usess
, uchan
);
1316 if (ret
!= LTTNG_OK
) {
1322 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1334 * Command LTTNG_TRACK_PID processed by the client thread.
1336 * Called with session lock held.
1338 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1346 case LTTNG_DOMAIN_KERNEL
:
1348 struct ltt_kernel_session
*ksess
;
1350 ksess
= session
->kernel_session
;
1352 ret
= kernel_track_pid(ksess
, pid
);
1353 if (ret
!= LTTNG_OK
) {
1357 kernel_wait_quiescent(kernel_tracer_fd
);
1360 case LTTNG_DOMAIN_UST
:
1362 struct ltt_ust_session
*usess
;
1364 usess
= session
->ust_session
;
1366 ret
= trace_ust_track_pid(usess
, pid
);
1367 if (ret
!= LTTNG_OK
) {
1373 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1385 * Command LTTNG_UNTRACK_PID processed by the client thread.
1387 * Called with session lock held.
1389 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1397 case LTTNG_DOMAIN_KERNEL
:
1399 struct ltt_kernel_session
*ksess
;
1401 ksess
= session
->kernel_session
;
1403 ret
= kernel_untrack_pid(ksess
, pid
);
1404 if (ret
!= LTTNG_OK
) {
1408 kernel_wait_quiescent(kernel_tracer_fd
);
1411 case LTTNG_DOMAIN_UST
:
1413 struct ltt_ust_session
*usess
;
1415 usess
= session
->ust_session
;
1417 ret
= trace_ust_untrack_pid(usess
, pid
);
1418 if (ret
!= LTTNG_OK
) {
1424 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1436 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1438 * The wpipe arguments is used as a notifier for the kernel thread.
1440 int cmd_enable_channel(struct ltt_session
*session
,
1441 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1444 struct ltt_ust_session
*usess
= session
->ust_session
;
1445 struct lttng_ht
*chan_ht
;
1452 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1454 /* Validate channel name */
1455 if (attr
->name
[0] == '.' ||
1456 memchr(attr
->name
, '/', len
) != NULL
) {
1457 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1461 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1466 * Don't try to enable a channel if the session has been started at
1467 * some point in time before. The tracer does not allow it.
1469 if (session
->has_been_started
) {
1470 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1475 * If the session is a live session, remove the switch timer, the
1476 * live timer does the same thing but sends also synchronisation
1477 * beacons for inactive streams.
1479 if (session
->live_timer
> 0) {
1480 attr
->attr
.live_timer_interval
= session
->live_timer
;
1481 attr
->attr
.switch_timer_interval
= 0;
1484 /* Check for feature support */
1485 switch (domain
->type
) {
1486 case LTTNG_DOMAIN_KERNEL
:
1488 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd
) != 1) {
1489 /* Sampling position of buffer is not supported */
1490 WARN("Kernel tracer does not support buffer monitoring. "
1491 "Setting the monitor interval timer to 0 "
1492 "(disabled) for channel '%s' of session '%s'",
1493 attr
-> name
, session
->name
);
1494 lttng_channel_set_monitor_timer_interval(attr
, 0);
1498 case LTTNG_DOMAIN_UST
:
1500 case LTTNG_DOMAIN_JUL
:
1501 case LTTNG_DOMAIN_LOG4J
:
1502 case LTTNG_DOMAIN_PYTHON
:
1503 if (!agent_tracing_is_enabled()) {
1504 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1505 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1510 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1514 switch (domain
->type
) {
1515 case LTTNG_DOMAIN_KERNEL
:
1517 struct ltt_kernel_channel
*kchan
;
1519 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1520 session
->kernel_session
);
1521 if (kchan
== NULL
) {
1522 if (session
->snapshot
.nb_output
> 0 ||
1523 session
->snapshot_mode
) {
1524 /* Enforce mmap output for snapshot sessions. */
1525 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1527 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1528 if (attr
->name
[0] != '\0') {
1529 session
->kernel_session
->has_non_default_channel
= 1;
1532 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1535 if (ret
!= LTTNG_OK
) {
1539 kernel_wait_quiescent(kernel_tracer_fd
);
1542 case LTTNG_DOMAIN_UST
:
1543 case LTTNG_DOMAIN_JUL
:
1544 case LTTNG_DOMAIN_LOG4J
:
1545 case LTTNG_DOMAIN_PYTHON
:
1547 struct ltt_ust_channel
*uchan
;
1552 * Current agent implementation limitations force us to allow
1553 * only one channel at once in "agent" subdomains. Each
1554 * subdomain has a default channel name which must be strictly
1557 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1558 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1559 LTTNG_SYMBOL_NAME_LEN
)) {
1560 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1563 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1564 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1565 LTTNG_SYMBOL_NAME_LEN
)) {
1566 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1569 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1570 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1571 LTTNG_SYMBOL_NAME_LEN
)) {
1572 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1577 chan_ht
= usess
->domain_global
.channels
;
1579 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1580 if (uchan
== NULL
) {
1581 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1582 if (attr
->name
[0] != '\0') {
1583 usess
->has_non_default_channel
= 1;
1586 ret
= channel_ust_enable(usess
, uchan
);
1591 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1595 if (ret
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1596 session
->has_non_mmap_channel
= true;
1605 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1607 int cmd_disable_event(struct ltt_session
*session
,
1608 enum lttng_domain_type domain
, char *channel_name
,
1609 struct lttng_event
*event
)
1614 DBG("Disable event command for event \'%s\'", event
->name
);
1616 event_name
= event
->name
;
1618 /* Error out on unhandled search criteria */
1619 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1620 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1621 ret
= LTTNG_ERR_UNK
;
1628 case LTTNG_DOMAIN_KERNEL
:
1630 struct ltt_kernel_channel
*kchan
;
1631 struct ltt_kernel_session
*ksess
;
1633 ksess
= session
->kernel_session
;
1636 * If a non-default channel has been created in the
1637 * session, explicitely require that -c chan_name needs
1640 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1641 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1645 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1646 if (kchan
== NULL
) {
1647 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1651 switch (event
->type
) {
1652 case LTTNG_EVENT_ALL
:
1653 case LTTNG_EVENT_TRACEPOINT
:
1654 case LTTNG_EVENT_SYSCALL
:
1655 case LTTNG_EVENT_PROBE
:
1656 case LTTNG_EVENT_FUNCTION
:
1657 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1658 if (event_name
[0] == '\0') {
1659 ret
= event_kernel_disable_event(kchan
,
1662 ret
= event_kernel_disable_event(kchan
,
1663 event_name
, event
->type
);
1665 if (ret
!= LTTNG_OK
) {
1670 ret
= LTTNG_ERR_UNK
;
1674 kernel_wait_quiescent(kernel_tracer_fd
);
1677 case LTTNG_DOMAIN_UST
:
1679 struct ltt_ust_channel
*uchan
;
1680 struct ltt_ust_session
*usess
;
1682 usess
= session
->ust_session
;
1684 if (validate_ust_event_name(event_name
)) {
1685 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1690 * If a non-default channel has been created in the
1691 * session, explicitly require that -c chan_name needs
1694 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1695 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1699 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1701 if (uchan
== NULL
) {
1702 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1706 switch (event
->type
) {
1707 case LTTNG_EVENT_ALL
:
1709 * An empty event name means that everything
1710 * should be disabled.
1712 if (event
->name
[0] == '\0') {
1713 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1715 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1718 if (ret
!= LTTNG_OK
) {
1723 ret
= LTTNG_ERR_UNK
;
1727 DBG3("Disable UST event %s in channel %s completed", event_name
,
1731 case LTTNG_DOMAIN_LOG4J
:
1732 case LTTNG_DOMAIN_JUL
:
1733 case LTTNG_DOMAIN_PYTHON
:
1736 struct ltt_ust_session
*usess
= session
->ust_session
;
1740 switch (event
->type
) {
1741 case LTTNG_EVENT_ALL
:
1744 ret
= LTTNG_ERR_UNK
;
1748 agt
= trace_ust_find_agent(usess
, domain
);
1750 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1754 * An empty event name means that everything
1755 * should be disabled.
1757 if (event
->name
[0] == '\0') {
1758 ret
= event_agent_disable_all(usess
, agt
);
1760 ret
= event_agent_disable(usess
, agt
, event_name
);
1762 if (ret
!= LTTNG_OK
) {
1769 ret
= LTTNG_ERR_UND
;
1782 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1784 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1785 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1787 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1788 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1791 * Don't try to add a context if the session has been started at
1792 * some point in time before. The tracer does not allow it and would
1793 * result in a corrupted trace.
1795 if (session
->has_been_started
) {
1796 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1800 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1801 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1802 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1806 case LTTNG_DOMAIN_KERNEL
:
1807 assert(session
->kernel_session
);
1809 if (session
->kernel_session
->channel_count
== 0) {
1810 /* Create default channel */
1811 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1812 if (ret
!= LTTNG_OK
) {
1815 chan_kern_created
= 1;
1817 /* Add kernel context to kernel tracer */
1818 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1819 if (ret
!= LTTNG_OK
) {
1823 case LTTNG_DOMAIN_JUL
:
1824 case LTTNG_DOMAIN_LOG4J
:
1827 * Validate channel name.
1828 * If no channel name is given and the domain is JUL or LOG4J,
1829 * set it to the appropriate domain-specific channel name. If
1830 * a name is provided but does not match the expexted channel
1831 * name, return an error.
1833 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1834 strcmp(channel_name
,
1835 DEFAULT_JUL_CHANNEL_NAME
)) {
1836 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1838 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1839 strcmp(channel_name
,
1840 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1841 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1844 /* break is _not_ missing here. */
1846 case LTTNG_DOMAIN_UST
:
1848 struct ltt_ust_session
*usess
= session
->ust_session
;
1849 unsigned int chan_count
;
1853 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1854 if (chan_count
== 0) {
1855 struct lttng_channel
*attr
;
1856 /* Create default channel */
1857 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1859 ret
= LTTNG_ERR_FATAL
;
1863 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1864 if (ret
!= LTTNG_OK
) {
1868 channel_attr_destroy(attr
);
1869 chan_ust_created
= 1;
1872 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1873 free(app_ctx_provider_name
);
1875 app_ctx_name
= NULL
;
1876 app_ctx_provider_name
= NULL
;
1877 if (ret
!= LTTNG_OK
) {
1883 ret
= LTTNG_ERR_UND
;
1891 if (chan_kern_created
) {
1892 struct ltt_kernel_channel
*kchan
=
1893 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1894 session
->kernel_session
);
1895 /* Created previously, this should NOT fail. */
1897 kernel_destroy_channel(kchan
);
1900 if (chan_ust_created
) {
1901 struct ltt_ust_channel
*uchan
=
1902 trace_ust_find_channel_by_name(
1903 session
->ust_session
->domain_global
.channels
,
1904 DEFAULT_CHANNEL_NAME
);
1905 /* Created previously, this should NOT fail. */
1907 /* Remove from the channel list of the session. */
1908 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1910 trace_ust_destroy_channel(uchan
);
1913 free(app_ctx_provider_name
);
1918 static inline bool name_starts_with(const char *name
, const char *prefix
)
1920 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1922 return !strncmp(name
, prefix
, max_cmp_len
);
1925 /* Perform userspace-specific event name validation */
1926 static int validate_ust_event_name(const char *name
)
1936 * Check name against all internal UST event component namespaces used
1939 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1940 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1941 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1950 * Internal version of cmd_enable_event() with a supplemental
1951 * "internal_event" flag which is used to enable internal events which should
1952 * be hidden from clients. Such events are used in the agent implementation to
1953 * enable the events through which all "agent" events are funeled.
1955 static int _cmd_enable_event(struct ltt_session
*session
,
1956 struct lttng_domain
*domain
,
1957 char *channel_name
, struct lttng_event
*event
,
1958 char *filter_expression
,
1959 struct lttng_filter_bytecode
*filter
,
1960 struct lttng_event_exclusion
*exclusion
,
1961 int wpipe
, bool internal_event
)
1963 int ret
= 0, channel_created
= 0;
1964 struct lttng_channel
*attr
= NULL
;
1968 assert(channel_name
);
1970 /* If we have a filter, we must have its filter expression */
1971 assert(!(!!filter_expression
^ !!filter
));
1973 /* Normalize event name as a globbing pattern */
1974 strutils_normalize_star_glob_pattern(event
->name
);
1976 /* Normalize exclusion names as globbing patterns */
1980 for (i
= 0; i
< exclusion
->count
; i
++) {
1981 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
1983 strutils_normalize_star_glob_pattern(name
);
1987 DBG("Enable event command for event \'%s\'", event
->name
);
1991 switch (domain
->type
) {
1992 case LTTNG_DOMAIN_KERNEL
:
1994 struct ltt_kernel_channel
*kchan
;
1997 * If a non-default channel has been created in the
1998 * session, explicitely require that -c chan_name needs
2001 if (session
->kernel_session
->has_non_default_channel
2002 && channel_name
[0] == '\0') {
2003 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2007 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2008 session
->kernel_session
);
2009 if (kchan
== NULL
) {
2010 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2011 LTTNG_BUFFER_GLOBAL
);
2013 ret
= LTTNG_ERR_FATAL
;
2016 if (lttng_strncpy(attr
->name
, channel_name
,
2017 sizeof(attr
->name
))) {
2018 ret
= LTTNG_ERR_INVALID
;
2022 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2023 if (ret
!= LTTNG_OK
) {
2026 channel_created
= 1;
2029 /* Get the newly created kernel channel pointer */
2030 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2031 session
->kernel_session
);
2032 if (kchan
== NULL
) {
2033 /* This sould not happen... */
2034 ret
= LTTNG_ERR_FATAL
;
2038 switch (event
->type
) {
2039 case LTTNG_EVENT_ALL
:
2041 char *filter_expression_a
= NULL
;
2042 struct lttng_filter_bytecode
*filter_a
= NULL
;
2045 * We need to duplicate filter_expression and filter,
2046 * because ownership is passed to first enable
2049 if (filter_expression
) {
2050 filter_expression_a
= strdup(filter_expression
);
2051 if (!filter_expression_a
) {
2052 ret
= LTTNG_ERR_FATAL
;
2057 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2059 free(filter_expression_a
);
2060 ret
= LTTNG_ERR_FATAL
;
2063 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2065 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2066 ret
= event_kernel_enable_event(kchan
, event
,
2067 filter_expression
, filter
);
2068 /* We have passed ownership */
2069 filter_expression
= NULL
;
2071 if (ret
!= LTTNG_OK
) {
2072 if (channel_created
) {
2073 /* Let's not leak a useless channel. */
2074 kernel_destroy_channel(kchan
);
2076 free(filter_expression_a
);
2080 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2081 ret
= event_kernel_enable_event(kchan
, event
,
2082 filter_expression_a
, filter_a
);
2083 /* We have passed ownership */
2084 filter_expression_a
= NULL
;
2086 if (ret
!= LTTNG_OK
) {
2091 case LTTNG_EVENT_PROBE
:
2092 case LTTNG_EVENT_USERSPACE_PROBE
:
2093 case LTTNG_EVENT_FUNCTION
:
2094 case LTTNG_EVENT_FUNCTION_ENTRY
:
2095 case LTTNG_EVENT_TRACEPOINT
:
2096 ret
= event_kernel_enable_event(kchan
, event
,
2097 filter_expression
, filter
);
2098 /* We have passed ownership */
2099 filter_expression
= NULL
;
2101 if (ret
!= LTTNG_OK
) {
2102 if (channel_created
) {
2103 /* Let's not leak a useless channel. */
2104 kernel_destroy_channel(kchan
);
2109 case LTTNG_EVENT_SYSCALL
:
2110 ret
= event_kernel_enable_event(kchan
, event
,
2111 filter_expression
, filter
);
2112 /* We have passed ownership */
2113 filter_expression
= NULL
;
2115 if (ret
!= LTTNG_OK
) {
2120 ret
= LTTNG_ERR_UNK
;
2124 kernel_wait_quiescent(kernel_tracer_fd
);
2127 case LTTNG_DOMAIN_UST
:
2129 struct ltt_ust_channel
*uchan
;
2130 struct ltt_ust_session
*usess
= session
->ust_session
;
2135 * If a non-default channel has been created in the
2136 * session, explicitely require that -c chan_name needs
2139 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2140 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2144 /* Get channel from global UST domain */
2145 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2147 if (uchan
== NULL
) {
2148 /* Create default channel */
2149 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2150 usess
->buffer_type
);
2152 ret
= LTTNG_ERR_FATAL
;
2155 if (lttng_strncpy(attr
->name
, channel_name
,
2156 sizeof(attr
->name
))) {
2157 ret
= LTTNG_ERR_INVALID
;
2161 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2162 if (ret
!= LTTNG_OK
) {
2166 /* Get the newly created channel reference back */
2167 uchan
= trace_ust_find_channel_by_name(
2168 usess
->domain_global
.channels
, channel_name
);
2172 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2174 * Don't allow users to add UST events to channels which
2175 * are assigned to a userspace subdomain (JUL, Log4J,
2178 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2182 if (!internal_event
) {
2184 * Ensure the event name is not reserved for internal
2187 ret
= validate_ust_event_name(event
->name
);
2189 WARN("Userspace event name %s failed validation.",
2191 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2196 /* At this point, the session and channel exist on the tracer */
2197 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2198 filter_expression
, filter
, exclusion
,
2200 /* We have passed ownership */
2201 filter_expression
= NULL
;
2204 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2205 goto already_enabled
;
2206 } else if (ret
!= LTTNG_OK
) {
2211 case LTTNG_DOMAIN_LOG4J
:
2212 case LTTNG_DOMAIN_JUL
:
2213 case LTTNG_DOMAIN_PYTHON
:
2215 const char *default_event_name
, *default_chan_name
;
2217 struct lttng_event uevent
;
2218 struct lttng_domain tmp_dom
;
2219 struct ltt_ust_session
*usess
= session
->ust_session
;
2223 if (!agent_tracing_is_enabled()) {
2224 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2225 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2229 agt
= trace_ust_find_agent(usess
, domain
->type
);
2231 agt
= agent_create(domain
->type
);
2233 ret
= LTTNG_ERR_NOMEM
;
2236 agent_add(agt
, usess
->agents
);
2239 /* Create the default tracepoint. */
2240 memset(&uevent
, 0, sizeof(uevent
));
2241 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2242 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2243 default_event_name
= event_get_default_agent_ust_name(
2245 if (!default_event_name
) {
2246 ret
= LTTNG_ERR_FATAL
;
2249 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2250 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2253 * The domain type is changed because we are about to enable the
2254 * default channel and event for the JUL domain that are hardcoded.
2255 * This happens in the UST domain.
2257 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2258 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2260 switch (domain
->type
) {
2261 case LTTNG_DOMAIN_LOG4J
:
2262 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2264 case LTTNG_DOMAIN_JUL
:
2265 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2267 case LTTNG_DOMAIN_PYTHON
:
2268 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2271 /* The switch/case we are in makes this impossible */
2276 char *filter_expression_copy
= NULL
;
2277 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2280 const size_t filter_size
= sizeof(
2281 struct lttng_filter_bytecode
)
2284 filter_copy
= zmalloc(filter_size
);
2286 ret
= LTTNG_ERR_NOMEM
;
2289 memcpy(filter_copy
, filter
, filter_size
);
2291 filter_expression_copy
=
2292 strdup(filter_expression
);
2293 if (!filter_expression
) {
2294 ret
= LTTNG_ERR_NOMEM
;
2297 if (!filter_expression_copy
|| !filter_copy
) {
2298 free(filter_expression_copy
);
2304 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2305 (char *) default_chan_name
,
2306 &uevent
, filter_expression_copy
,
2307 filter_copy
, NULL
, wpipe
);
2310 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2311 goto already_enabled
;
2312 } else if (ret
!= LTTNG_OK
) {
2316 /* The wild card * means that everything should be enabled. */
2317 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2318 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2321 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2325 filter_expression
= NULL
;
2326 if (ret
!= LTTNG_OK
) {
2333 ret
= LTTNG_ERR_UND
;
2341 free(filter_expression
);
2344 channel_attr_destroy(attr
);
2350 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2351 * We own filter, exclusion, and filter_expression.
2353 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2354 char *channel_name
, struct lttng_event
*event
,
2355 char *filter_expression
,
2356 struct lttng_filter_bytecode
*filter
,
2357 struct lttng_event_exclusion
*exclusion
,
2360 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2361 filter_expression
, filter
, exclusion
, wpipe
, false);
2365 * Enable an event which is internal to LTTng. An internal should
2366 * never be made visible to clients and are immune to checks such as
2369 static int cmd_enable_event_internal(struct ltt_session
*session
,
2370 struct lttng_domain
*domain
,
2371 char *channel_name
, struct lttng_event
*event
,
2372 char *filter_expression
,
2373 struct lttng_filter_bytecode
*filter
,
2374 struct lttng_event_exclusion
*exclusion
,
2377 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2378 filter_expression
, filter
, exclusion
, wpipe
, true);
2382 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2384 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2385 struct lttng_event
**events
)
2388 ssize_t nb_events
= 0;
2391 case LTTNG_DOMAIN_KERNEL
:
2392 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2393 if (nb_events
< 0) {
2394 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2398 case LTTNG_DOMAIN_UST
:
2399 nb_events
= ust_app_list_events(events
);
2400 if (nb_events
< 0) {
2401 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2405 case LTTNG_DOMAIN_LOG4J
:
2406 case LTTNG_DOMAIN_JUL
:
2407 case LTTNG_DOMAIN_PYTHON
:
2408 nb_events
= agent_list_events(events
, domain
);
2409 if (nb_events
< 0) {
2410 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2415 ret
= LTTNG_ERR_UND
;
2422 /* Return negative value to differentiate return code */
2427 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2429 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2430 struct lttng_event_field
**fields
)
2433 ssize_t nb_fields
= 0;
2436 case LTTNG_DOMAIN_UST
:
2437 nb_fields
= ust_app_list_event_fields(fields
);
2438 if (nb_fields
< 0) {
2439 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2443 case LTTNG_DOMAIN_KERNEL
:
2444 default: /* fall-through */
2445 ret
= LTTNG_ERR_UND
;
2452 /* Return negative value to differentiate return code */
2456 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2458 return syscall_table_list(events
);
2462 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2464 * Called with session lock held.
2466 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2467 enum lttng_domain_type domain
, int32_t **pids
)
2470 ssize_t nr_pids
= 0;
2473 case LTTNG_DOMAIN_KERNEL
:
2475 struct ltt_kernel_session
*ksess
;
2477 ksess
= session
->kernel_session
;
2478 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2480 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2485 case LTTNG_DOMAIN_UST
:
2487 struct ltt_ust_session
*usess
;
2489 usess
= session
->ust_session
;
2490 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2492 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2497 case LTTNG_DOMAIN_LOG4J
:
2498 case LTTNG_DOMAIN_JUL
:
2499 case LTTNG_DOMAIN_PYTHON
:
2501 ret
= LTTNG_ERR_UND
;
2508 /* Return negative value to differentiate return code */
2513 int domain_mkdir(const struct consumer_output
*output
,
2514 const struct ltt_session
*session
,
2515 uid_t uid
, gid_t gid
)
2517 struct consumer_socket
*socket
;
2518 struct lttng_ht_iter iter
;
2520 char path
[LTTNG_PATH_MAX
];
2522 if (!output
|| !output
->socks
) {
2523 ERR("No consumer output found");
2528 ret
= snprintf(path
, sizeof(path
), "%s/%s%s",
2529 session_get_base_path(session
),
2531 output
->domain_subdir
);
2532 if (ret
< 0 || ret
>= LTTNG_PATH_MAX
) {
2533 ERR("Failed to format path new chunk domain path");
2538 DBG("Domain mkdir %s for session %" PRIu64
, path
, session
->id
);
2541 * We have to iterate to find a socket, but we only need to send the
2542 * rename command to one consumer, so we break after the first one.
2544 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
2545 pthread_mutex_lock(socket
->lock
);
2546 ret
= consumer_mkdir(socket
, session
->id
, output
, path
, uid
, gid
);
2547 pthread_mutex_unlock(socket
->lock
);
2549 ERR("Failed to create directory at \"%s\"", path
);
2565 * Command LTTNG_START_TRACE processed by the client thread.
2567 * Called with session mutex held.
2569 int cmd_start_trace(struct ltt_session
*session
)
2571 enum lttng_error_code ret
;
2572 unsigned long nb_chan
= 0;
2573 struct ltt_kernel_session
*ksession
;
2574 struct ltt_ust_session
*usess
;
2578 /* Ease our life a bit ;) */
2579 ksession
= session
->kernel_session
;
2580 usess
= session
->ust_session
;
2582 /* Is the session already started? */
2583 if (session
->active
) {
2584 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2589 * Starting a session without channel is useless since after that it's not
2590 * possible to enable channel thus inform the client.
2592 if (usess
&& usess
->domain_global
.channels
) {
2593 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2596 nb_chan
+= ksession
->channel_count
;
2599 ret
= LTTNG_ERR_NO_CHANNEL
;
2603 if (!session
->has_been_started
&& session
->output_traces
) {
2604 ret
= session_switch_trace_chunk(session
, NULL
, NULL
);
2605 if (ret
!= LTTNG_OK
) {
2610 /* Kernel tracing */
2611 if (ksession
!= NULL
) {
2612 DBG("Start kernel tracing session %s", session
->name
);
2613 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2614 if (ret
!= LTTNG_OK
) {
2619 /* Flag session that trace should start automatically */
2621 int int_ret
= ust_app_start_trace_all(usess
);
2624 ret
= LTTNG_ERR_UST_START_FAIL
;
2629 /* Flag this after a successful start. */
2630 session
->has_been_started
= 1;
2631 session
->active
= 1;
2634 * Clear the flag that indicates that a rotation was done while the
2635 * session was stopped.
2637 session
->rotated_after_last_stop
= false;
2639 if (session
->rotate_timer_period
) {
2640 int int_ret
= timer_session_rotation_schedule_timer_start(
2641 session
, session
->rotate_timer_period
);
2644 ERR("Failed to enable rotate timer");
2645 ret
= LTTNG_ERR_UNK
;
2657 * Command LTTNG_STOP_TRACE processed by the client thread.
2659 int cmd_stop_trace(struct ltt_session
*session
)
2662 struct ltt_kernel_channel
*kchan
;
2663 struct ltt_kernel_session
*ksession
;
2664 struct ltt_ust_session
*usess
;
2665 bool error_occurred
= false;
2669 DBG("Begin stop session %s (id %" PRIu64
")", session
->name
, session
->id
);
2671 ksession
= session
->kernel_session
;
2672 usess
= session
->ust_session
;
2674 /* Session is not active. Skip everythong and inform the client. */
2675 if (!session
->active
) {
2676 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2681 if (ksession
&& ksession
->active
) {
2682 DBG("Stop kernel tracing");
2684 ret
= kernel_stop_session(ksession
);
2686 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2690 kernel_wait_quiescent(kernel_tracer_fd
);
2692 /* Flush metadata after stopping (if exists) */
2693 if (ksession
->metadata_stream_fd
>= 0) {
2694 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2696 ERR("Kernel metadata flush failed");
2700 /* Flush all buffers after stopping */
2701 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2702 ret
= kernel_flush_buffer(kchan
);
2704 ERR("Kernel flush buffer error");
2708 ksession
->active
= 0;
2709 DBG("Kernel session stopped %s (id %" PRIu64
")", session
->name
,
2713 if (usess
&& usess
->active
) {
2714 ret
= ust_app_stop_trace_all(usess
);
2716 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2721 /* Flag inactive after a successful stop. */
2722 session
->active
= 0;
2723 ret
= !error_occurred
? LTTNG_OK
: LTTNG_ERR_UNK
;
2730 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2732 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2733 struct lttng_uri
*uris
)
2736 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2737 struct ltt_ust_session
*usess
= session
->ust_session
;
2743 /* Can't set consumer URI if the session is active. */
2744 if (session
->active
) {
2745 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2749 /* Set the "global" consumer URIs */
2750 for (i
= 0; i
< nb_uri
; i
++) {
2751 ret
= add_uri_to_consumer(session
,
2753 &uris
[i
], LTTNG_DOMAIN_NONE
);
2754 if (ret
!= LTTNG_OK
) {
2759 /* Set UST session URIs */
2760 if (session
->ust_session
) {
2761 for (i
= 0; i
< nb_uri
; i
++) {
2762 ret
= add_uri_to_consumer(session
,
2763 session
->ust_session
->consumer
,
2764 &uris
[i
], LTTNG_DOMAIN_UST
);
2765 if (ret
!= LTTNG_OK
) {
2771 /* Set kernel session URIs */
2772 if (session
->kernel_session
) {
2773 for (i
= 0; i
< nb_uri
; i
++) {
2774 ret
= add_uri_to_consumer(session
,
2775 session
->kernel_session
->consumer
,
2776 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2777 if (ret
!= LTTNG_OK
) {
2784 * Make sure to set the session in output mode after we set URI since a
2785 * session can be created without URL (thus flagged in no output mode).
2787 session
->output_traces
= 1;
2789 ksess
->output_traces
= 1;
2793 usess
->output_traces
= 1;
2804 enum lttng_error_code
set_session_output_from_descriptor(
2805 struct ltt_session
*session
,
2806 const struct lttng_session_descriptor
*descriptor
)
2809 enum lttng_error_code ret_code
= LTTNG_OK
;
2810 enum lttng_session_descriptor_type session_type
=
2811 lttng_session_descriptor_get_type(descriptor
);
2812 enum lttng_session_descriptor_output_type output_type
=
2813 lttng_session_descriptor_get_output_type(descriptor
);
2814 struct lttng_uri uris
[2] = {};
2815 size_t uri_count
= 0;
2817 switch (output_type
) {
2818 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2820 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2821 lttng_session_descriptor_get_local_output_uri(descriptor
,
2825 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2826 lttng_session_descriptor_get_network_output_uris(descriptor
,
2827 &uris
[0], &uris
[1]);
2831 ret_code
= LTTNG_ERR_INVALID
;
2835 switch (session_type
) {
2836 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2838 struct snapshot_output
*new_output
= NULL
;
2840 new_output
= snapshot_output_alloc();
2842 ret_code
= LTTNG_ERR_NOMEM
;
2846 ret
= snapshot_output_init_with_uri(session
,
2847 DEFAULT_SNAPSHOT_MAX_SIZE
,
2848 NULL
, uris
, uri_count
, session
->consumer
,
2849 new_output
, &session
->snapshot
);
2851 ret_code
= (ret
== -ENOMEM
) ?
2852 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2853 snapshot_output_destroy(new_output
);
2856 snapshot_add_output(&session
->snapshot
, new_output
);
2859 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2860 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2862 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2866 ret_code
= LTTNG_ERR_INVALID
;
2874 enum lttng_error_code
cmd_create_session_from_descriptor(
2875 struct lttng_session_descriptor
*descriptor
,
2876 const lttng_sock_cred
*creds
,
2877 const char *home_path
)
2880 enum lttng_error_code ret_code
;
2881 const char *session_name
;
2882 struct ltt_session
*new_session
= NULL
;
2883 enum lttng_session_descriptor_status descriptor_status
;
2885 session_lock_list();
2887 if (*home_path
!= '/') {
2888 ERR("Home path provided by client is not absolute");
2889 ret_code
= LTTNG_ERR_INVALID
;
2894 descriptor_status
= lttng_session_descriptor_get_session_name(
2895 descriptor
, &session_name
);
2896 switch (descriptor_status
) {
2897 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
2899 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
2900 session_name
= NULL
;
2903 ret_code
= LTTNG_ERR_INVALID
;
2906 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
2908 if (ret_code
!= LTTNG_OK
) {
2912 if (!session_name
) {
2913 ret
= lttng_session_descriptor_set_session_name(descriptor
,
2916 ret_code
= LTTNG_ERR_SESSION_FAIL
;
2921 if (!lttng_session_descriptor_is_output_destination_initialized(
2924 * Only include the session's creation time in the output
2925 * destination if the name of the session itself was
2926 * not auto-generated.
2928 ret_code
= lttng_session_descriptor_set_default_output(
2930 session_name
? &new_session
->creation_time
: NULL
,
2932 if (ret_code
!= LTTNG_OK
) {
2936 new_session
->has_user_specified_directory
=
2937 lttng_session_descriptor_has_output_directory(
2941 switch (lttng_session_descriptor_get_type(descriptor
)) {
2942 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2943 new_session
->snapshot_mode
= 1;
2945 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2946 new_session
->live_timer
=
2947 lttng_session_descriptor_live_get_timer_interval(
2954 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
2955 if (ret_code
!= LTTNG_OK
) {
2958 new_session
->consumer
->enabled
= 1;
2959 ret_code
= LTTNG_OK
;
2961 /* Release reference provided by the session_create function. */
2962 session_put(new_session
);
2963 if (ret_code
!= LTTNG_OK
&& new_session
) {
2964 /* Release the global reference on error. */
2965 session_destroy(new_session
);
2967 session_unlock_list();
2971 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
2972 struct lttng_session_descriptor
**return_descriptor
)
2975 size_t payload_size
;
2976 struct lttng_dynamic_buffer payload
;
2977 struct lttng_buffer_view home_dir_view
;
2978 struct lttng_buffer_view session_descriptor_view
;
2979 struct lttng_session_descriptor
*session_descriptor
= NULL
;
2980 enum lttng_error_code ret_code
;
2982 lttng_dynamic_buffer_init(&payload
);
2983 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
2985 ret_code
= LTTNG_ERR_INVALID
;
2988 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
2989 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
2990 ret_code
= LTTNG_ERR_INVALID
;
2994 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
2995 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
2996 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
2998 ret_code
= LTTNG_ERR_NOMEM
;
3002 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3004 ERR("Reception of session descriptor failed, aborting.");
3005 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3009 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3012 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3013 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3015 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3016 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3018 ret
= lttng_session_descriptor_create_from_buffer(
3019 &session_descriptor_view
, &session_descriptor
);
3021 ERR("Failed to create session descriptor from payload of \"create session\" command");
3022 ret_code
= LTTNG_ERR_INVALID
;
3027 * Sets the descriptor's auto-generated properties (name, output) if
3030 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3032 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3033 if (ret_code
!= LTTNG_OK
) {
3037 ret_code
= LTTNG_OK
;
3038 *return_descriptor
= session_descriptor
;
3039 session_descriptor
= NULL
;
3041 lttng_dynamic_buffer_reset(&payload
);
3042 lttng_session_descriptor_destroy(session_descriptor
);
3047 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3049 * Called with session lock held.
3051 int cmd_destroy_session(struct ltt_session
*session
,
3052 struct notification_thread_handle
*notification_thread_handle
)
3059 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3061 if (session
->rotation_schedule_timer_enabled
) {
3062 if (timer_session_rotation_schedule_timer_stop(
3064 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3069 if (session
->rotate_size
) {
3070 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3071 session
->rotate_size
= 0;
3074 if (session
->current_archive_id
!= 0) {
3075 if (!session
->rotated_after_last_stop
) {
3076 ret
= cmd_rotate_session(session
, NULL
);
3077 if (ret
!= LTTNG_OK
) {
3078 ERR("Failed to perform an implicit rotation as part of the rotation: %s", lttng_strerror(-ret
));
3082 * Rename the active chunk to ensure it has a name
3083 * of the form ts_begin-ts_end-id.
3085 * Note that no trace data has been produced since
3086 * the last rotation; the directory should be
3089 ret
= rename_active_chunk(session
);
3091 ERR("Failed to rename active chunk during the destruction of session \"%s\"",
3097 if (session
->shm_path
[0]) {
3099 * When a session is created with an explicit shm_path,
3100 * the consumer daemon will create its shared memory files
3101 * at that location and will *not* unlink them. This is normal
3102 * as the intention of that feature is to make it possible
3103 * to retrieve the content of those files should a crash occur.
3105 * To ensure the content of those files can be used, the
3106 * sessiond daemon will replicate the content of the metadata
3107 * cache in a metadata file.
3109 * On clean-up, it is expected that the consumer daemon will
3110 * unlink the shared memory files and that the session daemon
3111 * will unlink the metadata file. Then, the session's directory
3112 * in the shm path can be removed.
3114 * Unfortunately, a flaw in the design of the sessiond's and
3115 * consumerd's tear down of channels makes it impossible to
3116 * determine when the sessiond _and_ the consumerd have both
3117 * destroyed their representation of a channel. For one, the
3118 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3119 * callbacks in both daemons.
3121 * However, it is also impossible for the sessiond to know when
3122 * the consumer daemon is done destroying its channel(s) since
3123 * it occurs as a reaction to the closing of the channel's file
3124 * descriptor. There is no resulting communication initiated
3125 * from the consumerd to the sessiond to confirm that the
3126 * operation is completed (and was successful).
3128 * Until this is all fixed, the session daemon checks for the
3129 * removal of the session's shm path which makes it possible
3130 * to safely advertise a session as having been destroyed.
3132 * Prior to this fix, it was not possible to reliably save
3133 * a session making use of the --shm-path option, destroy it,
3134 * and load it again. This is because the creation of the
3135 * session would fail upon seeing the session's shm path
3136 * already in existence.
3138 * Note that none of the error paths in the check for the
3139 * directory's existence return an error. This is normal
3140 * as there isn't much that can be done. The session will
3141 * be destroyed properly, except that we can't offer the
3142 * guarantee that the same session can be re-created.
3144 current_completion_handler
= &destroy_completion_handler
.handler
;
3145 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3147 sizeof(destroy_completion_handler
.shm_path
));
3152 * The session is destroyed. However, note that the command context
3153 * still holds a reference to the session, thus delaying its destruction
3154 * _at least_ up to the point when that reference is released.
3156 session_destroy(session
);
3163 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3165 int cmd_register_consumer(struct ltt_session
*session
,
3166 enum lttng_domain_type domain
, const char *sock_path
,
3167 struct consumer_data
*cdata
)
3170 struct consumer_socket
*socket
= NULL
;
3177 case LTTNG_DOMAIN_KERNEL
:
3179 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3183 /* Can't register a consumer if there is already one */
3184 if (ksess
->consumer_fds_sent
!= 0) {
3185 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3189 sock
= lttcomm_connect_unix_sock(sock_path
);
3191 ret
= LTTNG_ERR_CONNECT_FAIL
;
3194 cdata
->cmd_sock
= sock
;
3196 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3197 if (socket
== NULL
) {
3200 PERROR("close register consumer");
3202 cdata
->cmd_sock
= -1;
3203 ret
= LTTNG_ERR_FATAL
;
3207 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3208 if (socket
->lock
== NULL
) {
3209 PERROR("zmalloc pthread mutex");
3210 ret
= LTTNG_ERR_FATAL
;
3213 pthread_mutex_init(socket
->lock
, NULL
);
3214 socket
->registered
= 1;
3217 consumer_add_socket(socket
, ksess
->consumer
);
3220 pthread_mutex_lock(&cdata
->pid_mutex
);
3222 pthread_mutex_unlock(&cdata
->pid_mutex
);
3227 /* TODO: Userspace tracing */
3228 ret
= LTTNG_ERR_UND
;
3236 consumer_destroy_socket(socket
);
3242 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3244 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3245 struct lttng_domain
**domains
)
3250 struct lttng_ht_iter iter
;
3252 if (session
->kernel_session
!= NULL
) {
3253 DBG3("Listing domains found kernel domain");
3257 if (session
->ust_session
!= NULL
) {
3258 DBG3("Listing domains found UST global domain");
3262 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3264 if (agt
->being_used
) {
3275 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3276 if (*domains
== NULL
) {
3277 ret
= LTTNG_ERR_FATAL
;
3281 if (session
->kernel_session
!= NULL
) {
3282 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3284 /* Kernel session buffer type is always GLOBAL */
3285 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3290 if (session
->ust_session
!= NULL
) {
3291 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3292 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3296 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3298 if (agt
->being_used
) {
3299 (*domains
)[index
].type
= agt
->domain
;
3300 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3310 /* Return negative value to differentiate return code */
3316 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3318 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3319 struct ltt_session
*session
, struct lttng_channel
**channels
)
3321 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3324 case LTTNG_DOMAIN_KERNEL
:
3325 if (session
->kernel_session
!= NULL
) {
3326 nb_chan
= session
->kernel_session
->channel_count
;
3328 DBG3("Number of kernel channels %zd", nb_chan
);
3330 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3334 case LTTNG_DOMAIN_UST
:
3335 if (session
->ust_session
!= NULL
) {
3337 nb_chan
= lttng_ht_get_count(
3338 session
->ust_session
->domain_global
.channels
);
3341 DBG3("Number of UST global channels %zd", nb_chan
);
3343 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3348 ret
= -LTTNG_ERR_UND
;
3353 const size_t channel_size
= sizeof(struct lttng_channel
) +
3354 sizeof(struct lttng_channel_extended
);
3355 struct lttng_channel_extended
*channel_exts
;
3357 payload_size
= nb_chan
* channel_size
;
3358 *channels
= zmalloc(payload_size
);
3359 if (*channels
== NULL
) {
3360 ret
= -LTTNG_ERR_FATAL
;
3364 channel_exts
= ((void *) *channels
) +
3365 (nb_chan
* sizeof(struct lttng_channel
));
3366 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3367 if (ret
!= LTTNG_OK
) {
3382 * Command LTTNG_LIST_EVENTS processed by the client thread.
3384 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3385 struct ltt_session
*session
, char *channel_name
,
3386 struct lttng_event
**events
, size_t *total_size
)
3389 ssize_t nb_event
= 0;
3392 case LTTNG_DOMAIN_KERNEL
:
3393 if (session
->kernel_session
!= NULL
) {
3394 nb_event
= list_lttng_kernel_events(channel_name
,
3395 session
->kernel_session
, events
,
3399 case LTTNG_DOMAIN_UST
:
3401 if (session
->ust_session
!= NULL
) {
3402 nb_event
= list_lttng_ust_global_events(channel_name
,
3403 &session
->ust_session
->domain_global
, events
,
3408 case LTTNG_DOMAIN_LOG4J
:
3409 case LTTNG_DOMAIN_JUL
:
3410 case LTTNG_DOMAIN_PYTHON
:
3411 if (session
->ust_session
) {
3412 struct lttng_ht_iter iter
;
3416 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3417 &iter
.iter
, agt
, node
.node
) {
3418 if (agt
->domain
== domain
) {
3419 nb_event
= list_lttng_agent_events(
3429 ret
= LTTNG_ERR_UND
;
3436 /* Return negative value to differentiate return code */
3441 * Using the session list, filled a lttng_session array to send back to the
3442 * client for session listing.
3444 * The session list lock MUST be acquired before calling this function. Use
3445 * session_lock_list() and session_unlock_list().
3447 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3448 size_t session_count
, uid_t uid
, gid_t gid
)
3452 struct ltt_session
*session
;
3453 struct ltt_session_list
*list
= session_get_list();
3454 struct lttng_session_extended
*extended
=
3455 (typeof(extended
)) (&sessions
[session_count
]);
3457 DBG("Getting all available session for UID %d GID %d",
3460 * Iterate over session list and append data after the control struct in
3463 cds_list_for_each_entry(session
, &list
->head
, list
) {
3464 if (!session_get(session
)) {
3468 * Only list the sessions the user can control.
3470 if (!session_access_ok(session
, uid
, gid
) ||
3471 session
->destroyed
) {
3472 session_put(session
);
3476 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3477 struct ltt_ust_session
*usess
= session
->ust_session
;
3479 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3480 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3481 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3482 ret
= build_network_session_path(sessions
[i
].path
,
3483 sizeof(sessions
[i
].path
), session
);
3485 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3486 session
->consumer
->dst
.session_root_path
);
3489 PERROR("snprintf session path");
3490 session_put(session
);
3494 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3495 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3496 sessions
[i
].enabled
= session
->active
;
3497 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3498 sessions
[i
].live_timer_interval
= session
->live_timer
;
3499 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3500 extended
[i
].creation_time
.is_set
= 1;
3502 session_put(session
);
3507 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3508 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3510 int cmd_data_pending(struct ltt_session
*session
)
3513 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3514 struct ltt_ust_session
*usess
= session
->ust_session
;
3518 DBG("Data pending for session %s", session
->name
);
3520 /* Session MUST be stopped to ask for data availability. */
3521 if (session
->active
) {
3522 ret
= LTTNG_ERR_SESSION_STARTED
;
3526 * If stopped, just make sure we've started before else the above call
3527 * will always send that there is data pending.
3529 * The consumer assumes that when the data pending command is received,
3530 * the trace has been started before or else no output data is written
3531 * by the streams which is a condition for data pending. So, this is
3532 * *VERY* important that we don't ask the consumer before a start
3535 if (!session
->has_been_started
) {
3541 /* A rotation is still pending, we have to wait. */
3542 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3543 DBG("Rotate still pending for session %s", session
->name
);
3548 if (ksess
&& ksess
->consumer
) {
3549 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3551 /* Data is still being extracted for the kernel. */
3556 if (usess
&& usess
->consumer
) {
3557 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3559 /* Data is still being extracted for the kernel. */
3564 /* Data is ready to be read by a viewer */
3572 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3574 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3576 int cmd_snapshot_add_output(struct ltt_session
*session
,
3577 struct lttng_snapshot_output
*output
, uint32_t *id
)
3580 struct snapshot_output
*new_output
;
3585 DBG("Cmd snapshot add output for session %s", session
->name
);
3588 * Can't create an output if the session is not set in no-output mode.
3590 if (session
->output_traces
) {
3591 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3595 if (session
->has_non_mmap_channel
) {
3596 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3600 /* Only one output is allowed until we have the "tee" feature. */
3601 if (session
->snapshot
.nb_output
== 1) {
3602 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3606 new_output
= snapshot_output_alloc();
3608 ret
= LTTNG_ERR_NOMEM
;
3612 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3613 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3614 &session
->snapshot
);
3616 if (ret
== -ENOMEM
) {
3617 ret
= LTTNG_ERR_NOMEM
;
3619 ret
= LTTNG_ERR_INVALID
;
3625 snapshot_add_output(&session
->snapshot
, new_output
);
3627 *id
= new_output
->id
;
3634 snapshot_output_destroy(new_output
);
3640 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3642 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3644 int cmd_snapshot_del_output(struct ltt_session
*session
,
3645 struct lttng_snapshot_output
*output
)
3648 struct snapshot_output
*sout
= NULL
;
3656 * Permission denied to create an output if the session is not
3657 * set in no output mode.
3659 if (session
->output_traces
) {
3660 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3665 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3667 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3668 } else if (*output
->name
!= '\0') {
3669 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3671 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3674 ret
= LTTNG_ERR_INVALID
;
3678 snapshot_delete_output(&session
->snapshot
, sout
);
3679 snapshot_output_destroy(sout
);
3688 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3690 * If no output is available, outputs is untouched and 0 is returned.
3692 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3694 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3695 struct lttng_snapshot_output
**outputs
)
3698 struct lttng_snapshot_output
*list
= NULL
;
3699 struct lttng_ht_iter iter
;
3700 struct snapshot_output
*output
;
3705 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3708 * Permission denied to create an output if the session is not
3709 * set in no output mode.
3711 if (session
->output_traces
) {
3712 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3716 if (session
->snapshot
.nb_output
== 0) {
3721 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3723 ret
= -LTTNG_ERR_NOMEM
;
3727 /* Copy list from session to the new list object. */
3729 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3730 output
, node
.node
) {
3731 assert(output
->consumer
);
3732 list
[idx
].id
= output
->id
;
3733 list
[idx
].max_size
= output
->max_size
;
3734 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3735 sizeof(list
[idx
].name
))) {
3736 ret
= -LTTNG_ERR_INVALID
;
3739 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3740 if (lttng_strncpy(list
[idx
].ctrl_url
,
3741 output
->consumer
->dst
.session_root_path
,
3742 sizeof(list
[idx
].ctrl_url
))) {
3743 ret
= -LTTNG_ERR_INVALID
;
3748 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3749 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3751 ret
= -LTTNG_ERR_NOMEM
;
3756 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3757 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3759 ret
= -LTTNG_ERR_NOMEM
;
3768 ret
= session
->snapshot
.nb_output
;
3777 * Check if we can regenerate the metadata for this session.
3778 * Only kernel, UST per-uid and non-live sessions are supported.
3780 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3783 int check_regenerate_metadata_support(struct ltt_session
*session
)
3789 if (session
->live_timer
!= 0) {
3790 ret
= LTTNG_ERR_LIVE_SESSION
;
3793 if (!session
->active
) {
3794 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3797 if (session
->ust_session
) {
3798 switch (session
->ust_session
->buffer_type
) {
3799 case LTTNG_BUFFER_PER_UID
:
3801 case LTTNG_BUFFER_PER_PID
:
3802 ret
= LTTNG_ERR_PER_PID_SESSION
;
3806 ret
= LTTNG_ERR_UNK
;
3810 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3811 session
->consumer
->relay_minor_version
< 8) {
3812 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3822 int clear_metadata_file(int fd
)
3827 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
3828 if (lseek_ret
< 0) {
3834 ret
= ftruncate(fd
, 0);
3836 PERROR("ftruncate");
3845 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3848 struct buffer_reg_uid
*uid_reg
= NULL
;
3849 struct buffer_reg_session
*session_reg
= NULL
;
3852 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3853 struct ust_registry_session
*registry
;
3854 struct ust_registry_channel
*chan
;
3855 struct lttng_ht_iter iter_chan
;
3857 session_reg
= uid_reg
->registry
;
3858 registry
= session_reg
->reg
.ust
;
3860 pthread_mutex_lock(®istry
->lock
);
3861 registry
->metadata_len_sent
= 0;
3862 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3863 registry
->metadata_len
= 0;
3864 registry
->metadata_version
++;
3865 if (registry
->metadata_fd
> 0) {
3866 /* Clear the metadata file's content. */
3867 ret
= clear_metadata_file(registry
->metadata_fd
);
3869 pthread_mutex_unlock(®istry
->lock
);
3874 ret
= ust_metadata_session_statedump(registry
, NULL
,
3875 registry
->major
, registry
->minor
);
3877 pthread_mutex_unlock(®istry
->lock
);
3878 ERR("Failed to generate session metadata (err = %d)",
3882 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3884 struct ust_registry_event
*event
;
3885 struct lttng_ht_iter iter_event
;
3887 ret
= ust_metadata_channel_statedump(registry
, chan
);
3889 pthread_mutex_unlock(®istry
->lock
);
3890 ERR("Failed to generate channel metadata "
3894 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3896 ret
= ust_metadata_event_statedump(registry
,
3899 pthread_mutex_unlock(®istry
->lock
);
3900 ERR("Failed to generate event metadata "
3906 pthread_mutex_unlock(®istry
->lock
);
3915 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3917 * Ask the consumer to truncate the existing metadata file(s) and
3918 * then regenerate the metadata. Live and per-pid sessions are not
3919 * supported and return an error.
3921 * Return 0 on success or else a LTTNG_ERR code.
3923 int cmd_regenerate_metadata(struct ltt_session
*session
)
3929 ret
= check_regenerate_metadata_support(session
);
3934 if (session
->kernel_session
) {
3935 ret
= kernctl_session_regenerate_metadata(
3936 session
->kernel_session
->fd
);
3938 ERR("Failed to regenerate the kernel metadata");
3943 if (session
->ust_session
) {
3944 ret
= ust_regenerate_metadata(session
->ust_session
);
3946 ERR("Failed to regenerate the UST metadata");
3950 DBG("Cmd metadata regenerate for session %s", session
->name
);
3958 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3960 * Ask the tracer to regenerate a new statedump.
3962 * Return 0 on success or else a LTTNG_ERR code.
3964 int cmd_regenerate_statedump(struct ltt_session
*session
)
3970 if (!session
->active
) {
3971 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3975 if (session
->kernel_session
) {
3976 ret
= kernctl_session_regenerate_statedump(
3977 session
->kernel_session
->fd
);
3979 * Currently, the statedump in kernel can only fail if out
3983 if (ret
== -ENOMEM
) {
3984 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3986 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3988 ERR("Failed to regenerate the kernel statedump");
3993 if (session
->ust_session
) {
3994 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3996 * Currently, the statedump in UST always returns 0.
3999 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4000 ERR("Failed to regenerate the UST statedump");
4004 DBG("Cmd regenerate statedump for session %s", session
->name
);
4011 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4012 struct notification_thread_handle
*notification_thread
)
4016 ssize_t sock_recv_len
;
4017 struct lttng_trigger
*trigger
= NULL
;
4018 struct lttng_buffer_view view
;
4019 struct lttng_dynamic_buffer trigger_buffer
;
4021 lttng_dynamic_buffer_init(&trigger_buffer
);
4022 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4023 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4025 ret
= LTTNG_ERR_NOMEM
;
4029 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4031 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4032 ERR("Failed to receive \"register trigger\" command payload");
4033 /* TODO: should this be a new error enum ? */
4034 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4038 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4039 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4041 ERR("Invalid trigger payload received in \"register trigger\" command");
4042 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4046 ret
= notification_thread_command_register_trigger(notification_thread
,
4048 /* Ownership of trigger was transferred. */
4051 lttng_trigger_destroy(trigger
);
4052 lttng_dynamic_buffer_reset(&trigger_buffer
);
4056 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4057 struct notification_thread_handle
*notification_thread
)
4061 ssize_t sock_recv_len
;
4062 struct lttng_trigger
*trigger
= NULL
;
4063 struct lttng_buffer_view view
;
4064 struct lttng_dynamic_buffer trigger_buffer
;
4066 lttng_dynamic_buffer_init(&trigger_buffer
);
4067 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4068 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4070 ret
= LTTNG_ERR_NOMEM
;
4074 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4076 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4077 ERR("Failed to receive \"unregister trigger\" command payload");
4078 /* TODO: should this be a new error enum ? */
4079 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4083 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4084 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4086 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4087 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4091 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4094 lttng_trigger_destroy(trigger
);
4095 lttng_dynamic_buffer_reset(&trigger_buffer
);
4100 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4101 * snapshot output is *not* set with a remote destination.
4103 * Return LTTNG_OK on success or a LTTNG_ERR code.
4105 static enum lttng_error_code
set_relayd_for_snapshot(
4106 struct consumer_output
*consumer
,
4107 const struct snapshot_output
*snap_output
,
4108 const struct ltt_session
*session
)
4110 enum lttng_error_code status
= LTTNG_OK
;
4111 struct lttng_ht_iter iter
;
4112 struct consumer_socket
*socket
;
4115 assert(snap_output
);
4118 DBG2("Set relayd object from snapshot output");
4120 /* Ignore if snapshot consumer output is not network. */
4121 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
4126 * For each consumer socket, create and send the relayd object of the
4130 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
4131 socket
, node
.node
) {
4132 pthread_mutex_lock(socket
->lock
);
4133 status
= send_consumer_relayd_sockets(0, session
->id
,
4134 snap_output
->consumer
, socket
,
4135 session
->name
, session
->hostname
,
4136 session
->live_timer
);
4137 pthread_mutex_unlock(socket
->lock
);
4138 if (status
!= LTTNG_OK
) {
4150 * Record a kernel snapshot.
4152 * Return LTTNG_OK on success or a LTTNG_ERR code.
4154 static enum lttng_error_code
record_kernel_snapshot(
4155 struct ltt_kernel_session
*ksess
,
4156 const struct snapshot_output
*output
,
4157 const struct ltt_session
*session
,
4158 int wait
, uint64_t nb_packets_per_stream
)
4161 enum lttng_error_code status
;
4168 * Copy kernel session sockets so we can communicate with the right
4169 * consumer for the snapshot record command.
4171 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
4173 status
= LTTNG_ERR_NOMEM
;
4177 status
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
4178 if (status
!= LTTNG_OK
) {
4179 goto error_snapshot
;
4182 status
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
4183 if (status
!= LTTNG_OK
) {
4184 goto error_snapshot
;
4190 /* Clean up copied sockets so this output can use some other later on. */
4191 consumer_destroy_output_sockets(output
->consumer
);
4198 * Record a UST snapshot.
4200 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4202 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4203 const struct snapshot_output
*output
,
4204 const struct ltt_session
*session
, int wait
,
4205 uint64_t nb_packets_per_stream
)
4208 enum lttng_error_code status
;
4215 * Copy UST session sockets so we can communicate with the right
4216 * consumer for the snapshot record command.
4218 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
4220 status
= LTTNG_ERR_NOMEM
;
4224 status
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
4225 if (status
!= LTTNG_OK
) {
4226 goto error_snapshot
;
4229 status
= ust_app_snapshot_record(usess
, output
, wait
,
4230 nb_packets_per_stream
);
4231 if (status
!= LTTNG_OK
) {
4232 goto error_snapshot
;
4238 /* Clean up copied sockets so this output can use some other later on. */
4239 consumer_destroy_output_sockets(output
->consumer
);
4246 uint64_t get_session_size_one_more_packet_per_stream(
4247 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4249 uint64_t tot_size
= 0;
4251 if (session
->kernel_session
) {
4252 struct ltt_kernel_channel
*chan
;
4253 const struct ltt_kernel_session
*ksess
=
4254 session
->kernel_session
;
4256 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4257 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4259 * Don't take channel into account if we
4260 * already grab all its packets.
4264 tot_size
+= chan
->channel
->attr
.subbuf_size
4265 * chan
->stream_count
;
4269 if (session
->ust_session
) {
4270 const struct ltt_ust_session
*usess
= session
->ust_session
;
4272 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4280 * Calculate the number of packets we can grab from each stream that
4281 * fits within the overall snapshot max size.
4283 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4284 * the number of packets per stream.
4286 * TODO: this approach is not perfect: we consider the worse case
4287 * (packet filling the sub-buffers) as an upper bound, but we could do
4288 * better if we do this calculation while we actually grab the packet
4289 * content: we would know how much padding we don't actually store into
4292 * This algorithm is currently bounded by the number of packets per
4295 * Since we call this algorithm before actually grabbing the data, it's
4296 * an approximation: for instance, applications could appear/disappear
4297 * in between this call and actually grabbing data.
4300 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4304 uint64_t cur_nb_packets
= 0;
4307 return 0; /* Infinite */
4310 size_left
= max_size
;
4312 uint64_t one_more_packet_tot_size
;
4314 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4315 session
, cur_nb_packets
);
4316 if (!one_more_packet_tot_size
) {
4317 /* We are already grabbing all packets. */
4320 size_left
-= one_more_packet_tot_size
;
4321 if (size_left
< 0) {
4326 if (!cur_nb_packets
) {
4327 /* Not enough room to grab one packet of each stream, error. */
4330 return cur_nb_packets
;
4334 enum lttng_error_code
snapshot_record(const struct ltt_session
*session
,
4335 const struct snapshot_output
*snapshot_output
, int wait
)
4337 int64_t nb_packets_per_stream
;
4338 enum lttng_error_code ret
= LTTNG_OK
;
4340 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4341 snapshot_output
->max_size
);
4342 if (nb_packets_per_stream
< 0) {
4343 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4347 if (session
->kernel_session
) {
4348 ret
= record_kernel_snapshot(session
->kernel_session
,
4349 snapshot_output
, session
,
4350 wait
, nb_packets_per_stream
);
4351 if (ret
!= LTTNG_OK
) {
4356 if (session
->ust_session
) {
4357 ret
= record_ust_snapshot(session
->ust_session
,
4358 snapshot_output
, session
,
4359 wait
, nb_packets_per_stream
);
4360 if (ret
!= LTTNG_OK
) {
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
)
4379 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4381 unsigned int use_tmp_output
= 0;
4382 struct snapshot_output tmp_output
;
4383 unsigned int snapshot_success
= 0;
4389 DBG("Cmd snapshot record for session %s", session
->name
);
4391 /* Get the datetime for the snapshot output directory. */
4392 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4395 cmd_ret
= LTTNG_ERR_INVALID
;
4400 * Permission denied to create an output if the session is not
4401 * set in no output mode.
4403 if (session
->output_traces
) {
4404 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4408 /* The session needs to be started at least once. */
4409 if (!session
->has_been_started
) {
4410 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4414 /* Use temporary output for the session. */
4415 if (*output
->ctrl_url
!= '\0') {
4416 ret
= snapshot_output_init(session
, output
->max_size
,
4418 output
->ctrl_url
, output
->data_url
,
4422 if (ret
== -ENOMEM
) {
4423 cmd_ret
= LTTNG_ERR_NOMEM
;
4425 cmd_ret
= LTTNG_ERR_INVALID
;
4429 /* Use the global session count for the temporary snapshot. */
4430 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4432 /* Use the global datetime */
4433 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4437 if (use_tmp_output
) {
4438 cmd_ret
= snapshot_record(session
, &tmp_output
, wait
);
4439 if (cmd_ret
!= LTTNG_OK
) {
4442 snapshot_success
= 1;
4444 struct snapshot_output
*sout
;
4445 struct lttng_ht_iter iter
;
4448 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4449 &iter
.iter
, sout
, node
.node
) {
4451 * Make a local copy of the output and assign the
4452 * possible temporary value given by the caller.
4454 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
4456 if (output
->max_size
!= (uint64_t) -1ULL) {
4457 tmp_output
.max_size
= output
->max_size
;
4460 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4461 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4463 /* Use temporary name. */
4464 if (*output
->name
!= '\0') {
4465 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4466 sizeof(tmp_output
.name
))) {
4467 cmd_ret
= LTTNG_ERR_INVALID
;
4473 cmd_ret
= snapshot_record(session
, &tmp_output
, wait
);
4474 if (cmd_ret
!= LTTNG_OK
) {
4478 snapshot_success
= 1;
4483 if (snapshot_success
) {
4484 session
->snapshot
.nb_snapshot
++;
4486 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4494 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4496 int cmd_set_session_shm_path(struct ltt_session
*session
,
4497 const char *shm_path
)
4503 * Can only set shm path before session is started.
4505 if (session
->has_been_started
) {
4506 return LTTNG_ERR_SESSION_STARTED
;
4509 strncpy(session
->shm_path
, shm_path
,
4510 sizeof(session
->shm_path
));
4511 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4517 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4519 * Ask the consumer to rotate the session output directory.
4520 * The session lock must be held.
4522 * Returns LTTNG_OK on success or else a negative LTTng error code.
4524 int cmd_rotate_session(struct ltt_session
*session
,
4525 struct lttng_rotate_session_return
*rotate_return
)
4528 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4530 struct tm
*timeinfo
;
4534 * Used to roll-back timestamps in case of failure to launch the
4537 time_t original_last_chunk_start_ts
, original_current_chunk_start_ts
;
4541 if (!session
->has_been_started
) {
4542 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4546 if (session
->live_timer
|| !session
->output_traces
) {
4547 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4552 * Unsupported feature in lttng-relayd before 2.11.
4554 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4555 (session
->consumer
->relay_major_version
== 2 &&
4556 session
->consumer
->relay_minor_version
< 11)) {
4557 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
4561 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4562 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
4564 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
4569 * After a stop, we only allow one rotation to occur, the other ones are
4570 * useless until a new start.
4572 if (session
->rotated_after_last_stop
) {
4573 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4575 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
4579 /* Special case for the first rotation. */
4580 if (session
->current_archive_id
== 0) {
4581 const char *base_path
= NULL
;
4583 assert(session
->kernel_session
|| session
->ust_session
);
4584 /* Either one of the two sessions is enough to get the root path. */
4585 base_path
= session_get_base_path(session
);
4588 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4590 sizeof(session
->rotation_chunk
.current_rotate_path
));
4592 ERR("Failed to copy session base path to current rotation chunk path");
4593 cmd_ret
= LTTNG_ERR_UNK
;
4598 * The currently active tracing path is now the folder we
4601 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4602 session
->rotation_chunk
.active_tracing_path
,
4603 sizeof(session
->rotation_chunk
.current_rotate_path
));
4605 ERR("Failed to copy the active tracing path to the current rotate path");
4606 cmd_ret
= LTTNG_ERR_UNK
;
4610 DBG("Current rotate path %s", session
->rotation_chunk
.current_rotate_path
);
4613 * Channels created after this point will belong to the next
4616 session
->current_archive_id
++;
4619 if (now
== (time_t) -1) {
4620 cmd_ret
= LTTNG_ERR_UNK
;
4624 /* Sample chunk bounds for roll-back in case of error. */
4625 original_last_chunk_start_ts
= session
->last_chunk_start_ts
;
4626 original_current_chunk_start_ts
= session
->current_chunk_start_ts
;
4628 session
->last_chunk_start_ts
= session
->current_chunk_start_ts
;
4629 session
->current_chunk_start_ts
= now
;
4631 timeinfo
= localtime(&now
);
4633 PERROR("Failed to sample local time in rotate session command");
4634 cmd_ret
= LTTNG_ERR_UNK
;
4637 strf_ret
= strftime(datetime
, sizeof(datetime
), "%Y%m%dT%H%M%S%z",
4640 ERR("Failed to format local time timestamp in rotate session command");
4641 cmd_ret
= LTTNG_ERR_UNK
;
4645 /* Current chunk directory, ex: 20170922-111754-42 */
4646 ret
= snprintf(session
->consumer
->chunk_path
,
4647 sizeof(session
->consumer
->chunk_path
),
4648 "%s-%" PRIu64
, datetime
,
4649 session
->current_archive_id
+ 1);
4650 if (ret
< 0 || ret
>= sizeof(session
->consumer
->chunk_path
)) {
4651 ERR("Failed to format the new chunk's directory in rotate session command");
4652 cmd_ret
= LTTNG_ERR_UNK
;
4657 * The active path for the next rotation/destroy.
4658 * Ex: ~/lttng-traces/auto-20170922-111748/20170922-111754-42
4660 ret
= snprintf(session
->rotation_chunk
.active_tracing_path
,
4661 sizeof(session
->rotation_chunk
.active_tracing_path
),
4663 session_get_base_path(session
),
4664 session
->consumer
->chunk_path
);
4665 if (ret
< 0 || ret
>= sizeof(session
->rotation_chunk
.active_tracing_path
)) {
4666 ERR("Failed to format active tracing path in rotate session command");
4667 cmd_ret
= LTTNG_ERR_UNK
;
4672 * A rotation has a local step even if the destination is a relay
4673 * daemon; the buffers must be consumed by the consumer daemon.
4675 session
->rotation_pending_local
= true;
4676 session
->rotation_pending_relay
=
4677 session_get_consumer_destination_type(session
) == CONSUMER_DST_NET
;
4678 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
4680 if (session
->kernel_session
) {
4681 ret
= lttng_strncpy(
4682 session
->kernel_session
->consumer
->chunk_path
,
4683 session
->consumer
->chunk_path
,
4684 sizeof(session
->kernel_session
->consumer
->chunk_path
));
4686 ERR("Failed to copy current chunk directory to kernel session");
4687 cmd_ret
= LTTNG_ERR_UNK
;
4691 * Create the new chunk folder, before the rotation begins so we
4692 * don't race with the consumer/tracer activity.
4694 ret
= domain_mkdir(session
->kernel_session
->consumer
, session
,
4695 session
->kernel_session
->uid
,
4696 session
->kernel_session
->gid
);
4698 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
4701 cmd_ret
= kernel_rotate_session(session
);
4702 if (cmd_ret
!= LTTNG_OK
) {
4706 if (session
->ust_session
) {
4707 ret
= lttng_strncpy(
4708 session
->ust_session
->consumer
->chunk_path
,
4709 session
->consumer
->chunk_path
,
4710 sizeof(session
->ust_session
->consumer
->chunk_path
));
4712 ERR("Failed to copy current chunk directory to userspace session");
4713 cmd_ret
= LTTNG_ERR_UNK
;
4716 ret
= domain_mkdir(session
->ust_session
->consumer
, session
,
4717 session
->ust_session
->uid
,
4718 session
->ust_session
->gid
);
4720 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
4723 cmd_ret
= ust_app_rotate_session(session
);
4724 if (cmd_ret
!= LTTNG_OK
) {
4729 ret
= timer_session_rotation_pending_check_start(session
,
4730 DEFAULT_ROTATE_PENDING_TIMER
);
4732 cmd_ret
= LTTNG_ERR_UNK
;
4736 if (!session
->active
) {
4737 session
->rotated_after_last_stop
= true;
4740 if (rotate_return
) {
4741 rotate_return
->rotation_id
= session
->current_archive_id
;
4744 ret
= notification_thread_command_session_rotation_ongoing(
4745 notification_thread_handle
,
4746 session
->name
, session
->uid
, session
->gid
,
4747 session
->current_archive_id
- 1);
4748 if (ret
!= LTTNG_OK
) {
4749 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
4754 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
4755 session
->name
, session
->current_archive_id
- 1);
4757 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
4760 session
->last_chunk_start_ts
= original_last_chunk_start_ts
;
4761 session
->current_archive_id
= original_current_chunk_start_ts
;
4762 if (session_reset_rotation_state(session
,
4763 LTTNG_ROTATION_STATE_NO_ROTATION
)) {
4764 ERR("Failed to reset rotation state of session \"%s\"",
4771 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
4773 * Check if the session has finished its rotation.
4775 * Return 0 on success or else a LTTNG_ERR code.
4777 int cmd_rotate_get_info(struct ltt_session
*session
,
4778 struct lttng_rotation_get_info_return
*info_return
,
4779 uint64_t rotation_id
)
4785 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
4786 session
->current_archive_id
);
4788 if (session
->current_archive_id
!= rotation_id
) {
4789 info_return
->status
= (int32_t) LTTNG_ROTATION_STATE_EXPIRED
;
4794 switch (session
->rotation_state
) {
4795 case LTTNG_ROTATION_STATE_ONGOING
:
4796 DBG("Reporting that rotation id %" PRIu64
" of session %s is still pending",
4797 rotation_id
, session
->name
);
4799 case LTTNG_ROTATION_STATE_COMPLETED
:
4801 char *current_tracing_path_reply
;
4802 size_t current_tracing_path_reply_len
;
4804 switch (session_get_consumer_destination_type(session
)) {
4805 case CONSUMER_DST_LOCAL
:
4806 current_tracing_path_reply
=
4807 info_return
->location
.local
.absolute_path
;
4808 current_tracing_path_reply_len
=
4809 sizeof(info_return
->location
.local
.absolute_path
);
4810 info_return
->location_type
=
4811 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
4813 case CONSUMER_DST_NET
:
4814 current_tracing_path_reply
=
4815 info_return
->location
.relay
.relative_path
;
4816 current_tracing_path_reply_len
=
4817 sizeof(info_return
->location
.relay
.relative_path
);
4818 /* Currently the only supported relay protocol. */
4819 info_return
->location
.relay
.protocol
=
4820 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
4822 ret
= lttng_strncpy(info_return
->location
.relay
.host
,
4823 session_get_net_consumer_hostname(session
),
4824 sizeof(info_return
->location
.relay
.host
));
4826 ERR("Failed to host name to rotate_get_info reply");
4827 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4828 ret
= -LTTNG_ERR_UNK
;
4832 session_get_net_consumer_ports(session
,
4833 &info_return
->location
.relay
.ports
.control
,
4834 &info_return
->location
.relay
.ports
.data
);
4835 info_return
->location_type
=
4836 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
4841 ret
= lttng_strncpy(current_tracing_path_reply
,
4842 session
->rotation_chunk
.current_rotate_path
,
4843 current_tracing_path_reply_len
);
4845 ERR("Failed to copy current tracing path to rotate_get_info reply");
4846 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4847 ret
= -LTTNG_ERR_UNK
;
4853 case LTTNG_ROTATION_STATE_ERROR
:
4854 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session %s",
4855 rotation_id
, session
->name
);
4861 info_return
->status
= (int32_t) session
->rotation_state
;
4868 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
4870 * Configure the automatic rotation parameters.
4871 * 'activate' to true means activate the rotation schedule type with 'new_value'.
4872 * 'activate' to false means deactivate the rotation schedule and validate that
4873 * 'new_value' has the same value as the currently active value.
4875 * Return 0 on success or else a positive LTTNG_ERR code.
4877 int cmd_rotation_set_schedule(struct ltt_session
*session
,
4878 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
4880 struct notification_thread_handle
*notification_thread_handle
)
4883 uint64_t *parameter_value
;
4887 DBG("Cmd rotate set schedule session %s", session
->name
);
4889 if (session
->live_timer
|| !session
->output_traces
) {
4890 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
4891 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4895 switch (schedule_type
) {
4896 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
4897 parameter_value
= &session
->rotate_size
;
4899 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
4900 parameter_value
= &session
->rotate_timer_period
;
4901 if (new_value
>= UINT_MAX
) {
4902 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
4903 new_value
, UINT_MAX
);
4904 ret
= LTTNG_ERR_INVALID
;
4909 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
4910 ret
= LTTNG_ERR_INVALID
;
4914 /* Improper use of the API. */
4915 if (new_value
== -1ULL) {
4916 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
4917 ret
= LTTNG_ERR_INVALID
;
4922 * As indicated in struct ltt_session's comments, a value of == 0 means
4923 * this schedule rotation type is not in use.
4925 * Reject the command if we were asked to activate a schedule that was
4928 if (activate
&& *parameter_value
!= 0) {
4929 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
4930 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
4935 * Reject the command if we were asked to deactivate a schedule that was
4938 if (!activate
&& *parameter_value
== 0) {
4939 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
4940 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
4945 * Reject the command if we were asked to deactivate a schedule that
4948 if (!activate
&& *parameter_value
!= new_value
) {
4949 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
4950 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
4954 *parameter_value
= activate
? new_value
: 0;
4956 switch (schedule_type
) {
4957 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
4958 if (activate
&& session
->active
) {
4960 * Only start the timer if the session is active,
4961 * otherwise it will be started when the session starts.
4963 ret
= timer_session_rotation_schedule_timer_start(
4964 session
, new_value
);
4966 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
4967 ret
= LTTNG_ERR_UNK
;
4971 ret
= timer_session_rotation_schedule_timer_stop(
4974 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
4975 ret
= LTTNG_ERR_UNK
;
4980 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
4982 ret
= subscribe_session_consumed_size_rotation(session
,
4983 new_value
, notification_thread_handle
);
4985 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
4986 ret
= LTTNG_ERR_UNK
;
4990 ret
= unsubscribe_session_consumed_size_rotation(session
,
4991 notification_thread_handle
);
4993 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
4994 ret
= LTTNG_ERR_UNK
;
5001 /* Would have been caught before. */
5013 /* Wait for a given path to be removed before continuing. */
5014 static enum lttng_error_code
wait_on_path(void *path_data
)
5016 const char *shm_path
= path_data
;
5018 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5024 ret
= stat(shm_path
, &st
);
5026 if (errno
!= ENOENT
) {
5027 PERROR("stat() returned an error while checking for the existence of the shm path");
5029 DBG("shm path no longer exists, completing the destruction of session");
5033 if (!S_ISDIR(st
.st_mode
)) {
5034 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5039 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5045 * Returns a pointer to a handler to run on completion of a command.
5046 * Returns NULL if no handler has to be run for the last command executed.
5048 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5050 struct cmd_completion_handler
*handler
= current_completion_handler
;
5052 current_completion_handler
= NULL
;
5057 * Init command subsystem.
5062 * Set network sequence index to 1 for streams to match a relayd
5063 * socket on the consumer side.
5065 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5066 relayd_net_seq_idx
= 1;
5067 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5069 DBG("Command subsystem initialized");