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 static enum lttng_error_code
wait_on_path(void *path
);
80 struct cmd_destroy_session_reply_context
{
82 bool implicit_rotation_on_destroy
;
84 * Indicates whether or not an error occurred while launching the
85 * destruction of a session.
87 enum lttng_error_code destruction_status
;
91 * Command completion handler that is used by the destroy command
92 * when a session that has a non-default shm_path is being destroyed.
94 * See comment in cmd_destroy_session() for the rationale.
96 struct destroy_completion_handler
{
97 struct cmd_completion_handler handler
;
98 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
99 } destroy_completion_handler
= {
102 .data
= destroy_completion_handler
.shm_path
108 * Used to keep a unique index for each relayd socket created where this value
109 * is associated with streams on the consumer so it can match the right relayd
110 * to send to. It must be accessed with the relayd_net_seq_idx_lock
113 pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
114 uint64_t relayd_net_seq_idx
;
117 static struct cmd_completion_handler
*current_completion_handler
;
118 static int validate_ust_event_name(const char *);
119 static int cmd_enable_event_internal(struct ltt_session
*session
,
120 const struct lttng_domain
*domain
,
121 char *channel_name
, struct lttng_event
*event
,
122 char *filter_expression
,
123 struct lttng_bytecode
*filter
,
124 struct lttng_event_exclusion
*exclusion
,
126 static enum lttng_error_code
cmd_enable_channel_internal(
127 struct ltt_session
*session
,
128 const struct lttng_domain
*domain
,
129 const struct lttng_channel
*_attr
,
133 * Create a session path used by list_lttng_sessions for the case that the
134 * session consumer is on the network.
136 static int build_network_session_path(char *dst
, size_t size
,
137 struct ltt_session
*session
)
139 int ret
, kdata_port
, udata_port
;
140 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
141 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
143 LTTNG_ASSERT(session
);
146 memset(tmp_urls
, 0, sizeof(tmp_urls
));
147 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
149 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
151 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
152 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
153 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
156 if (session
->ust_session
&& session
->ust_session
->consumer
) {
157 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
158 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
161 if (uuri
== NULL
&& kuri
== NULL
) {
162 uri
= &session
->consumer
->dst
.net
.control
;
163 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
164 } else if (kuri
&& uuri
) {
165 ret
= uri_compare(kuri
, uuri
);
169 /* Build uuri URL string */
170 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
177 } else if (kuri
&& uuri
== NULL
) {
179 } else if (uuri
&& kuri
== NULL
) {
183 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
189 * Do we have a UST url set. If yes, this means we have both kernel and UST
192 if (*tmp_uurl
!= '\0') {
193 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
194 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
197 if (kuri
|| (!kuri
&& !uuri
)) {
200 /* No kernel URI, use the UST port. */
203 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
211 * Get run-time attributes if the session has been started (discarded events,
214 static int get_kernel_runtime_stats(struct ltt_session
*session
,
215 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
216 uint64_t *lost_packets
)
220 if (!session
->has_been_started
) {
222 *discarded_events
= 0;
227 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
228 session
->kernel_session
->consumer
,
234 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
235 session
->kernel_session
->consumer
,
246 * Get run-time attributes if the session has been started (discarded events,
249 static int get_ust_runtime_stats(struct ltt_session
*session
,
250 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
251 uint64_t *lost_packets
)
254 struct ltt_ust_session
*usess
;
256 if (!discarded_events
|| !lost_packets
) {
261 usess
= session
->ust_session
;
262 LTTNG_ASSERT(discarded_events
);
263 LTTNG_ASSERT(lost_packets
);
265 if (!usess
|| !session
->has_been_started
) {
266 *discarded_events
= 0;
272 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
273 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
274 &usess
->buffer_reg_uid_list
,
275 usess
->consumer
, uchan
->id
,
276 uchan
->attr
.overwrite
,
279 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
280 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
281 uchan
, usess
->consumer
,
282 uchan
->attr
.overwrite
,
288 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
289 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
291 ERR("Unsupported buffer type");
302 * Create a list of agent domain events.
304 * Return number of events in list on success or else a negative value.
306 static enum lttng_error_code
list_lttng_agent_events(
307 struct agent
*agt
, struct lttng_payload
*reply_payload
,
308 unsigned int *nb_events
)
310 enum lttng_error_code ret_code
;
312 unsigned int local_nb_events
= 0;
313 struct agent_event
*event
;
314 struct lttng_ht_iter iter
;
315 unsigned long agent_event_count
;
318 assert(reply_payload
);
320 DBG3("Listing agent events");
323 agent_event_count
= lttng_ht_get_count(agt
->events
);
324 if (agent_event_count
== 0) {
329 if (agent_event_count
> UINT_MAX
) {
330 ret_code
= LTTNG_ERR_OVERFLOW
;
334 local_nb_events
= (unsigned int) agent_event_count
;
336 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
337 struct lttng_event
*tmp_event
= lttng_event_create();
340 ret_code
= LTTNG_ERR_NOMEM
;
344 if (lttng_strncpy(tmp_event
->name
, event
->name
, sizeof(tmp_event
->name
))) {
345 lttng_event_destroy(tmp_event
);
346 ret_code
= LTTNG_ERR_FATAL
;
350 tmp_event
->name
[sizeof(tmp_event
->name
) - 1] = '\0';
351 tmp_event
->enabled
= !!event
->enabled_count
;
352 tmp_event
->loglevel
= event
->loglevel_value
;
353 tmp_event
->loglevel_type
= event
->loglevel_type
;
355 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
356 event
->filter_expression
, 0, NULL
, reply_payload
);
357 lttng_event_destroy(tmp_event
);
359 ret_code
= LTTNG_ERR_FATAL
;
366 *nb_events
= local_nb_events
;
373 * Create a list of ust global domain events.
375 static enum lttng_error_code
list_lttng_ust_global_events(char *channel_name
,
376 struct ltt_ust_domain_global
*ust_global
,
377 struct lttng_payload
*reply_payload
,
378 unsigned int *nb_events
)
380 enum lttng_error_code ret_code
;
382 struct lttng_ht_iter iter
;
383 struct lttng_ht_node_str
*node
;
384 struct ltt_ust_channel
*uchan
;
385 struct ltt_ust_event
*uevent
;
386 unsigned long channel_event_count
;
387 unsigned int local_nb_events
= 0;
389 assert(reply_payload
);
392 DBG("Listing UST global events for channel %s", channel_name
);
396 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
397 node
= lttng_ht_iter_get_node_str(&iter
);
399 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
403 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
405 channel_event_count
= lttng_ht_get_count(uchan
->events
);
406 if (channel_event_count
== 0) {
412 if (channel_event_count
> UINT_MAX
) {
413 ret_code
= LTTNG_ERR_OVERFLOW
;
417 local_nb_events
= (unsigned int) channel_event_count
;
419 DBG3("Listing UST global %d events", *nb_events
);
421 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
422 struct lttng_event
*tmp_event
= NULL
;
424 if (uevent
->internal
) {
425 /* This event should remain hidden from clients */
430 tmp_event
= lttng_event_create();
432 ret_code
= LTTNG_ERR_NOMEM
;
436 if (lttng_strncpy(tmp_event
->name
, uevent
->attr
.name
,
437 LTTNG_SYMBOL_NAME_LEN
)) {
438 ret_code
= LTTNG_ERR_FATAL
;
439 lttng_event_destroy(tmp_event
);
443 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
444 tmp_event
->enabled
= uevent
->enabled
;
446 switch (uevent
->attr
.instrumentation
) {
447 case LTTNG_UST_ABI_TRACEPOINT
:
448 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
450 case LTTNG_UST_ABI_PROBE
:
451 tmp_event
->type
= LTTNG_EVENT_PROBE
;
453 case LTTNG_UST_ABI_FUNCTION
:
454 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
458 tmp_event
->loglevel
= uevent
->attr
.loglevel
;
459 switch (uevent
->attr
.loglevel_type
) {
460 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
461 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
463 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
464 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
466 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
467 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
470 if (uevent
->filter
) {
471 tmp_event
->filter
= 1;
473 if (uevent
->exclusion
) {
474 tmp_event
->exclusion
= 1;
478 * We do not care about the filter bytecode and the fd from the
479 * userspace_probe_location.
481 ret
= lttng_event_serialize(tmp_event
, uevent
->exclusion
? uevent
->exclusion
->count
: 0,
482 uevent
->exclusion
? (char **) uevent
->exclusion
->names
: NULL
,
483 uevent
->filter_expression
, 0, NULL
, reply_payload
);
484 lttng_event_destroy(tmp_event
);
486 ret_code
= LTTNG_ERR_FATAL
;
492 /* nb_events is already set at this point. */
494 *nb_events
= local_nb_events
;
501 * Fill lttng_event array of all kernel events in the channel.
503 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
504 struct ltt_kernel_session
*kernel_session
,
505 struct lttng_payload
*reply_payload
,
506 unsigned int *nb_events
)
508 enum lttng_error_code ret_code
;
510 struct ltt_kernel_event
*event
;
511 struct ltt_kernel_channel
*kchan
;
513 assert(reply_payload
);
515 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
517 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
521 *nb_events
= kchan
->event_count
;
523 DBG("Listing events for channel %s", kchan
->channel
->name
);
525 if (*nb_events
== 0) {
530 /* Kernel channels */
531 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
532 struct lttng_event
*tmp_event
= lttng_event_create();
535 ret_code
= LTTNG_ERR_NOMEM
;
539 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
540 lttng_event_destroy(tmp_event
);
541 ret_code
= LTTNG_ERR_FATAL
;
546 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
547 tmp_event
->enabled
= event
->enabled
;
548 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
550 switch (event
->event
->instrumentation
) {
551 case LTTNG_KERNEL_ABI_TRACEPOINT
:
552 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
554 case LTTNG_KERNEL_ABI_KRETPROBE
:
555 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
556 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
557 sizeof(struct lttng_kernel_abi_kprobe
));
559 case LTTNG_KERNEL_ABI_KPROBE
:
560 tmp_event
->type
= LTTNG_EVENT_PROBE
;
561 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
562 sizeof(struct lttng_kernel_abi_kprobe
));
564 case LTTNG_KERNEL_ABI_UPROBE
:
565 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
567 case LTTNG_KERNEL_ABI_FUNCTION
:
568 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
569 memcpy(&(tmp_event
->attr
.ftrace
), &event
->event
->u
.ftrace
,
570 sizeof(struct lttng_kernel_abi_function
));
572 case LTTNG_KERNEL_ABI_NOOP
:
573 tmp_event
->type
= LTTNG_EVENT_NOOP
;
575 case LTTNG_KERNEL_ABI_SYSCALL
:
576 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
578 case LTTNG_KERNEL_ABI_ALL
:
585 if (event
->userspace_probe_location
) {
586 struct lttng_userspace_probe_location
*location_copy
=
587 lttng_userspace_probe_location_copy(
588 event
->userspace_probe_location
);
590 if (!location_copy
) {
591 lttng_event_destroy(tmp_event
);
592 ret_code
= LTTNG_ERR_NOMEM
;
596 ret
= lttng_event_set_userspace_probe_location(
597 tmp_event
, location_copy
);
599 lttng_event_destroy(tmp_event
);
600 lttng_userspace_probe_location_destroy(
602 ret_code
= LTTNG_ERR_INVALID
;
607 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
608 event
->filter_expression
, 0, NULL
, reply_payload
);
609 lttng_event_destroy(tmp_event
);
611 ret_code
= LTTNG_ERR_FATAL
;
622 * Add URI so the consumer output object. Set the correct path depending on the
623 * domain adding the default trace directory.
625 static enum lttng_error_code
add_uri_to_consumer(
626 const struct ltt_session
*session
,
627 struct consumer_output
*consumer
,
628 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
631 enum lttng_error_code ret_code
= LTTNG_OK
;
635 if (consumer
== NULL
) {
636 DBG("No consumer detected. Don't add URI. Stopping.");
637 ret_code
= LTTNG_ERR_NO_CONSUMER
;
642 case LTTNG_DOMAIN_KERNEL
:
643 ret
= lttng_strncpy(consumer
->domain_subdir
,
644 DEFAULT_KERNEL_TRACE_DIR
,
645 sizeof(consumer
->domain_subdir
));
647 case LTTNG_DOMAIN_UST
:
648 ret
= lttng_strncpy(consumer
->domain_subdir
,
649 DEFAULT_UST_TRACE_DIR
,
650 sizeof(consumer
->domain_subdir
));
654 * This case is possible is we try to add the URI to the global
655 * tracing session consumer object which in this case there is
658 memset(consumer
->domain_subdir
, 0,
659 sizeof(consumer
->domain_subdir
));
663 ERR("Failed to initialize consumer output domain subdirectory");
664 ret_code
= LTTNG_ERR_FATAL
;
668 switch (uri
->dtype
) {
671 DBG2("Setting network URI to consumer");
673 if (consumer
->type
== CONSUMER_DST_NET
) {
674 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
675 consumer
->dst
.net
.control_isset
) ||
676 (uri
->stype
== LTTNG_STREAM_DATA
&&
677 consumer
->dst
.net
.data_isset
)) {
678 ret_code
= LTTNG_ERR_URL_EXIST
;
682 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
685 /* Set URI into consumer output object */
686 ret
= consumer_set_network_uri(session
, consumer
, uri
);
688 ret_code
= (lttng_error_code
) -ret
;
690 } else if (ret
== 1) {
692 * URI was the same in the consumer so we do not append the subdir
693 * again so to not duplicate output dir.
700 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
701 ret_code
= LTTNG_ERR_INVALID
;
704 DBG2("Setting trace directory path from URI to %s",
706 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
708 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
710 sizeof(consumer
->dst
.session_root_path
));
712 ret_code
= LTTNG_ERR_FATAL
;
715 consumer
->type
= CONSUMER_DST_LOCAL
;
725 * Init tracing by creating trace directory and sending fds kernel consumer.
727 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
730 struct lttng_ht_iter iter
;
731 struct consumer_socket
*socket
;
733 LTTNG_ASSERT(session
);
737 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
738 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
740 pthread_mutex_lock(socket
->lock
);
741 ret
= kernel_consumer_send_session(socket
, session
);
742 pthread_mutex_unlock(socket
->lock
);
744 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
756 * Create a socket to the relayd using the URI.
758 * On success, the relayd_sock pointer is set to the created socket.
759 * Else, it remains untouched and an LTTng error code is returned.
761 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
762 struct lttcomm_relayd_sock
**relayd_sock
,
763 struct consumer_output
*consumer
)
766 enum lttng_error_code status
= LTTNG_OK
;
767 struct lttcomm_relayd_sock
*rsock
;
769 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
770 RELAYD_VERSION_COMM_MINOR
);
772 status
= LTTNG_ERR_FATAL
;
777 * Connect to relayd so we can proceed with a session creation. This call
778 * can possibly block for an arbitrary amount of time to set the health
779 * state to be in poll execution.
782 ret
= relayd_connect(rsock
);
785 ERR("Unable to reach lttng-relayd");
786 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
790 /* Create socket for control stream. */
791 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
792 uint64_t result_flags
;
794 DBG3("Creating relayd stream socket from URI");
796 /* Check relayd version */
797 ret
= relayd_version_check(rsock
);
798 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
799 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
801 } else if (ret
< 0) {
802 ERR("Unable to reach lttng-relayd");
803 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
806 consumer
->relay_major_version
= rsock
->major
;
807 consumer
->relay_minor_version
= rsock
->minor
;
808 ret
= relayd_get_configuration(rsock
, 0,
811 ERR("Unable to get relayd configuration");
812 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
815 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
816 consumer
->relay_allows_clear
= true;
818 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
819 DBG3("Creating relayd data socket from URI");
821 /* Command is not valid */
822 ERR("Relayd invalid stream type: %d", uri
->stype
);
823 status
= LTTNG_ERR_INVALID
;
827 *relayd_sock
= rsock
;
832 /* The returned value is not useful since we are on an error path. */
833 (void) relayd_close(rsock
);
841 * Connect to the relayd using URI and send the socket to the right consumer.
843 * The consumer socket lock must be held by the caller.
845 * Returns LTTNG_OK on success or an LTTng error code on failure.
847 static enum lttng_error_code
send_consumer_relayd_socket(
848 unsigned int session_id
,
849 struct lttng_uri
*relayd_uri
,
850 struct consumer_output
*consumer
,
851 struct consumer_socket
*consumer_sock
,
852 const char *session_name
, const char *hostname
,
853 const char *base_path
, int session_live_timer
,
854 const uint64_t *current_chunk_id
,
855 time_t session_creation_time
,
856 bool session_name_contains_creation_time
)
859 struct lttcomm_relayd_sock
*rsock
= NULL
;
860 enum lttng_error_code status
;
862 /* Connect to relayd and make version check if uri is the control. */
863 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
864 if (status
!= LTTNG_OK
) {
865 goto relayd_comm_error
;
869 /* Set the network sequence index if not set. */
870 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
871 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
873 * Increment net_seq_idx because we are about to transfer the
874 * new relayd socket to the consumer.
875 * Assign unique key so the consumer can match streams.
877 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
878 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
881 /* Send relayd socket to consumer. */
882 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
883 relayd_uri
->stype
, session_id
,
884 session_name
, hostname
, base_path
,
885 session_live_timer
, current_chunk_id
,
886 session_creation_time
, session_name_contains_creation_time
);
888 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
892 /* Flag that the corresponding socket was sent. */
893 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
894 consumer_sock
->control_sock_sent
= 1;
895 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
896 consumer_sock
->data_sock_sent
= 1;
900 * Close socket which was dup on the consumer side. The session daemon does
901 * NOT keep track of the relayd socket(s) once transfer to the consumer.
905 if (status
!= LTTNG_OK
) {
907 * The consumer output for this session should not be used anymore
908 * since the relayd connection failed thus making any tracing or/and
909 * streaming not usable.
911 consumer
->enabled
= 0;
913 (void) relayd_close(rsock
);
921 * Send both relayd sockets to a specific consumer and domain. This is a
922 * helper function to facilitate sending the information to the consumer for a
925 * The consumer socket lock must be held by the caller.
927 * Returns LTTNG_OK, or an LTTng error code on failure.
929 static enum lttng_error_code
send_consumer_relayd_sockets(
930 unsigned int session_id
, struct consumer_output
*consumer
,
931 struct consumer_socket
*sock
, const char *session_name
,
932 const char *hostname
, const char *base_path
, int session_live_timer
,
933 const uint64_t *current_chunk_id
, time_t session_creation_time
,
934 bool session_name_contains_creation_time
)
936 enum lttng_error_code status
= LTTNG_OK
;
938 LTTNG_ASSERT(consumer
);
941 /* Sending control relayd socket. */
942 if (!sock
->control_sock_sent
) {
943 status
= send_consumer_relayd_socket(session_id
,
944 &consumer
->dst
.net
.control
, consumer
, sock
,
945 session_name
, hostname
, base_path
, session_live_timer
,
946 current_chunk_id
, session_creation_time
,
947 session_name_contains_creation_time
);
948 if (status
!= LTTNG_OK
) {
953 /* Sending data relayd socket. */
954 if (!sock
->data_sock_sent
) {
955 status
= send_consumer_relayd_socket(session_id
,
956 &consumer
->dst
.net
.data
, consumer
, sock
,
957 session_name
, hostname
, base_path
, session_live_timer
,
958 current_chunk_id
, session_creation_time
,
959 session_name_contains_creation_time
);
960 if (status
!= LTTNG_OK
) {
970 * Setup relayd connections for a tracing session. First creates the socket to
971 * the relayd and send them to the right domain consumer. Consumer type MUST be
974 int cmd_setup_relayd(struct ltt_session
*session
)
977 struct ltt_ust_session
*usess
;
978 struct ltt_kernel_session
*ksess
;
979 struct consumer_socket
*socket
;
980 struct lttng_ht_iter iter
;
981 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
983 LTTNG_ASSERT(session
);
985 usess
= session
->ust_session
;
986 ksess
= session
->kernel_session
;
988 DBG("Setting relayd for session %s", session
->name
);
991 if (session
->current_trace_chunk
) {
992 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
993 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
995 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
996 current_chunk_id
.is_set
= true;
998 ERR("Failed to get current trace chunk id");
1004 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1005 && usess
->consumer
->enabled
) {
1006 /* For each consumer socket, send relayd sockets */
1007 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1008 socket
, node
.node
) {
1009 pthread_mutex_lock(socket
->lock
);
1010 ret
= send_consumer_relayd_sockets(session
->id
,
1011 usess
->consumer
, socket
,
1012 session
->name
, session
->hostname
,
1014 session
->live_timer
,
1015 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1016 session
->creation_time
,
1017 session
->name_contains_creation_time
);
1018 pthread_mutex_unlock(socket
->lock
);
1019 if (ret
!= LTTNG_OK
) {
1022 /* Session is now ready for network streaming. */
1023 session
->net_handle
= 1;
1025 session
->consumer
->relay_major_version
=
1026 usess
->consumer
->relay_major_version
;
1027 session
->consumer
->relay_minor_version
=
1028 usess
->consumer
->relay_minor_version
;
1029 session
->consumer
->relay_allows_clear
=
1030 usess
->consumer
->relay_allows_clear
;
1033 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1034 && ksess
->consumer
->enabled
) {
1035 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1036 socket
, node
.node
) {
1037 pthread_mutex_lock(socket
->lock
);
1038 ret
= send_consumer_relayd_sockets(session
->id
,
1039 ksess
->consumer
, socket
,
1040 session
->name
, session
->hostname
,
1042 session
->live_timer
,
1043 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1044 session
->creation_time
,
1045 session
->name_contains_creation_time
);
1046 pthread_mutex_unlock(socket
->lock
);
1047 if (ret
!= LTTNG_OK
) {
1050 /* Session is now ready for network streaming. */
1051 session
->net_handle
= 1;
1053 session
->consumer
->relay_major_version
=
1054 ksess
->consumer
->relay_major_version
;
1055 session
->consumer
->relay_minor_version
=
1056 ksess
->consumer
->relay_minor_version
;
1057 session
->consumer
->relay_allows_clear
=
1058 ksess
->consumer
->relay_allows_clear
;
1067 * Start a kernel session by opening all necessary streams.
1069 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1072 struct ltt_kernel_channel
*kchan
;
1074 /* Open kernel metadata */
1075 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1076 ret
= kernel_open_metadata(ksess
);
1078 ret
= LTTNG_ERR_KERN_META_FAIL
;
1083 /* Open kernel metadata stream */
1084 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1085 ret
= kernel_open_metadata_stream(ksess
);
1087 ERR("Kernel create metadata stream failed");
1088 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1093 /* For each channel */
1094 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1095 if (kchan
->stream_count
== 0) {
1096 ret
= kernel_open_channel_stream(kchan
);
1098 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1101 /* Update the stream global counter */
1102 ksess
->stream_count_global
+= ret
;
1106 /* Setup kernel consumer socket and send fds to it */
1107 ret
= init_kernel_tracing(ksess
);
1109 ret
= LTTNG_ERR_KERN_START_FAIL
;
1113 /* This start the kernel tracing */
1114 ret
= kernel_start_session(ksess
);
1116 ret
= LTTNG_ERR_KERN_START_FAIL
;
1120 /* Quiescent wait after starting trace */
1121 kernel_wait_quiescent();
1131 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1133 struct ltt_kernel_channel
*kchan
;
1134 bool error_occurred
= false;
1137 if (!ksess
|| !ksess
->active
) {
1140 DBG("Stopping kernel tracing");
1142 ret
= kernel_stop_session(ksess
);
1144 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1148 kernel_wait_quiescent();
1150 /* Flush metadata after stopping (if exists) */
1151 if (ksess
->metadata_stream_fd
>= 0) {
1152 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1154 ERR("Kernel metadata flush failed");
1155 error_occurred
= true;
1159 /* Flush all buffers after stopping */
1160 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1161 ret
= kernel_flush_buffer(kchan
);
1163 ERR("Kernel flush buffer error");
1164 error_occurred
= true;
1169 if (error_occurred
) {
1170 ret
= LTTNG_ERR_UNK
;
1179 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1181 int cmd_disable_channel(struct ltt_session
*session
,
1182 enum lttng_domain_type domain
, char *channel_name
)
1185 struct ltt_ust_session
*usess
;
1187 usess
= session
->ust_session
;
1192 case LTTNG_DOMAIN_KERNEL
:
1194 ret
= channel_kernel_disable(session
->kernel_session
,
1196 if (ret
!= LTTNG_OK
) {
1200 kernel_wait_quiescent();
1203 case LTTNG_DOMAIN_UST
:
1205 struct ltt_ust_channel
*uchan
;
1206 struct lttng_ht
*chan_ht
;
1208 chan_ht
= usess
->domain_global
.channels
;
1210 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1211 if (uchan
== NULL
) {
1212 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1216 ret
= channel_ust_disable(usess
, uchan
);
1217 if (ret
!= LTTNG_OK
) {
1223 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1235 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1237 * The wpipe arguments is used as a notifier for the kernel thread.
1239 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1243 ssize_t sock_recv_len
;
1244 struct lttng_channel
*channel
= NULL
;
1245 struct lttng_buffer_view view
;
1246 struct lttng_dynamic_buffer channel_buffer
;
1247 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1249 lttng_dynamic_buffer_init(&channel_buffer
);
1250 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1251 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1253 ret
= LTTNG_ERR_NOMEM
;
1257 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1259 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1260 ERR("Failed to receive \"enable channel\" command payload");
1261 ret
= LTTNG_ERR_INVALID
;
1265 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1266 if (!lttng_buffer_view_is_valid(&view
)) {
1267 ret
= LTTNG_ERR_INVALID
;
1271 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1272 ERR("Invalid channel payload received in \"enable channel\" command");
1273 ret
= LTTNG_ERR_INVALID
;
1277 ret
= cmd_enable_channel_internal(
1278 cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1281 lttng_dynamic_buffer_reset(&channel_buffer
);
1282 lttng_channel_destroy(channel
);
1286 static enum lttng_error_code
cmd_enable_channel_internal(
1287 struct ltt_session
*session
,
1288 const struct lttng_domain
*domain
,
1289 const struct lttng_channel
*_attr
,
1292 enum lttng_error_code ret_code
;
1293 struct ltt_ust_session
*usess
= session
->ust_session
;
1294 struct lttng_ht
*chan_ht
;
1296 struct lttng_channel
*attr
= NULL
;
1298 LTTNG_ASSERT(session
);
1299 LTTNG_ASSERT(_attr
);
1300 LTTNG_ASSERT(domain
);
1302 attr
= lttng_channel_copy(_attr
);
1304 ret_code
= LTTNG_ERR_NOMEM
;
1308 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1310 /* Validate channel name */
1311 if (attr
->name
[0] == '.' ||
1312 memchr(attr
->name
, '/', len
) != NULL
) {
1313 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1317 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1322 * If the session is a live session, remove the switch timer, the
1323 * live timer does the same thing but sends also synchronisation
1324 * beacons for inactive streams.
1326 if (session
->live_timer
> 0) {
1327 attr
->attr
.live_timer_interval
= session
->live_timer
;
1328 attr
->attr
.switch_timer_interval
= 0;
1331 /* Check for feature support */
1332 switch (domain
->type
) {
1333 case LTTNG_DOMAIN_KERNEL
:
1335 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1336 /* Sampling position of buffer is not supported */
1337 WARN("Kernel tracer does not support buffer monitoring. "
1338 "Setting the monitor interval timer to 0 "
1339 "(disabled) for channel '%s' of session '%s'",
1340 attr
->name
, session
->name
);
1341 lttng_channel_set_monitor_timer_interval(attr
, 0);
1345 case LTTNG_DOMAIN_UST
:
1347 case LTTNG_DOMAIN_JUL
:
1348 case LTTNG_DOMAIN_LOG4J
:
1349 case LTTNG_DOMAIN_PYTHON
:
1350 if (!agent_tracing_is_enabled()) {
1351 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1352 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1357 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1361 switch (domain
->type
) {
1362 case LTTNG_DOMAIN_KERNEL
:
1364 struct ltt_kernel_channel
*kchan
;
1366 kchan
= trace_kernel_get_channel_by_name(
1367 attr
->name
, session
->kernel_session
);
1368 if (kchan
== NULL
) {
1370 * Don't try to create a channel if the session has been started at
1371 * some point in time before. The tracer does not allow it.
1373 if (session
->has_been_started
) {
1374 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1378 if (session
->snapshot
.nb_output
> 0 ||
1379 session
->snapshot_mode
) {
1380 /* Enforce mmap output for snapshot sessions. */
1381 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1383 ret_code
= channel_kernel_create(
1384 session
->kernel_session
, attr
, wpipe
);
1385 if (attr
->name
[0] != '\0') {
1386 session
->kernel_session
->has_non_default_channel
= 1;
1389 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1392 if (ret_code
!= LTTNG_OK
) {
1396 kernel_wait_quiescent();
1399 case LTTNG_DOMAIN_UST
:
1400 case LTTNG_DOMAIN_JUL
:
1401 case LTTNG_DOMAIN_LOG4J
:
1402 case LTTNG_DOMAIN_PYTHON
:
1404 struct ltt_ust_channel
*uchan
;
1409 * Current agent implementation limitations force us to allow
1410 * only one channel at once in "agent" subdomains. Each
1411 * subdomain has a default channel name which must be strictly
1414 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1415 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1416 LTTNG_SYMBOL_NAME_LEN
)) {
1417 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1420 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1421 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1422 LTTNG_SYMBOL_NAME_LEN
)) {
1423 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1426 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1427 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1428 LTTNG_SYMBOL_NAME_LEN
)) {
1429 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1434 chan_ht
= usess
->domain_global
.channels
;
1436 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1437 if (uchan
== NULL
) {
1439 * Don't try to create a channel if the session has been started at
1440 * some point in time before. The tracer does not allow it.
1442 if (session
->has_been_started
) {
1443 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1447 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1448 if (attr
->name
[0] != '\0') {
1449 usess
->has_non_default_channel
= 1;
1452 ret_code
= channel_ust_enable(usess
, uchan
);
1457 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1461 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1462 session
->has_non_mmap_channel
= true;
1467 lttng_channel_destroy(attr
);
1471 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1472 struct ltt_session
*session
,
1473 enum lttng_domain_type domain
,
1474 enum lttng_process_attr process_attr
,
1475 enum lttng_tracking_policy
*policy
)
1477 enum lttng_error_code ret_code
= LTTNG_OK
;
1478 const struct process_attr_tracker
*tracker
;
1481 case LTTNG_DOMAIN_KERNEL
:
1482 if (!session
->kernel_session
) {
1483 ret_code
= LTTNG_ERR_INVALID
;
1486 tracker
= kernel_get_process_attr_tracker(
1487 session
->kernel_session
, process_attr
);
1489 case LTTNG_DOMAIN_UST
:
1490 if (!session
->ust_session
) {
1491 ret_code
= LTTNG_ERR_INVALID
;
1494 tracker
= trace_ust_get_process_attr_tracker(
1495 session
->ust_session
, process_attr
);
1498 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1502 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1504 ret_code
= LTTNG_ERR_INVALID
;
1510 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1511 struct ltt_session
*session
,
1512 enum lttng_domain_type domain
,
1513 enum lttng_process_attr process_attr
,
1514 enum lttng_tracking_policy policy
)
1516 enum lttng_error_code ret_code
= LTTNG_OK
;
1519 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1520 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1521 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1524 ret_code
= LTTNG_ERR_INVALID
;
1529 case LTTNG_DOMAIN_KERNEL
:
1530 if (!session
->kernel_session
) {
1531 ret_code
= LTTNG_ERR_INVALID
;
1534 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1535 session
->kernel_session
, process_attr
, policy
);
1537 case LTTNG_DOMAIN_UST
:
1538 if (!session
->ust_session
) {
1539 ret_code
= LTTNG_ERR_INVALID
;
1542 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1543 session
->ust_session
, process_attr
, policy
);
1546 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1553 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1554 struct ltt_session
*session
,
1555 enum lttng_domain_type domain
,
1556 enum lttng_process_attr process_attr
,
1557 const struct process_attr_value
*value
)
1559 enum lttng_error_code ret_code
= LTTNG_OK
;
1562 case LTTNG_DOMAIN_KERNEL
:
1563 if (!session
->kernel_session
) {
1564 ret_code
= LTTNG_ERR_INVALID
;
1567 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1568 session
->kernel_session
, process_attr
, value
);
1570 case LTTNG_DOMAIN_UST
:
1571 if (!session
->ust_session
) {
1572 ret_code
= LTTNG_ERR_INVALID
;
1575 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1576 session
->ust_session
, process_attr
, value
);
1579 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1586 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1587 struct ltt_session
*session
,
1588 enum lttng_domain_type domain
,
1589 enum lttng_process_attr process_attr
,
1590 const struct process_attr_value
*value
)
1592 enum lttng_error_code ret_code
= LTTNG_OK
;
1595 case LTTNG_DOMAIN_KERNEL
:
1596 if (!session
->kernel_session
) {
1597 ret_code
= LTTNG_ERR_INVALID
;
1600 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1601 session
->kernel_session
, process_attr
, value
);
1603 case LTTNG_DOMAIN_UST
:
1604 if (!session
->ust_session
) {
1605 ret_code
= LTTNG_ERR_INVALID
;
1608 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1609 session
->ust_session
, process_attr
, value
);
1612 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1619 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1620 struct ltt_session
*session
,
1621 enum lttng_domain_type domain
,
1622 enum lttng_process_attr process_attr
,
1623 struct lttng_process_attr_values
**values
)
1625 enum lttng_error_code ret_code
= LTTNG_OK
;
1626 const struct process_attr_tracker
*tracker
;
1627 enum process_attr_tracker_status status
;
1630 case LTTNG_DOMAIN_KERNEL
:
1631 if (!session
->kernel_session
) {
1632 ret_code
= LTTNG_ERR_INVALID
;
1635 tracker
= kernel_get_process_attr_tracker(
1636 session
->kernel_session
, process_attr
);
1638 case LTTNG_DOMAIN_UST
:
1639 if (!session
->ust_session
) {
1640 ret_code
= LTTNG_ERR_INVALID
;
1643 tracker
= trace_ust_get_process_attr_tracker(
1644 session
->ust_session
, process_attr
);
1647 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1652 ret_code
= LTTNG_ERR_INVALID
;
1656 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1658 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1659 ret_code
= LTTNG_OK
;
1661 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1662 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1664 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1665 ret_code
= LTTNG_ERR_NOMEM
;
1668 ret_code
= LTTNG_ERR_UNK
;
1677 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1679 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1680 struct lttng_event
*event
,
1681 char *filter_expression
,
1682 struct lttng_bytecode
*bytecode
,
1683 struct lttng_event_exclusion
*exclusion
)
1686 const char *event_name
;
1687 const struct ltt_session
*session
= cmd_ctx
->session
;
1688 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1689 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1691 DBG("Disable event command for event \'%s\'", event
->name
);
1694 * Filter and exclusions are simply not handled by the
1695 * disable event command at this time.
1699 (void) filter_expression
;
1702 /* Ignore the presence of filter or exclusion for the event */
1704 event
->exclusion
= 0;
1706 event_name
= event
->name
;
1708 /* Error out on unhandled search criteria */
1709 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1710 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1711 ret
= LTTNG_ERR_UNK
;
1718 case LTTNG_DOMAIN_KERNEL
:
1720 struct ltt_kernel_channel
*kchan
;
1721 struct ltt_kernel_session
*ksess
;
1723 ksess
= session
->kernel_session
;
1726 * If a non-default channel has been created in the
1727 * session, explicitely require that -c chan_name needs
1730 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1731 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1735 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1736 if (kchan
== NULL
) {
1737 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1741 switch (event
->type
) {
1742 case LTTNG_EVENT_ALL
:
1743 case LTTNG_EVENT_TRACEPOINT
:
1744 case LTTNG_EVENT_SYSCALL
:
1745 case LTTNG_EVENT_PROBE
:
1746 case LTTNG_EVENT_FUNCTION
:
1747 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1748 if (event_name
[0] == '\0') {
1749 ret
= event_kernel_disable_event(kchan
,
1752 ret
= event_kernel_disable_event(kchan
,
1753 event_name
, event
->type
);
1755 if (ret
!= LTTNG_OK
) {
1760 ret
= LTTNG_ERR_UNK
;
1764 kernel_wait_quiescent();
1767 case LTTNG_DOMAIN_UST
:
1769 struct ltt_ust_channel
*uchan
;
1770 struct ltt_ust_session
*usess
;
1772 usess
= session
->ust_session
;
1774 if (validate_ust_event_name(event_name
)) {
1775 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1780 * If a non-default channel has been created in the
1781 * session, explicitly require that -c chan_name needs
1784 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1785 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1789 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1791 if (uchan
== NULL
) {
1792 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1796 switch (event
->type
) {
1797 case LTTNG_EVENT_ALL
:
1799 * An empty event name means that everything
1800 * should be disabled.
1802 if (event
->name
[0] == '\0') {
1803 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1805 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1808 if (ret
!= LTTNG_OK
) {
1813 ret
= LTTNG_ERR_UNK
;
1817 DBG3("Disable UST event %s in channel %s completed", event_name
,
1821 case LTTNG_DOMAIN_LOG4J
:
1822 case LTTNG_DOMAIN_JUL
:
1823 case LTTNG_DOMAIN_PYTHON
:
1826 struct ltt_ust_session
*usess
= session
->ust_session
;
1828 LTTNG_ASSERT(usess
);
1830 switch (event
->type
) {
1831 case LTTNG_EVENT_ALL
:
1834 ret
= LTTNG_ERR_UNK
;
1838 agt
= trace_ust_find_agent(usess
, domain
);
1840 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1844 * An empty event name means that everything
1845 * should be disabled.
1847 if (event
->name
[0] == '\0') {
1848 ret
= event_agent_disable_all(usess
, agt
);
1850 ret
= event_agent_disable(usess
, agt
, event_name
);
1852 if (ret
!= LTTNG_OK
) {
1859 ret
= LTTNG_ERR_UND
;
1870 free(filter_expression
);
1875 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1877 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1878 const struct lttng_event_context
*event_context
, int kwpipe
)
1880 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1881 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1882 const struct ltt_session
*session
= cmd_ctx
->session
;
1883 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1886 * Don't try to add a context if the session has been started at
1887 * some point in time before. The tracer does not allow it and would
1888 * result in a corrupted trace.
1890 if (cmd_ctx
->session
->has_been_started
) {
1891 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1896 case LTTNG_DOMAIN_KERNEL
:
1897 LTTNG_ASSERT(session
->kernel_session
);
1899 if (session
->kernel_session
->channel_count
== 0) {
1900 /* Create default channel */
1901 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1902 if (ret
!= LTTNG_OK
) {
1905 chan_kern_created
= 1;
1907 /* Add kernel context to kernel tracer */
1908 ret
= context_kernel_add(session
->kernel_session
,
1909 event_context
, channel_name
);
1910 if (ret
!= LTTNG_OK
) {
1914 case LTTNG_DOMAIN_JUL
:
1915 case LTTNG_DOMAIN_LOG4J
:
1918 * Validate channel name.
1919 * If no channel name is given and the domain is JUL or LOG4J,
1920 * set it to the appropriate domain-specific channel name. If
1921 * a name is provided but does not match the expexted channel
1922 * name, return an error.
1924 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1925 strcmp(channel_name
,
1926 DEFAULT_JUL_CHANNEL_NAME
)) {
1927 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1929 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1930 strcmp(channel_name
,
1931 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1932 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1937 case LTTNG_DOMAIN_UST
:
1939 struct ltt_ust_session
*usess
= session
->ust_session
;
1940 unsigned int chan_count
;
1942 LTTNG_ASSERT(usess
);
1944 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1945 if (chan_count
== 0) {
1946 struct lttng_channel
*attr
;
1947 /* Create default channel */
1948 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1950 ret
= LTTNG_ERR_FATAL
;
1954 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1955 if (ret
!= LTTNG_OK
) {
1959 channel_attr_destroy(attr
);
1960 chan_ust_created
= 1;
1963 ret
= context_ust_add(usess
, domain
, event_context
,
1965 if (ret
!= LTTNG_OK
) {
1971 ret
= LTTNG_ERR_UND
;
1979 if (chan_kern_created
) {
1980 struct ltt_kernel_channel
*kchan
=
1981 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1982 session
->kernel_session
);
1983 /* Created previously, this should NOT fail. */
1984 LTTNG_ASSERT(kchan
);
1985 kernel_destroy_channel(kchan
);
1988 if (chan_ust_created
) {
1989 struct ltt_ust_channel
*uchan
=
1990 trace_ust_find_channel_by_name(
1991 session
->ust_session
->domain_global
.channels
,
1992 DEFAULT_CHANNEL_NAME
);
1993 /* Created previously, this should NOT fail. */
1994 LTTNG_ASSERT(uchan
);
1995 /* Remove from the channel list of the session. */
1996 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1998 trace_ust_destroy_channel(uchan
);
2004 static inline bool name_starts_with(const char *name
, const char *prefix
)
2006 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2008 return !strncmp(name
, prefix
, max_cmp_len
);
2011 /* Perform userspace-specific event name validation */
2012 static int validate_ust_event_name(const char *name
)
2022 * Check name against all internal UST event component namespaces used
2025 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2026 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2027 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2036 * Internal version of cmd_enable_event() with a supplemental
2037 * "internal_event" flag which is used to enable internal events which should
2038 * be hidden from clients. Such events are used in the agent implementation to
2039 * enable the events through which all "agent" events are funeled.
2041 static int _cmd_enable_event(struct ltt_session
*session
,
2042 const struct lttng_domain
*domain
,
2043 char *channel_name
, struct lttng_event
*event
,
2044 char *filter_expression
,
2045 struct lttng_bytecode
*filter
,
2046 struct lttng_event_exclusion
*exclusion
,
2047 int wpipe
, bool internal_event
)
2049 int ret
= 0, channel_created
= 0;
2050 struct lttng_channel
*attr
= NULL
;
2052 LTTNG_ASSERT(session
);
2053 LTTNG_ASSERT(event
);
2054 LTTNG_ASSERT(channel_name
);
2056 /* If we have a filter, we must have its filter expression */
2057 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2059 /* Normalize event name as a globbing pattern */
2060 strutils_normalize_star_glob_pattern(event
->name
);
2062 /* Normalize exclusion names as globbing patterns */
2066 for (i
= 0; i
< exclusion
->count
; i
++) {
2067 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2069 strutils_normalize_star_glob_pattern(name
);
2073 DBG("Enable event command for event \'%s\'", event
->name
);
2077 switch (domain
->type
) {
2078 case LTTNG_DOMAIN_KERNEL
:
2080 struct ltt_kernel_channel
*kchan
;
2083 * If a non-default channel has been created in the
2084 * session, explicitely require that -c chan_name needs
2087 if (session
->kernel_session
->has_non_default_channel
2088 && channel_name
[0] == '\0') {
2089 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2093 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2094 session
->kernel_session
);
2095 if (kchan
== NULL
) {
2096 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2097 LTTNG_BUFFER_GLOBAL
);
2099 ret
= LTTNG_ERR_FATAL
;
2102 if (lttng_strncpy(attr
->name
, channel_name
,
2103 sizeof(attr
->name
))) {
2104 ret
= LTTNG_ERR_INVALID
;
2108 ret
= cmd_enable_channel_internal(
2109 session
, domain
, attr
, wpipe
);
2110 if (ret
!= LTTNG_OK
) {
2113 channel_created
= 1;
2116 /* Get the newly created kernel channel pointer */
2117 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2118 session
->kernel_session
);
2119 if (kchan
== NULL
) {
2120 /* This sould not happen... */
2121 ret
= LTTNG_ERR_FATAL
;
2125 switch (event
->type
) {
2126 case LTTNG_EVENT_ALL
:
2128 char *filter_expression_a
= NULL
;
2129 struct lttng_bytecode
*filter_a
= NULL
;
2132 * We need to duplicate filter_expression and filter,
2133 * because ownership is passed to first enable
2136 if (filter_expression
) {
2137 filter_expression_a
= strdup(filter_expression
);
2138 if (!filter_expression_a
) {
2139 ret
= LTTNG_ERR_FATAL
;
2144 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2146 free(filter_expression_a
);
2147 ret
= LTTNG_ERR_FATAL
;
2150 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2152 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2153 ret
= event_kernel_enable_event(kchan
, event
,
2154 filter_expression
, filter
);
2155 /* We have passed ownership */
2156 filter_expression
= NULL
;
2158 if (ret
!= LTTNG_OK
) {
2159 if (channel_created
) {
2160 /* Let's not leak a useless channel. */
2161 kernel_destroy_channel(kchan
);
2163 free(filter_expression_a
);
2167 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2168 ret
= event_kernel_enable_event(kchan
, event
,
2169 filter_expression_a
, filter_a
);
2170 /* We have passed ownership */
2171 filter_expression_a
= NULL
;
2173 if (ret
!= LTTNG_OK
) {
2178 case LTTNG_EVENT_PROBE
:
2179 case LTTNG_EVENT_USERSPACE_PROBE
:
2180 case LTTNG_EVENT_FUNCTION
:
2181 case LTTNG_EVENT_FUNCTION_ENTRY
:
2182 case LTTNG_EVENT_TRACEPOINT
:
2183 ret
= event_kernel_enable_event(kchan
, event
,
2184 filter_expression
, filter
);
2185 /* We have passed ownership */
2186 filter_expression
= NULL
;
2188 if (ret
!= LTTNG_OK
) {
2189 if (channel_created
) {
2190 /* Let's not leak a useless channel. */
2191 kernel_destroy_channel(kchan
);
2196 case LTTNG_EVENT_SYSCALL
:
2197 ret
= event_kernel_enable_event(kchan
, event
,
2198 filter_expression
, filter
);
2199 /* We have passed ownership */
2200 filter_expression
= NULL
;
2202 if (ret
!= LTTNG_OK
) {
2207 ret
= LTTNG_ERR_UNK
;
2211 kernel_wait_quiescent();
2214 case LTTNG_DOMAIN_UST
:
2216 struct ltt_ust_channel
*uchan
;
2217 struct ltt_ust_session
*usess
= session
->ust_session
;
2219 LTTNG_ASSERT(usess
);
2222 * If a non-default channel has been created in the
2223 * session, explicitely require that -c chan_name needs
2226 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2227 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2231 /* Get channel from global UST domain */
2232 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2234 if (uchan
== NULL
) {
2235 /* Create default channel */
2236 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2237 usess
->buffer_type
);
2239 ret
= LTTNG_ERR_FATAL
;
2242 if (lttng_strncpy(attr
->name
, channel_name
,
2243 sizeof(attr
->name
))) {
2244 ret
= LTTNG_ERR_INVALID
;
2248 ret
= cmd_enable_channel_internal(
2249 session
, domain
, attr
, wpipe
);
2250 if (ret
!= LTTNG_OK
) {
2254 /* Get the newly created channel reference back */
2255 uchan
= trace_ust_find_channel_by_name(
2256 usess
->domain_global
.channels
, channel_name
);
2257 LTTNG_ASSERT(uchan
);
2260 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2262 * Don't allow users to add UST events to channels which
2263 * are assigned to a userspace subdomain (JUL, Log4J,
2266 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2270 if (!internal_event
) {
2272 * Ensure the event name is not reserved for internal
2275 ret
= validate_ust_event_name(event
->name
);
2277 WARN("Userspace event name %s failed validation.",
2279 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2284 /* At this point, the session and channel exist on the tracer */
2285 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2286 filter_expression
, filter
, exclusion
,
2288 /* We have passed ownership */
2289 filter_expression
= NULL
;
2292 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2293 goto already_enabled
;
2294 } else if (ret
!= LTTNG_OK
) {
2299 case LTTNG_DOMAIN_LOG4J
:
2300 case LTTNG_DOMAIN_JUL
:
2301 case LTTNG_DOMAIN_PYTHON
:
2303 const char *default_event_name
, *default_chan_name
;
2305 struct lttng_event uevent
;
2306 struct lttng_domain tmp_dom
;
2307 struct ltt_ust_session
*usess
= session
->ust_session
;
2309 LTTNG_ASSERT(usess
);
2311 if (!agent_tracing_is_enabled()) {
2312 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2313 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2317 agt
= trace_ust_find_agent(usess
, domain
->type
);
2319 agt
= agent_create(domain
->type
);
2321 ret
= LTTNG_ERR_NOMEM
;
2324 agent_add(agt
, usess
->agents
);
2327 /* Create the default tracepoint. */
2328 memset(&uevent
, 0, sizeof(uevent
));
2329 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2330 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2331 default_event_name
= event_get_default_agent_ust_name(
2333 if (!default_event_name
) {
2334 ret
= LTTNG_ERR_FATAL
;
2337 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2338 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2341 * The domain type is changed because we are about to enable the
2342 * default channel and event for the JUL domain that are hardcoded.
2343 * This happens in the UST domain.
2345 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2346 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2348 switch (domain
->type
) {
2349 case LTTNG_DOMAIN_LOG4J
:
2350 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2352 case LTTNG_DOMAIN_JUL
:
2353 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2355 case LTTNG_DOMAIN_PYTHON
:
2356 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2359 /* The switch/case we are in makes this impossible */
2364 char *filter_expression_copy
= NULL
;
2365 struct lttng_bytecode
*filter_copy
= NULL
;
2368 const size_t filter_size
= sizeof(
2369 struct lttng_bytecode
)
2372 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2374 ret
= LTTNG_ERR_NOMEM
;
2377 memcpy(filter_copy
, filter
, filter_size
);
2379 filter_expression_copy
=
2380 strdup(filter_expression
);
2381 if (!filter_expression
) {
2382 ret
= LTTNG_ERR_NOMEM
;
2385 if (!filter_expression_copy
|| !filter_copy
) {
2386 free(filter_expression_copy
);
2392 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2393 (char *) default_chan_name
,
2394 &uevent
, filter_expression_copy
,
2395 filter_copy
, NULL
, wpipe
);
2398 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2399 goto already_enabled
;
2400 } else if (ret
!= LTTNG_OK
) {
2404 /* The wild card * means that everything should be enabled. */
2405 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2406 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2409 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2413 filter_expression
= NULL
;
2414 if (ret
!= LTTNG_OK
) {
2421 ret
= LTTNG_ERR_UND
;
2429 free(filter_expression
);
2432 channel_attr_destroy(attr
);
2438 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2439 * We own filter, exclusion, and filter_expression.
2441 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2442 struct lttng_event
*event
,
2443 char *filter_expression
,
2444 struct lttng_event_exclusion
*exclusion
,
2445 struct lttng_bytecode
*bytecode
,
2450 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2452 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2455 * The ownership of the following parameters is transferred to
2456 * _cmd_enable_event:
2458 * - filter_expression,
2462 ret
= _cmd_enable_event(cmd_ctx
->session
,
2464 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2465 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2466 filter_expression
= NULL
;
2473 * Enable an event which is internal to LTTng. An internal should
2474 * never be made visible to clients and are immune to checks such as
2477 static int cmd_enable_event_internal(struct ltt_session
*session
,
2478 const struct lttng_domain
*domain
,
2479 char *channel_name
, struct lttng_event
*event
,
2480 char *filter_expression
,
2481 struct lttng_bytecode
*filter
,
2482 struct lttng_event_exclusion
*exclusion
,
2485 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2486 filter_expression
, filter
, exclusion
, wpipe
, true);
2490 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2492 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2493 struct lttng_payload
*reply_payload
)
2495 enum lttng_error_code ret_code
;
2497 ssize_t i
, nb_events
= 0;
2498 struct lttng_event
*events
= NULL
;
2499 struct lttcomm_list_command_header reply_command_header
= {};
2500 size_t reply_command_header_offset
;
2502 assert(reply_payload
);
2504 /* Reserve space for command reply header. */
2505 reply_command_header_offset
= reply_payload
->buffer
.size
;
2506 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2507 reply_command_header_offset
+
2508 sizeof(struct lttcomm_list_command_header
));
2510 ret_code
= LTTNG_ERR_NOMEM
;
2515 case LTTNG_DOMAIN_KERNEL
:
2516 nb_events
= kernel_list_events(&events
);
2517 if (nb_events
< 0) {
2518 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2522 case LTTNG_DOMAIN_UST
:
2523 nb_events
= ust_app_list_events(&events
);
2524 if (nb_events
< 0) {
2525 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2529 case LTTNG_DOMAIN_LOG4J
:
2530 case LTTNG_DOMAIN_JUL
:
2531 case LTTNG_DOMAIN_PYTHON
:
2532 nb_events
= agent_list_events(&events
, domain
);
2533 if (nb_events
< 0) {
2534 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2539 ret_code
= LTTNG_ERR_UND
;
2543 for (i
= 0; i
< nb_events
; i
++) {
2544 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2547 ret_code
= LTTNG_ERR_NOMEM
;
2552 if (nb_events
> UINT32_MAX
) {
2553 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2554 ret_code
= LTTNG_ERR_OVERFLOW
;
2558 /* Update command reply header. */
2559 reply_command_header
.count
= (uint32_t) nb_events
;
2560 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2561 sizeof(reply_command_header
));
2563 ret_code
= LTTNG_OK
;
2570 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2572 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2573 struct lttng_payload
*reply
)
2575 enum lttng_error_code ret_code
;
2577 unsigned int i
, nb_fields
;
2578 struct lttng_event_field
*fields
= NULL
;
2579 struct lttcomm_list_command_header reply_command_header
= {};
2580 size_t reply_command_header_offset
;
2584 /* Reserve space for command reply header. */
2585 reply_command_header_offset
= reply
->buffer
.size
;
2586 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2587 reply_command_header_offset
+
2588 sizeof(struct lttcomm_list_command_header
));
2590 ret_code
= LTTNG_ERR_NOMEM
;
2595 case LTTNG_DOMAIN_UST
:
2596 ret
= ust_app_list_event_fields(&fields
);
2598 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2603 case LTTNG_DOMAIN_KERNEL
:
2604 default: /* fall-through */
2605 ret_code
= LTTNG_ERR_UND
;
2611 for (i
= 0; i
< nb_fields
; i
++) {
2612 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2614 ret_code
= LTTNG_ERR_NOMEM
;
2619 if (nb_fields
> UINT32_MAX
) {
2620 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2621 ret_code
= LTTNG_ERR_OVERFLOW
;
2625 /* Update command reply header. */
2626 reply_command_header
.count
= (uint32_t) nb_fields
;
2628 memcpy(reply
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2629 sizeof(reply_command_header
));
2631 ret_code
= LTTNG_OK
;
2638 enum lttng_error_code
cmd_list_syscalls(
2639 struct lttng_payload
*reply_payload
)
2641 enum lttng_error_code ret_code
;
2642 ssize_t nb_events
, i
;
2644 struct lttng_event
*events
= NULL
;
2645 struct lttcomm_list_command_header reply_command_header
= {};
2646 size_t reply_command_header_offset
;
2648 assert(reply_payload
);
2650 /* Reserve space for command reply header. */
2651 reply_command_header_offset
= reply_payload
->buffer
.size
;
2652 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2653 reply_command_header_offset
+
2654 sizeof(struct lttcomm_list_command_header
));
2656 ret_code
= LTTNG_ERR_NOMEM
;
2660 nb_events
= syscall_table_list(&events
);
2661 if (nb_events
< 0) {
2662 ret_code
= (enum lttng_error_code
) -nb_events
;
2666 for (i
= 0; i
< nb_events
; i
++) {
2667 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2670 ret_code
= LTTNG_ERR_NOMEM
;
2675 if (nb_events
> UINT32_MAX
) {
2676 ERR("Syscall count would overflow the syscall listing command's reply");
2677 ret_code
= LTTNG_ERR_OVERFLOW
;
2681 /* Update command reply header. */
2682 reply_command_header
.count
= (uint32_t) nb_events
;
2683 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2684 sizeof(reply_command_header
));
2686 ret_code
= LTTNG_OK
;
2693 * Command LTTNG_START_TRACE processed by the client thread.
2695 * Called with session mutex held.
2697 int cmd_start_trace(struct ltt_session
*session
)
2699 enum lttng_error_code ret
;
2700 unsigned long nb_chan
= 0;
2701 struct ltt_kernel_session
*ksession
;
2702 struct ltt_ust_session
*usess
;
2703 const bool session_rotated_after_last_stop
=
2704 session
->rotated_after_last_stop
;
2705 const bool session_cleared_after_last_stop
=
2706 session
->cleared_after_last_stop
;
2708 LTTNG_ASSERT(session
);
2710 /* Ease our life a bit ;) */
2711 ksession
= session
->kernel_session
;
2712 usess
= session
->ust_session
;
2714 /* Is the session already started? */
2715 if (session
->active
) {
2716 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2717 /* Perform nothing */
2721 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2722 !session
->current_trace_chunk
) {
2724 * A rotation was launched while the session was stopped and
2725 * it has not been completed yet. It is not possible to start
2726 * the session since starting the session here would require a
2727 * rotation from "NULL" to a new trace chunk. That rotation
2728 * would overlap with the ongoing rotation, which is not
2731 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2733 ret
= LTTNG_ERR_ROTATION_PENDING
;
2738 * Starting a session without channel is useless since after that it's not
2739 * possible to enable channel thus inform the client.
2741 if (usess
&& usess
->domain_global
.channels
) {
2742 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2745 nb_chan
+= ksession
->channel_count
;
2748 ret
= LTTNG_ERR_NO_CHANNEL
;
2752 session
->active
= 1;
2753 session
->rotated_after_last_stop
= false;
2754 session
->cleared_after_last_stop
= false;
2755 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2756 if (!session
->has_been_started
) {
2757 struct lttng_trace_chunk
*trace_chunk
;
2759 DBG("Creating initial trace chunk of session \"%s\"",
2761 trace_chunk
= session_create_new_trace_chunk(
2762 session
, NULL
, NULL
, NULL
);
2764 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2767 LTTNG_ASSERT(!session
->current_trace_chunk
);
2768 ret
= (lttng_error_code
) session_set_trace_chunk(session
, trace_chunk
,
2770 lttng_trace_chunk_put(trace_chunk
);
2772 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2776 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2779 * Rotate existing streams into the new chunk.
2780 * This is a "quiet" rotation has no client has
2781 * explicitly requested this operation.
2783 * There is also no need to wait for the rotation
2784 * to complete as it will happen immediately. No data
2785 * was produced as the session was stopped, so the
2786 * rotation should happen on reception of the command.
2788 ret
= (lttng_error_code
) cmd_rotate_session(session
, NULL
, true,
2789 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2790 if (ret
!= LTTNG_OK
) {
2796 /* Kernel tracing */
2797 if (ksession
!= NULL
) {
2798 DBG("Start kernel tracing session %s", session
->name
);
2799 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2800 if (ret
!= LTTNG_OK
) {
2805 /* Flag session that trace should start automatically */
2807 int int_ret
= ust_app_start_trace_all(usess
);
2810 ret
= LTTNG_ERR_UST_START_FAIL
;
2816 * Open a packet in every stream of the session to ensure that viewers
2817 * can correctly identify the boundaries of the periods during which
2818 * tracing was active for this session.
2820 ret
= session_open_packets(session
);
2821 if (ret
!= LTTNG_OK
) {
2826 * Clear the flag that indicates that a rotation was done while the
2827 * session was stopped.
2829 session
->rotated_after_last_stop
= false;
2831 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2832 int int_ret
= timer_session_rotation_schedule_timer_start(
2833 session
, session
->rotate_timer_period
);
2836 ERR("Failed to enable rotate timer");
2837 ret
= LTTNG_ERR_UNK
;
2845 if (ret
== LTTNG_OK
) {
2846 /* Flag this after a successful start. */
2847 session
->has_been_started
|= 1;
2849 session
->active
= 0;
2850 /* Restore initial state on error. */
2851 session
->rotated_after_last_stop
=
2852 session_rotated_after_last_stop
;
2853 session
->cleared_after_last_stop
=
2854 session_cleared_after_last_stop
;
2861 * Command LTTNG_STOP_TRACE processed by the client thread.
2863 int cmd_stop_trace(struct ltt_session
*session
)
2866 struct ltt_kernel_session
*ksession
;
2867 struct ltt_ust_session
*usess
;
2869 LTTNG_ASSERT(session
);
2871 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2873 ksession
= session
->kernel_session
;
2874 usess
= session
->ust_session
;
2876 /* Session is not active. Skip everything and inform the client. */
2877 if (!session
->active
) {
2878 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2882 ret
= stop_kernel_session(ksession
);
2883 if (ret
!= LTTNG_OK
) {
2887 if (usess
&& usess
->active
) {
2888 ret
= ust_app_stop_trace_all(usess
);
2890 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2895 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2897 /* Flag inactive after a successful stop. */
2898 session
->active
= 0;
2906 * Set the base_path of the session only if subdir of a control uris is set.
2907 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2909 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2911 struct lttng_uri
*uris
)
2916 for (i
= 0; i
< nb_uri
; i
++) {
2917 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2918 uris
[i
].subdir
[0] == '\0') {
2919 /* Not interested in these URIs */
2923 if (session
->base_path
!= NULL
) {
2924 free(session
->base_path
);
2925 session
->base_path
= NULL
;
2928 /* Set session base_path */
2929 session
->base_path
= strdup(uris
[i
].subdir
);
2930 if (!session
->base_path
) {
2931 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2932 uris
[i
].subdir
, session
->name
);
2933 ret
= LTTNG_ERR_NOMEM
;
2936 DBG2("Setting base path \"%s\" for session \"%s\"",
2937 session
->base_path
, session
->name
);
2945 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2947 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2948 struct lttng_uri
*uris
)
2951 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2952 struct ltt_ust_session
*usess
= session
->ust_session
;
2954 LTTNG_ASSERT(session
);
2956 LTTNG_ASSERT(nb_uri
> 0);
2958 /* Can't set consumer URI if the session is active. */
2959 if (session
->active
) {
2960 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2965 * Set the session base path if any. This is done inside
2966 * cmd_set_consumer_uri to preserve backward compatibility of the
2967 * previous session creation api vs the session descriptor api.
2969 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2970 if (ret
!= LTTNG_OK
) {
2974 /* Set the "global" consumer URIs */
2975 for (i
= 0; i
< nb_uri
; i
++) {
2976 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2978 if (ret
!= LTTNG_OK
) {
2983 /* Set UST session URIs */
2984 if (session
->ust_session
) {
2985 for (i
= 0; i
< nb_uri
; i
++) {
2986 ret
= add_uri_to_consumer(session
,
2987 session
->ust_session
->consumer
,
2988 &uris
[i
], LTTNG_DOMAIN_UST
);
2989 if (ret
!= LTTNG_OK
) {
2995 /* Set kernel session URIs */
2996 if (session
->kernel_session
) {
2997 for (i
= 0; i
< nb_uri
; i
++) {
2998 ret
= add_uri_to_consumer(session
,
2999 session
->kernel_session
->consumer
,
3000 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3001 if (ret
!= LTTNG_OK
) {
3008 * Make sure to set the session in output mode after we set URI since a
3009 * session can be created without URL (thus flagged in no output mode).
3011 session
->output_traces
= 1;
3013 ksess
->output_traces
= 1;
3017 usess
->output_traces
= 1;
3028 enum lttng_error_code
set_session_output_from_descriptor(
3029 struct ltt_session
*session
,
3030 const struct lttng_session_descriptor
*descriptor
)
3033 enum lttng_error_code ret_code
= LTTNG_OK
;
3034 enum lttng_session_descriptor_type session_type
=
3035 lttng_session_descriptor_get_type(descriptor
);
3036 enum lttng_session_descriptor_output_type output_type
=
3037 lttng_session_descriptor_get_output_type(descriptor
);
3038 struct lttng_uri uris
[2] = {};
3039 size_t uri_count
= 0;
3041 switch (output_type
) {
3042 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3044 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3045 lttng_session_descriptor_get_local_output_uri(descriptor
,
3049 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3050 lttng_session_descriptor_get_network_output_uris(descriptor
,
3051 &uris
[0], &uris
[1]);
3055 ret_code
= LTTNG_ERR_INVALID
;
3059 switch (session_type
) {
3060 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3062 struct snapshot_output
*new_output
= NULL
;
3064 new_output
= snapshot_output_alloc();
3066 ret_code
= LTTNG_ERR_NOMEM
;
3070 ret
= snapshot_output_init_with_uri(session
,
3071 DEFAULT_SNAPSHOT_MAX_SIZE
,
3072 NULL
, uris
, uri_count
, session
->consumer
,
3073 new_output
, &session
->snapshot
);
3075 ret_code
= (ret
== -ENOMEM
) ?
3076 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3077 snapshot_output_destroy(new_output
);
3080 snapshot_add_output(&session
->snapshot
, new_output
);
3083 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3084 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3086 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3090 ret_code
= LTTNG_ERR_INVALID
;
3098 enum lttng_error_code
cmd_create_session_from_descriptor(
3099 struct lttng_session_descriptor
*descriptor
,
3100 const lttng_sock_cred
*creds
,
3101 const char *home_path
)
3104 enum lttng_error_code ret_code
;
3105 const char *session_name
;
3106 struct ltt_session
*new_session
= NULL
;
3107 enum lttng_session_descriptor_status descriptor_status
;
3109 session_lock_list();
3111 if (*home_path
!= '/') {
3112 ERR("Home path provided by client is not absolute");
3113 ret_code
= LTTNG_ERR_INVALID
;
3118 descriptor_status
= lttng_session_descriptor_get_session_name(
3119 descriptor
, &session_name
);
3120 switch (descriptor_status
) {
3121 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3123 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3124 session_name
= NULL
;
3127 ret_code
= LTTNG_ERR_INVALID
;
3131 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3133 if (ret_code
!= LTTNG_OK
) {
3137 if (!session_name
) {
3138 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3141 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3146 if (!lttng_session_descriptor_is_output_destination_initialized(
3149 * Only include the session's creation time in the output
3150 * destination if the name of the session itself was
3151 * not auto-generated.
3153 ret_code
= lttng_session_descriptor_set_default_output(
3155 session_name
? &new_session
->creation_time
: NULL
,
3157 if (ret_code
!= LTTNG_OK
) {
3161 new_session
->has_user_specified_directory
=
3162 lttng_session_descriptor_has_output_directory(
3166 switch (lttng_session_descriptor_get_type(descriptor
)) {
3167 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3168 new_session
->snapshot_mode
= 1;
3170 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3171 new_session
->live_timer
=
3172 lttng_session_descriptor_live_get_timer_interval(
3179 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3180 if (ret_code
!= LTTNG_OK
) {
3183 new_session
->consumer
->enabled
= 1;
3184 ret_code
= LTTNG_OK
;
3186 /* Release reference provided by the session_create function. */
3187 session_put(new_session
);
3188 if (ret_code
!= LTTNG_OK
&& new_session
) {
3189 /* Release the global reference on error. */
3190 session_destroy(new_session
);
3192 session_unlock_list();
3196 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3197 struct lttng_session_descriptor
**return_descriptor
)
3200 size_t payload_size
;
3201 struct lttng_dynamic_buffer payload
;
3202 struct lttng_buffer_view home_dir_view
;
3203 struct lttng_buffer_view session_descriptor_view
;
3204 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3205 enum lttng_error_code ret_code
;
3207 lttng_dynamic_buffer_init(&payload
);
3208 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3210 ret_code
= LTTNG_ERR_INVALID
;
3213 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3214 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3215 ret_code
= LTTNG_ERR_INVALID
;
3219 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3220 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3221 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3223 ret_code
= LTTNG_ERR_NOMEM
;
3227 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3229 ERR("Reception of session descriptor failed, aborting.");
3230 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3234 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3237 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3238 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3239 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3240 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3241 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3245 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3247 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3248 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3249 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3250 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3251 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3255 ret
= lttng_session_descriptor_create_from_buffer(
3256 &session_descriptor_view
, &session_descriptor
);
3258 ERR("Failed to create session descriptor from payload of \"create session\" command");
3259 ret_code
= LTTNG_ERR_INVALID
;
3264 * Sets the descriptor's auto-generated properties (name, output) if
3267 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3269 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3270 if (ret_code
!= LTTNG_OK
) {
3274 ret_code
= LTTNG_OK
;
3275 *return_descriptor
= session_descriptor
;
3276 session_descriptor
= NULL
;
3278 lttng_dynamic_buffer_reset(&payload
);
3279 lttng_session_descriptor_destroy(session_descriptor
);
3284 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3285 void *_reply_context
)
3289 const struct cmd_destroy_session_reply_context
*reply_context
=
3290 (cmd_destroy_session_reply_context
*) _reply_context
;
3291 struct lttng_dynamic_buffer payload
;
3292 struct lttcomm_session_destroy_command_header cmd_header
;
3293 struct lttng_trace_archive_location
*location
= NULL
;
3294 struct lttcomm_lttng_msg llm
= {
3295 .cmd_type
= LTTNG_DESTROY_SESSION
,
3296 .ret_code
= reply_context
->destruction_status
,
3299 sizeof(struct lttcomm_session_destroy_command_header
),
3303 size_t payload_size_before_location
;
3305 lttng_dynamic_buffer_init(&payload
);
3307 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3309 ERR("Failed to append session destruction message");
3313 cmd_header
.rotation_state
=
3314 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3315 session
->rotation_state
:
3316 LTTNG_ROTATION_STATE_NO_ROTATION
);
3317 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3318 sizeof(cmd_header
));
3320 ERR("Failed to append session destruction command header");
3324 if (!reply_context
->implicit_rotation_on_destroy
) {
3325 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3329 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3330 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3335 location
= session_get_trace_archive_location(session
);
3337 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3342 payload_size_before_location
= payload
.size
;
3343 comm_ret
= lttng_trace_archive_location_serialize(location
,
3345 lttng_trace_archive_location_put(location
);
3347 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3351 /* Update the message to indicate the location's length. */
3352 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3353 payload
.size
- payload_size_before_location
;
3355 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3356 payload
.data
, payload
.size
);
3357 if (comm_ret
!= (ssize_t
) payload
.size
) {
3358 ERR("Failed to send result of the destruction of session \"%s\" to client",
3362 ret
= close(reply_context
->reply_sock_fd
);
3364 PERROR("Failed to close client socket in deferred session destroy reply");
3366 lttng_dynamic_buffer_reset(&payload
);
3367 free(_reply_context
);
3371 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3373 * Called with session lock held.
3375 int cmd_destroy_session(struct ltt_session
*session
,
3376 struct notification_thread_handle
*notification_thread_handle
,
3380 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3381 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3384 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3385 if (!reply_context
) {
3386 ret
= LTTNG_ERR_NOMEM
;
3390 reply_context
->reply_sock_fd
= *sock_fd
;
3394 LTTNG_ASSERT(session
);
3396 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3398 if (session
->active
) {
3399 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3401 ret
= cmd_stop_trace(session
);
3402 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3403 /* Carry on with the destruction of the session. */
3404 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3405 session
->name
, lttng_strerror(-ret
));
3406 destruction_last_error
= (lttng_error_code
) ret
;
3410 if (session
->rotation_schedule_timer_enabled
) {
3411 if (timer_session_rotation_schedule_timer_stop(
3413 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3415 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3419 if (session
->rotate_size
) {
3420 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3421 session
->rotate_size
= 0;
3424 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3426 * Perform a last rotation on destruction if rotations have
3427 * occurred during the session's lifetime.
3429 ret
= cmd_rotate_session(session
, NULL
, false,
3430 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3431 if (ret
!= LTTNG_OK
) {
3432 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3433 session
->name
, lttng_strerror(-ret
));
3434 destruction_last_error
= (lttng_error_code
) -ret
;
3436 if (reply_context
) {
3437 reply_context
->implicit_rotation_on_destroy
= true;
3439 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3441 * The user has not triggered a session rotation. However, to
3442 * ensure all data has been consumed, the session is rotated
3443 * to a 'null' trace chunk before it is destroyed.
3445 * This is a "quiet" rotation meaning that no notification is
3446 * emitted and no renaming of the current trace chunk takes
3449 ret
= cmd_rotate_session(session
, NULL
, true,
3450 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3452 * Rotation operations may not be supported by the kernel
3453 * tracer. Hence, do not consider this implicit rotation as
3454 * a session destruction error. The library has already stopped
3455 * the session and waited for pending data; there is nothing
3456 * left to do but complete the destruction of the session.
3458 if (ret
!= LTTNG_OK
&&
3459 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3460 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3461 session
->name
, lttng_strerror(ret
));
3462 destruction_last_error
= (lttng_error_code
) -ret
;
3466 if (session
->shm_path
[0]) {
3468 * When a session is created with an explicit shm_path,
3469 * the consumer daemon will create its shared memory files
3470 * at that location and will *not* unlink them. This is normal
3471 * as the intention of that feature is to make it possible
3472 * to retrieve the content of those files should a crash occur.
3474 * To ensure the content of those files can be used, the
3475 * sessiond daemon will replicate the content of the metadata
3476 * cache in a metadata file.
3478 * On clean-up, it is expected that the consumer daemon will
3479 * unlink the shared memory files and that the session daemon
3480 * will unlink the metadata file. Then, the session's directory
3481 * in the shm path can be removed.
3483 * Unfortunately, a flaw in the design of the sessiond's and
3484 * consumerd's tear down of channels makes it impossible to
3485 * determine when the sessiond _and_ the consumerd have both
3486 * destroyed their representation of a channel. For one, the
3487 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3488 * callbacks in both daemons.
3490 * However, it is also impossible for the sessiond to know when
3491 * the consumer daemon is done destroying its channel(s) since
3492 * it occurs as a reaction to the closing of the channel's file
3493 * descriptor. There is no resulting communication initiated
3494 * from the consumerd to the sessiond to confirm that the
3495 * operation is completed (and was successful).
3497 * Until this is all fixed, the session daemon checks for the
3498 * removal of the session's shm path which makes it possible
3499 * to safely advertise a session as having been destroyed.
3501 * Prior to this fix, it was not possible to reliably save
3502 * a session making use of the --shm-path option, destroy it,
3503 * and load it again. This is because the creation of the
3504 * session would fail upon seeing the session's shm path
3505 * already in existence.
3507 * Note that none of the error paths in the check for the
3508 * directory's existence return an error. This is normal
3509 * as there isn't much that can be done. The session will
3510 * be destroyed properly, except that we can't offer the
3511 * guarantee that the same session can be re-created.
3513 current_completion_handler
= &destroy_completion_handler
.handler
;
3514 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3516 sizeof(destroy_completion_handler
.shm_path
));
3521 * The session is destroyed. However, note that the command context
3522 * still holds a reference to the session, thus delaying its destruction
3523 * _at least_ up to the point when that reference is released.
3525 session_destroy(session
);
3526 if (reply_context
) {
3527 reply_context
->destruction_status
= destruction_last_error
;
3528 ret
= session_add_destroy_notifier(session
,
3529 cmd_destroy_session_reply
,
3530 (void *) reply_context
);
3532 ret
= LTTNG_ERR_FATAL
;
3544 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3546 int cmd_register_consumer(struct ltt_session
*session
,
3547 enum lttng_domain_type domain
, const char *sock_path
,
3548 struct consumer_data
*cdata
)
3551 struct consumer_socket
*socket
= NULL
;
3553 LTTNG_ASSERT(session
);
3554 LTTNG_ASSERT(cdata
);
3555 LTTNG_ASSERT(sock_path
);
3558 case LTTNG_DOMAIN_KERNEL
:
3560 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3562 LTTNG_ASSERT(ksess
);
3564 /* Can't register a consumer if there is already one */
3565 if (ksess
->consumer_fds_sent
!= 0) {
3566 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3570 sock
= lttcomm_connect_unix_sock(sock_path
);
3572 ret
= LTTNG_ERR_CONNECT_FAIL
;
3575 cdata
->cmd_sock
= sock
;
3577 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3578 if (socket
== NULL
) {
3581 PERROR("close register consumer");
3583 cdata
->cmd_sock
= -1;
3584 ret
= LTTNG_ERR_FATAL
;
3588 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3589 if (socket
->lock
== NULL
) {
3590 PERROR("zmalloc pthread mutex");
3591 ret
= LTTNG_ERR_FATAL
;
3595 pthread_mutex_init(socket
->lock
, NULL
);
3596 socket
->registered
= 1;
3599 consumer_add_socket(socket
, ksess
->consumer
);
3602 pthread_mutex_lock(&cdata
->pid_mutex
);
3604 pthread_mutex_unlock(&cdata
->pid_mutex
);
3609 /* TODO: Userspace tracing */
3610 ret
= LTTNG_ERR_UND
;
3618 consumer_destroy_socket(socket
);
3624 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3626 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3627 struct lttng_domain
**domains
)
3632 struct lttng_ht_iter iter
;
3634 if (session
->kernel_session
!= NULL
) {
3635 DBG3("Listing domains found kernel domain");
3639 if (session
->ust_session
!= NULL
) {
3640 DBG3("Listing domains found UST global domain");
3644 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3646 if (agt
->being_used
) {
3657 *domains
= calloc
<lttng_domain
>(nb_dom
);
3658 if (*domains
== NULL
) {
3659 ret
= LTTNG_ERR_FATAL
;
3663 if (session
->kernel_session
!= NULL
) {
3664 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3666 /* Kernel session buffer type is always GLOBAL */
3667 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3672 if (session
->ust_session
!= NULL
) {
3673 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3674 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3678 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3680 if (agt
->being_used
) {
3681 (*domains
)[index
].type
= agt
->domain
;
3682 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3692 /* Return negative value to differentiate return code */
3698 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3700 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3701 struct ltt_session
*session
,
3702 struct lttng_payload
*payload
)
3706 struct lttcomm_list_command_header cmd_header
= {};
3707 size_t cmd_header_offset
;
3708 enum lttng_error_code ret_code
;
3713 DBG("Listing channels for session %s", session
->name
);
3715 cmd_header_offset
= payload
->buffer
.size
;
3717 /* Reserve space for command reply header. */
3718 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3719 cmd_header_offset
+ sizeof(cmd_header
));
3721 ret_code
= LTTNG_ERR_NOMEM
;
3726 case LTTNG_DOMAIN_KERNEL
:
3728 /* Kernel channels */
3729 struct ltt_kernel_channel
*kchan
;
3730 if (session
->kernel_session
!= NULL
) {
3731 cds_list_for_each_entry(kchan
,
3732 &session
->kernel_session
->channel_list
.head
, list
) {
3733 uint64_t discarded_events
, lost_packets
;
3734 struct lttng_channel_extended
*extended
;
3736 extended
= (struct lttng_channel_extended
*)
3737 kchan
->channel
->attr
.extended
.ptr
;
3739 ret
= get_kernel_runtime_stats(session
, kchan
,
3740 &discarded_events
, &lost_packets
);
3742 ret_code
= LTTNG_ERR_UNK
;
3747 * Update the discarded_events and lost_packets
3748 * count for the channel
3750 extended
->discarded_events
= discarded_events
;
3751 extended
->lost_packets
= lost_packets
;
3753 ret
= lttng_channel_serialize(
3754 kchan
->channel
, &payload
->buffer
);
3756 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3757 kchan
->channel
->name
);
3758 ret_code
= LTTNG_ERR_UNK
;
3767 case LTTNG_DOMAIN_UST
:
3769 struct lttng_ht_iter iter
;
3770 struct ltt_ust_channel
*uchan
;
3773 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3774 &iter
.iter
, uchan
, node
.node
) {
3775 uint64_t discarded_events
= 0, lost_packets
= 0;
3776 struct lttng_channel
*channel
= NULL
;
3777 struct lttng_channel_extended
*extended
;
3779 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3781 ret_code
= LTTNG_ERR_NOMEM
;
3785 extended
= (struct lttng_channel_extended
*)
3786 channel
->attr
.extended
.ptr
;
3788 ret
= get_ust_runtime_stats(session
, uchan
,
3789 &discarded_events
, &lost_packets
);
3791 lttng_channel_destroy(channel
);
3792 ret_code
= LTTNG_ERR_UNK
;
3796 extended
->discarded_events
= discarded_events
;
3797 extended
->lost_packets
= lost_packets
;
3799 ret
= lttng_channel_serialize(
3800 channel
, &payload
->buffer
);
3802 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3804 lttng_channel_destroy(channel
);
3805 ret_code
= LTTNG_ERR_UNK
;
3809 lttng_channel_destroy(channel
);
3819 if (i
> UINT32_MAX
) {
3820 ERR("Channel count would overflow the channel listing command's reply");
3821 ret_code
= LTTNG_ERR_OVERFLOW
;
3825 /* Update command reply header. */
3826 cmd_header
.count
= (uint32_t) i
;
3827 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3828 sizeof(cmd_header
));
3829 ret_code
= LTTNG_OK
;
3836 * Command LTTNG_LIST_EVENTS processed by the client thread.
3838 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3839 struct ltt_session
*session
,
3841 struct lttng_payload
*reply_payload
)
3843 int buffer_resize_ret
;
3844 enum lttng_error_code ret_code
= LTTNG_OK
;
3845 struct lttcomm_list_command_header reply_command_header
= {};
3846 size_t reply_command_header_offset
;
3847 unsigned int nb_events
= 0;
3849 assert(reply_payload
);
3851 /* Reserve space for command reply header. */
3852 reply_command_header_offset
= reply_payload
->buffer
.size
;
3853 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3854 reply_command_header_offset
+
3855 sizeof(struct lttcomm_list_command_header
));
3856 if (buffer_resize_ret
) {
3857 ret_code
= LTTNG_ERR_NOMEM
;
3862 case LTTNG_DOMAIN_KERNEL
:
3863 if (session
->kernel_session
!= NULL
) {
3864 ret_code
= list_lttng_kernel_events(channel_name
,
3865 session
->kernel_session
, reply_payload
, &nb_events
);
3869 case LTTNG_DOMAIN_UST
:
3871 if (session
->ust_session
!= NULL
) {
3872 ret_code
= list_lttng_ust_global_events(channel_name
,
3873 &session
->ust_session
->domain_global
,
3874 reply_payload
, &nb_events
);
3879 case LTTNG_DOMAIN_LOG4J
:
3880 case LTTNG_DOMAIN_JUL
:
3881 case LTTNG_DOMAIN_PYTHON
:
3882 if (session
->ust_session
) {
3883 struct lttng_ht_iter iter
;
3887 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3888 &iter
.iter
, agt
, node
.node
) {
3889 if (agt
->domain
== domain
) {
3890 ret_code
= list_lttng_agent_events(
3891 agt
, reply_payload
, &nb_events
);
3900 ret_code
= LTTNG_ERR_UND
;
3904 if (nb_events
> UINT32_MAX
) {
3905 ret_code
= LTTNG_ERR_OVERFLOW
;
3909 /* Update command reply header. */
3910 reply_command_header
.count
= (uint32_t) nb_events
;
3911 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
3912 sizeof(reply_command_header
));
3919 * Using the session list, filled a lttng_session array to send back to the
3920 * client for session listing.
3922 * The session list lock MUST be acquired before calling this function. Use
3923 * session_lock_list() and session_unlock_list().
3925 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3926 size_t session_count
, uid_t uid
, gid_t gid
)
3930 struct ltt_session
*session
;
3931 struct ltt_session_list
*list
= session_get_list();
3932 struct lttng_session_extended
*extended
=
3933 (typeof(extended
)) (&sessions
[session_count
]);
3935 DBG("Getting all available session for UID %d GID %d",
3938 * Iterate over session list and append data after the control struct in
3941 cds_list_for_each_entry(session
, &list
->head
, list
) {
3942 if (!session_get(session
)) {
3946 * Only list the sessions the user can control.
3948 if (!session_access_ok(session
, uid
) ||
3949 session
->destroyed
) {
3950 session_put(session
);
3954 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3955 struct ltt_ust_session
*usess
= session
->ust_session
;
3957 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3958 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3959 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3960 ret
= build_network_session_path(sessions
[i
].path
,
3961 sizeof(sessions
[i
].path
), session
);
3963 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3964 session
->consumer
->dst
.session_root_path
);
3967 PERROR("snprintf session path");
3968 session_put(session
);
3972 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3973 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3974 sessions
[i
].enabled
= session
->active
;
3975 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3976 sessions
[i
].live_timer_interval
= session
->live_timer
;
3977 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3978 extended
[i
].creation_time
.is_set
= 1;
3980 session_put(session
);
3985 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3986 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3988 int cmd_data_pending(struct ltt_session
*session
)
3991 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3992 struct ltt_ust_session
*usess
= session
->ust_session
;
3994 LTTNG_ASSERT(session
);
3996 DBG("Data pending for session %s", session
->name
);
3998 /* Session MUST be stopped to ask for data availability. */
3999 if (session
->active
) {
4000 ret
= LTTNG_ERR_SESSION_STARTED
;
4004 * If stopped, just make sure we've started before else the above call
4005 * will always send that there is data pending.
4007 * The consumer assumes that when the data pending command is received,
4008 * the trace has been started before or else no output data is written
4009 * by the streams which is a condition for data pending. So, this is
4010 * *VERY* important that we don't ask the consumer before a start
4013 if (!session
->has_been_started
) {
4019 /* A rotation is still pending, we have to wait. */
4020 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4021 DBG("Rotate still pending for session %s", session
->name
);
4026 if (ksess
&& ksess
->consumer
) {
4027 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4029 /* Data is still being extracted for the kernel. */
4034 if (usess
&& usess
->consumer
) {
4035 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4037 /* Data is still being extracted for the kernel. */
4042 /* Data is ready to be read by a viewer */
4050 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4052 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4054 int cmd_snapshot_add_output(struct ltt_session
*session
,
4055 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4058 struct snapshot_output
*new_output
;
4060 LTTNG_ASSERT(session
);
4061 LTTNG_ASSERT(output
);
4063 DBG("Cmd snapshot add output for session %s", session
->name
);
4066 * Can't create an output if the session is not set in no-output mode.
4068 if (session
->output_traces
) {
4069 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4073 if (session
->has_non_mmap_channel
) {
4074 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4078 /* Only one output is allowed until we have the "tee" feature. */
4079 if (session
->snapshot
.nb_output
== 1) {
4080 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4084 new_output
= snapshot_output_alloc();
4086 ret
= LTTNG_ERR_NOMEM
;
4090 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4091 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4092 &session
->snapshot
);
4094 if (ret
== -ENOMEM
) {
4095 ret
= LTTNG_ERR_NOMEM
;
4097 ret
= LTTNG_ERR_INVALID
;
4103 snapshot_add_output(&session
->snapshot
, new_output
);
4105 *id
= new_output
->id
;
4112 snapshot_output_destroy(new_output
);
4118 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4120 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4122 int cmd_snapshot_del_output(struct ltt_session
*session
,
4123 const struct lttng_snapshot_output
*output
)
4126 struct snapshot_output
*sout
= NULL
;
4128 LTTNG_ASSERT(session
);
4129 LTTNG_ASSERT(output
);
4134 * Permission denied to create an output if the session is not
4135 * set in no output mode.
4137 if (session
->output_traces
) {
4138 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4143 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4145 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4146 } else if (*output
->name
!= '\0') {
4147 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4149 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4152 ret
= LTTNG_ERR_INVALID
;
4156 snapshot_delete_output(&session
->snapshot
, sout
);
4157 snapshot_output_destroy(sout
);
4166 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4168 * If no output is available, outputs is untouched and 0 is returned.
4170 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4172 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4173 struct lttng_snapshot_output
**outputs
)
4176 struct lttng_snapshot_output
*list
= NULL
;
4177 struct lttng_ht_iter iter
;
4178 struct snapshot_output
*output
;
4180 LTTNG_ASSERT(session
);
4181 LTTNG_ASSERT(outputs
);
4183 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4186 * Permission denied to create an output if the session is not
4187 * set in no output mode.
4189 if (session
->output_traces
) {
4190 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4194 if (session
->snapshot
.nb_output
== 0) {
4199 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4201 ret
= -LTTNG_ERR_NOMEM
;
4205 /* Copy list from session to the new list object. */
4207 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4208 output
, node
.node
) {
4209 LTTNG_ASSERT(output
->consumer
);
4210 list
[idx
].id
= output
->id
;
4211 list
[idx
].max_size
= output
->max_size
;
4212 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4213 sizeof(list
[idx
].name
))) {
4214 ret
= -LTTNG_ERR_INVALID
;
4217 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4218 if (lttng_strncpy(list
[idx
].ctrl_url
,
4219 output
->consumer
->dst
.session_root_path
,
4220 sizeof(list
[idx
].ctrl_url
))) {
4221 ret
= -LTTNG_ERR_INVALID
;
4226 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4227 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4229 ret
= -LTTNG_ERR_NOMEM
;
4234 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4235 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4237 ret
= -LTTNG_ERR_NOMEM
;
4246 ret
= session
->snapshot
.nb_output
;
4255 * Check if we can regenerate the metadata for this session.
4256 * Only kernel, UST per-uid and non-live sessions are supported.
4258 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4261 int check_regenerate_metadata_support(struct ltt_session
*session
)
4265 LTTNG_ASSERT(session
);
4267 if (session
->live_timer
!= 0) {
4268 ret
= LTTNG_ERR_LIVE_SESSION
;
4271 if (!session
->active
) {
4272 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4275 if (session
->ust_session
) {
4276 switch (session
->ust_session
->buffer_type
) {
4277 case LTTNG_BUFFER_PER_UID
:
4279 case LTTNG_BUFFER_PER_PID
:
4280 ret
= LTTNG_ERR_PER_PID_SESSION
;
4284 ret
= LTTNG_ERR_UNK
;
4288 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4289 session
->consumer
->relay_minor_version
< 8) {
4290 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4300 int clear_metadata_file(int fd
)
4305 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4306 if (lseek_ret
< 0) {
4312 ret
= ftruncate(fd
, 0);
4314 PERROR("ftruncate");
4323 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4326 struct buffer_reg_uid
*uid_reg
= NULL
;
4327 struct buffer_reg_session
*session_reg
= NULL
;
4330 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4331 struct ust_registry_session
*registry
;
4332 struct ust_registry_channel
*chan
;
4333 struct lttng_ht_iter iter_chan
;
4335 session_reg
= uid_reg
->registry
;
4336 registry
= session_reg
->reg
.ust
;
4338 pthread_mutex_lock(®istry
->lock
);
4339 registry
->metadata_len_sent
= 0;
4340 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4341 registry
->metadata_len
= 0;
4342 registry
->metadata_version
++;
4343 if (registry
->metadata_fd
> 0) {
4344 /* Clear the metadata file's content. */
4345 ret
= clear_metadata_file(registry
->metadata_fd
);
4347 pthread_mutex_unlock(®istry
->lock
);
4352 ret
= ust_metadata_session_statedump(registry
, NULL
,
4353 registry
->major
, registry
->minor
);
4355 pthread_mutex_unlock(®istry
->lock
);
4356 ERR("Failed to generate session metadata (err = %d)",
4360 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4362 struct ust_registry_event
*event
;
4363 struct lttng_ht_iter iter_event
;
4365 chan
->metadata_dumped
= 0;
4367 ret
= ust_metadata_channel_statedump(registry
, chan
);
4369 pthread_mutex_unlock(®istry
->lock
);
4370 ERR("Failed to generate channel metadata "
4374 cds_lfht_for_each_entry(chan
->events
->ht
, &iter_event
.iter
,
4376 event
->metadata_dumped
= 0;
4377 ret
= ust_metadata_event_statedump(registry
,
4380 pthread_mutex_unlock(®istry
->lock
);
4381 ERR("Failed to generate event metadata "
4387 pthread_mutex_unlock(®istry
->lock
);
4396 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4398 * Ask the consumer to truncate the existing metadata file(s) and
4399 * then regenerate the metadata. Live and per-pid sessions are not
4400 * supported and return an error.
4402 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4404 int cmd_regenerate_metadata(struct ltt_session
*session
)
4408 LTTNG_ASSERT(session
);
4410 ret
= check_regenerate_metadata_support(session
);
4415 if (session
->kernel_session
) {
4416 ret
= kernctl_session_regenerate_metadata(
4417 session
->kernel_session
->fd
);
4419 ERR("Failed to regenerate the kernel metadata");
4424 if (session
->ust_session
) {
4425 ret
= ust_regenerate_metadata(session
->ust_session
);
4427 ERR("Failed to regenerate the UST metadata");
4431 DBG("Cmd metadata regenerate for session %s", session
->name
);
4439 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4441 * Ask the tracer to regenerate a new statedump.
4443 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4445 int cmd_regenerate_statedump(struct ltt_session
*session
)
4449 LTTNG_ASSERT(session
);
4451 if (!session
->active
) {
4452 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4456 if (session
->kernel_session
) {
4457 ret
= kernctl_session_regenerate_statedump(
4458 session
->kernel_session
->fd
);
4460 * Currently, the statedump in kernel can only fail if out
4464 if (ret
== -ENOMEM
) {
4465 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4467 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4469 ERR("Failed to regenerate the kernel statedump");
4474 if (session
->ust_session
) {
4475 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4477 * Currently, the statedump in UST always returns 0.
4480 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4481 ERR("Failed to regenerate the UST statedump");
4485 DBG("Cmd regenerate statedump for session %s", session
->name
);
4493 enum lttng_error_code
synchronize_tracer_notifier_register(
4494 struct notification_thread_handle
*notification_thread
,
4495 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4497 enum lttng_error_code ret_code
;
4498 const struct lttng_condition
*condition
=
4499 lttng_trigger_get_const_condition(trigger
);
4500 const char *trigger_name
;
4501 uid_t trigger_owner
;
4502 enum lttng_trigger_status trigger_status
;
4503 const enum lttng_domain_type trigger_domain
=
4504 lttng_trigger_get_underlying_domain_type_restriction(
4507 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4508 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4510 LTTNG_ASSERT(condition
);
4511 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4512 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4514 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4515 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4516 trigger_name
: "(anonymous)";
4518 session_lock_list();
4519 switch (trigger_domain
) {
4520 case LTTNG_DOMAIN_KERNEL
:
4522 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4523 if (ret_code
!= LTTNG_OK
) {
4524 enum lttng_error_code notif_thread_unregister_ret
;
4526 notif_thread_unregister_ret
=
4527 notification_thread_command_unregister_trigger(
4528 notification_thread
, trigger
);
4530 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4531 /* Return the original error code. */
4532 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4534 (int) trigger_owner
,
4540 case LTTNG_DOMAIN_UST
:
4541 ust_app_global_update_all_event_notifier_rules();
4543 case LTTNG_DOMAIN_JUL
:
4544 case LTTNG_DOMAIN_LOG4J
:
4545 case LTTNG_DOMAIN_PYTHON
:
4547 /* Agent domains. */
4548 struct agent
*agt
= agent_find_by_event_notifier_domain(
4552 agt
= agent_create(trigger_domain
);
4554 ret_code
= LTTNG_ERR_NOMEM
;
4555 goto end_unlock_session_list
;
4558 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4561 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4562 if (ret_code
!= LTTNG_OK
) {
4563 goto end_unlock_session_list
;
4568 case LTTNG_DOMAIN_NONE
:
4573 ret_code
= LTTNG_OK
;
4574 end_unlock_session_list
:
4575 session_unlock_list();
4579 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4580 struct lttng_trigger
*trigger
,
4581 bool is_trigger_anonymous
,
4582 struct notification_thread_handle
*notification_thread
,
4583 struct lttng_trigger
**return_trigger
)
4585 enum lttng_error_code ret_code
;
4586 const char *trigger_name
;
4587 uid_t trigger_owner
;
4588 enum lttng_trigger_status trigger_status
;
4590 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4591 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4592 trigger_name
: "(anonymous)";
4594 trigger_status
= lttng_trigger_get_owner_uid(
4595 trigger
, &trigger_owner
);
4596 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4598 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4599 trigger_name
, (int) trigger_owner
,
4600 (int) lttng_credentials_get_uid(cmd_creds
));
4603 * Validate the trigger credentials against the command credentials.
4604 * Only the root user can register a trigger with non-matching
4607 if (!lttng_credentials_is_equal_uid(
4608 lttng_trigger_get_credentials(trigger
),
4610 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4611 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4612 trigger_name
, (int) trigger_owner
,
4613 (int) lttng_credentials_get_uid(cmd_creds
));
4614 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4620 * The bytecode generation also serves as a validation step for the
4621 * bytecode expressions.
4623 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4624 if (ret_code
!= LTTNG_OK
) {
4625 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4626 trigger_name
, (int) trigger_owner
, ret_code
);
4631 * A reference to the trigger is acquired by the notification thread.
4632 * It is safe to return the same trigger to the caller since it the
4633 * other user holds a reference.
4635 * The trigger is modified during the execution of the
4636 * "register trigger" command. However, by the time the command returns,
4637 * it is safe to use without any locking as its properties are
4640 ret_code
= notification_thread_command_register_trigger(
4641 notification_thread
, trigger
, is_trigger_anonymous
);
4642 if (ret_code
!= LTTNG_OK
) {
4643 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4644 trigger_name
, (int) trigger_owner
, ret_code
);
4648 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4649 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4650 trigger_name
: "(anonymous)";
4653 * Synchronize tracers if the trigger adds an event notifier.
4655 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4656 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4657 trigger
, cmd_creds
);
4658 if (ret_code
!= LTTNG_OK
) {
4659 ERR("Error registering tracer notifier: %s",
4660 lttng_strerror(-ret_code
));
4666 * Return an updated trigger to the client.
4668 * Since a modified version of the same trigger is returned, acquire a
4669 * reference to the trigger so the caller doesn't have to care if those
4670 * are distinct instances or not.
4672 if (ret_code
== LTTNG_OK
) {
4673 lttng_trigger_get(trigger
);
4674 *return_trigger
= trigger
;
4675 /* Ownership of trigger was transferred to caller. */
4683 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4684 const struct lttng_trigger
*trigger
)
4686 enum lttng_error_code ret_code
;
4687 const struct lttng_condition
*condition
=
4688 lttng_trigger_get_const_condition(trigger
);
4689 const enum lttng_domain_type trigger_domain
=
4690 lttng_trigger_get_underlying_domain_type_restriction(
4693 LTTNG_ASSERT(condition
);
4694 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4695 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4697 session_lock_list();
4698 switch (trigger_domain
) {
4699 case LTTNG_DOMAIN_KERNEL
:
4700 ret_code
= kernel_unregister_event_notifier(trigger
);
4701 if (ret_code
!= LTTNG_OK
) {
4702 goto end_unlock_session_list
;
4706 case LTTNG_DOMAIN_UST
:
4707 ust_app_global_update_all_event_notifier_rules();
4709 case LTTNG_DOMAIN_JUL
:
4710 case LTTNG_DOMAIN_LOG4J
:
4711 case LTTNG_DOMAIN_PYTHON
:
4713 /* Agent domains. */
4714 struct agent
*agt
= agent_find_by_event_notifier_domain(
4718 * This trigger was never registered in the first place. Calling
4719 * this function under those circumstances is an internal error.
4722 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4723 if (ret_code
!= LTTNG_OK
) {
4724 goto end_unlock_session_list
;
4729 case LTTNG_DOMAIN_NONE
:
4734 ret_code
= LTTNG_OK
;
4736 end_unlock_session_list
:
4737 session_unlock_list();
4741 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4742 const struct lttng_trigger
*trigger
,
4743 struct notification_thread_handle
*notification_thread
)
4745 enum lttng_error_code ret_code
;
4746 const char *trigger_name
;
4747 uid_t trigger_owner
;
4748 enum lttng_trigger_status trigger_status
;
4749 struct lttng_trigger
*sessiond_trigger
= NULL
;
4751 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4752 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4753 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4754 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4756 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4757 trigger_name
, (int) trigger_owner
,
4758 (int) lttng_credentials_get_uid(cmd_creds
));
4761 * Validate the trigger credentials against the command credentials.
4762 * Only the root user can unregister a trigger with non-matching
4765 if (!lttng_credentials_is_equal_uid(
4766 lttng_trigger_get_credentials(trigger
),
4768 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4769 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4770 trigger_name
, (int) trigger_owner
,
4771 (int) lttng_credentials_get_uid(cmd_creds
));
4772 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4777 /* Fetch the sessiond side trigger object. */
4778 ret_code
= notification_thread_command_get_trigger(
4779 notification_thread
, trigger
, &sessiond_trigger
);
4780 if (ret_code
!= LTTNG_OK
) {
4781 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4782 trigger_name
, (int) trigger_owner
, ret_code
);
4786 LTTNG_ASSERT(sessiond_trigger
);
4789 * From this point on, no matter what, consider the trigger
4792 * We set the unregistered state of the sessiond side trigger object in
4793 * the client thread since we want to minimize the possibility of the
4794 * notification thread being stalled due to a long execution of an
4795 * action that required the trigger lock.
4797 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4799 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4801 if (ret_code
!= LTTNG_OK
) {
4802 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4803 trigger_name
, (int) trigger_owner
, ret_code
);
4808 * Synchronize tracers if the trigger removes an event notifier.
4809 * Do this even if the trigger unregistration failed to at least stop
4810 * the tracers from producing notifications associated with this
4813 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4814 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4815 if (ret_code
!= LTTNG_OK
) {
4816 ERR("Error unregistering trigger to tracer.");
4823 lttng_trigger_put(sessiond_trigger
);
4827 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4828 struct notification_thread_handle
*notification_thread
,
4829 struct lttng_triggers
**return_triggers
)
4832 enum lttng_error_code ret_code
;
4833 struct lttng_triggers
*triggers
= NULL
;
4835 /* Get the set of triggers from the notification thread. */
4836 ret_code
= notification_thread_command_list_triggers(
4837 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4838 if (ret_code
!= LTTNG_OK
) {
4842 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4844 ret_code
= LTTNG_ERR_UNK
;
4848 *return_triggers
= triggers
;
4850 ret_code
= LTTNG_OK
;
4852 lttng_triggers_destroy(triggers
);
4856 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4857 const struct lttng_error_query
*query
,
4858 struct lttng_error_query_results
**_results
,
4859 struct notification_thread_handle
*notification_thread
)
4861 enum lttng_error_code ret_code
;
4862 const struct lttng_trigger
*query_target_trigger
;
4863 const struct lttng_action
*query_target_action
= NULL
;
4864 struct lttng_trigger
*matching_trigger
= NULL
;
4865 const char *trigger_name
;
4866 uid_t trigger_owner
;
4867 enum lttng_trigger_status trigger_status
;
4868 struct lttng_error_query_results
*results
= NULL
;
4870 switch (lttng_error_query_get_target_type(query
)) {
4871 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4872 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4874 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4875 query_target_trigger
=
4876 lttng_error_query_condition_borrow_target(query
);
4878 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4879 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4886 LTTNG_ASSERT(query_target_trigger
);
4888 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4889 query_target_trigger
, &matching_trigger
);
4890 if (ret_code
!= LTTNG_OK
) {
4894 /* No longer needed. */
4895 query_target_trigger
= NULL
;
4897 if (lttng_error_query_get_target_type(query
) ==
4898 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4899 /* Get the sessiond-side version of the target action. */
4900 query_target_action
=
4901 lttng_error_query_action_borrow_action_target(
4902 query
, matching_trigger
);
4905 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4906 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4907 trigger_name
: "(anonymous)";
4908 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4910 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4912 results
= lttng_error_query_results_create();
4914 ret_code
= LTTNG_ERR_NOMEM
;
4918 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4919 trigger_name
, (int) trigger_owner
,
4920 (int) lttng_credentials_get_uid(cmd_creds
));
4923 * Validate the trigger credentials against the command credentials.
4924 * Only the root user can target a trigger with non-matching
4927 if (!lttng_credentials_is_equal_uid(
4928 lttng_trigger_get_credentials(matching_trigger
),
4930 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4931 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4932 trigger_name
, (int) trigger_owner
,
4933 (int) lttng_credentials_get_uid(cmd_creds
));
4934 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4939 switch (lttng_error_query_get_target_type(query
)) {
4940 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4941 trigger_status
= lttng_trigger_add_error_results(
4942 matching_trigger
, results
);
4944 switch (trigger_status
) {
4945 case LTTNG_TRIGGER_STATUS_OK
:
4948 ret_code
= LTTNG_ERR_UNK
;
4953 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4955 trigger_status
= lttng_trigger_condition_add_error_results(
4956 matching_trigger
, results
);
4958 switch (trigger_status
) {
4959 case LTTNG_TRIGGER_STATUS_OK
:
4962 ret_code
= LTTNG_ERR_UNK
;
4968 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4970 const enum lttng_action_status action_status
=
4971 lttng_action_add_error_query_results(
4972 query_target_action
, results
);
4974 switch (action_status
) {
4975 case LTTNG_ACTION_STATUS_OK
:
4978 ret_code
= LTTNG_ERR_UNK
;
4989 *_results
= results
;
4991 ret_code
= LTTNG_OK
;
4993 lttng_trigger_put(matching_trigger
);
4994 lttng_error_query_results_destroy(results
);
4999 * Send relayd sockets from snapshot output to consumer. Ignore request if the
5000 * snapshot output is *not* set with a remote destination.
5002 * Return LTTNG_OK on success or a LTTNG_ERR code.
5004 static enum lttng_error_code
set_relayd_for_snapshot(
5005 struct consumer_output
*output
,
5006 const struct ltt_session
*session
)
5008 enum lttng_error_code status
= LTTNG_OK
;
5009 struct lttng_ht_iter iter
;
5010 struct consumer_socket
*socket
;
5011 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
5012 const char *base_path
;
5014 LTTNG_ASSERT(output
);
5015 LTTNG_ASSERT(session
);
5017 DBG2("Set relayd object from snapshot output");
5019 if (session
->current_trace_chunk
) {
5020 enum lttng_trace_chunk_status chunk_status
=
5021 lttng_trace_chunk_get_id(
5022 session
->current_trace_chunk
,
5023 ¤t_chunk_id
.value
);
5025 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5026 current_chunk_id
.is_set
= true;
5028 ERR("Failed to get current trace chunk id");
5029 status
= LTTNG_ERR_UNK
;
5034 /* Ignore if snapshot consumer output is not network. */
5035 if (output
->type
!= CONSUMER_DST_NET
) {
5040 * The snapshot record URI base path overrides the session
5043 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5044 base_path
= output
->dst
.net
.control
.subdir
;
5046 base_path
= session
->base_path
;
5050 * For each consumer socket, create and send the relayd object of the
5054 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
5055 socket
, node
.node
) {
5056 pthread_mutex_lock(socket
->lock
);
5057 status
= send_consumer_relayd_sockets(session
->id
,
5059 session
->name
, session
->hostname
,
5061 session
->live_timer
,
5062 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
5063 session
->creation_time
,
5064 session
->name_contains_creation_time
);
5065 pthread_mutex_unlock(socket
->lock
);
5066 if (status
!= LTTNG_OK
) {
5078 * Record a kernel snapshot.
5080 * Return LTTNG_OK on success or a LTTNG_ERR code.
5082 static enum lttng_error_code
record_kernel_snapshot(
5083 struct ltt_kernel_session
*ksess
,
5084 const struct consumer_output
*output
,
5085 const struct ltt_session
*session
,
5086 uint64_t nb_packets_per_stream
)
5088 enum lttng_error_code status
;
5090 LTTNG_ASSERT(ksess
);
5091 LTTNG_ASSERT(output
);
5092 LTTNG_ASSERT(session
);
5094 status
= kernel_snapshot_record(
5095 ksess
, output
, nb_packets_per_stream
);
5100 * Record a UST snapshot.
5102 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5104 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5105 const struct consumer_output
*output
,
5106 const struct ltt_session
*session
,
5107 uint64_t nb_packets_per_stream
)
5109 enum lttng_error_code status
;
5111 LTTNG_ASSERT(usess
);
5112 LTTNG_ASSERT(output
);
5113 LTTNG_ASSERT(session
);
5115 status
= ust_app_snapshot_record(
5116 usess
, output
, nb_packets_per_stream
);
5121 uint64_t get_session_size_one_more_packet_per_stream(
5122 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5124 uint64_t tot_size
= 0;
5126 if (session
->kernel_session
) {
5127 struct ltt_kernel_channel
*chan
;
5128 const struct ltt_kernel_session
*ksess
=
5129 session
->kernel_session
;
5131 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5132 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5134 * Don't take channel into account if we
5135 * already grab all its packets.
5139 tot_size
+= chan
->channel
->attr
.subbuf_size
5140 * chan
->stream_count
;
5144 if (session
->ust_session
) {
5145 const struct ltt_ust_session
*usess
= session
->ust_session
;
5147 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5155 * Calculate the number of packets we can grab from each stream that
5156 * fits within the overall snapshot max size.
5158 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5159 * the number of packets per stream.
5161 * TODO: this approach is not perfect: we consider the worse case
5162 * (packet filling the sub-buffers) as an upper bound, but we could do
5163 * better if we do this calculation while we actually grab the packet
5164 * content: we would know how much padding we don't actually store into
5167 * This algorithm is currently bounded by the number of packets per
5170 * Since we call this algorithm before actually grabbing the data, it's
5171 * an approximation: for instance, applications could appear/disappear
5172 * in between this call and actually grabbing data.
5175 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5179 uint64_t cur_nb_packets
= 0;
5182 return 0; /* Infinite */
5185 size_left
= max_size
;
5187 uint64_t one_more_packet_tot_size
;
5189 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5190 session
, cur_nb_packets
);
5191 if (!one_more_packet_tot_size
) {
5192 /* We are already grabbing all packets. */
5195 size_left
-= one_more_packet_tot_size
;
5196 if (size_left
< 0) {
5201 if (!cur_nb_packets
&& size_left
!= max_size
) {
5202 /* Not enough room to grab one packet of each stream, error. */
5205 return cur_nb_packets
;
5209 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5210 const struct snapshot_output
*snapshot_output
)
5212 int64_t nb_packets_per_stream
;
5213 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5215 enum lttng_error_code ret_code
= LTTNG_OK
;
5216 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5217 struct consumer_output
*original_ust_consumer_output
= NULL
;
5218 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5219 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5220 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5222 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5224 snapshot_output
->name
,
5225 snapshot_output
->datetime
,
5226 snapshot_output
->nb_snapshot
);
5227 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5228 ERR("Failed to format snapshot name");
5229 ret_code
= LTTNG_ERR_INVALID
;
5232 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5233 snapshot_output
->name
, session
->name
,
5234 snapshot_chunk_name
);
5235 if (!session
->kernel_session
&& !session
->ust_session
) {
5236 ERR("Failed to record snapshot as no channels exist");
5237 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5241 if (session
->kernel_session
) {
5242 original_kernel_consumer_output
=
5243 session
->kernel_session
->consumer
;
5244 snapshot_kernel_consumer_output
=
5245 consumer_copy_output(snapshot_output
->consumer
);
5246 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5247 snapshot_chunk_name
);
5249 /* Copy the original domain subdir. */
5250 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5251 original_kernel_consumer_output
->domain_subdir
);
5253 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5254 original_kernel_consumer_output
);
5256 ERR("Failed to copy consumer sockets from snapshot output configuration");
5257 ret_code
= LTTNG_ERR_NOMEM
;
5260 ret_code
= set_relayd_for_snapshot(
5261 snapshot_kernel_consumer_output
, session
);
5262 if (ret_code
!= LTTNG_OK
) {
5263 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5266 session
->kernel_session
->consumer
=
5267 snapshot_kernel_consumer_output
;
5269 if (session
->ust_session
) {
5270 original_ust_consumer_output
= session
->ust_session
->consumer
;
5271 snapshot_ust_consumer_output
=
5272 consumer_copy_output(snapshot_output
->consumer
);
5273 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5274 snapshot_chunk_name
);
5276 /* Copy the original domain subdir. */
5277 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5278 original_ust_consumer_output
->domain_subdir
);
5280 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5281 original_ust_consumer_output
);
5283 ERR("Failed to copy consumer sockets from snapshot output configuration");
5284 ret_code
= LTTNG_ERR_NOMEM
;
5287 ret_code
= set_relayd_for_snapshot(
5288 snapshot_ust_consumer_output
, session
);
5289 if (ret_code
!= LTTNG_OK
) {
5290 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5293 session
->ust_session
->consumer
=
5294 snapshot_ust_consumer_output
;
5297 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5298 snapshot_kernel_consumer_output
?:
5299 snapshot_ust_consumer_output
,
5300 consumer_output_get_base_path(
5301 snapshot_output
->consumer
),
5302 snapshot_chunk_name
);
5303 if (!snapshot_trace_chunk
) {
5304 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5306 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5309 LTTNG_ASSERT(!session
->current_trace_chunk
);
5310 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5311 lttng_trace_chunk_put(snapshot_trace_chunk
);
5312 snapshot_trace_chunk
= NULL
;
5314 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5316 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5320 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5321 snapshot_output
->max_size
);
5322 if (nb_packets_per_stream
< 0) {
5323 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5324 goto error_close_trace_chunk
;
5327 if (session
->kernel_session
) {
5328 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5329 snapshot_kernel_consumer_output
, session
,
5330 nb_packets_per_stream
);
5331 if (ret_code
!= LTTNG_OK
) {
5332 goto error_close_trace_chunk
;
5336 if (session
->ust_session
) {
5337 ret_code
= record_ust_snapshot(session
->ust_session
,
5338 snapshot_ust_consumer_output
, session
,
5339 nb_packets_per_stream
);
5340 if (ret_code
!= LTTNG_OK
) {
5341 goto error_close_trace_chunk
;
5345 error_close_trace_chunk
:
5346 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5347 ERR("Failed to release the current trace chunk of session \"%s\"",
5349 ret_code
= LTTNG_ERR_UNK
;
5352 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5353 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5355 * Don't goto end; make sure the chunk is closed for the session
5356 * to allow future snapshots.
5358 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5360 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5363 lttng_trace_chunk_put(snapshot_trace_chunk
);
5364 snapshot_trace_chunk
= NULL
;
5366 if (original_ust_consumer_output
) {
5367 session
->ust_session
->consumer
= original_ust_consumer_output
;
5369 if (original_kernel_consumer_output
) {
5370 session
->kernel_session
->consumer
=
5371 original_kernel_consumer_output
;
5373 consumer_output_put(snapshot_ust_consumer_output
);
5374 consumer_output_put(snapshot_kernel_consumer_output
);
5379 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5381 * The wait parameter is ignored so this call always wait for the snapshot to
5382 * complete before returning.
5384 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5386 int cmd_snapshot_record(struct ltt_session
*session
,
5387 const struct lttng_snapshot_output
*output
,
5388 int wait
__attribute__((unused
)))
5390 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5392 unsigned int snapshot_success
= 0;
5394 struct snapshot_output
*tmp_output
= NULL
;
5396 LTTNG_ASSERT(session
);
5397 LTTNG_ASSERT(output
);
5399 DBG("Cmd snapshot record for session %s", session
->name
);
5401 /* Get the datetime for the snapshot output directory. */
5402 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5405 cmd_ret
= LTTNG_ERR_INVALID
;
5410 * Permission denied to create an output if the session is not
5411 * set in no output mode.
5413 if (session
->output_traces
) {
5414 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5418 /* The session needs to be started at least once. */
5419 if (!session
->has_been_started
) {
5420 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5424 /* Use temporary output for the session. */
5425 if (*output
->ctrl_url
!= '\0') {
5426 tmp_output
= snapshot_output_alloc();
5428 cmd_ret
= LTTNG_ERR_NOMEM
;
5432 ret
= snapshot_output_init(session
, output
->max_size
,
5434 output
->ctrl_url
, output
->data_url
,
5438 if (ret
== -ENOMEM
) {
5439 cmd_ret
= LTTNG_ERR_NOMEM
;
5441 cmd_ret
= LTTNG_ERR_INVALID
;
5445 /* Use the global session count for the temporary snapshot. */
5446 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5448 /* Use the global datetime */
5449 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5450 cmd_ret
= snapshot_record(session
, tmp_output
);
5451 if (cmd_ret
!= LTTNG_OK
) {
5454 snapshot_success
= 1;
5456 struct snapshot_output
*sout
;
5457 struct lttng_ht_iter iter
;
5460 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5461 &iter
.iter
, sout
, node
.node
) {
5462 struct snapshot_output output_copy
;
5465 * Make a local copy of the output and override output
5466 * parameters with those provided as part of the
5469 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5471 if (output
->max_size
!= (uint64_t) -1ULL) {
5472 output_copy
.max_size
= output
->max_size
;
5475 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5476 memcpy(output_copy
.datetime
, datetime
,
5479 /* Use temporary name. */
5480 if (*output
->name
!= '\0') {
5481 if (lttng_strncpy(output_copy
.name
,
5483 sizeof(output_copy
.name
))) {
5484 cmd_ret
= LTTNG_ERR_INVALID
;
5490 cmd_ret
= snapshot_record(session
, &output_copy
);
5491 if (cmd_ret
!= LTTNG_OK
) {
5495 snapshot_success
= 1;
5500 if (snapshot_success
) {
5501 session
->snapshot
.nb_snapshot
++;
5503 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5508 snapshot_output_destroy(tmp_output
);
5514 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5516 int cmd_set_session_shm_path(struct ltt_session
*session
,
5517 const char *shm_path
)
5520 LTTNG_ASSERT(session
);
5523 * Can only set shm path before session is started.
5525 if (session
->has_been_started
) {
5526 return LTTNG_ERR_SESSION_STARTED
;
5529 strncpy(session
->shm_path
, shm_path
,
5530 sizeof(session
->shm_path
));
5531 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5537 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5539 * Ask the consumer to rotate the session output directory.
5540 * The session lock must be held.
5542 * Returns LTTNG_OK on success or else a negative LTTng error code.
5544 int cmd_rotate_session(struct ltt_session
*session
,
5545 struct lttng_rotate_session_return
*rotate_return
,
5546 bool quiet_rotation
,
5547 enum lttng_trace_chunk_command_type command
)
5550 uint64_t ongoing_rotation_chunk_id
;
5551 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5552 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5553 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5554 enum lttng_trace_chunk_status chunk_status
;
5555 bool failed_to_rotate
= false;
5556 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5558 LTTNG_ASSERT(session
);
5560 if (!session
->has_been_started
) {
5561 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5566 * Explicit rotation is not supported for live sessions.
5567 * However, live sessions can perform a quiet rotation on
5569 * Rotation is not supported for snapshot traces (no output).
5571 if ((!quiet_rotation
&& session
->live_timer
) ||
5572 !session
->output_traces
) {
5573 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5577 /* Unsupported feature in lttng-relayd before 2.11. */
5578 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5579 (session
->consumer
->relay_major_version
== 2 &&
5580 session
->consumer
->relay_minor_version
< 11)) {
5581 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5585 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5586 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5587 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5591 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5592 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5594 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5599 * After a stop, we only allow one rotation to occur, the other ones are
5600 * useless until a new start.
5602 if (session
->rotated_after_last_stop
) {
5603 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5605 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5610 * After a stop followed by a clear, disallow following rotations a they would
5611 * generate empty chunks.
5613 if (session
->cleared_after_last_stop
) {
5614 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5616 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5620 if (session
->active
) {
5621 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5623 if (!new_trace_chunk
) {
5624 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5630 * The current trace chunk becomes the chunk being archived.
5632 * After this point, "chunk_being_archived" must absolutely
5633 * be closed on the consumer(s), otherwise it will never be
5634 * cleaned-up, which will result in a leak.
5636 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5637 &chunk_being_archived
);
5639 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5643 if (session
->kernel_session
) {
5644 cmd_ret
= kernel_rotate_session(session
);
5645 if (cmd_ret
!= LTTNG_OK
) {
5646 failed_to_rotate
= true;
5647 rotation_fail_code
= cmd_ret
;
5650 if (session
->ust_session
) {
5651 cmd_ret
= ust_app_rotate_session(session
);
5652 if (cmd_ret
!= LTTNG_OK
) {
5653 failed_to_rotate
= true;
5654 rotation_fail_code
= cmd_ret
;
5658 if (!session
->active
) {
5659 session
->rotated_after_last_stop
= true;
5662 if (!chunk_being_archived
) {
5663 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5665 if (failed_to_rotate
) {
5666 cmd_ret
= rotation_fail_code
;
5673 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5674 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5675 &ongoing_rotation_chunk_id
);
5676 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5678 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5679 command
, session
->last_chunk_path
);
5681 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5685 if (failed_to_rotate
) {
5686 cmd_ret
= rotation_fail_code
;
5690 session
->quiet_rotation
= quiet_rotation
;
5691 ret
= timer_session_rotation_pending_check_start(session
,
5692 DEFAULT_ROTATE_PENDING_TIMER
);
5694 cmd_ret
= LTTNG_ERR_UNK
;
5698 if (rotate_return
) {
5699 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5702 session
->chunk_being_archived
= chunk_being_archived
;
5703 chunk_being_archived
= NULL
;
5704 if (!quiet_rotation
) {
5705 ret
= notification_thread_command_session_rotation_ongoing(
5706 the_notification_thread_handle
, session
->name
,
5707 session
->uid
, session
->gid
,
5708 ongoing_rotation_chunk_id
);
5709 if (ret
!= LTTNG_OK
) {
5710 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5712 cmd_ret
= (lttng_error_code
) ret
;
5716 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5717 session
->name
, ongoing_rotation_chunk_id
);
5719 lttng_trace_chunk_put(new_trace_chunk
);
5720 lttng_trace_chunk_put(chunk_being_archived
);
5721 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5724 if (session_reset_rotation_state(session
,
5725 LTTNG_ROTATION_STATE_ERROR
)) {
5726 ERR("Failed to reset rotation state of session \"%s\"",
5733 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5735 * Check if the session has finished its rotation.
5737 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5739 int cmd_rotate_get_info(struct ltt_session
*session
,
5740 struct lttng_rotation_get_info_return
*info_return
,
5741 uint64_t rotation_id
)
5743 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5744 enum lttng_rotation_state rotation_state
;
5746 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5747 session
->most_recent_chunk_id
.value
);
5749 if (session
->chunk_being_archived
) {
5750 enum lttng_trace_chunk_status chunk_status
;
5753 chunk_status
= lttng_trace_chunk_get_id(
5754 session
->chunk_being_archived
,
5756 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5758 rotation_state
= rotation_id
== chunk_id
?
5759 LTTNG_ROTATION_STATE_ONGOING
:
5760 LTTNG_ROTATION_STATE_EXPIRED
;
5762 if (session
->last_archived_chunk_id
.is_set
&&
5763 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5764 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5766 rotation_state
= session
->rotation_state
;
5770 switch (rotation_state
) {
5771 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5772 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5775 case LTTNG_ROTATION_STATE_EXPIRED
:
5776 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5777 rotation_id
, session
->name
);
5779 case LTTNG_ROTATION_STATE_ONGOING
:
5780 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5781 rotation_id
, session
->name
);
5783 case LTTNG_ROTATION_STATE_COMPLETED
:
5787 char *current_tracing_path_reply
;
5788 size_t current_tracing_path_reply_len
;
5790 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5791 rotation_id
, session
->name
);
5793 switch (session_get_consumer_destination_type(session
)) {
5794 case CONSUMER_DST_LOCAL
:
5795 current_tracing_path_reply
=
5796 info_return
->location
.local
.absolute_path
;
5797 current_tracing_path_reply_len
=
5798 sizeof(info_return
->location
.local
.absolute_path
);
5799 info_return
->location_type
=
5800 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5801 fmt_ret
= asprintf(&chunk_path
,
5802 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5803 session_get_base_path(session
),
5804 session
->last_archived_chunk_name
);
5805 if (fmt_ret
== -1) {
5806 PERROR("Failed to format the path of the last archived trace chunk");
5807 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5808 cmd_ret
= LTTNG_ERR_UNK
;
5812 case CONSUMER_DST_NET
:
5814 uint16_t ctrl_port
, data_port
;
5816 current_tracing_path_reply
=
5817 info_return
->location
.relay
.relative_path
;
5818 current_tracing_path_reply_len
=
5819 sizeof(info_return
->location
.relay
.relative_path
);
5820 /* Currently the only supported relay protocol. */
5821 info_return
->location
.relay
.protocol
=
5822 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5824 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5825 session_get_net_consumer_hostname(session
),
5826 sizeof(info_return
->location
.relay
.host
));
5828 ERR("Failed to copy host name to rotate_get_info reply");
5829 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5830 cmd_ret
= LTTNG_ERR_SET_URL
;
5834 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5835 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5836 info_return
->location
.relay
.ports
.data
= data_port
;
5837 info_return
->location_type
=
5838 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5839 chunk_path
= strdup(session
->last_chunk_path
);
5841 ERR("Failed to allocate the path of the last archived trace chunk");
5842 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5843 cmd_ret
= LTTNG_ERR_UNK
;
5852 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5853 chunk_path
, current_tracing_path_reply_len
);
5856 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5857 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5858 cmd_ret
= LTTNG_ERR_UNK
;
5864 case LTTNG_ROTATION_STATE_ERROR
:
5865 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5866 rotation_id
, session
->name
);
5874 info_return
->status
= (int32_t) rotation_state
;
5879 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5881 * Configure the automatic rotation parameters.
5882 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5883 * 'activate' to false means deactivate the rotation schedule and validate that
5884 * 'new_value' has the same value as the currently active value.
5886 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5888 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5889 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5891 struct notification_thread_handle
*notification_thread_handle
)
5894 uint64_t *parameter_value
;
5896 LTTNG_ASSERT(session
);
5898 DBG("Cmd rotate set schedule session %s", session
->name
);
5900 if (session
->live_timer
|| !session
->output_traces
) {
5901 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5902 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5906 switch (schedule_type
) {
5907 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5908 parameter_value
= &session
->rotate_size
;
5910 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5911 parameter_value
= &session
->rotate_timer_period
;
5912 if (new_value
>= UINT_MAX
) {
5913 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5914 new_value
, UINT_MAX
);
5915 ret
= LTTNG_ERR_INVALID
;
5920 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5921 ret
= LTTNG_ERR_INVALID
;
5925 /* Improper use of the API. */
5926 if (new_value
== -1ULL) {
5927 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5928 ret
= LTTNG_ERR_INVALID
;
5933 * As indicated in struct ltt_session's comments, a value of == 0 means
5934 * this schedule rotation type is not in use.
5936 * Reject the command if we were asked to activate a schedule that was
5939 if (activate
&& *parameter_value
!= 0) {
5940 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5941 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5946 * Reject the command if we were asked to deactivate a schedule that was
5949 if (!activate
&& *parameter_value
== 0) {
5950 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5951 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5956 * Reject the command if we were asked to deactivate a schedule that
5959 if (!activate
&& *parameter_value
!= new_value
) {
5960 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5961 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5965 *parameter_value
= activate
? new_value
: 0;
5967 switch (schedule_type
) {
5968 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5969 if (activate
&& session
->active
) {
5971 * Only start the timer if the session is active,
5972 * otherwise it will be started when the session starts.
5974 ret
= timer_session_rotation_schedule_timer_start(
5975 session
, new_value
);
5977 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5978 ret
= LTTNG_ERR_UNK
;
5982 ret
= timer_session_rotation_schedule_timer_stop(
5985 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5986 ret
= LTTNG_ERR_UNK
;
5991 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5993 ret
= subscribe_session_consumed_size_rotation(session
,
5994 new_value
, notification_thread_handle
);
5996 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5997 ret
= LTTNG_ERR_UNK
;
6001 ret
= unsubscribe_session_consumed_size_rotation(session
,
6002 notification_thread_handle
);
6004 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
6005 ret
= LTTNG_ERR_UNK
;
6012 /* Would have been caught before. */
6024 /* Wait for a given path to be removed before continuing. */
6025 static enum lttng_error_code
wait_on_path(void *path_data
)
6027 const char *shm_path
= (const char *) path_data
;
6029 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6035 ret
= stat(shm_path
, &st
);
6037 if (errno
!= ENOENT
) {
6038 PERROR("stat() returned an error while checking for the existence of the shm path");
6040 DBG("shm path no longer exists, completing the destruction of session");
6044 if (!S_ISDIR(st
.st_mode
)) {
6045 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6050 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6056 * Returns a pointer to a handler to run on completion of a command.
6057 * Returns NULL if no handler has to be run for the last command executed.
6059 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6061 struct cmd_completion_handler
*handler
= current_completion_handler
;
6063 current_completion_handler
= NULL
;
6068 * Init command subsystem.
6073 * Set network sequence index to 1 for streams to match a relayd
6074 * socket on the consumer side.
6076 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6077 relayd_net_seq_idx
= 1;
6078 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6080 DBG("Command subsystem initialized");