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
15 #include <urcu/list.h>
16 #include <urcu/uatomic.h>
18 #include <common/buffer-view.h>
19 #include <common/common.h>
20 #include <common/compat/string.h>
21 #include <common/defaults.h>
22 #include <common/dynamic-buffer.h>
23 #include <common/kernel-ctl/kernel-ctl.h>
24 #include <common/relayd/relayd.h>
25 #include <common/sessiond-comm/sessiond-comm.h>
26 #include <common/string-utils/string-utils.h>
27 #include <common/trace-chunk.h>
28 #include <common/utils.h>
30 #include <lttng/action/action.h>
31 #include <lttng/action/action-internal.h>
32 #include <lttng/channel-internal.h>
33 #include <lttng/channel.h>
34 #include <lttng/condition/condition.h>
35 #include <lttng/error-query-internal.h>
36 #include <lttng/event-internal.h>
37 #include <lttng/location-internal.h>
38 #include <lttng/rotate-internal.h>
39 #include <lttng/session-descriptor-internal.h>
40 #include <lttng/session-internal.h>
41 #include <lttng/trigger/trigger-internal.h>
42 #include <lttng/userspace-probe-internal.h>
44 #include "agent-thread.h"
46 #include "buffer-registry.h"
50 #include "event-notifier-error-accounting.h"
52 #include "health-sessiond.h"
53 #include "kernel-consumer.h"
55 #include "lttng-sessiond.h"
56 #include "lttng-syscall.h"
57 #include "notification-thread-commands.h"
58 #include "notification-thread.h"
60 #include "rotation-thread.h"
66 /* Sleep for 100ms between each check for the shm path's deletion. */
67 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
69 struct cmd_destroy_session_reply_context
{
71 bool implicit_rotation_on_destroy
;
73 * Indicates whether or not an error occurred while launching the
74 * destruction of a session.
76 enum lttng_error_code destruction_status
;
79 static enum lttng_error_code
wait_on_path(void *path
);
82 * Command completion handler that is used by the destroy command
83 * when a session that has a non-default shm_path is being destroyed.
85 * See comment in cmd_destroy_session() for the rationale.
87 static struct destroy_completion_handler
{
88 struct cmd_completion_handler handler
;
89 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
90 } destroy_completion_handler
= {
93 .data
= destroy_completion_handler
.shm_path
98 static struct cmd_completion_handler
*current_completion_handler
;
101 * Used to keep a unique index for each relayd socket created where this value
102 * is associated with streams on the consumer so it can match the right relayd
103 * to send to. It must be accessed with the relayd_net_seq_idx_lock
106 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
107 static uint64_t relayd_net_seq_idx
;
109 static int validate_ust_event_name(const char *);
110 static int cmd_enable_event_internal(struct ltt_session
*session
,
111 const struct lttng_domain
*domain
,
112 char *channel_name
, struct lttng_event
*event
,
113 char *filter_expression
,
114 struct lttng_bytecode
*filter
,
115 struct lttng_event_exclusion
*exclusion
,
117 static int cmd_enable_channel_internal(struct ltt_session
*session
,
118 const struct lttng_domain
*domain
,
119 const struct lttng_channel
*_attr
,
123 * Create a session path used by list_lttng_sessions for the case that the
124 * session consumer is on the network.
126 static int build_network_session_path(char *dst
, size_t size
,
127 struct ltt_session
*session
)
129 int ret
, kdata_port
, udata_port
;
130 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
131 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
133 LTTNG_ASSERT(session
);
136 memset(tmp_urls
, 0, sizeof(tmp_urls
));
137 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
139 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
141 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
142 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
143 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
146 if (session
->ust_session
&& session
->ust_session
->consumer
) {
147 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
148 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
151 if (uuri
== NULL
&& kuri
== NULL
) {
152 uri
= &session
->consumer
->dst
.net
.control
;
153 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
154 } else if (kuri
&& uuri
) {
155 ret
= uri_compare(kuri
, uuri
);
159 /* Build uuri URL string */
160 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
167 } else if (kuri
&& uuri
== NULL
) {
169 } else if (uuri
&& kuri
== NULL
) {
173 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
179 * Do we have a UST url set. If yes, this means we have both kernel and UST
182 if (*tmp_uurl
!= '\0') {
183 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
184 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
187 if (kuri
|| (!kuri
&& !uuri
)) {
190 /* No kernel URI, use the UST port. */
193 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
201 * Get run-time attributes if the session has been started (discarded events,
204 static int get_kernel_runtime_stats(struct ltt_session
*session
,
205 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
206 uint64_t *lost_packets
)
210 if (!session
->has_been_started
) {
212 *discarded_events
= 0;
217 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
218 session
->kernel_session
->consumer
,
224 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
225 session
->kernel_session
->consumer
,
236 * Get run-time attributes if the session has been started (discarded events,
239 static int get_ust_runtime_stats(struct ltt_session
*session
,
240 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
241 uint64_t *lost_packets
)
244 struct ltt_ust_session
*usess
;
246 if (!discarded_events
|| !lost_packets
) {
251 usess
= session
->ust_session
;
252 LTTNG_ASSERT(discarded_events
);
253 LTTNG_ASSERT(lost_packets
);
255 if (!usess
|| !session
->has_been_started
) {
256 *discarded_events
= 0;
262 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
263 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
264 &usess
->buffer_reg_uid_list
,
265 usess
->consumer
, uchan
->id
,
266 uchan
->attr
.overwrite
,
269 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
270 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
271 uchan
, usess
->consumer
,
272 uchan
->attr
.overwrite
,
278 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
279 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
281 ERR("Unsupported buffer type");
292 * Create a list of agent domain events.
294 * Return number of events in list on success or else a negative value.
296 static enum lttng_error_code
list_lttng_agent_events(
297 struct agent
*agt
, struct lttng_payload
*reply_payload
,
298 unsigned int *nb_events
)
300 enum lttng_error_code ret_code
;
302 unsigned int local_nb_events
= 0;
303 struct agent_event
*event
;
304 struct lttng_ht_iter iter
;
305 unsigned long agent_event_count
;
308 assert(reply_payload
);
310 DBG3("Listing agent events");
313 agent_event_count
= lttng_ht_get_count(agt
->events
);
314 if (agent_event_count
== 0) {
319 if (agent_event_count
> UINT_MAX
) {
320 ret_code
= LTTNG_ERR_OVERFLOW
;
324 local_nb_events
= (unsigned int) agent_event_count
;
326 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
327 struct lttng_event
*tmp_event
= lttng_event_create();
330 ret_code
= LTTNG_ERR_NOMEM
;
334 if (lttng_strncpy(tmp_event
->name
, event
->name
, sizeof(tmp_event
->name
))) {
335 lttng_event_destroy(tmp_event
);
336 ret_code
= LTTNG_ERR_FATAL
;
340 tmp_event
->name
[sizeof(tmp_event
->name
) - 1] = '\0';
341 tmp_event
->enabled
= !!event
->enabled_count
;
342 tmp_event
->loglevel
= event
->loglevel_value
;
343 tmp_event
->loglevel_type
= event
->loglevel_type
;
345 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
346 event
->filter_expression
, 0, NULL
, reply_payload
);
347 lttng_event_destroy(tmp_event
);
349 ret_code
= LTTNG_ERR_FATAL
;
356 *nb_events
= local_nb_events
;
363 * Create a list of ust global domain events.
365 static enum lttng_error_code
list_lttng_ust_global_events(char *channel_name
,
366 struct ltt_ust_domain_global
*ust_global
,
367 struct lttng_payload
*reply_payload
,
368 unsigned int *nb_events
)
370 enum lttng_error_code ret_code
;
372 struct lttng_ht_iter iter
;
373 struct lttng_ht_node_str
*node
;
374 struct ltt_ust_channel
*uchan
;
375 struct ltt_ust_event
*uevent
;
376 unsigned long channel_event_count
;
377 unsigned int local_nb_events
= 0;
379 assert(reply_payload
);
382 DBG("Listing UST global events for channel %s", channel_name
);
386 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
387 node
= lttng_ht_iter_get_node_str(&iter
);
389 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
393 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
395 channel_event_count
= lttng_ht_get_count(uchan
->events
);
396 if (channel_event_count
== 0) {
402 if (channel_event_count
> UINT_MAX
) {
403 ret_code
= LTTNG_ERR_OVERFLOW
;
407 local_nb_events
= (unsigned int) channel_event_count
;
409 DBG3("Listing UST global %d events", *nb_events
);
411 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
412 struct lttng_event
*tmp_event
= NULL
;
414 if (uevent
->internal
) {
415 /* This event should remain hidden from clients */
420 tmp_event
= lttng_event_create();
422 ret_code
= LTTNG_ERR_NOMEM
;
426 if (lttng_strncpy(tmp_event
->name
, uevent
->attr
.name
,
427 LTTNG_SYMBOL_NAME_LEN
)) {
428 ret_code
= LTTNG_ERR_FATAL
;
429 lttng_event_destroy(tmp_event
);
433 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
434 tmp_event
->enabled
= uevent
->enabled
;
436 switch (uevent
->attr
.instrumentation
) {
437 case LTTNG_UST_ABI_TRACEPOINT
:
438 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
440 case LTTNG_UST_ABI_PROBE
:
441 tmp_event
->type
= LTTNG_EVENT_PROBE
;
443 case LTTNG_UST_ABI_FUNCTION
:
444 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
448 tmp_event
->loglevel
= uevent
->attr
.loglevel
;
449 switch (uevent
->attr
.loglevel_type
) {
450 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
451 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
453 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
454 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
456 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
457 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
460 if (uevent
->filter
) {
461 tmp_event
->filter
= 1;
463 if (uevent
->exclusion
) {
464 tmp_event
->exclusion
= 1;
468 * We do not care about the filter bytecode and the fd from the
469 * userspace_probe_location.
471 ret
= lttng_event_serialize(tmp_event
, uevent
->exclusion
? uevent
->exclusion
->count
: 0,
472 uevent
->exclusion
? (char **) uevent
->exclusion
->names
: NULL
,
473 uevent
->filter_expression
, 0, NULL
, reply_payload
);
474 lttng_event_destroy(tmp_event
);
476 ret_code
= LTTNG_ERR_FATAL
;
482 /* nb_events is already set at this point. */
484 *nb_events
= local_nb_events
;
491 * Fill lttng_event array of all kernel events in the channel.
493 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
494 struct ltt_kernel_session
*kernel_session
,
495 struct lttng_payload
*reply_payload
,
496 unsigned int *nb_events
)
498 enum lttng_error_code ret_code
;
500 struct ltt_kernel_event
*event
;
501 struct ltt_kernel_channel
*kchan
;
503 assert(reply_payload
);
505 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
507 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
511 *nb_events
= kchan
->event_count
;
513 DBG("Listing events for channel %s", kchan
->channel
->name
);
515 if (*nb_events
== 0) {
520 /* Kernel channels */
521 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
522 struct lttng_event
*tmp_event
= lttng_event_create();
525 ret_code
= LTTNG_ERR_NOMEM
;
529 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
530 lttng_event_destroy(tmp_event
);
531 ret_code
= LTTNG_ERR_FATAL
;
536 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
537 tmp_event
->enabled
= event
->enabled
;
538 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
540 switch (event
->event
->instrumentation
) {
541 case LTTNG_KERNEL_ABI_TRACEPOINT
:
542 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
544 case LTTNG_KERNEL_ABI_KRETPROBE
:
545 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
546 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
547 sizeof(struct lttng_kernel_abi_kprobe
));
549 case LTTNG_KERNEL_ABI_KPROBE
:
550 tmp_event
->type
= LTTNG_EVENT_PROBE
;
551 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
552 sizeof(struct lttng_kernel_abi_kprobe
));
554 case LTTNG_KERNEL_ABI_UPROBE
:
555 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
557 case LTTNG_KERNEL_ABI_FUNCTION
:
558 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
559 memcpy(&(tmp_event
->attr
.ftrace
), &event
->event
->u
.ftrace
,
560 sizeof(struct lttng_kernel_abi_function
));
562 case LTTNG_KERNEL_ABI_NOOP
:
563 tmp_event
->type
= LTTNG_EVENT_NOOP
;
565 case LTTNG_KERNEL_ABI_SYSCALL
:
566 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
568 case LTTNG_KERNEL_ABI_ALL
:
575 if (event
->userspace_probe_location
) {
576 struct lttng_userspace_probe_location
*location_copy
=
577 lttng_userspace_probe_location_copy(
578 event
->userspace_probe_location
);
580 if (!location_copy
) {
581 lttng_event_destroy(tmp_event
);
582 ret_code
= LTTNG_ERR_NOMEM
;
586 ret
= lttng_event_set_userspace_probe_location(
587 tmp_event
, location_copy
);
589 lttng_event_destroy(tmp_event
);
590 lttng_userspace_probe_location_destroy(
592 ret_code
= LTTNG_ERR_INVALID
;
597 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
598 event
->filter_expression
, 0, NULL
, reply_payload
);
599 lttng_event_destroy(tmp_event
);
601 ret_code
= LTTNG_ERR_FATAL
;
612 * Add URI so the consumer output object. Set the correct path depending on the
613 * domain adding the default trace directory.
615 static enum lttng_error_code
add_uri_to_consumer(
616 const struct ltt_session
*session
,
617 struct consumer_output
*consumer
,
618 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
621 enum lttng_error_code ret_code
= LTTNG_OK
;
625 if (consumer
== NULL
) {
626 DBG("No consumer detected. Don't add URI. Stopping.");
627 ret_code
= LTTNG_ERR_NO_CONSUMER
;
632 case LTTNG_DOMAIN_KERNEL
:
633 ret
= lttng_strncpy(consumer
->domain_subdir
,
634 DEFAULT_KERNEL_TRACE_DIR
,
635 sizeof(consumer
->domain_subdir
));
637 case LTTNG_DOMAIN_UST
:
638 ret
= lttng_strncpy(consumer
->domain_subdir
,
639 DEFAULT_UST_TRACE_DIR
,
640 sizeof(consumer
->domain_subdir
));
644 * This case is possible is we try to add the URI to the global
645 * tracing session consumer object which in this case there is
648 memset(consumer
->domain_subdir
, 0,
649 sizeof(consumer
->domain_subdir
));
653 ERR("Failed to initialize consumer output domain subdirectory");
654 ret_code
= LTTNG_ERR_FATAL
;
658 switch (uri
->dtype
) {
661 DBG2("Setting network URI to consumer");
663 if (consumer
->type
== CONSUMER_DST_NET
) {
664 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
665 consumer
->dst
.net
.control_isset
) ||
666 (uri
->stype
== LTTNG_STREAM_DATA
&&
667 consumer
->dst
.net
.data_isset
)) {
668 ret_code
= LTTNG_ERR_URL_EXIST
;
672 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
675 /* Set URI into consumer output object */
676 ret
= consumer_set_network_uri(session
, consumer
, uri
);
678 ret_code
= (lttng_error_code
) -ret
;
680 } else if (ret
== 1) {
682 * URI was the same in the consumer so we do not append the subdir
683 * again so to not duplicate output dir.
690 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
691 ret_code
= LTTNG_ERR_INVALID
;
694 DBG2("Setting trace directory path from URI to %s",
696 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
698 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
700 sizeof(consumer
->dst
.session_root_path
));
702 ret_code
= LTTNG_ERR_FATAL
;
705 consumer
->type
= CONSUMER_DST_LOCAL
;
715 * Init tracing by creating trace directory and sending fds kernel consumer.
717 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
720 struct lttng_ht_iter iter
;
721 struct consumer_socket
*socket
;
723 LTTNG_ASSERT(session
);
727 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
728 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
730 pthread_mutex_lock(socket
->lock
);
731 ret
= kernel_consumer_send_session(socket
, session
);
732 pthread_mutex_unlock(socket
->lock
);
734 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
746 * Create a socket to the relayd using the URI.
748 * On success, the relayd_sock pointer is set to the created socket.
749 * Else, it remains untouched and an LTTng error code is returned.
751 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
752 struct lttcomm_relayd_sock
**relayd_sock
,
753 struct consumer_output
*consumer
)
756 enum lttng_error_code status
= LTTNG_OK
;
757 struct lttcomm_relayd_sock
*rsock
;
759 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
760 RELAYD_VERSION_COMM_MINOR
);
762 status
= LTTNG_ERR_FATAL
;
767 * Connect to relayd so we can proceed with a session creation. This call
768 * can possibly block for an arbitrary amount of time to set the health
769 * state to be in poll execution.
772 ret
= relayd_connect(rsock
);
775 ERR("Unable to reach lttng-relayd");
776 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
780 /* Create socket for control stream. */
781 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
782 uint64_t result_flags
;
784 DBG3("Creating relayd stream socket from URI");
786 /* Check relayd version */
787 ret
= relayd_version_check(rsock
);
788 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
789 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
791 } else if (ret
< 0) {
792 ERR("Unable to reach lttng-relayd");
793 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
796 consumer
->relay_major_version
= rsock
->major
;
797 consumer
->relay_minor_version
= rsock
->minor
;
798 ret
= relayd_get_configuration(rsock
, 0,
801 ERR("Unable to get relayd configuration");
802 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
805 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
806 consumer
->relay_allows_clear
= true;
808 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
809 DBG3("Creating relayd data socket from URI");
811 /* Command is not valid */
812 ERR("Relayd invalid stream type: %d", uri
->stype
);
813 status
= LTTNG_ERR_INVALID
;
817 *relayd_sock
= rsock
;
822 /* The returned value is not useful since we are on an error path. */
823 (void) relayd_close(rsock
);
831 * Connect to the relayd using URI and send the socket to the right consumer.
833 * The consumer socket lock must be held by the caller.
835 * Returns LTTNG_OK on success or an LTTng error code on failure.
837 static enum lttng_error_code
send_consumer_relayd_socket(
838 unsigned int session_id
,
839 struct lttng_uri
*relayd_uri
,
840 struct consumer_output
*consumer
,
841 struct consumer_socket
*consumer_sock
,
842 const char *session_name
, const char *hostname
,
843 const char *base_path
, int session_live_timer
,
844 const uint64_t *current_chunk_id
,
845 time_t session_creation_time
,
846 bool session_name_contains_creation_time
)
849 struct lttcomm_relayd_sock
*rsock
= NULL
;
850 enum lttng_error_code status
;
852 /* Connect to relayd and make version check if uri is the control. */
853 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
854 if (status
!= LTTNG_OK
) {
855 goto relayd_comm_error
;
859 /* Set the network sequence index if not set. */
860 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
861 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
863 * Increment net_seq_idx because we are about to transfer the
864 * new relayd socket to the consumer.
865 * Assign unique key so the consumer can match streams.
867 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
868 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
871 /* Send relayd socket to consumer. */
872 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
873 relayd_uri
->stype
, session_id
,
874 session_name
, hostname
, base_path
,
875 session_live_timer
, current_chunk_id
,
876 session_creation_time
, session_name_contains_creation_time
);
878 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
882 /* Flag that the corresponding socket was sent. */
883 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
884 consumer_sock
->control_sock_sent
= 1;
885 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
886 consumer_sock
->data_sock_sent
= 1;
890 * Close socket which was dup on the consumer side. The session daemon does
891 * NOT keep track of the relayd socket(s) once transfer to the consumer.
895 if (status
!= LTTNG_OK
) {
897 * The consumer output for this session should not be used anymore
898 * since the relayd connection failed thus making any tracing or/and
899 * streaming not usable.
901 consumer
->enabled
= 0;
903 (void) relayd_close(rsock
);
911 * Send both relayd sockets to a specific consumer and domain. This is a
912 * helper function to facilitate sending the information to the consumer for a
915 * The consumer socket lock must be held by the caller.
917 * Returns LTTNG_OK, or an LTTng error code on failure.
919 static enum lttng_error_code
send_consumer_relayd_sockets(
920 unsigned int session_id
, struct consumer_output
*consumer
,
921 struct consumer_socket
*sock
, const char *session_name
,
922 const char *hostname
, const char *base_path
, int session_live_timer
,
923 const uint64_t *current_chunk_id
, time_t session_creation_time
,
924 bool session_name_contains_creation_time
)
926 enum lttng_error_code status
= LTTNG_OK
;
928 LTTNG_ASSERT(consumer
);
931 /* Sending control relayd socket. */
932 if (!sock
->control_sock_sent
) {
933 status
= send_consumer_relayd_socket(session_id
,
934 &consumer
->dst
.net
.control
, consumer
, sock
,
935 session_name
, hostname
, base_path
, session_live_timer
,
936 current_chunk_id
, session_creation_time
,
937 session_name_contains_creation_time
);
938 if (status
!= LTTNG_OK
) {
943 /* Sending data relayd socket. */
944 if (!sock
->data_sock_sent
) {
945 status
= send_consumer_relayd_socket(session_id
,
946 &consumer
->dst
.net
.data
, consumer
, sock
,
947 session_name
, hostname
, base_path
, session_live_timer
,
948 current_chunk_id
, session_creation_time
,
949 session_name_contains_creation_time
);
950 if (status
!= LTTNG_OK
) {
960 * Setup relayd connections for a tracing session. First creates the socket to
961 * the relayd and send them to the right domain consumer. Consumer type MUST be
964 int cmd_setup_relayd(struct ltt_session
*session
)
967 struct ltt_ust_session
*usess
;
968 struct ltt_kernel_session
*ksess
;
969 struct consumer_socket
*socket
;
970 struct lttng_ht_iter iter
;
971 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
973 LTTNG_ASSERT(session
);
975 usess
= session
->ust_session
;
976 ksess
= session
->kernel_session
;
978 DBG("Setting relayd for session %s", session
->name
);
981 if (session
->current_trace_chunk
) {
982 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
983 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
985 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
986 current_chunk_id
.is_set
= true;
988 ERR("Failed to get current trace chunk id");
994 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
995 && usess
->consumer
->enabled
) {
996 /* For each consumer socket, send relayd sockets */
997 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
999 pthread_mutex_lock(socket
->lock
);
1000 ret
= send_consumer_relayd_sockets(session
->id
,
1001 usess
->consumer
, socket
,
1002 session
->name
, session
->hostname
,
1004 session
->live_timer
,
1005 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1006 session
->creation_time
,
1007 session
->name_contains_creation_time
);
1008 pthread_mutex_unlock(socket
->lock
);
1009 if (ret
!= LTTNG_OK
) {
1012 /* Session is now ready for network streaming. */
1013 session
->net_handle
= 1;
1015 session
->consumer
->relay_major_version
=
1016 usess
->consumer
->relay_major_version
;
1017 session
->consumer
->relay_minor_version
=
1018 usess
->consumer
->relay_minor_version
;
1019 session
->consumer
->relay_allows_clear
=
1020 usess
->consumer
->relay_allows_clear
;
1023 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1024 && ksess
->consumer
->enabled
) {
1025 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1026 socket
, node
.node
) {
1027 pthread_mutex_lock(socket
->lock
);
1028 ret
= send_consumer_relayd_sockets(session
->id
,
1029 ksess
->consumer
, socket
,
1030 session
->name
, session
->hostname
,
1032 session
->live_timer
,
1033 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1034 session
->creation_time
,
1035 session
->name_contains_creation_time
);
1036 pthread_mutex_unlock(socket
->lock
);
1037 if (ret
!= LTTNG_OK
) {
1040 /* Session is now ready for network streaming. */
1041 session
->net_handle
= 1;
1043 session
->consumer
->relay_major_version
=
1044 ksess
->consumer
->relay_major_version
;
1045 session
->consumer
->relay_minor_version
=
1046 ksess
->consumer
->relay_minor_version
;
1047 session
->consumer
->relay_allows_clear
=
1048 ksess
->consumer
->relay_allows_clear
;
1057 * Start a kernel session by opening all necessary streams.
1059 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1062 struct ltt_kernel_channel
*kchan
;
1064 /* Open kernel metadata */
1065 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1066 ret
= kernel_open_metadata(ksess
);
1068 ret
= LTTNG_ERR_KERN_META_FAIL
;
1073 /* Open kernel metadata stream */
1074 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1075 ret
= kernel_open_metadata_stream(ksess
);
1077 ERR("Kernel create metadata stream failed");
1078 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1083 /* For each channel */
1084 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1085 if (kchan
->stream_count
== 0) {
1086 ret
= kernel_open_channel_stream(kchan
);
1088 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1091 /* Update the stream global counter */
1092 ksess
->stream_count_global
+= ret
;
1096 /* Setup kernel consumer socket and send fds to it */
1097 ret
= init_kernel_tracing(ksess
);
1099 ret
= LTTNG_ERR_KERN_START_FAIL
;
1103 /* This start the kernel tracing */
1104 ret
= kernel_start_session(ksess
);
1106 ret
= LTTNG_ERR_KERN_START_FAIL
;
1110 /* Quiescent wait after starting trace */
1111 kernel_wait_quiescent();
1121 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1123 struct ltt_kernel_channel
*kchan
;
1124 bool error_occurred
= false;
1127 if (!ksess
|| !ksess
->active
) {
1130 DBG("Stopping kernel tracing");
1132 ret
= kernel_stop_session(ksess
);
1134 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1138 kernel_wait_quiescent();
1140 /* Flush metadata after stopping (if exists) */
1141 if (ksess
->metadata_stream_fd
>= 0) {
1142 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1144 ERR("Kernel metadata flush failed");
1145 error_occurred
= true;
1149 /* Flush all buffers after stopping */
1150 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1151 ret
= kernel_flush_buffer(kchan
);
1153 ERR("Kernel flush buffer error");
1154 error_occurred
= true;
1159 if (error_occurred
) {
1160 ret
= LTTNG_ERR_UNK
;
1169 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1171 int cmd_disable_channel(struct ltt_session
*session
,
1172 enum lttng_domain_type domain
, char *channel_name
)
1175 struct ltt_ust_session
*usess
;
1177 usess
= session
->ust_session
;
1182 case LTTNG_DOMAIN_KERNEL
:
1184 ret
= channel_kernel_disable(session
->kernel_session
,
1186 if (ret
!= LTTNG_OK
) {
1190 kernel_wait_quiescent();
1193 case LTTNG_DOMAIN_UST
:
1195 struct ltt_ust_channel
*uchan
;
1196 struct lttng_ht
*chan_ht
;
1198 chan_ht
= usess
->domain_global
.channels
;
1200 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1201 if (uchan
== NULL
) {
1202 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1206 ret
= channel_ust_disable(usess
, uchan
);
1207 if (ret
!= LTTNG_OK
) {
1213 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1225 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1227 * The wpipe arguments is used as a notifier for the kernel thread.
1229 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1233 ssize_t sock_recv_len
;
1234 struct lttng_channel
*channel
= NULL
;
1235 struct lttng_buffer_view view
;
1236 struct lttng_dynamic_buffer channel_buffer
;
1237 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1239 lttng_dynamic_buffer_init(&channel_buffer
);
1240 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1241 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1243 ret
= LTTNG_ERR_NOMEM
;
1247 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1249 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1250 ERR("Failed to receive \"enable channel\" command payload");
1251 ret
= LTTNG_ERR_INVALID
;
1255 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1256 if (!lttng_buffer_view_is_valid(&view
)) {
1257 ret
= LTTNG_ERR_INVALID
;
1261 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1262 ERR("Invalid channel payload received in \"enable channel\" command");
1263 ret
= LTTNG_ERR_INVALID
;
1267 ret
= cmd_enable_channel_internal(
1268 cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1271 lttng_dynamic_buffer_reset(&channel_buffer
);
1272 lttng_channel_destroy(channel
);
1276 static int cmd_enable_channel_internal(struct ltt_session
*session
,
1277 const struct lttng_domain
*domain
,
1278 const struct lttng_channel
*_attr
,
1282 struct ltt_ust_session
*usess
= session
->ust_session
;
1283 struct lttng_ht
*chan_ht
;
1285 struct lttng_channel
*attr
= NULL
;
1287 LTTNG_ASSERT(session
);
1288 LTTNG_ASSERT(_attr
);
1289 LTTNG_ASSERT(domain
);
1291 attr
= lttng_channel_copy(_attr
);
1293 ret
= -LTTNG_ERR_NOMEM
;
1297 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1299 /* Validate channel name */
1300 if (attr
->name
[0] == '.' ||
1301 memchr(attr
->name
, '/', len
) != NULL
) {
1302 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1306 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1311 * If the session is a live session, remove the switch timer, the
1312 * live timer does the same thing but sends also synchronisation
1313 * beacons for inactive streams.
1315 if (session
->live_timer
> 0) {
1316 attr
->attr
.live_timer_interval
= session
->live_timer
;
1317 attr
->attr
.switch_timer_interval
= 0;
1320 /* Check for feature support */
1321 switch (domain
->type
) {
1322 case LTTNG_DOMAIN_KERNEL
:
1324 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1325 /* Sampling position of buffer is not supported */
1326 WARN("Kernel tracer does not support buffer monitoring. "
1327 "Setting the monitor interval timer to 0 "
1328 "(disabled) for channel '%s' of session '%s'",
1329 attr
->name
, session
->name
);
1330 lttng_channel_set_monitor_timer_interval(attr
, 0);
1334 case LTTNG_DOMAIN_UST
:
1336 case LTTNG_DOMAIN_JUL
:
1337 case LTTNG_DOMAIN_LOG4J
:
1338 case LTTNG_DOMAIN_PYTHON
:
1339 if (!agent_tracing_is_enabled()) {
1340 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1341 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1346 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1350 switch (domain
->type
) {
1351 case LTTNG_DOMAIN_KERNEL
:
1353 struct ltt_kernel_channel
*kchan
;
1355 kchan
= trace_kernel_get_channel_by_name(
1356 attr
->name
, session
->kernel_session
);
1357 if (kchan
== NULL
) {
1359 * Don't try to create a channel if the session has been started at
1360 * some point in time before. The tracer does not allow it.
1362 if (session
->has_been_started
) {
1363 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1367 if (session
->snapshot
.nb_output
> 0 ||
1368 session
->snapshot_mode
) {
1369 /* Enforce mmap output for snapshot sessions. */
1370 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1372 ret
= channel_kernel_create(
1373 session
->kernel_session
, attr
, wpipe
);
1374 if (attr
->name
[0] != '\0') {
1375 session
->kernel_session
->has_non_default_channel
= 1;
1378 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1381 if (ret
!= LTTNG_OK
) {
1385 kernel_wait_quiescent();
1388 case LTTNG_DOMAIN_UST
:
1389 case LTTNG_DOMAIN_JUL
:
1390 case LTTNG_DOMAIN_LOG4J
:
1391 case LTTNG_DOMAIN_PYTHON
:
1393 struct ltt_ust_channel
*uchan
;
1398 * Current agent implementation limitations force us to allow
1399 * only one channel at once in "agent" subdomains. Each
1400 * subdomain has a default channel name which must be strictly
1403 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1404 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1405 LTTNG_SYMBOL_NAME_LEN
)) {
1406 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1409 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1410 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1411 LTTNG_SYMBOL_NAME_LEN
)) {
1412 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1415 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1416 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1417 LTTNG_SYMBOL_NAME_LEN
)) {
1418 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1423 chan_ht
= usess
->domain_global
.channels
;
1425 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1426 if (uchan
== NULL
) {
1428 * Don't try to create a channel if the session has been started at
1429 * some point in time before. The tracer does not allow it.
1431 if (session
->has_been_started
) {
1432 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1436 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1437 if (attr
->name
[0] != '\0') {
1438 usess
->has_non_default_channel
= 1;
1441 ret
= channel_ust_enable(usess
, uchan
);
1446 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1450 if (ret
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1451 session
->has_non_mmap_channel
= true;
1456 lttng_channel_destroy(attr
);
1460 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1461 struct ltt_session
*session
,
1462 enum lttng_domain_type domain
,
1463 enum lttng_process_attr process_attr
,
1464 enum lttng_tracking_policy
*policy
)
1466 enum lttng_error_code ret_code
= LTTNG_OK
;
1467 const struct process_attr_tracker
*tracker
;
1470 case LTTNG_DOMAIN_KERNEL
:
1471 if (!session
->kernel_session
) {
1472 ret_code
= LTTNG_ERR_INVALID
;
1475 tracker
= kernel_get_process_attr_tracker(
1476 session
->kernel_session
, process_attr
);
1478 case LTTNG_DOMAIN_UST
:
1479 if (!session
->ust_session
) {
1480 ret_code
= LTTNG_ERR_INVALID
;
1483 tracker
= trace_ust_get_process_attr_tracker(
1484 session
->ust_session
, process_attr
);
1487 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1491 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1493 ret_code
= LTTNG_ERR_INVALID
;
1499 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1500 struct ltt_session
*session
,
1501 enum lttng_domain_type domain
,
1502 enum lttng_process_attr process_attr
,
1503 enum lttng_tracking_policy policy
)
1505 enum lttng_error_code ret_code
= LTTNG_OK
;
1508 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1509 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1510 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1513 ret_code
= LTTNG_ERR_INVALID
;
1518 case LTTNG_DOMAIN_KERNEL
:
1519 if (!session
->kernel_session
) {
1520 ret_code
= LTTNG_ERR_INVALID
;
1523 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1524 session
->kernel_session
, process_attr
, policy
);
1526 case LTTNG_DOMAIN_UST
:
1527 if (!session
->ust_session
) {
1528 ret_code
= LTTNG_ERR_INVALID
;
1531 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1532 session
->ust_session
, process_attr
, policy
);
1535 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1542 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1543 struct ltt_session
*session
,
1544 enum lttng_domain_type domain
,
1545 enum lttng_process_attr process_attr
,
1546 const struct process_attr_value
*value
)
1548 enum lttng_error_code ret_code
= LTTNG_OK
;
1551 case LTTNG_DOMAIN_KERNEL
:
1552 if (!session
->kernel_session
) {
1553 ret_code
= LTTNG_ERR_INVALID
;
1556 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1557 session
->kernel_session
, process_attr
, value
);
1559 case LTTNG_DOMAIN_UST
:
1560 if (!session
->ust_session
) {
1561 ret_code
= LTTNG_ERR_INVALID
;
1564 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1565 session
->ust_session
, process_attr
, value
);
1568 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1575 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1576 struct ltt_session
*session
,
1577 enum lttng_domain_type domain
,
1578 enum lttng_process_attr process_attr
,
1579 const struct process_attr_value
*value
)
1581 enum lttng_error_code ret_code
= LTTNG_OK
;
1584 case LTTNG_DOMAIN_KERNEL
:
1585 if (!session
->kernel_session
) {
1586 ret_code
= LTTNG_ERR_INVALID
;
1589 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1590 session
->kernel_session
, process_attr
, value
);
1592 case LTTNG_DOMAIN_UST
:
1593 if (!session
->ust_session
) {
1594 ret_code
= LTTNG_ERR_INVALID
;
1597 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1598 session
->ust_session
, process_attr
, value
);
1601 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1608 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1609 struct ltt_session
*session
,
1610 enum lttng_domain_type domain
,
1611 enum lttng_process_attr process_attr
,
1612 struct lttng_process_attr_values
**values
)
1614 enum lttng_error_code ret_code
= LTTNG_OK
;
1615 const struct process_attr_tracker
*tracker
;
1616 enum process_attr_tracker_status status
;
1619 case LTTNG_DOMAIN_KERNEL
:
1620 if (!session
->kernel_session
) {
1621 ret_code
= LTTNG_ERR_INVALID
;
1624 tracker
= kernel_get_process_attr_tracker(
1625 session
->kernel_session
, process_attr
);
1627 case LTTNG_DOMAIN_UST
:
1628 if (!session
->ust_session
) {
1629 ret_code
= LTTNG_ERR_INVALID
;
1632 tracker
= trace_ust_get_process_attr_tracker(
1633 session
->ust_session
, process_attr
);
1636 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1641 ret_code
= LTTNG_ERR_INVALID
;
1645 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1647 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1648 ret_code
= LTTNG_OK
;
1650 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1651 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1653 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1654 ret_code
= LTTNG_ERR_NOMEM
;
1657 ret_code
= LTTNG_ERR_UNK
;
1666 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1668 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1669 struct lttng_event
*event
,
1670 char *filter_expression
,
1671 struct lttng_bytecode
*bytecode
,
1672 struct lttng_event_exclusion
*exclusion
)
1675 const char *event_name
;
1676 const struct ltt_session
*session
= cmd_ctx
->session
;
1677 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1678 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1680 DBG("Disable event command for event \'%s\'", event
->name
);
1683 * Filter and exclusions are simply not handled by the
1684 * disable event command at this time.
1688 (void) filter_expression
;
1691 /* Ignore the presence of filter or exclusion for the event */
1693 event
->exclusion
= 0;
1695 event_name
= event
->name
;
1697 /* Error out on unhandled search criteria */
1698 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1699 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1700 ret
= LTTNG_ERR_UNK
;
1707 case LTTNG_DOMAIN_KERNEL
:
1709 struct ltt_kernel_channel
*kchan
;
1710 struct ltt_kernel_session
*ksess
;
1712 ksess
= session
->kernel_session
;
1715 * If a non-default channel has been created in the
1716 * session, explicitely require that -c chan_name needs
1719 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1720 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1724 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1725 if (kchan
== NULL
) {
1726 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1730 switch (event
->type
) {
1731 case LTTNG_EVENT_ALL
:
1732 case LTTNG_EVENT_TRACEPOINT
:
1733 case LTTNG_EVENT_SYSCALL
:
1734 case LTTNG_EVENT_PROBE
:
1735 case LTTNG_EVENT_FUNCTION
:
1736 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1737 if (event_name
[0] == '\0') {
1738 ret
= event_kernel_disable_event(kchan
,
1741 ret
= event_kernel_disable_event(kchan
,
1742 event_name
, event
->type
);
1744 if (ret
!= LTTNG_OK
) {
1749 ret
= LTTNG_ERR_UNK
;
1753 kernel_wait_quiescent();
1756 case LTTNG_DOMAIN_UST
:
1758 struct ltt_ust_channel
*uchan
;
1759 struct ltt_ust_session
*usess
;
1761 usess
= session
->ust_session
;
1763 if (validate_ust_event_name(event_name
)) {
1764 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1769 * If a non-default channel has been created in the
1770 * session, explicitly require that -c chan_name needs
1773 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1774 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1778 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1780 if (uchan
== NULL
) {
1781 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1785 switch (event
->type
) {
1786 case LTTNG_EVENT_ALL
:
1788 * An empty event name means that everything
1789 * should be disabled.
1791 if (event
->name
[0] == '\0') {
1792 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1794 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1797 if (ret
!= LTTNG_OK
) {
1802 ret
= LTTNG_ERR_UNK
;
1806 DBG3("Disable UST event %s in channel %s completed", event_name
,
1810 case LTTNG_DOMAIN_LOG4J
:
1811 case LTTNG_DOMAIN_JUL
:
1812 case LTTNG_DOMAIN_PYTHON
:
1815 struct ltt_ust_session
*usess
= session
->ust_session
;
1817 LTTNG_ASSERT(usess
);
1819 switch (event
->type
) {
1820 case LTTNG_EVENT_ALL
:
1823 ret
= LTTNG_ERR_UNK
;
1827 agt
= trace_ust_find_agent(usess
, domain
);
1829 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1833 * An empty event name means that everything
1834 * should be disabled.
1836 if (event
->name
[0] == '\0') {
1837 ret
= event_agent_disable_all(usess
, agt
);
1839 ret
= event_agent_disable(usess
, agt
, event_name
);
1841 if (ret
!= LTTNG_OK
) {
1848 ret
= LTTNG_ERR_UND
;
1859 free(filter_expression
);
1864 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1866 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1867 const struct lttng_event_context
*event_context
, int kwpipe
)
1869 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1870 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1871 const struct ltt_session
*session
= cmd_ctx
->session
;
1872 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1875 * Don't try to add a context if the session has been started at
1876 * some point in time before. The tracer does not allow it and would
1877 * result in a corrupted trace.
1879 if (cmd_ctx
->session
->has_been_started
) {
1880 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1885 case LTTNG_DOMAIN_KERNEL
:
1886 LTTNG_ASSERT(session
->kernel_session
);
1888 if (session
->kernel_session
->channel_count
== 0) {
1889 /* Create default channel */
1890 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1891 if (ret
!= LTTNG_OK
) {
1894 chan_kern_created
= 1;
1896 /* Add kernel context to kernel tracer */
1897 ret
= context_kernel_add(session
->kernel_session
,
1898 event_context
, channel_name
);
1899 if (ret
!= LTTNG_OK
) {
1903 case LTTNG_DOMAIN_JUL
:
1904 case LTTNG_DOMAIN_LOG4J
:
1907 * Validate channel name.
1908 * If no channel name is given and the domain is JUL or LOG4J,
1909 * set it to the appropriate domain-specific channel name. If
1910 * a name is provided but does not match the expexted channel
1911 * name, return an error.
1913 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1914 strcmp(channel_name
,
1915 DEFAULT_JUL_CHANNEL_NAME
)) {
1916 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1918 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1919 strcmp(channel_name
,
1920 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1921 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1926 case LTTNG_DOMAIN_UST
:
1928 struct ltt_ust_session
*usess
= session
->ust_session
;
1929 unsigned int chan_count
;
1931 LTTNG_ASSERT(usess
);
1933 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1934 if (chan_count
== 0) {
1935 struct lttng_channel
*attr
;
1936 /* Create default channel */
1937 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1939 ret
= LTTNG_ERR_FATAL
;
1943 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1944 if (ret
!= LTTNG_OK
) {
1948 channel_attr_destroy(attr
);
1949 chan_ust_created
= 1;
1952 ret
= context_ust_add(usess
, domain
, event_context
,
1954 if (ret
!= LTTNG_OK
) {
1960 ret
= LTTNG_ERR_UND
;
1968 if (chan_kern_created
) {
1969 struct ltt_kernel_channel
*kchan
=
1970 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1971 session
->kernel_session
);
1972 /* Created previously, this should NOT fail. */
1973 LTTNG_ASSERT(kchan
);
1974 kernel_destroy_channel(kchan
);
1977 if (chan_ust_created
) {
1978 struct ltt_ust_channel
*uchan
=
1979 trace_ust_find_channel_by_name(
1980 session
->ust_session
->domain_global
.channels
,
1981 DEFAULT_CHANNEL_NAME
);
1982 /* Created previously, this should NOT fail. */
1983 LTTNG_ASSERT(uchan
);
1984 /* Remove from the channel list of the session. */
1985 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1987 trace_ust_destroy_channel(uchan
);
1993 static inline bool name_starts_with(const char *name
, const char *prefix
)
1995 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
1997 return !strncmp(name
, prefix
, max_cmp_len
);
2000 /* Perform userspace-specific event name validation */
2001 static int validate_ust_event_name(const char *name
)
2011 * Check name against all internal UST event component namespaces used
2014 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2015 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2016 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2025 * Internal version of cmd_enable_event() with a supplemental
2026 * "internal_event" flag which is used to enable internal events which should
2027 * be hidden from clients. Such events are used in the agent implementation to
2028 * enable the events through which all "agent" events are funeled.
2030 static int _cmd_enable_event(struct ltt_session
*session
,
2031 const struct lttng_domain
*domain
,
2032 char *channel_name
, struct lttng_event
*event
,
2033 char *filter_expression
,
2034 struct lttng_bytecode
*filter
,
2035 struct lttng_event_exclusion
*exclusion
,
2036 int wpipe
, bool internal_event
)
2038 int ret
= 0, channel_created
= 0;
2039 struct lttng_channel
*attr
= NULL
;
2041 LTTNG_ASSERT(session
);
2042 LTTNG_ASSERT(event
);
2043 LTTNG_ASSERT(channel_name
);
2045 /* If we have a filter, we must have its filter expression */
2046 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2048 /* Normalize event name as a globbing pattern */
2049 strutils_normalize_star_glob_pattern(event
->name
);
2051 /* Normalize exclusion names as globbing patterns */
2055 for (i
= 0; i
< exclusion
->count
; i
++) {
2056 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2058 strutils_normalize_star_glob_pattern(name
);
2062 DBG("Enable event command for event \'%s\'", event
->name
);
2066 switch (domain
->type
) {
2067 case LTTNG_DOMAIN_KERNEL
:
2069 struct ltt_kernel_channel
*kchan
;
2072 * If a non-default channel has been created in the
2073 * session, explicitely require that -c chan_name needs
2076 if (session
->kernel_session
->has_non_default_channel
2077 && channel_name
[0] == '\0') {
2078 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2082 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2083 session
->kernel_session
);
2084 if (kchan
== NULL
) {
2085 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2086 LTTNG_BUFFER_GLOBAL
);
2088 ret
= LTTNG_ERR_FATAL
;
2091 if (lttng_strncpy(attr
->name
, channel_name
,
2092 sizeof(attr
->name
))) {
2093 ret
= LTTNG_ERR_INVALID
;
2097 ret
= cmd_enable_channel_internal(
2098 session
, domain
, attr
, wpipe
);
2099 if (ret
!= LTTNG_OK
) {
2102 channel_created
= 1;
2105 /* Get the newly created kernel channel pointer */
2106 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2107 session
->kernel_session
);
2108 if (kchan
== NULL
) {
2109 /* This sould not happen... */
2110 ret
= LTTNG_ERR_FATAL
;
2114 switch (event
->type
) {
2115 case LTTNG_EVENT_ALL
:
2117 char *filter_expression_a
= NULL
;
2118 struct lttng_bytecode
*filter_a
= NULL
;
2121 * We need to duplicate filter_expression and filter,
2122 * because ownership is passed to first enable
2125 if (filter_expression
) {
2126 filter_expression_a
= strdup(filter_expression
);
2127 if (!filter_expression_a
) {
2128 ret
= LTTNG_ERR_FATAL
;
2133 filter_a
= (lttng_bytecode
*) zmalloc(sizeof(*filter_a
) + filter
->len
);
2135 free(filter_expression_a
);
2136 ret
= LTTNG_ERR_FATAL
;
2139 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2141 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2142 ret
= event_kernel_enable_event(kchan
, event
,
2143 filter_expression
, filter
);
2144 /* We have passed ownership */
2145 filter_expression
= NULL
;
2147 if (ret
!= LTTNG_OK
) {
2148 if (channel_created
) {
2149 /* Let's not leak a useless channel. */
2150 kernel_destroy_channel(kchan
);
2152 free(filter_expression_a
);
2156 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2157 ret
= event_kernel_enable_event(kchan
, event
,
2158 filter_expression_a
, filter_a
);
2159 /* We have passed ownership */
2160 filter_expression_a
= NULL
;
2162 if (ret
!= LTTNG_OK
) {
2167 case LTTNG_EVENT_PROBE
:
2168 case LTTNG_EVENT_USERSPACE_PROBE
:
2169 case LTTNG_EVENT_FUNCTION
:
2170 case LTTNG_EVENT_FUNCTION_ENTRY
:
2171 case LTTNG_EVENT_TRACEPOINT
:
2172 ret
= event_kernel_enable_event(kchan
, event
,
2173 filter_expression
, filter
);
2174 /* We have passed ownership */
2175 filter_expression
= NULL
;
2177 if (ret
!= LTTNG_OK
) {
2178 if (channel_created
) {
2179 /* Let's not leak a useless channel. */
2180 kernel_destroy_channel(kchan
);
2185 case LTTNG_EVENT_SYSCALL
:
2186 ret
= event_kernel_enable_event(kchan
, event
,
2187 filter_expression
, filter
);
2188 /* We have passed ownership */
2189 filter_expression
= NULL
;
2191 if (ret
!= LTTNG_OK
) {
2196 ret
= LTTNG_ERR_UNK
;
2200 kernel_wait_quiescent();
2203 case LTTNG_DOMAIN_UST
:
2205 struct ltt_ust_channel
*uchan
;
2206 struct ltt_ust_session
*usess
= session
->ust_session
;
2208 LTTNG_ASSERT(usess
);
2211 * If a non-default channel has been created in the
2212 * session, explicitely require that -c chan_name needs
2215 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2216 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2220 /* Get channel from global UST domain */
2221 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2223 if (uchan
== NULL
) {
2224 /* Create default channel */
2225 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2226 usess
->buffer_type
);
2228 ret
= LTTNG_ERR_FATAL
;
2231 if (lttng_strncpy(attr
->name
, channel_name
,
2232 sizeof(attr
->name
))) {
2233 ret
= LTTNG_ERR_INVALID
;
2237 ret
= cmd_enable_channel_internal(
2238 session
, domain
, attr
, wpipe
);
2239 if (ret
!= LTTNG_OK
) {
2243 /* Get the newly created channel reference back */
2244 uchan
= trace_ust_find_channel_by_name(
2245 usess
->domain_global
.channels
, channel_name
);
2246 LTTNG_ASSERT(uchan
);
2249 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2251 * Don't allow users to add UST events to channels which
2252 * are assigned to a userspace subdomain (JUL, Log4J,
2255 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2259 if (!internal_event
) {
2261 * Ensure the event name is not reserved for internal
2264 ret
= validate_ust_event_name(event
->name
);
2266 WARN("Userspace event name %s failed validation.",
2268 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2273 /* At this point, the session and channel exist on the tracer */
2274 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2275 filter_expression
, filter
, exclusion
,
2277 /* We have passed ownership */
2278 filter_expression
= NULL
;
2281 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2282 goto already_enabled
;
2283 } else if (ret
!= LTTNG_OK
) {
2288 case LTTNG_DOMAIN_LOG4J
:
2289 case LTTNG_DOMAIN_JUL
:
2290 case LTTNG_DOMAIN_PYTHON
:
2292 const char *default_event_name
, *default_chan_name
;
2294 struct lttng_event uevent
;
2295 struct lttng_domain tmp_dom
;
2296 struct ltt_ust_session
*usess
= session
->ust_session
;
2298 LTTNG_ASSERT(usess
);
2300 if (!agent_tracing_is_enabled()) {
2301 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2302 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2306 agt
= trace_ust_find_agent(usess
, domain
->type
);
2308 agt
= agent_create(domain
->type
);
2310 ret
= LTTNG_ERR_NOMEM
;
2313 agent_add(agt
, usess
->agents
);
2316 /* Create the default tracepoint. */
2317 memset(&uevent
, 0, sizeof(uevent
));
2318 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2319 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2320 default_event_name
= event_get_default_agent_ust_name(
2322 if (!default_event_name
) {
2323 ret
= LTTNG_ERR_FATAL
;
2326 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2327 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2330 * The domain type is changed because we are about to enable the
2331 * default channel and event for the JUL domain that are hardcoded.
2332 * This happens in the UST domain.
2334 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2335 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2337 switch (domain
->type
) {
2338 case LTTNG_DOMAIN_LOG4J
:
2339 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2341 case LTTNG_DOMAIN_JUL
:
2342 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2344 case LTTNG_DOMAIN_PYTHON
:
2345 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2348 /* The switch/case we are in makes this impossible */
2353 char *filter_expression_copy
= NULL
;
2354 struct lttng_bytecode
*filter_copy
= NULL
;
2357 const size_t filter_size
= sizeof(
2358 struct lttng_bytecode
)
2361 filter_copy
= (lttng_bytecode
*) zmalloc(filter_size
);
2363 ret
= LTTNG_ERR_NOMEM
;
2366 memcpy(filter_copy
, filter
, filter_size
);
2368 filter_expression_copy
=
2369 strdup(filter_expression
);
2370 if (!filter_expression
) {
2371 ret
= LTTNG_ERR_NOMEM
;
2374 if (!filter_expression_copy
|| !filter_copy
) {
2375 free(filter_expression_copy
);
2381 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2382 (char *) default_chan_name
,
2383 &uevent
, filter_expression_copy
,
2384 filter_copy
, NULL
, wpipe
);
2387 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2388 goto already_enabled
;
2389 } else if (ret
!= LTTNG_OK
) {
2393 /* The wild card * means that everything should be enabled. */
2394 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2395 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2398 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2402 filter_expression
= NULL
;
2403 if (ret
!= LTTNG_OK
) {
2410 ret
= LTTNG_ERR_UND
;
2418 free(filter_expression
);
2421 channel_attr_destroy(attr
);
2427 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2428 * We own filter, exclusion, and filter_expression.
2430 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2431 struct lttng_event
*event
,
2432 char *filter_expression
,
2433 struct lttng_event_exclusion
*exclusion
,
2434 struct lttng_bytecode
*bytecode
,
2439 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2441 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2444 * The ownership of the following parameters is transferred to
2445 * _cmd_enable_event:
2447 * - filter_expression,
2451 ret
= _cmd_enable_event(cmd_ctx
->session
,
2453 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2454 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2455 filter_expression
= NULL
;
2462 * Enable an event which is internal to LTTng. An internal should
2463 * never be made visible to clients and are immune to checks such as
2466 static int cmd_enable_event_internal(struct ltt_session
*session
,
2467 const struct lttng_domain
*domain
,
2468 char *channel_name
, struct lttng_event
*event
,
2469 char *filter_expression
,
2470 struct lttng_bytecode
*filter
,
2471 struct lttng_event_exclusion
*exclusion
,
2474 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2475 filter_expression
, filter
, exclusion
, wpipe
, true);
2479 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2481 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2482 struct lttng_payload
*reply_payload
)
2484 enum lttng_error_code ret_code
;
2486 ssize_t i
, nb_events
= 0;
2487 struct lttng_event
*events
= NULL
;
2488 struct lttcomm_list_command_header reply_command_header
= {};
2489 size_t reply_command_header_offset
;
2491 assert(reply_payload
);
2493 /* Reserve space for command reply header. */
2494 reply_command_header_offset
= reply_payload
->buffer
.size
;
2495 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2496 reply_command_header_offset
+
2497 sizeof(struct lttcomm_list_command_header
));
2499 ret_code
= LTTNG_ERR_NOMEM
;
2504 case LTTNG_DOMAIN_KERNEL
:
2505 nb_events
= kernel_list_events(&events
);
2506 if (nb_events
< 0) {
2507 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2511 case LTTNG_DOMAIN_UST
:
2512 nb_events
= ust_app_list_events(&events
);
2513 if (nb_events
< 0) {
2514 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2518 case LTTNG_DOMAIN_LOG4J
:
2519 case LTTNG_DOMAIN_JUL
:
2520 case LTTNG_DOMAIN_PYTHON
:
2521 nb_events
= agent_list_events(&events
, domain
);
2522 if (nb_events
< 0) {
2523 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2528 ret_code
= LTTNG_ERR_UND
;
2532 for (i
= 0; i
< nb_events
; i
++) {
2533 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2536 ret_code
= LTTNG_ERR_NOMEM
;
2541 if (nb_events
> UINT32_MAX
) {
2542 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2543 ret_code
= LTTNG_ERR_OVERFLOW
;
2547 /* Update command reply header. */
2548 reply_command_header
.count
= (uint32_t) nb_events
;
2549 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2550 sizeof(reply_command_header
));
2552 ret_code
= LTTNG_OK
;
2559 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2561 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2562 struct lttng_payload
*reply
)
2564 enum lttng_error_code ret_code
;
2566 unsigned int i
, nb_fields
;
2567 struct lttng_event_field
*fields
= NULL
;
2568 struct lttcomm_list_command_header reply_command_header
= {};
2569 size_t reply_command_header_offset
;
2573 /* Reserve space for command reply header. */
2574 reply_command_header_offset
= reply
->buffer
.size
;
2575 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2576 reply_command_header_offset
+
2577 sizeof(struct lttcomm_list_command_header
));
2579 ret_code
= LTTNG_ERR_NOMEM
;
2584 case LTTNG_DOMAIN_UST
:
2585 ret
= ust_app_list_event_fields(&fields
);
2587 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2592 case LTTNG_DOMAIN_KERNEL
:
2593 default: /* fall-through */
2594 ret_code
= LTTNG_ERR_UND
;
2600 for (i
= 0; i
< nb_fields
; i
++) {
2601 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2603 ret_code
= LTTNG_ERR_NOMEM
;
2608 if (nb_fields
> UINT32_MAX
) {
2609 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2610 ret_code
= LTTNG_ERR_OVERFLOW
;
2614 /* Update command reply header. */
2615 reply_command_header
.count
= (uint32_t) nb_fields
;
2617 memcpy(reply
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2618 sizeof(reply_command_header
));
2620 ret_code
= LTTNG_OK
;
2627 enum lttng_error_code
cmd_list_syscalls(
2628 struct lttng_payload
*reply_payload
)
2630 enum lttng_error_code ret_code
;
2631 ssize_t nb_events
, i
;
2633 struct lttng_event
*events
= NULL
;
2634 struct lttcomm_list_command_header reply_command_header
= {};
2635 size_t reply_command_header_offset
;
2637 assert(reply_payload
);
2639 /* Reserve space for command reply header. */
2640 reply_command_header_offset
= reply_payload
->buffer
.size
;
2641 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2642 reply_command_header_offset
+
2643 sizeof(struct lttcomm_list_command_header
));
2645 ret_code
= LTTNG_ERR_NOMEM
;
2649 nb_events
= syscall_table_list(&events
);
2650 if (nb_events
< 0) {
2651 ret_code
= (enum lttng_error_code
) -nb_events
;
2655 for (i
= 0; i
< nb_events
; i
++) {
2656 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2659 ret_code
= LTTNG_ERR_NOMEM
;
2664 if (nb_events
> UINT32_MAX
) {
2665 ERR("Syscall count would overflow the syscall listing command's reply");
2666 ret_code
= LTTNG_ERR_OVERFLOW
;
2670 /* Update command reply header. */
2671 reply_command_header
.count
= (uint32_t) nb_events
;
2672 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2673 sizeof(reply_command_header
));
2675 ret_code
= LTTNG_OK
;
2682 * Command LTTNG_START_TRACE processed by the client thread.
2684 * Called with session mutex held.
2686 int cmd_start_trace(struct ltt_session
*session
)
2688 enum lttng_error_code ret
;
2689 unsigned long nb_chan
= 0;
2690 struct ltt_kernel_session
*ksession
;
2691 struct ltt_ust_session
*usess
;
2692 const bool session_rotated_after_last_stop
=
2693 session
->rotated_after_last_stop
;
2694 const bool session_cleared_after_last_stop
=
2695 session
->cleared_after_last_stop
;
2697 LTTNG_ASSERT(session
);
2699 /* Ease our life a bit ;) */
2700 ksession
= session
->kernel_session
;
2701 usess
= session
->ust_session
;
2703 /* Is the session already started? */
2704 if (session
->active
) {
2705 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2706 /* Perform nothing */
2710 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2711 !session
->current_trace_chunk
) {
2713 * A rotation was launched while the session was stopped and
2714 * it has not been completed yet. It is not possible to start
2715 * the session since starting the session here would require a
2716 * rotation from "NULL" to a new trace chunk. That rotation
2717 * would overlap with the ongoing rotation, which is not
2720 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2722 ret
= LTTNG_ERR_ROTATION_PENDING
;
2727 * Starting a session without channel is useless since after that it's not
2728 * possible to enable channel thus inform the client.
2730 if (usess
&& usess
->domain_global
.channels
) {
2731 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2734 nb_chan
+= ksession
->channel_count
;
2737 ret
= LTTNG_ERR_NO_CHANNEL
;
2741 session
->active
= 1;
2742 session
->rotated_after_last_stop
= false;
2743 session
->cleared_after_last_stop
= false;
2744 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2745 if (!session
->has_been_started
) {
2746 struct lttng_trace_chunk
*trace_chunk
;
2748 DBG("Creating initial trace chunk of session \"%s\"",
2750 trace_chunk
= session_create_new_trace_chunk(
2751 session
, NULL
, NULL
, NULL
);
2753 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2756 LTTNG_ASSERT(!session
->current_trace_chunk
);
2757 ret
= (lttng_error_code
) session_set_trace_chunk(session
, trace_chunk
,
2759 lttng_trace_chunk_put(trace_chunk
);
2761 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2765 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2768 * Rotate existing streams into the new chunk.
2769 * This is a "quiet" rotation has no client has
2770 * explicitly requested this operation.
2772 * There is also no need to wait for the rotation
2773 * to complete as it will happen immediately. No data
2774 * was produced as the session was stopped, so the
2775 * rotation should happen on reception of the command.
2777 ret
= (lttng_error_code
) cmd_rotate_session(session
, NULL
, true,
2778 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2779 if (ret
!= LTTNG_OK
) {
2785 /* Kernel tracing */
2786 if (ksession
!= NULL
) {
2787 DBG("Start kernel tracing session %s", session
->name
);
2788 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2789 if (ret
!= LTTNG_OK
) {
2794 /* Flag session that trace should start automatically */
2796 int int_ret
= ust_app_start_trace_all(usess
);
2799 ret
= LTTNG_ERR_UST_START_FAIL
;
2805 * Open a packet in every stream of the session to ensure that viewers
2806 * can correctly identify the boundaries of the periods during which
2807 * tracing was active for this session.
2809 ret
= session_open_packets(session
);
2810 if (ret
!= LTTNG_OK
) {
2815 * Clear the flag that indicates that a rotation was done while the
2816 * session was stopped.
2818 session
->rotated_after_last_stop
= false;
2820 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2821 int int_ret
= timer_session_rotation_schedule_timer_start(
2822 session
, session
->rotate_timer_period
);
2825 ERR("Failed to enable rotate timer");
2826 ret
= LTTNG_ERR_UNK
;
2834 if (ret
== LTTNG_OK
) {
2835 /* Flag this after a successful start. */
2836 session
->has_been_started
|= 1;
2838 session
->active
= 0;
2839 /* Restore initial state on error. */
2840 session
->rotated_after_last_stop
=
2841 session_rotated_after_last_stop
;
2842 session
->cleared_after_last_stop
=
2843 session_cleared_after_last_stop
;
2850 * Command LTTNG_STOP_TRACE processed by the client thread.
2852 int cmd_stop_trace(struct ltt_session
*session
)
2855 struct ltt_kernel_session
*ksession
;
2856 struct ltt_ust_session
*usess
;
2858 LTTNG_ASSERT(session
);
2860 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2862 ksession
= session
->kernel_session
;
2863 usess
= session
->ust_session
;
2865 /* Session is not active. Skip everything and inform the client. */
2866 if (!session
->active
) {
2867 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2871 ret
= stop_kernel_session(ksession
);
2872 if (ret
!= LTTNG_OK
) {
2876 if (usess
&& usess
->active
) {
2877 ret
= ust_app_stop_trace_all(usess
);
2879 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2884 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2886 /* Flag inactive after a successful stop. */
2887 session
->active
= 0;
2895 * Set the base_path of the session only if subdir of a control uris is set.
2896 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2898 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2900 struct lttng_uri
*uris
)
2905 for (i
= 0; i
< nb_uri
; i
++) {
2906 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2907 uris
[i
].subdir
[0] == '\0') {
2908 /* Not interested in these URIs */
2912 if (session
->base_path
!= NULL
) {
2913 free(session
->base_path
);
2914 session
->base_path
= NULL
;
2917 /* Set session base_path */
2918 session
->base_path
= strdup(uris
[i
].subdir
);
2919 if (!session
->base_path
) {
2920 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2921 uris
[i
].subdir
, session
->name
);
2922 ret
= LTTNG_ERR_NOMEM
;
2925 DBG2("Setting base path \"%s\" for session \"%s\"",
2926 session
->base_path
, session
->name
);
2934 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2936 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2937 struct lttng_uri
*uris
)
2940 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2941 struct ltt_ust_session
*usess
= session
->ust_session
;
2943 LTTNG_ASSERT(session
);
2945 LTTNG_ASSERT(nb_uri
> 0);
2947 /* Can't set consumer URI if the session is active. */
2948 if (session
->active
) {
2949 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2954 * Set the session base path if any. This is done inside
2955 * cmd_set_consumer_uri to preserve backward compatibility of the
2956 * previous session creation api vs the session descriptor api.
2958 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2959 if (ret
!= LTTNG_OK
) {
2963 /* Set the "global" consumer URIs */
2964 for (i
= 0; i
< nb_uri
; i
++) {
2965 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2967 if (ret
!= LTTNG_OK
) {
2972 /* Set UST session URIs */
2973 if (session
->ust_session
) {
2974 for (i
= 0; i
< nb_uri
; i
++) {
2975 ret
= add_uri_to_consumer(session
,
2976 session
->ust_session
->consumer
,
2977 &uris
[i
], LTTNG_DOMAIN_UST
);
2978 if (ret
!= LTTNG_OK
) {
2984 /* Set kernel session URIs */
2985 if (session
->kernel_session
) {
2986 for (i
= 0; i
< nb_uri
; i
++) {
2987 ret
= add_uri_to_consumer(session
,
2988 session
->kernel_session
->consumer
,
2989 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2990 if (ret
!= LTTNG_OK
) {
2997 * Make sure to set the session in output mode after we set URI since a
2998 * session can be created without URL (thus flagged in no output mode).
3000 session
->output_traces
= 1;
3002 ksess
->output_traces
= 1;
3006 usess
->output_traces
= 1;
3017 enum lttng_error_code
set_session_output_from_descriptor(
3018 struct ltt_session
*session
,
3019 const struct lttng_session_descriptor
*descriptor
)
3022 enum lttng_error_code ret_code
= LTTNG_OK
;
3023 enum lttng_session_descriptor_type session_type
=
3024 lttng_session_descriptor_get_type(descriptor
);
3025 enum lttng_session_descriptor_output_type output_type
=
3026 lttng_session_descriptor_get_output_type(descriptor
);
3027 struct lttng_uri uris
[2] = {};
3028 size_t uri_count
= 0;
3030 switch (output_type
) {
3031 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3033 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3034 lttng_session_descriptor_get_local_output_uri(descriptor
,
3038 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3039 lttng_session_descriptor_get_network_output_uris(descriptor
,
3040 &uris
[0], &uris
[1]);
3044 ret_code
= LTTNG_ERR_INVALID
;
3048 switch (session_type
) {
3049 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3051 struct snapshot_output
*new_output
= NULL
;
3053 new_output
= snapshot_output_alloc();
3055 ret_code
= LTTNG_ERR_NOMEM
;
3059 ret
= snapshot_output_init_with_uri(session
,
3060 DEFAULT_SNAPSHOT_MAX_SIZE
,
3061 NULL
, uris
, uri_count
, session
->consumer
,
3062 new_output
, &session
->snapshot
);
3064 ret_code
= (ret
== -ENOMEM
) ?
3065 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3066 snapshot_output_destroy(new_output
);
3069 snapshot_add_output(&session
->snapshot
, new_output
);
3072 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3073 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3075 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3079 ret_code
= LTTNG_ERR_INVALID
;
3087 enum lttng_error_code
cmd_create_session_from_descriptor(
3088 struct lttng_session_descriptor
*descriptor
,
3089 const lttng_sock_cred
*creds
,
3090 const char *home_path
)
3093 enum lttng_error_code ret_code
;
3094 const char *session_name
;
3095 struct ltt_session
*new_session
= NULL
;
3096 enum lttng_session_descriptor_status descriptor_status
;
3098 session_lock_list();
3100 if (*home_path
!= '/') {
3101 ERR("Home path provided by client is not absolute");
3102 ret_code
= LTTNG_ERR_INVALID
;
3107 descriptor_status
= lttng_session_descriptor_get_session_name(
3108 descriptor
, &session_name
);
3109 switch (descriptor_status
) {
3110 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3112 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3113 session_name
= NULL
;
3116 ret_code
= LTTNG_ERR_INVALID
;
3120 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3122 if (ret_code
!= LTTNG_OK
) {
3126 if (!session_name
) {
3127 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3130 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3135 if (!lttng_session_descriptor_is_output_destination_initialized(
3138 * Only include the session's creation time in the output
3139 * destination if the name of the session itself was
3140 * not auto-generated.
3142 ret_code
= lttng_session_descriptor_set_default_output(
3144 session_name
? &new_session
->creation_time
: NULL
,
3146 if (ret_code
!= LTTNG_OK
) {
3150 new_session
->has_user_specified_directory
=
3151 lttng_session_descriptor_has_output_directory(
3155 switch (lttng_session_descriptor_get_type(descriptor
)) {
3156 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3157 new_session
->snapshot_mode
= 1;
3159 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3160 new_session
->live_timer
=
3161 lttng_session_descriptor_live_get_timer_interval(
3168 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3169 if (ret_code
!= LTTNG_OK
) {
3172 new_session
->consumer
->enabled
= 1;
3173 ret_code
= LTTNG_OK
;
3175 /* Release reference provided by the session_create function. */
3176 session_put(new_session
);
3177 if (ret_code
!= LTTNG_OK
&& new_session
) {
3178 /* Release the global reference on error. */
3179 session_destroy(new_session
);
3181 session_unlock_list();
3185 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3186 struct lttng_session_descriptor
**return_descriptor
)
3189 size_t payload_size
;
3190 struct lttng_dynamic_buffer payload
;
3191 struct lttng_buffer_view home_dir_view
;
3192 struct lttng_buffer_view session_descriptor_view
;
3193 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3194 enum lttng_error_code ret_code
;
3196 lttng_dynamic_buffer_init(&payload
);
3197 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3199 ret_code
= LTTNG_ERR_INVALID
;
3202 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3203 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3204 ret_code
= LTTNG_ERR_INVALID
;
3208 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3209 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3210 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3212 ret_code
= LTTNG_ERR_NOMEM
;
3216 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3218 ERR("Reception of session descriptor failed, aborting.");
3219 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3223 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3226 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3227 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3228 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3229 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3230 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3234 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3236 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3237 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3238 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3239 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3240 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3244 ret
= lttng_session_descriptor_create_from_buffer(
3245 &session_descriptor_view
, &session_descriptor
);
3247 ERR("Failed to create session descriptor from payload of \"create session\" command");
3248 ret_code
= LTTNG_ERR_INVALID
;
3253 * Sets the descriptor's auto-generated properties (name, output) if
3256 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3258 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3259 if (ret_code
!= LTTNG_OK
) {
3263 ret_code
= LTTNG_OK
;
3264 *return_descriptor
= session_descriptor
;
3265 session_descriptor
= NULL
;
3267 lttng_dynamic_buffer_reset(&payload
);
3268 lttng_session_descriptor_destroy(session_descriptor
);
3273 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3274 void *_reply_context
)
3278 const struct cmd_destroy_session_reply_context
*reply_context
=
3279 (cmd_destroy_session_reply_context
*) _reply_context
;
3280 struct lttng_dynamic_buffer payload
;
3281 struct lttcomm_session_destroy_command_header cmd_header
;
3282 struct lttng_trace_archive_location
*location
= NULL
;
3283 struct lttcomm_lttng_msg llm
= {
3284 .cmd_type
= LTTNG_DESTROY_SESSION
,
3285 .ret_code
= reply_context
->destruction_status
,
3288 sizeof(struct lttcomm_session_destroy_command_header
),
3292 size_t payload_size_before_location
;
3294 lttng_dynamic_buffer_init(&payload
);
3296 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3298 ERR("Failed to append session destruction message");
3302 cmd_header
.rotation_state
=
3303 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3304 session
->rotation_state
:
3305 LTTNG_ROTATION_STATE_NO_ROTATION
);
3306 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3307 sizeof(cmd_header
));
3309 ERR("Failed to append session destruction command header");
3313 if (!reply_context
->implicit_rotation_on_destroy
) {
3314 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3318 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3319 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3324 location
= session_get_trace_archive_location(session
);
3326 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3331 payload_size_before_location
= payload
.size
;
3332 comm_ret
= lttng_trace_archive_location_serialize(location
,
3334 lttng_trace_archive_location_put(location
);
3336 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3340 /* Update the message to indicate the location's length. */
3341 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3342 payload
.size
- payload_size_before_location
;
3344 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3345 payload
.data
, payload
.size
);
3346 if (comm_ret
!= (ssize_t
) payload
.size
) {
3347 ERR("Failed to send result of the destruction of session \"%s\" to client",
3351 ret
= close(reply_context
->reply_sock_fd
);
3353 PERROR("Failed to close client socket in deferred session destroy reply");
3355 lttng_dynamic_buffer_reset(&payload
);
3356 free(_reply_context
);
3360 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3362 * Called with session lock held.
3364 int cmd_destroy_session(struct ltt_session
*session
,
3365 struct notification_thread_handle
*notification_thread_handle
,
3369 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3370 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3373 reply_context
= (cmd_destroy_session_reply_context
*) zmalloc(sizeof(*reply_context
));
3374 if (!reply_context
) {
3375 ret
= LTTNG_ERR_NOMEM
;
3378 reply_context
->reply_sock_fd
= *sock_fd
;
3382 LTTNG_ASSERT(session
);
3384 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3386 if (session
->active
) {
3387 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3389 ret
= cmd_stop_trace(session
);
3390 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3391 /* Carry on with the destruction of the session. */
3392 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3393 session
->name
, lttng_strerror(-ret
));
3394 destruction_last_error
= (lttng_error_code
) ret
;
3398 if (session
->rotation_schedule_timer_enabled
) {
3399 if (timer_session_rotation_schedule_timer_stop(
3401 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3403 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3407 if (session
->rotate_size
) {
3408 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3409 session
->rotate_size
= 0;
3412 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3414 * Perform a last rotation on destruction if rotations have
3415 * occurred during the session's lifetime.
3417 ret
= cmd_rotate_session(session
, NULL
, false,
3418 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3419 if (ret
!= LTTNG_OK
) {
3420 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3421 session
->name
, lttng_strerror(-ret
));
3422 destruction_last_error
= (lttng_error_code
) -ret
;
3424 if (reply_context
) {
3425 reply_context
->implicit_rotation_on_destroy
= true;
3427 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3429 * The user has not triggered a session rotation. However, to
3430 * ensure all data has been consumed, the session is rotated
3431 * to a 'null' trace chunk before it is destroyed.
3433 * This is a "quiet" rotation meaning that no notification is
3434 * emitted and no renaming of the current trace chunk takes
3437 ret
= cmd_rotate_session(session
, NULL
, true,
3438 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3440 * Rotation operations may not be supported by the kernel
3441 * tracer. Hence, do not consider this implicit rotation as
3442 * a session destruction error. The library has already stopped
3443 * the session and waited for pending data; there is nothing
3444 * left to do but complete the destruction of the session.
3446 if (ret
!= LTTNG_OK
&&
3447 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3448 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3449 session
->name
, lttng_strerror(ret
));
3450 destruction_last_error
= (lttng_error_code
) -ret
;
3454 if (session
->shm_path
[0]) {
3456 * When a session is created with an explicit shm_path,
3457 * the consumer daemon will create its shared memory files
3458 * at that location and will *not* unlink them. This is normal
3459 * as the intention of that feature is to make it possible
3460 * to retrieve the content of those files should a crash occur.
3462 * To ensure the content of those files can be used, the
3463 * sessiond daemon will replicate the content of the metadata
3464 * cache in a metadata file.
3466 * On clean-up, it is expected that the consumer daemon will
3467 * unlink the shared memory files and that the session daemon
3468 * will unlink the metadata file. Then, the session's directory
3469 * in the shm path can be removed.
3471 * Unfortunately, a flaw in the design of the sessiond's and
3472 * consumerd's tear down of channels makes it impossible to
3473 * determine when the sessiond _and_ the consumerd have both
3474 * destroyed their representation of a channel. For one, the
3475 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3476 * callbacks in both daemons.
3478 * However, it is also impossible for the sessiond to know when
3479 * the consumer daemon is done destroying its channel(s) since
3480 * it occurs as a reaction to the closing of the channel's file
3481 * descriptor. There is no resulting communication initiated
3482 * from the consumerd to the sessiond to confirm that the
3483 * operation is completed (and was successful).
3485 * Until this is all fixed, the session daemon checks for the
3486 * removal of the session's shm path which makes it possible
3487 * to safely advertise a session as having been destroyed.
3489 * Prior to this fix, it was not possible to reliably save
3490 * a session making use of the --shm-path option, destroy it,
3491 * and load it again. This is because the creation of the
3492 * session would fail upon seeing the session's shm path
3493 * already in existence.
3495 * Note that none of the error paths in the check for the
3496 * directory's existence return an error. This is normal
3497 * as there isn't much that can be done. The session will
3498 * be destroyed properly, except that we can't offer the
3499 * guarantee that the same session can be re-created.
3501 current_completion_handler
= &destroy_completion_handler
.handler
;
3502 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3504 sizeof(destroy_completion_handler
.shm_path
));
3509 * The session is destroyed. However, note that the command context
3510 * still holds a reference to the session, thus delaying its destruction
3511 * _at least_ up to the point when that reference is released.
3513 session_destroy(session
);
3514 if (reply_context
) {
3515 reply_context
->destruction_status
= destruction_last_error
;
3516 ret
= session_add_destroy_notifier(session
,
3517 cmd_destroy_session_reply
,
3518 (void *) reply_context
);
3520 ret
= LTTNG_ERR_FATAL
;
3532 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3534 int cmd_register_consumer(struct ltt_session
*session
,
3535 enum lttng_domain_type domain
, const char *sock_path
,
3536 struct consumer_data
*cdata
)
3539 struct consumer_socket
*socket
= NULL
;
3541 LTTNG_ASSERT(session
);
3542 LTTNG_ASSERT(cdata
);
3543 LTTNG_ASSERT(sock_path
);
3546 case LTTNG_DOMAIN_KERNEL
:
3548 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3550 LTTNG_ASSERT(ksess
);
3552 /* Can't register a consumer if there is already one */
3553 if (ksess
->consumer_fds_sent
!= 0) {
3554 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3558 sock
= lttcomm_connect_unix_sock(sock_path
);
3560 ret
= LTTNG_ERR_CONNECT_FAIL
;
3563 cdata
->cmd_sock
= sock
;
3565 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3566 if (socket
== NULL
) {
3569 PERROR("close register consumer");
3571 cdata
->cmd_sock
= -1;
3572 ret
= LTTNG_ERR_FATAL
;
3576 socket
->lock
= (pthread_mutex_t
*) zmalloc(sizeof(pthread_mutex_t
));
3577 if (socket
->lock
== NULL
) {
3578 PERROR("zmalloc pthread mutex");
3579 ret
= LTTNG_ERR_FATAL
;
3582 pthread_mutex_init(socket
->lock
, NULL
);
3583 socket
->registered
= 1;
3586 consumer_add_socket(socket
, ksess
->consumer
);
3589 pthread_mutex_lock(&cdata
->pid_mutex
);
3591 pthread_mutex_unlock(&cdata
->pid_mutex
);
3596 /* TODO: Userspace tracing */
3597 ret
= LTTNG_ERR_UND
;
3605 consumer_destroy_socket(socket
);
3611 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3613 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3614 struct lttng_domain
**domains
)
3619 struct lttng_ht_iter iter
;
3621 if (session
->kernel_session
!= NULL
) {
3622 DBG3("Listing domains found kernel domain");
3626 if (session
->ust_session
!= NULL
) {
3627 DBG3("Listing domains found UST global domain");
3631 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3633 if (agt
->being_used
) {
3644 *domains
= (lttng_domain
*) zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3645 if (*domains
== NULL
) {
3646 ret
= LTTNG_ERR_FATAL
;
3650 if (session
->kernel_session
!= NULL
) {
3651 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3653 /* Kernel session buffer type is always GLOBAL */
3654 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3659 if (session
->ust_session
!= NULL
) {
3660 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3661 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3665 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3667 if (agt
->being_used
) {
3668 (*domains
)[index
].type
= agt
->domain
;
3669 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3679 /* Return negative value to differentiate return code */
3685 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3687 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3688 struct ltt_session
*session
,
3689 struct lttng_payload
*payload
)
3693 struct lttcomm_list_command_header cmd_header
= {};
3694 size_t cmd_header_offset
;
3695 enum lttng_error_code ret_code
;
3700 DBG("Listing channels for session %s", session
->name
);
3702 cmd_header_offset
= payload
->buffer
.size
;
3704 /* Reserve space for command reply header. */
3705 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3706 cmd_header_offset
+ sizeof(cmd_header
));
3708 ret_code
= LTTNG_ERR_NOMEM
;
3713 case LTTNG_DOMAIN_KERNEL
:
3715 /* Kernel channels */
3716 struct ltt_kernel_channel
*kchan
;
3717 if (session
->kernel_session
!= NULL
) {
3718 cds_list_for_each_entry(kchan
,
3719 &session
->kernel_session
->channel_list
.head
, list
) {
3720 uint64_t discarded_events
, lost_packets
;
3721 struct lttng_channel_extended
*extended
;
3723 extended
= (struct lttng_channel_extended
*)
3724 kchan
->channel
->attr
.extended
.ptr
;
3726 ret
= get_kernel_runtime_stats(session
, kchan
,
3727 &discarded_events
, &lost_packets
);
3729 ret_code
= LTTNG_ERR_UNK
;
3734 * Update the discarded_events and lost_packets
3735 * count for the channel
3737 extended
->discarded_events
= discarded_events
;
3738 extended
->lost_packets
= lost_packets
;
3740 ret
= lttng_channel_serialize(
3741 kchan
->channel
, &payload
->buffer
);
3743 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3744 kchan
->channel
->name
);
3745 ret_code
= LTTNG_ERR_UNK
;
3754 case LTTNG_DOMAIN_UST
:
3756 struct lttng_ht_iter iter
;
3757 struct ltt_ust_channel
*uchan
;
3760 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3761 &iter
.iter
, uchan
, node
.node
) {
3762 uint64_t discarded_events
= 0, lost_packets
= 0;
3763 struct lttng_channel
*channel
= NULL
;
3764 struct lttng_channel_extended
*extended
;
3766 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3768 ret_code
= LTTNG_ERR_NOMEM
;
3772 extended
= (struct lttng_channel_extended
*)
3773 channel
->attr
.extended
.ptr
;
3775 ret
= get_ust_runtime_stats(session
, uchan
,
3776 &discarded_events
, &lost_packets
);
3778 lttng_channel_destroy(channel
);
3779 ret_code
= LTTNG_ERR_UNK
;
3783 extended
->discarded_events
= discarded_events
;
3784 extended
->lost_packets
= lost_packets
;
3786 ret
= lttng_channel_serialize(
3787 channel
, &payload
->buffer
);
3789 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3791 lttng_channel_destroy(channel
);
3792 ret_code
= LTTNG_ERR_UNK
;
3796 lttng_channel_destroy(channel
);
3806 if (i
> UINT32_MAX
) {
3807 ERR("Channel count would overflow the channel listing command's reply");
3808 ret_code
= LTTNG_ERR_OVERFLOW
;
3812 /* Update command reply header. */
3813 cmd_header
.count
= (uint32_t) i
;
3814 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3815 sizeof(cmd_header
));
3816 ret_code
= LTTNG_OK
;
3823 * Command LTTNG_LIST_EVENTS processed by the client thread.
3825 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3826 struct ltt_session
*session
,
3828 struct lttng_payload
*reply_payload
)
3830 int buffer_resize_ret
;
3831 enum lttng_error_code ret_code
= LTTNG_OK
;
3832 struct lttcomm_list_command_header reply_command_header
= {};
3833 size_t reply_command_header_offset
;
3834 unsigned int nb_events
= 0;
3836 assert(reply_payload
);
3838 /* Reserve space for command reply header. */
3839 reply_command_header_offset
= reply_payload
->buffer
.size
;
3840 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3841 reply_command_header_offset
+
3842 sizeof(struct lttcomm_list_command_header
));
3843 if (buffer_resize_ret
) {
3844 ret_code
= LTTNG_ERR_NOMEM
;
3849 case LTTNG_DOMAIN_KERNEL
:
3850 if (session
->kernel_session
!= NULL
) {
3851 ret_code
= list_lttng_kernel_events(channel_name
,
3852 session
->kernel_session
, reply_payload
, &nb_events
);
3856 case LTTNG_DOMAIN_UST
:
3858 if (session
->ust_session
!= NULL
) {
3859 ret_code
= list_lttng_ust_global_events(channel_name
,
3860 &session
->ust_session
->domain_global
,
3861 reply_payload
, &nb_events
);
3866 case LTTNG_DOMAIN_LOG4J
:
3867 case LTTNG_DOMAIN_JUL
:
3868 case LTTNG_DOMAIN_PYTHON
:
3869 if (session
->ust_session
) {
3870 struct lttng_ht_iter iter
;
3874 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3875 &iter
.iter
, agt
, node
.node
) {
3876 if (agt
->domain
== domain
) {
3877 ret_code
= list_lttng_agent_events(
3878 agt
, reply_payload
, &nb_events
);
3887 ret_code
= LTTNG_ERR_UND
;
3891 if (nb_events
> UINT32_MAX
) {
3892 ret_code
= LTTNG_ERR_OVERFLOW
;
3896 /* Update command reply header. */
3897 reply_command_header
.count
= (uint32_t) nb_events
;
3898 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
3899 sizeof(reply_command_header
));
3906 * Using the session list, filled a lttng_session array to send back to the
3907 * client for session listing.
3909 * The session list lock MUST be acquired before calling this function. Use
3910 * session_lock_list() and session_unlock_list().
3912 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3913 size_t session_count
, uid_t uid
, gid_t gid
)
3917 struct ltt_session
*session
;
3918 struct ltt_session_list
*list
= session_get_list();
3919 struct lttng_session_extended
*extended
=
3920 (typeof(extended
)) (&sessions
[session_count
]);
3922 DBG("Getting all available session for UID %d GID %d",
3925 * Iterate over session list and append data after the control struct in
3928 cds_list_for_each_entry(session
, &list
->head
, list
) {
3929 if (!session_get(session
)) {
3933 * Only list the sessions the user can control.
3935 if (!session_access_ok(session
, uid
) ||
3936 session
->destroyed
) {
3937 session_put(session
);
3941 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3942 struct ltt_ust_session
*usess
= session
->ust_session
;
3944 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3945 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3946 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3947 ret
= build_network_session_path(sessions
[i
].path
,
3948 sizeof(sessions
[i
].path
), session
);
3950 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3951 session
->consumer
->dst
.session_root_path
);
3954 PERROR("snprintf session path");
3955 session_put(session
);
3959 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3960 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3961 sessions
[i
].enabled
= session
->active
;
3962 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3963 sessions
[i
].live_timer_interval
= session
->live_timer
;
3964 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3965 extended
[i
].creation_time
.is_set
= 1;
3967 session_put(session
);
3972 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3973 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3975 int cmd_data_pending(struct ltt_session
*session
)
3978 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3979 struct ltt_ust_session
*usess
= session
->ust_session
;
3981 LTTNG_ASSERT(session
);
3983 DBG("Data pending for session %s", session
->name
);
3985 /* Session MUST be stopped to ask for data availability. */
3986 if (session
->active
) {
3987 ret
= LTTNG_ERR_SESSION_STARTED
;
3991 * If stopped, just make sure we've started before else the above call
3992 * will always send that there is data pending.
3994 * The consumer assumes that when the data pending command is received,
3995 * the trace has been started before or else no output data is written
3996 * by the streams which is a condition for data pending. So, this is
3997 * *VERY* important that we don't ask the consumer before a start
4000 if (!session
->has_been_started
) {
4006 /* A rotation is still pending, we have to wait. */
4007 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4008 DBG("Rotate still pending for session %s", session
->name
);
4013 if (ksess
&& ksess
->consumer
) {
4014 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4016 /* Data is still being extracted for the kernel. */
4021 if (usess
&& usess
->consumer
) {
4022 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4024 /* Data is still being extracted for the kernel. */
4029 /* Data is ready to be read by a viewer */
4037 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4039 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4041 int cmd_snapshot_add_output(struct ltt_session
*session
,
4042 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4045 struct snapshot_output
*new_output
;
4047 LTTNG_ASSERT(session
);
4048 LTTNG_ASSERT(output
);
4050 DBG("Cmd snapshot add output for session %s", session
->name
);
4053 * Can't create an output if the session is not set in no-output mode.
4055 if (session
->output_traces
) {
4056 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4060 if (session
->has_non_mmap_channel
) {
4061 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4065 /* Only one output is allowed until we have the "tee" feature. */
4066 if (session
->snapshot
.nb_output
== 1) {
4067 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4071 new_output
= snapshot_output_alloc();
4073 ret
= LTTNG_ERR_NOMEM
;
4077 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4078 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4079 &session
->snapshot
);
4081 if (ret
== -ENOMEM
) {
4082 ret
= LTTNG_ERR_NOMEM
;
4084 ret
= LTTNG_ERR_INVALID
;
4090 snapshot_add_output(&session
->snapshot
, new_output
);
4092 *id
= new_output
->id
;
4099 snapshot_output_destroy(new_output
);
4105 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4107 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4109 int cmd_snapshot_del_output(struct ltt_session
*session
,
4110 const struct lttng_snapshot_output
*output
)
4113 struct snapshot_output
*sout
= NULL
;
4115 LTTNG_ASSERT(session
);
4116 LTTNG_ASSERT(output
);
4121 * Permission denied to create an output if the session is not
4122 * set in no output mode.
4124 if (session
->output_traces
) {
4125 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4130 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4132 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4133 } else if (*output
->name
!= '\0') {
4134 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4136 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4139 ret
= LTTNG_ERR_INVALID
;
4143 snapshot_delete_output(&session
->snapshot
, sout
);
4144 snapshot_output_destroy(sout
);
4153 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4155 * If no output is available, outputs is untouched and 0 is returned.
4157 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4159 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4160 struct lttng_snapshot_output
**outputs
)
4163 struct lttng_snapshot_output
*list
= NULL
;
4164 struct lttng_ht_iter iter
;
4165 struct snapshot_output
*output
;
4167 LTTNG_ASSERT(session
);
4168 LTTNG_ASSERT(outputs
);
4170 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4173 * Permission denied to create an output if the session is not
4174 * set in no output mode.
4176 if (session
->output_traces
) {
4177 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4181 if (session
->snapshot
.nb_output
== 0) {
4186 list
= (lttng_snapshot_output
*) zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4188 ret
= -LTTNG_ERR_NOMEM
;
4192 /* Copy list from session to the new list object. */
4194 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4195 output
, node
.node
) {
4196 LTTNG_ASSERT(output
->consumer
);
4197 list
[idx
].id
= output
->id
;
4198 list
[idx
].max_size
= output
->max_size
;
4199 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4200 sizeof(list
[idx
].name
))) {
4201 ret
= -LTTNG_ERR_INVALID
;
4204 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4205 if (lttng_strncpy(list
[idx
].ctrl_url
,
4206 output
->consumer
->dst
.session_root_path
,
4207 sizeof(list
[idx
].ctrl_url
))) {
4208 ret
= -LTTNG_ERR_INVALID
;
4213 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4214 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4216 ret
= -LTTNG_ERR_NOMEM
;
4221 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4222 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4224 ret
= -LTTNG_ERR_NOMEM
;
4233 ret
= session
->snapshot
.nb_output
;
4242 * Check if we can regenerate the metadata for this session.
4243 * Only kernel, UST per-uid and non-live sessions are supported.
4245 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4248 int check_regenerate_metadata_support(struct ltt_session
*session
)
4252 LTTNG_ASSERT(session
);
4254 if (session
->live_timer
!= 0) {
4255 ret
= LTTNG_ERR_LIVE_SESSION
;
4258 if (!session
->active
) {
4259 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4262 if (session
->ust_session
) {
4263 switch (session
->ust_session
->buffer_type
) {
4264 case LTTNG_BUFFER_PER_UID
:
4266 case LTTNG_BUFFER_PER_PID
:
4267 ret
= LTTNG_ERR_PER_PID_SESSION
;
4271 ret
= LTTNG_ERR_UNK
;
4275 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4276 session
->consumer
->relay_minor_version
< 8) {
4277 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4287 int clear_metadata_file(int fd
)
4292 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4293 if (lseek_ret
< 0) {
4299 ret
= ftruncate(fd
, 0);
4301 PERROR("ftruncate");
4310 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4313 struct buffer_reg_uid
*uid_reg
= NULL
;
4314 struct buffer_reg_session
*session_reg
= NULL
;
4317 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4318 struct ust_registry_session
*registry
;
4319 struct ust_registry_channel
*chan
;
4320 struct lttng_ht_iter iter_chan
;
4322 session_reg
= uid_reg
->registry
;
4323 registry
= session_reg
->reg
.ust
;
4325 pthread_mutex_lock(®istry
->lock
);
4326 registry
->metadata_len_sent
= 0;
4327 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4328 registry
->metadata_len
= 0;
4329 registry
->metadata_version
++;
4330 if (registry
->metadata_fd
> 0) {
4331 /* Clear the metadata file's content. */
4332 ret
= clear_metadata_file(registry
->metadata_fd
);
4334 pthread_mutex_unlock(®istry
->lock
);
4339 ret
= ust_metadata_session_statedump(registry
, NULL
,
4340 registry
->major
, registry
->minor
);
4342 pthread_mutex_unlock(®istry
->lock
);
4343 ERR("Failed to generate session metadata (err = %d)",
4347 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4349 struct ust_registry_event
*event
;
4350 struct lttng_ht_iter iter_event
;
4352 ret
= ust_metadata_channel_statedump(registry
, chan
);
4354 pthread_mutex_unlock(®istry
->lock
);
4355 ERR("Failed to generate channel metadata "
4359 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4361 ret
= ust_metadata_event_statedump(registry
,
4364 pthread_mutex_unlock(®istry
->lock
);
4365 ERR("Failed to generate event metadata "
4371 pthread_mutex_unlock(®istry
->lock
);
4380 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4382 * Ask the consumer to truncate the existing metadata file(s) and
4383 * then regenerate the metadata. Live and per-pid sessions are not
4384 * supported and return an error.
4386 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4388 int cmd_regenerate_metadata(struct ltt_session
*session
)
4392 LTTNG_ASSERT(session
);
4394 ret
= check_regenerate_metadata_support(session
);
4399 if (session
->kernel_session
) {
4400 ret
= kernctl_session_regenerate_metadata(
4401 session
->kernel_session
->fd
);
4403 ERR("Failed to regenerate the kernel metadata");
4408 if (session
->ust_session
) {
4409 ret
= ust_regenerate_metadata(session
->ust_session
);
4411 ERR("Failed to regenerate the UST metadata");
4415 DBG("Cmd metadata regenerate for session %s", session
->name
);
4423 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4425 * Ask the tracer to regenerate a new statedump.
4427 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4429 int cmd_regenerate_statedump(struct ltt_session
*session
)
4433 LTTNG_ASSERT(session
);
4435 if (!session
->active
) {
4436 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4440 if (session
->kernel_session
) {
4441 ret
= kernctl_session_regenerate_statedump(
4442 session
->kernel_session
->fd
);
4444 * Currently, the statedump in kernel can only fail if out
4448 if (ret
== -ENOMEM
) {
4449 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4451 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4453 ERR("Failed to regenerate the kernel statedump");
4458 if (session
->ust_session
) {
4459 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4461 * Currently, the statedump in UST always returns 0.
4464 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4465 ERR("Failed to regenerate the UST statedump");
4469 DBG("Cmd regenerate statedump for session %s", session
->name
);
4477 enum lttng_error_code
synchronize_tracer_notifier_register(
4478 struct notification_thread_handle
*notification_thread
,
4479 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4481 enum lttng_error_code ret_code
;
4482 const struct lttng_condition
*condition
=
4483 lttng_trigger_get_const_condition(trigger
);
4484 const char *trigger_name
;
4485 uid_t trigger_owner
;
4486 enum lttng_trigger_status trigger_status
;
4487 const enum lttng_domain_type trigger_domain
=
4488 lttng_trigger_get_underlying_domain_type_restriction(
4491 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4492 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4494 LTTNG_ASSERT(condition
);
4495 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4496 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4498 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4499 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4500 trigger_name
: "(anonymous)";
4502 session_lock_list();
4503 switch (trigger_domain
) {
4504 case LTTNG_DOMAIN_KERNEL
:
4506 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4507 if (ret_code
!= LTTNG_OK
) {
4508 enum lttng_error_code notif_thread_unregister_ret
;
4510 notif_thread_unregister_ret
=
4511 notification_thread_command_unregister_trigger(
4512 notification_thread
, trigger
);
4514 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4515 /* Return the original error code. */
4516 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4518 (int) trigger_owner
,
4524 case LTTNG_DOMAIN_UST
:
4525 ust_app_global_update_all_event_notifier_rules();
4527 case LTTNG_DOMAIN_JUL
:
4528 case LTTNG_DOMAIN_LOG4J
:
4529 case LTTNG_DOMAIN_PYTHON
:
4531 /* Agent domains. */
4532 struct agent
*agt
= agent_find_by_event_notifier_domain(
4536 agt
= agent_create(trigger_domain
);
4538 ret_code
= LTTNG_ERR_NOMEM
;
4539 goto end_unlock_session_list
;
4542 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4545 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4546 if (ret_code
!= LTTNG_OK
) {
4547 goto end_unlock_session_list
;
4552 case LTTNG_DOMAIN_NONE
:
4557 ret_code
= LTTNG_OK
;
4558 end_unlock_session_list
:
4559 session_unlock_list();
4563 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4564 struct lttng_trigger
*trigger
,
4565 bool is_trigger_anonymous
,
4566 struct notification_thread_handle
*notification_thread
,
4567 struct lttng_trigger
**return_trigger
)
4569 enum lttng_error_code ret_code
;
4570 const char *trigger_name
;
4571 uid_t trigger_owner
;
4572 enum lttng_trigger_status trigger_status
;
4574 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4575 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4576 trigger_name
: "(anonymous)";
4578 trigger_status
= lttng_trigger_get_owner_uid(
4579 trigger
, &trigger_owner
);
4580 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4582 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4583 trigger_name
, (int) trigger_owner
,
4584 (int) lttng_credentials_get_uid(cmd_creds
));
4587 * Validate the trigger credentials against the command credentials.
4588 * Only the root user can register a trigger with non-matching
4591 if (!lttng_credentials_is_equal_uid(
4592 lttng_trigger_get_credentials(trigger
),
4594 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4595 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4596 trigger_name
, (int) trigger_owner
,
4597 (int) lttng_credentials_get_uid(cmd_creds
));
4598 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4604 * The bytecode generation also serves as a validation step for the
4605 * bytecode expressions.
4607 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4608 if (ret_code
!= LTTNG_OK
) {
4609 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4610 trigger_name
, (int) trigger_owner
, ret_code
);
4615 * A reference to the trigger is acquired by the notification thread.
4616 * It is safe to return the same trigger to the caller since it the
4617 * other user holds a reference.
4619 * The trigger is modified during the execution of the
4620 * "register trigger" command. However, by the time the command returns,
4621 * it is safe to use without any locking as its properties are
4624 ret_code
= notification_thread_command_register_trigger(
4625 notification_thread
, trigger
, is_trigger_anonymous
);
4626 if (ret_code
!= LTTNG_OK
) {
4627 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4628 trigger_name
, (int) trigger_owner
, ret_code
);
4632 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4633 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4634 trigger_name
: "(anonymous)";
4637 * Synchronize tracers if the trigger adds an event notifier.
4639 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4640 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4641 trigger
, cmd_creds
);
4642 if (ret_code
!= LTTNG_OK
) {
4643 ERR("Error registering tracer notifier: %s",
4644 lttng_strerror(-ret_code
));
4650 * Return an updated trigger to the client.
4652 * Since a modified version of the same trigger is returned, acquire a
4653 * reference to the trigger so the caller doesn't have to care if those
4654 * are distinct instances or not.
4656 if (ret_code
== LTTNG_OK
) {
4657 lttng_trigger_get(trigger
);
4658 *return_trigger
= trigger
;
4659 /* Ownership of trigger was transferred to caller. */
4667 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4668 const struct lttng_trigger
*trigger
)
4670 enum lttng_error_code ret_code
;
4671 const struct lttng_condition
*condition
=
4672 lttng_trigger_get_const_condition(trigger
);
4673 const enum lttng_domain_type trigger_domain
=
4674 lttng_trigger_get_underlying_domain_type_restriction(
4677 LTTNG_ASSERT(condition
);
4678 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4679 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4681 session_lock_list();
4682 switch (trigger_domain
) {
4683 case LTTNG_DOMAIN_KERNEL
:
4684 ret_code
= kernel_unregister_event_notifier(trigger
);
4685 if (ret_code
!= LTTNG_OK
) {
4686 goto end_unlock_session_list
;
4690 case LTTNG_DOMAIN_UST
:
4691 ust_app_global_update_all_event_notifier_rules();
4693 case LTTNG_DOMAIN_JUL
:
4694 case LTTNG_DOMAIN_LOG4J
:
4695 case LTTNG_DOMAIN_PYTHON
:
4697 /* Agent domains. */
4698 struct agent
*agt
= agent_find_by_event_notifier_domain(
4702 * This trigger was never registered in the first place. Calling
4703 * this function under those circumstances is an internal error.
4706 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4707 if (ret_code
!= LTTNG_OK
) {
4708 goto end_unlock_session_list
;
4713 case LTTNG_DOMAIN_NONE
:
4718 ret_code
= LTTNG_OK
;
4720 end_unlock_session_list
:
4721 session_unlock_list();
4725 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4726 const struct lttng_trigger
*trigger
,
4727 struct notification_thread_handle
*notification_thread
)
4729 enum lttng_error_code ret_code
;
4730 const char *trigger_name
;
4731 uid_t trigger_owner
;
4732 enum lttng_trigger_status trigger_status
;
4733 struct lttng_trigger
*sessiond_trigger
= NULL
;
4735 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4736 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4737 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4738 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4740 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4741 trigger_name
, (int) trigger_owner
,
4742 (int) lttng_credentials_get_uid(cmd_creds
));
4745 * Validate the trigger credentials against the command credentials.
4746 * Only the root user can unregister a trigger with non-matching
4749 if (!lttng_credentials_is_equal_uid(
4750 lttng_trigger_get_credentials(trigger
),
4752 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4753 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4754 trigger_name
, (int) trigger_owner
,
4755 (int) lttng_credentials_get_uid(cmd_creds
));
4756 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4761 /* Fetch the sessiond side trigger object. */
4762 ret_code
= notification_thread_command_get_trigger(
4763 notification_thread
, trigger
, &sessiond_trigger
);
4764 if (ret_code
!= LTTNG_OK
) {
4765 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4766 trigger_name
, (int) trigger_owner
, ret_code
);
4770 LTTNG_ASSERT(sessiond_trigger
);
4773 * From this point on, no matter what, consider the trigger
4776 * We set the unregistered state of the sessiond side trigger object in
4777 * the client thread since we want to minimize the possibility of the
4778 * notification thread being stalled due to a long execution of an
4779 * action that required the trigger lock.
4781 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4783 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4785 if (ret_code
!= LTTNG_OK
) {
4786 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4787 trigger_name
, (int) trigger_owner
, ret_code
);
4792 * Synchronize tracers if the trigger removes an event notifier.
4793 * Do this even if the trigger unregistration failed to at least stop
4794 * the tracers from producing notifications associated with this
4797 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4798 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4799 if (ret_code
!= LTTNG_OK
) {
4800 ERR("Error unregistering trigger to tracer.");
4807 lttng_trigger_put(sessiond_trigger
);
4811 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4812 struct notification_thread_handle
*notification_thread
,
4813 struct lttng_triggers
**return_triggers
)
4816 enum lttng_error_code ret_code
;
4817 struct lttng_triggers
*triggers
= NULL
;
4819 /* Get the set of triggers from the notification thread. */
4820 ret_code
= notification_thread_command_list_triggers(
4821 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4822 if (ret_code
!= LTTNG_OK
) {
4826 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4828 ret_code
= LTTNG_ERR_UNK
;
4832 *return_triggers
= triggers
;
4834 ret_code
= LTTNG_OK
;
4836 lttng_triggers_destroy(triggers
);
4840 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4841 const struct lttng_error_query
*query
,
4842 struct lttng_error_query_results
**_results
,
4843 struct notification_thread_handle
*notification_thread
)
4845 enum lttng_error_code ret_code
;
4846 const struct lttng_trigger
*query_target_trigger
;
4847 const struct lttng_action
*query_target_action
= NULL
;
4848 struct lttng_trigger
*matching_trigger
= NULL
;
4849 const char *trigger_name
;
4850 uid_t trigger_owner
;
4851 enum lttng_trigger_status trigger_status
;
4852 struct lttng_error_query_results
*results
= NULL
;
4854 switch (lttng_error_query_get_target_type(query
)) {
4855 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4856 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4858 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4859 query_target_trigger
=
4860 lttng_error_query_condition_borrow_target(query
);
4862 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4863 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4870 LTTNG_ASSERT(query_target_trigger
);
4872 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4873 query_target_trigger
, &matching_trigger
);
4874 if (ret_code
!= LTTNG_OK
) {
4878 /* No longer needed. */
4879 query_target_trigger
= NULL
;
4881 if (lttng_error_query_get_target_type(query
) ==
4882 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4883 /* Get the sessiond-side version of the target action. */
4884 query_target_action
=
4885 lttng_error_query_action_borrow_action_target(
4886 query
, matching_trigger
);
4889 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4890 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4891 trigger_name
: "(anonymous)";
4892 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4894 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4896 results
= lttng_error_query_results_create();
4898 ret_code
= LTTNG_ERR_NOMEM
;
4902 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4903 trigger_name
, (int) trigger_owner
,
4904 (int) lttng_credentials_get_uid(cmd_creds
));
4907 * Validate the trigger credentials against the command credentials.
4908 * Only the root user can target a trigger with non-matching
4911 if (!lttng_credentials_is_equal_uid(
4912 lttng_trigger_get_credentials(matching_trigger
),
4914 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4915 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4916 trigger_name
, (int) trigger_owner
,
4917 (int) lttng_credentials_get_uid(cmd_creds
));
4918 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4923 switch (lttng_error_query_get_target_type(query
)) {
4924 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4925 trigger_status
= lttng_trigger_add_error_results(
4926 matching_trigger
, results
);
4928 switch (trigger_status
) {
4929 case LTTNG_TRIGGER_STATUS_OK
:
4932 ret_code
= LTTNG_ERR_UNK
;
4937 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4939 trigger_status
= lttng_trigger_condition_add_error_results(
4940 matching_trigger
, results
);
4942 switch (trigger_status
) {
4943 case LTTNG_TRIGGER_STATUS_OK
:
4946 ret_code
= LTTNG_ERR_UNK
;
4952 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4954 const enum lttng_action_status action_status
=
4955 lttng_action_add_error_query_results(
4956 query_target_action
, results
);
4958 switch (action_status
) {
4959 case LTTNG_ACTION_STATUS_OK
:
4962 ret_code
= LTTNG_ERR_UNK
;
4973 *_results
= results
;
4975 ret_code
= LTTNG_OK
;
4977 lttng_trigger_put(matching_trigger
);
4978 lttng_error_query_results_destroy(results
);
4983 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4984 * snapshot output is *not* set with a remote destination.
4986 * Return LTTNG_OK on success or a LTTNG_ERR code.
4988 static enum lttng_error_code
set_relayd_for_snapshot(
4989 struct consumer_output
*output
,
4990 const struct ltt_session
*session
)
4992 enum lttng_error_code status
= LTTNG_OK
;
4993 struct lttng_ht_iter iter
;
4994 struct consumer_socket
*socket
;
4995 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4996 const char *base_path
;
4998 LTTNG_ASSERT(output
);
4999 LTTNG_ASSERT(session
);
5001 DBG2("Set relayd object from snapshot output");
5003 if (session
->current_trace_chunk
) {
5004 enum lttng_trace_chunk_status chunk_status
=
5005 lttng_trace_chunk_get_id(
5006 session
->current_trace_chunk
,
5007 ¤t_chunk_id
.value
);
5009 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5010 current_chunk_id
.is_set
= true;
5012 ERR("Failed to get current trace chunk id");
5013 status
= LTTNG_ERR_UNK
;
5018 /* Ignore if snapshot consumer output is not network. */
5019 if (output
->type
!= CONSUMER_DST_NET
) {
5024 * The snapshot record URI base path overrides the session
5027 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5028 base_path
= output
->dst
.net
.control
.subdir
;
5030 base_path
= session
->base_path
;
5034 * For each consumer socket, create and send the relayd object of the
5038 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
5039 socket
, node
.node
) {
5040 pthread_mutex_lock(socket
->lock
);
5041 status
= send_consumer_relayd_sockets(session
->id
,
5043 session
->name
, session
->hostname
,
5045 session
->live_timer
,
5046 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
5047 session
->creation_time
,
5048 session
->name_contains_creation_time
);
5049 pthread_mutex_unlock(socket
->lock
);
5050 if (status
!= LTTNG_OK
) {
5062 * Record a kernel snapshot.
5064 * Return LTTNG_OK on success or a LTTNG_ERR code.
5066 static enum lttng_error_code
record_kernel_snapshot(
5067 struct ltt_kernel_session
*ksess
,
5068 const struct consumer_output
*output
,
5069 const struct ltt_session
*session
,
5070 uint64_t nb_packets_per_stream
)
5072 enum lttng_error_code status
;
5074 LTTNG_ASSERT(ksess
);
5075 LTTNG_ASSERT(output
);
5076 LTTNG_ASSERT(session
);
5078 status
= kernel_snapshot_record(
5079 ksess
, output
, nb_packets_per_stream
);
5084 * Record a UST snapshot.
5086 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5088 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5089 const struct consumer_output
*output
,
5090 const struct ltt_session
*session
,
5091 uint64_t nb_packets_per_stream
)
5093 enum lttng_error_code status
;
5095 LTTNG_ASSERT(usess
);
5096 LTTNG_ASSERT(output
);
5097 LTTNG_ASSERT(session
);
5099 status
= ust_app_snapshot_record(
5100 usess
, output
, nb_packets_per_stream
);
5105 uint64_t get_session_size_one_more_packet_per_stream(
5106 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5108 uint64_t tot_size
= 0;
5110 if (session
->kernel_session
) {
5111 struct ltt_kernel_channel
*chan
;
5112 const struct ltt_kernel_session
*ksess
=
5113 session
->kernel_session
;
5115 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5116 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5118 * Don't take channel into account if we
5119 * already grab all its packets.
5123 tot_size
+= chan
->channel
->attr
.subbuf_size
5124 * chan
->stream_count
;
5128 if (session
->ust_session
) {
5129 const struct ltt_ust_session
*usess
= session
->ust_session
;
5131 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5139 * Calculate the number of packets we can grab from each stream that
5140 * fits within the overall snapshot max size.
5142 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5143 * the number of packets per stream.
5145 * TODO: this approach is not perfect: we consider the worse case
5146 * (packet filling the sub-buffers) as an upper bound, but we could do
5147 * better if we do this calculation while we actually grab the packet
5148 * content: we would know how much padding we don't actually store into
5151 * This algorithm is currently bounded by the number of packets per
5154 * Since we call this algorithm before actually grabbing the data, it's
5155 * an approximation: for instance, applications could appear/disappear
5156 * in between this call and actually grabbing data.
5159 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5163 uint64_t cur_nb_packets
= 0;
5166 return 0; /* Infinite */
5169 size_left
= max_size
;
5171 uint64_t one_more_packet_tot_size
;
5173 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5174 session
, cur_nb_packets
);
5175 if (!one_more_packet_tot_size
) {
5176 /* We are already grabbing all packets. */
5179 size_left
-= one_more_packet_tot_size
;
5180 if (size_left
< 0) {
5185 if (!cur_nb_packets
&& size_left
!= max_size
) {
5186 /* Not enough room to grab one packet of each stream, error. */
5189 return cur_nb_packets
;
5193 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5194 const struct snapshot_output
*snapshot_output
)
5196 int64_t nb_packets_per_stream
;
5197 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5199 enum lttng_error_code ret_code
= LTTNG_OK
;
5200 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5201 struct consumer_output
*original_ust_consumer_output
= NULL
;
5202 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5203 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5204 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5206 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5208 snapshot_output
->name
,
5209 snapshot_output
->datetime
,
5210 snapshot_output
->nb_snapshot
);
5211 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5212 ERR("Failed to format snapshot name");
5213 ret_code
= LTTNG_ERR_INVALID
;
5216 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5217 snapshot_output
->name
, session
->name
,
5218 snapshot_chunk_name
);
5219 if (!session
->kernel_session
&& !session
->ust_session
) {
5220 ERR("Failed to record snapshot as no channels exist");
5221 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5225 if (session
->kernel_session
) {
5226 original_kernel_consumer_output
=
5227 session
->kernel_session
->consumer
;
5228 snapshot_kernel_consumer_output
=
5229 consumer_copy_output(snapshot_output
->consumer
);
5230 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5231 snapshot_chunk_name
);
5233 /* Copy the original domain subdir. */
5234 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5235 original_kernel_consumer_output
->domain_subdir
);
5237 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5238 original_kernel_consumer_output
);
5240 ERR("Failed to copy consumer sockets from snapshot output configuration");
5241 ret_code
= LTTNG_ERR_NOMEM
;
5244 ret_code
= set_relayd_for_snapshot(
5245 snapshot_kernel_consumer_output
, session
);
5246 if (ret_code
!= LTTNG_OK
) {
5247 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5250 session
->kernel_session
->consumer
=
5251 snapshot_kernel_consumer_output
;
5253 if (session
->ust_session
) {
5254 original_ust_consumer_output
= session
->ust_session
->consumer
;
5255 snapshot_ust_consumer_output
=
5256 consumer_copy_output(snapshot_output
->consumer
);
5257 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5258 snapshot_chunk_name
);
5260 /* Copy the original domain subdir. */
5261 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5262 original_ust_consumer_output
->domain_subdir
);
5264 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5265 original_ust_consumer_output
);
5267 ERR("Failed to copy consumer sockets from snapshot output configuration");
5268 ret_code
= LTTNG_ERR_NOMEM
;
5271 ret_code
= set_relayd_for_snapshot(
5272 snapshot_ust_consumer_output
, session
);
5273 if (ret_code
!= LTTNG_OK
) {
5274 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5277 session
->ust_session
->consumer
=
5278 snapshot_ust_consumer_output
;
5281 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5282 snapshot_kernel_consumer_output
?:
5283 snapshot_ust_consumer_output
,
5284 consumer_output_get_base_path(
5285 snapshot_output
->consumer
),
5286 snapshot_chunk_name
);
5287 if (!snapshot_trace_chunk
) {
5288 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5290 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5293 LTTNG_ASSERT(!session
->current_trace_chunk
);
5294 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5295 lttng_trace_chunk_put(snapshot_trace_chunk
);
5296 snapshot_trace_chunk
= NULL
;
5298 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5300 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5304 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5305 snapshot_output
->max_size
);
5306 if (nb_packets_per_stream
< 0) {
5307 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5308 goto error_close_trace_chunk
;
5311 if (session
->kernel_session
) {
5312 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5313 snapshot_kernel_consumer_output
, session
,
5314 nb_packets_per_stream
);
5315 if (ret_code
!= LTTNG_OK
) {
5316 goto error_close_trace_chunk
;
5320 if (session
->ust_session
) {
5321 ret_code
= record_ust_snapshot(session
->ust_session
,
5322 snapshot_ust_consumer_output
, session
,
5323 nb_packets_per_stream
);
5324 if (ret_code
!= LTTNG_OK
) {
5325 goto error_close_trace_chunk
;
5329 error_close_trace_chunk
:
5330 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5331 ERR("Failed to release the current trace chunk of session \"%s\"",
5333 ret_code
= LTTNG_ERR_UNK
;
5336 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5337 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5339 * Don't goto end; make sure the chunk is closed for the session
5340 * to allow future snapshots.
5342 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5344 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5347 lttng_trace_chunk_put(snapshot_trace_chunk
);
5348 snapshot_trace_chunk
= NULL
;
5350 if (original_ust_consumer_output
) {
5351 session
->ust_session
->consumer
= original_ust_consumer_output
;
5353 if (original_kernel_consumer_output
) {
5354 session
->kernel_session
->consumer
=
5355 original_kernel_consumer_output
;
5357 consumer_output_put(snapshot_ust_consumer_output
);
5358 consumer_output_put(snapshot_kernel_consumer_output
);
5363 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5365 * The wait parameter is ignored so this call always wait for the snapshot to
5366 * complete before returning.
5368 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5370 int cmd_snapshot_record(struct ltt_session
*session
,
5371 const struct lttng_snapshot_output
*output
,
5372 int wait
__attribute__((unused
)))
5374 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5376 unsigned int snapshot_success
= 0;
5378 struct snapshot_output
*tmp_output
= NULL
;
5380 LTTNG_ASSERT(session
);
5381 LTTNG_ASSERT(output
);
5383 DBG("Cmd snapshot record for session %s", session
->name
);
5385 /* Get the datetime for the snapshot output directory. */
5386 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5389 cmd_ret
= LTTNG_ERR_INVALID
;
5394 * Permission denied to create an output if the session is not
5395 * set in no output mode.
5397 if (session
->output_traces
) {
5398 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5402 /* The session needs to be started at least once. */
5403 if (!session
->has_been_started
) {
5404 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5408 /* Use temporary output for the session. */
5409 if (*output
->ctrl_url
!= '\0') {
5410 tmp_output
= snapshot_output_alloc();
5412 cmd_ret
= LTTNG_ERR_NOMEM
;
5416 ret
= snapshot_output_init(session
, output
->max_size
,
5418 output
->ctrl_url
, output
->data_url
,
5422 if (ret
== -ENOMEM
) {
5423 cmd_ret
= LTTNG_ERR_NOMEM
;
5425 cmd_ret
= LTTNG_ERR_INVALID
;
5429 /* Use the global session count for the temporary snapshot. */
5430 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5432 /* Use the global datetime */
5433 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5434 cmd_ret
= snapshot_record(session
, tmp_output
);
5435 if (cmd_ret
!= LTTNG_OK
) {
5438 snapshot_success
= 1;
5440 struct snapshot_output
*sout
;
5441 struct lttng_ht_iter iter
;
5444 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5445 &iter
.iter
, sout
, node
.node
) {
5446 struct snapshot_output output_copy
;
5449 * Make a local copy of the output and override output
5450 * parameters with those provided as part of the
5453 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5455 if (output
->max_size
!= (uint64_t) -1ULL) {
5456 output_copy
.max_size
= output
->max_size
;
5459 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5460 memcpy(output_copy
.datetime
, datetime
,
5463 /* Use temporary name. */
5464 if (*output
->name
!= '\0') {
5465 if (lttng_strncpy(output_copy
.name
,
5467 sizeof(output_copy
.name
))) {
5468 cmd_ret
= LTTNG_ERR_INVALID
;
5474 cmd_ret
= snapshot_record(session
, &output_copy
);
5475 if (cmd_ret
!= LTTNG_OK
) {
5479 snapshot_success
= 1;
5484 if (snapshot_success
) {
5485 session
->snapshot
.nb_snapshot
++;
5487 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5492 snapshot_output_destroy(tmp_output
);
5498 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5500 int cmd_set_session_shm_path(struct ltt_session
*session
,
5501 const char *shm_path
)
5504 LTTNG_ASSERT(session
);
5507 * Can only set shm path before session is started.
5509 if (session
->has_been_started
) {
5510 return LTTNG_ERR_SESSION_STARTED
;
5513 strncpy(session
->shm_path
, shm_path
,
5514 sizeof(session
->shm_path
));
5515 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5521 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5523 * Ask the consumer to rotate the session output directory.
5524 * The session lock must be held.
5526 * Returns LTTNG_OK on success or else a negative LTTng error code.
5528 int cmd_rotate_session(struct ltt_session
*session
,
5529 struct lttng_rotate_session_return
*rotate_return
,
5530 bool quiet_rotation
,
5531 enum lttng_trace_chunk_command_type command
)
5534 uint64_t ongoing_rotation_chunk_id
;
5535 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5536 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5537 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5538 enum lttng_trace_chunk_status chunk_status
;
5539 bool failed_to_rotate
= false;
5540 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5542 LTTNG_ASSERT(session
);
5544 if (!session
->has_been_started
) {
5545 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5550 * Explicit rotation is not supported for live sessions.
5551 * However, live sessions can perform a quiet rotation on
5553 * Rotation is not supported for snapshot traces (no output).
5555 if ((!quiet_rotation
&& session
->live_timer
) ||
5556 !session
->output_traces
) {
5557 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5561 /* Unsupported feature in lttng-relayd before 2.11. */
5562 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5563 (session
->consumer
->relay_major_version
== 2 &&
5564 session
->consumer
->relay_minor_version
< 11)) {
5565 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5569 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5570 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5571 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5575 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5576 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5578 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5583 * After a stop, we only allow one rotation to occur, the other ones are
5584 * useless until a new start.
5586 if (session
->rotated_after_last_stop
) {
5587 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5589 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5594 * After a stop followed by a clear, disallow following rotations a they would
5595 * generate empty chunks.
5597 if (session
->cleared_after_last_stop
) {
5598 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5600 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5604 if (session
->active
) {
5605 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5607 if (!new_trace_chunk
) {
5608 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5614 * The current trace chunk becomes the chunk being archived.
5616 * After this point, "chunk_being_archived" must absolutely
5617 * be closed on the consumer(s), otherwise it will never be
5618 * cleaned-up, which will result in a leak.
5620 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5621 &chunk_being_archived
);
5623 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5627 if (session
->kernel_session
) {
5628 cmd_ret
= kernel_rotate_session(session
);
5629 if (cmd_ret
!= LTTNG_OK
) {
5630 failed_to_rotate
= true;
5631 rotation_fail_code
= cmd_ret
;
5634 if (session
->ust_session
) {
5635 cmd_ret
= ust_app_rotate_session(session
);
5636 if (cmd_ret
!= LTTNG_OK
) {
5637 failed_to_rotate
= true;
5638 rotation_fail_code
= cmd_ret
;
5642 if (!session
->active
) {
5643 session
->rotated_after_last_stop
= true;
5646 if (!chunk_being_archived
) {
5647 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5649 if (failed_to_rotate
) {
5650 cmd_ret
= rotation_fail_code
;
5657 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5658 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5659 &ongoing_rotation_chunk_id
);
5660 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5662 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5663 command
, session
->last_chunk_path
);
5665 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5669 if (failed_to_rotate
) {
5670 cmd_ret
= rotation_fail_code
;
5674 session
->quiet_rotation
= quiet_rotation
;
5675 ret
= timer_session_rotation_pending_check_start(session
,
5676 DEFAULT_ROTATE_PENDING_TIMER
);
5678 cmd_ret
= LTTNG_ERR_UNK
;
5682 if (rotate_return
) {
5683 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5686 session
->chunk_being_archived
= chunk_being_archived
;
5687 chunk_being_archived
= NULL
;
5688 if (!quiet_rotation
) {
5689 ret
= notification_thread_command_session_rotation_ongoing(
5690 the_notification_thread_handle
, session
->name
,
5691 session
->uid
, session
->gid
,
5692 ongoing_rotation_chunk_id
);
5693 if (ret
!= LTTNG_OK
) {
5694 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5696 cmd_ret
= (lttng_error_code
) ret
;
5700 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5701 session
->name
, ongoing_rotation_chunk_id
);
5703 lttng_trace_chunk_put(new_trace_chunk
);
5704 lttng_trace_chunk_put(chunk_being_archived
);
5705 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5708 if (session_reset_rotation_state(session
,
5709 LTTNG_ROTATION_STATE_ERROR
)) {
5710 ERR("Failed to reset rotation state of session \"%s\"",
5717 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5719 * Check if the session has finished its rotation.
5721 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5723 int cmd_rotate_get_info(struct ltt_session
*session
,
5724 struct lttng_rotation_get_info_return
*info_return
,
5725 uint64_t rotation_id
)
5727 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5728 enum lttng_rotation_state rotation_state
;
5730 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5731 session
->most_recent_chunk_id
.value
);
5733 if (session
->chunk_being_archived
) {
5734 enum lttng_trace_chunk_status chunk_status
;
5737 chunk_status
= lttng_trace_chunk_get_id(
5738 session
->chunk_being_archived
,
5740 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5742 rotation_state
= rotation_id
== chunk_id
?
5743 LTTNG_ROTATION_STATE_ONGOING
:
5744 LTTNG_ROTATION_STATE_EXPIRED
;
5746 if (session
->last_archived_chunk_id
.is_set
&&
5747 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5748 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5750 rotation_state
= session
->rotation_state
;
5754 switch (rotation_state
) {
5755 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5756 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5759 case LTTNG_ROTATION_STATE_EXPIRED
:
5760 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5761 rotation_id
, session
->name
);
5763 case LTTNG_ROTATION_STATE_ONGOING
:
5764 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5765 rotation_id
, session
->name
);
5767 case LTTNG_ROTATION_STATE_COMPLETED
:
5771 char *current_tracing_path_reply
;
5772 size_t current_tracing_path_reply_len
;
5774 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5775 rotation_id
, session
->name
);
5777 switch (session_get_consumer_destination_type(session
)) {
5778 case CONSUMER_DST_LOCAL
:
5779 current_tracing_path_reply
=
5780 info_return
->location
.local
.absolute_path
;
5781 current_tracing_path_reply_len
=
5782 sizeof(info_return
->location
.local
.absolute_path
);
5783 info_return
->location_type
=
5784 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5785 fmt_ret
= asprintf(&chunk_path
,
5786 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5787 session_get_base_path(session
),
5788 session
->last_archived_chunk_name
);
5789 if (fmt_ret
== -1) {
5790 PERROR("Failed to format the path of the last archived trace chunk");
5791 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5792 cmd_ret
= LTTNG_ERR_UNK
;
5796 case CONSUMER_DST_NET
:
5798 uint16_t ctrl_port
, data_port
;
5800 current_tracing_path_reply
=
5801 info_return
->location
.relay
.relative_path
;
5802 current_tracing_path_reply_len
=
5803 sizeof(info_return
->location
.relay
.relative_path
);
5804 /* Currently the only supported relay protocol. */
5805 info_return
->location
.relay
.protocol
=
5806 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5808 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5809 session_get_net_consumer_hostname(session
),
5810 sizeof(info_return
->location
.relay
.host
));
5812 ERR("Failed to copy host name to rotate_get_info reply");
5813 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5814 cmd_ret
= LTTNG_ERR_SET_URL
;
5818 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5819 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5820 info_return
->location
.relay
.ports
.data
= data_port
;
5821 info_return
->location_type
=
5822 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5823 chunk_path
= strdup(session
->last_chunk_path
);
5825 ERR("Failed to allocate the path of the last archived trace chunk");
5826 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5827 cmd_ret
= LTTNG_ERR_UNK
;
5836 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5837 chunk_path
, current_tracing_path_reply_len
);
5840 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5841 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5842 cmd_ret
= LTTNG_ERR_UNK
;
5848 case LTTNG_ROTATION_STATE_ERROR
:
5849 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5850 rotation_id
, session
->name
);
5858 info_return
->status
= (int32_t) rotation_state
;
5863 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5865 * Configure the automatic rotation parameters.
5866 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5867 * 'activate' to false means deactivate the rotation schedule and validate that
5868 * 'new_value' has the same value as the currently active value.
5870 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5872 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5873 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5875 struct notification_thread_handle
*notification_thread_handle
)
5878 uint64_t *parameter_value
;
5880 LTTNG_ASSERT(session
);
5882 DBG("Cmd rotate set schedule session %s", session
->name
);
5884 if (session
->live_timer
|| !session
->output_traces
) {
5885 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5886 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5890 switch (schedule_type
) {
5891 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5892 parameter_value
= &session
->rotate_size
;
5894 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5895 parameter_value
= &session
->rotate_timer_period
;
5896 if (new_value
>= UINT_MAX
) {
5897 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5898 new_value
, UINT_MAX
);
5899 ret
= LTTNG_ERR_INVALID
;
5904 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5905 ret
= LTTNG_ERR_INVALID
;
5909 /* Improper use of the API. */
5910 if (new_value
== -1ULL) {
5911 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5912 ret
= LTTNG_ERR_INVALID
;
5917 * As indicated in struct ltt_session's comments, a value of == 0 means
5918 * this schedule rotation type is not in use.
5920 * Reject the command if we were asked to activate a schedule that was
5923 if (activate
&& *parameter_value
!= 0) {
5924 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5925 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5930 * Reject the command if we were asked to deactivate a schedule that was
5933 if (!activate
&& *parameter_value
== 0) {
5934 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5935 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5940 * Reject the command if we were asked to deactivate a schedule that
5943 if (!activate
&& *parameter_value
!= new_value
) {
5944 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5945 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5949 *parameter_value
= activate
? new_value
: 0;
5951 switch (schedule_type
) {
5952 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5953 if (activate
&& session
->active
) {
5955 * Only start the timer if the session is active,
5956 * otherwise it will be started when the session starts.
5958 ret
= timer_session_rotation_schedule_timer_start(
5959 session
, new_value
);
5961 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5962 ret
= LTTNG_ERR_UNK
;
5966 ret
= timer_session_rotation_schedule_timer_stop(
5969 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5970 ret
= LTTNG_ERR_UNK
;
5975 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5977 ret
= subscribe_session_consumed_size_rotation(session
,
5978 new_value
, notification_thread_handle
);
5980 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5981 ret
= LTTNG_ERR_UNK
;
5985 ret
= unsubscribe_session_consumed_size_rotation(session
,
5986 notification_thread_handle
);
5988 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5989 ret
= LTTNG_ERR_UNK
;
5996 /* Would have been caught before. */
6008 /* Wait for a given path to be removed before continuing. */
6009 static enum lttng_error_code
wait_on_path(void *path_data
)
6011 const char *shm_path
= (const char *) path_data
;
6013 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6019 ret
= stat(shm_path
, &st
);
6021 if (errno
!= ENOENT
) {
6022 PERROR("stat() returned an error while checking for the existence of the shm path");
6024 DBG("shm path no longer exists, completing the destruction of session");
6028 if (!S_ISDIR(st
.st_mode
)) {
6029 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6034 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6040 * Returns a pointer to a handler to run on completion of a command.
6041 * Returns NULL if no handler has to be run for the last command executed.
6043 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6045 struct cmd_completion_handler
*handler
= current_completion_handler
;
6047 current_completion_handler
= NULL
;
6052 * Init command subsystem.
6057 * Set network sequence index to 1 for streams to match a relayd
6058 * socket on the consumer side.
6060 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6061 relayd_net_seq_idx
= 1;
6062 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6064 DBG("Command subsystem initialized");