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 char *session_name
, char *hostname
, int session_live_timer
)
1036 struct lttcomm_relayd_sock
*rsock
= NULL
;
1037 enum lttng_error_code status
;
1039 /* Connect to relayd and make version check if uri is the control. */
1040 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1041 if (status
!= LTTNG_OK
) {
1042 goto relayd_comm_error
;
1046 /* Set the network sequence index if not set. */
1047 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1048 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1050 * Increment net_seq_idx because we are about to transfer the
1051 * new relayd socket to the consumer.
1052 * Assign unique key so the consumer can match streams.
1054 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1055 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1058 /* Send relayd socket to consumer. */
1059 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1060 relayd_uri
->stype
, session_id
,
1061 session_name
, hostname
, session_live_timer
);
1063 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1067 /* Flag that the corresponding socket was sent. */
1068 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1069 consumer_sock
->control_sock_sent
= 1;
1070 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1071 consumer_sock
->data_sock_sent
= 1;
1075 * Close socket which was dup on the consumer side. The session daemon does
1076 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1080 if (status
!= LTTNG_OK
) {
1082 * The consumer output for this session should not be used anymore
1083 * since the relayd connection failed thus making any tracing or/and
1084 * streaming not usable.
1086 consumer
->enabled
= 0;
1088 (void) relayd_close(rsock
);
1096 * Send both relayd sockets to a specific consumer and domain. This is a
1097 * helper function to facilitate sending the information to the consumer for a
1100 * The consumer socket lock must be held by the caller.
1102 * Returns LTTNG_OK, or an LTTng error code on failure.
1104 static enum lttng_error_code
send_consumer_relayd_sockets(
1105 enum lttng_domain_type domain
,
1106 unsigned int session_id
, struct consumer_output
*consumer
,
1107 struct consumer_socket
*sock
, char *session_name
,
1108 char *hostname
, int session_live_timer
)
1110 enum lttng_error_code status
= LTTNG_OK
;
1115 /* Sending control relayd socket. */
1116 if (!sock
->control_sock_sent
) {
1117 status
= send_consumer_relayd_socket(session_id
,
1118 &consumer
->dst
.net
.control
, consumer
, sock
,
1119 session_name
, hostname
, session_live_timer
);
1120 if (status
!= LTTNG_OK
) {
1125 /* Sending data relayd socket. */
1126 if (!sock
->data_sock_sent
) {
1127 status
= send_consumer_relayd_socket(session_id
,
1128 &consumer
->dst
.net
.data
, consumer
, sock
,
1129 session_name
, hostname
, session_live_timer
);
1130 if (status
!= LTTNG_OK
) {
1140 * Setup relayd connections for a tracing session. First creates the socket to
1141 * the relayd and send them to the right domain consumer. Consumer type MUST be
1144 int cmd_setup_relayd(struct ltt_session
*session
)
1147 struct ltt_ust_session
*usess
;
1148 struct ltt_kernel_session
*ksess
;
1149 struct consumer_socket
*socket
;
1150 struct lttng_ht_iter iter
;
1154 usess
= session
->ust_session
;
1155 ksess
= session
->kernel_session
;
1157 DBG("Setting relayd for session %s", session
->name
);
1161 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1162 && usess
->consumer
->enabled
) {
1163 /* For each consumer socket, send relayd sockets */
1164 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1165 socket
, node
.node
) {
1166 pthread_mutex_lock(socket
->lock
);
1167 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1168 usess
->consumer
, socket
,
1169 session
->name
, session
->hostname
,
1170 session
->live_timer
);
1171 pthread_mutex_unlock(socket
->lock
);
1172 if (ret
!= LTTNG_OK
) {
1175 /* Session is now ready for network streaming. */
1176 session
->net_handle
= 1;
1178 session
->consumer
->relay_major_version
=
1179 usess
->consumer
->relay_major_version
;
1180 session
->consumer
->relay_minor_version
=
1181 usess
->consumer
->relay_minor_version
;
1184 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1185 && ksess
->consumer
->enabled
) {
1186 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1187 socket
, node
.node
) {
1188 pthread_mutex_lock(socket
->lock
);
1189 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1190 ksess
->consumer
, socket
,
1191 session
->name
, session
->hostname
,
1192 session
->live_timer
);
1193 pthread_mutex_unlock(socket
->lock
);
1194 if (ret
!= LTTNG_OK
) {
1197 /* Session is now ready for network streaming. */
1198 session
->net_handle
= 1;
1200 session
->consumer
->relay_major_version
=
1201 ksess
->consumer
->relay_major_version
;
1202 session
->consumer
->relay_minor_version
=
1203 ksess
->consumer
->relay_minor_version
;
1212 * Start a kernel session by opening all necessary streams.
1214 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1217 struct ltt_kernel_channel
*kchan
;
1219 /* Open kernel metadata */
1220 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1221 ret
= kernel_open_metadata(ksess
);
1223 ret
= LTTNG_ERR_KERN_META_FAIL
;
1228 /* Open kernel metadata stream */
1229 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1230 ret
= kernel_open_metadata_stream(ksess
);
1232 ERR("Kernel create metadata stream failed");
1233 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1238 /* For each channel */
1239 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1240 if (kchan
->stream_count
== 0) {
1241 ret
= kernel_open_channel_stream(kchan
);
1243 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1246 /* Update the stream global counter */
1247 ksess
->stream_count_global
+= ret
;
1251 /* Setup kernel consumer socket and send fds to it */
1252 ret
= init_kernel_tracing(ksess
);
1254 ret
= LTTNG_ERR_KERN_START_FAIL
;
1258 /* This start the kernel tracing */
1259 ret
= kernel_start_session(ksess
);
1261 ret
= LTTNG_ERR_KERN_START_FAIL
;
1265 /* Quiescent wait after starting trace */
1266 kernel_wait_quiescent(wpipe
);
1277 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1279 int cmd_disable_channel(struct ltt_session
*session
,
1280 enum lttng_domain_type domain
, char *channel_name
)
1283 struct ltt_ust_session
*usess
;
1285 usess
= session
->ust_session
;
1290 case LTTNG_DOMAIN_KERNEL
:
1292 ret
= channel_kernel_disable(session
->kernel_session
,
1294 if (ret
!= LTTNG_OK
) {
1298 kernel_wait_quiescent(kernel_tracer_fd
);
1301 case LTTNG_DOMAIN_UST
:
1303 struct ltt_ust_channel
*uchan
;
1304 struct lttng_ht
*chan_ht
;
1306 chan_ht
= usess
->domain_global
.channels
;
1308 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1309 if (uchan
== NULL
) {
1310 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1314 ret
= channel_ust_disable(usess
, uchan
);
1315 if (ret
!= LTTNG_OK
) {
1321 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1333 * Command LTTNG_TRACK_PID processed by the client thread.
1335 * Called with session lock held.
1337 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1345 case LTTNG_DOMAIN_KERNEL
:
1347 struct ltt_kernel_session
*ksess
;
1349 ksess
= session
->kernel_session
;
1351 ret
= kernel_track_pid(ksess
, pid
);
1352 if (ret
!= LTTNG_OK
) {
1356 kernel_wait_quiescent(kernel_tracer_fd
);
1359 case LTTNG_DOMAIN_UST
:
1361 struct ltt_ust_session
*usess
;
1363 usess
= session
->ust_session
;
1365 ret
= trace_ust_track_pid(usess
, pid
);
1366 if (ret
!= LTTNG_OK
) {
1372 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1384 * Command LTTNG_UNTRACK_PID processed by the client thread.
1386 * Called with session lock held.
1388 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1396 case LTTNG_DOMAIN_KERNEL
:
1398 struct ltt_kernel_session
*ksess
;
1400 ksess
= session
->kernel_session
;
1402 ret
= kernel_untrack_pid(ksess
, pid
);
1403 if (ret
!= LTTNG_OK
) {
1407 kernel_wait_quiescent(kernel_tracer_fd
);
1410 case LTTNG_DOMAIN_UST
:
1412 struct ltt_ust_session
*usess
;
1414 usess
= session
->ust_session
;
1416 ret
= trace_ust_untrack_pid(usess
, pid
);
1417 if (ret
!= LTTNG_OK
) {
1423 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1435 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1437 * The wpipe arguments is used as a notifier for the kernel thread.
1439 int cmd_enable_channel(struct ltt_session
*session
,
1440 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1443 struct ltt_ust_session
*usess
= session
->ust_session
;
1444 struct lttng_ht
*chan_ht
;
1451 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1453 /* Validate channel name */
1454 if (attr
->name
[0] == '.' ||
1455 memchr(attr
->name
, '/', len
) != NULL
) {
1456 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1460 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1465 * Don't try to enable a channel if the session has been started at
1466 * some point in time before. The tracer does not allow it.
1468 if (session
->has_been_started
) {
1469 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1474 * If the session is a live session, remove the switch timer, the
1475 * live timer does the same thing but sends also synchronisation
1476 * beacons for inactive streams.
1478 if (session
->live_timer
> 0) {
1479 attr
->attr
.live_timer_interval
= session
->live_timer
;
1480 attr
->attr
.switch_timer_interval
= 0;
1483 /* Check for feature support */
1484 switch (domain
->type
) {
1485 case LTTNG_DOMAIN_KERNEL
:
1487 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd
) != 1) {
1488 /* Sampling position of buffer is not supported */
1489 WARN("Kernel tracer does not support buffer monitoring. "
1490 "Setting the monitor interval timer to 0 "
1491 "(disabled) for channel '%s' of session '%s'",
1492 attr
-> name
, session
->name
);
1493 lttng_channel_set_monitor_timer_interval(attr
, 0);
1497 case LTTNG_DOMAIN_UST
:
1499 case LTTNG_DOMAIN_JUL
:
1500 case LTTNG_DOMAIN_LOG4J
:
1501 case LTTNG_DOMAIN_PYTHON
:
1502 if (!agent_tracing_is_enabled()) {
1503 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1504 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1509 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1513 switch (domain
->type
) {
1514 case LTTNG_DOMAIN_KERNEL
:
1516 struct ltt_kernel_channel
*kchan
;
1518 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1519 session
->kernel_session
);
1520 if (kchan
== NULL
) {
1521 if (session
->snapshot
.nb_output
> 0 ||
1522 session
->snapshot_mode
) {
1523 /* Enforce mmap output for snapshot sessions. */
1524 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1526 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1527 if (attr
->name
[0] != '\0') {
1528 session
->kernel_session
->has_non_default_channel
= 1;
1531 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1534 if (ret
!= LTTNG_OK
) {
1538 kernel_wait_quiescent(kernel_tracer_fd
);
1541 case LTTNG_DOMAIN_UST
:
1542 case LTTNG_DOMAIN_JUL
:
1543 case LTTNG_DOMAIN_LOG4J
:
1544 case LTTNG_DOMAIN_PYTHON
:
1546 struct ltt_ust_channel
*uchan
;
1551 * Current agent implementation limitations force us to allow
1552 * only one channel at once in "agent" subdomains. Each
1553 * subdomain has a default channel name which must be strictly
1556 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1557 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1558 LTTNG_SYMBOL_NAME_LEN
)) {
1559 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1562 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1563 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1564 LTTNG_SYMBOL_NAME_LEN
)) {
1565 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1568 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1569 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1570 LTTNG_SYMBOL_NAME_LEN
)) {
1571 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1576 chan_ht
= usess
->domain_global
.channels
;
1578 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1579 if (uchan
== NULL
) {
1580 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1581 if (attr
->name
[0] != '\0') {
1582 usess
->has_non_default_channel
= 1;
1585 ret
= channel_ust_enable(usess
, uchan
);
1590 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1594 if (ret
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1595 session
->has_non_mmap_channel
= true;
1604 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1606 int cmd_disable_event(struct ltt_session
*session
,
1607 enum lttng_domain_type domain
, char *channel_name
,
1608 struct lttng_event
*event
)
1613 DBG("Disable event command for event \'%s\'", event
->name
);
1615 event_name
= event
->name
;
1617 /* Error out on unhandled search criteria */
1618 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1619 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1620 ret
= LTTNG_ERR_UNK
;
1627 case LTTNG_DOMAIN_KERNEL
:
1629 struct ltt_kernel_channel
*kchan
;
1630 struct ltt_kernel_session
*ksess
;
1632 ksess
= session
->kernel_session
;
1635 * If a non-default channel has been created in the
1636 * session, explicitely require that -c chan_name needs
1639 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1640 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1644 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1645 if (kchan
== NULL
) {
1646 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1650 switch (event
->type
) {
1651 case LTTNG_EVENT_ALL
:
1652 case LTTNG_EVENT_TRACEPOINT
:
1653 case LTTNG_EVENT_SYSCALL
:
1654 case LTTNG_EVENT_PROBE
:
1655 case LTTNG_EVENT_FUNCTION
:
1656 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1657 if (event_name
[0] == '\0') {
1658 ret
= event_kernel_disable_event(kchan
,
1661 ret
= event_kernel_disable_event(kchan
,
1662 event_name
, event
->type
);
1664 if (ret
!= LTTNG_OK
) {
1669 ret
= LTTNG_ERR_UNK
;
1673 kernel_wait_quiescent(kernel_tracer_fd
);
1676 case LTTNG_DOMAIN_UST
:
1678 struct ltt_ust_channel
*uchan
;
1679 struct ltt_ust_session
*usess
;
1681 usess
= session
->ust_session
;
1683 if (validate_ust_event_name(event_name
)) {
1684 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1689 * If a non-default channel has been created in the
1690 * session, explicitly require that -c chan_name needs
1693 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1694 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1698 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1700 if (uchan
== NULL
) {
1701 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1705 switch (event
->type
) {
1706 case LTTNG_EVENT_ALL
:
1708 * An empty event name means that everything
1709 * should be disabled.
1711 if (event
->name
[0] == '\0') {
1712 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1714 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1717 if (ret
!= LTTNG_OK
) {
1722 ret
= LTTNG_ERR_UNK
;
1726 DBG3("Disable UST event %s in channel %s completed", event_name
,
1730 case LTTNG_DOMAIN_LOG4J
:
1731 case LTTNG_DOMAIN_JUL
:
1732 case LTTNG_DOMAIN_PYTHON
:
1735 struct ltt_ust_session
*usess
= session
->ust_session
;
1739 switch (event
->type
) {
1740 case LTTNG_EVENT_ALL
:
1743 ret
= LTTNG_ERR_UNK
;
1747 agt
= trace_ust_find_agent(usess
, domain
);
1749 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1753 * An empty event name means that everything
1754 * should be disabled.
1756 if (event
->name
[0] == '\0') {
1757 ret
= event_agent_disable_all(usess
, agt
);
1759 ret
= event_agent_disable(usess
, agt
, event_name
);
1761 if (ret
!= LTTNG_OK
) {
1768 ret
= LTTNG_ERR_UND
;
1781 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1783 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1784 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1786 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1787 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1790 * Don't try to add a context if the session has been started at
1791 * some point in time before. The tracer does not allow it and would
1792 * result in a corrupted trace.
1794 if (session
->has_been_started
) {
1795 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1799 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1800 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1801 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1805 case LTTNG_DOMAIN_KERNEL
:
1806 assert(session
->kernel_session
);
1808 if (session
->kernel_session
->channel_count
== 0) {
1809 /* Create default channel */
1810 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1811 if (ret
!= LTTNG_OK
) {
1814 chan_kern_created
= 1;
1816 /* Add kernel context to kernel tracer */
1817 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1818 if (ret
!= LTTNG_OK
) {
1822 case LTTNG_DOMAIN_JUL
:
1823 case LTTNG_DOMAIN_LOG4J
:
1826 * Validate channel name.
1827 * If no channel name is given and the domain is JUL or LOG4J,
1828 * set it to the appropriate domain-specific channel name. If
1829 * a name is provided but does not match the expexted channel
1830 * name, return an error.
1832 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1833 strcmp(channel_name
,
1834 DEFAULT_JUL_CHANNEL_NAME
)) {
1835 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1837 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1838 strcmp(channel_name
,
1839 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1840 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1843 /* break is _not_ missing here. */
1845 case LTTNG_DOMAIN_UST
:
1847 struct ltt_ust_session
*usess
= session
->ust_session
;
1848 unsigned int chan_count
;
1852 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1853 if (chan_count
== 0) {
1854 struct lttng_channel
*attr
;
1855 /* Create default channel */
1856 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1858 ret
= LTTNG_ERR_FATAL
;
1862 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1863 if (ret
!= LTTNG_OK
) {
1867 channel_attr_destroy(attr
);
1868 chan_ust_created
= 1;
1871 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1872 free(app_ctx_provider_name
);
1874 app_ctx_name
= NULL
;
1875 app_ctx_provider_name
= NULL
;
1876 if (ret
!= LTTNG_OK
) {
1882 ret
= LTTNG_ERR_UND
;
1890 if (chan_kern_created
) {
1891 struct ltt_kernel_channel
*kchan
=
1892 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1893 session
->kernel_session
);
1894 /* Created previously, this should NOT fail. */
1896 kernel_destroy_channel(kchan
);
1899 if (chan_ust_created
) {
1900 struct ltt_ust_channel
*uchan
=
1901 trace_ust_find_channel_by_name(
1902 session
->ust_session
->domain_global
.channels
,
1903 DEFAULT_CHANNEL_NAME
);
1904 /* Created previously, this should NOT fail. */
1906 /* Remove from the channel list of the session. */
1907 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1909 trace_ust_destroy_channel(uchan
);
1912 free(app_ctx_provider_name
);
1917 static inline bool name_starts_with(const char *name
, const char *prefix
)
1919 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1921 return !strncmp(name
, prefix
, max_cmp_len
);
1924 /* Perform userspace-specific event name validation */
1925 static int validate_ust_event_name(const char *name
)
1935 * Check name against all internal UST event component namespaces used
1938 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1939 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1940 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1949 * Internal version of cmd_enable_event() with a supplemental
1950 * "internal_event" flag which is used to enable internal events which should
1951 * be hidden from clients. Such events are used in the agent implementation to
1952 * enable the events through which all "agent" events are funeled.
1954 static int _cmd_enable_event(struct ltt_session
*session
,
1955 struct lttng_domain
*domain
,
1956 char *channel_name
, struct lttng_event
*event
,
1957 char *filter_expression
,
1958 struct lttng_filter_bytecode
*filter
,
1959 struct lttng_event_exclusion
*exclusion
,
1960 int wpipe
, bool internal_event
)
1962 int ret
= 0, channel_created
= 0;
1963 struct lttng_channel
*attr
= NULL
;
1967 assert(channel_name
);
1969 /* If we have a filter, we must have its filter expression */
1970 assert(!(!!filter_expression
^ !!filter
));
1972 /* Normalize event name as a globbing pattern */
1973 strutils_normalize_star_glob_pattern(event
->name
);
1975 /* Normalize exclusion names as globbing patterns */
1979 for (i
= 0; i
< exclusion
->count
; i
++) {
1980 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
1982 strutils_normalize_star_glob_pattern(name
);
1986 DBG("Enable event command for event \'%s\'", event
->name
);
1990 switch (domain
->type
) {
1991 case LTTNG_DOMAIN_KERNEL
:
1993 struct ltt_kernel_channel
*kchan
;
1996 * If a non-default channel has been created in the
1997 * session, explicitely require that -c chan_name needs
2000 if (session
->kernel_session
->has_non_default_channel
2001 && channel_name
[0] == '\0') {
2002 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2006 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2007 session
->kernel_session
);
2008 if (kchan
== NULL
) {
2009 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2010 LTTNG_BUFFER_GLOBAL
);
2012 ret
= LTTNG_ERR_FATAL
;
2015 if (lttng_strncpy(attr
->name
, channel_name
,
2016 sizeof(attr
->name
))) {
2017 ret
= LTTNG_ERR_INVALID
;
2021 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2022 if (ret
!= LTTNG_OK
) {
2025 channel_created
= 1;
2028 /* Get the newly created kernel channel pointer */
2029 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2030 session
->kernel_session
);
2031 if (kchan
== NULL
) {
2032 /* This sould not happen... */
2033 ret
= LTTNG_ERR_FATAL
;
2037 switch (event
->type
) {
2038 case LTTNG_EVENT_ALL
:
2040 char *filter_expression_a
= NULL
;
2041 struct lttng_filter_bytecode
*filter_a
= NULL
;
2044 * We need to duplicate filter_expression and filter,
2045 * because ownership is passed to first enable
2048 if (filter_expression
) {
2049 filter_expression_a
= strdup(filter_expression
);
2050 if (!filter_expression_a
) {
2051 ret
= LTTNG_ERR_FATAL
;
2056 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2058 free(filter_expression_a
);
2059 ret
= LTTNG_ERR_FATAL
;
2062 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2064 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2065 ret
= event_kernel_enable_event(kchan
, event
,
2066 filter_expression
, filter
);
2067 /* We have passed ownership */
2068 filter_expression
= NULL
;
2070 if (ret
!= LTTNG_OK
) {
2071 if (channel_created
) {
2072 /* Let's not leak a useless channel. */
2073 kernel_destroy_channel(kchan
);
2075 free(filter_expression_a
);
2079 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2080 ret
= event_kernel_enable_event(kchan
, event
,
2081 filter_expression_a
, filter_a
);
2082 /* We have passed ownership */
2083 filter_expression_a
= NULL
;
2085 if (ret
!= LTTNG_OK
) {
2090 case LTTNG_EVENT_PROBE
:
2091 case LTTNG_EVENT_USERSPACE_PROBE
:
2092 case LTTNG_EVENT_FUNCTION
:
2093 case LTTNG_EVENT_FUNCTION_ENTRY
:
2094 case LTTNG_EVENT_TRACEPOINT
:
2095 ret
= event_kernel_enable_event(kchan
, event
,
2096 filter_expression
, filter
);
2097 /* We have passed ownership */
2098 filter_expression
= NULL
;
2100 if (ret
!= LTTNG_OK
) {
2101 if (channel_created
) {
2102 /* Let's not leak a useless channel. */
2103 kernel_destroy_channel(kchan
);
2108 case LTTNG_EVENT_SYSCALL
:
2109 ret
= event_kernel_enable_event(kchan
, event
,
2110 filter_expression
, filter
);
2111 /* We have passed ownership */
2112 filter_expression
= NULL
;
2114 if (ret
!= LTTNG_OK
) {
2119 ret
= LTTNG_ERR_UNK
;
2123 kernel_wait_quiescent(kernel_tracer_fd
);
2126 case LTTNG_DOMAIN_UST
:
2128 struct ltt_ust_channel
*uchan
;
2129 struct ltt_ust_session
*usess
= session
->ust_session
;
2134 * If a non-default channel has been created in the
2135 * session, explicitely require that -c chan_name needs
2138 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2139 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2143 /* Get channel from global UST domain */
2144 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2146 if (uchan
== NULL
) {
2147 /* Create default channel */
2148 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2149 usess
->buffer_type
);
2151 ret
= LTTNG_ERR_FATAL
;
2154 if (lttng_strncpy(attr
->name
, channel_name
,
2155 sizeof(attr
->name
))) {
2156 ret
= LTTNG_ERR_INVALID
;
2160 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2161 if (ret
!= LTTNG_OK
) {
2165 /* Get the newly created channel reference back */
2166 uchan
= trace_ust_find_channel_by_name(
2167 usess
->domain_global
.channels
, channel_name
);
2171 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2173 * Don't allow users to add UST events to channels which
2174 * are assigned to a userspace subdomain (JUL, Log4J,
2177 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2181 if (!internal_event
) {
2183 * Ensure the event name is not reserved for internal
2186 ret
= validate_ust_event_name(event
->name
);
2188 WARN("Userspace event name %s failed validation.",
2190 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2195 /* At this point, the session and channel exist on the tracer */
2196 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2197 filter_expression
, filter
, exclusion
,
2199 /* We have passed ownership */
2200 filter_expression
= NULL
;
2203 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2204 goto already_enabled
;
2205 } else if (ret
!= LTTNG_OK
) {
2210 case LTTNG_DOMAIN_LOG4J
:
2211 case LTTNG_DOMAIN_JUL
:
2212 case LTTNG_DOMAIN_PYTHON
:
2214 const char *default_event_name
, *default_chan_name
;
2216 struct lttng_event uevent
;
2217 struct lttng_domain tmp_dom
;
2218 struct ltt_ust_session
*usess
= session
->ust_session
;
2222 if (!agent_tracing_is_enabled()) {
2223 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2224 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2228 agt
= trace_ust_find_agent(usess
, domain
->type
);
2230 agt
= agent_create(domain
->type
);
2232 ret
= LTTNG_ERR_NOMEM
;
2235 agent_add(agt
, usess
->agents
);
2238 /* Create the default tracepoint. */
2239 memset(&uevent
, 0, sizeof(uevent
));
2240 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2241 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2242 default_event_name
= event_get_default_agent_ust_name(
2244 if (!default_event_name
) {
2245 ret
= LTTNG_ERR_FATAL
;
2248 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2249 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2252 * The domain type is changed because we are about to enable the
2253 * default channel and event for the JUL domain that are hardcoded.
2254 * This happens in the UST domain.
2256 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2257 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2259 switch (domain
->type
) {
2260 case LTTNG_DOMAIN_LOG4J
:
2261 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2263 case LTTNG_DOMAIN_JUL
:
2264 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2266 case LTTNG_DOMAIN_PYTHON
:
2267 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2270 /* The switch/case we are in makes this impossible */
2275 char *filter_expression_copy
= NULL
;
2276 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2279 const size_t filter_size
= sizeof(
2280 struct lttng_filter_bytecode
)
2283 filter_copy
= zmalloc(filter_size
);
2285 ret
= LTTNG_ERR_NOMEM
;
2288 memcpy(filter_copy
, filter
, filter_size
);
2290 filter_expression_copy
=
2291 strdup(filter_expression
);
2292 if (!filter_expression
) {
2293 ret
= LTTNG_ERR_NOMEM
;
2296 if (!filter_expression_copy
|| !filter_copy
) {
2297 free(filter_expression_copy
);
2303 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2304 (char *) default_chan_name
,
2305 &uevent
, filter_expression_copy
,
2306 filter_copy
, NULL
, wpipe
);
2309 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2310 goto already_enabled
;
2311 } else if (ret
!= LTTNG_OK
) {
2315 /* The wild card * means that everything should be enabled. */
2316 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2317 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2320 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2324 filter_expression
= NULL
;
2325 if (ret
!= LTTNG_OK
) {
2332 ret
= LTTNG_ERR_UND
;
2340 free(filter_expression
);
2343 channel_attr_destroy(attr
);
2349 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2350 * We own filter, exclusion, and filter_expression.
2352 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2353 char *channel_name
, struct lttng_event
*event
,
2354 char *filter_expression
,
2355 struct lttng_filter_bytecode
*filter
,
2356 struct lttng_event_exclusion
*exclusion
,
2359 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2360 filter_expression
, filter
, exclusion
, wpipe
, false);
2364 * Enable an event which is internal to LTTng. An internal should
2365 * never be made visible to clients and are immune to checks such as
2368 static int cmd_enable_event_internal(struct ltt_session
*session
,
2369 struct lttng_domain
*domain
,
2370 char *channel_name
, struct lttng_event
*event
,
2371 char *filter_expression
,
2372 struct lttng_filter_bytecode
*filter
,
2373 struct lttng_event_exclusion
*exclusion
,
2376 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2377 filter_expression
, filter
, exclusion
, wpipe
, true);
2381 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2383 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2384 struct lttng_event
**events
)
2387 ssize_t nb_events
= 0;
2390 case LTTNG_DOMAIN_KERNEL
:
2391 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2392 if (nb_events
< 0) {
2393 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2397 case LTTNG_DOMAIN_UST
:
2398 nb_events
= ust_app_list_events(events
);
2399 if (nb_events
< 0) {
2400 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2404 case LTTNG_DOMAIN_LOG4J
:
2405 case LTTNG_DOMAIN_JUL
:
2406 case LTTNG_DOMAIN_PYTHON
:
2407 nb_events
= agent_list_events(events
, domain
);
2408 if (nb_events
< 0) {
2409 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2414 ret
= LTTNG_ERR_UND
;
2421 /* Return negative value to differentiate return code */
2426 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2428 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2429 struct lttng_event_field
**fields
)
2432 ssize_t nb_fields
= 0;
2435 case LTTNG_DOMAIN_UST
:
2436 nb_fields
= ust_app_list_event_fields(fields
);
2437 if (nb_fields
< 0) {
2438 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2442 case LTTNG_DOMAIN_KERNEL
:
2443 default: /* fall-through */
2444 ret
= LTTNG_ERR_UND
;
2451 /* Return negative value to differentiate return code */
2455 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2457 return syscall_table_list(events
);
2461 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2463 * Called with session lock held.
2465 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2466 enum lttng_domain_type domain
, int32_t **pids
)
2469 ssize_t nr_pids
= 0;
2472 case LTTNG_DOMAIN_KERNEL
:
2474 struct ltt_kernel_session
*ksess
;
2476 ksess
= session
->kernel_session
;
2477 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2479 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2484 case LTTNG_DOMAIN_UST
:
2486 struct ltt_ust_session
*usess
;
2488 usess
= session
->ust_session
;
2489 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2491 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2496 case LTTNG_DOMAIN_LOG4J
:
2497 case LTTNG_DOMAIN_JUL
:
2498 case LTTNG_DOMAIN_PYTHON
:
2500 ret
= LTTNG_ERR_UND
;
2507 /* Return negative value to differentiate return code */
2512 int domain_mkdir(const struct consumer_output
*output
,
2513 const struct ltt_session
*session
,
2514 uid_t uid
, gid_t gid
)
2516 struct consumer_socket
*socket
;
2517 struct lttng_ht_iter iter
;
2519 char path
[LTTNG_PATH_MAX
];
2521 if (!output
|| !output
->socks
) {
2522 ERR("No consumer output found");
2527 ret
= snprintf(path
, sizeof(path
), "%s/%s%s",
2528 session_get_base_path(session
),
2530 output
->domain_subdir
);
2531 if (ret
< 0 || ret
>= LTTNG_PATH_MAX
) {
2532 ERR("Failed to format path new chunk domain path");
2537 DBG("Domain mkdir %s for session %" PRIu64
, path
, session
->id
);
2540 * We have to iterate to find a socket, but we only need to send the
2541 * rename command to one consumer, so we break after the first one.
2543 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
2544 pthread_mutex_lock(socket
->lock
);
2545 ret
= consumer_mkdir(socket
, session
->id
, output
, path
, uid
, gid
);
2546 pthread_mutex_unlock(socket
->lock
);
2548 ERR("Failed to create directory at \"%s\"", path
);
2564 * Command LTTNG_START_TRACE processed by the client thread.
2566 * Called with session mutex held.
2568 int cmd_start_trace(struct ltt_session
*session
)
2570 enum lttng_error_code ret
;
2571 unsigned long nb_chan
= 0;
2572 struct ltt_kernel_session
*ksession
;
2573 struct ltt_ust_session
*usess
;
2577 /* Ease our life a bit ;) */
2578 ksession
= session
->kernel_session
;
2579 usess
= session
->ust_session
;
2581 /* Is the session already started? */
2582 if (session
->active
) {
2583 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2588 * Starting a session without channel is useless since after that it's not
2589 * possible to enable channel thus inform the client.
2591 if (usess
&& usess
->domain_global
.channels
) {
2592 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2595 nb_chan
+= ksession
->channel_count
;
2598 ret
= LTTNG_ERR_NO_CHANNEL
;
2602 if (!session
->has_been_started
&& session
->output_traces
) {
2603 ret
= session_switch_trace_chunk(session
, NULL
, NULL
);
2604 if (ret
!= LTTNG_OK
) {
2609 /* Kernel tracing */
2610 if (ksession
!= NULL
) {
2611 DBG("Start kernel tracing session %s", session
->name
);
2612 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2613 if (ret
!= LTTNG_OK
) {
2618 /* Flag session that trace should start automatically */
2620 int int_ret
= ust_app_start_trace_all(usess
);
2623 ret
= LTTNG_ERR_UST_START_FAIL
;
2628 /* Flag this after a successful start. */
2629 session
->has_been_started
= 1;
2630 session
->active
= 1;
2633 * Clear the flag that indicates that a rotation was done while the
2634 * session was stopped.
2636 session
->rotated_after_last_stop
= false;
2638 if (session
->rotate_timer_period
) {
2639 int int_ret
= timer_session_rotation_schedule_timer_start(
2640 session
, session
->rotate_timer_period
);
2643 ERR("Failed to enable rotate timer");
2644 ret
= LTTNG_ERR_UNK
;
2656 * Command LTTNG_STOP_TRACE processed by the client thread.
2658 int cmd_stop_trace(struct ltt_session
*session
)
2661 struct ltt_kernel_channel
*kchan
;
2662 struct ltt_kernel_session
*ksession
;
2663 struct ltt_ust_session
*usess
;
2664 bool error_occurred
= false;
2668 DBG("Begin stop session %s (id %" PRIu64
")", session
->name
, session
->id
);
2670 ksession
= session
->kernel_session
;
2671 usess
= session
->ust_session
;
2673 /* Session is not active. Skip everythong and inform the client. */
2674 if (!session
->active
) {
2675 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2680 if (ksession
&& ksession
->active
) {
2681 DBG("Stop kernel tracing");
2683 ret
= kernel_stop_session(ksession
);
2685 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2689 kernel_wait_quiescent(kernel_tracer_fd
);
2691 /* Flush metadata after stopping (if exists) */
2692 if (ksession
->metadata_stream_fd
>= 0) {
2693 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2695 ERR("Kernel metadata flush failed");
2699 /* Flush all buffers after stopping */
2700 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2701 ret
= kernel_flush_buffer(kchan
);
2703 ERR("Kernel flush buffer error");
2707 ksession
->active
= 0;
2708 DBG("Kernel session stopped %s (id %" PRIu64
")", session
->name
,
2712 if (usess
&& usess
->active
) {
2713 ret
= ust_app_stop_trace_all(usess
);
2715 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2720 /* Flag inactive after a successful stop. */
2721 session
->active
= 0;
2722 ret
= !error_occurred
? LTTNG_OK
: LTTNG_ERR_UNK
;
2729 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2731 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2732 struct lttng_uri
*uris
)
2735 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2736 struct ltt_ust_session
*usess
= session
->ust_session
;
2742 /* Can't set consumer URI if the session is active. */
2743 if (session
->active
) {
2744 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2748 /* Set the "global" consumer URIs */
2749 for (i
= 0; i
< nb_uri
; i
++) {
2750 ret
= add_uri_to_consumer(session
,
2752 &uris
[i
], LTTNG_DOMAIN_NONE
);
2753 if (ret
!= LTTNG_OK
) {
2758 /* Set UST session URIs */
2759 if (session
->ust_session
) {
2760 for (i
= 0; i
< nb_uri
; i
++) {
2761 ret
= add_uri_to_consumer(session
,
2762 session
->ust_session
->consumer
,
2763 &uris
[i
], LTTNG_DOMAIN_UST
);
2764 if (ret
!= LTTNG_OK
) {
2770 /* Set kernel session URIs */
2771 if (session
->kernel_session
) {
2772 for (i
= 0; i
< nb_uri
; i
++) {
2773 ret
= add_uri_to_consumer(session
,
2774 session
->kernel_session
->consumer
,
2775 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2776 if (ret
!= LTTNG_OK
) {
2783 * Make sure to set the session in output mode after we set URI since a
2784 * session can be created without URL (thus flagged in no output mode).
2786 session
->output_traces
= 1;
2788 ksess
->output_traces
= 1;
2792 usess
->output_traces
= 1;
2803 enum lttng_error_code
set_session_output_from_descriptor(
2804 struct ltt_session
*session
,
2805 const struct lttng_session_descriptor
*descriptor
)
2808 enum lttng_error_code ret_code
= LTTNG_OK
;
2809 enum lttng_session_descriptor_type session_type
=
2810 lttng_session_descriptor_get_type(descriptor
);
2811 enum lttng_session_descriptor_output_type output_type
=
2812 lttng_session_descriptor_get_output_type(descriptor
);
2813 struct lttng_uri uris
[2] = {};
2814 size_t uri_count
= 0;
2816 switch (output_type
) {
2817 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2819 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2820 lttng_session_descriptor_get_local_output_uri(descriptor
,
2824 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2825 lttng_session_descriptor_get_network_output_uris(descriptor
,
2826 &uris
[0], &uris
[1]);
2830 ret_code
= LTTNG_ERR_INVALID
;
2834 switch (session_type
) {
2835 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2837 struct snapshot_output
*new_output
= NULL
;
2839 new_output
= snapshot_output_alloc();
2841 ret_code
= LTTNG_ERR_NOMEM
;
2845 ret
= snapshot_output_init_with_uri(session
,
2846 DEFAULT_SNAPSHOT_MAX_SIZE
,
2847 NULL
, uris
, uri_count
, session
->consumer
,
2848 new_output
, &session
->snapshot
);
2850 ret_code
= (ret
== -ENOMEM
) ?
2851 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2852 snapshot_output_destroy(new_output
);
2855 snapshot_add_output(&session
->snapshot
, new_output
);
2858 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2859 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2861 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2865 ret_code
= LTTNG_ERR_INVALID
;
2873 enum lttng_error_code
cmd_create_session_from_descriptor(
2874 struct lttng_session_descriptor
*descriptor
,
2875 const lttng_sock_cred
*creds
,
2876 const char *home_path
)
2879 enum lttng_error_code ret_code
;
2880 const char *session_name
;
2881 struct ltt_session
*new_session
= NULL
;
2882 enum lttng_session_descriptor_status descriptor_status
;
2884 session_lock_list();
2886 if (*home_path
!= '/') {
2887 ERR("Home path provided by client is not absolute");
2888 ret_code
= LTTNG_ERR_INVALID
;
2893 descriptor_status
= lttng_session_descriptor_get_session_name(
2894 descriptor
, &session_name
);
2895 switch (descriptor_status
) {
2896 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
2898 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
2899 session_name
= NULL
;
2902 ret_code
= LTTNG_ERR_INVALID
;
2905 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
2907 if (ret_code
!= LTTNG_OK
) {
2911 if (!session_name
) {
2912 ret
= lttng_session_descriptor_set_session_name(descriptor
,
2915 ret_code
= LTTNG_ERR_SESSION_FAIL
;
2920 if (!lttng_session_descriptor_is_output_destination_initialized(
2923 * Only include the session's creation time in the output
2924 * destination if the name of the session itself was
2925 * not auto-generated.
2927 ret_code
= lttng_session_descriptor_set_default_output(
2929 session_name
? &new_session
->creation_time
: NULL
,
2931 if (ret_code
!= LTTNG_OK
) {
2935 new_session
->has_user_specified_directory
=
2936 lttng_session_descriptor_has_output_directory(
2940 switch (lttng_session_descriptor_get_type(descriptor
)) {
2941 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2942 new_session
->snapshot_mode
= 1;
2944 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2945 new_session
->live_timer
=
2946 lttng_session_descriptor_live_get_timer_interval(
2953 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
2954 if (ret_code
!= LTTNG_OK
) {
2957 new_session
->consumer
->enabled
= 1;
2958 ret_code
= LTTNG_OK
;
2960 /* Release reference provided by the session_create function. */
2961 session_put(new_session
);
2962 if (ret_code
!= LTTNG_OK
&& new_session
) {
2963 /* Release the global reference on error. */
2964 session_destroy(new_session
);
2966 session_unlock_list();
2970 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
2971 struct lttng_session_descriptor
**return_descriptor
)
2974 size_t payload_size
;
2975 struct lttng_dynamic_buffer payload
;
2976 struct lttng_buffer_view home_dir_view
;
2977 struct lttng_buffer_view session_descriptor_view
;
2978 struct lttng_session_descriptor
*session_descriptor
= NULL
;
2979 enum lttng_error_code ret_code
;
2981 lttng_dynamic_buffer_init(&payload
);
2982 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
2984 ret_code
= LTTNG_ERR_INVALID
;
2987 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
2988 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
2989 ret_code
= LTTNG_ERR_INVALID
;
2993 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
2994 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
2995 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
2997 ret_code
= LTTNG_ERR_NOMEM
;
3001 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3003 ERR("Reception of session descriptor failed, aborting.");
3004 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3008 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3011 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3012 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3014 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3015 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3017 ret
= lttng_session_descriptor_create_from_buffer(
3018 &session_descriptor_view
, &session_descriptor
);
3020 ERR("Failed to create session descriptor from payload of \"create session\" command");
3021 ret_code
= LTTNG_ERR_INVALID
;
3026 * Sets the descriptor's auto-generated properties (name, output) if
3029 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3031 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3032 if (ret_code
!= LTTNG_OK
) {
3036 ret_code
= LTTNG_OK
;
3037 *return_descriptor
= session_descriptor
;
3038 session_descriptor
= NULL
;
3040 lttng_dynamic_buffer_reset(&payload
);
3041 lttng_session_descriptor_destroy(session_descriptor
);
3046 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3048 * Called with session lock held.
3050 int cmd_destroy_session(struct ltt_session
*session
,
3051 struct notification_thread_handle
*notification_thread_handle
)
3058 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3060 if (session
->rotation_schedule_timer_enabled
) {
3061 if (timer_session_rotation_schedule_timer_stop(
3063 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3068 if (session
->rotate_size
) {
3069 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3070 session
->rotate_size
= 0;
3073 if (session
->current_archive_id
!= 0) {
3074 if (!session
->rotated_after_last_stop
) {
3075 ret
= cmd_rotate_session(session
, NULL
);
3076 if (ret
!= LTTNG_OK
) {
3077 ERR("Failed to perform an implicit rotation as part of the rotation: %s", lttng_strerror(-ret
));
3081 * Rename the active chunk to ensure it has a name
3082 * of the form ts_begin-ts_end-id.
3084 * Note that no trace data has been produced since
3085 * the last rotation; the directory should be
3088 ret
= rename_active_chunk(session
);
3090 ERR("Failed to rename active chunk during the destruction of session \"%s\"",
3096 if (session
->shm_path
[0]) {
3098 * When a session is created with an explicit shm_path,
3099 * the consumer daemon will create its shared memory files
3100 * at that location and will *not* unlink them. This is normal
3101 * as the intention of that feature is to make it possible
3102 * to retrieve the content of those files should a crash occur.
3104 * To ensure the content of those files can be used, the
3105 * sessiond daemon will replicate the content of the metadata
3106 * cache in a metadata file.
3108 * On clean-up, it is expected that the consumer daemon will
3109 * unlink the shared memory files and that the session daemon
3110 * will unlink the metadata file. Then, the session's directory
3111 * in the shm path can be removed.
3113 * Unfortunately, a flaw in the design of the sessiond's and
3114 * consumerd's tear down of channels makes it impossible to
3115 * determine when the sessiond _and_ the consumerd have both
3116 * destroyed their representation of a channel. For one, the
3117 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3118 * callbacks in both daemons.
3120 * However, it is also impossible for the sessiond to know when
3121 * the consumer daemon is done destroying its channel(s) since
3122 * it occurs as a reaction to the closing of the channel's file
3123 * descriptor. There is no resulting communication initiated
3124 * from the consumerd to the sessiond to confirm that the
3125 * operation is completed (and was successful).
3127 * Until this is all fixed, the session daemon checks for the
3128 * removal of the session's shm path which makes it possible
3129 * to safely advertise a session as having been destroyed.
3131 * Prior to this fix, it was not possible to reliably save
3132 * a session making use of the --shm-path option, destroy it,
3133 * and load it again. This is because the creation of the
3134 * session would fail upon seeing the session's shm path
3135 * already in existence.
3137 * Note that none of the error paths in the check for the
3138 * directory's existence return an error. This is normal
3139 * as there isn't much that can be done. The session will
3140 * be destroyed properly, except that we can't offer the
3141 * guarantee that the same session can be re-created.
3143 current_completion_handler
= &destroy_completion_handler
.handler
;
3144 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3146 sizeof(destroy_completion_handler
.shm_path
));
3151 * The session is destroyed. However, note that the command context
3152 * still holds a reference to the session, thus delaying its destruction
3153 * _at least_ up to the point when that reference is released.
3155 session_destroy(session
);
3162 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3164 int cmd_register_consumer(struct ltt_session
*session
,
3165 enum lttng_domain_type domain
, const char *sock_path
,
3166 struct consumer_data
*cdata
)
3169 struct consumer_socket
*socket
= NULL
;
3176 case LTTNG_DOMAIN_KERNEL
:
3178 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3182 /* Can't register a consumer if there is already one */
3183 if (ksess
->consumer_fds_sent
!= 0) {
3184 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3188 sock
= lttcomm_connect_unix_sock(sock_path
);
3190 ret
= LTTNG_ERR_CONNECT_FAIL
;
3193 cdata
->cmd_sock
= sock
;
3195 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3196 if (socket
== NULL
) {
3199 PERROR("close register consumer");
3201 cdata
->cmd_sock
= -1;
3202 ret
= LTTNG_ERR_FATAL
;
3206 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3207 if (socket
->lock
== NULL
) {
3208 PERROR("zmalloc pthread mutex");
3209 ret
= LTTNG_ERR_FATAL
;
3212 pthread_mutex_init(socket
->lock
, NULL
);
3213 socket
->registered
= 1;
3216 consumer_add_socket(socket
, ksess
->consumer
);
3219 pthread_mutex_lock(&cdata
->pid_mutex
);
3221 pthread_mutex_unlock(&cdata
->pid_mutex
);
3226 /* TODO: Userspace tracing */
3227 ret
= LTTNG_ERR_UND
;
3235 consumer_destroy_socket(socket
);
3241 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3243 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3244 struct lttng_domain
**domains
)
3249 struct lttng_ht_iter iter
;
3251 if (session
->kernel_session
!= NULL
) {
3252 DBG3("Listing domains found kernel domain");
3256 if (session
->ust_session
!= NULL
) {
3257 DBG3("Listing domains found UST global domain");
3261 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3263 if (agt
->being_used
) {
3274 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3275 if (*domains
== NULL
) {
3276 ret
= LTTNG_ERR_FATAL
;
3280 if (session
->kernel_session
!= NULL
) {
3281 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3283 /* Kernel session buffer type is always GLOBAL */
3284 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3289 if (session
->ust_session
!= NULL
) {
3290 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3291 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3295 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3297 if (agt
->being_used
) {
3298 (*domains
)[index
].type
= agt
->domain
;
3299 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3309 /* Return negative value to differentiate return code */
3315 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3317 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3318 struct ltt_session
*session
, struct lttng_channel
**channels
)
3320 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3323 case LTTNG_DOMAIN_KERNEL
:
3324 if (session
->kernel_session
!= NULL
) {
3325 nb_chan
= session
->kernel_session
->channel_count
;
3327 DBG3("Number of kernel channels %zd", nb_chan
);
3329 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3333 case LTTNG_DOMAIN_UST
:
3334 if (session
->ust_session
!= NULL
) {
3336 nb_chan
= lttng_ht_get_count(
3337 session
->ust_session
->domain_global
.channels
);
3340 DBG3("Number of UST global channels %zd", nb_chan
);
3342 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3347 ret
= -LTTNG_ERR_UND
;
3352 const size_t channel_size
= sizeof(struct lttng_channel
) +
3353 sizeof(struct lttng_channel_extended
);
3354 struct lttng_channel_extended
*channel_exts
;
3356 payload_size
= nb_chan
* channel_size
;
3357 *channels
= zmalloc(payload_size
);
3358 if (*channels
== NULL
) {
3359 ret
= -LTTNG_ERR_FATAL
;
3363 channel_exts
= ((void *) *channels
) +
3364 (nb_chan
* sizeof(struct lttng_channel
));
3365 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3366 if (ret
!= LTTNG_OK
) {
3381 * Command LTTNG_LIST_EVENTS processed by the client thread.
3383 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3384 struct ltt_session
*session
, char *channel_name
,
3385 struct lttng_event
**events
, size_t *total_size
)
3388 ssize_t nb_event
= 0;
3391 case LTTNG_DOMAIN_KERNEL
:
3392 if (session
->kernel_session
!= NULL
) {
3393 nb_event
= list_lttng_kernel_events(channel_name
,
3394 session
->kernel_session
, events
,
3398 case LTTNG_DOMAIN_UST
:
3400 if (session
->ust_session
!= NULL
) {
3401 nb_event
= list_lttng_ust_global_events(channel_name
,
3402 &session
->ust_session
->domain_global
, events
,
3407 case LTTNG_DOMAIN_LOG4J
:
3408 case LTTNG_DOMAIN_JUL
:
3409 case LTTNG_DOMAIN_PYTHON
:
3410 if (session
->ust_session
) {
3411 struct lttng_ht_iter iter
;
3415 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3416 &iter
.iter
, agt
, node
.node
) {
3417 if (agt
->domain
== domain
) {
3418 nb_event
= list_lttng_agent_events(
3428 ret
= LTTNG_ERR_UND
;
3435 /* Return negative value to differentiate return code */
3440 * Using the session list, filled a lttng_session array to send back to the
3441 * client for session listing.
3443 * The session list lock MUST be acquired before calling this function. Use
3444 * session_lock_list() and session_unlock_list().
3446 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3447 size_t session_count
, uid_t uid
, gid_t gid
)
3451 struct ltt_session
*session
;
3452 struct ltt_session_list
*list
= session_get_list();
3453 struct lttng_session_extended
*extended
=
3454 (typeof(extended
)) (&sessions
[session_count
]);
3456 DBG("Getting all available session for UID %d GID %d",
3459 * Iterate over session list and append data after the control struct in
3462 cds_list_for_each_entry(session
, &list
->head
, list
) {
3463 if (!session_get(session
)) {
3467 * Only list the sessions the user can control.
3469 if (!session_access_ok(session
, uid
, gid
) ||
3470 session
->destroyed
) {
3471 session_put(session
);
3475 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3476 struct ltt_ust_session
*usess
= session
->ust_session
;
3478 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3479 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3480 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3481 ret
= build_network_session_path(sessions
[i
].path
,
3482 sizeof(sessions
[i
].path
), session
);
3484 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3485 session
->consumer
->dst
.session_root_path
);
3488 PERROR("snprintf session path");
3489 session_put(session
);
3493 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3494 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3495 sessions
[i
].enabled
= session
->active
;
3496 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3497 sessions
[i
].live_timer_interval
= session
->live_timer
;
3498 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3499 extended
[i
].creation_time
.is_set
= 1;
3501 session_put(session
);
3506 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3507 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3509 int cmd_data_pending(struct ltt_session
*session
)
3512 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3513 struct ltt_ust_session
*usess
= session
->ust_session
;
3517 DBG("Data pending for session %s", session
->name
);
3519 /* Session MUST be stopped to ask for data availability. */
3520 if (session
->active
) {
3521 ret
= LTTNG_ERR_SESSION_STARTED
;
3525 * If stopped, just make sure we've started before else the above call
3526 * will always send that there is data pending.
3528 * The consumer assumes that when the data pending command is received,
3529 * the trace has been started before or else no output data is written
3530 * by the streams which is a condition for data pending. So, this is
3531 * *VERY* important that we don't ask the consumer before a start
3534 if (!session
->has_been_started
) {
3540 /* A rotation is still pending, we have to wait. */
3541 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3542 DBG("Rotate still pending for session %s", session
->name
);
3547 if (ksess
&& ksess
->consumer
) {
3548 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3550 /* Data is still being extracted for the kernel. */
3555 if (usess
&& usess
->consumer
) {
3556 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3558 /* Data is still being extracted for the kernel. */
3563 /* Data is ready to be read by a viewer */
3571 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3573 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3575 int cmd_snapshot_add_output(struct ltt_session
*session
,
3576 struct lttng_snapshot_output
*output
, uint32_t *id
)
3579 struct snapshot_output
*new_output
;
3584 DBG("Cmd snapshot add output for session %s", session
->name
);
3587 * Can't create an output if the session is not set in no-output mode.
3589 if (session
->output_traces
) {
3590 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3594 if (session
->has_non_mmap_channel
) {
3595 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3599 /* Only one output is allowed until we have the "tee" feature. */
3600 if (session
->snapshot
.nb_output
== 1) {
3601 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3605 new_output
= snapshot_output_alloc();
3607 ret
= LTTNG_ERR_NOMEM
;
3611 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3612 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3613 &session
->snapshot
);
3615 if (ret
== -ENOMEM
) {
3616 ret
= LTTNG_ERR_NOMEM
;
3618 ret
= LTTNG_ERR_INVALID
;
3624 snapshot_add_output(&session
->snapshot
, new_output
);
3626 *id
= new_output
->id
;
3633 snapshot_output_destroy(new_output
);
3639 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3641 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3643 int cmd_snapshot_del_output(struct ltt_session
*session
,
3644 struct lttng_snapshot_output
*output
)
3647 struct snapshot_output
*sout
= NULL
;
3655 * Permission denied to create an output if the session is not
3656 * set in no output mode.
3658 if (session
->output_traces
) {
3659 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3664 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3666 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3667 } else if (*output
->name
!= '\0') {
3668 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3670 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3673 ret
= LTTNG_ERR_INVALID
;
3677 snapshot_delete_output(&session
->snapshot
, sout
);
3678 snapshot_output_destroy(sout
);
3687 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3689 * If no output is available, outputs is untouched and 0 is returned.
3691 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3693 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3694 struct lttng_snapshot_output
**outputs
)
3697 struct lttng_snapshot_output
*list
= NULL
;
3698 struct lttng_ht_iter iter
;
3699 struct snapshot_output
*output
;
3704 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3707 * Permission denied to create an output if the session is not
3708 * set in no output mode.
3710 if (session
->output_traces
) {
3711 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3715 if (session
->snapshot
.nb_output
== 0) {
3720 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3722 ret
= -LTTNG_ERR_NOMEM
;
3726 /* Copy list from session to the new list object. */
3728 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3729 output
, node
.node
) {
3730 assert(output
->consumer
);
3731 list
[idx
].id
= output
->id
;
3732 list
[idx
].max_size
= output
->max_size
;
3733 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3734 sizeof(list
[idx
].name
))) {
3735 ret
= -LTTNG_ERR_INVALID
;
3738 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3739 if (lttng_strncpy(list
[idx
].ctrl_url
,
3740 output
->consumer
->dst
.session_root_path
,
3741 sizeof(list
[idx
].ctrl_url
))) {
3742 ret
= -LTTNG_ERR_INVALID
;
3747 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3748 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3750 ret
= -LTTNG_ERR_NOMEM
;
3755 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3756 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3758 ret
= -LTTNG_ERR_NOMEM
;
3767 ret
= session
->snapshot
.nb_output
;
3776 * Check if we can regenerate the metadata for this session.
3777 * Only kernel, UST per-uid and non-live sessions are supported.
3779 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3782 int check_regenerate_metadata_support(struct ltt_session
*session
)
3788 if (session
->live_timer
!= 0) {
3789 ret
= LTTNG_ERR_LIVE_SESSION
;
3792 if (!session
->active
) {
3793 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3796 if (session
->ust_session
) {
3797 switch (session
->ust_session
->buffer_type
) {
3798 case LTTNG_BUFFER_PER_UID
:
3800 case LTTNG_BUFFER_PER_PID
:
3801 ret
= LTTNG_ERR_PER_PID_SESSION
;
3805 ret
= LTTNG_ERR_UNK
;
3809 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3810 session
->consumer
->relay_minor_version
< 8) {
3811 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3821 int clear_metadata_file(int fd
)
3826 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
3827 if (lseek_ret
< 0) {
3833 ret
= ftruncate(fd
, 0);
3835 PERROR("ftruncate");
3844 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3847 struct buffer_reg_uid
*uid_reg
= NULL
;
3848 struct buffer_reg_session
*session_reg
= NULL
;
3851 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3852 struct ust_registry_session
*registry
;
3853 struct ust_registry_channel
*chan
;
3854 struct lttng_ht_iter iter_chan
;
3856 session_reg
= uid_reg
->registry
;
3857 registry
= session_reg
->reg
.ust
;
3859 pthread_mutex_lock(®istry
->lock
);
3860 registry
->metadata_len_sent
= 0;
3861 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3862 registry
->metadata_len
= 0;
3863 registry
->metadata_version
++;
3864 if (registry
->metadata_fd
> 0) {
3865 /* Clear the metadata file's content. */
3866 ret
= clear_metadata_file(registry
->metadata_fd
);
3868 pthread_mutex_unlock(®istry
->lock
);
3873 ret
= ust_metadata_session_statedump(registry
, NULL
,
3874 registry
->major
, registry
->minor
);
3876 pthread_mutex_unlock(®istry
->lock
);
3877 ERR("Failed to generate session metadata (err = %d)",
3881 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3883 struct ust_registry_event
*event
;
3884 struct lttng_ht_iter iter_event
;
3886 ret
= ust_metadata_channel_statedump(registry
, chan
);
3888 pthread_mutex_unlock(®istry
->lock
);
3889 ERR("Failed to generate channel metadata "
3893 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3895 ret
= ust_metadata_event_statedump(registry
,
3898 pthread_mutex_unlock(®istry
->lock
);
3899 ERR("Failed to generate event metadata "
3905 pthread_mutex_unlock(®istry
->lock
);
3914 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3916 * Ask the consumer to truncate the existing metadata file(s) and
3917 * then regenerate the metadata. Live and per-pid sessions are not
3918 * supported and return an error.
3920 * Return 0 on success or else a LTTNG_ERR code.
3922 int cmd_regenerate_metadata(struct ltt_session
*session
)
3928 ret
= check_regenerate_metadata_support(session
);
3933 if (session
->kernel_session
) {
3934 ret
= kernctl_session_regenerate_metadata(
3935 session
->kernel_session
->fd
);
3937 ERR("Failed to regenerate the kernel metadata");
3942 if (session
->ust_session
) {
3943 ret
= ust_regenerate_metadata(session
->ust_session
);
3945 ERR("Failed to regenerate the UST metadata");
3949 DBG("Cmd metadata regenerate for session %s", session
->name
);
3957 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3959 * Ask the tracer to regenerate a new statedump.
3961 * Return 0 on success or else a LTTNG_ERR code.
3963 int cmd_regenerate_statedump(struct ltt_session
*session
)
3969 if (!session
->active
) {
3970 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3974 if (session
->kernel_session
) {
3975 ret
= kernctl_session_regenerate_statedump(
3976 session
->kernel_session
->fd
);
3978 * Currently, the statedump in kernel can only fail if out
3982 if (ret
== -ENOMEM
) {
3983 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3985 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3987 ERR("Failed to regenerate the kernel statedump");
3992 if (session
->ust_session
) {
3993 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3995 * Currently, the statedump in UST always returns 0.
3998 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3999 ERR("Failed to regenerate the UST statedump");
4003 DBG("Cmd regenerate statedump for session %s", session
->name
);
4010 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4011 struct notification_thread_handle
*notification_thread
)
4015 ssize_t sock_recv_len
;
4016 struct lttng_trigger
*trigger
= NULL
;
4017 struct lttng_buffer_view view
;
4018 struct lttng_dynamic_buffer trigger_buffer
;
4020 lttng_dynamic_buffer_init(&trigger_buffer
);
4021 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4022 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4024 ret
= LTTNG_ERR_NOMEM
;
4028 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4030 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4031 ERR("Failed to receive \"register trigger\" command payload");
4032 /* TODO: should this be a new error enum ? */
4033 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4037 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4038 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4040 ERR("Invalid trigger payload received in \"register trigger\" command");
4041 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4045 ret
= notification_thread_command_register_trigger(notification_thread
,
4047 /* Ownership of trigger was transferred. */
4050 lttng_trigger_destroy(trigger
);
4051 lttng_dynamic_buffer_reset(&trigger_buffer
);
4055 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4056 struct notification_thread_handle
*notification_thread
)
4060 ssize_t sock_recv_len
;
4061 struct lttng_trigger
*trigger
= NULL
;
4062 struct lttng_buffer_view view
;
4063 struct lttng_dynamic_buffer trigger_buffer
;
4065 lttng_dynamic_buffer_init(&trigger_buffer
);
4066 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4067 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4069 ret
= LTTNG_ERR_NOMEM
;
4073 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4075 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4076 ERR("Failed to receive \"unregister trigger\" command payload");
4077 /* TODO: should this be a new error enum ? */
4078 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4082 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4083 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4085 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4086 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4090 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4093 lttng_trigger_destroy(trigger
);
4094 lttng_dynamic_buffer_reset(&trigger_buffer
);
4099 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4100 * snapshot output is *not* set with a remote destination.
4102 * Return LTTNG_OK on success or a LTTNG_ERR code.
4104 static enum lttng_error_code
set_relayd_for_snapshot(
4105 struct consumer_output
*consumer
,
4106 struct snapshot_output
*snap_output
,
4107 struct ltt_session
*session
)
4109 enum lttng_error_code status
= LTTNG_OK
;
4110 struct lttng_ht_iter iter
;
4111 struct consumer_socket
*socket
;
4114 assert(snap_output
);
4117 DBG2("Set relayd object from snapshot output");
4119 /* Ignore if snapshot consumer output is not network. */
4120 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
4125 * For each consumer socket, create and send the relayd object of the
4129 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
4130 socket
, node
.node
) {
4131 pthread_mutex_lock(socket
->lock
);
4132 status
= send_consumer_relayd_sockets(0, session
->id
,
4133 snap_output
->consumer
, socket
,
4134 session
->name
, session
->hostname
,
4135 session
->live_timer
);
4136 pthread_mutex_unlock(socket
->lock
);
4137 if (status
!= LTTNG_OK
) {
4149 * Record a kernel snapshot.
4151 * Return LTTNG_OK on success or a LTTNG_ERR code.
4153 static enum lttng_error_code
record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
4154 struct snapshot_output
*output
, struct ltt_session
*session
,
4155 int wait
, uint64_t nb_packets_per_stream
)
4158 enum lttng_error_code status
;
4165 * Copy kernel session sockets so we can communicate with the right
4166 * consumer for the snapshot record command.
4168 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
4170 status
= LTTNG_ERR_NOMEM
;
4174 status
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
4175 if (status
!= LTTNG_OK
) {
4176 goto error_snapshot
;
4179 status
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
4180 if (status
!= LTTNG_OK
) {
4181 goto error_snapshot
;
4187 /* Clean up copied sockets so this output can use some other later on. */
4188 consumer_destroy_output_sockets(output
->consumer
);
4195 * Record a UST snapshot.
4197 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4199 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4200 struct snapshot_output
*output
, struct ltt_session
*session
,
4201 int wait
, uint64_t nb_packets_per_stream
)
4204 enum lttng_error_code status
;
4211 * Copy UST session sockets so we can communicate with the right
4212 * consumer for the snapshot record command.
4214 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
4216 status
= LTTNG_ERR_NOMEM
;
4220 status
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
4221 if (status
!= LTTNG_OK
) {
4222 goto error_snapshot
;
4225 status
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
4226 if (status
!= LTTNG_OK
) {
4227 goto error_snapshot
;
4233 /* Clean up copied sockets so this output can use some other later on. */
4234 consumer_destroy_output_sockets(output
->consumer
);
4241 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
4242 uint64_t cur_nr_packets
)
4244 uint64_t tot_size
= 0;
4246 if (session
->kernel_session
) {
4247 struct ltt_kernel_channel
*chan
;
4248 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4250 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4251 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4253 * Don't take channel into account if we
4254 * already grab all its packets.
4258 tot_size
+= chan
->channel
->attr
.subbuf_size
4259 * chan
->stream_count
;
4263 if (session
->ust_session
) {
4264 struct ltt_ust_session
*usess
= session
->ust_session
;
4266 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4274 * Calculate the number of packets we can grab from each stream that
4275 * fits within the overall snapshot max size.
4277 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4278 * the number of packets per stream.
4280 * TODO: this approach is not perfect: we consider the worse case
4281 * (packet filling the sub-buffers) as an upper bound, but we could do
4282 * better if we do this calculation while we actually grab the packet
4283 * content: we would know how much padding we don't actually store into
4286 * This algorithm is currently bounded by the number of packets per
4289 * Since we call this algorithm before actually grabbing the data, it's
4290 * an approximation: for instance, applications could appear/disappear
4291 * in between this call and actually grabbing data.
4294 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
4297 uint64_t cur_nb_packets
= 0;
4300 return 0; /* Infinite */
4303 size_left
= max_size
;
4305 uint64_t one_more_packet_tot_size
;
4307 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
4309 if (!one_more_packet_tot_size
) {
4310 /* We are already grabbing all packets. */
4313 size_left
-= one_more_packet_tot_size
;
4314 if (size_left
< 0) {
4319 if (!cur_nb_packets
) {
4320 /* Not enough room to grab one packet of each stream, error. */
4323 return cur_nb_packets
;
4327 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4329 * The wait parameter is ignored so this call always wait for the snapshot to
4330 * complete before returning.
4332 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4334 int cmd_snapshot_record(struct ltt_session
*session
,
4335 struct lttng_snapshot_output
*output
, int wait
)
4337 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4339 unsigned int use_tmp_output
= 0;
4340 struct snapshot_output tmp_output
;
4341 unsigned int snapshot_success
= 0;
4347 DBG("Cmd snapshot record for session %s", session
->name
);
4349 /* Get the datetime for the snapshot output directory. */
4350 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4353 cmd_ret
= LTTNG_ERR_INVALID
;
4358 * Permission denied to create an output if the session is not
4359 * set in no output mode.
4361 if (session
->output_traces
) {
4362 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4366 /* The session needs to be started at least once. */
4367 if (!session
->has_been_started
) {
4368 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4372 /* Use temporary output for the session. */
4373 if (*output
->ctrl_url
!= '\0') {
4374 ret
= snapshot_output_init(session
, output
->max_size
,
4376 output
->ctrl_url
, output
->data_url
,
4380 if (ret
== -ENOMEM
) {
4381 cmd_ret
= LTTNG_ERR_NOMEM
;
4383 cmd_ret
= LTTNG_ERR_INVALID
;
4387 /* Use the global session count for the temporary snapshot. */
4388 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4390 /* Use the global datetime */
4391 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4395 if (use_tmp_output
) {
4396 int64_t nb_packets_per_stream
;
4398 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4399 tmp_output
.max_size
);
4400 if (nb_packets_per_stream
< 0) {
4401 cmd_ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4405 if (session
->kernel_session
) {
4406 cmd_ret
= record_kernel_snapshot(session
->kernel_session
,
4407 &tmp_output
, session
,
4408 wait
, nb_packets_per_stream
);
4409 if (cmd_ret
!= LTTNG_OK
) {
4414 if (session
->ust_session
) {
4415 cmd_ret
= record_ust_snapshot(session
->ust_session
,
4416 &tmp_output
, session
,
4417 wait
, nb_packets_per_stream
);
4418 if (cmd_ret
!= LTTNG_OK
) {
4423 snapshot_success
= 1;
4425 struct snapshot_output
*sout
;
4426 struct lttng_ht_iter iter
;
4429 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4430 &iter
.iter
, sout
, node
.node
) {
4431 int64_t nb_packets_per_stream
;
4434 * Make a local copy of the output and assign the possible
4435 * temporary value given by the caller.
4437 memset(&tmp_output
, 0, sizeof(tmp_output
));
4438 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
4440 if (output
->max_size
!= (uint64_t) -1ULL) {
4441 tmp_output
.max_size
= output
->max_size
;
4444 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4445 tmp_output
.max_size
);
4446 if (nb_packets_per_stream
< 0) {
4447 cmd_ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4452 /* Use temporary name. */
4453 if (*output
->name
!= '\0') {
4454 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4455 sizeof(tmp_output
.name
))) {
4456 cmd_ret
= LTTNG_ERR_INVALID
;
4462 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4463 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4465 if (session
->kernel_session
) {
4466 cmd_ret
= record_kernel_snapshot(session
->kernel_session
,
4467 &tmp_output
, session
,
4468 wait
, nb_packets_per_stream
);
4469 if (cmd_ret
!= LTTNG_OK
) {
4475 if (session
->ust_session
) {
4476 cmd_ret
= record_ust_snapshot(session
->ust_session
,
4477 &tmp_output
, session
,
4478 wait
, nb_packets_per_stream
);
4479 if (cmd_ret
!= LTTNG_OK
) {
4484 snapshot_success
= 1;
4489 if (snapshot_success
) {
4490 session
->snapshot
.nb_snapshot
++;
4492 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4500 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4502 int cmd_set_session_shm_path(struct ltt_session
*session
,
4503 const char *shm_path
)
4509 * Can only set shm path before session is started.
4511 if (session
->has_been_started
) {
4512 return LTTNG_ERR_SESSION_STARTED
;
4515 strncpy(session
->shm_path
, shm_path
,
4516 sizeof(session
->shm_path
));
4517 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4523 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4525 * Ask the consumer to rotate the session output directory.
4526 * The session lock must be held.
4528 * Returns LTTNG_OK on success or else a negative LTTng error code.
4530 int cmd_rotate_session(struct ltt_session
*session
,
4531 struct lttng_rotate_session_return
*rotate_return
)
4534 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4536 struct tm
*timeinfo
;
4540 * Used to roll-back timestamps in case of failure to launch the
4543 time_t original_last_chunk_start_ts
, original_current_chunk_start_ts
;
4547 if (!session
->has_been_started
) {
4548 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4552 if (session
->live_timer
|| !session
->output_traces
) {
4553 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4558 * Unsupported feature in lttng-relayd before 2.11.
4560 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4561 (session
->consumer
->relay_major_version
== 2 &&
4562 session
->consumer
->relay_minor_version
< 11)) {
4563 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
4567 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4568 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
4570 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
4575 * After a stop, we only allow one rotation to occur, the other ones are
4576 * useless until a new start.
4578 if (session
->rotated_after_last_stop
) {
4579 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4581 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
4585 /* Special case for the first rotation. */
4586 if (session
->current_archive_id
== 0) {
4587 const char *base_path
= NULL
;
4589 assert(session
->kernel_session
|| session
->ust_session
);
4590 /* Either one of the two sessions is enough to get the root path. */
4591 base_path
= session_get_base_path(session
);
4594 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4596 sizeof(session
->rotation_chunk
.current_rotate_path
));
4598 ERR("Failed to copy session base path to current rotation chunk path");
4599 cmd_ret
= LTTNG_ERR_UNK
;
4604 * The currently active tracing path is now the folder we
4607 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4608 session
->rotation_chunk
.active_tracing_path
,
4609 sizeof(session
->rotation_chunk
.current_rotate_path
));
4611 ERR("Failed to copy the active tracing path to the current rotate path");
4612 cmd_ret
= LTTNG_ERR_UNK
;
4616 DBG("Current rotate path %s", session
->rotation_chunk
.current_rotate_path
);
4619 * Channels created after this point will belong to the next
4622 session
->current_archive_id
++;
4625 if (now
== (time_t) -1) {
4626 cmd_ret
= LTTNG_ERR_UNK
;
4630 /* Sample chunk bounds for roll-back in case of error. */
4631 original_last_chunk_start_ts
= session
->last_chunk_start_ts
;
4632 original_current_chunk_start_ts
= session
->current_chunk_start_ts
;
4634 session
->last_chunk_start_ts
= session
->current_chunk_start_ts
;
4635 session
->current_chunk_start_ts
= now
;
4637 timeinfo
= localtime(&now
);
4639 PERROR("Failed to sample local time in rotate session command");
4640 cmd_ret
= LTTNG_ERR_UNK
;
4643 strf_ret
= strftime(datetime
, sizeof(datetime
), "%Y%m%dT%H%M%S%z",
4646 ERR("Failed to format local time timestamp in rotate session command");
4647 cmd_ret
= LTTNG_ERR_UNK
;
4651 /* Current chunk directory, ex: 20170922-111754-42 */
4652 ret
= snprintf(session
->consumer
->chunk_path
,
4653 sizeof(session
->consumer
->chunk_path
),
4654 "%s-%" PRIu64
, datetime
,
4655 session
->current_archive_id
+ 1);
4656 if (ret
< 0 || ret
>= sizeof(session
->consumer
->chunk_path
)) {
4657 ERR("Failed to format the new chunk's directory in rotate session command");
4658 cmd_ret
= LTTNG_ERR_UNK
;
4663 * The active path for the next rotation/destroy.
4664 * Ex: ~/lttng-traces/auto-20170922-111748/20170922-111754-42
4666 ret
= snprintf(session
->rotation_chunk
.active_tracing_path
,
4667 sizeof(session
->rotation_chunk
.active_tracing_path
),
4669 session_get_base_path(session
),
4670 session
->consumer
->chunk_path
);
4671 if (ret
< 0 || ret
>= sizeof(session
->rotation_chunk
.active_tracing_path
)) {
4672 ERR("Failed to format active tracing path in rotate session command");
4673 cmd_ret
= LTTNG_ERR_UNK
;
4678 * A rotation has a local step even if the destination is a relay
4679 * daemon; the buffers must be consumed by the consumer daemon.
4681 session
->rotation_pending_local
= true;
4682 session
->rotation_pending_relay
=
4683 session_get_consumer_destination_type(session
) == CONSUMER_DST_NET
;
4684 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
4686 if (session
->kernel_session
) {
4687 ret
= lttng_strncpy(
4688 session
->kernel_session
->consumer
->chunk_path
,
4689 session
->consumer
->chunk_path
,
4690 sizeof(session
->kernel_session
->consumer
->chunk_path
));
4692 ERR("Failed to copy current chunk directory to kernel session");
4693 cmd_ret
= LTTNG_ERR_UNK
;
4697 * Create the new chunk folder, before the rotation begins so we
4698 * don't race with the consumer/tracer activity.
4700 ret
= domain_mkdir(session
->kernel_session
->consumer
, session
,
4701 session
->kernel_session
->uid
,
4702 session
->kernel_session
->gid
);
4704 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
4707 cmd_ret
= kernel_rotate_session(session
);
4708 if (cmd_ret
!= LTTNG_OK
) {
4712 if (session
->ust_session
) {
4713 ret
= lttng_strncpy(
4714 session
->ust_session
->consumer
->chunk_path
,
4715 session
->consumer
->chunk_path
,
4716 sizeof(session
->ust_session
->consumer
->chunk_path
));
4718 ERR("Failed to copy current chunk directory to userspace session");
4719 cmd_ret
= LTTNG_ERR_UNK
;
4722 ret
= domain_mkdir(session
->ust_session
->consumer
, session
,
4723 session
->ust_session
->uid
,
4724 session
->ust_session
->gid
);
4726 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
4729 cmd_ret
= ust_app_rotate_session(session
);
4730 if (cmd_ret
!= LTTNG_OK
) {
4735 ret
= timer_session_rotation_pending_check_start(session
,
4736 DEFAULT_ROTATE_PENDING_TIMER
);
4738 cmd_ret
= LTTNG_ERR_UNK
;
4742 if (!session
->active
) {
4743 session
->rotated_after_last_stop
= true;
4746 if (rotate_return
) {
4747 rotate_return
->rotation_id
= session
->current_archive_id
;
4750 ret
= notification_thread_command_session_rotation_ongoing(
4751 notification_thread_handle
,
4752 session
->name
, session
->uid
, session
->gid
,
4753 session
->current_archive_id
- 1);
4754 if (ret
!= LTTNG_OK
) {
4755 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
4760 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
4761 session
->name
, session
->current_archive_id
- 1);
4763 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
4766 session
->last_chunk_start_ts
= original_last_chunk_start_ts
;
4767 session
->current_archive_id
= original_current_chunk_start_ts
;
4768 if (session_reset_rotation_state(session
,
4769 LTTNG_ROTATION_STATE_NO_ROTATION
)) {
4770 ERR("Failed to reset rotation state of session \"%s\"",
4777 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
4779 * Check if the session has finished its rotation.
4781 * Return 0 on success or else a LTTNG_ERR code.
4783 int cmd_rotate_get_info(struct ltt_session
*session
,
4784 struct lttng_rotation_get_info_return
*info_return
,
4785 uint64_t rotation_id
)
4791 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
4792 session
->current_archive_id
);
4794 if (session
->current_archive_id
!= rotation_id
) {
4795 info_return
->status
= (int32_t) LTTNG_ROTATION_STATE_EXPIRED
;
4800 switch (session
->rotation_state
) {
4801 case LTTNG_ROTATION_STATE_ONGOING
:
4802 DBG("Reporting that rotation id %" PRIu64
" of session %s is still pending",
4803 rotation_id
, session
->name
);
4805 case LTTNG_ROTATION_STATE_COMPLETED
:
4807 char *current_tracing_path_reply
;
4808 size_t current_tracing_path_reply_len
;
4810 switch (session_get_consumer_destination_type(session
)) {
4811 case CONSUMER_DST_LOCAL
:
4812 current_tracing_path_reply
=
4813 info_return
->location
.local
.absolute_path
;
4814 current_tracing_path_reply_len
=
4815 sizeof(info_return
->location
.local
.absolute_path
);
4816 info_return
->location_type
=
4817 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
4819 case CONSUMER_DST_NET
:
4820 current_tracing_path_reply
=
4821 info_return
->location
.relay
.relative_path
;
4822 current_tracing_path_reply_len
=
4823 sizeof(info_return
->location
.relay
.relative_path
);
4824 /* Currently the only supported relay protocol. */
4825 info_return
->location
.relay
.protocol
=
4826 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
4828 ret
= lttng_strncpy(info_return
->location
.relay
.host
,
4829 session_get_net_consumer_hostname(session
),
4830 sizeof(info_return
->location
.relay
.host
));
4832 ERR("Failed to host name to rotate_get_info reply");
4833 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4834 ret
= -LTTNG_ERR_UNK
;
4838 session_get_net_consumer_ports(session
,
4839 &info_return
->location
.relay
.ports
.control
,
4840 &info_return
->location
.relay
.ports
.data
);
4841 info_return
->location_type
=
4842 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
4847 ret
= lttng_strncpy(current_tracing_path_reply
,
4848 session
->rotation_chunk
.current_rotate_path
,
4849 current_tracing_path_reply_len
);
4851 ERR("Failed to copy current tracing path to rotate_get_info reply");
4852 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4853 ret
= -LTTNG_ERR_UNK
;
4859 case LTTNG_ROTATION_STATE_ERROR
:
4860 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session %s",
4861 rotation_id
, session
->name
);
4867 info_return
->status
= (int32_t) session
->rotation_state
;
4874 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
4876 * Configure the automatic rotation parameters.
4877 * 'activate' to true means activate the rotation schedule type with 'new_value'.
4878 * 'activate' to false means deactivate the rotation schedule and validate that
4879 * 'new_value' has the same value as the currently active value.
4881 * Return 0 on success or else a positive LTTNG_ERR code.
4883 int cmd_rotation_set_schedule(struct ltt_session
*session
,
4884 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
4886 struct notification_thread_handle
*notification_thread_handle
)
4889 uint64_t *parameter_value
;
4893 DBG("Cmd rotate set schedule session %s", session
->name
);
4895 if (session
->live_timer
|| !session
->output_traces
) {
4896 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
4897 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4901 switch (schedule_type
) {
4902 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
4903 parameter_value
= &session
->rotate_size
;
4905 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
4906 parameter_value
= &session
->rotate_timer_period
;
4907 if (new_value
>= UINT_MAX
) {
4908 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
4909 new_value
, UINT_MAX
);
4910 ret
= LTTNG_ERR_INVALID
;
4915 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
4916 ret
= LTTNG_ERR_INVALID
;
4920 /* Improper use of the API. */
4921 if (new_value
== -1ULL) {
4922 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
4923 ret
= LTTNG_ERR_INVALID
;
4928 * As indicated in struct ltt_session's comments, a value of == 0 means
4929 * this schedule rotation type is not in use.
4931 * Reject the command if we were asked to activate a schedule that was
4934 if (activate
&& *parameter_value
!= 0) {
4935 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
4936 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
4941 * Reject the command if we were asked to deactivate a schedule that was
4944 if (!activate
&& *parameter_value
== 0) {
4945 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
4946 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
4951 * Reject the command if we were asked to deactivate a schedule that
4954 if (!activate
&& *parameter_value
!= new_value
) {
4955 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
4956 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
4960 *parameter_value
= activate
? new_value
: 0;
4962 switch (schedule_type
) {
4963 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
4964 if (activate
&& session
->active
) {
4966 * Only start the timer if the session is active,
4967 * otherwise it will be started when the session starts.
4969 ret
= timer_session_rotation_schedule_timer_start(
4970 session
, new_value
);
4972 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
4973 ret
= LTTNG_ERR_UNK
;
4977 ret
= timer_session_rotation_schedule_timer_stop(
4980 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
4981 ret
= LTTNG_ERR_UNK
;
4986 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
4988 ret
= subscribe_session_consumed_size_rotation(session
,
4989 new_value
, notification_thread_handle
);
4991 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
4992 ret
= LTTNG_ERR_UNK
;
4996 ret
= unsubscribe_session_consumed_size_rotation(session
,
4997 notification_thread_handle
);
4999 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5000 ret
= LTTNG_ERR_UNK
;
5007 /* Would have been caught before. */
5019 /* Wait for a given path to be removed before continuing. */
5020 static enum lttng_error_code
wait_on_path(void *path_data
)
5022 const char *shm_path
= path_data
;
5024 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5030 ret
= stat(shm_path
, &st
);
5032 if (errno
!= ENOENT
) {
5033 PERROR("stat() returned an error while checking for the existence of the shm path");
5035 DBG("shm path no longer exists, completing the destruction of session");
5039 if (!S_ISDIR(st
.st_mode
)) {
5040 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5045 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5051 * Returns a pointer to a handler to run on completion of a command.
5052 * Returns NULL if no handler has to be run for the last command executed.
5054 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5056 struct cmd_completion_handler
*handler
= current_completion_handler
;
5058 current_completion_handler
= NULL
;
5063 * Init command subsystem.
5068 * Set network sequence index to 1 for streams to match a relayd
5069 * socket on the consumer side.
5071 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5072 relayd_net_seq_idx
= 1;
5073 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5075 DBG("Command subsystem initialized");