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
12 #include <urcu/list.h>
13 #include <urcu/uatomic.h>
17 #include <common/defaults.h>
18 #include <common/common.h>
19 #include <common/sessiond-comm/sessiond-comm.h>
20 #include <common/relayd/relayd.h>
21 #include <common/utils.h>
22 #include <common/compat/string.h>
23 #include <common/kernel-ctl/kernel-ctl.h>
24 #include <common/dynamic-buffer.h>
25 #include <common/buffer-view.h>
26 #include <common/trace-chunk.h>
27 #include <lttng/location-internal.h>
28 #include <lttng/trigger/trigger-internal.h>
29 #include <lttng/condition/condition.h>
30 #include <lttng/condition/condition-internal.h>
31 #include <lttng/condition/event-rule.h>
32 #include <lttng/condition/event-rule-internal.h>
33 #include <lttng/event-rule/event-rule.h>
34 #include <lttng/event-rule/event-rule-internal.h>
35 #include <lttng/event-rule/uprobe-internal.h>
36 #include <lttng/event-rule/tracepoint.h>
37 #include <lttng/action/action.h>
38 #include <lttng/channel.h>
39 #include <lttng/channel-internal.h>
40 #include <lttng/rotate-internal.h>
41 #include <lttng/location-internal.h>
42 #include <lttng/session-internal.h>
43 #include <lttng/userspace-probe-internal.h>
44 #include <lttng/session-descriptor-internal.h>
45 #include <common/string-utils/string-utils.h>
50 #include "health-sessiond.h"
52 #include "kernel-consumer.h"
53 #include "lttng-sessiond.h"
55 #include "lttng-syscall.h"
57 #include "buffer-registry.h"
58 #include "notification-thread.h"
59 #include "notification-thread-commands.h"
61 #include "rotation-thread.h"
63 #include "agent-thread.h"
67 /* Sleep for 100ms between each check for the shm path's deletion. */
68 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
70 struct cmd_destroy_session_reply_context
{
72 bool implicit_rotation_on_destroy
;
74 * Indicates whether or not an error occurred while launching the
75 * destruction of a session.
77 enum lttng_error_code destruction_status
;
80 static enum lttng_error_code
wait_on_path(void *path
);
83 * Command completion handler that is used by the destroy command
84 * when a session that has a non-default shm_path is being destroyed.
86 * See comment in cmd_destroy_session() for the rationale.
88 static struct destroy_completion_handler
{
89 struct cmd_completion_handler handler
;
90 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
91 } destroy_completion_handler
= {
94 .data
= destroy_completion_handler
.shm_path
99 static struct cmd_completion_handler
*current_completion_handler
;
102 * Used to keep a unique index for each relayd socket created where this value
103 * is associated with streams on the consumer so it can match the right relayd
104 * to send to. It must be accessed with the relayd_net_seq_idx_lock
107 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
108 static uint64_t relayd_net_seq_idx
;
110 static int validate_ust_event_name(const char *);
111 static int cmd_enable_event_internal(struct ltt_session
*session
,
112 const struct lttng_domain
*domain
,
113 char *channel_name
, struct lttng_event
*event
,
114 char *filter_expression
,
115 struct lttng_filter_bytecode
*filter
,
116 struct lttng_event_exclusion
*exclusion
,
120 * Create a session path used by list_lttng_sessions for the case that the
121 * session consumer is on the network.
123 static int build_network_session_path(char *dst
, size_t size
,
124 struct ltt_session
*session
)
126 int ret
, kdata_port
, udata_port
;
127 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
128 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
133 memset(tmp_urls
, 0, sizeof(tmp_urls
));
134 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
136 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
138 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
139 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
140 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
143 if (session
->ust_session
&& session
->ust_session
->consumer
) {
144 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
145 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
148 if (uuri
== NULL
&& kuri
== NULL
) {
149 uri
= &session
->consumer
->dst
.net
.control
;
150 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
151 } else if (kuri
&& uuri
) {
152 ret
= uri_compare(kuri
, uuri
);
156 /* Build uuri URL string */
157 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
164 } else if (kuri
&& uuri
== NULL
) {
166 } else if (uuri
&& kuri
== NULL
) {
170 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
176 * Do we have a UST url set. If yes, this means we have both kernel and UST
179 if (*tmp_uurl
!= '\0') {
180 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
181 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
184 if (kuri
|| (!kuri
&& !uuri
)) {
187 /* No kernel URI, use the UST port. */
190 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
198 * Get run-time attributes if the session has been started (discarded events,
201 static int get_kernel_runtime_stats(struct ltt_session
*session
,
202 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
203 uint64_t *lost_packets
)
207 if (!session
->has_been_started
) {
209 *discarded_events
= 0;
214 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
215 session
->kernel_session
->consumer
,
221 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
222 session
->kernel_session
->consumer
,
233 * Get run-time attributes if the session has been started (discarded events,
236 static int get_ust_runtime_stats(struct ltt_session
*session
,
237 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
238 uint64_t *lost_packets
)
241 struct ltt_ust_session
*usess
;
243 if (!discarded_events
|| !lost_packets
) {
248 usess
= session
->ust_session
;
249 assert(discarded_events
);
250 assert(lost_packets
);
252 if (!usess
|| !session
->has_been_started
) {
253 *discarded_events
= 0;
259 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
260 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
261 &usess
->buffer_reg_uid_list
,
262 usess
->consumer
, uchan
->id
,
263 uchan
->attr
.overwrite
,
266 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
267 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
268 uchan
, usess
->consumer
,
269 uchan
->attr
.overwrite
,
275 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
276 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
278 ERR("Unsupported buffer type");
289 * Fill lttng_channel array of all channels.
291 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
292 struct ltt_session
*session
, struct lttng_channel
*channels
,
293 struct lttng_channel_extended
*chan_exts
)
296 struct ltt_kernel_channel
*kchan
;
298 DBG("Listing channels for session %s", session
->name
);
301 case LTTNG_DOMAIN_KERNEL
:
302 /* Kernel channels */
303 if (session
->kernel_session
!= NULL
) {
304 cds_list_for_each_entry(kchan
,
305 &session
->kernel_session
->channel_list
.head
, list
) {
306 uint64_t discarded_events
, lost_packets
;
307 struct lttng_channel_extended
*extended
;
309 extended
= (struct lttng_channel_extended
*)
310 kchan
->channel
->attr
.extended
.ptr
;
312 ret
= get_kernel_runtime_stats(session
, kchan
,
313 &discarded_events
, &lost_packets
);
317 /* Copy lttng_channel struct to array */
318 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
319 channels
[i
].enabled
= kchan
->enabled
;
320 chan_exts
[i
].discarded_events
=
322 chan_exts
[i
].lost_packets
= lost_packets
;
323 chan_exts
[i
].monitor_timer_interval
=
324 extended
->monitor_timer_interval
;
325 chan_exts
[i
].blocking_timeout
= 0;
330 case LTTNG_DOMAIN_UST
:
332 struct lttng_ht_iter iter
;
333 struct ltt_ust_channel
*uchan
;
336 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
337 &iter
.iter
, uchan
, node
.node
) {
338 uint64_t discarded_events
= 0, lost_packets
= 0;
340 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
341 LTTNG_SYMBOL_NAME_LEN
)) {
344 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
345 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
346 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
347 channels
[i
].attr
.switch_timer_interval
=
348 uchan
->attr
.switch_timer_interval
;
349 channels
[i
].attr
.read_timer_interval
=
350 uchan
->attr
.read_timer_interval
;
351 channels
[i
].enabled
= uchan
->enabled
;
352 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
353 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
356 * Map enum lttng_ust_output to enum lttng_event_output.
358 switch (uchan
->attr
.output
) {
360 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
364 * LTTNG_UST_MMAP is the only supported UST
371 chan_exts
[i
].monitor_timer_interval
=
372 uchan
->monitor_timer_interval
;
373 chan_exts
[i
].blocking_timeout
=
374 uchan
->attr
.u
.s
.blocking_timeout
;
376 ret
= get_ust_runtime_stats(session
, uchan
,
377 &discarded_events
, &lost_packets
);
381 chan_exts
[i
].discarded_events
= discarded_events
;
382 chan_exts
[i
].lost_packets
= lost_packets
;
394 return -LTTNG_ERR_FATAL
;
400 static int increment_extended_len(const char *filter_expression
,
401 struct lttng_event_exclusion
*exclusion
,
402 const struct lttng_userspace_probe_location
*probe_location
,
403 size_t *extended_len
)
407 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
409 if (filter_expression
) {
410 *extended_len
+= strlen(filter_expression
) + 1;
414 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
417 if (probe_location
) {
418 ret
= lttng_userspace_probe_location_serialize(probe_location
,
423 *extended_len
+= ret
;
430 static int append_extended_info(const char *filter_expression
,
431 struct lttng_event_exclusion
*exclusion
,
432 struct lttng_userspace_probe_location
*probe_location
,
436 size_t filter_len
= 0;
437 size_t nb_exclusions
= 0;
438 size_t userspace_probe_location_len
= 0;
439 struct lttng_dynamic_buffer location_buffer
;
440 struct lttcomm_event_extended_header extended_header
;
442 if (filter_expression
) {
443 filter_len
= strlen(filter_expression
) + 1;
447 nb_exclusions
= exclusion
->count
;
450 if (probe_location
) {
451 lttng_dynamic_buffer_init(&location_buffer
);
452 ret
= lttng_userspace_probe_location_serialize(probe_location
,
453 &location_buffer
, NULL
);
458 userspace_probe_location_len
= location_buffer
.size
;
461 /* Set header fields */
462 extended_header
.filter_len
= filter_len
;
463 extended_header
.nb_exclusions
= nb_exclusions
;
464 extended_header
.userspace_probe_location_len
= userspace_probe_location_len
;
467 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
468 *extended_at
+= sizeof(extended_header
);
470 /* Copy filter string */
471 if (filter_expression
) {
472 memcpy(*extended_at
, filter_expression
, filter_len
);
473 *extended_at
+= filter_len
;
476 /* Copy exclusion names */
478 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
480 memcpy(*extended_at
, &exclusion
->names
, len
);
484 if (probe_location
) {
485 memcpy(*extended_at
, location_buffer
.data
, location_buffer
.size
);
486 *extended_at
+= location_buffer
.size
;
487 lttng_dynamic_buffer_reset(&location_buffer
);
495 * Create a list of agent domain events.
497 * Return number of events in list on success or else a negative value.
499 static int list_lttng_agent_events(struct agent
*agt
,
500 struct lttng_event
**events
, size_t *total_size
)
503 unsigned int nb_event
= 0;
504 struct agent_event
*event
;
505 struct lttng_event
*tmp_events
= NULL
;
506 struct lttng_ht_iter iter
;
507 size_t extended_len
= 0;
513 DBG3("Listing agent events");
516 nb_event
= lttng_ht_get_count(agt
->events
);
524 /* Compute required extended infos size */
525 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
528 * This is only valid because the commands which add events are
529 * processed in the same thread as the listing.
532 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
533 ret
= increment_extended_len(event
->filter_expression
, NULL
, NULL
,
536 DBG("Error computing the length of extended info message");
537 ret
= -LTTNG_ERR_FATAL
;
543 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
544 tmp_events
= zmalloc(*total_size
);
546 PERROR("zmalloc agent events session");
547 ret
= -LTTNG_ERR_FATAL
;
551 extended_at
= ((uint8_t *) tmp_events
) +
552 nb_event
* sizeof(struct lttng_event
);
555 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
556 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
557 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
558 tmp_events
[i
].enabled
= event
->enabled
;
559 tmp_events
[i
].loglevel
= event
->loglevel_value
;
560 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
563 /* Append extended info */
564 ret
= append_extended_info(event
->filter_expression
, NULL
, NULL
,
567 DBG("Error appending extended info message");
568 ret
= -LTTNG_ERR_FATAL
;
573 *events
= tmp_events
;
575 assert(nb_event
== i
);
586 * Create a list of ust global domain events.
588 static int list_lttng_ust_global_events(char *channel_name
,
589 struct ltt_ust_domain_global
*ust_global
,
590 struct lttng_event
**events
, size_t *total_size
)
593 unsigned int nb_event
= 0;
594 struct lttng_ht_iter iter
;
595 struct lttng_ht_node_str
*node
;
596 struct ltt_ust_channel
*uchan
;
597 struct ltt_ust_event
*uevent
;
598 struct lttng_event
*tmp
;
599 size_t extended_len
= 0;
602 DBG("Listing UST global events for channel %s", channel_name
);
606 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
607 node
= lttng_ht_iter_get_node_str(&iter
);
609 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
613 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
615 nb_event
= lttng_ht_get_count(uchan
->events
);
622 DBG3("Listing UST global %d events", nb_event
);
624 /* Compute required extended infos size */
625 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
626 if (uevent
->internal
) {
631 ret
= increment_extended_len(uevent
->filter_expression
,
632 uevent
->exclusion
, NULL
, &extended_len
);
634 DBG("Error computing the length of extended info message");
635 ret
= -LTTNG_ERR_FATAL
;
640 /* All events are internal, skip. */
646 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
647 tmp
= zmalloc(*total_size
);
649 ret
= -LTTNG_ERR_FATAL
;
653 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
655 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
656 if (uevent
->internal
) {
657 /* This event should remain hidden from clients */
660 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
661 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
662 tmp
[i
].enabled
= uevent
->enabled
;
664 switch (uevent
->attr
.instrumentation
) {
665 case LTTNG_UST_TRACEPOINT
:
666 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
668 case LTTNG_UST_PROBE
:
669 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
671 case LTTNG_UST_FUNCTION
:
672 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
676 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
677 switch (uevent
->attr
.loglevel_type
) {
678 case LTTNG_UST_LOGLEVEL_ALL
:
679 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
681 case LTTNG_UST_LOGLEVEL_RANGE
:
682 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
684 case LTTNG_UST_LOGLEVEL_SINGLE
:
685 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
688 if (uevent
->filter
) {
691 if (uevent
->exclusion
) {
692 tmp
[i
].exclusion
= 1;
696 /* Append extended info */
697 ret
= append_extended_info(uevent
->filter_expression
,
698 uevent
->exclusion
, NULL
, &extended_at
);
700 DBG("Error appending extended info message");
701 ret
= -LTTNG_ERR_FATAL
;
714 * Fill lttng_event array of all kernel events in the channel.
716 static int list_lttng_kernel_events(char *channel_name
,
717 struct ltt_kernel_session
*kernel_session
,
718 struct lttng_event
**events
, size_t *total_size
)
721 unsigned int nb_event
;
722 struct ltt_kernel_event
*event
;
723 struct ltt_kernel_channel
*kchan
;
724 size_t extended_len
= 0;
727 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
729 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
733 nb_event
= kchan
->event_count
;
735 DBG("Listing events for channel %s", kchan
->channel
->name
);
743 /* Compute required extended infos size */
744 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
745 ret
= increment_extended_len(event
->filter_expression
, NULL
,
746 event
->userspace_probe_location
,
749 DBG("Error computing the length of extended info message");
750 ret
= -LTTNG_ERR_FATAL
;
755 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
756 *events
= zmalloc(*total_size
);
757 if (*events
== NULL
) {
758 ret
= -LTTNG_ERR_FATAL
;
762 extended_at
= ((void *) *events
) +
763 nb_event
* sizeof(struct lttng_event
);
765 /* Kernel channels */
766 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
767 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
768 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
769 (*events
)[i
].enabled
= event
->enabled
;
770 (*events
)[i
].filter
=
771 (unsigned char) !!event
->filter_expression
;
773 switch (event
->event
->instrumentation
) {
774 case LTTNG_KERNEL_TRACEPOINT
:
775 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
777 case LTTNG_KERNEL_KRETPROBE
:
778 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
779 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
780 sizeof(struct lttng_kernel_kprobe
));
782 case LTTNG_KERNEL_KPROBE
:
783 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
784 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
785 sizeof(struct lttng_kernel_kprobe
));
787 case LTTNG_KERNEL_UPROBE
:
788 (*events
)[i
].type
= LTTNG_EVENT_USERSPACE_PROBE
;
790 case LTTNG_KERNEL_FUNCTION
:
791 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
792 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
793 sizeof(struct lttng_kernel_function
));
795 case LTTNG_KERNEL_NOOP
:
796 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
798 case LTTNG_KERNEL_SYSCALL
:
799 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
801 case LTTNG_KERNEL_ALL
:
809 /* Append extended info */
810 ret
= append_extended_info(event
->filter_expression
, NULL
,
811 event
->userspace_probe_location
, &extended_at
);
813 DBG("Error appending extended info message");
814 ret
= -LTTNG_ERR_FATAL
;
823 /* Negate the error code to differentiate the size from an error */
828 * Add URI so the consumer output object. Set the correct path depending on the
829 * domain adding the default trace directory.
831 static enum lttng_error_code
add_uri_to_consumer(
832 const struct ltt_session
*session
,
833 struct consumer_output
*consumer
,
834 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
837 enum lttng_error_code ret_code
= LTTNG_OK
;
841 if (consumer
== NULL
) {
842 DBG("No consumer detected. Don't add URI. Stopping.");
843 ret_code
= LTTNG_ERR_NO_CONSUMER
;
848 case LTTNG_DOMAIN_KERNEL
:
849 ret
= lttng_strncpy(consumer
->domain_subdir
,
850 DEFAULT_KERNEL_TRACE_DIR
,
851 sizeof(consumer
->domain_subdir
));
853 case LTTNG_DOMAIN_UST
:
854 ret
= lttng_strncpy(consumer
->domain_subdir
,
855 DEFAULT_UST_TRACE_DIR
,
856 sizeof(consumer
->domain_subdir
));
860 * This case is possible is we try to add the URI to the global
861 * tracing session consumer object which in this case there is
864 memset(consumer
->domain_subdir
, 0,
865 sizeof(consumer
->domain_subdir
));
869 ERR("Failed to initialize consumer output domain subdirectory");
870 ret_code
= LTTNG_ERR_FATAL
;
874 switch (uri
->dtype
) {
877 DBG2("Setting network URI to consumer");
879 if (consumer
->type
== CONSUMER_DST_NET
) {
880 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
881 consumer
->dst
.net
.control_isset
) ||
882 (uri
->stype
== LTTNG_STREAM_DATA
&&
883 consumer
->dst
.net
.data_isset
)) {
884 ret_code
= LTTNG_ERR_URL_EXIST
;
888 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
891 /* Set URI into consumer output object */
892 ret
= consumer_set_network_uri(session
, consumer
, uri
);
896 } else if (ret
== 1) {
898 * URI was the same in the consumer so we do not append the subdir
899 * again so to not duplicate output dir.
906 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
907 ret_code
= LTTNG_ERR_INVALID
;
910 DBG2("Setting trace directory path from URI to %s",
912 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
914 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
916 sizeof(consumer
->dst
.session_root_path
));
918 ret_code
= LTTNG_ERR_FATAL
;
921 consumer
->type
= CONSUMER_DST_LOCAL
;
931 * Init tracing by creating trace directory and sending fds kernel consumer.
933 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
936 struct lttng_ht_iter iter
;
937 struct consumer_socket
*socket
;
943 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
944 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
946 pthread_mutex_lock(socket
->lock
);
947 ret
= kernel_consumer_send_session(socket
, session
);
948 pthread_mutex_unlock(socket
->lock
);
950 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
962 * Create a socket to the relayd using the URI.
964 * On success, the relayd_sock pointer is set to the created socket.
965 * Else, it remains untouched and an LTTng error code is returned.
967 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
968 struct lttcomm_relayd_sock
**relayd_sock
,
969 struct consumer_output
*consumer
)
972 enum lttng_error_code status
= LTTNG_OK
;
973 struct lttcomm_relayd_sock
*rsock
;
975 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
976 RELAYD_VERSION_COMM_MINOR
);
978 status
= LTTNG_ERR_FATAL
;
983 * Connect to relayd so we can proceed with a session creation. This call
984 * can possibly block for an arbitrary amount of time to set the health
985 * state to be in poll execution.
988 ret
= relayd_connect(rsock
);
991 ERR("Unable to reach lttng-relayd");
992 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
996 /* Create socket for control stream. */
997 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
998 uint64_t result_flags
;
1000 DBG3("Creating relayd stream socket from URI");
1002 /* Check relayd version */
1003 ret
= relayd_version_check(rsock
);
1004 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
1005 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
1007 } else if (ret
< 0) {
1008 ERR("Unable to reach lttng-relayd");
1009 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1012 consumer
->relay_major_version
= rsock
->major
;
1013 consumer
->relay_minor_version
= rsock
->minor
;
1014 ret
= relayd_get_configuration(rsock
, 0,
1017 ERR("Unable to get relayd configuration");
1018 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1021 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
1022 consumer
->relay_allows_clear
= true;
1024 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1025 DBG3("Creating relayd data socket from URI");
1027 /* Command is not valid */
1028 ERR("Relayd invalid stream type: %d", uri
->stype
);
1029 status
= LTTNG_ERR_INVALID
;
1033 *relayd_sock
= rsock
;
1038 /* The returned value is not useful since we are on an error path. */
1039 (void) relayd_close(rsock
);
1047 * Connect to the relayd using URI and send the socket to the right consumer.
1049 * The consumer socket lock must be held by the caller.
1051 * Returns LTTNG_OK on success or an LTTng error code on failure.
1053 static enum lttng_error_code
send_consumer_relayd_socket(
1054 unsigned int session_id
,
1055 struct lttng_uri
*relayd_uri
,
1056 struct consumer_output
*consumer
,
1057 struct consumer_socket
*consumer_sock
,
1058 const char *session_name
, const char *hostname
,
1059 const char *base_path
, int session_live_timer
,
1060 const uint64_t *current_chunk_id
,
1061 time_t session_creation_time
,
1062 bool session_name_contains_creation_time
)
1065 struct lttcomm_relayd_sock
*rsock
= NULL
;
1066 enum lttng_error_code status
;
1068 /* Connect to relayd and make version check if uri is the control. */
1069 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1070 if (status
!= LTTNG_OK
) {
1071 goto relayd_comm_error
;
1075 /* Set the network sequence index if not set. */
1076 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1077 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1079 * Increment net_seq_idx because we are about to transfer the
1080 * new relayd socket to the consumer.
1081 * Assign unique key so the consumer can match streams.
1083 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1084 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1087 /* Send relayd socket to consumer. */
1088 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1089 relayd_uri
->stype
, session_id
,
1090 session_name
, hostname
, base_path
,
1091 session_live_timer
, current_chunk_id
,
1092 session_creation_time
, session_name_contains_creation_time
);
1094 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1098 /* Flag that the corresponding socket was sent. */
1099 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1100 consumer_sock
->control_sock_sent
= 1;
1101 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1102 consumer_sock
->data_sock_sent
= 1;
1106 * Close socket which was dup on the consumer side. The session daemon does
1107 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1111 if (status
!= LTTNG_OK
) {
1113 * The consumer output for this session should not be used anymore
1114 * since the relayd connection failed thus making any tracing or/and
1115 * streaming not usable.
1117 consumer
->enabled
= 0;
1119 (void) relayd_close(rsock
);
1127 * Send both relayd sockets to a specific consumer and domain. This is a
1128 * helper function to facilitate sending the information to the consumer for a
1131 * The consumer socket lock must be held by the caller.
1133 * Returns LTTNG_OK, or an LTTng error code on failure.
1135 static enum lttng_error_code
send_consumer_relayd_sockets(
1136 enum lttng_domain_type domain
,
1137 unsigned int session_id
, struct consumer_output
*consumer
,
1138 struct consumer_socket
*sock
, const char *session_name
,
1139 const char *hostname
, const char *base_path
, int session_live_timer
,
1140 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1141 bool session_name_contains_creation_time
)
1143 enum lttng_error_code status
= LTTNG_OK
;
1148 /* Sending control relayd socket. */
1149 if (!sock
->control_sock_sent
) {
1150 status
= send_consumer_relayd_socket(session_id
,
1151 &consumer
->dst
.net
.control
, consumer
, sock
,
1152 session_name
, hostname
, base_path
, session_live_timer
,
1153 current_chunk_id
, session_creation_time
,
1154 session_name_contains_creation_time
);
1155 if (status
!= LTTNG_OK
) {
1160 /* Sending data relayd socket. */
1161 if (!sock
->data_sock_sent
) {
1162 status
= send_consumer_relayd_socket(session_id
,
1163 &consumer
->dst
.net
.data
, consumer
, sock
,
1164 session_name
, hostname
, base_path
, session_live_timer
,
1165 current_chunk_id
, session_creation_time
,
1166 session_name_contains_creation_time
);
1167 if (status
!= LTTNG_OK
) {
1177 * Setup relayd connections for a tracing session. First creates the socket to
1178 * the relayd and send them to the right domain consumer. Consumer type MUST be
1181 int cmd_setup_relayd(struct ltt_session
*session
)
1184 struct ltt_ust_session
*usess
;
1185 struct ltt_kernel_session
*ksess
;
1186 struct consumer_socket
*socket
;
1187 struct lttng_ht_iter iter
;
1188 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1192 usess
= session
->ust_session
;
1193 ksess
= session
->kernel_session
;
1195 DBG("Setting relayd for session %s", session
->name
);
1198 if (session
->current_trace_chunk
) {
1199 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1200 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1202 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1203 current_chunk_id
.is_set
= true;
1205 ERR("Failed to get current trace chunk id");
1206 ret
= LTTNG_ERR_UNK
;
1211 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1212 && usess
->consumer
->enabled
) {
1213 /* For each consumer socket, send relayd sockets */
1214 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1215 socket
, node
.node
) {
1216 pthread_mutex_lock(socket
->lock
);
1217 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1218 usess
->consumer
, socket
,
1219 session
->name
, session
->hostname
,
1221 session
->live_timer
,
1222 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1223 session
->creation_time
,
1224 session
->name_contains_creation_time
);
1225 pthread_mutex_unlock(socket
->lock
);
1226 if (ret
!= LTTNG_OK
) {
1229 /* Session is now ready for network streaming. */
1230 session
->net_handle
= 1;
1232 session
->consumer
->relay_major_version
=
1233 usess
->consumer
->relay_major_version
;
1234 session
->consumer
->relay_minor_version
=
1235 usess
->consumer
->relay_minor_version
;
1236 session
->consumer
->relay_allows_clear
=
1237 usess
->consumer
->relay_allows_clear
;
1240 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1241 && ksess
->consumer
->enabled
) {
1242 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1243 socket
, node
.node
) {
1244 pthread_mutex_lock(socket
->lock
);
1245 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1246 ksess
->consumer
, socket
,
1247 session
->name
, session
->hostname
,
1249 session
->live_timer
,
1250 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1251 session
->creation_time
,
1252 session
->name_contains_creation_time
);
1253 pthread_mutex_unlock(socket
->lock
);
1254 if (ret
!= LTTNG_OK
) {
1257 /* Session is now ready for network streaming. */
1258 session
->net_handle
= 1;
1260 session
->consumer
->relay_major_version
=
1261 ksess
->consumer
->relay_major_version
;
1262 session
->consumer
->relay_minor_version
=
1263 ksess
->consumer
->relay_minor_version
;
1264 session
->consumer
->relay_allows_clear
=
1265 ksess
->consumer
->relay_allows_clear
;
1274 * Start a kernel session by opening all necessary streams.
1276 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1279 struct ltt_kernel_channel
*kchan
;
1281 /* Open kernel metadata */
1282 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1283 ret
= kernel_open_metadata(ksess
);
1285 ret
= LTTNG_ERR_KERN_META_FAIL
;
1290 /* Open kernel metadata stream */
1291 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1292 ret
= kernel_open_metadata_stream(ksess
);
1294 ERR("Kernel create metadata stream failed");
1295 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1300 /* For each channel */
1301 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1302 if (kchan
->stream_count
== 0) {
1303 ret
= kernel_open_channel_stream(kchan
);
1305 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1308 /* Update the stream global counter */
1309 ksess
->stream_count_global
+= ret
;
1313 /* Setup kernel consumer socket and send fds to it */
1314 ret
= init_kernel_tracing(ksess
);
1316 ret
= LTTNG_ERR_KERN_START_FAIL
;
1320 /* This start the kernel tracing */
1321 ret
= kernel_start_session(ksess
);
1323 ret
= LTTNG_ERR_KERN_START_FAIL
;
1327 /* Quiescent wait after starting trace */
1328 kernel_wait_quiescent();
1338 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1340 struct ltt_kernel_channel
*kchan
;
1341 bool error_occurred
= false;
1344 if (!ksess
|| !ksess
->active
) {
1347 DBG("Stopping kernel tracing");
1349 ret
= kernel_stop_session(ksess
);
1351 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1355 kernel_wait_quiescent();
1357 /* Flush metadata after stopping (if exists) */
1358 if (ksess
->metadata_stream_fd
>= 0) {
1359 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1361 ERR("Kernel metadata flush failed");
1362 error_occurred
= true;
1366 /* Flush all buffers after stopping */
1367 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1368 ret
= kernel_flush_buffer(kchan
);
1370 ERR("Kernel flush buffer error");
1371 error_occurred
= true;
1376 if (error_occurred
) {
1377 ret
= LTTNG_ERR_UNK
;
1386 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1388 int cmd_disable_channel(struct ltt_session
*session
,
1389 enum lttng_domain_type domain
, char *channel_name
)
1392 struct ltt_ust_session
*usess
;
1394 usess
= session
->ust_session
;
1399 case LTTNG_DOMAIN_KERNEL
:
1401 ret
= channel_kernel_disable(session
->kernel_session
,
1403 if (ret
!= LTTNG_OK
) {
1407 kernel_wait_quiescent();
1410 case LTTNG_DOMAIN_UST
:
1412 struct ltt_ust_channel
*uchan
;
1413 struct lttng_ht
*chan_ht
;
1415 chan_ht
= usess
->domain_global
.channels
;
1417 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1418 if (uchan
== NULL
) {
1419 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1423 ret
= channel_ust_disable(usess
, uchan
);
1424 if (ret
!= LTTNG_OK
) {
1430 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1442 * Command LTTNG_TRACK_ID processed by the client thread.
1444 * Called with session lock held.
1446 int cmd_track_id(struct ltt_session
*session
,
1447 enum lttng_tracker_type tracker_type
,
1448 enum lttng_domain_type domain
,
1449 const struct lttng_tracker_id
*id
)
1456 case LTTNG_DOMAIN_KERNEL
:
1458 struct ltt_kernel_session
*ksess
;
1460 ksess
= session
->kernel_session
;
1462 ret
= kernel_track_id(tracker_type
, ksess
, id
);
1463 if (ret
!= LTTNG_OK
) {
1467 kernel_wait_quiescent();
1470 case LTTNG_DOMAIN_UST
:
1472 struct ltt_ust_session
*usess
;
1474 usess
= session
->ust_session
;
1476 ret
= trace_ust_track_id(tracker_type
, usess
, id
);
1477 if (ret
!= LTTNG_OK
) {
1483 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1495 * Command LTTNG_UNTRACK_ID processed by the client thread.
1497 * Called with session lock held.
1499 int cmd_untrack_id(struct ltt_session
*session
,
1500 enum lttng_tracker_type tracker_type
,
1501 enum lttng_domain_type domain
,
1502 const struct lttng_tracker_id
*id
)
1509 case LTTNG_DOMAIN_KERNEL
:
1511 struct ltt_kernel_session
*ksess
;
1513 ksess
= session
->kernel_session
;
1515 ret
= kernel_untrack_id(tracker_type
, ksess
, id
);
1516 if (ret
!= LTTNG_OK
) {
1520 kernel_wait_quiescent();
1523 case LTTNG_DOMAIN_UST
:
1525 struct ltt_ust_session
*usess
;
1527 usess
= session
->ust_session
;
1529 ret
= trace_ust_untrack_id(tracker_type
, usess
, id
);
1530 if (ret
!= LTTNG_OK
) {
1536 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1548 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1550 * The wpipe arguments is used as a notifier for the kernel thread.
1552 int cmd_enable_channel(struct ltt_session
*session
,
1553 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1556 struct ltt_ust_session
*usess
= session
->ust_session
;
1557 struct lttng_ht
*chan_ht
;
1559 struct lttng_channel attr
;
1566 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1568 /* Validate channel name */
1569 if (attr
.name
[0] == '.' ||
1570 memchr(attr
.name
, '/', len
) != NULL
) {
1571 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1575 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1580 * Don't try to enable a channel if the session has been started at
1581 * some point in time before. The tracer does not allow it.
1583 if (session
->has_been_started
) {
1584 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1589 * If the session is a live session, remove the switch timer, the
1590 * live timer does the same thing but sends also synchronisation
1591 * beacons for inactive streams.
1593 if (session
->live_timer
> 0) {
1594 attr
.attr
.live_timer_interval
= session
->live_timer
;
1595 attr
.attr
.switch_timer_interval
= 0;
1598 /* Check for feature support */
1599 switch (domain
->type
) {
1600 case LTTNG_DOMAIN_KERNEL
:
1602 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1603 /* Sampling position of buffer is not supported */
1604 WARN("Kernel tracer does not support buffer monitoring. "
1605 "Setting the monitor interval timer to 0 "
1606 "(disabled) for channel '%s' of session '%s'",
1607 attr
.name
, session
->name
);
1608 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1612 case LTTNG_DOMAIN_UST
:
1614 case LTTNG_DOMAIN_JUL
:
1615 case LTTNG_DOMAIN_LOG4J
:
1616 case LTTNG_DOMAIN_PYTHON
:
1617 if (!agent_tracing_is_enabled()) {
1618 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1619 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1624 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1628 switch (domain
->type
) {
1629 case LTTNG_DOMAIN_KERNEL
:
1631 struct ltt_kernel_channel
*kchan
;
1633 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1634 session
->kernel_session
);
1635 if (kchan
== NULL
) {
1636 if (session
->snapshot
.nb_output
> 0 ||
1637 session
->snapshot_mode
) {
1638 /* Enforce mmap output for snapshot sessions. */
1639 attr
.attr
.output
= LTTNG_EVENT_MMAP
;
1641 ret
= channel_kernel_create(session
->kernel_session
, &attr
, wpipe
);
1642 if (attr
.name
[0] != '\0') {
1643 session
->kernel_session
->has_non_default_channel
= 1;
1646 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1649 if (ret
!= LTTNG_OK
) {
1653 kernel_wait_quiescent();
1656 case LTTNG_DOMAIN_UST
:
1657 case LTTNG_DOMAIN_JUL
:
1658 case LTTNG_DOMAIN_LOG4J
:
1659 case LTTNG_DOMAIN_PYTHON
:
1661 struct ltt_ust_channel
*uchan
;
1666 * Current agent implementation limitations force us to allow
1667 * only one channel at once in "agent" subdomains. Each
1668 * subdomain has a default channel name which must be strictly
1671 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1672 if (strncmp(attr
.name
, DEFAULT_JUL_CHANNEL_NAME
,
1673 LTTNG_SYMBOL_NAME_LEN
)) {
1674 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1677 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1678 if (strncmp(attr
.name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1679 LTTNG_SYMBOL_NAME_LEN
)) {
1680 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1683 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1684 if (strncmp(attr
.name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1685 LTTNG_SYMBOL_NAME_LEN
)) {
1686 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1691 chan_ht
= usess
->domain_global
.channels
;
1693 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1694 if (uchan
== NULL
) {
1695 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1696 if (attr
.name
[0] != '\0') {
1697 usess
->has_non_default_channel
= 1;
1700 ret
= channel_ust_enable(usess
, uchan
);
1705 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1709 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1710 session
->has_non_mmap_channel
= true;
1719 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1721 int cmd_disable_event(struct ltt_session
*session
,
1722 enum lttng_domain_type domain
, const char *channel_name
,
1723 const struct lttng_event
*event
)
1726 const char *event_name
;
1728 DBG("Disable event command for event \'%s\'", event
->name
);
1730 event_name
= event
->name
;
1732 /* Error out on unhandled search criteria */
1733 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1734 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1735 ret
= LTTNG_ERR_UNK
;
1742 case LTTNG_DOMAIN_KERNEL
:
1744 struct ltt_kernel_channel
*kchan
;
1745 struct ltt_kernel_session
*ksess
;
1747 ksess
= session
->kernel_session
;
1750 * If a non-default channel has been created in the
1751 * session, explicitely require that -c chan_name needs
1754 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1755 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1759 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1760 if (kchan
== NULL
) {
1761 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1765 switch (event
->type
) {
1766 case LTTNG_EVENT_ALL
:
1767 case LTTNG_EVENT_TRACEPOINT
:
1768 case LTTNG_EVENT_SYSCALL
:
1769 case LTTNG_EVENT_PROBE
:
1770 case LTTNG_EVENT_FUNCTION
:
1771 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1772 if (event_name
[0] == '\0') {
1773 ret
= event_kernel_disable_event(kchan
,
1776 ret
= event_kernel_disable_event(kchan
,
1777 event_name
, event
->type
);
1779 if (ret
!= LTTNG_OK
) {
1784 ret
= LTTNG_ERR_UNK
;
1788 kernel_wait_quiescent();
1791 case LTTNG_DOMAIN_UST
:
1793 struct ltt_ust_channel
*uchan
;
1794 struct ltt_ust_session
*usess
;
1796 usess
= session
->ust_session
;
1798 if (validate_ust_event_name(event_name
)) {
1799 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1804 * If a non-default channel has been created in the
1805 * session, explicitly require that -c chan_name needs
1808 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1809 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1813 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1815 if (uchan
== NULL
) {
1816 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1820 switch (event
->type
) {
1821 case LTTNG_EVENT_ALL
:
1823 * An empty event name means that everything
1824 * should be disabled.
1826 if (event
->name
[0] == '\0') {
1827 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1829 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1832 if (ret
!= LTTNG_OK
) {
1837 ret
= LTTNG_ERR_UNK
;
1841 DBG3("Disable UST event %s in channel %s completed", event_name
,
1845 case LTTNG_DOMAIN_LOG4J
:
1846 case LTTNG_DOMAIN_JUL
:
1847 case LTTNG_DOMAIN_PYTHON
:
1850 struct ltt_ust_session
*usess
= session
->ust_session
;
1854 switch (event
->type
) {
1855 case LTTNG_EVENT_ALL
:
1858 ret
= LTTNG_ERR_UNK
;
1862 agt
= trace_ust_find_agent(usess
, domain
);
1864 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1868 * An empty event name means that everything
1869 * should be disabled.
1871 if (event
->name
[0] == '\0') {
1872 ret
= event_agent_disable_all(usess
, agt
);
1874 ret
= event_agent_disable(usess
, agt
, event_name
);
1876 if (ret
!= LTTNG_OK
) {
1883 ret
= LTTNG_ERR_UND
;
1896 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1898 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1899 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1901 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1902 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1905 * Don't try to add a context if the session has been started at
1906 * some point in time before. The tracer does not allow it and would
1907 * result in a corrupted trace.
1909 if (session
->has_been_started
) {
1910 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1914 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1915 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1916 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1920 case LTTNG_DOMAIN_KERNEL
:
1921 assert(session
->kernel_session
);
1923 if (session
->kernel_session
->channel_count
== 0) {
1924 /* Create default channel */
1925 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1926 if (ret
!= LTTNG_OK
) {
1929 chan_kern_created
= 1;
1931 /* Add kernel context to kernel tracer */
1932 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1933 if (ret
!= LTTNG_OK
) {
1937 case LTTNG_DOMAIN_JUL
:
1938 case LTTNG_DOMAIN_LOG4J
:
1941 * Validate channel name.
1942 * If no channel name is given and the domain is JUL or LOG4J,
1943 * set it to the appropriate domain-specific channel name. If
1944 * a name is provided but does not match the expexted channel
1945 * name, return an error.
1947 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1948 strcmp(channel_name
,
1949 DEFAULT_JUL_CHANNEL_NAME
)) {
1950 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1952 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1953 strcmp(channel_name
,
1954 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1955 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1958 /* break is _not_ missing here. */
1960 case LTTNG_DOMAIN_UST
:
1962 struct ltt_ust_session
*usess
= session
->ust_session
;
1963 unsigned int chan_count
;
1967 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1968 if (chan_count
== 0) {
1969 struct lttng_channel
*attr
;
1970 /* Create default channel */
1971 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1973 ret
= LTTNG_ERR_FATAL
;
1977 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1978 if (ret
!= LTTNG_OK
) {
1982 channel_attr_destroy(attr
);
1983 chan_ust_created
= 1;
1986 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1987 free(app_ctx_provider_name
);
1989 app_ctx_name
= NULL
;
1990 app_ctx_provider_name
= NULL
;
1991 if (ret
!= LTTNG_OK
) {
1997 ret
= LTTNG_ERR_UND
;
2005 if (chan_kern_created
) {
2006 struct ltt_kernel_channel
*kchan
=
2007 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2008 session
->kernel_session
);
2009 /* Created previously, this should NOT fail. */
2011 kernel_destroy_channel(kchan
);
2014 if (chan_ust_created
) {
2015 struct ltt_ust_channel
*uchan
=
2016 trace_ust_find_channel_by_name(
2017 session
->ust_session
->domain_global
.channels
,
2018 DEFAULT_CHANNEL_NAME
);
2019 /* Created previously, this should NOT fail. */
2021 /* Remove from the channel list of the session. */
2022 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2024 trace_ust_destroy_channel(uchan
);
2027 free(app_ctx_provider_name
);
2032 static inline bool name_starts_with(const char *name
, const char *prefix
)
2034 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2036 return !strncmp(name
, prefix
, max_cmp_len
);
2039 /* Perform userspace-specific event name validation */
2040 static int validate_ust_event_name(const char *name
)
2050 * Check name against all internal UST event component namespaces used
2053 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2054 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2055 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2064 * Internal version of cmd_enable_event() with a supplemental
2065 * "internal_event" flag which is used to enable internal events which should
2066 * be hidden from clients. Such events are used in the agent implementation to
2067 * enable the events through which all "agent" events are funeled.
2069 static int _cmd_enable_event(struct ltt_session
*session
,
2070 const struct lttng_domain
*domain
,
2071 char *channel_name
, struct lttng_event
*event
,
2072 char *filter_expression
,
2073 struct lttng_filter_bytecode
*filter
,
2074 struct lttng_event_exclusion
*exclusion
,
2075 int wpipe
, bool internal_event
)
2077 int ret
= 0, channel_created
= 0;
2078 struct lttng_channel
*attr
= NULL
;
2082 assert(channel_name
);
2084 /* If we have a filter, we must have its filter expression */
2085 assert(!(!!filter_expression
^ !!filter
));
2087 /* Normalize event name as a globbing pattern */
2088 strutils_normalize_star_glob_pattern(event
->name
);
2090 /* Normalize exclusion names as globbing patterns */
2094 for (i
= 0; i
< exclusion
->count
; i
++) {
2095 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2097 strutils_normalize_star_glob_pattern(name
);
2101 DBG("Enable event command for event \'%s\'", event
->name
);
2105 switch (domain
->type
) {
2106 case LTTNG_DOMAIN_KERNEL
:
2108 struct ltt_kernel_channel
*kchan
;
2111 * If a non-default channel has been created in the
2112 * session, explicitely require that -c chan_name needs
2115 if (session
->kernel_session
->has_non_default_channel
2116 && channel_name
[0] == '\0') {
2117 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2121 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2122 session
->kernel_session
);
2123 if (kchan
== NULL
) {
2124 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2125 LTTNG_BUFFER_GLOBAL
);
2127 ret
= LTTNG_ERR_FATAL
;
2130 if (lttng_strncpy(attr
->name
, channel_name
,
2131 sizeof(attr
->name
))) {
2132 ret
= LTTNG_ERR_INVALID
;
2136 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2137 if (ret
!= LTTNG_OK
) {
2140 channel_created
= 1;
2143 /* Get the newly created kernel channel pointer */
2144 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2145 session
->kernel_session
);
2146 if (kchan
== NULL
) {
2147 /* This sould not happen... */
2148 ret
= LTTNG_ERR_FATAL
;
2152 switch (event
->type
) {
2153 case LTTNG_EVENT_ALL
:
2155 char *filter_expression_a
= NULL
;
2156 struct lttng_filter_bytecode
*filter_a
= NULL
;
2159 * We need to duplicate filter_expression and filter,
2160 * because ownership is passed to first enable
2163 if (filter_expression
) {
2164 filter_expression_a
= strdup(filter_expression
);
2165 if (!filter_expression_a
) {
2166 ret
= LTTNG_ERR_FATAL
;
2171 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2173 free(filter_expression_a
);
2174 ret
= LTTNG_ERR_FATAL
;
2177 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2179 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2180 ret
= event_kernel_enable_event(kchan
, event
,
2181 filter_expression
, filter
);
2182 /* We have passed ownership */
2183 filter_expression
= NULL
;
2185 if (ret
!= LTTNG_OK
) {
2186 if (channel_created
) {
2187 /* Let's not leak a useless channel. */
2188 kernel_destroy_channel(kchan
);
2190 free(filter_expression_a
);
2194 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2195 ret
= event_kernel_enable_event(kchan
, event
,
2196 filter_expression_a
, filter_a
);
2197 /* We have passed ownership */
2198 filter_expression_a
= NULL
;
2200 if (ret
!= LTTNG_OK
) {
2205 case LTTNG_EVENT_PROBE
:
2206 case LTTNG_EVENT_USERSPACE_PROBE
:
2207 case LTTNG_EVENT_FUNCTION
:
2208 case LTTNG_EVENT_FUNCTION_ENTRY
:
2209 case LTTNG_EVENT_TRACEPOINT
:
2210 ret
= event_kernel_enable_event(kchan
, event
,
2211 filter_expression
, filter
);
2212 /* We have passed ownership */
2213 filter_expression
= NULL
;
2215 if (ret
!= LTTNG_OK
) {
2216 if (channel_created
) {
2217 /* Let's not leak a useless channel. */
2218 kernel_destroy_channel(kchan
);
2223 case LTTNG_EVENT_SYSCALL
:
2224 ret
= event_kernel_enable_event(kchan
, event
,
2225 filter_expression
, filter
);
2226 /* We have passed ownership */
2227 filter_expression
= NULL
;
2229 if (ret
!= LTTNG_OK
) {
2234 ret
= LTTNG_ERR_UNK
;
2238 kernel_wait_quiescent();
2241 case LTTNG_DOMAIN_UST
:
2243 struct ltt_ust_channel
*uchan
;
2244 struct ltt_ust_session
*usess
= session
->ust_session
;
2249 * If a non-default channel has been created in the
2250 * session, explicitely require that -c chan_name needs
2253 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2254 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2258 /* Get channel from global UST domain */
2259 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2261 if (uchan
== NULL
) {
2262 /* Create default channel */
2263 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2264 usess
->buffer_type
);
2266 ret
= LTTNG_ERR_FATAL
;
2269 if (lttng_strncpy(attr
->name
, channel_name
,
2270 sizeof(attr
->name
))) {
2271 ret
= LTTNG_ERR_INVALID
;
2275 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2276 if (ret
!= LTTNG_OK
) {
2280 /* Get the newly created channel reference back */
2281 uchan
= trace_ust_find_channel_by_name(
2282 usess
->domain_global
.channels
, channel_name
);
2286 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2288 * Don't allow users to add UST events to channels which
2289 * are assigned to a userspace subdomain (JUL, Log4J,
2292 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2296 if (!internal_event
) {
2298 * Ensure the event name is not reserved for internal
2301 ret
= validate_ust_event_name(event
->name
);
2303 WARN("Userspace event name %s failed validation.",
2305 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2310 /* At this point, the session and channel exist on the tracer */
2311 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2312 filter_expression
, filter
, exclusion
,
2314 /* We have passed ownership */
2315 filter_expression
= NULL
;
2318 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2319 goto already_enabled
;
2320 } else if (ret
!= LTTNG_OK
) {
2325 case LTTNG_DOMAIN_LOG4J
:
2326 case LTTNG_DOMAIN_JUL
:
2327 case LTTNG_DOMAIN_PYTHON
:
2329 const char *default_event_name
, *default_chan_name
;
2331 struct lttng_event uevent
;
2332 struct lttng_domain tmp_dom
;
2333 struct ltt_ust_session
*usess
= session
->ust_session
;
2337 if (!agent_tracing_is_enabled()) {
2338 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2339 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2343 agt
= trace_ust_find_agent(usess
, domain
->type
);
2345 agt
= agent_create(domain
->type
);
2347 ret
= LTTNG_ERR_NOMEM
;
2350 agent_add(agt
, usess
->agents
);
2353 /* Create the default tracepoint. */
2354 memset(&uevent
, 0, sizeof(uevent
));
2355 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2356 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2357 default_event_name
= event_get_default_agent_ust_name(
2359 if (!default_event_name
) {
2360 ret
= LTTNG_ERR_FATAL
;
2363 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2364 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2367 * The domain type is changed because we are about to enable the
2368 * default channel and event for the JUL domain that are hardcoded.
2369 * This happens in the UST domain.
2371 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2372 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2374 switch (domain
->type
) {
2375 case LTTNG_DOMAIN_LOG4J
:
2376 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2378 case LTTNG_DOMAIN_JUL
:
2379 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2381 case LTTNG_DOMAIN_PYTHON
:
2382 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2385 /* The switch/case we are in makes this impossible */
2390 char *filter_expression_copy
= NULL
;
2391 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2394 const size_t filter_size
= sizeof(
2395 struct lttng_filter_bytecode
)
2398 filter_copy
= zmalloc(filter_size
);
2400 ret
= LTTNG_ERR_NOMEM
;
2403 memcpy(filter_copy
, filter
, filter_size
);
2405 filter_expression_copy
=
2406 strdup(filter_expression
);
2407 if (!filter_expression
) {
2408 ret
= LTTNG_ERR_NOMEM
;
2411 if (!filter_expression_copy
|| !filter_copy
) {
2412 free(filter_expression_copy
);
2418 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2419 (char *) default_chan_name
,
2420 &uevent
, filter_expression_copy
,
2421 filter_copy
, NULL
, wpipe
);
2424 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2425 goto already_enabled
;
2426 } else if (ret
!= LTTNG_OK
) {
2430 /* The wild card * means that everything should be enabled. */
2431 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2432 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2435 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2439 filter_expression
= NULL
;
2440 if (ret
!= LTTNG_OK
) {
2447 ret
= LTTNG_ERR_UND
;
2455 free(filter_expression
);
2458 channel_attr_destroy(attr
);
2464 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2465 * We own filter, exclusion, and filter_expression.
2467 int cmd_enable_event(struct ltt_session
*session
,
2468 const struct lttng_domain
*domain
,
2469 char *channel_name
, struct lttng_event
*event
,
2470 char *filter_expression
,
2471 struct lttng_filter_bytecode
*filter
,
2472 struct lttng_event_exclusion
*exclusion
,
2475 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2476 filter_expression
, filter
, exclusion
, wpipe
, false);
2480 * Enable an event which is internal to LTTng. An internal should
2481 * never be made visible to clients and are immune to checks such as
2484 static int cmd_enable_event_internal(struct ltt_session
*session
,
2485 const struct lttng_domain
*domain
,
2486 char *channel_name
, struct lttng_event
*event
,
2487 char *filter_expression
,
2488 struct lttng_filter_bytecode
*filter
,
2489 struct lttng_event_exclusion
*exclusion
,
2492 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2493 filter_expression
, filter
, exclusion
, wpipe
, true);
2497 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2499 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2500 struct lttng_event
**events
)
2503 ssize_t nb_events
= 0;
2506 case LTTNG_DOMAIN_KERNEL
:
2507 nb_events
= kernel_list_events(events
);
2508 if (nb_events
< 0) {
2509 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2513 case LTTNG_DOMAIN_UST
:
2514 nb_events
= ust_app_list_events(events
);
2515 if (nb_events
< 0) {
2516 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2520 case LTTNG_DOMAIN_LOG4J
:
2521 case LTTNG_DOMAIN_JUL
:
2522 case LTTNG_DOMAIN_PYTHON
:
2523 nb_events
= agent_list_events(events
, domain
);
2524 if (nb_events
< 0) {
2525 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2530 ret
= LTTNG_ERR_UND
;
2537 /* Return negative value to differentiate return code */
2542 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2544 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2545 struct lttng_event_field
**fields
)
2548 ssize_t nb_fields
= 0;
2551 case LTTNG_DOMAIN_UST
:
2552 nb_fields
= ust_app_list_event_fields(fields
);
2553 if (nb_fields
< 0) {
2554 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2558 case LTTNG_DOMAIN_KERNEL
:
2559 default: /* fall-through */
2560 ret
= LTTNG_ERR_UND
;
2567 /* Return negative value to differentiate return code */
2571 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2573 return syscall_table_list(events
);
2577 * Command LTTNG_LIST_TRACKER_IDS processed by the client thread.
2579 * Called with session lock held.
2581 int cmd_list_tracker_ids(enum lttng_tracker_type tracker_type
,
2582 struct ltt_session
*session
,
2583 enum lttng_domain_type domain
,
2584 struct lttng_tracker_ids
**ids
)
2589 case LTTNG_DOMAIN_KERNEL
:
2591 struct ltt_kernel_session
*ksess
;
2593 ksess
= session
->kernel_session
;
2594 ret
= kernel_list_tracker_ids(tracker_type
, ksess
, ids
);
2595 if (ret
!= LTTNG_OK
) {
2596 ret
= -LTTNG_ERR_KERN_LIST_FAIL
;
2601 case LTTNG_DOMAIN_UST
:
2603 struct ltt_ust_session
*usess
;
2605 usess
= session
->ust_session
;
2606 ret
= trace_ust_list_tracker_ids(tracker_type
, usess
, ids
);
2607 if (ret
!= LTTNG_OK
) {
2608 ret
= -LTTNG_ERR_UST_LIST_FAIL
;
2613 case LTTNG_DOMAIN_LOG4J
:
2614 case LTTNG_DOMAIN_JUL
:
2615 case LTTNG_DOMAIN_PYTHON
:
2617 ret
= -LTTNG_ERR_UND
;
2622 /* Return negative value to differentiate return code */
2627 * Command LTTNG_START_TRACE processed by the client thread.
2629 * Called with session mutex held.
2631 int cmd_start_trace(struct ltt_session
*session
)
2633 enum lttng_error_code ret
;
2634 unsigned long nb_chan
= 0;
2635 struct ltt_kernel_session
*ksession
;
2636 struct ltt_ust_session
*usess
;
2637 const bool session_rotated_after_last_stop
=
2638 session
->rotated_after_last_stop
;
2639 const bool session_cleared_after_last_stop
=
2640 session
->cleared_after_last_stop
;
2644 /* Ease our life a bit ;) */
2645 ksession
= session
->kernel_session
;
2646 usess
= session
->ust_session
;
2648 /* Is the session already started? */
2649 if (session
->active
) {
2650 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2651 /* Perform nothing */
2655 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2656 !session
->current_trace_chunk
) {
2658 * A rotation was launched while the session was stopped and
2659 * it has not been completed yet. It is not possible to start
2660 * the session since starting the session here would require a
2661 * rotation from "NULL" to a new trace chunk. That rotation
2662 * would overlap with the ongoing rotation, which is not
2665 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2667 ret
= LTTNG_ERR_ROTATION_PENDING
;
2672 * Starting a session without channel is useless since after that it's not
2673 * possible to enable channel thus inform the client.
2675 if (usess
&& usess
->domain_global
.channels
) {
2676 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2679 nb_chan
+= ksession
->channel_count
;
2682 ret
= LTTNG_ERR_NO_CHANNEL
;
2686 session
->active
= 1;
2687 session
->rotated_after_last_stop
= false;
2688 session
->cleared_after_last_stop
= false;
2689 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2690 if (!session
->has_been_started
) {
2691 struct lttng_trace_chunk
*trace_chunk
;
2693 DBG("Creating initial trace chunk of session \"%s\"",
2695 trace_chunk
= session_create_new_trace_chunk(
2696 session
, NULL
, NULL
, NULL
);
2698 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2701 assert(!session
->current_trace_chunk
);
2702 ret
= session_set_trace_chunk(session
, trace_chunk
,
2704 lttng_trace_chunk_put(trace_chunk
);
2706 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2710 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2713 * Rotate existing streams into the new chunk.
2714 * This is a "quiet" rotation has no client has
2715 * explicitly requested this operation.
2717 * There is also no need to wait for the rotation
2718 * to complete as it will happen immediately. No data
2719 * was produced as the session was stopped, so the
2720 * rotation should happen on reception of the command.
2722 ret
= cmd_rotate_session(session
, NULL
, true,
2723 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2724 if (ret
!= LTTNG_OK
) {
2730 /* Kernel tracing */
2731 if (ksession
!= NULL
) {
2732 DBG("Start kernel tracing session %s", session
->name
);
2733 ret
= start_kernel_session(ksession
);
2734 if (ret
!= LTTNG_OK
) {
2739 /* Flag session that trace should start automatically */
2741 int int_ret
= ust_app_start_trace_all(usess
);
2744 ret
= LTTNG_ERR_UST_START_FAIL
;
2750 * Clear the flag that indicates that a rotation was done while the
2751 * session was stopped.
2753 session
->rotated_after_last_stop
= false;
2755 if (session
->rotate_timer_period
) {
2756 int int_ret
= timer_session_rotation_schedule_timer_start(
2757 session
, session
->rotate_timer_period
);
2760 ERR("Failed to enable rotate timer");
2761 ret
= LTTNG_ERR_UNK
;
2769 if (ret
== LTTNG_OK
) {
2770 /* Flag this after a successful start. */
2771 session
->has_been_started
|= 1;
2773 session
->active
= 0;
2774 /* Restore initial state on error. */
2775 session
->rotated_after_last_stop
=
2776 session_rotated_after_last_stop
;
2777 session
->cleared_after_last_stop
=
2778 session_cleared_after_last_stop
;
2785 * Command LTTNG_STOP_TRACE processed by the client thread.
2787 int cmd_stop_trace(struct ltt_session
*session
)
2790 struct ltt_kernel_session
*ksession
;
2791 struct ltt_ust_session
*usess
;
2795 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2797 ksession
= session
->kernel_session
;
2798 usess
= session
->ust_session
;
2800 /* Session is not active. Skip everythong and inform the client. */
2801 if (!session
->active
) {
2802 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2806 ret
= stop_kernel_session(ksession
);
2807 if (ret
!= LTTNG_OK
) {
2811 if (usess
&& usess
->active
) {
2812 ret
= ust_app_stop_trace_all(usess
);
2814 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2819 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2821 /* Flag inactive after a successful stop. */
2822 session
->active
= 0;
2830 * Set the base_path of the session only if subdir of a control uris is set.
2831 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2833 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2835 struct lttng_uri
*uris
)
2840 for (i
= 0; i
< nb_uri
; i
++) {
2841 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2842 uris
[i
].subdir
[0] == '\0') {
2843 /* Not interested in these URIs */
2847 if (session
->base_path
!= NULL
) {
2848 free(session
->base_path
);
2849 session
->base_path
= NULL
;
2852 /* Set session base_path */
2853 session
->base_path
= strdup(uris
[i
].subdir
);
2854 if (!session
->base_path
) {
2855 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2856 uris
[i
].subdir
, session
->name
);
2857 ret
= LTTNG_ERR_NOMEM
;
2860 DBG2("Setting base path \"%s\" for session \"%s\"",
2861 session
->base_path
, session
->name
);
2869 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2871 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2872 struct lttng_uri
*uris
)
2875 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2876 struct ltt_ust_session
*usess
= session
->ust_session
;
2882 /* Can't set consumer URI if the session is active. */
2883 if (session
->active
) {
2884 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2889 * Set the session base path if any. This is done inside
2890 * cmd_set_consumer_uri to preserve backward compatibility of the
2891 * previous session creation api vs the session descriptor api.
2893 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2894 if (ret
!= LTTNG_OK
) {
2898 /* Set the "global" consumer URIs */
2899 for (i
= 0; i
< nb_uri
; i
++) {
2900 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2902 if (ret
!= LTTNG_OK
) {
2907 /* Set UST session URIs */
2908 if (session
->ust_session
) {
2909 for (i
= 0; i
< nb_uri
; i
++) {
2910 ret
= add_uri_to_consumer(session
,
2911 session
->ust_session
->consumer
,
2912 &uris
[i
], LTTNG_DOMAIN_UST
);
2913 if (ret
!= LTTNG_OK
) {
2919 /* Set kernel session URIs */
2920 if (session
->kernel_session
) {
2921 for (i
= 0; i
< nb_uri
; i
++) {
2922 ret
= add_uri_to_consumer(session
,
2923 session
->kernel_session
->consumer
,
2924 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2925 if (ret
!= LTTNG_OK
) {
2932 * Make sure to set the session in output mode after we set URI since a
2933 * session can be created without URL (thus flagged in no output mode).
2935 session
->output_traces
= 1;
2937 ksess
->output_traces
= 1;
2941 usess
->output_traces
= 1;
2952 enum lttng_error_code
set_session_output_from_descriptor(
2953 struct ltt_session
*session
,
2954 const struct lttng_session_descriptor
*descriptor
)
2957 enum lttng_error_code ret_code
= LTTNG_OK
;
2958 enum lttng_session_descriptor_type session_type
=
2959 lttng_session_descriptor_get_type(descriptor
);
2960 enum lttng_session_descriptor_output_type output_type
=
2961 lttng_session_descriptor_get_output_type(descriptor
);
2962 struct lttng_uri uris
[2] = {};
2963 size_t uri_count
= 0;
2965 switch (output_type
) {
2966 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2968 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2969 lttng_session_descriptor_get_local_output_uri(descriptor
,
2973 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2974 lttng_session_descriptor_get_network_output_uris(descriptor
,
2975 &uris
[0], &uris
[1]);
2979 ret_code
= LTTNG_ERR_INVALID
;
2983 switch (session_type
) {
2984 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2986 struct snapshot_output
*new_output
= NULL
;
2988 new_output
= snapshot_output_alloc();
2990 ret_code
= LTTNG_ERR_NOMEM
;
2994 ret
= snapshot_output_init_with_uri(session
,
2995 DEFAULT_SNAPSHOT_MAX_SIZE
,
2996 NULL
, uris
, uri_count
, session
->consumer
,
2997 new_output
, &session
->snapshot
);
2999 ret_code
= (ret
== -ENOMEM
) ?
3000 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3001 snapshot_output_destroy(new_output
);
3004 snapshot_add_output(&session
->snapshot
, new_output
);
3007 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3008 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3010 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
3014 ret_code
= LTTNG_ERR_INVALID
;
3022 enum lttng_error_code
cmd_create_session_from_descriptor(
3023 struct lttng_session_descriptor
*descriptor
,
3024 const lttng_sock_cred
*creds
,
3025 const char *home_path
)
3028 enum lttng_error_code ret_code
;
3029 const char *session_name
;
3030 struct ltt_session
*new_session
= NULL
;
3031 enum lttng_session_descriptor_status descriptor_status
;
3033 session_lock_list();
3035 if (*home_path
!= '/') {
3036 ERR("Home path provided by client is not absolute");
3037 ret_code
= LTTNG_ERR_INVALID
;
3042 descriptor_status
= lttng_session_descriptor_get_session_name(
3043 descriptor
, &session_name
);
3044 switch (descriptor_status
) {
3045 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3047 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3048 session_name
= NULL
;
3051 ret_code
= LTTNG_ERR_INVALID
;
3055 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3057 if (ret_code
!= LTTNG_OK
) {
3061 if (!session_name
) {
3062 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3065 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3070 if (!lttng_session_descriptor_is_output_destination_initialized(
3073 * Only include the session's creation time in the output
3074 * destination if the name of the session itself was
3075 * not auto-generated.
3077 ret_code
= lttng_session_descriptor_set_default_output(
3079 session_name
? &new_session
->creation_time
: NULL
,
3081 if (ret_code
!= LTTNG_OK
) {
3085 new_session
->has_user_specified_directory
=
3086 lttng_session_descriptor_has_output_directory(
3090 switch (lttng_session_descriptor_get_type(descriptor
)) {
3091 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3092 new_session
->snapshot_mode
= 1;
3094 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3095 new_session
->live_timer
=
3096 lttng_session_descriptor_live_get_timer_interval(
3103 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3104 if (ret_code
!= LTTNG_OK
) {
3107 new_session
->consumer
->enabled
= 1;
3108 ret_code
= LTTNG_OK
;
3110 /* Release reference provided by the session_create function. */
3111 session_put(new_session
);
3112 if (ret_code
!= LTTNG_OK
&& new_session
) {
3113 /* Release the global reference on error. */
3114 session_destroy(new_session
);
3116 session_unlock_list();
3120 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3121 struct lttng_session_descriptor
**return_descriptor
)
3124 size_t payload_size
;
3125 struct lttng_dynamic_buffer payload
;
3126 struct lttng_buffer_view home_dir_view
;
3127 struct lttng_buffer_view session_descriptor_view
;
3128 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3129 enum lttng_error_code ret_code
;
3131 lttng_dynamic_buffer_init(&payload
);
3132 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
3134 ret_code
= LTTNG_ERR_INVALID
;
3137 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
3138 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3139 ret_code
= LTTNG_ERR_INVALID
;
3143 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
3144 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
3145 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3147 ret_code
= LTTNG_ERR_NOMEM
;
3151 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3153 ERR("Reception of session descriptor failed, aborting.");
3154 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3158 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3161 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3162 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3164 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3165 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3167 ret
= lttng_session_descriptor_create_from_buffer(
3168 &session_descriptor_view
, &session_descriptor
);
3170 ERR("Failed to create session descriptor from payload of \"create session\" command");
3171 ret_code
= LTTNG_ERR_INVALID
;
3176 * Sets the descriptor's auto-generated properties (name, output) if
3179 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3181 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3182 if (ret_code
!= LTTNG_OK
) {
3186 ret_code
= LTTNG_OK
;
3187 *return_descriptor
= session_descriptor
;
3188 session_descriptor
= NULL
;
3190 lttng_dynamic_buffer_reset(&payload
);
3191 lttng_session_descriptor_destroy(session_descriptor
);
3196 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3197 void *_reply_context
)
3201 const struct cmd_destroy_session_reply_context
*reply_context
=
3203 struct lttng_dynamic_buffer payload
;
3204 struct lttcomm_session_destroy_command_header cmd_header
;
3205 struct lttng_trace_archive_location
*location
= NULL
;
3206 struct lttcomm_lttng_msg llm
= {
3207 .cmd_type
= LTTNG_DESTROY_SESSION
,
3208 .ret_code
= reply_context
->destruction_status
,
3211 sizeof(struct lttcomm_session_destroy_command_header
),
3214 size_t payload_size_before_location
;
3216 lttng_dynamic_buffer_init(&payload
);
3218 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3220 ERR("Failed to append session destruction message");
3224 cmd_header
.rotation_state
=
3225 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3226 session
->rotation_state
:
3227 LTTNG_ROTATION_STATE_NO_ROTATION
);
3228 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3229 sizeof(cmd_header
));
3231 ERR("Failed to append session destruction command header");
3235 if (!reply_context
->implicit_rotation_on_destroy
) {
3236 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3240 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3241 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3246 location
= session_get_trace_archive_location(session
);
3248 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3253 payload_size_before_location
= payload
.size
;
3254 comm_ret
= lttng_trace_archive_location_serialize(location
,
3257 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3261 /* Update the message to indicate the location's length. */
3262 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3263 payload
.size
- payload_size_before_location
;
3265 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3266 payload
.data
, payload
.size
);
3267 if (comm_ret
!= (ssize_t
) payload
.size
) {
3268 ERR("Failed to send result of the destruction of session \"%s\" to client",
3272 ret
= close(reply_context
->reply_sock_fd
);
3274 PERROR("Failed to close client socket in deferred session destroy reply");
3276 lttng_dynamic_buffer_reset(&payload
);
3277 free(_reply_context
);
3281 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3283 * Called with session lock held.
3285 int cmd_destroy_session(struct ltt_session
*session
,
3286 struct notification_thread_handle
*notification_thread_handle
,
3290 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3291 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3294 reply_context
= zmalloc(sizeof(*reply_context
));
3295 if (!reply_context
) {
3296 ret
= LTTNG_ERR_NOMEM
;
3299 reply_context
->reply_sock_fd
= *sock_fd
;
3305 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3307 if (session
->active
) {
3308 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3310 ret
= cmd_stop_trace(session
);
3311 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3312 /* Carry on with the destruction of the session. */
3313 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3314 session
->name
, lttng_strerror(-ret
));
3315 destruction_last_error
= ret
;
3319 if (session
->rotation_schedule_timer_enabled
) {
3320 if (timer_session_rotation_schedule_timer_stop(
3322 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3324 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3328 if (session
->rotate_size
) {
3329 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3330 session
->rotate_size
= 0;
3333 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3335 * Perform a last rotation on destruction if rotations have
3336 * occurred during the session's lifetime.
3338 ret
= cmd_rotate_session(session
, NULL
, false,
3339 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3340 if (ret
!= LTTNG_OK
) {
3341 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3342 session
->name
, lttng_strerror(-ret
));
3343 destruction_last_error
= -ret
;
3345 if (reply_context
) {
3346 reply_context
->implicit_rotation_on_destroy
= true;
3348 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3350 * The user has not triggered a session rotation. However, to
3351 * ensure all data has been consumed, the session is rotated
3352 * to a 'null' trace chunk before it is destroyed.
3354 * This is a "quiet" rotation meaning that no notification is
3355 * emitted and no renaming of the current trace chunk takes
3358 ret
= cmd_rotate_session(session
, NULL
, true,
3359 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3360 if (ret
!= LTTNG_OK
) {
3361 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3362 session
->name
, lttng_strerror(-ret
));
3363 destruction_last_error
= -ret
;
3367 if (session
->shm_path
[0]) {
3369 * When a session is created with an explicit shm_path,
3370 * the consumer daemon will create its shared memory files
3371 * at that location and will *not* unlink them. This is normal
3372 * as the intention of that feature is to make it possible
3373 * to retrieve the content of those files should a crash occur.
3375 * To ensure the content of those files can be used, the
3376 * sessiond daemon will replicate the content of the metadata
3377 * cache in a metadata file.
3379 * On clean-up, it is expected that the consumer daemon will
3380 * unlink the shared memory files and that the session daemon
3381 * will unlink the metadata file. Then, the session's directory
3382 * in the shm path can be removed.
3384 * Unfortunately, a flaw in the design of the sessiond's and
3385 * consumerd's tear down of channels makes it impossible to
3386 * determine when the sessiond _and_ the consumerd have both
3387 * destroyed their representation of a channel. For one, the
3388 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3389 * callbacks in both daemons.
3391 * However, it is also impossible for the sessiond to know when
3392 * the consumer daemon is done destroying its channel(s) since
3393 * it occurs as a reaction to the closing of the channel's file
3394 * descriptor. There is no resulting communication initiated
3395 * from the consumerd to the sessiond to confirm that the
3396 * operation is completed (and was successful).
3398 * Until this is all fixed, the session daemon checks for the
3399 * removal of the session's shm path which makes it possible
3400 * to safely advertise a session as having been destroyed.
3402 * Prior to this fix, it was not possible to reliably save
3403 * a session making use of the --shm-path option, destroy it,
3404 * and load it again. This is because the creation of the
3405 * session would fail upon seeing the session's shm path
3406 * already in existence.
3408 * Note that none of the error paths in the check for the
3409 * directory's existence return an error. This is normal
3410 * as there isn't much that can be done. The session will
3411 * be destroyed properly, except that we can't offer the
3412 * guarantee that the same session can be re-created.
3414 current_completion_handler
= &destroy_completion_handler
.handler
;
3415 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3417 sizeof(destroy_completion_handler
.shm_path
));
3422 * The session is destroyed. However, note that the command context
3423 * still holds a reference to the session, thus delaying its destruction
3424 * _at least_ up to the point when that reference is released.
3426 session_destroy(session
);
3427 if (reply_context
) {
3428 reply_context
->destruction_status
= destruction_last_error
;
3429 ret
= session_add_destroy_notifier(session
,
3430 cmd_destroy_session_reply
,
3431 (void *) reply_context
);
3433 ret
= LTTNG_ERR_FATAL
;
3445 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3447 int cmd_register_consumer(struct ltt_session
*session
,
3448 enum lttng_domain_type domain
, const char *sock_path
,
3449 struct consumer_data
*cdata
)
3452 struct consumer_socket
*socket
= NULL
;
3459 case LTTNG_DOMAIN_KERNEL
:
3461 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3465 /* Can't register a consumer if there is already one */
3466 if (ksess
->consumer_fds_sent
!= 0) {
3467 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3471 sock
= lttcomm_connect_unix_sock(sock_path
);
3473 ret
= LTTNG_ERR_CONNECT_FAIL
;
3476 cdata
->cmd_sock
= sock
;
3478 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3479 if (socket
== NULL
) {
3482 PERROR("close register consumer");
3484 cdata
->cmd_sock
= -1;
3485 ret
= LTTNG_ERR_FATAL
;
3489 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3490 if (socket
->lock
== NULL
) {
3491 PERROR("zmalloc pthread mutex");
3492 ret
= LTTNG_ERR_FATAL
;
3495 pthread_mutex_init(socket
->lock
, NULL
);
3496 socket
->registered
= 1;
3499 consumer_add_socket(socket
, ksess
->consumer
);
3502 pthread_mutex_lock(&cdata
->pid_mutex
);
3504 pthread_mutex_unlock(&cdata
->pid_mutex
);
3509 /* TODO: Userspace tracing */
3510 ret
= LTTNG_ERR_UND
;
3518 consumer_destroy_socket(socket
);
3524 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3526 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3527 struct lttng_domain
**domains
)
3532 struct lttng_ht_iter iter
;
3534 if (session
->kernel_session
!= NULL
) {
3535 DBG3("Listing domains found kernel domain");
3539 if (session
->ust_session
!= NULL
) {
3540 DBG3("Listing domains found UST global domain");
3544 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3546 if (agt
->being_used
) {
3557 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3558 if (*domains
== NULL
) {
3559 ret
= LTTNG_ERR_FATAL
;
3563 if (session
->kernel_session
!= NULL
) {
3564 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3566 /* Kernel session buffer type is always GLOBAL */
3567 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3572 if (session
->ust_session
!= NULL
) {
3573 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3574 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3578 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3580 if (agt
->being_used
) {
3581 (*domains
)[index
].type
= agt
->domain
;
3582 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3592 /* Return negative value to differentiate return code */
3598 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3600 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3601 struct ltt_session
*session
, struct lttng_channel
**channels
)
3603 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3606 case LTTNG_DOMAIN_KERNEL
:
3607 if (session
->kernel_session
!= NULL
) {
3608 nb_chan
= session
->kernel_session
->channel_count
;
3610 DBG3("Number of kernel channels %zd", nb_chan
);
3612 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3616 case LTTNG_DOMAIN_UST
:
3617 if (session
->ust_session
!= NULL
) {
3619 nb_chan
= lttng_ht_get_count(
3620 session
->ust_session
->domain_global
.channels
);
3623 DBG3("Number of UST global channels %zd", nb_chan
);
3625 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3630 ret
= -LTTNG_ERR_UND
;
3635 const size_t channel_size
= sizeof(struct lttng_channel
) +
3636 sizeof(struct lttng_channel_extended
);
3637 struct lttng_channel_extended
*channel_exts
;
3639 payload_size
= nb_chan
* channel_size
;
3640 *channels
= zmalloc(payload_size
);
3641 if (*channels
== NULL
) {
3642 ret
= -LTTNG_ERR_FATAL
;
3646 channel_exts
= ((void *) *channels
) +
3647 (nb_chan
* sizeof(struct lttng_channel
));
3648 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3649 if (ret
!= LTTNG_OK
) {
3664 * Command LTTNG_LIST_EVENTS processed by the client thread.
3666 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3667 struct ltt_session
*session
, char *channel_name
,
3668 struct lttng_event
**events
, size_t *total_size
)
3671 ssize_t nb_event
= 0;
3674 case LTTNG_DOMAIN_KERNEL
:
3675 if (session
->kernel_session
!= NULL
) {
3676 nb_event
= list_lttng_kernel_events(channel_name
,
3677 session
->kernel_session
, events
,
3681 case LTTNG_DOMAIN_UST
:
3683 if (session
->ust_session
!= NULL
) {
3684 nb_event
= list_lttng_ust_global_events(channel_name
,
3685 &session
->ust_session
->domain_global
, events
,
3690 case LTTNG_DOMAIN_LOG4J
:
3691 case LTTNG_DOMAIN_JUL
:
3692 case LTTNG_DOMAIN_PYTHON
:
3693 if (session
->ust_session
) {
3694 struct lttng_ht_iter iter
;
3698 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3699 &iter
.iter
, agt
, node
.node
) {
3700 if (agt
->domain
== domain
) {
3701 nb_event
= list_lttng_agent_events(
3711 ret
= LTTNG_ERR_UND
;
3718 /* Return negative value to differentiate return code */
3723 * Using the session list, filled a lttng_session array to send back to the
3724 * client for session listing.
3726 * The session list lock MUST be acquired before calling this function. Use
3727 * session_lock_list() and session_unlock_list().
3729 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3730 size_t session_count
, uid_t uid
, gid_t gid
)
3734 struct ltt_session
*session
;
3735 struct ltt_session_list
*list
= session_get_list();
3736 struct lttng_session_extended
*extended
=
3737 (typeof(extended
)) (&sessions
[session_count
]);
3739 DBG("Getting all available session for UID %d GID %d",
3742 * Iterate over session list and append data after the control struct in
3745 cds_list_for_each_entry(session
, &list
->head
, list
) {
3746 if (!session_get(session
)) {
3750 * Only list the sessions the user can control.
3752 if (!session_access_ok(session
, uid
, gid
) ||
3753 session
->destroyed
) {
3754 session_put(session
);
3758 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3759 struct ltt_ust_session
*usess
= session
->ust_session
;
3761 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3762 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3763 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3764 ret
= build_network_session_path(sessions
[i
].path
,
3765 sizeof(sessions
[i
].path
), session
);
3767 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3768 session
->consumer
->dst
.session_root_path
);
3771 PERROR("snprintf session path");
3772 session_put(session
);
3776 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3777 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3778 sessions
[i
].enabled
= session
->active
;
3779 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3780 sessions
[i
].live_timer_interval
= session
->live_timer
;
3781 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3782 extended
[i
].creation_time
.is_set
= 1;
3784 session_put(session
);
3789 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3790 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3792 int cmd_data_pending(struct ltt_session
*session
)
3795 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3796 struct ltt_ust_session
*usess
= session
->ust_session
;
3800 DBG("Data pending for session %s", session
->name
);
3802 /* Session MUST be stopped to ask for data availability. */
3803 if (session
->active
) {
3804 ret
= LTTNG_ERR_SESSION_STARTED
;
3808 * If stopped, just make sure we've started before else the above call
3809 * will always send that there is data pending.
3811 * The consumer assumes that when the data pending command is received,
3812 * the trace has been started before or else no output data is written
3813 * by the streams which is a condition for data pending. So, this is
3814 * *VERY* important that we don't ask the consumer before a start
3817 if (!session
->has_been_started
) {
3823 /* A rotation is still pending, we have to wait. */
3824 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3825 DBG("Rotate still pending for session %s", session
->name
);
3830 if (ksess
&& ksess
->consumer
) {
3831 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3833 /* Data is still being extracted for the kernel. */
3838 if (usess
&& usess
->consumer
) {
3839 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3841 /* Data is still being extracted for the kernel. */
3846 /* Data is ready to be read by a viewer */
3854 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3856 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3858 int cmd_snapshot_add_output(struct ltt_session
*session
,
3859 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3862 struct snapshot_output
*new_output
;
3867 DBG("Cmd snapshot add output for session %s", session
->name
);
3870 * Can't create an output if the session is not set in no-output mode.
3872 if (session
->output_traces
) {
3873 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3877 if (session
->has_non_mmap_channel
) {
3878 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3882 /* Only one output is allowed until we have the "tee" feature. */
3883 if (session
->snapshot
.nb_output
== 1) {
3884 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3888 new_output
= snapshot_output_alloc();
3890 ret
= LTTNG_ERR_NOMEM
;
3894 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3895 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3896 &session
->snapshot
);
3898 if (ret
== -ENOMEM
) {
3899 ret
= LTTNG_ERR_NOMEM
;
3901 ret
= LTTNG_ERR_INVALID
;
3907 snapshot_add_output(&session
->snapshot
, new_output
);
3909 *id
= new_output
->id
;
3916 snapshot_output_destroy(new_output
);
3922 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3924 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3926 int cmd_snapshot_del_output(struct ltt_session
*session
,
3927 const struct lttng_snapshot_output
*output
)
3930 struct snapshot_output
*sout
= NULL
;
3938 * Permission denied to create an output if the session is not
3939 * set in no output mode.
3941 if (session
->output_traces
) {
3942 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3947 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3949 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3950 } else if (*output
->name
!= '\0') {
3951 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3953 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3956 ret
= LTTNG_ERR_INVALID
;
3960 snapshot_delete_output(&session
->snapshot
, sout
);
3961 snapshot_output_destroy(sout
);
3970 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3972 * If no output is available, outputs is untouched and 0 is returned.
3974 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3976 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3977 struct lttng_snapshot_output
**outputs
)
3980 struct lttng_snapshot_output
*list
= NULL
;
3981 struct lttng_ht_iter iter
;
3982 struct snapshot_output
*output
;
3987 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3990 * Permission denied to create an output if the session is not
3991 * set in no output mode.
3993 if (session
->output_traces
) {
3994 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3998 if (session
->snapshot
.nb_output
== 0) {
4003 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4005 ret
= -LTTNG_ERR_NOMEM
;
4009 /* Copy list from session to the new list object. */
4011 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4012 output
, node
.node
) {
4013 assert(output
->consumer
);
4014 list
[idx
].id
= output
->id
;
4015 list
[idx
].max_size
= output
->max_size
;
4016 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4017 sizeof(list
[idx
].name
))) {
4018 ret
= -LTTNG_ERR_INVALID
;
4021 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4022 if (lttng_strncpy(list
[idx
].ctrl_url
,
4023 output
->consumer
->dst
.session_root_path
,
4024 sizeof(list
[idx
].ctrl_url
))) {
4025 ret
= -LTTNG_ERR_INVALID
;
4030 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4031 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4033 ret
= -LTTNG_ERR_NOMEM
;
4038 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4039 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4041 ret
= -LTTNG_ERR_NOMEM
;
4050 ret
= session
->snapshot
.nb_output
;
4059 * Check if we can regenerate the metadata for this session.
4060 * Only kernel, UST per-uid and non-live sessions are supported.
4062 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4065 int check_regenerate_metadata_support(struct ltt_session
*session
)
4071 if (session
->live_timer
!= 0) {
4072 ret
= LTTNG_ERR_LIVE_SESSION
;
4075 if (!session
->active
) {
4076 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4079 if (session
->ust_session
) {
4080 switch (session
->ust_session
->buffer_type
) {
4081 case LTTNG_BUFFER_PER_UID
:
4083 case LTTNG_BUFFER_PER_PID
:
4084 ret
= LTTNG_ERR_PER_PID_SESSION
;
4088 ret
= LTTNG_ERR_UNK
;
4092 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4093 session
->consumer
->relay_minor_version
< 8) {
4094 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4104 int clear_metadata_file(int fd
)
4109 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4110 if (lseek_ret
< 0) {
4116 ret
= ftruncate(fd
, 0);
4118 PERROR("ftruncate");
4127 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4130 struct buffer_reg_uid
*uid_reg
= NULL
;
4131 struct buffer_reg_session
*session_reg
= NULL
;
4134 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4135 struct ust_registry_session
*registry
;
4136 struct ust_registry_channel
*chan
;
4137 struct lttng_ht_iter iter_chan
;
4139 session_reg
= uid_reg
->registry
;
4140 registry
= session_reg
->reg
.ust
;
4142 pthread_mutex_lock(®istry
->lock
);
4143 registry
->metadata_len_sent
= 0;
4144 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4145 registry
->metadata_len
= 0;
4146 registry
->metadata_version
++;
4147 if (registry
->metadata_fd
> 0) {
4148 /* Clear the metadata file's content. */
4149 ret
= clear_metadata_file(registry
->metadata_fd
);
4151 pthread_mutex_unlock(®istry
->lock
);
4156 ret
= ust_metadata_session_statedump(registry
, NULL
,
4157 registry
->major
, registry
->minor
);
4159 pthread_mutex_unlock(®istry
->lock
);
4160 ERR("Failed to generate session metadata (err = %d)",
4164 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4166 struct ust_registry_event
*event
;
4167 struct lttng_ht_iter iter_event
;
4169 ret
= ust_metadata_channel_statedump(registry
, chan
);
4171 pthread_mutex_unlock(®istry
->lock
);
4172 ERR("Failed to generate channel metadata "
4176 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4178 ret
= ust_metadata_event_statedump(registry
,
4181 pthread_mutex_unlock(®istry
->lock
);
4182 ERR("Failed to generate event metadata "
4188 pthread_mutex_unlock(®istry
->lock
);
4197 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4199 * Ask the consumer to truncate the existing metadata file(s) and
4200 * then regenerate the metadata. Live and per-pid sessions are not
4201 * supported and return an error.
4203 * Return 0 on success or else a LTTNG_ERR code.
4205 int cmd_regenerate_metadata(struct ltt_session
*session
)
4211 ret
= check_regenerate_metadata_support(session
);
4216 if (session
->kernel_session
) {
4217 ret
= kernctl_session_regenerate_metadata(
4218 session
->kernel_session
->fd
);
4220 ERR("Failed to regenerate the kernel metadata");
4225 if (session
->ust_session
) {
4226 ret
= ust_regenerate_metadata(session
->ust_session
);
4228 ERR("Failed to regenerate the UST metadata");
4232 DBG("Cmd metadata regenerate for session %s", session
->name
);
4240 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4242 * Ask the tracer to regenerate a new statedump.
4244 * Return 0 on success or else a LTTNG_ERR code.
4246 int cmd_regenerate_statedump(struct ltt_session
*session
)
4252 if (!session
->active
) {
4253 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4257 if (session
->kernel_session
) {
4258 ret
= kernctl_session_regenerate_statedump(
4259 session
->kernel_session
->fd
);
4261 * Currently, the statedump in kernel can only fail if out
4265 if (ret
== -ENOMEM
) {
4266 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4268 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4270 ERR("Failed to regenerate the kernel statedump");
4275 if (session
->ust_session
) {
4276 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4278 * Currently, the statedump in UST always returns 0.
4281 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4282 ERR("Failed to regenerate the UST statedump");
4286 DBG("Cmd regenerate statedump for session %s", session
->name
);
4293 /* TODO: is this the best place to perform this? (code wise) */
4295 * Set sock to -1 if reception of more information is not necessary e.g on
4296 * unregister. TODO find a better way.
4298 * On success LTTNG_OK. On error, returns lttng_error code.
4300 static enum lttng_error_code
prepare_trigger_object(struct lttng_trigger
*trigger
, int sock
)
4302 enum lttng_error_code ret
;
4303 /* Internal object of the trigger might have to "generate" and
4304 * "populate" internal field e.g filter bytecode
4306 struct lttng_condition
*condition
= NULL
;
4307 condition
= lttng_trigger_get_condition(trigger
);
4309 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4313 switch (lttng_condition_get_type(condition
)) {
4314 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
4316 struct lttng_event_rule
*event_rule
;
4317 const struct lttng_credentials
*credential
= lttng_trigger_get_credentials(trigger
);
4318 lttng_condition_event_rule_get_rule_no_const(
4319 condition
, &event_rule
);
4320 ret
= lttng_event_rule_populate(event_rule
, credential
->uid
, credential
->gid
);
4321 if (ret
!= LTTNG_OK
) {
4325 switch (lttng_event_rule_get_type(event_rule
)) {
4326 case LTTNG_EVENT_RULE_TYPE_UPROBE
:
4329 struct lttng_userspace_probe_location
*location
= lttng_event_rule_uprobe_get_location_no_const(event_rule
);
4332 /* Nothing to receive */
4336 * Receive the file descriptor to the target binary from
4339 DBG("Receiving userspace probe target FD from client ...");
4340 ret
= lttcomm_recv_fds_unix_sock(sock
, &fd
, 1);
4342 DBG("Nothing recv() from client userspace probe fd... continuing");
4343 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
4348 * Set the file descriptor received from the client
4349 * through the unix socket in the probe location.
4351 ret
= lttng_userspace_probe_location_set_binary_fd(
4354 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
4377 /* Caller must call lttng_destroy_trigger on the returned trigger object */
4378 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4379 struct notification_thread_handle
*notification_thread
,
4380 struct lttng_trigger
**return_trigger
)
4384 ssize_t sock_recv_len
;
4385 struct lttng_trigger
*trigger
= NULL
;
4386 struct lttng_buffer_view view
;
4387 struct lttng_dynamic_buffer trigger_buffer
;
4389 lttng_dynamic_buffer_init(&trigger_buffer
);
4390 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4391 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4393 ret
= LTTNG_ERR_NOMEM
;
4397 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4399 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4400 ERR("Failed to receive \"register trigger\" command payload");
4401 /* TODO: should this be a new error enum ? */
4402 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4406 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4407 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4409 ERR("Invalid trigger payload received in \"register trigger\" command");
4410 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4414 /* Set the trigger credential */
4415 lttng_trigger_set_credentials(trigger
, cmd_ctx
->creds
.uid
, cmd_ctx
->creds
.gid
);
4417 /* Prepare internal trigger object if needed on reception.
4418 * Handles also special treatment for certain internal object of the
4419 * trigger (e.g uprobe event rule binary fd.
4421 ret
= prepare_trigger_object(trigger
, sock
);
4422 if (ret
!= LTTNG_OK
) {
4427 * Since we return the trigger object, take a reference to it
4428 * Caller is responsible for calling lttng_destroy_trigger on it.
4429 * This thread does not OWN the trigger.
4431 lttng_trigger_get(trigger
);
4433 /* Inform the notification thread */
4434 ret
= notification_thread_command_register_trigger(notification_thread
,
4436 if (ret
!= LTTNG_OK
) {
4440 /* Synchronize tracers, only if needed */
4441 /* TODO: maybe extract somewhere else */
4443 struct lttng_condition
*condition
= NULL
;
4444 condition
= lttng_trigger_get_condition(trigger
);
4446 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4450 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
4451 const struct lttng_event_rule
*rule
= NULL
;
4452 (void) lttng_condition_event_rule_get_rule(condition
, &rule
);
4454 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4457 if (lttng_event_rule_get_domain_type(rule
) == LTTNG_DOMAIN_KERNEL
) {
4458 /* TODO: get the token value from the
4459 * notification thread and only perform an
4460 * enable and a disable.... This is NOT
4463 kernel_update_tokens();
4465 /* TODO: get the token value from the
4466 * notification thread and only perform an
4467 * enable and a disable.... This is NOT
4470 ust_app_global_update_all_tokens();
4471 /* Agent handling */
4472 if (lttng_event_rule_is_agent(rule
)) {
4474 const char *pattern
;
4475 enum lttng_domain_type domain_type
;
4476 domain_type
= lttng_event_rule_get_domain_type(
4478 (void) lttng_event_rule_tracepoint_get_pattern(
4480 agt
= trigger_find_agent(domain_type
);
4482 agt
= agent_create(domain_type
);
4484 ret
= LTTNG_ERR_NOMEM
;
4487 agent_add(agt
, trigger_agents_ht_by_domain
);
4490 ret
= trigger_agent_enable(
4492 if (ret
!= LTTNG_OK
) {
4500 /* Return an image of the updated object to the client */
4501 *return_trigger
= trigger
;
4502 /* Ownership of trigger was transferred to caller. */
4505 lttng_trigger_destroy(trigger
);
4506 lttng_dynamic_buffer_reset(&trigger_buffer
);
4510 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4511 struct notification_thread_handle
*notification_thread
)
4515 ssize_t sock_recv_len
;
4516 struct lttng_trigger
*trigger
= NULL
;
4517 struct lttng_buffer_view view
;
4518 struct lttng_dynamic_buffer trigger_buffer
;
4520 lttng_dynamic_buffer_init(&trigger_buffer
);
4521 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4522 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4524 ret
= LTTNG_ERR_NOMEM
;
4528 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4530 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4531 ERR("Failed to receive \"unregister trigger\" command payload");
4532 /* TODO: should this be a new error enum ? */
4533 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4537 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4538 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4540 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4541 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4545 lttng_trigger_set_credentials(trigger
, cmd_ctx
->creds
.uid
, cmd_ctx
->creds
.gid
);
4547 /* TODO: forgotting this bited me for agent since the filter is
4548 * genereted on this side. Wonder if we could find a way to detect when
4552 /* Prepare internal trigger object if needed on reception.
4553 * Handles also special treatment for certain internal object of the
4554 * trigger (e.g uprobe event rule binary fd.
4556 ret
= prepare_trigger_object(trigger
, -1);
4557 if (ret
!= LTTNG_OK
) {
4561 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4564 /* Synchronize tracers, only if needed */
4565 /* TODO: maybe extract somewhere else */
4567 struct lttng_condition
*condition
= NULL
;
4568 condition
= lttng_trigger_get_condition(trigger
);
4570 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4574 if (lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
) {
4575 const struct lttng_event_rule
*rule
= NULL
;
4576 (void) lttng_condition_event_rule_get_rule(condition
, &rule
);
4578 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4581 if (lttng_event_rule_get_domain_type(rule
) == LTTNG_DOMAIN_KERNEL
) {
4582 /* TODO: get the token value from the
4583 * notification thread and only perform an
4584 * enable and a disable.... This is NOT
4587 kernel_update_tokens();
4589 /* TODO: get the token value from the
4590 * notification thread and only perform an
4591 * enable and a disable.... This is NOT
4594 ust_app_global_update_all_tokens();
4595 if (lttng_event_rule_is_agent(rule
)) {
4597 const char *pattern
;
4598 enum lttng_domain_type domain_type
;
4600 domain_type
= lttng_event_rule_get_domain_type(
4602 (void) lttng_event_rule_tracepoint_get_pattern(
4605 agt
= trigger_find_agent(domain_type
);
4607 ret
= LTTNG_ERR_UST_EVENT_NOT_FOUND
;
4610 ret
= trigger_agent_disable(
4612 if (ret
!= LTTNG_OK
) {
4621 lttng_trigger_destroy(trigger
);
4622 lttng_dynamic_buffer_reset(&trigger_buffer
);
4626 int cmd_list_triggers(struct command_ctx
*cmd_ctx
, int sock
,
4627 struct notification_thread_handle
*notification_thread
,
4628 struct lttng_triggers
**return_triggers
)
4631 enum lttng_error_code ret_code
;
4632 struct lttng_triggers
*triggers
= NULL
;
4634 /* Get list of token trigger from the notification thread here */
4635 ret_code
= notification_thread_command_list_triggers(notification_thread_handle
, cmd_ctx
->creds
.uid
, cmd_ctx
->creds
.gid
, &triggers
);
4636 if (ret_code
!= LTTNG_OK
) {
4641 /* Return a "view" of the current triggers */
4642 *return_triggers
= triggers
;
4646 lttng_triggers_destroy(triggers
);
4650 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4651 * snapshot output is *not* set with a remote destination.
4653 * Return LTTNG_OK on success or a LTTNG_ERR code.
4655 static enum lttng_error_code
set_relayd_for_snapshot(
4656 struct consumer_output
*output
,
4657 const struct ltt_session
*session
)
4659 enum lttng_error_code status
= LTTNG_OK
;
4660 struct lttng_ht_iter iter
;
4661 struct consumer_socket
*socket
;
4662 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4663 const char *base_path
;
4668 DBG2("Set relayd object from snapshot output");
4670 if (session
->current_trace_chunk
) {
4671 enum lttng_trace_chunk_status chunk_status
=
4672 lttng_trace_chunk_get_id(
4673 session
->current_trace_chunk
,
4674 ¤t_chunk_id
.value
);
4676 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4677 current_chunk_id
.is_set
= true;
4679 ERR("Failed to get current trace chunk id");
4680 status
= LTTNG_ERR_UNK
;
4685 /* Ignore if snapshot consumer output is not network. */
4686 if (output
->type
!= CONSUMER_DST_NET
) {
4691 * The snapshot record URI base path overrides the session
4694 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4695 base_path
= output
->dst
.net
.control
.subdir
;
4697 base_path
= session
->base_path
;
4701 * For each consumer socket, create and send the relayd object of the
4705 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4706 socket
, node
.node
) {
4707 pthread_mutex_lock(socket
->lock
);
4708 status
= send_consumer_relayd_sockets(0, session
->id
,
4710 session
->name
, session
->hostname
,
4712 session
->live_timer
,
4713 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4714 session
->creation_time
,
4715 session
->name_contains_creation_time
);
4716 pthread_mutex_unlock(socket
->lock
);
4717 if (status
!= LTTNG_OK
) {
4729 * Record a kernel snapshot.
4731 * Return LTTNG_OK on success or a LTTNG_ERR code.
4733 static enum lttng_error_code
record_kernel_snapshot(
4734 struct ltt_kernel_session
*ksess
,
4735 const struct consumer_output
*output
,
4736 const struct ltt_session
*session
,
4737 int wait
, uint64_t nb_packets_per_stream
)
4739 enum lttng_error_code status
;
4745 status
= kernel_snapshot_record(
4746 ksess
, output
, wait
, nb_packets_per_stream
);
4751 * Record a UST snapshot.
4753 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4755 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4756 const struct consumer_output
*output
,
4757 const struct ltt_session
*session
,
4758 int wait
, uint64_t nb_packets_per_stream
)
4760 enum lttng_error_code status
;
4766 status
= ust_app_snapshot_record(
4767 usess
, output
, wait
, nb_packets_per_stream
);
4772 uint64_t get_session_size_one_more_packet_per_stream(
4773 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4775 uint64_t tot_size
= 0;
4777 if (session
->kernel_session
) {
4778 struct ltt_kernel_channel
*chan
;
4779 const struct ltt_kernel_session
*ksess
=
4780 session
->kernel_session
;
4782 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4783 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4785 * Don't take channel into account if we
4786 * already grab all its packets.
4790 tot_size
+= chan
->channel
->attr
.subbuf_size
4791 * chan
->stream_count
;
4795 if (session
->ust_session
) {
4796 const struct ltt_ust_session
*usess
= session
->ust_session
;
4798 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4806 * Calculate the number of packets we can grab from each stream that
4807 * fits within the overall snapshot max size.
4809 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4810 * the number of packets per stream.
4812 * TODO: this approach is not perfect: we consider the worse case
4813 * (packet filling the sub-buffers) as an upper bound, but we could do
4814 * better if we do this calculation while we actually grab the packet
4815 * content: we would know how much padding we don't actually store into
4818 * This algorithm is currently bounded by the number of packets per
4821 * Since we call this algorithm before actually grabbing the data, it's
4822 * an approximation: for instance, applications could appear/disappear
4823 * in between this call and actually grabbing data.
4826 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4830 uint64_t cur_nb_packets
= 0;
4833 return 0; /* Infinite */
4836 size_left
= max_size
;
4838 uint64_t one_more_packet_tot_size
;
4840 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4841 session
, cur_nb_packets
);
4842 if (!one_more_packet_tot_size
) {
4843 /* We are already grabbing all packets. */
4846 size_left
-= one_more_packet_tot_size
;
4847 if (size_left
< 0) {
4852 if (!cur_nb_packets
&& size_left
!= max_size
) {
4853 /* Not enough room to grab one packet of each stream, error. */
4856 return cur_nb_packets
;
4860 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4861 const struct snapshot_output
*snapshot_output
, int wait
)
4863 int64_t nb_packets_per_stream
;
4864 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4866 enum lttng_error_code ret_code
= LTTNG_OK
;
4867 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4868 struct consumer_output
*original_ust_consumer_output
= NULL
;
4869 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4870 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4871 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4873 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4875 snapshot_output
->name
,
4876 snapshot_output
->datetime
,
4877 snapshot_output
->nb_snapshot
);
4878 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4879 ERR("Failed to format snapshot name");
4880 ret_code
= LTTNG_ERR_INVALID
;
4883 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4884 snapshot_output
->name
, session
->name
,
4885 snapshot_chunk_name
);
4886 if (!session
->kernel_session
&& !session
->ust_session
) {
4887 ERR("Failed to record snapshot as no channels exist");
4888 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4892 if (session
->kernel_session
) {
4893 original_kernel_consumer_output
=
4894 session
->kernel_session
->consumer
;
4895 snapshot_kernel_consumer_output
=
4896 consumer_copy_output(snapshot_output
->consumer
);
4897 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
4898 snapshot_chunk_name
);
4899 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4900 original_kernel_consumer_output
);
4902 ERR("Failed to copy consumer sockets from snapshot output configuration");
4903 ret_code
= LTTNG_ERR_NOMEM
;
4906 ret_code
= set_relayd_for_snapshot(
4907 snapshot_kernel_consumer_output
, session
);
4908 if (ret_code
!= LTTNG_OK
) {
4909 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4912 session
->kernel_session
->consumer
=
4913 snapshot_kernel_consumer_output
;
4915 if (session
->ust_session
) {
4916 original_ust_consumer_output
= session
->ust_session
->consumer
;
4917 snapshot_ust_consumer_output
=
4918 consumer_copy_output(snapshot_output
->consumer
);
4919 strcpy(snapshot_ust_consumer_output
->chunk_path
,
4920 snapshot_chunk_name
);
4921 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4922 original_ust_consumer_output
);
4924 ERR("Failed to copy consumer sockets from snapshot output configuration");
4925 ret_code
= LTTNG_ERR_NOMEM
;
4928 ret_code
= set_relayd_for_snapshot(
4929 snapshot_ust_consumer_output
, session
);
4930 if (ret_code
!= LTTNG_OK
) {
4931 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4934 session
->ust_session
->consumer
=
4935 snapshot_ust_consumer_output
;
4938 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4939 snapshot_kernel_consumer_output
?:
4940 snapshot_ust_consumer_output
,
4941 consumer_output_get_base_path(
4942 snapshot_output
->consumer
),
4943 snapshot_chunk_name
);
4944 if (!snapshot_trace_chunk
) {
4945 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4947 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4950 assert(!session
->current_trace_chunk
);
4951 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4952 lttng_trace_chunk_put(snapshot_trace_chunk
);
4953 snapshot_trace_chunk
= NULL
;
4955 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4957 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4961 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4962 snapshot_output
->max_size
);
4963 if (nb_packets_per_stream
< 0) {
4964 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4965 goto error_close_trace_chunk
;
4968 if (session
->kernel_session
) {
4969 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4970 snapshot_kernel_consumer_output
, session
,
4971 wait
, nb_packets_per_stream
);
4972 if (ret_code
!= LTTNG_OK
) {
4973 goto error_close_trace_chunk
;
4977 if (session
->ust_session
) {
4978 ret_code
= record_ust_snapshot(session
->ust_session
,
4979 snapshot_ust_consumer_output
, session
,
4980 wait
, nb_packets_per_stream
);
4981 if (ret_code
!= LTTNG_OK
) {
4982 goto error_close_trace_chunk
;
4986 error_close_trace_chunk
:
4987 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
4988 ERR("Failed to release the current trace chunk of session \"%s\"",
4990 ret_code
= LTTNG_ERR_UNK
;
4993 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
4994 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
4996 * Don't goto end; make sure the chunk is closed for the session
4997 * to allow future snapshots.
4999 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5001 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5004 if (original_ust_consumer_output
) {
5005 session
->ust_session
->consumer
= original_ust_consumer_output
;
5007 if (original_kernel_consumer_output
) {
5008 session
->kernel_session
->consumer
=
5009 original_kernel_consumer_output
;
5011 consumer_output_put(snapshot_ust_consumer_output
);
5012 consumer_output_put(snapshot_kernel_consumer_output
);
5017 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5019 * The wait parameter is ignored so this call always wait for the snapshot to
5020 * complete before returning.
5022 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5024 int cmd_snapshot_record(struct ltt_session
*session
,
5025 const struct lttng_snapshot_output
*output
, int wait
)
5027 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5029 unsigned int snapshot_success
= 0;
5031 struct snapshot_output
*tmp_output
= NULL
;
5036 DBG("Cmd snapshot record for session %s", session
->name
);
5038 /* Get the datetime for the snapshot output directory. */
5039 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5042 cmd_ret
= LTTNG_ERR_INVALID
;
5047 * Permission denied to create an output if the session is not
5048 * set in no output mode.
5050 if (session
->output_traces
) {
5051 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5055 /* The session needs to be started at least once. */
5056 if (!session
->has_been_started
) {
5057 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5061 /* Use temporary output for the session. */
5062 if (*output
->ctrl_url
!= '\0') {
5063 tmp_output
= snapshot_output_alloc();
5065 cmd_ret
= LTTNG_ERR_NOMEM
;
5069 ret
= snapshot_output_init(session
, output
->max_size
,
5071 output
->ctrl_url
, output
->data_url
,
5075 if (ret
== -ENOMEM
) {
5076 cmd_ret
= LTTNG_ERR_NOMEM
;
5078 cmd_ret
= LTTNG_ERR_INVALID
;
5082 /* Use the global session count for the temporary snapshot. */
5083 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5085 /* Use the global datetime */
5086 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5087 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
5088 if (cmd_ret
!= LTTNG_OK
) {
5091 snapshot_success
= 1;
5093 struct snapshot_output
*sout
;
5094 struct lttng_ht_iter iter
;
5097 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5098 &iter
.iter
, sout
, node
.node
) {
5099 struct snapshot_output output_copy
;
5102 * Make a local copy of the output and override output
5103 * parameters with those provided as part of the
5106 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5108 if (output
->max_size
!= (uint64_t) -1ULL) {
5109 output_copy
.max_size
= output
->max_size
;
5112 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5113 memcpy(output_copy
.datetime
, datetime
,
5116 /* Use temporary name. */
5117 if (*output
->name
!= '\0') {
5118 if (lttng_strncpy(output_copy
.name
,
5120 sizeof(output_copy
.name
))) {
5121 cmd_ret
= LTTNG_ERR_INVALID
;
5127 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
5128 if (cmd_ret
!= LTTNG_OK
) {
5132 snapshot_success
= 1;
5137 if (snapshot_success
) {
5138 session
->snapshot
.nb_snapshot
++;
5140 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5145 snapshot_output_destroy(tmp_output
);
5151 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5153 int cmd_set_session_shm_path(struct ltt_session
*session
,
5154 const char *shm_path
)
5160 * Can only set shm path before session is started.
5162 if (session
->has_been_started
) {
5163 return LTTNG_ERR_SESSION_STARTED
;
5166 strncpy(session
->shm_path
, shm_path
,
5167 sizeof(session
->shm_path
));
5168 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5174 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5176 * Ask the consumer to rotate the session output directory.
5177 * The session lock must be held.
5179 * Returns LTTNG_OK on success or else a negative LTTng error code.
5181 int cmd_rotate_session(struct ltt_session
*session
,
5182 struct lttng_rotate_session_return
*rotate_return
,
5183 bool quiet_rotation
,
5184 enum lttng_trace_chunk_command_type command
)
5187 uint64_t ongoing_rotation_chunk_id
;
5188 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5189 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5190 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5191 enum lttng_trace_chunk_status chunk_status
;
5192 bool failed_to_rotate
= false;
5193 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5197 if (!session
->has_been_started
) {
5198 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5203 * Explicit rotation is not supported for live sessions.
5204 * However, live sessions can perform a quiet rotation on
5206 * Rotation is not supported for snapshot traces (no output).
5208 if ((!quiet_rotation
&& session
->live_timer
) ||
5209 !session
->output_traces
) {
5210 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5214 /* Unsupported feature in lttng-relayd before 2.11. */
5215 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5216 (session
->consumer
->relay_major_version
== 2 &&
5217 session
->consumer
->relay_minor_version
< 11)) {
5218 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5222 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5223 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5224 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5228 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5229 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5231 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5236 * After a stop, we only allow one rotation to occur, the other ones are
5237 * useless until a new start.
5239 if (session
->rotated_after_last_stop
) {
5240 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5242 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5247 * After a stop followed by a clear, disallow following rotations a they would
5248 * generate empty chunks.
5250 if (session
->cleared_after_last_stop
) {
5251 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5253 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5257 if (session
->active
) {
5258 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5260 if (!new_trace_chunk
) {
5261 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5267 * The current trace chunk becomes the chunk being archived.
5269 * After this point, "chunk_being_archived" must absolutely
5270 * be closed on the consumer(s), otherwise it will never be
5271 * cleaned-up, which will result in a leak.
5273 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5274 &chunk_being_archived
);
5276 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5280 if (session
->kernel_session
) {
5281 cmd_ret
= kernel_rotate_session(session
);
5282 if (cmd_ret
!= LTTNG_OK
) {
5283 failed_to_rotate
= true;
5284 rotation_fail_code
= cmd_ret
;
5287 if (session
->ust_session
) {
5288 cmd_ret
= ust_app_rotate_session(session
);
5289 if (cmd_ret
!= LTTNG_OK
) {
5290 failed_to_rotate
= true;
5291 rotation_fail_code
= cmd_ret
;
5295 if (!session
->active
) {
5296 session
->rotated_after_last_stop
= true;
5299 if (!chunk_being_archived
) {
5300 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5302 if (failed_to_rotate
) {
5303 cmd_ret
= rotation_fail_code
;
5310 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5311 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5312 &ongoing_rotation_chunk_id
);
5313 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5315 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5316 command
, session
->last_chunk_path
);
5318 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5322 if (failed_to_rotate
) {
5323 cmd_ret
= rotation_fail_code
;
5327 session
->quiet_rotation
= quiet_rotation
;
5328 ret
= timer_session_rotation_pending_check_start(session
,
5329 DEFAULT_ROTATE_PENDING_TIMER
);
5331 cmd_ret
= LTTNG_ERR_UNK
;
5335 if (rotate_return
) {
5336 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5339 session
->chunk_being_archived
= chunk_being_archived
;
5340 chunk_being_archived
= NULL
;
5341 if (!quiet_rotation
) {
5342 ret
= notification_thread_command_session_rotation_ongoing(
5343 notification_thread_handle
,
5344 session
->name
, session
->uid
, session
->gid
,
5345 ongoing_rotation_chunk_id
);
5346 if (ret
!= LTTNG_OK
) {
5347 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5353 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5354 session
->name
, ongoing_rotation_chunk_id
);
5356 lttng_trace_chunk_put(new_trace_chunk
);
5357 lttng_trace_chunk_put(chunk_being_archived
);
5358 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5361 if (session_reset_rotation_state(session
,
5362 LTTNG_ROTATION_STATE_ERROR
)) {
5363 ERR("Failed to reset rotation state of session \"%s\"",
5370 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5372 * Check if the session has finished its rotation.
5374 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5376 int cmd_rotate_get_info(struct ltt_session
*session
,
5377 struct lttng_rotation_get_info_return
*info_return
,
5378 uint64_t rotation_id
)
5380 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5381 enum lttng_rotation_state rotation_state
;
5383 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5384 session
->most_recent_chunk_id
.value
);
5386 if (session
->chunk_being_archived
) {
5387 enum lttng_trace_chunk_status chunk_status
;
5390 chunk_status
= lttng_trace_chunk_get_id(
5391 session
->chunk_being_archived
,
5393 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5395 rotation_state
= rotation_id
== chunk_id
?
5396 LTTNG_ROTATION_STATE_ONGOING
:
5397 LTTNG_ROTATION_STATE_EXPIRED
;
5399 if (session
->last_archived_chunk_id
.is_set
&&
5400 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5401 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5403 rotation_state
= session
->rotation_state
;
5407 switch (rotation_state
) {
5408 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5409 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5412 case LTTNG_ROTATION_STATE_EXPIRED
:
5413 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5414 rotation_id
, session
->name
);
5416 case LTTNG_ROTATION_STATE_ONGOING
:
5417 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5418 rotation_id
, session
->name
);
5420 case LTTNG_ROTATION_STATE_COMPLETED
:
5424 char *current_tracing_path_reply
;
5425 size_t current_tracing_path_reply_len
;
5427 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5428 rotation_id
, session
->name
);
5430 switch (session_get_consumer_destination_type(session
)) {
5431 case CONSUMER_DST_LOCAL
:
5432 current_tracing_path_reply
=
5433 info_return
->location
.local
.absolute_path
;
5434 current_tracing_path_reply_len
=
5435 sizeof(info_return
->location
.local
.absolute_path
);
5436 info_return
->location_type
=
5437 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5438 fmt_ret
= asprintf(&chunk_path
,
5439 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5440 session_get_base_path(session
),
5441 session
->last_archived_chunk_name
);
5442 if (fmt_ret
== -1) {
5443 PERROR("Failed to format the path of the last archived trace chunk");
5444 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5445 cmd_ret
= LTTNG_ERR_UNK
;
5449 case CONSUMER_DST_NET
:
5451 uint16_t ctrl_port
, data_port
;
5453 current_tracing_path_reply
=
5454 info_return
->location
.relay
.relative_path
;
5455 current_tracing_path_reply_len
=
5456 sizeof(info_return
->location
.relay
.relative_path
);
5457 /* Currently the only supported relay protocol. */
5458 info_return
->location
.relay
.protocol
=
5459 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5461 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5462 session_get_net_consumer_hostname(session
),
5463 sizeof(info_return
->location
.relay
.host
));
5465 ERR("Failed to copy host name to rotate_get_info reply");
5466 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5467 cmd_ret
= LTTNG_ERR_SET_URL
;
5471 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5472 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5473 info_return
->location
.relay
.ports
.data
= data_port
;
5474 info_return
->location_type
=
5475 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5476 chunk_path
= strdup(session
->last_chunk_path
);
5478 ERR("Failed to allocate the path of the last archived trace chunk");
5479 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5480 cmd_ret
= LTTNG_ERR_UNK
;
5489 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5490 chunk_path
, current_tracing_path_reply_len
);
5493 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5494 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5495 cmd_ret
= LTTNG_ERR_UNK
;
5501 case LTTNG_ROTATION_STATE_ERROR
:
5502 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5503 rotation_id
, session
->name
);
5511 info_return
->status
= (int32_t) rotation_state
;
5516 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5518 * Configure the automatic rotation parameters.
5519 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5520 * 'activate' to false means deactivate the rotation schedule and validate that
5521 * 'new_value' has the same value as the currently active value.
5523 * Return 0 on success or else a positive LTTNG_ERR code.
5525 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5526 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5528 struct notification_thread_handle
*notification_thread_handle
)
5531 uint64_t *parameter_value
;
5535 DBG("Cmd rotate set schedule session %s", session
->name
);
5537 if (session
->live_timer
|| !session
->output_traces
) {
5538 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5539 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5543 switch (schedule_type
) {
5544 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5545 parameter_value
= &session
->rotate_size
;
5547 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5548 parameter_value
= &session
->rotate_timer_period
;
5549 if (new_value
>= UINT_MAX
) {
5550 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5551 new_value
, UINT_MAX
);
5552 ret
= LTTNG_ERR_INVALID
;
5557 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5558 ret
= LTTNG_ERR_INVALID
;
5562 /* Improper use of the API. */
5563 if (new_value
== -1ULL) {
5564 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5565 ret
= LTTNG_ERR_INVALID
;
5570 * As indicated in struct ltt_session's comments, a value of == 0 means
5571 * this schedule rotation type is not in use.
5573 * Reject the command if we were asked to activate a schedule that was
5576 if (activate
&& *parameter_value
!= 0) {
5577 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5578 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5583 * Reject the command if we were asked to deactivate a schedule that was
5586 if (!activate
&& *parameter_value
== 0) {
5587 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5588 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5593 * Reject the command if we were asked to deactivate a schedule that
5596 if (!activate
&& *parameter_value
!= new_value
) {
5597 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5598 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5602 *parameter_value
= activate
? new_value
: 0;
5604 switch (schedule_type
) {
5605 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5606 if (activate
&& session
->active
) {
5608 * Only start the timer if the session is active,
5609 * otherwise it will be started when the session starts.
5611 ret
= timer_session_rotation_schedule_timer_start(
5612 session
, new_value
);
5614 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5615 ret
= LTTNG_ERR_UNK
;
5619 ret
= timer_session_rotation_schedule_timer_stop(
5622 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5623 ret
= LTTNG_ERR_UNK
;
5628 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5630 ret
= subscribe_session_consumed_size_rotation(session
,
5631 new_value
, notification_thread_handle
);
5633 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5634 ret
= LTTNG_ERR_UNK
;
5638 ret
= unsubscribe_session_consumed_size_rotation(session
,
5639 notification_thread_handle
);
5641 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5642 ret
= LTTNG_ERR_UNK
;
5649 /* Would have been caught before. */
5661 /* Wait for a given path to be removed before continuing. */
5662 static enum lttng_error_code
wait_on_path(void *path_data
)
5664 const char *shm_path
= path_data
;
5666 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5672 ret
= stat(shm_path
, &st
);
5674 if (errno
!= ENOENT
) {
5675 PERROR("stat() returned an error while checking for the existence of the shm path");
5677 DBG("shm path no longer exists, completing the destruction of session");
5681 if (!S_ISDIR(st
.st_mode
)) {
5682 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5687 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5693 * Returns a pointer to a handler to run on completion of a command.
5694 * Returns NULL if no handler has to be run for the last command executed.
5696 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5698 struct cmd_completion_handler
*handler
= current_completion_handler
;
5700 current_completion_handler
= NULL
;
5705 * Init command subsystem.
5710 * Set network sequence index to 1 for streams to match a relayd
5711 * socket on the consumer side.
5713 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5714 relayd_net_seq_idx
= 1;
5715 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5717 DBG("Command subsystem initialized");