2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
15 #include <urcu/list.h>
16 #include <urcu/uatomic.h>
18 #include <common/buffer-view.h>
19 #include <common/common.h>
20 #include <common/compat/string.h>
21 #include <common/defaults.h>
22 #include <common/dynamic-buffer.h>
23 #include <common/kernel-ctl/kernel-ctl.h>
24 #include <common/relayd/relayd.h>
25 #include <common/sessiond-comm/sessiond-comm.h>
26 #include <common/string-utils/string-utils.h>
27 #include <common/trace-chunk.h>
28 #include <common/utils.h>
30 #include <lttng/action/action.h>
31 #include <lttng/action/action-internal.h>
32 #include <lttng/channel-internal.h>
33 #include <lttng/channel.h>
34 #include <lttng/condition/condition.h>
35 #include <lttng/error-query-internal.h>
36 #include <lttng/event-internal.h>
37 #include <lttng/location-internal.h>
38 #include <lttng/rotate-internal.h>
39 #include <lttng/session-descriptor-internal.h>
40 #include <lttng/session-internal.h>
41 #include <lttng/trigger/trigger-internal.h>
42 #include <lttng/userspace-probe-internal.h>
44 #include "agent-thread.h"
46 #include "buffer-registry.h"
50 #include "event-notifier-error-accounting.h"
52 #include "health-sessiond.h"
53 #include "kernel-consumer.h"
55 #include "lttng-sessiond.h"
56 #include "lttng-syscall.h"
57 #include "notification-thread-commands.h"
58 #include "notification-thread.h"
60 #include "rotation-thread.h"
66 /* Sleep for 100ms between each check for the shm path's deletion. */
67 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
69 struct cmd_destroy_session_reply_context
{
71 bool implicit_rotation_on_destroy
;
73 * Indicates whether or not an error occurred while launching the
74 * destruction of a session.
76 enum lttng_error_code destruction_status
;
79 static enum lttng_error_code
wait_on_path(void *path
);
82 * Command completion handler that is used by the destroy command
83 * when a session that has a non-default shm_path is being destroyed.
85 * See comment in cmd_destroy_session() for the rationale.
87 static struct destroy_completion_handler
{
88 struct cmd_completion_handler handler
;
89 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
90 } destroy_completion_handler
= {
93 .data
= destroy_completion_handler
.shm_path
98 static struct cmd_completion_handler
*current_completion_handler
;
101 * Used to keep a unique index for each relayd socket created where this value
102 * is associated with streams on the consumer so it can match the right relayd
103 * to send to. It must be accessed with the relayd_net_seq_idx_lock
106 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
107 static uint64_t relayd_net_seq_idx
;
109 static int validate_ust_event_name(const char *);
110 static int cmd_enable_event_internal(struct ltt_session
*session
,
111 const struct lttng_domain
*domain
,
112 char *channel_name
, struct lttng_event
*event
,
113 char *filter_expression
,
114 struct lttng_bytecode
*filter
,
115 struct lttng_event_exclusion
*exclusion
,
117 static int cmd_enable_channel_internal(struct ltt_session
*session
,
118 const struct lttng_domain
*domain
,
119 const struct lttng_channel
*_attr
,
123 * Create a session path used by list_lttng_sessions for the case that the
124 * session consumer is on the network.
126 static int build_network_session_path(char *dst
, size_t size
,
127 struct ltt_session
*session
)
129 int ret
, kdata_port
, udata_port
;
130 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
131 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
136 memset(tmp_urls
, 0, sizeof(tmp_urls
));
137 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
139 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
141 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
142 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
143 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
146 if (session
->ust_session
&& session
->ust_session
->consumer
) {
147 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
148 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
151 if (uuri
== NULL
&& kuri
== NULL
) {
152 uri
= &session
->consumer
->dst
.net
.control
;
153 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
154 } else if (kuri
&& uuri
) {
155 ret
= uri_compare(kuri
, uuri
);
159 /* Build uuri URL string */
160 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
167 } else if (kuri
&& uuri
== NULL
) {
169 } else if (uuri
&& kuri
== NULL
) {
173 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
179 * Do we have a UST url set. If yes, this means we have both kernel and UST
182 if (*tmp_uurl
!= '\0') {
183 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
184 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
187 if (kuri
|| (!kuri
&& !uuri
)) {
190 /* No kernel URI, use the UST port. */
193 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
201 * Get run-time attributes if the session has been started (discarded events,
204 static int get_kernel_runtime_stats(struct ltt_session
*session
,
205 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
206 uint64_t *lost_packets
)
210 if (!session
->has_been_started
) {
212 *discarded_events
= 0;
217 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
218 session
->kernel_session
->consumer
,
224 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
225 session
->kernel_session
->consumer
,
236 * Get run-time attributes if the session has been started (discarded events,
239 static int get_ust_runtime_stats(struct ltt_session
*session
,
240 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
241 uint64_t *lost_packets
)
244 struct ltt_ust_session
*usess
;
246 if (!discarded_events
|| !lost_packets
) {
251 usess
= session
->ust_session
;
252 assert(discarded_events
);
253 assert(lost_packets
);
255 if (!usess
|| !session
->has_been_started
) {
256 *discarded_events
= 0;
262 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
263 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
264 &usess
->buffer_reg_uid_list
,
265 usess
->consumer
, uchan
->id
,
266 uchan
->attr
.overwrite
,
269 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
270 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
271 uchan
, usess
->consumer
,
272 uchan
->attr
.overwrite
,
278 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
279 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
281 ERR("Unsupported buffer type");
292 * Create a list of agent domain events.
294 * Return number of events in list on success or else a negative value.
296 static enum lttng_error_code
list_lttng_agent_events(
297 struct agent
*agt
, struct lttng_payload
*reply_payload
,
298 unsigned int *nb_events
)
300 enum lttng_error_code ret_code
;
302 unsigned int local_nb_events
= 0;
303 struct agent_event
*event
;
304 struct lttng_ht_iter iter
;
305 unsigned long agent_event_count
;
308 assert(reply_payload
);
310 DBG3("Listing agent events");
314 agent_event_count
= lttng_ht_get_count(agt
->events
);
315 if (agent_event_count
== 0) {
320 if (agent_event_count
> UINT_MAX
) {
321 ret_code
= LTTNG_ERR_OVERFLOW
;
325 local_nb_events
= (unsigned int) agent_event_count
;
327 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
328 struct lttng_event
*tmp_event
= lttng_event_create();
331 ret_code
= LTTNG_ERR_NOMEM
;
335 if (lttng_strncpy(tmp_event
->name
, event
->name
, sizeof(tmp_event
->name
))) {
336 lttng_event_destroy(tmp_event
);
337 ret_code
= LTTNG_ERR_FATAL
;
341 tmp_event
->name
[sizeof(tmp_event
->name
) - 1] = '\0';
342 tmp_event
->enabled
= !!event
->enabled_count
;
343 tmp_event
->loglevel
= event
->loglevel_value
;
344 tmp_event
->loglevel_type
= event
->loglevel_type
;
346 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
347 event
->filter_expression
, 0, NULL
, reply_payload
);
348 lttng_event_destroy(tmp_event
);
350 ret_code
= LTTNG_ERR_FATAL
;
357 *nb_events
= local_nb_events
;
364 * Create a list of ust global domain events.
366 static enum lttng_error_code
list_lttng_ust_global_events(char *channel_name
,
367 struct ltt_ust_domain_global
*ust_global
,
368 struct lttng_payload
*reply_payload
,
369 unsigned int *nb_events
)
371 enum lttng_error_code ret_code
;
373 struct lttng_ht_iter iter
;
374 struct lttng_ht_node_str
*node
;
375 struct ltt_ust_channel
*uchan
;
376 struct ltt_ust_event
*uevent
;
377 unsigned long channel_event_count
;
378 unsigned int local_nb_events
= 0;
379 struct lttng_dynamic_pointer_array exclusion_names
;
381 assert(reply_payload
);
384 lttng_dynamic_pointer_array_init(&exclusion_names
, NULL
);
386 DBG("Listing UST global events for channel %s", channel_name
);
390 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
391 node
= lttng_ht_iter_get_node_str(&iter
);
393 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
397 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
399 channel_event_count
= lttng_ht_get_count(uchan
->events
);
400 if (channel_event_count
== 0) {
406 if (channel_event_count
> UINT_MAX
) {
407 ret_code
= LTTNG_ERR_OVERFLOW
;
411 local_nb_events
= (unsigned int) channel_event_count
;
413 DBG3("Listing UST global %d events", *nb_events
);
415 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
416 struct lttng_event
*tmp_event
= NULL
;
418 if (uevent
->internal
) {
419 /* This event should remain hidden from clients */
424 tmp_event
= lttng_event_create();
426 ret_code
= LTTNG_ERR_NOMEM
;
430 if (lttng_strncpy(tmp_event
->name
, uevent
->attr
.name
,
431 LTTNG_SYMBOL_NAME_LEN
)) {
432 ret_code
= LTTNG_ERR_FATAL
;
433 lttng_event_destroy(tmp_event
);
437 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
438 tmp_event
->enabled
= uevent
->enabled
;
440 switch (uevent
->attr
.instrumentation
) {
441 case LTTNG_UST_ABI_TRACEPOINT
:
442 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
444 case LTTNG_UST_ABI_PROBE
:
445 tmp_event
->type
= LTTNG_EVENT_PROBE
;
447 case LTTNG_UST_ABI_FUNCTION
:
448 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
452 tmp_event
->loglevel
= uevent
->attr
.loglevel
;
453 switch (uevent
->attr
.loglevel_type
) {
454 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
455 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
457 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
458 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
460 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
461 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
464 if (uevent
->filter
) {
465 tmp_event
->filter
= 1;
467 if (uevent
->exclusion
) {
468 tmp_event
->exclusion
= 1;
471 if (uevent
->exclusion
) {
474 for (i
= 0; i
< uevent
->exclusion
->count
; i
++) {
475 const int add_ret
= lttng_dynamic_pointer_array_add_pointer(
477 LTTNG_EVENT_EXCLUSION_NAME_AT(uevent
->exclusion
, i
));
480 PERROR("Failed to add exclusion name to temporary serialization array");
481 ret_code
= LTTNG_ERR_NOMEM
;
488 * We do not care about the filter bytecode and the fd from the
489 * userspace_probe_location.
491 ret
= lttng_event_serialize(tmp_event
,
492 lttng_dynamic_pointer_array_get_count(&exclusion_names
),
493 lttng_dynamic_pointer_array_get_count(&exclusion_names
) ?
494 (char **) exclusion_names
.array
.buffer
.data
: NULL
,
495 uevent
->filter_expression
, 0, NULL
,
497 lttng_event_destroy(tmp_event
);
498 lttng_dynamic_pointer_array_clear(&exclusion_names
);
500 ret_code
= LTTNG_ERR_FATAL
;
506 /* nb_events is already set at this point. */
508 *nb_events
= local_nb_events
;
510 lttng_dynamic_pointer_array_reset(&exclusion_names
);
516 * Fill lttng_event array of all kernel events in the channel.
518 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
519 struct ltt_kernel_session
*kernel_session
,
520 struct lttng_payload
*reply_payload
,
521 unsigned int *nb_events
)
523 enum lttng_error_code ret_code
;
525 struct ltt_kernel_event
*event
;
526 struct ltt_kernel_channel
*kchan
;
528 assert(reply_payload
);
530 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
532 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
536 *nb_events
= kchan
->event_count
;
538 DBG("Listing events for channel %s", kchan
->channel
->name
);
540 if (*nb_events
== 0) {
545 /* Kernel channels */
546 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
547 struct lttng_event
*tmp_event
= lttng_event_create();
550 ret_code
= LTTNG_ERR_NOMEM
;
554 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
555 lttng_event_destroy(tmp_event
);
556 ret_code
= LTTNG_ERR_FATAL
;
561 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
562 tmp_event
->enabled
= event
->enabled
;
563 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
565 switch (event
->event
->instrumentation
) {
566 case LTTNG_KERNEL_ABI_TRACEPOINT
:
567 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
569 case LTTNG_KERNEL_ABI_KRETPROBE
:
570 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
571 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
572 sizeof(struct lttng_kernel_abi_kprobe
));
574 case LTTNG_KERNEL_ABI_KPROBE
:
575 tmp_event
->type
= LTTNG_EVENT_PROBE
;
576 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
577 sizeof(struct lttng_kernel_abi_kprobe
));
579 case LTTNG_KERNEL_ABI_UPROBE
:
580 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
582 case LTTNG_KERNEL_ABI_FUNCTION
:
583 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
584 memcpy(&(tmp_event
->attr
.ftrace
), &event
->event
->u
.ftrace
,
585 sizeof(struct lttng_kernel_abi_function
));
587 case LTTNG_KERNEL_ABI_NOOP
:
588 tmp_event
->type
= LTTNG_EVENT_NOOP
;
590 case LTTNG_KERNEL_ABI_SYSCALL
:
591 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
593 case LTTNG_KERNEL_ABI_ALL
:
600 if (event
->userspace_probe_location
) {
601 struct lttng_userspace_probe_location
*location_copy
=
602 lttng_userspace_probe_location_copy(
603 event
->userspace_probe_location
);
605 if (!location_copy
) {
606 lttng_event_destroy(tmp_event
);
607 ret_code
= LTTNG_ERR_NOMEM
;
611 ret
= lttng_event_set_userspace_probe_location(
612 tmp_event
, location_copy
);
614 lttng_event_destroy(tmp_event
);
615 lttng_userspace_probe_location_destroy(
617 ret_code
= LTTNG_ERR_INVALID
;
622 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
623 event
->filter_expression
, 0, NULL
, reply_payload
);
624 lttng_event_destroy(tmp_event
);
626 ret_code
= LTTNG_ERR_FATAL
;
637 * Add URI so the consumer output object. Set the correct path depending on the
638 * domain adding the default trace directory.
640 static enum lttng_error_code
add_uri_to_consumer(
641 const struct ltt_session
*session
,
642 struct consumer_output
*consumer
,
643 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
646 enum lttng_error_code ret_code
= LTTNG_OK
;
650 if (consumer
== NULL
) {
651 DBG("No consumer detected. Don't add URI. Stopping.");
652 ret_code
= LTTNG_ERR_NO_CONSUMER
;
657 case LTTNG_DOMAIN_KERNEL
:
658 ret
= lttng_strncpy(consumer
->domain_subdir
,
659 DEFAULT_KERNEL_TRACE_DIR
,
660 sizeof(consumer
->domain_subdir
));
662 case LTTNG_DOMAIN_UST
:
663 ret
= lttng_strncpy(consumer
->domain_subdir
,
664 DEFAULT_UST_TRACE_DIR
,
665 sizeof(consumer
->domain_subdir
));
669 * This case is possible is we try to add the URI to the global
670 * tracing session consumer object which in this case there is
673 memset(consumer
->domain_subdir
, 0,
674 sizeof(consumer
->domain_subdir
));
678 ERR("Failed to initialize consumer output domain subdirectory");
679 ret_code
= LTTNG_ERR_FATAL
;
683 switch (uri
->dtype
) {
686 DBG2("Setting network URI to consumer");
688 if (consumer
->type
== CONSUMER_DST_NET
) {
689 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
690 consumer
->dst
.net
.control_isset
) ||
691 (uri
->stype
== LTTNG_STREAM_DATA
&&
692 consumer
->dst
.net
.data_isset
)) {
693 ret_code
= LTTNG_ERR_URL_EXIST
;
697 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
700 /* Set URI into consumer output object */
701 ret
= consumer_set_network_uri(session
, consumer
, uri
);
705 } else if (ret
== 1) {
707 * URI was the same in the consumer so we do not append the subdir
708 * again so to not duplicate output dir.
715 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
716 ret_code
= LTTNG_ERR_INVALID
;
719 DBG2("Setting trace directory path from URI to %s",
721 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
723 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
725 sizeof(consumer
->dst
.session_root_path
));
727 ret_code
= LTTNG_ERR_FATAL
;
730 consumer
->type
= CONSUMER_DST_LOCAL
;
740 * Init tracing by creating trace directory and sending fds kernel consumer.
742 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
745 struct lttng_ht_iter iter
;
746 struct consumer_socket
*socket
;
752 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
753 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
755 pthread_mutex_lock(socket
->lock
);
756 ret
= kernel_consumer_send_session(socket
, session
);
757 pthread_mutex_unlock(socket
->lock
);
759 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
771 * Create a socket to the relayd using the URI.
773 * On success, the relayd_sock pointer is set to the created socket.
774 * Else, it remains untouched and an LTTng error code is returned.
776 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
777 struct lttcomm_relayd_sock
**relayd_sock
,
778 struct consumer_output
*consumer
)
781 enum lttng_error_code status
= LTTNG_OK
;
782 struct lttcomm_relayd_sock
*rsock
;
784 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
785 RELAYD_VERSION_COMM_MINOR
);
787 status
= LTTNG_ERR_FATAL
;
792 * Connect to relayd so we can proceed with a session creation. This call
793 * can possibly block for an arbitrary amount of time to set the health
794 * state to be in poll execution.
797 ret
= relayd_connect(rsock
);
800 ERR("Unable to reach lttng-relayd");
801 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
805 /* Create socket for control stream. */
806 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
807 uint64_t result_flags
;
809 DBG3("Creating relayd stream socket from URI");
811 /* Check relayd version */
812 ret
= relayd_version_check(rsock
);
813 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
814 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
816 } else if (ret
< 0) {
817 ERR("Unable to reach lttng-relayd");
818 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
821 consumer
->relay_major_version
= rsock
->major
;
822 consumer
->relay_minor_version
= rsock
->minor
;
823 ret
= relayd_get_configuration(rsock
, 0,
826 ERR("Unable to get relayd configuration");
827 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
830 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
831 consumer
->relay_allows_clear
= true;
833 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
834 DBG3("Creating relayd data socket from URI");
836 /* Command is not valid */
837 ERR("Relayd invalid stream type: %d", uri
->stype
);
838 status
= LTTNG_ERR_INVALID
;
842 *relayd_sock
= rsock
;
847 /* The returned value is not useful since we are on an error path. */
848 (void) relayd_close(rsock
);
856 * Connect to the relayd using URI and send the socket to the right consumer.
858 * The consumer socket lock must be held by the caller.
860 * Returns LTTNG_OK on success or an LTTng error code on failure.
862 static enum lttng_error_code
send_consumer_relayd_socket(
863 unsigned int session_id
,
864 struct lttng_uri
*relayd_uri
,
865 struct consumer_output
*consumer
,
866 struct consumer_socket
*consumer_sock
,
867 const char *session_name
, const char *hostname
,
868 const char *base_path
, int session_live_timer
,
869 const uint64_t *current_chunk_id
,
870 time_t session_creation_time
,
871 bool session_name_contains_creation_time
)
874 struct lttcomm_relayd_sock
*rsock
= NULL
;
875 enum lttng_error_code status
;
877 /* Connect to relayd and make version check if uri is the control. */
878 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
879 if (status
!= LTTNG_OK
) {
880 goto relayd_comm_error
;
884 /* Set the network sequence index if not set. */
885 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
886 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
888 * Increment net_seq_idx because we are about to transfer the
889 * new relayd socket to the consumer.
890 * Assign unique key so the consumer can match streams.
892 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
893 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
896 /* Send relayd socket to consumer. */
897 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
898 relayd_uri
->stype
, session_id
,
899 session_name
, hostname
, base_path
,
900 session_live_timer
, current_chunk_id
,
901 session_creation_time
, session_name_contains_creation_time
);
903 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
907 /* Flag that the corresponding socket was sent. */
908 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
909 consumer_sock
->control_sock_sent
= 1;
910 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
911 consumer_sock
->data_sock_sent
= 1;
915 * Close socket which was dup on the consumer side. The session daemon does
916 * NOT keep track of the relayd socket(s) once transfer to the consumer.
920 if (status
!= LTTNG_OK
) {
922 * The consumer output for this session should not be used anymore
923 * since the relayd connection failed thus making any tracing or/and
924 * streaming not usable.
926 consumer
->enabled
= 0;
928 (void) relayd_close(rsock
);
936 * Send both relayd sockets to a specific consumer and domain. This is a
937 * helper function to facilitate sending the information to the consumer for a
940 * The consumer socket lock must be held by the caller.
942 * Returns LTTNG_OK, or an LTTng error code on failure.
944 static enum lttng_error_code
send_consumer_relayd_sockets(
945 enum lttng_domain_type domain
,
946 unsigned int session_id
, struct consumer_output
*consumer
,
947 struct consumer_socket
*sock
, const char *session_name
,
948 const char *hostname
, const char *base_path
, int session_live_timer
,
949 const uint64_t *current_chunk_id
, time_t session_creation_time
,
950 bool session_name_contains_creation_time
)
952 enum lttng_error_code status
= LTTNG_OK
;
957 /* Sending control relayd socket. */
958 if (!sock
->control_sock_sent
) {
959 status
= send_consumer_relayd_socket(session_id
,
960 &consumer
->dst
.net
.control
, consumer
, sock
,
961 session_name
, hostname
, base_path
, session_live_timer
,
962 current_chunk_id
, session_creation_time
,
963 session_name_contains_creation_time
);
964 if (status
!= LTTNG_OK
) {
969 /* Sending data relayd socket. */
970 if (!sock
->data_sock_sent
) {
971 status
= send_consumer_relayd_socket(session_id
,
972 &consumer
->dst
.net
.data
, consumer
, sock
,
973 session_name
, hostname
, base_path
, session_live_timer
,
974 current_chunk_id
, session_creation_time
,
975 session_name_contains_creation_time
);
976 if (status
!= LTTNG_OK
) {
986 * Setup relayd connections for a tracing session. First creates the socket to
987 * the relayd and send them to the right domain consumer. Consumer type MUST be
990 int cmd_setup_relayd(struct ltt_session
*session
)
993 struct ltt_ust_session
*usess
;
994 struct ltt_kernel_session
*ksess
;
995 struct consumer_socket
*socket
;
996 struct lttng_ht_iter iter
;
997 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1001 usess
= session
->ust_session
;
1002 ksess
= session
->kernel_session
;
1004 DBG("Setting relayd for session %s", session
->name
);
1007 if (session
->current_trace_chunk
) {
1008 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1009 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1011 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1012 current_chunk_id
.is_set
= true;
1014 ERR("Failed to get current trace chunk id");
1015 ret
= LTTNG_ERR_UNK
;
1020 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1021 && usess
->consumer
->enabled
) {
1022 /* For each consumer socket, send relayd sockets */
1023 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1024 socket
, node
.node
) {
1025 pthread_mutex_lock(socket
->lock
);
1026 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1027 usess
->consumer
, socket
,
1028 session
->name
, session
->hostname
,
1030 session
->live_timer
,
1031 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1032 session
->creation_time
,
1033 session
->name_contains_creation_time
);
1034 pthread_mutex_unlock(socket
->lock
);
1035 if (ret
!= LTTNG_OK
) {
1038 /* Session is now ready for network streaming. */
1039 session
->net_handle
= 1;
1041 session
->consumer
->relay_major_version
=
1042 usess
->consumer
->relay_major_version
;
1043 session
->consumer
->relay_minor_version
=
1044 usess
->consumer
->relay_minor_version
;
1045 session
->consumer
->relay_allows_clear
=
1046 usess
->consumer
->relay_allows_clear
;
1049 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1050 && ksess
->consumer
->enabled
) {
1051 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1052 socket
, node
.node
) {
1053 pthread_mutex_lock(socket
->lock
);
1054 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1055 ksess
->consumer
, socket
,
1056 session
->name
, session
->hostname
,
1058 session
->live_timer
,
1059 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1060 session
->creation_time
,
1061 session
->name_contains_creation_time
);
1062 pthread_mutex_unlock(socket
->lock
);
1063 if (ret
!= LTTNG_OK
) {
1066 /* Session is now ready for network streaming. */
1067 session
->net_handle
= 1;
1069 session
->consumer
->relay_major_version
=
1070 ksess
->consumer
->relay_major_version
;
1071 session
->consumer
->relay_minor_version
=
1072 ksess
->consumer
->relay_minor_version
;
1073 session
->consumer
->relay_allows_clear
=
1074 ksess
->consumer
->relay_allows_clear
;
1083 * Start a kernel session by opening all necessary streams.
1085 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1088 struct ltt_kernel_channel
*kchan
;
1090 /* Open kernel metadata */
1091 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1092 ret
= kernel_open_metadata(ksess
);
1094 ret
= LTTNG_ERR_KERN_META_FAIL
;
1099 /* Open kernel metadata stream */
1100 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1101 ret
= kernel_open_metadata_stream(ksess
);
1103 ERR("Kernel create metadata stream failed");
1104 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1109 /* For each channel */
1110 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1111 if (kchan
->stream_count
== 0) {
1112 ret
= kernel_open_channel_stream(kchan
);
1114 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1117 /* Update the stream global counter */
1118 ksess
->stream_count_global
+= ret
;
1122 /* Setup kernel consumer socket and send fds to it */
1123 ret
= init_kernel_tracing(ksess
);
1125 ret
= LTTNG_ERR_KERN_START_FAIL
;
1129 /* This start the kernel tracing */
1130 ret
= kernel_start_session(ksess
);
1132 ret
= LTTNG_ERR_KERN_START_FAIL
;
1136 /* Quiescent wait after starting trace */
1137 kernel_wait_quiescent();
1147 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1149 struct ltt_kernel_channel
*kchan
;
1150 bool error_occurred
= false;
1153 if (!ksess
|| !ksess
->active
) {
1156 DBG("Stopping kernel tracing");
1158 ret
= kernel_stop_session(ksess
);
1160 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1164 kernel_wait_quiescent();
1166 /* Flush metadata after stopping (if exists) */
1167 if (ksess
->metadata_stream_fd
>= 0) {
1168 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1170 ERR("Kernel metadata flush failed");
1171 error_occurred
= true;
1175 /* Flush all buffers after stopping */
1176 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1177 ret
= kernel_flush_buffer(kchan
);
1179 ERR("Kernel flush buffer error");
1180 error_occurred
= true;
1185 if (error_occurred
) {
1186 ret
= LTTNG_ERR_UNK
;
1195 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1197 int cmd_disable_channel(struct ltt_session
*session
,
1198 enum lttng_domain_type domain
, char *channel_name
)
1201 struct ltt_ust_session
*usess
;
1203 usess
= session
->ust_session
;
1208 case LTTNG_DOMAIN_KERNEL
:
1210 ret
= channel_kernel_disable(session
->kernel_session
,
1212 if (ret
!= LTTNG_OK
) {
1216 kernel_wait_quiescent();
1219 case LTTNG_DOMAIN_UST
:
1221 struct ltt_ust_channel
*uchan
;
1222 struct lttng_ht
*chan_ht
;
1224 chan_ht
= usess
->domain_global
.channels
;
1226 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1227 if (uchan
== NULL
) {
1228 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1232 ret
= channel_ust_disable(usess
, uchan
);
1233 if (ret
!= LTTNG_OK
) {
1239 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1251 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1253 * The wpipe arguments is used as a notifier for the kernel thread.
1255 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1259 ssize_t sock_recv_len
;
1260 struct lttng_channel
*channel
= NULL
;
1261 struct lttng_buffer_view view
;
1262 struct lttng_dynamic_buffer channel_buffer
;
1263 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1265 lttng_dynamic_buffer_init(&channel_buffer
);
1266 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1267 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1269 ret
= LTTNG_ERR_NOMEM
;
1273 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1275 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1276 ERR("Failed to receive \"enable channel\" command payload");
1277 ret
= LTTNG_ERR_INVALID
;
1281 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1282 if (!lttng_buffer_view_is_valid(&view
)) {
1283 ret
= LTTNG_ERR_INVALID
;
1287 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1288 ERR("Invalid channel payload received in \"enable channel\" command");
1289 ret
= LTTNG_ERR_INVALID
;
1293 ret
= cmd_enable_channel_internal(
1294 cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1297 lttng_dynamic_buffer_reset(&channel_buffer
);
1298 lttng_channel_destroy(channel
);
1302 static int cmd_enable_channel_internal(struct ltt_session
*session
,
1303 const struct lttng_domain
*domain
,
1304 const struct lttng_channel
*_attr
,
1308 struct ltt_ust_session
*usess
= session
->ust_session
;
1309 struct lttng_ht
*chan_ht
;
1311 struct lttng_channel
*attr
= NULL
;
1317 attr
= lttng_channel_copy(_attr
);
1319 ret
= LTTNG_ERR_NOMEM
;
1323 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1325 /* Validate channel name */
1326 if (attr
->name
[0] == '.' ||
1327 memchr(attr
->name
, '/', len
) != NULL
) {
1328 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1332 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1337 * If the session is a live session, remove the switch timer, the
1338 * live timer does the same thing but sends also synchronisation
1339 * beacons for inactive streams.
1341 if (session
->live_timer
> 0) {
1342 attr
->attr
.live_timer_interval
= session
->live_timer
;
1343 attr
->attr
.switch_timer_interval
= 0;
1346 /* Check for feature support */
1347 switch (domain
->type
) {
1348 case LTTNG_DOMAIN_KERNEL
:
1350 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1351 /* Sampling position of buffer is not supported */
1352 WARN("Kernel tracer does not support buffer monitoring. "
1353 "Setting the monitor interval timer to 0 "
1354 "(disabled) for channel '%s' of session '%s'",
1355 attr
->name
, session
->name
);
1356 lttng_channel_set_monitor_timer_interval(attr
, 0);
1360 case LTTNG_DOMAIN_UST
:
1362 case LTTNG_DOMAIN_JUL
:
1363 case LTTNG_DOMAIN_LOG4J
:
1364 case LTTNG_DOMAIN_PYTHON
:
1365 if (!agent_tracing_is_enabled()) {
1366 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1367 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1372 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1376 switch (domain
->type
) {
1377 case LTTNG_DOMAIN_KERNEL
:
1379 struct ltt_kernel_channel
*kchan
;
1381 kchan
= trace_kernel_get_channel_by_name(
1382 attr
->name
, session
->kernel_session
);
1383 if (kchan
== NULL
) {
1385 * Don't try to create a channel if the session has been started at
1386 * some point in time before. The tracer does not allow it.
1388 if (session
->has_been_started
) {
1389 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1393 if (session
->snapshot
.nb_output
> 0 ||
1394 session
->snapshot_mode
) {
1395 /* Enforce mmap output for snapshot sessions. */
1396 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1398 ret
= channel_kernel_create(
1399 session
->kernel_session
, attr
, wpipe
);
1400 if (attr
->name
[0] != '\0') {
1401 session
->kernel_session
->has_non_default_channel
= 1;
1404 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1407 if (ret
!= LTTNG_OK
) {
1411 kernel_wait_quiescent();
1414 case LTTNG_DOMAIN_UST
:
1415 case LTTNG_DOMAIN_JUL
:
1416 case LTTNG_DOMAIN_LOG4J
:
1417 case LTTNG_DOMAIN_PYTHON
:
1419 struct ltt_ust_channel
*uchan
;
1424 * Current agent implementation limitations force us to allow
1425 * only one channel at once in "agent" subdomains. Each
1426 * subdomain has a default channel name which must be strictly
1429 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1430 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1431 LTTNG_SYMBOL_NAME_LEN
)) {
1432 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1435 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1436 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1437 LTTNG_SYMBOL_NAME_LEN
)) {
1438 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1441 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1442 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1443 LTTNG_SYMBOL_NAME_LEN
)) {
1444 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1449 chan_ht
= usess
->domain_global
.channels
;
1451 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1452 if (uchan
== NULL
) {
1454 * Don't try to create a channel if the session has been started at
1455 * some point in time before. The tracer does not allow it.
1457 if (session
->has_been_started
) {
1458 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1462 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1463 if (attr
->name
[0] != '\0') {
1464 usess
->has_non_default_channel
= 1;
1467 ret
= channel_ust_enable(usess
, uchan
);
1472 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1476 if (ret
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1477 session
->has_non_mmap_channel
= true;
1482 lttng_channel_destroy(attr
);
1486 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1487 struct ltt_session
*session
,
1488 enum lttng_domain_type domain
,
1489 enum lttng_process_attr process_attr
,
1490 enum lttng_tracking_policy
*policy
)
1492 enum lttng_error_code ret_code
= LTTNG_OK
;
1493 const struct process_attr_tracker
*tracker
;
1496 case LTTNG_DOMAIN_KERNEL
:
1497 if (!session
->kernel_session
) {
1498 ret_code
= LTTNG_ERR_INVALID
;
1501 tracker
= kernel_get_process_attr_tracker(
1502 session
->kernel_session
, process_attr
);
1504 case LTTNG_DOMAIN_UST
:
1505 if (!session
->ust_session
) {
1506 ret_code
= LTTNG_ERR_INVALID
;
1509 tracker
= trace_ust_get_process_attr_tracker(
1510 session
->ust_session
, process_attr
);
1513 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1517 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1519 ret_code
= LTTNG_ERR_INVALID
;
1525 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1526 struct ltt_session
*session
,
1527 enum lttng_domain_type domain
,
1528 enum lttng_process_attr process_attr
,
1529 enum lttng_tracking_policy policy
)
1531 enum lttng_error_code ret_code
= LTTNG_OK
;
1534 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1535 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1536 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1539 ret_code
= LTTNG_ERR_INVALID
;
1544 case LTTNG_DOMAIN_KERNEL
:
1545 if (!session
->kernel_session
) {
1546 ret_code
= LTTNG_ERR_INVALID
;
1549 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1550 session
->kernel_session
, process_attr
, policy
);
1552 case LTTNG_DOMAIN_UST
:
1553 if (!session
->ust_session
) {
1554 ret_code
= LTTNG_ERR_INVALID
;
1557 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1558 session
->ust_session
, process_attr
, policy
);
1561 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1568 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1569 struct ltt_session
*session
,
1570 enum lttng_domain_type domain
,
1571 enum lttng_process_attr process_attr
,
1572 const struct process_attr_value
*value
)
1574 enum lttng_error_code ret_code
= LTTNG_OK
;
1577 case LTTNG_DOMAIN_KERNEL
:
1578 if (!session
->kernel_session
) {
1579 ret_code
= LTTNG_ERR_INVALID
;
1582 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1583 session
->kernel_session
, process_attr
, value
);
1585 case LTTNG_DOMAIN_UST
:
1586 if (!session
->ust_session
) {
1587 ret_code
= LTTNG_ERR_INVALID
;
1590 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1591 session
->ust_session
, process_attr
, value
);
1594 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1601 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1602 struct ltt_session
*session
,
1603 enum lttng_domain_type domain
,
1604 enum lttng_process_attr process_attr
,
1605 const struct process_attr_value
*value
)
1607 enum lttng_error_code ret_code
= LTTNG_OK
;
1610 case LTTNG_DOMAIN_KERNEL
:
1611 if (!session
->kernel_session
) {
1612 ret_code
= LTTNG_ERR_INVALID
;
1615 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1616 session
->kernel_session
, process_attr
, value
);
1618 case LTTNG_DOMAIN_UST
:
1619 if (!session
->ust_session
) {
1620 ret_code
= LTTNG_ERR_INVALID
;
1623 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1624 session
->ust_session
, process_attr
, value
);
1627 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1634 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1635 struct ltt_session
*session
,
1636 enum lttng_domain_type domain
,
1637 enum lttng_process_attr process_attr
,
1638 struct lttng_process_attr_values
**values
)
1640 enum lttng_error_code ret_code
= LTTNG_OK
;
1641 const struct process_attr_tracker
*tracker
;
1642 enum process_attr_tracker_status status
;
1645 case LTTNG_DOMAIN_KERNEL
:
1646 if (!session
->kernel_session
) {
1647 ret_code
= LTTNG_ERR_INVALID
;
1650 tracker
= kernel_get_process_attr_tracker(
1651 session
->kernel_session
, process_attr
);
1653 case LTTNG_DOMAIN_UST
:
1654 if (!session
->ust_session
) {
1655 ret_code
= LTTNG_ERR_INVALID
;
1658 tracker
= trace_ust_get_process_attr_tracker(
1659 session
->ust_session
, process_attr
);
1662 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1667 ret_code
= LTTNG_ERR_INVALID
;
1671 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1673 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1674 ret_code
= LTTNG_OK
;
1676 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1677 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1679 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1680 ret_code
= LTTNG_ERR_NOMEM
;
1683 ret_code
= LTTNG_ERR_UNK
;
1692 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1694 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1695 struct lttng_event
*event
,
1696 char *filter_expression
,
1697 struct lttng_bytecode
*bytecode
,
1698 struct lttng_event_exclusion
*exclusion
)
1701 const char *event_name
;
1702 const struct ltt_session
*session
= cmd_ctx
->session
;
1703 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1704 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1706 DBG("Disable event command for event \'%s\'", event
->name
);
1709 * Filter and exclusions are simply not handled by the
1710 * disable event command at this time.
1714 (void) filter_expression
;
1717 /* Ignore the presence of filter or exclusion for the event */
1719 event
->exclusion
= 0;
1721 event_name
= event
->name
;
1723 /* Error out on unhandled search criteria */
1724 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1725 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1726 ret
= LTTNG_ERR_UNK
;
1733 case LTTNG_DOMAIN_KERNEL
:
1735 struct ltt_kernel_channel
*kchan
;
1736 struct ltt_kernel_session
*ksess
;
1738 ksess
= session
->kernel_session
;
1741 * If a non-default channel has been created in the
1742 * session, explicitely require that -c chan_name needs
1745 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1746 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1750 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1751 if (kchan
== NULL
) {
1752 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1756 switch (event
->type
) {
1757 case LTTNG_EVENT_ALL
:
1758 case LTTNG_EVENT_TRACEPOINT
:
1759 case LTTNG_EVENT_SYSCALL
:
1760 case LTTNG_EVENT_PROBE
:
1761 case LTTNG_EVENT_FUNCTION
:
1762 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1763 if (event_name
[0] == '\0') {
1764 ret
= event_kernel_disable_event(kchan
,
1767 ret
= event_kernel_disable_event(kchan
,
1768 event_name
, event
->type
);
1770 if (ret
!= LTTNG_OK
) {
1775 ret
= LTTNG_ERR_UNK
;
1779 kernel_wait_quiescent();
1782 case LTTNG_DOMAIN_UST
:
1784 struct ltt_ust_channel
*uchan
;
1785 struct ltt_ust_session
*usess
;
1787 usess
= session
->ust_session
;
1789 if (validate_ust_event_name(event_name
)) {
1790 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1795 * If a non-default channel has been created in the
1796 * session, explicitly require that -c chan_name needs
1799 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1800 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1804 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1806 if (uchan
== NULL
) {
1807 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1811 switch (event
->type
) {
1812 case LTTNG_EVENT_ALL
:
1814 * An empty event name means that everything
1815 * should be disabled.
1817 if (event
->name
[0] == '\0') {
1818 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1820 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1823 if (ret
!= LTTNG_OK
) {
1828 ret
= LTTNG_ERR_UNK
;
1832 DBG3("Disable UST event %s in channel %s completed", event_name
,
1836 case LTTNG_DOMAIN_LOG4J
:
1837 case LTTNG_DOMAIN_JUL
:
1838 case LTTNG_DOMAIN_PYTHON
:
1841 struct ltt_ust_session
*usess
= session
->ust_session
;
1845 switch (event
->type
) {
1846 case LTTNG_EVENT_ALL
:
1849 ret
= LTTNG_ERR_UNK
;
1853 agt
= trace_ust_find_agent(usess
, domain
);
1855 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1859 * An empty event name means that everything
1860 * should be disabled.
1862 if (event
->name
[0] == '\0') {
1863 ret
= event_agent_disable_all(usess
, agt
);
1865 ret
= event_agent_disable(usess
, agt
, event_name
);
1867 if (ret
!= LTTNG_OK
) {
1874 ret
= LTTNG_ERR_UND
;
1885 free(filter_expression
);
1890 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1892 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1893 const struct lttng_event_context
*event_context
, int kwpipe
)
1895 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1896 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1897 const struct ltt_session
*session
= cmd_ctx
->session
;
1898 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1901 * Don't try to add a context if the session has been started at
1902 * some point in time before. The tracer does not allow it and would
1903 * result in a corrupted trace.
1905 if (cmd_ctx
->session
->has_been_started
) {
1906 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1911 case LTTNG_DOMAIN_KERNEL
:
1912 assert(session
->kernel_session
);
1914 if (session
->kernel_session
->channel_count
== 0) {
1915 /* Create default channel */
1916 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1917 if (ret
!= LTTNG_OK
) {
1920 chan_kern_created
= 1;
1922 /* Add kernel context to kernel tracer */
1923 ret
= context_kernel_add(session
->kernel_session
,
1924 event_context
, channel_name
);
1925 if (ret
!= LTTNG_OK
) {
1929 case LTTNG_DOMAIN_JUL
:
1930 case LTTNG_DOMAIN_LOG4J
:
1933 * Validate channel name.
1934 * If no channel name is given and the domain is JUL or LOG4J,
1935 * set it to the appropriate domain-specific channel name. If
1936 * a name is provided but does not match the expexted channel
1937 * name, return an error.
1939 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1940 strcmp(channel_name
,
1941 DEFAULT_JUL_CHANNEL_NAME
)) {
1942 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1944 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1945 strcmp(channel_name
,
1946 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1947 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1950 /* break is _not_ missing here. */
1952 case LTTNG_DOMAIN_UST
:
1954 struct ltt_ust_session
*usess
= session
->ust_session
;
1955 unsigned int chan_count
;
1959 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1960 if (chan_count
== 0) {
1961 struct lttng_channel
*attr
;
1962 /* Create default channel */
1963 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1965 ret
= LTTNG_ERR_FATAL
;
1969 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1970 if (ret
!= LTTNG_OK
) {
1974 channel_attr_destroy(attr
);
1975 chan_ust_created
= 1;
1978 ret
= context_ust_add(usess
, domain
, event_context
,
1980 if (ret
!= LTTNG_OK
) {
1986 ret
= LTTNG_ERR_UND
;
1994 if (chan_kern_created
) {
1995 struct ltt_kernel_channel
*kchan
=
1996 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1997 session
->kernel_session
);
1998 /* Created previously, this should NOT fail. */
2000 kernel_destroy_channel(kchan
);
2003 if (chan_ust_created
) {
2004 struct ltt_ust_channel
*uchan
=
2005 trace_ust_find_channel_by_name(
2006 session
->ust_session
->domain_global
.channels
,
2007 DEFAULT_CHANNEL_NAME
);
2008 /* Created previously, this should NOT fail. */
2010 /* Remove from the channel list of the session. */
2011 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2013 trace_ust_destroy_channel(uchan
);
2019 static inline bool name_starts_with(const char *name
, const char *prefix
)
2021 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2023 return !strncmp(name
, prefix
, max_cmp_len
);
2026 /* Perform userspace-specific event name validation */
2027 static int validate_ust_event_name(const char *name
)
2037 * Check name against all internal UST event component namespaces used
2040 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2041 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2042 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2051 * Internal version of cmd_enable_event() with a supplemental
2052 * "internal_event" flag which is used to enable internal events which should
2053 * be hidden from clients. Such events are used in the agent implementation to
2054 * enable the events through which all "agent" events are funeled.
2056 static int _cmd_enable_event(struct ltt_session
*session
,
2057 const struct lttng_domain
*domain
,
2058 char *channel_name
, struct lttng_event
*event
,
2059 char *filter_expression
,
2060 struct lttng_bytecode
*filter
,
2061 struct lttng_event_exclusion
*exclusion
,
2062 int wpipe
, bool internal_event
)
2064 int ret
= 0, channel_created
= 0;
2065 struct lttng_channel
*attr
= NULL
;
2069 assert(channel_name
);
2071 /* If we have a filter, we must have its filter expression */
2072 assert(!(!!filter_expression
^ !!filter
));
2074 /* Normalize event name as a globbing pattern */
2075 strutils_normalize_star_glob_pattern(event
->name
);
2077 /* Normalize exclusion names as globbing patterns */
2081 for (i
= 0; i
< exclusion
->count
; i
++) {
2082 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2084 strutils_normalize_star_glob_pattern(name
);
2088 DBG("Enable event command for event \'%s\'", event
->name
);
2092 switch (domain
->type
) {
2093 case LTTNG_DOMAIN_KERNEL
:
2095 struct ltt_kernel_channel
*kchan
;
2098 * If a non-default channel has been created in the
2099 * session, explicitely require that -c chan_name needs
2102 if (session
->kernel_session
->has_non_default_channel
2103 && channel_name
[0] == '\0') {
2104 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2108 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2109 session
->kernel_session
);
2110 if (kchan
== NULL
) {
2111 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2112 LTTNG_BUFFER_GLOBAL
);
2114 ret
= LTTNG_ERR_FATAL
;
2117 if (lttng_strncpy(attr
->name
, channel_name
,
2118 sizeof(attr
->name
))) {
2119 ret
= LTTNG_ERR_INVALID
;
2123 ret
= cmd_enable_channel_internal(
2124 session
, domain
, attr
, wpipe
);
2125 if (ret
!= LTTNG_OK
) {
2128 channel_created
= 1;
2131 /* Get the newly created kernel channel pointer */
2132 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2133 session
->kernel_session
);
2134 if (kchan
== NULL
) {
2135 /* This sould not happen... */
2136 ret
= LTTNG_ERR_FATAL
;
2140 switch (event
->type
) {
2141 case LTTNG_EVENT_ALL
:
2143 char *filter_expression_a
= NULL
;
2144 struct lttng_bytecode
*filter_a
= NULL
;
2147 * We need to duplicate filter_expression and filter,
2148 * because ownership is passed to first enable
2151 if (filter_expression
) {
2152 filter_expression_a
= strdup(filter_expression
);
2153 if (!filter_expression_a
) {
2154 ret
= LTTNG_ERR_FATAL
;
2159 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2161 free(filter_expression_a
);
2162 ret
= LTTNG_ERR_FATAL
;
2165 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2167 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2168 ret
= event_kernel_enable_event(kchan
, event
,
2169 filter_expression
, filter
);
2170 /* We have passed ownership */
2171 filter_expression
= NULL
;
2173 if (ret
!= LTTNG_OK
) {
2174 if (channel_created
) {
2175 /* Let's not leak a useless channel. */
2176 kernel_destroy_channel(kchan
);
2178 free(filter_expression_a
);
2182 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2183 ret
= event_kernel_enable_event(kchan
, event
,
2184 filter_expression_a
, filter_a
);
2185 /* We have passed ownership */
2186 filter_expression_a
= NULL
;
2188 if (ret
!= LTTNG_OK
) {
2193 case LTTNG_EVENT_PROBE
:
2194 case LTTNG_EVENT_USERSPACE_PROBE
:
2195 case LTTNG_EVENT_FUNCTION
:
2196 case LTTNG_EVENT_FUNCTION_ENTRY
:
2197 case LTTNG_EVENT_TRACEPOINT
:
2198 ret
= event_kernel_enable_event(kchan
, event
,
2199 filter_expression
, filter
);
2200 /* We have passed ownership */
2201 filter_expression
= NULL
;
2203 if (ret
!= LTTNG_OK
) {
2204 if (channel_created
) {
2205 /* Let's not leak a useless channel. */
2206 kernel_destroy_channel(kchan
);
2211 case LTTNG_EVENT_SYSCALL
:
2212 ret
= event_kernel_enable_event(kchan
, event
,
2213 filter_expression
, filter
);
2214 /* We have passed ownership */
2215 filter_expression
= NULL
;
2217 if (ret
!= LTTNG_OK
) {
2222 ret
= LTTNG_ERR_UNK
;
2226 kernel_wait_quiescent();
2229 case LTTNG_DOMAIN_UST
:
2231 struct ltt_ust_channel
*uchan
;
2232 struct ltt_ust_session
*usess
= session
->ust_session
;
2237 * If a non-default channel has been created in the
2238 * session, explicitely require that -c chan_name needs
2241 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2242 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2246 /* Get channel from global UST domain */
2247 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2249 if (uchan
== NULL
) {
2250 /* Create default channel */
2251 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2252 usess
->buffer_type
);
2254 ret
= LTTNG_ERR_FATAL
;
2257 if (lttng_strncpy(attr
->name
, channel_name
,
2258 sizeof(attr
->name
))) {
2259 ret
= LTTNG_ERR_INVALID
;
2263 ret
= cmd_enable_channel_internal(
2264 session
, domain
, attr
, wpipe
);
2265 if (ret
!= LTTNG_OK
) {
2269 /* Get the newly created channel reference back */
2270 uchan
= trace_ust_find_channel_by_name(
2271 usess
->domain_global
.channels
, channel_name
);
2275 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2277 * Don't allow users to add UST events to channels which
2278 * are assigned to a userspace subdomain (JUL, Log4J,
2281 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2285 if (!internal_event
) {
2287 * Ensure the event name is not reserved for internal
2290 ret
= validate_ust_event_name(event
->name
);
2292 WARN("Userspace event name %s failed validation.",
2294 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2299 /* At this point, the session and channel exist on the tracer */
2300 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2301 filter_expression
, filter
, exclusion
,
2303 /* We have passed ownership */
2304 filter_expression
= NULL
;
2307 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2308 goto already_enabled
;
2309 } else if (ret
!= LTTNG_OK
) {
2314 case LTTNG_DOMAIN_LOG4J
:
2315 case LTTNG_DOMAIN_JUL
:
2316 case LTTNG_DOMAIN_PYTHON
:
2318 const char *default_event_name
, *default_chan_name
;
2320 struct lttng_event uevent
;
2321 struct lttng_domain tmp_dom
;
2322 struct ltt_ust_session
*usess
= session
->ust_session
;
2326 if (!agent_tracing_is_enabled()) {
2327 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2328 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2332 agt
= trace_ust_find_agent(usess
, domain
->type
);
2334 agt
= agent_create(domain
->type
);
2336 ret
= LTTNG_ERR_NOMEM
;
2339 agent_add(agt
, usess
->agents
);
2342 /* Create the default tracepoint. */
2343 memset(&uevent
, 0, sizeof(uevent
));
2344 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2345 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2346 default_event_name
= event_get_default_agent_ust_name(
2348 if (!default_event_name
) {
2349 ret
= LTTNG_ERR_FATAL
;
2352 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2353 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2356 * The domain type is changed because we are about to enable the
2357 * default channel and event for the JUL domain that are hardcoded.
2358 * This happens in the UST domain.
2360 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2361 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2363 switch (domain
->type
) {
2364 case LTTNG_DOMAIN_LOG4J
:
2365 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2367 case LTTNG_DOMAIN_JUL
:
2368 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2370 case LTTNG_DOMAIN_PYTHON
:
2371 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2374 /* The switch/case we are in makes this impossible */
2379 char *filter_expression_copy
= NULL
;
2380 struct lttng_bytecode
*filter_copy
= NULL
;
2383 const size_t filter_size
= sizeof(
2384 struct lttng_bytecode
)
2387 filter_copy
= zmalloc(filter_size
);
2389 ret
= LTTNG_ERR_NOMEM
;
2392 memcpy(filter_copy
, filter
, filter_size
);
2394 filter_expression_copy
=
2395 strdup(filter_expression
);
2396 if (!filter_expression
) {
2397 ret
= LTTNG_ERR_NOMEM
;
2400 if (!filter_expression_copy
|| !filter_copy
) {
2401 free(filter_expression_copy
);
2407 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2408 (char *) default_chan_name
,
2409 &uevent
, filter_expression_copy
,
2410 filter_copy
, NULL
, wpipe
);
2413 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2414 goto already_enabled
;
2415 } else if (ret
!= LTTNG_OK
) {
2419 /* The wild card * means that everything should be enabled. */
2420 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2421 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2424 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2428 filter_expression
= NULL
;
2429 if (ret
!= LTTNG_OK
) {
2436 ret
= LTTNG_ERR_UND
;
2444 free(filter_expression
);
2447 channel_attr_destroy(attr
);
2453 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2454 * We own filter, exclusion, and filter_expression.
2456 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2457 struct lttng_event
*event
,
2458 char *filter_expression
,
2459 struct lttng_event_exclusion
*exclusion
,
2460 struct lttng_bytecode
*bytecode
,
2465 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2467 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2470 * The ownership of the following parameters is transferred to
2471 * _cmd_enable_event:
2473 * - filter_expression,
2477 ret
= _cmd_enable_event(cmd_ctx
->session
,
2479 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2480 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2481 filter_expression
= NULL
;
2488 * Enable an event which is internal to LTTng. An internal should
2489 * never be made visible to clients and are immune to checks such as
2492 static int cmd_enable_event_internal(struct ltt_session
*session
,
2493 const struct lttng_domain
*domain
,
2494 char *channel_name
, struct lttng_event
*event
,
2495 char *filter_expression
,
2496 struct lttng_bytecode
*filter
,
2497 struct lttng_event_exclusion
*exclusion
,
2500 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2501 filter_expression
, filter
, exclusion
, wpipe
, true);
2505 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2507 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2508 struct lttng_payload
*reply_payload
)
2510 enum lttng_error_code ret_code
;
2512 ssize_t i
, nb_events
= 0;
2513 struct lttng_event
*events
= NULL
;
2514 struct lttcomm_list_command_header reply_command_header
= {};
2515 size_t reply_command_header_offset
;
2517 assert(reply_payload
);
2519 /* Reserve space for command reply header. */
2520 reply_command_header_offset
= reply_payload
->buffer
.size
;
2521 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2522 reply_command_header_offset
+
2523 sizeof(struct lttcomm_list_command_header
));
2525 ret_code
= LTTNG_ERR_NOMEM
;
2530 case LTTNG_DOMAIN_KERNEL
:
2531 nb_events
= kernel_list_events(&events
);
2532 if (nb_events
< 0) {
2533 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2537 case LTTNG_DOMAIN_UST
:
2538 nb_events
= ust_app_list_events(&events
);
2539 if (nb_events
< 0) {
2540 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2544 case LTTNG_DOMAIN_LOG4J
:
2545 case LTTNG_DOMAIN_JUL
:
2546 case LTTNG_DOMAIN_PYTHON
:
2547 nb_events
= agent_list_events(&events
, domain
);
2548 if (nb_events
< 0) {
2549 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2554 ret_code
= LTTNG_ERR_UND
;
2558 for (i
= 0; i
< nb_events
; i
++) {
2559 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2562 ret_code
= LTTNG_ERR_NOMEM
;
2567 if (nb_events
> UINT32_MAX
) {
2568 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2569 ret_code
= LTTNG_ERR_OVERFLOW
;
2573 /* Update command reply header. */
2574 reply_command_header
.count
= (uint32_t) nb_events
;
2575 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2576 sizeof(reply_command_header
));
2578 ret_code
= LTTNG_OK
;
2585 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2587 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2588 struct lttng_payload
*reply
)
2590 enum lttng_error_code ret_code
;
2592 unsigned int i
, nb_fields
;
2593 struct lttng_event_field
*fields
= NULL
;
2594 struct lttcomm_list_command_header reply_command_header
= {};
2595 size_t reply_command_header_offset
;
2599 /* Reserve space for command reply header. */
2600 reply_command_header_offset
= reply
->buffer
.size
;
2601 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2602 reply_command_header_offset
+
2603 sizeof(struct lttcomm_list_command_header
));
2605 ret_code
= LTTNG_ERR_NOMEM
;
2610 case LTTNG_DOMAIN_UST
:
2611 ret
= ust_app_list_event_fields(&fields
);
2613 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2618 case LTTNG_DOMAIN_KERNEL
:
2619 default: /* fall-through */
2620 ret_code
= LTTNG_ERR_UND
;
2626 for (i
= 0; i
< nb_fields
; i
++) {
2627 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2629 ret_code
= LTTNG_ERR_NOMEM
;
2634 if (nb_fields
> UINT32_MAX
) {
2635 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2636 ret_code
= LTTNG_ERR_OVERFLOW
;
2640 /* Update command reply header. */
2641 reply_command_header
.count
= (uint32_t) nb_fields
;
2643 memcpy(reply
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2644 sizeof(reply_command_header
));
2646 ret_code
= LTTNG_OK
;
2653 enum lttng_error_code
cmd_list_syscalls(
2654 struct lttng_payload
*reply_payload
)
2656 enum lttng_error_code ret_code
;
2657 ssize_t nb_events
, i
;
2659 struct lttng_event
*events
= NULL
;
2660 struct lttcomm_list_command_header reply_command_header
= {};
2661 size_t reply_command_header_offset
;
2663 assert(reply_payload
);
2665 /* Reserve space for command reply header. */
2666 reply_command_header_offset
= reply_payload
->buffer
.size
;
2667 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2668 reply_command_header_offset
+
2669 sizeof(struct lttcomm_list_command_header
));
2671 ret_code
= LTTNG_ERR_NOMEM
;
2675 nb_events
= syscall_table_list(&events
);
2676 if (nb_events
< 0) {
2677 ret_code
= (enum lttng_error_code
) -nb_events
;
2681 for (i
= 0; i
< nb_events
; i
++) {
2682 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2685 ret_code
= LTTNG_ERR_NOMEM
;
2690 if (nb_events
> UINT32_MAX
) {
2691 ERR("Syscall count would overflow the syscall listing command's reply");
2692 ret_code
= LTTNG_ERR_OVERFLOW
;
2696 /* Update command reply header. */
2697 reply_command_header
.count
= (uint32_t) nb_events
;
2698 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2699 sizeof(reply_command_header
));
2701 ret_code
= LTTNG_OK
;
2708 * Command LTTNG_START_TRACE processed by the client thread.
2710 * Called with session mutex held.
2712 int cmd_start_trace(struct ltt_session
*session
)
2714 enum lttng_error_code ret
;
2715 unsigned long nb_chan
= 0;
2716 struct ltt_kernel_session
*ksession
;
2717 struct ltt_ust_session
*usess
;
2718 const bool session_rotated_after_last_stop
=
2719 session
->rotated_after_last_stop
;
2720 const bool session_cleared_after_last_stop
=
2721 session
->cleared_after_last_stop
;
2725 /* Ease our life a bit ;) */
2726 ksession
= session
->kernel_session
;
2727 usess
= session
->ust_session
;
2729 /* Is the session already started? */
2730 if (session
->active
) {
2731 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2732 /* Perform nothing */
2736 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2737 !session
->current_trace_chunk
) {
2739 * A rotation was launched while the session was stopped and
2740 * it has not been completed yet. It is not possible to start
2741 * the session since starting the session here would require a
2742 * rotation from "NULL" to a new trace chunk. That rotation
2743 * would overlap with the ongoing rotation, which is not
2746 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2748 ret
= LTTNG_ERR_ROTATION_PENDING
;
2753 * Starting a session without channel is useless since after that it's not
2754 * possible to enable channel thus inform the client.
2756 if (usess
&& usess
->domain_global
.channels
) {
2757 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2760 nb_chan
+= ksession
->channel_count
;
2763 ret
= LTTNG_ERR_NO_CHANNEL
;
2767 session
->active
= 1;
2768 session
->rotated_after_last_stop
= false;
2769 session
->cleared_after_last_stop
= false;
2770 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2771 if (!session
->has_been_started
) {
2772 struct lttng_trace_chunk
*trace_chunk
;
2774 DBG("Creating initial trace chunk of session \"%s\"",
2776 trace_chunk
= session_create_new_trace_chunk(
2777 session
, NULL
, NULL
, NULL
);
2779 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2782 assert(!session
->current_trace_chunk
);
2783 ret
= session_set_trace_chunk(session
, trace_chunk
,
2785 lttng_trace_chunk_put(trace_chunk
);
2787 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2791 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2794 * Rotate existing streams into the new chunk.
2795 * This is a "quiet" rotation has no client has
2796 * explicitly requested this operation.
2798 * There is also no need to wait for the rotation
2799 * to complete as it will happen immediately. No data
2800 * was produced as the session was stopped, so the
2801 * rotation should happen on reception of the command.
2803 ret
= cmd_rotate_session(session
, NULL
, true,
2804 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2805 if (ret
!= LTTNG_OK
) {
2811 /* Kernel tracing */
2812 if (ksession
!= NULL
) {
2813 DBG("Start kernel tracing session %s", session
->name
);
2814 ret
= start_kernel_session(ksession
);
2815 if (ret
!= LTTNG_OK
) {
2820 /* Flag session that trace should start automatically */
2822 int int_ret
= ust_app_start_trace_all(usess
);
2825 ret
= LTTNG_ERR_UST_START_FAIL
;
2831 * Open a packet in every stream of the session to ensure that viewers
2832 * can correctly identify the boundaries of the periods during which
2833 * tracing was active for this session.
2835 ret
= session_open_packets(session
);
2836 if (ret
!= LTTNG_OK
) {
2841 * Clear the flag that indicates that a rotation was done while the
2842 * session was stopped.
2844 session
->rotated_after_last_stop
= false;
2846 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2847 int int_ret
= timer_session_rotation_schedule_timer_start(
2848 session
, session
->rotate_timer_period
);
2851 ERR("Failed to enable rotate timer");
2852 ret
= LTTNG_ERR_UNK
;
2860 if (ret
== LTTNG_OK
) {
2861 /* Flag this after a successful start. */
2862 session
->has_been_started
|= 1;
2864 session
->active
= 0;
2865 /* Restore initial state on error. */
2866 session
->rotated_after_last_stop
=
2867 session_rotated_after_last_stop
;
2868 session
->cleared_after_last_stop
=
2869 session_cleared_after_last_stop
;
2876 * Command LTTNG_STOP_TRACE processed by the client thread.
2878 int cmd_stop_trace(struct ltt_session
*session
)
2881 struct ltt_kernel_session
*ksession
;
2882 struct ltt_ust_session
*usess
;
2886 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2888 ksession
= session
->kernel_session
;
2889 usess
= session
->ust_session
;
2891 /* Session is not active. Skip everythong and inform the client. */
2892 if (!session
->active
) {
2893 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2897 ret
= stop_kernel_session(ksession
);
2898 if (ret
!= LTTNG_OK
) {
2902 if (usess
&& usess
->active
) {
2903 ret
= ust_app_stop_trace_all(usess
);
2905 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2910 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2912 /* Flag inactive after a successful stop. */
2913 session
->active
= 0;
2921 * Set the base_path of the session only if subdir of a control uris is set.
2922 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2924 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2926 struct lttng_uri
*uris
)
2931 for (i
= 0; i
< nb_uri
; i
++) {
2932 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2933 uris
[i
].subdir
[0] == '\0') {
2934 /* Not interested in these URIs */
2938 if (session
->base_path
!= NULL
) {
2939 free(session
->base_path
);
2940 session
->base_path
= NULL
;
2943 /* Set session base_path */
2944 session
->base_path
= strdup(uris
[i
].subdir
);
2945 if (!session
->base_path
) {
2946 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2947 uris
[i
].subdir
, session
->name
);
2948 ret
= LTTNG_ERR_NOMEM
;
2951 DBG2("Setting base path \"%s\" for session \"%s\"",
2952 session
->base_path
, session
->name
);
2960 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2962 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2963 struct lttng_uri
*uris
)
2966 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2967 struct ltt_ust_session
*usess
= session
->ust_session
;
2973 /* Can't set consumer URI if the session is active. */
2974 if (session
->active
) {
2975 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2980 * Set the session base path if any. This is done inside
2981 * cmd_set_consumer_uri to preserve backward compatibility of the
2982 * previous session creation api vs the session descriptor api.
2984 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2985 if (ret
!= LTTNG_OK
) {
2989 /* Set the "global" consumer URIs */
2990 for (i
= 0; i
< nb_uri
; i
++) {
2991 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2993 if (ret
!= LTTNG_OK
) {
2998 /* Set UST session URIs */
2999 if (session
->ust_session
) {
3000 for (i
= 0; i
< nb_uri
; i
++) {
3001 ret
= add_uri_to_consumer(session
,
3002 session
->ust_session
->consumer
,
3003 &uris
[i
], LTTNG_DOMAIN_UST
);
3004 if (ret
!= LTTNG_OK
) {
3010 /* Set kernel session URIs */
3011 if (session
->kernel_session
) {
3012 for (i
= 0; i
< nb_uri
; i
++) {
3013 ret
= add_uri_to_consumer(session
,
3014 session
->kernel_session
->consumer
,
3015 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3016 if (ret
!= LTTNG_OK
) {
3023 * Make sure to set the session in output mode after we set URI since a
3024 * session can be created without URL (thus flagged in no output mode).
3026 session
->output_traces
= 1;
3028 ksess
->output_traces
= 1;
3032 usess
->output_traces
= 1;
3043 enum lttng_error_code
set_session_output_from_descriptor(
3044 struct ltt_session
*session
,
3045 const struct lttng_session_descriptor
*descriptor
)
3048 enum lttng_error_code ret_code
= LTTNG_OK
;
3049 enum lttng_session_descriptor_type session_type
=
3050 lttng_session_descriptor_get_type(descriptor
);
3051 enum lttng_session_descriptor_output_type output_type
=
3052 lttng_session_descriptor_get_output_type(descriptor
);
3053 struct lttng_uri uris
[2] = {};
3054 size_t uri_count
= 0;
3056 switch (output_type
) {
3057 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3059 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3060 lttng_session_descriptor_get_local_output_uri(descriptor
,
3064 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3065 lttng_session_descriptor_get_network_output_uris(descriptor
,
3066 &uris
[0], &uris
[1]);
3070 ret_code
= LTTNG_ERR_INVALID
;
3074 switch (session_type
) {
3075 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3077 struct snapshot_output
*new_output
= NULL
;
3079 new_output
= snapshot_output_alloc();
3081 ret_code
= LTTNG_ERR_NOMEM
;
3085 ret
= snapshot_output_init_with_uri(session
,
3086 DEFAULT_SNAPSHOT_MAX_SIZE
,
3087 NULL
, uris
, uri_count
, session
->consumer
,
3088 new_output
, &session
->snapshot
);
3090 ret_code
= (ret
== -ENOMEM
) ?
3091 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3092 snapshot_output_destroy(new_output
);
3095 snapshot_add_output(&session
->snapshot
, new_output
);
3098 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3099 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3101 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
3105 ret_code
= LTTNG_ERR_INVALID
;
3113 enum lttng_error_code
cmd_create_session_from_descriptor(
3114 struct lttng_session_descriptor
*descriptor
,
3115 const lttng_sock_cred
*creds
,
3116 const char *home_path
)
3119 enum lttng_error_code ret_code
;
3120 const char *session_name
;
3121 struct ltt_session
*new_session
= NULL
;
3122 enum lttng_session_descriptor_status descriptor_status
;
3124 session_lock_list();
3126 if (*home_path
!= '/') {
3127 ERR("Home path provided by client is not absolute");
3128 ret_code
= LTTNG_ERR_INVALID
;
3133 descriptor_status
= lttng_session_descriptor_get_session_name(
3134 descriptor
, &session_name
);
3135 switch (descriptor_status
) {
3136 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3138 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3139 session_name
= NULL
;
3142 ret_code
= LTTNG_ERR_INVALID
;
3146 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3148 if (ret_code
!= LTTNG_OK
) {
3152 if (!session_name
) {
3153 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3156 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3161 if (!lttng_session_descriptor_is_output_destination_initialized(
3164 * Only include the session's creation time in the output
3165 * destination if the name of the session itself was
3166 * not auto-generated.
3168 ret_code
= lttng_session_descriptor_set_default_output(
3170 session_name
? &new_session
->creation_time
: NULL
,
3172 if (ret_code
!= LTTNG_OK
) {
3176 new_session
->has_user_specified_directory
=
3177 lttng_session_descriptor_has_output_directory(
3181 switch (lttng_session_descriptor_get_type(descriptor
)) {
3182 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3183 new_session
->snapshot_mode
= 1;
3185 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3186 new_session
->live_timer
=
3187 lttng_session_descriptor_live_get_timer_interval(
3194 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3195 if (ret_code
!= LTTNG_OK
) {
3198 new_session
->consumer
->enabled
= 1;
3199 ret_code
= LTTNG_OK
;
3201 /* Release reference provided by the session_create function. */
3202 session_put(new_session
);
3203 if (ret_code
!= LTTNG_OK
&& new_session
) {
3204 /* Release the global reference on error. */
3205 session_destroy(new_session
);
3207 session_unlock_list();
3211 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3212 struct lttng_session_descriptor
**return_descriptor
)
3215 size_t payload_size
;
3216 struct lttng_dynamic_buffer payload
;
3217 struct lttng_buffer_view home_dir_view
;
3218 struct lttng_buffer_view session_descriptor_view
;
3219 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3220 enum lttng_error_code ret_code
;
3222 lttng_dynamic_buffer_init(&payload
);
3223 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3225 ret_code
= LTTNG_ERR_INVALID
;
3228 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3229 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3230 ret_code
= LTTNG_ERR_INVALID
;
3234 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3235 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3236 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3238 ret_code
= LTTNG_ERR_NOMEM
;
3242 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3244 ERR("Reception of session descriptor failed, aborting.");
3245 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3249 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3252 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3253 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3254 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3255 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3256 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3260 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3262 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3263 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3264 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3265 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3266 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3270 ret
= lttng_session_descriptor_create_from_buffer(
3271 &session_descriptor_view
, &session_descriptor
);
3273 ERR("Failed to create session descriptor from payload of \"create session\" command");
3274 ret_code
= LTTNG_ERR_INVALID
;
3279 * Sets the descriptor's auto-generated properties (name, output) if
3282 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3284 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3285 if (ret_code
!= LTTNG_OK
) {
3289 ret_code
= LTTNG_OK
;
3290 *return_descriptor
= session_descriptor
;
3291 session_descriptor
= NULL
;
3293 lttng_dynamic_buffer_reset(&payload
);
3294 lttng_session_descriptor_destroy(session_descriptor
);
3299 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3300 void *_reply_context
)
3304 const struct cmd_destroy_session_reply_context
*reply_context
=
3306 struct lttng_dynamic_buffer payload
;
3307 struct lttcomm_session_destroy_command_header cmd_header
;
3308 struct lttng_trace_archive_location
*location
= NULL
;
3309 struct lttcomm_lttng_msg llm
= {
3310 .cmd_type
= LTTNG_DESTROY_SESSION
,
3311 .ret_code
= reply_context
->destruction_status
,
3314 sizeof(struct lttcomm_session_destroy_command_header
),
3317 size_t payload_size_before_location
;
3319 lttng_dynamic_buffer_init(&payload
);
3321 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3323 ERR("Failed to append session destruction message");
3327 cmd_header
.rotation_state
=
3328 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3329 session
->rotation_state
:
3330 LTTNG_ROTATION_STATE_NO_ROTATION
);
3331 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3332 sizeof(cmd_header
));
3334 ERR("Failed to append session destruction command header");
3338 if (!reply_context
->implicit_rotation_on_destroy
) {
3339 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3343 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3344 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3349 location
= session_get_trace_archive_location(session
);
3351 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3356 payload_size_before_location
= payload
.size
;
3357 comm_ret
= lttng_trace_archive_location_serialize(location
,
3359 lttng_trace_archive_location_put(location
);
3361 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3365 /* Update the message to indicate the location's length. */
3366 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3367 payload
.size
- payload_size_before_location
;
3369 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3370 payload
.data
, payload
.size
);
3371 if (comm_ret
!= (ssize_t
) payload
.size
) {
3372 ERR("Failed to send result of the destruction of session \"%s\" to client",
3376 ret
= close(reply_context
->reply_sock_fd
);
3378 PERROR("Failed to close client socket in deferred session destroy reply");
3380 lttng_dynamic_buffer_reset(&payload
);
3381 free(_reply_context
);
3385 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3387 * Called with session lock held.
3389 int cmd_destroy_session(struct ltt_session
*session
,
3390 struct notification_thread_handle
*notification_thread_handle
,
3394 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3395 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3398 reply_context
= zmalloc(sizeof(*reply_context
));
3399 if (!reply_context
) {
3400 ret
= LTTNG_ERR_NOMEM
;
3403 reply_context
->reply_sock_fd
= *sock_fd
;
3409 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3411 if (session
->active
) {
3412 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3414 ret
= cmd_stop_trace(session
);
3415 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3416 /* Carry on with the destruction of the session. */
3417 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3418 session
->name
, lttng_strerror(-ret
));
3419 destruction_last_error
= ret
;
3423 if (session
->rotation_schedule_timer_enabled
) {
3424 if (timer_session_rotation_schedule_timer_stop(
3426 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3428 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3432 if (session
->rotate_size
) {
3433 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3434 session
->rotate_size
= 0;
3437 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3439 * Perform a last rotation on destruction if rotations have
3440 * occurred during the session's lifetime.
3442 ret
= cmd_rotate_session(session
, NULL
, false,
3443 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3444 if (ret
!= LTTNG_OK
) {
3445 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3446 session
->name
, lttng_strerror(-ret
));
3447 destruction_last_error
= -ret
;
3449 if (reply_context
) {
3450 reply_context
->implicit_rotation_on_destroy
= true;
3452 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3454 * The user has not triggered a session rotation. However, to
3455 * ensure all data has been consumed, the session is rotated
3456 * to a 'null' trace chunk before it is destroyed.
3458 * This is a "quiet" rotation meaning that no notification is
3459 * emitted and no renaming of the current trace chunk takes
3462 ret
= cmd_rotate_session(session
, NULL
, true,
3463 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3465 * Rotation operations may not be supported by the kernel
3466 * tracer. Hence, do not consider this implicit rotation as
3467 * a session destruction error. The library has already stopped
3468 * the session and waited for pending data; there is nothing
3469 * left to do but complete the destruction of the session.
3471 if (ret
!= LTTNG_OK
&&
3472 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3473 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3474 session
->name
, lttng_strerror(ret
));
3475 destruction_last_error
= -ret
;
3479 if (session
->shm_path
[0]) {
3481 * When a session is created with an explicit shm_path,
3482 * the consumer daemon will create its shared memory files
3483 * at that location and will *not* unlink them. This is normal
3484 * as the intention of that feature is to make it possible
3485 * to retrieve the content of those files should a crash occur.
3487 * To ensure the content of those files can be used, the
3488 * sessiond daemon will replicate the content of the metadata
3489 * cache in a metadata file.
3491 * On clean-up, it is expected that the consumer daemon will
3492 * unlink the shared memory files and that the session daemon
3493 * will unlink the metadata file. Then, the session's directory
3494 * in the shm path can be removed.
3496 * Unfortunately, a flaw in the design of the sessiond's and
3497 * consumerd's tear down of channels makes it impossible to
3498 * determine when the sessiond _and_ the consumerd have both
3499 * destroyed their representation of a channel. For one, the
3500 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3501 * callbacks in both daemons.
3503 * However, it is also impossible for the sessiond to know when
3504 * the consumer daemon is done destroying its channel(s) since
3505 * it occurs as a reaction to the closing of the channel's file
3506 * descriptor. There is no resulting communication initiated
3507 * from the consumerd to the sessiond to confirm that the
3508 * operation is completed (and was successful).
3510 * Until this is all fixed, the session daemon checks for the
3511 * removal of the session's shm path which makes it possible
3512 * to safely advertise a session as having been destroyed.
3514 * Prior to this fix, it was not possible to reliably save
3515 * a session making use of the --shm-path option, destroy it,
3516 * and load it again. This is because the creation of the
3517 * session would fail upon seeing the session's shm path
3518 * already in existence.
3520 * Note that none of the error paths in the check for the
3521 * directory's existence return an error. This is normal
3522 * as there isn't much that can be done. The session will
3523 * be destroyed properly, except that we can't offer the
3524 * guarantee that the same session can be re-created.
3526 current_completion_handler
= &destroy_completion_handler
.handler
;
3527 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3529 sizeof(destroy_completion_handler
.shm_path
));
3534 * The session is destroyed. However, note that the command context
3535 * still holds a reference to the session, thus delaying its destruction
3536 * _at least_ up to the point when that reference is released.
3538 session_destroy(session
);
3539 if (reply_context
) {
3540 reply_context
->destruction_status
= destruction_last_error
;
3541 ret
= session_add_destroy_notifier(session
,
3542 cmd_destroy_session_reply
,
3543 (void *) reply_context
);
3545 ret
= LTTNG_ERR_FATAL
;
3557 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3559 int cmd_register_consumer(struct ltt_session
*session
,
3560 enum lttng_domain_type domain
, const char *sock_path
,
3561 struct consumer_data
*cdata
)
3564 struct consumer_socket
*socket
= NULL
;
3571 case LTTNG_DOMAIN_KERNEL
:
3573 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3577 /* Can't register a consumer if there is already one */
3578 if (ksess
->consumer_fds_sent
!= 0) {
3579 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3583 sock
= lttcomm_connect_unix_sock(sock_path
);
3585 ret
= LTTNG_ERR_CONNECT_FAIL
;
3588 cdata
->cmd_sock
= sock
;
3590 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3591 if (socket
== NULL
) {
3594 PERROR("close register consumer");
3596 cdata
->cmd_sock
= -1;
3597 ret
= LTTNG_ERR_FATAL
;
3601 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3602 if (socket
->lock
== NULL
) {
3603 PERROR("zmalloc pthread mutex");
3604 ret
= LTTNG_ERR_FATAL
;
3607 pthread_mutex_init(socket
->lock
, NULL
);
3608 socket
->registered
= 1;
3611 consumer_add_socket(socket
, ksess
->consumer
);
3614 pthread_mutex_lock(&cdata
->pid_mutex
);
3616 pthread_mutex_unlock(&cdata
->pid_mutex
);
3621 /* TODO: Userspace tracing */
3622 ret
= LTTNG_ERR_UND
;
3630 consumer_destroy_socket(socket
);
3636 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3638 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3639 struct lttng_domain
**domains
)
3644 struct lttng_ht_iter iter
;
3646 if (session
->kernel_session
!= NULL
) {
3647 DBG3("Listing domains found kernel domain");
3651 if (session
->ust_session
!= NULL
) {
3652 DBG3("Listing domains found UST global domain");
3656 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3658 if (agt
->being_used
) {
3669 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3670 if (*domains
== NULL
) {
3671 ret
= LTTNG_ERR_FATAL
;
3675 if (session
->kernel_session
!= NULL
) {
3676 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3678 /* Kernel session buffer type is always GLOBAL */
3679 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3684 if (session
->ust_session
!= NULL
) {
3685 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3686 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3690 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3692 if (agt
->being_used
) {
3693 (*domains
)[index
].type
= agt
->domain
;
3694 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3704 /* Return negative value to differentiate return code */
3710 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3712 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3713 struct ltt_session
*session
,
3714 struct lttng_payload
*payload
)
3718 struct lttcomm_list_command_header cmd_header
= {};
3719 size_t cmd_header_offset
;
3720 enum lttng_error_code ret_code
;
3725 DBG("Listing channels for session %s", session
->name
);
3727 cmd_header_offset
= payload
->buffer
.size
;
3729 /* Reserve space for command reply header. */
3730 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3731 cmd_header_offset
+ sizeof(cmd_header
));
3733 ret_code
= LTTNG_ERR_NOMEM
;
3738 case LTTNG_DOMAIN_KERNEL
:
3740 /* Kernel channels */
3741 struct ltt_kernel_channel
*kchan
;
3742 if (session
->kernel_session
!= NULL
) {
3743 cds_list_for_each_entry(kchan
,
3744 &session
->kernel_session
->channel_list
.head
, list
) {
3745 uint64_t discarded_events
, lost_packets
;
3746 struct lttng_channel_extended
*extended
;
3748 extended
= (struct lttng_channel_extended
*)
3749 kchan
->channel
->attr
.extended
.ptr
;
3751 ret
= get_kernel_runtime_stats(session
, kchan
,
3752 &discarded_events
, &lost_packets
);
3754 ret_code
= LTTNG_ERR_UNK
;
3759 * Update the discarded_events and lost_packets
3760 * count for the channel
3762 extended
->discarded_events
= discarded_events
;
3763 extended
->lost_packets
= lost_packets
;
3765 ret
= lttng_channel_serialize(
3766 kchan
->channel
, &payload
->buffer
);
3768 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3769 kchan
->channel
->name
);
3770 ret_code
= LTTNG_ERR_UNK
;
3779 case LTTNG_DOMAIN_UST
:
3781 struct lttng_ht_iter iter
;
3782 struct ltt_ust_channel
*uchan
;
3785 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3786 &iter
.iter
, uchan
, node
.node
) {
3787 uint64_t discarded_events
= 0, lost_packets
= 0;
3788 struct lttng_channel
*channel
= NULL
;
3789 struct lttng_channel_extended
*extended
;
3791 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3793 ret_code
= LTTNG_ERR_NOMEM
;
3797 extended
= (struct lttng_channel_extended
*)
3798 channel
->attr
.extended
.ptr
;
3800 ret
= get_ust_runtime_stats(session
, uchan
,
3801 &discarded_events
, &lost_packets
);
3803 lttng_channel_destroy(channel
);
3804 ret_code
= LTTNG_ERR_UNK
;
3808 extended
->discarded_events
= discarded_events
;
3809 extended
->lost_packets
= lost_packets
;
3811 ret
= lttng_channel_serialize(
3812 channel
, &payload
->buffer
);
3814 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3816 lttng_channel_destroy(channel
);
3817 ret_code
= LTTNG_ERR_UNK
;
3821 lttng_channel_destroy(channel
);
3831 if (i
> UINT32_MAX
) {
3832 ERR("Channel count would overflow the channel listing command's reply");
3833 ret_code
= LTTNG_ERR_OVERFLOW
;
3837 /* Update command reply header. */
3838 cmd_header
.count
= (uint32_t) i
;
3839 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3840 sizeof(cmd_header
));
3841 ret_code
= LTTNG_OK
;
3848 * Command LTTNG_LIST_EVENTS processed by the client thread.
3850 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3851 struct ltt_session
*session
,
3853 struct lttng_payload
*reply_payload
)
3855 int buffer_resize_ret
;
3856 enum lttng_error_code ret_code
= LTTNG_OK
;
3857 struct lttcomm_list_command_header reply_command_header
= {};
3858 size_t reply_command_header_offset
;
3859 unsigned int nb_events
= 0;
3861 assert(reply_payload
);
3863 /* Reserve space for command reply header. */
3864 reply_command_header_offset
= reply_payload
->buffer
.size
;
3865 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3866 reply_command_header_offset
+
3867 sizeof(struct lttcomm_list_command_header
));
3868 if (buffer_resize_ret
) {
3869 ret_code
= LTTNG_ERR_NOMEM
;
3874 case LTTNG_DOMAIN_KERNEL
:
3875 if (session
->kernel_session
!= NULL
) {
3876 ret_code
= list_lttng_kernel_events(channel_name
,
3877 session
->kernel_session
, reply_payload
, &nb_events
);
3881 case LTTNG_DOMAIN_UST
:
3883 if (session
->ust_session
!= NULL
) {
3884 ret_code
= list_lttng_ust_global_events(channel_name
,
3885 &session
->ust_session
->domain_global
,
3886 reply_payload
, &nb_events
);
3891 case LTTNG_DOMAIN_LOG4J
:
3892 case LTTNG_DOMAIN_JUL
:
3893 case LTTNG_DOMAIN_PYTHON
:
3894 if (session
->ust_session
) {
3895 struct lttng_ht_iter iter
;
3899 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3900 &iter
.iter
, agt
, node
.node
) {
3901 if (agt
->domain
== domain
) {
3902 ret_code
= list_lttng_agent_events(
3903 agt
, reply_payload
, &nb_events
);
3912 ret_code
= LTTNG_ERR_UND
;
3916 if (nb_events
> UINT32_MAX
) {
3917 ret_code
= LTTNG_ERR_OVERFLOW
;
3921 /* Update command reply header. */
3922 reply_command_header
.count
= (uint32_t) nb_events
;
3923 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
3924 sizeof(reply_command_header
));
3931 * Using the session list, filled a lttng_session array to send back to the
3932 * client for session listing.
3934 * The session list lock MUST be acquired before calling this function. Use
3935 * session_lock_list() and session_unlock_list().
3937 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3938 size_t session_count
, uid_t uid
, gid_t gid
)
3942 struct ltt_session
*session
;
3943 struct ltt_session_list
*list
= session_get_list();
3944 struct lttng_session_extended
*extended
=
3945 (typeof(extended
)) (&sessions
[session_count
]);
3947 DBG("Getting all available session for UID %d GID %d",
3950 * Iterate over session list and append data after the control struct in
3953 cds_list_for_each_entry(session
, &list
->head
, list
) {
3954 if (!session_get(session
)) {
3958 * Only list the sessions the user can control.
3960 if (!session_access_ok(session
, uid
) ||
3961 session
->destroyed
) {
3962 session_put(session
);
3966 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3967 struct ltt_ust_session
*usess
= session
->ust_session
;
3969 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3970 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3971 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3972 ret
= build_network_session_path(sessions
[i
].path
,
3973 sizeof(sessions
[i
].path
), session
);
3975 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3976 session
->consumer
->dst
.session_root_path
);
3979 PERROR("snprintf session path");
3980 session_put(session
);
3984 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3985 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3986 sessions
[i
].enabled
= session
->active
;
3987 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3988 sessions
[i
].live_timer_interval
= session
->live_timer
;
3989 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3990 extended
[i
].creation_time
.is_set
= 1;
3992 session_put(session
);
3997 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3998 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4000 int cmd_data_pending(struct ltt_session
*session
)
4003 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4004 struct ltt_ust_session
*usess
= session
->ust_session
;
4008 DBG("Data pending for session %s", session
->name
);
4010 /* Session MUST be stopped to ask for data availability. */
4011 if (session
->active
) {
4012 ret
= LTTNG_ERR_SESSION_STARTED
;
4016 * If stopped, just make sure we've started before else the above call
4017 * will always send that there is data pending.
4019 * The consumer assumes that when the data pending command is received,
4020 * the trace has been started before or else no output data is written
4021 * by the streams which is a condition for data pending. So, this is
4022 * *VERY* important that we don't ask the consumer before a start
4025 if (!session
->has_been_started
) {
4031 /* A rotation is still pending, we have to wait. */
4032 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4033 DBG("Rotate still pending for session %s", session
->name
);
4038 if (ksess
&& ksess
->consumer
) {
4039 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4041 /* Data is still being extracted for the kernel. */
4046 if (usess
&& usess
->consumer
) {
4047 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4049 /* Data is still being extracted for the kernel. */
4054 /* Data is ready to be read by a viewer */
4062 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4064 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4066 int cmd_snapshot_add_output(struct ltt_session
*session
,
4067 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4070 struct snapshot_output
*new_output
;
4075 DBG("Cmd snapshot add output for session %s", session
->name
);
4078 * Can't create an output if the session is not set in no-output mode.
4080 if (session
->output_traces
) {
4081 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4085 if (session
->has_non_mmap_channel
) {
4086 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4090 /* Only one output is allowed until we have the "tee" feature. */
4091 if (session
->snapshot
.nb_output
== 1) {
4092 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4096 new_output
= snapshot_output_alloc();
4098 ret
= LTTNG_ERR_NOMEM
;
4102 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4103 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4104 &session
->snapshot
);
4106 if (ret
== -ENOMEM
) {
4107 ret
= LTTNG_ERR_NOMEM
;
4109 ret
= LTTNG_ERR_INVALID
;
4115 snapshot_add_output(&session
->snapshot
, new_output
);
4117 *id
= new_output
->id
;
4124 snapshot_output_destroy(new_output
);
4130 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4132 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4134 int cmd_snapshot_del_output(struct ltt_session
*session
,
4135 const struct lttng_snapshot_output
*output
)
4138 struct snapshot_output
*sout
= NULL
;
4146 * Permission denied to create an output if the session is not
4147 * set in no output mode.
4149 if (session
->output_traces
) {
4150 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4155 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4157 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4158 } else if (*output
->name
!= '\0') {
4159 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4161 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4164 ret
= LTTNG_ERR_INVALID
;
4168 snapshot_delete_output(&session
->snapshot
, sout
);
4169 snapshot_output_destroy(sout
);
4178 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4180 * If no output is available, outputs is untouched and 0 is returned.
4182 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4184 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4185 struct lttng_snapshot_output
**outputs
)
4188 struct lttng_snapshot_output
*list
= NULL
;
4189 struct lttng_ht_iter iter
;
4190 struct snapshot_output
*output
;
4195 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4198 * Permission denied to create an output if the session is not
4199 * set in no output mode.
4201 if (session
->output_traces
) {
4202 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4206 if (session
->snapshot
.nb_output
== 0) {
4211 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4213 ret
= -LTTNG_ERR_NOMEM
;
4217 /* Copy list from session to the new list object. */
4219 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4220 output
, node
.node
) {
4221 assert(output
->consumer
);
4222 list
[idx
].id
= output
->id
;
4223 list
[idx
].max_size
= output
->max_size
;
4224 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4225 sizeof(list
[idx
].name
))) {
4226 ret
= -LTTNG_ERR_INVALID
;
4229 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4230 if (lttng_strncpy(list
[idx
].ctrl_url
,
4231 output
->consumer
->dst
.session_root_path
,
4232 sizeof(list
[idx
].ctrl_url
))) {
4233 ret
= -LTTNG_ERR_INVALID
;
4238 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4239 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4241 ret
= -LTTNG_ERR_NOMEM
;
4246 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4247 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4249 ret
= -LTTNG_ERR_NOMEM
;
4258 ret
= session
->snapshot
.nb_output
;
4267 * Check if we can regenerate the metadata for this session.
4268 * Only kernel, UST per-uid and non-live sessions are supported.
4270 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4273 int check_regenerate_metadata_support(struct ltt_session
*session
)
4279 if (session
->live_timer
!= 0) {
4280 ret
= LTTNG_ERR_LIVE_SESSION
;
4283 if (!session
->active
) {
4284 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4287 if (session
->ust_session
) {
4288 switch (session
->ust_session
->buffer_type
) {
4289 case LTTNG_BUFFER_PER_UID
:
4291 case LTTNG_BUFFER_PER_PID
:
4292 ret
= LTTNG_ERR_PER_PID_SESSION
;
4296 ret
= LTTNG_ERR_UNK
;
4300 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4301 session
->consumer
->relay_minor_version
< 8) {
4302 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4312 int clear_metadata_file(int fd
)
4317 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4318 if (lseek_ret
< 0) {
4324 ret
= ftruncate(fd
, 0);
4326 PERROR("ftruncate");
4335 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4338 struct buffer_reg_uid
*uid_reg
= NULL
;
4339 struct buffer_reg_session
*session_reg
= NULL
;
4342 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4343 struct ust_registry_session
*registry
;
4344 struct ust_registry_channel
*chan
;
4345 struct lttng_ht_iter iter_chan
;
4347 session_reg
= uid_reg
->registry
;
4348 registry
= session_reg
->reg
.ust
;
4350 pthread_mutex_lock(®istry
->lock
);
4351 registry
->metadata_len_sent
= 0;
4352 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4353 registry
->metadata_len
= 0;
4354 registry
->metadata_version
++;
4355 if (registry
->metadata_fd
> 0) {
4356 /* Clear the metadata file's content. */
4357 ret
= clear_metadata_file(registry
->metadata_fd
);
4359 pthread_mutex_unlock(®istry
->lock
);
4364 ret
= ust_metadata_session_statedump(registry
, NULL
,
4365 registry
->major
, registry
->minor
);
4367 pthread_mutex_unlock(®istry
->lock
);
4368 ERR("Failed to generate session metadata (err = %d)",
4372 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4374 struct ust_registry_event
*event
;
4375 struct lttng_ht_iter iter_event
;
4377 ret
= ust_metadata_channel_statedump(registry
, chan
);
4379 pthread_mutex_unlock(®istry
->lock
);
4380 ERR("Failed to generate channel metadata "
4384 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4386 ret
= ust_metadata_event_statedump(registry
,
4389 pthread_mutex_unlock(®istry
->lock
);
4390 ERR("Failed to generate event metadata "
4396 pthread_mutex_unlock(®istry
->lock
);
4405 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4407 * Ask the consumer to truncate the existing metadata file(s) and
4408 * then regenerate the metadata. Live and per-pid sessions are not
4409 * supported and return an error.
4411 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4413 int cmd_regenerate_metadata(struct ltt_session
*session
)
4419 ret
= check_regenerate_metadata_support(session
);
4424 if (session
->kernel_session
) {
4425 ret
= kernctl_session_regenerate_metadata(
4426 session
->kernel_session
->fd
);
4428 ERR("Failed to regenerate the kernel metadata");
4433 if (session
->ust_session
) {
4434 ret
= ust_regenerate_metadata(session
->ust_session
);
4436 ERR("Failed to regenerate the UST metadata");
4440 DBG("Cmd metadata regenerate for session %s", session
->name
);
4448 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4450 * Ask the tracer to regenerate a new statedump.
4452 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4454 int cmd_regenerate_statedump(struct ltt_session
*session
)
4460 if (!session
->active
) {
4461 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4465 if (session
->kernel_session
) {
4466 ret
= kernctl_session_regenerate_statedump(
4467 session
->kernel_session
->fd
);
4469 * Currently, the statedump in kernel can only fail if out
4473 if (ret
== -ENOMEM
) {
4474 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4476 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4478 ERR("Failed to regenerate the kernel statedump");
4483 if (session
->ust_session
) {
4484 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4486 * Currently, the statedump in UST always returns 0.
4489 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4490 ERR("Failed to regenerate the UST statedump");
4494 DBG("Cmd regenerate statedump for session %s", session
->name
);
4502 enum lttng_error_code
synchronize_tracer_notifier_register(
4503 struct notification_thread_handle
*notification_thread
,
4504 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4506 enum lttng_error_code ret_code
;
4507 const struct lttng_condition
*condition
=
4508 lttng_trigger_get_const_condition(trigger
);
4509 const char *trigger_name
;
4510 uid_t trigger_owner
;
4511 enum lttng_trigger_status trigger_status
;
4512 const enum lttng_domain_type trigger_domain
=
4513 lttng_trigger_get_underlying_domain_type_restriction(
4516 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4517 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4520 assert(lttng_condition_get_type(condition
) ==
4521 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4523 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4524 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4525 trigger_name
: "(anonymous)";
4527 session_lock_list();
4528 switch (trigger_domain
) {
4529 case LTTNG_DOMAIN_KERNEL
:
4531 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4532 if (ret_code
!= LTTNG_OK
) {
4533 enum lttng_error_code notif_thread_unregister_ret
;
4535 notif_thread_unregister_ret
=
4536 notification_thread_command_unregister_trigger(
4537 notification_thread
, trigger
);
4539 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4540 /* Return the original error code. */
4541 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4543 (int) trigger_owner
,
4547 goto end_unlock_session_list
;
4551 case LTTNG_DOMAIN_UST
:
4552 ust_app_global_update_all_event_notifier_rules();
4554 case LTTNG_DOMAIN_JUL
:
4555 case LTTNG_DOMAIN_LOG4J
:
4556 case LTTNG_DOMAIN_PYTHON
:
4558 /* Agent domains. */
4559 struct agent
*agt
= agent_find_by_event_notifier_domain(
4563 agt
= agent_create(trigger_domain
);
4565 ret_code
= LTTNG_ERR_NOMEM
;
4566 goto end_unlock_session_list
;
4569 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4572 ret_code
= trigger_agent_enable(trigger
, agt
);
4573 if (ret_code
!= LTTNG_OK
) {
4574 goto end_unlock_session_list
;
4579 case LTTNG_DOMAIN_NONE
:
4584 ret_code
= LTTNG_OK
;
4585 end_unlock_session_list
:
4586 session_unlock_list();
4590 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4591 struct lttng_trigger
*trigger
,
4592 bool is_trigger_anonymous
,
4593 struct notification_thread_handle
*notification_thread
,
4594 struct lttng_trigger
**return_trigger
)
4596 enum lttng_error_code ret_code
;
4597 const char *trigger_name
;
4598 uid_t trigger_owner
;
4599 enum lttng_trigger_status trigger_status
;
4601 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4602 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4603 trigger_name
: "(anonymous)";
4605 trigger_status
= lttng_trigger_get_owner_uid(
4606 trigger
, &trigger_owner
);
4607 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4609 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4610 trigger_name
, (int) trigger_owner
,
4611 (int) lttng_credentials_get_uid(cmd_creds
));
4614 * Validate the trigger credentials against the command credentials.
4615 * Only the root user can register a trigger with non-matching
4618 if (!lttng_credentials_is_equal_uid(
4619 lttng_trigger_get_credentials(trigger
),
4621 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4622 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4623 trigger_name
, (int) trigger_owner
,
4624 (int) lttng_credentials_get_uid(cmd_creds
));
4625 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4631 * The bytecode generation also serves as a validation step for the
4632 * bytecode expressions.
4634 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4635 if (ret_code
!= LTTNG_OK
) {
4636 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4637 trigger_name
, (int) trigger_owner
, ret_code
);
4642 * A reference to the trigger is acquired by the notification thread.
4643 * It is safe to return the same trigger to the caller since it the
4644 * other user holds a reference.
4646 * The trigger is modified during the execution of the
4647 * "register trigger" command. However, by the time the command returns,
4648 * it is safe to use without any locking as its properties are
4651 ret_code
= notification_thread_command_register_trigger(
4652 notification_thread
, trigger
, is_trigger_anonymous
);
4653 if (ret_code
!= LTTNG_OK
) {
4654 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4655 trigger_name
, (int) trigger_owner
, ret_code
);
4659 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4660 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4661 trigger_name
: "(anonymous)";
4664 * Synchronize tracers if the trigger adds an event notifier.
4666 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4667 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4668 trigger
, cmd_creds
);
4669 if (ret_code
!= LTTNG_OK
) {
4670 ERR("Error registering tracer notifier: %s",
4671 lttng_strerror(-ret_code
));
4677 * Return an updated trigger to the client.
4679 * Since a modified version of the same trigger is returned, acquire a
4680 * reference to the trigger so the caller doesn't have to care if those
4681 * are distinct instances or not.
4683 if (ret_code
== LTTNG_OK
) {
4684 lttng_trigger_get(trigger
);
4685 *return_trigger
= trigger
;
4686 /* Ownership of trigger was transferred to caller. */
4694 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4695 const struct lttng_trigger
*trigger
)
4697 enum lttng_error_code ret_code
;
4698 const struct lttng_condition
*condition
=
4699 lttng_trigger_get_const_condition(trigger
);
4700 const enum lttng_domain_type trigger_domain
=
4701 lttng_trigger_get_underlying_domain_type_restriction(
4705 assert(lttng_condition_get_type(condition
) ==
4706 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4708 session_lock_list();
4709 switch (trigger_domain
) {
4710 case LTTNG_DOMAIN_KERNEL
:
4711 ret_code
= kernel_unregister_event_notifier(trigger
);
4712 if (ret_code
!= LTTNG_OK
) {
4713 goto end_unlock_session_list
;
4717 case LTTNG_DOMAIN_UST
:
4718 ust_app_global_update_all_event_notifier_rules();
4720 case LTTNG_DOMAIN_JUL
:
4721 case LTTNG_DOMAIN_LOG4J
:
4722 case LTTNG_DOMAIN_PYTHON
:
4724 /* Agent domains. */
4725 struct agent
*agt
= agent_find_by_event_notifier_domain(
4729 * This trigger was never registered in the first place. Calling
4730 * this function under those circumstances is an internal error.
4733 ret_code
= trigger_agent_disable(trigger
, agt
);
4734 if (ret_code
!= LTTNG_OK
) {
4735 goto end_unlock_session_list
;
4740 case LTTNG_DOMAIN_NONE
:
4745 ret_code
= LTTNG_OK
;
4747 end_unlock_session_list
:
4748 session_unlock_list();
4752 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4753 const struct lttng_trigger
*trigger
,
4754 struct notification_thread_handle
*notification_thread
)
4756 enum lttng_error_code ret_code
;
4757 const char *trigger_name
;
4758 uid_t trigger_owner
;
4759 enum lttng_trigger_status trigger_status
;
4760 struct lttng_trigger
*sessiond_trigger
= NULL
;
4762 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4763 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4764 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4765 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4767 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4768 trigger_name
, (int) trigger_owner
,
4769 (int) lttng_credentials_get_uid(cmd_creds
));
4772 * Validate the trigger credentials against the command credentials.
4773 * Only the root user can unregister a trigger with non-matching
4776 if (!lttng_credentials_is_equal_uid(
4777 lttng_trigger_get_credentials(trigger
),
4779 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4780 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4781 trigger_name
, (int) trigger_owner
,
4782 (int) lttng_credentials_get_uid(cmd_creds
));
4783 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4788 /* Fetch the sessiond side trigger object. */
4789 ret_code
= notification_thread_command_get_trigger(
4790 notification_thread
, trigger
, &sessiond_trigger
);
4791 if (ret_code
!= LTTNG_OK
) {
4792 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4793 trigger_name
, (int) trigger_owner
, ret_code
);
4797 assert(sessiond_trigger
);
4800 * From this point on, no matter what, consider the trigger
4803 * We set the unregistered state of the sessiond side trigger object in
4804 * the client thread since we want to minimize the possibility of the
4805 * notification thread being stalled due to a long execution of an
4806 * action that required the trigger lock.
4808 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4810 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4812 if (ret_code
!= LTTNG_OK
) {
4813 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4814 trigger_name
, (int) trigger_owner
, ret_code
);
4819 * Synchronize tracers if the trigger removes an event notifier.
4820 * Do this even if the trigger unregistration failed to at least stop
4821 * the tracers from producing notifications associated with this
4824 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4825 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4826 if (ret_code
!= LTTNG_OK
) {
4827 ERR("Error unregistering trigger to tracer.");
4834 lttng_trigger_put(sessiond_trigger
);
4838 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4839 struct notification_thread_handle
*notification_thread
,
4840 struct lttng_triggers
**return_triggers
)
4843 enum lttng_error_code ret_code
;
4844 struct lttng_triggers
*triggers
= NULL
;
4846 /* Get the set of triggers from the notification thread. */
4847 ret_code
= notification_thread_command_list_triggers(
4848 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4849 if (ret_code
!= LTTNG_OK
) {
4853 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4855 ret_code
= LTTNG_ERR_UNK
;
4859 *return_triggers
= triggers
;
4861 ret_code
= LTTNG_OK
;
4863 lttng_triggers_destroy(triggers
);
4867 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4868 const struct lttng_error_query
*query
,
4869 struct lttng_error_query_results
**_results
,
4870 struct notification_thread_handle
*notification_thread
)
4872 enum lttng_error_code ret_code
;
4873 const struct lttng_trigger
*query_target_trigger
;
4874 const struct lttng_action
*query_target_action
= NULL
;
4875 struct lttng_trigger
*matching_trigger
= NULL
;
4876 const char *trigger_name
;
4877 uid_t trigger_owner
;
4878 enum lttng_trigger_status trigger_status
;
4879 struct lttng_error_query_results
*results
= NULL
;
4881 switch (lttng_error_query_get_target_type(query
)) {
4882 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4883 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4885 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4886 query_target_trigger
=
4887 lttng_error_query_condition_borrow_target(query
);
4889 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4890 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4897 assert(query_target_trigger
);
4899 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4900 query_target_trigger
, &matching_trigger
);
4901 if (ret_code
!= LTTNG_OK
) {
4905 /* No longer needed. */
4906 query_target_trigger
= NULL
;
4908 if (lttng_error_query_get_target_type(query
) ==
4909 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4910 /* Get the sessiond-side version of the target action. */
4911 query_target_action
=
4912 lttng_error_query_action_borrow_action_target(
4913 query
, matching_trigger
);
4916 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4917 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4918 trigger_name
: "(anonymous)";
4919 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4921 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4923 results
= lttng_error_query_results_create();
4925 ret_code
= LTTNG_ERR_NOMEM
;
4929 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4930 trigger_name
, (int) trigger_owner
,
4931 (int) lttng_credentials_get_uid(cmd_creds
));
4934 * Validate the trigger credentials against the command credentials.
4935 * Only the root user can target a trigger with non-matching
4938 if (!lttng_credentials_is_equal_uid(
4939 lttng_trigger_get_credentials(matching_trigger
),
4941 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4942 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4943 trigger_name
, (int) trigger_owner
,
4944 (int) lttng_credentials_get_uid(cmd_creds
));
4945 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4950 switch (lttng_error_query_get_target_type(query
)) {
4951 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4952 trigger_status
= lttng_trigger_add_error_results(
4953 matching_trigger
, results
);
4955 switch (trigger_status
) {
4956 case LTTNG_TRIGGER_STATUS_OK
:
4959 ret_code
= LTTNG_ERR_UNK
;
4964 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4966 trigger_status
= lttng_trigger_condition_add_error_results(
4967 matching_trigger
, results
);
4969 switch (trigger_status
) {
4970 case LTTNG_TRIGGER_STATUS_OK
:
4973 ret_code
= LTTNG_ERR_UNK
;
4979 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4981 const enum lttng_action_status action_status
=
4982 lttng_action_add_error_query_results(
4983 query_target_action
, results
);
4985 switch (action_status
) {
4986 case LTTNG_ACTION_STATUS_OK
:
4989 ret_code
= LTTNG_ERR_UNK
;
5000 *_results
= results
;
5002 ret_code
= LTTNG_OK
;
5004 lttng_trigger_put(matching_trigger
);
5005 lttng_error_query_results_destroy(results
);
5010 * Send relayd sockets from snapshot output to consumer. Ignore request if the
5011 * snapshot output is *not* set with a remote destination.
5013 * Return LTTNG_OK on success or a LTTNG_ERR code.
5015 static enum lttng_error_code
set_relayd_for_snapshot(
5016 struct consumer_output
*output
,
5017 const struct ltt_session
*session
)
5019 enum lttng_error_code status
= LTTNG_OK
;
5020 struct lttng_ht_iter iter
;
5021 struct consumer_socket
*socket
;
5022 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
5023 const char *base_path
;
5028 DBG2("Set relayd object from snapshot output");
5030 if (session
->current_trace_chunk
) {
5031 enum lttng_trace_chunk_status chunk_status
=
5032 lttng_trace_chunk_get_id(
5033 session
->current_trace_chunk
,
5034 ¤t_chunk_id
.value
);
5036 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5037 current_chunk_id
.is_set
= true;
5039 ERR("Failed to get current trace chunk id");
5040 status
= LTTNG_ERR_UNK
;
5045 /* Ignore if snapshot consumer output is not network. */
5046 if (output
->type
!= CONSUMER_DST_NET
) {
5051 * The snapshot record URI base path overrides the session
5054 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5055 base_path
= output
->dst
.net
.control
.subdir
;
5057 base_path
= session
->base_path
;
5061 * For each consumer socket, create and send the relayd object of the
5065 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
5066 socket
, node
.node
) {
5067 pthread_mutex_lock(socket
->lock
);
5068 status
= send_consumer_relayd_sockets(0, session
->id
,
5070 session
->name
, session
->hostname
,
5072 session
->live_timer
,
5073 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
5074 session
->creation_time
,
5075 session
->name_contains_creation_time
);
5076 pthread_mutex_unlock(socket
->lock
);
5077 if (status
!= LTTNG_OK
) {
5089 * Record a kernel snapshot.
5091 * Return LTTNG_OK on success or a LTTNG_ERR code.
5093 static enum lttng_error_code
record_kernel_snapshot(
5094 struct ltt_kernel_session
*ksess
,
5095 const struct consumer_output
*output
,
5096 const struct ltt_session
*session
,
5097 int wait
, uint64_t nb_packets_per_stream
)
5099 enum lttng_error_code status
;
5105 status
= kernel_snapshot_record(
5106 ksess
, output
, wait
, nb_packets_per_stream
);
5111 * Record a UST snapshot.
5113 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5115 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5116 const struct consumer_output
*output
,
5117 const struct ltt_session
*session
,
5118 int wait
, uint64_t nb_packets_per_stream
)
5120 enum lttng_error_code status
;
5126 status
= ust_app_snapshot_record(
5127 usess
, output
, wait
, nb_packets_per_stream
);
5132 uint64_t get_session_size_one_more_packet_per_stream(
5133 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5135 uint64_t tot_size
= 0;
5137 if (session
->kernel_session
) {
5138 struct ltt_kernel_channel
*chan
;
5139 const struct ltt_kernel_session
*ksess
=
5140 session
->kernel_session
;
5142 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5143 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5145 * Don't take channel into account if we
5146 * already grab all its packets.
5150 tot_size
+= chan
->channel
->attr
.subbuf_size
5151 * chan
->stream_count
;
5155 if (session
->ust_session
) {
5156 const struct ltt_ust_session
*usess
= session
->ust_session
;
5158 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5166 * Calculate the number of packets we can grab from each stream that
5167 * fits within the overall snapshot max size.
5169 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5170 * the number of packets per stream.
5172 * TODO: this approach is not perfect: we consider the worse case
5173 * (packet filling the sub-buffers) as an upper bound, but we could do
5174 * better if we do this calculation while we actually grab the packet
5175 * content: we would know how much padding we don't actually store into
5178 * This algorithm is currently bounded by the number of packets per
5181 * Since we call this algorithm before actually grabbing the data, it's
5182 * an approximation: for instance, applications could appear/disappear
5183 * in between this call and actually grabbing data.
5186 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5190 uint64_t cur_nb_packets
= 0;
5193 return 0; /* Infinite */
5196 size_left
= max_size
;
5198 uint64_t one_more_packet_tot_size
;
5200 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5201 session
, cur_nb_packets
);
5202 if (!one_more_packet_tot_size
) {
5203 /* We are already grabbing all packets. */
5206 size_left
-= one_more_packet_tot_size
;
5207 if (size_left
< 0) {
5212 if (!cur_nb_packets
&& size_left
!= max_size
) {
5213 /* Not enough room to grab one packet of each stream, error. */
5216 return cur_nb_packets
;
5220 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5221 const struct snapshot_output
*snapshot_output
, int wait
)
5223 int64_t nb_packets_per_stream
;
5224 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5226 enum lttng_error_code ret_code
= LTTNG_OK
;
5227 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5228 struct consumer_output
*original_ust_consumer_output
= NULL
;
5229 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5230 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5231 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5233 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5235 snapshot_output
->name
,
5236 snapshot_output
->datetime
,
5237 snapshot_output
->nb_snapshot
);
5238 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5239 ERR("Failed to format snapshot name");
5240 ret_code
= LTTNG_ERR_INVALID
;
5243 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5244 snapshot_output
->name
, session
->name
,
5245 snapshot_chunk_name
);
5246 if (!session
->kernel_session
&& !session
->ust_session
) {
5247 ERR("Failed to record snapshot as no channels exist");
5248 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5252 if (session
->kernel_session
) {
5253 original_kernel_consumer_output
=
5254 session
->kernel_session
->consumer
;
5255 snapshot_kernel_consumer_output
=
5256 consumer_copy_output(snapshot_output
->consumer
);
5257 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5258 snapshot_chunk_name
);
5260 /* Copy the original domain subdir. */
5261 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5262 original_kernel_consumer_output
->domain_subdir
);
5264 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5265 original_kernel_consumer_output
);
5267 ERR("Failed to copy consumer sockets from snapshot output configuration");
5268 ret_code
= LTTNG_ERR_NOMEM
;
5271 ret_code
= set_relayd_for_snapshot(
5272 snapshot_kernel_consumer_output
, session
);
5273 if (ret_code
!= LTTNG_OK
) {
5274 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5277 session
->kernel_session
->consumer
=
5278 snapshot_kernel_consumer_output
;
5280 if (session
->ust_session
) {
5281 original_ust_consumer_output
= session
->ust_session
->consumer
;
5282 snapshot_ust_consumer_output
=
5283 consumer_copy_output(snapshot_output
->consumer
);
5284 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5285 snapshot_chunk_name
);
5287 /* Copy the original domain subdir. */
5288 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5289 original_ust_consumer_output
->domain_subdir
);
5291 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5292 original_ust_consumer_output
);
5294 ERR("Failed to copy consumer sockets from snapshot output configuration");
5295 ret_code
= LTTNG_ERR_NOMEM
;
5298 ret_code
= set_relayd_for_snapshot(
5299 snapshot_ust_consumer_output
, session
);
5300 if (ret_code
!= LTTNG_OK
) {
5301 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5304 session
->ust_session
->consumer
=
5305 snapshot_ust_consumer_output
;
5308 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5309 snapshot_kernel_consumer_output
?:
5310 snapshot_ust_consumer_output
,
5311 consumer_output_get_base_path(
5312 snapshot_output
->consumer
),
5313 snapshot_chunk_name
);
5314 if (!snapshot_trace_chunk
) {
5315 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5317 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5320 assert(!session
->current_trace_chunk
);
5321 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5322 lttng_trace_chunk_put(snapshot_trace_chunk
);
5323 snapshot_trace_chunk
= NULL
;
5325 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5327 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5331 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5332 snapshot_output
->max_size
);
5333 if (nb_packets_per_stream
< 0) {
5334 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5335 goto error_close_trace_chunk
;
5338 if (session
->kernel_session
) {
5339 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5340 snapshot_kernel_consumer_output
, session
,
5341 wait
, nb_packets_per_stream
);
5342 if (ret_code
!= LTTNG_OK
) {
5343 goto error_close_trace_chunk
;
5347 if (session
->ust_session
) {
5348 ret_code
= record_ust_snapshot(session
->ust_session
,
5349 snapshot_ust_consumer_output
, session
,
5350 wait
, nb_packets_per_stream
);
5351 if (ret_code
!= LTTNG_OK
) {
5352 goto error_close_trace_chunk
;
5356 error_close_trace_chunk
:
5357 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5358 ERR("Failed to release the current trace chunk of session \"%s\"",
5360 ret_code
= LTTNG_ERR_UNK
;
5363 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5364 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5366 * Don't goto end; make sure the chunk is closed for the session
5367 * to allow future snapshots.
5369 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5371 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5374 lttng_trace_chunk_put(snapshot_trace_chunk
);
5375 snapshot_trace_chunk
= NULL
;
5377 if (original_ust_consumer_output
) {
5378 session
->ust_session
->consumer
= original_ust_consumer_output
;
5380 if (original_kernel_consumer_output
) {
5381 session
->kernel_session
->consumer
=
5382 original_kernel_consumer_output
;
5384 consumer_output_put(snapshot_ust_consumer_output
);
5385 consumer_output_put(snapshot_kernel_consumer_output
);
5390 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5392 * The wait parameter is ignored so this call always wait for the snapshot to
5393 * complete before returning.
5395 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5397 int cmd_snapshot_record(struct ltt_session
*session
,
5398 const struct lttng_snapshot_output
*output
, int wait
)
5400 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5402 unsigned int snapshot_success
= 0;
5404 struct snapshot_output
*tmp_output
= NULL
;
5409 DBG("Cmd snapshot record for session %s", session
->name
);
5411 /* Get the datetime for the snapshot output directory. */
5412 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5415 cmd_ret
= LTTNG_ERR_INVALID
;
5420 * Permission denied to create an output if the session is not
5421 * set in no output mode.
5423 if (session
->output_traces
) {
5424 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5428 /* The session needs to be started at least once. */
5429 if (!session
->has_been_started
) {
5430 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5434 /* Use temporary output for the session. */
5435 if (*output
->ctrl_url
!= '\0') {
5436 tmp_output
= snapshot_output_alloc();
5438 cmd_ret
= LTTNG_ERR_NOMEM
;
5442 ret
= snapshot_output_init(session
, output
->max_size
,
5444 output
->ctrl_url
, output
->data_url
,
5448 if (ret
== -ENOMEM
) {
5449 cmd_ret
= LTTNG_ERR_NOMEM
;
5451 cmd_ret
= LTTNG_ERR_INVALID
;
5455 /* Use the global session count for the temporary snapshot. */
5456 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5458 /* Use the global datetime */
5459 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5460 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
5461 if (cmd_ret
!= LTTNG_OK
) {
5464 snapshot_success
= 1;
5466 struct snapshot_output
*sout
;
5467 struct lttng_ht_iter iter
;
5470 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5471 &iter
.iter
, sout
, node
.node
) {
5472 struct snapshot_output output_copy
;
5475 * Make a local copy of the output and override output
5476 * parameters with those provided as part of the
5479 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5481 if (output
->max_size
!= (uint64_t) -1ULL) {
5482 output_copy
.max_size
= output
->max_size
;
5485 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5486 memcpy(output_copy
.datetime
, datetime
,
5489 /* Use temporary name. */
5490 if (*output
->name
!= '\0') {
5491 if (lttng_strncpy(output_copy
.name
,
5493 sizeof(output_copy
.name
))) {
5494 cmd_ret
= LTTNG_ERR_INVALID
;
5500 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
5501 if (cmd_ret
!= LTTNG_OK
) {
5505 snapshot_success
= 1;
5510 if (snapshot_success
) {
5511 session
->snapshot
.nb_snapshot
++;
5513 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5518 snapshot_output_destroy(tmp_output
);
5524 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5526 int cmd_set_session_shm_path(struct ltt_session
*session
,
5527 const char *shm_path
)
5533 * Can only set shm path before session is started.
5535 if (session
->has_been_started
) {
5536 return LTTNG_ERR_SESSION_STARTED
;
5539 strncpy(session
->shm_path
, shm_path
,
5540 sizeof(session
->shm_path
));
5541 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5547 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5549 * Ask the consumer to rotate the session output directory.
5550 * The session lock must be held.
5552 * Returns LTTNG_OK on success or else a negative LTTng error code.
5554 int cmd_rotate_session(struct ltt_session
*session
,
5555 struct lttng_rotate_session_return
*rotate_return
,
5556 bool quiet_rotation
,
5557 enum lttng_trace_chunk_command_type command
)
5560 uint64_t ongoing_rotation_chunk_id
;
5561 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5562 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5563 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5564 enum lttng_trace_chunk_status chunk_status
;
5565 bool failed_to_rotate
= false;
5566 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5570 if (!session
->has_been_started
) {
5571 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5576 * Explicit rotation is not supported for live sessions.
5577 * However, live sessions can perform a quiet rotation on
5579 * Rotation is not supported for snapshot traces (no output).
5581 if ((!quiet_rotation
&& session
->live_timer
) ||
5582 !session
->output_traces
) {
5583 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5587 /* Unsupported feature in lttng-relayd before 2.11. */
5588 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5589 (session
->consumer
->relay_major_version
== 2 &&
5590 session
->consumer
->relay_minor_version
< 11)) {
5591 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5595 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5596 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5597 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5601 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5602 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5604 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5609 * After a stop, we only allow one rotation to occur, the other ones are
5610 * useless until a new start.
5612 if (session
->rotated_after_last_stop
) {
5613 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5615 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5620 * After a stop followed by a clear, disallow following rotations a they would
5621 * generate empty chunks.
5623 if (session
->cleared_after_last_stop
) {
5624 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5626 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5630 if (session
->active
) {
5631 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5633 if (!new_trace_chunk
) {
5634 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5640 * The current trace chunk becomes the chunk being archived.
5642 * After this point, "chunk_being_archived" must absolutely
5643 * be closed on the consumer(s), otherwise it will never be
5644 * cleaned-up, which will result in a leak.
5646 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5647 &chunk_being_archived
);
5649 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5653 if (session
->kernel_session
) {
5654 cmd_ret
= kernel_rotate_session(session
);
5655 if (cmd_ret
!= LTTNG_OK
) {
5656 failed_to_rotate
= true;
5657 rotation_fail_code
= cmd_ret
;
5660 if (session
->ust_session
) {
5661 cmd_ret
= ust_app_rotate_session(session
);
5662 if (cmd_ret
!= LTTNG_OK
) {
5663 failed_to_rotate
= true;
5664 rotation_fail_code
= cmd_ret
;
5668 if (!session
->active
) {
5669 session
->rotated_after_last_stop
= true;
5672 if (!chunk_being_archived
) {
5673 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5675 if (failed_to_rotate
) {
5676 cmd_ret
= rotation_fail_code
;
5683 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5684 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5685 &ongoing_rotation_chunk_id
);
5686 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5688 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5689 command
, session
->last_chunk_path
);
5691 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5695 if (failed_to_rotate
) {
5696 cmd_ret
= rotation_fail_code
;
5700 session
->quiet_rotation
= quiet_rotation
;
5701 ret
= timer_session_rotation_pending_check_start(session
,
5702 DEFAULT_ROTATE_PENDING_TIMER
);
5704 cmd_ret
= LTTNG_ERR_UNK
;
5708 if (rotate_return
) {
5709 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5712 session
->chunk_being_archived
= chunk_being_archived
;
5713 chunk_being_archived
= NULL
;
5714 if (!quiet_rotation
) {
5715 ret
= notification_thread_command_session_rotation_ongoing(
5716 the_notification_thread_handle
, session
->name
,
5717 session
->uid
, session
->gid
,
5718 ongoing_rotation_chunk_id
);
5719 if (ret
!= LTTNG_OK
) {
5720 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5726 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5727 session
->name
, ongoing_rotation_chunk_id
);
5729 lttng_trace_chunk_put(new_trace_chunk
);
5730 lttng_trace_chunk_put(chunk_being_archived
);
5731 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5734 if (session_reset_rotation_state(session
,
5735 LTTNG_ROTATION_STATE_ERROR
)) {
5736 ERR("Failed to reset rotation state of session \"%s\"",
5743 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5745 * Check if the session has finished its rotation.
5747 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5749 int cmd_rotate_get_info(struct ltt_session
*session
,
5750 struct lttng_rotation_get_info_return
*info_return
,
5751 uint64_t rotation_id
)
5753 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5754 enum lttng_rotation_state rotation_state
;
5756 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5757 session
->most_recent_chunk_id
.value
);
5759 if (session
->chunk_being_archived
) {
5760 enum lttng_trace_chunk_status chunk_status
;
5763 chunk_status
= lttng_trace_chunk_get_id(
5764 session
->chunk_being_archived
,
5766 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5768 rotation_state
= rotation_id
== chunk_id
?
5769 LTTNG_ROTATION_STATE_ONGOING
:
5770 LTTNG_ROTATION_STATE_EXPIRED
;
5772 if (session
->last_archived_chunk_id
.is_set
&&
5773 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5774 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5776 rotation_state
= session
->rotation_state
;
5780 switch (rotation_state
) {
5781 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5782 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5785 case LTTNG_ROTATION_STATE_EXPIRED
:
5786 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5787 rotation_id
, session
->name
);
5789 case LTTNG_ROTATION_STATE_ONGOING
:
5790 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5791 rotation_id
, session
->name
);
5793 case LTTNG_ROTATION_STATE_COMPLETED
:
5797 char *current_tracing_path_reply
;
5798 size_t current_tracing_path_reply_len
;
5800 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5801 rotation_id
, session
->name
);
5803 switch (session_get_consumer_destination_type(session
)) {
5804 case CONSUMER_DST_LOCAL
:
5805 current_tracing_path_reply
=
5806 info_return
->location
.local
.absolute_path
;
5807 current_tracing_path_reply_len
=
5808 sizeof(info_return
->location
.local
.absolute_path
);
5809 info_return
->location_type
=
5810 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5811 fmt_ret
= asprintf(&chunk_path
,
5812 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5813 session_get_base_path(session
),
5814 session
->last_archived_chunk_name
);
5815 if (fmt_ret
== -1) {
5816 PERROR("Failed to format the path of the last archived trace chunk");
5817 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5818 cmd_ret
= LTTNG_ERR_UNK
;
5822 case CONSUMER_DST_NET
:
5824 uint16_t ctrl_port
, data_port
;
5826 current_tracing_path_reply
=
5827 info_return
->location
.relay
.relative_path
;
5828 current_tracing_path_reply_len
=
5829 sizeof(info_return
->location
.relay
.relative_path
);
5830 /* Currently the only supported relay protocol. */
5831 info_return
->location
.relay
.protocol
=
5832 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5834 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5835 session_get_net_consumer_hostname(session
),
5836 sizeof(info_return
->location
.relay
.host
));
5838 ERR("Failed to copy host name to rotate_get_info reply");
5839 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5840 cmd_ret
= LTTNG_ERR_SET_URL
;
5844 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5845 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5846 info_return
->location
.relay
.ports
.data
= data_port
;
5847 info_return
->location_type
=
5848 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5849 chunk_path
= strdup(session
->last_chunk_path
);
5851 ERR("Failed to allocate the path of the last archived trace chunk");
5852 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5853 cmd_ret
= LTTNG_ERR_UNK
;
5862 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5863 chunk_path
, current_tracing_path_reply_len
);
5866 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5867 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5868 cmd_ret
= LTTNG_ERR_UNK
;
5874 case LTTNG_ROTATION_STATE_ERROR
:
5875 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5876 rotation_id
, session
->name
);
5884 info_return
->status
= (int32_t) rotation_state
;
5889 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5891 * Configure the automatic rotation parameters.
5892 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5893 * 'activate' to false means deactivate the rotation schedule and validate that
5894 * 'new_value' has the same value as the currently active value.
5896 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5898 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5899 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5901 struct notification_thread_handle
*notification_thread_handle
)
5904 uint64_t *parameter_value
;
5908 DBG("Cmd rotate set schedule session %s", session
->name
);
5910 if (session
->live_timer
|| !session
->output_traces
) {
5911 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5912 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5916 switch (schedule_type
) {
5917 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5918 parameter_value
= &session
->rotate_size
;
5920 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5921 parameter_value
= &session
->rotate_timer_period
;
5922 if (new_value
>= UINT_MAX
) {
5923 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5924 new_value
, UINT_MAX
);
5925 ret
= LTTNG_ERR_INVALID
;
5930 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5931 ret
= LTTNG_ERR_INVALID
;
5935 /* Improper use of the API. */
5936 if (new_value
== -1ULL) {
5937 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5938 ret
= LTTNG_ERR_INVALID
;
5943 * As indicated in struct ltt_session's comments, a value of == 0 means
5944 * this schedule rotation type is not in use.
5946 * Reject the command if we were asked to activate a schedule that was
5949 if (activate
&& *parameter_value
!= 0) {
5950 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5951 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5956 * Reject the command if we were asked to deactivate a schedule that was
5959 if (!activate
&& *parameter_value
== 0) {
5960 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5961 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5966 * Reject the command if we were asked to deactivate a schedule that
5969 if (!activate
&& *parameter_value
!= new_value
) {
5970 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5971 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5975 *parameter_value
= activate
? new_value
: 0;
5977 switch (schedule_type
) {
5978 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5979 if (activate
&& session
->active
) {
5981 * Only start the timer if the session is active,
5982 * otherwise it will be started when the session starts.
5984 ret
= timer_session_rotation_schedule_timer_start(
5985 session
, new_value
);
5987 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5988 ret
= LTTNG_ERR_UNK
;
5992 ret
= timer_session_rotation_schedule_timer_stop(
5995 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5996 ret
= LTTNG_ERR_UNK
;
6001 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
6003 ret
= subscribe_session_consumed_size_rotation(session
,
6004 new_value
, notification_thread_handle
);
6006 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
6007 ret
= LTTNG_ERR_UNK
;
6011 ret
= unsubscribe_session_consumed_size_rotation(session
,
6012 notification_thread_handle
);
6014 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
6015 ret
= LTTNG_ERR_UNK
;
6022 /* Would have been caught before. */
6034 /* Wait for a given path to be removed before continuing. */
6035 static enum lttng_error_code
wait_on_path(void *path_data
)
6037 const char *shm_path
= path_data
;
6039 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6045 ret
= stat(shm_path
, &st
);
6047 if (errno
!= ENOENT
) {
6048 PERROR("stat() returned an error while checking for the existence of the shm path");
6050 DBG("shm path no longer exists, completing the destruction of session");
6054 if (!S_ISDIR(st
.st_mode
)) {
6055 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6060 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6066 * Returns a pointer to a handler to run on completion of a command.
6067 * Returns NULL if no handler has to be run for the last command executed.
6069 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6071 struct cmd_completion_handler
*handler
= current_completion_handler
;
6073 current_completion_handler
= NULL
;
6078 * Init command subsystem.
6083 * Set network sequence index to 1 for streams to match a relayd
6084 * socket on the consumer side.
6086 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6087 relayd_net_seq_idx
= 1;
6088 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6090 DBG("Command subsystem initialized");