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.hpp>
19 #include <common/common.hpp>
20 #include <common/compat/string.hpp>
21 #include <common/defaults.hpp>
22 #include <common/dynamic-buffer.hpp>
23 #include <common/kernel-ctl/kernel-ctl.hpp>
24 #include <common/payload-view.hpp>
25 #include <common/payload.hpp>
26 #include <common/relayd/relayd.hpp>
27 #include <common/sessiond-comm/sessiond-comm.hpp>
28 #include <common/string-utils/string-utils.hpp>
29 #include <common/trace-chunk.hpp>
30 #include <common/utils.hpp>
31 #include <lttng/action/action-internal.hpp>
32 #include <lttng/action/action.h>
33 #include <lttng/channel-internal.hpp>
34 #include <lttng/channel.h>
35 #include <lttng/condition/condition-internal.hpp>
36 #include <lttng/condition/condition.h>
37 #include <lttng/condition/event-rule-matches-internal.hpp>
38 #include <lttng/condition/event-rule-matches.h>
39 #include <lttng/error-query-internal.hpp>
40 #include <lttng/event-internal.hpp>
41 #include <lttng/event-rule/event-rule-internal.hpp>
42 #include <lttng/event-rule/event-rule.h>
43 #include <lttng/location-internal.hpp>
44 #include <lttng/lttng-error.h>
45 #include <lttng/rotate-internal.hpp>
46 #include <lttng/session-descriptor-internal.hpp>
47 #include <lttng/session-internal.hpp>
48 #include <lttng/tracker.h>
49 #include <lttng/trigger/trigger-internal.hpp>
50 #include <lttng/userspace-probe-internal.hpp>
52 #include "agent-thread.hpp"
54 #include "buffer-registry.hpp"
55 #include "channel.hpp"
57 #include "consumer.hpp"
58 #include "event-notifier-error-accounting.hpp"
60 #include "health-sessiond.hpp"
61 #include "kernel-consumer.hpp"
63 #include "lttng-sessiond.hpp"
64 #include "lttng-syscall.hpp"
65 #include "notification-thread-commands.hpp"
66 #include "notification-thread.hpp"
68 #include "rotation-thread.hpp"
69 #include "session.hpp"
71 #include "tracker.hpp"
74 /* Sleep for 100ms between each check for the shm path's deletion. */
75 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
77 struct cmd_destroy_session_reply_context
{
79 bool implicit_rotation_on_destroy
;
81 * Indicates whether or not an error occurred while launching the
82 * destruction of a session.
84 enum lttng_error_code destruction_status
;
87 static enum lttng_error_code
wait_on_path(void *path
);
90 * Command completion handler that is used by the destroy command
91 * when a session that has a non-default shm_path is being destroyed.
93 * See comment in cmd_destroy_session() for the rationale.
95 static struct destroy_completion_handler
{
96 struct cmd_completion_handler handler
;
97 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
98 } destroy_completion_handler
= {
101 .data
= destroy_completion_handler
.shm_path
106 static struct cmd_completion_handler
*current_completion_handler
;
109 * Used to keep a unique index for each relayd socket created where this value
110 * is associated with streams on the consumer so it can match the right relayd
111 * to send to. It must be accessed with the relayd_net_seq_idx_lock
114 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
115 static uint64_t relayd_net_seq_idx
;
117 static int validate_ust_event_name(const char *);
118 static int cmd_enable_event_internal(struct ltt_session
*session
,
119 const struct lttng_domain
*domain
,
120 char *channel_name
, struct lttng_event
*event
,
121 char *filter_expression
,
122 struct lttng_bytecode
*filter
,
123 struct lttng_event_exclusion
*exclusion
,
125 static enum lttng_error_code
cmd_enable_channel_internal(
126 struct ltt_session
*session
,
127 const struct lttng_domain
*domain
,
128 const struct lttng_channel
*_attr
,
132 * Create a session path used by list_lttng_sessions for the case that the
133 * session consumer is on the network.
135 static int build_network_session_path(char *dst
, size_t size
,
136 struct ltt_session
*session
)
138 int ret
, kdata_port
, udata_port
;
139 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
140 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
142 LTTNG_ASSERT(session
);
145 memset(tmp_urls
, 0, sizeof(tmp_urls
));
146 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
148 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
150 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
151 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
152 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
155 if (session
->ust_session
&& session
->ust_session
->consumer
) {
156 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
157 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
160 if (uuri
== NULL
&& kuri
== NULL
) {
161 uri
= &session
->consumer
->dst
.net
.control
;
162 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
163 } else if (kuri
&& uuri
) {
164 ret
= uri_compare(kuri
, uuri
);
168 /* Build uuri URL string */
169 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
176 } else if (kuri
&& uuri
== NULL
) {
178 } else if (uuri
&& kuri
== NULL
) {
182 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
188 * Do we have a UST url set. If yes, this means we have both kernel and UST
191 if (*tmp_uurl
!= '\0') {
192 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
193 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
196 if (kuri
|| (!kuri
&& !uuri
)) {
199 /* No kernel URI, use the UST port. */
202 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
210 * Get run-time attributes if the session has been started (discarded events,
213 static int get_kernel_runtime_stats(struct ltt_session
*session
,
214 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
215 uint64_t *lost_packets
)
219 if (!session
->has_been_started
) {
221 *discarded_events
= 0;
226 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
227 session
->kernel_session
->consumer
,
233 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
234 session
->kernel_session
->consumer
,
245 * Get run-time attributes if the session has been started (discarded events,
248 static int get_ust_runtime_stats(struct ltt_session
*session
,
249 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
250 uint64_t *lost_packets
)
253 struct ltt_ust_session
*usess
;
255 if (!discarded_events
|| !lost_packets
) {
260 usess
= session
->ust_session
;
261 LTTNG_ASSERT(discarded_events
);
262 LTTNG_ASSERT(lost_packets
);
264 if (!usess
|| !session
->has_been_started
) {
265 *discarded_events
= 0;
271 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
272 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
273 &usess
->buffer_reg_uid_list
,
274 usess
->consumer
, uchan
->id
,
275 uchan
->attr
.overwrite
,
278 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
279 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
280 uchan
, usess
->consumer
,
281 uchan
->attr
.overwrite
,
287 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
288 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
290 ERR("Unsupported buffer type");
301 * Create a list of agent domain events.
303 * Return number of events in list on success or else a negative value.
305 static enum lttng_error_code
list_lttng_agent_events(
306 struct agent
*agt
, struct lttng_payload
*reply_payload
,
307 unsigned int *nb_events
)
309 enum lttng_error_code ret_code
;
311 unsigned int local_nb_events
= 0;
312 struct agent_event
*event
;
313 struct lttng_ht_iter iter
;
314 unsigned long agent_event_count
;
317 assert(reply_payload
);
319 DBG3("Listing agent events");
322 agent_event_count
= lttng_ht_get_count(agt
->events
);
323 if (agent_event_count
== 0) {
328 if (agent_event_count
> UINT_MAX
) {
329 ret_code
= LTTNG_ERR_OVERFLOW
;
333 local_nb_events
= (unsigned int) agent_event_count
;
335 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
336 struct lttng_event
*tmp_event
= lttng_event_create();
339 ret_code
= LTTNG_ERR_NOMEM
;
343 if (lttng_strncpy(tmp_event
->name
, event
->name
, sizeof(tmp_event
->name
))) {
344 lttng_event_destroy(tmp_event
);
345 ret_code
= LTTNG_ERR_FATAL
;
349 tmp_event
->name
[sizeof(tmp_event
->name
) - 1] = '\0';
350 tmp_event
->enabled
= !!event
->enabled_count
;
351 tmp_event
->loglevel
= event
->loglevel_value
;
352 tmp_event
->loglevel_type
= event
->loglevel_type
;
354 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
355 event
->filter_expression
, 0, NULL
, reply_payload
);
356 lttng_event_destroy(tmp_event
);
358 ret_code
= LTTNG_ERR_FATAL
;
365 *nb_events
= local_nb_events
;
372 * Create a list of ust global domain events.
374 static enum lttng_error_code
list_lttng_ust_global_events(char *channel_name
,
375 struct ltt_ust_domain_global
*ust_global
,
376 struct lttng_payload
*reply_payload
,
377 unsigned int *nb_events
)
379 enum lttng_error_code ret_code
;
381 struct lttng_ht_iter iter
;
382 struct lttng_ht_node_str
*node
;
383 struct ltt_ust_channel
*uchan
;
384 struct ltt_ust_event
*uevent
;
385 unsigned long channel_event_count
;
386 unsigned int local_nb_events
= 0;
388 assert(reply_payload
);
391 DBG("Listing UST global events for channel %s", channel_name
);
395 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
396 node
= lttng_ht_iter_get_node_str(&iter
);
398 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
402 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
404 channel_event_count
= lttng_ht_get_count(uchan
->events
);
405 if (channel_event_count
== 0) {
411 if (channel_event_count
> UINT_MAX
) {
412 ret_code
= LTTNG_ERR_OVERFLOW
;
416 local_nb_events
= (unsigned int) channel_event_count
;
418 DBG3("Listing UST global %d events", *nb_events
);
420 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
421 struct lttng_event
*tmp_event
= NULL
;
423 if (uevent
->internal
) {
424 /* This event should remain hidden from clients */
429 tmp_event
= lttng_event_create();
431 ret_code
= LTTNG_ERR_NOMEM
;
435 if (lttng_strncpy(tmp_event
->name
, uevent
->attr
.name
,
436 LTTNG_SYMBOL_NAME_LEN
)) {
437 ret_code
= LTTNG_ERR_FATAL
;
438 lttng_event_destroy(tmp_event
);
442 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
443 tmp_event
->enabled
= uevent
->enabled
;
445 switch (uevent
->attr
.instrumentation
) {
446 case LTTNG_UST_ABI_TRACEPOINT
:
447 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
449 case LTTNG_UST_ABI_PROBE
:
450 tmp_event
->type
= LTTNG_EVENT_PROBE
;
452 case LTTNG_UST_ABI_FUNCTION
:
453 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
457 tmp_event
->loglevel
= uevent
->attr
.loglevel
;
458 switch (uevent
->attr
.loglevel_type
) {
459 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
460 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
462 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
463 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
465 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
466 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
469 if (uevent
->filter
) {
470 tmp_event
->filter
= 1;
472 if (uevent
->exclusion
) {
473 tmp_event
->exclusion
= 1;
477 * We do not care about the filter bytecode and the fd from the
478 * userspace_probe_location.
480 ret
= lttng_event_serialize(tmp_event
, uevent
->exclusion
? uevent
->exclusion
->count
: 0,
481 uevent
->exclusion
? (char **) uevent
->exclusion
->names
: NULL
,
482 uevent
->filter_expression
, 0, NULL
, reply_payload
);
483 lttng_event_destroy(tmp_event
);
485 ret_code
= LTTNG_ERR_FATAL
;
491 /* nb_events is already set at this point. */
493 *nb_events
= local_nb_events
;
500 * Fill lttng_event array of all kernel events in the channel.
502 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
503 struct ltt_kernel_session
*kernel_session
,
504 struct lttng_payload
*reply_payload
,
505 unsigned int *nb_events
)
507 enum lttng_error_code ret_code
;
509 struct ltt_kernel_event
*event
;
510 struct ltt_kernel_channel
*kchan
;
512 assert(reply_payload
);
514 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
516 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
520 *nb_events
= kchan
->event_count
;
522 DBG("Listing events for channel %s", kchan
->channel
->name
);
524 if (*nb_events
== 0) {
529 /* Kernel channels */
530 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
531 struct lttng_event
*tmp_event
= lttng_event_create();
534 ret_code
= LTTNG_ERR_NOMEM
;
538 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
539 lttng_event_destroy(tmp_event
);
540 ret_code
= LTTNG_ERR_FATAL
;
545 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
546 tmp_event
->enabled
= event
->enabled
;
547 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
549 switch (event
->event
->instrumentation
) {
550 case LTTNG_KERNEL_ABI_TRACEPOINT
:
551 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
553 case LTTNG_KERNEL_ABI_KRETPROBE
:
554 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
555 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
556 sizeof(struct lttng_kernel_abi_kprobe
));
558 case LTTNG_KERNEL_ABI_KPROBE
:
559 tmp_event
->type
= LTTNG_EVENT_PROBE
;
560 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
561 sizeof(struct lttng_kernel_abi_kprobe
));
563 case LTTNG_KERNEL_ABI_UPROBE
:
564 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
566 case LTTNG_KERNEL_ABI_FUNCTION
:
567 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
568 memcpy(&(tmp_event
->attr
.ftrace
), &event
->event
->u
.ftrace
,
569 sizeof(struct lttng_kernel_abi_function
));
571 case LTTNG_KERNEL_ABI_NOOP
:
572 tmp_event
->type
= LTTNG_EVENT_NOOP
;
574 case LTTNG_KERNEL_ABI_SYSCALL
:
575 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
577 case LTTNG_KERNEL_ABI_ALL
:
584 if (event
->userspace_probe_location
) {
585 struct lttng_userspace_probe_location
*location_copy
=
586 lttng_userspace_probe_location_copy(
587 event
->userspace_probe_location
);
589 if (!location_copy
) {
590 lttng_event_destroy(tmp_event
);
591 ret_code
= LTTNG_ERR_NOMEM
;
595 ret
= lttng_event_set_userspace_probe_location(
596 tmp_event
, location_copy
);
598 lttng_event_destroy(tmp_event
);
599 lttng_userspace_probe_location_destroy(
601 ret_code
= LTTNG_ERR_INVALID
;
606 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
607 event
->filter_expression
, 0, NULL
, reply_payload
);
608 lttng_event_destroy(tmp_event
);
610 ret_code
= LTTNG_ERR_FATAL
;
621 * Add URI so the consumer output object. Set the correct path depending on the
622 * domain adding the default trace directory.
624 static enum lttng_error_code
add_uri_to_consumer(
625 const struct ltt_session
*session
,
626 struct consumer_output
*consumer
,
627 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
630 enum lttng_error_code ret_code
= LTTNG_OK
;
634 if (consumer
== NULL
) {
635 DBG("No consumer detected. Don't add URI. Stopping.");
636 ret_code
= LTTNG_ERR_NO_CONSUMER
;
641 case LTTNG_DOMAIN_KERNEL
:
642 ret
= lttng_strncpy(consumer
->domain_subdir
,
643 DEFAULT_KERNEL_TRACE_DIR
,
644 sizeof(consumer
->domain_subdir
));
646 case LTTNG_DOMAIN_UST
:
647 ret
= lttng_strncpy(consumer
->domain_subdir
,
648 DEFAULT_UST_TRACE_DIR
,
649 sizeof(consumer
->domain_subdir
));
653 * This case is possible is we try to add the URI to the global
654 * tracing session consumer object which in this case there is
657 memset(consumer
->domain_subdir
, 0,
658 sizeof(consumer
->domain_subdir
));
662 ERR("Failed to initialize consumer output domain subdirectory");
663 ret_code
= LTTNG_ERR_FATAL
;
667 switch (uri
->dtype
) {
670 DBG2("Setting network URI to consumer");
672 if (consumer
->type
== CONSUMER_DST_NET
) {
673 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
674 consumer
->dst
.net
.control_isset
) ||
675 (uri
->stype
== LTTNG_STREAM_DATA
&&
676 consumer
->dst
.net
.data_isset
)) {
677 ret_code
= LTTNG_ERR_URL_EXIST
;
681 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
684 /* Set URI into consumer output object */
685 ret
= consumer_set_network_uri(session
, consumer
, uri
);
687 ret_code
= (lttng_error_code
) -ret
;
689 } else if (ret
== 1) {
691 * URI was the same in the consumer so we do not append the subdir
692 * again so to not duplicate output dir.
699 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
700 ret_code
= LTTNG_ERR_INVALID
;
703 DBG2("Setting trace directory path from URI to %s",
705 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
707 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
709 sizeof(consumer
->dst
.session_root_path
));
711 ret_code
= LTTNG_ERR_FATAL
;
714 consumer
->type
= CONSUMER_DST_LOCAL
;
724 * Init tracing by creating trace directory and sending fds kernel consumer.
726 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
729 struct lttng_ht_iter iter
;
730 struct consumer_socket
*socket
;
732 LTTNG_ASSERT(session
);
736 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
737 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
739 pthread_mutex_lock(socket
->lock
);
740 ret
= kernel_consumer_send_session(socket
, session
);
741 pthread_mutex_unlock(socket
->lock
);
743 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
755 * Create a socket to the relayd using the URI.
757 * On success, the relayd_sock pointer is set to the created socket.
758 * Else, it remains untouched and an LTTng error code is returned.
760 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
761 struct lttcomm_relayd_sock
**relayd_sock
,
762 struct consumer_output
*consumer
)
765 enum lttng_error_code status
= LTTNG_OK
;
766 struct lttcomm_relayd_sock
*rsock
;
768 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
769 RELAYD_VERSION_COMM_MINOR
);
771 status
= LTTNG_ERR_FATAL
;
776 * Connect to relayd so we can proceed with a session creation. This call
777 * can possibly block for an arbitrary amount of time to set the health
778 * state to be in poll execution.
781 ret
= relayd_connect(rsock
);
784 ERR("Unable to reach lttng-relayd");
785 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
789 /* Create socket for control stream. */
790 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
791 uint64_t result_flags
;
793 DBG3("Creating relayd stream socket from URI");
795 /* Check relayd version */
796 ret
= relayd_version_check(rsock
);
797 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
798 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
800 } else if (ret
< 0) {
801 ERR("Unable to reach lttng-relayd");
802 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
805 consumer
->relay_major_version
= rsock
->major
;
806 consumer
->relay_minor_version
= rsock
->minor
;
807 ret
= relayd_get_configuration(rsock
, 0,
810 ERR("Unable to get relayd configuration");
811 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
814 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
815 consumer
->relay_allows_clear
= true;
817 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
818 DBG3("Creating relayd data socket from URI");
820 /* Command is not valid */
821 ERR("Relayd invalid stream type: %d", uri
->stype
);
822 status
= LTTNG_ERR_INVALID
;
826 *relayd_sock
= rsock
;
831 /* The returned value is not useful since we are on an error path. */
832 (void) relayd_close(rsock
);
840 * Connect to the relayd using URI and send the socket to the right consumer.
842 * The consumer socket lock must be held by the caller.
844 * Returns LTTNG_OK on success or an LTTng error code on failure.
846 static enum lttng_error_code
send_consumer_relayd_socket(
847 unsigned int session_id
,
848 struct lttng_uri
*relayd_uri
,
849 struct consumer_output
*consumer
,
850 struct consumer_socket
*consumer_sock
,
851 const char *session_name
, const char *hostname
,
852 const char *base_path
, int session_live_timer
,
853 const uint64_t *current_chunk_id
,
854 time_t session_creation_time
,
855 bool session_name_contains_creation_time
)
858 struct lttcomm_relayd_sock
*rsock
= NULL
;
859 enum lttng_error_code status
;
861 /* Connect to relayd and make version check if uri is the control. */
862 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
863 if (status
!= LTTNG_OK
) {
864 goto relayd_comm_error
;
868 /* Set the network sequence index if not set. */
869 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
870 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
872 * Increment net_seq_idx because we are about to transfer the
873 * new relayd socket to the consumer.
874 * Assign unique key so the consumer can match streams.
876 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
877 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
880 /* Send relayd socket to consumer. */
881 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
882 relayd_uri
->stype
, session_id
,
883 session_name
, hostname
, base_path
,
884 session_live_timer
, current_chunk_id
,
885 session_creation_time
, session_name_contains_creation_time
);
887 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
891 /* Flag that the corresponding socket was sent. */
892 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
893 consumer_sock
->control_sock_sent
= 1;
894 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
895 consumer_sock
->data_sock_sent
= 1;
899 * Close socket which was dup on the consumer side. The session daemon does
900 * NOT keep track of the relayd socket(s) once transfer to the consumer.
904 if (status
!= LTTNG_OK
) {
906 * The consumer output for this session should not be used anymore
907 * since the relayd connection failed thus making any tracing or/and
908 * streaming not usable.
910 consumer
->enabled
= 0;
912 (void) relayd_close(rsock
);
920 * Send both relayd sockets to a specific consumer and domain. This is a
921 * helper function to facilitate sending the information to the consumer for a
924 * The consumer socket lock must be held by the caller.
926 * Returns LTTNG_OK, or an LTTng error code on failure.
928 static enum lttng_error_code
send_consumer_relayd_sockets(
929 unsigned int session_id
, struct consumer_output
*consumer
,
930 struct consumer_socket
*sock
, const char *session_name
,
931 const char *hostname
, const char *base_path
, int session_live_timer
,
932 const uint64_t *current_chunk_id
, time_t session_creation_time
,
933 bool session_name_contains_creation_time
)
935 enum lttng_error_code status
= LTTNG_OK
;
937 LTTNG_ASSERT(consumer
);
940 /* Sending control relayd socket. */
941 if (!sock
->control_sock_sent
) {
942 status
= send_consumer_relayd_socket(session_id
,
943 &consumer
->dst
.net
.control
, consumer
, sock
,
944 session_name
, hostname
, base_path
, session_live_timer
,
945 current_chunk_id
, session_creation_time
,
946 session_name_contains_creation_time
);
947 if (status
!= LTTNG_OK
) {
952 /* Sending data relayd socket. */
953 if (!sock
->data_sock_sent
) {
954 status
= send_consumer_relayd_socket(session_id
,
955 &consumer
->dst
.net
.data
, consumer
, sock
,
956 session_name
, hostname
, base_path
, session_live_timer
,
957 current_chunk_id
, session_creation_time
,
958 session_name_contains_creation_time
);
959 if (status
!= LTTNG_OK
) {
969 * Setup relayd connections for a tracing session. First creates the socket to
970 * the relayd and send them to the right domain consumer. Consumer type MUST be
973 int cmd_setup_relayd(struct ltt_session
*session
)
976 struct ltt_ust_session
*usess
;
977 struct ltt_kernel_session
*ksess
;
978 struct consumer_socket
*socket
;
979 struct lttng_ht_iter iter
;
980 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
982 LTTNG_ASSERT(session
);
984 usess
= session
->ust_session
;
985 ksess
= session
->kernel_session
;
987 DBG("Setting relayd for session %s", session
->name
);
990 if (session
->current_trace_chunk
) {
991 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
992 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
994 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
995 current_chunk_id
.is_set
= true;
997 ERR("Failed to get current trace chunk id");
1003 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1004 && usess
->consumer
->enabled
) {
1005 /* For each consumer socket, send relayd sockets */
1006 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1007 socket
, node
.node
) {
1008 pthread_mutex_lock(socket
->lock
);
1009 ret
= send_consumer_relayd_sockets(session
->id
,
1010 usess
->consumer
, socket
,
1011 session
->name
, session
->hostname
,
1013 session
->live_timer
,
1014 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1015 session
->creation_time
,
1016 session
->name_contains_creation_time
);
1017 pthread_mutex_unlock(socket
->lock
);
1018 if (ret
!= LTTNG_OK
) {
1021 /* Session is now ready for network streaming. */
1022 session
->net_handle
= 1;
1024 session
->consumer
->relay_major_version
=
1025 usess
->consumer
->relay_major_version
;
1026 session
->consumer
->relay_minor_version
=
1027 usess
->consumer
->relay_minor_version
;
1028 session
->consumer
->relay_allows_clear
=
1029 usess
->consumer
->relay_allows_clear
;
1032 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1033 && ksess
->consumer
->enabled
) {
1034 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1035 socket
, node
.node
) {
1036 pthread_mutex_lock(socket
->lock
);
1037 ret
= send_consumer_relayd_sockets(session
->id
,
1038 ksess
->consumer
, socket
,
1039 session
->name
, session
->hostname
,
1041 session
->live_timer
,
1042 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1043 session
->creation_time
,
1044 session
->name_contains_creation_time
);
1045 pthread_mutex_unlock(socket
->lock
);
1046 if (ret
!= LTTNG_OK
) {
1049 /* Session is now ready for network streaming. */
1050 session
->net_handle
= 1;
1052 session
->consumer
->relay_major_version
=
1053 ksess
->consumer
->relay_major_version
;
1054 session
->consumer
->relay_minor_version
=
1055 ksess
->consumer
->relay_minor_version
;
1056 session
->consumer
->relay_allows_clear
=
1057 ksess
->consumer
->relay_allows_clear
;
1066 * Start a kernel session by opening all necessary streams.
1068 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1071 struct ltt_kernel_channel
*kchan
;
1073 /* Open kernel metadata */
1074 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1075 ret
= kernel_open_metadata(ksess
);
1077 ret
= LTTNG_ERR_KERN_META_FAIL
;
1082 /* Open kernel metadata stream */
1083 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1084 ret
= kernel_open_metadata_stream(ksess
);
1086 ERR("Kernel create metadata stream failed");
1087 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1092 /* For each channel */
1093 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1094 if (kchan
->stream_count
== 0) {
1095 ret
= kernel_open_channel_stream(kchan
);
1097 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1100 /* Update the stream global counter */
1101 ksess
->stream_count_global
+= ret
;
1105 /* Setup kernel consumer socket and send fds to it */
1106 ret
= init_kernel_tracing(ksess
);
1108 ret
= LTTNG_ERR_KERN_START_FAIL
;
1112 /* This start the kernel tracing */
1113 ret
= kernel_start_session(ksess
);
1115 ret
= LTTNG_ERR_KERN_START_FAIL
;
1119 /* Quiescent wait after starting trace */
1120 kernel_wait_quiescent();
1130 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1132 struct ltt_kernel_channel
*kchan
;
1133 bool error_occurred
= false;
1136 if (!ksess
|| !ksess
->active
) {
1139 DBG("Stopping kernel tracing");
1141 ret
= kernel_stop_session(ksess
);
1143 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1147 kernel_wait_quiescent();
1149 /* Flush metadata after stopping (if exists) */
1150 if (ksess
->metadata_stream_fd
>= 0) {
1151 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1153 ERR("Kernel metadata flush failed");
1154 error_occurred
= true;
1158 /* Flush all buffers after stopping */
1159 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1160 ret
= kernel_flush_buffer(kchan
);
1162 ERR("Kernel flush buffer error");
1163 error_occurred
= true;
1168 if (error_occurred
) {
1169 ret
= LTTNG_ERR_UNK
;
1178 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1180 int cmd_disable_channel(struct ltt_session
*session
,
1181 enum lttng_domain_type domain
, char *channel_name
)
1184 struct ltt_ust_session
*usess
;
1186 usess
= session
->ust_session
;
1191 case LTTNG_DOMAIN_KERNEL
:
1193 ret
= channel_kernel_disable(session
->kernel_session
,
1195 if (ret
!= LTTNG_OK
) {
1199 kernel_wait_quiescent();
1202 case LTTNG_DOMAIN_UST
:
1204 struct ltt_ust_channel
*uchan
;
1205 struct lttng_ht
*chan_ht
;
1207 chan_ht
= usess
->domain_global
.channels
;
1209 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1210 if (uchan
== NULL
) {
1211 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1215 ret
= channel_ust_disable(usess
, uchan
);
1216 if (ret
!= LTTNG_OK
) {
1222 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1234 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1236 * The wpipe arguments is used as a notifier for the kernel thread.
1238 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1242 ssize_t sock_recv_len
;
1243 struct lttng_channel
*channel
= NULL
;
1244 struct lttng_buffer_view view
;
1245 struct lttng_dynamic_buffer channel_buffer
;
1246 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1248 lttng_dynamic_buffer_init(&channel_buffer
);
1249 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1250 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1252 ret
= LTTNG_ERR_NOMEM
;
1256 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1258 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1259 ERR("Failed to receive \"enable channel\" command payload");
1260 ret
= LTTNG_ERR_INVALID
;
1264 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1265 if (!lttng_buffer_view_is_valid(&view
)) {
1266 ret
= LTTNG_ERR_INVALID
;
1270 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1271 ERR("Invalid channel payload received in \"enable channel\" command");
1272 ret
= LTTNG_ERR_INVALID
;
1276 ret
= cmd_enable_channel_internal(
1277 cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1280 lttng_dynamic_buffer_reset(&channel_buffer
);
1281 lttng_channel_destroy(channel
);
1285 static enum lttng_error_code
cmd_enable_channel_internal(
1286 struct ltt_session
*session
,
1287 const struct lttng_domain
*domain
,
1288 const struct lttng_channel
*_attr
,
1291 enum lttng_error_code ret_code
;
1292 struct ltt_ust_session
*usess
= session
->ust_session
;
1293 struct lttng_ht
*chan_ht
;
1295 struct lttng_channel
*attr
= NULL
;
1297 LTTNG_ASSERT(session
);
1298 LTTNG_ASSERT(_attr
);
1299 LTTNG_ASSERT(domain
);
1301 attr
= lttng_channel_copy(_attr
);
1303 ret_code
= LTTNG_ERR_NOMEM
;
1307 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1309 /* Validate channel name */
1310 if (attr
->name
[0] == '.' ||
1311 memchr(attr
->name
, '/', len
) != NULL
) {
1312 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1316 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1321 * If the session is a live session, remove the switch timer, the
1322 * live timer does the same thing but sends also synchronisation
1323 * beacons for inactive streams.
1325 if (session
->live_timer
> 0) {
1326 attr
->attr
.live_timer_interval
= session
->live_timer
;
1327 attr
->attr
.switch_timer_interval
= 0;
1330 /* Check for feature support */
1331 switch (domain
->type
) {
1332 case LTTNG_DOMAIN_KERNEL
:
1334 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1335 /* Sampling position of buffer is not supported */
1336 WARN("Kernel tracer does not support buffer monitoring. "
1337 "Setting the monitor interval timer to 0 "
1338 "(disabled) for channel '%s' of session '%s'",
1339 attr
->name
, session
->name
);
1340 lttng_channel_set_monitor_timer_interval(attr
, 0);
1344 case LTTNG_DOMAIN_UST
:
1346 case LTTNG_DOMAIN_JUL
:
1347 case LTTNG_DOMAIN_LOG4J
:
1348 case LTTNG_DOMAIN_PYTHON
:
1349 if (!agent_tracing_is_enabled()) {
1350 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1351 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1356 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1360 switch (domain
->type
) {
1361 case LTTNG_DOMAIN_KERNEL
:
1363 struct ltt_kernel_channel
*kchan
;
1365 kchan
= trace_kernel_get_channel_by_name(
1366 attr
->name
, session
->kernel_session
);
1367 if (kchan
== NULL
) {
1369 * Don't try to create a channel if the session has been started at
1370 * some point in time before. The tracer does not allow it.
1372 if (session
->has_been_started
) {
1373 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1377 if (session
->snapshot
.nb_output
> 0 ||
1378 session
->snapshot_mode
) {
1379 /* Enforce mmap output for snapshot sessions. */
1380 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1382 ret_code
= channel_kernel_create(
1383 session
->kernel_session
, attr
, wpipe
);
1384 if (attr
->name
[0] != '\0') {
1385 session
->kernel_session
->has_non_default_channel
= 1;
1388 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1391 if (ret_code
!= LTTNG_OK
) {
1395 kernel_wait_quiescent();
1398 case LTTNG_DOMAIN_UST
:
1399 case LTTNG_DOMAIN_JUL
:
1400 case LTTNG_DOMAIN_LOG4J
:
1401 case LTTNG_DOMAIN_PYTHON
:
1403 struct ltt_ust_channel
*uchan
;
1408 * Current agent implementation limitations force us to allow
1409 * only one channel at once in "agent" subdomains. Each
1410 * subdomain has a default channel name which must be strictly
1413 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1414 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1415 LTTNG_SYMBOL_NAME_LEN
)) {
1416 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1419 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1420 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1421 LTTNG_SYMBOL_NAME_LEN
)) {
1422 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1425 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1426 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1427 LTTNG_SYMBOL_NAME_LEN
)) {
1428 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1433 chan_ht
= usess
->domain_global
.channels
;
1435 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1436 if (uchan
== NULL
) {
1438 * Don't try to create a channel if the session has been started at
1439 * some point in time before. The tracer does not allow it.
1441 if (session
->has_been_started
) {
1442 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1446 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1447 if (attr
->name
[0] != '\0') {
1448 usess
->has_non_default_channel
= 1;
1451 ret_code
= channel_ust_enable(usess
, uchan
);
1456 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1460 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1461 session
->has_non_mmap_channel
= true;
1466 lttng_channel_destroy(attr
);
1470 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1471 struct ltt_session
*session
,
1472 enum lttng_domain_type domain
,
1473 enum lttng_process_attr process_attr
,
1474 enum lttng_tracking_policy
*policy
)
1476 enum lttng_error_code ret_code
= LTTNG_OK
;
1477 const struct process_attr_tracker
*tracker
;
1480 case LTTNG_DOMAIN_KERNEL
:
1481 if (!session
->kernel_session
) {
1482 ret_code
= LTTNG_ERR_INVALID
;
1485 tracker
= kernel_get_process_attr_tracker(
1486 session
->kernel_session
, process_attr
);
1488 case LTTNG_DOMAIN_UST
:
1489 if (!session
->ust_session
) {
1490 ret_code
= LTTNG_ERR_INVALID
;
1493 tracker
= trace_ust_get_process_attr_tracker(
1494 session
->ust_session
, process_attr
);
1497 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1501 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1503 ret_code
= LTTNG_ERR_INVALID
;
1509 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1510 struct ltt_session
*session
,
1511 enum lttng_domain_type domain
,
1512 enum lttng_process_attr process_attr
,
1513 enum lttng_tracking_policy policy
)
1515 enum lttng_error_code ret_code
= LTTNG_OK
;
1518 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1519 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1520 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1523 ret_code
= LTTNG_ERR_INVALID
;
1528 case LTTNG_DOMAIN_KERNEL
:
1529 if (!session
->kernel_session
) {
1530 ret_code
= LTTNG_ERR_INVALID
;
1533 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1534 session
->kernel_session
, process_attr
, policy
);
1536 case LTTNG_DOMAIN_UST
:
1537 if (!session
->ust_session
) {
1538 ret_code
= LTTNG_ERR_INVALID
;
1541 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1542 session
->ust_session
, process_attr
, policy
);
1545 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1552 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1553 struct ltt_session
*session
,
1554 enum lttng_domain_type domain
,
1555 enum lttng_process_attr process_attr
,
1556 const struct process_attr_value
*value
)
1558 enum lttng_error_code ret_code
= LTTNG_OK
;
1561 case LTTNG_DOMAIN_KERNEL
:
1562 if (!session
->kernel_session
) {
1563 ret_code
= LTTNG_ERR_INVALID
;
1566 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1567 session
->kernel_session
, process_attr
, value
);
1569 case LTTNG_DOMAIN_UST
:
1570 if (!session
->ust_session
) {
1571 ret_code
= LTTNG_ERR_INVALID
;
1574 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1575 session
->ust_session
, process_attr
, value
);
1578 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1585 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1586 struct ltt_session
*session
,
1587 enum lttng_domain_type domain
,
1588 enum lttng_process_attr process_attr
,
1589 const struct process_attr_value
*value
)
1591 enum lttng_error_code ret_code
= LTTNG_OK
;
1594 case LTTNG_DOMAIN_KERNEL
:
1595 if (!session
->kernel_session
) {
1596 ret_code
= LTTNG_ERR_INVALID
;
1599 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1600 session
->kernel_session
, process_attr
, value
);
1602 case LTTNG_DOMAIN_UST
:
1603 if (!session
->ust_session
) {
1604 ret_code
= LTTNG_ERR_INVALID
;
1607 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1608 session
->ust_session
, process_attr
, value
);
1611 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1618 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1619 struct ltt_session
*session
,
1620 enum lttng_domain_type domain
,
1621 enum lttng_process_attr process_attr
,
1622 struct lttng_process_attr_values
**values
)
1624 enum lttng_error_code ret_code
= LTTNG_OK
;
1625 const struct process_attr_tracker
*tracker
;
1626 enum process_attr_tracker_status status
;
1629 case LTTNG_DOMAIN_KERNEL
:
1630 if (!session
->kernel_session
) {
1631 ret_code
= LTTNG_ERR_INVALID
;
1634 tracker
= kernel_get_process_attr_tracker(
1635 session
->kernel_session
, process_attr
);
1637 case LTTNG_DOMAIN_UST
:
1638 if (!session
->ust_session
) {
1639 ret_code
= LTTNG_ERR_INVALID
;
1642 tracker
= trace_ust_get_process_attr_tracker(
1643 session
->ust_session
, process_attr
);
1646 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1651 ret_code
= LTTNG_ERR_INVALID
;
1655 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1657 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1658 ret_code
= LTTNG_OK
;
1660 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1661 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1663 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1664 ret_code
= LTTNG_ERR_NOMEM
;
1667 ret_code
= LTTNG_ERR_UNK
;
1676 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1678 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1679 struct lttng_event
*event
,
1680 char *filter_expression
,
1681 struct lttng_bytecode
*bytecode
,
1682 struct lttng_event_exclusion
*exclusion
)
1685 const char *event_name
;
1686 const struct ltt_session
*session
= cmd_ctx
->session
;
1687 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1688 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1690 DBG("Disable event command for event \'%s\'", event
->name
);
1693 * Filter and exclusions are simply not handled by the
1694 * disable event command at this time.
1698 (void) filter_expression
;
1701 /* Ignore the presence of filter or exclusion for the event */
1703 event
->exclusion
= 0;
1705 event_name
= event
->name
;
1707 /* Error out on unhandled search criteria */
1708 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1709 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1710 ret
= LTTNG_ERR_UNK
;
1717 case LTTNG_DOMAIN_KERNEL
:
1719 struct ltt_kernel_channel
*kchan
;
1720 struct ltt_kernel_session
*ksess
;
1722 ksess
= session
->kernel_session
;
1725 * If a non-default channel has been created in the
1726 * session, explicitely require that -c chan_name needs
1729 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1730 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1734 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1735 if (kchan
== NULL
) {
1736 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1740 switch (event
->type
) {
1741 case LTTNG_EVENT_ALL
:
1742 case LTTNG_EVENT_TRACEPOINT
:
1743 case LTTNG_EVENT_SYSCALL
:
1744 case LTTNG_EVENT_PROBE
:
1745 case LTTNG_EVENT_FUNCTION
:
1746 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1747 if (event_name
[0] == '\0') {
1748 ret
= event_kernel_disable_event(kchan
,
1751 ret
= event_kernel_disable_event(kchan
,
1752 event_name
, event
->type
);
1754 if (ret
!= LTTNG_OK
) {
1759 ret
= LTTNG_ERR_UNK
;
1763 kernel_wait_quiescent();
1766 case LTTNG_DOMAIN_UST
:
1768 struct ltt_ust_channel
*uchan
;
1769 struct ltt_ust_session
*usess
;
1771 usess
= session
->ust_session
;
1773 if (validate_ust_event_name(event_name
)) {
1774 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1779 * If a non-default channel has been created in the
1780 * session, explicitly require that -c chan_name needs
1783 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1784 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1788 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1790 if (uchan
== NULL
) {
1791 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1795 switch (event
->type
) {
1796 case LTTNG_EVENT_ALL
:
1798 * An empty event name means that everything
1799 * should be disabled.
1801 if (event
->name
[0] == '\0') {
1802 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1804 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1807 if (ret
!= LTTNG_OK
) {
1812 ret
= LTTNG_ERR_UNK
;
1816 DBG3("Disable UST event %s in channel %s completed", event_name
,
1820 case LTTNG_DOMAIN_LOG4J
:
1821 case LTTNG_DOMAIN_JUL
:
1822 case LTTNG_DOMAIN_PYTHON
:
1825 struct ltt_ust_session
*usess
= session
->ust_session
;
1827 LTTNG_ASSERT(usess
);
1829 switch (event
->type
) {
1830 case LTTNG_EVENT_ALL
:
1833 ret
= LTTNG_ERR_UNK
;
1837 agt
= trace_ust_find_agent(usess
, domain
);
1839 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1843 * An empty event name means that everything
1844 * should be disabled.
1846 if (event
->name
[0] == '\0') {
1847 ret
= event_agent_disable_all(usess
, agt
);
1849 ret
= event_agent_disable(usess
, agt
, event_name
);
1851 if (ret
!= LTTNG_OK
) {
1858 ret
= LTTNG_ERR_UND
;
1869 free(filter_expression
);
1874 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1876 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1877 const struct lttng_event_context
*event_context
, int kwpipe
)
1879 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1880 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1881 const struct ltt_session
*session
= cmd_ctx
->session
;
1882 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1885 * Don't try to add a context if the session has been started at
1886 * some point in time before. The tracer does not allow it and would
1887 * result in a corrupted trace.
1889 if (cmd_ctx
->session
->has_been_started
) {
1890 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1895 case LTTNG_DOMAIN_KERNEL
:
1896 LTTNG_ASSERT(session
->kernel_session
);
1898 if (session
->kernel_session
->channel_count
== 0) {
1899 /* Create default channel */
1900 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1901 if (ret
!= LTTNG_OK
) {
1904 chan_kern_created
= 1;
1906 /* Add kernel context to kernel tracer */
1907 ret
= context_kernel_add(session
->kernel_session
,
1908 event_context
, channel_name
);
1909 if (ret
!= LTTNG_OK
) {
1913 case LTTNG_DOMAIN_JUL
:
1914 case LTTNG_DOMAIN_LOG4J
:
1917 * Validate channel name.
1918 * If no channel name is given and the domain is JUL or LOG4J,
1919 * set it to the appropriate domain-specific channel name. If
1920 * a name is provided but does not match the expexted channel
1921 * name, return an error.
1923 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1924 strcmp(channel_name
,
1925 DEFAULT_JUL_CHANNEL_NAME
)) {
1926 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1928 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1929 strcmp(channel_name
,
1930 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1931 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1936 case LTTNG_DOMAIN_UST
:
1938 struct ltt_ust_session
*usess
= session
->ust_session
;
1939 unsigned int chan_count
;
1941 LTTNG_ASSERT(usess
);
1943 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1944 if (chan_count
== 0) {
1945 struct lttng_channel
*attr
;
1946 /* Create default channel */
1947 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1949 ret
= LTTNG_ERR_FATAL
;
1953 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1954 if (ret
!= LTTNG_OK
) {
1958 channel_attr_destroy(attr
);
1959 chan_ust_created
= 1;
1962 ret
= context_ust_add(usess
, domain
, event_context
,
1964 if (ret
!= LTTNG_OK
) {
1970 ret
= LTTNG_ERR_UND
;
1978 if (chan_kern_created
) {
1979 struct ltt_kernel_channel
*kchan
=
1980 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1981 session
->kernel_session
);
1982 /* Created previously, this should NOT fail. */
1983 LTTNG_ASSERT(kchan
);
1984 kernel_destroy_channel(kchan
);
1987 if (chan_ust_created
) {
1988 struct ltt_ust_channel
*uchan
=
1989 trace_ust_find_channel_by_name(
1990 session
->ust_session
->domain_global
.channels
,
1991 DEFAULT_CHANNEL_NAME
);
1992 /* Created previously, this should NOT fail. */
1993 LTTNG_ASSERT(uchan
);
1994 /* Remove from the channel list of the session. */
1995 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1997 trace_ust_destroy_channel(uchan
);
2003 static inline bool name_starts_with(const char *name
, const char *prefix
)
2005 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2007 return !strncmp(name
, prefix
, max_cmp_len
);
2010 /* Perform userspace-specific event name validation */
2011 static int validate_ust_event_name(const char *name
)
2021 * Check name against all internal UST event component namespaces used
2024 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2025 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2026 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2035 * Internal version of cmd_enable_event() with a supplemental
2036 * "internal_event" flag which is used to enable internal events which should
2037 * be hidden from clients. Such events are used in the agent implementation to
2038 * enable the events through which all "agent" events are funeled.
2040 static int _cmd_enable_event(struct ltt_session
*session
,
2041 const struct lttng_domain
*domain
,
2042 char *channel_name
, struct lttng_event
*event
,
2043 char *filter_expression
,
2044 struct lttng_bytecode
*filter
,
2045 struct lttng_event_exclusion
*exclusion
,
2046 int wpipe
, bool internal_event
)
2048 int ret
= 0, channel_created
= 0;
2049 struct lttng_channel
*attr
= NULL
;
2051 LTTNG_ASSERT(session
);
2052 LTTNG_ASSERT(event
);
2053 LTTNG_ASSERT(channel_name
);
2055 /* If we have a filter, we must have its filter expression */
2056 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2058 /* Normalize event name as a globbing pattern */
2059 strutils_normalize_star_glob_pattern(event
->name
);
2061 /* Normalize exclusion names as globbing patterns */
2065 for (i
= 0; i
< exclusion
->count
; i
++) {
2066 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2068 strutils_normalize_star_glob_pattern(name
);
2072 DBG("Enable event command for event \'%s\'", event
->name
);
2076 switch (domain
->type
) {
2077 case LTTNG_DOMAIN_KERNEL
:
2079 struct ltt_kernel_channel
*kchan
;
2082 * If a non-default channel has been created in the
2083 * session, explicitely require that -c chan_name needs
2086 if (session
->kernel_session
->has_non_default_channel
2087 && channel_name
[0] == '\0') {
2088 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2092 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2093 session
->kernel_session
);
2094 if (kchan
== NULL
) {
2095 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2096 LTTNG_BUFFER_GLOBAL
);
2098 ret
= LTTNG_ERR_FATAL
;
2101 if (lttng_strncpy(attr
->name
, channel_name
,
2102 sizeof(attr
->name
))) {
2103 ret
= LTTNG_ERR_INVALID
;
2107 ret
= cmd_enable_channel_internal(
2108 session
, domain
, attr
, wpipe
);
2109 if (ret
!= LTTNG_OK
) {
2112 channel_created
= 1;
2115 /* Get the newly created kernel channel pointer */
2116 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2117 session
->kernel_session
);
2118 if (kchan
== NULL
) {
2119 /* This sould not happen... */
2120 ret
= LTTNG_ERR_FATAL
;
2124 switch (event
->type
) {
2125 case LTTNG_EVENT_ALL
:
2127 char *filter_expression_a
= NULL
;
2128 struct lttng_bytecode
*filter_a
= NULL
;
2131 * We need to duplicate filter_expression and filter,
2132 * because ownership is passed to first enable
2135 if (filter_expression
) {
2136 filter_expression_a
= strdup(filter_expression
);
2137 if (!filter_expression_a
) {
2138 ret
= LTTNG_ERR_FATAL
;
2143 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2145 free(filter_expression_a
);
2146 ret
= LTTNG_ERR_FATAL
;
2149 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2151 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2152 ret
= event_kernel_enable_event(kchan
, event
,
2153 filter_expression
, filter
);
2154 /* We have passed ownership */
2155 filter_expression
= NULL
;
2157 if (ret
!= LTTNG_OK
) {
2158 if (channel_created
) {
2159 /* Let's not leak a useless channel. */
2160 kernel_destroy_channel(kchan
);
2162 free(filter_expression_a
);
2166 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2167 ret
= event_kernel_enable_event(kchan
, event
,
2168 filter_expression_a
, filter_a
);
2169 /* We have passed ownership */
2170 filter_expression_a
= NULL
;
2172 if (ret
!= LTTNG_OK
) {
2177 case LTTNG_EVENT_PROBE
:
2178 case LTTNG_EVENT_USERSPACE_PROBE
:
2179 case LTTNG_EVENT_FUNCTION
:
2180 case LTTNG_EVENT_FUNCTION_ENTRY
:
2181 case LTTNG_EVENT_TRACEPOINT
:
2182 ret
= event_kernel_enable_event(kchan
, event
,
2183 filter_expression
, filter
);
2184 /* We have passed ownership */
2185 filter_expression
= NULL
;
2187 if (ret
!= LTTNG_OK
) {
2188 if (channel_created
) {
2189 /* Let's not leak a useless channel. */
2190 kernel_destroy_channel(kchan
);
2195 case LTTNG_EVENT_SYSCALL
:
2196 ret
= event_kernel_enable_event(kchan
, event
,
2197 filter_expression
, filter
);
2198 /* We have passed ownership */
2199 filter_expression
= NULL
;
2201 if (ret
!= LTTNG_OK
) {
2206 ret
= LTTNG_ERR_UNK
;
2210 kernel_wait_quiescent();
2213 case LTTNG_DOMAIN_UST
:
2215 struct ltt_ust_channel
*uchan
;
2216 struct ltt_ust_session
*usess
= session
->ust_session
;
2218 LTTNG_ASSERT(usess
);
2221 * If a non-default channel has been created in the
2222 * session, explicitely require that -c chan_name needs
2225 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2226 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2230 /* Get channel from global UST domain */
2231 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2233 if (uchan
== NULL
) {
2234 /* Create default channel */
2235 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2236 usess
->buffer_type
);
2238 ret
= LTTNG_ERR_FATAL
;
2241 if (lttng_strncpy(attr
->name
, channel_name
,
2242 sizeof(attr
->name
))) {
2243 ret
= LTTNG_ERR_INVALID
;
2247 ret
= cmd_enable_channel_internal(
2248 session
, domain
, attr
, wpipe
);
2249 if (ret
!= LTTNG_OK
) {
2253 /* Get the newly created channel reference back */
2254 uchan
= trace_ust_find_channel_by_name(
2255 usess
->domain_global
.channels
, channel_name
);
2256 LTTNG_ASSERT(uchan
);
2259 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2261 * Don't allow users to add UST events to channels which
2262 * are assigned to a userspace subdomain (JUL, Log4J,
2265 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2269 if (!internal_event
) {
2271 * Ensure the event name is not reserved for internal
2274 ret
= validate_ust_event_name(event
->name
);
2276 WARN("Userspace event name %s failed validation.",
2278 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2283 /* At this point, the session and channel exist on the tracer */
2284 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2285 filter_expression
, filter
, exclusion
,
2287 /* We have passed ownership */
2288 filter_expression
= NULL
;
2291 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2292 goto already_enabled
;
2293 } else if (ret
!= LTTNG_OK
) {
2298 case LTTNG_DOMAIN_LOG4J
:
2299 case LTTNG_DOMAIN_JUL
:
2300 case LTTNG_DOMAIN_PYTHON
:
2302 const char *default_event_name
, *default_chan_name
;
2304 struct lttng_event uevent
;
2305 struct lttng_domain tmp_dom
;
2306 struct ltt_ust_session
*usess
= session
->ust_session
;
2308 LTTNG_ASSERT(usess
);
2310 if (!agent_tracing_is_enabled()) {
2311 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2312 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2316 agt
= trace_ust_find_agent(usess
, domain
->type
);
2318 agt
= agent_create(domain
->type
);
2320 ret
= LTTNG_ERR_NOMEM
;
2323 agent_add(agt
, usess
->agents
);
2326 /* Create the default tracepoint. */
2327 memset(&uevent
, 0, sizeof(uevent
));
2328 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2329 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2330 default_event_name
= event_get_default_agent_ust_name(
2332 if (!default_event_name
) {
2333 ret
= LTTNG_ERR_FATAL
;
2336 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2337 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2340 * The domain type is changed because we are about to enable the
2341 * default channel and event for the JUL domain that are hardcoded.
2342 * This happens in the UST domain.
2344 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2345 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2347 switch (domain
->type
) {
2348 case LTTNG_DOMAIN_LOG4J
:
2349 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2351 case LTTNG_DOMAIN_JUL
:
2352 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2354 case LTTNG_DOMAIN_PYTHON
:
2355 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2358 /* The switch/case we are in makes this impossible */
2363 char *filter_expression_copy
= NULL
;
2364 struct lttng_bytecode
*filter_copy
= NULL
;
2367 const size_t filter_size
= sizeof(
2368 struct lttng_bytecode
)
2371 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2373 ret
= LTTNG_ERR_NOMEM
;
2376 memcpy(filter_copy
, filter
, filter_size
);
2378 filter_expression_copy
=
2379 strdup(filter_expression
);
2380 if (!filter_expression
) {
2381 ret
= LTTNG_ERR_NOMEM
;
2384 if (!filter_expression_copy
|| !filter_copy
) {
2385 free(filter_expression_copy
);
2391 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2392 (char *) default_chan_name
,
2393 &uevent
, filter_expression_copy
,
2394 filter_copy
, NULL
, wpipe
);
2397 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2398 goto already_enabled
;
2399 } else if (ret
!= LTTNG_OK
) {
2403 /* The wild card * means that everything should be enabled. */
2404 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2405 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2408 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2412 filter_expression
= NULL
;
2413 if (ret
!= LTTNG_OK
) {
2420 ret
= LTTNG_ERR_UND
;
2428 free(filter_expression
);
2431 channel_attr_destroy(attr
);
2437 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2438 * We own filter, exclusion, and filter_expression.
2440 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2441 struct lttng_event
*event
,
2442 char *filter_expression
,
2443 struct lttng_event_exclusion
*exclusion
,
2444 struct lttng_bytecode
*bytecode
,
2449 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2451 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2454 * The ownership of the following parameters is transferred to
2455 * _cmd_enable_event:
2457 * - filter_expression,
2461 ret
= _cmd_enable_event(cmd_ctx
->session
,
2463 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2464 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2465 filter_expression
= NULL
;
2472 * Enable an event which is internal to LTTng. An internal should
2473 * never be made visible to clients and are immune to checks such as
2476 static int cmd_enable_event_internal(struct ltt_session
*session
,
2477 const struct lttng_domain
*domain
,
2478 char *channel_name
, struct lttng_event
*event
,
2479 char *filter_expression
,
2480 struct lttng_bytecode
*filter
,
2481 struct lttng_event_exclusion
*exclusion
,
2484 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2485 filter_expression
, filter
, exclusion
, wpipe
, true);
2489 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2491 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2492 struct lttng_payload
*reply_payload
)
2494 enum lttng_error_code ret_code
;
2496 ssize_t i
, nb_events
= 0;
2497 struct lttng_event
*events
= NULL
;
2498 struct lttcomm_list_command_header reply_command_header
= {};
2499 size_t reply_command_header_offset
;
2501 assert(reply_payload
);
2503 /* Reserve space for command reply header. */
2504 reply_command_header_offset
= reply_payload
->buffer
.size
;
2505 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2506 reply_command_header_offset
+
2507 sizeof(struct lttcomm_list_command_header
));
2509 ret_code
= LTTNG_ERR_NOMEM
;
2514 case LTTNG_DOMAIN_KERNEL
:
2515 nb_events
= kernel_list_events(&events
);
2516 if (nb_events
< 0) {
2517 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2521 case LTTNG_DOMAIN_UST
:
2522 nb_events
= ust_app_list_events(&events
);
2523 if (nb_events
< 0) {
2524 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2528 case LTTNG_DOMAIN_LOG4J
:
2529 case LTTNG_DOMAIN_JUL
:
2530 case LTTNG_DOMAIN_PYTHON
:
2531 nb_events
= agent_list_events(&events
, domain
);
2532 if (nb_events
< 0) {
2533 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2538 ret_code
= LTTNG_ERR_UND
;
2542 for (i
= 0; i
< nb_events
; i
++) {
2543 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2546 ret_code
= LTTNG_ERR_NOMEM
;
2551 if (nb_events
> UINT32_MAX
) {
2552 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2553 ret_code
= LTTNG_ERR_OVERFLOW
;
2557 /* Update command reply header. */
2558 reply_command_header
.count
= (uint32_t) nb_events
;
2559 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2560 sizeof(reply_command_header
));
2562 ret_code
= LTTNG_OK
;
2569 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2571 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2572 struct lttng_payload
*reply
)
2574 enum lttng_error_code ret_code
;
2576 unsigned int i
, nb_fields
;
2577 struct lttng_event_field
*fields
= NULL
;
2578 struct lttcomm_list_command_header reply_command_header
= {};
2579 size_t reply_command_header_offset
;
2583 /* Reserve space for command reply header. */
2584 reply_command_header_offset
= reply
->buffer
.size
;
2585 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2586 reply_command_header_offset
+
2587 sizeof(struct lttcomm_list_command_header
));
2589 ret_code
= LTTNG_ERR_NOMEM
;
2594 case LTTNG_DOMAIN_UST
:
2595 ret
= ust_app_list_event_fields(&fields
);
2597 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2602 case LTTNG_DOMAIN_KERNEL
:
2603 default: /* fall-through */
2604 ret_code
= LTTNG_ERR_UND
;
2610 for (i
= 0; i
< nb_fields
; i
++) {
2611 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2613 ret_code
= LTTNG_ERR_NOMEM
;
2618 if (nb_fields
> UINT32_MAX
) {
2619 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2620 ret_code
= LTTNG_ERR_OVERFLOW
;
2624 /* Update command reply header. */
2625 reply_command_header
.count
= (uint32_t) nb_fields
;
2627 memcpy(reply
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2628 sizeof(reply_command_header
));
2630 ret_code
= LTTNG_OK
;
2637 enum lttng_error_code
cmd_list_syscalls(
2638 struct lttng_payload
*reply_payload
)
2640 enum lttng_error_code ret_code
;
2641 ssize_t nb_events
, i
;
2643 struct lttng_event
*events
= NULL
;
2644 struct lttcomm_list_command_header reply_command_header
= {};
2645 size_t reply_command_header_offset
;
2647 assert(reply_payload
);
2649 /* Reserve space for command reply header. */
2650 reply_command_header_offset
= reply_payload
->buffer
.size
;
2651 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2652 reply_command_header_offset
+
2653 sizeof(struct lttcomm_list_command_header
));
2655 ret_code
= LTTNG_ERR_NOMEM
;
2659 nb_events
= syscall_table_list(&events
);
2660 if (nb_events
< 0) {
2661 ret_code
= (enum lttng_error_code
) -nb_events
;
2665 for (i
= 0; i
< nb_events
; i
++) {
2666 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2669 ret_code
= LTTNG_ERR_NOMEM
;
2674 if (nb_events
> UINT32_MAX
) {
2675 ERR("Syscall count would overflow the syscall listing command's reply");
2676 ret_code
= LTTNG_ERR_OVERFLOW
;
2680 /* Update command reply header. */
2681 reply_command_header
.count
= (uint32_t) nb_events
;
2682 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2683 sizeof(reply_command_header
));
2685 ret_code
= LTTNG_OK
;
2692 * Command LTTNG_START_TRACE processed by the client thread.
2694 * Called with session mutex held.
2696 int cmd_start_trace(struct ltt_session
*session
)
2698 enum lttng_error_code ret
;
2699 unsigned long nb_chan
= 0;
2700 struct ltt_kernel_session
*ksession
;
2701 struct ltt_ust_session
*usess
;
2702 const bool session_rotated_after_last_stop
=
2703 session
->rotated_after_last_stop
;
2704 const bool session_cleared_after_last_stop
=
2705 session
->cleared_after_last_stop
;
2707 LTTNG_ASSERT(session
);
2709 /* Ease our life a bit ;) */
2710 ksession
= session
->kernel_session
;
2711 usess
= session
->ust_session
;
2713 /* Is the session already started? */
2714 if (session
->active
) {
2715 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2716 /* Perform nothing */
2720 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2721 !session
->current_trace_chunk
) {
2723 * A rotation was launched while the session was stopped and
2724 * it has not been completed yet. It is not possible to start
2725 * the session since starting the session here would require a
2726 * rotation from "NULL" to a new trace chunk. That rotation
2727 * would overlap with the ongoing rotation, which is not
2730 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2732 ret
= LTTNG_ERR_ROTATION_PENDING
;
2737 * Starting a session without channel is useless since after that it's not
2738 * possible to enable channel thus inform the client.
2740 if (usess
&& usess
->domain_global
.channels
) {
2741 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2744 nb_chan
+= ksession
->channel_count
;
2747 ret
= LTTNG_ERR_NO_CHANNEL
;
2751 session
->active
= 1;
2752 session
->rotated_after_last_stop
= false;
2753 session
->cleared_after_last_stop
= false;
2754 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2755 if (!session
->has_been_started
) {
2756 struct lttng_trace_chunk
*trace_chunk
;
2758 DBG("Creating initial trace chunk of session \"%s\"",
2760 trace_chunk
= session_create_new_trace_chunk(
2761 session
, NULL
, NULL
, NULL
);
2763 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2766 LTTNG_ASSERT(!session
->current_trace_chunk
);
2767 ret
= (lttng_error_code
) session_set_trace_chunk(session
, trace_chunk
,
2769 lttng_trace_chunk_put(trace_chunk
);
2771 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2775 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2778 * Rotate existing streams into the new chunk.
2779 * This is a "quiet" rotation has no client has
2780 * explicitly requested this operation.
2782 * There is also no need to wait for the rotation
2783 * to complete as it will happen immediately. No data
2784 * was produced as the session was stopped, so the
2785 * rotation should happen on reception of the command.
2787 ret
= (lttng_error_code
) cmd_rotate_session(session
, NULL
, true,
2788 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2789 if (ret
!= LTTNG_OK
) {
2795 /* Kernel tracing */
2796 if (ksession
!= NULL
) {
2797 DBG("Start kernel tracing session %s", session
->name
);
2798 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2799 if (ret
!= LTTNG_OK
) {
2804 /* Flag session that trace should start automatically */
2806 int int_ret
= ust_app_start_trace_all(usess
);
2809 ret
= LTTNG_ERR_UST_START_FAIL
;
2815 * Open a packet in every stream of the session to ensure that viewers
2816 * can correctly identify the boundaries of the periods during which
2817 * tracing was active for this session.
2819 ret
= session_open_packets(session
);
2820 if (ret
!= LTTNG_OK
) {
2825 * Clear the flag that indicates that a rotation was done while the
2826 * session was stopped.
2828 session
->rotated_after_last_stop
= false;
2830 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2831 int int_ret
= timer_session_rotation_schedule_timer_start(
2832 session
, session
->rotate_timer_period
);
2835 ERR("Failed to enable rotate timer");
2836 ret
= LTTNG_ERR_UNK
;
2844 if (ret
== LTTNG_OK
) {
2845 /* Flag this after a successful start. */
2846 session
->has_been_started
|= 1;
2848 session
->active
= 0;
2849 /* Restore initial state on error. */
2850 session
->rotated_after_last_stop
=
2851 session_rotated_after_last_stop
;
2852 session
->cleared_after_last_stop
=
2853 session_cleared_after_last_stop
;
2860 * Command LTTNG_STOP_TRACE processed by the client thread.
2862 int cmd_stop_trace(struct ltt_session
*session
)
2865 struct ltt_kernel_session
*ksession
;
2866 struct ltt_ust_session
*usess
;
2868 LTTNG_ASSERT(session
);
2870 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2872 ksession
= session
->kernel_session
;
2873 usess
= session
->ust_session
;
2875 /* Session is not active. Skip everything and inform the client. */
2876 if (!session
->active
) {
2877 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2881 ret
= stop_kernel_session(ksession
);
2882 if (ret
!= LTTNG_OK
) {
2886 if (usess
&& usess
->active
) {
2887 ret
= ust_app_stop_trace_all(usess
);
2889 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2894 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2896 /* Flag inactive after a successful stop. */
2897 session
->active
= 0;
2905 * Set the base_path of the session only if subdir of a control uris is set.
2906 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2908 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2910 struct lttng_uri
*uris
)
2915 for (i
= 0; i
< nb_uri
; i
++) {
2916 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2917 uris
[i
].subdir
[0] == '\0') {
2918 /* Not interested in these URIs */
2922 if (session
->base_path
!= NULL
) {
2923 free(session
->base_path
);
2924 session
->base_path
= NULL
;
2927 /* Set session base_path */
2928 session
->base_path
= strdup(uris
[i
].subdir
);
2929 if (!session
->base_path
) {
2930 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2931 uris
[i
].subdir
, session
->name
);
2932 ret
= LTTNG_ERR_NOMEM
;
2935 DBG2("Setting base path \"%s\" for session \"%s\"",
2936 session
->base_path
, session
->name
);
2944 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2946 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2947 struct lttng_uri
*uris
)
2950 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2951 struct ltt_ust_session
*usess
= session
->ust_session
;
2953 LTTNG_ASSERT(session
);
2955 LTTNG_ASSERT(nb_uri
> 0);
2957 /* Can't set consumer URI if the session is active. */
2958 if (session
->active
) {
2959 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2964 * Set the session base path if any. This is done inside
2965 * cmd_set_consumer_uri to preserve backward compatibility of the
2966 * previous session creation api vs the session descriptor api.
2968 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2969 if (ret
!= LTTNG_OK
) {
2973 /* Set the "global" consumer URIs */
2974 for (i
= 0; i
< nb_uri
; i
++) {
2975 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2977 if (ret
!= LTTNG_OK
) {
2982 /* Set UST session URIs */
2983 if (session
->ust_session
) {
2984 for (i
= 0; i
< nb_uri
; i
++) {
2985 ret
= add_uri_to_consumer(session
,
2986 session
->ust_session
->consumer
,
2987 &uris
[i
], LTTNG_DOMAIN_UST
);
2988 if (ret
!= LTTNG_OK
) {
2994 /* Set kernel session URIs */
2995 if (session
->kernel_session
) {
2996 for (i
= 0; i
< nb_uri
; i
++) {
2997 ret
= add_uri_to_consumer(session
,
2998 session
->kernel_session
->consumer
,
2999 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3000 if (ret
!= LTTNG_OK
) {
3007 * Make sure to set the session in output mode after we set URI since a
3008 * session can be created without URL (thus flagged in no output mode).
3010 session
->output_traces
= 1;
3012 ksess
->output_traces
= 1;
3016 usess
->output_traces
= 1;
3027 enum lttng_error_code
set_session_output_from_descriptor(
3028 struct ltt_session
*session
,
3029 const struct lttng_session_descriptor
*descriptor
)
3032 enum lttng_error_code ret_code
= LTTNG_OK
;
3033 enum lttng_session_descriptor_type session_type
=
3034 lttng_session_descriptor_get_type(descriptor
);
3035 enum lttng_session_descriptor_output_type output_type
=
3036 lttng_session_descriptor_get_output_type(descriptor
);
3037 struct lttng_uri uris
[2] = {};
3038 size_t uri_count
= 0;
3040 switch (output_type
) {
3041 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3043 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3044 lttng_session_descriptor_get_local_output_uri(descriptor
,
3048 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3049 lttng_session_descriptor_get_network_output_uris(descriptor
,
3050 &uris
[0], &uris
[1]);
3054 ret_code
= LTTNG_ERR_INVALID
;
3058 switch (session_type
) {
3059 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3061 struct snapshot_output
*new_output
= NULL
;
3063 new_output
= snapshot_output_alloc();
3065 ret_code
= LTTNG_ERR_NOMEM
;
3069 ret
= snapshot_output_init_with_uri(session
,
3070 DEFAULT_SNAPSHOT_MAX_SIZE
,
3071 NULL
, uris
, uri_count
, session
->consumer
,
3072 new_output
, &session
->snapshot
);
3074 ret_code
= (ret
== -ENOMEM
) ?
3075 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3076 snapshot_output_destroy(new_output
);
3079 snapshot_add_output(&session
->snapshot
, new_output
);
3082 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3083 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3085 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3089 ret_code
= LTTNG_ERR_INVALID
;
3097 enum lttng_error_code
cmd_create_session_from_descriptor(
3098 struct lttng_session_descriptor
*descriptor
,
3099 const lttng_sock_cred
*creds
,
3100 const char *home_path
)
3103 enum lttng_error_code ret_code
;
3104 const char *session_name
;
3105 struct ltt_session
*new_session
= NULL
;
3106 enum lttng_session_descriptor_status descriptor_status
;
3108 session_lock_list();
3110 if (*home_path
!= '/') {
3111 ERR("Home path provided by client is not absolute");
3112 ret_code
= LTTNG_ERR_INVALID
;
3117 descriptor_status
= lttng_session_descriptor_get_session_name(
3118 descriptor
, &session_name
);
3119 switch (descriptor_status
) {
3120 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3122 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3123 session_name
= NULL
;
3126 ret_code
= LTTNG_ERR_INVALID
;
3130 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3132 if (ret_code
!= LTTNG_OK
) {
3136 if (!session_name
) {
3137 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3140 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3145 if (!lttng_session_descriptor_is_output_destination_initialized(
3148 * Only include the session's creation time in the output
3149 * destination if the name of the session itself was
3150 * not auto-generated.
3152 ret_code
= lttng_session_descriptor_set_default_output(
3154 session_name
? &new_session
->creation_time
: NULL
,
3156 if (ret_code
!= LTTNG_OK
) {
3160 new_session
->has_user_specified_directory
=
3161 lttng_session_descriptor_has_output_directory(
3165 switch (lttng_session_descriptor_get_type(descriptor
)) {
3166 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3167 new_session
->snapshot_mode
= 1;
3169 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3170 new_session
->live_timer
=
3171 lttng_session_descriptor_live_get_timer_interval(
3178 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3179 if (ret_code
!= LTTNG_OK
) {
3182 new_session
->consumer
->enabled
= 1;
3183 ret_code
= LTTNG_OK
;
3185 /* Release reference provided by the session_create function. */
3186 session_put(new_session
);
3187 if (ret_code
!= LTTNG_OK
&& new_session
) {
3188 /* Release the global reference on error. */
3189 session_destroy(new_session
);
3191 session_unlock_list();
3195 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3196 struct lttng_session_descriptor
**return_descriptor
)
3199 size_t payload_size
;
3200 struct lttng_dynamic_buffer payload
;
3201 struct lttng_buffer_view home_dir_view
;
3202 struct lttng_buffer_view session_descriptor_view
;
3203 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3204 enum lttng_error_code ret_code
;
3206 lttng_dynamic_buffer_init(&payload
);
3207 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3209 ret_code
= LTTNG_ERR_INVALID
;
3212 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3213 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3214 ret_code
= LTTNG_ERR_INVALID
;
3218 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3219 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3220 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3222 ret_code
= LTTNG_ERR_NOMEM
;
3226 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3228 ERR("Reception of session descriptor failed, aborting.");
3229 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3233 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3236 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3237 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3238 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3239 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3240 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3244 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3246 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3247 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3248 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3249 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3250 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3254 ret
= lttng_session_descriptor_create_from_buffer(
3255 &session_descriptor_view
, &session_descriptor
);
3257 ERR("Failed to create session descriptor from payload of \"create session\" command");
3258 ret_code
= LTTNG_ERR_INVALID
;
3263 * Sets the descriptor's auto-generated properties (name, output) if
3266 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3268 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3269 if (ret_code
!= LTTNG_OK
) {
3273 ret_code
= LTTNG_OK
;
3274 *return_descriptor
= session_descriptor
;
3275 session_descriptor
= NULL
;
3277 lttng_dynamic_buffer_reset(&payload
);
3278 lttng_session_descriptor_destroy(session_descriptor
);
3283 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3284 void *_reply_context
)
3288 const struct cmd_destroy_session_reply_context
*reply_context
=
3289 (cmd_destroy_session_reply_context
*) _reply_context
;
3290 struct lttng_dynamic_buffer payload
;
3291 struct lttcomm_session_destroy_command_header cmd_header
;
3292 struct lttng_trace_archive_location
*location
= NULL
;
3293 struct lttcomm_lttng_msg llm
= {
3294 .cmd_type
= LTTNG_DESTROY_SESSION
,
3295 .ret_code
= reply_context
->destruction_status
,
3298 sizeof(struct lttcomm_session_destroy_command_header
),
3302 size_t payload_size_before_location
;
3304 lttng_dynamic_buffer_init(&payload
);
3306 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3308 ERR("Failed to append session destruction message");
3312 cmd_header
.rotation_state
=
3313 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3314 session
->rotation_state
:
3315 LTTNG_ROTATION_STATE_NO_ROTATION
);
3316 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3317 sizeof(cmd_header
));
3319 ERR("Failed to append session destruction command header");
3323 if (!reply_context
->implicit_rotation_on_destroy
) {
3324 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3328 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3329 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3334 location
= session_get_trace_archive_location(session
);
3336 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3341 payload_size_before_location
= payload
.size
;
3342 comm_ret
= lttng_trace_archive_location_serialize(location
,
3344 lttng_trace_archive_location_put(location
);
3346 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3350 /* Update the message to indicate the location's length. */
3351 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3352 payload
.size
- payload_size_before_location
;
3354 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3355 payload
.data
, payload
.size
);
3356 if (comm_ret
!= (ssize_t
) payload
.size
) {
3357 ERR("Failed to send result of the destruction of session \"%s\" to client",
3361 ret
= close(reply_context
->reply_sock_fd
);
3363 PERROR("Failed to close client socket in deferred session destroy reply");
3365 lttng_dynamic_buffer_reset(&payload
);
3366 free(_reply_context
);
3370 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3372 * Called with session lock held.
3374 int cmd_destroy_session(struct ltt_session
*session
,
3375 struct notification_thread_handle
*notification_thread_handle
,
3379 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3380 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3383 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3384 if (!reply_context
) {
3385 ret
= LTTNG_ERR_NOMEM
;
3389 reply_context
->reply_sock_fd
= *sock_fd
;
3393 LTTNG_ASSERT(session
);
3395 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3397 if (session
->active
) {
3398 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3400 ret
= cmd_stop_trace(session
);
3401 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3402 /* Carry on with the destruction of the session. */
3403 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3404 session
->name
, lttng_strerror(-ret
));
3405 destruction_last_error
= (lttng_error_code
) ret
;
3409 if (session
->rotation_schedule_timer_enabled
) {
3410 if (timer_session_rotation_schedule_timer_stop(
3412 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3414 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3418 if (session
->rotate_size
) {
3419 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3420 session
->rotate_size
= 0;
3423 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3425 * Perform a last rotation on destruction if rotations have
3426 * occurred during the session's lifetime.
3428 ret
= cmd_rotate_session(session
, NULL
, false,
3429 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3430 if (ret
!= LTTNG_OK
) {
3431 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3432 session
->name
, lttng_strerror(-ret
));
3433 destruction_last_error
= (lttng_error_code
) -ret
;
3435 if (reply_context
) {
3436 reply_context
->implicit_rotation_on_destroy
= true;
3438 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3440 * The user has not triggered a session rotation. However, to
3441 * ensure all data has been consumed, the session is rotated
3442 * to a 'null' trace chunk before it is destroyed.
3444 * This is a "quiet" rotation meaning that no notification is
3445 * emitted and no renaming of the current trace chunk takes
3448 ret
= cmd_rotate_session(session
, NULL
, true,
3449 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3451 * Rotation operations may not be supported by the kernel
3452 * tracer. Hence, do not consider this implicit rotation as
3453 * a session destruction error. The library has already stopped
3454 * the session and waited for pending data; there is nothing
3455 * left to do but complete the destruction of the session.
3457 if (ret
!= LTTNG_OK
&&
3458 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3459 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3460 session
->name
, lttng_strerror(ret
));
3461 destruction_last_error
= (lttng_error_code
) -ret
;
3465 if (session
->shm_path
[0]) {
3467 * When a session is created with an explicit shm_path,
3468 * the consumer daemon will create its shared memory files
3469 * at that location and will *not* unlink them. This is normal
3470 * as the intention of that feature is to make it possible
3471 * to retrieve the content of those files should a crash occur.
3473 * To ensure the content of those files can be used, the
3474 * sessiond daemon will replicate the content of the metadata
3475 * cache in a metadata file.
3477 * On clean-up, it is expected that the consumer daemon will
3478 * unlink the shared memory files and that the session daemon
3479 * will unlink the metadata file. Then, the session's directory
3480 * in the shm path can be removed.
3482 * Unfortunately, a flaw in the design of the sessiond's and
3483 * consumerd's tear down of channels makes it impossible to
3484 * determine when the sessiond _and_ the consumerd have both
3485 * destroyed their representation of a channel. For one, the
3486 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3487 * callbacks in both daemons.
3489 * However, it is also impossible for the sessiond to know when
3490 * the consumer daemon is done destroying its channel(s) since
3491 * it occurs as a reaction to the closing of the channel's file
3492 * descriptor. There is no resulting communication initiated
3493 * from the consumerd to the sessiond to confirm that the
3494 * operation is completed (and was successful).
3496 * Until this is all fixed, the session daemon checks for the
3497 * removal of the session's shm path which makes it possible
3498 * to safely advertise a session as having been destroyed.
3500 * Prior to this fix, it was not possible to reliably save
3501 * a session making use of the --shm-path option, destroy it,
3502 * and load it again. This is because the creation of the
3503 * session would fail upon seeing the session's shm path
3504 * already in existence.
3506 * Note that none of the error paths in the check for the
3507 * directory's existence return an error. This is normal
3508 * as there isn't much that can be done. The session will
3509 * be destroyed properly, except that we can't offer the
3510 * guarantee that the same session can be re-created.
3512 current_completion_handler
= &destroy_completion_handler
.handler
;
3513 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3515 sizeof(destroy_completion_handler
.shm_path
));
3520 * The session is destroyed. However, note that the command context
3521 * still holds a reference to the session, thus delaying its destruction
3522 * _at least_ up to the point when that reference is released.
3524 session_destroy(session
);
3525 if (reply_context
) {
3526 reply_context
->destruction_status
= destruction_last_error
;
3527 ret
= session_add_destroy_notifier(session
,
3528 cmd_destroy_session_reply
,
3529 (void *) reply_context
);
3531 ret
= LTTNG_ERR_FATAL
;
3543 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3545 int cmd_register_consumer(struct ltt_session
*session
,
3546 enum lttng_domain_type domain
, const char *sock_path
,
3547 struct consumer_data
*cdata
)
3550 struct consumer_socket
*socket
= NULL
;
3552 LTTNG_ASSERT(session
);
3553 LTTNG_ASSERT(cdata
);
3554 LTTNG_ASSERT(sock_path
);
3557 case LTTNG_DOMAIN_KERNEL
:
3559 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3561 LTTNG_ASSERT(ksess
);
3563 /* Can't register a consumer if there is already one */
3564 if (ksess
->consumer_fds_sent
!= 0) {
3565 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3569 sock
= lttcomm_connect_unix_sock(sock_path
);
3571 ret
= LTTNG_ERR_CONNECT_FAIL
;
3574 cdata
->cmd_sock
= sock
;
3576 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3577 if (socket
== NULL
) {
3580 PERROR("close register consumer");
3582 cdata
->cmd_sock
= -1;
3583 ret
= LTTNG_ERR_FATAL
;
3587 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3588 if (socket
->lock
== NULL
) {
3589 PERROR("zmalloc pthread mutex");
3590 ret
= LTTNG_ERR_FATAL
;
3594 pthread_mutex_init(socket
->lock
, NULL
);
3595 socket
->registered
= 1;
3598 consumer_add_socket(socket
, ksess
->consumer
);
3601 pthread_mutex_lock(&cdata
->pid_mutex
);
3603 pthread_mutex_unlock(&cdata
->pid_mutex
);
3608 /* TODO: Userspace tracing */
3609 ret
= LTTNG_ERR_UND
;
3617 consumer_destroy_socket(socket
);
3623 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3625 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3626 struct lttng_domain
**domains
)
3631 struct lttng_ht_iter iter
;
3633 if (session
->kernel_session
!= NULL
) {
3634 DBG3("Listing domains found kernel domain");
3638 if (session
->ust_session
!= NULL
) {
3639 DBG3("Listing domains found UST global domain");
3643 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3645 if (agt
->being_used
) {
3656 *domains
= calloc
<lttng_domain
>(nb_dom
);
3657 if (*domains
== NULL
) {
3658 ret
= LTTNG_ERR_FATAL
;
3662 if (session
->kernel_session
!= NULL
) {
3663 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3665 /* Kernel session buffer type is always GLOBAL */
3666 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3671 if (session
->ust_session
!= NULL
) {
3672 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3673 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3677 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3679 if (agt
->being_used
) {
3680 (*domains
)[index
].type
= agt
->domain
;
3681 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3691 /* Return negative value to differentiate return code */
3697 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3699 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3700 struct ltt_session
*session
,
3701 struct lttng_payload
*payload
)
3705 struct lttcomm_list_command_header cmd_header
= {};
3706 size_t cmd_header_offset
;
3707 enum lttng_error_code ret_code
;
3712 DBG("Listing channels for session %s", session
->name
);
3714 cmd_header_offset
= payload
->buffer
.size
;
3716 /* Reserve space for command reply header. */
3717 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3718 cmd_header_offset
+ sizeof(cmd_header
));
3720 ret_code
= LTTNG_ERR_NOMEM
;
3725 case LTTNG_DOMAIN_KERNEL
:
3727 /* Kernel channels */
3728 struct ltt_kernel_channel
*kchan
;
3729 if (session
->kernel_session
!= NULL
) {
3730 cds_list_for_each_entry(kchan
,
3731 &session
->kernel_session
->channel_list
.head
, list
) {
3732 uint64_t discarded_events
, lost_packets
;
3733 struct lttng_channel_extended
*extended
;
3735 extended
= (struct lttng_channel_extended
*)
3736 kchan
->channel
->attr
.extended
.ptr
;
3738 ret
= get_kernel_runtime_stats(session
, kchan
,
3739 &discarded_events
, &lost_packets
);
3741 ret_code
= LTTNG_ERR_UNK
;
3746 * Update the discarded_events and lost_packets
3747 * count for the channel
3749 extended
->discarded_events
= discarded_events
;
3750 extended
->lost_packets
= lost_packets
;
3752 ret
= lttng_channel_serialize(
3753 kchan
->channel
, &payload
->buffer
);
3755 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3756 kchan
->channel
->name
);
3757 ret_code
= LTTNG_ERR_UNK
;
3766 case LTTNG_DOMAIN_UST
:
3768 struct lttng_ht_iter iter
;
3769 struct ltt_ust_channel
*uchan
;
3772 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3773 &iter
.iter
, uchan
, node
.node
) {
3774 uint64_t discarded_events
= 0, lost_packets
= 0;
3775 struct lttng_channel
*channel
= NULL
;
3776 struct lttng_channel_extended
*extended
;
3778 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3780 ret_code
= LTTNG_ERR_NOMEM
;
3784 extended
= (struct lttng_channel_extended
*)
3785 channel
->attr
.extended
.ptr
;
3787 ret
= get_ust_runtime_stats(session
, uchan
,
3788 &discarded_events
, &lost_packets
);
3790 lttng_channel_destroy(channel
);
3791 ret_code
= LTTNG_ERR_UNK
;
3795 extended
->discarded_events
= discarded_events
;
3796 extended
->lost_packets
= lost_packets
;
3798 ret
= lttng_channel_serialize(
3799 channel
, &payload
->buffer
);
3801 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3803 lttng_channel_destroy(channel
);
3804 ret_code
= LTTNG_ERR_UNK
;
3808 lttng_channel_destroy(channel
);
3818 if (i
> UINT32_MAX
) {
3819 ERR("Channel count would overflow the channel listing command's reply");
3820 ret_code
= LTTNG_ERR_OVERFLOW
;
3824 /* Update command reply header. */
3825 cmd_header
.count
= (uint32_t) i
;
3826 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3827 sizeof(cmd_header
));
3828 ret_code
= LTTNG_OK
;
3835 * Command LTTNG_LIST_EVENTS processed by the client thread.
3837 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3838 struct ltt_session
*session
,
3840 struct lttng_payload
*reply_payload
)
3842 int buffer_resize_ret
;
3843 enum lttng_error_code ret_code
= LTTNG_OK
;
3844 struct lttcomm_list_command_header reply_command_header
= {};
3845 size_t reply_command_header_offset
;
3846 unsigned int nb_events
= 0;
3848 assert(reply_payload
);
3850 /* Reserve space for command reply header. */
3851 reply_command_header_offset
= reply_payload
->buffer
.size
;
3852 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3853 reply_command_header_offset
+
3854 sizeof(struct lttcomm_list_command_header
));
3855 if (buffer_resize_ret
) {
3856 ret_code
= LTTNG_ERR_NOMEM
;
3861 case LTTNG_DOMAIN_KERNEL
:
3862 if (session
->kernel_session
!= NULL
) {
3863 ret_code
= list_lttng_kernel_events(channel_name
,
3864 session
->kernel_session
, reply_payload
, &nb_events
);
3868 case LTTNG_DOMAIN_UST
:
3870 if (session
->ust_session
!= NULL
) {
3871 ret_code
= list_lttng_ust_global_events(channel_name
,
3872 &session
->ust_session
->domain_global
,
3873 reply_payload
, &nb_events
);
3878 case LTTNG_DOMAIN_LOG4J
:
3879 case LTTNG_DOMAIN_JUL
:
3880 case LTTNG_DOMAIN_PYTHON
:
3881 if (session
->ust_session
) {
3882 struct lttng_ht_iter iter
;
3886 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3887 &iter
.iter
, agt
, node
.node
) {
3888 if (agt
->domain
== domain
) {
3889 ret_code
= list_lttng_agent_events(
3890 agt
, reply_payload
, &nb_events
);
3899 ret_code
= LTTNG_ERR_UND
;
3903 if (nb_events
> UINT32_MAX
) {
3904 ret_code
= LTTNG_ERR_OVERFLOW
;
3908 /* Update command reply header. */
3909 reply_command_header
.count
= (uint32_t) nb_events
;
3910 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
3911 sizeof(reply_command_header
));
3918 * Using the session list, filled a lttng_session array to send back to the
3919 * client for session listing.
3921 * The session list lock MUST be acquired before calling this function. Use
3922 * session_lock_list() and session_unlock_list().
3924 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3925 size_t session_count
, uid_t uid
, gid_t gid
)
3929 struct ltt_session
*session
;
3930 struct ltt_session_list
*list
= session_get_list();
3931 struct lttng_session_extended
*extended
=
3932 (typeof(extended
)) (&sessions
[session_count
]);
3934 DBG("Getting all available session for UID %d GID %d",
3937 * Iterate over session list and append data after the control struct in
3940 cds_list_for_each_entry(session
, &list
->head
, list
) {
3941 if (!session_get(session
)) {
3945 * Only list the sessions the user can control.
3947 if (!session_access_ok(session
, uid
) ||
3948 session
->destroyed
) {
3949 session_put(session
);
3953 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3954 struct ltt_ust_session
*usess
= session
->ust_session
;
3956 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3957 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3958 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3959 ret
= build_network_session_path(sessions
[i
].path
,
3960 sizeof(sessions
[i
].path
), session
);
3962 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3963 session
->consumer
->dst
.session_root_path
);
3966 PERROR("snprintf session path");
3967 session_put(session
);
3971 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3972 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3973 sessions
[i
].enabled
= session
->active
;
3974 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3975 sessions
[i
].live_timer_interval
= session
->live_timer
;
3976 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3977 extended
[i
].creation_time
.is_set
= 1;
3979 session_put(session
);
3984 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3985 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3987 int cmd_data_pending(struct ltt_session
*session
)
3990 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3991 struct ltt_ust_session
*usess
= session
->ust_session
;
3993 LTTNG_ASSERT(session
);
3995 DBG("Data pending for session %s", session
->name
);
3997 /* Session MUST be stopped to ask for data availability. */
3998 if (session
->active
) {
3999 ret
= LTTNG_ERR_SESSION_STARTED
;
4003 * If stopped, just make sure we've started before else the above call
4004 * will always send that there is data pending.
4006 * The consumer assumes that when the data pending command is received,
4007 * the trace has been started before or else no output data is written
4008 * by the streams which is a condition for data pending. So, this is
4009 * *VERY* important that we don't ask the consumer before a start
4012 if (!session
->has_been_started
) {
4018 /* A rotation is still pending, we have to wait. */
4019 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4020 DBG("Rotate still pending for session %s", session
->name
);
4025 if (ksess
&& ksess
->consumer
) {
4026 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4028 /* Data is still being extracted for the kernel. */
4033 if (usess
&& usess
->consumer
) {
4034 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4036 /* Data is still being extracted for the kernel. */
4041 /* Data is ready to be read by a viewer */
4049 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4051 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4053 int cmd_snapshot_add_output(struct ltt_session
*session
,
4054 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4057 struct snapshot_output
*new_output
;
4059 LTTNG_ASSERT(session
);
4060 LTTNG_ASSERT(output
);
4062 DBG("Cmd snapshot add output for session %s", session
->name
);
4065 * Can't create an output if the session is not set in no-output mode.
4067 if (session
->output_traces
) {
4068 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4072 if (session
->has_non_mmap_channel
) {
4073 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4077 /* Only one output is allowed until we have the "tee" feature. */
4078 if (session
->snapshot
.nb_output
== 1) {
4079 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4083 new_output
= snapshot_output_alloc();
4085 ret
= LTTNG_ERR_NOMEM
;
4089 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4090 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4091 &session
->snapshot
);
4093 if (ret
== -ENOMEM
) {
4094 ret
= LTTNG_ERR_NOMEM
;
4096 ret
= LTTNG_ERR_INVALID
;
4102 snapshot_add_output(&session
->snapshot
, new_output
);
4104 *id
= new_output
->id
;
4111 snapshot_output_destroy(new_output
);
4117 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4119 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4121 int cmd_snapshot_del_output(struct ltt_session
*session
,
4122 const struct lttng_snapshot_output
*output
)
4125 struct snapshot_output
*sout
= NULL
;
4127 LTTNG_ASSERT(session
);
4128 LTTNG_ASSERT(output
);
4133 * Permission denied to create an output if the session is not
4134 * set in no output mode.
4136 if (session
->output_traces
) {
4137 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4142 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4144 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4145 } else if (*output
->name
!= '\0') {
4146 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4148 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4151 ret
= LTTNG_ERR_INVALID
;
4155 snapshot_delete_output(&session
->snapshot
, sout
);
4156 snapshot_output_destroy(sout
);
4165 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4167 * If no output is available, outputs is untouched and 0 is returned.
4169 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4171 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4172 struct lttng_snapshot_output
**outputs
)
4175 struct lttng_snapshot_output
*list
= NULL
;
4176 struct lttng_ht_iter iter
;
4177 struct snapshot_output
*output
;
4179 LTTNG_ASSERT(session
);
4180 LTTNG_ASSERT(outputs
);
4182 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4185 * Permission denied to create an output if the session is not
4186 * set in no output mode.
4188 if (session
->output_traces
) {
4189 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4193 if (session
->snapshot
.nb_output
== 0) {
4198 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4200 ret
= -LTTNG_ERR_NOMEM
;
4204 /* Copy list from session to the new list object. */
4206 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4207 output
, node
.node
) {
4208 LTTNG_ASSERT(output
->consumer
);
4209 list
[idx
].id
= output
->id
;
4210 list
[idx
].max_size
= output
->max_size
;
4211 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4212 sizeof(list
[idx
].name
))) {
4213 ret
= -LTTNG_ERR_INVALID
;
4216 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4217 if (lttng_strncpy(list
[idx
].ctrl_url
,
4218 output
->consumer
->dst
.session_root_path
,
4219 sizeof(list
[idx
].ctrl_url
))) {
4220 ret
= -LTTNG_ERR_INVALID
;
4225 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4226 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4228 ret
= -LTTNG_ERR_NOMEM
;
4233 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4234 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4236 ret
= -LTTNG_ERR_NOMEM
;
4245 ret
= session
->snapshot
.nb_output
;
4254 * Check if we can regenerate the metadata for this session.
4255 * Only kernel, UST per-uid and non-live sessions are supported.
4257 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4260 int check_regenerate_metadata_support(struct ltt_session
*session
)
4264 LTTNG_ASSERT(session
);
4266 if (session
->live_timer
!= 0) {
4267 ret
= LTTNG_ERR_LIVE_SESSION
;
4270 if (!session
->active
) {
4271 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4274 if (session
->ust_session
) {
4275 switch (session
->ust_session
->buffer_type
) {
4276 case LTTNG_BUFFER_PER_UID
:
4278 case LTTNG_BUFFER_PER_PID
:
4279 ret
= LTTNG_ERR_PER_PID_SESSION
;
4283 ret
= LTTNG_ERR_UNK
;
4287 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4288 session
->consumer
->relay_minor_version
< 8) {
4289 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4299 int clear_metadata_file(int fd
)
4304 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4305 if (lseek_ret
< 0) {
4311 ret
= ftruncate(fd
, 0);
4313 PERROR("ftruncate");
4322 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4325 struct buffer_reg_uid
*uid_reg
= NULL
;
4326 struct buffer_reg_session
*session_reg
= NULL
;
4329 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4330 struct ust_registry_session
*registry
;
4331 struct ust_registry_channel
*chan
;
4332 struct lttng_ht_iter iter_chan
;
4334 session_reg
= uid_reg
->registry
;
4335 registry
= session_reg
->reg
.ust
;
4337 pthread_mutex_lock(®istry
->lock
);
4338 registry
->metadata_len_sent
= 0;
4339 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4340 registry
->metadata_len
= 0;
4341 registry
->metadata_version
++;
4342 if (registry
->metadata_fd
> 0) {
4343 /* Clear the metadata file's content. */
4344 ret
= clear_metadata_file(registry
->metadata_fd
);
4346 pthread_mutex_unlock(®istry
->lock
);
4351 ret
= ust_metadata_session_statedump(registry
, NULL
,
4352 registry
->major
, registry
->minor
);
4354 pthread_mutex_unlock(®istry
->lock
);
4355 ERR("Failed to generate session metadata (err = %d)",
4359 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4361 struct ust_registry_event
*event
;
4362 struct lttng_ht_iter iter_event
;
4364 chan
->metadata_dumped
= 0;
4366 ret
= ust_metadata_channel_statedump(registry
, chan
);
4368 pthread_mutex_unlock(®istry
->lock
);
4369 ERR("Failed to generate channel metadata "
4373 cds_lfht_for_each_entry(chan
->events
->ht
, &iter_event
.iter
,
4375 event
->metadata_dumped
= 0;
4376 ret
= ust_metadata_event_statedump(registry
,
4379 pthread_mutex_unlock(®istry
->lock
);
4380 ERR("Failed to generate event metadata "
4386 pthread_mutex_unlock(®istry
->lock
);
4395 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4397 * Ask the consumer to truncate the existing metadata file(s) and
4398 * then regenerate the metadata. Live and per-pid sessions are not
4399 * supported and return an error.
4401 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4403 int cmd_regenerate_metadata(struct ltt_session
*session
)
4407 LTTNG_ASSERT(session
);
4409 ret
= check_regenerate_metadata_support(session
);
4414 if (session
->kernel_session
) {
4415 ret
= kernctl_session_regenerate_metadata(
4416 session
->kernel_session
->fd
);
4418 ERR("Failed to regenerate the kernel metadata");
4423 if (session
->ust_session
) {
4424 ret
= ust_regenerate_metadata(session
->ust_session
);
4426 ERR("Failed to regenerate the UST metadata");
4430 DBG("Cmd metadata regenerate for session %s", session
->name
);
4438 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4440 * Ask the tracer to regenerate a new statedump.
4442 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4444 int cmd_regenerate_statedump(struct ltt_session
*session
)
4448 LTTNG_ASSERT(session
);
4450 if (!session
->active
) {
4451 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4455 if (session
->kernel_session
) {
4456 ret
= kernctl_session_regenerate_statedump(
4457 session
->kernel_session
->fd
);
4459 * Currently, the statedump in kernel can only fail if out
4463 if (ret
== -ENOMEM
) {
4464 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4466 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4468 ERR("Failed to regenerate the kernel statedump");
4473 if (session
->ust_session
) {
4474 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4476 * Currently, the statedump in UST always returns 0.
4479 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4480 ERR("Failed to regenerate the UST statedump");
4484 DBG("Cmd regenerate statedump for session %s", session
->name
);
4492 enum lttng_error_code
synchronize_tracer_notifier_register(
4493 struct notification_thread_handle
*notification_thread
,
4494 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4496 enum lttng_error_code ret_code
;
4497 const struct lttng_condition
*condition
=
4498 lttng_trigger_get_const_condition(trigger
);
4499 const char *trigger_name
;
4500 uid_t trigger_owner
;
4501 enum lttng_trigger_status trigger_status
;
4502 const enum lttng_domain_type trigger_domain
=
4503 lttng_trigger_get_underlying_domain_type_restriction(
4506 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4507 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4509 LTTNG_ASSERT(condition
);
4510 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4511 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4513 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4514 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4515 trigger_name
: "(anonymous)";
4517 session_lock_list();
4518 switch (trigger_domain
) {
4519 case LTTNG_DOMAIN_KERNEL
:
4521 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4522 if (ret_code
!= LTTNG_OK
) {
4523 enum lttng_error_code notif_thread_unregister_ret
;
4525 notif_thread_unregister_ret
=
4526 notification_thread_command_unregister_trigger(
4527 notification_thread
, trigger
);
4529 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4530 /* Return the original error code. */
4531 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4533 (int) trigger_owner
,
4539 case LTTNG_DOMAIN_UST
:
4540 ust_app_global_update_all_event_notifier_rules();
4542 case LTTNG_DOMAIN_JUL
:
4543 case LTTNG_DOMAIN_LOG4J
:
4544 case LTTNG_DOMAIN_PYTHON
:
4546 /* Agent domains. */
4547 struct agent
*agt
= agent_find_by_event_notifier_domain(
4551 agt
= agent_create(trigger_domain
);
4553 ret_code
= LTTNG_ERR_NOMEM
;
4554 goto end_unlock_session_list
;
4557 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4560 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4561 if (ret_code
!= LTTNG_OK
) {
4562 goto end_unlock_session_list
;
4567 case LTTNG_DOMAIN_NONE
:
4572 ret_code
= LTTNG_OK
;
4573 end_unlock_session_list
:
4574 session_unlock_list();
4578 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4579 struct lttng_trigger
*trigger
,
4580 bool is_trigger_anonymous
,
4581 struct notification_thread_handle
*notification_thread
,
4582 struct lttng_trigger
**return_trigger
)
4584 enum lttng_error_code ret_code
;
4585 const char *trigger_name
;
4586 uid_t trigger_owner
;
4587 enum lttng_trigger_status trigger_status
;
4589 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4590 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4591 trigger_name
: "(anonymous)";
4593 trigger_status
= lttng_trigger_get_owner_uid(
4594 trigger
, &trigger_owner
);
4595 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4597 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4598 trigger_name
, (int) trigger_owner
,
4599 (int) lttng_credentials_get_uid(cmd_creds
));
4602 * Validate the trigger credentials against the command credentials.
4603 * Only the root user can register a trigger with non-matching
4606 if (!lttng_credentials_is_equal_uid(
4607 lttng_trigger_get_credentials(trigger
),
4609 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4610 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4611 trigger_name
, (int) trigger_owner
,
4612 (int) lttng_credentials_get_uid(cmd_creds
));
4613 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4619 * The bytecode generation also serves as a validation step for the
4620 * bytecode expressions.
4622 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4623 if (ret_code
!= LTTNG_OK
) {
4624 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4625 trigger_name
, (int) trigger_owner
, ret_code
);
4630 * A reference to the trigger is acquired by the notification thread.
4631 * It is safe to return the same trigger to the caller since it the
4632 * other user holds a reference.
4634 * The trigger is modified during the execution of the
4635 * "register trigger" command. However, by the time the command returns,
4636 * it is safe to use without any locking as its properties are
4639 ret_code
= notification_thread_command_register_trigger(
4640 notification_thread
, trigger
, is_trigger_anonymous
);
4641 if (ret_code
!= LTTNG_OK
) {
4642 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4643 trigger_name
, (int) trigger_owner
, ret_code
);
4647 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4648 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4649 trigger_name
: "(anonymous)";
4652 * Synchronize tracers if the trigger adds an event notifier.
4654 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4655 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4656 trigger
, cmd_creds
);
4657 if (ret_code
!= LTTNG_OK
) {
4658 ERR("Error registering tracer notifier: %s",
4659 lttng_strerror(-ret_code
));
4665 * Return an updated trigger to the client.
4667 * Since a modified version of the same trigger is returned, acquire a
4668 * reference to the trigger so the caller doesn't have to care if those
4669 * are distinct instances or not.
4671 if (ret_code
== LTTNG_OK
) {
4672 lttng_trigger_get(trigger
);
4673 *return_trigger
= trigger
;
4674 /* Ownership of trigger was transferred to caller. */
4682 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4683 const struct lttng_trigger
*trigger
)
4685 enum lttng_error_code ret_code
;
4686 const struct lttng_condition
*condition
=
4687 lttng_trigger_get_const_condition(trigger
);
4688 const enum lttng_domain_type trigger_domain
=
4689 lttng_trigger_get_underlying_domain_type_restriction(
4692 LTTNG_ASSERT(condition
);
4693 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4694 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4696 session_lock_list();
4697 switch (trigger_domain
) {
4698 case LTTNG_DOMAIN_KERNEL
:
4699 ret_code
= kernel_unregister_event_notifier(trigger
);
4700 if (ret_code
!= LTTNG_OK
) {
4701 goto end_unlock_session_list
;
4705 case LTTNG_DOMAIN_UST
:
4706 ust_app_global_update_all_event_notifier_rules();
4708 case LTTNG_DOMAIN_JUL
:
4709 case LTTNG_DOMAIN_LOG4J
:
4710 case LTTNG_DOMAIN_PYTHON
:
4712 /* Agent domains. */
4713 struct agent
*agt
= agent_find_by_event_notifier_domain(
4717 * This trigger was never registered in the first place. Calling
4718 * this function under those circumstances is an internal error.
4721 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4722 if (ret_code
!= LTTNG_OK
) {
4723 goto end_unlock_session_list
;
4728 case LTTNG_DOMAIN_NONE
:
4733 ret_code
= LTTNG_OK
;
4735 end_unlock_session_list
:
4736 session_unlock_list();
4740 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4741 const struct lttng_trigger
*trigger
,
4742 struct notification_thread_handle
*notification_thread
)
4744 enum lttng_error_code ret_code
;
4745 const char *trigger_name
;
4746 uid_t trigger_owner
;
4747 enum lttng_trigger_status trigger_status
;
4748 struct lttng_trigger
*sessiond_trigger
= NULL
;
4750 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4751 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4752 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4753 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4755 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4756 trigger_name
, (int) trigger_owner
,
4757 (int) lttng_credentials_get_uid(cmd_creds
));
4760 * Validate the trigger credentials against the command credentials.
4761 * Only the root user can unregister a trigger with non-matching
4764 if (!lttng_credentials_is_equal_uid(
4765 lttng_trigger_get_credentials(trigger
),
4767 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4768 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4769 trigger_name
, (int) trigger_owner
,
4770 (int) lttng_credentials_get_uid(cmd_creds
));
4771 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4776 /* Fetch the sessiond side trigger object. */
4777 ret_code
= notification_thread_command_get_trigger(
4778 notification_thread
, trigger
, &sessiond_trigger
);
4779 if (ret_code
!= LTTNG_OK
) {
4780 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4781 trigger_name
, (int) trigger_owner
, ret_code
);
4785 LTTNG_ASSERT(sessiond_trigger
);
4788 * From this point on, no matter what, consider the trigger
4791 * We set the unregistered state of the sessiond side trigger object in
4792 * the client thread since we want to minimize the possibility of the
4793 * notification thread being stalled due to a long execution of an
4794 * action that required the trigger lock.
4796 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4798 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4800 if (ret_code
!= LTTNG_OK
) {
4801 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4802 trigger_name
, (int) trigger_owner
, ret_code
);
4807 * Synchronize tracers if the trigger removes an event notifier.
4808 * Do this even if the trigger unregistration failed to at least stop
4809 * the tracers from producing notifications associated with this
4812 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4813 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4814 if (ret_code
!= LTTNG_OK
) {
4815 ERR("Error unregistering trigger to tracer.");
4822 lttng_trigger_put(sessiond_trigger
);
4826 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4827 struct notification_thread_handle
*notification_thread
,
4828 struct lttng_triggers
**return_triggers
)
4831 enum lttng_error_code ret_code
;
4832 struct lttng_triggers
*triggers
= NULL
;
4834 /* Get the set of triggers from the notification thread. */
4835 ret_code
= notification_thread_command_list_triggers(
4836 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4837 if (ret_code
!= LTTNG_OK
) {
4841 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4843 ret_code
= LTTNG_ERR_UNK
;
4847 *return_triggers
= triggers
;
4849 ret_code
= LTTNG_OK
;
4851 lttng_triggers_destroy(triggers
);
4855 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4856 const struct lttng_error_query
*query
,
4857 struct lttng_error_query_results
**_results
,
4858 struct notification_thread_handle
*notification_thread
)
4860 enum lttng_error_code ret_code
;
4861 const struct lttng_trigger
*query_target_trigger
;
4862 const struct lttng_action
*query_target_action
= NULL
;
4863 struct lttng_trigger
*matching_trigger
= NULL
;
4864 const char *trigger_name
;
4865 uid_t trigger_owner
;
4866 enum lttng_trigger_status trigger_status
;
4867 struct lttng_error_query_results
*results
= NULL
;
4869 switch (lttng_error_query_get_target_type(query
)) {
4870 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4871 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4873 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4874 query_target_trigger
=
4875 lttng_error_query_condition_borrow_target(query
);
4877 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4878 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4885 LTTNG_ASSERT(query_target_trigger
);
4887 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4888 query_target_trigger
, &matching_trigger
);
4889 if (ret_code
!= LTTNG_OK
) {
4893 /* No longer needed. */
4894 query_target_trigger
= NULL
;
4896 if (lttng_error_query_get_target_type(query
) ==
4897 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4898 /* Get the sessiond-side version of the target action. */
4899 query_target_action
=
4900 lttng_error_query_action_borrow_action_target(
4901 query
, matching_trigger
);
4904 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4905 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4906 trigger_name
: "(anonymous)";
4907 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4909 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4911 results
= lttng_error_query_results_create();
4913 ret_code
= LTTNG_ERR_NOMEM
;
4917 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4918 trigger_name
, (int) trigger_owner
,
4919 (int) lttng_credentials_get_uid(cmd_creds
));
4922 * Validate the trigger credentials against the command credentials.
4923 * Only the root user can target a trigger with non-matching
4926 if (!lttng_credentials_is_equal_uid(
4927 lttng_trigger_get_credentials(matching_trigger
),
4929 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4930 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4931 trigger_name
, (int) trigger_owner
,
4932 (int) lttng_credentials_get_uid(cmd_creds
));
4933 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4938 switch (lttng_error_query_get_target_type(query
)) {
4939 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4940 trigger_status
= lttng_trigger_add_error_results(
4941 matching_trigger
, results
);
4943 switch (trigger_status
) {
4944 case LTTNG_TRIGGER_STATUS_OK
:
4947 ret_code
= LTTNG_ERR_UNK
;
4952 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4954 trigger_status
= lttng_trigger_condition_add_error_results(
4955 matching_trigger
, results
);
4957 switch (trigger_status
) {
4958 case LTTNG_TRIGGER_STATUS_OK
:
4961 ret_code
= LTTNG_ERR_UNK
;
4967 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4969 const enum lttng_action_status action_status
=
4970 lttng_action_add_error_query_results(
4971 query_target_action
, results
);
4973 switch (action_status
) {
4974 case LTTNG_ACTION_STATUS_OK
:
4977 ret_code
= LTTNG_ERR_UNK
;
4988 *_results
= results
;
4990 ret_code
= LTTNG_OK
;
4992 lttng_trigger_put(matching_trigger
);
4993 lttng_error_query_results_destroy(results
);
4998 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4999 * snapshot output is *not* set with a remote destination.
5001 * Return LTTNG_OK on success or a LTTNG_ERR code.
5003 static enum lttng_error_code
set_relayd_for_snapshot(
5004 struct consumer_output
*output
,
5005 const struct ltt_session
*session
)
5007 enum lttng_error_code status
= LTTNG_OK
;
5008 struct lttng_ht_iter iter
;
5009 struct consumer_socket
*socket
;
5010 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
5011 const char *base_path
;
5013 LTTNG_ASSERT(output
);
5014 LTTNG_ASSERT(session
);
5016 DBG2("Set relayd object from snapshot output");
5018 if (session
->current_trace_chunk
) {
5019 enum lttng_trace_chunk_status chunk_status
=
5020 lttng_trace_chunk_get_id(
5021 session
->current_trace_chunk
,
5022 ¤t_chunk_id
.value
);
5024 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5025 current_chunk_id
.is_set
= true;
5027 ERR("Failed to get current trace chunk id");
5028 status
= LTTNG_ERR_UNK
;
5033 /* Ignore if snapshot consumer output is not network. */
5034 if (output
->type
!= CONSUMER_DST_NET
) {
5039 * The snapshot record URI base path overrides the session
5042 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5043 base_path
= output
->dst
.net
.control
.subdir
;
5045 base_path
= session
->base_path
;
5049 * For each consumer socket, create and send the relayd object of the
5053 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
5054 socket
, node
.node
) {
5055 pthread_mutex_lock(socket
->lock
);
5056 status
= send_consumer_relayd_sockets(session
->id
,
5058 session
->name
, session
->hostname
,
5060 session
->live_timer
,
5061 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
5062 session
->creation_time
,
5063 session
->name_contains_creation_time
);
5064 pthread_mutex_unlock(socket
->lock
);
5065 if (status
!= LTTNG_OK
) {
5077 * Record a kernel snapshot.
5079 * Return LTTNG_OK on success or a LTTNG_ERR code.
5081 static enum lttng_error_code
record_kernel_snapshot(
5082 struct ltt_kernel_session
*ksess
,
5083 const struct consumer_output
*output
,
5084 const struct ltt_session
*session
,
5085 uint64_t nb_packets_per_stream
)
5087 enum lttng_error_code status
;
5089 LTTNG_ASSERT(ksess
);
5090 LTTNG_ASSERT(output
);
5091 LTTNG_ASSERT(session
);
5093 status
= kernel_snapshot_record(
5094 ksess
, output
, nb_packets_per_stream
);
5099 * Record a UST snapshot.
5101 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5103 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5104 const struct consumer_output
*output
,
5105 const struct ltt_session
*session
,
5106 uint64_t nb_packets_per_stream
)
5108 enum lttng_error_code status
;
5110 LTTNG_ASSERT(usess
);
5111 LTTNG_ASSERT(output
);
5112 LTTNG_ASSERT(session
);
5114 status
= ust_app_snapshot_record(
5115 usess
, output
, nb_packets_per_stream
);
5120 uint64_t get_session_size_one_more_packet_per_stream(
5121 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5123 uint64_t tot_size
= 0;
5125 if (session
->kernel_session
) {
5126 struct ltt_kernel_channel
*chan
;
5127 const struct ltt_kernel_session
*ksess
=
5128 session
->kernel_session
;
5130 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5131 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5133 * Don't take channel into account if we
5134 * already grab all its packets.
5138 tot_size
+= chan
->channel
->attr
.subbuf_size
5139 * chan
->stream_count
;
5143 if (session
->ust_session
) {
5144 const struct ltt_ust_session
*usess
= session
->ust_session
;
5146 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5154 * Calculate the number of packets we can grab from each stream that
5155 * fits within the overall snapshot max size.
5157 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5158 * the number of packets per stream.
5160 * TODO: this approach is not perfect: we consider the worse case
5161 * (packet filling the sub-buffers) as an upper bound, but we could do
5162 * better if we do this calculation while we actually grab the packet
5163 * content: we would know how much padding we don't actually store into
5166 * This algorithm is currently bounded by the number of packets per
5169 * Since we call this algorithm before actually grabbing the data, it's
5170 * an approximation: for instance, applications could appear/disappear
5171 * in between this call and actually grabbing data.
5174 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5178 uint64_t cur_nb_packets
= 0;
5181 return 0; /* Infinite */
5184 size_left
= max_size
;
5186 uint64_t one_more_packet_tot_size
;
5188 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5189 session
, cur_nb_packets
);
5190 if (!one_more_packet_tot_size
) {
5191 /* We are already grabbing all packets. */
5194 size_left
-= one_more_packet_tot_size
;
5195 if (size_left
< 0) {
5200 if (!cur_nb_packets
&& size_left
!= max_size
) {
5201 /* Not enough room to grab one packet of each stream, error. */
5204 return cur_nb_packets
;
5208 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5209 const struct snapshot_output
*snapshot_output
)
5211 int64_t nb_packets_per_stream
;
5212 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5214 enum lttng_error_code ret_code
= LTTNG_OK
;
5215 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5216 struct consumer_output
*original_ust_consumer_output
= NULL
;
5217 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5218 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5219 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5221 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5223 snapshot_output
->name
,
5224 snapshot_output
->datetime
,
5225 snapshot_output
->nb_snapshot
);
5226 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5227 ERR("Failed to format snapshot name");
5228 ret_code
= LTTNG_ERR_INVALID
;
5231 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5232 snapshot_output
->name
, session
->name
,
5233 snapshot_chunk_name
);
5234 if (!session
->kernel_session
&& !session
->ust_session
) {
5235 ERR("Failed to record snapshot as no channels exist");
5236 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5240 if (session
->kernel_session
) {
5241 original_kernel_consumer_output
=
5242 session
->kernel_session
->consumer
;
5243 snapshot_kernel_consumer_output
=
5244 consumer_copy_output(snapshot_output
->consumer
);
5245 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5246 snapshot_chunk_name
);
5248 /* Copy the original domain subdir. */
5249 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5250 original_kernel_consumer_output
->domain_subdir
);
5252 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5253 original_kernel_consumer_output
);
5255 ERR("Failed to copy consumer sockets from snapshot output configuration");
5256 ret_code
= LTTNG_ERR_NOMEM
;
5259 ret_code
= set_relayd_for_snapshot(
5260 snapshot_kernel_consumer_output
, session
);
5261 if (ret_code
!= LTTNG_OK
) {
5262 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5265 session
->kernel_session
->consumer
=
5266 snapshot_kernel_consumer_output
;
5268 if (session
->ust_session
) {
5269 original_ust_consumer_output
= session
->ust_session
->consumer
;
5270 snapshot_ust_consumer_output
=
5271 consumer_copy_output(snapshot_output
->consumer
);
5272 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5273 snapshot_chunk_name
);
5275 /* Copy the original domain subdir. */
5276 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5277 original_ust_consumer_output
->domain_subdir
);
5279 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5280 original_ust_consumer_output
);
5282 ERR("Failed to copy consumer sockets from snapshot output configuration");
5283 ret_code
= LTTNG_ERR_NOMEM
;
5286 ret_code
= set_relayd_for_snapshot(
5287 snapshot_ust_consumer_output
, session
);
5288 if (ret_code
!= LTTNG_OK
) {
5289 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5292 session
->ust_session
->consumer
=
5293 snapshot_ust_consumer_output
;
5296 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5297 snapshot_kernel_consumer_output
?:
5298 snapshot_ust_consumer_output
,
5299 consumer_output_get_base_path(
5300 snapshot_output
->consumer
),
5301 snapshot_chunk_name
);
5302 if (!snapshot_trace_chunk
) {
5303 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5305 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5308 LTTNG_ASSERT(!session
->current_trace_chunk
);
5309 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5310 lttng_trace_chunk_put(snapshot_trace_chunk
);
5311 snapshot_trace_chunk
= NULL
;
5313 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5315 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5319 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5320 snapshot_output
->max_size
);
5321 if (nb_packets_per_stream
< 0) {
5322 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5323 goto error_close_trace_chunk
;
5326 if (session
->kernel_session
) {
5327 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5328 snapshot_kernel_consumer_output
, session
,
5329 nb_packets_per_stream
);
5330 if (ret_code
!= LTTNG_OK
) {
5331 goto error_close_trace_chunk
;
5335 if (session
->ust_session
) {
5336 ret_code
= record_ust_snapshot(session
->ust_session
,
5337 snapshot_ust_consumer_output
, session
,
5338 nb_packets_per_stream
);
5339 if (ret_code
!= LTTNG_OK
) {
5340 goto error_close_trace_chunk
;
5344 error_close_trace_chunk
:
5345 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5346 ERR("Failed to release the current trace chunk of session \"%s\"",
5348 ret_code
= LTTNG_ERR_UNK
;
5351 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5352 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5354 * Don't goto end; make sure the chunk is closed for the session
5355 * to allow future snapshots.
5357 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5359 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5362 lttng_trace_chunk_put(snapshot_trace_chunk
);
5363 snapshot_trace_chunk
= NULL
;
5365 if (original_ust_consumer_output
) {
5366 session
->ust_session
->consumer
= original_ust_consumer_output
;
5368 if (original_kernel_consumer_output
) {
5369 session
->kernel_session
->consumer
=
5370 original_kernel_consumer_output
;
5372 consumer_output_put(snapshot_ust_consumer_output
);
5373 consumer_output_put(snapshot_kernel_consumer_output
);
5378 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5380 * The wait parameter is ignored so this call always wait for the snapshot to
5381 * complete before returning.
5383 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5385 int cmd_snapshot_record(struct ltt_session
*session
,
5386 const struct lttng_snapshot_output
*output
,
5387 int wait
__attribute__((unused
)))
5389 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5391 unsigned int snapshot_success
= 0;
5393 struct snapshot_output
*tmp_output
= NULL
;
5395 LTTNG_ASSERT(session
);
5396 LTTNG_ASSERT(output
);
5398 DBG("Cmd snapshot record for session %s", session
->name
);
5400 /* Get the datetime for the snapshot output directory. */
5401 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5404 cmd_ret
= LTTNG_ERR_INVALID
;
5409 * Permission denied to create an output if the session is not
5410 * set in no output mode.
5412 if (session
->output_traces
) {
5413 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5417 /* The session needs to be started at least once. */
5418 if (!session
->has_been_started
) {
5419 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5423 /* Use temporary output for the session. */
5424 if (*output
->ctrl_url
!= '\0') {
5425 tmp_output
= snapshot_output_alloc();
5427 cmd_ret
= LTTNG_ERR_NOMEM
;
5431 ret
= snapshot_output_init(session
, output
->max_size
,
5433 output
->ctrl_url
, output
->data_url
,
5437 if (ret
== -ENOMEM
) {
5438 cmd_ret
= LTTNG_ERR_NOMEM
;
5440 cmd_ret
= LTTNG_ERR_INVALID
;
5444 /* Use the global session count for the temporary snapshot. */
5445 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5447 /* Use the global datetime */
5448 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5449 cmd_ret
= snapshot_record(session
, tmp_output
);
5450 if (cmd_ret
!= LTTNG_OK
) {
5453 snapshot_success
= 1;
5455 struct snapshot_output
*sout
;
5456 struct lttng_ht_iter iter
;
5459 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5460 &iter
.iter
, sout
, node
.node
) {
5461 struct snapshot_output output_copy
;
5464 * Make a local copy of the output and override output
5465 * parameters with those provided as part of the
5468 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5470 if (output
->max_size
!= (uint64_t) -1ULL) {
5471 output_copy
.max_size
= output
->max_size
;
5474 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5475 memcpy(output_copy
.datetime
, datetime
,
5478 /* Use temporary name. */
5479 if (*output
->name
!= '\0') {
5480 if (lttng_strncpy(output_copy
.name
,
5482 sizeof(output_copy
.name
))) {
5483 cmd_ret
= LTTNG_ERR_INVALID
;
5489 cmd_ret
= snapshot_record(session
, &output_copy
);
5490 if (cmd_ret
!= LTTNG_OK
) {
5494 snapshot_success
= 1;
5499 if (snapshot_success
) {
5500 session
->snapshot
.nb_snapshot
++;
5502 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5507 snapshot_output_destroy(tmp_output
);
5513 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5515 int cmd_set_session_shm_path(struct ltt_session
*session
,
5516 const char *shm_path
)
5519 LTTNG_ASSERT(session
);
5522 * Can only set shm path before session is started.
5524 if (session
->has_been_started
) {
5525 return LTTNG_ERR_SESSION_STARTED
;
5528 strncpy(session
->shm_path
, shm_path
,
5529 sizeof(session
->shm_path
));
5530 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5536 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5538 * Ask the consumer to rotate the session output directory.
5539 * The session lock must be held.
5541 * Returns LTTNG_OK on success or else a negative LTTng error code.
5543 int cmd_rotate_session(struct ltt_session
*session
,
5544 struct lttng_rotate_session_return
*rotate_return
,
5545 bool quiet_rotation
,
5546 enum lttng_trace_chunk_command_type command
)
5549 uint64_t ongoing_rotation_chunk_id
;
5550 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5551 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5552 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5553 enum lttng_trace_chunk_status chunk_status
;
5554 bool failed_to_rotate
= false;
5555 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5557 LTTNG_ASSERT(session
);
5559 if (!session
->has_been_started
) {
5560 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5565 * Explicit rotation is not supported for live sessions.
5566 * However, live sessions can perform a quiet rotation on
5568 * Rotation is not supported for snapshot traces (no output).
5570 if ((!quiet_rotation
&& session
->live_timer
) ||
5571 !session
->output_traces
) {
5572 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5576 /* Unsupported feature in lttng-relayd before 2.11. */
5577 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5578 (session
->consumer
->relay_major_version
== 2 &&
5579 session
->consumer
->relay_minor_version
< 11)) {
5580 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5584 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5585 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5586 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5590 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5591 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5593 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5598 * After a stop, we only allow one rotation to occur, the other ones are
5599 * useless until a new start.
5601 if (session
->rotated_after_last_stop
) {
5602 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5604 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5609 * After a stop followed by a clear, disallow following rotations a they would
5610 * generate empty chunks.
5612 if (session
->cleared_after_last_stop
) {
5613 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5615 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5619 if (session
->active
) {
5620 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5622 if (!new_trace_chunk
) {
5623 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5629 * The current trace chunk becomes the chunk being archived.
5631 * After this point, "chunk_being_archived" must absolutely
5632 * be closed on the consumer(s), otherwise it will never be
5633 * cleaned-up, which will result in a leak.
5635 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5636 &chunk_being_archived
);
5638 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5642 if (session
->kernel_session
) {
5643 cmd_ret
= kernel_rotate_session(session
);
5644 if (cmd_ret
!= LTTNG_OK
) {
5645 failed_to_rotate
= true;
5646 rotation_fail_code
= cmd_ret
;
5649 if (session
->ust_session
) {
5650 cmd_ret
= ust_app_rotate_session(session
);
5651 if (cmd_ret
!= LTTNG_OK
) {
5652 failed_to_rotate
= true;
5653 rotation_fail_code
= cmd_ret
;
5657 if (!session
->active
) {
5658 session
->rotated_after_last_stop
= true;
5661 if (!chunk_being_archived
) {
5662 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5664 if (failed_to_rotate
) {
5665 cmd_ret
= rotation_fail_code
;
5672 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5673 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5674 &ongoing_rotation_chunk_id
);
5675 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5677 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5678 command
, session
->last_chunk_path
);
5680 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5684 if (failed_to_rotate
) {
5685 cmd_ret
= rotation_fail_code
;
5689 session
->quiet_rotation
= quiet_rotation
;
5690 ret
= timer_session_rotation_pending_check_start(session
,
5691 DEFAULT_ROTATE_PENDING_TIMER
);
5693 cmd_ret
= LTTNG_ERR_UNK
;
5697 if (rotate_return
) {
5698 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5701 session
->chunk_being_archived
= chunk_being_archived
;
5702 chunk_being_archived
= NULL
;
5703 if (!quiet_rotation
) {
5704 ret
= notification_thread_command_session_rotation_ongoing(
5705 the_notification_thread_handle
, session
->name
,
5706 session
->uid
, session
->gid
,
5707 ongoing_rotation_chunk_id
);
5708 if (ret
!= LTTNG_OK
) {
5709 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5711 cmd_ret
= (lttng_error_code
) ret
;
5715 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5716 session
->name
, ongoing_rotation_chunk_id
);
5718 lttng_trace_chunk_put(new_trace_chunk
);
5719 lttng_trace_chunk_put(chunk_being_archived
);
5720 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5723 if (session_reset_rotation_state(session
,
5724 LTTNG_ROTATION_STATE_ERROR
)) {
5725 ERR("Failed to reset rotation state of session \"%s\"",
5732 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5734 * Check if the session has finished its rotation.
5736 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5738 int cmd_rotate_get_info(struct ltt_session
*session
,
5739 struct lttng_rotation_get_info_return
*info_return
,
5740 uint64_t rotation_id
)
5742 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5743 enum lttng_rotation_state rotation_state
;
5745 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5746 session
->most_recent_chunk_id
.value
);
5748 if (session
->chunk_being_archived
) {
5749 enum lttng_trace_chunk_status chunk_status
;
5752 chunk_status
= lttng_trace_chunk_get_id(
5753 session
->chunk_being_archived
,
5755 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5757 rotation_state
= rotation_id
== chunk_id
?
5758 LTTNG_ROTATION_STATE_ONGOING
:
5759 LTTNG_ROTATION_STATE_EXPIRED
;
5761 if (session
->last_archived_chunk_id
.is_set
&&
5762 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5763 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5765 rotation_state
= session
->rotation_state
;
5769 switch (rotation_state
) {
5770 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5771 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5774 case LTTNG_ROTATION_STATE_EXPIRED
:
5775 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5776 rotation_id
, session
->name
);
5778 case LTTNG_ROTATION_STATE_ONGOING
:
5779 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5780 rotation_id
, session
->name
);
5782 case LTTNG_ROTATION_STATE_COMPLETED
:
5786 char *current_tracing_path_reply
;
5787 size_t current_tracing_path_reply_len
;
5789 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5790 rotation_id
, session
->name
);
5792 switch (session_get_consumer_destination_type(session
)) {
5793 case CONSUMER_DST_LOCAL
:
5794 current_tracing_path_reply
=
5795 info_return
->location
.local
.absolute_path
;
5796 current_tracing_path_reply_len
=
5797 sizeof(info_return
->location
.local
.absolute_path
);
5798 info_return
->location_type
=
5799 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5800 fmt_ret
= asprintf(&chunk_path
,
5801 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5802 session_get_base_path(session
),
5803 session
->last_archived_chunk_name
);
5804 if (fmt_ret
== -1) {
5805 PERROR("Failed to format the path of the last archived trace chunk");
5806 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5807 cmd_ret
= LTTNG_ERR_UNK
;
5811 case CONSUMER_DST_NET
:
5813 uint16_t ctrl_port
, data_port
;
5815 current_tracing_path_reply
=
5816 info_return
->location
.relay
.relative_path
;
5817 current_tracing_path_reply_len
=
5818 sizeof(info_return
->location
.relay
.relative_path
);
5819 /* Currently the only supported relay protocol. */
5820 info_return
->location
.relay
.protocol
=
5821 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5823 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5824 session_get_net_consumer_hostname(session
),
5825 sizeof(info_return
->location
.relay
.host
));
5827 ERR("Failed to copy host name to rotate_get_info reply");
5828 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5829 cmd_ret
= LTTNG_ERR_SET_URL
;
5833 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5834 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5835 info_return
->location
.relay
.ports
.data
= data_port
;
5836 info_return
->location_type
=
5837 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5838 chunk_path
= strdup(session
->last_chunk_path
);
5840 ERR("Failed to allocate the path of the last archived trace chunk");
5841 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5842 cmd_ret
= LTTNG_ERR_UNK
;
5851 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5852 chunk_path
, current_tracing_path_reply_len
);
5855 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5856 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5857 cmd_ret
= LTTNG_ERR_UNK
;
5863 case LTTNG_ROTATION_STATE_ERROR
:
5864 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5865 rotation_id
, session
->name
);
5873 info_return
->status
= (int32_t) rotation_state
;
5878 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5880 * Configure the automatic rotation parameters.
5881 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5882 * 'activate' to false means deactivate the rotation schedule and validate that
5883 * 'new_value' has the same value as the currently active value.
5885 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5887 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5888 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5890 struct notification_thread_handle
*notification_thread_handle
)
5893 uint64_t *parameter_value
;
5895 LTTNG_ASSERT(session
);
5897 DBG("Cmd rotate set schedule session %s", session
->name
);
5899 if (session
->live_timer
|| !session
->output_traces
) {
5900 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5901 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5905 switch (schedule_type
) {
5906 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5907 parameter_value
= &session
->rotate_size
;
5909 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5910 parameter_value
= &session
->rotate_timer_period
;
5911 if (new_value
>= UINT_MAX
) {
5912 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5913 new_value
, UINT_MAX
);
5914 ret
= LTTNG_ERR_INVALID
;
5919 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5920 ret
= LTTNG_ERR_INVALID
;
5924 /* Improper use of the API. */
5925 if (new_value
== -1ULL) {
5926 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5927 ret
= LTTNG_ERR_INVALID
;
5932 * As indicated in struct ltt_session's comments, a value of == 0 means
5933 * this schedule rotation type is not in use.
5935 * Reject the command if we were asked to activate a schedule that was
5938 if (activate
&& *parameter_value
!= 0) {
5939 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5940 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5945 * Reject the command if we were asked to deactivate a schedule that was
5948 if (!activate
&& *parameter_value
== 0) {
5949 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5950 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5955 * Reject the command if we were asked to deactivate a schedule that
5958 if (!activate
&& *parameter_value
!= new_value
) {
5959 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5960 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5964 *parameter_value
= activate
? new_value
: 0;
5966 switch (schedule_type
) {
5967 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5968 if (activate
&& session
->active
) {
5970 * Only start the timer if the session is active,
5971 * otherwise it will be started when the session starts.
5973 ret
= timer_session_rotation_schedule_timer_start(
5974 session
, new_value
);
5976 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5977 ret
= LTTNG_ERR_UNK
;
5981 ret
= timer_session_rotation_schedule_timer_stop(
5984 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5985 ret
= LTTNG_ERR_UNK
;
5990 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5992 ret
= subscribe_session_consumed_size_rotation(session
,
5993 new_value
, notification_thread_handle
);
5995 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5996 ret
= LTTNG_ERR_UNK
;
6000 ret
= unsubscribe_session_consumed_size_rotation(session
,
6001 notification_thread_handle
);
6003 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
6004 ret
= LTTNG_ERR_UNK
;
6011 /* Would have been caught before. */
6023 /* Wait for a given path to be removed before continuing. */
6024 static enum lttng_error_code
wait_on_path(void *path_data
)
6026 const char *shm_path
= (const char *) path_data
;
6028 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6034 ret
= stat(shm_path
, &st
);
6036 if (errno
!= ENOENT
) {
6037 PERROR("stat() returned an error while checking for the existence of the shm path");
6039 DBG("shm path no longer exists, completing the destruction of session");
6043 if (!S_ISDIR(st
.st_mode
)) {
6044 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6049 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6055 * Returns a pointer to a handler to run on completion of a command.
6056 * Returns NULL if no handler has to be run for the last command executed.
6058 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6060 struct cmd_completion_handler
*handler
= current_completion_handler
;
6062 current_completion_handler
= NULL
;
6067 * Init command subsystem.
6072 * Set network sequence index to 1 for streams to match a relayd
6073 * socket on the consumer side.
6075 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6076 relayd_net_seq_idx
= 1;
6077 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6079 DBG("Command subsystem initialized");