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
];
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 assert(discarded_events
);
253 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");
314 agent_event_count
= lttng_ht_get_count(agt
->events
);
315 if (agent_event_count
== 0) {
320 if (agent_event_count
> UINT_MAX
) {
321 ret_code
= LTTNG_ERR_OVERFLOW
;
325 local_nb_events
= (unsigned int) agent_event_count
;
327 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
328 struct lttng_event
*tmp_event
= lttng_event_create();
331 ret_code
= LTTNG_ERR_NOMEM
;
335 if (lttng_strncpy(tmp_event
->name
, event
->name
, sizeof(tmp_event
->name
))) {
336 lttng_event_destroy(tmp_event
);
337 ret_code
= LTTNG_ERR_FATAL
;
341 tmp_event
->name
[sizeof(tmp_event
->name
) - 1] = '\0';
342 tmp_event
->enabled
= !!event
->enabled_count
;
343 tmp_event
->loglevel
= event
->loglevel_value
;
344 tmp_event
->loglevel_type
= event
->loglevel_type
;
346 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
347 event
->filter_expression
, 0, NULL
, reply_payload
);
348 lttng_event_destroy(tmp_event
);
350 ret_code
= LTTNG_ERR_FATAL
;
357 *nb_events
= local_nb_events
;
364 * Create a list of ust global domain events.
366 static enum lttng_error_code
list_lttng_ust_global_events(char *channel_name
,
367 struct ltt_ust_domain_global
*ust_global
,
368 struct lttng_payload
*reply_payload
,
369 unsigned int *nb_events
)
371 enum lttng_error_code ret_code
;
373 struct lttng_ht_iter iter
;
374 struct lttng_ht_node_str
*node
;
375 struct ltt_ust_channel
*uchan
;
376 struct ltt_ust_event
*uevent
;
377 unsigned long channel_event_count
;
378 unsigned int local_nb_events
= 0;
380 assert(reply_payload
);
383 DBG("Listing UST global events for channel %s", channel_name
);
387 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
388 node
= lttng_ht_iter_get_node_str(&iter
);
390 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
394 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
396 channel_event_count
= lttng_ht_get_count(uchan
->events
);
397 if (channel_event_count
== 0) {
403 if (channel_event_count
> UINT_MAX
) {
404 ret_code
= LTTNG_ERR_OVERFLOW
;
408 local_nb_events
= (unsigned int) channel_event_count
;
410 DBG3("Listing UST global %d events", *nb_events
);
412 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
413 struct lttng_event
*tmp_event
= NULL
;
415 if (uevent
->internal
) {
416 /* This event should remain hidden from clients */
421 tmp_event
= lttng_event_create();
423 ret_code
= LTTNG_ERR_NOMEM
;
427 if (lttng_strncpy(tmp_event
->name
, uevent
->attr
.name
,
428 LTTNG_SYMBOL_NAME_LEN
)) {
429 ret_code
= LTTNG_ERR_FATAL
;
430 lttng_event_destroy(tmp_event
);
434 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
435 tmp_event
->enabled
= uevent
->enabled
;
437 switch (uevent
->attr
.instrumentation
) {
438 case LTTNG_UST_ABI_TRACEPOINT
:
439 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
441 case LTTNG_UST_ABI_PROBE
:
442 tmp_event
->type
= LTTNG_EVENT_PROBE
;
444 case LTTNG_UST_ABI_FUNCTION
:
445 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
449 tmp_event
->loglevel
= uevent
->attr
.loglevel
;
450 switch (uevent
->attr
.loglevel_type
) {
451 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
452 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
454 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
455 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
457 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
458 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
461 if (uevent
->filter
) {
462 tmp_event
->filter
= 1;
464 if (uevent
->exclusion
) {
465 tmp_event
->exclusion
= 1;
469 * We do not care about the filter bytecode and the fd from the
470 * userspace_probe_location.
472 ret
= lttng_event_serialize(tmp_event
, uevent
->exclusion
? uevent
->exclusion
->count
: 0,
473 uevent
->exclusion
? (char **) uevent
->exclusion
->names
: NULL
,
474 uevent
->filter_expression
, 0, NULL
, reply_payload
);
475 lttng_event_destroy(tmp_event
);
477 ret_code
= LTTNG_ERR_FATAL
;
483 /* nb_events is already set at this point. */
485 *nb_events
= local_nb_events
;
492 * Fill lttng_event array of all kernel events in the channel.
494 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
495 struct ltt_kernel_session
*kernel_session
,
496 struct lttng_payload
*reply_payload
,
497 unsigned int *nb_events
)
499 enum lttng_error_code ret_code
;
501 struct ltt_kernel_event
*event
;
502 struct ltt_kernel_channel
*kchan
;
504 assert(reply_payload
);
506 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
508 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
512 *nb_events
= kchan
->event_count
;
514 DBG("Listing events for channel %s", kchan
->channel
->name
);
516 if (*nb_events
== 0) {
521 /* Kernel channels */
522 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
523 struct lttng_event
*tmp_event
= lttng_event_create();
526 ret_code
= LTTNG_ERR_NOMEM
;
530 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
531 lttng_event_destroy(tmp_event
);
532 ret_code
= LTTNG_ERR_FATAL
;
537 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
538 tmp_event
->enabled
= event
->enabled
;
539 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
541 switch (event
->event
->instrumentation
) {
542 case LTTNG_KERNEL_ABI_TRACEPOINT
:
543 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
545 case LTTNG_KERNEL_ABI_KRETPROBE
:
546 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
547 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
548 sizeof(struct lttng_kernel_abi_kprobe
));
550 case LTTNG_KERNEL_ABI_KPROBE
:
551 tmp_event
->type
= LTTNG_EVENT_PROBE
;
552 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
553 sizeof(struct lttng_kernel_abi_kprobe
));
555 case LTTNG_KERNEL_ABI_UPROBE
:
556 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
558 case LTTNG_KERNEL_ABI_FUNCTION
:
559 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
560 memcpy(&(tmp_event
->attr
.ftrace
), &event
->event
->u
.ftrace
,
561 sizeof(struct lttng_kernel_abi_function
));
563 case LTTNG_KERNEL_ABI_NOOP
:
564 tmp_event
->type
= LTTNG_EVENT_NOOP
;
566 case LTTNG_KERNEL_ABI_SYSCALL
:
567 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
569 case LTTNG_KERNEL_ABI_ALL
:
576 if (event
->userspace_probe_location
) {
577 struct lttng_userspace_probe_location
*location_copy
=
578 lttng_userspace_probe_location_copy(
579 event
->userspace_probe_location
);
581 if (!location_copy
) {
582 lttng_event_destroy(tmp_event
);
583 ret_code
= LTTNG_ERR_NOMEM
;
587 ret
= lttng_event_set_userspace_probe_location(
588 tmp_event
, location_copy
);
590 lttng_event_destroy(tmp_event
);
591 lttng_userspace_probe_location_destroy(
593 ret_code
= LTTNG_ERR_INVALID
;
598 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
599 event
->filter_expression
, 0, NULL
, reply_payload
);
600 lttng_event_destroy(tmp_event
);
602 ret_code
= LTTNG_ERR_FATAL
;
613 * Add URI so the consumer output object. Set the correct path depending on the
614 * domain adding the default trace directory.
616 static enum lttng_error_code
add_uri_to_consumer(
617 const struct ltt_session
*session
,
618 struct consumer_output
*consumer
,
619 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
622 enum lttng_error_code ret_code
= LTTNG_OK
;
626 if (consumer
== NULL
) {
627 DBG("No consumer detected. Don't add URI. Stopping.");
628 ret_code
= LTTNG_ERR_NO_CONSUMER
;
633 case LTTNG_DOMAIN_KERNEL
:
634 ret
= lttng_strncpy(consumer
->domain_subdir
,
635 DEFAULT_KERNEL_TRACE_DIR
,
636 sizeof(consumer
->domain_subdir
));
638 case LTTNG_DOMAIN_UST
:
639 ret
= lttng_strncpy(consumer
->domain_subdir
,
640 DEFAULT_UST_TRACE_DIR
,
641 sizeof(consumer
->domain_subdir
));
645 * This case is possible is we try to add the URI to the global
646 * tracing session consumer object which in this case there is
649 memset(consumer
->domain_subdir
, 0,
650 sizeof(consumer
->domain_subdir
));
654 ERR("Failed to initialize consumer output domain subdirectory");
655 ret_code
= LTTNG_ERR_FATAL
;
659 switch (uri
->dtype
) {
662 DBG2("Setting network URI to consumer");
664 if (consumer
->type
== CONSUMER_DST_NET
) {
665 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
666 consumer
->dst
.net
.control_isset
) ||
667 (uri
->stype
== LTTNG_STREAM_DATA
&&
668 consumer
->dst
.net
.data_isset
)) {
669 ret_code
= LTTNG_ERR_URL_EXIST
;
673 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
676 /* Set URI into consumer output object */
677 ret
= consumer_set_network_uri(session
, consumer
, uri
);
681 } else if (ret
== 1) {
683 * URI was the same in the consumer so we do not append the subdir
684 * again so to not duplicate output dir.
691 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
692 ret_code
= LTTNG_ERR_INVALID
;
695 DBG2("Setting trace directory path from URI to %s",
697 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
699 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
701 sizeof(consumer
->dst
.session_root_path
));
703 ret_code
= LTTNG_ERR_FATAL
;
706 consumer
->type
= CONSUMER_DST_LOCAL
;
716 * Init tracing by creating trace directory and sending fds kernel consumer.
718 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
721 struct lttng_ht_iter iter
;
722 struct consumer_socket
*socket
;
728 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
729 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
731 pthread_mutex_lock(socket
->lock
);
732 ret
= kernel_consumer_send_session(socket
, session
);
733 pthread_mutex_unlock(socket
->lock
);
735 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
747 * Create a socket to the relayd using the URI.
749 * On success, the relayd_sock pointer is set to the created socket.
750 * Else, it remains untouched and an LTTng error code is returned.
752 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
753 struct lttcomm_relayd_sock
**relayd_sock
,
754 struct consumer_output
*consumer
)
757 enum lttng_error_code status
= LTTNG_OK
;
758 struct lttcomm_relayd_sock
*rsock
;
760 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
761 RELAYD_VERSION_COMM_MINOR
);
763 status
= LTTNG_ERR_FATAL
;
768 * Connect to relayd so we can proceed with a session creation. This call
769 * can possibly block for an arbitrary amount of time to set the health
770 * state to be in poll execution.
773 ret
= relayd_connect(rsock
);
776 ERR("Unable to reach lttng-relayd");
777 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
781 /* Create socket for control stream. */
782 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
783 uint64_t result_flags
;
785 DBG3("Creating relayd stream socket from URI");
787 /* Check relayd version */
788 ret
= relayd_version_check(rsock
);
789 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
790 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
792 } else if (ret
< 0) {
793 ERR("Unable to reach lttng-relayd");
794 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
797 consumer
->relay_major_version
= rsock
->major
;
798 consumer
->relay_minor_version
= rsock
->minor
;
799 ret
= relayd_get_configuration(rsock
, 0,
802 ERR("Unable to get relayd configuration");
803 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
806 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
807 consumer
->relay_allows_clear
= true;
809 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
810 DBG3("Creating relayd data socket from URI");
812 /* Command is not valid */
813 ERR("Relayd invalid stream type: %d", uri
->stype
);
814 status
= LTTNG_ERR_INVALID
;
818 *relayd_sock
= rsock
;
823 /* The returned value is not useful since we are on an error path. */
824 (void) relayd_close(rsock
);
832 * Connect to the relayd using URI and send the socket to the right consumer.
834 * The consumer socket lock must be held by the caller.
836 * Returns LTTNG_OK on success or an LTTng error code on failure.
838 static enum lttng_error_code
send_consumer_relayd_socket(
839 unsigned int session_id
,
840 struct lttng_uri
*relayd_uri
,
841 struct consumer_output
*consumer
,
842 struct consumer_socket
*consumer_sock
,
843 const char *session_name
, const char *hostname
,
844 const char *base_path
, int session_live_timer
,
845 const uint64_t *current_chunk_id
,
846 time_t session_creation_time
,
847 bool session_name_contains_creation_time
)
850 struct lttcomm_relayd_sock
*rsock
= NULL
;
851 enum lttng_error_code status
;
853 /* Connect to relayd and make version check if uri is the control. */
854 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
855 if (status
!= LTTNG_OK
) {
856 goto relayd_comm_error
;
860 /* Set the network sequence index if not set. */
861 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
862 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
864 * Increment net_seq_idx because we are about to transfer the
865 * new relayd socket to the consumer.
866 * Assign unique key so the consumer can match streams.
868 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
869 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
872 /* Send relayd socket to consumer. */
873 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
874 relayd_uri
->stype
, session_id
,
875 session_name
, hostname
, base_path
,
876 session_live_timer
, current_chunk_id
,
877 session_creation_time
, session_name_contains_creation_time
);
879 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
883 /* Flag that the corresponding socket was sent. */
884 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
885 consumer_sock
->control_sock_sent
= 1;
886 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
887 consumer_sock
->data_sock_sent
= 1;
891 * Close socket which was dup on the consumer side. The session daemon does
892 * NOT keep track of the relayd socket(s) once transfer to the consumer.
896 if (status
!= LTTNG_OK
) {
898 * The consumer output for this session should not be used anymore
899 * since the relayd connection failed thus making any tracing or/and
900 * streaming not usable.
902 consumer
->enabled
= 0;
904 (void) relayd_close(rsock
);
912 * Send both relayd sockets to a specific consumer and domain. This is a
913 * helper function to facilitate sending the information to the consumer for a
916 * The consumer socket lock must be held by the caller.
918 * Returns LTTNG_OK, or an LTTng error code on failure.
920 static enum lttng_error_code
send_consumer_relayd_sockets(
921 enum lttng_domain_type domain
,
922 unsigned int session_id
, struct consumer_output
*consumer
,
923 struct consumer_socket
*sock
, const char *session_name
,
924 const char *hostname
, const char *base_path
, int session_live_timer
,
925 const uint64_t *current_chunk_id
, time_t session_creation_time
,
926 bool session_name_contains_creation_time
)
928 enum lttng_error_code status
= LTTNG_OK
;
933 /* Sending control relayd socket. */
934 if (!sock
->control_sock_sent
) {
935 status
= send_consumer_relayd_socket(session_id
,
936 &consumer
->dst
.net
.control
, consumer
, sock
,
937 session_name
, hostname
, base_path
, session_live_timer
,
938 current_chunk_id
, session_creation_time
,
939 session_name_contains_creation_time
);
940 if (status
!= LTTNG_OK
) {
945 /* Sending data relayd socket. */
946 if (!sock
->data_sock_sent
) {
947 status
= send_consumer_relayd_socket(session_id
,
948 &consumer
->dst
.net
.data
, consumer
, sock
,
949 session_name
, hostname
, base_path
, session_live_timer
,
950 current_chunk_id
, session_creation_time
,
951 session_name_contains_creation_time
);
952 if (status
!= LTTNG_OK
) {
962 * Setup relayd connections for a tracing session. First creates the socket to
963 * the relayd and send them to the right domain consumer. Consumer type MUST be
966 int cmd_setup_relayd(struct ltt_session
*session
)
969 struct ltt_ust_session
*usess
;
970 struct ltt_kernel_session
*ksess
;
971 struct consumer_socket
*socket
;
972 struct lttng_ht_iter iter
;
973 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
977 usess
= session
->ust_session
;
978 ksess
= session
->kernel_session
;
980 DBG("Setting relayd for session %s", session
->name
);
983 if (session
->current_trace_chunk
) {
984 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
985 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
987 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
988 current_chunk_id
.is_set
= true;
990 ERR("Failed to get current trace chunk id");
996 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
997 && usess
->consumer
->enabled
) {
998 /* For each consumer socket, send relayd sockets */
999 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1000 socket
, node
.node
) {
1001 pthread_mutex_lock(socket
->lock
);
1002 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1003 usess
->consumer
, socket
,
1004 session
->name
, session
->hostname
,
1006 session
->live_timer
,
1007 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1008 session
->creation_time
,
1009 session
->name_contains_creation_time
);
1010 pthread_mutex_unlock(socket
->lock
);
1011 if (ret
!= LTTNG_OK
) {
1014 /* Session is now ready for network streaming. */
1015 session
->net_handle
= 1;
1017 session
->consumer
->relay_major_version
=
1018 usess
->consumer
->relay_major_version
;
1019 session
->consumer
->relay_minor_version
=
1020 usess
->consumer
->relay_minor_version
;
1021 session
->consumer
->relay_allows_clear
=
1022 usess
->consumer
->relay_allows_clear
;
1025 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1026 && ksess
->consumer
->enabled
) {
1027 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1028 socket
, node
.node
) {
1029 pthread_mutex_lock(socket
->lock
);
1030 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1031 ksess
->consumer
, socket
,
1032 session
->name
, session
->hostname
,
1034 session
->live_timer
,
1035 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1036 session
->creation_time
,
1037 session
->name_contains_creation_time
);
1038 pthread_mutex_unlock(socket
->lock
);
1039 if (ret
!= LTTNG_OK
) {
1042 /* Session is now ready for network streaming. */
1043 session
->net_handle
= 1;
1045 session
->consumer
->relay_major_version
=
1046 ksess
->consumer
->relay_major_version
;
1047 session
->consumer
->relay_minor_version
=
1048 ksess
->consumer
->relay_minor_version
;
1049 session
->consumer
->relay_allows_clear
=
1050 ksess
->consumer
->relay_allows_clear
;
1059 * Start a kernel session by opening all necessary streams.
1061 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1064 struct ltt_kernel_channel
*kchan
;
1066 /* Open kernel metadata */
1067 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1068 ret
= kernel_open_metadata(ksess
);
1070 ret
= LTTNG_ERR_KERN_META_FAIL
;
1075 /* Open kernel metadata stream */
1076 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1077 ret
= kernel_open_metadata_stream(ksess
);
1079 ERR("Kernel create metadata stream failed");
1080 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1085 /* For each channel */
1086 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1087 if (kchan
->stream_count
== 0) {
1088 ret
= kernel_open_channel_stream(kchan
);
1090 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1093 /* Update the stream global counter */
1094 ksess
->stream_count_global
+= ret
;
1098 /* Setup kernel consumer socket and send fds to it */
1099 ret
= init_kernel_tracing(ksess
);
1101 ret
= LTTNG_ERR_KERN_START_FAIL
;
1105 /* This start the kernel tracing */
1106 ret
= kernel_start_session(ksess
);
1108 ret
= LTTNG_ERR_KERN_START_FAIL
;
1112 /* Quiescent wait after starting trace */
1113 kernel_wait_quiescent();
1123 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1125 struct ltt_kernel_channel
*kchan
;
1126 bool error_occurred
= false;
1129 if (!ksess
|| !ksess
->active
) {
1132 DBG("Stopping kernel tracing");
1134 ret
= kernel_stop_session(ksess
);
1136 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1140 kernel_wait_quiescent();
1142 /* Flush metadata after stopping (if exists) */
1143 if (ksess
->metadata_stream_fd
>= 0) {
1144 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1146 ERR("Kernel metadata flush failed");
1147 error_occurred
= true;
1151 /* Flush all buffers after stopping */
1152 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1153 ret
= kernel_flush_buffer(kchan
);
1155 ERR("Kernel flush buffer error");
1156 error_occurred
= true;
1161 if (error_occurred
) {
1162 ret
= LTTNG_ERR_UNK
;
1171 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1173 int cmd_disable_channel(struct ltt_session
*session
,
1174 enum lttng_domain_type domain
, char *channel_name
)
1177 struct ltt_ust_session
*usess
;
1179 usess
= session
->ust_session
;
1184 case LTTNG_DOMAIN_KERNEL
:
1186 ret
= channel_kernel_disable(session
->kernel_session
,
1188 if (ret
!= LTTNG_OK
) {
1192 kernel_wait_quiescent();
1195 case LTTNG_DOMAIN_UST
:
1197 struct ltt_ust_channel
*uchan
;
1198 struct lttng_ht
*chan_ht
;
1200 chan_ht
= usess
->domain_global
.channels
;
1202 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1203 if (uchan
== NULL
) {
1204 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1208 ret
= channel_ust_disable(usess
, uchan
);
1209 if (ret
!= LTTNG_OK
) {
1215 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1227 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1229 * The wpipe arguments is used as a notifier for the kernel thread.
1231 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1235 ssize_t sock_recv_len
;
1236 struct lttng_channel
*channel
= NULL
;
1237 struct lttng_buffer_view view
;
1238 struct lttng_dynamic_buffer channel_buffer
;
1239 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1241 lttng_dynamic_buffer_init(&channel_buffer
);
1242 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1243 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1245 ret
= LTTNG_ERR_NOMEM
;
1249 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1251 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1252 ERR("Failed to receive \"enable channel\" command payload");
1253 ret
= LTTNG_ERR_INVALID
;
1257 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1258 if (!lttng_buffer_view_is_valid(&view
)) {
1259 ret
= LTTNG_ERR_INVALID
;
1263 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1264 ERR("Invalid channel payload received in \"enable channel\" command");
1265 ret
= LTTNG_ERR_INVALID
;
1269 ret
= cmd_enable_channel_internal(
1270 cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1273 lttng_dynamic_buffer_reset(&channel_buffer
);
1274 lttng_channel_destroy(channel
);
1278 static int cmd_enable_channel_internal(struct ltt_session
*session
,
1279 const struct lttng_domain
*domain
,
1280 const struct lttng_channel
*_attr
,
1284 struct ltt_ust_session
*usess
= session
->ust_session
;
1285 struct lttng_ht
*chan_ht
;
1287 struct lttng_channel
*attr
= NULL
;
1293 attr
= lttng_channel_copy(_attr
);
1295 ret
= -LTTNG_ERR_NOMEM
;
1299 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1301 /* Validate channel name */
1302 if (attr
->name
[0] == '.' ||
1303 memchr(attr
->name
, '/', len
) != NULL
) {
1304 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1308 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1313 * If the session is a live session, remove the switch timer, the
1314 * live timer does the same thing but sends also synchronisation
1315 * beacons for inactive streams.
1317 if (session
->live_timer
> 0) {
1318 attr
->attr
.live_timer_interval
= session
->live_timer
;
1319 attr
->attr
.switch_timer_interval
= 0;
1322 /* Check for feature support */
1323 switch (domain
->type
) {
1324 case LTTNG_DOMAIN_KERNEL
:
1326 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1327 /* Sampling position of buffer is not supported */
1328 WARN("Kernel tracer does not support buffer monitoring. "
1329 "Setting the monitor interval timer to 0 "
1330 "(disabled) for channel '%s' of session '%s'",
1331 attr
->name
, session
->name
);
1332 lttng_channel_set_monitor_timer_interval(attr
, 0);
1336 case LTTNG_DOMAIN_UST
:
1338 case LTTNG_DOMAIN_JUL
:
1339 case LTTNG_DOMAIN_LOG4J
:
1340 case LTTNG_DOMAIN_PYTHON
:
1341 if (!agent_tracing_is_enabled()) {
1342 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1343 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1348 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1352 switch (domain
->type
) {
1353 case LTTNG_DOMAIN_KERNEL
:
1355 struct ltt_kernel_channel
*kchan
;
1357 kchan
= trace_kernel_get_channel_by_name(
1358 attr
->name
, session
->kernel_session
);
1359 if (kchan
== NULL
) {
1361 * Don't try to create a channel if the session has been started at
1362 * some point in time before. The tracer does not allow it.
1364 if (session
->has_been_started
) {
1365 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1369 if (session
->snapshot
.nb_output
> 0 ||
1370 session
->snapshot_mode
) {
1371 /* Enforce mmap output for snapshot sessions. */
1372 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1374 ret
= channel_kernel_create(
1375 session
->kernel_session
, attr
, wpipe
);
1376 if (attr
->name
[0] != '\0') {
1377 session
->kernel_session
->has_non_default_channel
= 1;
1380 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1383 if (ret
!= LTTNG_OK
) {
1387 kernel_wait_quiescent();
1390 case LTTNG_DOMAIN_UST
:
1391 case LTTNG_DOMAIN_JUL
:
1392 case LTTNG_DOMAIN_LOG4J
:
1393 case LTTNG_DOMAIN_PYTHON
:
1395 struct ltt_ust_channel
*uchan
;
1400 * Current agent implementation limitations force us to allow
1401 * only one channel at once in "agent" subdomains. Each
1402 * subdomain has a default channel name which must be strictly
1405 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1406 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1407 LTTNG_SYMBOL_NAME_LEN
)) {
1408 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1411 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1412 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1413 LTTNG_SYMBOL_NAME_LEN
)) {
1414 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1417 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1418 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1419 LTTNG_SYMBOL_NAME_LEN
)) {
1420 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1425 chan_ht
= usess
->domain_global
.channels
;
1427 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1428 if (uchan
== NULL
) {
1430 * Don't try to create a channel if the session has been started at
1431 * some point in time before. The tracer does not allow it.
1433 if (session
->has_been_started
) {
1434 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1438 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1439 if (attr
->name
[0] != '\0') {
1440 usess
->has_non_default_channel
= 1;
1443 ret
= channel_ust_enable(usess
, uchan
);
1448 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1452 if (ret
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1453 session
->has_non_mmap_channel
= true;
1458 lttng_channel_destroy(attr
);
1462 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1463 struct ltt_session
*session
,
1464 enum lttng_domain_type domain
,
1465 enum lttng_process_attr process_attr
,
1466 enum lttng_tracking_policy
*policy
)
1468 enum lttng_error_code ret_code
= LTTNG_OK
;
1469 const struct process_attr_tracker
*tracker
;
1472 case LTTNG_DOMAIN_KERNEL
:
1473 if (!session
->kernel_session
) {
1474 ret_code
= LTTNG_ERR_INVALID
;
1477 tracker
= kernel_get_process_attr_tracker(
1478 session
->kernel_session
, process_attr
);
1480 case LTTNG_DOMAIN_UST
:
1481 if (!session
->ust_session
) {
1482 ret_code
= LTTNG_ERR_INVALID
;
1485 tracker
= trace_ust_get_process_attr_tracker(
1486 session
->ust_session
, process_attr
);
1489 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1493 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1495 ret_code
= LTTNG_ERR_INVALID
;
1501 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1502 struct ltt_session
*session
,
1503 enum lttng_domain_type domain
,
1504 enum lttng_process_attr process_attr
,
1505 enum lttng_tracking_policy policy
)
1507 enum lttng_error_code ret_code
= LTTNG_OK
;
1510 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1511 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1512 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1515 ret_code
= LTTNG_ERR_INVALID
;
1520 case LTTNG_DOMAIN_KERNEL
:
1521 if (!session
->kernel_session
) {
1522 ret_code
= LTTNG_ERR_INVALID
;
1525 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1526 session
->kernel_session
, process_attr
, policy
);
1528 case LTTNG_DOMAIN_UST
:
1529 if (!session
->ust_session
) {
1530 ret_code
= LTTNG_ERR_INVALID
;
1533 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1534 session
->ust_session
, process_attr
, policy
);
1537 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1544 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1545 struct ltt_session
*session
,
1546 enum lttng_domain_type domain
,
1547 enum lttng_process_attr process_attr
,
1548 const struct process_attr_value
*value
)
1550 enum lttng_error_code ret_code
= LTTNG_OK
;
1553 case LTTNG_DOMAIN_KERNEL
:
1554 if (!session
->kernel_session
) {
1555 ret_code
= LTTNG_ERR_INVALID
;
1558 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1559 session
->kernel_session
, process_attr
, value
);
1561 case LTTNG_DOMAIN_UST
:
1562 if (!session
->ust_session
) {
1563 ret_code
= LTTNG_ERR_INVALID
;
1566 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1567 session
->ust_session
, process_attr
, value
);
1570 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1577 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1578 struct ltt_session
*session
,
1579 enum lttng_domain_type domain
,
1580 enum lttng_process_attr process_attr
,
1581 const struct process_attr_value
*value
)
1583 enum lttng_error_code ret_code
= LTTNG_OK
;
1586 case LTTNG_DOMAIN_KERNEL
:
1587 if (!session
->kernel_session
) {
1588 ret_code
= LTTNG_ERR_INVALID
;
1591 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1592 session
->kernel_session
, process_attr
, value
);
1594 case LTTNG_DOMAIN_UST
:
1595 if (!session
->ust_session
) {
1596 ret_code
= LTTNG_ERR_INVALID
;
1599 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1600 session
->ust_session
, process_attr
, value
);
1603 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1610 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1611 struct ltt_session
*session
,
1612 enum lttng_domain_type domain
,
1613 enum lttng_process_attr process_attr
,
1614 struct lttng_process_attr_values
**values
)
1616 enum lttng_error_code ret_code
= LTTNG_OK
;
1617 const struct process_attr_tracker
*tracker
;
1618 enum process_attr_tracker_status status
;
1621 case LTTNG_DOMAIN_KERNEL
:
1622 if (!session
->kernel_session
) {
1623 ret_code
= LTTNG_ERR_INVALID
;
1626 tracker
= kernel_get_process_attr_tracker(
1627 session
->kernel_session
, process_attr
);
1629 case LTTNG_DOMAIN_UST
:
1630 if (!session
->ust_session
) {
1631 ret_code
= LTTNG_ERR_INVALID
;
1634 tracker
= trace_ust_get_process_attr_tracker(
1635 session
->ust_session
, process_attr
);
1638 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1643 ret_code
= LTTNG_ERR_INVALID
;
1647 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1649 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1650 ret_code
= LTTNG_OK
;
1652 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1653 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1655 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1656 ret_code
= LTTNG_ERR_NOMEM
;
1659 ret_code
= LTTNG_ERR_UNK
;
1668 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1670 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1671 struct lttng_event
*event
,
1672 char *filter_expression
,
1673 struct lttng_bytecode
*bytecode
,
1674 struct lttng_event_exclusion
*exclusion
)
1677 const char *event_name
;
1678 const struct ltt_session
*session
= cmd_ctx
->session
;
1679 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1680 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1682 DBG("Disable event command for event \'%s\'", event
->name
);
1685 * Filter and exclusions are simply not handled by the
1686 * disable event command at this time.
1690 (void) filter_expression
;
1693 /* Ignore the presence of filter or exclusion for the event */
1695 event
->exclusion
= 0;
1697 event_name
= event
->name
;
1699 /* Error out on unhandled search criteria */
1700 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1701 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1702 ret
= LTTNG_ERR_UNK
;
1709 case LTTNG_DOMAIN_KERNEL
:
1711 struct ltt_kernel_channel
*kchan
;
1712 struct ltt_kernel_session
*ksess
;
1714 ksess
= session
->kernel_session
;
1717 * If a non-default channel has been created in the
1718 * session, explicitely require that -c chan_name needs
1721 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1722 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1726 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1727 if (kchan
== NULL
) {
1728 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1732 switch (event
->type
) {
1733 case LTTNG_EVENT_ALL
:
1734 case LTTNG_EVENT_TRACEPOINT
:
1735 case LTTNG_EVENT_SYSCALL
:
1736 case LTTNG_EVENT_PROBE
:
1737 case LTTNG_EVENT_FUNCTION
:
1738 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1739 if (event_name
[0] == '\0') {
1740 ret
= event_kernel_disable_event(kchan
,
1743 ret
= event_kernel_disable_event(kchan
,
1744 event_name
, event
->type
);
1746 if (ret
!= LTTNG_OK
) {
1751 ret
= LTTNG_ERR_UNK
;
1755 kernel_wait_quiescent();
1758 case LTTNG_DOMAIN_UST
:
1760 struct ltt_ust_channel
*uchan
;
1761 struct ltt_ust_session
*usess
;
1763 usess
= session
->ust_session
;
1765 if (validate_ust_event_name(event_name
)) {
1766 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1771 * If a non-default channel has been created in the
1772 * session, explicitly require that -c chan_name needs
1775 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1776 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1780 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1782 if (uchan
== NULL
) {
1783 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1787 switch (event
->type
) {
1788 case LTTNG_EVENT_ALL
:
1790 * An empty event name means that everything
1791 * should be disabled.
1793 if (event
->name
[0] == '\0') {
1794 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1796 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1799 if (ret
!= LTTNG_OK
) {
1804 ret
= LTTNG_ERR_UNK
;
1808 DBG3("Disable UST event %s in channel %s completed", event_name
,
1812 case LTTNG_DOMAIN_LOG4J
:
1813 case LTTNG_DOMAIN_JUL
:
1814 case LTTNG_DOMAIN_PYTHON
:
1817 struct ltt_ust_session
*usess
= session
->ust_session
;
1821 switch (event
->type
) {
1822 case LTTNG_EVENT_ALL
:
1825 ret
= LTTNG_ERR_UNK
;
1829 agt
= trace_ust_find_agent(usess
, domain
);
1831 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1835 * An empty event name means that everything
1836 * should be disabled.
1838 if (event
->name
[0] == '\0') {
1839 ret
= event_agent_disable_all(usess
, agt
);
1841 ret
= event_agent_disable(usess
, agt
, event_name
);
1843 if (ret
!= LTTNG_OK
) {
1850 ret
= LTTNG_ERR_UND
;
1861 free(filter_expression
);
1866 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1868 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1869 const struct lttng_event_context
*event_context
, int kwpipe
)
1871 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1872 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1873 const struct ltt_session
*session
= cmd_ctx
->session
;
1874 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1877 * Don't try to add a context if the session has been started at
1878 * some point in time before. The tracer does not allow it and would
1879 * result in a corrupted trace.
1881 if (cmd_ctx
->session
->has_been_started
) {
1882 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1887 case LTTNG_DOMAIN_KERNEL
:
1888 assert(session
->kernel_session
);
1890 if (session
->kernel_session
->channel_count
== 0) {
1891 /* Create default channel */
1892 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1893 if (ret
!= LTTNG_OK
) {
1896 chan_kern_created
= 1;
1898 /* Add kernel context to kernel tracer */
1899 ret
= context_kernel_add(session
->kernel_session
,
1900 event_context
, channel_name
);
1901 if (ret
!= LTTNG_OK
) {
1905 case LTTNG_DOMAIN_JUL
:
1906 case LTTNG_DOMAIN_LOG4J
:
1909 * Validate channel name.
1910 * If no channel name is given and the domain is JUL or LOG4J,
1911 * set it to the appropriate domain-specific channel name. If
1912 * a name is provided but does not match the expexted channel
1913 * name, return an error.
1915 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1916 strcmp(channel_name
,
1917 DEFAULT_JUL_CHANNEL_NAME
)) {
1918 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1920 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1921 strcmp(channel_name
,
1922 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1923 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1926 /* break is _not_ missing here. */
1928 case LTTNG_DOMAIN_UST
:
1930 struct ltt_ust_session
*usess
= session
->ust_session
;
1931 unsigned int chan_count
;
1935 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1936 if (chan_count
== 0) {
1937 struct lttng_channel
*attr
;
1938 /* Create default channel */
1939 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1941 ret
= LTTNG_ERR_FATAL
;
1945 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1946 if (ret
!= LTTNG_OK
) {
1950 channel_attr_destroy(attr
);
1951 chan_ust_created
= 1;
1954 ret
= context_ust_add(usess
, domain
, event_context
,
1956 if (ret
!= LTTNG_OK
) {
1962 ret
= LTTNG_ERR_UND
;
1970 if (chan_kern_created
) {
1971 struct ltt_kernel_channel
*kchan
=
1972 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1973 session
->kernel_session
);
1974 /* Created previously, this should NOT fail. */
1976 kernel_destroy_channel(kchan
);
1979 if (chan_ust_created
) {
1980 struct ltt_ust_channel
*uchan
=
1981 trace_ust_find_channel_by_name(
1982 session
->ust_session
->domain_global
.channels
,
1983 DEFAULT_CHANNEL_NAME
);
1984 /* Created previously, this should NOT fail. */
1986 /* Remove from the channel list of the session. */
1987 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1989 trace_ust_destroy_channel(uchan
);
1995 static inline bool name_starts_with(const char *name
, const char *prefix
)
1997 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1999 return !strncmp(name
, prefix
, max_cmp_len
);
2002 /* Perform userspace-specific event name validation */
2003 static int validate_ust_event_name(const char *name
)
2013 * Check name against all internal UST event component namespaces used
2016 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2017 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2018 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2027 * Internal version of cmd_enable_event() with a supplemental
2028 * "internal_event" flag which is used to enable internal events which should
2029 * be hidden from clients. Such events are used in the agent implementation to
2030 * enable the events through which all "agent" events are funeled.
2032 static int _cmd_enable_event(struct ltt_session
*session
,
2033 const struct lttng_domain
*domain
,
2034 char *channel_name
, struct lttng_event
*event
,
2035 char *filter_expression
,
2036 struct lttng_bytecode
*filter
,
2037 struct lttng_event_exclusion
*exclusion
,
2038 int wpipe
, bool internal_event
)
2040 int ret
= 0, channel_created
= 0;
2041 struct lttng_channel
*attr
= NULL
;
2045 assert(channel_name
);
2047 /* If we have a filter, we must have its filter expression */
2048 assert(!(!!filter_expression
^ !!filter
));
2050 /* Normalize event name as a globbing pattern */
2051 strutils_normalize_star_glob_pattern(event
->name
);
2053 /* Normalize exclusion names as globbing patterns */
2057 for (i
= 0; i
< exclusion
->count
; i
++) {
2058 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2060 strutils_normalize_star_glob_pattern(name
);
2064 DBG("Enable event command for event \'%s\'", event
->name
);
2068 switch (domain
->type
) {
2069 case LTTNG_DOMAIN_KERNEL
:
2071 struct ltt_kernel_channel
*kchan
;
2074 * If a non-default channel has been created in the
2075 * session, explicitely require that -c chan_name needs
2078 if (session
->kernel_session
->has_non_default_channel
2079 && channel_name
[0] == '\0') {
2080 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2084 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2085 session
->kernel_session
);
2086 if (kchan
== NULL
) {
2087 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2088 LTTNG_BUFFER_GLOBAL
);
2090 ret
= LTTNG_ERR_FATAL
;
2093 if (lttng_strncpy(attr
->name
, channel_name
,
2094 sizeof(attr
->name
))) {
2095 ret
= LTTNG_ERR_INVALID
;
2099 ret
= cmd_enable_channel_internal(
2100 session
, domain
, attr
, wpipe
);
2101 if (ret
!= LTTNG_OK
) {
2104 channel_created
= 1;
2107 /* Get the newly created kernel channel pointer */
2108 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2109 session
->kernel_session
);
2110 if (kchan
== NULL
) {
2111 /* This sould not happen... */
2112 ret
= LTTNG_ERR_FATAL
;
2116 switch (event
->type
) {
2117 case LTTNG_EVENT_ALL
:
2119 char *filter_expression_a
= NULL
;
2120 struct lttng_bytecode
*filter_a
= NULL
;
2123 * We need to duplicate filter_expression and filter,
2124 * because ownership is passed to first enable
2127 if (filter_expression
) {
2128 filter_expression_a
= strdup(filter_expression
);
2129 if (!filter_expression_a
) {
2130 ret
= LTTNG_ERR_FATAL
;
2135 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2137 free(filter_expression_a
);
2138 ret
= LTTNG_ERR_FATAL
;
2141 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2143 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2144 ret
= event_kernel_enable_event(kchan
, event
,
2145 filter_expression
, filter
);
2146 /* We have passed ownership */
2147 filter_expression
= NULL
;
2149 if (ret
!= LTTNG_OK
) {
2150 if (channel_created
) {
2151 /* Let's not leak a useless channel. */
2152 kernel_destroy_channel(kchan
);
2154 free(filter_expression_a
);
2158 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2159 ret
= event_kernel_enable_event(kchan
, event
,
2160 filter_expression_a
, filter_a
);
2161 /* We have passed ownership */
2162 filter_expression_a
= NULL
;
2164 if (ret
!= LTTNG_OK
) {
2169 case LTTNG_EVENT_PROBE
:
2170 case LTTNG_EVENT_USERSPACE_PROBE
:
2171 case LTTNG_EVENT_FUNCTION
:
2172 case LTTNG_EVENT_FUNCTION_ENTRY
:
2173 case LTTNG_EVENT_TRACEPOINT
:
2174 ret
= event_kernel_enable_event(kchan
, event
,
2175 filter_expression
, filter
);
2176 /* We have passed ownership */
2177 filter_expression
= NULL
;
2179 if (ret
!= LTTNG_OK
) {
2180 if (channel_created
) {
2181 /* Let's not leak a useless channel. */
2182 kernel_destroy_channel(kchan
);
2187 case LTTNG_EVENT_SYSCALL
:
2188 ret
= event_kernel_enable_event(kchan
, event
,
2189 filter_expression
, filter
);
2190 /* We have passed ownership */
2191 filter_expression
= NULL
;
2193 if (ret
!= LTTNG_OK
) {
2198 ret
= LTTNG_ERR_UNK
;
2202 kernel_wait_quiescent();
2205 case LTTNG_DOMAIN_UST
:
2207 struct ltt_ust_channel
*uchan
;
2208 struct ltt_ust_session
*usess
= session
->ust_session
;
2213 * If a non-default channel has been created in the
2214 * session, explicitely require that -c chan_name needs
2217 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2218 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2222 /* Get channel from global UST domain */
2223 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2225 if (uchan
== NULL
) {
2226 /* Create default channel */
2227 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2228 usess
->buffer_type
);
2230 ret
= LTTNG_ERR_FATAL
;
2233 if (lttng_strncpy(attr
->name
, channel_name
,
2234 sizeof(attr
->name
))) {
2235 ret
= LTTNG_ERR_INVALID
;
2239 ret
= cmd_enable_channel_internal(
2240 session
, domain
, attr
, wpipe
);
2241 if (ret
!= LTTNG_OK
) {
2245 /* Get the newly created channel reference back */
2246 uchan
= trace_ust_find_channel_by_name(
2247 usess
->domain_global
.channels
, channel_name
);
2251 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2253 * Don't allow users to add UST events to channels which
2254 * are assigned to a userspace subdomain (JUL, Log4J,
2257 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2261 if (!internal_event
) {
2263 * Ensure the event name is not reserved for internal
2266 ret
= validate_ust_event_name(event
->name
);
2268 WARN("Userspace event name %s failed validation.",
2270 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2275 /* At this point, the session and channel exist on the tracer */
2276 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2277 filter_expression
, filter
, exclusion
,
2279 /* We have passed ownership */
2280 filter_expression
= NULL
;
2283 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2284 goto already_enabled
;
2285 } else if (ret
!= LTTNG_OK
) {
2290 case LTTNG_DOMAIN_LOG4J
:
2291 case LTTNG_DOMAIN_JUL
:
2292 case LTTNG_DOMAIN_PYTHON
:
2294 const char *default_event_name
, *default_chan_name
;
2296 struct lttng_event uevent
;
2297 struct lttng_domain tmp_dom
;
2298 struct ltt_ust_session
*usess
= session
->ust_session
;
2302 if (!agent_tracing_is_enabled()) {
2303 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2304 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2308 agt
= trace_ust_find_agent(usess
, domain
->type
);
2310 agt
= agent_create(domain
->type
);
2312 ret
= LTTNG_ERR_NOMEM
;
2315 agent_add(agt
, usess
->agents
);
2318 /* Create the default tracepoint. */
2319 memset(&uevent
, 0, sizeof(uevent
));
2320 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2321 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2322 default_event_name
= event_get_default_agent_ust_name(
2324 if (!default_event_name
) {
2325 ret
= LTTNG_ERR_FATAL
;
2328 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2329 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2332 * The domain type is changed because we are about to enable the
2333 * default channel and event for the JUL domain that are hardcoded.
2334 * This happens in the UST domain.
2336 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2337 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2339 switch (domain
->type
) {
2340 case LTTNG_DOMAIN_LOG4J
:
2341 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2343 case LTTNG_DOMAIN_JUL
:
2344 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2346 case LTTNG_DOMAIN_PYTHON
:
2347 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2350 /* The switch/case we are in makes this impossible */
2355 char *filter_expression_copy
= NULL
;
2356 struct lttng_bytecode
*filter_copy
= NULL
;
2359 const size_t filter_size
= sizeof(
2360 struct lttng_bytecode
)
2363 filter_copy
= zmalloc(filter_size
);
2365 ret
= LTTNG_ERR_NOMEM
;
2368 memcpy(filter_copy
, filter
, filter_size
);
2370 filter_expression_copy
=
2371 strdup(filter_expression
);
2372 if (!filter_expression
) {
2373 ret
= LTTNG_ERR_NOMEM
;
2376 if (!filter_expression_copy
|| !filter_copy
) {
2377 free(filter_expression_copy
);
2383 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2384 (char *) default_chan_name
,
2385 &uevent
, filter_expression_copy
,
2386 filter_copy
, NULL
, wpipe
);
2389 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2390 goto already_enabled
;
2391 } else if (ret
!= LTTNG_OK
) {
2395 /* The wild card * means that everything should be enabled. */
2396 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2397 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2400 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2404 filter_expression
= NULL
;
2405 if (ret
!= LTTNG_OK
) {
2412 ret
= LTTNG_ERR_UND
;
2420 free(filter_expression
);
2423 channel_attr_destroy(attr
);
2429 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2430 * We own filter, exclusion, and filter_expression.
2432 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2433 struct lttng_event
*event
,
2434 char *filter_expression
,
2435 struct lttng_event_exclusion
*exclusion
,
2436 struct lttng_bytecode
*bytecode
,
2441 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2443 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2446 * The ownership of the following parameters is transferred to
2447 * _cmd_enable_event:
2449 * - filter_expression,
2453 ret
= _cmd_enable_event(cmd_ctx
->session
,
2455 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2456 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2457 filter_expression
= NULL
;
2464 * Enable an event which is internal to LTTng. An internal should
2465 * never be made visible to clients and are immune to checks such as
2468 static int cmd_enable_event_internal(struct ltt_session
*session
,
2469 const struct lttng_domain
*domain
,
2470 char *channel_name
, struct lttng_event
*event
,
2471 char *filter_expression
,
2472 struct lttng_bytecode
*filter
,
2473 struct lttng_event_exclusion
*exclusion
,
2476 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2477 filter_expression
, filter
, exclusion
, wpipe
, true);
2481 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2483 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2484 struct lttng_payload
*reply_payload
)
2486 enum lttng_error_code ret_code
;
2488 ssize_t i
, nb_events
= 0;
2489 struct lttng_event
*events
= NULL
;
2490 struct lttcomm_list_command_header reply_command_header
= {};
2491 size_t reply_command_header_offset
;
2493 assert(reply_payload
);
2495 /* Reserve space for command reply header. */
2496 reply_command_header_offset
= reply_payload
->buffer
.size
;
2497 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2498 reply_command_header_offset
+
2499 sizeof(struct lttcomm_list_command_header
));
2501 ret_code
= LTTNG_ERR_NOMEM
;
2506 case LTTNG_DOMAIN_KERNEL
:
2507 nb_events
= kernel_list_events(&events
);
2508 if (nb_events
< 0) {
2509 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2513 case LTTNG_DOMAIN_UST
:
2514 nb_events
= ust_app_list_events(&events
);
2515 if (nb_events
< 0) {
2516 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2520 case LTTNG_DOMAIN_LOG4J
:
2521 case LTTNG_DOMAIN_JUL
:
2522 case LTTNG_DOMAIN_PYTHON
:
2523 nb_events
= agent_list_events(&events
, domain
);
2524 if (nb_events
< 0) {
2525 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2530 ret_code
= LTTNG_ERR_UND
;
2534 for (i
= 0; i
< nb_events
; i
++) {
2535 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2538 ret_code
= LTTNG_ERR_NOMEM
;
2543 if (nb_events
> UINT32_MAX
) {
2544 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2545 ret_code
= LTTNG_ERR_OVERFLOW
;
2549 /* Update command reply header. */
2550 reply_command_header
.count
= (uint32_t) nb_events
;
2551 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2552 sizeof(reply_command_header
));
2554 ret_code
= LTTNG_OK
;
2561 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2563 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2564 struct lttng_payload
*reply
)
2566 enum lttng_error_code ret_code
;
2568 unsigned int i
, nb_fields
;
2569 struct lttng_event_field
*fields
= NULL
;
2570 struct lttcomm_list_command_header reply_command_header
= {};
2571 size_t reply_command_header_offset
;
2575 /* Reserve space for command reply header. */
2576 reply_command_header_offset
= reply
->buffer
.size
;
2577 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2578 reply_command_header_offset
+
2579 sizeof(struct lttcomm_list_command_header
));
2581 ret_code
= LTTNG_ERR_NOMEM
;
2586 case LTTNG_DOMAIN_UST
:
2587 ret
= ust_app_list_event_fields(&fields
);
2589 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2594 case LTTNG_DOMAIN_KERNEL
:
2595 default: /* fall-through */
2596 ret_code
= LTTNG_ERR_UND
;
2602 for (i
= 0; i
< nb_fields
; i
++) {
2603 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2605 ret_code
= LTTNG_ERR_NOMEM
;
2610 if (nb_fields
> UINT32_MAX
) {
2611 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2612 ret_code
= LTTNG_ERR_OVERFLOW
;
2616 /* Update command reply header. */
2617 reply_command_header
.count
= (uint32_t) nb_fields
;
2619 memcpy(reply
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2620 sizeof(reply_command_header
));
2622 ret_code
= LTTNG_OK
;
2629 enum lttng_error_code
cmd_list_syscalls(
2630 struct lttng_payload
*reply_payload
)
2632 enum lttng_error_code ret_code
;
2633 ssize_t nb_events
, i
;
2635 struct lttng_event
*events
= NULL
;
2636 struct lttcomm_list_command_header reply_command_header
= {};
2637 size_t reply_command_header_offset
;
2639 assert(reply_payload
);
2641 /* Reserve space for command reply header. */
2642 reply_command_header_offset
= reply_payload
->buffer
.size
;
2643 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2644 reply_command_header_offset
+
2645 sizeof(struct lttcomm_list_command_header
));
2647 ret_code
= LTTNG_ERR_NOMEM
;
2651 nb_events
= syscall_table_list(&events
);
2652 if (nb_events
< 0) {
2653 ret_code
= (enum lttng_error_code
) -nb_events
;
2657 for (i
= 0; i
< nb_events
; i
++) {
2658 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2661 ret_code
= LTTNG_ERR_NOMEM
;
2666 if (nb_events
> UINT32_MAX
) {
2667 ERR("Syscall count would overflow the syscall listing command's reply");
2668 ret_code
= LTTNG_ERR_OVERFLOW
;
2672 /* Update command reply header. */
2673 reply_command_header
.count
= (uint32_t) nb_events
;
2674 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2675 sizeof(reply_command_header
));
2677 ret_code
= LTTNG_OK
;
2684 * Command LTTNG_START_TRACE processed by the client thread.
2686 * Called with session mutex held.
2688 int cmd_start_trace(struct ltt_session
*session
)
2690 enum lttng_error_code ret
;
2691 unsigned long nb_chan
= 0;
2692 struct ltt_kernel_session
*ksession
;
2693 struct ltt_ust_session
*usess
;
2694 const bool session_rotated_after_last_stop
=
2695 session
->rotated_after_last_stop
;
2696 const bool session_cleared_after_last_stop
=
2697 session
->cleared_after_last_stop
;
2701 /* Ease our life a bit ;) */
2702 ksession
= session
->kernel_session
;
2703 usess
= session
->ust_session
;
2705 /* Is the session already started? */
2706 if (session
->active
) {
2707 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2708 /* Perform nothing */
2712 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2713 !session
->current_trace_chunk
) {
2715 * A rotation was launched while the session was stopped and
2716 * it has not been completed yet. It is not possible to start
2717 * the session since starting the session here would require a
2718 * rotation from "NULL" to a new trace chunk. That rotation
2719 * would overlap with the ongoing rotation, which is not
2722 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2724 ret
= LTTNG_ERR_ROTATION_PENDING
;
2729 * Starting a session without channel is useless since after that it's not
2730 * possible to enable channel thus inform the client.
2732 if (usess
&& usess
->domain_global
.channels
) {
2733 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2736 nb_chan
+= ksession
->channel_count
;
2739 ret
= LTTNG_ERR_NO_CHANNEL
;
2743 session
->active
= 1;
2744 session
->rotated_after_last_stop
= false;
2745 session
->cleared_after_last_stop
= false;
2746 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2747 if (!session
->has_been_started
) {
2748 struct lttng_trace_chunk
*trace_chunk
;
2750 DBG("Creating initial trace chunk of session \"%s\"",
2752 trace_chunk
= session_create_new_trace_chunk(
2753 session
, NULL
, NULL
, NULL
);
2755 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2758 assert(!session
->current_trace_chunk
);
2759 ret
= session_set_trace_chunk(session
, trace_chunk
,
2761 lttng_trace_chunk_put(trace_chunk
);
2763 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2767 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2770 * Rotate existing streams into the new chunk.
2771 * This is a "quiet" rotation has no client has
2772 * explicitly requested this operation.
2774 * There is also no need to wait for the rotation
2775 * to complete as it will happen immediately. No data
2776 * was produced as the session was stopped, so the
2777 * rotation should happen on reception of the command.
2779 ret
= cmd_rotate_session(session
, NULL
, true,
2780 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2781 if (ret
!= LTTNG_OK
) {
2787 /* Kernel tracing */
2788 if (ksession
!= NULL
) {
2789 DBG("Start kernel tracing session %s", session
->name
);
2790 ret
= start_kernel_session(ksession
);
2791 if (ret
!= LTTNG_OK
) {
2796 /* Flag session that trace should start automatically */
2798 int int_ret
= ust_app_start_trace_all(usess
);
2801 ret
= LTTNG_ERR_UST_START_FAIL
;
2807 * Open a packet in every stream of the session to ensure that viewers
2808 * can correctly identify the boundaries of the periods during which
2809 * tracing was active for this session.
2811 ret
= session_open_packets(session
);
2812 if (ret
!= LTTNG_OK
) {
2817 * Clear the flag that indicates that a rotation was done while the
2818 * session was stopped.
2820 session
->rotated_after_last_stop
= false;
2822 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2823 int int_ret
= timer_session_rotation_schedule_timer_start(
2824 session
, session
->rotate_timer_period
);
2827 ERR("Failed to enable rotate timer");
2828 ret
= LTTNG_ERR_UNK
;
2836 if (ret
== LTTNG_OK
) {
2837 /* Flag this after a successful start. */
2838 session
->has_been_started
|= 1;
2840 session
->active
= 0;
2841 /* Restore initial state on error. */
2842 session
->rotated_after_last_stop
=
2843 session_rotated_after_last_stop
;
2844 session
->cleared_after_last_stop
=
2845 session_cleared_after_last_stop
;
2852 * Command LTTNG_STOP_TRACE processed by the client thread.
2854 int cmd_stop_trace(struct ltt_session
*session
)
2857 struct ltt_kernel_session
*ksession
;
2858 struct ltt_ust_session
*usess
;
2862 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2864 ksession
= session
->kernel_session
;
2865 usess
= session
->ust_session
;
2867 /* Session is not active. Skip everythong and inform the client. */
2868 if (!session
->active
) {
2869 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2873 ret
= stop_kernel_session(ksession
);
2874 if (ret
!= LTTNG_OK
) {
2878 if (usess
&& usess
->active
) {
2879 ret
= ust_app_stop_trace_all(usess
);
2881 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2886 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2888 /* Flag inactive after a successful stop. */
2889 session
->active
= 0;
2897 * Set the base_path of the session only if subdir of a control uris is set.
2898 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2900 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2902 struct lttng_uri
*uris
)
2907 for (i
= 0; i
< nb_uri
; i
++) {
2908 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2909 uris
[i
].subdir
[0] == '\0') {
2910 /* Not interested in these URIs */
2914 if (session
->base_path
!= NULL
) {
2915 free(session
->base_path
);
2916 session
->base_path
= NULL
;
2919 /* Set session base_path */
2920 session
->base_path
= strdup(uris
[i
].subdir
);
2921 if (!session
->base_path
) {
2922 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2923 uris
[i
].subdir
, session
->name
);
2924 ret
= LTTNG_ERR_NOMEM
;
2927 DBG2("Setting base path \"%s\" for session \"%s\"",
2928 session
->base_path
, session
->name
);
2936 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2938 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2939 struct lttng_uri
*uris
)
2942 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2943 struct ltt_ust_session
*usess
= session
->ust_session
;
2949 /* Can't set consumer URI if the session is active. */
2950 if (session
->active
) {
2951 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2956 * Set the session base path if any. This is done inside
2957 * cmd_set_consumer_uri to preserve backward compatibility of the
2958 * previous session creation api vs the session descriptor api.
2960 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2961 if (ret
!= LTTNG_OK
) {
2965 /* Set the "global" consumer URIs */
2966 for (i
= 0; i
< nb_uri
; i
++) {
2967 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2969 if (ret
!= LTTNG_OK
) {
2974 /* Set UST session URIs */
2975 if (session
->ust_session
) {
2976 for (i
= 0; i
< nb_uri
; i
++) {
2977 ret
= add_uri_to_consumer(session
,
2978 session
->ust_session
->consumer
,
2979 &uris
[i
], LTTNG_DOMAIN_UST
);
2980 if (ret
!= LTTNG_OK
) {
2986 /* Set kernel session URIs */
2987 if (session
->kernel_session
) {
2988 for (i
= 0; i
< nb_uri
; i
++) {
2989 ret
= add_uri_to_consumer(session
,
2990 session
->kernel_session
->consumer
,
2991 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2992 if (ret
!= LTTNG_OK
) {
2999 * Make sure to set the session in output mode after we set URI since a
3000 * session can be created without URL (thus flagged in no output mode).
3002 session
->output_traces
= 1;
3004 ksess
->output_traces
= 1;
3008 usess
->output_traces
= 1;
3019 enum lttng_error_code
set_session_output_from_descriptor(
3020 struct ltt_session
*session
,
3021 const struct lttng_session_descriptor
*descriptor
)
3024 enum lttng_error_code ret_code
= LTTNG_OK
;
3025 enum lttng_session_descriptor_type session_type
=
3026 lttng_session_descriptor_get_type(descriptor
);
3027 enum lttng_session_descriptor_output_type output_type
=
3028 lttng_session_descriptor_get_output_type(descriptor
);
3029 struct lttng_uri uris
[2] = {};
3030 size_t uri_count
= 0;
3032 switch (output_type
) {
3033 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3035 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3036 lttng_session_descriptor_get_local_output_uri(descriptor
,
3040 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3041 lttng_session_descriptor_get_network_output_uris(descriptor
,
3042 &uris
[0], &uris
[1]);
3046 ret_code
= LTTNG_ERR_INVALID
;
3050 switch (session_type
) {
3051 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3053 struct snapshot_output
*new_output
= NULL
;
3055 new_output
= snapshot_output_alloc();
3057 ret_code
= LTTNG_ERR_NOMEM
;
3061 ret
= snapshot_output_init_with_uri(session
,
3062 DEFAULT_SNAPSHOT_MAX_SIZE
,
3063 NULL
, uris
, uri_count
, session
->consumer
,
3064 new_output
, &session
->snapshot
);
3066 ret_code
= (ret
== -ENOMEM
) ?
3067 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3068 snapshot_output_destroy(new_output
);
3071 snapshot_add_output(&session
->snapshot
, new_output
);
3074 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3075 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3077 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
3081 ret_code
= LTTNG_ERR_INVALID
;
3089 enum lttng_error_code
cmd_create_session_from_descriptor(
3090 struct lttng_session_descriptor
*descriptor
,
3091 const lttng_sock_cred
*creds
,
3092 const char *home_path
)
3095 enum lttng_error_code ret_code
;
3096 const char *session_name
;
3097 struct ltt_session
*new_session
= NULL
;
3098 enum lttng_session_descriptor_status descriptor_status
;
3100 session_lock_list();
3102 if (*home_path
!= '/') {
3103 ERR("Home path provided by client is not absolute");
3104 ret_code
= LTTNG_ERR_INVALID
;
3109 descriptor_status
= lttng_session_descriptor_get_session_name(
3110 descriptor
, &session_name
);
3111 switch (descriptor_status
) {
3112 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3114 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3115 session_name
= NULL
;
3118 ret_code
= LTTNG_ERR_INVALID
;
3122 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3124 if (ret_code
!= LTTNG_OK
) {
3128 if (!session_name
) {
3129 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3132 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3137 if (!lttng_session_descriptor_is_output_destination_initialized(
3140 * Only include the session's creation time in the output
3141 * destination if the name of the session itself was
3142 * not auto-generated.
3144 ret_code
= lttng_session_descriptor_set_default_output(
3146 session_name
? &new_session
->creation_time
: NULL
,
3148 if (ret_code
!= LTTNG_OK
) {
3152 new_session
->has_user_specified_directory
=
3153 lttng_session_descriptor_has_output_directory(
3157 switch (lttng_session_descriptor_get_type(descriptor
)) {
3158 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3159 new_session
->snapshot_mode
= 1;
3161 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3162 new_session
->live_timer
=
3163 lttng_session_descriptor_live_get_timer_interval(
3170 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3171 if (ret_code
!= LTTNG_OK
) {
3174 new_session
->consumer
->enabled
= 1;
3175 ret_code
= LTTNG_OK
;
3177 /* Release reference provided by the session_create function. */
3178 session_put(new_session
);
3179 if (ret_code
!= LTTNG_OK
&& new_session
) {
3180 /* Release the global reference on error. */
3181 session_destroy(new_session
);
3183 session_unlock_list();
3187 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3188 struct lttng_session_descriptor
**return_descriptor
)
3191 size_t payload_size
;
3192 struct lttng_dynamic_buffer payload
;
3193 struct lttng_buffer_view home_dir_view
;
3194 struct lttng_buffer_view session_descriptor_view
;
3195 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3196 enum lttng_error_code ret_code
;
3198 lttng_dynamic_buffer_init(&payload
);
3199 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3201 ret_code
= LTTNG_ERR_INVALID
;
3204 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3205 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3206 ret_code
= LTTNG_ERR_INVALID
;
3210 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3211 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3212 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3214 ret_code
= LTTNG_ERR_NOMEM
;
3218 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3220 ERR("Reception of session descriptor failed, aborting.");
3221 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3225 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3228 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3229 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3230 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3231 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3232 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3236 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3238 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3239 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3240 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3241 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3242 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3246 ret
= lttng_session_descriptor_create_from_buffer(
3247 &session_descriptor_view
, &session_descriptor
);
3249 ERR("Failed to create session descriptor from payload of \"create session\" command");
3250 ret_code
= LTTNG_ERR_INVALID
;
3255 * Sets the descriptor's auto-generated properties (name, output) if
3258 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3260 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3261 if (ret_code
!= LTTNG_OK
) {
3265 ret_code
= LTTNG_OK
;
3266 *return_descriptor
= session_descriptor
;
3267 session_descriptor
= NULL
;
3269 lttng_dynamic_buffer_reset(&payload
);
3270 lttng_session_descriptor_destroy(session_descriptor
);
3275 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3276 void *_reply_context
)
3280 const struct cmd_destroy_session_reply_context
*reply_context
=
3282 struct lttng_dynamic_buffer payload
;
3283 struct lttcomm_session_destroy_command_header cmd_header
;
3284 struct lttng_trace_archive_location
*location
= NULL
;
3285 struct lttcomm_lttng_msg llm
= {
3286 .cmd_type
= LTTNG_DESTROY_SESSION
,
3287 .ret_code
= reply_context
->destruction_status
,
3290 sizeof(struct lttcomm_session_destroy_command_header
),
3293 size_t payload_size_before_location
;
3295 lttng_dynamic_buffer_init(&payload
);
3297 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3299 ERR("Failed to append session destruction message");
3303 cmd_header
.rotation_state
=
3304 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3305 session
->rotation_state
:
3306 LTTNG_ROTATION_STATE_NO_ROTATION
);
3307 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3308 sizeof(cmd_header
));
3310 ERR("Failed to append session destruction command header");
3314 if (!reply_context
->implicit_rotation_on_destroy
) {
3315 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3319 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3320 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3325 location
= session_get_trace_archive_location(session
);
3327 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3332 payload_size_before_location
= payload
.size
;
3333 comm_ret
= lttng_trace_archive_location_serialize(location
,
3335 lttng_trace_archive_location_put(location
);
3337 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3341 /* Update the message to indicate the location's length. */
3342 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3343 payload
.size
- payload_size_before_location
;
3345 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3346 payload
.data
, payload
.size
);
3347 if (comm_ret
!= (ssize_t
) payload
.size
) {
3348 ERR("Failed to send result of the destruction of session \"%s\" to client",
3352 ret
= close(reply_context
->reply_sock_fd
);
3354 PERROR("Failed to close client socket in deferred session destroy reply");
3356 lttng_dynamic_buffer_reset(&payload
);
3357 free(_reply_context
);
3361 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3363 * Called with session lock held.
3365 int cmd_destroy_session(struct ltt_session
*session
,
3366 struct notification_thread_handle
*notification_thread_handle
,
3370 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3371 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3374 reply_context
= zmalloc(sizeof(*reply_context
));
3375 if (!reply_context
) {
3376 ret
= LTTNG_ERR_NOMEM
;
3379 reply_context
->reply_sock_fd
= *sock_fd
;
3385 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3387 if (session
->active
) {
3388 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3390 ret
= cmd_stop_trace(session
);
3391 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3392 /* Carry on with the destruction of the session. */
3393 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3394 session
->name
, lttng_strerror(-ret
));
3395 destruction_last_error
= ret
;
3399 if (session
->rotation_schedule_timer_enabled
) {
3400 if (timer_session_rotation_schedule_timer_stop(
3402 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3404 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3408 if (session
->rotate_size
) {
3409 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3410 session
->rotate_size
= 0;
3413 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3415 * Perform a last rotation on destruction if rotations have
3416 * occurred during the session's lifetime.
3418 ret
= cmd_rotate_session(session
, NULL
, false,
3419 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3420 if (ret
!= LTTNG_OK
) {
3421 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3422 session
->name
, lttng_strerror(-ret
));
3423 destruction_last_error
= -ret
;
3425 if (reply_context
) {
3426 reply_context
->implicit_rotation_on_destroy
= true;
3428 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3430 * The user has not triggered a session rotation. However, to
3431 * ensure all data has been consumed, the session is rotated
3432 * to a 'null' trace chunk before it is destroyed.
3434 * This is a "quiet" rotation meaning that no notification is
3435 * emitted and no renaming of the current trace chunk takes
3438 ret
= cmd_rotate_session(session
, NULL
, true,
3439 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3441 * Rotation operations may not be supported by the kernel
3442 * tracer. Hence, do not consider this implicit rotation as
3443 * a session destruction error. The library has already stopped
3444 * the session and waited for pending data; there is nothing
3445 * left to do but complete the destruction of the session.
3447 if (ret
!= LTTNG_OK
&&
3448 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3449 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3450 session
->name
, lttng_strerror(ret
));
3451 destruction_last_error
= -ret
;
3455 if (session
->shm_path
[0]) {
3457 * When a session is created with an explicit shm_path,
3458 * the consumer daemon will create its shared memory files
3459 * at that location and will *not* unlink them. This is normal
3460 * as the intention of that feature is to make it possible
3461 * to retrieve the content of those files should a crash occur.
3463 * To ensure the content of those files can be used, the
3464 * sessiond daemon will replicate the content of the metadata
3465 * cache in a metadata file.
3467 * On clean-up, it is expected that the consumer daemon will
3468 * unlink the shared memory files and that the session daemon
3469 * will unlink the metadata file. Then, the session's directory
3470 * in the shm path can be removed.
3472 * Unfortunately, a flaw in the design of the sessiond's and
3473 * consumerd's tear down of channels makes it impossible to
3474 * determine when the sessiond _and_ the consumerd have both
3475 * destroyed their representation of a channel. For one, the
3476 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3477 * callbacks in both daemons.
3479 * However, it is also impossible for the sessiond to know when
3480 * the consumer daemon is done destroying its channel(s) since
3481 * it occurs as a reaction to the closing of the channel's file
3482 * descriptor. There is no resulting communication initiated
3483 * from the consumerd to the sessiond to confirm that the
3484 * operation is completed (and was successful).
3486 * Until this is all fixed, the session daemon checks for the
3487 * removal of the session's shm path which makes it possible
3488 * to safely advertise a session as having been destroyed.
3490 * Prior to this fix, it was not possible to reliably save
3491 * a session making use of the --shm-path option, destroy it,
3492 * and load it again. This is because the creation of the
3493 * session would fail upon seeing the session's shm path
3494 * already in existence.
3496 * Note that none of the error paths in the check for the
3497 * directory's existence return an error. This is normal
3498 * as there isn't much that can be done. The session will
3499 * be destroyed properly, except that we can't offer the
3500 * guarantee that the same session can be re-created.
3502 current_completion_handler
= &destroy_completion_handler
.handler
;
3503 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3505 sizeof(destroy_completion_handler
.shm_path
));
3510 * The session is destroyed. However, note that the command context
3511 * still holds a reference to the session, thus delaying its destruction
3512 * _at least_ up to the point when that reference is released.
3514 session_destroy(session
);
3515 if (reply_context
) {
3516 reply_context
->destruction_status
= destruction_last_error
;
3517 ret
= session_add_destroy_notifier(session
,
3518 cmd_destroy_session_reply
,
3519 (void *) reply_context
);
3521 ret
= LTTNG_ERR_FATAL
;
3533 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3535 int cmd_register_consumer(struct ltt_session
*session
,
3536 enum lttng_domain_type domain
, const char *sock_path
,
3537 struct consumer_data
*cdata
)
3540 struct consumer_socket
*socket
= NULL
;
3547 case LTTNG_DOMAIN_KERNEL
:
3549 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3553 /* Can't register a consumer if there is already one */
3554 if (ksess
->consumer_fds_sent
!= 0) {
3555 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3559 sock
= lttcomm_connect_unix_sock(sock_path
);
3561 ret
= LTTNG_ERR_CONNECT_FAIL
;
3564 cdata
->cmd_sock
= sock
;
3566 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3567 if (socket
== NULL
) {
3570 PERROR("close register consumer");
3572 cdata
->cmd_sock
= -1;
3573 ret
= LTTNG_ERR_FATAL
;
3577 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3578 if (socket
->lock
== NULL
) {
3579 PERROR("zmalloc pthread mutex");
3580 ret
= LTTNG_ERR_FATAL
;
3583 pthread_mutex_init(socket
->lock
, NULL
);
3584 socket
->registered
= 1;
3587 consumer_add_socket(socket
, ksess
->consumer
);
3590 pthread_mutex_lock(&cdata
->pid_mutex
);
3592 pthread_mutex_unlock(&cdata
->pid_mutex
);
3597 /* TODO: Userspace tracing */
3598 ret
= LTTNG_ERR_UND
;
3606 consumer_destroy_socket(socket
);
3612 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3614 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3615 struct lttng_domain
**domains
)
3620 struct lttng_ht_iter iter
;
3622 if (session
->kernel_session
!= NULL
) {
3623 DBG3("Listing domains found kernel domain");
3627 if (session
->ust_session
!= NULL
) {
3628 DBG3("Listing domains found UST global domain");
3632 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3634 if (agt
->being_used
) {
3645 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3646 if (*domains
== NULL
) {
3647 ret
= LTTNG_ERR_FATAL
;
3651 if (session
->kernel_session
!= NULL
) {
3652 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3654 /* Kernel session buffer type is always GLOBAL */
3655 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3660 if (session
->ust_session
!= NULL
) {
3661 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3662 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3666 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3668 if (agt
->being_used
) {
3669 (*domains
)[index
].type
= agt
->domain
;
3670 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3680 /* Return negative value to differentiate return code */
3686 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3688 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3689 struct ltt_session
*session
,
3690 struct lttng_payload
*payload
)
3694 struct lttcomm_list_command_header cmd_header
= {};
3695 size_t cmd_header_offset
;
3696 enum lttng_error_code ret_code
;
3701 DBG("Listing channels for session %s", session
->name
);
3703 cmd_header_offset
= payload
->buffer
.size
;
3705 /* Reserve space for command reply header. */
3706 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3707 cmd_header_offset
+ sizeof(cmd_header
));
3709 ret_code
= LTTNG_ERR_NOMEM
;
3714 case LTTNG_DOMAIN_KERNEL
:
3716 /* Kernel channels */
3717 struct ltt_kernel_channel
*kchan
;
3718 if (session
->kernel_session
!= NULL
) {
3719 cds_list_for_each_entry(kchan
,
3720 &session
->kernel_session
->channel_list
.head
, list
) {
3721 uint64_t discarded_events
, lost_packets
;
3722 struct lttng_channel_extended
*extended
;
3724 extended
= (struct lttng_channel_extended
*)
3725 kchan
->channel
->attr
.extended
.ptr
;
3727 ret
= get_kernel_runtime_stats(session
, kchan
,
3728 &discarded_events
, &lost_packets
);
3730 ret_code
= LTTNG_ERR_UNK
;
3735 * Update the discarded_events and lost_packets
3736 * count for the channel
3738 extended
->discarded_events
= discarded_events
;
3739 extended
->lost_packets
= lost_packets
;
3741 ret
= lttng_channel_serialize(
3742 kchan
->channel
, &payload
->buffer
);
3744 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3745 kchan
->channel
->name
);
3746 ret_code
= LTTNG_ERR_UNK
;
3755 case LTTNG_DOMAIN_UST
:
3757 struct lttng_ht_iter iter
;
3758 struct ltt_ust_channel
*uchan
;
3761 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3762 &iter
.iter
, uchan
, node
.node
) {
3763 uint64_t discarded_events
= 0, lost_packets
= 0;
3764 struct lttng_channel
*channel
= NULL
;
3765 struct lttng_channel_extended
*extended
;
3767 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3769 ret_code
= LTTNG_ERR_NOMEM
;
3773 extended
= (struct lttng_channel_extended
*)
3774 channel
->attr
.extended
.ptr
;
3776 ret
= get_ust_runtime_stats(session
, uchan
,
3777 &discarded_events
, &lost_packets
);
3779 lttng_channel_destroy(channel
);
3780 ret_code
= LTTNG_ERR_UNK
;
3784 extended
->discarded_events
= discarded_events
;
3785 extended
->lost_packets
= lost_packets
;
3787 ret
= lttng_channel_serialize(
3788 channel
, &payload
->buffer
);
3790 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3792 lttng_channel_destroy(channel
);
3793 ret_code
= LTTNG_ERR_UNK
;
3797 lttng_channel_destroy(channel
);
3807 if (i
> UINT32_MAX
) {
3808 ERR("Channel count would overflow the channel listing command's reply");
3809 ret_code
= LTTNG_ERR_OVERFLOW
;
3813 /* Update command reply header. */
3814 cmd_header
.count
= (uint32_t) i
;
3815 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3816 sizeof(cmd_header
));
3817 ret_code
= LTTNG_OK
;
3824 * Command LTTNG_LIST_EVENTS processed by the client thread.
3826 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3827 struct ltt_session
*session
,
3829 struct lttng_payload
*reply_payload
)
3831 int buffer_resize_ret
;
3832 enum lttng_error_code ret_code
= LTTNG_OK
;
3833 struct lttcomm_list_command_header reply_command_header
= {};
3834 size_t reply_command_header_offset
;
3835 unsigned int nb_events
= 0;
3837 assert(reply_payload
);
3839 /* Reserve space for command reply header. */
3840 reply_command_header_offset
= reply_payload
->buffer
.size
;
3841 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3842 reply_command_header_offset
+
3843 sizeof(struct lttcomm_list_command_header
));
3844 if (buffer_resize_ret
) {
3845 ret_code
= LTTNG_ERR_NOMEM
;
3850 case LTTNG_DOMAIN_KERNEL
:
3851 if (session
->kernel_session
!= NULL
) {
3852 ret_code
= list_lttng_kernel_events(channel_name
,
3853 session
->kernel_session
, reply_payload
, &nb_events
);
3857 case LTTNG_DOMAIN_UST
:
3859 if (session
->ust_session
!= NULL
) {
3860 ret_code
= list_lttng_ust_global_events(channel_name
,
3861 &session
->ust_session
->domain_global
,
3862 reply_payload
, &nb_events
);
3867 case LTTNG_DOMAIN_LOG4J
:
3868 case LTTNG_DOMAIN_JUL
:
3869 case LTTNG_DOMAIN_PYTHON
:
3870 if (session
->ust_session
) {
3871 struct lttng_ht_iter iter
;
3875 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3876 &iter
.iter
, agt
, node
.node
) {
3877 if (agt
->domain
== domain
) {
3878 ret_code
= list_lttng_agent_events(
3879 agt
, reply_payload
, &nb_events
);
3888 ret_code
= LTTNG_ERR_UND
;
3892 if (nb_events
> UINT32_MAX
) {
3893 ret_code
= LTTNG_ERR_OVERFLOW
;
3897 /* Update command reply header. */
3898 reply_command_header
.count
= (uint32_t) nb_events
;
3899 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
3900 sizeof(reply_command_header
));
3907 * Using the session list, filled a lttng_session array to send back to the
3908 * client for session listing.
3910 * The session list lock MUST be acquired before calling this function. Use
3911 * session_lock_list() and session_unlock_list().
3913 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3914 size_t session_count
, uid_t uid
, gid_t gid
)
3918 struct ltt_session
*session
;
3919 struct ltt_session_list
*list
= session_get_list();
3920 struct lttng_session_extended
*extended
=
3921 (typeof(extended
)) (&sessions
[session_count
]);
3923 DBG("Getting all available session for UID %d GID %d",
3926 * Iterate over session list and append data after the control struct in
3929 cds_list_for_each_entry(session
, &list
->head
, list
) {
3930 if (!session_get(session
)) {
3934 * Only list the sessions the user can control.
3936 if (!session_access_ok(session
, uid
) ||
3937 session
->destroyed
) {
3938 session_put(session
);
3942 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3943 struct ltt_ust_session
*usess
= session
->ust_session
;
3945 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3946 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3947 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3948 ret
= build_network_session_path(sessions
[i
].path
,
3949 sizeof(sessions
[i
].path
), session
);
3951 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3952 session
->consumer
->dst
.session_root_path
);
3955 PERROR("snprintf session path");
3956 session_put(session
);
3960 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3961 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3962 sessions
[i
].enabled
= session
->active
;
3963 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3964 sessions
[i
].live_timer_interval
= session
->live_timer
;
3965 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3966 extended
[i
].creation_time
.is_set
= 1;
3968 session_put(session
);
3973 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3974 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3976 int cmd_data_pending(struct ltt_session
*session
)
3979 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3980 struct ltt_ust_session
*usess
= session
->ust_session
;
3984 DBG("Data pending for session %s", session
->name
);
3986 /* Session MUST be stopped to ask for data availability. */
3987 if (session
->active
) {
3988 ret
= LTTNG_ERR_SESSION_STARTED
;
3992 * If stopped, just make sure we've started before else the above call
3993 * will always send that there is data pending.
3995 * The consumer assumes that when the data pending command is received,
3996 * the trace has been started before or else no output data is written
3997 * by the streams which is a condition for data pending. So, this is
3998 * *VERY* important that we don't ask the consumer before a start
4001 if (!session
->has_been_started
) {
4007 /* A rotation is still pending, we have to wait. */
4008 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4009 DBG("Rotate still pending for session %s", session
->name
);
4014 if (ksess
&& ksess
->consumer
) {
4015 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4017 /* Data is still being extracted for the kernel. */
4022 if (usess
&& usess
->consumer
) {
4023 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4025 /* Data is still being extracted for the kernel. */
4030 /* Data is ready to be read by a viewer */
4038 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4040 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4042 int cmd_snapshot_add_output(struct ltt_session
*session
,
4043 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4046 struct snapshot_output
*new_output
;
4051 DBG("Cmd snapshot add output for session %s", session
->name
);
4054 * Can't create an output if the session is not set in no-output mode.
4056 if (session
->output_traces
) {
4057 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4061 if (session
->has_non_mmap_channel
) {
4062 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4066 /* Only one output is allowed until we have the "tee" feature. */
4067 if (session
->snapshot
.nb_output
== 1) {
4068 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4072 new_output
= snapshot_output_alloc();
4074 ret
= LTTNG_ERR_NOMEM
;
4078 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4079 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4080 &session
->snapshot
);
4082 if (ret
== -ENOMEM
) {
4083 ret
= LTTNG_ERR_NOMEM
;
4085 ret
= LTTNG_ERR_INVALID
;
4091 snapshot_add_output(&session
->snapshot
, new_output
);
4093 *id
= new_output
->id
;
4100 snapshot_output_destroy(new_output
);
4106 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4108 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4110 int cmd_snapshot_del_output(struct ltt_session
*session
,
4111 const struct lttng_snapshot_output
*output
)
4114 struct snapshot_output
*sout
= NULL
;
4122 * Permission denied to create an output if the session is not
4123 * set in no output mode.
4125 if (session
->output_traces
) {
4126 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4131 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4133 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4134 } else if (*output
->name
!= '\0') {
4135 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4137 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4140 ret
= LTTNG_ERR_INVALID
;
4144 snapshot_delete_output(&session
->snapshot
, sout
);
4145 snapshot_output_destroy(sout
);
4154 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4156 * If no output is available, outputs is untouched and 0 is returned.
4158 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4160 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4161 struct lttng_snapshot_output
**outputs
)
4164 struct lttng_snapshot_output
*list
= NULL
;
4165 struct lttng_ht_iter iter
;
4166 struct snapshot_output
*output
;
4171 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4174 * Permission denied to create an output if the session is not
4175 * set in no output mode.
4177 if (session
->output_traces
) {
4178 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4182 if (session
->snapshot
.nb_output
== 0) {
4187 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4189 ret
= -LTTNG_ERR_NOMEM
;
4193 /* Copy list from session to the new list object. */
4195 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4196 output
, node
.node
) {
4197 assert(output
->consumer
);
4198 list
[idx
].id
= output
->id
;
4199 list
[idx
].max_size
= output
->max_size
;
4200 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4201 sizeof(list
[idx
].name
))) {
4202 ret
= -LTTNG_ERR_INVALID
;
4205 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4206 if (lttng_strncpy(list
[idx
].ctrl_url
,
4207 output
->consumer
->dst
.session_root_path
,
4208 sizeof(list
[idx
].ctrl_url
))) {
4209 ret
= -LTTNG_ERR_INVALID
;
4214 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4215 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4217 ret
= -LTTNG_ERR_NOMEM
;
4222 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4223 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4225 ret
= -LTTNG_ERR_NOMEM
;
4234 ret
= session
->snapshot
.nb_output
;
4243 * Check if we can regenerate the metadata for this session.
4244 * Only kernel, UST per-uid and non-live sessions are supported.
4246 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4249 int check_regenerate_metadata_support(struct ltt_session
*session
)
4255 if (session
->live_timer
!= 0) {
4256 ret
= LTTNG_ERR_LIVE_SESSION
;
4259 if (!session
->active
) {
4260 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4263 if (session
->ust_session
) {
4264 switch (session
->ust_session
->buffer_type
) {
4265 case LTTNG_BUFFER_PER_UID
:
4267 case LTTNG_BUFFER_PER_PID
:
4268 ret
= LTTNG_ERR_PER_PID_SESSION
;
4272 ret
= LTTNG_ERR_UNK
;
4276 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4277 session
->consumer
->relay_minor_version
< 8) {
4278 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4288 int clear_metadata_file(int fd
)
4293 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4294 if (lseek_ret
< 0) {
4300 ret
= ftruncate(fd
, 0);
4302 PERROR("ftruncate");
4311 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4314 struct buffer_reg_uid
*uid_reg
= NULL
;
4315 struct buffer_reg_session
*session_reg
= NULL
;
4318 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4319 struct ust_registry_session
*registry
;
4320 struct ust_registry_channel
*chan
;
4321 struct lttng_ht_iter iter_chan
;
4323 session_reg
= uid_reg
->registry
;
4324 registry
= session_reg
->reg
.ust
;
4326 pthread_mutex_lock(®istry
->lock
);
4327 registry
->metadata_len_sent
= 0;
4328 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4329 registry
->metadata_len
= 0;
4330 registry
->metadata_version
++;
4331 if (registry
->metadata_fd
> 0) {
4332 /* Clear the metadata file's content. */
4333 ret
= clear_metadata_file(registry
->metadata_fd
);
4335 pthread_mutex_unlock(®istry
->lock
);
4340 ret
= ust_metadata_session_statedump(registry
, NULL
,
4341 registry
->major
, registry
->minor
);
4343 pthread_mutex_unlock(®istry
->lock
);
4344 ERR("Failed to generate session metadata (err = %d)",
4348 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4350 struct ust_registry_event
*event
;
4351 struct lttng_ht_iter iter_event
;
4353 ret
= ust_metadata_channel_statedump(registry
, chan
);
4355 pthread_mutex_unlock(®istry
->lock
);
4356 ERR("Failed to generate channel metadata "
4360 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4362 ret
= ust_metadata_event_statedump(registry
,
4365 pthread_mutex_unlock(®istry
->lock
);
4366 ERR("Failed to generate event metadata "
4372 pthread_mutex_unlock(®istry
->lock
);
4381 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4383 * Ask the consumer to truncate the existing metadata file(s) and
4384 * then regenerate the metadata. Live and per-pid sessions are not
4385 * supported and return an error.
4387 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4389 int cmd_regenerate_metadata(struct ltt_session
*session
)
4395 ret
= check_regenerate_metadata_support(session
);
4400 if (session
->kernel_session
) {
4401 ret
= kernctl_session_regenerate_metadata(
4402 session
->kernel_session
->fd
);
4404 ERR("Failed to regenerate the kernel metadata");
4409 if (session
->ust_session
) {
4410 ret
= ust_regenerate_metadata(session
->ust_session
);
4412 ERR("Failed to regenerate the UST metadata");
4416 DBG("Cmd metadata regenerate for session %s", session
->name
);
4424 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4426 * Ask the tracer to regenerate a new statedump.
4428 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4430 int cmd_regenerate_statedump(struct ltt_session
*session
)
4436 if (!session
->active
) {
4437 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4441 if (session
->kernel_session
) {
4442 ret
= kernctl_session_regenerate_statedump(
4443 session
->kernel_session
->fd
);
4445 * Currently, the statedump in kernel can only fail if out
4449 if (ret
== -ENOMEM
) {
4450 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4452 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4454 ERR("Failed to regenerate the kernel statedump");
4459 if (session
->ust_session
) {
4460 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4462 * Currently, the statedump in UST always returns 0.
4465 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4466 ERR("Failed to regenerate the UST statedump");
4470 DBG("Cmd regenerate statedump for session %s", session
->name
);
4478 enum lttng_error_code
synchronize_tracer_notifier_register(
4479 struct notification_thread_handle
*notification_thread
,
4480 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4482 enum lttng_error_code ret_code
;
4483 const struct lttng_condition
*condition
=
4484 lttng_trigger_get_const_condition(trigger
);
4485 const char *trigger_name
;
4486 uid_t trigger_owner
;
4487 enum lttng_trigger_status trigger_status
;
4488 const enum lttng_domain_type trigger_domain
=
4489 lttng_trigger_get_underlying_domain_type_restriction(
4492 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4493 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4496 assert(lttng_condition_get_type(condition
) ==
4497 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4499 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4500 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4501 trigger_name
: "(anonymous)";
4503 session_lock_list();
4504 switch (trigger_domain
) {
4505 case LTTNG_DOMAIN_KERNEL
:
4507 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4508 if (ret_code
!= LTTNG_OK
) {
4509 enum lttng_error_code notif_thread_unregister_ret
;
4511 notif_thread_unregister_ret
=
4512 notification_thread_command_unregister_trigger(
4513 notification_thread
, trigger
);
4515 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4516 /* Return the original error code. */
4517 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4519 (int) trigger_owner
,
4525 case LTTNG_DOMAIN_UST
:
4526 ust_app_global_update_all_event_notifier_rules();
4528 case LTTNG_DOMAIN_JUL
:
4529 case LTTNG_DOMAIN_LOG4J
:
4530 case LTTNG_DOMAIN_PYTHON
:
4532 /* Agent domains. */
4533 struct agent
*agt
= agent_find_by_event_notifier_domain(
4537 agt
= agent_create(trigger_domain
);
4539 ret_code
= LTTNG_ERR_NOMEM
;
4540 goto end_unlock_session_list
;
4543 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4546 ret_code
= trigger_agent_enable(trigger
, agt
);
4547 if (ret_code
!= LTTNG_OK
) {
4548 goto end_unlock_session_list
;
4553 case LTTNG_DOMAIN_NONE
:
4558 ret_code
= LTTNG_OK
;
4559 end_unlock_session_list
:
4560 session_unlock_list();
4564 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4565 struct lttng_trigger
*trigger
,
4566 bool is_trigger_anonymous
,
4567 struct notification_thread_handle
*notification_thread
,
4568 struct lttng_trigger
**return_trigger
)
4570 enum lttng_error_code ret_code
;
4571 const char *trigger_name
;
4572 uid_t trigger_owner
;
4573 enum lttng_trigger_status trigger_status
;
4575 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4576 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4577 trigger_name
: "(anonymous)";
4579 trigger_status
= lttng_trigger_get_owner_uid(
4580 trigger
, &trigger_owner
);
4581 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4583 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4584 trigger_name
, (int) trigger_owner
,
4585 (int) lttng_credentials_get_uid(cmd_creds
));
4588 * Validate the trigger credentials against the command credentials.
4589 * Only the root user can register a trigger with non-matching
4592 if (!lttng_credentials_is_equal_uid(
4593 lttng_trigger_get_credentials(trigger
),
4595 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4596 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4597 trigger_name
, (int) trigger_owner
,
4598 (int) lttng_credentials_get_uid(cmd_creds
));
4599 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4605 * The bytecode generation also serves as a validation step for the
4606 * bytecode expressions.
4608 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4609 if (ret_code
!= LTTNG_OK
) {
4610 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4611 trigger_name
, (int) trigger_owner
, ret_code
);
4616 * A reference to the trigger is acquired by the notification thread.
4617 * It is safe to return the same trigger to the caller since it the
4618 * other user holds a reference.
4620 * The trigger is modified during the execution of the
4621 * "register trigger" command. However, by the time the command returns,
4622 * it is safe to use without any locking as its properties are
4625 ret_code
= notification_thread_command_register_trigger(
4626 notification_thread
, trigger
, is_trigger_anonymous
);
4627 if (ret_code
!= LTTNG_OK
) {
4628 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4629 trigger_name
, (int) trigger_owner
, ret_code
);
4633 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4634 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4635 trigger_name
: "(anonymous)";
4638 * Synchronize tracers if the trigger adds an event notifier.
4640 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4641 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4642 trigger
, cmd_creds
);
4643 if (ret_code
!= LTTNG_OK
) {
4644 ERR("Error registering tracer notifier: %s",
4645 lttng_strerror(-ret_code
));
4651 * Return an updated trigger to the client.
4653 * Since a modified version of the same trigger is returned, acquire a
4654 * reference to the trigger so the caller doesn't have to care if those
4655 * are distinct instances or not.
4657 if (ret_code
== LTTNG_OK
) {
4658 lttng_trigger_get(trigger
);
4659 *return_trigger
= trigger
;
4660 /* Ownership of trigger was transferred to caller. */
4668 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4669 const struct lttng_trigger
*trigger
)
4671 enum lttng_error_code ret_code
;
4672 const struct lttng_condition
*condition
=
4673 lttng_trigger_get_const_condition(trigger
);
4674 const enum lttng_domain_type trigger_domain
=
4675 lttng_trigger_get_underlying_domain_type_restriction(
4679 assert(lttng_condition_get_type(condition
) ==
4680 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4682 session_lock_list();
4683 switch (trigger_domain
) {
4684 case LTTNG_DOMAIN_KERNEL
:
4685 ret_code
= kernel_unregister_event_notifier(trigger
);
4686 if (ret_code
!= LTTNG_OK
) {
4687 goto end_unlock_session_list
;
4691 case LTTNG_DOMAIN_UST
:
4692 ust_app_global_update_all_event_notifier_rules();
4694 case LTTNG_DOMAIN_JUL
:
4695 case LTTNG_DOMAIN_LOG4J
:
4696 case LTTNG_DOMAIN_PYTHON
:
4698 /* Agent domains. */
4699 struct agent
*agt
= agent_find_by_event_notifier_domain(
4703 * This trigger was never registered in the first place. Calling
4704 * this function under those circumstances is an internal error.
4707 ret_code
= trigger_agent_disable(trigger
, agt
);
4708 if (ret_code
!= LTTNG_OK
) {
4709 goto end_unlock_session_list
;
4714 case LTTNG_DOMAIN_NONE
:
4719 ret_code
= LTTNG_OK
;
4721 end_unlock_session_list
:
4722 session_unlock_list();
4726 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4727 const struct lttng_trigger
*trigger
,
4728 struct notification_thread_handle
*notification_thread
)
4730 enum lttng_error_code ret_code
;
4731 const char *trigger_name
;
4732 uid_t trigger_owner
;
4733 enum lttng_trigger_status trigger_status
;
4734 struct lttng_trigger
*sessiond_trigger
= NULL
;
4736 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4737 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4738 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4739 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4741 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4742 trigger_name
, (int) trigger_owner
,
4743 (int) lttng_credentials_get_uid(cmd_creds
));
4746 * Validate the trigger credentials against the command credentials.
4747 * Only the root user can unregister a trigger with non-matching
4750 if (!lttng_credentials_is_equal_uid(
4751 lttng_trigger_get_credentials(trigger
),
4753 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4754 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4755 trigger_name
, (int) trigger_owner
,
4756 (int) lttng_credentials_get_uid(cmd_creds
));
4757 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4762 /* Fetch the sessiond side trigger object. */
4763 ret_code
= notification_thread_command_get_trigger(
4764 notification_thread
, trigger
, &sessiond_trigger
);
4765 if (ret_code
!= LTTNG_OK
) {
4766 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4767 trigger_name
, (int) trigger_owner
, ret_code
);
4771 assert(sessiond_trigger
);
4774 * From this point on, no matter what, consider the trigger
4777 * We set the unregistered state of the sessiond side trigger object in
4778 * the client thread since we want to minimize the possibility of the
4779 * notification thread being stalled due to a long execution of an
4780 * action that required the trigger lock.
4782 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4784 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4786 if (ret_code
!= LTTNG_OK
) {
4787 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4788 trigger_name
, (int) trigger_owner
, ret_code
);
4793 * Synchronize tracers if the trigger removes an event notifier.
4794 * Do this even if the trigger unregistration failed to at least stop
4795 * the tracers from producing notifications associated with this
4798 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4799 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4800 if (ret_code
!= LTTNG_OK
) {
4801 ERR("Error unregistering trigger to tracer.");
4808 lttng_trigger_put(sessiond_trigger
);
4812 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4813 struct notification_thread_handle
*notification_thread
,
4814 struct lttng_triggers
**return_triggers
)
4817 enum lttng_error_code ret_code
;
4818 struct lttng_triggers
*triggers
= NULL
;
4820 /* Get the set of triggers from the notification thread. */
4821 ret_code
= notification_thread_command_list_triggers(
4822 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4823 if (ret_code
!= LTTNG_OK
) {
4827 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4829 ret_code
= LTTNG_ERR_UNK
;
4833 *return_triggers
= triggers
;
4835 ret_code
= LTTNG_OK
;
4837 lttng_triggers_destroy(triggers
);
4841 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4842 const struct lttng_error_query
*query
,
4843 struct lttng_error_query_results
**_results
,
4844 struct notification_thread_handle
*notification_thread
)
4846 enum lttng_error_code ret_code
;
4847 const struct lttng_trigger
*query_target_trigger
;
4848 const struct lttng_action
*query_target_action
= NULL
;
4849 struct lttng_trigger
*matching_trigger
= NULL
;
4850 const char *trigger_name
;
4851 uid_t trigger_owner
;
4852 enum lttng_trigger_status trigger_status
;
4853 struct lttng_error_query_results
*results
= NULL
;
4855 switch (lttng_error_query_get_target_type(query
)) {
4856 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4857 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4859 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4860 query_target_trigger
=
4861 lttng_error_query_condition_borrow_target(query
);
4863 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4864 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4871 assert(query_target_trigger
);
4873 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4874 query_target_trigger
, &matching_trigger
);
4875 if (ret_code
!= LTTNG_OK
) {
4879 /* No longer needed. */
4880 query_target_trigger
= NULL
;
4882 if (lttng_error_query_get_target_type(query
) ==
4883 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4884 /* Get the sessiond-side version of the target action. */
4885 query_target_action
=
4886 lttng_error_query_action_borrow_action_target(
4887 query
, matching_trigger
);
4890 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4891 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4892 trigger_name
: "(anonymous)";
4893 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4895 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4897 results
= lttng_error_query_results_create();
4899 ret_code
= LTTNG_ERR_NOMEM
;
4903 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4904 trigger_name
, (int) trigger_owner
,
4905 (int) lttng_credentials_get_uid(cmd_creds
));
4908 * Validate the trigger credentials against the command credentials.
4909 * Only the root user can target a trigger with non-matching
4912 if (!lttng_credentials_is_equal_uid(
4913 lttng_trigger_get_credentials(matching_trigger
),
4915 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4916 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4917 trigger_name
, (int) trigger_owner
,
4918 (int) lttng_credentials_get_uid(cmd_creds
));
4919 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4924 switch (lttng_error_query_get_target_type(query
)) {
4925 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4926 trigger_status
= lttng_trigger_add_error_results(
4927 matching_trigger
, results
);
4929 switch (trigger_status
) {
4930 case LTTNG_TRIGGER_STATUS_OK
:
4933 ret_code
= LTTNG_ERR_UNK
;
4938 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4940 trigger_status
= lttng_trigger_condition_add_error_results(
4941 matching_trigger
, results
);
4943 switch (trigger_status
) {
4944 case LTTNG_TRIGGER_STATUS_OK
:
4947 ret_code
= LTTNG_ERR_UNK
;
4953 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4955 const enum lttng_action_status action_status
=
4956 lttng_action_add_error_query_results(
4957 query_target_action
, results
);
4959 switch (action_status
) {
4960 case LTTNG_ACTION_STATUS_OK
:
4963 ret_code
= LTTNG_ERR_UNK
;
4974 *_results
= results
;
4976 ret_code
= LTTNG_OK
;
4978 lttng_trigger_put(matching_trigger
);
4979 lttng_error_query_results_destroy(results
);
4984 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4985 * snapshot output is *not* set with a remote destination.
4987 * Return LTTNG_OK on success or a LTTNG_ERR code.
4989 static enum lttng_error_code
set_relayd_for_snapshot(
4990 struct consumer_output
*output
,
4991 const struct ltt_session
*session
)
4993 enum lttng_error_code status
= LTTNG_OK
;
4994 struct lttng_ht_iter iter
;
4995 struct consumer_socket
*socket
;
4996 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4997 const char *base_path
;
5002 DBG2("Set relayd object from snapshot output");
5004 if (session
->current_trace_chunk
) {
5005 enum lttng_trace_chunk_status chunk_status
=
5006 lttng_trace_chunk_get_id(
5007 session
->current_trace_chunk
,
5008 ¤t_chunk_id
.value
);
5010 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5011 current_chunk_id
.is_set
= true;
5013 ERR("Failed to get current trace chunk id");
5014 status
= LTTNG_ERR_UNK
;
5019 /* Ignore if snapshot consumer output is not network. */
5020 if (output
->type
!= CONSUMER_DST_NET
) {
5025 * The snapshot record URI base path overrides the session
5028 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5029 base_path
= output
->dst
.net
.control
.subdir
;
5031 base_path
= session
->base_path
;
5035 * For each consumer socket, create and send the relayd object of the
5039 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
5040 socket
, node
.node
) {
5041 pthread_mutex_lock(socket
->lock
);
5042 status
= send_consumer_relayd_sockets(0, session
->id
,
5044 session
->name
, session
->hostname
,
5046 session
->live_timer
,
5047 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
5048 session
->creation_time
,
5049 session
->name_contains_creation_time
);
5050 pthread_mutex_unlock(socket
->lock
);
5051 if (status
!= LTTNG_OK
) {
5063 * Record a kernel snapshot.
5065 * Return LTTNG_OK on success or a LTTNG_ERR code.
5067 static enum lttng_error_code
record_kernel_snapshot(
5068 struct ltt_kernel_session
*ksess
,
5069 const struct consumer_output
*output
,
5070 const struct ltt_session
*session
,
5071 int wait
, uint64_t nb_packets_per_stream
)
5073 enum lttng_error_code status
;
5079 status
= kernel_snapshot_record(
5080 ksess
, output
, wait
, nb_packets_per_stream
);
5085 * Record a UST snapshot.
5087 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5089 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5090 const struct consumer_output
*output
,
5091 const struct ltt_session
*session
,
5092 int wait
, uint64_t nb_packets_per_stream
)
5094 enum lttng_error_code status
;
5100 status
= ust_app_snapshot_record(
5101 usess
, output
, wait
, nb_packets_per_stream
);
5106 uint64_t get_session_size_one_more_packet_per_stream(
5107 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5109 uint64_t tot_size
= 0;
5111 if (session
->kernel_session
) {
5112 struct ltt_kernel_channel
*chan
;
5113 const struct ltt_kernel_session
*ksess
=
5114 session
->kernel_session
;
5116 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5117 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5119 * Don't take channel into account if we
5120 * already grab all its packets.
5124 tot_size
+= chan
->channel
->attr
.subbuf_size
5125 * chan
->stream_count
;
5129 if (session
->ust_session
) {
5130 const struct ltt_ust_session
*usess
= session
->ust_session
;
5132 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5140 * Calculate the number of packets we can grab from each stream that
5141 * fits within the overall snapshot max size.
5143 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5144 * the number of packets per stream.
5146 * TODO: this approach is not perfect: we consider the worse case
5147 * (packet filling the sub-buffers) as an upper bound, but we could do
5148 * better if we do this calculation while we actually grab the packet
5149 * content: we would know how much padding we don't actually store into
5152 * This algorithm is currently bounded by the number of packets per
5155 * Since we call this algorithm before actually grabbing the data, it's
5156 * an approximation: for instance, applications could appear/disappear
5157 * in between this call and actually grabbing data.
5160 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5164 uint64_t cur_nb_packets
= 0;
5167 return 0; /* Infinite */
5170 size_left
= max_size
;
5172 uint64_t one_more_packet_tot_size
;
5174 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5175 session
, cur_nb_packets
);
5176 if (!one_more_packet_tot_size
) {
5177 /* We are already grabbing all packets. */
5180 size_left
-= one_more_packet_tot_size
;
5181 if (size_left
< 0) {
5186 if (!cur_nb_packets
&& size_left
!= max_size
) {
5187 /* Not enough room to grab one packet of each stream, error. */
5190 return cur_nb_packets
;
5194 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5195 const struct snapshot_output
*snapshot_output
, int wait
)
5197 int64_t nb_packets_per_stream
;
5198 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5200 enum lttng_error_code ret_code
= LTTNG_OK
;
5201 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5202 struct consumer_output
*original_ust_consumer_output
= NULL
;
5203 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5204 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5205 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5207 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5209 snapshot_output
->name
,
5210 snapshot_output
->datetime
,
5211 snapshot_output
->nb_snapshot
);
5212 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5213 ERR("Failed to format snapshot name");
5214 ret_code
= LTTNG_ERR_INVALID
;
5217 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5218 snapshot_output
->name
, session
->name
,
5219 snapshot_chunk_name
);
5220 if (!session
->kernel_session
&& !session
->ust_session
) {
5221 ERR("Failed to record snapshot as no channels exist");
5222 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5226 if (session
->kernel_session
) {
5227 original_kernel_consumer_output
=
5228 session
->kernel_session
->consumer
;
5229 snapshot_kernel_consumer_output
=
5230 consumer_copy_output(snapshot_output
->consumer
);
5231 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5232 snapshot_chunk_name
);
5234 /* Copy the original domain subdir. */
5235 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5236 original_kernel_consumer_output
->domain_subdir
);
5238 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5239 original_kernel_consumer_output
);
5241 ERR("Failed to copy consumer sockets from snapshot output configuration");
5242 ret_code
= LTTNG_ERR_NOMEM
;
5245 ret_code
= set_relayd_for_snapshot(
5246 snapshot_kernel_consumer_output
, session
);
5247 if (ret_code
!= LTTNG_OK
) {
5248 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5251 session
->kernel_session
->consumer
=
5252 snapshot_kernel_consumer_output
;
5254 if (session
->ust_session
) {
5255 original_ust_consumer_output
= session
->ust_session
->consumer
;
5256 snapshot_ust_consumer_output
=
5257 consumer_copy_output(snapshot_output
->consumer
);
5258 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5259 snapshot_chunk_name
);
5261 /* Copy the original domain subdir. */
5262 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5263 original_ust_consumer_output
->domain_subdir
);
5265 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5266 original_ust_consumer_output
);
5268 ERR("Failed to copy consumer sockets from snapshot output configuration");
5269 ret_code
= LTTNG_ERR_NOMEM
;
5272 ret_code
= set_relayd_for_snapshot(
5273 snapshot_ust_consumer_output
, session
);
5274 if (ret_code
!= LTTNG_OK
) {
5275 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5278 session
->ust_session
->consumer
=
5279 snapshot_ust_consumer_output
;
5282 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5283 snapshot_kernel_consumer_output
?:
5284 snapshot_ust_consumer_output
,
5285 consumer_output_get_base_path(
5286 snapshot_output
->consumer
),
5287 snapshot_chunk_name
);
5288 if (!snapshot_trace_chunk
) {
5289 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5291 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5294 assert(!session
->current_trace_chunk
);
5295 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5296 lttng_trace_chunk_put(snapshot_trace_chunk
);
5297 snapshot_trace_chunk
= NULL
;
5299 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5301 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5305 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5306 snapshot_output
->max_size
);
5307 if (nb_packets_per_stream
< 0) {
5308 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5309 goto error_close_trace_chunk
;
5312 if (session
->kernel_session
) {
5313 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5314 snapshot_kernel_consumer_output
, session
,
5315 wait
, nb_packets_per_stream
);
5316 if (ret_code
!= LTTNG_OK
) {
5317 goto error_close_trace_chunk
;
5321 if (session
->ust_session
) {
5322 ret_code
= record_ust_snapshot(session
->ust_session
,
5323 snapshot_ust_consumer_output
, session
,
5324 wait
, nb_packets_per_stream
);
5325 if (ret_code
!= LTTNG_OK
) {
5326 goto error_close_trace_chunk
;
5330 error_close_trace_chunk
:
5331 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5332 ERR("Failed to release the current trace chunk of session \"%s\"",
5334 ret_code
= LTTNG_ERR_UNK
;
5337 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5338 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5340 * Don't goto end; make sure the chunk is closed for the session
5341 * to allow future snapshots.
5343 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5345 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5348 lttng_trace_chunk_put(snapshot_trace_chunk
);
5349 snapshot_trace_chunk
= NULL
;
5351 if (original_ust_consumer_output
) {
5352 session
->ust_session
->consumer
= original_ust_consumer_output
;
5354 if (original_kernel_consumer_output
) {
5355 session
->kernel_session
->consumer
=
5356 original_kernel_consumer_output
;
5358 consumer_output_put(snapshot_ust_consumer_output
);
5359 consumer_output_put(snapshot_kernel_consumer_output
);
5364 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5366 * The wait parameter is ignored so this call always wait for the snapshot to
5367 * complete before returning.
5369 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5371 int cmd_snapshot_record(struct ltt_session
*session
,
5372 const struct lttng_snapshot_output
*output
, int wait
)
5374 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5376 unsigned int snapshot_success
= 0;
5378 struct snapshot_output
*tmp_output
= NULL
;
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
, wait
);
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
, wait
);
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
)
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
;
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 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",
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 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
;
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
= 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");