2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
9 #include "bin/lttng-sessiond/tracker.h"
10 #include "lttng/lttng-error.h"
11 #include "lttng/tracker.h"
15 #include <urcu/list.h>
16 #include <urcu/uatomic.h>
20 #include <common/defaults.h>
21 #include <common/common.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/relayd/relayd.h>
24 #include <common/utils.h>
25 #include <common/compat/string.h>
26 #include <common/kernel-ctl/kernel-ctl.h>
27 #include <common/dynamic-buffer.h>
28 #include <common/buffer-view.h>
29 #include <common/payload.h>
30 #include <common/payload-view.h>
31 #include <common/trace-chunk.h>
32 #include <lttng/location-internal.h>
33 #include <lttng/trigger/trigger-internal.h>
34 #include <lttng/condition/condition.h>
35 #include <lttng/action/action.h>
36 #include <lttng/channel.h>
37 #include <lttng/channel-internal.h>
38 #include <lttng/rotate-internal.h>
39 #include <lttng/location-internal.h>
40 #include <lttng/session-internal.h>
41 #include <lttng/userspace-probe-internal.h>
42 #include <lttng/session-descriptor-internal.h>
43 #include <common/string-utils/string-utils.h>
48 #include "health-sessiond.h"
50 #include "kernel-consumer.h"
51 #include "lttng-sessiond.h"
53 #include "lttng-syscall.h"
55 #include "buffer-registry.h"
56 #include "notification-thread.h"
57 #include "notification-thread-commands.h"
59 #include "rotation-thread.h"
61 #include "agent-thread.h"
65 /* Sleep for 100ms between each check for the shm path's deletion. */
66 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
68 struct cmd_destroy_session_reply_context
{
70 bool implicit_rotation_on_destroy
;
72 * Indicates whether or not an error occurred while launching the
73 * destruction of a session.
75 enum lttng_error_code destruction_status
;
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 const 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
;
825 * Add URI so the consumer output object. Set the correct path depending on the
826 * domain adding the default trace directory.
828 static enum lttng_error_code
add_uri_to_consumer(
829 const struct ltt_session
*session
,
830 struct consumer_output
*consumer
,
831 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
834 enum lttng_error_code ret_code
= LTTNG_OK
;
838 if (consumer
== NULL
) {
839 DBG("No consumer detected. Don't add URI. Stopping.");
840 ret_code
= LTTNG_ERR_NO_CONSUMER
;
845 case LTTNG_DOMAIN_KERNEL
:
846 ret
= lttng_strncpy(consumer
->domain_subdir
,
847 DEFAULT_KERNEL_TRACE_DIR
,
848 sizeof(consumer
->domain_subdir
));
850 case LTTNG_DOMAIN_UST
:
851 ret
= lttng_strncpy(consumer
->domain_subdir
,
852 DEFAULT_UST_TRACE_DIR
,
853 sizeof(consumer
->domain_subdir
));
857 * This case is possible is we try to add the URI to the global
858 * tracing session consumer object which in this case there is
861 memset(consumer
->domain_subdir
, 0,
862 sizeof(consumer
->domain_subdir
));
866 ERR("Failed to initialize consumer output domain subdirectory");
867 ret_code
= LTTNG_ERR_FATAL
;
871 switch (uri
->dtype
) {
874 DBG2("Setting network URI to consumer");
876 if (consumer
->type
== CONSUMER_DST_NET
) {
877 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
878 consumer
->dst
.net
.control_isset
) ||
879 (uri
->stype
== LTTNG_STREAM_DATA
&&
880 consumer
->dst
.net
.data_isset
)) {
881 ret_code
= LTTNG_ERR_URL_EXIST
;
885 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
888 /* Set URI into consumer output object */
889 ret
= consumer_set_network_uri(session
, consumer
, uri
);
893 } else if (ret
== 1) {
895 * URI was the same in the consumer so we do not append the subdir
896 * again so to not duplicate output dir.
903 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
904 ret_code
= LTTNG_ERR_INVALID
;
907 DBG2("Setting trace directory path from URI to %s",
909 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
911 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
913 sizeof(consumer
->dst
.session_root_path
));
915 ret_code
= LTTNG_ERR_FATAL
;
918 consumer
->type
= CONSUMER_DST_LOCAL
;
928 * Init tracing by creating trace directory and sending fds kernel consumer.
930 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
933 struct lttng_ht_iter iter
;
934 struct consumer_socket
*socket
;
940 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
941 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
943 pthread_mutex_lock(socket
->lock
);
944 ret
= kernel_consumer_send_session(socket
, session
);
945 pthread_mutex_unlock(socket
->lock
);
947 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
959 * Create a socket to the relayd using the URI.
961 * On success, the relayd_sock pointer is set to the created socket.
962 * Else, it remains untouched and an LTTng error code is returned.
964 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
965 struct lttcomm_relayd_sock
**relayd_sock
,
966 struct consumer_output
*consumer
)
969 enum lttng_error_code status
= LTTNG_OK
;
970 struct lttcomm_relayd_sock
*rsock
;
972 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
973 RELAYD_VERSION_COMM_MINOR
);
975 status
= LTTNG_ERR_FATAL
;
980 * Connect to relayd so we can proceed with a session creation. This call
981 * can possibly block for an arbitrary amount of time to set the health
982 * state to be in poll execution.
985 ret
= relayd_connect(rsock
);
988 ERR("Unable to reach lttng-relayd");
989 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
993 /* Create socket for control stream. */
994 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
995 uint64_t result_flags
;
997 DBG3("Creating relayd stream socket from URI");
999 /* Check relayd version */
1000 ret
= relayd_version_check(rsock
);
1001 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
1002 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
1004 } else if (ret
< 0) {
1005 ERR("Unable to reach lttng-relayd");
1006 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1009 consumer
->relay_major_version
= rsock
->major
;
1010 consumer
->relay_minor_version
= rsock
->minor
;
1011 ret
= relayd_get_configuration(rsock
, 0,
1014 ERR("Unable to get relayd configuration");
1015 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1018 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
1019 consumer
->relay_allows_clear
= true;
1021 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1022 DBG3("Creating relayd data socket from URI");
1024 /* Command is not valid */
1025 ERR("Relayd invalid stream type: %d", uri
->stype
);
1026 status
= LTTNG_ERR_INVALID
;
1030 *relayd_sock
= rsock
;
1035 /* The returned value is not useful since we are on an error path. */
1036 (void) relayd_close(rsock
);
1044 * Connect to the relayd using URI and send the socket to the right consumer.
1046 * The consumer socket lock must be held by the caller.
1048 * Returns LTTNG_OK on success or an LTTng error code on failure.
1050 static enum lttng_error_code
send_consumer_relayd_socket(
1051 unsigned int session_id
,
1052 struct lttng_uri
*relayd_uri
,
1053 struct consumer_output
*consumer
,
1054 struct consumer_socket
*consumer_sock
,
1055 const char *session_name
, const char *hostname
,
1056 const char *base_path
, int session_live_timer
,
1057 const uint64_t *current_chunk_id
,
1058 time_t session_creation_time
,
1059 bool session_name_contains_creation_time
)
1062 struct lttcomm_relayd_sock
*rsock
= NULL
;
1063 enum lttng_error_code status
;
1065 /* Connect to relayd and make version check if uri is the control. */
1066 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1067 if (status
!= LTTNG_OK
) {
1068 goto relayd_comm_error
;
1072 /* Set the network sequence index if not set. */
1073 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1074 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1076 * Increment net_seq_idx because we are about to transfer the
1077 * new relayd socket to the consumer.
1078 * Assign unique key so the consumer can match streams.
1080 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1081 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1084 /* Send relayd socket to consumer. */
1085 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1086 relayd_uri
->stype
, session_id
,
1087 session_name
, hostname
, base_path
,
1088 session_live_timer
, current_chunk_id
,
1089 session_creation_time
, session_name_contains_creation_time
);
1091 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1095 /* Flag that the corresponding socket was sent. */
1096 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1097 consumer_sock
->control_sock_sent
= 1;
1098 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1099 consumer_sock
->data_sock_sent
= 1;
1103 * Close socket which was dup on the consumer side. The session daemon does
1104 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1108 if (status
!= LTTNG_OK
) {
1110 * The consumer output for this session should not be used anymore
1111 * since the relayd connection failed thus making any tracing or/and
1112 * streaming not usable.
1114 consumer
->enabled
= 0;
1116 (void) relayd_close(rsock
);
1124 * Send both relayd sockets to a specific consumer and domain. This is a
1125 * helper function to facilitate sending the information to the consumer for a
1128 * The consumer socket lock must be held by the caller.
1130 * Returns LTTNG_OK, or an LTTng error code on failure.
1132 static enum lttng_error_code
send_consumer_relayd_sockets(
1133 enum lttng_domain_type domain
,
1134 unsigned int session_id
, struct consumer_output
*consumer
,
1135 struct consumer_socket
*sock
, const char *session_name
,
1136 const char *hostname
, const char *base_path
, int session_live_timer
,
1137 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1138 bool session_name_contains_creation_time
)
1140 enum lttng_error_code status
= LTTNG_OK
;
1145 /* Sending control relayd socket. */
1146 if (!sock
->control_sock_sent
) {
1147 status
= send_consumer_relayd_socket(session_id
,
1148 &consumer
->dst
.net
.control
, consumer
, sock
,
1149 session_name
, hostname
, base_path
, session_live_timer
,
1150 current_chunk_id
, session_creation_time
,
1151 session_name_contains_creation_time
);
1152 if (status
!= LTTNG_OK
) {
1157 /* Sending data relayd socket. */
1158 if (!sock
->data_sock_sent
) {
1159 status
= send_consumer_relayd_socket(session_id
,
1160 &consumer
->dst
.net
.data
, consumer
, sock
,
1161 session_name
, hostname
, base_path
, session_live_timer
,
1162 current_chunk_id
, session_creation_time
,
1163 session_name_contains_creation_time
);
1164 if (status
!= LTTNG_OK
) {
1174 * Setup relayd connections for a tracing session. First creates the socket to
1175 * the relayd and send them to the right domain consumer. Consumer type MUST be
1178 int cmd_setup_relayd(struct ltt_session
*session
)
1181 struct ltt_ust_session
*usess
;
1182 struct ltt_kernel_session
*ksess
;
1183 struct consumer_socket
*socket
;
1184 struct lttng_ht_iter iter
;
1185 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1189 usess
= session
->ust_session
;
1190 ksess
= session
->kernel_session
;
1192 DBG("Setting relayd for session %s", session
->name
);
1195 if (session
->current_trace_chunk
) {
1196 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1197 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1199 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1200 current_chunk_id
.is_set
= true;
1202 ERR("Failed to get current trace chunk id");
1203 ret
= LTTNG_ERR_UNK
;
1208 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1209 && usess
->consumer
->enabled
) {
1210 /* For each consumer socket, send relayd sockets */
1211 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1212 socket
, node
.node
) {
1213 pthread_mutex_lock(socket
->lock
);
1214 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1215 usess
->consumer
, socket
,
1216 session
->name
, session
->hostname
,
1218 session
->live_timer
,
1219 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1220 session
->creation_time
,
1221 session
->name_contains_creation_time
);
1222 pthread_mutex_unlock(socket
->lock
);
1223 if (ret
!= LTTNG_OK
) {
1226 /* Session is now ready for network streaming. */
1227 session
->net_handle
= 1;
1229 session
->consumer
->relay_major_version
=
1230 usess
->consumer
->relay_major_version
;
1231 session
->consumer
->relay_minor_version
=
1232 usess
->consumer
->relay_minor_version
;
1233 session
->consumer
->relay_allows_clear
=
1234 usess
->consumer
->relay_allows_clear
;
1237 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1238 && ksess
->consumer
->enabled
) {
1239 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1240 socket
, node
.node
) {
1241 pthread_mutex_lock(socket
->lock
);
1242 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1243 ksess
->consumer
, socket
,
1244 session
->name
, session
->hostname
,
1246 session
->live_timer
,
1247 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1248 session
->creation_time
,
1249 session
->name_contains_creation_time
);
1250 pthread_mutex_unlock(socket
->lock
);
1251 if (ret
!= LTTNG_OK
) {
1254 /* Session is now ready for network streaming. */
1255 session
->net_handle
= 1;
1257 session
->consumer
->relay_major_version
=
1258 ksess
->consumer
->relay_major_version
;
1259 session
->consumer
->relay_minor_version
=
1260 ksess
->consumer
->relay_minor_version
;
1261 session
->consumer
->relay_allows_clear
=
1262 ksess
->consumer
->relay_allows_clear
;
1271 * Start a kernel session by opening all necessary streams.
1273 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1276 struct ltt_kernel_channel
*kchan
;
1278 /* Open kernel metadata */
1279 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1280 ret
= kernel_open_metadata(ksess
);
1282 ret
= LTTNG_ERR_KERN_META_FAIL
;
1287 /* Open kernel metadata stream */
1288 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1289 ret
= kernel_open_metadata_stream(ksess
);
1291 ERR("Kernel create metadata stream failed");
1292 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1297 /* For each channel */
1298 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1299 if (kchan
->stream_count
== 0) {
1300 ret
= kernel_open_channel_stream(kchan
);
1302 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1305 /* Update the stream global counter */
1306 ksess
->stream_count_global
+= ret
;
1310 /* Setup kernel consumer socket and send fds to it */
1311 ret
= init_kernel_tracing(ksess
);
1313 ret
= LTTNG_ERR_KERN_START_FAIL
;
1317 /* This start the kernel tracing */
1318 ret
= kernel_start_session(ksess
);
1320 ret
= LTTNG_ERR_KERN_START_FAIL
;
1324 /* Quiescent wait after starting trace */
1325 kernel_wait_quiescent();
1335 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1337 struct ltt_kernel_channel
*kchan
;
1338 bool error_occurred
= false;
1341 if (!ksess
|| !ksess
->active
) {
1344 DBG("Stopping kernel tracing");
1346 ret
= kernel_stop_session(ksess
);
1348 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1352 kernel_wait_quiescent();
1354 /* Flush metadata after stopping (if exists) */
1355 if (ksess
->metadata_stream_fd
>= 0) {
1356 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1358 ERR("Kernel metadata flush failed");
1359 error_occurred
= true;
1363 /* Flush all buffers after stopping */
1364 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1365 ret
= kernel_flush_buffer(kchan
);
1367 ERR("Kernel flush buffer error");
1368 error_occurred
= true;
1373 if (error_occurred
) {
1374 ret
= LTTNG_ERR_UNK
;
1383 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1385 int cmd_disable_channel(struct ltt_session
*session
,
1386 enum lttng_domain_type domain
, char *channel_name
)
1389 struct ltt_ust_session
*usess
;
1391 usess
= session
->ust_session
;
1396 case LTTNG_DOMAIN_KERNEL
:
1398 ret
= channel_kernel_disable(session
->kernel_session
,
1400 if (ret
!= LTTNG_OK
) {
1404 kernel_wait_quiescent();
1407 case LTTNG_DOMAIN_UST
:
1409 struct ltt_ust_channel
*uchan
;
1410 struct lttng_ht
*chan_ht
;
1412 chan_ht
= usess
->domain_global
.channels
;
1414 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1415 if (uchan
== NULL
) {
1416 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1420 ret
= channel_ust_disable(usess
, uchan
);
1421 if (ret
!= LTTNG_OK
) {
1427 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1439 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1441 * The wpipe arguments is used as a notifier for the kernel thread.
1443 int cmd_enable_channel(struct ltt_session
*session
,
1444 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1447 struct ltt_ust_session
*usess
= session
->ust_session
;
1448 struct lttng_ht
*chan_ht
;
1450 struct lttng_channel attr
;
1457 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1459 /* Validate channel name */
1460 if (attr
.name
[0] == '.' ||
1461 memchr(attr
.name
, '/', len
) != NULL
) {
1462 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1466 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1471 * Don't try to enable a channel if the session has been started at
1472 * some point in time before. The tracer does not allow it.
1474 if (session
->has_been_started
) {
1475 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1480 * If the session is a live session, remove the switch timer, the
1481 * live timer does the same thing but sends also synchronisation
1482 * beacons for inactive streams.
1484 if (session
->live_timer
> 0) {
1485 attr
.attr
.live_timer_interval
= session
->live_timer
;
1486 attr
.attr
.switch_timer_interval
= 0;
1489 /* Check for feature support */
1490 switch (domain
->type
) {
1491 case LTTNG_DOMAIN_KERNEL
:
1493 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1494 /* Sampling position of buffer is not supported */
1495 WARN("Kernel tracer does not support buffer monitoring. "
1496 "Setting the monitor interval timer to 0 "
1497 "(disabled) for channel '%s' of session '%s'",
1498 attr
.name
, session
->name
);
1499 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1503 case LTTNG_DOMAIN_UST
:
1505 case LTTNG_DOMAIN_JUL
:
1506 case LTTNG_DOMAIN_LOG4J
:
1507 case LTTNG_DOMAIN_PYTHON
:
1508 if (!agent_tracing_is_enabled()) {
1509 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1510 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1515 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1519 switch (domain
->type
) {
1520 case LTTNG_DOMAIN_KERNEL
:
1522 struct ltt_kernel_channel
*kchan
;
1524 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1525 session
->kernel_session
);
1526 if (kchan
== NULL
) {
1527 if (session
->snapshot
.nb_output
> 0 ||
1528 session
->snapshot_mode
) {
1529 /* Enforce mmap output for snapshot sessions. */
1530 attr
.attr
.output
= LTTNG_EVENT_MMAP
;
1532 ret
= channel_kernel_create(session
->kernel_session
, &attr
, wpipe
);
1533 if (attr
.name
[0] != '\0') {
1534 session
->kernel_session
->has_non_default_channel
= 1;
1537 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1540 if (ret
!= LTTNG_OK
) {
1544 kernel_wait_quiescent();
1547 case LTTNG_DOMAIN_UST
:
1548 case LTTNG_DOMAIN_JUL
:
1549 case LTTNG_DOMAIN_LOG4J
:
1550 case LTTNG_DOMAIN_PYTHON
:
1552 struct ltt_ust_channel
*uchan
;
1557 * Current agent implementation limitations force us to allow
1558 * only one channel at once in "agent" subdomains. Each
1559 * subdomain has a default channel name which must be strictly
1562 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1563 if (strncmp(attr
.name
, DEFAULT_JUL_CHANNEL_NAME
,
1564 LTTNG_SYMBOL_NAME_LEN
)) {
1565 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1568 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1569 if (strncmp(attr
.name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1570 LTTNG_SYMBOL_NAME_LEN
)) {
1571 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1574 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1575 if (strncmp(attr
.name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1576 LTTNG_SYMBOL_NAME_LEN
)) {
1577 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1582 chan_ht
= usess
->domain_global
.channels
;
1584 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1585 if (uchan
== NULL
) {
1586 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1587 if (attr
.name
[0] != '\0') {
1588 usess
->has_non_default_channel
= 1;
1591 ret
= channel_ust_enable(usess
, uchan
);
1596 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1600 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1601 session
->has_non_mmap_channel
= true;
1609 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1610 struct ltt_session
*session
,
1611 enum lttng_domain_type domain
,
1612 enum lttng_process_attr process_attr
,
1613 enum lttng_tracking_policy
*policy
)
1615 enum lttng_error_code ret_code
= LTTNG_OK
;
1616 const struct process_attr_tracker
*tracker
;
1619 case LTTNG_DOMAIN_KERNEL
:
1620 if (!session
->kernel_session
) {
1621 ret_code
= LTTNG_ERR_INVALID
;
1624 tracker
= kernel_get_process_attr_tracker(
1625 session
->kernel_session
, process_attr
);
1627 case LTTNG_DOMAIN_UST
:
1628 if (!session
->ust_session
) {
1629 ret_code
= LTTNG_ERR_INVALID
;
1632 tracker
= trace_ust_get_process_attr_tracker(
1633 session
->ust_session
, process_attr
);
1636 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1640 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1642 ret_code
= LTTNG_ERR_INVALID
;
1648 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1649 struct ltt_session
*session
,
1650 enum lttng_domain_type domain
,
1651 enum lttng_process_attr process_attr
,
1652 enum lttng_tracking_policy policy
)
1654 enum lttng_error_code ret_code
= LTTNG_OK
;
1657 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1658 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1659 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1662 ret_code
= LTTNG_ERR_INVALID
;
1667 case LTTNG_DOMAIN_KERNEL
:
1668 if (!session
->kernel_session
) {
1669 ret_code
= LTTNG_ERR_INVALID
;
1672 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1673 session
->kernel_session
, process_attr
, policy
);
1675 case LTTNG_DOMAIN_UST
:
1676 if (!session
->ust_session
) {
1677 ret_code
= LTTNG_ERR_INVALID
;
1680 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1681 session
->ust_session
, process_attr
, policy
);
1684 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1691 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1692 struct ltt_session
*session
,
1693 enum lttng_domain_type domain
,
1694 enum lttng_process_attr process_attr
,
1695 const struct process_attr_value
*value
)
1697 enum lttng_error_code ret_code
= LTTNG_OK
;
1700 case LTTNG_DOMAIN_KERNEL
:
1701 if (!session
->kernel_session
) {
1702 ret_code
= LTTNG_ERR_INVALID
;
1705 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1706 session
->kernel_session
, process_attr
, value
);
1708 case LTTNG_DOMAIN_UST
:
1709 if (!session
->ust_session
) {
1710 ret_code
= LTTNG_ERR_INVALID
;
1713 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1714 session
->ust_session
, process_attr
, value
);
1717 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1724 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1725 struct ltt_session
*session
,
1726 enum lttng_domain_type domain
,
1727 enum lttng_process_attr process_attr
,
1728 const struct process_attr_value
*value
)
1730 enum lttng_error_code ret_code
= LTTNG_OK
;
1733 case LTTNG_DOMAIN_KERNEL
:
1734 if (!session
->kernel_session
) {
1735 ret_code
= LTTNG_ERR_INVALID
;
1738 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1739 session
->kernel_session
, process_attr
, value
);
1741 case LTTNG_DOMAIN_UST
:
1742 if (!session
->ust_session
) {
1743 ret_code
= LTTNG_ERR_INVALID
;
1746 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1747 session
->ust_session
, process_attr
, value
);
1750 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1757 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1758 struct ltt_session
*session
,
1759 enum lttng_domain_type domain
,
1760 enum lttng_process_attr process_attr
,
1761 struct lttng_process_attr_values
**values
)
1763 enum lttng_error_code ret_code
= LTTNG_OK
;
1764 const struct process_attr_tracker
*tracker
;
1765 enum process_attr_tracker_status status
;
1768 case LTTNG_DOMAIN_KERNEL
:
1769 if (!session
->kernel_session
) {
1770 ret_code
= LTTNG_ERR_INVALID
;
1773 tracker
= kernel_get_process_attr_tracker(
1774 session
->kernel_session
, process_attr
);
1776 case LTTNG_DOMAIN_UST
:
1777 if (!session
->ust_session
) {
1778 ret_code
= LTTNG_ERR_INVALID
;
1781 tracker
= trace_ust_get_process_attr_tracker(
1782 session
->ust_session
, process_attr
);
1785 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1790 ret_code
= LTTNG_ERR_INVALID
;
1794 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1796 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1797 ret_code
= LTTNG_OK
;
1799 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1800 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1802 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1803 ret_code
= LTTNG_ERR_NOMEM
;
1806 ret_code
= LTTNG_ERR_UNK
;
1815 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1817 int cmd_disable_event(struct ltt_session
*session
,
1818 enum lttng_domain_type domain
, const char *channel_name
,
1819 const struct lttng_event
*event
)
1822 const char *event_name
;
1824 DBG("Disable event command for event \'%s\'", event
->name
);
1826 event_name
= event
->name
;
1828 /* Error out on unhandled search criteria */
1829 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1830 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1831 ret
= LTTNG_ERR_UNK
;
1838 case LTTNG_DOMAIN_KERNEL
:
1840 struct ltt_kernel_channel
*kchan
;
1841 struct ltt_kernel_session
*ksess
;
1843 ksess
= session
->kernel_session
;
1846 * If a non-default channel has been created in the
1847 * session, explicitely require that -c chan_name needs
1850 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1851 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1855 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1856 if (kchan
== NULL
) {
1857 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1861 switch (event
->type
) {
1862 case LTTNG_EVENT_ALL
:
1863 case LTTNG_EVENT_TRACEPOINT
:
1864 case LTTNG_EVENT_SYSCALL
:
1865 case LTTNG_EVENT_PROBE
:
1866 case LTTNG_EVENT_FUNCTION
:
1867 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1868 if (event_name
[0] == '\0') {
1869 ret
= event_kernel_disable_event(kchan
,
1872 ret
= event_kernel_disable_event(kchan
,
1873 event_name
, event
->type
);
1875 if (ret
!= LTTNG_OK
) {
1880 ret
= LTTNG_ERR_UNK
;
1884 kernel_wait_quiescent();
1887 case LTTNG_DOMAIN_UST
:
1889 struct ltt_ust_channel
*uchan
;
1890 struct ltt_ust_session
*usess
;
1892 usess
= session
->ust_session
;
1894 if (validate_ust_event_name(event_name
)) {
1895 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1900 * If a non-default channel has been created in the
1901 * session, explicitly require that -c chan_name needs
1904 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1905 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1909 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1911 if (uchan
== NULL
) {
1912 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1916 switch (event
->type
) {
1917 case LTTNG_EVENT_ALL
:
1919 * An empty event name means that everything
1920 * should be disabled.
1922 if (event
->name
[0] == '\0') {
1923 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1925 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1928 if (ret
!= LTTNG_OK
) {
1933 ret
= LTTNG_ERR_UNK
;
1937 DBG3("Disable UST event %s in channel %s completed", event_name
,
1941 case LTTNG_DOMAIN_LOG4J
:
1942 case LTTNG_DOMAIN_JUL
:
1943 case LTTNG_DOMAIN_PYTHON
:
1946 struct ltt_ust_session
*usess
= session
->ust_session
;
1950 switch (event
->type
) {
1951 case LTTNG_EVENT_ALL
:
1954 ret
= LTTNG_ERR_UNK
;
1958 agt
= trace_ust_find_agent(usess
, domain
);
1960 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1964 * An empty event name means that everything
1965 * should be disabled.
1967 if (event
->name
[0] == '\0') {
1968 ret
= event_agent_disable_all(usess
, agt
);
1970 ret
= event_agent_disable(usess
, agt
, event_name
);
1972 if (ret
!= LTTNG_OK
) {
1979 ret
= LTTNG_ERR_UND
;
1992 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1994 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1995 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1997 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1998 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
2001 * Don't try to add a context if the session has been started at
2002 * some point in time before. The tracer does not allow it and would
2003 * result in a corrupted trace.
2005 if (session
->has_been_started
) {
2006 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2010 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2011 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
2012 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
2016 case LTTNG_DOMAIN_KERNEL
:
2017 assert(session
->kernel_session
);
2019 if (session
->kernel_session
->channel_count
== 0) {
2020 /* Create default channel */
2021 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
2022 if (ret
!= LTTNG_OK
) {
2025 chan_kern_created
= 1;
2027 /* Add kernel context to kernel tracer */
2028 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
2029 if (ret
!= LTTNG_OK
) {
2033 case LTTNG_DOMAIN_JUL
:
2034 case LTTNG_DOMAIN_LOG4J
:
2037 * Validate channel name.
2038 * If no channel name is given and the domain is JUL or LOG4J,
2039 * set it to the appropriate domain-specific channel name. If
2040 * a name is provided but does not match the expexted channel
2041 * name, return an error.
2043 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
2044 strcmp(channel_name
,
2045 DEFAULT_JUL_CHANNEL_NAME
)) {
2046 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2048 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
2049 strcmp(channel_name
,
2050 DEFAULT_LOG4J_CHANNEL_NAME
)) {
2051 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2054 /* break is _not_ missing here. */
2056 case LTTNG_DOMAIN_UST
:
2058 struct ltt_ust_session
*usess
= session
->ust_session
;
2059 unsigned int chan_count
;
2063 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
2064 if (chan_count
== 0) {
2065 struct lttng_channel
*attr
;
2066 /* Create default channel */
2067 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
2069 ret
= LTTNG_ERR_FATAL
;
2073 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
2074 if (ret
!= LTTNG_OK
) {
2078 channel_attr_destroy(attr
);
2079 chan_ust_created
= 1;
2082 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2083 free(app_ctx_provider_name
);
2085 app_ctx_name
= NULL
;
2086 app_ctx_provider_name
= NULL
;
2087 if (ret
!= LTTNG_OK
) {
2093 ret
= LTTNG_ERR_UND
;
2101 if (chan_kern_created
) {
2102 struct ltt_kernel_channel
*kchan
=
2103 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2104 session
->kernel_session
);
2105 /* Created previously, this should NOT fail. */
2107 kernel_destroy_channel(kchan
);
2110 if (chan_ust_created
) {
2111 struct ltt_ust_channel
*uchan
=
2112 trace_ust_find_channel_by_name(
2113 session
->ust_session
->domain_global
.channels
,
2114 DEFAULT_CHANNEL_NAME
);
2115 /* Created previously, this should NOT fail. */
2117 /* Remove from the channel list of the session. */
2118 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2120 trace_ust_destroy_channel(uchan
);
2123 free(app_ctx_provider_name
);
2128 static inline bool name_starts_with(const char *name
, const char *prefix
)
2130 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2132 return !strncmp(name
, prefix
, max_cmp_len
);
2135 /* Perform userspace-specific event name validation */
2136 static int validate_ust_event_name(const char *name
)
2146 * Check name against all internal UST event component namespaces used
2149 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2150 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2151 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2160 * Internal version of cmd_enable_event() with a supplemental
2161 * "internal_event" flag which is used to enable internal events which should
2162 * be hidden from clients. Such events are used in the agent implementation to
2163 * enable the events through which all "agent" events are funeled.
2165 static int _cmd_enable_event(struct ltt_session
*session
,
2166 const struct lttng_domain
*domain
,
2167 char *channel_name
, struct lttng_event
*event
,
2168 char *filter_expression
,
2169 struct lttng_filter_bytecode
*filter
,
2170 struct lttng_event_exclusion
*exclusion
,
2171 int wpipe
, bool internal_event
)
2173 int ret
= 0, channel_created
= 0;
2174 struct lttng_channel
*attr
= NULL
;
2178 assert(channel_name
);
2180 /* If we have a filter, we must have its filter expression */
2181 assert(!(!!filter_expression
^ !!filter
));
2183 /* Normalize event name as a globbing pattern */
2184 strutils_normalize_star_glob_pattern(event
->name
);
2186 /* Normalize exclusion names as globbing patterns */
2190 for (i
= 0; i
< exclusion
->count
; i
++) {
2191 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2193 strutils_normalize_star_glob_pattern(name
);
2197 DBG("Enable event command for event \'%s\'", event
->name
);
2201 switch (domain
->type
) {
2202 case LTTNG_DOMAIN_KERNEL
:
2204 struct ltt_kernel_channel
*kchan
;
2207 * If a non-default channel has been created in the
2208 * session, explicitely require that -c chan_name needs
2211 if (session
->kernel_session
->has_non_default_channel
2212 && channel_name
[0] == '\0') {
2213 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2217 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2218 session
->kernel_session
);
2219 if (kchan
== NULL
) {
2220 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2221 LTTNG_BUFFER_GLOBAL
);
2223 ret
= LTTNG_ERR_FATAL
;
2226 if (lttng_strncpy(attr
->name
, channel_name
,
2227 sizeof(attr
->name
))) {
2228 ret
= LTTNG_ERR_INVALID
;
2232 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2233 if (ret
!= LTTNG_OK
) {
2236 channel_created
= 1;
2239 /* Get the newly created kernel channel pointer */
2240 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2241 session
->kernel_session
);
2242 if (kchan
== NULL
) {
2243 /* This sould not happen... */
2244 ret
= LTTNG_ERR_FATAL
;
2248 switch (event
->type
) {
2249 case LTTNG_EVENT_ALL
:
2251 char *filter_expression_a
= NULL
;
2252 struct lttng_filter_bytecode
*filter_a
= NULL
;
2255 * We need to duplicate filter_expression and filter,
2256 * because ownership is passed to first enable
2259 if (filter_expression
) {
2260 filter_expression_a
= strdup(filter_expression
);
2261 if (!filter_expression_a
) {
2262 ret
= LTTNG_ERR_FATAL
;
2267 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2269 free(filter_expression_a
);
2270 ret
= LTTNG_ERR_FATAL
;
2273 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2275 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2276 ret
= event_kernel_enable_event(kchan
, event
,
2277 filter_expression
, filter
);
2278 /* We have passed ownership */
2279 filter_expression
= NULL
;
2281 if (ret
!= LTTNG_OK
) {
2282 if (channel_created
) {
2283 /* Let's not leak a useless channel. */
2284 kernel_destroy_channel(kchan
);
2286 free(filter_expression_a
);
2290 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2291 ret
= event_kernel_enable_event(kchan
, event
,
2292 filter_expression_a
, filter_a
);
2293 /* We have passed ownership */
2294 filter_expression_a
= NULL
;
2296 if (ret
!= LTTNG_OK
) {
2301 case LTTNG_EVENT_PROBE
:
2302 case LTTNG_EVENT_USERSPACE_PROBE
:
2303 case LTTNG_EVENT_FUNCTION
:
2304 case LTTNG_EVENT_FUNCTION_ENTRY
:
2305 case LTTNG_EVENT_TRACEPOINT
:
2306 ret
= event_kernel_enable_event(kchan
, event
,
2307 filter_expression
, filter
);
2308 /* We have passed ownership */
2309 filter_expression
= NULL
;
2311 if (ret
!= LTTNG_OK
) {
2312 if (channel_created
) {
2313 /* Let's not leak a useless channel. */
2314 kernel_destroy_channel(kchan
);
2319 case LTTNG_EVENT_SYSCALL
:
2320 ret
= event_kernel_enable_event(kchan
, event
,
2321 filter_expression
, filter
);
2322 /* We have passed ownership */
2323 filter_expression
= NULL
;
2325 if (ret
!= LTTNG_OK
) {
2330 ret
= LTTNG_ERR_UNK
;
2334 kernel_wait_quiescent();
2337 case LTTNG_DOMAIN_UST
:
2339 struct ltt_ust_channel
*uchan
;
2340 struct ltt_ust_session
*usess
= session
->ust_session
;
2345 * If a non-default channel has been created in the
2346 * session, explicitely require that -c chan_name needs
2349 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2350 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2354 /* Get channel from global UST domain */
2355 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2357 if (uchan
== NULL
) {
2358 /* Create default channel */
2359 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2360 usess
->buffer_type
);
2362 ret
= LTTNG_ERR_FATAL
;
2365 if (lttng_strncpy(attr
->name
, channel_name
,
2366 sizeof(attr
->name
))) {
2367 ret
= LTTNG_ERR_INVALID
;
2371 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2372 if (ret
!= LTTNG_OK
) {
2376 /* Get the newly created channel reference back */
2377 uchan
= trace_ust_find_channel_by_name(
2378 usess
->domain_global
.channels
, channel_name
);
2382 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2384 * Don't allow users to add UST events to channels which
2385 * are assigned to a userspace subdomain (JUL, Log4J,
2388 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2392 if (!internal_event
) {
2394 * Ensure the event name is not reserved for internal
2397 ret
= validate_ust_event_name(event
->name
);
2399 WARN("Userspace event name %s failed validation.",
2401 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2406 /* At this point, the session and channel exist on the tracer */
2407 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2408 filter_expression
, filter
, exclusion
,
2410 /* We have passed ownership */
2411 filter_expression
= NULL
;
2414 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2415 goto already_enabled
;
2416 } else if (ret
!= LTTNG_OK
) {
2421 case LTTNG_DOMAIN_LOG4J
:
2422 case LTTNG_DOMAIN_JUL
:
2423 case LTTNG_DOMAIN_PYTHON
:
2425 const char *default_event_name
, *default_chan_name
;
2427 struct lttng_event uevent
;
2428 struct lttng_domain tmp_dom
;
2429 struct ltt_ust_session
*usess
= session
->ust_session
;
2433 if (!agent_tracing_is_enabled()) {
2434 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2435 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2439 agt
= trace_ust_find_agent(usess
, domain
->type
);
2441 agt
= agent_create(domain
->type
);
2443 ret
= LTTNG_ERR_NOMEM
;
2446 agent_add(agt
, usess
->agents
);
2449 /* Create the default tracepoint. */
2450 memset(&uevent
, 0, sizeof(uevent
));
2451 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2452 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2453 default_event_name
= event_get_default_agent_ust_name(
2455 if (!default_event_name
) {
2456 ret
= LTTNG_ERR_FATAL
;
2459 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2460 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2463 * The domain type is changed because we are about to enable the
2464 * default channel and event for the JUL domain that are hardcoded.
2465 * This happens in the UST domain.
2467 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2468 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2470 switch (domain
->type
) {
2471 case LTTNG_DOMAIN_LOG4J
:
2472 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2474 case LTTNG_DOMAIN_JUL
:
2475 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2477 case LTTNG_DOMAIN_PYTHON
:
2478 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2481 /* The switch/case we are in makes this impossible */
2486 char *filter_expression_copy
= NULL
;
2487 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2490 const size_t filter_size
= sizeof(
2491 struct lttng_filter_bytecode
)
2494 filter_copy
= zmalloc(filter_size
);
2496 ret
= LTTNG_ERR_NOMEM
;
2499 memcpy(filter_copy
, filter
, filter_size
);
2501 filter_expression_copy
=
2502 strdup(filter_expression
);
2503 if (!filter_expression
) {
2504 ret
= LTTNG_ERR_NOMEM
;
2507 if (!filter_expression_copy
|| !filter_copy
) {
2508 free(filter_expression_copy
);
2514 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2515 (char *) default_chan_name
,
2516 &uevent
, filter_expression_copy
,
2517 filter_copy
, NULL
, wpipe
);
2520 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2521 goto already_enabled
;
2522 } else if (ret
!= LTTNG_OK
) {
2526 /* The wild card * means that everything should be enabled. */
2527 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2528 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2531 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2535 filter_expression
= NULL
;
2536 if (ret
!= LTTNG_OK
) {
2543 ret
= LTTNG_ERR_UND
;
2551 free(filter_expression
);
2554 channel_attr_destroy(attr
);
2560 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2561 * We own filter, exclusion, and filter_expression.
2563 int cmd_enable_event(struct ltt_session
*session
,
2564 const struct lttng_domain
*domain
,
2565 char *channel_name
, struct lttng_event
*event
,
2566 char *filter_expression
,
2567 struct lttng_filter_bytecode
*filter
,
2568 struct lttng_event_exclusion
*exclusion
,
2571 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2572 filter_expression
, filter
, exclusion
, wpipe
, false);
2576 * Enable an event which is internal to LTTng. An internal should
2577 * never be made visible to clients and are immune to checks such as
2580 static int cmd_enable_event_internal(struct ltt_session
*session
,
2581 const struct lttng_domain
*domain
,
2582 char *channel_name
, struct lttng_event
*event
,
2583 char *filter_expression
,
2584 struct lttng_filter_bytecode
*filter
,
2585 struct lttng_event_exclusion
*exclusion
,
2588 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2589 filter_expression
, filter
, exclusion
, wpipe
, true);
2593 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2595 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2596 struct lttng_event
**events
)
2599 ssize_t nb_events
= 0;
2602 case LTTNG_DOMAIN_KERNEL
:
2603 nb_events
= kernel_list_events(events
);
2604 if (nb_events
< 0) {
2605 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2609 case LTTNG_DOMAIN_UST
:
2610 nb_events
= ust_app_list_events(events
);
2611 if (nb_events
< 0) {
2612 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2616 case LTTNG_DOMAIN_LOG4J
:
2617 case LTTNG_DOMAIN_JUL
:
2618 case LTTNG_DOMAIN_PYTHON
:
2619 nb_events
= agent_list_events(events
, domain
);
2620 if (nb_events
< 0) {
2621 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2626 ret
= LTTNG_ERR_UND
;
2633 /* Return negative value to differentiate return code */
2638 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2640 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2641 struct lttng_event_field
**fields
)
2644 ssize_t nb_fields
= 0;
2647 case LTTNG_DOMAIN_UST
:
2648 nb_fields
= ust_app_list_event_fields(fields
);
2649 if (nb_fields
< 0) {
2650 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2654 case LTTNG_DOMAIN_KERNEL
:
2655 default: /* fall-through */
2656 ret
= LTTNG_ERR_UND
;
2663 /* Return negative value to differentiate return code */
2667 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2669 return syscall_table_list(events
);
2673 * Command LTTNG_START_TRACE processed by the client thread.
2675 * Called with session mutex held.
2677 int cmd_start_trace(struct ltt_session
*session
)
2679 enum lttng_error_code ret
;
2680 unsigned long nb_chan
= 0;
2681 struct ltt_kernel_session
*ksession
;
2682 struct ltt_ust_session
*usess
;
2683 const bool session_rotated_after_last_stop
=
2684 session
->rotated_after_last_stop
;
2685 const bool session_cleared_after_last_stop
=
2686 session
->cleared_after_last_stop
;
2690 /* Ease our life a bit ;) */
2691 ksession
= session
->kernel_session
;
2692 usess
= session
->ust_session
;
2694 /* Is the session already started? */
2695 if (session
->active
) {
2696 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2697 /* Perform nothing */
2701 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2702 !session
->current_trace_chunk
) {
2704 * A rotation was launched while the session was stopped and
2705 * it has not been completed yet. It is not possible to start
2706 * the session since starting the session here would require a
2707 * rotation from "NULL" to a new trace chunk. That rotation
2708 * would overlap with the ongoing rotation, which is not
2711 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2713 ret
= LTTNG_ERR_ROTATION_PENDING
;
2718 * Starting a session without channel is useless since after that it's not
2719 * possible to enable channel thus inform the client.
2721 if (usess
&& usess
->domain_global
.channels
) {
2722 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2725 nb_chan
+= ksession
->channel_count
;
2728 ret
= LTTNG_ERR_NO_CHANNEL
;
2732 session
->active
= 1;
2733 session
->rotated_after_last_stop
= false;
2734 session
->cleared_after_last_stop
= false;
2735 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2736 if (!session
->has_been_started
) {
2737 struct lttng_trace_chunk
*trace_chunk
;
2739 DBG("Creating initial trace chunk of session \"%s\"",
2741 trace_chunk
= session_create_new_trace_chunk(
2742 session
, NULL
, NULL
, NULL
);
2744 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2747 assert(!session
->current_trace_chunk
);
2748 ret
= session_set_trace_chunk(session
, trace_chunk
,
2750 lttng_trace_chunk_put(trace_chunk
);
2752 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2756 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2759 * Rotate existing streams into the new chunk.
2760 * This is a "quiet" rotation has no client has
2761 * explicitly requested this operation.
2763 * There is also no need to wait for the rotation
2764 * to complete as it will happen immediately. No data
2765 * was produced as the session was stopped, so the
2766 * rotation should happen on reception of the command.
2768 ret
= cmd_rotate_session(session
, NULL
, true,
2769 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2770 if (ret
!= LTTNG_OK
) {
2776 /* Kernel tracing */
2777 if (ksession
!= NULL
) {
2778 DBG("Start kernel tracing session %s", session
->name
);
2779 ret
= start_kernel_session(ksession
);
2780 if (ret
!= LTTNG_OK
) {
2785 /* Flag session that trace should start automatically */
2787 int int_ret
= ust_app_start_trace_all(usess
);
2790 ret
= LTTNG_ERR_UST_START_FAIL
;
2796 * Open a packet in every stream of the session to ensure that viewers
2797 * can correctly identify the boundaries of the periods during which
2798 * tracing was active for this session.
2800 ret
= session_open_packets(session
);
2801 if (ret
!= LTTNG_OK
) {
2806 * Clear the flag that indicates that a rotation was done while the
2807 * session was stopped.
2809 session
->rotated_after_last_stop
= false;
2811 if (session
->rotate_timer_period
) {
2812 int int_ret
= timer_session_rotation_schedule_timer_start(
2813 session
, session
->rotate_timer_period
);
2816 ERR("Failed to enable rotate timer");
2817 ret
= LTTNG_ERR_UNK
;
2825 if (ret
== LTTNG_OK
) {
2826 /* Flag this after a successful start. */
2827 session
->has_been_started
|= 1;
2829 session
->active
= 0;
2830 /* Restore initial state on error. */
2831 session
->rotated_after_last_stop
=
2832 session_rotated_after_last_stop
;
2833 session
->cleared_after_last_stop
=
2834 session_cleared_after_last_stop
;
2841 * Command LTTNG_STOP_TRACE processed by the client thread.
2843 int cmd_stop_trace(struct ltt_session
*session
)
2846 struct ltt_kernel_session
*ksession
;
2847 struct ltt_ust_session
*usess
;
2851 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2853 ksession
= session
->kernel_session
;
2854 usess
= session
->ust_session
;
2856 /* Session is not active. Skip everythong and inform the client. */
2857 if (!session
->active
) {
2858 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2862 ret
= stop_kernel_session(ksession
);
2863 if (ret
!= LTTNG_OK
) {
2867 if (usess
&& usess
->active
) {
2868 ret
= ust_app_stop_trace_all(usess
);
2870 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2875 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2877 /* Flag inactive after a successful stop. */
2878 session
->active
= 0;
2886 * Set the base_path of the session only if subdir of a control uris is set.
2887 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2889 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2891 struct lttng_uri
*uris
)
2896 for (i
= 0; i
< nb_uri
; i
++) {
2897 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2898 uris
[i
].subdir
[0] == '\0') {
2899 /* Not interested in these URIs */
2903 if (session
->base_path
!= NULL
) {
2904 free(session
->base_path
);
2905 session
->base_path
= NULL
;
2908 /* Set session base_path */
2909 session
->base_path
= strdup(uris
[i
].subdir
);
2910 if (!session
->base_path
) {
2911 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2912 uris
[i
].subdir
, session
->name
);
2913 ret
= LTTNG_ERR_NOMEM
;
2916 DBG2("Setting base path \"%s\" for session \"%s\"",
2917 session
->base_path
, session
->name
);
2925 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2927 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2928 struct lttng_uri
*uris
)
2931 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2932 struct ltt_ust_session
*usess
= session
->ust_session
;
2938 /* Can't set consumer URI if the session is active. */
2939 if (session
->active
) {
2940 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2945 * Set the session base path if any. This is done inside
2946 * cmd_set_consumer_uri to preserve backward compatibility of the
2947 * previous session creation api vs the session descriptor api.
2949 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2950 if (ret
!= LTTNG_OK
) {
2954 /* Set the "global" consumer URIs */
2955 for (i
= 0; i
< nb_uri
; i
++) {
2956 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2958 if (ret
!= LTTNG_OK
) {
2963 /* Set UST session URIs */
2964 if (session
->ust_session
) {
2965 for (i
= 0; i
< nb_uri
; i
++) {
2966 ret
= add_uri_to_consumer(session
,
2967 session
->ust_session
->consumer
,
2968 &uris
[i
], LTTNG_DOMAIN_UST
);
2969 if (ret
!= LTTNG_OK
) {
2975 /* Set kernel session URIs */
2976 if (session
->kernel_session
) {
2977 for (i
= 0; i
< nb_uri
; i
++) {
2978 ret
= add_uri_to_consumer(session
,
2979 session
->kernel_session
->consumer
,
2980 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2981 if (ret
!= LTTNG_OK
) {
2988 * Make sure to set the session in output mode after we set URI since a
2989 * session can be created without URL (thus flagged in no output mode).
2991 session
->output_traces
= 1;
2993 ksess
->output_traces
= 1;
2997 usess
->output_traces
= 1;
3008 enum lttng_error_code
set_session_output_from_descriptor(
3009 struct ltt_session
*session
,
3010 const struct lttng_session_descriptor
*descriptor
)
3013 enum lttng_error_code ret_code
= LTTNG_OK
;
3014 enum lttng_session_descriptor_type session_type
=
3015 lttng_session_descriptor_get_type(descriptor
);
3016 enum lttng_session_descriptor_output_type output_type
=
3017 lttng_session_descriptor_get_output_type(descriptor
);
3018 struct lttng_uri uris
[2] = {};
3019 size_t uri_count
= 0;
3021 switch (output_type
) {
3022 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3024 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3025 lttng_session_descriptor_get_local_output_uri(descriptor
,
3029 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3030 lttng_session_descriptor_get_network_output_uris(descriptor
,
3031 &uris
[0], &uris
[1]);
3035 ret_code
= LTTNG_ERR_INVALID
;
3039 switch (session_type
) {
3040 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3042 struct snapshot_output
*new_output
= NULL
;
3044 new_output
= snapshot_output_alloc();
3046 ret_code
= LTTNG_ERR_NOMEM
;
3050 ret
= snapshot_output_init_with_uri(session
,
3051 DEFAULT_SNAPSHOT_MAX_SIZE
,
3052 NULL
, uris
, uri_count
, session
->consumer
,
3053 new_output
, &session
->snapshot
);
3055 ret_code
= (ret
== -ENOMEM
) ?
3056 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3057 snapshot_output_destroy(new_output
);
3060 snapshot_add_output(&session
->snapshot
, new_output
);
3063 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3064 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3066 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
3070 ret_code
= LTTNG_ERR_INVALID
;
3078 enum lttng_error_code
cmd_create_session_from_descriptor(
3079 struct lttng_session_descriptor
*descriptor
,
3080 const lttng_sock_cred
*creds
,
3081 const char *home_path
)
3084 enum lttng_error_code ret_code
;
3085 const char *session_name
;
3086 struct ltt_session
*new_session
= NULL
;
3087 enum lttng_session_descriptor_status descriptor_status
;
3089 session_lock_list();
3091 if (*home_path
!= '/') {
3092 ERR("Home path provided by client is not absolute");
3093 ret_code
= LTTNG_ERR_INVALID
;
3098 descriptor_status
= lttng_session_descriptor_get_session_name(
3099 descriptor
, &session_name
);
3100 switch (descriptor_status
) {
3101 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3103 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3104 session_name
= NULL
;
3107 ret_code
= LTTNG_ERR_INVALID
;
3111 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3113 if (ret_code
!= LTTNG_OK
) {
3117 if (!session_name
) {
3118 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3121 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3126 if (!lttng_session_descriptor_is_output_destination_initialized(
3129 * Only include the session's creation time in the output
3130 * destination if the name of the session itself was
3131 * not auto-generated.
3133 ret_code
= lttng_session_descriptor_set_default_output(
3135 session_name
? &new_session
->creation_time
: NULL
,
3137 if (ret_code
!= LTTNG_OK
) {
3141 new_session
->has_user_specified_directory
=
3142 lttng_session_descriptor_has_output_directory(
3146 switch (lttng_session_descriptor_get_type(descriptor
)) {
3147 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3148 new_session
->snapshot_mode
= 1;
3150 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3151 new_session
->live_timer
=
3152 lttng_session_descriptor_live_get_timer_interval(
3159 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3160 if (ret_code
!= LTTNG_OK
) {
3163 new_session
->consumer
->enabled
= 1;
3164 ret_code
= LTTNG_OK
;
3166 /* Release reference provided by the session_create function. */
3167 session_put(new_session
);
3168 if (ret_code
!= LTTNG_OK
&& new_session
) {
3169 /* Release the global reference on error. */
3170 session_destroy(new_session
);
3172 session_unlock_list();
3176 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3177 struct lttng_session_descriptor
**return_descriptor
)
3180 size_t payload_size
;
3181 struct lttng_dynamic_buffer payload
;
3182 struct lttng_buffer_view home_dir_view
;
3183 struct lttng_buffer_view session_descriptor_view
;
3184 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3185 enum lttng_error_code ret_code
;
3187 lttng_dynamic_buffer_init(&payload
);
3188 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3190 ret_code
= LTTNG_ERR_INVALID
;
3193 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3194 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3195 ret_code
= LTTNG_ERR_INVALID
;
3199 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3200 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3201 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3203 ret_code
= LTTNG_ERR_NOMEM
;
3207 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3209 ERR("Reception of session descriptor failed, aborting.");
3210 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3214 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3217 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3218 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3220 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3221 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3223 ret
= lttng_session_descriptor_create_from_buffer(
3224 &session_descriptor_view
, &session_descriptor
);
3226 ERR("Failed to create session descriptor from payload of \"create session\" command");
3227 ret_code
= LTTNG_ERR_INVALID
;
3232 * Sets the descriptor's auto-generated properties (name, output) if
3235 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3237 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3238 if (ret_code
!= LTTNG_OK
) {
3242 ret_code
= LTTNG_OK
;
3243 *return_descriptor
= session_descriptor
;
3244 session_descriptor
= NULL
;
3246 lttng_dynamic_buffer_reset(&payload
);
3247 lttng_session_descriptor_destroy(session_descriptor
);
3252 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3253 void *_reply_context
)
3257 const struct cmd_destroy_session_reply_context
*reply_context
=
3259 struct lttng_dynamic_buffer payload
;
3260 struct lttcomm_session_destroy_command_header cmd_header
;
3261 struct lttng_trace_archive_location
*location
= NULL
;
3262 struct lttcomm_lttng_msg llm
= {
3263 .cmd_type
= LTTNG_DESTROY_SESSION
,
3264 .ret_code
= reply_context
->destruction_status
,
3267 sizeof(struct lttcomm_session_destroy_command_header
),
3270 size_t payload_size_before_location
;
3272 lttng_dynamic_buffer_init(&payload
);
3274 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3276 ERR("Failed to append session destruction message");
3280 cmd_header
.rotation_state
=
3281 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3282 session
->rotation_state
:
3283 LTTNG_ROTATION_STATE_NO_ROTATION
);
3284 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3285 sizeof(cmd_header
));
3287 ERR("Failed to append session destruction command header");
3291 if (!reply_context
->implicit_rotation_on_destroy
) {
3292 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3296 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3297 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3302 location
= session_get_trace_archive_location(session
);
3304 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3309 payload_size_before_location
= payload
.size
;
3310 comm_ret
= lttng_trace_archive_location_serialize(location
,
3313 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3317 /* Update the message to indicate the location's length. */
3318 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3319 payload
.size
- payload_size_before_location
;
3321 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3322 payload
.data
, payload
.size
);
3323 if (comm_ret
!= (ssize_t
) payload
.size
) {
3324 ERR("Failed to send result of the destruction of session \"%s\" to client",
3328 ret
= close(reply_context
->reply_sock_fd
);
3330 PERROR("Failed to close client socket in deferred session destroy reply");
3332 lttng_dynamic_buffer_reset(&payload
);
3333 free(_reply_context
);
3337 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3339 * Called with session lock held.
3341 int cmd_destroy_session(struct ltt_session
*session
,
3342 struct notification_thread_handle
*notification_thread_handle
,
3346 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3347 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3350 reply_context
= zmalloc(sizeof(*reply_context
));
3351 if (!reply_context
) {
3352 ret
= LTTNG_ERR_NOMEM
;
3355 reply_context
->reply_sock_fd
= *sock_fd
;
3361 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3363 if (session
->active
) {
3364 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3366 ret
= cmd_stop_trace(session
);
3367 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3368 /* Carry on with the destruction of the session. */
3369 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3370 session
->name
, lttng_strerror(-ret
));
3371 destruction_last_error
= ret
;
3375 if (session
->rotation_schedule_timer_enabled
) {
3376 if (timer_session_rotation_schedule_timer_stop(
3378 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3380 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3384 if (session
->rotate_size
) {
3385 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3386 session
->rotate_size
= 0;
3389 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3391 * Perform a last rotation on destruction if rotations have
3392 * occurred during the session's lifetime.
3394 ret
= cmd_rotate_session(session
, NULL
, false,
3395 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3396 if (ret
!= LTTNG_OK
) {
3397 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3398 session
->name
, lttng_strerror(-ret
));
3399 destruction_last_error
= -ret
;
3401 if (reply_context
) {
3402 reply_context
->implicit_rotation_on_destroy
= true;
3404 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3406 * The user has not triggered a session rotation. However, to
3407 * ensure all data has been consumed, the session is rotated
3408 * to a 'null' trace chunk before it is destroyed.
3410 * This is a "quiet" rotation meaning that no notification is
3411 * emitted and no renaming of the current trace chunk takes
3414 ret
= cmd_rotate_session(session
, NULL
, true,
3415 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3417 * Rotation operations may not be supported by the kernel
3418 * tracer. Hence, do not consider this implicit rotation as
3419 * a session destruction error. The library has already stopped
3420 * the session and waited for pending data; there is nothing
3421 * left to do but complete the destruction of the session.
3423 if (ret
!= LTTNG_OK
&&
3424 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3425 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3426 session
->name
, lttng_strerror(ret
));
3427 destruction_last_error
= -ret
;
3431 if (session
->shm_path
[0]) {
3433 * When a session is created with an explicit shm_path,
3434 * the consumer daemon will create its shared memory files
3435 * at that location and will *not* unlink them. This is normal
3436 * as the intention of that feature is to make it possible
3437 * to retrieve the content of those files should a crash occur.
3439 * To ensure the content of those files can be used, the
3440 * sessiond daemon will replicate the content of the metadata
3441 * cache in a metadata file.
3443 * On clean-up, it is expected that the consumer daemon will
3444 * unlink the shared memory files and that the session daemon
3445 * will unlink the metadata file. Then, the session's directory
3446 * in the shm path can be removed.
3448 * Unfortunately, a flaw in the design of the sessiond's and
3449 * consumerd's tear down of channels makes it impossible to
3450 * determine when the sessiond _and_ the consumerd have both
3451 * destroyed their representation of a channel. For one, the
3452 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3453 * callbacks in both daemons.
3455 * However, it is also impossible for the sessiond to know when
3456 * the consumer daemon is done destroying its channel(s) since
3457 * it occurs as a reaction to the closing of the channel's file
3458 * descriptor. There is no resulting communication initiated
3459 * from the consumerd to the sessiond to confirm that the
3460 * operation is completed (and was successful).
3462 * Until this is all fixed, the session daemon checks for the
3463 * removal of the session's shm path which makes it possible
3464 * to safely advertise a session as having been destroyed.
3466 * Prior to this fix, it was not possible to reliably save
3467 * a session making use of the --shm-path option, destroy it,
3468 * and load it again. This is because the creation of the
3469 * session would fail upon seeing the session's shm path
3470 * already in existence.
3472 * Note that none of the error paths in the check for the
3473 * directory's existence return an error. This is normal
3474 * as there isn't much that can be done. The session will
3475 * be destroyed properly, except that we can't offer the
3476 * guarantee that the same session can be re-created.
3478 current_completion_handler
= &destroy_completion_handler
.handler
;
3479 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3481 sizeof(destroy_completion_handler
.shm_path
));
3486 * The session is destroyed. However, note that the command context
3487 * still holds a reference to the session, thus delaying its destruction
3488 * _at least_ up to the point when that reference is released.
3490 session_destroy(session
);
3491 if (reply_context
) {
3492 reply_context
->destruction_status
= destruction_last_error
;
3493 ret
= session_add_destroy_notifier(session
,
3494 cmd_destroy_session_reply
,
3495 (void *) reply_context
);
3497 ret
= LTTNG_ERR_FATAL
;
3509 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3511 int cmd_register_consumer(struct ltt_session
*session
,
3512 enum lttng_domain_type domain
, const char *sock_path
,
3513 struct consumer_data
*cdata
)
3516 struct consumer_socket
*socket
= NULL
;
3523 case LTTNG_DOMAIN_KERNEL
:
3525 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3529 /* Can't register a consumer if there is already one */
3530 if (ksess
->consumer_fds_sent
!= 0) {
3531 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3535 sock
= lttcomm_connect_unix_sock(sock_path
);
3537 ret
= LTTNG_ERR_CONNECT_FAIL
;
3540 cdata
->cmd_sock
= sock
;
3542 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3543 if (socket
== NULL
) {
3546 PERROR("close register consumer");
3548 cdata
->cmd_sock
= -1;
3549 ret
= LTTNG_ERR_FATAL
;
3553 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3554 if (socket
->lock
== NULL
) {
3555 PERROR("zmalloc pthread mutex");
3556 ret
= LTTNG_ERR_FATAL
;
3559 pthread_mutex_init(socket
->lock
, NULL
);
3560 socket
->registered
= 1;
3563 consumer_add_socket(socket
, ksess
->consumer
);
3566 pthread_mutex_lock(&cdata
->pid_mutex
);
3568 pthread_mutex_unlock(&cdata
->pid_mutex
);
3573 /* TODO: Userspace tracing */
3574 ret
= LTTNG_ERR_UND
;
3582 consumer_destroy_socket(socket
);
3588 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3590 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3591 struct lttng_domain
**domains
)
3596 struct lttng_ht_iter iter
;
3598 if (session
->kernel_session
!= NULL
) {
3599 DBG3("Listing domains found kernel domain");
3603 if (session
->ust_session
!= NULL
) {
3604 DBG3("Listing domains found UST global domain");
3608 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3610 if (agt
->being_used
) {
3621 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3622 if (*domains
== NULL
) {
3623 ret
= LTTNG_ERR_FATAL
;
3627 if (session
->kernel_session
!= NULL
) {
3628 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3630 /* Kernel session buffer type is always GLOBAL */
3631 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3636 if (session
->ust_session
!= NULL
) {
3637 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3638 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3642 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3644 if (agt
->being_used
) {
3645 (*domains
)[index
].type
= agt
->domain
;
3646 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3656 /* Return negative value to differentiate return code */
3662 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3664 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3665 struct ltt_session
*session
, struct lttng_channel
**channels
)
3667 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3670 case LTTNG_DOMAIN_KERNEL
:
3671 if (session
->kernel_session
!= NULL
) {
3672 nb_chan
= session
->kernel_session
->channel_count
;
3674 DBG3("Number of kernel channels %zd", nb_chan
);
3676 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3680 case LTTNG_DOMAIN_UST
:
3681 if (session
->ust_session
!= NULL
) {
3683 nb_chan
= lttng_ht_get_count(
3684 session
->ust_session
->domain_global
.channels
);
3687 DBG3("Number of UST global channels %zd", nb_chan
);
3689 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3694 ret
= -LTTNG_ERR_UND
;
3699 const size_t channel_size
= sizeof(struct lttng_channel
) +
3700 sizeof(struct lttng_channel_extended
);
3701 struct lttng_channel_extended
*channel_exts
;
3703 payload_size
= nb_chan
* channel_size
;
3704 *channels
= zmalloc(payload_size
);
3705 if (*channels
== NULL
) {
3706 ret
= -LTTNG_ERR_FATAL
;
3710 channel_exts
= ((void *) *channels
) +
3711 (nb_chan
* sizeof(struct lttng_channel
));
3712 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3713 if (ret
!= LTTNG_OK
) {
3728 * Command LTTNG_LIST_EVENTS processed by the client thread.
3730 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3731 struct ltt_session
*session
, char *channel_name
,
3732 struct lttng_event
**events
, size_t *total_size
)
3735 ssize_t nb_event
= 0;
3738 case LTTNG_DOMAIN_KERNEL
:
3739 if (session
->kernel_session
!= NULL
) {
3740 nb_event
= list_lttng_kernel_events(channel_name
,
3741 session
->kernel_session
, events
,
3745 case LTTNG_DOMAIN_UST
:
3747 if (session
->ust_session
!= NULL
) {
3748 nb_event
= list_lttng_ust_global_events(channel_name
,
3749 &session
->ust_session
->domain_global
, events
,
3754 case LTTNG_DOMAIN_LOG4J
:
3755 case LTTNG_DOMAIN_JUL
:
3756 case LTTNG_DOMAIN_PYTHON
:
3757 if (session
->ust_session
) {
3758 struct lttng_ht_iter iter
;
3762 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3763 &iter
.iter
, agt
, node
.node
) {
3764 if (agt
->domain
== domain
) {
3765 nb_event
= list_lttng_agent_events(
3775 ret
= LTTNG_ERR_UND
;
3782 /* Return negative value to differentiate return code */
3787 * Using the session list, filled a lttng_session array to send back to the
3788 * client for session listing.
3790 * The session list lock MUST be acquired before calling this function. Use
3791 * session_lock_list() and session_unlock_list().
3793 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3794 size_t session_count
, uid_t uid
, gid_t gid
)
3798 struct ltt_session
*session
;
3799 struct ltt_session_list
*list
= session_get_list();
3800 struct lttng_session_extended
*extended
=
3801 (typeof(extended
)) (&sessions
[session_count
]);
3803 DBG("Getting all available session for UID %d GID %d",
3806 * Iterate over session list and append data after the control struct in
3809 cds_list_for_each_entry(session
, &list
->head
, list
) {
3810 if (!session_get(session
)) {
3814 * Only list the sessions the user can control.
3816 if (!session_access_ok(session
, uid
, gid
) ||
3817 session
->destroyed
) {
3818 session_put(session
);
3822 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3823 struct ltt_ust_session
*usess
= session
->ust_session
;
3825 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3826 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3827 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3828 ret
= build_network_session_path(sessions
[i
].path
,
3829 sizeof(sessions
[i
].path
), session
);
3831 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3832 session
->consumer
->dst
.session_root_path
);
3835 PERROR("snprintf session path");
3836 session_put(session
);
3840 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3841 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3842 sessions
[i
].enabled
= session
->active
;
3843 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3844 sessions
[i
].live_timer_interval
= session
->live_timer
;
3845 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3846 extended
[i
].creation_time
.is_set
= 1;
3848 session_put(session
);
3853 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3854 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3856 int cmd_data_pending(struct ltt_session
*session
)
3859 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3860 struct ltt_ust_session
*usess
= session
->ust_session
;
3864 DBG("Data pending for session %s", session
->name
);
3866 /* Session MUST be stopped to ask for data availability. */
3867 if (session
->active
) {
3868 ret
= LTTNG_ERR_SESSION_STARTED
;
3872 * If stopped, just make sure we've started before else the above call
3873 * will always send that there is data pending.
3875 * The consumer assumes that when the data pending command is received,
3876 * the trace has been started before or else no output data is written
3877 * by the streams which is a condition for data pending. So, this is
3878 * *VERY* important that we don't ask the consumer before a start
3881 if (!session
->has_been_started
) {
3887 /* A rotation is still pending, we have to wait. */
3888 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3889 DBG("Rotate still pending for session %s", session
->name
);
3894 if (ksess
&& ksess
->consumer
) {
3895 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3897 /* Data is still being extracted for the kernel. */
3902 if (usess
&& usess
->consumer
) {
3903 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3905 /* Data is still being extracted for the kernel. */
3910 /* Data is ready to be read by a viewer */
3918 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3920 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3922 int cmd_snapshot_add_output(struct ltt_session
*session
,
3923 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3926 struct snapshot_output
*new_output
;
3931 DBG("Cmd snapshot add output for session %s", session
->name
);
3934 * Can't create an output if the session is not set in no-output mode.
3936 if (session
->output_traces
) {
3937 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3941 if (session
->has_non_mmap_channel
) {
3942 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3946 /* Only one output is allowed until we have the "tee" feature. */
3947 if (session
->snapshot
.nb_output
== 1) {
3948 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3952 new_output
= snapshot_output_alloc();
3954 ret
= LTTNG_ERR_NOMEM
;
3958 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3959 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3960 &session
->snapshot
);
3962 if (ret
== -ENOMEM
) {
3963 ret
= LTTNG_ERR_NOMEM
;
3965 ret
= LTTNG_ERR_INVALID
;
3971 snapshot_add_output(&session
->snapshot
, new_output
);
3973 *id
= new_output
->id
;
3980 snapshot_output_destroy(new_output
);
3986 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3988 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3990 int cmd_snapshot_del_output(struct ltt_session
*session
,
3991 const struct lttng_snapshot_output
*output
)
3994 struct snapshot_output
*sout
= NULL
;
4002 * Permission denied to create an output if the session is not
4003 * set in no output mode.
4005 if (session
->output_traces
) {
4006 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4011 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4013 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4014 } else if (*output
->name
!= '\0') {
4015 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4017 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4020 ret
= LTTNG_ERR_INVALID
;
4024 snapshot_delete_output(&session
->snapshot
, sout
);
4025 snapshot_output_destroy(sout
);
4034 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4036 * If no output is available, outputs is untouched and 0 is returned.
4038 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4040 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4041 struct lttng_snapshot_output
**outputs
)
4044 struct lttng_snapshot_output
*list
= NULL
;
4045 struct lttng_ht_iter iter
;
4046 struct snapshot_output
*output
;
4051 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4054 * Permission denied to create an output if the session is not
4055 * set in no output mode.
4057 if (session
->output_traces
) {
4058 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4062 if (session
->snapshot
.nb_output
== 0) {
4067 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4069 ret
= -LTTNG_ERR_NOMEM
;
4073 /* Copy list from session to the new list object. */
4075 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4076 output
, node
.node
) {
4077 assert(output
->consumer
);
4078 list
[idx
].id
= output
->id
;
4079 list
[idx
].max_size
= output
->max_size
;
4080 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4081 sizeof(list
[idx
].name
))) {
4082 ret
= -LTTNG_ERR_INVALID
;
4085 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4086 if (lttng_strncpy(list
[idx
].ctrl_url
,
4087 output
->consumer
->dst
.session_root_path
,
4088 sizeof(list
[idx
].ctrl_url
))) {
4089 ret
= -LTTNG_ERR_INVALID
;
4094 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4095 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4097 ret
= -LTTNG_ERR_NOMEM
;
4102 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4103 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4105 ret
= -LTTNG_ERR_NOMEM
;
4114 ret
= session
->snapshot
.nb_output
;
4123 * Check if we can regenerate the metadata for this session.
4124 * Only kernel, UST per-uid and non-live sessions are supported.
4126 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4129 int check_regenerate_metadata_support(struct ltt_session
*session
)
4135 if (session
->live_timer
!= 0) {
4136 ret
= LTTNG_ERR_LIVE_SESSION
;
4139 if (!session
->active
) {
4140 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4143 if (session
->ust_session
) {
4144 switch (session
->ust_session
->buffer_type
) {
4145 case LTTNG_BUFFER_PER_UID
:
4147 case LTTNG_BUFFER_PER_PID
:
4148 ret
= LTTNG_ERR_PER_PID_SESSION
;
4152 ret
= LTTNG_ERR_UNK
;
4156 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4157 session
->consumer
->relay_minor_version
< 8) {
4158 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4168 int clear_metadata_file(int fd
)
4173 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4174 if (lseek_ret
< 0) {
4180 ret
= ftruncate(fd
, 0);
4182 PERROR("ftruncate");
4191 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4194 struct buffer_reg_uid
*uid_reg
= NULL
;
4195 struct buffer_reg_session
*session_reg
= NULL
;
4198 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4199 struct ust_registry_session
*registry
;
4200 struct ust_registry_channel
*chan
;
4201 struct lttng_ht_iter iter_chan
;
4203 session_reg
= uid_reg
->registry
;
4204 registry
= session_reg
->reg
.ust
;
4206 pthread_mutex_lock(®istry
->lock
);
4207 registry
->metadata_len_sent
= 0;
4208 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4209 registry
->metadata_len
= 0;
4210 registry
->metadata_version
++;
4211 if (registry
->metadata_fd
> 0) {
4212 /* Clear the metadata file's content. */
4213 ret
= clear_metadata_file(registry
->metadata_fd
);
4215 pthread_mutex_unlock(®istry
->lock
);
4220 ret
= ust_metadata_session_statedump(registry
, NULL
,
4221 registry
->major
, registry
->minor
);
4223 pthread_mutex_unlock(®istry
->lock
);
4224 ERR("Failed to generate session metadata (err = %d)",
4228 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4230 struct ust_registry_event
*event
;
4231 struct lttng_ht_iter iter_event
;
4233 ret
= ust_metadata_channel_statedump(registry
, chan
);
4235 pthread_mutex_unlock(®istry
->lock
);
4236 ERR("Failed to generate channel metadata "
4240 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4242 ret
= ust_metadata_event_statedump(registry
,
4245 pthread_mutex_unlock(®istry
->lock
);
4246 ERR("Failed to generate event metadata "
4252 pthread_mutex_unlock(®istry
->lock
);
4261 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4263 * Ask the consumer to truncate the existing metadata file(s) and
4264 * then regenerate the metadata. Live and per-pid sessions are not
4265 * supported and return an error.
4267 * Return 0 on success or else a LTTNG_ERR code.
4269 int cmd_regenerate_metadata(struct ltt_session
*session
)
4275 ret
= check_regenerate_metadata_support(session
);
4280 if (session
->kernel_session
) {
4281 ret
= kernctl_session_regenerate_metadata(
4282 session
->kernel_session
->fd
);
4284 ERR("Failed to regenerate the kernel metadata");
4289 if (session
->ust_session
) {
4290 ret
= ust_regenerate_metadata(session
->ust_session
);
4292 ERR("Failed to regenerate the UST metadata");
4296 DBG("Cmd metadata regenerate for session %s", session
->name
);
4304 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4306 * Ask the tracer to regenerate a new statedump.
4308 * Return 0 on success or else a LTTNG_ERR code.
4310 int cmd_regenerate_statedump(struct ltt_session
*session
)
4316 if (!session
->active
) {
4317 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4321 if (session
->kernel_session
) {
4322 ret
= kernctl_session_regenerate_statedump(
4323 session
->kernel_session
->fd
);
4325 * Currently, the statedump in kernel can only fail if out
4329 if (ret
== -ENOMEM
) {
4330 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4332 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4334 ERR("Failed to regenerate the kernel statedump");
4339 if (session
->ust_session
) {
4340 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4342 * Currently, the statedump in UST always returns 0.
4345 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4346 ERR("Failed to regenerate the UST statedump");
4350 DBG("Cmd regenerate statedump for session %s", session
->name
);
4357 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4358 struct notification_thread_handle
*notification_thread
)
4362 ssize_t sock_recv_len
;
4363 struct lttng_trigger
*trigger
= NULL
;
4364 struct lttng_payload trigger_payload
;
4366 lttng_payload_init(&trigger_payload
);
4367 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
4368 ret
= lttng_dynamic_buffer_set_size(
4369 &trigger_payload
.buffer
, trigger_len
);
4371 ret
= LTTNG_ERR_NOMEM
;
4375 sock_recv_len
= lttcomm_recv_unix_sock(
4376 sock
, trigger_payload
.buffer
.data
, trigger_len
);
4377 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4378 ERR("Failed to receive \"register trigger\" command payload");
4379 /* TODO: should this be a new error enum ? */
4380 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4385 struct lttng_payload_view view
=
4386 lttng_payload_view_from_payload(
4387 &trigger_payload
, 0, -1);
4389 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
4391 ERR("Invalid trigger payload received in \"register trigger\" command");
4392 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4397 ret
= notification_thread_command_register_trigger(notification_thread
,
4399 /* Ownership of trigger was transferred. */
4402 lttng_trigger_destroy(trigger
);
4403 lttng_payload_reset(&trigger_payload
);
4407 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4408 struct notification_thread_handle
*notification_thread
)
4412 ssize_t sock_recv_len
;
4413 struct lttng_trigger
*trigger
= NULL
;
4414 struct lttng_payload trigger_payload
;
4416 lttng_payload_init(&trigger_payload
);
4417 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
4418 ret
= lttng_dynamic_buffer_set_size(
4419 &trigger_payload
.buffer
, trigger_len
);
4421 ret
= LTTNG_ERR_NOMEM
;
4425 sock_recv_len
= lttcomm_recv_unix_sock(
4426 sock
, trigger_payload
.buffer
.data
, trigger_len
);
4427 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4428 ERR("Failed to receive \"unregister trigger\" command payload");
4429 /* TODO: should this be a new error enum ? */
4430 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4435 struct lttng_payload_view view
=
4436 lttng_payload_view_from_payload(
4437 &trigger_payload
, 0, -1);
4439 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
4441 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4442 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4447 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4450 lttng_trigger_destroy(trigger
);
4451 lttng_payload_reset(&trigger_payload
);
4456 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4457 * snapshot output is *not* set with a remote destination.
4459 * Return LTTNG_OK on success or a LTTNG_ERR code.
4461 static enum lttng_error_code
set_relayd_for_snapshot(
4462 struct consumer_output
*output
,
4463 const struct ltt_session
*session
)
4465 enum lttng_error_code status
= LTTNG_OK
;
4466 struct lttng_ht_iter iter
;
4467 struct consumer_socket
*socket
;
4468 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4469 const char *base_path
;
4474 DBG2("Set relayd object from snapshot output");
4476 if (session
->current_trace_chunk
) {
4477 enum lttng_trace_chunk_status chunk_status
=
4478 lttng_trace_chunk_get_id(
4479 session
->current_trace_chunk
,
4480 ¤t_chunk_id
.value
);
4482 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4483 current_chunk_id
.is_set
= true;
4485 ERR("Failed to get current trace chunk id");
4486 status
= LTTNG_ERR_UNK
;
4491 /* Ignore if snapshot consumer output is not network. */
4492 if (output
->type
!= CONSUMER_DST_NET
) {
4497 * The snapshot record URI base path overrides the session
4500 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4501 base_path
= output
->dst
.net
.control
.subdir
;
4503 base_path
= session
->base_path
;
4507 * For each consumer socket, create and send the relayd object of the
4511 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4512 socket
, node
.node
) {
4513 pthread_mutex_lock(socket
->lock
);
4514 status
= send_consumer_relayd_sockets(0, session
->id
,
4516 session
->name
, session
->hostname
,
4518 session
->live_timer
,
4519 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4520 session
->creation_time
,
4521 session
->name_contains_creation_time
);
4522 pthread_mutex_unlock(socket
->lock
);
4523 if (status
!= LTTNG_OK
) {
4535 * Record a kernel snapshot.
4537 * Return LTTNG_OK on success or a LTTNG_ERR code.
4539 static enum lttng_error_code
record_kernel_snapshot(
4540 struct ltt_kernel_session
*ksess
,
4541 const struct consumer_output
*output
,
4542 const struct ltt_session
*session
,
4543 int wait
, uint64_t nb_packets_per_stream
)
4545 enum lttng_error_code status
;
4551 status
= kernel_snapshot_record(
4552 ksess
, output
, wait
, nb_packets_per_stream
);
4557 * Record a UST snapshot.
4559 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4561 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4562 const struct consumer_output
*output
,
4563 const struct ltt_session
*session
,
4564 int wait
, uint64_t nb_packets_per_stream
)
4566 enum lttng_error_code status
;
4572 status
= ust_app_snapshot_record(
4573 usess
, output
, wait
, nb_packets_per_stream
);
4578 uint64_t get_session_size_one_more_packet_per_stream(
4579 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4581 uint64_t tot_size
= 0;
4583 if (session
->kernel_session
) {
4584 struct ltt_kernel_channel
*chan
;
4585 const struct ltt_kernel_session
*ksess
=
4586 session
->kernel_session
;
4588 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4589 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4591 * Don't take channel into account if we
4592 * already grab all its packets.
4596 tot_size
+= chan
->channel
->attr
.subbuf_size
4597 * chan
->stream_count
;
4601 if (session
->ust_session
) {
4602 const struct ltt_ust_session
*usess
= session
->ust_session
;
4604 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4612 * Calculate the number of packets we can grab from each stream that
4613 * fits within the overall snapshot max size.
4615 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4616 * the number of packets per stream.
4618 * TODO: this approach is not perfect: we consider the worse case
4619 * (packet filling the sub-buffers) as an upper bound, but we could do
4620 * better if we do this calculation while we actually grab the packet
4621 * content: we would know how much padding we don't actually store into
4624 * This algorithm is currently bounded by the number of packets per
4627 * Since we call this algorithm before actually grabbing the data, it's
4628 * an approximation: for instance, applications could appear/disappear
4629 * in between this call and actually grabbing data.
4632 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4636 uint64_t cur_nb_packets
= 0;
4639 return 0; /* Infinite */
4642 size_left
= max_size
;
4644 uint64_t one_more_packet_tot_size
;
4646 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4647 session
, cur_nb_packets
);
4648 if (!one_more_packet_tot_size
) {
4649 /* We are already grabbing all packets. */
4652 size_left
-= one_more_packet_tot_size
;
4653 if (size_left
< 0) {
4658 if (!cur_nb_packets
&& size_left
!= max_size
) {
4659 /* Not enough room to grab one packet of each stream, error. */
4662 return cur_nb_packets
;
4666 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4667 const struct snapshot_output
*snapshot_output
, int wait
)
4669 int64_t nb_packets_per_stream
;
4670 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4672 enum lttng_error_code ret_code
= LTTNG_OK
;
4673 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4674 struct consumer_output
*original_ust_consumer_output
= NULL
;
4675 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4676 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4677 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4679 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4681 snapshot_output
->name
,
4682 snapshot_output
->datetime
,
4683 snapshot_output
->nb_snapshot
);
4684 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4685 ERR("Failed to format snapshot name");
4686 ret_code
= LTTNG_ERR_INVALID
;
4689 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4690 snapshot_output
->name
, session
->name
,
4691 snapshot_chunk_name
);
4692 if (!session
->kernel_session
&& !session
->ust_session
) {
4693 ERR("Failed to record snapshot as no channels exist");
4694 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4698 if (session
->kernel_session
) {
4699 original_kernel_consumer_output
=
4700 session
->kernel_session
->consumer
;
4701 snapshot_kernel_consumer_output
=
4702 consumer_copy_output(snapshot_output
->consumer
);
4703 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
4704 snapshot_chunk_name
);
4705 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4706 original_kernel_consumer_output
);
4708 ERR("Failed to copy consumer sockets from snapshot output configuration");
4709 ret_code
= LTTNG_ERR_NOMEM
;
4712 ret_code
= set_relayd_for_snapshot(
4713 snapshot_kernel_consumer_output
, session
);
4714 if (ret_code
!= LTTNG_OK
) {
4715 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4718 session
->kernel_session
->consumer
=
4719 snapshot_kernel_consumer_output
;
4721 if (session
->ust_session
) {
4722 original_ust_consumer_output
= session
->ust_session
->consumer
;
4723 snapshot_ust_consumer_output
=
4724 consumer_copy_output(snapshot_output
->consumer
);
4725 strcpy(snapshot_ust_consumer_output
->chunk_path
,
4726 snapshot_chunk_name
);
4727 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4728 original_ust_consumer_output
);
4730 ERR("Failed to copy consumer sockets from snapshot output configuration");
4731 ret_code
= LTTNG_ERR_NOMEM
;
4734 ret_code
= set_relayd_for_snapshot(
4735 snapshot_ust_consumer_output
, session
);
4736 if (ret_code
!= LTTNG_OK
) {
4737 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4740 session
->ust_session
->consumer
=
4741 snapshot_ust_consumer_output
;
4744 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4745 snapshot_kernel_consumer_output
?:
4746 snapshot_ust_consumer_output
,
4747 consumer_output_get_base_path(
4748 snapshot_output
->consumer
),
4749 snapshot_chunk_name
);
4750 if (!snapshot_trace_chunk
) {
4751 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4753 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4756 assert(!session
->current_trace_chunk
);
4757 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4758 lttng_trace_chunk_put(snapshot_trace_chunk
);
4759 snapshot_trace_chunk
= NULL
;
4761 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4763 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4767 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4768 snapshot_output
->max_size
);
4769 if (nb_packets_per_stream
< 0) {
4770 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4771 goto error_close_trace_chunk
;
4774 if (session
->kernel_session
) {
4775 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4776 snapshot_kernel_consumer_output
, session
,
4777 wait
, nb_packets_per_stream
);
4778 if (ret_code
!= LTTNG_OK
) {
4779 goto error_close_trace_chunk
;
4783 if (session
->ust_session
) {
4784 ret_code
= record_ust_snapshot(session
->ust_session
,
4785 snapshot_ust_consumer_output
, session
,
4786 wait
, nb_packets_per_stream
);
4787 if (ret_code
!= LTTNG_OK
) {
4788 goto error_close_trace_chunk
;
4792 error_close_trace_chunk
:
4793 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
4794 ERR("Failed to release the current trace chunk of session \"%s\"",
4796 ret_code
= LTTNG_ERR_UNK
;
4799 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
4800 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
4802 * Don't goto end; make sure the chunk is closed for the session
4803 * to allow future snapshots.
4805 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4807 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4810 if (original_ust_consumer_output
) {
4811 session
->ust_session
->consumer
= original_ust_consumer_output
;
4813 if (original_kernel_consumer_output
) {
4814 session
->kernel_session
->consumer
=
4815 original_kernel_consumer_output
;
4817 consumer_output_put(snapshot_ust_consumer_output
);
4818 consumer_output_put(snapshot_kernel_consumer_output
);
4823 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4825 * The wait parameter is ignored so this call always wait for the snapshot to
4826 * complete before returning.
4828 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4830 int cmd_snapshot_record(struct ltt_session
*session
,
4831 const struct lttng_snapshot_output
*output
, int wait
)
4833 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4835 unsigned int snapshot_success
= 0;
4837 struct snapshot_output
*tmp_output
= NULL
;
4842 DBG("Cmd snapshot record for session %s", session
->name
);
4844 /* Get the datetime for the snapshot output directory. */
4845 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4848 cmd_ret
= LTTNG_ERR_INVALID
;
4853 * Permission denied to create an output if the session is not
4854 * set in no output mode.
4856 if (session
->output_traces
) {
4857 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4861 /* The session needs to be started at least once. */
4862 if (!session
->has_been_started
) {
4863 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4867 /* Use temporary output for the session. */
4868 if (*output
->ctrl_url
!= '\0') {
4869 tmp_output
= snapshot_output_alloc();
4871 cmd_ret
= LTTNG_ERR_NOMEM
;
4875 ret
= snapshot_output_init(session
, output
->max_size
,
4877 output
->ctrl_url
, output
->data_url
,
4881 if (ret
== -ENOMEM
) {
4882 cmd_ret
= LTTNG_ERR_NOMEM
;
4884 cmd_ret
= LTTNG_ERR_INVALID
;
4888 /* Use the global session count for the temporary snapshot. */
4889 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
4891 /* Use the global datetime */
4892 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
4893 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
4894 if (cmd_ret
!= LTTNG_OK
) {
4897 snapshot_success
= 1;
4899 struct snapshot_output
*sout
;
4900 struct lttng_ht_iter iter
;
4903 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4904 &iter
.iter
, sout
, node
.node
) {
4905 struct snapshot_output output_copy
;
4908 * Make a local copy of the output and override output
4909 * parameters with those provided as part of the
4912 memcpy(&output_copy
, sout
, sizeof(output_copy
));
4914 if (output
->max_size
!= (uint64_t) -1ULL) {
4915 output_copy
.max_size
= output
->max_size
;
4918 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4919 memcpy(output_copy
.datetime
, datetime
,
4922 /* Use temporary name. */
4923 if (*output
->name
!= '\0') {
4924 if (lttng_strncpy(output_copy
.name
,
4926 sizeof(output_copy
.name
))) {
4927 cmd_ret
= LTTNG_ERR_INVALID
;
4933 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
4934 if (cmd_ret
!= LTTNG_OK
) {
4938 snapshot_success
= 1;
4943 if (snapshot_success
) {
4944 session
->snapshot
.nb_snapshot
++;
4946 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4951 snapshot_output_destroy(tmp_output
);
4957 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4959 int cmd_set_session_shm_path(struct ltt_session
*session
,
4960 const char *shm_path
)
4966 * Can only set shm path before session is started.
4968 if (session
->has_been_started
) {
4969 return LTTNG_ERR_SESSION_STARTED
;
4972 strncpy(session
->shm_path
, shm_path
,
4973 sizeof(session
->shm_path
));
4974 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4980 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4982 * Ask the consumer to rotate the session output directory.
4983 * The session lock must be held.
4985 * Returns LTTNG_OK on success or else a negative LTTng error code.
4987 int cmd_rotate_session(struct ltt_session
*session
,
4988 struct lttng_rotate_session_return
*rotate_return
,
4989 bool quiet_rotation
,
4990 enum lttng_trace_chunk_command_type command
)
4993 uint64_t ongoing_rotation_chunk_id
;
4994 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4995 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
4996 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
4997 enum lttng_trace_chunk_status chunk_status
;
4998 bool failed_to_rotate
= false;
4999 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5003 if (!session
->has_been_started
) {
5004 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5009 * Explicit rotation is not supported for live sessions.
5010 * However, live sessions can perform a quiet rotation on
5012 * Rotation is not supported for snapshot traces (no output).
5014 if ((!quiet_rotation
&& session
->live_timer
) ||
5015 !session
->output_traces
) {
5016 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5020 /* Unsupported feature in lttng-relayd before 2.11. */
5021 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5022 (session
->consumer
->relay_major_version
== 2 &&
5023 session
->consumer
->relay_minor_version
< 11)) {
5024 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5028 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5029 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5030 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5034 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5035 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5037 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5042 * After a stop, we only allow one rotation to occur, the other ones are
5043 * useless until a new start.
5045 if (session
->rotated_after_last_stop
) {
5046 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5048 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5053 * After a stop followed by a clear, disallow following rotations a they would
5054 * generate empty chunks.
5056 if (session
->cleared_after_last_stop
) {
5057 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5059 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5063 if (session
->active
) {
5064 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5066 if (!new_trace_chunk
) {
5067 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5073 * The current trace chunk becomes the chunk being archived.
5075 * After this point, "chunk_being_archived" must absolutely
5076 * be closed on the consumer(s), otherwise it will never be
5077 * cleaned-up, which will result in a leak.
5079 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5080 &chunk_being_archived
);
5082 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5086 if (session
->kernel_session
) {
5087 cmd_ret
= kernel_rotate_session(session
);
5088 if (cmd_ret
!= LTTNG_OK
) {
5089 failed_to_rotate
= true;
5090 rotation_fail_code
= cmd_ret
;
5093 if (session
->ust_session
) {
5094 cmd_ret
= ust_app_rotate_session(session
);
5095 if (cmd_ret
!= LTTNG_OK
) {
5096 failed_to_rotate
= true;
5097 rotation_fail_code
= cmd_ret
;
5101 if (!session
->active
) {
5102 session
->rotated_after_last_stop
= true;
5105 if (!chunk_being_archived
) {
5106 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5108 if (failed_to_rotate
) {
5109 cmd_ret
= rotation_fail_code
;
5116 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5117 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5118 &ongoing_rotation_chunk_id
);
5119 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5121 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5122 command
, session
->last_chunk_path
);
5124 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5128 if (failed_to_rotate
) {
5129 cmd_ret
= rotation_fail_code
;
5133 session
->quiet_rotation
= quiet_rotation
;
5134 ret
= timer_session_rotation_pending_check_start(session
,
5135 DEFAULT_ROTATE_PENDING_TIMER
);
5137 cmd_ret
= LTTNG_ERR_UNK
;
5141 if (rotate_return
) {
5142 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5145 session
->chunk_being_archived
= chunk_being_archived
;
5146 chunk_being_archived
= NULL
;
5147 if (!quiet_rotation
) {
5148 ret
= notification_thread_command_session_rotation_ongoing(
5149 notification_thread_handle
,
5150 session
->name
, session
->uid
, session
->gid
,
5151 ongoing_rotation_chunk_id
);
5152 if (ret
!= LTTNG_OK
) {
5153 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5159 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5160 session
->name
, ongoing_rotation_chunk_id
);
5162 lttng_trace_chunk_put(new_trace_chunk
);
5163 lttng_trace_chunk_put(chunk_being_archived
);
5164 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5167 if (session_reset_rotation_state(session
,
5168 LTTNG_ROTATION_STATE_ERROR
)) {
5169 ERR("Failed to reset rotation state of session \"%s\"",
5176 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5178 * Check if the session has finished its rotation.
5180 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5182 int cmd_rotate_get_info(struct ltt_session
*session
,
5183 struct lttng_rotation_get_info_return
*info_return
,
5184 uint64_t rotation_id
)
5186 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5187 enum lttng_rotation_state rotation_state
;
5189 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5190 session
->most_recent_chunk_id
.value
);
5192 if (session
->chunk_being_archived
) {
5193 enum lttng_trace_chunk_status chunk_status
;
5196 chunk_status
= lttng_trace_chunk_get_id(
5197 session
->chunk_being_archived
,
5199 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5201 rotation_state
= rotation_id
== chunk_id
?
5202 LTTNG_ROTATION_STATE_ONGOING
:
5203 LTTNG_ROTATION_STATE_EXPIRED
;
5205 if (session
->last_archived_chunk_id
.is_set
&&
5206 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5207 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5209 rotation_state
= session
->rotation_state
;
5213 switch (rotation_state
) {
5214 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5215 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5218 case LTTNG_ROTATION_STATE_EXPIRED
:
5219 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5220 rotation_id
, session
->name
);
5222 case LTTNG_ROTATION_STATE_ONGOING
:
5223 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5224 rotation_id
, session
->name
);
5226 case LTTNG_ROTATION_STATE_COMPLETED
:
5230 char *current_tracing_path_reply
;
5231 size_t current_tracing_path_reply_len
;
5233 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5234 rotation_id
, session
->name
);
5236 switch (session_get_consumer_destination_type(session
)) {
5237 case CONSUMER_DST_LOCAL
:
5238 current_tracing_path_reply
=
5239 info_return
->location
.local
.absolute_path
;
5240 current_tracing_path_reply_len
=
5241 sizeof(info_return
->location
.local
.absolute_path
);
5242 info_return
->location_type
=
5243 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5244 fmt_ret
= asprintf(&chunk_path
,
5245 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5246 session_get_base_path(session
),
5247 session
->last_archived_chunk_name
);
5248 if (fmt_ret
== -1) {
5249 PERROR("Failed to format the path of the last archived trace chunk");
5250 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5251 cmd_ret
= LTTNG_ERR_UNK
;
5255 case CONSUMER_DST_NET
:
5257 uint16_t ctrl_port
, data_port
;
5259 current_tracing_path_reply
=
5260 info_return
->location
.relay
.relative_path
;
5261 current_tracing_path_reply_len
=
5262 sizeof(info_return
->location
.relay
.relative_path
);
5263 /* Currently the only supported relay protocol. */
5264 info_return
->location
.relay
.protocol
=
5265 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5267 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5268 session_get_net_consumer_hostname(session
),
5269 sizeof(info_return
->location
.relay
.host
));
5271 ERR("Failed to copy host name to rotate_get_info reply");
5272 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5273 cmd_ret
= LTTNG_ERR_SET_URL
;
5277 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5278 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5279 info_return
->location
.relay
.ports
.data
= data_port
;
5280 info_return
->location_type
=
5281 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5282 chunk_path
= strdup(session
->last_chunk_path
);
5284 ERR("Failed to allocate the path of the last archived trace chunk");
5285 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5286 cmd_ret
= LTTNG_ERR_UNK
;
5295 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5296 chunk_path
, current_tracing_path_reply_len
);
5299 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5300 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5301 cmd_ret
= LTTNG_ERR_UNK
;
5307 case LTTNG_ROTATION_STATE_ERROR
:
5308 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5309 rotation_id
, session
->name
);
5317 info_return
->status
= (int32_t) rotation_state
;
5322 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5324 * Configure the automatic rotation parameters.
5325 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5326 * 'activate' to false means deactivate the rotation schedule and validate that
5327 * 'new_value' has the same value as the currently active value.
5329 * Return 0 on success or else a positive LTTNG_ERR code.
5331 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5332 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5334 struct notification_thread_handle
*notification_thread_handle
)
5337 uint64_t *parameter_value
;
5341 DBG("Cmd rotate set schedule session %s", session
->name
);
5343 if (session
->live_timer
|| !session
->output_traces
) {
5344 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5345 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5349 switch (schedule_type
) {
5350 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5351 parameter_value
= &session
->rotate_size
;
5353 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5354 parameter_value
= &session
->rotate_timer_period
;
5355 if (new_value
>= UINT_MAX
) {
5356 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5357 new_value
, UINT_MAX
);
5358 ret
= LTTNG_ERR_INVALID
;
5363 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5364 ret
= LTTNG_ERR_INVALID
;
5368 /* Improper use of the API. */
5369 if (new_value
== -1ULL) {
5370 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5371 ret
= LTTNG_ERR_INVALID
;
5376 * As indicated in struct ltt_session's comments, a value of == 0 means
5377 * this schedule rotation type is not in use.
5379 * Reject the command if we were asked to activate a schedule that was
5382 if (activate
&& *parameter_value
!= 0) {
5383 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5384 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5389 * Reject the command if we were asked to deactivate a schedule that was
5392 if (!activate
&& *parameter_value
== 0) {
5393 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5394 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5399 * Reject the command if we were asked to deactivate a schedule that
5402 if (!activate
&& *parameter_value
!= new_value
) {
5403 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5404 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5408 *parameter_value
= activate
? new_value
: 0;
5410 switch (schedule_type
) {
5411 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5412 if (activate
&& session
->active
) {
5414 * Only start the timer if the session is active,
5415 * otherwise it will be started when the session starts.
5417 ret
= timer_session_rotation_schedule_timer_start(
5418 session
, new_value
);
5420 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5421 ret
= LTTNG_ERR_UNK
;
5425 ret
= timer_session_rotation_schedule_timer_stop(
5428 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5429 ret
= LTTNG_ERR_UNK
;
5434 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5436 ret
= subscribe_session_consumed_size_rotation(session
,
5437 new_value
, notification_thread_handle
);
5439 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5440 ret
= LTTNG_ERR_UNK
;
5444 ret
= unsubscribe_session_consumed_size_rotation(session
,
5445 notification_thread_handle
);
5447 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5448 ret
= LTTNG_ERR_UNK
;
5455 /* Would have been caught before. */
5467 /* Wait for a given path to be removed before continuing. */
5468 static enum lttng_error_code
wait_on_path(void *path_data
)
5470 const char *shm_path
= path_data
;
5472 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5478 ret
= stat(shm_path
, &st
);
5480 if (errno
!= ENOENT
) {
5481 PERROR("stat() returned an error while checking for the existence of the shm path");
5483 DBG("shm path no longer exists, completing the destruction of session");
5487 if (!S_ISDIR(st
.st_mode
)) {
5488 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5493 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5499 * Returns a pointer to a handler to run on completion of a command.
5500 * Returns NULL if no handler has to be run for the last command executed.
5502 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5504 struct cmd_completion_handler
*handler
= current_completion_handler
;
5506 current_completion_handler
= NULL
;
5511 * Init command subsystem.
5516 * Set network sequence index to 1 for streams to match a relayd
5517 * socket on the consumer side.
5519 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5520 relayd_net_seq_idx
= 1;
5521 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5523 DBG("Command subsystem initialized");