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
10 #include "agent-thread.hpp"
12 #include "buffer-registry.hpp"
13 #include "channel.hpp"
15 #include "consumer.hpp"
16 #include "event-notifier-error-accounting.hpp"
18 #include "health-sessiond.hpp"
19 #include "kernel-consumer.hpp"
21 #include "lttng-sessiond.hpp"
22 #include "lttng-syscall.hpp"
23 #include "notification-thread-commands.hpp"
24 #include "notification-thread.hpp"
26 #include "rotation-thread.hpp"
27 #include "session.hpp"
29 #include "tracker.hpp"
32 #include <common/buffer-view.hpp>
33 #include <common/common.hpp>
34 #include <common/compat/string.hpp>
35 #include <common/defaults.hpp>
36 #include <common/dynamic-buffer.hpp>
37 #include <common/kernel-ctl/kernel-ctl.hpp>
38 #include <common/payload-view.hpp>
39 #include <common/payload.hpp>
40 #include <common/relayd/relayd.hpp>
41 #include <common/sessiond-comm/sessiond-comm.hpp>
42 #include <common/string-utils/string-utils.hpp>
43 #include <common/trace-chunk.hpp>
44 #include <common/utils.hpp>
46 #include <lttng/action/action-internal.hpp>
47 #include <lttng/action/action.h>
48 #include <lttng/channel-internal.hpp>
49 #include <lttng/channel.h>
50 #include <lttng/condition/condition-internal.hpp>
51 #include <lttng/condition/condition.h>
52 #include <lttng/condition/event-rule-matches-internal.hpp>
53 #include <lttng/condition/event-rule-matches.h>
54 #include <lttng/error-query-internal.hpp>
55 #include <lttng/event-internal.hpp>
56 #include <lttng/event-rule/event-rule-internal.hpp>
57 #include <lttng/event-rule/event-rule.h>
58 #include <lttng/location-internal.hpp>
59 #include <lttng/lttng-error.h>
60 #include <lttng/rotate-internal.hpp>
61 #include <lttng/session-descriptor-internal.hpp>
62 #include <lttng/session-internal.hpp>
63 #include <lttng/tracker.h>
64 #include <lttng/trigger/trigger-internal.hpp>
65 #include <lttng/userspace-probe-internal.hpp>
71 #include <urcu/list.h>
72 #include <urcu/uatomic.h>
74 /* Sleep for 100ms between each check for the shm path's deletion. */
75 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
77 namespace lsu
= lttng::sessiond::ust
;
79 static enum lttng_error_code
wait_on_path(void *path
);
82 struct cmd_destroy_session_reply_context
{
84 bool implicit_rotation_on_destroy
;
86 * Indicates whether or not an error occurred while launching the
87 * destruction of a session.
89 enum lttng_error_code destruction_status
;
93 * Command completion handler that is used by the destroy command
94 * when a session that has a non-default shm_path is being destroyed.
96 * See comment in cmd_destroy_session() for the rationale.
98 struct destroy_completion_handler
{
99 struct cmd_completion_handler handler
;
100 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
101 } destroy_completion_handler
= {
102 .handler
= { .run
= wait_on_path
, .data
= destroy_completion_handler
.shm_path
},
107 * Used to keep a unique index for each relayd socket created where this value
108 * is associated with streams on the consumer so it can match the right relayd
109 * to send to. It must be accessed with the relayd_net_seq_idx_lock
112 pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
113 uint64_t relayd_net_seq_idx
;
116 static struct cmd_completion_handler
*current_completion_handler
;
117 static int validate_ust_event_name(const char *);
118 static int cmd_enable_event_internal(struct ltt_session
*session
,
119 const struct lttng_domain
*domain
,
121 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(struct ltt_session
*session
,
127 const struct lttng_domain
*domain
,
128 const struct lttng_channel
*_attr
,
132 * Create a session path used by list_lttng_sessions for the case that the
133 * session consumer is on the network.
135 static int build_network_session_path(char *dst
, size_t size
, struct ltt_session
*session
)
137 int ret
, kdata_port
, udata_port
;
138 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
139 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
141 LTTNG_ASSERT(session
);
144 memset(tmp_urls
, 0, sizeof(tmp_urls
));
145 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
147 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
149 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
150 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
151 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
154 if (session
->ust_session
&& session
->ust_session
->consumer
) {
155 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
156 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
159 if (uuri
== NULL
&& kuri
== NULL
) {
160 uri
= &session
->consumer
->dst
.net
.control
;
161 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
162 } else if (kuri
&& uuri
) {
163 ret
= uri_compare(kuri
, uuri
);
167 /* Build uuri URL string */
168 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
175 } else if (kuri
&& uuri
== NULL
) {
177 } else if (uuri
&& kuri
== NULL
) {
181 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
187 * Do we have a UST url set. If yes, this means we have both kernel and UST
190 if (*tmp_uurl
!= '\0') {
193 "[K]: %s [data: %d] -- [U]: %s [data: %d]",
200 if (kuri
|| (!kuri
&& !uuri
)) {
203 /* No kernel URI, use the UST port. */
206 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
214 * Get run-time attributes if the session has been started (discarded events,
217 static int get_kernel_runtime_stats(struct ltt_session
*session
,
218 struct ltt_kernel_channel
*kchan
,
219 uint64_t *discarded_events
,
220 uint64_t *lost_packets
)
224 if (!session
->has_been_started
) {
226 *discarded_events
= 0;
231 ret
= consumer_get_discarded_events(
232 session
->id
, kchan
->key
, session
->kernel_session
->consumer
, discarded_events
);
237 ret
= consumer_get_lost_packets(
238 session
->id
, kchan
->key
, session
->kernel_session
->consumer
, lost_packets
);
248 * Get run-time attributes if the session has been started (discarded events,
251 static int get_ust_runtime_stats(struct ltt_session
*session
,
252 struct ltt_ust_channel
*uchan
,
253 uint64_t *discarded_events
,
254 uint64_t *lost_packets
)
257 struct ltt_ust_session
*usess
;
259 if (!discarded_events
|| !lost_packets
) {
264 usess
= session
->ust_session
;
265 LTTNG_ASSERT(discarded_events
);
266 LTTNG_ASSERT(lost_packets
);
268 if (!usess
|| !session
->has_been_started
) {
269 *discarded_events
= 0;
275 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
276 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
277 &usess
->buffer_reg_uid_list
,
280 uchan
->attr
.overwrite
,
283 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
284 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
287 uchan
->attr
.overwrite
,
293 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
294 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
296 ERR("Unsupported buffer type");
307 * Create a list of agent domain events.
309 * Return number of events in list on success or else a negative value.
311 static enum lttng_error_code
list_lttng_agent_events(struct agent
*agt
,
312 struct lttng_payload
*reply_payload
,
313 unsigned int *nb_events
)
315 enum lttng_error_code ret_code
;
317 unsigned int local_nb_events
= 0;
318 struct agent_event
*event
;
319 struct lttng_ht_iter iter
;
320 unsigned long agent_event_count
;
323 assert(reply_payload
);
325 DBG3("Listing agent events");
328 agent_event_count
= lttng_ht_get_count(agt
->events
);
329 if (agent_event_count
== 0) {
334 if (agent_event_count
> UINT_MAX
) {
335 ret_code
= LTTNG_ERR_OVERFLOW
;
339 local_nb_events
= (unsigned int) agent_event_count
;
341 cds_lfht_for_each_entry (agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
342 struct lttng_event
*tmp_event
= lttng_event_create();
345 ret_code
= LTTNG_ERR_NOMEM
;
349 if (lttng_strncpy(tmp_event
->name
, event
->name
, sizeof(tmp_event
->name
))) {
350 lttng_event_destroy(tmp_event
);
351 ret_code
= LTTNG_ERR_FATAL
;
355 tmp_event
->name
[sizeof(tmp_event
->name
) - 1] = '\0';
356 tmp_event
->enabled
= !!event
->enabled_count
;
357 tmp_event
->loglevel
= event
->loglevel_value
;
358 tmp_event
->loglevel_type
= event
->loglevel_type
;
360 ret
= lttng_event_serialize(
361 tmp_event
, 0, NULL
, event
->filter_expression
, 0, NULL
, reply_payload
);
362 lttng_event_destroy(tmp_event
);
364 ret_code
= LTTNG_ERR_FATAL
;
371 *nb_events
= local_nb_events
;
378 * Create a list of ust global domain events.
380 static enum lttng_error_code
list_lttng_ust_global_events(char *channel_name
,
381 struct ltt_ust_domain_global
*ust_global
,
382 struct lttng_payload
*reply_payload
,
383 unsigned int *nb_events
)
385 enum lttng_error_code ret_code
;
387 struct lttng_ht_iter iter
;
388 struct lttng_ht_node_str
*node
;
389 struct ltt_ust_channel
*uchan
;
390 struct ltt_ust_event
*uevent
;
391 unsigned long channel_event_count
;
392 unsigned int local_nb_events
= 0;
394 assert(reply_payload
);
397 DBG("Listing UST global events for channel %s", channel_name
);
401 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
402 node
= lttng_ht_iter_get_node_str(&iter
);
404 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
408 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
410 channel_event_count
= lttng_ht_get_count(uchan
->events
);
411 if (channel_event_count
== 0) {
417 if (channel_event_count
> UINT_MAX
) {
418 ret_code
= LTTNG_ERR_OVERFLOW
;
422 local_nb_events
= (unsigned int) channel_event_count
;
424 DBG3("Listing UST global %d events", *nb_events
);
426 cds_lfht_for_each_entry (uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
427 struct lttng_event
*tmp_event
= NULL
;
429 if (uevent
->internal
) {
430 /* This event should remain hidden from clients */
435 tmp_event
= lttng_event_create();
437 ret_code
= LTTNG_ERR_NOMEM
;
441 if (lttng_strncpy(tmp_event
->name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
)) {
442 ret_code
= LTTNG_ERR_FATAL
;
443 lttng_event_destroy(tmp_event
);
447 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
448 tmp_event
->enabled
= uevent
->enabled
;
450 switch (uevent
->attr
.instrumentation
) {
451 case LTTNG_UST_ABI_TRACEPOINT
:
452 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
454 case LTTNG_UST_ABI_PROBE
:
455 tmp_event
->type
= LTTNG_EVENT_PROBE
;
457 case LTTNG_UST_ABI_FUNCTION
:
458 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
462 tmp_event
->loglevel
= uevent
->attr
.loglevel
;
463 switch (uevent
->attr
.loglevel_type
) {
464 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
465 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
467 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
468 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
470 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
471 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
474 if (uevent
->filter
) {
475 tmp_event
->filter
= 1;
477 if (uevent
->exclusion
) {
478 tmp_event
->exclusion
= 1;
482 * We do not care about the filter bytecode and the fd from the
483 * userspace_probe_location.
485 ret
= lttng_event_serialize(tmp_event
,
486 uevent
->exclusion
? uevent
->exclusion
->count
: 0,
487 uevent
->exclusion
? (char **) uevent
->exclusion
->names
:
489 uevent
->filter_expression
,
493 lttng_event_destroy(tmp_event
);
495 ret_code
= LTTNG_ERR_FATAL
;
501 /* nb_events is already set at this point. */
503 *nb_events
= local_nb_events
;
510 * Fill lttng_event array of all kernel events in the channel.
512 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
513 struct ltt_kernel_session
*kernel_session
,
514 struct lttng_payload
*reply_payload
,
515 unsigned int *nb_events
)
517 enum lttng_error_code ret_code
;
519 struct ltt_kernel_event
*event
;
520 struct ltt_kernel_channel
*kchan
;
522 assert(reply_payload
);
524 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
526 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
530 *nb_events
= kchan
->event_count
;
532 DBG("Listing events for channel %s", kchan
->channel
->name
);
534 if (*nb_events
== 0) {
539 /* Kernel channels */
540 cds_list_for_each_entry (event
, &kchan
->events_list
.head
, list
) {
541 struct lttng_event
*tmp_event
= lttng_event_create();
544 ret_code
= LTTNG_ERR_NOMEM
;
548 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
549 lttng_event_destroy(tmp_event
);
550 ret_code
= LTTNG_ERR_FATAL
;
554 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
555 tmp_event
->enabled
= event
->enabled
;
556 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
558 switch (event
->event
->instrumentation
) {
559 case LTTNG_KERNEL_ABI_TRACEPOINT
:
560 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
562 case LTTNG_KERNEL_ABI_KRETPROBE
:
563 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
564 memcpy(&tmp_event
->attr
.probe
,
565 &event
->event
->u
.kprobe
,
566 sizeof(struct lttng_kernel_abi_kprobe
));
568 case LTTNG_KERNEL_ABI_KPROBE
:
569 tmp_event
->type
= LTTNG_EVENT_PROBE
;
570 memcpy(&tmp_event
->attr
.probe
,
571 &event
->event
->u
.kprobe
,
572 sizeof(struct lttng_kernel_abi_kprobe
));
574 case LTTNG_KERNEL_ABI_UPROBE
:
575 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
577 case LTTNG_KERNEL_ABI_FUNCTION
:
578 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
579 memcpy(&(tmp_event
->attr
.ftrace
),
580 &event
->event
->u
.ftrace
,
581 sizeof(struct lttng_kernel_abi_function
));
583 case LTTNG_KERNEL_ABI_NOOP
:
584 tmp_event
->type
= LTTNG_EVENT_NOOP
;
586 case LTTNG_KERNEL_ABI_SYSCALL
:
587 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
589 case LTTNG_KERNEL_ABI_ALL
:
596 if (event
->userspace_probe_location
) {
597 struct lttng_userspace_probe_location
*location_copy
=
598 lttng_userspace_probe_location_copy(
599 event
->userspace_probe_location
);
601 if (!location_copy
) {
602 lttng_event_destroy(tmp_event
);
603 ret_code
= LTTNG_ERR_NOMEM
;
607 ret
= lttng_event_set_userspace_probe_location(tmp_event
, location_copy
);
609 lttng_event_destroy(tmp_event
);
610 lttng_userspace_probe_location_destroy(location_copy
);
611 ret_code
= LTTNG_ERR_INVALID
;
616 ret
= lttng_event_serialize(
617 tmp_event
, 0, NULL
, event
->filter_expression
, 0, NULL
, reply_payload
);
618 lttng_event_destroy(tmp_event
);
620 ret_code
= LTTNG_ERR_FATAL
;
631 * Add URI so the consumer output object. Set the correct path depending on the
632 * domain adding the default trace directory.
634 static enum lttng_error_code
add_uri_to_consumer(const struct ltt_session
*session
,
635 struct consumer_output
*consumer
,
636 struct lttng_uri
*uri
,
637 enum lttng_domain_type domain
)
640 enum lttng_error_code ret_code
= LTTNG_OK
;
644 if (consumer
== NULL
) {
645 DBG("No consumer detected. Don't add URI. Stopping.");
646 ret_code
= LTTNG_ERR_NO_CONSUMER
;
651 case LTTNG_DOMAIN_KERNEL
:
652 ret
= lttng_strncpy(consumer
->domain_subdir
,
653 DEFAULT_KERNEL_TRACE_DIR
,
654 sizeof(consumer
->domain_subdir
));
656 case LTTNG_DOMAIN_UST
:
657 ret
= lttng_strncpy(consumer
->domain_subdir
,
658 DEFAULT_UST_TRACE_DIR
,
659 sizeof(consumer
->domain_subdir
));
663 * This case is possible is we try to add the URI to the global
664 * tracing session consumer object which in this case there is
667 memset(consumer
->domain_subdir
, 0, sizeof(consumer
->domain_subdir
));
671 ERR("Failed to initialize consumer output domain subdirectory");
672 ret_code
= LTTNG_ERR_FATAL
;
676 switch (uri
->dtype
) {
679 DBG2("Setting network URI to consumer");
681 if (consumer
->type
== CONSUMER_DST_NET
) {
682 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
683 consumer
->dst
.net
.control_isset
) ||
684 (uri
->stype
== LTTNG_STREAM_DATA
&& consumer
->dst
.net
.data_isset
)) {
685 ret_code
= LTTNG_ERR_URL_EXIST
;
689 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
692 /* Set URI into consumer output object */
693 ret
= consumer_set_network_uri(session
, consumer
, uri
);
695 ret_code
= (lttng_error_code
) -ret
;
697 } else if (ret
== 1) {
699 * URI was the same in the consumer so we do not append the subdir
700 * again so to not duplicate output dir.
707 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
708 ret_code
= LTTNG_ERR_INVALID
;
711 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
712 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
714 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
716 sizeof(consumer
->dst
.session_root_path
));
718 ret_code
= LTTNG_ERR_FATAL
;
721 consumer
->type
= CONSUMER_DST_LOCAL
;
731 * Init tracing by creating trace directory and sending fds kernel consumer.
733 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
736 struct lttng_ht_iter iter
;
737 struct consumer_socket
*socket
;
739 LTTNG_ASSERT(session
);
743 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
744 cds_lfht_for_each_entry (
745 session
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
746 pthread_mutex_lock(socket
->lock
);
747 ret
= kernel_consumer_send_session(socket
, session
);
748 pthread_mutex_unlock(socket
->lock
);
750 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
762 * Create a socket to the relayd using the URI.
764 * On success, the relayd_sock pointer is set to the created socket.
765 * Else, it remains untouched and an LTTng error code is returned.
767 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
768 struct lttcomm_relayd_sock
**relayd_sock
,
769 struct consumer_output
*consumer
)
772 enum lttng_error_code status
= LTTNG_OK
;
773 struct lttcomm_relayd_sock
*rsock
;
775 rsock
= lttcomm_alloc_relayd_sock(
776 uri
, RELAYD_VERSION_COMM_MAJOR
, RELAYD_VERSION_COMM_MINOR
);
778 status
= LTTNG_ERR_FATAL
;
783 * Connect to relayd so we can proceed with a session creation. This call
784 * can possibly block for an arbitrary amount of time to set the health
785 * state to be in poll execution.
788 ret
= relayd_connect(rsock
);
791 ERR("Unable to reach lttng-relayd");
792 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
796 /* Create socket for control stream. */
797 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
798 uint64_t result_flags
;
800 DBG3("Creating relayd stream socket from URI");
802 /* Check relayd version */
803 ret
= relayd_version_check(rsock
);
804 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
805 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
807 } else if (ret
< 0) {
808 ERR("Unable to reach lttng-relayd");
809 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
812 consumer
->relay_major_version
= rsock
->major
;
813 consumer
->relay_minor_version
= rsock
->minor
;
814 ret
= relayd_get_configuration(rsock
, 0, &result_flags
);
816 ERR("Unable to get relayd configuration");
817 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
820 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
821 consumer
->relay_allows_clear
= true;
823 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
824 DBG3("Creating relayd data socket from URI");
826 /* Command is not valid */
827 ERR("Relayd invalid stream type: %d", uri
->stype
);
828 status
= LTTNG_ERR_INVALID
;
832 *relayd_sock
= rsock
;
837 /* The returned value is not useful since we are on an error path. */
838 (void) relayd_close(rsock
);
846 * Connect to the relayd using URI and send the socket to the right consumer.
848 * The consumer socket lock must be held by the caller.
850 * Returns LTTNG_OK on success or an LTTng error code on failure.
852 static enum lttng_error_code
send_consumer_relayd_socket(unsigned int session_id
,
853 struct lttng_uri
*relayd_uri
,
854 struct consumer_output
*consumer
,
855 struct consumer_socket
*consumer_sock
,
856 const char *session_name
,
857 const char *hostname
,
858 const char *base_path
,
859 int session_live_timer
,
860 const uint64_t *current_chunk_id
,
861 time_t session_creation_time
,
862 bool session_name_contains_creation_time
)
865 struct lttcomm_relayd_sock
*rsock
= NULL
;
866 enum lttng_error_code status
;
868 /* Connect to relayd and make version check if uri is the control. */
869 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
870 if (status
!= LTTNG_OK
) {
871 goto relayd_comm_error
;
875 /* Set the network sequence index if not set. */
876 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
877 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
879 * Increment net_seq_idx because we are about to transfer the
880 * new relayd socket to the consumer.
881 * Assign unique key so the consumer can match streams.
883 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
884 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
887 /* Send relayd socket to consumer. */
888 ret
= consumer_send_relayd_socket(consumer_sock
,
898 session_creation_time
,
899 session_name_contains_creation_time
);
901 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
905 /* Flag that the corresponding socket was sent. */
906 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
907 consumer_sock
->control_sock_sent
= 1;
908 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
909 consumer_sock
->data_sock_sent
= 1;
913 * Close socket which was dup on the consumer side. The session daemon does
914 * NOT keep track of the relayd socket(s) once transfer to the consumer.
918 if (status
!= LTTNG_OK
) {
920 * The consumer output for this session should not be used anymore
921 * since the relayd connection failed thus making any tracing or/and
922 * streaming not usable.
924 consumer
->enabled
= 0;
926 (void) relayd_close(rsock
);
934 * Send both relayd sockets to a specific consumer and domain. This is a
935 * helper function to facilitate sending the information to the consumer for a
938 * The consumer socket lock must be held by the caller.
940 * Returns LTTNG_OK, or an LTTng error code on failure.
942 static enum lttng_error_code
send_consumer_relayd_sockets(unsigned int session_id
,
943 struct consumer_output
*consumer
,
944 struct consumer_socket
*sock
,
945 const char *session_name
,
946 const char *hostname
,
947 const char *base_path
,
948 int session_live_timer
,
949 const uint64_t *current_chunk_id
,
950 time_t session_creation_time
,
951 bool session_name_contains_creation_time
)
953 enum lttng_error_code status
= LTTNG_OK
;
955 LTTNG_ASSERT(consumer
);
958 /* Sending control relayd socket. */
959 if (!sock
->control_sock_sent
) {
960 status
= send_consumer_relayd_socket(session_id
,
961 &consumer
->dst
.net
.control
,
969 session_creation_time
,
970 session_name_contains_creation_time
);
971 if (status
!= LTTNG_OK
) {
976 /* Sending data relayd socket. */
977 if (!sock
->data_sock_sent
) {
978 status
= send_consumer_relayd_socket(session_id
,
979 &consumer
->dst
.net
.data
,
987 session_creation_time
,
988 session_name_contains_creation_time
);
989 if (status
!= LTTNG_OK
) {
999 * Setup relayd connections for a tracing session. First creates the socket to
1000 * the relayd and send them to the right domain consumer. Consumer type MUST be
1003 int cmd_setup_relayd(struct ltt_session
*session
)
1006 struct ltt_ust_session
*usess
;
1007 struct ltt_kernel_session
*ksess
;
1008 struct consumer_socket
*socket
;
1009 struct lttng_ht_iter iter
;
1010 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1012 LTTNG_ASSERT(session
);
1014 usess
= session
->ust_session
;
1015 ksess
= session
->kernel_session
;
1017 DBG("Setting relayd for session %s", session
->name
);
1020 if (session
->current_trace_chunk
) {
1021 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1022 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1024 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1025 current_chunk_id
.is_set
= true;
1027 ERR("Failed to get current trace chunk id");
1028 ret
= LTTNG_ERR_UNK
;
1033 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
&&
1034 usess
->consumer
->enabled
) {
1035 /* For each consumer socket, send relayd sockets */
1036 cds_lfht_for_each_entry (
1037 usess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1038 pthread_mutex_lock(socket
->lock
);
1039 ret
= send_consumer_relayd_sockets(
1046 session
->live_timer
,
1047 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1048 session
->creation_time
,
1049 session
->name_contains_creation_time
);
1050 pthread_mutex_unlock(socket
->lock
);
1051 if (ret
!= LTTNG_OK
) {
1054 /* Session is now ready for network streaming. */
1055 session
->net_handle
= 1;
1057 session
->consumer
->relay_major_version
= usess
->consumer
->relay_major_version
;
1058 session
->consumer
->relay_minor_version
= usess
->consumer
->relay_minor_version
;
1059 session
->consumer
->relay_allows_clear
= usess
->consumer
->relay_allows_clear
;
1062 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
&&
1063 ksess
->consumer
->enabled
) {
1064 cds_lfht_for_each_entry (
1065 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1066 pthread_mutex_lock(socket
->lock
);
1067 ret
= send_consumer_relayd_sockets(
1074 session
->live_timer
,
1075 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1076 session
->creation_time
,
1077 session
->name_contains_creation_time
);
1078 pthread_mutex_unlock(socket
->lock
);
1079 if (ret
!= LTTNG_OK
) {
1082 /* Session is now ready for network streaming. */
1083 session
->net_handle
= 1;
1085 session
->consumer
->relay_major_version
= ksess
->consumer
->relay_major_version
;
1086 session
->consumer
->relay_minor_version
= ksess
->consumer
->relay_minor_version
;
1087 session
->consumer
->relay_allows_clear
= ksess
->consumer
->relay_allows_clear
;
1096 * Start a kernel session by opening all necessary streams.
1098 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1101 struct ltt_kernel_channel
*kchan
;
1103 /* Open kernel metadata */
1104 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1105 ret
= kernel_open_metadata(ksess
);
1107 ret
= LTTNG_ERR_KERN_META_FAIL
;
1112 /* Open kernel metadata stream */
1113 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1114 ret
= kernel_open_metadata_stream(ksess
);
1116 ERR("Kernel create metadata stream failed");
1117 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1122 /* For each channel */
1123 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1124 if (kchan
->stream_count
== 0) {
1125 ret
= kernel_open_channel_stream(kchan
);
1127 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1130 /* Update the stream global counter */
1131 ksess
->stream_count_global
+= ret
;
1135 /* Setup kernel consumer socket and send fds to it */
1136 ret
= init_kernel_tracing(ksess
);
1138 ret
= LTTNG_ERR_KERN_START_FAIL
;
1142 /* This start the kernel tracing */
1143 ret
= kernel_start_session(ksess
);
1145 ret
= LTTNG_ERR_KERN_START_FAIL
;
1149 /* Quiescent wait after starting trace */
1150 kernel_wait_quiescent();
1160 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1162 struct ltt_kernel_channel
*kchan
;
1163 bool error_occurred
= false;
1166 if (!ksess
|| !ksess
->active
) {
1169 DBG("Stopping kernel tracing");
1171 ret
= kernel_stop_session(ksess
);
1173 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1177 kernel_wait_quiescent();
1179 /* Flush metadata after stopping (if exists) */
1180 if (ksess
->metadata_stream_fd
>= 0) {
1181 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1183 ERR("Kernel metadata flush failed");
1184 error_occurred
= true;
1188 /* Flush all buffers after stopping */
1189 cds_list_for_each_entry (kchan
, &ksess
->channel_list
.head
, list
) {
1190 ret
= kernel_flush_buffer(kchan
);
1192 ERR("Kernel flush buffer error");
1193 error_occurred
= true;
1198 if (error_occurred
) {
1199 ret
= LTTNG_ERR_UNK
;
1208 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1210 int cmd_disable_channel(struct ltt_session
*session
,
1211 enum lttng_domain_type domain
,
1215 struct ltt_ust_session
*usess
;
1217 usess
= session
->ust_session
;
1222 case LTTNG_DOMAIN_KERNEL
:
1224 ret
= channel_kernel_disable(session
->kernel_session
, channel_name
);
1225 if (ret
!= LTTNG_OK
) {
1229 kernel_wait_quiescent();
1232 case LTTNG_DOMAIN_UST
:
1234 struct ltt_ust_channel
*uchan
;
1235 struct lttng_ht
*chan_ht
;
1237 chan_ht
= usess
->domain_global
.channels
;
1239 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1240 if (uchan
== NULL
) {
1241 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1245 ret
= channel_ust_disable(usess
, uchan
);
1246 if (ret
!= LTTNG_OK
) {
1252 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1264 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1266 * The wpipe arguments is used as a notifier for the kernel thread.
1268 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1272 ssize_t sock_recv_len
;
1273 struct lttng_channel
*channel
= NULL
;
1274 struct lttng_buffer_view view
;
1275 struct lttng_dynamic_buffer channel_buffer
;
1276 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1278 lttng_dynamic_buffer_init(&channel_buffer
);
1279 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1280 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1282 ret
= LTTNG_ERR_NOMEM
;
1286 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
, channel_len
);
1287 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1288 ERR("Failed to receive \"enable channel\" command payload");
1289 ret
= LTTNG_ERR_INVALID
;
1293 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1294 if (!lttng_buffer_view_is_valid(&view
)) {
1295 ret
= LTTNG_ERR_INVALID
;
1299 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1300 ERR("Invalid channel payload received in \"enable channel\" command");
1301 ret
= LTTNG_ERR_INVALID
;
1305 ret
= cmd_enable_channel_internal(cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1308 lttng_dynamic_buffer_reset(&channel_buffer
);
1309 lttng_channel_destroy(channel
);
1313 static enum lttng_error_code
cmd_enable_channel_internal(struct ltt_session
*session
,
1314 const struct lttng_domain
*domain
,
1315 const struct lttng_channel
*_attr
,
1318 enum lttng_error_code ret_code
;
1319 struct ltt_ust_session
*usess
= session
->ust_session
;
1320 struct lttng_ht
*chan_ht
;
1322 struct lttng_channel
*attr
= NULL
;
1324 LTTNG_ASSERT(session
);
1325 LTTNG_ASSERT(_attr
);
1326 LTTNG_ASSERT(domain
);
1328 attr
= lttng_channel_copy(_attr
);
1330 ret_code
= LTTNG_ERR_NOMEM
;
1334 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1336 /* Validate channel name */
1337 if (attr
->name
[0] == '.' || memchr(attr
->name
, '/', len
) != NULL
) {
1338 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1342 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1347 * If the session is a live session, remove the switch timer, the
1348 * live timer does the same thing but sends also synchronisation
1349 * beacons for inactive streams.
1351 if (session
->live_timer
> 0) {
1352 attr
->attr
.live_timer_interval
= session
->live_timer
;
1353 attr
->attr
.switch_timer_interval
= 0;
1356 /* Check for feature support */
1357 switch (domain
->type
) {
1358 case LTTNG_DOMAIN_KERNEL
:
1360 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1361 /* Sampling position of buffer is not supported */
1362 WARN("Kernel tracer does not support buffer monitoring. "
1363 "Setting the monitor interval timer to 0 "
1364 "(disabled) for channel '%s' of session '%s'",
1367 lttng_channel_set_monitor_timer_interval(attr
, 0);
1371 case LTTNG_DOMAIN_UST
:
1373 case LTTNG_DOMAIN_JUL
:
1374 case LTTNG_DOMAIN_LOG4J
:
1375 case LTTNG_DOMAIN_PYTHON
:
1376 if (!agent_tracing_is_enabled()) {
1377 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1378 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1383 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1387 switch (domain
->type
) {
1388 case LTTNG_DOMAIN_KERNEL
:
1390 struct ltt_kernel_channel
*kchan
;
1392 kchan
= trace_kernel_get_channel_by_name(attr
->name
, session
->kernel_session
);
1393 if (kchan
== NULL
) {
1395 * Don't try to create a channel if the session has been started at
1396 * some point in time before. The tracer does not allow it.
1398 if (session
->has_been_started
) {
1399 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1403 if (session
->snapshot
.nb_output
> 0 || session
->snapshot_mode
) {
1404 /* Enforce mmap output for snapshot sessions. */
1405 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1407 ret_code
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1408 if (attr
->name
[0] != '\0') {
1409 session
->kernel_session
->has_non_default_channel
= 1;
1412 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1415 if (ret_code
!= LTTNG_OK
) {
1419 kernel_wait_quiescent();
1422 case LTTNG_DOMAIN_UST
:
1423 case LTTNG_DOMAIN_JUL
:
1424 case LTTNG_DOMAIN_LOG4J
:
1425 case LTTNG_DOMAIN_PYTHON
:
1427 struct ltt_ust_channel
*uchan
;
1432 * Current agent implementation limitations force us to allow
1433 * only one channel at once in "agent" subdomains. Each
1434 * subdomain has a default channel name which must be strictly
1437 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1438 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
, LTTNG_SYMBOL_NAME_LEN
)) {
1439 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1442 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1443 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
, LTTNG_SYMBOL_NAME_LEN
)) {
1444 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1447 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1448 if (strncmp(attr
->name
,
1449 DEFAULT_PYTHON_CHANNEL_NAME
,
1450 LTTNG_SYMBOL_NAME_LEN
)) {
1451 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1456 chan_ht
= usess
->domain_global
.channels
;
1458 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1459 if (uchan
== NULL
) {
1461 * Don't try to create a channel if the session has been started at
1462 * some point in time before. The tracer does not allow it.
1464 if (session
->has_been_started
) {
1465 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1469 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1470 if (attr
->name
[0] != '\0') {
1471 usess
->has_non_default_channel
= 1;
1474 ret_code
= channel_ust_enable(usess
, uchan
);
1479 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1483 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1484 session
->has_non_mmap_channel
= true;
1489 lttng_channel_destroy(attr
);
1493 enum lttng_error_code
1494 cmd_process_attr_tracker_get_tracking_policy(struct ltt_session
*session
,
1495 enum lttng_domain_type domain
,
1496 enum lttng_process_attr process_attr
,
1497 enum lttng_tracking_policy
*policy
)
1499 enum lttng_error_code ret_code
= LTTNG_OK
;
1500 const struct process_attr_tracker
*tracker
;
1503 case LTTNG_DOMAIN_KERNEL
:
1504 if (!session
->kernel_session
) {
1505 ret_code
= LTTNG_ERR_INVALID
;
1508 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1510 case LTTNG_DOMAIN_UST
:
1511 if (!session
->ust_session
) {
1512 ret_code
= LTTNG_ERR_INVALID
;
1515 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1518 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1522 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1524 ret_code
= LTTNG_ERR_INVALID
;
1530 enum lttng_error_code
1531 cmd_process_attr_tracker_set_tracking_policy(struct ltt_session
*session
,
1532 enum lttng_domain_type domain
,
1533 enum lttng_process_attr process_attr
,
1534 enum lttng_tracking_policy policy
)
1536 enum lttng_error_code ret_code
= LTTNG_OK
;
1539 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1540 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1541 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1544 ret_code
= LTTNG_ERR_INVALID
;
1549 case LTTNG_DOMAIN_KERNEL
:
1550 if (!session
->kernel_session
) {
1551 ret_code
= LTTNG_ERR_INVALID
;
1554 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1555 session
->kernel_session
, process_attr
, policy
);
1557 case LTTNG_DOMAIN_UST
:
1558 if (!session
->ust_session
) {
1559 ret_code
= LTTNG_ERR_INVALID
;
1562 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1563 session
->ust_session
, process_attr
, policy
);
1566 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1573 enum lttng_error_code
1574 cmd_process_attr_tracker_inclusion_set_add_value(struct ltt_session
*session
,
1575 enum lttng_domain_type domain
,
1576 enum lttng_process_attr process_attr
,
1577 const struct process_attr_value
*value
)
1579 enum lttng_error_code ret_code
= LTTNG_OK
;
1582 case LTTNG_DOMAIN_KERNEL
:
1583 if (!session
->kernel_session
) {
1584 ret_code
= LTTNG_ERR_INVALID
;
1587 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1588 session
->kernel_session
, process_attr
, value
);
1590 case LTTNG_DOMAIN_UST
:
1591 if (!session
->ust_session
) {
1592 ret_code
= LTTNG_ERR_INVALID
;
1595 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1596 session
->ust_session
, process_attr
, value
);
1599 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1606 enum lttng_error_code
1607 cmd_process_attr_tracker_inclusion_set_remove_value(struct ltt_session
*session
,
1608 enum lttng_domain_type domain
,
1609 enum lttng_process_attr process_attr
,
1610 const struct process_attr_value
*value
)
1612 enum lttng_error_code ret_code
= LTTNG_OK
;
1615 case LTTNG_DOMAIN_KERNEL
:
1616 if (!session
->kernel_session
) {
1617 ret_code
= LTTNG_ERR_INVALID
;
1620 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1621 session
->kernel_session
, process_attr
, value
);
1623 case LTTNG_DOMAIN_UST
:
1624 if (!session
->ust_session
) {
1625 ret_code
= LTTNG_ERR_INVALID
;
1628 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1629 session
->ust_session
, process_attr
, value
);
1632 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1639 enum lttng_error_code
1640 cmd_process_attr_tracker_get_inclusion_set(struct ltt_session
*session
,
1641 enum lttng_domain_type domain
,
1642 enum lttng_process_attr process_attr
,
1643 struct lttng_process_attr_values
**values
)
1645 enum lttng_error_code ret_code
= LTTNG_OK
;
1646 const struct process_attr_tracker
*tracker
;
1647 enum process_attr_tracker_status status
;
1650 case LTTNG_DOMAIN_KERNEL
:
1651 if (!session
->kernel_session
) {
1652 ret_code
= LTTNG_ERR_INVALID
;
1655 tracker
= kernel_get_process_attr_tracker(session
->kernel_session
, process_attr
);
1657 case LTTNG_DOMAIN_UST
:
1658 if (!session
->ust_session
) {
1659 ret_code
= LTTNG_ERR_INVALID
;
1662 tracker
= trace_ust_get_process_attr_tracker(session
->ust_session
, process_attr
);
1665 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1670 ret_code
= LTTNG_ERR_INVALID
;
1674 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1676 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1677 ret_code
= LTTNG_OK
;
1679 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1680 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1682 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1683 ret_code
= LTTNG_ERR_NOMEM
;
1686 ret_code
= LTTNG_ERR_UNK
;
1695 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1697 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1698 struct lttng_event
*event
,
1699 char *filter_expression
,
1700 struct lttng_bytecode
*bytecode
,
1701 struct lttng_event_exclusion
*exclusion
)
1704 const char *event_name
;
1705 const struct ltt_session
*session
= cmd_ctx
->session
;
1706 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1707 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1709 DBG("Disable event command for event \'%s\'", event
->name
);
1712 * Filter and exclusions are simply not handled by the
1713 * disable event command at this time.
1717 (void) filter_expression
;
1720 /* Ignore the presence of filter or exclusion for the event */
1722 event
->exclusion
= 0;
1724 event_name
= event
->name
;
1726 /* Error out on unhandled search criteria */
1727 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
|| event
->pid
||
1728 event
->filter
|| event
->exclusion
) {
1729 ret
= LTTNG_ERR_UNK
;
1736 case LTTNG_DOMAIN_KERNEL
:
1738 struct ltt_kernel_channel
*kchan
;
1739 struct ltt_kernel_session
*ksess
;
1741 ksess
= session
->kernel_session
;
1744 * If a non-default channel has been created in the
1745 * session, explicitely require that -c chan_name needs
1748 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1749 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1753 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1754 if (kchan
== NULL
) {
1755 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1759 switch (event
->type
) {
1760 case LTTNG_EVENT_ALL
:
1761 case LTTNG_EVENT_TRACEPOINT
:
1762 case LTTNG_EVENT_SYSCALL
:
1763 case LTTNG_EVENT_PROBE
:
1764 case LTTNG_EVENT_FUNCTION
:
1765 case LTTNG_EVENT_FUNCTION_ENTRY
: /* fall-through */
1766 if (event_name
[0] == '\0') {
1767 ret
= event_kernel_disable_event(kchan
, NULL
, event
->type
);
1769 ret
= event_kernel_disable_event(kchan
, event_name
, event
->type
);
1771 if (ret
!= LTTNG_OK
) {
1776 ret
= LTTNG_ERR_UNK
;
1780 kernel_wait_quiescent();
1783 case LTTNG_DOMAIN_UST
:
1785 struct ltt_ust_channel
*uchan
;
1786 struct ltt_ust_session
*usess
;
1788 usess
= session
->ust_session
;
1790 if (validate_ust_event_name(event_name
)) {
1791 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1796 * If a non-default channel has been created in the
1797 * session, explicitly require that -c chan_name needs
1800 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1801 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1805 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
1806 if (uchan
== NULL
) {
1807 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1811 switch (event
->type
) {
1812 case LTTNG_EVENT_ALL
:
1814 * An empty event name means that everything
1815 * should be disabled.
1817 if (event
->name
[0] == '\0') {
1818 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1820 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1822 if (ret
!= LTTNG_OK
) {
1827 ret
= LTTNG_ERR_UNK
;
1831 DBG3("Disable UST event %s in channel %s completed", event_name
, channel_name
);
1834 case LTTNG_DOMAIN_LOG4J
:
1835 case LTTNG_DOMAIN_JUL
:
1836 case LTTNG_DOMAIN_PYTHON
:
1839 struct ltt_ust_session
*usess
= session
->ust_session
;
1841 LTTNG_ASSERT(usess
);
1843 switch (event
->type
) {
1844 case LTTNG_EVENT_ALL
:
1847 ret
= LTTNG_ERR_UNK
;
1851 agt
= trace_ust_find_agent(usess
, domain
);
1853 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1857 * An empty event name means that everything
1858 * should be disabled.
1860 if (event
->name
[0] == '\0') {
1861 ret
= event_agent_disable_all(usess
, agt
);
1863 ret
= event_agent_disable(usess
, agt
, event_name
);
1865 if (ret
!= LTTNG_OK
) {
1872 ret
= LTTNG_ERR_UND
;
1883 free(filter_expression
);
1888 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1890 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1891 const struct lttng_event_context
*event_context
,
1894 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1895 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1896 const struct ltt_session
*session
= cmd_ctx
->session
;
1897 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1900 * Don't try to add a context if the session has been started at
1901 * some point in time before. The tracer does not allow it and would
1902 * result in a corrupted trace.
1904 if (cmd_ctx
->session
->has_been_started
) {
1905 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1910 case LTTNG_DOMAIN_KERNEL
:
1911 LTTNG_ASSERT(session
->kernel_session
);
1913 if (session
->kernel_session
->channel_count
== 0) {
1914 /* Create default channel */
1915 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1916 if (ret
!= LTTNG_OK
) {
1919 chan_kern_created
= 1;
1921 /* Add kernel context to kernel tracer */
1922 ret
= context_kernel_add(session
->kernel_session
, event_context
, channel_name
);
1923 if (ret
!= LTTNG_OK
) {
1927 case LTTNG_DOMAIN_JUL
:
1928 case LTTNG_DOMAIN_LOG4J
:
1931 * Validate channel name.
1932 * If no channel name is given and the domain is JUL or LOG4J,
1933 * set it to the appropriate domain-specific channel name. If
1934 * a name is provided but does not match the expexted channel
1935 * name, return an error.
1937 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1938 strcmp(channel_name
, DEFAULT_JUL_CHANNEL_NAME
)) {
1939 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1941 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1942 strcmp(channel_name
, DEFAULT_LOG4J_CHANNEL_NAME
)) {
1943 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1948 case LTTNG_DOMAIN_UST
:
1950 struct ltt_ust_session
*usess
= session
->ust_session
;
1951 unsigned int chan_count
;
1953 LTTNG_ASSERT(usess
);
1955 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1956 if (chan_count
== 0) {
1957 struct lttng_channel
*attr
;
1958 /* Create default channel */
1959 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1961 ret
= LTTNG_ERR_FATAL
;
1965 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1966 if (ret
!= LTTNG_OK
) {
1970 channel_attr_destroy(attr
);
1971 chan_ust_created
= 1;
1974 ret
= context_ust_add(usess
, domain
, event_context
, channel_name
);
1975 if (ret
!= LTTNG_OK
) {
1981 ret
= LTTNG_ERR_UND
;
1989 if (chan_kern_created
) {
1990 struct ltt_kernel_channel
*kchan
= trace_kernel_get_channel_by_name(
1991 DEFAULT_CHANNEL_NAME
, session
->kernel_session
);
1992 /* Created previously, this should NOT fail. */
1993 LTTNG_ASSERT(kchan
);
1994 kernel_destroy_channel(kchan
);
1997 if (chan_ust_created
) {
1998 struct ltt_ust_channel
*uchan
= trace_ust_find_channel_by_name(
1999 session
->ust_session
->domain_global
.channels
, DEFAULT_CHANNEL_NAME
);
2000 /* Created previously, this should NOT fail. */
2001 LTTNG_ASSERT(uchan
);
2002 /* Remove from the channel list of the session. */
2003 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
, uchan
);
2004 trace_ust_destroy_channel(uchan
);
2010 static inline bool name_starts_with(const char *name
, const char *prefix
)
2012 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2014 return !strncmp(name
, prefix
, max_cmp_len
);
2017 /* Perform userspace-specific event name validation */
2018 static int validate_ust_event_name(const char *name
)
2028 * Check name against all internal UST event component namespaces used
2031 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2032 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2033 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2042 * Internal version of cmd_enable_event() with a supplemental
2043 * "internal_event" flag which is used to enable internal events which should
2044 * be hidden from clients. Such events are used in the agent implementation to
2045 * enable the events through which all "agent" events are funeled.
2047 static int _cmd_enable_event(struct ltt_session
*session
,
2048 const struct lttng_domain
*domain
,
2050 struct lttng_event
*event
,
2051 char *filter_expression
,
2052 struct lttng_bytecode
*filter
,
2053 struct lttng_event_exclusion
*exclusion
,
2055 bool internal_event
)
2057 int ret
= 0, channel_created
= 0;
2058 struct lttng_channel
*attr
= NULL
;
2060 LTTNG_ASSERT(session
);
2061 LTTNG_ASSERT(event
);
2062 LTTNG_ASSERT(channel_name
);
2064 /* If we have a filter, we must have its filter expression */
2065 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2067 /* Normalize event name as a globbing pattern */
2068 strutils_normalize_star_glob_pattern(event
->name
);
2070 /* Normalize exclusion names as globbing patterns */
2074 for (i
= 0; i
< exclusion
->count
; i
++) {
2075 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2077 strutils_normalize_star_glob_pattern(name
);
2081 DBG("Enable event command for event \'%s\'", event
->name
);
2085 switch (domain
->type
) {
2086 case LTTNG_DOMAIN_KERNEL
:
2088 struct ltt_kernel_channel
*kchan
;
2091 * If a non-default channel has been created in the
2092 * session, explicitely require that -c chan_name needs
2095 if (session
->kernel_session
->has_non_default_channel
&& channel_name
[0] == '\0') {
2096 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2100 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2101 if (kchan
== NULL
) {
2102 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
, LTTNG_BUFFER_GLOBAL
);
2104 ret
= LTTNG_ERR_FATAL
;
2107 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2108 ret
= LTTNG_ERR_INVALID
;
2112 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2113 if (ret
!= LTTNG_OK
) {
2116 channel_created
= 1;
2119 /* Get the newly created kernel channel pointer */
2120 kchan
= trace_kernel_get_channel_by_name(channel_name
, session
->kernel_session
);
2121 if (kchan
== NULL
) {
2122 /* This sould not happen... */
2123 ret
= LTTNG_ERR_FATAL
;
2127 switch (event
->type
) {
2128 case LTTNG_EVENT_ALL
:
2130 char *filter_expression_a
= NULL
;
2131 struct lttng_bytecode
*filter_a
= NULL
;
2134 * We need to duplicate filter_expression and filter,
2135 * because ownership is passed to first enable
2138 if (filter_expression
) {
2139 filter_expression_a
= strdup(filter_expression
);
2140 if (!filter_expression_a
) {
2141 ret
= LTTNG_ERR_FATAL
;
2146 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2148 free(filter_expression_a
);
2149 ret
= LTTNG_ERR_FATAL
;
2152 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2154 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2155 ret
= event_kernel_enable_event(kchan
, event
, filter_expression
, filter
);
2156 /* We have passed ownership */
2157 filter_expression
= NULL
;
2159 if (ret
!= LTTNG_OK
) {
2160 if (channel_created
) {
2161 /* Let's not leak a useless channel. */
2162 kernel_destroy_channel(kchan
);
2164 free(filter_expression_a
);
2168 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2169 ret
= event_kernel_enable_event(
2170 kchan
, event
, filter_expression_a
, filter_a
);
2171 /* We have passed ownership */
2172 filter_expression_a
= NULL
;
2174 if (ret
!= LTTNG_OK
) {
2179 case LTTNG_EVENT_PROBE
:
2180 case LTTNG_EVENT_USERSPACE_PROBE
:
2181 case LTTNG_EVENT_FUNCTION
:
2182 case LTTNG_EVENT_FUNCTION_ENTRY
:
2183 case LTTNG_EVENT_TRACEPOINT
:
2184 ret
= event_kernel_enable_event(kchan
, event
, 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
, filter_expression
, filter
);
2198 /* We have passed ownership */
2199 filter_expression
= NULL
;
2201 if (ret
!= LTTNG_OK
) {
2206 ret
= LTTNG_ERR_UNK
;
2210 kernel_wait_quiescent();
2213 case LTTNG_DOMAIN_UST
:
2215 struct ltt_ust_channel
*uchan
;
2216 struct ltt_ust_session
*usess
= session
->ust_session
;
2218 LTTNG_ASSERT(usess
);
2221 * If a non-default channel has been created in the
2222 * session, explicitely require that -c chan_name needs
2225 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2226 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2230 /* Get channel from global UST domain */
2231 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
, channel_name
);
2232 if (uchan
== NULL
) {
2233 /* Create default channel */
2234 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
, usess
->buffer_type
);
2236 ret
= LTTNG_ERR_FATAL
;
2239 if (lttng_strncpy(attr
->name
, channel_name
, sizeof(attr
->name
))) {
2240 ret
= LTTNG_ERR_INVALID
;
2244 ret
= cmd_enable_channel_internal(session
, domain
, attr
, wpipe
);
2245 if (ret
!= LTTNG_OK
) {
2249 /* Get the newly created channel reference back */
2250 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2252 LTTNG_ASSERT(uchan
);
2255 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2257 * Don't allow users to add UST events to channels which
2258 * are assigned to a userspace subdomain (JUL, Log4J,
2261 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2265 if (!internal_event
) {
2267 * Ensure the event name is not reserved for internal
2270 ret
= validate_ust_event_name(event
->name
);
2272 WARN("Userspace event name %s failed validation.", event
->name
);
2273 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2278 /* At this point, the session and channel exist on the tracer */
2279 ret
= event_ust_enable_tracepoint(
2280 usess
, uchan
, event
, filter_expression
, filter
, exclusion
, internal_event
);
2281 /* We have passed ownership */
2282 filter_expression
= NULL
;
2285 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2286 goto already_enabled
;
2287 } else if (ret
!= LTTNG_OK
) {
2292 case LTTNG_DOMAIN_LOG4J
:
2293 case LTTNG_DOMAIN_JUL
:
2294 case LTTNG_DOMAIN_PYTHON
:
2296 const char *default_event_name
, *default_chan_name
;
2298 struct lttng_event uevent
;
2299 struct lttng_domain tmp_dom
;
2300 struct ltt_ust_session
*usess
= session
->ust_session
;
2302 LTTNG_ASSERT(usess
);
2304 if (!agent_tracing_is_enabled()) {
2305 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2306 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2310 agt
= trace_ust_find_agent(usess
, domain
->type
);
2312 agt
= agent_create(domain
->type
);
2314 ret
= LTTNG_ERR_NOMEM
;
2317 agent_add(agt
, usess
->agents
);
2320 /* Create the default tracepoint. */
2321 memset(&uevent
, 0, sizeof(uevent
));
2322 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2323 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2324 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
2325 if (!default_event_name
) {
2326 ret
= LTTNG_ERR_FATAL
;
2329 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2330 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2333 * The domain type is changed because we are about to enable the
2334 * default channel and event for the JUL domain that are hardcoded.
2335 * This happens in the UST domain.
2337 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2338 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2340 switch (domain
->type
) {
2341 case LTTNG_DOMAIN_LOG4J
:
2342 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2344 case LTTNG_DOMAIN_JUL
:
2345 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2347 case LTTNG_DOMAIN_PYTHON
:
2348 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2351 /* The switch/case we are in makes this impossible */
2356 char *filter_expression_copy
= NULL
;
2357 struct lttng_bytecode
*filter_copy
= NULL
;
2360 const size_t filter_size
=
2361 sizeof(struct lttng_bytecode
) + filter
->len
;
2363 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2365 ret
= LTTNG_ERR_NOMEM
;
2368 memcpy(filter_copy
, filter
, filter_size
);
2370 filter_expression_copy
= strdup(filter_expression
);
2371 if (!filter_expression
) {
2372 ret
= LTTNG_ERR_NOMEM
;
2375 if (!filter_expression_copy
|| !filter_copy
) {
2376 free(filter_expression_copy
);
2382 ret
= cmd_enable_event_internal(session
,
2384 (char *) default_chan_name
,
2386 filter_expression_copy
,
2392 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2393 goto already_enabled
;
2394 } else if (ret
!= LTTNG_OK
) {
2398 /* The wild card * means that everything should be enabled. */
2399 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2400 ret
= event_agent_enable_all(usess
, agt
, event
, filter
, filter_expression
);
2402 ret
= event_agent_enable(usess
, agt
, event
, filter
, filter_expression
);
2405 filter_expression
= NULL
;
2406 if (ret
!= LTTNG_OK
) {
2413 ret
= LTTNG_ERR_UND
;
2421 free(filter_expression
);
2424 channel_attr_destroy(attr
);
2430 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2431 * We own filter, exclusion, and filter_expression.
2433 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2434 struct lttng_event
*event
,
2435 char *filter_expression
,
2436 struct lttng_event_exclusion
*exclusion
,
2437 struct lttng_bytecode
*bytecode
,
2442 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2444 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2447 * The ownership of the following parameters is transferred to
2448 * _cmd_enable_event:
2450 * - filter_expression,
2454 ret
= _cmd_enable_event(cmd_ctx
->session
,
2456 cmd_ctx
->lsm
.u
.enable
.channel_name
,
2463 filter_expression
= NULL
;
2470 * Enable an event which is internal to LTTng. An internal should
2471 * never be made visible to clients and are immune to checks such as
2474 static int cmd_enable_event_internal(struct ltt_session
*session
,
2475 const struct lttng_domain
*domain
,
2477 struct lttng_event
*event
,
2478 char *filter_expression
,
2479 struct lttng_bytecode
*filter
,
2480 struct lttng_event_exclusion
*exclusion
,
2483 return _cmd_enable_event(session
,
2495 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2497 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2498 struct lttng_payload
*reply_payload
)
2500 enum lttng_error_code ret_code
;
2502 ssize_t i
, nb_events
= 0;
2503 struct lttng_event
*events
= NULL
;
2504 struct lttcomm_list_command_header reply_command_header
= {};
2505 size_t reply_command_header_offset
;
2507 assert(reply_payload
);
2509 /* Reserve space for command reply header. */
2510 reply_command_header_offset
= reply_payload
->buffer
.size
;
2511 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2512 reply_command_header_offset
+
2513 sizeof(struct lttcomm_list_command_header
));
2515 ret_code
= LTTNG_ERR_NOMEM
;
2520 case LTTNG_DOMAIN_KERNEL
:
2521 nb_events
= kernel_list_events(&events
);
2522 if (nb_events
< 0) {
2523 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2527 case LTTNG_DOMAIN_UST
:
2528 nb_events
= ust_app_list_events(&events
);
2529 if (nb_events
< 0) {
2530 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2534 case LTTNG_DOMAIN_LOG4J
:
2535 case LTTNG_DOMAIN_JUL
:
2536 case LTTNG_DOMAIN_PYTHON
:
2537 nb_events
= agent_list_events(&events
, domain
);
2538 if (nb_events
< 0) {
2539 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2544 ret_code
= LTTNG_ERR_UND
;
2548 for (i
= 0; i
< nb_events
; i
++) {
2549 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
, reply_payload
);
2551 ret_code
= LTTNG_ERR_NOMEM
;
2556 if (nb_events
> UINT32_MAX
) {
2557 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2558 ret_code
= LTTNG_ERR_OVERFLOW
;
2562 /* Update command reply header. */
2563 reply_command_header
.count
= (uint32_t) nb_events
;
2564 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2565 &reply_command_header
,
2566 sizeof(reply_command_header
));
2568 ret_code
= LTTNG_OK
;
2575 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2577 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2578 struct lttng_payload
*reply
)
2580 enum lttng_error_code ret_code
;
2582 unsigned int i
, nb_fields
;
2583 struct lttng_event_field
*fields
= NULL
;
2584 struct lttcomm_list_command_header reply_command_header
= {};
2585 size_t reply_command_header_offset
;
2589 /* Reserve space for command reply header. */
2590 reply_command_header_offset
= reply
->buffer
.size
;
2591 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2592 reply_command_header_offset
+
2593 sizeof(struct lttcomm_list_command_header
));
2595 ret_code
= LTTNG_ERR_NOMEM
;
2600 case LTTNG_DOMAIN_UST
:
2601 ret
= ust_app_list_event_fields(&fields
);
2603 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2608 case LTTNG_DOMAIN_KERNEL
:
2609 default: /* fall-through */
2610 ret_code
= LTTNG_ERR_UND
;
2616 for (i
= 0; i
< nb_fields
; i
++) {
2617 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2619 ret_code
= LTTNG_ERR_NOMEM
;
2624 if (nb_fields
> UINT32_MAX
) {
2625 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2626 ret_code
= LTTNG_ERR_OVERFLOW
;
2630 /* Update command reply header. */
2631 reply_command_header
.count
= (uint32_t) nb_fields
;
2633 memcpy(reply
->buffer
.data
+ reply_command_header_offset
,
2634 &reply_command_header
,
2635 sizeof(reply_command_header
));
2637 ret_code
= LTTNG_OK
;
2644 enum lttng_error_code
cmd_list_syscalls(struct lttng_payload
*reply_payload
)
2646 enum lttng_error_code ret_code
;
2647 ssize_t nb_events
, i
;
2649 struct lttng_event
*events
= NULL
;
2650 struct lttcomm_list_command_header reply_command_header
= {};
2651 size_t reply_command_header_offset
;
2653 assert(reply_payload
);
2655 /* Reserve space for command reply header. */
2656 reply_command_header_offset
= reply_payload
->buffer
.size
;
2657 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2658 reply_command_header_offset
+
2659 sizeof(struct lttcomm_list_command_header
));
2661 ret_code
= LTTNG_ERR_NOMEM
;
2665 nb_events
= syscall_table_list(&events
);
2666 if (nb_events
< 0) {
2667 ret_code
= (enum lttng_error_code
) - nb_events
;
2671 for (i
= 0; i
< nb_events
; i
++) {
2672 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
, reply_payload
);
2674 ret_code
= LTTNG_ERR_NOMEM
;
2679 if (nb_events
> UINT32_MAX
) {
2680 ERR("Syscall count would overflow the syscall listing command's reply");
2681 ret_code
= LTTNG_ERR_OVERFLOW
;
2685 /* Update command reply header. */
2686 reply_command_header
.count
= (uint32_t) nb_events
;
2687 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
2688 &reply_command_header
,
2689 sizeof(reply_command_header
));
2691 ret_code
= LTTNG_OK
;
2698 * Command LTTNG_START_TRACE processed by the client thread.
2700 * Called with session mutex held.
2702 int cmd_start_trace(struct ltt_session
*session
)
2704 enum lttng_error_code ret
;
2705 unsigned long nb_chan
= 0;
2706 struct ltt_kernel_session
*ksession
;
2707 struct ltt_ust_session
*usess
;
2708 const bool session_rotated_after_last_stop
= session
->rotated_after_last_stop
;
2709 const bool session_cleared_after_last_stop
= session
->cleared_after_last_stop
;
2711 LTTNG_ASSERT(session
);
2713 /* Ease our life a bit ;) */
2714 ksession
= session
->kernel_session
;
2715 usess
= session
->ust_session
;
2717 /* Is the session already started? */
2718 if (session
->active
) {
2719 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2720 /* Perform nothing */
2724 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2725 !session
->current_trace_chunk
) {
2727 * A rotation was launched while the session was stopped and
2728 * it has not been completed yet. It is not possible to start
2729 * the session since starting the session here would require a
2730 * rotation from "NULL" to a new trace chunk. That rotation
2731 * would overlap with the ongoing rotation, which is not
2734 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2736 ret
= LTTNG_ERR_ROTATION_PENDING
;
2741 * Starting a session without channel is useless since after that it's not
2742 * possible to enable channel thus inform the client.
2744 if (usess
&& usess
->domain_global
.channels
) {
2745 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2748 nb_chan
+= ksession
->channel_count
;
2751 ret
= LTTNG_ERR_NO_CHANNEL
;
2755 session
->active
= 1;
2756 session
->rotated_after_last_stop
= false;
2757 session
->cleared_after_last_stop
= false;
2758 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2759 if (!session
->has_been_started
) {
2760 struct lttng_trace_chunk
*trace_chunk
;
2762 DBG("Creating initial trace chunk of session \"%s\"", session
->name
);
2763 trace_chunk
= session_create_new_trace_chunk(session
, NULL
, NULL
, NULL
);
2765 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2768 LTTNG_ASSERT(!session
->current_trace_chunk
);
2769 ret
= (lttng_error_code
) session_set_trace_chunk(
2770 session
, trace_chunk
, NULL
);
2771 lttng_trace_chunk_put(trace_chunk
);
2773 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2777 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2780 * Rotate existing streams into the new chunk.
2781 * This is a "quiet" rotation has no client has
2782 * explicitly requested this operation.
2784 * There is also no need to wait for the rotation
2785 * to complete as it will happen immediately. No data
2786 * was produced as the session was stopped, so the
2787 * rotation should happen on reception of the command.
2789 ret
= (lttng_error_code
) cmd_rotate_session(
2790 session
, NULL
, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2791 if (ret
!= LTTNG_OK
) {
2797 /* Kernel tracing */
2798 if (ksession
!= NULL
) {
2799 DBG("Start kernel tracing session %s", session
->name
);
2800 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2801 if (ret
!= LTTNG_OK
) {
2806 /* Flag session that trace should start automatically */
2808 int int_ret
= ust_app_start_trace_all(usess
);
2811 ret
= LTTNG_ERR_UST_START_FAIL
;
2817 * Open a packet in every stream of the session to ensure that viewers
2818 * can correctly identify the boundaries of the periods during which
2819 * tracing was active for this session.
2821 ret
= session_open_packets(session
);
2822 if (ret
!= LTTNG_OK
) {
2827 * Clear the flag that indicates that a rotation was done while the
2828 * session was stopped.
2830 session
->rotated_after_last_stop
= false;
2832 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2833 int int_ret
= timer_session_rotation_schedule_timer_start(
2834 session
, session
->rotate_timer_period
);
2837 ERR("Failed to enable rotate timer");
2838 ret
= LTTNG_ERR_UNK
;
2846 if (ret
== LTTNG_OK
) {
2847 /* Flag this after a successful start. */
2848 session
->has_been_started
|= 1;
2850 session
->active
= 0;
2851 /* Restore initial state on error. */
2852 session
->rotated_after_last_stop
= session_rotated_after_last_stop
;
2853 session
->cleared_after_last_stop
= session_cleared_after_last_stop
;
2860 * Command LTTNG_STOP_TRACE processed by the client thread.
2862 int cmd_stop_trace(struct ltt_session
*session
)
2865 struct ltt_kernel_session
*ksession
;
2866 struct ltt_ust_session
*usess
;
2868 LTTNG_ASSERT(session
);
2870 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2872 ksession
= session
->kernel_session
;
2873 usess
= session
->ust_session
;
2875 /* Session is not active. Skip everything and inform the client. */
2876 if (!session
->active
) {
2877 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2881 ret
= stop_kernel_session(ksession
);
2882 if (ret
!= LTTNG_OK
) {
2886 if (usess
&& usess
->active
) {
2887 ret
= ust_app_stop_trace_all(usess
);
2889 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2894 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2895 /* Flag inactive after a successful stop. */
2896 session
->active
= 0;
2904 * Set the base_path of the session only if subdir of a control uris is set.
2905 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2908 set_session_base_path_from_uris(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2913 for (i
= 0; i
< nb_uri
; i
++) {
2914 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
|| uris
[i
].subdir
[0] == '\0') {
2915 /* Not interested in these URIs */
2919 if (session
->base_path
!= NULL
) {
2920 free(session
->base_path
);
2921 session
->base_path
= NULL
;
2924 /* Set session base_path */
2925 session
->base_path
= strdup(uris
[i
].subdir
);
2926 if (!session
->base_path
) {
2927 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2930 ret
= LTTNG_ERR_NOMEM
;
2933 DBG2("Setting base path \"%s\" for session \"%s\"",
2943 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2945 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
, struct lttng_uri
*uris
)
2948 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2949 struct ltt_ust_session
*usess
= session
->ust_session
;
2951 LTTNG_ASSERT(session
);
2953 LTTNG_ASSERT(nb_uri
> 0);
2955 /* Can't set consumer URI if the session is active. */
2956 if (session
->active
) {
2957 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2962 * Set the session base path if any. This is done inside
2963 * cmd_set_consumer_uri to preserve backward compatibility of the
2964 * previous session creation api vs the session descriptor api.
2966 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2967 if (ret
!= LTTNG_OK
) {
2971 /* Set the "global" consumer URIs */
2972 for (i
= 0; i
< nb_uri
; i
++) {
2973 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
], LTTNG_DOMAIN_NONE
);
2974 if (ret
!= LTTNG_OK
) {
2979 /* Set UST session URIs */
2980 if (session
->ust_session
) {
2981 for (i
= 0; i
< nb_uri
; i
++) {
2982 ret
= add_uri_to_consumer(session
,
2983 session
->ust_session
->consumer
,
2986 if (ret
!= LTTNG_OK
) {
2992 /* Set kernel session URIs */
2993 if (session
->kernel_session
) {
2994 for (i
= 0; i
< nb_uri
; i
++) {
2995 ret
= add_uri_to_consumer(session
,
2996 session
->kernel_session
->consumer
,
2998 LTTNG_DOMAIN_KERNEL
);
2999 if (ret
!= LTTNG_OK
) {
3006 * Make sure to set the session in output mode after we set URI since a
3007 * session can be created without URL (thus flagged in no output mode).
3009 session
->output_traces
= 1;
3011 ksess
->output_traces
= 1;
3015 usess
->output_traces
= 1;
3025 static enum lttng_error_code
3026 set_session_output_from_descriptor(struct ltt_session
*session
,
3027 const struct lttng_session_descriptor
*descriptor
)
3030 enum lttng_error_code ret_code
= LTTNG_OK
;
3031 enum lttng_session_descriptor_type session_type
=
3032 lttng_session_descriptor_get_type(descriptor
);
3033 enum lttng_session_descriptor_output_type output_type
=
3034 lttng_session_descriptor_get_output_type(descriptor
);
3035 struct lttng_uri uris
[2] = {};
3036 size_t uri_count
= 0;
3038 switch (output_type
) {
3039 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3041 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3042 lttng_session_descriptor_get_local_output_uri(descriptor
, &uris
[0]);
3045 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3046 lttng_session_descriptor_get_network_output_uris(descriptor
, &uris
[0], &uris
[1]);
3050 ret_code
= LTTNG_ERR_INVALID
;
3054 switch (session_type
) {
3055 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3057 struct snapshot_output
*new_output
= NULL
;
3059 new_output
= snapshot_output_alloc();
3061 ret_code
= LTTNG_ERR_NOMEM
;
3065 ret
= snapshot_output_init_with_uri(session
,
3066 DEFAULT_SNAPSHOT_MAX_SIZE
,
3072 &session
->snapshot
);
3074 ret_code
= (ret
== -ENOMEM
) ? LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3075 snapshot_output_destroy(new_output
);
3078 snapshot_add_output(&session
->snapshot
, new_output
);
3081 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3082 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3084 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3088 ret_code
= LTTNG_ERR_INVALID
;
3095 static enum lttng_error_code
3096 cmd_create_session_from_descriptor(struct lttng_session_descriptor
*descriptor
,
3097 const lttng_sock_cred
*creds
,
3098 const char *home_path
)
3101 enum lttng_error_code ret_code
;
3102 const char *session_name
;
3103 struct ltt_session
*new_session
= NULL
;
3104 enum lttng_session_descriptor_status descriptor_status
;
3106 session_lock_list();
3108 if (*home_path
!= '/') {
3109 ERR("Home path provided by client is not absolute");
3110 ret_code
= LTTNG_ERR_INVALID
;
3115 descriptor_status
= lttng_session_descriptor_get_session_name(descriptor
, &session_name
);
3116 switch (descriptor_status
) {
3117 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3119 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3120 session_name
= NULL
;
3123 ret_code
= LTTNG_ERR_INVALID
;
3127 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, &new_session
);
3128 if (ret_code
!= LTTNG_OK
) {
3132 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3137 if (ret_code
!= LTTNG_OK
) {
3141 /* Announce the session's destruction to the notification thread when it is destroyed. */
3142 ret
= session_add_destroy_notifier(
3144 [](const struct ltt_session
*session
, void *user_data
__attribute__((unused
))) {
3145 (void) notification_thread_command_remove_session(
3146 the_notification_thread_handle
, session
->id
);
3150 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3152 ret
= LTTNG_ERR_NOMEM
;
3156 if (!session_name
) {
3157 ret
= lttng_session_descriptor_set_session_name(descriptor
, new_session
->name
);
3159 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3164 if (!lttng_session_descriptor_is_output_destination_initialized(descriptor
)) {
3166 * Only include the session's creation time in the output
3167 * destination if the name of the session itself was
3168 * not auto-generated.
3170 ret_code
= lttng_session_descriptor_set_default_output(
3171 descriptor
, session_name
? &new_session
->creation_time
: NULL
, home_path
);
3172 if (ret_code
!= LTTNG_OK
) {
3176 new_session
->has_user_specified_directory
=
3177 lttng_session_descriptor_has_output_directory(descriptor
);
3180 switch (lttng_session_descriptor_get_type(descriptor
)) {
3181 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3182 new_session
->snapshot_mode
= 1;
3184 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3185 new_session
->live_timer
=
3186 lttng_session_descriptor_live_get_timer_interval(descriptor
);
3192 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3193 if (ret_code
!= LTTNG_OK
) {
3196 new_session
->consumer
->enabled
= 1;
3197 ret_code
= LTTNG_OK
;
3199 /* Release reference provided by the session_create function. */
3200 session_put(new_session
);
3201 if (ret_code
!= LTTNG_OK
&& new_session
) {
3202 /* Release the global reference on error. */
3203 session_destroy(new_session
);
3205 session_unlock_list();
3209 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
,
3211 struct lttng_session_descriptor
**return_descriptor
)
3214 size_t payload_size
;
3215 struct lttng_dynamic_buffer payload
;
3216 struct lttng_buffer_view home_dir_view
;
3217 struct lttng_buffer_view session_descriptor_view
;
3218 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3219 enum lttng_error_code ret_code
;
3221 lttng_dynamic_buffer_init(&payload
);
3222 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>= LTTNG_PATH_MAX
) {
3223 ret_code
= LTTNG_ERR_INVALID
;
3226 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3227 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3228 ret_code
= LTTNG_ERR_INVALID
;
3232 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3233 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3234 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3236 ret_code
= LTTNG_ERR_NOMEM
;
3240 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3242 ERR("Reception of session descriptor failed, aborting.");
3243 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3247 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3248 &payload
, 0, cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3249 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3250 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3251 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3252 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3256 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3258 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3259 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3260 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3261 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3262 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3266 ret
= lttng_session_descriptor_create_from_buffer(&session_descriptor_view
,
3267 &session_descriptor
);
3269 ERR("Failed to create session descriptor from payload of \"create session\" command");
3270 ret_code
= LTTNG_ERR_INVALID
;
3275 * Sets the descriptor's auto-generated properties (name, output) if
3279 cmd_create_session_from_descriptor(session_descriptor
,
3281 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3282 if (ret_code
!= LTTNG_OK
) {
3286 ret_code
= LTTNG_OK
;
3287 *return_descriptor
= session_descriptor
;
3288 session_descriptor
= NULL
;
3290 lttng_dynamic_buffer_reset(&payload
);
3291 lttng_session_descriptor_destroy(session_descriptor
);
3295 static void cmd_destroy_session_reply(const struct ltt_session
*session
, void *_reply_context
)
3299 const struct cmd_destroy_session_reply_context
*reply_context
=
3300 (cmd_destroy_session_reply_context
*) _reply_context
;
3301 struct lttng_dynamic_buffer payload
;
3302 struct lttcomm_session_destroy_command_header cmd_header
;
3303 struct lttng_trace_archive_location
*location
= NULL
;
3304 struct lttcomm_lttng_msg llm
= {
3305 .cmd_type
= LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION
,
3306 .ret_code
= reply_context
->destruction_status
,
3308 .cmd_header_size
= sizeof(struct lttcomm_session_destroy_command_header
),
3312 size_t payload_size_before_location
;
3314 lttng_dynamic_buffer_init(&payload
);
3316 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3318 ERR("Failed to append session destruction message");
3322 cmd_header
.rotation_state
= (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3323 session
->rotation_state
:
3324 LTTNG_ROTATION_STATE_NO_ROTATION
);
3325 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
, sizeof(cmd_header
));
3327 ERR("Failed to append session destruction command header");
3331 if (!reply_context
->implicit_rotation_on_destroy
) {
3332 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3336 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3337 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3342 location
= session_get_trace_archive_location(session
);
3344 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3349 payload_size_before_location
= payload
.size
;
3350 comm_ret
= lttng_trace_archive_location_serialize(location
, &payload
);
3351 lttng_trace_archive_location_put(location
);
3353 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3357 /* Update the message to indicate the location's length. */
3358 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3359 payload
.size
- payload_size_before_location
;
3361 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
, payload
.data
, payload
.size
);
3362 if (comm_ret
!= (ssize_t
) payload
.size
) {
3363 ERR("Failed to send result of the destruction of session \"%s\" to client",
3367 ret
= close(reply_context
->reply_sock_fd
);
3369 PERROR("Failed to close client socket in deferred session destroy reply");
3371 lttng_dynamic_buffer_reset(&payload
);
3372 free(_reply_context
);
3376 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3378 * Called with session lock held.
3380 int cmd_destroy_session(struct ltt_session
*session
,
3381 struct notification_thread_handle
*notification_thread_handle
,
3385 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3386 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3389 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3390 if (!reply_context
) {
3391 ret
= LTTNG_ERR_NOMEM
;
3395 reply_context
->reply_sock_fd
= *sock_fd
;
3399 LTTNG_ASSERT(session
);
3401 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3402 if (session
->active
) {
3403 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3405 ret
= cmd_stop_trace(session
);
3406 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3407 /* Carry on with the destruction of the session. */
3408 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3410 lttng_strerror(-ret
));
3411 destruction_last_error
= (lttng_error_code
) ret
;
3415 if (session
->rotation_schedule_timer_enabled
) {
3416 if (timer_session_rotation_schedule_timer_stop(session
)) {
3417 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3419 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3423 if (session
->rotate_size
) {
3424 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3425 session
->rotate_size
= 0;
3428 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3430 * Perform a last rotation on destruction if rotations have
3431 * occurred during the session's lifetime.
3433 ret
= cmd_rotate_session(
3434 session
, NULL
, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3435 if (ret
!= LTTNG_OK
) {
3436 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3438 lttng_strerror(-ret
));
3439 destruction_last_error
= (lttng_error_code
) -ret
;
3441 if (reply_context
) {
3442 reply_context
->implicit_rotation_on_destroy
= true;
3444 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3446 * The user has not triggered a session rotation. However, to
3447 * ensure all data has been consumed, the session is rotated
3448 * to a 'null' trace chunk before it is destroyed.
3450 * This is a "quiet" rotation meaning that no notification is
3451 * emitted and no renaming of the current trace chunk takes
3454 ret
= cmd_rotate_session(
3455 session
, NULL
, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3457 * Rotation operations may not be supported by the kernel
3458 * tracer. Hence, do not consider this implicit rotation as
3459 * a session destruction error. The library has already stopped
3460 * the session and waited for pending data; there is nothing
3461 * left to do but complete the destruction of the session.
3463 if (ret
!= LTTNG_OK
&& ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3464 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3466 lttng_strerror(ret
));
3467 destruction_last_error
= (lttng_error_code
) -ret
;
3471 if (session
->shm_path
[0]) {
3473 * When a session is created with an explicit shm_path,
3474 * the consumer daemon will create its shared memory files
3475 * at that location and will *not* unlink them. This is normal
3476 * as the intention of that feature is to make it possible
3477 * to retrieve the content of those files should a crash occur.
3479 * To ensure the content of those files can be used, the
3480 * sessiond daemon will replicate the content of the metadata
3481 * cache in a metadata file.
3483 * On clean-up, it is expected that the consumer daemon will
3484 * unlink the shared memory files and that the session daemon
3485 * will unlink the metadata file. Then, the session's directory
3486 * in the shm path can be removed.
3488 * Unfortunately, a flaw in the design of the sessiond's and
3489 * consumerd's tear down of channels makes it impossible to
3490 * determine when the sessiond _and_ the consumerd have both
3491 * destroyed their representation of a channel. For one, the
3492 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3493 * callbacks in both daemons.
3495 * However, it is also impossible for the sessiond to know when
3496 * the consumer daemon is done destroying its channel(s) since
3497 * it occurs as a reaction to the closing of the channel's file
3498 * descriptor. There is no resulting communication initiated
3499 * from the consumerd to the sessiond to confirm that the
3500 * operation is completed (and was successful).
3502 * Until this is all fixed, the session daemon checks for the
3503 * removal of the session's shm path which makes it possible
3504 * to safely advertise a session as having been destroyed.
3506 * Prior to this fix, it was not possible to reliably save
3507 * a session making use of the --shm-path option, destroy it,
3508 * and load it again. This is because the creation of the
3509 * session would fail upon seeing the session's shm path
3510 * already in existence.
3512 * Note that none of the error paths in the check for the
3513 * directory's existence return an error. This is normal
3514 * as there isn't much that can be done. The session will
3515 * be destroyed properly, except that we can't offer the
3516 * guarantee that the same session can be re-created.
3518 current_completion_handler
= &destroy_completion_handler
.handler
;
3519 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3521 sizeof(destroy_completion_handler
.shm_path
));
3526 * The session is destroyed. However, note that the command context
3527 * still holds a reference to the session, thus delaying its destruction
3528 * _at least_ up to the point when that reference is released.
3530 session_destroy(session
);
3531 if (reply_context
) {
3532 reply_context
->destruction_status
= destruction_last_error
;
3533 ret
= session_add_destroy_notifier(
3534 session
, cmd_destroy_session_reply
, (void *) reply_context
);
3536 ret
= LTTNG_ERR_FATAL
;
3548 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3550 int cmd_register_consumer(struct ltt_session
*session
,
3551 enum lttng_domain_type domain
,
3552 const char *sock_path
,
3553 struct consumer_data
*cdata
)
3556 struct consumer_socket
*socket
= NULL
;
3558 LTTNG_ASSERT(session
);
3559 LTTNG_ASSERT(cdata
);
3560 LTTNG_ASSERT(sock_path
);
3563 case LTTNG_DOMAIN_KERNEL
:
3565 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3567 LTTNG_ASSERT(ksess
);
3569 /* Can't register a consumer if there is already one */
3570 if (ksess
->consumer_fds_sent
!= 0) {
3571 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3575 sock
= lttcomm_connect_unix_sock(sock_path
);
3577 ret
= LTTNG_ERR_CONNECT_FAIL
;
3580 cdata
->cmd_sock
= sock
;
3582 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3583 if (socket
== NULL
) {
3586 PERROR("close register consumer");
3588 cdata
->cmd_sock
= -1;
3589 ret
= LTTNG_ERR_FATAL
;
3593 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3594 if (socket
->lock
== NULL
) {
3595 PERROR("zmalloc pthread mutex");
3596 ret
= LTTNG_ERR_FATAL
;
3600 pthread_mutex_init(socket
->lock
, NULL
);
3601 socket
->registered
= 1;
3604 consumer_add_socket(socket
, ksess
->consumer
);
3607 pthread_mutex_lock(&cdata
->pid_mutex
);
3609 pthread_mutex_unlock(&cdata
->pid_mutex
);
3614 /* TODO: Userspace tracing */
3615 ret
= LTTNG_ERR_UND
;
3623 consumer_destroy_socket(socket
);
3629 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3631 ssize_t
cmd_list_domains(struct ltt_session
*session
, struct lttng_domain
**domains
)
3636 struct lttng_ht_iter iter
;
3638 if (session
->kernel_session
!= NULL
) {
3639 DBG3("Listing domains found kernel domain");
3643 if (session
->ust_session
!= NULL
) {
3644 DBG3("Listing domains found UST global domain");
3648 cds_lfht_for_each_entry (
3649 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3650 if (agt
->being_used
) {
3661 *domains
= calloc
<lttng_domain
>(nb_dom
);
3662 if (*domains
== NULL
) {
3663 ret
= LTTNG_ERR_FATAL
;
3667 if (session
->kernel_session
!= NULL
) {
3668 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3670 /* Kernel session buffer type is always GLOBAL */
3671 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3676 if (session
->ust_session
!= NULL
) {
3677 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3678 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3682 cds_lfht_for_each_entry (
3683 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3684 if (agt
->being_used
) {
3685 (*domains
)[index
].type
= agt
->domain
;
3686 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3696 /* Return negative value to differentiate return code */
3701 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3703 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3704 struct ltt_session
*session
,
3705 struct lttng_payload
*payload
)
3709 struct lttcomm_list_command_header cmd_header
= {};
3710 size_t cmd_header_offset
;
3711 enum lttng_error_code ret_code
;
3716 DBG("Listing channels for session %s", session
->name
);
3718 cmd_header_offset
= payload
->buffer
.size
;
3720 /* Reserve space for command reply header. */
3721 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3722 cmd_header_offset
+ sizeof(cmd_header
));
3724 ret_code
= LTTNG_ERR_NOMEM
;
3729 case LTTNG_DOMAIN_KERNEL
:
3731 /* Kernel channels */
3732 struct ltt_kernel_channel
*kchan
;
3733 if (session
->kernel_session
!= NULL
) {
3734 cds_list_for_each_entry (
3735 kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
3736 uint64_t discarded_events
, lost_packets
;
3737 struct lttng_channel_extended
*extended
;
3739 extended
= (struct lttng_channel_extended
*)
3740 kchan
->channel
->attr
.extended
.ptr
;
3742 ret
= get_kernel_runtime_stats(
3743 session
, kchan
, &discarded_events
, &lost_packets
);
3745 ret_code
= LTTNG_ERR_UNK
;
3750 * Update the discarded_events and lost_packets
3751 * count for the channel
3753 extended
->discarded_events
= discarded_events
;
3754 extended
->lost_packets
= lost_packets
;
3756 ret
= lttng_channel_serialize(kchan
->channel
, &payload
->buffer
);
3758 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3759 kchan
->channel
->name
);
3760 ret_code
= LTTNG_ERR_UNK
;
3769 case LTTNG_DOMAIN_UST
:
3771 struct lttng_ht_iter iter
;
3772 struct ltt_ust_channel
*uchan
;
3775 cds_lfht_for_each_entry (session
->ust_session
->domain_global
.channels
->ht
,
3779 uint64_t discarded_events
= 0, lost_packets
= 0;
3780 struct lttng_channel
*channel
= NULL
;
3781 struct lttng_channel_extended
*extended
;
3783 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3785 ret_code
= LTTNG_ERR_NOMEM
;
3789 extended
= (struct lttng_channel_extended
*) channel
->attr
.extended
.ptr
;
3791 ret
= get_ust_runtime_stats(
3792 session
, uchan
, &discarded_events
, &lost_packets
);
3794 lttng_channel_destroy(channel
);
3795 ret_code
= LTTNG_ERR_UNK
;
3799 extended
->discarded_events
= discarded_events
;
3800 extended
->lost_packets
= lost_packets
;
3802 ret
= lttng_channel_serialize(channel
, &payload
->buffer
);
3804 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3806 lttng_channel_destroy(channel
);
3807 ret_code
= LTTNG_ERR_UNK
;
3811 lttng_channel_destroy(channel
);
3821 if (i
> UINT32_MAX
) {
3822 ERR("Channel count would overflow the channel listing command's reply");
3823 ret_code
= LTTNG_ERR_OVERFLOW
;
3827 /* Update command reply header. */
3828 cmd_header
.count
= (uint32_t) i
;
3829 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
, sizeof(cmd_header
));
3830 ret_code
= LTTNG_OK
;
3837 * Command LTTNG_LIST_EVENTS processed by the client thread.
3839 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3840 struct ltt_session
*session
,
3842 struct lttng_payload
*reply_payload
)
3844 int buffer_resize_ret
;
3845 enum lttng_error_code ret_code
= LTTNG_OK
;
3846 struct lttcomm_list_command_header reply_command_header
= {};
3847 size_t reply_command_header_offset
;
3848 unsigned int nb_events
= 0;
3850 assert(reply_payload
);
3852 /* Reserve space for command reply header. */
3853 reply_command_header_offset
= reply_payload
->buffer
.size
;
3854 buffer_resize_ret
= lttng_dynamic_buffer_set_size(
3855 &reply_payload
->buffer
,
3856 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
3857 if (buffer_resize_ret
) {
3858 ret_code
= LTTNG_ERR_NOMEM
;
3863 case LTTNG_DOMAIN_KERNEL
:
3864 if (session
->kernel_session
!= NULL
) {
3865 ret_code
= list_lttng_kernel_events(
3866 channel_name
, session
->kernel_session
, reply_payload
, &nb_events
);
3870 case LTTNG_DOMAIN_UST
:
3872 if (session
->ust_session
!= NULL
) {
3874 list_lttng_ust_global_events(channel_name
,
3875 &session
->ust_session
->domain_global
,
3882 case LTTNG_DOMAIN_LOG4J
:
3883 case LTTNG_DOMAIN_JUL
:
3884 case LTTNG_DOMAIN_PYTHON
:
3885 if (session
->ust_session
) {
3886 struct lttng_ht_iter iter
;
3890 cds_lfht_for_each_entry (
3891 session
->ust_session
->agents
->ht
, &iter
.iter
, agt
, node
.node
) {
3892 if (agt
->domain
== domain
) {
3893 ret_code
= list_lttng_agent_events(
3894 agt
, reply_payload
, &nb_events
);
3903 ret_code
= LTTNG_ERR_UND
;
3907 if (nb_events
> UINT32_MAX
) {
3908 ret_code
= LTTNG_ERR_OVERFLOW
;
3912 /* Update command reply header. */
3913 reply_command_header
.count
= (uint32_t) nb_events
;
3914 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
,
3915 &reply_command_header
,
3916 sizeof(reply_command_header
));
3923 * Using the session list, filled a lttng_session array to send back to the
3924 * client for session listing.
3926 * The session list lock MUST be acquired before calling this function. Use
3927 * session_lock_list() and session_unlock_list().
3929 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3930 size_t session_count
,
3936 struct ltt_session
*session
;
3937 struct ltt_session_list
*list
= session_get_list();
3938 struct lttng_session_extended
*extended
= (typeof(extended
)) (&sessions
[session_count
]);
3940 DBG("Getting all available session for UID %d GID %d", uid
, gid
);
3942 * Iterate over session list and append data after the control struct in
3945 cds_list_for_each_entry (session
, &list
->head
, list
) {
3946 if (!session_get(session
)) {
3950 * Only list the sessions the user can control.
3952 if (!session_access_ok(session
, uid
) || session
->destroyed
) {
3953 session_put(session
);
3957 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3958 struct ltt_ust_session
*usess
= session
->ust_session
;
3960 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3961 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3962 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3963 ret
= build_network_session_path(
3964 sessions
[i
].path
, sizeof(sessions
[i
].path
), session
);
3966 ret
= snprintf(sessions
[i
].path
,
3967 sizeof(sessions
[i
].path
),
3969 session
->consumer
->dst
.session_root_path
);
3972 PERROR("snprintf session path");
3973 session_put(session
);
3977 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3978 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3979 sessions
[i
].enabled
= session
->active
;
3980 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3981 sessions
[i
].live_timer_interval
= session
->live_timer
;
3982 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3983 extended
[i
].creation_time
.is_set
= 1;
3985 session_put(session
);
3990 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3991 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3993 int cmd_data_pending(struct ltt_session
*session
)
3996 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3997 struct ltt_ust_session
*usess
= session
->ust_session
;
3999 LTTNG_ASSERT(session
);
4001 DBG("Data pending for session %s", session
->name
);
4003 /* Session MUST be stopped to ask for data availability. */
4004 if (session
->active
) {
4005 ret
= LTTNG_ERR_SESSION_STARTED
;
4009 * If stopped, just make sure we've started before else the above call
4010 * will always send that there is data pending.
4012 * The consumer assumes that when the data pending command is received,
4013 * the trace has been started before or else no output data is written
4014 * by the streams which is a condition for data pending. So, this is
4015 * *VERY* important that we don't ask the consumer before a start
4018 if (!session
->has_been_started
) {
4024 /* A rotation is still pending, we have to wait. */
4025 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4026 DBG("Rotate still pending for session %s", session
->name
);
4031 if (ksess
&& ksess
->consumer
) {
4032 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4034 /* Data is still being extracted for the kernel. */
4039 if (usess
&& usess
->consumer
) {
4040 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4042 /* Data is still being extracted for the kernel. */
4047 /* Data is ready to be read by a viewer */
4055 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4057 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4059 int cmd_snapshot_add_output(struct ltt_session
*session
,
4060 const struct lttng_snapshot_output
*output
,
4064 struct snapshot_output
*new_output
;
4066 LTTNG_ASSERT(session
);
4067 LTTNG_ASSERT(output
);
4069 DBG("Cmd snapshot add output for session %s", session
->name
);
4072 * Can't create an output if the session is not set in no-output mode.
4074 if (session
->output_traces
) {
4075 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4079 if (session
->has_non_mmap_channel
) {
4080 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4084 /* Only one output is allowed until we have the "tee" feature. */
4085 if (session
->snapshot
.nb_output
== 1) {
4086 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4090 new_output
= snapshot_output_alloc();
4092 ret
= LTTNG_ERR_NOMEM
;
4096 ret
= snapshot_output_init(session
,
4103 &session
->snapshot
);
4105 if (ret
== -ENOMEM
) {
4106 ret
= LTTNG_ERR_NOMEM
;
4108 ret
= LTTNG_ERR_INVALID
;
4114 snapshot_add_output(&session
->snapshot
, new_output
);
4116 *id
= new_output
->id
;
4123 snapshot_output_destroy(new_output
);
4129 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4131 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4133 int cmd_snapshot_del_output(struct ltt_session
*session
, const struct lttng_snapshot_output
*output
)
4136 struct snapshot_output
*sout
= NULL
;
4138 LTTNG_ASSERT(session
);
4139 LTTNG_ASSERT(output
);
4144 * Permission denied to create an output if the session is not
4145 * set in no output mode.
4147 if (session
->output_traces
) {
4148 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4153 DBG("Cmd snapshot del output id %" PRIu32
" for session %s",
4156 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4157 } else if (*output
->name
!= '\0') {
4158 DBG("Cmd snapshot del output name %s for session %s", output
->name
, session
->name
);
4159 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4162 ret
= LTTNG_ERR_INVALID
;
4166 snapshot_delete_output(&session
->snapshot
, sout
);
4167 snapshot_output_destroy(sout
);
4176 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4178 * If no output is available, outputs is untouched and 0 is returned.
4180 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4182 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4183 struct lttng_snapshot_output
**outputs
)
4186 struct lttng_snapshot_output
*list
= NULL
;
4187 struct lttng_ht_iter iter
;
4188 struct snapshot_output
*output
;
4190 LTTNG_ASSERT(session
);
4191 LTTNG_ASSERT(outputs
);
4193 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4196 * Permission denied to create an output if the session is not
4197 * set in no output mode.
4199 if (session
->output_traces
) {
4200 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4204 if (session
->snapshot
.nb_output
== 0) {
4209 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4211 ret
= -LTTNG_ERR_NOMEM
;
4215 /* Copy list from session to the new list object. */
4217 cds_lfht_for_each_entry (session
->snapshot
.output_ht
->ht
, &iter
.iter
, output
, node
.node
) {
4218 LTTNG_ASSERT(output
->consumer
);
4219 list
[idx
].id
= output
->id
;
4220 list
[idx
].max_size
= output
->max_size
;
4221 if (lttng_strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
))) {
4222 ret
= -LTTNG_ERR_INVALID
;
4225 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4226 if (lttng_strncpy(list
[idx
].ctrl_url
,
4227 output
->consumer
->dst
.session_root_path
,
4228 sizeof(list
[idx
].ctrl_url
))) {
4229 ret
= -LTTNG_ERR_INVALID
;
4234 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4236 sizeof(list
[idx
].ctrl_url
));
4238 ret
= -LTTNG_ERR_NOMEM
;
4243 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4245 sizeof(list
[idx
].data_url
));
4247 ret
= -LTTNG_ERR_NOMEM
;
4256 ret
= session
->snapshot
.nb_output
;
4265 * Check if we can regenerate the metadata for this session.
4266 * Only kernel, UST per-uid and non-live sessions are supported.
4268 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4270 static int check_regenerate_metadata_support(struct ltt_session
*session
)
4274 LTTNG_ASSERT(session
);
4276 if (session
->live_timer
!= 0) {
4277 ret
= LTTNG_ERR_LIVE_SESSION
;
4280 if (!session
->active
) {
4281 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4284 if (session
->ust_session
) {
4285 switch (session
->ust_session
->buffer_type
) {
4286 case LTTNG_BUFFER_PER_UID
:
4288 case LTTNG_BUFFER_PER_PID
:
4289 ret
= LTTNG_ERR_PER_PID_SESSION
;
4293 ret
= LTTNG_ERR_UNK
;
4297 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4298 session
->consumer
->relay_minor_version
< 8) {
4299 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4309 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4311 * Ask the consumer to truncate the existing metadata file(s) and
4312 * then regenerate the metadata. Live and per-pid sessions are not
4313 * supported and return an error.
4315 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4317 int cmd_regenerate_metadata(struct ltt_session
*session
)
4321 LTTNG_ASSERT(session
);
4323 ret
= check_regenerate_metadata_support(session
);
4328 if (session
->kernel_session
) {
4329 ret
= kernctl_session_regenerate_metadata(session
->kernel_session
->fd
);
4331 ERR("Failed to regenerate the kernel metadata");
4336 if (session
->ust_session
) {
4337 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4339 ERR("Failed to regenerate the UST metadata");
4343 DBG("Cmd metadata regenerate for session %s", session
->name
);
4351 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4353 * Ask the tracer to regenerate a new statedump.
4355 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4357 int cmd_regenerate_statedump(struct ltt_session
*session
)
4361 LTTNG_ASSERT(session
);
4363 if (!session
->active
) {
4364 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4368 if (session
->kernel_session
) {
4369 ret
= kernctl_session_regenerate_statedump(session
->kernel_session
->fd
);
4371 * Currently, the statedump in kernel can only fail if out
4375 if (ret
== -ENOMEM
) {
4376 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4378 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4380 ERR("Failed to regenerate the kernel statedump");
4385 if (session
->ust_session
) {
4386 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4388 * Currently, the statedump in UST always returns 0.
4391 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4392 ERR("Failed to regenerate the UST statedump");
4396 DBG("Cmd regenerate statedump for session %s", session
->name
);
4403 static enum lttng_error_code
4404 synchronize_tracer_notifier_register(struct notification_thread_handle
*notification_thread
,
4405 struct lttng_trigger
*trigger
,
4406 const struct lttng_credentials
*cmd_creds
)
4408 enum lttng_error_code ret_code
;
4409 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4410 const char *trigger_name
;
4411 uid_t trigger_owner
;
4412 enum lttng_trigger_status trigger_status
;
4413 const enum lttng_domain_type trigger_domain
=
4414 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4416 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4417 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4419 LTTNG_ASSERT(condition
);
4420 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4421 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4423 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4424 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4426 session_lock_list();
4427 switch (trigger_domain
) {
4428 case LTTNG_DOMAIN_KERNEL
:
4430 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4431 if (ret_code
!= LTTNG_OK
) {
4432 enum lttng_error_code notif_thread_unregister_ret
;
4434 notif_thread_unregister_ret
=
4435 notification_thread_command_unregister_trigger(notification_thread
,
4438 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4439 /* Return the original error code. */
4440 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4442 (int) trigger_owner
,
4448 case LTTNG_DOMAIN_UST
:
4449 ust_app_global_update_all_event_notifier_rules();
4451 case LTTNG_DOMAIN_JUL
:
4452 case LTTNG_DOMAIN_LOG4J
:
4453 case LTTNG_DOMAIN_PYTHON
:
4455 /* Agent domains. */
4456 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4459 agt
= agent_create(trigger_domain
);
4461 ret_code
= LTTNG_ERR_NOMEM
;
4462 goto end_unlock_session_list
;
4465 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4468 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4469 if (ret_code
!= LTTNG_OK
) {
4470 goto end_unlock_session_list
;
4475 case LTTNG_DOMAIN_NONE
:
4480 ret_code
= LTTNG_OK
;
4481 end_unlock_session_list
:
4482 session_unlock_list();
4486 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4487 struct lttng_trigger
*trigger
,
4488 bool is_trigger_anonymous
,
4489 struct notification_thread_handle
*notification_thread
,
4490 struct lttng_trigger
**return_trigger
)
4492 enum lttng_error_code ret_code
;
4493 const char *trigger_name
;
4494 uid_t trigger_owner
;
4495 enum lttng_trigger_status trigger_status
;
4497 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4498 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4500 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4501 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4503 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4505 (int) trigger_owner
,
4506 (int) lttng_credentials_get_uid(cmd_creds
));
4509 * Validate the trigger credentials against the command credentials.
4510 * Only the root user can register a trigger with non-matching
4513 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4514 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4515 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4517 (int) trigger_owner
,
4518 (int) lttng_credentials_get_uid(cmd_creds
));
4519 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4525 * The bytecode generation also serves as a validation step for the
4526 * bytecode expressions.
4528 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4529 if (ret_code
!= LTTNG_OK
) {
4530 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4532 (int) trigger_owner
,
4538 * A reference to the trigger is acquired by the notification thread.
4539 * It is safe to return the same trigger to the caller since it the
4540 * other user holds a reference.
4542 * The trigger is modified during the execution of the
4543 * "register trigger" command. However, by the time the command returns,
4544 * it is safe to use without any locking as its properties are
4547 ret_code
= notification_thread_command_register_trigger(
4548 notification_thread
, trigger
, is_trigger_anonymous
);
4549 if (ret_code
!= LTTNG_OK
) {
4550 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4552 (int) trigger_owner
,
4557 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4558 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4561 * Synchronize tracers if the trigger adds an event notifier.
4563 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4564 ret_code
= synchronize_tracer_notifier_register(
4565 notification_thread
, trigger
, cmd_creds
);
4566 if (ret_code
!= LTTNG_OK
) {
4567 ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code
));
4573 * Return an updated trigger to the client.
4575 * Since a modified version of the same trigger is returned, acquire a
4576 * reference to the trigger so the caller doesn't have to care if those
4577 * are distinct instances or not.
4579 if (ret_code
== LTTNG_OK
) {
4580 lttng_trigger_get(trigger
);
4581 *return_trigger
= trigger
;
4582 /* Ownership of trigger was transferred to caller. */
4589 static enum lttng_error_code
4590 synchronize_tracer_notifier_unregister(const struct lttng_trigger
*trigger
)
4592 enum lttng_error_code ret_code
;
4593 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
4594 const enum lttng_domain_type trigger_domain
=
4595 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4597 LTTNG_ASSERT(condition
);
4598 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4599 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4601 session_lock_list();
4602 switch (trigger_domain
) {
4603 case LTTNG_DOMAIN_KERNEL
:
4604 ret_code
= kernel_unregister_event_notifier(trigger
);
4605 if (ret_code
!= LTTNG_OK
) {
4606 goto end_unlock_session_list
;
4610 case LTTNG_DOMAIN_UST
:
4611 ust_app_global_update_all_event_notifier_rules();
4613 case LTTNG_DOMAIN_JUL
:
4614 case LTTNG_DOMAIN_LOG4J
:
4615 case LTTNG_DOMAIN_PYTHON
:
4617 /* Agent domains. */
4618 struct agent
*agt
= agent_find_by_event_notifier_domain(trigger_domain
);
4621 * This trigger was never registered in the first place. Calling
4622 * this function under those circumstances is an internal error.
4625 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4626 if (ret_code
!= LTTNG_OK
) {
4627 goto end_unlock_session_list
;
4632 case LTTNG_DOMAIN_NONE
:
4637 ret_code
= LTTNG_OK
;
4639 end_unlock_session_list
:
4640 session_unlock_list();
4644 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4645 const struct lttng_trigger
*trigger
,
4646 struct notification_thread_handle
*notification_thread
)
4648 enum lttng_error_code ret_code
;
4649 const char *trigger_name
;
4650 uid_t trigger_owner
;
4651 enum lttng_trigger_status trigger_status
;
4652 struct lttng_trigger
*sessiond_trigger
= NULL
;
4654 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4655 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4656 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4657 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4659 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4661 (int) trigger_owner
,
4662 (int) lttng_credentials_get_uid(cmd_creds
));
4665 * Validate the trigger credentials against the command credentials.
4666 * Only the root user can unregister a trigger with non-matching
4669 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger
), cmd_creds
)) {
4670 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4671 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4673 (int) trigger_owner
,
4674 (int) lttng_credentials_get_uid(cmd_creds
));
4675 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4680 /* Fetch the sessiond side trigger object. */
4681 ret_code
= notification_thread_command_get_trigger(
4682 notification_thread
, trigger
, &sessiond_trigger
);
4683 if (ret_code
!= LTTNG_OK
) {
4684 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4686 (int) trigger_owner
,
4691 LTTNG_ASSERT(sessiond_trigger
);
4694 * From this point on, no matter what, consider the trigger
4697 * We set the unregistered state of the sessiond side trigger object in
4698 * the client thread since we want to minimize the possibility of the
4699 * notification thread being stalled due to a long execution of an
4700 * action that required the trigger lock.
4702 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4704 ret_code
= notification_thread_command_unregister_trigger(notification_thread
, trigger
);
4705 if (ret_code
!= LTTNG_OK
) {
4706 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4708 (int) trigger_owner
,
4714 * Synchronize tracers if the trigger removes an event notifier.
4715 * Do this even if the trigger unregistration failed to at least stop
4716 * the tracers from producing notifications associated with this
4719 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4720 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4721 if (ret_code
!= LTTNG_OK
) {
4722 ERR("Error unregistering trigger to tracer.");
4728 lttng_trigger_put(sessiond_trigger
);
4732 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4733 struct notification_thread_handle
*notification_thread
,
4734 struct lttng_triggers
**return_triggers
)
4737 enum lttng_error_code ret_code
;
4738 struct lttng_triggers
*triggers
= NULL
;
4740 /* Get the set of triggers from the notification thread. */
4741 ret_code
= notification_thread_command_list_triggers(
4742 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4743 if (ret_code
!= LTTNG_OK
) {
4747 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4749 ret_code
= LTTNG_ERR_UNK
;
4753 *return_triggers
= triggers
;
4755 ret_code
= LTTNG_OK
;
4757 lttng_triggers_destroy(triggers
);
4761 enum lttng_error_code
4762 cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4763 const struct lttng_error_query
*query
,
4764 struct lttng_error_query_results
**_results
,
4765 struct notification_thread_handle
*notification_thread
)
4767 enum lttng_error_code ret_code
;
4768 const struct lttng_trigger
*query_target_trigger
;
4769 const struct lttng_action
*query_target_action
= NULL
;
4770 struct lttng_trigger
*matching_trigger
= NULL
;
4771 const char *trigger_name
;
4772 uid_t trigger_owner
;
4773 enum lttng_trigger_status trigger_status
;
4774 struct lttng_error_query_results
*results
= NULL
;
4776 switch (lttng_error_query_get_target_type(query
)) {
4777 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4778 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4780 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4781 query_target_trigger
= lttng_error_query_condition_borrow_target(query
);
4783 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4784 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(query
);
4790 LTTNG_ASSERT(query_target_trigger
);
4792 ret_code
= notification_thread_command_get_trigger(
4793 notification_thread
, query_target_trigger
, &matching_trigger
);
4794 if (ret_code
!= LTTNG_OK
) {
4798 /* No longer needed. */
4799 query_target_trigger
= NULL
;
4801 if (lttng_error_query_get_target_type(query
) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4802 /* Get the sessiond-side version of the target action. */
4803 query_target_action
=
4804 lttng_error_query_action_borrow_action_target(query
, matching_trigger
);
4807 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4808 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4809 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
, &trigger_owner
);
4810 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4812 results
= lttng_error_query_results_create();
4814 ret_code
= LTTNG_ERR_NOMEM
;
4818 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4820 (int) trigger_owner
,
4821 (int) lttng_credentials_get_uid(cmd_creds
));
4824 * Validate the trigger credentials against the command credentials.
4825 * Only the root user can target a trigger with non-matching
4828 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger
),
4830 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4831 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4833 (int) trigger_owner
,
4834 (int) lttng_credentials_get_uid(cmd_creds
));
4835 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4840 switch (lttng_error_query_get_target_type(query
)) {
4841 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4842 trigger_status
= lttng_trigger_add_error_results(matching_trigger
, results
);
4844 switch (trigger_status
) {
4845 case LTTNG_TRIGGER_STATUS_OK
:
4848 ret_code
= LTTNG_ERR_UNK
;
4853 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4856 lttng_trigger_condition_add_error_results(matching_trigger
, results
);
4858 switch (trigger_status
) {
4859 case LTTNG_TRIGGER_STATUS_OK
:
4862 ret_code
= LTTNG_ERR_UNK
;
4868 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4870 const enum lttng_action_status action_status
=
4871 lttng_action_add_error_query_results(query_target_action
, results
);
4873 switch (action_status
) {
4874 case LTTNG_ACTION_STATUS_OK
:
4877 ret_code
= LTTNG_ERR_UNK
;
4888 *_results
= results
;
4890 ret_code
= LTTNG_OK
;
4892 lttng_trigger_put(matching_trigger
);
4893 lttng_error_query_results_destroy(results
);
4898 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4899 * snapshot output is *not* set with a remote destination.
4901 * Return LTTNG_OK on success or a LTTNG_ERR code.
4903 static enum lttng_error_code
set_relayd_for_snapshot(struct consumer_output
*output
,
4904 const struct ltt_session
*session
)
4906 enum lttng_error_code status
= LTTNG_OK
;
4907 struct lttng_ht_iter iter
;
4908 struct consumer_socket
*socket
;
4909 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4910 const char *base_path
;
4912 LTTNG_ASSERT(output
);
4913 LTTNG_ASSERT(session
);
4915 DBG2("Set relayd object from snapshot output");
4917 if (session
->current_trace_chunk
) {
4918 enum lttng_trace_chunk_status chunk_status
= lttng_trace_chunk_get_id(
4919 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
4921 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4922 current_chunk_id
.is_set
= true;
4924 ERR("Failed to get current trace chunk id");
4925 status
= LTTNG_ERR_UNK
;
4930 /* Ignore if snapshot consumer output is not network. */
4931 if (output
->type
!= CONSUMER_DST_NET
) {
4936 * The snapshot record URI base path overrides the session
4939 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4940 base_path
= output
->dst
.net
.control
.subdir
;
4942 base_path
= session
->base_path
;
4946 * For each consumer socket, create and send the relayd object of the
4950 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
4951 pthread_mutex_lock(socket
->lock
);
4952 status
= send_consumer_relayd_sockets(
4959 session
->live_timer
,
4960 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4961 session
->creation_time
,
4962 session
->name_contains_creation_time
);
4963 pthread_mutex_unlock(socket
->lock
);
4964 if (status
!= LTTNG_OK
) {
4976 * Record a kernel snapshot.
4978 * Return LTTNG_OK on success or a LTTNG_ERR code.
4980 static enum lttng_error_code
record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
4981 const struct consumer_output
*output
,
4982 const struct ltt_session
*session
,
4983 uint64_t nb_packets_per_stream
)
4985 enum lttng_error_code status
;
4987 LTTNG_ASSERT(ksess
);
4988 LTTNG_ASSERT(output
);
4989 LTTNG_ASSERT(session
);
4991 status
= kernel_snapshot_record(ksess
, output
, nb_packets_per_stream
);
4996 * Record a UST snapshot.
4998 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5000 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5001 const struct consumer_output
*output
,
5002 const struct ltt_session
*session
,
5003 uint64_t nb_packets_per_stream
)
5005 enum lttng_error_code status
;
5007 LTTNG_ASSERT(usess
);
5008 LTTNG_ASSERT(output
);
5009 LTTNG_ASSERT(session
);
5011 status
= ust_app_snapshot_record(usess
, output
, nb_packets_per_stream
);
5015 static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session
*session
,
5016 uint64_t cur_nr_packets
)
5018 uint64_t tot_size
= 0;
5020 if (session
->kernel_session
) {
5021 struct ltt_kernel_channel
*chan
;
5022 const struct ltt_kernel_session
*ksess
= session
->kernel_session
;
5024 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
5025 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5027 * Don't take channel into account if we
5028 * already grab all its packets.
5032 tot_size
+= chan
->channel
->attr
.subbuf_size
* chan
->stream_count
;
5036 if (session
->ust_session
) {
5037 const struct ltt_ust_session
*usess
= session
->ust_session
;
5039 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
, cur_nr_packets
);
5046 * Calculate the number of packets we can grab from each stream that
5047 * fits within the overall snapshot max size.
5049 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5050 * the number of packets per stream.
5052 * TODO: this approach is not perfect: we consider the worse case
5053 * (packet filling the sub-buffers) as an upper bound, but we could do
5054 * better if we do this calculation while we actually grab the packet
5055 * content: we would know how much padding we don't actually store into
5058 * This algorithm is currently bounded by the number of packets per
5061 * Since we call this algorithm before actually grabbing the data, it's
5062 * an approximation: for instance, applications could appear/disappear
5063 * in between this call and actually grabbing data.
5065 static int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5069 uint64_t cur_nb_packets
= 0;
5072 return 0; /* Infinite */
5075 size_left
= max_size
;
5077 uint64_t one_more_packet_tot_size
;
5079 one_more_packet_tot_size
=
5080 get_session_size_one_more_packet_per_stream(session
, cur_nb_packets
);
5081 if (!one_more_packet_tot_size
) {
5082 /* We are already grabbing all packets. */
5085 size_left
-= one_more_packet_tot_size
;
5086 if (size_left
< 0) {
5091 if (!cur_nb_packets
&& size_left
!= max_size
) {
5092 /* Not enough room to grab one packet of each stream, error. */
5095 return cur_nb_packets
;
5098 static enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5099 const struct snapshot_output
*snapshot_output
)
5101 int64_t nb_packets_per_stream
;
5102 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5104 enum lttng_error_code ret_code
= LTTNG_OK
;
5105 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5106 struct consumer_output
*original_ust_consumer_output
= NULL
;
5107 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5108 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5109 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5111 ret
= snprintf(snapshot_chunk_name
,
5112 sizeof(snapshot_chunk_name
),
5114 snapshot_output
->name
,
5115 snapshot_output
->datetime
,
5116 snapshot_output
->nb_snapshot
);
5117 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5118 ERR("Failed to format snapshot name");
5119 ret_code
= LTTNG_ERR_INVALID
;
5122 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5123 snapshot_output
->name
,
5125 snapshot_chunk_name
);
5126 if (!session
->kernel_session
&& !session
->ust_session
) {
5127 ERR("Failed to record snapshot as no channels exist");
5128 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5132 if (session
->kernel_session
) {
5133 original_kernel_consumer_output
= session
->kernel_session
->consumer
;
5134 snapshot_kernel_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5135 strcpy(snapshot_kernel_consumer_output
->chunk_path
, snapshot_chunk_name
);
5137 /* Copy the original domain subdir. */
5138 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5139 original_kernel_consumer_output
->domain_subdir
);
5141 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5142 original_kernel_consumer_output
);
5144 ERR("Failed to copy consumer sockets from snapshot output configuration");
5145 ret_code
= LTTNG_ERR_NOMEM
;
5148 ret_code
= set_relayd_for_snapshot(snapshot_kernel_consumer_output
, session
);
5149 if (ret_code
!= LTTNG_OK
) {
5150 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5153 session
->kernel_session
->consumer
= snapshot_kernel_consumer_output
;
5155 if (session
->ust_session
) {
5156 original_ust_consumer_output
= session
->ust_session
->consumer
;
5157 snapshot_ust_consumer_output
= consumer_copy_output(snapshot_output
->consumer
);
5158 strcpy(snapshot_ust_consumer_output
->chunk_path
, snapshot_chunk_name
);
5160 /* Copy the original domain subdir. */
5161 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5162 original_ust_consumer_output
->domain_subdir
);
5164 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5165 original_ust_consumer_output
);
5167 ERR("Failed to copy consumer sockets from snapshot output configuration");
5168 ret_code
= LTTNG_ERR_NOMEM
;
5171 ret_code
= set_relayd_for_snapshot(snapshot_ust_consumer_output
, session
);
5172 if (ret_code
!= LTTNG_OK
) {
5173 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5176 session
->ust_session
->consumer
= snapshot_ust_consumer_output
;
5179 snapshot_trace_chunk
= session_create_new_trace_chunk(
5181 snapshot_kernel_consumer_output
?: snapshot_ust_consumer_output
,
5182 consumer_output_get_base_path(snapshot_output
->consumer
),
5183 snapshot_chunk_name
);
5184 if (!snapshot_trace_chunk
) {
5185 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5187 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5190 LTTNG_ASSERT(!session
->current_trace_chunk
);
5191 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5192 lttng_trace_chunk_put(snapshot_trace_chunk
);
5193 snapshot_trace_chunk
= NULL
;
5195 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5197 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5201 nb_packets_per_stream
=
5202 get_session_nb_packets_per_stream(session
, snapshot_output
->max_size
);
5203 if (nb_packets_per_stream
< 0) {
5204 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5205 goto error_close_trace_chunk
;
5208 if (session
->kernel_session
) {
5209 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5210 snapshot_kernel_consumer_output
,
5212 nb_packets_per_stream
);
5213 if (ret_code
!= LTTNG_OK
) {
5214 goto error_close_trace_chunk
;
5218 if (session
->ust_session
) {
5219 ret_code
= record_ust_snapshot(session
->ust_session
,
5220 snapshot_ust_consumer_output
,
5222 nb_packets_per_stream
);
5223 if (ret_code
!= LTTNG_OK
) {
5224 goto error_close_trace_chunk
;
5228 error_close_trace_chunk
:
5229 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5230 ERR("Failed to release the current trace chunk of session \"%s\"", session
->name
);
5231 ret_code
= LTTNG_ERR_UNK
;
5234 if (session_close_trace_chunk(session
,
5235 snapshot_trace_chunk
,
5236 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
,
5239 * Don't goto end; make sure the chunk is closed for the session
5240 * to allow future snapshots.
5242 ERR("Failed to close snapshot trace chunk of session \"%s\"", session
->name
);
5243 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5246 lttng_trace_chunk_put(snapshot_trace_chunk
);
5247 snapshot_trace_chunk
= NULL
;
5249 if (original_ust_consumer_output
) {
5250 session
->ust_session
->consumer
= original_ust_consumer_output
;
5252 if (original_kernel_consumer_output
) {
5253 session
->kernel_session
->consumer
= original_kernel_consumer_output
;
5255 consumer_output_put(snapshot_ust_consumer_output
);
5256 consumer_output_put(snapshot_kernel_consumer_output
);
5261 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5263 * The wait parameter is ignored so this call always wait for the snapshot to
5264 * complete before returning.
5266 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5268 int cmd_snapshot_record(struct ltt_session
*session
,
5269 const struct lttng_snapshot_output
*output
,
5270 int wait
__attribute__((unused
)))
5272 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5274 unsigned int snapshot_success
= 0;
5276 struct snapshot_output
*tmp_output
= NULL
;
5278 LTTNG_ASSERT(session
);
5279 LTTNG_ASSERT(output
);
5281 DBG("Cmd snapshot record for session %s", session
->name
);
5283 /* Get the datetime for the snapshot output directory. */
5284 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
, sizeof(datetime
));
5286 cmd_ret
= LTTNG_ERR_INVALID
;
5291 * Permission denied to create an output if the session is not
5292 * set in no output mode.
5294 if (session
->output_traces
) {
5295 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5299 /* The session needs to be started at least once. */
5300 if (!session
->has_been_started
) {
5301 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5305 /* Use temporary output for the session. */
5306 if (*output
->ctrl_url
!= '\0') {
5307 tmp_output
= snapshot_output_alloc();
5309 cmd_ret
= LTTNG_ERR_NOMEM
;
5313 ret
= snapshot_output_init(session
,
5322 if (ret
== -ENOMEM
) {
5323 cmd_ret
= LTTNG_ERR_NOMEM
;
5325 cmd_ret
= LTTNG_ERR_INVALID
;
5329 /* Use the global session count for the temporary snapshot. */
5330 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5332 /* Use the global datetime */
5333 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5334 cmd_ret
= snapshot_record(session
, tmp_output
);
5335 if (cmd_ret
!= LTTNG_OK
) {
5338 snapshot_success
= 1;
5340 struct snapshot_output
*sout
;
5341 struct lttng_ht_iter iter
;
5344 cds_lfht_for_each_entry (
5345 session
->snapshot
.output_ht
->ht
, &iter
.iter
, sout
, node
.node
) {
5346 struct snapshot_output output_copy
;
5349 * Make a local copy of the output and override output
5350 * parameters with those provided as part of the
5353 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5355 if (output
->max_size
!= (uint64_t) -1ULL) {
5356 output_copy
.max_size
= output
->max_size
;
5359 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5360 memcpy(output_copy
.datetime
, datetime
, sizeof(datetime
));
5362 /* Use temporary name. */
5363 if (*output
->name
!= '\0') {
5364 if (lttng_strncpy(output_copy
.name
,
5366 sizeof(output_copy
.name
))) {
5367 cmd_ret
= LTTNG_ERR_INVALID
;
5373 cmd_ret
= snapshot_record(session
, &output_copy
);
5374 if (cmd_ret
!= LTTNG_OK
) {
5378 snapshot_success
= 1;
5383 if (snapshot_success
) {
5384 session
->snapshot
.nb_snapshot
++;
5386 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5391 snapshot_output_destroy(tmp_output
);
5397 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5399 int cmd_set_session_shm_path(struct ltt_session
*session
, const char *shm_path
)
5402 LTTNG_ASSERT(session
);
5405 * Can only set shm path before session is started.
5407 if (session
->has_been_started
) {
5408 return LTTNG_ERR_SESSION_STARTED
;
5411 strncpy(session
->shm_path
, shm_path
, sizeof(session
->shm_path
));
5412 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5418 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5420 * Ask the consumer to rotate the session output directory.
5421 * The session lock must be held.
5423 * Returns LTTNG_OK on success or else a negative LTTng error code.
5425 int cmd_rotate_session(struct ltt_session
*session
,
5426 struct lttng_rotate_session_return
*rotate_return
,
5427 bool quiet_rotation
,
5428 enum lttng_trace_chunk_command_type command
)
5431 uint64_t ongoing_rotation_chunk_id
;
5432 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5433 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5434 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5435 enum lttng_trace_chunk_status chunk_status
;
5436 bool failed_to_rotate
= false;
5437 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5439 LTTNG_ASSERT(session
);
5441 if (!session
->has_been_started
) {
5442 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5447 * Explicit rotation is not supported for live sessions.
5448 * However, live sessions can perform a quiet rotation on
5450 * Rotation is not supported for snapshot traces (no output).
5452 if ((!quiet_rotation
&& session
->live_timer
) || !session
->output_traces
) {
5453 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5457 /* Unsupported feature in lttng-relayd before 2.11. */
5458 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5459 (session
->consumer
->relay_major_version
== 2 &&
5460 session
->consumer
->relay_minor_version
< 11)) {
5461 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5465 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5466 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5467 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5471 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5472 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5474 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5479 * After a stop, we only allow one rotation to occur, the other ones are
5480 * useless until a new start.
5482 if (session
->rotated_after_last_stop
) {
5483 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5485 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5490 * After a stop followed by a clear, disallow following rotations a they would
5491 * generate empty chunks.
5493 if (session
->cleared_after_last_stop
) {
5494 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5496 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5500 if (session
->active
) {
5501 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
, NULL
, NULL
);
5502 if (!new_trace_chunk
) {
5503 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5509 * The current trace chunk becomes the chunk being archived.
5511 * After this point, "chunk_being_archived" must absolutely
5512 * be closed on the consumer(s), otherwise it will never be
5513 * cleaned-up, which will result in a leak.
5515 ret
= session_set_trace_chunk(session
, new_trace_chunk
, &chunk_being_archived
);
5517 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5521 if (session
->kernel_session
) {
5522 cmd_ret
= kernel_rotate_session(session
);
5523 if (cmd_ret
!= LTTNG_OK
) {
5524 failed_to_rotate
= true;
5525 rotation_fail_code
= cmd_ret
;
5528 if (session
->ust_session
) {
5529 cmd_ret
= ust_app_rotate_session(session
);
5530 if (cmd_ret
!= LTTNG_OK
) {
5531 failed_to_rotate
= true;
5532 rotation_fail_code
= cmd_ret
;
5536 if (!session
->active
) {
5537 session
->rotated_after_last_stop
= true;
5540 if (!chunk_being_archived
) {
5541 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5543 if (failed_to_rotate
) {
5544 cmd_ret
= rotation_fail_code
;
5551 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5552 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
, &ongoing_rotation_chunk_id
);
5553 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5555 ret
= session_close_trace_chunk(
5556 session
, chunk_being_archived
, command
, session
->last_chunk_path
);
5558 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5562 if (failed_to_rotate
) {
5563 cmd_ret
= rotation_fail_code
;
5567 session
->quiet_rotation
= quiet_rotation
;
5568 ret
= timer_session_rotation_pending_check_start(session
, DEFAULT_ROTATE_PENDING_TIMER
);
5570 cmd_ret
= LTTNG_ERR_UNK
;
5574 if (rotate_return
) {
5575 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5578 session
->chunk_being_archived
= chunk_being_archived
;
5579 chunk_being_archived
= NULL
;
5580 if (!quiet_rotation
) {
5581 ret
= notification_thread_command_session_rotation_ongoing(
5582 the_notification_thread_handle
, session
->id
, ongoing_rotation_chunk_id
);
5583 if (ret
!= LTTNG_OK
) {
5584 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5586 cmd_ret
= (lttng_error_code
) ret
;
5590 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5592 ongoing_rotation_chunk_id
);
5594 lttng_trace_chunk_put(new_trace_chunk
);
5595 lttng_trace_chunk_put(chunk_being_archived
);
5596 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5599 if (session_reset_rotation_state(session
, LTTNG_ROTATION_STATE_ERROR
)) {
5600 ERR("Failed to reset rotation state of session \"%s\"", session
->name
);
5606 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5608 * Check if the session has finished its rotation.
5610 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5612 int cmd_rotate_get_info(struct ltt_session
*session
,
5613 struct lttng_rotation_get_info_return
*info_return
,
5614 uint64_t rotation_id
)
5616 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5617 enum lttng_rotation_state rotation_state
;
5619 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
,
5621 session
->most_recent_chunk_id
.value
);
5623 if (session
->chunk_being_archived
) {
5624 enum lttng_trace_chunk_status chunk_status
;
5627 chunk_status
= lttng_trace_chunk_get_id(session
->chunk_being_archived
, &chunk_id
);
5628 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5630 rotation_state
= rotation_id
== chunk_id
? LTTNG_ROTATION_STATE_ONGOING
:
5631 LTTNG_ROTATION_STATE_EXPIRED
;
5633 if (session
->last_archived_chunk_id
.is_set
&&
5634 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5635 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5637 rotation_state
= session
->rotation_state
;
5641 switch (rotation_state
) {
5642 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5643 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5646 case LTTNG_ROTATION_STATE_EXPIRED
:
5647 DBG("Reporting that the rotation state of rotation id %" PRIu64
5648 " of session \"%s\" has expired",
5652 case LTTNG_ROTATION_STATE_ONGOING
:
5653 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5657 case LTTNG_ROTATION_STATE_COMPLETED
:
5661 char *current_tracing_path_reply
;
5662 size_t current_tracing_path_reply_len
;
5664 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5668 switch (session_get_consumer_destination_type(session
)) {
5669 case CONSUMER_DST_LOCAL
:
5670 current_tracing_path_reply
= info_return
->location
.local
.absolute_path
;
5671 current_tracing_path_reply_len
=
5672 sizeof(info_return
->location
.local
.absolute_path
);
5673 info_return
->location_type
=
5674 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5675 fmt_ret
= asprintf(&chunk_path
,
5676 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5677 session_get_base_path(session
),
5678 session
->last_archived_chunk_name
);
5679 if (fmt_ret
== -1) {
5680 PERROR("Failed to format the path of the last archived trace chunk");
5681 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5682 cmd_ret
= LTTNG_ERR_UNK
;
5686 case CONSUMER_DST_NET
:
5688 uint16_t ctrl_port
, data_port
;
5690 current_tracing_path_reply
= info_return
->location
.relay
.relative_path
;
5691 current_tracing_path_reply_len
=
5692 sizeof(info_return
->location
.relay
.relative_path
);
5693 /* Currently the only supported relay protocol. */
5694 info_return
->location
.relay
.protocol
=
5695 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5697 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5698 session_get_net_consumer_hostname(session
),
5699 sizeof(info_return
->location
.relay
.host
));
5701 ERR("Failed to copy host name to rotate_get_info reply");
5702 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5703 cmd_ret
= LTTNG_ERR_SET_URL
;
5707 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5708 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5709 info_return
->location
.relay
.ports
.data
= data_port
;
5710 info_return
->location_type
=
5711 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5712 chunk_path
= strdup(session
->last_chunk_path
);
5714 ERR("Failed to allocate the path of the last archived trace chunk");
5715 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5716 cmd_ret
= LTTNG_ERR_UNK
;
5725 fmt_ret
= lttng_strncpy(
5726 current_tracing_path_reply
, chunk_path
, current_tracing_path_reply_len
);
5729 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5730 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5731 cmd_ret
= LTTNG_ERR_UNK
;
5737 case LTTNG_ROTATION_STATE_ERROR
:
5738 DBG("Reporting that an error occurred during rotation %" PRIu64
5739 " of session \"%s\"",
5749 info_return
->status
= (int32_t) rotation_state
;
5754 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5756 * Configure the automatic rotation parameters.
5757 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5758 * 'activate' to false means deactivate the rotation schedule and validate that
5759 * 'new_value' has the same value as the currently active value.
5761 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5763 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5765 enum lttng_rotation_schedule_type schedule_type
,
5767 struct notification_thread_handle
*notification_thread_handle
)
5770 uint64_t *parameter_value
;
5772 LTTNG_ASSERT(session
);
5774 DBG("Cmd rotate set schedule session %s", session
->name
);
5776 if (session
->live_timer
|| !session
->output_traces
) {
5777 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5778 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5782 switch (schedule_type
) {
5783 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5784 parameter_value
= &session
->rotate_size
;
5786 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5787 parameter_value
= &session
->rotate_timer_period
;
5788 if (new_value
>= UINT_MAX
) {
5789 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
5793 ret
= LTTNG_ERR_INVALID
;
5798 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5799 ret
= LTTNG_ERR_INVALID
;
5803 /* Improper use of the API. */
5804 if (new_value
== -1ULL) {
5805 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5806 ret
= LTTNG_ERR_INVALID
;
5811 * As indicated in struct ltt_session's comments, a value of == 0 means
5812 * this schedule rotation type is not in use.
5814 * Reject the command if we were asked to activate a schedule that was
5817 if (activate
&& *parameter_value
!= 0) {
5818 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5819 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5824 * Reject the command if we were asked to deactivate a schedule that was
5827 if (!activate
&& *parameter_value
== 0) {
5828 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5829 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5834 * Reject the command if we were asked to deactivate a schedule that
5837 if (!activate
&& *parameter_value
!= new_value
) {
5838 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5839 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5843 *parameter_value
= activate
? new_value
: 0;
5845 switch (schedule_type
) {
5846 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5847 if (activate
&& session
->active
) {
5849 * Only start the timer if the session is active,
5850 * otherwise it will be started when the session starts.
5852 ret
= timer_session_rotation_schedule_timer_start(session
, new_value
);
5854 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5855 ret
= LTTNG_ERR_UNK
;
5859 ret
= timer_session_rotation_schedule_timer_stop(session
);
5861 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5862 ret
= LTTNG_ERR_UNK
;
5867 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5869 ret
= subscribe_session_consumed_size_rotation(
5870 session
, new_value
, notification_thread_handle
);
5872 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5873 ret
= LTTNG_ERR_UNK
;
5877 ret
= unsubscribe_session_consumed_size_rotation(
5878 session
, notification_thread_handle
);
5880 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5881 ret
= LTTNG_ERR_UNK
;
5887 /* Would have been caught before. */
5899 /* Wait for a given path to be removed before continuing. */
5900 static enum lttng_error_code
wait_on_path(void *path_data
)
5902 const char *shm_path
= (const char *) path_data
;
5904 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5910 ret
= stat(shm_path
, &st
);
5912 if (errno
!= ENOENT
) {
5913 PERROR("stat() returned an error while checking for the existence of the shm path");
5915 DBG("shm path no longer exists, completing the destruction of session");
5919 if (!S_ISDIR(st
.st_mode
)) {
5920 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5925 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5931 * Returns a pointer to a handler to run on completion of a command.
5932 * Returns NULL if no handler has to be run for the last command executed.
5934 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5936 struct cmd_completion_handler
*handler
= current_completion_handler
;
5938 current_completion_handler
= NULL
;
5943 * Init command subsystem.
5948 * Set network sequence index to 1 for streams to match a relayd
5949 * socket on the consumer side.
5951 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5952 relayd_net_seq_idx
= 1;
5953 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5955 DBG("Command subsystem initialized");