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(tmp_event
,
500 exclusion_names
.size(),
501 exclusion_names
.size() ? exclusion_names
.data() :
503 uevent
->filter_expression
,
507 lttng_event_destroy(tmp_event
);
509 ret_code
= LTTNG_ERR_FATAL
;
515 /* nb_events is already set at this point. */
517 *nb_events
= local_nb_events
;
523 * Fill lttng_event array of all kernel events in the channel.
525 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
526 struct ltt_kernel_session
*kernel_session
,
527 struct lttng_payload
*reply_payload
,
528 unsigned int *nb_events
)
530 enum lttng_error_code ret_code
;
532 struct ltt_kernel_event
*event
;
533 struct ltt_kernel_channel
*kchan
;
535 assert(reply_payload
);
537 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
538 if (kchan
== nullptr) {
539 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
543 *nb_events
= kchan
->event_count
;
545 DBG("Listing events for channel %s", kchan
->channel
->name
);
547 if (*nb_events
== 0) {
552 /* Kernel channels */
553 cds_list_for_each_entry (event
, &kchan
->events_list
.head
, list
) {
554 struct lttng_event
*tmp_event
= lttng_event_create();
557 ret_code
= LTTNG_ERR_NOMEM
;
561 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
562 lttng_event_destroy(tmp_event
);
563 ret_code
= LTTNG_ERR_FATAL
;
567 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
568 tmp_event
->enabled
= event
->enabled
;
569 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
571 switch (event
->event
->instrumentation
) {
572 case LTTNG_KERNEL_ABI_TRACEPOINT
:
573 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
575 case LTTNG_KERNEL_ABI_KRETPROBE
:
576 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
577 memcpy(&tmp_event
->attr
.probe
,
578 &event
->event
->u
.kprobe
,
579 sizeof(struct lttng_kernel_abi_kprobe
));
581 case LTTNG_KERNEL_ABI_KPROBE
:
582 tmp_event
->type
= LTTNG_EVENT_PROBE
;
583 memcpy(&tmp_event
->attr
.probe
,
584 &event
->event
->u
.kprobe
,
585 sizeof(struct lttng_kernel_abi_kprobe
));
587 case LTTNG_KERNEL_ABI_UPROBE
:
588 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
590 case LTTNG_KERNEL_ABI_FUNCTION
:
591 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
592 memcpy(&(tmp_event
->attr
.ftrace
),
593 &event
->event
->u
.ftrace
,
594 sizeof(struct lttng_kernel_abi_function
));
596 case LTTNG_KERNEL_ABI_NOOP
:
597 tmp_event
->type
= LTTNG_EVENT_NOOP
;
599 case LTTNG_KERNEL_ABI_SYSCALL
:
600 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
602 case LTTNG_KERNEL_ABI_ALL
:
609 if (event
->userspace_probe_location
) {
610 struct lttng_userspace_probe_location
*location_copy
=
611 lttng_userspace_probe_location_copy(
612 event
->userspace_probe_location
);
614 if (!location_copy
) {
615 lttng_event_destroy(tmp_event
);
616 ret_code
= LTTNG_ERR_NOMEM
;
620 ret
= lttng_event_set_userspace_probe_location(tmp_event
, location_copy
);
622 lttng_event_destroy(tmp_event
);
623 lttng_userspace_probe_location_destroy(location_copy
);
624 ret_code
= LTTNG_ERR_INVALID
;
629 ret
= lttng_event_serialize(
630 tmp_event
, 0, nullptr, event
->filter_expression
, 0, nullptr, reply_payload
);
631 lttng_event_destroy(tmp_event
);
633 ret_code
= LTTNG_ERR_FATAL
;
644 * Add URI so the consumer output object. Set the correct path depending on the
645 * domain adding the default trace directory.
647 static enum lttng_error_code
add_uri_to_consumer(const struct ltt_session
*session
,
648 struct consumer_output
*consumer
,
649 struct lttng_uri
*uri
,
650 enum lttng_domain_type domain
)
653 enum lttng_error_code ret_code
= LTTNG_OK
;
657 if (consumer
== nullptr) {
658 DBG("No consumer detected. Don't add URI. Stopping.");
659 ret_code
= LTTNG_ERR_NO_CONSUMER
;
664 case LTTNG_DOMAIN_KERNEL
:
665 ret
= lttng_strncpy(consumer
->domain_subdir
,
666 DEFAULT_KERNEL_TRACE_DIR
,
667 sizeof(consumer
->domain_subdir
));
669 case LTTNG_DOMAIN_UST
:
670 ret
= lttng_strncpy(consumer
->domain_subdir
,
671 DEFAULT_UST_TRACE_DIR
,
672 sizeof(consumer
->domain_subdir
));
676 * This case is possible is we try to add the URI to the global
677 * tracing session consumer object which in this case there is
680 memset(consumer
->domain_subdir
, 0, sizeof(consumer
->domain_subdir
));
684 ERR("Failed to initialize consumer output domain subdirectory");
685 ret_code
= LTTNG_ERR_FATAL
;
689 switch (uri
->dtype
) {
692 DBG2("Setting network URI to consumer");
694 if (consumer
->type
== CONSUMER_DST_NET
) {
695 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
696 consumer
->dst
.net
.control_isset
) ||
697 (uri
->stype
== LTTNG_STREAM_DATA
&& consumer
->dst
.net
.data_isset
)) {
698 ret_code
= LTTNG_ERR_URL_EXIST
;
702 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
705 /* Set URI into consumer output object */
706 ret
= consumer_set_network_uri(session
, consumer
, uri
);
708 ret_code
= (lttng_error_code
) -ret
;
710 } else if (ret
== 1) {
712 * URI was the same in the consumer so we do not append the subdir
713 * again so to not duplicate output dir.
720 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
721 ret_code
= LTTNG_ERR_INVALID
;
724 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
725 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
727 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
729 sizeof(consumer
->dst
.session_root_path
));
731 ret_code
= LTTNG_ERR_FATAL
;
734 consumer
->type
= CONSUMER_DST_LOCAL
;
744 * Init tracing by creating trace directory and sending fds kernel consumer.
746 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
749 struct lttng_ht_iter iter
;
750 struct consumer_socket
*socket
;
752 LTTNG_ASSERT(session
);
754 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= nullptr) {
755 lttng::urcu::read_lock_guard read_lock
;
757 cds_lfht_for_each_entry (
758 session
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
759 pthread_mutex_lock(socket
->lock
);
760 ret
= kernel_consumer_send_session(socket
, session
);
761 pthread_mutex_unlock(socket
->lock
);
763 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
774 * Create a socket to the relayd using the URI.
776 * On success, the relayd_sock pointer is set to the created socket.
777 * Else, it remains untouched and an LTTng error code is returned.
779 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
780 struct lttcomm_relayd_sock
**relayd_sock
,
781 struct consumer_output
*consumer
)
784 enum lttng_error_code status
= LTTNG_OK
;
785 struct lttcomm_relayd_sock
*rsock
;
787 rsock
= lttcomm_alloc_relayd_sock(
788 uri
, RELAYD_VERSION_COMM_MAJOR
, RELAYD_VERSION_COMM_MINOR
);
790 status
= LTTNG_ERR_FATAL
;
795 * Connect to relayd so we can proceed with a session creation. This call
796 * can possibly block for an arbitrary amount of time to set the health
797 * state to be in poll execution.
800 ret
= relayd_connect(rsock
);
803 ERR("Unable to reach lttng-relayd");
804 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
808 /* Create socket for control stream. */
809 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
810 uint64_t result_flags
;
812 DBG3("Creating relayd stream socket from URI");
814 /* Check relayd version */
815 ret
= relayd_version_check(rsock
);
816 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
817 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
819 } else if (ret
< 0) {
820 ERR("Unable to reach lttng-relayd");
821 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
824 consumer
->relay_major_version
= rsock
->major
;
825 consumer
->relay_minor_version
= rsock
->minor
;
826 ret
= relayd_get_configuration(rsock
, 0, &result_flags
);
828 ERR("Unable to get relayd configuration");
829 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
832 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
833 consumer
->relay_allows_clear
= true;
835 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
836 DBG3("Creating relayd data socket from URI");
838 /* Command is not valid */
839 ERR("Relayd invalid stream type: %d", uri
->stype
);
840 status
= LTTNG_ERR_INVALID
;
844 *relayd_sock
= rsock
;
849 /* The returned value is not useful since we are on an error path. */
850 (void) relayd_close(rsock
);
858 * Connect to the relayd using URI and send the socket to the right consumer.
860 * The consumer socket lock must be held by the caller.
862 * Returns LTTNG_OK on success or an LTTng error code on failure.
864 static enum lttng_error_code
send_consumer_relayd_socket(unsigned int session_id
,
865 struct lttng_uri
*relayd_uri
,
866 struct consumer_output
*consumer
,
867 struct consumer_socket
*consumer_sock
,
868 const char *session_name
,
869 const char *hostname
,
870 const char *base_path
,
871 int session_live_timer
,
872 const uint64_t *current_chunk_id
,
873 time_t session_creation_time
,
874 bool session_name_contains_creation_time
)
877 struct lttcomm_relayd_sock
*rsock
= nullptr;
878 enum lttng_error_code status
;
880 /* Connect to relayd and make version check if uri is the control. */
881 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
882 if (status
!= LTTNG_OK
) {
883 goto relayd_comm_error
;
887 /* Set the network sequence index if not set. */
888 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
889 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
891 * Increment net_seq_idx because we are about to transfer the
892 * new relayd socket to the consumer.
893 * Assign unique key so the consumer can match streams.
895 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
896 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
899 /* Send relayd socket to consumer. */
900 ret
= consumer_send_relayd_socket(consumer_sock
,
910 session_creation_time
,
911 session_name_contains_creation_time
);
913 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
917 /* Flag that the corresponding socket was sent. */
918 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
919 consumer_sock
->control_sock_sent
= 1;
920 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
921 consumer_sock
->data_sock_sent
= 1;
925 * Close socket which was dup on the consumer side. The session daemon does
926 * NOT keep track of the relayd socket(s) once transfer to the consumer.
930 if (status
!= LTTNG_OK
) {
932 * The consumer output for this session should not be used anymore
933 * since the relayd connection failed thus making any tracing or/and
934 * streaming not usable.
936 consumer
->enabled
= false;
938 (void) relayd_close(rsock
);
946 * Send both relayd sockets to a specific consumer and domain. This is a
947 * helper function to facilitate sending the information to the consumer for a
950 * The consumer socket lock must be held by the caller.
952 * Returns LTTNG_OK, or an LTTng error code on failure.
954 static enum lttng_error_code
send_consumer_relayd_sockets(unsigned int session_id
,
955 struct consumer_output
*consumer
,
956 struct consumer_socket
*sock
,
957 const char *session_name
,
958 const char *hostname
,
959 const char *base_path
,
960 int session_live_timer
,
961 const uint64_t *current_chunk_id
,
962 time_t session_creation_time
,
963 bool session_name_contains_creation_time
)
965 enum lttng_error_code status
= LTTNG_OK
;
967 LTTNG_ASSERT(consumer
);
970 /* Sending control relayd socket. */
971 if (!sock
->control_sock_sent
) {
972 status
= send_consumer_relayd_socket(session_id
,
973 &consumer
->dst
.net
.control
,
981 session_creation_time
,
982 session_name_contains_creation_time
);
983 if (status
!= LTTNG_OK
) {
988 /* Sending data relayd socket. */
989 if (!sock
->data_sock_sent
) {
990 status
= send_consumer_relayd_socket(session_id
,
991 &consumer
->dst
.net
.data
,
999 session_creation_time
,
1000 session_name_contains_creation_time
);
1001 if (status
!= LTTNG_OK
) {
1011 * Setup relayd connections for a tracing session. First creates the socket to
1012 * the relayd and send them to the right domain consumer. Consumer type MUST be
1015 int cmd_setup_relayd(struct ltt_session
*session
)
1018 struct ltt_ust_session
*usess
;
1019 struct ltt_kernel_session
*ksess
;
1020 struct consumer_socket
*socket
;
1021 struct lttng_ht_iter iter
;
1022 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1024 LTTNG_ASSERT(session
);
1026 usess
= session
->ust_session
;
1027 ksess
= session
->kernel_session
;
1029 DBG("Setting relayd for session %s", session
->name
);
1031 if (session
->current_trace_chunk
) {
1032 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1033 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1035 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1036 current_chunk_id
.is_set
= true;
1038 ERR("Failed to get current trace chunk id");
1039 ret
= LTTNG_ERR_UNK
;
1044 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
&&
1045 usess
->consumer
->enabled
) {
1046 /* For each consumer socket, send relayd sockets */
1047 lttng::urcu::read_lock_guard read_lock
;
1049 cds_lfht_for_each_entry (
1050 usess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1051 pthread_mutex_lock(socket
->lock
);
1052 ret
= send_consumer_relayd_sockets(
1059 session
->live_timer
,
1060 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
1061 session
->creation_time
,
1062 session
->name_contains_creation_time
);
1063 pthread_mutex_unlock(socket
->lock
);
1064 if (ret
!= LTTNG_OK
) {
1067 /* Session is now ready for network streaming. */
1068 session
->net_handle
= 1;
1071 session
->consumer
->relay_major_version
= usess
->consumer
->relay_major_version
;
1072 session
->consumer
->relay_minor_version
= usess
->consumer
->relay_minor_version
;
1073 session
->consumer
->relay_allows_clear
= usess
->consumer
->relay_allows_clear
;
1076 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
&&
1077 ksess
->consumer
->enabled
) {
1078 lttng::urcu::read_lock_guard read_lock
;
1080 cds_lfht_for_each_entry (
1081 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1082 pthread_mutex_lock(socket
->lock
);
1083 ret
= send_consumer_relayd_sockets(
1090 session
->live_timer
,
1091 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
1092 session
->creation_time
,
1093 session
->name_contains_creation_time
);
1094 pthread_mutex_unlock(socket
->lock
);
1095 if (ret
!= LTTNG_OK
) {
1098 /* Session is now ready for network streaming. */
1099 session
->net_handle
= 1;
1102 session
->consumer
->relay_major_version
= ksess
->consumer
->relay_major_version
;
1103 session
->consumer
->relay_minor_version
= ksess
->consumer
->relay_minor_version
;
1104 session
->consumer
->relay_allows_clear
= ksess
->consumer
->relay_allows_clear
;
1112 * Start a kernel session by opening all necessary streams.
1114 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1117 struct ltt_kernel_channel
*kchan
;
1119 /* Open kernel metadata */
1120 if (ksess
->metadata
== nullptr && ksess
->output_traces
) {
1121 ret
= kernel_open_metadata(ksess
);
1123 ret
= LTTNG_ERR_KERN_META_FAIL
;
1128 /* Open kernel metadata stream */
1129 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1130 ret
= kernel_open_metadata_stream(ksess
);
1132 ERR("Kernel create metadata stream failed");
1133 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1138 /* For each channel */
1139 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1140 if (kchan
->stream_count
== 0) {
1141 ret
= kernel_open_channel_stream(kchan
);
1143 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1146 /* Update the stream global counter */
1147 ksess
->stream_count_global
+= ret
;
1151 /* Setup kernel consumer socket and send fds to it */
1152 ret
= init_kernel_tracing(ksess
);
1154 ret
= LTTNG_ERR_KERN_START_FAIL
;
1158 /* This start the kernel tracing */
1159 ret
= kernel_start_session(ksess
);
1161 ret
= LTTNG_ERR_KERN_START_FAIL
;
1165 /* Quiescent wait after starting trace */
1166 kernel_wait_quiescent();
1168 ksess
->active
= true;
1176 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1178 struct ltt_kernel_channel
*kchan
;
1179 bool error_occurred
= false;
1182 if (!ksess
|| !ksess
->active
) {
1185 DBG("Stopping kernel tracing");
1187 ret
= kernel_stop_session(ksess
);
1189 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1193 kernel_wait_quiescent();
1195 /* Flush metadata after stopping (if exists) */
1196 if (ksess
->metadata_stream_fd
>= 0) {
1197 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1199 ERR("Kernel metadata flush failed");
1200 error_occurred
= true;
1204 /* Flush all buffers after stopping */
1205 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1206 ret
= kernel_flush_buffer(kchan
);
1208 ERR("Kernel flush buffer error");
1209 error_occurred
= true;
1213 ksess
->active
= false;
1214 if (error_occurred
) {
1215 ret
= LTTNG_ERR_UNK
;
1224 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1226 int cmd_disable_channel(struct ltt_session
*session
,
1227 enum lttng_domain_type domain
,
1231 struct ltt_ust_session
*usess
;
1233 usess
= session
->ust_session
;
1235 lttng::urcu::read_lock_guard read_lock
;
1238 case LTTNG_DOMAIN_KERNEL
:
1240 ret
= channel_kernel_disable(session
->kernel_session
, channel_name
);
1241 if (ret
!= LTTNG_OK
) {
1245 kernel_wait_quiescent();
1248 case LTTNG_DOMAIN_UST
:
1250 struct ltt_ust_channel
*uchan
;
1251 struct lttng_ht
*chan_ht
;
1253 chan_ht
= usess
->domain_global
.channels
;
1255 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1256 if (uchan
== nullptr) {
1257 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1261 ret
= channel_ust_disable(usess
, uchan
);
1262 if (ret
!= LTTNG_OK
) {
1268 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1279 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1281 * The wpipe arguments is used as a notifier for the kernel thread.
1283 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1287 ssize_t sock_recv_len
;
1288 struct lttng_channel
*channel
= nullptr;
1289 struct lttng_buffer_view view
;
1290 struct lttng_dynamic_buffer channel_buffer
;
1291 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1293 lttng_dynamic_buffer_init(&channel_buffer
);
1294 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1295 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1297 ret
= LTTNG_ERR_NOMEM
;
1301 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
, channel_len
);
1302 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1303 ERR("Failed to receive \"enable channel\" command payload");
1304 ret
= LTTNG_ERR_INVALID
;
1308 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1309 if (!lttng_buffer_view_is_valid(&view
)) {
1310 ret
= LTTNG_ERR_INVALID
;
1314 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1315 ERR("Invalid channel payload received in \"enable channel\" command");
1316 ret
= LTTNG_ERR_INVALID
;
1320 ret
= cmd_enable_channel_internal(cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1323 lttng_dynamic_buffer_reset(&channel_buffer
);
1324 lttng_channel_destroy(channel
);
1328 static enum lttng_error_code
cmd_enable_channel_internal(struct ltt_session
*session
,
1329 const struct lttng_domain
*domain
,
1330 const struct lttng_channel
*_attr
,
1333 enum lttng_error_code ret_code
;
1334 struct ltt_ust_session
*usess
= session
->ust_session
;
1335 struct lttng_ht
*chan_ht
;
1337 struct lttng_channel
*attr
= nullptr;
1339 LTTNG_ASSERT(session
);
1340 LTTNG_ASSERT(_attr
);
1341 LTTNG_ASSERT(domain
);
1343 lttng::urcu::read_lock_guard read_lock
;
1345 attr
= lttng_channel_copy(_attr
);
1347 ret_code
= LTTNG_ERR_NOMEM
;
1351 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1353 /* Validate channel name */
1354 if (attr
->name
[0] == '.' || memchr(attr
->name
, '/', len
) != nullptr) {
1355 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1359 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1362 * If the session is a live session, remove the switch timer, the
1363 * live timer does the same thing but sends also synchronisation
1364 * beacons for inactive streams.
1366 if (session
->live_timer
> 0) {
1367 attr
->attr
.live_timer_interval
= session
->live_timer
;
1368 attr
->attr
.switch_timer_interval
= 0;
1371 /* Check for feature support */
1372 switch (domain
->type
) {
1373 case LTTNG_DOMAIN_KERNEL
:
1375 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1376 /* Sampling position of buffer is not supported */
1377 WARN("Kernel tracer does not support buffer monitoring. "
1378 "Setting the monitor interval timer to 0 "
1379 "(disabled) for channel '%s' of session '%s'",
1382 lttng_channel_set_monitor_timer_interval(attr
, 0);
1386 case LTTNG_DOMAIN_UST
:
1388 case LTTNG_DOMAIN_JUL
:
1389 case LTTNG_DOMAIN_LOG4J
:
1390 case LTTNG_DOMAIN_PYTHON
:
1391 if (!agent_tracing_is_enabled()) {
1392 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1393 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1398 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1402 switch (domain
->type
) {
1403 case LTTNG_DOMAIN_KERNEL
:
1405 struct ltt_kernel_channel
*kchan
;
1407 kchan
= trace_kernel_get_channel_by_name(attr
->name
, session
->kernel_session
);
1408 if (kchan
== nullptr) {
1410 * Don't try to create a channel if the session has been started at
1411 * some point in time before. The tracer does not allow it.
1413 if (session
->has_been_started
) {
1414 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1418 if (session
->snapshot
.nb_output
> 0 || session
->snapshot_mode
) {
1419 /* Enforce mmap output for snapshot sessions. */
1420 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1422 ret_code
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1423 if (attr
->name
[0] != '\0') {
1424 session
->kernel_session
->has_non_default_channel
= 1;
1427 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1430 if (ret_code
!= LTTNG_OK
) {
1434 kernel_wait_quiescent();
1437 case LTTNG_DOMAIN_UST
:
1438 case LTTNG_DOMAIN_JUL
:
1439 case LTTNG_DOMAIN_LOG4J
:
1440 case LTTNG_DOMAIN_PYTHON
:
1442 struct ltt_ust_channel
*uchan
;
1447 * Current agent implementation limitations force us to allow
1448 * only one channel at once in "agent" subdomains. Each
1449 * subdomain has a default channel name which must be strictly
1452 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1453 if (strncmp(attr
->name
,
1454 DEFAULT_JUL_CHANNEL_NAME
,
1455 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1456 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1459 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1460 if (strncmp(attr
->name
,
1461 DEFAULT_LOG4J_CHANNEL_NAME
,
1462 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1463 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1466 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1467 if (strncmp(attr
->name
,
1468 DEFAULT_PYTHON_CHANNEL_NAME
,
1469 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1470 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1475 chan_ht
= usess
->domain_global
.channels
;
1477 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1478 if (uchan
== nullptr) {
1480 * Don't try to create a channel if the session has been started at
1481 * some point in time before. The tracer does not allow it.
1483 if (session
->has_been_started
) {
1484 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1488 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1489 if (attr
->name
[0] != '\0') {
1490 usess
->has_non_default_channel
= 1;
1493 ret_code
= channel_ust_enable(usess
, uchan
);
1498 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1502 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1503 session
->has_non_mmap_channel
= true;
1507 lttng_channel_destroy(attr
);
1511 enum lttng_error_code
1512 cmd_process_attr_tracker_get_tracking_policy(struct ltt_session
*session
,
1513 enum lttng_domain_type domain
,
1514 enum lttng_process_attr process_attr
,
1515 enum lttng_tracking_policy
*policy
)
1517 enum lttng_error_code ret_code
= LTTNG_OK
;
1518 const struct process_attr_tracker
*tracker
;
1521 case LTTNG_DOMAIN_KERNEL
:
1522 if (!session
->kernel_session
) {
1523 ret_code
= LTTNG_ERR_INVALID
;
1526 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1528 case LTTNG_DOMAIN_UST
:
1529 if (!session
->ust_session
) {
1530 ret_code
= LTTNG_ERR_INVALID
;
1533 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1536 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1540 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1542 ret_code
= LTTNG_ERR_INVALID
;
1548 enum lttng_error_code
1549 cmd_process_attr_tracker_set_tracking_policy(struct ltt_session
*session
,
1550 enum lttng_domain_type domain
,
1551 enum lttng_process_attr process_attr
,
1552 enum lttng_tracking_policy policy
)
1554 enum lttng_error_code ret_code
= LTTNG_OK
;
1557 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1558 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1559 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1562 ret_code
= LTTNG_ERR_INVALID
;
1567 case LTTNG_DOMAIN_KERNEL
:
1568 if (!session
->kernel_session
) {
1569 ret_code
= LTTNG_ERR_INVALID
;
1572 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1573 session
->kernel_session
, process_attr
, policy
);
1575 case LTTNG_DOMAIN_UST
:
1576 if (!session
->ust_session
) {
1577 ret_code
= LTTNG_ERR_INVALID
;
1580 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1581 session
->ust_session
, process_attr
, policy
);
1584 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1591 enum lttng_error_code
1592 cmd_process_attr_tracker_inclusion_set_add_value(struct ltt_session
*session
,
1593 enum lttng_domain_type domain
,
1594 enum lttng_process_attr process_attr
,
1595 const struct process_attr_value
*value
)
1597 enum lttng_error_code ret_code
= LTTNG_OK
;
1600 case LTTNG_DOMAIN_KERNEL
:
1601 if (!session
->kernel_session
) {
1602 ret_code
= LTTNG_ERR_INVALID
;
1605 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1606 session
->kernel_session
, process_attr
, value
);
1608 case LTTNG_DOMAIN_UST
:
1609 if (!session
->ust_session
) {
1610 ret_code
= LTTNG_ERR_INVALID
;
1613 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1614 session
->ust_session
, process_attr
, value
);
1617 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1624 enum lttng_error_code
1625 cmd_process_attr_tracker_inclusion_set_remove_value(struct ltt_session
*session
,
1626 enum lttng_domain_type domain
,
1627 enum lttng_process_attr process_attr
,
1628 const struct process_attr_value
*value
)
1630 enum lttng_error_code ret_code
= LTTNG_OK
;
1633 case LTTNG_DOMAIN_KERNEL
:
1634 if (!session
->kernel_session
) {
1635 ret_code
= LTTNG_ERR_INVALID
;
1638 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1639 session
->kernel_session
, process_attr
, value
);
1641 case LTTNG_DOMAIN_UST
:
1642 if (!session
->ust_session
) {
1643 ret_code
= LTTNG_ERR_INVALID
;
1646 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1647 session
->ust_session
, process_attr
, value
);
1650 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1657 enum lttng_error_code
1658 cmd_process_attr_tracker_get_inclusion_set(struct ltt_session
*session
,
1659 enum lttng_domain_type domain
,
1660 enum lttng_process_attr process_attr
,
1661 struct lttng_process_attr_values
**values
)
1663 enum lttng_error_code ret_code
= LTTNG_OK
;
1664 const struct process_attr_tracker
*tracker
;
1665 enum process_attr_tracker_status status
;
1668 case LTTNG_DOMAIN_KERNEL
:
1669 if (!session
->kernel_session
) {
1670 ret_code
= LTTNG_ERR_INVALID
;
1673 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1675 case LTTNG_DOMAIN_UST
:
1676 if (!session
->ust_session
) {
1677 ret_code
= LTTNG_ERR_INVALID
;
1680 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1683 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1688 ret_code
= LTTNG_ERR_INVALID
;
1692 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1694 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1695 ret_code
= LTTNG_OK
;
1697 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1698 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1700 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1701 ret_code
= LTTNG_ERR_NOMEM
;
1704 ret_code
= LTTNG_ERR_UNK
;
1713 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1715 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1716 struct lttng_event
*event
,
1717 char *filter_expression
,
1718 struct lttng_bytecode
*bytecode
,
1719 struct lttng_event_exclusion
*exclusion
)
1722 const char *event_name
;
1723 const struct ltt_session
*session
= cmd_ctx
->session
;
1724 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1725 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1727 DBG("Disable event command for event \'%s\'", event
->name
);
1730 * Filter and exclusions are simply not handled by the
1731 * disable event command at this time.
1735 (void) filter_expression
;
1738 /* Ignore the presence of filter or exclusion for the event */
1740 event
->exclusion
= 0;
1742 event_name
= event
->name
;
1744 lttng::urcu::read_lock_guard read_lock
;
1746 /* Error out on unhandled search criteria */
1747 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
|| event
->pid
||
1748 event
->filter
|| event
->exclusion
) {
1749 ret
= LTTNG_ERR_UNK
;
1754 case LTTNG_DOMAIN_KERNEL
:
1756 struct ltt_kernel_channel
*kchan
;
1757 struct ltt_kernel_session
*ksess
;
1759 ksess
= session
->kernel_session
;
1762 * If a non-default channel has been created in the
1763 * session, explicitely require that -c chan_name needs
1766 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1767 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1771 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1772 if (kchan
== nullptr) {
1773 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1777 switch (event
->type
) {
1778 case LTTNG_EVENT_ALL
:
1779 case LTTNG_EVENT_TRACEPOINT
:
1780 case LTTNG_EVENT_SYSCALL
:
1781 case LTTNG_EVENT_PROBE
:
1782 case LTTNG_EVENT_FUNCTION
:
1783 case LTTNG_EVENT_FUNCTION_ENTRY
: /* fall-through */
1784 if (event_name
[0] == '\0') {
1785 ret
= event_kernel_disable_event(kchan
, nullptr, event
->type
);
1787 ret
= event_kernel_disable_event(kchan
, event_name
, event
->type
);
1789 if (ret
!= LTTNG_OK
) {
1794 ret
= LTTNG_ERR_UNK
;
1798 kernel_wait_quiescent();
1801 case LTTNG_DOMAIN_UST
:
1803 struct ltt_ust_channel
*uchan
;
1804 struct ltt_ust_session
*usess
;
1806 usess
= session
->ust_session
;
1808 if (validate_ust_event_name(event_name
)) {
1809 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1814 * If a non-default channel has been created in the
1815 * session, explicitly require that -c chan_name needs
1818 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1819 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1823 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
1824 if (uchan
== nullptr) {
1825 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1829 switch (event
->type
) {
1830 case LTTNG_EVENT_ALL
:
1832 * An empty event name means that everything
1833 * should be disabled.
1835 if (event
->name
[0] == '\0') {
1836 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1838 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1840 if (ret
!= LTTNG_OK
) {
1845 ret
= LTTNG_ERR_UNK
;
1849 DBG3("Disable UST event %s in channel %s completed", event_name
, channel_name
);
1852 case LTTNG_DOMAIN_LOG4J
:
1853 case LTTNG_DOMAIN_JUL
:
1854 case LTTNG_DOMAIN_PYTHON
:
1857 struct ltt_ust_session
*usess
= session
->ust_session
;
1859 LTTNG_ASSERT(usess
);
1861 switch (event
->type
) {
1862 case LTTNG_EVENT_ALL
:
1865 ret
= LTTNG_ERR_UNK
;
1869 agt
= trace_ust_find_agent(usess
, domain
);
1871 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1875 * An empty event name means that everything
1876 * should be disabled.
1878 if (event
->name
[0] == '\0') {
1879 ret
= event_agent_disable_all(usess
, agt
);
1881 ret
= event_agent_disable(usess
, agt
, event_name
);
1883 if (ret
!= LTTNG_OK
) {
1890 ret
= LTTNG_ERR_UND
;
1900 free(filter_expression
);
1905 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1907 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1908 const struct lttng_event_context
*event_context
,
1911 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1912 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1913 const struct ltt_session
*session
= cmd_ctx
->session
;
1914 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1917 * Don't try to add a context if the session has been started at
1918 * some point in time before. The tracer does not allow it and would
1919 * result in a corrupted trace.
1921 if (cmd_ctx
->session
->has_been_started
) {
1922 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1927 case LTTNG_DOMAIN_KERNEL
:
1928 LTTNG_ASSERT(session
->kernel_session
);
1930 if (session
->kernel_session
->channel_count
== 0) {
1931 /* Create default channel */
1932 ret
= channel_kernel_create(session
->kernel_session
, nullptr, kwpipe
);
1933 if (ret
!= LTTNG_OK
) {
1936 chan_kern_created
= 1;
1938 /* Add kernel context to kernel tracer */
1939 ret
= context_kernel_add(session
->kernel_session
, event_context
, channel_name
);
1940 if (ret
!= LTTNG_OK
) {
1944 case LTTNG_DOMAIN_JUL
:
1945 case LTTNG_DOMAIN_LOG4J
:
1948 * Validate channel name.
1949 * If no channel name is given and the domain is JUL or LOG4J,
1950 * set it to the appropriate domain-specific channel name. If
1951 * a name is provided but does not match the expexted channel
1952 * name, return an error.
1954 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1955 strcmp(channel_name
, DEFAULT_JUL_CHANNEL_NAME
) != 0) {
1956 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1958 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1959 strcmp(channel_name
, DEFAULT_LOG4J_CHANNEL_NAME
) != 0) {
1960 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1965 case LTTNG_DOMAIN_UST
:
1967 struct ltt_ust_session
*usess
= session
->ust_session
;
1968 unsigned int chan_count
;
1970 LTTNG_ASSERT(usess
);
1972 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1973 if (chan_count
== 0) {
1974 struct lttng_channel
*attr
;
1975 /* Create default channel */
1976 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1977 if (attr
== nullptr) {
1978 ret
= LTTNG_ERR_FATAL
;
1982 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1983 if (ret
!= LTTNG_OK
) {
1987 channel_attr_destroy(attr
);
1988 chan_ust_created
= 1;
1991 ret
= context_ust_add(usess
, domain
, event_context
, channel_name
);
1992 if (ret
!= LTTNG_OK
) {
1998 ret
= LTTNG_ERR_UND
;
2006 if (chan_kern_created
) {
2007 struct ltt_kernel_channel
*kchan
= trace_kernel_get_channel_by_name(
2008 DEFAULT_CHANNEL_NAME
, session
->kernel_session
);
2009 /* Created previously, this should NOT fail. */
2010 LTTNG_ASSERT(kchan
);
2011 kernel_destroy_channel(kchan
);
2014 if (chan_ust_created
) {
2015 struct ltt_ust_channel
*uchan
= trace_ust_find_channel_by_name(
2016 session
->ust_session
->domain_global
.channels
, DEFAULT_CHANNEL_NAME
);
2017 /* Created previously, this should NOT fail. */
2018 LTTNG_ASSERT(uchan
);
2019 /* Remove from the channel list of the session. */
2020 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
, uchan
);
2021 trace_ust_destroy_channel(uchan
);
2027 static inline bool name_starts_with(const char *name
, const char *prefix
)
2029 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2031 return !strncmp(name
, prefix
, max_cmp_len
);
2034 /* Perform userspace-specific event name validation */
2035 static int validate_ust_event_name(const char *name
)
2045 * Check name against all internal UST event component namespaces used
2048 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2049 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2050 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2059 * Internal version of cmd_enable_event() with a supplemental
2060 * "internal_event" flag which is used to enable internal events which should
2061 * be hidden from clients. Such events are used in the agent implementation to
2062 * enable the events through which all "agent" events are funeled.
2064 static int _cmd_enable_event(struct ltt_session
*session
,
2065 const struct lttng_domain
*domain
,
2067 struct lttng_event
*event
,
2068 char *filter_expression
,
2069 struct lttng_bytecode
*filter
,
2070 struct lttng_event_exclusion
*exclusion
,
2072 bool internal_event
)
2074 int ret
= 0, channel_created
= 0;
2075 struct lttng_channel
*attr
= nullptr;
2077 LTTNG_ASSERT(session
);
2078 LTTNG_ASSERT(event
);
2079 LTTNG_ASSERT(channel_name
);
2081 /* If we have a filter, we must have its filter expression */
2082 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2084 /* Normalize event name as a globbing pattern */
2085 strutils_normalize_star_glob_pattern(event
->name
);
2087 /* Normalize exclusion names as globbing patterns */
2091 for (i
= 0; i
< exclusion
->count
; i
++) {
2092 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2094 strutils_normalize_star_glob_pattern(name
);
2098 lttng::urcu::read_lock_guard read_lock
;
2100 switch (domain
->type
) {
2101 case LTTNG_DOMAIN_KERNEL
:
2103 struct ltt_kernel_channel
*kchan
;
2106 * If a non-default channel has been created in the
2107 * session, explicitely require that -c chan_name needs
2110 if (session
->kernel_session
->has_non_default_channel
&& channel_name
[0] == '\0') {
2111 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2115 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2116 if (kchan
== nullptr) {
2117 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
, LTTNG_BUFFER_GLOBAL
);
2118 if (attr
== nullptr) {
2119 ret
= LTTNG_ERR_FATAL
;
2122 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2123 ret
= LTTNG_ERR_INVALID
;
2127 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2128 if (ret
!= LTTNG_OK
) {
2131 channel_created
= 1;
2134 /* Get the newly created kernel channel pointer */
2135 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2136 if (kchan
== nullptr) {
2137 /* This sould not happen... */
2138 ret
= LTTNG_ERR_FATAL
;
2142 switch (event
->type
) {
2143 case LTTNG_EVENT_ALL
:
2145 char *filter_expression_a
= nullptr;
2146 struct lttng_bytecode
*filter_a
= nullptr;
2149 * We need to duplicate filter_expression and filter,
2150 * because ownership is passed to first enable
2153 if (filter_expression
) {
2154 filter_expression_a
= strdup(filter_expression
);
2155 if (!filter_expression_a
) {
2156 ret
= LTTNG_ERR_FATAL
;
2161 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2163 free(filter_expression_a
);
2164 ret
= LTTNG_ERR_FATAL
;
2167 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2169 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2170 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2171 /* We have passed ownership */
2172 filter_expression
= nullptr;
2174 if (ret
!= LTTNG_OK
) {
2175 if (channel_created
) {
2176 /* Let's not leak a useless channel. */
2177 kernel_destroy_channel(kchan
);
2179 free(filter_expression_a
);
2183 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2184 ret
= event_kernel_enable_event(
2185 kchan
, event
, filter_expression_a
, filter_a
);
2186 /* We have passed ownership */
2187 filter_expression_a
= nullptr;
2189 if (ret
!= LTTNG_OK
) {
2194 case LTTNG_EVENT_PROBE
:
2195 case LTTNG_EVENT_USERSPACE_PROBE
:
2196 case LTTNG_EVENT_FUNCTION
:
2197 case LTTNG_EVENT_FUNCTION_ENTRY
:
2198 case LTTNG_EVENT_TRACEPOINT
:
2199 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2200 /* We have passed ownership */
2201 filter_expression
= nullptr;
2203 if (ret
!= LTTNG_OK
) {
2204 if (channel_created
) {
2205 /* Let's not leak a useless channel. */
2206 kernel_destroy_channel(kchan
);
2211 case LTTNG_EVENT_SYSCALL
:
2212 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2213 /* We have passed ownership */
2214 filter_expression
= nullptr;
2216 if (ret
!= LTTNG_OK
) {
2221 ret
= LTTNG_ERR_UNK
;
2225 kernel_wait_quiescent();
2228 case LTTNG_DOMAIN_UST
:
2230 struct ltt_ust_channel
*uchan
;
2231 struct ltt_ust_session
*usess
= session
->ust_session
;
2233 LTTNG_ASSERT(usess
);
2236 * If a non-default channel has been created in the
2237 * session, explicitely require that -c chan_name needs
2240 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2241 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2245 /* Get channel from global UST domain */
2246 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
2247 if (uchan
== nullptr) {
2248 /* Create default channel */
2249 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, usess
->buffer_type
);
2250 if (attr
== nullptr) {
2251 ret
= LTTNG_ERR_FATAL
;
2254 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2255 ret
= LTTNG_ERR_INVALID
;
2259 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2260 if (ret
!= LTTNG_OK
) {
2264 /* Get the newly created channel reference back */
2265 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2267 LTTNG_ASSERT(uchan
);
2270 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2272 * Don't allow users to add UST events to channels which
2273 * are assigned to a userspace subdomain (JUL, Log4J,
2276 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2280 if (!internal_event
) {
2282 * Ensure the event name is not reserved for internal
2285 ret
= validate_ust_event_name(event
->name
);
2287 WARN("Userspace event name %s failed validation.", event
->name
);
2288 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2293 /* At this point, the session and channel exist on the tracer */
2294 ret
= event_ust_enable_tracepoint(
2295 usess
, uchan
, event
, filter_expression
, filter
, exclusion
, internal_event
);
2296 /* We have passed ownership */
2297 filter_expression
= nullptr;
2299 exclusion
= nullptr;
2300 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2301 goto already_enabled
;
2302 } else if (ret
!= LTTNG_OK
) {
2307 case LTTNG_DOMAIN_LOG4J
:
2308 case LTTNG_DOMAIN_JUL
:
2309 case LTTNG_DOMAIN_PYTHON
:
2311 const char *default_event_name
, *default_chan_name
;
2313 struct lttng_event uevent
;
2314 struct lttng_domain tmp_dom
;
2315 struct ltt_ust_session
*usess
= session
->ust_session
;
2317 LTTNG_ASSERT(usess
);
2319 if (!agent_tracing_is_enabled()) {
2320 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2321 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2325 agt
= trace_ust_find_agent(usess
, domain
->type
);
2327 agt
= agent_create(domain
->type
);
2329 ret
= LTTNG_ERR_NOMEM
;
2332 agent_add(agt
, usess
->agents
);
2335 /* Create the default tracepoint. */
2336 memset(&uevent
, 0, sizeof(uevent
));
2337 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2338 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2339 uevent
.loglevel
= -1;
2340 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
2341 if (!default_event_name
) {
2342 ret
= LTTNG_ERR_FATAL
;
2345 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2346 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2349 * The domain type is changed because we are about to enable the
2350 * default channel and event for the JUL domain that are hardcoded.
2351 * This happens in the UST domain.
2353 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2354 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2356 switch (domain
->type
) {
2357 case LTTNG_DOMAIN_LOG4J
:
2358 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2360 case LTTNG_DOMAIN_JUL
:
2361 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2363 case LTTNG_DOMAIN_PYTHON
:
2364 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2367 /* The switch/case we are in makes this impossible */
2372 char *filter_expression_copy
= nullptr;
2373 struct lttng_bytecode
*filter_copy
= nullptr;
2376 const size_t filter_size
=
2377 sizeof(struct lttng_bytecode
) + filter
->len
;
2379 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2381 ret
= LTTNG_ERR_NOMEM
;
2384 memcpy(filter_copy
, filter
, filter_size
);
2386 filter_expression_copy
= strdup(filter_expression
);
2387 if (!filter_expression
) {
2388 ret
= LTTNG_ERR_NOMEM
;
2391 if (!filter_expression_copy
|| !filter_copy
) {
2392 free(filter_expression_copy
);
2398 ret
= cmd_enable_event_internal(session
,
2400 (char *) default_chan_name
,
2402 filter_expression_copy
,
2408 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2409 goto already_enabled
;
2410 } else if (ret
!= LTTNG_OK
) {
2414 /* The wild card * means that everything should be enabled. */
2415 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2416 ret
= event_agent_enable_all(usess
, agt
, event
, filter
, filter_expression
);
2418 ret
= event_agent_enable(usess
, agt
, event
, filter
, filter_expression
);
2421 filter_expression
= nullptr;
2422 if (ret
!= LTTNG_OK
) {
2429 ret
= LTTNG_ERR_UND
;
2437 free(filter_expression
);
2440 channel_attr_destroy(attr
);
2445 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2446 * We own filter, exclusion, and filter_expression.
2448 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2449 struct lttng_event
*event
,
2450 char *filter_expression
,
2451 struct lttng_event_exclusion
*exclusion
,
2452 struct lttng_bytecode
*bytecode
,
2457 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2459 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2462 * The ownership of the following parameters is transferred to
2463 * _cmd_enable_event:
2465 * - filter_expression,
2469 ret
= _cmd_enable_event(cmd_ctx
->session
,
2471 cmd_ctx
->lsm
.u
.enable
.channel_name
,
2478 filter_expression
= nullptr;
2480 exclusion
= nullptr;
2485 * Enable an event which is internal to LTTng. An internal should
2486 * never be made visible to clients and are immune to checks such as
2489 static int cmd_enable_event_internal(struct ltt_session
*session
,
2490 const struct lttng_domain
*domain
,
2492 struct lttng_event
*event
,
2493 char *filter_expression
,
2494 struct lttng_bytecode
*filter
,
2495 struct lttng_event_exclusion
*exclusion
,
2498 return _cmd_enable_event(session
,
2510 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2512 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2513 struct lttng_payload
*reply_payload
)
2515 enum lttng_error_code ret_code
;
2517 ssize_t i
, nb_events
= 0;
2518 struct lttng_event
*events
= nullptr;
2519 struct lttcomm_list_command_header reply_command_header
= {};
2520 size_t reply_command_header_offset
;
2522 assert(reply_payload
);
2524 /* Reserve space for command reply header. */
2525 reply_command_header_offset
= reply_payload
->buffer
.size
;
2526 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2527 reply_command_header_offset
+
2528 sizeof(struct lttcomm_list_command_header
));
2530 ret_code
= LTTNG_ERR_NOMEM
;
2535 case LTTNG_DOMAIN_KERNEL
:
2536 nb_events
= kernel_list_events(&events
);
2537 if (nb_events
< 0) {
2538 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2542 case LTTNG_DOMAIN_UST
:
2543 nb_events
= ust_app_list_events(&events
);
2544 if (nb_events
< 0) {
2545 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2549 case LTTNG_DOMAIN_LOG4J
:
2550 case LTTNG_DOMAIN_JUL
:
2551 case LTTNG_DOMAIN_PYTHON
:
2552 nb_events
= agent_list_events(&events
, domain
);
2553 if (nb_events
< 0) {
2554 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2559 ret_code
= LTTNG_ERR_UND
;
2563 for (i
= 0; i
< nb_events
; i
++) {
2564 ret
= lttng_event_serialize(
2565 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2567 ret_code
= LTTNG_ERR_NOMEM
;
2572 if (nb_events
> UINT32_MAX
) {
2573 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2574 ret_code
= LTTNG_ERR_OVERFLOW
;
2578 /* Update command reply header. */
2579 reply_command_header
.count
= (uint32_t) nb_events
;
2580 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2581 &reply_command_header
,
2582 sizeof(reply_command_header
));
2584 ret_code
= LTTNG_OK
;
2591 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2593 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2594 struct lttng_payload
*reply
)
2596 enum lttng_error_code ret_code
;
2598 unsigned int i
, nb_fields
;
2599 struct lttng_event_field
*fields
= nullptr;
2600 struct lttcomm_list_command_header reply_command_header
= {};
2601 size_t reply_command_header_offset
;
2605 /* Reserve space for command reply header. */
2606 reply_command_header_offset
= reply
->buffer
.size
;
2607 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2608 reply_command_header_offset
+
2609 sizeof(struct lttcomm_list_command_header
));
2611 ret_code
= LTTNG_ERR_NOMEM
;
2616 case LTTNG_DOMAIN_UST
:
2617 ret
= ust_app_list_event_fields(&fields
);
2619 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2624 case LTTNG_DOMAIN_KERNEL
:
2625 default: /* fall-through */
2626 ret_code
= LTTNG_ERR_UND
;
2632 for (i
= 0; i
< nb_fields
; i
++) {
2633 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2635 ret_code
= LTTNG_ERR_NOMEM
;
2640 if (nb_fields
> UINT32_MAX
) {
2641 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2642 ret_code
= LTTNG_ERR_OVERFLOW
;
2646 /* Update command reply header. */
2647 reply_command_header
.count
= (uint32_t) nb_fields
;
2649 memcpy(reply
->buffer
.data
+ reply_command_header_offset
,
2650 &reply_command_header
,
2651 sizeof(reply_command_header
));
2653 ret_code
= LTTNG_OK
;
2660 enum lttng_error_code
cmd_list_syscalls(struct lttng_payload
*reply_payload
)
2662 enum lttng_error_code ret_code
;
2663 ssize_t nb_events
, i
;
2665 struct lttng_event
*events
= nullptr;
2666 struct lttcomm_list_command_header reply_command_header
= {};
2667 size_t reply_command_header_offset
;
2669 assert(reply_payload
);
2671 /* Reserve space for command reply header. */
2672 reply_command_header_offset
= reply_payload
->buffer
.size
;
2673 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2674 reply_command_header_offset
+
2675 sizeof(struct lttcomm_list_command_header
));
2677 ret_code
= LTTNG_ERR_NOMEM
;
2681 nb_events
= syscall_table_list(&events
);
2682 if (nb_events
< 0) {
2683 ret_code
= (enum lttng_error_code
) - nb_events
;
2687 for (i
= 0; i
< nb_events
; i
++) {
2688 ret
= lttng_event_serialize(
2689 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2691 ret_code
= LTTNG_ERR_NOMEM
;
2696 if (nb_events
> UINT32_MAX
) {
2697 ERR("Syscall count would overflow the syscall listing command's reply");
2698 ret_code
= LTTNG_ERR_OVERFLOW
;
2702 /* Update command reply header. */
2703 reply_command_header
.count
= (uint32_t) nb_events
;
2704 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2705 &reply_command_header
,
2706 sizeof(reply_command_header
));
2708 ret_code
= LTTNG_OK
;
2715 * Command LTTNG_START_TRACE processed by the client thread.
2717 * Called with session mutex held.
2719 int cmd_start_trace(struct ltt_session
*session
)
2721 enum lttng_error_code ret
;
2722 unsigned long nb_chan
= 0;
2723 struct ltt_kernel_session
*ksession
;
2724 struct ltt_ust_session
*usess
;
2725 const bool session_rotated_after_last_stop
= session
->rotated_after_last_stop
;
2726 const bool session_cleared_after_last_stop
= session
->cleared_after_last_stop
;
2728 LTTNG_ASSERT(session
);
2730 /* Ease our life a bit ;) */
2731 ksession
= session
->kernel_session
;
2732 usess
= session
->ust_session
;
2734 /* Is the session already started? */
2735 if (session
->active
) {
2736 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2737 /* Perform nothing */
2741 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2742 !session
->current_trace_chunk
) {
2744 * A rotation was launched while the session was stopped and
2745 * it has not been completed yet. It is not possible to start
2746 * the session since starting the session here would require a
2747 * rotation from "NULL" to a new trace chunk. That rotation
2748 * would overlap with the ongoing rotation, which is not
2751 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2753 ret
= LTTNG_ERR_ROTATION_PENDING
;
2758 * Starting a session without channel is useless since after that it's not
2759 * possible to enable channel thus inform the client.
2761 if (usess
&& usess
->domain_global
.channels
) {
2762 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2765 nb_chan
+= ksession
->channel_count
;
2768 ret
= LTTNG_ERR_NO_CHANNEL
;
2772 session
->active
= true;
2773 session
->rotated_after_last_stop
= false;
2774 session
->cleared_after_last_stop
= false;
2775 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2776 if (!session
->has_been_started
) {
2777 struct lttng_trace_chunk
*trace_chunk
;
2779 DBG("Creating initial trace chunk of session \"%s\"", session
->name
);
2781 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
2783 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2786 LTTNG_ASSERT(!session
->current_trace_chunk
);
2787 ret
= (lttng_error_code
) session_set_trace_chunk(
2788 session
, trace_chunk
, nullptr);
2789 lttng_trace_chunk_put(trace_chunk
);
2791 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2795 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2798 * Rotate existing streams into the new chunk.
2799 * This is a "quiet" rotation has no client has
2800 * explicitly requested this operation.
2802 * There is also no need to wait for the rotation
2803 * to complete as it will happen immediately. No data
2804 * was produced as the session was stopped, so the
2805 * rotation should happen on reception of the command.
2807 ret
= (lttng_error_code
) cmd_rotate_session(
2808 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2809 if (ret
!= LTTNG_OK
) {
2815 /* Kernel tracing */
2816 if (ksession
!= nullptr) {
2817 DBG("Start kernel tracing session %s", session
->name
);
2818 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2819 if (ret
!= LTTNG_OK
) {
2824 /* Flag session that trace should start automatically */
2826 int int_ret
= ust_app_start_trace_all(usess
);
2829 ret
= LTTNG_ERR_UST_START_FAIL
;
2835 * Open a packet in every stream of the session to ensure that viewers
2836 * can correctly identify the boundaries of the periods during which
2837 * tracing was active for this session.
2839 ret
= session_open_packets(session
);
2840 if (ret
!= LTTNG_OK
) {
2845 * Clear the flag that indicates that a rotation was done while the
2846 * session was stopped.
2848 session
->rotated_after_last_stop
= false;
2850 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2851 int int_ret
= timer_session_rotation_schedule_timer_start(
2852 session
, session
->rotate_timer_period
);
2855 ERR("Failed to enable rotate timer");
2856 ret
= LTTNG_ERR_UNK
;
2864 if (ret
== LTTNG_OK
) {
2865 /* Flag this after a successful start. */
2866 session
->has_been_started
= true;
2868 session
->active
= false;
2869 /* Restore initial state on error. */
2870 session
->rotated_after_last_stop
= session_rotated_after_last_stop
;
2871 session
->cleared_after_last_stop
= session_cleared_after_last_stop
;
2878 * Command LTTNG_STOP_TRACE processed by the client thread.
2880 int cmd_stop_trace(struct ltt_session
*session
)
2883 struct ltt_kernel_session
*ksession
;
2884 struct ltt_ust_session
*usess
;
2886 LTTNG_ASSERT(session
);
2888 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2890 ksession
= session
->kernel_session
;
2891 usess
= session
->ust_session
;
2893 /* Session is not active. Skip everything and inform the client. */
2894 if (!session
->active
) {
2895 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2899 ret
= stop_kernel_session(ksession
);
2900 if (ret
!= LTTNG_OK
) {
2904 if (usess
&& usess
->active
) {
2905 ret
= ust_app_stop_trace_all(usess
);
2907 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2912 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2913 /* Flag inactive after a successful stop. */
2914 session
->active
= false;
2922 * Set the base_path of the session only if subdir of a control uris is set.
2923 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2926 set_session_base_path_from_uris(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2931 for (i
= 0; i
< nb_uri
; i
++) {
2932 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
|| uris
[i
].subdir
[0] == '\0') {
2933 /* Not interested in these URIs */
2937 if (session
->base_path
!= nullptr) {
2938 free(session
->base_path
);
2939 session
->base_path
= nullptr;
2942 /* Set session base_path */
2943 session
->base_path
= strdup(uris
[i
].subdir
);
2944 if (!session
->base_path
) {
2945 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2948 ret
= LTTNG_ERR_NOMEM
;
2951 DBG2("Setting base path \"%s\" for session \"%s\"",
2961 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2963 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2966 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2967 struct ltt_ust_session
*usess
= session
->ust_session
;
2969 LTTNG_ASSERT(session
);
2971 LTTNG_ASSERT(nb_uri
> 0);
2973 /* Can't set consumer URI if the session is active. */
2974 if (session
->active
) {
2975 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2980 * Set the session base path if any. This is done inside
2981 * cmd_set_consumer_uri to preserve backward compatibility of the
2982 * previous session creation api vs the session descriptor api.
2984 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2985 if (ret
!= LTTNG_OK
) {
2989 /* Set the "global" consumer URIs */
2990 for (i
= 0; i
< nb_uri
; i
++) {
2991 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
], LTTNG_DOMAIN_NONE
);
2992 if (ret
!= LTTNG_OK
) {
2997 /* Set UST session URIs */
2998 if (session
->ust_session
) {
2999 for (i
= 0; i
< nb_uri
; i
++) {
3000 ret
= add_uri_to_consumer(session
,
3001 session
->ust_session
->consumer
,
3004 if (ret
!= LTTNG_OK
) {
3010 /* Set kernel session URIs */
3011 if (session
->kernel_session
) {
3012 for (i
= 0; i
< nb_uri
; i
++) {
3013 ret
= add_uri_to_consumer(session
,
3014 session
->kernel_session
->consumer
,
3016 LTTNG_DOMAIN_KERNEL
);
3017 if (ret
!= LTTNG_OK
) {
3024 * Make sure to set the session in output mode after we set URI since a
3025 * session can be created without URL (thus flagged in no output mode).
3027 session
->output_traces
= 1;
3029 ksess
->output_traces
= 1;
3033 usess
->output_traces
= 1;
3043 static enum lttng_error_code
3044 set_session_output_from_descriptor(struct ltt_session
*session
,
3045 const struct lttng_session_descriptor
*descriptor
)
3048 enum lttng_error_code ret_code
= LTTNG_OK
;
3049 enum lttng_session_descriptor_type session_type
=
3050 lttng_session_descriptor_get_type(descriptor
);
3051 enum lttng_session_descriptor_output_type output_type
=
3052 lttng_session_descriptor_get_output_type(descriptor
);
3053 struct lttng_uri uris
[2] = {};
3054 size_t uri_count
= 0;
3056 switch (output_type
) {
3057 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3059 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3060 lttng_session_descriptor_get_local_output_uri(descriptor
, &uris
[0]);
3063 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3064 lttng_session_descriptor_get_network_output_uris(descriptor
, &uris
[0], &uris
[1]);
3068 ret_code
= LTTNG_ERR_INVALID
;
3072 switch (session_type
) {
3073 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3075 struct snapshot_output
*new_output
= nullptr;
3077 new_output
= snapshot_output_alloc();
3079 ret_code
= LTTNG_ERR_NOMEM
;
3083 ret
= snapshot_output_init_with_uri(session
,
3084 DEFAULT_SNAPSHOT_MAX_SIZE
,
3090 &session
->snapshot
);
3092 ret_code
= (ret
== -ENOMEM
) ? LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3093 snapshot_output_destroy(new_output
);
3096 snapshot_add_output(&session
->snapshot
, new_output
);
3099 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3100 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3102 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3106 ret_code
= LTTNG_ERR_INVALID
;
3113 static enum lttng_error_code
3114 cmd_create_session_from_descriptor(struct lttng_session_descriptor
*descriptor
,
3115 const lttng_sock_cred
*creds
,
3116 const char *home_path
)
3119 enum lttng_error_code ret_code
;
3120 const char *session_name
;
3121 struct ltt_session
*new_session
= nullptr;
3122 enum lttng_session_descriptor_status descriptor_status
;
3124 session_lock_list();
3126 if (*home_path
!= '/') {
3127 ERR("Home path provided by client is not absolute");
3128 ret_code
= LTTNG_ERR_INVALID
;
3133 descriptor_status
= lttng_session_descriptor_get_session_name(descriptor
, &session_name
);
3134 switch (descriptor_status
) {
3135 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3137 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3138 session_name
= nullptr;
3141 ret_code
= LTTNG_ERR_INVALID
;
3145 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, &new_session
);
3146 if (ret_code
!= LTTNG_OK
) {
3150 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3155 if (ret_code
!= LTTNG_OK
) {
3159 /* Announce the session's destruction to the notification thread when it is destroyed. */
3160 ret
= session_add_destroy_notifier(
3162 [](const struct ltt_session
*session
, void *user_data
__attribute__((unused
))) {
3163 (void) notification_thread_command_remove_session(
3164 the_notification_thread_handle
, session
->id
);
3168 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3170 ret
= LTTNG_ERR_NOMEM
;
3174 if (!session_name
) {
3175 ret
= lttng_session_descriptor_set_session_name(descriptor
, new_session
->name
);
3177 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3182 if (!lttng_session_descriptor_is_output_destination_initialized(descriptor
)) {
3184 * Only include the session's creation time in the output
3185 * destination if the name of the session itself was
3186 * not auto-generated.
3188 ret_code
= lttng_session_descriptor_set_default_output(
3190 session_name
? &new_session
->creation_time
: nullptr,
3192 if (ret_code
!= LTTNG_OK
) {
3196 new_session
->has_user_specified_directory
=
3197 lttng_session_descriptor_has_output_directory(descriptor
);
3200 switch (lttng_session_descriptor_get_type(descriptor
)) {
3201 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3202 new_session
->snapshot_mode
= 1;
3204 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3205 new_session
->live_timer
=
3206 lttng_session_descriptor_live_get_timer_interval(descriptor
);
3212 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3213 if (ret_code
!= LTTNG_OK
) {
3216 new_session
->consumer
->enabled
= true;
3217 ret_code
= LTTNG_OK
;
3219 /* Release reference provided by the session_create function. */
3220 session_put(new_session
);
3221 if (ret_code
!= LTTNG_OK
&& new_session
) {
3222 /* Release the global reference on error. */
3223 session_destroy(new_session
);
3225 session_unlock_list();
3229 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
,
3231 struct lttng_session_descriptor
**return_descriptor
)
3234 size_t payload_size
;
3235 struct lttng_dynamic_buffer payload
;
3236 struct lttng_buffer_view home_dir_view
;
3237 struct lttng_buffer_view session_descriptor_view
;
3238 struct lttng_session_descriptor
*session_descriptor
= nullptr;
3239 enum lttng_error_code ret_code
;
3241 lttng_dynamic_buffer_init(&payload
);
3242 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>= LTTNG_PATH_MAX
) {
3243 ret_code
= LTTNG_ERR_INVALID
;
3246 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3247 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3248 ret_code
= LTTNG_ERR_INVALID
;
3252 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3253 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3254 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3256 ret_code
= LTTNG_ERR_NOMEM
;
3260 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3262 ERR("Reception of session descriptor failed, aborting.");
3263 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3267 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3268 &payload
, 0, cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3269 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3270 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3271 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3272 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3276 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3278 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3279 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3280 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3281 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3282 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3286 ret
= lttng_session_descriptor_create_from_buffer(&session_descriptor_view
,
3287 &session_descriptor
);
3289 ERR("Failed to create session descriptor from payload of \"create session\" command");
3290 ret_code
= LTTNG_ERR_INVALID
;
3295 * Sets the descriptor's auto-generated properties (name, output) if
3298 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3300 home_dir_view
.size
? home_dir_view
.data
:
3302 if (ret_code
!= LTTNG_OK
) {
3306 ret_code
= LTTNG_OK
;
3307 *return_descriptor
= session_descriptor
;
3308 session_descriptor
= nullptr;
3310 lttng_dynamic_buffer_reset(&payload
);
3311 lttng_session_descriptor_destroy(session_descriptor
);
3315 static void cmd_destroy_session_reply(const struct ltt_session
*session
, void *_reply_context
)
3319 const struct cmd_destroy_session_reply_context
*reply_context
=
3320 (cmd_destroy_session_reply_context
*) _reply_context
;
3321 struct lttng_dynamic_buffer payload
;
3322 struct lttcomm_session_destroy_command_header cmd_header
;
3323 struct lttng_trace_archive_location
*location
= nullptr;
3324 struct lttcomm_lttng_msg llm
= {
3325 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION
,
3326 .ret_code
= reply_context
->destruction_status
,
3328 .cmd_header_size
= sizeof(struct lttcomm_session_destroy_command_header
),
3332 size_t payload_size_before_location
;
3334 lttng_dynamic_buffer_init(&payload
);
3336 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3338 ERR("Failed to append session destruction message");
3342 cmd_header
.rotation_state
= (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3343 session
->rotation_state
:
3344 LTTNG_ROTATION_STATE_NO_ROTATION
);
3345 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
, sizeof(cmd_header
));
3347 ERR("Failed to append session destruction command header");
3351 if (!reply_context
->implicit_rotation_on_destroy
) {
3352 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3356 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3357 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3362 location
= session_get_trace_archive_location(session
);
3364 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3369 payload_size_before_location
= payload
.size
;
3370 comm_ret
= lttng_trace_archive_location_serialize(location
, &payload
);
3371 lttng_trace_archive_location_put(location
);
3373 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3377 /* Update the message to indicate the location's length. */
3378 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3379 payload
.size
- payload_size_before_location
;
3381 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
, payload
.data
, payload
.size
);
3382 if (comm_ret
!= (ssize_t
) payload
.size
) {
3383 ERR("Failed to send result of the destruction of session \"%s\" to client",
3387 ret
= close(reply_context
->reply_sock_fd
);
3389 PERROR("Failed to close client socket in deferred session destroy reply");
3391 lttng_dynamic_buffer_reset(&payload
);
3392 free(_reply_context
);
3396 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3398 * Called with session lock held.
3400 int cmd_destroy_session(struct ltt_session
*session
, int *sock_fd
)
3403 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3404 struct cmd_destroy_session_reply_context
*reply_context
= nullptr;
3407 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3408 if (!reply_context
) {
3409 ret
= LTTNG_ERR_NOMEM
;
3413 reply_context
->reply_sock_fd
= *sock_fd
;
3417 LTTNG_ASSERT(session
);
3419 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3420 if (session
->active
) {
3421 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3423 ret
= cmd_stop_trace(session
);
3424 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3425 /* Carry on with the destruction of the session. */
3426 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3428 lttng_strerror(-ret
));
3429 destruction_last_error
= (lttng_error_code
) ret
;
3433 if (session
->rotation_schedule_timer_enabled
) {
3434 if (timer_session_rotation_schedule_timer_stop(session
)) {
3435 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3437 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3441 if (session
->rotate_size
) {
3443 the_rotation_thread_handle
->unsubscribe_session_consumed_size_rotation(
3445 } catch (const std::exception
& e
) {
3446 /* Continue the destruction of the session anyway. */
3447 ERR("Failed to unsubscribe rotation thread notification channel from consumed size condition during session destruction: %s",
3451 session
->rotate_size
= 0;
3454 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3456 * Perform a last rotation on destruction if rotations have
3457 * occurred during the session's lifetime.
3459 ret
= cmd_rotate_session(
3460 session
, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3461 if (ret
!= LTTNG_OK
) {
3462 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3464 lttng_strerror(-ret
));
3465 destruction_last_error
= (lttng_error_code
) -ret
;
3467 if (reply_context
) {
3468 reply_context
->implicit_rotation_on_destroy
= true;
3470 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3472 * The user has not triggered a session rotation. However, to
3473 * ensure all data has been consumed, the session is rotated
3474 * to a 'null' trace chunk before it is destroyed.
3476 * This is a "quiet" rotation meaning that no notification is
3477 * emitted and no renaming of the current trace chunk takes
3480 ret
= cmd_rotate_session(
3481 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3483 * Rotation operations may not be supported by the kernel
3484 * tracer. Hence, do not consider this implicit rotation as
3485 * a session destruction error. The library has already stopped
3486 * the session and waited for pending data; there is nothing
3487 * left to do but complete the destruction of the session.
3489 if (ret
!= LTTNG_OK
&& ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3490 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3492 lttng_strerror(ret
));
3493 destruction_last_error
= (lttng_error_code
) -ret
;
3497 if (session
->shm_path
[0]) {
3499 * When a session is created with an explicit shm_path,
3500 * the consumer daemon will create its shared memory files
3501 * at that location and will *not* unlink them. This is normal
3502 * as the intention of that feature is to make it possible
3503 * to retrieve the content of those files should a crash occur.
3505 * To ensure the content of those files can be used, the
3506 * sessiond daemon will replicate the content of the metadata
3507 * cache in a metadata file.
3509 * On clean-up, it is expected that the consumer daemon will
3510 * unlink the shared memory files and that the session daemon
3511 * will unlink the metadata file. Then, the session's directory
3512 * in the shm path can be removed.
3514 * Unfortunately, a flaw in the design of the sessiond's and
3515 * consumerd's tear down of channels makes it impossible to
3516 * determine when the sessiond _and_ the consumerd have both
3517 * destroyed their representation of a channel. For one, the
3518 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3519 * callbacks in both daemons.
3521 * However, it is also impossible for the sessiond to know when
3522 * the consumer daemon is done destroying its channel(s) since
3523 * it occurs as a reaction to the closing of the channel's file
3524 * descriptor. There is no resulting communication initiated
3525 * from the consumerd to the sessiond to confirm that the
3526 * operation is completed (and was successful).
3528 * Until this is all fixed, the session daemon checks for the
3529 * removal of the session's shm path which makes it possible
3530 * to safely advertise a session as having been destroyed.
3532 * Prior to this fix, it was not possible to reliably save
3533 * a session making use of the --shm-path option, destroy it,
3534 * and load it again. This is because the creation of the
3535 * session would fail upon seeing the session's shm path
3536 * already in existence.
3538 * Note that none of the error paths in the check for the
3539 * directory's existence return an error. This is normal
3540 * as there isn't much that can be done. The session will
3541 * be destroyed properly, except that we can't offer the
3542 * guarantee that the same session can be re-created.
3544 current_completion_handler
= &destroy_completion_handler
.handler
;
3545 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3547 sizeof(destroy_completion_handler
.shm_path
));
3552 * The session is destroyed. However, note that the command context
3553 * still holds a reference to the session, thus delaying its destruction
3554 * _at least_ up to the point when that reference is released.
3556 session_destroy(session
);
3557 if (reply_context
) {
3558 reply_context
->destruction_status
= destruction_last_error
;
3559 ret
= session_add_destroy_notifier(
3560 session
, cmd_destroy_session_reply
, (void *) reply_context
);
3562 ret
= LTTNG_ERR_FATAL
;
3574 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3576 int cmd_register_consumer(struct ltt_session
*session
,
3577 enum lttng_domain_type domain
,
3578 const char *sock_path
,
3579 struct consumer_data
*cdata
)
3582 struct consumer_socket
*socket
= nullptr;
3584 LTTNG_ASSERT(session
);
3585 LTTNG_ASSERT(cdata
);
3586 LTTNG_ASSERT(sock_path
);
3589 case LTTNG_DOMAIN_KERNEL
:
3591 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3593 LTTNG_ASSERT(ksess
);
3595 /* Can't register a consumer if there is already one */
3596 if (ksess
->consumer_fds_sent
!= 0) {
3597 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3601 sock
= lttcomm_connect_unix_sock(sock_path
);
3603 ret
= LTTNG_ERR_CONNECT_FAIL
;
3606 cdata
->cmd_sock
= sock
;
3608 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3609 if (socket
== nullptr) {
3612 PERROR("close register consumer");
3614 cdata
->cmd_sock
= -1;
3615 ret
= LTTNG_ERR_FATAL
;
3619 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3620 if (socket
->lock
== nullptr) {
3621 PERROR("zmalloc pthread mutex");
3622 ret
= LTTNG_ERR_FATAL
;
3626 pthread_mutex_init(socket
->lock
, nullptr);
3627 socket
->registered
= 1;
3629 lttng::urcu::read_lock_guard read_lock
;
3630 consumer_add_socket(socket
, ksess
->consumer
);
3632 pthread_mutex_lock(&cdata
->pid_mutex
);
3634 pthread_mutex_unlock(&cdata
->pid_mutex
);
3639 /* TODO: Userspace tracing */
3640 ret
= LTTNG_ERR_UND
;
3648 consumer_destroy_socket(socket
);
3654 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3656 ssize_t
cmd_list_domains(struct ltt_session
*session
, struct lttng_domain
**domains
)
3661 struct lttng_ht_iter iter
;
3663 if (session
->kernel_session
!= nullptr) {
3664 DBG3("Listing domains found kernel domain");
3668 if (session
->ust_session
!= nullptr) {
3669 DBG3("Listing domains found UST global domain");
3672 lttng::urcu::read_lock_guard read_lock
;
3674 cds_lfht_for_each_entry (
3675 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3676 if (agt
->being_used
) {
3686 *domains
= calloc
<lttng_domain
>(nb_dom
);
3687 if (*domains
== nullptr) {
3688 ret
= LTTNG_ERR_FATAL
;
3692 if (session
->kernel_session
!= nullptr) {
3693 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3695 /* Kernel session buffer type is always GLOBAL */
3696 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3701 if (session
->ust_session
!= nullptr) {
3702 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3703 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3707 lttng::urcu::read_lock_guard read_lock
;
3709 cds_lfht_for_each_entry (
3710 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3711 if (agt
->being_used
) {
3712 (*domains
)[index
].type
= agt
->domain
;
3713 (*domains
)[index
].buf_type
=
3714 session
->ust_session
->buffer_type
;
3724 /* Return negative value to differentiate return code */
3729 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3731 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3732 struct ltt_session
*session
,
3733 struct lttng_payload
*payload
)
3737 struct lttcomm_list_command_header cmd_header
= {};
3738 size_t cmd_header_offset
;
3739 enum lttng_error_code ret_code
;
3744 DBG("Listing channels for session %s", session
->name
);
3746 cmd_header_offset
= payload
->buffer
.size
;
3748 /* Reserve space for command reply header. */
3749 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3750 cmd_header_offset
+ sizeof(cmd_header
));
3752 ret_code
= LTTNG_ERR_NOMEM
;
3757 case LTTNG_DOMAIN_KERNEL
:
3759 /* Kernel channels */
3760 struct ltt_kernel_channel
*kchan
;
3761 if (session
->kernel_session
!= nullptr) {
3762 cds_list_for_each_entry (
3763 kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
3764 uint64_t discarded_events
, lost_packets
;
3765 struct lttng_channel_extended
*extended
;
3767 extended
= (struct lttng_channel_extended
*)
3768 kchan
->channel
->attr
.extended
.ptr
;
3770 ret
= get_kernel_runtime_stats(
3771 session
, kchan
, &discarded_events
, &lost_packets
);
3773 ret_code
= LTTNG_ERR_UNK
;
3778 * Update the discarded_events and lost_packets
3779 * count for the channel
3781 extended
->discarded_events
= discarded_events
;
3782 extended
->lost_packets
= lost_packets
;
3784 ret
= lttng_channel_serialize(kchan
->channel
, &payload
->buffer
);
3786 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3787 kchan
->channel
->name
);
3788 ret_code
= LTTNG_ERR_UNK
;
3797 case LTTNG_DOMAIN_UST
:
3799 struct lttng_ht_iter iter
;
3800 struct ltt_ust_channel
*uchan
;
3803 lttng::urcu::read_lock_guard read_lock
;
3805 cds_lfht_for_each_entry (session
->ust_session
->domain_global
.channels
->ht
,
3809 uint64_t discarded_events
= 0, lost_packets
= 0;
3810 struct lttng_channel
*channel
= nullptr;
3811 struct lttng_channel_extended
*extended
;
3813 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3815 ret_code
= LTTNG_ERR_NOMEM
;
3819 extended
= (struct lttng_channel_extended
*)
3820 channel
->attr
.extended
.ptr
;
3822 ret
= get_ust_runtime_stats(
3823 session
, uchan
, &discarded_events
, &lost_packets
);
3825 lttng_channel_destroy(channel
);
3826 ret_code
= LTTNG_ERR_UNK
;
3830 extended
->discarded_events
= discarded_events
;
3831 extended
->lost_packets
= lost_packets
;
3833 ret
= lttng_channel_serialize(channel
, &payload
->buffer
);
3835 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3837 lttng_channel_destroy(channel
);
3838 ret_code
= LTTNG_ERR_UNK
;
3842 lttng_channel_destroy(channel
);
3853 if (i
> UINT32_MAX
) {
3854 ERR("Channel count would overflow the channel listing command's reply");
3855 ret_code
= LTTNG_ERR_OVERFLOW
;
3859 /* Update command reply header. */
3860 cmd_header
.count
= (uint32_t) i
;
3861 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
, sizeof(cmd_header
));
3862 ret_code
= LTTNG_OK
;
3869 * Command LTTNG_LIST_EVENTS processed by the client thread.
3871 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3872 struct ltt_session
*session
,
3874 struct lttng_payload
*reply_payload
)
3876 int buffer_resize_ret
;
3877 enum lttng_error_code ret_code
= LTTNG_OK
;
3878 struct lttcomm_list_command_header reply_command_header
= {};
3879 size_t reply_command_header_offset
;
3880 unsigned int nb_events
= 0;
3882 assert(reply_payload
);
3884 /* Reserve space for command reply header. */
3885 reply_command_header_offset
= reply_payload
->buffer
.size
;
3886 buffer_resize_ret
= lttng_dynamic_buffer_set_size(
3887 &reply_payload
->buffer
,
3888 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
3889 if (buffer_resize_ret
) {
3890 ret_code
= LTTNG_ERR_NOMEM
;
3895 case LTTNG_DOMAIN_KERNEL
:
3896 if (session
->kernel_session
!= nullptr) {
3897 ret_code
= list_lttng_kernel_events(
3898 channel_name
, session
->kernel_session
, reply_payload
, &nb_events
);
3902 case LTTNG_DOMAIN_UST
:
3904 if (session
->ust_session
!= nullptr) {
3906 list_lttng_ust_global_events(channel_name
,
3907 &session
->ust_session
->domain_global
,
3914 case LTTNG_DOMAIN_LOG4J
:
3915 case LTTNG_DOMAIN_JUL
:
3916 case LTTNG_DOMAIN_PYTHON
:
3917 if (session
->ust_session
) {
3918 struct lttng_ht_iter iter
;
3921 lttng::urcu::read_lock_guard read_lock
;
3923 cds_lfht_for_each_entry (
3924 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3925 if (agt
->domain
== domain
) {
3926 ret_code
= list_lttng_agent_events(
3927 agt
, reply_payload
, &nb_events
);
3934 ret_code
= LTTNG_ERR_UND
;
3938 if (nb_events
> UINT32_MAX
) {
3939 ret_code
= LTTNG_ERR_OVERFLOW
;
3943 /* Update command reply header. */
3944 reply_command_header
.count
= (uint32_t) nb_events
;
3945 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
3946 &reply_command_header
,
3947 sizeof(reply_command_header
));
3954 * Using the session list, filled a lttng_session array to send back to the
3955 * client for session listing.
3957 * The session list lock MUST be acquired before calling this function. Use
3958 * session_lock_list() and session_unlock_list().
3960 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3961 size_t session_count
,
3967 struct ltt_session
*session
;
3968 struct ltt_session_list
*list
= session_get_list();
3969 struct lttng_session_extended
*extended
= (typeof(extended
)) (&sessions
[session_count
]);
3971 DBG("Getting all available session for UID %d GID %d", uid
, gid
);
3973 * Iterate over session list and append data after the control struct in
3976 cds_list_for_each_entry (session
, &list
->head
, list
) {
3977 if (!session_get(session
)) {
3981 * Only list the sessions the user can control.
3983 if (!session_access_ok(session
, uid
) || session
->destroyed
) {
3984 session_put(session
);
3988 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3989 struct ltt_ust_session
*usess
= session
->ust_session
;
3991 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3992 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3993 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3994 ret
= build_network_session_path(
3995 sessions
[i
].path
, sizeof(sessions
[i
].path
), session
);
3997 ret
= snprintf(sessions
[i
].path
,
3998 sizeof(sessions
[i
].path
),
4000 session
->consumer
->dst
.session_root_path
);
4003 PERROR("snprintf session path");
4004 session_put(session
);
4008 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
4009 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
4010 sessions
[i
].enabled
= session
->active
;
4011 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
4012 sessions
[i
].live_timer_interval
= session
->live_timer
;
4013 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
4014 extended
[i
].creation_time
.is_set
= 1;
4016 session_put(session
);
4021 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4022 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4024 int cmd_data_pending(struct ltt_session
*session
)
4027 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4028 struct ltt_ust_session
*usess
= session
->ust_session
;
4030 LTTNG_ASSERT(session
);
4032 DBG("Data pending for session %s", session
->name
);
4034 /* Session MUST be stopped to ask for data availability. */
4035 if (session
->active
) {
4036 ret
= LTTNG_ERR_SESSION_STARTED
;
4040 * If stopped, just make sure we've started before else the above call
4041 * will always send that there is data pending.
4043 * The consumer assumes that when the data pending command is received,
4044 * the trace has been started before or else no output data is written
4045 * by the streams which is a condition for data pending. So, this is
4046 * *VERY* important that we don't ask the consumer before a start
4049 if (!session
->has_been_started
) {
4055 /* A rotation is still pending, we have to wait. */
4056 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4057 DBG("Rotate still pending for session %s", session
->name
);
4062 if (ksess
&& ksess
->consumer
) {
4063 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4065 /* Data is still being extracted for the kernel. */
4070 if (usess
&& usess
->consumer
) {
4071 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4073 /* Data is still being extracted for the kernel. */
4078 /* Data is ready to be read by a viewer */
4086 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4088 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4090 int cmd_snapshot_add_output(struct ltt_session
*session
,
4091 const struct lttng_snapshot_output
*output
,
4095 struct snapshot_output
*new_output
;
4097 LTTNG_ASSERT(session
);
4098 LTTNG_ASSERT(output
);
4100 DBG("Cmd snapshot add output for session %s", session
->name
);
4103 * Can't create an output if the session is not set in no-output mode.
4105 if (session
->output_traces
) {
4106 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4110 if (session
->has_non_mmap_channel
) {
4111 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4115 /* Only one output is allowed until we have the "tee" feature. */
4116 if (session
->snapshot
.nb_output
== 1) {
4117 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4121 new_output
= snapshot_output_alloc();
4123 ret
= LTTNG_ERR_NOMEM
;
4127 ret
= snapshot_output_init(session
,
4134 &session
->snapshot
);
4136 if (ret
== -ENOMEM
) {
4137 ret
= LTTNG_ERR_NOMEM
;
4139 ret
= LTTNG_ERR_INVALID
;
4144 snapshot_add_output(&session
->snapshot
, new_output
);
4146 *id
= new_output
->id
;
4152 snapshot_output_destroy(new_output
);
4158 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4160 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4162 int cmd_snapshot_del_output(struct ltt_session
*session
, const struct lttng_snapshot_output
*output
)
4165 struct snapshot_output
*sout
= nullptr;
4167 LTTNG_ASSERT(session
);
4168 LTTNG_ASSERT(output
);
4170 lttng::urcu::read_lock_guard read_lock
;
4173 * Permission denied to create an output if the session is not
4174 * set in no output mode.
4176 if (session
->output_traces
) {
4177 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4182 DBG("Cmd snapshot del output id %" PRIu32
" for session %s",
4185 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4186 } else if (*output
->name
!= '\0') {
4187 DBG("Cmd snapshot del output name %s for session %s", output
->name
, session
->name
);
4188 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4191 ret
= LTTNG_ERR_INVALID
;
4195 snapshot_delete_output(&session
->snapshot
, sout
);
4196 snapshot_output_destroy(sout
);
4204 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4206 * If no output is available, outputs is untouched and 0 is returned.
4208 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4210 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4211 struct lttng_snapshot_output
**outputs
)
4214 struct lttng_snapshot_output
*list
= nullptr;
4215 struct lttng_ht_iter iter
;
4216 struct snapshot_output
*output
;
4218 LTTNG_ASSERT(session
);
4219 LTTNG_ASSERT(outputs
);
4221 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4224 * Permission denied to create an output if the session is not
4225 * set in no output mode.
4227 if (session
->output_traces
) {
4228 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4232 if (session
->snapshot
.nb_output
== 0) {
4237 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4239 ret
= -LTTNG_ERR_NOMEM
;
4243 /* Copy list from session to the new list object. */
4245 lttng::urcu::read_lock_guard read_lock
;
4247 cds_lfht_for_each_entry (
4248 session
->snapshot
.output_ht
->ht
, &iter
.iter
, output
, node
.node
) {
4249 LTTNG_ASSERT(output
->consumer
);
4250 list
[idx
].id
= output
->id
;
4251 list
[idx
].max_size
= output
->max_size
;
4252 if (lttng_strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
))) {
4253 ret
= -LTTNG_ERR_INVALID
;
4257 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4258 if (lttng_strncpy(list
[idx
].ctrl_url
,
4259 output
->consumer
->dst
.session_root_path
,
4260 sizeof(list
[idx
].ctrl_url
))) {
4261 ret
= -LTTNG_ERR_INVALID
;
4266 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4268 sizeof(list
[idx
].ctrl_url
));
4270 ret
= -LTTNG_ERR_NOMEM
;
4275 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4277 sizeof(list
[idx
].data_url
));
4279 ret
= -LTTNG_ERR_NOMEM
;
4290 ret
= session
->snapshot
.nb_output
;
4298 * Check if we can regenerate the metadata for this session.
4299 * Only kernel, UST per-uid and non-live sessions are supported.
4301 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4303 static int check_regenerate_metadata_support(struct ltt_session
*session
)
4307 LTTNG_ASSERT(session
);
4309 if (session
->live_timer
!= 0) {
4310 ret
= LTTNG_ERR_LIVE_SESSION
;
4313 if (!session
->active
) {
4314 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4317 if (session
->ust_session
) {
4318 switch (session
->ust_session
->buffer_type
) {
4319 case LTTNG_BUFFER_PER_UID
:
4321 case LTTNG_BUFFER_PER_PID
:
4322 ret
= LTTNG_ERR_PER_PID_SESSION
;
4326 ret
= LTTNG_ERR_UNK
;
4330 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4331 session
->consumer
->relay_minor_version
< 8) {
4332 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4342 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4344 * Ask the consumer to truncate the existing metadata file(s) and
4345 * then regenerate the metadata. Live and per-pid sessions are not
4346 * supported and return an error.
4348 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4350 int cmd_regenerate_metadata(struct ltt_session
*session
)
4354 LTTNG_ASSERT(session
);
4356 ret
= check_regenerate_metadata_support(session
);
4361 if (session
->kernel_session
) {
4362 ret
= kernctl_session_regenerate_metadata(session
->kernel_session
->fd
);
4364 ERR("Failed to regenerate the kernel metadata");
4369 if (session
->ust_session
) {
4370 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4372 ERR("Failed to regenerate the UST metadata");
4376 DBG("Cmd metadata regenerate for session %s", session
->name
);
4384 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4386 * Ask the tracer to regenerate a new statedump.
4388 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4390 int cmd_regenerate_statedump(struct ltt_session
*session
)
4394 LTTNG_ASSERT(session
);
4396 if (!session
->active
) {
4397 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4401 if (session
->kernel_session
) {
4402 ret
= kernctl_session_regenerate_statedump(session
->kernel_session
->fd
);
4404 * Currently, the statedump in kernel can only fail if out
4408 if (ret
== -ENOMEM
) {
4409 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4411 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4413 ERR("Failed to regenerate the kernel statedump");
4418 if (session
->ust_session
) {
4419 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4421 * Currently, the statedump in UST always returns 0.
4424 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4425 ERR("Failed to regenerate the UST statedump");
4429 DBG("Cmd regenerate statedump for session %s", session
->name
);
4436 static enum lttng_error_code
4437 synchronize_tracer_notifier_register(struct notification_thread_handle
*notification_thread
,
4438 struct lttng_trigger
*trigger
,
4439 const struct lttng_credentials
*cmd_creds
)
4441 enum lttng_error_code ret_code
;
4442 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4443 const char *trigger_name
;
4444 uid_t trigger_owner
;
4445 enum lttng_trigger_status trigger_status
;
4446 const enum lttng_domain_type trigger_domain
=
4447 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4449 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4450 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4452 LTTNG_ASSERT(condition
);
4453 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4454 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4456 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4457 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4459 session_lock_list();
4460 switch (trigger_domain
) {
4461 case LTTNG_DOMAIN_KERNEL
:
4463 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4464 if (ret_code
!= LTTNG_OK
) {
4465 enum lttng_error_code notif_thread_unregister_ret
;
4467 notif_thread_unregister_ret
=
4468 notification_thread_command_unregister_trigger(notification_thread
,
4471 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4472 /* Return the original error code. */
4473 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4475 (int) trigger_owner
,
4479 goto end_unlock_session_list
;
4483 case LTTNG_DOMAIN_UST
:
4484 ust_app_global_update_all_event_notifier_rules();
4486 case LTTNG_DOMAIN_JUL
:
4487 case LTTNG_DOMAIN_LOG4J
:
4488 case LTTNG_DOMAIN_PYTHON
:
4490 /* Agent domains. */
4491 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4494 agt
= agent_create(trigger_domain
);
4496 ret_code
= LTTNG_ERR_NOMEM
;
4497 goto end_unlock_session_list
;
4500 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4503 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4504 if (ret_code
!= LTTNG_OK
) {
4505 goto end_unlock_session_list
;
4510 case LTTNG_DOMAIN_NONE
:
4515 ret_code
= LTTNG_OK
;
4516 end_unlock_session_list
:
4517 session_unlock_list();
4521 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4522 struct lttng_trigger
*trigger
,
4523 bool is_trigger_anonymous
,
4524 struct notification_thread_handle
*notification_thread
,
4525 struct lttng_trigger
**return_trigger
)
4527 enum lttng_error_code ret_code
;
4528 const char *trigger_name
;
4529 uid_t trigger_owner
;
4530 enum lttng_trigger_status trigger_status
;
4532 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4533 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4535 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4536 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4538 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4540 (int) trigger_owner
,
4541 (int) lttng_credentials_get_uid(cmd_creds
));
4544 * Validate the trigger credentials against the command credentials.
4545 * Only the root user can register a trigger with non-matching
4548 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4549 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4550 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4552 (int) trigger_owner
,
4553 (int) lttng_credentials_get_uid(cmd_creds
));
4554 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4560 * The bytecode generation also serves as a validation step for the
4561 * bytecode expressions.
4563 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4564 if (ret_code
!= LTTNG_OK
) {
4565 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4567 (int) trigger_owner
,
4573 * A reference to the trigger is acquired by the notification thread.
4574 * It is safe to return the same trigger to the caller since it the
4575 * other user holds a reference.
4577 * The trigger is modified during the execution of the
4578 * "register trigger" command. However, by the time the command returns,
4579 * it is safe to use without any locking as its properties are
4582 ret_code
= notification_thread_command_register_trigger(
4583 notification_thread
, trigger
, is_trigger_anonymous
);
4584 if (ret_code
!= LTTNG_OK
) {
4585 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4587 (int) trigger_owner
,
4592 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4593 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4596 * Synchronize tracers if the trigger adds an event notifier.
4598 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4599 ret_code
= synchronize_tracer_notifier_register(
4600 notification_thread
, trigger
, cmd_creds
);
4601 if (ret_code
!= LTTNG_OK
) {
4602 ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code
));
4608 * Return an updated trigger to the client.
4610 * Since a modified version of the same trigger is returned, acquire a
4611 * reference to the trigger so the caller doesn't have to care if those
4612 * are distinct instances or not.
4614 if (ret_code
== LTTNG_OK
) {
4615 lttng_trigger_get(trigger
);
4616 *return_trigger
= trigger
;
4617 /* Ownership of trigger was transferred to caller. */
4624 static enum lttng_error_code
4625 synchronize_tracer_notifier_unregister(const struct lttng_trigger
*trigger
)
4627 enum lttng_error_code ret_code
;
4628 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4629 const enum lttng_domain_type trigger_domain
=
4630 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4632 LTTNG_ASSERT(condition
);
4633 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4634 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4636 session_lock_list();
4637 switch (trigger_domain
) {
4638 case LTTNG_DOMAIN_KERNEL
:
4639 ret_code
= kernel_unregister_event_notifier(trigger
);
4640 if (ret_code
!= LTTNG_OK
) {
4641 goto end_unlock_session_list
;
4645 case LTTNG_DOMAIN_UST
:
4646 ust_app_global_update_all_event_notifier_rules();
4648 case LTTNG_DOMAIN_JUL
:
4649 case LTTNG_DOMAIN_LOG4J
:
4650 case LTTNG_DOMAIN_PYTHON
:
4652 /* Agent domains. */
4653 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4656 * This trigger was never registered in the first place. Calling
4657 * this function under those circumstances is an internal error.
4660 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4661 if (ret_code
!= LTTNG_OK
) {
4662 goto end_unlock_session_list
;
4667 case LTTNG_DOMAIN_NONE
:
4672 ret_code
= LTTNG_OK
;
4674 end_unlock_session_list
:
4675 session_unlock_list();
4679 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4680 const struct lttng_trigger
*trigger
,
4681 struct notification_thread_handle
*notification_thread
)
4683 enum lttng_error_code ret_code
;
4684 const char *trigger_name
;
4685 uid_t trigger_owner
;
4686 enum lttng_trigger_status trigger_status
;
4687 struct lttng_trigger
*sessiond_trigger
= nullptr;
4689 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4690 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4691 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4692 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4694 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4696 (int) trigger_owner
,
4697 (int) lttng_credentials_get_uid(cmd_creds
));
4700 * Validate the trigger credentials against the command credentials.
4701 * Only the root user can unregister a trigger with non-matching
4704 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4705 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4706 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4708 (int) trigger_owner
,
4709 (int) lttng_credentials_get_uid(cmd_creds
));
4710 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4715 /* Fetch the sessiond side trigger object. */
4716 ret_code
= notification_thread_command_get_trigger(
4717 notification_thread
, trigger
, &sessiond_trigger
);
4718 if (ret_code
!= LTTNG_OK
) {
4719 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4721 (int) trigger_owner
,
4726 LTTNG_ASSERT(sessiond_trigger
);
4729 * From this point on, no matter what, consider the trigger
4732 * We set the unregistered state of the sessiond side trigger object in
4733 * the client thread since we want to minimize the possibility of the
4734 * notification thread being stalled due to a long execution of an
4735 * action that required the trigger lock.
4737 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4739 ret_code
= notification_thread_command_unregister_trigger(notification_thread
, trigger
);
4740 if (ret_code
!= LTTNG_OK
) {
4741 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4743 (int) trigger_owner
,
4749 * Synchronize tracers if the trigger removes an event notifier.
4750 * Do this even if the trigger unregistration failed to at least stop
4751 * the tracers from producing notifications associated with this
4754 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4755 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4756 if (ret_code
!= LTTNG_OK
) {
4757 ERR("Error unregistering trigger to tracer.");
4763 lttng_trigger_put(sessiond_trigger
);
4767 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4768 struct notification_thread_handle
*notification_thread
,
4769 struct lttng_triggers
**return_triggers
)
4772 enum lttng_error_code ret_code
;
4773 struct lttng_triggers
*triggers
= nullptr;
4775 /* Get the set of triggers from the notification thread. */
4776 ret_code
= notification_thread_command_list_triggers(
4777 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4778 if (ret_code
!= LTTNG_OK
) {
4782 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4784 ret_code
= LTTNG_ERR_UNK
;
4788 *return_triggers
= triggers
;
4790 ret_code
= LTTNG_OK
;
4792 lttng_triggers_destroy(triggers
);
4796 enum lttng_error_code
4797 cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4798 const struct lttng_error_query
*query
,
4799 struct lttng_error_query_results
**_results
,
4800 struct notification_thread_handle
*notification_thread
)
4802 enum lttng_error_code ret_code
;
4803 const struct lttng_trigger
*query_target_trigger
;
4804 const struct lttng_action
*query_target_action
= nullptr;
4805 struct lttng_trigger
*matching_trigger
= nullptr;
4806 const char *trigger_name
;
4807 uid_t trigger_owner
;
4808 enum lttng_trigger_status trigger_status
;
4809 struct lttng_error_query_results
*results
= nullptr;
4811 switch (lttng_error_query_get_target_type(query
)) {
4812 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4813 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4815 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4816 query_target_trigger
= lttng_error_query_condition_borrow_target(query
);
4818 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4819 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(query
);
4825 LTTNG_ASSERT(query_target_trigger
);
4827 ret_code
= notification_thread_command_get_trigger(
4828 notification_thread
, query_target_trigger
, &matching_trigger
);
4829 if (ret_code
!= LTTNG_OK
) {
4833 /* No longer needed. */
4834 query_target_trigger
= nullptr;
4836 if (lttng_error_query_get_target_type(query
) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4837 /* Get the sessiond-side version of the target action. */
4838 query_target_action
=
4839 lttng_error_query_action_borrow_action_target(query
, matching_trigger
);
4842 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4843 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4844 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
, &trigger_owner
);
4845 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4847 results
= lttng_error_query_results_create();
4849 ret_code
= LTTNG_ERR_NOMEM
;
4853 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4855 (int) trigger_owner
,
4856 (int) lttng_credentials_get_uid(cmd_creds
));
4859 * Validate the trigger credentials against the command credentials.
4860 * Only the root user can target a trigger with non-matching
4863 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger
),
4865 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4866 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4868 (int) trigger_owner
,
4869 (int) lttng_credentials_get_uid(cmd_creds
));
4870 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4875 switch (lttng_error_query_get_target_type(query
)) {
4876 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4877 trigger_status
= lttng_trigger_add_error_results(matching_trigger
, results
);
4879 switch (trigger_status
) {
4880 case LTTNG_TRIGGER_STATUS_OK
:
4883 ret_code
= LTTNG_ERR_UNK
;
4888 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4891 lttng_trigger_condition_add_error_results(matching_trigger
, results
);
4893 switch (trigger_status
) {
4894 case LTTNG_TRIGGER_STATUS_OK
:
4897 ret_code
= LTTNG_ERR_UNK
;
4903 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4905 const enum lttng_action_status action_status
=
4906 lttng_action_add_error_query_results(query_target_action
, results
);
4908 switch (action_status
) {
4909 case LTTNG_ACTION_STATUS_OK
:
4912 ret_code
= LTTNG_ERR_UNK
;
4923 *_results
= results
;
4925 ret_code
= LTTNG_OK
;
4927 lttng_trigger_put(matching_trigger
);
4928 lttng_error_query_results_destroy(results
);
4933 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4934 * snapshot output is *not* set with a remote destination.
4936 * Return LTTNG_OK on success or a LTTNG_ERR code.
4938 static enum lttng_error_code
set_relayd_for_snapshot(struct consumer_output
*output
,
4939 const struct ltt_session
*session
)
4941 enum lttng_error_code status
= LTTNG_OK
;
4942 struct lttng_ht_iter iter
;
4943 struct consumer_socket
*socket
;
4944 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4945 const char *base_path
;
4947 LTTNG_ASSERT(output
);
4948 LTTNG_ASSERT(session
);
4950 DBG2("Set relayd object from snapshot output");
4952 if (session
->current_trace_chunk
) {
4953 enum lttng_trace_chunk_status chunk_status
= lttng_trace_chunk_get_id(
4954 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
4956 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4957 current_chunk_id
.is_set
= true;
4959 ERR("Failed to get current trace chunk id");
4960 status
= LTTNG_ERR_UNK
;
4965 /* Ignore if snapshot consumer output is not network. */
4966 if (output
->type
!= CONSUMER_DST_NET
) {
4971 * The snapshot record URI base path overrides the session
4974 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4975 base_path
= output
->dst
.net
.control
.subdir
;
4977 base_path
= session
->base_path
;
4981 * For each consumer socket, create and send the relayd object of the
4985 lttng::urcu::read_lock_guard read_lock
;
4987 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
4988 pthread_mutex_lock(socket
->lock
);
4989 status
= send_consumer_relayd_sockets(
4996 session
->live_timer
,
4997 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
4998 session
->creation_time
,
4999 session
->name_contains_creation_time
);
5000 pthread_mutex_unlock(socket
->lock
);
5001 if (status
!= LTTNG_OK
) {
5012 * Record a kernel snapshot.
5014 * Return LTTNG_OK on success or a LTTNG_ERR code.
5016 static enum lttng_error_code
record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
5017 const struct consumer_output
*output
,
5018 const struct ltt_session
*session
,
5019 uint64_t nb_packets_per_stream
)
5021 enum lttng_error_code status
;
5023 LTTNG_ASSERT(ksess
);
5024 LTTNG_ASSERT(output
);
5025 LTTNG_ASSERT(session
);
5027 status
= kernel_snapshot_record(ksess
, output
, nb_packets_per_stream
);
5032 * Record a UST snapshot.
5034 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5036 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5037 const struct consumer_output
*output
,
5038 const struct ltt_session
*session
,
5039 uint64_t nb_packets_per_stream
)
5041 enum lttng_error_code status
;
5043 LTTNG_ASSERT(usess
);
5044 LTTNG_ASSERT(output
);
5045 LTTNG_ASSERT(session
);
5047 status
= ust_app_snapshot_record(usess
, output
, nb_packets_per_stream
);
5051 static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session
*session
,
5052 uint64_t cur_nr_packets
)
5054 uint64_t tot_size
= 0;
5056 if (session
->kernel_session
) {
5057 struct ltt_kernel_channel
*chan
;
5058 const struct ltt_kernel_session
*ksess
= session
->kernel_session
;
5060 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
5061 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5063 * Don't take channel into account if we
5064 * already grab all its packets.
5068 tot_size
+= chan
->channel
->attr
.subbuf_size
* chan
->stream_count
;
5072 if (session
->ust_session
) {
5073 const struct ltt_ust_session
*usess
= session
->ust_session
;
5075 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
, cur_nr_packets
);
5082 * Calculate the number of packets we can grab from each stream that
5083 * fits within the overall snapshot max size.
5085 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5086 * the number of packets per stream.
5088 * TODO: this approach is not perfect: we consider the worse case
5089 * (packet filling the sub-buffers) as an upper bound, but we could do
5090 * better if we do this calculation while we actually grab the packet
5091 * content: we would know how much padding we don't actually store into
5094 * This algorithm is currently bounded by the number of packets per
5097 * Since we call this algorithm before actually grabbing the data, it's
5098 * an approximation: for instance, applications could appear/disappear
5099 * in between this call and actually grabbing data.
5101 static int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5105 uint64_t cur_nb_packets
= 0;
5108 return 0; /* Infinite */
5111 size_left
= max_size
;
5113 uint64_t one_more_packet_tot_size
;
5115 one_more_packet_tot_size
=
5116 get_session_size_one_more_packet_per_stream(session
, cur_nb_packets
);
5117 if (!one_more_packet_tot_size
) {
5118 /* We are already grabbing all packets. */
5121 size_left
-= one_more_packet_tot_size
;
5122 if (size_left
< 0) {
5127 if (!cur_nb_packets
&& size_left
!= max_size
) {
5128 /* Not enough room to grab one packet of each stream, error. */
5131 return cur_nb_packets
;
5134 static enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5135 const struct snapshot_output
*snapshot_output
)
5137 int64_t nb_packets_per_stream
;
5138 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5140 enum lttng_error_code ret_code
= LTTNG_OK
;
5141 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5142 struct consumer_output
*original_ust_consumer_output
= nullptr;
5143 struct consumer_output
*original_kernel_consumer_output
= nullptr;
5144 struct consumer_output
*snapshot_ust_consumer_output
= nullptr;
5145 struct consumer_output
*snapshot_kernel_consumer_output
= nullptr;
5147 ret
= snprintf(snapshot_chunk_name
,
5148 sizeof(snapshot_chunk_name
),
5150 snapshot_output
->name
,
5151 snapshot_output
->datetime
,
5152 snapshot_output
->nb_snapshot
);
5153 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5154 ERR("Failed to format snapshot name");
5155 ret_code
= LTTNG_ERR_INVALID
;
5158 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5159 snapshot_output
->name
,
5161 snapshot_chunk_name
);
5162 if (!session
->kernel_session
&& !session
->ust_session
) {
5163 ERR("Failed to record snapshot as no channels exist");
5164 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5168 if (session
->kernel_session
) {
5169 original_kernel_consumer_output
= session
->kernel_session
->consumer
;
5170 snapshot_kernel_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5171 strcpy(snapshot_kernel_consumer_output
->chunk_path
, snapshot_chunk_name
);
5173 /* Copy the original domain subdir. */
5174 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5175 original_kernel_consumer_output
->domain_subdir
);
5177 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5178 original_kernel_consumer_output
);
5180 ERR("Failed to copy consumer sockets from snapshot output configuration");
5181 ret_code
= LTTNG_ERR_NOMEM
;
5184 ret_code
= set_relayd_for_snapshot(snapshot_kernel_consumer_output
, session
);
5185 if (ret_code
!= LTTNG_OK
) {
5186 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5189 session
->kernel_session
->consumer
= snapshot_kernel_consumer_output
;
5191 if (session
->ust_session
) {
5192 original_ust_consumer_output
= session
->ust_session
->consumer
;
5193 snapshot_ust_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5194 strcpy(snapshot_ust_consumer_output
->chunk_path
, snapshot_chunk_name
);
5196 /* Copy the original domain subdir. */
5197 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5198 original_ust_consumer_output
->domain_subdir
);
5200 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5201 original_ust_consumer_output
);
5203 ERR("Failed to copy consumer sockets from snapshot output configuration");
5204 ret_code
= LTTNG_ERR_NOMEM
;
5207 ret_code
= set_relayd_for_snapshot(snapshot_ust_consumer_output
, session
);
5208 if (ret_code
!= LTTNG_OK
) {
5209 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5212 session
->ust_session
->consumer
= snapshot_ust_consumer_output
;
5215 snapshot_trace_chunk
= session_create_new_trace_chunk(
5217 snapshot_kernel_consumer_output
?: snapshot_ust_consumer_output
,
5218 consumer_output_get_base_path(snapshot_output
->consumer
),
5219 snapshot_chunk_name
);
5220 if (!snapshot_trace_chunk
) {
5221 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5223 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5226 LTTNG_ASSERT(!session
->current_trace_chunk
);
5227 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, nullptr);
5228 lttng_trace_chunk_put(snapshot_trace_chunk
);
5229 snapshot_trace_chunk
= nullptr;
5231 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5233 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5237 nb_packets_per_stream
=
5238 get_session_nb_packets_per_stream(session
, snapshot_output
->max_size
);
5239 if (nb_packets_per_stream
< 0) {
5240 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5241 goto error_close_trace_chunk
;
5244 if (session
->kernel_session
) {
5245 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5246 snapshot_kernel_consumer_output
,
5248 nb_packets_per_stream
);
5249 if (ret_code
!= LTTNG_OK
) {
5250 goto error_close_trace_chunk
;
5254 if (session
->ust_session
) {
5255 ret_code
= record_ust_snapshot(session
->ust_session
,
5256 snapshot_ust_consumer_output
,
5258 nb_packets_per_stream
);
5259 if (ret_code
!= LTTNG_OK
) {
5260 goto error_close_trace_chunk
;
5264 error_close_trace_chunk
:
5265 if (session_set_trace_chunk(session
, nullptr, &snapshot_trace_chunk
)) {
5266 ERR("Failed to release the current trace chunk of session \"%s\"", session
->name
);
5267 ret_code
= LTTNG_ERR_UNK
;
5270 if (session_close_trace_chunk(session
,
5271 snapshot_trace_chunk
,
5272 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
,
5275 * Don't goto end; make sure the chunk is closed for the session
5276 * to allow future snapshots.
5278 ERR("Failed to close snapshot trace chunk of session \"%s\"", session
->name
);
5279 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5282 lttng_trace_chunk_put(snapshot_trace_chunk
);
5283 snapshot_trace_chunk
= nullptr;
5285 if (original_ust_consumer_output
) {
5286 session
->ust_session
->consumer
= original_ust_consumer_output
;
5288 if (original_kernel_consumer_output
) {
5289 session
->kernel_session
->consumer
= original_kernel_consumer_output
;
5291 consumer_output_put(snapshot_ust_consumer_output
);
5292 consumer_output_put(snapshot_kernel_consumer_output
);
5297 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5299 * The wait parameter is ignored so this call always wait for the snapshot to
5300 * complete before returning.
5302 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5304 int cmd_snapshot_record(struct ltt_session
*session
,
5305 const struct lttng_snapshot_output
*output
,
5306 int wait
__attribute__((unused
)))
5308 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5310 unsigned int snapshot_success
= 0;
5312 struct snapshot_output
*tmp_output
= nullptr;
5314 LTTNG_ASSERT(session
);
5315 LTTNG_ASSERT(output
);
5317 DBG("Cmd snapshot record for session %s", session
->name
);
5319 /* Get the datetime for the snapshot output directory. */
5320 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
, sizeof(datetime
));
5322 cmd_ret
= LTTNG_ERR_INVALID
;
5327 * Permission denied to create an output if the session is not
5328 * set in no output mode.
5330 if (session
->output_traces
) {
5331 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5335 /* The session needs to be started at least once. */
5336 if (!session
->has_been_started
) {
5337 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5341 /* Use temporary output for the session. */
5342 if (*output
->ctrl_url
!= '\0') {
5343 tmp_output
= snapshot_output_alloc();
5345 cmd_ret
= LTTNG_ERR_NOMEM
;
5349 ret
= snapshot_output_init(session
,
5358 if (ret
== -ENOMEM
) {
5359 cmd_ret
= LTTNG_ERR_NOMEM
;
5361 cmd_ret
= LTTNG_ERR_INVALID
;
5365 /* Use the global session count for the temporary snapshot. */
5366 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5368 /* Use the global datetime */
5369 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5370 cmd_ret
= snapshot_record(session
, tmp_output
);
5371 if (cmd_ret
!= LTTNG_OK
) {
5374 snapshot_success
= 1;
5376 struct snapshot_output
*sout
;
5377 struct lttng_ht_iter iter
;
5379 lttng::urcu::read_lock_guard read_lock
;
5381 cds_lfht_for_each_entry (
5382 session
->snapshot
.output_ht
->ht
, &iter
.iter
, sout
, node
.node
) {
5383 struct snapshot_output output_copy
;
5386 * Make a local copy of the output and override output
5387 * parameters with those provided as part of the
5390 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5392 if (output
->max_size
!= (uint64_t) -1ULL) {
5393 output_copy
.max_size
= output
->max_size
;
5396 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5397 memcpy(output_copy
.datetime
, datetime
, sizeof(datetime
));
5399 /* Use temporary name. */
5400 if (*output
->name
!= '\0') {
5401 if (lttng_strncpy(output_copy
.name
,
5403 sizeof(output_copy
.name
))) {
5404 cmd_ret
= LTTNG_ERR_INVALID
;
5409 cmd_ret
= snapshot_record(session
, &output_copy
);
5410 if (cmd_ret
!= LTTNG_OK
) {
5414 snapshot_success
= 1;
5418 if (snapshot_success
) {
5419 session
->snapshot
.nb_snapshot
++;
5421 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5426 snapshot_output_destroy(tmp_output
);
5433 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5435 int cmd_set_session_shm_path(struct ltt_session
*session
, const char *shm_path
)
5438 LTTNG_ASSERT(session
);
5441 * Can only set shm path before session is started.
5443 if (session
->has_been_started
) {
5444 return LTTNG_ERR_SESSION_STARTED
;
5447 strncpy(session
->shm_path
, shm_path
, sizeof(session
->shm_path
));
5448 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5454 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5456 * Ask the consumer to rotate the session output directory.
5457 * The session lock must be held.
5459 * Returns LTTNG_OK on success or else a negative LTTng error code.
5461 int cmd_rotate_session(struct ltt_session
*session
,
5462 struct lttng_rotate_session_return
*rotate_return
,
5463 bool quiet_rotation
,
5464 enum lttng_trace_chunk_command_type command
)
5467 uint64_t ongoing_rotation_chunk_id
;
5468 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5469 struct lttng_trace_chunk
*chunk_being_archived
= nullptr;
5470 struct lttng_trace_chunk
*new_trace_chunk
= nullptr;
5471 enum lttng_trace_chunk_status chunk_status
;
5472 bool failed_to_rotate
= false;
5473 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5475 LTTNG_ASSERT(session
);
5477 if (!session
->has_been_started
) {
5478 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5483 * Explicit rotation is not supported for live sessions.
5484 * However, live sessions can perform a quiet rotation on
5486 * Rotation is not supported for snapshot traces (no output).
5488 if ((!quiet_rotation
&& session
->live_timer
) || !session
->output_traces
) {
5489 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5493 /* Unsupported feature in lttng-relayd before 2.11. */
5494 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5495 (session
->consumer
->relay_major_version
== 2 &&
5496 session
->consumer
->relay_minor_version
< 11)) {
5497 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5501 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5502 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5503 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5507 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5508 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5510 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5515 * After a stop, we only allow one rotation to occur, the other ones are
5516 * useless until a new start.
5518 if (session
->rotated_after_last_stop
) {
5519 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5521 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5526 * After a stop followed by a clear, disallow following rotations a they would
5527 * generate empty chunks.
5529 if (session
->cleared_after_last_stop
) {
5530 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5532 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5536 if (session
->active
) {
5538 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
5539 if (!new_trace_chunk
) {
5540 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5546 * The current trace chunk becomes the chunk being archived.
5548 * After this point, "chunk_being_archived" must absolutely
5549 * be closed on the consumer(s), otherwise it will never be
5550 * cleaned-up, which will result in a leak.
5552 ret
= session_set_trace_chunk(session
, new_trace_chunk
, &chunk_being_archived
);
5554 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5558 if (session
->kernel_session
) {
5559 cmd_ret
= kernel_rotate_session(session
);
5560 if (cmd_ret
!= LTTNG_OK
) {
5561 failed_to_rotate
= true;
5562 rotation_fail_code
= cmd_ret
;
5565 if (session
->ust_session
) {
5566 cmd_ret
= ust_app_rotate_session(session
);
5567 if (cmd_ret
!= LTTNG_OK
) {
5568 failed_to_rotate
= true;
5569 rotation_fail_code
= cmd_ret
;
5573 if (!session
->active
) {
5574 session
->rotated_after_last_stop
= true;
5577 if (!chunk_being_archived
) {
5578 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5580 if (failed_to_rotate
) {
5581 cmd_ret
= rotation_fail_code
;
5588 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5589 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
, &ongoing_rotation_chunk_id
);
5590 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5592 ret
= session_close_trace_chunk(
5593 session
, chunk_being_archived
, command
, session
->last_chunk_path
);
5595 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5599 if (failed_to_rotate
) {
5600 cmd_ret
= rotation_fail_code
;
5604 session
->quiet_rotation
= quiet_rotation
;
5605 ret
= timer_session_rotation_pending_check_start(session
, DEFAULT_ROTATE_PENDING_TIMER
);
5607 cmd_ret
= LTTNG_ERR_UNK
;
5611 if (rotate_return
) {
5612 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5615 session
->chunk_being_archived
= chunk_being_archived
;
5616 chunk_being_archived
= nullptr;
5617 if (!quiet_rotation
) {
5618 ret
= notification_thread_command_session_rotation_ongoing(
5619 the_notification_thread_handle
, session
->id
, ongoing_rotation_chunk_id
);
5620 if (ret
!= LTTNG_OK
) {
5621 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5623 cmd_ret
= (lttng_error_code
) ret
;
5627 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5629 ongoing_rotation_chunk_id
);
5631 lttng_trace_chunk_put(new_trace_chunk
);
5632 lttng_trace_chunk_put(chunk_being_archived
);
5633 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5636 if (session_reset_rotation_state(*session
, LTTNG_ROTATION_STATE_ERROR
)) {
5637 ERR("Failed to reset rotation state of session \"%s\"", session
->name
);
5643 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5645 * Check if the session has finished its rotation.
5647 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5649 int cmd_rotate_get_info(struct ltt_session
*session
,
5650 struct lttng_rotation_get_info_return
*info_return
,
5651 uint64_t rotation_id
)
5653 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5654 enum lttng_rotation_state rotation_state
;
5656 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
,
5658 session
->most_recent_chunk_id
.value
);
5660 if (session
->chunk_being_archived
) {
5661 enum lttng_trace_chunk_status chunk_status
;
5664 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
, &chunk_id
);
5665 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5667 rotation_state
= rotation_id
== chunk_id
? LTTNG_ROTATION_STATE_ONGOING
:
5668 LTTNG_ROTATION_STATE_EXPIRED
;
5670 if (session
->last_archived_chunk_id
.is_set
&&
5671 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5672 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5674 rotation_state
= session
->rotation_state
;
5678 switch (rotation_state
) {
5679 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5680 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5683 case LTTNG_ROTATION_STATE_EXPIRED
:
5684 DBG("Reporting that the rotation state of rotation id %" PRIu64
5685 " of session \"%s\" has expired",
5689 case LTTNG_ROTATION_STATE_ONGOING
:
5690 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5694 case LTTNG_ROTATION_STATE_COMPLETED
:
5698 char *current_tracing_path_reply
;
5699 size_t current_tracing_path_reply_len
;
5701 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5705 switch (session_get_consumer_destination_type(session
)) {
5706 case CONSUMER_DST_LOCAL
:
5707 current_tracing_path_reply
= info_return
->location
.local
.absolute_path
;
5708 current_tracing_path_reply_len
=
5709 sizeof(info_return
->location
.local
.absolute_path
);
5710 info_return
->location_type
=
5711 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5712 fmt_ret
= asprintf(&chunk_path
,
5713 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5714 session_get_base_path(session
),
5715 session
->last_archived_chunk_name
);
5716 if (fmt_ret
== -1) {
5717 PERROR("Failed to format the path of the last archived trace chunk");
5718 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5719 cmd_ret
= LTTNG_ERR_UNK
;
5723 case CONSUMER_DST_NET
:
5725 uint16_t ctrl_port
, data_port
;
5727 current_tracing_path_reply
= info_return
->location
.relay
.relative_path
;
5728 current_tracing_path_reply_len
=
5729 sizeof(info_return
->location
.relay
.relative_path
);
5730 /* Currently the only supported relay protocol. */
5731 info_return
->location
.relay
.protocol
=
5732 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5734 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5735 session_get_net_consumer_hostname(session
),
5736 sizeof(info_return
->location
.relay
.host
));
5738 ERR("Failed to copy host name to rotate_get_info reply");
5739 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5740 cmd_ret
= LTTNG_ERR_SET_URL
;
5744 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5745 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5746 info_return
->location
.relay
.ports
.data
= data_port
;
5747 info_return
->location_type
=
5748 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5749 chunk_path
= strdup(session
->last_chunk_path
);
5751 ERR("Failed to allocate the path of the last archived trace chunk");
5752 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5753 cmd_ret
= LTTNG_ERR_UNK
;
5762 fmt_ret
= lttng_strncpy(
5763 current_tracing_path_reply
, chunk_path
, current_tracing_path_reply_len
);
5766 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5767 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5768 cmd_ret
= LTTNG_ERR_UNK
;
5774 case LTTNG_ROTATION_STATE_ERROR
:
5775 DBG("Reporting that an error occurred during rotation %" PRIu64
5776 " of session \"%s\"",
5786 info_return
->status
= (int32_t) rotation_state
;
5791 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5793 * Configure the automatic rotation parameters.
5794 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5795 * 'activate' to false means deactivate the rotation schedule and validate that
5796 * 'new_value' has the same value as the currently active value.
5798 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5800 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5802 enum lttng_rotation_schedule_type schedule_type
,
5806 uint64_t *parameter_value
;
5808 LTTNG_ASSERT(session
);
5810 DBG("Cmd rotate set schedule session %s", session
->name
);
5812 if (session
->live_timer
|| !session
->output_traces
) {
5813 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5814 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5818 switch (schedule_type
) {
5819 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5820 parameter_value
= &session
->rotate_size
;
5822 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5823 parameter_value
= &session
->rotate_timer_period
;
5824 if (new_value
>= UINT_MAX
) {
5825 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
5829 ret
= LTTNG_ERR_INVALID
;
5834 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5835 ret
= LTTNG_ERR_INVALID
;
5839 /* Improper use of the API. */
5840 if (new_value
== -1ULL) {
5841 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5842 ret
= LTTNG_ERR_INVALID
;
5847 * As indicated in struct ltt_session's comments, a value of == 0 means
5848 * this schedule rotation type is not in use.
5850 * Reject the command if we were asked to activate a schedule that was
5853 if (activate
&& *parameter_value
!= 0) {
5854 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5855 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5860 * Reject the command if we were asked to deactivate a schedule that was
5863 if (!activate
&& *parameter_value
== 0) {
5864 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5865 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5870 * Reject the command if we were asked to deactivate a schedule that
5873 if (!activate
&& *parameter_value
!= new_value
) {
5874 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5875 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5879 *parameter_value
= activate
? new_value
: 0;
5881 switch (schedule_type
) {
5882 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5883 if (activate
&& session
->active
) {
5885 * Only start the timer if the session is active,
5886 * otherwise it will be started when the session starts.
5888 ret
= timer_session_rotation_schedule_timer_start(session
, new_value
);
5890 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5891 ret
= LTTNG_ERR_UNK
;
5895 ret
= timer_session_rotation_schedule_timer_stop(session
);
5897 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5898 ret
= LTTNG_ERR_UNK
;
5903 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5906 the_rotation_thread_handle
->subscribe_session_consumed_size_rotation(
5907 *session
, new_value
);
5908 } catch (const std::exception
& e
) {
5909 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5911 ret
= LTTNG_ERR_UNK
;
5916 the_rotation_thread_handle
5917 ->unsubscribe_session_consumed_size_rotation(*session
);
5918 } catch (const std::exception
& e
) {
5919 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5921 ret
= LTTNG_ERR_UNK
;
5927 /* Would have been caught before. */
5939 /* Wait for a given path to be removed before continuing. */
5940 static enum lttng_error_code
wait_on_path(void *path_data
)
5942 const char *shm_path
= (const char *) path_data
;
5944 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5950 ret
= stat(shm_path
, &st
);
5952 if (errno
!= ENOENT
) {
5953 PERROR("stat() returned an error while checking for the existence of the shm path");
5955 DBG("shm path no longer exists, completing the destruction of session");
5959 if (!S_ISDIR(st
.st_mode
)) {
5960 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5965 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5971 * Returns a pointer to a handler to run on completion of a command.
5972 * Returns NULL if no handler has to be run for the last command executed.
5974 const struct cmd_completion_handler
*cmd_pop_completion_handler()
5976 struct cmd_completion_handler
*handler
= current_completion_handler
;
5978 current_completion_handler
= nullptr;
5983 * Init command subsystem.
5988 * Set network sequence index to 1 for streams to match a relayd
5989 * socket on the consumer side.
5991 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5992 relayd_net_seq_idx
= 1;
5993 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5995 DBG("Command subsystem initialized");