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
16 #include <urcu/list.h>
17 #include <urcu/uatomic.h>
19 #include <common/buffer-view.hpp>
20 #include <common/common.hpp>
21 #include <common/compat/string.hpp>
22 #include <common/defaults.hpp>
23 #include <common/dynamic-buffer.hpp>
24 #include <common/kernel-ctl/kernel-ctl.hpp>
25 #include <common/payload-view.hpp>
26 #include <common/payload.hpp>
27 #include <common/relayd/relayd.hpp>
28 #include <common/sessiond-comm/sessiond-comm.hpp>
29 #include <common/string-utils/string-utils.hpp>
30 #include <common/trace-chunk.hpp>
31 #include <common/utils.hpp>
32 #include <lttng/action/action-internal.hpp>
33 #include <lttng/action/action.h>
34 #include <lttng/channel-internal.hpp>
35 #include <lttng/channel.h>
36 #include <lttng/condition/condition-internal.hpp>
37 #include <lttng/condition/condition.h>
38 #include <lttng/condition/event-rule-matches-internal.hpp>
39 #include <lttng/condition/event-rule-matches.h>
40 #include <lttng/error-query-internal.hpp>
41 #include <lttng/event-internal.hpp>
42 #include <lttng/event-rule/event-rule-internal.hpp>
43 #include <lttng/event-rule/event-rule.h>
44 #include <lttng/location-internal.hpp>
45 #include <lttng/lttng-error.h>
46 #include <lttng/rotate-internal.hpp>
47 #include <lttng/session-descriptor-internal.hpp>
48 #include <lttng/session-internal.hpp>
49 #include <lttng/tracker.h>
50 #include <lttng/trigger/trigger-internal.hpp>
51 #include <lttng/userspace-probe-internal.hpp>
53 #include "agent-thread.hpp"
55 #include "buffer-registry.hpp"
56 #include "channel.hpp"
58 #include "consumer.hpp"
59 #include "event-notifier-error-accounting.hpp"
61 #include "health-sessiond.hpp"
62 #include "kernel-consumer.hpp"
64 #include "lttng-sessiond.hpp"
65 #include "lttng-syscall.hpp"
66 #include "notification-thread-commands.hpp"
67 #include "notification-thread.hpp"
69 #include "rotation-thread.hpp"
70 #include "session.hpp"
72 #include "tracker.hpp"
75 /* Sleep for 100ms between each check for the shm path's deletion. */
76 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
78 namespace lsu
= lttng::sessiond::ust
;
80 static enum lttng_error_code
wait_on_path(void *path
);
83 struct cmd_destroy_session_reply_context
{
85 bool implicit_rotation_on_destroy
;
87 * Indicates whether or not an error occurred while launching the
88 * destruction of a session.
90 enum lttng_error_code destruction_status
;
94 * Command completion handler that is used by the destroy command
95 * when a session that has a non-default shm_path is being destroyed.
97 * See comment in cmd_destroy_session() for the rationale.
99 struct destroy_completion_handler
{
100 struct cmd_completion_handler handler
;
101 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
102 } destroy_completion_handler
= {
105 .data
= destroy_completion_handler
.shm_path
111 * Used to keep a unique index for each relayd socket created where this value
112 * is associated with streams on the consumer so it can match the right relayd
113 * to send to. It must be accessed with the relayd_net_seq_idx_lock
116 pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
117 uint64_t relayd_net_seq_idx
;
120 static struct cmd_completion_handler
*current_completion_handler
;
121 static int validate_ust_event_name(const char *);
122 static int cmd_enable_event_internal(struct ltt_session
*session
,
123 const struct lttng_domain
*domain
,
124 char *channel_name
, struct lttng_event
*event
,
125 char *filter_expression
,
126 struct lttng_bytecode
*filter
,
127 struct lttng_event_exclusion
*exclusion
,
129 static enum lttng_error_code
cmd_enable_channel_internal(
130 struct ltt_session
*session
,
131 const struct lttng_domain
*domain
,
132 const struct lttng_channel
*_attr
,
136 * Create a session path used by list_lttng_sessions for the case that the
137 * session consumer is on the network.
139 static int build_network_session_path(char *dst
, size_t size
,
140 struct ltt_session
*session
)
142 int ret
, kdata_port
, udata_port
;
143 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
144 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
146 LTTNG_ASSERT(session
);
149 memset(tmp_urls
, 0, sizeof(tmp_urls
));
150 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
152 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
154 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
155 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
156 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
159 if (session
->ust_session
&& session
->ust_session
->consumer
) {
160 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
161 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
164 if (uuri
== NULL
&& kuri
== NULL
) {
165 uri
= &session
->consumer
->dst
.net
.control
;
166 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
167 } else if (kuri
&& uuri
) {
168 ret
= uri_compare(kuri
, uuri
);
172 /* Build uuri URL string */
173 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
180 } else if (kuri
&& uuri
== NULL
) {
182 } else if (uuri
&& kuri
== NULL
) {
186 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
192 * Do we have a UST url set. If yes, this means we have both kernel and UST
195 if (*tmp_uurl
!= '\0') {
196 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
197 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
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
, uint64_t *discarded_events
,
219 uint64_t *lost_packets
)
223 if (!session
->has_been_started
) {
225 *discarded_events
= 0;
230 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
231 session
->kernel_session
->consumer
,
237 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
238 session
->kernel_session
->consumer
,
249 * Get run-time attributes if the session has been started (discarded events,
252 static int get_ust_runtime_stats(struct ltt_session
*session
,
253 struct ltt_ust_channel
*uchan
, 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
,
278 usess
->consumer
, uchan
->id
,
279 uchan
->attr
.overwrite
,
282 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
283 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
284 uchan
, usess
->consumer
,
285 uchan
->attr
.overwrite
,
291 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
292 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
294 ERR("Unsupported buffer type");
305 * Create a list of agent domain events.
307 * Return number of events in list on success or else a negative value.
309 static enum lttng_error_code
list_lttng_agent_events(
310 struct agent
*agt
, struct lttng_payload
*reply_payload
,
311 unsigned int *nb_events
)
313 enum lttng_error_code ret_code
;
315 unsigned int local_nb_events
= 0;
316 struct agent_event
*event
;
317 struct lttng_ht_iter iter
;
318 unsigned long agent_event_count
;
321 assert(reply_payload
);
323 DBG3("Listing agent events");
326 agent_event_count
= lttng_ht_get_count(agt
->events
);
327 if (agent_event_count
== 0) {
332 if (agent_event_count
> UINT_MAX
) {
333 ret_code
= LTTNG_ERR_OVERFLOW
;
337 local_nb_events
= (unsigned int) agent_event_count
;
339 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
340 struct lttng_event
*tmp_event
= lttng_event_create();
343 ret_code
= LTTNG_ERR_NOMEM
;
347 if (lttng_strncpy(tmp_event
->name
, event
->name
, sizeof(tmp_event
->name
))) {
348 lttng_event_destroy(tmp_event
);
349 ret_code
= LTTNG_ERR_FATAL
;
353 tmp_event
->name
[sizeof(tmp_event
->name
) - 1] = '\0';
354 tmp_event
->enabled
= !!event
->enabled_count
;
355 tmp_event
->loglevel
= event
->loglevel_value
;
356 tmp_event
->loglevel_type
= event
->loglevel_type
;
358 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
359 event
->filter_expression
, 0, NULL
, reply_payload
);
360 lttng_event_destroy(tmp_event
);
362 ret_code
= LTTNG_ERR_FATAL
;
369 *nb_events
= local_nb_events
;
376 * Create a list of ust global domain events.
378 static enum lttng_error_code
list_lttng_ust_global_events(char *channel_name
,
379 struct ltt_ust_domain_global
*ust_global
,
380 struct lttng_payload
*reply_payload
,
381 unsigned int *nb_events
)
383 enum lttng_error_code ret_code
;
385 struct lttng_ht_iter iter
;
386 struct lttng_ht_node_str
*node
;
387 struct ltt_ust_channel
*uchan
;
388 struct ltt_ust_event
*uevent
;
389 unsigned long channel_event_count
;
390 unsigned int local_nb_events
= 0;
392 assert(reply_payload
);
395 DBG("Listing UST global events for channel %s", channel_name
);
399 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
400 node
= lttng_ht_iter_get_node_str(&iter
);
402 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
406 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
408 channel_event_count
= lttng_ht_get_count(uchan
->events
);
409 if (channel_event_count
== 0) {
415 if (channel_event_count
> UINT_MAX
) {
416 ret_code
= LTTNG_ERR_OVERFLOW
;
420 local_nb_events
= (unsigned int) channel_event_count
;
422 DBG3("Listing UST global %d events", *nb_events
);
424 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
425 struct lttng_event
*tmp_event
= NULL
;
427 if (uevent
->internal
) {
428 /* This event should remain hidden from clients */
433 tmp_event
= lttng_event_create();
435 ret_code
= LTTNG_ERR_NOMEM
;
439 if (lttng_strncpy(tmp_event
->name
, uevent
->attr
.name
,
440 LTTNG_SYMBOL_NAME_LEN
)) {
441 ret_code
= LTTNG_ERR_FATAL
;
442 lttng_event_destroy(tmp_event
);
446 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
447 tmp_event
->enabled
= uevent
->enabled
;
449 switch (uevent
->attr
.instrumentation
) {
450 case LTTNG_UST_ABI_TRACEPOINT
:
451 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
453 case LTTNG_UST_ABI_PROBE
:
454 tmp_event
->type
= LTTNG_EVENT_PROBE
;
456 case LTTNG_UST_ABI_FUNCTION
:
457 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
461 tmp_event
->loglevel
= uevent
->attr
.loglevel
;
462 switch (uevent
->attr
.loglevel_type
) {
463 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
464 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
466 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
467 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
469 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
470 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
473 if (uevent
->filter
) {
474 tmp_event
->filter
= 1;
476 if (uevent
->exclusion
) {
477 tmp_event
->exclusion
= 1;
481 * We do not care about the filter bytecode and the fd from the
482 * userspace_probe_location.
484 ret
= lttng_event_serialize(tmp_event
, uevent
->exclusion
? uevent
->exclusion
->count
: 0,
485 uevent
->exclusion
? (char **) uevent
->exclusion
->names
: NULL
,
486 uevent
->filter_expression
, 0, NULL
, reply_payload
);
487 lttng_event_destroy(tmp_event
);
489 ret_code
= LTTNG_ERR_FATAL
;
495 /* nb_events is already set at this point. */
497 *nb_events
= local_nb_events
;
504 * Fill lttng_event array of all kernel events in the channel.
506 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
507 struct ltt_kernel_session
*kernel_session
,
508 struct lttng_payload
*reply_payload
,
509 unsigned int *nb_events
)
511 enum lttng_error_code ret_code
;
513 struct ltt_kernel_event
*event
;
514 struct ltt_kernel_channel
*kchan
;
516 assert(reply_payload
);
518 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
520 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
524 *nb_events
= kchan
->event_count
;
526 DBG("Listing events for channel %s", kchan
->channel
->name
);
528 if (*nb_events
== 0) {
533 /* Kernel channels */
534 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
535 struct lttng_event
*tmp_event
= lttng_event_create();
538 ret_code
= LTTNG_ERR_NOMEM
;
542 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
543 lttng_event_destroy(tmp_event
);
544 ret_code
= LTTNG_ERR_FATAL
;
549 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
550 tmp_event
->enabled
= event
->enabled
;
551 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
553 switch (event
->event
->instrumentation
) {
554 case LTTNG_KERNEL_ABI_TRACEPOINT
:
555 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
557 case LTTNG_KERNEL_ABI_KRETPROBE
:
558 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
559 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
560 sizeof(struct lttng_kernel_abi_kprobe
));
562 case LTTNG_KERNEL_ABI_KPROBE
:
563 tmp_event
->type
= LTTNG_EVENT_PROBE
;
564 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
565 sizeof(struct lttng_kernel_abi_kprobe
));
567 case LTTNG_KERNEL_ABI_UPROBE
:
568 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
570 case LTTNG_KERNEL_ABI_FUNCTION
:
571 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
572 memcpy(&(tmp_event
->attr
.ftrace
), &event
->event
->u
.ftrace
,
573 sizeof(struct lttng_kernel_abi_function
));
575 case LTTNG_KERNEL_ABI_NOOP
:
576 tmp_event
->type
= LTTNG_EVENT_NOOP
;
578 case LTTNG_KERNEL_ABI_SYSCALL
:
579 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
581 case LTTNG_KERNEL_ABI_ALL
:
588 if (event
->userspace_probe_location
) {
589 struct lttng_userspace_probe_location
*location_copy
=
590 lttng_userspace_probe_location_copy(
591 event
->userspace_probe_location
);
593 if (!location_copy
) {
594 lttng_event_destroy(tmp_event
);
595 ret_code
= LTTNG_ERR_NOMEM
;
599 ret
= lttng_event_set_userspace_probe_location(
600 tmp_event
, location_copy
);
602 lttng_event_destroy(tmp_event
);
603 lttng_userspace_probe_location_destroy(
605 ret_code
= LTTNG_ERR_INVALID
;
610 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
611 event
->filter_expression
, 0, NULL
, reply_payload
);
612 lttng_event_destroy(tmp_event
);
614 ret_code
= LTTNG_ERR_FATAL
;
625 * Add URI so the consumer output object. Set the correct path depending on the
626 * domain adding the default trace directory.
628 static enum lttng_error_code
add_uri_to_consumer(
629 const struct ltt_session
*session
,
630 struct consumer_output
*consumer
,
631 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
634 enum lttng_error_code ret_code
= LTTNG_OK
;
638 if (consumer
== NULL
) {
639 DBG("No consumer detected. Don't add URI. Stopping.");
640 ret_code
= LTTNG_ERR_NO_CONSUMER
;
645 case LTTNG_DOMAIN_KERNEL
:
646 ret
= lttng_strncpy(consumer
->domain_subdir
,
647 DEFAULT_KERNEL_TRACE_DIR
,
648 sizeof(consumer
->domain_subdir
));
650 case LTTNG_DOMAIN_UST
:
651 ret
= lttng_strncpy(consumer
->domain_subdir
,
652 DEFAULT_UST_TRACE_DIR
,
653 sizeof(consumer
->domain_subdir
));
657 * This case is possible is we try to add the URI to the global
658 * tracing session consumer object which in this case there is
661 memset(consumer
->domain_subdir
, 0,
662 sizeof(consumer
->domain_subdir
));
666 ERR("Failed to initialize consumer output domain subdirectory");
667 ret_code
= LTTNG_ERR_FATAL
;
671 switch (uri
->dtype
) {
674 DBG2("Setting network URI to consumer");
676 if (consumer
->type
== CONSUMER_DST_NET
) {
677 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
678 consumer
->dst
.net
.control_isset
) ||
679 (uri
->stype
== LTTNG_STREAM_DATA
&&
680 consumer
->dst
.net
.data_isset
)) {
681 ret_code
= LTTNG_ERR_URL_EXIST
;
685 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
688 /* Set URI into consumer output object */
689 ret
= consumer_set_network_uri(session
, consumer
, uri
);
691 ret_code
= (lttng_error_code
) -ret
;
693 } else if (ret
== 1) {
695 * URI was the same in the consumer so we do not append the subdir
696 * again so to not duplicate output dir.
703 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
704 ret_code
= LTTNG_ERR_INVALID
;
707 DBG2("Setting trace directory path from URI to %s",
709 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
711 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
713 sizeof(consumer
->dst
.session_root_path
));
715 ret_code
= LTTNG_ERR_FATAL
;
718 consumer
->type
= CONSUMER_DST_LOCAL
;
728 * Init tracing by creating trace directory and sending fds kernel consumer.
730 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
733 struct lttng_ht_iter iter
;
734 struct consumer_socket
*socket
;
736 LTTNG_ASSERT(session
);
740 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
741 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
743 pthread_mutex_lock(socket
->lock
);
744 ret
= kernel_consumer_send_session(socket
, session
);
745 pthread_mutex_unlock(socket
->lock
);
747 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
759 * Create a socket to the relayd using the URI.
761 * On success, the relayd_sock pointer is set to the created socket.
762 * Else, it remains untouched and an LTTng error code is returned.
764 static lttng_error_code
create_connect_relayd(
765 struct lttng_uri
*uri
, struct lttcomm_relayd_sock
**relayd_sock
)
768 enum lttng_error_code status
= LTTNG_OK
;
769 struct lttcomm_relayd_sock
*rsock
;
771 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
772 RELAYD_VERSION_COMM_MINOR
);
774 status
= LTTNG_ERR_FATAL
;
779 * Connect to relayd so we can proceed with a session creation. This call
780 * can possibly block for an arbitrary amount of time to set the health
781 * state to be in poll execution.
784 ret
= relayd_connect(rsock
);
787 ERR("Unable to reach lttng-relayd");
788 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
792 /* Create socket for control stream. */
793 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
794 DBG3("Creating relayd stream socket from URI");
796 /* Check relayd version */
797 ret
= relayd_version_check(rsock
);
798 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
799 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
801 } else if (ret
< 0) {
802 ERR("Unable to reach lttng-relayd");
803 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
806 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
807 DBG3("Creating relayd data socket from URI");
809 /* Command is not valid */
810 ERR("Relayd invalid stream type: %d", uri
->stype
);
811 status
= LTTNG_ERR_INVALID
;
815 *relayd_sock
= rsock
;
820 /* The returned value is not useful since we are on an error path. */
821 (void) relayd_close(rsock
);
829 * Connect to the relayd using URI and send the socket to the right consumer.
831 * The consumer socket lock must be held by the caller.
833 * Returns LTTNG_OK on success or an LTTng error code on failure.
835 static enum lttng_error_code
send_consumer_relayd_socket(const struct ltt_session
& session
,
836 struct lttng_uri
*relayd_uri
,
837 struct consumer_output
*consumer
,
838 struct consumer_socket
*consumer_sock
,
839 const char *base_path_override
,
840 const uint64_t *current_chunk_id
,
841 struct lttcomm_relayd_sock
& rsock
)
844 enum lttng_error_code status
= LTTNG_OK
;
846 /* Set the network sequence index if not set. */
847 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
848 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
850 * Increment net_seq_idx because we are about to transfer the
851 * new relayd socket to the consumer.
852 * Assign unique key so the consumer can match streams.
854 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
855 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
858 /* Send relayd socket to consumer. */
859 ret
= consumer_send_relayd_socket(consumer_sock
, &rsock
, consumer
, relayd_uri
->stype
,
860 session
, base_path_override
, current_chunk_id
);
862 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
866 /* Flag that the corresponding socket was sent. */
867 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
868 consumer_sock
->control_sock_sent
= 1;
869 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
870 consumer_sock
->data_sock_sent
= 1;
874 if (status
!= LTTNG_OK
) {
876 * The consumer output for this session should not be used anymore
877 * since the relayd connection failed thus making any tracing or/and
878 * streaming not usable.
880 consumer
->enabled
= 0;
886 static bool is_trace_format_configuration_supported(
887 uint64_t supported_trace_format
, lttng::trace_format_descriptor
& trace_format
)
889 static const std::unordered_map
<enum lttng_trace_format_descriptor_type
,
890 enum lttcomm_relayd_configuration_trace_format_flag
>
892 {LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1
,
893 LTTCOMM_RELAYD_CONFIGURATION_TRACE_FORMAT_SUPPORTED_CTF1
},
894 {LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_2
,
895 LTTCOMM_RELAYD_CONFIGURATION_TRACE_FORMAT_SUPPORTED_CTF2
},
898 auto it
= mapping
.find(trace_format
.type());
899 if (it
== mapping
.end()) {
903 if (!(supported_trace_format
& it
->second
)) {
911 * Send both relayd sockets to a specific consumer and domain. This is a
912 * helper function to facilitate sending the information to the consumer for a
915 * The consumer socket lock must be held by the caller.
917 * Returns LTTNG_OK, or an LTTng error code on failure.
919 static lttng_error_code
send_consumer_relayd_sockets(const ltt_session
& session
,
920 struct consumer_output
*consumer
,
921 struct consumer_socket
*sock
,
922 const char *base_path_override
,
923 const uint64_t *current_chunk_id
)
925 enum lttng_error_code status
= LTTNG_OK
;
926 struct lttcomm_relayd_sock
*control_sock
= nullptr;
927 struct lttcomm_relayd_sock
*data_sock
= nullptr;
929 LTTNG_ASSERT(consumer
);
932 /* Sending control relayd socket. */
933 if (!sock
->control_sock_sent
) {
935 uint64_t result_flags
= 0;
936 uint64_t supported_trace_format
= 0;
937 /* Connect to relayd and make version check if uri is the control. */
938 status
= create_connect_relayd(&consumer
->dst
.net
.control
, &control_sock
);
939 if (status
!= LTTNG_OK
) {
942 LTTNG_ASSERT(control_sock
);
944 consumer
->relay_major_version
= control_sock
->major
;
945 consumer
->relay_minor_version
= control_sock
->minor
;
947 ret
= relayd_get_configuration(control_sock
,
948 LTTCOMM_RELAYD_CONFIGURATION_QUERY_FLAG_SUPPORTED_TRACE_FORMAT
,
949 result_flags
, &supported_trace_format
);
951 ERR("Unable to get relayd configuration");
952 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
956 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
957 consumer
->relay_allows_clear
= true;
960 if (!is_trace_format_configuration_supported(
961 supported_trace_format
, *session
.trace_format
)) {
962 ERR("Relayd does not support the requested trace format");
963 status
= LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_RELAY_DAEMON
;
967 status
= send_consumer_relayd_socket(session
, &consumer
->dst
.net
.control
, consumer
,
968 sock
, base_path_override
, current_chunk_id
, *control_sock
);
970 if (status
!= LTTNG_OK
) {
975 /* Sending data relayd socket. */
976 if (!sock
->data_sock_sent
) {
977 /* Connect to relayd and make version check if uri is the control. */
978 status
= create_connect_relayd(&consumer
->dst
.net
.data
, &data_sock
);
979 if (status
!= LTTNG_OK
) {
982 LTTNG_ASSERT(data_sock
);
984 status
= send_consumer_relayd_socket(session
, &consumer
->dst
.net
.data
, consumer
,
985 sock
, base_path_override
, current_chunk_id
, *data_sock
);
987 if (status
!= LTTNG_OK
) {
993 if (control_sock
!= nullptr) {
994 relayd_close(control_sock
);
998 if (data_sock
!= nullptr) {
999 relayd_close(data_sock
);
1007 * Setup relayd connections for a tracing session. First creates the socket to
1008 * the relayd and send them to the right domain consumer. Consumer type MUST be
1011 int cmd_setup_relayd(struct ltt_session
*session
)
1014 struct ltt_ust_session
*usess
;
1015 struct ltt_kernel_session
*ksess
;
1016 struct consumer_socket
*socket
;
1017 struct lttng_ht_iter iter
;
1018 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1020 LTTNG_ASSERT(session
);
1022 usess
= session
->ust_session
;
1023 ksess
= session
->kernel_session
;
1025 DBG("Setting relayd for session %s", session
->name
);
1028 if (session
->current_trace_chunk
) {
1029 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1030 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1032 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1033 current_chunk_id
.is_set
= true;
1035 ERR("Failed to get current trace chunk id");
1036 ret
= LTTNG_ERR_UNK
;
1041 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1042 && usess
->consumer
->enabled
) {
1043 /* For each consumer socket, send relayd sockets */
1044 cds_lfht_for_each_entry (
1045 usess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1046 pthread_mutex_lock(socket
->lock
);
1047 ret
= send_consumer_relayd_sockets(*session
, usess
->consumer
, socket
, NULL
,
1048 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
1049 pthread_mutex_unlock(socket
->lock
);
1050 if (ret
!= LTTNG_OK
) {
1053 /* Session is now ready for network streaming. */
1054 session
->net_handle
= 1;
1056 session
->consumer
->relay_major_version
=
1057 usess
->consumer
->relay_major_version
;
1058 session
->consumer
->relay_minor_version
=
1059 usess
->consumer
->relay_minor_version
;
1060 session
->consumer
->relay_allows_clear
=
1061 usess
->consumer
->relay_allows_clear
;
1064 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1065 && ksess
->consumer
->enabled
) {
1066 cds_lfht_for_each_entry (
1067 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1068 pthread_mutex_lock(socket
->lock
);
1069 ret
= send_consumer_relayd_sockets(*session
, ksess
->consumer
, socket
, NULL
,
1070 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
1071 pthread_mutex_unlock(socket
->lock
);
1072 if (ret
!= LTTNG_OK
) {
1075 /* Session is now ready for network streaming. */
1076 session
->net_handle
= 1;
1078 session
->consumer
->relay_major_version
=
1079 ksess
->consumer
->relay_major_version
;
1080 session
->consumer
->relay_minor_version
=
1081 ksess
->consumer
->relay_minor_version
;
1082 session
->consumer
->relay_allows_clear
=
1083 ksess
->consumer
->relay_allows_clear
;
1092 * Start a kernel session by opening all necessary streams.
1094 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1097 struct ltt_kernel_channel
*kchan
;
1099 /* Open kernel metadata */
1100 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1101 ret
= kernel_open_metadata(ksess
);
1103 ret
= LTTNG_ERR_KERN_META_FAIL
;
1108 /* Open kernel metadata stream */
1109 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1110 ret
= kernel_open_metadata_stream(ksess
);
1112 ERR("Kernel create metadata stream failed");
1113 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1118 /* For each channel */
1119 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1120 if (kchan
->stream_count
== 0) {
1121 ret
= kernel_open_channel_stream(kchan
);
1123 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1126 /* Update the stream global counter */
1127 ksess
->stream_count_global
+= ret
;
1131 /* Setup kernel consumer socket and send fds to it */
1132 ret
= init_kernel_tracing(ksess
);
1134 ret
= LTTNG_ERR_KERN_START_FAIL
;
1138 /* This start the kernel tracing */
1139 ret
= kernel_start_session(ksess
);
1141 ret
= LTTNG_ERR_KERN_START_FAIL
;
1145 /* Quiescent wait after starting trace */
1146 kernel_wait_quiescent();
1156 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1158 struct ltt_kernel_channel
*kchan
;
1159 bool error_occurred
= false;
1162 if (!ksess
|| !ksess
->active
) {
1165 DBG("Stopping kernel tracing");
1167 ret
= kernel_stop_session(ksess
);
1169 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1173 kernel_wait_quiescent();
1175 /* Flush metadata after stopping (if exists) */
1176 if (ksess
->metadata_stream_fd
>= 0) {
1177 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1179 ERR("Kernel metadata flush failed");
1180 error_occurred
= true;
1184 /* Flush all buffers after stopping */
1185 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1186 ret
= kernel_flush_buffer(kchan
);
1188 ERR("Kernel flush buffer error");
1189 error_occurred
= true;
1194 if (error_occurred
) {
1195 ret
= LTTNG_ERR_UNK
;
1204 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1206 int cmd_disable_channel(struct ltt_session
*session
,
1207 enum lttng_domain_type domain
, char *channel_name
)
1210 struct ltt_ust_session
*usess
;
1212 usess
= session
->ust_session
;
1217 case LTTNG_DOMAIN_KERNEL
:
1219 ret
= channel_kernel_disable(session
->kernel_session
,
1221 if (ret
!= LTTNG_OK
) {
1225 kernel_wait_quiescent();
1228 case LTTNG_DOMAIN_UST
:
1230 struct ltt_ust_channel
*uchan
;
1231 struct lttng_ht
*chan_ht
;
1233 chan_ht
= usess
->domain_global
.channels
;
1235 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1236 if (uchan
== NULL
) {
1237 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1241 ret
= channel_ust_disable(usess
, uchan
);
1242 if (ret
!= LTTNG_OK
) {
1248 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1260 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1262 * The wpipe arguments is used as a notifier for the kernel thread.
1264 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1268 ssize_t sock_recv_len
;
1269 struct lttng_channel
*channel
= NULL
;
1270 struct lttng_buffer_view view
;
1271 struct lttng_dynamic_buffer channel_buffer
;
1272 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1274 lttng_dynamic_buffer_init(&channel_buffer
);
1275 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1276 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1278 ret
= LTTNG_ERR_NOMEM
;
1282 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1284 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1285 ERR("Failed to receive \"enable channel\" command payload");
1286 ret
= LTTNG_ERR_INVALID
;
1290 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1291 if (!lttng_buffer_view_is_valid(&view
)) {
1292 ret
= LTTNG_ERR_INVALID
;
1296 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1297 ERR("Invalid channel payload received in \"enable channel\" command");
1298 ret
= LTTNG_ERR_INVALID
;
1302 ret
= cmd_enable_channel_internal(
1303 cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1306 lttng_dynamic_buffer_reset(&channel_buffer
);
1307 lttng_channel_destroy(channel
);
1311 static enum lttng_error_code
kernel_domain_check_trace_format_requirements(
1312 const lttng::trace_format_descriptor
& descriptor
)
1314 enum lttng_error_code ret_code
= LTTNG_OK
;
1315 switch (descriptor
.type()) {
1316 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1
:
1317 /* Supported by all kernel tracer. */
1319 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_2
:
1320 if (!kernel_supports_ctf2()) {
1321 ret_code
= LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_KERNEL_TRACER
;
1330 static enum lttng_error_code
cmd_enable_channel_internal(
1331 struct ltt_session
*session
,
1332 const struct lttng_domain
*domain
,
1333 const struct lttng_channel
*_attr
,
1336 enum lttng_error_code ret_code
;
1337 struct ltt_ust_session
*usess
= session
->ust_session
;
1338 struct lttng_ht
*chan_ht
;
1340 struct lttng_channel
*attr
= NULL
;
1342 LTTNG_ASSERT(session
);
1343 LTTNG_ASSERT(_attr
);
1344 LTTNG_ASSERT(domain
);
1346 attr
= lttng_channel_copy(_attr
);
1348 ret_code
= LTTNG_ERR_NOMEM
;
1352 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1354 /* Validate channel name */
1355 if (attr
->name
[0] == '.' ||
1356 memchr(attr
->name
, '/', len
) != NULL
) {
1357 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1361 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1366 * If the session is a live session, remove the switch timer, the
1367 * live timer does the same thing but sends also synchronisation
1368 * beacons for inactive streams.
1370 if (session
->live_timer
> 0) {
1371 attr
->attr
.live_timer_interval
= session
->live_timer
;
1372 attr
->attr
.switch_timer_interval
= 0;
1375 /* Check for feature support */
1376 switch (domain
->type
) {
1377 case LTTNG_DOMAIN_KERNEL
:
1379 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1380 /* Sampling position of buffer is not supported */
1381 WARN("Kernel tracer does not support buffer monitoring. "
1382 "Setting the monitor interval timer to 0 "
1383 "(disabled) for channel '%s' of session '%s'",
1384 attr
->name
, session
->name
);
1385 lttng_channel_set_monitor_timer_interval(attr
, 0);
1388 ret_code
= kernel_domain_check_trace_format_requirements(*session
->trace_format
);
1389 if (ret_code
!= LTTNG_OK
) {
1390 WARN("Kernel tracer does not support the configured trace format of session '%s'",
1396 case LTTNG_DOMAIN_UST
:
1398 case LTTNG_DOMAIN_JUL
:
1399 case LTTNG_DOMAIN_LOG4J
:
1400 case LTTNG_DOMAIN_PYTHON
:
1401 if (!agent_tracing_is_enabled()) {
1402 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1403 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1408 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1412 switch (domain
->type
) {
1413 case LTTNG_DOMAIN_KERNEL
:
1415 struct ltt_kernel_channel
*kchan
;
1417 kchan
= trace_kernel_get_channel_by_name(
1418 attr
->name
, session
->kernel_session
);
1419 if (kchan
== NULL
) {
1421 * Don't try to create a channel if the session has been started at
1422 * some point in time before. The tracer does not allow it.
1424 if (session
->has_been_started
) {
1425 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1429 if (session
->snapshot
.nb_output
> 0 ||
1430 session
->snapshot_mode
) {
1431 /* Enforce mmap output for snapshot sessions. */
1432 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1434 ret_code
= channel_kernel_create(
1435 session
->kernel_session
, attr
, wpipe
);
1436 if (attr
->name
[0] != '\0') {
1437 session
->kernel_session
->has_non_default_channel
= 1;
1440 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1443 if (ret_code
!= LTTNG_OK
) {
1447 kernel_wait_quiescent();
1450 case LTTNG_DOMAIN_UST
:
1451 case LTTNG_DOMAIN_JUL
:
1452 case LTTNG_DOMAIN_LOG4J
:
1453 case LTTNG_DOMAIN_PYTHON
:
1455 struct ltt_ust_channel
*uchan
;
1460 * Current agent implementation limitations force us to allow
1461 * only one channel at once in "agent" subdomains. Each
1462 * subdomain has a default channel name which must be strictly
1465 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1466 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1467 LTTNG_SYMBOL_NAME_LEN
)) {
1468 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1471 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1472 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1473 LTTNG_SYMBOL_NAME_LEN
)) {
1474 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1477 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1478 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1479 LTTNG_SYMBOL_NAME_LEN
)) {
1480 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1485 chan_ht
= usess
->domain_global
.channels
;
1487 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1488 if (uchan
== NULL
) {
1490 * Don't try to create a channel if the session has been started at
1491 * some point in time before. The tracer does not allow it.
1493 if (session
->has_been_started
) {
1494 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1498 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1499 if (attr
->name
[0] != '\0') {
1500 usess
->has_non_default_channel
= 1;
1503 ret_code
= channel_ust_enable(usess
, uchan
);
1508 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1512 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1513 session
->has_non_mmap_channel
= true;
1518 lttng_channel_destroy(attr
);
1522 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1523 struct ltt_session
*session
,
1524 enum lttng_domain_type domain
,
1525 enum lttng_process_attr process_attr
,
1526 enum lttng_tracking_policy
*policy
)
1528 enum lttng_error_code ret_code
= LTTNG_OK
;
1529 const struct process_attr_tracker
*tracker
;
1532 case LTTNG_DOMAIN_KERNEL
:
1533 if (!session
->kernel_session
) {
1534 ret_code
= LTTNG_ERR_INVALID
;
1537 tracker
= kernel_get_process_attr_tracker(
1538 session
->kernel_session
, process_attr
);
1540 case LTTNG_DOMAIN_UST
:
1541 if (!session
->ust_session
) {
1542 ret_code
= LTTNG_ERR_INVALID
;
1545 tracker
= trace_ust_get_process_attr_tracker(
1546 session
->ust_session
, process_attr
);
1549 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1553 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1555 ret_code
= LTTNG_ERR_INVALID
;
1561 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1562 struct ltt_session
*session
,
1563 enum lttng_domain_type domain
,
1564 enum lttng_process_attr process_attr
,
1565 enum lttng_tracking_policy policy
)
1567 enum lttng_error_code ret_code
= LTTNG_OK
;
1570 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1571 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1572 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1575 ret_code
= LTTNG_ERR_INVALID
;
1580 case LTTNG_DOMAIN_KERNEL
:
1581 if (!session
->kernel_session
) {
1582 ret_code
= LTTNG_ERR_INVALID
;
1585 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1586 session
->kernel_session
, process_attr
, policy
);
1588 case LTTNG_DOMAIN_UST
:
1589 if (!session
->ust_session
) {
1590 ret_code
= LTTNG_ERR_INVALID
;
1593 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1594 session
->ust_session
, process_attr
, policy
);
1597 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1604 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1605 struct ltt_session
*session
,
1606 enum lttng_domain_type domain
,
1607 enum lttng_process_attr process_attr
,
1608 const struct process_attr_value
*value
)
1610 enum lttng_error_code ret_code
= LTTNG_OK
;
1613 case LTTNG_DOMAIN_KERNEL
:
1614 if (!session
->kernel_session
) {
1615 ret_code
= LTTNG_ERR_INVALID
;
1618 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1619 session
->kernel_session
, process_attr
, value
);
1621 case LTTNG_DOMAIN_UST
:
1622 if (!session
->ust_session
) {
1623 ret_code
= LTTNG_ERR_INVALID
;
1626 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1627 session
->ust_session
, process_attr
, value
);
1630 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1637 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1638 struct ltt_session
*session
,
1639 enum lttng_domain_type domain
,
1640 enum lttng_process_attr process_attr
,
1641 const struct process_attr_value
*value
)
1643 enum lttng_error_code ret_code
= LTTNG_OK
;
1646 case LTTNG_DOMAIN_KERNEL
:
1647 if (!session
->kernel_session
) {
1648 ret_code
= LTTNG_ERR_INVALID
;
1651 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1652 session
->kernel_session
, process_attr
, value
);
1654 case LTTNG_DOMAIN_UST
:
1655 if (!session
->ust_session
) {
1656 ret_code
= LTTNG_ERR_INVALID
;
1659 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1660 session
->ust_session
, process_attr
, value
);
1663 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1670 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1671 struct ltt_session
*session
,
1672 enum lttng_domain_type domain
,
1673 enum lttng_process_attr process_attr
,
1674 struct lttng_process_attr_values
**values
)
1676 enum lttng_error_code ret_code
= LTTNG_OK
;
1677 const struct process_attr_tracker
*tracker
;
1678 enum process_attr_tracker_status status
;
1681 case LTTNG_DOMAIN_KERNEL
:
1682 if (!session
->kernel_session
) {
1683 ret_code
= LTTNG_ERR_INVALID
;
1686 tracker
= kernel_get_process_attr_tracker(
1687 session
->kernel_session
, process_attr
);
1689 case LTTNG_DOMAIN_UST
:
1690 if (!session
->ust_session
) {
1691 ret_code
= LTTNG_ERR_INVALID
;
1694 tracker
= trace_ust_get_process_attr_tracker(
1695 session
->ust_session
, process_attr
);
1698 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1703 ret_code
= LTTNG_ERR_INVALID
;
1707 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1709 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1710 ret_code
= LTTNG_OK
;
1712 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1713 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1715 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1716 ret_code
= LTTNG_ERR_NOMEM
;
1719 ret_code
= LTTNG_ERR_UNK
;
1728 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1730 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1731 struct lttng_event
*event
,
1732 char *filter_expression
,
1733 struct lttng_bytecode
*bytecode
,
1734 struct lttng_event_exclusion
*exclusion
)
1737 const char *event_name
;
1738 const struct ltt_session
*session
= cmd_ctx
->session
;
1739 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1740 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1742 DBG("Disable event command for event \'%s\'", event
->name
);
1745 * Filter and exclusions are simply not handled by the
1746 * disable event command at this time.
1750 (void) filter_expression
;
1753 /* Ignore the presence of filter or exclusion for the event */
1755 event
->exclusion
= 0;
1757 event_name
= event
->name
;
1759 /* Error out on unhandled search criteria */
1760 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1761 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1762 ret
= LTTNG_ERR_UNK
;
1769 case LTTNG_DOMAIN_KERNEL
:
1771 struct ltt_kernel_channel
*kchan
;
1772 struct ltt_kernel_session
*ksess
;
1774 ksess
= session
->kernel_session
;
1777 * If a non-default channel has been created in the
1778 * session, explicitely require that -c chan_name needs
1781 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1782 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1786 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1787 if (kchan
== NULL
) {
1788 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1792 switch (event
->type
) {
1793 case LTTNG_EVENT_ALL
:
1794 case LTTNG_EVENT_TRACEPOINT
:
1795 case LTTNG_EVENT_SYSCALL
:
1796 case LTTNG_EVENT_PROBE
:
1797 case LTTNG_EVENT_FUNCTION
:
1798 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1799 if (event_name
[0] == '\0') {
1800 ret
= event_kernel_disable_event(kchan
,
1803 ret
= event_kernel_disable_event(kchan
,
1804 event_name
, event
->type
);
1806 if (ret
!= LTTNG_OK
) {
1811 ret
= LTTNG_ERR_UNK
;
1815 kernel_wait_quiescent();
1818 case LTTNG_DOMAIN_UST
:
1820 struct ltt_ust_channel
*uchan
;
1821 struct ltt_ust_session
*usess
;
1823 usess
= session
->ust_session
;
1825 if (validate_ust_event_name(event_name
)) {
1826 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1831 * If a non-default channel has been created in the
1832 * session, explicitly require that -c chan_name needs
1835 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1836 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1840 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1842 if (uchan
== NULL
) {
1843 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1847 switch (event
->type
) {
1848 case LTTNG_EVENT_ALL
:
1850 * An empty event name means that everything
1851 * should be disabled.
1853 if (event
->name
[0] == '\0') {
1854 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1856 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1859 if (ret
!= LTTNG_OK
) {
1864 ret
= LTTNG_ERR_UNK
;
1868 DBG3("Disable UST event %s in channel %s completed", event_name
,
1872 case LTTNG_DOMAIN_LOG4J
:
1873 case LTTNG_DOMAIN_JUL
:
1874 case LTTNG_DOMAIN_PYTHON
:
1877 struct ltt_ust_session
*usess
= session
->ust_session
;
1879 LTTNG_ASSERT(usess
);
1881 switch (event
->type
) {
1882 case LTTNG_EVENT_ALL
:
1885 ret
= LTTNG_ERR_UNK
;
1889 agt
= trace_ust_find_agent(usess
, domain
);
1891 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1895 * An empty event name means that everything
1896 * should be disabled.
1898 if (event
->name
[0] == '\0') {
1899 ret
= event_agent_disable_all(usess
, agt
);
1901 ret
= event_agent_disable(usess
, agt
, event_name
);
1903 if (ret
!= LTTNG_OK
) {
1910 ret
= LTTNG_ERR_UND
;
1921 free(filter_expression
);
1926 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1928 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1929 const struct lttng_event_context
*event_context
, int kwpipe
)
1931 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1932 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1933 const struct ltt_session
*session
= cmd_ctx
->session
;
1934 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1937 * Don't try to add a context if the session has been started at
1938 * some point in time before. The tracer does not allow it and would
1939 * result in a corrupted trace.
1941 if (cmd_ctx
->session
->has_been_started
) {
1942 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1947 case LTTNG_DOMAIN_KERNEL
:
1948 LTTNG_ASSERT(session
->kernel_session
);
1950 if (session
->kernel_session
->channel_count
== 0) {
1951 /* Create default channel */
1952 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1953 if (ret
!= LTTNG_OK
) {
1956 chan_kern_created
= 1;
1958 /* Add kernel context to kernel tracer */
1959 ret
= context_kernel_add(session
->kernel_session
,
1960 event_context
, channel_name
);
1961 if (ret
!= LTTNG_OK
) {
1965 case LTTNG_DOMAIN_JUL
:
1966 case LTTNG_DOMAIN_LOG4J
:
1969 * Validate channel name.
1970 * If no channel name is given and the domain is JUL or LOG4J,
1971 * set it to the appropriate domain-specific channel name. If
1972 * a name is provided but does not match the expexted channel
1973 * name, return an error.
1975 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1976 strcmp(channel_name
,
1977 DEFAULT_JUL_CHANNEL_NAME
)) {
1978 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1980 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1981 strcmp(channel_name
,
1982 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1983 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1988 case LTTNG_DOMAIN_UST
:
1990 struct ltt_ust_session
*usess
= session
->ust_session
;
1991 unsigned int chan_count
;
1993 LTTNG_ASSERT(usess
);
1995 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1996 if (chan_count
== 0) {
1997 struct lttng_channel
*attr
;
1998 /* Create default channel */
1999 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
2001 ret
= LTTNG_ERR_FATAL
;
2005 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
2006 if (ret
!= LTTNG_OK
) {
2010 channel_attr_destroy(attr
);
2011 chan_ust_created
= 1;
2014 ret
= context_ust_add(usess
, domain
, event_context
,
2016 if (ret
!= LTTNG_OK
) {
2022 ret
= LTTNG_ERR_UND
;
2030 if (chan_kern_created
) {
2031 struct ltt_kernel_channel
*kchan
=
2032 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2033 session
->kernel_session
);
2034 /* Created previously, this should NOT fail. */
2035 LTTNG_ASSERT(kchan
);
2036 kernel_destroy_channel(kchan
);
2039 if (chan_ust_created
) {
2040 struct ltt_ust_channel
*uchan
=
2041 trace_ust_find_channel_by_name(
2042 session
->ust_session
->domain_global
.channels
,
2043 DEFAULT_CHANNEL_NAME
);
2044 /* Created previously, this should NOT fail. */
2045 LTTNG_ASSERT(uchan
);
2046 /* Remove from the channel list of the session. */
2047 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2049 trace_ust_destroy_channel(uchan
);
2055 static inline bool name_starts_with(const char *name
, const char *prefix
)
2057 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2059 return !strncmp(name
, prefix
, max_cmp_len
);
2062 /* Perform userspace-specific event name validation */
2063 static int validate_ust_event_name(const char *name
)
2073 * Check name against all internal UST event component namespaces used
2076 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2077 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2078 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2087 * Internal version of cmd_enable_event() with a supplemental
2088 * "internal_event" flag which is used to enable internal events which should
2089 * be hidden from clients. Such events are used in the agent implementation to
2090 * enable the events through which all "agent" events are funeled.
2092 static int _cmd_enable_event(struct ltt_session
*session
,
2093 const struct lttng_domain
*domain
,
2094 char *channel_name
, struct lttng_event
*event
,
2095 char *filter_expression
,
2096 struct lttng_bytecode
*filter
,
2097 struct lttng_event_exclusion
*exclusion
,
2098 int wpipe
, bool internal_event
)
2100 int ret
= 0, channel_created
= 0;
2101 struct lttng_channel
*attr
= NULL
;
2103 LTTNG_ASSERT(session
);
2104 LTTNG_ASSERT(event
);
2105 LTTNG_ASSERT(channel_name
);
2107 /* If we have a filter, we must have its filter expression */
2108 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2110 /* Normalize event name as a globbing pattern */
2111 strutils_normalize_star_glob_pattern(event
->name
);
2113 /* Normalize exclusion names as globbing patterns */
2117 for (i
= 0; i
< exclusion
->count
; i
++) {
2118 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2120 strutils_normalize_star_glob_pattern(name
);
2124 DBG("Enable event command for event \'%s\'", event
->name
);
2128 switch (domain
->type
) {
2129 case LTTNG_DOMAIN_KERNEL
:
2131 struct ltt_kernel_channel
*kchan
;
2134 * If a non-default channel has been created in the
2135 * session, explicitely require that -c chan_name needs
2138 if (session
->kernel_session
->has_non_default_channel
2139 && channel_name
[0] == '\0') {
2140 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2144 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2145 session
->kernel_session
);
2146 if (kchan
== NULL
) {
2147 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2148 LTTNG_BUFFER_GLOBAL
);
2150 ret
= LTTNG_ERR_FATAL
;
2153 if (lttng_strncpy(attr
->name
, channel_name
,
2154 sizeof(attr
->name
))) {
2155 ret
= LTTNG_ERR_INVALID
;
2159 ret
= cmd_enable_channel_internal(
2160 session
, domain
, attr
, wpipe
);
2161 if (ret
!= LTTNG_OK
) {
2164 channel_created
= 1;
2167 /* Get the newly created kernel channel pointer */
2168 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2169 session
->kernel_session
);
2170 if (kchan
== NULL
) {
2171 /* This sould not happen... */
2172 ret
= LTTNG_ERR_FATAL
;
2176 switch (event
->type
) {
2177 case LTTNG_EVENT_ALL
:
2179 char *filter_expression_a
= NULL
;
2180 struct lttng_bytecode
*filter_a
= NULL
;
2183 * We need to duplicate filter_expression and filter,
2184 * because ownership is passed to first enable
2187 if (filter_expression
) {
2188 filter_expression_a
= strdup(filter_expression
);
2189 if (!filter_expression_a
) {
2190 ret
= LTTNG_ERR_FATAL
;
2195 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2197 free(filter_expression_a
);
2198 ret
= LTTNG_ERR_FATAL
;
2201 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2203 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2204 ret
= event_kernel_enable_event(kchan
, event
,
2205 filter_expression
, filter
);
2206 /* We have passed ownership */
2207 filter_expression
= NULL
;
2209 if (ret
!= LTTNG_OK
) {
2210 if (channel_created
) {
2211 /* Let's not leak a useless channel. */
2212 kernel_destroy_channel(kchan
);
2214 free(filter_expression_a
);
2218 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2219 ret
= event_kernel_enable_event(kchan
, event
,
2220 filter_expression_a
, filter_a
);
2221 /* We have passed ownership */
2222 filter_expression_a
= NULL
;
2224 if (ret
!= LTTNG_OK
) {
2229 case LTTNG_EVENT_PROBE
:
2230 case LTTNG_EVENT_USERSPACE_PROBE
:
2231 case LTTNG_EVENT_FUNCTION
:
2232 case LTTNG_EVENT_FUNCTION_ENTRY
:
2233 case LTTNG_EVENT_TRACEPOINT
:
2234 ret
= event_kernel_enable_event(kchan
, event
,
2235 filter_expression
, filter
);
2236 /* We have passed ownership */
2237 filter_expression
= NULL
;
2239 if (ret
!= LTTNG_OK
) {
2240 if (channel_created
) {
2241 /* Let's not leak a useless channel. */
2242 kernel_destroy_channel(kchan
);
2247 case LTTNG_EVENT_SYSCALL
:
2248 ret
= event_kernel_enable_event(kchan
, event
,
2249 filter_expression
, filter
);
2250 /* We have passed ownership */
2251 filter_expression
= NULL
;
2253 if (ret
!= LTTNG_OK
) {
2258 ret
= LTTNG_ERR_UNK
;
2262 kernel_wait_quiescent();
2265 case LTTNG_DOMAIN_UST
:
2267 struct ltt_ust_channel
*uchan
;
2268 struct ltt_ust_session
*usess
= session
->ust_session
;
2270 LTTNG_ASSERT(usess
);
2273 * If a non-default channel has been created in the
2274 * session, explicitely require that -c chan_name needs
2277 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2278 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2282 /* Get channel from global UST domain */
2283 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2285 if (uchan
== NULL
) {
2286 /* Create default channel */
2287 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2288 usess
->buffer_type
);
2290 ret
= LTTNG_ERR_FATAL
;
2293 if (lttng_strncpy(attr
->name
, channel_name
,
2294 sizeof(attr
->name
))) {
2295 ret
= LTTNG_ERR_INVALID
;
2299 ret
= cmd_enable_channel_internal(
2300 session
, domain
, attr
, wpipe
);
2301 if (ret
!= LTTNG_OK
) {
2305 /* Get the newly created channel reference back */
2306 uchan
= trace_ust_find_channel_by_name(
2307 usess
->domain_global
.channels
, channel_name
);
2308 LTTNG_ASSERT(uchan
);
2311 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2313 * Don't allow users to add UST events to channels which
2314 * are assigned to a userspace subdomain (JUL, Log4J,
2317 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2321 if (!internal_event
) {
2323 * Ensure the event name is not reserved for internal
2326 ret
= validate_ust_event_name(event
->name
);
2328 WARN("Userspace event name %s failed validation.",
2330 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2335 /* At this point, the session and channel exist on the tracer */
2336 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2337 filter_expression
, filter
, exclusion
,
2339 /* We have passed ownership */
2340 filter_expression
= NULL
;
2343 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2344 goto already_enabled
;
2345 } else if (ret
!= LTTNG_OK
) {
2350 case LTTNG_DOMAIN_LOG4J
:
2351 case LTTNG_DOMAIN_JUL
:
2352 case LTTNG_DOMAIN_PYTHON
:
2354 const char *default_event_name
, *default_chan_name
;
2356 struct lttng_event uevent
;
2357 struct lttng_domain tmp_dom
;
2358 struct ltt_ust_session
*usess
= session
->ust_session
;
2360 LTTNG_ASSERT(usess
);
2362 if (!agent_tracing_is_enabled()) {
2363 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2364 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2368 agt
= trace_ust_find_agent(usess
, domain
->type
);
2370 agt
= agent_create(domain
->type
);
2372 ret
= LTTNG_ERR_NOMEM
;
2375 agent_add(agt
, usess
->agents
);
2378 /* Create the default tracepoint. */
2379 memset(&uevent
, 0, sizeof(uevent
));
2380 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2381 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2382 default_event_name
= event_get_default_agent_ust_name(
2384 if (!default_event_name
) {
2385 ret
= LTTNG_ERR_FATAL
;
2388 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2389 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2392 * The domain type is changed because we are about to enable the
2393 * default channel and event for the JUL domain that are hardcoded.
2394 * This happens in the UST domain.
2396 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2397 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2399 switch (domain
->type
) {
2400 case LTTNG_DOMAIN_LOG4J
:
2401 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2403 case LTTNG_DOMAIN_JUL
:
2404 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2406 case LTTNG_DOMAIN_PYTHON
:
2407 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2410 /* The switch/case we are in makes this impossible */
2415 char *filter_expression_copy
= NULL
;
2416 struct lttng_bytecode
*filter_copy
= NULL
;
2419 const size_t filter_size
= sizeof(
2420 struct lttng_bytecode
)
2423 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2425 ret
= LTTNG_ERR_NOMEM
;
2428 memcpy(filter_copy
, filter
, filter_size
);
2430 filter_expression_copy
=
2431 strdup(filter_expression
);
2432 if (!filter_expression
) {
2433 ret
= LTTNG_ERR_NOMEM
;
2436 if (!filter_expression_copy
|| !filter_copy
) {
2437 free(filter_expression_copy
);
2443 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2444 (char *) default_chan_name
,
2445 &uevent
, filter_expression_copy
,
2446 filter_copy
, NULL
, wpipe
);
2449 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2450 goto already_enabled
;
2451 } else if (ret
!= LTTNG_OK
) {
2455 /* The wild card * means that everything should be enabled. */
2456 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2457 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2460 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2464 filter_expression
= NULL
;
2465 if (ret
!= LTTNG_OK
) {
2472 ret
= LTTNG_ERR_UND
;
2480 free(filter_expression
);
2483 channel_attr_destroy(attr
);
2489 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2490 * We own filter, exclusion, and filter_expression.
2492 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2493 struct lttng_event
*event
,
2494 char *filter_expression
,
2495 struct lttng_event_exclusion
*exclusion
,
2496 struct lttng_bytecode
*bytecode
,
2501 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2503 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2506 * The ownership of the following parameters is transferred to
2507 * _cmd_enable_event:
2509 * - filter_expression,
2513 ret
= _cmd_enable_event(cmd_ctx
->session
,
2515 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2516 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2517 filter_expression
= NULL
;
2524 * Enable an event which is internal to LTTng. An internal should
2525 * never be made visible to clients and are immune to checks such as
2528 static int cmd_enable_event_internal(struct ltt_session
*session
,
2529 const struct lttng_domain
*domain
,
2530 char *channel_name
, struct lttng_event
*event
,
2531 char *filter_expression
,
2532 struct lttng_bytecode
*filter
,
2533 struct lttng_event_exclusion
*exclusion
,
2536 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2537 filter_expression
, filter
, exclusion
, wpipe
, true);
2541 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2543 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2544 struct lttng_payload
*reply_payload
)
2546 enum lttng_error_code ret_code
;
2548 ssize_t i
, nb_events
= 0;
2549 struct lttng_event
*events
= NULL
;
2550 struct lttcomm_list_command_header reply_command_header
= {};
2551 size_t reply_command_header_offset
;
2553 assert(reply_payload
);
2555 /* Reserve space for command reply header. */
2556 reply_command_header_offset
= reply_payload
->buffer
.size
;
2557 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2558 reply_command_header_offset
+
2559 sizeof(struct lttcomm_list_command_header
));
2561 ret_code
= LTTNG_ERR_NOMEM
;
2566 case LTTNG_DOMAIN_KERNEL
:
2567 nb_events
= kernel_list_events(&events
);
2568 if (nb_events
< 0) {
2569 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2573 case LTTNG_DOMAIN_UST
:
2574 nb_events
= ust_app_list_events(&events
);
2575 if (nb_events
< 0) {
2576 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2580 case LTTNG_DOMAIN_LOG4J
:
2581 case LTTNG_DOMAIN_JUL
:
2582 case LTTNG_DOMAIN_PYTHON
:
2583 nb_events
= agent_list_events(&events
, domain
);
2584 if (nb_events
< 0) {
2585 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2590 ret_code
= LTTNG_ERR_UND
;
2594 for (i
= 0; i
< nb_events
; i
++) {
2595 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2598 ret_code
= LTTNG_ERR_NOMEM
;
2603 if (nb_events
> UINT32_MAX
) {
2604 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2605 ret_code
= LTTNG_ERR_OVERFLOW
;
2609 /* Update command reply header. */
2610 reply_command_header
.count
= (uint32_t) nb_events
;
2611 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2612 sizeof(reply_command_header
));
2614 ret_code
= LTTNG_OK
;
2621 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2623 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2624 struct lttng_payload
*reply
)
2626 enum lttng_error_code ret_code
;
2628 unsigned int i
, nb_fields
;
2629 struct lttng_event_field
*fields
= NULL
;
2630 struct lttcomm_list_command_header reply_command_header
= {};
2631 size_t reply_command_header_offset
;
2635 /* Reserve space for command reply header. */
2636 reply_command_header_offset
= reply
->buffer
.size
;
2637 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2638 reply_command_header_offset
+
2639 sizeof(struct lttcomm_list_command_header
));
2641 ret_code
= LTTNG_ERR_NOMEM
;
2646 case LTTNG_DOMAIN_UST
:
2647 ret
= ust_app_list_event_fields(&fields
);
2649 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2654 case LTTNG_DOMAIN_KERNEL
:
2655 default: /* fall-through */
2656 ret_code
= LTTNG_ERR_UND
;
2662 for (i
= 0; i
< nb_fields
; i
++) {
2663 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2665 ret_code
= LTTNG_ERR_NOMEM
;
2670 if (nb_fields
> UINT32_MAX
) {
2671 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2672 ret_code
= LTTNG_ERR_OVERFLOW
;
2676 /* Update command reply header. */
2677 reply_command_header
.count
= (uint32_t) nb_fields
;
2679 memcpy(reply
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2680 sizeof(reply_command_header
));
2682 ret_code
= LTTNG_OK
;
2689 enum lttng_error_code
cmd_list_syscalls(
2690 struct lttng_payload
*reply_payload
)
2692 enum lttng_error_code ret_code
;
2693 ssize_t nb_events
, i
;
2695 struct lttng_event
*events
= NULL
;
2696 struct lttcomm_list_command_header reply_command_header
= {};
2697 size_t reply_command_header_offset
;
2699 assert(reply_payload
);
2701 /* Reserve space for command reply header. */
2702 reply_command_header_offset
= reply_payload
->buffer
.size
;
2703 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2704 reply_command_header_offset
+
2705 sizeof(struct lttcomm_list_command_header
));
2707 ret_code
= LTTNG_ERR_NOMEM
;
2711 nb_events
= syscall_table_list(&events
);
2712 if (nb_events
< 0) {
2713 ret_code
= (enum lttng_error_code
) -nb_events
;
2717 for (i
= 0; i
< nb_events
; i
++) {
2718 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2721 ret_code
= LTTNG_ERR_NOMEM
;
2726 if (nb_events
> UINT32_MAX
) {
2727 ERR("Syscall count would overflow the syscall listing command's reply");
2728 ret_code
= LTTNG_ERR_OVERFLOW
;
2732 /* Update command reply header. */
2733 reply_command_header
.count
= (uint32_t) nb_events
;
2734 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2735 sizeof(reply_command_header
));
2737 ret_code
= LTTNG_OK
;
2744 * Command LTTNG_START_TRACE processed by the client thread.
2746 * Called with session mutex held.
2748 int cmd_start_trace(struct ltt_session
*session
)
2750 enum lttng_error_code ret
;
2751 unsigned long nb_chan
= 0;
2752 struct ltt_kernel_session
*ksession
;
2753 struct ltt_ust_session
*usess
;
2754 const bool session_rotated_after_last_stop
=
2755 session
->rotated_after_last_stop
;
2756 const bool session_cleared_after_last_stop
=
2757 session
->cleared_after_last_stop
;
2759 LTTNG_ASSERT(session
);
2761 /* Ease our life a bit ;) */
2762 ksession
= session
->kernel_session
;
2763 usess
= session
->ust_session
;
2765 /* Is the session already started? */
2766 if (session
->active
) {
2767 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2768 /* Perform nothing */
2772 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2773 !session
->current_trace_chunk
) {
2775 * A rotation was launched while the session was stopped and
2776 * it has not been completed yet. It is not possible to start
2777 * the session since starting the session here would require a
2778 * rotation from "NULL" to a new trace chunk. That rotation
2779 * would overlap with the ongoing rotation, which is not
2782 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2784 ret
= LTTNG_ERR_ROTATION_PENDING
;
2789 * Starting a session without channel is useless since after that it's not
2790 * possible to enable channel thus inform the client.
2792 if (usess
&& usess
->domain_global
.channels
) {
2793 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2796 nb_chan
+= ksession
->channel_count
;
2799 ret
= LTTNG_ERR_NO_CHANNEL
;
2803 session
->active
= true;
2804 session
->rotated_after_last_stop
= false;
2805 session
->cleared_after_last_stop
= false;
2806 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2807 if (!session
->has_been_started
) {
2808 struct lttng_trace_chunk
*trace_chunk
;
2810 DBG("Creating initial trace chunk of session \"%s\"",
2812 trace_chunk
= session_create_new_trace_chunk(
2813 session
, NULL
, NULL
, NULL
);
2815 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2818 LTTNG_ASSERT(!session
->current_trace_chunk
);
2819 ret
= (lttng_error_code
) session_set_trace_chunk(session
, trace_chunk
,
2821 lttng_trace_chunk_put(trace_chunk
);
2823 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2827 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2830 * Rotate existing streams into the new chunk.
2831 * This is a "quiet" rotation has no client has
2832 * explicitly requested this operation.
2834 * There is also no need to wait for the rotation
2835 * to complete as it will happen immediately. No data
2836 * was produced as the session was stopped, so the
2837 * rotation should happen on reception of the command.
2839 ret
= (lttng_error_code
) cmd_rotate_session(session
, NULL
, true,
2840 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2841 if (ret
!= LTTNG_OK
) {
2847 /* Kernel tracing */
2848 if (ksession
!= NULL
) {
2849 DBG("Start kernel tracing session %s", session
->name
);
2850 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2851 if (ret
!= LTTNG_OK
) {
2856 /* Flag session that trace should start automatically */
2858 int int_ret
= ust_app_start_trace_all(usess
);
2861 ret
= LTTNG_ERR_UST_START_FAIL
;
2867 * Open a packet in every stream of the session to ensure that viewers
2868 * can correctly identify the boundaries of the periods during which
2869 * tracing was active for this session.
2871 ret
= session_open_packets(session
);
2872 if (ret
!= LTTNG_OK
) {
2877 * Clear the flag that indicates that a rotation was done while the
2878 * session was stopped.
2880 session
->rotated_after_last_stop
= false;
2882 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2883 int int_ret
= timer_session_rotation_schedule_timer_start(
2884 session
, session
->rotate_timer_period
);
2887 ERR("Failed to enable rotate timer");
2888 ret
= LTTNG_ERR_UNK
;
2896 if (ret
== LTTNG_OK
) {
2897 /* Flag this after a successful start. */
2898 session
->has_been_started
= true;
2900 session
->active
= false;
2901 /* Restore initial state on error. */
2902 session
->rotated_after_last_stop
=
2903 session_rotated_after_last_stop
;
2904 session
->cleared_after_last_stop
=
2905 session_cleared_after_last_stop
;
2912 * Command LTTNG_STOP_TRACE processed by the client thread.
2914 int cmd_stop_trace(struct ltt_session
*session
)
2917 struct ltt_kernel_session
*ksession
;
2918 struct ltt_ust_session
*usess
;
2920 LTTNG_ASSERT(session
);
2922 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2924 ksession
= session
->kernel_session
;
2925 usess
= session
->ust_session
;
2927 /* Session is not active. Skip everything and inform the client. */
2928 if (!session
->active
) {
2929 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2933 ret
= stop_kernel_session(ksession
);
2934 if (ret
!= LTTNG_OK
) {
2938 if (usess
&& usess
->active
) {
2939 ret
= ust_app_stop_trace_all(usess
);
2941 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2946 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2948 /* Flag inactive after a successful stop. */
2949 session
->active
= 0;
2957 * Set the base_path of the session only if subdir of a control uris is set.
2958 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2960 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2962 struct lttng_uri
*uris
)
2967 for (i
= 0; i
< nb_uri
; i
++) {
2968 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2969 uris
[i
].subdir
[0] == '\0') {
2970 /* Not interested in these URIs */
2974 if (session
->base_path
!= NULL
) {
2975 free(session
->base_path
);
2976 session
->base_path
= NULL
;
2979 /* Set session base_path */
2980 session
->base_path
= strdup(uris
[i
].subdir
);
2981 if (!session
->base_path
) {
2982 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2983 uris
[i
].subdir
, session
->name
);
2984 ret
= LTTNG_ERR_NOMEM
;
2987 DBG2("Setting base path \"%s\" for session \"%s\"",
2988 session
->base_path
, session
->name
);
2996 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2998 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2999 struct lttng_uri
*uris
)
3002 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3003 struct ltt_ust_session
*usess
= session
->ust_session
;
3005 LTTNG_ASSERT(session
);
3007 LTTNG_ASSERT(nb_uri
> 0);
3009 /* Can't set consumer URI if the session is active. */
3010 if (session
->active
) {
3011 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
3016 * Set the session base path if any. This is done inside
3017 * cmd_set_consumer_uri to preserve backward compatibility of the
3018 * previous session creation api vs the session descriptor api.
3020 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
3021 if (ret
!= LTTNG_OK
) {
3025 /* Set the "global" consumer URIs */
3026 for (i
= 0; i
< nb_uri
; i
++) {
3027 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
3029 if (ret
!= LTTNG_OK
) {
3034 /* Set UST session URIs */
3035 if (session
->ust_session
) {
3036 for (i
= 0; i
< nb_uri
; i
++) {
3037 ret
= add_uri_to_consumer(session
,
3038 session
->ust_session
->consumer
,
3039 &uris
[i
], LTTNG_DOMAIN_UST
);
3040 if (ret
!= LTTNG_OK
) {
3046 /* Set kernel session URIs */
3047 if (session
->kernel_session
) {
3048 for (i
= 0; i
< nb_uri
; i
++) {
3049 ret
= add_uri_to_consumer(session
,
3050 session
->kernel_session
->consumer
,
3051 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3052 if (ret
!= LTTNG_OK
) {
3059 * Make sure to set the session in output mode after we set URI since a
3060 * session can be created without URL (thus flagged in no output mode).
3062 session
->output_traces
= 1;
3064 ksess
->output_traces
= 1;
3068 usess
->output_traces
= 1;
3079 enum lttng_error_code
set_session_output_from_descriptor(
3080 struct ltt_session
*session
,
3081 const struct lttng_session_descriptor
*descriptor
)
3084 enum lttng_error_code ret_code
= LTTNG_OK
;
3085 enum lttng_session_descriptor_type session_type
=
3086 lttng_session_descriptor_get_type(descriptor
);
3087 enum lttng_session_descriptor_output_type output_type
=
3088 lttng_session_descriptor_get_output_type(descriptor
);
3089 struct lttng_uri uris
[2] = {};
3090 size_t uri_count
= 0;
3092 switch (output_type
) {
3093 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3095 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3096 lttng_session_descriptor_get_local_output_uri(descriptor
,
3100 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3101 lttng_session_descriptor_get_network_output_uris(descriptor
,
3102 &uris
[0], &uris
[1]);
3106 ret_code
= LTTNG_ERR_INVALID
;
3110 switch (session_type
) {
3111 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3113 struct snapshot_output
*new_output
= NULL
;
3115 new_output
= snapshot_output_alloc();
3117 ret_code
= LTTNG_ERR_NOMEM
;
3121 ret
= snapshot_output_init_with_uri(session
,
3122 DEFAULT_SNAPSHOT_MAX_SIZE
,
3123 NULL
, uris
, uri_count
, session
->consumer
,
3124 new_output
, &session
->snapshot
);
3126 ret_code
= (ret
== -ENOMEM
) ?
3127 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3128 snapshot_output_destroy(new_output
);
3131 snapshot_add_output(&session
->snapshot
, new_output
);
3134 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3135 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3137 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3141 ret_code
= LTTNG_ERR_INVALID
;
3149 enum lttng_error_code
cmd_create_session_from_descriptor(
3150 struct lttng_session_descriptor
*descriptor
,
3151 const lttng_sock_cred
*creds
,
3152 const char *home_path
)
3155 enum lttng_error_code ret_code
;
3156 const char *session_name
;
3157 struct ltt_session
*new_session
= NULL
;
3158 enum lttng_session_descriptor_status descriptor_status
;
3159 const lttng_trace_format_descriptor
*trace_format_descriptor
= NULL
;
3160 lttng::trace_format_descriptor::uptr trace_format_descriptor_ptr
;
3162 session_lock_list();
3164 if (*home_path
!= '/') {
3165 ERR("Home path provided by client is not absolute");
3166 ret_code
= LTTNG_ERR_INVALID
;
3171 descriptor_status
= lttng_session_descriptor_get_session_name(
3172 descriptor
, &session_name
);
3173 switch (descriptor_status
) {
3174 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3176 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3177 session_name
= NULL
;
3180 ret_code
= LTTNG_ERR_INVALID
;
3184 descriptor_status
= lttng_session_descriptor_get_trace_format_descriptor(
3185 descriptor
, &trace_format_descriptor
);
3186 if (descriptor_status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
3187 ret_code
= LTTNG_ERR_INVALID
;
3192 trace_format_descriptor_ptr
=
3193 reinterpret_cast<const lttng::trace_format_descriptor
*>(
3194 trace_format_descriptor
)
3196 } catch (std::exception
& e
) {
3197 ERR("%s", e
.what());
3198 ret_code
= LTTNG_ERR_UNK
;
3202 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, trace_format_descriptor_ptr
,
3204 if (ret_code
!= LTTNG_OK
) {
3208 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3209 new_session
->id
, new_session
->name
, new_session
->uid
, new_session
->gid
);
3210 if (ret_code
!= LTTNG_OK
) {
3214 /* Announce the session's destruction to the notification thread when it is destroyed. */
3215 ret
= session_add_destroy_notifier(
3217 [](const struct ltt_session
*session
,
3218 void *user_data
__attribute__((unused
))) {
3219 (void) notification_thread_command_remove_session(
3220 the_notification_thread_handle
, session
->id
);
3224 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3226 ret
= LTTNG_ERR_NOMEM
;
3230 if (!session_name
) {
3231 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3234 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3239 if (!lttng_session_descriptor_is_output_destination_initialized(
3242 * Only include the session's creation time in the output
3243 * destination if the name of the session itself was
3244 * not auto-generated.
3246 ret_code
= lttng_session_descriptor_set_default_output(
3248 session_name
? &new_session
->creation_time
: NULL
,
3250 if (ret_code
!= LTTNG_OK
) {
3254 new_session
->has_user_specified_directory
=
3255 lttng_session_descriptor_has_output_directory(
3259 switch (lttng_session_descriptor_get_type(descriptor
)) {
3260 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3261 new_session
->snapshot_mode
= 1;
3263 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3264 new_session
->live_timer
=
3265 lttng_session_descriptor_live_get_timer_interval(
3272 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3273 if (ret_code
!= LTTNG_OK
) {
3276 new_session
->consumer
->enabled
= 1;
3277 ret_code
= LTTNG_OK
;
3279 /* Release reference provided by the session_create function. */
3280 session_put(new_session
);
3281 if (ret_code
!= LTTNG_OK
&& new_session
) {
3282 /* Release the global reference on error. */
3283 session_destroy(new_session
);
3285 session_unlock_list();
3289 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3290 struct lttng_session_descriptor
**return_descriptor
)
3293 size_t payload_size
;
3294 struct lttng_dynamic_buffer payload
;
3295 struct lttng_buffer_view home_dir_view
;
3296 struct lttng_buffer_view session_descriptor_view
;
3297 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3298 enum lttng_error_code ret_code
;
3300 lttng_dynamic_buffer_init(&payload
);
3301 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3303 ret_code
= LTTNG_ERR_INVALID
;
3306 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3307 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3308 ret_code
= LTTNG_ERR_INVALID
;
3312 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3313 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3314 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3316 ret_code
= LTTNG_ERR_NOMEM
;
3320 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3322 ERR("Reception of session descriptor failed, aborting.");
3323 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3327 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3330 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3331 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3332 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3333 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3334 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3338 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3340 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3341 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3342 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3343 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3344 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3348 ret
= lttng_session_descriptor_create_from_buffer(
3349 &session_descriptor_view
, &session_descriptor
);
3351 ERR("Failed to create session descriptor from payload of \"create session\" command");
3352 ret_code
= LTTNG_ERR_INVALID
;
3357 * Sets the descriptor's auto-generated properties (name, output) if
3360 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3362 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3363 if (ret_code
!= LTTNG_OK
) {
3367 ret_code
= LTTNG_OK
;
3368 *return_descriptor
= session_descriptor
;
3369 session_descriptor
= NULL
;
3371 lttng_dynamic_buffer_reset(&payload
);
3372 lttng_session_descriptor_destroy(session_descriptor
);
3377 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3378 void *_reply_context
)
3382 const struct cmd_destroy_session_reply_context
*reply_context
=
3383 (cmd_destroy_session_reply_context
*) _reply_context
;
3384 struct lttng_dynamic_buffer payload
;
3385 struct lttcomm_session_destroy_command_header cmd_header
;
3386 struct lttng_trace_archive_location
*location
= NULL
;
3387 struct lttcomm_lttng_msg llm
= {
3388 .cmd_type
= LTTNG_DESTROY_SESSION
,
3389 .ret_code
= reply_context
->destruction_status
,
3392 sizeof(struct lttcomm_session_destroy_command_header
),
3396 size_t payload_size_before_location
;
3398 lttng_dynamic_buffer_init(&payload
);
3400 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3402 ERR("Failed to append session destruction message");
3406 cmd_header
.rotation_state
=
3407 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3408 session
->rotation_state
:
3409 LTTNG_ROTATION_STATE_NO_ROTATION
);
3410 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3411 sizeof(cmd_header
));
3413 ERR("Failed to append session destruction command header");
3417 if (!reply_context
->implicit_rotation_on_destroy
) {
3418 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3422 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3423 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3428 location
= session_get_trace_archive_location(session
);
3430 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3435 payload_size_before_location
= payload
.size
;
3436 comm_ret
= lttng_trace_archive_location_serialize(location
,
3438 lttng_trace_archive_location_put(location
);
3440 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3444 /* Update the message to indicate the location's length. */
3445 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3446 payload
.size
- payload_size_before_location
;
3448 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3449 payload
.data
, payload
.size
);
3450 if (comm_ret
!= (ssize_t
) payload
.size
) {
3451 ERR("Failed to send result of the destruction of session \"%s\" to client",
3455 ret
= close(reply_context
->reply_sock_fd
);
3457 PERROR("Failed to close client socket in deferred session destroy reply");
3459 lttng_dynamic_buffer_reset(&payload
);
3460 free(_reply_context
);
3464 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3466 * Called with session lock held.
3468 int cmd_destroy_session(struct ltt_session
*session
,
3469 struct notification_thread_handle
*notification_thread_handle
,
3473 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3474 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3477 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3478 if (!reply_context
) {
3479 ret
= LTTNG_ERR_NOMEM
;
3483 reply_context
->reply_sock_fd
= *sock_fd
;
3487 LTTNG_ASSERT(session
);
3489 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3491 if (session
->active
) {
3492 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3494 ret
= cmd_stop_trace(session
);
3495 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3496 /* Carry on with the destruction of the session. */
3497 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3498 session
->name
, lttng_strerror(-ret
));
3499 destruction_last_error
= (lttng_error_code
) ret
;
3503 if (session
->rotation_schedule_timer_enabled
) {
3504 if (timer_session_rotation_schedule_timer_stop(
3506 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3508 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3512 if (session
->rotate_size
) {
3513 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3514 session
->rotate_size
= 0;
3517 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3519 * Perform a last rotation on destruction if rotations have
3520 * occurred during the session's lifetime.
3522 ret
= cmd_rotate_session(session
, NULL
, false,
3523 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3524 if (ret
!= LTTNG_OK
) {
3525 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3526 session
->name
, lttng_strerror(-ret
));
3527 destruction_last_error
= (lttng_error_code
) -ret
;
3529 if (reply_context
) {
3530 reply_context
->implicit_rotation_on_destroy
= true;
3532 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3534 * The user has not triggered a session rotation. However, to
3535 * ensure all data has been consumed, the session is rotated
3536 * to a 'null' trace chunk before it is destroyed.
3538 * This is a "quiet" rotation meaning that no notification is
3539 * emitted and no renaming of the current trace chunk takes
3542 ret
= cmd_rotate_session(session
, NULL
, true,
3543 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3545 * Rotation operations may not be supported by the kernel
3546 * tracer. Hence, do not consider this implicit rotation as
3547 * a session destruction error. The library has already stopped
3548 * the session and waited for pending data; there is nothing
3549 * left to do but complete the destruction of the session.
3551 if (ret
!= LTTNG_OK
&&
3552 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3553 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3554 session
->name
, lttng_strerror(ret
));
3555 destruction_last_error
= (lttng_error_code
) -ret
;
3559 if (session
->shm_path
[0]) {
3561 * When a session is created with an explicit shm_path,
3562 * the consumer daemon will create its shared memory files
3563 * at that location and will *not* unlink them. This is normal
3564 * as the intention of that feature is to make it possible
3565 * to retrieve the content of those files should a crash occur.
3567 * To ensure the content of those files can be used, the
3568 * sessiond daemon will replicate the content of the metadata
3569 * cache in a metadata file.
3571 * On clean-up, it is expected that the consumer daemon will
3572 * unlink the shared memory files and that the session daemon
3573 * will unlink the metadata file. Then, the session's directory
3574 * in the shm path can be removed.
3576 * Unfortunately, a flaw in the design of the sessiond's and
3577 * consumerd's tear down of channels makes it impossible to
3578 * determine when the sessiond _and_ the consumerd have both
3579 * destroyed their representation of a channel. For one, the
3580 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3581 * callbacks in both daemons.
3583 * However, it is also impossible for the sessiond to know when
3584 * the consumer daemon is done destroying its channel(s) since
3585 * it occurs as a reaction to the closing of the channel's file
3586 * descriptor. There is no resulting communication initiated
3587 * from the consumerd to the sessiond to confirm that the
3588 * operation is completed (and was successful).
3590 * Until this is all fixed, the session daemon checks for the
3591 * removal of the session's shm path which makes it possible
3592 * to safely advertise a session as having been destroyed.
3594 * Prior to this fix, it was not possible to reliably save
3595 * a session making use of the --shm-path option, destroy it,
3596 * and load it again. This is because the creation of the
3597 * session would fail upon seeing the session's shm path
3598 * already in existence.
3600 * Note that none of the error paths in the check for the
3601 * directory's existence return an error. This is normal
3602 * as there isn't much that can be done. The session will
3603 * be destroyed properly, except that we can't offer the
3604 * guarantee that the same session can be re-created.
3606 current_completion_handler
= &destroy_completion_handler
.handler
;
3607 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3609 sizeof(destroy_completion_handler
.shm_path
));
3614 * The session is destroyed. However, note that the command context
3615 * still holds a reference to the session, thus delaying its destruction
3616 * _at least_ up to the point when that reference is released.
3618 session_destroy(session
);
3619 if (reply_context
) {
3620 reply_context
->destruction_status
= destruction_last_error
;
3621 ret
= session_add_destroy_notifier(session
,
3622 cmd_destroy_session_reply
,
3623 (void *) reply_context
);
3625 ret
= LTTNG_ERR_FATAL
;
3637 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3639 int cmd_register_consumer(struct ltt_session
*session
,
3640 enum lttng_domain_type domain
, const char *sock_path
,
3641 struct consumer_data
*cdata
)
3644 struct consumer_socket
*socket
= NULL
;
3646 LTTNG_ASSERT(session
);
3647 LTTNG_ASSERT(cdata
);
3648 LTTNG_ASSERT(sock_path
);
3651 case LTTNG_DOMAIN_KERNEL
:
3653 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3655 LTTNG_ASSERT(ksess
);
3657 /* Can't register a consumer if there is already one */
3658 if (ksess
->consumer_fds_sent
!= 0) {
3659 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3663 sock
= lttcomm_connect_unix_sock(sock_path
);
3665 ret
= LTTNG_ERR_CONNECT_FAIL
;
3668 cdata
->cmd_sock
= sock
;
3670 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3671 if (socket
== NULL
) {
3674 PERROR("close register consumer");
3676 cdata
->cmd_sock
= -1;
3677 ret
= LTTNG_ERR_FATAL
;
3681 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3682 if (socket
->lock
== NULL
) {
3683 PERROR("zmalloc pthread mutex");
3684 ret
= LTTNG_ERR_FATAL
;
3688 pthread_mutex_init(socket
->lock
, NULL
);
3689 socket
->registered
= 1;
3692 consumer_add_socket(socket
, ksess
->consumer
);
3695 pthread_mutex_lock(&cdata
->pid_mutex
);
3697 pthread_mutex_unlock(&cdata
->pid_mutex
);
3702 /* TODO: Userspace tracing */
3703 ret
= LTTNG_ERR_UND
;
3711 consumer_destroy_socket(socket
);
3717 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3719 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3720 struct lttng_domain
**domains
)
3725 struct lttng_ht_iter iter
;
3727 if (session
->kernel_session
!= NULL
) {
3728 DBG3("Listing domains found kernel domain");
3732 if (session
->ust_session
!= NULL
) {
3733 DBG3("Listing domains found UST global domain");
3737 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3739 if (agt
->being_used
) {
3750 *domains
= calloc
<lttng_domain
>(nb_dom
);
3751 if (*domains
== NULL
) {
3752 ret
= LTTNG_ERR_FATAL
;
3756 if (session
->kernel_session
!= NULL
) {
3757 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3759 /* Kernel session buffer type is always GLOBAL */
3760 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3765 if (session
->ust_session
!= NULL
) {
3766 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3767 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3771 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3773 if (agt
->being_used
) {
3774 (*domains
)[index
].type
= agt
->domain
;
3775 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3785 /* Return negative value to differentiate return code */
3791 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3793 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3794 struct ltt_session
*session
,
3795 struct lttng_payload
*payload
)
3799 struct lttcomm_list_command_header cmd_header
= {};
3800 size_t cmd_header_offset
;
3801 enum lttng_error_code ret_code
;
3806 DBG("Listing channels for session %s", session
->name
);
3808 cmd_header_offset
= payload
->buffer
.size
;
3810 /* Reserve space for command reply header. */
3811 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3812 cmd_header_offset
+ sizeof(cmd_header
));
3814 ret_code
= LTTNG_ERR_NOMEM
;
3819 case LTTNG_DOMAIN_KERNEL
:
3821 /* Kernel channels */
3822 struct ltt_kernel_channel
*kchan
;
3823 if (session
->kernel_session
!= NULL
) {
3824 cds_list_for_each_entry(kchan
,
3825 &session
->kernel_session
->channel_list
.head
, list
) {
3826 uint64_t discarded_events
, lost_packets
;
3827 struct lttng_channel_extended
*extended
;
3829 extended
= (struct lttng_channel_extended
*)
3830 kchan
->channel
->attr
.extended
.ptr
;
3832 ret
= get_kernel_runtime_stats(session
, kchan
,
3833 &discarded_events
, &lost_packets
);
3835 ret_code
= LTTNG_ERR_UNK
;
3840 * Update the discarded_events and lost_packets
3841 * count for the channel
3843 extended
->discarded_events
= discarded_events
;
3844 extended
->lost_packets
= lost_packets
;
3846 ret
= lttng_channel_serialize(
3847 kchan
->channel
, &payload
->buffer
);
3849 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3850 kchan
->channel
->name
);
3851 ret_code
= LTTNG_ERR_UNK
;
3860 case LTTNG_DOMAIN_UST
:
3862 struct lttng_ht_iter iter
;
3863 struct ltt_ust_channel
*uchan
;
3866 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3867 &iter
.iter
, uchan
, node
.node
) {
3868 uint64_t discarded_events
= 0, lost_packets
= 0;
3869 struct lttng_channel
*channel
= NULL
;
3870 struct lttng_channel_extended
*extended
;
3872 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3874 ret_code
= LTTNG_ERR_NOMEM
;
3878 extended
= (struct lttng_channel_extended
*)
3879 channel
->attr
.extended
.ptr
;
3881 ret
= get_ust_runtime_stats(session
, uchan
,
3882 &discarded_events
, &lost_packets
);
3884 lttng_channel_destroy(channel
);
3885 ret_code
= LTTNG_ERR_UNK
;
3889 extended
->discarded_events
= discarded_events
;
3890 extended
->lost_packets
= lost_packets
;
3892 ret
= lttng_channel_serialize(
3893 channel
, &payload
->buffer
);
3895 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3897 lttng_channel_destroy(channel
);
3898 ret_code
= LTTNG_ERR_UNK
;
3902 lttng_channel_destroy(channel
);
3912 if (i
> UINT32_MAX
) {
3913 ERR("Channel count would overflow the channel listing command's reply");
3914 ret_code
= LTTNG_ERR_OVERFLOW
;
3918 /* Update command reply header. */
3919 cmd_header
.count
= (uint32_t) i
;
3920 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3921 sizeof(cmd_header
));
3922 ret_code
= LTTNG_OK
;
3929 * Command LTTNG_LIST_EVENTS processed by the client thread.
3931 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3932 struct ltt_session
*session
,
3934 struct lttng_payload
*reply_payload
)
3936 int buffer_resize_ret
;
3937 enum lttng_error_code ret_code
= LTTNG_OK
;
3938 struct lttcomm_list_command_header reply_command_header
= {};
3939 size_t reply_command_header_offset
;
3940 unsigned int nb_events
= 0;
3942 assert(reply_payload
);
3944 /* Reserve space for command reply header. */
3945 reply_command_header_offset
= reply_payload
->buffer
.size
;
3946 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3947 reply_command_header_offset
+
3948 sizeof(struct lttcomm_list_command_header
));
3949 if (buffer_resize_ret
) {
3950 ret_code
= LTTNG_ERR_NOMEM
;
3955 case LTTNG_DOMAIN_KERNEL
:
3956 if (session
->kernel_session
!= NULL
) {
3957 ret_code
= list_lttng_kernel_events(channel_name
,
3958 session
->kernel_session
, reply_payload
, &nb_events
);
3962 case LTTNG_DOMAIN_UST
:
3964 if (session
->ust_session
!= NULL
) {
3965 ret_code
= list_lttng_ust_global_events(channel_name
,
3966 &session
->ust_session
->domain_global
,
3967 reply_payload
, &nb_events
);
3972 case LTTNG_DOMAIN_LOG4J
:
3973 case LTTNG_DOMAIN_JUL
:
3974 case LTTNG_DOMAIN_PYTHON
:
3975 if (session
->ust_session
) {
3976 struct lttng_ht_iter iter
;
3980 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3981 &iter
.iter
, agt
, node
.node
) {
3982 if (agt
->domain
== domain
) {
3983 ret_code
= list_lttng_agent_events(
3984 agt
, reply_payload
, &nb_events
);
3993 ret_code
= LTTNG_ERR_UND
;
3997 if (nb_events
> UINT32_MAX
) {
3998 ret_code
= LTTNG_ERR_OVERFLOW
;
4002 /* Update command reply header. */
4003 reply_command_header
.count
= (uint32_t) nb_events
;
4004 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
4005 sizeof(reply_command_header
));
4012 * Using the session list, filled a lttng_session array to send back to the
4013 * client for session listing.
4015 * The session list lock MUST be acquired before calling this function. Use
4016 * session_lock_list() and session_unlock_list().
4018 enum lttng_error_code
cmd_list_lttng_sessions(
4019 struct lttng_payload
*reply_payload
, uid_t uid
, gid_t gid
)
4021 int buffer_resize_ret
;
4022 enum lttng_error_code ret_code
= LTTNG_OK
;
4023 struct lttcomm_list_command_header reply_command_header
= {};
4024 size_t reply_command_header_offset
;
4025 struct ltt_session
*session
;
4026 struct ltt_session_list
*list
= session_get_list();
4030 assert(reply_payload
);
4032 /* Reserve space for command reply header. */
4033 reply_command_header_offset
= reply_payload
->buffer
.size
;
4034 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
4035 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
4036 if (buffer_resize_ret
) {
4037 ret_code
= LTTNG_ERR_NOMEM
;
4041 DBG("Getting all available session for UID %d GID %d",
4044 cds_list_for_each_entry(session
, &list
->head
, list
) {
4045 struct lttng_session tmp_session
= {};
4046 struct lttng_session_extended tmp_extended
= {};
4047 struct lttng_payload trace_format_buffer
;
4048 lttng_payload_init(&trace_format_buffer
);
4050 tmp_session
.extended
.ptr
= &tmp_extended
;
4052 if (!session_get(session
)) {
4053 lttng_payload_reset(&trace_format_buffer
);
4057 * Only list the sessions the user can control.
4059 if (!session_access_ok(session
, uid
) ||
4060 session
->destroyed
) {
4061 session_put(session
);
4062 lttng_payload_reset(&trace_format_buffer
);
4066 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4067 struct ltt_ust_session
*usess
= session
->ust_session
;
4069 if (session
->consumer
->type
== CONSUMER_DST_NET
||
4070 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
4071 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
4072 ret
= build_network_session_path(
4073 tmp_session
.path
, sizeof(tmp_session
.path
), session
);
4075 ret
= snprintf(tmp_session
.path
, sizeof(tmp_session
.path
), "%s",
4076 session
->consumer
->dst
.session_root_path
);
4079 PERROR("snprintf session path");
4080 session_put(session
);
4084 session
->trace_format
->serialize(&trace_format_buffer
);
4086 strncpy(tmp_session
.name
, session
->name
, NAME_MAX
);
4087 tmp_session
.name
[NAME_MAX
- 1] = '\0';
4088 tmp_session
.enabled
= session
->active
;
4089 tmp_session
.snapshot_mode
= session
->snapshot_mode
;
4090 tmp_session
.live_timer_interval
= session
->live_timer
;
4091 LTTNG_OPTIONAL_SET(&tmp_extended
.creation_time
, (uint64_t) session
->creation_time
);
4092 tmp_extended
.serialized_trace_format_descriptor
=
4093 lttng_buffer_view_from_dynamic_buffer(&trace_format_buffer
.buffer
,
4094 0, trace_format_buffer
.buffer
.size
);
4095 ret
= lttng_session_serialize(&tmp_session
, reply_payload
);
4096 lttng_payload_reset(&trace_format_buffer
);
4098 ret_code
= LTTNG_ERR_FATAL
;
4102 session_put(session
);
4105 if (i
> UINT32_MAX
) {
4106 ret_code
= LTTNG_ERR_OVERFLOW
;
4110 /* Update command reply header. */
4111 reply_command_header
.count
= (uint32_t) i
;
4112 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
4113 sizeof(reply_command_header
));
4114 ret_code
= LTTNG_OK
;
4120 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4121 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4123 int cmd_data_pending(struct ltt_session
*session
)
4126 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4127 struct ltt_ust_session
*usess
= session
->ust_session
;
4129 LTTNG_ASSERT(session
);
4131 DBG("Data pending for session %s", session
->name
);
4133 /* Session MUST be stopped to ask for data availability. */
4134 if (session
->active
) {
4135 ret
= LTTNG_ERR_SESSION_STARTED
;
4139 * If stopped, just make sure we've started before else the above call
4140 * will always send that there is data pending.
4142 * The consumer assumes that when the data pending command is received,
4143 * the trace has been started before or else no output data is written
4144 * by the streams which is a condition for data pending. So, this is
4145 * *VERY* important that we don't ask the consumer before a start
4148 if (!session
->has_been_started
) {
4154 /* A rotation is still pending, we have to wait. */
4155 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4156 DBG("Rotate still pending for session %s", session
->name
);
4161 if (ksess
&& ksess
->consumer
) {
4162 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4164 /* Data is still being extracted for the kernel. */
4169 if (usess
&& usess
->consumer
) {
4170 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4172 /* Data is still being extracted for the kernel. */
4177 /* Data is ready to be read by a viewer */
4185 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4187 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4189 int cmd_snapshot_add_output(struct ltt_session
*session
,
4190 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4193 struct snapshot_output
*new_output
;
4195 LTTNG_ASSERT(session
);
4196 LTTNG_ASSERT(output
);
4198 DBG("Cmd snapshot add output for session %s", session
->name
);
4201 * Can't create an output if the session is not set in no-output mode.
4203 if (session
->output_traces
) {
4204 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4208 if (session
->has_non_mmap_channel
) {
4209 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4213 /* Only one output is allowed until we have the "tee" feature. */
4214 if (session
->snapshot
.nb_output
== 1) {
4215 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4219 new_output
= snapshot_output_alloc();
4221 ret
= LTTNG_ERR_NOMEM
;
4225 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4226 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4227 &session
->snapshot
);
4229 if (ret
== -ENOMEM
) {
4230 ret
= LTTNG_ERR_NOMEM
;
4232 ret
= LTTNG_ERR_INVALID
;
4238 snapshot_add_output(&session
->snapshot
, new_output
);
4240 *id
= new_output
->id
;
4247 snapshot_output_destroy(new_output
);
4253 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4255 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4257 int cmd_snapshot_del_output(struct ltt_session
*session
,
4258 const struct lttng_snapshot_output
*output
)
4261 struct snapshot_output
*sout
= NULL
;
4263 LTTNG_ASSERT(session
);
4264 LTTNG_ASSERT(output
);
4269 * Permission denied to create an output if the session is not
4270 * set in no output mode.
4272 if (session
->output_traces
) {
4273 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4278 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4280 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4281 } else if (*output
->name
!= '\0') {
4282 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4284 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4287 ret
= LTTNG_ERR_INVALID
;
4291 snapshot_delete_output(&session
->snapshot
, sout
);
4292 snapshot_output_destroy(sout
);
4301 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4303 * If no output is available, outputs is untouched and 0 is returned.
4305 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4307 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4308 struct lttng_snapshot_output
**outputs
)
4311 struct lttng_snapshot_output
*list
= NULL
;
4312 struct lttng_ht_iter iter
;
4313 struct snapshot_output
*output
;
4315 LTTNG_ASSERT(session
);
4316 LTTNG_ASSERT(outputs
);
4318 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4321 * Permission denied to create an output if the session is not
4322 * set in no output mode.
4324 if (session
->output_traces
) {
4325 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4329 if (session
->snapshot
.nb_output
== 0) {
4334 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4336 ret
= -LTTNG_ERR_NOMEM
;
4340 /* Copy list from session to the new list object. */
4342 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4343 output
, node
.node
) {
4344 LTTNG_ASSERT(output
->consumer
);
4345 list
[idx
].id
= output
->id
;
4346 list
[idx
].max_size
= output
->max_size
;
4347 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4348 sizeof(list
[idx
].name
))) {
4349 ret
= -LTTNG_ERR_INVALID
;
4352 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4353 if (lttng_strncpy(list
[idx
].ctrl_url
,
4354 output
->consumer
->dst
.session_root_path
,
4355 sizeof(list
[idx
].ctrl_url
))) {
4356 ret
= -LTTNG_ERR_INVALID
;
4361 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4362 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4364 ret
= -LTTNG_ERR_NOMEM
;
4369 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4370 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4372 ret
= -LTTNG_ERR_NOMEM
;
4381 ret
= session
->snapshot
.nb_output
;
4390 * Check if we can regenerate the metadata for this session.
4391 * Only kernel, UST per-uid and non-live sessions are supported.
4393 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4396 int check_regenerate_metadata_support(struct ltt_session
*session
)
4400 LTTNG_ASSERT(session
);
4402 if (session
->live_timer
!= 0) {
4403 ret
= LTTNG_ERR_LIVE_SESSION
;
4406 if (!session
->active
) {
4407 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4410 if (session
->ust_session
) {
4411 switch (session
->ust_session
->buffer_type
) {
4412 case LTTNG_BUFFER_PER_UID
:
4414 case LTTNG_BUFFER_PER_PID
:
4415 ret
= LTTNG_ERR_PER_PID_SESSION
;
4419 ret
= LTTNG_ERR_UNK
;
4423 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4424 session
->consumer
->relay_minor_version
< 8) {
4425 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4435 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4437 * Ask the consumer to truncate the existing metadata file(s) and
4438 * then regenerate the metadata. Live and per-pid sessions are not
4439 * supported and return an error.
4441 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4443 int cmd_regenerate_metadata(struct ltt_session
*session
)
4447 LTTNG_ASSERT(session
);
4449 ret
= check_regenerate_metadata_support(session
);
4454 if (session
->kernel_session
) {
4455 ret
= kernctl_session_regenerate_metadata(
4456 session
->kernel_session
->fd
);
4458 ERR("Failed to regenerate the kernel metadata");
4463 if (session
->ust_session
) {
4464 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4466 ERR("Failed to regenerate the UST metadata");
4470 DBG("Cmd metadata regenerate for session %s", session
->name
);
4478 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4480 * Ask the tracer to regenerate a new statedump.
4482 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4484 int cmd_regenerate_statedump(struct ltt_session
*session
)
4488 LTTNG_ASSERT(session
);
4490 if (!session
->active
) {
4491 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4495 if (session
->kernel_session
) {
4496 ret
= kernctl_session_regenerate_statedump(
4497 session
->kernel_session
->fd
);
4499 * Currently, the statedump in kernel can only fail if out
4503 if (ret
== -ENOMEM
) {
4504 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4506 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4508 ERR("Failed to regenerate the kernel statedump");
4513 if (session
->ust_session
) {
4514 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4516 * Currently, the statedump in UST always returns 0.
4519 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4520 ERR("Failed to regenerate the UST statedump");
4524 DBG("Cmd regenerate statedump for session %s", session
->name
);
4532 enum lttng_error_code
synchronize_tracer_notifier_register(
4533 struct notification_thread_handle
*notification_thread
,
4534 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4536 enum lttng_error_code ret_code
;
4537 const struct lttng_condition
*condition
=
4538 lttng_trigger_get_const_condition(trigger
);
4539 const char *trigger_name
;
4540 uid_t trigger_owner
;
4541 enum lttng_trigger_status trigger_status
;
4542 const enum lttng_domain_type trigger_domain
=
4543 lttng_trigger_get_underlying_domain_type_restriction(
4546 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4547 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4549 LTTNG_ASSERT(condition
);
4550 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4551 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4553 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4554 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4555 trigger_name
: "(anonymous)";
4557 session_lock_list();
4558 switch (trigger_domain
) {
4559 case LTTNG_DOMAIN_KERNEL
:
4561 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4562 if (ret_code
!= LTTNG_OK
) {
4563 enum lttng_error_code notif_thread_unregister_ret
;
4565 notif_thread_unregister_ret
=
4566 notification_thread_command_unregister_trigger(
4567 notification_thread
, trigger
);
4569 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4570 /* Return the original error code. */
4571 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4573 (int) trigger_owner
,
4579 case LTTNG_DOMAIN_UST
:
4580 ust_app_global_update_all_event_notifier_rules();
4582 case LTTNG_DOMAIN_JUL
:
4583 case LTTNG_DOMAIN_LOG4J
:
4584 case LTTNG_DOMAIN_PYTHON
:
4586 /* Agent domains. */
4587 struct agent
*agt
= agent_find_by_event_notifier_domain(
4591 agt
= agent_create(trigger_domain
);
4593 ret_code
= LTTNG_ERR_NOMEM
;
4594 goto end_unlock_session_list
;
4597 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4600 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4601 if (ret_code
!= LTTNG_OK
) {
4602 goto end_unlock_session_list
;
4607 case LTTNG_DOMAIN_NONE
:
4612 ret_code
= LTTNG_OK
;
4613 end_unlock_session_list
:
4614 session_unlock_list();
4618 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4619 struct lttng_trigger
*trigger
,
4620 bool is_trigger_anonymous
,
4621 struct notification_thread_handle
*notification_thread
,
4622 struct lttng_trigger
**return_trigger
)
4624 enum lttng_error_code ret_code
;
4625 const char *trigger_name
;
4626 uid_t trigger_owner
;
4627 enum lttng_trigger_status trigger_status
;
4629 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4630 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4631 trigger_name
: "(anonymous)";
4633 trigger_status
= lttng_trigger_get_owner_uid(
4634 trigger
, &trigger_owner
);
4635 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4637 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4638 trigger_name
, (int) trigger_owner
,
4639 (int) lttng_credentials_get_uid(cmd_creds
));
4642 * Validate the trigger credentials against the command credentials.
4643 * Only the root user can register a trigger with non-matching
4646 if (!lttng_credentials_is_equal_uid(
4647 lttng_trigger_get_credentials(trigger
),
4649 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4650 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4651 trigger_name
, (int) trigger_owner
,
4652 (int) lttng_credentials_get_uid(cmd_creds
));
4653 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4659 * The bytecode generation also serves as a validation step for the
4660 * bytecode expressions.
4662 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4663 if (ret_code
!= LTTNG_OK
) {
4664 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4665 trigger_name
, (int) trigger_owner
, ret_code
);
4670 * A reference to the trigger is acquired by the notification thread.
4671 * It is safe to return the same trigger to the caller since it the
4672 * other user holds a reference.
4674 * The trigger is modified during the execution of the
4675 * "register trigger" command. However, by the time the command returns,
4676 * it is safe to use without any locking as its properties are
4679 ret_code
= notification_thread_command_register_trigger(
4680 notification_thread
, trigger
, is_trigger_anonymous
);
4681 if (ret_code
!= LTTNG_OK
) {
4682 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4683 trigger_name
, (int) trigger_owner
, ret_code
);
4687 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4688 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4689 trigger_name
: "(anonymous)";
4692 * Synchronize tracers if the trigger adds an event notifier.
4694 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4695 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4696 trigger
, cmd_creds
);
4697 if (ret_code
!= LTTNG_OK
) {
4698 ERR("Error registering tracer notifier: %s",
4699 lttng_strerror(-ret_code
));
4705 * Return an updated trigger to the client.
4707 * Since a modified version of the same trigger is returned, acquire a
4708 * reference to the trigger so the caller doesn't have to care if those
4709 * are distinct instances or not.
4711 if (ret_code
== LTTNG_OK
) {
4712 lttng_trigger_get(trigger
);
4713 *return_trigger
= trigger
;
4714 /* Ownership of trigger was transferred to caller. */
4722 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4723 const struct lttng_trigger
*trigger
)
4725 enum lttng_error_code ret_code
;
4726 const struct lttng_condition
*condition
=
4727 lttng_trigger_get_const_condition(trigger
);
4728 const enum lttng_domain_type trigger_domain
=
4729 lttng_trigger_get_underlying_domain_type_restriction(
4732 LTTNG_ASSERT(condition
);
4733 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4734 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4736 session_lock_list();
4737 switch (trigger_domain
) {
4738 case LTTNG_DOMAIN_KERNEL
:
4739 ret_code
= kernel_unregister_event_notifier(trigger
);
4740 if (ret_code
!= LTTNG_OK
) {
4741 goto end_unlock_session_list
;
4745 case LTTNG_DOMAIN_UST
:
4746 ust_app_global_update_all_event_notifier_rules();
4748 case LTTNG_DOMAIN_JUL
:
4749 case LTTNG_DOMAIN_LOG4J
:
4750 case LTTNG_DOMAIN_PYTHON
:
4752 /* Agent domains. */
4753 struct agent
*agt
= agent_find_by_event_notifier_domain(
4757 * This trigger was never registered in the first place. Calling
4758 * this function under those circumstances is an internal error.
4761 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4762 if (ret_code
!= LTTNG_OK
) {
4763 goto end_unlock_session_list
;
4768 case LTTNG_DOMAIN_NONE
:
4773 ret_code
= LTTNG_OK
;
4775 end_unlock_session_list
:
4776 session_unlock_list();
4780 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4781 const struct lttng_trigger
*trigger
,
4782 struct notification_thread_handle
*notification_thread
)
4784 enum lttng_error_code ret_code
;
4785 const char *trigger_name
;
4786 uid_t trigger_owner
;
4787 enum lttng_trigger_status trigger_status
;
4788 struct lttng_trigger
*sessiond_trigger
= NULL
;
4790 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4791 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4792 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4793 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4795 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4796 trigger_name
, (int) trigger_owner
,
4797 (int) lttng_credentials_get_uid(cmd_creds
));
4800 * Validate the trigger credentials against the command credentials.
4801 * Only the root user can unregister a trigger with non-matching
4804 if (!lttng_credentials_is_equal_uid(
4805 lttng_trigger_get_credentials(trigger
),
4807 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4808 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4809 trigger_name
, (int) trigger_owner
,
4810 (int) lttng_credentials_get_uid(cmd_creds
));
4811 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4816 /* Fetch the sessiond side trigger object. */
4817 ret_code
= notification_thread_command_get_trigger(
4818 notification_thread
, trigger
, &sessiond_trigger
);
4819 if (ret_code
!= LTTNG_OK
) {
4820 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4821 trigger_name
, (int) trigger_owner
, ret_code
);
4825 LTTNG_ASSERT(sessiond_trigger
);
4828 * From this point on, no matter what, consider the trigger
4831 * We set the unregistered state of the sessiond side trigger object in
4832 * the client thread since we want to minimize the possibility of the
4833 * notification thread being stalled due to a long execution of an
4834 * action that required the trigger lock.
4836 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4838 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4840 if (ret_code
!= LTTNG_OK
) {
4841 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4842 trigger_name
, (int) trigger_owner
, ret_code
);
4847 * Synchronize tracers if the trigger removes an event notifier.
4848 * Do this even if the trigger unregistration failed to at least stop
4849 * the tracers from producing notifications associated with this
4852 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4853 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4854 if (ret_code
!= LTTNG_OK
) {
4855 ERR("Error unregistering trigger to tracer.");
4862 lttng_trigger_put(sessiond_trigger
);
4866 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4867 struct notification_thread_handle
*notification_thread
,
4868 struct lttng_triggers
**return_triggers
)
4871 enum lttng_error_code ret_code
;
4872 struct lttng_triggers
*triggers
= NULL
;
4874 /* Get the set of triggers from the notification thread. */
4875 ret_code
= notification_thread_command_list_triggers(
4876 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4877 if (ret_code
!= LTTNG_OK
) {
4881 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4883 ret_code
= LTTNG_ERR_UNK
;
4887 *return_triggers
= triggers
;
4889 ret_code
= LTTNG_OK
;
4891 lttng_triggers_destroy(triggers
);
4895 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4896 const struct lttng_error_query
*query
,
4897 struct lttng_error_query_results
**_results
,
4898 struct notification_thread_handle
*notification_thread
)
4900 enum lttng_error_code ret_code
;
4901 const struct lttng_trigger
*query_target_trigger
;
4902 const struct lttng_action
*query_target_action
= NULL
;
4903 struct lttng_trigger
*matching_trigger
= NULL
;
4904 const char *trigger_name
;
4905 uid_t trigger_owner
;
4906 enum lttng_trigger_status trigger_status
;
4907 struct lttng_error_query_results
*results
= NULL
;
4909 switch (lttng_error_query_get_target_type(query
)) {
4910 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4911 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4913 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4914 query_target_trigger
=
4915 lttng_error_query_condition_borrow_target(query
);
4917 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4918 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4925 LTTNG_ASSERT(query_target_trigger
);
4927 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4928 query_target_trigger
, &matching_trigger
);
4929 if (ret_code
!= LTTNG_OK
) {
4933 /* No longer needed. */
4934 query_target_trigger
= NULL
;
4936 if (lttng_error_query_get_target_type(query
) ==
4937 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4938 /* Get the sessiond-side version of the target action. */
4939 query_target_action
=
4940 lttng_error_query_action_borrow_action_target(
4941 query
, matching_trigger
);
4944 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4945 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4946 trigger_name
: "(anonymous)";
4947 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4949 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4951 results
= lttng_error_query_results_create();
4953 ret_code
= LTTNG_ERR_NOMEM
;
4957 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4958 trigger_name
, (int) trigger_owner
,
4959 (int) lttng_credentials_get_uid(cmd_creds
));
4962 * Validate the trigger credentials against the command credentials.
4963 * Only the root user can target a trigger with non-matching
4966 if (!lttng_credentials_is_equal_uid(
4967 lttng_trigger_get_credentials(matching_trigger
),
4969 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4970 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4971 trigger_name
, (int) trigger_owner
,
4972 (int) lttng_credentials_get_uid(cmd_creds
));
4973 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4978 switch (lttng_error_query_get_target_type(query
)) {
4979 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4980 trigger_status
= lttng_trigger_add_error_results(
4981 matching_trigger
, results
);
4983 switch (trigger_status
) {
4984 case LTTNG_TRIGGER_STATUS_OK
:
4987 ret_code
= LTTNG_ERR_UNK
;
4992 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4994 trigger_status
= lttng_trigger_condition_add_error_results(
4995 matching_trigger
, results
);
4997 switch (trigger_status
) {
4998 case LTTNG_TRIGGER_STATUS_OK
:
5001 ret_code
= LTTNG_ERR_UNK
;
5007 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
5009 const enum lttng_action_status action_status
=
5010 lttng_action_add_error_query_results(
5011 query_target_action
, results
);
5013 switch (action_status
) {
5014 case LTTNG_ACTION_STATUS_OK
:
5017 ret_code
= LTTNG_ERR_UNK
;
5028 *_results
= results
;
5030 ret_code
= LTTNG_OK
;
5032 lttng_trigger_put(matching_trigger
);
5033 lttng_error_query_results_destroy(results
);
5038 * Send relayd sockets from snapshot output to consumer. Ignore request if the
5039 * snapshot output is *not* set with a remote destination.
5041 * Return LTTNG_OK on success or a LTTNG_ERR code.
5043 static enum lttng_error_code
set_relayd_for_snapshot(
5044 struct consumer_output
*output
,
5045 const struct ltt_session
*session
)
5047 enum lttng_error_code status
= LTTNG_OK
;
5048 struct lttng_ht_iter iter
;
5049 struct consumer_socket
*socket
;
5050 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
5051 const char *base_path_override
= NULL
;
5053 LTTNG_ASSERT(output
);
5054 LTTNG_ASSERT(session
);
5056 DBG2("Set relayd object from snapshot output");
5058 if (session
->current_trace_chunk
) {
5059 enum lttng_trace_chunk_status chunk_status
=
5060 lttng_trace_chunk_get_id(
5061 session
->current_trace_chunk
,
5062 ¤t_chunk_id
.value
);
5064 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5065 current_chunk_id
.is_set
= true;
5067 ERR("Failed to get current trace chunk id");
5068 status
= LTTNG_ERR_UNK
;
5073 /* Ignore if snapshot consumer output is not network. */
5074 if (output
->type
!= CONSUMER_DST_NET
) {
5079 * The snapshot record URI base path overrides the session
5082 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5083 base_path_override
= output
->dst
.net
.control
.subdir
;
5087 * For each consumer socket, create and send the relayd object of the
5091 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
5092 pthread_mutex_lock(socket
->lock
);
5093 status
= send_consumer_relayd_sockets(*session
, output
, socket
, base_path_override
,
5094 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
5095 pthread_mutex_unlock(socket
->lock
);
5096 if (status
!= LTTNG_OK
) {
5108 * Record a kernel snapshot.
5110 * Return LTTNG_OK on success or a LTTNG_ERR code.
5112 static enum lttng_error_code
record_kernel_snapshot(
5113 struct ltt_kernel_session
*ksess
,
5114 const struct consumer_output
*output
,
5115 const struct ltt_session
*session
,
5116 uint64_t nb_packets_per_stream
)
5118 enum lttng_error_code status
;
5120 LTTNG_ASSERT(ksess
);
5121 LTTNG_ASSERT(output
);
5122 LTTNG_ASSERT(session
);
5124 status
= kernel_snapshot_record(
5125 ksess
, output
, nb_packets_per_stream
);
5130 * Record a UST snapshot.
5132 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5134 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5135 const struct consumer_output
*output
,
5136 const struct ltt_session
*session
,
5137 uint64_t nb_packets_per_stream
)
5139 enum lttng_error_code status
;
5141 LTTNG_ASSERT(usess
);
5142 LTTNG_ASSERT(output
);
5143 LTTNG_ASSERT(session
);
5145 status
= ust_app_snapshot_record(
5146 usess
, output
, nb_packets_per_stream
);
5151 uint64_t get_session_size_one_more_packet_per_stream(
5152 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5154 uint64_t tot_size
= 0;
5156 if (session
->kernel_session
) {
5157 struct ltt_kernel_channel
*chan
;
5158 const struct ltt_kernel_session
*ksess
=
5159 session
->kernel_session
;
5161 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5162 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5164 * Don't take channel into account if we
5165 * already grab all its packets.
5169 tot_size
+= chan
->channel
->attr
.subbuf_size
5170 * chan
->stream_count
;
5174 if (session
->ust_session
) {
5175 const struct ltt_ust_session
*usess
= session
->ust_session
;
5177 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5185 * Calculate the number of packets we can grab from each stream that
5186 * fits within the overall snapshot max size.
5188 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5189 * the number of packets per stream.
5191 * TODO: this approach is not perfect: we consider the worse case
5192 * (packet filling the sub-buffers) as an upper bound, but we could do
5193 * better if we do this calculation while we actually grab the packet
5194 * content: we would know how much padding we don't actually store into
5197 * This algorithm is currently bounded by the number of packets per
5200 * Since we call this algorithm before actually grabbing the data, it's
5201 * an approximation: for instance, applications could appear/disappear
5202 * in between this call and actually grabbing data.
5205 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5209 uint64_t cur_nb_packets
= 0;
5212 return 0; /* Infinite */
5215 size_left
= max_size
;
5217 uint64_t one_more_packet_tot_size
;
5219 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5220 session
, cur_nb_packets
);
5221 if (!one_more_packet_tot_size
) {
5222 /* We are already grabbing all packets. */
5225 size_left
-= one_more_packet_tot_size
;
5226 if (size_left
< 0) {
5231 if (!cur_nb_packets
&& size_left
!= max_size
) {
5232 /* Not enough room to grab one packet of each stream, error. */
5235 return cur_nb_packets
;
5239 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5240 const struct snapshot_output
*snapshot_output
)
5242 int64_t nb_packets_per_stream
;
5243 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5245 enum lttng_error_code ret_code
= LTTNG_OK
;
5246 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5247 struct consumer_output
*original_ust_consumer_output
= NULL
;
5248 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5249 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5250 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5252 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5254 snapshot_output
->name
,
5255 snapshot_output
->datetime
,
5256 snapshot_output
->nb_snapshot
);
5257 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5258 ERR("Failed to format snapshot name");
5259 ret_code
= LTTNG_ERR_INVALID
;
5262 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5263 snapshot_output
->name
, session
->name
,
5264 snapshot_chunk_name
);
5265 if (!session
->kernel_session
&& !session
->ust_session
) {
5266 ERR("Failed to record snapshot as no channels exist");
5267 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5271 if (session
->kernel_session
) {
5272 original_kernel_consumer_output
=
5273 session
->kernel_session
->consumer
;
5274 snapshot_kernel_consumer_output
=
5275 consumer_copy_output(snapshot_output
->consumer
);
5276 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5277 snapshot_chunk_name
);
5279 /* Copy the original domain subdir. */
5280 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5281 original_kernel_consumer_output
->domain_subdir
);
5283 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5284 original_kernel_consumer_output
);
5286 ERR("Failed to copy consumer sockets from snapshot output configuration");
5287 ret_code
= LTTNG_ERR_NOMEM
;
5290 ret_code
= set_relayd_for_snapshot(
5291 snapshot_kernel_consumer_output
, session
);
5292 if (ret_code
!= LTTNG_OK
) {
5293 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5296 session
->kernel_session
->consumer
=
5297 snapshot_kernel_consumer_output
;
5299 if (session
->ust_session
) {
5300 original_ust_consumer_output
= session
->ust_session
->consumer
;
5301 snapshot_ust_consumer_output
=
5302 consumer_copy_output(snapshot_output
->consumer
);
5303 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5304 snapshot_chunk_name
);
5306 /* Copy the original domain subdir. */
5307 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5308 original_ust_consumer_output
->domain_subdir
);
5310 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5311 original_ust_consumer_output
);
5313 ERR("Failed to copy consumer sockets from snapshot output configuration");
5314 ret_code
= LTTNG_ERR_NOMEM
;
5317 ret_code
= set_relayd_for_snapshot(
5318 snapshot_ust_consumer_output
, session
);
5319 if (ret_code
!= LTTNG_OK
) {
5320 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5323 session
->ust_session
->consumer
=
5324 snapshot_ust_consumer_output
;
5327 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5328 snapshot_kernel_consumer_output
?:
5329 snapshot_ust_consumer_output
,
5330 consumer_output_get_base_path(
5331 snapshot_output
->consumer
),
5332 snapshot_chunk_name
);
5333 if (!snapshot_trace_chunk
) {
5334 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5336 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5339 LTTNG_ASSERT(!session
->current_trace_chunk
);
5340 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5341 lttng_trace_chunk_put(snapshot_trace_chunk
);
5342 snapshot_trace_chunk
= NULL
;
5344 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5346 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5350 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5351 snapshot_output
->max_size
);
5352 if (nb_packets_per_stream
< 0) {
5353 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5354 goto error_close_trace_chunk
;
5357 if (session
->kernel_session
) {
5358 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5359 snapshot_kernel_consumer_output
, session
,
5360 nb_packets_per_stream
);
5361 if (ret_code
!= LTTNG_OK
) {
5362 goto error_close_trace_chunk
;
5366 if (session
->ust_session
) {
5367 ret_code
= record_ust_snapshot(session
->ust_session
,
5368 snapshot_ust_consumer_output
, session
,
5369 nb_packets_per_stream
);
5370 if (ret_code
!= LTTNG_OK
) {
5371 goto error_close_trace_chunk
;
5375 error_close_trace_chunk
:
5376 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5377 ERR("Failed to release the current trace chunk of session \"%s\"",
5379 ret_code
= LTTNG_ERR_UNK
;
5382 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5383 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5385 * Don't goto end; make sure the chunk is closed for the session
5386 * to allow future snapshots.
5388 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5390 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5393 lttng_trace_chunk_put(snapshot_trace_chunk
);
5394 snapshot_trace_chunk
= NULL
;
5396 if (original_ust_consumer_output
) {
5397 session
->ust_session
->consumer
= original_ust_consumer_output
;
5399 if (original_kernel_consumer_output
) {
5400 session
->kernel_session
->consumer
=
5401 original_kernel_consumer_output
;
5403 consumer_output_put(snapshot_ust_consumer_output
);
5404 consumer_output_put(snapshot_kernel_consumer_output
);
5409 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5411 * The wait parameter is ignored so this call always wait for the snapshot to
5412 * complete before returning.
5414 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5416 int cmd_snapshot_record(struct ltt_session
*session
,
5417 const struct lttng_snapshot_output
*output
,
5418 int wait
__attribute__((unused
)))
5420 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5422 unsigned int snapshot_success
= 0;
5424 struct snapshot_output
*tmp_output
= NULL
;
5426 LTTNG_ASSERT(session
);
5427 LTTNG_ASSERT(output
);
5429 DBG("Cmd snapshot record for session %s", session
->name
);
5431 /* Get the datetime for the snapshot output directory. */
5432 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5435 cmd_ret
= LTTNG_ERR_INVALID
;
5440 * Permission denied to create an output if the session is not
5441 * set in no output mode.
5443 if (session
->output_traces
) {
5444 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5448 /* The session needs to be started at least once. */
5449 if (!session
->has_been_started
) {
5450 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5454 /* Use temporary output for the session. */
5455 if (*output
->ctrl_url
!= '\0') {
5456 tmp_output
= snapshot_output_alloc();
5458 cmd_ret
= LTTNG_ERR_NOMEM
;
5462 ret
= snapshot_output_init(session
, output
->max_size
,
5464 output
->ctrl_url
, output
->data_url
,
5468 if (ret
== -ENOMEM
) {
5469 cmd_ret
= LTTNG_ERR_NOMEM
;
5471 cmd_ret
= LTTNG_ERR_INVALID
;
5475 /* Use the global session count for the temporary snapshot. */
5476 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5478 /* Use the global datetime */
5479 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5480 cmd_ret
= snapshot_record(session
, tmp_output
);
5481 if (cmd_ret
!= LTTNG_OK
) {
5484 snapshot_success
= 1;
5486 struct snapshot_output
*sout
;
5487 struct lttng_ht_iter iter
;
5490 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5491 &iter
.iter
, sout
, node
.node
) {
5492 struct snapshot_output output_copy
;
5495 * Make a local copy of the output and override output
5496 * parameters with those provided as part of the
5499 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5501 if (output
->max_size
!= (uint64_t) -1ULL) {
5502 output_copy
.max_size
= output
->max_size
;
5505 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5506 memcpy(output_copy
.datetime
, datetime
,
5509 /* Use temporary name. */
5510 if (*output
->name
!= '\0') {
5511 if (lttng_strncpy(output_copy
.name
,
5513 sizeof(output_copy
.name
))) {
5514 cmd_ret
= LTTNG_ERR_INVALID
;
5520 cmd_ret
= snapshot_record(session
, &output_copy
);
5521 if (cmd_ret
!= LTTNG_OK
) {
5525 snapshot_success
= 1;
5530 if (snapshot_success
) {
5531 session
->snapshot
.nb_snapshot
++;
5533 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5538 snapshot_output_destroy(tmp_output
);
5544 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5546 int cmd_set_session_shm_path(struct ltt_session
*session
,
5547 const char *shm_path
)
5550 LTTNG_ASSERT(session
);
5553 * Can only set shm path before session is started.
5555 if (session
->has_been_started
) {
5556 return LTTNG_ERR_SESSION_STARTED
;
5559 strncpy(session
->shm_path
, shm_path
,
5560 sizeof(session
->shm_path
));
5561 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5567 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5569 * Ask the consumer to rotate the session output directory.
5570 * The session lock must be held.
5572 * Returns LTTNG_OK on success or else a negative LTTng error code.
5574 int cmd_rotate_session(struct ltt_session
*session
,
5575 struct lttng_rotate_session_return
*rotate_return
,
5576 bool quiet_rotation
,
5577 enum lttng_trace_chunk_command_type command
)
5580 uint64_t ongoing_rotation_chunk_id
;
5581 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5582 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5583 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5584 enum lttng_trace_chunk_status chunk_status
;
5585 bool failed_to_rotate
= false;
5586 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5588 LTTNG_ASSERT(session
);
5590 if (!session
->has_been_started
) {
5591 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5596 * Explicit rotation is not supported for live sessions.
5597 * However, live sessions can perform a quiet rotation on
5599 * Rotation is not supported for snapshot traces (no output).
5601 if ((!quiet_rotation
&& session
->live_timer
) ||
5602 !session
->output_traces
) {
5603 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5607 /* Unsupported feature in lttng-relayd before 2.11. */
5608 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5609 (session
->consumer
->relay_major_version
== 2 &&
5610 session
->consumer
->relay_minor_version
< 11)) {
5611 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5615 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5616 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5617 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5621 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5622 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5624 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5629 * After a stop, we only allow one rotation to occur, the other ones are
5630 * useless until a new start.
5632 if (session
->rotated_after_last_stop
) {
5633 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5635 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5640 * After a stop followed by a clear, disallow following rotations a they would
5641 * generate empty chunks.
5643 if (session
->cleared_after_last_stop
) {
5644 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5646 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5650 if (session
->active
) {
5651 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5653 if (!new_trace_chunk
) {
5654 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5660 * The current trace chunk becomes the chunk being archived.
5662 * After this point, "chunk_being_archived" must absolutely
5663 * be closed on the consumer(s), otherwise it will never be
5664 * cleaned-up, which will result in a leak.
5666 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5667 &chunk_being_archived
);
5669 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5673 if (session
->kernel_session
) {
5674 cmd_ret
= kernel_rotate_session(session
);
5675 if (cmd_ret
!= LTTNG_OK
) {
5676 failed_to_rotate
= true;
5677 rotation_fail_code
= cmd_ret
;
5680 if (session
->ust_session
) {
5681 cmd_ret
= ust_app_rotate_session(session
);
5682 if (cmd_ret
!= LTTNG_OK
) {
5683 failed_to_rotate
= true;
5684 rotation_fail_code
= cmd_ret
;
5688 if (!session
->active
) {
5689 session
->rotated_after_last_stop
= true;
5692 if (!chunk_being_archived
) {
5693 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5695 if (failed_to_rotate
) {
5696 cmd_ret
= rotation_fail_code
;
5703 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5704 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5705 &ongoing_rotation_chunk_id
);
5706 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5708 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5709 command
, session
->last_chunk_path
);
5711 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5715 if (failed_to_rotate
) {
5716 cmd_ret
= rotation_fail_code
;
5720 session
->quiet_rotation
= quiet_rotation
;
5721 ret
= timer_session_rotation_pending_check_start(session
,
5722 DEFAULT_ROTATE_PENDING_TIMER
);
5724 cmd_ret
= LTTNG_ERR_UNK
;
5728 if (rotate_return
) {
5729 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5732 session
->chunk_being_archived
= chunk_being_archived
;
5733 chunk_being_archived
= NULL
;
5734 if (!quiet_rotation
) {
5735 ret
= notification_thread_command_session_rotation_ongoing(
5736 the_notification_thread_handle
, session
->id
,
5737 ongoing_rotation_chunk_id
);
5738 if (ret
!= LTTNG_OK
) {
5739 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5741 cmd_ret
= (lttng_error_code
) ret
;
5745 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5746 session
->name
, ongoing_rotation_chunk_id
);
5748 lttng_trace_chunk_put(new_trace_chunk
);
5749 lttng_trace_chunk_put(chunk_being_archived
);
5750 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5753 if (session_reset_rotation_state(session
,
5754 LTTNG_ROTATION_STATE_ERROR
)) {
5755 ERR("Failed to reset rotation state of session \"%s\"",
5762 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5764 * Check if the session has finished its rotation.
5766 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5768 int cmd_rotate_get_info(struct ltt_session
*session
,
5769 struct lttng_rotation_get_info_return
*info_return
,
5770 uint64_t rotation_id
)
5772 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5773 enum lttng_rotation_state rotation_state
;
5775 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5776 session
->most_recent_chunk_id
.value
);
5778 if (session
->chunk_being_archived
) {
5779 enum lttng_trace_chunk_status chunk_status
;
5782 chunk_status
= lttng_trace_chunk_get_id(
5783 session
->chunk_being_archived
,
5785 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5787 rotation_state
= rotation_id
== chunk_id
?
5788 LTTNG_ROTATION_STATE_ONGOING
:
5789 LTTNG_ROTATION_STATE_EXPIRED
;
5791 if (session
->last_archived_chunk_id
.is_set
&&
5792 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5793 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5795 rotation_state
= session
->rotation_state
;
5799 switch (rotation_state
) {
5800 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5801 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5804 case LTTNG_ROTATION_STATE_EXPIRED
:
5805 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5806 rotation_id
, session
->name
);
5808 case LTTNG_ROTATION_STATE_ONGOING
:
5809 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5810 rotation_id
, session
->name
);
5812 case LTTNG_ROTATION_STATE_COMPLETED
:
5816 char *current_tracing_path_reply
;
5817 size_t current_tracing_path_reply_len
;
5819 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5820 rotation_id
, session
->name
);
5822 switch (session_get_consumer_destination_type(session
)) {
5823 case CONSUMER_DST_LOCAL
:
5824 current_tracing_path_reply
=
5825 info_return
->location
.local
.absolute_path
;
5826 current_tracing_path_reply_len
=
5827 sizeof(info_return
->location
.local
.absolute_path
);
5828 info_return
->location_type
=
5829 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5830 fmt_ret
= asprintf(&chunk_path
,
5831 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5832 session_get_base_path(session
),
5833 session
->last_archived_chunk_name
);
5834 if (fmt_ret
== -1) {
5835 PERROR("Failed to format the path of the last archived trace chunk");
5836 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5837 cmd_ret
= LTTNG_ERR_UNK
;
5841 case CONSUMER_DST_NET
:
5843 uint16_t ctrl_port
, data_port
;
5845 current_tracing_path_reply
=
5846 info_return
->location
.relay
.relative_path
;
5847 current_tracing_path_reply_len
=
5848 sizeof(info_return
->location
.relay
.relative_path
);
5849 /* Currently the only supported relay protocol. */
5850 info_return
->location
.relay
.protocol
=
5851 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5853 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5854 session_get_net_consumer_hostname(session
),
5855 sizeof(info_return
->location
.relay
.host
));
5857 ERR("Failed to copy host name to rotate_get_info reply");
5858 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5859 cmd_ret
= LTTNG_ERR_SET_URL
;
5863 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5864 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5865 info_return
->location
.relay
.ports
.data
= data_port
;
5866 info_return
->location_type
=
5867 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5868 chunk_path
= strdup(session
->last_chunk_path
);
5870 ERR("Failed to allocate the path of the last archived trace chunk");
5871 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5872 cmd_ret
= LTTNG_ERR_UNK
;
5881 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5882 chunk_path
, current_tracing_path_reply_len
);
5885 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5886 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5887 cmd_ret
= LTTNG_ERR_UNK
;
5893 case LTTNG_ROTATION_STATE_ERROR
:
5894 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5895 rotation_id
, session
->name
);
5903 info_return
->status
= (int32_t) rotation_state
;
5908 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5910 * Configure the automatic rotation parameters.
5911 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5912 * 'activate' to false means deactivate the rotation schedule and validate that
5913 * 'new_value' has the same value as the currently active value.
5915 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5917 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5918 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5920 struct notification_thread_handle
*notification_thread_handle
)
5923 uint64_t *parameter_value
;
5925 LTTNG_ASSERT(session
);
5927 DBG("Cmd rotate set schedule session %s", session
->name
);
5929 if (session
->live_timer
|| !session
->output_traces
) {
5930 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5931 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5935 switch (schedule_type
) {
5936 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5937 parameter_value
= &session
->rotate_size
;
5939 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5940 parameter_value
= &session
->rotate_timer_period
;
5941 if (new_value
>= UINT_MAX
) {
5942 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5943 new_value
, UINT_MAX
);
5944 ret
= LTTNG_ERR_INVALID
;
5949 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5950 ret
= LTTNG_ERR_INVALID
;
5954 /* Improper use of the API. */
5955 if (new_value
== -1ULL) {
5956 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5957 ret
= LTTNG_ERR_INVALID
;
5962 * As indicated in struct ltt_session's comments, a value of == 0 means
5963 * this schedule rotation type is not in use.
5965 * Reject the command if we were asked to activate a schedule that was
5968 if (activate
&& *parameter_value
!= 0) {
5969 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5970 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5975 * Reject the command if we were asked to deactivate a schedule that was
5978 if (!activate
&& *parameter_value
== 0) {
5979 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5980 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5985 * Reject the command if we were asked to deactivate a schedule that
5988 if (!activate
&& *parameter_value
!= new_value
) {
5989 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5990 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5994 *parameter_value
= activate
? new_value
: 0;
5996 switch (schedule_type
) {
5997 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5998 if (activate
&& session
->active
) {
6000 * Only start the timer if the session is active,
6001 * otherwise it will be started when the session starts.
6003 ret
= timer_session_rotation_schedule_timer_start(
6004 session
, new_value
);
6006 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
6007 ret
= LTTNG_ERR_UNK
;
6011 ret
= timer_session_rotation_schedule_timer_stop(
6014 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
6015 ret
= LTTNG_ERR_UNK
;
6020 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
6022 ret
= subscribe_session_consumed_size_rotation(session
,
6023 new_value
, notification_thread_handle
);
6025 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
6026 ret
= LTTNG_ERR_UNK
;
6030 ret
= unsubscribe_session_consumed_size_rotation(session
,
6031 notification_thread_handle
);
6033 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
6034 ret
= LTTNG_ERR_UNK
;
6041 /* Would have been caught before. */
6053 enum lttng_error_code
cmd_execute_trace_format_support_query(
6054 struct command_ctx
& cmd_ctx
, int sock
, int& sock_error
)
6057 enum lttng_error_code ret_code
;
6058 struct lttng_payload query_payload
;
6059 size_t payload_len
, offset
= 0;
6060 ssize_t sock_recv_len
, consumed
;
6062 lttng::trace_format_descriptor::uptr trace_format
;
6064 lttng_payload_init(&query_payload
);
6066 /* Receive the payload */
6067 payload_len
= (size_t) cmd_ctx
.lsm
.u
.trace_format_support_query
.trace_format_length
+
6068 cmd_ctx
.lsm
.u
.trace_format_support_query
.type_payload_length
;
6069 ret
= lttng_dynamic_buffer_set_size(&query_payload
.buffer
, payload_len
);
6071 ret_code
= LTTNG_ERR_NOMEM
;
6074 sock_recv_len
= lttcomm_recv_unix_sock(sock
, query_payload
.buffer
.data
, payload_len
);
6075 if (sock_recv_len
< 0 || sock_recv_len
!= payload_len
) {
6076 ERR("Failed to receive error query in command payload");
6078 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
6082 /* Receive fds, if any. */
6083 if (cmd_ctx
.lsm
.fd_count
> 0) {
6084 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
6085 sock
, cmd_ctx
.lsm
.fd_count
, &query_payload
);
6086 if (sock_recv_len
> 0 && sock_recv_len
!= cmd_ctx
.lsm
.fd_count
* sizeof(int)) {
6087 ERR("Failed to receive all file descriptors for error query in command payload: expected fd count = %u, ret = %d",
6088 cmd_ctx
.lsm
.fd_count
, (int) ret
);
6089 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
6092 } else if (sock_recv_len
<= 0) {
6093 ERR("Failed to receive file descriptors for error query in command payload: expected fd count = %u, ret = %d",
6094 cmd_ctx
.lsm
.fd_count
, (int) ret
);
6095 ret_code
= LTTNG_ERR_FATAL
;
6101 /* Deserialize trace_format_descriptor. */
6103 struct lttng_payload_view view
=
6104 lttng_payload_view_from_payload(&query_payload
, offset
, -1);
6105 if (!lttng_payload_view_is_valid(&view
)) {
6106 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
6110 consumed
= lttng::trace_format_descriptor::create_from_payload(&view
, trace_format
);
6112 if (consumed
!= cmd_ctx
.lsm
.u
.trace_format_support_query
.trace_format_length
) {
6113 ERR("Invalid trace format descriptor received as part of command payload");
6114 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
6120 switch (cmd_ctx
.lsm
.u
.trace_format_support_query
.query_type
) {
6123 if (cmd_ctx
.lsm
.domain
.type
!= LTTNG_DOMAIN_KERNEL
) {
6124 /* UST supports all trace format*/
6125 ret_code
= LTTNG_OK
;
6128 ret_code
= kernel_domain_check_trace_format_requirements(*trace_format
);
6133 struct lttcomm_relayd_sock
*relay_sock
= nullptr;
6134 struct lttng_uri uri
;
6135 uint64_t result_flags
= 0;
6136 uint64_t supported_trace_format
= 0;
6137 struct lttng_payload_view view
= lttng_payload_view_from_payload(&query_payload
,
6139 cmd_ctx
.lsm
.u
.trace_format_support_query
.type_payload_length
);
6140 if (!lttng_payload_view_is_valid(&view
)) {
6141 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
6145 memcpy(&uri
, view
.buffer
.data
, sizeof(uri
));
6146 ret_code
= create_connect_relayd(&uri
, &relay_sock
);
6147 if (ret_code
!= LTTNG_OK
) {
6152 ret_code
= relayd_get_configuration(relay_sock
,
6153 LTTCOMM_RELAYD_CONFIGURATION_QUERY_FLAG_SUPPORTED_TRACE_FORMAT
,
6154 result_flags
, &supported_trace_format
);
6155 if (ret_code
!= LTTNG_OK
) {
6159 if (!is_trace_format_configuration_supported(
6160 supported_trace_format
, *trace_format
)) {
6161 ERR("Relayd does not support the requested trace format");
6162 ret_code
= LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_RELAY_DAEMON
;
6166 ret_code
= LTTNG_OK
;
6171 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
6176 lttng_payload_reset(&query_payload
);
6180 /* Wait for a given path to be removed before continuing. */
6181 static enum lttng_error_code
wait_on_path(void *path_data
)
6183 const char *shm_path
= (const char *) path_data
;
6185 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6191 ret
= stat(shm_path
, &st
);
6193 if (errno
!= ENOENT
) {
6194 PERROR("stat() returned an error while checking for the existence of the shm path");
6196 DBG("shm path no longer exists, completing the destruction of session");
6200 if (!S_ISDIR(st
.st_mode
)) {
6201 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6206 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6212 * Returns a pointer to a handler to run on completion of a command.
6213 * Returns NULL if no handler has to be run for the last command executed.
6215 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6217 struct cmd_completion_handler
*handler
= current_completion_handler
;
6219 current_completion_handler
= NULL
;
6224 * Init command subsystem.
6229 * Set network sequence index to 1 for streams to match a relayd
6230 * socket on the consumer side.
6232 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6233 relayd_net_seq_idx
= 1;
6234 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6236 DBG("Command subsystem initialized");