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
];
136 LTTNG_ASSERT(session
);
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 LTTNG_ASSERT(discarded_events
);
256 LTTNG_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
{};
495 event
.loglevel_type
= agent_event
->loglevel_type
;
496 event
.loglevel
= agent_event
->loglevel_value
;
497 event
.enabled
= AGENT_EVENT_IS_ENABLED(agent_event
);
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
);
811 ret_code
= (lttng_error_code
) -ret
;
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
;
856 LTTNG_ASSERT(session
);
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
;
1062 LTTNG_ASSERT(consumer
);
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
= {};
1107 LTTNG_ASSERT(session
);
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
;
1372 LTTNG_ASSERT(session
);
1373 LTTNG_ASSERT(_attr
);
1374 LTTNG_ASSERT(domain
);
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 * If the session is a live session, remove the switch timer, the
1392 * live timer does the same thing but sends also synchronisation
1393 * beacons for inactive streams.
1395 if (session
->live_timer
> 0) {
1396 attr
.attr
.live_timer_interval
= session
->live_timer
;
1397 attr
.attr
.switch_timer_interval
= 0;
1400 /* Check for feature support */
1401 switch (domain
->type
) {
1402 case LTTNG_DOMAIN_KERNEL
:
1404 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1405 /* Sampling position of buffer is not supported */
1406 WARN("Kernel tracer does not support buffer monitoring. "
1407 "Setting the monitor interval timer to 0 "
1408 "(disabled) for channel '%s' of session '%s'",
1409 attr
.name
, session
->name
);
1410 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1414 case LTTNG_DOMAIN_UST
:
1416 case LTTNG_DOMAIN_JUL
:
1417 case LTTNG_DOMAIN_LOG4J
:
1418 case LTTNG_DOMAIN_PYTHON
:
1419 if (!agent_tracing_is_enabled()) {
1420 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1421 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1426 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1430 switch (domain
->type
) {
1431 case LTTNG_DOMAIN_KERNEL
:
1433 struct ltt_kernel_channel
*kchan
;
1435 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1436 session
->kernel_session
);
1437 if (kchan
== NULL
) {
1439 * Don't try to create a channel if the session has been started at
1440 * some point in time before. The tracer does not allow it.
1442 if (session
->has_been_started
) {
1443 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
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
) {
1507 * Don't try to create a channel if the session has been started at
1508 * some point in time before. The tracer does not allow it.
1510 if (session
->has_been_started
) {
1511 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1515 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1516 if (attr
.name
[0] != '\0') {
1517 usess
->has_non_default_channel
= 1;
1520 ret
= channel_ust_enable(usess
, uchan
);
1525 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1529 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1530 session
->has_non_mmap_channel
= true;
1538 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1539 struct ltt_session
*session
,
1540 enum lttng_domain_type domain
,
1541 enum lttng_process_attr process_attr
,
1542 enum lttng_tracking_policy
*policy
)
1544 enum lttng_error_code ret_code
= LTTNG_OK
;
1545 const struct process_attr_tracker
*tracker
;
1548 case LTTNG_DOMAIN_KERNEL
:
1549 if (!session
->kernel_session
) {
1550 ret_code
= LTTNG_ERR_INVALID
;
1553 tracker
= kernel_get_process_attr_tracker(
1554 session
->kernel_session
, process_attr
);
1556 case LTTNG_DOMAIN_UST
:
1557 if (!session
->ust_session
) {
1558 ret_code
= LTTNG_ERR_INVALID
;
1561 tracker
= trace_ust_get_process_attr_tracker(
1562 session
->ust_session
, process_attr
);
1565 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1569 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1571 ret_code
= LTTNG_ERR_INVALID
;
1577 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1578 struct ltt_session
*session
,
1579 enum lttng_domain_type domain
,
1580 enum lttng_process_attr process_attr
,
1581 enum lttng_tracking_policy policy
)
1583 enum lttng_error_code ret_code
= LTTNG_OK
;
1586 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1587 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1588 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1591 ret_code
= LTTNG_ERR_INVALID
;
1596 case LTTNG_DOMAIN_KERNEL
:
1597 if (!session
->kernel_session
) {
1598 ret_code
= LTTNG_ERR_INVALID
;
1601 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1602 session
->kernel_session
, process_attr
, policy
);
1604 case LTTNG_DOMAIN_UST
:
1605 if (!session
->ust_session
) {
1606 ret_code
= LTTNG_ERR_INVALID
;
1609 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1610 session
->ust_session
, process_attr
, policy
);
1613 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1620 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1621 struct ltt_session
*session
,
1622 enum lttng_domain_type domain
,
1623 enum lttng_process_attr process_attr
,
1624 const struct process_attr_value
*value
)
1626 enum lttng_error_code ret_code
= LTTNG_OK
;
1629 case LTTNG_DOMAIN_KERNEL
:
1630 if (!session
->kernel_session
) {
1631 ret_code
= LTTNG_ERR_INVALID
;
1634 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1635 session
->kernel_session
, process_attr
, value
);
1637 case LTTNG_DOMAIN_UST
:
1638 if (!session
->ust_session
) {
1639 ret_code
= LTTNG_ERR_INVALID
;
1642 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1643 session
->ust_session
, process_attr
, value
);
1646 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1653 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1654 struct ltt_session
*session
,
1655 enum lttng_domain_type domain
,
1656 enum lttng_process_attr process_attr
,
1657 const struct process_attr_value
*value
)
1659 enum lttng_error_code ret_code
= LTTNG_OK
;
1662 case LTTNG_DOMAIN_KERNEL
:
1663 if (!session
->kernel_session
) {
1664 ret_code
= LTTNG_ERR_INVALID
;
1667 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1668 session
->kernel_session
, process_attr
, value
);
1670 case LTTNG_DOMAIN_UST
:
1671 if (!session
->ust_session
) {
1672 ret_code
= LTTNG_ERR_INVALID
;
1675 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1676 session
->ust_session
, process_attr
, value
);
1679 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1686 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1687 struct ltt_session
*session
,
1688 enum lttng_domain_type domain
,
1689 enum lttng_process_attr process_attr
,
1690 struct lttng_process_attr_values
**values
)
1692 enum lttng_error_code ret_code
= LTTNG_OK
;
1693 const struct process_attr_tracker
*tracker
;
1694 enum process_attr_tracker_status status
;
1697 case LTTNG_DOMAIN_KERNEL
:
1698 if (!session
->kernel_session
) {
1699 ret_code
= LTTNG_ERR_INVALID
;
1702 tracker
= kernel_get_process_attr_tracker(
1703 session
->kernel_session
, process_attr
);
1705 case LTTNG_DOMAIN_UST
:
1706 if (!session
->ust_session
) {
1707 ret_code
= LTTNG_ERR_INVALID
;
1710 tracker
= trace_ust_get_process_attr_tracker(
1711 session
->ust_session
, process_attr
);
1714 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1719 ret_code
= LTTNG_ERR_INVALID
;
1723 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1725 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1726 ret_code
= LTTNG_OK
;
1728 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1729 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1731 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1732 ret_code
= LTTNG_ERR_NOMEM
;
1735 ret_code
= LTTNG_ERR_UNK
;
1744 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1746 int cmd_disable_event(struct ltt_session
*session
,
1747 enum lttng_domain_type domain
, const char *channel_name
,
1748 const struct lttng_event
*event
)
1751 const char *event_name
;
1753 DBG("Disable event command for event \'%s\'", event
->name
);
1755 event_name
= event
->name
;
1757 /* Error out on unhandled search criteria */
1758 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1759 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1760 ret
= LTTNG_ERR_UNK
;
1767 case LTTNG_DOMAIN_KERNEL
:
1769 struct ltt_kernel_channel
*kchan
;
1770 struct ltt_kernel_session
*ksess
;
1772 ksess
= session
->kernel_session
;
1775 * If a non-default channel has been created in the
1776 * session, explicitely require that -c chan_name needs
1779 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1780 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1784 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1785 if (kchan
== NULL
) {
1786 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1790 switch (event
->type
) {
1791 case LTTNG_EVENT_ALL
:
1792 case LTTNG_EVENT_TRACEPOINT
:
1793 case LTTNG_EVENT_SYSCALL
:
1794 case LTTNG_EVENT_PROBE
:
1795 case LTTNG_EVENT_FUNCTION
:
1796 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1797 if (event_name
[0] == '\0') {
1798 ret
= event_kernel_disable_event(kchan
,
1801 ret
= event_kernel_disable_event(kchan
,
1802 event_name
, event
->type
);
1804 if (ret
!= LTTNG_OK
) {
1809 ret
= LTTNG_ERR_UNK
;
1813 kernel_wait_quiescent();
1816 case LTTNG_DOMAIN_UST
:
1818 struct ltt_ust_channel
*uchan
;
1819 struct ltt_ust_session
*usess
;
1821 usess
= session
->ust_session
;
1823 if (validate_ust_event_name(event_name
)) {
1824 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1829 * If a non-default channel has been created in the
1830 * session, explicitly require that -c chan_name needs
1833 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1834 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1838 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1840 if (uchan
== NULL
) {
1841 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1845 switch (event
->type
) {
1846 case LTTNG_EVENT_ALL
:
1848 * An empty event name means that everything
1849 * should be disabled.
1851 if (event
->name
[0] == '\0') {
1852 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1854 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1857 if (ret
!= LTTNG_OK
) {
1862 ret
= LTTNG_ERR_UNK
;
1866 DBG3("Disable UST event %s in channel %s completed", event_name
,
1870 case LTTNG_DOMAIN_LOG4J
:
1871 case LTTNG_DOMAIN_JUL
:
1872 case LTTNG_DOMAIN_PYTHON
:
1875 struct ltt_ust_session
*usess
= session
->ust_session
;
1877 LTTNG_ASSERT(usess
);
1879 switch (event
->type
) {
1880 case LTTNG_EVENT_ALL
:
1883 ret
= LTTNG_ERR_UNK
;
1887 agt
= trace_ust_find_agent(usess
, domain
);
1889 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1893 * An empty event name means that everything
1894 * should be disabled.
1896 if (event
->name
[0] == '\0') {
1897 ret
= event_agent_disable_all(usess
, agt
);
1899 ret
= event_agent_disable(usess
, agt
, event_name
);
1901 if (ret
!= LTTNG_OK
) {
1908 ret
= LTTNG_ERR_UND
;
1921 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1923 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1924 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1926 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1927 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1930 * Don't try to add a context if the session has been started at
1931 * some point in time before. The tracer does not allow it and would
1932 * result in a corrupted trace.
1934 if (session
->has_been_started
) {
1935 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1939 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1940 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1941 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1945 case LTTNG_DOMAIN_KERNEL
:
1946 LTTNG_ASSERT(session
->kernel_session
);
1948 if (session
->kernel_session
->channel_count
== 0) {
1949 /* Create default channel */
1950 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1951 if (ret
!= LTTNG_OK
) {
1954 chan_kern_created
= 1;
1956 /* Add kernel context to kernel tracer */
1957 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1958 if (ret
!= LTTNG_OK
) {
1962 case LTTNG_DOMAIN_JUL
:
1963 case LTTNG_DOMAIN_LOG4J
:
1966 * Validate channel name.
1967 * If no channel name is given and the domain is JUL or LOG4J,
1968 * set it to the appropriate domain-specific channel name. If
1969 * a name is provided but does not match the expexted channel
1970 * name, return an error.
1972 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1973 strcmp(channel_name
,
1974 DEFAULT_JUL_CHANNEL_NAME
)) {
1975 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1977 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1978 strcmp(channel_name
,
1979 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1980 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1983 /* break is _not_ missing here. */
1985 case LTTNG_DOMAIN_UST
:
1987 struct ltt_ust_session
*usess
= session
->ust_session
;
1988 unsigned int chan_count
;
1990 LTTNG_ASSERT(usess
);
1992 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1993 if (chan_count
== 0) {
1994 struct lttng_channel
*attr
;
1995 /* Create default channel */
1996 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1998 ret
= LTTNG_ERR_FATAL
;
2002 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
2003 if (ret
!= LTTNG_OK
) {
2007 channel_attr_destroy(attr
);
2008 chan_ust_created
= 1;
2011 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2012 free(app_ctx_provider_name
);
2014 app_ctx_name
= NULL
;
2015 app_ctx_provider_name
= NULL
;
2016 if (ret
!= LTTNG_OK
) {
2022 ret
= LTTNG_ERR_UND
;
2030 if (chan_kern_created
) {
2031 struct ltt_kernel_channel
*kchan
=
2032 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2033 session
->kernel_session
);
2034 /* Created previously, this should NOT fail. */
2035 LTTNG_ASSERT(kchan
);
2036 kernel_destroy_channel(kchan
);
2039 if (chan_ust_created
) {
2040 struct ltt_ust_channel
*uchan
=
2041 trace_ust_find_channel_by_name(
2042 session
->ust_session
->domain_global
.channels
,
2043 DEFAULT_CHANNEL_NAME
);
2044 /* Created previously, this should NOT fail. */
2045 LTTNG_ASSERT(uchan
);
2046 /* Remove from the channel list of the session. */
2047 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2049 trace_ust_destroy_channel(uchan
);
2052 free(app_ctx_provider_name
);
2057 static inline bool name_starts_with(const char *name
, const char *prefix
)
2059 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2061 return !strncmp(name
, prefix
, max_cmp_len
);
2064 /* Perform userspace-specific event name validation */
2065 static int validate_ust_event_name(const char *name
)
2075 * Check name against all internal UST event component namespaces used
2078 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2079 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2080 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2089 * Internal version of cmd_enable_event() with a supplemental
2090 * "internal_event" flag which is used to enable internal events which should
2091 * be hidden from clients. Such events are used in the agent implementation to
2092 * enable the events through which all "agent" events are funeled.
2094 static int _cmd_enable_event(struct ltt_session
*session
,
2095 const struct lttng_domain
*domain
,
2096 char *channel_name
, struct lttng_event
*event
,
2097 char *filter_expression
,
2098 struct lttng_bytecode
*filter
,
2099 struct lttng_event_exclusion
*exclusion
,
2100 int wpipe
, bool internal_event
)
2102 int ret
= 0, channel_created
= 0;
2103 struct lttng_channel
*attr
= NULL
;
2105 LTTNG_ASSERT(session
);
2106 LTTNG_ASSERT(event
);
2107 LTTNG_ASSERT(channel_name
);
2109 /* If we have a filter, we must have its filter expression */
2110 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2112 /* Normalize event name as a globbing pattern */
2113 strutils_normalize_star_glob_pattern(event
->name
);
2115 /* Normalize exclusion names as globbing patterns */
2119 for (i
= 0; i
< exclusion
->count
; i
++) {
2120 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2122 strutils_normalize_star_glob_pattern(name
);
2126 DBG("Enable event command for event \'%s\'", event
->name
);
2130 switch (domain
->type
) {
2131 case LTTNG_DOMAIN_KERNEL
:
2133 struct ltt_kernel_channel
*kchan
;
2136 * If a non-default channel has been created in the
2137 * session, explicitely require that -c chan_name needs
2140 if (session
->kernel_session
->has_non_default_channel
2141 && channel_name
[0] == '\0') {
2142 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2146 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2147 session
->kernel_session
);
2148 if (kchan
== NULL
) {
2149 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2150 LTTNG_BUFFER_GLOBAL
);
2152 ret
= LTTNG_ERR_FATAL
;
2155 if (lttng_strncpy(attr
->name
, channel_name
,
2156 sizeof(attr
->name
))) {
2157 ret
= LTTNG_ERR_INVALID
;
2161 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2162 if (ret
!= LTTNG_OK
) {
2165 channel_created
= 1;
2168 /* Get the newly created kernel channel pointer */
2169 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2170 session
->kernel_session
);
2171 if (kchan
== NULL
) {
2172 /* This sould not happen... */
2173 ret
= LTTNG_ERR_FATAL
;
2177 switch (event
->type
) {
2178 case LTTNG_EVENT_ALL
:
2180 char *filter_expression_a
= NULL
;
2181 struct lttng_bytecode
*filter_a
= NULL
;
2184 * We need to duplicate filter_expression and filter,
2185 * because ownership is passed to first enable
2188 if (filter_expression
) {
2189 filter_expression_a
= strdup(filter_expression
);
2190 if (!filter_expression_a
) {
2191 ret
= LTTNG_ERR_FATAL
;
2196 filter_a
= (lttng_bytecode
*) zmalloc(sizeof(*filter_a
) + filter
->len
);
2198 free(filter_expression_a
);
2199 ret
= LTTNG_ERR_FATAL
;
2202 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2204 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2205 ret
= event_kernel_enable_event(kchan
, event
,
2206 filter_expression
, filter
);
2207 /* We have passed ownership */
2208 filter_expression
= NULL
;
2210 if (ret
!= LTTNG_OK
) {
2211 if (channel_created
) {
2212 /* Let's not leak a useless channel. */
2213 kernel_destroy_channel(kchan
);
2215 free(filter_expression_a
);
2219 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2220 ret
= event_kernel_enable_event(kchan
, event
,
2221 filter_expression_a
, filter_a
);
2222 /* We have passed ownership */
2223 filter_expression_a
= NULL
;
2225 if (ret
!= LTTNG_OK
) {
2230 case LTTNG_EVENT_PROBE
:
2231 case LTTNG_EVENT_USERSPACE_PROBE
:
2232 case LTTNG_EVENT_FUNCTION
:
2233 case LTTNG_EVENT_FUNCTION_ENTRY
:
2234 case LTTNG_EVENT_TRACEPOINT
:
2235 ret
= event_kernel_enable_event(kchan
, event
,
2236 filter_expression
, filter
);
2237 /* We have passed ownership */
2238 filter_expression
= NULL
;
2240 if (ret
!= LTTNG_OK
) {
2241 if (channel_created
) {
2242 /* Let's not leak a useless channel. */
2243 kernel_destroy_channel(kchan
);
2248 case LTTNG_EVENT_SYSCALL
:
2249 ret
= event_kernel_enable_event(kchan
, event
,
2250 filter_expression
, filter
);
2251 /* We have passed ownership */
2252 filter_expression
= NULL
;
2254 if (ret
!= LTTNG_OK
) {
2259 ret
= LTTNG_ERR_UNK
;
2263 kernel_wait_quiescent();
2266 case LTTNG_DOMAIN_UST
:
2268 struct ltt_ust_channel
*uchan
;
2269 struct ltt_ust_session
*usess
= session
->ust_session
;
2271 LTTNG_ASSERT(usess
);
2274 * If a non-default channel has been created in the
2275 * session, explicitely require that -c chan_name needs
2278 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2279 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2283 /* Get channel from global UST domain */
2284 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2286 if (uchan
== NULL
) {
2287 /* Create default channel */
2288 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2289 usess
->buffer_type
);
2291 ret
= LTTNG_ERR_FATAL
;
2294 if (lttng_strncpy(attr
->name
, channel_name
,
2295 sizeof(attr
->name
))) {
2296 ret
= LTTNG_ERR_INVALID
;
2300 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2301 if (ret
!= LTTNG_OK
) {
2305 /* Get the newly created channel reference back */
2306 uchan
= trace_ust_find_channel_by_name(
2307 usess
->domain_global
.channels
, channel_name
);
2308 LTTNG_ASSERT(uchan
);
2311 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2313 * Don't allow users to add UST events to channels which
2314 * are assigned to a userspace subdomain (JUL, Log4J,
2317 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2321 if (!internal_event
) {
2323 * Ensure the event name is not reserved for internal
2326 ret
= validate_ust_event_name(event
->name
);
2328 WARN("Userspace event name %s failed validation.",
2330 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2335 /* At this point, the session and channel exist on the tracer */
2336 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2337 filter_expression
, filter
, exclusion
,
2339 /* We have passed ownership */
2340 filter_expression
= NULL
;
2343 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2344 goto already_enabled
;
2345 } else if (ret
!= LTTNG_OK
) {
2350 case LTTNG_DOMAIN_LOG4J
:
2351 case LTTNG_DOMAIN_JUL
:
2352 case LTTNG_DOMAIN_PYTHON
:
2354 const char *default_event_name
, *default_chan_name
;
2356 struct lttng_event uevent
;
2357 struct lttng_domain tmp_dom
;
2358 struct ltt_ust_session
*usess
= session
->ust_session
;
2360 LTTNG_ASSERT(usess
);
2362 if (!agent_tracing_is_enabled()) {
2363 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2364 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2368 agt
= trace_ust_find_agent(usess
, domain
->type
);
2370 agt
= agent_create(domain
->type
);
2372 ret
= LTTNG_ERR_NOMEM
;
2375 agent_add(agt
, usess
->agents
);
2378 /* Create the default tracepoint. */
2379 memset(&uevent
, 0, sizeof(uevent
));
2380 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2381 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2382 default_event_name
= event_get_default_agent_ust_name(
2384 if (!default_event_name
) {
2385 ret
= LTTNG_ERR_FATAL
;
2388 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2389 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2392 * The domain type is changed because we are about to enable the
2393 * default channel and event for the JUL domain that are hardcoded.
2394 * This happens in the UST domain.
2396 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2397 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2399 switch (domain
->type
) {
2400 case LTTNG_DOMAIN_LOG4J
:
2401 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2403 case LTTNG_DOMAIN_JUL
:
2404 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2406 case LTTNG_DOMAIN_PYTHON
:
2407 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2410 /* The switch/case we are in makes this impossible */
2415 char *filter_expression_copy
= NULL
;
2416 struct lttng_bytecode
*filter_copy
= NULL
;
2419 const size_t filter_size
= sizeof(
2420 struct lttng_bytecode
)
2423 filter_copy
= (lttng_bytecode
*) zmalloc(filter_size
);
2425 ret
= LTTNG_ERR_NOMEM
;
2428 memcpy(filter_copy
, filter
, filter_size
);
2430 filter_expression_copy
=
2431 strdup(filter_expression
);
2432 if (!filter_expression
) {
2433 ret
= LTTNG_ERR_NOMEM
;
2436 if (!filter_expression_copy
|| !filter_copy
) {
2437 free(filter_expression_copy
);
2443 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2444 (char *) default_chan_name
,
2445 &uevent
, filter_expression_copy
,
2446 filter_copy
, NULL
, wpipe
);
2449 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2450 goto already_enabled
;
2451 } else if (ret
!= LTTNG_OK
) {
2455 /* The wild card * means that everything should be enabled. */
2456 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2457 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2460 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2464 filter_expression
= NULL
;
2465 if (ret
!= LTTNG_OK
) {
2472 ret
= LTTNG_ERR_UND
;
2480 free(filter_expression
);
2483 channel_attr_destroy(attr
);
2489 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2490 * We own filter, exclusion, and filter_expression.
2492 int cmd_enable_event(struct ltt_session
*session
,
2493 const struct lttng_domain
*domain
,
2494 char *channel_name
, struct lttng_event
*event
,
2495 char *filter_expression
,
2496 struct lttng_bytecode
*filter
,
2497 struct lttng_event_exclusion
*exclusion
,
2500 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2501 filter_expression
, filter
, exclusion
, wpipe
, false);
2505 * Enable an event which is internal to LTTng. An internal should
2506 * never be made visible to clients and are immune to checks such as
2509 static int cmd_enable_event_internal(struct ltt_session
*session
,
2510 const struct lttng_domain
*domain
,
2511 char *channel_name
, struct lttng_event
*event
,
2512 char *filter_expression
,
2513 struct lttng_bytecode
*filter
,
2514 struct lttng_event_exclusion
*exclusion
,
2517 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2518 filter_expression
, filter
, exclusion
, wpipe
, true);
2522 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2524 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2525 struct lttng_event
**events
)
2528 ssize_t nb_events
= 0;
2531 case LTTNG_DOMAIN_KERNEL
:
2532 nb_events
= kernel_list_events(events
);
2533 if (nb_events
< 0) {
2534 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2538 case LTTNG_DOMAIN_UST
:
2539 nb_events
= ust_app_list_events(events
);
2540 if (nb_events
< 0) {
2541 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2545 case LTTNG_DOMAIN_LOG4J
:
2546 case LTTNG_DOMAIN_JUL
:
2547 case LTTNG_DOMAIN_PYTHON
:
2548 nb_events
= agent_list_events(events
, domain
);
2549 if (nb_events
< 0) {
2550 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2555 ret
= LTTNG_ERR_UND
;
2562 /* Return negative value to differentiate return code */
2567 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2569 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2570 struct lttng_event_field
**fields
)
2573 ssize_t nb_fields
= 0;
2576 case LTTNG_DOMAIN_UST
:
2577 nb_fields
= ust_app_list_event_fields(fields
);
2578 if (nb_fields
< 0) {
2579 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2583 case LTTNG_DOMAIN_KERNEL
:
2584 default: /* fall-through */
2585 ret
= LTTNG_ERR_UND
;
2592 /* Return negative value to differentiate return code */
2596 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2598 return syscall_table_list(events
);
2602 * Command LTTNG_START_TRACE processed by the client thread.
2604 * Called with session mutex held.
2606 int cmd_start_trace(struct ltt_session
*session
)
2608 enum lttng_error_code ret
;
2609 unsigned long nb_chan
= 0;
2610 struct ltt_kernel_session
*ksession
;
2611 struct ltt_ust_session
*usess
;
2612 const bool session_rotated_after_last_stop
=
2613 session
->rotated_after_last_stop
;
2614 const bool session_cleared_after_last_stop
=
2615 session
->cleared_after_last_stop
;
2617 LTTNG_ASSERT(session
);
2619 /* Ease our life a bit ;) */
2620 ksession
= session
->kernel_session
;
2621 usess
= session
->ust_session
;
2623 /* Is the session already started? */
2624 if (session
->active
) {
2625 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2626 /* Perform nothing */
2630 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2631 !session
->current_trace_chunk
) {
2633 * A rotation was launched while the session was stopped and
2634 * it has not been completed yet. It is not possible to start
2635 * the session since starting the session here would require a
2636 * rotation from "NULL" to a new trace chunk. That rotation
2637 * would overlap with the ongoing rotation, which is not
2640 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2642 ret
= LTTNG_ERR_ROTATION_PENDING
;
2647 * Starting a session without channel is useless since after that it's not
2648 * possible to enable channel thus inform the client.
2650 if (usess
&& usess
->domain_global
.channels
) {
2651 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2654 nb_chan
+= ksession
->channel_count
;
2657 ret
= LTTNG_ERR_NO_CHANNEL
;
2661 session
->active
= 1;
2662 session
->rotated_after_last_stop
= false;
2663 session
->cleared_after_last_stop
= false;
2664 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2665 if (!session
->has_been_started
) {
2666 struct lttng_trace_chunk
*trace_chunk
;
2668 DBG("Creating initial trace chunk of session \"%s\"",
2670 trace_chunk
= session_create_new_trace_chunk(
2671 session
, NULL
, NULL
, NULL
);
2673 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2676 LTTNG_ASSERT(!session
->current_trace_chunk
);
2677 ret
= (lttng_error_code
) session_set_trace_chunk(session
, trace_chunk
,
2679 lttng_trace_chunk_put(trace_chunk
);
2681 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2685 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2688 * Rotate existing streams into the new chunk.
2689 * This is a "quiet" rotation has no client has
2690 * explicitly requested this operation.
2692 * There is also no need to wait for the rotation
2693 * to complete as it will happen immediately. No data
2694 * was produced as the session was stopped, so the
2695 * rotation should happen on reception of the command.
2697 ret
= (lttng_error_code
) cmd_rotate_session(session
, NULL
, true,
2698 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2699 if (ret
!= LTTNG_OK
) {
2705 /* Kernel tracing */
2706 if (ksession
!= NULL
) {
2707 DBG("Start kernel tracing session %s", session
->name
);
2708 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2709 if (ret
!= LTTNG_OK
) {
2714 /* Flag session that trace should start automatically */
2716 int int_ret
= ust_app_start_trace_all(usess
);
2719 ret
= LTTNG_ERR_UST_START_FAIL
;
2725 * Open a packet in every stream of the session to ensure that viewers
2726 * can correctly identify the boundaries of the periods during which
2727 * tracing was active for this session.
2729 ret
= session_open_packets(session
);
2730 if (ret
!= LTTNG_OK
) {
2735 * Clear the flag that indicates that a rotation was done while the
2736 * session was stopped.
2738 session
->rotated_after_last_stop
= false;
2740 if (session
->rotate_timer_period
) {
2741 int int_ret
= timer_session_rotation_schedule_timer_start(
2742 session
, session
->rotate_timer_period
);
2745 ERR("Failed to enable rotate timer");
2746 ret
= LTTNG_ERR_UNK
;
2754 if (ret
== LTTNG_OK
) {
2755 /* Flag this after a successful start. */
2756 session
->has_been_started
|= 1;
2758 session
->active
= 0;
2759 /* Restore initial state on error. */
2760 session
->rotated_after_last_stop
=
2761 session_rotated_after_last_stop
;
2762 session
->cleared_after_last_stop
=
2763 session_cleared_after_last_stop
;
2770 * Command LTTNG_STOP_TRACE processed by the client thread.
2772 int cmd_stop_trace(struct ltt_session
*session
)
2775 struct ltt_kernel_session
*ksession
;
2776 struct ltt_ust_session
*usess
;
2778 LTTNG_ASSERT(session
);
2780 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2782 ksession
= session
->kernel_session
;
2783 usess
= session
->ust_session
;
2785 /* Session is not active. Skip everything and inform the client. */
2786 if (!session
->active
) {
2787 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2791 ret
= stop_kernel_session(ksession
);
2792 if (ret
!= LTTNG_OK
) {
2796 if (usess
&& usess
->active
) {
2797 ret
= ust_app_stop_trace_all(usess
);
2799 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2804 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2806 /* Flag inactive after a successful stop. */
2807 session
->active
= 0;
2815 * Set the base_path of the session only if subdir of a control uris is set.
2816 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2818 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2820 struct lttng_uri
*uris
)
2825 for (i
= 0; i
< nb_uri
; i
++) {
2826 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2827 uris
[i
].subdir
[0] == '\0') {
2828 /* Not interested in these URIs */
2832 if (session
->base_path
!= NULL
) {
2833 free(session
->base_path
);
2834 session
->base_path
= NULL
;
2837 /* Set session base_path */
2838 session
->base_path
= strdup(uris
[i
].subdir
);
2839 if (!session
->base_path
) {
2840 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2841 uris
[i
].subdir
, session
->name
);
2842 ret
= LTTNG_ERR_NOMEM
;
2845 DBG2("Setting base path \"%s\" for session \"%s\"",
2846 session
->base_path
, session
->name
);
2854 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2856 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2857 struct lttng_uri
*uris
)
2860 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2861 struct ltt_ust_session
*usess
= session
->ust_session
;
2863 LTTNG_ASSERT(session
);
2865 LTTNG_ASSERT(nb_uri
> 0);
2867 /* Can't set consumer URI if the session is active. */
2868 if (session
->active
) {
2869 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2874 * Set the session base path if any. This is done inside
2875 * cmd_set_consumer_uri to preserve backward compatibility of the
2876 * previous session creation api vs the session descriptor api.
2878 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2879 if (ret
!= LTTNG_OK
) {
2883 /* Set the "global" consumer URIs */
2884 for (i
= 0; i
< nb_uri
; i
++) {
2885 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2887 if (ret
!= LTTNG_OK
) {
2892 /* Set UST session URIs */
2893 if (session
->ust_session
) {
2894 for (i
= 0; i
< nb_uri
; i
++) {
2895 ret
= add_uri_to_consumer(session
,
2896 session
->ust_session
->consumer
,
2897 &uris
[i
], LTTNG_DOMAIN_UST
);
2898 if (ret
!= LTTNG_OK
) {
2904 /* Set kernel session URIs */
2905 if (session
->kernel_session
) {
2906 for (i
= 0; i
< nb_uri
; i
++) {
2907 ret
= add_uri_to_consumer(session
,
2908 session
->kernel_session
->consumer
,
2909 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2910 if (ret
!= LTTNG_OK
) {
2917 * Make sure to set the session in output mode after we set URI since a
2918 * session can be created without URL (thus flagged in no output mode).
2920 session
->output_traces
= 1;
2922 ksess
->output_traces
= 1;
2926 usess
->output_traces
= 1;
2937 enum lttng_error_code
set_session_output_from_descriptor(
2938 struct ltt_session
*session
,
2939 const struct lttng_session_descriptor
*descriptor
)
2942 enum lttng_error_code ret_code
= LTTNG_OK
;
2943 enum lttng_session_descriptor_type session_type
=
2944 lttng_session_descriptor_get_type(descriptor
);
2945 enum lttng_session_descriptor_output_type output_type
=
2946 lttng_session_descriptor_get_output_type(descriptor
);
2947 struct lttng_uri uris
[2] = {};
2948 size_t uri_count
= 0;
2950 switch (output_type
) {
2951 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2953 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2954 lttng_session_descriptor_get_local_output_uri(descriptor
,
2958 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2959 lttng_session_descriptor_get_network_output_uris(descriptor
,
2960 &uris
[0], &uris
[1]);
2964 ret_code
= LTTNG_ERR_INVALID
;
2968 switch (session_type
) {
2969 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2971 struct snapshot_output
*new_output
= NULL
;
2973 new_output
= snapshot_output_alloc();
2975 ret_code
= LTTNG_ERR_NOMEM
;
2979 ret
= snapshot_output_init_with_uri(session
,
2980 DEFAULT_SNAPSHOT_MAX_SIZE
,
2981 NULL
, uris
, uri_count
, session
->consumer
,
2982 new_output
, &session
->snapshot
);
2984 ret_code
= (ret
== -ENOMEM
) ?
2985 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2986 snapshot_output_destroy(new_output
);
2989 snapshot_add_output(&session
->snapshot
, new_output
);
2992 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2993 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2995 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
2999 ret_code
= LTTNG_ERR_INVALID
;
3007 enum lttng_error_code
cmd_create_session_from_descriptor(
3008 struct lttng_session_descriptor
*descriptor
,
3009 const lttng_sock_cred
*creds
,
3010 const char *home_path
)
3013 enum lttng_error_code ret_code
;
3014 const char *session_name
;
3015 struct ltt_session
*new_session
= NULL
;
3016 enum lttng_session_descriptor_status descriptor_status
;
3018 session_lock_list();
3020 if (*home_path
!= '/') {
3021 ERR("Home path provided by client is not absolute");
3022 ret_code
= LTTNG_ERR_INVALID
;
3027 descriptor_status
= lttng_session_descriptor_get_session_name(
3028 descriptor
, &session_name
);
3029 switch (descriptor_status
) {
3030 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3032 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3033 session_name
= NULL
;
3036 ret_code
= LTTNG_ERR_INVALID
;
3040 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3042 if (ret_code
!= LTTNG_OK
) {
3046 if (!session_name
) {
3047 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3050 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3055 if (!lttng_session_descriptor_is_output_destination_initialized(
3058 * Only include the session's creation time in the output
3059 * destination if the name of the session itself was
3060 * not auto-generated.
3062 ret_code
= lttng_session_descriptor_set_default_output(
3064 session_name
? &new_session
->creation_time
: NULL
,
3066 if (ret_code
!= LTTNG_OK
) {
3070 new_session
->has_user_specified_directory
=
3071 lttng_session_descriptor_has_output_directory(
3075 switch (lttng_session_descriptor_get_type(descriptor
)) {
3076 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3077 new_session
->snapshot_mode
= 1;
3079 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3080 new_session
->live_timer
=
3081 lttng_session_descriptor_live_get_timer_interval(
3088 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3089 if (ret_code
!= LTTNG_OK
) {
3092 new_session
->consumer
->enabled
= 1;
3093 ret_code
= LTTNG_OK
;
3095 /* Release reference provided by the session_create function. */
3096 session_put(new_session
);
3097 if (ret_code
!= LTTNG_OK
&& new_session
) {
3098 /* Release the global reference on error. */
3099 session_destroy(new_session
);
3101 session_unlock_list();
3105 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3106 struct lttng_session_descriptor
**return_descriptor
)
3109 size_t payload_size
;
3110 struct lttng_dynamic_buffer payload
;
3111 struct lttng_buffer_view home_dir_view
;
3112 struct lttng_buffer_view session_descriptor_view
;
3113 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3114 enum lttng_error_code ret_code
;
3116 lttng_dynamic_buffer_init(&payload
);
3117 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3119 ret_code
= LTTNG_ERR_INVALID
;
3122 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3123 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3124 ret_code
= LTTNG_ERR_INVALID
;
3128 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3129 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3130 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3132 ret_code
= LTTNG_ERR_NOMEM
;
3136 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3138 ERR("Reception of session descriptor failed, aborting.");
3139 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3143 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3146 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3147 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3148 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3149 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3150 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3154 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3156 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3157 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3158 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3159 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3160 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3164 ret
= lttng_session_descriptor_create_from_buffer(
3165 &session_descriptor_view
, &session_descriptor
);
3167 ERR("Failed to create session descriptor from payload of \"create session\" command");
3168 ret_code
= LTTNG_ERR_INVALID
;
3173 * Sets the descriptor's auto-generated properties (name, output) if
3176 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3178 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3179 if (ret_code
!= LTTNG_OK
) {
3183 ret_code
= LTTNG_OK
;
3184 *return_descriptor
= session_descriptor
;
3185 session_descriptor
= NULL
;
3187 lttng_dynamic_buffer_reset(&payload
);
3188 lttng_session_descriptor_destroy(session_descriptor
);
3193 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3194 void *_reply_context
)
3198 const struct cmd_destroy_session_reply_context
*reply_context
=
3199 (cmd_destroy_session_reply_context
*) _reply_context
;
3200 struct lttng_dynamic_buffer payload
;
3201 struct lttcomm_session_destroy_command_header cmd_header
;
3202 struct lttng_trace_archive_location
*location
= NULL
;
3203 struct lttcomm_lttng_msg llm
= {
3204 .cmd_type
= LTTNG_DESTROY_SESSION
,
3205 .ret_code
= reply_context
->destruction_status
,
3208 sizeof(struct lttcomm_session_destroy_command_header
),
3211 size_t payload_size_before_location
;
3213 lttng_dynamic_buffer_init(&payload
);
3215 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3217 ERR("Failed to append session destruction message");
3221 cmd_header
.rotation_state
=
3222 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3223 session
->rotation_state
:
3224 LTTNG_ROTATION_STATE_NO_ROTATION
);
3225 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3226 sizeof(cmd_header
));
3228 ERR("Failed to append session destruction command header");
3232 if (!reply_context
->implicit_rotation_on_destroy
) {
3233 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3237 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3238 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3243 location
= session_get_trace_archive_location(session
);
3245 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3250 payload_size_before_location
= payload
.size
;
3251 comm_ret
= lttng_trace_archive_location_serialize(location
,
3253 lttng_trace_archive_location_put(location
);
3255 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3259 /* Update the message to indicate the location's length. */
3260 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3261 payload
.size
- payload_size_before_location
;
3263 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3264 payload
.data
, payload
.size
);
3265 if (comm_ret
!= (ssize_t
) payload
.size
) {
3266 ERR("Failed to send result of the destruction of session \"%s\" to client",
3270 ret
= close(reply_context
->reply_sock_fd
);
3272 PERROR("Failed to close client socket in deferred session destroy reply");
3274 lttng_dynamic_buffer_reset(&payload
);
3275 free(_reply_context
);
3279 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3281 * Called with session lock held.
3283 int cmd_destroy_session(struct ltt_session
*session
,
3284 struct notification_thread_handle
*notification_thread_handle
,
3288 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3289 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3292 reply_context
= (cmd_destroy_session_reply_context
*) zmalloc(sizeof(*reply_context
));
3293 if (!reply_context
) {
3294 ret
= LTTNG_ERR_NOMEM
;
3297 reply_context
->reply_sock_fd
= *sock_fd
;
3301 LTTNG_ASSERT(session
);
3303 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3305 if (session
->active
) {
3306 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3308 ret
= cmd_stop_trace(session
);
3309 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3310 /* Carry on with the destruction of the session. */
3311 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3312 session
->name
, lttng_strerror(-ret
));
3313 destruction_last_error
= (lttng_error_code
) ret
;
3317 if (session
->rotation_schedule_timer_enabled
) {
3318 if (timer_session_rotation_schedule_timer_stop(
3320 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3322 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3326 if (session
->rotate_size
) {
3327 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3328 session
->rotate_size
= 0;
3331 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3333 * Perform a last rotation on destruction if rotations have
3334 * occurred during the session's lifetime.
3336 ret
= cmd_rotate_session(session
, NULL
, false,
3337 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3338 if (ret
!= LTTNG_OK
) {
3339 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3340 session
->name
, lttng_strerror(-ret
));
3341 destruction_last_error
= (lttng_error_code
) -ret
;
3343 if (reply_context
) {
3344 reply_context
->implicit_rotation_on_destroy
= true;
3346 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3348 * The user has not triggered a session rotation. However, to
3349 * ensure all data has been consumed, the session is rotated
3350 * to a 'null' trace chunk before it is destroyed.
3352 * This is a "quiet" rotation meaning that no notification is
3353 * emitted and no renaming of the current trace chunk takes
3356 ret
= cmd_rotate_session(session
, NULL
, true,
3357 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3359 * Rotation operations may not be supported by the kernel
3360 * tracer. Hence, do not consider this implicit rotation as
3361 * a session destruction error. The library has already stopped
3362 * the session and waited for pending data; there is nothing
3363 * left to do but complete the destruction of the session.
3365 if (ret
!= LTTNG_OK
&&
3366 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3367 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3368 session
->name
, lttng_strerror(ret
));
3369 destruction_last_error
= (lttng_error_code
) -ret
;
3373 if (session
->shm_path
[0]) {
3375 * When a session is created with an explicit shm_path,
3376 * the consumer daemon will create its shared memory files
3377 * at that location and will *not* unlink them. This is normal
3378 * as the intention of that feature is to make it possible
3379 * to retrieve the content of those files should a crash occur.
3381 * To ensure the content of those files can be used, the
3382 * sessiond daemon will replicate the content of the metadata
3383 * cache in a metadata file.
3385 * On clean-up, it is expected that the consumer daemon will
3386 * unlink the shared memory files and that the session daemon
3387 * will unlink the metadata file. Then, the session's directory
3388 * in the shm path can be removed.
3390 * Unfortunately, a flaw in the design of the sessiond's and
3391 * consumerd's tear down of channels makes it impossible to
3392 * determine when the sessiond _and_ the consumerd have both
3393 * destroyed their representation of a channel. For one, the
3394 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3395 * callbacks in both daemons.
3397 * However, it is also impossible for the sessiond to know when
3398 * the consumer daemon is done destroying its channel(s) since
3399 * it occurs as a reaction to the closing of the channel's file
3400 * descriptor. There is no resulting communication initiated
3401 * from the consumerd to the sessiond to confirm that the
3402 * operation is completed (and was successful).
3404 * Until this is all fixed, the session daemon checks for the
3405 * removal of the session's shm path which makes it possible
3406 * to safely advertise a session as having been destroyed.
3408 * Prior to this fix, it was not possible to reliably save
3409 * a session making use of the --shm-path option, destroy it,
3410 * and load it again. This is because the creation of the
3411 * session would fail upon seeing the session's shm path
3412 * already in existence.
3414 * Note that none of the error paths in the check for the
3415 * directory's existence return an error. This is normal
3416 * as there isn't much that can be done. The session will
3417 * be destroyed properly, except that we can't offer the
3418 * guarantee that the same session can be re-created.
3420 current_completion_handler
= &destroy_completion_handler
.handler
;
3421 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3423 sizeof(destroy_completion_handler
.shm_path
));
3428 * The session is destroyed. However, note that the command context
3429 * still holds a reference to the session, thus delaying its destruction
3430 * _at least_ up to the point when that reference is released.
3432 session_destroy(session
);
3433 if (reply_context
) {
3434 reply_context
->destruction_status
= destruction_last_error
;
3435 ret
= session_add_destroy_notifier(session
,
3436 cmd_destroy_session_reply
,
3437 (void *) reply_context
);
3439 ret
= LTTNG_ERR_FATAL
;
3451 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3453 int cmd_register_consumer(struct ltt_session
*session
,
3454 enum lttng_domain_type domain
, const char *sock_path
,
3455 struct consumer_data
*cdata
)
3458 struct consumer_socket
*socket
= NULL
;
3460 LTTNG_ASSERT(session
);
3461 LTTNG_ASSERT(cdata
);
3462 LTTNG_ASSERT(sock_path
);
3465 case LTTNG_DOMAIN_KERNEL
:
3467 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3469 LTTNG_ASSERT(ksess
);
3471 /* Can't register a consumer if there is already one */
3472 if (ksess
->consumer_fds_sent
!= 0) {
3473 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3477 sock
= lttcomm_connect_unix_sock(sock_path
);
3479 ret
= LTTNG_ERR_CONNECT_FAIL
;
3482 cdata
->cmd_sock
= sock
;
3484 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3485 if (socket
== NULL
) {
3488 PERROR("close register consumer");
3490 cdata
->cmd_sock
= -1;
3491 ret
= LTTNG_ERR_FATAL
;
3495 socket
->lock
= (pthread_mutex_t
*) zmalloc(sizeof(pthread_mutex_t
));
3496 if (socket
->lock
== NULL
) {
3497 PERROR("zmalloc pthread mutex");
3498 ret
= LTTNG_ERR_FATAL
;
3501 pthread_mutex_init(socket
->lock
, NULL
);
3502 socket
->registered
= 1;
3505 consumer_add_socket(socket
, ksess
->consumer
);
3508 pthread_mutex_lock(&cdata
->pid_mutex
);
3510 pthread_mutex_unlock(&cdata
->pid_mutex
);
3515 /* TODO: Userspace tracing */
3516 ret
= LTTNG_ERR_UND
;
3524 consumer_destroy_socket(socket
);
3530 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3532 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3533 struct lttng_domain
**domains
)
3538 struct lttng_ht_iter iter
;
3540 if (session
->kernel_session
!= NULL
) {
3541 DBG3("Listing domains found kernel domain");
3545 if (session
->ust_session
!= NULL
) {
3546 DBG3("Listing domains found UST global domain");
3550 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3552 if (agt
->being_used
) {
3563 *domains
= (lttng_domain
*) zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3564 if (*domains
== NULL
) {
3565 ret
= LTTNG_ERR_FATAL
;
3569 if (session
->kernel_session
!= NULL
) {
3570 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3572 /* Kernel session buffer type is always GLOBAL */
3573 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3578 if (session
->ust_session
!= NULL
) {
3579 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3580 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3584 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3586 if (agt
->being_used
) {
3587 (*domains
)[index
].type
= agt
->domain
;
3588 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3598 /* Return negative value to differentiate return code */
3604 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3606 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3607 struct ltt_session
*session
, struct lttng_channel
**channels
)
3609 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3612 case LTTNG_DOMAIN_KERNEL
:
3613 if (session
->kernel_session
!= NULL
) {
3614 nb_chan
= session
->kernel_session
->channel_count
;
3616 DBG3("Number of kernel channels %zd", nb_chan
);
3618 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3622 case LTTNG_DOMAIN_UST
:
3623 if (session
->ust_session
!= NULL
) {
3625 nb_chan
= lttng_ht_get_count(
3626 session
->ust_session
->domain_global
.channels
);
3629 DBG3("Number of UST global channels %zd", nb_chan
);
3631 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3636 ret
= -LTTNG_ERR_UND
;
3641 const size_t channel_size
= sizeof(struct lttng_channel
) +
3642 sizeof(struct lttng_channel_extended
);
3643 struct lttng_channel_extended
*channel_exts
;
3645 payload_size
= nb_chan
* channel_size
;
3646 *channels
= (lttng_channel
*) zmalloc(payload_size
);
3647 if (*channels
== NULL
) {
3648 ret
= -LTTNG_ERR_FATAL
;
3652 channel_exts
= (lttng_channel_extended
*)
3653 (((char *) *channels
) + (nb_chan
* sizeof(struct lttng_channel
)));
3654 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3655 if (ret
!= LTTNG_OK
) {
3670 * Command LTTNG_LIST_EVENTS processed by the client thread.
3672 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3673 struct ltt_session
*session
, char *channel_name
,
3674 struct lttng_payload
*payload
)
3677 ssize_t nb_events
= 0;
3678 struct lttcomm_event_command_header cmd_header
= {};
3679 const size_t cmd_header_offset
= payload
->buffer
.size
;
3681 ret
= lttng_dynamic_buffer_append(
3682 &payload
->buffer
, &cmd_header
, sizeof(cmd_header
));
3684 ret
= LTTNG_ERR_NOMEM
;
3689 case LTTNG_DOMAIN_KERNEL
:
3690 if (session
->kernel_session
!= NULL
) {
3691 nb_events
= list_lttng_kernel_events(channel_name
,
3692 session
->kernel_session
, payload
);
3695 case LTTNG_DOMAIN_UST
:
3697 if (session
->ust_session
!= NULL
) {
3698 nb_events
= list_lttng_ust_global_events(channel_name
,
3699 &session
->ust_session
->domain_global
,
3704 case LTTNG_DOMAIN_LOG4J
:
3705 case LTTNG_DOMAIN_JUL
:
3706 case LTTNG_DOMAIN_PYTHON
:
3707 if (session
->ust_session
) {
3708 struct lttng_ht_iter iter
;
3712 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3713 &iter
.iter
, agt
, node
.node
) {
3714 if (agt
->domain
== domain
) {
3715 nb_events
= list_lttng_agent_events(
3724 ret
= LTTNG_ERR_UND
;
3728 ((struct lttcomm_event_command_header
*) (payload
->buffer
.data
+
3729 cmd_header_offset
))->nb_events
= (uint32_t) nb_events
;
3734 /* Return negative value to differentiate return code */
3739 * Using the session list, filled a lttng_session array to send back to the
3740 * client for session listing.
3742 * The session list lock MUST be acquired before calling this function. Use
3743 * session_lock_list() and session_unlock_list().
3745 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3746 size_t session_count
, uid_t uid
, gid_t gid
)
3750 struct ltt_session
*session
;
3751 struct ltt_session_list
*list
= session_get_list();
3752 struct lttng_session_extended
*extended
=
3753 (typeof(extended
)) (&sessions
[session_count
]);
3755 DBG("Getting all available session for UID %d GID %d",
3758 * Iterate over session list and append data after the control struct in
3761 cds_list_for_each_entry(session
, &list
->head
, list
) {
3762 if (!session_get(session
)) {
3766 * Only list the sessions the user can control.
3768 if (!session_access_ok(session
, uid
) ||
3769 session
->destroyed
) {
3770 session_put(session
);
3774 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3775 struct ltt_ust_session
*usess
= session
->ust_session
;
3777 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3778 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3779 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3780 ret
= build_network_session_path(sessions
[i
].path
,
3781 sizeof(sessions
[i
].path
), session
);
3783 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3784 session
->consumer
->dst
.session_root_path
);
3787 PERROR("snprintf session path");
3788 session_put(session
);
3792 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3793 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3794 sessions
[i
].enabled
= session
->active
;
3795 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3796 sessions
[i
].live_timer_interval
= session
->live_timer
;
3797 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3798 extended
[i
].creation_time
.is_set
= 1;
3800 session_put(session
);
3805 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3806 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3808 int cmd_data_pending(struct ltt_session
*session
)
3811 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3812 struct ltt_ust_session
*usess
= session
->ust_session
;
3814 LTTNG_ASSERT(session
);
3816 DBG("Data pending for session %s", session
->name
);
3818 /* Session MUST be stopped to ask for data availability. */
3819 if (session
->active
) {
3820 ret
= LTTNG_ERR_SESSION_STARTED
;
3824 * If stopped, just make sure we've started before else the above call
3825 * will always send that there is data pending.
3827 * The consumer assumes that when the data pending command is received,
3828 * the trace has been started before or else no output data is written
3829 * by the streams which is a condition for data pending. So, this is
3830 * *VERY* important that we don't ask the consumer before a start
3833 if (!session
->has_been_started
) {
3839 /* A rotation is still pending, we have to wait. */
3840 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3841 DBG("Rotate still pending for session %s", session
->name
);
3846 if (ksess
&& ksess
->consumer
) {
3847 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3849 /* Data is still being extracted for the kernel. */
3854 if (usess
&& usess
->consumer
) {
3855 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3857 /* Data is still being extracted for the kernel. */
3862 /* Data is ready to be read by a viewer */
3870 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3872 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3874 int cmd_snapshot_add_output(struct ltt_session
*session
,
3875 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3878 struct snapshot_output
*new_output
;
3880 LTTNG_ASSERT(session
);
3881 LTTNG_ASSERT(output
);
3883 DBG("Cmd snapshot add output for session %s", session
->name
);
3886 * Can't create an output if the session is not set in no-output mode.
3888 if (session
->output_traces
) {
3889 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3893 if (session
->has_non_mmap_channel
) {
3894 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3898 /* Only one output is allowed until we have the "tee" feature. */
3899 if (session
->snapshot
.nb_output
== 1) {
3900 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3904 new_output
= snapshot_output_alloc();
3906 ret
= LTTNG_ERR_NOMEM
;
3910 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3911 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3912 &session
->snapshot
);
3914 if (ret
== -ENOMEM
) {
3915 ret
= LTTNG_ERR_NOMEM
;
3917 ret
= LTTNG_ERR_INVALID
;
3923 snapshot_add_output(&session
->snapshot
, new_output
);
3925 *id
= new_output
->id
;
3932 snapshot_output_destroy(new_output
);
3938 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3940 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3942 int cmd_snapshot_del_output(struct ltt_session
*session
,
3943 const struct lttng_snapshot_output
*output
)
3946 struct snapshot_output
*sout
= NULL
;
3948 LTTNG_ASSERT(session
);
3949 LTTNG_ASSERT(output
);
3954 * Permission denied to create an output if the session is not
3955 * set in no output mode.
3957 if (session
->output_traces
) {
3958 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3963 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3965 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3966 } else if (*output
->name
!= '\0') {
3967 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3969 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3972 ret
= LTTNG_ERR_INVALID
;
3976 snapshot_delete_output(&session
->snapshot
, sout
);
3977 snapshot_output_destroy(sout
);
3986 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3988 * If no output is available, outputs is untouched and 0 is returned.
3990 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3992 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3993 struct lttng_snapshot_output
**outputs
)
3996 struct lttng_snapshot_output
*list
= NULL
;
3997 struct lttng_ht_iter iter
;
3998 struct snapshot_output
*output
;
4000 LTTNG_ASSERT(session
);
4001 LTTNG_ASSERT(outputs
);
4003 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4006 * Permission denied to create an output if the session is not
4007 * set in no output mode.
4009 if (session
->output_traces
) {
4010 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4014 if (session
->snapshot
.nb_output
== 0) {
4019 list
= (lttng_snapshot_output
*) zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4021 ret
= -LTTNG_ERR_NOMEM
;
4025 /* Copy list from session to the new list object. */
4027 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4028 output
, node
.node
) {
4029 LTTNG_ASSERT(output
->consumer
);
4030 list
[idx
].id
= output
->id
;
4031 list
[idx
].max_size
= output
->max_size
;
4032 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4033 sizeof(list
[idx
].name
))) {
4034 ret
= -LTTNG_ERR_INVALID
;
4037 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4038 if (lttng_strncpy(list
[idx
].ctrl_url
,
4039 output
->consumer
->dst
.session_root_path
,
4040 sizeof(list
[idx
].ctrl_url
))) {
4041 ret
= -LTTNG_ERR_INVALID
;
4046 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4047 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4049 ret
= -LTTNG_ERR_NOMEM
;
4054 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4055 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4057 ret
= -LTTNG_ERR_NOMEM
;
4066 ret
= session
->snapshot
.nb_output
;
4075 * Check if we can regenerate the metadata for this session.
4076 * Only kernel, UST per-uid and non-live sessions are supported.
4078 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4081 int check_regenerate_metadata_support(struct ltt_session
*session
)
4085 LTTNG_ASSERT(session
);
4087 if (session
->live_timer
!= 0) {
4088 ret
= LTTNG_ERR_LIVE_SESSION
;
4091 if (!session
->active
) {
4092 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4095 if (session
->ust_session
) {
4096 switch (session
->ust_session
->buffer_type
) {
4097 case LTTNG_BUFFER_PER_UID
:
4099 case LTTNG_BUFFER_PER_PID
:
4100 ret
= LTTNG_ERR_PER_PID_SESSION
;
4104 ret
= LTTNG_ERR_UNK
;
4108 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4109 session
->consumer
->relay_minor_version
< 8) {
4110 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4120 int clear_metadata_file(int fd
)
4125 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4126 if (lseek_ret
< 0) {
4132 ret
= ftruncate(fd
, 0);
4134 PERROR("ftruncate");
4143 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4146 struct buffer_reg_uid
*uid_reg
= NULL
;
4147 struct buffer_reg_session
*session_reg
= NULL
;
4150 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4151 struct ust_registry_session
*registry
;
4152 struct ust_registry_channel
*chan
;
4153 struct lttng_ht_iter iter_chan
;
4155 session_reg
= uid_reg
->registry
;
4156 registry
= session_reg
->reg
.ust
;
4158 pthread_mutex_lock(®istry
->lock
);
4159 registry
->metadata_len_sent
= 0;
4160 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4161 registry
->metadata_len
= 0;
4162 registry
->metadata_version
++;
4163 if (registry
->metadata_fd
> 0) {
4164 /* Clear the metadata file's content. */
4165 ret
= clear_metadata_file(registry
->metadata_fd
);
4167 pthread_mutex_unlock(®istry
->lock
);
4172 ret
= ust_metadata_session_statedump(registry
, NULL
,
4173 registry
->major
, registry
->minor
);
4175 pthread_mutex_unlock(®istry
->lock
);
4176 ERR("Failed to generate session metadata (err = %d)",
4180 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4182 struct ust_registry_event
*event
;
4183 struct lttng_ht_iter iter_event
;
4185 ret
= ust_metadata_channel_statedump(registry
, chan
);
4187 pthread_mutex_unlock(®istry
->lock
);
4188 ERR("Failed to generate channel metadata "
4192 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4194 ret
= ust_metadata_event_statedump(registry
,
4197 pthread_mutex_unlock(®istry
->lock
);
4198 ERR("Failed to generate event metadata "
4204 pthread_mutex_unlock(®istry
->lock
);
4213 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4215 * Ask the consumer to truncate the existing metadata file(s) and
4216 * then regenerate the metadata. Live and per-pid sessions are not
4217 * supported and return an error.
4219 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4221 int cmd_regenerate_metadata(struct ltt_session
*session
)
4225 LTTNG_ASSERT(session
);
4227 ret
= check_regenerate_metadata_support(session
);
4232 if (session
->kernel_session
) {
4233 ret
= kernctl_session_regenerate_metadata(
4234 session
->kernel_session
->fd
);
4236 ERR("Failed to regenerate the kernel metadata");
4241 if (session
->ust_session
) {
4242 ret
= ust_regenerate_metadata(session
->ust_session
);
4244 ERR("Failed to regenerate the UST metadata");
4248 DBG("Cmd metadata regenerate for session %s", session
->name
);
4256 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4258 * Ask the tracer to regenerate a new statedump.
4260 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4262 int cmd_regenerate_statedump(struct ltt_session
*session
)
4266 LTTNG_ASSERT(session
);
4268 if (!session
->active
) {
4269 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4273 if (session
->kernel_session
) {
4274 ret
= kernctl_session_regenerate_statedump(
4275 session
->kernel_session
->fd
);
4277 * Currently, the statedump in kernel can only fail if out
4281 if (ret
== -ENOMEM
) {
4282 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4284 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4286 ERR("Failed to regenerate the kernel statedump");
4291 if (session
->ust_session
) {
4292 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4294 * Currently, the statedump in UST always returns 0.
4297 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4298 ERR("Failed to regenerate the UST statedump");
4302 DBG("Cmd regenerate statedump for session %s", session
->name
);
4310 enum lttng_error_code
synchronize_tracer_notifier_register(
4311 struct notification_thread_handle
*notification_thread
,
4312 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4314 enum lttng_error_code ret_code
;
4315 const struct lttng_condition
*condition
=
4316 lttng_trigger_get_const_condition(trigger
);
4317 const char *trigger_name
;
4318 uid_t trigger_owner
;
4319 enum lttng_trigger_status trigger_status
;
4320 const enum lttng_domain_type trigger_domain
=
4321 lttng_trigger_get_underlying_domain_type_restriction(
4324 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4325 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4327 LTTNG_ASSERT(condition
);
4328 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4329 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4331 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4332 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4333 trigger_name
: "(anonymous)";
4335 session_lock_list();
4336 switch (trigger_domain
) {
4337 case LTTNG_DOMAIN_KERNEL
:
4339 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4340 if (ret_code
!= LTTNG_OK
) {
4341 enum lttng_error_code notif_thread_unregister_ret
;
4343 notif_thread_unregister_ret
=
4344 notification_thread_command_unregister_trigger(
4345 notification_thread
, trigger
);
4347 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4348 /* Return the original error code. */
4349 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4351 (int) trigger_owner
,
4357 case LTTNG_DOMAIN_UST
:
4358 ust_app_global_update_all_event_notifier_rules();
4360 case LTTNG_DOMAIN_JUL
:
4361 case LTTNG_DOMAIN_LOG4J
:
4362 case LTTNG_DOMAIN_PYTHON
:
4364 /* Agent domains. */
4365 struct agent
*agt
= agent_find_by_event_notifier_domain(
4369 agt
= agent_create(trigger_domain
);
4371 ret_code
= LTTNG_ERR_NOMEM
;
4372 goto end_unlock_session_list
;
4375 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4378 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4379 if (ret_code
!= LTTNG_OK
) {
4380 goto end_unlock_session_list
;
4385 case LTTNG_DOMAIN_NONE
:
4390 ret_code
= LTTNG_OK
;
4391 end_unlock_session_list
:
4392 session_unlock_list();
4396 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4397 struct lttng_trigger
*trigger
,
4398 bool is_trigger_anonymous
,
4399 struct notification_thread_handle
*notification_thread
,
4400 struct lttng_trigger
**return_trigger
)
4402 enum lttng_error_code ret_code
;
4403 const char *trigger_name
;
4404 uid_t trigger_owner
;
4405 enum lttng_trigger_status trigger_status
;
4407 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4408 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4409 trigger_name
: "(anonymous)";
4411 trigger_status
= lttng_trigger_get_owner_uid(
4412 trigger
, &trigger_owner
);
4413 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4415 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4416 trigger_name
, (int) trigger_owner
,
4417 (int) lttng_credentials_get_uid(cmd_creds
));
4420 * Validate the trigger credentials against the command credentials.
4421 * Only the root user can register a trigger with non-matching
4424 if (!lttng_credentials_is_equal_uid(
4425 lttng_trigger_get_credentials(trigger
),
4427 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4428 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4429 trigger_name
, (int) trigger_owner
,
4430 (int) lttng_credentials_get_uid(cmd_creds
));
4431 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4437 * The bytecode generation also serves as a validation step for the
4438 * bytecode expressions.
4440 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4441 if (ret_code
!= LTTNG_OK
) {
4442 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4443 trigger_name
, (int) trigger_owner
, ret_code
);
4448 * A reference to the trigger is acquired by the notification thread.
4449 * It is safe to return the same trigger to the caller since it the
4450 * other user holds a reference.
4452 * The trigger is modified during the execution of the
4453 * "register trigger" command. However, by the time the command returns,
4454 * it is safe to use without any locking as its properties are
4457 ret_code
= notification_thread_command_register_trigger(
4458 notification_thread
, trigger
, is_trigger_anonymous
);
4459 if (ret_code
!= LTTNG_OK
) {
4460 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4461 trigger_name
, (int) trigger_owner
, ret_code
);
4465 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4466 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4467 trigger_name
: "(anonymous)";
4470 * Synchronize tracers if the trigger adds an event notifier.
4472 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4473 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4474 trigger
, cmd_creds
);
4475 if (ret_code
!= LTTNG_OK
) {
4476 ERR("Error registering tracer notifier: %s",
4477 lttng_strerror(-ret_code
));
4483 * Return an updated trigger to the client.
4485 * Since a modified version of the same trigger is returned, acquire a
4486 * reference to the trigger so the caller doesn't have to care if those
4487 * are distinct instances or not.
4489 if (ret_code
== LTTNG_OK
) {
4490 lttng_trigger_get(trigger
);
4491 *return_trigger
= trigger
;
4492 /* Ownership of trigger was transferred to caller. */
4500 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4501 const struct lttng_trigger
*trigger
)
4503 enum lttng_error_code ret_code
;
4504 const struct lttng_condition
*condition
=
4505 lttng_trigger_get_const_condition(trigger
);
4506 const enum lttng_domain_type trigger_domain
=
4507 lttng_trigger_get_underlying_domain_type_restriction(
4510 LTTNG_ASSERT(condition
);
4511 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4512 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4514 session_lock_list();
4515 switch (trigger_domain
) {
4516 case LTTNG_DOMAIN_KERNEL
:
4517 ret_code
= kernel_unregister_event_notifier(trigger
);
4518 if (ret_code
!= LTTNG_OK
) {
4519 goto end_unlock_session_list
;
4523 case LTTNG_DOMAIN_UST
:
4524 ust_app_global_update_all_event_notifier_rules();
4526 case LTTNG_DOMAIN_JUL
:
4527 case LTTNG_DOMAIN_LOG4J
:
4528 case LTTNG_DOMAIN_PYTHON
:
4530 /* Agent domains. */
4531 struct agent
*agt
= agent_find_by_event_notifier_domain(
4535 * This trigger was never registered in the first place. Calling
4536 * this function under those circumstances is an internal error.
4539 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4540 if (ret_code
!= LTTNG_OK
) {
4541 goto end_unlock_session_list
;
4546 case LTTNG_DOMAIN_NONE
:
4551 ret_code
= LTTNG_OK
;
4553 end_unlock_session_list
:
4554 session_unlock_list();
4558 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4559 const struct lttng_trigger
*trigger
,
4560 struct notification_thread_handle
*notification_thread
)
4562 enum lttng_error_code ret_code
;
4563 const char *trigger_name
;
4564 uid_t trigger_owner
;
4565 enum lttng_trigger_status trigger_status
;
4566 struct lttng_trigger
*sessiond_trigger
= NULL
;
4568 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4569 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4570 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4571 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4573 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4574 trigger_name
, (int) trigger_owner
,
4575 (int) lttng_credentials_get_uid(cmd_creds
));
4578 * Validate the trigger credentials against the command credentials.
4579 * Only the root user can unregister a trigger with non-matching
4582 if (!lttng_credentials_is_equal_uid(
4583 lttng_trigger_get_credentials(trigger
),
4585 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4586 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4587 trigger_name
, (int) trigger_owner
,
4588 (int) lttng_credentials_get_uid(cmd_creds
));
4589 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4594 /* Fetch the sessiond side trigger object. */
4595 ret_code
= notification_thread_command_get_trigger(
4596 notification_thread
, trigger
, &sessiond_trigger
);
4597 if (ret_code
!= LTTNG_OK
) {
4598 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4599 trigger_name
, (int) trigger_owner
, ret_code
);
4603 LTTNG_ASSERT(sessiond_trigger
);
4606 * From this point on, no matter what, consider the trigger
4609 * We set the unregistered state of the sessiond side trigger object in
4610 * the client thread since we want to minimize the possibility of the
4611 * notification thread being stalled due to a long execution of an
4612 * action that required the trigger lock.
4614 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4616 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4618 if (ret_code
!= LTTNG_OK
) {
4619 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4620 trigger_name
, (int) trigger_owner
, ret_code
);
4625 * Synchronize tracers if the trigger removes an event notifier.
4626 * Do this even if the trigger unregistration failed to at least stop
4627 * the tracers from producing notifications associated with this
4630 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4631 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4632 if (ret_code
!= LTTNG_OK
) {
4633 ERR("Error unregistering trigger to tracer.");
4640 lttng_trigger_put(sessiond_trigger
);
4644 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4645 struct notification_thread_handle
*notification_thread
,
4646 struct lttng_triggers
**return_triggers
)
4649 enum lttng_error_code ret_code
;
4650 struct lttng_triggers
*triggers
= NULL
;
4652 /* Get the set of triggers from the notification thread. */
4653 ret_code
= notification_thread_command_list_triggers(
4654 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4655 if (ret_code
!= LTTNG_OK
) {
4659 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4661 ret_code
= LTTNG_ERR_UNK
;
4665 *return_triggers
= triggers
;
4667 ret_code
= LTTNG_OK
;
4669 lttng_triggers_destroy(triggers
);
4673 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4674 const struct lttng_error_query
*query
,
4675 struct lttng_error_query_results
**_results
,
4676 struct notification_thread_handle
*notification_thread
)
4678 enum lttng_error_code ret_code
;
4679 const struct lttng_trigger
*query_target_trigger
;
4680 const struct lttng_action
*query_target_action
= NULL
;
4681 struct lttng_trigger
*matching_trigger
= NULL
;
4682 const char *trigger_name
;
4683 uid_t trigger_owner
;
4684 enum lttng_trigger_status trigger_status
;
4685 struct lttng_error_query_results
*results
= NULL
;
4687 switch (lttng_error_query_get_target_type(query
)) {
4688 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4689 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4691 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4692 query_target_trigger
=
4693 lttng_error_query_condition_borrow_target(query
);
4695 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4696 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4703 LTTNG_ASSERT(query_target_trigger
);
4705 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4706 query_target_trigger
, &matching_trigger
);
4707 if (ret_code
!= LTTNG_OK
) {
4711 /* No longer needed. */
4712 query_target_trigger
= NULL
;
4714 if (lttng_error_query_get_target_type(query
) ==
4715 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4716 /* Get the sessiond-side version of the target action. */
4717 query_target_action
=
4718 lttng_error_query_action_borrow_action_target(
4719 query
, matching_trigger
);
4722 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4723 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4724 trigger_name
: "(anonymous)";
4725 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4727 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4729 results
= lttng_error_query_results_create();
4731 ret_code
= LTTNG_ERR_NOMEM
;
4735 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4736 trigger_name
, (int) trigger_owner
,
4737 (int) lttng_credentials_get_uid(cmd_creds
));
4740 * Validate the trigger credentials against the command credentials.
4741 * Only the root user can target a trigger with non-matching
4744 if (!lttng_credentials_is_equal_uid(
4745 lttng_trigger_get_credentials(matching_trigger
),
4747 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4748 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4749 trigger_name
, (int) trigger_owner
,
4750 (int) lttng_credentials_get_uid(cmd_creds
));
4751 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4756 switch (lttng_error_query_get_target_type(query
)) {
4757 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4758 trigger_status
= lttng_trigger_add_error_results(
4759 matching_trigger
, results
);
4761 switch (trigger_status
) {
4762 case LTTNG_TRIGGER_STATUS_OK
:
4765 ret_code
= LTTNG_ERR_UNK
;
4770 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4772 trigger_status
= lttng_trigger_condition_add_error_results(
4773 matching_trigger
, results
);
4775 switch (trigger_status
) {
4776 case LTTNG_TRIGGER_STATUS_OK
:
4779 ret_code
= LTTNG_ERR_UNK
;
4785 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4787 const enum lttng_action_status action_status
=
4788 lttng_action_add_error_query_results(
4789 query_target_action
, results
);
4791 switch (action_status
) {
4792 case LTTNG_ACTION_STATUS_OK
:
4795 ret_code
= LTTNG_ERR_UNK
;
4806 *_results
= results
;
4808 ret_code
= LTTNG_OK
;
4810 lttng_trigger_put(matching_trigger
);
4811 lttng_error_query_results_destroy(results
);
4816 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4817 * snapshot output is *not* set with a remote destination.
4819 * Return LTTNG_OK on success or a LTTNG_ERR code.
4821 static enum lttng_error_code
set_relayd_for_snapshot(
4822 struct consumer_output
*output
,
4823 const struct ltt_session
*session
)
4825 enum lttng_error_code status
= LTTNG_OK
;
4826 struct lttng_ht_iter iter
;
4827 struct consumer_socket
*socket
;
4828 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4829 const char *base_path
;
4831 LTTNG_ASSERT(output
);
4832 LTTNG_ASSERT(session
);
4834 DBG2("Set relayd object from snapshot output");
4836 if (session
->current_trace_chunk
) {
4837 enum lttng_trace_chunk_status chunk_status
=
4838 lttng_trace_chunk_get_id(
4839 session
->current_trace_chunk
,
4840 ¤t_chunk_id
.value
);
4842 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4843 current_chunk_id
.is_set
= true;
4845 ERR("Failed to get current trace chunk id");
4846 status
= LTTNG_ERR_UNK
;
4851 /* Ignore if snapshot consumer output is not network. */
4852 if (output
->type
!= CONSUMER_DST_NET
) {
4857 * The snapshot record URI base path overrides the session
4860 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4861 base_path
= output
->dst
.net
.control
.subdir
;
4863 base_path
= session
->base_path
;
4867 * For each consumer socket, create and send the relayd object of the
4871 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4872 socket
, node
.node
) {
4873 pthread_mutex_lock(socket
->lock
);
4874 status
= send_consumer_relayd_sockets(LTTNG_DOMAIN_NONE
, session
->id
,
4876 session
->name
, session
->hostname
,
4878 session
->live_timer
,
4879 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4880 session
->creation_time
,
4881 session
->name_contains_creation_time
);
4882 pthread_mutex_unlock(socket
->lock
);
4883 if (status
!= LTTNG_OK
) {
4895 * Record a kernel snapshot.
4897 * Return LTTNG_OK on success or a LTTNG_ERR code.
4899 static enum lttng_error_code
record_kernel_snapshot(
4900 struct ltt_kernel_session
*ksess
,
4901 const struct consumer_output
*output
,
4902 const struct ltt_session
*session
,
4903 int wait
, uint64_t nb_packets_per_stream
)
4905 enum lttng_error_code status
;
4907 LTTNG_ASSERT(ksess
);
4908 LTTNG_ASSERT(output
);
4909 LTTNG_ASSERT(session
);
4911 status
= kernel_snapshot_record(
4912 ksess
, output
, wait
, nb_packets_per_stream
);
4917 * Record a UST snapshot.
4919 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4921 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4922 const struct consumer_output
*output
,
4923 const struct ltt_session
*session
,
4924 int wait
, uint64_t nb_packets_per_stream
)
4926 enum lttng_error_code status
;
4928 LTTNG_ASSERT(usess
);
4929 LTTNG_ASSERT(output
);
4930 LTTNG_ASSERT(session
);
4932 status
= ust_app_snapshot_record(
4933 usess
, output
, wait
, nb_packets_per_stream
);
4938 uint64_t get_session_size_one_more_packet_per_stream(
4939 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4941 uint64_t tot_size
= 0;
4943 if (session
->kernel_session
) {
4944 struct ltt_kernel_channel
*chan
;
4945 const struct ltt_kernel_session
*ksess
=
4946 session
->kernel_session
;
4948 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4949 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4951 * Don't take channel into account if we
4952 * already grab all its packets.
4956 tot_size
+= chan
->channel
->attr
.subbuf_size
4957 * chan
->stream_count
;
4961 if (session
->ust_session
) {
4962 const struct ltt_ust_session
*usess
= session
->ust_session
;
4964 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4972 * Calculate the number of packets we can grab from each stream that
4973 * fits within the overall snapshot max size.
4975 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4976 * the number of packets per stream.
4978 * TODO: this approach is not perfect: we consider the worse case
4979 * (packet filling the sub-buffers) as an upper bound, but we could do
4980 * better if we do this calculation while we actually grab the packet
4981 * content: we would know how much padding we don't actually store into
4984 * This algorithm is currently bounded by the number of packets per
4987 * Since we call this algorithm before actually grabbing the data, it's
4988 * an approximation: for instance, applications could appear/disappear
4989 * in between this call and actually grabbing data.
4992 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4996 uint64_t cur_nb_packets
= 0;
4999 return 0; /* Infinite */
5002 size_left
= max_size
;
5004 uint64_t one_more_packet_tot_size
;
5006 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5007 session
, cur_nb_packets
);
5008 if (!one_more_packet_tot_size
) {
5009 /* We are already grabbing all packets. */
5012 size_left
-= one_more_packet_tot_size
;
5013 if (size_left
< 0) {
5018 if (!cur_nb_packets
&& size_left
!= max_size
) {
5019 /* Not enough room to grab one packet of each stream, error. */
5022 return cur_nb_packets
;
5026 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5027 const struct snapshot_output
*snapshot_output
, int wait
)
5029 int64_t nb_packets_per_stream
;
5030 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5032 enum lttng_error_code ret_code
= LTTNG_OK
;
5033 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5034 struct consumer_output
*original_ust_consumer_output
= NULL
;
5035 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5036 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5037 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5039 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5041 snapshot_output
->name
,
5042 snapshot_output
->datetime
,
5043 snapshot_output
->nb_snapshot
);
5044 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5045 ERR("Failed to format snapshot name");
5046 ret_code
= LTTNG_ERR_INVALID
;
5049 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5050 snapshot_output
->name
, session
->name
,
5051 snapshot_chunk_name
);
5052 if (!session
->kernel_session
&& !session
->ust_session
) {
5053 ERR("Failed to record snapshot as no channels exist");
5054 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5058 if (session
->kernel_session
) {
5059 original_kernel_consumer_output
=
5060 session
->kernel_session
->consumer
;
5061 snapshot_kernel_consumer_output
=
5062 consumer_copy_output(snapshot_output
->consumer
);
5063 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5064 snapshot_chunk_name
);
5066 /* Copy the original domain subdir. */
5067 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5068 original_kernel_consumer_output
->domain_subdir
);
5070 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5071 original_kernel_consumer_output
);
5073 ERR("Failed to copy consumer sockets from snapshot output configuration");
5074 ret_code
= LTTNG_ERR_NOMEM
;
5077 ret_code
= set_relayd_for_snapshot(
5078 snapshot_kernel_consumer_output
, session
);
5079 if (ret_code
!= LTTNG_OK
) {
5080 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5083 session
->kernel_session
->consumer
=
5084 snapshot_kernel_consumer_output
;
5086 if (session
->ust_session
) {
5087 original_ust_consumer_output
= session
->ust_session
->consumer
;
5088 snapshot_ust_consumer_output
=
5089 consumer_copy_output(snapshot_output
->consumer
);
5090 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5091 snapshot_chunk_name
);
5093 /* Copy the original domain subdir. */
5094 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5095 original_ust_consumer_output
->domain_subdir
);
5097 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5098 original_ust_consumer_output
);
5100 ERR("Failed to copy consumer sockets from snapshot output configuration");
5101 ret_code
= LTTNG_ERR_NOMEM
;
5104 ret_code
= set_relayd_for_snapshot(
5105 snapshot_ust_consumer_output
, session
);
5106 if (ret_code
!= LTTNG_OK
) {
5107 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5110 session
->ust_session
->consumer
=
5111 snapshot_ust_consumer_output
;
5114 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5115 snapshot_kernel_consumer_output
?:
5116 snapshot_ust_consumer_output
,
5117 consumer_output_get_base_path(
5118 snapshot_output
->consumer
),
5119 snapshot_chunk_name
);
5120 if (!snapshot_trace_chunk
) {
5121 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5123 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5126 LTTNG_ASSERT(!session
->current_trace_chunk
);
5127 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5128 lttng_trace_chunk_put(snapshot_trace_chunk
);
5129 snapshot_trace_chunk
= NULL
;
5131 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5133 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5137 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5138 snapshot_output
->max_size
);
5139 if (nb_packets_per_stream
< 0) {
5140 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5141 goto error_close_trace_chunk
;
5144 if (session
->kernel_session
) {
5145 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5146 snapshot_kernel_consumer_output
, session
,
5147 wait
, nb_packets_per_stream
);
5148 if (ret_code
!= LTTNG_OK
) {
5149 goto error_close_trace_chunk
;
5153 if (session
->ust_session
) {
5154 ret_code
= record_ust_snapshot(session
->ust_session
,
5155 snapshot_ust_consumer_output
, session
,
5156 wait
, nb_packets_per_stream
);
5157 if (ret_code
!= LTTNG_OK
) {
5158 goto error_close_trace_chunk
;
5162 error_close_trace_chunk
:
5163 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5164 ERR("Failed to release the current trace chunk of session \"%s\"",
5166 ret_code
= LTTNG_ERR_UNK
;
5169 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5170 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5172 * Don't goto end; make sure the chunk is closed for the session
5173 * to allow future snapshots.
5175 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5177 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5180 if (original_ust_consumer_output
) {
5181 session
->ust_session
->consumer
= original_ust_consumer_output
;
5183 if (original_kernel_consumer_output
) {
5184 session
->kernel_session
->consumer
=
5185 original_kernel_consumer_output
;
5187 consumer_output_put(snapshot_ust_consumer_output
);
5188 consumer_output_put(snapshot_kernel_consumer_output
);
5193 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5195 * The wait parameter is ignored so this call always wait for the snapshot to
5196 * complete before returning.
5198 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5200 int cmd_snapshot_record(struct ltt_session
*session
,
5201 const struct lttng_snapshot_output
*output
, int wait
)
5203 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5205 unsigned int snapshot_success
= 0;
5207 struct snapshot_output
*tmp_output
= NULL
;
5209 LTTNG_ASSERT(session
);
5210 LTTNG_ASSERT(output
);
5212 DBG("Cmd snapshot record for session %s", session
->name
);
5214 /* Get the datetime for the snapshot output directory. */
5215 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5218 cmd_ret
= LTTNG_ERR_INVALID
;
5223 * Permission denied to create an output if the session is not
5224 * set in no output mode.
5226 if (session
->output_traces
) {
5227 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5231 /* The session needs to be started at least once. */
5232 if (!session
->has_been_started
) {
5233 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5237 /* Use temporary output for the session. */
5238 if (*output
->ctrl_url
!= '\0') {
5239 tmp_output
= snapshot_output_alloc();
5241 cmd_ret
= LTTNG_ERR_NOMEM
;
5245 ret
= snapshot_output_init(session
, output
->max_size
,
5247 output
->ctrl_url
, output
->data_url
,
5251 if (ret
== -ENOMEM
) {
5252 cmd_ret
= LTTNG_ERR_NOMEM
;
5254 cmd_ret
= LTTNG_ERR_INVALID
;
5258 /* Use the global session count for the temporary snapshot. */
5259 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5261 /* Use the global datetime */
5262 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5263 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
5264 if (cmd_ret
!= LTTNG_OK
) {
5267 snapshot_success
= 1;
5269 struct snapshot_output
*sout
;
5270 struct lttng_ht_iter iter
;
5273 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5274 &iter
.iter
, sout
, node
.node
) {
5275 struct snapshot_output output_copy
;
5278 * Make a local copy of the output and override output
5279 * parameters with those provided as part of the
5282 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5284 if (output
->max_size
!= (uint64_t) -1ULL) {
5285 output_copy
.max_size
= output
->max_size
;
5288 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5289 memcpy(output_copy
.datetime
, datetime
,
5292 /* Use temporary name. */
5293 if (*output
->name
!= '\0') {
5294 if (lttng_strncpy(output_copy
.name
,
5296 sizeof(output_copy
.name
))) {
5297 cmd_ret
= LTTNG_ERR_INVALID
;
5303 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
5304 if (cmd_ret
!= LTTNG_OK
) {
5308 snapshot_success
= 1;
5313 if (snapshot_success
) {
5314 session
->snapshot
.nb_snapshot
++;
5316 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5321 snapshot_output_destroy(tmp_output
);
5327 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5329 int cmd_set_session_shm_path(struct ltt_session
*session
,
5330 const char *shm_path
)
5333 LTTNG_ASSERT(session
);
5336 * Can only set shm path before session is started.
5338 if (session
->has_been_started
) {
5339 return LTTNG_ERR_SESSION_STARTED
;
5342 strncpy(session
->shm_path
, shm_path
,
5343 sizeof(session
->shm_path
));
5344 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5350 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5352 * Ask the consumer to rotate the session output directory.
5353 * The session lock must be held.
5355 * Returns LTTNG_OK on success or else a negative LTTng error code.
5357 int cmd_rotate_session(struct ltt_session
*session
,
5358 struct lttng_rotate_session_return
*rotate_return
,
5359 bool quiet_rotation
,
5360 enum lttng_trace_chunk_command_type command
)
5363 uint64_t ongoing_rotation_chunk_id
;
5364 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5365 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5366 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5367 enum lttng_trace_chunk_status chunk_status
;
5368 bool failed_to_rotate
= false;
5369 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5371 LTTNG_ASSERT(session
);
5373 if (!session
->has_been_started
) {
5374 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5379 * Explicit rotation is not supported for live sessions.
5380 * However, live sessions can perform a quiet rotation on
5382 * Rotation is not supported for snapshot traces (no output).
5384 if ((!quiet_rotation
&& session
->live_timer
) ||
5385 !session
->output_traces
) {
5386 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5390 /* Unsupported feature in lttng-relayd before 2.11. */
5391 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5392 (session
->consumer
->relay_major_version
== 2 &&
5393 session
->consumer
->relay_minor_version
< 11)) {
5394 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5398 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5399 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5400 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5404 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5405 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5407 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5412 * After a stop, we only allow one rotation to occur, the other ones are
5413 * useless until a new start.
5415 if (session
->rotated_after_last_stop
) {
5416 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5418 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5423 * After a stop followed by a clear, disallow following rotations a they would
5424 * generate empty chunks.
5426 if (session
->cleared_after_last_stop
) {
5427 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5429 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5433 if (session
->active
) {
5434 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5436 if (!new_trace_chunk
) {
5437 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5443 * The current trace chunk becomes the chunk being archived.
5445 * After this point, "chunk_being_archived" must absolutely
5446 * be closed on the consumer(s), otherwise it will never be
5447 * cleaned-up, which will result in a leak.
5449 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5450 &chunk_being_archived
);
5452 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5456 if (session
->kernel_session
) {
5457 cmd_ret
= kernel_rotate_session(session
);
5458 if (cmd_ret
!= LTTNG_OK
) {
5459 failed_to_rotate
= true;
5460 rotation_fail_code
= cmd_ret
;
5463 if (session
->ust_session
) {
5464 cmd_ret
= ust_app_rotate_session(session
);
5465 if (cmd_ret
!= LTTNG_OK
) {
5466 failed_to_rotate
= true;
5467 rotation_fail_code
= cmd_ret
;
5471 if (!session
->active
) {
5472 session
->rotated_after_last_stop
= true;
5475 if (!chunk_being_archived
) {
5476 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5478 if (failed_to_rotate
) {
5479 cmd_ret
= rotation_fail_code
;
5486 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5487 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5488 &ongoing_rotation_chunk_id
);
5489 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5491 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5492 command
, session
->last_chunk_path
);
5494 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5498 if (failed_to_rotate
) {
5499 cmd_ret
= rotation_fail_code
;
5503 session
->quiet_rotation
= quiet_rotation
;
5504 ret
= timer_session_rotation_pending_check_start(session
,
5505 DEFAULT_ROTATE_PENDING_TIMER
);
5507 cmd_ret
= LTTNG_ERR_UNK
;
5511 if (rotate_return
) {
5512 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5515 session
->chunk_being_archived
= chunk_being_archived
;
5516 chunk_being_archived
= NULL
;
5517 if (!quiet_rotation
) {
5518 ret
= notification_thread_command_session_rotation_ongoing(
5519 the_notification_thread_handle
, session
->name
,
5520 session
->uid
, session
->gid
,
5521 ongoing_rotation_chunk_id
);
5522 if (ret
!= LTTNG_OK
) {
5523 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5525 cmd_ret
= (lttng_error_code
) ret
;
5529 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5530 session
->name
, ongoing_rotation_chunk_id
);
5532 lttng_trace_chunk_put(new_trace_chunk
);
5533 lttng_trace_chunk_put(chunk_being_archived
);
5534 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5537 if (session_reset_rotation_state(session
,
5538 LTTNG_ROTATION_STATE_ERROR
)) {
5539 ERR("Failed to reset rotation state of session \"%s\"",
5546 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5548 * Check if the session has finished its rotation.
5550 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5552 int cmd_rotate_get_info(struct ltt_session
*session
,
5553 struct lttng_rotation_get_info_return
*info_return
,
5554 uint64_t rotation_id
)
5556 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5557 enum lttng_rotation_state rotation_state
;
5559 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5560 session
->most_recent_chunk_id
.value
);
5562 if (session
->chunk_being_archived
) {
5563 enum lttng_trace_chunk_status chunk_status
;
5566 chunk_status
= lttng_trace_chunk_get_id(
5567 session
->chunk_being_archived
,
5569 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5571 rotation_state
= rotation_id
== chunk_id
?
5572 LTTNG_ROTATION_STATE_ONGOING
:
5573 LTTNG_ROTATION_STATE_EXPIRED
;
5575 if (session
->last_archived_chunk_id
.is_set
&&
5576 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5577 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5579 rotation_state
= session
->rotation_state
;
5583 switch (rotation_state
) {
5584 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5585 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5588 case LTTNG_ROTATION_STATE_EXPIRED
:
5589 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5590 rotation_id
, session
->name
);
5592 case LTTNG_ROTATION_STATE_ONGOING
:
5593 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5594 rotation_id
, session
->name
);
5596 case LTTNG_ROTATION_STATE_COMPLETED
:
5600 char *current_tracing_path_reply
;
5601 size_t current_tracing_path_reply_len
;
5603 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5604 rotation_id
, session
->name
);
5606 switch (session_get_consumer_destination_type(session
)) {
5607 case CONSUMER_DST_LOCAL
:
5608 current_tracing_path_reply
=
5609 info_return
->location
.local
.absolute_path
;
5610 current_tracing_path_reply_len
=
5611 sizeof(info_return
->location
.local
.absolute_path
);
5612 info_return
->location_type
=
5613 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5614 fmt_ret
= asprintf(&chunk_path
,
5615 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5616 session_get_base_path(session
),
5617 session
->last_archived_chunk_name
);
5618 if (fmt_ret
== -1) {
5619 PERROR("Failed to format the path of the last archived trace chunk");
5620 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5621 cmd_ret
= LTTNG_ERR_UNK
;
5625 case CONSUMER_DST_NET
:
5627 uint16_t ctrl_port
, data_port
;
5629 current_tracing_path_reply
=
5630 info_return
->location
.relay
.relative_path
;
5631 current_tracing_path_reply_len
=
5632 sizeof(info_return
->location
.relay
.relative_path
);
5633 /* Currently the only supported relay protocol. */
5634 info_return
->location
.relay
.protocol
=
5635 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5637 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5638 session_get_net_consumer_hostname(session
),
5639 sizeof(info_return
->location
.relay
.host
));
5641 ERR("Failed to copy host name to rotate_get_info reply");
5642 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5643 cmd_ret
= LTTNG_ERR_SET_URL
;
5647 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5648 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5649 info_return
->location
.relay
.ports
.data
= data_port
;
5650 info_return
->location_type
=
5651 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5652 chunk_path
= strdup(session
->last_chunk_path
);
5654 ERR("Failed to allocate the path of the last archived trace chunk");
5655 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5656 cmd_ret
= LTTNG_ERR_UNK
;
5665 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5666 chunk_path
, current_tracing_path_reply_len
);
5669 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5670 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5671 cmd_ret
= LTTNG_ERR_UNK
;
5677 case LTTNG_ROTATION_STATE_ERROR
:
5678 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5679 rotation_id
, session
->name
);
5687 info_return
->status
= (int32_t) rotation_state
;
5692 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5694 * Configure the automatic rotation parameters.
5695 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5696 * 'activate' to false means deactivate the rotation schedule and validate that
5697 * 'new_value' has the same value as the currently active value.
5699 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5701 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5702 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5704 struct notification_thread_handle
*notification_thread_handle
)
5707 uint64_t *parameter_value
;
5709 LTTNG_ASSERT(session
);
5711 DBG("Cmd rotate set schedule session %s", session
->name
);
5713 if (session
->live_timer
|| !session
->output_traces
) {
5714 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5715 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5719 switch (schedule_type
) {
5720 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5721 parameter_value
= &session
->rotate_size
;
5723 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5724 parameter_value
= &session
->rotate_timer_period
;
5725 if (new_value
>= UINT_MAX
) {
5726 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5727 new_value
, UINT_MAX
);
5728 ret
= LTTNG_ERR_INVALID
;
5733 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5734 ret
= LTTNG_ERR_INVALID
;
5738 /* Improper use of the API. */
5739 if (new_value
== -1ULL) {
5740 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5741 ret
= LTTNG_ERR_INVALID
;
5746 * As indicated in struct ltt_session's comments, a value of == 0 means
5747 * this schedule rotation type is not in use.
5749 * Reject the command if we were asked to activate a schedule that was
5752 if (activate
&& *parameter_value
!= 0) {
5753 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5754 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5759 * Reject the command if we were asked to deactivate a schedule that was
5762 if (!activate
&& *parameter_value
== 0) {
5763 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5764 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5769 * Reject the command if we were asked to deactivate a schedule that
5772 if (!activate
&& *parameter_value
!= new_value
) {
5773 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5774 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5778 *parameter_value
= activate
? new_value
: 0;
5780 switch (schedule_type
) {
5781 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5782 if (activate
&& session
->active
) {
5784 * Only start the timer if the session is active,
5785 * otherwise it will be started when the session starts.
5787 ret
= timer_session_rotation_schedule_timer_start(
5788 session
, new_value
);
5790 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5791 ret
= LTTNG_ERR_UNK
;
5795 ret
= timer_session_rotation_schedule_timer_stop(
5798 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5799 ret
= LTTNG_ERR_UNK
;
5804 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5806 ret
= subscribe_session_consumed_size_rotation(session
,
5807 new_value
, notification_thread_handle
);
5809 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5810 ret
= LTTNG_ERR_UNK
;
5814 ret
= unsubscribe_session_consumed_size_rotation(session
,
5815 notification_thread_handle
);
5817 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5818 ret
= LTTNG_ERR_UNK
;
5825 /* Would have been caught before. */
5837 /* Wait for a given path to be removed before continuing. */
5838 static enum lttng_error_code
wait_on_path(void *path_data
)
5840 const char *shm_path
= (const char *) path_data
;
5842 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5848 ret
= stat(shm_path
, &st
);
5850 if (errno
!= ENOENT
) {
5851 PERROR("stat() returned an error while checking for the existence of the shm path");
5853 DBG("shm path no longer exists, completing the destruction of session");
5857 if (!S_ISDIR(st
.st_mode
)) {
5858 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5863 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5869 * Returns a pointer to a handler to run on completion of a command.
5870 * Returns NULL if no handler has to be run for the last command executed.
5872 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5874 struct cmd_completion_handler
*handler
= current_completion_handler
;
5876 current_completion_handler
= NULL
;
5881 * Init command subsystem.
5886 * Set network sequence index to 1 for streams to match a relayd
5887 * socket on the consumer side.
5889 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5890 relayd_net_seq_idx
= 1;
5891 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5893 DBG("Command subsystem initialized");