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
14 #include <urcu/list.h>
15 #include <urcu/uatomic.h>
17 #include <common/buffer-view.h>
18 #include <common/common.h>
19 #include <common/compat/string.h>
20 #include <common/defaults.h>
21 #include <common/dynamic-buffer.h>
22 #include <common/kernel-ctl/kernel-ctl.h>
23 #include <common/payload-view.h>
24 #include <common/payload.h>
25 #include <common/relayd/relayd.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27 #include <common/string-utils/string-utils.h>
28 #include <common/trace-chunk.h>
29 #include <common/utils.h>
30 #include <lttng/action/action-internal.h>
31 #include <lttng/action/action.h>
32 #include <lttng/channel-internal.h>
33 #include <lttng/channel.h>
34 #include <lttng/condition/condition-internal.h>
35 #include <lttng/condition/condition.h>
36 #include <lttng/condition/event-rule-matches-internal.h>
37 #include <lttng/condition/event-rule-matches.h>
38 #include <lttng/error-query-internal.h>
39 #include <lttng/event-rule/event-rule-internal.h>
40 #include <lttng/event-rule/event-rule.h>
41 #include <lttng/location-internal.h>
42 #include <lttng/lttng-error.h>
43 #include <lttng/rotate-internal.h>
44 #include <lttng/session-descriptor-internal.h>
45 #include <lttng/session-internal.h>
46 #include <lttng/tracker.h>
47 #include <lttng/trigger/trigger-internal.h>
48 #include <lttng/userspace-probe-internal.h>
50 #include "agent-thread.h"
52 #include "buffer-registry.h"
56 #include "event-notifier-error-accounting.h"
58 #include "health-sessiond.h"
59 #include "kernel-consumer.h"
61 #include "lttng-sessiond.h"
62 #include "lttng-syscall.h"
63 #include "notification-thread-commands.h"
64 #include "notification-thread.h"
66 #include "rotation-thread.h"
72 /* Sleep for 100ms between each check for the shm path's deletion. */
73 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
75 struct cmd_destroy_session_reply_context
{
77 bool implicit_rotation_on_destroy
;
79 * Indicates whether or not an error occurred while launching the
80 * destruction of a session.
82 enum lttng_error_code destruction_status
;
85 static enum lttng_error_code
wait_on_path(void *path
);
88 * Command completion handler that is used by the destroy command
89 * when a session that has a non-default shm_path is being destroyed.
91 * See comment in cmd_destroy_session() for the rationale.
93 static struct destroy_completion_handler
{
94 struct cmd_completion_handler handler
;
95 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
96 } destroy_completion_handler
= {
99 .data
= destroy_completion_handler
.shm_path
104 static struct cmd_completion_handler
*current_completion_handler
;
107 * Used to keep a unique index for each relayd socket created where this value
108 * is associated with streams on the consumer so it can match the right relayd
109 * to send to. It must be accessed with the relayd_net_seq_idx_lock
112 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
113 static uint64_t relayd_net_seq_idx
;
115 static int validate_ust_event_name(const char *);
116 static int cmd_enable_event_internal(struct ltt_session
*session
,
117 const struct lttng_domain
*domain
,
118 char *channel_name
, struct lttng_event
*event
,
119 char *filter_expression
,
120 struct lttng_bytecode
*filter
,
121 struct lttng_event_exclusion
*exclusion
,
125 * Create a session path used by list_lttng_sessions for the case that the
126 * session consumer is on the network.
128 static int build_network_session_path(char *dst
, size_t size
,
129 struct ltt_session
*session
)
131 int ret
, kdata_port
, udata_port
;
132 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
133 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
135 LTTNG_ASSERT(session
);
138 memset(tmp_urls
, 0, sizeof(tmp_urls
));
139 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
141 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
143 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
144 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
145 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
148 if (session
->ust_session
&& session
->ust_session
->consumer
) {
149 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
150 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
153 if (uuri
== NULL
&& kuri
== NULL
) {
154 uri
= &session
->consumer
->dst
.net
.control
;
155 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
156 } else if (kuri
&& uuri
) {
157 ret
= uri_compare(kuri
, uuri
);
161 /* Build uuri URL string */
162 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
169 } else if (kuri
&& uuri
== NULL
) {
171 } else if (uuri
&& kuri
== NULL
) {
175 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
181 * Do we have a UST url set. If yes, this means we have both kernel and UST
184 if (*tmp_uurl
!= '\0') {
185 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
186 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
189 if (kuri
|| (!kuri
&& !uuri
)) {
192 /* No kernel URI, use the UST port. */
195 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
203 * Get run-time attributes if the session has been started (discarded events,
206 static int get_kernel_runtime_stats(struct ltt_session
*session
,
207 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
208 uint64_t *lost_packets
)
212 if (!session
->has_been_started
) {
214 *discarded_events
= 0;
219 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
220 session
->kernel_session
->consumer
,
226 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
227 session
->kernel_session
->consumer
,
238 * Get run-time attributes if the session has been started (discarded events,
241 static int get_ust_runtime_stats(struct ltt_session
*session
,
242 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
243 uint64_t *lost_packets
)
246 struct ltt_ust_session
*usess
;
248 if (!discarded_events
|| !lost_packets
) {
253 usess
= session
->ust_session
;
254 LTTNG_ASSERT(discarded_events
);
255 LTTNG_ASSERT(lost_packets
);
257 if (!usess
|| !session
->has_been_started
) {
258 *discarded_events
= 0;
264 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
265 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
266 &usess
->buffer_reg_uid_list
,
267 usess
->consumer
, uchan
->id
,
268 uchan
->attr
.overwrite
,
271 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
272 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
273 uchan
, usess
->consumer
,
274 uchan
->attr
.overwrite
,
280 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
281 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
283 ERR("Unsupported buffer type");
294 * Fill lttng_channel array of all channels.
296 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
297 struct ltt_session
*session
, struct lttng_channel
*channels
,
298 struct lttng_channel_extended
*chan_exts
)
301 struct ltt_kernel_channel
*kchan
;
303 DBG("Listing channels for session %s", session
->name
);
306 case LTTNG_DOMAIN_KERNEL
:
307 /* Kernel channels */
308 if (session
->kernel_session
!= NULL
) {
309 cds_list_for_each_entry(kchan
,
310 &session
->kernel_session
->channel_list
.head
, list
) {
311 uint64_t discarded_events
, lost_packets
;
312 struct lttng_channel_extended
*extended
;
314 extended
= (struct lttng_channel_extended
*)
315 kchan
->channel
->attr
.extended
.ptr
;
317 ret
= get_kernel_runtime_stats(session
, kchan
,
318 &discarded_events
, &lost_packets
);
322 /* Copy lttng_channel struct to array */
323 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
324 channels
[i
].enabled
= kchan
->enabled
;
325 chan_exts
[i
].discarded_events
=
327 chan_exts
[i
].lost_packets
= lost_packets
;
328 chan_exts
[i
].monitor_timer_interval
=
329 extended
->monitor_timer_interval
;
330 chan_exts
[i
].blocking_timeout
= 0;
335 case LTTNG_DOMAIN_UST
:
337 struct lttng_ht_iter iter
;
338 struct ltt_ust_channel
*uchan
;
341 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
342 &iter
.iter
, uchan
, node
.node
) {
343 uint64_t discarded_events
= 0, lost_packets
= 0;
345 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
346 LTTNG_SYMBOL_NAME_LEN
)) {
349 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
350 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
351 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
352 channels
[i
].attr
.switch_timer_interval
=
353 uchan
->attr
.switch_timer_interval
;
354 channels
[i
].attr
.read_timer_interval
=
355 uchan
->attr
.read_timer_interval
;
356 channels
[i
].enabled
= uchan
->enabled
;
357 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
358 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
361 * Map enum lttng_ust_output to enum lttng_event_output.
363 switch (uchan
->attr
.output
) {
364 case LTTNG_UST_ABI_MMAP
:
365 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
369 * LTTNG_UST_MMAP is the only supported UST
376 chan_exts
[i
].monitor_timer_interval
=
377 uchan
->monitor_timer_interval
;
378 chan_exts
[i
].blocking_timeout
=
379 uchan
->attr
.u
.s
.blocking_timeout
;
381 ret
= get_ust_runtime_stats(session
, uchan
,
382 &discarded_events
, &lost_packets
);
386 chan_exts
[i
].discarded_events
= discarded_events
;
387 chan_exts
[i
].lost_packets
= lost_packets
;
399 return -LTTNG_ERR_FATAL
;
405 static int append_extended_info(const char *filter_expression
,
406 struct lttng_event_exclusion
*exclusion
,
407 struct lttng_userspace_probe_location
*probe_location
,
408 struct lttng_payload
*payload
)
411 size_t filter_len
= 0;
412 size_t nb_exclusions
= 0;
413 size_t userspace_probe_location_len
= 0;
414 struct lttcomm_event_extended_header extended_header
= {};
415 struct lttcomm_event_extended_header
*p_extended_header
;
416 const size_t original_payload_size
= payload
->buffer
.size
;
418 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &extended_header
,
419 sizeof(extended_header
));
424 if (filter_expression
) {
425 filter_len
= strlen(filter_expression
) + 1;
426 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
427 filter_expression
, filter_len
);
434 const size_t len
= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
436 nb_exclusions
= exclusion
->count
;
438 ret
= lttng_dynamic_buffer_append(
439 &payload
->buffer
, &exclusion
->names
, len
);
445 if (probe_location
) {
446 const size_t size_before_probe
= payload
->buffer
.size
;
448 ret
= lttng_userspace_probe_location_serialize(probe_location
,
455 userspace_probe_location_len
=
456 payload
->buffer
.size
- size_before_probe
;
459 /* Set header fields */
460 p_extended_header
= (struct lttcomm_event_extended_header
*)
461 (payload
->buffer
.data
+ original_payload_size
);
463 p_extended_header
->filter_len
= filter_len
;
464 p_extended_header
->nb_exclusions
= nb_exclusions
;
465 p_extended_header
->userspace_probe_location_len
=
466 userspace_probe_location_len
;
474 * Create a list of agent domain events.
476 * Return number of events in list on success or else a negative value.
478 static int list_lttng_agent_events(struct agent
*agt
,
479 struct lttng_payload
*payload
)
481 int nb_events
= 0, ret
= 0;
482 const struct agent_event
*agent_event
;
483 struct lttng_ht_iter iter
;
487 DBG3("Listing agent events");
490 cds_lfht_for_each_entry (
491 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
492 struct lttng_event event
= {
493 .enabled
= AGENT_EVENT_IS_ENABLED(agent_event
),
494 .loglevel
= agent_event
->loglevel_value
,
495 .loglevel_type
= agent_event
->loglevel_type
,
498 ret
= lttng_strncpy(event
.name
, agent_event
->name
, sizeof(event
.name
));
500 /* Internal error, invalid name. */
501 ERR("Invalid event name while listing agent events: '%s' exceeds the maximal allowed length of %zu bytes",
502 agent_event
->name
, sizeof(event
.name
));
503 ret
= -LTTNG_ERR_UNK
;
507 ret
= lttng_dynamic_buffer_append(
508 &payload
->buffer
, &event
, sizeof(event
));
510 ERR("Failed to append event to payload");
511 ret
= -LTTNG_ERR_NOMEM
;
518 cds_lfht_for_each_entry (
519 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
520 /* Append extended info. */
521 ret
= append_extended_info(agent_event
->filter_expression
, NULL
,
524 ERR("Failed to append extended event info to payload");
525 ret
= -LTTNG_ERR_NOMEM
;
537 * Create a list of ust global domain events.
539 static int list_lttng_ust_global_events(char *channel_name
,
540 struct ltt_ust_domain_global
*ust_global
,
541 struct lttng_payload
*payload
)
544 unsigned int nb_events
= 0;
545 struct lttng_ht_iter iter
;
546 const struct lttng_ht_node_str
*node
;
547 const struct ltt_ust_channel
*uchan
;
548 const struct ltt_ust_event
*uevent
;
550 DBG("Listing UST global events for channel %s", channel_name
);
554 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
555 node
= lttng_ht_iter_get_node_str(&iter
);
557 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
561 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
563 DBG3("Listing UST global events");
565 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
566 struct lttng_event event
= {};
568 if (uevent
->internal
) {
572 ret
= lttng_strncpy(event
.name
, uevent
->attr
.name
, sizeof(event
.name
));
574 /* Internal error, invalid name. */
575 ERR("Invalid event name while listing user space tracer events: '%s' exceeds the maximal allowed length of %zu bytes",
576 uevent
->attr
.name
, sizeof(event
.name
));
577 ret
= -LTTNG_ERR_UNK
;
581 event
.enabled
= uevent
->enabled
;
583 switch (uevent
->attr
.instrumentation
) {
584 case LTTNG_UST_ABI_TRACEPOINT
:
585 event
.type
= LTTNG_EVENT_TRACEPOINT
;
587 case LTTNG_UST_ABI_PROBE
:
588 event
.type
= LTTNG_EVENT_PROBE
;
590 case LTTNG_UST_ABI_FUNCTION
:
591 event
.type
= LTTNG_EVENT_FUNCTION
;
595 event
.loglevel
= uevent
->attr
.loglevel
;
596 switch (uevent
->attr
.loglevel_type
) {
597 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
598 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
600 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
601 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
603 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
604 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
608 if (uevent
->filter
) {
612 if (uevent
->exclusion
) {
616 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &event
, sizeof(event
));
618 ERR("Failed to append event to payload");
619 ret
= -LTTNG_ERR_NOMEM
;
626 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
627 /* Append extended info. */
628 ret
= append_extended_info(uevent
->filter_expression
,
629 uevent
->exclusion
, NULL
, payload
);
631 ERR("Failed to append extended event info to payload");
632 ret
= -LTTNG_ERR_FATAL
;
644 * Fill lttng_event array of all kernel events in the channel.
646 static int list_lttng_kernel_events(char *channel_name
,
647 struct ltt_kernel_session
*kernel_session
,
648 struct lttng_payload
*payload
)
651 unsigned int nb_event
;
652 const struct ltt_kernel_event
*kevent
;
653 const struct ltt_kernel_channel
*kchan
;
655 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
657 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
661 nb_event
= kchan
->event_count
;
663 DBG("Listing events for channel %s", kchan
->channel
->name
);
665 /* Kernel channels */
666 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
667 struct lttng_event event
= {};
669 ret
= lttng_strncpy(event
.name
, kevent
->event
->name
, sizeof(event
.name
));
671 /* Internal error, invalid name. */
672 ERR("Invalid event name while listing kernel events: '%s' exceeds the maximal allowed length of %zu bytes",
675 ret
= -LTTNG_ERR_UNK
;
679 event
.enabled
= kevent
->enabled
;
680 event
.filter
= (unsigned char) !!kevent
->filter_expression
;
682 switch (kevent
->event
->instrumentation
) {
683 case LTTNG_KERNEL_ABI_TRACEPOINT
:
684 event
.type
= LTTNG_EVENT_TRACEPOINT
;
686 case LTTNG_KERNEL_ABI_KRETPROBE
:
687 event
.type
= LTTNG_EVENT_FUNCTION
;
688 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
689 sizeof(struct lttng_kernel_abi_kprobe
));
691 case LTTNG_KERNEL_ABI_KPROBE
:
692 event
.type
= LTTNG_EVENT_PROBE
;
693 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
694 sizeof(struct lttng_kernel_abi_kprobe
));
696 case LTTNG_KERNEL_ABI_UPROBE
:
697 event
.type
= LTTNG_EVENT_USERSPACE_PROBE
;
699 case LTTNG_KERNEL_ABI_FUNCTION
:
700 event
.type
= LTTNG_EVENT_FUNCTION
;
701 memcpy(&event
.attr
.ftrace
, &kevent
->event
->u
.ftrace
,
702 sizeof(struct lttng_kernel_abi_function
));
704 case LTTNG_KERNEL_ABI_NOOP
:
705 event
.type
= LTTNG_EVENT_NOOP
;
707 case LTTNG_KERNEL_ABI_SYSCALL
:
708 event
.type
= LTTNG_EVENT_SYSCALL
;
710 case LTTNG_KERNEL_ABI_ALL
:
717 ret
= lttng_dynamic_buffer_append(
718 &payload
->buffer
, &event
, sizeof(event
));
720 ERR("Failed to append event to payload");
721 ret
= -LTTNG_ERR_NOMEM
;
726 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
727 /* Append extended info. */
728 ret
= append_extended_info(kevent
->filter_expression
, NULL
,
729 kevent
->userspace_probe_location
, payload
);
731 DBG("Error appending extended info message");
732 ret
= -LTTNG_ERR_FATAL
;
744 * Add URI so the consumer output object. Set the correct path depending on the
745 * domain adding the default trace directory.
747 static enum lttng_error_code
add_uri_to_consumer(
748 const struct ltt_session
*session
,
749 struct consumer_output
*consumer
,
750 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
753 enum lttng_error_code ret_code
= LTTNG_OK
;
757 if (consumer
== NULL
) {
758 DBG("No consumer detected. Don't add URI. Stopping.");
759 ret_code
= LTTNG_ERR_NO_CONSUMER
;
764 case LTTNG_DOMAIN_KERNEL
:
765 ret
= lttng_strncpy(consumer
->domain_subdir
,
766 DEFAULT_KERNEL_TRACE_DIR
,
767 sizeof(consumer
->domain_subdir
));
769 case LTTNG_DOMAIN_UST
:
770 ret
= lttng_strncpy(consumer
->domain_subdir
,
771 DEFAULT_UST_TRACE_DIR
,
772 sizeof(consumer
->domain_subdir
));
776 * This case is possible is we try to add the URI to the global
777 * tracing session consumer object which in this case there is
780 memset(consumer
->domain_subdir
, 0,
781 sizeof(consumer
->domain_subdir
));
785 ERR("Failed to initialize consumer output domain subdirectory");
786 ret_code
= LTTNG_ERR_FATAL
;
790 switch (uri
->dtype
) {
793 DBG2("Setting network URI to consumer");
795 if (consumer
->type
== CONSUMER_DST_NET
) {
796 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
797 consumer
->dst
.net
.control_isset
) ||
798 (uri
->stype
== LTTNG_STREAM_DATA
&&
799 consumer
->dst
.net
.data_isset
)) {
800 ret_code
= LTTNG_ERR_URL_EXIST
;
804 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
807 /* Set URI into consumer output object */
808 ret
= consumer_set_network_uri(session
, consumer
, uri
);
812 } else if (ret
== 1) {
814 * URI was the same in the consumer so we do not append the subdir
815 * again so to not duplicate output dir.
822 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
823 ret_code
= LTTNG_ERR_INVALID
;
826 DBG2("Setting trace directory path from URI to %s",
828 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
830 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
832 sizeof(consumer
->dst
.session_root_path
));
834 ret_code
= LTTNG_ERR_FATAL
;
837 consumer
->type
= CONSUMER_DST_LOCAL
;
847 * Init tracing by creating trace directory and sending fds kernel consumer.
849 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
852 struct lttng_ht_iter iter
;
853 struct consumer_socket
*socket
;
855 LTTNG_ASSERT(session
);
859 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
860 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
862 pthread_mutex_lock(socket
->lock
);
863 ret
= kernel_consumer_send_session(socket
, session
);
864 pthread_mutex_unlock(socket
->lock
);
866 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
878 * Create a socket to the relayd using the URI.
880 * On success, the relayd_sock pointer is set to the created socket.
881 * Else, it remains untouched and an LTTng error code is returned.
883 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
884 struct lttcomm_relayd_sock
**relayd_sock
,
885 struct consumer_output
*consumer
)
888 enum lttng_error_code status
= LTTNG_OK
;
889 struct lttcomm_relayd_sock
*rsock
;
891 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
892 RELAYD_VERSION_COMM_MINOR
);
894 status
= LTTNG_ERR_FATAL
;
899 * Connect to relayd so we can proceed with a session creation. This call
900 * can possibly block for an arbitrary amount of time to set the health
901 * state to be in poll execution.
904 ret
= relayd_connect(rsock
);
907 ERR("Unable to reach lttng-relayd");
908 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
912 /* Create socket for control stream. */
913 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
914 uint64_t result_flags
;
916 DBG3("Creating relayd stream socket from URI");
918 /* Check relayd version */
919 ret
= relayd_version_check(rsock
);
920 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
921 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
923 } else if (ret
< 0) {
924 ERR("Unable to reach lttng-relayd");
925 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
928 consumer
->relay_major_version
= rsock
->major
;
929 consumer
->relay_minor_version
= rsock
->minor
;
930 ret
= relayd_get_configuration(rsock
, 0,
933 ERR("Unable to get relayd configuration");
934 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
937 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
938 consumer
->relay_allows_clear
= true;
940 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
941 DBG3("Creating relayd data socket from URI");
943 /* Command is not valid */
944 ERR("Relayd invalid stream type: %d", uri
->stype
);
945 status
= LTTNG_ERR_INVALID
;
949 *relayd_sock
= rsock
;
954 /* The returned value is not useful since we are on an error path. */
955 (void) relayd_close(rsock
);
963 * Connect to the relayd using URI and send the socket to the right consumer.
965 * The consumer socket lock must be held by the caller.
967 * Returns LTTNG_OK on success or an LTTng error code on failure.
969 static enum lttng_error_code
send_consumer_relayd_socket(
970 unsigned int session_id
,
971 struct lttng_uri
*relayd_uri
,
972 struct consumer_output
*consumer
,
973 struct consumer_socket
*consumer_sock
,
974 const char *session_name
, const char *hostname
,
975 const char *base_path
, int session_live_timer
,
976 const uint64_t *current_chunk_id
,
977 time_t session_creation_time
,
978 bool session_name_contains_creation_time
)
981 struct lttcomm_relayd_sock
*rsock
= NULL
;
982 enum lttng_error_code status
;
984 /* Connect to relayd and make version check if uri is the control. */
985 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
986 if (status
!= LTTNG_OK
) {
987 goto relayd_comm_error
;
991 /* Set the network sequence index if not set. */
992 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
993 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
995 * Increment net_seq_idx because we are about to transfer the
996 * new relayd socket to the consumer.
997 * Assign unique key so the consumer can match streams.
999 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1000 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1003 /* Send relayd socket to consumer. */
1004 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1005 relayd_uri
->stype
, session_id
,
1006 session_name
, hostname
, base_path
,
1007 session_live_timer
, current_chunk_id
,
1008 session_creation_time
, session_name_contains_creation_time
);
1010 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1014 /* Flag that the corresponding socket was sent. */
1015 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1016 consumer_sock
->control_sock_sent
= 1;
1017 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1018 consumer_sock
->data_sock_sent
= 1;
1022 * Close socket which was dup on the consumer side. The session daemon does
1023 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1027 if (status
!= LTTNG_OK
) {
1029 * The consumer output for this session should not be used anymore
1030 * since the relayd connection failed thus making any tracing or/and
1031 * streaming not usable.
1033 consumer
->enabled
= 0;
1035 (void) relayd_close(rsock
);
1043 * Send both relayd sockets to a specific consumer and domain. This is a
1044 * helper function to facilitate sending the information to the consumer for a
1047 * The consumer socket lock must be held by the caller.
1049 * Returns LTTNG_OK, or an LTTng error code on failure.
1051 static enum lttng_error_code
send_consumer_relayd_sockets(
1052 enum lttng_domain_type domain
,
1053 unsigned int session_id
, struct consumer_output
*consumer
,
1054 struct consumer_socket
*sock
, const char *session_name
,
1055 const char *hostname
, const char *base_path
, int session_live_timer
,
1056 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1057 bool session_name_contains_creation_time
)
1059 enum lttng_error_code status
= LTTNG_OK
;
1061 LTTNG_ASSERT(consumer
);
1064 /* Sending control relayd socket. */
1065 if (!sock
->control_sock_sent
) {
1066 status
= send_consumer_relayd_socket(session_id
,
1067 &consumer
->dst
.net
.control
, consumer
, sock
,
1068 session_name
, hostname
, base_path
, session_live_timer
,
1069 current_chunk_id
, session_creation_time
,
1070 session_name_contains_creation_time
);
1071 if (status
!= LTTNG_OK
) {
1076 /* Sending data relayd socket. */
1077 if (!sock
->data_sock_sent
) {
1078 status
= send_consumer_relayd_socket(session_id
,
1079 &consumer
->dst
.net
.data
, consumer
, sock
,
1080 session_name
, hostname
, base_path
, session_live_timer
,
1081 current_chunk_id
, session_creation_time
,
1082 session_name_contains_creation_time
);
1083 if (status
!= LTTNG_OK
) {
1093 * Setup relayd connections for a tracing session. First creates the socket to
1094 * the relayd and send them to the right domain consumer. Consumer type MUST be
1097 int cmd_setup_relayd(struct ltt_session
*session
)
1100 struct ltt_ust_session
*usess
;
1101 struct ltt_kernel_session
*ksess
;
1102 struct consumer_socket
*socket
;
1103 struct lttng_ht_iter iter
;
1104 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1106 LTTNG_ASSERT(session
);
1108 usess
= session
->ust_session
;
1109 ksess
= session
->kernel_session
;
1111 DBG("Setting relayd for session %s", session
->name
);
1114 if (session
->current_trace_chunk
) {
1115 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1116 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1118 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1119 current_chunk_id
.is_set
= true;
1121 ERR("Failed to get current trace chunk id");
1122 ret
= LTTNG_ERR_UNK
;
1127 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1128 && usess
->consumer
->enabled
) {
1129 /* For each consumer socket, send relayd sockets */
1130 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1131 socket
, node
.node
) {
1132 pthread_mutex_lock(socket
->lock
);
1133 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1134 usess
->consumer
, socket
,
1135 session
->name
, session
->hostname
,
1137 session
->live_timer
,
1138 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1139 session
->creation_time
,
1140 session
->name_contains_creation_time
);
1141 pthread_mutex_unlock(socket
->lock
);
1142 if (ret
!= LTTNG_OK
) {
1145 /* Session is now ready for network streaming. */
1146 session
->net_handle
= 1;
1148 session
->consumer
->relay_major_version
=
1149 usess
->consumer
->relay_major_version
;
1150 session
->consumer
->relay_minor_version
=
1151 usess
->consumer
->relay_minor_version
;
1152 session
->consumer
->relay_allows_clear
=
1153 usess
->consumer
->relay_allows_clear
;
1156 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1157 && ksess
->consumer
->enabled
) {
1158 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1159 socket
, node
.node
) {
1160 pthread_mutex_lock(socket
->lock
);
1161 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1162 ksess
->consumer
, socket
,
1163 session
->name
, session
->hostname
,
1165 session
->live_timer
,
1166 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1167 session
->creation_time
,
1168 session
->name_contains_creation_time
);
1169 pthread_mutex_unlock(socket
->lock
);
1170 if (ret
!= LTTNG_OK
) {
1173 /* Session is now ready for network streaming. */
1174 session
->net_handle
= 1;
1176 session
->consumer
->relay_major_version
=
1177 ksess
->consumer
->relay_major_version
;
1178 session
->consumer
->relay_minor_version
=
1179 ksess
->consumer
->relay_minor_version
;
1180 session
->consumer
->relay_allows_clear
=
1181 ksess
->consumer
->relay_allows_clear
;
1190 * Start a kernel session by opening all necessary streams.
1192 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1195 struct ltt_kernel_channel
*kchan
;
1197 /* Open kernel metadata */
1198 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1199 ret
= kernel_open_metadata(ksess
);
1201 ret
= LTTNG_ERR_KERN_META_FAIL
;
1206 /* Open kernel metadata stream */
1207 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1208 ret
= kernel_open_metadata_stream(ksess
);
1210 ERR("Kernel create metadata stream failed");
1211 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1216 /* For each channel */
1217 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1218 if (kchan
->stream_count
== 0) {
1219 ret
= kernel_open_channel_stream(kchan
);
1221 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1224 /* Update the stream global counter */
1225 ksess
->stream_count_global
+= ret
;
1229 /* Setup kernel consumer socket and send fds to it */
1230 ret
= init_kernel_tracing(ksess
);
1232 ret
= LTTNG_ERR_KERN_START_FAIL
;
1236 /* This start the kernel tracing */
1237 ret
= kernel_start_session(ksess
);
1239 ret
= LTTNG_ERR_KERN_START_FAIL
;
1243 /* Quiescent wait after starting trace */
1244 kernel_wait_quiescent();
1254 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1256 struct ltt_kernel_channel
*kchan
;
1257 bool error_occurred
= false;
1260 if (!ksess
|| !ksess
->active
) {
1263 DBG("Stopping kernel tracing");
1265 ret
= kernel_stop_session(ksess
);
1267 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1271 kernel_wait_quiescent();
1273 /* Flush metadata after stopping (if exists) */
1274 if (ksess
->metadata_stream_fd
>= 0) {
1275 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1277 ERR("Kernel metadata flush failed");
1278 error_occurred
= true;
1282 /* Flush all buffers after stopping */
1283 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1284 ret
= kernel_flush_buffer(kchan
);
1286 ERR("Kernel flush buffer error");
1287 error_occurred
= true;
1292 if (error_occurred
) {
1293 ret
= LTTNG_ERR_UNK
;
1302 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1304 int cmd_disable_channel(struct ltt_session
*session
,
1305 enum lttng_domain_type domain
, char *channel_name
)
1308 struct ltt_ust_session
*usess
;
1310 usess
= session
->ust_session
;
1315 case LTTNG_DOMAIN_KERNEL
:
1317 ret
= channel_kernel_disable(session
->kernel_session
,
1319 if (ret
!= LTTNG_OK
) {
1323 kernel_wait_quiescent();
1326 case LTTNG_DOMAIN_UST
:
1328 struct ltt_ust_channel
*uchan
;
1329 struct lttng_ht
*chan_ht
;
1331 chan_ht
= usess
->domain_global
.channels
;
1333 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1334 if (uchan
== NULL
) {
1335 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1339 ret
= channel_ust_disable(usess
, uchan
);
1340 if (ret
!= LTTNG_OK
) {
1346 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1358 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1360 * The wpipe arguments is used as a notifier for the kernel thread.
1362 int cmd_enable_channel(struct ltt_session
*session
,
1363 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1366 struct ltt_ust_session
*usess
= session
->ust_session
;
1367 struct lttng_ht
*chan_ht
;
1369 struct lttng_channel attr
;
1371 LTTNG_ASSERT(session
);
1372 LTTNG_ASSERT(_attr
);
1373 LTTNG_ASSERT(domain
);
1376 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1378 /* Validate channel name */
1379 if (attr
.name
[0] == '.' ||
1380 memchr(attr
.name
, '/', len
) != NULL
) {
1381 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1385 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1390 * Don't try to enable a channel if the session has been started at
1391 * some point in time before. The tracer does not allow it.
1393 if (session
->has_been_started
) {
1394 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1399 * If the session is a live session, remove the switch timer, the
1400 * live timer does the same thing but sends also synchronisation
1401 * beacons for inactive streams.
1403 if (session
->live_timer
> 0) {
1404 attr
.attr
.live_timer_interval
= session
->live_timer
;
1405 attr
.attr
.switch_timer_interval
= 0;
1408 /* Check for feature support */
1409 switch (domain
->type
) {
1410 case LTTNG_DOMAIN_KERNEL
:
1412 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1413 /* Sampling position of buffer is not supported */
1414 WARN("Kernel tracer does not support buffer monitoring. "
1415 "Setting the monitor interval timer to 0 "
1416 "(disabled) for channel '%s' of session '%s'",
1417 attr
.name
, session
->name
);
1418 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1422 case LTTNG_DOMAIN_UST
:
1424 case LTTNG_DOMAIN_JUL
:
1425 case LTTNG_DOMAIN_LOG4J
:
1426 case LTTNG_DOMAIN_PYTHON
:
1427 if (!agent_tracing_is_enabled()) {
1428 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1429 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1434 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1438 switch (domain
->type
) {
1439 case LTTNG_DOMAIN_KERNEL
:
1441 struct ltt_kernel_channel
*kchan
;
1443 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1444 session
->kernel_session
);
1445 if (kchan
== NULL
) {
1446 if (session
->snapshot
.nb_output
> 0 ||
1447 session
->snapshot_mode
) {
1448 /* Enforce mmap output for snapshot sessions. */
1449 attr
.attr
.output
= LTTNG_EVENT_MMAP
;
1451 ret
= channel_kernel_create(session
->kernel_session
, &attr
, wpipe
);
1452 if (attr
.name
[0] != '\0') {
1453 session
->kernel_session
->has_non_default_channel
= 1;
1456 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1459 if (ret
!= LTTNG_OK
) {
1463 kernel_wait_quiescent();
1466 case LTTNG_DOMAIN_UST
:
1467 case LTTNG_DOMAIN_JUL
:
1468 case LTTNG_DOMAIN_LOG4J
:
1469 case LTTNG_DOMAIN_PYTHON
:
1471 struct ltt_ust_channel
*uchan
;
1476 * Current agent implementation limitations force us to allow
1477 * only one channel at once in "agent" subdomains. Each
1478 * subdomain has a default channel name which must be strictly
1481 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1482 if (strncmp(attr
.name
, DEFAULT_JUL_CHANNEL_NAME
,
1483 LTTNG_SYMBOL_NAME_LEN
)) {
1484 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1487 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1488 if (strncmp(attr
.name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1489 LTTNG_SYMBOL_NAME_LEN
)) {
1490 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1493 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1494 if (strncmp(attr
.name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1495 LTTNG_SYMBOL_NAME_LEN
)) {
1496 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1501 chan_ht
= usess
->domain_global
.channels
;
1503 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1504 if (uchan
== NULL
) {
1505 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1506 if (attr
.name
[0] != '\0') {
1507 usess
->has_non_default_channel
= 1;
1510 ret
= channel_ust_enable(usess
, uchan
);
1515 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1519 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1520 session
->has_non_mmap_channel
= true;
1528 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1529 struct ltt_session
*session
,
1530 enum lttng_domain_type domain
,
1531 enum lttng_process_attr process_attr
,
1532 enum lttng_tracking_policy
*policy
)
1534 enum lttng_error_code ret_code
= LTTNG_OK
;
1535 const struct process_attr_tracker
*tracker
;
1538 case LTTNG_DOMAIN_KERNEL
:
1539 if (!session
->kernel_session
) {
1540 ret_code
= LTTNG_ERR_INVALID
;
1543 tracker
= kernel_get_process_attr_tracker(
1544 session
->kernel_session
, process_attr
);
1546 case LTTNG_DOMAIN_UST
:
1547 if (!session
->ust_session
) {
1548 ret_code
= LTTNG_ERR_INVALID
;
1551 tracker
= trace_ust_get_process_attr_tracker(
1552 session
->ust_session
, process_attr
);
1555 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1559 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1561 ret_code
= LTTNG_ERR_INVALID
;
1567 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1568 struct ltt_session
*session
,
1569 enum lttng_domain_type domain
,
1570 enum lttng_process_attr process_attr
,
1571 enum lttng_tracking_policy policy
)
1573 enum lttng_error_code ret_code
= LTTNG_OK
;
1576 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1577 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1578 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1581 ret_code
= LTTNG_ERR_INVALID
;
1586 case LTTNG_DOMAIN_KERNEL
:
1587 if (!session
->kernel_session
) {
1588 ret_code
= LTTNG_ERR_INVALID
;
1591 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1592 session
->kernel_session
, process_attr
, policy
);
1594 case LTTNG_DOMAIN_UST
:
1595 if (!session
->ust_session
) {
1596 ret_code
= LTTNG_ERR_INVALID
;
1599 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1600 session
->ust_session
, process_attr
, policy
);
1603 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1610 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1611 struct ltt_session
*session
,
1612 enum lttng_domain_type domain
,
1613 enum lttng_process_attr process_attr
,
1614 const struct process_attr_value
*value
)
1616 enum lttng_error_code ret_code
= LTTNG_OK
;
1619 case LTTNG_DOMAIN_KERNEL
:
1620 if (!session
->kernel_session
) {
1621 ret_code
= LTTNG_ERR_INVALID
;
1624 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1625 session
->kernel_session
, process_attr
, value
);
1627 case LTTNG_DOMAIN_UST
:
1628 if (!session
->ust_session
) {
1629 ret_code
= LTTNG_ERR_INVALID
;
1632 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1633 session
->ust_session
, process_attr
, value
);
1636 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1643 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1644 struct ltt_session
*session
,
1645 enum lttng_domain_type domain
,
1646 enum lttng_process_attr process_attr
,
1647 const struct process_attr_value
*value
)
1649 enum lttng_error_code ret_code
= LTTNG_OK
;
1652 case LTTNG_DOMAIN_KERNEL
:
1653 if (!session
->kernel_session
) {
1654 ret_code
= LTTNG_ERR_INVALID
;
1657 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1658 session
->kernel_session
, process_attr
, value
);
1660 case LTTNG_DOMAIN_UST
:
1661 if (!session
->ust_session
) {
1662 ret_code
= LTTNG_ERR_INVALID
;
1665 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1666 session
->ust_session
, process_attr
, value
);
1669 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1676 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1677 struct ltt_session
*session
,
1678 enum lttng_domain_type domain
,
1679 enum lttng_process_attr process_attr
,
1680 struct lttng_process_attr_values
**values
)
1682 enum lttng_error_code ret_code
= LTTNG_OK
;
1683 const struct process_attr_tracker
*tracker
;
1684 enum process_attr_tracker_status status
;
1687 case LTTNG_DOMAIN_KERNEL
:
1688 if (!session
->kernel_session
) {
1689 ret_code
= LTTNG_ERR_INVALID
;
1692 tracker
= kernel_get_process_attr_tracker(
1693 session
->kernel_session
, process_attr
);
1695 case LTTNG_DOMAIN_UST
:
1696 if (!session
->ust_session
) {
1697 ret_code
= LTTNG_ERR_INVALID
;
1700 tracker
= trace_ust_get_process_attr_tracker(
1701 session
->ust_session
, process_attr
);
1704 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1709 ret_code
= LTTNG_ERR_INVALID
;
1713 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1715 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1716 ret_code
= LTTNG_OK
;
1718 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1719 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1721 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1722 ret_code
= LTTNG_ERR_NOMEM
;
1725 ret_code
= LTTNG_ERR_UNK
;
1734 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1736 int cmd_disable_event(struct ltt_session
*session
,
1737 enum lttng_domain_type domain
, const char *channel_name
,
1738 const struct lttng_event
*event
)
1741 const char *event_name
;
1743 DBG("Disable event command for event \'%s\'", event
->name
);
1745 event_name
= event
->name
;
1747 /* Error out on unhandled search criteria */
1748 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1749 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1750 ret
= LTTNG_ERR_UNK
;
1757 case LTTNG_DOMAIN_KERNEL
:
1759 struct ltt_kernel_channel
*kchan
;
1760 struct ltt_kernel_session
*ksess
;
1762 ksess
= session
->kernel_session
;
1765 * If a non-default channel has been created in the
1766 * session, explicitely require that -c chan_name needs
1769 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1770 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1774 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1775 if (kchan
== NULL
) {
1776 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1780 switch (event
->type
) {
1781 case LTTNG_EVENT_ALL
:
1782 case LTTNG_EVENT_TRACEPOINT
:
1783 case LTTNG_EVENT_SYSCALL
:
1784 case LTTNG_EVENT_PROBE
:
1785 case LTTNG_EVENT_FUNCTION
:
1786 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1787 if (event_name
[0] == '\0') {
1788 ret
= event_kernel_disable_event(kchan
,
1791 ret
= event_kernel_disable_event(kchan
,
1792 event_name
, event
->type
);
1794 if (ret
!= LTTNG_OK
) {
1799 ret
= LTTNG_ERR_UNK
;
1803 kernel_wait_quiescent();
1806 case LTTNG_DOMAIN_UST
:
1808 struct ltt_ust_channel
*uchan
;
1809 struct ltt_ust_session
*usess
;
1811 usess
= session
->ust_session
;
1813 if (validate_ust_event_name(event_name
)) {
1814 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1819 * If a non-default channel has been created in the
1820 * session, explicitly require that -c chan_name needs
1823 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1824 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1828 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1830 if (uchan
== NULL
) {
1831 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1835 switch (event
->type
) {
1836 case LTTNG_EVENT_ALL
:
1838 * An empty event name means that everything
1839 * should be disabled.
1841 if (event
->name
[0] == '\0') {
1842 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1844 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1847 if (ret
!= LTTNG_OK
) {
1852 ret
= LTTNG_ERR_UNK
;
1856 DBG3("Disable UST event %s in channel %s completed", event_name
,
1860 case LTTNG_DOMAIN_LOG4J
:
1861 case LTTNG_DOMAIN_JUL
:
1862 case LTTNG_DOMAIN_PYTHON
:
1865 struct ltt_ust_session
*usess
= session
->ust_session
;
1867 LTTNG_ASSERT(usess
);
1869 switch (event
->type
) {
1870 case LTTNG_EVENT_ALL
:
1873 ret
= LTTNG_ERR_UNK
;
1877 agt
= trace_ust_find_agent(usess
, domain
);
1879 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1883 * An empty event name means that everything
1884 * should be disabled.
1886 if (event
->name
[0] == '\0') {
1887 ret
= event_agent_disable_all(usess
, agt
);
1889 ret
= event_agent_disable(usess
, agt
, event_name
);
1891 if (ret
!= LTTNG_OK
) {
1898 ret
= LTTNG_ERR_UND
;
1911 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1913 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1914 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1916 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1917 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1920 * Don't try to add a context if the session has been started at
1921 * some point in time before. The tracer does not allow it and would
1922 * result in a corrupted trace.
1924 if (session
->has_been_started
) {
1925 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1929 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1930 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1931 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1935 case LTTNG_DOMAIN_KERNEL
:
1936 LTTNG_ASSERT(session
->kernel_session
);
1938 if (session
->kernel_session
->channel_count
== 0) {
1939 /* Create default channel */
1940 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1941 if (ret
!= LTTNG_OK
) {
1944 chan_kern_created
= 1;
1946 /* Add kernel context to kernel tracer */
1947 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1948 if (ret
!= LTTNG_OK
) {
1952 case LTTNG_DOMAIN_JUL
:
1953 case LTTNG_DOMAIN_LOG4J
:
1956 * Validate channel name.
1957 * If no channel name is given and the domain is JUL or LOG4J,
1958 * set it to the appropriate domain-specific channel name. If
1959 * a name is provided but does not match the expexted channel
1960 * name, return an error.
1962 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1963 strcmp(channel_name
,
1964 DEFAULT_JUL_CHANNEL_NAME
)) {
1965 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1967 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1968 strcmp(channel_name
,
1969 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1970 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1973 /* break is _not_ missing here. */
1975 case LTTNG_DOMAIN_UST
:
1977 struct ltt_ust_session
*usess
= session
->ust_session
;
1978 unsigned int chan_count
;
1980 LTTNG_ASSERT(usess
);
1982 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1983 if (chan_count
== 0) {
1984 struct lttng_channel
*attr
;
1985 /* Create default channel */
1986 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1988 ret
= LTTNG_ERR_FATAL
;
1992 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1993 if (ret
!= LTTNG_OK
) {
1997 channel_attr_destroy(attr
);
1998 chan_ust_created
= 1;
2001 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2002 free(app_ctx_provider_name
);
2004 app_ctx_name
= NULL
;
2005 app_ctx_provider_name
= NULL
;
2006 if (ret
!= LTTNG_OK
) {
2012 ret
= LTTNG_ERR_UND
;
2020 if (chan_kern_created
) {
2021 struct ltt_kernel_channel
*kchan
=
2022 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2023 session
->kernel_session
);
2024 /* Created previously, this should NOT fail. */
2025 LTTNG_ASSERT(kchan
);
2026 kernel_destroy_channel(kchan
);
2029 if (chan_ust_created
) {
2030 struct ltt_ust_channel
*uchan
=
2031 trace_ust_find_channel_by_name(
2032 session
->ust_session
->domain_global
.channels
,
2033 DEFAULT_CHANNEL_NAME
);
2034 /* Created previously, this should NOT fail. */
2035 LTTNG_ASSERT(uchan
);
2036 /* Remove from the channel list of the session. */
2037 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2039 trace_ust_destroy_channel(uchan
);
2042 free(app_ctx_provider_name
);
2047 static inline bool name_starts_with(const char *name
, const char *prefix
)
2049 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2051 return !strncmp(name
, prefix
, max_cmp_len
);
2054 /* Perform userspace-specific event name validation */
2055 static int validate_ust_event_name(const char *name
)
2065 * Check name against all internal UST event component namespaces used
2068 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2069 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2070 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2079 * Internal version of cmd_enable_event() with a supplemental
2080 * "internal_event" flag which is used to enable internal events which should
2081 * be hidden from clients. Such events are used in the agent implementation to
2082 * enable the events through which all "agent" events are funeled.
2084 static int _cmd_enable_event(struct ltt_session
*session
,
2085 const struct lttng_domain
*domain
,
2086 char *channel_name
, struct lttng_event
*event
,
2087 char *filter_expression
,
2088 struct lttng_bytecode
*filter
,
2089 struct lttng_event_exclusion
*exclusion
,
2090 int wpipe
, bool internal_event
)
2092 int ret
= 0, channel_created
= 0;
2093 struct lttng_channel
*attr
= NULL
;
2095 LTTNG_ASSERT(session
);
2096 LTTNG_ASSERT(event
);
2097 LTTNG_ASSERT(channel_name
);
2099 /* If we have a filter, we must have its filter expression */
2100 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2102 /* Normalize event name as a globbing pattern */
2103 strutils_normalize_star_glob_pattern(event
->name
);
2105 /* Normalize exclusion names as globbing patterns */
2109 for (i
= 0; i
< exclusion
->count
; i
++) {
2110 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2112 strutils_normalize_star_glob_pattern(name
);
2116 DBG("Enable event command for event \'%s\'", event
->name
);
2120 switch (domain
->type
) {
2121 case LTTNG_DOMAIN_KERNEL
:
2123 struct ltt_kernel_channel
*kchan
;
2126 * If a non-default channel has been created in the
2127 * session, explicitely require that -c chan_name needs
2130 if (session
->kernel_session
->has_non_default_channel
2131 && channel_name
[0] == '\0') {
2132 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2136 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2137 session
->kernel_session
);
2138 if (kchan
== NULL
) {
2139 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2140 LTTNG_BUFFER_GLOBAL
);
2142 ret
= LTTNG_ERR_FATAL
;
2145 if (lttng_strncpy(attr
->name
, channel_name
,
2146 sizeof(attr
->name
))) {
2147 ret
= LTTNG_ERR_INVALID
;
2151 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2152 if (ret
!= LTTNG_OK
) {
2155 channel_created
= 1;
2158 /* Get the newly created kernel channel pointer */
2159 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2160 session
->kernel_session
);
2161 if (kchan
== NULL
) {
2162 /* This sould not happen... */
2163 ret
= LTTNG_ERR_FATAL
;
2167 switch (event
->type
) {
2168 case LTTNG_EVENT_ALL
:
2170 char *filter_expression_a
= NULL
;
2171 struct lttng_bytecode
*filter_a
= NULL
;
2174 * We need to duplicate filter_expression and filter,
2175 * because ownership is passed to first enable
2178 if (filter_expression
) {
2179 filter_expression_a
= strdup(filter_expression
);
2180 if (!filter_expression_a
) {
2181 ret
= LTTNG_ERR_FATAL
;
2186 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2188 free(filter_expression_a
);
2189 ret
= LTTNG_ERR_FATAL
;
2192 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2194 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2195 ret
= event_kernel_enable_event(kchan
, event
,
2196 filter_expression
, filter
);
2197 /* We have passed ownership */
2198 filter_expression
= NULL
;
2200 if (ret
!= LTTNG_OK
) {
2201 if (channel_created
) {
2202 /* Let's not leak a useless channel. */
2203 kernel_destroy_channel(kchan
);
2205 free(filter_expression_a
);
2209 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2210 ret
= event_kernel_enable_event(kchan
, event
,
2211 filter_expression_a
, filter_a
);
2212 /* We have passed ownership */
2213 filter_expression_a
= NULL
;
2215 if (ret
!= LTTNG_OK
) {
2220 case LTTNG_EVENT_PROBE
:
2221 case LTTNG_EVENT_USERSPACE_PROBE
:
2222 case LTTNG_EVENT_FUNCTION
:
2223 case LTTNG_EVENT_FUNCTION_ENTRY
:
2224 case LTTNG_EVENT_TRACEPOINT
:
2225 ret
= event_kernel_enable_event(kchan
, event
,
2226 filter_expression
, filter
);
2227 /* We have passed ownership */
2228 filter_expression
= NULL
;
2230 if (ret
!= LTTNG_OK
) {
2231 if (channel_created
) {
2232 /* Let's not leak a useless channel. */
2233 kernel_destroy_channel(kchan
);
2238 case LTTNG_EVENT_SYSCALL
:
2239 ret
= event_kernel_enable_event(kchan
, event
,
2240 filter_expression
, filter
);
2241 /* We have passed ownership */
2242 filter_expression
= NULL
;
2244 if (ret
!= LTTNG_OK
) {
2249 ret
= LTTNG_ERR_UNK
;
2253 kernel_wait_quiescent();
2256 case LTTNG_DOMAIN_UST
:
2258 struct ltt_ust_channel
*uchan
;
2259 struct ltt_ust_session
*usess
= session
->ust_session
;
2261 LTTNG_ASSERT(usess
);
2264 * If a non-default channel has been created in the
2265 * session, explicitely require that -c chan_name needs
2268 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2269 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2273 /* Get channel from global UST domain */
2274 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2276 if (uchan
== NULL
) {
2277 /* Create default channel */
2278 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2279 usess
->buffer_type
);
2281 ret
= LTTNG_ERR_FATAL
;
2284 if (lttng_strncpy(attr
->name
, channel_name
,
2285 sizeof(attr
->name
))) {
2286 ret
= LTTNG_ERR_INVALID
;
2290 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2291 if (ret
!= LTTNG_OK
) {
2295 /* Get the newly created channel reference back */
2296 uchan
= trace_ust_find_channel_by_name(
2297 usess
->domain_global
.channels
, channel_name
);
2298 LTTNG_ASSERT(uchan
);
2301 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2303 * Don't allow users to add UST events to channels which
2304 * are assigned to a userspace subdomain (JUL, Log4J,
2307 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2311 if (!internal_event
) {
2313 * Ensure the event name is not reserved for internal
2316 ret
= validate_ust_event_name(event
->name
);
2318 WARN("Userspace event name %s failed validation.",
2320 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2325 /* At this point, the session and channel exist on the tracer */
2326 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2327 filter_expression
, filter
, exclusion
,
2329 /* We have passed ownership */
2330 filter_expression
= NULL
;
2333 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2334 goto already_enabled
;
2335 } else if (ret
!= LTTNG_OK
) {
2340 case LTTNG_DOMAIN_LOG4J
:
2341 case LTTNG_DOMAIN_JUL
:
2342 case LTTNG_DOMAIN_PYTHON
:
2344 const char *default_event_name
, *default_chan_name
;
2346 struct lttng_event uevent
;
2347 struct lttng_domain tmp_dom
;
2348 struct ltt_ust_session
*usess
= session
->ust_session
;
2350 LTTNG_ASSERT(usess
);
2352 if (!agent_tracing_is_enabled()) {
2353 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2354 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2358 agt
= trace_ust_find_agent(usess
, domain
->type
);
2360 agt
= agent_create(domain
->type
);
2362 ret
= LTTNG_ERR_NOMEM
;
2365 agent_add(agt
, usess
->agents
);
2368 /* Create the default tracepoint. */
2369 memset(&uevent
, 0, sizeof(uevent
));
2370 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2371 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2372 default_event_name
= event_get_default_agent_ust_name(
2374 if (!default_event_name
) {
2375 ret
= LTTNG_ERR_FATAL
;
2378 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2379 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2382 * The domain type is changed because we are about to enable the
2383 * default channel and event for the JUL domain that are hardcoded.
2384 * This happens in the UST domain.
2386 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2387 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2389 switch (domain
->type
) {
2390 case LTTNG_DOMAIN_LOG4J
:
2391 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2393 case LTTNG_DOMAIN_JUL
:
2394 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2396 case LTTNG_DOMAIN_PYTHON
:
2397 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2400 /* The switch/case we are in makes this impossible */
2405 char *filter_expression_copy
= NULL
;
2406 struct lttng_bytecode
*filter_copy
= NULL
;
2409 const size_t filter_size
= sizeof(
2410 struct lttng_bytecode
)
2413 filter_copy
= zmalloc(filter_size
);
2415 ret
= LTTNG_ERR_NOMEM
;
2418 memcpy(filter_copy
, filter
, filter_size
);
2420 filter_expression_copy
=
2421 strdup(filter_expression
);
2422 if (!filter_expression
) {
2423 ret
= LTTNG_ERR_NOMEM
;
2426 if (!filter_expression_copy
|| !filter_copy
) {
2427 free(filter_expression_copy
);
2433 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2434 (char *) default_chan_name
,
2435 &uevent
, filter_expression_copy
,
2436 filter_copy
, NULL
, wpipe
);
2439 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2440 goto already_enabled
;
2441 } else if (ret
!= LTTNG_OK
) {
2445 /* The wild card * means that everything should be enabled. */
2446 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2447 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2450 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2454 filter_expression
= NULL
;
2455 if (ret
!= LTTNG_OK
) {
2462 ret
= LTTNG_ERR_UND
;
2470 free(filter_expression
);
2473 channel_attr_destroy(attr
);
2479 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2480 * We own filter, exclusion, and filter_expression.
2482 int cmd_enable_event(struct ltt_session
*session
,
2483 const struct lttng_domain
*domain
,
2484 char *channel_name
, struct lttng_event
*event
,
2485 char *filter_expression
,
2486 struct lttng_bytecode
*filter
,
2487 struct lttng_event_exclusion
*exclusion
,
2490 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2491 filter_expression
, filter
, exclusion
, wpipe
, false);
2495 * Enable an event which is internal to LTTng. An internal should
2496 * never be made visible to clients and are immune to checks such as
2499 static int cmd_enable_event_internal(struct ltt_session
*session
,
2500 const struct lttng_domain
*domain
,
2501 char *channel_name
, struct lttng_event
*event
,
2502 char *filter_expression
,
2503 struct lttng_bytecode
*filter
,
2504 struct lttng_event_exclusion
*exclusion
,
2507 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2508 filter_expression
, filter
, exclusion
, wpipe
, true);
2512 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2514 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2515 struct lttng_event
**events
)
2518 ssize_t nb_events
= 0;
2521 case LTTNG_DOMAIN_KERNEL
:
2522 nb_events
= kernel_list_events(events
);
2523 if (nb_events
< 0) {
2524 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2528 case LTTNG_DOMAIN_UST
:
2529 nb_events
= ust_app_list_events(events
);
2530 if (nb_events
< 0) {
2531 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2535 case LTTNG_DOMAIN_LOG4J
:
2536 case LTTNG_DOMAIN_JUL
:
2537 case LTTNG_DOMAIN_PYTHON
:
2538 nb_events
= agent_list_events(events
, domain
);
2539 if (nb_events
< 0) {
2540 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2545 ret
= LTTNG_ERR_UND
;
2552 /* Return negative value to differentiate return code */
2557 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2559 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2560 struct lttng_event_field
**fields
)
2563 ssize_t nb_fields
= 0;
2566 case LTTNG_DOMAIN_UST
:
2567 nb_fields
= ust_app_list_event_fields(fields
);
2568 if (nb_fields
< 0) {
2569 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2573 case LTTNG_DOMAIN_KERNEL
:
2574 default: /* fall-through */
2575 ret
= LTTNG_ERR_UND
;
2582 /* Return negative value to differentiate return code */
2586 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2588 return syscall_table_list(events
);
2592 * Command LTTNG_START_TRACE processed by the client thread.
2594 * Called with session mutex held.
2596 int cmd_start_trace(struct ltt_session
*session
)
2598 enum lttng_error_code ret
;
2599 unsigned long nb_chan
= 0;
2600 struct ltt_kernel_session
*ksession
;
2601 struct ltt_ust_session
*usess
;
2602 const bool session_rotated_after_last_stop
=
2603 session
->rotated_after_last_stop
;
2604 const bool session_cleared_after_last_stop
=
2605 session
->cleared_after_last_stop
;
2607 LTTNG_ASSERT(session
);
2609 /* Ease our life a bit ;) */
2610 ksession
= session
->kernel_session
;
2611 usess
= session
->ust_session
;
2613 /* Is the session already started? */
2614 if (session
->active
) {
2615 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2616 /* Perform nothing */
2620 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2621 !session
->current_trace_chunk
) {
2623 * A rotation was launched while the session was stopped and
2624 * it has not been completed yet. It is not possible to start
2625 * the session since starting the session here would require a
2626 * rotation from "NULL" to a new trace chunk. That rotation
2627 * would overlap with the ongoing rotation, which is not
2630 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2632 ret
= LTTNG_ERR_ROTATION_PENDING
;
2637 * Starting a session without channel is useless since after that it's not
2638 * possible to enable channel thus inform the client.
2640 if (usess
&& usess
->domain_global
.channels
) {
2641 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2644 nb_chan
+= ksession
->channel_count
;
2647 ret
= LTTNG_ERR_NO_CHANNEL
;
2651 session
->active
= 1;
2652 session
->rotated_after_last_stop
= false;
2653 session
->cleared_after_last_stop
= false;
2654 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2655 if (!session
->has_been_started
) {
2656 struct lttng_trace_chunk
*trace_chunk
;
2658 DBG("Creating initial trace chunk of session \"%s\"",
2660 trace_chunk
= session_create_new_trace_chunk(
2661 session
, NULL
, NULL
, NULL
);
2663 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2666 LTTNG_ASSERT(!session
->current_trace_chunk
);
2667 ret
= session_set_trace_chunk(session
, trace_chunk
,
2669 lttng_trace_chunk_put(trace_chunk
);
2671 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2675 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2678 * Rotate existing streams into the new chunk.
2679 * This is a "quiet" rotation has no client has
2680 * explicitly requested this operation.
2682 * There is also no need to wait for the rotation
2683 * to complete as it will happen immediately. No data
2684 * was produced as the session was stopped, so the
2685 * rotation should happen on reception of the command.
2687 ret
= cmd_rotate_session(session
, NULL
, true,
2688 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2689 if (ret
!= LTTNG_OK
) {
2695 /* Kernel tracing */
2696 if (ksession
!= NULL
) {
2697 DBG("Start kernel tracing session %s", session
->name
);
2698 ret
= start_kernel_session(ksession
);
2699 if (ret
!= LTTNG_OK
) {
2704 /* Flag session that trace should start automatically */
2706 int int_ret
= ust_app_start_trace_all(usess
);
2709 ret
= LTTNG_ERR_UST_START_FAIL
;
2715 * Open a packet in every stream of the session to ensure that viewers
2716 * can correctly identify the boundaries of the periods during which
2717 * tracing was active for this session.
2719 ret
= session_open_packets(session
);
2720 if (ret
!= LTTNG_OK
) {
2725 * Clear the flag that indicates that a rotation was done while the
2726 * session was stopped.
2728 session
->rotated_after_last_stop
= false;
2730 if (session
->rotate_timer_period
) {
2731 int int_ret
= timer_session_rotation_schedule_timer_start(
2732 session
, session
->rotate_timer_period
);
2735 ERR("Failed to enable rotate timer");
2736 ret
= LTTNG_ERR_UNK
;
2744 if (ret
== LTTNG_OK
) {
2745 /* Flag this after a successful start. */
2746 session
->has_been_started
|= 1;
2748 session
->active
= 0;
2749 /* Restore initial state on error. */
2750 session
->rotated_after_last_stop
=
2751 session_rotated_after_last_stop
;
2752 session
->cleared_after_last_stop
=
2753 session_cleared_after_last_stop
;
2760 * Command LTTNG_STOP_TRACE processed by the client thread.
2762 int cmd_stop_trace(struct ltt_session
*session
)
2765 struct ltt_kernel_session
*ksession
;
2766 struct ltt_ust_session
*usess
;
2768 LTTNG_ASSERT(session
);
2770 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2772 ksession
= session
->kernel_session
;
2773 usess
= session
->ust_session
;
2775 /* Session is not active. Skip everythong and inform the client. */
2776 if (!session
->active
) {
2777 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2781 ret
= stop_kernel_session(ksession
);
2782 if (ret
!= LTTNG_OK
) {
2786 if (usess
&& usess
->active
) {
2787 ret
= ust_app_stop_trace_all(usess
);
2789 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2794 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2796 /* Flag inactive after a successful stop. */
2797 session
->active
= 0;
2805 * Set the base_path of the session only if subdir of a control uris is set.
2806 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2808 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2810 struct lttng_uri
*uris
)
2815 for (i
= 0; i
< nb_uri
; i
++) {
2816 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2817 uris
[i
].subdir
[0] == '\0') {
2818 /* Not interested in these URIs */
2822 if (session
->base_path
!= NULL
) {
2823 free(session
->base_path
);
2824 session
->base_path
= NULL
;
2827 /* Set session base_path */
2828 session
->base_path
= strdup(uris
[i
].subdir
);
2829 if (!session
->base_path
) {
2830 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2831 uris
[i
].subdir
, session
->name
);
2832 ret
= LTTNG_ERR_NOMEM
;
2835 DBG2("Setting base path \"%s\" for session \"%s\"",
2836 session
->base_path
, session
->name
);
2844 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2846 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2847 struct lttng_uri
*uris
)
2850 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2851 struct ltt_ust_session
*usess
= session
->ust_session
;
2853 LTTNG_ASSERT(session
);
2855 LTTNG_ASSERT(nb_uri
> 0);
2857 /* Can't set consumer URI if the session is active. */
2858 if (session
->active
) {
2859 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2864 * Set the session base path if any. This is done inside
2865 * cmd_set_consumer_uri to preserve backward compatibility of the
2866 * previous session creation api vs the session descriptor api.
2868 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2869 if (ret
!= LTTNG_OK
) {
2873 /* Set the "global" consumer URIs */
2874 for (i
= 0; i
< nb_uri
; i
++) {
2875 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2877 if (ret
!= LTTNG_OK
) {
2882 /* Set UST session URIs */
2883 if (session
->ust_session
) {
2884 for (i
= 0; i
< nb_uri
; i
++) {
2885 ret
= add_uri_to_consumer(session
,
2886 session
->ust_session
->consumer
,
2887 &uris
[i
], LTTNG_DOMAIN_UST
);
2888 if (ret
!= LTTNG_OK
) {
2894 /* Set kernel session URIs */
2895 if (session
->kernel_session
) {
2896 for (i
= 0; i
< nb_uri
; i
++) {
2897 ret
= add_uri_to_consumer(session
,
2898 session
->kernel_session
->consumer
,
2899 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2900 if (ret
!= LTTNG_OK
) {
2907 * Make sure to set the session in output mode after we set URI since a
2908 * session can be created without URL (thus flagged in no output mode).
2910 session
->output_traces
= 1;
2912 ksess
->output_traces
= 1;
2916 usess
->output_traces
= 1;
2927 enum lttng_error_code
set_session_output_from_descriptor(
2928 struct ltt_session
*session
,
2929 const struct lttng_session_descriptor
*descriptor
)
2932 enum lttng_error_code ret_code
= LTTNG_OK
;
2933 enum lttng_session_descriptor_type session_type
=
2934 lttng_session_descriptor_get_type(descriptor
);
2935 enum lttng_session_descriptor_output_type output_type
=
2936 lttng_session_descriptor_get_output_type(descriptor
);
2937 struct lttng_uri uris
[2] = {};
2938 size_t uri_count
= 0;
2940 switch (output_type
) {
2941 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2943 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2944 lttng_session_descriptor_get_local_output_uri(descriptor
,
2948 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2949 lttng_session_descriptor_get_network_output_uris(descriptor
,
2950 &uris
[0], &uris
[1]);
2954 ret_code
= LTTNG_ERR_INVALID
;
2958 switch (session_type
) {
2959 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2961 struct snapshot_output
*new_output
= NULL
;
2963 new_output
= snapshot_output_alloc();
2965 ret_code
= LTTNG_ERR_NOMEM
;
2969 ret
= snapshot_output_init_with_uri(session
,
2970 DEFAULT_SNAPSHOT_MAX_SIZE
,
2971 NULL
, uris
, uri_count
, session
->consumer
,
2972 new_output
, &session
->snapshot
);
2974 ret_code
= (ret
== -ENOMEM
) ?
2975 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2976 snapshot_output_destroy(new_output
);
2979 snapshot_add_output(&session
->snapshot
, new_output
);
2982 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2983 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2985 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2989 ret_code
= LTTNG_ERR_INVALID
;
2997 enum lttng_error_code
cmd_create_session_from_descriptor(
2998 struct lttng_session_descriptor
*descriptor
,
2999 const lttng_sock_cred
*creds
,
3000 const char *home_path
)
3003 enum lttng_error_code ret_code
;
3004 const char *session_name
;
3005 struct ltt_session
*new_session
= NULL
;
3006 enum lttng_session_descriptor_status descriptor_status
;
3008 session_lock_list();
3010 if (*home_path
!= '/') {
3011 ERR("Home path provided by client is not absolute");
3012 ret_code
= LTTNG_ERR_INVALID
;
3017 descriptor_status
= lttng_session_descriptor_get_session_name(
3018 descriptor
, &session_name
);
3019 switch (descriptor_status
) {
3020 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3022 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3023 session_name
= NULL
;
3026 ret_code
= LTTNG_ERR_INVALID
;
3030 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3032 if (ret_code
!= LTTNG_OK
) {
3036 if (!session_name
) {
3037 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3040 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3045 if (!lttng_session_descriptor_is_output_destination_initialized(
3048 * Only include the session's creation time in the output
3049 * destination if the name of the session itself was
3050 * not auto-generated.
3052 ret_code
= lttng_session_descriptor_set_default_output(
3054 session_name
? &new_session
->creation_time
: NULL
,
3056 if (ret_code
!= LTTNG_OK
) {
3060 new_session
->has_user_specified_directory
=
3061 lttng_session_descriptor_has_output_directory(
3065 switch (lttng_session_descriptor_get_type(descriptor
)) {
3066 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3067 new_session
->snapshot_mode
= 1;
3069 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3070 new_session
->live_timer
=
3071 lttng_session_descriptor_live_get_timer_interval(
3078 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3079 if (ret_code
!= LTTNG_OK
) {
3082 new_session
->consumer
->enabled
= 1;
3083 ret_code
= LTTNG_OK
;
3085 /* Release reference provided by the session_create function. */
3086 session_put(new_session
);
3087 if (ret_code
!= LTTNG_OK
&& new_session
) {
3088 /* Release the global reference on error. */
3089 session_destroy(new_session
);
3091 session_unlock_list();
3095 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3096 struct lttng_session_descriptor
**return_descriptor
)
3099 size_t payload_size
;
3100 struct lttng_dynamic_buffer payload
;
3101 struct lttng_buffer_view home_dir_view
;
3102 struct lttng_buffer_view session_descriptor_view
;
3103 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3104 enum lttng_error_code ret_code
;
3106 lttng_dynamic_buffer_init(&payload
);
3107 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3109 ret_code
= LTTNG_ERR_INVALID
;
3112 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3113 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3114 ret_code
= LTTNG_ERR_INVALID
;
3118 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3119 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3120 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3122 ret_code
= LTTNG_ERR_NOMEM
;
3126 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3128 ERR("Reception of session descriptor failed, aborting.");
3129 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3133 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3136 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3137 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3138 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3139 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3140 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3144 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3146 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3147 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3148 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3149 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3150 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3154 ret
= lttng_session_descriptor_create_from_buffer(
3155 &session_descriptor_view
, &session_descriptor
);
3157 ERR("Failed to create session descriptor from payload of \"create session\" command");
3158 ret_code
= LTTNG_ERR_INVALID
;
3163 * Sets the descriptor's auto-generated properties (name, output) if
3166 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3168 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3169 if (ret_code
!= LTTNG_OK
) {
3173 ret_code
= LTTNG_OK
;
3174 *return_descriptor
= session_descriptor
;
3175 session_descriptor
= NULL
;
3177 lttng_dynamic_buffer_reset(&payload
);
3178 lttng_session_descriptor_destroy(session_descriptor
);
3183 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3184 void *_reply_context
)
3188 const struct cmd_destroy_session_reply_context
*reply_context
=
3190 struct lttng_dynamic_buffer payload
;
3191 struct lttcomm_session_destroy_command_header cmd_header
;
3192 struct lttng_trace_archive_location
*location
= NULL
;
3193 struct lttcomm_lttng_msg llm
= {
3194 .cmd_type
= LTTNG_DESTROY_SESSION
,
3195 .ret_code
= reply_context
->destruction_status
,
3198 sizeof(struct lttcomm_session_destroy_command_header
),
3201 size_t payload_size_before_location
;
3203 lttng_dynamic_buffer_init(&payload
);
3205 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3207 ERR("Failed to append session destruction message");
3211 cmd_header
.rotation_state
=
3212 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3213 session
->rotation_state
:
3214 LTTNG_ROTATION_STATE_NO_ROTATION
);
3215 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3216 sizeof(cmd_header
));
3218 ERR("Failed to append session destruction command header");
3222 if (!reply_context
->implicit_rotation_on_destroy
) {
3223 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3227 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3228 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3233 location
= session_get_trace_archive_location(session
);
3235 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3240 payload_size_before_location
= payload
.size
;
3241 comm_ret
= lttng_trace_archive_location_serialize(location
,
3243 lttng_trace_archive_location_put(location
);
3245 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3249 /* Update the message to indicate the location's length. */
3250 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3251 payload
.size
- payload_size_before_location
;
3253 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3254 payload
.data
, payload
.size
);
3255 if (comm_ret
!= (ssize_t
) payload
.size
) {
3256 ERR("Failed to send result of the destruction of session \"%s\" to client",
3260 ret
= close(reply_context
->reply_sock_fd
);
3262 PERROR("Failed to close client socket in deferred session destroy reply");
3264 lttng_dynamic_buffer_reset(&payload
);
3265 free(_reply_context
);
3269 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3271 * Called with session lock held.
3273 int cmd_destroy_session(struct ltt_session
*session
,
3274 struct notification_thread_handle
*notification_thread_handle
,
3278 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3279 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3282 reply_context
= zmalloc(sizeof(*reply_context
));
3283 if (!reply_context
) {
3284 ret
= LTTNG_ERR_NOMEM
;
3287 reply_context
->reply_sock_fd
= *sock_fd
;
3291 LTTNG_ASSERT(session
);
3293 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3295 if (session
->active
) {
3296 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3298 ret
= cmd_stop_trace(session
);
3299 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3300 /* Carry on with the destruction of the session. */
3301 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3302 session
->name
, lttng_strerror(-ret
));
3303 destruction_last_error
= ret
;
3307 if (session
->rotation_schedule_timer_enabled
) {
3308 if (timer_session_rotation_schedule_timer_stop(
3310 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3312 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3316 if (session
->rotate_size
) {
3317 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3318 session
->rotate_size
= 0;
3321 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3323 * Perform a last rotation on destruction if rotations have
3324 * occurred during the session's lifetime.
3326 ret
= cmd_rotate_session(session
, NULL
, false,
3327 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3328 if (ret
!= LTTNG_OK
) {
3329 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3330 session
->name
, lttng_strerror(-ret
));
3331 destruction_last_error
= -ret
;
3333 if (reply_context
) {
3334 reply_context
->implicit_rotation_on_destroy
= true;
3336 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3338 * The user has not triggered a session rotation. However, to
3339 * ensure all data has been consumed, the session is rotated
3340 * to a 'null' trace chunk before it is destroyed.
3342 * This is a "quiet" rotation meaning that no notification is
3343 * emitted and no renaming of the current trace chunk takes
3346 ret
= cmd_rotate_session(session
, NULL
, true,
3347 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3349 * Rotation operations may not be supported by the kernel
3350 * tracer. Hence, do not consider this implicit rotation as
3351 * a session destruction error. The library has already stopped
3352 * the session and waited for pending data; there is nothing
3353 * left to do but complete the destruction of the session.
3355 if (ret
!= LTTNG_OK
&&
3356 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3357 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3358 session
->name
, lttng_strerror(ret
));
3359 destruction_last_error
= -ret
;
3363 if (session
->shm_path
[0]) {
3365 * When a session is created with an explicit shm_path,
3366 * the consumer daemon will create its shared memory files
3367 * at that location and will *not* unlink them. This is normal
3368 * as the intention of that feature is to make it possible
3369 * to retrieve the content of those files should a crash occur.
3371 * To ensure the content of those files can be used, the
3372 * sessiond daemon will replicate the content of the metadata
3373 * cache in a metadata file.
3375 * On clean-up, it is expected that the consumer daemon will
3376 * unlink the shared memory files and that the session daemon
3377 * will unlink the metadata file. Then, the session's directory
3378 * in the shm path can be removed.
3380 * Unfortunately, a flaw in the design of the sessiond's and
3381 * consumerd's tear down of channels makes it impossible to
3382 * determine when the sessiond _and_ the consumerd have both
3383 * destroyed their representation of a channel. For one, the
3384 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3385 * callbacks in both daemons.
3387 * However, it is also impossible for the sessiond to know when
3388 * the consumer daemon is done destroying its channel(s) since
3389 * it occurs as a reaction to the closing of the channel's file
3390 * descriptor. There is no resulting communication initiated
3391 * from the consumerd to the sessiond to confirm that the
3392 * operation is completed (and was successful).
3394 * Until this is all fixed, the session daemon checks for the
3395 * removal of the session's shm path which makes it possible
3396 * to safely advertise a session as having been destroyed.
3398 * Prior to this fix, it was not possible to reliably save
3399 * a session making use of the --shm-path option, destroy it,
3400 * and load it again. This is because the creation of the
3401 * session would fail upon seeing the session's shm path
3402 * already in existence.
3404 * Note that none of the error paths in the check for the
3405 * directory's existence return an error. This is normal
3406 * as there isn't much that can be done. The session will
3407 * be destroyed properly, except that we can't offer the
3408 * guarantee that the same session can be re-created.
3410 current_completion_handler
= &destroy_completion_handler
.handler
;
3411 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3413 sizeof(destroy_completion_handler
.shm_path
));
3418 * The session is destroyed. However, note that the command context
3419 * still holds a reference to the session, thus delaying its destruction
3420 * _at least_ up to the point when that reference is released.
3422 session_destroy(session
);
3423 if (reply_context
) {
3424 reply_context
->destruction_status
= destruction_last_error
;
3425 ret
= session_add_destroy_notifier(session
,
3426 cmd_destroy_session_reply
,
3427 (void *) reply_context
);
3429 ret
= LTTNG_ERR_FATAL
;
3441 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3443 int cmd_register_consumer(struct ltt_session
*session
,
3444 enum lttng_domain_type domain
, const char *sock_path
,
3445 struct consumer_data
*cdata
)
3448 struct consumer_socket
*socket
= NULL
;
3450 LTTNG_ASSERT(session
);
3451 LTTNG_ASSERT(cdata
);
3452 LTTNG_ASSERT(sock_path
);
3455 case LTTNG_DOMAIN_KERNEL
:
3457 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3459 LTTNG_ASSERT(ksess
);
3461 /* Can't register a consumer if there is already one */
3462 if (ksess
->consumer_fds_sent
!= 0) {
3463 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3467 sock
= lttcomm_connect_unix_sock(sock_path
);
3469 ret
= LTTNG_ERR_CONNECT_FAIL
;
3472 cdata
->cmd_sock
= sock
;
3474 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3475 if (socket
== NULL
) {
3478 PERROR("close register consumer");
3480 cdata
->cmd_sock
= -1;
3481 ret
= LTTNG_ERR_FATAL
;
3485 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3486 if (socket
->lock
== NULL
) {
3487 PERROR("zmalloc pthread mutex");
3488 ret
= LTTNG_ERR_FATAL
;
3491 pthread_mutex_init(socket
->lock
, NULL
);
3492 socket
->registered
= 1;
3495 consumer_add_socket(socket
, ksess
->consumer
);
3498 pthread_mutex_lock(&cdata
->pid_mutex
);
3500 pthread_mutex_unlock(&cdata
->pid_mutex
);
3505 /* TODO: Userspace tracing */
3506 ret
= LTTNG_ERR_UND
;
3514 consumer_destroy_socket(socket
);
3520 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3522 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3523 struct lttng_domain
**domains
)
3528 struct lttng_ht_iter iter
;
3530 if (session
->kernel_session
!= NULL
) {
3531 DBG3("Listing domains found kernel domain");
3535 if (session
->ust_session
!= NULL
) {
3536 DBG3("Listing domains found UST global domain");
3540 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3542 if (agt
->being_used
) {
3553 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3554 if (*domains
== NULL
) {
3555 ret
= LTTNG_ERR_FATAL
;
3559 if (session
->kernel_session
!= NULL
) {
3560 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3562 /* Kernel session buffer type is always GLOBAL */
3563 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3568 if (session
->ust_session
!= NULL
) {
3569 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3570 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3574 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3576 if (agt
->being_used
) {
3577 (*domains
)[index
].type
= agt
->domain
;
3578 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3588 /* Return negative value to differentiate return code */
3594 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3596 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3597 struct ltt_session
*session
, struct lttng_channel
**channels
)
3599 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3602 case LTTNG_DOMAIN_KERNEL
:
3603 if (session
->kernel_session
!= NULL
) {
3604 nb_chan
= session
->kernel_session
->channel_count
;
3606 DBG3("Number of kernel channels %zd", nb_chan
);
3608 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3612 case LTTNG_DOMAIN_UST
:
3613 if (session
->ust_session
!= NULL
) {
3615 nb_chan
= lttng_ht_get_count(
3616 session
->ust_session
->domain_global
.channels
);
3619 DBG3("Number of UST global channels %zd", nb_chan
);
3621 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3626 ret
= -LTTNG_ERR_UND
;
3631 const size_t channel_size
= sizeof(struct lttng_channel
) +
3632 sizeof(struct lttng_channel_extended
);
3633 struct lttng_channel_extended
*channel_exts
;
3635 payload_size
= nb_chan
* channel_size
;
3636 *channels
= zmalloc(payload_size
);
3637 if (*channels
== NULL
) {
3638 ret
= -LTTNG_ERR_FATAL
;
3642 channel_exts
= ((void *) *channels
) +
3643 (nb_chan
* sizeof(struct lttng_channel
));
3644 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3645 if (ret
!= LTTNG_OK
) {
3660 * Command LTTNG_LIST_EVENTS processed by the client thread.
3662 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3663 struct ltt_session
*session
, char *channel_name
,
3664 struct lttng_payload
*payload
)
3667 ssize_t nb_events
= 0;
3668 struct lttcomm_event_command_header cmd_header
= {};
3669 const size_t cmd_header_offset
= payload
->buffer
.size
;
3671 ret
= lttng_dynamic_buffer_append(
3672 &payload
->buffer
, &cmd_header
, sizeof(cmd_header
));
3674 ret
= LTTNG_ERR_NOMEM
;
3679 case LTTNG_DOMAIN_KERNEL
:
3680 if (session
->kernel_session
!= NULL
) {
3681 nb_events
= list_lttng_kernel_events(channel_name
,
3682 session
->kernel_session
, payload
);
3685 case LTTNG_DOMAIN_UST
:
3687 if (session
->ust_session
!= NULL
) {
3688 nb_events
= list_lttng_ust_global_events(channel_name
,
3689 &session
->ust_session
->domain_global
,
3694 case LTTNG_DOMAIN_LOG4J
:
3695 case LTTNG_DOMAIN_JUL
:
3696 case LTTNG_DOMAIN_PYTHON
:
3697 if (session
->ust_session
) {
3698 struct lttng_ht_iter iter
;
3702 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3703 &iter
.iter
, agt
, node
.node
) {
3704 if (agt
->domain
== domain
) {
3705 nb_events
= list_lttng_agent_events(
3714 ret
= LTTNG_ERR_UND
;
3718 ((struct lttcomm_event_command_header
*) (payload
->buffer
.data
+
3719 cmd_header_offset
))->nb_events
= (uint32_t) nb_events
;
3724 /* Return negative value to differentiate return code */
3729 * Using the session list, filled a lttng_session array to send back to the
3730 * client for session listing.
3732 * The session list lock MUST be acquired before calling this function. Use
3733 * session_lock_list() and session_unlock_list().
3735 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3736 size_t session_count
, uid_t uid
, gid_t gid
)
3740 struct ltt_session
*session
;
3741 struct ltt_session_list
*list
= session_get_list();
3742 struct lttng_session_extended
*extended
=
3743 (typeof(extended
)) (&sessions
[session_count
]);
3745 DBG("Getting all available session for UID %d GID %d",
3748 * Iterate over session list and append data after the control struct in
3751 cds_list_for_each_entry(session
, &list
->head
, list
) {
3752 if (!session_get(session
)) {
3756 * Only list the sessions the user can control.
3758 if (!session_access_ok(session
, uid
) ||
3759 session
->destroyed
) {
3760 session_put(session
);
3764 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3765 struct ltt_ust_session
*usess
= session
->ust_session
;
3767 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3768 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3769 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3770 ret
= build_network_session_path(sessions
[i
].path
,
3771 sizeof(sessions
[i
].path
), session
);
3773 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3774 session
->consumer
->dst
.session_root_path
);
3777 PERROR("snprintf session path");
3778 session_put(session
);
3782 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3783 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3784 sessions
[i
].enabled
= session
->active
;
3785 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3786 sessions
[i
].live_timer_interval
= session
->live_timer
;
3787 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3788 extended
[i
].creation_time
.is_set
= 1;
3790 session_put(session
);
3795 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3796 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3798 int cmd_data_pending(struct ltt_session
*session
)
3801 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3802 struct ltt_ust_session
*usess
= session
->ust_session
;
3804 LTTNG_ASSERT(session
);
3806 DBG("Data pending for session %s", session
->name
);
3808 /* Session MUST be stopped to ask for data availability. */
3809 if (session
->active
) {
3810 ret
= LTTNG_ERR_SESSION_STARTED
;
3814 * If stopped, just make sure we've started before else the above call
3815 * will always send that there is data pending.
3817 * The consumer assumes that when the data pending command is received,
3818 * the trace has been started before or else no output data is written
3819 * by the streams which is a condition for data pending. So, this is
3820 * *VERY* important that we don't ask the consumer before a start
3823 if (!session
->has_been_started
) {
3829 /* A rotation is still pending, we have to wait. */
3830 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3831 DBG("Rotate still pending for session %s", session
->name
);
3836 if (ksess
&& ksess
->consumer
) {
3837 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3839 /* Data is still being extracted for the kernel. */
3844 if (usess
&& usess
->consumer
) {
3845 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3847 /* Data is still being extracted for the kernel. */
3852 /* Data is ready to be read by a viewer */
3860 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3862 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3864 int cmd_snapshot_add_output(struct ltt_session
*session
,
3865 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3868 struct snapshot_output
*new_output
;
3870 LTTNG_ASSERT(session
);
3871 LTTNG_ASSERT(output
);
3873 DBG("Cmd snapshot add output for session %s", session
->name
);
3876 * Can't create an output if the session is not set in no-output mode.
3878 if (session
->output_traces
) {
3879 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3883 if (session
->has_non_mmap_channel
) {
3884 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3888 /* Only one output is allowed until we have the "tee" feature. */
3889 if (session
->snapshot
.nb_output
== 1) {
3890 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3894 new_output
= snapshot_output_alloc();
3896 ret
= LTTNG_ERR_NOMEM
;
3900 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3901 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3902 &session
->snapshot
);
3904 if (ret
== -ENOMEM
) {
3905 ret
= LTTNG_ERR_NOMEM
;
3907 ret
= LTTNG_ERR_INVALID
;
3913 snapshot_add_output(&session
->snapshot
, new_output
);
3915 *id
= new_output
->id
;
3922 snapshot_output_destroy(new_output
);
3928 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3930 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3932 int cmd_snapshot_del_output(struct ltt_session
*session
,
3933 const struct lttng_snapshot_output
*output
)
3936 struct snapshot_output
*sout
= NULL
;
3938 LTTNG_ASSERT(session
);
3939 LTTNG_ASSERT(output
);
3944 * Permission denied to create an output if the session is not
3945 * set in no output mode.
3947 if (session
->output_traces
) {
3948 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3953 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3955 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3956 } else if (*output
->name
!= '\0') {
3957 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3959 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3962 ret
= LTTNG_ERR_INVALID
;
3966 snapshot_delete_output(&session
->snapshot
, sout
);
3967 snapshot_output_destroy(sout
);
3976 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3978 * If no output is available, outputs is untouched and 0 is returned.
3980 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3982 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3983 struct lttng_snapshot_output
**outputs
)
3986 struct lttng_snapshot_output
*list
= NULL
;
3987 struct lttng_ht_iter iter
;
3988 struct snapshot_output
*output
;
3990 LTTNG_ASSERT(session
);
3991 LTTNG_ASSERT(outputs
);
3993 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3996 * Permission denied to create an output if the session is not
3997 * set in no output mode.
3999 if (session
->output_traces
) {
4000 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4004 if (session
->snapshot
.nb_output
== 0) {
4009 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4011 ret
= -LTTNG_ERR_NOMEM
;
4015 /* Copy list from session to the new list object. */
4017 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4018 output
, node
.node
) {
4019 LTTNG_ASSERT(output
->consumer
);
4020 list
[idx
].id
= output
->id
;
4021 list
[idx
].max_size
= output
->max_size
;
4022 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4023 sizeof(list
[idx
].name
))) {
4024 ret
= -LTTNG_ERR_INVALID
;
4027 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4028 if (lttng_strncpy(list
[idx
].ctrl_url
,
4029 output
->consumer
->dst
.session_root_path
,
4030 sizeof(list
[idx
].ctrl_url
))) {
4031 ret
= -LTTNG_ERR_INVALID
;
4036 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4037 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4039 ret
= -LTTNG_ERR_NOMEM
;
4044 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4045 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4047 ret
= -LTTNG_ERR_NOMEM
;
4056 ret
= session
->snapshot
.nb_output
;
4065 * Check if we can regenerate the metadata for this session.
4066 * Only kernel, UST per-uid and non-live sessions are supported.
4068 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4071 int check_regenerate_metadata_support(struct ltt_session
*session
)
4075 LTTNG_ASSERT(session
);
4077 if (session
->live_timer
!= 0) {
4078 ret
= LTTNG_ERR_LIVE_SESSION
;
4081 if (!session
->active
) {
4082 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4085 if (session
->ust_session
) {
4086 switch (session
->ust_session
->buffer_type
) {
4087 case LTTNG_BUFFER_PER_UID
:
4089 case LTTNG_BUFFER_PER_PID
:
4090 ret
= LTTNG_ERR_PER_PID_SESSION
;
4094 ret
= LTTNG_ERR_UNK
;
4098 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4099 session
->consumer
->relay_minor_version
< 8) {
4100 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4110 int clear_metadata_file(int fd
)
4115 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4116 if (lseek_ret
< 0) {
4122 ret
= ftruncate(fd
, 0);
4124 PERROR("ftruncate");
4133 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4136 struct buffer_reg_uid
*uid_reg
= NULL
;
4137 struct buffer_reg_session
*session_reg
= NULL
;
4140 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4141 struct ust_registry_session
*registry
;
4142 struct ust_registry_channel
*chan
;
4143 struct lttng_ht_iter iter_chan
;
4145 session_reg
= uid_reg
->registry
;
4146 registry
= session_reg
->reg
.ust
;
4148 pthread_mutex_lock(®istry
->lock
);
4149 registry
->metadata_len_sent
= 0;
4150 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4151 registry
->metadata_len
= 0;
4152 registry
->metadata_version
++;
4153 if (registry
->metadata_fd
> 0) {
4154 /* Clear the metadata file's content. */
4155 ret
= clear_metadata_file(registry
->metadata_fd
);
4157 pthread_mutex_unlock(®istry
->lock
);
4162 ret
= ust_metadata_session_statedump(registry
, NULL
,
4163 registry
->major
, registry
->minor
);
4165 pthread_mutex_unlock(®istry
->lock
);
4166 ERR("Failed to generate session metadata (err = %d)",
4170 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4172 struct ust_registry_event
*event
;
4173 struct lttng_ht_iter iter_event
;
4175 ret
= ust_metadata_channel_statedump(registry
, chan
);
4177 pthread_mutex_unlock(®istry
->lock
);
4178 ERR("Failed to generate channel metadata "
4182 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4184 ret
= ust_metadata_event_statedump(registry
,
4187 pthread_mutex_unlock(®istry
->lock
);
4188 ERR("Failed to generate event metadata "
4194 pthread_mutex_unlock(®istry
->lock
);
4203 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4205 * Ask the consumer to truncate the existing metadata file(s) and
4206 * then regenerate the metadata. Live and per-pid sessions are not
4207 * supported and return an error.
4209 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4211 int cmd_regenerate_metadata(struct ltt_session
*session
)
4215 LTTNG_ASSERT(session
);
4217 ret
= check_regenerate_metadata_support(session
);
4222 if (session
->kernel_session
) {
4223 ret
= kernctl_session_regenerate_metadata(
4224 session
->kernel_session
->fd
);
4226 ERR("Failed to regenerate the kernel metadata");
4231 if (session
->ust_session
) {
4232 ret
= ust_regenerate_metadata(session
->ust_session
);
4234 ERR("Failed to regenerate the UST metadata");
4238 DBG("Cmd metadata regenerate for session %s", session
->name
);
4246 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4248 * Ask the tracer to regenerate a new statedump.
4250 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4252 int cmd_regenerate_statedump(struct ltt_session
*session
)
4256 LTTNG_ASSERT(session
);
4258 if (!session
->active
) {
4259 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4263 if (session
->kernel_session
) {
4264 ret
= kernctl_session_regenerate_statedump(
4265 session
->kernel_session
->fd
);
4267 * Currently, the statedump in kernel can only fail if out
4271 if (ret
== -ENOMEM
) {
4272 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4274 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4276 ERR("Failed to regenerate the kernel statedump");
4281 if (session
->ust_session
) {
4282 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4284 * Currently, the statedump in UST always returns 0.
4287 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4288 ERR("Failed to regenerate the UST statedump");
4292 DBG("Cmd regenerate statedump for session %s", session
->name
);
4300 enum lttng_error_code
synchronize_tracer_notifier_register(
4301 struct notification_thread_handle
*notification_thread
,
4302 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4304 enum lttng_error_code ret_code
;
4305 const struct lttng_condition
*condition
=
4306 lttng_trigger_get_const_condition(trigger
);
4307 const char *trigger_name
;
4308 uid_t trigger_owner
;
4309 enum lttng_trigger_status trigger_status
;
4310 const enum lttng_domain_type trigger_domain
=
4311 lttng_trigger_get_underlying_domain_type_restriction(
4314 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4315 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4317 LTTNG_ASSERT(condition
);
4318 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4319 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4321 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4322 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4323 trigger_name
: "(anonymous)";
4325 session_lock_list();
4326 switch (trigger_domain
) {
4327 case LTTNG_DOMAIN_KERNEL
:
4329 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4330 if (ret_code
!= LTTNG_OK
) {
4331 enum lttng_error_code notif_thread_unregister_ret
;
4333 notif_thread_unregister_ret
=
4334 notification_thread_command_unregister_trigger(
4335 notification_thread
, trigger
);
4337 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4338 /* Return the original error code. */
4339 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4341 (int) trigger_owner
,
4347 case LTTNG_DOMAIN_UST
:
4348 ust_app_global_update_all_event_notifier_rules();
4350 case LTTNG_DOMAIN_JUL
:
4351 case LTTNG_DOMAIN_LOG4J
:
4352 case LTTNG_DOMAIN_PYTHON
:
4354 /* Agent domains. */
4355 struct agent
*agt
= agent_find_by_event_notifier_domain(
4359 agt
= agent_create(trigger_domain
);
4361 ret_code
= LTTNG_ERR_NOMEM
;
4362 goto end_unlock_session_list
;
4365 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4368 ret_code
= trigger_agent_enable(trigger
, agt
);
4369 if (ret_code
!= LTTNG_OK
) {
4370 goto end_unlock_session_list
;
4375 case LTTNG_DOMAIN_NONE
:
4380 ret_code
= LTTNG_OK
;
4381 end_unlock_session_list
:
4382 session_unlock_list();
4386 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4387 struct lttng_trigger
*trigger
,
4388 bool is_trigger_anonymous
,
4389 struct notification_thread_handle
*notification_thread
,
4390 struct lttng_trigger
**return_trigger
)
4392 enum lttng_error_code ret_code
;
4393 const char *trigger_name
;
4394 uid_t trigger_owner
;
4395 enum lttng_trigger_status trigger_status
;
4397 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4398 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4399 trigger_name
: "(anonymous)";
4401 trigger_status
= lttng_trigger_get_owner_uid(
4402 trigger
, &trigger_owner
);
4403 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4405 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4406 trigger_name
, (int) trigger_owner
,
4407 (int) lttng_credentials_get_uid(cmd_creds
));
4410 * Validate the trigger credentials against the command credentials.
4411 * Only the root user can register a trigger with non-matching
4414 if (!lttng_credentials_is_equal_uid(
4415 lttng_trigger_get_credentials(trigger
),
4417 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4418 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4419 trigger_name
, (int) trigger_owner
,
4420 (int) lttng_credentials_get_uid(cmd_creds
));
4421 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4427 * The bytecode generation also serves as a validation step for the
4428 * bytecode expressions.
4430 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4431 if (ret_code
!= LTTNG_OK
) {
4432 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4433 trigger_name
, (int) trigger_owner
, ret_code
);
4438 * A reference to the trigger is acquired by the notification thread.
4439 * It is safe to return the same trigger to the caller since it the
4440 * other user holds a reference.
4442 * The trigger is modified during the execution of the
4443 * "register trigger" command. However, by the time the command returns,
4444 * it is safe to use without any locking as its properties are
4447 ret_code
= notification_thread_command_register_trigger(
4448 notification_thread
, trigger
, is_trigger_anonymous
);
4449 if (ret_code
!= LTTNG_OK
) {
4450 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4451 trigger_name
, (int) trigger_owner
, ret_code
);
4455 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4456 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4457 trigger_name
: "(anonymous)";
4460 * Synchronize tracers if the trigger adds an event notifier.
4462 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4463 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4464 trigger
, cmd_creds
);
4465 if (ret_code
!= LTTNG_OK
) {
4466 ERR("Error registering tracer notifier: %s",
4467 lttng_strerror(-ret_code
));
4473 * Return an updated trigger to the client.
4475 * Since a modified version of the same trigger is returned, acquire a
4476 * reference to the trigger so the caller doesn't have to care if those
4477 * are distinct instances or not.
4479 if (ret_code
== LTTNG_OK
) {
4480 lttng_trigger_get(trigger
);
4481 *return_trigger
= trigger
;
4482 /* Ownership of trigger was transferred to caller. */
4490 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4491 const struct lttng_trigger
*trigger
)
4493 enum lttng_error_code ret_code
;
4494 const struct lttng_condition
*condition
=
4495 lttng_trigger_get_const_condition(trigger
);
4496 const enum lttng_domain_type trigger_domain
=
4497 lttng_trigger_get_underlying_domain_type_restriction(
4500 LTTNG_ASSERT(condition
);
4501 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4502 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4504 session_lock_list();
4505 switch (trigger_domain
) {
4506 case LTTNG_DOMAIN_KERNEL
:
4507 ret_code
= kernel_unregister_event_notifier(trigger
);
4508 if (ret_code
!= LTTNG_OK
) {
4509 goto end_unlock_session_list
;
4513 case LTTNG_DOMAIN_UST
:
4514 ust_app_global_update_all_event_notifier_rules();
4516 case LTTNG_DOMAIN_JUL
:
4517 case LTTNG_DOMAIN_LOG4J
:
4518 case LTTNG_DOMAIN_PYTHON
:
4520 /* Agent domains. */
4521 struct agent
*agt
= agent_find_by_event_notifier_domain(
4525 * This trigger was never registered in the first place. Calling
4526 * this function under those circumstances is an internal error.
4529 ret_code
= trigger_agent_disable(trigger
, agt
);
4530 if (ret_code
!= LTTNG_OK
) {
4531 goto end_unlock_session_list
;
4536 case LTTNG_DOMAIN_NONE
:
4541 ret_code
= LTTNG_OK
;
4543 end_unlock_session_list
:
4544 session_unlock_list();
4548 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4549 const struct lttng_trigger
*trigger
,
4550 struct notification_thread_handle
*notification_thread
)
4552 enum lttng_error_code ret_code
;
4553 const char *trigger_name
;
4554 uid_t trigger_owner
;
4555 enum lttng_trigger_status trigger_status
;
4556 struct lttng_trigger
*sessiond_trigger
= NULL
;
4558 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4559 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4560 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4561 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4563 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4564 trigger_name
, (int) trigger_owner
,
4565 (int) lttng_credentials_get_uid(cmd_creds
));
4568 * Validate the trigger credentials against the command credentials.
4569 * Only the root user can unregister a trigger with non-matching
4572 if (!lttng_credentials_is_equal_uid(
4573 lttng_trigger_get_credentials(trigger
),
4575 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4576 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4577 trigger_name
, (int) trigger_owner
,
4578 (int) lttng_credentials_get_uid(cmd_creds
));
4579 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4584 /* Fetch the sessiond side trigger object. */
4585 ret_code
= notification_thread_command_get_trigger(
4586 notification_thread
, trigger
, &sessiond_trigger
);
4587 if (ret_code
!= LTTNG_OK
) {
4588 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4589 trigger_name
, (int) trigger_owner
, ret_code
);
4593 LTTNG_ASSERT(sessiond_trigger
);
4596 * From this point on, no matter what, consider the trigger
4599 * We set the unregistered state of the sessiond side trigger object in
4600 * the client thread since we want to minimize the possibility of the
4601 * notification thread being stalled due to a long execution of an
4602 * action that required the trigger lock.
4604 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4606 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4608 if (ret_code
!= LTTNG_OK
) {
4609 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4610 trigger_name
, (int) trigger_owner
, ret_code
);
4615 * Synchronize tracers if the trigger removes an event notifier.
4616 * Do this even if the trigger unregistration failed to at least stop
4617 * the tracers from producing notifications associated with this
4620 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4621 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4622 if (ret_code
!= LTTNG_OK
) {
4623 ERR("Error unregistering trigger to tracer.");
4630 lttng_trigger_put(sessiond_trigger
);
4634 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4635 struct notification_thread_handle
*notification_thread
,
4636 struct lttng_triggers
**return_triggers
)
4639 enum lttng_error_code ret_code
;
4640 struct lttng_triggers
*triggers
= NULL
;
4642 /* Get the set of triggers from the notification thread. */
4643 ret_code
= notification_thread_command_list_triggers(
4644 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4645 if (ret_code
!= LTTNG_OK
) {
4649 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4651 ret_code
= LTTNG_ERR_UNK
;
4655 *return_triggers
= triggers
;
4657 ret_code
= LTTNG_OK
;
4659 lttng_triggers_destroy(triggers
);
4663 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4664 const struct lttng_error_query
*query
,
4665 struct lttng_error_query_results
**_results
,
4666 struct notification_thread_handle
*notification_thread
)
4668 enum lttng_error_code ret_code
;
4669 const struct lttng_trigger
*query_target_trigger
;
4670 const struct lttng_action
*query_target_action
= NULL
;
4671 struct lttng_trigger
*matching_trigger
= NULL
;
4672 const char *trigger_name
;
4673 uid_t trigger_owner
;
4674 enum lttng_trigger_status trigger_status
;
4675 struct lttng_error_query_results
*results
= NULL
;
4677 switch (lttng_error_query_get_target_type(query
)) {
4678 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4679 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4681 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4682 query_target_trigger
=
4683 lttng_error_query_condition_borrow_target(query
);
4685 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4686 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4693 LTTNG_ASSERT(query_target_trigger
);
4695 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4696 query_target_trigger
, &matching_trigger
);
4697 if (ret_code
!= LTTNG_OK
) {
4701 /* No longer needed. */
4702 query_target_trigger
= NULL
;
4704 if (lttng_error_query_get_target_type(query
) ==
4705 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4706 /* Get the sessiond-side version of the target action. */
4707 query_target_action
=
4708 lttng_error_query_action_borrow_action_target(
4709 query
, matching_trigger
);
4712 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4713 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4714 trigger_name
: "(anonymous)";
4715 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4717 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4719 results
= lttng_error_query_results_create();
4721 ret_code
= LTTNG_ERR_NOMEM
;
4725 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4726 trigger_name
, (int) trigger_owner
,
4727 (int) lttng_credentials_get_uid(cmd_creds
));
4730 * Validate the trigger credentials against the command credentials.
4731 * Only the root user can target a trigger with non-matching
4734 if (!lttng_credentials_is_equal_uid(
4735 lttng_trigger_get_credentials(matching_trigger
),
4737 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4738 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4739 trigger_name
, (int) trigger_owner
,
4740 (int) lttng_credentials_get_uid(cmd_creds
));
4741 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4746 switch (lttng_error_query_get_target_type(query
)) {
4747 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4748 trigger_status
= lttng_trigger_add_error_results(
4749 matching_trigger
, results
);
4751 switch (trigger_status
) {
4752 case LTTNG_TRIGGER_STATUS_OK
:
4755 ret_code
= LTTNG_ERR_UNK
;
4760 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4762 trigger_status
= lttng_trigger_condition_add_error_results(
4763 matching_trigger
, results
);
4765 switch (trigger_status
) {
4766 case LTTNG_TRIGGER_STATUS_OK
:
4769 ret_code
= LTTNG_ERR_UNK
;
4775 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4777 const enum lttng_action_status action_status
=
4778 lttng_action_add_error_query_results(
4779 query_target_action
, results
);
4781 switch (action_status
) {
4782 case LTTNG_ACTION_STATUS_OK
:
4785 ret_code
= LTTNG_ERR_UNK
;
4796 *_results
= results
;
4798 ret_code
= LTTNG_OK
;
4800 lttng_trigger_put(matching_trigger
);
4801 lttng_error_query_results_destroy(results
);
4806 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4807 * snapshot output is *not* set with a remote destination.
4809 * Return LTTNG_OK on success or a LTTNG_ERR code.
4811 static enum lttng_error_code
set_relayd_for_snapshot(
4812 struct consumer_output
*output
,
4813 const struct ltt_session
*session
)
4815 enum lttng_error_code status
= LTTNG_OK
;
4816 struct lttng_ht_iter iter
;
4817 struct consumer_socket
*socket
;
4818 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4819 const char *base_path
;
4821 LTTNG_ASSERT(output
);
4822 LTTNG_ASSERT(session
);
4824 DBG2("Set relayd object from snapshot output");
4826 if (session
->current_trace_chunk
) {
4827 enum lttng_trace_chunk_status chunk_status
=
4828 lttng_trace_chunk_get_id(
4829 session
->current_trace_chunk
,
4830 ¤t_chunk_id
.value
);
4832 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4833 current_chunk_id
.is_set
= true;
4835 ERR("Failed to get current trace chunk id");
4836 status
= LTTNG_ERR_UNK
;
4841 /* Ignore if snapshot consumer output is not network. */
4842 if (output
->type
!= CONSUMER_DST_NET
) {
4847 * The snapshot record URI base path overrides the session
4850 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4851 base_path
= output
->dst
.net
.control
.subdir
;
4853 base_path
= session
->base_path
;
4857 * For each consumer socket, create and send the relayd object of the
4861 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4862 socket
, node
.node
) {
4863 pthread_mutex_lock(socket
->lock
);
4864 status
= send_consumer_relayd_sockets(0, session
->id
,
4866 session
->name
, session
->hostname
,
4868 session
->live_timer
,
4869 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4870 session
->creation_time
,
4871 session
->name_contains_creation_time
);
4872 pthread_mutex_unlock(socket
->lock
);
4873 if (status
!= LTTNG_OK
) {
4885 * Record a kernel snapshot.
4887 * Return LTTNG_OK on success or a LTTNG_ERR code.
4889 static enum lttng_error_code
record_kernel_snapshot(
4890 struct ltt_kernel_session
*ksess
,
4891 const struct consumer_output
*output
,
4892 const struct ltt_session
*session
,
4893 int wait
, uint64_t nb_packets_per_stream
)
4895 enum lttng_error_code status
;
4897 LTTNG_ASSERT(ksess
);
4898 LTTNG_ASSERT(output
);
4899 LTTNG_ASSERT(session
);
4901 status
= kernel_snapshot_record(
4902 ksess
, output
, wait
, nb_packets_per_stream
);
4907 * Record a UST snapshot.
4909 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4911 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4912 const struct consumer_output
*output
,
4913 const struct ltt_session
*session
,
4914 int wait
, uint64_t nb_packets_per_stream
)
4916 enum lttng_error_code status
;
4918 LTTNG_ASSERT(usess
);
4919 LTTNG_ASSERT(output
);
4920 LTTNG_ASSERT(session
);
4922 status
= ust_app_snapshot_record(
4923 usess
, output
, wait
, nb_packets_per_stream
);
4928 uint64_t get_session_size_one_more_packet_per_stream(
4929 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4931 uint64_t tot_size
= 0;
4933 if (session
->kernel_session
) {
4934 struct ltt_kernel_channel
*chan
;
4935 const struct ltt_kernel_session
*ksess
=
4936 session
->kernel_session
;
4938 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4939 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4941 * Don't take channel into account if we
4942 * already grab all its packets.
4946 tot_size
+= chan
->channel
->attr
.subbuf_size
4947 * chan
->stream_count
;
4951 if (session
->ust_session
) {
4952 const struct ltt_ust_session
*usess
= session
->ust_session
;
4954 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4962 * Calculate the number of packets we can grab from each stream that
4963 * fits within the overall snapshot max size.
4965 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4966 * the number of packets per stream.
4968 * TODO: this approach is not perfect: we consider the worse case
4969 * (packet filling the sub-buffers) as an upper bound, but we could do
4970 * better if we do this calculation while we actually grab the packet
4971 * content: we would know how much padding we don't actually store into
4974 * This algorithm is currently bounded by the number of packets per
4977 * Since we call this algorithm before actually grabbing the data, it's
4978 * an approximation: for instance, applications could appear/disappear
4979 * in between this call and actually grabbing data.
4982 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4986 uint64_t cur_nb_packets
= 0;
4989 return 0; /* Infinite */
4992 size_left
= max_size
;
4994 uint64_t one_more_packet_tot_size
;
4996 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4997 session
, cur_nb_packets
);
4998 if (!one_more_packet_tot_size
) {
4999 /* We are already grabbing all packets. */
5002 size_left
-= one_more_packet_tot_size
;
5003 if (size_left
< 0) {
5008 if (!cur_nb_packets
&& size_left
!= max_size
) {
5009 /* Not enough room to grab one packet of each stream, error. */
5012 return cur_nb_packets
;
5016 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5017 const struct snapshot_output
*snapshot_output
, int wait
)
5019 int64_t nb_packets_per_stream
;
5020 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5022 enum lttng_error_code ret_code
= LTTNG_OK
;
5023 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5024 struct consumer_output
*original_ust_consumer_output
= NULL
;
5025 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5026 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5027 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5029 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5031 snapshot_output
->name
,
5032 snapshot_output
->datetime
,
5033 snapshot_output
->nb_snapshot
);
5034 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5035 ERR("Failed to format snapshot name");
5036 ret_code
= LTTNG_ERR_INVALID
;
5039 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5040 snapshot_output
->name
, session
->name
,
5041 snapshot_chunk_name
);
5042 if (!session
->kernel_session
&& !session
->ust_session
) {
5043 ERR("Failed to record snapshot as no channels exist");
5044 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5048 if (session
->kernel_session
) {
5049 original_kernel_consumer_output
=
5050 session
->kernel_session
->consumer
;
5051 snapshot_kernel_consumer_output
=
5052 consumer_copy_output(snapshot_output
->consumer
);
5053 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5054 snapshot_chunk_name
);
5056 /* Copy the original domain subdir. */
5057 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5058 original_kernel_consumer_output
->domain_subdir
);
5060 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5061 original_kernel_consumer_output
);
5063 ERR("Failed to copy consumer sockets from snapshot output configuration");
5064 ret_code
= LTTNG_ERR_NOMEM
;
5067 ret_code
= set_relayd_for_snapshot(
5068 snapshot_kernel_consumer_output
, session
);
5069 if (ret_code
!= LTTNG_OK
) {
5070 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5073 session
->kernel_session
->consumer
=
5074 snapshot_kernel_consumer_output
;
5076 if (session
->ust_session
) {
5077 original_ust_consumer_output
= session
->ust_session
->consumer
;
5078 snapshot_ust_consumer_output
=
5079 consumer_copy_output(snapshot_output
->consumer
);
5080 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5081 snapshot_chunk_name
);
5083 /* Copy the original domain subdir. */
5084 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5085 original_ust_consumer_output
->domain_subdir
);
5087 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5088 original_ust_consumer_output
);
5090 ERR("Failed to copy consumer sockets from snapshot output configuration");
5091 ret_code
= LTTNG_ERR_NOMEM
;
5094 ret_code
= set_relayd_for_snapshot(
5095 snapshot_ust_consumer_output
, session
);
5096 if (ret_code
!= LTTNG_OK
) {
5097 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5100 session
->ust_session
->consumer
=
5101 snapshot_ust_consumer_output
;
5104 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5105 snapshot_kernel_consumer_output
?:
5106 snapshot_ust_consumer_output
,
5107 consumer_output_get_base_path(
5108 snapshot_output
->consumer
),
5109 snapshot_chunk_name
);
5110 if (!snapshot_trace_chunk
) {
5111 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5113 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5116 LTTNG_ASSERT(!session
->current_trace_chunk
);
5117 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5118 lttng_trace_chunk_put(snapshot_trace_chunk
);
5119 snapshot_trace_chunk
= NULL
;
5121 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5123 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5127 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5128 snapshot_output
->max_size
);
5129 if (nb_packets_per_stream
< 0) {
5130 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5131 goto error_close_trace_chunk
;
5134 if (session
->kernel_session
) {
5135 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5136 snapshot_kernel_consumer_output
, session
,
5137 wait
, nb_packets_per_stream
);
5138 if (ret_code
!= LTTNG_OK
) {
5139 goto error_close_trace_chunk
;
5143 if (session
->ust_session
) {
5144 ret_code
= record_ust_snapshot(session
->ust_session
,
5145 snapshot_ust_consumer_output
, session
,
5146 wait
, nb_packets_per_stream
);
5147 if (ret_code
!= LTTNG_OK
) {
5148 goto error_close_trace_chunk
;
5152 error_close_trace_chunk
:
5153 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5154 ERR("Failed to release the current trace chunk of session \"%s\"",
5156 ret_code
= LTTNG_ERR_UNK
;
5159 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5160 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5162 * Don't goto end; make sure the chunk is closed for the session
5163 * to allow future snapshots.
5165 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5167 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5170 if (original_ust_consumer_output
) {
5171 session
->ust_session
->consumer
= original_ust_consumer_output
;
5173 if (original_kernel_consumer_output
) {
5174 session
->kernel_session
->consumer
=
5175 original_kernel_consumer_output
;
5177 consumer_output_put(snapshot_ust_consumer_output
);
5178 consumer_output_put(snapshot_kernel_consumer_output
);
5183 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5185 * The wait parameter is ignored so this call always wait for the snapshot to
5186 * complete before returning.
5188 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5190 int cmd_snapshot_record(struct ltt_session
*session
,
5191 const struct lttng_snapshot_output
*output
, int wait
)
5193 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5195 unsigned int snapshot_success
= 0;
5197 struct snapshot_output
*tmp_output
= NULL
;
5199 LTTNG_ASSERT(session
);
5200 LTTNG_ASSERT(output
);
5202 DBG("Cmd snapshot record for session %s", session
->name
);
5204 /* Get the datetime for the snapshot output directory. */
5205 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5208 cmd_ret
= LTTNG_ERR_INVALID
;
5213 * Permission denied to create an output if the session is not
5214 * set in no output mode.
5216 if (session
->output_traces
) {
5217 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5221 /* The session needs to be started at least once. */
5222 if (!session
->has_been_started
) {
5223 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5227 /* Use temporary output for the session. */
5228 if (*output
->ctrl_url
!= '\0') {
5229 tmp_output
= snapshot_output_alloc();
5231 cmd_ret
= LTTNG_ERR_NOMEM
;
5235 ret
= snapshot_output_init(session
, output
->max_size
,
5237 output
->ctrl_url
, output
->data_url
,
5241 if (ret
== -ENOMEM
) {
5242 cmd_ret
= LTTNG_ERR_NOMEM
;
5244 cmd_ret
= LTTNG_ERR_INVALID
;
5248 /* Use the global session count for the temporary snapshot. */
5249 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5251 /* Use the global datetime */
5252 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5253 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
5254 if (cmd_ret
!= LTTNG_OK
) {
5257 snapshot_success
= 1;
5259 struct snapshot_output
*sout
;
5260 struct lttng_ht_iter iter
;
5263 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5264 &iter
.iter
, sout
, node
.node
) {
5265 struct snapshot_output output_copy
;
5268 * Make a local copy of the output and override output
5269 * parameters with those provided as part of the
5272 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5274 if (output
->max_size
!= (uint64_t) -1ULL) {
5275 output_copy
.max_size
= output
->max_size
;
5278 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5279 memcpy(output_copy
.datetime
, datetime
,
5282 /* Use temporary name. */
5283 if (*output
->name
!= '\0') {
5284 if (lttng_strncpy(output_copy
.name
,
5286 sizeof(output_copy
.name
))) {
5287 cmd_ret
= LTTNG_ERR_INVALID
;
5293 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
5294 if (cmd_ret
!= LTTNG_OK
) {
5298 snapshot_success
= 1;
5303 if (snapshot_success
) {
5304 session
->snapshot
.nb_snapshot
++;
5306 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5311 snapshot_output_destroy(tmp_output
);
5317 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5319 int cmd_set_session_shm_path(struct ltt_session
*session
,
5320 const char *shm_path
)
5323 LTTNG_ASSERT(session
);
5326 * Can only set shm path before session is started.
5328 if (session
->has_been_started
) {
5329 return LTTNG_ERR_SESSION_STARTED
;
5332 strncpy(session
->shm_path
, shm_path
,
5333 sizeof(session
->shm_path
));
5334 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5340 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5342 * Ask the consumer to rotate the session output directory.
5343 * The session lock must be held.
5345 * Returns LTTNG_OK on success or else a negative LTTng error code.
5347 int cmd_rotate_session(struct ltt_session
*session
,
5348 struct lttng_rotate_session_return
*rotate_return
,
5349 bool quiet_rotation
,
5350 enum lttng_trace_chunk_command_type command
)
5353 uint64_t ongoing_rotation_chunk_id
;
5354 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5355 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5356 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5357 enum lttng_trace_chunk_status chunk_status
;
5358 bool failed_to_rotate
= false;
5359 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5361 LTTNG_ASSERT(session
);
5363 if (!session
->has_been_started
) {
5364 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5369 * Explicit rotation is not supported for live sessions.
5370 * However, live sessions can perform a quiet rotation on
5372 * Rotation is not supported for snapshot traces (no output).
5374 if ((!quiet_rotation
&& session
->live_timer
) ||
5375 !session
->output_traces
) {
5376 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5380 /* Unsupported feature in lttng-relayd before 2.11. */
5381 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5382 (session
->consumer
->relay_major_version
== 2 &&
5383 session
->consumer
->relay_minor_version
< 11)) {
5384 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5388 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5389 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5390 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5394 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5395 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5397 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5402 * After a stop, we only allow one rotation to occur, the other ones are
5403 * useless until a new start.
5405 if (session
->rotated_after_last_stop
) {
5406 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5408 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5413 * After a stop followed by a clear, disallow following rotations a they would
5414 * generate empty chunks.
5416 if (session
->cleared_after_last_stop
) {
5417 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5419 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5423 if (session
->active
) {
5424 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5426 if (!new_trace_chunk
) {
5427 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5433 * The current trace chunk becomes the chunk being archived.
5435 * After this point, "chunk_being_archived" must absolutely
5436 * be closed on the consumer(s), otherwise it will never be
5437 * cleaned-up, which will result in a leak.
5439 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5440 &chunk_being_archived
);
5442 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5446 if (session
->kernel_session
) {
5447 cmd_ret
= kernel_rotate_session(session
);
5448 if (cmd_ret
!= LTTNG_OK
) {
5449 failed_to_rotate
= true;
5450 rotation_fail_code
= cmd_ret
;
5453 if (session
->ust_session
) {
5454 cmd_ret
= ust_app_rotate_session(session
);
5455 if (cmd_ret
!= LTTNG_OK
) {
5456 failed_to_rotate
= true;
5457 rotation_fail_code
= cmd_ret
;
5461 if (!session
->active
) {
5462 session
->rotated_after_last_stop
= true;
5465 if (!chunk_being_archived
) {
5466 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5468 if (failed_to_rotate
) {
5469 cmd_ret
= rotation_fail_code
;
5476 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5477 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5478 &ongoing_rotation_chunk_id
);
5479 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5481 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5482 command
, session
->last_chunk_path
);
5484 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5488 if (failed_to_rotate
) {
5489 cmd_ret
= rotation_fail_code
;
5493 session
->quiet_rotation
= quiet_rotation
;
5494 ret
= timer_session_rotation_pending_check_start(session
,
5495 DEFAULT_ROTATE_PENDING_TIMER
);
5497 cmd_ret
= LTTNG_ERR_UNK
;
5501 if (rotate_return
) {
5502 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5505 session
->chunk_being_archived
= chunk_being_archived
;
5506 chunk_being_archived
= NULL
;
5507 if (!quiet_rotation
) {
5508 ret
= notification_thread_command_session_rotation_ongoing(
5509 the_notification_thread_handle
, session
->name
,
5510 session
->uid
, session
->gid
,
5511 ongoing_rotation_chunk_id
);
5512 if (ret
!= LTTNG_OK
) {
5513 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5519 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5520 session
->name
, ongoing_rotation_chunk_id
);
5522 lttng_trace_chunk_put(new_trace_chunk
);
5523 lttng_trace_chunk_put(chunk_being_archived
);
5524 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5527 if (session_reset_rotation_state(session
,
5528 LTTNG_ROTATION_STATE_ERROR
)) {
5529 ERR("Failed to reset rotation state of session \"%s\"",
5536 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5538 * Check if the session has finished its rotation.
5540 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5542 int cmd_rotate_get_info(struct ltt_session
*session
,
5543 struct lttng_rotation_get_info_return
*info_return
,
5544 uint64_t rotation_id
)
5546 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5547 enum lttng_rotation_state rotation_state
;
5549 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5550 session
->most_recent_chunk_id
.value
);
5552 if (session
->chunk_being_archived
) {
5553 enum lttng_trace_chunk_status chunk_status
;
5556 chunk_status
= lttng_trace_chunk_get_id(
5557 session
->chunk_being_archived
,
5559 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5561 rotation_state
= rotation_id
== chunk_id
?
5562 LTTNG_ROTATION_STATE_ONGOING
:
5563 LTTNG_ROTATION_STATE_EXPIRED
;
5565 if (session
->last_archived_chunk_id
.is_set
&&
5566 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5567 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5569 rotation_state
= session
->rotation_state
;
5573 switch (rotation_state
) {
5574 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5575 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5578 case LTTNG_ROTATION_STATE_EXPIRED
:
5579 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5580 rotation_id
, session
->name
);
5582 case LTTNG_ROTATION_STATE_ONGOING
:
5583 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5584 rotation_id
, session
->name
);
5586 case LTTNG_ROTATION_STATE_COMPLETED
:
5590 char *current_tracing_path_reply
;
5591 size_t current_tracing_path_reply_len
;
5593 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5594 rotation_id
, session
->name
);
5596 switch (session_get_consumer_destination_type(session
)) {
5597 case CONSUMER_DST_LOCAL
:
5598 current_tracing_path_reply
=
5599 info_return
->location
.local
.absolute_path
;
5600 current_tracing_path_reply_len
=
5601 sizeof(info_return
->location
.local
.absolute_path
);
5602 info_return
->location_type
=
5603 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5604 fmt_ret
= asprintf(&chunk_path
,
5605 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5606 session_get_base_path(session
),
5607 session
->last_archived_chunk_name
);
5608 if (fmt_ret
== -1) {
5609 PERROR("Failed to format the path of the last archived trace chunk");
5610 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5611 cmd_ret
= LTTNG_ERR_UNK
;
5615 case CONSUMER_DST_NET
:
5617 uint16_t ctrl_port
, data_port
;
5619 current_tracing_path_reply
=
5620 info_return
->location
.relay
.relative_path
;
5621 current_tracing_path_reply_len
=
5622 sizeof(info_return
->location
.relay
.relative_path
);
5623 /* Currently the only supported relay protocol. */
5624 info_return
->location
.relay
.protocol
=
5625 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5627 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5628 session_get_net_consumer_hostname(session
),
5629 sizeof(info_return
->location
.relay
.host
));
5631 ERR("Failed to copy host name to rotate_get_info reply");
5632 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5633 cmd_ret
= LTTNG_ERR_SET_URL
;
5637 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5638 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5639 info_return
->location
.relay
.ports
.data
= data_port
;
5640 info_return
->location_type
=
5641 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5642 chunk_path
= strdup(session
->last_chunk_path
);
5644 ERR("Failed to allocate the path of the last archived trace chunk");
5645 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5646 cmd_ret
= LTTNG_ERR_UNK
;
5655 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5656 chunk_path
, current_tracing_path_reply_len
);
5659 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5660 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5661 cmd_ret
= LTTNG_ERR_UNK
;
5667 case LTTNG_ROTATION_STATE_ERROR
:
5668 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5669 rotation_id
, session
->name
);
5677 info_return
->status
= (int32_t) rotation_state
;
5682 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5684 * Configure the automatic rotation parameters.
5685 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5686 * 'activate' to false means deactivate the rotation schedule and validate that
5687 * 'new_value' has the same value as the currently active value.
5689 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5691 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5692 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5694 struct notification_thread_handle
*notification_thread_handle
)
5697 uint64_t *parameter_value
;
5699 LTTNG_ASSERT(session
);
5701 DBG("Cmd rotate set schedule session %s", session
->name
);
5703 if (session
->live_timer
|| !session
->output_traces
) {
5704 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5705 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5709 switch (schedule_type
) {
5710 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5711 parameter_value
= &session
->rotate_size
;
5713 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5714 parameter_value
= &session
->rotate_timer_period
;
5715 if (new_value
>= UINT_MAX
) {
5716 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5717 new_value
, UINT_MAX
);
5718 ret
= LTTNG_ERR_INVALID
;
5723 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5724 ret
= LTTNG_ERR_INVALID
;
5728 /* Improper use of the API. */
5729 if (new_value
== -1ULL) {
5730 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5731 ret
= LTTNG_ERR_INVALID
;
5736 * As indicated in struct ltt_session's comments, a value of == 0 means
5737 * this schedule rotation type is not in use.
5739 * Reject the command if we were asked to activate a schedule that was
5742 if (activate
&& *parameter_value
!= 0) {
5743 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5744 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5749 * Reject the command if we were asked to deactivate a schedule that was
5752 if (!activate
&& *parameter_value
== 0) {
5753 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5754 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5759 * Reject the command if we were asked to deactivate a schedule that
5762 if (!activate
&& *parameter_value
!= new_value
) {
5763 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5764 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5768 *parameter_value
= activate
? new_value
: 0;
5770 switch (schedule_type
) {
5771 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5772 if (activate
&& session
->active
) {
5774 * Only start the timer if the session is active,
5775 * otherwise it will be started when the session starts.
5777 ret
= timer_session_rotation_schedule_timer_start(
5778 session
, new_value
);
5780 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5781 ret
= LTTNG_ERR_UNK
;
5785 ret
= timer_session_rotation_schedule_timer_stop(
5788 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5789 ret
= LTTNG_ERR_UNK
;
5794 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5796 ret
= subscribe_session_consumed_size_rotation(session
,
5797 new_value
, notification_thread_handle
);
5799 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5800 ret
= LTTNG_ERR_UNK
;
5804 ret
= unsubscribe_session_consumed_size_rotation(session
,
5805 notification_thread_handle
);
5807 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5808 ret
= LTTNG_ERR_UNK
;
5815 /* Would have been caught before. */
5827 /* Wait for a given path to be removed before continuing. */
5828 static enum lttng_error_code
wait_on_path(void *path_data
)
5830 const char *shm_path
= path_data
;
5832 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5838 ret
= stat(shm_path
, &st
);
5840 if (errno
!= ENOENT
) {
5841 PERROR("stat() returned an error while checking for the existence of the shm path");
5843 DBG("shm path no longer exists, completing the destruction of session");
5847 if (!S_ISDIR(st
.st_mode
)) {
5848 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5853 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5859 * Returns a pointer to a handler to run on completion of a command.
5860 * Returns NULL if no handler has to be run for the last command executed.
5862 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5864 struct cmd_completion_handler
*handler
= current_completion_handler
;
5866 current_completion_handler
= NULL
;
5871 * Init command subsystem.
5876 * Set network sequence index to 1 for streams to match a relayd
5877 * socket on the consumer side.
5879 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5880 relayd_net_seq_idx
= 1;
5881 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5883 DBG("Command subsystem initialized");