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 ltt_session
*session
, enum lttng_domain_type domain
,
1869 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1871 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1872 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1875 * Don't try to add a context if the session has been started at
1876 * some point in time before. The tracer does not allow it and would
1877 * result in a corrupted trace.
1879 if (session
->has_been_started
) {
1880 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1884 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1885 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1886 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1890 case LTTNG_DOMAIN_KERNEL
:
1891 assert(session
->kernel_session
);
1893 if (session
->kernel_session
->channel_count
== 0) {
1894 /* Create default channel */
1895 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1896 if (ret
!= LTTNG_OK
) {
1899 chan_kern_created
= 1;
1901 /* Add kernel context to kernel tracer */
1902 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1903 if (ret
!= LTTNG_OK
) {
1907 case LTTNG_DOMAIN_JUL
:
1908 case LTTNG_DOMAIN_LOG4J
:
1911 * Validate channel name.
1912 * If no channel name is given and the domain is JUL or LOG4J,
1913 * set it to the appropriate domain-specific channel name. If
1914 * a name is provided but does not match the expexted channel
1915 * name, return an error.
1917 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1918 strcmp(channel_name
,
1919 DEFAULT_JUL_CHANNEL_NAME
)) {
1920 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1922 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1923 strcmp(channel_name
,
1924 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1925 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1928 /* break is _not_ missing here. */
1930 case LTTNG_DOMAIN_UST
:
1932 struct ltt_ust_session
*usess
= session
->ust_session
;
1933 unsigned int chan_count
;
1937 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1938 if (chan_count
== 0) {
1939 struct lttng_channel
*attr
;
1940 /* Create default channel */
1941 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1943 ret
= LTTNG_ERR_FATAL
;
1947 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1948 if (ret
!= LTTNG_OK
) {
1952 channel_attr_destroy(attr
);
1953 chan_ust_created
= 1;
1956 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1957 free(app_ctx_provider_name
);
1959 app_ctx_name
= NULL
;
1960 app_ctx_provider_name
= NULL
;
1961 if (ret
!= LTTNG_OK
) {
1967 ret
= LTTNG_ERR_UND
;
1975 if (chan_kern_created
) {
1976 struct ltt_kernel_channel
*kchan
=
1977 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1978 session
->kernel_session
);
1979 /* Created previously, this should NOT fail. */
1981 kernel_destroy_channel(kchan
);
1984 if (chan_ust_created
) {
1985 struct ltt_ust_channel
*uchan
=
1986 trace_ust_find_channel_by_name(
1987 session
->ust_session
->domain_global
.channels
,
1988 DEFAULT_CHANNEL_NAME
);
1989 /* Created previously, this should NOT fail. */
1991 /* Remove from the channel list of the session. */
1992 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1994 trace_ust_destroy_channel(uchan
);
1997 free(app_ctx_provider_name
);
2002 static inline bool name_starts_with(const char *name
, const char *prefix
)
2004 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2006 return !strncmp(name
, prefix
, max_cmp_len
);
2009 /* Perform userspace-specific event name validation */
2010 static int validate_ust_event_name(const char *name
)
2020 * Check name against all internal UST event component namespaces used
2023 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2024 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2025 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2034 * Internal version of cmd_enable_event() with a supplemental
2035 * "internal_event" flag which is used to enable internal events which should
2036 * be hidden from clients. Such events are used in the agent implementation to
2037 * enable the events through which all "agent" events are funeled.
2039 static int _cmd_enable_event(struct ltt_session
*session
,
2040 const struct lttng_domain
*domain
,
2041 char *channel_name
, struct lttng_event
*event
,
2042 char *filter_expression
,
2043 struct lttng_bytecode
*filter
,
2044 struct lttng_event_exclusion
*exclusion
,
2045 int wpipe
, bool internal_event
)
2047 int ret
= 0, channel_created
= 0;
2048 struct lttng_channel
*attr
= NULL
;
2052 assert(channel_name
);
2054 /* If we have a filter, we must have its filter expression */
2055 assert(!(!!filter_expression
^ !!filter
));
2057 /* Normalize event name as a globbing pattern */
2058 strutils_normalize_star_glob_pattern(event
->name
);
2060 /* Normalize exclusion names as globbing patterns */
2064 for (i
= 0; i
< exclusion
->count
; i
++) {
2065 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2067 strutils_normalize_star_glob_pattern(name
);
2071 DBG("Enable event command for event \'%s\'", event
->name
);
2075 switch (domain
->type
) {
2076 case LTTNG_DOMAIN_KERNEL
:
2078 struct ltt_kernel_channel
*kchan
;
2081 * If a non-default channel has been created in the
2082 * session, explicitely require that -c chan_name needs
2085 if (session
->kernel_session
->has_non_default_channel
2086 && channel_name
[0] == '\0') {
2087 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2091 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2092 session
->kernel_session
);
2093 if (kchan
== NULL
) {
2094 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2095 LTTNG_BUFFER_GLOBAL
);
2097 ret
= LTTNG_ERR_FATAL
;
2100 if (lttng_strncpy(attr
->name
, channel_name
,
2101 sizeof(attr
->name
))) {
2102 ret
= LTTNG_ERR_INVALID
;
2106 ret
= cmd_enable_channel_internal(
2107 session
, domain
, attr
, wpipe
);
2108 if (ret
!= LTTNG_OK
) {
2111 channel_created
= 1;
2114 /* Get the newly created kernel channel pointer */
2115 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2116 session
->kernel_session
);
2117 if (kchan
== NULL
) {
2118 /* This sould not happen... */
2119 ret
= LTTNG_ERR_FATAL
;
2123 switch (event
->type
) {
2124 case LTTNG_EVENT_ALL
:
2126 char *filter_expression_a
= NULL
;
2127 struct lttng_bytecode
*filter_a
= NULL
;
2130 * We need to duplicate filter_expression and filter,
2131 * because ownership is passed to first enable
2134 if (filter_expression
) {
2135 filter_expression_a
= strdup(filter_expression
);
2136 if (!filter_expression_a
) {
2137 ret
= LTTNG_ERR_FATAL
;
2142 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2144 free(filter_expression_a
);
2145 ret
= LTTNG_ERR_FATAL
;
2148 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2150 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2151 ret
= event_kernel_enable_event(kchan
, event
,
2152 filter_expression
, filter
);
2153 /* We have passed ownership */
2154 filter_expression
= NULL
;
2156 if (ret
!= LTTNG_OK
) {
2157 if (channel_created
) {
2158 /* Let's not leak a useless channel. */
2159 kernel_destroy_channel(kchan
);
2161 free(filter_expression_a
);
2165 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2166 ret
= event_kernel_enable_event(kchan
, event
,
2167 filter_expression_a
, filter_a
);
2168 /* We have passed ownership */
2169 filter_expression_a
= NULL
;
2171 if (ret
!= LTTNG_OK
) {
2176 case LTTNG_EVENT_PROBE
:
2177 case LTTNG_EVENT_USERSPACE_PROBE
:
2178 case LTTNG_EVENT_FUNCTION
:
2179 case LTTNG_EVENT_FUNCTION_ENTRY
:
2180 case LTTNG_EVENT_TRACEPOINT
:
2181 ret
= event_kernel_enable_event(kchan
, event
,
2182 filter_expression
, filter
);
2183 /* We have passed ownership */
2184 filter_expression
= NULL
;
2186 if (ret
!= LTTNG_OK
) {
2187 if (channel_created
) {
2188 /* Let's not leak a useless channel. */
2189 kernel_destroy_channel(kchan
);
2194 case LTTNG_EVENT_SYSCALL
:
2195 ret
= event_kernel_enable_event(kchan
, event
,
2196 filter_expression
, filter
);
2197 /* We have passed ownership */
2198 filter_expression
= NULL
;
2200 if (ret
!= LTTNG_OK
) {
2205 ret
= LTTNG_ERR_UNK
;
2209 kernel_wait_quiescent();
2212 case LTTNG_DOMAIN_UST
:
2214 struct ltt_ust_channel
*uchan
;
2215 struct ltt_ust_session
*usess
= session
->ust_session
;
2220 * If a non-default channel has been created in the
2221 * session, explicitely require that -c chan_name needs
2224 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2225 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2229 /* Get channel from global UST domain */
2230 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2232 if (uchan
== NULL
) {
2233 /* Create default channel */
2234 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2235 usess
->buffer_type
);
2237 ret
= LTTNG_ERR_FATAL
;
2240 if (lttng_strncpy(attr
->name
, channel_name
,
2241 sizeof(attr
->name
))) {
2242 ret
= LTTNG_ERR_INVALID
;
2246 ret
= cmd_enable_channel_internal(
2247 session
, domain
, attr
, wpipe
);
2248 if (ret
!= LTTNG_OK
) {
2252 /* Get the newly created channel reference back */
2253 uchan
= trace_ust_find_channel_by_name(
2254 usess
->domain_global
.channels
, channel_name
);
2258 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2260 * Don't allow users to add UST events to channels which
2261 * are assigned to a userspace subdomain (JUL, Log4J,
2264 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2268 if (!internal_event
) {
2270 * Ensure the event name is not reserved for internal
2273 ret
= validate_ust_event_name(event
->name
);
2275 WARN("Userspace event name %s failed validation.",
2277 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2282 /* At this point, the session and channel exist on the tracer */
2283 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2284 filter_expression
, filter
, exclusion
,
2286 /* We have passed ownership */
2287 filter_expression
= NULL
;
2290 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2291 goto already_enabled
;
2292 } else if (ret
!= LTTNG_OK
) {
2297 case LTTNG_DOMAIN_LOG4J
:
2298 case LTTNG_DOMAIN_JUL
:
2299 case LTTNG_DOMAIN_PYTHON
:
2301 const char *default_event_name
, *default_chan_name
;
2303 struct lttng_event uevent
;
2304 struct lttng_domain tmp_dom
;
2305 struct ltt_ust_session
*usess
= session
->ust_session
;
2309 if (!agent_tracing_is_enabled()) {
2310 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2311 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2315 agt
= trace_ust_find_agent(usess
, domain
->type
);
2317 agt
= agent_create(domain
->type
);
2319 ret
= LTTNG_ERR_NOMEM
;
2322 agent_add(agt
, usess
->agents
);
2325 /* Create the default tracepoint. */
2326 memset(&uevent
, 0, sizeof(uevent
));
2327 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2328 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2329 default_event_name
= event_get_default_agent_ust_name(
2331 if (!default_event_name
) {
2332 ret
= LTTNG_ERR_FATAL
;
2335 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2336 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2339 * The domain type is changed because we are about to enable the
2340 * default channel and event for the JUL domain that are hardcoded.
2341 * This happens in the UST domain.
2343 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2344 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2346 switch (domain
->type
) {
2347 case LTTNG_DOMAIN_LOG4J
:
2348 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2350 case LTTNG_DOMAIN_JUL
:
2351 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2353 case LTTNG_DOMAIN_PYTHON
:
2354 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2357 /* The switch/case we are in makes this impossible */
2362 char *filter_expression_copy
= NULL
;
2363 struct lttng_bytecode
*filter_copy
= NULL
;
2366 const size_t filter_size
= sizeof(
2367 struct lttng_bytecode
)
2370 filter_copy
= zmalloc(filter_size
);
2372 ret
= LTTNG_ERR_NOMEM
;
2375 memcpy(filter_copy
, filter
, filter_size
);
2377 filter_expression_copy
=
2378 strdup(filter_expression
);
2379 if (!filter_expression
) {
2380 ret
= LTTNG_ERR_NOMEM
;
2383 if (!filter_expression_copy
|| !filter_copy
) {
2384 free(filter_expression_copy
);
2390 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2391 (char *) default_chan_name
,
2392 &uevent
, filter_expression_copy
,
2393 filter_copy
, NULL
, wpipe
);
2396 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2397 goto already_enabled
;
2398 } else if (ret
!= LTTNG_OK
) {
2402 /* The wild card * means that everything should be enabled. */
2403 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2404 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2407 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2411 filter_expression
= NULL
;
2412 if (ret
!= LTTNG_OK
) {
2419 ret
= LTTNG_ERR_UND
;
2427 free(filter_expression
);
2430 channel_attr_destroy(attr
);
2436 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2437 * We own filter, exclusion, and filter_expression.
2439 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2440 struct lttng_event
*event
,
2441 char *filter_expression
,
2442 struct lttng_event_exclusion
*exclusion
,
2443 struct lttng_bytecode
*bytecode
,
2448 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2450 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2453 * The ownership of the following parameters is transferred to
2454 * _cmd_enable_event:
2456 * - filter_expression,
2460 ret
= _cmd_enable_event(cmd_ctx
->session
,
2462 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2463 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2464 filter_expression
= NULL
;
2471 * Enable an event which is internal to LTTng. An internal should
2472 * never be made visible to clients and are immune to checks such as
2475 static int cmd_enable_event_internal(struct ltt_session
*session
,
2476 const struct lttng_domain
*domain
,
2477 char *channel_name
, struct lttng_event
*event
,
2478 char *filter_expression
,
2479 struct lttng_bytecode
*filter
,
2480 struct lttng_event_exclusion
*exclusion
,
2483 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2484 filter_expression
, filter
, exclusion
, wpipe
, true);
2488 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2490 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2491 struct lttng_payload
*reply_payload
)
2493 enum lttng_error_code ret_code
;
2495 ssize_t i
, nb_events
= 0;
2496 struct lttng_event
*events
= NULL
;
2497 struct lttcomm_list_command_header reply_command_header
= {};
2498 size_t reply_command_header_offset
;
2500 assert(reply_payload
);
2502 /* Reserve space for command reply header. */
2503 reply_command_header_offset
= reply_payload
->buffer
.size
;
2504 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2505 reply_command_header_offset
+
2506 sizeof(struct lttcomm_list_command_header
));
2508 ret_code
= LTTNG_ERR_NOMEM
;
2513 case LTTNG_DOMAIN_KERNEL
:
2514 nb_events
= kernel_list_events(&events
);
2515 if (nb_events
< 0) {
2516 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2520 case LTTNG_DOMAIN_UST
:
2521 nb_events
= ust_app_list_events(&events
);
2522 if (nb_events
< 0) {
2523 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2527 case LTTNG_DOMAIN_LOG4J
:
2528 case LTTNG_DOMAIN_JUL
:
2529 case LTTNG_DOMAIN_PYTHON
:
2530 nb_events
= agent_list_events(&events
, domain
);
2531 if (nb_events
< 0) {
2532 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2537 ret_code
= LTTNG_ERR_UND
;
2541 for (i
= 0; i
< nb_events
; i
++) {
2542 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2545 ret_code
= LTTNG_ERR_NOMEM
;
2550 if (nb_events
> UINT32_MAX
) {
2551 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2552 ret_code
= LTTNG_ERR_OVERFLOW
;
2556 /* Update command reply header. */
2557 reply_command_header
.count
= (uint32_t) nb_events
;
2558 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2559 sizeof(reply_command_header
));
2561 ret_code
= LTTNG_OK
;
2568 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2570 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2571 struct lttng_event_field
**fields
)
2574 ssize_t nb_fields
= 0;
2577 case LTTNG_DOMAIN_UST
:
2578 nb_fields
= ust_app_list_event_fields(fields
);
2579 if (nb_fields
< 0) {
2580 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2584 case LTTNG_DOMAIN_KERNEL
:
2585 default: /* fall-through */
2586 ret
= LTTNG_ERR_UND
;
2593 /* Return negative value to differentiate return code */
2597 enum lttng_error_code
cmd_list_syscalls(
2598 struct lttng_payload
*reply_payload
)
2600 enum lttng_error_code ret_code
;
2601 ssize_t nb_events
, i
;
2603 struct lttng_event
*events
= NULL
;
2604 struct lttcomm_list_command_header reply_command_header
= {};
2605 size_t reply_command_header_offset
;
2607 assert(reply_payload
);
2609 /* Reserve space for command reply header. */
2610 reply_command_header_offset
= reply_payload
->buffer
.size
;
2611 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2612 reply_command_header_offset
+
2613 sizeof(struct lttcomm_list_command_header
));
2615 ret_code
= LTTNG_ERR_NOMEM
;
2619 nb_events
= syscall_table_list(&events
);
2620 if (nb_events
< 0) {
2621 ret_code
= (enum lttng_error_code
) -nb_events
;
2625 for (i
= 0; i
< nb_events
; i
++) {
2626 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2629 ret_code
= LTTNG_ERR_NOMEM
;
2634 if (nb_events
> UINT32_MAX
) {
2635 ERR("Syscall count would overflow the syscall listing command's reply");
2636 ret_code
= LTTNG_ERR_OVERFLOW
;
2640 /* Update command reply header. */
2641 reply_command_header
.count
= (uint32_t) nb_events
;
2642 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2643 sizeof(reply_command_header
));
2645 ret_code
= LTTNG_OK
;
2652 * Command LTTNG_START_TRACE processed by the client thread.
2654 * Called with session mutex held.
2656 int cmd_start_trace(struct ltt_session
*session
)
2658 enum lttng_error_code ret
;
2659 unsigned long nb_chan
= 0;
2660 struct ltt_kernel_session
*ksession
;
2661 struct ltt_ust_session
*usess
;
2662 const bool session_rotated_after_last_stop
=
2663 session
->rotated_after_last_stop
;
2664 const bool session_cleared_after_last_stop
=
2665 session
->cleared_after_last_stop
;
2669 /* Ease our life a bit ;) */
2670 ksession
= session
->kernel_session
;
2671 usess
= session
->ust_session
;
2673 /* Is the session already started? */
2674 if (session
->active
) {
2675 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2676 /* Perform nothing */
2680 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2681 !session
->current_trace_chunk
) {
2683 * A rotation was launched while the session was stopped and
2684 * it has not been completed yet. It is not possible to start
2685 * the session since starting the session here would require a
2686 * rotation from "NULL" to a new trace chunk. That rotation
2687 * would overlap with the ongoing rotation, which is not
2690 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2692 ret
= LTTNG_ERR_ROTATION_PENDING
;
2697 * Starting a session without channel is useless since after that it's not
2698 * possible to enable channel thus inform the client.
2700 if (usess
&& usess
->domain_global
.channels
) {
2701 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2704 nb_chan
+= ksession
->channel_count
;
2707 ret
= LTTNG_ERR_NO_CHANNEL
;
2711 session
->active
= 1;
2712 session
->rotated_after_last_stop
= false;
2713 session
->cleared_after_last_stop
= false;
2714 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2715 if (!session
->has_been_started
) {
2716 struct lttng_trace_chunk
*trace_chunk
;
2718 DBG("Creating initial trace chunk of session \"%s\"",
2720 trace_chunk
= session_create_new_trace_chunk(
2721 session
, NULL
, NULL
, NULL
);
2723 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2726 assert(!session
->current_trace_chunk
);
2727 ret
= session_set_trace_chunk(session
, trace_chunk
,
2729 lttng_trace_chunk_put(trace_chunk
);
2731 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2735 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2738 * Rotate existing streams into the new chunk.
2739 * This is a "quiet" rotation has no client has
2740 * explicitly requested this operation.
2742 * There is also no need to wait for the rotation
2743 * to complete as it will happen immediately. No data
2744 * was produced as the session was stopped, so the
2745 * rotation should happen on reception of the command.
2747 ret
= cmd_rotate_session(session
, NULL
, true,
2748 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2749 if (ret
!= LTTNG_OK
) {
2755 /* Kernel tracing */
2756 if (ksession
!= NULL
) {
2757 DBG("Start kernel tracing session %s", session
->name
);
2758 ret
= start_kernel_session(ksession
);
2759 if (ret
!= LTTNG_OK
) {
2764 /* Flag session that trace should start automatically */
2766 int int_ret
= ust_app_start_trace_all(usess
);
2769 ret
= LTTNG_ERR_UST_START_FAIL
;
2775 * Open a packet in every stream of the session to ensure that viewers
2776 * can correctly identify the boundaries of the periods during which
2777 * tracing was active for this session.
2779 ret
= session_open_packets(session
);
2780 if (ret
!= LTTNG_OK
) {
2785 * Clear the flag that indicates that a rotation was done while the
2786 * session was stopped.
2788 session
->rotated_after_last_stop
= false;
2790 if (session
->rotate_timer_period
) {
2791 int int_ret
= timer_session_rotation_schedule_timer_start(
2792 session
, session
->rotate_timer_period
);
2795 ERR("Failed to enable rotate timer");
2796 ret
= LTTNG_ERR_UNK
;
2804 if (ret
== LTTNG_OK
) {
2805 /* Flag this after a successful start. */
2806 session
->has_been_started
|= 1;
2808 session
->active
= 0;
2809 /* Restore initial state on error. */
2810 session
->rotated_after_last_stop
=
2811 session_rotated_after_last_stop
;
2812 session
->cleared_after_last_stop
=
2813 session_cleared_after_last_stop
;
2820 * Command LTTNG_STOP_TRACE processed by the client thread.
2822 int cmd_stop_trace(struct ltt_session
*session
)
2825 struct ltt_kernel_session
*ksession
;
2826 struct ltt_ust_session
*usess
;
2830 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2832 ksession
= session
->kernel_session
;
2833 usess
= session
->ust_session
;
2835 /* Session is not active. Skip everythong and inform the client. */
2836 if (!session
->active
) {
2837 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2841 ret
= stop_kernel_session(ksession
);
2842 if (ret
!= LTTNG_OK
) {
2846 if (usess
&& usess
->active
) {
2847 ret
= ust_app_stop_trace_all(usess
);
2849 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2854 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2856 /* Flag inactive after a successful stop. */
2857 session
->active
= 0;
2865 * Set the base_path of the session only if subdir of a control uris is set.
2866 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2868 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2870 struct lttng_uri
*uris
)
2875 for (i
= 0; i
< nb_uri
; i
++) {
2876 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2877 uris
[i
].subdir
[0] == '\0') {
2878 /* Not interested in these URIs */
2882 if (session
->base_path
!= NULL
) {
2883 free(session
->base_path
);
2884 session
->base_path
= NULL
;
2887 /* Set session base_path */
2888 session
->base_path
= strdup(uris
[i
].subdir
);
2889 if (!session
->base_path
) {
2890 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2891 uris
[i
].subdir
, session
->name
);
2892 ret
= LTTNG_ERR_NOMEM
;
2895 DBG2("Setting base path \"%s\" for session \"%s\"",
2896 session
->base_path
, session
->name
);
2904 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2906 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2907 struct lttng_uri
*uris
)
2910 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2911 struct ltt_ust_session
*usess
= session
->ust_session
;
2917 /* Can't set consumer URI if the session is active. */
2918 if (session
->active
) {
2919 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2924 * Set the session base path if any. This is done inside
2925 * cmd_set_consumer_uri to preserve backward compatibility of the
2926 * previous session creation api vs the session descriptor api.
2928 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2929 if (ret
!= LTTNG_OK
) {
2933 /* Set the "global" consumer URIs */
2934 for (i
= 0; i
< nb_uri
; i
++) {
2935 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2937 if (ret
!= LTTNG_OK
) {
2942 /* Set UST session URIs */
2943 if (session
->ust_session
) {
2944 for (i
= 0; i
< nb_uri
; i
++) {
2945 ret
= add_uri_to_consumer(session
,
2946 session
->ust_session
->consumer
,
2947 &uris
[i
], LTTNG_DOMAIN_UST
);
2948 if (ret
!= LTTNG_OK
) {
2954 /* Set kernel session URIs */
2955 if (session
->kernel_session
) {
2956 for (i
= 0; i
< nb_uri
; i
++) {
2957 ret
= add_uri_to_consumer(session
,
2958 session
->kernel_session
->consumer
,
2959 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2960 if (ret
!= LTTNG_OK
) {
2967 * Make sure to set the session in output mode after we set URI since a
2968 * session can be created without URL (thus flagged in no output mode).
2970 session
->output_traces
= 1;
2972 ksess
->output_traces
= 1;
2976 usess
->output_traces
= 1;
2987 enum lttng_error_code
set_session_output_from_descriptor(
2988 struct ltt_session
*session
,
2989 const struct lttng_session_descriptor
*descriptor
)
2992 enum lttng_error_code ret_code
= LTTNG_OK
;
2993 enum lttng_session_descriptor_type session_type
=
2994 lttng_session_descriptor_get_type(descriptor
);
2995 enum lttng_session_descriptor_output_type output_type
=
2996 lttng_session_descriptor_get_output_type(descriptor
);
2997 struct lttng_uri uris
[2] = {};
2998 size_t uri_count
= 0;
3000 switch (output_type
) {
3001 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3003 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3004 lttng_session_descriptor_get_local_output_uri(descriptor
,
3008 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3009 lttng_session_descriptor_get_network_output_uris(descriptor
,
3010 &uris
[0], &uris
[1]);
3014 ret_code
= LTTNG_ERR_INVALID
;
3018 switch (session_type
) {
3019 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3021 struct snapshot_output
*new_output
= NULL
;
3023 new_output
= snapshot_output_alloc();
3025 ret_code
= LTTNG_ERR_NOMEM
;
3029 ret
= snapshot_output_init_with_uri(session
,
3030 DEFAULT_SNAPSHOT_MAX_SIZE
,
3031 NULL
, uris
, uri_count
, session
->consumer
,
3032 new_output
, &session
->snapshot
);
3034 ret_code
= (ret
== -ENOMEM
) ?
3035 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3036 snapshot_output_destroy(new_output
);
3039 snapshot_add_output(&session
->snapshot
, new_output
);
3042 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3043 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3045 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
3049 ret_code
= LTTNG_ERR_INVALID
;
3057 enum lttng_error_code
cmd_create_session_from_descriptor(
3058 struct lttng_session_descriptor
*descriptor
,
3059 const lttng_sock_cred
*creds
,
3060 const char *home_path
)
3063 enum lttng_error_code ret_code
;
3064 const char *session_name
;
3065 struct ltt_session
*new_session
= NULL
;
3066 enum lttng_session_descriptor_status descriptor_status
;
3068 session_lock_list();
3070 if (*home_path
!= '/') {
3071 ERR("Home path provided by client is not absolute");
3072 ret_code
= LTTNG_ERR_INVALID
;
3077 descriptor_status
= lttng_session_descriptor_get_session_name(
3078 descriptor
, &session_name
);
3079 switch (descriptor_status
) {
3080 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3082 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3083 session_name
= NULL
;
3086 ret_code
= LTTNG_ERR_INVALID
;
3090 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3092 if (ret_code
!= LTTNG_OK
) {
3096 if (!session_name
) {
3097 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3100 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3105 if (!lttng_session_descriptor_is_output_destination_initialized(
3108 * Only include the session's creation time in the output
3109 * destination if the name of the session itself was
3110 * not auto-generated.
3112 ret_code
= lttng_session_descriptor_set_default_output(
3114 session_name
? &new_session
->creation_time
: NULL
,
3116 if (ret_code
!= LTTNG_OK
) {
3120 new_session
->has_user_specified_directory
=
3121 lttng_session_descriptor_has_output_directory(
3125 switch (lttng_session_descriptor_get_type(descriptor
)) {
3126 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3127 new_session
->snapshot_mode
= 1;
3129 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3130 new_session
->live_timer
=
3131 lttng_session_descriptor_live_get_timer_interval(
3138 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3139 if (ret_code
!= LTTNG_OK
) {
3142 new_session
->consumer
->enabled
= 1;
3143 ret_code
= LTTNG_OK
;
3145 /* Release reference provided by the session_create function. */
3146 session_put(new_session
);
3147 if (ret_code
!= LTTNG_OK
&& new_session
) {
3148 /* Release the global reference on error. */
3149 session_destroy(new_session
);
3151 session_unlock_list();
3155 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3156 struct lttng_session_descriptor
**return_descriptor
)
3159 size_t payload_size
;
3160 struct lttng_dynamic_buffer payload
;
3161 struct lttng_buffer_view home_dir_view
;
3162 struct lttng_buffer_view session_descriptor_view
;
3163 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3164 enum lttng_error_code ret_code
;
3166 lttng_dynamic_buffer_init(&payload
);
3167 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3169 ret_code
= LTTNG_ERR_INVALID
;
3172 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3173 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3174 ret_code
= LTTNG_ERR_INVALID
;
3178 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3179 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3180 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3182 ret_code
= LTTNG_ERR_NOMEM
;
3186 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3188 ERR("Reception of session descriptor failed, aborting.");
3189 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3193 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3196 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3197 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3198 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3199 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3200 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3204 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3206 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3207 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3208 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3209 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3210 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3214 ret
= lttng_session_descriptor_create_from_buffer(
3215 &session_descriptor_view
, &session_descriptor
);
3217 ERR("Failed to create session descriptor from payload of \"create session\" command");
3218 ret_code
= LTTNG_ERR_INVALID
;
3223 * Sets the descriptor's auto-generated properties (name, output) if
3226 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3228 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3229 if (ret_code
!= LTTNG_OK
) {
3233 ret_code
= LTTNG_OK
;
3234 *return_descriptor
= session_descriptor
;
3235 session_descriptor
= NULL
;
3237 lttng_dynamic_buffer_reset(&payload
);
3238 lttng_session_descriptor_destroy(session_descriptor
);
3243 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3244 void *_reply_context
)
3248 const struct cmd_destroy_session_reply_context
*reply_context
=
3250 struct lttng_dynamic_buffer payload
;
3251 struct lttcomm_session_destroy_command_header cmd_header
;
3252 struct lttng_trace_archive_location
*location
= NULL
;
3253 struct lttcomm_lttng_msg llm
= {
3254 .cmd_type
= LTTNG_DESTROY_SESSION
,
3255 .ret_code
= reply_context
->destruction_status
,
3258 sizeof(struct lttcomm_session_destroy_command_header
),
3261 size_t payload_size_before_location
;
3263 lttng_dynamic_buffer_init(&payload
);
3265 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3267 ERR("Failed to append session destruction message");
3271 cmd_header
.rotation_state
=
3272 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3273 session
->rotation_state
:
3274 LTTNG_ROTATION_STATE_NO_ROTATION
);
3275 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3276 sizeof(cmd_header
));
3278 ERR("Failed to append session destruction command header");
3282 if (!reply_context
->implicit_rotation_on_destroy
) {
3283 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3287 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3288 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3293 location
= session_get_trace_archive_location(session
);
3295 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3300 payload_size_before_location
= payload
.size
;
3301 comm_ret
= lttng_trace_archive_location_serialize(location
,
3303 lttng_trace_archive_location_put(location
);
3305 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3309 /* Update the message to indicate the location's length. */
3310 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3311 payload
.size
- payload_size_before_location
;
3313 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3314 payload
.data
, payload
.size
);
3315 if (comm_ret
!= (ssize_t
) payload
.size
) {
3316 ERR("Failed to send result of the destruction of session \"%s\" to client",
3320 ret
= close(reply_context
->reply_sock_fd
);
3322 PERROR("Failed to close client socket in deferred session destroy reply");
3324 lttng_dynamic_buffer_reset(&payload
);
3325 free(_reply_context
);
3329 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3331 * Called with session lock held.
3333 int cmd_destroy_session(struct ltt_session
*session
,
3334 struct notification_thread_handle
*notification_thread_handle
,
3338 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3339 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3342 reply_context
= zmalloc(sizeof(*reply_context
));
3343 if (!reply_context
) {
3344 ret
= LTTNG_ERR_NOMEM
;
3347 reply_context
->reply_sock_fd
= *sock_fd
;
3353 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3355 if (session
->active
) {
3356 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3358 ret
= cmd_stop_trace(session
);
3359 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3360 /* Carry on with the destruction of the session. */
3361 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3362 session
->name
, lttng_strerror(-ret
));
3363 destruction_last_error
= ret
;
3367 if (session
->rotation_schedule_timer_enabled
) {
3368 if (timer_session_rotation_schedule_timer_stop(
3370 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3372 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3376 if (session
->rotate_size
) {
3377 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3378 session
->rotate_size
= 0;
3381 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3383 * Perform a last rotation on destruction if rotations have
3384 * occurred during the session's lifetime.
3386 ret
= cmd_rotate_session(session
, NULL
, false,
3387 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3388 if (ret
!= LTTNG_OK
) {
3389 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3390 session
->name
, lttng_strerror(-ret
));
3391 destruction_last_error
= -ret
;
3393 if (reply_context
) {
3394 reply_context
->implicit_rotation_on_destroy
= true;
3396 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3398 * The user has not triggered a session rotation. However, to
3399 * ensure all data has been consumed, the session is rotated
3400 * to a 'null' trace chunk before it is destroyed.
3402 * This is a "quiet" rotation meaning that no notification is
3403 * emitted and no renaming of the current trace chunk takes
3406 ret
= cmd_rotate_session(session
, NULL
, true,
3407 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3409 * Rotation operations may not be supported by the kernel
3410 * tracer. Hence, do not consider this implicit rotation as
3411 * a session destruction error. The library has already stopped
3412 * the session and waited for pending data; there is nothing
3413 * left to do but complete the destruction of the session.
3415 if (ret
!= LTTNG_OK
&&
3416 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3417 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3418 session
->name
, lttng_strerror(ret
));
3419 destruction_last_error
= -ret
;
3423 if (session
->shm_path
[0]) {
3425 * When a session is created with an explicit shm_path,
3426 * the consumer daemon will create its shared memory files
3427 * at that location and will *not* unlink them. This is normal
3428 * as the intention of that feature is to make it possible
3429 * to retrieve the content of those files should a crash occur.
3431 * To ensure the content of those files can be used, the
3432 * sessiond daemon will replicate the content of the metadata
3433 * cache in a metadata file.
3435 * On clean-up, it is expected that the consumer daemon will
3436 * unlink the shared memory files and that the session daemon
3437 * will unlink the metadata file. Then, the session's directory
3438 * in the shm path can be removed.
3440 * Unfortunately, a flaw in the design of the sessiond's and
3441 * consumerd's tear down of channels makes it impossible to
3442 * determine when the sessiond _and_ the consumerd have both
3443 * destroyed their representation of a channel. For one, the
3444 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3445 * callbacks in both daemons.
3447 * However, it is also impossible for the sessiond to know when
3448 * the consumer daemon is done destroying its channel(s) since
3449 * it occurs as a reaction to the closing of the channel's file
3450 * descriptor. There is no resulting communication initiated
3451 * from the consumerd to the sessiond to confirm that the
3452 * operation is completed (and was successful).
3454 * Until this is all fixed, the session daemon checks for the
3455 * removal of the session's shm path which makes it possible
3456 * to safely advertise a session as having been destroyed.
3458 * Prior to this fix, it was not possible to reliably save
3459 * a session making use of the --shm-path option, destroy it,
3460 * and load it again. This is because the creation of the
3461 * session would fail upon seeing the session's shm path
3462 * already in existence.
3464 * Note that none of the error paths in the check for the
3465 * directory's existence return an error. This is normal
3466 * as there isn't much that can be done. The session will
3467 * be destroyed properly, except that we can't offer the
3468 * guarantee that the same session can be re-created.
3470 current_completion_handler
= &destroy_completion_handler
.handler
;
3471 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3473 sizeof(destroy_completion_handler
.shm_path
));
3478 * The session is destroyed. However, note that the command context
3479 * still holds a reference to the session, thus delaying its destruction
3480 * _at least_ up to the point when that reference is released.
3482 session_destroy(session
);
3483 if (reply_context
) {
3484 reply_context
->destruction_status
= destruction_last_error
;
3485 ret
= session_add_destroy_notifier(session
,
3486 cmd_destroy_session_reply
,
3487 (void *) reply_context
);
3489 ret
= LTTNG_ERR_FATAL
;
3501 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3503 int cmd_register_consumer(struct ltt_session
*session
,
3504 enum lttng_domain_type domain
, const char *sock_path
,
3505 struct consumer_data
*cdata
)
3508 struct consumer_socket
*socket
= NULL
;
3515 case LTTNG_DOMAIN_KERNEL
:
3517 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3521 /* Can't register a consumer if there is already one */
3522 if (ksess
->consumer_fds_sent
!= 0) {
3523 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3527 sock
= lttcomm_connect_unix_sock(sock_path
);
3529 ret
= LTTNG_ERR_CONNECT_FAIL
;
3532 cdata
->cmd_sock
= sock
;
3534 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3535 if (socket
== NULL
) {
3538 PERROR("close register consumer");
3540 cdata
->cmd_sock
= -1;
3541 ret
= LTTNG_ERR_FATAL
;
3545 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3546 if (socket
->lock
== NULL
) {
3547 PERROR("zmalloc pthread mutex");
3548 ret
= LTTNG_ERR_FATAL
;
3551 pthread_mutex_init(socket
->lock
, NULL
);
3552 socket
->registered
= 1;
3555 consumer_add_socket(socket
, ksess
->consumer
);
3558 pthread_mutex_lock(&cdata
->pid_mutex
);
3560 pthread_mutex_unlock(&cdata
->pid_mutex
);
3565 /* TODO: Userspace tracing */
3566 ret
= LTTNG_ERR_UND
;
3574 consumer_destroy_socket(socket
);
3580 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3582 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3583 struct lttng_domain
**domains
)
3588 struct lttng_ht_iter iter
;
3590 if (session
->kernel_session
!= NULL
) {
3591 DBG3("Listing domains found kernel domain");
3595 if (session
->ust_session
!= NULL
) {
3596 DBG3("Listing domains found UST global domain");
3600 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3602 if (agt
->being_used
) {
3613 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3614 if (*domains
== NULL
) {
3615 ret
= LTTNG_ERR_FATAL
;
3619 if (session
->kernel_session
!= NULL
) {
3620 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3622 /* Kernel session buffer type is always GLOBAL */
3623 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3628 if (session
->ust_session
!= NULL
) {
3629 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3630 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3634 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3636 if (agt
->being_used
) {
3637 (*domains
)[index
].type
= agt
->domain
;
3638 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3648 /* Return negative value to differentiate return code */
3654 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3656 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3657 struct ltt_session
*session
,
3658 struct lttng_payload
*payload
)
3662 struct lttcomm_list_command_header cmd_header
= {};
3663 size_t cmd_header_offset
;
3664 enum lttng_error_code ret_code
;
3669 DBG("Listing channels for session %s", session
->name
);
3671 cmd_header_offset
= payload
->buffer
.size
;
3673 /* Reserve space for command reply header. */
3674 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3675 cmd_header_offset
+ sizeof(cmd_header
));
3677 ret_code
= LTTNG_ERR_NOMEM
;
3682 case LTTNG_DOMAIN_KERNEL
:
3684 /* Kernel channels */
3685 struct ltt_kernel_channel
*kchan
;
3686 if (session
->kernel_session
!= NULL
) {
3687 cds_list_for_each_entry(kchan
,
3688 &session
->kernel_session
->channel_list
.head
, list
) {
3689 uint64_t discarded_events
, lost_packets
;
3690 struct lttng_channel_extended
*extended
;
3692 extended
= (struct lttng_channel_extended
*)
3693 kchan
->channel
->attr
.extended
.ptr
;
3695 ret
= get_kernel_runtime_stats(session
, kchan
,
3696 &discarded_events
, &lost_packets
);
3698 ret_code
= LTTNG_ERR_UNK
;
3703 * Update the discarded_events and lost_packets
3704 * count for the channel
3706 extended
->discarded_events
= discarded_events
;
3707 extended
->lost_packets
= lost_packets
;
3709 ret
= lttng_channel_serialize(
3710 kchan
->channel
, &payload
->buffer
);
3712 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3713 kchan
->channel
->name
);
3714 ret_code
= LTTNG_ERR_UNK
;
3723 case LTTNG_DOMAIN_UST
:
3725 struct lttng_ht_iter iter
;
3726 struct ltt_ust_channel
*uchan
;
3729 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3730 &iter
.iter
, uchan
, node
.node
) {
3731 uint64_t discarded_events
= 0, lost_packets
= 0;
3732 struct lttng_channel
*channel
= NULL
;
3733 struct lttng_channel_extended
*extended
;
3735 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3737 ret
= LTTNG_ERR_NOMEM
;
3741 extended
= (struct lttng_channel_extended
*)
3742 channel
->attr
.extended
.ptr
;
3744 ret
= get_ust_runtime_stats(session
, uchan
,
3745 &discarded_events
, &lost_packets
);
3747 lttng_channel_destroy(channel
);
3748 ret_code
= LTTNG_ERR_UNK
;
3752 extended
->discarded_events
= discarded_events
;
3753 extended
->lost_packets
= lost_packets
;
3755 ret
= lttng_channel_serialize(
3756 channel
, &payload
->buffer
);
3758 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3760 ret_code
= LTTNG_ERR_UNK
;
3774 if (i
> UINT32_MAX
) {
3775 ERR("Channel count would overflow the channel listing command's reply");
3776 ret_code
= LTTNG_ERR_OVERFLOW
;
3780 /* Update command reply header. */
3781 cmd_header
.count
= (uint32_t) i
;
3782 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3783 sizeof(cmd_header
));
3784 ret_code
= LTTNG_OK
;
3791 * Command LTTNG_LIST_EVENTS processed by the client thread.
3793 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3794 struct ltt_session
*session
,
3796 struct lttng_payload
*reply_payload
)
3798 int buffer_resize_ret
;
3799 enum lttng_error_code ret_code
= LTTNG_OK
;
3800 struct lttcomm_list_command_header reply_command_header
= {};
3801 size_t reply_command_header_offset
;
3802 unsigned int nb_events
= 0;
3804 assert(reply_payload
);
3806 /* Reserve space for command reply header. */
3807 reply_command_header_offset
= reply_payload
->buffer
.size
;
3808 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3809 reply_command_header_offset
+
3810 sizeof(struct lttcomm_list_command_header
));
3811 if (buffer_resize_ret
) {
3812 ret_code
= LTTNG_ERR_NOMEM
;
3817 case LTTNG_DOMAIN_KERNEL
:
3818 if (session
->kernel_session
!= NULL
) {
3819 ret_code
= list_lttng_kernel_events(channel_name
,
3820 session
->kernel_session
, reply_payload
, &nb_events
);
3824 case LTTNG_DOMAIN_UST
:
3826 if (session
->ust_session
!= NULL
) {
3827 ret_code
= list_lttng_ust_global_events(channel_name
,
3828 &session
->ust_session
->domain_global
,
3829 reply_payload
, &nb_events
);
3834 case LTTNG_DOMAIN_LOG4J
:
3835 case LTTNG_DOMAIN_JUL
:
3836 case LTTNG_DOMAIN_PYTHON
:
3837 if (session
->ust_session
) {
3838 struct lttng_ht_iter iter
;
3842 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3843 &iter
.iter
, agt
, node
.node
) {
3844 if (agt
->domain
== domain
) {
3845 ret_code
= list_lttng_agent_events(
3846 agt
, reply_payload
, &nb_events
);
3855 ret_code
= LTTNG_ERR_UND
;
3859 if (nb_events
> UINT32_MAX
) {
3860 ret_code
= LTTNG_ERR_OVERFLOW
;
3864 /* Update command reply header. */
3865 reply_command_header
.count
= (uint32_t) nb_events
;
3866 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
3867 sizeof(reply_command_header
));
3874 * Using the session list, filled a lttng_session array to send back to the
3875 * client for session listing.
3877 * The session list lock MUST be acquired before calling this function. Use
3878 * session_lock_list() and session_unlock_list().
3880 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3881 size_t session_count
, uid_t uid
, gid_t gid
)
3885 struct ltt_session
*session
;
3886 struct ltt_session_list
*list
= session_get_list();
3887 struct lttng_session_extended
*extended
=
3888 (typeof(extended
)) (&sessions
[session_count
]);
3890 DBG("Getting all available session for UID %d GID %d",
3893 * Iterate over session list and append data after the control struct in
3896 cds_list_for_each_entry(session
, &list
->head
, list
) {
3897 if (!session_get(session
)) {
3901 * Only list the sessions the user can control.
3903 if (!session_access_ok(session
, uid
) ||
3904 session
->destroyed
) {
3905 session_put(session
);
3909 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3910 struct ltt_ust_session
*usess
= session
->ust_session
;
3912 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3913 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3914 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3915 ret
= build_network_session_path(sessions
[i
].path
,
3916 sizeof(sessions
[i
].path
), session
);
3918 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3919 session
->consumer
->dst
.session_root_path
);
3922 PERROR("snprintf session path");
3923 session_put(session
);
3927 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3928 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3929 sessions
[i
].enabled
= session
->active
;
3930 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3931 sessions
[i
].live_timer_interval
= session
->live_timer
;
3932 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3933 extended
[i
].creation_time
.is_set
= 1;
3935 session_put(session
);
3940 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3941 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3943 int cmd_data_pending(struct ltt_session
*session
)
3946 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3947 struct ltt_ust_session
*usess
= session
->ust_session
;
3951 DBG("Data pending for session %s", session
->name
);
3953 /* Session MUST be stopped to ask for data availability. */
3954 if (session
->active
) {
3955 ret
= LTTNG_ERR_SESSION_STARTED
;
3959 * If stopped, just make sure we've started before else the above call
3960 * will always send that there is data pending.
3962 * The consumer assumes that when the data pending command is received,
3963 * the trace has been started before or else no output data is written
3964 * by the streams which is a condition for data pending. So, this is
3965 * *VERY* important that we don't ask the consumer before a start
3968 if (!session
->has_been_started
) {
3974 /* A rotation is still pending, we have to wait. */
3975 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3976 DBG("Rotate still pending for session %s", session
->name
);
3981 if (ksess
&& ksess
->consumer
) {
3982 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3984 /* Data is still being extracted for the kernel. */
3989 if (usess
&& usess
->consumer
) {
3990 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3992 /* Data is still being extracted for the kernel. */
3997 /* Data is ready to be read by a viewer */
4005 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4007 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4009 int cmd_snapshot_add_output(struct ltt_session
*session
,
4010 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4013 struct snapshot_output
*new_output
;
4018 DBG("Cmd snapshot add output for session %s", session
->name
);
4021 * Can't create an output if the session is not set in no-output mode.
4023 if (session
->output_traces
) {
4024 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4028 if (session
->has_non_mmap_channel
) {
4029 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4033 /* Only one output is allowed until we have the "tee" feature. */
4034 if (session
->snapshot
.nb_output
== 1) {
4035 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4039 new_output
= snapshot_output_alloc();
4041 ret
= LTTNG_ERR_NOMEM
;
4045 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4046 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4047 &session
->snapshot
);
4049 if (ret
== -ENOMEM
) {
4050 ret
= LTTNG_ERR_NOMEM
;
4052 ret
= LTTNG_ERR_INVALID
;
4058 snapshot_add_output(&session
->snapshot
, new_output
);
4060 *id
= new_output
->id
;
4067 snapshot_output_destroy(new_output
);
4073 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4075 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4077 int cmd_snapshot_del_output(struct ltt_session
*session
,
4078 const struct lttng_snapshot_output
*output
)
4081 struct snapshot_output
*sout
= NULL
;
4089 * Permission denied to create an output if the session is not
4090 * set in no output mode.
4092 if (session
->output_traces
) {
4093 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4098 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4100 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4101 } else if (*output
->name
!= '\0') {
4102 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4104 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4107 ret
= LTTNG_ERR_INVALID
;
4111 snapshot_delete_output(&session
->snapshot
, sout
);
4112 snapshot_output_destroy(sout
);
4121 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4123 * If no output is available, outputs is untouched and 0 is returned.
4125 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4127 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4128 struct lttng_snapshot_output
**outputs
)
4131 struct lttng_snapshot_output
*list
= NULL
;
4132 struct lttng_ht_iter iter
;
4133 struct snapshot_output
*output
;
4138 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4141 * Permission denied to create an output if the session is not
4142 * set in no output mode.
4144 if (session
->output_traces
) {
4145 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4149 if (session
->snapshot
.nb_output
== 0) {
4154 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4156 ret
= -LTTNG_ERR_NOMEM
;
4160 /* Copy list from session to the new list object. */
4162 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4163 output
, node
.node
) {
4164 assert(output
->consumer
);
4165 list
[idx
].id
= output
->id
;
4166 list
[idx
].max_size
= output
->max_size
;
4167 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4168 sizeof(list
[idx
].name
))) {
4169 ret
= -LTTNG_ERR_INVALID
;
4172 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4173 if (lttng_strncpy(list
[idx
].ctrl_url
,
4174 output
->consumer
->dst
.session_root_path
,
4175 sizeof(list
[idx
].ctrl_url
))) {
4176 ret
= -LTTNG_ERR_INVALID
;
4181 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4182 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4184 ret
= -LTTNG_ERR_NOMEM
;
4189 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4190 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4192 ret
= -LTTNG_ERR_NOMEM
;
4201 ret
= session
->snapshot
.nb_output
;
4210 * Check if we can regenerate the metadata for this session.
4211 * Only kernel, UST per-uid and non-live sessions are supported.
4213 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4216 int check_regenerate_metadata_support(struct ltt_session
*session
)
4222 if (session
->live_timer
!= 0) {
4223 ret
= LTTNG_ERR_LIVE_SESSION
;
4226 if (!session
->active
) {
4227 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4230 if (session
->ust_session
) {
4231 switch (session
->ust_session
->buffer_type
) {
4232 case LTTNG_BUFFER_PER_UID
:
4234 case LTTNG_BUFFER_PER_PID
:
4235 ret
= LTTNG_ERR_PER_PID_SESSION
;
4239 ret
= LTTNG_ERR_UNK
;
4243 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4244 session
->consumer
->relay_minor_version
< 8) {
4245 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4255 int clear_metadata_file(int fd
)
4260 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4261 if (lseek_ret
< 0) {
4267 ret
= ftruncate(fd
, 0);
4269 PERROR("ftruncate");
4278 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4281 struct buffer_reg_uid
*uid_reg
= NULL
;
4282 struct buffer_reg_session
*session_reg
= NULL
;
4285 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4286 struct ust_registry_session
*registry
;
4287 struct ust_registry_channel
*chan
;
4288 struct lttng_ht_iter iter_chan
;
4290 session_reg
= uid_reg
->registry
;
4291 registry
= session_reg
->reg
.ust
;
4293 pthread_mutex_lock(®istry
->lock
);
4294 registry
->metadata_len_sent
= 0;
4295 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4296 registry
->metadata_len
= 0;
4297 registry
->metadata_version
++;
4298 if (registry
->metadata_fd
> 0) {
4299 /* Clear the metadata file's content. */
4300 ret
= clear_metadata_file(registry
->metadata_fd
);
4302 pthread_mutex_unlock(®istry
->lock
);
4307 ret
= ust_metadata_session_statedump(registry
, NULL
,
4308 registry
->major
, registry
->minor
);
4310 pthread_mutex_unlock(®istry
->lock
);
4311 ERR("Failed to generate session metadata (err = %d)",
4315 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4317 struct ust_registry_event
*event
;
4318 struct lttng_ht_iter iter_event
;
4320 ret
= ust_metadata_channel_statedump(registry
, chan
);
4322 pthread_mutex_unlock(®istry
->lock
);
4323 ERR("Failed to generate channel metadata "
4327 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4329 ret
= ust_metadata_event_statedump(registry
,
4332 pthread_mutex_unlock(®istry
->lock
);
4333 ERR("Failed to generate event metadata "
4339 pthread_mutex_unlock(®istry
->lock
);
4348 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4350 * Ask the consumer to truncate the existing metadata file(s) and
4351 * then regenerate the metadata. Live and per-pid sessions are not
4352 * supported and return an error.
4354 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4356 int cmd_regenerate_metadata(struct ltt_session
*session
)
4362 ret
= check_regenerate_metadata_support(session
);
4367 if (session
->kernel_session
) {
4368 ret
= kernctl_session_regenerate_metadata(
4369 session
->kernel_session
->fd
);
4371 ERR("Failed to regenerate the kernel metadata");
4376 if (session
->ust_session
) {
4377 ret
= ust_regenerate_metadata(session
->ust_session
);
4379 ERR("Failed to regenerate the UST metadata");
4383 DBG("Cmd metadata regenerate for session %s", session
->name
);
4391 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4393 * Ask the tracer to regenerate a new statedump.
4395 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4397 int cmd_regenerate_statedump(struct ltt_session
*session
)
4403 if (!session
->active
) {
4404 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4408 if (session
->kernel_session
) {
4409 ret
= kernctl_session_regenerate_statedump(
4410 session
->kernel_session
->fd
);
4412 * Currently, the statedump in kernel can only fail if out
4416 if (ret
== -ENOMEM
) {
4417 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4419 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4421 ERR("Failed to regenerate the kernel statedump");
4426 if (session
->ust_session
) {
4427 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4429 * Currently, the statedump in UST always returns 0.
4432 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4433 ERR("Failed to regenerate the UST statedump");
4437 DBG("Cmd regenerate statedump for session %s", session
->name
);
4445 enum lttng_error_code
synchronize_tracer_notifier_register(
4446 struct notification_thread_handle
*notification_thread
,
4447 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4449 enum lttng_error_code ret_code
;
4450 const struct lttng_condition
*condition
=
4451 lttng_trigger_get_const_condition(trigger
);
4452 const char *trigger_name
;
4453 uid_t trigger_owner
;
4454 enum lttng_trigger_status trigger_status
;
4455 const enum lttng_domain_type trigger_domain
=
4456 lttng_trigger_get_underlying_domain_type_restriction(
4459 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4460 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4463 assert(lttng_condition_get_type(condition
) ==
4464 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4466 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4467 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4468 trigger_name
: "(anonymous)";
4470 session_lock_list();
4471 switch (trigger_domain
) {
4472 case LTTNG_DOMAIN_KERNEL
:
4474 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4475 if (ret_code
!= LTTNG_OK
) {
4476 enum lttng_error_code notif_thread_unregister_ret
;
4478 notif_thread_unregister_ret
=
4479 notification_thread_command_unregister_trigger(
4480 notification_thread
, trigger
);
4482 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4483 /* Return the original error code. */
4484 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4486 (int) trigger_owner
,
4492 case LTTNG_DOMAIN_UST
:
4493 ust_app_global_update_all_event_notifier_rules();
4495 case LTTNG_DOMAIN_JUL
:
4496 case LTTNG_DOMAIN_LOG4J
:
4497 case LTTNG_DOMAIN_PYTHON
:
4499 /* Agent domains. */
4500 struct agent
*agt
= agent_find_by_event_notifier_domain(
4504 agt
= agent_create(trigger_domain
);
4506 ret_code
= LTTNG_ERR_NOMEM
;
4507 goto end_unlock_session_list
;
4510 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4513 ret_code
= trigger_agent_enable(trigger
, agt
);
4514 if (ret_code
!= LTTNG_OK
) {
4515 goto end_unlock_session_list
;
4520 case LTTNG_DOMAIN_NONE
:
4525 ret_code
= LTTNG_OK
;
4526 end_unlock_session_list
:
4527 session_unlock_list();
4531 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4532 struct lttng_trigger
*trigger
,
4533 bool is_trigger_anonymous
,
4534 struct notification_thread_handle
*notification_thread
,
4535 struct lttng_trigger
**return_trigger
)
4537 enum lttng_error_code ret_code
;
4538 const char *trigger_name
;
4539 uid_t trigger_owner
;
4540 enum lttng_trigger_status trigger_status
;
4542 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4543 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4544 trigger_name
: "(anonymous)";
4546 trigger_status
= lttng_trigger_get_owner_uid(
4547 trigger
, &trigger_owner
);
4548 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4550 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4551 trigger_name
, (int) trigger_owner
,
4552 (int) lttng_credentials_get_uid(cmd_creds
));
4555 * Validate the trigger credentials against the command credentials.
4556 * Only the root user can register a trigger with non-matching
4559 if (!lttng_credentials_is_equal_uid(
4560 lttng_trigger_get_credentials(trigger
),
4562 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4563 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4564 trigger_name
, (int) trigger_owner
,
4565 (int) lttng_credentials_get_uid(cmd_creds
));
4566 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4572 * The bytecode generation also serves as a validation step for the
4573 * bytecode expressions.
4575 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4576 if (ret_code
!= LTTNG_OK
) {
4577 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4578 trigger_name
, (int) trigger_owner
, ret_code
);
4583 * A reference to the trigger is acquired by the notification thread.
4584 * It is safe to return the same trigger to the caller since it the
4585 * other user holds a reference.
4587 * The trigger is modified during the execution of the
4588 * "register trigger" command. However, by the time the command returns,
4589 * it is safe to use without any locking as its properties are
4592 ret_code
= notification_thread_command_register_trigger(
4593 notification_thread
, trigger
, is_trigger_anonymous
);
4594 if (ret_code
!= LTTNG_OK
) {
4595 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4596 trigger_name
, (int) trigger_owner
, ret_code
);
4600 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4601 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4602 trigger_name
: "(anonymous)";
4605 * Synchronize tracers if the trigger adds an event notifier.
4607 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4608 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4609 trigger
, cmd_creds
);
4610 if (ret_code
!= LTTNG_OK
) {
4611 ERR("Error registering tracer notifier: %s",
4612 lttng_strerror(-ret_code
));
4618 * Return an updated trigger to the client.
4620 * Since a modified version of the same trigger is returned, acquire a
4621 * reference to the trigger so the caller doesn't have to care if those
4622 * are distinct instances or not.
4624 if (ret_code
== LTTNG_OK
) {
4625 lttng_trigger_get(trigger
);
4626 *return_trigger
= trigger
;
4627 /* Ownership of trigger was transferred to caller. */
4635 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4636 const struct lttng_trigger
*trigger
)
4638 enum lttng_error_code ret_code
;
4639 const struct lttng_condition
*condition
=
4640 lttng_trigger_get_const_condition(trigger
);
4641 const enum lttng_domain_type trigger_domain
=
4642 lttng_trigger_get_underlying_domain_type_restriction(
4646 assert(lttng_condition_get_type(condition
) ==
4647 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4649 session_lock_list();
4650 switch (trigger_domain
) {
4651 case LTTNG_DOMAIN_KERNEL
:
4652 ret_code
= kernel_unregister_event_notifier(trigger
);
4653 if (ret_code
!= LTTNG_OK
) {
4654 goto end_unlock_session_list
;
4658 case LTTNG_DOMAIN_UST
:
4659 ust_app_global_update_all_event_notifier_rules();
4661 case LTTNG_DOMAIN_JUL
:
4662 case LTTNG_DOMAIN_LOG4J
:
4663 case LTTNG_DOMAIN_PYTHON
:
4665 /* Agent domains. */
4666 struct agent
*agt
= agent_find_by_event_notifier_domain(
4670 * This trigger was never registered in the first place. Calling
4671 * this function under those circumstances is an internal error.
4674 ret_code
= trigger_agent_disable(trigger
, agt
);
4675 if (ret_code
!= LTTNG_OK
) {
4676 goto end_unlock_session_list
;
4681 case LTTNG_DOMAIN_NONE
:
4686 ret_code
= LTTNG_OK
;
4688 end_unlock_session_list
:
4689 session_unlock_list();
4693 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4694 const struct lttng_trigger
*trigger
,
4695 struct notification_thread_handle
*notification_thread
)
4697 enum lttng_error_code ret_code
;
4698 const char *trigger_name
;
4699 uid_t trigger_owner
;
4700 enum lttng_trigger_status trigger_status
;
4701 struct lttng_trigger
*sessiond_trigger
= NULL
;
4703 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4704 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4705 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4706 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4708 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4709 trigger_name
, (int) trigger_owner
,
4710 (int) lttng_credentials_get_uid(cmd_creds
));
4713 * Validate the trigger credentials against the command credentials.
4714 * Only the root user can unregister a trigger with non-matching
4717 if (!lttng_credentials_is_equal_uid(
4718 lttng_trigger_get_credentials(trigger
),
4720 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4721 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4722 trigger_name
, (int) trigger_owner
,
4723 (int) lttng_credentials_get_uid(cmd_creds
));
4724 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4729 /* Fetch the sessiond side trigger object. */
4730 ret_code
= notification_thread_command_get_trigger(
4731 notification_thread
, trigger
, &sessiond_trigger
);
4732 if (ret_code
!= LTTNG_OK
) {
4733 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4734 trigger_name
, (int) trigger_owner
, ret_code
);
4738 assert(sessiond_trigger
);
4741 * From this point on, no matter what, consider the trigger
4744 * We set the unregistered state of the sessiond side trigger object in
4745 * the client thread since we want to minimize the possibility of the
4746 * notification thread being stalled due to a long execution of an
4747 * action that required the trigger lock.
4749 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4751 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4753 if (ret_code
!= LTTNG_OK
) {
4754 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4755 trigger_name
, (int) trigger_owner
, ret_code
);
4760 * Synchronize tracers if the trigger removes an event notifier.
4761 * Do this even if the trigger unregistration failed to at least stop
4762 * the tracers from producing notifications associated with this
4765 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4766 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4767 if (ret_code
!= LTTNG_OK
) {
4768 ERR("Error unregistering trigger to tracer.");
4775 lttng_trigger_put(sessiond_trigger
);
4779 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4780 struct notification_thread_handle
*notification_thread
,
4781 struct lttng_triggers
**return_triggers
)
4784 enum lttng_error_code ret_code
;
4785 struct lttng_triggers
*triggers
= NULL
;
4787 /* Get the set of triggers from the notification thread. */
4788 ret_code
= notification_thread_command_list_triggers(
4789 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4790 if (ret_code
!= LTTNG_OK
) {
4794 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4796 ret_code
= LTTNG_ERR_UNK
;
4800 *return_triggers
= triggers
;
4802 ret_code
= LTTNG_OK
;
4804 lttng_triggers_destroy(triggers
);
4808 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4809 const struct lttng_error_query
*query
,
4810 struct lttng_error_query_results
**_results
,
4811 struct notification_thread_handle
*notification_thread
)
4813 enum lttng_error_code ret_code
;
4814 const struct lttng_trigger
*query_target_trigger
;
4815 const struct lttng_action
*query_target_action
= NULL
;
4816 struct lttng_trigger
*matching_trigger
= NULL
;
4817 const char *trigger_name
;
4818 uid_t trigger_owner
;
4819 enum lttng_trigger_status trigger_status
;
4820 struct lttng_error_query_results
*results
= NULL
;
4822 switch (lttng_error_query_get_target_type(query
)) {
4823 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4824 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4826 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4827 query_target_trigger
=
4828 lttng_error_query_condition_borrow_target(query
);
4830 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4831 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4838 assert(query_target_trigger
);
4840 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4841 query_target_trigger
, &matching_trigger
);
4842 if (ret_code
!= LTTNG_OK
) {
4846 /* No longer needed. */
4847 query_target_trigger
= NULL
;
4849 if (lttng_error_query_get_target_type(query
) ==
4850 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4851 /* Get the sessiond-side version of the target action. */
4852 query_target_action
=
4853 lttng_error_query_action_borrow_action_target(
4854 query
, matching_trigger
);
4857 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4858 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4859 trigger_name
: "(anonymous)";
4860 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4862 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4864 results
= lttng_error_query_results_create();
4866 ret_code
= LTTNG_ERR_NOMEM
;
4870 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4871 trigger_name
, (int) trigger_owner
,
4872 (int) lttng_credentials_get_uid(cmd_creds
));
4875 * Validate the trigger credentials against the command credentials.
4876 * Only the root user can target a trigger with non-matching
4879 if (!lttng_credentials_is_equal_uid(
4880 lttng_trigger_get_credentials(matching_trigger
),
4882 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4883 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4884 trigger_name
, (int) trigger_owner
,
4885 (int) lttng_credentials_get_uid(cmd_creds
));
4886 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4891 switch (lttng_error_query_get_target_type(query
)) {
4892 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4893 trigger_status
= lttng_trigger_add_error_results(
4894 matching_trigger
, results
);
4896 switch (trigger_status
) {
4897 case LTTNG_TRIGGER_STATUS_OK
:
4900 ret_code
= LTTNG_ERR_UNK
;
4905 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4907 trigger_status
= lttng_trigger_condition_add_error_results(
4908 matching_trigger
, results
);
4910 switch (trigger_status
) {
4911 case LTTNG_TRIGGER_STATUS_OK
:
4914 ret_code
= LTTNG_ERR_UNK
;
4920 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4922 const enum lttng_action_status action_status
=
4923 lttng_action_add_error_query_results(
4924 query_target_action
, results
);
4926 switch (action_status
) {
4927 case LTTNG_ACTION_STATUS_OK
:
4930 ret_code
= LTTNG_ERR_UNK
;
4941 *_results
= results
;
4943 ret_code
= LTTNG_OK
;
4945 lttng_trigger_put(matching_trigger
);
4946 lttng_error_query_results_destroy(results
);
4951 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4952 * snapshot output is *not* set with a remote destination.
4954 * Return LTTNG_OK on success or a LTTNG_ERR code.
4956 static enum lttng_error_code
set_relayd_for_snapshot(
4957 struct consumer_output
*output
,
4958 const struct ltt_session
*session
)
4960 enum lttng_error_code status
= LTTNG_OK
;
4961 struct lttng_ht_iter iter
;
4962 struct consumer_socket
*socket
;
4963 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4964 const char *base_path
;
4969 DBG2("Set relayd object from snapshot output");
4971 if (session
->current_trace_chunk
) {
4972 enum lttng_trace_chunk_status chunk_status
=
4973 lttng_trace_chunk_get_id(
4974 session
->current_trace_chunk
,
4975 ¤t_chunk_id
.value
);
4977 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4978 current_chunk_id
.is_set
= true;
4980 ERR("Failed to get current trace chunk id");
4981 status
= LTTNG_ERR_UNK
;
4986 /* Ignore if snapshot consumer output is not network. */
4987 if (output
->type
!= CONSUMER_DST_NET
) {
4992 * The snapshot record URI base path overrides the session
4995 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4996 base_path
= output
->dst
.net
.control
.subdir
;
4998 base_path
= session
->base_path
;
5002 * For each consumer socket, create and send the relayd object of the
5006 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
5007 socket
, node
.node
) {
5008 pthread_mutex_lock(socket
->lock
);
5009 status
= send_consumer_relayd_sockets(0, session
->id
,
5011 session
->name
, session
->hostname
,
5013 session
->live_timer
,
5014 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
5015 session
->creation_time
,
5016 session
->name_contains_creation_time
);
5017 pthread_mutex_unlock(socket
->lock
);
5018 if (status
!= LTTNG_OK
) {
5030 * Record a kernel snapshot.
5032 * Return LTTNG_OK on success or a LTTNG_ERR code.
5034 static enum lttng_error_code
record_kernel_snapshot(
5035 struct ltt_kernel_session
*ksess
,
5036 const struct consumer_output
*output
,
5037 const struct ltt_session
*session
,
5038 int wait
, uint64_t nb_packets_per_stream
)
5040 enum lttng_error_code status
;
5046 status
= kernel_snapshot_record(
5047 ksess
, output
, wait
, nb_packets_per_stream
);
5052 * Record a UST snapshot.
5054 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5056 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5057 const struct consumer_output
*output
,
5058 const struct ltt_session
*session
,
5059 int wait
, uint64_t nb_packets_per_stream
)
5061 enum lttng_error_code status
;
5067 status
= ust_app_snapshot_record(
5068 usess
, output
, wait
, nb_packets_per_stream
);
5073 uint64_t get_session_size_one_more_packet_per_stream(
5074 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5076 uint64_t tot_size
= 0;
5078 if (session
->kernel_session
) {
5079 struct ltt_kernel_channel
*chan
;
5080 const struct ltt_kernel_session
*ksess
=
5081 session
->kernel_session
;
5083 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5084 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5086 * Don't take channel into account if we
5087 * already grab all its packets.
5091 tot_size
+= chan
->channel
->attr
.subbuf_size
5092 * chan
->stream_count
;
5096 if (session
->ust_session
) {
5097 const struct ltt_ust_session
*usess
= session
->ust_session
;
5099 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5107 * Calculate the number of packets we can grab from each stream that
5108 * fits within the overall snapshot max size.
5110 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5111 * the number of packets per stream.
5113 * TODO: this approach is not perfect: we consider the worse case
5114 * (packet filling the sub-buffers) as an upper bound, but we could do
5115 * better if we do this calculation while we actually grab the packet
5116 * content: we would know how much padding we don't actually store into
5119 * This algorithm is currently bounded by the number of packets per
5122 * Since we call this algorithm before actually grabbing the data, it's
5123 * an approximation: for instance, applications could appear/disappear
5124 * in between this call and actually grabbing data.
5127 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5131 uint64_t cur_nb_packets
= 0;
5134 return 0; /* Infinite */
5137 size_left
= max_size
;
5139 uint64_t one_more_packet_tot_size
;
5141 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5142 session
, cur_nb_packets
);
5143 if (!one_more_packet_tot_size
) {
5144 /* We are already grabbing all packets. */
5147 size_left
-= one_more_packet_tot_size
;
5148 if (size_left
< 0) {
5153 if (!cur_nb_packets
&& size_left
!= max_size
) {
5154 /* Not enough room to grab one packet of each stream, error. */
5157 return cur_nb_packets
;
5161 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5162 const struct snapshot_output
*snapshot_output
, int wait
)
5164 int64_t nb_packets_per_stream
;
5165 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5167 enum lttng_error_code ret_code
= LTTNG_OK
;
5168 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5169 struct consumer_output
*original_ust_consumer_output
= NULL
;
5170 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5171 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5172 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5174 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5176 snapshot_output
->name
,
5177 snapshot_output
->datetime
,
5178 snapshot_output
->nb_snapshot
);
5179 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5180 ERR("Failed to format snapshot name");
5181 ret_code
= LTTNG_ERR_INVALID
;
5184 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5185 snapshot_output
->name
, session
->name
,
5186 snapshot_chunk_name
);
5187 if (!session
->kernel_session
&& !session
->ust_session
) {
5188 ERR("Failed to record snapshot as no channels exist");
5189 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5193 if (session
->kernel_session
) {
5194 original_kernel_consumer_output
=
5195 session
->kernel_session
->consumer
;
5196 snapshot_kernel_consumer_output
=
5197 consumer_copy_output(snapshot_output
->consumer
);
5198 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5199 snapshot_chunk_name
);
5201 /* Copy the original domain subdir. */
5202 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5203 original_kernel_consumer_output
->domain_subdir
);
5205 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5206 original_kernel_consumer_output
);
5208 ERR("Failed to copy consumer sockets from snapshot output configuration");
5209 ret_code
= LTTNG_ERR_NOMEM
;
5212 ret_code
= set_relayd_for_snapshot(
5213 snapshot_kernel_consumer_output
, session
);
5214 if (ret_code
!= LTTNG_OK
) {
5215 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5218 session
->kernel_session
->consumer
=
5219 snapshot_kernel_consumer_output
;
5221 if (session
->ust_session
) {
5222 original_ust_consumer_output
= session
->ust_session
->consumer
;
5223 snapshot_ust_consumer_output
=
5224 consumer_copy_output(snapshot_output
->consumer
);
5225 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5226 snapshot_chunk_name
);
5228 /* Copy the original domain subdir. */
5229 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5230 original_ust_consumer_output
->domain_subdir
);
5232 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5233 original_ust_consumer_output
);
5235 ERR("Failed to copy consumer sockets from snapshot output configuration");
5236 ret_code
= LTTNG_ERR_NOMEM
;
5239 ret_code
= set_relayd_for_snapshot(
5240 snapshot_ust_consumer_output
, session
);
5241 if (ret_code
!= LTTNG_OK
) {
5242 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5245 session
->ust_session
->consumer
=
5246 snapshot_ust_consumer_output
;
5249 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5250 snapshot_kernel_consumer_output
?:
5251 snapshot_ust_consumer_output
,
5252 consumer_output_get_base_path(
5253 snapshot_output
->consumer
),
5254 snapshot_chunk_name
);
5255 if (!snapshot_trace_chunk
) {
5256 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5258 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5261 assert(!session
->current_trace_chunk
);
5262 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5263 lttng_trace_chunk_put(snapshot_trace_chunk
);
5264 snapshot_trace_chunk
= NULL
;
5266 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5268 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5272 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5273 snapshot_output
->max_size
);
5274 if (nb_packets_per_stream
< 0) {
5275 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5276 goto error_close_trace_chunk
;
5279 if (session
->kernel_session
) {
5280 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5281 snapshot_kernel_consumer_output
, session
,
5282 wait
, nb_packets_per_stream
);
5283 if (ret_code
!= LTTNG_OK
) {
5284 goto error_close_trace_chunk
;
5288 if (session
->ust_session
) {
5289 ret_code
= record_ust_snapshot(session
->ust_session
,
5290 snapshot_ust_consumer_output
, session
,
5291 wait
, nb_packets_per_stream
);
5292 if (ret_code
!= LTTNG_OK
) {
5293 goto error_close_trace_chunk
;
5297 error_close_trace_chunk
:
5298 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5299 ERR("Failed to release the current trace chunk of session \"%s\"",
5301 ret_code
= LTTNG_ERR_UNK
;
5304 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5305 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5307 * Don't goto end; make sure the chunk is closed for the session
5308 * to allow future snapshots.
5310 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5312 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5315 lttng_trace_chunk_put(snapshot_trace_chunk
);
5316 snapshot_trace_chunk
= NULL
;
5318 if (original_ust_consumer_output
) {
5319 session
->ust_session
->consumer
= original_ust_consumer_output
;
5321 if (original_kernel_consumer_output
) {
5322 session
->kernel_session
->consumer
=
5323 original_kernel_consumer_output
;
5325 consumer_output_put(snapshot_ust_consumer_output
);
5326 consumer_output_put(snapshot_kernel_consumer_output
);
5331 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5333 * The wait parameter is ignored so this call always wait for the snapshot to
5334 * complete before returning.
5336 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5338 int cmd_snapshot_record(struct ltt_session
*session
,
5339 const struct lttng_snapshot_output
*output
, int wait
)
5341 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5343 unsigned int snapshot_success
= 0;
5345 struct snapshot_output
*tmp_output
= NULL
;
5350 DBG("Cmd snapshot record for session %s", session
->name
);
5352 /* Get the datetime for the snapshot output directory. */
5353 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5356 cmd_ret
= LTTNG_ERR_INVALID
;
5361 * Permission denied to create an output if the session is not
5362 * set in no output mode.
5364 if (session
->output_traces
) {
5365 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5369 /* The session needs to be started at least once. */
5370 if (!session
->has_been_started
) {
5371 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5375 /* Use temporary output for the session. */
5376 if (*output
->ctrl_url
!= '\0') {
5377 tmp_output
= snapshot_output_alloc();
5379 cmd_ret
= LTTNG_ERR_NOMEM
;
5383 ret
= snapshot_output_init(session
, output
->max_size
,
5385 output
->ctrl_url
, output
->data_url
,
5389 if (ret
== -ENOMEM
) {
5390 cmd_ret
= LTTNG_ERR_NOMEM
;
5392 cmd_ret
= LTTNG_ERR_INVALID
;
5396 /* Use the global session count for the temporary snapshot. */
5397 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5399 /* Use the global datetime */
5400 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5401 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
5402 if (cmd_ret
!= LTTNG_OK
) {
5405 snapshot_success
= 1;
5407 struct snapshot_output
*sout
;
5408 struct lttng_ht_iter iter
;
5411 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5412 &iter
.iter
, sout
, node
.node
) {
5413 struct snapshot_output output_copy
;
5416 * Make a local copy of the output and override output
5417 * parameters with those provided as part of the
5420 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5422 if (output
->max_size
!= (uint64_t) -1ULL) {
5423 output_copy
.max_size
= output
->max_size
;
5426 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5427 memcpy(output_copy
.datetime
, datetime
,
5430 /* Use temporary name. */
5431 if (*output
->name
!= '\0') {
5432 if (lttng_strncpy(output_copy
.name
,
5434 sizeof(output_copy
.name
))) {
5435 cmd_ret
= LTTNG_ERR_INVALID
;
5441 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
5442 if (cmd_ret
!= LTTNG_OK
) {
5446 snapshot_success
= 1;
5451 if (snapshot_success
) {
5452 session
->snapshot
.nb_snapshot
++;
5454 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5459 snapshot_output_destroy(tmp_output
);
5465 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5467 int cmd_set_session_shm_path(struct ltt_session
*session
,
5468 const char *shm_path
)
5474 * Can only set shm path before session is started.
5476 if (session
->has_been_started
) {
5477 return LTTNG_ERR_SESSION_STARTED
;
5480 strncpy(session
->shm_path
, shm_path
,
5481 sizeof(session
->shm_path
));
5482 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5488 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5490 * Ask the consumer to rotate the session output directory.
5491 * The session lock must be held.
5493 * Returns LTTNG_OK on success or else a negative LTTng error code.
5495 int cmd_rotate_session(struct ltt_session
*session
,
5496 struct lttng_rotate_session_return
*rotate_return
,
5497 bool quiet_rotation
,
5498 enum lttng_trace_chunk_command_type command
)
5501 uint64_t ongoing_rotation_chunk_id
;
5502 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5503 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5504 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5505 enum lttng_trace_chunk_status chunk_status
;
5506 bool failed_to_rotate
= false;
5507 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5511 if (!session
->has_been_started
) {
5512 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5517 * Explicit rotation is not supported for live sessions.
5518 * However, live sessions can perform a quiet rotation on
5520 * Rotation is not supported for snapshot traces (no output).
5522 if ((!quiet_rotation
&& session
->live_timer
) ||
5523 !session
->output_traces
) {
5524 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5528 /* Unsupported feature in lttng-relayd before 2.11. */
5529 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5530 (session
->consumer
->relay_major_version
== 2 &&
5531 session
->consumer
->relay_minor_version
< 11)) {
5532 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5536 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5537 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5538 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5542 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5543 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5545 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5550 * After a stop, we only allow one rotation to occur, the other ones are
5551 * useless until a new start.
5553 if (session
->rotated_after_last_stop
) {
5554 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5556 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5561 * After a stop followed by a clear, disallow following rotations a they would
5562 * generate empty chunks.
5564 if (session
->cleared_after_last_stop
) {
5565 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5567 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5571 if (session
->active
) {
5572 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5574 if (!new_trace_chunk
) {
5575 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5581 * The current trace chunk becomes the chunk being archived.
5583 * After this point, "chunk_being_archived" must absolutely
5584 * be closed on the consumer(s), otherwise it will never be
5585 * cleaned-up, which will result in a leak.
5587 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5588 &chunk_being_archived
);
5590 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5594 if (session
->kernel_session
) {
5595 cmd_ret
= kernel_rotate_session(session
);
5596 if (cmd_ret
!= LTTNG_OK
) {
5597 failed_to_rotate
= true;
5598 rotation_fail_code
= cmd_ret
;
5601 if (session
->ust_session
) {
5602 cmd_ret
= ust_app_rotate_session(session
);
5603 if (cmd_ret
!= LTTNG_OK
) {
5604 failed_to_rotate
= true;
5605 rotation_fail_code
= cmd_ret
;
5609 if (!session
->active
) {
5610 session
->rotated_after_last_stop
= true;
5613 if (!chunk_being_archived
) {
5614 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5616 if (failed_to_rotate
) {
5617 cmd_ret
= rotation_fail_code
;
5624 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5625 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5626 &ongoing_rotation_chunk_id
);
5627 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5629 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5630 command
, session
->last_chunk_path
);
5632 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5636 if (failed_to_rotate
) {
5637 cmd_ret
= rotation_fail_code
;
5641 session
->quiet_rotation
= quiet_rotation
;
5642 ret
= timer_session_rotation_pending_check_start(session
,
5643 DEFAULT_ROTATE_PENDING_TIMER
);
5645 cmd_ret
= LTTNG_ERR_UNK
;
5649 if (rotate_return
) {
5650 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5653 session
->chunk_being_archived
= chunk_being_archived
;
5654 chunk_being_archived
= NULL
;
5655 if (!quiet_rotation
) {
5656 ret
= notification_thread_command_session_rotation_ongoing(
5657 the_notification_thread_handle
, session
->name
,
5658 session
->uid
, session
->gid
,
5659 ongoing_rotation_chunk_id
);
5660 if (ret
!= LTTNG_OK
) {
5661 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5667 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5668 session
->name
, ongoing_rotation_chunk_id
);
5670 lttng_trace_chunk_put(new_trace_chunk
);
5671 lttng_trace_chunk_put(chunk_being_archived
);
5672 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5675 if (session_reset_rotation_state(session
,
5676 LTTNG_ROTATION_STATE_ERROR
)) {
5677 ERR("Failed to reset rotation state of session \"%s\"",
5684 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5686 * Check if the session has finished its rotation.
5688 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5690 int cmd_rotate_get_info(struct ltt_session
*session
,
5691 struct lttng_rotation_get_info_return
*info_return
,
5692 uint64_t rotation_id
)
5694 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5695 enum lttng_rotation_state rotation_state
;
5697 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5698 session
->most_recent_chunk_id
.value
);
5700 if (session
->chunk_being_archived
) {
5701 enum lttng_trace_chunk_status chunk_status
;
5704 chunk_status
= lttng_trace_chunk_get_id(
5705 session
->chunk_being_archived
,
5707 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5709 rotation_state
= rotation_id
== chunk_id
?
5710 LTTNG_ROTATION_STATE_ONGOING
:
5711 LTTNG_ROTATION_STATE_EXPIRED
;
5713 if (session
->last_archived_chunk_id
.is_set
&&
5714 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5715 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5717 rotation_state
= session
->rotation_state
;
5721 switch (rotation_state
) {
5722 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5723 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5726 case LTTNG_ROTATION_STATE_EXPIRED
:
5727 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5728 rotation_id
, session
->name
);
5730 case LTTNG_ROTATION_STATE_ONGOING
:
5731 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5732 rotation_id
, session
->name
);
5734 case LTTNG_ROTATION_STATE_COMPLETED
:
5738 char *current_tracing_path_reply
;
5739 size_t current_tracing_path_reply_len
;
5741 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5742 rotation_id
, session
->name
);
5744 switch (session_get_consumer_destination_type(session
)) {
5745 case CONSUMER_DST_LOCAL
:
5746 current_tracing_path_reply
=
5747 info_return
->location
.local
.absolute_path
;
5748 current_tracing_path_reply_len
=
5749 sizeof(info_return
->location
.local
.absolute_path
);
5750 info_return
->location_type
=
5751 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5752 fmt_ret
= asprintf(&chunk_path
,
5753 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5754 session_get_base_path(session
),
5755 session
->last_archived_chunk_name
);
5756 if (fmt_ret
== -1) {
5757 PERROR("Failed to format the path of the last archived trace chunk");
5758 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5759 cmd_ret
= LTTNG_ERR_UNK
;
5763 case CONSUMER_DST_NET
:
5765 uint16_t ctrl_port
, data_port
;
5767 current_tracing_path_reply
=
5768 info_return
->location
.relay
.relative_path
;
5769 current_tracing_path_reply_len
=
5770 sizeof(info_return
->location
.relay
.relative_path
);
5771 /* Currently the only supported relay protocol. */
5772 info_return
->location
.relay
.protocol
=
5773 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5775 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5776 session_get_net_consumer_hostname(session
),
5777 sizeof(info_return
->location
.relay
.host
));
5779 ERR("Failed to copy host name to rotate_get_info reply");
5780 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5781 cmd_ret
= LTTNG_ERR_SET_URL
;
5785 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5786 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5787 info_return
->location
.relay
.ports
.data
= data_port
;
5788 info_return
->location_type
=
5789 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5790 chunk_path
= strdup(session
->last_chunk_path
);
5792 ERR("Failed to allocate the path of the last archived trace chunk");
5793 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5794 cmd_ret
= LTTNG_ERR_UNK
;
5803 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5804 chunk_path
, current_tracing_path_reply_len
);
5807 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5808 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5809 cmd_ret
= LTTNG_ERR_UNK
;
5815 case LTTNG_ROTATION_STATE_ERROR
:
5816 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5817 rotation_id
, session
->name
);
5825 info_return
->status
= (int32_t) rotation_state
;
5830 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5832 * Configure the automatic rotation parameters.
5833 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5834 * 'activate' to false means deactivate the rotation schedule and validate that
5835 * 'new_value' has the same value as the currently active value.
5837 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5839 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5840 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5842 struct notification_thread_handle
*notification_thread_handle
)
5845 uint64_t *parameter_value
;
5849 DBG("Cmd rotate set schedule session %s", session
->name
);
5851 if (session
->live_timer
|| !session
->output_traces
) {
5852 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5853 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5857 switch (schedule_type
) {
5858 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5859 parameter_value
= &session
->rotate_size
;
5861 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5862 parameter_value
= &session
->rotate_timer_period
;
5863 if (new_value
>= UINT_MAX
) {
5864 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5865 new_value
, UINT_MAX
);
5866 ret
= LTTNG_ERR_INVALID
;
5871 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5872 ret
= LTTNG_ERR_INVALID
;
5876 /* Improper use of the API. */
5877 if (new_value
== -1ULL) {
5878 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5879 ret
= LTTNG_ERR_INVALID
;
5884 * As indicated in struct ltt_session's comments, a value of == 0 means
5885 * this schedule rotation type is not in use.
5887 * Reject the command if we were asked to activate a schedule that was
5890 if (activate
&& *parameter_value
!= 0) {
5891 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5892 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5897 * Reject the command if we were asked to deactivate a schedule that was
5900 if (!activate
&& *parameter_value
== 0) {
5901 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5902 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5907 * Reject the command if we were asked to deactivate a schedule that
5910 if (!activate
&& *parameter_value
!= new_value
) {
5911 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5912 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5916 *parameter_value
= activate
? new_value
: 0;
5918 switch (schedule_type
) {
5919 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5920 if (activate
&& session
->active
) {
5922 * Only start the timer if the session is active,
5923 * otherwise it will be started when the session starts.
5925 ret
= timer_session_rotation_schedule_timer_start(
5926 session
, new_value
);
5928 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5929 ret
= LTTNG_ERR_UNK
;
5933 ret
= timer_session_rotation_schedule_timer_stop(
5936 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5937 ret
= LTTNG_ERR_UNK
;
5942 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5944 ret
= subscribe_session_consumed_size_rotation(session
,
5945 new_value
, notification_thread_handle
);
5947 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5948 ret
= LTTNG_ERR_UNK
;
5952 ret
= unsubscribe_session_consumed_size_rotation(session
,
5953 notification_thread_handle
);
5955 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5956 ret
= LTTNG_ERR_UNK
;
5963 /* Would have been caught before. */
5975 /* Wait for a given path to be removed before continuing. */
5976 static enum lttng_error_code
wait_on_path(void *path_data
)
5978 const char *shm_path
= path_data
;
5980 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5986 ret
= stat(shm_path
, &st
);
5988 if (errno
!= ENOENT
) {
5989 PERROR("stat() returned an error while checking for the existence of the shm path");
5991 DBG("shm path no longer exists, completing the destruction of session");
5995 if (!S_ISDIR(st
.st_mode
)) {
5996 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6001 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6007 * Returns a pointer to a handler to run on completion of a command.
6008 * Returns NULL if no handler has to be run for the last command executed.
6010 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6012 struct cmd_completion_handler
*handler
= current_completion_handler
;
6014 current_completion_handler
= NULL
;
6019 * Init command subsystem.
6024 * Set network sequence index to 1 for streams to match a relayd
6025 * socket on the consumer side.
6027 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6028 relayd_net_seq_idx
= 1;
6029 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6031 DBG("Command subsystem initialized");