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/utils.hpp>
46 #include <lttng/action/action-internal.hpp>
47 #include <lttng/action/action.h>
48 #include <lttng/channel-internal.hpp>
49 #include <lttng/channel.h>
50 #include <lttng/condition/condition-internal.hpp>
51 #include <lttng/condition/condition.h>
52 #include <lttng/condition/event-rule-matches-internal.hpp>
53 #include <lttng/condition/event-rule-matches.h>
54 #include <lttng/error-query-internal.hpp>
55 #include <lttng/event-internal.hpp>
56 #include <lttng/event-rule/event-rule-internal.hpp>
57 #include <lttng/event-rule/event-rule.h>
58 #include <lttng/location-internal.hpp>
59 #include <lttng/lttng-error.h>
60 #include <lttng/rotate-internal.hpp>
61 #include <lttng/session-descriptor-internal.hpp>
62 #include <lttng/session-internal.hpp>
63 #include <lttng/tracker.h>
64 #include <lttng/trigger/trigger-internal.hpp>
65 #include <lttng/userspace-probe-internal.hpp>
71 #include <urcu/list.h>
72 #include <urcu/uatomic.h>
74 /* Sleep for 100ms between each check for the shm path's deletion. */
75 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
77 namespace lsu
= lttng::sessiond::ust
;
79 static enum lttng_error_code
wait_on_path(void *path
);
82 struct cmd_destroy_session_reply_context
{
84 bool implicit_rotation_on_destroy
;
86 * Indicates whether or not an error occurred while launching the
87 * destruction of a session.
89 enum lttng_error_code destruction_status
;
93 * Command completion handler that is used by the destroy command
94 * when a session that has a non-default shm_path is being destroyed.
96 * See comment in cmd_destroy_session() for the rationale.
98 struct destroy_completion_handler
{
99 struct cmd_completion_handler handler
;
100 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
101 } destroy_completion_handler
= {
102 .handler
= { .run
= wait_on_path
, .data
= destroy_completion_handler
.shm_path
},
107 * Used to keep a unique index for each relayd socket created where this value
108 * is associated with streams on the consumer so it can match the right relayd
109 * to send to. It must be accessed with the relayd_net_seq_idx_lock
112 pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
113 uint64_t relayd_net_seq_idx
;
116 static struct cmd_completion_handler
*current_completion_handler
;
117 static int validate_ust_event_name(const char *);
118 static int cmd_enable_event_internal(struct ltt_session
*session
,
119 const struct lttng_domain
*domain
,
121 struct lttng_event
*event
,
122 char *filter_expression
,
123 struct lttng_bytecode
*filter
,
124 struct lttng_event_exclusion
*exclusion
,
126 static enum lttng_error_code
cmd_enable_channel_internal(struct ltt_session
*session
,
127 const struct lttng_domain
*domain
,
128 const struct lttng_channel
*_attr
,
132 * Create a session path used by list_lttng_sessions for the case that the
133 * session consumer is on the network.
135 static int build_network_session_path(char *dst
, size_t size
, struct ltt_session
*session
)
137 int ret
, kdata_port
, udata_port
;
138 struct lttng_uri
*kuri
= nullptr, *uuri
= nullptr, *uri
= nullptr;
139 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
141 LTTNG_ASSERT(session
);
144 memset(tmp_urls
, 0, sizeof(tmp_urls
));
145 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
147 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
149 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
150 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
151 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
154 if (session
->ust_session
&& session
->ust_session
->consumer
) {
155 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
156 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
159 if (uuri
== nullptr && kuri
== nullptr) {
160 uri
= &session
->consumer
->dst
.net
.control
;
161 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
162 } else if (kuri
&& uuri
) {
163 ret
= uri_compare(kuri
, uuri
);
167 /* Build uuri URL string */
168 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
175 } else if (kuri
&& uuri
== nullptr) {
177 } else if (uuri
&& kuri
== nullptr) {
181 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
187 * Do we have a UST url set. If yes, this means we have both kernel and UST
190 if (*tmp_uurl
!= '\0') {
193 "[K]: %s [data: %d] -- [U]: %s [data: %d]",
200 if (kuri
|| (!kuri
&& !uuri
)) {
203 /* No kernel URI, use the UST port. */
206 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
214 * Get run-time attributes if the session has been started (discarded events,
217 static int get_kernel_runtime_stats(struct ltt_session
*session
,
218 struct ltt_kernel_channel
*kchan
,
219 uint64_t *discarded_events
,
220 uint64_t *lost_packets
)
224 if (!session
->has_been_started
) {
226 *discarded_events
= 0;
231 ret
= consumer_get_discarded_events(
232 session
->id
, kchan
->key
, session
->kernel_session
->consumer
, discarded_events
);
237 ret
= consumer_get_lost_packets(
238 session
->id
, kchan
->key
, session
->kernel_session
->consumer
, lost_packets
);
248 * Get run-time attributes if the session has been started (discarded events,
251 static int get_ust_runtime_stats(struct ltt_session
*session
,
252 struct ltt_ust_channel
*uchan
,
253 uint64_t *discarded_events
,
254 uint64_t *lost_packets
)
257 struct ltt_ust_session
*usess
;
259 if (!discarded_events
|| !lost_packets
) {
264 usess
= session
->ust_session
;
265 LTTNG_ASSERT(discarded_events
);
266 LTTNG_ASSERT(lost_packets
);
268 if (!usess
|| !session
->has_been_started
) {
269 *discarded_events
= 0;
275 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
276 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
277 &usess
->buffer_reg_uid_list
,
280 uchan
->attr
.overwrite
,
283 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
284 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
287 uchan
->attr
.overwrite
,
293 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
294 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
296 ERR("Unsupported buffer type");
307 * Create a list of agent domain events.
309 * Return number of events in list on success or else a negative value.
311 static enum lttng_error_code
list_lttng_agent_events(struct agent
*agt
,
312 struct lttng_payload
*reply_payload
,
313 unsigned int *nb_events
)
315 enum lttng_error_code ret_code
;
317 unsigned int local_nb_events
= 0;
318 struct agent_event
*event
;
319 struct lttng_ht_iter iter
;
320 unsigned long agent_event_count
;
323 assert(reply_payload
);
325 DBG3("Listing agent events");
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
;
341 cds_lfht_for_each_entry (agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
342 struct lttng_event
*tmp_event
= lttng_event_create();
345 ret_code
= LTTNG_ERR_NOMEM
;
349 if (lttng_strncpy(tmp_event
->name
, event
->name
, sizeof(tmp_event
->name
))) {
350 lttng_event_destroy(tmp_event
);
351 ret_code
= LTTNG_ERR_FATAL
;
355 tmp_event
->name
[sizeof(tmp_event
->name
) - 1] = '\0';
356 tmp_event
->enabled
= !!event
->enabled_count
;
357 tmp_event
->loglevel
= event
->loglevel_value
;
358 tmp_event
->loglevel_type
= event
->loglevel_type
;
360 ret
= lttng_event_serialize(
361 tmp_event
, 0, nullptr, event
->filter_expression
, 0, nullptr, reply_payload
);
362 lttng_event_destroy(tmp_event
);
364 ret_code
= LTTNG_ERR_FATAL
;
371 *nb_events
= local_nb_events
;
378 * Create a list of ust global domain events.
380 static enum lttng_error_code
list_lttng_ust_global_events(char *channel_name
,
381 struct ltt_ust_domain_global
*ust_global
,
382 struct lttng_payload
*reply_payload
,
383 unsigned int *nb_events
)
385 enum lttng_error_code ret_code
;
387 struct lttng_ht_iter iter
;
388 struct lttng_ht_node_str
*node
;
389 struct ltt_ust_channel
*uchan
;
390 struct ltt_ust_event
*uevent
;
391 unsigned long channel_event_count
;
392 unsigned int local_nb_events
= 0;
394 assert(reply_payload
);
397 DBG("Listing UST global events for channel %s", channel_name
);
401 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
402 node
= lttng_ht_iter_get_node_str(&iter
);
403 if (node
== nullptr) {
404 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
408 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
410 channel_event_count
= lttng_ht_get_count(uchan
->events
);
411 if (channel_event_count
== 0) {
417 if (channel_event_count
> UINT_MAX
) {
418 ret_code
= LTTNG_ERR_OVERFLOW
;
422 local_nb_events
= (unsigned int) channel_event_count
;
424 DBG3("Listing UST global %d events", *nb_events
);
426 cds_lfht_for_each_entry (uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
427 struct lttng_event
*tmp_event
= nullptr;
429 if (uevent
->internal
) {
430 /* This event should remain hidden from clients */
435 tmp_event
= lttng_event_create();
437 ret_code
= LTTNG_ERR_NOMEM
;
441 if (lttng_strncpy(tmp_event
->name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
)) {
442 ret_code
= LTTNG_ERR_FATAL
;
443 lttng_event_destroy(tmp_event
);
447 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
448 tmp_event
->enabled
= uevent
->enabled
;
450 switch (uevent
->attr
.instrumentation
) {
451 case LTTNG_UST_ABI_TRACEPOINT
:
452 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
454 case LTTNG_UST_ABI_PROBE
:
455 tmp_event
->type
= LTTNG_EVENT_PROBE
;
457 case LTTNG_UST_ABI_FUNCTION
:
458 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
462 tmp_event
->loglevel
= uevent
->attr
.loglevel
;
463 switch (uevent
->attr
.loglevel_type
) {
464 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
465 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
467 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
468 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
470 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
471 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
474 if (uevent
->filter
) {
475 tmp_event
->filter
= 1;
477 if (uevent
->exclusion
) {
478 tmp_event
->exclusion
= 1;
482 * We do not care about the filter bytecode and the fd from the
483 * userspace_probe_location.
485 ret
= lttng_event_serialize(tmp_event
,
486 uevent
->exclusion
? uevent
->exclusion
->count
: 0,
487 uevent
->exclusion
? (char **) uevent
->exclusion
->names
:
489 uevent
->filter_expression
,
493 lttng_event_destroy(tmp_event
);
495 ret_code
= LTTNG_ERR_FATAL
;
501 /* nb_events is already set at this point. */
503 *nb_events
= local_nb_events
;
510 * Fill lttng_event array of all kernel events in the channel.
512 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
513 struct ltt_kernel_session
*kernel_session
,
514 struct lttng_payload
*reply_payload
,
515 unsigned int *nb_events
)
517 enum lttng_error_code ret_code
;
519 struct ltt_kernel_event
*event
;
520 struct ltt_kernel_channel
*kchan
;
522 assert(reply_payload
);
524 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
525 if (kchan
== nullptr) {
526 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
530 *nb_events
= kchan
->event_count
;
532 DBG("Listing events for channel %s", kchan
->channel
->name
);
534 if (*nb_events
== 0) {
539 /* Kernel channels */
540 cds_list_for_each_entry (event
, &kchan
->events_list
.head
, list
) {
541 struct lttng_event
*tmp_event
= lttng_event_create();
544 ret_code
= LTTNG_ERR_NOMEM
;
548 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
549 lttng_event_destroy(tmp_event
);
550 ret_code
= LTTNG_ERR_FATAL
;
554 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
555 tmp_event
->enabled
= event
->enabled
;
556 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
558 switch (event
->event
->instrumentation
) {
559 case LTTNG_KERNEL_ABI_TRACEPOINT
:
560 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
562 case LTTNG_KERNEL_ABI_KRETPROBE
:
563 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
564 memcpy(&tmp_event
->attr
.probe
,
565 &event
->event
->u
.kprobe
,
566 sizeof(struct lttng_kernel_abi_kprobe
));
568 case LTTNG_KERNEL_ABI_KPROBE
:
569 tmp_event
->type
= LTTNG_EVENT_PROBE
;
570 memcpy(&tmp_event
->attr
.probe
,
571 &event
->event
->u
.kprobe
,
572 sizeof(struct lttng_kernel_abi_kprobe
));
574 case LTTNG_KERNEL_ABI_UPROBE
:
575 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
577 case LTTNG_KERNEL_ABI_FUNCTION
:
578 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
579 memcpy(&(tmp_event
->attr
.ftrace
),
580 &event
->event
->u
.ftrace
,
581 sizeof(struct lttng_kernel_abi_function
));
583 case LTTNG_KERNEL_ABI_NOOP
:
584 tmp_event
->type
= LTTNG_EVENT_NOOP
;
586 case LTTNG_KERNEL_ABI_SYSCALL
:
587 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
589 case LTTNG_KERNEL_ABI_ALL
:
596 if (event
->userspace_probe_location
) {
597 struct lttng_userspace_probe_location
*location_copy
=
598 lttng_userspace_probe_location_copy(
599 event
->userspace_probe_location
);
601 if (!location_copy
) {
602 lttng_event_destroy(tmp_event
);
603 ret_code
= LTTNG_ERR_NOMEM
;
607 ret
= lttng_event_set_userspace_probe_location(tmp_event
, location_copy
);
609 lttng_event_destroy(tmp_event
);
610 lttng_userspace_probe_location_destroy(location_copy
);
611 ret_code
= LTTNG_ERR_INVALID
;
616 ret
= lttng_event_serialize(
617 tmp_event
, 0, nullptr, event
->filter_expression
, 0, nullptr, reply_payload
);
618 lttng_event_destroy(tmp_event
);
620 ret_code
= LTTNG_ERR_FATAL
;
631 * Add URI so the consumer output object. Set the correct path depending on the
632 * domain adding the default trace directory.
634 static enum lttng_error_code
add_uri_to_consumer(const struct ltt_session
*session
,
635 struct consumer_output
*consumer
,
636 struct lttng_uri
*uri
,
637 enum lttng_domain_type domain
)
640 enum lttng_error_code ret_code
= LTTNG_OK
;
644 if (consumer
== nullptr) {
645 DBG("No consumer detected. Don't add URI. Stopping.");
646 ret_code
= LTTNG_ERR_NO_CONSUMER
;
651 case LTTNG_DOMAIN_KERNEL
:
652 ret
= lttng_strncpy(consumer
->domain_subdir
,
653 DEFAULT_KERNEL_TRACE_DIR
,
654 sizeof(consumer
->domain_subdir
));
656 case LTTNG_DOMAIN_UST
:
657 ret
= lttng_strncpy(consumer
->domain_subdir
,
658 DEFAULT_UST_TRACE_DIR
,
659 sizeof(consumer
->domain_subdir
));
663 * This case is possible is we try to add the URI to the global
664 * tracing session consumer object which in this case there is
667 memset(consumer
->domain_subdir
, 0, sizeof(consumer
->domain_subdir
));
671 ERR("Failed to initialize consumer output domain subdirectory");
672 ret_code
= LTTNG_ERR_FATAL
;
676 switch (uri
->dtype
) {
679 DBG2("Setting network URI to consumer");
681 if (consumer
->type
== CONSUMER_DST_NET
) {
682 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
683 consumer
->dst
.net
.control_isset
) ||
684 (uri
->stype
== LTTNG_STREAM_DATA
&& consumer
->dst
.net
.data_isset
)) {
685 ret_code
= LTTNG_ERR_URL_EXIST
;
689 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
692 /* Set URI into consumer output object */
693 ret
= consumer_set_network_uri(session
, consumer
, uri
);
695 ret_code
= (lttng_error_code
) -ret
;
697 } else if (ret
== 1) {
699 * URI was the same in the consumer so we do not append the subdir
700 * again so to not duplicate output dir.
707 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
708 ret_code
= LTTNG_ERR_INVALID
;
711 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
712 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
714 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
716 sizeof(consumer
->dst
.session_root_path
));
718 ret_code
= LTTNG_ERR_FATAL
;
721 consumer
->type
= CONSUMER_DST_LOCAL
;
731 * Init tracing by creating trace directory and sending fds kernel consumer.
733 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
736 struct lttng_ht_iter iter
;
737 struct consumer_socket
*socket
;
739 LTTNG_ASSERT(session
);
743 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= nullptr) {
744 cds_lfht_for_each_entry (
745 session
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
746 pthread_mutex_lock(socket
->lock
);
747 ret
= kernel_consumer_send_session(socket
, session
);
748 pthread_mutex_unlock(socket
->lock
);
750 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
762 * Create a socket to the relayd using the URI.
764 * On success, the relayd_sock pointer is set to the created socket.
765 * Else, it remains untouched and an LTTng error code is returned.
767 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
768 struct lttcomm_relayd_sock
**relayd_sock
,
769 struct consumer_output
*consumer
)
772 enum lttng_error_code status
= LTTNG_OK
;
773 struct lttcomm_relayd_sock
*rsock
;
775 rsock
= lttcomm_alloc_relayd_sock(
776 uri
, RELAYD_VERSION_COMM_MAJOR
, RELAYD_VERSION_COMM_MINOR
);
778 status
= LTTNG_ERR_FATAL
;
783 * Connect to relayd so we can proceed with a session creation. This call
784 * can possibly block for an arbitrary amount of time to set the health
785 * state to be in poll execution.
788 ret
= relayd_connect(rsock
);
791 ERR("Unable to reach lttng-relayd");
792 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
796 /* Create socket for control stream. */
797 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
798 uint64_t result_flags
;
800 DBG3("Creating relayd stream socket from URI");
802 /* Check relayd version */
803 ret
= relayd_version_check(rsock
);
804 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
805 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
807 } else if (ret
< 0) {
808 ERR("Unable to reach lttng-relayd");
809 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
812 consumer
->relay_major_version
= rsock
->major
;
813 consumer
->relay_minor_version
= rsock
->minor
;
814 ret
= relayd_get_configuration(rsock
, 0, &result_flags
);
816 ERR("Unable to get relayd configuration");
817 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
820 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
821 consumer
->relay_allows_clear
= true;
823 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
824 DBG3("Creating relayd data socket from URI");
826 /* Command is not valid */
827 ERR("Relayd invalid stream type: %d", uri
->stype
);
828 status
= LTTNG_ERR_INVALID
;
832 *relayd_sock
= rsock
;
837 /* The returned value is not useful since we are on an error path. */
838 (void) relayd_close(rsock
);
846 * Connect to the relayd using URI and send the socket to the right consumer.
848 * The consumer socket lock must be held by the caller.
850 * Returns LTTNG_OK on success or an LTTng error code on failure.
852 static enum lttng_error_code
send_consumer_relayd_socket(unsigned int session_id
,
853 struct lttng_uri
*relayd_uri
,
854 struct consumer_output
*consumer
,
855 struct consumer_socket
*consumer_sock
,
856 const char *session_name
,
857 const char *hostname
,
858 const char *base_path
,
859 int session_live_timer
,
860 const uint64_t *current_chunk_id
,
861 time_t session_creation_time
,
862 bool session_name_contains_creation_time
)
865 struct lttcomm_relayd_sock
*rsock
= nullptr;
866 enum lttng_error_code status
;
868 /* Connect to relayd and make version check if uri is the control. */
869 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
870 if (status
!= LTTNG_OK
) {
871 goto relayd_comm_error
;
875 /* Set the network sequence index if not set. */
876 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
877 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
879 * Increment net_seq_idx because we are about to transfer the
880 * new relayd socket to the consumer.
881 * Assign unique key so the consumer can match streams.
883 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
884 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
887 /* Send relayd socket to consumer. */
888 ret
= consumer_send_relayd_socket(consumer_sock
,
898 session_creation_time
,
899 session_name_contains_creation_time
);
901 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
905 /* Flag that the corresponding socket was sent. */
906 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
907 consumer_sock
->control_sock_sent
= 1;
908 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
909 consumer_sock
->data_sock_sent
= 1;
913 * Close socket which was dup on the consumer side. The session daemon does
914 * NOT keep track of the relayd socket(s) once transfer to the consumer.
918 if (status
!= LTTNG_OK
) {
920 * The consumer output for this session should not be used anymore
921 * since the relayd connection failed thus making any tracing or/and
922 * streaming not usable.
924 consumer
->enabled
= 0;
926 (void) relayd_close(rsock
);
934 * Send both relayd sockets to a specific consumer and domain. This is a
935 * helper function to facilitate sending the information to the consumer for a
938 * The consumer socket lock must be held by the caller.
940 * Returns LTTNG_OK, or an LTTng error code on failure.
942 static enum lttng_error_code
send_consumer_relayd_sockets(unsigned int session_id
,
943 struct consumer_output
*consumer
,
944 struct consumer_socket
*sock
,
945 const char *session_name
,
946 const char *hostname
,
947 const char *base_path
,
948 int session_live_timer
,
949 const uint64_t *current_chunk_id
,
950 time_t session_creation_time
,
951 bool session_name_contains_creation_time
)
953 enum lttng_error_code status
= LTTNG_OK
;
955 LTTNG_ASSERT(consumer
);
958 /* Sending control relayd socket. */
959 if (!sock
->control_sock_sent
) {
960 status
= send_consumer_relayd_socket(session_id
,
961 &consumer
->dst
.net
.control
,
969 session_creation_time
,
970 session_name_contains_creation_time
);
971 if (status
!= LTTNG_OK
) {
976 /* Sending data relayd socket. */
977 if (!sock
->data_sock_sent
) {
978 status
= send_consumer_relayd_socket(session_id
,
979 &consumer
->dst
.net
.data
,
987 session_creation_time
,
988 session_name_contains_creation_time
);
989 if (status
!= LTTNG_OK
) {
999 * Setup relayd connections for a tracing session. First creates the socket to
1000 * the relayd and send them to the right domain consumer. Consumer type MUST be
1003 int cmd_setup_relayd(struct ltt_session
*session
)
1006 struct ltt_ust_session
*usess
;
1007 struct ltt_kernel_session
*ksess
;
1008 struct consumer_socket
*socket
;
1009 struct lttng_ht_iter iter
;
1010 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1012 LTTNG_ASSERT(session
);
1014 usess
= session
->ust_session
;
1015 ksess
= session
->kernel_session
;
1017 DBG("Setting relayd for session %s", session
->name
);
1020 if (session
->current_trace_chunk
) {
1021 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1022 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1024 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1025 current_chunk_id
.is_set
= true;
1027 ERR("Failed to get current trace chunk id");
1028 ret
= LTTNG_ERR_UNK
;
1033 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
&&
1034 usess
->consumer
->enabled
) {
1035 /* For each consumer socket, send relayd sockets */
1036 cds_lfht_for_each_entry (
1037 usess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1038 pthread_mutex_lock(socket
->lock
);
1039 ret
= send_consumer_relayd_sockets(
1046 session
->live_timer
,
1047 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
1048 session
->creation_time
,
1049 session
->name_contains_creation_time
);
1050 pthread_mutex_unlock(socket
->lock
);
1051 if (ret
!= LTTNG_OK
) {
1054 /* Session is now ready for network streaming. */
1055 session
->net_handle
= 1;
1057 session
->consumer
->relay_major_version
= usess
->consumer
->relay_major_version
;
1058 session
->consumer
->relay_minor_version
= usess
->consumer
->relay_minor_version
;
1059 session
->consumer
->relay_allows_clear
= usess
->consumer
->relay_allows_clear
;
1062 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
&&
1063 ksess
->consumer
->enabled
) {
1064 cds_lfht_for_each_entry (
1065 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1066 pthread_mutex_lock(socket
->lock
);
1067 ret
= send_consumer_relayd_sockets(
1074 session
->live_timer
,
1075 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
1076 session
->creation_time
,
1077 session
->name_contains_creation_time
);
1078 pthread_mutex_unlock(socket
->lock
);
1079 if (ret
!= LTTNG_OK
) {
1082 /* Session is now ready for network streaming. */
1083 session
->net_handle
= 1;
1085 session
->consumer
->relay_major_version
= ksess
->consumer
->relay_major_version
;
1086 session
->consumer
->relay_minor_version
= ksess
->consumer
->relay_minor_version
;
1087 session
->consumer
->relay_allows_clear
= ksess
->consumer
->relay_allows_clear
;
1096 * Start a kernel session by opening all necessary streams.
1098 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1101 struct ltt_kernel_channel
*kchan
;
1103 /* Open kernel metadata */
1104 if (ksess
->metadata
== nullptr && ksess
->output_traces
) {
1105 ret
= kernel_open_metadata(ksess
);
1107 ret
= LTTNG_ERR_KERN_META_FAIL
;
1112 /* Open kernel metadata stream */
1113 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1114 ret
= kernel_open_metadata_stream(ksess
);
1116 ERR("Kernel create metadata stream failed");
1117 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1122 /* For each channel */
1123 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1124 if (kchan
->stream_count
== 0) {
1125 ret
= kernel_open_channel_stream(kchan
);
1127 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1130 /* Update the stream global counter */
1131 ksess
->stream_count_global
+= ret
;
1135 /* Setup kernel consumer socket and send fds to it */
1136 ret
= init_kernel_tracing(ksess
);
1138 ret
= LTTNG_ERR_KERN_START_FAIL
;
1142 /* This start the kernel tracing */
1143 ret
= kernel_start_session(ksess
);
1145 ret
= LTTNG_ERR_KERN_START_FAIL
;
1149 /* Quiescent wait after starting trace */
1150 kernel_wait_quiescent();
1160 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1162 struct ltt_kernel_channel
*kchan
;
1163 bool error_occurred
= false;
1166 if (!ksess
|| !ksess
->active
) {
1169 DBG("Stopping kernel tracing");
1171 ret
= kernel_stop_session(ksess
);
1173 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1177 kernel_wait_quiescent();
1179 /* Flush metadata after stopping (if exists) */
1180 if (ksess
->metadata_stream_fd
>= 0) {
1181 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1183 ERR("Kernel metadata flush failed");
1184 error_occurred
= true;
1188 /* Flush all buffers after stopping */
1189 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1190 ret
= kernel_flush_buffer(kchan
);
1192 ERR("Kernel flush buffer error");
1193 error_occurred
= true;
1198 if (error_occurred
) {
1199 ret
= LTTNG_ERR_UNK
;
1208 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1210 int cmd_disable_channel(struct ltt_session
*session
,
1211 enum lttng_domain_type domain
,
1215 struct ltt_ust_session
*usess
;
1217 usess
= session
->ust_session
;
1222 case LTTNG_DOMAIN_KERNEL
:
1224 ret
= channel_kernel_disable(session
->kernel_session
, channel_name
);
1225 if (ret
!= LTTNG_OK
) {
1229 kernel_wait_quiescent();
1232 case LTTNG_DOMAIN_UST
:
1234 struct ltt_ust_channel
*uchan
;
1235 struct lttng_ht
*chan_ht
;
1237 chan_ht
= usess
->domain_global
.channels
;
1239 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1240 if (uchan
== nullptr) {
1241 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1245 ret
= channel_ust_disable(usess
, uchan
);
1246 if (ret
!= LTTNG_OK
) {
1252 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1264 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1266 * The wpipe arguments is used as a notifier for the kernel thread.
1268 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1272 ssize_t sock_recv_len
;
1273 struct lttng_channel
*channel
= nullptr;
1274 struct lttng_buffer_view view
;
1275 struct lttng_dynamic_buffer channel_buffer
;
1276 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1278 lttng_dynamic_buffer_init(&channel_buffer
);
1279 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1280 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1282 ret
= LTTNG_ERR_NOMEM
;
1286 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
, channel_len
);
1287 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1288 ERR("Failed to receive \"enable channel\" command payload");
1289 ret
= LTTNG_ERR_INVALID
;
1293 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1294 if (!lttng_buffer_view_is_valid(&view
)) {
1295 ret
= LTTNG_ERR_INVALID
;
1299 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1300 ERR("Invalid channel payload received in \"enable channel\" command");
1301 ret
= LTTNG_ERR_INVALID
;
1305 ret
= cmd_enable_channel_internal(cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1308 lttng_dynamic_buffer_reset(&channel_buffer
);
1309 lttng_channel_destroy(channel
);
1313 static enum lttng_error_code
cmd_enable_channel_internal(struct ltt_session
*session
,
1314 const struct lttng_domain
*domain
,
1315 const struct lttng_channel
*_attr
,
1318 enum lttng_error_code ret_code
;
1319 struct ltt_ust_session
*usess
= session
->ust_session
;
1320 struct lttng_ht
*chan_ht
;
1322 struct lttng_channel
*attr
= nullptr;
1324 LTTNG_ASSERT(session
);
1325 LTTNG_ASSERT(_attr
);
1326 LTTNG_ASSERT(domain
);
1328 attr
= lttng_channel_copy(_attr
);
1330 ret_code
= LTTNG_ERR_NOMEM
;
1334 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1336 /* Validate channel name */
1337 if (attr
->name
[0] == '.' || memchr(attr
->name
, '/', len
) != nullptr) {
1338 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1342 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1347 * If the session is a live session, remove the switch timer, the
1348 * live timer does the same thing but sends also synchronisation
1349 * beacons for inactive streams.
1351 if (session
->live_timer
> 0) {
1352 attr
->attr
.live_timer_interval
= session
->live_timer
;
1353 attr
->attr
.switch_timer_interval
= 0;
1356 /* Check for feature support */
1357 switch (domain
->type
) {
1358 case LTTNG_DOMAIN_KERNEL
:
1360 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1361 /* Sampling position of buffer is not supported */
1362 WARN("Kernel tracer does not support buffer monitoring. "
1363 "Setting the monitor interval timer to 0 "
1364 "(disabled) for channel '%s' of session '%s'",
1367 lttng_channel_set_monitor_timer_interval(attr
, 0);
1371 case LTTNG_DOMAIN_UST
:
1373 case LTTNG_DOMAIN_JUL
:
1374 case LTTNG_DOMAIN_LOG4J
:
1375 case LTTNG_DOMAIN_PYTHON
:
1376 if (!agent_tracing_is_enabled()) {
1377 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1378 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1383 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1387 switch (domain
->type
) {
1388 case LTTNG_DOMAIN_KERNEL
:
1390 struct ltt_kernel_channel
*kchan
;
1392 kchan
= trace_kernel_get_channel_by_name(attr
->name
, session
->kernel_session
);
1393 if (kchan
== nullptr) {
1395 * Don't try to create a channel if the session has been started at
1396 * some point in time before. The tracer does not allow it.
1398 if (session
->has_been_started
) {
1399 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1403 if (session
->snapshot
.nb_output
> 0 || session
->snapshot_mode
) {
1404 /* Enforce mmap output for snapshot sessions. */
1405 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1407 ret_code
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1408 if (attr
->name
[0] != '\0') {
1409 session
->kernel_session
->has_non_default_channel
= 1;
1412 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1415 if (ret_code
!= LTTNG_OK
) {
1419 kernel_wait_quiescent();
1422 case LTTNG_DOMAIN_UST
:
1423 case LTTNG_DOMAIN_JUL
:
1424 case LTTNG_DOMAIN_LOG4J
:
1425 case LTTNG_DOMAIN_PYTHON
:
1427 struct ltt_ust_channel
*uchan
;
1432 * Current agent implementation limitations force us to allow
1433 * only one channel at once in "agent" subdomains. Each
1434 * subdomain has a default channel name which must be strictly
1437 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1438 if (strncmp(attr
->name
,
1439 DEFAULT_JUL_CHANNEL_NAME
,
1440 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1441 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1444 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1445 if (strncmp(attr
->name
,
1446 DEFAULT_LOG4J_CHANNEL_NAME
,
1447 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1448 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1451 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1452 if (strncmp(attr
->name
,
1453 DEFAULT_PYTHON_CHANNEL_NAME
,
1454 LTTNG_SYMBOL_NAME_LEN
- 1) != 0) {
1455 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1460 chan_ht
= usess
->domain_global
.channels
;
1462 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1463 if (uchan
== nullptr) {
1465 * Don't try to create a channel if the session has been started at
1466 * some point in time before. The tracer does not allow it.
1468 if (session
->has_been_started
) {
1469 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1473 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1474 if (attr
->name
[0] != '\0') {
1475 usess
->has_non_default_channel
= 1;
1478 ret_code
= channel_ust_enable(usess
, uchan
);
1483 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1487 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1488 session
->has_non_mmap_channel
= true;
1493 lttng_channel_destroy(attr
);
1497 enum lttng_error_code
1498 cmd_process_attr_tracker_get_tracking_policy(struct ltt_session
*session
,
1499 enum lttng_domain_type domain
,
1500 enum lttng_process_attr process_attr
,
1501 enum lttng_tracking_policy
*policy
)
1503 enum lttng_error_code ret_code
= LTTNG_OK
;
1504 const struct process_attr_tracker
*tracker
;
1507 case LTTNG_DOMAIN_KERNEL
:
1508 if (!session
->kernel_session
) {
1509 ret_code
= LTTNG_ERR_INVALID
;
1512 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1514 case LTTNG_DOMAIN_UST
:
1515 if (!session
->ust_session
) {
1516 ret_code
= LTTNG_ERR_INVALID
;
1519 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1522 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1526 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1528 ret_code
= LTTNG_ERR_INVALID
;
1534 enum lttng_error_code
1535 cmd_process_attr_tracker_set_tracking_policy(struct ltt_session
*session
,
1536 enum lttng_domain_type domain
,
1537 enum lttng_process_attr process_attr
,
1538 enum lttng_tracking_policy policy
)
1540 enum lttng_error_code ret_code
= LTTNG_OK
;
1543 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1544 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1545 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1548 ret_code
= LTTNG_ERR_INVALID
;
1553 case LTTNG_DOMAIN_KERNEL
:
1554 if (!session
->kernel_session
) {
1555 ret_code
= LTTNG_ERR_INVALID
;
1558 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1559 session
->kernel_session
, process_attr
, policy
);
1561 case LTTNG_DOMAIN_UST
:
1562 if (!session
->ust_session
) {
1563 ret_code
= LTTNG_ERR_INVALID
;
1566 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1567 session
->ust_session
, process_attr
, policy
);
1570 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1577 enum lttng_error_code
1578 cmd_process_attr_tracker_inclusion_set_add_value(struct ltt_session
*session
,
1579 enum lttng_domain_type domain
,
1580 enum lttng_process_attr process_attr
,
1581 const struct process_attr_value
*value
)
1583 enum lttng_error_code ret_code
= LTTNG_OK
;
1586 case LTTNG_DOMAIN_KERNEL
:
1587 if (!session
->kernel_session
) {
1588 ret_code
= LTTNG_ERR_INVALID
;
1591 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1592 session
->kernel_session
, process_attr
, value
);
1594 case LTTNG_DOMAIN_UST
:
1595 if (!session
->ust_session
) {
1596 ret_code
= LTTNG_ERR_INVALID
;
1599 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1600 session
->ust_session
, process_attr
, value
);
1603 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1610 enum lttng_error_code
1611 cmd_process_attr_tracker_inclusion_set_remove_value(struct ltt_session
*session
,
1612 enum lttng_domain_type domain
,
1613 enum lttng_process_attr process_attr
,
1614 const struct process_attr_value
*value
)
1616 enum lttng_error_code ret_code
= LTTNG_OK
;
1619 case LTTNG_DOMAIN_KERNEL
:
1620 if (!session
->kernel_session
) {
1621 ret_code
= LTTNG_ERR_INVALID
;
1624 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1625 session
->kernel_session
, process_attr
, value
);
1627 case LTTNG_DOMAIN_UST
:
1628 if (!session
->ust_session
) {
1629 ret_code
= LTTNG_ERR_INVALID
;
1632 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1633 session
->ust_session
, process_attr
, value
);
1636 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1643 enum lttng_error_code
1644 cmd_process_attr_tracker_get_inclusion_set(struct ltt_session
*session
,
1645 enum lttng_domain_type domain
,
1646 enum lttng_process_attr process_attr
,
1647 struct lttng_process_attr_values
**values
)
1649 enum lttng_error_code ret_code
= LTTNG_OK
;
1650 const struct process_attr_tracker
*tracker
;
1651 enum process_attr_tracker_status status
;
1654 case LTTNG_DOMAIN_KERNEL
:
1655 if (!session
->kernel_session
) {
1656 ret_code
= LTTNG_ERR_INVALID
;
1659 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1661 case LTTNG_DOMAIN_UST
:
1662 if (!session
->ust_session
) {
1663 ret_code
= LTTNG_ERR_INVALID
;
1666 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1669 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1674 ret_code
= LTTNG_ERR_INVALID
;
1678 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1680 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1681 ret_code
= LTTNG_OK
;
1683 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1684 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1686 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1687 ret_code
= LTTNG_ERR_NOMEM
;
1690 ret_code
= LTTNG_ERR_UNK
;
1699 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1701 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1702 struct lttng_event
*event
,
1703 char *filter_expression
,
1704 struct lttng_bytecode
*bytecode
,
1705 struct lttng_event_exclusion
*exclusion
)
1708 const char *event_name
;
1709 const struct ltt_session
*session
= cmd_ctx
->session
;
1710 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1711 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1713 DBG("Disable event command for event \'%s\'", event
->name
);
1716 * Filter and exclusions are simply not handled by the
1717 * disable event command at this time.
1721 (void) filter_expression
;
1724 /* Ignore the presence of filter or exclusion for the event */
1726 event
->exclusion
= 0;
1728 event_name
= event
->name
;
1730 /* Error out on unhandled search criteria */
1731 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
|| event
->pid
||
1732 event
->filter
|| event
->exclusion
) {
1733 ret
= LTTNG_ERR_UNK
;
1740 case LTTNG_DOMAIN_KERNEL
:
1742 struct ltt_kernel_channel
*kchan
;
1743 struct ltt_kernel_session
*ksess
;
1745 ksess
= session
->kernel_session
;
1748 * If a non-default channel has been created in the
1749 * session, explicitely require that -c chan_name needs
1752 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1753 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1757 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1758 if (kchan
== nullptr) {
1759 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1763 switch (event
->type
) {
1764 case LTTNG_EVENT_ALL
:
1765 case LTTNG_EVENT_TRACEPOINT
:
1766 case LTTNG_EVENT_SYSCALL
:
1767 case LTTNG_EVENT_PROBE
:
1768 case LTTNG_EVENT_FUNCTION
:
1769 case LTTNG_EVENT_FUNCTION_ENTRY
: /* fall-through */
1770 if (event_name
[0] == '\0') {
1771 ret
= event_kernel_disable_event(kchan
, nullptr, event
->type
);
1773 ret
= event_kernel_disable_event(kchan
, event_name
, event
->type
);
1775 if (ret
!= LTTNG_OK
) {
1780 ret
= LTTNG_ERR_UNK
;
1784 kernel_wait_quiescent();
1787 case LTTNG_DOMAIN_UST
:
1789 struct ltt_ust_channel
*uchan
;
1790 struct ltt_ust_session
*usess
;
1792 usess
= session
->ust_session
;
1794 if (validate_ust_event_name(event_name
)) {
1795 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1800 * If a non-default channel has been created in the
1801 * session, explicitly require that -c chan_name needs
1804 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1805 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1809 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
1810 if (uchan
== nullptr) {
1811 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1815 switch (event
->type
) {
1816 case LTTNG_EVENT_ALL
:
1818 * An empty event name means that everything
1819 * should be disabled.
1821 if (event
->name
[0] == '\0') {
1822 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1824 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1826 if (ret
!= LTTNG_OK
) {
1831 ret
= LTTNG_ERR_UNK
;
1835 DBG3("Disable UST event %s in channel %s completed", event_name
, channel_name
);
1838 case LTTNG_DOMAIN_LOG4J
:
1839 case LTTNG_DOMAIN_JUL
:
1840 case LTTNG_DOMAIN_PYTHON
:
1843 struct ltt_ust_session
*usess
= session
->ust_session
;
1845 LTTNG_ASSERT(usess
);
1847 switch (event
->type
) {
1848 case LTTNG_EVENT_ALL
:
1851 ret
= LTTNG_ERR_UNK
;
1855 agt
= trace_ust_find_agent(usess
, domain
);
1857 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1861 * An empty event name means that everything
1862 * should be disabled.
1864 if (event
->name
[0] == '\0') {
1865 ret
= event_agent_disable_all(usess
, agt
);
1867 ret
= event_agent_disable(usess
, agt
, event_name
);
1869 if (ret
!= LTTNG_OK
) {
1876 ret
= LTTNG_ERR_UND
;
1887 free(filter_expression
);
1892 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1894 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1895 const struct lttng_event_context
*event_context
,
1898 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1899 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1900 const struct ltt_session
*session
= cmd_ctx
->session
;
1901 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1904 * Don't try to add a context if the session has been started at
1905 * some point in time before. The tracer does not allow it and would
1906 * result in a corrupted trace.
1908 if (cmd_ctx
->session
->has_been_started
) {
1909 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1914 case LTTNG_DOMAIN_KERNEL
:
1915 LTTNG_ASSERT(session
->kernel_session
);
1917 if (session
->kernel_session
->channel_count
== 0) {
1918 /* Create default channel */
1919 ret
= channel_kernel_create(session
->kernel_session
, nullptr, kwpipe
);
1920 if (ret
!= LTTNG_OK
) {
1923 chan_kern_created
= 1;
1925 /* Add kernel context to kernel tracer */
1926 ret
= context_kernel_add(session
->kernel_session
, event_context
, channel_name
);
1927 if (ret
!= LTTNG_OK
) {
1931 case LTTNG_DOMAIN_JUL
:
1932 case LTTNG_DOMAIN_LOG4J
:
1935 * Validate channel name.
1936 * If no channel name is given and the domain is JUL or LOG4J,
1937 * set it to the appropriate domain-specific channel name. If
1938 * a name is provided but does not match the expexted channel
1939 * name, return an error.
1941 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1942 strcmp(channel_name
, DEFAULT_JUL_CHANNEL_NAME
) != 0) {
1943 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1945 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1946 strcmp(channel_name
, DEFAULT_LOG4J_CHANNEL_NAME
) != 0) {
1947 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1952 case LTTNG_DOMAIN_UST
:
1954 struct ltt_ust_session
*usess
= session
->ust_session
;
1955 unsigned int chan_count
;
1957 LTTNG_ASSERT(usess
);
1959 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1960 if (chan_count
== 0) {
1961 struct lttng_channel
*attr
;
1962 /* Create default channel */
1963 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1964 if (attr
== nullptr) {
1965 ret
= LTTNG_ERR_FATAL
;
1969 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1970 if (ret
!= LTTNG_OK
) {
1974 channel_attr_destroy(attr
);
1975 chan_ust_created
= 1;
1978 ret
= context_ust_add(usess
, domain
, event_context
, channel_name
);
1979 if (ret
!= LTTNG_OK
) {
1985 ret
= LTTNG_ERR_UND
;
1993 if (chan_kern_created
) {
1994 struct ltt_kernel_channel
*kchan
= trace_kernel_get_channel_by_name(
1995 DEFAULT_CHANNEL_NAME
, session
->kernel_session
);
1996 /* Created previously, this should NOT fail. */
1997 LTTNG_ASSERT(kchan
);
1998 kernel_destroy_channel(kchan
);
2001 if (chan_ust_created
) {
2002 struct ltt_ust_channel
*uchan
= trace_ust_find_channel_by_name(
2003 session
->ust_session
->domain_global
.channels
, DEFAULT_CHANNEL_NAME
);
2004 /* Created previously, this should NOT fail. */
2005 LTTNG_ASSERT(uchan
);
2006 /* Remove from the channel list of the session. */
2007 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
, uchan
);
2008 trace_ust_destroy_channel(uchan
);
2014 static inline bool name_starts_with(const char *name
, const char *prefix
)
2016 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2018 return !strncmp(name
, prefix
, max_cmp_len
);
2021 /* Perform userspace-specific event name validation */
2022 static int validate_ust_event_name(const char *name
)
2032 * Check name against all internal UST event component namespaces used
2035 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2036 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2037 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2046 * Internal version of cmd_enable_event() with a supplemental
2047 * "internal_event" flag which is used to enable internal events which should
2048 * be hidden from clients. Such events are used in the agent implementation to
2049 * enable the events through which all "agent" events are funeled.
2051 static int _cmd_enable_event(struct ltt_session
*session
,
2052 const struct lttng_domain
*domain
,
2054 struct lttng_event
*event
,
2055 char *filter_expression
,
2056 struct lttng_bytecode
*filter
,
2057 struct lttng_event_exclusion
*exclusion
,
2059 bool internal_event
)
2061 int ret
= 0, channel_created
= 0;
2062 struct lttng_channel
*attr
= nullptr;
2064 LTTNG_ASSERT(session
);
2065 LTTNG_ASSERT(event
);
2066 LTTNG_ASSERT(channel_name
);
2068 /* If we have a filter, we must have its filter expression */
2069 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2071 /* Normalize event name as a globbing pattern */
2072 strutils_normalize_star_glob_pattern(event
->name
);
2074 /* Normalize exclusion names as globbing patterns */
2078 for (i
= 0; i
< exclusion
->count
; i
++) {
2079 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2081 strutils_normalize_star_glob_pattern(name
);
2085 DBG("Enable event command for event \'%s\'", event
->name
);
2089 switch (domain
->type
) {
2090 case LTTNG_DOMAIN_KERNEL
:
2092 struct ltt_kernel_channel
*kchan
;
2095 * If a non-default channel has been created in the
2096 * session, explicitely require that -c chan_name needs
2099 if (session
->kernel_session
->has_non_default_channel
&& channel_name
[0] == '\0') {
2100 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2104 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2105 if (kchan
== nullptr) {
2106 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
, LTTNG_BUFFER_GLOBAL
);
2107 if (attr
== nullptr) {
2108 ret
= LTTNG_ERR_FATAL
;
2111 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2112 ret
= LTTNG_ERR_INVALID
;
2116 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2117 if (ret
!= LTTNG_OK
) {
2120 channel_created
= 1;
2123 /* Get the newly created kernel channel pointer */
2124 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2125 if (kchan
== nullptr) {
2126 /* This sould not happen... */
2127 ret
= LTTNG_ERR_FATAL
;
2131 switch (event
->type
) {
2132 case LTTNG_EVENT_ALL
:
2134 char *filter_expression_a
= nullptr;
2135 struct lttng_bytecode
*filter_a
= nullptr;
2138 * We need to duplicate filter_expression and filter,
2139 * because ownership is passed to first enable
2142 if (filter_expression
) {
2143 filter_expression_a
= strdup(filter_expression
);
2144 if (!filter_expression_a
) {
2145 ret
= LTTNG_ERR_FATAL
;
2150 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2152 free(filter_expression_a
);
2153 ret
= LTTNG_ERR_FATAL
;
2156 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2158 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2159 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2160 /* We have passed ownership */
2161 filter_expression
= nullptr;
2163 if (ret
!= LTTNG_OK
) {
2164 if (channel_created
) {
2165 /* Let's not leak a useless channel. */
2166 kernel_destroy_channel(kchan
);
2168 free(filter_expression_a
);
2172 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2173 ret
= event_kernel_enable_event(
2174 kchan
, event
, filter_expression_a
, filter_a
);
2175 /* We have passed ownership */
2176 filter_expression_a
= nullptr;
2178 if (ret
!= LTTNG_OK
) {
2183 case LTTNG_EVENT_PROBE
:
2184 case LTTNG_EVENT_USERSPACE_PROBE
:
2185 case LTTNG_EVENT_FUNCTION
:
2186 case LTTNG_EVENT_FUNCTION_ENTRY
:
2187 case LTTNG_EVENT_TRACEPOINT
:
2188 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2189 /* We have passed ownership */
2190 filter_expression
= nullptr;
2192 if (ret
!= LTTNG_OK
) {
2193 if (channel_created
) {
2194 /* Let's not leak a useless channel. */
2195 kernel_destroy_channel(kchan
);
2200 case LTTNG_EVENT_SYSCALL
:
2201 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2202 /* We have passed ownership */
2203 filter_expression
= nullptr;
2205 if (ret
!= LTTNG_OK
) {
2210 ret
= LTTNG_ERR_UNK
;
2214 kernel_wait_quiescent();
2217 case LTTNG_DOMAIN_UST
:
2219 struct ltt_ust_channel
*uchan
;
2220 struct ltt_ust_session
*usess
= session
->ust_session
;
2222 LTTNG_ASSERT(usess
);
2225 * If a non-default channel has been created in the
2226 * session, explicitely require that -c chan_name needs
2229 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2230 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2234 /* Get channel from global UST domain */
2235 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
2236 if (uchan
== nullptr) {
2237 /* Create default channel */
2238 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, usess
->buffer_type
);
2239 if (attr
== nullptr) {
2240 ret
= LTTNG_ERR_FATAL
;
2243 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2244 ret
= LTTNG_ERR_INVALID
;
2248 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2249 if (ret
!= LTTNG_OK
) {
2253 /* Get the newly created channel reference back */
2254 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2256 LTTNG_ASSERT(uchan
);
2259 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2261 * Don't allow users to add UST events to channels which
2262 * are assigned to a userspace subdomain (JUL, Log4J,
2265 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2269 if (!internal_event
) {
2271 * Ensure the event name is not reserved for internal
2274 ret
= validate_ust_event_name(event
->name
);
2276 WARN("Userspace event name %s failed validation.", event
->name
);
2277 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2282 /* At this point, the session and channel exist on the tracer */
2283 ret
= event_ust_enable_tracepoint(
2284 usess
, uchan
, event
, filter_expression
, filter
, exclusion
, internal_event
);
2285 /* We have passed ownership */
2286 filter_expression
= nullptr;
2288 exclusion
= nullptr;
2289 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2290 goto already_enabled
;
2291 } else if (ret
!= LTTNG_OK
) {
2296 case LTTNG_DOMAIN_LOG4J
:
2297 case LTTNG_DOMAIN_JUL
:
2298 case LTTNG_DOMAIN_PYTHON
:
2300 const char *default_event_name
, *default_chan_name
;
2302 struct lttng_event uevent
;
2303 struct lttng_domain tmp_dom
;
2304 struct ltt_ust_session
*usess
= session
->ust_session
;
2306 LTTNG_ASSERT(usess
);
2308 if (!agent_tracing_is_enabled()) {
2309 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2310 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2314 agt
= trace_ust_find_agent(usess
, domain
->type
);
2316 agt
= agent_create(domain
->type
);
2318 ret
= LTTNG_ERR_NOMEM
;
2321 agent_add(agt
, usess
->agents
);
2324 /* Create the default tracepoint. */
2325 memset(&uevent
, 0, sizeof(uevent
));
2326 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2327 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2328 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
2329 if (!default_event_name
) {
2330 ret
= LTTNG_ERR_FATAL
;
2333 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2334 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2337 * The domain type is changed because we are about to enable the
2338 * default channel and event for the JUL domain that are hardcoded.
2339 * This happens in the UST domain.
2341 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2342 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2344 switch (domain
->type
) {
2345 case LTTNG_DOMAIN_LOG4J
:
2346 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2348 case LTTNG_DOMAIN_JUL
:
2349 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2351 case LTTNG_DOMAIN_PYTHON
:
2352 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2355 /* The switch/case we are in makes this impossible */
2360 char *filter_expression_copy
= nullptr;
2361 struct lttng_bytecode
*filter_copy
= nullptr;
2364 const size_t filter_size
=
2365 sizeof(struct lttng_bytecode
) + filter
->len
;
2367 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2369 ret
= LTTNG_ERR_NOMEM
;
2372 memcpy(filter_copy
, filter
, filter_size
);
2374 filter_expression_copy
= strdup(filter_expression
);
2375 if (!filter_expression
) {
2376 ret
= LTTNG_ERR_NOMEM
;
2379 if (!filter_expression_copy
|| !filter_copy
) {
2380 free(filter_expression_copy
);
2386 ret
= cmd_enable_event_internal(session
,
2388 (char *) default_chan_name
,
2390 filter_expression_copy
,
2396 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2397 goto already_enabled
;
2398 } else if (ret
!= LTTNG_OK
) {
2402 /* The wild card * means that everything should be enabled. */
2403 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2404 ret
= event_agent_enable_all(usess
, agt
, event
, filter
, filter_expression
);
2406 ret
= event_agent_enable(usess
, agt
, event
, filter
, filter_expression
);
2409 filter_expression
= nullptr;
2410 if (ret
!= LTTNG_OK
) {
2417 ret
= LTTNG_ERR_UND
;
2425 free(filter_expression
);
2428 channel_attr_destroy(attr
);
2434 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2435 * We own filter, exclusion, and filter_expression.
2437 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2438 struct lttng_event
*event
,
2439 char *filter_expression
,
2440 struct lttng_event_exclusion
*exclusion
,
2441 struct lttng_bytecode
*bytecode
,
2446 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2448 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2451 * The ownership of the following parameters is transferred to
2452 * _cmd_enable_event:
2454 * - filter_expression,
2458 ret
= _cmd_enable_event(cmd_ctx
->session
,
2460 cmd_ctx
->lsm
.u
.enable
.channel_name
,
2467 filter_expression
= nullptr;
2469 exclusion
= nullptr;
2474 * Enable an event which is internal to LTTng. An internal should
2475 * never be made visible to clients and are immune to checks such as
2478 static int cmd_enable_event_internal(struct ltt_session
*session
,
2479 const struct lttng_domain
*domain
,
2481 struct lttng_event
*event
,
2482 char *filter_expression
,
2483 struct lttng_bytecode
*filter
,
2484 struct lttng_event_exclusion
*exclusion
,
2487 return _cmd_enable_event(session
,
2499 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2501 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2502 struct lttng_payload
*reply_payload
)
2504 enum lttng_error_code ret_code
;
2506 ssize_t i
, nb_events
= 0;
2507 struct lttng_event
*events
= nullptr;
2508 struct lttcomm_list_command_header reply_command_header
= {};
2509 size_t reply_command_header_offset
;
2511 assert(reply_payload
);
2513 /* Reserve space for command reply header. */
2514 reply_command_header_offset
= reply_payload
->buffer
.size
;
2515 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2516 reply_command_header_offset
+
2517 sizeof(struct lttcomm_list_command_header
));
2519 ret_code
= LTTNG_ERR_NOMEM
;
2524 case LTTNG_DOMAIN_KERNEL
:
2525 nb_events
= kernel_list_events(&events
);
2526 if (nb_events
< 0) {
2527 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2531 case LTTNG_DOMAIN_UST
:
2532 nb_events
= ust_app_list_events(&events
);
2533 if (nb_events
< 0) {
2534 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2538 case LTTNG_DOMAIN_LOG4J
:
2539 case LTTNG_DOMAIN_JUL
:
2540 case LTTNG_DOMAIN_PYTHON
:
2541 nb_events
= agent_list_events(&events
, domain
);
2542 if (nb_events
< 0) {
2543 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2548 ret_code
= LTTNG_ERR_UND
;
2552 for (i
= 0; i
< nb_events
; i
++) {
2553 ret
= lttng_event_serialize(
2554 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2556 ret_code
= LTTNG_ERR_NOMEM
;
2561 if (nb_events
> UINT32_MAX
) {
2562 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2563 ret_code
= LTTNG_ERR_OVERFLOW
;
2567 /* Update command reply header. */
2568 reply_command_header
.count
= (uint32_t) nb_events
;
2569 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2570 &reply_command_header
,
2571 sizeof(reply_command_header
));
2573 ret_code
= LTTNG_OK
;
2580 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2582 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2583 struct lttng_payload
*reply
)
2585 enum lttng_error_code ret_code
;
2587 unsigned int i
, nb_fields
;
2588 struct lttng_event_field
*fields
= nullptr;
2589 struct lttcomm_list_command_header reply_command_header
= {};
2590 size_t reply_command_header_offset
;
2594 /* Reserve space for command reply header. */
2595 reply_command_header_offset
= reply
->buffer
.size
;
2596 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2597 reply_command_header_offset
+
2598 sizeof(struct lttcomm_list_command_header
));
2600 ret_code
= LTTNG_ERR_NOMEM
;
2605 case LTTNG_DOMAIN_UST
:
2606 ret
= ust_app_list_event_fields(&fields
);
2608 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2613 case LTTNG_DOMAIN_KERNEL
:
2614 default: /* fall-through */
2615 ret_code
= LTTNG_ERR_UND
;
2621 for (i
= 0; i
< nb_fields
; i
++) {
2622 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2624 ret_code
= LTTNG_ERR_NOMEM
;
2629 if (nb_fields
> UINT32_MAX
) {
2630 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2631 ret_code
= LTTNG_ERR_OVERFLOW
;
2635 /* Update command reply header. */
2636 reply_command_header
.count
= (uint32_t) nb_fields
;
2638 memcpy(reply
->buffer
.data
+ reply_command_header_offset
,
2639 &reply_command_header
,
2640 sizeof(reply_command_header
));
2642 ret_code
= LTTNG_OK
;
2649 enum lttng_error_code
cmd_list_syscalls(struct lttng_payload
*reply_payload
)
2651 enum lttng_error_code ret_code
;
2652 ssize_t nb_events
, i
;
2654 struct lttng_event
*events
= nullptr;
2655 struct lttcomm_list_command_header reply_command_header
= {};
2656 size_t reply_command_header_offset
;
2658 assert(reply_payload
);
2660 /* Reserve space for command reply header. */
2661 reply_command_header_offset
= reply_payload
->buffer
.size
;
2662 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2663 reply_command_header_offset
+
2664 sizeof(struct lttcomm_list_command_header
));
2666 ret_code
= LTTNG_ERR_NOMEM
;
2670 nb_events
= syscall_table_list(&events
);
2671 if (nb_events
< 0) {
2672 ret_code
= (enum lttng_error_code
) - nb_events
;
2676 for (i
= 0; i
< nb_events
; i
++) {
2677 ret
= lttng_event_serialize(
2678 &events
[i
], 0, nullptr, nullptr, 0, nullptr, reply_payload
);
2680 ret_code
= LTTNG_ERR_NOMEM
;
2685 if (nb_events
> UINT32_MAX
) {
2686 ERR("Syscall count would overflow the syscall listing command's reply");
2687 ret_code
= LTTNG_ERR_OVERFLOW
;
2691 /* Update command reply header. */
2692 reply_command_header
.count
= (uint32_t) nb_events
;
2693 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2694 &reply_command_header
,
2695 sizeof(reply_command_header
));
2697 ret_code
= LTTNG_OK
;
2704 * Command LTTNG_START_TRACE processed by the client thread.
2706 * Called with session mutex held.
2708 int cmd_start_trace(struct ltt_session
*session
)
2710 enum lttng_error_code ret
;
2711 unsigned long nb_chan
= 0;
2712 struct ltt_kernel_session
*ksession
;
2713 struct ltt_ust_session
*usess
;
2714 const bool session_rotated_after_last_stop
= session
->rotated_after_last_stop
;
2715 const bool session_cleared_after_last_stop
= session
->cleared_after_last_stop
;
2717 LTTNG_ASSERT(session
);
2719 /* Ease our life a bit ;) */
2720 ksession
= session
->kernel_session
;
2721 usess
= session
->ust_session
;
2723 /* Is the session already started? */
2724 if (session
->active
) {
2725 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2726 /* Perform nothing */
2730 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2731 !session
->current_trace_chunk
) {
2733 * A rotation was launched while the session was stopped and
2734 * it has not been completed yet. It is not possible to start
2735 * the session since starting the session here would require a
2736 * rotation from "NULL" to a new trace chunk. That rotation
2737 * would overlap with the ongoing rotation, which is not
2740 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2742 ret
= LTTNG_ERR_ROTATION_PENDING
;
2747 * Starting a session without channel is useless since after that it's not
2748 * possible to enable channel thus inform the client.
2750 if (usess
&& usess
->domain_global
.channels
) {
2751 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2754 nb_chan
+= ksession
->channel_count
;
2757 ret
= LTTNG_ERR_NO_CHANNEL
;
2761 session
->active
= 1;
2762 session
->rotated_after_last_stop
= false;
2763 session
->cleared_after_last_stop
= false;
2764 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2765 if (!session
->has_been_started
) {
2766 struct lttng_trace_chunk
*trace_chunk
;
2768 DBG("Creating initial trace chunk of session \"%s\"", session
->name
);
2770 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
2772 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2775 LTTNG_ASSERT(!session
->current_trace_chunk
);
2776 ret
= (lttng_error_code
) session_set_trace_chunk(
2777 session
, trace_chunk
, nullptr);
2778 lttng_trace_chunk_put(trace_chunk
);
2780 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2784 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2787 * Rotate existing streams into the new chunk.
2788 * This is a "quiet" rotation has no client has
2789 * explicitly requested this operation.
2791 * There is also no need to wait for the rotation
2792 * to complete as it will happen immediately. No data
2793 * was produced as the session was stopped, so the
2794 * rotation should happen on reception of the command.
2796 ret
= (lttng_error_code
) cmd_rotate_session(
2797 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2798 if (ret
!= LTTNG_OK
) {
2804 /* Kernel tracing */
2805 if (ksession
!= nullptr) {
2806 DBG("Start kernel tracing session %s", session
->name
);
2807 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2808 if (ret
!= LTTNG_OK
) {
2813 /* Flag session that trace should start automatically */
2815 int int_ret
= ust_app_start_trace_all(usess
);
2818 ret
= LTTNG_ERR_UST_START_FAIL
;
2824 * Open a packet in every stream of the session to ensure that viewers
2825 * can correctly identify the boundaries of the periods during which
2826 * tracing was active for this session.
2828 ret
= session_open_packets(session
);
2829 if (ret
!= LTTNG_OK
) {
2834 * Clear the flag that indicates that a rotation was done while the
2835 * session was stopped.
2837 session
->rotated_after_last_stop
= false;
2839 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2840 int int_ret
= timer_session_rotation_schedule_timer_start(
2841 session
, session
->rotate_timer_period
);
2844 ERR("Failed to enable rotate timer");
2845 ret
= LTTNG_ERR_UNK
;
2853 if (ret
== LTTNG_OK
) {
2854 /* Flag this after a successful start. */
2855 session
->has_been_started
|= 1;
2857 session
->active
= 0;
2858 /* Restore initial state on error. */
2859 session
->rotated_after_last_stop
= session_rotated_after_last_stop
;
2860 session
->cleared_after_last_stop
= session_cleared_after_last_stop
;
2867 * Command LTTNG_STOP_TRACE processed by the client thread.
2869 int cmd_stop_trace(struct ltt_session
*session
)
2872 struct ltt_kernel_session
*ksession
;
2873 struct ltt_ust_session
*usess
;
2875 LTTNG_ASSERT(session
);
2877 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2879 ksession
= session
->kernel_session
;
2880 usess
= session
->ust_session
;
2882 /* Session is not active. Skip everything and inform the client. */
2883 if (!session
->active
) {
2884 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2888 ret
= stop_kernel_session(ksession
);
2889 if (ret
!= LTTNG_OK
) {
2893 if (usess
&& usess
->active
) {
2894 ret
= ust_app_stop_trace_all(usess
);
2896 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2901 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2902 /* Flag inactive after a successful stop. */
2903 session
->active
= 0;
2911 * Set the base_path of the session only if subdir of a control uris is set.
2912 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2915 set_session_base_path_from_uris(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2920 for (i
= 0; i
< nb_uri
; i
++) {
2921 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
|| uris
[i
].subdir
[0] == '\0') {
2922 /* Not interested in these URIs */
2926 if (session
->base_path
!= nullptr) {
2927 free(session
->base_path
);
2928 session
->base_path
= nullptr;
2931 /* Set session base_path */
2932 session
->base_path
= strdup(uris
[i
].subdir
);
2933 if (!session
->base_path
) {
2934 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2937 ret
= LTTNG_ERR_NOMEM
;
2940 DBG2("Setting base path \"%s\" for session \"%s\"",
2950 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2952 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2955 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2956 struct ltt_ust_session
*usess
= session
->ust_session
;
2958 LTTNG_ASSERT(session
);
2960 LTTNG_ASSERT(nb_uri
> 0);
2962 /* Can't set consumer URI if the session is active. */
2963 if (session
->active
) {
2964 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2969 * Set the session base path if any. This is done inside
2970 * cmd_set_consumer_uri to preserve backward compatibility of the
2971 * previous session creation api vs the session descriptor api.
2973 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2974 if (ret
!= LTTNG_OK
) {
2978 /* Set the "global" consumer URIs */
2979 for (i
= 0; i
< nb_uri
; i
++) {
2980 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
], LTTNG_DOMAIN_NONE
);
2981 if (ret
!= LTTNG_OK
) {
2986 /* Set UST session URIs */
2987 if (session
->ust_session
) {
2988 for (i
= 0; i
< nb_uri
; i
++) {
2989 ret
= add_uri_to_consumer(session
,
2990 session
->ust_session
->consumer
,
2993 if (ret
!= LTTNG_OK
) {
2999 /* Set kernel session URIs */
3000 if (session
->kernel_session
) {
3001 for (i
= 0; i
< nb_uri
; i
++) {
3002 ret
= add_uri_to_consumer(session
,
3003 session
->kernel_session
->consumer
,
3005 LTTNG_DOMAIN_KERNEL
);
3006 if (ret
!= LTTNG_OK
) {
3013 * Make sure to set the session in output mode after we set URI since a
3014 * session can be created without URL (thus flagged in no output mode).
3016 session
->output_traces
= 1;
3018 ksess
->output_traces
= 1;
3022 usess
->output_traces
= 1;
3032 static enum lttng_error_code
3033 set_session_output_from_descriptor(struct ltt_session
*session
,
3034 const struct lttng_session_descriptor
*descriptor
)
3037 enum lttng_error_code ret_code
= LTTNG_OK
;
3038 enum lttng_session_descriptor_type session_type
=
3039 lttng_session_descriptor_get_type(descriptor
);
3040 enum lttng_session_descriptor_output_type output_type
=
3041 lttng_session_descriptor_get_output_type(descriptor
);
3042 struct lttng_uri uris
[2] = {};
3043 size_t uri_count
= 0;
3045 switch (output_type
) {
3046 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3048 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3049 lttng_session_descriptor_get_local_output_uri(descriptor
, &uris
[0]);
3052 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3053 lttng_session_descriptor_get_network_output_uris(descriptor
, &uris
[0], &uris
[1]);
3057 ret_code
= LTTNG_ERR_INVALID
;
3061 switch (session_type
) {
3062 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3064 struct snapshot_output
*new_output
= nullptr;
3066 new_output
= snapshot_output_alloc();
3068 ret_code
= LTTNG_ERR_NOMEM
;
3072 ret
= snapshot_output_init_with_uri(session
,
3073 DEFAULT_SNAPSHOT_MAX_SIZE
,
3079 &session
->snapshot
);
3081 ret_code
= (ret
== -ENOMEM
) ? LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3082 snapshot_output_destroy(new_output
);
3085 snapshot_add_output(&session
->snapshot
, new_output
);
3088 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3089 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3091 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3095 ret_code
= LTTNG_ERR_INVALID
;
3102 static enum lttng_error_code
3103 cmd_create_session_from_descriptor(struct lttng_session_descriptor
*descriptor
,
3104 const lttng_sock_cred
*creds
,
3105 const char *home_path
)
3108 enum lttng_error_code ret_code
;
3109 const char *session_name
;
3110 struct ltt_session
*new_session
= nullptr;
3111 enum lttng_session_descriptor_status descriptor_status
;
3113 session_lock_list();
3115 if (*home_path
!= '/') {
3116 ERR("Home path provided by client is not absolute");
3117 ret_code
= LTTNG_ERR_INVALID
;
3122 descriptor_status
= lttng_session_descriptor_get_session_name(descriptor
, &session_name
);
3123 switch (descriptor_status
) {
3124 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3126 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3127 session_name
= nullptr;
3130 ret_code
= LTTNG_ERR_INVALID
;
3134 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, &new_session
);
3135 if (ret_code
!= LTTNG_OK
) {
3139 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3144 if (ret_code
!= LTTNG_OK
) {
3148 /* Announce the session's destruction to the notification thread when it is destroyed. */
3149 ret
= session_add_destroy_notifier(
3151 [](const struct ltt_session
*session
, void *user_data
__attribute__((unused
))) {
3152 (void) notification_thread_command_remove_session(
3153 the_notification_thread_handle
, session
->id
);
3157 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3159 ret
= LTTNG_ERR_NOMEM
;
3163 if (!session_name
) {
3164 ret
= lttng_session_descriptor_set_session_name(descriptor
, new_session
->name
);
3166 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3171 if (!lttng_session_descriptor_is_output_destination_initialized(descriptor
)) {
3173 * Only include the session's creation time in the output
3174 * destination if the name of the session itself was
3175 * not auto-generated.
3177 ret_code
= lttng_session_descriptor_set_default_output(
3179 session_name
? &new_session
->creation_time
: nullptr,
3181 if (ret_code
!= LTTNG_OK
) {
3185 new_session
->has_user_specified_directory
=
3186 lttng_session_descriptor_has_output_directory(descriptor
);
3189 switch (lttng_session_descriptor_get_type(descriptor
)) {
3190 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3191 new_session
->snapshot_mode
= 1;
3193 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3194 new_session
->live_timer
=
3195 lttng_session_descriptor_live_get_timer_interval(descriptor
);
3201 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3202 if (ret_code
!= LTTNG_OK
) {
3205 new_session
->consumer
->enabled
= 1;
3206 ret_code
= LTTNG_OK
;
3208 /* Release reference provided by the session_create function. */
3209 session_put(new_session
);
3210 if (ret_code
!= LTTNG_OK
&& new_session
) {
3211 /* Release the global reference on error. */
3212 session_destroy(new_session
);
3214 session_unlock_list();
3218 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
,
3220 struct lttng_session_descriptor
**return_descriptor
)
3223 size_t payload_size
;
3224 struct lttng_dynamic_buffer payload
;
3225 struct lttng_buffer_view home_dir_view
;
3226 struct lttng_buffer_view session_descriptor_view
;
3227 struct lttng_session_descriptor
*session_descriptor
= nullptr;
3228 enum lttng_error_code ret_code
;
3230 lttng_dynamic_buffer_init(&payload
);
3231 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>= LTTNG_PATH_MAX
) {
3232 ret_code
= LTTNG_ERR_INVALID
;
3235 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3236 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3237 ret_code
= LTTNG_ERR_INVALID
;
3241 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3242 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3243 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3245 ret_code
= LTTNG_ERR_NOMEM
;
3249 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3251 ERR("Reception of session descriptor failed, aborting.");
3252 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3256 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3257 &payload
, 0, cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3258 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3259 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3260 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3261 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3265 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3267 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3268 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3269 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3270 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3271 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3275 ret
= lttng_session_descriptor_create_from_buffer(&session_descriptor_view
,
3276 &session_descriptor
);
3278 ERR("Failed to create session descriptor from payload of \"create session\" command");
3279 ret_code
= LTTNG_ERR_INVALID
;
3284 * Sets the descriptor's auto-generated properties (name, output) if
3287 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3289 home_dir_view
.size
? home_dir_view
.data
:
3291 if (ret_code
!= LTTNG_OK
) {
3295 ret_code
= LTTNG_OK
;
3296 *return_descriptor
= session_descriptor
;
3297 session_descriptor
= nullptr;
3299 lttng_dynamic_buffer_reset(&payload
);
3300 lttng_session_descriptor_destroy(session_descriptor
);
3304 static void cmd_destroy_session_reply(const struct ltt_session
*session
, void *_reply_context
)
3308 const struct cmd_destroy_session_reply_context
*reply_context
=
3309 (cmd_destroy_session_reply_context
*) _reply_context
;
3310 struct lttng_dynamic_buffer payload
;
3311 struct lttcomm_session_destroy_command_header cmd_header
;
3312 struct lttng_trace_archive_location
*location
= nullptr;
3313 struct lttcomm_lttng_msg llm
= {
3314 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION
,
3315 .ret_code
= reply_context
->destruction_status
,
3317 .cmd_header_size
= sizeof(struct lttcomm_session_destroy_command_header
),
3321 size_t payload_size_before_location
;
3323 lttng_dynamic_buffer_init(&payload
);
3325 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3327 ERR("Failed to append session destruction message");
3331 cmd_header
.rotation_state
= (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3332 session
->rotation_state
:
3333 LTTNG_ROTATION_STATE_NO_ROTATION
);
3334 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
, sizeof(cmd_header
));
3336 ERR("Failed to append session destruction command header");
3340 if (!reply_context
->implicit_rotation_on_destroy
) {
3341 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3345 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3346 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3351 location
= session_get_trace_archive_location(session
);
3353 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3358 payload_size_before_location
= payload
.size
;
3359 comm_ret
= lttng_trace_archive_location_serialize(location
, &payload
);
3360 lttng_trace_archive_location_put(location
);
3362 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3366 /* Update the message to indicate the location's length. */
3367 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3368 payload
.size
- payload_size_before_location
;
3370 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
, payload
.data
, payload
.size
);
3371 if (comm_ret
!= (ssize_t
) payload
.size
) {
3372 ERR("Failed to send result of the destruction of session \"%s\" to client",
3376 ret
= close(reply_context
->reply_sock_fd
);
3378 PERROR("Failed to close client socket in deferred session destroy reply");
3380 lttng_dynamic_buffer_reset(&payload
);
3381 free(_reply_context
);
3385 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3387 * Called with session lock held.
3389 int cmd_destroy_session(struct ltt_session
*session
,
3390 struct notification_thread_handle
*notification_thread_handle
,
3394 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3395 struct cmd_destroy_session_reply_context
*reply_context
= nullptr;
3398 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3399 if (!reply_context
) {
3400 ret
= LTTNG_ERR_NOMEM
;
3404 reply_context
->reply_sock_fd
= *sock_fd
;
3408 LTTNG_ASSERT(session
);
3410 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3411 if (session
->active
) {
3412 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3414 ret
= cmd_stop_trace(session
);
3415 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3416 /* Carry on with the destruction of the session. */
3417 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3419 lttng_strerror(-ret
));
3420 destruction_last_error
= (lttng_error_code
) ret
;
3424 if (session
->rotation_schedule_timer_enabled
) {
3425 if (timer_session_rotation_schedule_timer_stop(session
)) {
3426 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3428 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3432 if (session
->rotate_size
) {
3433 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3434 session
->rotate_size
= 0;
3437 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3439 * Perform a last rotation on destruction if rotations have
3440 * occurred during the session's lifetime.
3442 ret
= cmd_rotate_session(
3443 session
, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3444 if (ret
!= LTTNG_OK
) {
3445 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3447 lttng_strerror(-ret
));
3448 destruction_last_error
= (lttng_error_code
) -ret
;
3450 if (reply_context
) {
3451 reply_context
->implicit_rotation_on_destroy
= true;
3453 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3455 * The user has not triggered a session rotation. However, to
3456 * ensure all data has been consumed, the session is rotated
3457 * to a 'null' trace chunk before it is destroyed.
3459 * This is a "quiet" rotation meaning that no notification is
3460 * emitted and no renaming of the current trace chunk takes
3463 ret
= cmd_rotate_session(
3464 session
, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3466 * Rotation operations may not be supported by the kernel
3467 * tracer. Hence, do not consider this implicit rotation as
3468 * a session destruction error. The library has already stopped
3469 * the session and waited for pending data; there is nothing
3470 * left to do but complete the destruction of the session.
3472 if (ret
!= LTTNG_OK
&& ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3473 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3475 lttng_strerror(ret
));
3476 destruction_last_error
= (lttng_error_code
) -ret
;
3480 if (session
->shm_path
[0]) {
3482 * When a session is created with an explicit shm_path,
3483 * the consumer daemon will create its shared memory files
3484 * at that location and will *not* unlink them. This is normal
3485 * as the intention of that feature is to make it possible
3486 * to retrieve the content of those files should a crash occur.
3488 * To ensure the content of those files can be used, the
3489 * sessiond daemon will replicate the content of the metadata
3490 * cache in a metadata file.
3492 * On clean-up, it is expected that the consumer daemon will
3493 * unlink the shared memory files and that the session daemon
3494 * will unlink the metadata file. Then, the session's directory
3495 * in the shm path can be removed.
3497 * Unfortunately, a flaw in the design of the sessiond's and
3498 * consumerd's tear down of channels makes it impossible to
3499 * determine when the sessiond _and_ the consumerd have both
3500 * destroyed their representation of a channel. For one, the
3501 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3502 * callbacks in both daemons.
3504 * However, it is also impossible for the sessiond to know when
3505 * the consumer daemon is done destroying its channel(s) since
3506 * it occurs as a reaction to the closing of the channel's file
3507 * descriptor. There is no resulting communication initiated
3508 * from the consumerd to the sessiond to confirm that the
3509 * operation is completed (and was successful).
3511 * Until this is all fixed, the session daemon checks for the
3512 * removal of the session's shm path which makes it possible
3513 * to safely advertise a session as having been destroyed.
3515 * Prior to this fix, it was not possible to reliably save
3516 * a session making use of the --shm-path option, destroy it,
3517 * and load it again. This is because the creation of the
3518 * session would fail upon seeing the session's shm path
3519 * already in existence.
3521 * Note that none of the error paths in the check for the
3522 * directory's existence return an error. This is normal
3523 * as there isn't much that can be done. The session will
3524 * be destroyed properly, except that we can't offer the
3525 * guarantee that the same session can be re-created.
3527 current_completion_handler
= &destroy_completion_handler
.handler
;
3528 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3530 sizeof(destroy_completion_handler
.shm_path
));
3535 * The session is destroyed. However, note that the command context
3536 * still holds a reference to the session, thus delaying its destruction
3537 * _at least_ up to the point when that reference is released.
3539 session_destroy(session
);
3540 if (reply_context
) {
3541 reply_context
->destruction_status
= destruction_last_error
;
3542 ret
= session_add_destroy_notifier(
3543 session
, cmd_destroy_session_reply
, (void *) reply_context
);
3545 ret
= LTTNG_ERR_FATAL
;
3557 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3559 int cmd_register_consumer(struct ltt_session
*session
,
3560 enum lttng_domain_type domain
,
3561 const char *sock_path
,
3562 struct consumer_data
*cdata
)
3565 struct consumer_socket
*socket
= nullptr;
3567 LTTNG_ASSERT(session
);
3568 LTTNG_ASSERT(cdata
);
3569 LTTNG_ASSERT(sock_path
);
3572 case LTTNG_DOMAIN_KERNEL
:
3574 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3576 LTTNG_ASSERT(ksess
);
3578 /* Can't register a consumer if there is already one */
3579 if (ksess
->consumer_fds_sent
!= 0) {
3580 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3584 sock
= lttcomm_connect_unix_sock(sock_path
);
3586 ret
= LTTNG_ERR_CONNECT_FAIL
;
3589 cdata
->cmd_sock
= sock
;
3591 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3592 if (socket
== nullptr) {
3595 PERROR("close register consumer");
3597 cdata
->cmd_sock
= -1;
3598 ret
= LTTNG_ERR_FATAL
;
3602 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3603 if (socket
->lock
== nullptr) {
3604 PERROR("zmalloc pthread mutex");
3605 ret
= LTTNG_ERR_FATAL
;
3609 pthread_mutex_init(socket
->lock
, nullptr);
3610 socket
->registered
= 1;
3613 consumer_add_socket(socket
, ksess
->consumer
);
3616 pthread_mutex_lock(&cdata
->pid_mutex
);
3618 pthread_mutex_unlock(&cdata
->pid_mutex
);
3623 /* TODO: Userspace tracing */
3624 ret
= LTTNG_ERR_UND
;
3632 consumer_destroy_socket(socket
);
3638 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3640 ssize_t
cmd_list_domains(struct ltt_session
*session
, struct lttng_domain
**domains
)
3645 struct lttng_ht_iter iter
;
3647 if (session
->kernel_session
!= nullptr) {
3648 DBG3("Listing domains found kernel domain");
3652 if (session
->ust_session
!= nullptr) {
3653 DBG3("Listing domains found UST global domain");
3657 cds_lfht_for_each_entry (
3658 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3659 if (agt
->being_used
) {
3670 *domains
= calloc
<lttng_domain
>(nb_dom
);
3671 if (*domains
== nullptr) {
3672 ret
= LTTNG_ERR_FATAL
;
3676 if (session
->kernel_session
!= nullptr) {
3677 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3679 /* Kernel session buffer type is always GLOBAL */
3680 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3685 if (session
->ust_session
!= nullptr) {
3686 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3687 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3691 cds_lfht_for_each_entry (
3692 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3693 if (agt
->being_used
) {
3694 (*domains
)[index
].type
= agt
->domain
;
3695 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3705 /* Return negative value to differentiate return code */
3710 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3712 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3713 struct ltt_session
*session
,
3714 struct lttng_payload
*payload
)
3718 struct lttcomm_list_command_header cmd_header
= {};
3719 size_t cmd_header_offset
;
3720 enum lttng_error_code ret_code
;
3725 DBG("Listing channels for session %s", session
->name
);
3727 cmd_header_offset
= payload
->buffer
.size
;
3729 /* Reserve space for command reply header. */
3730 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3731 cmd_header_offset
+ sizeof(cmd_header
));
3733 ret_code
= LTTNG_ERR_NOMEM
;
3738 case LTTNG_DOMAIN_KERNEL
:
3740 /* Kernel channels */
3741 struct ltt_kernel_channel
*kchan
;
3742 if (session
->kernel_session
!= nullptr) {
3743 cds_list_for_each_entry (
3744 kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
3745 uint64_t discarded_events
, lost_packets
;
3746 struct lttng_channel_extended
*extended
;
3748 extended
= (struct lttng_channel_extended
*)
3749 kchan
->channel
->attr
.extended
.ptr
;
3751 ret
= get_kernel_runtime_stats(
3752 session
, kchan
, &discarded_events
, &lost_packets
);
3754 ret_code
= LTTNG_ERR_UNK
;
3759 * Update the discarded_events and lost_packets
3760 * count for the channel
3762 extended
->discarded_events
= discarded_events
;
3763 extended
->lost_packets
= lost_packets
;
3765 ret
= lttng_channel_serialize(kchan
->channel
, &payload
->buffer
);
3767 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3768 kchan
->channel
->name
);
3769 ret_code
= LTTNG_ERR_UNK
;
3778 case LTTNG_DOMAIN_UST
:
3780 struct lttng_ht_iter iter
;
3781 struct ltt_ust_channel
*uchan
;
3784 cds_lfht_for_each_entry (session
->ust_session
->domain_global
.channels
->ht
,
3788 uint64_t discarded_events
= 0, lost_packets
= 0;
3789 struct lttng_channel
*channel
= nullptr;
3790 struct lttng_channel_extended
*extended
;
3792 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3794 ret_code
= LTTNG_ERR_NOMEM
;
3798 extended
= (struct lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
3800 ret
= get_ust_runtime_stats(
3801 session
, uchan
, &discarded_events
, &lost_packets
);
3803 lttng_channel_destroy(channel
);
3804 ret_code
= LTTNG_ERR_UNK
;
3808 extended
->discarded_events
= discarded_events
;
3809 extended
->lost_packets
= lost_packets
;
3811 ret
= lttng_channel_serialize(channel
, &payload
->buffer
);
3813 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3815 lttng_channel_destroy(channel
);
3816 ret_code
= LTTNG_ERR_UNK
;
3820 lttng_channel_destroy(channel
);
3830 if (i
> UINT32_MAX
) {
3831 ERR("Channel count would overflow the channel listing command's reply");
3832 ret_code
= LTTNG_ERR_OVERFLOW
;
3836 /* Update command reply header. */
3837 cmd_header
.count
= (uint32_t) i
;
3838 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
, sizeof(cmd_header
));
3839 ret_code
= LTTNG_OK
;
3846 * Command LTTNG_LIST_EVENTS processed by the client thread.
3848 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3849 struct ltt_session
*session
,
3851 struct lttng_payload
*reply_payload
)
3853 int buffer_resize_ret
;
3854 enum lttng_error_code ret_code
= LTTNG_OK
;
3855 struct lttcomm_list_command_header reply_command_header
= {};
3856 size_t reply_command_header_offset
;
3857 unsigned int nb_events
= 0;
3859 assert(reply_payload
);
3861 /* Reserve space for command reply header. */
3862 reply_command_header_offset
= reply_payload
->buffer
.size
;
3863 buffer_resize_ret
= lttng_dynamic_buffer_set_size(
3864 &reply_payload
->buffer
,
3865 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
3866 if (buffer_resize_ret
) {
3867 ret_code
= LTTNG_ERR_NOMEM
;
3872 case LTTNG_DOMAIN_KERNEL
:
3873 if (session
->kernel_session
!= nullptr) {
3874 ret_code
= list_lttng_kernel_events(
3875 channel_name
, session
->kernel_session
, reply_payload
, &nb_events
);
3879 case LTTNG_DOMAIN_UST
:
3881 if (session
->ust_session
!= nullptr) {
3883 list_lttng_ust_global_events(channel_name
,
3884 &session
->ust_session
->domain_global
,
3891 case LTTNG_DOMAIN_LOG4J
:
3892 case LTTNG_DOMAIN_JUL
:
3893 case LTTNG_DOMAIN_PYTHON
:
3894 if (session
->ust_session
) {
3895 struct lttng_ht_iter iter
;
3899 cds_lfht_for_each_entry (
3900 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3901 if (agt
->domain
== domain
) {
3902 ret_code
= list_lttng_agent_events(
3903 agt
, reply_payload
, &nb_events
);
3912 ret_code
= LTTNG_ERR_UND
;
3916 if (nb_events
> UINT32_MAX
) {
3917 ret_code
= LTTNG_ERR_OVERFLOW
;
3921 /* Update command reply header. */
3922 reply_command_header
.count
= (uint32_t) nb_events
;
3923 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
3924 &reply_command_header
,
3925 sizeof(reply_command_header
));
3932 * Using the session list, filled a lttng_session array to send back to the
3933 * client for session listing.
3935 * The session list lock MUST be acquired before calling this function. Use
3936 * session_lock_list() and session_unlock_list().
3938 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3939 size_t session_count
,
3945 struct ltt_session
*session
;
3946 struct ltt_session_list
*list
= session_get_list();
3947 struct lttng_session_extended
*extended
= (typeof(extended
)) (&sessions
[session_count
]);
3949 DBG("Getting all available session for UID %d GID %d", uid
, gid
);
3951 * Iterate over session list and append data after the control struct in
3954 cds_list_for_each_entry (session
, &list
->head
, list
) {
3955 if (!session_get(session
)) {
3959 * Only list the sessions the user can control.
3961 if (!session_access_ok(session
, uid
) || session
->destroyed
) {
3962 session_put(session
);
3966 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3967 struct ltt_ust_session
*usess
= session
->ust_session
;
3969 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3970 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3971 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3972 ret
= build_network_session_path(
3973 sessions
[i
].path
, sizeof(sessions
[i
].path
), session
);
3975 ret
= snprintf(sessions
[i
].path
,
3976 sizeof(sessions
[i
].path
),
3978 session
->consumer
->dst
.session_root_path
);
3981 PERROR("snprintf session path");
3982 session_put(session
);
3986 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3987 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3988 sessions
[i
].enabled
= session
->active
;
3989 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3990 sessions
[i
].live_timer_interval
= session
->live_timer
;
3991 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3992 extended
[i
].creation_time
.is_set
= 1;
3994 session_put(session
);
3999 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4000 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4002 int cmd_data_pending(struct ltt_session
*session
)
4005 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4006 struct ltt_ust_session
*usess
= session
->ust_session
;
4008 LTTNG_ASSERT(session
);
4010 DBG("Data pending for session %s", session
->name
);
4012 /* Session MUST be stopped to ask for data availability. */
4013 if (session
->active
) {
4014 ret
= LTTNG_ERR_SESSION_STARTED
;
4018 * If stopped, just make sure we've started before else the above call
4019 * will always send that there is data pending.
4021 * The consumer assumes that when the data pending command is received,
4022 * the trace has been started before or else no output data is written
4023 * by the streams which is a condition for data pending. So, this is
4024 * *VERY* important that we don't ask the consumer before a start
4027 if (!session
->has_been_started
) {
4033 /* A rotation is still pending, we have to wait. */
4034 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4035 DBG("Rotate still pending for session %s", session
->name
);
4040 if (ksess
&& ksess
->consumer
) {
4041 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4043 /* Data is still being extracted for the kernel. */
4048 if (usess
&& usess
->consumer
) {
4049 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4051 /* Data is still being extracted for the kernel. */
4056 /* Data is ready to be read by a viewer */
4064 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4066 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4068 int cmd_snapshot_add_output(struct ltt_session
*session
,
4069 const struct lttng_snapshot_output
*output
,
4073 struct snapshot_output
*new_output
;
4075 LTTNG_ASSERT(session
);
4076 LTTNG_ASSERT(output
);
4078 DBG("Cmd snapshot add output for session %s", session
->name
);
4081 * Can't create an output if the session is not set in no-output mode.
4083 if (session
->output_traces
) {
4084 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4088 if (session
->has_non_mmap_channel
) {
4089 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4093 /* Only one output is allowed until we have the "tee" feature. */
4094 if (session
->snapshot
.nb_output
== 1) {
4095 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4099 new_output
= snapshot_output_alloc();
4101 ret
= LTTNG_ERR_NOMEM
;
4105 ret
= snapshot_output_init(session
,
4112 &session
->snapshot
);
4114 if (ret
== -ENOMEM
) {
4115 ret
= LTTNG_ERR_NOMEM
;
4117 ret
= LTTNG_ERR_INVALID
;
4123 snapshot_add_output(&session
->snapshot
, new_output
);
4125 *id
= new_output
->id
;
4132 snapshot_output_destroy(new_output
);
4138 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4140 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4142 int cmd_snapshot_del_output(struct ltt_session
*session
, const struct lttng_snapshot_output
*output
)
4145 struct snapshot_output
*sout
= nullptr;
4147 LTTNG_ASSERT(session
);
4148 LTTNG_ASSERT(output
);
4153 * Permission denied to create an output if the session is not
4154 * set in no output mode.
4156 if (session
->output_traces
) {
4157 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4162 DBG("Cmd snapshot del output id %" PRIu32
" for session %s",
4165 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4166 } else if (*output
->name
!= '\0') {
4167 DBG("Cmd snapshot del output name %s for session %s", output
->name
, session
->name
);
4168 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4171 ret
= LTTNG_ERR_INVALID
;
4175 snapshot_delete_output(&session
->snapshot
, sout
);
4176 snapshot_output_destroy(sout
);
4185 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4187 * If no output is available, outputs is untouched and 0 is returned.
4189 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4191 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4192 struct lttng_snapshot_output
**outputs
)
4195 struct lttng_snapshot_output
*list
= nullptr;
4196 struct lttng_ht_iter iter
;
4197 struct snapshot_output
*output
;
4199 LTTNG_ASSERT(session
);
4200 LTTNG_ASSERT(outputs
);
4202 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4205 * Permission denied to create an output if the session is not
4206 * set in no output mode.
4208 if (session
->output_traces
) {
4209 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4213 if (session
->snapshot
.nb_output
== 0) {
4218 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4220 ret
= -LTTNG_ERR_NOMEM
;
4224 /* Copy list from session to the new list object. */
4226 cds_lfht_for_each_entry (session
->snapshot
.output_ht
->ht
, &iter
.iter
, output
, node
.node
) {
4227 LTTNG_ASSERT(output
->consumer
);
4228 list
[idx
].id
= output
->id
;
4229 list
[idx
].max_size
= output
->max_size
;
4230 if (lttng_strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
))) {
4231 ret
= -LTTNG_ERR_INVALID
;
4234 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4235 if (lttng_strncpy(list
[idx
].ctrl_url
,
4236 output
->consumer
->dst
.session_root_path
,
4237 sizeof(list
[idx
].ctrl_url
))) {
4238 ret
= -LTTNG_ERR_INVALID
;
4243 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4245 sizeof(list
[idx
].ctrl_url
));
4247 ret
= -LTTNG_ERR_NOMEM
;
4252 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4254 sizeof(list
[idx
].data_url
));
4256 ret
= -LTTNG_ERR_NOMEM
;
4265 ret
= session
->snapshot
.nb_output
;
4274 * Check if we can regenerate the metadata for this session.
4275 * Only kernel, UST per-uid and non-live sessions are supported.
4277 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4279 static int check_regenerate_metadata_support(struct ltt_session
*session
)
4283 LTTNG_ASSERT(session
);
4285 if (session
->live_timer
!= 0) {
4286 ret
= LTTNG_ERR_LIVE_SESSION
;
4289 if (!session
->active
) {
4290 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4293 if (session
->ust_session
) {
4294 switch (session
->ust_session
->buffer_type
) {
4295 case LTTNG_BUFFER_PER_UID
:
4297 case LTTNG_BUFFER_PER_PID
:
4298 ret
= LTTNG_ERR_PER_PID_SESSION
;
4302 ret
= LTTNG_ERR_UNK
;
4306 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4307 session
->consumer
->relay_minor_version
< 8) {
4308 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4318 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4320 * Ask the consumer to truncate the existing metadata file(s) and
4321 * then regenerate the metadata. Live and per-pid sessions are not
4322 * supported and return an error.
4324 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4326 int cmd_regenerate_metadata(struct ltt_session
*session
)
4330 LTTNG_ASSERT(session
);
4332 ret
= check_regenerate_metadata_support(session
);
4337 if (session
->kernel_session
) {
4338 ret
= kernctl_session_regenerate_metadata(session
->kernel_session
->fd
);
4340 ERR("Failed to regenerate the kernel metadata");
4345 if (session
->ust_session
) {
4346 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4348 ERR("Failed to regenerate the UST metadata");
4352 DBG("Cmd metadata regenerate for session %s", session
->name
);
4360 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4362 * Ask the tracer to regenerate a new statedump.
4364 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4366 int cmd_regenerate_statedump(struct ltt_session
*session
)
4370 LTTNG_ASSERT(session
);
4372 if (!session
->active
) {
4373 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4377 if (session
->kernel_session
) {
4378 ret
= kernctl_session_regenerate_statedump(session
->kernel_session
->fd
);
4380 * Currently, the statedump in kernel can only fail if out
4384 if (ret
== -ENOMEM
) {
4385 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4387 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4389 ERR("Failed to regenerate the kernel statedump");
4394 if (session
->ust_session
) {
4395 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4397 * Currently, the statedump in UST always returns 0.
4400 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4401 ERR("Failed to regenerate the UST statedump");
4405 DBG("Cmd regenerate statedump for session %s", session
->name
);
4412 static enum lttng_error_code
4413 synchronize_tracer_notifier_register(struct notification_thread_handle
*notification_thread
,
4414 struct lttng_trigger
*trigger
,
4415 const struct lttng_credentials
*cmd_creds
)
4417 enum lttng_error_code ret_code
;
4418 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4419 const char *trigger_name
;
4420 uid_t trigger_owner
;
4421 enum lttng_trigger_status trigger_status
;
4422 const enum lttng_domain_type trigger_domain
=
4423 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4425 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4426 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4428 LTTNG_ASSERT(condition
);
4429 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4430 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4432 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4433 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4435 session_lock_list();
4436 switch (trigger_domain
) {
4437 case LTTNG_DOMAIN_KERNEL
:
4439 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4440 if (ret_code
!= LTTNG_OK
) {
4441 enum lttng_error_code notif_thread_unregister_ret
;
4443 notif_thread_unregister_ret
=
4444 notification_thread_command_unregister_trigger(notification_thread
,
4447 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4448 /* Return the original error code. */
4449 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4451 (int) trigger_owner
,
4457 case LTTNG_DOMAIN_UST
:
4458 ust_app_global_update_all_event_notifier_rules();
4460 case LTTNG_DOMAIN_JUL
:
4461 case LTTNG_DOMAIN_LOG4J
:
4462 case LTTNG_DOMAIN_PYTHON
:
4464 /* Agent domains. */
4465 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4468 agt
= agent_create(trigger_domain
);
4470 ret_code
= LTTNG_ERR_NOMEM
;
4471 goto end_unlock_session_list
;
4474 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4477 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4478 if (ret_code
!= LTTNG_OK
) {
4479 goto end_unlock_session_list
;
4484 case LTTNG_DOMAIN_NONE
:
4489 ret_code
= LTTNG_OK
;
4490 end_unlock_session_list
:
4491 session_unlock_list();
4495 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4496 struct lttng_trigger
*trigger
,
4497 bool is_trigger_anonymous
,
4498 struct notification_thread_handle
*notification_thread
,
4499 struct lttng_trigger
**return_trigger
)
4501 enum lttng_error_code ret_code
;
4502 const char *trigger_name
;
4503 uid_t trigger_owner
;
4504 enum lttng_trigger_status trigger_status
;
4506 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4507 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4509 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4510 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4512 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4514 (int) trigger_owner
,
4515 (int) lttng_credentials_get_uid(cmd_creds
));
4518 * Validate the trigger credentials against the command credentials.
4519 * Only the root user can register a trigger with non-matching
4522 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4523 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4524 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4526 (int) trigger_owner
,
4527 (int) lttng_credentials_get_uid(cmd_creds
));
4528 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4534 * The bytecode generation also serves as a validation step for the
4535 * bytecode expressions.
4537 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4538 if (ret_code
!= LTTNG_OK
) {
4539 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4541 (int) trigger_owner
,
4547 * A reference to the trigger is acquired by the notification thread.
4548 * It is safe to return the same trigger to the caller since it the
4549 * other user holds a reference.
4551 * The trigger is modified during the execution of the
4552 * "register trigger" command. However, by the time the command returns,
4553 * it is safe to use without any locking as its properties are
4556 ret_code
= notification_thread_command_register_trigger(
4557 notification_thread
, trigger
, is_trigger_anonymous
);
4558 if (ret_code
!= LTTNG_OK
) {
4559 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4561 (int) trigger_owner
,
4566 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4567 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4570 * Synchronize tracers if the trigger adds an event notifier.
4572 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4573 ret_code
= synchronize_tracer_notifier_register(
4574 notification_thread
, trigger
, cmd_creds
);
4575 if (ret_code
!= LTTNG_OK
) {
4576 ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code
));
4582 * Return an updated trigger to the client.
4584 * Since a modified version of the same trigger is returned, acquire a
4585 * reference to the trigger so the caller doesn't have to care if those
4586 * are distinct instances or not.
4588 if (ret_code
== LTTNG_OK
) {
4589 lttng_trigger_get(trigger
);
4590 *return_trigger
= trigger
;
4591 /* Ownership of trigger was transferred to caller. */
4598 static enum lttng_error_code
4599 synchronize_tracer_notifier_unregister(const struct lttng_trigger
*trigger
)
4601 enum lttng_error_code ret_code
;
4602 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4603 const enum lttng_domain_type trigger_domain
=
4604 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4606 LTTNG_ASSERT(condition
);
4607 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4608 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4610 session_lock_list();
4611 switch (trigger_domain
) {
4612 case LTTNG_DOMAIN_KERNEL
:
4613 ret_code
= kernel_unregister_event_notifier(trigger
);
4614 if (ret_code
!= LTTNG_OK
) {
4615 goto end_unlock_session_list
;
4619 case LTTNG_DOMAIN_UST
:
4620 ust_app_global_update_all_event_notifier_rules();
4622 case LTTNG_DOMAIN_JUL
:
4623 case LTTNG_DOMAIN_LOG4J
:
4624 case LTTNG_DOMAIN_PYTHON
:
4626 /* Agent domains. */
4627 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4630 * This trigger was never registered in the first place. Calling
4631 * this function under those circumstances is an internal error.
4634 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4635 if (ret_code
!= LTTNG_OK
) {
4636 goto end_unlock_session_list
;
4641 case LTTNG_DOMAIN_NONE
:
4646 ret_code
= LTTNG_OK
;
4648 end_unlock_session_list
:
4649 session_unlock_list();
4653 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4654 const struct lttng_trigger
*trigger
,
4655 struct notification_thread_handle
*notification_thread
)
4657 enum lttng_error_code ret_code
;
4658 const char *trigger_name
;
4659 uid_t trigger_owner
;
4660 enum lttng_trigger_status trigger_status
;
4661 struct lttng_trigger
*sessiond_trigger
= nullptr;
4663 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4664 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4665 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4666 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4668 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4670 (int) trigger_owner
,
4671 (int) lttng_credentials_get_uid(cmd_creds
));
4674 * Validate the trigger credentials against the command credentials.
4675 * Only the root user can unregister a trigger with non-matching
4678 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4679 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4680 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4682 (int) trigger_owner
,
4683 (int) lttng_credentials_get_uid(cmd_creds
));
4684 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4689 /* Fetch the sessiond side trigger object. */
4690 ret_code
= notification_thread_command_get_trigger(
4691 notification_thread
, trigger
, &sessiond_trigger
);
4692 if (ret_code
!= LTTNG_OK
) {
4693 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4695 (int) trigger_owner
,
4700 LTTNG_ASSERT(sessiond_trigger
);
4703 * From this point on, no matter what, consider the trigger
4706 * We set the unregistered state of the sessiond side trigger object in
4707 * the client thread since we want to minimize the possibility of the
4708 * notification thread being stalled due to a long execution of an
4709 * action that required the trigger lock.
4711 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4713 ret_code
= notification_thread_command_unregister_trigger(notification_thread
, trigger
);
4714 if (ret_code
!= LTTNG_OK
) {
4715 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4717 (int) trigger_owner
,
4723 * Synchronize tracers if the trigger removes an event notifier.
4724 * Do this even if the trigger unregistration failed to at least stop
4725 * the tracers from producing notifications associated with this
4728 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4729 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4730 if (ret_code
!= LTTNG_OK
) {
4731 ERR("Error unregistering trigger to tracer.");
4737 lttng_trigger_put(sessiond_trigger
);
4741 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4742 struct notification_thread_handle
*notification_thread
,
4743 struct lttng_triggers
**return_triggers
)
4746 enum lttng_error_code ret_code
;
4747 struct lttng_triggers
*triggers
= nullptr;
4749 /* Get the set of triggers from the notification thread. */
4750 ret_code
= notification_thread_command_list_triggers(
4751 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4752 if (ret_code
!= LTTNG_OK
) {
4756 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4758 ret_code
= LTTNG_ERR_UNK
;
4762 *return_triggers
= triggers
;
4764 ret_code
= LTTNG_OK
;
4766 lttng_triggers_destroy(triggers
);
4770 enum lttng_error_code
4771 cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4772 const struct lttng_error_query
*query
,
4773 struct lttng_error_query_results
**_results
,
4774 struct notification_thread_handle
*notification_thread
)
4776 enum lttng_error_code ret_code
;
4777 const struct lttng_trigger
*query_target_trigger
;
4778 const struct lttng_action
*query_target_action
= nullptr;
4779 struct lttng_trigger
*matching_trigger
= nullptr;
4780 const char *trigger_name
;
4781 uid_t trigger_owner
;
4782 enum lttng_trigger_status trigger_status
;
4783 struct lttng_error_query_results
*results
= nullptr;
4785 switch (lttng_error_query_get_target_type(query
)) {
4786 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4787 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4789 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4790 query_target_trigger
= lttng_error_query_condition_borrow_target(query
);
4792 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4793 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(query
);
4799 LTTNG_ASSERT(query_target_trigger
);
4801 ret_code
= notification_thread_command_get_trigger(
4802 notification_thread
, query_target_trigger
, &matching_trigger
);
4803 if (ret_code
!= LTTNG_OK
) {
4807 /* No longer needed. */
4808 query_target_trigger
= nullptr;
4810 if (lttng_error_query_get_target_type(query
) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4811 /* Get the sessiond-side version of the target action. */
4812 query_target_action
=
4813 lttng_error_query_action_borrow_action_target(query
, matching_trigger
);
4816 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4817 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4818 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
, &trigger_owner
);
4819 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4821 results
= lttng_error_query_results_create();
4823 ret_code
= LTTNG_ERR_NOMEM
;
4827 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4829 (int) trigger_owner
,
4830 (int) lttng_credentials_get_uid(cmd_creds
));
4833 * Validate the trigger credentials against the command credentials.
4834 * Only the root user can target a trigger with non-matching
4837 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger
),
4839 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4840 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4842 (int) trigger_owner
,
4843 (int) lttng_credentials_get_uid(cmd_creds
));
4844 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4849 switch (lttng_error_query_get_target_type(query
)) {
4850 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4851 trigger_status
= lttng_trigger_add_error_results(matching_trigger
, results
);
4853 switch (trigger_status
) {
4854 case LTTNG_TRIGGER_STATUS_OK
:
4857 ret_code
= LTTNG_ERR_UNK
;
4862 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4865 lttng_trigger_condition_add_error_results(matching_trigger
, results
);
4867 switch (trigger_status
) {
4868 case LTTNG_TRIGGER_STATUS_OK
:
4871 ret_code
= LTTNG_ERR_UNK
;
4877 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4879 const enum lttng_action_status action_status
=
4880 lttng_action_add_error_query_results(query_target_action
, results
);
4882 switch (action_status
) {
4883 case LTTNG_ACTION_STATUS_OK
:
4886 ret_code
= LTTNG_ERR_UNK
;
4897 *_results
= results
;
4899 ret_code
= LTTNG_OK
;
4901 lttng_trigger_put(matching_trigger
);
4902 lttng_error_query_results_destroy(results
);
4907 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4908 * snapshot output is *not* set with a remote destination.
4910 * Return LTTNG_OK on success or a LTTNG_ERR code.
4912 static enum lttng_error_code
set_relayd_for_snapshot(struct consumer_output
*output
,
4913 const struct ltt_session
*session
)
4915 enum lttng_error_code status
= LTTNG_OK
;
4916 struct lttng_ht_iter iter
;
4917 struct consumer_socket
*socket
;
4918 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4919 const char *base_path
;
4921 LTTNG_ASSERT(output
);
4922 LTTNG_ASSERT(session
);
4924 DBG2("Set relayd object from snapshot output");
4926 if (session
->current_trace_chunk
) {
4927 enum lttng_trace_chunk_status chunk_status
= lttng_trace_chunk_get_id(
4928 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
4930 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4931 current_chunk_id
.is_set
= true;
4933 ERR("Failed to get current trace chunk id");
4934 status
= LTTNG_ERR_UNK
;
4939 /* Ignore if snapshot consumer output is not network. */
4940 if (output
->type
!= CONSUMER_DST_NET
) {
4945 * The snapshot record URI base path overrides the session
4948 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4949 base_path
= output
->dst
.net
.control
.subdir
;
4951 base_path
= session
->base_path
;
4955 * For each consumer socket, create and send the relayd object of the
4959 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
4960 pthread_mutex_lock(socket
->lock
);
4961 status
= send_consumer_relayd_sockets(
4968 session
->live_timer
,
4969 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: nullptr,
4970 session
->creation_time
,
4971 session
->name_contains_creation_time
);
4972 pthread_mutex_unlock(socket
->lock
);
4973 if (status
!= LTTNG_OK
) {
4985 * Record a kernel snapshot.
4987 * Return LTTNG_OK on success or a LTTNG_ERR code.
4989 static enum lttng_error_code
record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
4990 const struct consumer_output
*output
,
4991 const struct ltt_session
*session
,
4992 uint64_t nb_packets_per_stream
)
4994 enum lttng_error_code status
;
4996 LTTNG_ASSERT(ksess
);
4997 LTTNG_ASSERT(output
);
4998 LTTNG_ASSERT(session
);
5000 status
= kernel_snapshot_record(ksess
, output
, nb_packets_per_stream
);
5005 * Record a UST snapshot.
5007 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5009 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5010 const struct consumer_output
*output
,
5011 const struct ltt_session
*session
,
5012 uint64_t nb_packets_per_stream
)
5014 enum lttng_error_code status
;
5016 LTTNG_ASSERT(usess
);
5017 LTTNG_ASSERT(output
);
5018 LTTNG_ASSERT(session
);
5020 status
= ust_app_snapshot_record(usess
, output
, nb_packets_per_stream
);
5024 static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session
*session
,
5025 uint64_t cur_nr_packets
)
5027 uint64_t tot_size
= 0;
5029 if (session
->kernel_session
) {
5030 struct ltt_kernel_channel
*chan
;
5031 const struct ltt_kernel_session
*ksess
= session
->kernel_session
;
5033 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
5034 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5036 * Don't take channel into account if we
5037 * already grab all its packets.
5041 tot_size
+= chan
->channel
->attr
.subbuf_size
* chan
->stream_count
;
5045 if (session
->ust_session
) {
5046 const struct ltt_ust_session
*usess
= session
->ust_session
;
5048 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
, cur_nr_packets
);
5055 * Calculate the number of packets we can grab from each stream that
5056 * fits within the overall snapshot max size.
5058 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5059 * the number of packets per stream.
5061 * TODO: this approach is not perfect: we consider the worse case
5062 * (packet filling the sub-buffers) as an upper bound, but we could do
5063 * better if we do this calculation while we actually grab the packet
5064 * content: we would know how much padding we don't actually store into
5067 * This algorithm is currently bounded by the number of packets per
5070 * Since we call this algorithm before actually grabbing the data, it's
5071 * an approximation: for instance, applications could appear/disappear
5072 * in between this call and actually grabbing data.
5074 static int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5078 uint64_t cur_nb_packets
= 0;
5081 return 0; /* Infinite */
5084 size_left
= max_size
;
5086 uint64_t one_more_packet_tot_size
;
5088 one_more_packet_tot_size
=
5089 get_session_size_one_more_packet_per_stream(session
, cur_nb_packets
);
5090 if (!one_more_packet_tot_size
) {
5091 /* We are already grabbing all packets. */
5094 size_left
-= one_more_packet_tot_size
;
5095 if (size_left
< 0) {
5100 if (!cur_nb_packets
&& size_left
!= max_size
) {
5101 /* Not enough room to grab one packet of each stream, error. */
5104 return cur_nb_packets
;
5107 static enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5108 const struct snapshot_output
*snapshot_output
)
5110 int64_t nb_packets_per_stream
;
5111 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5113 enum lttng_error_code ret_code
= LTTNG_OK
;
5114 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5115 struct consumer_output
*original_ust_consumer_output
= nullptr;
5116 struct consumer_output
*original_kernel_consumer_output
= nullptr;
5117 struct consumer_output
*snapshot_ust_consumer_output
= nullptr;
5118 struct consumer_output
*snapshot_kernel_consumer_output
= nullptr;
5120 ret
= snprintf(snapshot_chunk_name
,
5121 sizeof(snapshot_chunk_name
),
5123 snapshot_output
->name
,
5124 snapshot_output
->datetime
,
5125 snapshot_output
->nb_snapshot
);
5126 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5127 ERR("Failed to format snapshot name");
5128 ret_code
= LTTNG_ERR_INVALID
;
5131 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5132 snapshot_output
->name
,
5134 snapshot_chunk_name
);
5135 if (!session
->kernel_session
&& !session
->ust_session
) {
5136 ERR("Failed to record snapshot as no channels exist");
5137 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5141 if (session
->kernel_session
) {
5142 original_kernel_consumer_output
= session
->kernel_session
->consumer
;
5143 snapshot_kernel_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5144 strcpy(snapshot_kernel_consumer_output
->chunk_path
, snapshot_chunk_name
);
5146 /* Copy the original domain subdir. */
5147 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5148 original_kernel_consumer_output
->domain_subdir
);
5150 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5151 original_kernel_consumer_output
);
5153 ERR("Failed to copy consumer sockets from snapshot output configuration");
5154 ret_code
= LTTNG_ERR_NOMEM
;
5157 ret_code
= set_relayd_for_snapshot(snapshot_kernel_consumer_output
, session
);
5158 if (ret_code
!= LTTNG_OK
) {
5159 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5162 session
->kernel_session
->consumer
= snapshot_kernel_consumer_output
;
5164 if (session
->ust_session
) {
5165 original_ust_consumer_output
= session
->ust_session
->consumer
;
5166 snapshot_ust_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5167 strcpy(snapshot_ust_consumer_output
->chunk_path
, snapshot_chunk_name
);
5169 /* Copy the original domain subdir. */
5170 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5171 original_ust_consumer_output
->domain_subdir
);
5173 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5174 original_ust_consumer_output
);
5176 ERR("Failed to copy consumer sockets from snapshot output configuration");
5177 ret_code
= LTTNG_ERR_NOMEM
;
5180 ret_code
= set_relayd_for_snapshot(snapshot_ust_consumer_output
, session
);
5181 if (ret_code
!= LTTNG_OK
) {
5182 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5185 session
->ust_session
->consumer
= snapshot_ust_consumer_output
;
5188 snapshot_trace_chunk
= session_create_new_trace_chunk(
5190 snapshot_kernel_consumer_output
?: snapshot_ust_consumer_output
,
5191 consumer_output_get_base_path(snapshot_output
->consumer
),
5192 snapshot_chunk_name
);
5193 if (!snapshot_trace_chunk
) {
5194 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5196 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5199 LTTNG_ASSERT(!session
->current_trace_chunk
);
5200 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, nullptr);
5201 lttng_trace_chunk_put(snapshot_trace_chunk
);
5202 snapshot_trace_chunk
= nullptr;
5204 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5206 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5210 nb_packets_per_stream
=
5211 get_session_nb_packets_per_stream(session
, snapshot_output
->max_size
);
5212 if (nb_packets_per_stream
< 0) {
5213 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5214 goto error_close_trace_chunk
;
5217 if (session
->kernel_session
) {
5218 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5219 snapshot_kernel_consumer_output
,
5221 nb_packets_per_stream
);
5222 if (ret_code
!= LTTNG_OK
) {
5223 goto error_close_trace_chunk
;
5227 if (session
->ust_session
) {
5228 ret_code
= record_ust_snapshot(session
->ust_session
,
5229 snapshot_ust_consumer_output
,
5231 nb_packets_per_stream
);
5232 if (ret_code
!= LTTNG_OK
) {
5233 goto error_close_trace_chunk
;
5237 error_close_trace_chunk
:
5238 if (session_set_trace_chunk(session
, nullptr, &snapshot_trace_chunk
)) {
5239 ERR("Failed to release the current trace chunk of session \"%s\"", session
->name
);
5240 ret_code
= LTTNG_ERR_UNK
;
5243 if (session_close_trace_chunk(session
,
5244 snapshot_trace_chunk
,
5245 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
,
5248 * Don't goto end; make sure the chunk is closed for the session
5249 * to allow future snapshots.
5251 ERR("Failed to close snapshot trace chunk of session \"%s\"", session
->name
);
5252 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5255 lttng_trace_chunk_put(snapshot_trace_chunk
);
5256 snapshot_trace_chunk
= nullptr;
5258 if (original_ust_consumer_output
) {
5259 session
->ust_session
->consumer
= original_ust_consumer_output
;
5261 if (original_kernel_consumer_output
) {
5262 session
->kernel_session
->consumer
= original_kernel_consumer_output
;
5264 consumer_output_put(snapshot_ust_consumer_output
);
5265 consumer_output_put(snapshot_kernel_consumer_output
);
5270 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5272 * The wait parameter is ignored so this call always wait for the snapshot to
5273 * complete before returning.
5275 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5277 int cmd_snapshot_record(struct ltt_session
*session
,
5278 const struct lttng_snapshot_output
*output
,
5279 int wait
__attribute__((unused
)))
5281 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5283 unsigned int snapshot_success
= 0;
5285 struct snapshot_output
*tmp_output
= nullptr;
5287 LTTNG_ASSERT(session
);
5288 LTTNG_ASSERT(output
);
5290 DBG("Cmd snapshot record for session %s", session
->name
);
5292 /* Get the datetime for the snapshot output directory. */
5293 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
, sizeof(datetime
));
5295 cmd_ret
= LTTNG_ERR_INVALID
;
5300 * Permission denied to create an output if the session is not
5301 * set in no output mode.
5303 if (session
->output_traces
) {
5304 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5308 /* The session needs to be started at least once. */
5309 if (!session
->has_been_started
) {
5310 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5314 /* Use temporary output for the session. */
5315 if (*output
->ctrl_url
!= '\0') {
5316 tmp_output
= snapshot_output_alloc();
5318 cmd_ret
= LTTNG_ERR_NOMEM
;
5322 ret
= snapshot_output_init(session
,
5331 if (ret
== -ENOMEM
) {
5332 cmd_ret
= LTTNG_ERR_NOMEM
;
5334 cmd_ret
= LTTNG_ERR_INVALID
;
5338 /* Use the global session count for the temporary snapshot. */
5339 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5341 /* Use the global datetime */
5342 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5343 cmd_ret
= snapshot_record(session
, tmp_output
);
5344 if (cmd_ret
!= LTTNG_OK
) {
5347 snapshot_success
= 1;
5349 struct snapshot_output
*sout
;
5350 struct lttng_ht_iter iter
;
5353 cds_lfht_for_each_entry (
5354 session
->snapshot
.output_ht
->ht
, &iter
.iter
, sout
, node
.node
) {
5355 struct snapshot_output output_copy
;
5358 * Make a local copy of the output and override output
5359 * parameters with those provided as part of the
5362 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5364 if (output
->max_size
!= (uint64_t) -1ULL) {
5365 output_copy
.max_size
= output
->max_size
;
5368 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5369 memcpy(output_copy
.datetime
, datetime
, sizeof(datetime
));
5371 /* Use temporary name. */
5372 if (*output
->name
!= '\0') {
5373 if (lttng_strncpy(output_copy
.name
,
5375 sizeof(output_copy
.name
))) {
5376 cmd_ret
= LTTNG_ERR_INVALID
;
5382 cmd_ret
= snapshot_record(session
, &output_copy
);
5383 if (cmd_ret
!= LTTNG_OK
) {
5387 snapshot_success
= 1;
5392 if (snapshot_success
) {
5393 session
->snapshot
.nb_snapshot
++;
5395 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5400 snapshot_output_destroy(tmp_output
);
5406 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5408 int cmd_set_session_shm_path(struct ltt_session
*session
, const char *shm_path
)
5411 LTTNG_ASSERT(session
);
5414 * Can only set shm path before session is started.
5416 if (session
->has_been_started
) {
5417 return LTTNG_ERR_SESSION_STARTED
;
5420 strncpy(session
->shm_path
, shm_path
, sizeof(session
->shm_path
));
5421 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5427 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5429 * Ask the consumer to rotate the session output directory.
5430 * The session lock must be held.
5432 * Returns LTTNG_OK on success or else a negative LTTng error code.
5434 int cmd_rotate_session(struct ltt_session
*session
,
5435 struct lttng_rotate_session_return
*rotate_return
,
5436 bool quiet_rotation
,
5437 enum lttng_trace_chunk_command_type command
)
5440 uint64_t ongoing_rotation_chunk_id
;
5441 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5442 struct lttng_trace_chunk
*chunk_being_archived
= nullptr;
5443 struct lttng_trace_chunk
*new_trace_chunk
= nullptr;
5444 enum lttng_trace_chunk_status chunk_status
;
5445 bool failed_to_rotate
= false;
5446 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5448 LTTNG_ASSERT(session
);
5450 if (!session
->has_been_started
) {
5451 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5456 * Explicit rotation is not supported for live sessions.
5457 * However, live sessions can perform a quiet rotation on
5459 * Rotation is not supported for snapshot traces (no output).
5461 if ((!quiet_rotation
&& session
->live_timer
) || !session
->output_traces
) {
5462 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5466 /* Unsupported feature in lttng-relayd before 2.11. */
5467 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5468 (session
->consumer
->relay_major_version
== 2 &&
5469 session
->consumer
->relay_minor_version
< 11)) {
5470 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5474 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5475 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5476 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5480 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5481 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5483 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5488 * After a stop, we only allow one rotation to occur, the other ones are
5489 * useless until a new start.
5491 if (session
->rotated_after_last_stop
) {
5492 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5494 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5499 * After a stop followed by a clear, disallow following rotations a they would
5500 * generate empty chunks.
5502 if (session
->cleared_after_last_stop
) {
5503 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5505 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5509 if (session
->active
) {
5511 session_create_new_trace_chunk(session
, nullptr, nullptr, nullptr);
5512 if (!new_trace_chunk
) {
5513 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5519 * The current trace chunk becomes the chunk being archived.
5521 * After this point, "chunk_being_archived" must absolutely
5522 * be closed on the consumer(s), otherwise it will never be
5523 * cleaned-up, which will result in a leak.
5525 ret
= session_set_trace_chunk(session
, new_trace_chunk
, &chunk_being_archived
);
5527 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5531 if (session
->kernel_session
) {
5532 cmd_ret
= kernel_rotate_session(session
);
5533 if (cmd_ret
!= LTTNG_OK
) {
5534 failed_to_rotate
= true;
5535 rotation_fail_code
= cmd_ret
;
5538 if (session
->ust_session
) {
5539 cmd_ret
= ust_app_rotate_session(session
);
5540 if (cmd_ret
!= LTTNG_OK
) {
5541 failed_to_rotate
= true;
5542 rotation_fail_code
= cmd_ret
;
5546 if (!session
->active
) {
5547 session
->rotated_after_last_stop
= true;
5550 if (!chunk_being_archived
) {
5551 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5553 if (failed_to_rotate
) {
5554 cmd_ret
= rotation_fail_code
;
5561 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5562 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
, &ongoing_rotation_chunk_id
);
5563 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5565 ret
= session_close_trace_chunk(
5566 session
, chunk_being_archived
, command
, session
->last_chunk_path
);
5568 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5572 if (failed_to_rotate
) {
5573 cmd_ret
= rotation_fail_code
;
5577 session
->quiet_rotation
= quiet_rotation
;
5578 ret
= timer_session_rotation_pending_check_start(session
, DEFAULT_ROTATE_PENDING_TIMER
);
5580 cmd_ret
= LTTNG_ERR_UNK
;
5584 if (rotate_return
) {
5585 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5588 session
->chunk_being_archived
= chunk_being_archived
;
5589 chunk_being_archived
= nullptr;
5590 if (!quiet_rotation
) {
5591 ret
= notification_thread_command_session_rotation_ongoing(
5592 the_notification_thread_handle
, session
->id
, ongoing_rotation_chunk_id
);
5593 if (ret
!= LTTNG_OK
) {
5594 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5596 cmd_ret
= (lttng_error_code
) ret
;
5600 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5602 ongoing_rotation_chunk_id
);
5604 lttng_trace_chunk_put(new_trace_chunk
);
5605 lttng_trace_chunk_put(chunk_being_archived
);
5606 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5609 if (session_reset_rotation_state(session
, LTTNG_ROTATION_STATE_ERROR
)) {
5610 ERR("Failed to reset rotation state of session \"%s\"", session
->name
);
5616 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5618 * Check if the session has finished its rotation.
5620 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5622 int cmd_rotate_get_info(struct ltt_session
*session
,
5623 struct lttng_rotation_get_info_return
*info_return
,
5624 uint64_t rotation_id
)
5626 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5627 enum lttng_rotation_state rotation_state
;
5629 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
,
5631 session
->most_recent_chunk_id
.value
);
5633 if (session
->chunk_being_archived
) {
5634 enum lttng_trace_chunk_status chunk_status
;
5637 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
, &chunk_id
);
5638 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5640 rotation_state
= rotation_id
== chunk_id
? LTTNG_ROTATION_STATE_ONGOING
:
5641 LTTNG_ROTATION_STATE_EXPIRED
;
5643 if (session
->last_archived_chunk_id
.is_set
&&
5644 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5645 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5647 rotation_state
= session
->rotation_state
;
5651 switch (rotation_state
) {
5652 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5653 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5656 case LTTNG_ROTATION_STATE_EXPIRED
:
5657 DBG("Reporting that the rotation state of rotation id %" PRIu64
5658 " of session \"%s\" has expired",
5662 case LTTNG_ROTATION_STATE_ONGOING
:
5663 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5667 case LTTNG_ROTATION_STATE_COMPLETED
:
5671 char *current_tracing_path_reply
;
5672 size_t current_tracing_path_reply_len
;
5674 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5678 switch (session_get_consumer_destination_type(session
)) {
5679 case CONSUMER_DST_LOCAL
:
5680 current_tracing_path_reply
= info_return
->location
.local
.absolute_path
;
5681 current_tracing_path_reply_len
=
5682 sizeof(info_return
->location
.local
.absolute_path
);
5683 info_return
->location_type
=
5684 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5685 fmt_ret
= asprintf(&chunk_path
,
5686 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5687 session_get_base_path(session
),
5688 session
->last_archived_chunk_name
);
5689 if (fmt_ret
== -1) {
5690 PERROR("Failed to format the path of the last archived trace chunk");
5691 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5692 cmd_ret
= LTTNG_ERR_UNK
;
5696 case CONSUMER_DST_NET
:
5698 uint16_t ctrl_port
, data_port
;
5700 current_tracing_path_reply
= info_return
->location
.relay
.relative_path
;
5701 current_tracing_path_reply_len
=
5702 sizeof(info_return
->location
.relay
.relative_path
);
5703 /* Currently the only supported relay protocol. */
5704 info_return
->location
.relay
.protocol
=
5705 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5707 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5708 session_get_net_consumer_hostname(session
),
5709 sizeof(info_return
->location
.relay
.host
));
5711 ERR("Failed to copy host name to rotate_get_info reply");
5712 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5713 cmd_ret
= LTTNG_ERR_SET_URL
;
5717 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5718 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5719 info_return
->location
.relay
.ports
.data
= data_port
;
5720 info_return
->location_type
=
5721 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5722 chunk_path
= strdup(session
->last_chunk_path
);
5724 ERR("Failed to allocate the path of the last archived trace chunk");
5725 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5726 cmd_ret
= LTTNG_ERR_UNK
;
5735 fmt_ret
= lttng_strncpy(
5736 current_tracing_path_reply
, chunk_path
, current_tracing_path_reply_len
);
5739 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5740 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5741 cmd_ret
= LTTNG_ERR_UNK
;
5747 case LTTNG_ROTATION_STATE_ERROR
:
5748 DBG("Reporting that an error occurred during rotation %" PRIu64
5749 " of session \"%s\"",
5759 info_return
->status
= (int32_t) rotation_state
;
5764 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5766 * Configure the automatic rotation parameters.
5767 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5768 * 'activate' to false means deactivate the rotation schedule and validate that
5769 * 'new_value' has the same value as the currently active value.
5771 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5773 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5775 enum lttng_rotation_schedule_type schedule_type
,
5777 struct notification_thread_handle
*notification_thread_handle
)
5780 uint64_t *parameter_value
;
5782 LTTNG_ASSERT(session
);
5784 DBG("Cmd rotate set schedule session %s", session
->name
);
5786 if (session
->live_timer
|| !session
->output_traces
) {
5787 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5788 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5792 switch (schedule_type
) {
5793 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5794 parameter_value
= &session
->rotate_size
;
5796 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5797 parameter_value
= &session
->rotate_timer_period
;
5798 if (new_value
>= UINT_MAX
) {
5799 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
5803 ret
= LTTNG_ERR_INVALID
;
5808 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5809 ret
= LTTNG_ERR_INVALID
;
5813 /* Improper use of the API. */
5814 if (new_value
== -1ULL) {
5815 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5816 ret
= LTTNG_ERR_INVALID
;
5821 * As indicated in struct ltt_session's comments, a value of == 0 means
5822 * this schedule rotation type is not in use.
5824 * Reject the command if we were asked to activate a schedule that was
5827 if (activate
&& *parameter_value
!= 0) {
5828 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5829 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5834 * Reject the command if we were asked to deactivate a schedule that was
5837 if (!activate
&& *parameter_value
== 0) {
5838 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5839 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5844 * Reject the command if we were asked to deactivate a schedule that
5847 if (!activate
&& *parameter_value
!= new_value
) {
5848 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5849 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5853 *parameter_value
= activate
? new_value
: 0;
5855 switch (schedule_type
) {
5856 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5857 if (activate
&& session
->active
) {
5859 * Only start the timer if the session is active,
5860 * otherwise it will be started when the session starts.
5862 ret
= timer_session_rotation_schedule_timer_start(session
, new_value
);
5864 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5865 ret
= LTTNG_ERR_UNK
;
5869 ret
= timer_session_rotation_schedule_timer_stop(session
);
5871 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5872 ret
= LTTNG_ERR_UNK
;
5877 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5879 ret
= subscribe_session_consumed_size_rotation(
5880 session
, new_value
, notification_thread_handle
);
5882 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5883 ret
= LTTNG_ERR_UNK
;
5887 ret
= unsubscribe_session_consumed_size_rotation(
5888 session
, notification_thread_handle
);
5890 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5891 ret
= LTTNG_ERR_UNK
;
5897 /* Would have been caught before. */
5909 /* Wait for a given path to be removed before continuing. */
5910 static enum lttng_error_code
wait_on_path(void *path_data
)
5912 const char *shm_path
= (const char *) path_data
;
5914 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5920 ret
= stat(shm_path
, &st
);
5922 if (errno
!= ENOENT
) {
5923 PERROR("stat() returned an error while checking for the existence of the shm path");
5925 DBG("shm path no longer exists, completing the destruction of session");
5929 if (!S_ISDIR(st
.st_mode
)) {
5930 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5935 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5941 * Returns a pointer to a handler to run on completion of a command.
5942 * Returns NULL if no handler has to be run for the last command executed.
5944 const struct cmd_completion_handler
*cmd_pop_completion_handler()
5946 struct cmd_completion_handler
*handler
= current_completion_handler
;
5948 current_completion_handler
= nullptr;
5953 * Init command subsystem.
5958 * Set network sequence index to 1 for streams to match a relayd
5959 * socket on the consumer side.
5961 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5962 relayd_net_seq_idx
= 1;
5963 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5965 DBG("Command subsystem initialized");