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;
488 * We do not care about the filter bytecode and the fd from the
489 * userspace_probe_location.
491 ret
= lttng_event_serialize(tmp_event
,
492 uevent
->exclusion
? uevent
->exclusion
->count
: 0,
493 uevent
->exclusion
? (char **) uevent
->exclusion
->names
:
495 uevent
->filter_expression
,
499 lttng_event_destroy(tmp_event
);
501 ret_code
= LTTNG_ERR_FATAL
;
507 /* nb_events is already set at this point. */
509 *nb_events
= local_nb_events
;
515 * Fill lttng_event array of all kernel events in the channel.
517 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
518 struct ltt_kernel_session
*kernel_session
,
519 struct lttng_payload
*reply_payload
,
520 unsigned int *nb_events
)
522 enum lttng_error_code ret_code
;
524 struct ltt_kernel_event
*event
;
525 struct ltt_kernel_channel
*kchan
;
527 assert(reply_payload
);
529 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
530 if (kchan
== nullptr) {
531 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
535 *nb_events
= kchan
->event_count
;
537 DBG("Listing events for channel %s", kchan
->channel
->name
);
539 if (*nb_events
== 0) {
544 /* Kernel channels */
545 cds_list_for_each_entry (event
, &kchan
->events_list
.head
, list
) {
546 struct lttng_event
*tmp_event
= lttng_event_create();
549 ret_code
= LTTNG_ERR_NOMEM
;
553 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
554 lttng_event_destroy(tmp_event
);
555 ret_code
= LTTNG_ERR_FATAL
;
559 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
560 tmp_event
->enabled
= event
->enabled
;
561 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
563 switch (event
->event
->instrumentation
) {
564 case LTTNG_KERNEL_ABI_TRACEPOINT
:
565 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
567 case LTTNG_KERNEL_ABI_KRETPROBE
:
568 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
569 memcpy(&tmp_event
->attr
.probe
,
570 &event
->event
->u
.kprobe
,
571 sizeof(struct lttng_kernel_abi_kprobe
));
573 case LTTNG_KERNEL_ABI_KPROBE
:
574 tmp_event
->type
= LTTNG_EVENT_PROBE
;
575 memcpy(&tmp_event
->attr
.probe
,
576 &event
->event
->u
.kprobe
,
577 sizeof(struct lttng_kernel_abi_kprobe
));
579 case LTTNG_KERNEL_ABI_UPROBE
:
580 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
582 case LTTNG_KERNEL_ABI_FUNCTION
:
583 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
584 memcpy(&(tmp_event
->attr
.ftrace
),
585 &event
->event
->u
.ftrace
,
586 sizeof(struct lttng_kernel_abi_function
));
588 case LTTNG_KERNEL_ABI_NOOP
:
589 tmp_event
->type
= LTTNG_EVENT_NOOP
;
591 case LTTNG_KERNEL_ABI_SYSCALL
:
592 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
594 case LTTNG_KERNEL_ABI_ALL
:
601 if (event
->userspace_probe_location
) {
602 struct lttng_userspace_probe_location
*location_copy
=
603 lttng_userspace_probe_location_copy(
604 event
->userspace_probe_location
);
606 if (!location_copy
) {
607 lttng_event_destroy(tmp_event
);
608 ret_code
= LTTNG_ERR_NOMEM
;
612 ret
= lttng_event_set_userspace_probe_location(tmp_event
, location_copy
);
614 lttng_event_destroy(tmp_event
);
615 lttng_userspace_probe_location_destroy(location_copy
);
616 ret_code
= LTTNG_ERR_INVALID
;
621 ret
= lttng_event_serialize(
622 tmp_event
, 0, nullptr, event
->filter_expression
, 0, nullptr, reply_payload
);
623 lttng_event_destroy(tmp_event
);
625 ret_code
= LTTNG_ERR_FATAL
;
636 * Add URI so the consumer output object. Set the correct path depending on the
637 * domain adding the default trace directory.
639 static enum lttng_error_code
add_uri_to_consumer(const struct ltt_session
*session
,
640 struct consumer_output
*consumer
,
641 struct lttng_uri
*uri
,
642 enum lttng_domain_type domain
)
645 enum lttng_error_code ret_code
= LTTNG_OK
;
649 if (consumer
== nullptr) {
650 DBG("No consumer detected. Don't add URI. Stopping.");
651 ret_code
= LTTNG_ERR_NO_CONSUMER
;
656 case LTTNG_DOMAIN_KERNEL
:
657 ret
= lttng_strncpy(consumer
->domain_subdir
,
658 DEFAULT_KERNEL_TRACE_DIR
,
659 sizeof(consumer
->domain_subdir
));
661 case LTTNG_DOMAIN_UST
:
662 ret
= lttng_strncpy(consumer
->domain_subdir
,
663 DEFAULT_UST_TRACE_DIR
,
664 sizeof(consumer
->domain_subdir
));
668 * This case is possible is we try to add the URI to the global
669 * tracing session consumer object which in this case there is
672 memset(consumer
->domain_subdir
, 0, sizeof(consumer
->domain_subdir
));
676 ERR("Failed to initialize consumer output domain subdirectory");
677 ret_code
= LTTNG_ERR_FATAL
;
681 switch (uri
->dtype
) {
684 DBG2("Setting network URI to consumer");
686 if (consumer
->type
== CONSUMER_DST_NET
) {
687 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
688 consumer
->dst
.net
.control_isset
) ||
689 (uri
->stype
== LTTNG_STREAM_DATA
&& consumer
->dst
.net
.data_isset
)) {
690 ret_code
= LTTNG_ERR_URL_EXIST
;
694 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
697 /* Set URI into consumer output object */
698 ret
= consumer_set_network_uri(session
, consumer
, uri
);
700 ret_code
= (lttng_error_code
) -ret
;
702 } else if (ret
== 1) {
704 * URI was the same in the consumer so we do not append the subdir
705 * again so to not duplicate output dir.
712 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
713 ret_code
= LTTNG_ERR_INVALID
;
716 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
717 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
719 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
721 sizeof(consumer
->dst
.session_root_path
));
723 ret_code
= LTTNG_ERR_FATAL
;
726 consumer
->type
= CONSUMER_DST_LOCAL
;
736 * Init tracing by creating trace directory and sending fds kernel consumer.
738 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
741 struct lttng_ht_iter iter
;
742 struct consumer_socket
*socket
;
744 LTTNG_ASSERT(session
);
746 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= nullptr) {
747 lttng::urcu::read_lock_guard read_lock
;
749 cds_lfht_for_each_entry (
750 session
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
751 pthread_mutex_lock(socket
->lock
);
752 ret
= kernel_consumer_send_session(socket
, session
);
753 pthread_mutex_unlock(socket
->lock
);
755 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
766 * Create a socket to the relayd using the URI.
768 * On success, the relayd_sock pointer is set to the created socket.
769 * Else, it remains untouched and an LTTng error code is returned.
771 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
772 struct lttcomm_relayd_sock
**relayd_sock
,
773 struct consumer_output
*consumer
)
776 enum lttng_error_code status
= LTTNG_OK
;
777 struct lttcomm_relayd_sock
*rsock
;
779 rsock
= lttcomm_alloc_relayd_sock(
780 uri
, RELAYD_VERSION_COMM_MAJOR
, RELAYD_VERSION_COMM_MINOR
);
782 status
= LTTNG_ERR_FATAL
;
787 * Connect to relayd so we can proceed with a session creation. This call
788 * can possibly block for an arbitrary amount of time to set the health
789 * state to be in poll execution.
792 ret
= relayd_connect(rsock
);
795 ERR("Unable to reach lttng-relayd");
796 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
800 /* Create socket for control stream. */
801 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
802 uint64_t result_flags
;
804 DBG3("Creating relayd stream socket from URI");
806 /* Check relayd version */
807 ret
= relayd_version_check(rsock
);
808 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
809 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
811 } else if (ret
< 0) {
812 ERR("Unable to reach lttng-relayd");
813 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
816 consumer
->relay_major_version
= rsock
->major
;
817 consumer
->relay_minor_version
= rsock
->minor
;
818 ret
= relayd_get_configuration(rsock
, 0, &result_flags
);
820 ERR("Unable to get relayd configuration");
821 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
824 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
825 consumer
->relay_allows_clear
= true;
827 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
828 DBG3("Creating relayd data socket from URI");
830 /* Command is not valid */
831 ERR("Relayd invalid stream type: %d", uri
->stype
);
832 status
= LTTNG_ERR_INVALID
;
836 *relayd_sock
= rsock
;
841 /* The returned value is not useful since we are on an error path. */
842 (void) relayd_close(rsock
);
850 * Connect to the relayd using URI and send the socket to the right consumer.
852 * The consumer socket lock must be held by the caller.
854 * Returns LTTNG_OK on success or an LTTng error code on failure.
856 static enum lttng_error_code
send_consumer_relayd_socket(unsigned int session_id
,
857 struct lttng_uri
*relayd_uri
,
858 struct consumer_output
*consumer
,
859 struct consumer_socket
*consumer_sock
,
860 const char *session_name
,
861 const char *hostname
,
862 const char *base_path
,
863 int session_live_timer
,
864 const uint64_t *current_chunk_id
,
865 time_t session_creation_time
,
866 bool session_name_contains_creation_time
)
869 struct lttcomm_relayd_sock
*rsock
= nullptr;
870 enum lttng_error_code status
;
872 /* Connect to relayd and make version check if uri is the control. */
873 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
874 if (status
!= LTTNG_OK
) {
875 goto relayd_comm_error
;
879 /* Set the network sequence index if not set. */
880 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
881 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
883 * Increment net_seq_idx because we are about to transfer the
884 * new relayd socket to the consumer.
885 * Assign unique key so the consumer can match streams.
887 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
888 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
891 /* Send relayd socket to consumer. */
892 ret
= consumer_send_relayd_socket(consumer_sock
,
902 session_creation_time
,
903 session_name_contains_creation_time
);
905 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
909 /* Flag that the corresponding socket was sent. */
910 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
911 consumer_sock
->control_sock_sent
= 1;
912 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
913 consumer_sock
->data_sock_sent
= 1;
917 * Close socket which was dup on the consumer side. The session daemon does
918 * NOT keep track of the relayd socket(s) once transfer to the consumer.
922 if (status
!= LTTNG_OK
) {
924 * The consumer output for this session should not be used anymore
925 * since the relayd connection failed thus making any tracing or/and
926 * streaming not usable.
928 consumer
->enabled
= false;
930 (void) relayd_close(rsock
);
938 * Send both relayd sockets to a specific consumer and domain. This is a
939 * helper function to facilitate sending the information to the consumer for a
942 * The consumer socket lock must be held by the caller.
944 * Returns LTTNG_OK, or an LTTng error code on failure.
946 static enum lttng_error_code
send_consumer_relayd_sockets(unsigned int session_id
,
947 struct consumer_output
*consumer
,
948 struct consumer_socket
*sock
,
949 const char *session_name
,
950 const char *hostname
,
951 const char *base_path
,
952 int session_live_timer
,
953 const uint64_t *current_chunk_id
,
954 time_t session_creation_time
,
955 bool session_name_contains_creation_time
)
957 enum lttng_error_code status
= LTTNG_OK
;
959 LTTNG_ASSERT(consumer
);
962 /* Sending control relayd socket. */
963 if (!sock
->control_sock_sent
) {
964 status
= send_consumer_relayd_socket(session_id
,
965 &consumer
->dst
.net
.control
,
973 session_creation_time
,
974 session_name_contains_creation_time
);
975 if (status
!= LTTNG_OK
) {
980 /* Sending data relayd socket. */
981 if (!sock
->data_sock_sent
) {
982 status
= send_consumer_relayd_socket(session_id
,
983 &consumer
->dst
.net
.data
,
991 session_creation_time
,
992 session_name_contains_creation_time
);
993 if (status
!= LTTNG_OK
) {
1003 * Setup relayd connections for a tracing session. First creates the socket to
1004 * the relayd and send them to the right domain consumer. Consumer type MUST be
1007 int cmd_setup_relayd(struct ltt_session
*session
)
1010 struct ltt_ust_session
*usess
;
1011 struct ltt_kernel_session
*ksess
;
1012 struct consumer_socket
*socket
;
1013 struct lttng_ht_iter iter
;
1014 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1016 LTTNG_ASSERT(session
);
1018 usess
= session
->ust_session
;
1019 ksess
= session
->kernel_session
;
1021 DBG("Setting relayd for session %s", session
->name
);
1023 if (session
->current_trace_chunk
) {
1024 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1025 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1027 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1028 current_chunk_id
.is_set
= true;
1030 ERR("Failed to get current trace chunk id");
1031 ret
= LTTNG_ERR_UNK
;
1036 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
&&
1037 usess
->consumer
->enabled
) {
1038 /* For each consumer socket, send relayd sockets */
1039 lttng::urcu::read_lock_guard read_lock
;
1041 cds_lfht_for_each_entry (
1042 usess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1043 pthread_mutex_lock(socket
->lock
);
1044 ret
= send_consumer_relayd_sockets(
1051 session
->live_timer
,
1052 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
1053 session
->creation_time
,
1054 session
->name_contains_creation_time
);
1055 pthread_mutex_unlock(socket
->lock
);
1056 if (ret
!= LTTNG_OK
) {
1059 /* Session is now ready for network streaming. */
1060 session
->net_handle
= 1;
1063 session
->consumer
->relay_major_version
= usess
->consumer
->relay_major_version
;
1064 session
->consumer
->relay_minor_version
= usess
->consumer
->relay_minor_version
;
1065 session
->consumer
->relay_allows_clear
= usess
->consumer
->relay_allows_clear
;
1068 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
&&
1069 ksess
->consumer
->enabled
) {
1070 lttng::urcu::read_lock_guard read_lock
;
1072 cds_lfht_for_each_entry (
1073 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1074 pthread_mutex_lock(socket
->lock
);
1075 ret
= send_consumer_relayd_sockets(
1082 session
->live_timer
,
1083 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
1084 session
->creation_time
,
1085 session
->name_contains_creation_time
);
1086 pthread_mutex_unlock(socket
->lock
);
1087 if (ret
!= LTTNG_OK
) {
1090 /* Session is now ready for network streaming. */
1091 session
->net_handle
= 1;
1094 session
->consumer
->relay_major_version
= ksess
->consumer
->relay_major_version
;
1095 session
->consumer
->relay_minor_version
= ksess
->consumer
->relay_minor_version
;
1096 session
->consumer
->relay_allows_clear
= ksess
->consumer
->relay_allows_clear
;
1104 * Start a kernel session by opening all necessary streams.
1106 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1109 struct ltt_kernel_channel
*kchan
;
1111 /* Open kernel metadata */
1112 if (ksess
->metadata
== nullptr && ksess
->output_traces
) {
1113 ret
= kernel_open_metadata(ksess
);
1115 ret
= LTTNG_ERR_KERN_META_FAIL
;
1120 /* Open kernel metadata stream */
1121 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1122 ret
= kernel_open_metadata_stream(ksess
);
1124 ERR("Kernel create metadata stream failed");
1125 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1130 /* For each channel */
1131 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1132 if (kchan
->stream_count
== 0) {
1133 ret
= kernel_open_channel_stream(kchan
);
1135 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1138 /* Update the stream global counter */
1139 ksess
->stream_count_global
+= ret
;
1143 /* Setup kernel consumer socket and send fds to it */
1144 ret
= init_kernel_tracing(ksess
);
1146 ret
= LTTNG_ERR_KERN_START_FAIL
;
1150 /* This start the kernel tracing */
1151 ret
= kernel_start_session(ksess
);
1153 ret
= LTTNG_ERR_KERN_START_FAIL
;
1157 /* Quiescent wait after starting trace */
1158 kernel_wait_quiescent();
1160 ksess
->active
= true;
1168 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1170 struct ltt_kernel_channel
*kchan
;
1171 bool error_occurred
= false;
1174 if (!ksess
|| !ksess
->active
) {
1177 DBG("Stopping kernel tracing");
1179 ret
= kernel_stop_session(ksess
);
1181 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1185 kernel_wait_quiescent();
1187 /* Flush metadata after stopping (if exists) */
1188 if (ksess
->metadata_stream_fd
>= 0) {
1189 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1191 ERR("Kernel metadata flush failed");
1192 error_occurred
= true;
1196 /* Flush all buffers after stopping */
1197 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1198 ret
= kernel_flush_buffer(kchan
);
1200 ERR("Kernel flush buffer error");
1201 error_occurred
= true;
1205 ksess
->active
= false;
1206 if (error_occurred
) {
1207 ret
= LTTNG_ERR_UNK
;
1216 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1218 int cmd_disable_channel(struct ltt_session
*session
,
1219 enum lttng_domain_type domain
,
1223 struct ltt_ust_session
*usess
;
1225 usess
= session
->ust_session
;
1227 lttng::urcu::read_lock_guard read_lock
;
1230 case LTTNG_DOMAIN_KERNEL
:
1232 ret
= channel_kernel_disable(session
->kernel_session
, channel_name
);
1233 if (ret
!= LTTNG_OK
) {
1237 kernel_wait_quiescent();
1240 case LTTNG_DOMAIN_UST
:
1242 struct ltt_ust_channel
*uchan
;
1243 struct lttng_ht
*chan_ht
;
1245 chan_ht
= usess
->domain_global
.channels
;
1247 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1248 if (uchan
== nullptr) {
1249 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1253 ret
= channel_ust_disable(usess
, uchan
);
1254 if (ret
!= LTTNG_OK
) {
1260 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1271 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1273 * The wpipe arguments is used as a notifier for the kernel thread.
1275 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1279 ssize_t sock_recv_len
;
1280 struct lttng_channel
*channel
= nullptr;
1281 struct lttng_buffer_view view
;
1282 struct lttng_dynamic_buffer channel_buffer
;
1283 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1285 lttng_dynamic_buffer_init(&channel_buffer
);
1286 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1287 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1289 ret
= LTTNG_ERR_NOMEM
;
1293 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
, channel_len
);
1294 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1295 ERR("Failed to receive \"enable channel\" command payload");
1296 ret
= LTTNG_ERR_INVALID
;
1300 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1301 if (!lttng_buffer_view_is_valid(&view
)) {
1302 ret
= LTTNG_ERR_INVALID
;
1306 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1307 ERR("Invalid channel payload received in \"enable channel\" command");
1308 ret
= LTTNG_ERR_INVALID
;
1312 ret
= cmd_enable_channel_internal(cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1315 lttng_dynamic_buffer_reset(&channel_buffer
);
1316 lttng_channel_destroy(channel
);
1320 static enum lttng_error_code
cmd_enable_channel_internal(struct ltt_session
*session
,
1321 const struct lttng_domain
*domain
,
1322 const struct lttng_channel
*_attr
,
1325 enum lttng_error_code ret_code
;
1326 struct ltt_ust_session
*usess
= session
->ust_session
;
1327 struct lttng_ht
*chan_ht
;
1329 struct lttng_channel
*attr
= nullptr;
1331 LTTNG_ASSERT(session
);
1332 LTTNG_ASSERT(_attr
);
1333 LTTNG_ASSERT(domain
);
1335 lttng::urcu::read_lock_guard read_lock
;
1337 attr
= lttng_channel_copy(_attr
);
1339 ret_code
= LTTNG_ERR_NOMEM
;
1343 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1345 /* Validate channel name */
1346 if (attr
->name
[0] == '.' || memchr(attr
->name
, '/', len
) != nullptr) {
1347 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1351 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1354 * If the session is a live session, remove the switch timer, the
1355 * live timer does the same thing but sends also synchronisation
1356 * beacons for inactive streams.
1358 if (session
->live_timer
> 0) {
1359 attr
->attr
.live_timer_interval
= session
->live_timer
;
1360 attr
->attr
.switch_timer_interval
= 0;
1363 /* Check for feature support */
1364 switch (domain
->type
) {
1365 case LTTNG_DOMAIN_KERNEL
:
1367 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1368 /* Sampling position of buffer is not supported */
1369 WARN("Kernel tracer does not support buffer monitoring. "
1370 "Setting the monitor interval timer to 0 "
1371 "(disabled) for channel '%s' of session '%s'",
1374 lttng_channel_set_monitor_timer_interval(attr
, 0);
1378 case LTTNG_DOMAIN_UST
:
1380 case LTTNG_DOMAIN_JUL
:
1381 case LTTNG_DOMAIN_LOG4J
:
1382 case LTTNG_DOMAIN_PYTHON
:
1383 if (!agent_tracing_is_enabled()) {
1384 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1385 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1390 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1394 switch (domain
->type
) {
1395 case LTTNG_DOMAIN_KERNEL
:
1397 struct ltt_kernel_channel
*kchan
;
1399 kchan
= trace_kernel_get_channel_by_name(attr
->name
, session
->kernel_session
);
1400 if (kchan
== nullptr) {
1402 * Don't try to create a channel if the session has been started at
1403 * some point in time before. The tracer does not allow it.
1405 if (session
->has_been_started
) {
1406 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1410 if (session
->snapshot
.nb_output
> 0 || session
->snapshot_mode
) {
1411 /* Enforce mmap output for snapshot sessions. */
1412 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1414 ret_code
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1415 if (attr
->name
[0] != '\0') {
1416 session
->kernel_session
->has_non_default_channel
= 1;
1419 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1422 if (ret_code
!= LTTNG_OK
) {
1426 kernel_wait_quiescent();
1429 case LTTNG_DOMAIN_UST
:
1430 case LTTNG_DOMAIN_JUL
:
1431 case LTTNG_DOMAIN_LOG4J
:
1432 case LTTNG_DOMAIN_PYTHON
:
1434 struct ltt_ust_channel
*uchan
;
1439 * Current agent implementation limitations force us to allow
1440 * only one channel at once in "agent" subdomains. Each
1441 * subdomain has a default channel name which must be strictly
1444 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1445 if (strncmp(attr
->name
,
1446 DEFAULT_JUL_CHANNEL_NAME
,
1447 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1448 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1451 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1452 if (strncmp(attr
->name
,
1453 DEFAULT_LOG4J_CHANNEL_NAME
,
1454 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1455 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1458 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1459 if (strncmp(attr
->name
,
1460 DEFAULT_PYTHON_CHANNEL_NAME
,
1461 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1462 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1467 chan_ht
= usess
->domain_global
.channels
;
1469 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1470 if (uchan
== nullptr) {
1472 * Don't try to create a channel if the session has been started at
1473 * some point in time before. The tracer does not allow it.
1475 if (session
->has_been_started
) {
1476 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1480 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1481 if (attr
->name
[0] != '\0') {
1482 usess
->has_non_default_channel
= 1;
1485 ret_code
= channel_ust_enable(usess
, uchan
);
1490 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1494 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1495 session
->has_non_mmap_channel
= true;
1499 lttng_channel_destroy(attr
);
1503 enum lttng_error_code
1504 cmd_process_attr_tracker_get_tracking_policy(struct ltt_session
*session
,
1505 enum lttng_domain_type domain
,
1506 enum lttng_process_attr process_attr
,
1507 enum lttng_tracking_policy
*policy
)
1509 enum lttng_error_code ret_code
= LTTNG_OK
;
1510 const struct process_attr_tracker
*tracker
;
1513 case LTTNG_DOMAIN_KERNEL
:
1514 if (!session
->kernel_session
) {
1515 ret_code
= LTTNG_ERR_INVALID
;
1518 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1520 case LTTNG_DOMAIN_UST
:
1521 if (!session
->ust_session
) {
1522 ret_code
= LTTNG_ERR_INVALID
;
1525 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1528 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1532 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1534 ret_code
= LTTNG_ERR_INVALID
;
1540 enum lttng_error_code
1541 cmd_process_attr_tracker_set_tracking_policy(struct ltt_session
*session
,
1542 enum lttng_domain_type domain
,
1543 enum lttng_process_attr process_attr
,
1544 enum lttng_tracking_policy policy
)
1546 enum lttng_error_code ret_code
= LTTNG_OK
;
1549 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1550 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1551 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1554 ret_code
= LTTNG_ERR_INVALID
;
1559 case LTTNG_DOMAIN_KERNEL
:
1560 if (!session
->kernel_session
) {
1561 ret_code
= LTTNG_ERR_INVALID
;
1564 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1565 session
->kernel_session
, process_attr
, policy
);
1567 case LTTNG_DOMAIN_UST
:
1568 if (!session
->ust_session
) {
1569 ret_code
= LTTNG_ERR_INVALID
;
1572 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1573 session
->ust_session
, process_attr
, policy
);
1576 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1583 enum lttng_error_code
1584 cmd_process_attr_tracker_inclusion_set_add_value(struct ltt_session
*session
,
1585 enum lttng_domain_type domain
,
1586 enum lttng_process_attr process_attr
,
1587 const struct process_attr_value
*value
)
1589 enum lttng_error_code ret_code
= LTTNG_OK
;
1592 case LTTNG_DOMAIN_KERNEL
:
1593 if (!session
->kernel_session
) {
1594 ret_code
= LTTNG_ERR_INVALID
;
1597 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1598 session
->kernel_session
, process_attr
, value
);
1600 case LTTNG_DOMAIN_UST
:
1601 if (!session
->ust_session
) {
1602 ret_code
= LTTNG_ERR_INVALID
;
1605 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1606 session
->ust_session
, process_attr
, value
);
1609 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1616 enum lttng_error_code
1617 cmd_process_attr_tracker_inclusion_set_remove_value(struct ltt_session
*session
,
1618 enum lttng_domain_type domain
,
1619 enum lttng_process_attr process_attr
,
1620 const struct process_attr_value
*value
)
1622 enum lttng_error_code ret_code
= LTTNG_OK
;
1625 case LTTNG_DOMAIN_KERNEL
:
1626 if (!session
->kernel_session
) {
1627 ret_code
= LTTNG_ERR_INVALID
;
1630 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1631 session
->kernel_session
, process_attr
, value
);
1633 case LTTNG_DOMAIN_UST
:
1634 if (!session
->ust_session
) {
1635 ret_code
= LTTNG_ERR_INVALID
;
1638 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1639 session
->ust_session
, process_attr
, value
);
1642 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1649 enum lttng_error_code
1650 cmd_process_attr_tracker_get_inclusion_set(struct ltt_session
*session
,
1651 enum lttng_domain_type domain
,
1652 enum lttng_process_attr process_attr
,
1653 struct lttng_process_attr_values
**values
)
1655 enum lttng_error_code ret_code
= LTTNG_OK
;
1656 const struct process_attr_tracker
*tracker
;
1657 enum process_attr_tracker_status status
;
1660 case LTTNG_DOMAIN_KERNEL
:
1661 if (!session
->kernel_session
) {
1662 ret_code
= LTTNG_ERR_INVALID
;
1665 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1667 case LTTNG_DOMAIN_UST
:
1668 if (!session
->ust_session
) {
1669 ret_code
= LTTNG_ERR_INVALID
;
1672 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1675 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1680 ret_code
= LTTNG_ERR_INVALID
;
1684 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1686 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1687 ret_code
= LTTNG_OK
;
1689 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1690 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1692 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1693 ret_code
= LTTNG_ERR_NOMEM
;
1696 ret_code
= LTTNG_ERR_UNK
;
1705 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1707 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1708 struct lttng_event
*event
,
1709 char *filter_expression
,
1710 struct lttng_bytecode
*bytecode
,
1711 struct lttng_event_exclusion
*exclusion
)
1714 const char *event_name
;
1715 const struct ltt_session
*session
= cmd_ctx
->session
;
1716 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1717 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1719 DBG("Disable event command for event \'%s\'", event
->name
);
1722 * Filter and exclusions are simply not handled by the
1723 * disable event command at this time.
1727 (void) filter_expression
;
1730 /* Ignore the presence of filter or exclusion for the event */
1732 event
->exclusion
= 0;
1734 event_name
= event
->name
;
1736 lttng::urcu::read_lock_guard read_lock
;
1738 /* Error out on unhandled search criteria */
1739 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
|| event
->pid
||
1740 event
->filter
|| event
->exclusion
) {
1741 ret
= LTTNG_ERR_UNK
;
1746 case LTTNG_DOMAIN_KERNEL
:
1748 struct ltt_kernel_channel
*kchan
;
1749 struct ltt_kernel_session
*ksess
;
1751 ksess
= session
->kernel_session
;
1754 * If a non-default channel has been created in the
1755 * session, explicitely require that -c chan_name needs
1758 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1759 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1763 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1764 if (kchan
== nullptr) {
1765 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1769 switch (event
->type
) {
1770 case LTTNG_EVENT_ALL
:
1771 case LTTNG_EVENT_TRACEPOINT
:
1772 case LTTNG_EVENT_SYSCALL
:
1773 case LTTNG_EVENT_PROBE
:
1774 case LTTNG_EVENT_FUNCTION
:
1775 case LTTNG_EVENT_FUNCTION_ENTRY
: /* fall-through */
1776 if (event_name
[0] == '\0') {
1777 ret
= event_kernel_disable_event(kchan
, nullptr, event
->type
);
1779 ret
= event_kernel_disable_event(kchan
, event_name
, event
->type
);
1781 if (ret
!= LTTNG_OK
) {
1786 ret
= LTTNG_ERR_UNK
;
1790 kernel_wait_quiescent();
1793 case LTTNG_DOMAIN_UST
:
1795 struct ltt_ust_channel
*uchan
;
1796 struct ltt_ust_session
*usess
;
1798 usess
= session
->ust_session
;
1800 if (validate_ust_event_name(event_name
)) {
1801 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1806 * If a non-default channel has been created in the
1807 * session, explicitly require that -c chan_name needs
1810 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1811 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1815 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
1816 if (uchan
== nullptr) {
1817 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1821 switch (event
->type
) {
1822 case LTTNG_EVENT_ALL
:
1824 * An empty event name means that everything
1825 * should be disabled.
1827 if (event
->name
[0] == '\0') {
1828 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1830 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1832 if (ret
!= LTTNG_OK
) {
1837 ret
= LTTNG_ERR_UNK
;
1841 DBG3("Disable UST event %s in channel %s completed", event_name
, channel_name
);
1844 case LTTNG_DOMAIN_LOG4J
:
1845 case LTTNG_DOMAIN_JUL
:
1846 case LTTNG_DOMAIN_PYTHON
:
1849 struct ltt_ust_session
*usess
= session
->ust_session
;
1851 LTTNG_ASSERT(usess
);
1853 switch (event
->type
) {
1854 case LTTNG_EVENT_ALL
:
1857 ret
= LTTNG_ERR_UNK
;
1861 agt
= trace_ust_find_agent(usess
, domain
);
1863 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1867 * An empty event name means that everything
1868 * should be disabled.
1870 if (event
->name
[0] == '\0') {
1871 ret
= event_agent_disable_all(usess
, agt
);
1873 ret
= event_agent_disable(usess
, agt
, event_name
);
1875 if (ret
!= LTTNG_OK
) {
1882 ret
= LTTNG_ERR_UND
;
1892 free(filter_expression
);
1897 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1899 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1900 const struct lttng_event_context
*event_context
,
1903 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1904 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1905 const struct ltt_session
*session
= cmd_ctx
->session
;
1906 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1909 * Don't try to add a context if the session has been started at
1910 * some point in time before. The tracer does not allow it and would
1911 * result in a corrupted trace.
1913 if (cmd_ctx
->session
->has_been_started
) {
1914 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1919 case LTTNG_DOMAIN_KERNEL
:
1920 LTTNG_ASSERT(session
->kernel_session
);
1922 if (session
->kernel_session
->channel_count
== 0) {
1923 /* Create default channel */
1924 ret
= channel_kernel_create(session
->kernel_session
, nullptr, kwpipe
);
1925 if (ret
!= LTTNG_OK
) {
1928 chan_kern_created
= 1;
1930 /* Add kernel context to kernel tracer */
1931 ret
= context_kernel_add(session
->kernel_session
, event_context
, channel_name
);
1932 if (ret
!= LTTNG_OK
) {
1936 case LTTNG_DOMAIN_JUL
:
1937 case LTTNG_DOMAIN_LOG4J
:
1940 * Validate channel name.
1941 * If no channel name is given and the domain is JUL or LOG4J,
1942 * set it to the appropriate domain-specific channel name. If
1943 * a name is provided but does not match the expexted channel
1944 * name, return an error.
1946 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1947 strcmp(channel_name
, DEFAULT_JUL_CHANNEL_NAME
) != 0) {
1948 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1950 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1951 strcmp(channel_name
, DEFAULT_LOG4J_CHANNEL_NAME
) != 0) {
1952 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1957 case LTTNG_DOMAIN_UST
:
1959 struct ltt_ust_session
*usess
= session
->ust_session
;
1960 unsigned int chan_count
;
1962 LTTNG_ASSERT(usess
);
1964 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1965 if (chan_count
== 0) {
1966 struct lttng_channel
*attr
;
1967 /* Create default channel */
1968 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1969 if (attr
== nullptr) {
1970 ret
= LTTNG_ERR_FATAL
;
1974 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1975 if (ret
!= LTTNG_OK
) {
1979 channel_attr_destroy(attr
);
1980 chan_ust_created
= 1;
1983 ret
= context_ust_add(usess
, domain
, event_context
, channel_name
);
1984 if (ret
!= LTTNG_OK
) {
1990 ret
= LTTNG_ERR_UND
;
1998 if (chan_kern_created
) {
1999 struct ltt_kernel_channel
*kchan
= trace_kernel_get_channel_by_name(
2000 DEFAULT_CHANNEL_NAME
, session
->kernel_session
);
2001 /* Created previously, this should NOT fail. */
2002 LTTNG_ASSERT(kchan
);
2003 kernel_destroy_channel(kchan
);
2006 if (chan_ust_created
) {
2007 struct ltt_ust_channel
*uchan
= trace_ust_find_channel_by_name(
2008 session
->ust_session
->domain_global
.channels
, DEFAULT_CHANNEL_NAME
);
2009 /* Created previously, this should NOT fail. */
2010 LTTNG_ASSERT(uchan
);
2011 /* Remove from the channel list of the session. */
2012 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
, uchan
);
2013 trace_ust_destroy_channel(uchan
);
2019 static inline bool name_starts_with(const char *name
, const char *prefix
)
2021 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2023 return !strncmp(name
, prefix
, max_cmp_len
);
2026 /* Perform userspace-specific event name validation */
2027 static int validate_ust_event_name(const char *name
)
2037 * Check name against all internal UST event component namespaces used
2040 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2041 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2042 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2051 * Internal version of cmd_enable_event() with a supplemental
2052 * "internal_event" flag which is used to enable internal events which should
2053 * be hidden from clients. Such events are used in the agent implementation to
2054 * enable the events through which all "agent" events are funeled.
2056 static int _cmd_enable_event(struct ltt_session
*session
,
2057 const struct lttng_domain
*domain
,
2059 struct lttng_event
*event
,
2060 char *filter_expression
,
2061 struct lttng_bytecode
*filter
,
2062 struct lttng_event_exclusion
*exclusion
,
2064 bool internal_event
)
2066 int ret
= 0, channel_created
= 0;
2067 struct lttng_channel
*attr
= nullptr;
2069 LTTNG_ASSERT(session
);
2070 LTTNG_ASSERT(event
);
2071 LTTNG_ASSERT(channel_name
);
2073 /* If we have a filter, we must have its filter expression */
2074 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2076 /* Normalize event name as a globbing pattern */
2077 strutils_normalize_star_glob_pattern(event
->name
);
2079 /* Normalize exclusion names as globbing patterns */
2083 for (i
= 0; i
< exclusion
->count
; i
++) {
2084 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2086 strutils_normalize_star_glob_pattern(name
);
2090 DBG("Enable event command for event \'%s\'", event
->name
);
2092 lttng::urcu::read_lock_guard read_lock
;
2094 switch (domain
->type
) {
2095 case LTTNG_DOMAIN_KERNEL
:
2097 struct ltt_kernel_channel
*kchan
;
2100 * If a non-default channel has been created in the
2101 * session, explicitely require that -c chan_name needs
2104 if (session
->kernel_session
->has_non_default_channel
&& channel_name
[0] == '\0') {
2105 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2109 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2110 if (kchan
== nullptr) {
2111 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
, LTTNG_BUFFER_GLOBAL
);
2112 if (attr
== nullptr) {
2113 ret
= LTTNG_ERR_FATAL
;
2116 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2117 ret
= LTTNG_ERR_INVALID
;
2121 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2122 if (ret
!= LTTNG_OK
) {
2125 channel_created
= 1;
2128 /* Get the newly created kernel channel pointer */
2129 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2130 if (kchan
== nullptr) {
2131 /* This sould not happen... */
2132 ret
= LTTNG_ERR_FATAL
;
2136 switch (event
->type
) {
2137 case LTTNG_EVENT_ALL
:
2139 char *filter_expression_a
= nullptr;
2140 struct lttng_bytecode
*filter_a
= nullptr;
2143 * We need to duplicate filter_expression and filter,
2144 * because ownership is passed to first enable
2147 if (filter_expression
) {
2148 filter_expression_a
= strdup(filter_expression
);
2149 if (!filter_expression_a
) {
2150 ret
= LTTNG_ERR_FATAL
;
2155 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2157 free(filter_expression_a
);
2158 ret
= LTTNG_ERR_FATAL
;
2161 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2163 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2164 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2165 /* We have passed ownership */
2166 filter_expression
= nullptr;
2168 if (ret
!= LTTNG_OK
) {
2169 if (channel_created
) {
2170 /* Let's not leak a useless channel. */
2171 kernel_destroy_channel(kchan
);
2173 free(filter_expression_a
);
2177 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2178 ret
= event_kernel_enable_event(
2179 kchan
, event
, filter_expression_a
, filter_a
);
2180 /* We have passed ownership */
2181 filter_expression_a
= nullptr;
2183 if (ret
!= LTTNG_OK
) {
2188 case LTTNG_EVENT_PROBE
:
2189 case LTTNG_EVENT_USERSPACE_PROBE
:
2190 case LTTNG_EVENT_FUNCTION
:
2191 case LTTNG_EVENT_FUNCTION_ENTRY
:
2192 case LTTNG_EVENT_TRACEPOINT
:
2193 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2194 /* We have passed ownership */
2195 filter_expression
= nullptr;
2197 if (ret
!= LTTNG_OK
) {
2198 if (channel_created
) {
2199 /* Let's not leak a useless channel. */
2200 kernel_destroy_channel(kchan
);
2205 case LTTNG_EVENT_SYSCALL
:
2206 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2207 /* We have passed ownership */
2208 filter_expression
= nullptr;
2210 if (ret
!= LTTNG_OK
) {
2215 ret
= LTTNG_ERR_UNK
;
2219 kernel_wait_quiescent();
2222 case LTTNG_DOMAIN_UST
:
2224 struct ltt_ust_channel
*uchan
;
2225 struct ltt_ust_session
*usess
= session
->ust_session
;
2227 LTTNG_ASSERT(usess
);
2230 * If a non-default channel has been created in the
2231 * session, explicitely require that -c chan_name needs
2234 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2235 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2239 /* Get channel from global UST domain */
2240 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
2241 if (uchan
== nullptr) {
2242 /* Create default channel */
2243 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, usess
->buffer_type
);
2244 if (attr
== nullptr) {
2245 ret
= LTTNG_ERR_FATAL
;
2248 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2249 ret
= LTTNG_ERR_INVALID
;
2253 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2254 if (ret
!= LTTNG_OK
) {
2258 /* Get the newly created channel reference back */
2259 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2261 LTTNG_ASSERT(uchan
);
2264 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2266 * Don't allow users to add UST events to channels which
2267 * are assigned to a userspace subdomain (JUL, Log4J,
2270 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2274 if (!internal_event
) {
2276 * Ensure the event name is not reserved for internal
2279 ret
= validate_ust_event_name(event
->name
);
2281 WARN("Userspace event name %s failed validation.", event
->name
);
2282 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2287 /* At this point, the session and channel exist on the tracer */
2288 ret
= event_ust_enable_tracepoint(
2289 usess
, uchan
, event
, filter_expression
, filter
, exclusion
, internal_event
);
2290 /* We have passed ownership */
2291 filter_expression
= nullptr;
2293 exclusion
= nullptr;
2294 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2295 goto already_enabled
;
2296 } else if (ret
!= LTTNG_OK
) {
2301 case LTTNG_DOMAIN_LOG4J
:
2302 case LTTNG_DOMAIN_JUL
:
2303 case LTTNG_DOMAIN_PYTHON
:
2305 const char *default_event_name
, *default_chan_name
;
2307 struct lttng_event uevent
;
2308 struct lttng_domain tmp_dom
;
2309 struct ltt_ust_session
*usess
= session
->ust_session
;
2311 LTTNG_ASSERT(usess
);
2313 if (!agent_tracing_is_enabled()) {
2314 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2315 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2319 agt
= trace_ust_find_agent(usess
, domain
->type
);
2321 agt
= agent_create(domain
->type
);
2323 ret
= LTTNG_ERR_NOMEM
;
2326 agent_add(agt
, usess
->agents
);
2329 /* Create the default tracepoint. */
2330 memset(&uevent
, 0, sizeof(uevent
));
2331 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2332 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2333 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
2334 if (!default_event_name
) {
2335 ret
= LTTNG_ERR_FATAL
;
2338 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2339 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2342 * The domain type is changed because we are about to enable the
2343 * default channel and event for the JUL domain that are hardcoded.
2344 * This happens in the UST domain.
2346 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2347 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2349 switch (domain
->type
) {
2350 case LTTNG_DOMAIN_LOG4J
:
2351 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2353 case LTTNG_DOMAIN_JUL
:
2354 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2356 case LTTNG_DOMAIN_PYTHON
:
2357 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2360 /* The switch/case we are in makes this impossible */
2365 char *filter_expression_copy
= nullptr;
2366 struct lttng_bytecode
*filter_copy
= nullptr;
2369 const size_t filter_size
=
2370 sizeof(struct lttng_bytecode
) + filter
->len
;
2372 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2374 ret
= LTTNG_ERR_NOMEM
;
2377 memcpy(filter_copy
, filter
, filter_size
);
2379 filter_expression_copy
= strdup(filter_expression
);
2380 if (!filter_expression
) {
2381 ret
= LTTNG_ERR_NOMEM
;
2384 if (!filter_expression_copy
|| !filter_copy
) {
2385 free(filter_expression_copy
);
2391 ret
= cmd_enable_event_internal(session
,
2393 (char *) default_chan_name
,
2395 filter_expression_copy
,
2401 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2402 goto already_enabled
;
2403 } else if (ret
!= LTTNG_OK
) {
2407 /* The wild card * means that everything should be enabled. */
2408 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2409 ret
= event_agent_enable_all(usess
, agt
, event
, filter
, filter_expression
);
2411 ret
= event_agent_enable(usess
, agt
, event
, filter
, filter_expression
);
2414 filter_expression
= nullptr;
2415 if (ret
!= LTTNG_OK
) {
2422 ret
= LTTNG_ERR_UND
;
2430 free(filter_expression
);
2433 channel_attr_destroy(attr
);
2438 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2439 * We own filter, exclusion, and filter_expression.
2441 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2442 struct lttng_event
*event
,
2443 char *filter_expression
,
2444 struct lttng_event_exclusion
*exclusion
,
2445 struct lttng_bytecode
*bytecode
,
2450 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2452 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2455 * The ownership of the following parameters is transferred to
2456 * _cmd_enable_event:
2458 * - filter_expression,
2462 ret
= _cmd_enable_event(cmd_ctx
->session
,
2464 cmd_ctx
->lsm
.u
.enable
.channel_name
,
2471 filter_expression
= nullptr;
2473 exclusion
= nullptr;
2478 * Enable an event which is internal to LTTng. An internal should
2479 * never be made visible to clients and are immune to checks such as
2482 static int cmd_enable_event_internal(struct ltt_session
*session
,
2483 const struct lttng_domain
*domain
,
2485 struct lttng_event
*event
,
2486 char *filter_expression
,
2487 struct lttng_bytecode
*filter
,
2488 struct lttng_event_exclusion
*exclusion
,
2491 return _cmd_enable_event(session
,
2503 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2505 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2506 struct lttng_payload
*reply_payload
)
2508 enum lttng_error_code ret_code
;
2510 ssize_t i
, nb_events
= 0;
2511 struct lttng_event
*events
= nullptr;
2512 struct lttcomm_list_command_header reply_command_header
= {};
2513 size_t reply_command_header_offset
;
2515 assert(reply_payload
);
2517 /* Reserve space for command reply header. */
2518 reply_command_header_offset
= reply_payload
->buffer
.size
;
2519 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2520 reply_command_header_offset
+
2521 sizeof(struct lttcomm_list_command_header
));
2523 ret_code
= LTTNG_ERR_NOMEM
;
2528 case LTTNG_DOMAIN_KERNEL
:
2529 nb_events
= kernel_list_events(&events
);
2530 if (nb_events
< 0) {
2531 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2535 case LTTNG_DOMAIN_UST
:
2536 nb_events
= ust_app_list_events(&events
);
2537 if (nb_events
< 0) {
2538 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2542 case LTTNG_DOMAIN_LOG4J
:
2543 case LTTNG_DOMAIN_JUL
:
2544 case LTTNG_DOMAIN_PYTHON
:
2545 nb_events
= agent_list_events(&events
, domain
);
2546 if (nb_events
< 0) {
2547 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2552 ret_code
= LTTNG_ERR_UND
;
2556 for (i
= 0; i
< nb_events
; i
++) {
2557 ret
= lttng_event_serialize(
2558 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2560 ret_code
= LTTNG_ERR_NOMEM
;
2565 if (nb_events
> UINT32_MAX
) {
2566 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2567 ret_code
= LTTNG_ERR_OVERFLOW
;
2571 /* Update command reply header. */
2572 reply_command_header
.count
= (uint32_t) nb_events
;
2573 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2574 &reply_command_header
,
2575 sizeof(reply_command_header
));
2577 ret_code
= LTTNG_OK
;
2584 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2586 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2587 struct lttng_payload
*reply
)
2589 enum lttng_error_code ret_code
;
2591 unsigned int i
, nb_fields
;
2592 struct lttng_event_field
*fields
= nullptr;
2593 struct lttcomm_list_command_header reply_command_header
= {};
2594 size_t reply_command_header_offset
;
2598 /* Reserve space for command reply header. */
2599 reply_command_header_offset
= reply
->buffer
.size
;
2600 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2601 reply_command_header_offset
+
2602 sizeof(struct lttcomm_list_command_header
));
2604 ret_code
= LTTNG_ERR_NOMEM
;
2609 case LTTNG_DOMAIN_UST
:
2610 ret
= ust_app_list_event_fields(&fields
);
2612 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2617 case LTTNG_DOMAIN_KERNEL
:
2618 default: /* fall-through */
2619 ret_code
= LTTNG_ERR_UND
;
2625 for (i
= 0; i
< nb_fields
; i
++) {
2626 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2628 ret_code
= LTTNG_ERR_NOMEM
;
2633 if (nb_fields
> UINT32_MAX
) {
2634 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2635 ret_code
= LTTNG_ERR_OVERFLOW
;
2639 /* Update command reply header. */
2640 reply_command_header
.count
= (uint32_t) nb_fields
;
2642 memcpy(reply
->buffer
.data
+ reply_command_header_offset
,
2643 &reply_command_header
,
2644 sizeof(reply_command_header
));
2646 ret_code
= LTTNG_OK
;
2653 enum lttng_error_code
cmd_list_syscalls(struct lttng_payload
*reply_payload
)
2655 enum lttng_error_code ret_code
;
2656 ssize_t nb_events
, i
;
2658 struct lttng_event
*events
= nullptr;
2659 struct lttcomm_list_command_header reply_command_header
= {};
2660 size_t reply_command_header_offset
;
2662 assert(reply_payload
);
2664 /* Reserve space for command reply header. */
2665 reply_command_header_offset
= reply_payload
->buffer
.size
;
2666 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2667 reply_command_header_offset
+
2668 sizeof(struct lttcomm_list_command_header
));
2670 ret_code
= LTTNG_ERR_NOMEM
;
2674 nb_events
= syscall_table_list(&events
);
2675 if (nb_events
< 0) {
2676 ret_code
= (enum lttng_error_code
) - nb_events
;
2680 for (i
= 0; i
< nb_events
; i
++) {
2681 ret
= lttng_event_serialize(
2682 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2684 ret_code
= LTTNG_ERR_NOMEM
;
2689 if (nb_events
> UINT32_MAX
) {
2690 ERR("Syscall count would overflow the syscall listing command's reply");
2691 ret_code
= LTTNG_ERR_OVERFLOW
;
2695 /* Update command reply header. */
2696 reply_command_header
.count
= (uint32_t) nb_events
;
2697 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2698 &reply_command_header
,
2699 sizeof(reply_command_header
));
2701 ret_code
= LTTNG_OK
;
2708 * Command LTTNG_START_TRACE processed by the client thread.
2710 * Called with session mutex held.
2712 int cmd_start_trace(struct ltt_session
*session
)
2714 enum lttng_error_code ret
;
2715 unsigned long nb_chan
= 0;
2716 struct ltt_kernel_session
*ksession
;
2717 struct ltt_ust_session
*usess
;
2718 const bool session_rotated_after_last_stop
= session
->rotated_after_last_stop
;
2719 const bool session_cleared_after_last_stop
= session
->cleared_after_last_stop
;
2721 LTTNG_ASSERT(session
);
2723 /* Ease our life a bit ;) */
2724 ksession
= session
->kernel_session
;
2725 usess
= session
->ust_session
;
2727 /* Is the session already started? */
2728 if (session
->active
) {
2729 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2730 /* Perform nothing */
2734 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2735 !session
->current_trace_chunk
) {
2737 * A rotation was launched while the session was stopped and
2738 * it has not been completed yet. It is not possible to start
2739 * the session since starting the session here would require a
2740 * rotation from "NULL" to a new trace chunk. That rotation
2741 * would overlap with the ongoing rotation, which is not
2744 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2746 ret
= LTTNG_ERR_ROTATION_PENDING
;
2751 * Starting a session without channel is useless since after that it's not
2752 * possible to enable channel thus inform the client.
2754 if (usess
&& usess
->domain_global
.channels
) {
2755 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2758 nb_chan
+= ksession
->channel_count
;
2761 ret
= LTTNG_ERR_NO_CHANNEL
;
2765 session
->active
= true;
2766 session
->rotated_after_last_stop
= false;
2767 session
->cleared_after_last_stop
= false;
2768 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2769 if (!session
->has_been_started
) {
2770 struct lttng_trace_chunk
*trace_chunk
;
2772 DBG("Creating initial trace chunk of session \"%s\"", session
->name
);
2774 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
2776 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2779 LTTNG_ASSERT(!session
->current_trace_chunk
);
2780 ret
= (lttng_error_code
) session_set_trace_chunk(
2781 session
, trace_chunk
, nullptr);
2782 lttng_trace_chunk_put(trace_chunk
);
2784 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2788 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2791 * Rotate existing streams into the new chunk.
2792 * This is a "quiet" rotation has no client has
2793 * explicitly requested this operation.
2795 * There is also no need to wait for the rotation
2796 * to complete as it will happen immediately. No data
2797 * was produced as the session was stopped, so the
2798 * rotation should happen on reception of the command.
2800 ret
= (lttng_error_code
) cmd_rotate_session(
2801 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2802 if (ret
!= LTTNG_OK
) {
2808 /* Kernel tracing */
2809 if (ksession
!= nullptr) {
2810 DBG("Start kernel tracing session %s", session
->name
);
2811 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2812 if (ret
!= LTTNG_OK
) {
2817 /* Flag session that trace should start automatically */
2819 int int_ret
= ust_app_start_trace_all(usess
);
2822 ret
= LTTNG_ERR_UST_START_FAIL
;
2828 * Open a packet in every stream of the session to ensure that viewers
2829 * can correctly identify the boundaries of the periods during which
2830 * tracing was active for this session.
2832 ret
= session_open_packets(session
);
2833 if (ret
!= LTTNG_OK
) {
2838 * Clear the flag that indicates that a rotation was done while the
2839 * session was stopped.
2841 session
->rotated_after_last_stop
= false;
2843 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2844 int int_ret
= timer_session_rotation_schedule_timer_start(
2845 session
, session
->rotate_timer_period
);
2848 ERR("Failed to enable rotate timer");
2849 ret
= LTTNG_ERR_UNK
;
2857 if (ret
== LTTNG_OK
) {
2858 /* Flag this after a successful start. */
2859 session
->has_been_started
= true;
2861 session
->active
= false;
2862 /* Restore initial state on error. */
2863 session
->rotated_after_last_stop
= session_rotated_after_last_stop
;
2864 session
->cleared_after_last_stop
= session_cleared_after_last_stop
;
2871 * Command LTTNG_STOP_TRACE processed by the client thread.
2873 int cmd_stop_trace(struct ltt_session
*session
)
2876 struct ltt_kernel_session
*ksession
;
2877 struct ltt_ust_session
*usess
;
2879 LTTNG_ASSERT(session
);
2881 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2883 ksession
= session
->kernel_session
;
2884 usess
= session
->ust_session
;
2886 /* Session is not active. Skip everything and inform the client. */
2887 if (!session
->active
) {
2888 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2892 ret
= stop_kernel_session(ksession
);
2893 if (ret
!= LTTNG_OK
) {
2897 if (usess
&& usess
->active
) {
2898 ret
= ust_app_stop_trace_all(usess
);
2900 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2905 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2906 /* Flag inactive after a successful stop. */
2907 session
->active
= false;
2915 * Set the base_path of the session only if subdir of a control uris is set.
2916 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2919 set_session_base_path_from_uris(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2924 for (i
= 0; i
< nb_uri
; i
++) {
2925 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
|| uris
[i
].subdir
[0] == '\0') {
2926 /* Not interested in these URIs */
2930 if (session
->base_path
!= nullptr) {
2931 free(session
->base_path
);
2932 session
->base_path
= nullptr;
2935 /* Set session base_path */
2936 session
->base_path
= strdup(uris
[i
].subdir
);
2937 if (!session
->base_path
) {
2938 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2941 ret
= LTTNG_ERR_NOMEM
;
2944 DBG2("Setting base path \"%s\" for session \"%s\"",
2954 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2956 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2959 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2960 struct ltt_ust_session
*usess
= session
->ust_session
;
2962 LTTNG_ASSERT(session
);
2964 LTTNG_ASSERT(nb_uri
> 0);
2966 /* Can't set consumer URI if the session is active. */
2967 if (session
->active
) {
2968 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2973 * Set the session base path if any. This is done inside
2974 * cmd_set_consumer_uri to preserve backward compatibility of the
2975 * previous session creation api vs the session descriptor api.
2977 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2978 if (ret
!= LTTNG_OK
) {
2982 /* Set the "global" consumer URIs */
2983 for (i
= 0; i
< nb_uri
; i
++) {
2984 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
], LTTNG_DOMAIN_NONE
);
2985 if (ret
!= LTTNG_OK
) {
2990 /* Set UST session URIs */
2991 if (session
->ust_session
) {
2992 for (i
= 0; i
< nb_uri
; i
++) {
2993 ret
= add_uri_to_consumer(session
,
2994 session
->ust_session
->consumer
,
2997 if (ret
!= LTTNG_OK
) {
3003 /* Set kernel session URIs */
3004 if (session
->kernel_session
) {
3005 for (i
= 0; i
< nb_uri
; i
++) {
3006 ret
= add_uri_to_consumer(session
,
3007 session
->kernel_session
->consumer
,
3009 LTTNG_DOMAIN_KERNEL
);
3010 if (ret
!= LTTNG_OK
) {
3017 * Make sure to set the session in output mode after we set URI since a
3018 * session can be created without URL (thus flagged in no output mode).
3020 session
->output_traces
= 1;
3022 ksess
->output_traces
= 1;
3026 usess
->output_traces
= 1;
3036 static enum lttng_error_code
3037 set_session_output_from_descriptor(struct ltt_session
*session
,
3038 const struct lttng_session_descriptor
*descriptor
)
3041 enum lttng_error_code ret_code
= LTTNG_OK
;
3042 enum lttng_session_descriptor_type session_type
=
3043 lttng_session_descriptor_get_type(descriptor
);
3044 enum lttng_session_descriptor_output_type output_type
=
3045 lttng_session_descriptor_get_output_type(descriptor
);
3046 struct lttng_uri uris
[2] = {};
3047 size_t uri_count
= 0;
3049 switch (output_type
) {
3050 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3052 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3053 lttng_session_descriptor_get_local_output_uri(descriptor
, &uris
[0]);
3056 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3057 lttng_session_descriptor_get_network_output_uris(descriptor
, &uris
[0], &uris
[1]);
3061 ret_code
= LTTNG_ERR_INVALID
;
3065 switch (session_type
) {
3066 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3068 struct snapshot_output
*new_output
= nullptr;
3070 new_output
= snapshot_output_alloc();
3072 ret_code
= LTTNG_ERR_NOMEM
;
3076 ret
= snapshot_output_init_with_uri(session
,
3077 DEFAULT_SNAPSHOT_MAX_SIZE
,
3083 &session
->snapshot
);
3085 ret_code
= (ret
== -ENOMEM
) ? LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3086 snapshot_output_destroy(new_output
);
3089 snapshot_add_output(&session
->snapshot
, new_output
);
3092 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3093 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3095 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3099 ret_code
= LTTNG_ERR_INVALID
;
3106 static enum lttng_error_code
3107 cmd_create_session_from_descriptor(struct lttng_session_descriptor
*descriptor
,
3108 const lttng_sock_cred
*creds
,
3109 const char *home_path
)
3112 enum lttng_error_code ret_code
;
3113 const char *session_name
;
3114 struct ltt_session
*new_session
= nullptr;
3115 enum lttng_session_descriptor_status descriptor_status
;
3117 session_lock_list();
3119 if (*home_path
!= '/') {
3120 ERR("Home path provided by client is not absolute");
3121 ret_code
= LTTNG_ERR_INVALID
;
3126 descriptor_status
= lttng_session_descriptor_get_session_name(descriptor
, &session_name
);
3127 switch (descriptor_status
) {
3128 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3130 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3131 session_name
= nullptr;
3134 ret_code
= LTTNG_ERR_INVALID
;
3138 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, &new_session
);
3139 if (ret_code
!= LTTNG_OK
) {
3143 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3148 if (ret_code
!= LTTNG_OK
) {
3152 /* Announce the session's destruction to the notification thread when it is destroyed. */
3153 ret
= session_add_destroy_notifier(
3155 [](const struct ltt_session
*session
, void *user_data
__attribute__((unused
))) {
3156 (void) notification_thread_command_remove_session(
3157 the_notification_thread_handle
, session
->id
);
3161 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3163 ret
= LTTNG_ERR_NOMEM
;
3167 if (!session_name
) {
3168 ret
= lttng_session_descriptor_set_session_name(descriptor
, new_session
->name
);
3170 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3175 if (!lttng_session_descriptor_is_output_destination_initialized(descriptor
)) {
3177 * Only include the session's creation time in the output
3178 * destination if the name of the session itself was
3179 * not auto-generated.
3181 ret_code
= lttng_session_descriptor_set_default_output(
3183 session_name
? &new_session
->creation_time
: nullptr,
3185 if (ret_code
!= LTTNG_OK
) {
3189 new_session
->has_user_specified_directory
=
3190 lttng_session_descriptor_has_output_directory(descriptor
);
3193 switch (lttng_session_descriptor_get_type(descriptor
)) {
3194 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3195 new_session
->snapshot_mode
= 1;
3197 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3198 new_session
->live_timer
=
3199 lttng_session_descriptor_live_get_timer_interval(descriptor
);
3205 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3206 if (ret_code
!= LTTNG_OK
) {
3209 new_session
->consumer
->enabled
= true;
3210 ret_code
= LTTNG_OK
;
3212 /* Release reference provided by the session_create function. */
3213 session_put(new_session
);
3214 if (ret_code
!= LTTNG_OK
&& new_session
) {
3215 /* Release the global reference on error. */
3216 session_destroy(new_session
);
3218 session_unlock_list();
3222 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
,
3224 struct lttng_session_descriptor
**return_descriptor
)
3227 size_t payload_size
;
3228 struct lttng_dynamic_buffer payload
;
3229 struct lttng_buffer_view home_dir_view
;
3230 struct lttng_buffer_view session_descriptor_view
;
3231 struct lttng_session_descriptor
*session_descriptor
= nullptr;
3232 enum lttng_error_code ret_code
;
3234 lttng_dynamic_buffer_init(&payload
);
3235 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>= LTTNG_PATH_MAX
) {
3236 ret_code
= LTTNG_ERR_INVALID
;
3239 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3240 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3241 ret_code
= LTTNG_ERR_INVALID
;
3245 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3246 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3247 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3249 ret_code
= LTTNG_ERR_NOMEM
;
3253 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3255 ERR("Reception of session descriptor failed, aborting.");
3256 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3260 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3261 &payload
, 0, cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3262 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3263 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3264 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3265 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3269 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3271 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3272 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3273 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3274 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3275 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3279 ret
= lttng_session_descriptor_create_from_buffer(&session_descriptor_view
,
3280 &session_descriptor
);
3282 ERR("Failed to create session descriptor from payload of \"create session\" command");
3283 ret_code
= LTTNG_ERR_INVALID
;
3288 * Sets the descriptor's auto-generated properties (name, output) if
3291 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3293 home_dir_view
.size
? home_dir_view
.data
:
3295 if (ret_code
!= LTTNG_OK
) {
3299 ret_code
= LTTNG_OK
;
3300 *return_descriptor
= session_descriptor
;
3301 session_descriptor
= nullptr;
3303 lttng_dynamic_buffer_reset(&payload
);
3304 lttng_session_descriptor_destroy(session_descriptor
);
3308 static void cmd_destroy_session_reply(const struct ltt_session
*session
, void *_reply_context
)
3312 const struct cmd_destroy_session_reply_context
*reply_context
=
3313 (cmd_destroy_session_reply_context
*) _reply_context
;
3314 struct lttng_dynamic_buffer payload
;
3315 struct lttcomm_session_destroy_command_header cmd_header
;
3316 struct lttng_trace_archive_location
*location
= nullptr;
3317 struct lttcomm_lttng_msg llm
= {
3318 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION
,
3319 .ret_code
= reply_context
->destruction_status
,
3321 .cmd_header_size
= sizeof(struct lttcomm_session_destroy_command_header
),
3325 size_t payload_size_before_location
;
3327 lttng_dynamic_buffer_init(&payload
);
3329 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3331 ERR("Failed to append session destruction message");
3335 cmd_header
.rotation_state
= (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3336 session
->rotation_state
:
3337 LTTNG_ROTATION_STATE_NO_ROTATION
);
3338 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
, sizeof(cmd_header
));
3340 ERR("Failed to append session destruction command header");
3344 if (!reply_context
->implicit_rotation_on_destroy
) {
3345 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3349 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3350 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3355 location
= session_get_trace_archive_location(session
);
3357 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3362 payload_size_before_location
= payload
.size
;
3363 comm_ret
= lttng_trace_archive_location_serialize(location
, &payload
);
3364 lttng_trace_archive_location_put(location
);
3366 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3370 /* Update the message to indicate the location's length. */
3371 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3372 payload
.size
- payload_size_before_location
;
3374 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
, payload
.data
, payload
.size
);
3375 if (comm_ret
!= (ssize_t
) payload
.size
) {
3376 ERR("Failed to send result of the destruction of session \"%s\" to client",
3380 ret
= close(reply_context
->reply_sock_fd
);
3382 PERROR("Failed to close client socket in deferred session destroy reply");
3384 lttng_dynamic_buffer_reset(&payload
);
3385 free(_reply_context
);
3389 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3391 * Called with session lock held.
3393 int cmd_destroy_session(struct ltt_session
*session
,
3397 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3398 struct cmd_destroy_session_reply_context
*reply_context
= nullptr;
3401 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3402 if (!reply_context
) {
3403 ret
= LTTNG_ERR_NOMEM
;
3407 reply_context
->reply_sock_fd
= *sock_fd
;
3411 LTTNG_ASSERT(session
);
3413 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3414 if (session
->active
) {
3415 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3417 ret
= cmd_stop_trace(session
);
3418 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3419 /* Carry on with the destruction of the session. */
3420 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3422 lttng_strerror(-ret
));
3423 destruction_last_error
= (lttng_error_code
) ret
;
3427 if (session
->rotation_schedule_timer_enabled
) {
3428 if (timer_session_rotation_schedule_timer_stop(session
)) {
3429 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3431 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3435 if (session
->rotate_size
) {
3437 the_rotation_thread_handle
->unsubscribe_session_consumed_size_rotation(
3439 } catch (std::exception
& e
) {
3440 /* Continue the destruction of the session anyway. */
3441 ERR("Failed to unsubscribe rotation thread notification channel from consumed size condition during session destruction: %s",
3445 session
->rotate_size
= 0;
3448 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3450 * Perform a last rotation on destruction if rotations have
3451 * occurred during the session's lifetime.
3453 ret
= cmd_rotate_session(
3454 session
, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3455 if (ret
!= LTTNG_OK
) {
3456 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3458 lttng_strerror(-ret
));
3459 destruction_last_error
= (lttng_error_code
) -ret
;
3461 if (reply_context
) {
3462 reply_context
->implicit_rotation_on_destroy
= true;
3464 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3466 * The user has not triggered a session rotation. However, to
3467 * ensure all data has been consumed, the session is rotated
3468 * to a 'null' trace chunk before it is destroyed.
3470 * This is a "quiet" rotation meaning that no notification is
3471 * emitted and no renaming of the current trace chunk takes
3474 ret
= cmd_rotate_session(
3475 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3477 * Rotation operations may not be supported by the kernel
3478 * tracer. Hence, do not consider this implicit rotation as
3479 * a session destruction error. The library has already stopped
3480 * the session and waited for pending data; there is nothing
3481 * left to do but complete the destruction of the session.
3483 if (ret
!= LTTNG_OK
&& ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3484 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3486 lttng_strerror(ret
));
3487 destruction_last_error
= (lttng_error_code
) -ret
;
3491 if (session
->shm_path
[0]) {
3493 * When a session is created with an explicit shm_path,
3494 * the consumer daemon will create its shared memory files
3495 * at that location and will *not* unlink them. This is normal
3496 * as the intention of that feature is to make it possible
3497 * to retrieve the content of those files should a crash occur.
3499 * To ensure the content of those files can be used, the
3500 * sessiond daemon will replicate the content of the metadata
3501 * cache in a metadata file.
3503 * On clean-up, it is expected that the consumer daemon will
3504 * unlink the shared memory files and that the session daemon
3505 * will unlink the metadata file. Then, the session's directory
3506 * in the shm path can be removed.
3508 * Unfortunately, a flaw in the design of the sessiond's and
3509 * consumerd's tear down of channels makes it impossible to
3510 * determine when the sessiond _and_ the consumerd have both
3511 * destroyed their representation of a channel. For one, the
3512 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3513 * callbacks in both daemons.
3515 * However, it is also impossible for the sessiond to know when
3516 * the consumer daemon is done destroying its channel(s) since
3517 * it occurs as a reaction to the closing of the channel's file
3518 * descriptor. There is no resulting communication initiated
3519 * from the consumerd to the sessiond to confirm that the
3520 * operation is completed (and was successful).
3522 * Until this is all fixed, the session daemon checks for the
3523 * removal of the session's shm path which makes it possible
3524 * to safely advertise a session as having been destroyed.
3526 * Prior to this fix, it was not possible to reliably save
3527 * a session making use of the --shm-path option, destroy it,
3528 * and load it again. This is because the creation of the
3529 * session would fail upon seeing the session's shm path
3530 * already in existence.
3532 * Note that none of the error paths in the check for the
3533 * directory's existence return an error. This is normal
3534 * as there isn't much that can be done. The session will
3535 * be destroyed properly, except that we can't offer the
3536 * guarantee that the same session can be re-created.
3538 current_completion_handler
= &destroy_completion_handler
.handler
;
3539 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3541 sizeof(destroy_completion_handler
.shm_path
));
3546 * The session is destroyed. However, note that the command context
3547 * still holds a reference to the session, thus delaying its destruction
3548 * _at least_ up to the point when that reference is released.
3550 session_destroy(session
);
3551 if (reply_context
) {
3552 reply_context
->destruction_status
= destruction_last_error
;
3553 ret
= session_add_destroy_notifier(
3554 session
, cmd_destroy_session_reply
, (void *) reply_context
);
3556 ret
= LTTNG_ERR_FATAL
;
3568 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3570 int cmd_register_consumer(struct ltt_session
*session
,
3571 enum lttng_domain_type domain
,
3572 const char *sock_path
,
3573 struct consumer_data
*cdata
)
3576 struct consumer_socket
*socket
= nullptr;
3578 LTTNG_ASSERT(session
);
3579 LTTNG_ASSERT(cdata
);
3580 LTTNG_ASSERT(sock_path
);
3583 case LTTNG_DOMAIN_KERNEL
:
3585 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3587 LTTNG_ASSERT(ksess
);
3589 /* Can't register a consumer if there is already one */
3590 if (ksess
->consumer_fds_sent
!= 0) {
3591 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3595 sock
= lttcomm_connect_unix_sock(sock_path
);
3597 ret
= LTTNG_ERR_CONNECT_FAIL
;
3600 cdata
->cmd_sock
= sock
;
3602 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3603 if (socket
== nullptr) {
3606 PERROR("close register consumer");
3608 cdata
->cmd_sock
= -1;
3609 ret
= LTTNG_ERR_FATAL
;
3613 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3614 if (socket
->lock
== nullptr) {
3615 PERROR("zmalloc pthread mutex");
3616 ret
= LTTNG_ERR_FATAL
;
3620 pthread_mutex_init(socket
->lock
, nullptr);
3621 socket
->registered
= 1;
3623 lttng::urcu::read_lock_guard read_lock
;
3624 consumer_add_socket(socket
, ksess
->consumer
);
3626 pthread_mutex_lock(&cdata
->pid_mutex
);
3628 pthread_mutex_unlock(&cdata
->pid_mutex
);
3633 /* TODO: Userspace tracing */
3634 ret
= LTTNG_ERR_UND
;
3642 consumer_destroy_socket(socket
);
3648 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3650 ssize_t
cmd_list_domains(struct ltt_session
*session
, struct lttng_domain
**domains
)
3655 struct lttng_ht_iter iter
;
3657 if (session
->kernel_session
!= nullptr) {
3658 DBG3("Listing domains found kernel domain");
3662 if (session
->ust_session
!= nullptr) {
3663 DBG3("Listing domains found UST global domain");
3666 lttng::urcu::read_lock_guard read_lock
;
3668 cds_lfht_for_each_entry (
3669 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3670 if (agt
->being_used
) {
3680 *domains
= calloc
<lttng_domain
>(nb_dom
);
3681 if (*domains
== nullptr) {
3682 ret
= LTTNG_ERR_FATAL
;
3686 if (session
->kernel_session
!= nullptr) {
3687 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3689 /* Kernel session buffer type is always GLOBAL */
3690 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3695 if (session
->ust_session
!= nullptr) {
3696 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3697 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3701 lttng::urcu::read_lock_guard read_lock
;
3703 cds_lfht_for_each_entry (
3704 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3705 if (agt
->being_used
) {
3706 (*domains
)[index
].type
= agt
->domain
;
3707 (*domains
)[index
].buf_type
=
3708 session
->ust_session
->buffer_type
;
3718 /* Return negative value to differentiate return code */
3723 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3725 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3726 struct ltt_session
*session
,
3727 struct lttng_payload
*payload
)
3731 struct lttcomm_list_command_header cmd_header
= {};
3732 size_t cmd_header_offset
;
3733 enum lttng_error_code ret_code
;
3738 DBG("Listing channels for session %s", session
->name
);
3740 cmd_header_offset
= payload
->buffer
.size
;
3742 /* Reserve space for command reply header. */
3743 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3744 cmd_header_offset
+ sizeof(cmd_header
));
3746 ret_code
= LTTNG_ERR_NOMEM
;
3751 case LTTNG_DOMAIN_KERNEL
:
3753 /* Kernel channels */
3754 struct ltt_kernel_channel
*kchan
;
3755 if (session
->kernel_session
!= nullptr) {
3756 cds_list_for_each_entry (
3757 kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
3758 uint64_t discarded_events
, lost_packets
;
3759 struct lttng_channel_extended
*extended
;
3761 extended
= (struct lttng_channel_extended
*)
3762 kchan
->channel
->attr
.extended
.ptr
;
3764 ret
= get_kernel_runtime_stats(
3765 session
, kchan
, &discarded_events
, &lost_packets
);
3767 ret_code
= LTTNG_ERR_UNK
;
3772 * Update the discarded_events and lost_packets
3773 * count for the channel
3775 extended
->discarded_events
= discarded_events
;
3776 extended
->lost_packets
= lost_packets
;
3778 ret
= lttng_channel_serialize(kchan
->channel
, &payload
->buffer
);
3780 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3781 kchan
->channel
->name
);
3782 ret_code
= LTTNG_ERR_UNK
;
3791 case LTTNG_DOMAIN_UST
:
3793 struct lttng_ht_iter iter
;
3794 struct ltt_ust_channel
*uchan
;
3797 lttng::urcu::read_lock_guard read_lock
;
3799 cds_lfht_for_each_entry (session
->ust_session
->domain_global
.channels
->ht
,
3803 uint64_t discarded_events
= 0, lost_packets
= 0;
3804 struct lttng_channel
*channel
= nullptr;
3805 struct lttng_channel_extended
*extended
;
3807 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3809 ret_code
= LTTNG_ERR_NOMEM
;
3813 extended
= (struct lttng_channel_extended
*)
3814 channel
->attr
.extended
.ptr
;
3816 ret
= get_ust_runtime_stats(
3817 session
, uchan
, &discarded_events
, &lost_packets
);
3819 lttng_channel_destroy(channel
);
3820 ret_code
= LTTNG_ERR_UNK
;
3824 extended
->discarded_events
= discarded_events
;
3825 extended
->lost_packets
= lost_packets
;
3827 ret
= lttng_channel_serialize(channel
, &payload
->buffer
);
3829 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3831 lttng_channel_destroy(channel
);
3832 ret_code
= LTTNG_ERR_UNK
;
3836 lttng_channel_destroy(channel
);
3847 if (i
> UINT32_MAX
) {
3848 ERR("Channel count would overflow the channel listing command's reply");
3849 ret_code
= LTTNG_ERR_OVERFLOW
;
3853 /* Update command reply header. */
3854 cmd_header
.count
= (uint32_t) i
;
3855 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
, sizeof(cmd_header
));
3856 ret_code
= LTTNG_OK
;
3863 * Command LTTNG_LIST_EVENTS processed by the client thread.
3865 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3866 struct ltt_session
*session
,
3868 struct lttng_payload
*reply_payload
)
3870 int buffer_resize_ret
;
3871 enum lttng_error_code ret_code
= LTTNG_OK
;
3872 struct lttcomm_list_command_header reply_command_header
= {};
3873 size_t reply_command_header_offset
;
3874 unsigned int nb_events
= 0;
3876 assert(reply_payload
);
3878 /* Reserve space for command reply header. */
3879 reply_command_header_offset
= reply_payload
->buffer
.size
;
3880 buffer_resize_ret
= lttng_dynamic_buffer_set_size(
3881 &reply_payload
->buffer
,
3882 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
3883 if (buffer_resize_ret
) {
3884 ret_code
= LTTNG_ERR_NOMEM
;
3889 case LTTNG_DOMAIN_KERNEL
:
3890 if (session
->kernel_session
!= nullptr) {
3891 ret_code
= list_lttng_kernel_events(
3892 channel_name
, session
->kernel_session
, reply_payload
, &nb_events
);
3896 case LTTNG_DOMAIN_UST
:
3898 if (session
->ust_session
!= nullptr) {
3900 list_lttng_ust_global_events(channel_name
,
3901 &session
->ust_session
->domain_global
,
3908 case LTTNG_DOMAIN_LOG4J
:
3909 case LTTNG_DOMAIN_JUL
:
3910 case LTTNG_DOMAIN_PYTHON
:
3911 if (session
->ust_session
) {
3912 struct lttng_ht_iter iter
;
3915 lttng::urcu::read_lock_guard read_lock
;
3917 cds_lfht_for_each_entry (
3918 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3919 if (agt
->domain
== domain
) {
3920 ret_code
= list_lttng_agent_events(
3921 agt
, reply_payload
, &nb_events
);
3928 ret_code
= LTTNG_ERR_UND
;
3932 if (nb_events
> UINT32_MAX
) {
3933 ret_code
= LTTNG_ERR_OVERFLOW
;
3937 /* Update command reply header. */
3938 reply_command_header
.count
= (uint32_t) nb_events
;
3939 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
3940 &reply_command_header
,
3941 sizeof(reply_command_header
));
3948 * Using the session list, filled a lttng_session array to send back to the
3949 * client for session listing.
3951 * The session list lock MUST be acquired before calling this function. Use
3952 * session_lock_list() and session_unlock_list().
3954 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3955 size_t session_count
,
3961 struct ltt_session
*session
;
3962 struct ltt_session_list
*list
= session_get_list();
3963 struct lttng_session_extended
*extended
= (typeof(extended
)) (&sessions
[session_count
]);
3965 DBG("Getting all available session for UID %d GID %d", uid
, gid
);
3967 * Iterate over session list and append data after the control struct in
3970 cds_list_for_each_entry (session
, &list
->head
, list
) {
3971 if (!session_get(session
)) {
3975 * Only list the sessions the user can control.
3977 if (!session_access_ok(session
, uid
) || session
->destroyed
) {
3978 session_put(session
);
3982 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3983 struct ltt_ust_session
*usess
= session
->ust_session
;
3985 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3986 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3987 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3988 ret
= build_network_session_path(
3989 sessions
[i
].path
, sizeof(sessions
[i
].path
), session
);
3991 ret
= snprintf(sessions
[i
].path
,
3992 sizeof(sessions
[i
].path
),
3994 session
->consumer
->dst
.session_root_path
);
3997 PERROR("snprintf session path");
3998 session_put(session
);
4002 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
4003 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
4004 sessions
[i
].enabled
= session
->active
;
4005 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
4006 sessions
[i
].live_timer_interval
= session
->live_timer
;
4007 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
4008 extended
[i
].creation_time
.is_set
= 1;
4010 session_put(session
);
4015 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4016 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4018 int cmd_data_pending(struct ltt_session
*session
)
4021 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4022 struct ltt_ust_session
*usess
= session
->ust_session
;
4024 LTTNG_ASSERT(session
);
4026 DBG("Data pending for session %s", session
->name
);
4028 /* Session MUST be stopped to ask for data availability. */
4029 if (session
->active
) {
4030 ret
= LTTNG_ERR_SESSION_STARTED
;
4034 * If stopped, just make sure we've started before else the above call
4035 * will always send that there is data pending.
4037 * The consumer assumes that when the data pending command is received,
4038 * the trace has been started before or else no output data is written
4039 * by the streams which is a condition for data pending. So, this is
4040 * *VERY* important that we don't ask the consumer before a start
4043 if (!session
->has_been_started
) {
4049 /* A rotation is still pending, we have to wait. */
4050 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4051 DBG("Rotate still pending for session %s", session
->name
);
4056 if (ksess
&& ksess
->consumer
) {
4057 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4059 /* Data is still being extracted for the kernel. */
4064 if (usess
&& usess
->consumer
) {
4065 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4067 /* Data is still being extracted for the kernel. */
4072 /* Data is ready to be read by a viewer */
4080 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4082 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4084 int cmd_snapshot_add_output(struct ltt_session
*session
,
4085 const struct lttng_snapshot_output
*output
,
4089 struct snapshot_output
*new_output
;
4091 LTTNG_ASSERT(session
);
4092 LTTNG_ASSERT(output
);
4094 DBG("Cmd snapshot add output for session %s", session
->name
);
4097 * Can't create an output if the session is not set in no-output mode.
4099 if (session
->output_traces
) {
4100 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4104 if (session
->has_non_mmap_channel
) {
4105 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4109 /* Only one output is allowed until we have the "tee" feature. */
4110 if (session
->snapshot
.nb_output
== 1) {
4111 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4115 new_output
= snapshot_output_alloc();
4117 ret
= LTTNG_ERR_NOMEM
;
4121 ret
= snapshot_output_init(session
,
4128 &session
->snapshot
);
4130 if (ret
== -ENOMEM
) {
4131 ret
= LTTNG_ERR_NOMEM
;
4133 ret
= LTTNG_ERR_INVALID
;
4138 snapshot_add_output(&session
->snapshot
, new_output
);
4140 *id
= new_output
->id
;
4146 snapshot_output_destroy(new_output
);
4152 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4154 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4156 int cmd_snapshot_del_output(struct ltt_session
*session
, const struct lttng_snapshot_output
*output
)
4159 struct snapshot_output
*sout
= nullptr;
4161 LTTNG_ASSERT(session
);
4162 LTTNG_ASSERT(output
);
4164 lttng::urcu::read_lock_guard read_lock
;
4167 * Permission denied to create an output if the session is not
4168 * set in no output mode.
4170 if (session
->output_traces
) {
4171 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4176 DBG("Cmd snapshot del output id %" PRIu32
" for session %s",
4179 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4180 } else if (*output
->name
!= '\0') {
4181 DBG("Cmd snapshot del output name %s for session %s", output
->name
, session
->name
);
4182 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4185 ret
= LTTNG_ERR_INVALID
;
4189 snapshot_delete_output(&session
->snapshot
, sout
);
4190 snapshot_output_destroy(sout
);
4198 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4200 * If no output is available, outputs is untouched and 0 is returned.
4202 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4204 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4205 struct lttng_snapshot_output
**outputs
)
4208 struct lttng_snapshot_output
*list
= nullptr;
4209 struct lttng_ht_iter iter
;
4210 struct snapshot_output
*output
;
4212 LTTNG_ASSERT(session
);
4213 LTTNG_ASSERT(outputs
);
4215 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4218 * Permission denied to create an output if the session is not
4219 * set in no output mode.
4221 if (session
->output_traces
) {
4222 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4226 if (session
->snapshot
.nb_output
== 0) {
4231 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4233 ret
= -LTTNG_ERR_NOMEM
;
4237 /* Copy list from session to the new list object. */
4239 lttng::urcu::read_lock_guard read_lock
;
4241 cds_lfht_for_each_entry (
4242 session
->snapshot
.output_ht
->ht
, &iter
.iter
, output
, node
.node
) {
4243 LTTNG_ASSERT(output
->consumer
);
4244 list
[idx
].id
= output
->id
;
4245 list
[idx
].max_size
= output
->max_size
;
4246 if (lttng_strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
))) {
4247 ret
= -LTTNG_ERR_INVALID
;
4251 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4252 if (lttng_strncpy(list
[idx
].ctrl_url
,
4253 output
->consumer
->dst
.session_root_path
,
4254 sizeof(list
[idx
].ctrl_url
))) {
4255 ret
= -LTTNG_ERR_INVALID
;
4260 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4262 sizeof(list
[idx
].ctrl_url
));
4264 ret
= -LTTNG_ERR_NOMEM
;
4269 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4271 sizeof(list
[idx
].data_url
));
4273 ret
= -LTTNG_ERR_NOMEM
;
4284 ret
= session
->snapshot
.nb_output
;
4292 * Check if we can regenerate the metadata for this session.
4293 * Only kernel, UST per-uid and non-live sessions are supported.
4295 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4297 static int check_regenerate_metadata_support(struct ltt_session
*session
)
4301 LTTNG_ASSERT(session
);
4303 if (session
->live_timer
!= 0) {
4304 ret
= LTTNG_ERR_LIVE_SESSION
;
4307 if (!session
->active
) {
4308 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4311 if (session
->ust_session
) {
4312 switch (session
->ust_session
->buffer_type
) {
4313 case LTTNG_BUFFER_PER_UID
:
4315 case LTTNG_BUFFER_PER_PID
:
4316 ret
= LTTNG_ERR_PER_PID_SESSION
;
4320 ret
= LTTNG_ERR_UNK
;
4324 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4325 session
->consumer
->relay_minor_version
< 8) {
4326 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4336 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4338 * Ask the consumer to truncate the existing metadata file(s) and
4339 * then regenerate the metadata. Live and per-pid sessions are not
4340 * supported and return an error.
4342 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4344 int cmd_regenerate_metadata(struct ltt_session
*session
)
4348 LTTNG_ASSERT(session
);
4350 ret
= check_regenerate_metadata_support(session
);
4355 if (session
->kernel_session
) {
4356 ret
= kernctl_session_regenerate_metadata(session
->kernel_session
->fd
);
4358 ERR("Failed to regenerate the kernel metadata");
4363 if (session
->ust_session
) {
4364 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4366 ERR("Failed to regenerate the UST metadata");
4370 DBG("Cmd metadata regenerate for session %s", session
->name
);
4378 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4380 * Ask the tracer to regenerate a new statedump.
4382 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4384 int cmd_regenerate_statedump(struct ltt_session
*session
)
4388 LTTNG_ASSERT(session
);
4390 if (!session
->active
) {
4391 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4395 if (session
->kernel_session
) {
4396 ret
= kernctl_session_regenerate_statedump(session
->kernel_session
->fd
);
4398 * Currently, the statedump in kernel can only fail if out
4402 if (ret
== -ENOMEM
) {
4403 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4405 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4407 ERR("Failed to regenerate the kernel statedump");
4412 if (session
->ust_session
) {
4413 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4415 * Currently, the statedump in UST always returns 0.
4418 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4419 ERR("Failed to regenerate the UST statedump");
4423 DBG("Cmd regenerate statedump for session %s", session
->name
);
4430 static enum lttng_error_code
4431 synchronize_tracer_notifier_register(struct notification_thread_handle
*notification_thread
,
4432 struct lttng_trigger
*trigger
,
4433 const struct lttng_credentials
*cmd_creds
)
4435 enum lttng_error_code ret_code
;
4436 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4437 const char *trigger_name
;
4438 uid_t trigger_owner
;
4439 enum lttng_trigger_status trigger_status
;
4440 const enum lttng_domain_type trigger_domain
=
4441 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4443 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4444 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4446 LTTNG_ASSERT(condition
);
4447 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4448 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4450 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4451 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4453 session_lock_list();
4454 switch (trigger_domain
) {
4455 case LTTNG_DOMAIN_KERNEL
:
4457 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4458 if (ret_code
!= LTTNG_OK
) {
4459 enum lttng_error_code notif_thread_unregister_ret
;
4461 notif_thread_unregister_ret
=
4462 notification_thread_command_unregister_trigger(notification_thread
,
4465 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4466 /* Return the original error code. */
4467 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4469 (int) trigger_owner
,
4475 case LTTNG_DOMAIN_UST
:
4476 ust_app_global_update_all_event_notifier_rules();
4478 case LTTNG_DOMAIN_JUL
:
4479 case LTTNG_DOMAIN_LOG4J
:
4480 case LTTNG_DOMAIN_PYTHON
:
4482 /* Agent domains. */
4483 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4486 agt
= agent_create(trigger_domain
);
4488 ret_code
= LTTNG_ERR_NOMEM
;
4489 goto end_unlock_session_list
;
4492 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4495 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4496 if (ret_code
!= LTTNG_OK
) {
4497 goto end_unlock_session_list
;
4502 case LTTNG_DOMAIN_NONE
:
4507 ret_code
= LTTNG_OK
;
4508 end_unlock_session_list
:
4509 session_unlock_list();
4513 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4514 struct lttng_trigger
*trigger
,
4515 bool is_trigger_anonymous
,
4516 struct notification_thread_handle
*notification_thread
,
4517 struct lttng_trigger
**return_trigger
)
4519 enum lttng_error_code ret_code
;
4520 const char *trigger_name
;
4521 uid_t trigger_owner
;
4522 enum lttng_trigger_status trigger_status
;
4524 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4525 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4527 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4528 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4530 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4532 (int) trigger_owner
,
4533 (int) lttng_credentials_get_uid(cmd_creds
));
4536 * Validate the trigger credentials against the command credentials.
4537 * Only the root user can register a trigger with non-matching
4540 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4541 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4542 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4544 (int) trigger_owner
,
4545 (int) lttng_credentials_get_uid(cmd_creds
));
4546 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4552 * The bytecode generation also serves as a validation step for the
4553 * bytecode expressions.
4555 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4556 if (ret_code
!= LTTNG_OK
) {
4557 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4559 (int) trigger_owner
,
4565 * A reference to the trigger is acquired by the notification thread.
4566 * It is safe to return the same trigger to the caller since it the
4567 * other user holds a reference.
4569 * The trigger is modified during the execution of the
4570 * "register trigger" command. However, by the time the command returns,
4571 * it is safe to use without any locking as its properties are
4574 ret_code
= notification_thread_command_register_trigger(
4575 notification_thread
, trigger
, is_trigger_anonymous
);
4576 if (ret_code
!= LTTNG_OK
) {
4577 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4579 (int) trigger_owner
,
4584 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4585 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4588 * Synchronize tracers if the trigger adds an event notifier.
4590 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4591 ret_code
= synchronize_tracer_notifier_register(
4592 notification_thread
, trigger
, cmd_creds
);
4593 if (ret_code
!= LTTNG_OK
) {
4594 ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code
));
4600 * Return an updated trigger to the client.
4602 * Since a modified version of the same trigger is returned, acquire a
4603 * reference to the trigger so the caller doesn't have to care if those
4604 * are distinct instances or not.
4606 if (ret_code
== LTTNG_OK
) {
4607 lttng_trigger_get(trigger
);
4608 *return_trigger
= trigger
;
4609 /* Ownership of trigger was transferred to caller. */
4616 static enum lttng_error_code
4617 synchronize_tracer_notifier_unregister(const struct lttng_trigger
*trigger
)
4619 enum lttng_error_code ret_code
;
4620 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4621 const enum lttng_domain_type trigger_domain
=
4622 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4624 LTTNG_ASSERT(condition
);
4625 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4626 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4628 session_lock_list();
4629 switch (trigger_domain
) {
4630 case LTTNG_DOMAIN_KERNEL
:
4631 ret_code
= kernel_unregister_event_notifier(trigger
);
4632 if (ret_code
!= LTTNG_OK
) {
4633 goto end_unlock_session_list
;
4637 case LTTNG_DOMAIN_UST
:
4638 ust_app_global_update_all_event_notifier_rules();
4640 case LTTNG_DOMAIN_JUL
:
4641 case LTTNG_DOMAIN_LOG4J
:
4642 case LTTNG_DOMAIN_PYTHON
:
4644 /* Agent domains. */
4645 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4648 * This trigger was never registered in the first place. Calling
4649 * this function under those circumstances is an internal error.
4652 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4653 if (ret_code
!= LTTNG_OK
) {
4654 goto end_unlock_session_list
;
4659 case LTTNG_DOMAIN_NONE
:
4664 ret_code
= LTTNG_OK
;
4666 end_unlock_session_list
:
4667 session_unlock_list();
4671 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4672 const struct lttng_trigger
*trigger
,
4673 struct notification_thread_handle
*notification_thread
)
4675 enum lttng_error_code ret_code
;
4676 const char *trigger_name
;
4677 uid_t trigger_owner
;
4678 enum lttng_trigger_status trigger_status
;
4679 struct lttng_trigger
*sessiond_trigger
= nullptr;
4681 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4682 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4683 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4684 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4686 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4688 (int) trigger_owner
,
4689 (int) lttng_credentials_get_uid(cmd_creds
));
4692 * Validate the trigger credentials against the command credentials.
4693 * Only the root user can unregister a trigger with non-matching
4696 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4697 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4698 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4700 (int) trigger_owner
,
4701 (int) lttng_credentials_get_uid(cmd_creds
));
4702 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4707 /* Fetch the sessiond side trigger object. */
4708 ret_code
= notification_thread_command_get_trigger(
4709 notification_thread
, trigger
, &sessiond_trigger
);
4710 if (ret_code
!= LTTNG_OK
) {
4711 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4713 (int) trigger_owner
,
4718 LTTNG_ASSERT(sessiond_trigger
);
4721 * From this point on, no matter what, consider the trigger
4724 * We set the unregistered state of the sessiond side trigger object in
4725 * the client thread since we want to minimize the possibility of the
4726 * notification thread being stalled due to a long execution of an
4727 * action that required the trigger lock.
4729 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4731 ret_code
= notification_thread_command_unregister_trigger(notification_thread
, trigger
);
4732 if (ret_code
!= LTTNG_OK
) {
4733 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4735 (int) trigger_owner
,
4741 * Synchronize tracers if the trigger removes an event notifier.
4742 * Do this even if the trigger unregistration failed to at least stop
4743 * the tracers from producing notifications associated with this
4746 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4747 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4748 if (ret_code
!= LTTNG_OK
) {
4749 ERR("Error unregistering trigger to tracer.");
4755 lttng_trigger_put(sessiond_trigger
);
4759 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4760 struct notification_thread_handle
*notification_thread
,
4761 struct lttng_triggers
**return_triggers
)
4764 enum lttng_error_code ret_code
;
4765 struct lttng_triggers
*triggers
= nullptr;
4767 /* Get the set of triggers from the notification thread. */
4768 ret_code
= notification_thread_command_list_triggers(
4769 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4770 if (ret_code
!= LTTNG_OK
) {
4774 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4776 ret_code
= LTTNG_ERR_UNK
;
4780 *return_triggers
= triggers
;
4782 ret_code
= LTTNG_OK
;
4784 lttng_triggers_destroy(triggers
);
4788 enum lttng_error_code
4789 cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4790 const struct lttng_error_query
*query
,
4791 struct lttng_error_query_results
**_results
,
4792 struct notification_thread_handle
*notification_thread
)
4794 enum lttng_error_code ret_code
;
4795 const struct lttng_trigger
*query_target_trigger
;
4796 const struct lttng_action
*query_target_action
= nullptr;
4797 struct lttng_trigger
*matching_trigger
= nullptr;
4798 const char *trigger_name
;
4799 uid_t trigger_owner
;
4800 enum lttng_trigger_status trigger_status
;
4801 struct lttng_error_query_results
*results
= nullptr;
4803 switch (lttng_error_query_get_target_type(query
)) {
4804 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4805 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4807 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4808 query_target_trigger
= lttng_error_query_condition_borrow_target(query
);
4810 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4811 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(query
);
4817 LTTNG_ASSERT(query_target_trigger
);
4819 ret_code
= notification_thread_command_get_trigger(
4820 notification_thread
, query_target_trigger
, &matching_trigger
);
4821 if (ret_code
!= LTTNG_OK
) {
4825 /* No longer needed. */
4826 query_target_trigger
= nullptr;
4828 if (lttng_error_query_get_target_type(query
) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4829 /* Get the sessiond-side version of the target action. */
4830 query_target_action
=
4831 lttng_error_query_action_borrow_action_target(query
, matching_trigger
);
4834 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4835 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4836 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
, &trigger_owner
);
4837 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4839 results
= lttng_error_query_results_create();
4841 ret_code
= LTTNG_ERR_NOMEM
;
4845 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4847 (int) trigger_owner
,
4848 (int) lttng_credentials_get_uid(cmd_creds
));
4851 * Validate the trigger credentials against the command credentials.
4852 * Only the root user can target a trigger with non-matching
4855 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger
),
4857 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4858 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4860 (int) trigger_owner
,
4861 (int) lttng_credentials_get_uid(cmd_creds
));
4862 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4867 switch (lttng_error_query_get_target_type(query
)) {
4868 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4869 trigger_status
= lttng_trigger_add_error_results(matching_trigger
, results
);
4871 switch (trigger_status
) {
4872 case LTTNG_TRIGGER_STATUS_OK
:
4875 ret_code
= LTTNG_ERR_UNK
;
4880 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4883 lttng_trigger_condition_add_error_results(matching_trigger
, results
);
4885 switch (trigger_status
) {
4886 case LTTNG_TRIGGER_STATUS_OK
:
4889 ret_code
= LTTNG_ERR_UNK
;
4895 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4897 const enum lttng_action_status action_status
=
4898 lttng_action_add_error_query_results(query_target_action
, results
);
4900 switch (action_status
) {
4901 case LTTNG_ACTION_STATUS_OK
:
4904 ret_code
= LTTNG_ERR_UNK
;
4915 *_results
= results
;
4917 ret_code
= LTTNG_OK
;
4919 lttng_trigger_put(matching_trigger
);
4920 lttng_error_query_results_destroy(results
);
4925 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4926 * snapshot output is *not* set with a remote destination.
4928 * Return LTTNG_OK on success or a LTTNG_ERR code.
4930 static enum lttng_error_code
set_relayd_for_snapshot(struct consumer_output
*output
,
4931 const struct ltt_session
*session
)
4933 enum lttng_error_code status
= LTTNG_OK
;
4934 struct lttng_ht_iter iter
;
4935 struct consumer_socket
*socket
;
4936 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4937 const char *base_path
;
4939 LTTNG_ASSERT(output
);
4940 LTTNG_ASSERT(session
);
4942 DBG2("Set relayd object from snapshot output");
4944 if (session
->current_trace_chunk
) {
4945 enum lttng_trace_chunk_status chunk_status
= lttng_trace_chunk_get_id(
4946 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
4948 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4949 current_chunk_id
.is_set
= true;
4951 ERR("Failed to get current trace chunk id");
4952 status
= LTTNG_ERR_UNK
;
4957 /* Ignore if snapshot consumer output is not network. */
4958 if (output
->type
!= CONSUMER_DST_NET
) {
4963 * The snapshot record URI base path overrides the session
4966 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4967 base_path
= output
->dst
.net
.control
.subdir
;
4969 base_path
= session
->base_path
;
4973 * For each consumer socket, create and send the relayd object of the
4977 lttng::urcu::read_lock_guard read_lock
;
4979 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
4980 pthread_mutex_lock(socket
->lock
);
4981 status
= send_consumer_relayd_sockets(
4988 session
->live_timer
,
4989 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
4990 session
->creation_time
,
4991 session
->name_contains_creation_time
);
4992 pthread_mutex_unlock(socket
->lock
);
4993 if (status
!= LTTNG_OK
) {
5004 * Record a kernel snapshot.
5006 * Return LTTNG_OK on success or a LTTNG_ERR code.
5008 static enum lttng_error_code
record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
5009 const struct consumer_output
*output
,
5010 const struct ltt_session
*session
,
5011 uint64_t nb_packets_per_stream
)
5013 enum lttng_error_code status
;
5015 LTTNG_ASSERT(ksess
);
5016 LTTNG_ASSERT(output
);
5017 LTTNG_ASSERT(session
);
5019 status
= kernel_snapshot_record(ksess
, output
, nb_packets_per_stream
);
5024 * Record a UST snapshot.
5026 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5028 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5029 const struct consumer_output
*output
,
5030 const struct ltt_session
*session
,
5031 uint64_t nb_packets_per_stream
)
5033 enum lttng_error_code status
;
5035 LTTNG_ASSERT(usess
);
5036 LTTNG_ASSERT(output
);
5037 LTTNG_ASSERT(session
);
5039 status
= ust_app_snapshot_record(usess
, output
, nb_packets_per_stream
);
5043 static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session
*session
,
5044 uint64_t cur_nr_packets
)
5046 uint64_t tot_size
= 0;
5048 if (session
->kernel_session
) {
5049 struct ltt_kernel_channel
*chan
;
5050 const struct ltt_kernel_session
*ksess
= session
->kernel_session
;
5052 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
5053 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5055 * Don't take channel into account if we
5056 * already grab all its packets.
5060 tot_size
+= chan
->channel
->attr
.subbuf_size
* chan
->stream_count
;
5064 if (session
->ust_session
) {
5065 const struct ltt_ust_session
*usess
= session
->ust_session
;
5067 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
, cur_nr_packets
);
5074 * Calculate the number of packets we can grab from each stream that
5075 * fits within the overall snapshot max size.
5077 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5078 * the number of packets per stream.
5080 * TODO: this approach is not perfect: we consider the worse case
5081 * (packet filling the sub-buffers) as an upper bound, but we could do
5082 * better if we do this calculation while we actually grab the packet
5083 * content: we would know how much padding we don't actually store into
5086 * This algorithm is currently bounded by the number of packets per
5089 * Since we call this algorithm before actually grabbing the data, it's
5090 * an approximation: for instance, applications could appear/disappear
5091 * in between this call and actually grabbing data.
5093 static int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5097 uint64_t cur_nb_packets
= 0;
5100 return 0; /* Infinite */
5103 size_left
= max_size
;
5105 uint64_t one_more_packet_tot_size
;
5107 one_more_packet_tot_size
=
5108 get_session_size_one_more_packet_per_stream(session
, cur_nb_packets
);
5109 if (!one_more_packet_tot_size
) {
5110 /* We are already grabbing all packets. */
5113 size_left
-= one_more_packet_tot_size
;
5114 if (size_left
< 0) {
5119 if (!cur_nb_packets
&& size_left
!= max_size
) {
5120 /* Not enough room to grab one packet of each stream, error. */
5123 return cur_nb_packets
;
5126 static enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5127 const struct snapshot_output
*snapshot_output
)
5129 int64_t nb_packets_per_stream
;
5130 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5132 enum lttng_error_code ret_code
= LTTNG_OK
;
5133 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5134 struct consumer_output
*original_ust_consumer_output
= nullptr;
5135 struct consumer_output
*original_kernel_consumer_output
= nullptr;
5136 struct consumer_output
*snapshot_ust_consumer_output
= nullptr;
5137 struct consumer_output
*snapshot_kernel_consumer_output
= nullptr;
5139 ret
= snprintf(snapshot_chunk_name
,
5140 sizeof(snapshot_chunk_name
),
5142 snapshot_output
->name
,
5143 snapshot_output
->datetime
,
5144 snapshot_output
->nb_snapshot
);
5145 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5146 ERR("Failed to format snapshot name");
5147 ret_code
= LTTNG_ERR_INVALID
;
5150 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5151 snapshot_output
->name
,
5153 snapshot_chunk_name
);
5154 if (!session
->kernel_session
&& !session
->ust_session
) {
5155 ERR("Failed to record snapshot as no channels exist");
5156 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5160 if (session
->kernel_session
) {
5161 original_kernel_consumer_output
= session
->kernel_session
->consumer
;
5162 snapshot_kernel_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5163 strcpy(snapshot_kernel_consumer_output
->chunk_path
, snapshot_chunk_name
);
5165 /* Copy the original domain subdir. */
5166 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5167 original_kernel_consumer_output
->domain_subdir
);
5169 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5170 original_kernel_consumer_output
);
5172 ERR("Failed to copy consumer sockets from snapshot output configuration");
5173 ret_code
= LTTNG_ERR_NOMEM
;
5176 ret_code
= set_relayd_for_snapshot(snapshot_kernel_consumer_output
, session
);
5177 if (ret_code
!= LTTNG_OK
) {
5178 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5181 session
->kernel_session
->consumer
= snapshot_kernel_consumer_output
;
5183 if (session
->ust_session
) {
5184 original_ust_consumer_output
= session
->ust_session
->consumer
;
5185 snapshot_ust_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5186 strcpy(snapshot_ust_consumer_output
->chunk_path
, snapshot_chunk_name
);
5188 /* Copy the original domain subdir. */
5189 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5190 original_ust_consumer_output
->domain_subdir
);
5192 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5193 original_ust_consumer_output
);
5195 ERR("Failed to copy consumer sockets from snapshot output configuration");
5196 ret_code
= LTTNG_ERR_NOMEM
;
5199 ret_code
= set_relayd_for_snapshot(snapshot_ust_consumer_output
, session
);
5200 if (ret_code
!= LTTNG_OK
) {
5201 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5204 session
->ust_session
->consumer
= snapshot_ust_consumer_output
;
5207 snapshot_trace_chunk
= session_create_new_trace_chunk(
5209 snapshot_kernel_consumer_output
?: snapshot_ust_consumer_output
,
5210 consumer_output_get_base_path(snapshot_output
->consumer
),
5211 snapshot_chunk_name
);
5212 if (!snapshot_trace_chunk
) {
5213 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5215 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5218 LTTNG_ASSERT(!session
->current_trace_chunk
);
5219 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, nullptr);
5220 lttng_trace_chunk_put(snapshot_trace_chunk
);
5221 snapshot_trace_chunk
= nullptr;
5223 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5225 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5229 nb_packets_per_stream
=
5230 get_session_nb_packets_per_stream(session
, snapshot_output
->max_size
);
5231 if (nb_packets_per_stream
< 0) {
5232 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5233 goto error_close_trace_chunk
;
5236 if (session
->kernel_session
) {
5237 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5238 snapshot_kernel_consumer_output
,
5240 nb_packets_per_stream
);
5241 if (ret_code
!= LTTNG_OK
) {
5242 goto error_close_trace_chunk
;
5246 if (session
->ust_session
) {
5247 ret_code
= record_ust_snapshot(session
->ust_session
,
5248 snapshot_ust_consumer_output
,
5250 nb_packets_per_stream
);
5251 if (ret_code
!= LTTNG_OK
) {
5252 goto error_close_trace_chunk
;
5256 error_close_trace_chunk
:
5257 if (session_set_trace_chunk(session
, nullptr, &snapshot_trace_chunk
)) {
5258 ERR("Failed to release the current trace chunk of session \"%s\"", session
->name
);
5259 ret_code
= LTTNG_ERR_UNK
;
5262 if (session_close_trace_chunk(session
,
5263 snapshot_trace_chunk
,
5264 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
,
5267 * Don't goto end; make sure the chunk is closed for the session
5268 * to allow future snapshots.
5270 ERR("Failed to close snapshot trace chunk of session \"%s\"", session
->name
);
5271 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5274 lttng_trace_chunk_put(snapshot_trace_chunk
);
5275 snapshot_trace_chunk
= nullptr;
5277 if (original_ust_consumer_output
) {
5278 session
->ust_session
->consumer
= original_ust_consumer_output
;
5280 if (original_kernel_consumer_output
) {
5281 session
->kernel_session
->consumer
= original_kernel_consumer_output
;
5283 consumer_output_put(snapshot_ust_consumer_output
);
5284 consumer_output_put(snapshot_kernel_consumer_output
);
5289 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5291 * The wait parameter is ignored so this call always wait for the snapshot to
5292 * complete before returning.
5294 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5296 int cmd_snapshot_record(struct ltt_session
*session
,
5297 const struct lttng_snapshot_output
*output
,
5298 int wait
__attribute__((unused
)))
5300 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5302 unsigned int snapshot_success
= 0;
5304 struct snapshot_output
*tmp_output
= nullptr;
5306 LTTNG_ASSERT(session
);
5307 LTTNG_ASSERT(output
);
5309 DBG("Cmd snapshot record for session %s", session
->name
);
5311 /* Get the datetime for the snapshot output directory. */
5312 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
, sizeof(datetime
));
5314 cmd_ret
= LTTNG_ERR_INVALID
;
5319 * Permission denied to create an output if the session is not
5320 * set in no output mode.
5322 if (session
->output_traces
) {
5323 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5327 /* The session needs to be started at least once. */
5328 if (!session
->has_been_started
) {
5329 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5333 /* Use temporary output for the session. */
5334 if (*output
->ctrl_url
!= '\0') {
5335 tmp_output
= snapshot_output_alloc();
5337 cmd_ret
= LTTNG_ERR_NOMEM
;
5341 ret
= snapshot_output_init(session
,
5350 if (ret
== -ENOMEM
) {
5351 cmd_ret
= LTTNG_ERR_NOMEM
;
5353 cmd_ret
= LTTNG_ERR_INVALID
;
5357 /* Use the global session count for the temporary snapshot. */
5358 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5360 /* Use the global datetime */
5361 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5362 cmd_ret
= snapshot_record(session
, tmp_output
);
5363 if (cmd_ret
!= LTTNG_OK
) {
5366 snapshot_success
= 1;
5368 struct snapshot_output
*sout
;
5369 struct lttng_ht_iter iter
;
5371 lttng::urcu::read_lock_guard read_lock
;
5373 cds_lfht_for_each_entry (
5374 session
->snapshot
.output_ht
->ht
, &iter
.iter
, sout
, node
.node
) {
5375 struct snapshot_output output_copy
;
5378 * Make a local copy of the output and override output
5379 * parameters with those provided as part of the
5382 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5384 if (output
->max_size
!= (uint64_t) -1ULL) {
5385 output_copy
.max_size
= output
->max_size
;
5388 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5389 memcpy(output_copy
.datetime
, datetime
, sizeof(datetime
));
5391 /* Use temporary name. */
5392 if (*output
->name
!= '\0') {
5393 if (lttng_strncpy(output_copy
.name
,
5395 sizeof(output_copy
.name
))) {
5396 cmd_ret
= LTTNG_ERR_INVALID
;
5401 cmd_ret
= snapshot_record(session
, &output_copy
);
5402 if (cmd_ret
!= LTTNG_OK
) {
5406 snapshot_success
= 1;
5410 if (snapshot_success
) {
5411 session
->snapshot
.nb_snapshot
++;
5413 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5418 snapshot_output_destroy(tmp_output
);
5425 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5427 int cmd_set_session_shm_path(struct ltt_session
*session
, const char *shm_path
)
5430 LTTNG_ASSERT(session
);
5433 * Can only set shm path before session is started.
5435 if (session
->has_been_started
) {
5436 return LTTNG_ERR_SESSION_STARTED
;
5439 strncpy(session
->shm_path
, shm_path
, sizeof(session
->shm_path
));
5440 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5446 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5448 * Ask the consumer to rotate the session output directory.
5449 * The session lock must be held.
5451 * Returns LTTNG_OK on success or else a negative LTTng error code.
5453 int cmd_rotate_session(struct ltt_session
*session
,
5454 struct lttng_rotate_session_return
*rotate_return
,
5455 bool quiet_rotation
,
5456 enum lttng_trace_chunk_command_type command
)
5459 uint64_t ongoing_rotation_chunk_id
;
5460 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5461 struct lttng_trace_chunk
*chunk_being_archived
= nullptr;
5462 struct lttng_trace_chunk
*new_trace_chunk
= nullptr;
5463 enum lttng_trace_chunk_status chunk_status
;
5464 bool failed_to_rotate
= false;
5465 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5467 LTTNG_ASSERT(session
);
5469 if (!session
->has_been_started
) {
5470 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5475 * Explicit rotation is not supported for live sessions.
5476 * However, live sessions can perform a quiet rotation on
5478 * Rotation is not supported for snapshot traces (no output).
5480 if ((!quiet_rotation
&& session
->live_timer
) || !session
->output_traces
) {
5481 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5485 /* Unsupported feature in lttng-relayd before 2.11. */
5486 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5487 (session
->consumer
->relay_major_version
== 2 &&
5488 session
->consumer
->relay_minor_version
< 11)) {
5489 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5493 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5494 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5495 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5499 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5500 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5502 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5507 * After a stop, we only allow one rotation to occur, the other ones are
5508 * useless until a new start.
5510 if (session
->rotated_after_last_stop
) {
5511 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5513 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5518 * After a stop followed by a clear, disallow following rotations a they would
5519 * generate empty chunks.
5521 if (session
->cleared_after_last_stop
) {
5522 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5524 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5528 if (session
->active
) {
5530 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
5531 if (!new_trace_chunk
) {
5532 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5538 * The current trace chunk becomes the chunk being archived.
5540 * After this point, "chunk_being_archived" must absolutely
5541 * be closed on the consumer(s), otherwise it will never be
5542 * cleaned-up, which will result in a leak.
5544 ret
= session_set_trace_chunk(session
, new_trace_chunk
, &chunk_being_archived
);
5546 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5550 if (session
->kernel_session
) {
5551 cmd_ret
= kernel_rotate_session(session
);
5552 if (cmd_ret
!= LTTNG_OK
) {
5553 failed_to_rotate
= true;
5554 rotation_fail_code
= cmd_ret
;
5557 if (session
->ust_session
) {
5558 cmd_ret
= ust_app_rotate_session(session
);
5559 if (cmd_ret
!= LTTNG_OK
) {
5560 failed_to_rotate
= true;
5561 rotation_fail_code
= cmd_ret
;
5565 if (!session
->active
) {
5566 session
->rotated_after_last_stop
= true;
5569 if (!chunk_being_archived
) {
5570 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5572 if (failed_to_rotate
) {
5573 cmd_ret
= rotation_fail_code
;
5580 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5581 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
, &ongoing_rotation_chunk_id
);
5582 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5584 ret
= session_close_trace_chunk(
5585 session
, chunk_being_archived
, command
, session
->last_chunk_path
);
5587 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5591 if (failed_to_rotate
) {
5592 cmd_ret
= rotation_fail_code
;
5596 session
->quiet_rotation
= quiet_rotation
;
5597 ret
= timer_session_rotation_pending_check_start(session
, DEFAULT_ROTATE_PENDING_TIMER
);
5599 cmd_ret
= LTTNG_ERR_UNK
;
5603 if (rotate_return
) {
5604 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5607 session
->chunk_being_archived
= chunk_being_archived
;
5608 chunk_being_archived
= nullptr;
5609 if (!quiet_rotation
) {
5610 ret
= notification_thread_command_session_rotation_ongoing(
5611 the_notification_thread_handle
, session
->id
, ongoing_rotation_chunk_id
);
5612 if (ret
!= LTTNG_OK
) {
5613 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5615 cmd_ret
= (lttng_error_code
) ret
;
5619 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5621 ongoing_rotation_chunk_id
);
5623 lttng_trace_chunk_put(new_trace_chunk
);
5624 lttng_trace_chunk_put(chunk_being_archived
);
5625 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5628 if (session_reset_rotation_state(*session
, LTTNG_ROTATION_STATE_ERROR
)) {
5629 ERR("Failed to reset rotation state of session \"%s\"", session
->name
);
5635 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5637 * Check if the session has finished its rotation.
5639 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5641 int cmd_rotate_get_info(struct ltt_session
*session
,
5642 struct lttng_rotation_get_info_return
*info_return
,
5643 uint64_t rotation_id
)
5645 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5646 enum lttng_rotation_state rotation_state
;
5648 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
,
5650 session
->most_recent_chunk_id
.value
);
5652 if (session
->chunk_being_archived
) {
5653 enum lttng_trace_chunk_status chunk_status
;
5656 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
, &chunk_id
);
5657 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5659 rotation_state
= rotation_id
== chunk_id
? LTTNG_ROTATION_STATE_ONGOING
:
5660 LTTNG_ROTATION_STATE_EXPIRED
;
5662 if (session
->last_archived_chunk_id
.is_set
&&
5663 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5664 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5666 rotation_state
= session
->rotation_state
;
5670 switch (rotation_state
) {
5671 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5672 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5675 case LTTNG_ROTATION_STATE_EXPIRED
:
5676 DBG("Reporting that the rotation state of rotation id %" PRIu64
5677 " of session \"%s\" has expired",
5681 case LTTNG_ROTATION_STATE_ONGOING
:
5682 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5686 case LTTNG_ROTATION_STATE_COMPLETED
:
5690 char *current_tracing_path_reply
;
5691 size_t current_tracing_path_reply_len
;
5693 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5697 switch (session_get_consumer_destination_type(session
)) {
5698 case CONSUMER_DST_LOCAL
:
5699 current_tracing_path_reply
= info_return
->location
.local
.absolute_path
;
5700 current_tracing_path_reply_len
=
5701 sizeof(info_return
->location
.local
.absolute_path
);
5702 info_return
->location_type
=
5703 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5704 fmt_ret
= asprintf(&chunk_path
,
5705 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5706 session_get_base_path(session
),
5707 session
->last_archived_chunk_name
);
5708 if (fmt_ret
== -1) {
5709 PERROR("Failed to format the path of the last archived trace chunk");
5710 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5711 cmd_ret
= LTTNG_ERR_UNK
;
5715 case CONSUMER_DST_NET
:
5717 uint16_t ctrl_port
, data_port
;
5719 current_tracing_path_reply
= info_return
->location
.relay
.relative_path
;
5720 current_tracing_path_reply_len
=
5721 sizeof(info_return
->location
.relay
.relative_path
);
5722 /* Currently the only supported relay protocol. */
5723 info_return
->location
.relay
.protocol
=
5724 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5726 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5727 session_get_net_consumer_hostname(session
),
5728 sizeof(info_return
->location
.relay
.host
));
5730 ERR("Failed to copy host name to rotate_get_info reply");
5731 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5732 cmd_ret
= LTTNG_ERR_SET_URL
;
5736 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5737 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5738 info_return
->location
.relay
.ports
.data
= data_port
;
5739 info_return
->location_type
=
5740 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5741 chunk_path
= strdup(session
->last_chunk_path
);
5743 ERR("Failed to allocate the path of the last archived trace chunk");
5744 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5745 cmd_ret
= LTTNG_ERR_UNK
;
5754 fmt_ret
= lttng_strncpy(
5755 current_tracing_path_reply
, chunk_path
, current_tracing_path_reply_len
);
5758 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5759 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5760 cmd_ret
= LTTNG_ERR_UNK
;
5766 case LTTNG_ROTATION_STATE_ERROR
:
5767 DBG("Reporting that an error occurred during rotation %" PRIu64
5768 " of session \"%s\"",
5778 info_return
->status
= (int32_t) rotation_state
;
5783 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5785 * Configure the automatic rotation parameters.
5786 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5787 * 'activate' to false means deactivate the rotation schedule and validate that
5788 * 'new_value' has the same value as the currently active value.
5790 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5792 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5794 enum lttng_rotation_schedule_type schedule_type
,
5798 uint64_t *parameter_value
;
5800 LTTNG_ASSERT(session
);
5802 DBG("Cmd rotate set schedule session %s", session
->name
);
5804 if (session
->live_timer
|| !session
->output_traces
) {
5805 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5806 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5810 switch (schedule_type
) {
5811 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5812 parameter_value
= &session
->rotate_size
;
5814 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5815 parameter_value
= &session
->rotate_timer_period
;
5816 if (new_value
>= UINT_MAX
) {
5817 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
5821 ret
= LTTNG_ERR_INVALID
;
5826 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5827 ret
= LTTNG_ERR_INVALID
;
5831 /* Improper use of the API. */
5832 if (new_value
== -1ULL) {
5833 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5834 ret
= LTTNG_ERR_INVALID
;
5839 * As indicated in struct ltt_session's comments, a value of == 0 means
5840 * this schedule rotation type is not in use.
5842 * Reject the command if we were asked to activate a schedule that was
5845 if (activate
&& *parameter_value
!= 0) {
5846 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5847 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5852 * Reject the command if we were asked to deactivate a schedule that was
5855 if (!activate
&& *parameter_value
== 0) {
5856 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5857 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5862 * Reject the command if we were asked to deactivate a schedule that
5865 if (!activate
&& *parameter_value
!= new_value
) {
5866 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5867 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5871 *parameter_value
= activate
? new_value
: 0;
5873 switch (schedule_type
) {
5874 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5875 if (activate
&& session
->active
) {
5877 * Only start the timer if the session is active,
5878 * otherwise it will be started when the session starts.
5880 ret
= timer_session_rotation_schedule_timer_start(session
, new_value
);
5882 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5883 ret
= LTTNG_ERR_UNK
;
5887 ret
= timer_session_rotation_schedule_timer_stop(session
);
5889 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5890 ret
= LTTNG_ERR_UNK
;
5895 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5898 the_rotation_thread_handle
->subscribe_session_consumed_size_rotation(
5899 *session
, new_value
);
5900 } catch (std::exception
& e
) {
5901 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5903 ret
= LTTNG_ERR_UNK
;
5908 the_rotation_thread_handle
5909 ->unsubscribe_session_consumed_size_rotation(*session
);
5910 } catch (std::exception
& e
) {
5911 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5913 ret
= LTTNG_ERR_UNK
;
5919 /* Would have been caught before. */
5931 /* Wait for a given path to be removed before continuing. */
5932 static enum lttng_error_code
wait_on_path(void *path_data
)
5934 const char *shm_path
= (const char *) path_data
;
5936 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5942 ret
= stat(shm_path
, &st
);
5944 if (errno
!= ENOENT
) {
5945 PERROR("stat() returned an error while checking for the existence of the shm path");
5947 DBG("shm path no longer exists, completing the destruction of session");
5951 if (!S_ISDIR(st
.st_mode
)) {
5952 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5957 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5963 * Returns a pointer to a handler to run on completion of a command.
5964 * Returns NULL if no handler has to be run for the last command executed.
5966 const struct cmd_completion_handler
*cmd_pop_completion_handler()
5968 struct cmd_completion_handler
*handler
= current_completion_handler
;
5970 current_completion_handler
= nullptr;
5975 * Init command subsystem.
5980 * Set network sequence index to 1 for streams to match a relayd
5981 * socket on the consumer side.
5983 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5984 relayd_net_seq_idx
= 1;
5985 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5987 DBG("Command subsystem initialized");