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.
1917 int cmd_add_context(struct command_ctx
*cmd_ctx
, int sock
, int kwpipe
)
1919 struct lttng_event_context
*ctx
= NULL
;
1920 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1921 struct lttng_dynamic_buffer buffer
;
1922 struct lttng_buffer_view view
;
1924 ssize_t sock_recv_len
;
1925 enum lttng_domain_type domain
= cmd_ctx
->lsm
->domain
.type
;
1926 struct ltt_session
*session
= cmd_ctx
->session
;
1927 const char *channel_name
= cmd_ctx
->lsm
->u
.context
.channel_name
;
1929 lttng_dynamic_buffer_init(&buffer
);
1931 context_len
= (size_t) cmd_ctx
->lsm
->u
.context
.length
;
1932 ret
= lttng_dynamic_buffer_set_size(&buffer
, context_len
);
1934 ret
= LTTNG_ERR_NOMEM
;
1938 sock_recv_len
= lttcomm_recv_unix_sock(sock
, buffer
.data
, context_len
);
1939 if (sock_recv_len
< 0 || sock_recv_len
!= context_len
) {
1940 ERR("Failed to receive \"add context\" command payload");
1941 ret
= LTTNG_ERR_INVALID
;
1945 view
= lttng_buffer_view_from_dynamic_buffer(&buffer
, 0, -1);
1947 if (lttng_event_context_create_from_buffer(&view
, &ctx
) !=
1949 ERR("Invalid event context payload received in \"add context\" command");
1950 ret
= LTTNG_ERR_INVALID
;
1955 * Don't try to add a context if the session has been started at
1956 * some point in time before. The tracer does not allow it and would
1957 * result in a corrupted trace.
1959 if (cmd_ctx
->session
->has_been_started
) {
1960 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1965 case LTTNG_DOMAIN_KERNEL
:
1966 assert(session
->kernel_session
);
1968 if (session
->kernel_session
->channel_count
== 0) {
1969 /* Create default channel */
1970 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1971 if (ret
!= LTTNG_OK
) {
1974 chan_kern_created
= 1;
1976 /* Add kernel context to kernel tracer */
1977 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1978 if (ret
!= LTTNG_OK
) {
1982 case LTTNG_DOMAIN_JUL
:
1983 case LTTNG_DOMAIN_LOG4J
:
1986 * Validate channel name.
1987 * If no channel name is given and the domain is JUL or LOG4J,
1988 * set it to the appropriate domain-specific channel name. If
1989 * a name is provided but does not match the expexted channel
1990 * name, return an error.
1992 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1993 strcmp(channel_name
,
1994 DEFAULT_JUL_CHANNEL_NAME
)) {
1995 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1997 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1998 strcmp(channel_name
,
1999 DEFAULT_LOG4J_CHANNEL_NAME
)) {
2000 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2003 /* break is _not_ missing here. */
2005 case LTTNG_DOMAIN_UST
:
2007 struct ltt_ust_session
*usess
= session
->ust_session
;
2008 unsigned int chan_count
;
2012 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
2013 if (chan_count
== 0) {
2014 struct lttng_channel
*attr
;
2015 /* Create default channel */
2016 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
2018 ret
= LTTNG_ERR_FATAL
;
2022 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
2023 if (ret
!= LTTNG_OK
) {
2027 channel_attr_destroy(attr
);
2028 chan_ust_created
= 1;
2031 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2032 if (ret
!= LTTNG_OK
) {
2038 ret
= LTTNG_ERR_UND
;
2046 if (chan_kern_created
) {
2047 struct ltt_kernel_channel
*kchan
=
2048 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2049 session
->kernel_session
);
2050 /* Created previously, this should NOT fail. */
2052 kernel_destroy_channel(kchan
);
2055 if (chan_ust_created
) {
2056 struct ltt_ust_channel
*uchan
=
2057 trace_ust_find_channel_by_name(
2058 session
->ust_session
->domain_global
.channels
,
2059 DEFAULT_CHANNEL_NAME
);
2060 /* Created previously, this should NOT fail. */
2062 /* Remove from the channel list of the session. */
2063 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2065 trace_ust_destroy_channel(uchan
);
2068 lttng_event_context_destroy(ctx
);
2072 static inline bool name_starts_with(const char *name
, const char *prefix
)
2074 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2076 return !strncmp(name
, prefix
, max_cmp_len
);
2079 /* Perform userspace-specific event name validation */
2080 static int validate_ust_event_name(const char *name
)
2090 * Check name against all internal UST event component namespaces used
2093 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2094 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2095 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2104 * Internal version of cmd_enable_event() with a supplemental
2105 * "internal_event" flag which is used to enable internal events which should
2106 * be hidden from clients. Such events are used in the agent implementation to
2107 * enable the events through which all "agent" events are funeled.
2109 static int _cmd_enable_event(struct ltt_session
*session
,
2110 const struct lttng_domain
*domain
,
2111 char *channel_name
, struct lttng_event
*event
,
2112 char *filter_expression
,
2113 struct lttng_filter_bytecode
*filter
,
2114 struct lttng_event_exclusion
*exclusion
,
2115 int wpipe
, bool internal_event
)
2117 int ret
= 0, channel_created
= 0;
2118 struct lttng_channel
*attr
= NULL
;
2122 assert(channel_name
);
2124 /* If we have a filter, we must have its filter expression */
2125 assert(!(!!filter_expression
^ !!filter
));
2127 /* Normalize event name as a globbing pattern */
2128 strutils_normalize_star_glob_pattern(event
->name
);
2130 /* Normalize exclusion names as globbing patterns */
2134 for (i
= 0; i
< exclusion
->count
; i
++) {
2135 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2137 strutils_normalize_star_glob_pattern(name
);
2141 DBG("Enable event command for event \'%s\'", event
->name
);
2145 switch (domain
->type
) {
2146 case LTTNG_DOMAIN_KERNEL
:
2148 struct ltt_kernel_channel
*kchan
;
2151 * If a non-default channel has been created in the
2152 * session, explicitely require that -c chan_name needs
2155 if (session
->kernel_session
->has_non_default_channel
2156 && channel_name
[0] == '\0') {
2157 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2161 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2162 session
->kernel_session
);
2163 if (kchan
== NULL
) {
2164 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2165 LTTNG_BUFFER_GLOBAL
);
2167 ret
= LTTNG_ERR_FATAL
;
2170 if (lttng_strncpy(attr
->name
, channel_name
,
2171 sizeof(attr
->name
))) {
2172 ret
= LTTNG_ERR_INVALID
;
2176 ret
= cmd_enable_channel_internal(
2177 session
, domain
, attr
, wpipe
);
2178 if (ret
!= LTTNG_OK
) {
2181 channel_created
= 1;
2184 /* Get the newly created kernel channel pointer */
2185 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2186 session
->kernel_session
);
2187 if (kchan
== NULL
) {
2188 /* This sould not happen... */
2189 ret
= LTTNG_ERR_FATAL
;
2193 switch (event
->type
) {
2194 case LTTNG_EVENT_ALL
:
2196 char *filter_expression_a
= NULL
;
2197 struct lttng_filter_bytecode
*filter_a
= NULL
;
2200 * We need to duplicate filter_expression and filter,
2201 * because ownership is passed to first enable
2204 if (filter_expression
) {
2205 filter_expression_a
= strdup(filter_expression
);
2206 if (!filter_expression_a
) {
2207 ret
= LTTNG_ERR_FATAL
;
2212 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2214 free(filter_expression_a
);
2215 ret
= LTTNG_ERR_FATAL
;
2218 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2220 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2221 ret
= event_kernel_enable_event(kchan
, event
,
2222 filter_expression
, filter
);
2223 /* We have passed ownership */
2224 filter_expression
= NULL
;
2226 if (ret
!= LTTNG_OK
) {
2227 if (channel_created
) {
2228 /* Let's not leak a useless channel. */
2229 kernel_destroy_channel(kchan
);
2231 free(filter_expression_a
);
2235 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2236 ret
= event_kernel_enable_event(kchan
, event
,
2237 filter_expression_a
, filter_a
);
2238 /* We have passed ownership */
2239 filter_expression_a
= NULL
;
2241 if (ret
!= LTTNG_OK
) {
2246 case LTTNG_EVENT_PROBE
:
2247 case LTTNG_EVENT_USERSPACE_PROBE
:
2248 case LTTNG_EVENT_FUNCTION
:
2249 case LTTNG_EVENT_FUNCTION_ENTRY
:
2250 case LTTNG_EVENT_TRACEPOINT
:
2251 ret
= event_kernel_enable_event(kchan
, event
,
2252 filter_expression
, filter
);
2253 /* We have passed ownership */
2254 filter_expression
= NULL
;
2256 if (ret
!= LTTNG_OK
) {
2257 if (channel_created
) {
2258 /* Let's not leak a useless channel. */
2259 kernel_destroy_channel(kchan
);
2264 case LTTNG_EVENT_SYSCALL
:
2265 ret
= event_kernel_enable_event(kchan
, event
,
2266 filter_expression
, filter
);
2267 /* We have passed ownership */
2268 filter_expression
= NULL
;
2270 if (ret
!= LTTNG_OK
) {
2275 ret
= LTTNG_ERR_UNK
;
2279 kernel_wait_quiescent();
2282 case LTTNG_DOMAIN_UST
:
2284 struct ltt_ust_channel
*uchan
;
2285 struct ltt_ust_session
*usess
= session
->ust_session
;
2290 * If a non-default channel has been created in the
2291 * session, explicitely require that -c chan_name needs
2294 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2295 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2299 /* Get channel from global UST domain */
2300 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2302 if (uchan
== NULL
) {
2303 /* Create default channel */
2304 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2305 usess
->buffer_type
);
2307 ret
= LTTNG_ERR_FATAL
;
2310 if (lttng_strncpy(attr
->name
, channel_name
,
2311 sizeof(attr
->name
))) {
2312 ret
= LTTNG_ERR_INVALID
;
2316 ret
= cmd_enable_channel_internal(
2317 session
, domain
, attr
, wpipe
);
2318 if (ret
!= LTTNG_OK
) {
2322 /* Get the newly created channel reference back */
2323 uchan
= trace_ust_find_channel_by_name(
2324 usess
->domain_global
.channels
, channel_name
);
2328 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2330 * Don't allow users to add UST events to channels which
2331 * are assigned to a userspace subdomain (JUL, Log4J,
2334 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2338 if (!internal_event
) {
2340 * Ensure the event name is not reserved for internal
2343 ret
= validate_ust_event_name(event
->name
);
2345 WARN("Userspace event name %s failed validation.",
2347 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2352 /* At this point, the session and channel exist on the tracer */
2353 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2354 filter_expression
, filter
, exclusion
,
2356 /* We have passed ownership */
2357 filter_expression
= NULL
;
2360 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2361 goto already_enabled
;
2362 } else if (ret
!= LTTNG_OK
) {
2367 case LTTNG_DOMAIN_LOG4J
:
2368 case LTTNG_DOMAIN_JUL
:
2369 case LTTNG_DOMAIN_PYTHON
:
2371 const char *default_event_name
, *default_chan_name
;
2373 struct lttng_event uevent
;
2374 struct lttng_domain tmp_dom
;
2375 struct ltt_ust_session
*usess
= session
->ust_session
;
2379 if (!agent_tracing_is_enabled()) {
2380 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2381 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2385 agt
= trace_ust_find_agent(usess
, domain
->type
);
2387 agt
= agent_create(domain
->type
);
2389 ret
= LTTNG_ERR_NOMEM
;
2392 agent_add(agt
, usess
->agents
);
2395 /* Create the default tracepoint. */
2396 memset(&uevent
, 0, sizeof(uevent
));
2397 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2398 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2399 default_event_name
= event_get_default_agent_ust_name(
2401 if (!default_event_name
) {
2402 ret
= LTTNG_ERR_FATAL
;
2405 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2406 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2409 * The domain type is changed because we are about to enable the
2410 * default channel and event for the JUL domain that are hardcoded.
2411 * This happens in the UST domain.
2413 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2414 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2416 switch (domain
->type
) {
2417 case LTTNG_DOMAIN_LOG4J
:
2418 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2420 case LTTNG_DOMAIN_JUL
:
2421 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2423 case LTTNG_DOMAIN_PYTHON
:
2424 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2427 /* The switch/case we are in makes this impossible */
2432 char *filter_expression_copy
= NULL
;
2433 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2436 const size_t filter_size
= sizeof(
2437 struct lttng_filter_bytecode
)
2440 filter_copy
= zmalloc(filter_size
);
2442 ret
= LTTNG_ERR_NOMEM
;
2445 memcpy(filter_copy
, filter
, filter_size
);
2447 filter_expression_copy
=
2448 strdup(filter_expression
);
2449 if (!filter_expression
) {
2450 ret
= LTTNG_ERR_NOMEM
;
2453 if (!filter_expression_copy
|| !filter_copy
) {
2454 free(filter_expression_copy
);
2460 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2461 (char *) default_chan_name
,
2462 &uevent
, filter_expression_copy
,
2463 filter_copy
, NULL
, wpipe
);
2466 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2467 goto already_enabled
;
2468 } else if (ret
!= LTTNG_OK
) {
2472 /* The wild card * means that everything should be enabled. */
2473 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2474 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2477 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2481 filter_expression
= NULL
;
2482 if (ret
!= LTTNG_OK
) {
2489 ret
= LTTNG_ERR_UND
;
2497 free(filter_expression
);
2500 channel_attr_destroy(attr
);
2506 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2507 * We own filter, exclusion, and filter_expression.
2509 int cmd_enable_event(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
2513 ssize_t sock_recv_len
;
2514 struct lttng_event
*event
= NULL
;
2515 struct lttng_buffer_view view
;
2516 struct lttng_dynamic_buffer event_buffer
;
2517 struct lttng_event_exclusion
*exclusion
= NULL
;
2518 struct lttng_filter_bytecode
*bytecode
= NULL
;
2519 char *filter_expression
= NULL
;
2521 lttng_dynamic_buffer_init(&event_buffer
);
2522 event_len
= (size_t) cmd_ctx
->lsm
->u
.enable
.length
;
2523 ret
= lttng_dynamic_buffer_set_size(&event_buffer
, event_len
);
2525 ret
= LTTNG_ERR_NOMEM
;
2529 sock_recv_len
= lttcomm_recv_unix_sock(
2530 sock
, event_buffer
.data
, event_len
);
2531 if (sock_recv_len
< 0 || sock_recv_len
!= event_len
) {
2532 ERR("Failed to receive \"enable event\" command payload");
2533 ret
= LTTNG_ERR_INVALID
;
2537 view
= lttng_buffer_view_from_dynamic_buffer(&event_buffer
, 0, -1);
2539 if (lttng_event_create_from_buffer(&view
, &event
,
2540 &exclusion
, &filter_expression
,
2541 &bytecode
, sock
) != event_len
) {
2542 ERR("Invalid event payload received in \"enable event\" command");
2543 ret
= LTTNG_ERR_INVALID
;
2548 * The ownership of the following parameters is transferred to
2549 * _cmd_enable_event:
2551 * - filter_expression,
2555 ret
= _cmd_enable_event(cmd_ctx
->session
,
2556 ALIGNED_CONST_PTR(cmd_ctx
->lsm
->domain
),
2557 cmd_ctx
->lsm
->u
.enable
.channel_name
, event
,
2558 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2559 filter_expression
= NULL
;
2563 lttng_event_destroy(event
);
2564 lttng_dynamic_buffer_reset(&event_buffer
);
2569 * Enable an event which is internal to LTTng. An internal should
2570 * never be made visible to clients and are immune to checks such as
2573 static int cmd_enable_event_internal(struct ltt_session
*session
,
2574 const struct lttng_domain
*domain
,
2575 char *channel_name
, struct lttng_event
*event
,
2576 char *filter_expression
,
2577 struct lttng_filter_bytecode
*filter
,
2578 struct lttng_event_exclusion
*exclusion
,
2581 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2582 filter_expression
, filter
, exclusion
, wpipe
, true);
2586 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2588 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2589 struct lttng_dynamic_buffer
*buffer
, unsigned int *nb_tracepoints
)
2591 enum lttng_error_code ret_code
;
2593 ssize_t i
, nb_events
= 0;
2594 struct lttng_event
*events
= NULL
;
2597 assert(nb_tracepoints
);
2600 case LTTNG_DOMAIN_KERNEL
:
2601 nb_events
= kernel_list_events(&events
);
2602 if (nb_events
< 0) {
2603 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2607 case LTTNG_DOMAIN_UST
:
2608 nb_events
= ust_app_list_events(&events
);
2609 if (nb_events
< 0) {
2610 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2614 case LTTNG_DOMAIN_LOG4J
:
2615 case LTTNG_DOMAIN_JUL
:
2616 case LTTNG_DOMAIN_PYTHON
:
2617 nb_events
= agent_list_events(&events
, domain
);
2618 if (nb_events
< 0) {
2619 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2624 ret_code
= LTTNG_ERR_UND
;
2629 for (i
= 0; i
< nb_events
; i
++) {
2630 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2633 ret_code
= LTTNG_ERR_NOMEM
;
2638 if (nb_events
> UINT_MAX
) {
2639 ret_code
= LTTNG_ERR_OVERFLOW
;
2643 *nb_tracepoints
= (unsigned int) nb_events
;
2644 ret_code
= LTTNG_OK
;
2651 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2653 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2654 struct lttng_event_field
**fields
)
2657 ssize_t nb_fields
= 0;
2660 case LTTNG_DOMAIN_UST
:
2661 nb_fields
= ust_app_list_event_fields(fields
);
2662 if (nb_fields
< 0) {
2663 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2667 case LTTNG_DOMAIN_KERNEL
:
2668 default: /* fall-through */
2669 ret
= LTTNG_ERR_UND
;
2676 /* Return negative value to differentiate return code */
2680 enum lttng_error_code
cmd_list_syscalls(
2681 struct lttng_dynamic_buffer
*buffer
, unsigned int *nb_syscalls
)
2683 enum lttng_error_code ret_code
;
2684 ssize_t nb_events
, i
;
2686 struct lttng_event
*events
= NULL
;
2689 assert(nb_syscalls
);
2691 nb_events
= syscall_table_list(&events
);
2692 if (nb_events
< 0) {
2693 ret_code
= (enum lttng_error_code
) -nb_events
;
2697 for (i
= 0; i
< nb_events
; i
++) {
2698 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2701 ret_code
= LTTNG_ERR_NOMEM
;
2706 if (nb_events
> UINT_MAX
) {
2707 ret_code
= LTTNG_ERR_OVERFLOW
;
2711 *nb_syscalls
= (uint32_t) nb_events
;
2712 ret_code
= LTTNG_OK
;
2719 * Command LTTNG_START_TRACE processed by the client thread.
2721 * Called with session mutex held.
2723 int cmd_start_trace(struct ltt_session
*session
)
2725 enum lttng_error_code ret
;
2726 unsigned long nb_chan
= 0;
2727 struct ltt_kernel_session
*ksession
;
2728 struct ltt_ust_session
*usess
;
2729 const bool session_rotated_after_last_stop
=
2730 session
->rotated_after_last_stop
;
2731 const bool session_cleared_after_last_stop
=
2732 session
->cleared_after_last_stop
;
2736 /* Ease our life a bit ;) */
2737 ksession
= session
->kernel_session
;
2738 usess
= session
->ust_session
;
2740 /* Is the session already started? */
2741 if (session
->active
) {
2742 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2743 /* Perform nothing */
2747 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2748 !session
->current_trace_chunk
) {
2750 * A rotation was launched while the session was stopped and
2751 * it has not been completed yet. It is not possible to start
2752 * the session since starting the session here would require a
2753 * rotation from "NULL" to a new trace chunk. That rotation
2754 * would overlap with the ongoing rotation, which is not
2757 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2759 ret
= LTTNG_ERR_ROTATION_PENDING
;
2764 * Starting a session without channel is useless since after that it's not
2765 * possible to enable channel thus inform the client.
2767 if (usess
&& usess
->domain_global
.channels
) {
2768 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2771 nb_chan
+= ksession
->channel_count
;
2774 ret
= LTTNG_ERR_NO_CHANNEL
;
2778 session
->active
= 1;
2779 session
->rotated_after_last_stop
= false;
2780 session
->cleared_after_last_stop
= false;
2781 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2782 if (!session
->has_been_started
) {
2783 struct lttng_trace_chunk
*trace_chunk
;
2785 DBG("Creating initial trace chunk of session \"%s\"",
2787 trace_chunk
= session_create_new_trace_chunk(
2788 session
, NULL
, NULL
, NULL
);
2790 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2793 assert(!session
->current_trace_chunk
);
2794 ret
= session_set_trace_chunk(session
, trace_chunk
,
2796 lttng_trace_chunk_put(trace_chunk
);
2798 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2802 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2805 * Rotate existing streams into the new chunk.
2806 * This is a "quiet" rotation has no client has
2807 * explicitly requested this operation.
2809 * There is also no need to wait for the rotation
2810 * to complete as it will happen immediately. No data
2811 * was produced as the session was stopped, so the
2812 * rotation should happen on reception of the command.
2814 ret
= cmd_rotate_session(session
, NULL
, true,
2815 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2816 if (ret
!= LTTNG_OK
) {
2822 /* Kernel tracing */
2823 if (ksession
!= NULL
) {
2824 DBG("Start kernel tracing session %s", session
->name
);
2825 ret
= start_kernel_session(ksession
);
2826 if (ret
!= LTTNG_OK
) {
2831 /* Flag session that trace should start automatically */
2833 int int_ret
= ust_app_start_trace_all(usess
);
2836 ret
= LTTNG_ERR_UST_START_FAIL
;
2842 * Open a packet in every stream of the session to ensure that viewers
2843 * can correctly identify the boundaries of the periods during which
2844 * tracing was active for this session.
2846 ret
= session_open_packets(session
);
2847 if (ret
!= LTTNG_OK
) {
2852 * Clear the flag that indicates that a rotation was done while the
2853 * session was stopped.
2855 session
->rotated_after_last_stop
= false;
2857 if (session
->rotate_timer_period
) {
2858 int int_ret
= timer_session_rotation_schedule_timer_start(
2859 session
, session
->rotate_timer_period
);
2862 ERR("Failed to enable rotate timer");
2863 ret
= LTTNG_ERR_UNK
;
2871 if (ret
== LTTNG_OK
) {
2872 /* Flag this after a successful start. */
2873 session
->has_been_started
|= 1;
2875 session
->active
= 0;
2876 /* Restore initial state on error. */
2877 session
->rotated_after_last_stop
=
2878 session_rotated_after_last_stop
;
2879 session
->cleared_after_last_stop
=
2880 session_cleared_after_last_stop
;
2887 * Command LTTNG_STOP_TRACE processed by the client thread.
2889 int cmd_stop_trace(struct ltt_session
*session
)
2892 struct ltt_kernel_session
*ksession
;
2893 struct ltt_ust_session
*usess
;
2897 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2899 ksession
= session
->kernel_session
;
2900 usess
= session
->ust_session
;
2902 /* Session is not active. Skip everythong and inform the client. */
2903 if (!session
->active
) {
2904 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2908 ret
= stop_kernel_session(ksession
);
2909 if (ret
!= LTTNG_OK
) {
2913 if (usess
&& usess
->active
) {
2914 ret
= ust_app_stop_trace_all(usess
);
2916 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2921 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2923 /* Flag inactive after a successful stop. */
2924 session
->active
= 0;
2932 * Set the base_path of the session only if subdir of a control uris is set.
2933 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2935 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2937 struct lttng_uri
*uris
)
2942 for (i
= 0; i
< nb_uri
; i
++) {
2943 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2944 uris
[i
].subdir
[0] == '\0') {
2945 /* Not interested in these URIs */
2949 if (session
->base_path
!= NULL
) {
2950 free(session
->base_path
);
2951 session
->base_path
= NULL
;
2954 /* Set session base_path */
2955 session
->base_path
= strdup(uris
[i
].subdir
);
2956 if (!session
->base_path
) {
2957 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2958 uris
[i
].subdir
, session
->name
);
2959 ret
= LTTNG_ERR_NOMEM
;
2962 DBG2("Setting base path \"%s\" for session \"%s\"",
2963 session
->base_path
, session
->name
);
2971 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2973 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2974 struct lttng_uri
*uris
)
2977 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2978 struct ltt_ust_session
*usess
= session
->ust_session
;
2984 /* Can't set consumer URI if the session is active. */
2985 if (session
->active
) {
2986 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2991 * Set the session base path if any. This is done inside
2992 * cmd_set_consumer_uri to preserve backward compatibility of the
2993 * previous session creation api vs the session descriptor api.
2995 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2996 if (ret
!= LTTNG_OK
) {
3000 /* Set the "global" consumer URIs */
3001 for (i
= 0; i
< nb_uri
; i
++) {
3002 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
3004 if (ret
!= LTTNG_OK
) {
3009 /* Set UST session URIs */
3010 if (session
->ust_session
) {
3011 for (i
= 0; i
< nb_uri
; i
++) {
3012 ret
= add_uri_to_consumer(session
,
3013 session
->ust_session
->consumer
,
3014 &uris
[i
], LTTNG_DOMAIN_UST
);
3015 if (ret
!= LTTNG_OK
) {
3021 /* Set kernel session URIs */
3022 if (session
->kernel_session
) {
3023 for (i
= 0; i
< nb_uri
; i
++) {
3024 ret
= add_uri_to_consumer(session
,
3025 session
->kernel_session
->consumer
,
3026 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3027 if (ret
!= LTTNG_OK
) {
3034 * Make sure to set the session in output mode after we set URI since a
3035 * session can be created without URL (thus flagged in no output mode).
3037 session
->output_traces
= 1;
3039 ksess
->output_traces
= 1;
3043 usess
->output_traces
= 1;
3054 enum lttng_error_code
set_session_output_from_descriptor(
3055 struct ltt_session
*session
,
3056 const struct lttng_session_descriptor
*descriptor
)
3059 enum lttng_error_code ret_code
= LTTNG_OK
;
3060 enum lttng_session_descriptor_type session_type
=
3061 lttng_session_descriptor_get_type(descriptor
);
3062 enum lttng_session_descriptor_output_type output_type
=
3063 lttng_session_descriptor_get_output_type(descriptor
);
3064 struct lttng_uri uris
[2] = {};
3065 size_t uri_count
= 0;
3067 switch (output_type
) {
3068 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3070 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3071 lttng_session_descriptor_get_local_output_uri(descriptor
,
3075 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3076 lttng_session_descriptor_get_network_output_uris(descriptor
,
3077 &uris
[0], &uris
[1]);
3081 ret_code
= LTTNG_ERR_INVALID
;
3085 switch (session_type
) {
3086 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3088 struct snapshot_output
*new_output
= NULL
;
3090 new_output
= snapshot_output_alloc();
3092 ret_code
= LTTNG_ERR_NOMEM
;
3096 ret
= snapshot_output_init_with_uri(session
,
3097 DEFAULT_SNAPSHOT_MAX_SIZE
,
3098 NULL
, uris
, uri_count
, session
->consumer
,
3099 new_output
, &session
->snapshot
);
3101 ret_code
= (ret
== -ENOMEM
) ?
3102 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3103 snapshot_output_destroy(new_output
);
3106 snapshot_add_output(&session
->snapshot
, new_output
);
3109 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3110 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3112 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
3116 ret_code
= LTTNG_ERR_INVALID
;
3124 enum lttng_error_code
cmd_create_session_from_descriptor(
3125 struct lttng_session_descriptor
*descriptor
,
3126 const lttng_sock_cred
*creds
,
3127 const char *home_path
)
3130 enum lttng_error_code ret_code
;
3131 const char *session_name
;
3132 struct ltt_session
*new_session
= NULL
;
3133 enum lttng_session_descriptor_status descriptor_status
;
3135 session_lock_list();
3137 if (*home_path
!= '/') {
3138 ERR("Home path provided by client is not absolute");
3139 ret_code
= LTTNG_ERR_INVALID
;
3144 descriptor_status
= lttng_session_descriptor_get_session_name(
3145 descriptor
, &session_name
);
3146 switch (descriptor_status
) {
3147 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3149 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3150 session_name
= NULL
;
3153 ret_code
= LTTNG_ERR_INVALID
;
3157 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3159 if (ret_code
!= LTTNG_OK
) {
3163 if (!session_name
) {
3164 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3167 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3172 if (!lttng_session_descriptor_is_output_destination_initialized(
3175 * Only include the session's creation time in the output
3176 * destination if the name of the session itself was
3177 * not auto-generated.
3179 ret_code
= lttng_session_descriptor_set_default_output(
3181 session_name
? &new_session
->creation_time
: NULL
,
3183 if (ret_code
!= LTTNG_OK
) {
3187 new_session
->has_user_specified_directory
=
3188 lttng_session_descriptor_has_output_directory(
3192 switch (lttng_session_descriptor_get_type(descriptor
)) {
3193 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3194 new_session
->snapshot_mode
= 1;
3196 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3197 new_session
->live_timer
=
3198 lttng_session_descriptor_live_get_timer_interval(
3205 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3206 if (ret_code
!= LTTNG_OK
) {
3209 new_session
->consumer
->enabled
= 1;
3210 ret_code
= LTTNG_OK
;
3212 /* Release reference provided by the session_create function. */
3213 session_put(new_session
);
3214 if (ret_code
!= LTTNG_OK
&& new_session
) {
3215 /* Release the global reference on error. */
3216 session_destroy(new_session
);
3218 session_unlock_list();
3222 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3223 struct lttng_session_descriptor
**return_descriptor
)
3226 size_t payload_size
;
3227 struct lttng_dynamic_buffer payload
;
3228 struct lttng_buffer_view home_dir_view
;
3229 struct lttng_buffer_view session_descriptor_view
;
3230 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3231 enum lttng_error_code ret_code
;
3233 lttng_dynamic_buffer_init(&payload
);
3234 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
3236 ret_code
= LTTNG_ERR_INVALID
;
3239 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
3240 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3241 ret_code
= LTTNG_ERR_INVALID
;
3245 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
3246 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
3247 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3249 ret_code
= LTTNG_ERR_NOMEM
;
3253 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3255 ERR("Reception of session descriptor failed, aborting.");
3256 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3260 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3263 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3264 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3266 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3267 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3269 ret
= lttng_session_descriptor_create_from_buffer(
3270 &session_descriptor_view
, &session_descriptor
);
3272 ERR("Failed to create session descriptor from payload of \"create session\" command");
3273 ret_code
= LTTNG_ERR_INVALID
;
3278 * Sets the descriptor's auto-generated properties (name, output) if
3281 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3283 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3284 if (ret_code
!= LTTNG_OK
) {
3288 ret_code
= LTTNG_OK
;
3289 *return_descriptor
= session_descriptor
;
3290 session_descriptor
= NULL
;
3292 lttng_dynamic_buffer_reset(&payload
);
3293 lttng_session_descriptor_destroy(session_descriptor
);
3298 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3299 void *_reply_context
)
3303 const struct cmd_destroy_session_reply_context
*reply_context
=
3305 struct lttng_dynamic_buffer payload
;
3306 struct lttcomm_session_destroy_command_header cmd_header
;
3307 struct lttng_trace_archive_location
*location
= NULL
;
3308 struct lttcomm_lttng_msg llm
= {
3309 .cmd_type
= LTTNG_DESTROY_SESSION
,
3310 .ret_code
= reply_context
->destruction_status
,
3313 sizeof(struct lttcomm_session_destroy_command_header
),
3316 size_t payload_size_before_location
;
3318 lttng_dynamic_buffer_init(&payload
);
3320 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3322 ERR("Failed to append session destruction message");
3326 cmd_header
.rotation_state
=
3327 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3328 session
->rotation_state
:
3329 LTTNG_ROTATION_STATE_NO_ROTATION
);
3330 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3331 sizeof(cmd_header
));
3333 ERR("Failed to append session destruction command header");
3337 if (!reply_context
->implicit_rotation_on_destroy
) {
3338 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3342 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3343 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3348 location
= session_get_trace_archive_location(session
);
3350 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3355 payload_size_before_location
= payload
.size
;
3356 comm_ret
= lttng_trace_archive_location_serialize(location
,
3358 lttng_trace_archive_location_put(location
);
3360 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3364 /* Update the message to indicate the location's length. */
3365 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3366 payload
.size
- payload_size_before_location
;
3368 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3369 payload
.data
, payload
.size
);
3370 if (comm_ret
!= (ssize_t
) payload
.size
) {
3371 ERR("Failed to send result of the destruction of session \"%s\" to client",
3375 ret
= close(reply_context
->reply_sock_fd
);
3377 PERROR("Failed to close client socket in deferred session destroy reply");
3379 lttng_dynamic_buffer_reset(&payload
);
3380 free(_reply_context
);
3384 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3386 * Called with session lock held.
3388 int cmd_destroy_session(struct ltt_session
*session
,
3389 struct notification_thread_handle
*notification_thread_handle
,
3393 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3394 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3397 reply_context
= zmalloc(sizeof(*reply_context
));
3398 if (!reply_context
) {
3399 ret
= LTTNG_ERR_NOMEM
;
3402 reply_context
->reply_sock_fd
= *sock_fd
;
3408 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3410 if (session
->active
) {
3411 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3413 ret
= cmd_stop_trace(session
);
3414 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3415 /* Carry on with the destruction of the session. */
3416 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3417 session
->name
, lttng_strerror(-ret
));
3418 destruction_last_error
= ret
;
3422 if (session
->rotation_schedule_timer_enabled
) {
3423 if (timer_session_rotation_schedule_timer_stop(
3425 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3427 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3431 if (session
->rotate_size
) {
3432 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3433 session
->rotate_size
= 0;
3436 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3438 * Perform a last rotation on destruction if rotations have
3439 * occurred during the session's lifetime.
3441 ret
= cmd_rotate_session(session
, NULL
, false,
3442 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3443 if (ret
!= LTTNG_OK
) {
3444 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3445 session
->name
, lttng_strerror(-ret
));
3446 destruction_last_error
= -ret
;
3448 if (reply_context
) {
3449 reply_context
->implicit_rotation_on_destroy
= true;
3451 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3453 * The user has not triggered a session rotation. However, to
3454 * ensure all data has been consumed, the session is rotated
3455 * to a 'null' trace chunk before it is destroyed.
3457 * This is a "quiet" rotation meaning that no notification is
3458 * emitted and no renaming of the current trace chunk takes
3461 ret
= cmd_rotate_session(session
, NULL
, true,
3462 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3464 * Rotation operations may not be supported by the kernel
3465 * tracer. Hence, do not consider this implicit rotation as
3466 * a session destruction error. The library has already stopped
3467 * the session and waited for pending data; there is nothing
3468 * left to do but complete the destruction of the session.
3470 if (ret
!= LTTNG_OK
&&
3471 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3472 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3473 session
->name
, lttng_strerror(ret
));
3474 destruction_last_error
= -ret
;
3478 if (session
->shm_path
[0]) {
3480 * When a session is created with an explicit shm_path,
3481 * the consumer daemon will create its shared memory files
3482 * at that location and will *not* unlink them. This is normal
3483 * as the intention of that feature is to make it possible
3484 * to retrieve the content of those files should a crash occur.
3486 * To ensure the content of those files can be used, the
3487 * sessiond daemon will replicate the content of the metadata
3488 * cache in a metadata file.
3490 * On clean-up, it is expected that the consumer daemon will
3491 * unlink the shared memory files and that the session daemon
3492 * will unlink the metadata file. Then, the session's directory
3493 * in the shm path can be removed.
3495 * Unfortunately, a flaw in the design of the sessiond's and
3496 * consumerd's tear down of channels makes it impossible to
3497 * determine when the sessiond _and_ the consumerd have both
3498 * destroyed their representation of a channel. For one, the
3499 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3500 * callbacks in both daemons.
3502 * However, it is also impossible for the sessiond to know when
3503 * the consumer daemon is done destroying its channel(s) since
3504 * it occurs as a reaction to the closing of the channel's file
3505 * descriptor. There is no resulting communication initiated
3506 * from the consumerd to the sessiond to confirm that the
3507 * operation is completed (and was successful).
3509 * Until this is all fixed, the session daemon checks for the
3510 * removal of the session's shm path which makes it possible
3511 * to safely advertise a session as having been destroyed.
3513 * Prior to this fix, it was not possible to reliably save
3514 * a session making use of the --shm-path option, destroy it,
3515 * and load it again. This is because the creation of the
3516 * session would fail upon seeing the session's shm path
3517 * already in existence.
3519 * Note that none of the error paths in the check for the
3520 * directory's existence return an error. This is normal
3521 * as there isn't much that can be done. The session will
3522 * be destroyed properly, except that we can't offer the
3523 * guarantee that the same session can be re-created.
3525 current_completion_handler
= &destroy_completion_handler
.handler
;
3526 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3528 sizeof(destroy_completion_handler
.shm_path
));
3533 * The session is destroyed. However, note that the command context
3534 * still holds a reference to the session, thus delaying its destruction
3535 * _at least_ up to the point when that reference is released.
3537 session_destroy(session
);
3538 if (reply_context
) {
3539 reply_context
->destruction_status
= destruction_last_error
;
3540 ret
= session_add_destroy_notifier(session
,
3541 cmd_destroy_session_reply
,
3542 (void *) reply_context
);
3544 ret
= LTTNG_ERR_FATAL
;
3556 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3558 int cmd_register_consumer(struct ltt_session
*session
,
3559 enum lttng_domain_type domain
, const char *sock_path
,
3560 struct consumer_data
*cdata
)
3563 struct consumer_socket
*socket
= NULL
;
3570 case LTTNG_DOMAIN_KERNEL
:
3572 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3576 /* Can't register a consumer if there is already one */
3577 if (ksess
->consumer_fds_sent
!= 0) {
3578 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3582 sock
= lttcomm_connect_unix_sock(sock_path
);
3584 ret
= LTTNG_ERR_CONNECT_FAIL
;
3587 cdata
->cmd_sock
= sock
;
3589 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3590 if (socket
== NULL
) {
3593 PERROR("close register consumer");
3595 cdata
->cmd_sock
= -1;
3596 ret
= LTTNG_ERR_FATAL
;
3600 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3601 if (socket
->lock
== NULL
) {
3602 PERROR("zmalloc pthread mutex");
3603 ret
= LTTNG_ERR_FATAL
;
3606 pthread_mutex_init(socket
->lock
, NULL
);
3607 socket
->registered
= 1;
3610 consumer_add_socket(socket
, ksess
->consumer
);
3613 pthread_mutex_lock(&cdata
->pid_mutex
);
3615 pthread_mutex_unlock(&cdata
->pid_mutex
);
3620 /* TODO: Userspace tracing */
3621 ret
= LTTNG_ERR_UND
;
3629 consumer_destroy_socket(socket
);
3635 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3637 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3638 struct lttng_domain
**domains
)
3643 struct lttng_ht_iter iter
;
3645 if (session
->kernel_session
!= NULL
) {
3646 DBG3("Listing domains found kernel domain");
3650 if (session
->ust_session
!= NULL
) {
3651 DBG3("Listing domains found UST global domain");
3655 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3657 if (agt
->being_used
) {
3668 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3669 if (*domains
== NULL
) {
3670 ret
= LTTNG_ERR_FATAL
;
3674 if (session
->kernel_session
!= NULL
) {
3675 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3677 /* Kernel session buffer type is always GLOBAL */
3678 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3683 if (session
->ust_session
!= NULL
) {
3684 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3685 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3689 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3691 if (agt
->being_used
) {
3692 (*domains
)[index
].type
= agt
->domain
;
3693 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3703 /* Return negative value to differentiate return code */
3709 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3711 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3712 struct ltt_session
*session
,
3713 struct lttng_dynamic_buffer
*buffer
,
3714 uint32_t *nb_channel
)
3723 DBG("Listing channels for session %s", session
->name
);
3726 case LTTNG_DOMAIN_KERNEL
:
3728 /* Kernel channels */
3729 struct ltt_kernel_channel
*kchan
;
3730 if (session
->kernel_session
!= NULL
) {
3731 cds_list_for_each_entry(kchan
,
3732 &session
->kernel_session
->channel_list
.head
, list
) {
3733 uint64_t discarded_events
, lost_packets
;
3734 struct lttng_channel_extended
*extended
;
3736 extended
= (struct lttng_channel_extended
*)
3737 kchan
->channel
->attr
.extended
.ptr
;
3739 ret
= get_kernel_runtime_stats(session
, kchan
,
3740 &discarded_events
, &lost_packets
);
3746 * Update the discarded_events and lost_packets
3747 * count for the channel
3749 extended
->discarded_events
= discarded_events
;
3750 extended
->lost_packets
= lost_packets
;
3752 ret
= lttng_channel_serialize(
3753 kchan
->channel
, buffer
);
3764 case LTTNG_DOMAIN_UST
:
3766 struct lttng_ht_iter iter
;
3767 struct ltt_ust_channel
*uchan
;
3770 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3771 &iter
.iter
, uchan
, node
.node
) {
3772 uint64_t discarded_events
= 0, lost_packets
= 0;
3773 struct lttng_channel
*channel
= NULL
;
3774 struct lttng_channel_extended
*extended
;
3776 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3782 extended
= (struct lttng_channel_extended
*)
3783 channel
->attr
.extended
.ptr
;
3785 ret
= get_ust_runtime_stats(session
, uchan
,
3786 &discarded_events
, &lost_packets
);
3788 lttng_channel_destroy(channel
);
3792 extended
->discarded_events
= discarded_events
;
3793 extended
->lost_packets
= lost_packets
;
3795 ret
= lttng_channel_serialize(channel
, buffer
);
3813 return LTTNG_ERR_FATAL
;
3821 * Command LTTNG_LIST_EVENTS processed by the client thread.
3823 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3824 struct ltt_session
*session
,
3826 struct lttng_dynamic_buffer
*payload
,
3827 unsigned int *nb_events
)
3829 enum lttng_error_code ret_code
= LTTNG_OK
;
3834 case LTTNG_DOMAIN_KERNEL
:
3835 if (session
->kernel_session
!= NULL
) {
3836 ret_code
= list_lttng_kernel_events(channel_name
,
3837 session
->kernel_session
, payload
, nb_events
);
3840 case LTTNG_DOMAIN_UST
:
3842 if (session
->ust_session
!= NULL
) {
3843 ret_code
= list_lttng_ust_global_events(channel_name
,
3844 &session
->ust_session
->domain_global
,
3845 payload
, nb_events
);
3849 case LTTNG_DOMAIN_LOG4J
:
3850 case LTTNG_DOMAIN_JUL
:
3851 case LTTNG_DOMAIN_PYTHON
:
3852 if (session
->ust_session
) {
3853 struct lttng_ht_iter iter
;
3857 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3858 &iter
.iter
, agt
, node
.node
) {
3859 if (agt
->domain
== domain
) {
3860 ret_code
= list_lttng_agent_events(
3861 agt
, payload
, nb_events
);
3869 ret_code
= LTTNG_ERR_UND
;
3877 * Using the session list, filled a lttng_session array to send back to the
3878 * client for session listing.
3880 * The session list lock MUST be acquired before calling this function. Use
3881 * session_lock_list() and session_unlock_list().
3883 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3884 size_t session_count
, uid_t uid
, gid_t gid
)
3888 struct ltt_session
*session
;
3889 struct ltt_session_list
*list
= session_get_list();
3890 struct lttng_session_extended
*extended
=
3891 (typeof(extended
)) (&sessions
[session_count
]);
3893 DBG("Getting all available session for UID %d GID %d",
3896 * Iterate over session list and append data after the control struct in
3899 cds_list_for_each_entry(session
, &list
->head
, list
) {
3900 if (!session_get(session
)) {
3904 * Only list the sessions the user can control.
3906 if (!session_access_ok(session
, uid
, gid
) ||
3907 session
->destroyed
) {
3908 session_put(session
);
3912 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3913 struct ltt_ust_session
*usess
= session
->ust_session
;
3915 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3916 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3917 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3918 ret
= build_network_session_path(sessions
[i
].path
,
3919 sizeof(sessions
[i
].path
), session
);
3921 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3922 session
->consumer
->dst
.session_root_path
);
3925 PERROR("snprintf session path");
3926 session_put(session
);
3930 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3931 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3932 sessions
[i
].enabled
= session
->active
;
3933 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3934 sessions
[i
].live_timer_interval
= session
->live_timer
;
3935 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3936 extended
[i
].creation_time
.is_set
= 1;
3938 session_put(session
);
3943 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3944 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3946 int cmd_data_pending(struct ltt_session
*session
)
3949 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3950 struct ltt_ust_session
*usess
= session
->ust_session
;
3954 DBG("Data pending for session %s", session
->name
);
3956 /* Session MUST be stopped to ask for data availability. */
3957 if (session
->active
) {
3958 ret
= LTTNG_ERR_SESSION_STARTED
;
3962 * If stopped, just make sure we've started before else the above call
3963 * will always send that there is data pending.
3965 * The consumer assumes that when the data pending command is received,
3966 * the trace has been started before or else no output data is written
3967 * by the streams which is a condition for data pending. So, this is
3968 * *VERY* important that we don't ask the consumer before a start
3971 if (!session
->has_been_started
) {
3977 /* A rotation is still pending, we have to wait. */
3978 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3979 DBG("Rotate still pending for session %s", session
->name
);
3984 if (ksess
&& ksess
->consumer
) {
3985 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3987 /* Data is still being extracted for the kernel. */
3992 if (usess
&& usess
->consumer
) {
3993 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3995 /* Data is still being extracted for the kernel. */
4000 /* Data is ready to be read by a viewer */
4008 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4010 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4012 int cmd_snapshot_add_output(struct ltt_session
*session
,
4013 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4016 struct snapshot_output
*new_output
;
4021 DBG("Cmd snapshot add output for session %s", session
->name
);
4024 * Can't create an output if the session is not set in no-output mode.
4026 if (session
->output_traces
) {
4027 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4031 if (session
->has_non_mmap_channel
) {
4032 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4036 /* Only one output is allowed until we have the "tee" feature. */
4037 if (session
->snapshot
.nb_output
== 1) {
4038 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4042 new_output
= snapshot_output_alloc();
4044 ret
= LTTNG_ERR_NOMEM
;
4048 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4049 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4050 &session
->snapshot
);
4052 if (ret
== -ENOMEM
) {
4053 ret
= LTTNG_ERR_NOMEM
;
4055 ret
= LTTNG_ERR_INVALID
;
4061 snapshot_add_output(&session
->snapshot
, new_output
);
4063 *id
= new_output
->id
;
4070 snapshot_output_destroy(new_output
);
4076 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4078 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4080 int cmd_snapshot_del_output(struct ltt_session
*session
,
4081 const struct lttng_snapshot_output
*output
)
4084 struct snapshot_output
*sout
= NULL
;
4092 * Permission denied to create an output if the session is not
4093 * set in no output mode.
4095 if (session
->output_traces
) {
4096 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4101 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4103 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4104 } else if (*output
->name
!= '\0') {
4105 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4107 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4110 ret
= LTTNG_ERR_INVALID
;
4114 snapshot_delete_output(&session
->snapshot
, sout
);
4115 snapshot_output_destroy(sout
);
4124 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4126 * If no output is available, outputs is untouched and 0 is returned.
4128 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4130 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4131 struct lttng_snapshot_output
**outputs
)
4134 struct lttng_snapshot_output
*list
= NULL
;
4135 struct lttng_ht_iter iter
;
4136 struct snapshot_output
*output
;
4141 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4144 * Permission denied to create an output if the session is not
4145 * set in no output mode.
4147 if (session
->output_traces
) {
4148 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4152 if (session
->snapshot
.nb_output
== 0) {
4157 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4159 ret
= -LTTNG_ERR_NOMEM
;
4163 /* Copy list from session to the new list object. */
4165 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4166 output
, node
.node
) {
4167 assert(output
->consumer
);
4168 list
[idx
].id
= output
->id
;
4169 list
[idx
].max_size
= output
->max_size
;
4170 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4171 sizeof(list
[idx
].name
))) {
4172 ret
= -LTTNG_ERR_INVALID
;
4175 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4176 if (lttng_strncpy(list
[idx
].ctrl_url
,
4177 output
->consumer
->dst
.session_root_path
,
4178 sizeof(list
[idx
].ctrl_url
))) {
4179 ret
= -LTTNG_ERR_INVALID
;
4184 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4185 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4187 ret
= -LTTNG_ERR_NOMEM
;
4192 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4193 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4195 ret
= -LTTNG_ERR_NOMEM
;
4204 ret
= session
->snapshot
.nb_output
;
4213 * Check if we can regenerate the metadata for this session.
4214 * Only kernel, UST per-uid and non-live sessions are supported.
4216 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4219 int check_regenerate_metadata_support(struct ltt_session
*session
)
4225 if (session
->live_timer
!= 0) {
4226 ret
= LTTNG_ERR_LIVE_SESSION
;
4229 if (!session
->active
) {
4230 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4233 if (session
->ust_session
) {
4234 switch (session
->ust_session
->buffer_type
) {
4235 case LTTNG_BUFFER_PER_UID
:
4237 case LTTNG_BUFFER_PER_PID
:
4238 ret
= LTTNG_ERR_PER_PID_SESSION
;
4242 ret
= LTTNG_ERR_UNK
;
4246 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4247 session
->consumer
->relay_minor_version
< 8) {
4248 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4258 int clear_metadata_file(int fd
)
4263 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4264 if (lseek_ret
< 0) {
4270 ret
= ftruncate(fd
, 0);
4272 PERROR("ftruncate");
4281 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4284 struct buffer_reg_uid
*uid_reg
= NULL
;
4285 struct buffer_reg_session
*session_reg
= NULL
;
4288 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4289 struct ust_registry_session
*registry
;
4290 struct ust_registry_channel
*chan
;
4291 struct lttng_ht_iter iter_chan
;
4293 session_reg
= uid_reg
->registry
;
4294 registry
= session_reg
->reg
.ust
;
4296 pthread_mutex_lock(®istry
->lock
);
4297 registry
->metadata_len_sent
= 0;
4298 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4299 registry
->metadata_len
= 0;
4300 registry
->metadata_version
++;
4301 if (registry
->metadata_fd
> 0) {
4302 /* Clear the metadata file's content. */
4303 ret
= clear_metadata_file(registry
->metadata_fd
);
4305 pthread_mutex_unlock(®istry
->lock
);
4310 ret
= ust_metadata_session_statedump(registry
, NULL
,
4311 registry
->major
, registry
->minor
);
4313 pthread_mutex_unlock(®istry
->lock
);
4314 ERR("Failed to generate session metadata (err = %d)",
4318 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4320 struct ust_registry_event
*event
;
4321 struct lttng_ht_iter iter_event
;
4323 ret
= ust_metadata_channel_statedump(registry
, chan
);
4325 pthread_mutex_unlock(®istry
->lock
);
4326 ERR("Failed to generate channel metadata "
4330 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4332 ret
= ust_metadata_event_statedump(registry
,
4335 pthread_mutex_unlock(®istry
->lock
);
4336 ERR("Failed to generate event metadata "
4342 pthread_mutex_unlock(®istry
->lock
);
4351 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4353 * Ask the consumer to truncate the existing metadata file(s) and
4354 * then regenerate the metadata. Live and per-pid sessions are not
4355 * supported and return an error.
4357 * Return 0 on success or else a LTTNG_ERR code.
4359 int cmd_regenerate_metadata(struct ltt_session
*session
)
4365 ret
= check_regenerate_metadata_support(session
);
4370 if (session
->kernel_session
) {
4371 ret
= kernctl_session_regenerate_metadata(
4372 session
->kernel_session
->fd
);
4374 ERR("Failed to regenerate the kernel metadata");
4379 if (session
->ust_session
) {
4380 ret
= ust_regenerate_metadata(session
->ust_session
);
4382 ERR("Failed to regenerate the UST metadata");
4386 DBG("Cmd metadata regenerate for session %s", session
->name
);
4394 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4396 * Ask the tracer to regenerate a new statedump.
4398 * Return 0 on success or else a LTTNG_ERR code.
4400 int cmd_regenerate_statedump(struct ltt_session
*session
)
4406 if (!session
->active
) {
4407 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4411 if (session
->kernel_session
) {
4412 ret
= kernctl_session_regenerate_statedump(
4413 session
->kernel_session
->fd
);
4415 * Currently, the statedump in kernel can only fail if out
4419 if (ret
== -ENOMEM
) {
4420 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4422 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4424 ERR("Failed to regenerate the kernel statedump");
4429 if (session
->ust_session
) {
4430 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4432 * Currently, the statedump in UST always returns 0.
4435 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4436 ERR("Failed to regenerate the UST statedump");
4440 DBG("Cmd regenerate statedump for session %s", session
->name
);
4447 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4448 struct notification_thread_handle
*notification_thread
)
4452 ssize_t sock_recv_len
;
4453 struct lttng_trigger
*trigger
= NULL
;
4454 struct lttng_buffer_view view
;
4455 struct lttng_dynamic_buffer trigger_buffer
;
4457 lttng_dynamic_buffer_init(&trigger_buffer
);
4458 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4459 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4461 ret
= LTTNG_ERR_NOMEM
;
4465 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4467 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4468 ERR("Failed to receive \"register trigger\" command payload");
4469 /* TODO: should this be a new error enum ? */
4470 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4474 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4475 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4477 ERR("Invalid trigger payload received in \"register trigger\" command");
4478 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4482 ret
= notification_thread_command_register_trigger(notification_thread
,
4484 /* Ownership of trigger was transferred. */
4487 lttng_trigger_destroy(trigger
);
4488 lttng_dynamic_buffer_reset(&trigger_buffer
);
4492 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4493 struct notification_thread_handle
*notification_thread
)
4497 ssize_t sock_recv_len
;
4498 struct lttng_trigger
*trigger
= NULL
;
4499 struct lttng_buffer_view view
;
4500 struct lttng_dynamic_buffer trigger_buffer
;
4502 lttng_dynamic_buffer_init(&trigger_buffer
);
4503 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4504 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4506 ret
= LTTNG_ERR_NOMEM
;
4510 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4512 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4513 ERR("Failed to receive \"unregister trigger\" command payload");
4514 /* TODO: should this be a new error enum ? */
4515 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4519 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4520 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4522 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4523 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4527 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4530 lttng_trigger_destroy(trigger
);
4531 lttng_dynamic_buffer_reset(&trigger_buffer
);
4536 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4537 * snapshot output is *not* set with a remote destination.
4539 * Return LTTNG_OK on success or a LTTNG_ERR code.
4541 static enum lttng_error_code
set_relayd_for_snapshot(
4542 struct consumer_output
*output
,
4543 const struct ltt_session
*session
)
4545 enum lttng_error_code status
= LTTNG_OK
;
4546 struct lttng_ht_iter iter
;
4547 struct consumer_socket
*socket
;
4548 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4549 const char *base_path
;
4554 DBG2("Set relayd object from snapshot output");
4556 if (session
->current_trace_chunk
) {
4557 enum lttng_trace_chunk_status chunk_status
=
4558 lttng_trace_chunk_get_id(
4559 session
->current_trace_chunk
,
4560 ¤t_chunk_id
.value
);
4562 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4563 current_chunk_id
.is_set
= true;
4565 ERR("Failed to get current trace chunk id");
4566 status
= LTTNG_ERR_UNK
;
4571 /* Ignore if snapshot consumer output is not network. */
4572 if (output
->type
!= CONSUMER_DST_NET
) {
4577 * The snapshot record URI base path overrides the session
4580 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4581 base_path
= output
->dst
.net
.control
.subdir
;
4583 base_path
= session
->base_path
;
4587 * For each consumer socket, create and send the relayd object of the
4591 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4592 socket
, node
.node
) {
4593 pthread_mutex_lock(socket
->lock
);
4594 status
= send_consumer_relayd_sockets(0, session
->id
,
4596 session
->name
, session
->hostname
,
4598 session
->live_timer
,
4599 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4600 session
->creation_time
,
4601 session
->name_contains_creation_time
);
4602 pthread_mutex_unlock(socket
->lock
);
4603 if (status
!= LTTNG_OK
) {
4615 * Record a kernel snapshot.
4617 * Return LTTNG_OK on success or a LTTNG_ERR code.
4619 static enum lttng_error_code
record_kernel_snapshot(
4620 struct ltt_kernel_session
*ksess
,
4621 const struct consumer_output
*output
,
4622 const struct ltt_session
*session
,
4623 int wait
, uint64_t nb_packets_per_stream
)
4625 enum lttng_error_code status
;
4631 status
= kernel_snapshot_record(
4632 ksess
, output
, wait
, nb_packets_per_stream
);
4637 * Record a UST snapshot.
4639 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4641 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4642 const struct consumer_output
*output
,
4643 const struct ltt_session
*session
,
4644 int wait
, uint64_t nb_packets_per_stream
)
4646 enum lttng_error_code status
;
4652 status
= ust_app_snapshot_record(
4653 usess
, output
, wait
, nb_packets_per_stream
);
4658 uint64_t get_session_size_one_more_packet_per_stream(
4659 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4661 uint64_t tot_size
= 0;
4663 if (session
->kernel_session
) {
4664 struct ltt_kernel_channel
*chan
;
4665 const struct ltt_kernel_session
*ksess
=
4666 session
->kernel_session
;
4668 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4669 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4671 * Don't take channel into account if we
4672 * already grab all its packets.
4676 tot_size
+= chan
->channel
->attr
.subbuf_size
4677 * chan
->stream_count
;
4681 if (session
->ust_session
) {
4682 const struct ltt_ust_session
*usess
= session
->ust_session
;
4684 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4692 * Calculate the number of packets we can grab from each stream that
4693 * fits within the overall snapshot max size.
4695 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4696 * the number of packets per stream.
4698 * TODO: this approach is not perfect: we consider the worse case
4699 * (packet filling the sub-buffers) as an upper bound, but we could do
4700 * better if we do this calculation while we actually grab the packet
4701 * content: we would know how much padding we don't actually store into
4704 * This algorithm is currently bounded by the number of packets per
4707 * Since we call this algorithm before actually grabbing the data, it's
4708 * an approximation: for instance, applications could appear/disappear
4709 * in between this call and actually grabbing data.
4712 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4716 uint64_t cur_nb_packets
= 0;
4719 return 0; /* Infinite */
4722 size_left
= max_size
;
4724 uint64_t one_more_packet_tot_size
;
4726 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4727 session
, cur_nb_packets
);
4728 if (!one_more_packet_tot_size
) {
4729 /* We are already grabbing all packets. */
4732 size_left
-= one_more_packet_tot_size
;
4733 if (size_left
< 0) {
4738 if (!cur_nb_packets
&& size_left
!= max_size
) {
4739 /* Not enough room to grab one packet of each stream, error. */
4742 return cur_nb_packets
;
4746 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4747 const struct snapshot_output
*snapshot_output
, int wait
)
4749 int64_t nb_packets_per_stream
;
4750 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4752 enum lttng_error_code ret_code
= LTTNG_OK
;
4753 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4754 struct consumer_output
*original_ust_consumer_output
= NULL
;
4755 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4756 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4757 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4759 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4761 snapshot_output
->name
,
4762 snapshot_output
->datetime
,
4763 snapshot_output
->nb_snapshot
);
4764 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4765 ERR("Failed to format snapshot name");
4766 ret_code
= LTTNG_ERR_INVALID
;
4769 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4770 snapshot_output
->name
, session
->name
,
4771 snapshot_chunk_name
);
4772 if (!session
->kernel_session
&& !session
->ust_session
) {
4773 ERR("Failed to record snapshot as no channels exist");
4774 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4778 if (session
->kernel_session
) {
4779 original_kernel_consumer_output
=
4780 session
->kernel_session
->consumer
;
4781 snapshot_kernel_consumer_output
=
4782 consumer_copy_output(snapshot_output
->consumer
);
4783 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
4784 snapshot_chunk_name
);
4786 /* Copy the original domain subdir. */
4787 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
4788 original_kernel_consumer_output
->domain_subdir
);
4790 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4791 original_kernel_consumer_output
);
4793 ERR("Failed to copy consumer sockets from snapshot output configuration");
4794 ret_code
= LTTNG_ERR_NOMEM
;
4797 ret_code
= set_relayd_for_snapshot(
4798 snapshot_kernel_consumer_output
, session
);
4799 if (ret_code
!= LTTNG_OK
) {
4800 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4803 session
->kernel_session
->consumer
=
4804 snapshot_kernel_consumer_output
;
4806 if (session
->ust_session
) {
4807 original_ust_consumer_output
= session
->ust_session
->consumer
;
4808 snapshot_ust_consumer_output
=
4809 consumer_copy_output(snapshot_output
->consumer
);
4810 strcpy(snapshot_ust_consumer_output
->chunk_path
,
4811 snapshot_chunk_name
);
4813 /* Copy the original domain subdir. */
4814 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
4815 original_ust_consumer_output
->domain_subdir
);
4817 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4818 original_ust_consumer_output
);
4820 ERR("Failed to copy consumer sockets from snapshot output configuration");
4821 ret_code
= LTTNG_ERR_NOMEM
;
4824 ret_code
= set_relayd_for_snapshot(
4825 snapshot_ust_consumer_output
, session
);
4826 if (ret_code
!= LTTNG_OK
) {
4827 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4830 session
->ust_session
->consumer
=
4831 snapshot_ust_consumer_output
;
4834 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4835 snapshot_kernel_consumer_output
?:
4836 snapshot_ust_consumer_output
,
4837 consumer_output_get_base_path(
4838 snapshot_output
->consumer
),
4839 snapshot_chunk_name
);
4840 if (!snapshot_trace_chunk
) {
4841 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4843 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4846 assert(!session
->current_trace_chunk
);
4847 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4848 lttng_trace_chunk_put(snapshot_trace_chunk
);
4849 snapshot_trace_chunk
= NULL
;
4851 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4853 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4857 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4858 snapshot_output
->max_size
);
4859 if (nb_packets_per_stream
< 0) {
4860 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4861 goto error_close_trace_chunk
;
4864 if (session
->kernel_session
) {
4865 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4866 snapshot_kernel_consumer_output
, session
,
4867 wait
, nb_packets_per_stream
);
4868 if (ret_code
!= LTTNG_OK
) {
4869 goto error_close_trace_chunk
;
4873 if (session
->ust_session
) {
4874 ret_code
= record_ust_snapshot(session
->ust_session
,
4875 snapshot_ust_consumer_output
, session
,
4876 wait
, nb_packets_per_stream
);
4877 if (ret_code
!= LTTNG_OK
) {
4878 goto error_close_trace_chunk
;
4882 error_close_trace_chunk
:
4883 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
4884 ERR("Failed to release the current trace chunk of session \"%s\"",
4886 ret_code
= LTTNG_ERR_UNK
;
4889 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
4890 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
4892 * Don't goto end; make sure the chunk is closed for the session
4893 * to allow future snapshots.
4895 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4897 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4900 lttng_trace_chunk_put(snapshot_trace_chunk
);
4901 snapshot_trace_chunk
= NULL
;
4903 if (original_ust_consumer_output
) {
4904 session
->ust_session
->consumer
= original_ust_consumer_output
;
4906 if (original_kernel_consumer_output
) {
4907 session
->kernel_session
->consumer
=
4908 original_kernel_consumer_output
;
4910 consumer_output_put(snapshot_ust_consumer_output
);
4911 consumer_output_put(snapshot_kernel_consumer_output
);
4916 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4918 * The wait parameter is ignored so this call always wait for the snapshot to
4919 * complete before returning.
4921 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4923 int cmd_snapshot_record(struct ltt_session
*session
,
4924 const struct lttng_snapshot_output
*output
, int wait
)
4926 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4928 unsigned int snapshot_success
= 0;
4930 struct snapshot_output
*tmp_output
= NULL
;
4935 DBG("Cmd snapshot record for session %s", session
->name
);
4937 /* Get the datetime for the snapshot output directory. */
4938 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4941 cmd_ret
= LTTNG_ERR_INVALID
;
4946 * Permission denied to create an output if the session is not
4947 * set in no output mode.
4949 if (session
->output_traces
) {
4950 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4954 /* The session needs to be started at least once. */
4955 if (!session
->has_been_started
) {
4956 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4960 /* Use temporary output for the session. */
4961 if (*output
->ctrl_url
!= '\0') {
4962 tmp_output
= snapshot_output_alloc();
4964 cmd_ret
= LTTNG_ERR_NOMEM
;
4968 ret
= snapshot_output_init(session
, output
->max_size
,
4970 output
->ctrl_url
, output
->data_url
,
4974 if (ret
== -ENOMEM
) {
4975 cmd_ret
= LTTNG_ERR_NOMEM
;
4977 cmd_ret
= LTTNG_ERR_INVALID
;
4981 /* Use the global session count for the temporary snapshot. */
4982 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
4984 /* Use the global datetime */
4985 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
4986 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
4987 if (cmd_ret
!= LTTNG_OK
) {
4990 snapshot_success
= 1;
4992 struct snapshot_output
*sout
;
4993 struct lttng_ht_iter iter
;
4996 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4997 &iter
.iter
, sout
, node
.node
) {
4998 struct snapshot_output output_copy
;
5001 * Make a local copy of the output and override output
5002 * parameters with those provided as part of the
5005 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5007 if (output
->max_size
!= (uint64_t) -1ULL) {
5008 output_copy
.max_size
= output
->max_size
;
5011 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5012 memcpy(output_copy
.datetime
, datetime
,
5015 /* Use temporary name. */
5016 if (*output
->name
!= '\0') {
5017 if (lttng_strncpy(output_copy
.name
,
5019 sizeof(output_copy
.name
))) {
5020 cmd_ret
= LTTNG_ERR_INVALID
;
5026 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
5027 if (cmd_ret
!= LTTNG_OK
) {
5031 snapshot_success
= 1;
5036 if (snapshot_success
) {
5037 session
->snapshot
.nb_snapshot
++;
5039 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5044 snapshot_output_destroy(tmp_output
);
5050 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5052 int cmd_set_session_shm_path(struct ltt_session
*session
,
5053 const char *shm_path
)
5059 * Can only set shm path before session is started.
5061 if (session
->has_been_started
) {
5062 return LTTNG_ERR_SESSION_STARTED
;
5065 strncpy(session
->shm_path
, shm_path
,
5066 sizeof(session
->shm_path
));
5067 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5073 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5075 * Ask the consumer to rotate the session output directory.
5076 * The session lock must be held.
5078 * Returns LTTNG_OK on success or else a negative LTTng error code.
5080 int cmd_rotate_session(struct ltt_session
*session
,
5081 struct lttng_rotate_session_return
*rotate_return
,
5082 bool quiet_rotation
,
5083 enum lttng_trace_chunk_command_type command
)
5086 uint64_t ongoing_rotation_chunk_id
;
5087 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5088 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5089 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5090 enum lttng_trace_chunk_status chunk_status
;
5091 bool failed_to_rotate
= false;
5092 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5096 if (!session
->has_been_started
) {
5097 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5102 * Explicit rotation is not supported for live sessions.
5103 * However, live sessions can perform a quiet rotation on
5105 * Rotation is not supported for snapshot traces (no output).
5107 if ((!quiet_rotation
&& session
->live_timer
) ||
5108 !session
->output_traces
) {
5109 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5113 /* Unsupported feature in lttng-relayd before 2.11. */
5114 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5115 (session
->consumer
->relay_major_version
== 2 &&
5116 session
->consumer
->relay_minor_version
< 11)) {
5117 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5121 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5122 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5123 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5127 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5128 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5130 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5135 * After a stop, we only allow one rotation to occur, the other ones are
5136 * useless until a new start.
5138 if (session
->rotated_after_last_stop
) {
5139 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5141 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5146 * After a stop followed by a clear, disallow following rotations a they would
5147 * generate empty chunks.
5149 if (session
->cleared_after_last_stop
) {
5150 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5152 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5156 if (session
->active
) {
5157 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5159 if (!new_trace_chunk
) {
5160 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5166 * The current trace chunk becomes the chunk being archived.
5168 * After this point, "chunk_being_archived" must absolutely
5169 * be closed on the consumer(s), otherwise it will never be
5170 * cleaned-up, which will result in a leak.
5172 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5173 &chunk_being_archived
);
5175 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5179 if (session
->kernel_session
) {
5180 cmd_ret
= kernel_rotate_session(session
);
5181 if (cmd_ret
!= LTTNG_OK
) {
5182 failed_to_rotate
= true;
5183 rotation_fail_code
= cmd_ret
;
5186 if (session
->ust_session
) {
5187 cmd_ret
= ust_app_rotate_session(session
);
5188 if (cmd_ret
!= LTTNG_OK
) {
5189 failed_to_rotate
= true;
5190 rotation_fail_code
= cmd_ret
;
5194 if (!session
->active
) {
5195 session
->rotated_after_last_stop
= true;
5198 if (!chunk_being_archived
) {
5199 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5201 if (failed_to_rotate
) {
5202 cmd_ret
= rotation_fail_code
;
5209 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5210 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5211 &ongoing_rotation_chunk_id
);
5212 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5214 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5215 command
, session
->last_chunk_path
);
5217 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5221 if (failed_to_rotate
) {
5222 cmd_ret
= rotation_fail_code
;
5226 session
->quiet_rotation
= quiet_rotation
;
5227 ret
= timer_session_rotation_pending_check_start(session
,
5228 DEFAULT_ROTATE_PENDING_TIMER
);
5230 cmd_ret
= LTTNG_ERR_UNK
;
5234 if (rotate_return
) {
5235 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5238 session
->chunk_being_archived
= chunk_being_archived
;
5239 chunk_being_archived
= NULL
;
5240 if (!quiet_rotation
) {
5241 ret
= notification_thread_command_session_rotation_ongoing(
5242 notification_thread_handle
,
5243 session
->name
, session
->uid
, session
->gid
,
5244 ongoing_rotation_chunk_id
);
5245 if (ret
!= LTTNG_OK
) {
5246 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5252 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5253 session
->name
, ongoing_rotation_chunk_id
);
5255 lttng_trace_chunk_put(new_trace_chunk
);
5256 lttng_trace_chunk_put(chunk_being_archived
);
5257 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5260 if (session_reset_rotation_state(session
,
5261 LTTNG_ROTATION_STATE_ERROR
)) {
5262 ERR("Failed to reset rotation state of session \"%s\"",
5269 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5271 * Check if the session has finished its rotation.
5273 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5275 int cmd_rotate_get_info(struct ltt_session
*session
,
5276 struct lttng_rotation_get_info_return
*info_return
,
5277 uint64_t rotation_id
)
5279 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5280 enum lttng_rotation_state rotation_state
;
5282 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5283 session
->most_recent_chunk_id
.value
);
5285 if (session
->chunk_being_archived
) {
5286 enum lttng_trace_chunk_status chunk_status
;
5289 chunk_status
= lttng_trace_chunk_get_id(
5290 session
->chunk_being_archived
,
5292 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5294 rotation_state
= rotation_id
== chunk_id
?
5295 LTTNG_ROTATION_STATE_ONGOING
:
5296 LTTNG_ROTATION_STATE_EXPIRED
;
5298 if (session
->last_archived_chunk_id
.is_set
&&
5299 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5300 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5302 rotation_state
= session
->rotation_state
;
5306 switch (rotation_state
) {
5307 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5308 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5311 case LTTNG_ROTATION_STATE_EXPIRED
:
5312 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5313 rotation_id
, session
->name
);
5315 case LTTNG_ROTATION_STATE_ONGOING
:
5316 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5317 rotation_id
, session
->name
);
5319 case LTTNG_ROTATION_STATE_COMPLETED
:
5323 char *current_tracing_path_reply
;
5324 size_t current_tracing_path_reply_len
;
5326 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5327 rotation_id
, session
->name
);
5329 switch (session_get_consumer_destination_type(session
)) {
5330 case CONSUMER_DST_LOCAL
:
5331 current_tracing_path_reply
=
5332 info_return
->location
.local
.absolute_path
;
5333 current_tracing_path_reply_len
=
5334 sizeof(info_return
->location
.local
.absolute_path
);
5335 info_return
->location_type
=
5336 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5337 fmt_ret
= asprintf(&chunk_path
,
5338 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5339 session_get_base_path(session
),
5340 session
->last_archived_chunk_name
);
5341 if (fmt_ret
== -1) {
5342 PERROR("Failed to format the path of the last archived trace chunk");
5343 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5344 cmd_ret
= LTTNG_ERR_UNK
;
5348 case CONSUMER_DST_NET
:
5350 uint16_t ctrl_port
, data_port
;
5352 current_tracing_path_reply
=
5353 info_return
->location
.relay
.relative_path
;
5354 current_tracing_path_reply_len
=
5355 sizeof(info_return
->location
.relay
.relative_path
);
5356 /* Currently the only supported relay protocol. */
5357 info_return
->location
.relay
.protocol
=
5358 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5360 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5361 session_get_net_consumer_hostname(session
),
5362 sizeof(info_return
->location
.relay
.host
));
5364 ERR("Failed to copy host name to rotate_get_info reply");
5365 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5366 cmd_ret
= LTTNG_ERR_SET_URL
;
5370 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5371 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5372 info_return
->location
.relay
.ports
.data
= data_port
;
5373 info_return
->location_type
=
5374 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5375 chunk_path
= strdup(session
->last_chunk_path
);
5377 ERR("Failed to allocate the path of the last archived trace chunk");
5378 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5379 cmd_ret
= LTTNG_ERR_UNK
;
5388 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5389 chunk_path
, current_tracing_path_reply_len
);
5392 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5393 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5394 cmd_ret
= LTTNG_ERR_UNK
;
5400 case LTTNG_ROTATION_STATE_ERROR
:
5401 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5402 rotation_id
, session
->name
);
5410 info_return
->status
= (int32_t) rotation_state
;
5415 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5417 * Configure the automatic rotation parameters.
5418 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5419 * 'activate' to false means deactivate the rotation schedule and validate that
5420 * 'new_value' has the same value as the currently active value.
5422 * Return 0 on success or else a positive LTTNG_ERR code.
5424 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5425 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5427 struct notification_thread_handle
*notification_thread_handle
)
5430 uint64_t *parameter_value
;
5434 DBG("Cmd rotate set schedule session %s", session
->name
);
5436 if (session
->live_timer
|| !session
->output_traces
) {
5437 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5438 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5442 switch (schedule_type
) {
5443 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5444 parameter_value
= &session
->rotate_size
;
5446 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5447 parameter_value
= &session
->rotate_timer_period
;
5448 if (new_value
>= UINT_MAX
) {
5449 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5450 new_value
, UINT_MAX
);
5451 ret
= LTTNG_ERR_INVALID
;
5456 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5457 ret
= LTTNG_ERR_INVALID
;
5461 /* Improper use of the API. */
5462 if (new_value
== -1ULL) {
5463 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5464 ret
= LTTNG_ERR_INVALID
;
5469 * As indicated in struct ltt_session's comments, a value of == 0 means
5470 * this schedule rotation type is not in use.
5472 * Reject the command if we were asked to activate a schedule that was
5475 if (activate
&& *parameter_value
!= 0) {
5476 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5477 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5482 * Reject the command if we were asked to deactivate a schedule that was
5485 if (!activate
&& *parameter_value
== 0) {
5486 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5487 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5492 * Reject the command if we were asked to deactivate a schedule that
5495 if (!activate
&& *parameter_value
!= new_value
) {
5496 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5497 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5501 *parameter_value
= activate
? new_value
: 0;
5503 switch (schedule_type
) {
5504 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5505 if (activate
&& session
->active
) {
5507 * Only start the timer if the session is active,
5508 * otherwise it will be started when the session starts.
5510 ret
= timer_session_rotation_schedule_timer_start(
5511 session
, new_value
);
5513 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5514 ret
= LTTNG_ERR_UNK
;
5518 ret
= timer_session_rotation_schedule_timer_stop(
5521 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5522 ret
= LTTNG_ERR_UNK
;
5527 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5529 ret
= subscribe_session_consumed_size_rotation(session
,
5530 new_value
, notification_thread_handle
);
5532 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5533 ret
= LTTNG_ERR_UNK
;
5537 ret
= unsubscribe_session_consumed_size_rotation(session
,
5538 notification_thread_handle
);
5540 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5541 ret
= LTTNG_ERR_UNK
;
5548 /* Would have been caught before. */
5560 /* Wait for a given path to be removed before continuing. */
5561 static enum lttng_error_code
wait_on_path(void *path_data
)
5563 const char *shm_path
= path_data
;
5565 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5571 ret
= stat(shm_path
, &st
);
5573 if (errno
!= ENOENT
) {
5574 PERROR("stat() returned an error while checking for the existence of the shm path");
5576 DBG("shm path no longer exists, completing the destruction of session");
5580 if (!S_ISDIR(st
.st_mode
)) {
5581 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5586 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5592 * Returns a pointer to a handler to run on completion of a command.
5593 * Returns NULL if no handler has to be run for the last command executed.
5595 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5597 struct cmd_completion_handler
*handler
= current_completion_handler
;
5599 current_completion_handler
= NULL
;
5604 * Init command subsystem.
5609 * Set network sequence index to 1 for streams to match a relayd
5610 * socket on the consumer side.
5612 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5613 relayd_net_seq_idx
= 1;
5614 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5616 DBG("Command subsystem initialized");