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
9 #include "bin/lttng-sessiond/tracker.h"
10 #include "lttng/lttng-error.h"
11 #include "lttng/tracker.h"
15 #include <urcu/list.h>
16 #include <urcu/uatomic.h>
20 #include <common/buffer-view.h>
21 #include <common/common.h>
22 #include <common/compat/string.h>
23 #include <common/defaults.h>
24 #include <common/dynamic-buffer.h>
25 #include <common/kernel-ctl/kernel-ctl.h>
26 #include <common/relayd/relayd.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/string-utils/string-utils.h>
29 #include <common/trace-chunk.h>
30 #include <common/utils.h>
31 #include <lttng/action/action.h>
32 #include <lttng/channel-internal.h>
33 #include <lttng/channel.h>
34 #include <lttng/condition/condition.h>
35 #include <lttng/event-internal.h>
36 #include <lttng/location-internal.h>
37 #include <lttng/rotate-internal.h>
38 #include <lttng/session-descriptor-internal.h>
39 #include <lttng/session-internal.h>
40 #include <lttng/trigger/trigger-internal.h>
41 #include <lttng/userspace-probe-internal.h>
46 #include "health-sessiond.h"
48 #include "kernel-consumer.h"
49 #include "lttng-sessiond.h"
51 #include "lttng-syscall.h"
53 #include "buffer-registry.h"
54 #include "notification-thread.h"
55 #include "notification-thread-commands.h"
57 #include "rotation-thread.h"
59 #include "agent-thread.h"
63 /* Sleep for 100ms between each check for the shm path's deletion. */
64 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
66 struct cmd_destroy_session_reply_context
{
68 bool implicit_rotation_on_destroy
;
70 * Indicates whether or not an error occurred while launching the
71 * destruction of a session.
73 enum lttng_error_code destruction_status
;
76 static enum lttng_error_code
wait_on_path(void *path
);
79 * Command completion handler that is used by the destroy command
80 * when a session that has a non-default shm_path is being destroyed.
82 * See comment in cmd_destroy_session() for the rationale.
84 static struct destroy_completion_handler
{
85 struct cmd_completion_handler handler
;
86 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
87 } destroy_completion_handler
= {
90 .data
= destroy_completion_handler
.shm_path
95 static struct cmd_completion_handler
*current_completion_handler
;
98 * Used to keep a unique index for each relayd socket created where this value
99 * is associated with streams on the consumer so it can match the right relayd
100 * to send to. It must be accessed with the relayd_net_seq_idx_lock
103 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
104 static uint64_t relayd_net_seq_idx
;
106 static int validate_ust_event_name(const char *);
107 static int cmd_enable_event_internal(struct ltt_session
*session
,
108 const struct lttng_domain
*domain
,
109 char *channel_name
, struct lttng_event
*event
,
110 char *filter_expression
,
111 struct lttng_filter_bytecode
*filter
,
112 struct lttng_event_exclusion
*exclusion
,
114 static int cmd_enable_channel_internal(struct ltt_session
*session
,
115 const struct lttng_domain
*domain
,
116 const struct lttng_channel
*_attr
,
120 * Create a session path used by list_lttng_sessions for the case that the
121 * session consumer is on the network.
123 static int build_network_session_path(char *dst
, size_t size
,
124 struct ltt_session
*session
)
126 int ret
, kdata_port
, udata_port
;
127 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
128 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
133 memset(tmp_urls
, 0, sizeof(tmp_urls
));
134 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
136 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
138 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
139 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
140 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
143 if (session
->ust_session
&& session
->ust_session
->consumer
) {
144 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
145 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
148 if (uuri
== NULL
&& kuri
== NULL
) {
149 uri
= &session
->consumer
->dst
.net
.control
;
150 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
151 } else if (kuri
&& uuri
) {
152 ret
= uri_compare(kuri
, uuri
);
156 /* Build uuri URL string */
157 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
164 } else if (kuri
&& uuri
== NULL
) {
166 } else if (uuri
&& kuri
== NULL
) {
170 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
176 * Do we have a UST url set. If yes, this means we have both kernel and UST
179 if (*tmp_uurl
!= '\0') {
180 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
181 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
184 if (kuri
|| (!kuri
&& !uuri
)) {
187 /* No kernel URI, use the UST port. */
190 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
198 * Get run-time attributes if the session has been started (discarded events,
201 static int get_kernel_runtime_stats(struct ltt_session
*session
,
202 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
203 uint64_t *lost_packets
)
207 if (!session
->has_been_started
) {
209 *discarded_events
= 0;
214 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
215 session
->kernel_session
->consumer
,
221 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
222 session
->kernel_session
->consumer
,
233 * Get run-time attributes if the session has been started (discarded events,
236 static int get_ust_runtime_stats(struct ltt_session
*session
,
237 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
238 uint64_t *lost_packets
)
241 struct ltt_ust_session
*usess
;
243 if (!discarded_events
|| !lost_packets
) {
248 usess
= session
->ust_session
;
249 assert(discarded_events
);
250 assert(lost_packets
);
252 if (!usess
|| !session
->has_been_started
) {
253 *discarded_events
= 0;
259 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
260 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
261 &usess
->buffer_reg_uid_list
,
262 usess
->consumer
, uchan
->id
,
263 uchan
->attr
.overwrite
,
266 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
267 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
268 uchan
, usess
->consumer
,
269 uchan
->attr
.overwrite
,
275 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
276 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
278 ERR("Unsupported buffer type");
289 * Create a list of agent domain events.
291 * Return number of events in list on success or else a negative value.
293 static enum lttng_error_code
list_lttng_agent_events(
294 struct agent
*agt
, struct lttng_dynamic_buffer
*buffer
,
295 unsigned int *nb_events
)
297 enum lttng_error_code ret_code
;
299 unsigned int local_nb_events
= 0;
300 struct agent_event
*event
;
301 struct lttng_ht_iter iter
;
307 DBG3("Listing agent events");
310 tmp
= lttng_ht_get_count(agt
->events
);
316 if (tmp
> UINT_MAX
) {
317 ret_code
= LTTNG_ERR_OVERFLOW
;
321 local_nb_events
= (unsigned int) tmp
;
323 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
324 struct lttng_event
*tmp
= lttng_event_create();
327 ret_code
= LTTNG_ERR_NOMEM
;
331 if(lttng_strncpy(tmp
->name
, event
->name
, sizeof(tmp
->name
))) {
332 lttng_event_destroy(tmp
);
333 ret_code
= LTTNG_ERR_FATAL
;
337 tmp
->name
[sizeof(tmp
->name
) - 1] = '\0';
338 tmp
->enabled
= event
->enabled
;
339 tmp
->loglevel
= event
->loglevel_value
;
340 tmp
->loglevel_type
= event
->loglevel_type
;
342 ret
= lttng_event_serialize(tmp
, 0, NULL
,
343 event
->filter_expression
, 0, NULL
, buffer
,
345 lttng_event_destroy(tmp
);
347 ret_code
= LTTNG_ERR_FATAL
;
354 *nb_events
= local_nb_events
;
361 * Create a list of ust global domain events.
363 static enum lttng_error_code
list_lttng_ust_global_events(char *channel_name
,
364 struct ltt_ust_domain_global
*ust_global
,
365 struct lttng_dynamic_buffer
*buffer
,
366 unsigned int *nb_events
)
368 enum lttng_error_code ret_code
;
370 struct lttng_ht_iter iter
;
371 struct lttng_ht_node_str
*node
;
372 struct ltt_ust_channel
*uchan
;
373 struct ltt_ust_event
*uevent
;
375 unsigned int local_nb_events
= 0;
376 char **exclusion_list
= NULL
;
382 DBG("Listing UST global events for channel %s", channel_name
);
386 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
387 node
= lttng_ht_iter_get_node_str(&iter
);
389 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
393 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
395 tmp
= lttng_ht_get_count(uchan
->events
);
402 if (tmp
> UINT_MAX
) {
403 ret_code
= LTTNG_ERR_OVERFLOW
;
407 local_nb_events
= (unsigned int) tmp
;
409 DBG3("Listing UST global %d events", *nb_events
);
411 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
412 struct lttng_event
*tmp
= NULL
;
414 if (uevent
->internal
) {
415 /* This event should remain hidden from clients */
420 /* Prepare exclusion list. */
421 if (uevent
->exclusion
&& uevent
->exclusion
->count
> 0) {
422 exclusion_list
= zmalloc(sizeof(char *) * uevent
->exclusion
->count
);
423 if (!exclusion_list
) {
426 for (i
= 0; i
< uevent
->exclusion
->count
; i
++) {
427 exclusion_list
[i
] = (char *) &(uevent
->exclusion
->names
[i
]);
431 tmp
= lttng_event_create();
433 ret_code
= LTTNG_ERR_NOMEM
;
437 if (lttng_strncpy(tmp
->name
, uevent
->attr
.name
,
438 LTTNG_SYMBOL_NAME_LEN
)) {
439 ret_code
= LTTNG_ERR_FATAL
;
440 lttng_event_destroy(tmp
);
444 tmp
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
445 tmp
->enabled
= uevent
->enabled
;
447 switch (uevent
->attr
.instrumentation
) {
448 case LTTNG_UST_TRACEPOINT
:
449 tmp
->type
= LTTNG_EVENT_TRACEPOINT
;
451 case LTTNG_UST_PROBE
:
452 tmp
->type
= LTTNG_EVENT_PROBE
;
454 case LTTNG_UST_FUNCTION
:
455 tmp
->type
= LTTNG_EVENT_FUNCTION
;
459 tmp
->loglevel
= uevent
->attr
.loglevel
;
460 switch (uevent
->attr
.loglevel_type
) {
461 case LTTNG_UST_LOGLEVEL_ALL
:
462 tmp
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
464 case LTTNG_UST_LOGLEVEL_RANGE
:
465 tmp
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
467 case LTTNG_UST_LOGLEVEL_SINGLE
:
468 tmp
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
471 if (uevent
->filter
) {
474 if (uevent
->exclusion
) {
479 * We do not care about the filter bytecode and the fd from the
480 * userspace_probe_location.
482 ret
= lttng_event_serialize(tmp
,
483 exclusion_list
? uevent
->exclusion
->count
: 0,
484 exclusion_list
, uevent
->filter_expression
, 0,
486 lttng_event_destroy(tmp
);
487 free(exclusion_list
);
488 exclusion_list
= NULL
;
490 ret_code
= LTTNG_ERR_FATAL
;
496 /* nb_events is already set at this point. */
498 *nb_events
= local_nb_events
;
500 free(exclusion_list
);
506 * Fill lttng_event array of all kernel events in the channel.
508 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
509 struct ltt_kernel_session
*kernel_session
,
510 struct lttng_dynamic_buffer
*payload
,
511 unsigned int *nb_events
)
513 enum lttng_error_code ret_code
;
515 struct ltt_kernel_event
*event
;
516 struct ltt_kernel_channel
*kchan
;
520 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
522 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
526 *nb_events
= kchan
->event_count
;
528 DBG("Listing events for channel %s", kchan
->channel
->name
);
530 if (*nb_events
== 0) {
535 /* Kernel channels */
536 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
537 struct lttng_event
*tmp
= lttng_event_create();
540 ret_code
= LTTNG_ERR_NOMEM
;
544 if (lttng_strncpy(tmp
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
545 lttng_event_destroy(tmp
);
546 ret_code
= LTTNG_ERR_FATAL
;
551 tmp
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
552 tmp
->enabled
= event
->enabled
;
553 tmp
->filter
= (unsigned char) !!event
->filter_expression
;
555 switch (event
->event
->instrumentation
) {
556 case LTTNG_KERNEL_TRACEPOINT
:
557 tmp
->type
= LTTNG_EVENT_TRACEPOINT
;
559 case LTTNG_KERNEL_KRETPROBE
:
560 tmp
->type
= LTTNG_EVENT_FUNCTION
;
561 memcpy(&tmp
->attr
.probe
, &event
->event
->u
.kprobe
,
562 sizeof(struct lttng_kernel_kprobe
));
564 case LTTNG_KERNEL_KPROBE
:
565 tmp
->type
= LTTNG_EVENT_PROBE
;
566 memcpy(&tmp
->attr
.probe
, &event
->event
->u
.kprobe
,
567 sizeof(struct lttng_kernel_kprobe
));
569 case LTTNG_KERNEL_UPROBE
:
570 tmp
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
572 case LTTNG_KERNEL_FUNCTION
:
573 tmp
->type
= LTTNG_EVENT_FUNCTION
;
574 memcpy(&(tmp
->attr
.ftrace
), &event
->event
->u
.ftrace
,
575 sizeof(struct lttng_kernel_function
));
577 case LTTNG_KERNEL_NOOP
:
578 tmp
->type
= LTTNG_EVENT_NOOP
;
580 case LTTNG_KERNEL_SYSCALL
:
581 tmp
->type
= LTTNG_EVENT_SYSCALL
;
583 case LTTNG_KERNEL_ALL
:
590 if (event
->userspace_probe_location
) {
591 struct lttng_userspace_probe_location
*location_copy
=
592 lttng_userspace_probe_location_copy(
593 event
->userspace_probe_location
);
594 if (!location_copy
) {
595 lttng_event_destroy(tmp
);
596 ret_code
= LTTNG_ERR_NOMEM
;
599 ret
= lttng_event_set_userspace_probe_location(
602 lttng_event_destroy(tmp
);
603 lttng_userspace_probe_location_destroy(
605 ret_code
= LTTNG_ERR_INVALID
;
610 ret
= lttng_event_serialize(tmp
, 0, NULL
,
611 event
->filter_expression
, 0, NULL
, payload
,
613 lttng_event_destroy(tmp
);
615 ret_code
= LTTNG_ERR_FATAL
;
626 * Add URI so the consumer output object. Set the correct path depending on the
627 * domain adding the default trace directory.
629 static enum lttng_error_code
add_uri_to_consumer(
630 const struct ltt_session
*session
,
631 struct consumer_output
*consumer
,
632 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
635 enum lttng_error_code ret_code
= LTTNG_OK
;
639 if (consumer
== NULL
) {
640 DBG("No consumer detected. Don't add URI. Stopping.");
641 ret_code
= LTTNG_ERR_NO_CONSUMER
;
646 case LTTNG_DOMAIN_KERNEL
:
647 ret
= lttng_strncpy(consumer
->domain_subdir
,
648 DEFAULT_KERNEL_TRACE_DIR
,
649 sizeof(consumer
->domain_subdir
));
651 case LTTNG_DOMAIN_UST
:
652 ret
= lttng_strncpy(consumer
->domain_subdir
,
653 DEFAULT_UST_TRACE_DIR
,
654 sizeof(consumer
->domain_subdir
));
658 * This case is possible is we try to add the URI to the global
659 * tracing session consumer object which in this case there is
662 memset(consumer
->domain_subdir
, 0,
663 sizeof(consumer
->domain_subdir
));
667 ERR("Failed to initialize consumer output domain subdirectory");
668 ret_code
= LTTNG_ERR_FATAL
;
672 switch (uri
->dtype
) {
675 DBG2("Setting network URI to consumer");
677 if (consumer
->type
== CONSUMER_DST_NET
) {
678 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
679 consumer
->dst
.net
.control_isset
) ||
680 (uri
->stype
== LTTNG_STREAM_DATA
&&
681 consumer
->dst
.net
.data_isset
)) {
682 ret_code
= LTTNG_ERR_URL_EXIST
;
686 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
689 /* Set URI into consumer output object */
690 ret
= consumer_set_network_uri(session
, consumer
, uri
);
694 } else if (ret
== 1) {
696 * URI was the same in the consumer so we do not append the subdir
697 * again so to not duplicate output dir.
704 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
705 ret_code
= LTTNG_ERR_INVALID
;
708 DBG2("Setting trace directory path from URI to %s",
710 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
712 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
714 sizeof(consumer
->dst
.session_root_path
));
716 ret_code
= LTTNG_ERR_FATAL
;
719 consumer
->type
= CONSUMER_DST_LOCAL
;
729 * Init tracing by creating trace directory and sending fds kernel consumer.
731 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
734 struct lttng_ht_iter iter
;
735 struct consumer_socket
*socket
;
741 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
742 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
744 pthread_mutex_lock(socket
->lock
);
745 ret
= kernel_consumer_send_session(socket
, session
);
746 pthread_mutex_unlock(socket
->lock
);
748 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
760 * Create a socket to the relayd using the URI.
762 * On success, the relayd_sock pointer is set to the created socket.
763 * Else, it remains untouched and an LTTng error code is returned.
765 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
766 struct lttcomm_relayd_sock
**relayd_sock
,
767 struct consumer_output
*consumer
)
770 enum lttng_error_code status
= LTTNG_OK
;
771 struct lttcomm_relayd_sock
*rsock
;
773 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
774 RELAYD_VERSION_COMM_MINOR
);
776 status
= LTTNG_ERR_FATAL
;
781 * Connect to relayd so we can proceed with a session creation. This call
782 * can possibly block for an arbitrary amount of time to set the health
783 * state to be in poll execution.
786 ret
= relayd_connect(rsock
);
789 ERR("Unable to reach lttng-relayd");
790 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
794 /* Create socket for control stream. */
795 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
796 uint64_t result_flags
;
798 DBG3("Creating relayd stream socket from URI");
800 /* Check relayd version */
801 ret
= relayd_version_check(rsock
);
802 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
803 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
805 } else if (ret
< 0) {
806 ERR("Unable to reach lttng-relayd");
807 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
810 consumer
->relay_major_version
= rsock
->major
;
811 consumer
->relay_minor_version
= rsock
->minor
;
812 ret
= relayd_get_configuration(rsock
, 0,
815 ERR("Unable to get relayd configuration");
816 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
819 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
820 consumer
->relay_allows_clear
= true;
822 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
823 DBG3("Creating relayd data socket from URI");
825 /* Command is not valid */
826 ERR("Relayd invalid stream type: %d", uri
->stype
);
827 status
= LTTNG_ERR_INVALID
;
831 *relayd_sock
= rsock
;
836 /* The returned value is not useful since we are on an error path. */
837 (void) relayd_close(rsock
);
845 * Connect to the relayd using URI and send the socket to the right consumer.
847 * The consumer socket lock must be held by the caller.
849 * Returns LTTNG_OK on success or an LTTng error code on failure.
851 static enum lttng_error_code
send_consumer_relayd_socket(
852 unsigned int session_id
,
853 struct lttng_uri
*relayd_uri
,
854 struct consumer_output
*consumer
,
855 struct consumer_socket
*consumer_sock
,
856 const char *session_name
, const char *hostname
,
857 const char *base_path
, int session_live_timer
,
858 const uint64_t *current_chunk_id
,
859 time_t session_creation_time
,
860 bool session_name_contains_creation_time
)
863 struct lttcomm_relayd_sock
*rsock
= NULL
;
864 enum lttng_error_code status
;
866 /* Connect to relayd and make version check if uri is the control. */
867 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
868 if (status
!= LTTNG_OK
) {
869 goto relayd_comm_error
;
873 /* Set the network sequence index if not set. */
874 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
875 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
877 * Increment net_seq_idx because we are about to transfer the
878 * new relayd socket to the consumer.
879 * Assign unique key so the consumer can match streams.
881 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
882 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
885 /* Send relayd socket to consumer. */
886 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
887 relayd_uri
->stype
, session_id
,
888 session_name
, hostname
, base_path
,
889 session_live_timer
, current_chunk_id
,
890 session_creation_time
, session_name_contains_creation_time
);
892 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
896 /* Flag that the corresponding socket was sent. */
897 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
898 consumer_sock
->control_sock_sent
= 1;
899 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
900 consumer_sock
->data_sock_sent
= 1;
904 * Close socket which was dup on the consumer side. The session daemon does
905 * NOT keep track of the relayd socket(s) once transfer to the consumer.
909 if (status
!= LTTNG_OK
) {
911 * The consumer output for this session should not be used anymore
912 * since the relayd connection failed thus making any tracing or/and
913 * streaming not usable.
915 consumer
->enabled
= 0;
917 (void) relayd_close(rsock
);
925 * Send both relayd sockets to a specific consumer and domain. This is a
926 * helper function to facilitate sending the information to the consumer for a
929 * The consumer socket lock must be held by the caller.
931 * Returns LTTNG_OK, or an LTTng error code on failure.
933 static enum lttng_error_code
send_consumer_relayd_sockets(
934 enum lttng_domain_type domain
,
935 unsigned int session_id
, struct consumer_output
*consumer
,
936 struct consumer_socket
*sock
, const char *session_name
,
937 const char *hostname
, const char *base_path
, int session_live_timer
,
938 const uint64_t *current_chunk_id
, time_t session_creation_time
,
939 bool session_name_contains_creation_time
)
941 enum lttng_error_code status
= LTTNG_OK
;
946 /* Sending control relayd socket. */
947 if (!sock
->control_sock_sent
) {
948 status
= send_consumer_relayd_socket(session_id
,
949 &consumer
->dst
.net
.control
, consumer
, sock
,
950 session_name
, hostname
, base_path
, session_live_timer
,
951 current_chunk_id
, session_creation_time
,
952 session_name_contains_creation_time
);
953 if (status
!= LTTNG_OK
) {
958 /* Sending data relayd socket. */
959 if (!sock
->data_sock_sent
) {
960 status
= send_consumer_relayd_socket(session_id
,
961 &consumer
->dst
.net
.data
, consumer
, sock
,
962 session_name
, hostname
, base_path
, session_live_timer
,
963 current_chunk_id
, session_creation_time
,
964 session_name_contains_creation_time
);
965 if (status
!= LTTNG_OK
) {
975 * Setup relayd connections for a tracing session. First creates the socket to
976 * the relayd and send them to the right domain consumer. Consumer type MUST be
979 int cmd_setup_relayd(struct ltt_session
*session
)
982 struct ltt_ust_session
*usess
;
983 struct ltt_kernel_session
*ksess
;
984 struct consumer_socket
*socket
;
985 struct lttng_ht_iter iter
;
986 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
990 usess
= session
->ust_session
;
991 ksess
= session
->kernel_session
;
993 DBG("Setting relayd for session %s", session
->name
);
996 if (session
->current_trace_chunk
) {
997 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
998 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1000 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1001 current_chunk_id
.is_set
= true;
1003 ERR("Failed to get current trace chunk id");
1004 ret
= LTTNG_ERR_UNK
;
1009 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1010 && usess
->consumer
->enabled
) {
1011 /* For each consumer socket, send relayd sockets */
1012 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1013 socket
, node
.node
) {
1014 pthread_mutex_lock(socket
->lock
);
1015 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1016 usess
->consumer
, socket
,
1017 session
->name
, session
->hostname
,
1019 session
->live_timer
,
1020 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1021 session
->creation_time
,
1022 session
->name_contains_creation_time
);
1023 pthread_mutex_unlock(socket
->lock
);
1024 if (ret
!= LTTNG_OK
) {
1027 /* Session is now ready for network streaming. */
1028 session
->net_handle
= 1;
1030 session
->consumer
->relay_major_version
=
1031 usess
->consumer
->relay_major_version
;
1032 session
->consumer
->relay_minor_version
=
1033 usess
->consumer
->relay_minor_version
;
1034 session
->consumer
->relay_allows_clear
=
1035 usess
->consumer
->relay_allows_clear
;
1038 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1039 && ksess
->consumer
->enabled
) {
1040 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1041 socket
, node
.node
) {
1042 pthread_mutex_lock(socket
->lock
);
1043 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1044 ksess
->consumer
, socket
,
1045 session
->name
, session
->hostname
,
1047 session
->live_timer
,
1048 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1049 session
->creation_time
,
1050 session
->name_contains_creation_time
);
1051 pthread_mutex_unlock(socket
->lock
);
1052 if (ret
!= LTTNG_OK
) {
1055 /* Session is now ready for network streaming. */
1056 session
->net_handle
= 1;
1058 session
->consumer
->relay_major_version
=
1059 ksess
->consumer
->relay_major_version
;
1060 session
->consumer
->relay_minor_version
=
1061 ksess
->consumer
->relay_minor_version
;
1062 session
->consumer
->relay_allows_clear
=
1063 ksess
->consumer
->relay_allows_clear
;
1072 * Start a kernel session by opening all necessary streams.
1074 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1077 struct ltt_kernel_channel
*kchan
;
1079 /* Open kernel metadata */
1080 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1081 ret
= kernel_open_metadata(ksess
);
1083 ret
= LTTNG_ERR_KERN_META_FAIL
;
1088 /* Open kernel metadata stream */
1089 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1090 ret
= kernel_open_metadata_stream(ksess
);
1092 ERR("Kernel create metadata stream failed");
1093 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1098 /* For each channel */
1099 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1100 if (kchan
->stream_count
== 0) {
1101 ret
= kernel_open_channel_stream(kchan
);
1103 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1106 /* Update the stream global counter */
1107 ksess
->stream_count_global
+= ret
;
1111 /* Setup kernel consumer socket and send fds to it */
1112 ret
= init_kernel_tracing(ksess
);
1114 ret
= LTTNG_ERR_KERN_START_FAIL
;
1118 /* This start the kernel tracing */
1119 ret
= kernel_start_session(ksess
);
1121 ret
= LTTNG_ERR_KERN_START_FAIL
;
1125 /* Quiescent wait after starting trace */
1126 kernel_wait_quiescent();
1136 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1138 struct ltt_kernel_channel
*kchan
;
1139 bool error_occurred
= false;
1142 if (!ksess
|| !ksess
->active
) {
1145 DBG("Stopping kernel tracing");
1147 ret
= kernel_stop_session(ksess
);
1149 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1153 kernel_wait_quiescent();
1155 /* Flush metadata after stopping (if exists) */
1156 if (ksess
->metadata_stream_fd
>= 0) {
1157 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1159 ERR("Kernel metadata flush failed");
1160 error_occurred
= true;
1164 /* Flush all buffers after stopping */
1165 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1166 ret
= kernel_flush_buffer(kchan
);
1168 ERR("Kernel flush buffer error");
1169 error_occurred
= true;
1174 if (error_occurred
) {
1175 ret
= LTTNG_ERR_UNK
;
1184 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1186 int cmd_disable_channel(struct ltt_session
*session
,
1187 enum lttng_domain_type domain
, char *channel_name
)
1190 struct ltt_ust_session
*usess
;
1192 usess
= session
->ust_session
;
1197 case LTTNG_DOMAIN_KERNEL
:
1199 ret
= channel_kernel_disable(session
->kernel_session
,
1201 if (ret
!= LTTNG_OK
) {
1205 kernel_wait_quiescent();
1208 case LTTNG_DOMAIN_UST
:
1210 struct ltt_ust_channel
*uchan
;
1211 struct lttng_ht
*chan_ht
;
1213 chan_ht
= usess
->domain_global
.channels
;
1215 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1216 if (uchan
== NULL
) {
1217 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1221 ret
= channel_ust_disable(usess
, uchan
);
1222 if (ret
!= LTTNG_OK
) {
1228 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1240 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1242 * The wpipe arguments is used as a notifier for the kernel thread.
1244 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1248 ssize_t sock_recv_len
;
1249 struct lttng_channel
*channel
= NULL
;
1250 struct lttng_buffer_view view
;
1251 struct lttng_dynamic_buffer channel_buffer
;
1253 lttng_dynamic_buffer_init(&channel_buffer
);
1254 channel_len
= (size_t) cmd_ctx
->lsm
->u
.channel
.length
;
1255 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1257 ret
= LTTNG_ERR_NOMEM
;
1261 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1263 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1264 ERR("Failed to receive \"enable channel\" command payload");
1265 ret
= LTTNG_ERR_INVALID
;
1269 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1270 if (!lttng_buffer_view_is_valid(&view
)) {
1271 ret
= LTTNG_ERR_INVALID
;
1275 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1276 ERR("Invalid channel payload received in \"enable channel\" command");
1277 ret
= LTTNG_ERR_INVALID
;
1281 ret
= cmd_enable_channel_internal(cmd_ctx
->session
,
1282 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
), channel
,
1286 lttng_dynamic_buffer_reset(&channel_buffer
);
1287 lttng_channel_destroy(channel
);
1291 static int cmd_enable_channel_internal(struct ltt_session
*session
,
1292 const struct lttng_domain
*domain
,
1293 const struct lttng_channel
*_attr
,
1297 struct ltt_ust_session
*usess
= session
->ust_session
;
1298 struct lttng_ht
*chan_ht
;
1300 struct lttng_channel
*attr
= NULL
;
1306 attr
= lttng_channel_copy(_attr
);
1308 ret
= -LTTNG_ERR_NOMEM
;
1312 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1314 /* Validate channel name */
1315 if (attr
->name
[0] == '.' ||
1316 memchr(attr
->name
, '/', len
) != NULL
) {
1317 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1321 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1326 * If the session is a live session, remove the switch timer, the
1327 * live timer does the same thing but sends also synchronisation
1328 * beacons for inactive streams.
1330 if (session
->live_timer
> 0) {
1331 attr
->attr
.live_timer_interval
= session
->live_timer
;
1332 attr
->attr
.switch_timer_interval
= 0;
1335 /* Check for feature support */
1336 switch (domain
->type
) {
1337 case LTTNG_DOMAIN_KERNEL
:
1339 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1340 /* Sampling position of buffer is not supported */
1341 WARN("Kernel tracer does not support buffer monitoring. "
1342 "Setting the monitor interval timer to 0 "
1343 "(disabled) for channel '%s' of session '%s'",
1344 attr
->name
, session
->name
);
1345 lttng_channel_set_monitor_timer_interval(attr
, 0);
1349 case LTTNG_DOMAIN_UST
:
1351 case LTTNG_DOMAIN_JUL
:
1352 case LTTNG_DOMAIN_LOG4J
:
1353 case LTTNG_DOMAIN_PYTHON
:
1354 if (!agent_tracing_is_enabled()) {
1355 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1356 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1361 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1365 switch (domain
->type
) {
1366 case LTTNG_DOMAIN_KERNEL
:
1368 struct ltt_kernel_channel
*kchan
;
1370 kchan
= trace_kernel_get_channel_by_name(
1371 attr
->name
, session
->kernel_session
);
1372 if (kchan
== NULL
) {
1374 * Don't try to create a channel if the session has been started at
1375 * some point in time before. The tracer does not allow it.
1377 if (session
->has_been_started
) {
1378 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1382 if (session
->snapshot
.nb_output
> 0 ||
1383 session
->snapshot_mode
) {
1384 /* Enforce mmap output for snapshot sessions. */
1385 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1387 ret
= channel_kernel_create(
1388 session
->kernel_session
, attr
, wpipe
);
1389 if (attr
->name
[0] != '\0') {
1390 session
->kernel_session
->has_non_default_channel
= 1;
1393 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1396 if (ret
!= LTTNG_OK
) {
1400 kernel_wait_quiescent();
1403 case LTTNG_DOMAIN_UST
:
1404 case LTTNG_DOMAIN_JUL
:
1405 case LTTNG_DOMAIN_LOG4J
:
1406 case LTTNG_DOMAIN_PYTHON
:
1408 struct ltt_ust_channel
*uchan
;
1413 * Current agent implementation limitations force us to allow
1414 * only one channel at once in "agent" subdomains. Each
1415 * subdomain has a default channel name which must be strictly
1418 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1419 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1420 LTTNG_SYMBOL_NAME_LEN
)) {
1421 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1424 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1425 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1426 LTTNG_SYMBOL_NAME_LEN
)) {
1427 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1430 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1431 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1432 LTTNG_SYMBOL_NAME_LEN
)) {
1433 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1438 chan_ht
= usess
->domain_global
.channels
;
1440 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1441 if (uchan
== NULL
) {
1443 * Don't try to create a channel if the session has been started at
1444 * some point in time before. The tracer does not allow it.
1446 if (session
->has_been_started
) {
1447 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1451 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1452 if (attr
->name
[0] != '\0') {
1453 usess
->has_non_default_channel
= 1;
1456 ret
= channel_ust_enable(usess
, uchan
);
1461 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1465 if (ret
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1466 session
->has_non_mmap_channel
= true;
1471 lttng_channel_destroy(attr
);
1475 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1476 struct ltt_session
*session
,
1477 enum lttng_domain_type domain
,
1478 enum lttng_process_attr process_attr
,
1479 enum lttng_tracking_policy
*policy
)
1481 enum lttng_error_code ret_code
= LTTNG_OK
;
1482 const struct process_attr_tracker
*tracker
;
1485 case LTTNG_DOMAIN_KERNEL
:
1486 if (!session
->kernel_session
) {
1487 ret_code
= LTTNG_ERR_INVALID
;
1490 tracker
= kernel_get_process_attr_tracker(
1491 session
->kernel_session
, process_attr
);
1493 case LTTNG_DOMAIN_UST
:
1494 if (!session
->ust_session
) {
1495 ret_code
= LTTNG_ERR_INVALID
;
1498 tracker
= trace_ust_get_process_attr_tracker(
1499 session
->ust_session
, process_attr
);
1502 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1506 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1508 ret_code
= LTTNG_ERR_INVALID
;
1514 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1515 struct ltt_session
*session
,
1516 enum lttng_domain_type domain
,
1517 enum lttng_process_attr process_attr
,
1518 enum lttng_tracking_policy policy
)
1520 enum lttng_error_code ret_code
= LTTNG_OK
;
1523 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1524 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1525 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1528 ret_code
= LTTNG_ERR_INVALID
;
1533 case LTTNG_DOMAIN_KERNEL
:
1534 if (!session
->kernel_session
) {
1535 ret_code
= LTTNG_ERR_INVALID
;
1538 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1539 session
->kernel_session
, process_attr
, policy
);
1541 case LTTNG_DOMAIN_UST
:
1542 if (!session
->ust_session
) {
1543 ret_code
= LTTNG_ERR_INVALID
;
1546 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1547 session
->ust_session
, process_attr
, policy
);
1550 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1557 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1558 struct ltt_session
*session
,
1559 enum lttng_domain_type domain
,
1560 enum lttng_process_attr process_attr
,
1561 const struct process_attr_value
*value
)
1563 enum lttng_error_code ret_code
= LTTNG_OK
;
1566 case LTTNG_DOMAIN_KERNEL
:
1567 if (!session
->kernel_session
) {
1568 ret_code
= LTTNG_ERR_INVALID
;
1571 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1572 session
->kernel_session
, process_attr
, value
);
1574 case LTTNG_DOMAIN_UST
:
1575 if (!session
->ust_session
) {
1576 ret_code
= LTTNG_ERR_INVALID
;
1579 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1580 session
->ust_session
, process_attr
, value
);
1583 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1590 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1591 struct ltt_session
*session
,
1592 enum lttng_domain_type domain
,
1593 enum lttng_process_attr process_attr
,
1594 const struct process_attr_value
*value
)
1596 enum lttng_error_code ret_code
= LTTNG_OK
;
1599 case LTTNG_DOMAIN_KERNEL
:
1600 if (!session
->kernel_session
) {
1601 ret_code
= LTTNG_ERR_INVALID
;
1604 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1605 session
->kernel_session
, process_attr
, value
);
1607 case LTTNG_DOMAIN_UST
:
1608 if (!session
->ust_session
) {
1609 ret_code
= LTTNG_ERR_INVALID
;
1612 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1613 session
->ust_session
, process_attr
, value
);
1616 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1623 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1624 struct ltt_session
*session
,
1625 enum lttng_domain_type domain
,
1626 enum lttng_process_attr process_attr
,
1627 struct lttng_process_attr_values
**values
)
1629 enum lttng_error_code ret_code
= LTTNG_OK
;
1630 const struct process_attr_tracker
*tracker
;
1631 enum process_attr_tracker_status status
;
1634 case LTTNG_DOMAIN_KERNEL
:
1635 if (!session
->kernel_session
) {
1636 ret_code
= LTTNG_ERR_INVALID
;
1639 tracker
= kernel_get_process_attr_tracker(
1640 session
->kernel_session
, process_attr
);
1642 case LTTNG_DOMAIN_UST
:
1643 if (!session
->ust_session
) {
1644 ret_code
= LTTNG_ERR_INVALID
;
1647 tracker
= trace_ust_get_process_attr_tracker(
1648 session
->ust_session
, process_attr
);
1651 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1656 ret_code
= LTTNG_ERR_INVALID
;
1660 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1662 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1663 ret_code
= LTTNG_OK
;
1665 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1666 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1668 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1669 ret_code
= LTTNG_ERR_NOMEM
;
1672 ret_code
= LTTNG_ERR_UNK
;
1681 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1683 int cmd_disable_event(struct command_ctx
*cmd_ctx
, int sock
)
1687 const char *event_name
;
1688 ssize_t sock_recv_len
;
1689 struct lttng_event
*event
= NULL
;
1690 struct lttng_buffer_view view
;
1691 struct lttng_dynamic_buffer event_buffer
;
1692 struct lttng_event_exclusion
*exclusion
= NULL
;
1693 struct lttng_filter_bytecode
*bytecode
= NULL
;
1694 char *filter_expression
= NULL
;
1695 unsigned int exclusion_count
;
1696 const struct ltt_session
*session
= cmd_ctx
->session
;
1697 const char *channel_name
= cmd_ctx
->lsm
->u
.disable
.channel_name
;
1698 enum lttng_domain_type domain
= cmd_ctx
->lsm
->domain
.type
;
1700 lttng_dynamic_buffer_init(&event_buffer
);
1701 event_len
= (size_t) cmd_ctx
->lsm
->u
.disable
.length
;
1702 ret
= lttng_dynamic_buffer_set_size(&event_buffer
, event_len
);
1704 ret
= LTTNG_ERR_NOMEM
;
1708 sock_recv_len
= lttcomm_recv_unix_sock(
1709 sock
, event_buffer
.data
, event_len
);
1710 if (sock_recv_len
< 0 || sock_recv_len
!= event_len
) {
1711 ERR("Failed to receive \"disable event\" command payload");
1712 ret
= LTTNG_ERR_INVALID
;
1716 view
= lttng_buffer_view_from_dynamic_buffer(&event_buffer
, 0, -1);
1718 if (lttng_event_create_from_buffer(&view
, &event
,
1719 &exclusion
, &filter_expression
,
1720 &bytecode
, sock
) != event_len
) {
1721 ERR("Invalid event payload received in \"disable event\" command");
1722 ret
= LTTNG_ERR_INVALID
;
1726 DBG("Disable event command for event \'%s\'", event
->name
);
1729 * Filter and exclusions are simply not handled by the
1730 * disable event command at this time.
1734 (void) filter_expression
;
1736 (void) exclusion_count
;
1738 /* Ignore the presence of filter or exclusion for the event */
1740 event
->exclusion
= 0;
1742 event_name
= event
->name
;
1744 /* Error out on unhandled search criteria */
1745 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1746 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1747 ret
= LTTNG_ERR_UNK
;
1754 case LTTNG_DOMAIN_KERNEL
:
1756 struct ltt_kernel_channel
*kchan
;
1757 struct ltt_kernel_session
*ksess
;
1759 ksess
= session
->kernel_session
;
1762 * If a non-default channel has been created in the
1763 * session, explicitely require that -c chan_name needs
1766 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1767 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1771 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1772 if (kchan
== NULL
) {
1773 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1777 switch (event
->type
) {
1778 case LTTNG_EVENT_ALL
:
1779 case LTTNG_EVENT_TRACEPOINT
:
1780 case LTTNG_EVENT_SYSCALL
:
1781 case LTTNG_EVENT_PROBE
:
1782 case LTTNG_EVENT_FUNCTION
:
1783 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1784 if (event_name
[0] == '\0') {
1785 ret
= event_kernel_disable_event(kchan
,
1788 ret
= event_kernel_disable_event(kchan
,
1789 event_name
, event
->type
);
1791 if (ret
!= LTTNG_OK
) {
1796 ret
= LTTNG_ERR_UNK
;
1800 kernel_wait_quiescent();
1803 case LTTNG_DOMAIN_UST
:
1805 struct ltt_ust_channel
*uchan
;
1806 struct ltt_ust_session
*usess
;
1808 usess
= session
->ust_session
;
1810 if (validate_ust_event_name(event_name
)) {
1811 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1816 * If a non-default channel has been created in the
1817 * session, explicitly require that -c chan_name needs
1820 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1821 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1825 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1827 if (uchan
== NULL
) {
1828 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1832 switch (event
->type
) {
1833 case LTTNG_EVENT_ALL
:
1835 * An empty event name means that everything
1836 * should be disabled.
1838 if (event
->name
[0] == '\0') {
1839 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1841 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1844 if (ret
!= LTTNG_OK
) {
1849 ret
= LTTNG_ERR_UNK
;
1853 DBG3("Disable UST event %s in channel %s completed", event_name
,
1857 case LTTNG_DOMAIN_LOG4J
:
1858 case LTTNG_DOMAIN_JUL
:
1859 case LTTNG_DOMAIN_PYTHON
:
1862 struct ltt_ust_session
*usess
= session
->ust_session
;
1866 switch (event
->type
) {
1867 case LTTNG_EVENT_ALL
:
1870 ret
= LTTNG_ERR_UNK
;
1874 agt
= trace_ust_find_agent(usess
, domain
);
1876 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1880 * An empty event name means that everything
1881 * should be disabled.
1883 if (event
->name
[0] == '\0') {
1884 ret
= event_agent_disable_all(usess
, agt
);
1886 ret
= event_agent_disable(usess
, agt
, event_name
);
1888 if (ret
!= LTTNG_OK
) {
1895 ret
= LTTNG_ERR_UND
;
1906 free(filter_expression
);
1907 lttng_event_destroy(event
);
1908 lttng_dynamic_buffer_reset(&event_buffer
);
1914 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1916 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1917 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1919 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1920 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1923 * Don't try to add a context if the session has been started at
1924 * some point in time before. The tracer does not allow it and would
1925 * result in a corrupted trace.
1927 if (session
->has_been_started
) {
1928 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1932 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1933 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1934 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1938 case LTTNG_DOMAIN_KERNEL
:
1939 assert(session
->kernel_session
);
1941 if (session
->kernel_session
->channel_count
== 0) {
1942 /* Create default channel */
1943 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1944 if (ret
!= LTTNG_OK
) {
1947 chan_kern_created
= 1;
1949 /* Add kernel context to kernel tracer */
1950 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1951 if (ret
!= LTTNG_OK
) {
1955 case LTTNG_DOMAIN_JUL
:
1956 case LTTNG_DOMAIN_LOG4J
:
1959 * Validate channel name.
1960 * If no channel name is given and the domain is JUL or LOG4J,
1961 * set it to the appropriate domain-specific channel name. If
1962 * a name is provided but does not match the expexted channel
1963 * name, return an error.
1965 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1966 strcmp(channel_name
,
1967 DEFAULT_JUL_CHANNEL_NAME
)) {
1968 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1970 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1971 strcmp(channel_name
,
1972 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1973 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1976 /* break is _not_ missing here. */
1978 case LTTNG_DOMAIN_UST
:
1980 struct ltt_ust_session
*usess
= session
->ust_session
;
1981 unsigned int chan_count
;
1985 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1986 if (chan_count
== 0) {
1987 struct lttng_channel
*attr
;
1988 /* Create default channel */
1989 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1991 ret
= LTTNG_ERR_FATAL
;
1995 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1996 if (ret
!= LTTNG_OK
) {
2000 channel_attr_destroy(attr
);
2001 chan_ust_created
= 1;
2004 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2005 free(app_ctx_provider_name
);
2007 app_ctx_name
= NULL
;
2008 app_ctx_provider_name
= NULL
;
2009 if (ret
!= LTTNG_OK
) {
2015 ret
= LTTNG_ERR_UND
;
2023 if (chan_kern_created
) {
2024 struct ltt_kernel_channel
*kchan
=
2025 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2026 session
->kernel_session
);
2027 /* Created previously, this should NOT fail. */
2029 kernel_destroy_channel(kchan
);
2032 if (chan_ust_created
) {
2033 struct ltt_ust_channel
*uchan
=
2034 trace_ust_find_channel_by_name(
2035 session
->ust_session
->domain_global
.channels
,
2036 DEFAULT_CHANNEL_NAME
);
2037 /* Created previously, this should NOT fail. */
2039 /* Remove from the channel list of the session. */
2040 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2042 trace_ust_destroy_channel(uchan
);
2045 free(app_ctx_provider_name
);
2050 static inline bool name_starts_with(const char *name
, const char *prefix
)
2052 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2054 return !strncmp(name
, prefix
, max_cmp_len
);
2057 /* Perform userspace-specific event name validation */
2058 static int validate_ust_event_name(const char *name
)
2068 * Check name against all internal UST event component namespaces used
2071 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2072 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2073 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2082 * Internal version of cmd_enable_event() with a supplemental
2083 * "internal_event" flag which is used to enable internal events which should
2084 * be hidden from clients. Such events are used in the agent implementation to
2085 * enable the events through which all "agent" events are funeled.
2087 static int _cmd_enable_event(struct ltt_session
*session
,
2088 const struct lttng_domain
*domain
,
2089 char *channel_name
, struct lttng_event
*event
,
2090 char *filter_expression
,
2091 struct lttng_filter_bytecode
*filter
,
2092 struct lttng_event_exclusion
*exclusion
,
2093 int wpipe
, bool internal_event
)
2095 int ret
= 0, channel_created
= 0;
2096 struct lttng_channel
*attr
= NULL
;
2100 assert(channel_name
);
2102 /* If we have a filter, we must have its filter expression */
2103 assert(!(!!filter_expression
^ !!filter
));
2105 /* Normalize event name as a globbing pattern */
2106 strutils_normalize_star_glob_pattern(event
->name
);
2108 /* Normalize exclusion names as globbing patterns */
2112 for (i
= 0; i
< exclusion
->count
; i
++) {
2113 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2115 strutils_normalize_star_glob_pattern(name
);
2119 DBG("Enable event command for event \'%s\'", event
->name
);
2123 switch (domain
->type
) {
2124 case LTTNG_DOMAIN_KERNEL
:
2126 struct ltt_kernel_channel
*kchan
;
2129 * If a non-default channel has been created in the
2130 * session, explicitely require that -c chan_name needs
2133 if (session
->kernel_session
->has_non_default_channel
2134 && channel_name
[0] == '\0') {
2135 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2139 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2140 session
->kernel_session
);
2141 if (kchan
== NULL
) {
2142 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2143 LTTNG_BUFFER_GLOBAL
);
2145 ret
= LTTNG_ERR_FATAL
;
2148 if (lttng_strncpy(attr
->name
, channel_name
,
2149 sizeof(attr
->name
))) {
2150 ret
= LTTNG_ERR_INVALID
;
2154 ret
= cmd_enable_channel_internal(
2155 session
, domain
, attr
, wpipe
);
2156 if (ret
!= LTTNG_OK
) {
2159 channel_created
= 1;
2162 /* Get the newly created kernel channel pointer */
2163 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2164 session
->kernel_session
);
2165 if (kchan
== NULL
) {
2166 /* This sould not happen... */
2167 ret
= LTTNG_ERR_FATAL
;
2171 switch (event
->type
) {
2172 case LTTNG_EVENT_ALL
:
2174 char *filter_expression_a
= NULL
;
2175 struct lttng_filter_bytecode
*filter_a
= NULL
;
2178 * We need to duplicate filter_expression and filter,
2179 * because ownership is passed to first enable
2182 if (filter_expression
) {
2183 filter_expression_a
= strdup(filter_expression
);
2184 if (!filter_expression_a
) {
2185 ret
= LTTNG_ERR_FATAL
;
2190 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2192 free(filter_expression_a
);
2193 ret
= LTTNG_ERR_FATAL
;
2196 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2198 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2199 ret
= event_kernel_enable_event(kchan
, event
,
2200 filter_expression
, filter
);
2201 /* We have passed ownership */
2202 filter_expression
= NULL
;
2204 if (ret
!= LTTNG_OK
) {
2205 if (channel_created
) {
2206 /* Let's not leak a useless channel. */
2207 kernel_destroy_channel(kchan
);
2209 free(filter_expression_a
);
2213 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2214 ret
= event_kernel_enable_event(kchan
, event
,
2215 filter_expression_a
, filter_a
);
2216 /* We have passed ownership */
2217 filter_expression_a
= NULL
;
2219 if (ret
!= LTTNG_OK
) {
2224 case LTTNG_EVENT_PROBE
:
2225 case LTTNG_EVENT_USERSPACE_PROBE
:
2226 case LTTNG_EVENT_FUNCTION
:
2227 case LTTNG_EVENT_FUNCTION_ENTRY
:
2228 case LTTNG_EVENT_TRACEPOINT
:
2229 ret
= event_kernel_enable_event(kchan
, event
,
2230 filter_expression
, filter
);
2231 /* We have passed ownership */
2232 filter_expression
= NULL
;
2234 if (ret
!= LTTNG_OK
) {
2235 if (channel_created
) {
2236 /* Let's not leak a useless channel. */
2237 kernel_destroy_channel(kchan
);
2242 case LTTNG_EVENT_SYSCALL
:
2243 ret
= event_kernel_enable_event(kchan
, event
,
2244 filter_expression
, filter
);
2245 /* We have passed ownership */
2246 filter_expression
= NULL
;
2248 if (ret
!= LTTNG_OK
) {
2253 ret
= LTTNG_ERR_UNK
;
2257 kernel_wait_quiescent();
2260 case LTTNG_DOMAIN_UST
:
2262 struct ltt_ust_channel
*uchan
;
2263 struct ltt_ust_session
*usess
= session
->ust_session
;
2268 * If a non-default channel has been created in the
2269 * session, explicitely require that -c chan_name needs
2272 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2273 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2277 /* Get channel from global UST domain */
2278 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2280 if (uchan
== NULL
) {
2281 /* Create default channel */
2282 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2283 usess
->buffer_type
);
2285 ret
= LTTNG_ERR_FATAL
;
2288 if (lttng_strncpy(attr
->name
, channel_name
,
2289 sizeof(attr
->name
))) {
2290 ret
= LTTNG_ERR_INVALID
;
2294 ret
= cmd_enable_channel_internal(
2295 session
, domain
, attr
, wpipe
);
2296 if (ret
!= LTTNG_OK
) {
2300 /* Get the newly created channel reference back */
2301 uchan
= trace_ust_find_channel_by_name(
2302 usess
->domain_global
.channels
, channel_name
);
2306 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2308 * Don't allow users to add UST events to channels which
2309 * are assigned to a userspace subdomain (JUL, Log4J,
2312 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2316 if (!internal_event
) {
2318 * Ensure the event name is not reserved for internal
2321 ret
= validate_ust_event_name(event
->name
);
2323 WARN("Userspace event name %s failed validation.",
2325 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2330 /* At this point, the session and channel exist on the tracer */
2331 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2332 filter_expression
, filter
, exclusion
,
2334 /* We have passed ownership */
2335 filter_expression
= NULL
;
2338 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2339 goto already_enabled
;
2340 } else if (ret
!= LTTNG_OK
) {
2345 case LTTNG_DOMAIN_LOG4J
:
2346 case LTTNG_DOMAIN_JUL
:
2347 case LTTNG_DOMAIN_PYTHON
:
2349 const char *default_event_name
, *default_chan_name
;
2351 struct lttng_event uevent
;
2352 struct lttng_domain tmp_dom
;
2353 struct ltt_ust_session
*usess
= session
->ust_session
;
2357 if (!agent_tracing_is_enabled()) {
2358 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2359 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2363 agt
= trace_ust_find_agent(usess
, domain
->type
);
2365 agt
= agent_create(domain
->type
);
2367 ret
= LTTNG_ERR_NOMEM
;
2370 agent_add(agt
, usess
->agents
);
2373 /* Create the default tracepoint. */
2374 memset(&uevent
, 0, sizeof(uevent
));
2375 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2376 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2377 default_event_name
= event_get_default_agent_ust_name(
2379 if (!default_event_name
) {
2380 ret
= LTTNG_ERR_FATAL
;
2383 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2384 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2387 * The domain type is changed because we are about to enable the
2388 * default channel and event for the JUL domain that are hardcoded.
2389 * This happens in the UST domain.
2391 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2392 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2394 switch (domain
->type
) {
2395 case LTTNG_DOMAIN_LOG4J
:
2396 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2398 case LTTNG_DOMAIN_JUL
:
2399 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2401 case LTTNG_DOMAIN_PYTHON
:
2402 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2405 /* The switch/case we are in makes this impossible */
2410 char *filter_expression_copy
= NULL
;
2411 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2414 const size_t filter_size
= sizeof(
2415 struct lttng_filter_bytecode
)
2418 filter_copy
= zmalloc(filter_size
);
2420 ret
= LTTNG_ERR_NOMEM
;
2423 memcpy(filter_copy
, filter
, filter_size
);
2425 filter_expression_copy
=
2426 strdup(filter_expression
);
2427 if (!filter_expression
) {
2428 ret
= LTTNG_ERR_NOMEM
;
2431 if (!filter_expression_copy
|| !filter_copy
) {
2432 free(filter_expression_copy
);
2438 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2439 (char *) default_chan_name
,
2440 &uevent
, filter_expression_copy
,
2441 filter_copy
, NULL
, wpipe
);
2444 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2445 goto already_enabled
;
2446 } else if (ret
!= LTTNG_OK
) {
2450 /* The wild card * means that everything should be enabled. */
2451 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2452 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2455 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2459 filter_expression
= NULL
;
2460 if (ret
!= LTTNG_OK
) {
2467 ret
= LTTNG_ERR_UND
;
2475 free(filter_expression
);
2478 channel_attr_destroy(attr
);
2484 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2485 * We own filter, exclusion, and filter_expression.
2487 int cmd_enable_event(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
2491 ssize_t sock_recv_len
;
2492 struct lttng_event
*event
= NULL
;
2493 struct lttng_buffer_view view
;
2494 struct lttng_dynamic_buffer event_buffer
;
2495 struct lttng_event_exclusion
*exclusion
= NULL
;
2496 struct lttng_filter_bytecode
*bytecode
= NULL
;
2497 char *filter_expression
= NULL
;
2499 lttng_dynamic_buffer_init(&event_buffer
);
2500 event_len
= (size_t) cmd_ctx
->lsm
->u
.enable
.length
;
2501 ret
= lttng_dynamic_buffer_set_size(&event_buffer
, event_len
);
2503 ret
= LTTNG_ERR_NOMEM
;
2507 sock_recv_len
= lttcomm_recv_unix_sock(
2508 sock
, event_buffer
.data
, event_len
);
2509 if (sock_recv_len
< 0 || sock_recv_len
!= event_len
) {
2510 ERR("Failed to receive \"enable event\" command payload");
2511 ret
= LTTNG_ERR_INVALID
;
2515 view
= lttng_buffer_view_from_dynamic_buffer(&event_buffer
, 0, -1);
2517 if (lttng_event_create_from_buffer(&view
, &event
,
2518 &exclusion
, &filter_expression
,
2519 &bytecode
, sock
) != event_len
) {
2520 ERR("Invalid event payload received in \"enable event\" command");
2521 ret
= LTTNG_ERR_INVALID
;
2526 * The ownership of the following parameters is transferred to
2527 * _cmd_enable_event:
2529 * - filter_expression,
2533 ret
= _cmd_enable_event(cmd_ctx
->session
,
2534 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
2535 cmd_ctx
->lsm
->u
.enable
.channel_name
, event
,
2536 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2537 filter_expression
= NULL
;
2541 lttng_event_destroy(event
);
2542 lttng_dynamic_buffer_reset(&event_buffer
);
2547 * Enable an event which is internal to LTTng. An internal should
2548 * never be made visible to clients and are immune to checks such as
2551 static int cmd_enable_event_internal(struct ltt_session
*session
,
2552 const struct lttng_domain
*domain
,
2553 char *channel_name
, struct lttng_event
*event
,
2554 char *filter_expression
,
2555 struct lttng_filter_bytecode
*filter
,
2556 struct lttng_event_exclusion
*exclusion
,
2559 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2560 filter_expression
, filter
, exclusion
, wpipe
, true);
2564 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2566 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2567 struct lttng_dynamic_buffer
*buffer
, unsigned int *nb_tracepoints
)
2569 enum lttng_error_code ret_code
;
2571 ssize_t i
, nb_events
= 0;
2572 struct lttng_event
*events
= NULL
;
2575 assert(nb_tracepoints
);
2578 case LTTNG_DOMAIN_KERNEL
:
2579 nb_events
= kernel_list_events(&events
);
2580 if (nb_events
< 0) {
2581 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2585 case LTTNG_DOMAIN_UST
:
2586 nb_events
= ust_app_list_events(&events
);
2587 if (nb_events
< 0) {
2588 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2592 case LTTNG_DOMAIN_LOG4J
:
2593 case LTTNG_DOMAIN_JUL
:
2594 case LTTNG_DOMAIN_PYTHON
:
2595 nb_events
= agent_list_events(&events
, domain
);
2596 if (nb_events
< 0) {
2597 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2602 ret_code
= LTTNG_ERR_UND
;
2607 for (i
= 0; i
< nb_events
; i
++) {
2608 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2611 ret_code
= LTTNG_ERR_NOMEM
;
2616 if (nb_events
> UINT_MAX
) {
2617 ret_code
= LTTNG_ERR_OVERFLOW
;
2621 *nb_tracepoints
= (unsigned int) nb_events
;
2622 ret_code
= LTTNG_OK
;
2629 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2631 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2632 struct lttng_event_field
**fields
)
2635 ssize_t nb_fields
= 0;
2638 case LTTNG_DOMAIN_UST
:
2639 nb_fields
= ust_app_list_event_fields(fields
);
2640 if (nb_fields
< 0) {
2641 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2645 case LTTNG_DOMAIN_KERNEL
:
2646 default: /* fall-through */
2647 ret
= LTTNG_ERR_UND
;
2654 /* Return negative value to differentiate return code */
2658 enum lttng_error_code
cmd_list_syscalls(
2659 struct lttng_dynamic_buffer
*buffer
, unsigned int *nb_syscalls
)
2661 enum lttng_error_code ret_code
;
2662 ssize_t nb_events
, i
;
2664 struct lttng_event
*events
= NULL
;
2667 assert(nb_syscalls
);
2669 nb_events
= syscall_table_list(&events
);
2670 if (nb_events
< 0) {
2671 ret_code
= (enum lttng_error_code
) -nb_events
;
2675 for (i
= 0; i
< nb_events
; i
++) {
2676 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2679 ret_code
= LTTNG_ERR_NOMEM
;
2684 if (nb_events
> UINT_MAX
) {
2685 ret_code
= LTTNG_ERR_OVERFLOW
;
2689 *nb_syscalls
= (uint32_t) nb_events
;
2690 ret_code
= LTTNG_OK
;
2697 * Command LTTNG_START_TRACE processed by the client thread.
2699 * Called with session mutex held.
2701 int cmd_start_trace(struct ltt_session
*session
)
2703 enum lttng_error_code ret
;
2704 unsigned long nb_chan
= 0;
2705 struct ltt_kernel_session
*ksession
;
2706 struct ltt_ust_session
*usess
;
2707 const bool session_rotated_after_last_stop
=
2708 session
->rotated_after_last_stop
;
2709 const bool session_cleared_after_last_stop
=
2710 session
->cleared_after_last_stop
;
2714 /* Ease our life a bit ;) */
2715 ksession
= session
->kernel_session
;
2716 usess
= session
->ust_session
;
2718 /* Is the session already started? */
2719 if (session
->active
) {
2720 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2721 /* Perform nothing */
2725 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2726 !session
->current_trace_chunk
) {
2728 * A rotation was launched while the session was stopped and
2729 * it has not been completed yet. It is not possible to start
2730 * the session since starting the session here would require a
2731 * rotation from "NULL" to a new trace chunk. That rotation
2732 * would overlap with the ongoing rotation, which is not
2735 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2737 ret
= LTTNG_ERR_ROTATION_PENDING
;
2742 * Starting a session without channel is useless since after that it's not
2743 * possible to enable channel thus inform the client.
2745 if (usess
&& usess
->domain_global
.channels
) {
2746 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2749 nb_chan
+= ksession
->channel_count
;
2752 ret
= LTTNG_ERR_NO_CHANNEL
;
2756 session
->active
= 1;
2757 session
->rotated_after_last_stop
= false;
2758 session
->cleared_after_last_stop
= false;
2759 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2760 if (!session
->has_been_started
) {
2761 struct lttng_trace_chunk
*trace_chunk
;
2763 DBG("Creating initial trace chunk of session \"%s\"",
2765 trace_chunk
= session_create_new_trace_chunk(
2766 session
, NULL
, NULL
, NULL
);
2768 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2771 assert(!session
->current_trace_chunk
);
2772 ret
= session_set_trace_chunk(session
, trace_chunk
,
2774 lttng_trace_chunk_put(trace_chunk
);
2776 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2780 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2783 * Rotate existing streams into the new chunk.
2784 * This is a "quiet" rotation has no client has
2785 * explicitly requested this operation.
2787 * There is also no need to wait for the rotation
2788 * to complete as it will happen immediately. No data
2789 * was produced as the session was stopped, so the
2790 * rotation should happen on reception of the command.
2792 ret
= cmd_rotate_session(session
, NULL
, true,
2793 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2794 if (ret
!= LTTNG_OK
) {
2800 /* Kernel tracing */
2801 if (ksession
!= NULL
) {
2802 DBG("Start kernel tracing session %s", session
->name
);
2803 ret
= start_kernel_session(ksession
);
2804 if (ret
!= LTTNG_OK
) {
2809 /* Flag session that trace should start automatically */
2811 int int_ret
= ust_app_start_trace_all(usess
);
2814 ret
= LTTNG_ERR_UST_START_FAIL
;
2820 * Open a packet in every stream of the session to ensure that viewers
2821 * can correctly identify the boundaries of the periods during which
2822 * tracing was active for this session.
2824 ret
= session_open_packets(session
);
2825 if (ret
!= LTTNG_OK
) {
2830 * Clear the flag that indicates that a rotation was done while the
2831 * session was stopped.
2833 session
->rotated_after_last_stop
= false;
2835 if (session
->rotate_timer_period
) {
2836 int int_ret
= timer_session_rotation_schedule_timer_start(
2837 session
, session
->rotate_timer_period
);
2840 ERR("Failed to enable rotate timer");
2841 ret
= LTTNG_ERR_UNK
;
2849 if (ret
== LTTNG_OK
) {
2850 /* Flag this after a successful start. */
2851 session
->has_been_started
|= 1;
2853 session
->active
= 0;
2854 /* Restore initial state on error. */
2855 session
->rotated_after_last_stop
=
2856 session_rotated_after_last_stop
;
2857 session
->cleared_after_last_stop
=
2858 session_cleared_after_last_stop
;
2865 * Command LTTNG_STOP_TRACE processed by the client thread.
2867 int cmd_stop_trace(struct ltt_session
*session
)
2870 struct ltt_kernel_session
*ksession
;
2871 struct ltt_ust_session
*usess
;
2875 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2877 ksession
= session
->kernel_session
;
2878 usess
= session
->ust_session
;
2880 /* Session is not active. Skip everythong and inform the client. */
2881 if (!session
->active
) {
2882 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2886 ret
= stop_kernel_session(ksession
);
2887 if (ret
!= LTTNG_OK
) {
2891 if (usess
&& usess
->active
) {
2892 ret
= ust_app_stop_trace_all(usess
);
2894 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2899 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2901 /* Flag inactive after a successful stop. */
2902 session
->active
= 0;
2910 * Set the base_path of the session only if subdir of a control uris is set.
2911 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2913 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2915 struct lttng_uri
*uris
)
2920 for (i
= 0; i
< nb_uri
; i
++) {
2921 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2922 uris
[i
].subdir
[0] == '\0') {
2923 /* Not interested in these URIs */
2927 if (session
->base_path
!= NULL
) {
2928 free(session
->base_path
);
2929 session
->base_path
= NULL
;
2932 /* Set session base_path */
2933 session
->base_path
= strdup(uris
[i
].subdir
);
2934 if (!session
->base_path
) {
2935 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2936 uris
[i
].subdir
, session
->name
);
2937 ret
= LTTNG_ERR_NOMEM
;
2940 DBG2("Setting base path \"%s\" for session \"%s\"",
2941 session
->base_path
, session
->name
);
2949 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2951 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2952 struct lttng_uri
*uris
)
2955 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2956 struct ltt_ust_session
*usess
= session
->ust_session
;
2962 /* Can't set consumer URI if the session is active. */
2963 if (session
->active
) {
2964 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2969 * Set the session base path if any. This is done inside
2970 * cmd_set_consumer_uri to preserve backward compatibility of the
2971 * previous session creation api vs the session descriptor api.
2973 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2974 if (ret
!= LTTNG_OK
) {
2978 /* Set the "global" consumer URIs */
2979 for (i
= 0; i
< nb_uri
; i
++) {
2980 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2982 if (ret
!= LTTNG_OK
) {
2987 /* Set UST session URIs */
2988 if (session
->ust_session
) {
2989 for (i
= 0; i
< nb_uri
; i
++) {
2990 ret
= add_uri_to_consumer(session
,
2991 session
->ust_session
->consumer
,
2992 &uris
[i
], LTTNG_DOMAIN_UST
);
2993 if (ret
!= LTTNG_OK
) {
2999 /* Set kernel session URIs */
3000 if (session
->kernel_session
) {
3001 for (i
= 0; i
< nb_uri
; i
++) {
3002 ret
= add_uri_to_consumer(session
,
3003 session
->kernel_session
->consumer
,
3004 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3005 if (ret
!= LTTNG_OK
) {
3012 * Make sure to set the session in output mode after we set URI since a
3013 * session can be created without URL (thus flagged in no output mode).
3015 session
->output_traces
= 1;
3017 ksess
->output_traces
= 1;
3021 usess
->output_traces
= 1;
3032 enum lttng_error_code
set_session_output_from_descriptor(
3033 struct ltt_session
*session
,
3034 const struct lttng_session_descriptor
*descriptor
)
3037 enum lttng_error_code ret_code
= LTTNG_OK
;
3038 enum lttng_session_descriptor_type session_type
=
3039 lttng_session_descriptor_get_type(descriptor
);
3040 enum lttng_session_descriptor_output_type output_type
=
3041 lttng_session_descriptor_get_output_type(descriptor
);
3042 struct lttng_uri uris
[2] = {};
3043 size_t uri_count
= 0;
3045 switch (output_type
) {
3046 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3048 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3049 lttng_session_descriptor_get_local_output_uri(descriptor
,
3053 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3054 lttng_session_descriptor_get_network_output_uris(descriptor
,
3055 &uris
[0], &uris
[1]);
3059 ret_code
= LTTNG_ERR_INVALID
;
3063 switch (session_type
) {
3064 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3066 struct snapshot_output
*new_output
= NULL
;
3068 new_output
= snapshot_output_alloc();
3070 ret_code
= LTTNG_ERR_NOMEM
;
3074 ret
= snapshot_output_init_with_uri(session
,
3075 DEFAULT_SNAPSHOT_MAX_SIZE
,
3076 NULL
, uris
, uri_count
, session
->consumer
,
3077 new_output
, &session
->snapshot
);
3079 ret_code
= (ret
== -ENOMEM
) ?
3080 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3081 snapshot_output_destroy(new_output
);
3084 snapshot_add_output(&session
->snapshot
, new_output
);
3087 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3088 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3090 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
3094 ret_code
= LTTNG_ERR_INVALID
;
3102 enum lttng_error_code
cmd_create_session_from_descriptor(
3103 struct lttng_session_descriptor
*descriptor
,
3104 const lttng_sock_cred
*creds
,
3105 const char *home_path
)
3108 enum lttng_error_code ret_code
;
3109 const char *session_name
;
3110 struct ltt_session
*new_session
= NULL
;
3111 enum lttng_session_descriptor_status descriptor_status
;
3113 session_lock_list();
3115 if (*home_path
!= '/') {
3116 ERR("Home path provided by client is not absolute");
3117 ret_code
= LTTNG_ERR_INVALID
;
3122 descriptor_status
= lttng_session_descriptor_get_session_name(
3123 descriptor
, &session_name
);
3124 switch (descriptor_status
) {
3125 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3127 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3128 session_name
= NULL
;
3131 ret_code
= LTTNG_ERR_INVALID
;
3135 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3137 if (ret_code
!= LTTNG_OK
) {
3141 if (!session_name
) {
3142 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3145 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3150 if (!lttng_session_descriptor_is_output_destination_initialized(
3153 * Only include the session's creation time in the output
3154 * destination if the name of the session itself was
3155 * not auto-generated.
3157 ret_code
= lttng_session_descriptor_set_default_output(
3159 session_name
? &new_session
->creation_time
: NULL
,
3161 if (ret_code
!= LTTNG_OK
) {
3165 new_session
->has_user_specified_directory
=
3166 lttng_session_descriptor_has_output_directory(
3170 switch (lttng_session_descriptor_get_type(descriptor
)) {
3171 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3172 new_session
->snapshot_mode
= 1;
3174 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3175 new_session
->live_timer
=
3176 lttng_session_descriptor_live_get_timer_interval(
3183 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3184 if (ret_code
!= LTTNG_OK
) {
3187 new_session
->consumer
->enabled
= 1;
3188 ret_code
= LTTNG_OK
;
3190 /* Release reference provided by the session_create function. */
3191 session_put(new_session
);
3192 if (ret_code
!= LTTNG_OK
&& new_session
) {
3193 /* Release the global reference on error. */
3194 session_destroy(new_session
);
3196 session_unlock_list();
3200 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3201 struct lttng_session_descriptor
**return_descriptor
)
3204 size_t payload_size
;
3205 struct lttng_dynamic_buffer payload
;
3206 struct lttng_buffer_view home_dir_view
;
3207 struct lttng_buffer_view session_descriptor_view
;
3208 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3209 enum lttng_error_code ret_code
;
3211 lttng_dynamic_buffer_init(&payload
);
3212 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
3214 ret_code
= LTTNG_ERR_INVALID
;
3217 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
3218 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3219 ret_code
= LTTNG_ERR_INVALID
;
3223 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
3224 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
3225 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3227 ret_code
= LTTNG_ERR_NOMEM
;
3231 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3233 ERR("Reception of session descriptor failed, aborting.");
3234 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3238 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3241 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3242 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3244 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3245 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3247 ret
= lttng_session_descriptor_create_from_buffer(
3248 &session_descriptor_view
, &session_descriptor
);
3250 ERR("Failed to create session descriptor from payload of \"create session\" command");
3251 ret_code
= LTTNG_ERR_INVALID
;
3256 * Sets the descriptor's auto-generated properties (name, output) if
3259 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3261 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3262 if (ret_code
!= LTTNG_OK
) {
3266 ret_code
= LTTNG_OK
;
3267 *return_descriptor
= session_descriptor
;
3268 session_descriptor
= NULL
;
3270 lttng_dynamic_buffer_reset(&payload
);
3271 lttng_session_descriptor_destroy(session_descriptor
);
3276 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3277 void *_reply_context
)
3281 const struct cmd_destroy_session_reply_context
*reply_context
=
3283 struct lttng_dynamic_buffer payload
;
3284 struct lttcomm_session_destroy_command_header cmd_header
;
3285 struct lttng_trace_archive_location
*location
= NULL
;
3286 struct lttcomm_lttng_msg llm
= {
3287 .cmd_type
= LTTNG_DESTROY_SESSION
,
3288 .ret_code
= reply_context
->destruction_status
,
3291 sizeof(struct lttcomm_session_destroy_command_header
),
3294 size_t payload_size_before_location
;
3296 lttng_dynamic_buffer_init(&payload
);
3298 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3300 ERR("Failed to append session destruction message");
3304 cmd_header
.rotation_state
=
3305 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3306 session
->rotation_state
:
3307 LTTNG_ROTATION_STATE_NO_ROTATION
);
3308 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3309 sizeof(cmd_header
));
3311 ERR("Failed to append session destruction command header");
3315 if (!reply_context
->implicit_rotation_on_destroy
) {
3316 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3320 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3321 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3326 location
= session_get_trace_archive_location(session
);
3328 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3333 payload_size_before_location
= payload
.size
;
3334 comm_ret
= lttng_trace_archive_location_serialize(location
,
3336 lttng_trace_archive_location_put(location
);
3338 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3342 /* Update the message to indicate the location's length. */
3343 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3344 payload
.size
- payload_size_before_location
;
3346 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3347 payload
.data
, payload
.size
);
3348 if (comm_ret
!= (ssize_t
) payload
.size
) {
3349 ERR("Failed to send result of the destruction of session \"%s\" to client",
3353 ret
= close(reply_context
->reply_sock_fd
);
3355 PERROR("Failed to close client socket in deferred session destroy reply");
3357 lttng_dynamic_buffer_reset(&payload
);
3358 free(_reply_context
);
3362 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3364 * Called with session lock held.
3366 int cmd_destroy_session(struct ltt_session
*session
,
3367 struct notification_thread_handle
*notification_thread_handle
,
3371 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3372 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3375 reply_context
= zmalloc(sizeof(*reply_context
));
3376 if (!reply_context
) {
3377 ret
= LTTNG_ERR_NOMEM
;
3380 reply_context
->reply_sock_fd
= *sock_fd
;
3386 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3388 if (session
->active
) {
3389 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3391 ret
= cmd_stop_trace(session
);
3392 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3393 /* Carry on with the destruction of the session. */
3394 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3395 session
->name
, lttng_strerror(-ret
));
3396 destruction_last_error
= ret
;
3400 if (session
->rotation_schedule_timer_enabled
) {
3401 if (timer_session_rotation_schedule_timer_stop(
3403 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3405 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3409 if (session
->rotate_size
) {
3410 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3411 session
->rotate_size
= 0;
3414 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3416 * Perform a last rotation on destruction if rotations have
3417 * occurred during the session's lifetime.
3419 ret
= cmd_rotate_session(session
, NULL
, false,
3420 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3421 if (ret
!= LTTNG_OK
) {
3422 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3423 session
->name
, lttng_strerror(-ret
));
3424 destruction_last_error
= -ret
;
3426 if (reply_context
) {
3427 reply_context
->implicit_rotation_on_destroy
= true;
3429 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3431 * The user has not triggered a session rotation. However, to
3432 * ensure all data has been consumed, the session is rotated
3433 * to a 'null' trace chunk before it is destroyed.
3435 * This is a "quiet" rotation meaning that no notification is
3436 * emitted and no renaming of the current trace chunk takes
3439 ret
= cmd_rotate_session(session
, NULL
, true,
3440 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3442 * Rotation operations may not be supported by the kernel
3443 * tracer. Hence, do not consider this implicit rotation as
3444 * a session destruction error. The library has already stopped
3445 * the session and waited for pending data; there is nothing
3446 * left to do but complete the destruction of the session.
3448 if (ret
!= LTTNG_OK
&&
3449 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3450 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3451 session
->name
, lttng_strerror(ret
));
3452 destruction_last_error
= -ret
;
3456 if (session
->shm_path
[0]) {
3458 * When a session is created with an explicit shm_path,
3459 * the consumer daemon will create its shared memory files
3460 * at that location and will *not* unlink them. This is normal
3461 * as the intention of that feature is to make it possible
3462 * to retrieve the content of those files should a crash occur.
3464 * To ensure the content of those files can be used, the
3465 * sessiond daemon will replicate the content of the metadata
3466 * cache in a metadata file.
3468 * On clean-up, it is expected that the consumer daemon will
3469 * unlink the shared memory files and that the session daemon
3470 * will unlink the metadata file. Then, the session's directory
3471 * in the shm path can be removed.
3473 * Unfortunately, a flaw in the design of the sessiond's and
3474 * consumerd's tear down of channels makes it impossible to
3475 * determine when the sessiond _and_ the consumerd have both
3476 * destroyed their representation of a channel. For one, the
3477 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3478 * callbacks in both daemons.
3480 * However, it is also impossible for the sessiond to know when
3481 * the consumer daemon is done destroying its channel(s) since
3482 * it occurs as a reaction to the closing of the channel's file
3483 * descriptor. There is no resulting communication initiated
3484 * from the consumerd to the sessiond to confirm that the
3485 * operation is completed (and was successful).
3487 * Until this is all fixed, the session daemon checks for the
3488 * removal of the session's shm path which makes it possible
3489 * to safely advertise a session as having been destroyed.
3491 * Prior to this fix, it was not possible to reliably save
3492 * a session making use of the --shm-path option, destroy it,
3493 * and load it again. This is because the creation of the
3494 * session would fail upon seeing the session's shm path
3495 * already in existence.
3497 * Note that none of the error paths in the check for the
3498 * directory's existence return an error. This is normal
3499 * as there isn't much that can be done. The session will
3500 * be destroyed properly, except that we can't offer the
3501 * guarantee that the same session can be re-created.
3503 current_completion_handler
= &destroy_completion_handler
.handler
;
3504 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3506 sizeof(destroy_completion_handler
.shm_path
));
3511 * The session is destroyed. However, note that the command context
3512 * still holds a reference to the session, thus delaying its destruction
3513 * _at least_ up to the point when that reference is released.
3515 session_destroy(session
);
3516 if (reply_context
) {
3517 reply_context
->destruction_status
= destruction_last_error
;
3518 ret
= session_add_destroy_notifier(session
,
3519 cmd_destroy_session_reply
,
3520 (void *) reply_context
);
3522 ret
= LTTNG_ERR_FATAL
;
3534 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3536 int cmd_register_consumer(struct ltt_session
*session
,
3537 enum lttng_domain_type domain
, const char *sock_path
,
3538 struct consumer_data
*cdata
)
3541 struct consumer_socket
*socket
= NULL
;
3548 case LTTNG_DOMAIN_KERNEL
:
3550 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3554 /* Can't register a consumer if there is already one */
3555 if (ksess
->consumer_fds_sent
!= 0) {
3556 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3560 sock
= lttcomm_connect_unix_sock(sock_path
);
3562 ret
= LTTNG_ERR_CONNECT_FAIL
;
3565 cdata
->cmd_sock
= sock
;
3567 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3568 if (socket
== NULL
) {
3571 PERROR("close register consumer");
3573 cdata
->cmd_sock
= -1;
3574 ret
= LTTNG_ERR_FATAL
;
3578 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3579 if (socket
->lock
== NULL
) {
3580 PERROR("zmalloc pthread mutex");
3581 ret
= LTTNG_ERR_FATAL
;
3584 pthread_mutex_init(socket
->lock
, NULL
);
3585 socket
->registered
= 1;
3588 consumer_add_socket(socket
, ksess
->consumer
);
3591 pthread_mutex_lock(&cdata
->pid_mutex
);
3593 pthread_mutex_unlock(&cdata
->pid_mutex
);
3598 /* TODO: Userspace tracing */
3599 ret
= LTTNG_ERR_UND
;
3607 consumer_destroy_socket(socket
);
3613 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3615 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3616 struct lttng_domain
**domains
)
3621 struct lttng_ht_iter iter
;
3623 if (session
->kernel_session
!= NULL
) {
3624 DBG3("Listing domains found kernel domain");
3628 if (session
->ust_session
!= NULL
) {
3629 DBG3("Listing domains found UST global domain");
3633 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3635 if (agt
->being_used
) {
3646 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3647 if (*domains
== NULL
) {
3648 ret
= LTTNG_ERR_FATAL
;
3652 if (session
->kernel_session
!= NULL
) {
3653 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3655 /* Kernel session buffer type is always GLOBAL */
3656 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3661 if (session
->ust_session
!= NULL
) {
3662 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3663 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3667 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3669 if (agt
->being_used
) {
3670 (*domains
)[index
].type
= agt
->domain
;
3671 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3681 /* Return negative value to differentiate return code */
3687 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3689 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3690 struct ltt_session
*session
,
3691 struct lttng_dynamic_buffer
*buffer
,
3692 uint32_t *nb_channel
)
3701 DBG("Listing channels for session %s", session
->name
);
3704 case LTTNG_DOMAIN_KERNEL
:
3706 /* Kernel channels */
3707 struct ltt_kernel_channel
*kchan
;
3708 if (session
->kernel_session
!= NULL
) {
3709 cds_list_for_each_entry(kchan
,
3710 &session
->kernel_session
->channel_list
.head
, list
) {
3711 uint64_t discarded_events
, lost_packets
;
3712 struct lttng_channel_extended
*extended
;
3714 extended
= (struct lttng_channel_extended
*)
3715 kchan
->channel
->attr
.extended
.ptr
;
3717 ret
= get_kernel_runtime_stats(session
, kchan
,
3718 &discarded_events
, &lost_packets
);
3724 * Update the discarded_events and lost_packets
3725 * count for the channel
3727 extended
->discarded_events
= discarded_events
;
3728 extended
->lost_packets
= lost_packets
;
3730 ret
= lttng_channel_serialize(
3731 kchan
->channel
, buffer
);
3742 case LTTNG_DOMAIN_UST
:
3744 struct lttng_ht_iter iter
;
3745 struct ltt_ust_channel
*uchan
;
3748 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3749 &iter
.iter
, uchan
, node
.node
) {
3750 uint64_t discarded_events
= 0, lost_packets
= 0;
3751 struct lttng_channel
*channel
= NULL
;
3752 struct lttng_channel_extended
*extended
;
3754 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3760 extended
= (struct lttng_channel_extended
*)
3761 channel
->attr
.extended
.ptr
;
3763 ret
= get_ust_runtime_stats(session
, uchan
,
3764 &discarded_events
, &lost_packets
);
3766 lttng_channel_destroy(channel
);
3770 extended
->discarded_events
= discarded_events
;
3771 extended
->lost_packets
= lost_packets
;
3773 ret
= lttng_channel_serialize(channel
, buffer
);
3791 return LTTNG_ERR_FATAL
;
3799 * Command LTTNG_LIST_EVENTS processed by the client thread.
3801 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3802 struct ltt_session
*session
,
3804 struct lttng_dynamic_buffer
*payload
,
3805 unsigned int *nb_events
)
3807 enum lttng_error_code ret_code
= LTTNG_OK
;
3812 case LTTNG_DOMAIN_KERNEL
:
3813 if (session
->kernel_session
!= NULL
) {
3814 ret_code
= list_lttng_kernel_events(channel_name
,
3815 session
->kernel_session
, payload
, nb_events
);
3818 case LTTNG_DOMAIN_UST
:
3820 if (session
->ust_session
!= NULL
) {
3821 ret_code
= list_lttng_ust_global_events(channel_name
,
3822 &session
->ust_session
->domain_global
,
3823 payload
, nb_events
);
3827 case LTTNG_DOMAIN_LOG4J
:
3828 case LTTNG_DOMAIN_JUL
:
3829 case LTTNG_DOMAIN_PYTHON
:
3830 if (session
->ust_session
) {
3831 struct lttng_ht_iter iter
;
3835 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3836 &iter
.iter
, agt
, node
.node
) {
3837 if (agt
->domain
== domain
) {
3838 ret_code
= list_lttng_agent_events(
3839 agt
, payload
, nb_events
);
3847 ret_code
= LTTNG_ERR_UND
;
3855 * Using the session list, filled a lttng_session array to send back to the
3856 * client for session listing.
3858 * The session list lock MUST be acquired before calling this function. Use
3859 * session_lock_list() and session_unlock_list().
3861 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3862 size_t session_count
, uid_t uid
, gid_t gid
)
3866 struct ltt_session
*session
;
3867 struct ltt_session_list
*list
= session_get_list();
3868 struct lttng_session_extended
*extended
=
3869 (typeof(extended
)) (&sessions
[session_count
]);
3871 DBG("Getting all available session for UID %d GID %d",
3874 * Iterate over session list and append data after the control struct in
3877 cds_list_for_each_entry(session
, &list
->head
, list
) {
3878 if (!session_get(session
)) {
3882 * Only list the sessions the user can control.
3884 if (!session_access_ok(session
, uid
, gid
) ||
3885 session
->destroyed
) {
3886 session_put(session
);
3890 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3891 struct ltt_ust_session
*usess
= session
->ust_session
;
3893 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3894 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3895 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3896 ret
= build_network_session_path(sessions
[i
].path
,
3897 sizeof(sessions
[i
].path
), session
);
3899 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3900 session
->consumer
->dst
.session_root_path
);
3903 PERROR("snprintf session path");
3904 session_put(session
);
3908 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3909 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3910 sessions
[i
].enabled
= session
->active
;
3911 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3912 sessions
[i
].live_timer_interval
= session
->live_timer
;
3913 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3914 extended
[i
].creation_time
.is_set
= 1;
3916 session_put(session
);
3921 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3922 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3924 int cmd_data_pending(struct ltt_session
*session
)
3927 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3928 struct ltt_ust_session
*usess
= session
->ust_session
;
3932 DBG("Data pending for session %s", session
->name
);
3934 /* Session MUST be stopped to ask for data availability. */
3935 if (session
->active
) {
3936 ret
= LTTNG_ERR_SESSION_STARTED
;
3940 * If stopped, just make sure we've started before else the above call
3941 * will always send that there is data pending.
3943 * The consumer assumes that when the data pending command is received,
3944 * the trace has been started before or else no output data is written
3945 * by the streams which is a condition for data pending. So, this is
3946 * *VERY* important that we don't ask the consumer before a start
3949 if (!session
->has_been_started
) {
3955 /* A rotation is still pending, we have to wait. */
3956 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3957 DBG("Rotate still pending for session %s", session
->name
);
3962 if (ksess
&& ksess
->consumer
) {
3963 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3965 /* Data is still being extracted for the kernel. */
3970 if (usess
&& usess
->consumer
) {
3971 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3973 /* Data is still being extracted for the kernel. */
3978 /* Data is ready to be read by a viewer */
3986 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3988 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3990 int cmd_snapshot_add_output(struct ltt_session
*session
,
3991 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3994 struct snapshot_output
*new_output
;
3999 DBG("Cmd snapshot add output for session %s", session
->name
);
4002 * Can't create an output if the session is not set in no-output mode.
4004 if (session
->output_traces
) {
4005 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4009 if (session
->has_non_mmap_channel
) {
4010 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4014 /* Only one output is allowed until we have the "tee" feature. */
4015 if (session
->snapshot
.nb_output
== 1) {
4016 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4020 new_output
= snapshot_output_alloc();
4022 ret
= LTTNG_ERR_NOMEM
;
4026 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4027 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4028 &session
->snapshot
);
4030 if (ret
== -ENOMEM
) {
4031 ret
= LTTNG_ERR_NOMEM
;
4033 ret
= LTTNG_ERR_INVALID
;
4039 snapshot_add_output(&session
->snapshot
, new_output
);
4041 *id
= new_output
->id
;
4048 snapshot_output_destroy(new_output
);
4054 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4056 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4058 int cmd_snapshot_del_output(struct ltt_session
*session
,
4059 const struct lttng_snapshot_output
*output
)
4062 struct snapshot_output
*sout
= NULL
;
4070 * Permission denied to create an output if the session is not
4071 * set in no output mode.
4073 if (session
->output_traces
) {
4074 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4079 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4081 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4082 } else if (*output
->name
!= '\0') {
4083 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4085 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4088 ret
= LTTNG_ERR_INVALID
;
4092 snapshot_delete_output(&session
->snapshot
, sout
);
4093 snapshot_output_destroy(sout
);
4102 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4104 * If no output is available, outputs is untouched and 0 is returned.
4106 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4108 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4109 struct lttng_snapshot_output
**outputs
)
4112 struct lttng_snapshot_output
*list
= NULL
;
4113 struct lttng_ht_iter iter
;
4114 struct snapshot_output
*output
;
4119 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4122 * Permission denied to create an output if the session is not
4123 * set in no output mode.
4125 if (session
->output_traces
) {
4126 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4130 if (session
->snapshot
.nb_output
== 0) {
4135 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4137 ret
= -LTTNG_ERR_NOMEM
;
4141 /* Copy list from session to the new list object. */
4143 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4144 output
, node
.node
) {
4145 assert(output
->consumer
);
4146 list
[idx
].id
= output
->id
;
4147 list
[idx
].max_size
= output
->max_size
;
4148 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4149 sizeof(list
[idx
].name
))) {
4150 ret
= -LTTNG_ERR_INVALID
;
4153 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4154 if (lttng_strncpy(list
[idx
].ctrl_url
,
4155 output
->consumer
->dst
.session_root_path
,
4156 sizeof(list
[idx
].ctrl_url
))) {
4157 ret
= -LTTNG_ERR_INVALID
;
4162 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4163 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4165 ret
= -LTTNG_ERR_NOMEM
;
4170 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4171 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4173 ret
= -LTTNG_ERR_NOMEM
;
4182 ret
= session
->snapshot
.nb_output
;
4191 * Check if we can regenerate the metadata for this session.
4192 * Only kernel, UST per-uid and non-live sessions are supported.
4194 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4197 int check_regenerate_metadata_support(struct ltt_session
*session
)
4203 if (session
->live_timer
!= 0) {
4204 ret
= LTTNG_ERR_LIVE_SESSION
;
4207 if (!session
->active
) {
4208 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4211 if (session
->ust_session
) {
4212 switch (session
->ust_session
->buffer_type
) {
4213 case LTTNG_BUFFER_PER_UID
:
4215 case LTTNG_BUFFER_PER_PID
:
4216 ret
= LTTNG_ERR_PER_PID_SESSION
;
4220 ret
= LTTNG_ERR_UNK
;
4224 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4225 session
->consumer
->relay_minor_version
< 8) {
4226 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4236 int clear_metadata_file(int fd
)
4241 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4242 if (lseek_ret
< 0) {
4248 ret
= ftruncate(fd
, 0);
4250 PERROR("ftruncate");
4259 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4262 struct buffer_reg_uid
*uid_reg
= NULL
;
4263 struct buffer_reg_session
*session_reg
= NULL
;
4266 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4267 struct ust_registry_session
*registry
;
4268 struct ust_registry_channel
*chan
;
4269 struct lttng_ht_iter iter_chan
;
4271 session_reg
= uid_reg
->registry
;
4272 registry
= session_reg
->reg
.ust
;
4274 pthread_mutex_lock(®istry
->lock
);
4275 registry
->metadata_len_sent
= 0;
4276 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4277 registry
->metadata_len
= 0;
4278 registry
->metadata_version
++;
4279 if (registry
->metadata_fd
> 0) {
4280 /* Clear the metadata file's content. */
4281 ret
= clear_metadata_file(registry
->metadata_fd
);
4283 pthread_mutex_unlock(®istry
->lock
);
4288 ret
= ust_metadata_session_statedump(registry
, NULL
,
4289 registry
->major
, registry
->minor
);
4291 pthread_mutex_unlock(®istry
->lock
);
4292 ERR("Failed to generate session metadata (err = %d)",
4296 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4298 struct ust_registry_event
*event
;
4299 struct lttng_ht_iter iter_event
;
4301 ret
= ust_metadata_channel_statedump(registry
, chan
);
4303 pthread_mutex_unlock(®istry
->lock
);
4304 ERR("Failed to generate channel metadata "
4308 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4310 ret
= ust_metadata_event_statedump(registry
,
4313 pthread_mutex_unlock(®istry
->lock
);
4314 ERR("Failed to generate event metadata "
4320 pthread_mutex_unlock(®istry
->lock
);
4329 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4331 * Ask the consumer to truncate the existing metadata file(s) and
4332 * then regenerate the metadata. Live and per-pid sessions are not
4333 * supported and return an error.
4335 * Return 0 on success or else a LTTNG_ERR code.
4337 int cmd_regenerate_metadata(struct ltt_session
*session
)
4343 ret
= check_regenerate_metadata_support(session
);
4348 if (session
->kernel_session
) {
4349 ret
= kernctl_session_regenerate_metadata(
4350 session
->kernel_session
->fd
);
4352 ERR("Failed to regenerate the kernel metadata");
4357 if (session
->ust_session
) {
4358 ret
= ust_regenerate_metadata(session
->ust_session
);
4360 ERR("Failed to regenerate the UST metadata");
4364 DBG("Cmd metadata regenerate for session %s", session
->name
);
4372 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4374 * Ask the tracer to regenerate a new statedump.
4376 * Return 0 on success or else a LTTNG_ERR code.
4378 int cmd_regenerate_statedump(struct ltt_session
*session
)
4384 if (!session
->active
) {
4385 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4389 if (session
->kernel_session
) {
4390 ret
= kernctl_session_regenerate_statedump(
4391 session
->kernel_session
->fd
);
4393 * Currently, the statedump in kernel can only fail if out
4397 if (ret
== -ENOMEM
) {
4398 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4400 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4402 ERR("Failed to regenerate the kernel statedump");
4407 if (session
->ust_session
) {
4408 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4410 * Currently, the statedump in UST always returns 0.
4413 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4414 ERR("Failed to regenerate the UST statedump");
4418 DBG("Cmd regenerate statedump for session %s", session
->name
);
4425 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4426 struct notification_thread_handle
*notification_thread
)
4430 ssize_t sock_recv_len
;
4431 struct lttng_trigger
*trigger
= NULL
;
4432 struct lttng_buffer_view view
;
4433 struct lttng_dynamic_buffer trigger_buffer
;
4435 lttng_dynamic_buffer_init(&trigger_buffer
);
4436 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4437 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4439 ret
= LTTNG_ERR_NOMEM
;
4443 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4445 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4446 ERR("Failed to receive \"register trigger\" command payload");
4447 /* TODO: should this be a new error enum ? */
4448 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4452 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4453 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4455 ERR("Invalid trigger payload received in \"register trigger\" command");
4456 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4460 ret
= notification_thread_command_register_trigger(notification_thread
,
4462 /* Ownership of trigger was transferred. */
4465 lttng_trigger_destroy(trigger
);
4466 lttng_dynamic_buffer_reset(&trigger_buffer
);
4470 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4471 struct notification_thread_handle
*notification_thread
)
4475 ssize_t sock_recv_len
;
4476 struct lttng_trigger
*trigger
= NULL
;
4477 struct lttng_buffer_view view
;
4478 struct lttng_dynamic_buffer trigger_buffer
;
4480 lttng_dynamic_buffer_init(&trigger_buffer
);
4481 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4482 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4484 ret
= LTTNG_ERR_NOMEM
;
4488 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4490 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4491 ERR("Failed to receive \"unregister trigger\" command payload");
4492 /* TODO: should this be a new error enum ? */
4493 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4497 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4498 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4500 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4501 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4505 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4508 lttng_trigger_destroy(trigger
);
4509 lttng_dynamic_buffer_reset(&trigger_buffer
);
4514 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4515 * snapshot output is *not* set with a remote destination.
4517 * Return LTTNG_OK on success or a LTTNG_ERR code.
4519 static enum lttng_error_code
set_relayd_for_snapshot(
4520 struct consumer_output
*output
,
4521 const struct ltt_session
*session
)
4523 enum lttng_error_code status
= LTTNG_OK
;
4524 struct lttng_ht_iter iter
;
4525 struct consumer_socket
*socket
;
4526 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4527 const char *base_path
;
4532 DBG2("Set relayd object from snapshot output");
4534 if (session
->current_trace_chunk
) {
4535 enum lttng_trace_chunk_status chunk_status
=
4536 lttng_trace_chunk_get_id(
4537 session
->current_trace_chunk
,
4538 ¤t_chunk_id
.value
);
4540 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4541 current_chunk_id
.is_set
= true;
4543 ERR("Failed to get current trace chunk id");
4544 status
= LTTNG_ERR_UNK
;
4549 /* Ignore if snapshot consumer output is not network. */
4550 if (output
->type
!= CONSUMER_DST_NET
) {
4555 * The snapshot record URI base path overrides the session
4558 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4559 base_path
= output
->dst
.net
.control
.subdir
;
4561 base_path
= session
->base_path
;
4565 * For each consumer socket, create and send the relayd object of the
4569 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4570 socket
, node
.node
) {
4571 pthread_mutex_lock(socket
->lock
);
4572 status
= send_consumer_relayd_sockets(0, session
->id
,
4574 session
->name
, session
->hostname
,
4576 session
->live_timer
,
4577 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4578 session
->creation_time
,
4579 session
->name_contains_creation_time
);
4580 pthread_mutex_unlock(socket
->lock
);
4581 if (status
!= LTTNG_OK
) {
4593 * Record a kernel snapshot.
4595 * Return LTTNG_OK on success or a LTTNG_ERR code.
4597 static enum lttng_error_code
record_kernel_snapshot(
4598 struct ltt_kernel_session
*ksess
,
4599 const struct consumer_output
*output
,
4600 const struct ltt_session
*session
,
4601 int wait
, uint64_t nb_packets_per_stream
)
4603 enum lttng_error_code status
;
4609 status
= kernel_snapshot_record(
4610 ksess
, output
, wait
, nb_packets_per_stream
);
4615 * Record a UST snapshot.
4617 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4619 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4620 const struct consumer_output
*output
,
4621 const struct ltt_session
*session
,
4622 int wait
, uint64_t nb_packets_per_stream
)
4624 enum lttng_error_code status
;
4630 status
= ust_app_snapshot_record(
4631 usess
, output
, wait
, nb_packets_per_stream
);
4636 uint64_t get_session_size_one_more_packet_per_stream(
4637 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4639 uint64_t tot_size
= 0;
4641 if (session
->kernel_session
) {
4642 struct ltt_kernel_channel
*chan
;
4643 const struct ltt_kernel_session
*ksess
=
4644 session
->kernel_session
;
4646 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4647 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4649 * Don't take channel into account if we
4650 * already grab all its packets.
4654 tot_size
+= chan
->channel
->attr
.subbuf_size
4655 * chan
->stream_count
;
4659 if (session
->ust_session
) {
4660 const struct ltt_ust_session
*usess
= session
->ust_session
;
4662 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4670 * Calculate the number of packets we can grab from each stream that
4671 * fits within the overall snapshot max size.
4673 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4674 * the number of packets per stream.
4676 * TODO: this approach is not perfect: we consider the worse case
4677 * (packet filling the sub-buffers) as an upper bound, but we could do
4678 * better if we do this calculation while we actually grab the packet
4679 * content: we would know how much padding we don't actually store into
4682 * This algorithm is currently bounded by the number of packets per
4685 * Since we call this algorithm before actually grabbing the data, it's
4686 * an approximation: for instance, applications could appear/disappear
4687 * in between this call and actually grabbing data.
4690 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4694 uint64_t cur_nb_packets
= 0;
4697 return 0; /* Infinite */
4700 size_left
= max_size
;
4702 uint64_t one_more_packet_tot_size
;
4704 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4705 session
, cur_nb_packets
);
4706 if (!one_more_packet_tot_size
) {
4707 /* We are already grabbing all packets. */
4710 size_left
-= one_more_packet_tot_size
;
4711 if (size_left
< 0) {
4716 if (!cur_nb_packets
&& size_left
!= max_size
) {
4717 /* Not enough room to grab one packet of each stream, error. */
4720 return cur_nb_packets
;
4724 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4725 const struct snapshot_output
*snapshot_output
, int wait
)
4727 int64_t nb_packets_per_stream
;
4728 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4730 enum lttng_error_code ret_code
= LTTNG_OK
;
4731 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4732 struct consumer_output
*original_ust_consumer_output
= NULL
;
4733 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4734 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4735 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4737 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4739 snapshot_output
->name
,
4740 snapshot_output
->datetime
,
4741 snapshot_output
->nb_snapshot
);
4742 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4743 ERR("Failed to format snapshot name");
4744 ret_code
= LTTNG_ERR_INVALID
;
4747 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4748 snapshot_output
->name
, session
->name
,
4749 snapshot_chunk_name
);
4750 if (!session
->kernel_session
&& !session
->ust_session
) {
4751 ERR("Failed to record snapshot as no channels exist");
4752 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4756 if (session
->kernel_session
) {
4757 original_kernel_consumer_output
=
4758 session
->kernel_session
->consumer
;
4759 snapshot_kernel_consumer_output
=
4760 consumer_copy_output(snapshot_output
->consumer
);
4761 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
4762 snapshot_chunk_name
);
4764 /* Copy the original domain subdir. */
4765 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
4766 original_kernel_consumer_output
->domain_subdir
);
4768 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4769 original_kernel_consumer_output
);
4771 ERR("Failed to copy consumer sockets from snapshot output configuration");
4772 ret_code
= LTTNG_ERR_NOMEM
;
4775 ret_code
= set_relayd_for_snapshot(
4776 snapshot_kernel_consumer_output
, session
);
4777 if (ret_code
!= LTTNG_OK
) {
4778 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4781 session
->kernel_session
->consumer
=
4782 snapshot_kernel_consumer_output
;
4784 if (session
->ust_session
) {
4785 original_ust_consumer_output
= session
->ust_session
->consumer
;
4786 snapshot_ust_consumer_output
=
4787 consumer_copy_output(snapshot_output
->consumer
);
4788 strcpy(snapshot_ust_consumer_output
->chunk_path
,
4789 snapshot_chunk_name
);
4791 /* Copy the original domain subdir. */
4792 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
4793 original_ust_consumer_output
->domain_subdir
);
4795 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4796 original_ust_consumer_output
);
4798 ERR("Failed to copy consumer sockets from snapshot output configuration");
4799 ret_code
= LTTNG_ERR_NOMEM
;
4802 ret_code
= set_relayd_for_snapshot(
4803 snapshot_ust_consumer_output
, session
);
4804 if (ret_code
!= LTTNG_OK
) {
4805 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4808 session
->ust_session
->consumer
=
4809 snapshot_ust_consumer_output
;
4812 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4813 snapshot_kernel_consumer_output
?:
4814 snapshot_ust_consumer_output
,
4815 consumer_output_get_base_path(
4816 snapshot_output
->consumer
),
4817 snapshot_chunk_name
);
4818 if (!snapshot_trace_chunk
) {
4819 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4821 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4824 assert(!session
->current_trace_chunk
);
4825 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4826 lttng_trace_chunk_put(snapshot_trace_chunk
);
4827 snapshot_trace_chunk
= NULL
;
4829 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4831 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4835 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4836 snapshot_output
->max_size
);
4837 if (nb_packets_per_stream
< 0) {
4838 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4839 goto error_close_trace_chunk
;
4842 if (session
->kernel_session
) {
4843 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4844 snapshot_kernel_consumer_output
, session
,
4845 wait
, nb_packets_per_stream
);
4846 if (ret_code
!= LTTNG_OK
) {
4847 goto error_close_trace_chunk
;
4851 if (session
->ust_session
) {
4852 ret_code
= record_ust_snapshot(session
->ust_session
,
4853 snapshot_ust_consumer_output
, session
,
4854 wait
, nb_packets_per_stream
);
4855 if (ret_code
!= LTTNG_OK
) {
4856 goto error_close_trace_chunk
;
4860 error_close_trace_chunk
:
4861 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
4862 ERR("Failed to release the current trace chunk of session \"%s\"",
4864 ret_code
= LTTNG_ERR_UNK
;
4867 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
4868 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
4870 * Don't goto end; make sure the chunk is closed for the session
4871 * to allow future snapshots.
4873 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4875 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4878 lttng_trace_chunk_put(snapshot_trace_chunk
);
4879 snapshot_trace_chunk
= NULL
;
4881 if (original_ust_consumer_output
) {
4882 session
->ust_session
->consumer
= original_ust_consumer_output
;
4884 if (original_kernel_consumer_output
) {
4885 session
->kernel_session
->consumer
=
4886 original_kernel_consumer_output
;
4888 consumer_output_put(snapshot_ust_consumer_output
);
4889 consumer_output_put(snapshot_kernel_consumer_output
);
4894 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4896 * The wait parameter is ignored so this call always wait for the snapshot to
4897 * complete before returning.
4899 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4901 int cmd_snapshot_record(struct ltt_session
*session
,
4902 const struct lttng_snapshot_output
*output
, int wait
)
4904 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4906 unsigned int snapshot_success
= 0;
4908 struct snapshot_output
*tmp_output
= NULL
;
4913 DBG("Cmd snapshot record for session %s", session
->name
);
4915 /* Get the datetime for the snapshot output directory. */
4916 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4919 cmd_ret
= LTTNG_ERR_INVALID
;
4924 * Permission denied to create an output if the session is not
4925 * set in no output mode.
4927 if (session
->output_traces
) {
4928 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4932 /* The session needs to be started at least once. */
4933 if (!session
->has_been_started
) {
4934 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4938 /* Use temporary output for the session. */
4939 if (*output
->ctrl_url
!= '\0') {
4940 tmp_output
= snapshot_output_alloc();
4942 cmd_ret
= LTTNG_ERR_NOMEM
;
4946 ret
= snapshot_output_init(session
, output
->max_size
,
4948 output
->ctrl_url
, output
->data_url
,
4952 if (ret
== -ENOMEM
) {
4953 cmd_ret
= LTTNG_ERR_NOMEM
;
4955 cmd_ret
= LTTNG_ERR_INVALID
;
4959 /* Use the global session count for the temporary snapshot. */
4960 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
4962 /* Use the global datetime */
4963 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
4964 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
4965 if (cmd_ret
!= LTTNG_OK
) {
4968 snapshot_success
= 1;
4970 struct snapshot_output
*sout
;
4971 struct lttng_ht_iter iter
;
4974 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4975 &iter
.iter
, sout
, node
.node
) {
4976 struct snapshot_output output_copy
;
4979 * Make a local copy of the output and override output
4980 * parameters with those provided as part of the
4983 memcpy(&output_copy
, sout
, sizeof(output_copy
));
4985 if (output
->max_size
!= (uint64_t) -1ULL) {
4986 output_copy
.max_size
= output
->max_size
;
4989 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4990 memcpy(output_copy
.datetime
, datetime
,
4993 /* Use temporary name. */
4994 if (*output
->name
!= '\0') {
4995 if (lttng_strncpy(output_copy
.name
,
4997 sizeof(output_copy
.name
))) {
4998 cmd_ret
= LTTNG_ERR_INVALID
;
5004 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
5005 if (cmd_ret
!= LTTNG_OK
) {
5009 snapshot_success
= 1;
5014 if (snapshot_success
) {
5015 session
->snapshot
.nb_snapshot
++;
5017 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5022 snapshot_output_destroy(tmp_output
);
5028 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5030 int cmd_set_session_shm_path(struct ltt_session
*session
,
5031 const char *shm_path
)
5037 * Can only set shm path before session is started.
5039 if (session
->has_been_started
) {
5040 return LTTNG_ERR_SESSION_STARTED
;
5043 strncpy(session
->shm_path
, shm_path
,
5044 sizeof(session
->shm_path
));
5045 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5051 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5053 * Ask the consumer to rotate the session output directory.
5054 * The session lock must be held.
5056 * Returns LTTNG_OK on success or else a negative LTTng error code.
5058 int cmd_rotate_session(struct ltt_session
*session
,
5059 struct lttng_rotate_session_return
*rotate_return
,
5060 bool quiet_rotation
,
5061 enum lttng_trace_chunk_command_type command
)
5064 uint64_t ongoing_rotation_chunk_id
;
5065 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5066 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5067 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5068 enum lttng_trace_chunk_status chunk_status
;
5069 bool failed_to_rotate
= false;
5070 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5074 if (!session
->has_been_started
) {
5075 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5080 * Explicit rotation is not supported for live sessions.
5081 * However, live sessions can perform a quiet rotation on
5083 * Rotation is not supported for snapshot traces (no output).
5085 if ((!quiet_rotation
&& session
->live_timer
) ||
5086 !session
->output_traces
) {
5087 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5091 /* Unsupported feature in lttng-relayd before 2.11. */
5092 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5093 (session
->consumer
->relay_major_version
== 2 &&
5094 session
->consumer
->relay_minor_version
< 11)) {
5095 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5099 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5100 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5101 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5105 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5106 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5108 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5113 * After a stop, we only allow one rotation to occur, the other ones are
5114 * useless until a new start.
5116 if (session
->rotated_after_last_stop
) {
5117 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5119 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5124 * After a stop followed by a clear, disallow following rotations a they would
5125 * generate empty chunks.
5127 if (session
->cleared_after_last_stop
) {
5128 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5130 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5134 if (session
->active
) {
5135 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5137 if (!new_trace_chunk
) {
5138 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5144 * The current trace chunk becomes the chunk being archived.
5146 * After this point, "chunk_being_archived" must absolutely
5147 * be closed on the consumer(s), otherwise it will never be
5148 * cleaned-up, which will result in a leak.
5150 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5151 &chunk_being_archived
);
5153 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5157 if (session
->kernel_session
) {
5158 cmd_ret
= kernel_rotate_session(session
);
5159 if (cmd_ret
!= LTTNG_OK
) {
5160 failed_to_rotate
= true;
5161 rotation_fail_code
= cmd_ret
;
5164 if (session
->ust_session
) {
5165 cmd_ret
= ust_app_rotate_session(session
);
5166 if (cmd_ret
!= LTTNG_OK
) {
5167 failed_to_rotate
= true;
5168 rotation_fail_code
= cmd_ret
;
5172 if (!session
->active
) {
5173 session
->rotated_after_last_stop
= true;
5176 if (!chunk_being_archived
) {
5177 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5179 if (failed_to_rotate
) {
5180 cmd_ret
= rotation_fail_code
;
5187 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5188 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5189 &ongoing_rotation_chunk_id
);
5190 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5192 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5193 command
, session
->last_chunk_path
);
5195 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5199 if (failed_to_rotate
) {
5200 cmd_ret
= rotation_fail_code
;
5204 session
->quiet_rotation
= quiet_rotation
;
5205 ret
= timer_session_rotation_pending_check_start(session
,
5206 DEFAULT_ROTATE_PENDING_TIMER
);
5208 cmd_ret
= LTTNG_ERR_UNK
;
5212 if (rotate_return
) {
5213 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5216 session
->chunk_being_archived
= chunk_being_archived
;
5217 chunk_being_archived
= NULL
;
5218 if (!quiet_rotation
) {
5219 ret
= notification_thread_command_session_rotation_ongoing(
5220 notification_thread_handle
,
5221 session
->name
, session
->uid
, session
->gid
,
5222 ongoing_rotation_chunk_id
);
5223 if (ret
!= LTTNG_OK
) {
5224 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5230 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5231 session
->name
, ongoing_rotation_chunk_id
);
5233 lttng_trace_chunk_put(new_trace_chunk
);
5234 lttng_trace_chunk_put(chunk_being_archived
);
5235 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5238 if (session_reset_rotation_state(session
,
5239 LTTNG_ROTATION_STATE_ERROR
)) {
5240 ERR("Failed to reset rotation state of session \"%s\"",
5247 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5249 * Check if the session has finished its rotation.
5251 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5253 int cmd_rotate_get_info(struct ltt_session
*session
,
5254 struct lttng_rotation_get_info_return
*info_return
,
5255 uint64_t rotation_id
)
5257 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5258 enum lttng_rotation_state rotation_state
;
5260 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5261 session
->most_recent_chunk_id
.value
);
5263 if (session
->chunk_being_archived
) {
5264 enum lttng_trace_chunk_status chunk_status
;
5267 chunk_status
= lttng_trace_chunk_get_id(
5268 session
->chunk_being_archived
,
5270 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5272 rotation_state
= rotation_id
== chunk_id
?
5273 LTTNG_ROTATION_STATE_ONGOING
:
5274 LTTNG_ROTATION_STATE_EXPIRED
;
5276 if (session
->last_archived_chunk_id
.is_set
&&
5277 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5278 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5280 rotation_state
= session
->rotation_state
;
5284 switch (rotation_state
) {
5285 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5286 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5289 case LTTNG_ROTATION_STATE_EXPIRED
:
5290 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5291 rotation_id
, session
->name
);
5293 case LTTNG_ROTATION_STATE_ONGOING
:
5294 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5295 rotation_id
, session
->name
);
5297 case LTTNG_ROTATION_STATE_COMPLETED
:
5301 char *current_tracing_path_reply
;
5302 size_t current_tracing_path_reply_len
;
5304 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5305 rotation_id
, session
->name
);
5307 switch (session_get_consumer_destination_type(session
)) {
5308 case CONSUMER_DST_LOCAL
:
5309 current_tracing_path_reply
=
5310 info_return
->location
.local
.absolute_path
;
5311 current_tracing_path_reply_len
=
5312 sizeof(info_return
->location
.local
.absolute_path
);
5313 info_return
->location_type
=
5314 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5315 fmt_ret
= asprintf(&chunk_path
,
5316 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5317 session_get_base_path(session
),
5318 session
->last_archived_chunk_name
);
5319 if (fmt_ret
== -1) {
5320 PERROR("Failed to format the path of the last archived trace chunk");
5321 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5322 cmd_ret
= LTTNG_ERR_UNK
;
5326 case CONSUMER_DST_NET
:
5328 uint16_t ctrl_port
, data_port
;
5330 current_tracing_path_reply
=
5331 info_return
->location
.relay
.relative_path
;
5332 current_tracing_path_reply_len
=
5333 sizeof(info_return
->location
.relay
.relative_path
);
5334 /* Currently the only supported relay protocol. */
5335 info_return
->location
.relay
.protocol
=
5336 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5338 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5339 session_get_net_consumer_hostname(session
),
5340 sizeof(info_return
->location
.relay
.host
));
5342 ERR("Failed to copy host name to rotate_get_info reply");
5343 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5344 cmd_ret
= LTTNG_ERR_SET_URL
;
5348 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5349 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5350 info_return
->location
.relay
.ports
.data
= data_port
;
5351 info_return
->location_type
=
5352 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5353 chunk_path
= strdup(session
->last_chunk_path
);
5355 ERR("Failed to allocate the path of the last archived trace chunk");
5356 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5357 cmd_ret
= LTTNG_ERR_UNK
;
5366 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5367 chunk_path
, current_tracing_path_reply_len
);
5370 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5371 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5372 cmd_ret
= LTTNG_ERR_UNK
;
5378 case LTTNG_ROTATION_STATE_ERROR
:
5379 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5380 rotation_id
, session
->name
);
5388 info_return
->status
= (int32_t) rotation_state
;
5393 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5395 * Configure the automatic rotation parameters.
5396 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5397 * 'activate' to false means deactivate the rotation schedule and validate that
5398 * 'new_value' has the same value as the currently active value.
5400 * Return 0 on success or else a positive LTTNG_ERR code.
5402 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5403 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5405 struct notification_thread_handle
*notification_thread_handle
)
5408 uint64_t *parameter_value
;
5412 DBG("Cmd rotate set schedule session %s", session
->name
);
5414 if (session
->live_timer
|| !session
->output_traces
) {
5415 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5416 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5420 switch (schedule_type
) {
5421 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5422 parameter_value
= &session
->rotate_size
;
5424 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5425 parameter_value
= &session
->rotate_timer_period
;
5426 if (new_value
>= UINT_MAX
) {
5427 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5428 new_value
, UINT_MAX
);
5429 ret
= LTTNG_ERR_INVALID
;
5434 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5435 ret
= LTTNG_ERR_INVALID
;
5439 /* Improper use of the API. */
5440 if (new_value
== -1ULL) {
5441 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5442 ret
= LTTNG_ERR_INVALID
;
5447 * As indicated in struct ltt_session's comments, a value of == 0 means
5448 * this schedule rotation type is not in use.
5450 * Reject the command if we were asked to activate a schedule that was
5453 if (activate
&& *parameter_value
!= 0) {
5454 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5455 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5460 * Reject the command if we were asked to deactivate a schedule that was
5463 if (!activate
&& *parameter_value
== 0) {
5464 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5465 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5470 * Reject the command if we were asked to deactivate a schedule that
5473 if (!activate
&& *parameter_value
!= new_value
) {
5474 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5475 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5479 *parameter_value
= activate
? new_value
: 0;
5481 switch (schedule_type
) {
5482 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5483 if (activate
&& session
->active
) {
5485 * Only start the timer if the session is active,
5486 * otherwise it will be started when the session starts.
5488 ret
= timer_session_rotation_schedule_timer_start(
5489 session
, new_value
);
5491 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5492 ret
= LTTNG_ERR_UNK
;
5496 ret
= timer_session_rotation_schedule_timer_stop(
5499 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5500 ret
= LTTNG_ERR_UNK
;
5505 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5507 ret
= subscribe_session_consumed_size_rotation(session
,
5508 new_value
, notification_thread_handle
);
5510 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5511 ret
= LTTNG_ERR_UNK
;
5515 ret
= unsubscribe_session_consumed_size_rotation(session
,
5516 notification_thread_handle
);
5518 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5519 ret
= LTTNG_ERR_UNK
;
5526 /* Would have been caught before. */
5538 /* Wait for a given path to be removed before continuing. */
5539 static enum lttng_error_code
wait_on_path(void *path_data
)
5541 const char *shm_path
= path_data
;
5543 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5549 ret
= stat(shm_path
, &st
);
5551 if (errno
!= ENOENT
) {
5552 PERROR("stat() returned an error while checking for the existence of the shm path");
5554 DBG("shm path no longer exists, completing the destruction of session");
5558 if (!S_ISDIR(st
.st_mode
)) {
5559 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5564 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5570 * Returns a pointer to a handler to run on completion of a command.
5571 * Returns NULL if no handler has to be run for the last command executed.
5573 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5575 struct cmd_completion_handler
*handler
= current_completion_handler
;
5577 current_completion_handler
= NULL
;
5582 * Init command subsystem.
5587 * Set network sequence index to 1 for streams to match a relayd
5588 * socket on the consumer side.
5590 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5591 relayd_net_seq_idx
= 1;
5592 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5594 DBG("Command subsystem initialized");