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/kernel.h>
59 #include <lttng/location-internal.hpp>
60 #include <lttng/lttng-error.h>
61 #include <lttng/rotate-internal.hpp>
62 #include <lttng/session-descriptor-internal.hpp>
63 #include <lttng/session-internal.hpp>
64 #include <lttng/tracker.h>
65 #include <lttng/trigger/trigger-internal.hpp>
66 #include <lttng/userspace-probe-internal.hpp>
72 #include <urcu/list.h>
73 #include <urcu/uatomic.h>
75 /* Sleep for 100ms between each check for the shm path's deletion. */
76 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
78 namespace lsu
= lttng::sessiond::ust
;
80 static enum lttng_error_code
wait_on_path(void *path
);
83 struct cmd_destroy_session_reply_context
{
85 bool implicit_rotation_on_destroy
;
87 * Indicates whether or not an error occurred while launching the
88 * destruction of a session.
90 enum lttng_error_code destruction_status
;
94 * Command completion handler that is used by the destroy command
95 * when a session that has a non-default shm_path is being destroyed.
97 * See comment in cmd_destroy_session() for the rationale.
99 struct destroy_completion_handler
{
100 struct cmd_completion_handler handler
;
101 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
102 } destroy_completion_handler
= {
103 .handler
= { .run
= wait_on_path
, .data
= destroy_completion_handler
.shm_path
},
108 * Used to keep a unique index for each relayd socket created where this value
109 * is associated with streams on the consumer so it can match the right relayd
110 * to send to. It must be accessed with the relayd_net_seq_idx_lock
113 pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
114 uint64_t relayd_net_seq_idx
;
117 static struct cmd_completion_handler
*current_completion_handler
;
118 static int validate_ust_event_name(const char *);
119 static int cmd_enable_event_internal(struct ltt_session
*session
,
120 const struct lttng_domain
*domain
,
122 struct lttng_event
*event
,
123 char *filter_expression
,
124 struct lttng_bytecode
*filter
,
125 struct lttng_event_exclusion
*exclusion
,
127 static enum lttng_error_code
cmd_enable_channel_internal(struct ltt_session
*session
,
128 const struct lttng_domain
*domain
,
129 const struct lttng_channel
*_attr
,
133 * Create a session path used by list_lttng_sessions for the case that the
134 * session consumer is on the network.
136 static int build_network_session_path(char *dst
, size_t size
, struct ltt_session
*session
)
138 int ret
, kdata_port
, udata_port
;
139 struct lttng_uri
*kuri
= nullptr, *uuri
= nullptr, *uri
= nullptr;
140 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
142 LTTNG_ASSERT(session
);
145 memset(tmp_urls
, 0, sizeof(tmp_urls
));
146 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
148 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
150 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
151 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
152 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
155 if (session
->ust_session
&& session
->ust_session
->consumer
) {
156 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
157 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
160 if (uuri
== nullptr && kuri
== nullptr) {
161 uri
= &session
->consumer
->dst
.net
.control
;
162 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
163 } else if (kuri
&& uuri
) {
164 ret
= uri_compare(kuri
, uuri
);
168 /* Build uuri URL string */
169 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
176 } else if (kuri
&& uuri
== nullptr) {
178 } else if (uuri
&& kuri
== nullptr) {
182 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
188 * Do we have a UST url set. If yes, this means we have both kernel and UST
191 if (*tmp_uurl
!= '\0') {
194 "[K]: %s [data: %d] -- [U]: %s [data: %d]",
201 if (kuri
|| (!kuri
&& !uuri
)) {
204 /* No kernel URI, use the UST port. */
207 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
215 * Get run-time attributes if the session has been started (discarded events,
218 static int get_kernel_runtime_stats(struct ltt_session
*session
,
219 struct ltt_kernel_channel
*kchan
,
220 uint64_t *discarded_events
,
221 uint64_t *lost_packets
)
225 if (!session
->has_been_started
) {
227 *discarded_events
= 0;
232 ret
= consumer_get_discarded_events(
233 session
->id
, kchan
->key
, session
->kernel_session
->consumer
, discarded_events
);
238 ret
= consumer_get_lost_packets(
239 session
->id
, kchan
->key
, session
->kernel_session
->consumer
, lost_packets
);
249 * Get run-time attributes if the session has been started (discarded events,
252 static int get_ust_runtime_stats(struct ltt_session
*session
,
253 struct ltt_ust_channel
*uchan
,
254 uint64_t *discarded_events
,
255 uint64_t *lost_packets
)
258 struct ltt_ust_session
*usess
;
260 if (!discarded_events
|| !lost_packets
) {
265 usess
= session
->ust_session
;
266 LTTNG_ASSERT(discarded_events
);
267 LTTNG_ASSERT(lost_packets
);
269 if (!usess
|| !session
->has_been_started
) {
270 *discarded_events
= 0;
276 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
277 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
278 &usess
->buffer_reg_uid_list
,
281 uchan
->attr
.overwrite
,
284 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
285 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
288 uchan
->attr
.overwrite
,
294 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
295 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
297 ERR("Unsupported buffer type");
308 * Create a list of agent domain events.
310 * Return number of events in list on success or else a negative value.
312 static enum lttng_error_code
list_lttng_agent_events(struct agent
*agt
,
313 struct lttng_payload
*reply_payload
,
314 unsigned int *nb_events
)
316 enum lttng_error_code ret_code
;
318 unsigned int local_nb_events
= 0;
319 struct agent_event
*event
;
320 struct lttng_ht_iter iter
;
321 unsigned long agent_event_count
;
324 assert(reply_payload
);
326 DBG3("Listing agent events");
328 agent_event_count
= lttng_ht_get_count(agt
->events
);
329 if (agent_event_count
== 0) {
334 if (agent_event_count
> UINT_MAX
) {
335 ret_code
= LTTNG_ERR_OVERFLOW
;
339 local_nb_events
= (unsigned int) agent_event_count
;
342 lttng::urcu::read_lock_guard read_lock
;
344 cds_lfht_for_each_entry (agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
345 struct lttng_event
*tmp_event
= lttng_event_create();
348 ret_code
= LTTNG_ERR_NOMEM
;
352 if (lttng_strncpy(tmp_event
->name
, event
->name
, sizeof(tmp_event
->name
))) {
353 lttng_event_destroy(tmp_event
);
354 ret_code
= LTTNG_ERR_FATAL
;
358 tmp_event
->name
[sizeof(tmp_event
->name
) - 1] = '\0';
359 tmp_event
->enabled
= !!event
->enabled_count
;
360 tmp_event
->loglevel
= event
->loglevel_value
;
361 tmp_event
->loglevel_type
= event
->loglevel_type
;
363 ret
= lttng_event_serialize(tmp_event
,
366 event
->filter_expression
,
370 lttng_event_destroy(tmp_event
);
372 ret_code
= LTTNG_ERR_FATAL
;
379 *nb_events
= local_nb_events
;
385 * Create a list of ust global domain events.
387 static enum lttng_error_code
list_lttng_ust_global_events(char *channel_name
,
388 struct ltt_ust_domain_global
*ust_global
,
389 struct lttng_payload
*reply_payload
,
390 unsigned int *nb_events
)
392 enum lttng_error_code ret_code
;
394 struct lttng_ht_iter iter
;
395 struct lttng_ht_node_str
*node
;
396 struct ltt_ust_channel
*uchan
;
397 struct ltt_ust_event
*uevent
;
398 unsigned long channel_event_count
;
399 unsigned int local_nb_events
= 0;
401 assert(reply_payload
);
404 DBG("Listing UST global events for channel %s", channel_name
);
406 lttng::urcu::read_lock_guard read_lock
;
408 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
409 node
= lttng_ht_iter_get_node_str(&iter
);
410 if (node
== nullptr) {
411 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
415 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
417 channel_event_count
= lttng_ht_get_count(uchan
->events
);
418 if (channel_event_count
== 0) {
424 if (channel_event_count
> UINT_MAX
) {
425 ret_code
= LTTNG_ERR_OVERFLOW
;
429 local_nb_events
= (unsigned int) channel_event_count
;
431 DBG3("Listing UST global %d events", *nb_events
);
433 cds_lfht_for_each_entry (uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
434 struct lttng_event
*tmp_event
= nullptr;
436 if (uevent
->internal
) {
437 /* This event should remain hidden from clients */
442 tmp_event
= lttng_event_create();
444 ret_code
= LTTNG_ERR_NOMEM
;
448 if (lttng_strncpy(tmp_event
->name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
)) {
449 ret_code
= LTTNG_ERR_FATAL
;
450 lttng_event_destroy(tmp_event
);
454 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
455 tmp_event
->enabled
= uevent
->enabled
;
457 switch (uevent
->attr
.instrumentation
) {
458 case LTTNG_UST_ABI_TRACEPOINT
:
459 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
461 case LTTNG_UST_ABI_PROBE
:
462 tmp_event
->type
= LTTNG_EVENT_PROBE
;
464 case LTTNG_UST_ABI_FUNCTION
:
465 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
469 tmp_event
->loglevel
= uevent
->attr
.loglevel
;
470 switch (uevent
->attr
.loglevel_type
) {
471 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
472 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
474 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
475 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
477 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
478 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
481 if (uevent
->filter
) {
482 tmp_event
->filter
= 1;
484 if (uevent
->exclusion
) {
485 tmp_event
->exclusion
= 1;
488 std::vector
<const char *> exclusion_names
;
489 if (uevent
->exclusion
) {
490 for (int i
= 0; i
< uevent
->exclusion
->count
; i
++) {
491 exclusion_names
.emplace_back(
492 LTTNG_EVENT_EXCLUSION_NAME_AT(uevent
->exclusion
, i
));
497 * We do not care about the filter bytecode and the fd from the
498 * userspace_probe_location.
500 ret
= lttng_event_serialize(tmp_event
,
501 exclusion_names
.size(),
502 exclusion_names
.size() ? exclusion_names
.data() :
504 uevent
->filter_expression
,
508 lttng_event_destroy(tmp_event
);
510 ret_code
= LTTNG_ERR_FATAL
;
516 /* nb_events is already set at this point. */
518 *nb_events
= local_nb_events
;
524 * Fill lttng_event array of all kernel events in the channel.
526 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
527 struct ltt_kernel_session
*kernel_session
,
528 struct lttng_payload
*reply_payload
,
529 unsigned int *nb_events
)
531 enum lttng_error_code ret_code
;
533 struct ltt_kernel_event
*event
;
534 struct ltt_kernel_channel
*kchan
;
536 assert(reply_payload
);
538 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
539 if (kchan
== nullptr) {
540 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
544 *nb_events
= kchan
->event_count
;
546 DBG("Listing events for channel %s", kchan
->channel
->name
);
548 if (*nb_events
== 0) {
553 /* Kernel channels */
554 cds_list_for_each_entry (event
, &kchan
->events_list
.head
, list
) {
555 struct lttng_event
*tmp_event
= lttng_event_create();
558 ret_code
= LTTNG_ERR_NOMEM
;
562 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
563 lttng_event_destroy(tmp_event
);
564 ret_code
= LTTNG_ERR_FATAL
;
568 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
569 tmp_event
->enabled
= event
->enabled
;
570 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
572 switch (event
->event
->instrumentation
) {
573 case LTTNG_KERNEL_ABI_TRACEPOINT
:
574 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
576 case LTTNG_KERNEL_ABI_KRETPROBE
:
577 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
578 memcpy(&tmp_event
->attr
.probe
,
579 &event
->event
->u
.kprobe
,
580 sizeof(struct lttng_kernel_abi_kprobe
));
582 case LTTNG_KERNEL_ABI_KPROBE
:
583 tmp_event
->type
= LTTNG_EVENT_PROBE
;
584 memcpy(&tmp_event
->attr
.probe
,
585 &event
->event
->u
.kprobe
,
586 sizeof(struct lttng_kernel_abi_kprobe
));
588 case LTTNG_KERNEL_ABI_UPROBE
:
589 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
591 case LTTNG_KERNEL_ABI_FUNCTION
:
592 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
593 memcpy(&(tmp_event
->attr
.ftrace
),
594 &event
->event
->u
.ftrace
,
595 sizeof(struct lttng_kernel_abi_function
));
597 case LTTNG_KERNEL_ABI_NOOP
:
598 tmp_event
->type
= LTTNG_EVENT_NOOP
;
600 case LTTNG_KERNEL_ABI_SYSCALL
:
601 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
603 case LTTNG_KERNEL_ABI_ALL
:
610 if (event
->userspace_probe_location
) {
611 struct lttng_userspace_probe_location
*location_copy
=
612 lttng_userspace_probe_location_copy(
613 event
->userspace_probe_location
);
615 if (!location_copy
) {
616 lttng_event_destroy(tmp_event
);
617 ret_code
= LTTNG_ERR_NOMEM
;
621 ret
= lttng_event_set_userspace_probe_location(tmp_event
, location_copy
);
623 lttng_event_destroy(tmp_event
);
624 lttng_userspace_probe_location_destroy(location_copy
);
625 ret_code
= LTTNG_ERR_INVALID
;
630 ret
= lttng_event_serialize(
631 tmp_event
, 0, nullptr, event
->filter_expression
, 0, nullptr, reply_payload
);
632 lttng_event_destroy(tmp_event
);
634 ret_code
= LTTNG_ERR_FATAL
;
645 * Add URI so the consumer output object. Set the correct path depending on the
646 * domain adding the default trace directory.
648 static enum lttng_error_code
add_uri_to_consumer(const struct ltt_session
*session
,
649 struct consumer_output
*consumer
,
650 struct lttng_uri
*uri
,
651 enum lttng_domain_type domain
)
654 enum lttng_error_code ret_code
= LTTNG_OK
;
658 if (consumer
== nullptr) {
659 DBG("No consumer detected. Don't add URI. Stopping.");
660 ret_code
= LTTNG_ERR_NO_CONSUMER
;
665 case LTTNG_DOMAIN_KERNEL
:
666 ret
= lttng_strncpy(consumer
->domain_subdir
,
667 DEFAULT_KERNEL_TRACE_DIR
,
668 sizeof(consumer
->domain_subdir
));
670 case LTTNG_DOMAIN_UST
:
671 ret
= lttng_strncpy(consumer
->domain_subdir
,
672 DEFAULT_UST_TRACE_DIR
,
673 sizeof(consumer
->domain_subdir
));
677 * This case is possible is we try to add the URI to the global
678 * tracing session consumer object which in this case there is
681 memset(consumer
->domain_subdir
, 0, sizeof(consumer
->domain_subdir
));
685 ERR("Failed to initialize consumer output domain subdirectory");
686 ret_code
= LTTNG_ERR_FATAL
;
690 switch (uri
->dtype
) {
693 DBG2("Setting network URI to consumer");
695 if (consumer
->type
== CONSUMER_DST_NET
) {
696 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
697 consumer
->dst
.net
.control_isset
) ||
698 (uri
->stype
== LTTNG_STREAM_DATA
&& consumer
->dst
.net
.data_isset
)) {
699 ret_code
= LTTNG_ERR_URL_EXIST
;
703 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
706 /* Set URI into consumer output object */
707 ret
= consumer_set_network_uri(session
, consumer
, uri
);
709 ret_code
= (lttng_error_code
) -ret
;
711 } else if (ret
== 1) {
713 * URI was the same in the consumer so we do not append the subdir
714 * again so to not duplicate output dir.
721 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
722 ret_code
= LTTNG_ERR_INVALID
;
725 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
726 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
728 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
730 sizeof(consumer
->dst
.session_root_path
));
732 ret_code
= LTTNG_ERR_FATAL
;
735 consumer
->type
= CONSUMER_DST_LOCAL
;
745 * Init tracing by creating trace directory and sending fds kernel consumer.
747 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
750 struct lttng_ht_iter iter
;
751 struct consumer_socket
*socket
;
753 LTTNG_ASSERT(session
);
755 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= nullptr) {
756 lttng::urcu::read_lock_guard read_lock
;
758 cds_lfht_for_each_entry (
759 session
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
760 pthread_mutex_lock(socket
->lock
);
761 ret
= kernel_consumer_send_session(socket
, session
);
762 pthread_mutex_unlock(socket
->lock
);
764 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
775 * Create a socket to the relayd using the URI.
777 * On success, the relayd_sock pointer is set to the created socket.
778 * Else, it remains untouched and an LTTng error code is returned.
780 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
781 struct lttcomm_relayd_sock
**relayd_sock
,
782 struct consumer_output
*consumer
)
785 enum lttng_error_code status
= LTTNG_OK
;
786 struct lttcomm_relayd_sock
*rsock
;
788 rsock
= lttcomm_alloc_relayd_sock(
789 uri
, RELAYD_VERSION_COMM_MAJOR
, RELAYD_VERSION_COMM_MINOR
);
791 status
= LTTNG_ERR_FATAL
;
796 * Connect to relayd so we can proceed with a session creation. This call
797 * can possibly block for an arbitrary amount of time to set the health
798 * state to be in poll execution.
801 ret
= relayd_connect(rsock
);
804 ERR("Unable to reach lttng-relayd");
805 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
809 /* Create socket for control stream. */
810 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
811 uint64_t result_flags
;
813 DBG3("Creating relayd stream socket from URI");
815 /* Check relayd version */
816 ret
= relayd_version_check(rsock
);
817 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
818 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
820 } else if (ret
< 0) {
821 ERR("Unable to reach lttng-relayd");
822 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
825 consumer
->relay_major_version
= rsock
->major
;
826 consumer
->relay_minor_version
= rsock
->minor
;
827 ret
= relayd_get_configuration(rsock
, 0, &result_flags
);
829 ERR("Unable to get relayd configuration");
830 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
833 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
834 consumer
->relay_allows_clear
= true;
836 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
837 DBG3("Creating relayd data socket from URI");
839 /* Command is not valid */
840 ERR("Relayd invalid stream type: %d", uri
->stype
);
841 status
= LTTNG_ERR_INVALID
;
845 *relayd_sock
= rsock
;
850 /* The returned value is not useful since we are on an error path. */
851 (void) relayd_close(rsock
);
859 * Connect to the relayd using URI and send the socket to the right consumer.
861 * The consumer socket lock must be held by the caller.
863 * Returns LTTNG_OK on success or an LTTng error code on failure.
865 static enum lttng_error_code
send_consumer_relayd_socket(unsigned int session_id
,
866 struct lttng_uri
*relayd_uri
,
867 struct consumer_output
*consumer
,
868 struct consumer_socket
*consumer_sock
,
869 const char *session_name
,
870 const char *hostname
,
871 const char *base_path
,
872 int session_live_timer
,
873 const uint64_t *current_chunk_id
,
874 time_t session_creation_time
,
875 bool session_name_contains_creation_time
)
878 struct lttcomm_relayd_sock
*rsock
= nullptr;
879 enum lttng_error_code status
;
881 /* Connect to relayd and make version check if uri is the control. */
882 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
883 if (status
!= LTTNG_OK
) {
884 goto relayd_comm_error
;
888 /* Set the network sequence index if not set. */
889 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
890 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
892 * Increment net_seq_idx because we are about to transfer the
893 * new relayd socket to the consumer.
894 * Assign unique key so the consumer can match streams.
896 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
897 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
900 /* Send relayd socket to consumer. */
901 ret
= consumer_send_relayd_socket(consumer_sock
,
911 session_creation_time
,
912 session_name_contains_creation_time
);
914 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
918 /* Flag that the corresponding socket was sent. */
919 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
920 consumer_sock
->control_sock_sent
= 1;
921 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
922 consumer_sock
->data_sock_sent
= 1;
926 * Close socket which was dup on the consumer side. The session daemon does
927 * NOT keep track of the relayd socket(s) once transfer to the consumer.
931 if (status
!= LTTNG_OK
) {
933 * The consumer output for this session should not be used anymore
934 * since the relayd connection failed thus making any tracing or/and
935 * streaming not usable.
937 consumer
->enabled
= false;
939 (void) relayd_close(rsock
);
947 * Send both relayd sockets to a specific consumer and domain. This is a
948 * helper function to facilitate sending the information to the consumer for a
951 * The consumer socket lock must be held by the caller.
953 * Returns LTTNG_OK, or an LTTng error code on failure.
955 static enum lttng_error_code
send_consumer_relayd_sockets(unsigned int session_id
,
956 struct consumer_output
*consumer
,
957 struct consumer_socket
*sock
,
958 const char *session_name
,
959 const char *hostname
,
960 const char *base_path
,
961 int session_live_timer
,
962 const uint64_t *current_chunk_id
,
963 time_t session_creation_time
,
964 bool session_name_contains_creation_time
)
966 enum lttng_error_code status
= LTTNG_OK
;
968 LTTNG_ASSERT(consumer
);
971 /* Sending control relayd socket. */
972 if (!sock
->control_sock_sent
) {
973 status
= send_consumer_relayd_socket(session_id
,
974 &consumer
->dst
.net
.control
,
982 session_creation_time
,
983 session_name_contains_creation_time
);
984 if (status
!= LTTNG_OK
) {
989 /* Sending data relayd socket. */
990 if (!sock
->data_sock_sent
) {
991 status
= send_consumer_relayd_socket(session_id
,
992 &consumer
->dst
.net
.data
,
1000 session_creation_time
,
1001 session_name_contains_creation_time
);
1002 if (status
!= LTTNG_OK
) {
1012 * Setup relayd connections for a tracing session. First creates the socket to
1013 * the relayd and send them to the right domain consumer. Consumer type MUST be
1016 int cmd_setup_relayd(struct ltt_session
*session
)
1019 struct ltt_ust_session
*usess
;
1020 struct ltt_kernel_session
*ksess
;
1021 struct consumer_socket
*socket
;
1022 struct lttng_ht_iter iter
;
1023 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1025 LTTNG_ASSERT(session
);
1027 usess
= session
->ust_session
;
1028 ksess
= session
->kernel_session
;
1030 DBG("Setting relayd for session %s", session
->name
);
1032 if (session
->current_trace_chunk
) {
1033 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1034 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1036 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1037 current_chunk_id
.is_set
= true;
1039 ERR("Failed to get current trace chunk id");
1040 ret
= LTTNG_ERR_UNK
;
1045 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
&&
1046 usess
->consumer
->enabled
) {
1047 /* For each consumer socket, send relayd sockets */
1048 lttng::urcu::read_lock_guard read_lock
;
1050 cds_lfht_for_each_entry (
1051 usess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1052 pthread_mutex_lock(socket
->lock
);
1053 ret
= send_consumer_relayd_sockets(
1060 session
->live_timer
,
1061 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
1062 session
->creation_time
,
1063 session
->name_contains_creation_time
);
1064 pthread_mutex_unlock(socket
->lock
);
1065 if (ret
!= LTTNG_OK
) {
1068 /* Session is now ready for network streaming. */
1069 session
->net_handle
= 1;
1072 session
->consumer
->relay_major_version
= usess
->consumer
->relay_major_version
;
1073 session
->consumer
->relay_minor_version
= usess
->consumer
->relay_minor_version
;
1074 session
->consumer
->relay_allows_clear
= usess
->consumer
->relay_allows_clear
;
1077 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
&&
1078 ksess
->consumer
->enabled
) {
1079 lttng::urcu::read_lock_guard read_lock
;
1081 cds_lfht_for_each_entry (
1082 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1083 pthread_mutex_lock(socket
->lock
);
1084 ret
= send_consumer_relayd_sockets(
1091 session
->live_timer
,
1092 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
1093 session
->creation_time
,
1094 session
->name_contains_creation_time
);
1095 pthread_mutex_unlock(socket
->lock
);
1096 if (ret
!= LTTNG_OK
) {
1099 /* Session is now ready for network streaming. */
1100 session
->net_handle
= 1;
1103 session
->consumer
->relay_major_version
= ksess
->consumer
->relay_major_version
;
1104 session
->consumer
->relay_minor_version
= ksess
->consumer
->relay_minor_version
;
1105 session
->consumer
->relay_allows_clear
= ksess
->consumer
->relay_allows_clear
;
1113 * Start a kernel session by opening all necessary streams.
1115 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1118 struct ltt_kernel_channel
*kchan
;
1120 /* Open kernel metadata */
1121 if (ksess
->metadata
== nullptr && ksess
->output_traces
) {
1122 ret
= kernel_open_metadata(ksess
);
1124 ret
= LTTNG_ERR_KERN_META_FAIL
;
1129 /* Open kernel metadata stream */
1130 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1131 ret
= kernel_open_metadata_stream(ksess
);
1133 ERR("Kernel create metadata stream failed");
1134 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1139 /* For each channel */
1140 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1141 if (kchan
->stream_count
== 0) {
1142 ret
= kernel_open_channel_stream(kchan
);
1144 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1147 /* Update the stream global counter */
1148 ksess
->stream_count_global
+= ret
;
1152 /* Setup kernel consumer socket and send fds to it */
1153 ret
= init_kernel_tracing(ksess
);
1155 ret
= LTTNG_ERR_KERN_START_FAIL
;
1159 /* This start the kernel tracing */
1160 ret
= kernel_start_session(ksess
);
1162 ret
= LTTNG_ERR_KERN_START_FAIL
;
1166 /* Quiescent wait after starting trace */
1167 kernel_wait_quiescent();
1169 ksess
->active
= true;
1177 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1179 struct ltt_kernel_channel
*kchan
;
1180 bool error_occurred
= false;
1183 if (!ksess
|| !ksess
->active
) {
1186 DBG("Stopping kernel tracing");
1188 ret
= kernel_stop_session(ksess
);
1190 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1194 kernel_wait_quiescent();
1196 /* Flush metadata after stopping (if exists) */
1197 if (ksess
->metadata_stream_fd
>= 0) {
1198 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1200 ERR("Kernel metadata flush failed");
1201 error_occurred
= true;
1205 /* Flush all buffers after stopping */
1206 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1207 ret
= kernel_flush_buffer(kchan
);
1209 ERR("Kernel flush buffer error");
1210 error_occurred
= true;
1214 ksess
->active
= false;
1215 if (error_occurred
) {
1216 ret
= LTTNG_ERR_UNK
;
1225 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1227 int cmd_disable_channel(struct ltt_session
*session
,
1228 enum lttng_domain_type domain
,
1232 struct ltt_ust_session
*usess
;
1234 usess
= session
->ust_session
;
1236 lttng::urcu::read_lock_guard read_lock
;
1239 case LTTNG_DOMAIN_KERNEL
:
1241 ret
= channel_kernel_disable(session
->kernel_session
, channel_name
);
1242 if (ret
!= LTTNG_OK
) {
1246 kernel_wait_quiescent();
1249 case LTTNG_DOMAIN_UST
:
1251 struct ltt_ust_channel
*uchan
;
1252 struct lttng_ht
*chan_ht
;
1254 chan_ht
= usess
->domain_global
.channels
;
1256 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1257 if (uchan
== nullptr) {
1258 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1262 ret
= channel_ust_disable(usess
, uchan
);
1263 if (ret
!= LTTNG_OK
) {
1269 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1280 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1282 * The wpipe arguments is used as a notifier for the kernel thread.
1284 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1288 ssize_t sock_recv_len
;
1289 struct lttng_channel
*channel
= nullptr;
1290 struct lttng_buffer_view view
;
1291 struct lttng_dynamic_buffer channel_buffer
;
1292 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1294 lttng_dynamic_buffer_init(&channel_buffer
);
1295 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1296 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1298 ret
= LTTNG_ERR_NOMEM
;
1302 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
, channel_len
);
1303 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1304 ERR("Failed to receive \"enable channel\" command payload");
1305 ret
= LTTNG_ERR_INVALID
;
1309 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1310 if (!lttng_buffer_view_is_valid(&view
)) {
1311 ret
= LTTNG_ERR_INVALID
;
1315 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1316 ERR("Invalid channel payload received in \"enable channel\" command");
1317 ret
= LTTNG_ERR_INVALID
;
1321 ret
= cmd_enable_channel_internal(cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1324 lttng_dynamic_buffer_reset(&channel_buffer
);
1325 lttng_channel_destroy(channel
);
1329 static enum lttng_error_code
cmd_enable_channel_internal(struct ltt_session
*session
,
1330 const struct lttng_domain
*domain
,
1331 const struct lttng_channel
*_attr
,
1334 enum lttng_error_code ret_code
;
1335 struct ltt_ust_session
*usess
= session
->ust_session
;
1336 struct lttng_ht
*chan_ht
;
1338 struct lttng_channel
*attr
= nullptr;
1340 LTTNG_ASSERT(session
);
1341 LTTNG_ASSERT(_attr
);
1342 LTTNG_ASSERT(domain
);
1344 lttng::urcu::read_lock_guard read_lock
;
1346 attr
= lttng_channel_copy(_attr
);
1348 ret_code
= LTTNG_ERR_NOMEM
;
1352 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1354 /* Validate channel name */
1355 if (attr
->name
[0] == '.' || memchr(attr
->name
, '/', len
) != nullptr) {
1356 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1360 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1363 * If the session is a live session, remove the switch timer, the
1364 * live timer does the same thing but sends also synchronisation
1365 * beacons for inactive streams.
1367 if (session
->live_timer
> 0) {
1368 attr
->attr
.live_timer_interval
= session
->live_timer
;
1369 attr
->attr
.switch_timer_interval
= 0;
1372 /* Check for feature support */
1373 switch (domain
->type
) {
1374 case LTTNG_DOMAIN_KERNEL
:
1376 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1377 /* Sampling position of buffer is not supported */
1378 WARN("Kernel tracer does not support buffer monitoring. "
1379 "Setting the monitor interval timer to 0 "
1380 "(disabled) for channel '%s' of session '%s'",
1383 lttng_channel_set_monitor_timer_interval(attr
, 0);
1387 case LTTNG_DOMAIN_UST
:
1389 case LTTNG_DOMAIN_JUL
:
1390 case LTTNG_DOMAIN_LOG4J
:
1391 case LTTNG_DOMAIN_PYTHON
:
1392 if (!agent_tracing_is_enabled()) {
1393 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1394 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1399 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1403 switch (domain
->type
) {
1404 case LTTNG_DOMAIN_KERNEL
:
1406 struct ltt_kernel_channel
*kchan
;
1408 kchan
= trace_kernel_get_channel_by_name(attr
->name
, session
->kernel_session
);
1409 if (kchan
== nullptr) {
1411 * Don't try to create a channel if the session has been started at
1412 * some point in time before. The tracer does not allow it.
1414 if (session
->has_been_started
) {
1415 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1419 if (session
->snapshot
.nb_output
> 0 || session
->snapshot_mode
) {
1420 /* Enforce mmap output for snapshot sessions. */
1421 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1423 ret_code
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1424 if (attr
->name
[0] != '\0') {
1425 session
->kernel_session
->has_non_default_channel
= 1;
1428 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1431 if (ret_code
!= LTTNG_OK
) {
1435 kernel_wait_quiescent();
1438 case LTTNG_DOMAIN_UST
:
1439 case LTTNG_DOMAIN_JUL
:
1440 case LTTNG_DOMAIN_LOG4J
:
1441 case LTTNG_DOMAIN_PYTHON
:
1443 struct ltt_ust_channel
*uchan
;
1448 * Current agent implementation limitations force us to allow
1449 * only one channel at once in "agent" subdomains. Each
1450 * subdomain has a default channel name which must be strictly
1453 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1454 if (strncmp(attr
->name
,
1455 DEFAULT_JUL_CHANNEL_NAME
,
1456 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1457 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1460 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1461 if (strncmp(attr
->name
,
1462 DEFAULT_LOG4J_CHANNEL_NAME
,
1463 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1464 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1467 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1468 if (strncmp(attr
->name
,
1469 DEFAULT_PYTHON_CHANNEL_NAME
,
1470 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1471 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1476 chan_ht
= usess
->domain_global
.channels
;
1478 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1479 if (uchan
== nullptr) {
1481 * Don't try to create a channel if the session has been started at
1482 * some point in time before. The tracer does not allow it.
1484 if (session
->has_been_started
) {
1485 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1489 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1490 if (attr
->name
[0] != '\0') {
1491 usess
->has_non_default_channel
= 1;
1494 ret_code
= channel_ust_enable(usess
, uchan
);
1499 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1503 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1504 session
->has_non_mmap_channel
= true;
1508 lttng_channel_destroy(attr
);
1512 enum lttng_error_code
1513 cmd_process_attr_tracker_get_tracking_policy(struct ltt_session
*session
,
1514 enum lttng_domain_type domain
,
1515 enum lttng_process_attr process_attr
,
1516 enum lttng_tracking_policy
*policy
)
1518 enum lttng_error_code ret_code
= LTTNG_OK
;
1519 const struct process_attr_tracker
*tracker
;
1522 case LTTNG_DOMAIN_KERNEL
:
1523 if (!session
->kernel_session
) {
1524 ret_code
= LTTNG_ERR_INVALID
;
1527 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1529 case LTTNG_DOMAIN_UST
:
1530 if (!session
->ust_session
) {
1531 ret_code
= LTTNG_ERR_INVALID
;
1534 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1537 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1541 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1543 ret_code
= LTTNG_ERR_INVALID
;
1549 enum lttng_error_code
1550 cmd_process_attr_tracker_set_tracking_policy(struct ltt_session
*session
,
1551 enum lttng_domain_type domain
,
1552 enum lttng_process_attr process_attr
,
1553 enum lttng_tracking_policy policy
)
1555 enum lttng_error_code ret_code
= LTTNG_OK
;
1558 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1559 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1560 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1563 ret_code
= LTTNG_ERR_INVALID
;
1568 case LTTNG_DOMAIN_KERNEL
:
1569 if (!session
->kernel_session
) {
1570 ret_code
= LTTNG_ERR_INVALID
;
1573 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1574 session
->kernel_session
, process_attr
, policy
);
1576 case LTTNG_DOMAIN_UST
:
1577 if (!session
->ust_session
) {
1578 ret_code
= LTTNG_ERR_INVALID
;
1581 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1582 session
->ust_session
, process_attr
, policy
);
1585 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1592 enum lttng_error_code
1593 cmd_process_attr_tracker_inclusion_set_add_value(struct ltt_session
*session
,
1594 enum lttng_domain_type domain
,
1595 enum lttng_process_attr process_attr
,
1596 const struct process_attr_value
*value
)
1598 enum lttng_error_code ret_code
= LTTNG_OK
;
1601 case LTTNG_DOMAIN_KERNEL
:
1602 if (!session
->kernel_session
) {
1603 ret_code
= LTTNG_ERR_INVALID
;
1606 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1607 session
->kernel_session
, process_attr
, value
);
1609 case LTTNG_DOMAIN_UST
:
1610 if (!session
->ust_session
) {
1611 ret_code
= LTTNG_ERR_INVALID
;
1614 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1615 session
->ust_session
, process_attr
, value
);
1618 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1625 enum lttng_error_code
1626 cmd_process_attr_tracker_inclusion_set_remove_value(struct ltt_session
*session
,
1627 enum lttng_domain_type domain
,
1628 enum lttng_process_attr process_attr
,
1629 const struct process_attr_value
*value
)
1631 enum lttng_error_code ret_code
= LTTNG_OK
;
1634 case LTTNG_DOMAIN_KERNEL
:
1635 if (!session
->kernel_session
) {
1636 ret_code
= LTTNG_ERR_INVALID
;
1639 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1640 session
->kernel_session
, process_attr
, value
);
1642 case LTTNG_DOMAIN_UST
:
1643 if (!session
->ust_session
) {
1644 ret_code
= LTTNG_ERR_INVALID
;
1647 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1648 session
->ust_session
, process_attr
, value
);
1651 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1658 enum lttng_error_code
1659 cmd_process_attr_tracker_get_inclusion_set(struct ltt_session
*session
,
1660 enum lttng_domain_type domain
,
1661 enum lttng_process_attr process_attr
,
1662 struct lttng_process_attr_values
**values
)
1664 enum lttng_error_code ret_code
= LTTNG_OK
;
1665 const struct process_attr_tracker
*tracker
;
1666 enum process_attr_tracker_status status
;
1669 case LTTNG_DOMAIN_KERNEL
:
1670 if (!session
->kernel_session
) {
1671 ret_code
= LTTNG_ERR_INVALID
;
1674 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1676 case LTTNG_DOMAIN_UST
:
1677 if (!session
->ust_session
) {
1678 ret_code
= LTTNG_ERR_INVALID
;
1681 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1684 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1689 ret_code
= LTTNG_ERR_INVALID
;
1693 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1695 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1696 ret_code
= LTTNG_OK
;
1698 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1699 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1701 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1702 ret_code
= LTTNG_ERR_NOMEM
;
1705 ret_code
= LTTNG_ERR_UNK
;
1714 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1716 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1717 struct lttng_event
*event
,
1718 char *filter_expression
,
1719 struct lttng_bytecode
*bytecode
,
1720 struct lttng_event_exclusion
*exclusion
)
1723 const char *event_name
;
1724 const struct ltt_session
*session
= cmd_ctx
->session
;
1725 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1726 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1728 DBG("Disable event command for event \'%s\'", event
->name
);
1731 * Filter and exclusions are simply not handled by the
1732 * disable event command at this time.
1736 (void) filter_expression
;
1739 /* Ignore the presence of filter or exclusion for the event */
1741 event
->exclusion
= 0;
1743 event_name
= event
->name
;
1745 lttng::urcu::read_lock_guard read_lock
;
1747 /* Error out on unhandled search criteria */
1748 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
|| event
->pid
||
1749 event
->filter
|| event
->exclusion
) {
1750 ret
= LTTNG_ERR_UNK
;
1755 case LTTNG_DOMAIN_KERNEL
:
1757 struct ltt_kernel_channel
*kchan
;
1758 struct ltt_kernel_session
*ksess
;
1760 ksess
= session
->kernel_session
;
1763 * If a non-default channel has been created in the
1764 * session, explicitely require that -c chan_name needs
1767 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1768 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1772 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1773 if (kchan
== nullptr) {
1774 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1778 switch (event
->type
) {
1779 case LTTNG_EVENT_ALL
:
1780 case LTTNG_EVENT_TRACEPOINT
:
1781 case LTTNG_EVENT_SYSCALL
:
1782 case LTTNG_EVENT_PROBE
:
1783 case LTTNG_EVENT_FUNCTION
:
1784 case LTTNG_EVENT_FUNCTION_ENTRY
: /* fall-through */
1785 if (event_name
[0] == '\0') {
1786 ret
= event_kernel_disable_event(kchan
, nullptr, event
->type
);
1788 ret
= event_kernel_disable_event(kchan
, event_name
, event
->type
);
1790 if (ret
!= LTTNG_OK
) {
1795 ret
= LTTNG_ERR_UNK
;
1799 kernel_wait_quiescent();
1802 case LTTNG_DOMAIN_UST
:
1804 struct ltt_ust_channel
*uchan
;
1805 struct ltt_ust_session
*usess
;
1807 usess
= session
->ust_session
;
1809 if (validate_ust_event_name(event_name
)) {
1810 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1815 * If a non-default channel has been created in the
1816 * session, explicitly require that -c chan_name needs
1819 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1820 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1824 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
1825 if (uchan
== nullptr) {
1826 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1830 switch (event
->type
) {
1831 case LTTNG_EVENT_ALL
:
1833 * An empty event name means that everything
1834 * should be disabled.
1836 if (event
->name
[0] == '\0') {
1837 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1839 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1841 if (ret
!= LTTNG_OK
) {
1846 ret
= LTTNG_ERR_UNK
;
1850 DBG3("Disable UST event %s in channel %s completed", event_name
, channel_name
);
1853 case LTTNG_DOMAIN_LOG4J
:
1854 case LTTNG_DOMAIN_JUL
:
1855 case LTTNG_DOMAIN_PYTHON
:
1858 struct ltt_ust_session
*usess
= session
->ust_session
;
1860 LTTNG_ASSERT(usess
);
1862 switch (event
->type
) {
1863 case LTTNG_EVENT_ALL
:
1866 ret
= LTTNG_ERR_UNK
;
1870 agt
= trace_ust_find_agent(usess
, domain
);
1872 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1876 * An empty event name means that everything
1877 * should be disabled.
1879 if (event
->name
[0] == '\0') {
1880 ret
= event_agent_disable_all(usess
, agt
);
1882 ret
= event_agent_disable(usess
, agt
, event_name
);
1884 if (ret
!= LTTNG_OK
) {
1891 ret
= LTTNG_ERR_UND
;
1901 free(filter_expression
);
1906 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1908 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1909 const struct lttng_event_context
*event_context
,
1912 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1913 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1914 const struct ltt_session
*session
= cmd_ctx
->session
;
1915 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1918 * Don't try to add a context if the session has been started at
1919 * some point in time before. The tracer does not allow it and would
1920 * result in a corrupted trace.
1922 if (cmd_ctx
->session
->has_been_started
) {
1923 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1928 case LTTNG_DOMAIN_KERNEL
:
1929 LTTNG_ASSERT(session
->kernel_session
);
1931 if (session
->kernel_session
->channel_count
== 0) {
1932 /* Create default channel */
1933 ret
= channel_kernel_create(session
->kernel_session
, nullptr, kwpipe
);
1934 if (ret
!= LTTNG_OK
) {
1937 chan_kern_created
= 1;
1939 /* Add kernel context to kernel tracer */
1940 ret
= context_kernel_add(session
->kernel_session
, event_context
, channel_name
);
1941 if (ret
!= LTTNG_OK
) {
1945 case LTTNG_DOMAIN_JUL
:
1946 case LTTNG_DOMAIN_LOG4J
:
1949 * Validate channel name.
1950 * If no channel name is given and the domain is JUL or LOG4J,
1951 * set it to the appropriate domain-specific channel name. If
1952 * a name is provided but does not match the expexted channel
1953 * name, return an error.
1955 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1956 strcmp(channel_name
, DEFAULT_JUL_CHANNEL_NAME
) != 0) {
1957 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1959 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1960 strcmp(channel_name
, DEFAULT_LOG4J_CHANNEL_NAME
) != 0) {
1961 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1966 case LTTNG_DOMAIN_UST
:
1968 struct ltt_ust_session
*usess
= session
->ust_session
;
1969 unsigned int chan_count
;
1971 LTTNG_ASSERT(usess
);
1973 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1974 if (chan_count
== 0) {
1975 struct lttng_channel
*attr
;
1976 /* Create default channel */
1977 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1978 if (attr
== nullptr) {
1979 ret
= LTTNG_ERR_FATAL
;
1983 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1984 if (ret
!= LTTNG_OK
) {
1988 channel_attr_destroy(attr
);
1989 chan_ust_created
= 1;
1992 ret
= context_ust_add(usess
, domain
, event_context
, channel_name
);
1993 if (ret
!= LTTNG_OK
) {
1999 ret
= LTTNG_ERR_UND
;
2007 if (chan_kern_created
) {
2008 struct ltt_kernel_channel
*kchan
= trace_kernel_get_channel_by_name(
2009 DEFAULT_CHANNEL_NAME
, session
->kernel_session
);
2010 /* Created previously, this should NOT fail. */
2011 LTTNG_ASSERT(kchan
);
2012 kernel_destroy_channel(kchan
);
2015 if (chan_ust_created
) {
2016 struct ltt_ust_channel
*uchan
= trace_ust_find_channel_by_name(
2017 session
->ust_session
->domain_global
.channels
, DEFAULT_CHANNEL_NAME
);
2018 /* Created previously, this should NOT fail. */
2019 LTTNG_ASSERT(uchan
);
2020 /* Remove from the channel list of the session. */
2021 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
, uchan
);
2022 trace_ust_destroy_channel(uchan
);
2028 static inline bool name_starts_with(const char *name
, const char *prefix
)
2030 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2032 return !strncmp(name
, prefix
, max_cmp_len
);
2035 /* Perform userspace-specific event name validation */
2036 static int validate_ust_event_name(const char *name
)
2046 * Check name against all internal UST event component namespaces used
2049 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2050 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2051 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2060 * Internal version of cmd_enable_event() with a supplemental
2061 * "internal_event" flag which is used to enable internal events which should
2062 * be hidden from clients. Such events are used in the agent implementation to
2063 * enable the events through which all "agent" events are funeled.
2065 static int _cmd_enable_event(struct ltt_session
*session
,
2066 const struct lttng_domain
*domain
,
2068 struct lttng_event
*event
,
2069 char *filter_expression
,
2070 struct lttng_bytecode
*filter
,
2071 struct lttng_event_exclusion
*exclusion
,
2073 bool internal_event
)
2075 int ret
= 0, channel_created
= 0;
2076 struct lttng_channel
*attr
= nullptr;
2078 LTTNG_ASSERT(session
);
2079 LTTNG_ASSERT(event
);
2080 LTTNG_ASSERT(channel_name
);
2082 /* If we have a filter, we must have its filter expression */
2083 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2085 /* Normalize event name as a globbing pattern */
2086 strutils_normalize_star_glob_pattern(event
->name
);
2088 /* Normalize exclusion names as globbing patterns */
2092 for (i
= 0; i
< exclusion
->count
; i
++) {
2093 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2095 strutils_normalize_star_glob_pattern(name
);
2099 lttng::urcu::read_lock_guard read_lock
;
2101 switch (domain
->type
) {
2102 case LTTNG_DOMAIN_KERNEL
:
2104 struct ltt_kernel_channel
*kchan
;
2107 * If a non-default channel has been created in the
2108 * session, explicitely require that -c chan_name needs
2111 if (session
->kernel_session
->has_non_default_channel
&& channel_name
[0] == '\0') {
2112 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2116 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2117 if (kchan
== nullptr) {
2118 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
, LTTNG_BUFFER_GLOBAL
);
2119 if (attr
== nullptr) {
2120 ret
= LTTNG_ERR_FATAL
;
2123 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2124 ret
= LTTNG_ERR_INVALID
;
2128 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2129 if (ret
!= LTTNG_OK
) {
2132 channel_created
= 1;
2135 /* Get the newly created kernel channel pointer */
2136 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2137 if (kchan
== nullptr) {
2138 /* This sould not happen... */
2139 ret
= LTTNG_ERR_FATAL
;
2143 switch (event
->type
) {
2144 case LTTNG_EVENT_ALL
:
2146 char *filter_expression_a
= nullptr;
2147 struct lttng_bytecode
*filter_a
= nullptr;
2150 * We need to duplicate filter_expression and filter,
2151 * because ownership is passed to first enable
2154 if (filter_expression
) {
2155 filter_expression_a
= strdup(filter_expression
);
2156 if (!filter_expression_a
) {
2157 ret
= LTTNG_ERR_FATAL
;
2162 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2164 free(filter_expression_a
);
2165 ret
= LTTNG_ERR_FATAL
;
2168 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2170 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2171 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2172 /* We have passed ownership */
2173 filter_expression
= nullptr;
2175 if (ret
!= LTTNG_OK
) {
2176 if (channel_created
) {
2177 /* Let's not leak a useless channel. */
2178 kernel_destroy_channel(kchan
);
2180 free(filter_expression_a
);
2184 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2185 ret
= event_kernel_enable_event(
2186 kchan
, event
, filter_expression_a
, filter_a
);
2187 /* We have passed ownership */
2188 filter_expression_a
= nullptr;
2190 if (ret
!= LTTNG_OK
) {
2195 case LTTNG_EVENT_PROBE
:
2196 case LTTNG_EVENT_USERSPACE_PROBE
:
2197 case LTTNG_EVENT_FUNCTION
:
2198 case LTTNG_EVENT_FUNCTION_ENTRY
:
2199 case LTTNG_EVENT_TRACEPOINT
:
2200 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2201 /* We have passed ownership */
2202 filter_expression
= nullptr;
2204 if (ret
!= LTTNG_OK
) {
2205 if (channel_created
) {
2206 /* Let's not leak a useless channel. */
2207 kernel_destroy_channel(kchan
);
2212 case LTTNG_EVENT_SYSCALL
:
2213 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2214 /* We have passed ownership */
2215 filter_expression
= nullptr;
2217 if (ret
!= LTTNG_OK
) {
2222 ret
= LTTNG_ERR_UNK
;
2226 kernel_wait_quiescent();
2229 case LTTNG_DOMAIN_UST
:
2231 struct ltt_ust_channel
*uchan
;
2232 struct ltt_ust_session
*usess
= session
->ust_session
;
2234 LTTNG_ASSERT(usess
);
2237 * If a non-default channel has been created in the
2238 * session, explicitely require that -c chan_name needs
2241 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2242 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2246 /* Get channel from global UST domain */
2247 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
2248 if (uchan
== nullptr) {
2249 /* Create default channel */
2250 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, usess
->buffer_type
);
2251 if (attr
== nullptr) {
2252 ret
= LTTNG_ERR_FATAL
;
2255 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2256 ret
= LTTNG_ERR_INVALID
;
2260 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2261 if (ret
!= LTTNG_OK
) {
2265 /* Get the newly created channel reference back */
2266 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2268 LTTNG_ASSERT(uchan
);
2271 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2273 * Don't allow users to add UST events to channels which
2274 * are assigned to a userspace subdomain (JUL, Log4J,
2277 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2281 if (!internal_event
) {
2283 * Ensure the event name is not reserved for internal
2286 ret
= validate_ust_event_name(event
->name
);
2288 WARN("Userspace event name %s failed validation.", event
->name
);
2289 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2294 /* At this point, the session and channel exist on the tracer */
2295 ret
= event_ust_enable_tracepoint(
2296 usess
, uchan
, event
, filter_expression
, filter
, exclusion
, internal_event
);
2297 /* We have passed ownership */
2298 filter_expression
= nullptr;
2300 exclusion
= nullptr;
2301 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2302 goto already_enabled
;
2303 } else if (ret
!= LTTNG_OK
) {
2308 case LTTNG_DOMAIN_LOG4J
:
2309 case LTTNG_DOMAIN_JUL
:
2310 case LTTNG_DOMAIN_PYTHON
:
2312 const char *default_event_name
, *default_chan_name
;
2314 struct lttng_event uevent
;
2315 struct lttng_domain tmp_dom
;
2316 struct ltt_ust_session
*usess
= session
->ust_session
;
2318 LTTNG_ASSERT(usess
);
2320 if (!agent_tracing_is_enabled()) {
2321 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2322 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2326 agt
= trace_ust_find_agent(usess
, domain
->type
);
2328 agt
= agent_create(domain
->type
);
2330 ret
= LTTNG_ERR_NOMEM
;
2333 agent_add(agt
, usess
->agents
);
2336 /* Create the default tracepoint. */
2337 memset(&uevent
, 0, sizeof(uevent
));
2338 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2339 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2340 uevent
.loglevel
= -1;
2341 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
2342 if (!default_event_name
) {
2343 ret
= LTTNG_ERR_FATAL
;
2346 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2347 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2350 * The domain type is changed because we are about to enable the
2351 * default channel and event for the JUL domain that are hardcoded.
2352 * This happens in the UST domain.
2354 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2355 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2357 switch (domain
->type
) {
2358 case LTTNG_DOMAIN_LOG4J
:
2359 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2361 case LTTNG_DOMAIN_JUL
:
2362 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2364 case LTTNG_DOMAIN_PYTHON
:
2365 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2368 /* The switch/case we are in makes this impossible */
2373 char *filter_expression_copy
= nullptr;
2374 struct lttng_bytecode
*filter_copy
= nullptr;
2377 const size_t filter_size
=
2378 sizeof(struct lttng_bytecode
) + filter
->len
;
2380 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2382 ret
= LTTNG_ERR_NOMEM
;
2385 memcpy(filter_copy
, filter
, filter_size
);
2387 filter_expression_copy
= strdup(filter_expression
);
2388 if (!filter_expression
) {
2389 ret
= LTTNG_ERR_NOMEM
;
2392 if (!filter_expression_copy
|| !filter_copy
) {
2393 free(filter_expression_copy
);
2399 ret
= cmd_enable_event_internal(session
,
2401 (char *) default_chan_name
,
2403 filter_expression_copy
,
2409 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2410 goto already_enabled
;
2411 } else if (ret
!= LTTNG_OK
) {
2415 /* The wild card * means that everything should be enabled. */
2416 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2417 ret
= event_agent_enable_all(usess
, agt
, event
, filter
, filter_expression
);
2419 ret
= event_agent_enable(usess
, agt
, event
, filter
, filter_expression
);
2422 filter_expression
= nullptr;
2423 if (ret
!= LTTNG_OK
) {
2430 ret
= LTTNG_ERR_UND
;
2438 free(filter_expression
);
2441 channel_attr_destroy(attr
);
2446 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2447 * We own filter, exclusion, and filter_expression.
2449 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2450 struct lttng_event
*event
,
2451 char *filter_expression
,
2452 struct lttng_event_exclusion
*exclusion
,
2453 struct lttng_bytecode
*bytecode
,
2458 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2460 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2463 * The ownership of the following parameters is transferred to
2464 * _cmd_enable_event:
2466 * - filter_expression,
2470 ret
= _cmd_enable_event(cmd_ctx
->session
,
2472 cmd_ctx
->lsm
.u
.enable
.channel_name
,
2479 filter_expression
= nullptr;
2481 exclusion
= nullptr;
2486 * Enable an event which is internal to LTTng. An internal should
2487 * never be made visible to clients and are immune to checks such as
2490 static int cmd_enable_event_internal(struct ltt_session
*session
,
2491 const struct lttng_domain
*domain
,
2493 struct lttng_event
*event
,
2494 char *filter_expression
,
2495 struct lttng_bytecode
*filter
,
2496 struct lttng_event_exclusion
*exclusion
,
2499 return _cmd_enable_event(session
,
2511 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2513 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2514 struct lttng_payload
*reply_payload
)
2516 enum lttng_error_code ret_code
;
2518 ssize_t i
, nb_events
= 0;
2519 struct lttng_event
*events
= nullptr;
2520 struct lttcomm_list_command_header reply_command_header
= {};
2521 size_t reply_command_header_offset
;
2523 assert(reply_payload
);
2525 /* Reserve space for command reply header. */
2526 reply_command_header_offset
= reply_payload
->buffer
.size
;
2527 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2528 reply_command_header_offset
+
2529 sizeof(struct lttcomm_list_command_header
));
2531 ret_code
= LTTNG_ERR_NOMEM
;
2536 case LTTNG_DOMAIN_KERNEL
:
2537 nb_events
= kernel_list_events(&events
);
2538 if (nb_events
< 0) {
2539 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2543 case LTTNG_DOMAIN_UST
:
2544 nb_events
= ust_app_list_events(&events
);
2545 if (nb_events
< 0) {
2546 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2550 case LTTNG_DOMAIN_LOG4J
:
2551 case LTTNG_DOMAIN_JUL
:
2552 case LTTNG_DOMAIN_PYTHON
:
2553 nb_events
= agent_list_events(&events
, domain
);
2554 if (nb_events
< 0) {
2555 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2560 ret_code
= LTTNG_ERR_UND
;
2564 for (i
= 0; i
< nb_events
; i
++) {
2565 ret
= lttng_event_serialize(
2566 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2568 ret_code
= LTTNG_ERR_NOMEM
;
2573 if (nb_events
> UINT32_MAX
) {
2574 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2575 ret_code
= LTTNG_ERR_OVERFLOW
;
2579 /* Update command reply header. */
2580 reply_command_header
.count
= (uint32_t) nb_events
;
2581 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2582 &reply_command_header
,
2583 sizeof(reply_command_header
));
2585 ret_code
= LTTNG_OK
;
2592 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2594 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2595 struct lttng_payload
*reply
)
2597 enum lttng_error_code ret_code
;
2599 unsigned int i
, nb_fields
;
2600 struct lttng_event_field
*fields
= nullptr;
2601 struct lttcomm_list_command_header reply_command_header
= {};
2602 size_t reply_command_header_offset
;
2606 /* Reserve space for command reply header. */
2607 reply_command_header_offset
= reply
->buffer
.size
;
2608 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2609 reply_command_header_offset
+
2610 sizeof(struct lttcomm_list_command_header
));
2612 ret_code
= LTTNG_ERR_NOMEM
;
2617 case LTTNG_DOMAIN_UST
:
2618 ret
= ust_app_list_event_fields(&fields
);
2620 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2625 case LTTNG_DOMAIN_KERNEL
:
2626 default: /* fall-through */
2627 ret_code
= LTTNG_ERR_UND
;
2633 for (i
= 0; i
< nb_fields
; i
++) {
2634 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2636 ret_code
= LTTNG_ERR_NOMEM
;
2641 if (nb_fields
> UINT32_MAX
) {
2642 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2643 ret_code
= LTTNG_ERR_OVERFLOW
;
2647 /* Update command reply header. */
2648 reply_command_header
.count
= (uint32_t) nb_fields
;
2650 memcpy(reply
->buffer
.data
+ reply_command_header_offset
,
2651 &reply_command_header
,
2652 sizeof(reply_command_header
));
2654 ret_code
= LTTNG_OK
;
2661 enum lttng_error_code
cmd_list_syscalls(struct lttng_payload
*reply_payload
)
2663 enum lttng_error_code ret_code
;
2664 ssize_t nb_events
, i
;
2666 struct lttng_event
*events
= nullptr;
2667 struct lttcomm_list_command_header reply_command_header
= {};
2668 size_t reply_command_header_offset
;
2670 assert(reply_payload
);
2672 /* Reserve space for command reply header. */
2673 reply_command_header_offset
= reply_payload
->buffer
.size
;
2674 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2675 reply_command_header_offset
+
2676 sizeof(struct lttcomm_list_command_header
));
2678 ret_code
= LTTNG_ERR_NOMEM
;
2682 nb_events
= syscall_table_list(&events
);
2683 if (nb_events
< 0) {
2684 ret_code
= (enum lttng_error_code
) - nb_events
;
2688 for (i
= 0; i
< nb_events
; i
++) {
2689 ret
= lttng_event_serialize(
2690 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2692 ret_code
= LTTNG_ERR_NOMEM
;
2697 if (nb_events
> UINT32_MAX
) {
2698 ERR("Syscall count would overflow the syscall listing command's reply");
2699 ret_code
= LTTNG_ERR_OVERFLOW
;
2703 /* Update command reply header. */
2704 reply_command_header
.count
= (uint32_t) nb_events
;
2705 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2706 &reply_command_header
,
2707 sizeof(reply_command_header
));
2709 ret_code
= LTTNG_OK
;
2716 * Command LTTNG_START_TRACE processed by the client thread.
2718 * Called with session mutex held.
2720 int cmd_start_trace(struct ltt_session
*session
)
2722 enum lttng_error_code ret
;
2723 unsigned long nb_chan
= 0;
2724 struct ltt_kernel_session
*ksession
;
2725 struct ltt_ust_session
*usess
;
2726 const bool session_rotated_after_last_stop
= session
->rotated_after_last_stop
;
2727 const bool session_cleared_after_last_stop
= session
->cleared_after_last_stop
;
2729 LTTNG_ASSERT(session
);
2731 /* Ease our life a bit ;) */
2732 ksession
= session
->kernel_session
;
2733 usess
= session
->ust_session
;
2735 /* Is the session already started? */
2736 if (session
->active
) {
2737 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2738 /* Perform nothing */
2742 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2743 !session
->current_trace_chunk
) {
2745 * A rotation was launched while the session was stopped and
2746 * it has not been completed yet. It is not possible to start
2747 * the session since starting the session here would require a
2748 * rotation from "NULL" to a new trace chunk. That rotation
2749 * would overlap with the ongoing rotation, which is not
2752 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2754 ret
= LTTNG_ERR_ROTATION_PENDING
;
2759 * Starting a session without channel is useless since after that it's not
2760 * possible to enable channel thus inform the client.
2762 if (usess
&& usess
->domain_global
.channels
) {
2763 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2766 nb_chan
+= ksession
->channel_count
;
2769 ret
= LTTNG_ERR_NO_CHANNEL
;
2773 session
->active
= true;
2774 session
->rotated_after_last_stop
= false;
2775 session
->cleared_after_last_stop
= false;
2776 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2777 if (!session
->has_been_started
) {
2778 struct lttng_trace_chunk
*trace_chunk
;
2780 DBG("Creating initial trace chunk of session \"%s\"", session
->name
);
2782 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
2784 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2787 LTTNG_ASSERT(!session
->current_trace_chunk
);
2788 ret
= (lttng_error_code
) session_set_trace_chunk(
2789 session
, trace_chunk
, nullptr);
2790 lttng_trace_chunk_put(trace_chunk
);
2792 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2796 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2799 * Rotate existing streams into the new chunk.
2800 * This is a "quiet" rotation has no client has
2801 * explicitly requested this operation.
2803 * There is also no need to wait for the rotation
2804 * to complete as it will happen immediately. No data
2805 * was produced as the session was stopped, so the
2806 * rotation should happen on reception of the command.
2808 ret
= (lttng_error_code
) cmd_rotate_session(
2809 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2810 if (ret
!= LTTNG_OK
) {
2816 /* Kernel tracing */
2817 if (ksession
!= nullptr) {
2818 DBG("Start kernel tracing session %s", session
->name
);
2819 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2820 if (ret
!= LTTNG_OK
) {
2825 /* Flag session that trace should start automatically */
2827 int int_ret
= ust_app_start_trace_all(usess
);
2830 ret
= LTTNG_ERR_UST_START_FAIL
;
2836 * Open a packet in every stream of the session to ensure that viewers
2837 * can correctly identify the boundaries of the periods during which
2838 * tracing was active for this session.
2840 ret
= session_open_packets(session
);
2841 if (ret
!= LTTNG_OK
) {
2846 * Clear the flag that indicates that a rotation was done while the
2847 * session was stopped.
2849 session
->rotated_after_last_stop
= false;
2851 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2852 int int_ret
= timer_session_rotation_schedule_timer_start(
2853 session
, session
->rotate_timer_period
);
2856 ERR("Failed to enable rotate timer");
2857 ret
= LTTNG_ERR_UNK
;
2865 if (ret
== LTTNG_OK
) {
2866 /* Flag this after a successful start. */
2867 session
->has_been_started
= true;
2869 session
->active
= false;
2870 /* Restore initial state on error. */
2871 session
->rotated_after_last_stop
= session_rotated_after_last_stop
;
2872 session
->cleared_after_last_stop
= session_cleared_after_last_stop
;
2879 * Command LTTNG_STOP_TRACE processed by the client thread.
2881 int cmd_stop_trace(struct ltt_session
*session
)
2884 struct ltt_kernel_session
*ksession
;
2885 struct ltt_ust_session
*usess
;
2887 LTTNG_ASSERT(session
);
2889 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2891 ksession
= session
->kernel_session
;
2892 usess
= session
->ust_session
;
2894 /* Session is not active. Skip everything and inform the client. */
2895 if (!session
->active
) {
2896 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2900 ret
= stop_kernel_session(ksession
);
2901 if (ret
!= LTTNG_OK
) {
2905 if (usess
&& usess
->active
) {
2906 ret
= ust_app_stop_trace_all(usess
);
2908 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2913 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2914 /* Flag inactive after a successful stop. */
2915 session
->active
= false;
2923 * Set the base_path of the session only if subdir of a control uris is set.
2924 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2927 set_session_base_path_from_uris(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2932 for (i
= 0; i
< nb_uri
; i
++) {
2933 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
|| uris
[i
].subdir
[0] == '\0') {
2934 /* Not interested in these URIs */
2938 if (session
->base_path
!= nullptr) {
2939 free(session
->base_path
);
2940 session
->base_path
= nullptr;
2943 /* Set session base_path */
2944 session
->base_path
= strdup(uris
[i
].subdir
);
2945 if (!session
->base_path
) {
2946 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2949 ret
= LTTNG_ERR_NOMEM
;
2952 DBG2("Setting base path \"%s\" for session \"%s\"",
2962 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2964 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2967 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2968 struct ltt_ust_session
*usess
= session
->ust_session
;
2970 LTTNG_ASSERT(session
);
2972 LTTNG_ASSERT(nb_uri
> 0);
2974 /* Can't set consumer URI if the session is active. */
2975 if (session
->active
) {
2976 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2981 * Set the session base path if any. This is done inside
2982 * cmd_set_consumer_uri to preserve backward compatibility of the
2983 * previous session creation api vs the session descriptor api.
2985 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2986 if (ret
!= LTTNG_OK
) {
2990 /* Set the "global" consumer URIs */
2991 for (i
= 0; i
< nb_uri
; i
++) {
2992 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
], LTTNG_DOMAIN_NONE
);
2993 if (ret
!= LTTNG_OK
) {
2998 /* Set UST session URIs */
2999 if (session
->ust_session
) {
3000 for (i
= 0; i
< nb_uri
; i
++) {
3001 ret
= add_uri_to_consumer(session
,
3002 session
->ust_session
->consumer
,
3005 if (ret
!= LTTNG_OK
) {
3011 /* Set kernel session URIs */
3012 if (session
->kernel_session
) {
3013 for (i
= 0; i
< nb_uri
; i
++) {
3014 ret
= add_uri_to_consumer(session
,
3015 session
->kernel_session
->consumer
,
3017 LTTNG_DOMAIN_KERNEL
);
3018 if (ret
!= LTTNG_OK
) {
3025 * Make sure to set the session in output mode after we set URI since a
3026 * session can be created without URL (thus flagged in no output mode).
3028 session
->output_traces
= 1;
3030 ksess
->output_traces
= 1;
3034 usess
->output_traces
= 1;
3044 static enum lttng_error_code
3045 set_session_output_from_descriptor(struct ltt_session
*session
,
3046 const struct lttng_session_descriptor
*descriptor
)
3049 enum lttng_error_code ret_code
= LTTNG_OK
;
3050 enum lttng_session_descriptor_type session_type
=
3051 lttng_session_descriptor_get_type(descriptor
);
3052 enum lttng_session_descriptor_output_type output_type
=
3053 lttng_session_descriptor_get_output_type(descriptor
);
3054 struct lttng_uri uris
[2] = {};
3055 size_t uri_count
= 0;
3057 switch (output_type
) {
3058 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3060 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3061 lttng_session_descriptor_get_local_output_uri(descriptor
, &uris
[0]);
3064 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3065 lttng_session_descriptor_get_network_output_uris(descriptor
, &uris
[0], &uris
[1]);
3069 ret_code
= LTTNG_ERR_INVALID
;
3073 switch (session_type
) {
3074 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3076 struct snapshot_output
*new_output
= nullptr;
3078 new_output
= snapshot_output_alloc();
3080 ret_code
= LTTNG_ERR_NOMEM
;
3084 ret
= snapshot_output_init_with_uri(session
,
3085 DEFAULT_SNAPSHOT_MAX_SIZE
,
3091 &session
->snapshot
);
3093 ret_code
= (ret
== -ENOMEM
) ? LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3094 snapshot_output_destroy(new_output
);
3097 snapshot_add_output(&session
->snapshot
, new_output
);
3100 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3101 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3103 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3107 ret_code
= LTTNG_ERR_INVALID
;
3114 static enum lttng_error_code
3115 cmd_create_session_from_descriptor(struct lttng_session_descriptor
*descriptor
,
3116 const lttng_sock_cred
*creds
,
3117 const char *home_path
)
3120 enum lttng_error_code ret_code
;
3121 const char *session_name
;
3122 struct ltt_session
*new_session
= nullptr;
3123 enum lttng_session_descriptor_status descriptor_status
;
3125 session_lock_list();
3127 if (*home_path
!= '/') {
3128 ERR("Home path provided by client is not absolute");
3129 ret_code
= LTTNG_ERR_INVALID
;
3134 descriptor_status
= lttng_session_descriptor_get_session_name(descriptor
, &session_name
);
3135 switch (descriptor_status
) {
3136 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3138 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3139 session_name
= nullptr;
3142 ret_code
= LTTNG_ERR_INVALID
;
3146 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, &new_session
);
3147 if (ret_code
!= LTTNG_OK
) {
3151 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3156 if (ret_code
!= LTTNG_OK
) {
3160 /* Announce the session's destruction to the notification thread when it is destroyed. */
3161 ret
= session_add_destroy_notifier(
3163 [](const struct ltt_session
*session
, void *user_data
__attribute__((unused
))) {
3164 (void) notification_thread_command_remove_session(
3165 the_notification_thread_handle
, session
->id
);
3169 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3171 ret
= LTTNG_ERR_NOMEM
;
3175 if (!session_name
) {
3176 ret
= lttng_session_descriptor_set_session_name(descriptor
, new_session
->name
);
3178 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3183 if (!lttng_session_descriptor_is_output_destination_initialized(descriptor
)) {
3185 * Only include the session's creation time in the output
3186 * destination if the name of the session itself was
3187 * not auto-generated.
3189 ret_code
= lttng_session_descriptor_set_default_output(
3191 session_name
? &new_session
->creation_time
: nullptr,
3193 if (ret_code
!= LTTNG_OK
) {
3197 new_session
->has_user_specified_directory
=
3198 lttng_session_descriptor_has_output_directory(descriptor
);
3201 switch (lttng_session_descriptor_get_type(descriptor
)) {
3202 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3203 new_session
->snapshot_mode
= 1;
3205 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3206 new_session
->live_timer
=
3207 lttng_session_descriptor_live_get_timer_interval(descriptor
);
3213 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3214 if (ret_code
!= LTTNG_OK
) {
3217 new_session
->consumer
->enabled
= true;
3218 ret_code
= LTTNG_OK
;
3220 /* Release reference provided by the session_create function. */
3221 session_put(new_session
);
3222 if (ret_code
!= LTTNG_OK
&& new_session
) {
3223 /* Release the global reference on error. */
3224 session_destroy(new_session
);
3226 session_unlock_list();
3230 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
,
3232 struct lttng_session_descriptor
**return_descriptor
)
3235 size_t payload_size
;
3236 struct lttng_dynamic_buffer payload
;
3237 struct lttng_buffer_view home_dir_view
;
3238 struct lttng_buffer_view session_descriptor_view
;
3239 struct lttng_session_descriptor
*session_descriptor
= nullptr;
3240 enum lttng_error_code ret_code
;
3242 lttng_dynamic_buffer_init(&payload
);
3243 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>= LTTNG_PATH_MAX
) {
3244 ret_code
= LTTNG_ERR_INVALID
;
3247 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3248 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3249 ret_code
= LTTNG_ERR_INVALID
;
3253 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3254 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3255 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3257 ret_code
= LTTNG_ERR_NOMEM
;
3261 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3263 ERR("Reception of session descriptor failed, aborting.");
3264 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3268 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3269 &payload
, 0, cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3270 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3271 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3272 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3273 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3277 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3279 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3280 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3281 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3282 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3283 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3287 ret
= lttng_session_descriptor_create_from_buffer(&session_descriptor_view
,
3288 &session_descriptor
);
3290 ERR("Failed to create session descriptor from payload of \"create session\" command");
3291 ret_code
= LTTNG_ERR_INVALID
;
3296 * Sets the descriptor's auto-generated properties (name, output) if
3299 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3301 home_dir_view
.size
? home_dir_view
.data
:
3303 if (ret_code
!= LTTNG_OK
) {
3307 ret_code
= LTTNG_OK
;
3308 *return_descriptor
= session_descriptor
;
3309 session_descriptor
= nullptr;
3311 lttng_dynamic_buffer_reset(&payload
);
3312 lttng_session_descriptor_destroy(session_descriptor
);
3316 static void cmd_destroy_session_reply(const struct ltt_session
*session
, void *_reply_context
)
3320 const struct cmd_destroy_session_reply_context
*reply_context
=
3321 (cmd_destroy_session_reply_context
*) _reply_context
;
3322 struct lttng_dynamic_buffer payload
;
3323 struct lttcomm_session_destroy_command_header cmd_header
;
3324 struct lttng_trace_archive_location
*location
= nullptr;
3325 struct lttcomm_lttng_msg llm
= {
3326 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION
,
3327 .ret_code
= reply_context
->destruction_status
,
3329 .cmd_header_size
= sizeof(struct lttcomm_session_destroy_command_header
),
3333 size_t payload_size_before_location
;
3335 lttng_dynamic_buffer_init(&payload
);
3337 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3339 ERR("Failed to append session destruction message");
3343 cmd_header
.rotation_state
= (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3344 session
->rotation_state
:
3345 LTTNG_ROTATION_STATE_NO_ROTATION
);
3346 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
, sizeof(cmd_header
));
3348 ERR("Failed to append session destruction command header");
3352 if (!reply_context
->implicit_rotation_on_destroy
) {
3353 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3357 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3358 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3363 location
= session_get_trace_archive_location(session
);
3365 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3370 payload_size_before_location
= payload
.size
;
3371 comm_ret
= lttng_trace_archive_location_serialize(location
, &payload
);
3372 lttng_trace_archive_location_put(location
);
3374 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3378 /* Update the message to indicate the location's length. */
3379 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3380 payload
.size
- payload_size_before_location
;
3382 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
, payload
.data
, payload
.size
);
3383 if (comm_ret
!= (ssize_t
) payload
.size
) {
3384 ERR("Failed to send result of the destruction of session \"%s\" to client",
3388 ret
= close(reply_context
->reply_sock_fd
);
3390 PERROR("Failed to close client socket in deferred session destroy reply");
3392 lttng_dynamic_buffer_reset(&payload
);
3393 free(_reply_context
);
3397 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3399 * Called with session lock held.
3401 int cmd_destroy_session(struct ltt_session
*session
, int *sock_fd
)
3404 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3405 struct cmd_destroy_session_reply_context
*reply_context
= nullptr;
3408 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3409 if (!reply_context
) {
3410 ret
= LTTNG_ERR_NOMEM
;
3414 reply_context
->reply_sock_fd
= *sock_fd
;
3418 LTTNG_ASSERT(session
);
3420 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3421 if (session
->active
) {
3422 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3424 ret
= cmd_stop_trace(session
);
3425 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3426 /* Carry on with the destruction of the session. */
3427 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3429 lttng_strerror(-ret
));
3430 destruction_last_error
= (lttng_error_code
) ret
;
3434 if (session
->rotation_schedule_timer_enabled
) {
3435 if (timer_session_rotation_schedule_timer_stop(session
)) {
3436 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3438 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3442 if (session
->rotate_size
) {
3444 the_rotation_thread_handle
->unsubscribe_session_consumed_size_rotation(
3446 } catch (const std::exception
& e
) {
3447 /* Continue the destruction of the session anyway. */
3448 ERR("Failed to unsubscribe rotation thread notification channel from consumed size condition during session destruction: %s",
3452 session
->rotate_size
= 0;
3455 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3457 * Perform a last rotation on destruction if rotations have
3458 * occurred during the session's lifetime.
3460 ret
= cmd_rotate_session(
3461 session
, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3462 if (ret
!= LTTNG_OK
) {
3463 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3465 lttng_strerror(-ret
));
3466 destruction_last_error
= (lttng_error_code
) -ret
;
3468 if (reply_context
) {
3469 reply_context
->implicit_rotation_on_destroy
= true;
3471 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3473 * The user has not triggered a session rotation. However, to
3474 * ensure all data has been consumed, the session is rotated
3475 * to a 'null' trace chunk before it is destroyed.
3477 * This is a "quiet" rotation meaning that no notification is
3478 * emitted and no renaming of the current trace chunk takes
3481 ret
= cmd_rotate_session(
3482 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3484 * Rotation operations may not be supported by the kernel
3485 * tracer. Hence, do not consider this implicit rotation as
3486 * a session destruction error. The library has already stopped
3487 * the session and waited for pending data; there is nothing
3488 * left to do but complete the destruction of the session.
3490 if (ret
!= LTTNG_OK
&& ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3491 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3493 lttng_strerror(ret
));
3494 destruction_last_error
= (lttng_error_code
) -ret
;
3498 if (session
->shm_path
[0]) {
3500 * When a session is created with an explicit shm_path,
3501 * the consumer daemon will create its shared memory files
3502 * at that location and will *not* unlink them. This is normal
3503 * as the intention of that feature is to make it possible
3504 * to retrieve the content of those files should a crash occur.
3506 * To ensure the content of those files can be used, the
3507 * sessiond daemon will replicate the content of the metadata
3508 * cache in a metadata file.
3510 * On clean-up, it is expected that the consumer daemon will
3511 * unlink the shared memory files and that the session daemon
3512 * will unlink the metadata file. Then, the session's directory
3513 * in the shm path can be removed.
3515 * Unfortunately, a flaw in the design of the sessiond's and
3516 * consumerd's tear down of channels makes it impossible to
3517 * determine when the sessiond _and_ the consumerd have both
3518 * destroyed their representation of a channel. For one, the
3519 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3520 * callbacks in both daemons.
3522 * However, it is also impossible for the sessiond to know when
3523 * the consumer daemon is done destroying its channel(s) since
3524 * it occurs as a reaction to the closing of the channel's file
3525 * descriptor. There is no resulting communication initiated
3526 * from the consumerd to the sessiond to confirm that the
3527 * operation is completed (and was successful).
3529 * Until this is all fixed, the session daemon checks for the
3530 * removal of the session's shm path which makes it possible
3531 * to safely advertise a session as having been destroyed.
3533 * Prior to this fix, it was not possible to reliably save
3534 * a session making use of the --shm-path option, destroy it,
3535 * and load it again. This is because the creation of the
3536 * session would fail upon seeing the session's shm path
3537 * already in existence.
3539 * Note that none of the error paths in the check for the
3540 * directory's existence return an error. This is normal
3541 * as there isn't much that can be done. The session will
3542 * be destroyed properly, except that we can't offer the
3543 * guarantee that the same session can be re-created.
3545 current_completion_handler
= &destroy_completion_handler
.handler
;
3546 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3548 sizeof(destroy_completion_handler
.shm_path
));
3553 * The session is destroyed. However, note that the command context
3554 * still holds a reference to the session, thus delaying its destruction
3555 * _at least_ up to the point when that reference is released.
3557 session_destroy(session
);
3558 if (reply_context
) {
3559 reply_context
->destruction_status
= destruction_last_error
;
3560 ret
= session_add_destroy_notifier(
3561 session
, cmd_destroy_session_reply
, (void *) reply_context
);
3563 ret
= LTTNG_ERR_FATAL
;
3575 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3577 int cmd_register_consumer(struct ltt_session
*session
,
3578 enum lttng_domain_type domain
,
3579 const char *sock_path
,
3580 struct consumer_data
*cdata
)
3583 struct consumer_socket
*socket
= nullptr;
3585 LTTNG_ASSERT(session
);
3586 LTTNG_ASSERT(cdata
);
3587 LTTNG_ASSERT(sock_path
);
3590 case LTTNG_DOMAIN_KERNEL
:
3592 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3594 LTTNG_ASSERT(ksess
);
3596 /* Can't register a consumer if there is already one */
3597 if (ksess
->consumer_fds_sent
!= 0) {
3598 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3602 sock
= lttcomm_connect_unix_sock(sock_path
);
3604 ret
= LTTNG_ERR_CONNECT_FAIL
;
3607 cdata
->cmd_sock
= sock
;
3609 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3610 if (socket
== nullptr) {
3613 PERROR("close register consumer");
3615 cdata
->cmd_sock
= -1;
3616 ret
= LTTNG_ERR_FATAL
;
3620 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3621 if (socket
->lock
== nullptr) {
3622 PERROR("zmalloc pthread mutex");
3623 ret
= LTTNG_ERR_FATAL
;
3627 pthread_mutex_init(socket
->lock
, nullptr);
3628 socket
->registered
= 1;
3630 lttng::urcu::read_lock_guard read_lock
;
3631 consumer_add_socket(socket
, ksess
->consumer
);
3633 pthread_mutex_lock(&cdata
->pid_mutex
);
3635 pthread_mutex_unlock(&cdata
->pid_mutex
);
3640 /* TODO: Userspace tracing */
3641 ret
= LTTNG_ERR_UND
;
3649 consumer_destroy_socket(socket
);
3655 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3657 ssize_t
cmd_list_domains(struct ltt_session
*session
, struct lttng_domain
**domains
)
3662 struct lttng_ht_iter iter
;
3664 if (session
->kernel_session
!= nullptr) {
3665 DBG3("Listing domains found kernel domain");
3669 if (session
->ust_session
!= nullptr) {
3670 DBG3("Listing domains found UST global domain");
3673 lttng::urcu::read_lock_guard read_lock
;
3675 cds_lfht_for_each_entry (
3676 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3677 if (agt
->being_used
) {
3687 *domains
= calloc
<lttng_domain
>(nb_dom
);
3688 if (*domains
== nullptr) {
3689 ret
= LTTNG_ERR_FATAL
;
3693 if (session
->kernel_session
!= nullptr) {
3694 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3696 /* Kernel session buffer type is always GLOBAL */
3697 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3702 if (session
->ust_session
!= nullptr) {
3703 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3704 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3708 lttng::urcu::read_lock_guard read_lock
;
3710 cds_lfht_for_each_entry (
3711 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3712 if (agt
->being_used
) {
3713 (*domains
)[index
].type
= agt
->domain
;
3714 (*domains
)[index
].buf_type
=
3715 session
->ust_session
->buffer_type
;
3725 /* Return negative value to differentiate return code */
3730 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3732 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3733 struct ltt_session
*session
,
3734 struct lttng_payload
*payload
)
3738 struct lttcomm_list_command_header cmd_header
= {};
3739 size_t cmd_header_offset
;
3740 enum lttng_error_code ret_code
;
3745 DBG("Listing channels for session %s", session
->name
);
3747 cmd_header_offset
= payload
->buffer
.size
;
3749 /* Reserve space for command reply header. */
3750 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3751 cmd_header_offset
+ sizeof(cmd_header
));
3753 ret_code
= LTTNG_ERR_NOMEM
;
3758 case LTTNG_DOMAIN_KERNEL
:
3760 /* Kernel channels */
3761 struct ltt_kernel_channel
*kchan
;
3762 if (session
->kernel_session
!= nullptr) {
3763 cds_list_for_each_entry (
3764 kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
3765 uint64_t discarded_events
, lost_packets
;
3766 struct lttng_channel_extended
*extended
;
3768 extended
= (struct lttng_channel_extended
*)
3769 kchan
->channel
->attr
.extended
.ptr
;
3771 ret
= get_kernel_runtime_stats(
3772 session
, kchan
, &discarded_events
, &lost_packets
);
3774 ret_code
= LTTNG_ERR_UNK
;
3779 * Update the discarded_events and lost_packets
3780 * count for the channel
3782 extended
->discarded_events
= discarded_events
;
3783 extended
->lost_packets
= lost_packets
;
3785 ret
= lttng_channel_serialize(kchan
->channel
, &payload
->buffer
);
3787 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3788 kchan
->channel
->name
);
3789 ret_code
= LTTNG_ERR_UNK
;
3798 case LTTNG_DOMAIN_UST
:
3800 struct lttng_ht_iter iter
;
3801 struct ltt_ust_channel
*uchan
;
3804 lttng::urcu::read_lock_guard read_lock
;
3806 cds_lfht_for_each_entry (session
->ust_session
->domain_global
.channels
->ht
,
3810 uint64_t discarded_events
= 0, lost_packets
= 0;
3811 struct lttng_channel
*channel
= nullptr;
3812 struct lttng_channel_extended
*extended
;
3814 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3816 ret_code
= LTTNG_ERR_NOMEM
;
3820 extended
= (struct lttng_channel_extended
*)
3821 channel
->attr
.extended
.ptr
;
3823 ret
= get_ust_runtime_stats(
3824 session
, uchan
, &discarded_events
, &lost_packets
);
3826 lttng_channel_destroy(channel
);
3827 ret_code
= LTTNG_ERR_UNK
;
3831 extended
->discarded_events
= discarded_events
;
3832 extended
->lost_packets
= lost_packets
;
3834 ret
= lttng_channel_serialize(channel
, &payload
->buffer
);
3836 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3838 lttng_channel_destroy(channel
);
3839 ret_code
= LTTNG_ERR_UNK
;
3843 lttng_channel_destroy(channel
);
3854 if (i
> UINT32_MAX
) {
3855 ERR("Channel count would overflow the channel listing command's reply");
3856 ret_code
= LTTNG_ERR_OVERFLOW
;
3860 /* Update command reply header. */
3861 cmd_header
.count
= (uint32_t) i
;
3862 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
, sizeof(cmd_header
));
3863 ret_code
= LTTNG_OK
;
3870 * Command LTTNG_LIST_EVENTS processed by the client thread.
3872 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3873 struct ltt_session
*session
,
3875 struct lttng_payload
*reply_payload
)
3877 int buffer_resize_ret
;
3878 enum lttng_error_code ret_code
= LTTNG_OK
;
3879 struct lttcomm_list_command_header reply_command_header
= {};
3880 size_t reply_command_header_offset
;
3881 unsigned int nb_events
= 0;
3883 assert(reply_payload
);
3885 /* Reserve space for command reply header. */
3886 reply_command_header_offset
= reply_payload
->buffer
.size
;
3887 buffer_resize_ret
= lttng_dynamic_buffer_set_size(
3888 &reply_payload
->buffer
,
3889 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
3890 if (buffer_resize_ret
) {
3891 ret_code
= LTTNG_ERR_NOMEM
;
3896 case LTTNG_DOMAIN_KERNEL
:
3897 if (session
->kernel_session
!= nullptr) {
3898 ret_code
= list_lttng_kernel_events(
3899 channel_name
, session
->kernel_session
, reply_payload
, &nb_events
);
3903 case LTTNG_DOMAIN_UST
:
3905 if (session
->ust_session
!= nullptr) {
3907 list_lttng_ust_global_events(channel_name
,
3908 &session
->ust_session
->domain_global
,
3915 case LTTNG_DOMAIN_LOG4J
:
3916 case LTTNG_DOMAIN_JUL
:
3917 case LTTNG_DOMAIN_PYTHON
:
3918 if (session
->ust_session
) {
3919 struct lttng_ht_iter iter
;
3922 lttng::urcu::read_lock_guard read_lock
;
3924 cds_lfht_for_each_entry (
3925 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3926 if (agt
->domain
== domain
) {
3927 ret_code
= list_lttng_agent_events(
3928 agt
, reply_payload
, &nb_events
);
3935 ret_code
= LTTNG_ERR_UND
;
3939 if (nb_events
> UINT32_MAX
) {
3940 ret_code
= LTTNG_ERR_OVERFLOW
;
3944 /* Update command reply header. */
3945 reply_command_header
.count
= (uint32_t) nb_events
;
3946 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
3947 &reply_command_header
,
3948 sizeof(reply_command_header
));
3955 * Using the session list, filled a lttng_session array to send back to the
3956 * client for session listing.
3958 * The session list lock MUST be acquired before calling this function. Use
3959 * session_lock_list() and session_unlock_list().
3961 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3962 size_t session_count
,
3968 struct ltt_session
*session
;
3969 struct ltt_session_list
*list
= session_get_list();
3970 struct lttng_session_extended
*extended
= (typeof(extended
)) (&sessions
[session_count
]);
3972 DBG("Getting all available session for UID %d GID %d", uid
, gid
);
3974 * Iterate over session list and append data after the control struct in
3977 cds_list_for_each_entry (session
, &list
->head
, list
) {
3978 if (!session_get(session
)) {
3982 * Only list the sessions the user can control.
3984 if (!session_access_ok(session
, uid
) || session
->destroyed
) {
3985 session_put(session
);
3989 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3990 struct ltt_ust_session
*usess
= session
->ust_session
;
3992 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3993 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3994 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3995 ret
= build_network_session_path(
3996 sessions
[i
].path
, sizeof(sessions
[i
].path
), session
);
3998 ret
= snprintf(sessions
[i
].path
,
3999 sizeof(sessions
[i
].path
),
4001 session
->consumer
->dst
.session_root_path
);
4004 PERROR("snprintf session path");
4005 session_put(session
);
4009 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
4010 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
4011 sessions
[i
].enabled
= session
->active
;
4012 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
4013 sessions
[i
].live_timer_interval
= session
->live_timer
;
4014 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
4015 extended
[i
].creation_time
.is_set
= 1;
4017 session_put(session
);
4022 * Command LTTCOMM_SESSIOND_COMMAND_KERNEL_TRACER_STATUS
4024 enum lttng_error_code
cmd_kernel_tracer_status(enum lttng_kernel_tracer_status
*status
)
4026 if (status
== nullptr) {
4027 return LTTNG_ERR_INVALID
;
4030 *status
= get_kernel_tracer_status();
4035 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4036 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4038 int cmd_data_pending(struct ltt_session
*session
)
4041 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4042 struct ltt_ust_session
*usess
= session
->ust_session
;
4044 LTTNG_ASSERT(session
);
4046 DBG("Data pending for session %s", session
->name
);
4048 /* Session MUST be stopped to ask for data availability. */
4049 if (session
->active
) {
4050 ret
= LTTNG_ERR_SESSION_STARTED
;
4054 * If stopped, just make sure we've started before else the above call
4055 * will always send that there is data pending.
4057 * The consumer assumes that when the data pending command is received,
4058 * the trace has been started before or else no output data is written
4059 * by the streams which is a condition for data pending. So, this is
4060 * *VERY* important that we don't ask the consumer before a start
4063 if (!session
->has_been_started
) {
4069 /* A rotation is still pending, we have to wait. */
4070 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4071 DBG("Rotate still pending for session %s", session
->name
);
4076 if (ksess
&& ksess
->consumer
) {
4077 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4079 /* Data is still being extracted for the kernel. */
4084 if (usess
&& usess
->consumer
) {
4085 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4087 /* Data is still being extracted for the kernel. */
4092 /* Data is ready to be read by a viewer */
4100 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4102 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4104 int cmd_snapshot_add_output(struct ltt_session
*session
,
4105 const struct lttng_snapshot_output
*output
,
4109 struct snapshot_output
*new_output
;
4111 LTTNG_ASSERT(session
);
4112 LTTNG_ASSERT(output
);
4114 DBG("Cmd snapshot add output for session %s", session
->name
);
4117 * Can't create an output if the session is not set in no-output mode.
4119 if (session
->output_traces
) {
4120 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4124 if (session
->has_non_mmap_channel
) {
4125 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4129 /* Only one output is allowed until we have the "tee" feature. */
4130 if (session
->snapshot
.nb_output
== 1) {
4131 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4135 new_output
= snapshot_output_alloc();
4137 ret
= LTTNG_ERR_NOMEM
;
4141 ret
= snapshot_output_init(session
,
4148 &session
->snapshot
);
4150 if (ret
== -ENOMEM
) {
4151 ret
= LTTNG_ERR_NOMEM
;
4153 ret
= LTTNG_ERR_INVALID
;
4158 snapshot_add_output(&session
->snapshot
, new_output
);
4160 *id
= new_output
->id
;
4166 snapshot_output_destroy(new_output
);
4172 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4174 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4176 int cmd_snapshot_del_output(struct ltt_session
*session
, const struct lttng_snapshot_output
*output
)
4179 struct snapshot_output
*sout
= nullptr;
4181 LTTNG_ASSERT(session
);
4182 LTTNG_ASSERT(output
);
4184 lttng::urcu::read_lock_guard read_lock
;
4187 * Permission denied to create an output if the session is not
4188 * set in no output mode.
4190 if (session
->output_traces
) {
4191 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4196 DBG("Cmd snapshot del output id %" PRIu32
" for session %s",
4199 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4200 } else if (*output
->name
!= '\0') {
4201 DBG("Cmd snapshot del output name %s for session %s", output
->name
, session
->name
);
4202 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4205 ret
= LTTNG_ERR_INVALID
;
4209 snapshot_delete_output(&session
->snapshot
, sout
);
4210 snapshot_output_destroy(sout
);
4218 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4220 * If no output is available, outputs is untouched and 0 is returned.
4222 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4224 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4225 struct lttng_snapshot_output
**outputs
)
4228 struct lttng_snapshot_output
*list
= nullptr;
4229 struct lttng_ht_iter iter
;
4230 struct snapshot_output
*output
;
4232 LTTNG_ASSERT(session
);
4233 LTTNG_ASSERT(outputs
);
4235 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4238 * Permission denied to create an output if the session is not
4239 * set in no output mode.
4241 if (session
->output_traces
) {
4242 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4246 if (session
->snapshot
.nb_output
== 0) {
4251 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4253 ret
= -LTTNG_ERR_NOMEM
;
4257 /* Copy list from session to the new list object. */
4259 lttng::urcu::read_lock_guard read_lock
;
4261 cds_lfht_for_each_entry (
4262 session
->snapshot
.output_ht
->ht
, &iter
.iter
, output
, node
.node
) {
4263 LTTNG_ASSERT(output
->consumer
);
4264 list
[idx
].id
= output
->id
;
4265 list
[idx
].max_size
= output
->max_size
;
4266 if (lttng_strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
))) {
4267 ret
= -LTTNG_ERR_INVALID
;
4271 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4272 if (lttng_strncpy(list
[idx
].ctrl_url
,
4273 output
->consumer
->dst
.session_root_path
,
4274 sizeof(list
[idx
].ctrl_url
))) {
4275 ret
= -LTTNG_ERR_INVALID
;
4280 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4282 sizeof(list
[idx
].ctrl_url
));
4284 ret
= -LTTNG_ERR_NOMEM
;
4289 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4291 sizeof(list
[idx
].data_url
));
4293 ret
= -LTTNG_ERR_NOMEM
;
4304 ret
= session
->snapshot
.nb_output
;
4312 * Check if we can regenerate the metadata for this session.
4313 * Only kernel, UST per-uid and non-live sessions are supported.
4315 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4317 static int check_regenerate_metadata_support(struct ltt_session
*session
)
4321 LTTNG_ASSERT(session
);
4323 if (session
->live_timer
!= 0) {
4324 ret
= LTTNG_ERR_LIVE_SESSION
;
4327 if (!session
->active
) {
4328 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4331 if (session
->ust_session
) {
4332 switch (session
->ust_session
->buffer_type
) {
4333 case LTTNG_BUFFER_PER_UID
:
4335 case LTTNG_BUFFER_PER_PID
:
4336 ret
= LTTNG_ERR_PER_PID_SESSION
;
4340 ret
= LTTNG_ERR_UNK
;
4344 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4345 session
->consumer
->relay_minor_version
< 8) {
4346 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4356 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4358 * Ask the consumer to truncate the existing metadata file(s) and
4359 * then regenerate the metadata. Live and per-pid sessions are not
4360 * supported and return an error.
4362 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4364 int cmd_regenerate_metadata(struct ltt_session
*session
)
4368 LTTNG_ASSERT(session
);
4370 ret
= check_regenerate_metadata_support(session
);
4375 if (session
->kernel_session
) {
4376 ret
= kernctl_session_regenerate_metadata(session
->kernel_session
->fd
);
4378 ERR("Failed to regenerate the kernel metadata");
4383 if (session
->ust_session
) {
4384 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4386 ERR("Failed to regenerate the UST metadata");
4390 DBG("Cmd metadata regenerate for session %s", session
->name
);
4398 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4400 * Ask the tracer to regenerate a new statedump.
4402 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4404 int cmd_regenerate_statedump(struct ltt_session
*session
)
4408 LTTNG_ASSERT(session
);
4410 if (!session
->active
) {
4411 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4415 if (session
->kernel_session
) {
4416 ret
= kernctl_session_regenerate_statedump(session
->kernel_session
->fd
);
4418 * Currently, the statedump in kernel can only fail if out
4422 if (ret
== -ENOMEM
) {
4423 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4425 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4427 ERR("Failed to regenerate the kernel statedump");
4432 if (session
->ust_session
) {
4433 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4435 * Currently, the statedump in UST always returns 0.
4438 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4439 ERR("Failed to regenerate the UST statedump");
4443 DBG("Cmd regenerate statedump for session %s", session
->name
);
4450 static enum lttng_error_code
4451 synchronize_tracer_notifier_register(struct notification_thread_handle
*notification_thread
,
4452 struct lttng_trigger
*trigger
,
4453 const struct lttng_credentials
*cmd_creds
)
4455 enum lttng_error_code ret_code
;
4456 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4457 const char *trigger_name
;
4458 uid_t trigger_owner
;
4459 enum lttng_trigger_status trigger_status
;
4460 const enum lttng_domain_type trigger_domain
=
4461 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4463 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4464 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4466 LTTNG_ASSERT(condition
);
4467 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4468 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4470 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4471 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4473 session_lock_list();
4474 switch (trigger_domain
) {
4475 case LTTNG_DOMAIN_KERNEL
:
4477 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4478 if (ret_code
!= LTTNG_OK
) {
4479 enum lttng_error_code notif_thread_unregister_ret
;
4481 notif_thread_unregister_ret
=
4482 notification_thread_command_unregister_trigger(notification_thread
,
4485 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4486 /* Return the original error code. */
4487 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4489 (int) trigger_owner
,
4493 goto end_unlock_session_list
;
4497 case LTTNG_DOMAIN_UST
:
4498 ust_app_global_update_all_event_notifier_rules();
4500 case LTTNG_DOMAIN_JUL
:
4501 case LTTNG_DOMAIN_LOG4J
:
4502 case LTTNG_DOMAIN_PYTHON
:
4504 /* Agent domains. */
4505 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4508 agt
= agent_create(trigger_domain
);
4510 ret_code
= LTTNG_ERR_NOMEM
;
4511 goto end_unlock_session_list
;
4514 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4517 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4518 if (ret_code
!= LTTNG_OK
) {
4519 goto end_unlock_session_list
;
4524 case LTTNG_DOMAIN_NONE
:
4529 ret_code
= LTTNG_OK
;
4530 end_unlock_session_list
:
4531 session_unlock_list();
4535 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4536 struct lttng_trigger
*trigger
,
4537 bool is_trigger_anonymous
,
4538 struct notification_thread_handle
*notification_thread
,
4539 struct lttng_trigger
**return_trigger
)
4541 enum lttng_error_code ret_code
;
4542 const char *trigger_name
;
4543 uid_t trigger_owner
;
4544 enum lttng_trigger_status trigger_status
;
4546 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4547 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4549 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4550 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4552 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4554 (int) trigger_owner
,
4555 (int) lttng_credentials_get_uid(cmd_creds
));
4558 * Validate the trigger credentials against the command credentials.
4559 * Only the root user can register a trigger with non-matching
4562 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4563 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4564 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4566 (int) trigger_owner
,
4567 (int) lttng_credentials_get_uid(cmd_creds
));
4568 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4574 * The bytecode generation also serves as a validation step for the
4575 * bytecode expressions.
4577 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4578 if (ret_code
!= LTTNG_OK
) {
4579 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4581 (int) trigger_owner
,
4587 * A reference to the trigger is acquired by the notification thread.
4588 * It is safe to return the same trigger to the caller since it the
4589 * other user holds a reference.
4591 * The trigger is modified during the execution of the
4592 * "register trigger" command. However, by the time the command returns,
4593 * it is safe to use without any locking as its properties are
4596 ret_code
= notification_thread_command_register_trigger(
4597 notification_thread
, trigger
, is_trigger_anonymous
);
4598 if (ret_code
!= LTTNG_OK
) {
4599 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4601 (int) trigger_owner
,
4606 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4607 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4610 * Synchronize tracers if the trigger adds an event notifier.
4612 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4613 ret_code
= synchronize_tracer_notifier_register(
4614 notification_thread
, trigger
, cmd_creds
);
4615 if (ret_code
!= LTTNG_OK
) {
4616 ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code
));
4622 * Return an updated trigger to the client.
4624 * Since a modified version of the same trigger is returned, acquire a
4625 * reference to the trigger so the caller doesn't have to care if those
4626 * are distinct instances or not.
4628 if (ret_code
== LTTNG_OK
) {
4629 lttng_trigger_get(trigger
);
4630 *return_trigger
= trigger
;
4631 /* Ownership of trigger was transferred to caller. */
4638 static enum lttng_error_code
4639 synchronize_tracer_notifier_unregister(const struct lttng_trigger
*trigger
)
4641 enum lttng_error_code ret_code
;
4642 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4643 const enum lttng_domain_type trigger_domain
=
4644 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4646 LTTNG_ASSERT(condition
);
4647 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4648 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4650 session_lock_list();
4651 switch (trigger_domain
) {
4652 case LTTNG_DOMAIN_KERNEL
:
4653 ret_code
= kernel_unregister_event_notifier(trigger
);
4654 if (ret_code
!= LTTNG_OK
) {
4655 goto end_unlock_session_list
;
4659 case LTTNG_DOMAIN_UST
:
4660 ust_app_global_update_all_event_notifier_rules();
4662 case LTTNG_DOMAIN_JUL
:
4663 case LTTNG_DOMAIN_LOG4J
:
4664 case LTTNG_DOMAIN_PYTHON
:
4666 /* Agent domains. */
4667 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4670 * This trigger was never registered in the first place. Calling
4671 * this function under those circumstances is an internal error.
4674 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4675 if (ret_code
!= LTTNG_OK
) {
4676 goto end_unlock_session_list
;
4681 case LTTNG_DOMAIN_NONE
:
4686 ret_code
= LTTNG_OK
;
4688 end_unlock_session_list
:
4689 session_unlock_list();
4693 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4694 const struct lttng_trigger
*trigger
,
4695 struct notification_thread_handle
*notification_thread
)
4697 enum lttng_error_code ret_code
;
4698 const char *trigger_name
;
4699 uid_t trigger_owner
;
4700 enum lttng_trigger_status trigger_status
;
4701 struct lttng_trigger
*sessiond_trigger
= nullptr;
4703 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4704 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4705 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4706 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4708 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4710 (int) trigger_owner
,
4711 (int) lttng_credentials_get_uid(cmd_creds
));
4714 * Validate the trigger credentials against the command credentials.
4715 * Only the root user can unregister a trigger with non-matching
4718 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4719 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4720 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4722 (int) trigger_owner
,
4723 (int) lttng_credentials_get_uid(cmd_creds
));
4724 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4729 /* Fetch the sessiond side trigger object. */
4730 ret_code
= notification_thread_command_get_trigger(
4731 notification_thread
, trigger
, &sessiond_trigger
);
4732 if (ret_code
!= LTTNG_OK
) {
4733 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4735 (int) trigger_owner
,
4740 LTTNG_ASSERT(sessiond_trigger
);
4743 * From this point on, no matter what, consider the trigger
4746 * We set the unregistered state of the sessiond side trigger object in
4747 * the client thread since we want to minimize the possibility of the
4748 * notification thread being stalled due to a long execution of an
4749 * action that required the trigger lock.
4751 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4753 ret_code
= notification_thread_command_unregister_trigger(notification_thread
, trigger
);
4754 if (ret_code
!= LTTNG_OK
) {
4755 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4757 (int) trigger_owner
,
4763 * Synchronize tracers if the trigger removes an event notifier.
4764 * Do this even if the trigger unregistration failed to at least stop
4765 * the tracers from producing notifications associated with this
4768 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4769 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4770 if (ret_code
!= LTTNG_OK
) {
4771 ERR("Error unregistering trigger to tracer.");
4777 lttng_trigger_put(sessiond_trigger
);
4781 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4782 struct notification_thread_handle
*notification_thread
,
4783 struct lttng_triggers
**return_triggers
)
4786 enum lttng_error_code ret_code
;
4787 struct lttng_triggers
*triggers
= nullptr;
4789 /* Get the set of triggers from the notification thread. */
4790 ret_code
= notification_thread_command_list_triggers(
4791 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4792 if (ret_code
!= LTTNG_OK
) {
4796 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4798 ret_code
= LTTNG_ERR_UNK
;
4802 *return_triggers
= triggers
;
4804 ret_code
= LTTNG_OK
;
4806 lttng_triggers_destroy(triggers
);
4810 enum lttng_error_code
4811 cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4812 const struct lttng_error_query
*query
,
4813 struct lttng_error_query_results
**_results
,
4814 struct notification_thread_handle
*notification_thread
)
4816 enum lttng_error_code ret_code
;
4817 const struct lttng_trigger
*query_target_trigger
;
4818 const struct lttng_action
*query_target_action
= nullptr;
4819 struct lttng_trigger
*matching_trigger
= nullptr;
4820 const char *trigger_name
;
4821 uid_t trigger_owner
;
4822 enum lttng_trigger_status trigger_status
;
4823 struct lttng_error_query_results
*results
= nullptr;
4825 switch (lttng_error_query_get_target_type(query
)) {
4826 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4827 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4829 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4830 query_target_trigger
= lttng_error_query_condition_borrow_target(query
);
4832 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4833 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(query
);
4839 LTTNG_ASSERT(query_target_trigger
);
4841 ret_code
= notification_thread_command_get_trigger(
4842 notification_thread
, query_target_trigger
, &matching_trigger
);
4843 if (ret_code
!= LTTNG_OK
) {
4847 /* No longer needed. */
4848 query_target_trigger
= nullptr;
4850 if (lttng_error_query_get_target_type(query
) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4851 /* Get the sessiond-side version of the target action. */
4852 query_target_action
=
4853 lttng_error_query_action_borrow_action_target(query
, matching_trigger
);
4856 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4857 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4858 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
, &trigger_owner
);
4859 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4861 results
= lttng_error_query_results_create();
4863 ret_code
= LTTNG_ERR_NOMEM
;
4867 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4869 (int) trigger_owner
,
4870 (int) lttng_credentials_get_uid(cmd_creds
));
4873 * Validate the trigger credentials against the command credentials.
4874 * Only the root user can target a trigger with non-matching
4877 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger
),
4879 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4880 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4882 (int) trigger_owner
,
4883 (int) lttng_credentials_get_uid(cmd_creds
));
4884 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4889 switch (lttng_error_query_get_target_type(query
)) {
4890 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4891 trigger_status
= lttng_trigger_add_error_results(matching_trigger
, results
);
4893 switch (trigger_status
) {
4894 case LTTNG_TRIGGER_STATUS_OK
:
4897 ret_code
= LTTNG_ERR_UNK
;
4902 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4905 lttng_trigger_condition_add_error_results(matching_trigger
, results
);
4907 switch (trigger_status
) {
4908 case LTTNG_TRIGGER_STATUS_OK
:
4911 ret_code
= LTTNG_ERR_UNK
;
4917 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4919 const enum lttng_action_status action_status
=
4920 lttng_action_add_error_query_results(query_target_action
, results
);
4922 switch (action_status
) {
4923 case LTTNG_ACTION_STATUS_OK
:
4926 ret_code
= LTTNG_ERR_UNK
;
4937 *_results
= results
;
4939 ret_code
= LTTNG_OK
;
4941 lttng_trigger_put(matching_trigger
);
4942 lttng_error_query_results_destroy(results
);
4947 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4948 * snapshot output is *not* set with a remote destination.
4950 * Return LTTNG_OK on success or a LTTNG_ERR code.
4952 static enum lttng_error_code
set_relayd_for_snapshot(struct consumer_output
*output
,
4953 const struct ltt_session
*session
)
4955 enum lttng_error_code status
= LTTNG_OK
;
4956 struct lttng_ht_iter iter
;
4957 struct consumer_socket
*socket
;
4958 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4959 const char *base_path
;
4961 LTTNG_ASSERT(output
);
4962 LTTNG_ASSERT(session
);
4964 DBG2("Set relayd object from snapshot output");
4966 if (session
->current_trace_chunk
) {
4967 enum lttng_trace_chunk_status chunk_status
= lttng_trace_chunk_get_id(
4968 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
4970 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4971 current_chunk_id
.is_set
= true;
4973 ERR("Failed to get current trace chunk id");
4974 status
= LTTNG_ERR_UNK
;
4979 /* Ignore if snapshot consumer output is not network. */
4980 if (output
->type
!= CONSUMER_DST_NET
) {
4985 * The snapshot record URI base path overrides the session
4988 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4989 base_path
= output
->dst
.net
.control
.subdir
;
4991 base_path
= session
->base_path
;
4995 * For each consumer socket, create and send the relayd object of the
4999 lttng::urcu::read_lock_guard read_lock
;
5001 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
5002 pthread_mutex_lock(socket
->lock
);
5003 status
= send_consumer_relayd_sockets(
5010 session
->live_timer
,
5011 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
5012 session
->creation_time
,
5013 session
->name_contains_creation_time
);
5014 pthread_mutex_unlock(socket
->lock
);
5015 if (status
!= LTTNG_OK
) {
5026 * Record a kernel snapshot.
5028 * Return LTTNG_OK on success or a LTTNG_ERR code.
5030 static enum lttng_error_code
record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
5031 const struct consumer_output
*output
,
5032 const struct ltt_session
*session
,
5033 uint64_t nb_packets_per_stream
)
5035 enum lttng_error_code status
;
5037 LTTNG_ASSERT(ksess
);
5038 LTTNG_ASSERT(output
);
5039 LTTNG_ASSERT(session
);
5041 status
= kernel_snapshot_record(ksess
, output
, nb_packets_per_stream
);
5046 * Record a UST snapshot.
5048 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5050 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5051 const struct consumer_output
*output
,
5052 const struct ltt_session
*session
,
5053 uint64_t nb_packets_per_stream
)
5055 enum lttng_error_code status
;
5057 LTTNG_ASSERT(usess
);
5058 LTTNG_ASSERT(output
);
5059 LTTNG_ASSERT(session
);
5061 status
= ust_app_snapshot_record(usess
, output
, nb_packets_per_stream
);
5065 static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session
*session
,
5066 uint64_t cur_nr_packets
)
5068 uint64_t tot_size
= 0;
5070 if (session
->kernel_session
) {
5071 struct ltt_kernel_channel
*chan
;
5072 const struct ltt_kernel_session
*ksess
= session
->kernel_session
;
5074 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
5075 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5077 * Don't take channel into account if we
5078 * already grab all its packets.
5082 tot_size
+= chan
->channel
->attr
.subbuf_size
* chan
->stream_count
;
5086 if (session
->ust_session
) {
5087 const struct ltt_ust_session
*usess
= session
->ust_session
;
5089 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
, cur_nr_packets
);
5096 * Calculate the number of packets we can grab from each stream that
5097 * fits within the overall snapshot max size.
5099 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5100 * the number of packets per stream.
5102 * TODO: this approach is not perfect: we consider the worse case
5103 * (packet filling the sub-buffers) as an upper bound, but we could do
5104 * better if we do this calculation while we actually grab the packet
5105 * content: we would know how much padding we don't actually store into
5108 * This algorithm is currently bounded by the number of packets per
5111 * Since we call this algorithm before actually grabbing the data, it's
5112 * an approximation: for instance, applications could appear/disappear
5113 * in between this call and actually grabbing data.
5115 static int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5119 uint64_t cur_nb_packets
= 0;
5122 return 0; /* Infinite */
5125 size_left
= max_size
;
5127 uint64_t one_more_packet_tot_size
;
5129 one_more_packet_tot_size
=
5130 get_session_size_one_more_packet_per_stream(session
, cur_nb_packets
);
5131 if (!one_more_packet_tot_size
) {
5132 /* We are already grabbing all packets. */
5135 size_left
-= one_more_packet_tot_size
;
5136 if (size_left
< 0) {
5141 if (!cur_nb_packets
&& size_left
!= max_size
) {
5142 /* Not enough room to grab one packet of each stream, error. */
5145 return cur_nb_packets
;
5148 static enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5149 const struct snapshot_output
*snapshot_output
)
5151 int64_t nb_packets_per_stream
;
5152 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5154 enum lttng_error_code ret_code
= LTTNG_OK
;
5155 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5156 struct consumer_output
*original_ust_consumer_output
= nullptr;
5157 struct consumer_output
*original_kernel_consumer_output
= nullptr;
5158 struct consumer_output
*snapshot_ust_consumer_output
= nullptr;
5159 struct consumer_output
*snapshot_kernel_consumer_output
= nullptr;
5161 ret
= snprintf(snapshot_chunk_name
,
5162 sizeof(snapshot_chunk_name
),
5164 snapshot_output
->name
,
5165 snapshot_output
->datetime
,
5166 snapshot_output
->nb_snapshot
);
5167 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5168 ERR("Failed to format snapshot name");
5169 ret_code
= LTTNG_ERR_INVALID
;
5172 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5173 snapshot_output
->name
,
5175 snapshot_chunk_name
);
5176 if (!session
->kernel_session
&& !session
->ust_session
) {
5177 ERR("Failed to record snapshot as no channels exist");
5178 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5182 if (session
->kernel_session
) {
5183 original_kernel_consumer_output
= session
->kernel_session
->consumer
;
5184 snapshot_kernel_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5185 strcpy(snapshot_kernel_consumer_output
->chunk_path
, snapshot_chunk_name
);
5187 /* Copy the original domain subdir. */
5188 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5189 original_kernel_consumer_output
->domain_subdir
);
5191 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5192 original_kernel_consumer_output
);
5194 ERR("Failed to copy consumer sockets from snapshot output configuration");
5195 ret_code
= LTTNG_ERR_NOMEM
;
5198 ret_code
= set_relayd_for_snapshot(snapshot_kernel_consumer_output
, session
);
5199 if (ret_code
!= LTTNG_OK
) {
5200 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5203 session
->kernel_session
->consumer
= snapshot_kernel_consumer_output
;
5205 if (session
->ust_session
) {
5206 original_ust_consumer_output
= session
->ust_session
->consumer
;
5207 snapshot_ust_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5208 strcpy(snapshot_ust_consumer_output
->chunk_path
, snapshot_chunk_name
);
5210 /* Copy the original domain subdir. */
5211 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5212 original_ust_consumer_output
->domain_subdir
);
5214 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5215 original_ust_consumer_output
);
5217 ERR("Failed to copy consumer sockets from snapshot output configuration");
5218 ret_code
= LTTNG_ERR_NOMEM
;
5221 ret_code
= set_relayd_for_snapshot(snapshot_ust_consumer_output
, session
);
5222 if (ret_code
!= LTTNG_OK
) {
5223 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5226 session
->ust_session
->consumer
= snapshot_ust_consumer_output
;
5229 snapshot_trace_chunk
= session_create_new_trace_chunk(
5231 snapshot_kernel_consumer_output
?: snapshot_ust_consumer_output
,
5232 consumer_output_get_base_path(snapshot_output
->consumer
),
5233 snapshot_chunk_name
);
5234 if (!snapshot_trace_chunk
) {
5235 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5237 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5240 LTTNG_ASSERT(!session
->current_trace_chunk
);
5241 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, nullptr);
5242 lttng_trace_chunk_put(snapshot_trace_chunk
);
5243 snapshot_trace_chunk
= nullptr;
5245 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5247 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5251 nb_packets_per_stream
=
5252 get_session_nb_packets_per_stream(session
, snapshot_output
->max_size
);
5253 if (nb_packets_per_stream
< 0) {
5254 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5255 goto error_close_trace_chunk
;
5258 if (session
->kernel_session
) {
5259 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5260 snapshot_kernel_consumer_output
,
5262 nb_packets_per_stream
);
5263 if (ret_code
!= LTTNG_OK
) {
5264 goto error_close_trace_chunk
;
5268 if (session
->ust_session
) {
5269 ret_code
= record_ust_snapshot(session
->ust_session
,
5270 snapshot_ust_consumer_output
,
5272 nb_packets_per_stream
);
5273 if (ret_code
!= LTTNG_OK
) {
5274 goto error_close_trace_chunk
;
5278 error_close_trace_chunk
:
5279 if (session_set_trace_chunk(session
, nullptr, &snapshot_trace_chunk
)) {
5280 ERR("Failed to release the current trace chunk of session \"%s\"", session
->name
);
5281 ret_code
= LTTNG_ERR_UNK
;
5284 if (session_close_trace_chunk(session
,
5285 snapshot_trace_chunk
,
5286 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
,
5289 * Don't goto end; make sure the chunk is closed for the session
5290 * to allow future snapshots.
5292 ERR("Failed to close snapshot trace chunk of session \"%s\"", session
->name
);
5293 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5296 lttng_trace_chunk_put(snapshot_trace_chunk
);
5297 snapshot_trace_chunk
= nullptr;
5299 if (original_ust_consumer_output
) {
5300 session
->ust_session
->consumer
= original_ust_consumer_output
;
5302 if (original_kernel_consumer_output
) {
5303 session
->kernel_session
->consumer
= original_kernel_consumer_output
;
5305 consumer_output_put(snapshot_ust_consumer_output
);
5306 consumer_output_put(snapshot_kernel_consumer_output
);
5311 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5313 * The wait parameter is ignored so this call always wait for the snapshot to
5314 * complete before returning.
5316 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5318 int cmd_snapshot_record(struct ltt_session
*session
,
5319 const struct lttng_snapshot_output
*output
,
5320 int wait
__attribute__((unused
)))
5322 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5324 unsigned int snapshot_success
= 0;
5326 struct snapshot_output
*tmp_output
= nullptr;
5328 LTTNG_ASSERT(session
);
5329 LTTNG_ASSERT(output
);
5331 DBG("Cmd snapshot record for session %s", session
->name
);
5333 /* Get the datetime for the snapshot output directory. */
5334 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
, sizeof(datetime
));
5336 cmd_ret
= LTTNG_ERR_INVALID
;
5341 * Permission denied to create an output if the session is not
5342 * set in no output mode.
5344 if (session
->output_traces
) {
5345 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5349 /* The session needs to be started at least once. */
5350 if (!session
->has_been_started
) {
5351 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5355 /* Use temporary output for the session. */
5356 if (*output
->ctrl_url
!= '\0') {
5357 tmp_output
= snapshot_output_alloc();
5359 cmd_ret
= LTTNG_ERR_NOMEM
;
5363 ret
= snapshot_output_init(session
,
5372 if (ret
== -ENOMEM
) {
5373 cmd_ret
= LTTNG_ERR_NOMEM
;
5375 cmd_ret
= LTTNG_ERR_INVALID
;
5379 /* Use the global session count for the temporary snapshot. */
5380 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5382 /* Use the global datetime */
5383 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5384 cmd_ret
= snapshot_record(session
, tmp_output
);
5385 if (cmd_ret
!= LTTNG_OK
) {
5388 snapshot_success
= 1;
5390 struct snapshot_output
*sout
;
5391 struct lttng_ht_iter iter
;
5393 lttng::urcu::read_lock_guard read_lock
;
5395 cds_lfht_for_each_entry (
5396 session
->snapshot
.output_ht
->ht
, &iter
.iter
, sout
, node
.node
) {
5397 struct snapshot_output output_copy
;
5400 * Make a local copy of the output and override output
5401 * parameters with those provided as part of the
5404 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5406 if (output
->max_size
!= (uint64_t) -1ULL) {
5407 output_copy
.max_size
= output
->max_size
;
5410 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5411 memcpy(output_copy
.datetime
, datetime
, sizeof(datetime
));
5413 /* Use temporary name. */
5414 if (*output
->name
!= '\0') {
5415 if (lttng_strncpy(output_copy
.name
,
5417 sizeof(output_copy
.name
))) {
5418 cmd_ret
= LTTNG_ERR_INVALID
;
5423 cmd_ret
= snapshot_record(session
, &output_copy
);
5424 if (cmd_ret
!= LTTNG_OK
) {
5428 snapshot_success
= 1;
5432 if (snapshot_success
) {
5433 session
->snapshot
.nb_snapshot
++;
5435 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5440 snapshot_output_destroy(tmp_output
);
5447 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5449 int cmd_set_session_shm_path(struct ltt_session
*session
, const char *shm_path
)
5452 LTTNG_ASSERT(session
);
5455 * Can only set shm path before session is started.
5457 if (session
->has_been_started
) {
5458 return LTTNG_ERR_SESSION_STARTED
;
5461 strncpy(session
->shm_path
, shm_path
, sizeof(session
->shm_path
));
5462 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5468 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5470 * Ask the consumer to rotate the session output directory.
5471 * The session lock must be held.
5473 * Returns LTTNG_OK on success or else a negative LTTng error code.
5475 int cmd_rotate_session(struct ltt_session
*session
,
5476 struct lttng_rotate_session_return
*rotate_return
,
5477 bool quiet_rotation
,
5478 enum lttng_trace_chunk_command_type command
)
5481 uint64_t ongoing_rotation_chunk_id
;
5482 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5483 struct lttng_trace_chunk
*chunk_being_archived
= nullptr;
5484 struct lttng_trace_chunk
*new_trace_chunk
= nullptr;
5485 enum lttng_trace_chunk_status chunk_status
;
5486 bool failed_to_rotate
= false;
5487 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5489 LTTNG_ASSERT(session
);
5491 if (!session
->has_been_started
) {
5492 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5497 * Explicit rotation is not supported for live sessions.
5498 * However, live sessions can perform a quiet rotation on
5500 * Rotation is not supported for snapshot traces (no output).
5502 if ((!quiet_rotation
&& session
->live_timer
) || !session
->output_traces
) {
5503 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5507 /* Unsupported feature in lttng-relayd before 2.11. */
5508 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5509 (session
->consumer
->relay_major_version
== 2 &&
5510 session
->consumer
->relay_minor_version
< 11)) {
5511 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5515 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5516 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5517 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5521 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5522 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5524 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5529 * After a stop, we only allow one rotation to occur, the other ones are
5530 * useless until a new start.
5532 if (session
->rotated_after_last_stop
) {
5533 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5535 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5540 * After a stop followed by a clear, disallow following rotations a they would
5541 * generate empty chunks.
5543 if (session
->cleared_after_last_stop
) {
5544 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5546 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5550 if (session
->active
) {
5552 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
5553 if (!new_trace_chunk
) {
5554 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5560 * The current trace chunk becomes the chunk being archived.
5562 * After this point, "chunk_being_archived" must absolutely
5563 * be closed on the consumer(s), otherwise it will never be
5564 * cleaned-up, which will result in a leak.
5566 ret
= session_set_trace_chunk(session
, new_trace_chunk
, &chunk_being_archived
);
5568 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5572 if (session
->kernel_session
) {
5573 cmd_ret
= kernel_rotate_session(session
);
5574 if (cmd_ret
!= LTTNG_OK
) {
5575 failed_to_rotate
= true;
5576 rotation_fail_code
= cmd_ret
;
5579 if (session
->ust_session
) {
5580 cmd_ret
= ust_app_rotate_session(session
);
5581 if (cmd_ret
!= LTTNG_OK
) {
5582 failed_to_rotate
= true;
5583 rotation_fail_code
= cmd_ret
;
5587 if (!session
->active
) {
5588 session
->rotated_after_last_stop
= true;
5591 if (!chunk_being_archived
) {
5592 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5594 if (failed_to_rotate
) {
5595 cmd_ret
= rotation_fail_code
;
5602 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5603 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
, &ongoing_rotation_chunk_id
);
5604 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5606 ret
= session_close_trace_chunk(
5607 session
, chunk_being_archived
, command
, session
->last_chunk_path
);
5609 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5613 if (failed_to_rotate
) {
5614 cmd_ret
= rotation_fail_code
;
5618 session
->quiet_rotation
= quiet_rotation
;
5619 ret
= timer_session_rotation_pending_check_start(session
, DEFAULT_ROTATE_PENDING_TIMER
);
5621 cmd_ret
= LTTNG_ERR_UNK
;
5625 if (rotate_return
) {
5626 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5629 session
->chunk_being_archived
= chunk_being_archived
;
5630 chunk_being_archived
= nullptr;
5631 if (!quiet_rotation
) {
5632 ret
= notification_thread_command_session_rotation_ongoing(
5633 the_notification_thread_handle
, session
->id
, ongoing_rotation_chunk_id
);
5634 if (ret
!= LTTNG_OK
) {
5635 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5637 cmd_ret
= (lttng_error_code
) ret
;
5641 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5643 ongoing_rotation_chunk_id
);
5645 lttng_trace_chunk_put(new_trace_chunk
);
5646 lttng_trace_chunk_put(chunk_being_archived
);
5647 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5650 if (session_reset_rotation_state(*session
, LTTNG_ROTATION_STATE_ERROR
)) {
5651 ERR("Failed to reset rotation state of session \"%s\"", session
->name
);
5657 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5659 * Check if the session has finished its rotation.
5661 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5663 int cmd_rotate_get_info(struct ltt_session
*session
,
5664 struct lttng_rotation_get_info_return
*info_return
,
5665 uint64_t rotation_id
)
5667 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5668 enum lttng_rotation_state rotation_state
;
5670 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
,
5672 session
->most_recent_chunk_id
.value
);
5674 if (session
->chunk_being_archived
) {
5675 enum lttng_trace_chunk_status chunk_status
;
5678 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
, &chunk_id
);
5679 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5681 rotation_state
= rotation_id
== chunk_id
? LTTNG_ROTATION_STATE_ONGOING
:
5682 LTTNG_ROTATION_STATE_EXPIRED
;
5684 if (session
->last_archived_chunk_id
.is_set
&&
5685 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5686 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5688 rotation_state
= session
->rotation_state
;
5692 switch (rotation_state
) {
5693 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5694 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5697 case LTTNG_ROTATION_STATE_EXPIRED
:
5698 DBG("Reporting that the rotation state of rotation id %" PRIu64
5699 " of session \"%s\" has expired",
5703 case LTTNG_ROTATION_STATE_ONGOING
:
5704 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5708 case LTTNG_ROTATION_STATE_COMPLETED
:
5712 char *current_tracing_path_reply
;
5713 size_t current_tracing_path_reply_len
;
5715 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5719 switch (session_get_consumer_destination_type(session
)) {
5720 case CONSUMER_DST_LOCAL
:
5721 current_tracing_path_reply
= info_return
->location
.local
.absolute_path
;
5722 current_tracing_path_reply_len
=
5723 sizeof(info_return
->location
.local
.absolute_path
);
5724 info_return
->location_type
=
5725 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5726 fmt_ret
= asprintf(&chunk_path
,
5727 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5728 session_get_base_path(session
),
5729 session
->last_archived_chunk_name
);
5730 if (fmt_ret
== -1) {
5731 PERROR("Failed to format the path of the last archived trace chunk");
5732 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5733 cmd_ret
= LTTNG_ERR_UNK
;
5737 case CONSUMER_DST_NET
:
5739 uint16_t ctrl_port
, data_port
;
5741 current_tracing_path_reply
= info_return
->location
.relay
.relative_path
;
5742 current_tracing_path_reply_len
=
5743 sizeof(info_return
->location
.relay
.relative_path
);
5744 /* Currently the only supported relay protocol. */
5745 info_return
->location
.relay
.protocol
=
5746 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5748 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5749 session_get_net_consumer_hostname(session
),
5750 sizeof(info_return
->location
.relay
.host
));
5752 ERR("Failed to copy host name to rotate_get_info reply");
5753 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5754 cmd_ret
= LTTNG_ERR_SET_URL
;
5758 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5759 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5760 info_return
->location
.relay
.ports
.data
= data_port
;
5761 info_return
->location_type
=
5762 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5763 chunk_path
= strdup(session
->last_chunk_path
);
5765 ERR("Failed to allocate the path of the last archived trace chunk");
5766 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5767 cmd_ret
= LTTNG_ERR_UNK
;
5776 fmt_ret
= lttng_strncpy(
5777 current_tracing_path_reply
, chunk_path
, current_tracing_path_reply_len
);
5780 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5781 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5782 cmd_ret
= LTTNG_ERR_UNK
;
5788 case LTTNG_ROTATION_STATE_ERROR
:
5789 DBG("Reporting that an error occurred during rotation %" PRIu64
5790 " of session \"%s\"",
5800 info_return
->status
= (int32_t) rotation_state
;
5805 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5807 * Configure the automatic rotation parameters.
5808 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5809 * 'activate' to false means deactivate the rotation schedule and validate that
5810 * 'new_value' has the same value as the currently active value.
5812 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5814 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5816 enum lttng_rotation_schedule_type schedule_type
,
5820 uint64_t *parameter_value
;
5822 LTTNG_ASSERT(session
);
5824 DBG("Cmd rotate set schedule session %s", session
->name
);
5826 if (session
->live_timer
|| !session
->output_traces
) {
5827 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5828 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5832 switch (schedule_type
) {
5833 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5834 parameter_value
= &session
->rotate_size
;
5836 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5837 parameter_value
= &session
->rotate_timer_period
;
5838 if (new_value
>= UINT_MAX
) {
5839 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
5843 ret
= LTTNG_ERR_INVALID
;
5848 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5849 ret
= LTTNG_ERR_INVALID
;
5853 /* Improper use of the API. */
5854 if (new_value
== -1ULL) {
5855 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5856 ret
= LTTNG_ERR_INVALID
;
5861 * As indicated in struct ltt_session's comments, a value of == 0 means
5862 * this schedule rotation type is not in use.
5864 * Reject the command if we were asked to activate a schedule that was
5867 if (activate
&& *parameter_value
!= 0) {
5868 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5869 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5874 * Reject the command if we were asked to deactivate a schedule that was
5877 if (!activate
&& *parameter_value
== 0) {
5878 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5879 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5884 * Reject the command if we were asked to deactivate a schedule that
5887 if (!activate
&& *parameter_value
!= new_value
) {
5888 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5889 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5893 *parameter_value
= activate
? new_value
: 0;
5895 switch (schedule_type
) {
5896 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5897 if (activate
&& session
->active
) {
5899 * Only start the timer if the session is active,
5900 * otherwise it will be started when the session starts.
5902 ret
= timer_session_rotation_schedule_timer_start(session
, new_value
);
5904 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5905 ret
= LTTNG_ERR_UNK
;
5909 ret
= timer_session_rotation_schedule_timer_stop(session
);
5911 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5912 ret
= LTTNG_ERR_UNK
;
5917 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5920 the_rotation_thread_handle
->subscribe_session_consumed_size_rotation(
5921 *session
, new_value
);
5922 } catch (const std::exception
& e
) {
5923 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5925 ret
= LTTNG_ERR_UNK
;
5930 the_rotation_thread_handle
5931 ->unsubscribe_session_consumed_size_rotation(*session
);
5932 } catch (const std::exception
& e
) {
5933 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5935 ret
= LTTNG_ERR_UNK
;
5941 /* Would have been caught before. */
5953 /* Wait for a given path to be removed before continuing. */
5954 static enum lttng_error_code
wait_on_path(void *path_data
)
5956 const char *shm_path
= (const char *) path_data
;
5958 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5964 ret
= stat(shm_path
, &st
);
5966 if (errno
!= ENOENT
) {
5967 PERROR("stat() returned an error while checking for the existence of the shm path");
5969 DBG("shm path no longer exists, completing the destruction of session");
5973 if (!S_ISDIR(st
.st_mode
)) {
5974 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5979 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5985 * Returns a pointer to a handler to run on completion of a command.
5986 * Returns NULL if no handler has to be run for the last command executed.
5988 const struct cmd_completion_handler
*cmd_pop_completion_handler()
5990 struct cmd_completion_handler
*handler
= current_completion_handler
;
5992 current_completion_handler
= nullptr;
5997 * Init command subsystem.
6002 * Set network sequence index to 1 for streams to match a relayd
6003 * socket on the consumer side.
6005 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6006 relayd_net_seq_idx
= 1;
6007 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6009 DBG("Command subsystem initialized");