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 DBG("Enable event command for event \'%s\'", event
->name
);
2100 lttng::urcu::read_lock_guard read_lock
;
2102 switch (domain
->type
) {
2103 case LTTNG_DOMAIN_KERNEL
:
2105 struct ltt_kernel_channel
*kchan
;
2108 * If a non-default channel has been created in the
2109 * session, explicitely require that -c chan_name needs
2112 if (session
->kernel_session
->has_non_default_channel
&& channel_name
[0] == '\0') {
2113 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2117 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2118 if (kchan
== nullptr) {
2119 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
, LTTNG_BUFFER_GLOBAL
);
2120 if (attr
== nullptr) {
2121 ret
= LTTNG_ERR_FATAL
;
2124 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2125 ret
= LTTNG_ERR_INVALID
;
2129 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2130 if (ret
!= LTTNG_OK
) {
2133 channel_created
= 1;
2136 /* Get the newly created kernel channel pointer */
2137 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2138 if (kchan
== nullptr) {
2139 /* This sould not happen... */
2140 ret
= LTTNG_ERR_FATAL
;
2144 switch (event
->type
) {
2145 case LTTNG_EVENT_ALL
:
2147 char *filter_expression_a
= nullptr;
2148 struct lttng_bytecode
*filter_a
= nullptr;
2151 * We need to duplicate filter_expression and filter,
2152 * because ownership is passed to first enable
2155 if (filter_expression
) {
2156 filter_expression_a
= strdup(filter_expression
);
2157 if (!filter_expression_a
) {
2158 ret
= LTTNG_ERR_FATAL
;
2163 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2165 free(filter_expression_a
);
2166 ret
= LTTNG_ERR_FATAL
;
2169 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2171 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2172 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2173 /* We have passed ownership */
2174 filter_expression
= nullptr;
2176 if (ret
!= LTTNG_OK
) {
2177 if (channel_created
) {
2178 /* Let's not leak a useless channel. */
2179 kernel_destroy_channel(kchan
);
2181 free(filter_expression_a
);
2185 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2186 ret
= event_kernel_enable_event(
2187 kchan
, event
, filter_expression_a
, filter_a
);
2188 /* We have passed ownership */
2189 filter_expression_a
= nullptr;
2191 if (ret
!= LTTNG_OK
) {
2196 case LTTNG_EVENT_PROBE
:
2197 case LTTNG_EVENT_USERSPACE_PROBE
:
2198 case LTTNG_EVENT_FUNCTION
:
2199 case LTTNG_EVENT_FUNCTION_ENTRY
:
2200 case LTTNG_EVENT_TRACEPOINT
:
2201 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2202 /* We have passed ownership */
2203 filter_expression
= nullptr;
2205 if (ret
!= LTTNG_OK
) {
2206 if (channel_created
) {
2207 /* Let's not leak a useless channel. */
2208 kernel_destroy_channel(kchan
);
2213 case LTTNG_EVENT_SYSCALL
:
2214 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2215 /* We have passed ownership */
2216 filter_expression
= nullptr;
2218 if (ret
!= LTTNG_OK
) {
2223 ret
= LTTNG_ERR_UNK
;
2227 kernel_wait_quiescent();
2230 case LTTNG_DOMAIN_UST
:
2232 struct ltt_ust_channel
*uchan
;
2233 struct ltt_ust_session
*usess
= session
->ust_session
;
2235 LTTNG_ASSERT(usess
);
2238 * If a non-default channel has been created in the
2239 * session, explicitely require that -c chan_name needs
2242 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2243 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2247 /* Get channel from global UST domain */
2248 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
2249 if (uchan
== nullptr) {
2250 /* Create default channel */
2251 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, usess
->buffer_type
);
2252 if (attr
== nullptr) {
2253 ret
= LTTNG_ERR_FATAL
;
2256 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2257 ret
= LTTNG_ERR_INVALID
;
2261 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2262 if (ret
!= LTTNG_OK
) {
2266 /* Get the newly created channel reference back */
2267 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2269 LTTNG_ASSERT(uchan
);
2272 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2274 * Don't allow users to add UST events to channels which
2275 * are assigned to a userspace subdomain (JUL, Log4J,
2278 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2282 if (!internal_event
) {
2284 * Ensure the event name is not reserved for internal
2287 ret
= validate_ust_event_name(event
->name
);
2289 WARN("Userspace event name %s failed validation.", event
->name
);
2290 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2295 /* At this point, the session and channel exist on the tracer */
2296 ret
= event_ust_enable_tracepoint(
2297 usess
, uchan
, event
, filter_expression
, filter
, exclusion
, internal_event
);
2298 /* We have passed ownership */
2299 filter_expression
= nullptr;
2301 exclusion
= nullptr;
2302 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2303 goto already_enabled
;
2304 } else if (ret
!= LTTNG_OK
) {
2309 case LTTNG_DOMAIN_LOG4J
:
2310 case LTTNG_DOMAIN_JUL
:
2311 case LTTNG_DOMAIN_PYTHON
:
2313 const char *default_event_name
, *default_chan_name
;
2315 struct lttng_event uevent
;
2316 struct lttng_domain tmp_dom
;
2317 struct ltt_ust_session
*usess
= session
->ust_session
;
2319 LTTNG_ASSERT(usess
);
2321 if (!agent_tracing_is_enabled()) {
2322 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2323 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2327 agt
= trace_ust_find_agent(usess
, domain
->type
);
2329 agt
= agent_create(domain
->type
);
2331 ret
= LTTNG_ERR_NOMEM
;
2334 agent_add(agt
, usess
->agents
);
2337 /* Create the default tracepoint. */
2338 memset(&uevent
, 0, sizeof(uevent
));
2339 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2340 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2341 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
2342 if (!default_event_name
) {
2343 ret
= LTTNG_ERR_FATAL
;
2346 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2347 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2350 * The domain type is changed because we are about to enable the
2351 * default channel and event for the JUL domain that are hardcoded.
2352 * This happens in the UST domain.
2354 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2355 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2357 switch (domain
->type
) {
2358 case LTTNG_DOMAIN_LOG4J
:
2359 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2361 case LTTNG_DOMAIN_JUL
:
2362 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2364 case LTTNG_DOMAIN_PYTHON
:
2365 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2368 /* The switch/case we are in makes this impossible */
2373 char *filter_expression_copy
= nullptr;
2374 struct lttng_bytecode
*filter_copy
= nullptr;
2377 const size_t filter_size
=
2378 sizeof(struct lttng_bytecode
) + filter
->len
;
2380 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2382 ret
= LTTNG_ERR_NOMEM
;
2385 memcpy(filter_copy
, filter
, filter_size
);
2387 filter_expression_copy
= strdup(filter_expression
);
2388 if (!filter_expression
) {
2389 ret
= LTTNG_ERR_NOMEM
;
2392 if (!filter_expression_copy
|| !filter_copy
) {
2393 free(filter_expression_copy
);
2399 ret
= cmd_enable_event_internal(session
,
2401 (char *) default_chan_name
,
2403 filter_expression_copy
,
2409 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2410 goto already_enabled
;
2411 } else if (ret
!= LTTNG_OK
) {
2415 /* The wild card * means that everything should be enabled. */
2416 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2417 ret
= event_agent_enable_all(usess
, agt
, event
, filter
, filter_expression
);
2419 ret
= event_agent_enable(usess
, agt
, event
, filter
, filter_expression
);
2422 filter_expression
= nullptr;
2423 if (ret
!= LTTNG_OK
) {
2430 ret
= LTTNG_ERR_UND
;
2438 free(filter_expression
);
2441 channel_attr_destroy(attr
);
2446 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2447 * We own filter, exclusion, and filter_expression.
2449 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2450 struct lttng_event
*event
,
2451 char *filter_expression
,
2452 struct lttng_event_exclusion
*exclusion
,
2453 struct lttng_bytecode
*bytecode
,
2458 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2460 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2463 * The ownership of the following parameters is transferred to
2464 * _cmd_enable_event:
2466 * - filter_expression,
2470 ret
= _cmd_enable_event(cmd_ctx
->session
,
2472 cmd_ctx
->lsm
.u
.enable
.channel_name
,
2479 filter_expression
= nullptr;
2481 exclusion
= nullptr;
2486 * Enable an event which is internal to LTTng. An internal should
2487 * never be made visible to clients and are immune to checks such as
2490 static int cmd_enable_event_internal(struct ltt_session
*session
,
2491 const struct lttng_domain
*domain
,
2493 struct lttng_event
*event
,
2494 char *filter_expression
,
2495 struct lttng_bytecode
*filter
,
2496 struct lttng_event_exclusion
*exclusion
,
2499 return _cmd_enable_event(session
,
2511 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2513 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2514 struct lttng_payload
*reply_payload
)
2516 enum lttng_error_code ret_code
;
2518 ssize_t i
, nb_events
= 0;
2519 struct lttng_event
*events
= nullptr;
2520 struct lttcomm_list_command_header reply_command_header
= {};
2521 size_t reply_command_header_offset
;
2523 assert(reply_payload
);
2525 /* Reserve space for command reply header. */
2526 reply_command_header_offset
= reply_payload
->buffer
.size
;
2527 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2528 reply_command_header_offset
+
2529 sizeof(struct lttcomm_list_command_header
));
2531 ret_code
= LTTNG_ERR_NOMEM
;
2536 case LTTNG_DOMAIN_KERNEL
:
2537 nb_events
= kernel_list_events(&events
);
2538 if (nb_events
< 0) {
2539 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2543 case LTTNG_DOMAIN_UST
:
2544 nb_events
= ust_app_list_events(&events
);
2545 if (nb_events
< 0) {
2546 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2550 case LTTNG_DOMAIN_LOG4J
:
2551 case LTTNG_DOMAIN_JUL
:
2552 case LTTNG_DOMAIN_PYTHON
:
2553 nb_events
= agent_list_events(&events
, domain
);
2554 if (nb_events
< 0) {
2555 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2560 ret_code
= LTTNG_ERR_UND
;
2564 for (i
= 0; i
< nb_events
; i
++) {
2565 ret
= lttng_event_serialize(
2566 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2568 ret_code
= LTTNG_ERR_NOMEM
;
2573 if (nb_events
> UINT32_MAX
) {
2574 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2575 ret_code
= LTTNG_ERR_OVERFLOW
;
2579 /* Update command reply header. */
2580 reply_command_header
.count
= (uint32_t) nb_events
;
2581 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2582 &reply_command_header
,
2583 sizeof(reply_command_header
));
2585 ret_code
= LTTNG_OK
;
2592 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2594 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2595 struct lttng_payload
*reply
)
2597 enum lttng_error_code ret_code
;
2599 unsigned int i
, nb_fields
;
2600 struct lttng_event_field
*fields
= nullptr;
2601 struct lttcomm_list_command_header reply_command_header
= {};
2602 size_t reply_command_header_offset
;
2606 /* Reserve space for command reply header. */
2607 reply_command_header_offset
= reply
->buffer
.size
;
2608 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2609 reply_command_header_offset
+
2610 sizeof(struct lttcomm_list_command_header
));
2612 ret_code
= LTTNG_ERR_NOMEM
;
2617 case LTTNG_DOMAIN_UST
:
2618 ret
= ust_app_list_event_fields(&fields
);
2620 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2625 case LTTNG_DOMAIN_KERNEL
:
2626 default: /* fall-through */
2627 ret_code
= LTTNG_ERR_UND
;
2633 for (i
= 0; i
< nb_fields
; i
++) {
2634 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2636 ret_code
= LTTNG_ERR_NOMEM
;
2641 if (nb_fields
> UINT32_MAX
) {
2642 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2643 ret_code
= LTTNG_ERR_OVERFLOW
;
2647 /* Update command reply header. */
2648 reply_command_header
.count
= (uint32_t) nb_fields
;
2650 memcpy(reply
->buffer
.data
+ reply_command_header_offset
,
2651 &reply_command_header
,
2652 sizeof(reply_command_header
));
2654 ret_code
= LTTNG_OK
;
2661 enum lttng_error_code
cmd_list_syscalls(struct lttng_payload
*reply_payload
)
2663 enum lttng_error_code ret_code
;
2664 ssize_t nb_events
, i
;
2666 struct lttng_event
*events
= nullptr;
2667 struct lttcomm_list_command_header reply_command_header
= {};
2668 size_t reply_command_header_offset
;
2670 assert(reply_payload
);
2672 /* Reserve space for command reply header. */
2673 reply_command_header_offset
= reply_payload
->buffer
.size
;
2674 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2675 reply_command_header_offset
+
2676 sizeof(struct lttcomm_list_command_header
));
2678 ret_code
= LTTNG_ERR_NOMEM
;
2682 nb_events
= syscall_table_list(&events
);
2683 if (nb_events
< 0) {
2684 ret_code
= (enum lttng_error_code
) - nb_events
;
2688 for (i
= 0; i
< nb_events
; i
++) {
2689 ret
= lttng_event_serialize(
2690 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2692 ret_code
= LTTNG_ERR_NOMEM
;
2697 if (nb_events
> UINT32_MAX
) {
2698 ERR("Syscall count would overflow the syscall listing command's reply");
2699 ret_code
= LTTNG_ERR_OVERFLOW
;
2703 /* Update command reply header. */
2704 reply_command_header
.count
= (uint32_t) nb_events
;
2705 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2706 &reply_command_header
,
2707 sizeof(reply_command_header
));
2709 ret_code
= LTTNG_OK
;
2716 * Command LTTNG_START_TRACE processed by the client thread.
2718 * Called with session mutex held.
2720 int cmd_start_trace(struct ltt_session
*session
)
2722 enum lttng_error_code ret
;
2723 unsigned long nb_chan
= 0;
2724 struct ltt_kernel_session
*ksession
;
2725 struct ltt_ust_session
*usess
;
2726 const bool session_rotated_after_last_stop
= session
->rotated_after_last_stop
;
2727 const bool session_cleared_after_last_stop
= session
->cleared_after_last_stop
;
2729 LTTNG_ASSERT(session
);
2731 /* Ease our life a bit ;) */
2732 ksession
= session
->kernel_session
;
2733 usess
= session
->ust_session
;
2735 /* Is the session already started? */
2736 if (session
->active
) {
2737 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2738 /* Perform nothing */
2742 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2743 !session
->current_trace_chunk
) {
2745 * A rotation was launched while the session was stopped and
2746 * it has not been completed yet. It is not possible to start
2747 * the session since starting the session here would require a
2748 * rotation from "NULL" to a new trace chunk. That rotation
2749 * would overlap with the ongoing rotation, which is not
2752 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2754 ret
= LTTNG_ERR_ROTATION_PENDING
;
2759 * Starting a session without channel is useless since after that it's not
2760 * possible to enable channel thus inform the client.
2762 if (usess
&& usess
->domain_global
.channels
) {
2763 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2766 nb_chan
+= ksession
->channel_count
;
2769 ret
= LTTNG_ERR_NO_CHANNEL
;
2773 session
->active
= true;
2774 session
->rotated_after_last_stop
= false;
2775 session
->cleared_after_last_stop
= false;
2776 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2777 if (!session
->has_been_started
) {
2778 struct lttng_trace_chunk
*trace_chunk
;
2780 DBG("Creating initial trace chunk of session \"%s\"", session
->name
);
2782 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
2784 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2787 LTTNG_ASSERT(!session
->current_trace_chunk
);
2788 ret
= (lttng_error_code
) session_set_trace_chunk(
2789 session
, trace_chunk
, nullptr);
2790 lttng_trace_chunk_put(trace_chunk
);
2792 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2796 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2799 * Rotate existing streams into the new chunk.
2800 * This is a "quiet" rotation has no client has
2801 * explicitly requested this operation.
2803 * There is also no need to wait for the rotation
2804 * to complete as it will happen immediately. No data
2805 * was produced as the session was stopped, so the
2806 * rotation should happen on reception of the command.
2808 ret
= (lttng_error_code
) cmd_rotate_session(
2809 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2810 if (ret
!= LTTNG_OK
) {
2816 /* Kernel tracing */
2817 if (ksession
!= nullptr) {
2818 DBG("Start kernel tracing session %s", session
->name
);
2819 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2820 if (ret
!= LTTNG_OK
) {
2825 /* Flag session that trace should start automatically */
2827 int int_ret
= ust_app_start_trace_all(usess
);
2830 ret
= LTTNG_ERR_UST_START_FAIL
;
2836 * Open a packet in every stream of the session to ensure that viewers
2837 * can correctly identify the boundaries of the periods during which
2838 * tracing was active for this session.
2840 ret
= session_open_packets(session
);
2841 if (ret
!= LTTNG_OK
) {
2846 * Clear the flag that indicates that a rotation was done while the
2847 * session was stopped.
2849 session
->rotated_after_last_stop
= false;
2851 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2852 int int_ret
= timer_session_rotation_schedule_timer_start(
2853 session
, session
->rotate_timer_period
);
2856 ERR("Failed to enable rotate timer");
2857 ret
= LTTNG_ERR_UNK
;
2865 if (ret
== LTTNG_OK
) {
2866 /* Flag this after a successful start. */
2867 session
->has_been_started
= true;
2869 session
->active
= false;
2870 /* Restore initial state on error. */
2871 session
->rotated_after_last_stop
= session_rotated_after_last_stop
;
2872 session
->cleared_after_last_stop
= session_cleared_after_last_stop
;
2879 * Command LTTNG_STOP_TRACE processed by the client thread.
2881 int cmd_stop_trace(struct ltt_session
*session
)
2884 struct ltt_kernel_session
*ksession
;
2885 struct ltt_ust_session
*usess
;
2887 LTTNG_ASSERT(session
);
2889 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2891 ksession
= session
->kernel_session
;
2892 usess
= session
->ust_session
;
2894 /* Session is not active. Skip everything and inform the client. */
2895 if (!session
->active
) {
2896 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2900 ret
= stop_kernel_session(ksession
);
2901 if (ret
!= LTTNG_OK
) {
2905 if (usess
&& usess
->active
) {
2906 ret
= ust_app_stop_trace_all(usess
);
2908 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2913 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2914 /* Flag inactive after a successful stop. */
2915 session
->active
= false;
2923 * Set the base_path of the session only if subdir of a control uris is set.
2924 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2927 set_session_base_path_from_uris(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2932 for (i
= 0; i
< nb_uri
; i
++) {
2933 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
|| uris
[i
].subdir
[0] == '\0') {
2934 /* Not interested in these URIs */
2938 if (session
->base_path
!= nullptr) {
2939 free(session
->base_path
);
2940 session
->base_path
= nullptr;
2943 /* Set session base_path */
2944 session
->base_path
= strdup(uris
[i
].subdir
);
2945 if (!session
->base_path
) {
2946 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2949 ret
= LTTNG_ERR_NOMEM
;
2952 DBG2("Setting base path \"%s\" for session \"%s\"",
2962 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2964 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2967 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2968 struct ltt_ust_session
*usess
= session
->ust_session
;
2970 LTTNG_ASSERT(session
);
2972 LTTNG_ASSERT(nb_uri
> 0);
2974 /* Can't set consumer URI if the session is active. */
2975 if (session
->active
) {
2976 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2981 * Set the session base path if any. This is done inside
2982 * cmd_set_consumer_uri to preserve backward compatibility of the
2983 * previous session creation api vs the session descriptor api.
2985 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2986 if (ret
!= LTTNG_OK
) {
2990 /* Set the "global" consumer URIs */
2991 for (i
= 0; i
< nb_uri
; i
++) {
2992 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
], LTTNG_DOMAIN_NONE
);
2993 if (ret
!= LTTNG_OK
) {
2998 /* Set UST session URIs */
2999 if (session
->ust_session
) {
3000 for (i
= 0; i
< nb_uri
; i
++) {
3001 ret
= add_uri_to_consumer(session
,
3002 session
->ust_session
->consumer
,
3005 if (ret
!= LTTNG_OK
) {
3011 /* Set kernel session URIs */
3012 if (session
->kernel_session
) {
3013 for (i
= 0; i
< nb_uri
; i
++) {
3014 ret
= add_uri_to_consumer(session
,
3015 session
->kernel_session
->consumer
,
3017 LTTNG_DOMAIN_KERNEL
);
3018 if (ret
!= LTTNG_OK
) {
3025 * Make sure to set the session in output mode after we set URI since a
3026 * session can be created without URL (thus flagged in no output mode).
3028 session
->output_traces
= 1;
3030 ksess
->output_traces
= 1;
3034 usess
->output_traces
= 1;
3044 static enum lttng_error_code
3045 set_session_output_from_descriptor(struct ltt_session
*session
,
3046 const struct lttng_session_descriptor
*descriptor
)
3049 enum lttng_error_code ret_code
= LTTNG_OK
;
3050 enum lttng_session_descriptor_type session_type
=
3051 lttng_session_descriptor_get_type(descriptor
);
3052 enum lttng_session_descriptor_output_type output_type
=
3053 lttng_session_descriptor_get_output_type(descriptor
);
3054 struct lttng_uri uris
[2] = {};
3055 size_t uri_count
= 0;
3057 switch (output_type
) {
3058 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3060 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3061 lttng_session_descriptor_get_local_output_uri(descriptor
, &uris
[0]);
3064 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3065 lttng_session_descriptor_get_network_output_uris(descriptor
, &uris
[0], &uris
[1]);
3069 ret_code
= LTTNG_ERR_INVALID
;
3073 switch (session_type
) {
3074 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3076 struct snapshot_output
*new_output
= nullptr;
3078 new_output
= snapshot_output_alloc();
3080 ret_code
= LTTNG_ERR_NOMEM
;
3084 ret
= snapshot_output_init_with_uri(session
,
3085 DEFAULT_SNAPSHOT_MAX_SIZE
,
3091 &session
->snapshot
);
3093 ret_code
= (ret
== -ENOMEM
) ? LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3094 snapshot_output_destroy(new_output
);
3097 snapshot_add_output(&session
->snapshot
, new_output
);
3100 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3101 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3103 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3107 ret_code
= LTTNG_ERR_INVALID
;
3114 static enum lttng_error_code
3115 cmd_create_session_from_descriptor(struct lttng_session_descriptor
*descriptor
,
3116 const lttng_sock_cred
*creds
,
3117 const char *home_path
)
3120 enum lttng_error_code ret_code
;
3121 const char *session_name
;
3122 struct ltt_session
*new_session
= nullptr;
3123 enum lttng_session_descriptor_status descriptor_status
;
3125 session_lock_list();
3127 if (*home_path
!= '/') {
3128 ERR("Home path provided by client is not absolute");
3129 ret_code
= LTTNG_ERR_INVALID
;
3134 descriptor_status
= lttng_session_descriptor_get_session_name(descriptor
, &session_name
);
3135 switch (descriptor_status
) {
3136 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3138 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3139 session_name
= nullptr;
3142 ret_code
= LTTNG_ERR_INVALID
;
3146 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, &new_session
);
3147 if (ret_code
!= LTTNG_OK
) {
3151 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3156 if (ret_code
!= LTTNG_OK
) {
3160 /* Announce the session's destruction to the notification thread when it is destroyed. */
3161 ret
= session_add_destroy_notifier(
3163 [](const struct ltt_session
*session
, void *user_data
__attribute__((unused
))) {
3164 (void) notification_thread_command_remove_session(
3165 the_notification_thread_handle
, session
->id
);
3169 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3171 ret
= LTTNG_ERR_NOMEM
;
3175 if (!session_name
) {
3176 ret
= lttng_session_descriptor_set_session_name(descriptor
, new_session
->name
);
3178 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3183 if (!lttng_session_descriptor_is_output_destination_initialized(descriptor
)) {
3185 * Only include the session's creation time in the output
3186 * destination if the name of the session itself was
3187 * not auto-generated.
3189 ret_code
= lttng_session_descriptor_set_default_output(
3191 session_name
? &new_session
->creation_time
: nullptr,
3193 if (ret_code
!= LTTNG_OK
) {
3197 new_session
->has_user_specified_directory
=
3198 lttng_session_descriptor_has_output_directory(descriptor
);
3201 switch (lttng_session_descriptor_get_type(descriptor
)) {
3202 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3203 new_session
->snapshot_mode
= 1;
3205 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3206 new_session
->live_timer
=
3207 lttng_session_descriptor_live_get_timer_interval(descriptor
);
3213 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3214 if (ret_code
!= LTTNG_OK
) {
3217 new_session
->consumer
->enabled
= true;
3218 ret_code
= LTTNG_OK
;
3220 /* Release reference provided by the session_create function. */
3221 session_put(new_session
);
3222 if (ret_code
!= LTTNG_OK
&& new_session
) {
3223 /* Release the global reference on error. */
3224 session_destroy(new_session
);
3226 session_unlock_list();
3230 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
,
3232 struct lttng_session_descriptor
**return_descriptor
)
3235 size_t payload_size
;
3236 struct lttng_dynamic_buffer payload
;
3237 struct lttng_buffer_view home_dir_view
;
3238 struct lttng_buffer_view session_descriptor_view
;
3239 struct lttng_session_descriptor
*session_descriptor
= nullptr;
3240 enum lttng_error_code ret_code
;
3242 lttng_dynamic_buffer_init(&payload
);
3243 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>= LTTNG_PATH_MAX
) {
3244 ret_code
= LTTNG_ERR_INVALID
;
3247 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3248 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3249 ret_code
= LTTNG_ERR_INVALID
;
3253 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3254 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3255 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3257 ret_code
= LTTNG_ERR_NOMEM
;
3261 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3263 ERR("Reception of session descriptor failed, aborting.");
3264 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3268 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3269 &payload
, 0, cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3270 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3271 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3272 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3273 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3277 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3279 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3280 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3281 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3282 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3283 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3287 ret
= lttng_session_descriptor_create_from_buffer(&session_descriptor_view
,
3288 &session_descriptor
);
3290 ERR("Failed to create session descriptor from payload of \"create session\" command");
3291 ret_code
= LTTNG_ERR_INVALID
;
3296 * Sets the descriptor's auto-generated properties (name, output) if
3299 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3301 home_dir_view
.size
? home_dir_view
.data
:
3303 if (ret_code
!= LTTNG_OK
) {
3307 ret_code
= LTTNG_OK
;
3308 *return_descriptor
= session_descriptor
;
3309 session_descriptor
= nullptr;
3311 lttng_dynamic_buffer_reset(&payload
);
3312 lttng_session_descriptor_destroy(session_descriptor
);
3316 static void cmd_destroy_session_reply(const struct ltt_session
*session
, void *_reply_context
)
3320 const struct cmd_destroy_session_reply_context
*reply_context
=
3321 (cmd_destroy_session_reply_context
*) _reply_context
;
3322 struct lttng_dynamic_buffer payload
;
3323 struct lttcomm_session_destroy_command_header cmd_header
;
3324 struct lttng_trace_archive_location
*location
= nullptr;
3325 struct lttcomm_lttng_msg llm
= {
3326 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION
,
3327 .ret_code
= reply_context
->destruction_status
,
3329 .cmd_header_size
= sizeof(struct lttcomm_session_destroy_command_header
),
3333 size_t payload_size_before_location
;
3335 lttng_dynamic_buffer_init(&payload
);
3337 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3339 ERR("Failed to append session destruction message");
3343 cmd_header
.rotation_state
= (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3344 session
->rotation_state
:
3345 LTTNG_ROTATION_STATE_NO_ROTATION
);
3346 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
, sizeof(cmd_header
));
3348 ERR("Failed to append session destruction command header");
3352 if (!reply_context
->implicit_rotation_on_destroy
) {
3353 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3357 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3358 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3363 location
= session_get_trace_archive_location(session
);
3365 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3370 payload_size_before_location
= payload
.size
;
3371 comm_ret
= lttng_trace_archive_location_serialize(location
, &payload
);
3372 lttng_trace_archive_location_put(location
);
3374 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3378 /* Update the message to indicate the location's length. */
3379 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3380 payload
.size
- payload_size_before_location
;
3382 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
, payload
.data
, payload
.size
);
3383 if (comm_ret
!= (ssize_t
) payload
.size
) {
3384 ERR("Failed to send result of the destruction of session \"%s\" to client",
3388 ret
= close(reply_context
->reply_sock_fd
);
3390 PERROR("Failed to close client socket in deferred session destroy reply");
3392 lttng_dynamic_buffer_reset(&payload
);
3393 free(_reply_context
);
3397 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3399 * Called with session lock held.
3401 int cmd_destroy_session(struct ltt_session
*session
, int *sock_fd
)
3404 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3405 struct cmd_destroy_session_reply_context
*reply_context
= nullptr;
3408 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3409 if (!reply_context
) {
3410 ret
= LTTNG_ERR_NOMEM
;
3414 reply_context
->reply_sock_fd
= *sock_fd
;
3418 LTTNG_ASSERT(session
);
3420 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3421 if (session
->active
) {
3422 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3424 ret
= cmd_stop_trace(session
);
3425 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3426 /* Carry on with the destruction of the session. */
3427 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3429 lttng_strerror(-ret
));
3430 destruction_last_error
= (lttng_error_code
) ret
;
3434 if (session
->rotation_schedule_timer_enabled
) {
3435 if (timer_session_rotation_schedule_timer_stop(session
)) {
3436 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3438 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3442 if (session
->rotate_size
) {
3444 the_rotation_thread_handle
->unsubscribe_session_consumed_size_rotation(
3446 } catch (const std::exception
& e
) {
3447 /* Continue the destruction of the session anyway. */
3448 ERR("Failed to unsubscribe rotation thread notification channel from consumed size condition during session destruction: %s",
3452 session
->rotate_size
= 0;
3455 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3457 * Perform a last rotation on destruction if rotations have
3458 * occurred during the session's lifetime.
3460 ret
= cmd_rotate_session(
3461 session
, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3462 if (ret
!= LTTNG_OK
) {
3463 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3465 lttng_strerror(-ret
));
3466 destruction_last_error
= (lttng_error_code
) -ret
;
3468 if (reply_context
) {
3469 reply_context
->implicit_rotation_on_destroy
= true;
3471 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3473 * The user has not triggered a session rotation. However, to
3474 * ensure all data has been consumed, the session is rotated
3475 * to a 'null' trace chunk before it is destroyed.
3477 * This is a "quiet" rotation meaning that no notification is
3478 * emitted and no renaming of the current trace chunk takes
3481 ret
= cmd_rotate_session(
3482 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3484 * Rotation operations may not be supported by the kernel
3485 * tracer. Hence, do not consider this implicit rotation as
3486 * a session destruction error. The library has already stopped
3487 * the session and waited for pending data; there is nothing
3488 * left to do but complete the destruction of the session.
3490 if (ret
!= LTTNG_OK
&& ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3491 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3493 lttng_strerror(ret
));
3494 destruction_last_error
= (lttng_error_code
) -ret
;
3498 if (session
->shm_path
[0]) {
3500 * When a session is created with an explicit shm_path,
3501 * the consumer daemon will create its shared memory files
3502 * at that location and will *not* unlink them. This is normal
3503 * as the intention of that feature is to make it possible
3504 * to retrieve the content of those files should a crash occur.
3506 * To ensure the content of those files can be used, the
3507 * sessiond daemon will replicate the content of the metadata
3508 * cache in a metadata file.
3510 * On clean-up, it is expected that the consumer daemon will
3511 * unlink the shared memory files and that the session daemon
3512 * will unlink the metadata file. Then, the session's directory
3513 * in the shm path can be removed.
3515 * Unfortunately, a flaw in the design of the sessiond's and
3516 * consumerd's tear down of channels makes it impossible to
3517 * determine when the sessiond _and_ the consumerd have both
3518 * destroyed their representation of a channel. For one, the
3519 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3520 * callbacks in both daemons.
3522 * However, it is also impossible for the sessiond to know when
3523 * the consumer daemon is done destroying its channel(s) since
3524 * it occurs as a reaction to the closing of the channel's file
3525 * descriptor. There is no resulting communication initiated
3526 * from the consumerd to the sessiond to confirm that the
3527 * operation is completed (and was successful).
3529 * Until this is all fixed, the session daemon checks for the
3530 * removal of the session's shm path which makes it possible
3531 * to safely advertise a session as having been destroyed.
3533 * Prior to this fix, it was not possible to reliably save
3534 * a session making use of the --shm-path option, destroy it,
3535 * and load it again. This is because the creation of the
3536 * session would fail upon seeing the session's shm path
3537 * already in existence.
3539 * Note that none of the error paths in the check for the
3540 * directory's existence return an error. This is normal
3541 * as there isn't much that can be done. The session will
3542 * be destroyed properly, except that we can't offer the
3543 * guarantee that the same session can be re-created.
3545 current_completion_handler
= &destroy_completion_handler
.handler
;
3546 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3548 sizeof(destroy_completion_handler
.shm_path
));
3553 * The session is destroyed. However, note that the command context
3554 * still holds a reference to the session, thus delaying its destruction
3555 * _at least_ up to the point when that reference is released.
3557 session_destroy(session
);
3558 if (reply_context
) {
3559 reply_context
->destruction_status
= destruction_last_error
;
3560 ret
= session_add_destroy_notifier(
3561 session
, cmd_destroy_session_reply
, (void *) reply_context
);
3563 ret
= LTTNG_ERR_FATAL
;
3575 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3577 int cmd_register_consumer(struct ltt_session
*session
,
3578 enum lttng_domain_type domain
,
3579 const char *sock_path
,
3580 struct consumer_data
*cdata
)
3583 struct consumer_socket
*socket
= nullptr;
3585 LTTNG_ASSERT(session
);
3586 LTTNG_ASSERT(cdata
);
3587 LTTNG_ASSERT(sock_path
);
3590 case LTTNG_DOMAIN_KERNEL
:
3592 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3594 LTTNG_ASSERT(ksess
);
3596 /* Can't register a consumer if there is already one */
3597 if (ksess
->consumer_fds_sent
!= 0) {
3598 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3602 sock
= lttcomm_connect_unix_sock(sock_path
);
3604 ret
= LTTNG_ERR_CONNECT_FAIL
;
3607 cdata
->cmd_sock
= sock
;
3609 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3610 if (socket
== nullptr) {
3613 PERROR("close register consumer");
3615 cdata
->cmd_sock
= -1;
3616 ret
= LTTNG_ERR_FATAL
;
3620 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3621 if (socket
->lock
== nullptr) {
3622 PERROR("zmalloc pthread mutex");
3623 ret
= LTTNG_ERR_FATAL
;
3627 pthread_mutex_init(socket
->lock
, nullptr);
3628 socket
->registered
= 1;
3630 lttng::urcu::read_lock_guard read_lock
;
3631 consumer_add_socket(socket
, ksess
->consumer
);
3633 pthread_mutex_lock(&cdata
->pid_mutex
);
3635 pthread_mutex_unlock(&cdata
->pid_mutex
);
3640 /* TODO: Userspace tracing */
3641 ret
= LTTNG_ERR_UND
;
3649 consumer_destroy_socket(socket
);
3655 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3657 ssize_t
cmd_list_domains(struct ltt_session
*session
, struct lttng_domain
**domains
)
3662 struct lttng_ht_iter iter
;
3664 if (session
->kernel_session
!= nullptr) {
3665 DBG3("Listing domains found kernel domain");
3669 if (session
->ust_session
!= nullptr) {
3670 DBG3("Listing domains found UST global domain");
3673 lttng::urcu::read_lock_guard read_lock
;
3675 cds_lfht_for_each_entry (
3676 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3677 if (agt
->being_used
) {
3687 *domains
= calloc
<lttng_domain
>(nb_dom
);
3688 if (*domains
== nullptr) {
3689 ret
= LTTNG_ERR_FATAL
;
3693 if (session
->kernel_session
!= nullptr) {
3694 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3696 /* Kernel session buffer type is always GLOBAL */
3697 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3702 if (session
->ust_session
!= nullptr) {
3703 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3704 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3708 lttng::urcu::read_lock_guard read_lock
;
3710 cds_lfht_for_each_entry (
3711 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3712 if (agt
->being_used
) {
3713 (*domains
)[index
].type
= agt
->domain
;
3714 (*domains
)[index
].buf_type
=
3715 session
->ust_session
->buffer_type
;
3725 /* Return negative value to differentiate return code */
3730 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3732 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3733 struct ltt_session
*session
,
3734 struct lttng_payload
*payload
)
3738 struct lttcomm_list_command_header cmd_header
= {};
3739 size_t cmd_header_offset
;
3740 enum lttng_error_code ret_code
;
3745 DBG("Listing channels for session %s", session
->name
);
3747 cmd_header_offset
= payload
->buffer
.size
;
3749 /* Reserve space for command reply header. */
3750 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3751 cmd_header_offset
+ sizeof(cmd_header
));
3753 ret_code
= LTTNG_ERR_NOMEM
;
3758 case LTTNG_DOMAIN_KERNEL
:
3760 /* Kernel channels */
3761 struct ltt_kernel_channel
*kchan
;
3762 if (session
->kernel_session
!= nullptr) {
3763 cds_list_for_each_entry (
3764 kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
3765 uint64_t discarded_events
, lost_packets
;
3766 struct lttng_channel_extended
*extended
;
3768 extended
= (struct lttng_channel_extended
*)
3769 kchan
->channel
->attr
.extended
.ptr
;
3771 ret
= get_kernel_runtime_stats(
3772 session
, kchan
, &discarded_events
, &lost_packets
);
3774 ret_code
= LTTNG_ERR_UNK
;
3779 * Update the discarded_events and lost_packets
3780 * count for the channel
3782 extended
->discarded_events
= discarded_events
;
3783 extended
->lost_packets
= lost_packets
;
3785 ret
= lttng_channel_serialize(kchan
->channel
, &payload
->buffer
);
3787 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3788 kchan
->channel
->name
);
3789 ret_code
= LTTNG_ERR_UNK
;
3798 case LTTNG_DOMAIN_UST
:
3800 struct lttng_ht_iter iter
;
3801 struct ltt_ust_channel
*uchan
;
3804 lttng::urcu::read_lock_guard read_lock
;
3806 cds_lfht_for_each_entry (session
->ust_session
->domain_global
.channels
->ht
,
3810 uint64_t discarded_events
= 0, lost_packets
= 0;
3811 struct lttng_channel
*channel
= nullptr;
3812 struct lttng_channel_extended
*extended
;
3814 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3816 ret_code
= LTTNG_ERR_NOMEM
;
3820 extended
= (struct lttng_channel_extended
*)
3821 channel
->attr
.extended
.ptr
;
3823 ret
= get_ust_runtime_stats(
3824 session
, uchan
, &discarded_events
, &lost_packets
);
3826 lttng_channel_destroy(channel
);
3827 ret_code
= LTTNG_ERR_UNK
;
3831 extended
->discarded_events
= discarded_events
;
3832 extended
->lost_packets
= lost_packets
;
3834 ret
= lttng_channel_serialize(channel
, &payload
->buffer
);
3836 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3838 lttng_channel_destroy(channel
);
3839 ret_code
= LTTNG_ERR_UNK
;
3843 lttng_channel_destroy(channel
);
3854 if (i
> UINT32_MAX
) {
3855 ERR("Channel count would overflow the channel listing command's reply");
3856 ret_code
= LTTNG_ERR_OVERFLOW
;
3860 /* Update command reply header. */
3861 cmd_header
.count
= (uint32_t) i
;
3862 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
, sizeof(cmd_header
));
3863 ret_code
= LTTNG_OK
;
3870 * Command LTTNG_LIST_EVENTS processed by the client thread.
3872 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3873 struct ltt_session
*session
,
3875 struct lttng_payload
*reply_payload
)
3877 int buffer_resize_ret
;
3878 enum lttng_error_code ret_code
= LTTNG_OK
;
3879 struct lttcomm_list_command_header reply_command_header
= {};
3880 size_t reply_command_header_offset
;
3881 unsigned int nb_events
= 0;
3883 assert(reply_payload
);
3885 /* Reserve space for command reply header. */
3886 reply_command_header_offset
= reply_payload
->buffer
.size
;
3887 buffer_resize_ret
= lttng_dynamic_buffer_set_size(
3888 &reply_payload
->buffer
,
3889 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
3890 if (buffer_resize_ret
) {
3891 ret_code
= LTTNG_ERR_NOMEM
;
3896 case LTTNG_DOMAIN_KERNEL
:
3897 if (session
->kernel_session
!= nullptr) {
3898 ret_code
= list_lttng_kernel_events(
3899 channel_name
, session
->kernel_session
, reply_payload
, &nb_events
);
3903 case LTTNG_DOMAIN_UST
:
3905 if (session
->ust_session
!= nullptr) {
3907 list_lttng_ust_global_events(channel_name
,
3908 &session
->ust_session
->domain_global
,
3915 case LTTNG_DOMAIN_LOG4J
:
3916 case LTTNG_DOMAIN_JUL
:
3917 case LTTNG_DOMAIN_PYTHON
:
3918 if (session
->ust_session
) {
3919 struct lttng_ht_iter iter
;
3922 lttng::urcu::read_lock_guard read_lock
;
3924 cds_lfht_for_each_entry (
3925 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3926 if (agt
->domain
== domain
) {
3927 ret_code
= list_lttng_agent_events(
3928 agt
, reply_payload
, &nb_events
);
3935 ret_code
= LTTNG_ERR_UND
;
3939 if (nb_events
> UINT32_MAX
) {
3940 ret_code
= LTTNG_ERR_OVERFLOW
;
3944 /* Update command reply header. */
3945 reply_command_header
.count
= (uint32_t) nb_events
;
3946 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
3947 &reply_command_header
,
3948 sizeof(reply_command_header
));
3955 * Using the session list, filled a lttng_session array to send back to the
3956 * client for session listing.
3958 * The session list lock MUST be acquired before calling this function. Use
3959 * session_lock_list() and session_unlock_list().
3961 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3962 size_t session_count
,
3968 struct ltt_session
*session
;
3969 struct ltt_session_list
*list
= session_get_list();
3970 struct lttng_session_extended
*extended
= (typeof(extended
)) (&sessions
[session_count
]);
3972 DBG("Getting all available session for UID %d GID %d", uid
, gid
);
3974 * Iterate over session list and append data after the control struct in
3977 cds_list_for_each_entry (session
, &list
->head
, list
) {
3978 if (!session_get(session
)) {
3982 * Only list the sessions the user can control.
3984 if (!session_access_ok(session
, uid
) || session
->destroyed
) {
3985 session_put(session
);
3989 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3990 struct ltt_ust_session
*usess
= session
->ust_session
;
3992 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3993 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3994 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3995 ret
= build_network_session_path(
3996 sessions
[i
].path
, sizeof(sessions
[i
].path
), session
);
3998 ret
= snprintf(sessions
[i
].path
,
3999 sizeof(sessions
[i
].path
),
4001 session
->consumer
->dst
.session_root_path
);
4004 PERROR("snprintf session path");
4005 session_put(session
);
4009 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
4010 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
4011 sessions
[i
].enabled
= session
->active
;
4012 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
4013 sessions
[i
].live_timer_interval
= session
->live_timer
;
4014 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
4015 extended
[i
].creation_time
.is_set
= 1;
4017 session_put(session
);
4022 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4023 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4025 int cmd_data_pending(struct ltt_session
*session
)
4028 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4029 struct ltt_ust_session
*usess
= session
->ust_session
;
4031 LTTNG_ASSERT(session
);
4033 DBG("Data pending for session %s", session
->name
);
4035 /* Session MUST be stopped to ask for data availability. */
4036 if (session
->active
) {
4037 ret
= LTTNG_ERR_SESSION_STARTED
;
4041 * If stopped, just make sure we've started before else the above call
4042 * will always send that there is data pending.
4044 * The consumer assumes that when the data pending command is received,
4045 * the trace has been started before or else no output data is written
4046 * by the streams which is a condition for data pending. So, this is
4047 * *VERY* important that we don't ask the consumer before a start
4050 if (!session
->has_been_started
) {
4056 /* A rotation is still pending, we have to wait. */
4057 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4058 DBG("Rotate still pending for session %s", session
->name
);
4063 if (ksess
&& ksess
->consumer
) {
4064 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4066 /* Data is still being extracted for the kernel. */
4071 if (usess
&& usess
->consumer
) {
4072 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4074 /* Data is still being extracted for the kernel. */
4079 /* Data is ready to be read by a viewer */
4087 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4089 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4091 int cmd_snapshot_add_output(struct ltt_session
*session
,
4092 const struct lttng_snapshot_output
*output
,
4096 struct snapshot_output
*new_output
;
4098 LTTNG_ASSERT(session
);
4099 LTTNG_ASSERT(output
);
4101 DBG("Cmd snapshot add output for session %s", session
->name
);
4104 * Can't create an output if the session is not set in no-output mode.
4106 if (session
->output_traces
) {
4107 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4111 if (session
->has_non_mmap_channel
) {
4112 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4116 /* Only one output is allowed until we have the "tee" feature. */
4117 if (session
->snapshot
.nb_output
== 1) {
4118 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4122 new_output
= snapshot_output_alloc();
4124 ret
= LTTNG_ERR_NOMEM
;
4128 ret
= snapshot_output_init(session
,
4135 &session
->snapshot
);
4137 if (ret
== -ENOMEM
) {
4138 ret
= LTTNG_ERR_NOMEM
;
4140 ret
= LTTNG_ERR_INVALID
;
4145 snapshot_add_output(&session
->snapshot
, new_output
);
4147 *id
= new_output
->id
;
4153 snapshot_output_destroy(new_output
);
4159 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4161 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4163 int cmd_snapshot_del_output(struct ltt_session
*session
, const struct lttng_snapshot_output
*output
)
4166 struct snapshot_output
*sout
= nullptr;
4168 LTTNG_ASSERT(session
);
4169 LTTNG_ASSERT(output
);
4171 lttng::urcu::read_lock_guard read_lock
;
4174 * Permission denied to create an output if the session is not
4175 * set in no output mode.
4177 if (session
->output_traces
) {
4178 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4183 DBG("Cmd snapshot del output id %" PRIu32
" for session %s",
4186 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4187 } else if (*output
->name
!= '\0') {
4188 DBG("Cmd snapshot del output name %s for session %s", output
->name
, session
->name
);
4189 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4192 ret
= LTTNG_ERR_INVALID
;
4196 snapshot_delete_output(&session
->snapshot
, sout
);
4197 snapshot_output_destroy(sout
);
4205 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4207 * If no output is available, outputs is untouched and 0 is returned.
4209 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4211 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4212 struct lttng_snapshot_output
**outputs
)
4215 struct lttng_snapshot_output
*list
= nullptr;
4216 struct lttng_ht_iter iter
;
4217 struct snapshot_output
*output
;
4219 LTTNG_ASSERT(session
);
4220 LTTNG_ASSERT(outputs
);
4222 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4225 * Permission denied to create an output if the session is not
4226 * set in no output mode.
4228 if (session
->output_traces
) {
4229 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4233 if (session
->snapshot
.nb_output
== 0) {
4238 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4240 ret
= -LTTNG_ERR_NOMEM
;
4244 /* Copy list from session to the new list object. */
4246 lttng::urcu::read_lock_guard read_lock
;
4248 cds_lfht_for_each_entry (
4249 session
->snapshot
.output_ht
->ht
, &iter
.iter
, output
, node
.node
) {
4250 LTTNG_ASSERT(output
->consumer
);
4251 list
[idx
].id
= output
->id
;
4252 list
[idx
].max_size
= output
->max_size
;
4253 if (lttng_strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
))) {
4254 ret
= -LTTNG_ERR_INVALID
;
4258 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4259 if (lttng_strncpy(list
[idx
].ctrl_url
,
4260 output
->consumer
->dst
.session_root_path
,
4261 sizeof(list
[idx
].ctrl_url
))) {
4262 ret
= -LTTNG_ERR_INVALID
;
4267 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4269 sizeof(list
[idx
].ctrl_url
));
4271 ret
= -LTTNG_ERR_NOMEM
;
4276 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4278 sizeof(list
[idx
].data_url
));
4280 ret
= -LTTNG_ERR_NOMEM
;
4291 ret
= session
->snapshot
.nb_output
;
4299 * Check if we can regenerate the metadata for this session.
4300 * Only kernel, UST per-uid and non-live sessions are supported.
4302 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4304 static int check_regenerate_metadata_support(struct ltt_session
*session
)
4308 LTTNG_ASSERT(session
);
4310 if (session
->live_timer
!= 0) {
4311 ret
= LTTNG_ERR_LIVE_SESSION
;
4314 if (!session
->active
) {
4315 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4318 if (session
->ust_session
) {
4319 switch (session
->ust_session
->buffer_type
) {
4320 case LTTNG_BUFFER_PER_UID
:
4322 case LTTNG_BUFFER_PER_PID
:
4323 ret
= LTTNG_ERR_PER_PID_SESSION
;
4327 ret
= LTTNG_ERR_UNK
;
4331 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4332 session
->consumer
->relay_minor_version
< 8) {
4333 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4343 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4345 * Ask the consumer to truncate the existing metadata file(s) and
4346 * then regenerate the metadata. Live and per-pid sessions are not
4347 * supported and return an error.
4349 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4351 int cmd_regenerate_metadata(struct ltt_session
*session
)
4355 LTTNG_ASSERT(session
);
4357 ret
= check_regenerate_metadata_support(session
);
4362 if (session
->kernel_session
) {
4363 ret
= kernctl_session_regenerate_metadata(session
->kernel_session
->fd
);
4365 ERR("Failed to regenerate the kernel metadata");
4370 if (session
->ust_session
) {
4371 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4373 ERR("Failed to regenerate the UST metadata");
4377 DBG("Cmd metadata regenerate for session %s", session
->name
);
4385 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4387 * Ask the tracer to regenerate a new statedump.
4389 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4391 int cmd_regenerate_statedump(struct ltt_session
*session
)
4395 LTTNG_ASSERT(session
);
4397 if (!session
->active
) {
4398 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4402 if (session
->kernel_session
) {
4403 ret
= kernctl_session_regenerate_statedump(session
->kernel_session
->fd
);
4405 * Currently, the statedump in kernel can only fail if out
4409 if (ret
== -ENOMEM
) {
4410 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4412 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4414 ERR("Failed to regenerate the kernel statedump");
4419 if (session
->ust_session
) {
4420 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4422 * Currently, the statedump in UST always returns 0.
4425 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4426 ERR("Failed to regenerate the UST statedump");
4430 DBG("Cmd regenerate statedump for session %s", session
->name
);
4437 static enum lttng_error_code
4438 synchronize_tracer_notifier_register(struct notification_thread_handle
*notification_thread
,
4439 struct lttng_trigger
*trigger
,
4440 const struct lttng_credentials
*cmd_creds
)
4442 enum lttng_error_code ret_code
;
4443 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4444 const char *trigger_name
;
4445 uid_t trigger_owner
;
4446 enum lttng_trigger_status trigger_status
;
4447 const enum lttng_domain_type trigger_domain
=
4448 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4450 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4451 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4453 LTTNG_ASSERT(condition
);
4454 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4455 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4457 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4458 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4460 session_lock_list();
4461 switch (trigger_domain
) {
4462 case LTTNG_DOMAIN_KERNEL
:
4464 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4465 if (ret_code
!= LTTNG_OK
) {
4466 enum lttng_error_code notif_thread_unregister_ret
;
4468 notif_thread_unregister_ret
=
4469 notification_thread_command_unregister_trigger(notification_thread
,
4472 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4473 /* Return the original error code. */
4474 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4476 (int) trigger_owner
,
4480 goto end_unlock_session_list
;
4484 case LTTNG_DOMAIN_UST
:
4485 ust_app_global_update_all_event_notifier_rules();
4487 case LTTNG_DOMAIN_JUL
:
4488 case LTTNG_DOMAIN_LOG4J
:
4489 case LTTNG_DOMAIN_PYTHON
:
4491 /* Agent domains. */
4492 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4495 agt
= agent_create(trigger_domain
);
4497 ret_code
= LTTNG_ERR_NOMEM
;
4498 goto end_unlock_session_list
;
4501 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4504 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4505 if (ret_code
!= LTTNG_OK
) {
4506 goto end_unlock_session_list
;
4511 case LTTNG_DOMAIN_NONE
:
4516 ret_code
= LTTNG_OK
;
4517 end_unlock_session_list
:
4518 session_unlock_list();
4522 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4523 struct lttng_trigger
*trigger
,
4524 bool is_trigger_anonymous
,
4525 struct notification_thread_handle
*notification_thread
,
4526 struct lttng_trigger
**return_trigger
)
4528 enum lttng_error_code ret_code
;
4529 const char *trigger_name
;
4530 uid_t trigger_owner
;
4531 enum lttng_trigger_status trigger_status
;
4533 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4534 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4536 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4537 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4539 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4541 (int) trigger_owner
,
4542 (int) lttng_credentials_get_uid(cmd_creds
));
4545 * Validate the trigger credentials against the command credentials.
4546 * Only the root user can register a trigger with non-matching
4549 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4550 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4551 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4553 (int) trigger_owner
,
4554 (int) lttng_credentials_get_uid(cmd_creds
));
4555 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4561 * The bytecode generation also serves as a validation step for the
4562 * bytecode expressions.
4564 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4565 if (ret_code
!= LTTNG_OK
) {
4566 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4568 (int) trigger_owner
,
4574 * A reference to the trigger is acquired by the notification thread.
4575 * It is safe to return the same trigger to the caller since it the
4576 * other user holds a reference.
4578 * The trigger is modified during the execution of the
4579 * "register trigger" command. However, by the time the command returns,
4580 * it is safe to use without any locking as its properties are
4583 ret_code
= notification_thread_command_register_trigger(
4584 notification_thread
, trigger
, is_trigger_anonymous
);
4585 if (ret_code
!= LTTNG_OK
) {
4586 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4588 (int) trigger_owner
,
4593 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4594 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4597 * Synchronize tracers if the trigger adds an event notifier.
4599 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4600 ret_code
= synchronize_tracer_notifier_register(
4601 notification_thread
, trigger
, cmd_creds
);
4602 if (ret_code
!= LTTNG_OK
) {
4603 ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code
));
4609 * Return an updated trigger to the client.
4611 * Since a modified version of the same trigger is returned, acquire a
4612 * reference to the trigger so the caller doesn't have to care if those
4613 * are distinct instances or not.
4615 if (ret_code
== LTTNG_OK
) {
4616 lttng_trigger_get(trigger
);
4617 *return_trigger
= trigger
;
4618 /* Ownership of trigger was transferred to caller. */
4625 static enum lttng_error_code
4626 synchronize_tracer_notifier_unregister(const struct lttng_trigger
*trigger
)
4628 enum lttng_error_code ret_code
;
4629 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4630 const enum lttng_domain_type trigger_domain
=
4631 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4633 LTTNG_ASSERT(condition
);
4634 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4635 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4637 session_lock_list();
4638 switch (trigger_domain
) {
4639 case LTTNG_DOMAIN_KERNEL
:
4640 ret_code
= kernel_unregister_event_notifier(trigger
);
4641 if (ret_code
!= LTTNG_OK
) {
4642 goto end_unlock_session_list
;
4646 case LTTNG_DOMAIN_UST
:
4647 ust_app_global_update_all_event_notifier_rules();
4649 case LTTNG_DOMAIN_JUL
:
4650 case LTTNG_DOMAIN_LOG4J
:
4651 case LTTNG_DOMAIN_PYTHON
:
4653 /* Agent domains. */
4654 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4657 * This trigger was never registered in the first place. Calling
4658 * this function under those circumstances is an internal error.
4661 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4662 if (ret_code
!= LTTNG_OK
) {
4663 goto end_unlock_session_list
;
4668 case LTTNG_DOMAIN_NONE
:
4673 ret_code
= LTTNG_OK
;
4675 end_unlock_session_list
:
4676 session_unlock_list();
4680 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4681 const struct lttng_trigger
*trigger
,
4682 struct notification_thread_handle
*notification_thread
)
4684 enum lttng_error_code ret_code
;
4685 const char *trigger_name
;
4686 uid_t trigger_owner
;
4687 enum lttng_trigger_status trigger_status
;
4688 struct lttng_trigger
*sessiond_trigger
= nullptr;
4690 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4691 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4692 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4693 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4695 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4697 (int) trigger_owner
,
4698 (int) lttng_credentials_get_uid(cmd_creds
));
4701 * Validate the trigger credentials against the command credentials.
4702 * Only the root user can unregister a trigger with non-matching
4705 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4706 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4707 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4709 (int) trigger_owner
,
4710 (int) lttng_credentials_get_uid(cmd_creds
));
4711 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4716 /* Fetch the sessiond side trigger object. */
4717 ret_code
= notification_thread_command_get_trigger(
4718 notification_thread
, trigger
, &sessiond_trigger
);
4719 if (ret_code
!= LTTNG_OK
) {
4720 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4722 (int) trigger_owner
,
4727 LTTNG_ASSERT(sessiond_trigger
);
4730 * From this point on, no matter what, consider the trigger
4733 * We set the unregistered state of the sessiond side trigger object in
4734 * the client thread since we want to minimize the possibility of the
4735 * notification thread being stalled due to a long execution of an
4736 * action that required the trigger lock.
4738 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4740 ret_code
= notification_thread_command_unregister_trigger(notification_thread
, trigger
);
4741 if (ret_code
!= LTTNG_OK
) {
4742 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4744 (int) trigger_owner
,
4750 * Synchronize tracers if the trigger removes an event notifier.
4751 * Do this even if the trigger unregistration failed to at least stop
4752 * the tracers from producing notifications associated with this
4755 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4756 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4757 if (ret_code
!= LTTNG_OK
) {
4758 ERR("Error unregistering trigger to tracer.");
4764 lttng_trigger_put(sessiond_trigger
);
4768 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4769 struct notification_thread_handle
*notification_thread
,
4770 struct lttng_triggers
**return_triggers
)
4773 enum lttng_error_code ret_code
;
4774 struct lttng_triggers
*triggers
= nullptr;
4776 /* Get the set of triggers from the notification thread. */
4777 ret_code
= notification_thread_command_list_triggers(
4778 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4779 if (ret_code
!= LTTNG_OK
) {
4783 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4785 ret_code
= LTTNG_ERR_UNK
;
4789 *return_triggers
= triggers
;
4791 ret_code
= LTTNG_OK
;
4793 lttng_triggers_destroy(triggers
);
4797 enum lttng_error_code
4798 cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4799 const struct lttng_error_query
*query
,
4800 struct lttng_error_query_results
**_results
,
4801 struct notification_thread_handle
*notification_thread
)
4803 enum lttng_error_code ret_code
;
4804 const struct lttng_trigger
*query_target_trigger
;
4805 const struct lttng_action
*query_target_action
= nullptr;
4806 struct lttng_trigger
*matching_trigger
= nullptr;
4807 const char *trigger_name
;
4808 uid_t trigger_owner
;
4809 enum lttng_trigger_status trigger_status
;
4810 struct lttng_error_query_results
*results
= nullptr;
4812 switch (lttng_error_query_get_target_type(query
)) {
4813 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4814 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4816 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4817 query_target_trigger
= lttng_error_query_condition_borrow_target(query
);
4819 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4820 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(query
);
4826 LTTNG_ASSERT(query_target_trigger
);
4828 ret_code
= notification_thread_command_get_trigger(
4829 notification_thread
, query_target_trigger
, &matching_trigger
);
4830 if (ret_code
!= LTTNG_OK
) {
4834 /* No longer needed. */
4835 query_target_trigger
= nullptr;
4837 if (lttng_error_query_get_target_type(query
) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4838 /* Get the sessiond-side version of the target action. */
4839 query_target_action
=
4840 lttng_error_query_action_borrow_action_target(query
, matching_trigger
);
4843 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4844 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4845 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
, &trigger_owner
);
4846 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4848 results
= lttng_error_query_results_create();
4850 ret_code
= LTTNG_ERR_NOMEM
;
4854 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4856 (int) trigger_owner
,
4857 (int) lttng_credentials_get_uid(cmd_creds
));
4860 * Validate the trigger credentials against the command credentials.
4861 * Only the root user can target a trigger with non-matching
4864 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger
),
4866 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4867 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4869 (int) trigger_owner
,
4870 (int) lttng_credentials_get_uid(cmd_creds
));
4871 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4876 switch (lttng_error_query_get_target_type(query
)) {
4877 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4878 trigger_status
= lttng_trigger_add_error_results(matching_trigger
, results
);
4880 switch (trigger_status
) {
4881 case LTTNG_TRIGGER_STATUS_OK
:
4884 ret_code
= LTTNG_ERR_UNK
;
4889 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4892 lttng_trigger_condition_add_error_results(matching_trigger
, results
);
4894 switch (trigger_status
) {
4895 case LTTNG_TRIGGER_STATUS_OK
:
4898 ret_code
= LTTNG_ERR_UNK
;
4904 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4906 const enum lttng_action_status action_status
=
4907 lttng_action_add_error_query_results(query_target_action
, results
);
4909 switch (action_status
) {
4910 case LTTNG_ACTION_STATUS_OK
:
4913 ret_code
= LTTNG_ERR_UNK
;
4924 *_results
= results
;
4926 ret_code
= LTTNG_OK
;
4928 lttng_trigger_put(matching_trigger
);
4929 lttng_error_query_results_destroy(results
);
4934 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4935 * snapshot output is *not* set with a remote destination.
4937 * Return LTTNG_OK on success or a LTTNG_ERR code.
4939 static enum lttng_error_code
set_relayd_for_snapshot(struct consumer_output
*output
,
4940 const struct ltt_session
*session
)
4942 enum lttng_error_code status
= LTTNG_OK
;
4943 struct lttng_ht_iter iter
;
4944 struct consumer_socket
*socket
;
4945 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4946 const char *base_path
;
4948 LTTNG_ASSERT(output
);
4949 LTTNG_ASSERT(session
);
4951 DBG2("Set relayd object from snapshot output");
4953 if (session
->current_trace_chunk
) {
4954 enum lttng_trace_chunk_status chunk_status
= lttng_trace_chunk_get_id(
4955 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
4957 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4958 current_chunk_id
.is_set
= true;
4960 ERR("Failed to get current trace chunk id");
4961 status
= LTTNG_ERR_UNK
;
4966 /* Ignore if snapshot consumer output is not network. */
4967 if (output
->type
!= CONSUMER_DST_NET
) {
4972 * The snapshot record URI base path overrides the session
4975 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4976 base_path
= output
->dst
.net
.control
.subdir
;
4978 base_path
= session
->base_path
;
4982 * For each consumer socket, create and send the relayd object of the
4986 lttng::urcu::read_lock_guard read_lock
;
4988 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
4989 pthread_mutex_lock(socket
->lock
);
4990 status
= send_consumer_relayd_sockets(
4997 session
->live_timer
,
4998 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
4999 session
->creation_time
,
5000 session
->name_contains_creation_time
);
5001 pthread_mutex_unlock(socket
->lock
);
5002 if (status
!= LTTNG_OK
) {
5013 * Record a kernel snapshot.
5015 * Return LTTNG_OK on success or a LTTNG_ERR code.
5017 static enum lttng_error_code
record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
5018 const struct consumer_output
*output
,
5019 const struct ltt_session
*session
,
5020 uint64_t nb_packets_per_stream
)
5022 enum lttng_error_code status
;
5024 LTTNG_ASSERT(ksess
);
5025 LTTNG_ASSERT(output
);
5026 LTTNG_ASSERT(session
);
5028 status
= kernel_snapshot_record(ksess
, output
, nb_packets_per_stream
);
5033 * Record a UST snapshot.
5035 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5037 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5038 const struct consumer_output
*output
,
5039 const struct ltt_session
*session
,
5040 uint64_t nb_packets_per_stream
)
5042 enum lttng_error_code status
;
5044 LTTNG_ASSERT(usess
);
5045 LTTNG_ASSERT(output
);
5046 LTTNG_ASSERT(session
);
5048 status
= ust_app_snapshot_record(usess
, output
, nb_packets_per_stream
);
5052 static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session
*session
,
5053 uint64_t cur_nr_packets
)
5055 uint64_t tot_size
= 0;
5057 if (session
->kernel_session
) {
5058 struct ltt_kernel_channel
*chan
;
5059 const struct ltt_kernel_session
*ksess
= session
->kernel_session
;
5061 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
5062 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5064 * Don't take channel into account if we
5065 * already grab all its packets.
5069 tot_size
+= chan
->channel
->attr
.subbuf_size
* chan
->stream_count
;
5073 if (session
->ust_session
) {
5074 const struct ltt_ust_session
*usess
= session
->ust_session
;
5076 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
, cur_nr_packets
);
5083 * Calculate the number of packets we can grab from each stream that
5084 * fits within the overall snapshot max size.
5086 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5087 * the number of packets per stream.
5089 * TODO: this approach is not perfect: we consider the worse case
5090 * (packet filling the sub-buffers) as an upper bound, but we could do
5091 * better if we do this calculation while we actually grab the packet
5092 * content: we would know how much padding we don't actually store into
5095 * This algorithm is currently bounded by the number of packets per
5098 * Since we call this algorithm before actually grabbing the data, it's
5099 * an approximation: for instance, applications could appear/disappear
5100 * in between this call and actually grabbing data.
5102 static int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5106 uint64_t cur_nb_packets
= 0;
5109 return 0; /* Infinite */
5112 size_left
= max_size
;
5114 uint64_t one_more_packet_tot_size
;
5116 one_more_packet_tot_size
=
5117 get_session_size_one_more_packet_per_stream(session
, cur_nb_packets
);
5118 if (!one_more_packet_tot_size
) {
5119 /* We are already grabbing all packets. */
5122 size_left
-= one_more_packet_tot_size
;
5123 if (size_left
< 0) {
5128 if (!cur_nb_packets
&& size_left
!= max_size
) {
5129 /* Not enough room to grab one packet of each stream, error. */
5132 return cur_nb_packets
;
5135 static enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5136 const struct snapshot_output
*snapshot_output
)
5138 int64_t nb_packets_per_stream
;
5139 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5141 enum lttng_error_code ret_code
= LTTNG_OK
;
5142 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5143 struct consumer_output
*original_ust_consumer_output
= nullptr;
5144 struct consumer_output
*original_kernel_consumer_output
= nullptr;
5145 struct consumer_output
*snapshot_ust_consumer_output
= nullptr;
5146 struct consumer_output
*snapshot_kernel_consumer_output
= nullptr;
5148 ret
= snprintf(snapshot_chunk_name
,
5149 sizeof(snapshot_chunk_name
),
5151 snapshot_output
->name
,
5152 snapshot_output
->datetime
,
5153 snapshot_output
->nb_snapshot
);
5154 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5155 ERR("Failed to format snapshot name");
5156 ret_code
= LTTNG_ERR_INVALID
;
5159 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5160 snapshot_output
->name
,
5162 snapshot_chunk_name
);
5163 if (!session
->kernel_session
&& !session
->ust_session
) {
5164 ERR("Failed to record snapshot as no channels exist");
5165 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5169 if (session
->kernel_session
) {
5170 original_kernel_consumer_output
= session
->kernel_session
->consumer
;
5171 snapshot_kernel_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5172 strcpy(snapshot_kernel_consumer_output
->chunk_path
, snapshot_chunk_name
);
5174 /* Copy the original domain subdir. */
5175 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5176 original_kernel_consumer_output
->domain_subdir
);
5178 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5179 original_kernel_consumer_output
);
5181 ERR("Failed to copy consumer sockets from snapshot output configuration");
5182 ret_code
= LTTNG_ERR_NOMEM
;
5185 ret_code
= set_relayd_for_snapshot(snapshot_kernel_consumer_output
, session
);
5186 if (ret_code
!= LTTNG_OK
) {
5187 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5190 session
->kernel_session
->consumer
= snapshot_kernel_consumer_output
;
5192 if (session
->ust_session
) {
5193 original_ust_consumer_output
= session
->ust_session
->consumer
;
5194 snapshot_ust_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5195 strcpy(snapshot_ust_consumer_output
->chunk_path
, snapshot_chunk_name
);
5197 /* Copy the original domain subdir. */
5198 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5199 original_ust_consumer_output
->domain_subdir
);
5201 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5202 original_ust_consumer_output
);
5204 ERR("Failed to copy consumer sockets from snapshot output configuration");
5205 ret_code
= LTTNG_ERR_NOMEM
;
5208 ret_code
= set_relayd_for_snapshot(snapshot_ust_consumer_output
, session
);
5209 if (ret_code
!= LTTNG_OK
) {
5210 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5213 session
->ust_session
->consumer
= snapshot_ust_consumer_output
;
5216 snapshot_trace_chunk
= session_create_new_trace_chunk(
5218 snapshot_kernel_consumer_output
?: snapshot_ust_consumer_output
,
5219 consumer_output_get_base_path(snapshot_output
->consumer
),
5220 snapshot_chunk_name
);
5221 if (!snapshot_trace_chunk
) {
5222 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5224 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5227 LTTNG_ASSERT(!session
->current_trace_chunk
);
5228 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, nullptr);
5229 lttng_trace_chunk_put(snapshot_trace_chunk
);
5230 snapshot_trace_chunk
= nullptr;
5232 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5234 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5238 nb_packets_per_stream
=
5239 get_session_nb_packets_per_stream(session
, snapshot_output
->max_size
);
5240 if (nb_packets_per_stream
< 0) {
5241 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5242 goto error_close_trace_chunk
;
5245 if (session
->kernel_session
) {
5246 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5247 snapshot_kernel_consumer_output
,
5249 nb_packets_per_stream
);
5250 if (ret_code
!= LTTNG_OK
) {
5251 goto error_close_trace_chunk
;
5255 if (session
->ust_session
) {
5256 ret_code
= record_ust_snapshot(session
->ust_session
,
5257 snapshot_ust_consumer_output
,
5259 nb_packets_per_stream
);
5260 if (ret_code
!= LTTNG_OK
) {
5261 goto error_close_trace_chunk
;
5265 error_close_trace_chunk
:
5266 if (session_set_trace_chunk(session
, nullptr, &snapshot_trace_chunk
)) {
5267 ERR("Failed to release the current trace chunk of session \"%s\"", session
->name
);
5268 ret_code
= LTTNG_ERR_UNK
;
5271 if (session_close_trace_chunk(session
,
5272 snapshot_trace_chunk
,
5273 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
,
5276 * Don't goto end; make sure the chunk is closed for the session
5277 * to allow future snapshots.
5279 ERR("Failed to close snapshot trace chunk of session \"%s\"", session
->name
);
5280 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5283 lttng_trace_chunk_put(snapshot_trace_chunk
);
5284 snapshot_trace_chunk
= nullptr;
5286 if (original_ust_consumer_output
) {
5287 session
->ust_session
->consumer
= original_ust_consumer_output
;
5289 if (original_kernel_consumer_output
) {
5290 session
->kernel_session
->consumer
= original_kernel_consumer_output
;
5292 consumer_output_put(snapshot_ust_consumer_output
);
5293 consumer_output_put(snapshot_kernel_consumer_output
);
5298 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5300 * The wait parameter is ignored so this call always wait for the snapshot to
5301 * complete before returning.
5303 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5305 int cmd_snapshot_record(struct ltt_session
*session
,
5306 const struct lttng_snapshot_output
*output
,
5307 int wait
__attribute__((unused
)))
5309 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5311 unsigned int snapshot_success
= 0;
5313 struct snapshot_output
*tmp_output
= nullptr;
5315 LTTNG_ASSERT(session
);
5316 LTTNG_ASSERT(output
);
5318 DBG("Cmd snapshot record for session %s", session
->name
);
5320 /* Get the datetime for the snapshot output directory. */
5321 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
, sizeof(datetime
));
5323 cmd_ret
= LTTNG_ERR_INVALID
;
5328 * Permission denied to create an output if the session is not
5329 * set in no output mode.
5331 if (session
->output_traces
) {
5332 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5336 /* The session needs to be started at least once. */
5337 if (!session
->has_been_started
) {
5338 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5342 /* Use temporary output for the session. */
5343 if (*output
->ctrl_url
!= '\0') {
5344 tmp_output
= snapshot_output_alloc();
5346 cmd_ret
= LTTNG_ERR_NOMEM
;
5350 ret
= snapshot_output_init(session
,
5359 if (ret
== -ENOMEM
) {
5360 cmd_ret
= LTTNG_ERR_NOMEM
;
5362 cmd_ret
= LTTNG_ERR_INVALID
;
5366 /* Use the global session count for the temporary snapshot. */
5367 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5369 /* Use the global datetime */
5370 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5371 cmd_ret
= snapshot_record(session
, tmp_output
);
5372 if (cmd_ret
!= LTTNG_OK
) {
5375 snapshot_success
= 1;
5377 struct snapshot_output
*sout
;
5378 struct lttng_ht_iter iter
;
5380 lttng::urcu::read_lock_guard read_lock
;
5382 cds_lfht_for_each_entry (
5383 session
->snapshot
.output_ht
->ht
, &iter
.iter
, sout
, node
.node
) {
5384 struct snapshot_output output_copy
;
5387 * Make a local copy of the output and override output
5388 * parameters with those provided as part of the
5391 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5393 if (output
->max_size
!= (uint64_t) -1ULL) {
5394 output_copy
.max_size
= output
->max_size
;
5397 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5398 memcpy(output_copy
.datetime
, datetime
, sizeof(datetime
));
5400 /* Use temporary name. */
5401 if (*output
->name
!= '\0') {
5402 if (lttng_strncpy(output_copy
.name
,
5404 sizeof(output_copy
.name
))) {
5405 cmd_ret
= LTTNG_ERR_INVALID
;
5410 cmd_ret
= snapshot_record(session
, &output_copy
);
5411 if (cmd_ret
!= LTTNG_OK
) {
5415 snapshot_success
= 1;
5419 if (snapshot_success
) {
5420 session
->snapshot
.nb_snapshot
++;
5422 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5427 snapshot_output_destroy(tmp_output
);
5434 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5436 int cmd_set_session_shm_path(struct ltt_session
*session
, const char *shm_path
)
5439 LTTNG_ASSERT(session
);
5442 * Can only set shm path before session is started.
5444 if (session
->has_been_started
) {
5445 return LTTNG_ERR_SESSION_STARTED
;
5448 strncpy(session
->shm_path
, shm_path
, sizeof(session
->shm_path
));
5449 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5455 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5457 * Ask the consumer to rotate the session output directory.
5458 * The session lock must be held.
5460 * Returns LTTNG_OK on success or else a negative LTTng error code.
5462 int cmd_rotate_session(struct ltt_session
*session
,
5463 struct lttng_rotate_session_return
*rotate_return
,
5464 bool quiet_rotation
,
5465 enum lttng_trace_chunk_command_type command
)
5468 uint64_t ongoing_rotation_chunk_id
;
5469 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5470 struct lttng_trace_chunk
*chunk_being_archived
= nullptr;
5471 struct lttng_trace_chunk
*new_trace_chunk
= nullptr;
5472 enum lttng_trace_chunk_status chunk_status
;
5473 bool failed_to_rotate
= false;
5474 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5476 LTTNG_ASSERT(session
);
5478 if (!session
->has_been_started
) {
5479 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5484 * Explicit rotation is not supported for live sessions.
5485 * However, live sessions can perform a quiet rotation on
5487 * Rotation is not supported for snapshot traces (no output).
5489 if ((!quiet_rotation
&& session
->live_timer
) || !session
->output_traces
) {
5490 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5494 /* Unsupported feature in lttng-relayd before 2.11. */
5495 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5496 (session
->consumer
->relay_major_version
== 2 &&
5497 session
->consumer
->relay_minor_version
< 11)) {
5498 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5502 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5503 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5504 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5508 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5509 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5511 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5516 * After a stop, we only allow one rotation to occur, the other ones are
5517 * useless until a new start.
5519 if (session
->rotated_after_last_stop
) {
5520 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5522 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5527 * After a stop followed by a clear, disallow following rotations a they would
5528 * generate empty chunks.
5530 if (session
->cleared_after_last_stop
) {
5531 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5533 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5537 if (session
->active
) {
5539 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
5540 if (!new_trace_chunk
) {
5541 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5547 * The current trace chunk becomes the chunk being archived.
5549 * After this point, "chunk_being_archived" must absolutely
5550 * be closed on the consumer(s), otherwise it will never be
5551 * cleaned-up, which will result in a leak.
5553 ret
= session_set_trace_chunk(session
, new_trace_chunk
, &chunk_being_archived
);
5555 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5559 if (session
->kernel_session
) {
5560 cmd_ret
= kernel_rotate_session(session
);
5561 if (cmd_ret
!= LTTNG_OK
) {
5562 failed_to_rotate
= true;
5563 rotation_fail_code
= cmd_ret
;
5566 if (session
->ust_session
) {
5567 cmd_ret
= ust_app_rotate_session(session
);
5568 if (cmd_ret
!= LTTNG_OK
) {
5569 failed_to_rotate
= true;
5570 rotation_fail_code
= cmd_ret
;
5574 if (!session
->active
) {
5575 session
->rotated_after_last_stop
= true;
5578 if (!chunk_being_archived
) {
5579 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5581 if (failed_to_rotate
) {
5582 cmd_ret
= rotation_fail_code
;
5589 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5590 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
, &ongoing_rotation_chunk_id
);
5591 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5593 ret
= session_close_trace_chunk(
5594 session
, chunk_being_archived
, command
, session
->last_chunk_path
);
5596 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5600 if (failed_to_rotate
) {
5601 cmd_ret
= rotation_fail_code
;
5605 session
->quiet_rotation
= quiet_rotation
;
5606 ret
= timer_session_rotation_pending_check_start(session
, DEFAULT_ROTATE_PENDING_TIMER
);
5608 cmd_ret
= LTTNG_ERR_UNK
;
5612 if (rotate_return
) {
5613 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5616 session
->chunk_being_archived
= chunk_being_archived
;
5617 chunk_being_archived
= nullptr;
5618 if (!quiet_rotation
) {
5619 ret
= notification_thread_command_session_rotation_ongoing(
5620 the_notification_thread_handle
, session
->id
, ongoing_rotation_chunk_id
);
5621 if (ret
!= LTTNG_OK
) {
5622 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5624 cmd_ret
= (lttng_error_code
) ret
;
5628 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5630 ongoing_rotation_chunk_id
);
5632 lttng_trace_chunk_put(new_trace_chunk
);
5633 lttng_trace_chunk_put(chunk_being_archived
);
5634 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5637 if (session_reset_rotation_state(*session
, LTTNG_ROTATION_STATE_ERROR
)) {
5638 ERR("Failed to reset rotation state of session \"%s\"", session
->name
);
5644 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5646 * Check if the session has finished its rotation.
5648 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5650 int cmd_rotate_get_info(struct ltt_session
*session
,
5651 struct lttng_rotation_get_info_return
*info_return
,
5652 uint64_t rotation_id
)
5654 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5655 enum lttng_rotation_state rotation_state
;
5657 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
,
5659 session
->most_recent_chunk_id
.value
);
5661 if (session
->chunk_being_archived
) {
5662 enum lttng_trace_chunk_status chunk_status
;
5665 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
, &chunk_id
);
5666 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5668 rotation_state
= rotation_id
== chunk_id
? LTTNG_ROTATION_STATE_ONGOING
:
5669 LTTNG_ROTATION_STATE_EXPIRED
;
5671 if (session
->last_archived_chunk_id
.is_set
&&
5672 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5673 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5675 rotation_state
= session
->rotation_state
;
5679 switch (rotation_state
) {
5680 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5681 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5684 case LTTNG_ROTATION_STATE_EXPIRED
:
5685 DBG("Reporting that the rotation state of rotation id %" PRIu64
5686 " of session \"%s\" has expired",
5690 case LTTNG_ROTATION_STATE_ONGOING
:
5691 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5695 case LTTNG_ROTATION_STATE_COMPLETED
:
5699 char *current_tracing_path_reply
;
5700 size_t current_tracing_path_reply_len
;
5702 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5706 switch (session_get_consumer_destination_type(session
)) {
5707 case CONSUMER_DST_LOCAL
:
5708 current_tracing_path_reply
= info_return
->location
.local
.absolute_path
;
5709 current_tracing_path_reply_len
=
5710 sizeof(info_return
->location
.local
.absolute_path
);
5711 info_return
->location_type
=
5712 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5713 fmt_ret
= asprintf(&chunk_path
,
5714 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5715 session_get_base_path(session
),
5716 session
->last_archived_chunk_name
);
5717 if (fmt_ret
== -1) {
5718 PERROR("Failed to format the path of the last archived trace chunk");
5719 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5720 cmd_ret
= LTTNG_ERR_UNK
;
5724 case CONSUMER_DST_NET
:
5726 uint16_t ctrl_port
, data_port
;
5728 current_tracing_path_reply
= info_return
->location
.relay
.relative_path
;
5729 current_tracing_path_reply_len
=
5730 sizeof(info_return
->location
.relay
.relative_path
);
5731 /* Currently the only supported relay protocol. */
5732 info_return
->location
.relay
.protocol
=
5733 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5735 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5736 session_get_net_consumer_hostname(session
),
5737 sizeof(info_return
->location
.relay
.host
));
5739 ERR("Failed to copy host name to rotate_get_info reply");
5740 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5741 cmd_ret
= LTTNG_ERR_SET_URL
;
5745 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5746 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5747 info_return
->location
.relay
.ports
.data
= data_port
;
5748 info_return
->location_type
=
5749 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5750 chunk_path
= strdup(session
->last_chunk_path
);
5752 ERR("Failed to allocate the path of the last archived trace chunk");
5753 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5754 cmd_ret
= LTTNG_ERR_UNK
;
5763 fmt_ret
= lttng_strncpy(
5764 current_tracing_path_reply
, chunk_path
, current_tracing_path_reply_len
);
5767 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5768 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5769 cmd_ret
= LTTNG_ERR_UNK
;
5775 case LTTNG_ROTATION_STATE_ERROR
:
5776 DBG("Reporting that an error occurred during rotation %" PRIu64
5777 " of session \"%s\"",
5787 info_return
->status
= (int32_t) rotation_state
;
5792 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5794 * Configure the automatic rotation parameters.
5795 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5796 * 'activate' to false means deactivate the rotation schedule and validate that
5797 * 'new_value' has the same value as the currently active value.
5799 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5801 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5803 enum lttng_rotation_schedule_type schedule_type
,
5807 uint64_t *parameter_value
;
5809 LTTNG_ASSERT(session
);
5811 DBG("Cmd rotate set schedule session %s", session
->name
);
5813 if (session
->live_timer
|| !session
->output_traces
) {
5814 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5815 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5819 switch (schedule_type
) {
5820 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5821 parameter_value
= &session
->rotate_size
;
5823 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5824 parameter_value
= &session
->rotate_timer_period
;
5825 if (new_value
>= UINT_MAX
) {
5826 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
5830 ret
= LTTNG_ERR_INVALID
;
5835 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5836 ret
= LTTNG_ERR_INVALID
;
5840 /* Improper use of the API. */
5841 if (new_value
== -1ULL) {
5842 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5843 ret
= LTTNG_ERR_INVALID
;
5848 * As indicated in struct ltt_session's comments, a value of == 0 means
5849 * this schedule rotation type is not in use.
5851 * Reject the command if we were asked to activate a schedule that was
5854 if (activate
&& *parameter_value
!= 0) {
5855 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5856 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5861 * Reject the command if we were asked to deactivate a schedule that was
5864 if (!activate
&& *parameter_value
== 0) {
5865 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5866 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5871 * Reject the command if we were asked to deactivate a schedule that
5874 if (!activate
&& *parameter_value
!= new_value
) {
5875 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5876 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5880 *parameter_value
= activate
? new_value
: 0;
5882 switch (schedule_type
) {
5883 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5884 if (activate
&& session
->active
) {
5886 * Only start the timer if the session is active,
5887 * otherwise it will be started when the session starts.
5889 ret
= timer_session_rotation_schedule_timer_start(session
, new_value
);
5891 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5892 ret
= LTTNG_ERR_UNK
;
5896 ret
= timer_session_rotation_schedule_timer_stop(session
);
5898 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5899 ret
= LTTNG_ERR_UNK
;
5904 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5907 the_rotation_thread_handle
->subscribe_session_consumed_size_rotation(
5908 *session
, new_value
);
5909 } catch (const std::exception
& e
) {
5910 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5912 ret
= LTTNG_ERR_UNK
;
5917 the_rotation_thread_handle
5918 ->unsubscribe_session_consumed_size_rotation(*session
);
5919 } catch (const std::exception
& e
) {
5920 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5922 ret
= LTTNG_ERR_UNK
;
5928 /* Would have been caught before. */
5940 /* Wait for a given path to be removed before continuing. */
5941 static enum lttng_error_code
wait_on_path(void *path_data
)
5943 const char *shm_path
= (const char *) path_data
;
5945 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5951 ret
= stat(shm_path
, &st
);
5953 if (errno
!= ENOENT
) {
5954 PERROR("stat() returned an error while checking for the existence of the shm path");
5956 DBG("shm path no longer exists, completing the destruction of session");
5960 if (!S_ISDIR(st
.st_mode
)) {
5961 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5966 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5972 * Returns a pointer to a handler to run on completion of a command.
5973 * Returns NULL if no handler has to be run for the last command executed.
5975 const struct cmd_completion_handler
*cmd_pop_completion_handler()
5977 struct cmd_completion_handler
*handler
= current_completion_handler
;
5979 current_completion_handler
= nullptr;
5984 * Init command subsystem.
5989 * Set network sequence index to 1 for streams to match a relayd
5990 * socket on the consumer side.
5992 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5993 relayd_net_seq_idx
= 1;
5994 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5996 DBG("Command subsystem initialized");