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>
27 #include <common/defaults.h>
28 #include <common/common.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
30 #include <common/relayd/relayd.h>
31 #include <common/utils.h>
32 #include <common/compat/string.h>
33 #include <common/kernel-ctl/kernel-ctl.h>
34 #include <common/dynamic-buffer.h>
35 #include <common/buffer-view.h>
36 #include <common/trace-chunk.h>
37 #include <lttng/trigger/trigger-internal.h>
38 #include <lttng/condition/condition.h>
39 #include <lttng/action/action.h>
40 #include <lttng/channel.h>
41 #include <lttng/channel-internal.h>
42 #include <lttng/rotate-internal.h>
43 #include <lttng/location-internal.h>
44 #include <lttng/session-internal.h>
45 #include <lttng/userspace-probe-internal.h>
46 #include <lttng/session-descriptor-internal.h>
47 #include <common/string-utils/string-utils.h>
52 #include "health-sessiond.h"
54 #include "kernel-consumer.h"
55 #include "lttng-sessiond.h"
57 #include "lttng-syscall.h"
59 #include "buffer-registry.h"
60 #include "notification-thread.h"
61 #include "notification-thread-commands.h"
63 #include "rotation-thread.h"
65 #include "agent-thread.h"
69 /* Sleep for 100ms between each check for the shm path's deletion. */
70 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
72 static enum lttng_error_code
wait_on_path(void *path
);
75 * Command completion handler that is used by the destroy command
76 * when a session that has a non-default shm_path is being destroyed.
78 * See comment in cmd_destroy_session() for the rationale.
80 static struct destroy_completion_handler
{
81 struct cmd_completion_handler handler
;
82 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
83 } destroy_completion_handler
= {
86 .data
= destroy_completion_handler
.shm_path
91 static struct cmd_completion_handler
*current_completion_handler
;
94 * Used to keep a unique index for each relayd socket created where this value
95 * is associated with streams on the consumer so it can match the right relayd
96 * to send to. It must be accessed with the relayd_net_seq_idx_lock
99 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
100 static uint64_t relayd_net_seq_idx
;
102 static int validate_ust_event_name(const char *);
103 static int cmd_enable_event_internal(struct ltt_session
*session
,
104 struct lttng_domain
*domain
,
105 char *channel_name
, struct lttng_event
*event
,
106 char *filter_expression
,
107 struct lttng_filter_bytecode
*filter
,
108 struct lttng_event_exclusion
*exclusion
,
112 * Create a session path used by list_lttng_sessions for the case that the
113 * session consumer is on the network.
115 static int build_network_session_path(char *dst
, size_t size
,
116 struct ltt_session
*session
)
118 int ret
, kdata_port
, udata_port
;
119 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
120 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
125 memset(tmp_urls
, 0, sizeof(tmp_urls
));
126 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
128 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
130 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
131 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
132 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
135 if (session
->ust_session
&& session
->ust_session
->consumer
) {
136 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
137 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
140 if (uuri
== NULL
&& kuri
== NULL
) {
141 uri
= &session
->consumer
->dst
.net
.control
;
142 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
143 } else if (kuri
&& uuri
) {
144 ret
= uri_compare(kuri
, uuri
);
148 /* Build uuri URL string */
149 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
156 } else if (kuri
&& uuri
== NULL
) {
158 } else if (uuri
&& kuri
== NULL
) {
162 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
168 * Do we have a UST url set. If yes, this means we have both kernel and UST
171 if (*tmp_uurl
!= '\0') {
172 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
173 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
176 if (kuri
|| (!kuri
&& !uuri
)) {
179 /* No kernel URI, use the UST port. */
182 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
190 * Get run-time attributes if the session has been started (discarded events,
193 static int get_kernel_runtime_stats(struct ltt_session
*session
,
194 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
195 uint64_t *lost_packets
)
199 if (!session
->has_been_started
) {
201 *discarded_events
= 0;
206 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
207 session
->kernel_session
->consumer
,
213 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
214 session
->kernel_session
->consumer
,
225 * Get run-time attributes if the session has been started (discarded events,
228 static int get_ust_runtime_stats(struct ltt_session
*session
,
229 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
230 uint64_t *lost_packets
)
233 struct ltt_ust_session
*usess
;
235 if (!discarded_events
|| !lost_packets
) {
240 usess
= session
->ust_session
;
241 assert(discarded_events
);
242 assert(lost_packets
);
244 if (!usess
|| !session
->has_been_started
) {
245 *discarded_events
= 0;
251 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
252 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
253 &usess
->buffer_reg_uid_list
,
254 usess
->consumer
, uchan
->id
,
255 uchan
->attr
.overwrite
,
258 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
259 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
260 uchan
, usess
->consumer
,
261 uchan
->attr
.overwrite
,
267 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
268 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
270 ERR("Unsupported buffer type");
281 * Fill lttng_channel array of all channels.
283 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
284 struct ltt_session
*session
, struct lttng_channel
*channels
,
285 struct lttng_channel_extended
*chan_exts
)
288 struct ltt_kernel_channel
*kchan
;
290 DBG("Listing channels for session %s", session
->name
);
293 case LTTNG_DOMAIN_KERNEL
:
294 /* Kernel channels */
295 if (session
->kernel_session
!= NULL
) {
296 cds_list_for_each_entry(kchan
,
297 &session
->kernel_session
->channel_list
.head
, list
) {
298 uint64_t discarded_events
, lost_packets
;
299 struct lttng_channel_extended
*extended
;
301 extended
= (struct lttng_channel_extended
*)
302 kchan
->channel
->attr
.extended
.ptr
;
304 ret
= get_kernel_runtime_stats(session
, kchan
,
305 &discarded_events
, &lost_packets
);
309 /* Copy lttng_channel struct to array */
310 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
311 channels
[i
].enabled
= kchan
->enabled
;
312 chan_exts
[i
].discarded_events
=
314 chan_exts
[i
].lost_packets
= lost_packets
;
315 chan_exts
[i
].monitor_timer_interval
=
316 extended
->monitor_timer_interval
;
317 chan_exts
[i
].blocking_timeout
= 0;
322 case LTTNG_DOMAIN_UST
:
324 struct lttng_ht_iter iter
;
325 struct ltt_ust_channel
*uchan
;
328 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
329 &iter
.iter
, uchan
, node
.node
) {
330 uint64_t discarded_events
= 0, lost_packets
= 0;
332 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
333 LTTNG_SYMBOL_NAME_LEN
)) {
336 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
337 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
338 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
339 channels
[i
].attr
.switch_timer_interval
=
340 uchan
->attr
.switch_timer_interval
;
341 channels
[i
].attr
.read_timer_interval
=
342 uchan
->attr
.read_timer_interval
;
343 channels
[i
].enabled
= uchan
->enabled
;
344 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
345 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
348 * Map enum lttng_ust_output to enum lttng_event_output.
350 switch (uchan
->attr
.output
) {
352 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
356 * LTTNG_UST_MMAP is the only supported UST
363 chan_exts
[i
].monitor_timer_interval
=
364 uchan
->monitor_timer_interval
;
365 chan_exts
[i
].blocking_timeout
=
366 uchan
->attr
.u
.s
.blocking_timeout
;
368 ret
= get_ust_runtime_stats(session
, uchan
,
369 &discarded_events
, &lost_packets
);
373 chan_exts
[i
].discarded_events
= discarded_events
;
374 chan_exts
[i
].lost_packets
= lost_packets
;
386 return -LTTNG_ERR_FATAL
;
392 static int increment_extended_len(const char *filter_expression
,
393 struct lttng_event_exclusion
*exclusion
,
394 const struct lttng_userspace_probe_location
*probe_location
,
395 size_t *extended_len
)
399 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
401 if (filter_expression
) {
402 *extended_len
+= strlen(filter_expression
) + 1;
406 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
409 if (probe_location
) {
410 ret
= lttng_userspace_probe_location_serialize(probe_location
,
415 *extended_len
+= ret
;
422 static int append_extended_info(const char *filter_expression
,
423 struct lttng_event_exclusion
*exclusion
,
424 struct lttng_userspace_probe_location
*probe_location
,
428 size_t filter_len
= 0;
429 size_t nb_exclusions
= 0;
430 size_t userspace_probe_location_len
= 0;
431 struct lttng_dynamic_buffer location_buffer
;
432 struct lttcomm_event_extended_header extended_header
;
434 if (filter_expression
) {
435 filter_len
= strlen(filter_expression
) + 1;
439 nb_exclusions
= exclusion
->count
;
442 if (probe_location
) {
443 lttng_dynamic_buffer_init(&location_buffer
);
444 ret
= lttng_userspace_probe_location_serialize(probe_location
,
445 &location_buffer
, NULL
);
450 userspace_probe_location_len
= location_buffer
.size
;
453 /* Set header fields */
454 extended_header
.filter_len
= filter_len
;
455 extended_header
.nb_exclusions
= nb_exclusions
;
456 extended_header
.userspace_probe_location_len
= userspace_probe_location_len
;
459 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
460 *extended_at
+= sizeof(extended_header
);
462 /* Copy filter string */
463 if (filter_expression
) {
464 memcpy(*extended_at
, filter_expression
, filter_len
);
465 *extended_at
+= filter_len
;
468 /* Copy exclusion names */
470 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
472 memcpy(*extended_at
, &exclusion
->names
, len
);
476 if (probe_location
) {
477 memcpy(*extended_at
, location_buffer
.data
, location_buffer
.size
);
478 *extended_at
+= location_buffer
.size
;
479 lttng_dynamic_buffer_reset(&location_buffer
);
487 * Create a list of agent domain events.
489 * Return number of events in list on success or else a negative value.
491 static int list_lttng_agent_events(struct agent
*agt
,
492 struct lttng_event
**events
, size_t *total_size
)
495 unsigned int nb_event
= 0;
496 struct agent_event
*event
;
497 struct lttng_event
*tmp_events
= NULL
;
498 struct lttng_ht_iter iter
;
499 size_t extended_len
= 0;
505 DBG3("Listing agent events");
508 nb_event
= lttng_ht_get_count(agt
->events
);
516 /* Compute required extended infos size */
517 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
520 * This is only valid because the commands which add events are
521 * processed in the same thread as the listing.
524 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
525 ret
= increment_extended_len(event
->filter_expression
, NULL
, NULL
,
528 DBG("Error computing the length of extended info message");
529 ret
= -LTTNG_ERR_FATAL
;
535 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
536 tmp_events
= zmalloc(*total_size
);
538 PERROR("zmalloc agent events session");
539 ret
= -LTTNG_ERR_FATAL
;
543 extended_at
= ((uint8_t *) tmp_events
) +
544 nb_event
* sizeof(struct lttng_event
);
547 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
548 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
549 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
550 tmp_events
[i
].enabled
= event
->enabled
;
551 tmp_events
[i
].loglevel
= event
->loglevel_value
;
552 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
555 /* Append extended info */
556 ret
= append_extended_info(event
->filter_expression
, NULL
, NULL
,
559 DBG("Error appending extended info message");
560 ret
= -LTTNG_ERR_FATAL
;
565 *events
= tmp_events
;
567 assert(nb_event
== i
);
578 * Create a list of ust global domain events.
580 static int list_lttng_ust_global_events(char *channel_name
,
581 struct ltt_ust_domain_global
*ust_global
,
582 struct lttng_event
**events
, size_t *total_size
)
585 unsigned int nb_event
= 0;
586 struct lttng_ht_iter iter
;
587 struct lttng_ht_node_str
*node
;
588 struct ltt_ust_channel
*uchan
;
589 struct ltt_ust_event
*uevent
;
590 struct lttng_event
*tmp
;
591 size_t extended_len
= 0;
594 DBG("Listing UST global events for channel %s", channel_name
);
598 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
599 node
= lttng_ht_iter_get_node_str(&iter
);
601 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
605 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
607 nb_event
= lttng_ht_get_count(uchan
->events
);
614 DBG3("Listing UST global %d events", nb_event
);
616 /* Compute required extended infos size */
617 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
618 if (uevent
->internal
) {
623 ret
= increment_extended_len(uevent
->filter_expression
,
624 uevent
->exclusion
, NULL
, &extended_len
);
626 DBG("Error computing the length of extended info message");
627 ret
= -LTTNG_ERR_FATAL
;
632 /* All events are internal, skip. */
638 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
639 tmp
= zmalloc(*total_size
);
641 ret
= -LTTNG_ERR_FATAL
;
645 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
647 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
648 if (uevent
->internal
) {
649 /* This event should remain hidden from clients */
652 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
653 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
654 tmp
[i
].enabled
= uevent
->enabled
;
656 switch (uevent
->attr
.instrumentation
) {
657 case LTTNG_UST_TRACEPOINT
:
658 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
660 case LTTNG_UST_PROBE
:
661 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
663 case LTTNG_UST_FUNCTION
:
664 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
668 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
669 switch (uevent
->attr
.loglevel_type
) {
670 case LTTNG_UST_LOGLEVEL_ALL
:
671 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
673 case LTTNG_UST_LOGLEVEL_RANGE
:
674 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
676 case LTTNG_UST_LOGLEVEL_SINGLE
:
677 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
680 if (uevent
->filter
) {
683 if (uevent
->exclusion
) {
684 tmp
[i
].exclusion
= 1;
688 /* Append extended info */
689 ret
= append_extended_info(uevent
->filter_expression
,
690 uevent
->exclusion
, NULL
, &extended_at
);
692 DBG("Error appending extended info message");
693 ret
= -LTTNG_ERR_FATAL
;
706 * Fill lttng_event array of all kernel events in the channel.
708 static int list_lttng_kernel_events(char *channel_name
,
709 struct ltt_kernel_session
*kernel_session
,
710 struct lttng_event
**events
, size_t *total_size
)
713 unsigned int nb_event
;
714 struct ltt_kernel_event
*event
;
715 struct ltt_kernel_channel
*kchan
;
716 size_t extended_len
= 0;
719 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
721 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
725 nb_event
= kchan
->event_count
;
727 DBG("Listing events for channel %s", kchan
->channel
->name
);
735 /* Compute required extended infos size */
736 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
737 ret
= increment_extended_len(event
->filter_expression
, NULL
,
738 event
->userspace_probe_location
,
741 DBG("Error computing the length of extended info message");
742 ret
= -LTTNG_ERR_FATAL
;
747 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
748 *events
= zmalloc(*total_size
);
749 if (*events
== NULL
) {
750 ret
= -LTTNG_ERR_FATAL
;
754 extended_at
= ((void *) *events
) +
755 nb_event
* sizeof(struct lttng_event
);
757 /* Kernel channels */
758 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
759 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
760 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
761 (*events
)[i
].enabled
= event
->enabled
;
762 (*events
)[i
].filter
=
763 (unsigned char) !!event
->filter_expression
;
765 switch (event
->event
->instrumentation
) {
766 case LTTNG_KERNEL_TRACEPOINT
:
767 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
769 case LTTNG_KERNEL_KRETPROBE
:
770 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
771 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
772 sizeof(struct lttng_kernel_kprobe
));
774 case LTTNG_KERNEL_KPROBE
:
775 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
776 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
777 sizeof(struct lttng_kernel_kprobe
));
779 case LTTNG_KERNEL_UPROBE
:
780 (*events
)[i
].type
= LTTNG_EVENT_USERSPACE_PROBE
;
782 case LTTNG_KERNEL_FUNCTION
:
783 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
784 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
785 sizeof(struct lttng_kernel_function
));
787 case LTTNG_KERNEL_NOOP
:
788 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
790 case LTTNG_KERNEL_SYSCALL
:
791 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
793 case LTTNG_KERNEL_ALL
:
801 /* Append extended info */
802 ret
= append_extended_info(event
->filter_expression
, NULL
,
803 event
->userspace_probe_location
, &extended_at
);
805 DBG("Error appending extended info message");
806 ret
= -LTTNG_ERR_FATAL
;
815 /* Negate the error code to differentiate the size from an error */
820 * Add URI so the consumer output object. Set the correct path depending on the
821 * domain adding the default trace directory.
823 static enum lttng_error_code
add_uri_to_consumer(
824 const struct ltt_session
*session
,
825 struct consumer_output
*consumer
,
826 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
829 enum lttng_error_code ret_code
= LTTNG_OK
;
833 if (consumer
== NULL
) {
834 DBG("No consumer detected. Don't add URI. Stopping.");
835 ret_code
= LTTNG_ERR_NO_CONSUMER
;
840 case LTTNG_DOMAIN_KERNEL
:
841 ret
= lttng_strncpy(consumer
->domain_subdir
,
842 DEFAULT_KERNEL_TRACE_DIR
,
843 sizeof(consumer
->domain_subdir
));
845 case LTTNG_DOMAIN_UST
:
846 ret
= lttng_strncpy(consumer
->domain_subdir
,
847 DEFAULT_UST_TRACE_DIR
,
848 sizeof(consumer
->domain_subdir
));
852 * This case is possible is we try to add the URI to the global
853 * tracing session consumer object which in this case there is
856 memset(consumer
->domain_subdir
, 0,
857 sizeof(consumer
->domain_subdir
));
861 ERR("Failed to initialize consumer output domain subdirectory");
862 ret_code
= LTTNG_ERR_FATAL
;
866 switch (uri
->dtype
) {
869 DBG2("Setting network URI to consumer");
871 if (consumer
->type
== CONSUMER_DST_NET
) {
872 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
873 consumer
->dst
.net
.control_isset
) ||
874 (uri
->stype
== LTTNG_STREAM_DATA
&&
875 consumer
->dst
.net
.data_isset
)) {
876 ret_code
= LTTNG_ERR_URL_EXIST
;
880 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
883 /* Set URI into consumer output object */
884 ret
= consumer_set_network_uri(session
, consumer
, uri
);
888 } else if (ret
== 1) {
890 * URI was the same in the consumer so we do not append the subdir
891 * again so to not duplicate output dir.
898 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
899 ret_code
= LTTNG_ERR_INVALID
;
902 DBG2("Setting trace directory path from URI to %s",
904 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
906 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
908 sizeof(consumer
->dst
.session_root_path
));
909 consumer
->type
= CONSUMER_DST_LOCAL
;
919 * Init tracing by creating trace directory and sending fds kernel consumer.
921 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
924 struct lttng_ht_iter iter
;
925 struct consumer_socket
*socket
;
931 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
932 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
934 pthread_mutex_lock(socket
->lock
);
935 ret
= kernel_consumer_send_session(socket
, session
);
936 pthread_mutex_unlock(socket
->lock
);
938 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
950 * Create a socket to the relayd using the URI.
952 * On success, the relayd_sock pointer is set to the created socket.
953 * Else, it remains untouched and an LTTng error code is returned.
955 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
956 struct lttcomm_relayd_sock
**relayd_sock
,
957 struct consumer_output
*consumer
)
960 enum lttng_error_code status
= LTTNG_OK
;
961 struct lttcomm_relayd_sock
*rsock
;
963 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
964 RELAYD_VERSION_COMM_MINOR
);
966 status
= LTTNG_ERR_FATAL
;
971 * Connect to relayd so we can proceed with a session creation. This call
972 * can possibly block for an arbitrary amount of time to set the health
973 * state to be in poll execution.
976 ret
= relayd_connect(rsock
);
979 ERR("Unable to reach lttng-relayd");
980 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
984 /* Create socket for control stream. */
985 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
986 DBG3("Creating relayd stream socket from URI");
988 /* Check relayd version */
989 ret
= relayd_version_check(rsock
);
990 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
991 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
993 } else if (ret
< 0) {
994 ERR("Unable to reach lttng-relayd");
995 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
998 consumer
->relay_major_version
= rsock
->major
;
999 consumer
->relay_minor_version
= rsock
->minor
;
1000 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1001 DBG3("Creating relayd data socket from URI");
1003 /* Command is not valid */
1004 ERR("Relayd invalid stream type: %d", uri
->stype
);
1005 status
= LTTNG_ERR_INVALID
;
1009 *relayd_sock
= rsock
;
1014 /* The returned value is not useful since we are on an error path. */
1015 (void) relayd_close(rsock
);
1023 * Connect to the relayd using URI and send the socket to the right consumer.
1025 * The consumer socket lock must be held by the caller.
1027 * Returns LTTNG_OK on success or an LTTng error code on failure.
1029 static enum lttng_error_code
send_consumer_relayd_socket(
1030 unsigned int session_id
,
1031 struct lttng_uri
*relayd_uri
,
1032 struct consumer_output
*consumer
,
1033 struct consumer_socket
*consumer_sock
,
1034 const char *session_name
, const char *hostname
,
1035 int session_live_timer
)
1038 struct lttcomm_relayd_sock
*rsock
= NULL
;
1039 enum lttng_error_code status
;
1041 /* Connect to relayd and make version check if uri is the control. */
1042 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1043 if (status
!= LTTNG_OK
) {
1044 goto relayd_comm_error
;
1048 /* Set the network sequence index if not set. */
1049 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1050 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1052 * Increment net_seq_idx because we are about to transfer the
1053 * new relayd socket to the consumer.
1054 * Assign unique key so the consumer can match streams.
1056 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1057 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1060 /* Send relayd socket to consumer. */
1061 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1062 relayd_uri
->stype
, session_id
,
1063 session_name
, hostname
, session_live_timer
);
1065 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1069 /* Flag that the corresponding socket was sent. */
1070 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1071 consumer_sock
->control_sock_sent
= 1;
1072 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1073 consumer_sock
->data_sock_sent
= 1;
1077 * Close socket which was dup on the consumer side. The session daemon does
1078 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1082 if (status
!= LTTNG_OK
) {
1084 * The consumer output for this session should not be used anymore
1085 * since the relayd connection failed thus making any tracing or/and
1086 * streaming not usable.
1088 consumer
->enabled
= 0;
1090 (void) relayd_close(rsock
);
1098 * Send both relayd sockets to a specific consumer and domain. This is a
1099 * helper function to facilitate sending the information to the consumer for a
1102 * The consumer socket lock must be held by the caller.
1104 * Returns LTTNG_OK, or an LTTng error code on failure.
1106 static enum lttng_error_code
send_consumer_relayd_sockets(
1107 enum lttng_domain_type domain
,
1108 unsigned int session_id
, struct consumer_output
*consumer
,
1109 struct consumer_socket
*sock
, const char *session_name
,
1110 const char *hostname
, int session_live_timer
)
1112 enum lttng_error_code status
= LTTNG_OK
;
1117 /* Sending control relayd socket. */
1118 if (!sock
->control_sock_sent
) {
1119 status
= send_consumer_relayd_socket(session_id
,
1120 &consumer
->dst
.net
.control
, consumer
, sock
,
1121 session_name
, hostname
, session_live_timer
);
1122 if (status
!= LTTNG_OK
) {
1127 /* Sending data relayd socket. */
1128 if (!sock
->data_sock_sent
) {
1129 status
= send_consumer_relayd_socket(session_id
,
1130 &consumer
->dst
.net
.data
, consumer
, sock
,
1131 session_name
, hostname
, session_live_timer
);
1132 if (status
!= LTTNG_OK
) {
1142 * Setup relayd connections for a tracing session. First creates the socket to
1143 * the relayd and send them to the right domain consumer. Consumer type MUST be
1146 int cmd_setup_relayd(struct ltt_session
*session
)
1149 struct ltt_ust_session
*usess
;
1150 struct ltt_kernel_session
*ksess
;
1151 struct consumer_socket
*socket
;
1152 struct lttng_ht_iter iter
;
1156 usess
= session
->ust_session
;
1157 ksess
= session
->kernel_session
;
1159 DBG("Setting relayd for session %s", session
->name
);
1163 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1164 && usess
->consumer
->enabled
) {
1165 /* For each consumer socket, send relayd sockets */
1166 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1167 socket
, node
.node
) {
1168 pthread_mutex_lock(socket
->lock
);
1169 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1170 usess
->consumer
, socket
,
1171 session
->name
, session
->hostname
,
1172 session
->live_timer
);
1173 pthread_mutex_unlock(socket
->lock
);
1174 if (ret
!= LTTNG_OK
) {
1177 /* Session is now ready for network streaming. */
1178 session
->net_handle
= 1;
1180 session
->consumer
->relay_major_version
=
1181 usess
->consumer
->relay_major_version
;
1182 session
->consumer
->relay_minor_version
=
1183 usess
->consumer
->relay_minor_version
;
1186 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1187 && ksess
->consumer
->enabled
) {
1188 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1189 socket
, node
.node
) {
1190 pthread_mutex_lock(socket
->lock
);
1191 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1192 ksess
->consumer
, socket
,
1193 session
->name
, session
->hostname
,
1194 session
->live_timer
);
1195 pthread_mutex_unlock(socket
->lock
);
1196 if (ret
!= LTTNG_OK
) {
1199 /* Session is now ready for network streaming. */
1200 session
->net_handle
= 1;
1202 session
->consumer
->relay_major_version
=
1203 ksess
->consumer
->relay_major_version
;
1204 session
->consumer
->relay_minor_version
=
1205 ksess
->consumer
->relay_minor_version
;
1214 * Start a kernel session by opening all necessary streams.
1216 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1219 struct ltt_kernel_channel
*kchan
;
1221 /* Open kernel metadata */
1222 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1223 ret
= kernel_open_metadata(ksess
);
1225 ret
= LTTNG_ERR_KERN_META_FAIL
;
1230 /* Open kernel metadata stream */
1231 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1232 ret
= kernel_open_metadata_stream(ksess
);
1234 ERR("Kernel create metadata stream failed");
1235 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1240 /* For each channel */
1241 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1242 if (kchan
->stream_count
== 0) {
1243 ret
= kernel_open_channel_stream(kchan
);
1245 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1248 /* Update the stream global counter */
1249 ksess
->stream_count_global
+= ret
;
1253 /* Setup kernel consumer socket and send fds to it */
1254 ret
= init_kernel_tracing(ksess
);
1256 ret
= LTTNG_ERR_KERN_START_FAIL
;
1260 /* This start the kernel tracing */
1261 ret
= kernel_start_session(ksess
);
1263 ret
= LTTNG_ERR_KERN_START_FAIL
;
1267 /* Quiescent wait after starting trace */
1268 kernel_wait_quiescent(wpipe
);
1279 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1281 int cmd_disable_channel(struct ltt_session
*session
,
1282 enum lttng_domain_type domain
, char *channel_name
)
1285 struct ltt_ust_session
*usess
;
1287 usess
= session
->ust_session
;
1292 case LTTNG_DOMAIN_KERNEL
:
1294 ret
= channel_kernel_disable(session
->kernel_session
,
1296 if (ret
!= LTTNG_OK
) {
1300 kernel_wait_quiescent(kernel_tracer_fd
);
1303 case LTTNG_DOMAIN_UST
:
1305 struct ltt_ust_channel
*uchan
;
1306 struct lttng_ht
*chan_ht
;
1308 chan_ht
= usess
->domain_global
.channels
;
1310 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1311 if (uchan
== NULL
) {
1312 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1316 ret
= channel_ust_disable(usess
, uchan
);
1317 if (ret
!= LTTNG_OK
) {
1323 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1335 * Command LTTNG_TRACK_PID processed by the client thread.
1337 * Called with session lock held.
1339 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1347 case LTTNG_DOMAIN_KERNEL
:
1349 struct ltt_kernel_session
*ksess
;
1351 ksess
= session
->kernel_session
;
1353 ret
= kernel_track_pid(ksess
, pid
);
1354 if (ret
!= LTTNG_OK
) {
1358 kernel_wait_quiescent(kernel_tracer_fd
);
1361 case LTTNG_DOMAIN_UST
:
1363 struct ltt_ust_session
*usess
;
1365 usess
= session
->ust_session
;
1367 ret
= trace_ust_track_pid(usess
, pid
);
1368 if (ret
!= LTTNG_OK
) {
1374 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1386 * Command LTTNG_UNTRACK_PID processed by the client thread.
1388 * Called with session lock held.
1390 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1398 case LTTNG_DOMAIN_KERNEL
:
1400 struct ltt_kernel_session
*ksess
;
1402 ksess
= session
->kernel_session
;
1404 ret
= kernel_untrack_pid(ksess
, pid
);
1405 if (ret
!= LTTNG_OK
) {
1409 kernel_wait_quiescent(kernel_tracer_fd
);
1412 case LTTNG_DOMAIN_UST
:
1414 struct ltt_ust_session
*usess
;
1416 usess
= session
->ust_session
;
1418 ret
= trace_ust_untrack_pid(usess
, pid
);
1419 if (ret
!= LTTNG_OK
) {
1425 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1437 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1439 * The wpipe arguments is used as a notifier for the kernel thread.
1441 int cmd_enable_channel(struct ltt_session
*session
,
1442 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1445 struct ltt_ust_session
*usess
= session
->ust_session
;
1446 struct lttng_ht
*chan_ht
;
1453 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1455 /* Validate channel name */
1456 if (attr
->name
[0] == '.' ||
1457 memchr(attr
->name
, '/', len
) != NULL
) {
1458 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1462 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1467 * Don't try to enable a channel if the session has been started at
1468 * some point in time before. The tracer does not allow it.
1470 if (session
->has_been_started
) {
1471 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1476 * If the session is a live session, remove the switch timer, the
1477 * live timer does the same thing but sends also synchronisation
1478 * beacons for inactive streams.
1480 if (session
->live_timer
> 0) {
1481 attr
->attr
.live_timer_interval
= session
->live_timer
;
1482 attr
->attr
.switch_timer_interval
= 0;
1485 /* Check for feature support */
1486 switch (domain
->type
) {
1487 case LTTNG_DOMAIN_KERNEL
:
1489 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd
) != 1) {
1490 /* Sampling position of buffer is not supported */
1491 WARN("Kernel tracer does not support buffer monitoring. "
1492 "Setting the monitor interval timer to 0 "
1493 "(disabled) for channel '%s' of session '%s'",
1494 attr
-> name
, session
->name
);
1495 lttng_channel_set_monitor_timer_interval(attr
, 0);
1499 case LTTNG_DOMAIN_UST
:
1501 case LTTNG_DOMAIN_JUL
:
1502 case LTTNG_DOMAIN_LOG4J
:
1503 case LTTNG_DOMAIN_PYTHON
:
1504 if (!agent_tracing_is_enabled()) {
1505 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1506 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1511 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1515 switch (domain
->type
) {
1516 case LTTNG_DOMAIN_KERNEL
:
1518 struct ltt_kernel_channel
*kchan
;
1520 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1521 session
->kernel_session
);
1522 if (kchan
== NULL
) {
1523 if (session
->snapshot
.nb_output
> 0 ||
1524 session
->snapshot_mode
) {
1525 /* Enforce mmap output for snapshot sessions. */
1526 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1528 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1529 if (attr
->name
[0] != '\0') {
1530 session
->kernel_session
->has_non_default_channel
= 1;
1533 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1536 if (ret
!= LTTNG_OK
) {
1540 kernel_wait_quiescent(kernel_tracer_fd
);
1543 case LTTNG_DOMAIN_UST
:
1544 case LTTNG_DOMAIN_JUL
:
1545 case LTTNG_DOMAIN_LOG4J
:
1546 case LTTNG_DOMAIN_PYTHON
:
1548 struct ltt_ust_channel
*uchan
;
1553 * Current agent implementation limitations force us to allow
1554 * only one channel at once in "agent" subdomains. Each
1555 * subdomain has a default channel name which must be strictly
1558 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1559 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1560 LTTNG_SYMBOL_NAME_LEN
)) {
1561 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1564 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1565 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1566 LTTNG_SYMBOL_NAME_LEN
)) {
1567 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1570 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1571 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1572 LTTNG_SYMBOL_NAME_LEN
)) {
1573 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1578 chan_ht
= usess
->domain_global
.channels
;
1580 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1581 if (uchan
== NULL
) {
1582 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1583 if (attr
->name
[0] != '\0') {
1584 usess
->has_non_default_channel
= 1;
1587 ret
= channel_ust_enable(usess
, uchan
);
1592 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1596 if (ret
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1597 session
->has_non_mmap_channel
= true;
1606 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1608 int cmd_disable_event(struct ltt_session
*session
,
1609 enum lttng_domain_type domain
, char *channel_name
,
1610 struct lttng_event
*event
)
1615 DBG("Disable event command for event \'%s\'", event
->name
);
1617 event_name
= event
->name
;
1619 /* Error out on unhandled search criteria */
1620 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1621 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1622 ret
= LTTNG_ERR_UNK
;
1629 case LTTNG_DOMAIN_KERNEL
:
1631 struct ltt_kernel_channel
*kchan
;
1632 struct ltt_kernel_session
*ksess
;
1634 ksess
= session
->kernel_session
;
1637 * If a non-default channel has been created in the
1638 * session, explicitely require that -c chan_name needs
1641 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1642 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1646 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1647 if (kchan
== NULL
) {
1648 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1652 switch (event
->type
) {
1653 case LTTNG_EVENT_ALL
:
1654 case LTTNG_EVENT_TRACEPOINT
:
1655 case LTTNG_EVENT_SYSCALL
:
1656 case LTTNG_EVENT_PROBE
:
1657 case LTTNG_EVENT_FUNCTION
:
1658 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1659 if (event_name
[0] == '\0') {
1660 ret
= event_kernel_disable_event(kchan
,
1663 ret
= event_kernel_disable_event(kchan
,
1664 event_name
, event
->type
);
1666 if (ret
!= LTTNG_OK
) {
1671 ret
= LTTNG_ERR_UNK
;
1675 kernel_wait_quiescent(kernel_tracer_fd
);
1678 case LTTNG_DOMAIN_UST
:
1680 struct ltt_ust_channel
*uchan
;
1681 struct ltt_ust_session
*usess
;
1683 usess
= session
->ust_session
;
1685 if (validate_ust_event_name(event_name
)) {
1686 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1691 * If a non-default channel has been created in the
1692 * session, explicitly require that -c chan_name needs
1695 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1696 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1700 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1702 if (uchan
== NULL
) {
1703 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1707 switch (event
->type
) {
1708 case LTTNG_EVENT_ALL
:
1710 * An empty event name means that everything
1711 * should be disabled.
1713 if (event
->name
[0] == '\0') {
1714 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1716 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1719 if (ret
!= LTTNG_OK
) {
1724 ret
= LTTNG_ERR_UNK
;
1728 DBG3("Disable UST event %s in channel %s completed", event_name
,
1732 case LTTNG_DOMAIN_LOG4J
:
1733 case LTTNG_DOMAIN_JUL
:
1734 case LTTNG_DOMAIN_PYTHON
:
1737 struct ltt_ust_session
*usess
= session
->ust_session
;
1741 switch (event
->type
) {
1742 case LTTNG_EVENT_ALL
:
1745 ret
= LTTNG_ERR_UNK
;
1749 agt
= trace_ust_find_agent(usess
, domain
);
1751 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1755 * An empty event name means that everything
1756 * should be disabled.
1758 if (event
->name
[0] == '\0') {
1759 ret
= event_agent_disable_all(usess
, agt
);
1761 ret
= event_agent_disable(usess
, agt
, event_name
);
1763 if (ret
!= LTTNG_OK
) {
1770 ret
= LTTNG_ERR_UND
;
1783 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1785 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1786 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1788 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1789 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1792 * Don't try to add a context if the session has been started at
1793 * some point in time before. The tracer does not allow it and would
1794 * result in a corrupted trace.
1796 if (session
->has_been_started
) {
1797 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1801 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1802 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1803 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1807 case LTTNG_DOMAIN_KERNEL
:
1808 assert(session
->kernel_session
);
1810 if (session
->kernel_session
->channel_count
== 0) {
1811 /* Create default channel */
1812 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1813 if (ret
!= LTTNG_OK
) {
1816 chan_kern_created
= 1;
1818 /* Add kernel context to kernel tracer */
1819 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1820 if (ret
!= LTTNG_OK
) {
1824 case LTTNG_DOMAIN_JUL
:
1825 case LTTNG_DOMAIN_LOG4J
:
1828 * Validate channel name.
1829 * If no channel name is given and the domain is JUL or LOG4J,
1830 * set it to the appropriate domain-specific channel name. If
1831 * a name is provided but does not match the expexted channel
1832 * name, return an error.
1834 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1835 strcmp(channel_name
,
1836 DEFAULT_JUL_CHANNEL_NAME
)) {
1837 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1839 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1840 strcmp(channel_name
,
1841 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1842 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1845 /* break is _not_ missing here. */
1847 case LTTNG_DOMAIN_UST
:
1849 struct ltt_ust_session
*usess
= session
->ust_session
;
1850 unsigned int chan_count
;
1854 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1855 if (chan_count
== 0) {
1856 struct lttng_channel
*attr
;
1857 /* Create default channel */
1858 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1860 ret
= LTTNG_ERR_FATAL
;
1864 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1865 if (ret
!= LTTNG_OK
) {
1869 channel_attr_destroy(attr
);
1870 chan_ust_created
= 1;
1873 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1874 free(app_ctx_provider_name
);
1876 app_ctx_name
= NULL
;
1877 app_ctx_provider_name
= NULL
;
1878 if (ret
!= LTTNG_OK
) {
1884 ret
= LTTNG_ERR_UND
;
1892 if (chan_kern_created
) {
1893 struct ltt_kernel_channel
*kchan
=
1894 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1895 session
->kernel_session
);
1896 /* Created previously, this should NOT fail. */
1898 kernel_destroy_channel(kchan
);
1901 if (chan_ust_created
) {
1902 struct ltt_ust_channel
*uchan
=
1903 trace_ust_find_channel_by_name(
1904 session
->ust_session
->domain_global
.channels
,
1905 DEFAULT_CHANNEL_NAME
);
1906 /* Created previously, this should NOT fail. */
1908 /* Remove from the channel list of the session. */
1909 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1911 trace_ust_destroy_channel(uchan
);
1914 free(app_ctx_provider_name
);
1919 static inline bool name_starts_with(const char *name
, const char *prefix
)
1921 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1923 return !strncmp(name
, prefix
, max_cmp_len
);
1926 /* Perform userspace-specific event name validation */
1927 static int validate_ust_event_name(const char *name
)
1937 * Check name against all internal UST event component namespaces used
1940 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1941 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1942 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1951 * Internal version of cmd_enable_event() with a supplemental
1952 * "internal_event" flag which is used to enable internal events which should
1953 * be hidden from clients. Such events are used in the agent implementation to
1954 * enable the events through which all "agent" events are funeled.
1956 static int _cmd_enable_event(struct ltt_session
*session
,
1957 struct lttng_domain
*domain
,
1958 char *channel_name
, struct lttng_event
*event
,
1959 char *filter_expression
,
1960 struct lttng_filter_bytecode
*filter
,
1961 struct lttng_event_exclusion
*exclusion
,
1962 int wpipe
, bool internal_event
)
1964 int ret
= 0, channel_created
= 0;
1965 struct lttng_channel
*attr
= NULL
;
1969 assert(channel_name
);
1971 /* If we have a filter, we must have its filter expression */
1972 assert(!(!!filter_expression
^ !!filter
));
1974 /* Normalize event name as a globbing pattern */
1975 strutils_normalize_star_glob_pattern(event
->name
);
1977 /* Normalize exclusion names as globbing patterns */
1981 for (i
= 0; i
< exclusion
->count
; i
++) {
1982 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
1984 strutils_normalize_star_glob_pattern(name
);
1988 DBG("Enable event command for event \'%s\'", event
->name
);
1992 switch (domain
->type
) {
1993 case LTTNG_DOMAIN_KERNEL
:
1995 struct ltt_kernel_channel
*kchan
;
1998 * If a non-default channel has been created in the
1999 * session, explicitely require that -c chan_name needs
2002 if (session
->kernel_session
->has_non_default_channel
2003 && channel_name
[0] == '\0') {
2004 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2008 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2009 session
->kernel_session
);
2010 if (kchan
== NULL
) {
2011 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2012 LTTNG_BUFFER_GLOBAL
);
2014 ret
= LTTNG_ERR_FATAL
;
2017 if (lttng_strncpy(attr
->name
, channel_name
,
2018 sizeof(attr
->name
))) {
2019 ret
= LTTNG_ERR_INVALID
;
2023 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2024 if (ret
!= LTTNG_OK
) {
2027 channel_created
= 1;
2030 /* Get the newly created kernel channel pointer */
2031 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2032 session
->kernel_session
);
2033 if (kchan
== NULL
) {
2034 /* This sould not happen... */
2035 ret
= LTTNG_ERR_FATAL
;
2039 switch (event
->type
) {
2040 case LTTNG_EVENT_ALL
:
2042 char *filter_expression_a
= NULL
;
2043 struct lttng_filter_bytecode
*filter_a
= NULL
;
2046 * We need to duplicate filter_expression and filter,
2047 * because ownership is passed to first enable
2050 if (filter_expression
) {
2051 filter_expression_a
= strdup(filter_expression
);
2052 if (!filter_expression_a
) {
2053 ret
= LTTNG_ERR_FATAL
;
2058 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2060 free(filter_expression_a
);
2061 ret
= LTTNG_ERR_FATAL
;
2064 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2066 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2067 ret
= event_kernel_enable_event(kchan
, event
,
2068 filter_expression
, filter
);
2069 /* We have passed ownership */
2070 filter_expression
= NULL
;
2072 if (ret
!= LTTNG_OK
) {
2073 if (channel_created
) {
2074 /* Let's not leak a useless channel. */
2075 kernel_destroy_channel(kchan
);
2077 free(filter_expression_a
);
2081 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2082 ret
= event_kernel_enable_event(kchan
, event
,
2083 filter_expression_a
, filter_a
);
2084 /* We have passed ownership */
2085 filter_expression_a
= NULL
;
2087 if (ret
!= LTTNG_OK
) {
2092 case LTTNG_EVENT_PROBE
:
2093 case LTTNG_EVENT_USERSPACE_PROBE
:
2094 case LTTNG_EVENT_FUNCTION
:
2095 case LTTNG_EVENT_FUNCTION_ENTRY
:
2096 case LTTNG_EVENT_TRACEPOINT
:
2097 ret
= event_kernel_enable_event(kchan
, event
,
2098 filter_expression
, filter
);
2099 /* We have passed ownership */
2100 filter_expression
= NULL
;
2102 if (ret
!= LTTNG_OK
) {
2103 if (channel_created
) {
2104 /* Let's not leak a useless channel. */
2105 kernel_destroy_channel(kchan
);
2110 case LTTNG_EVENT_SYSCALL
:
2111 ret
= event_kernel_enable_event(kchan
, event
,
2112 filter_expression
, filter
);
2113 /* We have passed ownership */
2114 filter_expression
= NULL
;
2116 if (ret
!= LTTNG_OK
) {
2121 ret
= LTTNG_ERR_UNK
;
2125 kernel_wait_quiescent(kernel_tracer_fd
);
2128 case LTTNG_DOMAIN_UST
:
2130 struct ltt_ust_channel
*uchan
;
2131 struct ltt_ust_session
*usess
= session
->ust_session
;
2136 * If a non-default channel has been created in the
2137 * session, explicitely require that -c chan_name needs
2140 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2141 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2145 /* Get channel from global UST domain */
2146 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2148 if (uchan
== NULL
) {
2149 /* Create default channel */
2150 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2151 usess
->buffer_type
);
2153 ret
= LTTNG_ERR_FATAL
;
2156 if (lttng_strncpy(attr
->name
, channel_name
,
2157 sizeof(attr
->name
))) {
2158 ret
= LTTNG_ERR_INVALID
;
2162 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2163 if (ret
!= LTTNG_OK
) {
2167 /* Get the newly created channel reference back */
2168 uchan
= trace_ust_find_channel_by_name(
2169 usess
->domain_global
.channels
, channel_name
);
2173 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2175 * Don't allow users to add UST events to channels which
2176 * are assigned to a userspace subdomain (JUL, Log4J,
2179 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2183 if (!internal_event
) {
2185 * Ensure the event name is not reserved for internal
2188 ret
= validate_ust_event_name(event
->name
);
2190 WARN("Userspace event name %s failed validation.",
2192 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2197 /* At this point, the session and channel exist on the tracer */
2198 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2199 filter_expression
, filter
, exclusion
,
2201 /* We have passed ownership */
2202 filter_expression
= NULL
;
2205 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2206 goto already_enabled
;
2207 } else if (ret
!= LTTNG_OK
) {
2212 case LTTNG_DOMAIN_LOG4J
:
2213 case LTTNG_DOMAIN_JUL
:
2214 case LTTNG_DOMAIN_PYTHON
:
2216 const char *default_event_name
, *default_chan_name
;
2218 struct lttng_event uevent
;
2219 struct lttng_domain tmp_dom
;
2220 struct ltt_ust_session
*usess
= session
->ust_session
;
2224 if (!agent_tracing_is_enabled()) {
2225 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2226 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2230 agt
= trace_ust_find_agent(usess
, domain
->type
);
2232 agt
= agent_create(domain
->type
);
2234 ret
= LTTNG_ERR_NOMEM
;
2237 agent_add(agt
, usess
->agents
);
2240 /* Create the default tracepoint. */
2241 memset(&uevent
, 0, sizeof(uevent
));
2242 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2243 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2244 default_event_name
= event_get_default_agent_ust_name(
2246 if (!default_event_name
) {
2247 ret
= LTTNG_ERR_FATAL
;
2250 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2251 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2254 * The domain type is changed because we are about to enable the
2255 * default channel and event for the JUL domain that are hardcoded.
2256 * This happens in the UST domain.
2258 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2259 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2261 switch (domain
->type
) {
2262 case LTTNG_DOMAIN_LOG4J
:
2263 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2265 case LTTNG_DOMAIN_JUL
:
2266 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2268 case LTTNG_DOMAIN_PYTHON
:
2269 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2272 /* The switch/case we are in makes this impossible */
2277 char *filter_expression_copy
= NULL
;
2278 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2281 const size_t filter_size
= sizeof(
2282 struct lttng_filter_bytecode
)
2285 filter_copy
= zmalloc(filter_size
);
2287 ret
= LTTNG_ERR_NOMEM
;
2290 memcpy(filter_copy
, filter
, filter_size
);
2292 filter_expression_copy
=
2293 strdup(filter_expression
);
2294 if (!filter_expression
) {
2295 ret
= LTTNG_ERR_NOMEM
;
2298 if (!filter_expression_copy
|| !filter_copy
) {
2299 free(filter_expression_copy
);
2305 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2306 (char *) default_chan_name
,
2307 &uevent
, filter_expression_copy
,
2308 filter_copy
, NULL
, wpipe
);
2311 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2312 goto already_enabled
;
2313 } else if (ret
!= LTTNG_OK
) {
2317 /* The wild card * means that everything should be enabled. */
2318 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2319 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2322 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2326 filter_expression
= NULL
;
2327 if (ret
!= LTTNG_OK
) {
2334 ret
= LTTNG_ERR_UND
;
2342 free(filter_expression
);
2345 channel_attr_destroy(attr
);
2351 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2352 * We own filter, exclusion, and filter_expression.
2354 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2355 char *channel_name
, struct lttng_event
*event
,
2356 char *filter_expression
,
2357 struct lttng_filter_bytecode
*filter
,
2358 struct lttng_event_exclusion
*exclusion
,
2361 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2362 filter_expression
, filter
, exclusion
, wpipe
, false);
2366 * Enable an event which is internal to LTTng. An internal should
2367 * never be made visible to clients and are immune to checks such as
2370 static int cmd_enable_event_internal(struct ltt_session
*session
,
2371 struct lttng_domain
*domain
,
2372 char *channel_name
, struct lttng_event
*event
,
2373 char *filter_expression
,
2374 struct lttng_filter_bytecode
*filter
,
2375 struct lttng_event_exclusion
*exclusion
,
2378 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2379 filter_expression
, filter
, exclusion
, wpipe
, true);
2383 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2385 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2386 struct lttng_event
**events
)
2389 ssize_t nb_events
= 0;
2392 case LTTNG_DOMAIN_KERNEL
:
2393 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2394 if (nb_events
< 0) {
2395 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2399 case LTTNG_DOMAIN_UST
:
2400 nb_events
= ust_app_list_events(events
);
2401 if (nb_events
< 0) {
2402 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2406 case LTTNG_DOMAIN_LOG4J
:
2407 case LTTNG_DOMAIN_JUL
:
2408 case LTTNG_DOMAIN_PYTHON
:
2409 nb_events
= agent_list_events(events
, domain
);
2410 if (nb_events
< 0) {
2411 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2416 ret
= LTTNG_ERR_UND
;
2423 /* Return negative value to differentiate return code */
2428 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2430 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2431 struct lttng_event_field
**fields
)
2434 ssize_t nb_fields
= 0;
2437 case LTTNG_DOMAIN_UST
:
2438 nb_fields
= ust_app_list_event_fields(fields
);
2439 if (nb_fields
< 0) {
2440 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2444 case LTTNG_DOMAIN_KERNEL
:
2445 default: /* fall-through */
2446 ret
= LTTNG_ERR_UND
;
2453 /* Return negative value to differentiate return code */
2457 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2459 return syscall_table_list(events
);
2463 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2465 * Called with session lock held.
2467 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2468 enum lttng_domain_type domain
, int32_t **pids
)
2471 ssize_t nr_pids
= 0;
2474 case LTTNG_DOMAIN_KERNEL
:
2476 struct ltt_kernel_session
*ksess
;
2478 ksess
= session
->kernel_session
;
2479 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2481 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2486 case LTTNG_DOMAIN_UST
:
2488 struct ltt_ust_session
*usess
;
2490 usess
= session
->ust_session
;
2491 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2493 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2498 case LTTNG_DOMAIN_LOG4J
:
2499 case LTTNG_DOMAIN_JUL
:
2500 case LTTNG_DOMAIN_PYTHON
:
2502 ret
= LTTNG_ERR_UND
;
2509 /* Return negative value to differentiate return code */
2514 * Command LTTNG_START_TRACE processed by the client thread.
2516 * Called with session mutex held.
2518 int cmd_start_trace(struct ltt_session
*session
)
2520 enum lttng_error_code ret
;
2521 unsigned long nb_chan
= 0;
2522 struct ltt_kernel_session
*ksession
;
2523 struct ltt_ust_session
*usess
;
2527 /* Ease our life a bit ;) */
2528 ksession
= session
->kernel_session
;
2529 usess
= session
->ust_session
;
2531 /* Is the session already started? */
2532 if (session
->active
) {
2533 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2538 * Starting a session without channel is useless since after that it's not
2539 * possible to enable channel thus inform the client.
2541 if (usess
&& usess
->domain_global
.channels
) {
2542 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2545 nb_chan
+= ksession
->channel_count
;
2548 ret
= LTTNG_ERR_NO_CHANNEL
;
2552 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2553 struct lttng_trace_chunk
*trace_chunk
;
2555 trace_chunk
= session_create_new_trace_chunk(
2556 session
, NULL
, NULL
);
2558 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2561 assert(!session
->current_trace_chunk
);
2562 ret
= session_set_trace_chunk(session
, trace_chunk
, NULL
);
2563 lttng_trace_chunk_put(trace_chunk
);
2565 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2570 /* Kernel tracing */
2571 if (ksession
!= NULL
) {
2572 DBG("Start kernel tracing session %s", session
->name
);
2573 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2574 if (ret
!= LTTNG_OK
) {
2579 /* Flag session that trace should start automatically */
2581 int int_ret
= ust_app_start_trace_all(usess
);
2584 ret
= LTTNG_ERR_UST_START_FAIL
;
2589 /* Flag this after a successful start. */
2590 session
->has_been_started
= 1;
2591 session
->active
= 1;
2594 * Clear the flag that indicates that a rotation was done while the
2595 * session was stopped.
2597 session
->rotated_after_last_stop
= false;
2599 if (session
->rotate_timer_period
) {
2600 int int_ret
= timer_session_rotation_schedule_timer_start(
2601 session
, session
->rotate_timer_period
);
2604 ERR("Failed to enable rotate timer");
2605 ret
= LTTNG_ERR_UNK
;
2617 * Command LTTNG_STOP_TRACE processed by the client thread.
2619 int cmd_stop_trace(struct ltt_session
*session
)
2622 struct ltt_kernel_channel
*kchan
;
2623 struct ltt_kernel_session
*ksession
;
2624 struct ltt_ust_session
*usess
;
2625 bool error_occurred
= false;
2629 DBG("Begin stop session %s (id %" PRIu64
")", session
->name
, session
->id
);
2631 ksession
= session
->kernel_session
;
2632 usess
= session
->ust_session
;
2634 /* Session is not active. Skip everythong and inform the client. */
2635 if (!session
->active
) {
2636 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2641 if (ksession
&& ksession
->active
) {
2642 DBG("Stop kernel tracing");
2644 ret
= kernel_stop_session(ksession
);
2646 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2650 kernel_wait_quiescent(kernel_tracer_fd
);
2652 /* Flush metadata after stopping (if exists) */
2653 if (ksession
->metadata_stream_fd
>= 0) {
2654 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2656 ERR("Kernel metadata flush failed");
2660 /* Flush all buffers after stopping */
2661 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2662 ret
= kernel_flush_buffer(kchan
);
2664 ERR("Kernel flush buffer error");
2668 ksession
->active
= 0;
2669 DBG("Kernel session stopped %s (id %" PRIu64
")", session
->name
,
2673 if (usess
&& usess
->active
) {
2674 ret
= ust_app_stop_trace_all(usess
);
2676 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2681 /* Flag inactive after a successful stop. */
2682 session
->active
= 0;
2683 ret
= !error_occurred
? LTTNG_OK
: LTTNG_ERR_UNK
;
2690 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2692 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2693 struct lttng_uri
*uris
)
2696 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2697 struct ltt_ust_session
*usess
= session
->ust_session
;
2703 /* Can't set consumer URI if the session is active. */
2704 if (session
->active
) {
2705 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2709 /* Set the "global" consumer URIs */
2710 for (i
= 0; i
< nb_uri
; i
++) {
2711 ret
= add_uri_to_consumer(session
,
2713 &uris
[i
], LTTNG_DOMAIN_NONE
);
2714 if (ret
!= LTTNG_OK
) {
2719 /* Set UST session URIs */
2720 if (session
->ust_session
) {
2721 for (i
= 0; i
< nb_uri
; i
++) {
2722 ret
= add_uri_to_consumer(session
,
2723 session
->ust_session
->consumer
,
2724 &uris
[i
], LTTNG_DOMAIN_UST
);
2725 if (ret
!= LTTNG_OK
) {
2731 /* Set kernel session URIs */
2732 if (session
->kernel_session
) {
2733 for (i
= 0; i
< nb_uri
; i
++) {
2734 ret
= add_uri_to_consumer(session
,
2735 session
->kernel_session
->consumer
,
2736 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2737 if (ret
!= LTTNG_OK
) {
2744 * Make sure to set the session in output mode after we set URI since a
2745 * session can be created without URL (thus flagged in no output mode).
2747 session
->output_traces
= 1;
2749 ksess
->output_traces
= 1;
2753 usess
->output_traces
= 1;
2764 enum lttng_error_code
set_session_output_from_descriptor(
2765 struct ltt_session
*session
,
2766 const struct lttng_session_descriptor
*descriptor
)
2769 enum lttng_error_code ret_code
= LTTNG_OK
;
2770 enum lttng_session_descriptor_type session_type
=
2771 lttng_session_descriptor_get_type(descriptor
);
2772 enum lttng_session_descriptor_output_type output_type
=
2773 lttng_session_descriptor_get_output_type(descriptor
);
2774 struct lttng_uri uris
[2] = {};
2775 size_t uri_count
= 0;
2777 switch (output_type
) {
2778 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2780 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2781 lttng_session_descriptor_get_local_output_uri(descriptor
,
2785 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2786 lttng_session_descriptor_get_network_output_uris(descriptor
,
2787 &uris
[0], &uris
[1]);
2791 ret_code
= LTTNG_ERR_INVALID
;
2795 switch (session_type
) {
2796 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2798 struct snapshot_output
*new_output
= NULL
;
2800 new_output
= snapshot_output_alloc();
2802 ret_code
= LTTNG_ERR_NOMEM
;
2806 ret
= snapshot_output_init_with_uri(session
,
2807 DEFAULT_SNAPSHOT_MAX_SIZE
,
2808 NULL
, uris
, uri_count
, session
->consumer
,
2809 new_output
, &session
->snapshot
);
2811 ret_code
= (ret
== -ENOMEM
) ?
2812 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2813 snapshot_output_destroy(new_output
);
2816 snapshot_add_output(&session
->snapshot
, new_output
);
2819 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2820 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2822 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2826 ret_code
= LTTNG_ERR_INVALID
;
2834 enum lttng_error_code
cmd_create_session_from_descriptor(
2835 struct lttng_session_descriptor
*descriptor
,
2836 const lttng_sock_cred
*creds
,
2837 const char *home_path
)
2840 enum lttng_error_code ret_code
;
2841 const char *session_name
;
2842 struct ltt_session
*new_session
= NULL
;
2843 enum lttng_session_descriptor_status descriptor_status
;
2845 session_lock_list();
2847 if (*home_path
!= '/') {
2848 ERR("Home path provided by client is not absolute");
2849 ret_code
= LTTNG_ERR_INVALID
;
2854 descriptor_status
= lttng_session_descriptor_get_session_name(
2855 descriptor
, &session_name
);
2856 switch (descriptor_status
) {
2857 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
2859 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
2860 session_name
= NULL
;
2863 ret_code
= LTTNG_ERR_INVALID
;
2866 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
2868 if (ret_code
!= LTTNG_OK
) {
2872 if (!session_name
) {
2873 ret
= lttng_session_descriptor_set_session_name(descriptor
,
2876 ret_code
= LTTNG_ERR_SESSION_FAIL
;
2881 if (!lttng_session_descriptor_is_output_destination_initialized(
2884 * Only include the session's creation time in the output
2885 * destination if the name of the session itself was
2886 * not auto-generated.
2888 ret_code
= lttng_session_descriptor_set_default_output(
2890 session_name
? &new_session
->creation_time
: NULL
,
2892 if (ret_code
!= LTTNG_OK
) {
2896 new_session
->has_user_specified_directory
=
2897 lttng_session_descriptor_has_output_directory(
2901 switch (lttng_session_descriptor_get_type(descriptor
)) {
2902 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2903 new_session
->snapshot_mode
= 1;
2905 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2906 new_session
->live_timer
=
2907 lttng_session_descriptor_live_get_timer_interval(
2914 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
2915 if (ret_code
!= LTTNG_OK
) {
2918 new_session
->consumer
->enabled
= 1;
2919 ret_code
= LTTNG_OK
;
2921 /* Release reference provided by the session_create function. */
2922 session_put(new_session
);
2923 if (ret_code
!= LTTNG_OK
&& new_session
) {
2924 /* Release the global reference on error. */
2925 session_destroy(new_session
);
2927 session_unlock_list();
2931 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
2932 struct lttng_session_descriptor
**return_descriptor
)
2935 size_t payload_size
;
2936 struct lttng_dynamic_buffer payload
;
2937 struct lttng_buffer_view home_dir_view
;
2938 struct lttng_buffer_view session_descriptor_view
;
2939 struct lttng_session_descriptor
*session_descriptor
= NULL
;
2940 enum lttng_error_code ret_code
;
2942 lttng_dynamic_buffer_init(&payload
);
2943 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
2945 ret_code
= LTTNG_ERR_INVALID
;
2948 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
2949 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
2950 ret_code
= LTTNG_ERR_INVALID
;
2954 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
2955 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
2956 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
2958 ret_code
= LTTNG_ERR_NOMEM
;
2962 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
2964 ERR("Reception of session descriptor failed, aborting.");
2965 ret_code
= LTTNG_ERR_SESSION_FAIL
;
2969 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
2972 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
2973 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
2975 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
2976 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
2978 ret
= lttng_session_descriptor_create_from_buffer(
2979 &session_descriptor_view
, &session_descriptor
);
2981 ERR("Failed to create session descriptor from payload of \"create session\" command");
2982 ret_code
= LTTNG_ERR_INVALID
;
2987 * Sets the descriptor's auto-generated properties (name, output) if
2990 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
2992 home_dir_view
.size
? home_dir_view
.data
: NULL
);
2993 if (ret_code
!= LTTNG_OK
) {
2997 ret_code
= LTTNG_OK
;
2998 *return_descriptor
= session_descriptor
;
2999 session_descriptor
= NULL
;
3001 lttng_dynamic_buffer_reset(&payload
);
3002 lttng_session_descriptor_destroy(session_descriptor
);
3007 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3009 * Called with session lock held.
3011 int cmd_destroy_session(struct ltt_session
*session
,
3012 struct notification_thread_handle
*notification_thread_handle
)
3019 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3021 if (session
->rotation_schedule_timer_enabled
) {
3022 if (timer_session_rotation_schedule_timer_stop(
3024 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3029 if (session
->rotate_size
) {
3030 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3031 session
->rotate_size
= 0;
3034 if (session
->most_recent_chunk_id
.is_set
&&
3035 session
->most_recent_chunk_id
.value
!= 0 &&
3036 session
->current_trace_chunk
) {
3037 ret
= cmd_rotate_session(session
, NULL
);
3038 if (ret
!= LTTNG_OK
) {
3039 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3040 session
->name
, lttng_strerror(-ret
));
3044 if (session
->shm_path
[0]) {
3046 * When a session is created with an explicit shm_path,
3047 * the consumer daemon will create its shared memory files
3048 * at that location and will *not* unlink them. This is normal
3049 * as the intention of that feature is to make it possible
3050 * to retrieve the content of those files should a crash occur.
3052 * To ensure the content of those files can be used, the
3053 * sessiond daemon will replicate the content of the metadata
3054 * cache in a metadata file.
3056 * On clean-up, it is expected that the consumer daemon will
3057 * unlink the shared memory files and that the session daemon
3058 * will unlink the metadata file. Then, the session's directory
3059 * in the shm path can be removed.
3061 * Unfortunately, a flaw in the design of the sessiond's and
3062 * consumerd's tear down of channels makes it impossible to
3063 * determine when the sessiond _and_ the consumerd have both
3064 * destroyed their representation of a channel. For one, the
3065 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3066 * callbacks in both daemons.
3068 * However, it is also impossible for the sessiond to know when
3069 * the consumer daemon is done destroying its channel(s) since
3070 * it occurs as a reaction to the closing of the channel's file
3071 * descriptor. There is no resulting communication initiated
3072 * from the consumerd to the sessiond to confirm that the
3073 * operation is completed (and was successful).
3075 * Until this is all fixed, the session daemon checks for the
3076 * removal of the session's shm path which makes it possible
3077 * to safely advertise a session as having been destroyed.
3079 * Prior to this fix, it was not possible to reliably save
3080 * a session making use of the --shm-path option, destroy it,
3081 * and load it again. This is because the creation of the
3082 * session would fail upon seeing the session's shm path
3083 * already in existence.
3085 * Note that none of the error paths in the check for the
3086 * directory's existence return an error. This is normal
3087 * as there isn't much that can be done. The session will
3088 * be destroyed properly, except that we can't offer the
3089 * guarantee that the same session can be re-created.
3091 current_completion_handler
= &destroy_completion_handler
.handler
;
3092 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3094 sizeof(destroy_completion_handler
.shm_path
));
3099 * The session is destroyed. However, note that the command context
3100 * still holds a reference to the session, thus delaying its destruction
3101 * _at least_ up to the point when that reference is released.
3103 session_destroy(session
);
3110 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3112 int cmd_register_consumer(struct ltt_session
*session
,
3113 enum lttng_domain_type domain
, const char *sock_path
,
3114 struct consumer_data
*cdata
)
3117 struct consumer_socket
*socket
= NULL
;
3124 case LTTNG_DOMAIN_KERNEL
:
3126 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3130 /* Can't register a consumer if there is already one */
3131 if (ksess
->consumer_fds_sent
!= 0) {
3132 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3136 sock
= lttcomm_connect_unix_sock(sock_path
);
3138 ret
= LTTNG_ERR_CONNECT_FAIL
;
3141 cdata
->cmd_sock
= sock
;
3143 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3144 if (socket
== NULL
) {
3147 PERROR("close register consumer");
3149 cdata
->cmd_sock
= -1;
3150 ret
= LTTNG_ERR_FATAL
;
3154 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3155 if (socket
->lock
== NULL
) {
3156 PERROR("zmalloc pthread mutex");
3157 ret
= LTTNG_ERR_FATAL
;
3160 pthread_mutex_init(socket
->lock
, NULL
);
3161 socket
->registered
= 1;
3164 consumer_add_socket(socket
, ksess
->consumer
);
3167 pthread_mutex_lock(&cdata
->pid_mutex
);
3169 pthread_mutex_unlock(&cdata
->pid_mutex
);
3174 /* TODO: Userspace tracing */
3175 ret
= LTTNG_ERR_UND
;
3183 consumer_destroy_socket(socket
);
3189 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3191 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3192 struct lttng_domain
**domains
)
3197 struct lttng_ht_iter iter
;
3199 if (session
->kernel_session
!= NULL
) {
3200 DBG3("Listing domains found kernel domain");
3204 if (session
->ust_session
!= NULL
) {
3205 DBG3("Listing domains found UST global domain");
3209 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3211 if (agt
->being_used
) {
3222 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3223 if (*domains
== NULL
) {
3224 ret
= LTTNG_ERR_FATAL
;
3228 if (session
->kernel_session
!= NULL
) {
3229 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3231 /* Kernel session buffer type is always GLOBAL */
3232 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3237 if (session
->ust_session
!= NULL
) {
3238 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3239 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3243 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3245 if (agt
->being_used
) {
3246 (*domains
)[index
].type
= agt
->domain
;
3247 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3257 /* Return negative value to differentiate return code */
3263 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3265 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3266 struct ltt_session
*session
, struct lttng_channel
**channels
)
3268 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3271 case LTTNG_DOMAIN_KERNEL
:
3272 if (session
->kernel_session
!= NULL
) {
3273 nb_chan
= session
->kernel_session
->channel_count
;
3275 DBG3("Number of kernel channels %zd", nb_chan
);
3277 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3281 case LTTNG_DOMAIN_UST
:
3282 if (session
->ust_session
!= NULL
) {
3284 nb_chan
= lttng_ht_get_count(
3285 session
->ust_session
->domain_global
.channels
);
3288 DBG3("Number of UST global channels %zd", nb_chan
);
3290 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3295 ret
= -LTTNG_ERR_UND
;
3300 const size_t channel_size
= sizeof(struct lttng_channel
) +
3301 sizeof(struct lttng_channel_extended
);
3302 struct lttng_channel_extended
*channel_exts
;
3304 payload_size
= nb_chan
* channel_size
;
3305 *channels
= zmalloc(payload_size
);
3306 if (*channels
== NULL
) {
3307 ret
= -LTTNG_ERR_FATAL
;
3311 channel_exts
= ((void *) *channels
) +
3312 (nb_chan
* sizeof(struct lttng_channel
));
3313 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3314 if (ret
!= LTTNG_OK
) {
3329 * Command LTTNG_LIST_EVENTS processed by the client thread.
3331 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3332 struct ltt_session
*session
, char *channel_name
,
3333 struct lttng_event
**events
, size_t *total_size
)
3336 ssize_t nb_event
= 0;
3339 case LTTNG_DOMAIN_KERNEL
:
3340 if (session
->kernel_session
!= NULL
) {
3341 nb_event
= list_lttng_kernel_events(channel_name
,
3342 session
->kernel_session
, events
,
3346 case LTTNG_DOMAIN_UST
:
3348 if (session
->ust_session
!= NULL
) {
3349 nb_event
= list_lttng_ust_global_events(channel_name
,
3350 &session
->ust_session
->domain_global
, events
,
3355 case LTTNG_DOMAIN_LOG4J
:
3356 case LTTNG_DOMAIN_JUL
:
3357 case LTTNG_DOMAIN_PYTHON
:
3358 if (session
->ust_session
) {
3359 struct lttng_ht_iter iter
;
3363 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3364 &iter
.iter
, agt
, node
.node
) {
3365 if (agt
->domain
== domain
) {
3366 nb_event
= list_lttng_agent_events(
3376 ret
= LTTNG_ERR_UND
;
3383 /* Return negative value to differentiate return code */
3388 * Using the session list, filled a lttng_session array to send back to the
3389 * client for session listing.
3391 * The session list lock MUST be acquired before calling this function. Use
3392 * session_lock_list() and session_unlock_list().
3394 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3395 size_t session_count
, uid_t uid
, gid_t gid
)
3399 struct ltt_session
*session
;
3400 struct ltt_session_list
*list
= session_get_list();
3401 struct lttng_session_extended
*extended
=
3402 (typeof(extended
)) (&sessions
[session_count
]);
3404 DBG("Getting all available session for UID %d GID %d",
3407 * Iterate over session list and append data after the control struct in
3410 cds_list_for_each_entry(session
, &list
->head
, list
) {
3411 if (!session_get(session
)) {
3415 * Only list the sessions the user can control.
3417 if (!session_access_ok(session
, uid
, gid
) ||
3418 session
->destroyed
) {
3419 session_put(session
);
3423 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3424 struct ltt_ust_session
*usess
= session
->ust_session
;
3426 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3427 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3428 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3429 ret
= build_network_session_path(sessions
[i
].path
,
3430 sizeof(sessions
[i
].path
), session
);
3432 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3433 session
->consumer
->dst
.session_root_path
);
3436 PERROR("snprintf session path");
3437 session_put(session
);
3441 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3442 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3443 sessions
[i
].enabled
= session
->active
;
3444 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3445 sessions
[i
].live_timer_interval
= session
->live_timer
;
3446 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3447 extended
[i
].creation_time
.is_set
= 1;
3449 session_put(session
);
3454 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3455 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3457 int cmd_data_pending(struct ltt_session
*session
)
3460 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3461 struct ltt_ust_session
*usess
= session
->ust_session
;
3465 DBG("Data pending for session %s", session
->name
);
3467 /* Session MUST be stopped to ask for data availability. */
3468 if (session
->active
) {
3469 ret
= LTTNG_ERR_SESSION_STARTED
;
3473 * If stopped, just make sure we've started before else the above call
3474 * will always send that there is data pending.
3476 * The consumer assumes that when the data pending command is received,
3477 * the trace has been started before or else no output data is written
3478 * by the streams which is a condition for data pending. So, this is
3479 * *VERY* important that we don't ask the consumer before a start
3482 if (!session
->has_been_started
) {
3488 /* A rotation is still pending, we have to wait. */
3489 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3490 DBG("Rotate still pending for session %s", session
->name
);
3495 if (ksess
&& ksess
->consumer
) {
3496 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3498 /* Data is still being extracted for the kernel. */
3503 if (usess
&& usess
->consumer
) {
3504 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3506 /* Data is still being extracted for the kernel. */
3511 /* Data is ready to be read by a viewer */
3519 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3521 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3523 int cmd_snapshot_add_output(struct ltt_session
*session
,
3524 struct lttng_snapshot_output
*output
, uint32_t *id
)
3527 struct snapshot_output
*new_output
;
3532 DBG("Cmd snapshot add output for session %s", session
->name
);
3535 * Can't create an output if the session is not set in no-output mode.
3537 if (session
->output_traces
) {
3538 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3542 if (session
->has_non_mmap_channel
) {
3543 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3547 /* Only one output is allowed until we have the "tee" feature. */
3548 if (session
->snapshot
.nb_output
== 1) {
3549 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3553 new_output
= snapshot_output_alloc();
3555 ret
= LTTNG_ERR_NOMEM
;
3559 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3560 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3561 &session
->snapshot
);
3563 if (ret
== -ENOMEM
) {
3564 ret
= LTTNG_ERR_NOMEM
;
3566 ret
= LTTNG_ERR_INVALID
;
3572 snapshot_add_output(&session
->snapshot
, new_output
);
3574 *id
= new_output
->id
;
3581 snapshot_output_destroy(new_output
);
3587 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3589 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3591 int cmd_snapshot_del_output(struct ltt_session
*session
,
3592 struct lttng_snapshot_output
*output
)
3595 struct snapshot_output
*sout
= NULL
;
3603 * Permission denied to create an output if the session is not
3604 * set in no output mode.
3606 if (session
->output_traces
) {
3607 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3612 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3614 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3615 } else if (*output
->name
!= '\0') {
3616 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3618 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3621 ret
= LTTNG_ERR_INVALID
;
3625 snapshot_delete_output(&session
->snapshot
, sout
);
3626 snapshot_output_destroy(sout
);
3635 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3637 * If no output is available, outputs is untouched and 0 is returned.
3639 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3641 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3642 struct lttng_snapshot_output
**outputs
)
3645 struct lttng_snapshot_output
*list
= NULL
;
3646 struct lttng_ht_iter iter
;
3647 struct snapshot_output
*output
;
3652 DBG("Cmd snapshot list outputs for session %s", session
->name
);
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
;
3663 if (session
->snapshot
.nb_output
== 0) {
3668 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3670 ret
= -LTTNG_ERR_NOMEM
;
3674 /* Copy list from session to the new list object. */
3676 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3677 output
, node
.node
) {
3678 assert(output
->consumer
);
3679 list
[idx
].id
= output
->id
;
3680 list
[idx
].max_size
= output
->max_size
;
3681 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3682 sizeof(list
[idx
].name
))) {
3683 ret
= -LTTNG_ERR_INVALID
;
3686 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3687 if (lttng_strncpy(list
[idx
].ctrl_url
,
3688 output
->consumer
->dst
.session_root_path
,
3689 sizeof(list
[idx
].ctrl_url
))) {
3690 ret
= -LTTNG_ERR_INVALID
;
3695 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3696 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3698 ret
= -LTTNG_ERR_NOMEM
;
3703 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3704 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3706 ret
= -LTTNG_ERR_NOMEM
;
3715 ret
= session
->snapshot
.nb_output
;
3724 * Check if we can regenerate the metadata for this session.
3725 * Only kernel, UST per-uid and non-live sessions are supported.
3727 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3730 int check_regenerate_metadata_support(struct ltt_session
*session
)
3736 if (session
->live_timer
!= 0) {
3737 ret
= LTTNG_ERR_LIVE_SESSION
;
3740 if (!session
->active
) {
3741 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3744 if (session
->ust_session
) {
3745 switch (session
->ust_session
->buffer_type
) {
3746 case LTTNG_BUFFER_PER_UID
:
3748 case LTTNG_BUFFER_PER_PID
:
3749 ret
= LTTNG_ERR_PER_PID_SESSION
;
3753 ret
= LTTNG_ERR_UNK
;
3757 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3758 session
->consumer
->relay_minor_version
< 8) {
3759 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3769 int clear_metadata_file(int fd
)
3774 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
3775 if (lseek_ret
< 0) {
3781 ret
= ftruncate(fd
, 0);
3783 PERROR("ftruncate");
3792 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3795 struct buffer_reg_uid
*uid_reg
= NULL
;
3796 struct buffer_reg_session
*session_reg
= NULL
;
3799 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3800 struct ust_registry_session
*registry
;
3801 struct ust_registry_channel
*chan
;
3802 struct lttng_ht_iter iter_chan
;
3804 session_reg
= uid_reg
->registry
;
3805 registry
= session_reg
->reg
.ust
;
3807 pthread_mutex_lock(®istry
->lock
);
3808 registry
->metadata_len_sent
= 0;
3809 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3810 registry
->metadata_len
= 0;
3811 registry
->metadata_version
++;
3812 if (registry
->metadata_fd
> 0) {
3813 /* Clear the metadata file's content. */
3814 ret
= clear_metadata_file(registry
->metadata_fd
);
3816 pthread_mutex_unlock(®istry
->lock
);
3821 ret
= ust_metadata_session_statedump(registry
, NULL
,
3822 registry
->major
, registry
->minor
);
3824 pthread_mutex_unlock(®istry
->lock
);
3825 ERR("Failed to generate session metadata (err = %d)",
3829 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3831 struct ust_registry_event
*event
;
3832 struct lttng_ht_iter iter_event
;
3834 ret
= ust_metadata_channel_statedump(registry
, chan
);
3836 pthread_mutex_unlock(®istry
->lock
);
3837 ERR("Failed to generate channel metadata "
3841 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3843 ret
= ust_metadata_event_statedump(registry
,
3846 pthread_mutex_unlock(®istry
->lock
);
3847 ERR("Failed to generate event metadata "
3853 pthread_mutex_unlock(®istry
->lock
);
3862 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3864 * Ask the consumer to truncate the existing metadata file(s) and
3865 * then regenerate the metadata. Live and per-pid sessions are not
3866 * supported and return an error.
3868 * Return 0 on success or else a LTTNG_ERR code.
3870 int cmd_regenerate_metadata(struct ltt_session
*session
)
3876 ret
= check_regenerate_metadata_support(session
);
3881 if (session
->kernel_session
) {
3882 ret
= kernctl_session_regenerate_metadata(
3883 session
->kernel_session
->fd
);
3885 ERR("Failed to regenerate the kernel metadata");
3890 if (session
->ust_session
) {
3891 ret
= ust_regenerate_metadata(session
->ust_session
);
3893 ERR("Failed to regenerate the UST metadata");
3897 DBG("Cmd metadata regenerate for session %s", session
->name
);
3905 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3907 * Ask the tracer to regenerate a new statedump.
3909 * Return 0 on success or else a LTTNG_ERR code.
3911 int cmd_regenerate_statedump(struct ltt_session
*session
)
3917 if (!session
->active
) {
3918 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3922 if (session
->kernel_session
) {
3923 ret
= kernctl_session_regenerate_statedump(
3924 session
->kernel_session
->fd
);
3926 * Currently, the statedump in kernel can only fail if out
3930 if (ret
== -ENOMEM
) {
3931 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3933 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3935 ERR("Failed to regenerate the kernel statedump");
3940 if (session
->ust_session
) {
3941 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3943 * Currently, the statedump in UST always returns 0.
3946 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3947 ERR("Failed to regenerate the UST statedump");
3951 DBG("Cmd regenerate statedump for session %s", session
->name
);
3958 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3959 struct notification_thread_handle
*notification_thread
)
3963 ssize_t sock_recv_len
;
3964 struct lttng_trigger
*trigger
= NULL
;
3965 struct lttng_buffer_view view
;
3966 struct lttng_dynamic_buffer trigger_buffer
;
3968 lttng_dynamic_buffer_init(&trigger_buffer
);
3969 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3970 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3972 ret
= LTTNG_ERR_NOMEM
;
3976 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3978 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3979 ERR("Failed to receive \"register trigger\" command payload");
3980 /* TODO: should this be a new error enum ? */
3981 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3985 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3986 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3988 ERR("Invalid trigger payload received in \"register trigger\" command");
3989 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3993 ret
= notification_thread_command_register_trigger(notification_thread
,
3995 /* Ownership of trigger was transferred. */
3998 lttng_trigger_destroy(trigger
);
3999 lttng_dynamic_buffer_reset(&trigger_buffer
);
4003 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4004 struct notification_thread_handle
*notification_thread
)
4008 ssize_t sock_recv_len
;
4009 struct lttng_trigger
*trigger
= NULL
;
4010 struct lttng_buffer_view view
;
4011 struct lttng_dynamic_buffer trigger_buffer
;
4013 lttng_dynamic_buffer_init(&trigger_buffer
);
4014 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4015 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4017 ret
= LTTNG_ERR_NOMEM
;
4021 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4023 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4024 ERR("Failed to receive \"unregister trigger\" command payload");
4025 /* TODO: should this be a new error enum ? */
4026 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4030 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4031 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4033 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4034 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4038 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4041 lttng_trigger_destroy(trigger
);
4042 lttng_dynamic_buffer_reset(&trigger_buffer
);
4047 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4048 * snapshot output is *not* set with a remote destination.
4050 * Return LTTNG_OK on success or a LTTNG_ERR code.
4052 static enum lttng_error_code
set_relayd_for_snapshot(
4053 struct consumer_output
*consumer
,
4054 const struct snapshot_output
*snap_output
,
4055 const struct ltt_session
*session
)
4057 enum lttng_error_code status
= LTTNG_OK
;
4058 struct lttng_ht_iter iter
;
4059 struct consumer_socket
*socket
;
4062 assert(snap_output
);
4065 DBG2("Set relayd object from snapshot output");
4067 /* Ignore if snapshot consumer output is not network. */
4068 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
4073 * For each consumer socket, create and send the relayd object of the
4077 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
4078 socket
, node
.node
) {
4079 pthread_mutex_lock(socket
->lock
);
4080 status
= send_consumer_relayd_sockets(0, session
->id
,
4081 snap_output
->consumer
, socket
,
4082 session
->name
, session
->hostname
,
4083 session
->live_timer
);
4084 pthread_mutex_unlock(socket
->lock
);
4085 if (status
!= LTTNG_OK
) {
4097 * Record a kernel snapshot.
4099 * Return LTTNG_OK on success or a LTTNG_ERR code.
4101 static enum lttng_error_code
record_kernel_snapshot(
4102 struct ltt_kernel_session
*ksess
,
4103 const struct snapshot_output
*output
,
4104 const struct ltt_session
*session
,
4105 int wait
, uint64_t nb_packets_per_stream
)
4108 enum lttng_error_code status
;
4115 * Copy kernel session sockets so we can communicate with the right
4116 * consumer for the snapshot record command.
4118 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
4120 status
= LTTNG_ERR_NOMEM
;
4124 status
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
4125 if (status
!= LTTNG_OK
) {
4126 goto error_snapshot
;
4129 status
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
4130 if (status
!= LTTNG_OK
) {
4131 goto error_snapshot
;
4137 /* Clean up copied sockets so this output can use some other later on. */
4138 consumer_destroy_output_sockets(output
->consumer
);
4145 * Record a UST snapshot.
4147 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4149 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4150 const struct snapshot_output
*output
,
4151 const struct ltt_session
*session
, int wait
,
4152 uint64_t nb_packets_per_stream
)
4155 enum lttng_error_code status
;
4162 * Copy UST session sockets so we can communicate with the right
4163 * consumer for the snapshot record command.
4165 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
4167 status
= LTTNG_ERR_NOMEM
;
4171 status
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
4172 if (status
!= LTTNG_OK
) {
4173 goto error_snapshot
;
4176 status
= ust_app_snapshot_record(usess
, output
, wait
,
4177 nb_packets_per_stream
);
4178 if (status
!= LTTNG_OK
) {
4179 goto error_snapshot
;
4185 /* Clean up copied sockets so this output can use some other later on. */
4186 consumer_destroy_output_sockets(output
->consumer
);
4193 uint64_t get_session_size_one_more_packet_per_stream(
4194 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4196 uint64_t tot_size
= 0;
4198 if (session
->kernel_session
) {
4199 struct ltt_kernel_channel
*chan
;
4200 const struct ltt_kernel_session
*ksess
=
4201 session
->kernel_session
;
4203 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4204 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4206 * Don't take channel into account if we
4207 * already grab all its packets.
4211 tot_size
+= chan
->channel
->attr
.subbuf_size
4212 * chan
->stream_count
;
4216 if (session
->ust_session
) {
4217 const struct ltt_ust_session
*usess
= session
->ust_session
;
4219 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4227 * Calculate the number of packets we can grab from each stream that
4228 * fits within the overall snapshot max size.
4230 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4231 * the number of packets per stream.
4233 * TODO: this approach is not perfect: we consider the worse case
4234 * (packet filling the sub-buffers) as an upper bound, but we could do
4235 * better if we do this calculation while we actually grab the packet
4236 * content: we would know how much padding we don't actually store into
4239 * This algorithm is currently bounded by the number of packets per
4242 * Since we call this algorithm before actually grabbing the data, it's
4243 * an approximation: for instance, applications could appear/disappear
4244 * in between this call and actually grabbing data.
4247 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4251 uint64_t cur_nb_packets
= 0;
4254 return 0; /* Infinite */
4257 size_left
= max_size
;
4259 uint64_t one_more_packet_tot_size
;
4261 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4262 session
, cur_nb_packets
);
4263 if (!one_more_packet_tot_size
) {
4264 /* We are already grabbing all packets. */
4267 size_left
-= one_more_packet_tot_size
;
4268 if (size_left
< 0) {
4273 if (!cur_nb_packets
) {
4274 /* Not enough room to grab one packet of each stream, error. */
4277 return cur_nb_packets
;
4281 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4282 const struct snapshot_output
*snapshot_output
, int wait
)
4285 int64_t nb_packets_per_stream
;
4286 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4287 enum lttng_error_code ret
= LTTNG_OK
;
4288 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4290 fmt_ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4292 snapshot_output
->name
,
4293 snapshot_output
->datetime
,
4294 snapshot_output
->nb_snapshot
);
4295 if (fmt_ret
< 0 || fmt_ret
>= sizeof(snapshot_chunk_name
)) {
4296 ERR("Failed to format snapshot name");
4297 ret
= LTTNG_ERR_INVALID
;
4300 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4301 snapshot_output
->name
, session
->name
,
4302 snapshot_chunk_name
);
4303 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4304 snapshot_output_get_base_path(snapshot_output
),
4305 snapshot_chunk_name
);
4306 if (!snapshot_trace_chunk
) {
4307 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
4310 assert(!session
->current_trace_chunk
);
4311 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4312 lttng_trace_chunk_put(snapshot_trace_chunk
);
4313 snapshot_trace_chunk
= NULL
;
4315 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4319 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4320 snapshot_output
->max_size
);
4321 if (nb_packets_per_stream
< 0) {
4322 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4326 if (session
->kernel_session
) {
4327 ret
= record_kernel_snapshot(session
->kernel_session
,
4328 snapshot_output
, session
,
4329 wait
, nb_packets_per_stream
);
4330 if (ret
!= LTTNG_OK
) {
4335 if (session
->ust_session
) {
4336 ret
= record_ust_snapshot(session
->ust_session
,
4337 snapshot_output
, session
,
4338 wait
, nb_packets_per_stream
);
4339 if (ret
!= LTTNG_OK
) {
4344 if (session_close_trace_chunk(session
, session
->current_trace_chunk
)) {
4346 * Don't goto end; make sure the chunk is closed for the session
4347 * to allow future snapshots.
4349 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4353 if (session_set_trace_chunk(session
, NULL
, NULL
)) {
4354 ERR("Failed to release the current trace chunk of session \"%s\"",
4363 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4365 * The wait parameter is ignored so this call always wait for the snapshot to
4366 * complete before returning.
4368 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4370 int cmd_snapshot_record(struct ltt_session
*session
,
4371 struct lttng_snapshot_output
*output
, int wait
)
4373 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4375 unsigned int use_tmp_output
= 0;
4376 struct snapshot_output tmp_output
;
4377 unsigned int snapshot_success
= 0;
4383 DBG("Cmd snapshot record for session %s", session
->name
);
4385 /* Get the datetime for the snapshot output directory. */
4386 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4389 cmd_ret
= LTTNG_ERR_INVALID
;
4394 * Permission denied to create an output if the session is not
4395 * set in no output mode.
4397 if (session
->output_traces
) {
4398 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4402 /* The session needs to be started at least once. */
4403 if (!session
->has_been_started
) {
4404 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4408 /* Use temporary output for the session. */
4409 if (*output
->ctrl_url
!= '\0') {
4410 ret
= snapshot_output_init(session
, output
->max_size
,
4412 output
->ctrl_url
, output
->data_url
,
4416 if (ret
== -ENOMEM
) {
4417 cmd_ret
= LTTNG_ERR_NOMEM
;
4419 cmd_ret
= LTTNG_ERR_INVALID
;
4423 /* Use the global session count for the temporary snapshot. */
4424 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4426 /* Use the global datetime */
4427 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4431 if (use_tmp_output
) {
4432 cmd_ret
= snapshot_record(session
, &tmp_output
, wait
);
4433 if (cmd_ret
!= LTTNG_OK
) {
4436 snapshot_success
= 1;
4438 struct snapshot_output
*sout
;
4439 struct lttng_ht_iter iter
;
4442 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4443 &iter
.iter
, sout
, node
.node
) {
4445 * Make a local copy of the output and assign the
4446 * possible temporary value given by the caller.
4448 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
4450 if (output
->max_size
!= (uint64_t) -1ULL) {
4451 tmp_output
.max_size
= output
->max_size
;
4454 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4455 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4457 /* Use temporary name. */
4458 if (*output
->name
!= '\0') {
4459 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4460 sizeof(tmp_output
.name
))) {
4461 cmd_ret
= LTTNG_ERR_INVALID
;
4467 cmd_ret
= snapshot_record(session
, &tmp_output
, wait
);
4468 if (cmd_ret
!= LTTNG_OK
) {
4472 snapshot_success
= 1;
4477 if (snapshot_success
) {
4478 session
->snapshot
.nb_snapshot
++;
4480 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4488 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4490 int cmd_set_session_shm_path(struct ltt_session
*session
,
4491 const char *shm_path
)
4497 * Can only set shm path before session is started.
4499 if (session
->has_been_started
) {
4500 return LTTNG_ERR_SESSION_STARTED
;
4503 strncpy(session
->shm_path
, shm_path
,
4504 sizeof(session
->shm_path
));
4505 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4511 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4513 * Ask the consumer to rotate the session output directory.
4514 * The session lock must be held.
4516 * Returns LTTNG_OK on success or else a negative LTTng error code.
4518 int cmd_rotate_session(struct ltt_session
*session
,
4519 struct lttng_rotate_session_return
*rotate_return
)
4522 uint64_t ongoing_rotation_chunk_id
;
4523 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4524 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
4525 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
4526 enum lttng_trace_chunk_status chunk_status
;
4530 if (!session
->has_been_started
) {
4531 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4535 if (session
->live_timer
|| !session
->output_traces
) {
4536 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4540 /* Unsupported feature in lttng-relayd before 2.11. */
4541 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4542 (session
->consumer
->relay_major_version
== 2 &&
4543 session
->consumer
->relay_minor_version
< 11)) {
4544 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
4548 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4549 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
4551 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
4556 * After a stop, we only allow one rotation to occur, the other ones are
4557 * useless until a new start.
4559 if (session
->rotated_after_last_stop
) {
4560 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4562 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
4566 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
4568 if (session
->active
) {
4569 new_trace_chunk
= session_create_new_trace_chunk(session
,
4571 if (!new_trace_chunk
) {
4572 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
4577 /* The current trace chunk becomes the chunk being archived. */
4578 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
4579 &chunk_being_archived
);
4581 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4585 assert(chunk_being_archived
);
4586 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
4587 &ongoing_rotation_chunk_id
);
4588 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
4590 chunk_status
= lttng_trace_chunk_set_close_command(
4591 chunk_being_archived
,
4592 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
4593 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
4594 cmd_ret
= LTTNG_ERR_FATAL
;
4598 if (session
->kernel_session
) {
4599 cmd_ret
= kernel_rotate_session(session
);
4600 if (cmd_ret
!= LTTNG_OK
) {
4604 if (session
->ust_session
) {
4605 cmd_ret
= ust_app_rotate_session(session
);
4606 if (cmd_ret
!= LTTNG_OK
) {
4611 ret
= session_close_trace_chunk(session
, chunk_being_archived
);
4613 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4617 ret
= timer_session_rotation_pending_check_start(session
,
4618 DEFAULT_ROTATE_PENDING_TIMER
);
4620 cmd_ret
= LTTNG_ERR_UNK
;
4624 if (!session
->active
) {
4625 session
->rotated_after_last_stop
= true;
4628 if (rotate_return
) {
4629 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
4632 session
->chunk_being_archived
= chunk_being_archived
;
4633 chunk_being_archived
= NULL
;
4634 ret
= notification_thread_command_session_rotation_ongoing(
4635 notification_thread_handle
,
4636 session
->name
, session
->uid
, session
->gid
,
4637 ongoing_rotation_chunk_id
);
4638 if (ret
!= LTTNG_OK
) {
4639 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
4644 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
4645 session
->name
, ongoing_rotation_chunk_id
);
4647 lttng_trace_chunk_put(new_trace_chunk
);
4648 lttng_trace_chunk_put(chunk_being_archived
);
4649 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
4652 if (session_reset_rotation_state(session
,
4653 LTTNG_ROTATION_STATE_ERROR
)) {
4654 ERR("Failed to reset rotation state of session \"%s\"",
4661 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
4663 * Check if the session has finished its rotation.
4665 * Return LTTNG_OK on success or else an LTTNG_ERR code.
4667 int cmd_rotate_get_info(struct ltt_session
*session
,
4668 struct lttng_rotation_get_info_return
*info_return
,
4669 uint64_t rotation_id
)
4671 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4672 enum lttng_rotation_state rotation_state
;
4674 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
4675 session
->most_recent_chunk_id
.value
);
4677 if (session
->chunk_being_archived
) {
4678 enum lttng_trace_chunk_status chunk_status
;
4681 chunk_status
= lttng_trace_chunk_get_id(
4682 session
->chunk_being_archived
,
4684 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
4686 rotation_state
= rotation_id
== chunk_id
?
4687 LTTNG_ROTATION_STATE_ONGOING
:
4688 LTTNG_ROTATION_STATE_EXPIRED
;
4690 if (session
->last_archived_chunk_id
.is_set
&&
4691 rotation_id
!= session
->last_archived_chunk_id
.value
) {
4692 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
4694 rotation_state
= session
->rotation_state
;
4698 switch (rotation_state
) {
4699 case LTTNG_ROTATION_STATE_NO_ROTATION
:
4700 DBG("Reporting that no rotation has occured within the lifetime of session \"%s\"",
4703 case LTTNG_ROTATION_STATE_EXPIRED
:
4704 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
4705 rotation_id
, session
->name
);
4707 case LTTNG_ROTATION_STATE_ONGOING
:
4708 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
4709 rotation_id
, session
->name
);
4711 case LTTNG_ROTATION_STATE_COMPLETED
:
4715 char *current_tracing_path_reply
;
4716 size_t current_tracing_path_reply_len
;
4718 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
4719 rotation_id
, session
->name
);
4721 switch (session_get_consumer_destination_type(session
)) {
4722 case CONSUMER_DST_LOCAL
:
4723 current_tracing_path_reply
=
4724 info_return
->location
.local
.absolute_path
;
4725 current_tracing_path_reply_len
=
4726 sizeof(info_return
->location
.local
.absolute_path
);
4727 info_return
->location_type
=
4728 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
4730 case CONSUMER_DST_NET
:
4731 current_tracing_path_reply
=
4732 info_return
->location
.relay
.relative_path
;
4733 current_tracing_path_reply_len
=
4734 sizeof(info_return
->location
.relay
.relative_path
);
4735 /* Currently the only supported relay protocol. */
4736 info_return
->location
.relay
.protocol
=
4737 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
4739 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
4740 session_get_net_consumer_hostname(session
),
4741 sizeof(info_return
->location
.relay
.host
));
4743 ERR("Failed to copy host name to rotate_get_info reply");
4744 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4745 cmd_ret
= LTTNG_ERR_SET_URL
;
4749 session_get_net_consumer_ports(session
,
4750 &info_return
->location
.relay
.ports
.control
,
4751 &info_return
->location
.relay
.ports
.data
);
4752 info_return
->location_type
=
4753 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
4758 fmt_ret
= asprintf(&chunk_path
,
4759 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
4760 session_get_base_path(session
),
4761 session
->last_archived_chunk_name
);
4762 if (fmt_ret
== -1) {
4763 PERROR("Failed to format the path of the last archived trace chunk");
4764 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4765 cmd_ret
= LTTNG_ERR_UNK
;
4769 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
4770 chunk_path
, current_tracing_path_reply_len
);
4773 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
4774 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4775 cmd_ret
= LTTNG_ERR_UNK
;
4781 case LTTNG_ROTATION_STATE_ERROR
:
4782 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
4783 rotation_id
, session
->name
);
4791 info_return
->status
= (int32_t) rotation_state
;
4796 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
4798 * Configure the automatic rotation parameters.
4799 * 'activate' to true means activate the rotation schedule type with 'new_value'.
4800 * 'activate' to false means deactivate the rotation schedule and validate that
4801 * 'new_value' has the same value as the currently active value.
4803 * Return 0 on success or else a positive LTTNG_ERR code.
4805 int cmd_rotation_set_schedule(struct ltt_session
*session
,
4806 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
4808 struct notification_thread_handle
*notification_thread_handle
)
4811 uint64_t *parameter_value
;
4815 DBG("Cmd rotate set schedule session %s", session
->name
);
4817 if (session
->live_timer
|| !session
->output_traces
) {
4818 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
4819 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4823 switch (schedule_type
) {
4824 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
4825 parameter_value
= &session
->rotate_size
;
4827 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
4828 parameter_value
= &session
->rotate_timer_period
;
4829 if (new_value
>= UINT_MAX
) {
4830 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
4831 new_value
, UINT_MAX
);
4832 ret
= LTTNG_ERR_INVALID
;
4837 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
4838 ret
= LTTNG_ERR_INVALID
;
4842 /* Improper use of the API. */
4843 if (new_value
== -1ULL) {
4844 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
4845 ret
= LTTNG_ERR_INVALID
;
4850 * As indicated in struct ltt_session's comments, a value of == 0 means
4851 * this schedule rotation type is not in use.
4853 * Reject the command if we were asked to activate a schedule that was
4856 if (activate
&& *parameter_value
!= 0) {
4857 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
4858 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
4863 * Reject the command if we were asked to deactivate a schedule that was
4866 if (!activate
&& *parameter_value
== 0) {
4867 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
4868 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
4873 * Reject the command if we were asked to deactivate a schedule that
4876 if (!activate
&& *parameter_value
!= new_value
) {
4877 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
4878 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
4882 *parameter_value
= activate
? new_value
: 0;
4884 switch (schedule_type
) {
4885 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
4886 if (activate
&& session
->active
) {
4888 * Only start the timer if the session is active,
4889 * otherwise it will be started when the session starts.
4891 ret
= timer_session_rotation_schedule_timer_start(
4892 session
, new_value
);
4894 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
4895 ret
= LTTNG_ERR_UNK
;
4899 ret
= timer_session_rotation_schedule_timer_stop(
4902 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
4903 ret
= LTTNG_ERR_UNK
;
4908 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
4910 ret
= subscribe_session_consumed_size_rotation(session
,
4911 new_value
, notification_thread_handle
);
4913 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
4914 ret
= LTTNG_ERR_UNK
;
4918 ret
= unsubscribe_session_consumed_size_rotation(session
,
4919 notification_thread_handle
);
4921 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
4922 ret
= LTTNG_ERR_UNK
;
4929 /* Would have been caught before. */
4941 /* Wait for a given path to be removed before continuing. */
4942 static enum lttng_error_code
wait_on_path(void *path_data
)
4944 const char *shm_path
= path_data
;
4946 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
4952 ret
= stat(shm_path
, &st
);
4954 if (errno
!= ENOENT
) {
4955 PERROR("stat() returned an error while checking for the existence of the shm path");
4957 DBG("shm path no longer exists, completing the destruction of session");
4961 if (!S_ISDIR(st
.st_mode
)) {
4962 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
4967 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
4973 * Returns a pointer to a handler to run on completion of a command.
4974 * Returns NULL if no handler has to be run for the last command executed.
4976 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
4978 struct cmd_completion_handler
*handler
= current_completion_handler
;
4980 current_completion_handler
= NULL
;
4985 * Init command subsystem.
4990 * Set network sequence index to 1 for streams to match a relayd
4991 * socket on the consumer side.
4993 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4994 relayd_net_seq_idx
= 1;
4995 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4997 DBG("Command subsystem initialized");