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/location-internal.h>
38 #include <lttng/trigger/trigger-internal.h>
39 #include <lttng/condition/condition.h>
40 #include <lttng/action/action.h>
41 #include <lttng/channel.h>
42 #include <lttng/channel-internal.h>
43 #include <lttng/rotate-internal.h>
44 #include <lttng/location-internal.h>
45 #include <lttng/session-internal.h>
46 #include <lttng/userspace-probe-internal.h>
47 #include <lttng/session-descriptor-internal.h>
48 #include <common/string-utils/string-utils.h>
53 #include "health-sessiond.h"
55 #include "kernel-consumer.h"
56 #include "lttng-sessiond.h"
58 #include "lttng-syscall.h"
60 #include "buffer-registry.h"
61 #include "notification-thread.h"
62 #include "notification-thread-commands.h"
64 #include "rotation-thread.h"
66 #include "agent-thread.h"
70 /* Sleep for 100ms between each check for the shm path's deletion. */
71 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
73 struct cmd_destroy_session_reply_context
{
75 bool implicit_rotation_on_destroy
;
78 static enum lttng_error_code
wait_on_path(void *path
);
81 * Command completion handler that is used by the destroy command
82 * when a session that has a non-default shm_path is being destroyed.
84 * See comment in cmd_destroy_session() for the rationale.
86 static struct destroy_completion_handler
{
87 struct cmd_completion_handler handler
;
88 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
89 } destroy_completion_handler
= {
92 .data
= destroy_completion_handler
.shm_path
97 static struct cmd_completion_handler
*current_completion_handler
;
100 * Used to keep a unique index for each relayd socket created where this value
101 * is associated with streams on the consumer so it can match the right relayd
102 * to send to. It must be accessed with the relayd_net_seq_idx_lock
105 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
106 static uint64_t relayd_net_seq_idx
;
108 static int validate_ust_event_name(const char *);
109 static int cmd_enable_event_internal(struct ltt_session
*session
,
110 struct lttng_domain
*domain
,
111 char *channel_name
, struct lttng_event
*event
,
112 char *filter_expression
,
113 struct lttng_filter_bytecode
*filter
,
114 struct lttng_event_exclusion
*exclusion
,
118 * Create a session path used by list_lttng_sessions for the case that the
119 * session consumer is on the network.
121 static int build_network_session_path(char *dst
, size_t size
,
122 struct ltt_session
*session
)
124 int ret
, kdata_port
, udata_port
;
125 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
126 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
131 memset(tmp_urls
, 0, sizeof(tmp_urls
));
132 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
134 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
136 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
137 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
138 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
141 if (session
->ust_session
&& session
->ust_session
->consumer
) {
142 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
143 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
146 if (uuri
== NULL
&& kuri
== NULL
) {
147 uri
= &session
->consumer
->dst
.net
.control
;
148 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
149 } else if (kuri
&& uuri
) {
150 ret
= uri_compare(kuri
, uuri
);
154 /* Build uuri URL string */
155 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
162 } else if (kuri
&& uuri
== NULL
) {
164 } else if (uuri
&& kuri
== NULL
) {
168 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
174 * Do we have a UST url set. If yes, this means we have both kernel and UST
177 if (*tmp_uurl
!= '\0') {
178 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
179 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
182 if (kuri
|| (!kuri
&& !uuri
)) {
185 /* No kernel URI, use the UST port. */
188 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
196 * Get run-time attributes if the session has been started (discarded events,
199 static int get_kernel_runtime_stats(struct ltt_session
*session
,
200 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
201 uint64_t *lost_packets
)
205 if (!session
->has_been_started
) {
207 *discarded_events
= 0;
212 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
213 session
->kernel_session
->consumer
,
219 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
220 session
->kernel_session
->consumer
,
231 * Get run-time attributes if the session has been started (discarded events,
234 static int get_ust_runtime_stats(struct ltt_session
*session
,
235 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
236 uint64_t *lost_packets
)
239 struct ltt_ust_session
*usess
;
241 if (!discarded_events
|| !lost_packets
) {
246 usess
= session
->ust_session
;
247 assert(discarded_events
);
248 assert(lost_packets
);
250 if (!usess
|| !session
->has_been_started
) {
251 *discarded_events
= 0;
257 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
258 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
259 &usess
->buffer_reg_uid_list
,
260 usess
->consumer
, uchan
->id
,
261 uchan
->attr
.overwrite
,
264 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
265 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
266 uchan
, usess
->consumer
,
267 uchan
->attr
.overwrite
,
273 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
274 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
276 ERR("Unsupported buffer type");
287 * Fill lttng_channel array of all channels.
289 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
290 struct ltt_session
*session
, struct lttng_channel
*channels
,
291 struct lttng_channel_extended
*chan_exts
)
294 struct ltt_kernel_channel
*kchan
;
296 DBG("Listing channels for session %s", session
->name
);
299 case LTTNG_DOMAIN_KERNEL
:
300 /* Kernel channels */
301 if (session
->kernel_session
!= NULL
) {
302 cds_list_for_each_entry(kchan
,
303 &session
->kernel_session
->channel_list
.head
, list
) {
304 uint64_t discarded_events
, lost_packets
;
305 struct lttng_channel_extended
*extended
;
307 extended
= (struct lttng_channel_extended
*)
308 kchan
->channel
->attr
.extended
.ptr
;
310 ret
= get_kernel_runtime_stats(session
, kchan
,
311 &discarded_events
, &lost_packets
);
315 /* Copy lttng_channel struct to array */
316 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
317 channels
[i
].enabled
= kchan
->enabled
;
318 chan_exts
[i
].discarded_events
=
320 chan_exts
[i
].lost_packets
= lost_packets
;
321 chan_exts
[i
].monitor_timer_interval
=
322 extended
->monitor_timer_interval
;
323 chan_exts
[i
].blocking_timeout
= 0;
328 case LTTNG_DOMAIN_UST
:
330 struct lttng_ht_iter iter
;
331 struct ltt_ust_channel
*uchan
;
334 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
335 &iter
.iter
, uchan
, node
.node
) {
336 uint64_t discarded_events
= 0, lost_packets
= 0;
338 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
339 LTTNG_SYMBOL_NAME_LEN
)) {
342 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
343 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
344 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
345 channels
[i
].attr
.switch_timer_interval
=
346 uchan
->attr
.switch_timer_interval
;
347 channels
[i
].attr
.read_timer_interval
=
348 uchan
->attr
.read_timer_interval
;
349 channels
[i
].enabled
= uchan
->enabled
;
350 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
351 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
354 * Map enum lttng_ust_output to enum lttng_event_output.
356 switch (uchan
->attr
.output
) {
358 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
362 * LTTNG_UST_MMAP is the only supported UST
369 chan_exts
[i
].monitor_timer_interval
=
370 uchan
->monitor_timer_interval
;
371 chan_exts
[i
].blocking_timeout
=
372 uchan
->attr
.u
.s
.blocking_timeout
;
374 ret
= get_ust_runtime_stats(session
, uchan
,
375 &discarded_events
, &lost_packets
);
379 chan_exts
[i
].discarded_events
= discarded_events
;
380 chan_exts
[i
].lost_packets
= lost_packets
;
392 return -LTTNG_ERR_FATAL
;
398 static int increment_extended_len(const char *filter_expression
,
399 struct lttng_event_exclusion
*exclusion
,
400 const struct lttng_userspace_probe_location
*probe_location
,
401 size_t *extended_len
)
405 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
407 if (filter_expression
) {
408 *extended_len
+= strlen(filter_expression
) + 1;
412 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
415 if (probe_location
) {
416 ret
= lttng_userspace_probe_location_serialize(probe_location
,
421 *extended_len
+= ret
;
428 static int append_extended_info(const char *filter_expression
,
429 struct lttng_event_exclusion
*exclusion
,
430 struct lttng_userspace_probe_location
*probe_location
,
434 size_t filter_len
= 0;
435 size_t nb_exclusions
= 0;
436 size_t userspace_probe_location_len
= 0;
437 struct lttng_dynamic_buffer location_buffer
;
438 struct lttcomm_event_extended_header extended_header
;
440 if (filter_expression
) {
441 filter_len
= strlen(filter_expression
) + 1;
445 nb_exclusions
= exclusion
->count
;
448 if (probe_location
) {
449 lttng_dynamic_buffer_init(&location_buffer
);
450 ret
= lttng_userspace_probe_location_serialize(probe_location
,
451 &location_buffer
, NULL
);
456 userspace_probe_location_len
= location_buffer
.size
;
459 /* Set header fields */
460 extended_header
.filter_len
= filter_len
;
461 extended_header
.nb_exclusions
= nb_exclusions
;
462 extended_header
.userspace_probe_location_len
= userspace_probe_location_len
;
465 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
466 *extended_at
+= sizeof(extended_header
);
468 /* Copy filter string */
469 if (filter_expression
) {
470 memcpy(*extended_at
, filter_expression
, filter_len
);
471 *extended_at
+= filter_len
;
474 /* Copy exclusion names */
476 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
478 memcpy(*extended_at
, &exclusion
->names
, len
);
482 if (probe_location
) {
483 memcpy(*extended_at
, location_buffer
.data
, location_buffer
.size
);
484 *extended_at
+= location_buffer
.size
;
485 lttng_dynamic_buffer_reset(&location_buffer
);
493 * Create a list of agent domain events.
495 * Return number of events in list on success or else a negative value.
497 static int list_lttng_agent_events(struct agent
*agt
,
498 struct lttng_event
**events
, size_t *total_size
)
501 unsigned int nb_event
= 0;
502 struct agent_event
*event
;
503 struct lttng_event
*tmp_events
= NULL
;
504 struct lttng_ht_iter iter
;
505 size_t extended_len
= 0;
511 DBG3("Listing agent events");
514 nb_event
= lttng_ht_get_count(agt
->events
);
522 /* Compute required extended infos size */
523 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
526 * This is only valid because the commands which add events are
527 * processed in the same thread as the listing.
530 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
531 ret
= increment_extended_len(event
->filter_expression
, NULL
, NULL
,
534 DBG("Error computing the length of extended info message");
535 ret
= -LTTNG_ERR_FATAL
;
541 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
542 tmp_events
= zmalloc(*total_size
);
544 PERROR("zmalloc agent events session");
545 ret
= -LTTNG_ERR_FATAL
;
549 extended_at
= ((uint8_t *) tmp_events
) +
550 nb_event
* sizeof(struct lttng_event
);
553 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
554 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
555 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
556 tmp_events
[i
].enabled
= event
->enabled
;
557 tmp_events
[i
].loglevel
= event
->loglevel_value
;
558 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
561 /* Append extended info */
562 ret
= append_extended_info(event
->filter_expression
, NULL
, NULL
,
565 DBG("Error appending extended info message");
566 ret
= -LTTNG_ERR_FATAL
;
571 *events
= tmp_events
;
573 assert(nb_event
== i
);
584 * Create a list of ust global domain events.
586 static int list_lttng_ust_global_events(char *channel_name
,
587 struct ltt_ust_domain_global
*ust_global
,
588 struct lttng_event
**events
, size_t *total_size
)
591 unsigned int nb_event
= 0;
592 struct lttng_ht_iter iter
;
593 struct lttng_ht_node_str
*node
;
594 struct ltt_ust_channel
*uchan
;
595 struct ltt_ust_event
*uevent
;
596 struct lttng_event
*tmp
;
597 size_t extended_len
= 0;
600 DBG("Listing UST global events for channel %s", channel_name
);
604 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
605 node
= lttng_ht_iter_get_node_str(&iter
);
607 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
611 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
613 nb_event
= lttng_ht_get_count(uchan
->events
);
620 DBG3("Listing UST global %d events", nb_event
);
622 /* Compute required extended infos size */
623 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
624 if (uevent
->internal
) {
629 ret
= increment_extended_len(uevent
->filter_expression
,
630 uevent
->exclusion
, NULL
, &extended_len
);
632 DBG("Error computing the length of extended info message");
633 ret
= -LTTNG_ERR_FATAL
;
638 /* All events are internal, skip. */
644 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
645 tmp
= zmalloc(*total_size
);
647 ret
= -LTTNG_ERR_FATAL
;
651 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
653 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
654 if (uevent
->internal
) {
655 /* This event should remain hidden from clients */
658 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
659 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
660 tmp
[i
].enabled
= uevent
->enabled
;
662 switch (uevent
->attr
.instrumentation
) {
663 case LTTNG_UST_TRACEPOINT
:
664 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
666 case LTTNG_UST_PROBE
:
667 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
669 case LTTNG_UST_FUNCTION
:
670 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
674 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
675 switch (uevent
->attr
.loglevel_type
) {
676 case LTTNG_UST_LOGLEVEL_ALL
:
677 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
679 case LTTNG_UST_LOGLEVEL_RANGE
:
680 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
682 case LTTNG_UST_LOGLEVEL_SINGLE
:
683 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
686 if (uevent
->filter
) {
689 if (uevent
->exclusion
) {
690 tmp
[i
].exclusion
= 1;
694 /* Append extended info */
695 ret
= append_extended_info(uevent
->filter_expression
,
696 uevent
->exclusion
, NULL
, &extended_at
);
698 DBG("Error appending extended info message");
699 ret
= -LTTNG_ERR_FATAL
;
712 * Fill lttng_event array of all kernel events in the channel.
714 static int list_lttng_kernel_events(char *channel_name
,
715 struct ltt_kernel_session
*kernel_session
,
716 struct lttng_event
**events
, size_t *total_size
)
719 unsigned int nb_event
;
720 struct ltt_kernel_event
*event
;
721 struct ltt_kernel_channel
*kchan
;
722 size_t extended_len
= 0;
725 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
727 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
731 nb_event
= kchan
->event_count
;
733 DBG("Listing events for channel %s", kchan
->channel
->name
);
741 /* Compute required extended infos size */
742 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
743 ret
= increment_extended_len(event
->filter_expression
, NULL
,
744 event
->userspace_probe_location
,
747 DBG("Error computing the length of extended info message");
748 ret
= -LTTNG_ERR_FATAL
;
753 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
754 *events
= zmalloc(*total_size
);
755 if (*events
== NULL
) {
756 ret
= -LTTNG_ERR_FATAL
;
760 extended_at
= ((void *) *events
) +
761 nb_event
* sizeof(struct lttng_event
);
763 /* Kernel channels */
764 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
765 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
766 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
767 (*events
)[i
].enabled
= event
->enabled
;
768 (*events
)[i
].filter
=
769 (unsigned char) !!event
->filter_expression
;
771 switch (event
->event
->instrumentation
) {
772 case LTTNG_KERNEL_TRACEPOINT
:
773 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
775 case LTTNG_KERNEL_KRETPROBE
:
776 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
777 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
778 sizeof(struct lttng_kernel_kprobe
));
780 case LTTNG_KERNEL_KPROBE
:
781 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
782 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
783 sizeof(struct lttng_kernel_kprobe
));
785 case LTTNG_KERNEL_UPROBE
:
786 (*events
)[i
].type
= LTTNG_EVENT_USERSPACE_PROBE
;
788 case LTTNG_KERNEL_FUNCTION
:
789 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
790 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
791 sizeof(struct lttng_kernel_function
));
793 case LTTNG_KERNEL_NOOP
:
794 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
796 case LTTNG_KERNEL_SYSCALL
:
797 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
799 case LTTNG_KERNEL_ALL
:
807 /* Append extended info */
808 ret
= append_extended_info(event
->filter_expression
, NULL
,
809 event
->userspace_probe_location
, &extended_at
);
811 DBG("Error appending extended info message");
812 ret
= -LTTNG_ERR_FATAL
;
821 /* Negate the error code to differentiate the size from an error */
826 * Add URI so the consumer output object. Set the correct path depending on the
827 * domain adding the default trace directory.
829 static enum lttng_error_code
add_uri_to_consumer(
830 const struct ltt_session
*session
,
831 struct consumer_output
*consumer
,
832 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
835 enum lttng_error_code ret_code
= LTTNG_OK
;
839 if (consumer
== NULL
) {
840 DBG("No consumer detected. Don't add URI. Stopping.");
841 ret_code
= LTTNG_ERR_NO_CONSUMER
;
846 case LTTNG_DOMAIN_KERNEL
:
847 ret
= lttng_strncpy(consumer
->domain_subdir
,
848 DEFAULT_KERNEL_TRACE_DIR
,
849 sizeof(consumer
->domain_subdir
));
851 case LTTNG_DOMAIN_UST
:
852 ret
= lttng_strncpy(consumer
->domain_subdir
,
853 DEFAULT_UST_TRACE_DIR
,
854 sizeof(consumer
->domain_subdir
));
858 * This case is possible is we try to add the URI to the global
859 * tracing session consumer object which in this case there is
862 memset(consumer
->domain_subdir
, 0,
863 sizeof(consumer
->domain_subdir
));
867 ERR("Failed to initialize consumer output domain subdirectory");
868 ret_code
= LTTNG_ERR_FATAL
;
872 switch (uri
->dtype
) {
875 DBG2("Setting network URI to consumer");
877 if (consumer
->type
== CONSUMER_DST_NET
) {
878 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
879 consumer
->dst
.net
.control_isset
) ||
880 (uri
->stype
== LTTNG_STREAM_DATA
&&
881 consumer
->dst
.net
.data_isset
)) {
882 ret_code
= LTTNG_ERR_URL_EXIST
;
886 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
889 /* Set URI into consumer output object */
890 ret
= consumer_set_network_uri(session
, consumer
, uri
);
894 } else if (ret
== 1) {
896 * URI was the same in the consumer so we do not append the subdir
897 * again so to not duplicate output dir.
904 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
905 ret_code
= LTTNG_ERR_INVALID
;
908 DBG2("Setting trace directory path from URI to %s",
910 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
912 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
914 sizeof(consumer
->dst
.session_root_path
));
915 consumer
->type
= CONSUMER_DST_LOCAL
;
925 * Init tracing by creating trace directory and sending fds kernel consumer.
927 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
930 struct lttng_ht_iter iter
;
931 struct consumer_socket
*socket
;
937 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
938 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
940 pthread_mutex_lock(socket
->lock
);
941 ret
= kernel_consumer_send_session(socket
, session
);
942 pthread_mutex_unlock(socket
->lock
);
944 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
956 * Create a socket to the relayd using the URI.
958 * On success, the relayd_sock pointer is set to the created socket.
959 * Else, it remains untouched and an LTTng error code is returned.
961 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
962 struct lttcomm_relayd_sock
**relayd_sock
,
963 struct consumer_output
*consumer
)
966 enum lttng_error_code status
= LTTNG_OK
;
967 struct lttcomm_relayd_sock
*rsock
;
969 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
970 RELAYD_VERSION_COMM_MINOR
);
972 status
= LTTNG_ERR_FATAL
;
977 * Connect to relayd so we can proceed with a session creation. This call
978 * can possibly block for an arbitrary amount of time to set the health
979 * state to be in poll execution.
982 ret
= relayd_connect(rsock
);
985 ERR("Unable to reach lttng-relayd");
986 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
990 /* Create socket for control stream. */
991 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
992 DBG3("Creating relayd stream socket from URI");
994 /* Check relayd version */
995 ret
= relayd_version_check(rsock
);
996 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
997 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
999 } else if (ret
< 0) {
1000 ERR("Unable to reach lttng-relayd");
1001 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1004 consumer
->relay_major_version
= rsock
->major
;
1005 consumer
->relay_minor_version
= rsock
->minor
;
1006 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1007 DBG3("Creating relayd data socket from URI");
1009 /* Command is not valid */
1010 ERR("Relayd invalid stream type: %d", uri
->stype
);
1011 status
= LTTNG_ERR_INVALID
;
1015 *relayd_sock
= rsock
;
1020 /* The returned value is not useful since we are on an error path. */
1021 (void) relayd_close(rsock
);
1029 * Connect to the relayd using URI and send the socket to the right consumer.
1031 * The consumer socket lock must be held by the caller.
1033 * Returns LTTNG_OK on success or an LTTng error code on failure.
1035 static enum lttng_error_code
send_consumer_relayd_socket(
1036 unsigned int session_id
,
1037 struct lttng_uri
*relayd_uri
,
1038 struct consumer_output
*consumer
,
1039 struct consumer_socket
*consumer_sock
,
1040 const char *session_name
, const char *hostname
,
1041 const char *base_path
, int session_live_timer
,
1042 const uint64_t *current_chunk_id
,
1043 time_t session_creation_time
,
1044 bool session_name_contains_creation_time
)
1047 struct lttcomm_relayd_sock
*rsock
= NULL
;
1048 enum lttng_error_code status
;
1050 /* Connect to relayd and make version check if uri is the control. */
1051 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1052 if (status
!= LTTNG_OK
) {
1053 goto relayd_comm_error
;
1057 /* Set the network sequence index if not set. */
1058 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1059 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1061 * Increment net_seq_idx because we are about to transfer the
1062 * new relayd socket to the consumer.
1063 * Assign unique key so the consumer can match streams.
1065 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1066 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1069 /* Send relayd socket to consumer. */
1070 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1071 relayd_uri
->stype
, session_id
,
1072 session_name
, hostname
, base_path
,
1073 session_live_timer
, current_chunk_id
,
1074 session_creation_time
, session_name_contains_creation_time
);
1076 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1080 /* Flag that the corresponding socket was sent. */
1081 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1082 consumer_sock
->control_sock_sent
= 1;
1083 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1084 consumer_sock
->data_sock_sent
= 1;
1088 * Close socket which was dup on the consumer side. The session daemon does
1089 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1093 if (status
!= LTTNG_OK
) {
1095 * The consumer output for this session should not be used anymore
1096 * since the relayd connection failed thus making any tracing or/and
1097 * streaming not usable.
1099 consumer
->enabled
= 0;
1101 (void) relayd_close(rsock
);
1109 * Send both relayd sockets to a specific consumer and domain. This is a
1110 * helper function to facilitate sending the information to the consumer for a
1113 * The consumer socket lock must be held by the caller.
1115 * Returns LTTNG_OK, or an LTTng error code on failure.
1117 static enum lttng_error_code
send_consumer_relayd_sockets(
1118 enum lttng_domain_type domain
,
1119 unsigned int session_id
, struct consumer_output
*consumer
,
1120 struct consumer_socket
*sock
, const char *session_name
,
1121 const char *hostname
, const char *base_path
, int session_live_timer
,
1122 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1123 bool session_name_contains_creation_time
)
1125 enum lttng_error_code status
= LTTNG_OK
;
1130 /* Sending control relayd socket. */
1131 if (!sock
->control_sock_sent
) {
1132 status
= send_consumer_relayd_socket(session_id
,
1133 &consumer
->dst
.net
.control
, consumer
, sock
,
1134 session_name
, hostname
, base_path
, session_live_timer
,
1135 current_chunk_id
, session_creation_time
,
1136 session_name_contains_creation_time
);
1137 if (status
!= LTTNG_OK
) {
1142 /* Sending data relayd socket. */
1143 if (!sock
->data_sock_sent
) {
1144 status
= send_consumer_relayd_socket(session_id
,
1145 &consumer
->dst
.net
.data
, consumer
, sock
,
1146 session_name
, hostname
, base_path
, session_live_timer
,
1147 current_chunk_id
, session_creation_time
,
1148 session_name_contains_creation_time
);
1149 if (status
!= LTTNG_OK
) {
1159 * Setup relayd connections for a tracing session. First creates the socket to
1160 * the relayd and send them to the right domain consumer. Consumer type MUST be
1163 int cmd_setup_relayd(struct ltt_session
*session
)
1166 struct ltt_ust_session
*usess
;
1167 struct ltt_kernel_session
*ksess
;
1168 struct consumer_socket
*socket
;
1169 struct lttng_ht_iter iter
;
1170 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1174 usess
= session
->ust_session
;
1175 ksess
= session
->kernel_session
;
1177 DBG("Setting relayd for session %s", session
->name
);
1179 if (session
->current_trace_chunk
) {
1180 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1181 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1183 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1184 current_chunk_id
.is_set
= true;
1186 ERR("Failed to get current trace chunk id");
1187 ret
= LTTNG_ERR_UNK
;
1194 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1195 && usess
->consumer
->enabled
) {
1196 /* For each consumer socket, send relayd sockets */
1197 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1198 socket
, node
.node
) {
1199 pthread_mutex_lock(socket
->lock
);
1200 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1201 usess
->consumer
, socket
,
1202 session
->name
, session
->hostname
,
1204 session
->live_timer
,
1205 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1206 session
->creation_time
,
1207 session
->name_contains_creation_time
);
1208 pthread_mutex_unlock(socket
->lock
);
1209 if (ret
!= LTTNG_OK
) {
1212 /* Session is now ready for network streaming. */
1213 session
->net_handle
= 1;
1215 session
->consumer
->relay_major_version
=
1216 usess
->consumer
->relay_major_version
;
1217 session
->consumer
->relay_minor_version
=
1218 usess
->consumer
->relay_minor_version
;
1221 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1222 && ksess
->consumer
->enabled
) {
1223 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1224 socket
, node
.node
) {
1225 pthread_mutex_lock(socket
->lock
);
1226 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1227 ksess
->consumer
, socket
,
1228 session
->name
, session
->hostname
,
1230 session
->live_timer
,
1231 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1232 session
->creation_time
,
1233 session
->name_contains_creation_time
);
1234 pthread_mutex_unlock(socket
->lock
);
1235 if (ret
!= LTTNG_OK
) {
1238 /* Session is now ready for network streaming. */
1239 session
->net_handle
= 1;
1241 session
->consumer
->relay_major_version
=
1242 ksess
->consumer
->relay_major_version
;
1243 session
->consumer
->relay_minor_version
=
1244 ksess
->consumer
->relay_minor_version
;
1253 * Start a kernel session by opening all necessary streams.
1255 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1258 struct ltt_kernel_channel
*kchan
;
1260 /* Open kernel metadata */
1261 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1262 ret
= kernel_open_metadata(ksess
);
1264 ret
= LTTNG_ERR_KERN_META_FAIL
;
1269 /* Open kernel metadata stream */
1270 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1271 ret
= kernel_open_metadata_stream(ksess
);
1273 ERR("Kernel create metadata stream failed");
1274 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1279 /* For each channel */
1280 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1281 if (kchan
->stream_count
== 0) {
1282 ret
= kernel_open_channel_stream(kchan
);
1284 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1287 /* Update the stream global counter */
1288 ksess
->stream_count_global
+= ret
;
1292 /* Setup kernel consumer socket and send fds to it */
1293 ret
= init_kernel_tracing(ksess
);
1295 ret
= LTTNG_ERR_KERN_START_FAIL
;
1299 /* This start the kernel tracing */
1300 ret
= kernel_start_session(ksess
);
1302 ret
= LTTNG_ERR_KERN_START_FAIL
;
1306 /* Quiescent wait after starting trace */
1307 kernel_wait_quiescent(wpipe
);
1318 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1320 int cmd_disable_channel(struct ltt_session
*session
,
1321 enum lttng_domain_type domain
, char *channel_name
)
1324 struct ltt_ust_session
*usess
;
1326 usess
= session
->ust_session
;
1331 case LTTNG_DOMAIN_KERNEL
:
1333 ret
= channel_kernel_disable(session
->kernel_session
,
1335 if (ret
!= LTTNG_OK
) {
1339 kernel_wait_quiescent(kernel_tracer_fd
);
1342 case LTTNG_DOMAIN_UST
:
1344 struct ltt_ust_channel
*uchan
;
1345 struct lttng_ht
*chan_ht
;
1347 chan_ht
= usess
->domain_global
.channels
;
1349 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1350 if (uchan
== NULL
) {
1351 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1355 ret
= channel_ust_disable(usess
, uchan
);
1356 if (ret
!= LTTNG_OK
) {
1362 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1374 * Command LTTNG_TRACK_PID processed by the client thread.
1376 * Called with session lock held.
1378 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1386 case LTTNG_DOMAIN_KERNEL
:
1388 struct ltt_kernel_session
*ksess
;
1390 ksess
= session
->kernel_session
;
1392 ret
= kernel_track_pid(ksess
, pid
);
1393 if (ret
!= LTTNG_OK
) {
1397 kernel_wait_quiescent(kernel_tracer_fd
);
1400 case LTTNG_DOMAIN_UST
:
1402 struct ltt_ust_session
*usess
;
1404 usess
= session
->ust_session
;
1406 ret
= trace_ust_track_pid(usess
, pid
);
1407 if (ret
!= LTTNG_OK
) {
1413 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1425 * Command LTTNG_UNTRACK_PID processed by the client thread.
1427 * Called with session lock held.
1429 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1437 case LTTNG_DOMAIN_KERNEL
:
1439 struct ltt_kernel_session
*ksess
;
1441 ksess
= session
->kernel_session
;
1443 ret
= kernel_untrack_pid(ksess
, pid
);
1444 if (ret
!= LTTNG_OK
) {
1448 kernel_wait_quiescent(kernel_tracer_fd
);
1451 case LTTNG_DOMAIN_UST
:
1453 struct ltt_ust_session
*usess
;
1455 usess
= session
->ust_session
;
1457 ret
= trace_ust_untrack_pid(usess
, pid
);
1458 if (ret
!= LTTNG_OK
) {
1464 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1476 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1478 * The wpipe arguments is used as a notifier for the kernel thread.
1480 int cmd_enable_channel(struct ltt_session
*session
,
1481 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1484 struct ltt_ust_session
*usess
= session
->ust_session
;
1485 struct lttng_ht
*chan_ht
;
1492 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1494 /* Validate channel name */
1495 if (attr
->name
[0] == '.' ||
1496 memchr(attr
->name
, '/', len
) != NULL
) {
1497 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1501 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1506 * Don't try to enable a channel if the session has been started at
1507 * some point in time before. The tracer does not allow it.
1509 if (session
->has_been_started
) {
1510 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1515 * If the session is a live session, remove the switch timer, the
1516 * live timer does the same thing but sends also synchronisation
1517 * beacons for inactive streams.
1519 if (session
->live_timer
> 0) {
1520 attr
->attr
.live_timer_interval
= session
->live_timer
;
1521 attr
->attr
.switch_timer_interval
= 0;
1524 /* Check for feature support */
1525 switch (domain
->type
) {
1526 case LTTNG_DOMAIN_KERNEL
:
1528 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd
) != 1) {
1529 /* Sampling position of buffer is not supported */
1530 WARN("Kernel tracer does not support buffer monitoring. "
1531 "Setting the monitor interval timer to 0 "
1532 "(disabled) for channel '%s' of session '%s'",
1533 attr
-> name
, session
->name
);
1534 lttng_channel_set_monitor_timer_interval(attr
, 0);
1538 case LTTNG_DOMAIN_UST
:
1540 case LTTNG_DOMAIN_JUL
:
1541 case LTTNG_DOMAIN_LOG4J
:
1542 case LTTNG_DOMAIN_PYTHON
:
1543 if (!agent_tracing_is_enabled()) {
1544 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1545 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1550 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1554 switch (domain
->type
) {
1555 case LTTNG_DOMAIN_KERNEL
:
1557 struct ltt_kernel_channel
*kchan
;
1559 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1560 session
->kernel_session
);
1561 if (kchan
== NULL
) {
1562 if (session
->snapshot
.nb_output
> 0 ||
1563 session
->snapshot_mode
) {
1564 /* Enforce mmap output for snapshot sessions. */
1565 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1567 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1568 if (attr
->name
[0] != '\0') {
1569 session
->kernel_session
->has_non_default_channel
= 1;
1572 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1575 if (ret
!= LTTNG_OK
) {
1579 kernel_wait_quiescent(kernel_tracer_fd
);
1582 case LTTNG_DOMAIN_UST
:
1583 case LTTNG_DOMAIN_JUL
:
1584 case LTTNG_DOMAIN_LOG4J
:
1585 case LTTNG_DOMAIN_PYTHON
:
1587 struct ltt_ust_channel
*uchan
;
1592 * Current agent implementation limitations force us to allow
1593 * only one channel at once in "agent" subdomains. Each
1594 * subdomain has a default channel name which must be strictly
1597 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1598 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1599 LTTNG_SYMBOL_NAME_LEN
)) {
1600 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1603 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1604 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1605 LTTNG_SYMBOL_NAME_LEN
)) {
1606 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1609 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1610 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1611 LTTNG_SYMBOL_NAME_LEN
)) {
1612 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1617 chan_ht
= usess
->domain_global
.channels
;
1619 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1620 if (uchan
== NULL
) {
1621 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1622 if (attr
->name
[0] != '\0') {
1623 usess
->has_non_default_channel
= 1;
1626 ret
= channel_ust_enable(usess
, uchan
);
1631 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1635 if (ret
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1636 session
->has_non_mmap_channel
= true;
1645 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1647 int cmd_disable_event(struct ltt_session
*session
,
1648 enum lttng_domain_type domain
, char *channel_name
,
1649 struct lttng_event
*event
)
1654 DBG("Disable event command for event \'%s\'", event
->name
);
1656 event_name
= event
->name
;
1658 /* Error out on unhandled search criteria */
1659 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1660 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1661 ret
= LTTNG_ERR_UNK
;
1668 case LTTNG_DOMAIN_KERNEL
:
1670 struct ltt_kernel_channel
*kchan
;
1671 struct ltt_kernel_session
*ksess
;
1673 ksess
= session
->kernel_session
;
1676 * If a non-default channel has been created in the
1677 * session, explicitely require that -c chan_name needs
1680 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1681 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1685 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1686 if (kchan
== NULL
) {
1687 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1691 switch (event
->type
) {
1692 case LTTNG_EVENT_ALL
:
1693 case LTTNG_EVENT_TRACEPOINT
:
1694 case LTTNG_EVENT_SYSCALL
:
1695 case LTTNG_EVENT_PROBE
:
1696 case LTTNG_EVENT_FUNCTION
:
1697 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1698 if (event_name
[0] == '\0') {
1699 ret
= event_kernel_disable_event(kchan
,
1702 ret
= event_kernel_disable_event(kchan
,
1703 event_name
, event
->type
);
1705 if (ret
!= LTTNG_OK
) {
1710 ret
= LTTNG_ERR_UNK
;
1714 kernel_wait_quiescent(kernel_tracer_fd
);
1717 case LTTNG_DOMAIN_UST
:
1719 struct ltt_ust_channel
*uchan
;
1720 struct ltt_ust_session
*usess
;
1722 usess
= session
->ust_session
;
1724 if (validate_ust_event_name(event_name
)) {
1725 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1730 * If a non-default channel has been created in the
1731 * session, explicitly require that -c chan_name needs
1734 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1735 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1739 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1741 if (uchan
== NULL
) {
1742 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1746 switch (event
->type
) {
1747 case LTTNG_EVENT_ALL
:
1749 * An empty event name means that everything
1750 * should be disabled.
1752 if (event
->name
[0] == '\0') {
1753 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1755 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1758 if (ret
!= LTTNG_OK
) {
1763 ret
= LTTNG_ERR_UNK
;
1767 DBG3("Disable UST event %s in channel %s completed", event_name
,
1771 case LTTNG_DOMAIN_LOG4J
:
1772 case LTTNG_DOMAIN_JUL
:
1773 case LTTNG_DOMAIN_PYTHON
:
1776 struct ltt_ust_session
*usess
= session
->ust_session
;
1780 switch (event
->type
) {
1781 case LTTNG_EVENT_ALL
:
1784 ret
= LTTNG_ERR_UNK
;
1788 agt
= trace_ust_find_agent(usess
, domain
);
1790 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1794 * An empty event name means that everything
1795 * should be disabled.
1797 if (event
->name
[0] == '\0') {
1798 ret
= event_agent_disable_all(usess
, agt
);
1800 ret
= event_agent_disable(usess
, agt
, event_name
);
1802 if (ret
!= LTTNG_OK
) {
1809 ret
= LTTNG_ERR_UND
;
1822 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1824 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1825 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1827 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1828 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1831 * Don't try to add a context if the session has been started at
1832 * some point in time before. The tracer does not allow it and would
1833 * result in a corrupted trace.
1835 if (session
->has_been_started
) {
1836 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1840 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1841 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1842 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1846 case LTTNG_DOMAIN_KERNEL
:
1847 assert(session
->kernel_session
);
1849 if (session
->kernel_session
->channel_count
== 0) {
1850 /* Create default channel */
1851 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1852 if (ret
!= LTTNG_OK
) {
1855 chan_kern_created
= 1;
1857 /* Add kernel context to kernel tracer */
1858 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1859 if (ret
!= LTTNG_OK
) {
1863 case LTTNG_DOMAIN_JUL
:
1864 case LTTNG_DOMAIN_LOG4J
:
1867 * Validate channel name.
1868 * If no channel name is given and the domain is JUL or LOG4J,
1869 * set it to the appropriate domain-specific channel name. If
1870 * a name is provided but does not match the expexted channel
1871 * name, return an error.
1873 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1874 strcmp(channel_name
,
1875 DEFAULT_JUL_CHANNEL_NAME
)) {
1876 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1878 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1879 strcmp(channel_name
,
1880 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1881 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1884 /* break is _not_ missing here. */
1886 case LTTNG_DOMAIN_UST
:
1888 struct ltt_ust_session
*usess
= session
->ust_session
;
1889 unsigned int chan_count
;
1893 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1894 if (chan_count
== 0) {
1895 struct lttng_channel
*attr
;
1896 /* Create default channel */
1897 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1899 ret
= LTTNG_ERR_FATAL
;
1903 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1904 if (ret
!= LTTNG_OK
) {
1908 channel_attr_destroy(attr
);
1909 chan_ust_created
= 1;
1912 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1913 free(app_ctx_provider_name
);
1915 app_ctx_name
= NULL
;
1916 app_ctx_provider_name
= NULL
;
1917 if (ret
!= LTTNG_OK
) {
1923 ret
= LTTNG_ERR_UND
;
1931 if (chan_kern_created
) {
1932 struct ltt_kernel_channel
*kchan
=
1933 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1934 session
->kernel_session
);
1935 /* Created previously, this should NOT fail. */
1937 kernel_destroy_channel(kchan
);
1940 if (chan_ust_created
) {
1941 struct ltt_ust_channel
*uchan
=
1942 trace_ust_find_channel_by_name(
1943 session
->ust_session
->domain_global
.channels
,
1944 DEFAULT_CHANNEL_NAME
);
1945 /* Created previously, this should NOT fail. */
1947 /* Remove from the channel list of the session. */
1948 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1950 trace_ust_destroy_channel(uchan
);
1953 free(app_ctx_provider_name
);
1958 static inline bool name_starts_with(const char *name
, const char *prefix
)
1960 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1962 return !strncmp(name
, prefix
, max_cmp_len
);
1965 /* Perform userspace-specific event name validation */
1966 static int validate_ust_event_name(const char *name
)
1976 * Check name against all internal UST event component namespaces used
1979 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1980 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1981 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1990 * Internal version of cmd_enable_event() with a supplemental
1991 * "internal_event" flag which is used to enable internal events which should
1992 * be hidden from clients. Such events are used in the agent implementation to
1993 * enable the events through which all "agent" events are funeled.
1995 static int _cmd_enable_event(struct ltt_session
*session
,
1996 struct lttng_domain
*domain
,
1997 char *channel_name
, struct lttng_event
*event
,
1998 char *filter_expression
,
1999 struct lttng_filter_bytecode
*filter
,
2000 struct lttng_event_exclusion
*exclusion
,
2001 int wpipe
, bool internal_event
)
2003 int ret
= 0, channel_created
= 0;
2004 struct lttng_channel
*attr
= NULL
;
2008 assert(channel_name
);
2010 /* If we have a filter, we must have its filter expression */
2011 assert(!(!!filter_expression
^ !!filter
));
2013 /* Normalize event name as a globbing pattern */
2014 strutils_normalize_star_glob_pattern(event
->name
);
2016 /* Normalize exclusion names as globbing patterns */
2020 for (i
= 0; i
< exclusion
->count
; i
++) {
2021 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2023 strutils_normalize_star_glob_pattern(name
);
2027 DBG("Enable event command for event \'%s\'", event
->name
);
2031 switch (domain
->type
) {
2032 case LTTNG_DOMAIN_KERNEL
:
2034 struct ltt_kernel_channel
*kchan
;
2037 * If a non-default channel has been created in the
2038 * session, explicitely require that -c chan_name needs
2041 if (session
->kernel_session
->has_non_default_channel
2042 && channel_name
[0] == '\0') {
2043 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2047 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2048 session
->kernel_session
);
2049 if (kchan
== NULL
) {
2050 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2051 LTTNG_BUFFER_GLOBAL
);
2053 ret
= LTTNG_ERR_FATAL
;
2056 if (lttng_strncpy(attr
->name
, channel_name
,
2057 sizeof(attr
->name
))) {
2058 ret
= LTTNG_ERR_INVALID
;
2062 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2063 if (ret
!= LTTNG_OK
) {
2066 channel_created
= 1;
2069 /* Get the newly created kernel channel pointer */
2070 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2071 session
->kernel_session
);
2072 if (kchan
== NULL
) {
2073 /* This sould not happen... */
2074 ret
= LTTNG_ERR_FATAL
;
2078 switch (event
->type
) {
2079 case LTTNG_EVENT_ALL
:
2081 char *filter_expression_a
= NULL
;
2082 struct lttng_filter_bytecode
*filter_a
= NULL
;
2085 * We need to duplicate filter_expression and filter,
2086 * because ownership is passed to first enable
2089 if (filter_expression
) {
2090 filter_expression_a
= strdup(filter_expression
);
2091 if (!filter_expression_a
) {
2092 ret
= LTTNG_ERR_FATAL
;
2097 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2099 free(filter_expression_a
);
2100 ret
= LTTNG_ERR_FATAL
;
2103 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2105 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2106 ret
= event_kernel_enable_event(kchan
, event
,
2107 filter_expression
, filter
);
2108 /* We have passed ownership */
2109 filter_expression
= NULL
;
2111 if (ret
!= LTTNG_OK
) {
2112 if (channel_created
) {
2113 /* Let's not leak a useless channel. */
2114 kernel_destroy_channel(kchan
);
2116 free(filter_expression_a
);
2120 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2121 ret
= event_kernel_enable_event(kchan
, event
,
2122 filter_expression_a
, filter_a
);
2123 /* We have passed ownership */
2124 filter_expression_a
= NULL
;
2126 if (ret
!= LTTNG_OK
) {
2131 case LTTNG_EVENT_PROBE
:
2132 case LTTNG_EVENT_USERSPACE_PROBE
:
2133 case LTTNG_EVENT_FUNCTION
:
2134 case LTTNG_EVENT_FUNCTION_ENTRY
:
2135 case LTTNG_EVENT_TRACEPOINT
:
2136 ret
= event_kernel_enable_event(kchan
, event
,
2137 filter_expression
, filter
);
2138 /* We have passed ownership */
2139 filter_expression
= NULL
;
2141 if (ret
!= LTTNG_OK
) {
2142 if (channel_created
) {
2143 /* Let's not leak a useless channel. */
2144 kernel_destroy_channel(kchan
);
2149 case LTTNG_EVENT_SYSCALL
:
2150 ret
= event_kernel_enable_event(kchan
, event
,
2151 filter_expression
, filter
);
2152 /* We have passed ownership */
2153 filter_expression
= NULL
;
2155 if (ret
!= LTTNG_OK
) {
2160 ret
= LTTNG_ERR_UNK
;
2164 kernel_wait_quiescent(kernel_tracer_fd
);
2167 case LTTNG_DOMAIN_UST
:
2169 struct ltt_ust_channel
*uchan
;
2170 struct ltt_ust_session
*usess
= session
->ust_session
;
2175 * If a non-default channel has been created in the
2176 * session, explicitely require that -c chan_name needs
2179 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2180 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2184 /* Get channel from global UST domain */
2185 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2187 if (uchan
== NULL
) {
2188 /* Create default channel */
2189 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2190 usess
->buffer_type
);
2192 ret
= LTTNG_ERR_FATAL
;
2195 if (lttng_strncpy(attr
->name
, channel_name
,
2196 sizeof(attr
->name
))) {
2197 ret
= LTTNG_ERR_INVALID
;
2201 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2202 if (ret
!= LTTNG_OK
) {
2206 /* Get the newly created channel reference back */
2207 uchan
= trace_ust_find_channel_by_name(
2208 usess
->domain_global
.channels
, channel_name
);
2212 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2214 * Don't allow users to add UST events to channels which
2215 * are assigned to a userspace subdomain (JUL, Log4J,
2218 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2222 if (!internal_event
) {
2224 * Ensure the event name is not reserved for internal
2227 ret
= validate_ust_event_name(event
->name
);
2229 WARN("Userspace event name %s failed validation.",
2231 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2236 /* At this point, the session and channel exist on the tracer */
2237 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2238 filter_expression
, filter
, exclusion
,
2240 /* We have passed ownership */
2241 filter_expression
= NULL
;
2244 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2245 goto already_enabled
;
2246 } else if (ret
!= LTTNG_OK
) {
2251 case LTTNG_DOMAIN_LOG4J
:
2252 case LTTNG_DOMAIN_JUL
:
2253 case LTTNG_DOMAIN_PYTHON
:
2255 const char *default_event_name
, *default_chan_name
;
2257 struct lttng_event uevent
;
2258 struct lttng_domain tmp_dom
;
2259 struct ltt_ust_session
*usess
= session
->ust_session
;
2263 if (!agent_tracing_is_enabled()) {
2264 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2265 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2269 agt
= trace_ust_find_agent(usess
, domain
->type
);
2271 agt
= agent_create(domain
->type
);
2273 ret
= LTTNG_ERR_NOMEM
;
2276 agent_add(agt
, usess
->agents
);
2279 /* Create the default tracepoint. */
2280 memset(&uevent
, 0, sizeof(uevent
));
2281 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2282 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2283 default_event_name
= event_get_default_agent_ust_name(
2285 if (!default_event_name
) {
2286 ret
= LTTNG_ERR_FATAL
;
2289 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2290 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2293 * The domain type is changed because we are about to enable the
2294 * default channel and event for the JUL domain that are hardcoded.
2295 * This happens in the UST domain.
2297 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2298 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2300 switch (domain
->type
) {
2301 case LTTNG_DOMAIN_LOG4J
:
2302 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2304 case LTTNG_DOMAIN_JUL
:
2305 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2307 case LTTNG_DOMAIN_PYTHON
:
2308 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2311 /* The switch/case we are in makes this impossible */
2316 char *filter_expression_copy
= NULL
;
2317 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2320 const size_t filter_size
= sizeof(
2321 struct lttng_filter_bytecode
)
2324 filter_copy
= zmalloc(filter_size
);
2326 ret
= LTTNG_ERR_NOMEM
;
2329 memcpy(filter_copy
, filter
, filter_size
);
2331 filter_expression_copy
=
2332 strdup(filter_expression
);
2333 if (!filter_expression
) {
2334 ret
= LTTNG_ERR_NOMEM
;
2337 if (!filter_expression_copy
|| !filter_copy
) {
2338 free(filter_expression_copy
);
2344 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2345 (char *) default_chan_name
,
2346 &uevent
, filter_expression_copy
,
2347 filter_copy
, NULL
, wpipe
);
2350 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2351 goto already_enabled
;
2352 } else if (ret
!= LTTNG_OK
) {
2356 /* The wild card * means that everything should be enabled. */
2357 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2358 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2361 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2365 filter_expression
= NULL
;
2366 if (ret
!= LTTNG_OK
) {
2373 ret
= LTTNG_ERR_UND
;
2381 free(filter_expression
);
2384 channel_attr_destroy(attr
);
2390 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2391 * We own filter, exclusion, and filter_expression.
2393 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2394 char *channel_name
, struct lttng_event
*event
,
2395 char *filter_expression
,
2396 struct lttng_filter_bytecode
*filter
,
2397 struct lttng_event_exclusion
*exclusion
,
2400 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2401 filter_expression
, filter
, exclusion
, wpipe
, false);
2405 * Enable an event which is internal to LTTng. An internal should
2406 * never be made visible to clients and are immune to checks such as
2409 static int cmd_enable_event_internal(struct ltt_session
*session
,
2410 struct lttng_domain
*domain
,
2411 char *channel_name
, struct lttng_event
*event
,
2412 char *filter_expression
,
2413 struct lttng_filter_bytecode
*filter
,
2414 struct lttng_event_exclusion
*exclusion
,
2417 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2418 filter_expression
, filter
, exclusion
, wpipe
, true);
2422 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2424 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2425 struct lttng_event
**events
)
2428 ssize_t nb_events
= 0;
2431 case LTTNG_DOMAIN_KERNEL
:
2432 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2433 if (nb_events
< 0) {
2434 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2438 case LTTNG_DOMAIN_UST
:
2439 nb_events
= ust_app_list_events(events
);
2440 if (nb_events
< 0) {
2441 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2445 case LTTNG_DOMAIN_LOG4J
:
2446 case LTTNG_DOMAIN_JUL
:
2447 case LTTNG_DOMAIN_PYTHON
:
2448 nb_events
= agent_list_events(events
, domain
);
2449 if (nb_events
< 0) {
2450 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2455 ret
= LTTNG_ERR_UND
;
2462 /* Return negative value to differentiate return code */
2467 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2469 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2470 struct lttng_event_field
**fields
)
2473 ssize_t nb_fields
= 0;
2476 case LTTNG_DOMAIN_UST
:
2477 nb_fields
= ust_app_list_event_fields(fields
);
2478 if (nb_fields
< 0) {
2479 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2483 case LTTNG_DOMAIN_KERNEL
:
2484 default: /* fall-through */
2485 ret
= LTTNG_ERR_UND
;
2492 /* Return negative value to differentiate return code */
2496 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2498 return syscall_table_list(events
);
2502 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2504 * Called with session lock held.
2506 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2507 enum lttng_domain_type domain
, int32_t **pids
)
2510 ssize_t nr_pids
= 0;
2513 case LTTNG_DOMAIN_KERNEL
:
2515 struct ltt_kernel_session
*ksess
;
2517 ksess
= session
->kernel_session
;
2518 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2520 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2525 case LTTNG_DOMAIN_UST
:
2527 struct ltt_ust_session
*usess
;
2529 usess
= session
->ust_session
;
2530 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2532 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2537 case LTTNG_DOMAIN_LOG4J
:
2538 case LTTNG_DOMAIN_JUL
:
2539 case LTTNG_DOMAIN_PYTHON
:
2541 ret
= LTTNG_ERR_UND
;
2548 /* Return negative value to differentiate return code */
2553 * Command LTTNG_START_TRACE processed by the client thread.
2555 * Called with session mutex held.
2557 int cmd_start_trace(struct ltt_session
*session
)
2559 enum lttng_error_code ret
;
2560 unsigned long nb_chan
= 0;
2561 struct ltt_kernel_session
*ksession
;
2562 struct ltt_ust_session
*usess
;
2566 /* Ease our life a bit ;) */
2567 ksession
= session
->kernel_session
;
2568 usess
= session
->ust_session
;
2570 /* Is the session already started? */
2571 if (session
->active
) {
2572 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2577 * Starting a session without channel is useless since after that it's not
2578 * possible to enable channel thus inform the client.
2580 if (usess
&& usess
->domain_global
.channels
) {
2581 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2584 nb_chan
+= ksession
->channel_count
;
2587 ret
= LTTNG_ERR_NO_CHANNEL
;
2591 if (session
->output_traces
&& !session
->current_trace_chunk
&&
2592 session_output_supports_trace_chunks(session
)) {
2593 struct lttng_trace_chunk
*trace_chunk
;
2595 trace_chunk
= session_create_new_trace_chunk(
2596 session
, NULL
, NULL
, NULL
);
2598 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2601 assert(!session
->current_trace_chunk
);
2602 ret
= session_set_trace_chunk(session
, trace_chunk
, NULL
);
2603 lttng_trace_chunk_put(trace_chunk
);
2605 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2610 /* Kernel tracing */
2611 if (ksession
!= NULL
) {
2612 DBG("Start kernel tracing session %s", session
->name
);
2613 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2614 if (ret
!= LTTNG_OK
) {
2619 /* Flag session that trace should start automatically */
2621 int int_ret
= ust_app_start_trace_all(usess
);
2624 ret
= LTTNG_ERR_UST_START_FAIL
;
2629 /* Flag this after a successful start. */
2630 session
->has_been_started
= 1;
2631 session
->active
= 1;
2634 * Clear the flag that indicates that a rotation was done while the
2635 * session was stopped.
2637 session
->rotated_after_last_stop
= false;
2639 if (session
->rotate_timer_period
) {
2640 int int_ret
= timer_session_rotation_schedule_timer_start(
2641 session
, session
->rotate_timer_period
);
2644 ERR("Failed to enable rotate timer");
2645 ret
= LTTNG_ERR_UNK
;
2657 * Command LTTNG_STOP_TRACE processed by the client thread.
2659 int cmd_stop_trace(struct ltt_session
*session
)
2662 struct ltt_kernel_channel
*kchan
;
2663 struct ltt_kernel_session
*ksession
;
2664 struct ltt_ust_session
*usess
;
2665 bool error_occurred
= false;
2669 DBG("Begin stop session %s (id %" PRIu64
")", session
->name
, session
->id
);
2671 ksession
= session
->kernel_session
;
2672 usess
= session
->ust_session
;
2674 /* Session is not active. Skip everythong and inform the client. */
2675 if (!session
->active
) {
2676 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2681 if (ksession
&& ksession
->active
) {
2682 DBG("Stop kernel tracing");
2684 ret
= kernel_stop_session(ksession
);
2686 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2690 kernel_wait_quiescent(kernel_tracer_fd
);
2692 /* Flush metadata after stopping (if exists) */
2693 if (ksession
->metadata_stream_fd
>= 0) {
2694 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2696 ERR("Kernel metadata flush failed");
2700 /* Flush all buffers after stopping */
2701 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2702 ret
= kernel_flush_buffer(kchan
);
2704 ERR("Kernel flush buffer error");
2708 ksession
->active
= 0;
2709 DBG("Kernel session stopped %s (id %" PRIu64
")", session
->name
,
2713 if (usess
&& usess
->active
) {
2714 ret
= ust_app_stop_trace_all(usess
);
2716 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2721 /* Flag inactive after a successful stop. */
2722 session
->active
= 0;
2723 ret
= !error_occurred
? LTTNG_OK
: LTTNG_ERR_UNK
;
2730 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2732 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2733 struct lttng_uri
*uris
)
2736 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2737 struct ltt_ust_session
*usess
= session
->ust_session
;
2743 /* Can't set consumer URI if the session is active. */
2744 if (session
->active
) {
2745 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2749 /* Set the "global" consumer URIs */
2750 for (i
= 0; i
< nb_uri
; i
++) {
2751 ret
= add_uri_to_consumer(session
,
2753 &uris
[i
], LTTNG_DOMAIN_NONE
);
2754 if (ret
!= LTTNG_OK
) {
2759 /* Set UST session URIs */
2760 if (session
->ust_session
) {
2761 for (i
= 0; i
< nb_uri
; i
++) {
2762 ret
= add_uri_to_consumer(session
,
2763 session
->ust_session
->consumer
,
2764 &uris
[i
], LTTNG_DOMAIN_UST
);
2765 if (ret
!= LTTNG_OK
) {
2771 /* Set kernel session URIs */
2772 if (session
->kernel_session
) {
2773 for (i
= 0; i
< nb_uri
; i
++) {
2774 ret
= add_uri_to_consumer(session
,
2775 session
->kernel_session
->consumer
,
2776 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2777 if (ret
!= LTTNG_OK
) {
2784 * Make sure to set the session in output mode after we set URI since a
2785 * session can be created without URL (thus flagged in no output mode).
2787 session
->output_traces
= 1;
2789 ksess
->output_traces
= 1;
2793 usess
->output_traces
= 1;
2804 enum lttng_error_code
set_session_output_from_descriptor(
2805 struct ltt_session
*session
,
2806 const struct lttng_session_descriptor
*descriptor
)
2809 enum lttng_error_code ret_code
= LTTNG_OK
;
2810 enum lttng_session_descriptor_type session_type
=
2811 lttng_session_descriptor_get_type(descriptor
);
2812 enum lttng_session_descriptor_output_type output_type
=
2813 lttng_session_descriptor_get_output_type(descriptor
);
2814 struct lttng_uri uris
[2] = {};
2815 size_t uri_count
= 0;
2817 switch (output_type
) {
2818 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2820 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2821 lttng_session_descriptor_get_local_output_uri(descriptor
,
2825 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2826 lttng_session_descriptor_get_network_output_uris(descriptor
,
2827 &uris
[0], &uris
[1]);
2831 ret_code
= LTTNG_ERR_INVALID
;
2835 switch (session_type
) {
2836 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2838 struct snapshot_output
*new_output
= NULL
;
2840 new_output
= snapshot_output_alloc();
2842 ret_code
= LTTNG_ERR_NOMEM
;
2846 ret
= snapshot_output_init_with_uri(session
,
2847 DEFAULT_SNAPSHOT_MAX_SIZE
,
2848 NULL
, uris
, uri_count
, session
->consumer
,
2849 new_output
, &session
->snapshot
);
2851 ret_code
= (ret
== -ENOMEM
) ?
2852 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2853 snapshot_output_destroy(new_output
);
2856 snapshot_add_output(&session
->snapshot
, new_output
);
2859 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2860 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2862 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2866 ret_code
= LTTNG_ERR_INVALID
;
2874 enum lttng_error_code
cmd_create_session_from_descriptor(
2875 struct lttng_session_descriptor
*descriptor
,
2876 const lttng_sock_cred
*creds
,
2877 const char *home_path
)
2880 enum lttng_error_code ret_code
;
2881 const char *session_name
;
2882 struct ltt_session
*new_session
= NULL
;
2883 enum lttng_session_descriptor_status descriptor_status
;
2884 const char *base_path
;
2886 session_lock_list();
2888 if (*home_path
!= '/') {
2889 ERR("Home path provided by client is not absolute");
2890 ret_code
= LTTNG_ERR_INVALID
;
2895 descriptor_status
= lttng_session_descriptor_get_session_name(
2896 descriptor
, &session_name
);
2897 switch (descriptor_status
) {
2898 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
2900 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
2901 session_name
= NULL
;
2904 ret_code
= LTTNG_ERR_INVALID
;
2907 ret
= lttng_session_descriptor_get_base_path(descriptor
, &base_path
);
2909 ret_code
= LTTNG_ERR_INVALID
;
2912 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
2913 base_path
, &new_session
);
2914 if (ret_code
!= LTTNG_OK
) {
2918 if (!session_name
) {
2919 ret
= lttng_session_descriptor_set_session_name(descriptor
,
2922 ret_code
= LTTNG_ERR_SESSION_FAIL
;
2927 if (!lttng_session_descriptor_is_output_destination_initialized(
2930 * Only include the session's creation time in the output
2931 * destination if the name of the session itself was
2932 * not auto-generated.
2934 ret_code
= lttng_session_descriptor_set_default_output(
2936 session_name
? &new_session
->creation_time
: NULL
,
2938 if (ret_code
!= LTTNG_OK
) {
2942 new_session
->has_user_specified_directory
=
2943 lttng_session_descriptor_has_output_directory(
2947 switch (lttng_session_descriptor_get_type(descriptor
)) {
2948 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2949 new_session
->snapshot_mode
= 1;
2951 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2952 new_session
->live_timer
=
2953 lttng_session_descriptor_live_get_timer_interval(
2960 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
2961 if (ret_code
!= LTTNG_OK
) {
2964 new_session
->consumer
->enabled
= 1;
2965 ret_code
= LTTNG_OK
;
2967 /* Release reference provided by the session_create function. */
2968 session_put(new_session
);
2969 if (ret_code
!= LTTNG_OK
&& new_session
) {
2970 /* Release the global reference on error. */
2971 session_destroy(new_session
);
2973 session_unlock_list();
2977 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
2978 struct lttng_session_descriptor
**return_descriptor
)
2981 size_t payload_size
;
2982 struct lttng_dynamic_buffer payload
;
2983 struct lttng_buffer_view home_dir_view
;
2984 struct lttng_buffer_view session_descriptor_view
;
2985 struct lttng_session_descriptor
*session_descriptor
= NULL
;
2986 enum lttng_error_code ret_code
;
2988 lttng_dynamic_buffer_init(&payload
);
2989 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
2991 ret_code
= LTTNG_ERR_INVALID
;
2994 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
2995 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
2996 ret_code
= LTTNG_ERR_INVALID
;
3000 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
3001 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
3002 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3004 ret_code
= LTTNG_ERR_NOMEM
;
3008 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3010 ERR("Reception of session descriptor failed, aborting.");
3011 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3015 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3018 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3019 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3021 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3022 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3024 ret
= lttng_session_descriptor_create_from_buffer(
3025 &session_descriptor_view
, &session_descriptor
);
3027 ERR("Failed to create session descriptor from payload of \"create session\" command");
3028 ret_code
= LTTNG_ERR_INVALID
;
3033 * Sets the descriptor's auto-generated properties (name, output) if
3036 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3038 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3039 if (ret_code
!= LTTNG_OK
) {
3043 ret_code
= LTTNG_OK
;
3044 *return_descriptor
= session_descriptor
;
3045 session_descriptor
= NULL
;
3047 lttng_dynamic_buffer_reset(&payload
);
3048 lttng_session_descriptor_destroy(session_descriptor
);
3053 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3054 void *_reply_context
)
3058 const struct cmd_destroy_session_reply_context
*reply_context
=
3060 struct lttng_dynamic_buffer payload
;
3061 struct lttcomm_session_destroy_command_header cmd_header
;
3062 struct lttng_trace_archive_location
*location
= NULL
;
3063 struct lttcomm_lttng_msg llm
= {
3064 .cmd_type
= LTTNG_DESTROY_SESSION
,
3065 .ret_code
= LTTNG_OK
,
3068 sizeof(struct lttcomm_session_destroy_command_header
),
3071 size_t payload_size_before_location
;
3073 lttng_dynamic_buffer_init(&payload
);
3075 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3077 ERR("Failed to append session destruction message");
3081 cmd_header
.rotation_state
=
3082 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3083 session
->rotation_state
:
3084 LTTNG_ROTATION_STATE_NO_ROTATION
);
3085 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3086 sizeof(cmd_header
));
3088 ERR("Failed to append session destruction command header");
3092 if (!reply_context
->implicit_rotation_on_destroy
) {
3093 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3097 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3098 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3103 location
= session_get_trace_archive_location(session
);
3105 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3110 payload_size_before_location
= payload
.size
;
3111 comm_ret
= lttng_trace_archive_location_serialize(location
,
3114 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3118 /* Update the message to indicate the location's length. */
3119 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3120 payload
.size
- payload_size_before_location
;
3122 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3123 payload
.data
, payload
.size
);
3124 if (comm_ret
!= (ssize_t
) payload
.size
) {
3125 ERR("Failed to send result of the destruction of session \"%s\" to client",
3129 ret
= close(reply_context
->reply_sock_fd
);
3131 PERROR("Failed to close client socket in deferred session destroy reply");
3133 lttng_dynamic_buffer_reset(&payload
);
3134 free(_reply_context
);
3138 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3140 * Called with session lock held.
3142 int cmd_destroy_session(struct ltt_session
*session
,
3143 struct notification_thread_handle
*notification_thread_handle
,
3147 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3150 reply_context
= zmalloc(sizeof(*reply_context
));
3151 if (!reply_context
) {
3152 ret
= LTTNG_ERR_NOMEM
;
3155 reply_context
->reply_sock_fd
= *sock_fd
;
3161 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3163 if (session
->active
) {
3164 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3166 ret
= cmd_stop_trace(session
);
3167 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3168 /* Carry on with the destruction of the session. */
3169 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3170 session
->name
, lttng_strerror(-ret
));
3174 if (session
->rotation_schedule_timer_enabled
) {
3175 if (timer_session_rotation_schedule_timer_stop(
3177 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3182 if (session
->rotate_size
) {
3183 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3184 session
->rotate_size
= 0;
3187 if (session
->most_recent_chunk_id
.is_set
&&
3188 session
->most_recent_chunk_id
.value
!= 0 &&
3189 session
->current_trace_chunk
&& session
->output_traces
) {
3191 * Perform a last rotation on destruction if rotations have
3192 * occurred during the session's lifetime.
3194 ret
= cmd_rotate_session(session
, NULL
, false);
3195 if (ret
!= LTTNG_OK
) {
3196 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3197 session
->name
, lttng_strerror(-ret
));
3199 if (reply_context
) {
3200 reply_context
->implicit_rotation_on_destroy
= true;
3202 } else if (session
->has_been_started
&& session
->current_trace_chunk
&&
3203 session_output_supports_trace_chunks(session
)) {
3205 * The user has not triggered a session rotation. However, to
3206 * ensure all data has been consumed, the session is rotated
3207 * to a 'null' trace chunk before it is destroyed.
3209 * This is a "quiet" rotation meaning that no notification is
3210 * emitted and no renaming of the current trace chunk takes
3213 ret
= cmd_rotate_session(session
, NULL
, true);
3214 if (ret
!= LTTNG_OK
) {
3215 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3216 session
->name
, lttng_strerror(-ret
));
3220 if (session
->shm_path
[0]) {
3222 * When a session is created with an explicit shm_path,
3223 * the consumer daemon will create its shared memory files
3224 * at that location and will *not* unlink them. This is normal
3225 * as the intention of that feature is to make it possible
3226 * to retrieve the content of those files should a crash occur.
3228 * To ensure the content of those files can be used, the
3229 * sessiond daemon will replicate the content of the metadata
3230 * cache in a metadata file.
3232 * On clean-up, it is expected that the consumer daemon will
3233 * unlink the shared memory files and that the session daemon
3234 * will unlink the metadata file. Then, the session's directory
3235 * in the shm path can be removed.
3237 * Unfortunately, a flaw in the design of the sessiond's and
3238 * consumerd's tear down of channels makes it impossible to
3239 * determine when the sessiond _and_ the consumerd have both
3240 * destroyed their representation of a channel. For one, the
3241 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3242 * callbacks in both daemons.
3244 * However, it is also impossible for the sessiond to know when
3245 * the consumer daemon is done destroying its channel(s) since
3246 * it occurs as a reaction to the closing of the channel's file
3247 * descriptor. There is no resulting communication initiated
3248 * from the consumerd to the sessiond to confirm that the
3249 * operation is completed (and was successful).
3251 * Until this is all fixed, the session daemon checks for the
3252 * removal of the session's shm path which makes it possible
3253 * to safely advertise a session as having been destroyed.
3255 * Prior to this fix, it was not possible to reliably save
3256 * a session making use of the --shm-path option, destroy it,
3257 * and load it again. This is because the creation of the
3258 * session would fail upon seeing the session's shm path
3259 * already in existence.
3261 * Note that none of the error paths in the check for the
3262 * directory's existence return an error. This is normal
3263 * as there isn't much that can be done. The session will
3264 * be destroyed properly, except that we can't offer the
3265 * guarantee that the same session can be re-created.
3267 current_completion_handler
= &destroy_completion_handler
.handler
;
3268 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3270 sizeof(destroy_completion_handler
.shm_path
));
3275 * The session is destroyed. However, note that the command context
3276 * still holds a reference to the session, thus delaying its destruction
3277 * _at least_ up to the point when that reference is released.
3279 session_destroy(session
);
3280 if (reply_context
) {
3281 ret
= session_add_destroy_notifier(session
,
3282 cmd_destroy_session_reply
,
3283 (void *) reply_context
);
3285 ret
= LTTNG_ERR_FATAL
;
3297 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3299 int cmd_register_consumer(struct ltt_session
*session
,
3300 enum lttng_domain_type domain
, const char *sock_path
,
3301 struct consumer_data
*cdata
)
3304 struct consumer_socket
*socket
= NULL
;
3311 case LTTNG_DOMAIN_KERNEL
:
3313 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3317 /* Can't register a consumer if there is already one */
3318 if (ksess
->consumer_fds_sent
!= 0) {
3319 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3323 sock
= lttcomm_connect_unix_sock(sock_path
);
3325 ret
= LTTNG_ERR_CONNECT_FAIL
;
3328 cdata
->cmd_sock
= sock
;
3330 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3331 if (socket
== NULL
) {
3334 PERROR("close register consumer");
3336 cdata
->cmd_sock
= -1;
3337 ret
= LTTNG_ERR_FATAL
;
3341 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3342 if (socket
->lock
== NULL
) {
3343 PERROR("zmalloc pthread mutex");
3344 ret
= LTTNG_ERR_FATAL
;
3347 pthread_mutex_init(socket
->lock
, NULL
);
3348 socket
->registered
= 1;
3351 consumer_add_socket(socket
, ksess
->consumer
);
3354 pthread_mutex_lock(&cdata
->pid_mutex
);
3356 pthread_mutex_unlock(&cdata
->pid_mutex
);
3361 /* TODO: Userspace tracing */
3362 ret
= LTTNG_ERR_UND
;
3370 consumer_destroy_socket(socket
);
3376 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3378 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3379 struct lttng_domain
**domains
)
3384 struct lttng_ht_iter iter
;
3386 if (session
->kernel_session
!= NULL
) {
3387 DBG3("Listing domains found kernel domain");
3391 if (session
->ust_session
!= NULL
) {
3392 DBG3("Listing domains found UST global domain");
3396 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3398 if (agt
->being_used
) {
3409 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3410 if (*domains
== NULL
) {
3411 ret
= LTTNG_ERR_FATAL
;
3415 if (session
->kernel_session
!= NULL
) {
3416 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3418 /* Kernel session buffer type is always GLOBAL */
3419 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3424 if (session
->ust_session
!= NULL
) {
3425 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3426 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3430 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3432 if (agt
->being_used
) {
3433 (*domains
)[index
].type
= agt
->domain
;
3434 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3444 /* Return negative value to differentiate return code */
3450 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3452 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3453 struct ltt_session
*session
, struct lttng_channel
**channels
)
3455 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3458 case LTTNG_DOMAIN_KERNEL
:
3459 if (session
->kernel_session
!= NULL
) {
3460 nb_chan
= session
->kernel_session
->channel_count
;
3462 DBG3("Number of kernel channels %zd", nb_chan
);
3464 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3468 case LTTNG_DOMAIN_UST
:
3469 if (session
->ust_session
!= NULL
) {
3471 nb_chan
= lttng_ht_get_count(
3472 session
->ust_session
->domain_global
.channels
);
3475 DBG3("Number of UST global channels %zd", nb_chan
);
3477 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3482 ret
= -LTTNG_ERR_UND
;
3487 const size_t channel_size
= sizeof(struct lttng_channel
) +
3488 sizeof(struct lttng_channel_extended
);
3489 struct lttng_channel_extended
*channel_exts
;
3491 payload_size
= nb_chan
* channel_size
;
3492 *channels
= zmalloc(payload_size
);
3493 if (*channels
== NULL
) {
3494 ret
= -LTTNG_ERR_FATAL
;
3498 channel_exts
= ((void *) *channels
) +
3499 (nb_chan
* sizeof(struct lttng_channel
));
3500 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3501 if (ret
!= LTTNG_OK
) {
3516 * Command LTTNG_LIST_EVENTS processed by the client thread.
3518 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3519 struct ltt_session
*session
, char *channel_name
,
3520 struct lttng_event
**events
, size_t *total_size
)
3523 ssize_t nb_event
= 0;
3526 case LTTNG_DOMAIN_KERNEL
:
3527 if (session
->kernel_session
!= NULL
) {
3528 nb_event
= list_lttng_kernel_events(channel_name
,
3529 session
->kernel_session
, events
,
3533 case LTTNG_DOMAIN_UST
:
3535 if (session
->ust_session
!= NULL
) {
3536 nb_event
= list_lttng_ust_global_events(channel_name
,
3537 &session
->ust_session
->domain_global
, events
,
3542 case LTTNG_DOMAIN_LOG4J
:
3543 case LTTNG_DOMAIN_JUL
:
3544 case LTTNG_DOMAIN_PYTHON
:
3545 if (session
->ust_session
) {
3546 struct lttng_ht_iter iter
;
3550 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3551 &iter
.iter
, agt
, node
.node
) {
3552 if (agt
->domain
== domain
) {
3553 nb_event
= list_lttng_agent_events(
3563 ret
= LTTNG_ERR_UND
;
3570 /* Return negative value to differentiate return code */
3575 * Using the session list, filled a lttng_session array to send back to the
3576 * client for session listing.
3578 * The session list lock MUST be acquired before calling this function. Use
3579 * session_lock_list() and session_unlock_list().
3581 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3582 size_t session_count
, uid_t uid
, gid_t gid
)
3586 struct ltt_session
*session
;
3587 struct ltt_session_list
*list
= session_get_list();
3588 struct lttng_session_extended
*extended
=
3589 (typeof(extended
)) (&sessions
[session_count
]);
3591 DBG("Getting all available session for UID %d GID %d",
3594 * Iterate over session list and append data after the control struct in
3597 cds_list_for_each_entry(session
, &list
->head
, list
) {
3598 if (!session_get(session
)) {
3602 * Only list the sessions the user can control.
3604 if (!session_access_ok(session
, uid
, gid
) ||
3605 session
->destroyed
) {
3606 session_put(session
);
3610 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3611 struct ltt_ust_session
*usess
= session
->ust_session
;
3613 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3614 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3615 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3616 ret
= build_network_session_path(sessions
[i
].path
,
3617 sizeof(sessions
[i
].path
), session
);
3619 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3620 session
->consumer
->dst
.session_root_path
);
3623 PERROR("snprintf session path");
3624 session_put(session
);
3628 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3629 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3630 sessions
[i
].enabled
= session
->active
;
3631 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3632 sessions
[i
].live_timer_interval
= session
->live_timer
;
3633 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3634 extended
[i
].creation_time
.is_set
= 1;
3636 session_put(session
);
3641 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3642 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3644 int cmd_data_pending(struct ltt_session
*session
)
3647 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3648 struct ltt_ust_session
*usess
= session
->ust_session
;
3652 DBG("Data pending for session %s", session
->name
);
3654 /* Session MUST be stopped to ask for data availability. */
3655 if (session
->active
) {
3656 ret
= LTTNG_ERR_SESSION_STARTED
;
3660 * If stopped, just make sure we've started before else the above call
3661 * will always send that there is data pending.
3663 * The consumer assumes that when the data pending command is received,
3664 * the trace has been started before or else no output data is written
3665 * by the streams which is a condition for data pending. So, this is
3666 * *VERY* important that we don't ask the consumer before a start
3669 if (!session
->has_been_started
) {
3675 /* A rotation is still pending, we have to wait. */
3676 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3677 DBG("Rotate still pending for session %s", session
->name
);
3682 if (ksess
&& ksess
->consumer
) {
3683 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3685 /* Data is still being extracted for the kernel. */
3690 if (usess
&& usess
->consumer
) {
3691 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3693 /* Data is still being extracted for the kernel. */
3698 /* Data is ready to be read by a viewer */
3706 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3708 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3710 int cmd_snapshot_add_output(struct ltt_session
*session
,
3711 struct lttng_snapshot_output
*output
, uint32_t *id
)
3714 struct snapshot_output
*new_output
;
3719 DBG("Cmd snapshot add output for session %s", session
->name
);
3722 * Can't create an output if the session is not set in no-output mode.
3724 if (session
->output_traces
) {
3725 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3729 if (session
->has_non_mmap_channel
) {
3730 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3734 /* Only one output is allowed until we have the "tee" feature. */
3735 if (session
->snapshot
.nb_output
== 1) {
3736 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3740 new_output
= snapshot_output_alloc();
3742 ret
= LTTNG_ERR_NOMEM
;
3746 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3747 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3748 &session
->snapshot
);
3750 if (ret
== -ENOMEM
) {
3751 ret
= LTTNG_ERR_NOMEM
;
3753 ret
= LTTNG_ERR_INVALID
;
3759 snapshot_add_output(&session
->snapshot
, new_output
);
3761 *id
= new_output
->id
;
3768 snapshot_output_destroy(new_output
);
3774 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3776 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3778 int cmd_snapshot_del_output(struct ltt_session
*session
,
3779 struct lttng_snapshot_output
*output
)
3782 struct snapshot_output
*sout
= NULL
;
3790 * Permission denied to create an output if the session is not
3791 * set in no output mode.
3793 if (session
->output_traces
) {
3794 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3799 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3801 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3802 } else if (*output
->name
!= '\0') {
3803 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3805 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3808 ret
= LTTNG_ERR_INVALID
;
3812 snapshot_delete_output(&session
->snapshot
, sout
);
3813 snapshot_output_destroy(sout
);
3822 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3824 * If no output is available, outputs is untouched and 0 is returned.
3826 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3828 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3829 struct lttng_snapshot_output
**outputs
)
3832 struct lttng_snapshot_output
*list
= NULL
;
3833 struct lttng_ht_iter iter
;
3834 struct snapshot_output
*output
;
3839 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3842 * Permission denied to create an output if the session is not
3843 * set in no output mode.
3845 if (session
->output_traces
) {
3846 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3850 if (session
->snapshot
.nb_output
== 0) {
3855 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3857 ret
= -LTTNG_ERR_NOMEM
;
3861 /* Copy list from session to the new list object. */
3863 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3864 output
, node
.node
) {
3865 assert(output
->consumer
);
3866 list
[idx
].id
= output
->id
;
3867 list
[idx
].max_size
= output
->max_size
;
3868 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3869 sizeof(list
[idx
].name
))) {
3870 ret
= -LTTNG_ERR_INVALID
;
3873 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3874 if (lttng_strncpy(list
[idx
].ctrl_url
,
3875 output
->consumer
->dst
.session_root_path
,
3876 sizeof(list
[idx
].ctrl_url
))) {
3877 ret
= -LTTNG_ERR_INVALID
;
3882 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3883 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3885 ret
= -LTTNG_ERR_NOMEM
;
3890 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3891 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3893 ret
= -LTTNG_ERR_NOMEM
;
3902 ret
= session
->snapshot
.nb_output
;
3911 * Check if we can regenerate the metadata for this session.
3912 * Only kernel, UST per-uid and non-live sessions are supported.
3914 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3917 int check_regenerate_metadata_support(struct ltt_session
*session
)
3923 if (session
->live_timer
!= 0) {
3924 ret
= LTTNG_ERR_LIVE_SESSION
;
3927 if (!session
->active
) {
3928 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3931 if (session
->ust_session
) {
3932 switch (session
->ust_session
->buffer_type
) {
3933 case LTTNG_BUFFER_PER_UID
:
3935 case LTTNG_BUFFER_PER_PID
:
3936 ret
= LTTNG_ERR_PER_PID_SESSION
;
3940 ret
= LTTNG_ERR_UNK
;
3944 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3945 session
->consumer
->relay_minor_version
< 8) {
3946 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3956 int clear_metadata_file(int fd
)
3961 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
3962 if (lseek_ret
< 0) {
3968 ret
= ftruncate(fd
, 0);
3970 PERROR("ftruncate");
3979 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3982 struct buffer_reg_uid
*uid_reg
= NULL
;
3983 struct buffer_reg_session
*session_reg
= NULL
;
3986 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3987 struct ust_registry_session
*registry
;
3988 struct ust_registry_channel
*chan
;
3989 struct lttng_ht_iter iter_chan
;
3991 session_reg
= uid_reg
->registry
;
3992 registry
= session_reg
->reg
.ust
;
3994 pthread_mutex_lock(®istry
->lock
);
3995 registry
->metadata_len_sent
= 0;
3996 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3997 registry
->metadata_len
= 0;
3998 registry
->metadata_version
++;
3999 if (registry
->metadata_fd
> 0) {
4000 /* Clear the metadata file's content. */
4001 ret
= clear_metadata_file(registry
->metadata_fd
);
4003 pthread_mutex_unlock(®istry
->lock
);
4008 ret
= ust_metadata_session_statedump(registry
, NULL
,
4009 registry
->major
, registry
->minor
);
4011 pthread_mutex_unlock(®istry
->lock
);
4012 ERR("Failed to generate session metadata (err = %d)",
4016 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4018 struct ust_registry_event
*event
;
4019 struct lttng_ht_iter iter_event
;
4021 ret
= ust_metadata_channel_statedump(registry
, chan
);
4023 pthread_mutex_unlock(®istry
->lock
);
4024 ERR("Failed to generate channel metadata "
4028 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4030 ret
= ust_metadata_event_statedump(registry
,
4033 pthread_mutex_unlock(®istry
->lock
);
4034 ERR("Failed to generate event metadata "
4040 pthread_mutex_unlock(®istry
->lock
);
4049 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4051 * Ask the consumer to truncate the existing metadata file(s) and
4052 * then regenerate the metadata. Live and per-pid sessions are not
4053 * supported and return an error.
4055 * Return 0 on success or else a LTTNG_ERR code.
4057 int cmd_regenerate_metadata(struct ltt_session
*session
)
4063 ret
= check_regenerate_metadata_support(session
);
4068 if (session
->kernel_session
) {
4069 ret
= kernctl_session_regenerate_metadata(
4070 session
->kernel_session
->fd
);
4072 ERR("Failed to regenerate the kernel metadata");
4077 if (session
->ust_session
) {
4078 ret
= ust_regenerate_metadata(session
->ust_session
);
4080 ERR("Failed to regenerate the UST metadata");
4084 DBG("Cmd metadata regenerate for session %s", session
->name
);
4092 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4094 * Ask the tracer to regenerate a new statedump.
4096 * Return 0 on success or else a LTTNG_ERR code.
4098 int cmd_regenerate_statedump(struct ltt_session
*session
)
4104 if (!session
->active
) {
4105 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4109 if (session
->kernel_session
) {
4110 ret
= kernctl_session_regenerate_statedump(
4111 session
->kernel_session
->fd
);
4113 * Currently, the statedump in kernel can only fail if out
4117 if (ret
== -ENOMEM
) {
4118 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4120 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4122 ERR("Failed to regenerate the kernel statedump");
4127 if (session
->ust_session
) {
4128 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4130 * Currently, the statedump in UST always returns 0.
4133 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4134 ERR("Failed to regenerate the UST statedump");
4138 DBG("Cmd regenerate statedump for session %s", session
->name
);
4145 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4146 struct notification_thread_handle
*notification_thread
)
4150 ssize_t sock_recv_len
;
4151 struct lttng_trigger
*trigger
= NULL
;
4152 struct lttng_buffer_view view
;
4153 struct lttng_dynamic_buffer trigger_buffer
;
4155 lttng_dynamic_buffer_init(&trigger_buffer
);
4156 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4157 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4159 ret
= LTTNG_ERR_NOMEM
;
4163 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4165 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4166 ERR("Failed to receive \"register trigger\" command payload");
4167 /* TODO: should this be a new error enum ? */
4168 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4172 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4173 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4175 ERR("Invalid trigger payload received in \"register trigger\" command");
4176 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4180 ret
= notification_thread_command_register_trigger(notification_thread
,
4182 /* Ownership of trigger was transferred. */
4185 lttng_trigger_destroy(trigger
);
4186 lttng_dynamic_buffer_reset(&trigger_buffer
);
4190 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4191 struct notification_thread_handle
*notification_thread
)
4195 ssize_t sock_recv_len
;
4196 struct lttng_trigger
*trigger
= NULL
;
4197 struct lttng_buffer_view view
;
4198 struct lttng_dynamic_buffer trigger_buffer
;
4200 lttng_dynamic_buffer_init(&trigger_buffer
);
4201 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4202 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4204 ret
= LTTNG_ERR_NOMEM
;
4208 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4210 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4211 ERR("Failed to receive \"unregister trigger\" command payload");
4212 /* TODO: should this be a new error enum ? */
4213 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4217 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4218 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4220 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4221 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4225 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4228 lttng_trigger_destroy(trigger
);
4229 lttng_dynamic_buffer_reset(&trigger_buffer
);
4234 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4235 * snapshot output is *not* set with a remote destination.
4237 * Return LTTNG_OK on success or a LTTNG_ERR code.
4239 static enum lttng_error_code
set_relayd_for_snapshot(
4240 struct consumer_output
*output
,
4241 const struct ltt_session
*session
)
4243 enum lttng_error_code status
= LTTNG_OK
;
4244 struct lttng_ht_iter iter
;
4245 struct consumer_socket
*socket
;
4246 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4247 const char *base_path
;
4252 DBG2("Set relayd object from snapshot output");
4254 if (session
->current_trace_chunk
) {
4255 enum lttng_trace_chunk_status chunk_status
=
4256 lttng_trace_chunk_get_id(
4257 session
->current_trace_chunk
,
4258 ¤t_chunk_id
.value
);
4260 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4261 current_chunk_id
.is_set
= true;
4263 ERR("Failed to get current trace chunk id");
4264 status
= LTTNG_ERR_UNK
;
4269 /* Ignore if snapshot consumer output is not network. */
4270 if (output
->type
!= CONSUMER_DST_NET
) {
4275 * The snapshot record URI base path overrides the session
4278 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4279 base_path
= output
->dst
.net
.control
.subdir
;
4281 base_path
= session
->base_path
;
4285 * For each consumer socket, create and send the relayd object of the
4289 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4290 socket
, node
.node
) {
4291 pthread_mutex_lock(socket
->lock
);
4292 status
= send_consumer_relayd_sockets(0, session
->id
,
4294 session
->name
, session
->hostname
,
4296 session
->live_timer
,
4297 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4298 session
->creation_time
,
4299 session
->name_contains_creation_time
);
4300 pthread_mutex_unlock(socket
->lock
);
4301 if (status
!= LTTNG_OK
) {
4313 * Record a kernel snapshot.
4315 * Return LTTNG_OK on success or a LTTNG_ERR code.
4317 static enum lttng_error_code
record_kernel_snapshot(
4318 struct ltt_kernel_session
*ksess
,
4319 const struct consumer_output
*output
,
4320 const struct ltt_session
*session
,
4321 int wait
, uint64_t nb_packets_per_stream
)
4323 enum lttng_error_code status
;
4329 status
= kernel_snapshot_record(
4330 ksess
, output
, wait
, nb_packets_per_stream
);
4335 * Record a UST snapshot.
4337 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4339 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4340 const struct consumer_output
*output
,
4341 const struct ltt_session
*session
,
4342 int wait
, uint64_t nb_packets_per_stream
)
4344 enum lttng_error_code status
;
4350 status
= ust_app_snapshot_record(
4351 usess
, output
, wait
, nb_packets_per_stream
);
4356 uint64_t get_session_size_one_more_packet_per_stream(
4357 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4359 uint64_t tot_size
= 0;
4361 if (session
->kernel_session
) {
4362 struct ltt_kernel_channel
*chan
;
4363 const struct ltt_kernel_session
*ksess
=
4364 session
->kernel_session
;
4366 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4367 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4369 * Don't take channel into account if we
4370 * already grab all its packets.
4374 tot_size
+= chan
->channel
->attr
.subbuf_size
4375 * chan
->stream_count
;
4379 if (session
->ust_session
) {
4380 const struct ltt_ust_session
*usess
= session
->ust_session
;
4382 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4390 * Calculate the number of packets we can grab from each stream that
4391 * fits within the overall snapshot max size.
4393 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4394 * the number of packets per stream.
4396 * TODO: this approach is not perfect: we consider the worse case
4397 * (packet filling the sub-buffers) as an upper bound, but we could do
4398 * better if we do this calculation while we actually grab the packet
4399 * content: we would know how much padding we don't actually store into
4402 * This algorithm is currently bounded by the number of packets per
4405 * Since we call this algorithm before actually grabbing the data, it's
4406 * an approximation: for instance, applications could appear/disappear
4407 * in between this call and actually grabbing data.
4410 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4414 uint64_t cur_nb_packets
= 0;
4417 return 0; /* Infinite */
4420 size_left
= max_size
;
4422 uint64_t one_more_packet_tot_size
;
4424 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4425 session
, cur_nb_packets
);
4426 if (!one_more_packet_tot_size
) {
4427 /* We are already grabbing all packets. */
4430 size_left
-= one_more_packet_tot_size
;
4431 if (size_left
< 0) {
4436 if (!cur_nb_packets
) {
4437 /* Not enough room to grab one packet of each stream, error. */
4440 return cur_nb_packets
;
4444 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4445 const struct snapshot_output
*snapshot_output
, int wait
)
4447 int64_t nb_packets_per_stream
;
4448 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4450 enum lttng_error_code ret_code
= LTTNG_OK
;
4451 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4452 struct consumer_output
*original_ust_consumer_output
= NULL
;
4453 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4454 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4455 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4457 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4459 snapshot_output
->name
,
4460 snapshot_output
->datetime
,
4461 snapshot_output
->nb_snapshot
);
4462 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4463 ERR("Failed to format snapshot name");
4464 ret_code
= LTTNG_ERR_INVALID
;
4467 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4468 snapshot_output
->name
, session
->name
,
4469 snapshot_chunk_name
);
4470 if (!session
->kernel_session
&& !session
->ust_session
) {
4471 ERR("Failed to record snapshot as no channels exist");
4472 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4476 if (session
->kernel_session
) {
4477 original_kernel_consumer_output
=
4478 session
->kernel_session
->consumer
;
4479 snapshot_kernel_consumer_output
=
4480 consumer_copy_output(snapshot_output
->consumer
);
4481 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4482 original_kernel_consumer_output
);
4484 ERR("Failed to copy consumer sockets from snapshot output configuration");
4485 ret_code
= LTTNG_ERR_NOMEM
;
4488 ret_code
= set_relayd_for_snapshot(
4489 snapshot_kernel_consumer_output
, session
);
4490 if (ret_code
!= LTTNG_OK
) {
4491 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4494 session
->kernel_session
->consumer
=
4495 snapshot_kernel_consumer_output
;
4497 if (session
->ust_session
) {
4498 original_ust_consumer_output
= session
->ust_session
->consumer
;
4499 snapshot_ust_consumer_output
=
4500 consumer_copy_output(snapshot_output
->consumer
);
4501 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4502 original_ust_consumer_output
);
4504 ERR("Failed to copy consumer sockets from snapshot output configuration");
4505 ret_code
= LTTNG_ERR_NOMEM
;
4508 ret_code
= set_relayd_for_snapshot(
4509 snapshot_ust_consumer_output
, session
);
4510 if (ret_code
!= LTTNG_OK
) {
4511 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4514 session
->ust_session
->consumer
=
4515 snapshot_ust_consumer_output
;
4518 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4519 snapshot_kernel_consumer_output
?:
4520 snapshot_ust_consumer_output
,
4521 consumer_output_get_base_path(
4522 snapshot_output
->consumer
),
4523 snapshot_chunk_name
);
4524 if (!snapshot_trace_chunk
) {
4525 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4527 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4530 assert(!session
->current_trace_chunk
);
4531 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4532 lttng_trace_chunk_put(snapshot_trace_chunk
);
4533 snapshot_trace_chunk
= NULL
;
4535 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4537 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4541 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4542 snapshot_output
->max_size
);
4543 if (nb_packets_per_stream
< 0) {
4544 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4548 if (session
->kernel_session
) {
4549 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4550 snapshot_kernel_consumer_output
, session
,
4551 wait
, nb_packets_per_stream
);
4552 if (ret_code
!= LTTNG_OK
) {
4557 if (session
->ust_session
) {
4558 ret_code
= record_ust_snapshot(session
->ust_session
,
4559 snapshot_ust_consumer_output
, session
,
4560 wait
, nb_packets_per_stream
);
4561 if (ret_code
!= LTTNG_OK
) {
4566 if (session_close_trace_chunk(
4567 session
, session
->current_trace_chunk
, NULL
)) {
4569 * Don't goto end; make sure the chunk is closed for the session
4570 * to allow future snapshots.
4572 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4574 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4576 if (session_set_trace_chunk(session
, NULL
, NULL
)) {
4577 ERR("Failed to release the current trace chunk of session \"%s\"",
4579 ret_code
= LTTNG_ERR_UNK
;
4582 if (original_ust_consumer_output
) {
4583 session
->ust_session
->consumer
= original_ust_consumer_output
;
4585 if (original_kernel_consumer_output
) {
4586 session
->kernel_session
->consumer
=
4587 original_kernel_consumer_output
;
4589 consumer_output_put(snapshot_ust_consumer_output
);
4590 consumer_output_put(snapshot_kernel_consumer_output
);
4595 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4597 * The wait parameter is ignored so this call always wait for the snapshot to
4598 * complete before returning.
4600 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4602 int cmd_snapshot_record(struct ltt_session
*session
,
4603 struct lttng_snapshot_output
*output
, int wait
)
4605 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4607 unsigned int snapshot_success
= 0;
4609 struct snapshot_output
*tmp_output
= NULL
;
4614 DBG("Cmd snapshot record for session %s", session
->name
);
4616 /* Get the datetime for the snapshot output directory. */
4617 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4620 cmd_ret
= LTTNG_ERR_INVALID
;
4625 * Permission denied to create an output if the session is not
4626 * set in no output mode.
4628 if (session
->output_traces
) {
4629 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4633 /* The session needs to be started at least once. */
4634 if (!session
->has_been_started
) {
4635 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4639 /* Use temporary output for the session. */
4640 if (*output
->ctrl_url
!= '\0') {
4641 tmp_output
= snapshot_output_alloc();
4643 cmd_ret
= LTTNG_ERR_NOMEM
;
4647 ret
= snapshot_output_init(session
, output
->max_size
,
4649 output
->ctrl_url
, output
->data_url
,
4653 if (ret
== -ENOMEM
) {
4654 cmd_ret
= LTTNG_ERR_NOMEM
;
4656 cmd_ret
= LTTNG_ERR_INVALID
;
4660 /* Use the global session count for the temporary snapshot. */
4661 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
4663 /* Use the global datetime */
4664 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
4665 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
4666 if (cmd_ret
!= LTTNG_OK
) {
4669 snapshot_success
= 1;
4671 struct snapshot_output
*sout
;
4672 struct lttng_ht_iter iter
;
4675 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4676 &iter
.iter
, sout
, node
.node
) {
4677 struct snapshot_output output_copy
;
4680 * Make a local copy of the output and override output
4681 * parameters with those provided as part of the
4684 memcpy(&output_copy
, sout
, sizeof(output_copy
));
4686 if (output
->max_size
!= (uint64_t) -1ULL) {
4687 output_copy
.max_size
= output
->max_size
;
4690 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4691 memcpy(output_copy
.datetime
, datetime
,
4694 /* Use temporary name. */
4695 if (*output
->name
!= '\0') {
4696 if (lttng_strncpy(output_copy
.name
,
4698 sizeof(output_copy
.name
))) {
4699 cmd_ret
= LTTNG_ERR_INVALID
;
4705 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
4706 if (cmd_ret
!= LTTNG_OK
) {
4710 snapshot_success
= 1;
4715 if (snapshot_success
) {
4716 session
->snapshot
.nb_snapshot
++;
4718 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4723 snapshot_output_destroy(tmp_output
);
4729 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4731 int cmd_set_session_shm_path(struct ltt_session
*session
,
4732 const char *shm_path
)
4738 * Can only set shm path before session is started.
4740 if (session
->has_been_started
) {
4741 return LTTNG_ERR_SESSION_STARTED
;
4744 strncpy(session
->shm_path
, shm_path
,
4745 sizeof(session
->shm_path
));
4746 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4752 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4754 * Ask the consumer to rotate the session output directory.
4755 * The session lock must be held.
4757 * Returns LTTNG_OK on success or else a negative LTTng error code.
4759 int cmd_rotate_session(struct ltt_session
*session
,
4760 struct lttng_rotate_session_return
*rotate_return
,
4761 bool quiet_rotation
)
4764 uint64_t ongoing_rotation_chunk_id
;
4765 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4766 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
4767 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
4768 enum lttng_trace_chunk_status chunk_status
;
4772 if (!session
->has_been_started
) {
4773 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4778 * Explicit rotation is not supported for live sessions.
4779 * However, live sessions can perform a quiet rotation on
4781 * Rotation is not supported for snapshot traces (no output).
4783 if ((!quiet_rotation
&& session
->live_timer
) ||
4784 !session
->output_traces
) {
4785 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4789 /* Unsupported feature in lttng-relayd before 2.11. */
4790 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4791 (session
->consumer
->relay_major_version
== 2 &&
4792 session
->consumer
->relay_minor_version
< 11)) {
4793 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
4797 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4798 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
4800 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
4805 * After a stop, we only allow one rotation to occur, the other ones are
4806 * useless until a new start.
4808 if (session
->rotated_after_last_stop
) {
4809 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4811 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
4815 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
4817 if (session
->active
) {
4818 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
4820 if (!new_trace_chunk
) {
4821 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
4826 /* The current trace chunk becomes the chunk being archived. */
4827 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
4828 &chunk_being_archived
);
4830 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4834 assert(chunk_being_archived
);
4835 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
4836 &ongoing_rotation_chunk_id
);
4837 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
4839 if (session
->kernel_session
) {
4840 cmd_ret
= kernel_rotate_session(session
);
4841 if (cmd_ret
!= LTTNG_OK
) {
4845 if (session
->ust_session
) {
4846 cmd_ret
= ust_app_rotate_session(session
);
4847 if (cmd_ret
!= LTTNG_OK
) {
4852 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
4855 &((enum lttng_trace_chunk_command_type
){
4856 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
}));
4858 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4862 session
->quiet_rotation
= quiet_rotation
;
4863 ret
= timer_session_rotation_pending_check_start(session
,
4864 DEFAULT_ROTATE_PENDING_TIMER
);
4866 cmd_ret
= LTTNG_ERR_UNK
;
4870 if (!session
->active
) {
4871 session
->rotated_after_last_stop
= true;
4874 if (rotate_return
) {
4875 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
4878 session
->chunk_being_archived
= chunk_being_archived
;
4879 chunk_being_archived
= NULL
;
4880 if (!quiet_rotation
) {
4881 ret
= notification_thread_command_session_rotation_ongoing(
4882 notification_thread_handle
,
4883 session
->name
, session
->uid
, session
->gid
,
4884 ongoing_rotation_chunk_id
);
4885 if (ret
!= LTTNG_OK
) {
4886 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
4892 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
4893 session
->name
, ongoing_rotation_chunk_id
);
4895 lttng_trace_chunk_put(new_trace_chunk
);
4896 lttng_trace_chunk_put(chunk_being_archived
);
4897 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
4900 if (session_reset_rotation_state(session
,
4901 LTTNG_ROTATION_STATE_ERROR
)) {
4902 ERR("Failed to reset rotation state of session \"%s\"",
4909 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
4911 * Check if the session has finished its rotation.
4913 * Return LTTNG_OK on success or else an LTTNG_ERR code.
4915 int cmd_rotate_get_info(struct ltt_session
*session
,
4916 struct lttng_rotation_get_info_return
*info_return
,
4917 uint64_t rotation_id
)
4919 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4920 enum lttng_rotation_state rotation_state
;
4922 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
4923 session
->most_recent_chunk_id
.value
);
4925 if (session
->chunk_being_archived
) {
4926 enum lttng_trace_chunk_status chunk_status
;
4929 chunk_status
= lttng_trace_chunk_get_id(
4930 session
->chunk_being_archived
,
4932 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
4934 rotation_state
= rotation_id
== chunk_id
?
4935 LTTNG_ROTATION_STATE_ONGOING
:
4936 LTTNG_ROTATION_STATE_EXPIRED
;
4938 if (session
->last_archived_chunk_id
.is_set
&&
4939 rotation_id
!= session
->last_archived_chunk_id
.value
) {
4940 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
4942 rotation_state
= session
->rotation_state
;
4946 switch (rotation_state
) {
4947 case LTTNG_ROTATION_STATE_NO_ROTATION
:
4948 DBG("Reporting that no rotation has occured within the lifetime of session \"%s\"",
4951 case LTTNG_ROTATION_STATE_EXPIRED
:
4952 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
4953 rotation_id
, session
->name
);
4955 case LTTNG_ROTATION_STATE_ONGOING
:
4956 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
4957 rotation_id
, session
->name
);
4959 case LTTNG_ROTATION_STATE_COMPLETED
:
4963 char *current_tracing_path_reply
;
4964 size_t current_tracing_path_reply_len
;
4966 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
4967 rotation_id
, session
->name
);
4969 switch (session_get_consumer_destination_type(session
)) {
4970 case CONSUMER_DST_LOCAL
:
4971 current_tracing_path_reply
=
4972 info_return
->location
.local
.absolute_path
;
4973 current_tracing_path_reply_len
=
4974 sizeof(info_return
->location
.local
.absolute_path
);
4975 info_return
->location_type
=
4976 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
4978 case CONSUMER_DST_NET
:
4979 current_tracing_path_reply
=
4980 info_return
->location
.relay
.relative_path
;
4981 current_tracing_path_reply_len
=
4982 sizeof(info_return
->location
.relay
.relative_path
);
4983 /* Currently the only supported relay protocol. */
4984 info_return
->location
.relay
.protocol
=
4985 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
4987 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
4988 session_get_net_consumer_hostname(session
),
4989 sizeof(info_return
->location
.relay
.host
));
4991 ERR("Failed to copy host name to rotate_get_info reply");
4992 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4993 cmd_ret
= LTTNG_ERR_SET_URL
;
4997 session_get_net_consumer_ports(session
,
4998 &info_return
->location
.relay
.ports
.control
,
4999 &info_return
->location
.relay
.ports
.data
);
5000 info_return
->location_type
=
5001 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5006 fmt_ret
= asprintf(&chunk_path
,
5007 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5008 session_get_base_path(session
),
5009 session
->last_archived_chunk_name
);
5010 if (fmt_ret
== -1) {
5011 PERROR("Failed to format the path of the last archived trace chunk");
5012 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5013 cmd_ret
= LTTNG_ERR_UNK
;
5017 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5018 chunk_path
, current_tracing_path_reply_len
);
5021 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5022 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5023 cmd_ret
= LTTNG_ERR_UNK
;
5029 case LTTNG_ROTATION_STATE_ERROR
:
5030 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5031 rotation_id
, session
->name
);
5039 info_return
->status
= (int32_t) rotation_state
;
5044 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5046 * Configure the automatic rotation parameters.
5047 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5048 * 'activate' to false means deactivate the rotation schedule and validate that
5049 * 'new_value' has the same value as the currently active value.
5051 * Return 0 on success or else a positive LTTNG_ERR code.
5053 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5054 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5056 struct notification_thread_handle
*notification_thread_handle
)
5059 uint64_t *parameter_value
;
5063 DBG("Cmd rotate set schedule session %s", session
->name
);
5065 if (session
->live_timer
|| !session
->output_traces
) {
5066 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5067 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5071 switch (schedule_type
) {
5072 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5073 parameter_value
= &session
->rotate_size
;
5075 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5076 parameter_value
= &session
->rotate_timer_period
;
5077 if (new_value
>= UINT_MAX
) {
5078 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5079 new_value
, UINT_MAX
);
5080 ret
= LTTNG_ERR_INVALID
;
5085 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5086 ret
= LTTNG_ERR_INVALID
;
5090 /* Improper use of the API. */
5091 if (new_value
== -1ULL) {
5092 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5093 ret
= LTTNG_ERR_INVALID
;
5098 * As indicated in struct ltt_session's comments, a value of == 0 means
5099 * this schedule rotation type is not in use.
5101 * Reject the command if we were asked to activate a schedule that was
5104 if (activate
&& *parameter_value
!= 0) {
5105 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5106 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5111 * Reject the command if we were asked to deactivate a schedule that was
5114 if (!activate
&& *parameter_value
== 0) {
5115 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5116 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5121 * Reject the command if we were asked to deactivate a schedule that
5124 if (!activate
&& *parameter_value
!= new_value
) {
5125 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5126 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5130 *parameter_value
= activate
? new_value
: 0;
5132 switch (schedule_type
) {
5133 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5134 if (activate
&& session
->active
) {
5136 * Only start the timer if the session is active,
5137 * otherwise it will be started when the session starts.
5139 ret
= timer_session_rotation_schedule_timer_start(
5140 session
, new_value
);
5142 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5143 ret
= LTTNG_ERR_UNK
;
5147 ret
= timer_session_rotation_schedule_timer_stop(
5150 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5151 ret
= LTTNG_ERR_UNK
;
5156 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5158 ret
= subscribe_session_consumed_size_rotation(session
,
5159 new_value
, notification_thread_handle
);
5161 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5162 ret
= LTTNG_ERR_UNK
;
5166 ret
= unsubscribe_session_consumed_size_rotation(session
,
5167 notification_thread_handle
);
5169 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5170 ret
= LTTNG_ERR_UNK
;
5177 /* Would have been caught before. */
5189 /* Wait for a given path to be removed before continuing. */
5190 static enum lttng_error_code
wait_on_path(void *path_data
)
5192 const char *shm_path
= path_data
;
5194 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5200 ret
= stat(shm_path
, &st
);
5202 if (errno
!= ENOENT
) {
5203 PERROR("stat() returned an error while checking for the existence of the shm path");
5205 DBG("shm path no longer exists, completing the destruction of session");
5209 if (!S_ISDIR(st
.st_mode
)) {
5210 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5215 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5221 * Returns a pointer to a handler to run on completion of a command.
5222 * Returns NULL if no handler has to be run for the last command executed.
5224 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5226 struct cmd_completion_handler
*handler
= current_completion_handler
;
5228 current_completion_handler
= NULL
;
5233 * Init command subsystem.
5238 * Set network sequence index to 1 for streams to match a relayd
5239 * socket on the consumer side.
5241 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5242 relayd_net_seq_idx
= 1;
5243 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5245 DBG("Command subsystem initialized");