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(unsigned int session_id
,
836 struct lttng_uri
*relayd_uri
,
837 struct consumer_output
*consumer
,
838 struct consumer_socket
*consumer_sock
,
839 const char *session_name
,
840 const char *hostname
,
841 const char *base_path
,
842 int session_live_timer
,
843 const uint64_t *current_chunk_id
,
844 time_t session_creation_time
,
845 bool session_name_contains_creation_time
,
846 struct lttcomm_relayd_sock
& rsock
)
849 enum lttng_error_code status
= LTTNG_OK
;
851 /* Set the network sequence index if not set. */
852 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
853 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
855 * Increment net_seq_idx because we are about to transfer the
856 * new relayd socket to the consumer.
857 * Assign unique key so the consumer can match streams.
859 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
860 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
863 /* Send relayd socket to consumer. */
864 ret
= consumer_send_relayd_socket(consumer_sock
, &rsock
, consumer
, relayd_uri
->stype
,
865 session_id
, session_name
, hostname
, base_path
, session_live_timer
,
866 current_chunk_id
, session_creation_time
,
867 session_name_contains_creation_time
);
869 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
873 /* Flag that the corresponding socket was sent. */
874 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
875 consumer_sock
->control_sock_sent
= 1;
876 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
877 consumer_sock
->data_sock_sent
= 1;
881 if (status
!= LTTNG_OK
) {
883 * The consumer output for this session should not be used anymore
884 * since the relayd connection failed thus making any tracing or/and
885 * streaming not usable.
887 consumer
->enabled
= 0;
893 static bool is_trace_format_configuration_supported(
894 uint64_t supported_trace_format
, lttng::trace_format_descriptor
& trace_format
)
896 static const std::unordered_map
<enum lttng_trace_format_descriptor_type
,
897 enum lttcomm_relayd_configuration_trace_format_flag
>
899 {LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1
,
900 LTTCOMM_RELAYD_CONFIGURATION_TRACE_FORMAT_SUPPORTED_CTF1
},
901 {LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_2
,
902 LTTCOMM_RELAYD_CONFIGURATION_TRACE_FORMAT_SUPPORTED_CTF2
},
905 auto it
= mapping
.find(trace_format
.type());
906 if (it
== mapping
.end()) {
910 if (!(supported_trace_format
& it
->second
)) {
918 * Send both relayd sockets to a specific consumer and domain. This is a
919 * helper function to facilitate sending the information to the consumer for a
922 * The consumer socket lock must be held by the caller.
924 * Returns LTTNG_OK, or an LTTng error code on failure.
926 static lttng_error_code
send_consumer_relayd_sockets(const ltt_session
& session
,
927 struct consumer_output
*consumer
,
928 struct consumer_socket
*sock
,
929 const char *base_path
,
930 const uint64_t *current_chunk_id
)
932 enum lttng_error_code status
= LTTNG_OK
;
933 struct lttcomm_relayd_sock
*control_sock
= nullptr;
934 struct lttcomm_relayd_sock
*data_sock
= nullptr;
936 LTTNG_ASSERT(consumer
);
939 /* Sending control relayd socket. */
940 if (!sock
->control_sock_sent
) {
942 uint64_t result_flags
= 0;
943 uint64_t supported_trace_format
= 0;
944 /* Connect to relayd and make version check if uri is the control. */
945 status
= create_connect_relayd(&consumer
->dst
.net
.control
, &control_sock
);
946 if (status
!= LTTNG_OK
) {
949 LTTNG_ASSERT(control_sock
);
951 consumer
->relay_major_version
= control_sock
->major
;
952 consumer
->relay_minor_version
= control_sock
->minor
;
954 ret
= relayd_get_configuration(control_sock
,
955 LTTCOMM_RELAYD_CONFIGURATION_QUERY_FLAG_SUPPORTED_TRACE_FORMAT
,
956 result_flags
, &supported_trace_format
);
958 ERR("Unable to get relayd configuration");
959 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
963 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
964 consumer
->relay_allows_clear
= true;
967 if (!is_trace_format_configuration_supported(
968 supported_trace_format
, *session
.trace_format
)) {
969 ERR("Relayd does not support the requested trace format");
970 status
= LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_RELAY_DAEMON
;
974 status
= send_consumer_relayd_socket(session
.id
, &consumer
->dst
.net
.control
,
975 consumer
, sock
, session
.name
, session
.hostname
, base_path
,
976 session
.live_timer
, current_chunk_id
, session
.creation_time
,
977 session
.name_contains_creation_time
, *control_sock
);
979 if (status
!= LTTNG_OK
) {
984 /* Sending data relayd socket. */
985 if (!sock
->data_sock_sent
) {
986 /* Connect to relayd and make version check if uri is the control. */
987 status
= create_connect_relayd(&consumer
->dst
.net
.data
, &data_sock
);
988 if (status
!= LTTNG_OK
) {
991 LTTNG_ASSERT(data_sock
);
993 status
= send_consumer_relayd_socket(session
.id
, &consumer
->dst
.net
.data
, consumer
,
994 sock
, session
.name
, session
.hostname
, base_path
, session
.live_timer
,
995 current_chunk_id
, session
.creation_time
,
996 session
.name_contains_creation_time
, *data_sock
);
998 if (status
!= LTTNG_OK
) {
1004 if (control_sock
!= nullptr) {
1005 relayd_close(control_sock
);
1009 if (data_sock
!= nullptr) {
1010 relayd_close(data_sock
);
1018 * Setup relayd connections for a tracing session. First creates the socket to
1019 * the relayd and send them to the right domain consumer. Consumer type MUST be
1022 int cmd_setup_relayd(struct ltt_session
*session
)
1025 struct ltt_ust_session
*usess
;
1026 struct ltt_kernel_session
*ksess
;
1027 struct consumer_socket
*socket
;
1028 struct lttng_ht_iter iter
;
1029 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1031 LTTNG_ASSERT(session
);
1033 usess
= session
->ust_session
;
1034 ksess
= session
->kernel_session
;
1036 DBG("Setting relayd for session %s", session
->name
);
1039 if (session
->current_trace_chunk
) {
1040 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1041 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1043 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1044 current_chunk_id
.is_set
= true;
1046 ERR("Failed to get current trace chunk id");
1047 ret
= LTTNG_ERR_UNK
;
1052 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1053 && usess
->consumer
->enabled
) {
1054 /* For each consumer socket, send relayd sockets */
1055 cds_lfht_for_each_entry (
1056 usess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1057 pthread_mutex_lock(socket
->lock
);
1058 ret
= send_consumer_relayd_sockets(*session
, usess
->consumer
, socket
,
1060 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
1061 pthread_mutex_unlock(socket
->lock
);
1062 if (ret
!= LTTNG_OK
) {
1065 /* Session is now ready for network streaming. */
1066 session
->net_handle
= 1;
1068 session
->consumer
->relay_major_version
=
1069 usess
->consumer
->relay_major_version
;
1070 session
->consumer
->relay_minor_version
=
1071 usess
->consumer
->relay_minor_version
;
1072 session
->consumer
->relay_allows_clear
=
1073 usess
->consumer
->relay_allows_clear
;
1076 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1077 && ksess
->consumer
->enabled
) {
1078 cds_lfht_for_each_entry (
1079 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1080 pthread_mutex_lock(socket
->lock
);
1081 ret
= send_consumer_relayd_sockets(*session
, ksess
->consumer
, socket
,
1083 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
1084 pthread_mutex_unlock(socket
->lock
);
1085 if (ret
!= LTTNG_OK
) {
1088 /* Session is now ready for network streaming. */
1089 session
->net_handle
= 1;
1091 session
->consumer
->relay_major_version
=
1092 ksess
->consumer
->relay_major_version
;
1093 session
->consumer
->relay_minor_version
=
1094 ksess
->consumer
->relay_minor_version
;
1095 session
->consumer
->relay_allows_clear
=
1096 ksess
->consumer
->relay_allows_clear
;
1105 * Start a kernel session by opening all necessary streams.
1107 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1110 struct ltt_kernel_channel
*kchan
;
1112 /* Open kernel metadata */
1113 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1114 ret
= kernel_open_metadata(ksess
);
1116 ret
= LTTNG_ERR_KERN_META_FAIL
;
1121 /* Open kernel metadata stream */
1122 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1123 ret
= kernel_open_metadata_stream(ksess
);
1125 ERR("Kernel create metadata stream failed");
1126 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1131 /* For each channel */
1132 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1133 if (kchan
->stream_count
== 0) {
1134 ret
= kernel_open_channel_stream(kchan
);
1136 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1139 /* Update the stream global counter */
1140 ksess
->stream_count_global
+= ret
;
1144 /* Setup kernel consumer socket and send fds to it */
1145 ret
= init_kernel_tracing(ksess
);
1147 ret
= LTTNG_ERR_KERN_START_FAIL
;
1151 /* This start the kernel tracing */
1152 ret
= kernel_start_session(ksess
);
1154 ret
= LTTNG_ERR_KERN_START_FAIL
;
1158 /* Quiescent wait after starting trace */
1159 kernel_wait_quiescent();
1169 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1171 struct ltt_kernel_channel
*kchan
;
1172 bool error_occurred
= false;
1175 if (!ksess
|| !ksess
->active
) {
1178 DBG("Stopping kernel tracing");
1180 ret
= kernel_stop_session(ksess
);
1182 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1186 kernel_wait_quiescent();
1188 /* Flush metadata after stopping (if exists) */
1189 if (ksess
->metadata_stream_fd
>= 0) {
1190 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1192 ERR("Kernel metadata flush failed");
1193 error_occurred
= true;
1197 /* Flush all buffers after stopping */
1198 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1199 ret
= kernel_flush_buffer(kchan
);
1201 ERR("Kernel flush buffer error");
1202 error_occurred
= true;
1207 if (error_occurred
) {
1208 ret
= LTTNG_ERR_UNK
;
1217 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1219 int cmd_disable_channel(struct ltt_session
*session
,
1220 enum lttng_domain_type domain
, char *channel_name
)
1223 struct ltt_ust_session
*usess
;
1225 usess
= session
->ust_session
;
1230 case LTTNG_DOMAIN_KERNEL
:
1232 ret
= channel_kernel_disable(session
->kernel_session
,
1234 if (ret
!= LTTNG_OK
) {
1238 kernel_wait_quiescent();
1241 case LTTNG_DOMAIN_UST
:
1243 struct ltt_ust_channel
*uchan
;
1244 struct lttng_ht
*chan_ht
;
1246 chan_ht
= usess
->domain_global
.channels
;
1248 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1249 if (uchan
== NULL
) {
1250 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1254 ret
= channel_ust_disable(usess
, uchan
);
1255 if (ret
!= LTTNG_OK
) {
1261 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1273 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1275 * The wpipe arguments is used as a notifier for the kernel thread.
1277 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1281 ssize_t sock_recv_len
;
1282 struct lttng_channel
*channel
= NULL
;
1283 struct lttng_buffer_view view
;
1284 struct lttng_dynamic_buffer channel_buffer
;
1285 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1287 lttng_dynamic_buffer_init(&channel_buffer
);
1288 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1289 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1291 ret
= LTTNG_ERR_NOMEM
;
1295 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1297 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1298 ERR("Failed to receive \"enable channel\" command payload");
1299 ret
= LTTNG_ERR_INVALID
;
1303 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1304 if (!lttng_buffer_view_is_valid(&view
)) {
1305 ret
= LTTNG_ERR_INVALID
;
1309 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1310 ERR("Invalid channel payload received in \"enable channel\" command");
1311 ret
= LTTNG_ERR_INVALID
;
1315 ret
= cmd_enable_channel_internal(
1316 cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1319 lttng_dynamic_buffer_reset(&channel_buffer
);
1320 lttng_channel_destroy(channel
);
1324 static enum lttng_error_code
kernel_domain_check_trace_format_requirements(
1325 const lttng::trace_format_descriptor
& descriptor
)
1327 enum lttng_error_code ret_code
= LTTNG_OK
;
1328 switch (descriptor
.type()) {
1329 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1
:
1330 /* Supported by all kernel tracer. */
1332 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_2
:
1333 if (!kernel_supports_ctf2()) {
1334 ret_code
= LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_KERNEL_TRACER
;
1343 static enum lttng_error_code
cmd_enable_channel_internal(
1344 struct ltt_session
*session
,
1345 const struct lttng_domain
*domain
,
1346 const struct lttng_channel
*_attr
,
1349 enum lttng_error_code ret_code
;
1350 struct ltt_ust_session
*usess
= session
->ust_session
;
1351 struct lttng_ht
*chan_ht
;
1353 struct lttng_channel
*attr
= NULL
;
1355 LTTNG_ASSERT(session
);
1356 LTTNG_ASSERT(_attr
);
1357 LTTNG_ASSERT(domain
);
1359 attr
= lttng_channel_copy(_attr
);
1361 ret_code
= LTTNG_ERR_NOMEM
;
1365 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1367 /* Validate channel name */
1368 if (attr
->name
[0] == '.' ||
1369 memchr(attr
->name
, '/', len
) != NULL
) {
1370 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1374 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1379 * If the session is a live session, remove the switch timer, the
1380 * live timer does the same thing but sends also synchronisation
1381 * beacons for inactive streams.
1383 if (session
->live_timer
> 0) {
1384 attr
->attr
.live_timer_interval
= session
->live_timer
;
1385 attr
->attr
.switch_timer_interval
= 0;
1388 /* Check for feature support */
1389 switch (domain
->type
) {
1390 case LTTNG_DOMAIN_KERNEL
:
1392 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1393 /* Sampling position of buffer is not supported */
1394 WARN("Kernel tracer does not support buffer monitoring. "
1395 "Setting the monitor interval timer to 0 "
1396 "(disabled) for channel '%s' of session '%s'",
1397 attr
->name
, session
->name
);
1398 lttng_channel_set_monitor_timer_interval(attr
, 0);
1401 ret_code
= kernel_domain_check_trace_format_requirements(*session
->trace_format
);
1402 if (ret_code
!= LTTNG_OK
) {
1403 WARN("Kernel tracer does not support the configured trace format of session '%s'",
1409 case LTTNG_DOMAIN_UST
:
1411 case LTTNG_DOMAIN_JUL
:
1412 case LTTNG_DOMAIN_LOG4J
:
1413 case LTTNG_DOMAIN_PYTHON
:
1414 if (!agent_tracing_is_enabled()) {
1415 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1416 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1421 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1425 switch (domain
->type
) {
1426 case LTTNG_DOMAIN_KERNEL
:
1428 struct ltt_kernel_channel
*kchan
;
1430 kchan
= trace_kernel_get_channel_by_name(
1431 attr
->name
, session
->kernel_session
);
1432 if (kchan
== NULL
) {
1434 * Don't try to create a channel if the session has been started at
1435 * some point in time before. The tracer does not allow it.
1437 if (session
->has_been_started
) {
1438 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1442 if (session
->snapshot
.nb_output
> 0 ||
1443 session
->snapshot_mode
) {
1444 /* Enforce mmap output for snapshot sessions. */
1445 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1447 ret_code
= channel_kernel_create(
1448 session
->kernel_session
, attr
, wpipe
);
1449 if (attr
->name
[0] != '\0') {
1450 session
->kernel_session
->has_non_default_channel
= 1;
1453 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1456 if (ret_code
!= LTTNG_OK
) {
1460 kernel_wait_quiescent();
1463 case LTTNG_DOMAIN_UST
:
1464 case LTTNG_DOMAIN_JUL
:
1465 case LTTNG_DOMAIN_LOG4J
:
1466 case LTTNG_DOMAIN_PYTHON
:
1468 struct ltt_ust_channel
*uchan
;
1473 * Current agent implementation limitations force us to allow
1474 * only one channel at once in "agent" subdomains. Each
1475 * subdomain has a default channel name which must be strictly
1478 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1479 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1480 LTTNG_SYMBOL_NAME_LEN
)) {
1481 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1484 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1485 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1486 LTTNG_SYMBOL_NAME_LEN
)) {
1487 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1490 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1491 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1492 LTTNG_SYMBOL_NAME_LEN
)) {
1493 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1498 chan_ht
= usess
->domain_global
.channels
;
1500 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1501 if (uchan
== NULL
) {
1503 * Don't try to create a channel if the session has been started at
1504 * some point in time before. The tracer does not allow it.
1506 if (session
->has_been_started
) {
1507 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1511 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1512 if (attr
->name
[0] != '\0') {
1513 usess
->has_non_default_channel
= 1;
1516 ret_code
= channel_ust_enable(usess
, uchan
);
1521 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1525 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1526 session
->has_non_mmap_channel
= true;
1531 lttng_channel_destroy(attr
);
1535 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1536 struct ltt_session
*session
,
1537 enum lttng_domain_type domain
,
1538 enum lttng_process_attr process_attr
,
1539 enum lttng_tracking_policy
*policy
)
1541 enum lttng_error_code ret_code
= LTTNG_OK
;
1542 const struct process_attr_tracker
*tracker
;
1545 case LTTNG_DOMAIN_KERNEL
:
1546 if (!session
->kernel_session
) {
1547 ret_code
= LTTNG_ERR_INVALID
;
1550 tracker
= kernel_get_process_attr_tracker(
1551 session
->kernel_session
, process_attr
);
1553 case LTTNG_DOMAIN_UST
:
1554 if (!session
->ust_session
) {
1555 ret_code
= LTTNG_ERR_INVALID
;
1558 tracker
= trace_ust_get_process_attr_tracker(
1559 session
->ust_session
, process_attr
);
1562 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1566 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1568 ret_code
= LTTNG_ERR_INVALID
;
1574 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1575 struct ltt_session
*session
,
1576 enum lttng_domain_type domain
,
1577 enum lttng_process_attr process_attr
,
1578 enum lttng_tracking_policy policy
)
1580 enum lttng_error_code ret_code
= LTTNG_OK
;
1583 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1584 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1585 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1588 ret_code
= LTTNG_ERR_INVALID
;
1593 case LTTNG_DOMAIN_KERNEL
:
1594 if (!session
->kernel_session
) {
1595 ret_code
= LTTNG_ERR_INVALID
;
1598 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1599 session
->kernel_session
, process_attr
, policy
);
1601 case LTTNG_DOMAIN_UST
:
1602 if (!session
->ust_session
) {
1603 ret_code
= LTTNG_ERR_INVALID
;
1606 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1607 session
->ust_session
, process_attr
, policy
);
1610 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1617 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1618 struct ltt_session
*session
,
1619 enum lttng_domain_type domain
,
1620 enum lttng_process_attr process_attr
,
1621 const struct process_attr_value
*value
)
1623 enum lttng_error_code ret_code
= LTTNG_OK
;
1626 case LTTNG_DOMAIN_KERNEL
:
1627 if (!session
->kernel_session
) {
1628 ret_code
= LTTNG_ERR_INVALID
;
1631 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1632 session
->kernel_session
, process_attr
, value
);
1634 case LTTNG_DOMAIN_UST
:
1635 if (!session
->ust_session
) {
1636 ret_code
= LTTNG_ERR_INVALID
;
1639 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1640 session
->ust_session
, process_attr
, value
);
1643 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1650 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1651 struct ltt_session
*session
,
1652 enum lttng_domain_type domain
,
1653 enum lttng_process_attr process_attr
,
1654 const struct process_attr_value
*value
)
1656 enum lttng_error_code ret_code
= LTTNG_OK
;
1659 case LTTNG_DOMAIN_KERNEL
:
1660 if (!session
->kernel_session
) {
1661 ret_code
= LTTNG_ERR_INVALID
;
1664 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1665 session
->kernel_session
, process_attr
, value
);
1667 case LTTNG_DOMAIN_UST
:
1668 if (!session
->ust_session
) {
1669 ret_code
= LTTNG_ERR_INVALID
;
1672 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1673 session
->ust_session
, process_attr
, value
);
1676 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1683 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1684 struct ltt_session
*session
,
1685 enum lttng_domain_type domain
,
1686 enum lttng_process_attr process_attr
,
1687 struct lttng_process_attr_values
**values
)
1689 enum lttng_error_code ret_code
= LTTNG_OK
;
1690 const struct process_attr_tracker
*tracker
;
1691 enum process_attr_tracker_status status
;
1694 case LTTNG_DOMAIN_KERNEL
:
1695 if (!session
->kernel_session
) {
1696 ret_code
= LTTNG_ERR_INVALID
;
1699 tracker
= kernel_get_process_attr_tracker(
1700 session
->kernel_session
, process_attr
);
1702 case LTTNG_DOMAIN_UST
:
1703 if (!session
->ust_session
) {
1704 ret_code
= LTTNG_ERR_INVALID
;
1707 tracker
= trace_ust_get_process_attr_tracker(
1708 session
->ust_session
, process_attr
);
1711 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1716 ret_code
= LTTNG_ERR_INVALID
;
1720 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1722 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1723 ret_code
= LTTNG_OK
;
1725 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1726 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1728 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1729 ret_code
= LTTNG_ERR_NOMEM
;
1732 ret_code
= LTTNG_ERR_UNK
;
1741 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1743 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1744 struct lttng_event
*event
,
1745 char *filter_expression
,
1746 struct lttng_bytecode
*bytecode
,
1747 struct lttng_event_exclusion
*exclusion
)
1750 const char *event_name
;
1751 const struct ltt_session
*session
= cmd_ctx
->session
;
1752 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1753 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1755 DBG("Disable event command for event \'%s\'", event
->name
);
1758 * Filter and exclusions are simply not handled by the
1759 * disable event command at this time.
1763 (void) filter_expression
;
1766 /* Ignore the presence of filter or exclusion for the event */
1768 event
->exclusion
= 0;
1770 event_name
= event
->name
;
1772 /* Error out on unhandled search criteria */
1773 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1774 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1775 ret
= LTTNG_ERR_UNK
;
1782 case LTTNG_DOMAIN_KERNEL
:
1784 struct ltt_kernel_channel
*kchan
;
1785 struct ltt_kernel_session
*ksess
;
1787 ksess
= session
->kernel_session
;
1790 * If a non-default channel has been created in the
1791 * session, explicitely require that -c chan_name needs
1794 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1795 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1799 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1800 if (kchan
== NULL
) {
1801 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1805 switch (event
->type
) {
1806 case LTTNG_EVENT_ALL
:
1807 case LTTNG_EVENT_TRACEPOINT
:
1808 case LTTNG_EVENT_SYSCALL
:
1809 case LTTNG_EVENT_PROBE
:
1810 case LTTNG_EVENT_FUNCTION
:
1811 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1812 if (event_name
[0] == '\0') {
1813 ret
= event_kernel_disable_event(kchan
,
1816 ret
= event_kernel_disable_event(kchan
,
1817 event_name
, event
->type
);
1819 if (ret
!= LTTNG_OK
) {
1824 ret
= LTTNG_ERR_UNK
;
1828 kernel_wait_quiescent();
1831 case LTTNG_DOMAIN_UST
:
1833 struct ltt_ust_channel
*uchan
;
1834 struct ltt_ust_session
*usess
;
1836 usess
= session
->ust_session
;
1838 if (validate_ust_event_name(event_name
)) {
1839 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1844 * If a non-default channel has been created in the
1845 * session, explicitly require that -c chan_name needs
1848 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1849 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1853 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1855 if (uchan
== NULL
) {
1856 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1860 switch (event
->type
) {
1861 case LTTNG_EVENT_ALL
:
1863 * An empty event name means that everything
1864 * should be disabled.
1866 if (event
->name
[0] == '\0') {
1867 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1869 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1872 if (ret
!= LTTNG_OK
) {
1877 ret
= LTTNG_ERR_UNK
;
1881 DBG3("Disable UST event %s in channel %s completed", event_name
,
1885 case LTTNG_DOMAIN_LOG4J
:
1886 case LTTNG_DOMAIN_JUL
:
1887 case LTTNG_DOMAIN_PYTHON
:
1890 struct ltt_ust_session
*usess
= session
->ust_session
;
1892 LTTNG_ASSERT(usess
);
1894 switch (event
->type
) {
1895 case LTTNG_EVENT_ALL
:
1898 ret
= LTTNG_ERR_UNK
;
1902 agt
= trace_ust_find_agent(usess
, domain
);
1904 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1908 * An empty event name means that everything
1909 * should be disabled.
1911 if (event
->name
[0] == '\0') {
1912 ret
= event_agent_disable_all(usess
, agt
);
1914 ret
= event_agent_disable(usess
, agt
, event_name
);
1916 if (ret
!= LTTNG_OK
) {
1923 ret
= LTTNG_ERR_UND
;
1934 free(filter_expression
);
1939 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1941 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1942 const struct lttng_event_context
*event_context
, int kwpipe
)
1944 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1945 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1946 const struct ltt_session
*session
= cmd_ctx
->session
;
1947 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1950 * Don't try to add a context if the session has been started at
1951 * some point in time before. The tracer does not allow it and would
1952 * result in a corrupted trace.
1954 if (cmd_ctx
->session
->has_been_started
) {
1955 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1960 case LTTNG_DOMAIN_KERNEL
:
1961 LTTNG_ASSERT(session
->kernel_session
);
1963 if (session
->kernel_session
->channel_count
== 0) {
1964 /* Create default channel */
1965 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1966 if (ret
!= LTTNG_OK
) {
1969 chan_kern_created
= 1;
1971 /* Add kernel context to kernel tracer */
1972 ret
= context_kernel_add(session
->kernel_session
,
1973 event_context
, channel_name
);
1974 if (ret
!= LTTNG_OK
) {
1978 case LTTNG_DOMAIN_JUL
:
1979 case LTTNG_DOMAIN_LOG4J
:
1982 * Validate channel name.
1983 * If no channel name is given and the domain is JUL or LOG4J,
1984 * set it to the appropriate domain-specific channel name. If
1985 * a name is provided but does not match the expexted channel
1986 * name, return an error.
1988 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1989 strcmp(channel_name
,
1990 DEFAULT_JUL_CHANNEL_NAME
)) {
1991 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1993 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1994 strcmp(channel_name
,
1995 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1996 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2001 case LTTNG_DOMAIN_UST
:
2003 struct ltt_ust_session
*usess
= session
->ust_session
;
2004 unsigned int chan_count
;
2006 LTTNG_ASSERT(usess
);
2008 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
2009 if (chan_count
== 0) {
2010 struct lttng_channel
*attr
;
2011 /* Create default channel */
2012 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
2014 ret
= LTTNG_ERR_FATAL
;
2018 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
2019 if (ret
!= LTTNG_OK
) {
2023 channel_attr_destroy(attr
);
2024 chan_ust_created
= 1;
2027 ret
= context_ust_add(usess
, domain
, event_context
,
2029 if (ret
!= LTTNG_OK
) {
2035 ret
= LTTNG_ERR_UND
;
2043 if (chan_kern_created
) {
2044 struct ltt_kernel_channel
*kchan
=
2045 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2046 session
->kernel_session
);
2047 /* Created previously, this should NOT fail. */
2048 LTTNG_ASSERT(kchan
);
2049 kernel_destroy_channel(kchan
);
2052 if (chan_ust_created
) {
2053 struct ltt_ust_channel
*uchan
=
2054 trace_ust_find_channel_by_name(
2055 session
->ust_session
->domain_global
.channels
,
2056 DEFAULT_CHANNEL_NAME
);
2057 /* Created previously, this should NOT fail. */
2058 LTTNG_ASSERT(uchan
);
2059 /* Remove from the channel list of the session. */
2060 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2062 trace_ust_destroy_channel(uchan
);
2068 static inline bool name_starts_with(const char *name
, const char *prefix
)
2070 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2072 return !strncmp(name
, prefix
, max_cmp_len
);
2075 /* Perform userspace-specific event name validation */
2076 static int validate_ust_event_name(const char *name
)
2086 * Check name against all internal UST event component namespaces used
2089 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2090 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2091 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2100 * Internal version of cmd_enable_event() with a supplemental
2101 * "internal_event" flag which is used to enable internal events which should
2102 * be hidden from clients. Such events are used in the agent implementation to
2103 * enable the events through which all "agent" events are funeled.
2105 static int _cmd_enable_event(struct ltt_session
*session
,
2106 const struct lttng_domain
*domain
,
2107 char *channel_name
, struct lttng_event
*event
,
2108 char *filter_expression
,
2109 struct lttng_bytecode
*filter
,
2110 struct lttng_event_exclusion
*exclusion
,
2111 int wpipe
, bool internal_event
)
2113 int ret
= 0, channel_created
= 0;
2114 struct lttng_channel
*attr
= NULL
;
2116 LTTNG_ASSERT(session
);
2117 LTTNG_ASSERT(event
);
2118 LTTNG_ASSERT(channel_name
);
2120 /* If we have a filter, we must have its filter expression */
2121 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2123 /* Normalize event name as a globbing pattern */
2124 strutils_normalize_star_glob_pattern(event
->name
);
2126 /* Normalize exclusion names as globbing patterns */
2130 for (i
= 0; i
< exclusion
->count
; i
++) {
2131 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2133 strutils_normalize_star_glob_pattern(name
);
2137 DBG("Enable event command for event \'%s\'", event
->name
);
2141 switch (domain
->type
) {
2142 case LTTNG_DOMAIN_KERNEL
:
2144 struct ltt_kernel_channel
*kchan
;
2147 * If a non-default channel has been created in the
2148 * session, explicitely require that -c chan_name needs
2151 if (session
->kernel_session
->has_non_default_channel
2152 && channel_name
[0] == '\0') {
2153 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2157 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2158 session
->kernel_session
);
2159 if (kchan
== NULL
) {
2160 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2161 LTTNG_BUFFER_GLOBAL
);
2163 ret
= LTTNG_ERR_FATAL
;
2166 if (lttng_strncpy(attr
->name
, channel_name
,
2167 sizeof(attr
->name
))) {
2168 ret
= LTTNG_ERR_INVALID
;
2172 ret
= cmd_enable_channel_internal(
2173 session
, domain
, attr
, wpipe
);
2174 if (ret
!= LTTNG_OK
) {
2177 channel_created
= 1;
2180 /* Get the newly created kernel channel pointer */
2181 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2182 session
->kernel_session
);
2183 if (kchan
== NULL
) {
2184 /* This sould not happen... */
2185 ret
= LTTNG_ERR_FATAL
;
2189 switch (event
->type
) {
2190 case LTTNG_EVENT_ALL
:
2192 char *filter_expression_a
= NULL
;
2193 struct lttng_bytecode
*filter_a
= NULL
;
2196 * We need to duplicate filter_expression and filter,
2197 * because ownership is passed to first enable
2200 if (filter_expression
) {
2201 filter_expression_a
= strdup(filter_expression
);
2202 if (!filter_expression_a
) {
2203 ret
= LTTNG_ERR_FATAL
;
2208 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2210 free(filter_expression_a
);
2211 ret
= LTTNG_ERR_FATAL
;
2214 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2216 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2217 ret
= event_kernel_enable_event(kchan
, event
,
2218 filter_expression
, filter
);
2219 /* We have passed ownership */
2220 filter_expression
= NULL
;
2222 if (ret
!= LTTNG_OK
) {
2223 if (channel_created
) {
2224 /* Let's not leak a useless channel. */
2225 kernel_destroy_channel(kchan
);
2227 free(filter_expression_a
);
2231 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2232 ret
= event_kernel_enable_event(kchan
, event
,
2233 filter_expression_a
, filter_a
);
2234 /* We have passed ownership */
2235 filter_expression_a
= NULL
;
2237 if (ret
!= LTTNG_OK
) {
2242 case LTTNG_EVENT_PROBE
:
2243 case LTTNG_EVENT_USERSPACE_PROBE
:
2244 case LTTNG_EVENT_FUNCTION
:
2245 case LTTNG_EVENT_FUNCTION_ENTRY
:
2246 case LTTNG_EVENT_TRACEPOINT
:
2247 ret
= event_kernel_enable_event(kchan
, event
,
2248 filter_expression
, filter
);
2249 /* We have passed ownership */
2250 filter_expression
= NULL
;
2252 if (ret
!= LTTNG_OK
) {
2253 if (channel_created
) {
2254 /* Let's not leak a useless channel. */
2255 kernel_destroy_channel(kchan
);
2260 case LTTNG_EVENT_SYSCALL
:
2261 ret
= event_kernel_enable_event(kchan
, event
,
2262 filter_expression
, filter
);
2263 /* We have passed ownership */
2264 filter_expression
= NULL
;
2266 if (ret
!= LTTNG_OK
) {
2271 ret
= LTTNG_ERR_UNK
;
2275 kernel_wait_quiescent();
2278 case LTTNG_DOMAIN_UST
:
2280 struct ltt_ust_channel
*uchan
;
2281 struct ltt_ust_session
*usess
= session
->ust_session
;
2283 LTTNG_ASSERT(usess
);
2286 * If a non-default channel has been created in the
2287 * session, explicitely require that -c chan_name needs
2290 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2291 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2295 /* Get channel from global UST domain */
2296 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2298 if (uchan
== NULL
) {
2299 /* Create default channel */
2300 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2301 usess
->buffer_type
);
2303 ret
= LTTNG_ERR_FATAL
;
2306 if (lttng_strncpy(attr
->name
, channel_name
,
2307 sizeof(attr
->name
))) {
2308 ret
= LTTNG_ERR_INVALID
;
2312 ret
= cmd_enable_channel_internal(
2313 session
, domain
, attr
, wpipe
);
2314 if (ret
!= LTTNG_OK
) {
2318 /* Get the newly created channel reference back */
2319 uchan
= trace_ust_find_channel_by_name(
2320 usess
->domain_global
.channels
, channel_name
);
2321 LTTNG_ASSERT(uchan
);
2324 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2326 * Don't allow users to add UST events to channels which
2327 * are assigned to a userspace subdomain (JUL, Log4J,
2330 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2334 if (!internal_event
) {
2336 * Ensure the event name is not reserved for internal
2339 ret
= validate_ust_event_name(event
->name
);
2341 WARN("Userspace event name %s failed validation.",
2343 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2348 /* At this point, the session and channel exist on the tracer */
2349 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2350 filter_expression
, filter
, exclusion
,
2352 /* We have passed ownership */
2353 filter_expression
= NULL
;
2356 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2357 goto already_enabled
;
2358 } else if (ret
!= LTTNG_OK
) {
2363 case LTTNG_DOMAIN_LOG4J
:
2364 case LTTNG_DOMAIN_JUL
:
2365 case LTTNG_DOMAIN_PYTHON
:
2367 const char *default_event_name
, *default_chan_name
;
2369 struct lttng_event uevent
;
2370 struct lttng_domain tmp_dom
;
2371 struct ltt_ust_session
*usess
= session
->ust_session
;
2373 LTTNG_ASSERT(usess
);
2375 if (!agent_tracing_is_enabled()) {
2376 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2377 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2381 agt
= trace_ust_find_agent(usess
, domain
->type
);
2383 agt
= agent_create(domain
->type
);
2385 ret
= LTTNG_ERR_NOMEM
;
2388 agent_add(agt
, usess
->agents
);
2391 /* Create the default tracepoint. */
2392 memset(&uevent
, 0, sizeof(uevent
));
2393 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2394 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2395 default_event_name
= event_get_default_agent_ust_name(
2397 if (!default_event_name
) {
2398 ret
= LTTNG_ERR_FATAL
;
2401 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2402 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2405 * The domain type is changed because we are about to enable the
2406 * default channel and event for the JUL domain that are hardcoded.
2407 * This happens in the UST domain.
2409 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2410 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2412 switch (domain
->type
) {
2413 case LTTNG_DOMAIN_LOG4J
:
2414 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2416 case LTTNG_DOMAIN_JUL
:
2417 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2419 case LTTNG_DOMAIN_PYTHON
:
2420 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2423 /* The switch/case we are in makes this impossible */
2428 char *filter_expression_copy
= NULL
;
2429 struct lttng_bytecode
*filter_copy
= NULL
;
2432 const size_t filter_size
= sizeof(
2433 struct lttng_bytecode
)
2436 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2438 ret
= LTTNG_ERR_NOMEM
;
2441 memcpy(filter_copy
, filter
, filter_size
);
2443 filter_expression_copy
=
2444 strdup(filter_expression
);
2445 if (!filter_expression
) {
2446 ret
= LTTNG_ERR_NOMEM
;
2449 if (!filter_expression_copy
|| !filter_copy
) {
2450 free(filter_expression_copy
);
2456 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2457 (char *) default_chan_name
,
2458 &uevent
, filter_expression_copy
,
2459 filter_copy
, NULL
, wpipe
);
2462 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2463 goto already_enabled
;
2464 } else if (ret
!= LTTNG_OK
) {
2468 /* The wild card * means that everything should be enabled. */
2469 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2470 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2473 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2477 filter_expression
= NULL
;
2478 if (ret
!= LTTNG_OK
) {
2485 ret
= LTTNG_ERR_UND
;
2493 free(filter_expression
);
2496 channel_attr_destroy(attr
);
2502 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2503 * We own filter, exclusion, and filter_expression.
2505 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2506 struct lttng_event
*event
,
2507 char *filter_expression
,
2508 struct lttng_event_exclusion
*exclusion
,
2509 struct lttng_bytecode
*bytecode
,
2514 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2516 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2519 * The ownership of the following parameters is transferred to
2520 * _cmd_enable_event:
2522 * - filter_expression,
2526 ret
= _cmd_enable_event(cmd_ctx
->session
,
2528 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2529 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2530 filter_expression
= NULL
;
2537 * Enable an event which is internal to LTTng. An internal should
2538 * never be made visible to clients and are immune to checks such as
2541 static int cmd_enable_event_internal(struct ltt_session
*session
,
2542 const struct lttng_domain
*domain
,
2543 char *channel_name
, struct lttng_event
*event
,
2544 char *filter_expression
,
2545 struct lttng_bytecode
*filter
,
2546 struct lttng_event_exclusion
*exclusion
,
2549 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2550 filter_expression
, filter
, exclusion
, wpipe
, true);
2554 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2556 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2557 struct lttng_payload
*reply_payload
)
2559 enum lttng_error_code ret_code
;
2561 ssize_t i
, nb_events
= 0;
2562 struct lttng_event
*events
= NULL
;
2563 struct lttcomm_list_command_header reply_command_header
= {};
2564 size_t reply_command_header_offset
;
2566 assert(reply_payload
);
2568 /* Reserve space for command reply header. */
2569 reply_command_header_offset
= reply_payload
->buffer
.size
;
2570 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2571 reply_command_header_offset
+
2572 sizeof(struct lttcomm_list_command_header
));
2574 ret_code
= LTTNG_ERR_NOMEM
;
2579 case LTTNG_DOMAIN_KERNEL
:
2580 nb_events
= kernel_list_events(&events
);
2581 if (nb_events
< 0) {
2582 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2586 case LTTNG_DOMAIN_UST
:
2587 nb_events
= ust_app_list_events(&events
);
2588 if (nb_events
< 0) {
2589 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2593 case LTTNG_DOMAIN_LOG4J
:
2594 case LTTNG_DOMAIN_JUL
:
2595 case LTTNG_DOMAIN_PYTHON
:
2596 nb_events
= agent_list_events(&events
, domain
);
2597 if (nb_events
< 0) {
2598 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2603 ret_code
= LTTNG_ERR_UND
;
2607 for (i
= 0; i
< nb_events
; i
++) {
2608 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2611 ret_code
= LTTNG_ERR_NOMEM
;
2616 if (nb_events
> UINT32_MAX
) {
2617 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2618 ret_code
= LTTNG_ERR_OVERFLOW
;
2622 /* Update command reply header. */
2623 reply_command_header
.count
= (uint32_t) nb_events
;
2624 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2625 sizeof(reply_command_header
));
2627 ret_code
= LTTNG_OK
;
2634 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2636 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2637 struct lttng_payload
*reply
)
2639 enum lttng_error_code ret_code
;
2641 unsigned int i
, nb_fields
;
2642 struct lttng_event_field
*fields
= NULL
;
2643 struct lttcomm_list_command_header reply_command_header
= {};
2644 size_t reply_command_header_offset
;
2648 /* Reserve space for command reply header. */
2649 reply_command_header_offset
= reply
->buffer
.size
;
2650 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2651 reply_command_header_offset
+
2652 sizeof(struct lttcomm_list_command_header
));
2654 ret_code
= LTTNG_ERR_NOMEM
;
2659 case LTTNG_DOMAIN_UST
:
2660 ret
= ust_app_list_event_fields(&fields
);
2662 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2667 case LTTNG_DOMAIN_KERNEL
:
2668 default: /* fall-through */
2669 ret_code
= LTTNG_ERR_UND
;
2675 for (i
= 0; i
< nb_fields
; i
++) {
2676 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2678 ret_code
= LTTNG_ERR_NOMEM
;
2683 if (nb_fields
> UINT32_MAX
) {
2684 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2685 ret_code
= LTTNG_ERR_OVERFLOW
;
2689 /* Update command reply header. */
2690 reply_command_header
.count
= (uint32_t) nb_fields
;
2692 memcpy(reply
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2693 sizeof(reply_command_header
));
2695 ret_code
= LTTNG_OK
;
2702 enum lttng_error_code
cmd_list_syscalls(
2703 struct lttng_payload
*reply_payload
)
2705 enum lttng_error_code ret_code
;
2706 ssize_t nb_events
, i
;
2708 struct lttng_event
*events
= NULL
;
2709 struct lttcomm_list_command_header reply_command_header
= {};
2710 size_t reply_command_header_offset
;
2712 assert(reply_payload
);
2714 /* Reserve space for command reply header. */
2715 reply_command_header_offset
= reply_payload
->buffer
.size
;
2716 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2717 reply_command_header_offset
+
2718 sizeof(struct lttcomm_list_command_header
));
2720 ret_code
= LTTNG_ERR_NOMEM
;
2724 nb_events
= syscall_table_list(&events
);
2725 if (nb_events
< 0) {
2726 ret_code
= (enum lttng_error_code
) -nb_events
;
2730 for (i
= 0; i
< nb_events
; i
++) {
2731 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2734 ret_code
= LTTNG_ERR_NOMEM
;
2739 if (nb_events
> UINT32_MAX
) {
2740 ERR("Syscall count would overflow the syscall listing command's reply");
2741 ret_code
= LTTNG_ERR_OVERFLOW
;
2745 /* Update command reply header. */
2746 reply_command_header
.count
= (uint32_t) nb_events
;
2747 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2748 sizeof(reply_command_header
));
2750 ret_code
= LTTNG_OK
;
2757 * Command LTTNG_START_TRACE processed by the client thread.
2759 * Called with session mutex held.
2761 int cmd_start_trace(struct ltt_session
*session
)
2763 enum lttng_error_code ret
;
2764 unsigned long nb_chan
= 0;
2765 struct ltt_kernel_session
*ksession
;
2766 struct ltt_ust_session
*usess
;
2767 const bool session_rotated_after_last_stop
=
2768 session
->rotated_after_last_stop
;
2769 const bool session_cleared_after_last_stop
=
2770 session
->cleared_after_last_stop
;
2772 LTTNG_ASSERT(session
);
2774 /* Ease our life a bit ;) */
2775 ksession
= session
->kernel_session
;
2776 usess
= session
->ust_session
;
2778 /* Is the session already started? */
2779 if (session
->active
) {
2780 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2781 /* Perform nothing */
2785 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2786 !session
->current_trace_chunk
) {
2788 * A rotation was launched while the session was stopped and
2789 * it has not been completed yet. It is not possible to start
2790 * the session since starting the session here would require a
2791 * rotation from "NULL" to a new trace chunk. That rotation
2792 * would overlap with the ongoing rotation, which is not
2795 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2797 ret
= LTTNG_ERR_ROTATION_PENDING
;
2802 * Starting a session without channel is useless since after that it's not
2803 * possible to enable channel thus inform the client.
2805 if (usess
&& usess
->domain_global
.channels
) {
2806 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2809 nb_chan
+= ksession
->channel_count
;
2812 ret
= LTTNG_ERR_NO_CHANNEL
;
2816 session
->active
= true;
2817 session
->rotated_after_last_stop
= false;
2818 session
->cleared_after_last_stop
= false;
2819 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2820 if (!session
->has_been_started
) {
2821 struct lttng_trace_chunk
*trace_chunk
;
2823 DBG("Creating initial trace chunk of session \"%s\"",
2825 trace_chunk
= session_create_new_trace_chunk(
2826 session
, NULL
, NULL
, NULL
);
2828 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2831 LTTNG_ASSERT(!session
->current_trace_chunk
);
2832 ret
= (lttng_error_code
) session_set_trace_chunk(session
, trace_chunk
,
2834 lttng_trace_chunk_put(trace_chunk
);
2836 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2840 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2843 * Rotate existing streams into the new chunk.
2844 * This is a "quiet" rotation has no client has
2845 * explicitly requested this operation.
2847 * There is also no need to wait for the rotation
2848 * to complete as it will happen immediately. No data
2849 * was produced as the session was stopped, so the
2850 * rotation should happen on reception of the command.
2852 ret
= (lttng_error_code
) cmd_rotate_session(session
, NULL
, true,
2853 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2854 if (ret
!= LTTNG_OK
) {
2860 /* Kernel tracing */
2861 if (ksession
!= NULL
) {
2862 DBG("Start kernel tracing session %s", session
->name
);
2863 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2864 if (ret
!= LTTNG_OK
) {
2869 /* Flag session that trace should start automatically */
2871 int int_ret
= ust_app_start_trace_all(usess
);
2874 ret
= LTTNG_ERR_UST_START_FAIL
;
2880 * Open a packet in every stream of the session to ensure that viewers
2881 * can correctly identify the boundaries of the periods during which
2882 * tracing was active for this session.
2884 ret
= session_open_packets(session
);
2885 if (ret
!= LTTNG_OK
) {
2890 * Clear the flag that indicates that a rotation was done while the
2891 * session was stopped.
2893 session
->rotated_after_last_stop
= false;
2895 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2896 int int_ret
= timer_session_rotation_schedule_timer_start(
2897 session
, session
->rotate_timer_period
);
2900 ERR("Failed to enable rotate timer");
2901 ret
= LTTNG_ERR_UNK
;
2909 if (ret
== LTTNG_OK
) {
2910 /* Flag this after a successful start. */
2911 session
->has_been_started
= true;
2913 session
->active
= false;
2914 /* Restore initial state on error. */
2915 session
->rotated_after_last_stop
=
2916 session_rotated_after_last_stop
;
2917 session
->cleared_after_last_stop
=
2918 session_cleared_after_last_stop
;
2925 * Command LTTNG_STOP_TRACE processed by the client thread.
2927 int cmd_stop_trace(struct ltt_session
*session
)
2930 struct ltt_kernel_session
*ksession
;
2931 struct ltt_ust_session
*usess
;
2933 LTTNG_ASSERT(session
);
2935 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2937 ksession
= session
->kernel_session
;
2938 usess
= session
->ust_session
;
2940 /* Session is not active. Skip everything and inform the client. */
2941 if (!session
->active
) {
2942 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2946 ret
= stop_kernel_session(ksession
);
2947 if (ret
!= LTTNG_OK
) {
2951 if (usess
&& usess
->active
) {
2952 ret
= ust_app_stop_trace_all(usess
);
2954 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2959 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2961 /* Flag inactive after a successful stop. */
2962 session
->active
= 0;
2970 * Set the base_path of the session only if subdir of a control uris is set.
2971 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2973 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2975 struct lttng_uri
*uris
)
2980 for (i
= 0; i
< nb_uri
; i
++) {
2981 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2982 uris
[i
].subdir
[0] == '\0') {
2983 /* Not interested in these URIs */
2987 if (session
->base_path
!= NULL
) {
2988 free(session
->base_path
);
2989 session
->base_path
= NULL
;
2992 /* Set session base_path */
2993 session
->base_path
= strdup(uris
[i
].subdir
);
2994 if (!session
->base_path
) {
2995 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2996 uris
[i
].subdir
, session
->name
);
2997 ret
= LTTNG_ERR_NOMEM
;
3000 DBG2("Setting base path \"%s\" for session \"%s\"",
3001 session
->base_path
, session
->name
);
3009 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
3011 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
3012 struct lttng_uri
*uris
)
3015 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3016 struct ltt_ust_session
*usess
= session
->ust_session
;
3018 LTTNG_ASSERT(session
);
3020 LTTNG_ASSERT(nb_uri
> 0);
3022 /* Can't set consumer URI if the session is active. */
3023 if (session
->active
) {
3024 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
3029 * Set the session base path if any. This is done inside
3030 * cmd_set_consumer_uri to preserve backward compatibility of the
3031 * previous session creation api vs the session descriptor api.
3033 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
3034 if (ret
!= LTTNG_OK
) {
3038 /* Set the "global" consumer URIs */
3039 for (i
= 0; i
< nb_uri
; i
++) {
3040 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
3042 if (ret
!= LTTNG_OK
) {
3047 /* Set UST session URIs */
3048 if (session
->ust_session
) {
3049 for (i
= 0; i
< nb_uri
; i
++) {
3050 ret
= add_uri_to_consumer(session
,
3051 session
->ust_session
->consumer
,
3052 &uris
[i
], LTTNG_DOMAIN_UST
);
3053 if (ret
!= LTTNG_OK
) {
3059 /* Set kernel session URIs */
3060 if (session
->kernel_session
) {
3061 for (i
= 0; i
< nb_uri
; i
++) {
3062 ret
= add_uri_to_consumer(session
,
3063 session
->kernel_session
->consumer
,
3064 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3065 if (ret
!= LTTNG_OK
) {
3072 * Make sure to set the session in output mode after we set URI since a
3073 * session can be created without URL (thus flagged in no output mode).
3075 session
->output_traces
= 1;
3077 ksess
->output_traces
= 1;
3081 usess
->output_traces
= 1;
3092 enum lttng_error_code
set_session_output_from_descriptor(
3093 struct ltt_session
*session
,
3094 const struct lttng_session_descriptor
*descriptor
)
3097 enum lttng_error_code ret_code
= LTTNG_OK
;
3098 enum lttng_session_descriptor_type session_type
=
3099 lttng_session_descriptor_get_type(descriptor
);
3100 enum lttng_session_descriptor_output_type output_type
=
3101 lttng_session_descriptor_get_output_type(descriptor
);
3102 struct lttng_uri uris
[2] = {};
3103 size_t uri_count
= 0;
3105 switch (output_type
) {
3106 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3108 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3109 lttng_session_descriptor_get_local_output_uri(descriptor
,
3113 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3114 lttng_session_descriptor_get_network_output_uris(descriptor
,
3115 &uris
[0], &uris
[1]);
3119 ret_code
= LTTNG_ERR_INVALID
;
3123 switch (session_type
) {
3124 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3126 struct snapshot_output
*new_output
= NULL
;
3128 new_output
= snapshot_output_alloc();
3130 ret_code
= LTTNG_ERR_NOMEM
;
3134 ret
= snapshot_output_init_with_uri(session
,
3135 DEFAULT_SNAPSHOT_MAX_SIZE
,
3136 NULL
, uris
, uri_count
, session
->consumer
,
3137 new_output
, &session
->snapshot
);
3139 ret_code
= (ret
== -ENOMEM
) ?
3140 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3141 snapshot_output_destroy(new_output
);
3144 snapshot_add_output(&session
->snapshot
, new_output
);
3147 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3148 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3150 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3154 ret_code
= LTTNG_ERR_INVALID
;
3162 enum lttng_error_code
cmd_create_session_from_descriptor(
3163 struct lttng_session_descriptor
*descriptor
,
3164 const lttng_sock_cred
*creds
,
3165 const char *home_path
)
3168 enum lttng_error_code ret_code
;
3169 const char *session_name
;
3170 struct ltt_session
*new_session
= NULL
;
3171 enum lttng_session_descriptor_status descriptor_status
;
3172 const lttng_trace_format_descriptor
*trace_format_descriptor
= NULL
;
3173 lttng::trace_format_descriptor::uptr trace_format_descriptor_ptr
;
3175 session_lock_list();
3177 if (*home_path
!= '/') {
3178 ERR("Home path provided by client is not absolute");
3179 ret_code
= LTTNG_ERR_INVALID
;
3184 descriptor_status
= lttng_session_descriptor_get_session_name(
3185 descriptor
, &session_name
);
3186 switch (descriptor_status
) {
3187 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3189 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3190 session_name
= NULL
;
3193 ret_code
= LTTNG_ERR_INVALID
;
3197 descriptor_status
= lttng_session_descriptor_get_trace_format_descriptor(
3198 descriptor
, &trace_format_descriptor
);
3199 if (descriptor_status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
3200 ret_code
= LTTNG_ERR_INVALID
;
3205 trace_format_descriptor_ptr
=
3206 reinterpret_cast<const lttng::trace_format_descriptor
*>(
3207 trace_format_descriptor
)
3209 } catch (std::exception
& e
) {
3210 ERR("%s", e
.what());
3211 ret_code
= LTTNG_ERR_UNK
;
3215 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, trace_format_descriptor_ptr
,
3217 if (ret_code
!= LTTNG_OK
) {
3221 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3222 new_session
->id
, new_session
->name
, new_session
->uid
, new_session
->gid
);
3223 if (ret_code
!= LTTNG_OK
) {
3227 /* Announce the session's destruction to the notification thread when it is destroyed. */
3228 ret
= session_add_destroy_notifier(
3230 [](const struct ltt_session
*session
,
3231 void *user_data
__attribute__((unused
))) {
3232 (void) notification_thread_command_remove_session(
3233 the_notification_thread_handle
, session
->id
);
3237 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3239 ret
= LTTNG_ERR_NOMEM
;
3243 if (!session_name
) {
3244 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3247 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3252 if (!lttng_session_descriptor_is_output_destination_initialized(
3255 * Only include the session's creation time in the output
3256 * destination if the name of the session itself was
3257 * not auto-generated.
3259 ret_code
= lttng_session_descriptor_set_default_output(
3261 session_name
? &new_session
->creation_time
: NULL
,
3263 if (ret_code
!= LTTNG_OK
) {
3267 new_session
->has_user_specified_directory
=
3268 lttng_session_descriptor_has_output_directory(
3272 switch (lttng_session_descriptor_get_type(descriptor
)) {
3273 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3274 new_session
->snapshot_mode
= 1;
3276 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3277 new_session
->live_timer
=
3278 lttng_session_descriptor_live_get_timer_interval(
3285 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3286 if (ret_code
!= LTTNG_OK
) {
3289 new_session
->consumer
->enabled
= 1;
3290 ret_code
= LTTNG_OK
;
3292 /* Release reference provided by the session_create function. */
3293 session_put(new_session
);
3294 if (ret_code
!= LTTNG_OK
&& new_session
) {
3295 /* Release the global reference on error. */
3296 session_destroy(new_session
);
3298 session_unlock_list();
3302 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3303 struct lttng_session_descriptor
**return_descriptor
)
3306 size_t payload_size
;
3307 struct lttng_dynamic_buffer payload
;
3308 struct lttng_buffer_view home_dir_view
;
3309 struct lttng_buffer_view session_descriptor_view
;
3310 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3311 enum lttng_error_code ret_code
;
3313 lttng_dynamic_buffer_init(&payload
);
3314 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3316 ret_code
= LTTNG_ERR_INVALID
;
3319 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3320 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3321 ret_code
= LTTNG_ERR_INVALID
;
3325 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3326 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3327 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3329 ret_code
= LTTNG_ERR_NOMEM
;
3333 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3335 ERR("Reception of session descriptor failed, aborting.");
3336 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3340 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3343 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3344 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3345 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3346 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3347 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3351 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3353 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3354 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3355 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3356 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3357 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3361 ret
= lttng_session_descriptor_create_from_buffer(
3362 &session_descriptor_view
, &session_descriptor
);
3364 ERR("Failed to create session descriptor from payload of \"create session\" command");
3365 ret_code
= LTTNG_ERR_INVALID
;
3370 * Sets the descriptor's auto-generated properties (name, output) if
3373 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3375 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3376 if (ret_code
!= LTTNG_OK
) {
3380 ret_code
= LTTNG_OK
;
3381 *return_descriptor
= session_descriptor
;
3382 session_descriptor
= NULL
;
3384 lttng_dynamic_buffer_reset(&payload
);
3385 lttng_session_descriptor_destroy(session_descriptor
);
3390 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3391 void *_reply_context
)
3395 const struct cmd_destroy_session_reply_context
*reply_context
=
3396 (cmd_destroy_session_reply_context
*) _reply_context
;
3397 struct lttng_dynamic_buffer payload
;
3398 struct lttcomm_session_destroy_command_header cmd_header
;
3399 struct lttng_trace_archive_location
*location
= NULL
;
3400 struct lttcomm_lttng_msg llm
= {
3401 .cmd_type
= LTTNG_DESTROY_SESSION
,
3402 .ret_code
= reply_context
->destruction_status
,
3405 sizeof(struct lttcomm_session_destroy_command_header
),
3409 size_t payload_size_before_location
;
3411 lttng_dynamic_buffer_init(&payload
);
3413 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3415 ERR("Failed to append session destruction message");
3419 cmd_header
.rotation_state
=
3420 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3421 session
->rotation_state
:
3422 LTTNG_ROTATION_STATE_NO_ROTATION
);
3423 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3424 sizeof(cmd_header
));
3426 ERR("Failed to append session destruction command header");
3430 if (!reply_context
->implicit_rotation_on_destroy
) {
3431 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3435 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3436 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3441 location
= session_get_trace_archive_location(session
);
3443 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3448 payload_size_before_location
= payload
.size
;
3449 comm_ret
= lttng_trace_archive_location_serialize(location
,
3451 lttng_trace_archive_location_put(location
);
3453 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3457 /* Update the message to indicate the location's length. */
3458 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3459 payload
.size
- payload_size_before_location
;
3461 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3462 payload
.data
, payload
.size
);
3463 if (comm_ret
!= (ssize_t
) payload
.size
) {
3464 ERR("Failed to send result of the destruction of session \"%s\" to client",
3468 ret
= close(reply_context
->reply_sock_fd
);
3470 PERROR("Failed to close client socket in deferred session destroy reply");
3472 lttng_dynamic_buffer_reset(&payload
);
3473 free(_reply_context
);
3477 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3479 * Called with session lock held.
3481 int cmd_destroy_session(struct ltt_session
*session
,
3482 struct notification_thread_handle
*notification_thread_handle
,
3486 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3487 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3490 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3491 if (!reply_context
) {
3492 ret
= LTTNG_ERR_NOMEM
;
3496 reply_context
->reply_sock_fd
= *sock_fd
;
3500 LTTNG_ASSERT(session
);
3502 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3504 if (session
->active
) {
3505 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3507 ret
= cmd_stop_trace(session
);
3508 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3509 /* Carry on with the destruction of the session. */
3510 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3511 session
->name
, lttng_strerror(-ret
));
3512 destruction_last_error
= (lttng_error_code
) ret
;
3516 if (session
->rotation_schedule_timer_enabled
) {
3517 if (timer_session_rotation_schedule_timer_stop(
3519 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3521 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3525 if (session
->rotate_size
) {
3526 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3527 session
->rotate_size
= 0;
3530 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3532 * Perform a last rotation on destruction if rotations have
3533 * occurred during the session's lifetime.
3535 ret
= cmd_rotate_session(session
, NULL
, false,
3536 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3537 if (ret
!= LTTNG_OK
) {
3538 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3539 session
->name
, lttng_strerror(-ret
));
3540 destruction_last_error
= (lttng_error_code
) -ret
;
3542 if (reply_context
) {
3543 reply_context
->implicit_rotation_on_destroy
= true;
3545 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3547 * The user has not triggered a session rotation. However, to
3548 * ensure all data has been consumed, the session is rotated
3549 * to a 'null' trace chunk before it is destroyed.
3551 * This is a "quiet" rotation meaning that no notification is
3552 * emitted and no renaming of the current trace chunk takes
3555 ret
= cmd_rotate_session(session
, NULL
, true,
3556 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3558 * Rotation operations may not be supported by the kernel
3559 * tracer. Hence, do not consider this implicit rotation as
3560 * a session destruction error. The library has already stopped
3561 * the session and waited for pending data; there is nothing
3562 * left to do but complete the destruction of the session.
3564 if (ret
!= LTTNG_OK
&&
3565 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3566 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3567 session
->name
, lttng_strerror(ret
));
3568 destruction_last_error
= (lttng_error_code
) -ret
;
3572 if (session
->shm_path
[0]) {
3574 * When a session is created with an explicit shm_path,
3575 * the consumer daemon will create its shared memory files
3576 * at that location and will *not* unlink them. This is normal
3577 * as the intention of that feature is to make it possible
3578 * to retrieve the content of those files should a crash occur.
3580 * To ensure the content of those files can be used, the
3581 * sessiond daemon will replicate the content of the metadata
3582 * cache in a metadata file.
3584 * On clean-up, it is expected that the consumer daemon will
3585 * unlink the shared memory files and that the session daemon
3586 * will unlink the metadata file. Then, the session's directory
3587 * in the shm path can be removed.
3589 * Unfortunately, a flaw in the design of the sessiond's and
3590 * consumerd's tear down of channels makes it impossible to
3591 * determine when the sessiond _and_ the consumerd have both
3592 * destroyed their representation of a channel. For one, the
3593 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3594 * callbacks in both daemons.
3596 * However, it is also impossible for the sessiond to know when
3597 * the consumer daemon is done destroying its channel(s) since
3598 * it occurs as a reaction to the closing of the channel's file
3599 * descriptor. There is no resulting communication initiated
3600 * from the consumerd to the sessiond to confirm that the
3601 * operation is completed (and was successful).
3603 * Until this is all fixed, the session daemon checks for the
3604 * removal of the session's shm path which makes it possible
3605 * to safely advertise a session as having been destroyed.
3607 * Prior to this fix, it was not possible to reliably save
3608 * a session making use of the --shm-path option, destroy it,
3609 * and load it again. This is because the creation of the
3610 * session would fail upon seeing the session's shm path
3611 * already in existence.
3613 * Note that none of the error paths in the check for the
3614 * directory's existence return an error. This is normal
3615 * as there isn't much that can be done. The session will
3616 * be destroyed properly, except that we can't offer the
3617 * guarantee that the same session can be re-created.
3619 current_completion_handler
= &destroy_completion_handler
.handler
;
3620 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3622 sizeof(destroy_completion_handler
.shm_path
));
3627 * The session is destroyed. However, note that the command context
3628 * still holds a reference to the session, thus delaying its destruction
3629 * _at least_ up to the point when that reference is released.
3631 session_destroy(session
);
3632 if (reply_context
) {
3633 reply_context
->destruction_status
= destruction_last_error
;
3634 ret
= session_add_destroy_notifier(session
,
3635 cmd_destroy_session_reply
,
3636 (void *) reply_context
);
3638 ret
= LTTNG_ERR_FATAL
;
3650 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3652 int cmd_register_consumer(struct ltt_session
*session
,
3653 enum lttng_domain_type domain
, const char *sock_path
,
3654 struct consumer_data
*cdata
)
3657 struct consumer_socket
*socket
= NULL
;
3659 LTTNG_ASSERT(session
);
3660 LTTNG_ASSERT(cdata
);
3661 LTTNG_ASSERT(sock_path
);
3664 case LTTNG_DOMAIN_KERNEL
:
3666 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3668 LTTNG_ASSERT(ksess
);
3670 /* Can't register a consumer if there is already one */
3671 if (ksess
->consumer_fds_sent
!= 0) {
3672 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3676 sock
= lttcomm_connect_unix_sock(sock_path
);
3678 ret
= LTTNG_ERR_CONNECT_FAIL
;
3681 cdata
->cmd_sock
= sock
;
3683 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3684 if (socket
== NULL
) {
3687 PERROR("close register consumer");
3689 cdata
->cmd_sock
= -1;
3690 ret
= LTTNG_ERR_FATAL
;
3694 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3695 if (socket
->lock
== NULL
) {
3696 PERROR("zmalloc pthread mutex");
3697 ret
= LTTNG_ERR_FATAL
;
3701 pthread_mutex_init(socket
->lock
, NULL
);
3702 socket
->registered
= 1;
3705 consumer_add_socket(socket
, ksess
->consumer
);
3708 pthread_mutex_lock(&cdata
->pid_mutex
);
3710 pthread_mutex_unlock(&cdata
->pid_mutex
);
3715 /* TODO: Userspace tracing */
3716 ret
= LTTNG_ERR_UND
;
3724 consumer_destroy_socket(socket
);
3730 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3732 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3733 struct lttng_domain
**domains
)
3738 struct lttng_ht_iter iter
;
3740 if (session
->kernel_session
!= NULL
) {
3741 DBG3("Listing domains found kernel domain");
3745 if (session
->ust_session
!= NULL
) {
3746 DBG3("Listing domains found UST global domain");
3750 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3752 if (agt
->being_used
) {
3763 *domains
= calloc
<lttng_domain
>(nb_dom
);
3764 if (*domains
== NULL
) {
3765 ret
= LTTNG_ERR_FATAL
;
3769 if (session
->kernel_session
!= NULL
) {
3770 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3772 /* Kernel session buffer type is always GLOBAL */
3773 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3778 if (session
->ust_session
!= NULL
) {
3779 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3780 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3784 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3786 if (agt
->being_used
) {
3787 (*domains
)[index
].type
= agt
->domain
;
3788 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3798 /* Return negative value to differentiate return code */
3804 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3806 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3807 struct ltt_session
*session
,
3808 struct lttng_payload
*payload
)
3812 struct lttcomm_list_command_header cmd_header
= {};
3813 size_t cmd_header_offset
;
3814 enum lttng_error_code ret_code
;
3819 DBG("Listing channels for session %s", session
->name
);
3821 cmd_header_offset
= payload
->buffer
.size
;
3823 /* Reserve space for command reply header. */
3824 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3825 cmd_header_offset
+ sizeof(cmd_header
));
3827 ret_code
= LTTNG_ERR_NOMEM
;
3832 case LTTNG_DOMAIN_KERNEL
:
3834 /* Kernel channels */
3835 struct ltt_kernel_channel
*kchan
;
3836 if (session
->kernel_session
!= NULL
) {
3837 cds_list_for_each_entry(kchan
,
3838 &session
->kernel_session
->channel_list
.head
, list
) {
3839 uint64_t discarded_events
, lost_packets
;
3840 struct lttng_channel_extended
*extended
;
3842 extended
= (struct lttng_channel_extended
*)
3843 kchan
->channel
->attr
.extended
.ptr
;
3845 ret
= get_kernel_runtime_stats(session
, kchan
,
3846 &discarded_events
, &lost_packets
);
3848 ret_code
= LTTNG_ERR_UNK
;
3853 * Update the discarded_events and lost_packets
3854 * count for the channel
3856 extended
->discarded_events
= discarded_events
;
3857 extended
->lost_packets
= lost_packets
;
3859 ret
= lttng_channel_serialize(
3860 kchan
->channel
, &payload
->buffer
);
3862 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3863 kchan
->channel
->name
);
3864 ret_code
= LTTNG_ERR_UNK
;
3873 case LTTNG_DOMAIN_UST
:
3875 struct lttng_ht_iter iter
;
3876 struct ltt_ust_channel
*uchan
;
3879 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3880 &iter
.iter
, uchan
, node
.node
) {
3881 uint64_t discarded_events
= 0, lost_packets
= 0;
3882 struct lttng_channel
*channel
= NULL
;
3883 struct lttng_channel_extended
*extended
;
3885 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3887 ret_code
= LTTNG_ERR_NOMEM
;
3891 extended
= (struct lttng_channel_extended
*)
3892 channel
->attr
.extended
.ptr
;
3894 ret
= get_ust_runtime_stats(session
, uchan
,
3895 &discarded_events
, &lost_packets
);
3897 lttng_channel_destroy(channel
);
3898 ret_code
= LTTNG_ERR_UNK
;
3902 extended
->discarded_events
= discarded_events
;
3903 extended
->lost_packets
= lost_packets
;
3905 ret
= lttng_channel_serialize(
3906 channel
, &payload
->buffer
);
3908 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3910 lttng_channel_destroy(channel
);
3911 ret_code
= LTTNG_ERR_UNK
;
3915 lttng_channel_destroy(channel
);
3925 if (i
> UINT32_MAX
) {
3926 ERR("Channel count would overflow the channel listing command's reply");
3927 ret_code
= LTTNG_ERR_OVERFLOW
;
3931 /* Update command reply header. */
3932 cmd_header
.count
= (uint32_t) i
;
3933 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3934 sizeof(cmd_header
));
3935 ret_code
= LTTNG_OK
;
3942 * Command LTTNG_LIST_EVENTS processed by the client thread.
3944 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3945 struct ltt_session
*session
,
3947 struct lttng_payload
*reply_payload
)
3949 int buffer_resize_ret
;
3950 enum lttng_error_code ret_code
= LTTNG_OK
;
3951 struct lttcomm_list_command_header reply_command_header
= {};
3952 size_t reply_command_header_offset
;
3953 unsigned int nb_events
= 0;
3955 assert(reply_payload
);
3957 /* Reserve space for command reply header. */
3958 reply_command_header_offset
= reply_payload
->buffer
.size
;
3959 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3960 reply_command_header_offset
+
3961 sizeof(struct lttcomm_list_command_header
));
3962 if (buffer_resize_ret
) {
3963 ret_code
= LTTNG_ERR_NOMEM
;
3968 case LTTNG_DOMAIN_KERNEL
:
3969 if (session
->kernel_session
!= NULL
) {
3970 ret_code
= list_lttng_kernel_events(channel_name
,
3971 session
->kernel_session
, reply_payload
, &nb_events
);
3975 case LTTNG_DOMAIN_UST
:
3977 if (session
->ust_session
!= NULL
) {
3978 ret_code
= list_lttng_ust_global_events(channel_name
,
3979 &session
->ust_session
->domain_global
,
3980 reply_payload
, &nb_events
);
3985 case LTTNG_DOMAIN_LOG4J
:
3986 case LTTNG_DOMAIN_JUL
:
3987 case LTTNG_DOMAIN_PYTHON
:
3988 if (session
->ust_session
) {
3989 struct lttng_ht_iter iter
;
3993 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3994 &iter
.iter
, agt
, node
.node
) {
3995 if (agt
->domain
== domain
) {
3996 ret_code
= list_lttng_agent_events(
3997 agt
, reply_payload
, &nb_events
);
4006 ret_code
= LTTNG_ERR_UND
;
4010 if (nb_events
> UINT32_MAX
) {
4011 ret_code
= LTTNG_ERR_OVERFLOW
;
4015 /* Update command reply header. */
4016 reply_command_header
.count
= (uint32_t) nb_events
;
4017 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
4018 sizeof(reply_command_header
));
4025 * Using the session list, filled a lttng_session array to send back to the
4026 * client for session listing.
4028 * The session list lock MUST be acquired before calling this function. Use
4029 * session_lock_list() and session_unlock_list().
4031 enum lttng_error_code
cmd_list_lttng_sessions(
4032 struct lttng_payload
*reply_payload
, uid_t uid
, gid_t gid
)
4034 int buffer_resize_ret
;
4035 enum lttng_error_code ret_code
= LTTNG_OK
;
4036 struct lttcomm_list_command_header reply_command_header
= {};
4037 size_t reply_command_header_offset
;
4038 struct ltt_session
*session
;
4039 struct ltt_session_list
*list
= session_get_list();
4043 assert(reply_payload
);
4045 /* Reserve space for command reply header. */
4046 reply_command_header_offset
= reply_payload
->buffer
.size
;
4047 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
4048 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
4049 if (buffer_resize_ret
) {
4050 ret_code
= LTTNG_ERR_NOMEM
;
4054 DBG("Getting all available session for UID %d GID %d",
4057 cds_list_for_each_entry(session
, &list
->head
, list
) {
4058 struct lttng_session tmp_session
= {};
4059 struct lttng_session_extended tmp_extended
= {};
4060 struct lttng_payload trace_format_buffer
;
4061 lttng_payload_init(&trace_format_buffer
);
4063 tmp_session
.extended
.ptr
= &tmp_extended
;
4065 if (!session_get(session
)) {
4066 lttng_payload_reset(&trace_format_buffer
);
4070 * Only list the sessions the user can control.
4072 if (!session_access_ok(session
, uid
) ||
4073 session
->destroyed
) {
4074 session_put(session
);
4075 lttng_payload_reset(&trace_format_buffer
);
4079 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4080 struct ltt_ust_session
*usess
= session
->ust_session
;
4082 if (session
->consumer
->type
== CONSUMER_DST_NET
||
4083 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
4084 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
4085 ret
= build_network_session_path(
4086 tmp_session
.path
, sizeof(tmp_session
.path
), session
);
4088 ret
= snprintf(tmp_session
.path
, sizeof(tmp_session
.path
), "%s",
4089 session
->consumer
->dst
.session_root_path
);
4092 PERROR("snprintf session path");
4093 session_put(session
);
4097 session
->trace_format
->serialize(&trace_format_buffer
);
4099 strncpy(tmp_session
.name
, session
->name
, NAME_MAX
);
4100 tmp_session
.name
[NAME_MAX
- 1] = '\0';
4101 tmp_session
.enabled
= session
->active
;
4102 tmp_session
.snapshot_mode
= session
->snapshot_mode
;
4103 tmp_session
.live_timer_interval
= session
->live_timer
;
4104 LTTNG_OPTIONAL_SET(&tmp_extended
.creation_time
, (uint64_t) session
->creation_time
);
4105 tmp_extended
.serialized_trace_format_descriptor
=
4106 lttng_buffer_view_from_dynamic_buffer(&trace_format_buffer
.buffer
,
4107 0, trace_format_buffer
.buffer
.size
);
4108 ret
= lttng_session_serialize(&tmp_session
, reply_payload
);
4109 lttng_payload_reset(&trace_format_buffer
);
4111 ret_code
= LTTNG_ERR_FATAL
;
4115 session_put(session
);
4118 if (i
> UINT32_MAX
) {
4119 ret_code
= LTTNG_ERR_OVERFLOW
;
4123 /* Update command reply header. */
4124 reply_command_header
.count
= (uint32_t) i
;
4125 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
4126 sizeof(reply_command_header
));
4127 ret_code
= LTTNG_OK
;
4133 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4134 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4136 int cmd_data_pending(struct ltt_session
*session
)
4139 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4140 struct ltt_ust_session
*usess
= session
->ust_session
;
4142 LTTNG_ASSERT(session
);
4144 DBG("Data pending for session %s", session
->name
);
4146 /* Session MUST be stopped to ask for data availability. */
4147 if (session
->active
) {
4148 ret
= LTTNG_ERR_SESSION_STARTED
;
4152 * If stopped, just make sure we've started before else the above call
4153 * will always send that there is data pending.
4155 * The consumer assumes that when the data pending command is received,
4156 * the trace has been started before or else no output data is written
4157 * by the streams which is a condition for data pending. So, this is
4158 * *VERY* important that we don't ask the consumer before a start
4161 if (!session
->has_been_started
) {
4167 /* A rotation is still pending, we have to wait. */
4168 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4169 DBG("Rotate still pending for session %s", session
->name
);
4174 if (ksess
&& ksess
->consumer
) {
4175 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4177 /* Data is still being extracted for the kernel. */
4182 if (usess
&& usess
->consumer
) {
4183 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4185 /* Data is still being extracted for the kernel. */
4190 /* Data is ready to be read by a viewer */
4198 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4200 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4202 int cmd_snapshot_add_output(struct ltt_session
*session
,
4203 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4206 struct snapshot_output
*new_output
;
4208 LTTNG_ASSERT(session
);
4209 LTTNG_ASSERT(output
);
4211 DBG("Cmd snapshot add output for session %s", session
->name
);
4214 * Can't create an output if the session is not set in no-output mode.
4216 if (session
->output_traces
) {
4217 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4221 if (session
->has_non_mmap_channel
) {
4222 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4226 /* Only one output is allowed until we have the "tee" feature. */
4227 if (session
->snapshot
.nb_output
== 1) {
4228 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4232 new_output
= snapshot_output_alloc();
4234 ret
= LTTNG_ERR_NOMEM
;
4238 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4239 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4240 &session
->snapshot
);
4242 if (ret
== -ENOMEM
) {
4243 ret
= LTTNG_ERR_NOMEM
;
4245 ret
= LTTNG_ERR_INVALID
;
4251 snapshot_add_output(&session
->snapshot
, new_output
);
4253 *id
= new_output
->id
;
4260 snapshot_output_destroy(new_output
);
4266 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4268 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4270 int cmd_snapshot_del_output(struct ltt_session
*session
,
4271 const struct lttng_snapshot_output
*output
)
4274 struct snapshot_output
*sout
= NULL
;
4276 LTTNG_ASSERT(session
);
4277 LTTNG_ASSERT(output
);
4282 * Permission denied to create an output if the session is not
4283 * set in no output mode.
4285 if (session
->output_traces
) {
4286 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4291 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4293 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4294 } else if (*output
->name
!= '\0') {
4295 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4297 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4300 ret
= LTTNG_ERR_INVALID
;
4304 snapshot_delete_output(&session
->snapshot
, sout
);
4305 snapshot_output_destroy(sout
);
4314 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4316 * If no output is available, outputs is untouched and 0 is returned.
4318 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4320 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4321 struct lttng_snapshot_output
**outputs
)
4324 struct lttng_snapshot_output
*list
= NULL
;
4325 struct lttng_ht_iter iter
;
4326 struct snapshot_output
*output
;
4328 LTTNG_ASSERT(session
);
4329 LTTNG_ASSERT(outputs
);
4331 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4334 * Permission denied to create an output if the session is not
4335 * set in no output mode.
4337 if (session
->output_traces
) {
4338 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4342 if (session
->snapshot
.nb_output
== 0) {
4347 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4349 ret
= -LTTNG_ERR_NOMEM
;
4353 /* Copy list from session to the new list object. */
4355 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4356 output
, node
.node
) {
4357 LTTNG_ASSERT(output
->consumer
);
4358 list
[idx
].id
= output
->id
;
4359 list
[idx
].max_size
= output
->max_size
;
4360 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4361 sizeof(list
[idx
].name
))) {
4362 ret
= -LTTNG_ERR_INVALID
;
4365 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4366 if (lttng_strncpy(list
[idx
].ctrl_url
,
4367 output
->consumer
->dst
.session_root_path
,
4368 sizeof(list
[idx
].ctrl_url
))) {
4369 ret
= -LTTNG_ERR_INVALID
;
4374 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4375 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4377 ret
= -LTTNG_ERR_NOMEM
;
4382 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4383 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4385 ret
= -LTTNG_ERR_NOMEM
;
4394 ret
= session
->snapshot
.nb_output
;
4403 * Check if we can regenerate the metadata for this session.
4404 * Only kernel, UST per-uid and non-live sessions are supported.
4406 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4409 int check_regenerate_metadata_support(struct ltt_session
*session
)
4413 LTTNG_ASSERT(session
);
4415 if (session
->live_timer
!= 0) {
4416 ret
= LTTNG_ERR_LIVE_SESSION
;
4419 if (!session
->active
) {
4420 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4423 if (session
->ust_session
) {
4424 switch (session
->ust_session
->buffer_type
) {
4425 case LTTNG_BUFFER_PER_UID
:
4427 case LTTNG_BUFFER_PER_PID
:
4428 ret
= LTTNG_ERR_PER_PID_SESSION
;
4432 ret
= LTTNG_ERR_UNK
;
4436 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4437 session
->consumer
->relay_minor_version
< 8) {
4438 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4448 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4450 * Ask the consumer to truncate the existing metadata file(s) and
4451 * then regenerate the metadata. Live and per-pid sessions are not
4452 * supported and return an error.
4454 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4456 int cmd_regenerate_metadata(struct ltt_session
*session
)
4460 LTTNG_ASSERT(session
);
4462 ret
= check_regenerate_metadata_support(session
);
4467 if (session
->kernel_session
) {
4468 ret
= kernctl_session_regenerate_metadata(
4469 session
->kernel_session
->fd
);
4471 ERR("Failed to regenerate the kernel metadata");
4476 if (session
->ust_session
) {
4477 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4479 ERR("Failed to regenerate the UST metadata");
4483 DBG("Cmd metadata regenerate for session %s", session
->name
);
4491 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4493 * Ask the tracer to regenerate a new statedump.
4495 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4497 int cmd_regenerate_statedump(struct ltt_session
*session
)
4501 LTTNG_ASSERT(session
);
4503 if (!session
->active
) {
4504 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4508 if (session
->kernel_session
) {
4509 ret
= kernctl_session_regenerate_statedump(
4510 session
->kernel_session
->fd
);
4512 * Currently, the statedump in kernel can only fail if out
4516 if (ret
== -ENOMEM
) {
4517 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4519 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4521 ERR("Failed to regenerate the kernel statedump");
4526 if (session
->ust_session
) {
4527 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4529 * Currently, the statedump in UST always returns 0.
4532 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4533 ERR("Failed to regenerate the UST statedump");
4537 DBG("Cmd regenerate statedump for session %s", session
->name
);
4545 enum lttng_error_code
synchronize_tracer_notifier_register(
4546 struct notification_thread_handle
*notification_thread
,
4547 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4549 enum lttng_error_code ret_code
;
4550 const struct lttng_condition
*condition
=
4551 lttng_trigger_get_const_condition(trigger
);
4552 const char *trigger_name
;
4553 uid_t trigger_owner
;
4554 enum lttng_trigger_status trigger_status
;
4555 const enum lttng_domain_type trigger_domain
=
4556 lttng_trigger_get_underlying_domain_type_restriction(
4559 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4560 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4562 LTTNG_ASSERT(condition
);
4563 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4564 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4566 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4567 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4568 trigger_name
: "(anonymous)";
4570 session_lock_list();
4571 switch (trigger_domain
) {
4572 case LTTNG_DOMAIN_KERNEL
:
4574 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4575 if (ret_code
!= LTTNG_OK
) {
4576 enum lttng_error_code notif_thread_unregister_ret
;
4578 notif_thread_unregister_ret
=
4579 notification_thread_command_unregister_trigger(
4580 notification_thread
, trigger
);
4582 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4583 /* Return the original error code. */
4584 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4586 (int) trigger_owner
,
4592 case LTTNG_DOMAIN_UST
:
4593 ust_app_global_update_all_event_notifier_rules();
4595 case LTTNG_DOMAIN_JUL
:
4596 case LTTNG_DOMAIN_LOG4J
:
4597 case LTTNG_DOMAIN_PYTHON
:
4599 /* Agent domains. */
4600 struct agent
*agt
= agent_find_by_event_notifier_domain(
4604 agt
= agent_create(trigger_domain
);
4606 ret_code
= LTTNG_ERR_NOMEM
;
4607 goto end_unlock_session_list
;
4610 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4613 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4614 if (ret_code
!= LTTNG_OK
) {
4615 goto end_unlock_session_list
;
4620 case LTTNG_DOMAIN_NONE
:
4625 ret_code
= LTTNG_OK
;
4626 end_unlock_session_list
:
4627 session_unlock_list();
4631 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4632 struct lttng_trigger
*trigger
,
4633 bool is_trigger_anonymous
,
4634 struct notification_thread_handle
*notification_thread
,
4635 struct lttng_trigger
**return_trigger
)
4637 enum lttng_error_code ret_code
;
4638 const char *trigger_name
;
4639 uid_t trigger_owner
;
4640 enum lttng_trigger_status trigger_status
;
4642 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4643 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4644 trigger_name
: "(anonymous)";
4646 trigger_status
= lttng_trigger_get_owner_uid(
4647 trigger
, &trigger_owner
);
4648 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4650 DBG("Running register trigger command: 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
));
4655 * Validate the trigger credentials against the command credentials.
4656 * Only the root user can register a trigger with non-matching
4659 if (!lttng_credentials_is_equal_uid(
4660 lttng_trigger_get_credentials(trigger
),
4662 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4663 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4664 trigger_name
, (int) trigger_owner
,
4665 (int) lttng_credentials_get_uid(cmd_creds
));
4666 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4672 * The bytecode generation also serves as a validation step for the
4673 * bytecode expressions.
4675 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4676 if (ret_code
!= LTTNG_OK
) {
4677 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4678 trigger_name
, (int) trigger_owner
, ret_code
);
4683 * A reference to the trigger is acquired by the notification thread.
4684 * It is safe to return the same trigger to the caller since it the
4685 * other user holds a reference.
4687 * The trigger is modified during the execution of the
4688 * "register trigger" command. However, by the time the command returns,
4689 * it is safe to use without any locking as its properties are
4692 ret_code
= notification_thread_command_register_trigger(
4693 notification_thread
, trigger
, is_trigger_anonymous
);
4694 if (ret_code
!= LTTNG_OK
) {
4695 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4696 trigger_name
, (int) trigger_owner
, ret_code
);
4700 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4701 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4702 trigger_name
: "(anonymous)";
4705 * Synchronize tracers if the trigger adds an event notifier.
4707 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4708 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4709 trigger
, cmd_creds
);
4710 if (ret_code
!= LTTNG_OK
) {
4711 ERR("Error registering tracer notifier: %s",
4712 lttng_strerror(-ret_code
));
4718 * Return an updated trigger to the client.
4720 * Since a modified version of the same trigger is returned, acquire a
4721 * reference to the trigger so the caller doesn't have to care if those
4722 * are distinct instances or not.
4724 if (ret_code
== LTTNG_OK
) {
4725 lttng_trigger_get(trigger
);
4726 *return_trigger
= trigger
;
4727 /* Ownership of trigger was transferred to caller. */
4735 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4736 const struct lttng_trigger
*trigger
)
4738 enum lttng_error_code ret_code
;
4739 const struct lttng_condition
*condition
=
4740 lttng_trigger_get_const_condition(trigger
);
4741 const enum lttng_domain_type trigger_domain
=
4742 lttng_trigger_get_underlying_domain_type_restriction(
4745 LTTNG_ASSERT(condition
);
4746 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4747 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4749 session_lock_list();
4750 switch (trigger_domain
) {
4751 case LTTNG_DOMAIN_KERNEL
:
4752 ret_code
= kernel_unregister_event_notifier(trigger
);
4753 if (ret_code
!= LTTNG_OK
) {
4754 goto end_unlock_session_list
;
4758 case LTTNG_DOMAIN_UST
:
4759 ust_app_global_update_all_event_notifier_rules();
4761 case LTTNG_DOMAIN_JUL
:
4762 case LTTNG_DOMAIN_LOG4J
:
4763 case LTTNG_DOMAIN_PYTHON
:
4765 /* Agent domains. */
4766 struct agent
*agt
= agent_find_by_event_notifier_domain(
4770 * This trigger was never registered in the first place. Calling
4771 * this function under those circumstances is an internal error.
4774 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4775 if (ret_code
!= LTTNG_OK
) {
4776 goto end_unlock_session_list
;
4781 case LTTNG_DOMAIN_NONE
:
4786 ret_code
= LTTNG_OK
;
4788 end_unlock_session_list
:
4789 session_unlock_list();
4793 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4794 const struct lttng_trigger
*trigger
,
4795 struct notification_thread_handle
*notification_thread
)
4797 enum lttng_error_code ret_code
;
4798 const char *trigger_name
;
4799 uid_t trigger_owner
;
4800 enum lttng_trigger_status trigger_status
;
4801 struct lttng_trigger
*sessiond_trigger
= NULL
;
4803 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4804 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4805 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4806 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4808 DBG("Running unregister trigger command: 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
));
4813 * Validate the trigger credentials against the command credentials.
4814 * Only the root user can unregister a trigger with non-matching
4817 if (!lttng_credentials_is_equal_uid(
4818 lttng_trigger_get_credentials(trigger
),
4820 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4821 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4822 trigger_name
, (int) trigger_owner
,
4823 (int) lttng_credentials_get_uid(cmd_creds
));
4824 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4829 /* Fetch the sessiond side trigger object. */
4830 ret_code
= notification_thread_command_get_trigger(
4831 notification_thread
, trigger
, &sessiond_trigger
);
4832 if (ret_code
!= LTTNG_OK
) {
4833 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4834 trigger_name
, (int) trigger_owner
, ret_code
);
4838 LTTNG_ASSERT(sessiond_trigger
);
4841 * From this point on, no matter what, consider the trigger
4844 * We set the unregistered state of the sessiond side trigger object in
4845 * the client thread since we want to minimize the possibility of the
4846 * notification thread being stalled due to a long execution of an
4847 * action that required the trigger lock.
4849 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4851 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4853 if (ret_code
!= LTTNG_OK
) {
4854 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4855 trigger_name
, (int) trigger_owner
, ret_code
);
4860 * Synchronize tracers if the trigger removes an event notifier.
4861 * Do this even if the trigger unregistration failed to at least stop
4862 * the tracers from producing notifications associated with this
4865 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4866 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4867 if (ret_code
!= LTTNG_OK
) {
4868 ERR("Error unregistering trigger to tracer.");
4875 lttng_trigger_put(sessiond_trigger
);
4879 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4880 struct notification_thread_handle
*notification_thread
,
4881 struct lttng_triggers
**return_triggers
)
4884 enum lttng_error_code ret_code
;
4885 struct lttng_triggers
*triggers
= NULL
;
4887 /* Get the set of triggers from the notification thread. */
4888 ret_code
= notification_thread_command_list_triggers(
4889 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4890 if (ret_code
!= LTTNG_OK
) {
4894 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4896 ret_code
= LTTNG_ERR_UNK
;
4900 *return_triggers
= triggers
;
4902 ret_code
= LTTNG_OK
;
4904 lttng_triggers_destroy(triggers
);
4908 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4909 const struct lttng_error_query
*query
,
4910 struct lttng_error_query_results
**_results
,
4911 struct notification_thread_handle
*notification_thread
)
4913 enum lttng_error_code ret_code
;
4914 const struct lttng_trigger
*query_target_trigger
;
4915 const struct lttng_action
*query_target_action
= NULL
;
4916 struct lttng_trigger
*matching_trigger
= NULL
;
4917 const char *trigger_name
;
4918 uid_t trigger_owner
;
4919 enum lttng_trigger_status trigger_status
;
4920 struct lttng_error_query_results
*results
= NULL
;
4922 switch (lttng_error_query_get_target_type(query
)) {
4923 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4924 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4926 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4927 query_target_trigger
=
4928 lttng_error_query_condition_borrow_target(query
);
4930 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4931 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4938 LTTNG_ASSERT(query_target_trigger
);
4940 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4941 query_target_trigger
, &matching_trigger
);
4942 if (ret_code
!= LTTNG_OK
) {
4946 /* No longer needed. */
4947 query_target_trigger
= NULL
;
4949 if (lttng_error_query_get_target_type(query
) ==
4950 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4951 /* Get the sessiond-side version of the target action. */
4952 query_target_action
=
4953 lttng_error_query_action_borrow_action_target(
4954 query
, matching_trigger
);
4957 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4958 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4959 trigger_name
: "(anonymous)";
4960 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4962 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4964 results
= lttng_error_query_results_create();
4966 ret_code
= LTTNG_ERR_NOMEM
;
4970 DBG("Running \"execute error query\" command: 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
));
4975 * Validate the trigger credentials against the command credentials.
4976 * Only the root user can target a trigger with non-matching
4979 if (!lttng_credentials_is_equal_uid(
4980 lttng_trigger_get_credentials(matching_trigger
),
4982 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4983 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4984 trigger_name
, (int) trigger_owner
,
4985 (int) lttng_credentials_get_uid(cmd_creds
));
4986 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4991 switch (lttng_error_query_get_target_type(query
)) {
4992 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4993 trigger_status
= lttng_trigger_add_error_results(
4994 matching_trigger
, results
);
4996 switch (trigger_status
) {
4997 case LTTNG_TRIGGER_STATUS_OK
:
5000 ret_code
= LTTNG_ERR_UNK
;
5005 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
5007 trigger_status
= lttng_trigger_condition_add_error_results(
5008 matching_trigger
, results
);
5010 switch (trigger_status
) {
5011 case LTTNG_TRIGGER_STATUS_OK
:
5014 ret_code
= LTTNG_ERR_UNK
;
5020 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
5022 const enum lttng_action_status action_status
=
5023 lttng_action_add_error_query_results(
5024 query_target_action
, results
);
5026 switch (action_status
) {
5027 case LTTNG_ACTION_STATUS_OK
:
5030 ret_code
= LTTNG_ERR_UNK
;
5041 *_results
= results
;
5043 ret_code
= LTTNG_OK
;
5045 lttng_trigger_put(matching_trigger
);
5046 lttng_error_query_results_destroy(results
);
5051 * Send relayd sockets from snapshot output to consumer. Ignore request if the
5052 * snapshot output is *not* set with a remote destination.
5054 * Return LTTNG_OK on success or a LTTNG_ERR code.
5056 static enum lttng_error_code
set_relayd_for_snapshot(
5057 struct consumer_output
*output
,
5058 const struct ltt_session
*session
)
5060 enum lttng_error_code status
= LTTNG_OK
;
5061 struct lttng_ht_iter iter
;
5062 struct consumer_socket
*socket
;
5063 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
5064 const char *base_path
;
5066 LTTNG_ASSERT(output
);
5067 LTTNG_ASSERT(session
);
5069 DBG2("Set relayd object from snapshot output");
5071 if (session
->current_trace_chunk
) {
5072 enum lttng_trace_chunk_status chunk_status
=
5073 lttng_trace_chunk_get_id(
5074 session
->current_trace_chunk
,
5075 ¤t_chunk_id
.value
);
5077 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5078 current_chunk_id
.is_set
= true;
5080 ERR("Failed to get current trace chunk id");
5081 status
= LTTNG_ERR_UNK
;
5086 /* Ignore if snapshot consumer output is not network. */
5087 if (output
->type
!= CONSUMER_DST_NET
) {
5092 * The snapshot record URI base path overrides the session
5095 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5096 base_path
= output
->dst
.net
.control
.subdir
;
5098 base_path
= session
->base_path
;
5102 * For each consumer socket, create and send the relayd object of the
5106 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
5107 pthread_mutex_lock(socket
->lock
);
5108 status
= send_consumer_relayd_sockets(*session
, output
, socket
, base_path
,
5109 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
5110 pthread_mutex_unlock(socket
->lock
);
5111 if (status
!= LTTNG_OK
) {
5123 * Record a kernel snapshot.
5125 * Return LTTNG_OK on success or a LTTNG_ERR code.
5127 static enum lttng_error_code
record_kernel_snapshot(
5128 struct ltt_kernel_session
*ksess
,
5129 const struct consumer_output
*output
,
5130 const struct ltt_session
*session
,
5131 uint64_t nb_packets_per_stream
)
5133 enum lttng_error_code status
;
5135 LTTNG_ASSERT(ksess
);
5136 LTTNG_ASSERT(output
);
5137 LTTNG_ASSERT(session
);
5139 status
= kernel_snapshot_record(
5140 ksess
, output
, nb_packets_per_stream
);
5145 * Record a UST snapshot.
5147 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5149 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5150 const struct consumer_output
*output
,
5151 const struct ltt_session
*session
,
5152 uint64_t nb_packets_per_stream
)
5154 enum lttng_error_code status
;
5156 LTTNG_ASSERT(usess
);
5157 LTTNG_ASSERT(output
);
5158 LTTNG_ASSERT(session
);
5160 status
= ust_app_snapshot_record(
5161 usess
, output
, nb_packets_per_stream
);
5166 uint64_t get_session_size_one_more_packet_per_stream(
5167 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5169 uint64_t tot_size
= 0;
5171 if (session
->kernel_session
) {
5172 struct ltt_kernel_channel
*chan
;
5173 const struct ltt_kernel_session
*ksess
=
5174 session
->kernel_session
;
5176 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5177 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5179 * Don't take channel into account if we
5180 * already grab all its packets.
5184 tot_size
+= chan
->channel
->attr
.subbuf_size
5185 * chan
->stream_count
;
5189 if (session
->ust_session
) {
5190 const struct ltt_ust_session
*usess
= session
->ust_session
;
5192 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5200 * Calculate the number of packets we can grab from each stream that
5201 * fits within the overall snapshot max size.
5203 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5204 * the number of packets per stream.
5206 * TODO: this approach is not perfect: we consider the worse case
5207 * (packet filling the sub-buffers) as an upper bound, but we could do
5208 * better if we do this calculation while we actually grab the packet
5209 * content: we would know how much padding we don't actually store into
5212 * This algorithm is currently bounded by the number of packets per
5215 * Since we call this algorithm before actually grabbing the data, it's
5216 * an approximation: for instance, applications could appear/disappear
5217 * in between this call and actually grabbing data.
5220 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5224 uint64_t cur_nb_packets
= 0;
5227 return 0; /* Infinite */
5230 size_left
= max_size
;
5232 uint64_t one_more_packet_tot_size
;
5234 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5235 session
, cur_nb_packets
);
5236 if (!one_more_packet_tot_size
) {
5237 /* We are already grabbing all packets. */
5240 size_left
-= one_more_packet_tot_size
;
5241 if (size_left
< 0) {
5246 if (!cur_nb_packets
&& size_left
!= max_size
) {
5247 /* Not enough room to grab one packet of each stream, error. */
5250 return cur_nb_packets
;
5254 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5255 const struct snapshot_output
*snapshot_output
)
5257 int64_t nb_packets_per_stream
;
5258 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5260 enum lttng_error_code ret_code
= LTTNG_OK
;
5261 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5262 struct consumer_output
*original_ust_consumer_output
= NULL
;
5263 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5264 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5265 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5267 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5269 snapshot_output
->name
,
5270 snapshot_output
->datetime
,
5271 snapshot_output
->nb_snapshot
);
5272 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5273 ERR("Failed to format snapshot name");
5274 ret_code
= LTTNG_ERR_INVALID
;
5277 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5278 snapshot_output
->name
, session
->name
,
5279 snapshot_chunk_name
);
5280 if (!session
->kernel_session
&& !session
->ust_session
) {
5281 ERR("Failed to record snapshot as no channels exist");
5282 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5286 if (session
->kernel_session
) {
5287 original_kernel_consumer_output
=
5288 session
->kernel_session
->consumer
;
5289 snapshot_kernel_consumer_output
=
5290 consumer_copy_output(snapshot_output
->consumer
);
5291 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5292 snapshot_chunk_name
);
5294 /* Copy the original domain subdir. */
5295 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5296 original_kernel_consumer_output
->domain_subdir
);
5298 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5299 original_kernel_consumer_output
);
5301 ERR("Failed to copy consumer sockets from snapshot output configuration");
5302 ret_code
= LTTNG_ERR_NOMEM
;
5305 ret_code
= set_relayd_for_snapshot(
5306 snapshot_kernel_consumer_output
, session
);
5307 if (ret_code
!= LTTNG_OK
) {
5308 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5311 session
->kernel_session
->consumer
=
5312 snapshot_kernel_consumer_output
;
5314 if (session
->ust_session
) {
5315 original_ust_consumer_output
= session
->ust_session
->consumer
;
5316 snapshot_ust_consumer_output
=
5317 consumer_copy_output(snapshot_output
->consumer
);
5318 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5319 snapshot_chunk_name
);
5321 /* Copy the original domain subdir. */
5322 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5323 original_ust_consumer_output
->domain_subdir
);
5325 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5326 original_ust_consumer_output
);
5328 ERR("Failed to copy consumer sockets from snapshot output configuration");
5329 ret_code
= LTTNG_ERR_NOMEM
;
5332 ret_code
= set_relayd_for_snapshot(
5333 snapshot_ust_consumer_output
, session
);
5334 if (ret_code
!= LTTNG_OK
) {
5335 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5338 session
->ust_session
->consumer
=
5339 snapshot_ust_consumer_output
;
5342 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5343 snapshot_kernel_consumer_output
?:
5344 snapshot_ust_consumer_output
,
5345 consumer_output_get_base_path(
5346 snapshot_output
->consumer
),
5347 snapshot_chunk_name
);
5348 if (!snapshot_trace_chunk
) {
5349 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5351 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5354 LTTNG_ASSERT(!session
->current_trace_chunk
);
5355 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5356 lttng_trace_chunk_put(snapshot_trace_chunk
);
5357 snapshot_trace_chunk
= NULL
;
5359 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5361 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5365 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5366 snapshot_output
->max_size
);
5367 if (nb_packets_per_stream
< 0) {
5368 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5369 goto error_close_trace_chunk
;
5372 if (session
->kernel_session
) {
5373 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5374 snapshot_kernel_consumer_output
, session
,
5375 nb_packets_per_stream
);
5376 if (ret_code
!= LTTNG_OK
) {
5377 goto error_close_trace_chunk
;
5381 if (session
->ust_session
) {
5382 ret_code
= record_ust_snapshot(session
->ust_session
,
5383 snapshot_ust_consumer_output
, session
,
5384 nb_packets_per_stream
);
5385 if (ret_code
!= LTTNG_OK
) {
5386 goto error_close_trace_chunk
;
5390 error_close_trace_chunk
:
5391 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5392 ERR("Failed to release the current trace chunk of session \"%s\"",
5394 ret_code
= LTTNG_ERR_UNK
;
5397 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5398 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5400 * Don't goto end; make sure the chunk is closed for the session
5401 * to allow future snapshots.
5403 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5405 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5408 lttng_trace_chunk_put(snapshot_trace_chunk
);
5409 snapshot_trace_chunk
= NULL
;
5411 if (original_ust_consumer_output
) {
5412 session
->ust_session
->consumer
= original_ust_consumer_output
;
5414 if (original_kernel_consumer_output
) {
5415 session
->kernel_session
->consumer
=
5416 original_kernel_consumer_output
;
5418 consumer_output_put(snapshot_ust_consumer_output
);
5419 consumer_output_put(snapshot_kernel_consumer_output
);
5424 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5426 * The wait parameter is ignored so this call always wait for the snapshot to
5427 * complete before returning.
5429 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5431 int cmd_snapshot_record(struct ltt_session
*session
,
5432 const struct lttng_snapshot_output
*output
,
5433 int wait
__attribute__((unused
)))
5435 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5437 unsigned int snapshot_success
= 0;
5439 struct snapshot_output
*tmp_output
= NULL
;
5441 LTTNG_ASSERT(session
);
5442 LTTNG_ASSERT(output
);
5444 DBG("Cmd snapshot record for session %s", session
->name
);
5446 /* Get the datetime for the snapshot output directory. */
5447 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5450 cmd_ret
= LTTNG_ERR_INVALID
;
5455 * Permission denied to create an output if the session is not
5456 * set in no output mode.
5458 if (session
->output_traces
) {
5459 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5463 /* The session needs to be started at least once. */
5464 if (!session
->has_been_started
) {
5465 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5469 /* Use temporary output for the session. */
5470 if (*output
->ctrl_url
!= '\0') {
5471 tmp_output
= snapshot_output_alloc();
5473 cmd_ret
= LTTNG_ERR_NOMEM
;
5477 ret
= snapshot_output_init(session
, output
->max_size
,
5479 output
->ctrl_url
, output
->data_url
,
5483 if (ret
== -ENOMEM
) {
5484 cmd_ret
= LTTNG_ERR_NOMEM
;
5486 cmd_ret
= LTTNG_ERR_INVALID
;
5490 /* Use the global session count for the temporary snapshot. */
5491 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5493 /* Use the global datetime */
5494 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5495 cmd_ret
= snapshot_record(session
, tmp_output
);
5496 if (cmd_ret
!= LTTNG_OK
) {
5499 snapshot_success
= 1;
5501 struct snapshot_output
*sout
;
5502 struct lttng_ht_iter iter
;
5505 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5506 &iter
.iter
, sout
, node
.node
) {
5507 struct snapshot_output output_copy
;
5510 * Make a local copy of the output and override output
5511 * parameters with those provided as part of the
5514 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5516 if (output
->max_size
!= (uint64_t) -1ULL) {
5517 output_copy
.max_size
= output
->max_size
;
5520 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5521 memcpy(output_copy
.datetime
, datetime
,
5524 /* Use temporary name. */
5525 if (*output
->name
!= '\0') {
5526 if (lttng_strncpy(output_copy
.name
,
5528 sizeof(output_copy
.name
))) {
5529 cmd_ret
= LTTNG_ERR_INVALID
;
5535 cmd_ret
= snapshot_record(session
, &output_copy
);
5536 if (cmd_ret
!= LTTNG_OK
) {
5540 snapshot_success
= 1;
5545 if (snapshot_success
) {
5546 session
->snapshot
.nb_snapshot
++;
5548 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5553 snapshot_output_destroy(tmp_output
);
5559 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5561 int cmd_set_session_shm_path(struct ltt_session
*session
,
5562 const char *shm_path
)
5565 LTTNG_ASSERT(session
);
5568 * Can only set shm path before session is started.
5570 if (session
->has_been_started
) {
5571 return LTTNG_ERR_SESSION_STARTED
;
5574 strncpy(session
->shm_path
, shm_path
,
5575 sizeof(session
->shm_path
));
5576 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5582 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5584 * Ask the consumer to rotate the session output directory.
5585 * The session lock must be held.
5587 * Returns LTTNG_OK on success or else a negative LTTng error code.
5589 int cmd_rotate_session(struct ltt_session
*session
,
5590 struct lttng_rotate_session_return
*rotate_return
,
5591 bool quiet_rotation
,
5592 enum lttng_trace_chunk_command_type command
)
5595 uint64_t ongoing_rotation_chunk_id
;
5596 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5597 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5598 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5599 enum lttng_trace_chunk_status chunk_status
;
5600 bool failed_to_rotate
= false;
5601 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5603 LTTNG_ASSERT(session
);
5605 if (!session
->has_been_started
) {
5606 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5611 * Explicit rotation is not supported for live sessions.
5612 * However, live sessions can perform a quiet rotation on
5614 * Rotation is not supported for snapshot traces (no output).
5616 if ((!quiet_rotation
&& session
->live_timer
) ||
5617 !session
->output_traces
) {
5618 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5622 /* Unsupported feature in lttng-relayd before 2.11. */
5623 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5624 (session
->consumer
->relay_major_version
== 2 &&
5625 session
->consumer
->relay_minor_version
< 11)) {
5626 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5630 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5631 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5632 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5636 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5637 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5639 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5644 * After a stop, we only allow one rotation to occur, the other ones are
5645 * useless until a new start.
5647 if (session
->rotated_after_last_stop
) {
5648 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5650 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5655 * After a stop followed by a clear, disallow following rotations a they would
5656 * generate empty chunks.
5658 if (session
->cleared_after_last_stop
) {
5659 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5661 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5665 if (session
->active
) {
5666 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5668 if (!new_trace_chunk
) {
5669 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5675 * The current trace chunk becomes the chunk being archived.
5677 * After this point, "chunk_being_archived" must absolutely
5678 * be closed on the consumer(s), otherwise it will never be
5679 * cleaned-up, which will result in a leak.
5681 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5682 &chunk_being_archived
);
5684 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5688 if (session
->kernel_session
) {
5689 cmd_ret
= kernel_rotate_session(session
);
5690 if (cmd_ret
!= LTTNG_OK
) {
5691 failed_to_rotate
= true;
5692 rotation_fail_code
= cmd_ret
;
5695 if (session
->ust_session
) {
5696 cmd_ret
= ust_app_rotate_session(session
);
5697 if (cmd_ret
!= LTTNG_OK
) {
5698 failed_to_rotate
= true;
5699 rotation_fail_code
= cmd_ret
;
5703 if (!session
->active
) {
5704 session
->rotated_after_last_stop
= true;
5707 if (!chunk_being_archived
) {
5708 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5710 if (failed_to_rotate
) {
5711 cmd_ret
= rotation_fail_code
;
5718 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5719 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5720 &ongoing_rotation_chunk_id
);
5721 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5723 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5724 command
, session
->last_chunk_path
);
5726 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5730 if (failed_to_rotate
) {
5731 cmd_ret
= rotation_fail_code
;
5735 session
->quiet_rotation
= quiet_rotation
;
5736 ret
= timer_session_rotation_pending_check_start(session
,
5737 DEFAULT_ROTATE_PENDING_TIMER
);
5739 cmd_ret
= LTTNG_ERR_UNK
;
5743 if (rotate_return
) {
5744 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5747 session
->chunk_being_archived
= chunk_being_archived
;
5748 chunk_being_archived
= NULL
;
5749 if (!quiet_rotation
) {
5750 ret
= notification_thread_command_session_rotation_ongoing(
5751 the_notification_thread_handle
, session
->id
,
5752 ongoing_rotation_chunk_id
);
5753 if (ret
!= LTTNG_OK
) {
5754 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5756 cmd_ret
= (lttng_error_code
) ret
;
5760 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5761 session
->name
, ongoing_rotation_chunk_id
);
5763 lttng_trace_chunk_put(new_trace_chunk
);
5764 lttng_trace_chunk_put(chunk_being_archived
);
5765 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5768 if (session_reset_rotation_state(session
,
5769 LTTNG_ROTATION_STATE_ERROR
)) {
5770 ERR("Failed to reset rotation state of session \"%s\"",
5777 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5779 * Check if the session has finished its rotation.
5781 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5783 int cmd_rotate_get_info(struct ltt_session
*session
,
5784 struct lttng_rotation_get_info_return
*info_return
,
5785 uint64_t rotation_id
)
5787 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5788 enum lttng_rotation_state rotation_state
;
5790 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5791 session
->most_recent_chunk_id
.value
);
5793 if (session
->chunk_being_archived
) {
5794 enum lttng_trace_chunk_status chunk_status
;
5797 chunk_status
= lttng_trace_chunk_get_id(
5798 session
->chunk_being_archived
,
5800 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5802 rotation_state
= rotation_id
== chunk_id
?
5803 LTTNG_ROTATION_STATE_ONGOING
:
5804 LTTNG_ROTATION_STATE_EXPIRED
;
5806 if (session
->last_archived_chunk_id
.is_set
&&
5807 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5808 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5810 rotation_state
= session
->rotation_state
;
5814 switch (rotation_state
) {
5815 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5816 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5819 case LTTNG_ROTATION_STATE_EXPIRED
:
5820 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5821 rotation_id
, session
->name
);
5823 case LTTNG_ROTATION_STATE_ONGOING
:
5824 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5825 rotation_id
, session
->name
);
5827 case LTTNG_ROTATION_STATE_COMPLETED
:
5831 char *current_tracing_path_reply
;
5832 size_t current_tracing_path_reply_len
;
5834 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5835 rotation_id
, session
->name
);
5837 switch (session_get_consumer_destination_type(session
)) {
5838 case CONSUMER_DST_LOCAL
:
5839 current_tracing_path_reply
=
5840 info_return
->location
.local
.absolute_path
;
5841 current_tracing_path_reply_len
=
5842 sizeof(info_return
->location
.local
.absolute_path
);
5843 info_return
->location_type
=
5844 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5845 fmt_ret
= asprintf(&chunk_path
,
5846 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5847 session_get_base_path(session
),
5848 session
->last_archived_chunk_name
);
5849 if (fmt_ret
== -1) {
5850 PERROR("Failed to format the path of the last archived trace chunk");
5851 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5852 cmd_ret
= LTTNG_ERR_UNK
;
5856 case CONSUMER_DST_NET
:
5858 uint16_t ctrl_port
, data_port
;
5860 current_tracing_path_reply
=
5861 info_return
->location
.relay
.relative_path
;
5862 current_tracing_path_reply_len
=
5863 sizeof(info_return
->location
.relay
.relative_path
);
5864 /* Currently the only supported relay protocol. */
5865 info_return
->location
.relay
.protocol
=
5866 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5868 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5869 session_get_net_consumer_hostname(session
),
5870 sizeof(info_return
->location
.relay
.host
));
5872 ERR("Failed to copy host name to rotate_get_info reply");
5873 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5874 cmd_ret
= LTTNG_ERR_SET_URL
;
5878 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5879 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5880 info_return
->location
.relay
.ports
.data
= data_port
;
5881 info_return
->location_type
=
5882 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5883 chunk_path
= strdup(session
->last_chunk_path
);
5885 ERR("Failed to allocate the path of the last archived trace chunk");
5886 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5887 cmd_ret
= LTTNG_ERR_UNK
;
5896 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5897 chunk_path
, current_tracing_path_reply_len
);
5900 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5901 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5902 cmd_ret
= LTTNG_ERR_UNK
;
5908 case LTTNG_ROTATION_STATE_ERROR
:
5909 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5910 rotation_id
, session
->name
);
5918 info_return
->status
= (int32_t) rotation_state
;
5923 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5925 * Configure the automatic rotation parameters.
5926 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5927 * 'activate' to false means deactivate the rotation schedule and validate that
5928 * 'new_value' has the same value as the currently active value.
5930 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5932 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5933 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5935 struct notification_thread_handle
*notification_thread_handle
)
5938 uint64_t *parameter_value
;
5940 LTTNG_ASSERT(session
);
5942 DBG("Cmd rotate set schedule session %s", session
->name
);
5944 if (session
->live_timer
|| !session
->output_traces
) {
5945 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5946 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5950 switch (schedule_type
) {
5951 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5952 parameter_value
= &session
->rotate_size
;
5954 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5955 parameter_value
= &session
->rotate_timer_period
;
5956 if (new_value
>= UINT_MAX
) {
5957 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5958 new_value
, UINT_MAX
);
5959 ret
= LTTNG_ERR_INVALID
;
5964 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5965 ret
= LTTNG_ERR_INVALID
;
5969 /* Improper use of the API. */
5970 if (new_value
== -1ULL) {
5971 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5972 ret
= LTTNG_ERR_INVALID
;
5977 * As indicated in struct ltt_session's comments, a value of == 0 means
5978 * this schedule rotation type is not in use.
5980 * Reject the command if we were asked to activate a schedule that was
5983 if (activate
&& *parameter_value
!= 0) {
5984 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5985 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5990 * Reject the command if we were asked to deactivate a schedule that was
5993 if (!activate
&& *parameter_value
== 0) {
5994 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5995 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
6000 * Reject the command if we were asked to deactivate a schedule that
6003 if (!activate
&& *parameter_value
!= new_value
) {
6004 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
6005 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
6009 *parameter_value
= activate
? new_value
: 0;
6011 switch (schedule_type
) {
6012 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
6013 if (activate
&& session
->active
) {
6015 * Only start the timer if the session is active,
6016 * otherwise it will be started when the session starts.
6018 ret
= timer_session_rotation_schedule_timer_start(
6019 session
, new_value
);
6021 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
6022 ret
= LTTNG_ERR_UNK
;
6026 ret
= timer_session_rotation_schedule_timer_stop(
6029 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
6030 ret
= LTTNG_ERR_UNK
;
6035 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
6037 ret
= subscribe_session_consumed_size_rotation(session
,
6038 new_value
, notification_thread_handle
);
6040 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
6041 ret
= LTTNG_ERR_UNK
;
6045 ret
= unsubscribe_session_consumed_size_rotation(session
,
6046 notification_thread_handle
);
6048 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
6049 ret
= LTTNG_ERR_UNK
;
6056 /* Would have been caught before. */
6068 enum lttng_error_code
cmd_execute_trace_format_support_query(
6069 struct command_ctx
& cmd_ctx
, int sock
, int& sock_error
)
6072 enum lttng_error_code ret_code
;
6073 struct lttng_payload query_payload
;
6074 size_t payload_len
, offset
= 0;
6075 ssize_t sock_recv_len
, consumed
;
6077 lttng::trace_format_descriptor::uptr trace_format
;
6079 lttng_payload_init(&query_payload
);
6081 /* Receive the payload */
6082 payload_len
= (size_t) cmd_ctx
.lsm
.u
.trace_format_support_query
.trace_format_length
+
6083 cmd_ctx
.lsm
.u
.trace_format_support_query
.type_payload_length
;
6084 ret
= lttng_dynamic_buffer_set_size(&query_payload
.buffer
, payload_len
);
6086 ret_code
= LTTNG_ERR_NOMEM
;
6089 sock_recv_len
= lttcomm_recv_unix_sock(sock
, query_payload
.buffer
.data
, payload_len
);
6090 if (sock_recv_len
< 0 || sock_recv_len
!= payload_len
) {
6091 ERR("Failed to receive error query in command payload");
6093 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
6097 /* Receive fds, if any. */
6098 if (cmd_ctx
.lsm
.fd_count
> 0) {
6099 sock_recv_len
= lttcomm_recv_payload_fds_unix_sock(
6100 sock
, cmd_ctx
.lsm
.fd_count
, &query_payload
);
6101 if (sock_recv_len
> 0 && sock_recv_len
!= cmd_ctx
.lsm
.fd_count
* sizeof(int)) {
6102 ERR("Failed to receive all file descriptors for error query in command payload: expected fd count = %u, ret = %d",
6103 cmd_ctx
.lsm
.fd_count
, (int) ret
);
6104 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
6107 } else if (sock_recv_len
<= 0) {
6108 ERR("Failed to receive file descriptors for error query in command payload: expected fd count = %u, ret = %d",
6109 cmd_ctx
.lsm
.fd_count
, (int) ret
);
6110 ret_code
= LTTNG_ERR_FATAL
;
6116 /* Deserialize trace_format_descriptor. */
6118 struct lttng_payload_view view
=
6119 lttng_payload_view_from_payload(&query_payload
, offset
, -1);
6120 if (!lttng_payload_view_is_valid(&view
)) {
6121 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
6125 consumed
= lttng::trace_format_descriptor::create_from_payload(&view
, trace_format
);
6127 if (consumed
!= cmd_ctx
.lsm
.u
.trace_format_support_query
.trace_format_length
) {
6128 ERR("Invalid trace format descriptor received as part of command payload");
6129 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
6135 switch (cmd_ctx
.lsm
.u
.trace_format_support_query
.query_type
) {
6138 if (cmd_ctx
.lsm
.domain
.type
!= LTTNG_DOMAIN_KERNEL
) {
6139 /* UST supports all trace format*/
6140 ret_code
= LTTNG_OK
;
6143 ret_code
= kernel_domain_check_trace_format_requirements(*trace_format
);
6148 struct lttcomm_relayd_sock
*relay_sock
= nullptr;
6149 struct lttng_uri uri
;
6150 uint64_t result_flags
= 0;
6151 uint64_t supported_trace_format
= 0;
6152 struct lttng_payload_view view
= lttng_payload_view_from_payload(&query_payload
,
6154 cmd_ctx
.lsm
.u
.trace_format_support_query
.type_payload_length
);
6155 if (!lttng_payload_view_is_valid(&view
)) {
6156 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
6160 memcpy(&uri
, view
.buffer
.data
, sizeof(uri
));
6161 ret_code
= create_connect_relayd(&uri
, &relay_sock
);
6162 if (ret_code
!= LTTNG_OK
) {
6167 ret_code
= relayd_get_configuration(relay_sock
,
6168 LTTCOMM_RELAYD_CONFIGURATION_QUERY_FLAG_SUPPORTED_TRACE_FORMAT
,
6169 result_flags
, &supported_trace_format
);
6170 if (ret_code
!= LTTNG_OK
) {
6174 if (!(supported_trace_format
& trace_format
->relayd_type())) {
6175 ERR("Relayd does not support the requested trace format");
6176 ret_code
= LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_RELAY_DAEMON
;
6180 ret_code
= LTTNG_OK
;
6185 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
6190 lttng_payload_reset(&query_payload
);
6194 /* Wait for a given path to be removed before continuing. */
6195 static enum lttng_error_code
wait_on_path(void *path_data
)
6197 const char *shm_path
= (const char *) path_data
;
6199 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6205 ret
= stat(shm_path
, &st
);
6207 if (errno
!= ENOENT
) {
6208 PERROR("stat() returned an error while checking for the existence of the shm path");
6210 DBG("shm path no longer exists, completing the destruction of session");
6214 if (!S_ISDIR(st
.st_mode
)) {
6215 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6220 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6226 * Returns a pointer to a handler to run on completion of a command.
6227 * Returns NULL if no handler has to be run for the last command executed.
6229 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6231 struct cmd_completion_handler
*handler
= current_completion_handler
;
6233 current_completion_handler
= NULL
;
6238 * Init command subsystem.
6243 * Set network sequence index to 1 for streams to match a relayd
6244 * socket on the consumer side.
6246 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6247 relayd_net_seq_idx
= 1;
6248 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6250 DBG("Command subsystem initialized");