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
= (lttng_bytecode
*) zmalloc(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
= (lttng_bytecode
*) zmalloc(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
= (cmd_destroy_session_reply_context
*) zmalloc(sizeof(*reply_context
));
3384 if (!reply_context
) {
3385 ret
= LTTNG_ERR_NOMEM
;
3388 reply_context
->reply_sock_fd
= *sock_fd
;
3392 LTTNG_ASSERT(session
);
3394 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3396 if (session
->active
) {
3397 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3399 ret
= cmd_stop_trace(session
);
3400 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3401 /* Carry on with the destruction of the session. */
3402 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3403 session
->name
, lttng_strerror(-ret
));
3404 destruction_last_error
= (lttng_error_code
) ret
;
3408 if (session
->rotation_schedule_timer_enabled
) {
3409 if (timer_session_rotation_schedule_timer_stop(
3411 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3413 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3417 if (session
->rotate_size
) {
3418 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3419 session
->rotate_size
= 0;
3422 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3424 * Perform a last rotation on destruction if rotations have
3425 * occurred during the session's lifetime.
3427 ret
= cmd_rotate_session(session
, NULL
, false,
3428 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3429 if (ret
!= LTTNG_OK
) {
3430 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3431 session
->name
, lttng_strerror(-ret
));
3432 destruction_last_error
= (lttng_error_code
) -ret
;
3434 if (reply_context
) {
3435 reply_context
->implicit_rotation_on_destroy
= true;
3437 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3439 * The user has not triggered a session rotation. However, to
3440 * ensure all data has been consumed, the session is rotated
3441 * to a 'null' trace chunk before it is destroyed.
3443 * This is a "quiet" rotation meaning that no notification is
3444 * emitted and no renaming of the current trace chunk takes
3447 ret
= cmd_rotate_session(session
, NULL
, true,
3448 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3450 * Rotation operations may not be supported by the kernel
3451 * tracer. Hence, do not consider this implicit rotation as
3452 * a session destruction error. The library has already stopped
3453 * the session and waited for pending data; there is nothing
3454 * left to do but complete the destruction of the session.
3456 if (ret
!= LTTNG_OK
&&
3457 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3458 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3459 session
->name
, lttng_strerror(ret
));
3460 destruction_last_error
= (lttng_error_code
) -ret
;
3464 if (session
->shm_path
[0]) {
3466 * When a session is created with an explicit shm_path,
3467 * the consumer daemon will create its shared memory files
3468 * at that location and will *not* unlink them. This is normal
3469 * as the intention of that feature is to make it possible
3470 * to retrieve the content of those files should a crash occur.
3472 * To ensure the content of those files can be used, the
3473 * sessiond daemon will replicate the content of the metadata
3474 * cache in a metadata file.
3476 * On clean-up, it is expected that the consumer daemon will
3477 * unlink the shared memory files and that the session daemon
3478 * will unlink the metadata file. Then, the session's directory
3479 * in the shm path can be removed.
3481 * Unfortunately, a flaw in the design of the sessiond's and
3482 * consumerd's tear down of channels makes it impossible to
3483 * determine when the sessiond _and_ the consumerd have both
3484 * destroyed their representation of a channel. For one, the
3485 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3486 * callbacks in both daemons.
3488 * However, it is also impossible for the sessiond to know when
3489 * the consumer daemon is done destroying its channel(s) since
3490 * it occurs as a reaction to the closing of the channel's file
3491 * descriptor. There is no resulting communication initiated
3492 * from the consumerd to the sessiond to confirm that the
3493 * operation is completed (and was successful).
3495 * Until this is all fixed, the session daemon checks for the
3496 * removal of the session's shm path which makes it possible
3497 * to safely advertise a session as having been destroyed.
3499 * Prior to this fix, it was not possible to reliably save
3500 * a session making use of the --shm-path option, destroy it,
3501 * and load it again. This is because the creation of the
3502 * session would fail upon seeing the session's shm path
3503 * already in existence.
3505 * Note that none of the error paths in the check for the
3506 * directory's existence return an error. This is normal
3507 * as there isn't much that can be done. The session will
3508 * be destroyed properly, except that we can't offer the
3509 * guarantee that the same session can be re-created.
3511 current_completion_handler
= &destroy_completion_handler
.handler
;
3512 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3514 sizeof(destroy_completion_handler
.shm_path
));
3519 * The session is destroyed. However, note that the command context
3520 * still holds a reference to the session, thus delaying its destruction
3521 * _at least_ up to the point when that reference is released.
3523 session_destroy(session
);
3524 if (reply_context
) {
3525 reply_context
->destruction_status
= destruction_last_error
;
3526 ret
= session_add_destroy_notifier(session
,
3527 cmd_destroy_session_reply
,
3528 (void *) reply_context
);
3530 ret
= LTTNG_ERR_FATAL
;
3542 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3544 int cmd_register_consumer(struct ltt_session
*session
,
3545 enum lttng_domain_type domain
, const char *sock_path
,
3546 struct consumer_data
*cdata
)
3549 struct consumer_socket
*socket
= NULL
;
3551 LTTNG_ASSERT(session
);
3552 LTTNG_ASSERT(cdata
);
3553 LTTNG_ASSERT(sock_path
);
3556 case LTTNG_DOMAIN_KERNEL
:
3558 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3560 LTTNG_ASSERT(ksess
);
3562 /* Can't register a consumer if there is already one */
3563 if (ksess
->consumer_fds_sent
!= 0) {
3564 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3568 sock
= lttcomm_connect_unix_sock(sock_path
);
3570 ret
= LTTNG_ERR_CONNECT_FAIL
;
3573 cdata
->cmd_sock
= sock
;
3575 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3576 if (socket
== NULL
) {
3579 PERROR("close register consumer");
3581 cdata
->cmd_sock
= -1;
3582 ret
= LTTNG_ERR_FATAL
;
3586 socket
->lock
= (pthread_mutex_t
*) zmalloc(sizeof(pthread_mutex_t
));
3587 if (socket
->lock
== NULL
) {
3588 PERROR("zmalloc pthread mutex");
3589 ret
= LTTNG_ERR_FATAL
;
3592 pthread_mutex_init(socket
->lock
, NULL
);
3593 socket
->registered
= 1;
3596 consumer_add_socket(socket
, ksess
->consumer
);
3599 pthread_mutex_lock(&cdata
->pid_mutex
);
3601 pthread_mutex_unlock(&cdata
->pid_mutex
);
3606 /* TODO: Userspace tracing */
3607 ret
= LTTNG_ERR_UND
;
3615 consumer_destroy_socket(socket
);
3621 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3623 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3624 struct lttng_domain
**domains
)
3629 struct lttng_ht_iter iter
;
3631 if (session
->kernel_session
!= NULL
) {
3632 DBG3("Listing domains found kernel domain");
3636 if (session
->ust_session
!= NULL
) {
3637 DBG3("Listing domains found UST global domain");
3641 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3643 if (agt
->being_used
) {
3654 *domains
= (lttng_domain
*) zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3655 if (*domains
== NULL
) {
3656 ret
= LTTNG_ERR_FATAL
;
3660 if (session
->kernel_session
!= NULL
) {
3661 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3663 /* Kernel session buffer type is always GLOBAL */
3664 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3669 if (session
->ust_session
!= NULL
) {
3670 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3671 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3675 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3677 if (agt
->being_used
) {
3678 (*domains
)[index
].type
= agt
->domain
;
3679 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3689 /* Return negative value to differentiate return code */
3695 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3697 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3698 struct ltt_session
*session
,
3699 struct lttng_payload
*payload
)
3703 struct lttcomm_list_command_header cmd_header
= {};
3704 size_t cmd_header_offset
;
3705 enum lttng_error_code ret_code
;
3710 DBG("Listing channels for session %s", session
->name
);
3712 cmd_header_offset
= payload
->buffer
.size
;
3714 /* Reserve space for command reply header. */
3715 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3716 cmd_header_offset
+ sizeof(cmd_header
));
3718 ret_code
= LTTNG_ERR_NOMEM
;
3723 case LTTNG_DOMAIN_KERNEL
:
3725 /* Kernel channels */
3726 struct ltt_kernel_channel
*kchan
;
3727 if (session
->kernel_session
!= NULL
) {
3728 cds_list_for_each_entry(kchan
,
3729 &session
->kernel_session
->channel_list
.head
, list
) {
3730 uint64_t discarded_events
, lost_packets
;
3731 struct lttng_channel_extended
*extended
;
3733 extended
= (struct lttng_channel_extended
*)
3734 kchan
->channel
->attr
.extended
.ptr
;
3736 ret
= get_kernel_runtime_stats(session
, kchan
,
3737 &discarded_events
, &lost_packets
);
3739 ret_code
= LTTNG_ERR_UNK
;
3744 * Update the discarded_events and lost_packets
3745 * count for the channel
3747 extended
->discarded_events
= discarded_events
;
3748 extended
->lost_packets
= lost_packets
;
3750 ret
= lttng_channel_serialize(
3751 kchan
->channel
, &payload
->buffer
);
3753 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3754 kchan
->channel
->name
);
3755 ret_code
= LTTNG_ERR_UNK
;
3764 case LTTNG_DOMAIN_UST
:
3766 struct lttng_ht_iter iter
;
3767 struct ltt_ust_channel
*uchan
;
3770 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3771 &iter
.iter
, uchan
, node
.node
) {
3772 uint64_t discarded_events
= 0, lost_packets
= 0;
3773 struct lttng_channel
*channel
= NULL
;
3774 struct lttng_channel_extended
*extended
;
3776 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3778 ret_code
= LTTNG_ERR_NOMEM
;
3782 extended
= (struct lttng_channel_extended
*)
3783 channel
->attr
.extended
.ptr
;
3785 ret
= get_ust_runtime_stats(session
, uchan
,
3786 &discarded_events
, &lost_packets
);
3788 lttng_channel_destroy(channel
);
3789 ret_code
= LTTNG_ERR_UNK
;
3793 extended
->discarded_events
= discarded_events
;
3794 extended
->lost_packets
= lost_packets
;
3796 ret
= lttng_channel_serialize(
3797 channel
, &payload
->buffer
);
3799 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3801 lttng_channel_destroy(channel
);
3802 ret_code
= LTTNG_ERR_UNK
;
3806 lttng_channel_destroy(channel
);
3816 if (i
> UINT32_MAX
) {
3817 ERR("Channel count would overflow the channel listing command's reply");
3818 ret_code
= LTTNG_ERR_OVERFLOW
;
3822 /* Update command reply header. */
3823 cmd_header
.count
= (uint32_t) i
;
3824 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3825 sizeof(cmd_header
));
3826 ret_code
= LTTNG_OK
;
3833 * Command LTTNG_LIST_EVENTS processed by the client thread.
3835 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3836 struct ltt_session
*session
,
3838 struct lttng_payload
*reply_payload
)
3840 int buffer_resize_ret
;
3841 enum lttng_error_code ret_code
= LTTNG_OK
;
3842 struct lttcomm_list_command_header reply_command_header
= {};
3843 size_t reply_command_header_offset
;
3844 unsigned int nb_events
= 0;
3846 assert(reply_payload
);
3848 /* Reserve space for command reply header. */
3849 reply_command_header_offset
= reply_payload
->buffer
.size
;
3850 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3851 reply_command_header_offset
+
3852 sizeof(struct lttcomm_list_command_header
));
3853 if (buffer_resize_ret
) {
3854 ret_code
= LTTNG_ERR_NOMEM
;
3859 case LTTNG_DOMAIN_KERNEL
:
3860 if (session
->kernel_session
!= NULL
) {
3861 ret_code
= list_lttng_kernel_events(channel_name
,
3862 session
->kernel_session
, reply_payload
, &nb_events
);
3866 case LTTNG_DOMAIN_UST
:
3868 if (session
->ust_session
!= NULL
) {
3869 ret_code
= list_lttng_ust_global_events(channel_name
,
3870 &session
->ust_session
->domain_global
,
3871 reply_payload
, &nb_events
);
3876 case LTTNG_DOMAIN_LOG4J
:
3877 case LTTNG_DOMAIN_JUL
:
3878 case LTTNG_DOMAIN_PYTHON
:
3879 if (session
->ust_session
) {
3880 struct lttng_ht_iter iter
;
3884 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3885 &iter
.iter
, agt
, node
.node
) {
3886 if (agt
->domain
== domain
) {
3887 ret_code
= list_lttng_agent_events(
3888 agt
, reply_payload
, &nb_events
);
3897 ret_code
= LTTNG_ERR_UND
;
3901 if (nb_events
> UINT32_MAX
) {
3902 ret_code
= LTTNG_ERR_OVERFLOW
;
3906 /* Update command reply header. */
3907 reply_command_header
.count
= (uint32_t) nb_events
;
3908 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
3909 sizeof(reply_command_header
));
3916 * Using the session list, filled a lttng_session array to send back to the
3917 * client for session listing.
3919 * The session list lock MUST be acquired before calling this function. Use
3920 * session_lock_list() and session_unlock_list().
3922 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3923 size_t session_count
, uid_t uid
, gid_t gid
)
3927 struct ltt_session
*session
;
3928 struct ltt_session_list
*list
= session_get_list();
3929 struct lttng_session_extended
*extended
=
3930 (typeof(extended
)) (&sessions
[session_count
]);
3932 DBG("Getting all available session for UID %d GID %d",
3935 * Iterate over session list and append data after the control struct in
3938 cds_list_for_each_entry(session
, &list
->head
, list
) {
3939 if (!session_get(session
)) {
3943 * Only list the sessions the user can control.
3945 if (!session_access_ok(session
, uid
) ||
3946 session
->destroyed
) {
3947 session_put(session
);
3951 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3952 struct ltt_ust_session
*usess
= session
->ust_session
;
3954 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3955 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3956 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3957 ret
= build_network_session_path(sessions
[i
].path
,
3958 sizeof(sessions
[i
].path
), session
);
3960 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3961 session
->consumer
->dst
.session_root_path
);
3964 PERROR("snprintf session path");
3965 session_put(session
);
3969 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3970 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3971 sessions
[i
].enabled
= session
->active
;
3972 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3973 sessions
[i
].live_timer_interval
= session
->live_timer
;
3974 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3975 extended
[i
].creation_time
.is_set
= 1;
3977 session_put(session
);
3982 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3983 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3985 int cmd_data_pending(struct ltt_session
*session
)
3988 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3989 struct ltt_ust_session
*usess
= session
->ust_session
;
3991 LTTNG_ASSERT(session
);
3993 DBG("Data pending for session %s", session
->name
);
3995 /* Session MUST be stopped to ask for data availability. */
3996 if (session
->active
) {
3997 ret
= LTTNG_ERR_SESSION_STARTED
;
4001 * If stopped, just make sure we've started before else the above call
4002 * will always send that there is data pending.
4004 * The consumer assumes that when the data pending command is received,
4005 * the trace has been started before or else no output data is written
4006 * by the streams which is a condition for data pending. So, this is
4007 * *VERY* important that we don't ask the consumer before a start
4010 if (!session
->has_been_started
) {
4016 /* A rotation is still pending, we have to wait. */
4017 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4018 DBG("Rotate still pending for session %s", session
->name
);
4023 if (ksess
&& ksess
->consumer
) {
4024 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4026 /* Data is still being extracted for the kernel. */
4031 if (usess
&& usess
->consumer
) {
4032 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4034 /* Data is still being extracted for the kernel. */
4039 /* Data is ready to be read by a viewer */
4047 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4049 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4051 int cmd_snapshot_add_output(struct ltt_session
*session
,
4052 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4055 struct snapshot_output
*new_output
;
4057 LTTNG_ASSERT(session
);
4058 LTTNG_ASSERT(output
);
4060 DBG("Cmd snapshot add output for session %s", session
->name
);
4063 * Can't create an output if the session is not set in no-output mode.
4065 if (session
->output_traces
) {
4066 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4070 if (session
->has_non_mmap_channel
) {
4071 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4075 /* Only one output is allowed until we have the "tee" feature. */
4076 if (session
->snapshot
.nb_output
== 1) {
4077 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4081 new_output
= snapshot_output_alloc();
4083 ret
= LTTNG_ERR_NOMEM
;
4087 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4088 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4089 &session
->snapshot
);
4091 if (ret
== -ENOMEM
) {
4092 ret
= LTTNG_ERR_NOMEM
;
4094 ret
= LTTNG_ERR_INVALID
;
4100 snapshot_add_output(&session
->snapshot
, new_output
);
4102 *id
= new_output
->id
;
4109 snapshot_output_destroy(new_output
);
4115 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4117 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4119 int cmd_snapshot_del_output(struct ltt_session
*session
,
4120 const struct lttng_snapshot_output
*output
)
4123 struct snapshot_output
*sout
= NULL
;
4125 LTTNG_ASSERT(session
);
4126 LTTNG_ASSERT(output
);
4131 * Permission denied to create an output if the session is not
4132 * set in no output mode.
4134 if (session
->output_traces
) {
4135 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4140 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4142 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4143 } else if (*output
->name
!= '\0') {
4144 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4146 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4149 ret
= LTTNG_ERR_INVALID
;
4153 snapshot_delete_output(&session
->snapshot
, sout
);
4154 snapshot_output_destroy(sout
);
4163 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4165 * If no output is available, outputs is untouched and 0 is returned.
4167 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4169 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4170 struct lttng_snapshot_output
**outputs
)
4173 struct lttng_snapshot_output
*list
= NULL
;
4174 struct lttng_ht_iter iter
;
4175 struct snapshot_output
*output
;
4177 LTTNG_ASSERT(session
);
4178 LTTNG_ASSERT(outputs
);
4180 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4183 * Permission denied to create an output if the session is not
4184 * set in no output mode.
4186 if (session
->output_traces
) {
4187 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4191 if (session
->snapshot
.nb_output
== 0) {
4196 list
= (lttng_snapshot_output
*) zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4198 ret
= -LTTNG_ERR_NOMEM
;
4202 /* Copy list from session to the new list object. */
4204 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4205 output
, node
.node
) {
4206 LTTNG_ASSERT(output
->consumer
);
4207 list
[idx
].id
= output
->id
;
4208 list
[idx
].max_size
= output
->max_size
;
4209 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4210 sizeof(list
[idx
].name
))) {
4211 ret
= -LTTNG_ERR_INVALID
;
4214 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4215 if (lttng_strncpy(list
[idx
].ctrl_url
,
4216 output
->consumer
->dst
.session_root_path
,
4217 sizeof(list
[idx
].ctrl_url
))) {
4218 ret
= -LTTNG_ERR_INVALID
;
4223 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4224 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4226 ret
= -LTTNG_ERR_NOMEM
;
4231 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4232 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4234 ret
= -LTTNG_ERR_NOMEM
;
4243 ret
= session
->snapshot
.nb_output
;
4252 * Check if we can regenerate the metadata for this session.
4253 * Only kernel, UST per-uid and non-live sessions are supported.
4255 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4258 int check_regenerate_metadata_support(struct ltt_session
*session
)
4262 LTTNG_ASSERT(session
);
4264 if (session
->live_timer
!= 0) {
4265 ret
= LTTNG_ERR_LIVE_SESSION
;
4268 if (!session
->active
) {
4269 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4272 if (session
->ust_session
) {
4273 switch (session
->ust_session
->buffer_type
) {
4274 case LTTNG_BUFFER_PER_UID
:
4276 case LTTNG_BUFFER_PER_PID
:
4277 ret
= LTTNG_ERR_PER_PID_SESSION
;
4281 ret
= LTTNG_ERR_UNK
;
4285 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4286 session
->consumer
->relay_minor_version
< 8) {
4287 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4297 int clear_metadata_file(int fd
)
4302 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4303 if (lseek_ret
< 0) {
4309 ret
= ftruncate(fd
, 0);
4311 PERROR("ftruncate");
4320 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4323 struct buffer_reg_uid
*uid_reg
= NULL
;
4324 struct buffer_reg_session
*session_reg
= NULL
;
4327 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4328 struct ust_registry_session
*registry
;
4329 struct ust_registry_channel
*chan
;
4330 struct lttng_ht_iter iter_chan
;
4332 session_reg
= uid_reg
->registry
;
4333 registry
= session_reg
->reg
.ust
;
4335 pthread_mutex_lock(®istry
->lock
);
4336 registry
->metadata_len_sent
= 0;
4337 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4338 registry
->metadata_len
= 0;
4339 registry
->metadata_version
++;
4340 if (registry
->metadata_fd
> 0) {
4341 /* Clear the metadata file's content. */
4342 ret
= clear_metadata_file(registry
->metadata_fd
);
4344 pthread_mutex_unlock(®istry
->lock
);
4349 ret
= ust_metadata_session_statedump(registry
, NULL
,
4350 registry
->major
, registry
->minor
);
4352 pthread_mutex_unlock(®istry
->lock
);
4353 ERR("Failed to generate session metadata (err = %d)",
4357 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4359 struct ust_registry_event
*event
;
4360 struct lttng_ht_iter iter_event
;
4362 ret
= ust_metadata_channel_statedump(registry
, chan
);
4364 pthread_mutex_unlock(®istry
->lock
);
4365 ERR("Failed to generate channel metadata "
4369 cds_lfht_for_each_entry(chan
->events
->ht
, &iter_event
.iter
,
4371 ret
= ust_metadata_event_statedump(registry
,
4374 pthread_mutex_unlock(®istry
->lock
);
4375 ERR("Failed to generate event metadata "
4381 pthread_mutex_unlock(®istry
->lock
);
4390 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4392 * Ask the consumer to truncate the existing metadata file(s) and
4393 * then regenerate the metadata. Live and per-pid sessions are not
4394 * supported and return an error.
4396 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4398 int cmd_regenerate_metadata(struct ltt_session
*session
)
4402 LTTNG_ASSERT(session
);
4404 ret
= check_regenerate_metadata_support(session
);
4409 if (session
->kernel_session
) {
4410 ret
= kernctl_session_regenerate_metadata(
4411 session
->kernel_session
->fd
);
4413 ERR("Failed to regenerate the kernel metadata");
4418 if (session
->ust_session
) {
4419 ret
= ust_regenerate_metadata(session
->ust_session
);
4421 ERR("Failed to regenerate the UST metadata");
4425 DBG("Cmd metadata regenerate for session %s", session
->name
);
4433 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4435 * Ask the tracer to regenerate a new statedump.
4437 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4439 int cmd_regenerate_statedump(struct ltt_session
*session
)
4443 LTTNG_ASSERT(session
);
4445 if (!session
->active
) {
4446 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4450 if (session
->kernel_session
) {
4451 ret
= kernctl_session_regenerate_statedump(
4452 session
->kernel_session
->fd
);
4454 * Currently, the statedump in kernel can only fail if out
4458 if (ret
== -ENOMEM
) {
4459 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4461 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4463 ERR("Failed to regenerate the kernel statedump");
4468 if (session
->ust_session
) {
4469 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4471 * Currently, the statedump in UST always returns 0.
4474 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4475 ERR("Failed to regenerate the UST statedump");
4479 DBG("Cmd regenerate statedump for session %s", session
->name
);
4487 enum lttng_error_code
synchronize_tracer_notifier_register(
4488 struct notification_thread_handle
*notification_thread
,
4489 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4491 enum lttng_error_code ret_code
;
4492 const struct lttng_condition
*condition
=
4493 lttng_trigger_get_const_condition(trigger
);
4494 const char *trigger_name
;
4495 uid_t trigger_owner
;
4496 enum lttng_trigger_status trigger_status
;
4497 const enum lttng_domain_type trigger_domain
=
4498 lttng_trigger_get_underlying_domain_type_restriction(
4501 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4502 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4504 LTTNG_ASSERT(condition
);
4505 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4506 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4508 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4509 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4510 trigger_name
: "(anonymous)";
4512 session_lock_list();
4513 switch (trigger_domain
) {
4514 case LTTNG_DOMAIN_KERNEL
:
4516 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4517 if (ret_code
!= LTTNG_OK
) {
4518 enum lttng_error_code notif_thread_unregister_ret
;
4520 notif_thread_unregister_ret
=
4521 notification_thread_command_unregister_trigger(
4522 notification_thread
, trigger
);
4524 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4525 /* Return the original error code. */
4526 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4528 (int) trigger_owner
,
4534 case LTTNG_DOMAIN_UST
:
4535 ust_app_global_update_all_event_notifier_rules();
4537 case LTTNG_DOMAIN_JUL
:
4538 case LTTNG_DOMAIN_LOG4J
:
4539 case LTTNG_DOMAIN_PYTHON
:
4541 /* Agent domains. */
4542 struct agent
*agt
= agent_find_by_event_notifier_domain(
4546 agt
= agent_create(trigger_domain
);
4548 ret_code
= LTTNG_ERR_NOMEM
;
4549 goto end_unlock_session_list
;
4552 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4555 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4556 if (ret_code
!= LTTNG_OK
) {
4557 goto end_unlock_session_list
;
4562 case LTTNG_DOMAIN_NONE
:
4567 ret_code
= LTTNG_OK
;
4568 end_unlock_session_list
:
4569 session_unlock_list();
4573 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4574 struct lttng_trigger
*trigger
,
4575 bool is_trigger_anonymous
,
4576 struct notification_thread_handle
*notification_thread
,
4577 struct lttng_trigger
**return_trigger
)
4579 enum lttng_error_code ret_code
;
4580 const char *trigger_name
;
4581 uid_t trigger_owner
;
4582 enum lttng_trigger_status trigger_status
;
4584 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4585 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4586 trigger_name
: "(anonymous)";
4588 trigger_status
= lttng_trigger_get_owner_uid(
4589 trigger
, &trigger_owner
);
4590 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4592 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4593 trigger_name
, (int) trigger_owner
,
4594 (int) lttng_credentials_get_uid(cmd_creds
));
4597 * Validate the trigger credentials against the command credentials.
4598 * Only the root user can register a trigger with non-matching
4601 if (!lttng_credentials_is_equal_uid(
4602 lttng_trigger_get_credentials(trigger
),
4604 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4605 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4606 trigger_name
, (int) trigger_owner
,
4607 (int) lttng_credentials_get_uid(cmd_creds
));
4608 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4614 * The bytecode generation also serves as a validation step for the
4615 * bytecode expressions.
4617 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4618 if (ret_code
!= LTTNG_OK
) {
4619 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4620 trigger_name
, (int) trigger_owner
, ret_code
);
4625 * A reference to the trigger is acquired by the notification thread.
4626 * It is safe to return the same trigger to the caller since it the
4627 * other user holds a reference.
4629 * The trigger is modified during the execution of the
4630 * "register trigger" command. However, by the time the command returns,
4631 * it is safe to use without any locking as its properties are
4634 ret_code
= notification_thread_command_register_trigger(
4635 notification_thread
, trigger
, is_trigger_anonymous
);
4636 if (ret_code
!= LTTNG_OK
) {
4637 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4638 trigger_name
, (int) trigger_owner
, ret_code
);
4642 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4643 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4644 trigger_name
: "(anonymous)";
4647 * Synchronize tracers if the trigger adds an event notifier.
4649 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4650 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4651 trigger
, cmd_creds
);
4652 if (ret_code
!= LTTNG_OK
) {
4653 ERR("Error registering tracer notifier: %s",
4654 lttng_strerror(-ret_code
));
4660 * Return an updated trigger to the client.
4662 * Since a modified version of the same trigger is returned, acquire a
4663 * reference to the trigger so the caller doesn't have to care if those
4664 * are distinct instances or not.
4666 if (ret_code
== LTTNG_OK
) {
4667 lttng_trigger_get(trigger
);
4668 *return_trigger
= trigger
;
4669 /* Ownership of trigger was transferred to caller. */
4677 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4678 const struct lttng_trigger
*trigger
)
4680 enum lttng_error_code ret_code
;
4681 const struct lttng_condition
*condition
=
4682 lttng_trigger_get_const_condition(trigger
);
4683 const enum lttng_domain_type trigger_domain
=
4684 lttng_trigger_get_underlying_domain_type_restriction(
4687 LTTNG_ASSERT(condition
);
4688 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4689 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4691 session_lock_list();
4692 switch (trigger_domain
) {
4693 case LTTNG_DOMAIN_KERNEL
:
4694 ret_code
= kernel_unregister_event_notifier(trigger
);
4695 if (ret_code
!= LTTNG_OK
) {
4696 goto end_unlock_session_list
;
4700 case LTTNG_DOMAIN_UST
:
4701 ust_app_global_update_all_event_notifier_rules();
4703 case LTTNG_DOMAIN_JUL
:
4704 case LTTNG_DOMAIN_LOG4J
:
4705 case LTTNG_DOMAIN_PYTHON
:
4707 /* Agent domains. */
4708 struct agent
*agt
= agent_find_by_event_notifier_domain(
4712 * This trigger was never registered in the first place. Calling
4713 * this function under those circumstances is an internal error.
4716 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4717 if (ret_code
!= LTTNG_OK
) {
4718 goto end_unlock_session_list
;
4723 case LTTNG_DOMAIN_NONE
:
4728 ret_code
= LTTNG_OK
;
4730 end_unlock_session_list
:
4731 session_unlock_list();
4735 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4736 const struct lttng_trigger
*trigger
,
4737 struct notification_thread_handle
*notification_thread
)
4739 enum lttng_error_code ret_code
;
4740 const char *trigger_name
;
4741 uid_t trigger_owner
;
4742 enum lttng_trigger_status trigger_status
;
4743 struct lttng_trigger
*sessiond_trigger
= NULL
;
4745 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4746 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4747 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4748 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4750 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4751 trigger_name
, (int) trigger_owner
,
4752 (int) lttng_credentials_get_uid(cmd_creds
));
4755 * Validate the trigger credentials against the command credentials.
4756 * Only the root user can unregister a trigger with non-matching
4759 if (!lttng_credentials_is_equal_uid(
4760 lttng_trigger_get_credentials(trigger
),
4762 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4763 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4764 trigger_name
, (int) trigger_owner
,
4765 (int) lttng_credentials_get_uid(cmd_creds
));
4766 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4771 /* Fetch the sessiond side trigger object. */
4772 ret_code
= notification_thread_command_get_trigger(
4773 notification_thread
, trigger
, &sessiond_trigger
);
4774 if (ret_code
!= LTTNG_OK
) {
4775 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4776 trigger_name
, (int) trigger_owner
, ret_code
);
4780 LTTNG_ASSERT(sessiond_trigger
);
4783 * From this point on, no matter what, consider the trigger
4786 * We set the unregistered state of the sessiond side trigger object in
4787 * the client thread since we want to minimize the possibility of the
4788 * notification thread being stalled due to a long execution of an
4789 * action that required the trigger lock.
4791 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4793 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4795 if (ret_code
!= LTTNG_OK
) {
4796 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4797 trigger_name
, (int) trigger_owner
, ret_code
);
4802 * Synchronize tracers if the trigger removes an event notifier.
4803 * Do this even if the trigger unregistration failed to at least stop
4804 * the tracers from producing notifications associated with this
4807 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4808 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4809 if (ret_code
!= LTTNG_OK
) {
4810 ERR("Error unregistering trigger to tracer.");
4817 lttng_trigger_put(sessiond_trigger
);
4821 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4822 struct notification_thread_handle
*notification_thread
,
4823 struct lttng_triggers
**return_triggers
)
4826 enum lttng_error_code ret_code
;
4827 struct lttng_triggers
*triggers
= NULL
;
4829 /* Get the set of triggers from the notification thread. */
4830 ret_code
= notification_thread_command_list_triggers(
4831 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4832 if (ret_code
!= LTTNG_OK
) {
4836 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4838 ret_code
= LTTNG_ERR_UNK
;
4842 *return_triggers
= triggers
;
4844 ret_code
= LTTNG_OK
;
4846 lttng_triggers_destroy(triggers
);
4850 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4851 const struct lttng_error_query
*query
,
4852 struct lttng_error_query_results
**_results
,
4853 struct notification_thread_handle
*notification_thread
)
4855 enum lttng_error_code ret_code
;
4856 const struct lttng_trigger
*query_target_trigger
;
4857 const struct lttng_action
*query_target_action
= NULL
;
4858 struct lttng_trigger
*matching_trigger
= NULL
;
4859 const char *trigger_name
;
4860 uid_t trigger_owner
;
4861 enum lttng_trigger_status trigger_status
;
4862 struct lttng_error_query_results
*results
= NULL
;
4864 switch (lttng_error_query_get_target_type(query
)) {
4865 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4866 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4868 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4869 query_target_trigger
=
4870 lttng_error_query_condition_borrow_target(query
);
4872 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4873 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4880 LTTNG_ASSERT(query_target_trigger
);
4882 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4883 query_target_trigger
, &matching_trigger
);
4884 if (ret_code
!= LTTNG_OK
) {
4888 /* No longer needed. */
4889 query_target_trigger
= NULL
;
4891 if (lttng_error_query_get_target_type(query
) ==
4892 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4893 /* Get the sessiond-side version of the target action. */
4894 query_target_action
=
4895 lttng_error_query_action_borrow_action_target(
4896 query
, matching_trigger
);
4899 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4900 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4901 trigger_name
: "(anonymous)";
4902 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4904 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4906 results
= lttng_error_query_results_create();
4908 ret_code
= LTTNG_ERR_NOMEM
;
4912 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4913 trigger_name
, (int) trigger_owner
,
4914 (int) lttng_credentials_get_uid(cmd_creds
));
4917 * Validate the trigger credentials against the command credentials.
4918 * Only the root user can target a trigger with non-matching
4921 if (!lttng_credentials_is_equal_uid(
4922 lttng_trigger_get_credentials(matching_trigger
),
4924 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4925 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4926 trigger_name
, (int) trigger_owner
,
4927 (int) lttng_credentials_get_uid(cmd_creds
));
4928 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4933 switch (lttng_error_query_get_target_type(query
)) {
4934 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4935 trigger_status
= lttng_trigger_add_error_results(
4936 matching_trigger
, results
);
4938 switch (trigger_status
) {
4939 case LTTNG_TRIGGER_STATUS_OK
:
4942 ret_code
= LTTNG_ERR_UNK
;
4947 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4949 trigger_status
= lttng_trigger_condition_add_error_results(
4950 matching_trigger
, results
);
4952 switch (trigger_status
) {
4953 case LTTNG_TRIGGER_STATUS_OK
:
4956 ret_code
= LTTNG_ERR_UNK
;
4962 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4964 const enum lttng_action_status action_status
=
4965 lttng_action_add_error_query_results(
4966 query_target_action
, results
);
4968 switch (action_status
) {
4969 case LTTNG_ACTION_STATUS_OK
:
4972 ret_code
= LTTNG_ERR_UNK
;
4983 *_results
= results
;
4985 ret_code
= LTTNG_OK
;
4987 lttng_trigger_put(matching_trigger
);
4988 lttng_error_query_results_destroy(results
);
4993 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4994 * snapshot output is *not* set with a remote destination.
4996 * Return LTTNG_OK on success or a LTTNG_ERR code.
4998 static enum lttng_error_code
set_relayd_for_snapshot(
4999 struct consumer_output
*output
,
5000 const struct ltt_session
*session
)
5002 enum lttng_error_code status
= LTTNG_OK
;
5003 struct lttng_ht_iter iter
;
5004 struct consumer_socket
*socket
;
5005 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
5006 const char *base_path
;
5008 LTTNG_ASSERT(output
);
5009 LTTNG_ASSERT(session
);
5011 DBG2("Set relayd object from snapshot output");
5013 if (session
->current_trace_chunk
) {
5014 enum lttng_trace_chunk_status chunk_status
=
5015 lttng_trace_chunk_get_id(
5016 session
->current_trace_chunk
,
5017 ¤t_chunk_id
.value
);
5019 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5020 current_chunk_id
.is_set
= true;
5022 ERR("Failed to get current trace chunk id");
5023 status
= LTTNG_ERR_UNK
;
5028 /* Ignore if snapshot consumer output is not network. */
5029 if (output
->type
!= CONSUMER_DST_NET
) {
5034 * The snapshot record URI base path overrides the session
5037 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5038 base_path
= output
->dst
.net
.control
.subdir
;
5040 base_path
= session
->base_path
;
5044 * For each consumer socket, create and send the relayd object of the
5048 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
5049 socket
, node
.node
) {
5050 pthread_mutex_lock(socket
->lock
);
5051 status
= send_consumer_relayd_sockets(session
->id
,
5053 session
->name
, session
->hostname
,
5055 session
->live_timer
,
5056 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
5057 session
->creation_time
,
5058 session
->name_contains_creation_time
);
5059 pthread_mutex_unlock(socket
->lock
);
5060 if (status
!= LTTNG_OK
) {
5072 * Record a kernel snapshot.
5074 * Return LTTNG_OK on success or a LTTNG_ERR code.
5076 static enum lttng_error_code
record_kernel_snapshot(
5077 struct ltt_kernel_session
*ksess
,
5078 const struct consumer_output
*output
,
5079 const struct ltt_session
*session
,
5080 uint64_t nb_packets_per_stream
)
5082 enum lttng_error_code status
;
5084 LTTNG_ASSERT(ksess
);
5085 LTTNG_ASSERT(output
);
5086 LTTNG_ASSERT(session
);
5088 status
= kernel_snapshot_record(
5089 ksess
, output
, nb_packets_per_stream
);
5094 * Record a UST snapshot.
5096 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5098 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5099 const struct consumer_output
*output
,
5100 const struct ltt_session
*session
,
5101 uint64_t nb_packets_per_stream
)
5103 enum lttng_error_code status
;
5105 LTTNG_ASSERT(usess
);
5106 LTTNG_ASSERT(output
);
5107 LTTNG_ASSERT(session
);
5109 status
= ust_app_snapshot_record(
5110 usess
, output
, nb_packets_per_stream
);
5115 uint64_t get_session_size_one_more_packet_per_stream(
5116 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5118 uint64_t tot_size
= 0;
5120 if (session
->kernel_session
) {
5121 struct ltt_kernel_channel
*chan
;
5122 const struct ltt_kernel_session
*ksess
=
5123 session
->kernel_session
;
5125 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5126 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5128 * Don't take channel into account if we
5129 * already grab all its packets.
5133 tot_size
+= chan
->channel
->attr
.subbuf_size
5134 * chan
->stream_count
;
5138 if (session
->ust_session
) {
5139 const struct ltt_ust_session
*usess
= session
->ust_session
;
5141 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5149 * Calculate the number of packets we can grab from each stream that
5150 * fits within the overall snapshot max size.
5152 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5153 * the number of packets per stream.
5155 * TODO: this approach is not perfect: we consider the worse case
5156 * (packet filling the sub-buffers) as an upper bound, but we could do
5157 * better if we do this calculation while we actually grab the packet
5158 * content: we would know how much padding we don't actually store into
5161 * This algorithm is currently bounded by the number of packets per
5164 * Since we call this algorithm before actually grabbing the data, it's
5165 * an approximation: for instance, applications could appear/disappear
5166 * in between this call and actually grabbing data.
5169 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5173 uint64_t cur_nb_packets
= 0;
5176 return 0; /* Infinite */
5179 size_left
= max_size
;
5181 uint64_t one_more_packet_tot_size
;
5183 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5184 session
, cur_nb_packets
);
5185 if (!one_more_packet_tot_size
) {
5186 /* We are already grabbing all packets. */
5189 size_left
-= one_more_packet_tot_size
;
5190 if (size_left
< 0) {
5195 if (!cur_nb_packets
&& size_left
!= max_size
) {
5196 /* Not enough room to grab one packet of each stream, error. */
5199 return cur_nb_packets
;
5203 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5204 const struct snapshot_output
*snapshot_output
)
5206 int64_t nb_packets_per_stream
;
5207 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5209 enum lttng_error_code ret_code
= LTTNG_OK
;
5210 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5211 struct consumer_output
*original_ust_consumer_output
= NULL
;
5212 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5213 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5214 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5216 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5218 snapshot_output
->name
,
5219 snapshot_output
->datetime
,
5220 snapshot_output
->nb_snapshot
);
5221 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5222 ERR("Failed to format snapshot name");
5223 ret_code
= LTTNG_ERR_INVALID
;
5226 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5227 snapshot_output
->name
, session
->name
,
5228 snapshot_chunk_name
);
5229 if (!session
->kernel_session
&& !session
->ust_session
) {
5230 ERR("Failed to record snapshot as no channels exist");
5231 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5235 if (session
->kernel_session
) {
5236 original_kernel_consumer_output
=
5237 session
->kernel_session
->consumer
;
5238 snapshot_kernel_consumer_output
=
5239 consumer_copy_output(snapshot_output
->consumer
);
5240 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5241 snapshot_chunk_name
);
5243 /* Copy the original domain subdir. */
5244 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5245 original_kernel_consumer_output
->domain_subdir
);
5247 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5248 original_kernel_consumer_output
);
5250 ERR("Failed to copy consumer sockets from snapshot output configuration");
5251 ret_code
= LTTNG_ERR_NOMEM
;
5254 ret_code
= set_relayd_for_snapshot(
5255 snapshot_kernel_consumer_output
, session
);
5256 if (ret_code
!= LTTNG_OK
) {
5257 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5260 session
->kernel_session
->consumer
=
5261 snapshot_kernel_consumer_output
;
5263 if (session
->ust_session
) {
5264 original_ust_consumer_output
= session
->ust_session
->consumer
;
5265 snapshot_ust_consumer_output
=
5266 consumer_copy_output(snapshot_output
->consumer
);
5267 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5268 snapshot_chunk_name
);
5270 /* Copy the original domain subdir. */
5271 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5272 original_ust_consumer_output
->domain_subdir
);
5274 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5275 original_ust_consumer_output
);
5277 ERR("Failed to copy consumer sockets from snapshot output configuration");
5278 ret_code
= LTTNG_ERR_NOMEM
;
5281 ret_code
= set_relayd_for_snapshot(
5282 snapshot_ust_consumer_output
, session
);
5283 if (ret_code
!= LTTNG_OK
) {
5284 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5287 session
->ust_session
->consumer
=
5288 snapshot_ust_consumer_output
;
5291 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5292 snapshot_kernel_consumer_output
?:
5293 snapshot_ust_consumer_output
,
5294 consumer_output_get_base_path(
5295 snapshot_output
->consumer
),
5296 snapshot_chunk_name
);
5297 if (!snapshot_trace_chunk
) {
5298 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5300 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5303 LTTNG_ASSERT(!session
->current_trace_chunk
);
5304 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5305 lttng_trace_chunk_put(snapshot_trace_chunk
);
5306 snapshot_trace_chunk
= NULL
;
5308 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5310 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5314 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5315 snapshot_output
->max_size
);
5316 if (nb_packets_per_stream
< 0) {
5317 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5318 goto error_close_trace_chunk
;
5321 if (session
->kernel_session
) {
5322 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5323 snapshot_kernel_consumer_output
, session
,
5324 nb_packets_per_stream
);
5325 if (ret_code
!= LTTNG_OK
) {
5326 goto error_close_trace_chunk
;
5330 if (session
->ust_session
) {
5331 ret_code
= record_ust_snapshot(session
->ust_session
,
5332 snapshot_ust_consumer_output
, session
,
5333 nb_packets_per_stream
);
5334 if (ret_code
!= LTTNG_OK
) {
5335 goto error_close_trace_chunk
;
5339 error_close_trace_chunk
:
5340 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5341 ERR("Failed to release the current trace chunk of session \"%s\"",
5343 ret_code
= LTTNG_ERR_UNK
;
5346 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5347 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5349 * Don't goto end; make sure the chunk is closed for the session
5350 * to allow future snapshots.
5352 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5354 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5357 lttng_trace_chunk_put(snapshot_trace_chunk
);
5358 snapshot_trace_chunk
= NULL
;
5360 if (original_ust_consumer_output
) {
5361 session
->ust_session
->consumer
= original_ust_consumer_output
;
5363 if (original_kernel_consumer_output
) {
5364 session
->kernel_session
->consumer
=
5365 original_kernel_consumer_output
;
5367 consumer_output_put(snapshot_ust_consumer_output
);
5368 consumer_output_put(snapshot_kernel_consumer_output
);
5373 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5375 * The wait parameter is ignored so this call always wait for the snapshot to
5376 * complete before returning.
5378 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5380 int cmd_snapshot_record(struct ltt_session
*session
,
5381 const struct lttng_snapshot_output
*output
,
5382 int wait
__attribute__((unused
)))
5384 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5386 unsigned int snapshot_success
= 0;
5388 struct snapshot_output
*tmp_output
= NULL
;
5390 LTTNG_ASSERT(session
);
5391 LTTNG_ASSERT(output
);
5393 DBG("Cmd snapshot record for session %s", session
->name
);
5395 /* Get the datetime for the snapshot output directory. */
5396 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5399 cmd_ret
= LTTNG_ERR_INVALID
;
5404 * Permission denied to create an output if the session is not
5405 * set in no output mode.
5407 if (session
->output_traces
) {
5408 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5412 /* The session needs to be started at least once. */
5413 if (!session
->has_been_started
) {
5414 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5418 /* Use temporary output for the session. */
5419 if (*output
->ctrl_url
!= '\0') {
5420 tmp_output
= snapshot_output_alloc();
5422 cmd_ret
= LTTNG_ERR_NOMEM
;
5426 ret
= snapshot_output_init(session
, output
->max_size
,
5428 output
->ctrl_url
, output
->data_url
,
5432 if (ret
== -ENOMEM
) {
5433 cmd_ret
= LTTNG_ERR_NOMEM
;
5435 cmd_ret
= LTTNG_ERR_INVALID
;
5439 /* Use the global session count for the temporary snapshot. */
5440 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5442 /* Use the global datetime */
5443 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5444 cmd_ret
= snapshot_record(session
, tmp_output
);
5445 if (cmd_ret
!= LTTNG_OK
) {
5448 snapshot_success
= 1;
5450 struct snapshot_output
*sout
;
5451 struct lttng_ht_iter iter
;
5454 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5455 &iter
.iter
, sout
, node
.node
) {
5456 struct snapshot_output output_copy
;
5459 * Make a local copy of the output and override output
5460 * parameters with those provided as part of the
5463 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5465 if (output
->max_size
!= (uint64_t) -1ULL) {
5466 output_copy
.max_size
= output
->max_size
;
5469 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5470 memcpy(output_copy
.datetime
, datetime
,
5473 /* Use temporary name. */
5474 if (*output
->name
!= '\0') {
5475 if (lttng_strncpy(output_copy
.name
,
5477 sizeof(output_copy
.name
))) {
5478 cmd_ret
= LTTNG_ERR_INVALID
;
5484 cmd_ret
= snapshot_record(session
, &output_copy
);
5485 if (cmd_ret
!= LTTNG_OK
) {
5489 snapshot_success
= 1;
5494 if (snapshot_success
) {
5495 session
->snapshot
.nb_snapshot
++;
5497 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5502 snapshot_output_destroy(tmp_output
);
5508 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5510 int cmd_set_session_shm_path(struct ltt_session
*session
,
5511 const char *shm_path
)
5514 LTTNG_ASSERT(session
);
5517 * Can only set shm path before session is started.
5519 if (session
->has_been_started
) {
5520 return LTTNG_ERR_SESSION_STARTED
;
5523 strncpy(session
->shm_path
, shm_path
,
5524 sizeof(session
->shm_path
));
5525 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5531 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5533 * Ask the consumer to rotate the session output directory.
5534 * The session lock must be held.
5536 * Returns LTTNG_OK on success or else a negative LTTng error code.
5538 int cmd_rotate_session(struct ltt_session
*session
,
5539 struct lttng_rotate_session_return
*rotate_return
,
5540 bool quiet_rotation
,
5541 enum lttng_trace_chunk_command_type command
)
5544 uint64_t ongoing_rotation_chunk_id
;
5545 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5546 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5547 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5548 enum lttng_trace_chunk_status chunk_status
;
5549 bool failed_to_rotate
= false;
5550 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5552 LTTNG_ASSERT(session
);
5554 if (!session
->has_been_started
) {
5555 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5560 * Explicit rotation is not supported for live sessions.
5561 * However, live sessions can perform a quiet rotation on
5563 * Rotation is not supported for snapshot traces (no output).
5565 if ((!quiet_rotation
&& session
->live_timer
) ||
5566 !session
->output_traces
) {
5567 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5571 /* Unsupported feature in lttng-relayd before 2.11. */
5572 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5573 (session
->consumer
->relay_major_version
== 2 &&
5574 session
->consumer
->relay_minor_version
< 11)) {
5575 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5579 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5580 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5581 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5585 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5586 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5588 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5593 * After a stop, we only allow one rotation to occur, the other ones are
5594 * useless until a new start.
5596 if (session
->rotated_after_last_stop
) {
5597 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5599 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5604 * After a stop followed by a clear, disallow following rotations a they would
5605 * generate empty chunks.
5607 if (session
->cleared_after_last_stop
) {
5608 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5610 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5614 if (session
->active
) {
5615 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5617 if (!new_trace_chunk
) {
5618 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5624 * The current trace chunk becomes the chunk being archived.
5626 * After this point, "chunk_being_archived" must absolutely
5627 * be closed on the consumer(s), otherwise it will never be
5628 * cleaned-up, which will result in a leak.
5630 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5631 &chunk_being_archived
);
5633 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5637 if (session
->kernel_session
) {
5638 cmd_ret
= kernel_rotate_session(session
);
5639 if (cmd_ret
!= LTTNG_OK
) {
5640 failed_to_rotate
= true;
5641 rotation_fail_code
= cmd_ret
;
5644 if (session
->ust_session
) {
5645 cmd_ret
= ust_app_rotate_session(session
);
5646 if (cmd_ret
!= LTTNG_OK
) {
5647 failed_to_rotate
= true;
5648 rotation_fail_code
= cmd_ret
;
5652 if (!session
->active
) {
5653 session
->rotated_after_last_stop
= true;
5656 if (!chunk_being_archived
) {
5657 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5659 if (failed_to_rotate
) {
5660 cmd_ret
= rotation_fail_code
;
5667 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5668 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5669 &ongoing_rotation_chunk_id
);
5670 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5672 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5673 command
, session
->last_chunk_path
);
5675 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5679 if (failed_to_rotate
) {
5680 cmd_ret
= rotation_fail_code
;
5684 session
->quiet_rotation
= quiet_rotation
;
5685 ret
= timer_session_rotation_pending_check_start(session
,
5686 DEFAULT_ROTATE_PENDING_TIMER
);
5688 cmd_ret
= LTTNG_ERR_UNK
;
5692 if (rotate_return
) {
5693 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5696 session
->chunk_being_archived
= chunk_being_archived
;
5697 chunk_being_archived
= NULL
;
5698 if (!quiet_rotation
) {
5699 ret
= notification_thread_command_session_rotation_ongoing(
5700 the_notification_thread_handle
, session
->name
,
5701 session
->uid
, session
->gid
,
5702 ongoing_rotation_chunk_id
);
5703 if (ret
!= LTTNG_OK
) {
5704 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5706 cmd_ret
= (lttng_error_code
) ret
;
5710 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5711 session
->name
, ongoing_rotation_chunk_id
);
5713 lttng_trace_chunk_put(new_trace_chunk
);
5714 lttng_trace_chunk_put(chunk_being_archived
);
5715 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5718 if (session_reset_rotation_state(session
,
5719 LTTNG_ROTATION_STATE_ERROR
)) {
5720 ERR("Failed to reset rotation state of session \"%s\"",
5727 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5729 * Check if the session has finished its rotation.
5731 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5733 int cmd_rotate_get_info(struct ltt_session
*session
,
5734 struct lttng_rotation_get_info_return
*info_return
,
5735 uint64_t rotation_id
)
5737 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5738 enum lttng_rotation_state rotation_state
;
5740 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5741 session
->most_recent_chunk_id
.value
);
5743 if (session
->chunk_being_archived
) {
5744 enum lttng_trace_chunk_status chunk_status
;
5747 chunk_status
= lttng_trace_chunk_get_id(
5748 session
->chunk_being_archived
,
5750 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5752 rotation_state
= rotation_id
== chunk_id
?
5753 LTTNG_ROTATION_STATE_ONGOING
:
5754 LTTNG_ROTATION_STATE_EXPIRED
;
5756 if (session
->last_archived_chunk_id
.is_set
&&
5757 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5758 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5760 rotation_state
= session
->rotation_state
;
5764 switch (rotation_state
) {
5765 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5766 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5769 case LTTNG_ROTATION_STATE_EXPIRED
:
5770 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5771 rotation_id
, session
->name
);
5773 case LTTNG_ROTATION_STATE_ONGOING
:
5774 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5775 rotation_id
, session
->name
);
5777 case LTTNG_ROTATION_STATE_COMPLETED
:
5781 char *current_tracing_path_reply
;
5782 size_t current_tracing_path_reply_len
;
5784 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5785 rotation_id
, session
->name
);
5787 switch (session_get_consumer_destination_type(session
)) {
5788 case CONSUMER_DST_LOCAL
:
5789 current_tracing_path_reply
=
5790 info_return
->location
.local
.absolute_path
;
5791 current_tracing_path_reply_len
=
5792 sizeof(info_return
->location
.local
.absolute_path
);
5793 info_return
->location_type
=
5794 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5795 fmt_ret
= asprintf(&chunk_path
,
5796 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5797 session_get_base_path(session
),
5798 session
->last_archived_chunk_name
);
5799 if (fmt_ret
== -1) {
5800 PERROR("Failed to format the path of the last archived trace chunk");
5801 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5802 cmd_ret
= LTTNG_ERR_UNK
;
5806 case CONSUMER_DST_NET
:
5808 uint16_t ctrl_port
, data_port
;
5810 current_tracing_path_reply
=
5811 info_return
->location
.relay
.relative_path
;
5812 current_tracing_path_reply_len
=
5813 sizeof(info_return
->location
.relay
.relative_path
);
5814 /* Currently the only supported relay protocol. */
5815 info_return
->location
.relay
.protocol
=
5816 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5818 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5819 session_get_net_consumer_hostname(session
),
5820 sizeof(info_return
->location
.relay
.host
));
5822 ERR("Failed to copy host name to rotate_get_info reply");
5823 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5824 cmd_ret
= LTTNG_ERR_SET_URL
;
5828 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5829 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5830 info_return
->location
.relay
.ports
.data
= data_port
;
5831 info_return
->location_type
=
5832 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5833 chunk_path
= strdup(session
->last_chunk_path
);
5835 ERR("Failed to allocate the path of the last archived trace chunk");
5836 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5837 cmd_ret
= LTTNG_ERR_UNK
;
5846 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5847 chunk_path
, current_tracing_path_reply_len
);
5850 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5851 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5852 cmd_ret
= LTTNG_ERR_UNK
;
5858 case LTTNG_ROTATION_STATE_ERROR
:
5859 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5860 rotation_id
, session
->name
);
5868 info_return
->status
= (int32_t) rotation_state
;
5873 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5875 * Configure the automatic rotation parameters.
5876 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5877 * 'activate' to false means deactivate the rotation schedule and validate that
5878 * 'new_value' has the same value as the currently active value.
5880 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5882 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5883 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5885 struct notification_thread_handle
*notification_thread_handle
)
5888 uint64_t *parameter_value
;
5890 LTTNG_ASSERT(session
);
5892 DBG("Cmd rotate set schedule session %s", session
->name
);
5894 if (session
->live_timer
|| !session
->output_traces
) {
5895 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5896 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5900 switch (schedule_type
) {
5901 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5902 parameter_value
= &session
->rotate_size
;
5904 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5905 parameter_value
= &session
->rotate_timer_period
;
5906 if (new_value
>= UINT_MAX
) {
5907 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5908 new_value
, UINT_MAX
);
5909 ret
= LTTNG_ERR_INVALID
;
5914 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5915 ret
= LTTNG_ERR_INVALID
;
5919 /* Improper use of the API. */
5920 if (new_value
== -1ULL) {
5921 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5922 ret
= LTTNG_ERR_INVALID
;
5927 * As indicated in struct ltt_session's comments, a value of == 0 means
5928 * this schedule rotation type is not in use.
5930 * Reject the command if we were asked to activate a schedule that was
5933 if (activate
&& *parameter_value
!= 0) {
5934 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5935 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5940 * Reject the command if we were asked to deactivate a schedule that was
5943 if (!activate
&& *parameter_value
== 0) {
5944 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5945 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5950 * Reject the command if we were asked to deactivate a schedule that
5953 if (!activate
&& *parameter_value
!= new_value
) {
5954 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5955 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5959 *parameter_value
= activate
? new_value
: 0;
5961 switch (schedule_type
) {
5962 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5963 if (activate
&& session
->active
) {
5965 * Only start the timer if the session is active,
5966 * otherwise it will be started when the session starts.
5968 ret
= timer_session_rotation_schedule_timer_start(
5969 session
, new_value
);
5971 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5972 ret
= LTTNG_ERR_UNK
;
5976 ret
= timer_session_rotation_schedule_timer_stop(
5979 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5980 ret
= LTTNG_ERR_UNK
;
5985 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5987 ret
= subscribe_session_consumed_size_rotation(session
,
5988 new_value
, notification_thread_handle
);
5990 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5991 ret
= LTTNG_ERR_UNK
;
5995 ret
= unsubscribe_session_consumed_size_rotation(session
,
5996 notification_thread_handle
);
5998 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5999 ret
= LTTNG_ERR_UNK
;
6006 /* Would have been caught before. */
6018 /* Wait for a given path to be removed before continuing. */
6019 static enum lttng_error_code
wait_on_path(void *path_data
)
6021 const char *shm_path
= (const char *) path_data
;
6023 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6029 ret
= stat(shm_path
, &st
);
6031 if (errno
!= ENOENT
) {
6032 PERROR("stat() returned an error while checking for the existence of the shm path");
6034 DBG("shm path no longer exists, completing the destruction of session");
6038 if (!S_ISDIR(st
.st_mode
)) {
6039 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6044 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6050 * Returns a pointer to a handler to run on completion of a command.
6051 * Returns NULL if no handler has to be run for the last command executed.
6053 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6055 struct cmd_completion_handler
*handler
= current_completion_handler
;
6057 current_completion_handler
= NULL
;
6062 * Init command subsystem.
6067 * Set network sequence index to 1 for streams to match a relayd
6068 * socket on the consumer side.
6070 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6071 relayd_net_seq_idx
= 1;
6072 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6074 DBG("Command subsystem initialized");