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
15 #include <urcu/list.h>
16 #include <urcu/uatomic.h>
18 #include <common/buffer-view.h>
19 #include <common/common.h>
20 #include <common/compat/string.h>
21 #include <common/defaults.h>
22 #include <common/dynamic-buffer.h>
23 #include <common/kernel-ctl/kernel-ctl.h>
24 #include <common/payload-view.h>
25 #include <common/payload.h>
26 #include <common/relayd/relayd.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/string-utils/string-utils.h>
29 #include <common/trace-chunk.h>
30 #include <common/utils.h>
31 #include <lttng/action/action-internal.h>
32 #include <lttng/action/action.h>
33 #include <lttng/channel-internal.h>
34 #include <lttng/channel.h>
35 #include <lttng/condition/condition-internal.h>
36 #include <lttng/condition/condition.h>
37 #include <lttng/condition/event-rule-matches-internal.h>
38 #include <lttng/condition/event-rule-matches.h>
39 #include <lttng/error-query-internal.h>
40 #include <lttng/event-rule/event-rule-internal.h>
41 #include <lttng/event-rule/event-rule.h>
42 #include <lttng/location-internal.h>
43 #include <lttng/lttng-error.h>
44 #include <lttng/rotate-internal.h>
45 #include <lttng/session-descriptor-internal.h>
46 #include <lttng/session-internal.h>
47 #include <lttng/tracker.h>
48 #include <lttng/trigger/trigger-internal.h>
49 #include <lttng/userspace-probe-internal.h>
51 #include "agent-thread.h"
53 #include "buffer-registry.h"
57 #include "event-notifier-error-accounting.h"
59 #include "health-sessiond.h"
60 #include "kernel-consumer.h"
62 #include "lttng-sessiond.h"
63 #include "lttng-syscall.h"
64 #include "notification-thread-commands.h"
65 #include "notification-thread.h"
67 #include "rotation-thread.h"
73 /* Sleep for 100ms between each check for the shm path's deletion. */
74 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
76 struct cmd_destroy_session_reply_context
{
78 bool implicit_rotation_on_destroy
;
80 * Indicates whether or not an error occurred while launching the
81 * destruction of a session.
83 enum lttng_error_code destruction_status
;
86 static enum lttng_error_code
wait_on_path(void *path
);
89 * Command completion handler that is used by the destroy command
90 * when a session that has a non-default shm_path is being destroyed.
92 * See comment in cmd_destroy_session() for the rationale.
94 static struct destroy_completion_handler
{
95 struct cmd_completion_handler handler
;
96 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
97 } destroy_completion_handler
= {
100 .data
= destroy_completion_handler
.shm_path
105 static struct cmd_completion_handler
*current_completion_handler
;
108 * Used to keep a unique index for each relayd socket created where this value
109 * is associated with streams on the consumer so it can match the right relayd
110 * to send to. It must be accessed with the relayd_net_seq_idx_lock
113 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
114 static uint64_t relayd_net_seq_idx
;
116 static int validate_ust_event_name(const char *);
117 static int cmd_enable_event_internal(struct ltt_session
*session
,
118 const struct lttng_domain
*domain
,
119 char *channel_name
, struct lttng_event
*event
,
120 char *filter_expression
,
121 struct lttng_bytecode
*filter
,
122 struct lttng_event_exclusion
*exclusion
,
126 * Create a session path used by list_lttng_sessions for the case that the
127 * session consumer is on the network.
129 static int build_network_session_path(char *dst
, size_t size
,
130 struct ltt_session
*session
)
132 int ret
, kdata_port
, udata_port
;
133 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
134 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
139 memset(tmp_urls
, 0, sizeof(tmp_urls
));
140 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
142 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
144 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
145 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
146 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
149 if (session
->ust_session
&& session
->ust_session
->consumer
) {
150 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
151 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
154 if (uuri
== NULL
&& kuri
== NULL
) {
155 uri
= &session
->consumer
->dst
.net
.control
;
156 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
157 } else if (kuri
&& uuri
) {
158 ret
= uri_compare(kuri
, uuri
);
162 /* Build uuri URL string */
163 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
170 } else if (kuri
&& uuri
== NULL
) {
172 } else if (uuri
&& kuri
== NULL
) {
176 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
182 * Do we have a UST url set. If yes, this means we have both kernel and UST
185 if (*tmp_uurl
!= '\0') {
186 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
187 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
190 if (kuri
|| (!kuri
&& !uuri
)) {
193 /* No kernel URI, use the UST port. */
196 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
204 * Get run-time attributes if the session has been started (discarded events,
207 static int get_kernel_runtime_stats(struct ltt_session
*session
,
208 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
209 uint64_t *lost_packets
)
213 if (!session
->has_been_started
) {
215 *discarded_events
= 0;
220 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
221 session
->kernel_session
->consumer
,
227 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
228 session
->kernel_session
->consumer
,
239 * Get run-time attributes if the session has been started (discarded events,
242 static int get_ust_runtime_stats(struct ltt_session
*session
,
243 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
244 uint64_t *lost_packets
)
247 struct ltt_ust_session
*usess
;
249 if (!discarded_events
|| !lost_packets
) {
254 usess
= session
->ust_session
;
255 assert(discarded_events
);
256 assert(lost_packets
);
258 if (!usess
|| !session
->has_been_started
) {
259 *discarded_events
= 0;
265 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
266 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
267 &usess
->buffer_reg_uid_list
,
268 usess
->consumer
, uchan
->id
,
269 uchan
->attr
.overwrite
,
272 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
273 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
274 uchan
, usess
->consumer
,
275 uchan
->attr
.overwrite
,
281 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
282 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
284 ERR("Unsupported buffer type");
295 * Fill lttng_channel array of all channels.
297 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
298 struct ltt_session
*session
, struct lttng_channel
*channels
,
299 struct lttng_channel_extended
*chan_exts
)
302 struct ltt_kernel_channel
*kchan
;
304 DBG("Listing channels for session %s", session
->name
);
307 case LTTNG_DOMAIN_KERNEL
:
308 /* Kernel channels */
309 if (session
->kernel_session
!= NULL
) {
310 cds_list_for_each_entry(kchan
,
311 &session
->kernel_session
->channel_list
.head
, list
) {
312 uint64_t discarded_events
, lost_packets
;
313 struct lttng_channel_extended
*extended
;
315 extended
= (struct lttng_channel_extended
*)
316 kchan
->channel
->attr
.extended
.ptr
;
318 ret
= get_kernel_runtime_stats(session
, kchan
,
319 &discarded_events
, &lost_packets
);
323 /* Copy lttng_channel struct to array */
324 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
325 channels
[i
].enabled
= kchan
->enabled
;
326 chan_exts
[i
].discarded_events
=
328 chan_exts
[i
].lost_packets
= lost_packets
;
329 chan_exts
[i
].monitor_timer_interval
=
330 extended
->monitor_timer_interval
;
331 chan_exts
[i
].blocking_timeout
= 0;
336 case LTTNG_DOMAIN_UST
:
338 struct lttng_ht_iter iter
;
339 struct ltt_ust_channel
*uchan
;
342 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
343 &iter
.iter
, uchan
, node
.node
) {
344 uint64_t discarded_events
= 0, lost_packets
= 0;
346 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
347 LTTNG_SYMBOL_NAME_LEN
)) {
350 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
351 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
352 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
353 channels
[i
].attr
.switch_timer_interval
=
354 uchan
->attr
.switch_timer_interval
;
355 channels
[i
].attr
.read_timer_interval
=
356 uchan
->attr
.read_timer_interval
;
357 channels
[i
].enabled
= uchan
->enabled
;
358 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
359 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
362 * Map enum lttng_ust_output to enum lttng_event_output.
364 switch (uchan
->attr
.output
) {
365 case LTTNG_UST_ABI_MMAP
:
366 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
370 * LTTNG_UST_MMAP is the only supported UST
377 chan_exts
[i
].monitor_timer_interval
=
378 uchan
->monitor_timer_interval
;
379 chan_exts
[i
].blocking_timeout
=
380 uchan
->attr
.u
.s
.blocking_timeout
;
382 ret
= get_ust_runtime_stats(session
, uchan
,
383 &discarded_events
, &lost_packets
);
387 chan_exts
[i
].discarded_events
= discarded_events
;
388 chan_exts
[i
].lost_packets
= lost_packets
;
400 return -LTTNG_ERR_FATAL
;
406 static int append_extended_info(const char *filter_expression
,
407 struct lttng_event_exclusion
*exclusion
,
408 struct lttng_userspace_probe_location
*probe_location
,
409 struct lttng_payload
*payload
)
412 size_t filter_len
= 0;
413 size_t nb_exclusions
= 0;
414 size_t userspace_probe_location_len
= 0;
415 struct lttcomm_event_extended_header extended_header
= {};
416 struct lttcomm_event_extended_header
*p_extended_header
;
417 const size_t original_payload_size
= payload
->buffer
.size
;
419 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &extended_header
,
420 sizeof(extended_header
));
425 if (filter_expression
) {
426 filter_len
= strlen(filter_expression
) + 1;
427 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
428 filter_expression
, filter_len
);
435 const size_t len
= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
437 nb_exclusions
= exclusion
->count
;
439 ret
= lttng_dynamic_buffer_append(
440 &payload
->buffer
, &exclusion
->names
, len
);
446 if (probe_location
) {
447 const size_t size_before_probe
= payload
->buffer
.size
;
449 ret
= lttng_userspace_probe_location_serialize(probe_location
,
456 userspace_probe_location_len
=
457 payload
->buffer
.size
- size_before_probe
;
460 /* Set header fields */
461 p_extended_header
= (struct lttcomm_event_extended_header
*)
462 (payload
->buffer
.data
+ original_payload_size
);
464 p_extended_header
->filter_len
= filter_len
;
465 p_extended_header
->nb_exclusions
= nb_exclusions
;
466 p_extended_header
->userspace_probe_location_len
=
467 userspace_probe_location_len
;
475 * Create a list of agent domain events.
477 * Return number of events in list on success or else a negative value.
479 static int list_lttng_agent_events(struct agent
*agt
,
480 struct lttng_payload
*payload
)
482 int nb_events
= 0, ret
= 0;
483 const struct agent_event
*agent_event
;
484 struct lttng_ht_iter iter
;
488 DBG3("Listing agent events");
491 cds_lfht_for_each_entry (
492 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
493 struct lttng_event event
= {
494 .enabled
= AGENT_EVENT_IS_ENABLED(agent_event
),
495 .loglevel
= agent_event
->loglevel_value
,
496 .loglevel_type
= agent_event
->loglevel_type
,
499 ret
= lttng_strncpy(event
.name
, agent_event
->name
, sizeof(event
.name
));
501 /* Internal error, invalid name. */
502 ERR("Invalid event name while listing agent events: '%s' exceeds the maximal allowed length of %zu bytes",
503 agent_event
->name
, sizeof(event
.name
));
504 ret
= -LTTNG_ERR_UNK
;
508 ret
= lttng_dynamic_buffer_append(
509 &payload
->buffer
, &event
, sizeof(event
));
511 ERR("Failed to append event to payload");
512 ret
= -LTTNG_ERR_NOMEM
;
519 cds_lfht_for_each_entry (
520 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
521 /* Append extended info. */
522 ret
= append_extended_info(agent_event
->filter_expression
, NULL
,
525 ERR("Failed to append extended event info to payload");
526 ret
= -LTTNG_ERR_NOMEM
;
538 * Create a list of ust global domain events.
540 static int list_lttng_ust_global_events(char *channel_name
,
541 struct ltt_ust_domain_global
*ust_global
,
542 struct lttng_payload
*payload
)
545 unsigned int nb_events
= 0;
546 struct lttng_ht_iter iter
;
547 const struct lttng_ht_node_str
*node
;
548 const struct ltt_ust_channel
*uchan
;
549 const struct ltt_ust_event
*uevent
;
551 DBG("Listing UST global events for channel %s", channel_name
);
555 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
556 node
= lttng_ht_iter_get_node_str(&iter
);
558 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
562 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
564 DBG3("Listing UST global events");
566 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
567 struct lttng_event event
= {};
569 if (uevent
->internal
) {
573 ret
= lttng_strncpy(event
.name
, uevent
->attr
.name
, sizeof(event
.name
));
575 /* Internal error, invalid name. */
576 ERR("Invalid event name while listing user space tracer events: '%s' exceeds the maximal allowed length of %zu bytes",
577 uevent
->attr
.name
, sizeof(event
.name
));
578 ret
= -LTTNG_ERR_UNK
;
582 event
.enabled
= uevent
->enabled
;
584 switch (uevent
->attr
.instrumentation
) {
585 case LTTNG_UST_ABI_TRACEPOINT
:
586 event
.type
= LTTNG_EVENT_TRACEPOINT
;
588 case LTTNG_UST_ABI_PROBE
:
589 event
.type
= LTTNG_EVENT_PROBE
;
591 case LTTNG_UST_ABI_FUNCTION
:
592 event
.type
= LTTNG_EVENT_FUNCTION
;
596 event
.loglevel
= uevent
->attr
.loglevel
;
597 switch (uevent
->attr
.loglevel_type
) {
598 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
599 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
601 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
602 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
604 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
605 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
609 if (uevent
->filter
) {
613 if (uevent
->exclusion
) {
617 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &event
, sizeof(event
));
619 ERR("Failed to append event to payload");
620 ret
= -LTTNG_ERR_NOMEM
;
627 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
628 /* Append extended info. */
629 ret
= append_extended_info(uevent
->filter_expression
,
630 uevent
->exclusion
, NULL
, payload
);
632 ERR("Failed to append extended event info to payload");
633 ret
= -LTTNG_ERR_FATAL
;
645 * Fill lttng_event array of all kernel events in the channel.
647 static int list_lttng_kernel_events(char *channel_name
,
648 struct ltt_kernel_session
*kernel_session
,
649 struct lttng_payload
*payload
)
652 unsigned int nb_event
;
653 const struct ltt_kernel_event
*kevent
;
654 const struct ltt_kernel_channel
*kchan
;
656 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
658 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
662 nb_event
= kchan
->event_count
;
664 DBG("Listing events for channel %s", kchan
->channel
->name
);
666 /* Kernel channels */
667 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
668 struct lttng_event event
= {};
670 ret
= lttng_strncpy(event
.name
, kevent
->event
->name
, sizeof(event
.name
));
672 /* Internal error, invalid name. */
673 ERR("Invalid event name while listing kernel events: '%s' exceeds the maximal allowed length of %zu bytes",
676 ret
= -LTTNG_ERR_UNK
;
680 event
.enabled
= kevent
->enabled
;
681 event
.filter
= (unsigned char) !!kevent
->filter_expression
;
683 switch (kevent
->event
->instrumentation
) {
684 case LTTNG_KERNEL_ABI_TRACEPOINT
:
685 event
.type
= LTTNG_EVENT_TRACEPOINT
;
687 case LTTNG_KERNEL_ABI_KRETPROBE
:
688 event
.type
= LTTNG_EVENT_FUNCTION
;
689 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
690 sizeof(struct lttng_kernel_abi_kprobe
));
692 case LTTNG_KERNEL_ABI_KPROBE
:
693 event
.type
= LTTNG_EVENT_PROBE
;
694 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
695 sizeof(struct lttng_kernel_abi_kprobe
));
697 case LTTNG_KERNEL_ABI_UPROBE
:
698 event
.type
= LTTNG_EVENT_USERSPACE_PROBE
;
700 case LTTNG_KERNEL_ABI_FUNCTION
:
701 event
.type
= LTTNG_EVENT_FUNCTION
;
702 memcpy(&event
.attr
.ftrace
, &kevent
->event
->u
.ftrace
,
703 sizeof(struct lttng_kernel_abi_function
));
705 case LTTNG_KERNEL_ABI_NOOP
:
706 event
.type
= LTTNG_EVENT_NOOP
;
708 case LTTNG_KERNEL_ABI_SYSCALL
:
709 event
.type
= LTTNG_EVENT_SYSCALL
;
711 case LTTNG_KERNEL_ABI_ALL
:
718 ret
= lttng_dynamic_buffer_append(
719 &payload
->buffer
, &event
, sizeof(event
));
721 ERR("Failed to append event to payload");
722 ret
= -LTTNG_ERR_NOMEM
;
727 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
728 /* Append extended info. */
729 ret
= append_extended_info(kevent
->filter_expression
, NULL
,
730 kevent
->userspace_probe_location
, payload
);
732 DBG("Error appending extended info message");
733 ret
= -LTTNG_ERR_FATAL
;
745 * Add URI so the consumer output object. Set the correct path depending on the
746 * domain adding the default trace directory.
748 static enum lttng_error_code
add_uri_to_consumer(
749 const struct ltt_session
*session
,
750 struct consumer_output
*consumer
,
751 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
754 enum lttng_error_code ret_code
= LTTNG_OK
;
758 if (consumer
== NULL
) {
759 DBG("No consumer detected. Don't add URI. Stopping.");
760 ret_code
= LTTNG_ERR_NO_CONSUMER
;
765 case LTTNG_DOMAIN_KERNEL
:
766 ret
= lttng_strncpy(consumer
->domain_subdir
,
767 DEFAULT_KERNEL_TRACE_DIR
,
768 sizeof(consumer
->domain_subdir
));
770 case LTTNG_DOMAIN_UST
:
771 ret
= lttng_strncpy(consumer
->domain_subdir
,
772 DEFAULT_UST_TRACE_DIR
,
773 sizeof(consumer
->domain_subdir
));
777 * This case is possible is we try to add the URI to the global
778 * tracing session consumer object which in this case there is
781 memset(consumer
->domain_subdir
, 0,
782 sizeof(consumer
->domain_subdir
));
786 ERR("Failed to initialize consumer output domain subdirectory");
787 ret_code
= LTTNG_ERR_FATAL
;
791 switch (uri
->dtype
) {
794 DBG2("Setting network URI to consumer");
796 if (consumer
->type
== CONSUMER_DST_NET
) {
797 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
798 consumer
->dst
.net
.control_isset
) ||
799 (uri
->stype
== LTTNG_STREAM_DATA
&&
800 consumer
->dst
.net
.data_isset
)) {
801 ret_code
= LTTNG_ERR_URL_EXIST
;
805 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
808 /* Set URI into consumer output object */
809 ret
= consumer_set_network_uri(session
, consumer
, uri
);
813 } else if (ret
== 1) {
815 * URI was the same in the consumer so we do not append the subdir
816 * again so to not duplicate output dir.
823 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
824 ret_code
= LTTNG_ERR_INVALID
;
827 DBG2("Setting trace directory path from URI to %s",
829 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
831 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
833 sizeof(consumer
->dst
.session_root_path
));
835 ret_code
= LTTNG_ERR_FATAL
;
838 consumer
->type
= CONSUMER_DST_LOCAL
;
848 * Init tracing by creating trace directory and sending fds kernel consumer.
850 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
853 struct lttng_ht_iter iter
;
854 struct consumer_socket
*socket
;
860 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
861 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
863 pthread_mutex_lock(socket
->lock
);
864 ret
= kernel_consumer_send_session(socket
, session
);
865 pthread_mutex_unlock(socket
->lock
);
867 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
879 * Create a socket to the relayd using the URI.
881 * On success, the relayd_sock pointer is set to the created socket.
882 * Else, it remains untouched and an LTTng error code is returned.
884 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
885 struct lttcomm_relayd_sock
**relayd_sock
,
886 struct consumer_output
*consumer
)
889 enum lttng_error_code status
= LTTNG_OK
;
890 struct lttcomm_relayd_sock
*rsock
;
892 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
893 RELAYD_VERSION_COMM_MINOR
);
895 status
= LTTNG_ERR_FATAL
;
900 * Connect to relayd so we can proceed with a session creation. This call
901 * can possibly block for an arbitrary amount of time to set the health
902 * state to be in poll execution.
905 ret
= relayd_connect(rsock
);
908 ERR("Unable to reach lttng-relayd");
909 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
913 /* Create socket for control stream. */
914 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
915 uint64_t result_flags
;
917 DBG3("Creating relayd stream socket from URI");
919 /* Check relayd version */
920 ret
= relayd_version_check(rsock
);
921 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
922 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
924 } else if (ret
< 0) {
925 ERR("Unable to reach lttng-relayd");
926 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
929 consumer
->relay_major_version
= rsock
->major
;
930 consumer
->relay_minor_version
= rsock
->minor
;
931 ret
= relayd_get_configuration(rsock
, 0,
934 ERR("Unable to get relayd configuration");
935 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
938 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
939 consumer
->relay_allows_clear
= true;
941 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
942 DBG3("Creating relayd data socket from URI");
944 /* Command is not valid */
945 ERR("Relayd invalid stream type: %d", uri
->stype
);
946 status
= LTTNG_ERR_INVALID
;
950 *relayd_sock
= rsock
;
955 /* The returned value is not useful since we are on an error path. */
956 (void) relayd_close(rsock
);
964 * Connect to the relayd using URI and send the socket to the right consumer.
966 * The consumer socket lock must be held by the caller.
968 * Returns LTTNG_OK on success or an LTTng error code on failure.
970 static enum lttng_error_code
send_consumer_relayd_socket(
971 unsigned int session_id
,
972 struct lttng_uri
*relayd_uri
,
973 struct consumer_output
*consumer
,
974 struct consumer_socket
*consumer_sock
,
975 const char *session_name
, const char *hostname
,
976 const char *base_path
, int session_live_timer
,
977 const uint64_t *current_chunk_id
,
978 time_t session_creation_time
,
979 bool session_name_contains_creation_time
)
982 struct lttcomm_relayd_sock
*rsock
= NULL
;
983 enum lttng_error_code status
;
985 /* Connect to relayd and make version check if uri is the control. */
986 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
987 if (status
!= LTTNG_OK
) {
988 goto relayd_comm_error
;
992 /* Set the network sequence index if not set. */
993 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
994 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
996 * Increment net_seq_idx because we are about to transfer the
997 * new relayd socket to the consumer.
998 * Assign unique key so the consumer can match streams.
1000 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1001 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1004 /* Send relayd socket to consumer. */
1005 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1006 relayd_uri
->stype
, session_id
,
1007 session_name
, hostname
, base_path
,
1008 session_live_timer
, current_chunk_id
,
1009 session_creation_time
, session_name_contains_creation_time
);
1011 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1015 /* Flag that the corresponding socket was sent. */
1016 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1017 consumer_sock
->control_sock_sent
= 1;
1018 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1019 consumer_sock
->data_sock_sent
= 1;
1023 * Close socket which was dup on the consumer side. The session daemon does
1024 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1028 if (status
!= LTTNG_OK
) {
1030 * The consumer output for this session should not be used anymore
1031 * since the relayd connection failed thus making any tracing or/and
1032 * streaming not usable.
1034 consumer
->enabled
= 0;
1036 (void) relayd_close(rsock
);
1044 * Send both relayd sockets to a specific consumer and domain. This is a
1045 * helper function to facilitate sending the information to the consumer for a
1048 * The consumer socket lock must be held by the caller.
1050 * Returns LTTNG_OK, or an LTTng error code on failure.
1052 static enum lttng_error_code
send_consumer_relayd_sockets(
1053 enum lttng_domain_type domain
,
1054 unsigned int session_id
, struct consumer_output
*consumer
,
1055 struct consumer_socket
*sock
, const char *session_name
,
1056 const char *hostname
, const char *base_path
, int session_live_timer
,
1057 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1058 bool session_name_contains_creation_time
)
1060 enum lttng_error_code status
= LTTNG_OK
;
1065 /* Sending control relayd socket. */
1066 if (!sock
->control_sock_sent
) {
1067 status
= send_consumer_relayd_socket(session_id
,
1068 &consumer
->dst
.net
.control
, consumer
, sock
,
1069 session_name
, hostname
, base_path
, session_live_timer
,
1070 current_chunk_id
, session_creation_time
,
1071 session_name_contains_creation_time
);
1072 if (status
!= LTTNG_OK
) {
1077 /* Sending data relayd socket. */
1078 if (!sock
->data_sock_sent
) {
1079 status
= send_consumer_relayd_socket(session_id
,
1080 &consumer
->dst
.net
.data
, consumer
, sock
,
1081 session_name
, hostname
, base_path
, session_live_timer
,
1082 current_chunk_id
, session_creation_time
,
1083 session_name_contains_creation_time
);
1084 if (status
!= LTTNG_OK
) {
1094 * Setup relayd connections for a tracing session. First creates the socket to
1095 * the relayd and send them to the right domain consumer. Consumer type MUST be
1098 int cmd_setup_relayd(struct ltt_session
*session
)
1101 struct ltt_ust_session
*usess
;
1102 struct ltt_kernel_session
*ksess
;
1103 struct consumer_socket
*socket
;
1104 struct lttng_ht_iter iter
;
1105 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1109 usess
= session
->ust_session
;
1110 ksess
= session
->kernel_session
;
1112 DBG("Setting relayd for session %s", session
->name
);
1115 if (session
->current_trace_chunk
) {
1116 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1117 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1119 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1120 current_chunk_id
.is_set
= true;
1122 ERR("Failed to get current trace chunk id");
1123 ret
= LTTNG_ERR_UNK
;
1128 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1129 && usess
->consumer
->enabled
) {
1130 /* For each consumer socket, send relayd sockets */
1131 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1132 socket
, node
.node
) {
1133 pthread_mutex_lock(socket
->lock
);
1134 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1135 usess
->consumer
, socket
,
1136 session
->name
, session
->hostname
,
1138 session
->live_timer
,
1139 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1140 session
->creation_time
,
1141 session
->name_contains_creation_time
);
1142 pthread_mutex_unlock(socket
->lock
);
1143 if (ret
!= LTTNG_OK
) {
1146 /* Session is now ready for network streaming. */
1147 session
->net_handle
= 1;
1149 session
->consumer
->relay_major_version
=
1150 usess
->consumer
->relay_major_version
;
1151 session
->consumer
->relay_minor_version
=
1152 usess
->consumer
->relay_minor_version
;
1153 session
->consumer
->relay_allows_clear
=
1154 usess
->consumer
->relay_allows_clear
;
1157 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1158 && ksess
->consumer
->enabled
) {
1159 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1160 socket
, node
.node
) {
1161 pthread_mutex_lock(socket
->lock
);
1162 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1163 ksess
->consumer
, socket
,
1164 session
->name
, session
->hostname
,
1166 session
->live_timer
,
1167 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1168 session
->creation_time
,
1169 session
->name_contains_creation_time
);
1170 pthread_mutex_unlock(socket
->lock
);
1171 if (ret
!= LTTNG_OK
) {
1174 /* Session is now ready for network streaming. */
1175 session
->net_handle
= 1;
1177 session
->consumer
->relay_major_version
=
1178 ksess
->consumer
->relay_major_version
;
1179 session
->consumer
->relay_minor_version
=
1180 ksess
->consumer
->relay_minor_version
;
1181 session
->consumer
->relay_allows_clear
=
1182 ksess
->consumer
->relay_allows_clear
;
1191 * Start a kernel session by opening all necessary streams.
1193 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1196 struct ltt_kernel_channel
*kchan
;
1198 /* Open kernel metadata */
1199 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1200 ret
= kernel_open_metadata(ksess
);
1202 ret
= LTTNG_ERR_KERN_META_FAIL
;
1207 /* Open kernel metadata stream */
1208 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1209 ret
= kernel_open_metadata_stream(ksess
);
1211 ERR("Kernel create metadata stream failed");
1212 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1217 /* For each channel */
1218 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1219 if (kchan
->stream_count
== 0) {
1220 ret
= kernel_open_channel_stream(kchan
);
1222 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1225 /* Update the stream global counter */
1226 ksess
->stream_count_global
+= ret
;
1230 /* Setup kernel consumer socket and send fds to it */
1231 ret
= init_kernel_tracing(ksess
);
1233 ret
= LTTNG_ERR_KERN_START_FAIL
;
1237 /* This start the kernel tracing */
1238 ret
= kernel_start_session(ksess
);
1240 ret
= LTTNG_ERR_KERN_START_FAIL
;
1244 /* Quiescent wait after starting trace */
1245 kernel_wait_quiescent();
1255 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1257 struct ltt_kernel_channel
*kchan
;
1258 bool error_occurred
= false;
1261 if (!ksess
|| !ksess
->active
) {
1264 DBG("Stopping kernel tracing");
1266 ret
= kernel_stop_session(ksess
);
1268 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1272 kernel_wait_quiescent();
1274 /* Flush metadata after stopping (if exists) */
1275 if (ksess
->metadata_stream_fd
>= 0) {
1276 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1278 ERR("Kernel metadata flush failed");
1279 error_occurred
= true;
1283 /* Flush all buffers after stopping */
1284 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1285 ret
= kernel_flush_buffer(kchan
);
1287 ERR("Kernel flush buffer error");
1288 error_occurred
= true;
1293 if (error_occurred
) {
1294 ret
= LTTNG_ERR_UNK
;
1303 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1305 int cmd_disable_channel(struct ltt_session
*session
,
1306 enum lttng_domain_type domain
, char *channel_name
)
1309 struct ltt_ust_session
*usess
;
1311 usess
= session
->ust_session
;
1316 case LTTNG_DOMAIN_KERNEL
:
1318 ret
= channel_kernel_disable(session
->kernel_session
,
1320 if (ret
!= LTTNG_OK
) {
1324 kernel_wait_quiescent();
1327 case LTTNG_DOMAIN_UST
:
1329 struct ltt_ust_channel
*uchan
;
1330 struct lttng_ht
*chan_ht
;
1332 chan_ht
= usess
->domain_global
.channels
;
1334 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1335 if (uchan
== NULL
) {
1336 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1340 ret
= channel_ust_disable(usess
, uchan
);
1341 if (ret
!= LTTNG_OK
) {
1347 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1359 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1361 * The wpipe arguments is used as a notifier for the kernel thread.
1363 int cmd_enable_channel(struct ltt_session
*session
,
1364 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1367 struct ltt_ust_session
*usess
= session
->ust_session
;
1368 struct lttng_ht
*chan_ht
;
1370 struct lttng_channel attr
;
1377 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1379 /* Validate channel name */
1380 if (attr
.name
[0] == '.' ||
1381 memchr(attr
.name
, '/', len
) != NULL
) {
1382 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1386 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1391 * Don't try to enable a channel if the session has been started at
1392 * some point in time before. The tracer does not allow it.
1394 if (session
->has_been_started
) {
1395 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1400 * If the session is a live session, remove the switch timer, the
1401 * live timer does the same thing but sends also synchronisation
1402 * beacons for inactive streams.
1404 if (session
->live_timer
> 0) {
1405 attr
.attr
.live_timer_interval
= session
->live_timer
;
1406 attr
.attr
.switch_timer_interval
= 0;
1409 /* Check for feature support */
1410 switch (domain
->type
) {
1411 case LTTNG_DOMAIN_KERNEL
:
1413 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1414 /* Sampling position of buffer is not supported */
1415 WARN("Kernel tracer does not support buffer monitoring. "
1416 "Setting the monitor interval timer to 0 "
1417 "(disabled) for channel '%s' of session '%s'",
1418 attr
.name
, session
->name
);
1419 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1423 case LTTNG_DOMAIN_UST
:
1425 case LTTNG_DOMAIN_JUL
:
1426 case LTTNG_DOMAIN_LOG4J
:
1427 case LTTNG_DOMAIN_PYTHON
:
1428 if (!agent_tracing_is_enabled()) {
1429 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1430 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1435 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1439 switch (domain
->type
) {
1440 case LTTNG_DOMAIN_KERNEL
:
1442 struct ltt_kernel_channel
*kchan
;
1444 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1445 session
->kernel_session
);
1446 if (kchan
== NULL
) {
1447 if (session
->snapshot
.nb_output
> 0 ||
1448 session
->snapshot_mode
) {
1449 /* Enforce mmap output for snapshot sessions. */
1450 attr
.attr
.output
= LTTNG_EVENT_MMAP
;
1452 ret
= channel_kernel_create(session
->kernel_session
, &attr
, wpipe
);
1453 if (attr
.name
[0] != '\0') {
1454 session
->kernel_session
->has_non_default_channel
= 1;
1457 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1460 if (ret
!= LTTNG_OK
) {
1464 kernel_wait_quiescent();
1467 case LTTNG_DOMAIN_UST
:
1468 case LTTNG_DOMAIN_JUL
:
1469 case LTTNG_DOMAIN_LOG4J
:
1470 case LTTNG_DOMAIN_PYTHON
:
1472 struct ltt_ust_channel
*uchan
;
1477 * Current agent implementation limitations force us to allow
1478 * only one channel at once in "agent" subdomains. Each
1479 * subdomain has a default channel name which must be strictly
1482 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1483 if (strncmp(attr
.name
, DEFAULT_JUL_CHANNEL_NAME
,
1484 LTTNG_SYMBOL_NAME_LEN
)) {
1485 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1488 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1489 if (strncmp(attr
.name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1490 LTTNG_SYMBOL_NAME_LEN
)) {
1491 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1494 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1495 if (strncmp(attr
.name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1496 LTTNG_SYMBOL_NAME_LEN
)) {
1497 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1502 chan_ht
= usess
->domain_global
.channels
;
1504 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1505 if (uchan
== NULL
) {
1506 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1507 if (attr
.name
[0] != '\0') {
1508 usess
->has_non_default_channel
= 1;
1511 ret
= channel_ust_enable(usess
, uchan
);
1516 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1520 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1521 session
->has_non_mmap_channel
= true;
1529 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1530 struct ltt_session
*session
,
1531 enum lttng_domain_type domain
,
1532 enum lttng_process_attr process_attr
,
1533 enum lttng_tracking_policy
*policy
)
1535 enum lttng_error_code ret_code
= LTTNG_OK
;
1536 const struct process_attr_tracker
*tracker
;
1539 case LTTNG_DOMAIN_KERNEL
:
1540 if (!session
->kernel_session
) {
1541 ret_code
= LTTNG_ERR_INVALID
;
1544 tracker
= kernel_get_process_attr_tracker(
1545 session
->kernel_session
, process_attr
);
1547 case LTTNG_DOMAIN_UST
:
1548 if (!session
->ust_session
) {
1549 ret_code
= LTTNG_ERR_INVALID
;
1552 tracker
= trace_ust_get_process_attr_tracker(
1553 session
->ust_session
, process_attr
);
1556 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1560 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1562 ret_code
= LTTNG_ERR_INVALID
;
1568 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1569 struct ltt_session
*session
,
1570 enum lttng_domain_type domain
,
1571 enum lttng_process_attr process_attr
,
1572 enum lttng_tracking_policy policy
)
1574 enum lttng_error_code ret_code
= LTTNG_OK
;
1577 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1578 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1579 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1582 ret_code
= LTTNG_ERR_INVALID
;
1587 case LTTNG_DOMAIN_KERNEL
:
1588 if (!session
->kernel_session
) {
1589 ret_code
= LTTNG_ERR_INVALID
;
1592 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1593 session
->kernel_session
, process_attr
, policy
);
1595 case LTTNG_DOMAIN_UST
:
1596 if (!session
->ust_session
) {
1597 ret_code
= LTTNG_ERR_INVALID
;
1600 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1601 session
->ust_session
, process_attr
, policy
);
1604 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1611 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1612 struct ltt_session
*session
,
1613 enum lttng_domain_type domain
,
1614 enum lttng_process_attr process_attr
,
1615 const struct process_attr_value
*value
)
1617 enum lttng_error_code ret_code
= LTTNG_OK
;
1620 case LTTNG_DOMAIN_KERNEL
:
1621 if (!session
->kernel_session
) {
1622 ret_code
= LTTNG_ERR_INVALID
;
1625 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1626 session
->kernel_session
, process_attr
, value
);
1628 case LTTNG_DOMAIN_UST
:
1629 if (!session
->ust_session
) {
1630 ret_code
= LTTNG_ERR_INVALID
;
1633 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1634 session
->ust_session
, process_attr
, value
);
1637 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1644 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1645 struct ltt_session
*session
,
1646 enum lttng_domain_type domain
,
1647 enum lttng_process_attr process_attr
,
1648 const struct process_attr_value
*value
)
1650 enum lttng_error_code ret_code
= LTTNG_OK
;
1653 case LTTNG_DOMAIN_KERNEL
:
1654 if (!session
->kernel_session
) {
1655 ret_code
= LTTNG_ERR_INVALID
;
1658 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1659 session
->kernel_session
, process_attr
, value
);
1661 case LTTNG_DOMAIN_UST
:
1662 if (!session
->ust_session
) {
1663 ret_code
= LTTNG_ERR_INVALID
;
1666 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1667 session
->ust_session
, process_attr
, value
);
1670 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1677 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1678 struct ltt_session
*session
,
1679 enum lttng_domain_type domain
,
1680 enum lttng_process_attr process_attr
,
1681 struct lttng_process_attr_values
**values
)
1683 enum lttng_error_code ret_code
= LTTNG_OK
;
1684 const struct process_attr_tracker
*tracker
;
1685 enum process_attr_tracker_status status
;
1688 case LTTNG_DOMAIN_KERNEL
:
1689 if (!session
->kernel_session
) {
1690 ret_code
= LTTNG_ERR_INVALID
;
1693 tracker
= kernel_get_process_attr_tracker(
1694 session
->kernel_session
, process_attr
);
1696 case LTTNG_DOMAIN_UST
:
1697 if (!session
->ust_session
) {
1698 ret_code
= LTTNG_ERR_INVALID
;
1701 tracker
= trace_ust_get_process_attr_tracker(
1702 session
->ust_session
, process_attr
);
1705 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1710 ret_code
= LTTNG_ERR_INVALID
;
1714 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1716 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1717 ret_code
= LTTNG_OK
;
1719 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1720 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1722 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1723 ret_code
= LTTNG_ERR_NOMEM
;
1726 ret_code
= LTTNG_ERR_UNK
;
1735 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1737 int cmd_disable_event(struct ltt_session
*session
,
1738 enum lttng_domain_type domain
, const char *channel_name
,
1739 const struct lttng_event
*event
)
1742 const char *event_name
;
1744 DBG("Disable event command for event \'%s\'", event
->name
);
1746 event_name
= event
->name
;
1748 /* Error out on unhandled search criteria */
1749 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1750 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1751 ret
= LTTNG_ERR_UNK
;
1758 case LTTNG_DOMAIN_KERNEL
:
1760 struct ltt_kernel_channel
*kchan
;
1761 struct ltt_kernel_session
*ksess
;
1763 ksess
= session
->kernel_session
;
1766 * If a non-default channel has been created in the
1767 * session, explicitely require that -c chan_name needs
1770 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1771 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1775 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1776 if (kchan
== NULL
) {
1777 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1781 switch (event
->type
) {
1782 case LTTNG_EVENT_ALL
:
1783 case LTTNG_EVENT_TRACEPOINT
:
1784 case LTTNG_EVENT_SYSCALL
:
1785 case LTTNG_EVENT_PROBE
:
1786 case LTTNG_EVENT_FUNCTION
:
1787 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1788 if (event_name
[0] == '\0') {
1789 ret
= event_kernel_disable_event(kchan
,
1792 ret
= event_kernel_disable_event(kchan
,
1793 event_name
, event
->type
);
1795 if (ret
!= LTTNG_OK
) {
1800 ret
= LTTNG_ERR_UNK
;
1804 kernel_wait_quiescent();
1807 case LTTNG_DOMAIN_UST
:
1809 struct ltt_ust_channel
*uchan
;
1810 struct ltt_ust_session
*usess
;
1812 usess
= session
->ust_session
;
1814 if (validate_ust_event_name(event_name
)) {
1815 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1820 * If a non-default channel has been created in the
1821 * session, explicitly require that -c chan_name needs
1824 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1825 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1829 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1831 if (uchan
== NULL
) {
1832 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1836 switch (event
->type
) {
1837 case LTTNG_EVENT_ALL
:
1839 * An empty event name means that everything
1840 * should be disabled.
1842 if (event
->name
[0] == '\0') {
1843 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1845 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1848 if (ret
!= LTTNG_OK
) {
1853 ret
= LTTNG_ERR_UNK
;
1857 DBG3("Disable UST event %s in channel %s completed", event_name
,
1861 case LTTNG_DOMAIN_LOG4J
:
1862 case LTTNG_DOMAIN_JUL
:
1863 case LTTNG_DOMAIN_PYTHON
:
1866 struct ltt_ust_session
*usess
= session
->ust_session
;
1870 switch (event
->type
) {
1871 case LTTNG_EVENT_ALL
:
1874 ret
= LTTNG_ERR_UNK
;
1878 agt
= trace_ust_find_agent(usess
, domain
);
1880 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1884 * An empty event name means that everything
1885 * should be disabled.
1887 if (event
->name
[0] == '\0') {
1888 ret
= event_agent_disable_all(usess
, agt
);
1890 ret
= event_agent_disable(usess
, agt
, event_name
);
1892 if (ret
!= LTTNG_OK
) {
1899 ret
= LTTNG_ERR_UND
;
1912 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1914 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1915 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1917 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1918 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1921 * Don't try to add a context if the session has been started at
1922 * some point in time before. The tracer does not allow it and would
1923 * result in a corrupted trace.
1925 if (session
->has_been_started
) {
1926 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1930 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1931 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1932 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1936 case LTTNG_DOMAIN_KERNEL
:
1937 assert(session
->kernel_session
);
1939 if (session
->kernel_session
->channel_count
== 0) {
1940 /* Create default channel */
1941 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1942 if (ret
!= LTTNG_OK
) {
1945 chan_kern_created
= 1;
1947 /* Add kernel context to kernel tracer */
1948 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1949 if (ret
!= LTTNG_OK
) {
1953 case LTTNG_DOMAIN_JUL
:
1954 case LTTNG_DOMAIN_LOG4J
:
1957 * Validate channel name.
1958 * If no channel name is given and the domain is JUL or LOG4J,
1959 * set it to the appropriate domain-specific channel name. If
1960 * a name is provided but does not match the expexted channel
1961 * name, return an error.
1963 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1964 strcmp(channel_name
,
1965 DEFAULT_JUL_CHANNEL_NAME
)) {
1966 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1968 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1969 strcmp(channel_name
,
1970 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1971 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1974 /* break is _not_ missing here. */
1976 case LTTNG_DOMAIN_UST
:
1978 struct ltt_ust_session
*usess
= session
->ust_session
;
1979 unsigned int chan_count
;
1983 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1984 if (chan_count
== 0) {
1985 struct lttng_channel
*attr
;
1986 /* Create default channel */
1987 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1989 ret
= LTTNG_ERR_FATAL
;
1993 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1994 if (ret
!= LTTNG_OK
) {
1998 channel_attr_destroy(attr
);
1999 chan_ust_created
= 1;
2002 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2003 free(app_ctx_provider_name
);
2005 app_ctx_name
= NULL
;
2006 app_ctx_provider_name
= NULL
;
2007 if (ret
!= LTTNG_OK
) {
2013 ret
= LTTNG_ERR_UND
;
2021 if (chan_kern_created
) {
2022 struct ltt_kernel_channel
*kchan
=
2023 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2024 session
->kernel_session
);
2025 /* Created previously, this should NOT fail. */
2027 kernel_destroy_channel(kchan
);
2030 if (chan_ust_created
) {
2031 struct ltt_ust_channel
*uchan
=
2032 trace_ust_find_channel_by_name(
2033 session
->ust_session
->domain_global
.channels
,
2034 DEFAULT_CHANNEL_NAME
);
2035 /* Created previously, this should NOT fail. */
2037 /* Remove from the channel list of the session. */
2038 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2040 trace_ust_destroy_channel(uchan
);
2043 free(app_ctx_provider_name
);
2048 static inline bool name_starts_with(const char *name
, const char *prefix
)
2050 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2052 return !strncmp(name
, prefix
, max_cmp_len
);
2055 /* Perform userspace-specific event name validation */
2056 static int validate_ust_event_name(const char *name
)
2066 * Check name against all internal UST event component namespaces used
2069 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2070 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2071 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2080 * Internal version of cmd_enable_event() with a supplemental
2081 * "internal_event" flag which is used to enable internal events which should
2082 * be hidden from clients. Such events are used in the agent implementation to
2083 * enable the events through which all "agent" events are funeled.
2085 static int _cmd_enable_event(struct ltt_session
*session
,
2086 const struct lttng_domain
*domain
,
2087 char *channel_name
, struct lttng_event
*event
,
2088 char *filter_expression
,
2089 struct lttng_bytecode
*filter
,
2090 struct lttng_event_exclusion
*exclusion
,
2091 int wpipe
, bool internal_event
)
2093 int ret
= 0, channel_created
= 0;
2094 struct lttng_channel
*attr
= NULL
;
2098 assert(channel_name
);
2100 /* If we have a filter, we must have its filter expression */
2101 assert(!(!!filter_expression
^ !!filter
));
2103 /* Normalize event name as a globbing pattern */
2104 strutils_normalize_star_glob_pattern(event
->name
);
2106 /* Normalize exclusion names as globbing patterns */
2110 for (i
= 0; i
< exclusion
->count
; i
++) {
2111 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2113 strutils_normalize_star_glob_pattern(name
);
2117 DBG("Enable event command for event \'%s\'", event
->name
);
2121 switch (domain
->type
) {
2122 case LTTNG_DOMAIN_KERNEL
:
2124 struct ltt_kernel_channel
*kchan
;
2127 * If a non-default channel has been created in the
2128 * session, explicitely require that -c chan_name needs
2131 if (session
->kernel_session
->has_non_default_channel
2132 && channel_name
[0] == '\0') {
2133 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2137 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2138 session
->kernel_session
);
2139 if (kchan
== NULL
) {
2140 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2141 LTTNG_BUFFER_GLOBAL
);
2143 ret
= LTTNG_ERR_FATAL
;
2146 if (lttng_strncpy(attr
->name
, channel_name
,
2147 sizeof(attr
->name
))) {
2148 ret
= LTTNG_ERR_INVALID
;
2152 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2153 if (ret
!= LTTNG_OK
) {
2156 channel_created
= 1;
2159 /* Get the newly created kernel channel pointer */
2160 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2161 session
->kernel_session
);
2162 if (kchan
== NULL
) {
2163 /* This sould not happen... */
2164 ret
= LTTNG_ERR_FATAL
;
2168 switch (event
->type
) {
2169 case LTTNG_EVENT_ALL
:
2171 char *filter_expression_a
= NULL
;
2172 struct lttng_bytecode
*filter_a
= NULL
;
2175 * We need to duplicate filter_expression and filter,
2176 * because ownership is passed to first enable
2179 if (filter_expression
) {
2180 filter_expression_a
= strdup(filter_expression
);
2181 if (!filter_expression_a
) {
2182 ret
= LTTNG_ERR_FATAL
;
2187 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2189 free(filter_expression_a
);
2190 ret
= LTTNG_ERR_FATAL
;
2193 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2195 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2196 ret
= event_kernel_enable_event(kchan
, event
,
2197 filter_expression
, filter
);
2198 /* We have passed ownership */
2199 filter_expression
= NULL
;
2201 if (ret
!= LTTNG_OK
) {
2202 if (channel_created
) {
2203 /* Let's not leak a useless channel. */
2204 kernel_destroy_channel(kchan
);
2206 free(filter_expression_a
);
2210 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2211 ret
= event_kernel_enable_event(kchan
, event
,
2212 filter_expression_a
, filter_a
);
2213 /* We have passed ownership */
2214 filter_expression_a
= NULL
;
2216 if (ret
!= LTTNG_OK
) {
2221 case LTTNG_EVENT_PROBE
:
2222 case LTTNG_EVENT_USERSPACE_PROBE
:
2223 case LTTNG_EVENT_FUNCTION
:
2224 case LTTNG_EVENT_FUNCTION_ENTRY
:
2225 case LTTNG_EVENT_TRACEPOINT
:
2226 ret
= event_kernel_enable_event(kchan
, event
,
2227 filter_expression
, filter
);
2228 /* We have passed ownership */
2229 filter_expression
= NULL
;
2231 if (ret
!= LTTNG_OK
) {
2232 if (channel_created
) {
2233 /* Let's not leak a useless channel. */
2234 kernel_destroy_channel(kchan
);
2239 case LTTNG_EVENT_SYSCALL
:
2240 ret
= event_kernel_enable_event(kchan
, event
,
2241 filter_expression
, filter
);
2242 /* We have passed ownership */
2243 filter_expression
= NULL
;
2245 if (ret
!= LTTNG_OK
) {
2250 ret
= LTTNG_ERR_UNK
;
2254 kernel_wait_quiescent();
2257 case LTTNG_DOMAIN_UST
:
2259 struct ltt_ust_channel
*uchan
;
2260 struct ltt_ust_session
*usess
= session
->ust_session
;
2265 * If a non-default channel has been created in the
2266 * session, explicitely require that -c chan_name needs
2269 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2270 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2274 /* Get channel from global UST domain */
2275 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2277 if (uchan
== NULL
) {
2278 /* Create default channel */
2279 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2280 usess
->buffer_type
);
2282 ret
= LTTNG_ERR_FATAL
;
2285 if (lttng_strncpy(attr
->name
, channel_name
,
2286 sizeof(attr
->name
))) {
2287 ret
= LTTNG_ERR_INVALID
;
2291 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2292 if (ret
!= LTTNG_OK
) {
2296 /* Get the newly created channel reference back */
2297 uchan
= trace_ust_find_channel_by_name(
2298 usess
->domain_global
.channels
, channel_name
);
2302 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2304 * Don't allow users to add UST events to channels which
2305 * are assigned to a userspace subdomain (JUL, Log4J,
2308 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2312 if (!internal_event
) {
2314 * Ensure the event name is not reserved for internal
2317 ret
= validate_ust_event_name(event
->name
);
2319 WARN("Userspace event name %s failed validation.",
2321 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2326 /* At this point, the session and channel exist on the tracer */
2327 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2328 filter_expression
, filter
, exclusion
,
2330 /* We have passed ownership */
2331 filter_expression
= NULL
;
2334 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2335 goto already_enabled
;
2336 } else if (ret
!= LTTNG_OK
) {
2341 case LTTNG_DOMAIN_LOG4J
:
2342 case LTTNG_DOMAIN_JUL
:
2343 case LTTNG_DOMAIN_PYTHON
:
2345 const char *default_event_name
, *default_chan_name
;
2347 struct lttng_event uevent
;
2348 struct lttng_domain tmp_dom
;
2349 struct ltt_ust_session
*usess
= session
->ust_session
;
2353 if (!agent_tracing_is_enabled()) {
2354 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2355 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2359 agt
= trace_ust_find_agent(usess
, domain
->type
);
2361 agt
= agent_create(domain
->type
);
2363 ret
= LTTNG_ERR_NOMEM
;
2366 agent_add(agt
, usess
->agents
);
2369 /* Create the default tracepoint. */
2370 memset(&uevent
, 0, sizeof(uevent
));
2371 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2372 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2373 default_event_name
= event_get_default_agent_ust_name(
2375 if (!default_event_name
) {
2376 ret
= LTTNG_ERR_FATAL
;
2379 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2380 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2383 * The domain type is changed because we are about to enable the
2384 * default channel and event for the JUL domain that are hardcoded.
2385 * This happens in the UST domain.
2387 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2388 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2390 switch (domain
->type
) {
2391 case LTTNG_DOMAIN_LOG4J
:
2392 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2394 case LTTNG_DOMAIN_JUL
:
2395 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2397 case LTTNG_DOMAIN_PYTHON
:
2398 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2401 /* The switch/case we are in makes this impossible */
2406 char *filter_expression_copy
= NULL
;
2407 struct lttng_bytecode
*filter_copy
= NULL
;
2410 const size_t filter_size
= sizeof(
2411 struct lttng_bytecode
)
2414 filter_copy
= zmalloc(filter_size
);
2416 ret
= LTTNG_ERR_NOMEM
;
2419 memcpy(filter_copy
, filter
, filter_size
);
2421 filter_expression_copy
=
2422 strdup(filter_expression
);
2423 if (!filter_expression
) {
2424 ret
= LTTNG_ERR_NOMEM
;
2427 if (!filter_expression_copy
|| !filter_copy
) {
2428 free(filter_expression_copy
);
2434 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2435 (char *) default_chan_name
,
2436 &uevent
, filter_expression_copy
,
2437 filter_copy
, NULL
, wpipe
);
2440 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2441 goto already_enabled
;
2442 } else if (ret
!= LTTNG_OK
) {
2446 /* The wild card * means that everything should be enabled. */
2447 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2448 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2451 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2455 filter_expression
= NULL
;
2456 if (ret
!= LTTNG_OK
) {
2463 ret
= LTTNG_ERR_UND
;
2471 free(filter_expression
);
2474 channel_attr_destroy(attr
);
2480 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2481 * We own filter, exclusion, and filter_expression.
2483 int cmd_enable_event(struct ltt_session
*session
,
2484 const struct lttng_domain
*domain
,
2485 char *channel_name
, struct lttng_event
*event
,
2486 char *filter_expression
,
2487 struct lttng_bytecode
*filter
,
2488 struct lttng_event_exclusion
*exclusion
,
2491 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2492 filter_expression
, filter
, exclusion
, wpipe
, false);
2496 * Enable an event which is internal to LTTng. An internal should
2497 * never be made visible to clients and are immune to checks such as
2500 static int cmd_enable_event_internal(struct ltt_session
*session
,
2501 const struct lttng_domain
*domain
,
2502 char *channel_name
, struct lttng_event
*event
,
2503 char *filter_expression
,
2504 struct lttng_bytecode
*filter
,
2505 struct lttng_event_exclusion
*exclusion
,
2508 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2509 filter_expression
, filter
, exclusion
, wpipe
, true);
2513 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2515 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2516 struct lttng_event
**events
)
2519 ssize_t nb_events
= 0;
2522 case LTTNG_DOMAIN_KERNEL
:
2523 nb_events
= kernel_list_events(events
);
2524 if (nb_events
< 0) {
2525 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2529 case LTTNG_DOMAIN_UST
:
2530 nb_events
= ust_app_list_events(events
);
2531 if (nb_events
< 0) {
2532 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2536 case LTTNG_DOMAIN_LOG4J
:
2537 case LTTNG_DOMAIN_JUL
:
2538 case LTTNG_DOMAIN_PYTHON
:
2539 nb_events
= agent_list_events(events
, domain
);
2540 if (nb_events
< 0) {
2541 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2546 ret
= LTTNG_ERR_UND
;
2553 /* Return negative value to differentiate return code */
2558 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2560 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2561 struct lttng_event_field
**fields
)
2564 ssize_t nb_fields
= 0;
2567 case LTTNG_DOMAIN_UST
:
2568 nb_fields
= ust_app_list_event_fields(fields
);
2569 if (nb_fields
< 0) {
2570 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2574 case LTTNG_DOMAIN_KERNEL
:
2575 default: /* fall-through */
2576 ret
= LTTNG_ERR_UND
;
2583 /* Return negative value to differentiate return code */
2587 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2589 return syscall_table_list(events
);
2593 * Command LTTNG_START_TRACE processed by the client thread.
2595 * Called with session mutex held.
2597 int cmd_start_trace(struct ltt_session
*session
)
2599 enum lttng_error_code ret
;
2600 unsigned long nb_chan
= 0;
2601 struct ltt_kernel_session
*ksession
;
2602 struct ltt_ust_session
*usess
;
2603 const bool session_rotated_after_last_stop
=
2604 session
->rotated_after_last_stop
;
2605 const bool session_cleared_after_last_stop
=
2606 session
->cleared_after_last_stop
;
2610 /* Ease our life a bit ;) */
2611 ksession
= session
->kernel_session
;
2612 usess
= session
->ust_session
;
2614 /* Is the session already started? */
2615 if (session
->active
) {
2616 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2617 /* Perform nothing */
2621 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2622 !session
->current_trace_chunk
) {
2624 * A rotation was launched while the session was stopped and
2625 * it has not been completed yet. It is not possible to start
2626 * the session since starting the session here would require a
2627 * rotation from "NULL" to a new trace chunk. That rotation
2628 * would overlap with the ongoing rotation, which is not
2631 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2633 ret
= LTTNG_ERR_ROTATION_PENDING
;
2638 * Starting a session without channel is useless since after that it's not
2639 * possible to enable channel thus inform the client.
2641 if (usess
&& usess
->domain_global
.channels
) {
2642 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2645 nb_chan
+= ksession
->channel_count
;
2648 ret
= LTTNG_ERR_NO_CHANNEL
;
2652 session
->active
= 1;
2653 session
->rotated_after_last_stop
= false;
2654 session
->cleared_after_last_stop
= false;
2655 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2656 if (!session
->has_been_started
) {
2657 struct lttng_trace_chunk
*trace_chunk
;
2659 DBG("Creating initial trace chunk of session \"%s\"",
2661 trace_chunk
= session_create_new_trace_chunk(
2662 session
, NULL
, NULL
, NULL
);
2664 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2667 assert(!session
->current_trace_chunk
);
2668 ret
= session_set_trace_chunk(session
, trace_chunk
,
2670 lttng_trace_chunk_put(trace_chunk
);
2672 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2676 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2679 * Rotate existing streams into the new chunk.
2680 * This is a "quiet" rotation has no client has
2681 * explicitly requested this operation.
2683 * There is also no need to wait for the rotation
2684 * to complete as it will happen immediately. No data
2685 * was produced as the session was stopped, so the
2686 * rotation should happen on reception of the command.
2688 ret
= cmd_rotate_session(session
, NULL
, true,
2689 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2690 if (ret
!= LTTNG_OK
) {
2696 /* Kernel tracing */
2697 if (ksession
!= NULL
) {
2698 DBG("Start kernel tracing session %s", session
->name
);
2699 ret
= start_kernel_session(ksession
);
2700 if (ret
!= LTTNG_OK
) {
2705 /* Flag session that trace should start automatically */
2707 int int_ret
= ust_app_start_trace_all(usess
);
2710 ret
= LTTNG_ERR_UST_START_FAIL
;
2716 * Open a packet in every stream of the session to ensure that viewers
2717 * can correctly identify the boundaries of the periods during which
2718 * tracing was active for this session.
2720 ret
= session_open_packets(session
);
2721 if (ret
!= LTTNG_OK
) {
2726 * Clear the flag that indicates that a rotation was done while the
2727 * session was stopped.
2729 session
->rotated_after_last_stop
= false;
2731 if (session
->rotate_timer_period
) {
2732 int int_ret
= timer_session_rotation_schedule_timer_start(
2733 session
, session
->rotate_timer_period
);
2736 ERR("Failed to enable rotate timer");
2737 ret
= LTTNG_ERR_UNK
;
2745 if (ret
== LTTNG_OK
) {
2746 /* Flag this after a successful start. */
2747 session
->has_been_started
|= 1;
2749 session
->active
= 0;
2750 /* Restore initial state on error. */
2751 session
->rotated_after_last_stop
=
2752 session_rotated_after_last_stop
;
2753 session
->cleared_after_last_stop
=
2754 session_cleared_after_last_stop
;
2761 * Command LTTNG_STOP_TRACE processed by the client thread.
2763 int cmd_stop_trace(struct ltt_session
*session
)
2766 struct ltt_kernel_session
*ksession
;
2767 struct ltt_ust_session
*usess
;
2771 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2773 ksession
= session
->kernel_session
;
2774 usess
= session
->ust_session
;
2776 /* Session is not active. Skip everythong and inform the client. */
2777 if (!session
->active
) {
2778 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2782 ret
= stop_kernel_session(ksession
);
2783 if (ret
!= LTTNG_OK
) {
2787 if (usess
&& usess
->active
) {
2788 ret
= ust_app_stop_trace_all(usess
);
2790 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2795 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2797 /* Flag inactive after a successful stop. */
2798 session
->active
= 0;
2806 * Set the base_path of the session only if subdir of a control uris is set.
2807 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2809 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2811 struct lttng_uri
*uris
)
2816 for (i
= 0; i
< nb_uri
; i
++) {
2817 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2818 uris
[i
].subdir
[0] == '\0') {
2819 /* Not interested in these URIs */
2823 if (session
->base_path
!= NULL
) {
2824 free(session
->base_path
);
2825 session
->base_path
= NULL
;
2828 /* Set session base_path */
2829 session
->base_path
= strdup(uris
[i
].subdir
);
2830 if (!session
->base_path
) {
2831 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2832 uris
[i
].subdir
, session
->name
);
2833 ret
= LTTNG_ERR_NOMEM
;
2836 DBG2("Setting base path \"%s\" for session \"%s\"",
2837 session
->base_path
, session
->name
);
2845 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2847 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2848 struct lttng_uri
*uris
)
2851 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2852 struct ltt_ust_session
*usess
= session
->ust_session
;
2858 /* Can't set consumer URI if the session is active. */
2859 if (session
->active
) {
2860 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2865 * Set the session base path if any. This is done inside
2866 * cmd_set_consumer_uri to preserve backward compatibility of the
2867 * previous session creation api vs the session descriptor api.
2869 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2870 if (ret
!= LTTNG_OK
) {
2874 /* Set the "global" consumer URIs */
2875 for (i
= 0; i
< nb_uri
; i
++) {
2876 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2878 if (ret
!= LTTNG_OK
) {
2883 /* Set UST session URIs */
2884 if (session
->ust_session
) {
2885 for (i
= 0; i
< nb_uri
; i
++) {
2886 ret
= add_uri_to_consumer(session
,
2887 session
->ust_session
->consumer
,
2888 &uris
[i
], LTTNG_DOMAIN_UST
);
2889 if (ret
!= LTTNG_OK
) {
2895 /* Set kernel session URIs */
2896 if (session
->kernel_session
) {
2897 for (i
= 0; i
< nb_uri
; i
++) {
2898 ret
= add_uri_to_consumer(session
,
2899 session
->kernel_session
->consumer
,
2900 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2901 if (ret
!= LTTNG_OK
) {
2908 * Make sure to set the session in output mode after we set URI since a
2909 * session can be created without URL (thus flagged in no output mode).
2911 session
->output_traces
= 1;
2913 ksess
->output_traces
= 1;
2917 usess
->output_traces
= 1;
2928 enum lttng_error_code
set_session_output_from_descriptor(
2929 struct ltt_session
*session
,
2930 const struct lttng_session_descriptor
*descriptor
)
2933 enum lttng_error_code ret_code
= LTTNG_OK
;
2934 enum lttng_session_descriptor_type session_type
=
2935 lttng_session_descriptor_get_type(descriptor
);
2936 enum lttng_session_descriptor_output_type output_type
=
2937 lttng_session_descriptor_get_output_type(descriptor
);
2938 struct lttng_uri uris
[2] = {};
2939 size_t uri_count
= 0;
2941 switch (output_type
) {
2942 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2944 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2945 lttng_session_descriptor_get_local_output_uri(descriptor
,
2949 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2950 lttng_session_descriptor_get_network_output_uris(descriptor
,
2951 &uris
[0], &uris
[1]);
2955 ret_code
= LTTNG_ERR_INVALID
;
2959 switch (session_type
) {
2960 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2962 struct snapshot_output
*new_output
= NULL
;
2964 new_output
= snapshot_output_alloc();
2966 ret_code
= LTTNG_ERR_NOMEM
;
2970 ret
= snapshot_output_init_with_uri(session
,
2971 DEFAULT_SNAPSHOT_MAX_SIZE
,
2972 NULL
, uris
, uri_count
, session
->consumer
,
2973 new_output
, &session
->snapshot
);
2975 ret_code
= (ret
== -ENOMEM
) ?
2976 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2977 snapshot_output_destroy(new_output
);
2980 snapshot_add_output(&session
->snapshot
, new_output
);
2983 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2984 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2986 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2990 ret_code
= LTTNG_ERR_INVALID
;
2998 enum lttng_error_code
cmd_create_session_from_descriptor(
2999 struct lttng_session_descriptor
*descriptor
,
3000 const lttng_sock_cred
*creds
,
3001 const char *home_path
)
3004 enum lttng_error_code ret_code
;
3005 const char *session_name
;
3006 struct ltt_session
*new_session
= NULL
;
3007 enum lttng_session_descriptor_status descriptor_status
;
3009 session_lock_list();
3011 if (*home_path
!= '/') {
3012 ERR("Home path provided by client is not absolute");
3013 ret_code
= LTTNG_ERR_INVALID
;
3018 descriptor_status
= lttng_session_descriptor_get_session_name(
3019 descriptor
, &session_name
);
3020 switch (descriptor_status
) {
3021 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3023 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3024 session_name
= NULL
;
3027 ret_code
= LTTNG_ERR_INVALID
;
3031 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3033 if (ret_code
!= LTTNG_OK
) {
3037 if (!session_name
) {
3038 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3041 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3046 if (!lttng_session_descriptor_is_output_destination_initialized(
3049 * Only include the session's creation time in the output
3050 * destination if the name of the session itself was
3051 * not auto-generated.
3053 ret_code
= lttng_session_descriptor_set_default_output(
3055 session_name
? &new_session
->creation_time
: NULL
,
3057 if (ret_code
!= LTTNG_OK
) {
3061 new_session
->has_user_specified_directory
=
3062 lttng_session_descriptor_has_output_directory(
3066 switch (lttng_session_descriptor_get_type(descriptor
)) {
3067 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3068 new_session
->snapshot_mode
= 1;
3070 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3071 new_session
->live_timer
=
3072 lttng_session_descriptor_live_get_timer_interval(
3079 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3080 if (ret_code
!= LTTNG_OK
) {
3083 new_session
->consumer
->enabled
= 1;
3084 ret_code
= LTTNG_OK
;
3086 /* Release reference provided by the session_create function. */
3087 session_put(new_session
);
3088 if (ret_code
!= LTTNG_OK
&& new_session
) {
3089 /* Release the global reference on error. */
3090 session_destroy(new_session
);
3092 session_unlock_list();
3096 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3097 struct lttng_session_descriptor
**return_descriptor
)
3100 size_t payload_size
;
3101 struct lttng_dynamic_buffer payload
;
3102 struct lttng_buffer_view home_dir_view
;
3103 struct lttng_buffer_view session_descriptor_view
;
3104 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3105 enum lttng_error_code ret_code
;
3107 lttng_dynamic_buffer_init(&payload
);
3108 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3110 ret_code
= LTTNG_ERR_INVALID
;
3113 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3114 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3115 ret_code
= LTTNG_ERR_INVALID
;
3119 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3120 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3121 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3123 ret_code
= LTTNG_ERR_NOMEM
;
3127 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3129 ERR("Reception of session descriptor failed, aborting.");
3130 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3134 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3137 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3138 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3139 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3140 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3141 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3145 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3147 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3148 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3149 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3150 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3151 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3155 ret
= lttng_session_descriptor_create_from_buffer(
3156 &session_descriptor_view
, &session_descriptor
);
3158 ERR("Failed to create session descriptor from payload of \"create session\" command");
3159 ret_code
= LTTNG_ERR_INVALID
;
3164 * Sets the descriptor's auto-generated properties (name, output) if
3167 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3169 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3170 if (ret_code
!= LTTNG_OK
) {
3174 ret_code
= LTTNG_OK
;
3175 *return_descriptor
= session_descriptor
;
3176 session_descriptor
= NULL
;
3178 lttng_dynamic_buffer_reset(&payload
);
3179 lttng_session_descriptor_destroy(session_descriptor
);
3184 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3185 void *_reply_context
)
3189 const struct cmd_destroy_session_reply_context
*reply_context
=
3191 struct lttng_dynamic_buffer payload
;
3192 struct lttcomm_session_destroy_command_header cmd_header
;
3193 struct lttng_trace_archive_location
*location
= NULL
;
3194 struct lttcomm_lttng_msg llm
= {
3195 .cmd_type
= LTTNG_DESTROY_SESSION
,
3196 .ret_code
= reply_context
->destruction_status
,
3199 sizeof(struct lttcomm_session_destroy_command_header
),
3202 size_t payload_size_before_location
;
3204 lttng_dynamic_buffer_init(&payload
);
3206 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3208 ERR("Failed to append session destruction message");
3212 cmd_header
.rotation_state
=
3213 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3214 session
->rotation_state
:
3215 LTTNG_ROTATION_STATE_NO_ROTATION
);
3216 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3217 sizeof(cmd_header
));
3219 ERR("Failed to append session destruction command header");
3223 if (!reply_context
->implicit_rotation_on_destroy
) {
3224 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3228 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3229 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3234 location
= session_get_trace_archive_location(session
);
3236 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3241 payload_size_before_location
= payload
.size
;
3242 comm_ret
= lttng_trace_archive_location_serialize(location
,
3244 lttng_trace_archive_location_put(location
);
3246 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3250 /* Update the message to indicate the location's length. */
3251 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3252 payload
.size
- payload_size_before_location
;
3254 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3255 payload
.data
, payload
.size
);
3256 if (comm_ret
!= (ssize_t
) payload
.size
) {
3257 ERR("Failed to send result of the destruction of session \"%s\" to client",
3261 ret
= close(reply_context
->reply_sock_fd
);
3263 PERROR("Failed to close client socket in deferred session destroy reply");
3265 lttng_dynamic_buffer_reset(&payload
);
3266 free(_reply_context
);
3270 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3272 * Called with session lock held.
3274 int cmd_destroy_session(struct ltt_session
*session
,
3275 struct notification_thread_handle
*notification_thread_handle
,
3279 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3280 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3283 reply_context
= zmalloc(sizeof(*reply_context
));
3284 if (!reply_context
) {
3285 ret
= LTTNG_ERR_NOMEM
;
3288 reply_context
->reply_sock_fd
= *sock_fd
;
3294 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3296 if (session
->active
) {
3297 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3299 ret
= cmd_stop_trace(session
);
3300 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3301 /* Carry on with the destruction of the session. */
3302 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3303 session
->name
, lttng_strerror(-ret
));
3304 destruction_last_error
= ret
;
3308 if (session
->rotation_schedule_timer_enabled
) {
3309 if (timer_session_rotation_schedule_timer_stop(
3311 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3313 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3317 if (session
->rotate_size
) {
3318 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3319 session
->rotate_size
= 0;
3322 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3324 * Perform a last rotation on destruction if rotations have
3325 * occurred during the session's lifetime.
3327 ret
= cmd_rotate_session(session
, NULL
, false,
3328 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3329 if (ret
!= LTTNG_OK
) {
3330 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3331 session
->name
, lttng_strerror(-ret
));
3332 destruction_last_error
= -ret
;
3334 if (reply_context
) {
3335 reply_context
->implicit_rotation_on_destroy
= true;
3337 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3339 * The user has not triggered a session rotation. However, to
3340 * ensure all data has been consumed, the session is rotated
3341 * to a 'null' trace chunk before it is destroyed.
3343 * This is a "quiet" rotation meaning that no notification is
3344 * emitted and no renaming of the current trace chunk takes
3347 ret
= cmd_rotate_session(session
, NULL
, true,
3348 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3350 * Rotation operations may not be supported by the kernel
3351 * tracer. Hence, do not consider this implicit rotation as
3352 * a session destruction error. The library has already stopped
3353 * the session and waited for pending data; there is nothing
3354 * left to do but complete the destruction of the session.
3356 if (ret
!= LTTNG_OK
&&
3357 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3358 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3359 session
->name
, lttng_strerror(ret
));
3360 destruction_last_error
= -ret
;
3364 if (session
->shm_path
[0]) {
3366 * When a session is created with an explicit shm_path,
3367 * the consumer daemon will create its shared memory files
3368 * at that location and will *not* unlink them. This is normal
3369 * as the intention of that feature is to make it possible
3370 * to retrieve the content of those files should a crash occur.
3372 * To ensure the content of those files can be used, the
3373 * sessiond daemon will replicate the content of the metadata
3374 * cache in a metadata file.
3376 * On clean-up, it is expected that the consumer daemon will
3377 * unlink the shared memory files and that the session daemon
3378 * will unlink the metadata file. Then, the session's directory
3379 * in the shm path can be removed.
3381 * Unfortunately, a flaw in the design of the sessiond's and
3382 * consumerd's tear down of channels makes it impossible to
3383 * determine when the sessiond _and_ the consumerd have both
3384 * destroyed their representation of a channel. For one, the
3385 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3386 * callbacks in both daemons.
3388 * However, it is also impossible for the sessiond to know when
3389 * the consumer daemon is done destroying its channel(s) since
3390 * it occurs as a reaction to the closing of the channel's file
3391 * descriptor. There is no resulting communication initiated
3392 * from the consumerd to the sessiond to confirm that the
3393 * operation is completed (and was successful).
3395 * Until this is all fixed, the session daemon checks for the
3396 * removal of the session's shm path which makes it possible
3397 * to safely advertise a session as having been destroyed.
3399 * Prior to this fix, it was not possible to reliably save
3400 * a session making use of the --shm-path option, destroy it,
3401 * and load it again. This is because the creation of the
3402 * session would fail upon seeing the session's shm path
3403 * already in existence.
3405 * Note that none of the error paths in the check for the
3406 * directory's existence return an error. This is normal
3407 * as there isn't much that can be done. The session will
3408 * be destroyed properly, except that we can't offer the
3409 * guarantee that the same session can be re-created.
3411 current_completion_handler
= &destroy_completion_handler
.handler
;
3412 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3414 sizeof(destroy_completion_handler
.shm_path
));
3419 * The session is destroyed. However, note that the command context
3420 * still holds a reference to the session, thus delaying its destruction
3421 * _at least_ up to the point when that reference is released.
3423 session_destroy(session
);
3424 if (reply_context
) {
3425 reply_context
->destruction_status
= destruction_last_error
;
3426 ret
= session_add_destroy_notifier(session
,
3427 cmd_destroy_session_reply
,
3428 (void *) reply_context
);
3430 ret
= LTTNG_ERR_FATAL
;
3442 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3444 int cmd_register_consumer(struct ltt_session
*session
,
3445 enum lttng_domain_type domain
, const char *sock_path
,
3446 struct consumer_data
*cdata
)
3449 struct consumer_socket
*socket
= NULL
;
3456 case LTTNG_DOMAIN_KERNEL
:
3458 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3462 /* Can't register a consumer if there is already one */
3463 if (ksess
->consumer_fds_sent
!= 0) {
3464 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3468 sock
= lttcomm_connect_unix_sock(sock_path
);
3470 ret
= LTTNG_ERR_CONNECT_FAIL
;
3473 cdata
->cmd_sock
= sock
;
3475 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3476 if (socket
== NULL
) {
3479 PERROR("close register consumer");
3481 cdata
->cmd_sock
= -1;
3482 ret
= LTTNG_ERR_FATAL
;
3486 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3487 if (socket
->lock
== NULL
) {
3488 PERROR("zmalloc pthread mutex");
3489 ret
= LTTNG_ERR_FATAL
;
3492 pthread_mutex_init(socket
->lock
, NULL
);
3493 socket
->registered
= 1;
3496 consumer_add_socket(socket
, ksess
->consumer
);
3499 pthread_mutex_lock(&cdata
->pid_mutex
);
3501 pthread_mutex_unlock(&cdata
->pid_mutex
);
3506 /* TODO: Userspace tracing */
3507 ret
= LTTNG_ERR_UND
;
3515 consumer_destroy_socket(socket
);
3521 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3523 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3524 struct lttng_domain
**domains
)
3529 struct lttng_ht_iter iter
;
3531 if (session
->kernel_session
!= NULL
) {
3532 DBG3("Listing domains found kernel domain");
3536 if (session
->ust_session
!= NULL
) {
3537 DBG3("Listing domains found UST global domain");
3541 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3543 if (agt
->being_used
) {
3554 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3555 if (*domains
== NULL
) {
3556 ret
= LTTNG_ERR_FATAL
;
3560 if (session
->kernel_session
!= NULL
) {
3561 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3563 /* Kernel session buffer type is always GLOBAL */
3564 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3569 if (session
->ust_session
!= NULL
) {
3570 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3571 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3575 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3577 if (agt
->being_used
) {
3578 (*domains
)[index
].type
= agt
->domain
;
3579 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3589 /* Return negative value to differentiate return code */
3595 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3597 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3598 struct ltt_session
*session
, struct lttng_channel
**channels
)
3600 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3603 case LTTNG_DOMAIN_KERNEL
:
3604 if (session
->kernel_session
!= NULL
) {
3605 nb_chan
= session
->kernel_session
->channel_count
;
3607 DBG3("Number of kernel channels %zd", nb_chan
);
3609 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3613 case LTTNG_DOMAIN_UST
:
3614 if (session
->ust_session
!= NULL
) {
3616 nb_chan
= lttng_ht_get_count(
3617 session
->ust_session
->domain_global
.channels
);
3620 DBG3("Number of UST global channels %zd", nb_chan
);
3622 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3627 ret
= -LTTNG_ERR_UND
;
3632 const size_t channel_size
= sizeof(struct lttng_channel
) +
3633 sizeof(struct lttng_channel_extended
);
3634 struct lttng_channel_extended
*channel_exts
;
3636 payload_size
= nb_chan
* channel_size
;
3637 *channels
= zmalloc(payload_size
);
3638 if (*channels
== NULL
) {
3639 ret
= -LTTNG_ERR_FATAL
;
3643 channel_exts
= ((void *) *channels
) +
3644 (nb_chan
* sizeof(struct lttng_channel
));
3645 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3646 if (ret
!= LTTNG_OK
) {
3661 * Command LTTNG_LIST_EVENTS processed by the client thread.
3663 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3664 struct ltt_session
*session
, char *channel_name
,
3665 struct lttng_payload
*payload
)
3668 ssize_t nb_events
= 0;
3669 struct lttcomm_event_command_header cmd_header
= {};
3670 const size_t cmd_header_offset
= payload
->buffer
.size
;
3672 ret
= lttng_dynamic_buffer_append(
3673 &payload
->buffer
, &cmd_header
, sizeof(cmd_header
));
3675 ret
= LTTNG_ERR_NOMEM
;
3680 case LTTNG_DOMAIN_KERNEL
:
3681 if (session
->kernel_session
!= NULL
) {
3682 nb_events
= list_lttng_kernel_events(channel_name
,
3683 session
->kernel_session
, payload
);
3686 case LTTNG_DOMAIN_UST
:
3688 if (session
->ust_session
!= NULL
) {
3689 nb_events
= list_lttng_ust_global_events(channel_name
,
3690 &session
->ust_session
->domain_global
,
3695 case LTTNG_DOMAIN_LOG4J
:
3696 case LTTNG_DOMAIN_JUL
:
3697 case LTTNG_DOMAIN_PYTHON
:
3698 if (session
->ust_session
) {
3699 struct lttng_ht_iter iter
;
3703 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3704 &iter
.iter
, agt
, node
.node
) {
3705 if (agt
->domain
== domain
) {
3706 nb_events
= list_lttng_agent_events(
3715 ret
= LTTNG_ERR_UND
;
3719 ((struct lttcomm_event_command_header
*) (payload
->buffer
.data
+
3720 cmd_header_offset
))->nb_events
= (uint32_t) nb_events
;
3725 /* Return negative value to differentiate return code */
3730 * Using the session list, filled a lttng_session array to send back to the
3731 * client for session listing.
3733 * The session list lock MUST be acquired before calling this function. Use
3734 * session_lock_list() and session_unlock_list().
3736 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3737 size_t session_count
, uid_t uid
, gid_t gid
)
3741 struct ltt_session
*session
;
3742 struct ltt_session_list
*list
= session_get_list();
3743 struct lttng_session_extended
*extended
=
3744 (typeof(extended
)) (&sessions
[session_count
]);
3746 DBG("Getting all available session for UID %d GID %d",
3749 * Iterate over session list and append data after the control struct in
3752 cds_list_for_each_entry(session
, &list
->head
, list
) {
3753 if (!session_get(session
)) {
3757 * Only list the sessions the user can control.
3759 if (!session_access_ok(session
, uid
) ||
3760 session
->destroyed
) {
3761 session_put(session
);
3765 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3766 struct ltt_ust_session
*usess
= session
->ust_session
;
3768 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3769 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3770 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3771 ret
= build_network_session_path(sessions
[i
].path
,
3772 sizeof(sessions
[i
].path
), session
);
3774 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3775 session
->consumer
->dst
.session_root_path
);
3778 PERROR("snprintf session path");
3779 session_put(session
);
3783 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3784 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3785 sessions
[i
].enabled
= session
->active
;
3786 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3787 sessions
[i
].live_timer_interval
= session
->live_timer
;
3788 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3789 extended
[i
].creation_time
.is_set
= 1;
3791 session_put(session
);
3796 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3797 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3799 int cmd_data_pending(struct ltt_session
*session
)
3802 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3803 struct ltt_ust_session
*usess
= session
->ust_session
;
3807 DBG("Data pending for session %s", session
->name
);
3809 /* Session MUST be stopped to ask for data availability. */
3810 if (session
->active
) {
3811 ret
= LTTNG_ERR_SESSION_STARTED
;
3815 * If stopped, just make sure we've started before else the above call
3816 * will always send that there is data pending.
3818 * The consumer assumes that when the data pending command is received,
3819 * the trace has been started before or else no output data is written
3820 * by the streams which is a condition for data pending. So, this is
3821 * *VERY* important that we don't ask the consumer before a start
3824 if (!session
->has_been_started
) {
3830 /* A rotation is still pending, we have to wait. */
3831 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3832 DBG("Rotate still pending for session %s", session
->name
);
3837 if (ksess
&& ksess
->consumer
) {
3838 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3840 /* Data is still being extracted for the kernel. */
3845 if (usess
&& usess
->consumer
) {
3846 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3848 /* Data is still being extracted for the kernel. */
3853 /* Data is ready to be read by a viewer */
3861 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3863 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3865 int cmd_snapshot_add_output(struct ltt_session
*session
,
3866 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3869 struct snapshot_output
*new_output
;
3874 DBG("Cmd snapshot add output for session %s", session
->name
);
3877 * Can't create an output if the session is not set in no-output mode.
3879 if (session
->output_traces
) {
3880 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3884 if (session
->has_non_mmap_channel
) {
3885 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3889 /* Only one output is allowed until we have the "tee" feature. */
3890 if (session
->snapshot
.nb_output
== 1) {
3891 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3895 new_output
= snapshot_output_alloc();
3897 ret
= LTTNG_ERR_NOMEM
;
3901 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3902 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3903 &session
->snapshot
);
3905 if (ret
== -ENOMEM
) {
3906 ret
= LTTNG_ERR_NOMEM
;
3908 ret
= LTTNG_ERR_INVALID
;
3914 snapshot_add_output(&session
->snapshot
, new_output
);
3916 *id
= new_output
->id
;
3923 snapshot_output_destroy(new_output
);
3929 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3931 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3933 int cmd_snapshot_del_output(struct ltt_session
*session
,
3934 const struct lttng_snapshot_output
*output
)
3937 struct snapshot_output
*sout
= NULL
;
3945 * Permission denied to create an output if the session is not
3946 * set in no output mode.
3948 if (session
->output_traces
) {
3949 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3954 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3956 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3957 } else if (*output
->name
!= '\0') {
3958 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3960 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3963 ret
= LTTNG_ERR_INVALID
;
3967 snapshot_delete_output(&session
->snapshot
, sout
);
3968 snapshot_output_destroy(sout
);
3977 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3979 * If no output is available, outputs is untouched and 0 is returned.
3981 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3983 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3984 struct lttng_snapshot_output
**outputs
)
3987 struct lttng_snapshot_output
*list
= NULL
;
3988 struct lttng_ht_iter iter
;
3989 struct snapshot_output
*output
;
3994 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3997 * Permission denied to create an output if the session is not
3998 * set in no output mode.
4000 if (session
->output_traces
) {
4001 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4005 if (session
->snapshot
.nb_output
== 0) {
4010 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4012 ret
= -LTTNG_ERR_NOMEM
;
4016 /* Copy list from session to the new list object. */
4018 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4019 output
, node
.node
) {
4020 assert(output
->consumer
);
4021 list
[idx
].id
= output
->id
;
4022 list
[idx
].max_size
= output
->max_size
;
4023 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4024 sizeof(list
[idx
].name
))) {
4025 ret
= -LTTNG_ERR_INVALID
;
4028 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4029 if (lttng_strncpy(list
[idx
].ctrl_url
,
4030 output
->consumer
->dst
.session_root_path
,
4031 sizeof(list
[idx
].ctrl_url
))) {
4032 ret
= -LTTNG_ERR_INVALID
;
4037 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4038 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4040 ret
= -LTTNG_ERR_NOMEM
;
4045 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4046 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4048 ret
= -LTTNG_ERR_NOMEM
;
4057 ret
= session
->snapshot
.nb_output
;
4066 * Check if we can regenerate the metadata for this session.
4067 * Only kernel, UST per-uid and non-live sessions are supported.
4069 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4072 int check_regenerate_metadata_support(struct ltt_session
*session
)
4078 if (session
->live_timer
!= 0) {
4079 ret
= LTTNG_ERR_LIVE_SESSION
;
4082 if (!session
->active
) {
4083 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4086 if (session
->ust_session
) {
4087 switch (session
->ust_session
->buffer_type
) {
4088 case LTTNG_BUFFER_PER_UID
:
4090 case LTTNG_BUFFER_PER_PID
:
4091 ret
= LTTNG_ERR_PER_PID_SESSION
;
4095 ret
= LTTNG_ERR_UNK
;
4099 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4100 session
->consumer
->relay_minor_version
< 8) {
4101 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4111 int clear_metadata_file(int fd
)
4116 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4117 if (lseek_ret
< 0) {
4123 ret
= ftruncate(fd
, 0);
4125 PERROR("ftruncate");
4134 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4137 struct buffer_reg_uid
*uid_reg
= NULL
;
4138 struct buffer_reg_session
*session_reg
= NULL
;
4141 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4142 struct ust_registry_session
*registry
;
4143 struct ust_registry_channel
*chan
;
4144 struct lttng_ht_iter iter_chan
;
4146 session_reg
= uid_reg
->registry
;
4147 registry
= session_reg
->reg
.ust
;
4149 pthread_mutex_lock(®istry
->lock
);
4150 registry
->metadata_len_sent
= 0;
4151 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4152 registry
->metadata_len
= 0;
4153 registry
->metadata_version
++;
4154 if (registry
->metadata_fd
> 0) {
4155 /* Clear the metadata file's content. */
4156 ret
= clear_metadata_file(registry
->metadata_fd
);
4158 pthread_mutex_unlock(®istry
->lock
);
4163 ret
= ust_metadata_session_statedump(registry
, NULL
,
4164 registry
->major
, registry
->minor
);
4166 pthread_mutex_unlock(®istry
->lock
);
4167 ERR("Failed to generate session metadata (err = %d)",
4171 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4173 struct ust_registry_event
*event
;
4174 struct lttng_ht_iter iter_event
;
4176 ret
= ust_metadata_channel_statedump(registry
, chan
);
4178 pthread_mutex_unlock(®istry
->lock
);
4179 ERR("Failed to generate channel metadata "
4183 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4185 ret
= ust_metadata_event_statedump(registry
,
4188 pthread_mutex_unlock(®istry
->lock
);
4189 ERR("Failed to generate event metadata "
4195 pthread_mutex_unlock(®istry
->lock
);
4204 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4206 * Ask the consumer to truncate the existing metadata file(s) and
4207 * then regenerate the metadata. Live and per-pid sessions are not
4208 * supported and return an error.
4210 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4212 int cmd_regenerate_metadata(struct ltt_session
*session
)
4218 ret
= check_regenerate_metadata_support(session
);
4223 if (session
->kernel_session
) {
4224 ret
= kernctl_session_regenerate_metadata(
4225 session
->kernel_session
->fd
);
4227 ERR("Failed to regenerate the kernel metadata");
4232 if (session
->ust_session
) {
4233 ret
= ust_regenerate_metadata(session
->ust_session
);
4235 ERR("Failed to regenerate the UST metadata");
4239 DBG("Cmd metadata regenerate for session %s", session
->name
);
4247 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4249 * Ask the tracer to regenerate a new statedump.
4251 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4253 int cmd_regenerate_statedump(struct ltt_session
*session
)
4259 if (!session
->active
) {
4260 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4264 if (session
->kernel_session
) {
4265 ret
= kernctl_session_regenerate_statedump(
4266 session
->kernel_session
->fd
);
4268 * Currently, the statedump in kernel can only fail if out
4272 if (ret
== -ENOMEM
) {
4273 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4275 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4277 ERR("Failed to regenerate the kernel statedump");
4282 if (session
->ust_session
) {
4283 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4285 * Currently, the statedump in UST always returns 0.
4288 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4289 ERR("Failed to regenerate the UST statedump");
4293 DBG("Cmd regenerate statedump for session %s", session
->name
);
4301 enum lttng_error_code
synchronize_tracer_notifier_register(
4302 struct notification_thread_handle
*notification_thread
,
4303 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4305 enum lttng_error_code ret_code
;
4306 const struct lttng_condition
*condition
=
4307 lttng_trigger_get_const_condition(trigger
);
4308 const char *trigger_name
;
4309 uid_t trigger_owner
;
4310 enum lttng_trigger_status trigger_status
;
4311 const enum lttng_domain_type trigger_domain
=
4312 lttng_trigger_get_underlying_domain_type_restriction(
4315 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4316 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4319 assert(lttng_condition_get_type(condition
) ==
4320 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4322 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4323 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4324 trigger_name
: "(anonymous)";
4326 session_lock_list();
4327 switch (trigger_domain
) {
4328 case LTTNG_DOMAIN_KERNEL
:
4330 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4331 if (ret_code
!= LTTNG_OK
) {
4332 enum lttng_error_code notif_thread_unregister_ret
;
4334 notif_thread_unregister_ret
=
4335 notification_thread_command_unregister_trigger(
4336 notification_thread
, trigger
);
4338 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4339 /* Return the original error code. */
4340 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4342 (int) trigger_owner
,
4348 case LTTNG_DOMAIN_UST
:
4349 ust_app_global_update_all_event_notifier_rules();
4351 case LTTNG_DOMAIN_JUL
:
4352 case LTTNG_DOMAIN_LOG4J
:
4353 case LTTNG_DOMAIN_PYTHON
:
4355 /* Agent domains. */
4356 struct agent
*agt
= agent_find_by_event_notifier_domain(
4360 agt
= agent_create(trigger_domain
);
4362 ret_code
= LTTNG_ERR_NOMEM
;
4363 goto end_unlock_session_list
;
4366 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4369 ret_code
= trigger_agent_enable(trigger
, agt
);
4370 if (ret_code
!= LTTNG_OK
) {
4371 goto end_unlock_session_list
;
4376 case LTTNG_DOMAIN_NONE
:
4381 ret_code
= LTTNG_OK
;
4382 end_unlock_session_list
:
4383 session_unlock_list();
4387 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4388 struct lttng_trigger
*trigger
,
4389 bool is_trigger_anonymous
,
4390 struct notification_thread_handle
*notification_thread
,
4391 struct lttng_trigger
**return_trigger
)
4393 enum lttng_error_code ret_code
;
4394 const char *trigger_name
;
4395 uid_t trigger_owner
;
4396 enum lttng_trigger_status trigger_status
;
4398 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4399 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4400 trigger_name
: "(anonymous)";
4402 trigger_status
= lttng_trigger_get_owner_uid(
4403 trigger
, &trigger_owner
);
4404 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4406 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4407 trigger_name
, (int) trigger_owner
,
4408 (int) lttng_credentials_get_uid(cmd_creds
));
4411 * Validate the trigger credentials against the command credentials.
4412 * Only the root user can register a trigger with non-matching
4415 if (!lttng_credentials_is_equal_uid(
4416 lttng_trigger_get_credentials(trigger
),
4418 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4419 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4420 trigger_name
, (int) trigger_owner
,
4421 (int) lttng_credentials_get_uid(cmd_creds
));
4422 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4428 * The bytecode generation also serves as a validation step for the
4429 * bytecode expressions.
4431 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4432 if (ret_code
!= LTTNG_OK
) {
4433 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4434 trigger_name
, (int) trigger_owner
, ret_code
);
4439 * A reference to the trigger is acquired by the notification thread.
4440 * It is safe to return the same trigger to the caller since it the
4441 * other user holds a reference.
4443 * The trigger is modified during the execution of the
4444 * "register trigger" command. However, by the time the command returns,
4445 * it is safe to use without any locking as its properties are
4448 ret_code
= notification_thread_command_register_trigger(
4449 notification_thread
, trigger
, is_trigger_anonymous
);
4450 if (ret_code
!= LTTNG_OK
) {
4451 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4452 trigger_name
, (int) trigger_owner
, ret_code
);
4456 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4457 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4458 trigger_name
: "(anonymous)";
4461 * Synchronize tracers if the trigger adds an event notifier.
4463 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4464 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4465 trigger
, cmd_creds
);
4466 if (ret_code
!= LTTNG_OK
) {
4467 ERR("Error registering tracer notifier: %s",
4468 lttng_strerror(-ret_code
));
4474 * Return an updated trigger to the client.
4476 * Since a modified version of the same trigger is returned, acquire a
4477 * reference to the trigger so the caller doesn't have to care if those
4478 * are distinct instances or not.
4480 if (ret_code
== LTTNG_OK
) {
4481 lttng_trigger_get(trigger
);
4482 *return_trigger
= trigger
;
4483 /* Ownership of trigger was transferred to caller. */
4491 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4492 const struct lttng_trigger
*trigger
)
4494 enum lttng_error_code ret_code
;
4495 const struct lttng_condition
*condition
=
4496 lttng_trigger_get_const_condition(trigger
);
4497 const enum lttng_domain_type trigger_domain
=
4498 lttng_trigger_get_underlying_domain_type_restriction(
4502 assert(lttng_condition_get_type(condition
) ==
4503 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4505 session_lock_list();
4506 switch (trigger_domain
) {
4507 case LTTNG_DOMAIN_KERNEL
:
4508 ret_code
= kernel_unregister_event_notifier(trigger
);
4509 if (ret_code
!= LTTNG_OK
) {
4510 goto end_unlock_session_list
;
4514 case LTTNG_DOMAIN_UST
:
4515 ust_app_global_update_all_event_notifier_rules();
4517 case LTTNG_DOMAIN_JUL
:
4518 case LTTNG_DOMAIN_LOG4J
:
4519 case LTTNG_DOMAIN_PYTHON
:
4521 /* Agent domains. */
4522 struct agent
*agt
= agent_find_by_event_notifier_domain(
4526 * This trigger was never registered in the first place. Calling
4527 * this function under those circumstances is an internal error.
4530 ret_code
= trigger_agent_disable(trigger
, agt
);
4531 if (ret_code
!= LTTNG_OK
) {
4532 goto end_unlock_session_list
;
4537 case LTTNG_DOMAIN_NONE
:
4542 ret_code
= LTTNG_OK
;
4544 end_unlock_session_list
:
4545 session_unlock_list();
4549 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4550 const struct lttng_trigger
*trigger
,
4551 struct notification_thread_handle
*notification_thread
)
4553 enum lttng_error_code ret_code
;
4554 const char *trigger_name
;
4555 uid_t trigger_owner
;
4556 enum lttng_trigger_status trigger_status
;
4557 struct lttng_trigger
*sessiond_trigger
= NULL
;
4559 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4560 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4561 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4562 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4564 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4565 trigger_name
, (int) trigger_owner
,
4566 (int) lttng_credentials_get_uid(cmd_creds
));
4569 * Validate the trigger credentials against the command credentials.
4570 * Only the root user can unregister a trigger with non-matching
4573 if (!lttng_credentials_is_equal_uid(
4574 lttng_trigger_get_credentials(trigger
),
4576 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4577 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4578 trigger_name
, (int) trigger_owner
,
4579 (int) lttng_credentials_get_uid(cmd_creds
));
4580 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4585 /* Fetch the sessiond side trigger object. */
4586 ret_code
= notification_thread_command_get_trigger(
4587 notification_thread
, trigger
, &sessiond_trigger
);
4588 if (ret_code
!= LTTNG_OK
) {
4589 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4590 trigger_name
, (int) trigger_owner
, ret_code
);
4594 assert(sessiond_trigger
);
4597 * From this point on, no matter what, consider the trigger
4600 * We set the unregistered state of the sessiond side trigger object in
4601 * the client thread since we want to minimize the possibility of the
4602 * notification thread being stalled due to a long execution of an
4603 * action that required the trigger lock.
4605 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4607 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4609 if (ret_code
!= LTTNG_OK
) {
4610 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4611 trigger_name
, (int) trigger_owner
, ret_code
);
4616 * Synchronize tracers if the trigger removes an event notifier.
4617 * Do this even if the trigger unregistration failed to at least stop
4618 * the tracers from producing notifications associated with this
4621 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4622 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4623 if (ret_code
!= LTTNG_OK
) {
4624 ERR("Error unregistering trigger to tracer.");
4631 lttng_trigger_put(sessiond_trigger
);
4635 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4636 struct notification_thread_handle
*notification_thread
,
4637 struct lttng_triggers
**return_triggers
)
4640 enum lttng_error_code ret_code
;
4641 struct lttng_triggers
*triggers
= NULL
;
4643 /* Get the set of triggers from the notification thread. */
4644 ret_code
= notification_thread_command_list_triggers(
4645 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4646 if (ret_code
!= LTTNG_OK
) {
4650 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4652 ret_code
= LTTNG_ERR_UNK
;
4656 *return_triggers
= triggers
;
4658 ret_code
= LTTNG_OK
;
4660 lttng_triggers_destroy(triggers
);
4664 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4665 const struct lttng_error_query
*query
,
4666 struct lttng_error_query_results
**_results
,
4667 struct notification_thread_handle
*notification_thread
)
4669 enum lttng_error_code ret_code
;
4670 const struct lttng_trigger
*query_target_trigger
;
4671 const struct lttng_action
*query_target_action
= NULL
;
4672 struct lttng_trigger
*matching_trigger
= NULL
;
4673 const char *trigger_name
;
4674 uid_t trigger_owner
;
4675 enum lttng_trigger_status trigger_status
;
4676 struct lttng_error_query_results
*results
= NULL
;
4678 switch (lttng_error_query_get_target_type(query
)) {
4679 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4680 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4682 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4683 query_target_trigger
=
4684 lttng_error_query_condition_borrow_target(query
);
4686 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4687 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4694 assert(query_target_trigger
);
4696 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4697 query_target_trigger
, &matching_trigger
);
4698 if (ret_code
!= LTTNG_OK
) {
4702 /* No longer needed. */
4703 query_target_trigger
= NULL
;
4705 if (lttng_error_query_get_target_type(query
) ==
4706 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4707 /* Get the sessiond-side version of the target action. */
4708 query_target_action
=
4709 lttng_error_query_action_borrow_action_target(
4710 query
, matching_trigger
);
4713 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4714 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4715 trigger_name
: "(anonymous)";
4716 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4718 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4720 results
= lttng_error_query_results_create();
4722 ret_code
= LTTNG_ERR_NOMEM
;
4726 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4727 trigger_name
, (int) trigger_owner
,
4728 (int) lttng_credentials_get_uid(cmd_creds
));
4731 * Validate the trigger credentials against the command credentials.
4732 * Only the root user can target a trigger with non-matching
4735 if (!lttng_credentials_is_equal_uid(
4736 lttng_trigger_get_credentials(matching_trigger
),
4738 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4739 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4740 trigger_name
, (int) trigger_owner
,
4741 (int) lttng_credentials_get_uid(cmd_creds
));
4742 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4747 switch (lttng_error_query_get_target_type(query
)) {
4748 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4749 trigger_status
= lttng_trigger_add_error_results(
4750 matching_trigger
, results
);
4752 switch (trigger_status
) {
4753 case LTTNG_TRIGGER_STATUS_OK
:
4756 ret_code
= LTTNG_ERR_UNK
;
4761 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4763 trigger_status
= lttng_trigger_condition_add_error_results(
4764 matching_trigger
, results
);
4766 switch (trigger_status
) {
4767 case LTTNG_TRIGGER_STATUS_OK
:
4770 ret_code
= LTTNG_ERR_UNK
;
4776 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4778 const enum lttng_action_status action_status
=
4779 lttng_action_add_error_query_results(
4780 query_target_action
, results
);
4782 switch (action_status
) {
4783 case LTTNG_ACTION_STATUS_OK
:
4786 ret_code
= LTTNG_ERR_UNK
;
4797 *_results
= results
;
4799 ret_code
= LTTNG_OK
;
4801 lttng_trigger_put(matching_trigger
);
4802 lttng_error_query_results_destroy(results
);
4807 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4808 * snapshot output is *not* set with a remote destination.
4810 * Return LTTNG_OK on success or a LTTNG_ERR code.
4812 static enum lttng_error_code
set_relayd_for_snapshot(
4813 struct consumer_output
*output
,
4814 const struct ltt_session
*session
)
4816 enum lttng_error_code status
= LTTNG_OK
;
4817 struct lttng_ht_iter iter
;
4818 struct consumer_socket
*socket
;
4819 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4820 const char *base_path
;
4825 DBG2("Set relayd object from snapshot output");
4827 if (session
->current_trace_chunk
) {
4828 enum lttng_trace_chunk_status chunk_status
=
4829 lttng_trace_chunk_get_id(
4830 session
->current_trace_chunk
,
4831 ¤t_chunk_id
.value
);
4833 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4834 current_chunk_id
.is_set
= true;
4836 ERR("Failed to get current trace chunk id");
4837 status
= LTTNG_ERR_UNK
;
4842 /* Ignore if snapshot consumer output is not network. */
4843 if (output
->type
!= CONSUMER_DST_NET
) {
4848 * The snapshot record URI base path overrides the session
4851 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4852 base_path
= output
->dst
.net
.control
.subdir
;
4854 base_path
= session
->base_path
;
4858 * For each consumer socket, create and send the relayd object of the
4862 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4863 socket
, node
.node
) {
4864 pthread_mutex_lock(socket
->lock
);
4865 status
= send_consumer_relayd_sockets(0, session
->id
,
4867 session
->name
, session
->hostname
,
4869 session
->live_timer
,
4870 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4871 session
->creation_time
,
4872 session
->name_contains_creation_time
);
4873 pthread_mutex_unlock(socket
->lock
);
4874 if (status
!= LTTNG_OK
) {
4886 * Record a kernel snapshot.
4888 * Return LTTNG_OK on success or a LTTNG_ERR code.
4890 static enum lttng_error_code
record_kernel_snapshot(
4891 struct ltt_kernel_session
*ksess
,
4892 const struct consumer_output
*output
,
4893 const struct ltt_session
*session
,
4894 int wait
, uint64_t nb_packets_per_stream
)
4896 enum lttng_error_code status
;
4902 status
= kernel_snapshot_record(
4903 ksess
, output
, wait
, nb_packets_per_stream
);
4908 * Record a UST snapshot.
4910 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4912 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4913 const struct consumer_output
*output
,
4914 const struct ltt_session
*session
,
4915 int wait
, uint64_t nb_packets_per_stream
)
4917 enum lttng_error_code status
;
4923 status
= ust_app_snapshot_record(
4924 usess
, output
, wait
, nb_packets_per_stream
);
4929 uint64_t get_session_size_one_more_packet_per_stream(
4930 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4932 uint64_t tot_size
= 0;
4934 if (session
->kernel_session
) {
4935 struct ltt_kernel_channel
*chan
;
4936 const struct ltt_kernel_session
*ksess
=
4937 session
->kernel_session
;
4939 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4940 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4942 * Don't take channel into account if we
4943 * already grab all its packets.
4947 tot_size
+= chan
->channel
->attr
.subbuf_size
4948 * chan
->stream_count
;
4952 if (session
->ust_session
) {
4953 const struct ltt_ust_session
*usess
= session
->ust_session
;
4955 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4963 * Calculate the number of packets we can grab from each stream that
4964 * fits within the overall snapshot max size.
4966 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4967 * the number of packets per stream.
4969 * TODO: this approach is not perfect: we consider the worse case
4970 * (packet filling the sub-buffers) as an upper bound, but we could do
4971 * better if we do this calculation while we actually grab the packet
4972 * content: we would know how much padding we don't actually store into
4975 * This algorithm is currently bounded by the number of packets per
4978 * Since we call this algorithm before actually grabbing the data, it's
4979 * an approximation: for instance, applications could appear/disappear
4980 * in between this call and actually grabbing data.
4983 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4987 uint64_t cur_nb_packets
= 0;
4990 return 0; /* Infinite */
4993 size_left
= max_size
;
4995 uint64_t one_more_packet_tot_size
;
4997 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4998 session
, cur_nb_packets
);
4999 if (!one_more_packet_tot_size
) {
5000 /* We are already grabbing all packets. */
5003 size_left
-= one_more_packet_tot_size
;
5004 if (size_left
< 0) {
5009 if (!cur_nb_packets
&& size_left
!= max_size
) {
5010 /* Not enough room to grab one packet of each stream, error. */
5013 return cur_nb_packets
;
5017 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5018 const struct snapshot_output
*snapshot_output
, int wait
)
5020 int64_t nb_packets_per_stream
;
5021 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5023 enum lttng_error_code ret_code
= LTTNG_OK
;
5024 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5025 struct consumer_output
*original_ust_consumer_output
= NULL
;
5026 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5027 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5028 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5030 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5032 snapshot_output
->name
,
5033 snapshot_output
->datetime
,
5034 snapshot_output
->nb_snapshot
);
5035 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5036 ERR("Failed to format snapshot name");
5037 ret_code
= LTTNG_ERR_INVALID
;
5040 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5041 snapshot_output
->name
, session
->name
,
5042 snapshot_chunk_name
);
5043 if (!session
->kernel_session
&& !session
->ust_session
) {
5044 ERR("Failed to record snapshot as no channels exist");
5045 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5049 if (session
->kernel_session
) {
5050 original_kernel_consumer_output
=
5051 session
->kernel_session
->consumer
;
5052 snapshot_kernel_consumer_output
=
5053 consumer_copy_output(snapshot_output
->consumer
);
5054 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5055 snapshot_chunk_name
);
5057 /* Copy the original domain subdir. */
5058 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5059 original_kernel_consumer_output
->domain_subdir
);
5061 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5062 original_kernel_consumer_output
);
5064 ERR("Failed to copy consumer sockets from snapshot output configuration");
5065 ret_code
= LTTNG_ERR_NOMEM
;
5068 ret_code
= set_relayd_for_snapshot(
5069 snapshot_kernel_consumer_output
, session
);
5070 if (ret_code
!= LTTNG_OK
) {
5071 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5074 session
->kernel_session
->consumer
=
5075 snapshot_kernel_consumer_output
;
5077 if (session
->ust_session
) {
5078 original_ust_consumer_output
= session
->ust_session
->consumer
;
5079 snapshot_ust_consumer_output
=
5080 consumer_copy_output(snapshot_output
->consumer
);
5081 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5082 snapshot_chunk_name
);
5084 /* Copy the original domain subdir. */
5085 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5086 original_ust_consumer_output
->domain_subdir
);
5088 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5089 original_ust_consumer_output
);
5091 ERR("Failed to copy consumer sockets from snapshot output configuration");
5092 ret_code
= LTTNG_ERR_NOMEM
;
5095 ret_code
= set_relayd_for_snapshot(
5096 snapshot_ust_consumer_output
, session
);
5097 if (ret_code
!= LTTNG_OK
) {
5098 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5101 session
->ust_session
->consumer
=
5102 snapshot_ust_consumer_output
;
5105 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5106 snapshot_kernel_consumer_output
?:
5107 snapshot_ust_consumer_output
,
5108 consumer_output_get_base_path(
5109 snapshot_output
->consumer
),
5110 snapshot_chunk_name
);
5111 if (!snapshot_trace_chunk
) {
5112 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5114 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5117 assert(!session
->current_trace_chunk
);
5118 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5119 lttng_trace_chunk_put(snapshot_trace_chunk
);
5120 snapshot_trace_chunk
= NULL
;
5122 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5124 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5128 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5129 snapshot_output
->max_size
);
5130 if (nb_packets_per_stream
< 0) {
5131 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5132 goto error_close_trace_chunk
;
5135 if (session
->kernel_session
) {
5136 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5137 snapshot_kernel_consumer_output
, session
,
5138 wait
, nb_packets_per_stream
);
5139 if (ret_code
!= LTTNG_OK
) {
5140 goto error_close_trace_chunk
;
5144 if (session
->ust_session
) {
5145 ret_code
= record_ust_snapshot(session
->ust_session
,
5146 snapshot_ust_consumer_output
, session
,
5147 wait
, nb_packets_per_stream
);
5148 if (ret_code
!= LTTNG_OK
) {
5149 goto error_close_trace_chunk
;
5153 error_close_trace_chunk
:
5154 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5155 ERR("Failed to release the current trace chunk of session \"%s\"",
5157 ret_code
= LTTNG_ERR_UNK
;
5160 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5161 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5163 * Don't goto end; make sure the chunk is closed for the session
5164 * to allow future snapshots.
5166 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5168 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5171 if (original_ust_consumer_output
) {
5172 session
->ust_session
->consumer
= original_ust_consumer_output
;
5174 if (original_kernel_consumer_output
) {
5175 session
->kernel_session
->consumer
=
5176 original_kernel_consumer_output
;
5178 consumer_output_put(snapshot_ust_consumer_output
);
5179 consumer_output_put(snapshot_kernel_consumer_output
);
5184 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5186 * The wait parameter is ignored so this call always wait for the snapshot to
5187 * complete before returning.
5189 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5191 int cmd_snapshot_record(struct ltt_session
*session
,
5192 const struct lttng_snapshot_output
*output
, int wait
)
5194 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5196 unsigned int snapshot_success
= 0;
5198 struct snapshot_output
*tmp_output
= NULL
;
5203 DBG("Cmd snapshot record for session %s", session
->name
);
5205 /* Get the datetime for the snapshot output directory. */
5206 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5209 cmd_ret
= LTTNG_ERR_INVALID
;
5214 * Permission denied to create an output if the session is not
5215 * set in no output mode.
5217 if (session
->output_traces
) {
5218 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5222 /* The session needs to be started at least once. */
5223 if (!session
->has_been_started
) {
5224 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5228 /* Use temporary output for the session. */
5229 if (*output
->ctrl_url
!= '\0') {
5230 tmp_output
= snapshot_output_alloc();
5232 cmd_ret
= LTTNG_ERR_NOMEM
;
5236 ret
= snapshot_output_init(session
, output
->max_size
,
5238 output
->ctrl_url
, output
->data_url
,
5242 if (ret
== -ENOMEM
) {
5243 cmd_ret
= LTTNG_ERR_NOMEM
;
5245 cmd_ret
= LTTNG_ERR_INVALID
;
5249 /* Use the global session count for the temporary snapshot. */
5250 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5252 /* Use the global datetime */
5253 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5254 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
5255 if (cmd_ret
!= LTTNG_OK
) {
5258 snapshot_success
= 1;
5260 struct snapshot_output
*sout
;
5261 struct lttng_ht_iter iter
;
5264 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5265 &iter
.iter
, sout
, node
.node
) {
5266 struct snapshot_output output_copy
;
5269 * Make a local copy of the output and override output
5270 * parameters with those provided as part of the
5273 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5275 if (output
->max_size
!= (uint64_t) -1ULL) {
5276 output_copy
.max_size
= output
->max_size
;
5279 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5280 memcpy(output_copy
.datetime
, datetime
,
5283 /* Use temporary name. */
5284 if (*output
->name
!= '\0') {
5285 if (lttng_strncpy(output_copy
.name
,
5287 sizeof(output_copy
.name
))) {
5288 cmd_ret
= LTTNG_ERR_INVALID
;
5294 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
5295 if (cmd_ret
!= LTTNG_OK
) {
5299 snapshot_success
= 1;
5304 if (snapshot_success
) {
5305 session
->snapshot
.nb_snapshot
++;
5307 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5312 snapshot_output_destroy(tmp_output
);
5318 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5320 int cmd_set_session_shm_path(struct ltt_session
*session
,
5321 const char *shm_path
)
5327 * Can only set shm path before session is started.
5329 if (session
->has_been_started
) {
5330 return LTTNG_ERR_SESSION_STARTED
;
5333 strncpy(session
->shm_path
, shm_path
,
5334 sizeof(session
->shm_path
));
5335 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5341 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5343 * Ask the consumer to rotate the session output directory.
5344 * The session lock must be held.
5346 * Returns LTTNG_OK on success or else a negative LTTng error code.
5348 int cmd_rotate_session(struct ltt_session
*session
,
5349 struct lttng_rotate_session_return
*rotate_return
,
5350 bool quiet_rotation
,
5351 enum lttng_trace_chunk_command_type command
)
5354 uint64_t ongoing_rotation_chunk_id
;
5355 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5356 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5357 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5358 enum lttng_trace_chunk_status chunk_status
;
5359 bool failed_to_rotate
= false;
5360 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5364 if (!session
->has_been_started
) {
5365 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5370 * Explicit rotation is not supported for live sessions.
5371 * However, live sessions can perform a quiet rotation on
5373 * Rotation is not supported for snapshot traces (no output).
5375 if ((!quiet_rotation
&& session
->live_timer
) ||
5376 !session
->output_traces
) {
5377 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5381 /* Unsupported feature in lttng-relayd before 2.11. */
5382 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5383 (session
->consumer
->relay_major_version
== 2 &&
5384 session
->consumer
->relay_minor_version
< 11)) {
5385 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5389 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5390 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5391 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5395 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5396 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5398 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5403 * After a stop, we only allow one rotation to occur, the other ones are
5404 * useless until a new start.
5406 if (session
->rotated_after_last_stop
) {
5407 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5409 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5414 * After a stop followed by a clear, disallow following rotations a they would
5415 * generate empty chunks.
5417 if (session
->cleared_after_last_stop
) {
5418 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5420 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5424 if (session
->active
) {
5425 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5427 if (!new_trace_chunk
) {
5428 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5434 * The current trace chunk becomes the chunk being archived.
5436 * After this point, "chunk_being_archived" must absolutely
5437 * be closed on the consumer(s), otherwise it will never be
5438 * cleaned-up, which will result in a leak.
5440 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5441 &chunk_being_archived
);
5443 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5447 if (session
->kernel_session
) {
5448 cmd_ret
= kernel_rotate_session(session
);
5449 if (cmd_ret
!= LTTNG_OK
) {
5450 failed_to_rotate
= true;
5451 rotation_fail_code
= cmd_ret
;
5454 if (session
->ust_session
) {
5455 cmd_ret
= ust_app_rotate_session(session
);
5456 if (cmd_ret
!= LTTNG_OK
) {
5457 failed_to_rotate
= true;
5458 rotation_fail_code
= cmd_ret
;
5462 if (!session
->active
) {
5463 session
->rotated_after_last_stop
= true;
5466 if (!chunk_being_archived
) {
5467 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5469 if (failed_to_rotate
) {
5470 cmd_ret
= rotation_fail_code
;
5477 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5478 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5479 &ongoing_rotation_chunk_id
);
5480 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5482 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5483 command
, session
->last_chunk_path
);
5485 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5489 if (failed_to_rotate
) {
5490 cmd_ret
= rotation_fail_code
;
5494 session
->quiet_rotation
= quiet_rotation
;
5495 ret
= timer_session_rotation_pending_check_start(session
,
5496 DEFAULT_ROTATE_PENDING_TIMER
);
5498 cmd_ret
= LTTNG_ERR_UNK
;
5502 if (rotate_return
) {
5503 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5506 session
->chunk_being_archived
= chunk_being_archived
;
5507 chunk_being_archived
= NULL
;
5508 if (!quiet_rotation
) {
5509 ret
= notification_thread_command_session_rotation_ongoing(
5510 the_notification_thread_handle
, session
->name
,
5511 session
->uid
, session
->gid
,
5512 ongoing_rotation_chunk_id
);
5513 if (ret
!= LTTNG_OK
) {
5514 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5520 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5521 session
->name
, ongoing_rotation_chunk_id
);
5523 lttng_trace_chunk_put(new_trace_chunk
);
5524 lttng_trace_chunk_put(chunk_being_archived
);
5525 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5528 if (session_reset_rotation_state(session
,
5529 LTTNG_ROTATION_STATE_ERROR
)) {
5530 ERR("Failed to reset rotation state of session \"%s\"",
5537 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5539 * Check if the session has finished its rotation.
5541 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5543 int cmd_rotate_get_info(struct ltt_session
*session
,
5544 struct lttng_rotation_get_info_return
*info_return
,
5545 uint64_t rotation_id
)
5547 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5548 enum lttng_rotation_state rotation_state
;
5550 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5551 session
->most_recent_chunk_id
.value
);
5553 if (session
->chunk_being_archived
) {
5554 enum lttng_trace_chunk_status chunk_status
;
5557 chunk_status
= lttng_trace_chunk_get_id(
5558 session
->chunk_being_archived
,
5560 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5562 rotation_state
= rotation_id
== chunk_id
?
5563 LTTNG_ROTATION_STATE_ONGOING
:
5564 LTTNG_ROTATION_STATE_EXPIRED
;
5566 if (session
->last_archived_chunk_id
.is_set
&&
5567 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5568 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5570 rotation_state
= session
->rotation_state
;
5574 switch (rotation_state
) {
5575 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5576 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5579 case LTTNG_ROTATION_STATE_EXPIRED
:
5580 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5581 rotation_id
, session
->name
);
5583 case LTTNG_ROTATION_STATE_ONGOING
:
5584 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5585 rotation_id
, session
->name
);
5587 case LTTNG_ROTATION_STATE_COMPLETED
:
5591 char *current_tracing_path_reply
;
5592 size_t current_tracing_path_reply_len
;
5594 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5595 rotation_id
, session
->name
);
5597 switch (session_get_consumer_destination_type(session
)) {
5598 case CONSUMER_DST_LOCAL
:
5599 current_tracing_path_reply
=
5600 info_return
->location
.local
.absolute_path
;
5601 current_tracing_path_reply_len
=
5602 sizeof(info_return
->location
.local
.absolute_path
);
5603 info_return
->location_type
=
5604 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5605 fmt_ret
= asprintf(&chunk_path
,
5606 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5607 session_get_base_path(session
),
5608 session
->last_archived_chunk_name
);
5609 if (fmt_ret
== -1) {
5610 PERROR("Failed to format the path of the last archived trace chunk");
5611 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5612 cmd_ret
= LTTNG_ERR_UNK
;
5616 case CONSUMER_DST_NET
:
5618 uint16_t ctrl_port
, data_port
;
5620 current_tracing_path_reply
=
5621 info_return
->location
.relay
.relative_path
;
5622 current_tracing_path_reply_len
=
5623 sizeof(info_return
->location
.relay
.relative_path
);
5624 /* Currently the only supported relay protocol. */
5625 info_return
->location
.relay
.protocol
=
5626 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5628 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5629 session_get_net_consumer_hostname(session
),
5630 sizeof(info_return
->location
.relay
.host
));
5632 ERR("Failed to copy host name to rotate_get_info reply");
5633 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5634 cmd_ret
= LTTNG_ERR_SET_URL
;
5638 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5639 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5640 info_return
->location
.relay
.ports
.data
= data_port
;
5641 info_return
->location_type
=
5642 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5643 chunk_path
= strdup(session
->last_chunk_path
);
5645 ERR("Failed to allocate the path of the last archived trace chunk");
5646 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5647 cmd_ret
= LTTNG_ERR_UNK
;
5656 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5657 chunk_path
, current_tracing_path_reply_len
);
5660 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5661 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5662 cmd_ret
= LTTNG_ERR_UNK
;
5668 case LTTNG_ROTATION_STATE_ERROR
:
5669 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5670 rotation_id
, session
->name
);
5678 info_return
->status
= (int32_t) rotation_state
;
5683 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5685 * Configure the automatic rotation parameters.
5686 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5687 * 'activate' to false means deactivate the rotation schedule and validate that
5688 * 'new_value' has the same value as the currently active value.
5690 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5692 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5693 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5695 struct notification_thread_handle
*notification_thread_handle
)
5698 uint64_t *parameter_value
;
5702 DBG("Cmd rotate set schedule session %s", session
->name
);
5704 if (session
->live_timer
|| !session
->output_traces
) {
5705 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5706 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5710 switch (schedule_type
) {
5711 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5712 parameter_value
= &session
->rotate_size
;
5714 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5715 parameter_value
= &session
->rotate_timer_period
;
5716 if (new_value
>= UINT_MAX
) {
5717 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5718 new_value
, UINT_MAX
);
5719 ret
= LTTNG_ERR_INVALID
;
5724 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5725 ret
= LTTNG_ERR_INVALID
;
5729 /* Improper use of the API. */
5730 if (new_value
== -1ULL) {
5731 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5732 ret
= LTTNG_ERR_INVALID
;
5737 * As indicated in struct ltt_session's comments, a value of == 0 means
5738 * this schedule rotation type is not in use.
5740 * Reject the command if we were asked to activate a schedule that was
5743 if (activate
&& *parameter_value
!= 0) {
5744 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5745 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5750 * Reject the command if we were asked to deactivate a schedule that was
5753 if (!activate
&& *parameter_value
== 0) {
5754 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5755 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5760 * Reject the command if we were asked to deactivate a schedule that
5763 if (!activate
&& *parameter_value
!= new_value
) {
5764 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5765 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5769 *parameter_value
= activate
? new_value
: 0;
5771 switch (schedule_type
) {
5772 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5773 if (activate
&& session
->active
) {
5775 * Only start the timer if the session is active,
5776 * otherwise it will be started when the session starts.
5778 ret
= timer_session_rotation_schedule_timer_start(
5779 session
, new_value
);
5781 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5782 ret
= LTTNG_ERR_UNK
;
5786 ret
= timer_session_rotation_schedule_timer_stop(
5789 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5790 ret
= LTTNG_ERR_UNK
;
5795 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5797 ret
= subscribe_session_consumed_size_rotation(session
,
5798 new_value
, notification_thread_handle
);
5800 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5801 ret
= LTTNG_ERR_UNK
;
5805 ret
= unsubscribe_session_consumed_size_rotation(session
,
5806 notification_thread_handle
);
5808 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5809 ret
= LTTNG_ERR_UNK
;
5816 /* Would have been caught before. */
5828 /* Wait for a given path to be removed before continuing. */
5829 static enum lttng_error_code
wait_on_path(void *path_data
)
5831 const char *shm_path
= path_data
;
5833 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5839 ret
= stat(shm_path
, &st
);
5841 if (errno
!= ENOENT
) {
5842 PERROR("stat() returned an error while checking for the existence of the shm path");
5844 DBG("shm path no longer exists, completing the destruction of session");
5848 if (!S_ISDIR(st
.st_mode
)) {
5849 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5854 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5860 * Returns a pointer to a handler to run on completion of a command.
5861 * Returns NULL if no handler has to be run for the last command executed.
5863 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5865 struct cmd_completion_handler
*handler
= current_completion_handler
;
5867 current_completion_handler
= NULL
;
5872 * Init command subsystem.
5877 * Set network sequence index to 1 for streams to match a relayd
5878 * socket on the consumer side.
5880 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5881 relayd_net_seq_idx
= 1;
5882 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5884 DBG("Command subsystem initialized");