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"
26 #include "rotation-thread.hpp"
27 #include "session.hpp"
29 #include "tracker.hpp"
32 #include <common/buffer-view.hpp>
33 #include <common/common.hpp>
34 #include <common/compat/string.hpp>
35 #include <common/defaults.hpp>
36 #include <common/dynamic-buffer.hpp>
37 #include <common/kernel-ctl/kernel-ctl.hpp>
38 #include <common/payload-view.hpp>
39 #include <common/payload.hpp>
40 #include <common/relayd/relayd.hpp>
41 #include <common/sessiond-comm/sessiond-comm.hpp>
42 #include <common/string-utils/string-utils.hpp>
43 #include <common/trace-chunk.hpp>
44 #include <common/urcu.hpp>
45 #include <common/utils.hpp>
47 #include <lttng/action/action-internal.hpp>
48 #include <lttng/action/action.h>
49 #include <lttng/channel-internal.hpp>
50 #include <lttng/channel.h>
51 #include <lttng/condition/condition-internal.hpp>
52 #include <lttng/condition/condition.h>
53 #include <lttng/condition/event-rule-matches-internal.hpp>
54 #include <lttng/condition/event-rule-matches.h>
55 #include <lttng/error-query-internal.hpp>
56 #include <lttng/event-internal.hpp>
57 #include <lttng/event-rule/event-rule-internal.hpp>
58 #include <lttng/event-rule/event-rule.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;
489 * We do not care about the filter bytecode and the fd from the
490 * userspace_probe_location.
492 ret
= lttng_event_serialize(tmp_event
,
493 uevent
->exclusion
? uevent
->exclusion
->count
: 0,
494 uevent
->exclusion
? (char **) uevent
->exclusion
->names
:
496 uevent
->filter_expression
,
500 lttng_event_destroy(tmp_event
);
502 ret_code
= LTTNG_ERR_FATAL
;
508 /* nb_events is already set at this point. */
510 *nb_events
= local_nb_events
;
516 * Fill lttng_event array of all kernel events in the channel.
518 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
519 struct ltt_kernel_session
*kernel_session
,
520 struct lttng_payload
*reply_payload
,
521 unsigned int *nb_events
)
523 enum lttng_error_code ret_code
;
525 struct ltt_kernel_event
*event
;
526 struct ltt_kernel_channel
*kchan
;
528 assert(reply_payload
);
530 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
531 if (kchan
== nullptr) {
532 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
536 *nb_events
= kchan
->event_count
;
538 DBG("Listing events for channel %s", kchan
->channel
->name
);
540 if (*nb_events
== 0) {
545 /* Kernel channels */
546 cds_list_for_each_entry (event
, &kchan
->events_list
.head
, list
) {
547 struct lttng_event
*tmp_event
= lttng_event_create();
550 ret_code
= LTTNG_ERR_NOMEM
;
554 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
555 lttng_event_destroy(tmp_event
);
556 ret_code
= LTTNG_ERR_FATAL
;
560 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
561 tmp_event
->enabled
= event
->enabled
;
562 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
564 switch (event
->event
->instrumentation
) {
565 case LTTNG_KERNEL_ABI_TRACEPOINT
:
566 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
568 case LTTNG_KERNEL_ABI_KRETPROBE
:
569 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
570 memcpy(&tmp_event
->attr
.probe
,
571 &event
->event
->u
.kprobe
,
572 sizeof(struct lttng_kernel_abi_kprobe
));
574 case LTTNG_KERNEL_ABI_KPROBE
:
575 tmp_event
->type
= LTTNG_EVENT_PROBE
;
576 memcpy(&tmp_event
->attr
.probe
,
577 &event
->event
->u
.kprobe
,
578 sizeof(struct lttng_kernel_abi_kprobe
));
580 case LTTNG_KERNEL_ABI_UPROBE
:
581 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
583 case LTTNG_KERNEL_ABI_FUNCTION
:
584 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
585 memcpy(&(tmp_event
->attr
.ftrace
),
586 &event
->event
->u
.ftrace
,
587 sizeof(struct lttng_kernel_abi_function
));
589 case LTTNG_KERNEL_ABI_NOOP
:
590 tmp_event
->type
= LTTNG_EVENT_NOOP
;
592 case LTTNG_KERNEL_ABI_SYSCALL
:
593 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
595 case LTTNG_KERNEL_ABI_ALL
:
602 if (event
->userspace_probe_location
) {
603 struct lttng_userspace_probe_location
*location_copy
=
604 lttng_userspace_probe_location_copy(
605 event
->userspace_probe_location
);
607 if (!location_copy
) {
608 lttng_event_destroy(tmp_event
);
609 ret_code
= LTTNG_ERR_NOMEM
;
613 ret
= lttng_event_set_userspace_probe_location(tmp_event
, location_copy
);
615 lttng_event_destroy(tmp_event
);
616 lttng_userspace_probe_location_destroy(location_copy
);
617 ret_code
= LTTNG_ERR_INVALID
;
622 ret
= lttng_event_serialize(
623 tmp_event
, 0, nullptr, event
->filter_expression
, 0, nullptr, reply_payload
);
624 lttng_event_destroy(tmp_event
);
626 ret_code
= LTTNG_ERR_FATAL
;
637 * Add URI so the consumer output object. Set the correct path depending on the
638 * domain adding the default trace directory.
640 static enum lttng_error_code
add_uri_to_consumer(const struct ltt_session
*session
,
641 struct consumer_output
*consumer
,
642 struct lttng_uri
*uri
,
643 enum lttng_domain_type domain
)
646 enum lttng_error_code ret_code
= LTTNG_OK
;
650 if (consumer
== nullptr) {
651 DBG("No consumer detected. Don't add URI. Stopping.");
652 ret_code
= LTTNG_ERR_NO_CONSUMER
;
657 case LTTNG_DOMAIN_KERNEL
:
658 ret
= lttng_strncpy(consumer
->domain_subdir
,
659 DEFAULT_KERNEL_TRACE_DIR
,
660 sizeof(consumer
->domain_subdir
));
662 case LTTNG_DOMAIN_UST
:
663 ret
= lttng_strncpy(consumer
->domain_subdir
,
664 DEFAULT_UST_TRACE_DIR
,
665 sizeof(consumer
->domain_subdir
));
669 * This case is possible is we try to add the URI to the global
670 * tracing session consumer object which in this case there is
673 memset(consumer
->domain_subdir
, 0, sizeof(consumer
->domain_subdir
));
677 ERR("Failed to initialize consumer output domain subdirectory");
678 ret_code
= LTTNG_ERR_FATAL
;
682 switch (uri
->dtype
) {
685 DBG2("Setting network URI to consumer");
687 if (consumer
->type
== CONSUMER_DST_NET
) {
688 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
689 consumer
->dst
.net
.control_isset
) ||
690 (uri
->stype
== LTTNG_STREAM_DATA
&& consumer
->dst
.net
.data_isset
)) {
691 ret_code
= LTTNG_ERR_URL_EXIST
;
695 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
698 /* Set URI into consumer output object */
699 ret
= consumer_set_network_uri(session
, consumer
, uri
);
701 ret_code
= (lttng_error_code
) -ret
;
703 } else if (ret
== 1) {
705 * URI was the same in the consumer so we do not append the subdir
706 * again so to not duplicate output dir.
713 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
714 ret_code
= LTTNG_ERR_INVALID
;
717 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
718 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
720 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
722 sizeof(consumer
->dst
.session_root_path
));
724 ret_code
= LTTNG_ERR_FATAL
;
727 consumer
->type
= CONSUMER_DST_LOCAL
;
737 * Init tracing by creating trace directory and sending fds kernel consumer.
739 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
742 struct lttng_ht_iter iter
;
743 struct consumer_socket
*socket
;
745 LTTNG_ASSERT(session
);
747 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= nullptr) {
748 lttng::urcu::read_lock_guard read_lock
;
750 cds_lfht_for_each_entry (
751 session
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
752 pthread_mutex_lock(socket
->lock
);
753 ret
= kernel_consumer_send_session(socket
, session
);
754 pthread_mutex_unlock(socket
->lock
);
756 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
767 * Create a socket to the relayd using the URI.
769 * On success, the relayd_sock pointer is set to the created socket.
770 * Else, it remains untouched and an LTTng error code is returned.
772 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
773 struct lttcomm_relayd_sock
**relayd_sock
,
774 struct consumer_output
*consumer
)
777 enum lttng_error_code status
= LTTNG_OK
;
778 struct lttcomm_relayd_sock
*rsock
;
780 rsock
= lttcomm_alloc_relayd_sock(
781 uri
, RELAYD_VERSION_COMM_MAJOR
, RELAYD_VERSION_COMM_MINOR
);
783 status
= LTTNG_ERR_FATAL
;
788 * Connect to relayd so we can proceed with a session creation. This call
789 * can possibly block for an arbitrary amount of time to set the health
790 * state to be in poll execution.
793 ret
= relayd_connect(rsock
);
796 ERR("Unable to reach lttng-relayd");
797 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
801 /* Create socket for control stream. */
802 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
803 uint64_t result_flags
;
805 DBG3("Creating relayd stream socket from URI");
807 /* Check relayd version */
808 ret
= relayd_version_check(rsock
);
809 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
810 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
812 } else if (ret
< 0) {
813 ERR("Unable to reach lttng-relayd");
814 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
817 consumer
->relay_major_version
= rsock
->major
;
818 consumer
->relay_minor_version
= rsock
->minor
;
819 ret
= relayd_get_configuration(rsock
, 0, &result_flags
);
821 ERR("Unable to get relayd configuration");
822 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
825 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
826 consumer
->relay_allows_clear
= true;
828 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
829 DBG3("Creating relayd data socket from URI");
831 /* Command is not valid */
832 ERR("Relayd invalid stream type: %d", uri
->stype
);
833 status
= LTTNG_ERR_INVALID
;
837 *relayd_sock
= rsock
;
842 /* The returned value is not useful since we are on an error path. */
843 (void) relayd_close(rsock
);
851 * Connect to the relayd using URI and send the socket to the right consumer.
853 * The consumer socket lock must be held by the caller.
855 * Returns LTTNG_OK on success or an LTTng error code on failure.
857 static enum lttng_error_code
send_consumer_relayd_socket(unsigned int session_id
,
858 struct lttng_uri
*relayd_uri
,
859 struct consumer_output
*consumer
,
860 struct consumer_socket
*consumer_sock
,
861 const char *session_name
,
862 const char *hostname
,
863 const char *base_path
,
864 int session_live_timer
,
865 const uint64_t *current_chunk_id
,
866 time_t session_creation_time
,
867 bool session_name_contains_creation_time
)
870 struct lttcomm_relayd_sock
*rsock
= nullptr;
871 enum lttng_error_code status
;
873 /* Connect to relayd and make version check if uri is the control. */
874 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
875 if (status
!= LTTNG_OK
) {
876 goto relayd_comm_error
;
880 /* Set the network sequence index if not set. */
881 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
882 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
884 * Increment net_seq_idx because we are about to transfer the
885 * new relayd socket to the consumer.
886 * Assign unique key so the consumer can match streams.
888 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
889 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
892 /* Send relayd socket to consumer. */
893 ret
= consumer_send_relayd_socket(consumer_sock
,
903 session_creation_time
,
904 session_name_contains_creation_time
);
906 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
910 /* Flag that the corresponding socket was sent. */
911 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
912 consumer_sock
->control_sock_sent
= 1;
913 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
914 consumer_sock
->data_sock_sent
= 1;
918 * Close socket which was dup on the consumer side. The session daemon does
919 * NOT keep track of the relayd socket(s) once transfer to the consumer.
923 if (status
!= LTTNG_OK
) {
925 * The consumer output for this session should not be used anymore
926 * since the relayd connection failed thus making any tracing or/and
927 * streaming not usable.
929 consumer
->enabled
= false;
931 (void) relayd_close(rsock
);
939 * Send both relayd sockets to a specific consumer and domain. This is a
940 * helper function to facilitate sending the information to the consumer for a
943 * The consumer socket lock must be held by the caller.
945 * Returns LTTNG_OK, or an LTTng error code on failure.
947 static enum lttng_error_code
send_consumer_relayd_sockets(unsigned int session_id
,
948 struct consumer_output
*consumer
,
949 struct consumer_socket
*sock
,
950 const char *session_name
,
951 const char *hostname
,
952 const char *base_path
,
953 int session_live_timer
,
954 const uint64_t *current_chunk_id
,
955 time_t session_creation_time
,
956 bool session_name_contains_creation_time
)
958 enum lttng_error_code status
= LTTNG_OK
;
960 LTTNG_ASSERT(consumer
);
963 /* Sending control relayd socket. */
964 if (!sock
->control_sock_sent
) {
965 status
= send_consumer_relayd_socket(session_id
,
966 &consumer
->dst
.net
.control
,
974 session_creation_time
,
975 session_name_contains_creation_time
);
976 if (status
!= LTTNG_OK
) {
981 /* Sending data relayd socket. */
982 if (!sock
->data_sock_sent
) {
983 status
= send_consumer_relayd_socket(session_id
,
984 &consumer
->dst
.net
.data
,
992 session_creation_time
,
993 session_name_contains_creation_time
);
994 if (status
!= LTTNG_OK
) {
1004 * Setup relayd connections for a tracing session. First creates the socket to
1005 * the relayd and send them to the right domain consumer. Consumer type MUST be
1008 int cmd_setup_relayd(struct ltt_session
*session
)
1011 struct ltt_ust_session
*usess
;
1012 struct ltt_kernel_session
*ksess
;
1013 struct consumer_socket
*socket
;
1014 struct lttng_ht_iter iter
;
1015 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1017 LTTNG_ASSERT(session
);
1019 usess
= session
->ust_session
;
1020 ksess
= session
->kernel_session
;
1022 DBG("Setting relayd for session %s", session
->name
);
1024 if (session
->current_trace_chunk
) {
1025 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1026 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1028 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1029 current_chunk_id
.is_set
= true;
1031 ERR("Failed to get current trace chunk id");
1032 ret
= LTTNG_ERR_UNK
;
1037 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
&&
1038 usess
->consumer
->enabled
) {
1039 /* For each consumer socket, send relayd sockets */
1040 lttng::urcu::read_lock_guard read_lock
;
1042 cds_lfht_for_each_entry (
1043 usess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1044 pthread_mutex_lock(socket
->lock
);
1045 ret
= send_consumer_relayd_sockets(
1052 session
->live_timer
,
1053 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
1054 session
->creation_time
,
1055 session
->name_contains_creation_time
);
1056 pthread_mutex_unlock(socket
->lock
);
1057 if (ret
!= LTTNG_OK
) {
1060 /* Session is now ready for network streaming. */
1061 session
->net_handle
= 1;
1064 session
->consumer
->relay_major_version
= usess
->consumer
->relay_major_version
;
1065 session
->consumer
->relay_minor_version
= usess
->consumer
->relay_minor_version
;
1066 session
->consumer
->relay_allows_clear
= usess
->consumer
->relay_allows_clear
;
1069 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
&&
1070 ksess
->consumer
->enabled
) {
1071 lttng::urcu::read_lock_guard read_lock
;
1073 cds_lfht_for_each_entry (
1074 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1075 pthread_mutex_lock(socket
->lock
);
1076 ret
= send_consumer_relayd_sockets(
1083 session
->live_timer
,
1084 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
1085 session
->creation_time
,
1086 session
->name_contains_creation_time
);
1087 pthread_mutex_unlock(socket
->lock
);
1088 if (ret
!= LTTNG_OK
) {
1091 /* Session is now ready for network streaming. */
1092 session
->net_handle
= 1;
1095 session
->consumer
->relay_major_version
= ksess
->consumer
->relay_major_version
;
1096 session
->consumer
->relay_minor_version
= ksess
->consumer
->relay_minor_version
;
1097 session
->consumer
->relay_allows_clear
= ksess
->consumer
->relay_allows_clear
;
1105 * Start a kernel session by opening all necessary streams.
1107 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1110 struct ltt_kernel_channel
*kchan
;
1112 /* Open kernel metadata */
1113 if (ksess
->metadata
== nullptr && ksess
->output_traces
) {
1114 ret
= kernel_open_metadata(ksess
);
1116 ret
= LTTNG_ERR_KERN_META_FAIL
;
1121 /* Open kernel metadata stream */
1122 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1123 ret
= kernel_open_metadata_stream(ksess
);
1125 ERR("Kernel create metadata stream failed");
1126 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1131 /* For each channel */
1132 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1133 if (kchan
->stream_count
== 0) {
1134 ret
= kernel_open_channel_stream(kchan
);
1136 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1139 /* Update the stream global counter */
1140 ksess
->stream_count_global
+= ret
;
1144 /* Setup kernel consumer socket and send fds to it */
1145 ret
= init_kernel_tracing(ksess
);
1147 ret
= LTTNG_ERR_KERN_START_FAIL
;
1151 /* This start the kernel tracing */
1152 ret
= kernel_start_session(ksess
);
1154 ret
= LTTNG_ERR_KERN_START_FAIL
;
1158 /* Quiescent wait after starting trace */
1159 kernel_wait_quiescent();
1161 ksess
->active
= true;
1169 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1171 struct ltt_kernel_channel
*kchan
;
1172 bool error_occurred
= false;
1175 if (!ksess
|| !ksess
->active
) {
1178 DBG("Stopping kernel tracing");
1180 ret
= kernel_stop_session(ksess
);
1182 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1186 kernel_wait_quiescent();
1188 /* Flush metadata after stopping (if exists) */
1189 if (ksess
->metadata_stream_fd
>= 0) {
1190 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1192 ERR("Kernel metadata flush failed");
1193 error_occurred
= true;
1197 /* Flush all buffers after stopping */
1198 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1199 ret
= kernel_flush_buffer(kchan
);
1201 ERR("Kernel flush buffer error");
1202 error_occurred
= true;
1206 ksess
->active
= false;
1207 if (error_occurred
) {
1208 ret
= LTTNG_ERR_UNK
;
1217 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1219 int cmd_disable_channel(struct ltt_session
*session
,
1220 enum lttng_domain_type domain
,
1224 struct ltt_ust_session
*usess
;
1226 usess
= session
->ust_session
;
1228 lttng::urcu::read_lock_guard read_lock
;
1231 case LTTNG_DOMAIN_KERNEL
:
1233 ret
= channel_kernel_disable(session
->kernel_session
, channel_name
);
1234 if (ret
!= LTTNG_OK
) {
1238 kernel_wait_quiescent();
1241 case LTTNG_DOMAIN_UST
:
1243 struct ltt_ust_channel
*uchan
;
1244 struct lttng_ht
*chan_ht
;
1246 chan_ht
= usess
->domain_global
.channels
;
1248 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1249 if (uchan
== nullptr) {
1250 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1254 ret
= channel_ust_disable(usess
, uchan
);
1255 if (ret
!= LTTNG_OK
) {
1261 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1272 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1274 * The wpipe arguments is used as a notifier for the kernel thread.
1276 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1280 ssize_t sock_recv_len
;
1281 struct lttng_channel
*channel
= nullptr;
1282 struct lttng_buffer_view view
;
1283 struct lttng_dynamic_buffer channel_buffer
;
1284 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1286 lttng_dynamic_buffer_init(&channel_buffer
);
1287 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1288 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1290 ret
= LTTNG_ERR_NOMEM
;
1294 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
, channel_len
);
1295 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1296 ERR("Failed to receive \"enable channel\" command payload");
1297 ret
= LTTNG_ERR_INVALID
;
1301 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1302 if (!lttng_buffer_view_is_valid(&view
)) {
1303 ret
= LTTNG_ERR_INVALID
;
1307 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1308 ERR("Invalid channel payload received in \"enable channel\" command");
1309 ret
= LTTNG_ERR_INVALID
;
1313 ret
= cmd_enable_channel_internal(cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1316 lttng_dynamic_buffer_reset(&channel_buffer
);
1317 lttng_channel_destroy(channel
);
1321 static enum lttng_error_code
cmd_enable_channel_internal(struct ltt_session
*session
,
1322 const struct lttng_domain
*domain
,
1323 const struct lttng_channel
*_attr
,
1326 enum lttng_error_code ret_code
;
1327 struct ltt_ust_session
*usess
= session
->ust_session
;
1328 struct lttng_ht
*chan_ht
;
1330 struct lttng_channel
*attr
= nullptr;
1332 LTTNG_ASSERT(session
);
1333 LTTNG_ASSERT(_attr
);
1334 LTTNG_ASSERT(domain
);
1336 lttng::urcu::read_lock_guard read_lock
;
1338 attr
= lttng_channel_copy(_attr
);
1340 ret_code
= LTTNG_ERR_NOMEM
;
1344 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1346 /* Validate channel name */
1347 if (attr
->name
[0] == '.' || memchr(attr
->name
, '/', len
) != nullptr) {
1348 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1352 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1355 * If the session is a live session, remove the switch timer, the
1356 * live timer does the same thing but sends also synchronisation
1357 * beacons for inactive streams.
1359 if (session
->live_timer
> 0) {
1360 attr
->attr
.live_timer_interval
= session
->live_timer
;
1361 attr
->attr
.switch_timer_interval
= 0;
1364 /* Check for feature support */
1365 switch (domain
->type
) {
1366 case LTTNG_DOMAIN_KERNEL
:
1368 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1369 /* Sampling position of buffer is not supported */
1370 WARN("Kernel tracer does not support buffer monitoring. "
1371 "Setting the monitor interval timer to 0 "
1372 "(disabled) for channel '%s' of session '%s'",
1375 lttng_channel_set_monitor_timer_interval(attr
, 0);
1379 case LTTNG_DOMAIN_UST
:
1381 case LTTNG_DOMAIN_JUL
:
1382 case LTTNG_DOMAIN_LOG4J
:
1383 case LTTNG_DOMAIN_PYTHON
:
1384 if (!agent_tracing_is_enabled()) {
1385 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1386 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1391 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1395 switch (domain
->type
) {
1396 case LTTNG_DOMAIN_KERNEL
:
1398 struct ltt_kernel_channel
*kchan
;
1400 kchan
= trace_kernel_get_channel_by_name(attr
->name
, session
->kernel_session
);
1401 if (kchan
== nullptr) {
1403 * Don't try to create a channel if the session has been started at
1404 * some point in time before. The tracer does not allow it.
1406 if (session
->has_been_started
) {
1407 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1411 if (session
->snapshot
.nb_output
> 0 || session
->snapshot_mode
) {
1412 /* Enforce mmap output for snapshot sessions. */
1413 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1415 ret_code
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1416 if (attr
->name
[0] != '\0') {
1417 session
->kernel_session
->has_non_default_channel
= 1;
1420 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1423 if (ret_code
!= LTTNG_OK
) {
1427 kernel_wait_quiescent();
1430 case LTTNG_DOMAIN_UST
:
1431 case LTTNG_DOMAIN_JUL
:
1432 case LTTNG_DOMAIN_LOG4J
:
1433 case LTTNG_DOMAIN_PYTHON
:
1435 struct ltt_ust_channel
*uchan
;
1440 * Current agent implementation limitations force us to allow
1441 * only one channel at once in "agent" subdomains. Each
1442 * subdomain has a default channel name which must be strictly
1445 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1446 if (strncmp(attr
->name
,
1447 DEFAULT_JUL_CHANNEL_NAME
,
1448 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1449 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1452 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1453 if (strncmp(attr
->name
,
1454 DEFAULT_LOG4J_CHANNEL_NAME
,
1455 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1456 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1459 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1460 if (strncmp(attr
->name
,
1461 DEFAULT_PYTHON_CHANNEL_NAME
,
1462 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1463 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1468 chan_ht
= usess
->domain_global
.channels
;
1470 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1471 if (uchan
== nullptr) {
1473 * Don't try to create a channel if the session has been started at
1474 * some point in time before. The tracer does not allow it.
1476 if (session
->has_been_started
) {
1477 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1481 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1482 if (attr
->name
[0] != '\0') {
1483 usess
->has_non_default_channel
= 1;
1486 ret_code
= channel_ust_enable(usess
, uchan
);
1491 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1495 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1496 session
->has_non_mmap_channel
= true;
1500 lttng_channel_destroy(attr
);
1504 enum lttng_error_code
1505 cmd_process_attr_tracker_get_tracking_policy(struct ltt_session
*session
,
1506 enum lttng_domain_type domain
,
1507 enum lttng_process_attr process_attr
,
1508 enum lttng_tracking_policy
*policy
)
1510 enum lttng_error_code ret_code
= LTTNG_OK
;
1511 const struct process_attr_tracker
*tracker
;
1514 case LTTNG_DOMAIN_KERNEL
:
1515 if (!session
->kernel_session
) {
1516 ret_code
= LTTNG_ERR_INVALID
;
1519 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1521 case LTTNG_DOMAIN_UST
:
1522 if (!session
->ust_session
) {
1523 ret_code
= LTTNG_ERR_INVALID
;
1526 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1529 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1533 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1535 ret_code
= LTTNG_ERR_INVALID
;
1541 enum lttng_error_code
1542 cmd_process_attr_tracker_set_tracking_policy(struct ltt_session
*session
,
1543 enum lttng_domain_type domain
,
1544 enum lttng_process_attr process_attr
,
1545 enum lttng_tracking_policy policy
)
1547 enum lttng_error_code ret_code
= LTTNG_OK
;
1550 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1551 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1552 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1555 ret_code
= LTTNG_ERR_INVALID
;
1560 case LTTNG_DOMAIN_KERNEL
:
1561 if (!session
->kernel_session
) {
1562 ret_code
= LTTNG_ERR_INVALID
;
1565 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1566 session
->kernel_session
, process_attr
, policy
);
1568 case LTTNG_DOMAIN_UST
:
1569 if (!session
->ust_session
) {
1570 ret_code
= LTTNG_ERR_INVALID
;
1573 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1574 session
->ust_session
, process_attr
, policy
);
1577 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1584 enum lttng_error_code
1585 cmd_process_attr_tracker_inclusion_set_add_value(struct ltt_session
*session
,
1586 enum lttng_domain_type domain
,
1587 enum lttng_process_attr process_attr
,
1588 const struct process_attr_value
*value
)
1590 enum lttng_error_code ret_code
= LTTNG_OK
;
1593 case LTTNG_DOMAIN_KERNEL
:
1594 if (!session
->kernel_session
) {
1595 ret_code
= LTTNG_ERR_INVALID
;
1598 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1599 session
->kernel_session
, process_attr
, value
);
1601 case LTTNG_DOMAIN_UST
:
1602 if (!session
->ust_session
) {
1603 ret_code
= LTTNG_ERR_INVALID
;
1606 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1607 session
->ust_session
, process_attr
, value
);
1610 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1617 enum lttng_error_code
1618 cmd_process_attr_tracker_inclusion_set_remove_value(struct ltt_session
*session
,
1619 enum lttng_domain_type domain
,
1620 enum lttng_process_attr process_attr
,
1621 const struct process_attr_value
*value
)
1623 enum lttng_error_code ret_code
= LTTNG_OK
;
1626 case LTTNG_DOMAIN_KERNEL
:
1627 if (!session
->kernel_session
) {
1628 ret_code
= LTTNG_ERR_INVALID
;
1631 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1632 session
->kernel_session
, process_attr
, value
);
1634 case LTTNG_DOMAIN_UST
:
1635 if (!session
->ust_session
) {
1636 ret_code
= LTTNG_ERR_INVALID
;
1639 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1640 session
->ust_session
, process_attr
, value
);
1643 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1650 enum lttng_error_code
1651 cmd_process_attr_tracker_get_inclusion_set(struct ltt_session
*session
,
1652 enum lttng_domain_type domain
,
1653 enum lttng_process_attr process_attr
,
1654 struct lttng_process_attr_values
**values
)
1656 enum lttng_error_code ret_code
= LTTNG_OK
;
1657 const struct process_attr_tracker
*tracker
;
1658 enum process_attr_tracker_status status
;
1661 case LTTNG_DOMAIN_KERNEL
:
1662 if (!session
->kernel_session
) {
1663 ret_code
= LTTNG_ERR_INVALID
;
1666 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1668 case LTTNG_DOMAIN_UST
:
1669 if (!session
->ust_session
) {
1670 ret_code
= LTTNG_ERR_INVALID
;
1673 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1676 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1681 ret_code
= LTTNG_ERR_INVALID
;
1685 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1687 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1688 ret_code
= LTTNG_OK
;
1690 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1691 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1693 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1694 ret_code
= LTTNG_ERR_NOMEM
;
1697 ret_code
= LTTNG_ERR_UNK
;
1706 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1708 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1709 struct lttng_event
*event
,
1710 char *filter_expression
,
1711 struct lttng_bytecode
*bytecode
,
1712 struct lttng_event_exclusion
*exclusion
)
1715 const char *event_name
;
1716 const struct ltt_session
*session
= cmd_ctx
->session
;
1717 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1718 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1720 DBG("Disable event command for event \'%s\'", event
->name
);
1723 * Filter and exclusions are simply not handled by the
1724 * disable event command at this time.
1728 (void) filter_expression
;
1731 /* Ignore the presence of filter or exclusion for the event */
1733 event
->exclusion
= 0;
1735 event_name
= event
->name
;
1737 lttng::urcu::read_lock_guard read_lock
;
1739 /* Error out on unhandled search criteria */
1740 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
|| event
->pid
||
1741 event
->filter
|| event
->exclusion
) {
1742 ret
= LTTNG_ERR_UNK
;
1747 case LTTNG_DOMAIN_KERNEL
:
1749 struct ltt_kernel_channel
*kchan
;
1750 struct ltt_kernel_session
*ksess
;
1752 ksess
= session
->kernel_session
;
1755 * If a non-default channel has been created in the
1756 * session, explicitely require that -c chan_name needs
1759 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1760 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1764 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1765 if (kchan
== nullptr) {
1766 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1770 switch (event
->type
) {
1771 case LTTNG_EVENT_ALL
:
1772 case LTTNG_EVENT_TRACEPOINT
:
1773 case LTTNG_EVENT_SYSCALL
:
1774 case LTTNG_EVENT_PROBE
:
1775 case LTTNG_EVENT_FUNCTION
:
1776 case LTTNG_EVENT_FUNCTION_ENTRY
: /* fall-through */
1777 if (event_name
[0] == '\0') {
1778 ret
= event_kernel_disable_event(kchan
, nullptr, event
->type
);
1780 ret
= event_kernel_disable_event(kchan
, event_name
, event
->type
);
1782 if (ret
!= LTTNG_OK
) {
1787 ret
= LTTNG_ERR_UNK
;
1791 kernel_wait_quiescent();
1794 case LTTNG_DOMAIN_UST
:
1796 struct ltt_ust_channel
*uchan
;
1797 struct ltt_ust_session
*usess
;
1799 usess
= session
->ust_session
;
1801 if (validate_ust_event_name(event_name
)) {
1802 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1807 * If a non-default channel has been created in the
1808 * session, explicitly require that -c chan_name needs
1811 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1812 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1816 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
1817 if (uchan
== nullptr) {
1818 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1822 switch (event
->type
) {
1823 case LTTNG_EVENT_ALL
:
1825 * An empty event name means that everything
1826 * should be disabled.
1828 if (event
->name
[0] == '\0') {
1829 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1831 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1833 if (ret
!= LTTNG_OK
) {
1838 ret
= LTTNG_ERR_UNK
;
1842 DBG3("Disable UST event %s in channel %s completed", event_name
, channel_name
);
1845 case LTTNG_DOMAIN_LOG4J
:
1846 case LTTNG_DOMAIN_JUL
:
1847 case LTTNG_DOMAIN_PYTHON
:
1850 struct ltt_ust_session
*usess
= session
->ust_session
;
1852 LTTNG_ASSERT(usess
);
1854 switch (event
->type
) {
1855 case LTTNG_EVENT_ALL
:
1858 ret
= LTTNG_ERR_UNK
;
1862 agt
= trace_ust_find_agent(usess
, domain
);
1864 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1868 * An empty event name means that everything
1869 * should be disabled.
1871 if (event
->name
[0] == '\0') {
1872 ret
= event_agent_disable_all(usess
, agt
);
1874 ret
= event_agent_disable(usess
, agt
, event_name
);
1876 if (ret
!= LTTNG_OK
) {
1883 ret
= LTTNG_ERR_UND
;
1893 free(filter_expression
);
1898 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1900 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1901 const struct lttng_event_context
*event_context
,
1904 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1905 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1906 const struct ltt_session
*session
= cmd_ctx
->session
;
1907 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1910 * Don't try to add a context if the session has been started at
1911 * some point in time before. The tracer does not allow it and would
1912 * result in a corrupted trace.
1914 if (cmd_ctx
->session
->has_been_started
) {
1915 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1920 case LTTNG_DOMAIN_KERNEL
:
1921 LTTNG_ASSERT(session
->kernel_session
);
1923 if (session
->kernel_session
->channel_count
== 0) {
1924 /* Create default channel */
1925 ret
= channel_kernel_create(session
->kernel_session
, nullptr, kwpipe
);
1926 if (ret
!= LTTNG_OK
) {
1929 chan_kern_created
= 1;
1931 /* Add kernel context to kernel tracer */
1932 ret
= context_kernel_add(session
->kernel_session
, event_context
, channel_name
);
1933 if (ret
!= LTTNG_OK
) {
1937 case LTTNG_DOMAIN_JUL
:
1938 case LTTNG_DOMAIN_LOG4J
:
1941 * Validate channel name.
1942 * If no channel name is given and the domain is JUL or LOG4J,
1943 * set it to the appropriate domain-specific channel name. If
1944 * a name is provided but does not match the expexted channel
1945 * name, return an error.
1947 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1948 strcmp(channel_name
, DEFAULT_JUL_CHANNEL_NAME
) != 0) {
1949 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1951 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1952 strcmp(channel_name
, DEFAULT_LOG4J_CHANNEL_NAME
) != 0) {
1953 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1958 case LTTNG_DOMAIN_UST
:
1960 struct ltt_ust_session
*usess
= session
->ust_session
;
1961 unsigned int chan_count
;
1963 LTTNG_ASSERT(usess
);
1965 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1966 if (chan_count
== 0) {
1967 struct lttng_channel
*attr
;
1968 /* Create default channel */
1969 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1970 if (attr
== nullptr) {
1971 ret
= LTTNG_ERR_FATAL
;
1975 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1976 if (ret
!= LTTNG_OK
) {
1980 channel_attr_destroy(attr
);
1981 chan_ust_created
= 1;
1984 ret
= context_ust_add(usess
, domain
, event_context
, channel_name
);
1985 if (ret
!= LTTNG_OK
) {
1991 ret
= LTTNG_ERR_UND
;
1999 if (chan_kern_created
) {
2000 struct ltt_kernel_channel
*kchan
= trace_kernel_get_channel_by_name(
2001 DEFAULT_CHANNEL_NAME
, session
->kernel_session
);
2002 /* Created previously, this should NOT fail. */
2003 LTTNG_ASSERT(kchan
);
2004 kernel_destroy_channel(kchan
);
2007 if (chan_ust_created
) {
2008 struct ltt_ust_channel
*uchan
= trace_ust_find_channel_by_name(
2009 session
->ust_session
->domain_global
.channels
, DEFAULT_CHANNEL_NAME
);
2010 /* Created previously, this should NOT fail. */
2011 LTTNG_ASSERT(uchan
);
2012 /* Remove from the channel list of the session. */
2013 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
, uchan
);
2014 trace_ust_destroy_channel(uchan
);
2020 static inline bool name_starts_with(const char *name
, const char *prefix
)
2022 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2024 return !strncmp(name
, prefix
, max_cmp_len
);
2027 /* Perform userspace-specific event name validation */
2028 static int validate_ust_event_name(const char *name
)
2038 * Check name against all internal UST event component namespaces used
2041 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2042 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2043 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2052 * Internal version of cmd_enable_event() with a supplemental
2053 * "internal_event" flag which is used to enable internal events which should
2054 * be hidden from clients. Such events are used in the agent implementation to
2055 * enable the events through which all "agent" events are funeled.
2057 static int _cmd_enable_event(struct ltt_session
*session
,
2058 const struct lttng_domain
*domain
,
2060 struct lttng_event
*event
,
2061 char *filter_expression
,
2062 struct lttng_bytecode
*filter
,
2063 struct lttng_event_exclusion
*exclusion
,
2065 bool internal_event
)
2067 int ret
= 0, channel_created
= 0;
2068 struct lttng_channel
*attr
= nullptr;
2070 LTTNG_ASSERT(session
);
2071 LTTNG_ASSERT(event
);
2072 LTTNG_ASSERT(channel_name
);
2074 /* If we have a filter, we must have its filter expression */
2075 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2077 /* Normalize event name as a globbing pattern */
2078 strutils_normalize_star_glob_pattern(event
->name
);
2080 /* Normalize exclusion names as globbing patterns */
2084 for (i
= 0; i
< exclusion
->count
; i
++) {
2085 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2087 strutils_normalize_star_glob_pattern(name
);
2091 DBG("Enable event command for event \'%s\'", event
->name
);
2093 lttng::urcu::read_lock_guard read_lock
;
2095 switch (domain
->type
) {
2096 case LTTNG_DOMAIN_KERNEL
:
2098 struct ltt_kernel_channel
*kchan
;
2101 * If a non-default channel has been created in the
2102 * session, explicitely require that -c chan_name needs
2105 if (session
->kernel_session
->has_non_default_channel
&& channel_name
[0] == '\0') {
2106 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2110 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2111 if (kchan
== nullptr) {
2112 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
, LTTNG_BUFFER_GLOBAL
);
2113 if (attr
== nullptr) {
2114 ret
= LTTNG_ERR_FATAL
;
2117 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2118 ret
= LTTNG_ERR_INVALID
;
2122 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2123 if (ret
!= LTTNG_OK
) {
2126 channel_created
= 1;
2129 /* Get the newly created kernel channel pointer */
2130 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2131 if (kchan
== nullptr) {
2132 /* This sould not happen... */
2133 ret
= LTTNG_ERR_FATAL
;
2137 switch (event
->type
) {
2138 case LTTNG_EVENT_ALL
:
2140 char *filter_expression_a
= nullptr;
2141 struct lttng_bytecode
*filter_a
= nullptr;
2144 * We need to duplicate filter_expression and filter,
2145 * because ownership is passed to first enable
2148 if (filter_expression
) {
2149 filter_expression_a
= strdup(filter_expression
);
2150 if (!filter_expression_a
) {
2151 ret
= LTTNG_ERR_FATAL
;
2156 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2158 free(filter_expression_a
);
2159 ret
= LTTNG_ERR_FATAL
;
2162 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2164 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2165 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2166 /* We have passed ownership */
2167 filter_expression
= nullptr;
2169 if (ret
!= LTTNG_OK
) {
2170 if (channel_created
) {
2171 /* Let's not leak a useless channel. */
2172 kernel_destroy_channel(kchan
);
2174 free(filter_expression_a
);
2178 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2179 ret
= event_kernel_enable_event(
2180 kchan
, event
, filter_expression_a
, filter_a
);
2181 /* We have passed ownership */
2182 filter_expression_a
= nullptr;
2184 if (ret
!= LTTNG_OK
) {
2189 case LTTNG_EVENT_PROBE
:
2190 case LTTNG_EVENT_USERSPACE_PROBE
:
2191 case LTTNG_EVENT_FUNCTION
:
2192 case LTTNG_EVENT_FUNCTION_ENTRY
:
2193 case LTTNG_EVENT_TRACEPOINT
:
2194 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2195 /* We have passed ownership */
2196 filter_expression
= nullptr;
2198 if (ret
!= LTTNG_OK
) {
2199 if (channel_created
) {
2200 /* Let's not leak a useless channel. */
2201 kernel_destroy_channel(kchan
);
2206 case LTTNG_EVENT_SYSCALL
:
2207 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2208 /* We have passed ownership */
2209 filter_expression
= nullptr;
2211 if (ret
!= LTTNG_OK
) {
2216 ret
= LTTNG_ERR_UNK
;
2220 kernel_wait_quiescent();
2223 case LTTNG_DOMAIN_UST
:
2225 struct ltt_ust_channel
*uchan
;
2226 struct ltt_ust_session
*usess
= session
->ust_session
;
2228 LTTNG_ASSERT(usess
);
2231 * If a non-default channel has been created in the
2232 * session, explicitely require that -c chan_name needs
2235 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2236 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2240 /* Get channel from global UST domain */
2241 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
2242 if (uchan
== nullptr) {
2243 /* Create default channel */
2244 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, usess
->buffer_type
);
2245 if (attr
== nullptr) {
2246 ret
= LTTNG_ERR_FATAL
;
2249 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2250 ret
= LTTNG_ERR_INVALID
;
2254 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2255 if (ret
!= LTTNG_OK
) {
2259 /* Get the newly created channel reference back */
2260 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2262 LTTNG_ASSERT(uchan
);
2265 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2267 * Don't allow users to add UST events to channels which
2268 * are assigned to a userspace subdomain (JUL, Log4J,
2271 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2275 if (!internal_event
) {
2277 * Ensure the event name is not reserved for internal
2280 ret
= validate_ust_event_name(event
->name
);
2282 WARN("Userspace event name %s failed validation.", event
->name
);
2283 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2288 /* At this point, the session and channel exist on the tracer */
2289 ret
= event_ust_enable_tracepoint(
2290 usess
, uchan
, event
, filter_expression
, filter
, exclusion
, internal_event
);
2291 /* We have passed ownership */
2292 filter_expression
= nullptr;
2294 exclusion
= nullptr;
2295 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2296 goto already_enabled
;
2297 } else if (ret
!= LTTNG_OK
) {
2302 case LTTNG_DOMAIN_LOG4J
:
2303 case LTTNG_DOMAIN_JUL
:
2304 case LTTNG_DOMAIN_PYTHON
:
2306 const char *default_event_name
, *default_chan_name
;
2308 struct lttng_event uevent
;
2309 struct lttng_domain tmp_dom
;
2310 struct ltt_ust_session
*usess
= session
->ust_session
;
2312 LTTNG_ASSERT(usess
);
2314 if (!agent_tracing_is_enabled()) {
2315 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2316 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2320 agt
= trace_ust_find_agent(usess
, domain
->type
);
2322 agt
= agent_create(domain
->type
);
2324 ret
= LTTNG_ERR_NOMEM
;
2327 agent_add(agt
, usess
->agents
);
2330 /* Create the default tracepoint. */
2331 memset(&uevent
, 0, sizeof(uevent
));
2332 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2333 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2334 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
2335 if (!default_event_name
) {
2336 ret
= LTTNG_ERR_FATAL
;
2339 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2340 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2343 * The domain type is changed because we are about to enable the
2344 * default channel and event for the JUL domain that are hardcoded.
2345 * This happens in the UST domain.
2347 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2348 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2350 switch (domain
->type
) {
2351 case LTTNG_DOMAIN_LOG4J
:
2352 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2354 case LTTNG_DOMAIN_JUL
:
2355 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2357 case LTTNG_DOMAIN_PYTHON
:
2358 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2361 /* The switch/case we are in makes this impossible */
2366 char *filter_expression_copy
= nullptr;
2367 struct lttng_bytecode
*filter_copy
= nullptr;
2370 const size_t filter_size
=
2371 sizeof(struct lttng_bytecode
) + filter
->len
;
2373 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2375 ret
= LTTNG_ERR_NOMEM
;
2378 memcpy(filter_copy
, filter
, filter_size
);
2380 filter_expression_copy
= strdup(filter_expression
);
2381 if (!filter_expression
) {
2382 ret
= LTTNG_ERR_NOMEM
;
2385 if (!filter_expression_copy
|| !filter_copy
) {
2386 free(filter_expression_copy
);
2392 ret
= cmd_enable_event_internal(session
,
2394 (char *) default_chan_name
,
2396 filter_expression_copy
,
2402 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2403 goto already_enabled
;
2404 } else if (ret
!= LTTNG_OK
) {
2408 /* The wild card * means that everything should be enabled. */
2409 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2410 ret
= event_agent_enable_all(usess
, agt
, event
, filter
, filter_expression
);
2412 ret
= event_agent_enable(usess
, agt
, event
, filter
, filter_expression
);
2415 filter_expression
= nullptr;
2416 if (ret
!= LTTNG_OK
) {
2423 ret
= LTTNG_ERR_UND
;
2431 free(filter_expression
);
2434 channel_attr_destroy(attr
);
2439 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2440 * We own filter, exclusion, and filter_expression.
2442 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2443 struct lttng_event
*event
,
2444 char *filter_expression
,
2445 struct lttng_event_exclusion
*exclusion
,
2446 struct lttng_bytecode
*bytecode
,
2451 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2453 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2456 * The ownership of the following parameters is transferred to
2457 * _cmd_enable_event:
2459 * - filter_expression,
2463 ret
= _cmd_enable_event(cmd_ctx
->session
,
2465 cmd_ctx
->lsm
.u
.enable
.channel_name
,
2472 filter_expression
= nullptr;
2474 exclusion
= nullptr;
2479 * Enable an event which is internal to LTTng. An internal should
2480 * never be made visible to clients and are immune to checks such as
2483 static int cmd_enable_event_internal(struct ltt_session
*session
,
2484 const struct lttng_domain
*domain
,
2486 struct lttng_event
*event
,
2487 char *filter_expression
,
2488 struct lttng_bytecode
*filter
,
2489 struct lttng_event_exclusion
*exclusion
,
2492 return _cmd_enable_event(session
,
2504 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2506 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2507 struct lttng_payload
*reply_payload
)
2509 enum lttng_error_code ret_code
;
2511 ssize_t i
, nb_events
= 0;
2512 struct lttng_event
*events
= nullptr;
2513 struct lttcomm_list_command_header reply_command_header
= {};
2514 size_t reply_command_header_offset
;
2516 assert(reply_payload
);
2518 /* Reserve space for command reply header. */
2519 reply_command_header_offset
= reply_payload
->buffer
.size
;
2520 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2521 reply_command_header_offset
+
2522 sizeof(struct lttcomm_list_command_header
));
2524 ret_code
= LTTNG_ERR_NOMEM
;
2529 case LTTNG_DOMAIN_KERNEL
:
2530 nb_events
= kernel_list_events(&events
);
2531 if (nb_events
< 0) {
2532 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2536 case LTTNG_DOMAIN_UST
:
2537 nb_events
= ust_app_list_events(&events
);
2538 if (nb_events
< 0) {
2539 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2543 case LTTNG_DOMAIN_LOG4J
:
2544 case LTTNG_DOMAIN_JUL
:
2545 case LTTNG_DOMAIN_PYTHON
:
2546 nb_events
= agent_list_events(&events
, domain
);
2547 if (nb_events
< 0) {
2548 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2553 ret_code
= LTTNG_ERR_UND
;
2557 for (i
= 0; i
< nb_events
; i
++) {
2558 ret
= lttng_event_serialize(
2559 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2561 ret_code
= LTTNG_ERR_NOMEM
;
2566 if (nb_events
> UINT32_MAX
) {
2567 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2568 ret_code
= LTTNG_ERR_OVERFLOW
;
2572 /* Update command reply header. */
2573 reply_command_header
.count
= (uint32_t) nb_events
;
2574 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2575 &reply_command_header
,
2576 sizeof(reply_command_header
));
2578 ret_code
= LTTNG_OK
;
2585 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2587 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2588 struct lttng_payload
*reply
)
2590 enum lttng_error_code ret_code
;
2592 unsigned int i
, nb_fields
;
2593 struct lttng_event_field
*fields
= nullptr;
2594 struct lttcomm_list_command_header reply_command_header
= {};
2595 size_t reply_command_header_offset
;
2599 /* Reserve space for command reply header. */
2600 reply_command_header_offset
= reply
->buffer
.size
;
2601 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2602 reply_command_header_offset
+
2603 sizeof(struct lttcomm_list_command_header
));
2605 ret_code
= LTTNG_ERR_NOMEM
;
2610 case LTTNG_DOMAIN_UST
:
2611 ret
= ust_app_list_event_fields(&fields
);
2613 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2618 case LTTNG_DOMAIN_KERNEL
:
2619 default: /* fall-through */
2620 ret_code
= LTTNG_ERR_UND
;
2626 for (i
= 0; i
< nb_fields
; i
++) {
2627 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2629 ret_code
= LTTNG_ERR_NOMEM
;
2634 if (nb_fields
> UINT32_MAX
) {
2635 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2636 ret_code
= LTTNG_ERR_OVERFLOW
;
2640 /* Update command reply header. */
2641 reply_command_header
.count
= (uint32_t) nb_fields
;
2643 memcpy(reply
->buffer
.data
+ reply_command_header_offset
,
2644 &reply_command_header
,
2645 sizeof(reply_command_header
));
2647 ret_code
= LTTNG_OK
;
2654 enum lttng_error_code
cmd_list_syscalls(struct lttng_payload
*reply_payload
)
2656 enum lttng_error_code ret_code
;
2657 ssize_t nb_events
, i
;
2659 struct lttng_event
*events
= nullptr;
2660 struct lttcomm_list_command_header reply_command_header
= {};
2661 size_t reply_command_header_offset
;
2663 assert(reply_payload
);
2665 /* Reserve space for command reply header. */
2666 reply_command_header_offset
= reply_payload
->buffer
.size
;
2667 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2668 reply_command_header_offset
+
2669 sizeof(struct lttcomm_list_command_header
));
2671 ret_code
= LTTNG_ERR_NOMEM
;
2675 nb_events
= syscall_table_list(&events
);
2676 if (nb_events
< 0) {
2677 ret_code
= (enum lttng_error_code
) - nb_events
;
2681 for (i
= 0; i
< nb_events
; i
++) {
2682 ret
= lttng_event_serialize(
2683 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2685 ret_code
= LTTNG_ERR_NOMEM
;
2690 if (nb_events
> UINT32_MAX
) {
2691 ERR("Syscall count would overflow the syscall listing command's reply");
2692 ret_code
= LTTNG_ERR_OVERFLOW
;
2696 /* Update command reply header. */
2697 reply_command_header
.count
= (uint32_t) nb_events
;
2698 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2699 &reply_command_header
,
2700 sizeof(reply_command_header
));
2702 ret_code
= LTTNG_OK
;
2709 * Command LTTNG_START_TRACE processed by the client thread.
2711 * Called with session mutex held.
2713 int cmd_start_trace(struct ltt_session
*session
)
2715 enum lttng_error_code ret
;
2716 unsigned long nb_chan
= 0;
2717 struct ltt_kernel_session
*ksession
;
2718 struct ltt_ust_session
*usess
;
2719 const bool session_rotated_after_last_stop
= session
->rotated_after_last_stop
;
2720 const bool session_cleared_after_last_stop
= session
->cleared_after_last_stop
;
2722 LTTNG_ASSERT(session
);
2724 /* Ease our life a bit ;) */
2725 ksession
= session
->kernel_session
;
2726 usess
= session
->ust_session
;
2728 /* Is the session already started? */
2729 if (session
->active
) {
2730 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2731 /* Perform nothing */
2735 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2736 !session
->current_trace_chunk
) {
2738 * A rotation was launched while the session was stopped and
2739 * it has not been completed yet. It is not possible to start
2740 * the session since starting the session here would require a
2741 * rotation from "NULL" to a new trace chunk. That rotation
2742 * would overlap with the ongoing rotation, which is not
2745 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2747 ret
= LTTNG_ERR_ROTATION_PENDING
;
2752 * Starting a session without channel is useless since after that it's not
2753 * possible to enable channel thus inform the client.
2755 if (usess
&& usess
->domain_global
.channels
) {
2756 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2759 nb_chan
+= ksession
->channel_count
;
2762 ret
= LTTNG_ERR_NO_CHANNEL
;
2766 session
->active
= true;
2767 session
->rotated_after_last_stop
= false;
2768 session
->cleared_after_last_stop
= false;
2769 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2770 if (!session
->has_been_started
) {
2771 struct lttng_trace_chunk
*trace_chunk
;
2773 DBG("Creating initial trace chunk of session \"%s\"", session
->name
);
2775 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
2777 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2780 LTTNG_ASSERT(!session
->current_trace_chunk
);
2781 ret
= (lttng_error_code
) session_set_trace_chunk(
2782 session
, trace_chunk
, nullptr);
2783 lttng_trace_chunk_put(trace_chunk
);
2785 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2789 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2792 * Rotate existing streams into the new chunk.
2793 * This is a "quiet" rotation has no client has
2794 * explicitly requested this operation.
2796 * There is also no need to wait for the rotation
2797 * to complete as it will happen immediately. No data
2798 * was produced as the session was stopped, so the
2799 * rotation should happen on reception of the command.
2801 ret
= (lttng_error_code
) cmd_rotate_session(
2802 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2803 if (ret
!= LTTNG_OK
) {
2809 /* Kernel tracing */
2810 if (ksession
!= nullptr) {
2811 DBG("Start kernel tracing session %s", session
->name
);
2812 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2813 if (ret
!= LTTNG_OK
) {
2818 /* Flag session that trace should start automatically */
2820 int int_ret
= ust_app_start_trace_all(usess
);
2823 ret
= LTTNG_ERR_UST_START_FAIL
;
2829 * Open a packet in every stream of the session to ensure that viewers
2830 * can correctly identify the boundaries of the periods during which
2831 * tracing was active for this session.
2833 ret
= session_open_packets(session
);
2834 if (ret
!= LTTNG_OK
) {
2839 * Clear the flag that indicates that a rotation was done while the
2840 * session was stopped.
2842 session
->rotated_after_last_stop
= false;
2844 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2845 int int_ret
= timer_session_rotation_schedule_timer_start(
2846 session
, session
->rotate_timer_period
);
2849 ERR("Failed to enable rotate timer");
2850 ret
= LTTNG_ERR_UNK
;
2858 if (ret
== LTTNG_OK
) {
2859 /* Flag this after a successful start. */
2860 session
->has_been_started
= true;
2862 session
->active
= false;
2863 /* Restore initial state on error. */
2864 session
->rotated_after_last_stop
= session_rotated_after_last_stop
;
2865 session
->cleared_after_last_stop
= session_cleared_after_last_stop
;
2872 * Command LTTNG_STOP_TRACE processed by the client thread.
2874 int cmd_stop_trace(struct ltt_session
*session
)
2877 struct ltt_kernel_session
*ksession
;
2878 struct ltt_ust_session
*usess
;
2880 LTTNG_ASSERT(session
);
2882 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2884 ksession
= session
->kernel_session
;
2885 usess
= session
->ust_session
;
2887 /* Session is not active. Skip everything and inform the client. */
2888 if (!session
->active
) {
2889 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2893 ret
= stop_kernel_session(ksession
);
2894 if (ret
!= LTTNG_OK
) {
2898 if (usess
&& usess
->active
) {
2899 ret
= ust_app_stop_trace_all(usess
);
2901 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2906 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2907 /* Flag inactive after a successful stop. */
2908 session
->active
= false;
2916 * Set the base_path of the session only if subdir of a control uris is set.
2917 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2920 set_session_base_path_from_uris(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2925 for (i
= 0; i
< nb_uri
; i
++) {
2926 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
|| uris
[i
].subdir
[0] == '\0') {
2927 /* Not interested in these URIs */
2931 if (session
->base_path
!= nullptr) {
2932 free(session
->base_path
);
2933 session
->base_path
= nullptr;
2936 /* Set session base_path */
2937 session
->base_path
= strdup(uris
[i
].subdir
);
2938 if (!session
->base_path
) {
2939 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2942 ret
= LTTNG_ERR_NOMEM
;
2945 DBG2("Setting base path \"%s\" for session \"%s\"",
2955 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2957 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2960 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2961 struct ltt_ust_session
*usess
= session
->ust_session
;
2963 LTTNG_ASSERT(session
);
2965 LTTNG_ASSERT(nb_uri
> 0);
2967 /* Can't set consumer URI if the session is active. */
2968 if (session
->active
) {
2969 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2974 * Set the session base path if any. This is done inside
2975 * cmd_set_consumer_uri to preserve backward compatibility of the
2976 * previous session creation api vs the session descriptor api.
2978 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2979 if (ret
!= LTTNG_OK
) {
2983 /* Set the "global" consumer URIs */
2984 for (i
= 0; i
< nb_uri
; i
++) {
2985 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
], LTTNG_DOMAIN_NONE
);
2986 if (ret
!= LTTNG_OK
) {
2991 /* Set UST session URIs */
2992 if (session
->ust_session
) {
2993 for (i
= 0; i
< nb_uri
; i
++) {
2994 ret
= add_uri_to_consumer(session
,
2995 session
->ust_session
->consumer
,
2998 if (ret
!= LTTNG_OK
) {
3004 /* Set kernel session URIs */
3005 if (session
->kernel_session
) {
3006 for (i
= 0; i
< nb_uri
; i
++) {
3007 ret
= add_uri_to_consumer(session
,
3008 session
->kernel_session
->consumer
,
3010 LTTNG_DOMAIN_KERNEL
);
3011 if (ret
!= LTTNG_OK
) {
3018 * Make sure to set the session in output mode after we set URI since a
3019 * session can be created without URL (thus flagged in no output mode).
3021 session
->output_traces
= 1;
3023 ksess
->output_traces
= 1;
3027 usess
->output_traces
= 1;
3037 static enum lttng_error_code
3038 set_session_output_from_descriptor(struct ltt_session
*session
,
3039 const struct lttng_session_descriptor
*descriptor
)
3042 enum lttng_error_code ret_code
= LTTNG_OK
;
3043 enum lttng_session_descriptor_type session_type
=
3044 lttng_session_descriptor_get_type(descriptor
);
3045 enum lttng_session_descriptor_output_type output_type
=
3046 lttng_session_descriptor_get_output_type(descriptor
);
3047 struct lttng_uri uris
[2] = {};
3048 size_t uri_count
= 0;
3050 switch (output_type
) {
3051 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3053 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3054 lttng_session_descriptor_get_local_output_uri(descriptor
, &uris
[0]);
3057 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3058 lttng_session_descriptor_get_network_output_uris(descriptor
, &uris
[0], &uris
[1]);
3062 ret_code
= LTTNG_ERR_INVALID
;
3066 switch (session_type
) {
3067 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3069 struct snapshot_output
*new_output
= nullptr;
3071 new_output
= snapshot_output_alloc();
3073 ret_code
= LTTNG_ERR_NOMEM
;
3077 ret
= snapshot_output_init_with_uri(session
,
3078 DEFAULT_SNAPSHOT_MAX_SIZE
,
3084 &session
->snapshot
);
3086 ret_code
= (ret
== -ENOMEM
) ? LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3087 snapshot_output_destroy(new_output
);
3090 snapshot_add_output(&session
->snapshot
, new_output
);
3093 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3094 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3096 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3100 ret_code
= LTTNG_ERR_INVALID
;
3107 static enum lttng_error_code
3108 cmd_create_session_from_descriptor(struct lttng_session_descriptor
*descriptor
,
3109 const lttng_sock_cred
*creds
,
3110 const char *home_path
)
3113 enum lttng_error_code ret_code
;
3114 const char *session_name
;
3115 struct ltt_session
*new_session
= nullptr;
3116 enum lttng_session_descriptor_status descriptor_status
;
3118 session_lock_list();
3120 if (*home_path
!= '/') {
3121 ERR("Home path provided by client is not absolute");
3122 ret_code
= LTTNG_ERR_INVALID
;
3127 descriptor_status
= lttng_session_descriptor_get_session_name(descriptor
, &session_name
);
3128 switch (descriptor_status
) {
3129 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3131 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3132 session_name
= nullptr;
3135 ret_code
= LTTNG_ERR_INVALID
;
3139 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, &new_session
);
3140 if (ret_code
!= LTTNG_OK
) {
3144 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3149 if (ret_code
!= LTTNG_OK
) {
3153 /* Announce the session's destruction to the notification thread when it is destroyed. */
3154 ret
= session_add_destroy_notifier(
3156 [](const struct ltt_session
*session
, void *user_data
__attribute__((unused
))) {
3157 (void) notification_thread_command_remove_session(
3158 the_notification_thread_handle
, session
->id
);
3162 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3164 ret
= LTTNG_ERR_NOMEM
;
3168 if (!session_name
) {
3169 ret
= lttng_session_descriptor_set_session_name(descriptor
, new_session
->name
);
3171 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3176 if (!lttng_session_descriptor_is_output_destination_initialized(descriptor
)) {
3178 * Only include the session's creation time in the output
3179 * destination if the name of the session itself was
3180 * not auto-generated.
3182 ret_code
= lttng_session_descriptor_set_default_output(
3184 session_name
? &new_session
->creation_time
: nullptr,
3186 if (ret_code
!= LTTNG_OK
) {
3190 new_session
->has_user_specified_directory
=
3191 lttng_session_descriptor_has_output_directory(descriptor
);
3194 switch (lttng_session_descriptor_get_type(descriptor
)) {
3195 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3196 new_session
->snapshot_mode
= 1;
3198 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3199 new_session
->live_timer
=
3200 lttng_session_descriptor_live_get_timer_interval(descriptor
);
3206 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3207 if (ret_code
!= LTTNG_OK
) {
3210 new_session
->consumer
->enabled
= true;
3211 ret_code
= LTTNG_OK
;
3213 /* Release reference provided by the session_create function. */
3214 session_put(new_session
);
3215 if (ret_code
!= LTTNG_OK
&& new_session
) {
3216 /* Release the global reference on error. */
3217 session_destroy(new_session
);
3219 session_unlock_list();
3223 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
,
3225 struct lttng_session_descriptor
**return_descriptor
)
3228 size_t payload_size
;
3229 struct lttng_dynamic_buffer payload
;
3230 struct lttng_buffer_view home_dir_view
;
3231 struct lttng_buffer_view session_descriptor_view
;
3232 struct lttng_session_descriptor
*session_descriptor
= nullptr;
3233 enum lttng_error_code ret_code
;
3235 lttng_dynamic_buffer_init(&payload
);
3236 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>= LTTNG_PATH_MAX
) {
3237 ret_code
= LTTNG_ERR_INVALID
;
3240 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3241 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3242 ret_code
= LTTNG_ERR_INVALID
;
3246 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3247 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3248 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3250 ret_code
= LTTNG_ERR_NOMEM
;
3254 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3256 ERR("Reception of session descriptor failed, aborting.");
3257 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3261 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3262 &payload
, 0, cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3263 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3264 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3265 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3266 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3270 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3272 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3273 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3274 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3275 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3276 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3280 ret
= lttng_session_descriptor_create_from_buffer(&session_descriptor_view
,
3281 &session_descriptor
);
3283 ERR("Failed to create session descriptor from payload of \"create session\" command");
3284 ret_code
= LTTNG_ERR_INVALID
;
3289 * Sets the descriptor's auto-generated properties (name, output) if
3292 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3294 home_dir_view
.size
? home_dir_view
.data
:
3296 if (ret_code
!= LTTNG_OK
) {
3300 ret_code
= LTTNG_OK
;
3301 *return_descriptor
= session_descriptor
;
3302 session_descriptor
= nullptr;
3304 lttng_dynamic_buffer_reset(&payload
);
3305 lttng_session_descriptor_destroy(session_descriptor
);
3309 static void cmd_destroy_session_reply(const struct ltt_session
*session
, void *_reply_context
)
3313 const struct cmd_destroy_session_reply_context
*reply_context
=
3314 (cmd_destroy_session_reply_context
*) _reply_context
;
3315 struct lttng_dynamic_buffer payload
;
3316 struct lttcomm_session_destroy_command_header cmd_header
;
3317 struct lttng_trace_archive_location
*location
= nullptr;
3318 struct lttcomm_lttng_msg llm
= {
3319 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION
,
3320 .ret_code
= reply_context
->destruction_status
,
3322 .cmd_header_size
= sizeof(struct lttcomm_session_destroy_command_header
),
3326 size_t payload_size_before_location
;
3328 lttng_dynamic_buffer_init(&payload
);
3330 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3332 ERR("Failed to append session destruction message");
3336 cmd_header
.rotation_state
= (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3337 session
->rotation_state
:
3338 LTTNG_ROTATION_STATE_NO_ROTATION
);
3339 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
, sizeof(cmd_header
));
3341 ERR("Failed to append session destruction command header");
3345 if (!reply_context
->implicit_rotation_on_destroy
) {
3346 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3350 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3351 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3356 location
= session_get_trace_archive_location(session
);
3358 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3363 payload_size_before_location
= payload
.size
;
3364 comm_ret
= lttng_trace_archive_location_serialize(location
, &payload
);
3365 lttng_trace_archive_location_put(location
);
3367 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3371 /* Update the message to indicate the location's length. */
3372 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3373 payload
.size
- payload_size_before_location
;
3375 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
, payload
.data
, payload
.size
);
3376 if (comm_ret
!= (ssize_t
) payload
.size
) {
3377 ERR("Failed to send result of the destruction of session \"%s\" to client",
3381 ret
= close(reply_context
->reply_sock_fd
);
3383 PERROR("Failed to close client socket in deferred session destroy reply");
3385 lttng_dynamic_buffer_reset(&payload
);
3386 free(_reply_context
);
3390 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3392 * Called with session lock held.
3394 int cmd_destroy_session(struct ltt_session
*session
,
3395 struct notification_thread_handle
*notification_thread_handle
,
3399 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3400 struct cmd_destroy_session_reply_context
*reply_context
= nullptr;
3403 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3404 if (!reply_context
) {
3405 ret
= LTTNG_ERR_NOMEM
;
3409 reply_context
->reply_sock_fd
= *sock_fd
;
3413 LTTNG_ASSERT(session
);
3415 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3416 if (session
->active
) {
3417 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3419 ret
= cmd_stop_trace(session
);
3420 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3421 /* Carry on with the destruction of the session. */
3422 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3424 lttng_strerror(-ret
));
3425 destruction_last_error
= (lttng_error_code
) ret
;
3429 if (session
->rotation_schedule_timer_enabled
) {
3430 if (timer_session_rotation_schedule_timer_stop(session
)) {
3431 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3433 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3437 if (session
->rotate_size
) {
3438 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3439 session
->rotate_size
= 0;
3442 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3444 * Perform a last rotation on destruction if rotations have
3445 * occurred during the session's lifetime.
3447 ret
= cmd_rotate_session(
3448 session
, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3449 if (ret
!= LTTNG_OK
) {
3450 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3452 lttng_strerror(-ret
));
3453 destruction_last_error
= (lttng_error_code
) -ret
;
3455 if (reply_context
) {
3456 reply_context
->implicit_rotation_on_destroy
= true;
3458 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3460 * The user has not triggered a session rotation. However, to
3461 * ensure all data has been consumed, the session is rotated
3462 * to a 'null' trace chunk before it is destroyed.
3464 * This is a "quiet" rotation meaning that no notification is
3465 * emitted and no renaming of the current trace chunk takes
3468 ret
= cmd_rotate_session(
3469 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3471 * Rotation operations may not be supported by the kernel
3472 * tracer. Hence, do not consider this implicit rotation as
3473 * a session destruction error. The library has already stopped
3474 * the session and waited for pending data; there is nothing
3475 * left to do but complete the destruction of the session.
3477 if (ret
!= LTTNG_OK
&& ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3478 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3480 lttng_strerror(ret
));
3481 destruction_last_error
= (lttng_error_code
) -ret
;
3485 if (session
->shm_path
[0]) {
3487 * When a session is created with an explicit shm_path,
3488 * the consumer daemon will create its shared memory files
3489 * at that location and will *not* unlink them. This is normal
3490 * as the intention of that feature is to make it possible
3491 * to retrieve the content of those files should a crash occur.
3493 * To ensure the content of those files can be used, the
3494 * sessiond daemon will replicate the content of the metadata
3495 * cache in a metadata file.
3497 * On clean-up, it is expected that the consumer daemon will
3498 * unlink the shared memory files and that the session daemon
3499 * will unlink the metadata file. Then, the session's directory
3500 * in the shm path can be removed.
3502 * Unfortunately, a flaw in the design of the sessiond's and
3503 * consumerd's tear down of channels makes it impossible to
3504 * determine when the sessiond _and_ the consumerd have both
3505 * destroyed their representation of a channel. For one, the
3506 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3507 * callbacks in both daemons.
3509 * However, it is also impossible for the sessiond to know when
3510 * the consumer daemon is done destroying its channel(s) since
3511 * it occurs as a reaction to the closing of the channel's file
3512 * descriptor. There is no resulting communication initiated
3513 * from the consumerd to the sessiond to confirm that the
3514 * operation is completed (and was successful).
3516 * Until this is all fixed, the session daemon checks for the
3517 * removal of the session's shm path which makes it possible
3518 * to safely advertise a session as having been destroyed.
3520 * Prior to this fix, it was not possible to reliably save
3521 * a session making use of the --shm-path option, destroy it,
3522 * and load it again. This is because the creation of the
3523 * session would fail upon seeing the session's shm path
3524 * already in existence.
3526 * Note that none of the error paths in the check for the
3527 * directory's existence return an error. This is normal
3528 * as there isn't much that can be done. The session will
3529 * be destroyed properly, except that we can't offer the
3530 * guarantee that the same session can be re-created.
3532 current_completion_handler
= &destroy_completion_handler
.handler
;
3533 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3535 sizeof(destroy_completion_handler
.shm_path
));
3540 * The session is destroyed. However, note that the command context
3541 * still holds a reference to the session, thus delaying its destruction
3542 * _at least_ up to the point when that reference is released.
3544 session_destroy(session
);
3545 if (reply_context
) {
3546 reply_context
->destruction_status
= destruction_last_error
;
3547 ret
= session_add_destroy_notifier(
3548 session
, cmd_destroy_session_reply
, (void *) reply_context
);
3550 ret
= LTTNG_ERR_FATAL
;
3562 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3564 int cmd_register_consumer(struct ltt_session
*session
,
3565 enum lttng_domain_type domain
,
3566 const char *sock_path
,
3567 struct consumer_data
*cdata
)
3570 struct consumer_socket
*socket
= nullptr;
3572 LTTNG_ASSERT(session
);
3573 LTTNG_ASSERT(cdata
);
3574 LTTNG_ASSERT(sock_path
);
3577 case LTTNG_DOMAIN_KERNEL
:
3579 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3581 LTTNG_ASSERT(ksess
);
3583 /* Can't register a consumer if there is already one */
3584 if (ksess
->consumer_fds_sent
!= 0) {
3585 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3589 sock
= lttcomm_connect_unix_sock(sock_path
);
3591 ret
= LTTNG_ERR_CONNECT_FAIL
;
3594 cdata
->cmd_sock
= sock
;
3596 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3597 if (socket
== nullptr) {
3600 PERROR("close register consumer");
3602 cdata
->cmd_sock
= -1;
3603 ret
= LTTNG_ERR_FATAL
;
3607 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3608 if (socket
->lock
== nullptr) {
3609 PERROR("zmalloc pthread mutex");
3610 ret
= LTTNG_ERR_FATAL
;
3614 pthread_mutex_init(socket
->lock
, nullptr);
3615 socket
->registered
= 1;
3617 lttng::urcu::read_lock_guard read_lock
;
3618 consumer_add_socket(socket
, ksess
->consumer
);
3620 pthread_mutex_lock(&cdata
->pid_mutex
);
3622 pthread_mutex_unlock(&cdata
->pid_mutex
);
3627 /* TODO: Userspace tracing */
3628 ret
= LTTNG_ERR_UND
;
3636 consumer_destroy_socket(socket
);
3642 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3644 ssize_t
cmd_list_domains(struct ltt_session
*session
, struct lttng_domain
**domains
)
3649 struct lttng_ht_iter iter
;
3651 if (session
->kernel_session
!= nullptr) {
3652 DBG3("Listing domains found kernel domain");
3656 if (session
->ust_session
!= nullptr) {
3657 DBG3("Listing domains found UST global domain");
3660 lttng::urcu::read_lock_guard read_lock
;
3662 cds_lfht_for_each_entry (
3663 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3664 if (agt
->being_used
) {
3674 *domains
= calloc
<lttng_domain
>(nb_dom
);
3675 if (*domains
== nullptr) {
3676 ret
= LTTNG_ERR_FATAL
;
3680 if (session
->kernel_session
!= nullptr) {
3681 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3683 /* Kernel session buffer type is always GLOBAL */
3684 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3689 if (session
->ust_session
!= nullptr) {
3690 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3691 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3695 lttng::urcu::read_lock_guard read_lock
;
3697 cds_lfht_for_each_entry (
3698 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3699 if (agt
->being_used
) {
3700 (*domains
)[index
].type
= agt
->domain
;
3701 (*domains
)[index
].buf_type
=
3702 session
->ust_session
->buffer_type
;
3712 /* Return negative value to differentiate return code */
3717 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3719 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3720 struct ltt_session
*session
,
3721 struct lttng_payload
*payload
)
3725 struct lttcomm_list_command_header cmd_header
= {};
3726 size_t cmd_header_offset
;
3727 enum lttng_error_code ret_code
;
3732 DBG("Listing channels for session %s", session
->name
);
3734 cmd_header_offset
= payload
->buffer
.size
;
3736 /* Reserve space for command reply header. */
3737 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3738 cmd_header_offset
+ sizeof(cmd_header
));
3740 ret_code
= LTTNG_ERR_NOMEM
;
3745 case LTTNG_DOMAIN_KERNEL
:
3747 /* Kernel channels */
3748 struct ltt_kernel_channel
*kchan
;
3749 if (session
->kernel_session
!= nullptr) {
3750 cds_list_for_each_entry (
3751 kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
3752 uint64_t discarded_events
, lost_packets
;
3753 struct lttng_channel_extended
*extended
;
3755 extended
= (struct lttng_channel_extended
*)
3756 kchan
->channel
->attr
.extended
.ptr
;
3758 ret
= get_kernel_runtime_stats(
3759 session
, kchan
, &discarded_events
, &lost_packets
);
3761 ret_code
= LTTNG_ERR_UNK
;
3766 * Update the discarded_events and lost_packets
3767 * count for the channel
3769 extended
->discarded_events
= discarded_events
;
3770 extended
->lost_packets
= lost_packets
;
3772 ret
= lttng_channel_serialize(kchan
->channel
, &payload
->buffer
);
3774 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3775 kchan
->channel
->name
);
3776 ret_code
= LTTNG_ERR_UNK
;
3785 case LTTNG_DOMAIN_UST
:
3787 struct lttng_ht_iter iter
;
3788 struct ltt_ust_channel
*uchan
;
3791 lttng::urcu::read_lock_guard read_lock
;
3793 cds_lfht_for_each_entry (session
->ust_session
->domain_global
.channels
->ht
,
3797 uint64_t discarded_events
= 0, lost_packets
= 0;
3798 struct lttng_channel
*channel
= nullptr;
3799 struct lttng_channel_extended
*extended
;
3801 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3803 ret_code
= LTTNG_ERR_NOMEM
;
3807 extended
= (struct lttng_channel_extended
*)
3808 channel
->attr
.extended
.ptr
;
3810 ret
= get_ust_runtime_stats(
3811 session
, uchan
, &discarded_events
, &lost_packets
);
3813 lttng_channel_destroy(channel
);
3814 ret_code
= LTTNG_ERR_UNK
;
3818 extended
->discarded_events
= discarded_events
;
3819 extended
->lost_packets
= lost_packets
;
3821 ret
= lttng_channel_serialize(channel
, &payload
->buffer
);
3823 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3825 lttng_channel_destroy(channel
);
3826 ret_code
= LTTNG_ERR_UNK
;
3830 lttng_channel_destroy(channel
);
3841 if (i
> UINT32_MAX
) {
3842 ERR("Channel count would overflow the channel listing command's reply");
3843 ret_code
= LTTNG_ERR_OVERFLOW
;
3847 /* Update command reply header. */
3848 cmd_header
.count
= (uint32_t) i
;
3849 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
, sizeof(cmd_header
));
3850 ret_code
= LTTNG_OK
;
3857 * Command LTTNG_LIST_EVENTS processed by the client thread.
3859 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3860 struct ltt_session
*session
,
3862 struct lttng_payload
*reply_payload
)
3864 int buffer_resize_ret
;
3865 enum lttng_error_code ret_code
= LTTNG_OK
;
3866 struct lttcomm_list_command_header reply_command_header
= {};
3867 size_t reply_command_header_offset
;
3868 unsigned int nb_events
= 0;
3870 assert(reply_payload
);
3872 /* Reserve space for command reply header. */
3873 reply_command_header_offset
= reply_payload
->buffer
.size
;
3874 buffer_resize_ret
= lttng_dynamic_buffer_set_size(
3875 &reply_payload
->buffer
,
3876 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
3877 if (buffer_resize_ret
) {
3878 ret_code
= LTTNG_ERR_NOMEM
;
3883 case LTTNG_DOMAIN_KERNEL
:
3884 if (session
->kernel_session
!= nullptr) {
3885 ret_code
= list_lttng_kernel_events(
3886 channel_name
, session
->kernel_session
, reply_payload
, &nb_events
);
3890 case LTTNG_DOMAIN_UST
:
3892 if (session
->ust_session
!= nullptr) {
3894 list_lttng_ust_global_events(channel_name
,
3895 &session
->ust_session
->domain_global
,
3902 case LTTNG_DOMAIN_LOG4J
:
3903 case LTTNG_DOMAIN_JUL
:
3904 case LTTNG_DOMAIN_PYTHON
:
3905 if (session
->ust_session
) {
3906 struct lttng_ht_iter iter
;
3909 lttng::urcu::read_lock_guard read_lock
;
3911 cds_lfht_for_each_entry (
3912 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3913 if (agt
->domain
== domain
) {
3914 ret_code
= list_lttng_agent_events(
3915 agt
, reply_payload
, &nb_events
);
3922 ret_code
= LTTNG_ERR_UND
;
3926 if (nb_events
> UINT32_MAX
) {
3927 ret_code
= LTTNG_ERR_OVERFLOW
;
3931 /* Update command reply header. */
3932 reply_command_header
.count
= (uint32_t) nb_events
;
3933 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
3934 &reply_command_header
,
3935 sizeof(reply_command_header
));
3942 * Using the session list, filled a lttng_session array to send back to the
3943 * client for session listing.
3945 * The session list lock MUST be acquired before calling this function. Use
3946 * session_lock_list() and session_unlock_list().
3948 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3949 size_t session_count
,
3955 struct ltt_session
*session
;
3956 struct ltt_session_list
*list
= session_get_list();
3957 struct lttng_session_extended
*extended
= (typeof(extended
)) (&sessions
[session_count
]);
3959 DBG("Getting all available session for UID %d GID %d", uid
, gid
);
3961 * Iterate over session list and append data after the control struct in
3964 cds_list_for_each_entry (session
, &list
->head
, list
) {
3965 if (!session_get(session
)) {
3969 * Only list the sessions the user can control.
3971 if (!session_access_ok(session
, uid
) || session
->destroyed
) {
3972 session_put(session
);
3976 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3977 struct ltt_ust_session
*usess
= session
->ust_session
;
3979 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3980 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3981 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3982 ret
= build_network_session_path(
3983 sessions
[i
].path
, sizeof(sessions
[i
].path
), session
);
3985 ret
= snprintf(sessions
[i
].path
,
3986 sizeof(sessions
[i
].path
),
3988 session
->consumer
->dst
.session_root_path
);
3991 PERROR("snprintf session path");
3992 session_put(session
);
3996 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3997 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3998 sessions
[i
].enabled
= session
->active
;
3999 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
4000 sessions
[i
].live_timer_interval
= session
->live_timer
;
4001 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
4002 extended
[i
].creation_time
.is_set
= 1;
4004 session_put(session
);
4009 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4010 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4012 int cmd_data_pending(struct ltt_session
*session
)
4015 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4016 struct ltt_ust_session
*usess
= session
->ust_session
;
4018 LTTNG_ASSERT(session
);
4020 DBG("Data pending for session %s", session
->name
);
4022 /* Session MUST be stopped to ask for data availability. */
4023 if (session
->active
) {
4024 ret
= LTTNG_ERR_SESSION_STARTED
;
4028 * If stopped, just make sure we've started before else the above call
4029 * will always send that there is data pending.
4031 * The consumer assumes that when the data pending command is received,
4032 * the trace has been started before or else no output data is written
4033 * by the streams which is a condition for data pending. So, this is
4034 * *VERY* important that we don't ask the consumer before a start
4037 if (!session
->has_been_started
) {
4043 /* A rotation is still pending, we have to wait. */
4044 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4045 DBG("Rotate still pending for session %s", session
->name
);
4050 if (ksess
&& ksess
->consumer
) {
4051 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4053 /* Data is still being extracted for the kernel. */
4058 if (usess
&& usess
->consumer
) {
4059 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4061 /* Data is still being extracted for the kernel. */
4066 /* Data is ready to be read by a viewer */
4074 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4076 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4078 int cmd_snapshot_add_output(struct ltt_session
*session
,
4079 const struct lttng_snapshot_output
*output
,
4083 struct snapshot_output
*new_output
;
4085 LTTNG_ASSERT(session
);
4086 LTTNG_ASSERT(output
);
4088 DBG("Cmd snapshot add output for session %s", session
->name
);
4091 * Can't create an output if the session is not set in no-output mode.
4093 if (session
->output_traces
) {
4094 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4098 if (session
->has_non_mmap_channel
) {
4099 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4103 /* Only one output is allowed until we have the "tee" feature. */
4104 if (session
->snapshot
.nb_output
== 1) {
4105 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4109 new_output
= snapshot_output_alloc();
4111 ret
= LTTNG_ERR_NOMEM
;
4115 ret
= snapshot_output_init(session
,
4122 &session
->snapshot
);
4124 if (ret
== -ENOMEM
) {
4125 ret
= LTTNG_ERR_NOMEM
;
4127 ret
= LTTNG_ERR_INVALID
;
4132 snapshot_add_output(&session
->snapshot
, new_output
);
4134 *id
= new_output
->id
;
4140 snapshot_output_destroy(new_output
);
4146 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4148 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4150 int cmd_snapshot_del_output(struct ltt_session
*session
, const struct lttng_snapshot_output
*output
)
4153 struct snapshot_output
*sout
= nullptr;
4155 LTTNG_ASSERT(session
);
4156 LTTNG_ASSERT(output
);
4158 lttng::urcu::read_lock_guard read_lock
;
4161 * Permission denied to create an output if the session is not
4162 * set in no output mode.
4164 if (session
->output_traces
) {
4165 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4170 DBG("Cmd snapshot del output id %" PRIu32
" for session %s",
4173 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4174 } else if (*output
->name
!= '\0') {
4175 DBG("Cmd snapshot del output name %s for session %s", output
->name
, session
->name
);
4176 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4179 ret
= LTTNG_ERR_INVALID
;
4183 snapshot_delete_output(&session
->snapshot
, sout
);
4184 snapshot_output_destroy(sout
);
4192 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4194 * If no output is available, outputs is untouched and 0 is returned.
4196 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4198 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4199 struct lttng_snapshot_output
**outputs
)
4202 struct lttng_snapshot_output
*list
= nullptr;
4203 struct lttng_ht_iter iter
;
4204 struct snapshot_output
*output
;
4206 LTTNG_ASSERT(session
);
4207 LTTNG_ASSERT(outputs
);
4209 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4212 * Permission denied to create an output if the session is not
4213 * set in no output mode.
4215 if (session
->output_traces
) {
4216 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4220 if (session
->snapshot
.nb_output
== 0) {
4225 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4227 ret
= -LTTNG_ERR_NOMEM
;
4231 /* Copy list from session to the new list object. */
4233 lttng::urcu::read_lock_guard read_lock
;
4235 cds_lfht_for_each_entry (
4236 session
->snapshot
.output_ht
->ht
, &iter
.iter
, output
, node
.node
) {
4237 LTTNG_ASSERT(output
->consumer
);
4238 list
[idx
].id
= output
->id
;
4239 list
[idx
].max_size
= output
->max_size
;
4240 if (lttng_strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
))) {
4241 ret
= -LTTNG_ERR_INVALID
;
4245 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4246 if (lttng_strncpy(list
[idx
].ctrl_url
,
4247 output
->consumer
->dst
.session_root_path
,
4248 sizeof(list
[idx
].ctrl_url
))) {
4249 ret
= -LTTNG_ERR_INVALID
;
4254 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4256 sizeof(list
[idx
].ctrl_url
));
4258 ret
= -LTTNG_ERR_NOMEM
;
4263 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4265 sizeof(list
[idx
].data_url
));
4267 ret
= -LTTNG_ERR_NOMEM
;
4278 ret
= session
->snapshot
.nb_output
;
4286 * Check if we can regenerate the metadata for this session.
4287 * Only kernel, UST per-uid and non-live sessions are supported.
4289 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4291 static int check_regenerate_metadata_support(struct ltt_session
*session
)
4295 LTTNG_ASSERT(session
);
4297 if (session
->live_timer
!= 0) {
4298 ret
= LTTNG_ERR_LIVE_SESSION
;
4301 if (!session
->active
) {
4302 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4305 if (session
->ust_session
) {
4306 switch (session
->ust_session
->buffer_type
) {
4307 case LTTNG_BUFFER_PER_UID
:
4309 case LTTNG_BUFFER_PER_PID
:
4310 ret
= LTTNG_ERR_PER_PID_SESSION
;
4314 ret
= LTTNG_ERR_UNK
;
4318 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4319 session
->consumer
->relay_minor_version
< 8) {
4320 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4330 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4332 * Ask the consumer to truncate the existing metadata file(s) and
4333 * then regenerate the metadata. Live and per-pid sessions are not
4334 * supported and return an error.
4336 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4338 int cmd_regenerate_metadata(struct ltt_session
*session
)
4342 LTTNG_ASSERT(session
);
4344 ret
= check_regenerate_metadata_support(session
);
4349 if (session
->kernel_session
) {
4350 ret
= kernctl_session_regenerate_metadata(session
->kernel_session
->fd
);
4352 ERR("Failed to regenerate the kernel metadata");
4357 if (session
->ust_session
) {
4358 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4360 ERR("Failed to regenerate the UST metadata");
4364 DBG("Cmd metadata regenerate for session %s", session
->name
);
4372 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4374 * Ask the tracer to regenerate a new statedump.
4376 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4378 int cmd_regenerate_statedump(struct ltt_session
*session
)
4382 LTTNG_ASSERT(session
);
4384 if (!session
->active
) {
4385 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4389 if (session
->kernel_session
) {
4390 ret
= kernctl_session_regenerate_statedump(session
->kernel_session
->fd
);
4392 * Currently, the statedump in kernel can only fail if out
4396 if (ret
== -ENOMEM
) {
4397 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4399 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4401 ERR("Failed to regenerate the kernel statedump");
4406 if (session
->ust_session
) {
4407 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4409 * Currently, the statedump in UST always returns 0.
4412 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4413 ERR("Failed to regenerate the UST statedump");
4417 DBG("Cmd regenerate statedump for session %s", session
->name
);
4424 static enum lttng_error_code
4425 synchronize_tracer_notifier_register(struct notification_thread_handle
*notification_thread
,
4426 struct lttng_trigger
*trigger
,
4427 const struct lttng_credentials
*cmd_creds
)
4429 enum lttng_error_code ret_code
;
4430 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4431 const char *trigger_name
;
4432 uid_t trigger_owner
;
4433 enum lttng_trigger_status trigger_status
;
4434 const enum lttng_domain_type trigger_domain
=
4435 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4437 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4438 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4440 LTTNG_ASSERT(condition
);
4441 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4442 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4444 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4445 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4447 session_lock_list();
4448 switch (trigger_domain
) {
4449 case LTTNG_DOMAIN_KERNEL
:
4451 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4452 if (ret_code
!= LTTNG_OK
) {
4453 enum lttng_error_code notif_thread_unregister_ret
;
4455 notif_thread_unregister_ret
=
4456 notification_thread_command_unregister_trigger(notification_thread
,
4459 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4460 /* Return the original error code. */
4461 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4463 (int) trigger_owner
,
4469 case LTTNG_DOMAIN_UST
:
4470 ust_app_global_update_all_event_notifier_rules();
4472 case LTTNG_DOMAIN_JUL
:
4473 case LTTNG_DOMAIN_LOG4J
:
4474 case LTTNG_DOMAIN_PYTHON
:
4476 /* Agent domains. */
4477 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4480 agt
= agent_create(trigger_domain
);
4482 ret_code
= LTTNG_ERR_NOMEM
;
4483 goto end_unlock_session_list
;
4486 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4489 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4490 if (ret_code
!= LTTNG_OK
) {
4491 goto end_unlock_session_list
;
4496 case LTTNG_DOMAIN_NONE
:
4501 ret_code
= LTTNG_OK
;
4502 end_unlock_session_list
:
4503 session_unlock_list();
4507 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4508 struct lttng_trigger
*trigger
,
4509 bool is_trigger_anonymous
,
4510 struct notification_thread_handle
*notification_thread
,
4511 struct lttng_trigger
**return_trigger
)
4513 enum lttng_error_code ret_code
;
4514 const char *trigger_name
;
4515 uid_t trigger_owner
;
4516 enum lttng_trigger_status trigger_status
;
4518 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4519 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4521 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4522 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4524 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4526 (int) trigger_owner
,
4527 (int) lttng_credentials_get_uid(cmd_creds
));
4530 * Validate the trigger credentials against the command credentials.
4531 * Only the root user can register a trigger with non-matching
4534 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4535 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4536 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4538 (int) trigger_owner
,
4539 (int) lttng_credentials_get_uid(cmd_creds
));
4540 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4546 * The bytecode generation also serves as a validation step for the
4547 * bytecode expressions.
4549 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4550 if (ret_code
!= LTTNG_OK
) {
4551 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4553 (int) trigger_owner
,
4559 * A reference to the trigger is acquired by the notification thread.
4560 * It is safe to return the same trigger to the caller since it the
4561 * other user holds a reference.
4563 * The trigger is modified during the execution of the
4564 * "register trigger" command. However, by the time the command returns,
4565 * it is safe to use without any locking as its properties are
4568 ret_code
= notification_thread_command_register_trigger(
4569 notification_thread
, trigger
, is_trigger_anonymous
);
4570 if (ret_code
!= LTTNG_OK
) {
4571 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4573 (int) trigger_owner
,
4578 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4579 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4582 * Synchronize tracers if the trigger adds an event notifier.
4584 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4585 ret_code
= synchronize_tracer_notifier_register(
4586 notification_thread
, trigger
, cmd_creds
);
4587 if (ret_code
!= LTTNG_OK
) {
4588 ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code
));
4594 * Return an updated trigger to the client.
4596 * Since a modified version of the same trigger is returned, acquire a
4597 * reference to the trigger so the caller doesn't have to care if those
4598 * are distinct instances or not.
4600 if (ret_code
== LTTNG_OK
) {
4601 lttng_trigger_get(trigger
);
4602 *return_trigger
= trigger
;
4603 /* Ownership of trigger was transferred to caller. */
4610 static enum lttng_error_code
4611 synchronize_tracer_notifier_unregister(const struct lttng_trigger
*trigger
)
4613 enum lttng_error_code ret_code
;
4614 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4615 const enum lttng_domain_type trigger_domain
=
4616 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4618 LTTNG_ASSERT(condition
);
4619 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4620 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4622 session_lock_list();
4623 switch (trigger_domain
) {
4624 case LTTNG_DOMAIN_KERNEL
:
4625 ret_code
= kernel_unregister_event_notifier(trigger
);
4626 if (ret_code
!= LTTNG_OK
) {
4627 goto end_unlock_session_list
;
4631 case LTTNG_DOMAIN_UST
:
4632 ust_app_global_update_all_event_notifier_rules();
4634 case LTTNG_DOMAIN_JUL
:
4635 case LTTNG_DOMAIN_LOG4J
:
4636 case LTTNG_DOMAIN_PYTHON
:
4638 /* Agent domains. */
4639 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4642 * This trigger was never registered in the first place. Calling
4643 * this function under those circumstances is an internal error.
4646 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4647 if (ret_code
!= LTTNG_OK
) {
4648 goto end_unlock_session_list
;
4653 case LTTNG_DOMAIN_NONE
:
4658 ret_code
= LTTNG_OK
;
4660 end_unlock_session_list
:
4661 session_unlock_list();
4665 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4666 const struct lttng_trigger
*trigger
,
4667 struct notification_thread_handle
*notification_thread
)
4669 enum lttng_error_code ret_code
;
4670 const char *trigger_name
;
4671 uid_t trigger_owner
;
4672 enum lttng_trigger_status trigger_status
;
4673 struct lttng_trigger
*sessiond_trigger
= nullptr;
4675 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4676 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4677 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4678 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4680 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4682 (int) trigger_owner
,
4683 (int) lttng_credentials_get_uid(cmd_creds
));
4686 * Validate the trigger credentials against the command credentials.
4687 * Only the root user can unregister a trigger with non-matching
4690 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4691 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4692 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4694 (int) trigger_owner
,
4695 (int) lttng_credentials_get_uid(cmd_creds
));
4696 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4701 /* Fetch the sessiond side trigger object. */
4702 ret_code
= notification_thread_command_get_trigger(
4703 notification_thread
, trigger
, &sessiond_trigger
);
4704 if (ret_code
!= LTTNG_OK
) {
4705 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4707 (int) trigger_owner
,
4712 LTTNG_ASSERT(sessiond_trigger
);
4715 * From this point on, no matter what, consider the trigger
4718 * We set the unregistered state of the sessiond side trigger object in
4719 * the client thread since we want to minimize the possibility of the
4720 * notification thread being stalled due to a long execution of an
4721 * action that required the trigger lock.
4723 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4725 ret_code
= notification_thread_command_unregister_trigger(notification_thread
, trigger
);
4726 if (ret_code
!= LTTNG_OK
) {
4727 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4729 (int) trigger_owner
,
4735 * Synchronize tracers if the trigger removes an event notifier.
4736 * Do this even if the trigger unregistration failed to at least stop
4737 * the tracers from producing notifications associated with this
4740 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4741 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4742 if (ret_code
!= LTTNG_OK
) {
4743 ERR("Error unregistering trigger to tracer.");
4749 lttng_trigger_put(sessiond_trigger
);
4753 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4754 struct notification_thread_handle
*notification_thread
,
4755 struct lttng_triggers
**return_triggers
)
4758 enum lttng_error_code ret_code
;
4759 struct lttng_triggers
*triggers
= nullptr;
4761 /* Get the set of triggers from the notification thread. */
4762 ret_code
= notification_thread_command_list_triggers(
4763 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4764 if (ret_code
!= LTTNG_OK
) {
4768 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4770 ret_code
= LTTNG_ERR_UNK
;
4774 *return_triggers
= triggers
;
4776 ret_code
= LTTNG_OK
;
4778 lttng_triggers_destroy(triggers
);
4782 enum lttng_error_code
4783 cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4784 const struct lttng_error_query
*query
,
4785 struct lttng_error_query_results
**_results
,
4786 struct notification_thread_handle
*notification_thread
)
4788 enum lttng_error_code ret_code
;
4789 const struct lttng_trigger
*query_target_trigger
;
4790 const struct lttng_action
*query_target_action
= nullptr;
4791 struct lttng_trigger
*matching_trigger
= nullptr;
4792 const char *trigger_name
;
4793 uid_t trigger_owner
;
4794 enum lttng_trigger_status trigger_status
;
4795 struct lttng_error_query_results
*results
= nullptr;
4797 switch (lttng_error_query_get_target_type(query
)) {
4798 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4799 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4801 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4802 query_target_trigger
= lttng_error_query_condition_borrow_target(query
);
4804 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4805 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(query
);
4811 LTTNG_ASSERT(query_target_trigger
);
4813 ret_code
= notification_thread_command_get_trigger(
4814 notification_thread
, query_target_trigger
, &matching_trigger
);
4815 if (ret_code
!= LTTNG_OK
) {
4819 /* No longer needed. */
4820 query_target_trigger
= nullptr;
4822 if (lttng_error_query_get_target_type(query
) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4823 /* Get the sessiond-side version of the target action. */
4824 query_target_action
=
4825 lttng_error_query_action_borrow_action_target(query
, matching_trigger
);
4828 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4829 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4830 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
, &trigger_owner
);
4831 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4833 results
= lttng_error_query_results_create();
4835 ret_code
= LTTNG_ERR_NOMEM
;
4839 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4841 (int) trigger_owner
,
4842 (int) lttng_credentials_get_uid(cmd_creds
));
4845 * Validate the trigger credentials against the command credentials.
4846 * Only the root user can target a trigger with non-matching
4849 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger
),
4851 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4852 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4854 (int) trigger_owner
,
4855 (int) lttng_credentials_get_uid(cmd_creds
));
4856 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4861 switch (lttng_error_query_get_target_type(query
)) {
4862 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4863 trigger_status
= lttng_trigger_add_error_results(matching_trigger
, results
);
4865 switch (trigger_status
) {
4866 case LTTNG_TRIGGER_STATUS_OK
:
4869 ret_code
= LTTNG_ERR_UNK
;
4874 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4877 lttng_trigger_condition_add_error_results(matching_trigger
, results
);
4879 switch (trigger_status
) {
4880 case LTTNG_TRIGGER_STATUS_OK
:
4883 ret_code
= LTTNG_ERR_UNK
;
4889 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4891 const enum lttng_action_status action_status
=
4892 lttng_action_add_error_query_results(query_target_action
, results
);
4894 switch (action_status
) {
4895 case LTTNG_ACTION_STATUS_OK
:
4898 ret_code
= LTTNG_ERR_UNK
;
4909 *_results
= results
;
4911 ret_code
= LTTNG_OK
;
4913 lttng_trigger_put(matching_trigger
);
4914 lttng_error_query_results_destroy(results
);
4919 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4920 * snapshot output is *not* set with a remote destination.
4922 * Return LTTNG_OK on success or a LTTNG_ERR code.
4924 static enum lttng_error_code
set_relayd_for_snapshot(struct consumer_output
*output
,
4925 const struct ltt_session
*session
)
4927 enum lttng_error_code status
= LTTNG_OK
;
4928 struct lttng_ht_iter iter
;
4929 struct consumer_socket
*socket
;
4930 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4931 const char *base_path
;
4933 LTTNG_ASSERT(output
);
4934 LTTNG_ASSERT(session
);
4936 DBG2("Set relayd object from snapshot output");
4938 if (session
->current_trace_chunk
) {
4939 enum lttng_trace_chunk_status chunk_status
= lttng_trace_chunk_get_id(
4940 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
4942 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4943 current_chunk_id
.is_set
= true;
4945 ERR("Failed to get current trace chunk id");
4946 status
= LTTNG_ERR_UNK
;
4951 /* Ignore if snapshot consumer output is not network. */
4952 if (output
->type
!= CONSUMER_DST_NET
) {
4957 * The snapshot record URI base path overrides the session
4960 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4961 base_path
= output
->dst
.net
.control
.subdir
;
4963 base_path
= session
->base_path
;
4967 * For each consumer socket, create and send the relayd object of the
4971 lttng::urcu::read_lock_guard read_lock
;
4973 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
4974 pthread_mutex_lock(socket
->lock
);
4975 status
= send_consumer_relayd_sockets(
4982 session
->live_timer
,
4983 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
4984 session
->creation_time
,
4985 session
->name_contains_creation_time
);
4986 pthread_mutex_unlock(socket
->lock
);
4987 if (status
!= LTTNG_OK
) {
4998 * Record a kernel snapshot.
5000 * Return LTTNG_OK on success or a LTTNG_ERR code.
5002 static enum lttng_error_code
record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
5003 const struct consumer_output
*output
,
5004 const struct ltt_session
*session
,
5005 uint64_t nb_packets_per_stream
)
5007 enum lttng_error_code status
;
5009 LTTNG_ASSERT(ksess
);
5010 LTTNG_ASSERT(output
);
5011 LTTNG_ASSERT(session
);
5013 status
= kernel_snapshot_record(ksess
, output
, nb_packets_per_stream
);
5018 * Record a UST snapshot.
5020 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5022 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5023 const struct consumer_output
*output
,
5024 const struct ltt_session
*session
,
5025 uint64_t nb_packets_per_stream
)
5027 enum lttng_error_code status
;
5029 LTTNG_ASSERT(usess
);
5030 LTTNG_ASSERT(output
);
5031 LTTNG_ASSERT(session
);
5033 status
= ust_app_snapshot_record(usess
, output
, nb_packets_per_stream
);
5037 static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session
*session
,
5038 uint64_t cur_nr_packets
)
5040 uint64_t tot_size
= 0;
5042 if (session
->kernel_session
) {
5043 struct ltt_kernel_channel
*chan
;
5044 const struct ltt_kernel_session
*ksess
= session
->kernel_session
;
5046 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
5047 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5049 * Don't take channel into account if we
5050 * already grab all its packets.
5054 tot_size
+= chan
->channel
->attr
.subbuf_size
* chan
->stream_count
;
5058 if (session
->ust_session
) {
5059 const struct ltt_ust_session
*usess
= session
->ust_session
;
5061 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
, cur_nr_packets
);
5068 * Calculate the number of packets we can grab from each stream that
5069 * fits within the overall snapshot max size.
5071 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5072 * the number of packets per stream.
5074 * TODO: this approach is not perfect: we consider the worse case
5075 * (packet filling the sub-buffers) as an upper bound, but we could do
5076 * better if we do this calculation while we actually grab the packet
5077 * content: we would know how much padding we don't actually store into
5080 * This algorithm is currently bounded by the number of packets per
5083 * Since we call this algorithm before actually grabbing the data, it's
5084 * an approximation: for instance, applications could appear/disappear
5085 * in between this call and actually grabbing data.
5087 static int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5091 uint64_t cur_nb_packets
= 0;
5094 return 0; /* Infinite */
5097 size_left
= max_size
;
5099 uint64_t one_more_packet_tot_size
;
5101 one_more_packet_tot_size
=
5102 get_session_size_one_more_packet_per_stream(session
, cur_nb_packets
);
5103 if (!one_more_packet_tot_size
) {
5104 /* We are already grabbing all packets. */
5107 size_left
-= one_more_packet_tot_size
;
5108 if (size_left
< 0) {
5113 if (!cur_nb_packets
&& size_left
!= max_size
) {
5114 /* Not enough room to grab one packet of each stream, error. */
5117 return cur_nb_packets
;
5120 static enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5121 const struct snapshot_output
*snapshot_output
)
5123 int64_t nb_packets_per_stream
;
5124 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5126 enum lttng_error_code ret_code
= LTTNG_OK
;
5127 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5128 struct consumer_output
*original_ust_consumer_output
= nullptr;
5129 struct consumer_output
*original_kernel_consumer_output
= nullptr;
5130 struct consumer_output
*snapshot_ust_consumer_output
= nullptr;
5131 struct consumer_output
*snapshot_kernel_consumer_output
= nullptr;
5133 ret
= snprintf(snapshot_chunk_name
,
5134 sizeof(snapshot_chunk_name
),
5136 snapshot_output
->name
,
5137 snapshot_output
->datetime
,
5138 snapshot_output
->nb_snapshot
);
5139 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5140 ERR("Failed to format snapshot name");
5141 ret_code
= LTTNG_ERR_INVALID
;
5144 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5145 snapshot_output
->name
,
5147 snapshot_chunk_name
);
5148 if (!session
->kernel_session
&& !session
->ust_session
) {
5149 ERR("Failed to record snapshot as no channels exist");
5150 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5154 if (session
->kernel_session
) {
5155 original_kernel_consumer_output
= session
->kernel_session
->consumer
;
5156 snapshot_kernel_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5157 strcpy(snapshot_kernel_consumer_output
->chunk_path
, snapshot_chunk_name
);
5159 /* Copy the original domain subdir. */
5160 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5161 original_kernel_consumer_output
->domain_subdir
);
5163 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5164 original_kernel_consumer_output
);
5166 ERR("Failed to copy consumer sockets from snapshot output configuration");
5167 ret_code
= LTTNG_ERR_NOMEM
;
5170 ret_code
= set_relayd_for_snapshot(snapshot_kernel_consumer_output
, session
);
5171 if (ret_code
!= LTTNG_OK
) {
5172 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5175 session
->kernel_session
->consumer
= snapshot_kernel_consumer_output
;
5177 if (session
->ust_session
) {
5178 original_ust_consumer_output
= session
->ust_session
->consumer
;
5179 snapshot_ust_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5180 strcpy(snapshot_ust_consumer_output
->chunk_path
, snapshot_chunk_name
);
5182 /* Copy the original domain subdir. */
5183 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5184 original_ust_consumer_output
->domain_subdir
);
5186 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5187 original_ust_consumer_output
);
5189 ERR("Failed to copy consumer sockets from snapshot output configuration");
5190 ret_code
= LTTNG_ERR_NOMEM
;
5193 ret_code
= set_relayd_for_snapshot(snapshot_ust_consumer_output
, session
);
5194 if (ret_code
!= LTTNG_OK
) {
5195 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5198 session
->ust_session
->consumer
= snapshot_ust_consumer_output
;
5201 snapshot_trace_chunk
= session_create_new_trace_chunk(
5203 snapshot_kernel_consumer_output
?: snapshot_ust_consumer_output
,
5204 consumer_output_get_base_path(snapshot_output
->consumer
),
5205 snapshot_chunk_name
);
5206 if (!snapshot_trace_chunk
) {
5207 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5209 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5212 LTTNG_ASSERT(!session
->current_trace_chunk
);
5213 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, nullptr);
5214 lttng_trace_chunk_put(snapshot_trace_chunk
);
5215 snapshot_trace_chunk
= nullptr;
5217 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5219 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5223 nb_packets_per_stream
=
5224 get_session_nb_packets_per_stream(session
, snapshot_output
->max_size
);
5225 if (nb_packets_per_stream
< 0) {
5226 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5227 goto error_close_trace_chunk
;
5230 if (session
->kernel_session
) {
5231 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5232 snapshot_kernel_consumer_output
,
5234 nb_packets_per_stream
);
5235 if (ret_code
!= LTTNG_OK
) {
5236 goto error_close_trace_chunk
;
5240 if (session
->ust_session
) {
5241 ret_code
= record_ust_snapshot(session
->ust_session
,
5242 snapshot_ust_consumer_output
,
5244 nb_packets_per_stream
);
5245 if (ret_code
!= LTTNG_OK
) {
5246 goto error_close_trace_chunk
;
5250 error_close_trace_chunk
:
5251 if (session_set_trace_chunk(session
, nullptr, &snapshot_trace_chunk
)) {
5252 ERR("Failed to release the current trace chunk of session \"%s\"", session
->name
);
5253 ret_code
= LTTNG_ERR_UNK
;
5256 if (session_close_trace_chunk(session
,
5257 snapshot_trace_chunk
,
5258 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
,
5261 * Don't goto end; make sure the chunk is closed for the session
5262 * to allow future snapshots.
5264 ERR("Failed to close snapshot trace chunk of session \"%s\"", session
->name
);
5265 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5268 lttng_trace_chunk_put(snapshot_trace_chunk
);
5269 snapshot_trace_chunk
= nullptr;
5271 if (original_ust_consumer_output
) {
5272 session
->ust_session
->consumer
= original_ust_consumer_output
;
5274 if (original_kernel_consumer_output
) {
5275 session
->kernel_session
->consumer
= original_kernel_consumer_output
;
5277 consumer_output_put(snapshot_ust_consumer_output
);
5278 consumer_output_put(snapshot_kernel_consumer_output
);
5283 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5285 * The wait parameter is ignored so this call always wait for the snapshot to
5286 * complete before returning.
5288 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5290 int cmd_snapshot_record(struct ltt_session
*session
,
5291 const struct lttng_snapshot_output
*output
,
5292 int wait
__attribute__((unused
)))
5294 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5296 unsigned int snapshot_success
= 0;
5298 struct snapshot_output
*tmp_output
= nullptr;
5300 LTTNG_ASSERT(session
);
5301 LTTNG_ASSERT(output
);
5303 DBG("Cmd snapshot record for session %s", session
->name
);
5305 /* Get the datetime for the snapshot output directory. */
5306 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
, sizeof(datetime
));
5308 cmd_ret
= LTTNG_ERR_INVALID
;
5313 * Permission denied to create an output if the session is not
5314 * set in no output mode.
5316 if (session
->output_traces
) {
5317 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5321 /* The session needs to be started at least once. */
5322 if (!session
->has_been_started
) {
5323 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5327 /* Use temporary output for the session. */
5328 if (*output
->ctrl_url
!= '\0') {
5329 tmp_output
= snapshot_output_alloc();
5331 cmd_ret
= LTTNG_ERR_NOMEM
;
5335 ret
= snapshot_output_init(session
,
5344 if (ret
== -ENOMEM
) {
5345 cmd_ret
= LTTNG_ERR_NOMEM
;
5347 cmd_ret
= LTTNG_ERR_INVALID
;
5351 /* Use the global session count for the temporary snapshot. */
5352 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5354 /* Use the global datetime */
5355 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5356 cmd_ret
= snapshot_record(session
, tmp_output
);
5357 if (cmd_ret
!= LTTNG_OK
) {
5360 snapshot_success
= 1;
5362 struct snapshot_output
*sout
;
5363 struct lttng_ht_iter iter
;
5365 lttng::urcu::read_lock_guard read_lock
;
5367 cds_lfht_for_each_entry (
5368 session
->snapshot
.output_ht
->ht
, &iter
.iter
, sout
, node
.node
) {
5369 struct snapshot_output output_copy
;
5372 * Make a local copy of the output and override output
5373 * parameters with those provided as part of the
5376 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5378 if (output
->max_size
!= (uint64_t) -1ULL) {
5379 output_copy
.max_size
= output
->max_size
;
5382 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5383 memcpy(output_copy
.datetime
, datetime
, sizeof(datetime
));
5385 /* Use temporary name. */
5386 if (*output
->name
!= '\0') {
5387 if (lttng_strncpy(output_copy
.name
,
5389 sizeof(output_copy
.name
))) {
5390 cmd_ret
= LTTNG_ERR_INVALID
;
5395 cmd_ret
= snapshot_record(session
, &output_copy
);
5396 if (cmd_ret
!= LTTNG_OK
) {
5400 snapshot_success
= 1;
5404 if (snapshot_success
) {
5405 session
->snapshot
.nb_snapshot
++;
5407 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5412 snapshot_output_destroy(tmp_output
);
5419 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5421 int cmd_set_session_shm_path(struct ltt_session
*session
, const char *shm_path
)
5424 LTTNG_ASSERT(session
);
5427 * Can only set shm path before session is started.
5429 if (session
->has_been_started
) {
5430 return LTTNG_ERR_SESSION_STARTED
;
5433 strncpy(session
->shm_path
, shm_path
, sizeof(session
->shm_path
));
5434 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5440 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5442 * Ask the consumer to rotate the session output directory.
5443 * The session lock must be held.
5445 * Returns LTTNG_OK on success or else a negative LTTng error code.
5447 int cmd_rotate_session(struct ltt_session
*session
,
5448 struct lttng_rotate_session_return
*rotate_return
,
5449 bool quiet_rotation
,
5450 enum lttng_trace_chunk_command_type command
)
5453 uint64_t ongoing_rotation_chunk_id
;
5454 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5455 struct lttng_trace_chunk
*chunk_being_archived
= nullptr;
5456 struct lttng_trace_chunk
*new_trace_chunk
= nullptr;
5457 enum lttng_trace_chunk_status chunk_status
;
5458 bool failed_to_rotate
= false;
5459 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5461 LTTNG_ASSERT(session
);
5463 if (!session
->has_been_started
) {
5464 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5469 * Explicit rotation is not supported for live sessions.
5470 * However, live sessions can perform a quiet rotation on
5472 * Rotation is not supported for snapshot traces (no output).
5474 if ((!quiet_rotation
&& session
->live_timer
) || !session
->output_traces
) {
5475 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5479 /* Unsupported feature in lttng-relayd before 2.11. */
5480 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5481 (session
->consumer
->relay_major_version
== 2 &&
5482 session
->consumer
->relay_minor_version
< 11)) {
5483 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5487 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5488 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5489 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5493 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5494 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5496 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5501 * After a stop, we only allow one rotation to occur, the other ones are
5502 * useless until a new start.
5504 if (session
->rotated_after_last_stop
) {
5505 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5507 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5512 * After a stop followed by a clear, disallow following rotations a they would
5513 * generate empty chunks.
5515 if (session
->cleared_after_last_stop
) {
5516 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5518 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5522 if (session
->active
) {
5524 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
5525 if (!new_trace_chunk
) {
5526 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5532 * The current trace chunk becomes the chunk being archived.
5534 * After this point, "chunk_being_archived" must absolutely
5535 * be closed on the consumer(s), otherwise it will never be
5536 * cleaned-up, which will result in a leak.
5538 ret
= session_set_trace_chunk(session
, new_trace_chunk
, &chunk_being_archived
);
5540 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5544 if (session
->kernel_session
) {
5545 cmd_ret
= kernel_rotate_session(session
);
5546 if (cmd_ret
!= LTTNG_OK
) {
5547 failed_to_rotate
= true;
5548 rotation_fail_code
= cmd_ret
;
5551 if (session
->ust_session
) {
5552 cmd_ret
= ust_app_rotate_session(session
);
5553 if (cmd_ret
!= LTTNG_OK
) {
5554 failed_to_rotate
= true;
5555 rotation_fail_code
= cmd_ret
;
5559 if (!session
->active
) {
5560 session
->rotated_after_last_stop
= true;
5563 if (!chunk_being_archived
) {
5564 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5566 if (failed_to_rotate
) {
5567 cmd_ret
= rotation_fail_code
;
5574 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5575 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
, &ongoing_rotation_chunk_id
);
5576 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5578 ret
= session_close_trace_chunk(
5579 session
, chunk_being_archived
, command
, session
->last_chunk_path
);
5581 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5585 if (failed_to_rotate
) {
5586 cmd_ret
= rotation_fail_code
;
5590 session
->quiet_rotation
= quiet_rotation
;
5591 ret
= timer_session_rotation_pending_check_start(session
, DEFAULT_ROTATE_PENDING_TIMER
);
5593 cmd_ret
= LTTNG_ERR_UNK
;
5597 if (rotate_return
) {
5598 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5601 session
->chunk_being_archived
= chunk_being_archived
;
5602 chunk_being_archived
= nullptr;
5603 if (!quiet_rotation
) {
5604 ret
= notification_thread_command_session_rotation_ongoing(
5605 the_notification_thread_handle
, session
->id
, ongoing_rotation_chunk_id
);
5606 if (ret
!= LTTNG_OK
) {
5607 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5609 cmd_ret
= (lttng_error_code
) ret
;
5613 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5615 ongoing_rotation_chunk_id
);
5617 lttng_trace_chunk_put(new_trace_chunk
);
5618 lttng_trace_chunk_put(chunk_being_archived
);
5619 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5622 if (session_reset_rotation_state(session
, LTTNG_ROTATION_STATE_ERROR
)) {
5623 ERR("Failed to reset rotation state of session \"%s\"", session
->name
);
5629 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5631 * Check if the session has finished its rotation.
5633 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5635 int cmd_rotate_get_info(struct ltt_session
*session
,
5636 struct lttng_rotation_get_info_return
*info_return
,
5637 uint64_t rotation_id
)
5639 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5640 enum lttng_rotation_state rotation_state
;
5642 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
,
5644 session
->most_recent_chunk_id
.value
);
5646 if (session
->chunk_being_archived
) {
5647 enum lttng_trace_chunk_status chunk_status
;
5650 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
, &chunk_id
);
5651 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5653 rotation_state
= rotation_id
== chunk_id
? LTTNG_ROTATION_STATE_ONGOING
:
5654 LTTNG_ROTATION_STATE_EXPIRED
;
5656 if (session
->last_archived_chunk_id
.is_set
&&
5657 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5658 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5660 rotation_state
= session
->rotation_state
;
5664 switch (rotation_state
) {
5665 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5666 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5669 case LTTNG_ROTATION_STATE_EXPIRED
:
5670 DBG("Reporting that the rotation state of rotation id %" PRIu64
5671 " of session \"%s\" has expired",
5675 case LTTNG_ROTATION_STATE_ONGOING
:
5676 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5680 case LTTNG_ROTATION_STATE_COMPLETED
:
5684 char *current_tracing_path_reply
;
5685 size_t current_tracing_path_reply_len
;
5687 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5691 switch (session_get_consumer_destination_type(session
)) {
5692 case CONSUMER_DST_LOCAL
:
5693 current_tracing_path_reply
= info_return
->location
.local
.absolute_path
;
5694 current_tracing_path_reply_len
=
5695 sizeof(info_return
->location
.local
.absolute_path
);
5696 info_return
->location_type
=
5697 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5698 fmt_ret
= asprintf(&chunk_path
,
5699 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5700 session_get_base_path(session
),
5701 session
->last_archived_chunk_name
);
5702 if (fmt_ret
== -1) {
5703 PERROR("Failed to format the path of the last archived trace chunk");
5704 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5705 cmd_ret
= LTTNG_ERR_UNK
;
5709 case CONSUMER_DST_NET
:
5711 uint16_t ctrl_port
, data_port
;
5713 current_tracing_path_reply
= info_return
->location
.relay
.relative_path
;
5714 current_tracing_path_reply_len
=
5715 sizeof(info_return
->location
.relay
.relative_path
);
5716 /* Currently the only supported relay protocol. */
5717 info_return
->location
.relay
.protocol
=
5718 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5720 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5721 session_get_net_consumer_hostname(session
),
5722 sizeof(info_return
->location
.relay
.host
));
5724 ERR("Failed to copy host name to rotate_get_info reply");
5725 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5726 cmd_ret
= LTTNG_ERR_SET_URL
;
5730 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5731 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5732 info_return
->location
.relay
.ports
.data
= data_port
;
5733 info_return
->location_type
=
5734 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5735 chunk_path
= strdup(session
->last_chunk_path
);
5737 ERR("Failed to allocate the path of the last archived trace chunk");
5738 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5739 cmd_ret
= LTTNG_ERR_UNK
;
5748 fmt_ret
= lttng_strncpy(
5749 current_tracing_path_reply
, chunk_path
, current_tracing_path_reply_len
);
5752 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5753 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5754 cmd_ret
= LTTNG_ERR_UNK
;
5760 case LTTNG_ROTATION_STATE_ERROR
:
5761 DBG("Reporting that an error occurred during rotation %" PRIu64
5762 " of session \"%s\"",
5772 info_return
->status
= (int32_t) rotation_state
;
5777 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5779 * Configure the automatic rotation parameters.
5780 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5781 * 'activate' to false means deactivate the rotation schedule and validate that
5782 * 'new_value' has the same value as the currently active value.
5784 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5786 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5788 enum lttng_rotation_schedule_type schedule_type
,
5790 struct notification_thread_handle
*notification_thread_handle
)
5793 uint64_t *parameter_value
;
5795 LTTNG_ASSERT(session
);
5797 DBG("Cmd rotate set schedule session %s", session
->name
);
5799 if (session
->live_timer
|| !session
->output_traces
) {
5800 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5801 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5805 switch (schedule_type
) {
5806 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5807 parameter_value
= &session
->rotate_size
;
5809 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5810 parameter_value
= &session
->rotate_timer_period
;
5811 if (new_value
>= UINT_MAX
) {
5812 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
5816 ret
= LTTNG_ERR_INVALID
;
5821 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5822 ret
= LTTNG_ERR_INVALID
;
5826 /* Improper use of the API. */
5827 if (new_value
== -1ULL) {
5828 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5829 ret
= LTTNG_ERR_INVALID
;
5834 * As indicated in struct ltt_session's comments, a value of == 0 means
5835 * this schedule rotation type is not in use.
5837 * Reject the command if we were asked to activate a schedule that was
5840 if (activate
&& *parameter_value
!= 0) {
5841 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5842 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5847 * Reject the command if we were asked to deactivate a schedule that was
5850 if (!activate
&& *parameter_value
== 0) {
5851 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5852 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5857 * Reject the command if we were asked to deactivate a schedule that
5860 if (!activate
&& *parameter_value
!= new_value
) {
5861 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5862 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5866 *parameter_value
= activate
? new_value
: 0;
5868 switch (schedule_type
) {
5869 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5870 if (activate
&& session
->active
) {
5872 * Only start the timer if the session is active,
5873 * otherwise it will be started when the session starts.
5875 ret
= timer_session_rotation_schedule_timer_start(session
, new_value
);
5877 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5878 ret
= LTTNG_ERR_UNK
;
5882 ret
= timer_session_rotation_schedule_timer_stop(session
);
5884 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5885 ret
= LTTNG_ERR_UNK
;
5890 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5892 ret
= subscribe_session_consumed_size_rotation(
5893 session
, new_value
, notification_thread_handle
);
5895 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5896 ret
= LTTNG_ERR_UNK
;
5900 ret
= unsubscribe_session_consumed_size_rotation(
5901 session
, notification_thread_handle
);
5903 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5904 ret
= LTTNG_ERR_UNK
;
5910 /* Would have been caught before. */
5922 /* Wait for a given path to be removed before continuing. */
5923 static enum lttng_error_code
wait_on_path(void *path_data
)
5925 const char *shm_path
= (const char *) path_data
;
5927 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5933 ret
= stat(shm_path
, &st
);
5935 if (errno
!= ENOENT
) {
5936 PERROR("stat() returned an error while checking for the existence of the shm path");
5938 DBG("shm path no longer exists, completing the destruction of session");
5942 if (!S_ISDIR(st
.st_mode
)) {
5943 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5948 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5954 * Returns a pointer to a handler to run on completion of a command.
5955 * Returns NULL if no handler has to be run for the last command executed.
5957 const struct cmd_completion_handler
*cmd_pop_completion_handler()
5959 struct cmd_completion_handler
*handler
= current_completion_handler
;
5961 current_completion_handler
= nullptr;
5966 * Init command subsystem.
5971 * Set network sequence index to 1 for streams to match a relayd
5972 * socket on the consumer side.
5974 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5975 relayd_net_seq_idx
= 1;
5976 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5978 DBG("Command subsystem initialized");