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;
894 * Send both relayd sockets to a specific consumer and domain. This is a
895 * helper function to facilitate sending the information to the consumer for a
898 * The consumer socket lock must be held by the caller.
900 * Returns LTTNG_OK, or an LTTng error code on failure.
902 static lttng_error_code
send_consumer_relayd_sockets(const ltt_session
& session
,
903 struct consumer_output
*consumer
,
904 struct consumer_socket
*sock
,
905 const char *base_path
,
906 const uint64_t *current_chunk_id
)
908 enum lttng_error_code status
= LTTNG_OK
;
909 struct lttcomm_relayd_sock
*control_sock
= nullptr;
910 struct lttcomm_relayd_sock
*data_sock
= nullptr;
912 LTTNG_ASSERT(consumer
);
915 /* Sending control relayd socket. */
916 if (!sock
->control_sock_sent
) {
918 uint64_t result_flags
= 0;
919 /* Connect to relayd and make version check if uri is the control. */
920 status
= create_connect_relayd(&consumer
->dst
.net
.control
, &control_sock
);
921 if (status
!= LTTNG_OK
) {
924 LTTNG_ASSERT(control_sock
);
926 consumer
->relay_major_version
= control_sock
->major
;
927 consumer
->relay_minor_version
= control_sock
->minor
;
929 ret
= relayd_get_configuration(control_sock
, 0, &result_flags
);
931 ERR("Unable to get relayd configuration");
932 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
936 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
937 consumer
->relay_allows_clear
= true;
940 status
= send_consumer_relayd_socket(session
.id
, &consumer
->dst
.net
.control
,
941 consumer
, sock
, session
.name
, session
.hostname
, base_path
,
942 session
.live_timer
, current_chunk_id
, session
.creation_time
,
943 session
.name_contains_creation_time
, *control_sock
);
945 if (status
!= LTTNG_OK
) {
950 /* Sending data relayd socket. */
951 if (!sock
->data_sock_sent
) {
952 /* Connect to relayd and make version check if uri is the control. */
953 status
= create_connect_relayd(&consumer
->dst
.net
.data
, &data_sock
);
954 if (status
!= LTTNG_OK
) {
957 LTTNG_ASSERT(data_sock
);
959 status
= send_consumer_relayd_socket(session
.id
, &consumer
->dst
.net
.data
, consumer
,
960 sock
, session
.name
, session
.hostname
, base_path
, session
.live_timer
,
961 current_chunk_id
, session
.creation_time
,
962 session
.name_contains_creation_time
, *data_sock
);
964 if (status
!= LTTNG_OK
) {
970 if (control_sock
!= nullptr) {
971 relayd_close(control_sock
);
975 if (data_sock
!= nullptr) {
976 relayd_close(data_sock
);
984 * Setup relayd connections for a tracing session. First creates the socket to
985 * the relayd and send them to the right domain consumer. Consumer type MUST be
988 int cmd_setup_relayd(struct ltt_session
*session
)
991 struct ltt_ust_session
*usess
;
992 struct ltt_kernel_session
*ksess
;
993 struct consumer_socket
*socket
;
994 struct lttng_ht_iter iter
;
995 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
997 LTTNG_ASSERT(session
);
999 usess
= session
->ust_session
;
1000 ksess
= session
->kernel_session
;
1002 ERR("Setting relayd for session %s", session
->name
);
1005 if (session
->current_trace_chunk
) {
1006 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1007 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1009 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1010 current_chunk_id
.is_set
= true;
1012 ERR("Failed to get current trace chunk id");
1013 ret
= LTTNG_ERR_UNK
;
1018 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1019 && usess
->consumer
->enabled
) {
1020 /* For each consumer socket, send relayd sockets */
1021 cds_lfht_for_each_entry (
1022 usess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1023 pthread_mutex_lock(socket
->lock
);
1024 ret
= send_consumer_relayd_sockets(*session
, usess
->consumer
, socket
,
1026 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
1027 pthread_mutex_unlock(socket
->lock
);
1028 if (ret
!= LTTNG_OK
) {
1031 /* Session is now ready for network streaming. */
1032 session
->net_handle
= 1;
1034 session
->consumer
->relay_major_version
=
1035 usess
->consumer
->relay_major_version
;
1036 session
->consumer
->relay_minor_version
=
1037 usess
->consumer
->relay_minor_version
;
1038 session
->consumer
->relay_allows_clear
=
1039 usess
->consumer
->relay_allows_clear
;
1042 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1043 && ksess
->consumer
->enabled
) {
1044 cds_lfht_for_each_entry (
1045 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1046 pthread_mutex_lock(socket
->lock
);
1047 ret
= send_consumer_relayd_sockets(*session
, ksess
->consumer
, socket
,
1049 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
1050 pthread_mutex_unlock(socket
->lock
);
1051 if (ret
!= LTTNG_OK
) {
1054 /* Session is now ready for network streaming. */
1055 session
->net_handle
= 1;
1057 session
->consumer
->relay_major_version
=
1058 ksess
->consumer
->relay_major_version
;
1059 session
->consumer
->relay_minor_version
=
1060 ksess
->consumer
->relay_minor_version
;
1061 session
->consumer
->relay_allows_clear
=
1062 ksess
->consumer
->relay_allows_clear
;
1071 * Start a kernel session by opening all necessary streams.
1073 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1076 struct ltt_kernel_channel
*kchan
;
1078 /* Open kernel metadata */
1079 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1080 ret
= kernel_open_metadata(ksess
);
1082 ret
= LTTNG_ERR_KERN_META_FAIL
;
1087 /* Open kernel metadata stream */
1088 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1089 ret
= kernel_open_metadata_stream(ksess
);
1091 ERR("Kernel create metadata stream failed");
1092 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1097 /* For each channel */
1098 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1099 if (kchan
->stream_count
== 0) {
1100 ret
= kernel_open_channel_stream(kchan
);
1102 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1105 /* Update the stream global counter */
1106 ksess
->stream_count_global
+= ret
;
1110 /* Setup kernel consumer socket and send fds to it */
1111 ret
= init_kernel_tracing(ksess
);
1113 ret
= LTTNG_ERR_KERN_START_FAIL
;
1117 /* This start the kernel tracing */
1118 ret
= kernel_start_session(ksess
);
1120 ret
= LTTNG_ERR_KERN_START_FAIL
;
1124 /* Quiescent wait after starting trace */
1125 kernel_wait_quiescent();
1135 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1137 struct ltt_kernel_channel
*kchan
;
1138 bool error_occurred
= false;
1141 if (!ksess
|| !ksess
->active
) {
1144 DBG("Stopping kernel tracing");
1146 ret
= kernel_stop_session(ksess
);
1148 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1152 kernel_wait_quiescent();
1154 /* Flush metadata after stopping (if exists) */
1155 if (ksess
->metadata_stream_fd
>= 0) {
1156 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1158 ERR("Kernel metadata flush failed");
1159 error_occurred
= true;
1163 /* Flush all buffers after stopping */
1164 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1165 ret
= kernel_flush_buffer(kchan
);
1167 ERR("Kernel flush buffer error");
1168 error_occurred
= true;
1173 if (error_occurred
) {
1174 ret
= LTTNG_ERR_UNK
;
1183 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1185 int cmd_disable_channel(struct ltt_session
*session
,
1186 enum lttng_domain_type domain
, char *channel_name
)
1189 struct ltt_ust_session
*usess
;
1191 usess
= session
->ust_session
;
1196 case LTTNG_DOMAIN_KERNEL
:
1198 ret
= channel_kernel_disable(session
->kernel_session
,
1200 if (ret
!= LTTNG_OK
) {
1204 kernel_wait_quiescent();
1207 case LTTNG_DOMAIN_UST
:
1209 struct ltt_ust_channel
*uchan
;
1210 struct lttng_ht
*chan_ht
;
1212 chan_ht
= usess
->domain_global
.channels
;
1214 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1215 if (uchan
== NULL
) {
1216 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1220 ret
= channel_ust_disable(usess
, uchan
);
1221 if (ret
!= LTTNG_OK
) {
1227 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1239 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1241 * The wpipe arguments is used as a notifier for the kernel thread.
1243 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1247 ssize_t sock_recv_len
;
1248 struct lttng_channel
*channel
= NULL
;
1249 struct lttng_buffer_view view
;
1250 struct lttng_dynamic_buffer channel_buffer
;
1251 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1253 lttng_dynamic_buffer_init(&channel_buffer
);
1254 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1255 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1257 ret
= LTTNG_ERR_NOMEM
;
1261 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1263 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1264 ERR("Failed to receive \"enable channel\" command payload");
1265 ret
= LTTNG_ERR_INVALID
;
1269 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1270 if (!lttng_buffer_view_is_valid(&view
)) {
1271 ret
= LTTNG_ERR_INVALID
;
1275 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1276 ERR("Invalid channel payload received in \"enable channel\" command");
1277 ret
= LTTNG_ERR_INVALID
;
1281 ret
= cmd_enable_channel_internal(
1282 cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1285 lttng_dynamic_buffer_reset(&channel_buffer
);
1286 lttng_channel_destroy(channel
);
1290 static enum lttng_error_code
kernel_domain_check_trace_format_requirements(
1291 const lttng::trace_format_descriptor
& descriptor
)
1293 enum lttng_error_code ret_code
= LTTNG_OK
;
1294 switch (descriptor
.type()) {
1295 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1
:
1296 /* Supported by all kernel tracer. */
1298 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_2
:
1299 if (!kernel_supports_ctf2()) {
1300 ret_code
= LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_KERNEL_TRACER
;
1309 static enum lttng_error_code
cmd_enable_channel_internal(
1310 struct ltt_session
*session
,
1311 const struct lttng_domain
*domain
,
1312 const struct lttng_channel
*_attr
,
1315 enum lttng_error_code ret_code
;
1316 struct ltt_ust_session
*usess
= session
->ust_session
;
1317 struct lttng_ht
*chan_ht
;
1319 struct lttng_channel
*attr
= NULL
;
1321 LTTNG_ASSERT(session
);
1322 LTTNG_ASSERT(_attr
);
1323 LTTNG_ASSERT(domain
);
1325 attr
= lttng_channel_copy(_attr
);
1327 ret_code
= LTTNG_ERR_NOMEM
;
1331 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1333 /* Validate channel name */
1334 if (attr
->name
[0] == '.' ||
1335 memchr(attr
->name
, '/', len
) != NULL
) {
1336 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1340 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1345 * If the session is a live session, remove the switch timer, the
1346 * live timer does the same thing but sends also synchronisation
1347 * beacons for inactive streams.
1349 if (session
->live_timer
> 0) {
1350 attr
->attr
.live_timer_interval
= session
->live_timer
;
1351 attr
->attr
.switch_timer_interval
= 0;
1354 /* Check for feature support */
1355 switch (domain
->type
) {
1356 case LTTNG_DOMAIN_KERNEL
:
1358 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1359 /* Sampling position of buffer is not supported */
1360 WARN("Kernel tracer does not support buffer monitoring. "
1361 "Setting the monitor interval timer to 0 "
1362 "(disabled) for channel '%s' of session '%s'",
1363 attr
->name
, session
->name
);
1364 lttng_channel_set_monitor_timer_interval(attr
, 0);
1367 ret_code
= kernel_domain_check_trace_format_requirements(*session
->trace_format
);
1368 if (ret_code
!= LTTNG_OK
) {
1369 WARN("Kernel tracer does not support the configured trace format of session '%s'",
1375 case LTTNG_DOMAIN_UST
:
1377 case LTTNG_DOMAIN_JUL
:
1378 case LTTNG_DOMAIN_LOG4J
:
1379 case LTTNG_DOMAIN_PYTHON
:
1380 if (!agent_tracing_is_enabled()) {
1381 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1382 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1387 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1391 switch (domain
->type
) {
1392 case LTTNG_DOMAIN_KERNEL
:
1394 struct ltt_kernel_channel
*kchan
;
1396 kchan
= trace_kernel_get_channel_by_name(
1397 attr
->name
, session
->kernel_session
);
1398 if (kchan
== NULL
) {
1400 * Don't try to create a channel if the session has been started at
1401 * some point in time before. The tracer does not allow it.
1403 if (session
->has_been_started
) {
1404 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1408 if (session
->snapshot
.nb_output
> 0 ||
1409 session
->snapshot_mode
) {
1410 /* Enforce mmap output for snapshot sessions. */
1411 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1413 ret_code
= channel_kernel_create(
1414 session
->kernel_session
, attr
, wpipe
);
1415 if (attr
->name
[0] != '\0') {
1416 session
->kernel_session
->has_non_default_channel
= 1;
1419 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1422 if (ret_code
!= LTTNG_OK
) {
1426 kernel_wait_quiescent();
1429 case LTTNG_DOMAIN_UST
:
1430 case LTTNG_DOMAIN_JUL
:
1431 case LTTNG_DOMAIN_LOG4J
:
1432 case LTTNG_DOMAIN_PYTHON
:
1434 struct ltt_ust_channel
*uchan
;
1439 * Current agent implementation limitations force us to allow
1440 * only one channel at once in "agent" subdomains. Each
1441 * subdomain has a default channel name which must be strictly
1444 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1445 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1446 LTTNG_SYMBOL_NAME_LEN
)) {
1447 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1450 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1451 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1452 LTTNG_SYMBOL_NAME_LEN
)) {
1453 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1456 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1457 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1458 LTTNG_SYMBOL_NAME_LEN
)) {
1459 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1464 chan_ht
= usess
->domain_global
.channels
;
1466 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1467 if (uchan
== NULL
) {
1469 * Don't try to create a channel if the session has been started at
1470 * some point in time before. The tracer does not allow it.
1472 if (session
->has_been_started
) {
1473 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1477 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1478 if (attr
->name
[0] != '\0') {
1479 usess
->has_non_default_channel
= 1;
1482 ret_code
= channel_ust_enable(usess
, uchan
);
1487 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1491 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1492 session
->has_non_mmap_channel
= true;
1497 lttng_channel_destroy(attr
);
1501 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1502 struct ltt_session
*session
,
1503 enum lttng_domain_type domain
,
1504 enum lttng_process_attr process_attr
,
1505 enum lttng_tracking_policy
*policy
)
1507 enum lttng_error_code ret_code
= LTTNG_OK
;
1508 const struct process_attr_tracker
*tracker
;
1511 case LTTNG_DOMAIN_KERNEL
:
1512 if (!session
->kernel_session
) {
1513 ret_code
= LTTNG_ERR_INVALID
;
1516 tracker
= kernel_get_process_attr_tracker(
1517 session
->kernel_session
, process_attr
);
1519 case LTTNG_DOMAIN_UST
:
1520 if (!session
->ust_session
) {
1521 ret_code
= LTTNG_ERR_INVALID
;
1524 tracker
= trace_ust_get_process_attr_tracker(
1525 session
->ust_session
, process_attr
);
1528 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1532 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1534 ret_code
= LTTNG_ERR_INVALID
;
1540 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1541 struct ltt_session
*session
,
1542 enum lttng_domain_type domain
,
1543 enum lttng_process_attr process_attr
,
1544 enum lttng_tracking_policy policy
)
1546 enum lttng_error_code ret_code
= LTTNG_OK
;
1549 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1550 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1551 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1554 ret_code
= LTTNG_ERR_INVALID
;
1559 case LTTNG_DOMAIN_KERNEL
:
1560 if (!session
->kernel_session
) {
1561 ret_code
= LTTNG_ERR_INVALID
;
1564 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1565 session
->kernel_session
, process_attr
, policy
);
1567 case LTTNG_DOMAIN_UST
:
1568 if (!session
->ust_session
) {
1569 ret_code
= LTTNG_ERR_INVALID
;
1572 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1573 session
->ust_session
, process_attr
, policy
);
1576 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1583 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1584 struct ltt_session
*session
,
1585 enum lttng_domain_type domain
,
1586 enum lttng_process_attr process_attr
,
1587 const struct process_attr_value
*value
)
1589 enum lttng_error_code ret_code
= LTTNG_OK
;
1592 case LTTNG_DOMAIN_KERNEL
:
1593 if (!session
->kernel_session
) {
1594 ret_code
= LTTNG_ERR_INVALID
;
1597 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1598 session
->kernel_session
, process_attr
, value
);
1600 case LTTNG_DOMAIN_UST
:
1601 if (!session
->ust_session
) {
1602 ret_code
= LTTNG_ERR_INVALID
;
1605 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1606 session
->ust_session
, process_attr
, value
);
1609 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1616 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1617 struct ltt_session
*session
,
1618 enum lttng_domain_type domain
,
1619 enum lttng_process_attr process_attr
,
1620 const struct process_attr_value
*value
)
1622 enum lttng_error_code ret_code
= LTTNG_OK
;
1625 case LTTNG_DOMAIN_KERNEL
:
1626 if (!session
->kernel_session
) {
1627 ret_code
= LTTNG_ERR_INVALID
;
1630 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1631 session
->kernel_session
, process_attr
, value
);
1633 case LTTNG_DOMAIN_UST
:
1634 if (!session
->ust_session
) {
1635 ret_code
= LTTNG_ERR_INVALID
;
1638 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1639 session
->ust_session
, process_attr
, value
);
1642 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1649 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1650 struct ltt_session
*session
,
1651 enum lttng_domain_type domain
,
1652 enum lttng_process_attr process_attr
,
1653 struct lttng_process_attr_values
**values
)
1655 enum lttng_error_code ret_code
= LTTNG_OK
;
1656 const struct process_attr_tracker
*tracker
;
1657 enum process_attr_tracker_status status
;
1660 case LTTNG_DOMAIN_KERNEL
:
1661 if (!session
->kernel_session
) {
1662 ret_code
= LTTNG_ERR_INVALID
;
1665 tracker
= kernel_get_process_attr_tracker(
1666 session
->kernel_session
, process_attr
);
1668 case LTTNG_DOMAIN_UST
:
1669 if (!session
->ust_session
) {
1670 ret_code
= LTTNG_ERR_INVALID
;
1673 tracker
= trace_ust_get_process_attr_tracker(
1674 session
->ust_session
, process_attr
);
1677 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1682 ret_code
= LTTNG_ERR_INVALID
;
1686 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1688 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1689 ret_code
= LTTNG_OK
;
1691 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1692 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1694 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1695 ret_code
= LTTNG_ERR_NOMEM
;
1698 ret_code
= LTTNG_ERR_UNK
;
1707 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1709 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1710 struct lttng_event
*event
,
1711 char *filter_expression
,
1712 struct lttng_bytecode
*bytecode
,
1713 struct lttng_event_exclusion
*exclusion
)
1716 const char *event_name
;
1717 const struct ltt_session
*session
= cmd_ctx
->session
;
1718 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1719 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1721 DBG("Disable event command for event \'%s\'", event
->name
);
1724 * Filter and exclusions are simply not handled by the
1725 * disable event command at this time.
1729 (void) filter_expression
;
1732 /* Ignore the presence of filter or exclusion for the event */
1734 event
->exclusion
= 0;
1736 event_name
= event
->name
;
1738 /* Error out on unhandled search criteria */
1739 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1740 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1741 ret
= LTTNG_ERR_UNK
;
1748 case LTTNG_DOMAIN_KERNEL
:
1750 struct ltt_kernel_channel
*kchan
;
1751 struct ltt_kernel_session
*ksess
;
1753 ksess
= session
->kernel_session
;
1756 * If a non-default channel has been created in the
1757 * session, explicitely require that -c chan_name needs
1760 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1761 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1765 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1766 if (kchan
== NULL
) {
1767 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1771 switch (event
->type
) {
1772 case LTTNG_EVENT_ALL
:
1773 case LTTNG_EVENT_TRACEPOINT
:
1774 case LTTNG_EVENT_SYSCALL
:
1775 case LTTNG_EVENT_PROBE
:
1776 case LTTNG_EVENT_FUNCTION
:
1777 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1778 if (event_name
[0] == '\0') {
1779 ret
= event_kernel_disable_event(kchan
,
1782 ret
= event_kernel_disable_event(kchan
,
1783 event_name
, event
->type
);
1785 if (ret
!= LTTNG_OK
) {
1790 ret
= LTTNG_ERR_UNK
;
1794 kernel_wait_quiescent();
1797 case LTTNG_DOMAIN_UST
:
1799 struct ltt_ust_channel
*uchan
;
1800 struct ltt_ust_session
*usess
;
1802 usess
= session
->ust_session
;
1804 if (validate_ust_event_name(event_name
)) {
1805 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1810 * If a non-default channel has been created in the
1811 * session, explicitly require that -c chan_name needs
1814 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1815 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1819 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1821 if (uchan
== NULL
) {
1822 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1826 switch (event
->type
) {
1827 case LTTNG_EVENT_ALL
:
1829 * An empty event name means that everything
1830 * should be disabled.
1832 if (event
->name
[0] == '\0') {
1833 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1835 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1838 if (ret
!= LTTNG_OK
) {
1843 ret
= LTTNG_ERR_UNK
;
1847 DBG3("Disable UST event %s in channel %s completed", event_name
,
1851 case LTTNG_DOMAIN_LOG4J
:
1852 case LTTNG_DOMAIN_JUL
:
1853 case LTTNG_DOMAIN_PYTHON
:
1856 struct ltt_ust_session
*usess
= session
->ust_session
;
1858 LTTNG_ASSERT(usess
);
1860 switch (event
->type
) {
1861 case LTTNG_EVENT_ALL
:
1864 ret
= LTTNG_ERR_UNK
;
1868 agt
= trace_ust_find_agent(usess
, domain
);
1870 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1874 * An empty event name means that everything
1875 * should be disabled.
1877 if (event
->name
[0] == '\0') {
1878 ret
= event_agent_disable_all(usess
, agt
);
1880 ret
= event_agent_disable(usess
, agt
, event_name
);
1882 if (ret
!= LTTNG_OK
) {
1889 ret
= LTTNG_ERR_UND
;
1900 free(filter_expression
);
1905 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1907 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1908 const struct lttng_event_context
*event_context
, int kwpipe
)
1910 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1911 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1912 const struct ltt_session
*session
= cmd_ctx
->session
;
1913 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1916 * Don't try to add a context if the session has been started at
1917 * some point in time before. The tracer does not allow it and would
1918 * result in a corrupted trace.
1920 if (cmd_ctx
->session
->has_been_started
) {
1921 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1926 case LTTNG_DOMAIN_KERNEL
:
1927 LTTNG_ASSERT(session
->kernel_session
);
1929 if (session
->kernel_session
->channel_count
== 0) {
1930 /* Create default channel */
1931 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1932 if (ret
!= LTTNG_OK
) {
1935 chan_kern_created
= 1;
1937 /* Add kernel context to kernel tracer */
1938 ret
= context_kernel_add(session
->kernel_session
,
1939 event_context
, channel_name
);
1940 if (ret
!= LTTNG_OK
) {
1944 case LTTNG_DOMAIN_JUL
:
1945 case LTTNG_DOMAIN_LOG4J
:
1948 * Validate channel name.
1949 * If no channel name is given and the domain is JUL or LOG4J,
1950 * set it to the appropriate domain-specific channel name. If
1951 * a name is provided but does not match the expexted channel
1952 * name, return an error.
1954 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1955 strcmp(channel_name
,
1956 DEFAULT_JUL_CHANNEL_NAME
)) {
1957 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1959 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1960 strcmp(channel_name
,
1961 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1962 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1967 case LTTNG_DOMAIN_UST
:
1969 struct ltt_ust_session
*usess
= session
->ust_session
;
1970 unsigned int chan_count
;
1972 LTTNG_ASSERT(usess
);
1974 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1975 if (chan_count
== 0) {
1976 struct lttng_channel
*attr
;
1977 /* Create default channel */
1978 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1980 ret
= LTTNG_ERR_FATAL
;
1984 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1985 if (ret
!= LTTNG_OK
) {
1989 channel_attr_destroy(attr
);
1990 chan_ust_created
= 1;
1993 ret
= context_ust_add(usess
, domain
, event_context
,
1995 if (ret
!= LTTNG_OK
) {
2001 ret
= LTTNG_ERR_UND
;
2009 if (chan_kern_created
) {
2010 struct ltt_kernel_channel
*kchan
=
2011 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2012 session
->kernel_session
);
2013 /* Created previously, this should NOT fail. */
2014 LTTNG_ASSERT(kchan
);
2015 kernel_destroy_channel(kchan
);
2018 if (chan_ust_created
) {
2019 struct ltt_ust_channel
*uchan
=
2020 trace_ust_find_channel_by_name(
2021 session
->ust_session
->domain_global
.channels
,
2022 DEFAULT_CHANNEL_NAME
);
2023 /* Created previously, this should NOT fail. */
2024 LTTNG_ASSERT(uchan
);
2025 /* Remove from the channel list of the session. */
2026 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2028 trace_ust_destroy_channel(uchan
);
2034 static inline bool name_starts_with(const char *name
, const char *prefix
)
2036 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2038 return !strncmp(name
, prefix
, max_cmp_len
);
2041 /* Perform userspace-specific event name validation */
2042 static int validate_ust_event_name(const char *name
)
2052 * Check name against all internal UST event component namespaces used
2055 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2056 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2057 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2066 * Internal version of cmd_enable_event() with a supplemental
2067 * "internal_event" flag which is used to enable internal events which should
2068 * be hidden from clients. Such events are used in the agent implementation to
2069 * enable the events through which all "agent" events are funeled.
2071 static int _cmd_enable_event(struct ltt_session
*session
,
2072 const struct lttng_domain
*domain
,
2073 char *channel_name
, struct lttng_event
*event
,
2074 char *filter_expression
,
2075 struct lttng_bytecode
*filter
,
2076 struct lttng_event_exclusion
*exclusion
,
2077 int wpipe
, bool internal_event
)
2079 int ret
= 0, channel_created
= 0;
2080 struct lttng_channel
*attr
= NULL
;
2082 LTTNG_ASSERT(session
);
2083 LTTNG_ASSERT(event
);
2084 LTTNG_ASSERT(channel_name
);
2086 /* If we have a filter, we must have its filter expression */
2087 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2089 /* Normalize event name as a globbing pattern */
2090 strutils_normalize_star_glob_pattern(event
->name
);
2092 /* Normalize exclusion names as globbing patterns */
2096 for (i
= 0; i
< exclusion
->count
; i
++) {
2097 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2099 strutils_normalize_star_glob_pattern(name
);
2103 DBG("Enable event command for event \'%s\'", event
->name
);
2107 switch (domain
->type
) {
2108 case LTTNG_DOMAIN_KERNEL
:
2110 struct ltt_kernel_channel
*kchan
;
2113 * If a non-default channel has been created in the
2114 * session, explicitely require that -c chan_name needs
2117 if (session
->kernel_session
->has_non_default_channel
2118 && channel_name
[0] == '\0') {
2119 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2123 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2124 session
->kernel_session
);
2125 if (kchan
== NULL
) {
2126 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2127 LTTNG_BUFFER_GLOBAL
);
2129 ret
= LTTNG_ERR_FATAL
;
2132 if (lttng_strncpy(attr
->name
, channel_name
,
2133 sizeof(attr
->name
))) {
2134 ret
= LTTNG_ERR_INVALID
;
2138 ret
= cmd_enable_channel_internal(
2139 session
, domain
, attr
, wpipe
);
2140 if (ret
!= LTTNG_OK
) {
2143 channel_created
= 1;
2146 /* Get the newly created kernel channel pointer */
2147 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2148 session
->kernel_session
);
2149 if (kchan
== NULL
) {
2150 /* This sould not happen... */
2151 ret
= LTTNG_ERR_FATAL
;
2155 switch (event
->type
) {
2156 case LTTNG_EVENT_ALL
:
2158 char *filter_expression_a
= NULL
;
2159 struct lttng_bytecode
*filter_a
= NULL
;
2162 * We need to duplicate filter_expression and filter,
2163 * because ownership is passed to first enable
2166 if (filter_expression
) {
2167 filter_expression_a
= strdup(filter_expression
);
2168 if (!filter_expression_a
) {
2169 ret
= LTTNG_ERR_FATAL
;
2174 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2176 free(filter_expression_a
);
2177 ret
= LTTNG_ERR_FATAL
;
2180 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2182 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2183 ret
= event_kernel_enable_event(kchan
, event
,
2184 filter_expression
, filter
);
2185 /* We have passed ownership */
2186 filter_expression
= NULL
;
2188 if (ret
!= LTTNG_OK
) {
2189 if (channel_created
) {
2190 /* Let's not leak a useless channel. */
2191 kernel_destroy_channel(kchan
);
2193 free(filter_expression_a
);
2197 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2198 ret
= event_kernel_enable_event(kchan
, event
,
2199 filter_expression_a
, filter_a
);
2200 /* We have passed ownership */
2201 filter_expression_a
= NULL
;
2203 if (ret
!= LTTNG_OK
) {
2208 case LTTNG_EVENT_PROBE
:
2209 case LTTNG_EVENT_USERSPACE_PROBE
:
2210 case LTTNG_EVENT_FUNCTION
:
2211 case LTTNG_EVENT_FUNCTION_ENTRY
:
2212 case LTTNG_EVENT_TRACEPOINT
:
2213 ret
= event_kernel_enable_event(kchan
, event
,
2214 filter_expression
, filter
);
2215 /* We have passed ownership */
2216 filter_expression
= NULL
;
2218 if (ret
!= LTTNG_OK
) {
2219 if (channel_created
) {
2220 /* Let's not leak a useless channel. */
2221 kernel_destroy_channel(kchan
);
2226 case LTTNG_EVENT_SYSCALL
:
2227 ret
= event_kernel_enable_event(kchan
, event
,
2228 filter_expression
, filter
);
2229 /* We have passed ownership */
2230 filter_expression
= NULL
;
2232 if (ret
!= LTTNG_OK
) {
2237 ret
= LTTNG_ERR_UNK
;
2241 kernel_wait_quiescent();
2244 case LTTNG_DOMAIN_UST
:
2246 struct ltt_ust_channel
*uchan
;
2247 struct ltt_ust_session
*usess
= session
->ust_session
;
2249 LTTNG_ASSERT(usess
);
2252 * If a non-default channel has been created in the
2253 * session, explicitely require that -c chan_name needs
2256 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2257 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2261 /* Get channel from global UST domain */
2262 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2264 if (uchan
== NULL
) {
2265 /* Create default channel */
2266 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2267 usess
->buffer_type
);
2269 ret
= LTTNG_ERR_FATAL
;
2272 if (lttng_strncpy(attr
->name
, channel_name
,
2273 sizeof(attr
->name
))) {
2274 ret
= LTTNG_ERR_INVALID
;
2278 ret
= cmd_enable_channel_internal(
2279 session
, domain
, attr
, wpipe
);
2280 if (ret
!= LTTNG_OK
) {
2284 /* Get the newly created channel reference back */
2285 uchan
= trace_ust_find_channel_by_name(
2286 usess
->domain_global
.channels
, channel_name
);
2287 LTTNG_ASSERT(uchan
);
2290 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2292 * Don't allow users to add UST events to channels which
2293 * are assigned to a userspace subdomain (JUL, Log4J,
2296 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2300 if (!internal_event
) {
2302 * Ensure the event name is not reserved for internal
2305 ret
= validate_ust_event_name(event
->name
);
2307 WARN("Userspace event name %s failed validation.",
2309 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2314 /* At this point, the session and channel exist on the tracer */
2315 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2316 filter_expression
, filter
, exclusion
,
2318 /* We have passed ownership */
2319 filter_expression
= NULL
;
2322 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2323 goto already_enabled
;
2324 } else if (ret
!= LTTNG_OK
) {
2329 case LTTNG_DOMAIN_LOG4J
:
2330 case LTTNG_DOMAIN_JUL
:
2331 case LTTNG_DOMAIN_PYTHON
:
2333 const char *default_event_name
, *default_chan_name
;
2335 struct lttng_event uevent
;
2336 struct lttng_domain tmp_dom
;
2337 struct ltt_ust_session
*usess
= session
->ust_session
;
2339 LTTNG_ASSERT(usess
);
2341 if (!agent_tracing_is_enabled()) {
2342 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2343 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2347 agt
= trace_ust_find_agent(usess
, domain
->type
);
2349 agt
= agent_create(domain
->type
);
2351 ret
= LTTNG_ERR_NOMEM
;
2354 agent_add(agt
, usess
->agents
);
2357 /* Create the default tracepoint. */
2358 memset(&uevent
, 0, sizeof(uevent
));
2359 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2360 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2361 default_event_name
= event_get_default_agent_ust_name(
2363 if (!default_event_name
) {
2364 ret
= LTTNG_ERR_FATAL
;
2367 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2368 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2371 * The domain type is changed because we are about to enable the
2372 * default channel and event for the JUL domain that are hardcoded.
2373 * This happens in the UST domain.
2375 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2376 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2378 switch (domain
->type
) {
2379 case LTTNG_DOMAIN_LOG4J
:
2380 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2382 case LTTNG_DOMAIN_JUL
:
2383 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2385 case LTTNG_DOMAIN_PYTHON
:
2386 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2389 /* The switch/case we are in makes this impossible */
2394 char *filter_expression_copy
= NULL
;
2395 struct lttng_bytecode
*filter_copy
= NULL
;
2398 const size_t filter_size
= sizeof(
2399 struct lttng_bytecode
)
2402 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2404 ret
= LTTNG_ERR_NOMEM
;
2407 memcpy(filter_copy
, filter
, filter_size
);
2409 filter_expression_copy
=
2410 strdup(filter_expression
);
2411 if (!filter_expression
) {
2412 ret
= LTTNG_ERR_NOMEM
;
2415 if (!filter_expression_copy
|| !filter_copy
) {
2416 free(filter_expression_copy
);
2422 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2423 (char *) default_chan_name
,
2424 &uevent
, filter_expression_copy
,
2425 filter_copy
, NULL
, wpipe
);
2428 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2429 goto already_enabled
;
2430 } else if (ret
!= LTTNG_OK
) {
2434 /* The wild card * means that everything should be enabled. */
2435 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2436 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2439 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2443 filter_expression
= NULL
;
2444 if (ret
!= LTTNG_OK
) {
2451 ret
= LTTNG_ERR_UND
;
2459 free(filter_expression
);
2462 channel_attr_destroy(attr
);
2468 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2469 * We own filter, exclusion, and filter_expression.
2471 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2472 struct lttng_event
*event
,
2473 char *filter_expression
,
2474 struct lttng_event_exclusion
*exclusion
,
2475 struct lttng_bytecode
*bytecode
,
2480 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2482 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2485 * The ownership of the following parameters is transferred to
2486 * _cmd_enable_event:
2488 * - filter_expression,
2492 ret
= _cmd_enable_event(cmd_ctx
->session
,
2494 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2495 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2496 filter_expression
= NULL
;
2503 * Enable an event which is internal to LTTng. An internal should
2504 * never be made visible to clients and are immune to checks such as
2507 static int cmd_enable_event_internal(struct ltt_session
*session
,
2508 const struct lttng_domain
*domain
,
2509 char *channel_name
, struct lttng_event
*event
,
2510 char *filter_expression
,
2511 struct lttng_bytecode
*filter
,
2512 struct lttng_event_exclusion
*exclusion
,
2515 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2516 filter_expression
, filter
, exclusion
, wpipe
, true);
2520 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2522 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2523 struct lttng_payload
*reply_payload
)
2525 enum lttng_error_code ret_code
;
2527 ssize_t i
, nb_events
= 0;
2528 struct lttng_event
*events
= NULL
;
2529 struct lttcomm_list_command_header reply_command_header
= {};
2530 size_t reply_command_header_offset
;
2532 assert(reply_payload
);
2534 /* Reserve space for command reply header. */
2535 reply_command_header_offset
= reply_payload
->buffer
.size
;
2536 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2537 reply_command_header_offset
+
2538 sizeof(struct lttcomm_list_command_header
));
2540 ret_code
= LTTNG_ERR_NOMEM
;
2545 case LTTNG_DOMAIN_KERNEL
:
2546 nb_events
= kernel_list_events(&events
);
2547 if (nb_events
< 0) {
2548 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2552 case LTTNG_DOMAIN_UST
:
2553 nb_events
= ust_app_list_events(&events
);
2554 if (nb_events
< 0) {
2555 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2559 case LTTNG_DOMAIN_LOG4J
:
2560 case LTTNG_DOMAIN_JUL
:
2561 case LTTNG_DOMAIN_PYTHON
:
2562 nb_events
= agent_list_events(&events
, domain
);
2563 if (nb_events
< 0) {
2564 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2569 ret_code
= LTTNG_ERR_UND
;
2573 for (i
= 0; i
< nb_events
; i
++) {
2574 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2577 ret_code
= LTTNG_ERR_NOMEM
;
2582 if (nb_events
> UINT32_MAX
) {
2583 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2584 ret_code
= LTTNG_ERR_OVERFLOW
;
2588 /* Update command reply header. */
2589 reply_command_header
.count
= (uint32_t) nb_events
;
2590 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2591 sizeof(reply_command_header
));
2593 ret_code
= LTTNG_OK
;
2600 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2602 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2603 struct lttng_payload
*reply
)
2605 enum lttng_error_code ret_code
;
2607 unsigned int i
, nb_fields
;
2608 struct lttng_event_field
*fields
= NULL
;
2609 struct lttcomm_list_command_header reply_command_header
= {};
2610 size_t reply_command_header_offset
;
2614 /* Reserve space for command reply header. */
2615 reply_command_header_offset
= reply
->buffer
.size
;
2616 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2617 reply_command_header_offset
+
2618 sizeof(struct lttcomm_list_command_header
));
2620 ret_code
= LTTNG_ERR_NOMEM
;
2625 case LTTNG_DOMAIN_UST
:
2626 ret
= ust_app_list_event_fields(&fields
);
2628 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2633 case LTTNG_DOMAIN_KERNEL
:
2634 default: /* fall-through */
2635 ret_code
= LTTNG_ERR_UND
;
2641 for (i
= 0; i
< nb_fields
; i
++) {
2642 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2644 ret_code
= LTTNG_ERR_NOMEM
;
2649 if (nb_fields
> UINT32_MAX
) {
2650 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2651 ret_code
= LTTNG_ERR_OVERFLOW
;
2655 /* Update command reply header. */
2656 reply_command_header
.count
= (uint32_t) nb_fields
;
2658 memcpy(reply
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2659 sizeof(reply_command_header
));
2661 ret_code
= LTTNG_OK
;
2668 enum lttng_error_code
cmd_list_syscalls(
2669 struct lttng_payload
*reply_payload
)
2671 enum lttng_error_code ret_code
;
2672 ssize_t nb_events
, i
;
2674 struct lttng_event
*events
= NULL
;
2675 struct lttcomm_list_command_header reply_command_header
= {};
2676 size_t reply_command_header_offset
;
2678 assert(reply_payload
);
2680 /* Reserve space for command reply header. */
2681 reply_command_header_offset
= reply_payload
->buffer
.size
;
2682 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2683 reply_command_header_offset
+
2684 sizeof(struct lttcomm_list_command_header
));
2686 ret_code
= LTTNG_ERR_NOMEM
;
2690 nb_events
= syscall_table_list(&events
);
2691 if (nb_events
< 0) {
2692 ret_code
= (enum lttng_error_code
) -nb_events
;
2696 for (i
= 0; i
< nb_events
; i
++) {
2697 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2700 ret_code
= LTTNG_ERR_NOMEM
;
2705 if (nb_events
> UINT32_MAX
) {
2706 ERR("Syscall count would overflow the syscall listing command's reply");
2707 ret_code
= LTTNG_ERR_OVERFLOW
;
2711 /* Update command reply header. */
2712 reply_command_header
.count
= (uint32_t) nb_events
;
2713 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2714 sizeof(reply_command_header
));
2716 ret_code
= LTTNG_OK
;
2723 * Command LTTNG_START_TRACE processed by the client thread.
2725 * Called with session mutex held.
2727 int cmd_start_trace(struct ltt_session
*session
)
2729 enum lttng_error_code ret
;
2730 unsigned long nb_chan
= 0;
2731 struct ltt_kernel_session
*ksession
;
2732 struct ltt_ust_session
*usess
;
2733 const bool session_rotated_after_last_stop
=
2734 session
->rotated_after_last_stop
;
2735 const bool session_cleared_after_last_stop
=
2736 session
->cleared_after_last_stop
;
2738 LTTNG_ASSERT(session
);
2740 /* Ease our life a bit ;) */
2741 ksession
= session
->kernel_session
;
2742 usess
= session
->ust_session
;
2744 /* Is the session already started? */
2745 if (session
->active
) {
2746 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2747 /* Perform nothing */
2751 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2752 !session
->current_trace_chunk
) {
2754 * A rotation was launched while the session was stopped and
2755 * it has not been completed yet. It is not possible to start
2756 * the session since starting the session here would require a
2757 * rotation from "NULL" to a new trace chunk. That rotation
2758 * would overlap with the ongoing rotation, which is not
2761 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2763 ret
= LTTNG_ERR_ROTATION_PENDING
;
2768 * Starting a session without channel is useless since after that it's not
2769 * possible to enable channel thus inform the client.
2771 if (usess
&& usess
->domain_global
.channels
) {
2772 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2775 nb_chan
+= ksession
->channel_count
;
2778 ret
= LTTNG_ERR_NO_CHANNEL
;
2782 session
->active
= true;
2783 session
->rotated_after_last_stop
= false;
2784 session
->cleared_after_last_stop
= false;
2785 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2786 if (!session
->has_been_started
) {
2787 struct lttng_trace_chunk
*trace_chunk
;
2789 DBG("Creating initial trace chunk of session \"%s\"",
2791 trace_chunk
= session_create_new_trace_chunk(
2792 session
, NULL
, NULL
, NULL
);
2794 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2797 LTTNG_ASSERT(!session
->current_trace_chunk
);
2798 ret
= (lttng_error_code
) session_set_trace_chunk(session
, trace_chunk
,
2800 lttng_trace_chunk_put(trace_chunk
);
2802 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2806 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2809 * Rotate existing streams into the new chunk.
2810 * This is a "quiet" rotation has no client has
2811 * explicitly requested this operation.
2813 * There is also no need to wait for the rotation
2814 * to complete as it will happen immediately. No data
2815 * was produced as the session was stopped, so the
2816 * rotation should happen on reception of the command.
2818 ret
= (lttng_error_code
) cmd_rotate_session(session
, NULL
, true,
2819 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2820 if (ret
!= LTTNG_OK
) {
2826 /* Kernel tracing */
2827 if (ksession
!= NULL
) {
2828 DBG("Start kernel tracing session %s", session
->name
);
2829 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2830 if (ret
!= LTTNG_OK
) {
2835 /* Flag session that trace should start automatically */
2837 int int_ret
= ust_app_start_trace_all(usess
);
2840 ret
= LTTNG_ERR_UST_START_FAIL
;
2846 * Open a packet in every stream of the session to ensure that viewers
2847 * can correctly identify the boundaries of the periods during which
2848 * tracing was active for this session.
2850 ret
= session_open_packets(session
);
2851 if (ret
!= LTTNG_OK
) {
2856 * Clear the flag that indicates that a rotation was done while the
2857 * session was stopped.
2859 session
->rotated_after_last_stop
= false;
2861 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2862 int int_ret
= timer_session_rotation_schedule_timer_start(
2863 session
, session
->rotate_timer_period
);
2866 ERR("Failed to enable rotate timer");
2867 ret
= LTTNG_ERR_UNK
;
2875 if (ret
== LTTNG_OK
) {
2876 /* Flag this after a successful start. */
2877 session
->has_been_started
= true;
2879 session
->active
= false;
2880 /* Restore initial state on error. */
2881 session
->rotated_after_last_stop
=
2882 session_rotated_after_last_stop
;
2883 session
->cleared_after_last_stop
=
2884 session_cleared_after_last_stop
;
2891 * Command LTTNG_STOP_TRACE processed by the client thread.
2893 int cmd_stop_trace(struct ltt_session
*session
)
2896 struct ltt_kernel_session
*ksession
;
2897 struct ltt_ust_session
*usess
;
2899 LTTNG_ASSERT(session
);
2901 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2903 ksession
= session
->kernel_session
;
2904 usess
= session
->ust_session
;
2906 /* Session is not active. Skip everything and inform the client. */
2907 if (!session
->active
) {
2908 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2912 ret
= stop_kernel_session(ksession
);
2913 if (ret
!= LTTNG_OK
) {
2917 if (usess
&& usess
->active
) {
2918 ret
= ust_app_stop_trace_all(usess
);
2920 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2925 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2927 /* Flag inactive after a successful stop. */
2928 session
->active
= 0;
2936 * Set the base_path of the session only if subdir of a control uris is set.
2937 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2939 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2941 struct lttng_uri
*uris
)
2946 for (i
= 0; i
< nb_uri
; i
++) {
2947 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2948 uris
[i
].subdir
[0] == '\0') {
2949 /* Not interested in these URIs */
2953 if (session
->base_path
!= NULL
) {
2954 free(session
->base_path
);
2955 session
->base_path
= NULL
;
2958 /* Set session base_path */
2959 session
->base_path
= strdup(uris
[i
].subdir
);
2960 if (!session
->base_path
) {
2961 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2962 uris
[i
].subdir
, session
->name
);
2963 ret
= LTTNG_ERR_NOMEM
;
2966 DBG2("Setting base path \"%s\" for session \"%s\"",
2967 session
->base_path
, session
->name
);
2975 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2977 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2978 struct lttng_uri
*uris
)
2981 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2982 struct ltt_ust_session
*usess
= session
->ust_session
;
2984 LTTNG_ASSERT(session
);
2986 LTTNG_ASSERT(nb_uri
> 0);
2988 /* Can't set consumer URI if the session is active. */
2989 if (session
->active
) {
2990 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2995 * Set the session base path if any. This is done inside
2996 * cmd_set_consumer_uri to preserve backward compatibility of the
2997 * previous session creation api vs the session descriptor api.
2999 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
3000 if (ret
!= LTTNG_OK
) {
3004 /* Set the "global" consumer URIs */
3005 for (i
= 0; i
< nb_uri
; i
++) {
3006 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
3008 if (ret
!= LTTNG_OK
) {
3013 /* Set UST session URIs */
3014 if (session
->ust_session
) {
3015 for (i
= 0; i
< nb_uri
; i
++) {
3016 ret
= add_uri_to_consumer(session
,
3017 session
->ust_session
->consumer
,
3018 &uris
[i
], LTTNG_DOMAIN_UST
);
3019 if (ret
!= LTTNG_OK
) {
3025 /* Set kernel session URIs */
3026 if (session
->kernel_session
) {
3027 for (i
= 0; i
< nb_uri
; i
++) {
3028 ret
= add_uri_to_consumer(session
,
3029 session
->kernel_session
->consumer
,
3030 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3031 if (ret
!= LTTNG_OK
) {
3038 * Make sure to set the session in output mode after we set URI since a
3039 * session can be created without URL (thus flagged in no output mode).
3041 session
->output_traces
= 1;
3043 ksess
->output_traces
= 1;
3047 usess
->output_traces
= 1;
3058 enum lttng_error_code
set_session_output_from_descriptor(
3059 struct ltt_session
*session
,
3060 const struct lttng_session_descriptor
*descriptor
)
3063 enum lttng_error_code ret_code
= LTTNG_OK
;
3064 enum lttng_session_descriptor_type session_type
=
3065 lttng_session_descriptor_get_type(descriptor
);
3066 enum lttng_session_descriptor_output_type output_type
=
3067 lttng_session_descriptor_get_output_type(descriptor
);
3068 struct lttng_uri uris
[2] = {};
3069 size_t uri_count
= 0;
3071 switch (output_type
) {
3072 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3074 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3075 lttng_session_descriptor_get_local_output_uri(descriptor
,
3079 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3080 lttng_session_descriptor_get_network_output_uris(descriptor
,
3081 &uris
[0], &uris
[1]);
3085 ret_code
= LTTNG_ERR_INVALID
;
3089 switch (session_type
) {
3090 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3092 struct snapshot_output
*new_output
= NULL
;
3094 new_output
= snapshot_output_alloc();
3096 ret_code
= LTTNG_ERR_NOMEM
;
3100 ret
= snapshot_output_init_with_uri(session
,
3101 DEFAULT_SNAPSHOT_MAX_SIZE
,
3102 NULL
, uris
, uri_count
, session
->consumer
,
3103 new_output
, &session
->snapshot
);
3105 ret_code
= (ret
== -ENOMEM
) ?
3106 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3107 snapshot_output_destroy(new_output
);
3110 snapshot_add_output(&session
->snapshot
, new_output
);
3113 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3114 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3116 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3120 ret_code
= LTTNG_ERR_INVALID
;
3128 enum lttng_error_code
cmd_create_session_from_descriptor(
3129 struct lttng_session_descriptor
*descriptor
,
3130 const lttng_sock_cred
*creds
,
3131 const char *home_path
)
3134 enum lttng_error_code ret_code
;
3135 const char *session_name
;
3136 struct ltt_session
*new_session
= NULL
;
3137 enum lttng_session_descriptor_status descriptor_status
;
3138 const lttng_trace_format_descriptor
*trace_format_descriptor
= NULL
;
3139 lttng::trace_format_descriptor::uptr trace_format_descriptor_ptr
;
3141 session_lock_list();
3143 if (*home_path
!= '/') {
3144 ERR("Home path provided by client is not absolute");
3145 ret_code
= LTTNG_ERR_INVALID
;
3150 descriptor_status
= lttng_session_descriptor_get_session_name(
3151 descriptor
, &session_name
);
3152 switch (descriptor_status
) {
3153 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3155 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3156 session_name
= NULL
;
3159 ret_code
= LTTNG_ERR_INVALID
;
3163 descriptor_status
= lttng_session_descriptor_get_trace_format_descriptor(
3164 descriptor
, &trace_format_descriptor
);
3165 if (descriptor_status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
3166 ret_code
= LTTNG_ERR_INVALID
;
3171 trace_format_descriptor_ptr
=
3172 reinterpret_cast<const lttng::trace_format_descriptor
*>(
3173 trace_format_descriptor
)
3175 } catch (std::exception
& e
) {
3176 ERR("%s", e
.what());
3177 ret_code
= LTTNG_ERR_UNK
;
3181 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, trace_format_descriptor_ptr
,
3183 if (ret_code
!= LTTNG_OK
) {
3187 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3188 new_session
->id
, new_session
->name
, new_session
->uid
, new_session
->gid
);
3189 if (ret_code
!= LTTNG_OK
) {
3193 /* Announce the session's destruction to the notification thread when it is destroyed. */
3194 ret
= session_add_destroy_notifier(
3196 [](const struct ltt_session
*session
,
3197 void *user_data
__attribute__((unused
))) {
3198 (void) notification_thread_command_remove_session(
3199 the_notification_thread_handle
, session
->id
);
3203 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3205 ret
= LTTNG_ERR_NOMEM
;
3209 if (!session_name
) {
3210 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3213 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3218 if (!lttng_session_descriptor_is_output_destination_initialized(
3221 * Only include the session's creation time in the output
3222 * destination if the name of the session itself was
3223 * not auto-generated.
3225 ret_code
= lttng_session_descriptor_set_default_output(
3227 session_name
? &new_session
->creation_time
: NULL
,
3229 if (ret_code
!= LTTNG_OK
) {
3233 new_session
->has_user_specified_directory
=
3234 lttng_session_descriptor_has_output_directory(
3238 switch (lttng_session_descriptor_get_type(descriptor
)) {
3239 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3240 new_session
->snapshot_mode
= 1;
3242 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3243 new_session
->live_timer
=
3244 lttng_session_descriptor_live_get_timer_interval(
3251 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3252 if (ret_code
!= LTTNG_OK
) {
3255 new_session
->consumer
->enabled
= 1;
3256 ret_code
= LTTNG_OK
;
3258 /* Release reference provided by the session_create function. */
3259 session_put(new_session
);
3260 if (ret_code
!= LTTNG_OK
&& new_session
) {
3261 /* Release the global reference on error. */
3262 session_destroy(new_session
);
3264 session_unlock_list();
3268 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3269 struct lttng_session_descriptor
**return_descriptor
)
3272 size_t payload_size
;
3273 struct lttng_dynamic_buffer payload
;
3274 struct lttng_buffer_view home_dir_view
;
3275 struct lttng_buffer_view session_descriptor_view
;
3276 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3277 enum lttng_error_code ret_code
;
3279 lttng_dynamic_buffer_init(&payload
);
3280 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3282 ret_code
= LTTNG_ERR_INVALID
;
3285 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3286 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3287 ret_code
= LTTNG_ERR_INVALID
;
3291 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3292 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3293 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3295 ret_code
= LTTNG_ERR_NOMEM
;
3299 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3301 ERR("Reception of session descriptor failed, aborting.");
3302 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3306 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3309 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3310 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3311 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3312 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3313 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3317 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3319 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3320 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3321 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3322 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3323 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3327 ret
= lttng_session_descriptor_create_from_buffer(
3328 &session_descriptor_view
, &session_descriptor
);
3330 ERR("Failed to create session descriptor from payload of \"create session\" command");
3331 ret_code
= LTTNG_ERR_INVALID
;
3336 * Sets the descriptor's auto-generated properties (name, output) if
3339 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3341 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3342 if (ret_code
!= LTTNG_OK
) {
3346 ret_code
= LTTNG_OK
;
3347 *return_descriptor
= session_descriptor
;
3348 session_descriptor
= NULL
;
3350 lttng_dynamic_buffer_reset(&payload
);
3351 lttng_session_descriptor_destroy(session_descriptor
);
3356 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3357 void *_reply_context
)
3361 const struct cmd_destroy_session_reply_context
*reply_context
=
3362 (cmd_destroy_session_reply_context
*) _reply_context
;
3363 struct lttng_dynamic_buffer payload
;
3364 struct lttcomm_session_destroy_command_header cmd_header
;
3365 struct lttng_trace_archive_location
*location
= NULL
;
3366 struct lttcomm_lttng_msg llm
= {
3367 .cmd_type
= LTTNG_DESTROY_SESSION
,
3368 .ret_code
= reply_context
->destruction_status
,
3371 sizeof(struct lttcomm_session_destroy_command_header
),
3375 size_t payload_size_before_location
;
3377 lttng_dynamic_buffer_init(&payload
);
3379 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3381 ERR("Failed to append session destruction message");
3385 cmd_header
.rotation_state
=
3386 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3387 session
->rotation_state
:
3388 LTTNG_ROTATION_STATE_NO_ROTATION
);
3389 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3390 sizeof(cmd_header
));
3392 ERR("Failed to append session destruction command header");
3396 if (!reply_context
->implicit_rotation_on_destroy
) {
3397 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3401 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3402 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3407 location
= session_get_trace_archive_location(session
);
3409 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3414 payload_size_before_location
= payload
.size
;
3415 comm_ret
= lttng_trace_archive_location_serialize(location
,
3417 lttng_trace_archive_location_put(location
);
3419 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3423 /* Update the message to indicate the location's length. */
3424 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3425 payload
.size
- payload_size_before_location
;
3427 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3428 payload
.data
, payload
.size
);
3429 if (comm_ret
!= (ssize_t
) payload
.size
) {
3430 ERR("Failed to send result of the destruction of session \"%s\" to client",
3434 ret
= close(reply_context
->reply_sock_fd
);
3436 PERROR("Failed to close client socket in deferred session destroy reply");
3438 lttng_dynamic_buffer_reset(&payload
);
3439 free(_reply_context
);
3443 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3445 * Called with session lock held.
3447 int cmd_destroy_session(struct ltt_session
*session
,
3448 struct notification_thread_handle
*notification_thread_handle
,
3452 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3453 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3456 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3457 if (!reply_context
) {
3458 ret
= LTTNG_ERR_NOMEM
;
3462 reply_context
->reply_sock_fd
= *sock_fd
;
3466 LTTNG_ASSERT(session
);
3468 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3470 if (session
->active
) {
3471 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3473 ret
= cmd_stop_trace(session
);
3474 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3475 /* Carry on with the destruction of the session. */
3476 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3477 session
->name
, lttng_strerror(-ret
));
3478 destruction_last_error
= (lttng_error_code
) ret
;
3482 if (session
->rotation_schedule_timer_enabled
) {
3483 if (timer_session_rotation_schedule_timer_stop(
3485 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3487 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3491 if (session
->rotate_size
) {
3492 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3493 session
->rotate_size
= 0;
3496 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3498 * Perform a last rotation on destruction if rotations have
3499 * occurred during the session's lifetime.
3501 ret
= cmd_rotate_session(session
, NULL
, false,
3502 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3503 if (ret
!= LTTNG_OK
) {
3504 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3505 session
->name
, lttng_strerror(-ret
));
3506 destruction_last_error
= (lttng_error_code
) -ret
;
3508 if (reply_context
) {
3509 reply_context
->implicit_rotation_on_destroy
= true;
3511 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3513 * The user has not triggered a session rotation. However, to
3514 * ensure all data has been consumed, the session is rotated
3515 * to a 'null' trace chunk before it is destroyed.
3517 * This is a "quiet" rotation meaning that no notification is
3518 * emitted and no renaming of the current trace chunk takes
3521 ret
= cmd_rotate_session(session
, NULL
, true,
3522 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3524 * Rotation operations may not be supported by the kernel
3525 * tracer. Hence, do not consider this implicit rotation as
3526 * a session destruction error. The library has already stopped
3527 * the session and waited for pending data; there is nothing
3528 * left to do but complete the destruction of the session.
3530 if (ret
!= LTTNG_OK
&&
3531 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3532 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3533 session
->name
, lttng_strerror(ret
));
3534 destruction_last_error
= (lttng_error_code
) -ret
;
3538 if (session
->shm_path
[0]) {
3540 * When a session is created with an explicit shm_path,
3541 * the consumer daemon will create its shared memory files
3542 * at that location and will *not* unlink them. This is normal
3543 * as the intention of that feature is to make it possible
3544 * to retrieve the content of those files should a crash occur.
3546 * To ensure the content of those files can be used, the
3547 * sessiond daemon will replicate the content of the metadata
3548 * cache in a metadata file.
3550 * On clean-up, it is expected that the consumer daemon will
3551 * unlink the shared memory files and that the session daemon
3552 * will unlink the metadata file. Then, the session's directory
3553 * in the shm path can be removed.
3555 * Unfortunately, a flaw in the design of the sessiond's and
3556 * consumerd's tear down of channels makes it impossible to
3557 * determine when the sessiond _and_ the consumerd have both
3558 * destroyed their representation of a channel. For one, the
3559 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3560 * callbacks in both daemons.
3562 * However, it is also impossible for the sessiond to know when
3563 * the consumer daemon is done destroying its channel(s) since
3564 * it occurs as a reaction to the closing of the channel's file
3565 * descriptor. There is no resulting communication initiated
3566 * from the consumerd to the sessiond to confirm that the
3567 * operation is completed (and was successful).
3569 * Until this is all fixed, the session daemon checks for the
3570 * removal of the session's shm path which makes it possible
3571 * to safely advertise a session as having been destroyed.
3573 * Prior to this fix, it was not possible to reliably save
3574 * a session making use of the --shm-path option, destroy it,
3575 * and load it again. This is because the creation of the
3576 * session would fail upon seeing the session's shm path
3577 * already in existence.
3579 * Note that none of the error paths in the check for the
3580 * directory's existence return an error. This is normal
3581 * as there isn't much that can be done. The session will
3582 * be destroyed properly, except that we can't offer the
3583 * guarantee that the same session can be re-created.
3585 current_completion_handler
= &destroy_completion_handler
.handler
;
3586 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3588 sizeof(destroy_completion_handler
.shm_path
));
3593 * The session is destroyed. However, note that the command context
3594 * still holds a reference to the session, thus delaying its destruction
3595 * _at least_ up to the point when that reference is released.
3597 session_destroy(session
);
3598 if (reply_context
) {
3599 reply_context
->destruction_status
= destruction_last_error
;
3600 ret
= session_add_destroy_notifier(session
,
3601 cmd_destroy_session_reply
,
3602 (void *) reply_context
);
3604 ret
= LTTNG_ERR_FATAL
;
3616 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3618 int cmd_register_consumer(struct ltt_session
*session
,
3619 enum lttng_domain_type domain
, const char *sock_path
,
3620 struct consumer_data
*cdata
)
3623 struct consumer_socket
*socket
= NULL
;
3625 LTTNG_ASSERT(session
);
3626 LTTNG_ASSERT(cdata
);
3627 LTTNG_ASSERT(sock_path
);
3630 case LTTNG_DOMAIN_KERNEL
:
3632 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3634 LTTNG_ASSERT(ksess
);
3636 /* Can't register a consumer if there is already one */
3637 if (ksess
->consumer_fds_sent
!= 0) {
3638 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3642 sock
= lttcomm_connect_unix_sock(sock_path
);
3644 ret
= LTTNG_ERR_CONNECT_FAIL
;
3647 cdata
->cmd_sock
= sock
;
3649 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3650 if (socket
== NULL
) {
3653 PERROR("close register consumer");
3655 cdata
->cmd_sock
= -1;
3656 ret
= LTTNG_ERR_FATAL
;
3660 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3661 if (socket
->lock
== NULL
) {
3662 PERROR("zmalloc pthread mutex");
3663 ret
= LTTNG_ERR_FATAL
;
3667 pthread_mutex_init(socket
->lock
, NULL
);
3668 socket
->registered
= 1;
3671 consumer_add_socket(socket
, ksess
->consumer
);
3674 pthread_mutex_lock(&cdata
->pid_mutex
);
3676 pthread_mutex_unlock(&cdata
->pid_mutex
);
3681 /* TODO: Userspace tracing */
3682 ret
= LTTNG_ERR_UND
;
3690 consumer_destroy_socket(socket
);
3696 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3698 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3699 struct lttng_domain
**domains
)
3704 struct lttng_ht_iter iter
;
3706 if (session
->kernel_session
!= NULL
) {
3707 DBG3("Listing domains found kernel domain");
3711 if (session
->ust_session
!= NULL
) {
3712 DBG3("Listing domains found UST global domain");
3716 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3718 if (agt
->being_used
) {
3729 *domains
= calloc
<lttng_domain
>(nb_dom
);
3730 if (*domains
== NULL
) {
3731 ret
= LTTNG_ERR_FATAL
;
3735 if (session
->kernel_session
!= NULL
) {
3736 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3738 /* Kernel session buffer type is always GLOBAL */
3739 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3744 if (session
->ust_session
!= NULL
) {
3745 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3746 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3750 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3752 if (agt
->being_used
) {
3753 (*domains
)[index
].type
= agt
->domain
;
3754 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3764 /* Return negative value to differentiate return code */
3770 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3772 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3773 struct ltt_session
*session
,
3774 struct lttng_payload
*payload
)
3778 struct lttcomm_list_command_header cmd_header
= {};
3779 size_t cmd_header_offset
;
3780 enum lttng_error_code ret_code
;
3785 DBG("Listing channels for session %s", session
->name
);
3787 cmd_header_offset
= payload
->buffer
.size
;
3789 /* Reserve space for command reply header. */
3790 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3791 cmd_header_offset
+ sizeof(cmd_header
));
3793 ret_code
= LTTNG_ERR_NOMEM
;
3798 case LTTNG_DOMAIN_KERNEL
:
3800 /* Kernel channels */
3801 struct ltt_kernel_channel
*kchan
;
3802 if (session
->kernel_session
!= NULL
) {
3803 cds_list_for_each_entry(kchan
,
3804 &session
->kernel_session
->channel_list
.head
, list
) {
3805 uint64_t discarded_events
, lost_packets
;
3806 struct lttng_channel_extended
*extended
;
3808 extended
= (struct lttng_channel_extended
*)
3809 kchan
->channel
->attr
.extended
.ptr
;
3811 ret
= get_kernel_runtime_stats(session
, kchan
,
3812 &discarded_events
, &lost_packets
);
3814 ret_code
= LTTNG_ERR_UNK
;
3819 * Update the discarded_events and lost_packets
3820 * count for the channel
3822 extended
->discarded_events
= discarded_events
;
3823 extended
->lost_packets
= lost_packets
;
3825 ret
= lttng_channel_serialize(
3826 kchan
->channel
, &payload
->buffer
);
3828 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3829 kchan
->channel
->name
);
3830 ret_code
= LTTNG_ERR_UNK
;
3839 case LTTNG_DOMAIN_UST
:
3841 struct lttng_ht_iter iter
;
3842 struct ltt_ust_channel
*uchan
;
3845 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3846 &iter
.iter
, uchan
, node
.node
) {
3847 uint64_t discarded_events
= 0, lost_packets
= 0;
3848 struct lttng_channel
*channel
= NULL
;
3849 struct lttng_channel_extended
*extended
;
3851 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3853 ret_code
= LTTNG_ERR_NOMEM
;
3857 extended
= (struct lttng_channel_extended
*)
3858 channel
->attr
.extended
.ptr
;
3860 ret
= get_ust_runtime_stats(session
, uchan
,
3861 &discarded_events
, &lost_packets
);
3863 lttng_channel_destroy(channel
);
3864 ret_code
= LTTNG_ERR_UNK
;
3868 extended
->discarded_events
= discarded_events
;
3869 extended
->lost_packets
= lost_packets
;
3871 ret
= lttng_channel_serialize(
3872 channel
, &payload
->buffer
);
3874 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3876 lttng_channel_destroy(channel
);
3877 ret_code
= LTTNG_ERR_UNK
;
3881 lttng_channel_destroy(channel
);
3891 if (i
> UINT32_MAX
) {
3892 ERR("Channel count would overflow the channel listing command's reply");
3893 ret_code
= LTTNG_ERR_OVERFLOW
;
3897 /* Update command reply header. */
3898 cmd_header
.count
= (uint32_t) i
;
3899 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3900 sizeof(cmd_header
));
3901 ret_code
= LTTNG_OK
;
3908 * Command LTTNG_LIST_EVENTS processed by the client thread.
3910 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3911 struct ltt_session
*session
,
3913 struct lttng_payload
*reply_payload
)
3915 int buffer_resize_ret
;
3916 enum lttng_error_code ret_code
= LTTNG_OK
;
3917 struct lttcomm_list_command_header reply_command_header
= {};
3918 size_t reply_command_header_offset
;
3919 unsigned int nb_events
= 0;
3921 assert(reply_payload
);
3923 /* Reserve space for command reply header. */
3924 reply_command_header_offset
= reply_payload
->buffer
.size
;
3925 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3926 reply_command_header_offset
+
3927 sizeof(struct lttcomm_list_command_header
));
3928 if (buffer_resize_ret
) {
3929 ret_code
= LTTNG_ERR_NOMEM
;
3934 case LTTNG_DOMAIN_KERNEL
:
3935 if (session
->kernel_session
!= NULL
) {
3936 ret_code
= list_lttng_kernel_events(channel_name
,
3937 session
->kernel_session
, reply_payload
, &nb_events
);
3941 case LTTNG_DOMAIN_UST
:
3943 if (session
->ust_session
!= NULL
) {
3944 ret_code
= list_lttng_ust_global_events(channel_name
,
3945 &session
->ust_session
->domain_global
,
3946 reply_payload
, &nb_events
);
3951 case LTTNG_DOMAIN_LOG4J
:
3952 case LTTNG_DOMAIN_JUL
:
3953 case LTTNG_DOMAIN_PYTHON
:
3954 if (session
->ust_session
) {
3955 struct lttng_ht_iter iter
;
3959 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3960 &iter
.iter
, agt
, node
.node
) {
3961 if (agt
->domain
== domain
) {
3962 ret_code
= list_lttng_agent_events(
3963 agt
, reply_payload
, &nb_events
);
3972 ret_code
= LTTNG_ERR_UND
;
3976 if (nb_events
> UINT32_MAX
) {
3977 ret_code
= LTTNG_ERR_OVERFLOW
;
3981 /* Update command reply header. */
3982 reply_command_header
.count
= (uint32_t) nb_events
;
3983 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
3984 sizeof(reply_command_header
));
3991 * Using the session list, filled a lttng_session array to send back to the
3992 * client for session listing.
3994 * The session list lock MUST be acquired before calling this function. Use
3995 * session_lock_list() and session_unlock_list().
3997 enum lttng_error_code
cmd_list_lttng_sessions(
3998 struct lttng_payload
*reply_payload
, uid_t uid
, gid_t gid
)
4000 int buffer_resize_ret
;
4001 enum lttng_error_code ret_code
= LTTNG_OK
;
4002 struct lttcomm_list_command_header reply_command_header
= {};
4003 size_t reply_command_header_offset
;
4004 struct ltt_session
*session
;
4005 struct ltt_session_list
*list
= session_get_list();
4009 assert(reply_payload
);
4011 /* Reserve space for command reply header. */
4012 reply_command_header_offset
= reply_payload
->buffer
.size
;
4013 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
4014 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
4015 if (buffer_resize_ret
) {
4016 ret_code
= LTTNG_ERR_NOMEM
;
4020 DBG("Getting all available session for UID %d GID %d",
4023 cds_list_for_each_entry(session
, &list
->head
, list
) {
4024 struct lttng_session tmp_session
= {};
4025 struct lttng_session_extended tmp_extended
= {};
4027 tmp_session
.extended
.ptr
= &tmp_extended
;
4029 if (!session_get(session
)) {
4033 * Only list the sessions the user can control.
4035 if (!session_access_ok(session
, uid
) ||
4036 session
->destroyed
) {
4037 session_put(session
);
4041 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4042 struct ltt_ust_session
*usess
= session
->ust_session
;
4044 if (session
->consumer
->type
== CONSUMER_DST_NET
||
4045 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
4046 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
4047 ret
= build_network_session_path(
4048 tmp_session
.path
, sizeof(tmp_session
.path
), session
);
4050 ret
= snprintf(tmp_session
.path
, sizeof(tmp_session
.path
), "%s",
4051 session
->consumer
->dst
.session_root_path
);
4054 PERROR("snprintf session path");
4055 session_put(session
);
4059 strncpy(tmp_session
.name
, session
->name
, NAME_MAX
);
4060 tmp_session
.name
[NAME_MAX
- 1] = '\0';
4061 tmp_session
.enabled
= session
->active
;
4062 tmp_session
.snapshot_mode
= session
->snapshot_mode
;
4063 tmp_session
.live_timer_interval
= session
->live_timer
;
4064 LTTNG_OPTIONAL_SET(&tmp_extended
.creation_time
, (uint64_t) session
->creation_time
);
4065 ret
= lttng_session_serialize(&tmp_session
, reply_payload
);
4067 ret_code
= LTTNG_ERR_FATAL
;
4071 session_put(session
);
4074 if (i
> UINT32_MAX
) {
4075 ret_code
= LTTNG_ERR_OVERFLOW
;
4079 /* Update command reply header. */
4080 reply_command_header
.count
= (uint32_t) i
;
4081 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
4082 sizeof(reply_command_header
));
4083 ret_code
= LTTNG_OK
;
4089 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4090 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4092 int cmd_data_pending(struct ltt_session
*session
)
4095 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4096 struct ltt_ust_session
*usess
= session
->ust_session
;
4098 LTTNG_ASSERT(session
);
4100 DBG("Data pending for session %s", session
->name
);
4102 /* Session MUST be stopped to ask for data availability. */
4103 if (session
->active
) {
4104 ret
= LTTNG_ERR_SESSION_STARTED
;
4108 * If stopped, just make sure we've started before else the above call
4109 * will always send that there is data pending.
4111 * The consumer assumes that when the data pending command is received,
4112 * the trace has been started before or else no output data is written
4113 * by the streams which is a condition for data pending. So, this is
4114 * *VERY* important that we don't ask the consumer before a start
4117 if (!session
->has_been_started
) {
4123 /* A rotation is still pending, we have to wait. */
4124 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4125 DBG("Rotate still pending for session %s", session
->name
);
4130 if (ksess
&& ksess
->consumer
) {
4131 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4133 /* Data is still being extracted for the kernel. */
4138 if (usess
&& usess
->consumer
) {
4139 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4141 /* Data is still being extracted for the kernel. */
4146 /* Data is ready to be read by a viewer */
4154 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4156 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4158 int cmd_snapshot_add_output(struct ltt_session
*session
,
4159 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4162 struct snapshot_output
*new_output
;
4164 LTTNG_ASSERT(session
);
4165 LTTNG_ASSERT(output
);
4167 DBG("Cmd snapshot add output for session %s", session
->name
);
4170 * Can't create an output if the session is not set in no-output mode.
4172 if (session
->output_traces
) {
4173 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4177 if (session
->has_non_mmap_channel
) {
4178 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4182 /* Only one output is allowed until we have the "tee" feature. */
4183 if (session
->snapshot
.nb_output
== 1) {
4184 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4188 new_output
= snapshot_output_alloc();
4190 ret
= LTTNG_ERR_NOMEM
;
4194 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4195 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4196 &session
->snapshot
);
4198 if (ret
== -ENOMEM
) {
4199 ret
= LTTNG_ERR_NOMEM
;
4201 ret
= LTTNG_ERR_INVALID
;
4207 snapshot_add_output(&session
->snapshot
, new_output
);
4209 *id
= new_output
->id
;
4216 snapshot_output_destroy(new_output
);
4222 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4224 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4226 int cmd_snapshot_del_output(struct ltt_session
*session
,
4227 const struct lttng_snapshot_output
*output
)
4230 struct snapshot_output
*sout
= NULL
;
4232 LTTNG_ASSERT(session
);
4233 LTTNG_ASSERT(output
);
4238 * Permission denied to create an output if the session is not
4239 * set in no output mode.
4241 if (session
->output_traces
) {
4242 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4247 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4249 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4250 } else if (*output
->name
!= '\0') {
4251 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4253 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4256 ret
= LTTNG_ERR_INVALID
;
4260 snapshot_delete_output(&session
->snapshot
, sout
);
4261 snapshot_output_destroy(sout
);
4270 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4272 * If no output is available, outputs is untouched and 0 is returned.
4274 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4276 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4277 struct lttng_snapshot_output
**outputs
)
4280 struct lttng_snapshot_output
*list
= NULL
;
4281 struct lttng_ht_iter iter
;
4282 struct snapshot_output
*output
;
4284 LTTNG_ASSERT(session
);
4285 LTTNG_ASSERT(outputs
);
4287 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4290 * Permission denied to create an output if the session is not
4291 * set in no output mode.
4293 if (session
->output_traces
) {
4294 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4298 if (session
->snapshot
.nb_output
== 0) {
4303 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4305 ret
= -LTTNG_ERR_NOMEM
;
4309 /* Copy list from session to the new list object. */
4311 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4312 output
, node
.node
) {
4313 LTTNG_ASSERT(output
->consumer
);
4314 list
[idx
].id
= output
->id
;
4315 list
[idx
].max_size
= output
->max_size
;
4316 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4317 sizeof(list
[idx
].name
))) {
4318 ret
= -LTTNG_ERR_INVALID
;
4321 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4322 if (lttng_strncpy(list
[idx
].ctrl_url
,
4323 output
->consumer
->dst
.session_root_path
,
4324 sizeof(list
[idx
].ctrl_url
))) {
4325 ret
= -LTTNG_ERR_INVALID
;
4330 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4331 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4333 ret
= -LTTNG_ERR_NOMEM
;
4338 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4339 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4341 ret
= -LTTNG_ERR_NOMEM
;
4350 ret
= session
->snapshot
.nb_output
;
4359 * Check if we can regenerate the metadata for this session.
4360 * Only kernel, UST per-uid and non-live sessions are supported.
4362 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4365 int check_regenerate_metadata_support(struct ltt_session
*session
)
4369 LTTNG_ASSERT(session
);
4371 if (session
->live_timer
!= 0) {
4372 ret
= LTTNG_ERR_LIVE_SESSION
;
4375 if (!session
->active
) {
4376 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4379 if (session
->ust_session
) {
4380 switch (session
->ust_session
->buffer_type
) {
4381 case LTTNG_BUFFER_PER_UID
:
4383 case LTTNG_BUFFER_PER_PID
:
4384 ret
= LTTNG_ERR_PER_PID_SESSION
;
4388 ret
= LTTNG_ERR_UNK
;
4392 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4393 session
->consumer
->relay_minor_version
< 8) {
4394 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4404 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4406 * Ask the consumer to truncate the existing metadata file(s) and
4407 * then regenerate the metadata. Live and per-pid sessions are not
4408 * supported and return an error.
4410 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4412 int cmd_regenerate_metadata(struct ltt_session
*session
)
4416 LTTNG_ASSERT(session
);
4418 ret
= check_regenerate_metadata_support(session
);
4423 if (session
->kernel_session
) {
4424 ret
= kernctl_session_regenerate_metadata(
4425 session
->kernel_session
->fd
);
4427 ERR("Failed to regenerate the kernel metadata");
4432 if (session
->ust_session
) {
4433 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4435 ERR("Failed to regenerate the UST metadata");
4439 DBG("Cmd metadata regenerate for session %s", session
->name
);
4447 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4449 * Ask the tracer to regenerate a new statedump.
4451 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4453 int cmd_regenerate_statedump(struct ltt_session
*session
)
4457 LTTNG_ASSERT(session
);
4459 if (!session
->active
) {
4460 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4464 if (session
->kernel_session
) {
4465 ret
= kernctl_session_regenerate_statedump(
4466 session
->kernel_session
->fd
);
4468 * Currently, the statedump in kernel can only fail if out
4472 if (ret
== -ENOMEM
) {
4473 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4475 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4477 ERR("Failed to regenerate the kernel statedump");
4482 if (session
->ust_session
) {
4483 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4485 * Currently, the statedump in UST always returns 0.
4488 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4489 ERR("Failed to regenerate the UST statedump");
4493 DBG("Cmd regenerate statedump for session %s", session
->name
);
4501 enum lttng_error_code
synchronize_tracer_notifier_register(
4502 struct notification_thread_handle
*notification_thread
,
4503 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4505 enum lttng_error_code ret_code
;
4506 const struct lttng_condition
*condition
=
4507 lttng_trigger_get_const_condition(trigger
);
4508 const char *trigger_name
;
4509 uid_t trigger_owner
;
4510 enum lttng_trigger_status trigger_status
;
4511 const enum lttng_domain_type trigger_domain
=
4512 lttng_trigger_get_underlying_domain_type_restriction(
4515 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4516 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4518 LTTNG_ASSERT(condition
);
4519 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4520 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4522 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4523 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4524 trigger_name
: "(anonymous)";
4526 session_lock_list();
4527 switch (trigger_domain
) {
4528 case LTTNG_DOMAIN_KERNEL
:
4530 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4531 if (ret_code
!= LTTNG_OK
) {
4532 enum lttng_error_code notif_thread_unregister_ret
;
4534 notif_thread_unregister_ret
=
4535 notification_thread_command_unregister_trigger(
4536 notification_thread
, trigger
);
4538 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4539 /* Return the original error code. */
4540 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4542 (int) trigger_owner
,
4548 case LTTNG_DOMAIN_UST
:
4549 ust_app_global_update_all_event_notifier_rules();
4551 case LTTNG_DOMAIN_JUL
:
4552 case LTTNG_DOMAIN_LOG4J
:
4553 case LTTNG_DOMAIN_PYTHON
:
4555 /* Agent domains. */
4556 struct agent
*agt
= agent_find_by_event_notifier_domain(
4560 agt
= agent_create(trigger_domain
);
4562 ret_code
= LTTNG_ERR_NOMEM
;
4563 goto end_unlock_session_list
;
4566 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4569 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4570 if (ret_code
!= LTTNG_OK
) {
4571 goto end_unlock_session_list
;
4576 case LTTNG_DOMAIN_NONE
:
4581 ret_code
= LTTNG_OK
;
4582 end_unlock_session_list
:
4583 session_unlock_list();
4587 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4588 struct lttng_trigger
*trigger
,
4589 bool is_trigger_anonymous
,
4590 struct notification_thread_handle
*notification_thread
,
4591 struct lttng_trigger
**return_trigger
)
4593 enum lttng_error_code ret_code
;
4594 const char *trigger_name
;
4595 uid_t trigger_owner
;
4596 enum lttng_trigger_status trigger_status
;
4598 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4599 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4600 trigger_name
: "(anonymous)";
4602 trigger_status
= lttng_trigger_get_owner_uid(
4603 trigger
, &trigger_owner
);
4604 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4606 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4607 trigger_name
, (int) trigger_owner
,
4608 (int) lttng_credentials_get_uid(cmd_creds
));
4611 * Validate the trigger credentials against the command credentials.
4612 * Only the root user can register a trigger with non-matching
4615 if (!lttng_credentials_is_equal_uid(
4616 lttng_trigger_get_credentials(trigger
),
4618 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4619 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4620 trigger_name
, (int) trigger_owner
,
4621 (int) lttng_credentials_get_uid(cmd_creds
));
4622 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4628 * The bytecode generation also serves as a validation step for the
4629 * bytecode expressions.
4631 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4632 if (ret_code
!= LTTNG_OK
) {
4633 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4634 trigger_name
, (int) trigger_owner
, ret_code
);
4639 * A reference to the trigger is acquired by the notification thread.
4640 * It is safe to return the same trigger to the caller since it the
4641 * other user holds a reference.
4643 * The trigger is modified during the execution of the
4644 * "register trigger" command. However, by the time the command returns,
4645 * it is safe to use without any locking as its properties are
4648 ret_code
= notification_thread_command_register_trigger(
4649 notification_thread
, trigger
, is_trigger_anonymous
);
4650 if (ret_code
!= LTTNG_OK
) {
4651 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4652 trigger_name
, (int) trigger_owner
, ret_code
);
4656 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4657 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4658 trigger_name
: "(anonymous)";
4661 * Synchronize tracers if the trigger adds an event notifier.
4663 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4664 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4665 trigger
, cmd_creds
);
4666 if (ret_code
!= LTTNG_OK
) {
4667 ERR("Error registering tracer notifier: %s",
4668 lttng_strerror(-ret_code
));
4674 * Return an updated trigger to the client.
4676 * Since a modified version of the same trigger is returned, acquire a
4677 * reference to the trigger so the caller doesn't have to care if those
4678 * are distinct instances or not.
4680 if (ret_code
== LTTNG_OK
) {
4681 lttng_trigger_get(trigger
);
4682 *return_trigger
= trigger
;
4683 /* Ownership of trigger was transferred to caller. */
4691 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4692 const struct lttng_trigger
*trigger
)
4694 enum lttng_error_code ret_code
;
4695 const struct lttng_condition
*condition
=
4696 lttng_trigger_get_const_condition(trigger
);
4697 const enum lttng_domain_type trigger_domain
=
4698 lttng_trigger_get_underlying_domain_type_restriction(
4701 LTTNG_ASSERT(condition
);
4702 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4703 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4705 session_lock_list();
4706 switch (trigger_domain
) {
4707 case LTTNG_DOMAIN_KERNEL
:
4708 ret_code
= kernel_unregister_event_notifier(trigger
);
4709 if (ret_code
!= LTTNG_OK
) {
4710 goto end_unlock_session_list
;
4714 case LTTNG_DOMAIN_UST
:
4715 ust_app_global_update_all_event_notifier_rules();
4717 case LTTNG_DOMAIN_JUL
:
4718 case LTTNG_DOMAIN_LOG4J
:
4719 case LTTNG_DOMAIN_PYTHON
:
4721 /* Agent domains. */
4722 struct agent
*agt
= agent_find_by_event_notifier_domain(
4726 * This trigger was never registered in the first place. Calling
4727 * this function under those circumstances is an internal error.
4730 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4731 if (ret_code
!= LTTNG_OK
) {
4732 goto end_unlock_session_list
;
4737 case LTTNG_DOMAIN_NONE
:
4742 ret_code
= LTTNG_OK
;
4744 end_unlock_session_list
:
4745 session_unlock_list();
4749 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4750 const struct lttng_trigger
*trigger
,
4751 struct notification_thread_handle
*notification_thread
)
4753 enum lttng_error_code ret_code
;
4754 const char *trigger_name
;
4755 uid_t trigger_owner
;
4756 enum lttng_trigger_status trigger_status
;
4757 struct lttng_trigger
*sessiond_trigger
= NULL
;
4759 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4760 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4761 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4762 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4764 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4765 trigger_name
, (int) trigger_owner
,
4766 (int) lttng_credentials_get_uid(cmd_creds
));
4769 * Validate the trigger credentials against the command credentials.
4770 * Only the root user can unregister a trigger with non-matching
4773 if (!lttng_credentials_is_equal_uid(
4774 lttng_trigger_get_credentials(trigger
),
4776 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4777 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4778 trigger_name
, (int) trigger_owner
,
4779 (int) lttng_credentials_get_uid(cmd_creds
));
4780 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4785 /* Fetch the sessiond side trigger object. */
4786 ret_code
= notification_thread_command_get_trigger(
4787 notification_thread
, trigger
, &sessiond_trigger
);
4788 if (ret_code
!= LTTNG_OK
) {
4789 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4790 trigger_name
, (int) trigger_owner
, ret_code
);
4794 LTTNG_ASSERT(sessiond_trigger
);
4797 * From this point on, no matter what, consider the trigger
4800 * We set the unregistered state of the sessiond side trigger object in
4801 * the client thread since we want to minimize the possibility of the
4802 * notification thread being stalled due to a long execution of an
4803 * action that required the trigger lock.
4805 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4807 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4809 if (ret_code
!= LTTNG_OK
) {
4810 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4811 trigger_name
, (int) trigger_owner
, ret_code
);
4816 * Synchronize tracers if the trigger removes an event notifier.
4817 * Do this even if the trigger unregistration failed to at least stop
4818 * the tracers from producing notifications associated with this
4821 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4822 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4823 if (ret_code
!= LTTNG_OK
) {
4824 ERR("Error unregistering trigger to tracer.");
4831 lttng_trigger_put(sessiond_trigger
);
4835 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4836 struct notification_thread_handle
*notification_thread
,
4837 struct lttng_triggers
**return_triggers
)
4840 enum lttng_error_code ret_code
;
4841 struct lttng_triggers
*triggers
= NULL
;
4843 /* Get the set of triggers from the notification thread. */
4844 ret_code
= notification_thread_command_list_triggers(
4845 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4846 if (ret_code
!= LTTNG_OK
) {
4850 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4852 ret_code
= LTTNG_ERR_UNK
;
4856 *return_triggers
= triggers
;
4858 ret_code
= LTTNG_OK
;
4860 lttng_triggers_destroy(triggers
);
4864 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4865 const struct lttng_error_query
*query
,
4866 struct lttng_error_query_results
**_results
,
4867 struct notification_thread_handle
*notification_thread
)
4869 enum lttng_error_code ret_code
;
4870 const struct lttng_trigger
*query_target_trigger
;
4871 const struct lttng_action
*query_target_action
= NULL
;
4872 struct lttng_trigger
*matching_trigger
= NULL
;
4873 const char *trigger_name
;
4874 uid_t trigger_owner
;
4875 enum lttng_trigger_status trigger_status
;
4876 struct lttng_error_query_results
*results
= NULL
;
4878 switch (lttng_error_query_get_target_type(query
)) {
4879 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4880 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4882 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4883 query_target_trigger
=
4884 lttng_error_query_condition_borrow_target(query
);
4886 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4887 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4894 LTTNG_ASSERT(query_target_trigger
);
4896 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4897 query_target_trigger
, &matching_trigger
);
4898 if (ret_code
!= LTTNG_OK
) {
4902 /* No longer needed. */
4903 query_target_trigger
= NULL
;
4905 if (lttng_error_query_get_target_type(query
) ==
4906 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4907 /* Get the sessiond-side version of the target action. */
4908 query_target_action
=
4909 lttng_error_query_action_borrow_action_target(
4910 query
, matching_trigger
);
4913 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4914 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4915 trigger_name
: "(anonymous)";
4916 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4918 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4920 results
= lttng_error_query_results_create();
4922 ret_code
= LTTNG_ERR_NOMEM
;
4926 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4927 trigger_name
, (int) trigger_owner
,
4928 (int) lttng_credentials_get_uid(cmd_creds
));
4931 * Validate the trigger credentials against the command credentials.
4932 * Only the root user can target a trigger with non-matching
4935 if (!lttng_credentials_is_equal_uid(
4936 lttng_trigger_get_credentials(matching_trigger
),
4938 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4939 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4940 trigger_name
, (int) trigger_owner
,
4941 (int) lttng_credentials_get_uid(cmd_creds
));
4942 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4947 switch (lttng_error_query_get_target_type(query
)) {
4948 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4949 trigger_status
= lttng_trigger_add_error_results(
4950 matching_trigger
, results
);
4952 switch (trigger_status
) {
4953 case LTTNG_TRIGGER_STATUS_OK
:
4956 ret_code
= LTTNG_ERR_UNK
;
4961 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4963 trigger_status
= lttng_trigger_condition_add_error_results(
4964 matching_trigger
, results
);
4966 switch (trigger_status
) {
4967 case LTTNG_TRIGGER_STATUS_OK
:
4970 ret_code
= LTTNG_ERR_UNK
;
4976 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4978 const enum lttng_action_status action_status
=
4979 lttng_action_add_error_query_results(
4980 query_target_action
, results
);
4982 switch (action_status
) {
4983 case LTTNG_ACTION_STATUS_OK
:
4986 ret_code
= LTTNG_ERR_UNK
;
4997 *_results
= results
;
4999 ret_code
= LTTNG_OK
;
5001 lttng_trigger_put(matching_trigger
);
5002 lttng_error_query_results_destroy(results
);
5007 * Send relayd sockets from snapshot output to consumer. Ignore request if the
5008 * snapshot output is *not* set with a remote destination.
5010 * Return LTTNG_OK on success or a LTTNG_ERR code.
5012 static enum lttng_error_code
set_relayd_for_snapshot(
5013 struct consumer_output
*output
,
5014 const struct ltt_session
*session
)
5016 enum lttng_error_code status
= LTTNG_OK
;
5017 struct lttng_ht_iter iter
;
5018 struct consumer_socket
*socket
;
5019 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
5020 const char *base_path
;
5022 LTTNG_ASSERT(output
);
5023 LTTNG_ASSERT(session
);
5025 DBG2("Set relayd object from snapshot output");
5027 if (session
->current_trace_chunk
) {
5028 enum lttng_trace_chunk_status chunk_status
=
5029 lttng_trace_chunk_get_id(
5030 session
->current_trace_chunk
,
5031 ¤t_chunk_id
.value
);
5033 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5034 current_chunk_id
.is_set
= true;
5036 ERR("Failed to get current trace chunk id");
5037 status
= LTTNG_ERR_UNK
;
5042 /* Ignore if snapshot consumer output is not network. */
5043 if (output
->type
!= CONSUMER_DST_NET
) {
5048 * The snapshot record URI base path overrides the session
5051 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5052 base_path
= output
->dst
.net
.control
.subdir
;
5054 base_path
= session
->base_path
;
5058 * For each consumer socket, create and send the relayd object of the
5062 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
5063 pthread_mutex_lock(socket
->lock
);
5064 status
= send_consumer_relayd_sockets(*session
, output
, socket
, base_path
,
5065 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
5066 pthread_mutex_unlock(socket
->lock
);
5067 if (status
!= LTTNG_OK
) {
5079 * Record a kernel snapshot.
5081 * Return LTTNG_OK on success or a LTTNG_ERR code.
5083 static enum lttng_error_code
record_kernel_snapshot(
5084 struct ltt_kernel_session
*ksess
,
5085 const struct consumer_output
*output
,
5086 const struct ltt_session
*session
,
5087 uint64_t nb_packets_per_stream
)
5089 enum lttng_error_code status
;
5091 LTTNG_ASSERT(ksess
);
5092 LTTNG_ASSERT(output
);
5093 LTTNG_ASSERT(session
);
5095 status
= kernel_snapshot_record(
5096 ksess
, output
, nb_packets_per_stream
);
5101 * Record a UST snapshot.
5103 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5105 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5106 const struct consumer_output
*output
,
5107 const struct ltt_session
*session
,
5108 uint64_t nb_packets_per_stream
)
5110 enum lttng_error_code status
;
5112 LTTNG_ASSERT(usess
);
5113 LTTNG_ASSERT(output
);
5114 LTTNG_ASSERT(session
);
5116 status
= ust_app_snapshot_record(
5117 usess
, output
, nb_packets_per_stream
);
5122 uint64_t get_session_size_one_more_packet_per_stream(
5123 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5125 uint64_t tot_size
= 0;
5127 if (session
->kernel_session
) {
5128 struct ltt_kernel_channel
*chan
;
5129 const struct ltt_kernel_session
*ksess
=
5130 session
->kernel_session
;
5132 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5133 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5135 * Don't take channel into account if we
5136 * already grab all its packets.
5140 tot_size
+= chan
->channel
->attr
.subbuf_size
5141 * chan
->stream_count
;
5145 if (session
->ust_session
) {
5146 const struct ltt_ust_session
*usess
= session
->ust_session
;
5148 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5156 * Calculate the number of packets we can grab from each stream that
5157 * fits within the overall snapshot max size.
5159 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5160 * the number of packets per stream.
5162 * TODO: this approach is not perfect: we consider the worse case
5163 * (packet filling the sub-buffers) as an upper bound, but we could do
5164 * better if we do this calculation while we actually grab the packet
5165 * content: we would know how much padding we don't actually store into
5168 * This algorithm is currently bounded by the number of packets per
5171 * Since we call this algorithm before actually grabbing the data, it's
5172 * an approximation: for instance, applications could appear/disappear
5173 * in between this call and actually grabbing data.
5176 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5180 uint64_t cur_nb_packets
= 0;
5183 return 0; /* Infinite */
5186 size_left
= max_size
;
5188 uint64_t one_more_packet_tot_size
;
5190 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5191 session
, cur_nb_packets
);
5192 if (!one_more_packet_tot_size
) {
5193 /* We are already grabbing all packets. */
5196 size_left
-= one_more_packet_tot_size
;
5197 if (size_left
< 0) {
5202 if (!cur_nb_packets
&& size_left
!= max_size
) {
5203 /* Not enough room to grab one packet of each stream, error. */
5206 return cur_nb_packets
;
5210 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5211 const struct snapshot_output
*snapshot_output
)
5213 int64_t nb_packets_per_stream
;
5214 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5216 enum lttng_error_code ret_code
= LTTNG_OK
;
5217 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5218 struct consumer_output
*original_ust_consumer_output
= NULL
;
5219 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5220 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5221 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5223 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5225 snapshot_output
->name
,
5226 snapshot_output
->datetime
,
5227 snapshot_output
->nb_snapshot
);
5228 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5229 ERR("Failed to format snapshot name");
5230 ret_code
= LTTNG_ERR_INVALID
;
5233 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5234 snapshot_output
->name
, session
->name
,
5235 snapshot_chunk_name
);
5236 if (!session
->kernel_session
&& !session
->ust_session
) {
5237 ERR("Failed to record snapshot as no channels exist");
5238 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5242 if (session
->kernel_session
) {
5243 original_kernel_consumer_output
=
5244 session
->kernel_session
->consumer
;
5245 snapshot_kernel_consumer_output
=
5246 consumer_copy_output(snapshot_output
->consumer
);
5247 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5248 snapshot_chunk_name
);
5250 /* Copy the original domain subdir. */
5251 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5252 original_kernel_consumer_output
->domain_subdir
);
5254 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5255 original_kernel_consumer_output
);
5257 ERR("Failed to copy consumer sockets from snapshot output configuration");
5258 ret_code
= LTTNG_ERR_NOMEM
;
5261 ret_code
= set_relayd_for_snapshot(
5262 snapshot_kernel_consumer_output
, session
);
5263 if (ret_code
!= LTTNG_OK
) {
5264 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5267 session
->kernel_session
->consumer
=
5268 snapshot_kernel_consumer_output
;
5270 if (session
->ust_session
) {
5271 original_ust_consumer_output
= session
->ust_session
->consumer
;
5272 snapshot_ust_consumer_output
=
5273 consumer_copy_output(snapshot_output
->consumer
);
5274 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5275 snapshot_chunk_name
);
5277 /* Copy the original domain subdir. */
5278 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5279 original_ust_consumer_output
->domain_subdir
);
5281 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5282 original_ust_consumer_output
);
5284 ERR("Failed to copy consumer sockets from snapshot output configuration");
5285 ret_code
= LTTNG_ERR_NOMEM
;
5288 ret_code
= set_relayd_for_snapshot(
5289 snapshot_ust_consumer_output
, session
);
5290 if (ret_code
!= LTTNG_OK
) {
5291 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5294 session
->ust_session
->consumer
=
5295 snapshot_ust_consumer_output
;
5298 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5299 snapshot_kernel_consumer_output
?:
5300 snapshot_ust_consumer_output
,
5301 consumer_output_get_base_path(
5302 snapshot_output
->consumer
),
5303 snapshot_chunk_name
);
5304 if (!snapshot_trace_chunk
) {
5305 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5307 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5310 LTTNG_ASSERT(!session
->current_trace_chunk
);
5311 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5312 lttng_trace_chunk_put(snapshot_trace_chunk
);
5313 snapshot_trace_chunk
= NULL
;
5315 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5317 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5321 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5322 snapshot_output
->max_size
);
5323 if (nb_packets_per_stream
< 0) {
5324 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5325 goto error_close_trace_chunk
;
5328 if (session
->kernel_session
) {
5329 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5330 snapshot_kernel_consumer_output
, session
,
5331 nb_packets_per_stream
);
5332 if (ret_code
!= LTTNG_OK
) {
5333 goto error_close_trace_chunk
;
5337 if (session
->ust_session
) {
5338 ret_code
= record_ust_snapshot(session
->ust_session
,
5339 snapshot_ust_consumer_output
, session
,
5340 nb_packets_per_stream
);
5341 if (ret_code
!= LTTNG_OK
) {
5342 goto error_close_trace_chunk
;
5346 error_close_trace_chunk
:
5347 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5348 ERR("Failed to release the current trace chunk of session \"%s\"",
5350 ret_code
= LTTNG_ERR_UNK
;
5353 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5354 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5356 * Don't goto end; make sure the chunk is closed for the session
5357 * to allow future snapshots.
5359 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5361 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5364 lttng_trace_chunk_put(snapshot_trace_chunk
);
5365 snapshot_trace_chunk
= NULL
;
5367 if (original_ust_consumer_output
) {
5368 session
->ust_session
->consumer
= original_ust_consumer_output
;
5370 if (original_kernel_consumer_output
) {
5371 session
->kernel_session
->consumer
=
5372 original_kernel_consumer_output
;
5374 consumer_output_put(snapshot_ust_consumer_output
);
5375 consumer_output_put(snapshot_kernel_consumer_output
);
5380 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5382 * The wait parameter is ignored so this call always wait for the snapshot to
5383 * complete before returning.
5385 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5387 int cmd_snapshot_record(struct ltt_session
*session
,
5388 const struct lttng_snapshot_output
*output
,
5389 int wait
__attribute__((unused
)))
5391 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5393 unsigned int snapshot_success
= 0;
5395 struct snapshot_output
*tmp_output
= NULL
;
5397 LTTNG_ASSERT(session
);
5398 LTTNG_ASSERT(output
);
5400 DBG("Cmd snapshot record for session %s", session
->name
);
5402 /* Get the datetime for the snapshot output directory. */
5403 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5406 cmd_ret
= LTTNG_ERR_INVALID
;
5411 * Permission denied to create an output if the session is not
5412 * set in no output mode.
5414 if (session
->output_traces
) {
5415 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5419 /* The session needs to be started at least once. */
5420 if (!session
->has_been_started
) {
5421 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5425 /* Use temporary output for the session. */
5426 if (*output
->ctrl_url
!= '\0') {
5427 tmp_output
= snapshot_output_alloc();
5429 cmd_ret
= LTTNG_ERR_NOMEM
;
5433 ret
= snapshot_output_init(session
, output
->max_size
,
5435 output
->ctrl_url
, output
->data_url
,
5439 if (ret
== -ENOMEM
) {
5440 cmd_ret
= LTTNG_ERR_NOMEM
;
5442 cmd_ret
= LTTNG_ERR_INVALID
;
5446 /* Use the global session count for the temporary snapshot. */
5447 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5449 /* Use the global datetime */
5450 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5451 cmd_ret
= snapshot_record(session
, tmp_output
);
5452 if (cmd_ret
!= LTTNG_OK
) {
5455 snapshot_success
= 1;
5457 struct snapshot_output
*sout
;
5458 struct lttng_ht_iter iter
;
5461 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5462 &iter
.iter
, sout
, node
.node
) {
5463 struct snapshot_output output_copy
;
5466 * Make a local copy of the output and override output
5467 * parameters with those provided as part of the
5470 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5472 if (output
->max_size
!= (uint64_t) -1ULL) {
5473 output_copy
.max_size
= output
->max_size
;
5476 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5477 memcpy(output_copy
.datetime
, datetime
,
5480 /* Use temporary name. */
5481 if (*output
->name
!= '\0') {
5482 if (lttng_strncpy(output_copy
.name
,
5484 sizeof(output_copy
.name
))) {
5485 cmd_ret
= LTTNG_ERR_INVALID
;
5491 cmd_ret
= snapshot_record(session
, &output_copy
);
5492 if (cmd_ret
!= LTTNG_OK
) {
5496 snapshot_success
= 1;
5501 if (snapshot_success
) {
5502 session
->snapshot
.nb_snapshot
++;
5504 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5509 snapshot_output_destroy(tmp_output
);
5515 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5517 int cmd_set_session_shm_path(struct ltt_session
*session
,
5518 const char *shm_path
)
5521 LTTNG_ASSERT(session
);
5524 * Can only set shm path before session is started.
5526 if (session
->has_been_started
) {
5527 return LTTNG_ERR_SESSION_STARTED
;
5530 strncpy(session
->shm_path
, shm_path
,
5531 sizeof(session
->shm_path
));
5532 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5538 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5540 * Ask the consumer to rotate the session output directory.
5541 * The session lock must be held.
5543 * Returns LTTNG_OK on success or else a negative LTTng error code.
5545 int cmd_rotate_session(struct ltt_session
*session
,
5546 struct lttng_rotate_session_return
*rotate_return
,
5547 bool quiet_rotation
,
5548 enum lttng_trace_chunk_command_type command
)
5551 uint64_t ongoing_rotation_chunk_id
;
5552 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5553 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5554 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5555 enum lttng_trace_chunk_status chunk_status
;
5556 bool failed_to_rotate
= false;
5557 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5559 LTTNG_ASSERT(session
);
5561 if (!session
->has_been_started
) {
5562 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5567 * Explicit rotation is not supported for live sessions.
5568 * However, live sessions can perform a quiet rotation on
5570 * Rotation is not supported for snapshot traces (no output).
5572 if ((!quiet_rotation
&& session
->live_timer
) ||
5573 !session
->output_traces
) {
5574 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5578 /* Unsupported feature in lttng-relayd before 2.11. */
5579 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5580 (session
->consumer
->relay_major_version
== 2 &&
5581 session
->consumer
->relay_minor_version
< 11)) {
5582 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5586 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5587 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5588 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5592 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5593 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5595 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5600 * After a stop, we only allow one rotation to occur, the other ones are
5601 * useless until a new start.
5603 if (session
->rotated_after_last_stop
) {
5604 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5606 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5611 * After a stop followed by a clear, disallow following rotations a they would
5612 * generate empty chunks.
5614 if (session
->cleared_after_last_stop
) {
5615 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5617 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5621 if (session
->active
) {
5622 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5624 if (!new_trace_chunk
) {
5625 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5631 * The current trace chunk becomes the chunk being archived.
5633 * After this point, "chunk_being_archived" must absolutely
5634 * be closed on the consumer(s), otherwise it will never be
5635 * cleaned-up, which will result in a leak.
5637 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5638 &chunk_being_archived
);
5640 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5644 if (session
->kernel_session
) {
5645 cmd_ret
= kernel_rotate_session(session
);
5646 if (cmd_ret
!= LTTNG_OK
) {
5647 failed_to_rotate
= true;
5648 rotation_fail_code
= cmd_ret
;
5651 if (session
->ust_session
) {
5652 cmd_ret
= ust_app_rotate_session(session
);
5653 if (cmd_ret
!= LTTNG_OK
) {
5654 failed_to_rotate
= true;
5655 rotation_fail_code
= cmd_ret
;
5659 if (!session
->active
) {
5660 session
->rotated_after_last_stop
= true;
5663 if (!chunk_being_archived
) {
5664 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5666 if (failed_to_rotate
) {
5667 cmd_ret
= rotation_fail_code
;
5674 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5675 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5676 &ongoing_rotation_chunk_id
);
5677 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5679 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5680 command
, session
->last_chunk_path
);
5682 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5686 if (failed_to_rotate
) {
5687 cmd_ret
= rotation_fail_code
;
5691 session
->quiet_rotation
= quiet_rotation
;
5692 ret
= timer_session_rotation_pending_check_start(session
,
5693 DEFAULT_ROTATE_PENDING_TIMER
);
5695 cmd_ret
= LTTNG_ERR_UNK
;
5699 if (rotate_return
) {
5700 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5703 session
->chunk_being_archived
= chunk_being_archived
;
5704 chunk_being_archived
= NULL
;
5705 if (!quiet_rotation
) {
5706 ret
= notification_thread_command_session_rotation_ongoing(
5707 the_notification_thread_handle
, session
->id
,
5708 ongoing_rotation_chunk_id
);
5709 if (ret
!= LTTNG_OK
) {
5710 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5712 cmd_ret
= (lttng_error_code
) ret
;
5716 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5717 session
->name
, ongoing_rotation_chunk_id
);
5719 lttng_trace_chunk_put(new_trace_chunk
);
5720 lttng_trace_chunk_put(chunk_being_archived
);
5721 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5724 if (session_reset_rotation_state(session
,
5725 LTTNG_ROTATION_STATE_ERROR
)) {
5726 ERR("Failed to reset rotation state of session \"%s\"",
5733 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5735 * Check if the session has finished its rotation.
5737 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5739 int cmd_rotate_get_info(struct ltt_session
*session
,
5740 struct lttng_rotation_get_info_return
*info_return
,
5741 uint64_t rotation_id
)
5743 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5744 enum lttng_rotation_state rotation_state
;
5746 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5747 session
->most_recent_chunk_id
.value
);
5749 if (session
->chunk_being_archived
) {
5750 enum lttng_trace_chunk_status chunk_status
;
5753 chunk_status
= lttng_trace_chunk_get_id(
5754 session
->chunk_being_archived
,
5756 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5758 rotation_state
= rotation_id
== chunk_id
?
5759 LTTNG_ROTATION_STATE_ONGOING
:
5760 LTTNG_ROTATION_STATE_EXPIRED
;
5762 if (session
->last_archived_chunk_id
.is_set
&&
5763 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5764 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5766 rotation_state
= session
->rotation_state
;
5770 switch (rotation_state
) {
5771 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5772 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5775 case LTTNG_ROTATION_STATE_EXPIRED
:
5776 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5777 rotation_id
, session
->name
);
5779 case LTTNG_ROTATION_STATE_ONGOING
:
5780 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5781 rotation_id
, session
->name
);
5783 case LTTNG_ROTATION_STATE_COMPLETED
:
5787 char *current_tracing_path_reply
;
5788 size_t current_tracing_path_reply_len
;
5790 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5791 rotation_id
, session
->name
);
5793 switch (session_get_consumer_destination_type(session
)) {
5794 case CONSUMER_DST_LOCAL
:
5795 current_tracing_path_reply
=
5796 info_return
->location
.local
.absolute_path
;
5797 current_tracing_path_reply_len
=
5798 sizeof(info_return
->location
.local
.absolute_path
);
5799 info_return
->location_type
=
5800 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5801 fmt_ret
= asprintf(&chunk_path
,
5802 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5803 session_get_base_path(session
),
5804 session
->last_archived_chunk_name
);
5805 if (fmt_ret
== -1) {
5806 PERROR("Failed to format the path of the last archived trace chunk");
5807 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5808 cmd_ret
= LTTNG_ERR_UNK
;
5812 case CONSUMER_DST_NET
:
5814 uint16_t ctrl_port
, data_port
;
5816 current_tracing_path_reply
=
5817 info_return
->location
.relay
.relative_path
;
5818 current_tracing_path_reply_len
=
5819 sizeof(info_return
->location
.relay
.relative_path
);
5820 /* Currently the only supported relay protocol. */
5821 info_return
->location
.relay
.protocol
=
5822 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5824 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5825 session_get_net_consumer_hostname(session
),
5826 sizeof(info_return
->location
.relay
.host
));
5828 ERR("Failed to copy host name to rotate_get_info reply");
5829 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5830 cmd_ret
= LTTNG_ERR_SET_URL
;
5834 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5835 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5836 info_return
->location
.relay
.ports
.data
= data_port
;
5837 info_return
->location_type
=
5838 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5839 chunk_path
= strdup(session
->last_chunk_path
);
5841 ERR("Failed to allocate the path of the last archived trace chunk");
5842 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5843 cmd_ret
= LTTNG_ERR_UNK
;
5852 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5853 chunk_path
, current_tracing_path_reply_len
);
5856 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5857 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5858 cmd_ret
= LTTNG_ERR_UNK
;
5864 case LTTNG_ROTATION_STATE_ERROR
:
5865 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5866 rotation_id
, session
->name
);
5874 info_return
->status
= (int32_t) rotation_state
;
5879 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5881 * Configure the automatic rotation parameters.
5882 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5883 * 'activate' to false means deactivate the rotation schedule and validate that
5884 * 'new_value' has the same value as the currently active value.
5886 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5888 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5889 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5891 struct notification_thread_handle
*notification_thread_handle
)
5894 uint64_t *parameter_value
;
5896 LTTNG_ASSERT(session
);
5898 DBG("Cmd rotate set schedule session %s", session
->name
);
5900 if (session
->live_timer
|| !session
->output_traces
) {
5901 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5902 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5906 switch (schedule_type
) {
5907 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5908 parameter_value
= &session
->rotate_size
;
5910 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5911 parameter_value
= &session
->rotate_timer_period
;
5912 if (new_value
>= UINT_MAX
) {
5913 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5914 new_value
, UINT_MAX
);
5915 ret
= LTTNG_ERR_INVALID
;
5920 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5921 ret
= LTTNG_ERR_INVALID
;
5925 /* Improper use of the API. */
5926 if (new_value
== -1ULL) {
5927 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5928 ret
= LTTNG_ERR_INVALID
;
5933 * As indicated in struct ltt_session's comments, a value of == 0 means
5934 * this schedule rotation type is not in use.
5936 * Reject the command if we were asked to activate a schedule that was
5939 if (activate
&& *parameter_value
!= 0) {
5940 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5941 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5946 * Reject the command if we were asked to deactivate a schedule that was
5949 if (!activate
&& *parameter_value
== 0) {
5950 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5951 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5956 * Reject the command if we were asked to deactivate a schedule that
5959 if (!activate
&& *parameter_value
!= new_value
) {
5960 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5961 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5965 *parameter_value
= activate
? new_value
: 0;
5967 switch (schedule_type
) {
5968 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5969 if (activate
&& session
->active
) {
5971 * Only start the timer if the session is active,
5972 * otherwise it will be started when the session starts.
5974 ret
= timer_session_rotation_schedule_timer_start(
5975 session
, new_value
);
5977 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5978 ret
= LTTNG_ERR_UNK
;
5982 ret
= timer_session_rotation_schedule_timer_stop(
5985 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5986 ret
= LTTNG_ERR_UNK
;
5991 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5993 ret
= subscribe_session_consumed_size_rotation(session
,
5994 new_value
, notification_thread_handle
);
5996 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5997 ret
= LTTNG_ERR_UNK
;
6001 ret
= unsubscribe_session_consumed_size_rotation(session
,
6002 notification_thread_handle
);
6004 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
6005 ret
= LTTNG_ERR_UNK
;
6012 /* Would have been caught before. */
6024 /* Wait for a given path to be removed before continuing. */
6025 static enum lttng_error_code
wait_on_path(void *path_data
)
6027 const char *shm_path
= (const char *) path_data
;
6029 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6035 ret
= stat(shm_path
, &st
);
6037 if (errno
!= ENOENT
) {
6038 PERROR("stat() returned an error while checking for the existence of the shm path");
6040 DBG("shm path no longer exists, completing the destruction of session");
6044 if (!S_ISDIR(st
.st_mode
)) {
6045 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6050 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6056 * Returns a pointer to a handler to run on completion of a command.
6057 * Returns NULL if no handler has to be run for the last command executed.
6059 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6061 struct cmd_completion_handler
*handler
= current_completion_handler
;
6063 current_completion_handler
= NULL
;
6068 * Init command subsystem.
6073 * Set network sequence index to 1 for streams to match a relayd
6074 * socket on the consumer side.
6076 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6077 relayd_net_seq_idx
= 1;
6078 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6080 DBG("Command subsystem initialized");