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
10 #include "agent-thread.hpp"
12 #include "buffer-registry.hpp"
13 #include "channel.hpp"
15 #include "consumer.hpp"
16 #include "event-notifier-error-accounting.hpp"
18 #include "health-sessiond.hpp"
19 #include "kernel-consumer.hpp"
21 #include "lttng-sessiond.hpp"
22 #include "lttng-syscall.hpp"
23 #include "notification-thread-commands.hpp"
24 #include "notification-thread.hpp"
25 #include "rotation-thread.hpp"
26 #include "session.hpp"
28 #include "tracker.hpp"
31 #include <common/buffer-view.hpp>
32 #include <common/common.hpp>
33 #include <common/compat/string.hpp>
34 #include <common/defaults.hpp>
35 #include <common/dynamic-buffer.hpp>
36 #include <common/kernel-ctl/kernel-ctl.hpp>
37 #include <common/payload-view.hpp>
38 #include <common/payload.hpp>
39 #include <common/relayd/relayd.hpp>
40 #include <common/sessiond-comm/sessiond-comm.hpp>
41 #include <common/string-utils/string-utils.hpp>
42 #include <common/trace-chunk.hpp>
43 #include <common/urcu.hpp>
44 #include <common/utils.hpp>
46 #include <lttng/action/action-internal.hpp>
47 #include <lttng/action/action.h>
48 #include <lttng/channel-internal.hpp>
49 #include <lttng/channel.h>
50 #include <lttng/condition/condition-internal.hpp>
51 #include <lttng/condition/condition.h>
52 #include <lttng/condition/event-rule-matches-internal.hpp>
53 #include <lttng/condition/event-rule-matches.h>
54 #include <lttng/error-query-internal.hpp>
55 #include <lttng/event-internal.hpp>
56 #include <lttng/event-rule/event-rule-internal.hpp>
57 #include <lttng/event-rule/event-rule.h>
58 #include <lttng/location-internal.hpp>
59 #include <lttng/lttng-error.h>
60 #include <lttng/rotate-internal.hpp>
61 #include <lttng/session-descriptor-internal.hpp>
62 #include <lttng/session-internal.hpp>
63 #include <lttng/tracker.h>
64 #include <lttng/trigger/trigger-internal.hpp>
65 #include <lttng/userspace-probe-internal.hpp>
71 #include <urcu/list.h>
72 #include <urcu/uatomic.h>
74 /* Sleep for 100ms between each check for the shm path's deletion. */
75 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
77 namespace lsu
= lttng::sessiond::ust
;
79 static enum lttng_error_code
wait_on_path(void *path
);
82 struct cmd_destroy_session_reply_context
{
84 bool implicit_rotation_on_destroy
;
86 * Indicates whether or not an error occurred while launching the
87 * destruction of a session.
89 enum lttng_error_code destruction_status
;
93 * Command completion handler that is used by the destroy command
94 * when a session that has a non-default shm_path is being destroyed.
96 * See comment in cmd_destroy_session() for the rationale.
98 struct destroy_completion_handler
{
99 struct cmd_completion_handler handler
;
100 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
101 } destroy_completion_handler
= {
102 .handler
= { .run
= wait_on_path
, .data
= destroy_completion_handler
.shm_path
},
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 pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
113 uint64_t relayd_net_seq_idx
;
116 static struct cmd_completion_handler
*current_completion_handler
;
117 static int validate_ust_event_name(const char *);
118 static int cmd_enable_event_internal(struct ltt_session
*session
,
119 const struct lttng_domain
*domain
,
121 struct lttng_event
*event
,
122 char *filter_expression
,
123 struct lttng_bytecode
*filter
,
124 struct lttng_event_exclusion
*exclusion
,
126 static enum lttng_error_code
cmd_enable_channel_internal(struct ltt_session
*session
,
127 const struct lttng_domain
*domain
,
128 const struct lttng_channel
*_attr
,
132 * Create a session path used by list_lttng_sessions for the case that the
133 * session consumer is on the network.
135 static int build_network_session_path(char *dst
, size_t size
, struct ltt_session
*session
)
137 int ret
, kdata_port
, udata_port
;
138 struct lttng_uri
*kuri
= nullptr, *uuri
= nullptr, *uri
= nullptr;
139 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
141 LTTNG_ASSERT(session
);
144 memset(tmp_urls
, 0, sizeof(tmp_urls
));
145 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
147 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
149 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
150 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
151 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
154 if (session
->ust_session
&& session
->ust_session
->consumer
) {
155 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
156 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
159 if (uuri
== nullptr && kuri
== nullptr) {
160 uri
= &session
->consumer
->dst
.net
.control
;
161 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
162 } else if (kuri
&& uuri
) {
163 ret
= uri_compare(kuri
, uuri
);
167 /* Build uuri URL string */
168 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
175 } else if (kuri
&& uuri
== nullptr) {
177 } else if (uuri
&& kuri
== nullptr) {
181 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
187 * Do we have a UST url set. If yes, this means we have both kernel and UST
190 if (*tmp_uurl
!= '\0') {
193 "[K]: %s [data: %d] -- [U]: %s [data: %d]",
200 if (kuri
|| (!kuri
&& !uuri
)) {
203 /* No kernel URI, use the UST port. */
206 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
214 * Get run-time attributes if the session has been started (discarded events,
217 static int get_kernel_runtime_stats(struct ltt_session
*session
,
218 struct ltt_kernel_channel
*kchan
,
219 uint64_t *discarded_events
,
220 uint64_t *lost_packets
)
224 if (!session
->has_been_started
) {
226 *discarded_events
= 0;
231 ret
= consumer_get_discarded_events(
232 session
->id
, kchan
->key
, session
->kernel_session
->consumer
, discarded_events
);
237 ret
= consumer_get_lost_packets(
238 session
->id
, kchan
->key
, session
->kernel_session
->consumer
, lost_packets
);
248 * Get run-time attributes if the session has been started (discarded events,
251 static int get_ust_runtime_stats(struct ltt_session
*session
,
252 struct ltt_ust_channel
*uchan
,
253 uint64_t *discarded_events
,
254 uint64_t *lost_packets
)
257 struct ltt_ust_session
*usess
;
259 if (!discarded_events
|| !lost_packets
) {
264 usess
= session
->ust_session
;
265 LTTNG_ASSERT(discarded_events
);
266 LTTNG_ASSERT(lost_packets
);
268 if (!usess
|| !session
->has_been_started
) {
269 *discarded_events
= 0;
275 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
276 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
277 &usess
->buffer_reg_uid_list
,
280 uchan
->attr
.overwrite
,
283 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
284 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
287 uchan
->attr
.overwrite
,
293 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
294 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
296 ERR("Unsupported buffer type");
307 * Create a list of agent domain events.
309 * Return number of events in list on success or else a negative value.
311 static enum lttng_error_code
list_lttng_agent_events(struct agent
*agt
,
312 struct lttng_payload
*reply_payload
,
313 unsigned int *nb_events
)
315 enum lttng_error_code ret_code
;
317 unsigned int local_nb_events
= 0;
318 struct agent_event
*event
;
319 struct lttng_ht_iter iter
;
320 unsigned long agent_event_count
;
323 assert(reply_payload
);
325 DBG3("Listing agent events");
327 agent_event_count
= lttng_ht_get_count(agt
->events
);
328 if (agent_event_count
== 0) {
333 if (agent_event_count
> UINT_MAX
) {
334 ret_code
= LTTNG_ERR_OVERFLOW
;
338 local_nb_events
= (unsigned int) agent_event_count
;
341 lttng::urcu::read_lock_guard read_lock
;
343 cds_lfht_for_each_entry (agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
344 struct lttng_event
*tmp_event
= lttng_event_create();
347 ret_code
= LTTNG_ERR_NOMEM
;
351 if (lttng_strncpy(tmp_event
->name
, event
->name
, sizeof(tmp_event
->name
))) {
352 lttng_event_destroy(tmp_event
);
353 ret_code
= LTTNG_ERR_FATAL
;
357 tmp_event
->name
[sizeof(tmp_event
->name
) - 1] = '\0';
358 tmp_event
->enabled
= !!event
->enabled_count
;
359 tmp_event
->loglevel
= event
->loglevel_value
;
360 tmp_event
->loglevel_type
= event
->loglevel_type
;
362 ret
= lttng_event_serialize(tmp_event
,
365 event
->filter_expression
,
369 lttng_event_destroy(tmp_event
);
371 ret_code
= LTTNG_ERR_FATAL
;
378 *nb_events
= local_nb_events
;
384 * Create a list of ust global domain events.
386 static enum lttng_error_code
list_lttng_ust_global_events(char *channel_name
,
387 struct ltt_ust_domain_global
*ust_global
,
388 struct lttng_payload
*reply_payload
,
389 unsigned int *nb_events
)
391 enum lttng_error_code ret_code
;
393 struct lttng_ht_iter iter
;
394 struct lttng_ht_node_str
*node
;
395 struct ltt_ust_channel
*uchan
;
396 struct ltt_ust_event
*uevent
;
397 unsigned long channel_event_count
;
398 unsigned int local_nb_events
= 0;
400 assert(reply_payload
);
403 DBG("Listing UST global events for channel %s", channel_name
);
405 lttng::urcu::read_lock_guard read_lock
;
407 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
408 node
= lttng_ht_iter_get_node_str(&iter
);
409 if (node
== nullptr) {
410 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
414 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
416 channel_event_count
= lttng_ht_get_count(uchan
->events
);
417 if (channel_event_count
== 0) {
423 if (channel_event_count
> UINT_MAX
) {
424 ret_code
= LTTNG_ERR_OVERFLOW
;
428 local_nb_events
= (unsigned int) channel_event_count
;
430 DBG3("Listing UST global %d events", *nb_events
);
432 cds_lfht_for_each_entry (uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
433 struct lttng_event
*tmp_event
= nullptr;
435 if (uevent
->internal
) {
436 /* This event should remain hidden from clients */
441 tmp_event
= lttng_event_create();
443 ret_code
= LTTNG_ERR_NOMEM
;
447 if (lttng_strncpy(tmp_event
->name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
)) {
448 ret_code
= LTTNG_ERR_FATAL
;
449 lttng_event_destroy(tmp_event
);
453 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
454 tmp_event
->enabled
= uevent
->enabled
;
456 switch (uevent
->attr
.instrumentation
) {
457 case LTTNG_UST_ABI_TRACEPOINT
:
458 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
460 case LTTNG_UST_ABI_PROBE
:
461 tmp_event
->type
= LTTNG_EVENT_PROBE
;
463 case LTTNG_UST_ABI_FUNCTION
:
464 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
468 tmp_event
->loglevel
= uevent
->attr
.loglevel
;
469 switch (uevent
->attr
.loglevel_type
) {
470 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
471 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
473 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
474 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
476 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
477 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
480 if (uevent
->filter
) {
481 tmp_event
->filter
= 1;
483 if (uevent
->exclusion
) {
484 tmp_event
->exclusion
= 1;
487 std::vector
<const char *> exclusion_names
;
488 if (uevent
->exclusion
) {
489 for (int i
= 0; i
< uevent
->exclusion
->count
; i
++) {
490 exclusion_names
.emplace_back(
491 LTTNG_EVENT_EXCLUSION_NAME_AT(uevent
->exclusion
, i
));
496 * We do not care about the filter bytecode and the fd from the
497 * userspace_probe_location.
499 ret
= lttng_event_serialize(
501 exclusion_names
.size(),
502 exclusion_names
.size() ?
503 exclusion_names
.data() :
505 uevent
->filter_expression
,
509 lttng_event_destroy(tmp_event
);
511 ret_code
= LTTNG_ERR_FATAL
;
517 /* nb_events is already set at this point. */
519 *nb_events
= local_nb_events
;
525 * Fill lttng_event array of all kernel events in the channel.
527 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
528 struct ltt_kernel_session
*kernel_session
,
529 struct lttng_payload
*reply_payload
,
530 unsigned int *nb_events
)
532 enum lttng_error_code ret_code
;
534 struct ltt_kernel_event
*event
;
535 struct ltt_kernel_channel
*kchan
;
537 assert(reply_payload
);
539 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
540 if (kchan
== nullptr) {
541 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
545 *nb_events
= kchan
->event_count
;
547 DBG("Listing events for channel %s", kchan
->channel
->name
);
549 if (*nb_events
== 0) {
554 /* Kernel channels */
555 cds_list_for_each_entry (event
, &kchan
->events_list
.head
, list
) {
556 struct lttng_event
*tmp_event
= lttng_event_create();
559 ret_code
= LTTNG_ERR_NOMEM
;
563 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
564 lttng_event_destroy(tmp_event
);
565 ret_code
= LTTNG_ERR_FATAL
;
569 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
570 tmp_event
->enabled
= event
->enabled
;
571 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
573 switch (event
->event
->instrumentation
) {
574 case LTTNG_KERNEL_ABI_TRACEPOINT
:
575 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
577 case LTTNG_KERNEL_ABI_KRETPROBE
:
578 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
579 memcpy(&tmp_event
->attr
.probe
,
580 &event
->event
->u
.kprobe
,
581 sizeof(struct lttng_kernel_abi_kprobe
));
583 case LTTNG_KERNEL_ABI_KPROBE
:
584 tmp_event
->type
= LTTNG_EVENT_PROBE
;
585 memcpy(&tmp_event
->attr
.probe
,
586 &event
->event
->u
.kprobe
,
587 sizeof(struct lttng_kernel_abi_kprobe
));
589 case LTTNG_KERNEL_ABI_UPROBE
:
590 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
592 case LTTNG_KERNEL_ABI_FUNCTION
:
593 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
594 memcpy(&(tmp_event
->attr
.ftrace
),
595 &event
->event
->u
.ftrace
,
596 sizeof(struct lttng_kernel_abi_function
));
598 case LTTNG_KERNEL_ABI_NOOP
:
599 tmp_event
->type
= LTTNG_EVENT_NOOP
;
601 case LTTNG_KERNEL_ABI_SYSCALL
:
602 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
604 case LTTNG_KERNEL_ABI_ALL
:
611 if (event
->userspace_probe_location
) {
612 struct lttng_userspace_probe_location
*location_copy
=
613 lttng_userspace_probe_location_copy(
614 event
->userspace_probe_location
);
616 if (!location_copy
) {
617 lttng_event_destroy(tmp_event
);
618 ret_code
= LTTNG_ERR_NOMEM
;
622 ret
= lttng_event_set_userspace_probe_location(tmp_event
, location_copy
);
624 lttng_event_destroy(tmp_event
);
625 lttng_userspace_probe_location_destroy(location_copy
);
626 ret_code
= LTTNG_ERR_INVALID
;
631 ret
= lttng_event_serialize(
632 tmp_event
, 0, nullptr, event
->filter_expression
, 0, nullptr, reply_payload
);
633 lttng_event_destroy(tmp_event
);
635 ret_code
= LTTNG_ERR_FATAL
;
646 * Add URI so the consumer output object. Set the correct path depending on the
647 * domain adding the default trace directory.
649 static enum lttng_error_code
add_uri_to_consumer(const struct ltt_session
*session
,
650 struct consumer_output
*consumer
,
651 struct lttng_uri
*uri
,
652 enum lttng_domain_type domain
)
655 enum lttng_error_code ret_code
= LTTNG_OK
;
659 if (consumer
== nullptr) {
660 DBG("No consumer detected. Don't add URI. Stopping.");
661 ret_code
= LTTNG_ERR_NO_CONSUMER
;
666 case LTTNG_DOMAIN_KERNEL
:
667 ret
= lttng_strncpy(consumer
->domain_subdir
,
668 DEFAULT_KERNEL_TRACE_DIR
,
669 sizeof(consumer
->domain_subdir
));
671 case LTTNG_DOMAIN_UST
:
672 ret
= lttng_strncpy(consumer
->domain_subdir
,
673 DEFAULT_UST_TRACE_DIR
,
674 sizeof(consumer
->domain_subdir
));
678 * This case is possible is we try to add the URI to the global
679 * tracing session consumer object which in this case there is
682 memset(consumer
->domain_subdir
, 0, sizeof(consumer
->domain_subdir
));
686 ERR("Failed to initialize consumer output domain subdirectory");
687 ret_code
= LTTNG_ERR_FATAL
;
691 switch (uri
->dtype
) {
694 DBG2("Setting network URI to consumer");
696 if (consumer
->type
== CONSUMER_DST_NET
) {
697 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
698 consumer
->dst
.net
.control_isset
) ||
699 (uri
->stype
== LTTNG_STREAM_DATA
&& consumer
->dst
.net
.data_isset
)) {
700 ret_code
= LTTNG_ERR_URL_EXIST
;
704 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
707 /* Set URI into consumer output object */
708 ret
= consumer_set_network_uri(session
, consumer
, uri
);
710 ret_code
= (lttng_error_code
) -ret
;
712 } else if (ret
== 1) {
714 * URI was the same in the consumer so we do not append the subdir
715 * again so to not duplicate output dir.
722 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
723 ret_code
= LTTNG_ERR_INVALID
;
726 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
727 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
729 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
731 sizeof(consumer
->dst
.session_root_path
));
733 ret_code
= LTTNG_ERR_FATAL
;
736 consumer
->type
= CONSUMER_DST_LOCAL
;
746 * Init tracing by creating trace directory and sending fds kernel consumer.
748 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
751 struct lttng_ht_iter iter
;
752 struct consumer_socket
*socket
;
754 LTTNG_ASSERT(session
);
756 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= nullptr) {
757 lttng::urcu::read_lock_guard read_lock
;
759 cds_lfht_for_each_entry (
760 session
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
761 pthread_mutex_lock(socket
->lock
);
762 ret
= kernel_consumer_send_session(socket
, session
);
763 pthread_mutex_unlock(socket
->lock
);
765 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
776 * Create a socket to the relayd using the URI.
778 * On success, the relayd_sock pointer is set to the created socket.
779 * Else, it remains untouched and an LTTng error code is returned.
781 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
782 struct lttcomm_relayd_sock
**relayd_sock
,
783 struct consumer_output
*consumer
)
786 enum lttng_error_code status
= LTTNG_OK
;
787 struct lttcomm_relayd_sock
*rsock
;
789 rsock
= lttcomm_alloc_relayd_sock(
790 uri
, RELAYD_VERSION_COMM_MAJOR
, RELAYD_VERSION_COMM_MINOR
);
792 status
= LTTNG_ERR_FATAL
;
797 * Connect to relayd so we can proceed with a session creation. This call
798 * can possibly block for an arbitrary amount of time to set the health
799 * state to be in poll execution.
802 ret
= relayd_connect(rsock
);
805 ERR("Unable to reach lttng-relayd");
806 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
810 /* Create socket for control stream. */
811 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
812 uint64_t result_flags
;
814 DBG3("Creating relayd stream socket from URI");
816 /* Check relayd version */
817 ret
= relayd_version_check(rsock
);
818 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
819 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
821 } else if (ret
< 0) {
822 ERR("Unable to reach lttng-relayd");
823 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
826 consumer
->relay_major_version
= rsock
->major
;
827 consumer
->relay_minor_version
= rsock
->minor
;
828 ret
= relayd_get_configuration(rsock
, 0, &result_flags
);
830 ERR("Unable to get relayd configuration");
831 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
834 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
835 consumer
->relay_allows_clear
= true;
837 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
838 DBG3("Creating relayd data socket from URI");
840 /* Command is not valid */
841 ERR("Relayd invalid stream type: %d", uri
->stype
);
842 status
= LTTNG_ERR_INVALID
;
846 *relayd_sock
= rsock
;
851 /* The returned value is not useful since we are on an error path. */
852 (void) relayd_close(rsock
);
860 * Connect to the relayd using URI and send the socket to the right consumer.
862 * The consumer socket lock must be held by the caller.
864 * Returns LTTNG_OK on success or an LTTng error code on failure.
866 static enum lttng_error_code
send_consumer_relayd_socket(unsigned int session_id
,
867 struct lttng_uri
*relayd_uri
,
868 struct consumer_output
*consumer
,
869 struct consumer_socket
*consumer_sock
,
870 const char *session_name
,
871 const char *hostname
,
872 const char *base_path
,
873 int session_live_timer
,
874 const uint64_t *current_chunk_id
,
875 time_t session_creation_time
,
876 bool session_name_contains_creation_time
)
879 struct lttcomm_relayd_sock
*rsock
= nullptr;
880 enum lttng_error_code status
;
882 /* Connect to relayd and make version check if uri is the control. */
883 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
884 if (status
!= LTTNG_OK
) {
885 goto relayd_comm_error
;
889 /* Set the network sequence index if not set. */
890 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
891 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
893 * Increment net_seq_idx because we are about to transfer the
894 * new relayd socket to the consumer.
895 * Assign unique key so the consumer can match streams.
897 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
898 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
901 /* Send relayd socket to consumer. */
902 ret
= consumer_send_relayd_socket(consumer_sock
,
912 session_creation_time
,
913 session_name_contains_creation_time
);
915 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
919 /* Flag that the corresponding socket was sent. */
920 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
921 consumer_sock
->control_sock_sent
= 1;
922 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
923 consumer_sock
->data_sock_sent
= 1;
927 * Close socket which was dup on the consumer side. The session daemon does
928 * NOT keep track of the relayd socket(s) once transfer to the consumer.
932 if (status
!= LTTNG_OK
) {
934 * The consumer output for this session should not be used anymore
935 * since the relayd connection failed thus making any tracing or/and
936 * streaming not usable.
938 consumer
->enabled
= false;
940 (void) relayd_close(rsock
);
948 * Send both relayd sockets to a specific consumer and domain. This is a
949 * helper function to facilitate sending the information to the consumer for a
952 * The consumer socket lock must be held by the caller.
954 * Returns LTTNG_OK, or an LTTng error code on failure.
956 static enum lttng_error_code
send_consumer_relayd_sockets(unsigned int session_id
,
957 struct consumer_output
*consumer
,
958 struct consumer_socket
*sock
,
959 const char *session_name
,
960 const char *hostname
,
961 const char *base_path
,
962 int session_live_timer
,
963 const uint64_t *current_chunk_id
,
964 time_t session_creation_time
,
965 bool session_name_contains_creation_time
)
967 enum lttng_error_code status
= LTTNG_OK
;
969 LTTNG_ASSERT(consumer
);
972 /* Sending control relayd socket. */
973 if (!sock
->control_sock_sent
) {
974 status
= send_consumer_relayd_socket(session_id
,
975 &consumer
->dst
.net
.control
,
983 session_creation_time
,
984 session_name_contains_creation_time
);
985 if (status
!= LTTNG_OK
) {
990 /* Sending data relayd socket. */
991 if (!sock
->data_sock_sent
) {
992 status
= send_consumer_relayd_socket(session_id
,
993 &consumer
->dst
.net
.data
,
1001 session_creation_time
,
1002 session_name_contains_creation_time
);
1003 if (status
!= LTTNG_OK
) {
1013 * Setup relayd connections for a tracing session. First creates the socket to
1014 * the relayd and send them to the right domain consumer. Consumer type MUST be
1017 int cmd_setup_relayd(struct ltt_session
*session
)
1020 struct ltt_ust_session
*usess
;
1021 struct ltt_kernel_session
*ksess
;
1022 struct consumer_socket
*socket
;
1023 struct lttng_ht_iter iter
;
1024 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1026 LTTNG_ASSERT(session
);
1028 usess
= session
->ust_session
;
1029 ksess
= session
->kernel_session
;
1031 DBG("Setting relayd for session %s", session
->name
);
1033 if (session
->current_trace_chunk
) {
1034 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1035 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1037 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1038 current_chunk_id
.is_set
= true;
1040 ERR("Failed to get current trace chunk id");
1041 ret
= LTTNG_ERR_UNK
;
1046 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
&&
1047 usess
->consumer
->enabled
) {
1048 /* For each consumer socket, send relayd sockets */
1049 lttng::urcu::read_lock_guard read_lock
;
1051 cds_lfht_for_each_entry (
1052 usess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1053 pthread_mutex_lock(socket
->lock
);
1054 ret
= send_consumer_relayd_sockets(
1061 session
->live_timer
,
1062 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
1063 session
->creation_time
,
1064 session
->name_contains_creation_time
);
1065 pthread_mutex_unlock(socket
->lock
);
1066 if (ret
!= LTTNG_OK
) {
1069 /* Session is now ready for network streaming. */
1070 session
->net_handle
= 1;
1073 session
->consumer
->relay_major_version
= usess
->consumer
->relay_major_version
;
1074 session
->consumer
->relay_minor_version
= usess
->consumer
->relay_minor_version
;
1075 session
->consumer
->relay_allows_clear
= usess
->consumer
->relay_allows_clear
;
1078 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
&&
1079 ksess
->consumer
->enabled
) {
1080 lttng::urcu::read_lock_guard read_lock
;
1082 cds_lfht_for_each_entry (
1083 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1084 pthread_mutex_lock(socket
->lock
);
1085 ret
= send_consumer_relayd_sockets(
1092 session
->live_timer
,
1093 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
1094 session
->creation_time
,
1095 session
->name_contains_creation_time
);
1096 pthread_mutex_unlock(socket
->lock
);
1097 if (ret
!= LTTNG_OK
) {
1100 /* Session is now ready for network streaming. */
1101 session
->net_handle
= 1;
1104 session
->consumer
->relay_major_version
= ksess
->consumer
->relay_major_version
;
1105 session
->consumer
->relay_minor_version
= ksess
->consumer
->relay_minor_version
;
1106 session
->consumer
->relay_allows_clear
= ksess
->consumer
->relay_allows_clear
;
1114 * Start a kernel session by opening all necessary streams.
1116 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1119 struct ltt_kernel_channel
*kchan
;
1121 /* Open kernel metadata */
1122 if (ksess
->metadata
== nullptr && ksess
->output_traces
) {
1123 ret
= kernel_open_metadata(ksess
);
1125 ret
= LTTNG_ERR_KERN_META_FAIL
;
1130 /* Open kernel metadata stream */
1131 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1132 ret
= kernel_open_metadata_stream(ksess
);
1134 ERR("Kernel create metadata stream failed");
1135 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1140 /* For each channel */
1141 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1142 if (kchan
->stream_count
== 0) {
1143 ret
= kernel_open_channel_stream(kchan
);
1145 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1148 /* Update the stream global counter */
1149 ksess
->stream_count_global
+= ret
;
1153 /* Setup kernel consumer socket and send fds to it */
1154 ret
= init_kernel_tracing(ksess
);
1156 ret
= LTTNG_ERR_KERN_START_FAIL
;
1160 /* This start the kernel tracing */
1161 ret
= kernel_start_session(ksess
);
1163 ret
= LTTNG_ERR_KERN_START_FAIL
;
1167 /* Quiescent wait after starting trace */
1168 kernel_wait_quiescent();
1170 ksess
->active
= true;
1178 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1180 struct ltt_kernel_channel
*kchan
;
1181 bool error_occurred
= false;
1184 if (!ksess
|| !ksess
->active
) {
1187 DBG("Stopping kernel tracing");
1189 ret
= kernel_stop_session(ksess
);
1191 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1195 kernel_wait_quiescent();
1197 /* Flush metadata after stopping (if exists) */
1198 if (ksess
->metadata_stream_fd
>= 0) {
1199 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1201 ERR("Kernel metadata flush failed");
1202 error_occurred
= true;
1206 /* Flush all buffers after stopping */
1207 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1208 ret
= kernel_flush_buffer(kchan
);
1210 ERR("Kernel flush buffer error");
1211 error_occurred
= true;
1215 ksess
->active
= false;
1216 if (error_occurred
) {
1217 ret
= LTTNG_ERR_UNK
;
1226 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1228 int cmd_disable_channel(struct ltt_session
*session
,
1229 enum lttng_domain_type domain
,
1233 struct ltt_ust_session
*usess
;
1235 usess
= session
->ust_session
;
1237 lttng::urcu::read_lock_guard read_lock
;
1240 case LTTNG_DOMAIN_KERNEL
:
1242 ret
= channel_kernel_disable(session
->kernel_session
, channel_name
);
1243 if (ret
!= LTTNG_OK
) {
1247 kernel_wait_quiescent();
1250 case LTTNG_DOMAIN_UST
:
1252 struct ltt_ust_channel
*uchan
;
1253 struct lttng_ht
*chan_ht
;
1255 chan_ht
= usess
->domain_global
.channels
;
1257 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1258 if (uchan
== nullptr) {
1259 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1263 ret
= channel_ust_disable(usess
, uchan
);
1264 if (ret
!= LTTNG_OK
) {
1270 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1281 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1283 * The wpipe arguments is used as a notifier for the kernel thread.
1285 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1289 ssize_t sock_recv_len
;
1290 struct lttng_channel
*channel
= nullptr;
1291 struct lttng_buffer_view view
;
1292 struct lttng_dynamic_buffer channel_buffer
;
1293 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1295 lttng_dynamic_buffer_init(&channel_buffer
);
1296 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1297 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1299 ret
= LTTNG_ERR_NOMEM
;
1303 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
, channel_len
);
1304 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1305 ERR("Failed to receive \"enable channel\" command payload");
1306 ret
= LTTNG_ERR_INVALID
;
1310 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1311 if (!lttng_buffer_view_is_valid(&view
)) {
1312 ret
= LTTNG_ERR_INVALID
;
1316 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1317 ERR("Invalid channel payload received in \"enable channel\" command");
1318 ret
= LTTNG_ERR_INVALID
;
1322 ret
= cmd_enable_channel_internal(cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1325 lttng_dynamic_buffer_reset(&channel_buffer
);
1326 lttng_channel_destroy(channel
);
1330 static enum lttng_error_code
cmd_enable_channel_internal(struct ltt_session
*session
,
1331 const struct lttng_domain
*domain
,
1332 const struct lttng_channel
*_attr
,
1335 enum lttng_error_code ret_code
;
1336 struct ltt_ust_session
*usess
= session
->ust_session
;
1337 struct lttng_ht
*chan_ht
;
1339 struct lttng_channel
*attr
= nullptr;
1341 LTTNG_ASSERT(session
);
1342 LTTNG_ASSERT(_attr
);
1343 LTTNG_ASSERT(domain
);
1345 lttng::urcu::read_lock_guard read_lock
;
1347 attr
= lttng_channel_copy(_attr
);
1349 ret_code
= LTTNG_ERR_NOMEM
;
1353 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1355 /* Validate channel name */
1356 if (attr
->name
[0] == '.' || memchr(attr
->name
, '/', len
) != nullptr) {
1357 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1361 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1364 * If the session is a live session, remove the switch timer, the
1365 * live timer does the same thing but sends also synchronisation
1366 * beacons for inactive streams.
1368 if (session
->live_timer
> 0) {
1369 attr
->attr
.live_timer_interval
= session
->live_timer
;
1370 attr
->attr
.switch_timer_interval
= 0;
1373 /* Check for feature support */
1374 switch (domain
->type
) {
1375 case LTTNG_DOMAIN_KERNEL
:
1377 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1378 /* Sampling position of buffer is not supported */
1379 WARN("Kernel tracer does not support buffer monitoring. "
1380 "Setting the monitor interval timer to 0 "
1381 "(disabled) for channel '%s' of session '%s'",
1384 lttng_channel_set_monitor_timer_interval(attr
, 0);
1388 case LTTNG_DOMAIN_UST
:
1390 case LTTNG_DOMAIN_JUL
:
1391 case LTTNG_DOMAIN_LOG4J
:
1392 case LTTNG_DOMAIN_PYTHON
:
1393 if (!agent_tracing_is_enabled()) {
1394 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1395 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1400 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1404 switch (domain
->type
) {
1405 case LTTNG_DOMAIN_KERNEL
:
1407 struct ltt_kernel_channel
*kchan
;
1409 kchan
= trace_kernel_get_channel_by_name(attr
->name
, session
->kernel_session
);
1410 if (kchan
== nullptr) {
1412 * Don't try to create a channel if the session has been started at
1413 * some point in time before. The tracer does not allow it.
1415 if (session
->has_been_started
) {
1416 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1420 if (session
->snapshot
.nb_output
> 0 || session
->snapshot_mode
) {
1421 /* Enforce mmap output for snapshot sessions. */
1422 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1424 ret_code
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1425 if (attr
->name
[0] != '\0') {
1426 session
->kernel_session
->has_non_default_channel
= 1;
1429 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1432 if (ret_code
!= LTTNG_OK
) {
1436 kernel_wait_quiescent();
1439 case LTTNG_DOMAIN_UST
:
1440 case LTTNG_DOMAIN_JUL
:
1441 case LTTNG_DOMAIN_LOG4J
:
1442 case LTTNG_DOMAIN_PYTHON
:
1444 struct ltt_ust_channel
*uchan
;
1449 * Current agent implementation limitations force us to allow
1450 * only one channel at once in "agent" subdomains. Each
1451 * subdomain has a default channel name which must be strictly
1454 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1455 if (strncmp(attr
->name
,
1456 DEFAULT_JUL_CHANNEL_NAME
,
1457 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1458 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1461 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1462 if (strncmp(attr
->name
,
1463 DEFAULT_LOG4J_CHANNEL_NAME
,
1464 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1465 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1468 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1469 if (strncmp(attr
->name
,
1470 DEFAULT_PYTHON_CHANNEL_NAME
,
1471 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1472 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1477 chan_ht
= usess
->domain_global
.channels
;
1479 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1480 if (uchan
== nullptr) {
1482 * Don't try to create a channel if the session has been started at
1483 * some point in time before. The tracer does not allow it.
1485 if (session
->has_been_started
) {
1486 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1490 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1491 if (attr
->name
[0] != '\0') {
1492 usess
->has_non_default_channel
= 1;
1495 ret_code
= channel_ust_enable(usess
, uchan
);
1500 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1504 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1505 session
->has_non_mmap_channel
= true;
1509 lttng_channel_destroy(attr
);
1513 enum lttng_error_code
1514 cmd_process_attr_tracker_get_tracking_policy(struct ltt_session
*session
,
1515 enum lttng_domain_type domain
,
1516 enum lttng_process_attr process_attr
,
1517 enum lttng_tracking_policy
*policy
)
1519 enum lttng_error_code ret_code
= LTTNG_OK
;
1520 const struct process_attr_tracker
*tracker
;
1523 case LTTNG_DOMAIN_KERNEL
:
1524 if (!session
->kernel_session
) {
1525 ret_code
= LTTNG_ERR_INVALID
;
1528 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1530 case LTTNG_DOMAIN_UST
:
1531 if (!session
->ust_session
) {
1532 ret_code
= LTTNG_ERR_INVALID
;
1535 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1538 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1542 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1544 ret_code
= LTTNG_ERR_INVALID
;
1550 enum lttng_error_code
1551 cmd_process_attr_tracker_set_tracking_policy(struct ltt_session
*session
,
1552 enum lttng_domain_type domain
,
1553 enum lttng_process_attr process_attr
,
1554 enum lttng_tracking_policy policy
)
1556 enum lttng_error_code ret_code
= LTTNG_OK
;
1559 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1560 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1561 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1564 ret_code
= LTTNG_ERR_INVALID
;
1569 case LTTNG_DOMAIN_KERNEL
:
1570 if (!session
->kernel_session
) {
1571 ret_code
= LTTNG_ERR_INVALID
;
1574 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1575 session
->kernel_session
, process_attr
, policy
);
1577 case LTTNG_DOMAIN_UST
:
1578 if (!session
->ust_session
) {
1579 ret_code
= LTTNG_ERR_INVALID
;
1582 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1583 session
->ust_session
, process_attr
, policy
);
1586 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1593 enum lttng_error_code
1594 cmd_process_attr_tracker_inclusion_set_add_value(struct ltt_session
*session
,
1595 enum lttng_domain_type domain
,
1596 enum lttng_process_attr process_attr
,
1597 const struct process_attr_value
*value
)
1599 enum lttng_error_code ret_code
= LTTNG_OK
;
1602 case LTTNG_DOMAIN_KERNEL
:
1603 if (!session
->kernel_session
) {
1604 ret_code
= LTTNG_ERR_INVALID
;
1607 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1608 session
->kernel_session
, process_attr
, value
);
1610 case LTTNG_DOMAIN_UST
:
1611 if (!session
->ust_session
) {
1612 ret_code
= LTTNG_ERR_INVALID
;
1615 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1616 session
->ust_session
, process_attr
, value
);
1619 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1626 enum lttng_error_code
1627 cmd_process_attr_tracker_inclusion_set_remove_value(struct ltt_session
*session
,
1628 enum lttng_domain_type domain
,
1629 enum lttng_process_attr process_attr
,
1630 const struct process_attr_value
*value
)
1632 enum lttng_error_code ret_code
= LTTNG_OK
;
1635 case LTTNG_DOMAIN_KERNEL
:
1636 if (!session
->kernel_session
) {
1637 ret_code
= LTTNG_ERR_INVALID
;
1640 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1641 session
->kernel_session
, process_attr
, value
);
1643 case LTTNG_DOMAIN_UST
:
1644 if (!session
->ust_session
) {
1645 ret_code
= LTTNG_ERR_INVALID
;
1648 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1649 session
->ust_session
, process_attr
, value
);
1652 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1659 enum lttng_error_code
1660 cmd_process_attr_tracker_get_inclusion_set(struct ltt_session
*session
,
1661 enum lttng_domain_type domain
,
1662 enum lttng_process_attr process_attr
,
1663 struct lttng_process_attr_values
**values
)
1665 enum lttng_error_code ret_code
= LTTNG_OK
;
1666 const struct process_attr_tracker
*tracker
;
1667 enum process_attr_tracker_status status
;
1670 case LTTNG_DOMAIN_KERNEL
:
1671 if (!session
->kernel_session
) {
1672 ret_code
= LTTNG_ERR_INVALID
;
1675 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1677 case LTTNG_DOMAIN_UST
:
1678 if (!session
->ust_session
) {
1679 ret_code
= LTTNG_ERR_INVALID
;
1682 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1685 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1690 ret_code
= LTTNG_ERR_INVALID
;
1694 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1696 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1697 ret_code
= LTTNG_OK
;
1699 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1700 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1702 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1703 ret_code
= LTTNG_ERR_NOMEM
;
1706 ret_code
= LTTNG_ERR_UNK
;
1715 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1717 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1718 struct lttng_event
*event
,
1719 char *filter_expression
,
1720 struct lttng_bytecode
*bytecode
,
1721 struct lttng_event_exclusion
*exclusion
)
1724 const char *event_name
;
1725 const struct ltt_session
*session
= cmd_ctx
->session
;
1726 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1727 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1729 DBG("Disable event command for event \'%s\'", event
->name
);
1732 * Filter and exclusions are simply not handled by the
1733 * disable event command at this time.
1737 (void) filter_expression
;
1740 /* Ignore the presence of filter or exclusion for the event */
1742 event
->exclusion
= 0;
1744 event_name
= event
->name
;
1746 lttng::urcu::read_lock_guard read_lock
;
1748 /* Error out on unhandled search criteria */
1749 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
|| event
->pid
||
1750 event
->filter
|| event
->exclusion
) {
1751 ret
= LTTNG_ERR_UNK
;
1756 case LTTNG_DOMAIN_KERNEL
:
1758 struct ltt_kernel_channel
*kchan
;
1759 struct ltt_kernel_session
*ksess
;
1761 ksess
= session
->kernel_session
;
1764 * If a non-default channel has been created in the
1765 * session, explicitely require that -c chan_name needs
1768 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1769 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1773 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1774 if (kchan
== nullptr) {
1775 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1779 switch (event
->type
) {
1780 case LTTNG_EVENT_ALL
:
1781 case LTTNG_EVENT_TRACEPOINT
:
1782 case LTTNG_EVENT_SYSCALL
:
1783 case LTTNG_EVENT_PROBE
:
1784 case LTTNG_EVENT_FUNCTION
:
1785 case LTTNG_EVENT_FUNCTION_ENTRY
: /* fall-through */
1786 if (event_name
[0] == '\0') {
1787 ret
= event_kernel_disable_event(kchan
, nullptr, event
->type
);
1789 ret
= event_kernel_disable_event(kchan
, event_name
, event
->type
);
1791 if (ret
!= LTTNG_OK
) {
1796 ret
= LTTNG_ERR_UNK
;
1800 kernel_wait_quiescent();
1803 case LTTNG_DOMAIN_UST
:
1805 struct ltt_ust_channel
*uchan
;
1806 struct ltt_ust_session
*usess
;
1808 usess
= session
->ust_session
;
1810 if (validate_ust_event_name(event_name
)) {
1811 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1816 * If a non-default channel has been created in the
1817 * session, explicitly require that -c chan_name needs
1820 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1821 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1825 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
1826 if (uchan
== nullptr) {
1827 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1831 switch (event
->type
) {
1832 case LTTNG_EVENT_ALL
:
1834 * An empty event name means that everything
1835 * should be disabled.
1837 if (event
->name
[0] == '\0') {
1838 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1840 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1842 if (ret
!= LTTNG_OK
) {
1847 ret
= LTTNG_ERR_UNK
;
1851 DBG3("Disable UST event %s in channel %s completed", event_name
, channel_name
);
1854 case LTTNG_DOMAIN_LOG4J
:
1855 case LTTNG_DOMAIN_JUL
:
1856 case LTTNG_DOMAIN_PYTHON
:
1859 struct ltt_ust_session
*usess
= session
->ust_session
;
1861 LTTNG_ASSERT(usess
);
1863 switch (event
->type
) {
1864 case LTTNG_EVENT_ALL
:
1867 ret
= LTTNG_ERR_UNK
;
1871 agt
= trace_ust_find_agent(usess
, domain
);
1873 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1877 * An empty event name means that everything
1878 * should be disabled.
1880 if (event
->name
[0] == '\0') {
1881 ret
= event_agent_disable_all(usess
, agt
);
1883 ret
= event_agent_disable(usess
, agt
, event_name
);
1885 if (ret
!= LTTNG_OK
) {
1892 ret
= LTTNG_ERR_UND
;
1902 free(filter_expression
);
1907 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1909 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1910 const struct lttng_event_context
*event_context
,
1913 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1914 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1915 const struct ltt_session
*session
= cmd_ctx
->session
;
1916 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1919 * Don't try to add a context if the session has been started at
1920 * some point in time before. The tracer does not allow it and would
1921 * result in a corrupted trace.
1923 if (cmd_ctx
->session
->has_been_started
) {
1924 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1929 case LTTNG_DOMAIN_KERNEL
:
1930 LTTNG_ASSERT(session
->kernel_session
);
1932 if (session
->kernel_session
->channel_count
== 0) {
1933 /* Create default channel */
1934 ret
= channel_kernel_create(session
->kernel_session
, nullptr, kwpipe
);
1935 if (ret
!= LTTNG_OK
) {
1938 chan_kern_created
= 1;
1940 /* Add kernel context to kernel tracer */
1941 ret
= context_kernel_add(session
->kernel_session
, event_context
, channel_name
);
1942 if (ret
!= LTTNG_OK
) {
1946 case LTTNG_DOMAIN_JUL
:
1947 case LTTNG_DOMAIN_LOG4J
:
1950 * Validate channel name.
1951 * If no channel name is given and the domain is JUL or LOG4J,
1952 * set it to the appropriate domain-specific channel name. If
1953 * a name is provided but does not match the expexted channel
1954 * name, return an error.
1956 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1957 strcmp(channel_name
, DEFAULT_JUL_CHANNEL_NAME
) != 0) {
1958 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1960 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1961 strcmp(channel_name
, DEFAULT_LOG4J_CHANNEL_NAME
) != 0) {
1962 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1967 case LTTNG_DOMAIN_UST
:
1969 struct ltt_ust_session
*usess
= session
->ust_session
;
1970 unsigned int chan_count
;
1972 LTTNG_ASSERT(usess
);
1974 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1975 if (chan_count
== 0) {
1976 struct lttng_channel
*attr
;
1977 /* Create default channel */
1978 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1979 if (attr
== nullptr) {
1980 ret
= LTTNG_ERR_FATAL
;
1984 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1985 if (ret
!= LTTNG_OK
) {
1989 channel_attr_destroy(attr
);
1990 chan_ust_created
= 1;
1993 ret
= context_ust_add(usess
, domain
, event_context
, channel_name
);
1994 if (ret
!= LTTNG_OK
) {
2000 ret
= LTTNG_ERR_UND
;
2008 if (chan_kern_created
) {
2009 struct ltt_kernel_channel
*kchan
= trace_kernel_get_channel_by_name(
2010 DEFAULT_CHANNEL_NAME
, session
->kernel_session
);
2011 /* Created previously, this should NOT fail. */
2012 LTTNG_ASSERT(kchan
);
2013 kernel_destroy_channel(kchan
);
2016 if (chan_ust_created
) {
2017 struct ltt_ust_channel
*uchan
= trace_ust_find_channel_by_name(
2018 session
->ust_session
->domain_global
.channels
, DEFAULT_CHANNEL_NAME
);
2019 /* Created previously, this should NOT fail. */
2020 LTTNG_ASSERT(uchan
);
2021 /* Remove from the channel list of the session. */
2022 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
, uchan
);
2023 trace_ust_destroy_channel(uchan
);
2029 static inline bool name_starts_with(const char *name
, const char *prefix
)
2031 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2033 return !strncmp(name
, prefix
, max_cmp_len
);
2036 /* Perform userspace-specific event name validation */
2037 static int validate_ust_event_name(const char *name
)
2047 * Check name against all internal UST event component namespaces used
2050 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2051 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2052 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2061 * Internal version of cmd_enable_event() with a supplemental
2062 * "internal_event" flag which is used to enable internal events which should
2063 * be hidden from clients. Such events are used in the agent implementation to
2064 * enable the events through which all "agent" events are funeled.
2066 static int _cmd_enable_event(struct ltt_session
*session
,
2067 const struct lttng_domain
*domain
,
2069 struct lttng_event
*event
,
2070 char *filter_expression
,
2071 struct lttng_bytecode
*filter
,
2072 struct lttng_event_exclusion
*exclusion
,
2074 bool internal_event
)
2076 int ret
= 0, channel_created
= 0;
2077 struct lttng_channel
*attr
= nullptr;
2079 LTTNG_ASSERT(session
);
2080 LTTNG_ASSERT(event
);
2081 LTTNG_ASSERT(channel_name
);
2083 /* If we have a filter, we must have its filter expression */
2084 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2086 /* Normalize event name as a globbing pattern */
2087 strutils_normalize_star_glob_pattern(event
->name
);
2089 /* Normalize exclusion names as globbing patterns */
2093 for (i
= 0; i
< exclusion
->count
; i
++) {
2094 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2096 strutils_normalize_star_glob_pattern(name
);
2100 DBG("Enable event command for event \'%s\'", event
->name
);
2102 lttng::urcu::read_lock_guard read_lock
;
2104 switch (domain
->type
) {
2105 case LTTNG_DOMAIN_KERNEL
:
2107 struct ltt_kernel_channel
*kchan
;
2110 * If a non-default channel has been created in the
2111 * session, explicitely require that -c chan_name needs
2114 if (session
->kernel_session
->has_non_default_channel
&& channel_name
[0] == '\0') {
2115 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2119 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2120 if (kchan
== nullptr) {
2121 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
, LTTNG_BUFFER_GLOBAL
);
2122 if (attr
== nullptr) {
2123 ret
= LTTNG_ERR_FATAL
;
2126 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2127 ret
= LTTNG_ERR_INVALID
;
2131 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2132 if (ret
!= LTTNG_OK
) {
2135 channel_created
= 1;
2138 /* Get the newly created kernel channel pointer */
2139 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2140 if (kchan
== nullptr) {
2141 /* This sould not happen... */
2142 ret
= LTTNG_ERR_FATAL
;
2146 switch (event
->type
) {
2147 case LTTNG_EVENT_ALL
:
2149 char *filter_expression_a
= nullptr;
2150 struct lttng_bytecode
*filter_a
= nullptr;
2153 * We need to duplicate filter_expression and filter,
2154 * because ownership is passed to first enable
2157 if (filter_expression
) {
2158 filter_expression_a
= strdup(filter_expression
);
2159 if (!filter_expression_a
) {
2160 ret
= LTTNG_ERR_FATAL
;
2165 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2167 free(filter_expression_a
);
2168 ret
= LTTNG_ERR_FATAL
;
2171 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2173 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2174 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2175 /* We have passed ownership */
2176 filter_expression
= nullptr;
2178 if (ret
!= LTTNG_OK
) {
2179 if (channel_created
) {
2180 /* Let's not leak a useless channel. */
2181 kernel_destroy_channel(kchan
);
2183 free(filter_expression_a
);
2187 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2188 ret
= event_kernel_enable_event(
2189 kchan
, event
, filter_expression_a
, filter_a
);
2190 /* We have passed ownership */
2191 filter_expression_a
= nullptr;
2193 if (ret
!= LTTNG_OK
) {
2198 case LTTNG_EVENT_PROBE
:
2199 case LTTNG_EVENT_USERSPACE_PROBE
:
2200 case LTTNG_EVENT_FUNCTION
:
2201 case LTTNG_EVENT_FUNCTION_ENTRY
:
2202 case LTTNG_EVENT_TRACEPOINT
:
2203 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2204 /* We have passed ownership */
2205 filter_expression
= nullptr;
2207 if (ret
!= LTTNG_OK
) {
2208 if (channel_created
) {
2209 /* Let's not leak a useless channel. */
2210 kernel_destroy_channel(kchan
);
2215 case LTTNG_EVENT_SYSCALL
:
2216 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2217 /* We have passed ownership */
2218 filter_expression
= nullptr;
2220 if (ret
!= LTTNG_OK
) {
2225 ret
= LTTNG_ERR_UNK
;
2229 kernel_wait_quiescent();
2232 case LTTNG_DOMAIN_UST
:
2234 struct ltt_ust_channel
*uchan
;
2235 struct ltt_ust_session
*usess
= session
->ust_session
;
2237 LTTNG_ASSERT(usess
);
2240 * If a non-default channel has been created in the
2241 * session, explicitely require that -c chan_name needs
2244 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2245 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2249 /* Get channel from global UST domain */
2250 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
2251 if (uchan
== nullptr) {
2252 /* Create default channel */
2253 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, usess
->buffer_type
);
2254 if (attr
== nullptr) {
2255 ret
= LTTNG_ERR_FATAL
;
2258 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2259 ret
= LTTNG_ERR_INVALID
;
2263 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2264 if (ret
!= LTTNG_OK
) {
2268 /* Get the newly created channel reference back */
2269 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2271 LTTNG_ASSERT(uchan
);
2274 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2276 * Don't allow users to add UST events to channels which
2277 * are assigned to a userspace subdomain (JUL, Log4J,
2280 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2284 if (!internal_event
) {
2286 * Ensure the event name is not reserved for internal
2289 ret
= validate_ust_event_name(event
->name
);
2291 WARN("Userspace event name %s failed validation.", event
->name
);
2292 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2297 /* At this point, the session and channel exist on the tracer */
2298 ret
= event_ust_enable_tracepoint(
2299 usess
, uchan
, event
, filter_expression
, filter
, exclusion
, internal_event
);
2300 /* We have passed ownership */
2301 filter_expression
= nullptr;
2303 exclusion
= nullptr;
2304 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2305 goto already_enabled
;
2306 } else if (ret
!= LTTNG_OK
) {
2311 case LTTNG_DOMAIN_LOG4J
:
2312 case LTTNG_DOMAIN_JUL
:
2313 case LTTNG_DOMAIN_PYTHON
:
2315 const char *default_event_name
, *default_chan_name
;
2317 struct lttng_event uevent
;
2318 struct lttng_domain tmp_dom
;
2319 struct ltt_ust_session
*usess
= session
->ust_session
;
2321 LTTNG_ASSERT(usess
);
2323 if (!agent_tracing_is_enabled()) {
2324 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2325 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2329 agt
= trace_ust_find_agent(usess
, domain
->type
);
2331 agt
= agent_create(domain
->type
);
2333 ret
= LTTNG_ERR_NOMEM
;
2336 agent_add(agt
, usess
->agents
);
2339 /* Create the default tracepoint. */
2340 memset(&uevent
, 0, sizeof(uevent
));
2341 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2342 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2343 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
2344 if (!default_event_name
) {
2345 ret
= LTTNG_ERR_FATAL
;
2348 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2349 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2352 * The domain type is changed because we are about to enable the
2353 * default channel and event for the JUL domain that are hardcoded.
2354 * This happens in the UST domain.
2356 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2357 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2359 switch (domain
->type
) {
2360 case LTTNG_DOMAIN_LOG4J
:
2361 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2363 case LTTNG_DOMAIN_JUL
:
2364 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2366 case LTTNG_DOMAIN_PYTHON
:
2367 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2370 /* The switch/case we are in makes this impossible */
2375 char *filter_expression_copy
= nullptr;
2376 struct lttng_bytecode
*filter_copy
= nullptr;
2379 const size_t filter_size
=
2380 sizeof(struct lttng_bytecode
) + filter
->len
;
2382 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2384 ret
= LTTNG_ERR_NOMEM
;
2387 memcpy(filter_copy
, filter
, filter_size
);
2389 filter_expression_copy
= strdup(filter_expression
);
2390 if (!filter_expression
) {
2391 ret
= LTTNG_ERR_NOMEM
;
2394 if (!filter_expression_copy
|| !filter_copy
) {
2395 free(filter_expression_copy
);
2401 ret
= cmd_enable_event_internal(session
,
2403 (char *) default_chan_name
,
2405 filter_expression_copy
,
2411 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2412 goto already_enabled
;
2413 } else if (ret
!= LTTNG_OK
) {
2417 /* The wild card * means that everything should be enabled. */
2418 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2419 ret
= event_agent_enable_all(usess
, agt
, event
, filter
, filter_expression
);
2421 ret
= event_agent_enable(usess
, agt
, event
, filter
, filter_expression
);
2424 filter_expression
= nullptr;
2425 if (ret
!= LTTNG_OK
) {
2432 ret
= LTTNG_ERR_UND
;
2440 free(filter_expression
);
2443 channel_attr_destroy(attr
);
2448 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2449 * We own filter, exclusion, and filter_expression.
2451 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2452 struct lttng_event
*event
,
2453 char *filter_expression
,
2454 struct lttng_event_exclusion
*exclusion
,
2455 struct lttng_bytecode
*bytecode
,
2460 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2462 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2465 * The ownership of the following parameters is transferred to
2466 * _cmd_enable_event:
2468 * - filter_expression,
2472 ret
= _cmd_enable_event(cmd_ctx
->session
,
2474 cmd_ctx
->lsm
.u
.enable
.channel_name
,
2481 filter_expression
= nullptr;
2483 exclusion
= nullptr;
2488 * Enable an event which is internal to LTTng. An internal should
2489 * never be made visible to clients and are immune to checks such as
2492 static int cmd_enable_event_internal(struct ltt_session
*session
,
2493 const struct lttng_domain
*domain
,
2495 struct lttng_event
*event
,
2496 char *filter_expression
,
2497 struct lttng_bytecode
*filter
,
2498 struct lttng_event_exclusion
*exclusion
,
2501 return _cmd_enable_event(session
,
2513 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2515 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2516 struct lttng_payload
*reply_payload
)
2518 enum lttng_error_code ret_code
;
2520 ssize_t i
, nb_events
= 0;
2521 struct lttng_event
*events
= nullptr;
2522 struct lttcomm_list_command_header reply_command_header
= {};
2523 size_t reply_command_header_offset
;
2525 assert(reply_payload
);
2527 /* Reserve space for command reply header. */
2528 reply_command_header_offset
= reply_payload
->buffer
.size
;
2529 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2530 reply_command_header_offset
+
2531 sizeof(struct lttcomm_list_command_header
));
2533 ret_code
= LTTNG_ERR_NOMEM
;
2538 case LTTNG_DOMAIN_KERNEL
:
2539 nb_events
= kernel_list_events(&events
);
2540 if (nb_events
< 0) {
2541 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2545 case LTTNG_DOMAIN_UST
:
2546 nb_events
= ust_app_list_events(&events
);
2547 if (nb_events
< 0) {
2548 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2552 case LTTNG_DOMAIN_LOG4J
:
2553 case LTTNG_DOMAIN_JUL
:
2554 case LTTNG_DOMAIN_PYTHON
:
2555 nb_events
= agent_list_events(&events
, domain
);
2556 if (nb_events
< 0) {
2557 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2562 ret_code
= LTTNG_ERR_UND
;
2566 for (i
= 0; i
< nb_events
; i
++) {
2567 ret
= lttng_event_serialize(
2568 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2570 ret_code
= LTTNG_ERR_NOMEM
;
2575 if (nb_events
> UINT32_MAX
) {
2576 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2577 ret_code
= LTTNG_ERR_OVERFLOW
;
2581 /* Update command reply header. */
2582 reply_command_header
.count
= (uint32_t) nb_events
;
2583 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2584 &reply_command_header
,
2585 sizeof(reply_command_header
));
2587 ret_code
= LTTNG_OK
;
2594 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2596 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2597 struct lttng_payload
*reply
)
2599 enum lttng_error_code ret_code
;
2601 unsigned int i
, nb_fields
;
2602 struct lttng_event_field
*fields
= nullptr;
2603 struct lttcomm_list_command_header reply_command_header
= {};
2604 size_t reply_command_header_offset
;
2608 /* Reserve space for command reply header. */
2609 reply_command_header_offset
= reply
->buffer
.size
;
2610 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2611 reply_command_header_offset
+
2612 sizeof(struct lttcomm_list_command_header
));
2614 ret_code
= LTTNG_ERR_NOMEM
;
2619 case LTTNG_DOMAIN_UST
:
2620 ret
= ust_app_list_event_fields(&fields
);
2622 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2627 case LTTNG_DOMAIN_KERNEL
:
2628 default: /* fall-through */
2629 ret_code
= LTTNG_ERR_UND
;
2635 for (i
= 0; i
< nb_fields
; i
++) {
2636 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2638 ret_code
= LTTNG_ERR_NOMEM
;
2643 if (nb_fields
> UINT32_MAX
) {
2644 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2645 ret_code
= LTTNG_ERR_OVERFLOW
;
2649 /* Update command reply header. */
2650 reply_command_header
.count
= (uint32_t) nb_fields
;
2652 memcpy(reply
->buffer
.data
+ reply_command_header_offset
,
2653 &reply_command_header
,
2654 sizeof(reply_command_header
));
2656 ret_code
= LTTNG_OK
;
2663 enum lttng_error_code
cmd_list_syscalls(struct lttng_payload
*reply_payload
)
2665 enum lttng_error_code ret_code
;
2666 ssize_t nb_events
, i
;
2668 struct lttng_event
*events
= nullptr;
2669 struct lttcomm_list_command_header reply_command_header
= {};
2670 size_t reply_command_header_offset
;
2672 assert(reply_payload
);
2674 /* Reserve space for command reply header. */
2675 reply_command_header_offset
= reply_payload
->buffer
.size
;
2676 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2677 reply_command_header_offset
+
2678 sizeof(struct lttcomm_list_command_header
));
2680 ret_code
= LTTNG_ERR_NOMEM
;
2684 nb_events
= syscall_table_list(&events
);
2685 if (nb_events
< 0) {
2686 ret_code
= (enum lttng_error_code
) - nb_events
;
2690 for (i
= 0; i
< nb_events
; i
++) {
2691 ret
= lttng_event_serialize(
2692 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2694 ret_code
= LTTNG_ERR_NOMEM
;
2699 if (nb_events
> UINT32_MAX
) {
2700 ERR("Syscall count would overflow the syscall listing command's reply");
2701 ret_code
= LTTNG_ERR_OVERFLOW
;
2705 /* Update command reply header. */
2706 reply_command_header
.count
= (uint32_t) nb_events
;
2707 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2708 &reply_command_header
,
2709 sizeof(reply_command_header
));
2711 ret_code
= LTTNG_OK
;
2718 * Command LTTNG_START_TRACE processed by the client thread.
2720 * Called with session mutex held.
2722 int cmd_start_trace(struct ltt_session
*session
)
2724 enum lttng_error_code ret
;
2725 unsigned long nb_chan
= 0;
2726 struct ltt_kernel_session
*ksession
;
2727 struct ltt_ust_session
*usess
;
2728 const bool session_rotated_after_last_stop
= session
->rotated_after_last_stop
;
2729 const bool session_cleared_after_last_stop
= session
->cleared_after_last_stop
;
2731 LTTNG_ASSERT(session
);
2733 /* Ease our life a bit ;) */
2734 ksession
= session
->kernel_session
;
2735 usess
= session
->ust_session
;
2737 /* Is the session already started? */
2738 if (session
->active
) {
2739 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2740 /* Perform nothing */
2744 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2745 !session
->current_trace_chunk
) {
2747 * A rotation was launched while the session was stopped and
2748 * it has not been completed yet. It is not possible to start
2749 * the session since starting the session here would require a
2750 * rotation from "NULL" to a new trace chunk. That rotation
2751 * would overlap with the ongoing rotation, which is not
2754 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2756 ret
= LTTNG_ERR_ROTATION_PENDING
;
2761 * Starting a session without channel is useless since after that it's not
2762 * possible to enable channel thus inform the client.
2764 if (usess
&& usess
->domain_global
.channels
) {
2765 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2768 nb_chan
+= ksession
->channel_count
;
2771 ret
= LTTNG_ERR_NO_CHANNEL
;
2775 session
->active
= true;
2776 session
->rotated_after_last_stop
= false;
2777 session
->cleared_after_last_stop
= false;
2778 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2779 if (!session
->has_been_started
) {
2780 struct lttng_trace_chunk
*trace_chunk
;
2782 DBG("Creating initial trace chunk of session \"%s\"", session
->name
);
2784 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
2786 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2789 LTTNG_ASSERT(!session
->current_trace_chunk
);
2790 ret
= (lttng_error_code
) session_set_trace_chunk(
2791 session
, trace_chunk
, nullptr);
2792 lttng_trace_chunk_put(trace_chunk
);
2794 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2798 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2801 * Rotate existing streams into the new chunk.
2802 * This is a "quiet" rotation has no client has
2803 * explicitly requested this operation.
2805 * There is also no need to wait for the rotation
2806 * to complete as it will happen immediately. No data
2807 * was produced as the session was stopped, so the
2808 * rotation should happen on reception of the command.
2810 ret
= (lttng_error_code
) cmd_rotate_session(
2811 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2812 if (ret
!= LTTNG_OK
) {
2818 /* Kernel tracing */
2819 if (ksession
!= nullptr) {
2820 DBG("Start kernel tracing session %s", session
->name
);
2821 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2822 if (ret
!= LTTNG_OK
) {
2827 /* Flag session that trace should start automatically */
2829 int int_ret
= ust_app_start_trace_all(usess
);
2832 ret
= LTTNG_ERR_UST_START_FAIL
;
2838 * Open a packet in every stream of the session to ensure that viewers
2839 * can correctly identify the boundaries of the periods during which
2840 * tracing was active for this session.
2842 ret
= session_open_packets(session
);
2843 if (ret
!= LTTNG_OK
) {
2848 * Clear the flag that indicates that a rotation was done while the
2849 * session was stopped.
2851 session
->rotated_after_last_stop
= false;
2853 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2854 int int_ret
= timer_session_rotation_schedule_timer_start(
2855 session
, session
->rotate_timer_period
);
2858 ERR("Failed to enable rotate timer");
2859 ret
= LTTNG_ERR_UNK
;
2867 if (ret
== LTTNG_OK
) {
2868 /* Flag this after a successful start. */
2869 session
->has_been_started
= true;
2871 session
->active
= false;
2872 /* Restore initial state on error. */
2873 session
->rotated_after_last_stop
= session_rotated_after_last_stop
;
2874 session
->cleared_after_last_stop
= session_cleared_after_last_stop
;
2881 * Command LTTNG_STOP_TRACE processed by the client thread.
2883 int cmd_stop_trace(struct ltt_session
*session
)
2886 struct ltt_kernel_session
*ksession
;
2887 struct ltt_ust_session
*usess
;
2889 LTTNG_ASSERT(session
);
2891 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2893 ksession
= session
->kernel_session
;
2894 usess
= session
->ust_session
;
2896 /* Session is not active. Skip everything and inform the client. */
2897 if (!session
->active
) {
2898 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2902 ret
= stop_kernel_session(ksession
);
2903 if (ret
!= LTTNG_OK
) {
2907 if (usess
&& usess
->active
) {
2908 ret
= ust_app_stop_trace_all(usess
);
2910 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2915 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2916 /* Flag inactive after a successful stop. */
2917 session
->active
= false;
2925 * Set the base_path of the session only if subdir of a control uris is set.
2926 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2929 set_session_base_path_from_uris(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2934 for (i
= 0; i
< nb_uri
; i
++) {
2935 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
|| uris
[i
].subdir
[0] == '\0') {
2936 /* Not interested in these URIs */
2940 if (session
->base_path
!= nullptr) {
2941 free(session
->base_path
);
2942 session
->base_path
= nullptr;
2945 /* Set session base_path */
2946 session
->base_path
= strdup(uris
[i
].subdir
);
2947 if (!session
->base_path
) {
2948 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2951 ret
= LTTNG_ERR_NOMEM
;
2954 DBG2("Setting base path \"%s\" for session \"%s\"",
2964 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2966 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2969 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2970 struct ltt_ust_session
*usess
= session
->ust_session
;
2972 LTTNG_ASSERT(session
);
2974 LTTNG_ASSERT(nb_uri
> 0);
2976 /* Can't set consumer URI if the session is active. */
2977 if (session
->active
) {
2978 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2983 * Set the session base path if any. This is done inside
2984 * cmd_set_consumer_uri to preserve backward compatibility of the
2985 * previous session creation api vs the session descriptor api.
2987 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2988 if (ret
!= LTTNG_OK
) {
2992 /* Set the "global" consumer URIs */
2993 for (i
= 0; i
< nb_uri
; i
++) {
2994 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
], LTTNG_DOMAIN_NONE
);
2995 if (ret
!= LTTNG_OK
) {
3000 /* Set UST session URIs */
3001 if (session
->ust_session
) {
3002 for (i
= 0; i
< nb_uri
; i
++) {
3003 ret
= add_uri_to_consumer(session
,
3004 session
->ust_session
->consumer
,
3007 if (ret
!= LTTNG_OK
) {
3013 /* Set kernel session URIs */
3014 if (session
->kernel_session
) {
3015 for (i
= 0; i
< nb_uri
; i
++) {
3016 ret
= add_uri_to_consumer(session
,
3017 session
->kernel_session
->consumer
,
3019 LTTNG_DOMAIN_KERNEL
);
3020 if (ret
!= LTTNG_OK
) {
3027 * Make sure to set the session in output mode after we set URI since a
3028 * session can be created without URL (thus flagged in no output mode).
3030 session
->output_traces
= 1;
3032 ksess
->output_traces
= 1;
3036 usess
->output_traces
= 1;
3046 static enum lttng_error_code
3047 set_session_output_from_descriptor(struct ltt_session
*session
,
3048 const struct lttng_session_descriptor
*descriptor
)
3051 enum lttng_error_code ret_code
= LTTNG_OK
;
3052 enum lttng_session_descriptor_type session_type
=
3053 lttng_session_descriptor_get_type(descriptor
);
3054 enum lttng_session_descriptor_output_type output_type
=
3055 lttng_session_descriptor_get_output_type(descriptor
);
3056 struct lttng_uri uris
[2] = {};
3057 size_t uri_count
= 0;
3059 switch (output_type
) {
3060 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3062 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3063 lttng_session_descriptor_get_local_output_uri(descriptor
, &uris
[0]);
3066 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3067 lttng_session_descriptor_get_network_output_uris(descriptor
, &uris
[0], &uris
[1]);
3071 ret_code
= LTTNG_ERR_INVALID
;
3075 switch (session_type
) {
3076 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3078 struct snapshot_output
*new_output
= nullptr;
3080 new_output
= snapshot_output_alloc();
3082 ret_code
= LTTNG_ERR_NOMEM
;
3086 ret
= snapshot_output_init_with_uri(session
,
3087 DEFAULT_SNAPSHOT_MAX_SIZE
,
3093 &session
->snapshot
);
3095 ret_code
= (ret
== -ENOMEM
) ? LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3096 snapshot_output_destroy(new_output
);
3099 snapshot_add_output(&session
->snapshot
, new_output
);
3102 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3103 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3105 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3109 ret_code
= LTTNG_ERR_INVALID
;
3116 static enum lttng_error_code
3117 cmd_create_session_from_descriptor(struct lttng_session_descriptor
*descriptor
,
3118 const lttng_sock_cred
*creds
,
3119 const char *home_path
)
3122 enum lttng_error_code ret_code
;
3123 const char *session_name
;
3124 struct ltt_session
*new_session
= nullptr;
3125 enum lttng_session_descriptor_status descriptor_status
;
3127 session_lock_list();
3129 if (*home_path
!= '/') {
3130 ERR("Home path provided by client is not absolute");
3131 ret_code
= LTTNG_ERR_INVALID
;
3136 descriptor_status
= lttng_session_descriptor_get_session_name(descriptor
, &session_name
);
3137 switch (descriptor_status
) {
3138 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3140 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3141 session_name
= nullptr;
3144 ret_code
= LTTNG_ERR_INVALID
;
3148 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, &new_session
);
3149 if (ret_code
!= LTTNG_OK
) {
3153 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3158 if (ret_code
!= LTTNG_OK
) {
3162 /* Announce the session's destruction to the notification thread when it is destroyed. */
3163 ret
= session_add_destroy_notifier(
3165 [](const struct ltt_session
*session
, void *user_data
__attribute__((unused
))) {
3166 (void) notification_thread_command_remove_session(
3167 the_notification_thread_handle
, session
->id
);
3171 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3173 ret
= LTTNG_ERR_NOMEM
;
3177 if (!session_name
) {
3178 ret
= lttng_session_descriptor_set_session_name(descriptor
, new_session
->name
);
3180 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3185 if (!lttng_session_descriptor_is_output_destination_initialized(descriptor
)) {
3187 * Only include the session's creation time in the output
3188 * destination if the name of the session itself was
3189 * not auto-generated.
3191 ret_code
= lttng_session_descriptor_set_default_output(
3193 session_name
? &new_session
->creation_time
: nullptr,
3195 if (ret_code
!= LTTNG_OK
) {
3199 new_session
->has_user_specified_directory
=
3200 lttng_session_descriptor_has_output_directory(descriptor
);
3203 switch (lttng_session_descriptor_get_type(descriptor
)) {
3204 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3205 new_session
->snapshot_mode
= 1;
3207 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3208 new_session
->live_timer
=
3209 lttng_session_descriptor_live_get_timer_interval(descriptor
);
3215 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3216 if (ret_code
!= LTTNG_OK
) {
3219 new_session
->consumer
->enabled
= true;
3220 ret_code
= LTTNG_OK
;
3222 /* Release reference provided by the session_create function. */
3223 session_put(new_session
);
3224 if (ret_code
!= LTTNG_OK
&& new_session
) {
3225 /* Release the global reference on error. */
3226 session_destroy(new_session
);
3228 session_unlock_list();
3232 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
,
3234 struct lttng_session_descriptor
**return_descriptor
)
3237 size_t payload_size
;
3238 struct lttng_dynamic_buffer payload
;
3239 struct lttng_buffer_view home_dir_view
;
3240 struct lttng_buffer_view session_descriptor_view
;
3241 struct lttng_session_descriptor
*session_descriptor
= nullptr;
3242 enum lttng_error_code ret_code
;
3244 lttng_dynamic_buffer_init(&payload
);
3245 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>= LTTNG_PATH_MAX
) {
3246 ret_code
= LTTNG_ERR_INVALID
;
3249 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3250 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3251 ret_code
= LTTNG_ERR_INVALID
;
3255 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3256 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3257 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3259 ret_code
= LTTNG_ERR_NOMEM
;
3263 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3265 ERR("Reception of session descriptor failed, aborting.");
3266 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3270 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3271 &payload
, 0, cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3272 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3273 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3274 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3275 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3279 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3281 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3282 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3283 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3284 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3285 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3289 ret
= lttng_session_descriptor_create_from_buffer(&session_descriptor_view
,
3290 &session_descriptor
);
3292 ERR("Failed to create session descriptor from payload of \"create session\" command");
3293 ret_code
= LTTNG_ERR_INVALID
;
3298 * Sets the descriptor's auto-generated properties (name, output) if
3301 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3303 home_dir_view
.size
? home_dir_view
.data
:
3305 if (ret_code
!= LTTNG_OK
) {
3309 ret_code
= LTTNG_OK
;
3310 *return_descriptor
= session_descriptor
;
3311 session_descriptor
= nullptr;
3313 lttng_dynamic_buffer_reset(&payload
);
3314 lttng_session_descriptor_destroy(session_descriptor
);
3318 static void cmd_destroy_session_reply(const struct ltt_session
*session
, void *_reply_context
)
3322 const struct cmd_destroy_session_reply_context
*reply_context
=
3323 (cmd_destroy_session_reply_context
*) _reply_context
;
3324 struct lttng_dynamic_buffer payload
;
3325 struct lttcomm_session_destroy_command_header cmd_header
;
3326 struct lttng_trace_archive_location
*location
= nullptr;
3327 struct lttcomm_lttng_msg llm
= {
3328 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION
,
3329 .ret_code
= reply_context
->destruction_status
,
3331 .cmd_header_size
= sizeof(struct lttcomm_session_destroy_command_header
),
3335 size_t payload_size_before_location
;
3337 lttng_dynamic_buffer_init(&payload
);
3339 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3341 ERR("Failed to append session destruction message");
3345 cmd_header
.rotation_state
= (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3346 session
->rotation_state
:
3347 LTTNG_ROTATION_STATE_NO_ROTATION
);
3348 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
, sizeof(cmd_header
));
3350 ERR("Failed to append session destruction command header");
3354 if (!reply_context
->implicit_rotation_on_destroy
) {
3355 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3359 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3360 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3365 location
= session_get_trace_archive_location(session
);
3367 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3372 payload_size_before_location
= payload
.size
;
3373 comm_ret
= lttng_trace_archive_location_serialize(location
, &payload
);
3374 lttng_trace_archive_location_put(location
);
3376 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3380 /* Update the message to indicate the location's length. */
3381 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3382 payload
.size
- payload_size_before_location
;
3384 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
, payload
.data
, payload
.size
);
3385 if (comm_ret
!= (ssize_t
) payload
.size
) {
3386 ERR("Failed to send result of the destruction of session \"%s\" to client",
3390 ret
= close(reply_context
->reply_sock_fd
);
3392 PERROR("Failed to close client socket in deferred session destroy reply");
3394 lttng_dynamic_buffer_reset(&payload
);
3395 free(_reply_context
);
3399 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3401 * Called with session lock held.
3403 int cmd_destroy_session(struct ltt_session
*session
, int *sock_fd
)
3406 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3407 struct cmd_destroy_session_reply_context
*reply_context
= nullptr;
3410 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3411 if (!reply_context
) {
3412 ret
= LTTNG_ERR_NOMEM
;
3416 reply_context
->reply_sock_fd
= *sock_fd
;
3420 LTTNG_ASSERT(session
);
3422 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3423 if (session
->active
) {
3424 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3426 ret
= cmd_stop_trace(session
);
3427 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3428 /* Carry on with the destruction of the session. */
3429 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3431 lttng_strerror(-ret
));
3432 destruction_last_error
= (lttng_error_code
) ret
;
3436 if (session
->rotation_schedule_timer_enabled
) {
3437 if (timer_session_rotation_schedule_timer_stop(session
)) {
3438 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3440 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3444 if (session
->rotate_size
) {
3446 the_rotation_thread_handle
->unsubscribe_session_consumed_size_rotation(
3448 } catch (const std::exception
& e
) {
3449 /* Continue the destruction of the session anyway. */
3450 ERR("Failed to unsubscribe rotation thread notification channel from consumed size condition during session destruction: %s",
3454 session
->rotate_size
= 0;
3457 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3459 * Perform a last rotation on destruction if rotations have
3460 * occurred during the session's lifetime.
3462 ret
= cmd_rotate_session(
3463 session
, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3464 if (ret
!= LTTNG_OK
) {
3465 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3467 lttng_strerror(-ret
));
3468 destruction_last_error
= (lttng_error_code
) -ret
;
3470 if (reply_context
) {
3471 reply_context
->implicit_rotation_on_destroy
= true;
3473 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3475 * The user has not triggered a session rotation. However, to
3476 * ensure all data has been consumed, the session is rotated
3477 * to a 'null' trace chunk before it is destroyed.
3479 * This is a "quiet" rotation meaning that no notification is
3480 * emitted and no renaming of the current trace chunk takes
3483 ret
= cmd_rotate_session(
3484 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3486 * Rotation operations may not be supported by the kernel
3487 * tracer. Hence, do not consider this implicit rotation as
3488 * a session destruction error. The library has already stopped
3489 * the session and waited for pending data; there is nothing
3490 * left to do but complete the destruction of the session.
3492 if (ret
!= LTTNG_OK
&& ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3493 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3495 lttng_strerror(ret
));
3496 destruction_last_error
= (lttng_error_code
) -ret
;
3500 if (session
->shm_path
[0]) {
3502 * When a session is created with an explicit shm_path,
3503 * the consumer daemon will create its shared memory files
3504 * at that location and will *not* unlink them. This is normal
3505 * as the intention of that feature is to make it possible
3506 * to retrieve the content of those files should a crash occur.
3508 * To ensure the content of those files can be used, the
3509 * sessiond daemon will replicate the content of the metadata
3510 * cache in a metadata file.
3512 * On clean-up, it is expected that the consumer daemon will
3513 * unlink the shared memory files and that the session daemon
3514 * will unlink the metadata file. Then, the session's directory
3515 * in the shm path can be removed.
3517 * Unfortunately, a flaw in the design of the sessiond's and
3518 * consumerd's tear down of channels makes it impossible to
3519 * determine when the sessiond _and_ the consumerd have both
3520 * destroyed their representation of a channel. For one, the
3521 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3522 * callbacks in both daemons.
3524 * However, it is also impossible for the sessiond to know when
3525 * the consumer daemon is done destroying its channel(s) since
3526 * it occurs as a reaction to the closing of the channel's file
3527 * descriptor. There is no resulting communication initiated
3528 * from the consumerd to the sessiond to confirm that the
3529 * operation is completed (and was successful).
3531 * Until this is all fixed, the session daemon checks for the
3532 * removal of the session's shm path which makes it possible
3533 * to safely advertise a session as having been destroyed.
3535 * Prior to this fix, it was not possible to reliably save
3536 * a session making use of the --shm-path option, destroy it,
3537 * and load it again. This is because the creation of the
3538 * session would fail upon seeing the session's shm path
3539 * already in existence.
3541 * Note that none of the error paths in the check for the
3542 * directory's existence return an error. This is normal
3543 * as there isn't much that can be done. The session will
3544 * be destroyed properly, except that we can't offer the
3545 * guarantee that the same session can be re-created.
3547 current_completion_handler
= &destroy_completion_handler
.handler
;
3548 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3550 sizeof(destroy_completion_handler
.shm_path
));
3555 * The session is destroyed. However, note that the command context
3556 * still holds a reference to the session, thus delaying its destruction
3557 * _at least_ up to the point when that reference is released.
3559 session_destroy(session
);
3560 if (reply_context
) {
3561 reply_context
->destruction_status
= destruction_last_error
;
3562 ret
= session_add_destroy_notifier(
3563 session
, cmd_destroy_session_reply
, (void *) reply_context
);
3565 ret
= LTTNG_ERR_FATAL
;
3577 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3579 int cmd_register_consumer(struct ltt_session
*session
,
3580 enum lttng_domain_type domain
,
3581 const char *sock_path
,
3582 struct consumer_data
*cdata
)
3585 struct consumer_socket
*socket
= nullptr;
3587 LTTNG_ASSERT(session
);
3588 LTTNG_ASSERT(cdata
);
3589 LTTNG_ASSERT(sock_path
);
3592 case LTTNG_DOMAIN_KERNEL
:
3594 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3596 LTTNG_ASSERT(ksess
);
3598 /* Can't register a consumer if there is already one */
3599 if (ksess
->consumer_fds_sent
!= 0) {
3600 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3604 sock
= lttcomm_connect_unix_sock(sock_path
);
3606 ret
= LTTNG_ERR_CONNECT_FAIL
;
3609 cdata
->cmd_sock
= sock
;
3611 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3612 if (socket
== nullptr) {
3615 PERROR("close register consumer");
3617 cdata
->cmd_sock
= -1;
3618 ret
= LTTNG_ERR_FATAL
;
3622 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3623 if (socket
->lock
== nullptr) {
3624 PERROR("zmalloc pthread mutex");
3625 ret
= LTTNG_ERR_FATAL
;
3629 pthread_mutex_init(socket
->lock
, nullptr);
3630 socket
->registered
= 1;
3632 lttng::urcu::read_lock_guard read_lock
;
3633 consumer_add_socket(socket
, ksess
->consumer
);
3635 pthread_mutex_lock(&cdata
->pid_mutex
);
3637 pthread_mutex_unlock(&cdata
->pid_mutex
);
3642 /* TODO: Userspace tracing */
3643 ret
= LTTNG_ERR_UND
;
3651 consumer_destroy_socket(socket
);
3657 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3659 ssize_t
cmd_list_domains(struct ltt_session
*session
, struct lttng_domain
**domains
)
3664 struct lttng_ht_iter iter
;
3666 if (session
->kernel_session
!= nullptr) {
3667 DBG3("Listing domains found kernel domain");
3671 if (session
->ust_session
!= nullptr) {
3672 DBG3("Listing domains found UST global domain");
3675 lttng::urcu::read_lock_guard read_lock
;
3677 cds_lfht_for_each_entry (
3678 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3679 if (agt
->being_used
) {
3689 *domains
= calloc
<lttng_domain
>(nb_dom
);
3690 if (*domains
== nullptr) {
3691 ret
= LTTNG_ERR_FATAL
;
3695 if (session
->kernel_session
!= nullptr) {
3696 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3698 /* Kernel session buffer type is always GLOBAL */
3699 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3704 if (session
->ust_session
!= nullptr) {
3705 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3706 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3710 lttng::urcu::read_lock_guard read_lock
;
3712 cds_lfht_for_each_entry (
3713 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3714 if (agt
->being_used
) {
3715 (*domains
)[index
].type
= agt
->domain
;
3716 (*domains
)[index
].buf_type
=
3717 session
->ust_session
->buffer_type
;
3727 /* Return negative value to differentiate return code */
3732 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3734 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3735 struct ltt_session
*session
,
3736 struct lttng_payload
*payload
)
3740 struct lttcomm_list_command_header cmd_header
= {};
3741 size_t cmd_header_offset
;
3742 enum lttng_error_code ret_code
;
3747 DBG("Listing channels for session %s", session
->name
);
3749 cmd_header_offset
= payload
->buffer
.size
;
3751 /* Reserve space for command reply header. */
3752 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3753 cmd_header_offset
+ sizeof(cmd_header
));
3755 ret_code
= LTTNG_ERR_NOMEM
;
3760 case LTTNG_DOMAIN_KERNEL
:
3762 /* Kernel channels */
3763 struct ltt_kernel_channel
*kchan
;
3764 if (session
->kernel_session
!= nullptr) {
3765 cds_list_for_each_entry (
3766 kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
3767 uint64_t discarded_events
, lost_packets
;
3768 struct lttng_channel_extended
*extended
;
3770 extended
= (struct lttng_channel_extended
*)
3771 kchan
->channel
->attr
.extended
.ptr
;
3773 ret
= get_kernel_runtime_stats(
3774 session
, kchan
, &discarded_events
, &lost_packets
);
3776 ret_code
= LTTNG_ERR_UNK
;
3781 * Update the discarded_events and lost_packets
3782 * count for the channel
3784 extended
->discarded_events
= discarded_events
;
3785 extended
->lost_packets
= lost_packets
;
3787 ret
= lttng_channel_serialize(kchan
->channel
, &payload
->buffer
);
3789 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3790 kchan
->channel
->name
);
3791 ret_code
= LTTNG_ERR_UNK
;
3800 case LTTNG_DOMAIN_UST
:
3802 struct lttng_ht_iter iter
;
3803 struct ltt_ust_channel
*uchan
;
3806 lttng::urcu::read_lock_guard read_lock
;
3808 cds_lfht_for_each_entry (session
->ust_session
->domain_global
.channels
->ht
,
3812 uint64_t discarded_events
= 0, lost_packets
= 0;
3813 struct lttng_channel
*channel
= nullptr;
3814 struct lttng_channel_extended
*extended
;
3816 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3818 ret_code
= LTTNG_ERR_NOMEM
;
3822 extended
= (struct lttng_channel_extended
*)
3823 channel
->attr
.extended
.ptr
;
3825 ret
= get_ust_runtime_stats(
3826 session
, uchan
, &discarded_events
, &lost_packets
);
3828 lttng_channel_destroy(channel
);
3829 ret_code
= LTTNG_ERR_UNK
;
3833 extended
->discarded_events
= discarded_events
;
3834 extended
->lost_packets
= lost_packets
;
3836 ret
= lttng_channel_serialize(channel
, &payload
->buffer
);
3838 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3840 lttng_channel_destroy(channel
);
3841 ret_code
= LTTNG_ERR_UNK
;
3845 lttng_channel_destroy(channel
);
3856 if (i
> UINT32_MAX
) {
3857 ERR("Channel count would overflow the channel listing command's reply");
3858 ret_code
= LTTNG_ERR_OVERFLOW
;
3862 /* Update command reply header. */
3863 cmd_header
.count
= (uint32_t) i
;
3864 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
, sizeof(cmd_header
));
3865 ret_code
= LTTNG_OK
;
3872 * Command LTTNG_LIST_EVENTS processed by the client thread.
3874 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3875 struct ltt_session
*session
,
3877 struct lttng_payload
*reply_payload
)
3879 int buffer_resize_ret
;
3880 enum lttng_error_code ret_code
= LTTNG_OK
;
3881 struct lttcomm_list_command_header reply_command_header
= {};
3882 size_t reply_command_header_offset
;
3883 unsigned int nb_events
= 0;
3885 assert(reply_payload
);
3887 /* Reserve space for command reply header. */
3888 reply_command_header_offset
= reply_payload
->buffer
.size
;
3889 buffer_resize_ret
= lttng_dynamic_buffer_set_size(
3890 &reply_payload
->buffer
,
3891 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
3892 if (buffer_resize_ret
) {
3893 ret_code
= LTTNG_ERR_NOMEM
;
3898 case LTTNG_DOMAIN_KERNEL
:
3899 if (session
->kernel_session
!= nullptr) {
3900 ret_code
= list_lttng_kernel_events(
3901 channel_name
, session
->kernel_session
, reply_payload
, &nb_events
);
3905 case LTTNG_DOMAIN_UST
:
3907 if (session
->ust_session
!= nullptr) {
3909 list_lttng_ust_global_events(channel_name
,
3910 &session
->ust_session
->domain_global
,
3917 case LTTNG_DOMAIN_LOG4J
:
3918 case LTTNG_DOMAIN_JUL
:
3919 case LTTNG_DOMAIN_PYTHON
:
3920 if (session
->ust_session
) {
3921 struct lttng_ht_iter iter
;
3924 lttng::urcu::read_lock_guard read_lock
;
3926 cds_lfht_for_each_entry (
3927 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3928 if (agt
->domain
== domain
) {
3929 ret_code
= list_lttng_agent_events(
3930 agt
, reply_payload
, &nb_events
);
3937 ret_code
= LTTNG_ERR_UND
;
3941 if (nb_events
> UINT32_MAX
) {
3942 ret_code
= LTTNG_ERR_OVERFLOW
;
3946 /* Update command reply header. */
3947 reply_command_header
.count
= (uint32_t) nb_events
;
3948 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
3949 &reply_command_header
,
3950 sizeof(reply_command_header
));
3957 * Using the session list, filled a lttng_session array to send back to the
3958 * client for session listing.
3960 * The session list lock MUST be acquired before calling this function. Use
3961 * session_lock_list() and session_unlock_list().
3963 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3964 size_t session_count
,
3970 struct ltt_session
*session
;
3971 struct ltt_session_list
*list
= session_get_list();
3972 struct lttng_session_extended
*extended
= (typeof(extended
)) (&sessions
[session_count
]);
3974 DBG("Getting all available session for UID %d GID %d", uid
, gid
);
3976 * Iterate over session list and append data after the control struct in
3979 cds_list_for_each_entry (session
, &list
->head
, list
) {
3980 if (!session_get(session
)) {
3984 * Only list the sessions the user can control.
3986 if (!session_access_ok(session
, uid
) || session
->destroyed
) {
3987 session_put(session
);
3991 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3992 struct ltt_ust_session
*usess
= session
->ust_session
;
3994 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3995 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3996 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3997 ret
= build_network_session_path(
3998 sessions
[i
].path
, sizeof(sessions
[i
].path
), session
);
4000 ret
= snprintf(sessions
[i
].path
,
4001 sizeof(sessions
[i
].path
),
4003 session
->consumer
->dst
.session_root_path
);
4006 PERROR("snprintf session path");
4007 session_put(session
);
4011 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
4012 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
4013 sessions
[i
].enabled
= session
->active
;
4014 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
4015 sessions
[i
].live_timer_interval
= session
->live_timer
;
4016 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
4017 extended
[i
].creation_time
.is_set
= 1;
4019 session_put(session
);
4024 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4025 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4027 int cmd_data_pending(struct ltt_session
*session
)
4030 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4031 struct ltt_ust_session
*usess
= session
->ust_session
;
4033 LTTNG_ASSERT(session
);
4035 DBG("Data pending for session %s", session
->name
);
4037 /* Session MUST be stopped to ask for data availability. */
4038 if (session
->active
) {
4039 ret
= LTTNG_ERR_SESSION_STARTED
;
4043 * If stopped, just make sure we've started before else the above call
4044 * will always send that there is data pending.
4046 * The consumer assumes that when the data pending command is received,
4047 * the trace has been started before or else no output data is written
4048 * by the streams which is a condition for data pending. So, this is
4049 * *VERY* important that we don't ask the consumer before a start
4052 if (!session
->has_been_started
) {
4058 /* A rotation is still pending, we have to wait. */
4059 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4060 DBG("Rotate still pending for session %s", session
->name
);
4065 if (ksess
&& ksess
->consumer
) {
4066 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4068 /* Data is still being extracted for the kernel. */
4073 if (usess
&& usess
->consumer
) {
4074 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4076 /* Data is still being extracted for the kernel. */
4081 /* Data is ready to be read by a viewer */
4089 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4091 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4093 int cmd_snapshot_add_output(struct ltt_session
*session
,
4094 const struct lttng_snapshot_output
*output
,
4098 struct snapshot_output
*new_output
;
4100 LTTNG_ASSERT(session
);
4101 LTTNG_ASSERT(output
);
4103 DBG("Cmd snapshot add output for session %s", session
->name
);
4106 * Can't create an output if the session is not set in no-output mode.
4108 if (session
->output_traces
) {
4109 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4113 if (session
->has_non_mmap_channel
) {
4114 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4118 /* Only one output is allowed until we have the "tee" feature. */
4119 if (session
->snapshot
.nb_output
== 1) {
4120 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4124 new_output
= snapshot_output_alloc();
4126 ret
= LTTNG_ERR_NOMEM
;
4130 ret
= snapshot_output_init(session
,
4137 &session
->snapshot
);
4139 if (ret
== -ENOMEM
) {
4140 ret
= LTTNG_ERR_NOMEM
;
4142 ret
= LTTNG_ERR_INVALID
;
4147 snapshot_add_output(&session
->snapshot
, new_output
);
4149 *id
= new_output
->id
;
4155 snapshot_output_destroy(new_output
);
4161 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4163 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4165 int cmd_snapshot_del_output(struct ltt_session
*session
, const struct lttng_snapshot_output
*output
)
4168 struct snapshot_output
*sout
= nullptr;
4170 LTTNG_ASSERT(session
);
4171 LTTNG_ASSERT(output
);
4173 lttng::urcu::read_lock_guard read_lock
;
4176 * Permission denied to create an output if the session is not
4177 * set in no output mode.
4179 if (session
->output_traces
) {
4180 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4185 DBG("Cmd snapshot del output id %" PRIu32
" for session %s",
4188 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4189 } else if (*output
->name
!= '\0') {
4190 DBG("Cmd snapshot del output name %s for session %s", output
->name
, session
->name
);
4191 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4194 ret
= LTTNG_ERR_INVALID
;
4198 snapshot_delete_output(&session
->snapshot
, sout
);
4199 snapshot_output_destroy(sout
);
4207 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4209 * If no output is available, outputs is untouched and 0 is returned.
4211 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4213 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4214 struct lttng_snapshot_output
**outputs
)
4217 struct lttng_snapshot_output
*list
= nullptr;
4218 struct lttng_ht_iter iter
;
4219 struct snapshot_output
*output
;
4221 LTTNG_ASSERT(session
);
4222 LTTNG_ASSERT(outputs
);
4224 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4227 * Permission denied to create an output if the session is not
4228 * set in no output mode.
4230 if (session
->output_traces
) {
4231 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4235 if (session
->snapshot
.nb_output
== 0) {
4240 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4242 ret
= -LTTNG_ERR_NOMEM
;
4246 /* Copy list from session to the new list object. */
4248 lttng::urcu::read_lock_guard read_lock
;
4250 cds_lfht_for_each_entry (
4251 session
->snapshot
.output_ht
->ht
, &iter
.iter
, output
, node
.node
) {
4252 LTTNG_ASSERT(output
->consumer
);
4253 list
[idx
].id
= output
->id
;
4254 list
[idx
].max_size
= output
->max_size
;
4255 if (lttng_strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
))) {
4256 ret
= -LTTNG_ERR_INVALID
;
4260 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4261 if (lttng_strncpy(list
[idx
].ctrl_url
,
4262 output
->consumer
->dst
.session_root_path
,
4263 sizeof(list
[idx
].ctrl_url
))) {
4264 ret
= -LTTNG_ERR_INVALID
;
4269 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4271 sizeof(list
[idx
].ctrl_url
));
4273 ret
= -LTTNG_ERR_NOMEM
;
4278 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4280 sizeof(list
[idx
].data_url
));
4282 ret
= -LTTNG_ERR_NOMEM
;
4293 ret
= session
->snapshot
.nb_output
;
4301 * Check if we can regenerate the metadata for this session.
4302 * Only kernel, UST per-uid and non-live sessions are supported.
4304 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4306 static int check_regenerate_metadata_support(struct ltt_session
*session
)
4310 LTTNG_ASSERT(session
);
4312 if (session
->live_timer
!= 0) {
4313 ret
= LTTNG_ERR_LIVE_SESSION
;
4316 if (!session
->active
) {
4317 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4320 if (session
->ust_session
) {
4321 switch (session
->ust_session
->buffer_type
) {
4322 case LTTNG_BUFFER_PER_UID
:
4324 case LTTNG_BUFFER_PER_PID
:
4325 ret
= LTTNG_ERR_PER_PID_SESSION
;
4329 ret
= LTTNG_ERR_UNK
;
4333 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4334 session
->consumer
->relay_minor_version
< 8) {
4335 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4345 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4347 * Ask the consumer to truncate the existing metadata file(s) and
4348 * then regenerate the metadata. Live and per-pid sessions are not
4349 * supported and return an error.
4351 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4353 int cmd_regenerate_metadata(struct ltt_session
*session
)
4357 LTTNG_ASSERT(session
);
4359 ret
= check_regenerate_metadata_support(session
);
4364 if (session
->kernel_session
) {
4365 ret
= kernctl_session_regenerate_metadata(session
->kernel_session
->fd
);
4367 ERR("Failed to regenerate the kernel metadata");
4372 if (session
->ust_session
) {
4373 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4375 ERR("Failed to regenerate the UST metadata");
4379 DBG("Cmd metadata regenerate for session %s", session
->name
);
4387 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4389 * Ask the tracer to regenerate a new statedump.
4391 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4393 int cmd_regenerate_statedump(struct ltt_session
*session
)
4397 LTTNG_ASSERT(session
);
4399 if (!session
->active
) {
4400 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4404 if (session
->kernel_session
) {
4405 ret
= kernctl_session_regenerate_statedump(session
->kernel_session
->fd
);
4407 * Currently, the statedump in kernel can only fail if out
4411 if (ret
== -ENOMEM
) {
4412 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4414 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4416 ERR("Failed to regenerate the kernel statedump");
4421 if (session
->ust_session
) {
4422 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4424 * Currently, the statedump in UST always returns 0.
4427 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4428 ERR("Failed to regenerate the UST statedump");
4432 DBG("Cmd regenerate statedump for session %s", session
->name
);
4439 static enum lttng_error_code
4440 synchronize_tracer_notifier_register(struct notification_thread_handle
*notification_thread
,
4441 struct lttng_trigger
*trigger
,
4442 const struct lttng_credentials
*cmd_creds
)
4444 enum lttng_error_code ret_code
;
4445 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4446 const char *trigger_name
;
4447 uid_t trigger_owner
;
4448 enum lttng_trigger_status trigger_status
;
4449 const enum lttng_domain_type trigger_domain
=
4450 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4452 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4453 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4455 LTTNG_ASSERT(condition
);
4456 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4457 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4459 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4460 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4462 session_lock_list();
4463 switch (trigger_domain
) {
4464 case LTTNG_DOMAIN_KERNEL
:
4466 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4467 if (ret_code
!= LTTNG_OK
) {
4468 enum lttng_error_code notif_thread_unregister_ret
;
4470 notif_thread_unregister_ret
=
4471 notification_thread_command_unregister_trigger(notification_thread
,
4474 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4475 /* Return the original error code. */
4476 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4478 (int) trigger_owner
,
4482 goto end_unlock_session_list
;
4486 case LTTNG_DOMAIN_UST
:
4487 ust_app_global_update_all_event_notifier_rules();
4489 case LTTNG_DOMAIN_JUL
:
4490 case LTTNG_DOMAIN_LOG4J
:
4491 case LTTNG_DOMAIN_PYTHON
:
4493 /* Agent domains. */
4494 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4497 agt
= agent_create(trigger_domain
);
4499 ret_code
= LTTNG_ERR_NOMEM
;
4500 goto end_unlock_session_list
;
4503 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4506 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4507 if (ret_code
!= LTTNG_OK
) {
4508 goto end_unlock_session_list
;
4513 case LTTNG_DOMAIN_NONE
:
4518 ret_code
= LTTNG_OK
;
4519 end_unlock_session_list
:
4520 session_unlock_list();
4524 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4525 struct lttng_trigger
*trigger
,
4526 bool is_trigger_anonymous
,
4527 struct notification_thread_handle
*notification_thread
,
4528 struct lttng_trigger
**return_trigger
)
4530 enum lttng_error_code ret_code
;
4531 const char *trigger_name
;
4532 uid_t trigger_owner
;
4533 enum lttng_trigger_status trigger_status
;
4535 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4536 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4538 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4539 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4541 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4543 (int) trigger_owner
,
4544 (int) lttng_credentials_get_uid(cmd_creds
));
4547 * Validate the trigger credentials against the command credentials.
4548 * Only the root user can register a trigger with non-matching
4551 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4552 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4553 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4555 (int) trigger_owner
,
4556 (int) lttng_credentials_get_uid(cmd_creds
));
4557 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4563 * The bytecode generation also serves as a validation step for the
4564 * bytecode expressions.
4566 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4567 if (ret_code
!= LTTNG_OK
) {
4568 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4570 (int) trigger_owner
,
4576 * A reference to the trigger is acquired by the notification thread.
4577 * It is safe to return the same trigger to the caller since it the
4578 * other user holds a reference.
4580 * The trigger is modified during the execution of the
4581 * "register trigger" command. However, by the time the command returns,
4582 * it is safe to use without any locking as its properties are
4585 ret_code
= notification_thread_command_register_trigger(
4586 notification_thread
, trigger
, is_trigger_anonymous
);
4587 if (ret_code
!= LTTNG_OK
) {
4588 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4590 (int) trigger_owner
,
4595 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4596 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4599 * Synchronize tracers if the trigger adds an event notifier.
4601 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4602 ret_code
= synchronize_tracer_notifier_register(
4603 notification_thread
, trigger
, cmd_creds
);
4604 if (ret_code
!= LTTNG_OK
) {
4605 ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code
));
4611 * Return an updated trigger to the client.
4613 * Since a modified version of the same trigger is returned, acquire a
4614 * reference to the trigger so the caller doesn't have to care if those
4615 * are distinct instances or not.
4617 if (ret_code
== LTTNG_OK
) {
4618 lttng_trigger_get(trigger
);
4619 *return_trigger
= trigger
;
4620 /* Ownership of trigger was transferred to caller. */
4627 static enum lttng_error_code
4628 synchronize_tracer_notifier_unregister(const struct lttng_trigger
*trigger
)
4630 enum lttng_error_code ret_code
;
4631 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4632 const enum lttng_domain_type trigger_domain
=
4633 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4635 LTTNG_ASSERT(condition
);
4636 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4637 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4639 session_lock_list();
4640 switch (trigger_domain
) {
4641 case LTTNG_DOMAIN_KERNEL
:
4642 ret_code
= kernel_unregister_event_notifier(trigger
);
4643 if (ret_code
!= LTTNG_OK
) {
4644 goto end_unlock_session_list
;
4648 case LTTNG_DOMAIN_UST
:
4649 ust_app_global_update_all_event_notifier_rules();
4651 case LTTNG_DOMAIN_JUL
:
4652 case LTTNG_DOMAIN_LOG4J
:
4653 case LTTNG_DOMAIN_PYTHON
:
4655 /* Agent domains. */
4656 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4659 * This trigger was never registered in the first place. Calling
4660 * this function under those circumstances is an internal error.
4663 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4664 if (ret_code
!= LTTNG_OK
) {
4665 goto end_unlock_session_list
;
4670 case LTTNG_DOMAIN_NONE
:
4675 ret_code
= LTTNG_OK
;
4677 end_unlock_session_list
:
4678 session_unlock_list();
4682 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4683 const struct lttng_trigger
*trigger
,
4684 struct notification_thread_handle
*notification_thread
)
4686 enum lttng_error_code ret_code
;
4687 const char *trigger_name
;
4688 uid_t trigger_owner
;
4689 enum lttng_trigger_status trigger_status
;
4690 struct lttng_trigger
*sessiond_trigger
= nullptr;
4692 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4693 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4694 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4695 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4697 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4699 (int) trigger_owner
,
4700 (int) lttng_credentials_get_uid(cmd_creds
));
4703 * Validate the trigger credentials against the command credentials.
4704 * Only the root user can unregister a trigger with non-matching
4707 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4708 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4709 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4711 (int) trigger_owner
,
4712 (int) lttng_credentials_get_uid(cmd_creds
));
4713 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4718 /* Fetch the sessiond side trigger object. */
4719 ret_code
= notification_thread_command_get_trigger(
4720 notification_thread
, trigger
, &sessiond_trigger
);
4721 if (ret_code
!= LTTNG_OK
) {
4722 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4724 (int) trigger_owner
,
4729 LTTNG_ASSERT(sessiond_trigger
);
4732 * From this point on, no matter what, consider the trigger
4735 * We set the unregistered state of the sessiond side trigger object in
4736 * the client thread since we want to minimize the possibility of the
4737 * notification thread being stalled due to a long execution of an
4738 * action that required the trigger lock.
4740 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4742 ret_code
= notification_thread_command_unregister_trigger(notification_thread
, trigger
);
4743 if (ret_code
!= LTTNG_OK
) {
4744 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4746 (int) trigger_owner
,
4752 * Synchronize tracers if the trigger removes an event notifier.
4753 * Do this even if the trigger unregistration failed to at least stop
4754 * the tracers from producing notifications associated with this
4757 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4758 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4759 if (ret_code
!= LTTNG_OK
) {
4760 ERR("Error unregistering trigger to tracer.");
4766 lttng_trigger_put(sessiond_trigger
);
4770 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4771 struct notification_thread_handle
*notification_thread
,
4772 struct lttng_triggers
**return_triggers
)
4775 enum lttng_error_code ret_code
;
4776 struct lttng_triggers
*triggers
= nullptr;
4778 /* Get the set of triggers from the notification thread. */
4779 ret_code
= notification_thread_command_list_triggers(
4780 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4781 if (ret_code
!= LTTNG_OK
) {
4785 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4787 ret_code
= LTTNG_ERR_UNK
;
4791 *return_triggers
= triggers
;
4793 ret_code
= LTTNG_OK
;
4795 lttng_triggers_destroy(triggers
);
4799 enum lttng_error_code
4800 cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4801 const struct lttng_error_query
*query
,
4802 struct lttng_error_query_results
**_results
,
4803 struct notification_thread_handle
*notification_thread
)
4805 enum lttng_error_code ret_code
;
4806 const struct lttng_trigger
*query_target_trigger
;
4807 const struct lttng_action
*query_target_action
= nullptr;
4808 struct lttng_trigger
*matching_trigger
= nullptr;
4809 const char *trigger_name
;
4810 uid_t trigger_owner
;
4811 enum lttng_trigger_status trigger_status
;
4812 struct lttng_error_query_results
*results
= nullptr;
4814 switch (lttng_error_query_get_target_type(query
)) {
4815 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4816 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4818 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4819 query_target_trigger
= lttng_error_query_condition_borrow_target(query
);
4821 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4822 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(query
);
4828 LTTNG_ASSERT(query_target_trigger
);
4830 ret_code
= notification_thread_command_get_trigger(
4831 notification_thread
, query_target_trigger
, &matching_trigger
);
4832 if (ret_code
!= LTTNG_OK
) {
4836 /* No longer needed. */
4837 query_target_trigger
= nullptr;
4839 if (lttng_error_query_get_target_type(query
) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4840 /* Get the sessiond-side version of the target action. */
4841 query_target_action
=
4842 lttng_error_query_action_borrow_action_target(query
, matching_trigger
);
4845 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4846 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4847 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
, &trigger_owner
);
4848 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4850 results
= lttng_error_query_results_create();
4852 ret_code
= LTTNG_ERR_NOMEM
;
4856 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4858 (int) trigger_owner
,
4859 (int) lttng_credentials_get_uid(cmd_creds
));
4862 * Validate the trigger credentials against the command credentials.
4863 * Only the root user can target a trigger with non-matching
4866 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger
),
4868 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4869 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4871 (int) trigger_owner
,
4872 (int) lttng_credentials_get_uid(cmd_creds
));
4873 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4878 switch (lttng_error_query_get_target_type(query
)) {
4879 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4880 trigger_status
= lttng_trigger_add_error_results(matching_trigger
, results
);
4882 switch (trigger_status
) {
4883 case LTTNG_TRIGGER_STATUS_OK
:
4886 ret_code
= LTTNG_ERR_UNK
;
4891 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4894 lttng_trigger_condition_add_error_results(matching_trigger
, results
);
4896 switch (trigger_status
) {
4897 case LTTNG_TRIGGER_STATUS_OK
:
4900 ret_code
= LTTNG_ERR_UNK
;
4906 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4908 const enum lttng_action_status action_status
=
4909 lttng_action_add_error_query_results(query_target_action
, results
);
4911 switch (action_status
) {
4912 case LTTNG_ACTION_STATUS_OK
:
4915 ret_code
= LTTNG_ERR_UNK
;
4926 *_results
= results
;
4928 ret_code
= LTTNG_OK
;
4930 lttng_trigger_put(matching_trigger
);
4931 lttng_error_query_results_destroy(results
);
4936 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4937 * snapshot output is *not* set with a remote destination.
4939 * Return LTTNG_OK on success or a LTTNG_ERR code.
4941 static enum lttng_error_code
set_relayd_for_snapshot(struct consumer_output
*output
,
4942 const struct ltt_session
*session
)
4944 enum lttng_error_code status
= LTTNG_OK
;
4945 struct lttng_ht_iter iter
;
4946 struct consumer_socket
*socket
;
4947 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4948 const char *base_path
;
4950 LTTNG_ASSERT(output
);
4951 LTTNG_ASSERT(session
);
4953 DBG2("Set relayd object from snapshot output");
4955 if (session
->current_trace_chunk
) {
4956 enum lttng_trace_chunk_status chunk_status
= lttng_trace_chunk_get_id(
4957 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
4959 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4960 current_chunk_id
.is_set
= true;
4962 ERR("Failed to get current trace chunk id");
4963 status
= LTTNG_ERR_UNK
;
4968 /* Ignore if snapshot consumer output is not network. */
4969 if (output
->type
!= CONSUMER_DST_NET
) {
4974 * The snapshot record URI base path overrides the session
4977 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4978 base_path
= output
->dst
.net
.control
.subdir
;
4980 base_path
= session
->base_path
;
4984 * For each consumer socket, create and send the relayd object of the
4988 lttng::urcu::read_lock_guard read_lock
;
4990 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
4991 pthread_mutex_lock(socket
->lock
);
4992 status
= send_consumer_relayd_sockets(
4999 session
->live_timer
,
5000 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
5001 session
->creation_time
,
5002 session
->name_contains_creation_time
);
5003 pthread_mutex_unlock(socket
->lock
);
5004 if (status
!= LTTNG_OK
) {
5015 * Record a kernel snapshot.
5017 * Return LTTNG_OK on success or a LTTNG_ERR code.
5019 static enum lttng_error_code
record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
5020 const struct consumer_output
*output
,
5021 const struct ltt_session
*session
,
5022 uint64_t nb_packets_per_stream
)
5024 enum lttng_error_code status
;
5026 LTTNG_ASSERT(ksess
);
5027 LTTNG_ASSERT(output
);
5028 LTTNG_ASSERT(session
);
5030 status
= kernel_snapshot_record(ksess
, output
, nb_packets_per_stream
);
5035 * Record a UST snapshot.
5037 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5039 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5040 const struct consumer_output
*output
,
5041 const struct ltt_session
*session
,
5042 uint64_t nb_packets_per_stream
)
5044 enum lttng_error_code status
;
5046 LTTNG_ASSERT(usess
);
5047 LTTNG_ASSERT(output
);
5048 LTTNG_ASSERT(session
);
5050 status
= ust_app_snapshot_record(usess
, output
, nb_packets_per_stream
);
5054 static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session
*session
,
5055 uint64_t cur_nr_packets
)
5057 uint64_t tot_size
= 0;
5059 if (session
->kernel_session
) {
5060 struct ltt_kernel_channel
*chan
;
5061 const struct ltt_kernel_session
*ksess
= session
->kernel_session
;
5063 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
5064 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5066 * Don't take channel into account if we
5067 * already grab all its packets.
5071 tot_size
+= chan
->channel
->attr
.subbuf_size
* chan
->stream_count
;
5075 if (session
->ust_session
) {
5076 const struct ltt_ust_session
*usess
= session
->ust_session
;
5078 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
, cur_nr_packets
);
5085 * Calculate the number of packets we can grab from each stream that
5086 * fits within the overall snapshot max size.
5088 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5089 * the number of packets per stream.
5091 * TODO: this approach is not perfect: we consider the worse case
5092 * (packet filling the sub-buffers) as an upper bound, but we could do
5093 * better if we do this calculation while we actually grab the packet
5094 * content: we would know how much padding we don't actually store into
5097 * This algorithm is currently bounded by the number of packets per
5100 * Since we call this algorithm before actually grabbing the data, it's
5101 * an approximation: for instance, applications could appear/disappear
5102 * in between this call and actually grabbing data.
5104 static int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5108 uint64_t cur_nb_packets
= 0;
5111 return 0; /* Infinite */
5114 size_left
= max_size
;
5116 uint64_t one_more_packet_tot_size
;
5118 one_more_packet_tot_size
=
5119 get_session_size_one_more_packet_per_stream(session
, cur_nb_packets
);
5120 if (!one_more_packet_tot_size
) {
5121 /* We are already grabbing all packets. */
5124 size_left
-= one_more_packet_tot_size
;
5125 if (size_left
< 0) {
5130 if (!cur_nb_packets
&& size_left
!= max_size
) {
5131 /* Not enough room to grab one packet of each stream, error. */
5134 return cur_nb_packets
;
5137 static enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5138 const struct snapshot_output
*snapshot_output
)
5140 int64_t nb_packets_per_stream
;
5141 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5143 enum lttng_error_code ret_code
= LTTNG_OK
;
5144 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5145 struct consumer_output
*original_ust_consumer_output
= nullptr;
5146 struct consumer_output
*original_kernel_consumer_output
= nullptr;
5147 struct consumer_output
*snapshot_ust_consumer_output
= nullptr;
5148 struct consumer_output
*snapshot_kernel_consumer_output
= nullptr;
5150 ret
= snprintf(snapshot_chunk_name
,
5151 sizeof(snapshot_chunk_name
),
5153 snapshot_output
->name
,
5154 snapshot_output
->datetime
,
5155 snapshot_output
->nb_snapshot
);
5156 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5157 ERR("Failed to format snapshot name");
5158 ret_code
= LTTNG_ERR_INVALID
;
5161 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5162 snapshot_output
->name
,
5164 snapshot_chunk_name
);
5165 if (!session
->kernel_session
&& !session
->ust_session
) {
5166 ERR("Failed to record snapshot as no channels exist");
5167 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5171 if (session
->kernel_session
) {
5172 original_kernel_consumer_output
= session
->kernel_session
->consumer
;
5173 snapshot_kernel_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5174 strcpy(snapshot_kernel_consumer_output
->chunk_path
, snapshot_chunk_name
);
5176 /* Copy the original domain subdir. */
5177 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5178 original_kernel_consumer_output
->domain_subdir
);
5180 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5181 original_kernel_consumer_output
);
5183 ERR("Failed to copy consumer sockets from snapshot output configuration");
5184 ret_code
= LTTNG_ERR_NOMEM
;
5187 ret_code
= set_relayd_for_snapshot(snapshot_kernel_consumer_output
, session
);
5188 if (ret_code
!= LTTNG_OK
) {
5189 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5192 session
->kernel_session
->consumer
= snapshot_kernel_consumer_output
;
5194 if (session
->ust_session
) {
5195 original_ust_consumer_output
= session
->ust_session
->consumer
;
5196 snapshot_ust_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5197 strcpy(snapshot_ust_consumer_output
->chunk_path
, snapshot_chunk_name
);
5199 /* Copy the original domain subdir. */
5200 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5201 original_ust_consumer_output
->domain_subdir
);
5203 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5204 original_ust_consumer_output
);
5206 ERR("Failed to copy consumer sockets from snapshot output configuration");
5207 ret_code
= LTTNG_ERR_NOMEM
;
5210 ret_code
= set_relayd_for_snapshot(snapshot_ust_consumer_output
, session
);
5211 if (ret_code
!= LTTNG_OK
) {
5212 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5215 session
->ust_session
->consumer
= snapshot_ust_consumer_output
;
5218 snapshot_trace_chunk
= session_create_new_trace_chunk(
5220 snapshot_kernel_consumer_output
?: snapshot_ust_consumer_output
,
5221 consumer_output_get_base_path(snapshot_output
->consumer
),
5222 snapshot_chunk_name
);
5223 if (!snapshot_trace_chunk
) {
5224 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5226 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5229 LTTNG_ASSERT(!session
->current_trace_chunk
);
5230 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, nullptr);
5231 lttng_trace_chunk_put(snapshot_trace_chunk
);
5232 snapshot_trace_chunk
= nullptr;
5234 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5236 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5240 nb_packets_per_stream
=
5241 get_session_nb_packets_per_stream(session
, snapshot_output
->max_size
);
5242 if (nb_packets_per_stream
< 0) {
5243 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5244 goto error_close_trace_chunk
;
5247 if (session
->kernel_session
) {
5248 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5249 snapshot_kernel_consumer_output
,
5251 nb_packets_per_stream
);
5252 if (ret_code
!= LTTNG_OK
) {
5253 goto error_close_trace_chunk
;
5257 if (session
->ust_session
) {
5258 ret_code
= record_ust_snapshot(session
->ust_session
,
5259 snapshot_ust_consumer_output
,
5261 nb_packets_per_stream
);
5262 if (ret_code
!= LTTNG_OK
) {
5263 goto error_close_trace_chunk
;
5267 error_close_trace_chunk
:
5268 if (session_set_trace_chunk(session
, nullptr, &snapshot_trace_chunk
)) {
5269 ERR("Failed to release the current trace chunk of session \"%s\"", session
->name
);
5270 ret_code
= LTTNG_ERR_UNK
;
5273 if (session_close_trace_chunk(session
,
5274 snapshot_trace_chunk
,
5275 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
,
5278 * Don't goto end; make sure the chunk is closed for the session
5279 * to allow future snapshots.
5281 ERR("Failed to close snapshot trace chunk of session \"%s\"", session
->name
);
5282 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5285 lttng_trace_chunk_put(snapshot_trace_chunk
);
5286 snapshot_trace_chunk
= nullptr;
5288 if (original_ust_consumer_output
) {
5289 session
->ust_session
->consumer
= original_ust_consumer_output
;
5291 if (original_kernel_consumer_output
) {
5292 session
->kernel_session
->consumer
= original_kernel_consumer_output
;
5294 consumer_output_put(snapshot_ust_consumer_output
);
5295 consumer_output_put(snapshot_kernel_consumer_output
);
5300 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5302 * The wait parameter is ignored so this call always wait for the snapshot to
5303 * complete before returning.
5305 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5307 int cmd_snapshot_record(struct ltt_session
*session
,
5308 const struct lttng_snapshot_output
*output
,
5309 int wait
__attribute__((unused
)))
5311 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5313 unsigned int snapshot_success
= 0;
5315 struct snapshot_output
*tmp_output
= nullptr;
5317 LTTNG_ASSERT(session
);
5318 LTTNG_ASSERT(output
);
5320 DBG("Cmd snapshot record for session %s", session
->name
);
5322 /* Get the datetime for the snapshot output directory. */
5323 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
, sizeof(datetime
));
5325 cmd_ret
= LTTNG_ERR_INVALID
;
5330 * Permission denied to create an output if the session is not
5331 * set in no output mode.
5333 if (session
->output_traces
) {
5334 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5338 /* The session needs to be started at least once. */
5339 if (!session
->has_been_started
) {
5340 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5344 /* Use temporary output for the session. */
5345 if (*output
->ctrl_url
!= '\0') {
5346 tmp_output
= snapshot_output_alloc();
5348 cmd_ret
= LTTNG_ERR_NOMEM
;
5352 ret
= snapshot_output_init(session
,
5361 if (ret
== -ENOMEM
) {
5362 cmd_ret
= LTTNG_ERR_NOMEM
;
5364 cmd_ret
= LTTNG_ERR_INVALID
;
5368 /* Use the global session count for the temporary snapshot. */
5369 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5371 /* Use the global datetime */
5372 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5373 cmd_ret
= snapshot_record(session
, tmp_output
);
5374 if (cmd_ret
!= LTTNG_OK
) {
5377 snapshot_success
= 1;
5379 struct snapshot_output
*sout
;
5380 struct lttng_ht_iter iter
;
5382 lttng::urcu::read_lock_guard read_lock
;
5384 cds_lfht_for_each_entry (
5385 session
->snapshot
.output_ht
->ht
, &iter
.iter
, sout
, node
.node
) {
5386 struct snapshot_output output_copy
;
5389 * Make a local copy of the output and override output
5390 * parameters with those provided as part of the
5393 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5395 if (output
->max_size
!= (uint64_t) -1ULL) {
5396 output_copy
.max_size
= output
->max_size
;
5399 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5400 memcpy(output_copy
.datetime
, datetime
, sizeof(datetime
));
5402 /* Use temporary name. */
5403 if (*output
->name
!= '\0') {
5404 if (lttng_strncpy(output_copy
.name
,
5406 sizeof(output_copy
.name
))) {
5407 cmd_ret
= LTTNG_ERR_INVALID
;
5412 cmd_ret
= snapshot_record(session
, &output_copy
);
5413 if (cmd_ret
!= LTTNG_OK
) {
5417 snapshot_success
= 1;
5421 if (snapshot_success
) {
5422 session
->snapshot
.nb_snapshot
++;
5424 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5429 snapshot_output_destroy(tmp_output
);
5436 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5438 int cmd_set_session_shm_path(struct ltt_session
*session
, const char *shm_path
)
5441 LTTNG_ASSERT(session
);
5444 * Can only set shm path before session is started.
5446 if (session
->has_been_started
) {
5447 return LTTNG_ERR_SESSION_STARTED
;
5450 strncpy(session
->shm_path
, shm_path
, sizeof(session
->shm_path
));
5451 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5457 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5459 * Ask the consumer to rotate the session output directory.
5460 * The session lock must be held.
5462 * Returns LTTNG_OK on success or else a negative LTTng error code.
5464 int cmd_rotate_session(struct ltt_session
*session
,
5465 struct lttng_rotate_session_return
*rotate_return
,
5466 bool quiet_rotation
,
5467 enum lttng_trace_chunk_command_type command
)
5470 uint64_t ongoing_rotation_chunk_id
;
5471 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5472 struct lttng_trace_chunk
*chunk_being_archived
= nullptr;
5473 struct lttng_trace_chunk
*new_trace_chunk
= nullptr;
5474 enum lttng_trace_chunk_status chunk_status
;
5475 bool failed_to_rotate
= false;
5476 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5478 LTTNG_ASSERT(session
);
5480 if (!session
->has_been_started
) {
5481 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5486 * Explicit rotation is not supported for live sessions.
5487 * However, live sessions can perform a quiet rotation on
5489 * Rotation is not supported for snapshot traces (no output).
5491 if ((!quiet_rotation
&& session
->live_timer
) || !session
->output_traces
) {
5492 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5496 /* Unsupported feature in lttng-relayd before 2.11. */
5497 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5498 (session
->consumer
->relay_major_version
== 2 &&
5499 session
->consumer
->relay_minor_version
< 11)) {
5500 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5504 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5505 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5506 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5510 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5511 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5513 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5518 * After a stop, we only allow one rotation to occur, the other ones are
5519 * useless until a new start.
5521 if (session
->rotated_after_last_stop
) {
5522 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5524 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5529 * After a stop followed by a clear, disallow following rotations a they would
5530 * generate empty chunks.
5532 if (session
->cleared_after_last_stop
) {
5533 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5535 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5539 if (session
->active
) {
5541 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
5542 if (!new_trace_chunk
) {
5543 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5549 * The current trace chunk becomes the chunk being archived.
5551 * After this point, "chunk_being_archived" must absolutely
5552 * be closed on the consumer(s), otherwise it will never be
5553 * cleaned-up, which will result in a leak.
5555 ret
= session_set_trace_chunk(session
, new_trace_chunk
, &chunk_being_archived
);
5557 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5561 if (session
->kernel_session
) {
5562 cmd_ret
= kernel_rotate_session(session
);
5563 if (cmd_ret
!= LTTNG_OK
) {
5564 failed_to_rotate
= true;
5565 rotation_fail_code
= cmd_ret
;
5568 if (session
->ust_session
) {
5569 cmd_ret
= ust_app_rotate_session(session
);
5570 if (cmd_ret
!= LTTNG_OK
) {
5571 failed_to_rotate
= true;
5572 rotation_fail_code
= cmd_ret
;
5576 if (!session
->active
) {
5577 session
->rotated_after_last_stop
= true;
5580 if (!chunk_being_archived
) {
5581 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5583 if (failed_to_rotate
) {
5584 cmd_ret
= rotation_fail_code
;
5591 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5592 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
, &ongoing_rotation_chunk_id
);
5593 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5595 ret
= session_close_trace_chunk(
5596 session
, chunk_being_archived
, command
, session
->last_chunk_path
);
5598 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5602 if (failed_to_rotate
) {
5603 cmd_ret
= rotation_fail_code
;
5607 session
->quiet_rotation
= quiet_rotation
;
5608 ret
= timer_session_rotation_pending_check_start(session
, DEFAULT_ROTATE_PENDING_TIMER
);
5610 cmd_ret
= LTTNG_ERR_UNK
;
5614 if (rotate_return
) {
5615 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5618 session
->chunk_being_archived
= chunk_being_archived
;
5619 chunk_being_archived
= nullptr;
5620 if (!quiet_rotation
) {
5621 ret
= notification_thread_command_session_rotation_ongoing(
5622 the_notification_thread_handle
, session
->id
, ongoing_rotation_chunk_id
);
5623 if (ret
!= LTTNG_OK
) {
5624 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5626 cmd_ret
= (lttng_error_code
) ret
;
5630 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5632 ongoing_rotation_chunk_id
);
5634 lttng_trace_chunk_put(new_trace_chunk
);
5635 lttng_trace_chunk_put(chunk_being_archived
);
5636 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5639 if (session_reset_rotation_state(*session
, LTTNG_ROTATION_STATE_ERROR
)) {
5640 ERR("Failed to reset rotation state of session \"%s\"", session
->name
);
5646 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5648 * Check if the session has finished its rotation.
5650 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5652 int cmd_rotate_get_info(struct ltt_session
*session
,
5653 struct lttng_rotation_get_info_return
*info_return
,
5654 uint64_t rotation_id
)
5656 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5657 enum lttng_rotation_state rotation_state
;
5659 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
,
5661 session
->most_recent_chunk_id
.value
);
5663 if (session
->chunk_being_archived
) {
5664 enum lttng_trace_chunk_status chunk_status
;
5667 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
, &chunk_id
);
5668 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5670 rotation_state
= rotation_id
== chunk_id
? LTTNG_ROTATION_STATE_ONGOING
:
5671 LTTNG_ROTATION_STATE_EXPIRED
;
5673 if (session
->last_archived_chunk_id
.is_set
&&
5674 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5675 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5677 rotation_state
= session
->rotation_state
;
5681 switch (rotation_state
) {
5682 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5683 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5686 case LTTNG_ROTATION_STATE_EXPIRED
:
5687 DBG("Reporting that the rotation state of rotation id %" PRIu64
5688 " of session \"%s\" has expired",
5692 case LTTNG_ROTATION_STATE_ONGOING
:
5693 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5697 case LTTNG_ROTATION_STATE_COMPLETED
:
5701 char *current_tracing_path_reply
;
5702 size_t current_tracing_path_reply_len
;
5704 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5708 switch (session_get_consumer_destination_type(session
)) {
5709 case CONSUMER_DST_LOCAL
:
5710 current_tracing_path_reply
= info_return
->location
.local
.absolute_path
;
5711 current_tracing_path_reply_len
=
5712 sizeof(info_return
->location
.local
.absolute_path
);
5713 info_return
->location_type
=
5714 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5715 fmt_ret
= asprintf(&chunk_path
,
5716 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5717 session_get_base_path(session
),
5718 session
->last_archived_chunk_name
);
5719 if (fmt_ret
== -1) {
5720 PERROR("Failed to format the path of the last archived trace chunk");
5721 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5722 cmd_ret
= LTTNG_ERR_UNK
;
5726 case CONSUMER_DST_NET
:
5728 uint16_t ctrl_port
, data_port
;
5730 current_tracing_path_reply
= info_return
->location
.relay
.relative_path
;
5731 current_tracing_path_reply_len
=
5732 sizeof(info_return
->location
.relay
.relative_path
);
5733 /* Currently the only supported relay protocol. */
5734 info_return
->location
.relay
.protocol
=
5735 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5737 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5738 session_get_net_consumer_hostname(session
),
5739 sizeof(info_return
->location
.relay
.host
));
5741 ERR("Failed to copy host name to rotate_get_info reply");
5742 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5743 cmd_ret
= LTTNG_ERR_SET_URL
;
5747 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5748 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5749 info_return
->location
.relay
.ports
.data
= data_port
;
5750 info_return
->location_type
=
5751 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5752 chunk_path
= strdup(session
->last_chunk_path
);
5754 ERR("Failed to allocate the path of the last archived trace chunk");
5755 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5756 cmd_ret
= LTTNG_ERR_UNK
;
5765 fmt_ret
= lttng_strncpy(
5766 current_tracing_path_reply
, chunk_path
, current_tracing_path_reply_len
);
5769 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5770 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5771 cmd_ret
= LTTNG_ERR_UNK
;
5777 case LTTNG_ROTATION_STATE_ERROR
:
5778 DBG("Reporting that an error occurred during rotation %" PRIu64
5779 " of session \"%s\"",
5789 info_return
->status
= (int32_t) rotation_state
;
5794 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5796 * Configure the automatic rotation parameters.
5797 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5798 * 'activate' to false means deactivate the rotation schedule and validate that
5799 * 'new_value' has the same value as the currently active value.
5801 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5803 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5805 enum lttng_rotation_schedule_type schedule_type
,
5809 uint64_t *parameter_value
;
5811 LTTNG_ASSERT(session
);
5813 DBG("Cmd rotate set schedule session %s", session
->name
);
5815 if (session
->live_timer
|| !session
->output_traces
) {
5816 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5817 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5821 switch (schedule_type
) {
5822 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5823 parameter_value
= &session
->rotate_size
;
5825 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5826 parameter_value
= &session
->rotate_timer_period
;
5827 if (new_value
>= UINT_MAX
) {
5828 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
5832 ret
= LTTNG_ERR_INVALID
;
5837 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5838 ret
= LTTNG_ERR_INVALID
;
5842 /* Improper use of the API. */
5843 if (new_value
== -1ULL) {
5844 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5845 ret
= LTTNG_ERR_INVALID
;
5850 * As indicated in struct ltt_session's comments, a value of == 0 means
5851 * this schedule rotation type is not in use.
5853 * Reject the command if we were asked to activate a schedule that was
5856 if (activate
&& *parameter_value
!= 0) {
5857 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5858 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5863 * Reject the command if we were asked to deactivate a schedule that was
5866 if (!activate
&& *parameter_value
== 0) {
5867 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5868 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5873 * Reject the command if we were asked to deactivate a schedule that
5876 if (!activate
&& *parameter_value
!= new_value
) {
5877 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5878 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5882 *parameter_value
= activate
? new_value
: 0;
5884 switch (schedule_type
) {
5885 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5886 if (activate
&& session
->active
) {
5888 * Only start the timer if the session is active,
5889 * otherwise it will be started when the session starts.
5891 ret
= timer_session_rotation_schedule_timer_start(session
, new_value
);
5893 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5894 ret
= LTTNG_ERR_UNK
;
5898 ret
= timer_session_rotation_schedule_timer_stop(session
);
5900 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5901 ret
= LTTNG_ERR_UNK
;
5906 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5909 the_rotation_thread_handle
->subscribe_session_consumed_size_rotation(
5910 *session
, new_value
);
5911 } catch (const std::exception
& e
) {
5912 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5914 ret
= LTTNG_ERR_UNK
;
5919 the_rotation_thread_handle
5920 ->unsubscribe_session_consumed_size_rotation(*session
);
5921 } catch (const std::exception
& e
) {
5922 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5924 ret
= LTTNG_ERR_UNK
;
5930 /* Would have been caught before. */
5942 /* Wait for a given path to be removed before continuing. */
5943 static enum lttng_error_code
wait_on_path(void *path_data
)
5945 const char *shm_path
= (const char *) path_data
;
5947 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5953 ret
= stat(shm_path
, &st
);
5955 if (errno
!= ENOENT
) {
5956 PERROR("stat() returned an error while checking for the existence of the shm path");
5958 DBG("shm path no longer exists, completing the destruction of session");
5962 if (!S_ISDIR(st
.st_mode
)) {
5963 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5968 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5974 * Returns a pointer to a handler to run on completion of a command.
5975 * Returns NULL if no handler has to be run for the last command executed.
5977 const struct cmd_completion_handler
*cmd_pop_completion_handler()
5979 struct cmd_completion_handler
*handler
= current_completion_handler
;
5981 current_completion_handler
= nullptr;
5986 * Init command subsystem.
5991 * Set network sequence index to 1 for streams to match a relayd
5992 * socket on the consumer side.
5994 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5995 relayd_net_seq_idx
= 1;
5996 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5998 DBG("Command subsystem initialized");