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
16 #include <urcu/list.h>
17 #include <urcu/uatomic.h>
19 #include <common/buffer-view.hpp>
20 #include <common/common.hpp>
21 #include <common/compat/string.hpp>
22 #include <common/defaults.hpp>
23 #include <common/dynamic-buffer.hpp>
24 #include <common/kernel-ctl/kernel-ctl.hpp>
25 #include <common/payload-view.hpp>
26 #include <common/payload.hpp>
27 #include <common/relayd/relayd.hpp>
28 #include <common/sessiond-comm/sessiond-comm.hpp>
29 #include <common/string-utils/string-utils.hpp>
30 #include <common/trace-chunk.hpp>
31 #include <common/utils.hpp>
32 #include <lttng/action/action-internal.hpp>
33 #include <lttng/action/action.h>
34 #include <lttng/channel-internal.hpp>
35 #include <lttng/channel.h>
36 #include <lttng/condition/condition-internal.hpp>
37 #include <lttng/condition/condition.h>
38 #include <lttng/condition/event-rule-matches-internal.hpp>
39 #include <lttng/condition/event-rule-matches.h>
40 #include <lttng/error-query-internal.hpp>
41 #include <lttng/event-internal.hpp>
42 #include <lttng/event-rule/event-rule-internal.hpp>
43 #include <lttng/event-rule/event-rule.h>
44 #include <lttng/location-internal.hpp>
45 #include <lttng/lttng-error.h>
46 #include <lttng/rotate-internal.hpp>
47 #include <lttng/session-descriptor-internal.hpp>
48 #include <lttng/session-internal.hpp>
49 #include <lttng/tracker.h>
50 #include <lttng/trigger/trigger-internal.hpp>
51 #include <lttng/userspace-probe-internal.hpp>
53 #include "agent-thread.hpp"
55 #include "buffer-registry.hpp"
56 #include "channel.hpp"
58 #include "consumer.hpp"
59 #include "event-notifier-error-accounting.hpp"
61 #include "health-sessiond.hpp"
62 #include "kernel-consumer.hpp"
64 #include "lttng-sessiond.hpp"
65 #include "lttng-syscall.hpp"
66 #include "notification-thread-commands.hpp"
67 #include "notification-thread.hpp"
69 #include "rotation-thread.hpp"
70 #include "session.hpp"
72 #include "tracker.hpp"
75 /* Sleep for 100ms between each check for the shm path's deletion. */
76 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
78 namespace lsu
= lttng::sessiond::ust
;
80 static enum lttng_error_code
wait_on_path(void *path
);
83 struct cmd_destroy_session_reply_context
{
85 bool implicit_rotation_on_destroy
;
87 * Indicates whether or not an error occurred while launching the
88 * destruction of a session.
90 enum lttng_error_code destruction_status
;
94 * Command completion handler that is used by the destroy command
95 * when a session that has a non-default shm_path is being destroyed.
97 * See comment in cmd_destroy_session() for the rationale.
99 struct destroy_completion_handler
{
100 struct cmd_completion_handler handler
;
101 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
102 } destroy_completion_handler
= {
105 .data
= destroy_completion_handler
.shm_path
111 * Used to keep a unique index for each relayd socket created where this value
112 * is associated with streams on the consumer so it can match the right relayd
113 * to send to. It must be accessed with the relayd_net_seq_idx_lock
116 pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
117 uint64_t relayd_net_seq_idx
;
120 static struct cmd_completion_handler
*current_completion_handler
;
121 static int validate_ust_event_name(const char *);
122 static int cmd_enable_event_internal(struct ltt_session
*session
,
123 const struct lttng_domain
*domain
,
124 char *channel_name
, struct lttng_event
*event
,
125 char *filter_expression
,
126 struct lttng_bytecode
*filter
,
127 struct lttng_event_exclusion
*exclusion
,
129 static enum lttng_error_code
cmd_enable_channel_internal(
130 struct ltt_session
*session
,
131 const struct lttng_domain
*domain
,
132 const struct lttng_channel
*_attr
,
136 * Create a session path used by list_lttng_sessions for the case that the
137 * session consumer is on the network.
139 static int build_network_session_path(char *dst
, size_t size
,
140 struct ltt_session
*session
)
142 int ret
, kdata_port
, udata_port
;
143 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
144 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
146 LTTNG_ASSERT(session
);
149 memset(tmp_urls
, 0, sizeof(tmp_urls
));
150 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
152 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
154 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
155 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
156 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
159 if (session
->ust_session
&& session
->ust_session
->consumer
) {
160 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
161 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
164 if (uuri
== NULL
&& kuri
== NULL
) {
165 uri
= &session
->consumer
->dst
.net
.control
;
166 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
167 } else if (kuri
&& uuri
) {
168 ret
= uri_compare(kuri
, uuri
);
172 /* Build uuri URL string */
173 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
180 } else if (kuri
&& uuri
== NULL
) {
182 } else if (uuri
&& kuri
== NULL
) {
186 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
192 * Do we have a UST url set. If yes, this means we have both kernel and UST
195 if (*tmp_uurl
!= '\0') {
196 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
197 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
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
, uint64_t *discarded_events
,
219 uint64_t *lost_packets
)
223 if (!session
->has_been_started
) {
225 *discarded_events
= 0;
230 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
231 session
->kernel_session
->consumer
,
237 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
238 session
->kernel_session
->consumer
,
249 * Get run-time attributes if the session has been started (discarded events,
252 static int get_ust_runtime_stats(struct ltt_session
*session
,
253 struct ltt_ust_channel
*uchan
, 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
,
278 usess
->consumer
, uchan
->id
,
279 uchan
->attr
.overwrite
,
282 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
283 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
284 uchan
, usess
->consumer
,
285 uchan
->attr
.overwrite
,
291 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
292 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
294 ERR("Unsupported buffer type");
305 * Create a list of agent domain events.
307 * Return number of events in list on success or else a negative value.
309 static enum lttng_error_code
list_lttng_agent_events(
310 struct agent
*agt
, struct lttng_payload
*reply_payload
,
311 unsigned int *nb_events
)
313 enum lttng_error_code ret_code
;
315 unsigned int local_nb_events
= 0;
316 struct agent_event
*event
;
317 struct lttng_ht_iter iter
;
318 unsigned long agent_event_count
;
321 assert(reply_payload
);
323 DBG3("Listing agent events");
326 agent_event_count
= lttng_ht_get_count(agt
->events
);
327 if (agent_event_count
== 0) {
332 if (agent_event_count
> UINT_MAX
) {
333 ret_code
= LTTNG_ERR_OVERFLOW
;
337 local_nb_events
= (unsigned int) agent_event_count
;
339 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
340 struct lttng_event
*tmp_event
= lttng_event_create();
343 ret_code
= LTTNG_ERR_NOMEM
;
347 if (lttng_strncpy(tmp_event
->name
, event
->name
, sizeof(tmp_event
->name
))) {
348 lttng_event_destroy(tmp_event
);
349 ret_code
= LTTNG_ERR_FATAL
;
353 tmp_event
->name
[sizeof(tmp_event
->name
) - 1] = '\0';
354 tmp_event
->enabled
= !!event
->enabled_count
;
355 tmp_event
->loglevel
= event
->loglevel_value
;
356 tmp_event
->loglevel_type
= event
->loglevel_type
;
358 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
359 event
->filter_expression
, 0, NULL
, reply_payload
);
360 lttng_event_destroy(tmp_event
);
362 ret_code
= LTTNG_ERR_FATAL
;
369 *nb_events
= local_nb_events
;
376 * Create a list of ust global domain events.
378 static enum lttng_error_code
list_lttng_ust_global_events(char *channel_name
,
379 struct ltt_ust_domain_global
*ust_global
,
380 struct lttng_payload
*reply_payload
,
381 unsigned int *nb_events
)
383 enum lttng_error_code ret_code
;
385 struct lttng_ht_iter iter
;
386 struct lttng_ht_node_str
*node
;
387 struct ltt_ust_channel
*uchan
;
388 struct ltt_ust_event
*uevent
;
389 unsigned long channel_event_count
;
390 unsigned int local_nb_events
= 0;
392 assert(reply_payload
);
395 DBG("Listing UST global events for channel %s", channel_name
);
399 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
400 node
= lttng_ht_iter_get_node_str(&iter
);
402 ret_code
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
406 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
408 channel_event_count
= lttng_ht_get_count(uchan
->events
);
409 if (channel_event_count
== 0) {
415 if (channel_event_count
> UINT_MAX
) {
416 ret_code
= LTTNG_ERR_OVERFLOW
;
420 local_nb_events
= (unsigned int) channel_event_count
;
422 DBG3("Listing UST global %d events", *nb_events
);
424 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
425 struct lttng_event
*tmp_event
= NULL
;
427 if (uevent
->internal
) {
428 /* This event should remain hidden from clients */
433 tmp_event
= lttng_event_create();
435 ret_code
= LTTNG_ERR_NOMEM
;
439 if (lttng_strncpy(tmp_event
->name
, uevent
->attr
.name
,
440 LTTNG_SYMBOL_NAME_LEN
)) {
441 ret_code
= LTTNG_ERR_FATAL
;
442 lttng_event_destroy(tmp_event
);
446 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
447 tmp_event
->enabled
= uevent
->enabled
;
449 switch (uevent
->attr
.instrumentation
) {
450 case LTTNG_UST_ABI_TRACEPOINT
:
451 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
453 case LTTNG_UST_ABI_PROBE
:
454 tmp_event
->type
= LTTNG_EVENT_PROBE
;
456 case LTTNG_UST_ABI_FUNCTION
:
457 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
461 tmp_event
->loglevel
= uevent
->attr
.loglevel
;
462 switch (uevent
->attr
.loglevel_type
) {
463 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
464 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
466 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
467 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
469 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
470 tmp_event
->loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
473 if (uevent
->filter
) {
474 tmp_event
->filter
= 1;
476 if (uevent
->exclusion
) {
477 tmp_event
->exclusion
= 1;
481 * We do not care about the filter bytecode and the fd from the
482 * userspace_probe_location.
484 ret
= lttng_event_serialize(tmp_event
, uevent
->exclusion
? uevent
->exclusion
->count
: 0,
485 uevent
->exclusion
? (char **) uevent
->exclusion
->names
: NULL
,
486 uevent
->filter_expression
, 0, NULL
, reply_payload
);
487 lttng_event_destroy(tmp_event
);
489 ret_code
= LTTNG_ERR_FATAL
;
495 /* nb_events is already set at this point. */
497 *nb_events
= local_nb_events
;
504 * Fill lttng_event array of all kernel events in the channel.
506 static enum lttng_error_code
list_lttng_kernel_events(char *channel_name
,
507 struct ltt_kernel_session
*kernel_session
,
508 struct lttng_payload
*reply_payload
,
509 unsigned int *nb_events
)
511 enum lttng_error_code ret_code
;
513 struct ltt_kernel_event
*event
;
514 struct ltt_kernel_channel
*kchan
;
516 assert(reply_payload
);
518 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
520 ret_code
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
524 *nb_events
= kchan
->event_count
;
526 DBG("Listing events for channel %s", kchan
->channel
->name
);
528 if (*nb_events
== 0) {
533 /* Kernel channels */
534 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
535 struct lttng_event
*tmp_event
= lttng_event_create();
538 ret_code
= LTTNG_ERR_NOMEM
;
542 if (lttng_strncpy(tmp_event
->name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
)) {
543 lttng_event_destroy(tmp_event
);
544 ret_code
= LTTNG_ERR_FATAL
;
549 tmp_event
->name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
550 tmp_event
->enabled
= event
->enabled
;
551 tmp_event
->filter
= (unsigned char) !!event
->filter_expression
;
553 switch (event
->event
->instrumentation
) {
554 case LTTNG_KERNEL_ABI_TRACEPOINT
:
555 tmp_event
->type
= LTTNG_EVENT_TRACEPOINT
;
557 case LTTNG_KERNEL_ABI_KRETPROBE
:
558 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
559 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
560 sizeof(struct lttng_kernel_abi_kprobe
));
562 case LTTNG_KERNEL_ABI_KPROBE
:
563 tmp_event
->type
= LTTNG_EVENT_PROBE
;
564 memcpy(&tmp_event
->attr
.probe
, &event
->event
->u
.kprobe
,
565 sizeof(struct lttng_kernel_abi_kprobe
));
567 case LTTNG_KERNEL_ABI_UPROBE
:
568 tmp_event
->type
= LTTNG_EVENT_USERSPACE_PROBE
;
570 case LTTNG_KERNEL_ABI_FUNCTION
:
571 tmp_event
->type
= LTTNG_EVENT_FUNCTION
;
572 memcpy(&(tmp_event
->attr
.ftrace
), &event
->event
->u
.ftrace
,
573 sizeof(struct lttng_kernel_abi_function
));
575 case LTTNG_KERNEL_ABI_NOOP
:
576 tmp_event
->type
= LTTNG_EVENT_NOOP
;
578 case LTTNG_KERNEL_ABI_SYSCALL
:
579 tmp_event
->type
= LTTNG_EVENT_SYSCALL
;
581 case LTTNG_KERNEL_ABI_ALL
:
588 if (event
->userspace_probe_location
) {
589 struct lttng_userspace_probe_location
*location_copy
=
590 lttng_userspace_probe_location_copy(
591 event
->userspace_probe_location
);
593 if (!location_copy
) {
594 lttng_event_destroy(tmp_event
);
595 ret_code
= LTTNG_ERR_NOMEM
;
599 ret
= lttng_event_set_userspace_probe_location(
600 tmp_event
, location_copy
);
602 lttng_event_destroy(tmp_event
);
603 lttng_userspace_probe_location_destroy(
605 ret_code
= LTTNG_ERR_INVALID
;
610 ret
= lttng_event_serialize(tmp_event
, 0, NULL
,
611 event
->filter_expression
, 0, NULL
, reply_payload
);
612 lttng_event_destroy(tmp_event
);
614 ret_code
= LTTNG_ERR_FATAL
;
625 * Add URI so the consumer output object. Set the correct path depending on the
626 * domain adding the default trace directory.
628 static enum lttng_error_code
add_uri_to_consumer(
629 const struct ltt_session
*session
,
630 struct consumer_output
*consumer
,
631 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
634 enum lttng_error_code ret_code
= LTTNG_OK
;
638 if (consumer
== NULL
) {
639 DBG("No consumer detected. Don't add URI. Stopping.");
640 ret_code
= LTTNG_ERR_NO_CONSUMER
;
645 case LTTNG_DOMAIN_KERNEL
:
646 ret
= lttng_strncpy(consumer
->domain_subdir
,
647 DEFAULT_KERNEL_TRACE_DIR
,
648 sizeof(consumer
->domain_subdir
));
650 case LTTNG_DOMAIN_UST
:
651 ret
= lttng_strncpy(consumer
->domain_subdir
,
652 DEFAULT_UST_TRACE_DIR
,
653 sizeof(consumer
->domain_subdir
));
657 * This case is possible is we try to add the URI to the global
658 * tracing session consumer object which in this case there is
661 memset(consumer
->domain_subdir
, 0,
662 sizeof(consumer
->domain_subdir
));
666 ERR("Failed to initialize consumer output domain subdirectory");
667 ret_code
= LTTNG_ERR_FATAL
;
671 switch (uri
->dtype
) {
674 DBG2("Setting network URI to consumer");
676 if (consumer
->type
== CONSUMER_DST_NET
) {
677 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
678 consumer
->dst
.net
.control_isset
) ||
679 (uri
->stype
== LTTNG_STREAM_DATA
&&
680 consumer
->dst
.net
.data_isset
)) {
681 ret_code
= LTTNG_ERR_URL_EXIST
;
685 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
688 /* Set URI into consumer output object */
689 ret
= consumer_set_network_uri(session
, consumer
, uri
);
691 ret_code
= (lttng_error_code
) -ret
;
693 } else if (ret
== 1) {
695 * URI was the same in the consumer so we do not append the subdir
696 * again so to not duplicate output dir.
703 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
704 ret_code
= LTTNG_ERR_INVALID
;
707 DBG2("Setting trace directory path from URI to %s",
709 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
711 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
713 sizeof(consumer
->dst
.session_root_path
));
715 ret_code
= LTTNG_ERR_FATAL
;
718 consumer
->type
= CONSUMER_DST_LOCAL
;
728 * Init tracing by creating trace directory and sending fds kernel consumer.
730 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
733 struct lttng_ht_iter iter
;
734 struct consumer_socket
*socket
;
736 LTTNG_ASSERT(session
);
740 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
741 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
743 pthread_mutex_lock(socket
->lock
);
744 ret
= kernel_consumer_send_session(socket
, session
);
745 pthread_mutex_unlock(socket
->lock
);
747 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
759 * Create a socket to the relayd using the URI.
761 * On success, the relayd_sock pointer is set to the created socket.
762 * Else, it remains untouched and an LTTng error code is returned.
764 static lttng_error_code
create_connect_relayd(
765 struct lttng_uri
*uri
, struct lttcomm_relayd_sock
**relayd_sock
)
768 enum lttng_error_code status
= LTTNG_OK
;
769 struct lttcomm_relayd_sock
*rsock
;
771 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
772 RELAYD_VERSION_COMM_MINOR
);
774 status
= LTTNG_ERR_FATAL
;
779 * Connect to relayd so we can proceed with a session creation. This call
780 * can possibly block for an arbitrary amount of time to set the health
781 * state to be in poll execution.
784 ret
= relayd_connect(rsock
);
787 ERR("Unable to reach lttng-relayd");
788 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
792 /* Create socket for control stream. */
793 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
794 DBG3("Creating relayd stream socket from URI");
796 /* Check relayd version */
797 ret
= relayd_version_check(rsock
);
798 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
799 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
801 } else if (ret
< 0) {
802 ERR("Unable to reach lttng-relayd");
803 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
806 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
807 DBG3("Creating relayd data socket from URI");
809 /* Command is not valid */
810 ERR("Relayd invalid stream type: %d", uri
->stype
);
811 status
= LTTNG_ERR_INVALID
;
815 *relayd_sock
= rsock
;
820 /* The returned value is not useful since we are on an error path. */
821 (void) relayd_close(rsock
);
829 * Connect to the relayd using URI and send the socket to the right consumer.
831 * The consumer socket lock must be held by the caller.
833 * Returns LTTNG_OK on success or an LTTng error code on failure.
835 static enum lttng_error_code
send_consumer_relayd_socket(unsigned int session_id
,
836 struct lttng_uri
*relayd_uri
,
837 struct consumer_output
*consumer
,
838 struct consumer_socket
*consumer_sock
,
839 const char *session_name
,
840 const char *hostname
,
841 const char *base_path
,
842 int session_live_timer
,
843 const uint64_t *current_chunk_id
,
844 time_t session_creation_time
,
845 bool session_name_contains_creation_time
,
846 struct lttcomm_relayd_sock
& rsock
)
849 enum lttng_error_code status
= LTTNG_OK
;
851 /* Set the network sequence index if not set. */
852 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
853 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
855 * Increment net_seq_idx because we are about to transfer the
856 * new relayd socket to the consumer.
857 * Assign unique key so the consumer can match streams.
859 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
860 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
863 /* Send relayd socket to consumer. */
864 ret
= consumer_send_relayd_socket(consumer_sock
, &rsock
, consumer
, relayd_uri
->stype
,
865 session_id
, session_name
, hostname
, base_path
, session_live_timer
,
866 current_chunk_id
, session_creation_time
,
867 session_name_contains_creation_time
);
869 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
873 /* Flag that the corresponding socket was sent. */
874 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
875 consumer_sock
->control_sock_sent
= 1;
876 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
877 consumer_sock
->data_sock_sent
= 1;
881 if (status
!= LTTNG_OK
) {
883 * The consumer output for this session should not be used anymore
884 * since the relayd connection failed thus making any tracing or/and
885 * streaming not usable.
887 consumer
->enabled
= 0;
893 static bool is_trace_format_configuration_supported(
894 uint64_t supported_trace_format
, lttng::trace_format_descriptor
& trace_format
)
896 static const std::unordered_map
<enum lttng_trace_format_descriptor_type
,
897 enum lttcomm_relayd_configuration_trace_format_flag
>
899 {LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1
,
900 LTTCOMM_RELAYD_CONFIGURATION_TRACE_FORMAT_SUPPORTED_CTF1
},
903 auto it
= mapping
.find(trace_format
.type());
904 if (it
== mapping
.end()) {
908 if (!(supported_trace_format
& it
->second
)) {
916 * Send both relayd sockets to a specific consumer and domain. This is a
917 * helper function to facilitate sending the information to the consumer for a
920 * The consumer socket lock must be held by the caller.
922 * Returns LTTNG_OK, or an LTTng error code on failure.
924 static lttng_error_code
send_consumer_relayd_sockets(const ltt_session
& session
,
925 struct consumer_output
*consumer
,
926 struct consumer_socket
*sock
,
927 const char *base_path
,
928 const uint64_t *current_chunk_id
)
930 enum lttng_error_code status
= LTTNG_OK
;
931 struct lttcomm_relayd_sock
*control_sock
= nullptr;
932 struct lttcomm_relayd_sock
*data_sock
= nullptr;
934 LTTNG_ASSERT(consumer
);
937 /* Sending control relayd socket. */
938 if (!sock
->control_sock_sent
) {
940 uint64_t result_flags
= 0;
941 uint64_t supported_trace_format
= 0;
942 /* Connect to relayd and make version check if uri is the control. */
943 status
= create_connect_relayd(&consumer
->dst
.net
.control
, &control_sock
);
944 if (status
!= LTTNG_OK
) {
947 LTTNG_ASSERT(control_sock
);
949 consumer
->relay_major_version
= control_sock
->major
;
950 consumer
->relay_minor_version
= control_sock
->minor
;
952 ret
= relayd_get_configuration(control_sock
, 0, result_flags
, nullptr);
954 ERR("Unable to get relayd configuration");
955 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
959 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
960 consumer
->relay_allows_clear
= true;
963 if (!is_trace_format_configuration_supported(
964 supported_trace_format
, *session
.trace_format
)) {
965 ERR("Relayd does not support the requested trace format");
966 status
= LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_RELAY_DAEMON
;
970 status
= send_consumer_relayd_socket(session
.id
, &consumer
->dst
.net
.control
,
971 consumer
, sock
, session
.name
, session
.hostname
, base_path
,
972 session
.live_timer
, current_chunk_id
, session
.creation_time
,
973 session
.name_contains_creation_time
, *control_sock
);
975 if (status
!= LTTNG_OK
) {
980 /* Sending data relayd socket. */
981 if (!sock
->data_sock_sent
) {
982 /* Connect to relayd and make version check if uri is the control. */
983 status
= create_connect_relayd(&consumer
->dst
.net
.data
, &data_sock
);
984 if (status
!= LTTNG_OK
) {
987 LTTNG_ASSERT(data_sock
);
989 status
= send_consumer_relayd_socket(session
.id
, &consumer
->dst
.net
.data
, consumer
,
990 sock
, session
.name
, session
.hostname
, base_path
, session
.live_timer
,
991 current_chunk_id
, session
.creation_time
,
992 session
.name_contains_creation_time
, *data_sock
);
994 if (status
!= LTTNG_OK
) {
1000 if (control_sock
!= nullptr) {
1001 relayd_close(control_sock
);
1005 if (data_sock
!= nullptr) {
1006 relayd_close(data_sock
);
1014 * Setup relayd connections for a tracing session. First creates the socket to
1015 * the relayd and send them to the right domain consumer. Consumer type MUST be
1018 int cmd_setup_relayd(struct ltt_session
*session
)
1021 struct ltt_ust_session
*usess
;
1022 struct ltt_kernel_session
*ksess
;
1023 struct consumer_socket
*socket
;
1024 struct lttng_ht_iter iter
;
1025 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1027 LTTNG_ASSERT(session
);
1029 usess
= session
->ust_session
;
1030 ksess
= session
->kernel_session
;
1032 DBG("Setting relayd for session %s", session
->name
);
1035 if (session
->current_trace_chunk
) {
1036 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1037 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1039 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1040 current_chunk_id
.is_set
= true;
1042 ERR("Failed to get current trace chunk id");
1043 ret
= LTTNG_ERR_UNK
;
1048 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1049 && usess
->consumer
->enabled
) {
1050 /* For each consumer socket, send relayd sockets */
1051 cds_lfht_for_each_entry (
1052 usess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1053 pthread_mutex_lock(socket
->lock
);
1054 ret
= send_consumer_relayd_sockets(*session
, usess
->consumer
, socket
,
1056 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
1057 pthread_mutex_unlock(socket
->lock
);
1058 if (ret
!= LTTNG_OK
) {
1061 /* Session is now ready for network streaming. */
1062 session
->net_handle
= 1;
1064 session
->consumer
->relay_major_version
=
1065 usess
->consumer
->relay_major_version
;
1066 session
->consumer
->relay_minor_version
=
1067 usess
->consumer
->relay_minor_version
;
1068 session
->consumer
->relay_allows_clear
=
1069 usess
->consumer
->relay_allows_clear
;
1072 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1073 && ksess
->consumer
->enabled
) {
1074 cds_lfht_for_each_entry (
1075 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1076 pthread_mutex_lock(socket
->lock
);
1077 ret
= send_consumer_relayd_sockets(*session
, ksess
->consumer
, socket
,
1079 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
1080 pthread_mutex_unlock(socket
->lock
);
1081 if (ret
!= LTTNG_OK
) {
1084 /* Session is now ready for network streaming. */
1085 session
->net_handle
= 1;
1087 session
->consumer
->relay_major_version
=
1088 ksess
->consumer
->relay_major_version
;
1089 session
->consumer
->relay_minor_version
=
1090 ksess
->consumer
->relay_minor_version
;
1091 session
->consumer
->relay_allows_clear
=
1092 ksess
->consumer
->relay_allows_clear
;
1101 * Start a kernel session by opening all necessary streams.
1103 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1106 struct ltt_kernel_channel
*kchan
;
1108 /* Open kernel metadata */
1109 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1110 ret
= kernel_open_metadata(ksess
);
1112 ret
= LTTNG_ERR_KERN_META_FAIL
;
1117 /* Open kernel metadata stream */
1118 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1119 ret
= kernel_open_metadata_stream(ksess
);
1121 ERR("Kernel create metadata stream failed");
1122 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1127 /* For each channel */
1128 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1129 if (kchan
->stream_count
== 0) {
1130 ret
= kernel_open_channel_stream(kchan
);
1132 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1135 /* Update the stream global counter */
1136 ksess
->stream_count_global
+= ret
;
1140 /* Setup kernel consumer socket and send fds to it */
1141 ret
= init_kernel_tracing(ksess
);
1143 ret
= LTTNG_ERR_KERN_START_FAIL
;
1147 /* This start the kernel tracing */
1148 ret
= kernel_start_session(ksess
);
1150 ret
= LTTNG_ERR_KERN_START_FAIL
;
1154 /* Quiescent wait after starting trace */
1155 kernel_wait_quiescent();
1165 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1167 struct ltt_kernel_channel
*kchan
;
1168 bool error_occurred
= false;
1171 if (!ksess
|| !ksess
->active
) {
1174 DBG("Stopping kernel tracing");
1176 ret
= kernel_stop_session(ksess
);
1178 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1182 kernel_wait_quiescent();
1184 /* Flush metadata after stopping (if exists) */
1185 if (ksess
->metadata_stream_fd
>= 0) {
1186 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1188 ERR("Kernel metadata flush failed");
1189 error_occurred
= true;
1193 /* Flush all buffers after stopping */
1194 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1195 ret
= kernel_flush_buffer(kchan
);
1197 ERR("Kernel flush buffer error");
1198 error_occurred
= true;
1203 if (error_occurred
) {
1204 ret
= LTTNG_ERR_UNK
;
1213 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1215 int cmd_disable_channel(struct ltt_session
*session
,
1216 enum lttng_domain_type domain
, char *channel_name
)
1219 struct ltt_ust_session
*usess
;
1221 usess
= session
->ust_session
;
1226 case LTTNG_DOMAIN_KERNEL
:
1228 ret
= channel_kernel_disable(session
->kernel_session
,
1230 if (ret
!= LTTNG_OK
) {
1234 kernel_wait_quiescent();
1237 case LTTNG_DOMAIN_UST
:
1239 struct ltt_ust_channel
*uchan
;
1240 struct lttng_ht
*chan_ht
;
1242 chan_ht
= usess
->domain_global
.channels
;
1244 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1245 if (uchan
== NULL
) {
1246 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1250 ret
= channel_ust_disable(usess
, uchan
);
1251 if (ret
!= LTTNG_OK
) {
1257 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1269 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1271 * The wpipe arguments is used as a notifier for the kernel thread.
1273 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1277 ssize_t sock_recv_len
;
1278 struct lttng_channel
*channel
= NULL
;
1279 struct lttng_buffer_view view
;
1280 struct lttng_dynamic_buffer channel_buffer
;
1281 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1283 lttng_dynamic_buffer_init(&channel_buffer
);
1284 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1285 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1287 ret
= LTTNG_ERR_NOMEM
;
1291 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1293 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1294 ERR("Failed to receive \"enable channel\" command payload");
1295 ret
= LTTNG_ERR_INVALID
;
1299 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1300 if (!lttng_buffer_view_is_valid(&view
)) {
1301 ret
= LTTNG_ERR_INVALID
;
1305 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1306 ERR("Invalid channel payload received in \"enable channel\" command");
1307 ret
= LTTNG_ERR_INVALID
;
1311 ret
= cmd_enable_channel_internal(
1312 cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1315 lttng_dynamic_buffer_reset(&channel_buffer
);
1316 lttng_channel_destroy(channel
);
1320 static enum lttng_error_code
kernel_domain_check_trace_format_requirements(
1321 const lttng::trace_format_descriptor
& descriptor
)
1323 enum lttng_error_code ret_code
= LTTNG_OK
;
1324 switch (descriptor
.type()) {
1325 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1
:
1326 /* Supported by all kernel tracer. */
1328 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_2
:
1329 if (!kernel_supports_ctf2()) {
1330 ret_code
= LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_KERNEL_TRACER
;
1339 static enum lttng_error_code
cmd_enable_channel_internal(
1340 struct ltt_session
*session
,
1341 const struct lttng_domain
*domain
,
1342 const struct lttng_channel
*_attr
,
1345 enum lttng_error_code ret_code
;
1346 struct ltt_ust_session
*usess
= session
->ust_session
;
1347 struct lttng_ht
*chan_ht
;
1349 struct lttng_channel
*attr
= NULL
;
1351 LTTNG_ASSERT(session
);
1352 LTTNG_ASSERT(_attr
);
1353 LTTNG_ASSERT(domain
);
1355 attr
= lttng_channel_copy(_attr
);
1357 ret_code
= LTTNG_ERR_NOMEM
;
1361 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1363 /* Validate channel name */
1364 if (attr
->name
[0] == '.' ||
1365 memchr(attr
->name
, '/', len
) != NULL
) {
1366 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1370 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1375 * If the session is a live session, remove the switch timer, the
1376 * live timer does the same thing but sends also synchronisation
1377 * beacons for inactive streams.
1379 if (session
->live_timer
> 0) {
1380 attr
->attr
.live_timer_interval
= session
->live_timer
;
1381 attr
->attr
.switch_timer_interval
= 0;
1384 /* Check for feature support */
1385 switch (domain
->type
) {
1386 case LTTNG_DOMAIN_KERNEL
:
1388 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1389 /* Sampling position of buffer is not supported */
1390 WARN("Kernel tracer does not support buffer monitoring. "
1391 "Setting the monitor interval timer to 0 "
1392 "(disabled) for channel '%s' of session '%s'",
1393 attr
->name
, session
->name
);
1394 lttng_channel_set_monitor_timer_interval(attr
, 0);
1397 ret_code
= kernel_domain_check_trace_format_requirements(*session
->trace_format
);
1398 if (ret_code
!= LTTNG_OK
) {
1399 WARN("Kernel tracer does not support the configured trace format of session '%s'",
1405 case LTTNG_DOMAIN_UST
:
1407 case LTTNG_DOMAIN_JUL
:
1408 case LTTNG_DOMAIN_LOG4J
:
1409 case LTTNG_DOMAIN_PYTHON
:
1410 if (!agent_tracing_is_enabled()) {
1411 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1412 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1417 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1421 switch (domain
->type
) {
1422 case LTTNG_DOMAIN_KERNEL
:
1424 struct ltt_kernel_channel
*kchan
;
1426 kchan
= trace_kernel_get_channel_by_name(
1427 attr
->name
, session
->kernel_session
);
1428 if (kchan
== NULL
) {
1430 * Don't try to create a channel if the session has been started at
1431 * some point in time before. The tracer does not allow it.
1433 if (session
->has_been_started
) {
1434 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1438 if (session
->snapshot
.nb_output
> 0 ||
1439 session
->snapshot_mode
) {
1440 /* Enforce mmap output for snapshot sessions. */
1441 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1443 ret_code
= channel_kernel_create(
1444 session
->kernel_session
, attr
, wpipe
);
1445 if (attr
->name
[0] != '\0') {
1446 session
->kernel_session
->has_non_default_channel
= 1;
1449 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1452 if (ret_code
!= LTTNG_OK
) {
1456 kernel_wait_quiescent();
1459 case LTTNG_DOMAIN_UST
:
1460 case LTTNG_DOMAIN_JUL
:
1461 case LTTNG_DOMAIN_LOG4J
:
1462 case LTTNG_DOMAIN_PYTHON
:
1464 struct ltt_ust_channel
*uchan
;
1469 * Current agent implementation limitations force us to allow
1470 * only one channel at once in "agent" subdomains. Each
1471 * subdomain has a default channel name which must be strictly
1474 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1475 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1476 LTTNG_SYMBOL_NAME_LEN
)) {
1477 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1480 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1481 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1482 LTTNG_SYMBOL_NAME_LEN
)) {
1483 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1486 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1487 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1488 LTTNG_SYMBOL_NAME_LEN
)) {
1489 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1494 chan_ht
= usess
->domain_global
.channels
;
1496 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1497 if (uchan
== NULL
) {
1499 * Don't try to create a channel if the session has been started at
1500 * some point in time before. The tracer does not allow it.
1502 if (session
->has_been_started
) {
1503 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1507 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1508 if (attr
->name
[0] != '\0') {
1509 usess
->has_non_default_channel
= 1;
1512 ret_code
= channel_ust_enable(usess
, uchan
);
1517 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1521 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1522 session
->has_non_mmap_channel
= true;
1527 lttng_channel_destroy(attr
);
1531 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1532 struct ltt_session
*session
,
1533 enum lttng_domain_type domain
,
1534 enum lttng_process_attr process_attr
,
1535 enum lttng_tracking_policy
*policy
)
1537 enum lttng_error_code ret_code
= LTTNG_OK
;
1538 const struct process_attr_tracker
*tracker
;
1541 case LTTNG_DOMAIN_KERNEL
:
1542 if (!session
->kernel_session
) {
1543 ret_code
= LTTNG_ERR_INVALID
;
1546 tracker
= kernel_get_process_attr_tracker(
1547 session
->kernel_session
, process_attr
);
1549 case LTTNG_DOMAIN_UST
:
1550 if (!session
->ust_session
) {
1551 ret_code
= LTTNG_ERR_INVALID
;
1554 tracker
= trace_ust_get_process_attr_tracker(
1555 session
->ust_session
, process_attr
);
1558 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1562 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1564 ret_code
= LTTNG_ERR_INVALID
;
1570 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1571 struct ltt_session
*session
,
1572 enum lttng_domain_type domain
,
1573 enum lttng_process_attr process_attr
,
1574 enum lttng_tracking_policy policy
)
1576 enum lttng_error_code ret_code
= LTTNG_OK
;
1579 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1580 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1581 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1584 ret_code
= LTTNG_ERR_INVALID
;
1589 case LTTNG_DOMAIN_KERNEL
:
1590 if (!session
->kernel_session
) {
1591 ret_code
= LTTNG_ERR_INVALID
;
1594 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1595 session
->kernel_session
, process_attr
, policy
);
1597 case LTTNG_DOMAIN_UST
:
1598 if (!session
->ust_session
) {
1599 ret_code
= LTTNG_ERR_INVALID
;
1602 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1603 session
->ust_session
, process_attr
, policy
);
1606 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1613 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1614 struct ltt_session
*session
,
1615 enum lttng_domain_type domain
,
1616 enum lttng_process_attr process_attr
,
1617 const struct process_attr_value
*value
)
1619 enum lttng_error_code ret_code
= LTTNG_OK
;
1622 case LTTNG_DOMAIN_KERNEL
:
1623 if (!session
->kernel_session
) {
1624 ret_code
= LTTNG_ERR_INVALID
;
1627 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1628 session
->kernel_session
, process_attr
, value
);
1630 case LTTNG_DOMAIN_UST
:
1631 if (!session
->ust_session
) {
1632 ret_code
= LTTNG_ERR_INVALID
;
1635 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1636 session
->ust_session
, process_attr
, value
);
1639 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1646 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1647 struct ltt_session
*session
,
1648 enum lttng_domain_type domain
,
1649 enum lttng_process_attr process_attr
,
1650 const struct process_attr_value
*value
)
1652 enum lttng_error_code ret_code
= LTTNG_OK
;
1655 case LTTNG_DOMAIN_KERNEL
:
1656 if (!session
->kernel_session
) {
1657 ret_code
= LTTNG_ERR_INVALID
;
1660 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1661 session
->kernel_session
, process_attr
, value
);
1663 case LTTNG_DOMAIN_UST
:
1664 if (!session
->ust_session
) {
1665 ret_code
= LTTNG_ERR_INVALID
;
1668 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1669 session
->ust_session
, process_attr
, value
);
1672 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1679 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1680 struct ltt_session
*session
,
1681 enum lttng_domain_type domain
,
1682 enum lttng_process_attr process_attr
,
1683 struct lttng_process_attr_values
**values
)
1685 enum lttng_error_code ret_code
= LTTNG_OK
;
1686 const struct process_attr_tracker
*tracker
;
1687 enum process_attr_tracker_status status
;
1690 case LTTNG_DOMAIN_KERNEL
:
1691 if (!session
->kernel_session
) {
1692 ret_code
= LTTNG_ERR_INVALID
;
1695 tracker
= kernel_get_process_attr_tracker(
1696 session
->kernel_session
, process_attr
);
1698 case LTTNG_DOMAIN_UST
:
1699 if (!session
->ust_session
) {
1700 ret_code
= LTTNG_ERR_INVALID
;
1703 tracker
= trace_ust_get_process_attr_tracker(
1704 session
->ust_session
, process_attr
);
1707 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1712 ret_code
= LTTNG_ERR_INVALID
;
1716 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1718 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1719 ret_code
= LTTNG_OK
;
1721 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1722 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1724 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1725 ret_code
= LTTNG_ERR_NOMEM
;
1728 ret_code
= LTTNG_ERR_UNK
;
1737 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1739 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1740 struct lttng_event
*event
,
1741 char *filter_expression
,
1742 struct lttng_bytecode
*bytecode
,
1743 struct lttng_event_exclusion
*exclusion
)
1746 const char *event_name
;
1747 const struct ltt_session
*session
= cmd_ctx
->session
;
1748 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1749 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1751 DBG("Disable event command for event \'%s\'", event
->name
);
1754 * Filter and exclusions are simply not handled by the
1755 * disable event command at this time.
1759 (void) filter_expression
;
1762 /* Ignore the presence of filter or exclusion for the event */
1764 event
->exclusion
= 0;
1766 event_name
= event
->name
;
1768 /* Error out on unhandled search criteria */
1769 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1770 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1771 ret
= LTTNG_ERR_UNK
;
1778 case LTTNG_DOMAIN_KERNEL
:
1780 struct ltt_kernel_channel
*kchan
;
1781 struct ltt_kernel_session
*ksess
;
1783 ksess
= session
->kernel_session
;
1786 * If a non-default channel has been created in the
1787 * session, explicitely require that -c chan_name needs
1790 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1791 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1795 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1796 if (kchan
== NULL
) {
1797 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1801 switch (event
->type
) {
1802 case LTTNG_EVENT_ALL
:
1803 case LTTNG_EVENT_TRACEPOINT
:
1804 case LTTNG_EVENT_SYSCALL
:
1805 case LTTNG_EVENT_PROBE
:
1806 case LTTNG_EVENT_FUNCTION
:
1807 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1808 if (event_name
[0] == '\0') {
1809 ret
= event_kernel_disable_event(kchan
,
1812 ret
= event_kernel_disable_event(kchan
,
1813 event_name
, event
->type
);
1815 if (ret
!= LTTNG_OK
) {
1820 ret
= LTTNG_ERR_UNK
;
1824 kernel_wait_quiescent();
1827 case LTTNG_DOMAIN_UST
:
1829 struct ltt_ust_channel
*uchan
;
1830 struct ltt_ust_session
*usess
;
1832 usess
= session
->ust_session
;
1834 if (validate_ust_event_name(event_name
)) {
1835 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1840 * If a non-default channel has been created in the
1841 * session, explicitly require that -c chan_name needs
1844 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1845 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1849 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1851 if (uchan
== NULL
) {
1852 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1856 switch (event
->type
) {
1857 case LTTNG_EVENT_ALL
:
1859 * An empty event name means that everything
1860 * should be disabled.
1862 if (event
->name
[0] == '\0') {
1863 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1865 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1868 if (ret
!= LTTNG_OK
) {
1873 ret
= LTTNG_ERR_UNK
;
1877 DBG3("Disable UST event %s in channel %s completed", event_name
,
1881 case LTTNG_DOMAIN_LOG4J
:
1882 case LTTNG_DOMAIN_JUL
:
1883 case LTTNG_DOMAIN_PYTHON
:
1886 struct ltt_ust_session
*usess
= session
->ust_session
;
1888 LTTNG_ASSERT(usess
);
1890 switch (event
->type
) {
1891 case LTTNG_EVENT_ALL
:
1894 ret
= LTTNG_ERR_UNK
;
1898 agt
= trace_ust_find_agent(usess
, domain
);
1900 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1904 * An empty event name means that everything
1905 * should be disabled.
1907 if (event
->name
[0] == '\0') {
1908 ret
= event_agent_disable_all(usess
, agt
);
1910 ret
= event_agent_disable(usess
, agt
, event_name
);
1912 if (ret
!= LTTNG_OK
) {
1919 ret
= LTTNG_ERR_UND
;
1930 free(filter_expression
);
1935 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1937 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1938 const struct lttng_event_context
*event_context
, int kwpipe
)
1940 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1941 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1942 const struct ltt_session
*session
= cmd_ctx
->session
;
1943 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1946 * Don't try to add a context if the session has been started at
1947 * some point in time before. The tracer does not allow it and would
1948 * result in a corrupted trace.
1950 if (cmd_ctx
->session
->has_been_started
) {
1951 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1956 case LTTNG_DOMAIN_KERNEL
:
1957 LTTNG_ASSERT(session
->kernel_session
);
1959 if (session
->kernel_session
->channel_count
== 0) {
1960 /* Create default channel */
1961 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1962 if (ret
!= LTTNG_OK
) {
1965 chan_kern_created
= 1;
1967 /* Add kernel context to kernel tracer */
1968 ret
= context_kernel_add(session
->kernel_session
,
1969 event_context
, channel_name
);
1970 if (ret
!= LTTNG_OK
) {
1974 case LTTNG_DOMAIN_JUL
:
1975 case LTTNG_DOMAIN_LOG4J
:
1978 * Validate channel name.
1979 * If no channel name is given and the domain is JUL or LOG4J,
1980 * set it to the appropriate domain-specific channel name. If
1981 * a name is provided but does not match the expexted channel
1982 * name, return an error.
1984 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1985 strcmp(channel_name
,
1986 DEFAULT_JUL_CHANNEL_NAME
)) {
1987 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1989 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1990 strcmp(channel_name
,
1991 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1992 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1997 case LTTNG_DOMAIN_UST
:
1999 struct ltt_ust_session
*usess
= session
->ust_session
;
2000 unsigned int chan_count
;
2002 LTTNG_ASSERT(usess
);
2004 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
2005 if (chan_count
== 0) {
2006 struct lttng_channel
*attr
;
2007 /* Create default channel */
2008 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
2010 ret
= LTTNG_ERR_FATAL
;
2014 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
2015 if (ret
!= LTTNG_OK
) {
2019 channel_attr_destroy(attr
);
2020 chan_ust_created
= 1;
2023 ret
= context_ust_add(usess
, domain
, event_context
,
2025 if (ret
!= LTTNG_OK
) {
2031 ret
= LTTNG_ERR_UND
;
2039 if (chan_kern_created
) {
2040 struct ltt_kernel_channel
*kchan
=
2041 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2042 session
->kernel_session
);
2043 /* Created previously, this should NOT fail. */
2044 LTTNG_ASSERT(kchan
);
2045 kernel_destroy_channel(kchan
);
2048 if (chan_ust_created
) {
2049 struct ltt_ust_channel
*uchan
=
2050 trace_ust_find_channel_by_name(
2051 session
->ust_session
->domain_global
.channels
,
2052 DEFAULT_CHANNEL_NAME
);
2053 /* Created previously, this should NOT fail. */
2054 LTTNG_ASSERT(uchan
);
2055 /* Remove from the channel list of the session. */
2056 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2058 trace_ust_destroy_channel(uchan
);
2064 static inline bool name_starts_with(const char *name
, const char *prefix
)
2066 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2068 return !strncmp(name
, prefix
, max_cmp_len
);
2071 /* Perform userspace-specific event name validation */
2072 static int validate_ust_event_name(const char *name
)
2082 * Check name against all internal UST event component namespaces used
2085 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2086 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2087 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2096 * Internal version of cmd_enable_event() with a supplemental
2097 * "internal_event" flag which is used to enable internal events which should
2098 * be hidden from clients. Such events are used in the agent implementation to
2099 * enable the events through which all "agent" events are funeled.
2101 static int _cmd_enable_event(struct ltt_session
*session
,
2102 const struct lttng_domain
*domain
,
2103 char *channel_name
, struct lttng_event
*event
,
2104 char *filter_expression
,
2105 struct lttng_bytecode
*filter
,
2106 struct lttng_event_exclusion
*exclusion
,
2107 int wpipe
, bool internal_event
)
2109 int ret
= 0, channel_created
= 0;
2110 struct lttng_channel
*attr
= NULL
;
2112 LTTNG_ASSERT(session
);
2113 LTTNG_ASSERT(event
);
2114 LTTNG_ASSERT(channel_name
);
2116 /* If we have a filter, we must have its filter expression */
2117 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2119 /* Normalize event name as a globbing pattern */
2120 strutils_normalize_star_glob_pattern(event
->name
);
2122 /* Normalize exclusion names as globbing patterns */
2126 for (i
= 0; i
< exclusion
->count
; i
++) {
2127 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2129 strutils_normalize_star_glob_pattern(name
);
2133 DBG("Enable event command for event \'%s\'", event
->name
);
2137 switch (domain
->type
) {
2138 case LTTNG_DOMAIN_KERNEL
:
2140 struct ltt_kernel_channel
*kchan
;
2143 * If a non-default channel has been created in the
2144 * session, explicitely require that -c chan_name needs
2147 if (session
->kernel_session
->has_non_default_channel
2148 && channel_name
[0] == '\0') {
2149 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2153 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2154 session
->kernel_session
);
2155 if (kchan
== NULL
) {
2156 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2157 LTTNG_BUFFER_GLOBAL
);
2159 ret
= LTTNG_ERR_FATAL
;
2162 if (lttng_strncpy(attr
->name
, channel_name
,
2163 sizeof(attr
->name
))) {
2164 ret
= LTTNG_ERR_INVALID
;
2168 ret
= cmd_enable_channel_internal(
2169 session
, domain
, attr
, wpipe
);
2170 if (ret
!= LTTNG_OK
) {
2173 channel_created
= 1;
2176 /* Get the newly created kernel channel pointer */
2177 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2178 session
->kernel_session
);
2179 if (kchan
== NULL
) {
2180 /* This sould not happen... */
2181 ret
= LTTNG_ERR_FATAL
;
2185 switch (event
->type
) {
2186 case LTTNG_EVENT_ALL
:
2188 char *filter_expression_a
= NULL
;
2189 struct lttng_bytecode
*filter_a
= NULL
;
2192 * We need to duplicate filter_expression and filter,
2193 * because ownership is passed to first enable
2196 if (filter_expression
) {
2197 filter_expression_a
= strdup(filter_expression
);
2198 if (!filter_expression_a
) {
2199 ret
= LTTNG_ERR_FATAL
;
2204 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2206 free(filter_expression_a
);
2207 ret
= LTTNG_ERR_FATAL
;
2210 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2212 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2213 ret
= event_kernel_enable_event(kchan
, event
,
2214 filter_expression
, filter
);
2215 /* We have passed ownership */
2216 filter_expression
= NULL
;
2218 if (ret
!= LTTNG_OK
) {
2219 if (channel_created
) {
2220 /* Let's not leak a useless channel. */
2221 kernel_destroy_channel(kchan
);
2223 free(filter_expression_a
);
2227 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2228 ret
= event_kernel_enable_event(kchan
, event
,
2229 filter_expression_a
, filter_a
);
2230 /* We have passed ownership */
2231 filter_expression_a
= NULL
;
2233 if (ret
!= LTTNG_OK
) {
2238 case LTTNG_EVENT_PROBE
:
2239 case LTTNG_EVENT_USERSPACE_PROBE
:
2240 case LTTNG_EVENT_FUNCTION
:
2241 case LTTNG_EVENT_FUNCTION_ENTRY
:
2242 case LTTNG_EVENT_TRACEPOINT
:
2243 ret
= event_kernel_enable_event(kchan
, event
,
2244 filter_expression
, filter
);
2245 /* We have passed ownership */
2246 filter_expression
= NULL
;
2248 if (ret
!= LTTNG_OK
) {
2249 if (channel_created
) {
2250 /* Let's not leak a useless channel. */
2251 kernel_destroy_channel(kchan
);
2256 case LTTNG_EVENT_SYSCALL
:
2257 ret
= event_kernel_enable_event(kchan
, event
,
2258 filter_expression
, filter
);
2259 /* We have passed ownership */
2260 filter_expression
= NULL
;
2262 if (ret
!= LTTNG_OK
) {
2267 ret
= LTTNG_ERR_UNK
;
2271 kernel_wait_quiescent();
2274 case LTTNG_DOMAIN_UST
:
2276 struct ltt_ust_channel
*uchan
;
2277 struct ltt_ust_session
*usess
= session
->ust_session
;
2279 LTTNG_ASSERT(usess
);
2282 * If a non-default channel has been created in the
2283 * session, explicitely require that -c chan_name needs
2286 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2287 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2291 /* Get channel from global UST domain */
2292 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2294 if (uchan
== NULL
) {
2295 /* Create default channel */
2296 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2297 usess
->buffer_type
);
2299 ret
= LTTNG_ERR_FATAL
;
2302 if (lttng_strncpy(attr
->name
, channel_name
,
2303 sizeof(attr
->name
))) {
2304 ret
= LTTNG_ERR_INVALID
;
2308 ret
= cmd_enable_channel_internal(
2309 session
, domain
, attr
, wpipe
);
2310 if (ret
!= LTTNG_OK
) {
2314 /* Get the newly created channel reference back */
2315 uchan
= trace_ust_find_channel_by_name(
2316 usess
->domain_global
.channels
, channel_name
);
2317 LTTNG_ASSERT(uchan
);
2320 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2322 * Don't allow users to add UST events to channels which
2323 * are assigned to a userspace subdomain (JUL, Log4J,
2326 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2330 if (!internal_event
) {
2332 * Ensure the event name is not reserved for internal
2335 ret
= validate_ust_event_name(event
->name
);
2337 WARN("Userspace event name %s failed validation.",
2339 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2344 /* At this point, the session and channel exist on the tracer */
2345 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2346 filter_expression
, filter
, exclusion
,
2348 /* We have passed ownership */
2349 filter_expression
= NULL
;
2352 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2353 goto already_enabled
;
2354 } else if (ret
!= LTTNG_OK
) {
2359 case LTTNG_DOMAIN_LOG4J
:
2360 case LTTNG_DOMAIN_JUL
:
2361 case LTTNG_DOMAIN_PYTHON
:
2363 const char *default_event_name
, *default_chan_name
;
2365 struct lttng_event uevent
;
2366 struct lttng_domain tmp_dom
;
2367 struct ltt_ust_session
*usess
= session
->ust_session
;
2369 LTTNG_ASSERT(usess
);
2371 if (!agent_tracing_is_enabled()) {
2372 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2373 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2377 agt
= trace_ust_find_agent(usess
, domain
->type
);
2379 agt
= agent_create(domain
->type
);
2381 ret
= LTTNG_ERR_NOMEM
;
2384 agent_add(agt
, usess
->agents
);
2387 /* Create the default tracepoint. */
2388 memset(&uevent
, 0, sizeof(uevent
));
2389 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2390 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2391 default_event_name
= event_get_default_agent_ust_name(
2393 if (!default_event_name
) {
2394 ret
= LTTNG_ERR_FATAL
;
2397 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2398 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2401 * The domain type is changed because we are about to enable the
2402 * default channel and event for the JUL domain that are hardcoded.
2403 * This happens in the UST domain.
2405 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2406 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2408 switch (domain
->type
) {
2409 case LTTNG_DOMAIN_LOG4J
:
2410 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2412 case LTTNG_DOMAIN_JUL
:
2413 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2415 case LTTNG_DOMAIN_PYTHON
:
2416 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2419 /* The switch/case we are in makes this impossible */
2424 char *filter_expression_copy
= NULL
;
2425 struct lttng_bytecode
*filter_copy
= NULL
;
2428 const size_t filter_size
= sizeof(
2429 struct lttng_bytecode
)
2432 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2434 ret
= LTTNG_ERR_NOMEM
;
2437 memcpy(filter_copy
, filter
, filter_size
);
2439 filter_expression_copy
=
2440 strdup(filter_expression
);
2441 if (!filter_expression
) {
2442 ret
= LTTNG_ERR_NOMEM
;
2445 if (!filter_expression_copy
|| !filter_copy
) {
2446 free(filter_expression_copy
);
2452 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2453 (char *) default_chan_name
,
2454 &uevent
, filter_expression_copy
,
2455 filter_copy
, NULL
, wpipe
);
2458 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2459 goto already_enabled
;
2460 } else if (ret
!= LTTNG_OK
) {
2464 /* The wild card * means that everything should be enabled. */
2465 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2466 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2469 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2473 filter_expression
= NULL
;
2474 if (ret
!= LTTNG_OK
) {
2481 ret
= LTTNG_ERR_UND
;
2489 free(filter_expression
);
2492 channel_attr_destroy(attr
);
2498 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2499 * We own filter, exclusion, and filter_expression.
2501 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2502 struct lttng_event
*event
,
2503 char *filter_expression
,
2504 struct lttng_event_exclusion
*exclusion
,
2505 struct lttng_bytecode
*bytecode
,
2510 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2512 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2515 * The ownership of the following parameters is transferred to
2516 * _cmd_enable_event:
2518 * - filter_expression,
2522 ret
= _cmd_enable_event(cmd_ctx
->session
,
2524 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2525 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2526 filter_expression
= NULL
;
2533 * Enable an event which is internal to LTTng. An internal should
2534 * never be made visible to clients and are immune to checks such as
2537 static int cmd_enable_event_internal(struct ltt_session
*session
,
2538 const struct lttng_domain
*domain
,
2539 char *channel_name
, struct lttng_event
*event
,
2540 char *filter_expression
,
2541 struct lttng_bytecode
*filter
,
2542 struct lttng_event_exclusion
*exclusion
,
2545 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2546 filter_expression
, filter
, exclusion
, wpipe
, true);
2550 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2552 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2553 struct lttng_payload
*reply_payload
)
2555 enum lttng_error_code ret_code
;
2557 ssize_t i
, nb_events
= 0;
2558 struct lttng_event
*events
= NULL
;
2559 struct lttcomm_list_command_header reply_command_header
= {};
2560 size_t reply_command_header_offset
;
2562 assert(reply_payload
);
2564 /* Reserve space for command reply header. */
2565 reply_command_header_offset
= reply_payload
->buffer
.size
;
2566 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2567 reply_command_header_offset
+
2568 sizeof(struct lttcomm_list_command_header
));
2570 ret_code
= LTTNG_ERR_NOMEM
;
2575 case LTTNG_DOMAIN_KERNEL
:
2576 nb_events
= kernel_list_events(&events
);
2577 if (nb_events
< 0) {
2578 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2582 case LTTNG_DOMAIN_UST
:
2583 nb_events
= ust_app_list_events(&events
);
2584 if (nb_events
< 0) {
2585 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2589 case LTTNG_DOMAIN_LOG4J
:
2590 case LTTNG_DOMAIN_JUL
:
2591 case LTTNG_DOMAIN_PYTHON
:
2592 nb_events
= agent_list_events(&events
, domain
);
2593 if (nb_events
< 0) {
2594 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2599 ret_code
= LTTNG_ERR_UND
;
2603 for (i
= 0; i
< nb_events
; i
++) {
2604 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2607 ret_code
= LTTNG_ERR_NOMEM
;
2612 if (nb_events
> UINT32_MAX
) {
2613 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2614 ret_code
= LTTNG_ERR_OVERFLOW
;
2618 /* Update command reply header. */
2619 reply_command_header
.count
= (uint32_t) nb_events
;
2620 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2621 sizeof(reply_command_header
));
2623 ret_code
= LTTNG_OK
;
2630 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2632 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2633 struct lttng_payload
*reply
)
2635 enum lttng_error_code ret_code
;
2637 unsigned int i
, nb_fields
;
2638 struct lttng_event_field
*fields
= NULL
;
2639 struct lttcomm_list_command_header reply_command_header
= {};
2640 size_t reply_command_header_offset
;
2644 /* Reserve space for command reply header. */
2645 reply_command_header_offset
= reply
->buffer
.size
;
2646 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2647 reply_command_header_offset
+
2648 sizeof(struct lttcomm_list_command_header
));
2650 ret_code
= LTTNG_ERR_NOMEM
;
2655 case LTTNG_DOMAIN_UST
:
2656 ret
= ust_app_list_event_fields(&fields
);
2658 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2663 case LTTNG_DOMAIN_KERNEL
:
2664 default: /* fall-through */
2665 ret_code
= LTTNG_ERR_UND
;
2671 for (i
= 0; i
< nb_fields
; i
++) {
2672 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2674 ret_code
= LTTNG_ERR_NOMEM
;
2679 if (nb_fields
> UINT32_MAX
) {
2680 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2681 ret_code
= LTTNG_ERR_OVERFLOW
;
2685 /* Update command reply header. */
2686 reply_command_header
.count
= (uint32_t) nb_fields
;
2688 memcpy(reply
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2689 sizeof(reply_command_header
));
2691 ret_code
= LTTNG_OK
;
2698 enum lttng_error_code
cmd_list_syscalls(
2699 struct lttng_payload
*reply_payload
)
2701 enum lttng_error_code ret_code
;
2702 ssize_t nb_events
, i
;
2704 struct lttng_event
*events
= NULL
;
2705 struct lttcomm_list_command_header reply_command_header
= {};
2706 size_t reply_command_header_offset
;
2708 assert(reply_payload
);
2710 /* Reserve space for command reply header. */
2711 reply_command_header_offset
= reply_payload
->buffer
.size
;
2712 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2713 reply_command_header_offset
+
2714 sizeof(struct lttcomm_list_command_header
));
2716 ret_code
= LTTNG_ERR_NOMEM
;
2720 nb_events
= syscall_table_list(&events
);
2721 if (nb_events
< 0) {
2722 ret_code
= (enum lttng_error_code
) -nb_events
;
2726 for (i
= 0; i
< nb_events
; i
++) {
2727 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2730 ret_code
= LTTNG_ERR_NOMEM
;
2735 if (nb_events
> UINT32_MAX
) {
2736 ERR("Syscall count would overflow the syscall listing command's reply");
2737 ret_code
= LTTNG_ERR_OVERFLOW
;
2741 /* Update command reply header. */
2742 reply_command_header
.count
= (uint32_t) nb_events
;
2743 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2744 sizeof(reply_command_header
));
2746 ret_code
= LTTNG_OK
;
2753 * Command LTTNG_START_TRACE processed by the client thread.
2755 * Called with session mutex held.
2757 int cmd_start_trace(struct ltt_session
*session
)
2759 enum lttng_error_code ret
;
2760 unsigned long nb_chan
= 0;
2761 struct ltt_kernel_session
*ksession
;
2762 struct ltt_ust_session
*usess
;
2763 const bool session_rotated_after_last_stop
=
2764 session
->rotated_after_last_stop
;
2765 const bool session_cleared_after_last_stop
=
2766 session
->cleared_after_last_stop
;
2768 LTTNG_ASSERT(session
);
2770 /* Ease our life a bit ;) */
2771 ksession
= session
->kernel_session
;
2772 usess
= session
->ust_session
;
2774 /* Is the session already started? */
2775 if (session
->active
) {
2776 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2777 /* Perform nothing */
2781 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2782 !session
->current_trace_chunk
) {
2784 * A rotation was launched while the session was stopped and
2785 * it has not been completed yet. It is not possible to start
2786 * the session since starting the session here would require a
2787 * rotation from "NULL" to a new trace chunk. That rotation
2788 * would overlap with the ongoing rotation, which is not
2791 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2793 ret
= LTTNG_ERR_ROTATION_PENDING
;
2798 * Starting a session without channel is useless since after that it's not
2799 * possible to enable channel thus inform the client.
2801 if (usess
&& usess
->domain_global
.channels
) {
2802 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2805 nb_chan
+= ksession
->channel_count
;
2808 ret
= LTTNG_ERR_NO_CHANNEL
;
2812 session
->active
= true;
2813 session
->rotated_after_last_stop
= false;
2814 session
->cleared_after_last_stop
= false;
2815 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2816 if (!session
->has_been_started
) {
2817 struct lttng_trace_chunk
*trace_chunk
;
2819 DBG("Creating initial trace chunk of session \"%s\"",
2821 trace_chunk
= session_create_new_trace_chunk(
2822 session
, NULL
, NULL
, NULL
);
2824 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2827 LTTNG_ASSERT(!session
->current_trace_chunk
);
2828 ret
= (lttng_error_code
) session_set_trace_chunk(session
, trace_chunk
,
2830 lttng_trace_chunk_put(trace_chunk
);
2832 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2836 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2839 * Rotate existing streams into the new chunk.
2840 * This is a "quiet" rotation has no client has
2841 * explicitly requested this operation.
2843 * There is also no need to wait for the rotation
2844 * to complete as it will happen immediately. No data
2845 * was produced as the session was stopped, so the
2846 * rotation should happen on reception of the command.
2848 ret
= (lttng_error_code
) cmd_rotate_session(session
, NULL
, true,
2849 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2850 if (ret
!= LTTNG_OK
) {
2856 /* Kernel tracing */
2857 if (ksession
!= NULL
) {
2858 DBG("Start kernel tracing session %s", session
->name
);
2859 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2860 if (ret
!= LTTNG_OK
) {
2865 /* Flag session that trace should start automatically */
2867 int int_ret
= ust_app_start_trace_all(usess
);
2870 ret
= LTTNG_ERR_UST_START_FAIL
;
2876 * Open a packet in every stream of the session to ensure that viewers
2877 * can correctly identify the boundaries of the periods during which
2878 * tracing was active for this session.
2880 ret
= session_open_packets(session
);
2881 if (ret
!= LTTNG_OK
) {
2886 * Clear the flag that indicates that a rotation was done while the
2887 * session was stopped.
2889 session
->rotated_after_last_stop
= false;
2891 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2892 int int_ret
= timer_session_rotation_schedule_timer_start(
2893 session
, session
->rotate_timer_period
);
2896 ERR("Failed to enable rotate timer");
2897 ret
= LTTNG_ERR_UNK
;
2905 if (ret
== LTTNG_OK
) {
2906 /* Flag this after a successful start. */
2907 session
->has_been_started
= true;
2909 session
->active
= false;
2910 /* Restore initial state on error. */
2911 session
->rotated_after_last_stop
=
2912 session_rotated_after_last_stop
;
2913 session
->cleared_after_last_stop
=
2914 session_cleared_after_last_stop
;
2921 * Command LTTNG_STOP_TRACE processed by the client thread.
2923 int cmd_stop_trace(struct ltt_session
*session
)
2926 struct ltt_kernel_session
*ksession
;
2927 struct ltt_ust_session
*usess
;
2929 LTTNG_ASSERT(session
);
2931 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2933 ksession
= session
->kernel_session
;
2934 usess
= session
->ust_session
;
2936 /* Session is not active. Skip everything and inform the client. */
2937 if (!session
->active
) {
2938 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2942 ret
= stop_kernel_session(ksession
);
2943 if (ret
!= LTTNG_OK
) {
2947 if (usess
&& usess
->active
) {
2948 ret
= ust_app_stop_trace_all(usess
);
2950 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2955 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2957 /* Flag inactive after a successful stop. */
2958 session
->active
= 0;
2966 * Set the base_path of the session only if subdir of a control uris is set.
2967 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2969 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2971 struct lttng_uri
*uris
)
2976 for (i
= 0; i
< nb_uri
; i
++) {
2977 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2978 uris
[i
].subdir
[0] == '\0') {
2979 /* Not interested in these URIs */
2983 if (session
->base_path
!= NULL
) {
2984 free(session
->base_path
);
2985 session
->base_path
= NULL
;
2988 /* Set session base_path */
2989 session
->base_path
= strdup(uris
[i
].subdir
);
2990 if (!session
->base_path
) {
2991 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2992 uris
[i
].subdir
, session
->name
);
2993 ret
= LTTNG_ERR_NOMEM
;
2996 DBG2("Setting base path \"%s\" for session \"%s\"",
2997 session
->base_path
, session
->name
);
3005 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
3007 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
3008 struct lttng_uri
*uris
)
3011 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3012 struct ltt_ust_session
*usess
= session
->ust_session
;
3014 LTTNG_ASSERT(session
);
3016 LTTNG_ASSERT(nb_uri
> 0);
3018 /* Can't set consumer URI if the session is active. */
3019 if (session
->active
) {
3020 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
3025 * Set the session base path if any. This is done inside
3026 * cmd_set_consumer_uri to preserve backward compatibility of the
3027 * previous session creation api vs the session descriptor api.
3029 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
3030 if (ret
!= LTTNG_OK
) {
3034 /* Set the "global" consumer URIs */
3035 for (i
= 0; i
< nb_uri
; i
++) {
3036 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
3038 if (ret
!= LTTNG_OK
) {
3043 /* Set UST session URIs */
3044 if (session
->ust_session
) {
3045 for (i
= 0; i
< nb_uri
; i
++) {
3046 ret
= add_uri_to_consumer(session
,
3047 session
->ust_session
->consumer
,
3048 &uris
[i
], LTTNG_DOMAIN_UST
);
3049 if (ret
!= LTTNG_OK
) {
3055 /* Set kernel session URIs */
3056 if (session
->kernel_session
) {
3057 for (i
= 0; i
< nb_uri
; i
++) {
3058 ret
= add_uri_to_consumer(session
,
3059 session
->kernel_session
->consumer
,
3060 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3061 if (ret
!= LTTNG_OK
) {
3068 * Make sure to set the session in output mode after we set URI since a
3069 * session can be created without URL (thus flagged in no output mode).
3071 session
->output_traces
= 1;
3073 ksess
->output_traces
= 1;
3077 usess
->output_traces
= 1;
3088 enum lttng_error_code
set_session_output_from_descriptor(
3089 struct ltt_session
*session
,
3090 const struct lttng_session_descriptor
*descriptor
)
3093 enum lttng_error_code ret_code
= LTTNG_OK
;
3094 enum lttng_session_descriptor_type session_type
=
3095 lttng_session_descriptor_get_type(descriptor
);
3096 enum lttng_session_descriptor_output_type output_type
=
3097 lttng_session_descriptor_get_output_type(descriptor
);
3098 struct lttng_uri uris
[2] = {};
3099 size_t uri_count
= 0;
3101 switch (output_type
) {
3102 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3104 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3105 lttng_session_descriptor_get_local_output_uri(descriptor
,
3109 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3110 lttng_session_descriptor_get_network_output_uris(descriptor
,
3111 &uris
[0], &uris
[1]);
3115 ret_code
= LTTNG_ERR_INVALID
;
3119 switch (session_type
) {
3120 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3122 struct snapshot_output
*new_output
= NULL
;
3124 new_output
= snapshot_output_alloc();
3126 ret_code
= LTTNG_ERR_NOMEM
;
3130 ret
= snapshot_output_init_with_uri(session
,
3131 DEFAULT_SNAPSHOT_MAX_SIZE
,
3132 NULL
, uris
, uri_count
, session
->consumer
,
3133 new_output
, &session
->snapshot
);
3135 ret_code
= (ret
== -ENOMEM
) ?
3136 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3137 snapshot_output_destroy(new_output
);
3140 snapshot_add_output(&session
->snapshot
, new_output
);
3143 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3144 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3146 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3150 ret_code
= LTTNG_ERR_INVALID
;
3158 enum lttng_error_code
cmd_create_session_from_descriptor(
3159 struct lttng_session_descriptor
*descriptor
,
3160 const lttng_sock_cred
*creds
,
3161 const char *home_path
)
3164 enum lttng_error_code ret_code
;
3165 const char *session_name
;
3166 struct ltt_session
*new_session
= NULL
;
3167 enum lttng_session_descriptor_status descriptor_status
;
3168 const lttng_trace_format_descriptor
*trace_format_descriptor
= NULL
;
3169 lttng::trace_format_descriptor::uptr trace_format_descriptor_ptr
;
3171 session_lock_list();
3173 if (*home_path
!= '/') {
3174 ERR("Home path provided by client is not absolute");
3175 ret_code
= LTTNG_ERR_INVALID
;
3180 descriptor_status
= lttng_session_descriptor_get_session_name(
3181 descriptor
, &session_name
);
3182 switch (descriptor_status
) {
3183 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3185 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3186 session_name
= NULL
;
3189 ret_code
= LTTNG_ERR_INVALID
;
3193 descriptor_status
= lttng_session_descriptor_get_trace_format_descriptor(
3194 descriptor
, &trace_format_descriptor
);
3195 if (descriptor_status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
3196 ret_code
= LTTNG_ERR_INVALID
;
3201 trace_format_descriptor_ptr
=
3202 reinterpret_cast<const lttng::trace_format_descriptor
*>(
3203 trace_format_descriptor
)
3205 } catch (std::exception
& e
) {
3206 ERR("%s", e
.what());
3207 ret_code
= LTTNG_ERR_UNK
;
3211 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, trace_format_descriptor_ptr
,
3213 if (ret_code
!= LTTNG_OK
) {
3217 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3218 new_session
->id
, new_session
->name
, new_session
->uid
, new_session
->gid
);
3219 if (ret_code
!= LTTNG_OK
) {
3223 /* Announce the session's destruction to the notification thread when it is destroyed. */
3224 ret
= session_add_destroy_notifier(
3226 [](const struct ltt_session
*session
,
3227 void *user_data
__attribute__((unused
))) {
3228 (void) notification_thread_command_remove_session(
3229 the_notification_thread_handle
, session
->id
);
3233 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3235 ret
= LTTNG_ERR_NOMEM
;
3239 if (!session_name
) {
3240 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3243 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3248 if (!lttng_session_descriptor_is_output_destination_initialized(
3251 * Only include the session's creation time in the output
3252 * destination if the name of the session itself was
3253 * not auto-generated.
3255 ret_code
= lttng_session_descriptor_set_default_output(
3257 session_name
? &new_session
->creation_time
: NULL
,
3259 if (ret_code
!= LTTNG_OK
) {
3263 new_session
->has_user_specified_directory
=
3264 lttng_session_descriptor_has_output_directory(
3268 switch (lttng_session_descriptor_get_type(descriptor
)) {
3269 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3270 new_session
->snapshot_mode
= 1;
3272 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3273 new_session
->live_timer
=
3274 lttng_session_descriptor_live_get_timer_interval(
3281 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3282 if (ret_code
!= LTTNG_OK
) {
3285 new_session
->consumer
->enabled
= 1;
3286 ret_code
= LTTNG_OK
;
3288 /* Release reference provided by the session_create function. */
3289 session_put(new_session
);
3290 if (ret_code
!= LTTNG_OK
&& new_session
) {
3291 /* Release the global reference on error. */
3292 session_destroy(new_session
);
3294 session_unlock_list();
3298 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3299 struct lttng_session_descriptor
**return_descriptor
)
3302 size_t payload_size
;
3303 struct lttng_dynamic_buffer payload
;
3304 struct lttng_buffer_view home_dir_view
;
3305 struct lttng_buffer_view session_descriptor_view
;
3306 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3307 enum lttng_error_code ret_code
;
3309 lttng_dynamic_buffer_init(&payload
);
3310 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3312 ret_code
= LTTNG_ERR_INVALID
;
3315 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3316 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3317 ret_code
= LTTNG_ERR_INVALID
;
3321 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3322 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3323 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3325 ret_code
= LTTNG_ERR_NOMEM
;
3329 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3331 ERR("Reception of session descriptor failed, aborting.");
3332 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3336 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3339 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3340 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3341 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3342 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3343 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3347 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3349 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3350 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3351 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3352 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3353 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3357 ret
= lttng_session_descriptor_create_from_buffer(
3358 &session_descriptor_view
, &session_descriptor
);
3360 ERR("Failed to create session descriptor from payload of \"create session\" command");
3361 ret_code
= LTTNG_ERR_INVALID
;
3366 * Sets the descriptor's auto-generated properties (name, output) if
3369 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3371 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3372 if (ret_code
!= LTTNG_OK
) {
3376 ret_code
= LTTNG_OK
;
3377 *return_descriptor
= session_descriptor
;
3378 session_descriptor
= NULL
;
3380 lttng_dynamic_buffer_reset(&payload
);
3381 lttng_session_descriptor_destroy(session_descriptor
);
3386 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3387 void *_reply_context
)
3391 const struct cmd_destroy_session_reply_context
*reply_context
=
3392 (cmd_destroy_session_reply_context
*) _reply_context
;
3393 struct lttng_dynamic_buffer payload
;
3394 struct lttcomm_session_destroy_command_header cmd_header
;
3395 struct lttng_trace_archive_location
*location
= NULL
;
3396 struct lttcomm_lttng_msg llm
= {
3397 .cmd_type
= LTTNG_DESTROY_SESSION
,
3398 .ret_code
= reply_context
->destruction_status
,
3401 sizeof(struct lttcomm_session_destroy_command_header
),
3405 size_t payload_size_before_location
;
3407 lttng_dynamic_buffer_init(&payload
);
3409 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3411 ERR("Failed to append session destruction message");
3415 cmd_header
.rotation_state
=
3416 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3417 session
->rotation_state
:
3418 LTTNG_ROTATION_STATE_NO_ROTATION
);
3419 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3420 sizeof(cmd_header
));
3422 ERR("Failed to append session destruction command header");
3426 if (!reply_context
->implicit_rotation_on_destroy
) {
3427 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3431 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3432 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3437 location
= session_get_trace_archive_location(session
);
3439 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3444 payload_size_before_location
= payload
.size
;
3445 comm_ret
= lttng_trace_archive_location_serialize(location
,
3447 lttng_trace_archive_location_put(location
);
3449 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3453 /* Update the message to indicate the location's length. */
3454 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3455 payload
.size
- payload_size_before_location
;
3457 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3458 payload
.data
, payload
.size
);
3459 if (comm_ret
!= (ssize_t
) payload
.size
) {
3460 ERR("Failed to send result of the destruction of session \"%s\" to client",
3464 ret
= close(reply_context
->reply_sock_fd
);
3466 PERROR("Failed to close client socket in deferred session destroy reply");
3468 lttng_dynamic_buffer_reset(&payload
);
3469 free(_reply_context
);
3473 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3475 * Called with session lock held.
3477 int cmd_destroy_session(struct ltt_session
*session
,
3478 struct notification_thread_handle
*notification_thread_handle
,
3482 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3483 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3486 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3487 if (!reply_context
) {
3488 ret
= LTTNG_ERR_NOMEM
;
3492 reply_context
->reply_sock_fd
= *sock_fd
;
3496 LTTNG_ASSERT(session
);
3498 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3500 if (session
->active
) {
3501 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3503 ret
= cmd_stop_trace(session
);
3504 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3505 /* Carry on with the destruction of the session. */
3506 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3507 session
->name
, lttng_strerror(-ret
));
3508 destruction_last_error
= (lttng_error_code
) ret
;
3512 if (session
->rotation_schedule_timer_enabled
) {
3513 if (timer_session_rotation_schedule_timer_stop(
3515 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3517 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3521 if (session
->rotate_size
) {
3522 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3523 session
->rotate_size
= 0;
3526 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3528 * Perform a last rotation on destruction if rotations have
3529 * occurred during the session's lifetime.
3531 ret
= cmd_rotate_session(session
, NULL
, false,
3532 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3533 if (ret
!= LTTNG_OK
) {
3534 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3535 session
->name
, lttng_strerror(-ret
));
3536 destruction_last_error
= (lttng_error_code
) -ret
;
3538 if (reply_context
) {
3539 reply_context
->implicit_rotation_on_destroy
= true;
3541 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3543 * The user has not triggered a session rotation. However, to
3544 * ensure all data has been consumed, the session is rotated
3545 * to a 'null' trace chunk before it is destroyed.
3547 * This is a "quiet" rotation meaning that no notification is
3548 * emitted and no renaming of the current trace chunk takes
3551 ret
= cmd_rotate_session(session
, NULL
, true,
3552 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3554 * Rotation operations may not be supported by the kernel
3555 * tracer. Hence, do not consider this implicit rotation as
3556 * a session destruction error. The library has already stopped
3557 * the session and waited for pending data; there is nothing
3558 * left to do but complete the destruction of the session.
3560 if (ret
!= LTTNG_OK
&&
3561 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3562 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3563 session
->name
, lttng_strerror(ret
));
3564 destruction_last_error
= (lttng_error_code
) -ret
;
3568 if (session
->shm_path
[0]) {
3570 * When a session is created with an explicit shm_path,
3571 * the consumer daemon will create its shared memory files
3572 * at that location and will *not* unlink them. This is normal
3573 * as the intention of that feature is to make it possible
3574 * to retrieve the content of those files should a crash occur.
3576 * To ensure the content of those files can be used, the
3577 * sessiond daemon will replicate the content of the metadata
3578 * cache in a metadata file.
3580 * On clean-up, it is expected that the consumer daemon will
3581 * unlink the shared memory files and that the session daemon
3582 * will unlink the metadata file. Then, the session's directory
3583 * in the shm path can be removed.
3585 * Unfortunately, a flaw in the design of the sessiond's and
3586 * consumerd's tear down of channels makes it impossible to
3587 * determine when the sessiond _and_ the consumerd have both
3588 * destroyed their representation of a channel. For one, the
3589 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3590 * callbacks in both daemons.
3592 * However, it is also impossible for the sessiond to know when
3593 * the consumer daemon is done destroying its channel(s) since
3594 * it occurs as a reaction to the closing of the channel's file
3595 * descriptor. There is no resulting communication initiated
3596 * from the consumerd to the sessiond to confirm that the
3597 * operation is completed (and was successful).
3599 * Until this is all fixed, the session daemon checks for the
3600 * removal of the session's shm path which makes it possible
3601 * to safely advertise a session as having been destroyed.
3603 * Prior to this fix, it was not possible to reliably save
3604 * a session making use of the --shm-path option, destroy it,
3605 * and load it again. This is because the creation of the
3606 * session would fail upon seeing the session's shm path
3607 * already in existence.
3609 * Note that none of the error paths in the check for the
3610 * directory's existence return an error. This is normal
3611 * as there isn't much that can be done. The session will
3612 * be destroyed properly, except that we can't offer the
3613 * guarantee that the same session can be re-created.
3615 current_completion_handler
= &destroy_completion_handler
.handler
;
3616 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3618 sizeof(destroy_completion_handler
.shm_path
));
3623 * The session is destroyed. However, note that the command context
3624 * still holds a reference to the session, thus delaying its destruction
3625 * _at least_ up to the point when that reference is released.
3627 session_destroy(session
);
3628 if (reply_context
) {
3629 reply_context
->destruction_status
= destruction_last_error
;
3630 ret
= session_add_destroy_notifier(session
,
3631 cmd_destroy_session_reply
,
3632 (void *) reply_context
);
3634 ret
= LTTNG_ERR_FATAL
;
3646 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3648 int cmd_register_consumer(struct ltt_session
*session
,
3649 enum lttng_domain_type domain
, const char *sock_path
,
3650 struct consumer_data
*cdata
)
3653 struct consumer_socket
*socket
= NULL
;
3655 LTTNG_ASSERT(session
);
3656 LTTNG_ASSERT(cdata
);
3657 LTTNG_ASSERT(sock_path
);
3660 case LTTNG_DOMAIN_KERNEL
:
3662 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3664 LTTNG_ASSERT(ksess
);
3666 /* Can't register a consumer if there is already one */
3667 if (ksess
->consumer_fds_sent
!= 0) {
3668 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3672 sock
= lttcomm_connect_unix_sock(sock_path
);
3674 ret
= LTTNG_ERR_CONNECT_FAIL
;
3677 cdata
->cmd_sock
= sock
;
3679 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3680 if (socket
== NULL
) {
3683 PERROR("close register consumer");
3685 cdata
->cmd_sock
= -1;
3686 ret
= LTTNG_ERR_FATAL
;
3690 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3691 if (socket
->lock
== NULL
) {
3692 PERROR("zmalloc pthread mutex");
3693 ret
= LTTNG_ERR_FATAL
;
3697 pthread_mutex_init(socket
->lock
, NULL
);
3698 socket
->registered
= 1;
3701 consumer_add_socket(socket
, ksess
->consumer
);
3704 pthread_mutex_lock(&cdata
->pid_mutex
);
3706 pthread_mutex_unlock(&cdata
->pid_mutex
);
3711 /* TODO: Userspace tracing */
3712 ret
= LTTNG_ERR_UND
;
3720 consumer_destroy_socket(socket
);
3726 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3728 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3729 struct lttng_domain
**domains
)
3734 struct lttng_ht_iter iter
;
3736 if (session
->kernel_session
!= NULL
) {
3737 DBG3("Listing domains found kernel domain");
3741 if (session
->ust_session
!= NULL
) {
3742 DBG3("Listing domains found UST global domain");
3746 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3748 if (agt
->being_used
) {
3759 *domains
= calloc
<lttng_domain
>(nb_dom
);
3760 if (*domains
== NULL
) {
3761 ret
= LTTNG_ERR_FATAL
;
3765 if (session
->kernel_session
!= NULL
) {
3766 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3768 /* Kernel session buffer type is always GLOBAL */
3769 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3774 if (session
->ust_session
!= NULL
) {
3775 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3776 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3780 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3782 if (agt
->being_used
) {
3783 (*domains
)[index
].type
= agt
->domain
;
3784 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3794 /* Return negative value to differentiate return code */
3800 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3802 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3803 struct ltt_session
*session
,
3804 struct lttng_payload
*payload
)
3808 struct lttcomm_list_command_header cmd_header
= {};
3809 size_t cmd_header_offset
;
3810 enum lttng_error_code ret_code
;
3815 DBG("Listing channels for session %s", session
->name
);
3817 cmd_header_offset
= payload
->buffer
.size
;
3819 /* Reserve space for command reply header. */
3820 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3821 cmd_header_offset
+ sizeof(cmd_header
));
3823 ret_code
= LTTNG_ERR_NOMEM
;
3828 case LTTNG_DOMAIN_KERNEL
:
3830 /* Kernel channels */
3831 struct ltt_kernel_channel
*kchan
;
3832 if (session
->kernel_session
!= NULL
) {
3833 cds_list_for_each_entry(kchan
,
3834 &session
->kernel_session
->channel_list
.head
, list
) {
3835 uint64_t discarded_events
, lost_packets
;
3836 struct lttng_channel_extended
*extended
;
3838 extended
= (struct lttng_channel_extended
*)
3839 kchan
->channel
->attr
.extended
.ptr
;
3841 ret
= get_kernel_runtime_stats(session
, kchan
,
3842 &discarded_events
, &lost_packets
);
3844 ret_code
= LTTNG_ERR_UNK
;
3849 * Update the discarded_events and lost_packets
3850 * count for the channel
3852 extended
->discarded_events
= discarded_events
;
3853 extended
->lost_packets
= lost_packets
;
3855 ret
= lttng_channel_serialize(
3856 kchan
->channel
, &payload
->buffer
);
3858 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3859 kchan
->channel
->name
);
3860 ret_code
= LTTNG_ERR_UNK
;
3869 case LTTNG_DOMAIN_UST
:
3871 struct lttng_ht_iter iter
;
3872 struct ltt_ust_channel
*uchan
;
3875 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3876 &iter
.iter
, uchan
, node
.node
) {
3877 uint64_t discarded_events
= 0, lost_packets
= 0;
3878 struct lttng_channel
*channel
= NULL
;
3879 struct lttng_channel_extended
*extended
;
3881 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3883 ret_code
= LTTNG_ERR_NOMEM
;
3887 extended
= (struct lttng_channel_extended
*)
3888 channel
->attr
.extended
.ptr
;
3890 ret
= get_ust_runtime_stats(session
, uchan
,
3891 &discarded_events
, &lost_packets
);
3893 lttng_channel_destroy(channel
);
3894 ret_code
= LTTNG_ERR_UNK
;
3898 extended
->discarded_events
= discarded_events
;
3899 extended
->lost_packets
= lost_packets
;
3901 ret
= lttng_channel_serialize(
3902 channel
, &payload
->buffer
);
3904 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3906 lttng_channel_destroy(channel
);
3907 ret_code
= LTTNG_ERR_UNK
;
3911 lttng_channel_destroy(channel
);
3921 if (i
> UINT32_MAX
) {
3922 ERR("Channel count would overflow the channel listing command's reply");
3923 ret_code
= LTTNG_ERR_OVERFLOW
;
3927 /* Update command reply header. */
3928 cmd_header
.count
= (uint32_t) i
;
3929 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3930 sizeof(cmd_header
));
3931 ret_code
= LTTNG_OK
;
3938 * Command LTTNG_LIST_EVENTS processed by the client thread.
3940 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3941 struct ltt_session
*session
,
3943 struct lttng_payload
*reply_payload
)
3945 int buffer_resize_ret
;
3946 enum lttng_error_code ret_code
= LTTNG_OK
;
3947 struct lttcomm_list_command_header reply_command_header
= {};
3948 size_t reply_command_header_offset
;
3949 unsigned int nb_events
= 0;
3951 assert(reply_payload
);
3953 /* Reserve space for command reply header. */
3954 reply_command_header_offset
= reply_payload
->buffer
.size
;
3955 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3956 reply_command_header_offset
+
3957 sizeof(struct lttcomm_list_command_header
));
3958 if (buffer_resize_ret
) {
3959 ret_code
= LTTNG_ERR_NOMEM
;
3964 case LTTNG_DOMAIN_KERNEL
:
3965 if (session
->kernel_session
!= NULL
) {
3966 ret_code
= list_lttng_kernel_events(channel_name
,
3967 session
->kernel_session
, reply_payload
, &nb_events
);
3971 case LTTNG_DOMAIN_UST
:
3973 if (session
->ust_session
!= NULL
) {
3974 ret_code
= list_lttng_ust_global_events(channel_name
,
3975 &session
->ust_session
->domain_global
,
3976 reply_payload
, &nb_events
);
3981 case LTTNG_DOMAIN_LOG4J
:
3982 case LTTNG_DOMAIN_JUL
:
3983 case LTTNG_DOMAIN_PYTHON
:
3984 if (session
->ust_session
) {
3985 struct lttng_ht_iter iter
;
3989 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3990 &iter
.iter
, agt
, node
.node
) {
3991 if (agt
->domain
== domain
) {
3992 ret_code
= list_lttng_agent_events(
3993 agt
, reply_payload
, &nb_events
);
4002 ret_code
= LTTNG_ERR_UND
;
4006 if (nb_events
> UINT32_MAX
) {
4007 ret_code
= LTTNG_ERR_OVERFLOW
;
4011 /* Update command reply header. */
4012 reply_command_header
.count
= (uint32_t) nb_events
;
4013 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
4014 sizeof(reply_command_header
));
4021 * Using the session list, filled a lttng_session array to send back to the
4022 * client for session listing.
4024 * The session list lock MUST be acquired before calling this function. Use
4025 * session_lock_list() and session_unlock_list().
4027 enum lttng_error_code
cmd_list_lttng_sessions(
4028 struct lttng_payload
*reply_payload
, uid_t uid
, gid_t gid
)
4030 int buffer_resize_ret
;
4031 enum lttng_error_code ret_code
= LTTNG_OK
;
4032 struct lttcomm_list_command_header reply_command_header
= {};
4033 size_t reply_command_header_offset
;
4034 struct ltt_session
*session
;
4035 struct ltt_session_list
*list
= session_get_list();
4039 assert(reply_payload
);
4041 /* Reserve space for command reply header. */
4042 reply_command_header_offset
= reply_payload
->buffer
.size
;
4043 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
4044 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
4045 if (buffer_resize_ret
) {
4046 ret_code
= LTTNG_ERR_NOMEM
;
4050 DBG("Getting all available session for UID %d GID %d",
4053 cds_list_for_each_entry(session
, &list
->head
, list
) {
4054 struct lttng_session tmp_session
= {};
4055 struct lttng_session_extended tmp_extended
= {};
4057 tmp_session
.extended
.ptr
= &tmp_extended
;
4059 if (!session_get(session
)) {
4063 * Only list the sessions the user can control.
4065 if (!session_access_ok(session
, uid
) ||
4066 session
->destroyed
) {
4067 session_put(session
);
4071 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4072 struct ltt_ust_session
*usess
= session
->ust_session
;
4074 if (session
->consumer
->type
== CONSUMER_DST_NET
||
4075 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
4076 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
4077 ret
= build_network_session_path(
4078 tmp_session
.path
, sizeof(tmp_session
.path
), session
);
4080 ret
= snprintf(tmp_session
.path
, sizeof(tmp_session
.path
), "%s",
4081 session
->consumer
->dst
.session_root_path
);
4084 PERROR("snprintf session path");
4085 session_put(session
);
4089 strncpy(tmp_session
.name
, session
->name
, NAME_MAX
);
4090 tmp_session
.name
[NAME_MAX
- 1] = '\0';
4091 tmp_session
.enabled
= session
->active
;
4092 tmp_session
.snapshot_mode
= session
->snapshot_mode
;
4093 tmp_session
.live_timer_interval
= session
->live_timer
;
4094 LTTNG_OPTIONAL_SET(&tmp_extended
.creation_time
, (uint64_t) session
->creation_time
);
4095 ret
= lttng_session_serialize(&tmp_session
, reply_payload
);
4097 ret_code
= LTTNG_ERR_FATAL
;
4101 session_put(session
);
4104 if (i
> UINT32_MAX
) {
4105 ret_code
= LTTNG_ERR_OVERFLOW
;
4109 /* Update command reply header. */
4110 reply_command_header
.count
= (uint32_t) i
;
4111 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
4112 sizeof(reply_command_header
));
4113 ret_code
= LTTNG_OK
;
4119 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4120 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4122 int cmd_data_pending(struct ltt_session
*session
)
4125 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4126 struct ltt_ust_session
*usess
= session
->ust_session
;
4128 LTTNG_ASSERT(session
);
4130 DBG("Data pending for session %s", session
->name
);
4132 /* Session MUST be stopped to ask for data availability. */
4133 if (session
->active
) {
4134 ret
= LTTNG_ERR_SESSION_STARTED
;
4138 * If stopped, just make sure we've started before else the above call
4139 * will always send that there is data pending.
4141 * The consumer assumes that when the data pending command is received,
4142 * the trace has been started before or else no output data is written
4143 * by the streams which is a condition for data pending. So, this is
4144 * *VERY* important that we don't ask the consumer before a start
4147 if (!session
->has_been_started
) {
4153 /* A rotation is still pending, we have to wait. */
4154 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4155 DBG("Rotate still pending for session %s", session
->name
);
4160 if (ksess
&& ksess
->consumer
) {
4161 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4163 /* Data is still being extracted for the kernel. */
4168 if (usess
&& usess
->consumer
) {
4169 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4171 /* Data is still being extracted for the kernel. */
4176 /* Data is ready to be read by a viewer */
4184 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4186 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4188 int cmd_snapshot_add_output(struct ltt_session
*session
,
4189 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4192 struct snapshot_output
*new_output
;
4194 LTTNG_ASSERT(session
);
4195 LTTNG_ASSERT(output
);
4197 DBG("Cmd snapshot add output for session %s", session
->name
);
4200 * Can't create an output if the session is not set in no-output mode.
4202 if (session
->output_traces
) {
4203 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4207 if (session
->has_non_mmap_channel
) {
4208 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4212 /* Only one output is allowed until we have the "tee" feature. */
4213 if (session
->snapshot
.nb_output
== 1) {
4214 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4218 new_output
= snapshot_output_alloc();
4220 ret
= LTTNG_ERR_NOMEM
;
4224 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4225 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4226 &session
->snapshot
);
4228 if (ret
== -ENOMEM
) {
4229 ret
= LTTNG_ERR_NOMEM
;
4231 ret
= LTTNG_ERR_INVALID
;
4237 snapshot_add_output(&session
->snapshot
, new_output
);
4239 *id
= new_output
->id
;
4246 snapshot_output_destroy(new_output
);
4252 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4254 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4256 int cmd_snapshot_del_output(struct ltt_session
*session
,
4257 const struct lttng_snapshot_output
*output
)
4260 struct snapshot_output
*sout
= NULL
;
4262 LTTNG_ASSERT(session
);
4263 LTTNG_ASSERT(output
);
4268 * Permission denied to create an output if the session is not
4269 * set in no output mode.
4271 if (session
->output_traces
) {
4272 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4277 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4279 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4280 } else if (*output
->name
!= '\0') {
4281 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4283 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4286 ret
= LTTNG_ERR_INVALID
;
4290 snapshot_delete_output(&session
->snapshot
, sout
);
4291 snapshot_output_destroy(sout
);
4300 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4302 * If no output is available, outputs is untouched and 0 is returned.
4304 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4306 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4307 struct lttng_snapshot_output
**outputs
)
4310 struct lttng_snapshot_output
*list
= NULL
;
4311 struct lttng_ht_iter iter
;
4312 struct snapshot_output
*output
;
4314 LTTNG_ASSERT(session
);
4315 LTTNG_ASSERT(outputs
);
4317 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4320 * Permission denied to create an output if the session is not
4321 * set in no output mode.
4323 if (session
->output_traces
) {
4324 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4328 if (session
->snapshot
.nb_output
== 0) {
4333 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4335 ret
= -LTTNG_ERR_NOMEM
;
4339 /* Copy list from session to the new list object. */
4341 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4342 output
, node
.node
) {
4343 LTTNG_ASSERT(output
->consumer
);
4344 list
[idx
].id
= output
->id
;
4345 list
[idx
].max_size
= output
->max_size
;
4346 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4347 sizeof(list
[idx
].name
))) {
4348 ret
= -LTTNG_ERR_INVALID
;
4351 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4352 if (lttng_strncpy(list
[idx
].ctrl_url
,
4353 output
->consumer
->dst
.session_root_path
,
4354 sizeof(list
[idx
].ctrl_url
))) {
4355 ret
= -LTTNG_ERR_INVALID
;
4360 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4361 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4363 ret
= -LTTNG_ERR_NOMEM
;
4368 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4369 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4371 ret
= -LTTNG_ERR_NOMEM
;
4380 ret
= session
->snapshot
.nb_output
;
4389 * Check if we can regenerate the metadata for this session.
4390 * Only kernel, UST per-uid and non-live sessions are supported.
4392 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4395 int check_regenerate_metadata_support(struct ltt_session
*session
)
4399 LTTNG_ASSERT(session
);
4401 if (session
->live_timer
!= 0) {
4402 ret
= LTTNG_ERR_LIVE_SESSION
;
4405 if (!session
->active
) {
4406 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4409 if (session
->ust_session
) {
4410 switch (session
->ust_session
->buffer_type
) {
4411 case LTTNG_BUFFER_PER_UID
:
4413 case LTTNG_BUFFER_PER_PID
:
4414 ret
= LTTNG_ERR_PER_PID_SESSION
;
4418 ret
= LTTNG_ERR_UNK
;
4422 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4423 session
->consumer
->relay_minor_version
< 8) {
4424 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4434 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4436 * Ask the consumer to truncate the existing metadata file(s) and
4437 * then regenerate the metadata. Live and per-pid sessions are not
4438 * supported and return an error.
4440 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4442 int cmd_regenerate_metadata(struct ltt_session
*session
)
4446 LTTNG_ASSERT(session
);
4448 ret
= check_regenerate_metadata_support(session
);
4453 if (session
->kernel_session
) {
4454 ret
= kernctl_session_regenerate_metadata(
4455 session
->kernel_session
->fd
);
4457 ERR("Failed to regenerate the kernel metadata");
4462 if (session
->ust_session
) {
4463 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4465 ERR("Failed to regenerate the UST metadata");
4469 DBG("Cmd metadata regenerate for session %s", session
->name
);
4477 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4479 * Ask the tracer to regenerate a new statedump.
4481 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4483 int cmd_regenerate_statedump(struct ltt_session
*session
)
4487 LTTNG_ASSERT(session
);
4489 if (!session
->active
) {
4490 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4494 if (session
->kernel_session
) {
4495 ret
= kernctl_session_regenerate_statedump(
4496 session
->kernel_session
->fd
);
4498 * Currently, the statedump in kernel can only fail if out
4502 if (ret
== -ENOMEM
) {
4503 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4505 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4507 ERR("Failed to regenerate the kernel statedump");
4512 if (session
->ust_session
) {
4513 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4515 * Currently, the statedump in UST always returns 0.
4518 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4519 ERR("Failed to regenerate the UST statedump");
4523 DBG("Cmd regenerate statedump for session %s", session
->name
);
4531 enum lttng_error_code
synchronize_tracer_notifier_register(
4532 struct notification_thread_handle
*notification_thread
,
4533 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4535 enum lttng_error_code ret_code
;
4536 const struct lttng_condition
*condition
=
4537 lttng_trigger_get_const_condition(trigger
);
4538 const char *trigger_name
;
4539 uid_t trigger_owner
;
4540 enum lttng_trigger_status trigger_status
;
4541 const enum lttng_domain_type trigger_domain
=
4542 lttng_trigger_get_underlying_domain_type_restriction(
4545 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4546 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4548 LTTNG_ASSERT(condition
);
4549 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4550 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4552 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4553 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4554 trigger_name
: "(anonymous)";
4556 session_lock_list();
4557 switch (trigger_domain
) {
4558 case LTTNG_DOMAIN_KERNEL
:
4560 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4561 if (ret_code
!= LTTNG_OK
) {
4562 enum lttng_error_code notif_thread_unregister_ret
;
4564 notif_thread_unregister_ret
=
4565 notification_thread_command_unregister_trigger(
4566 notification_thread
, trigger
);
4568 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4569 /* Return the original error code. */
4570 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4572 (int) trigger_owner
,
4578 case LTTNG_DOMAIN_UST
:
4579 ust_app_global_update_all_event_notifier_rules();
4581 case LTTNG_DOMAIN_JUL
:
4582 case LTTNG_DOMAIN_LOG4J
:
4583 case LTTNG_DOMAIN_PYTHON
:
4585 /* Agent domains. */
4586 struct agent
*agt
= agent_find_by_event_notifier_domain(
4590 agt
= agent_create(trigger_domain
);
4592 ret_code
= LTTNG_ERR_NOMEM
;
4593 goto end_unlock_session_list
;
4596 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4599 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4600 if (ret_code
!= LTTNG_OK
) {
4601 goto end_unlock_session_list
;
4606 case LTTNG_DOMAIN_NONE
:
4611 ret_code
= LTTNG_OK
;
4612 end_unlock_session_list
:
4613 session_unlock_list();
4617 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4618 struct lttng_trigger
*trigger
,
4619 bool is_trigger_anonymous
,
4620 struct notification_thread_handle
*notification_thread
,
4621 struct lttng_trigger
**return_trigger
)
4623 enum lttng_error_code ret_code
;
4624 const char *trigger_name
;
4625 uid_t trigger_owner
;
4626 enum lttng_trigger_status trigger_status
;
4628 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4629 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4630 trigger_name
: "(anonymous)";
4632 trigger_status
= lttng_trigger_get_owner_uid(
4633 trigger
, &trigger_owner
);
4634 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4636 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4637 trigger_name
, (int) trigger_owner
,
4638 (int) lttng_credentials_get_uid(cmd_creds
));
4641 * Validate the trigger credentials against the command credentials.
4642 * Only the root user can register a trigger with non-matching
4645 if (!lttng_credentials_is_equal_uid(
4646 lttng_trigger_get_credentials(trigger
),
4648 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4649 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4650 trigger_name
, (int) trigger_owner
,
4651 (int) lttng_credentials_get_uid(cmd_creds
));
4652 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4658 * The bytecode generation also serves as a validation step for the
4659 * bytecode expressions.
4661 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4662 if (ret_code
!= LTTNG_OK
) {
4663 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4664 trigger_name
, (int) trigger_owner
, ret_code
);
4669 * A reference to the trigger is acquired by the notification thread.
4670 * It is safe to return the same trigger to the caller since it the
4671 * other user holds a reference.
4673 * The trigger is modified during the execution of the
4674 * "register trigger" command. However, by the time the command returns,
4675 * it is safe to use without any locking as its properties are
4678 ret_code
= notification_thread_command_register_trigger(
4679 notification_thread
, trigger
, is_trigger_anonymous
);
4680 if (ret_code
!= LTTNG_OK
) {
4681 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4682 trigger_name
, (int) trigger_owner
, ret_code
);
4686 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4687 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4688 trigger_name
: "(anonymous)";
4691 * Synchronize tracers if the trigger adds an event notifier.
4693 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4694 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4695 trigger
, cmd_creds
);
4696 if (ret_code
!= LTTNG_OK
) {
4697 ERR("Error registering tracer notifier: %s",
4698 lttng_strerror(-ret_code
));
4704 * Return an updated trigger to the client.
4706 * Since a modified version of the same trigger is returned, acquire a
4707 * reference to the trigger so the caller doesn't have to care if those
4708 * are distinct instances or not.
4710 if (ret_code
== LTTNG_OK
) {
4711 lttng_trigger_get(trigger
);
4712 *return_trigger
= trigger
;
4713 /* Ownership of trigger was transferred to caller. */
4721 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4722 const struct lttng_trigger
*trigger
)
4724 enum lttng_error_code ret_code
;
4725 const struct lttng_condition
*condition
=
4726 lttng_trigger_get_const_condition(trigger
);
4727 const enum lttng_domain_type trigger_domain
=
4728 lttng_trigger_get_underlying_domain_type_restriction(
4731 LTTNG_ASSERT(condition
);
4732 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4733 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4735 session_lock_list();
4736 switch (trigger_domain
) {
4737 case LTTNG_DOMAIN_KERNEL
:
4738 ret_code
= kernel_unregister_event_notifier(trigger
);
4739 if (ret_code
!= LTTNG_OK
) {
4740 goto end_unlock_session_list
;
4744 case LTTNG_DOMAIN_UST
:
4745 ust_app_global_update_all_event_notifier_rules();
4747 case LTTNG_DOMAIN_JUL
:
4748 case LTTNG_DOMAIN_LOG4J
:
4749 case LTTNG_DOMAIN_PYTHON
:
4751 /* Agent domains. */
4752 struct agent
*agt
= agent_find_by_event_notifier_domain(
4756 * This trigger was never registered in the first place. Calling
4757 * this function under those circumstances is an internal error.
4760 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4761 if (ret_code
!= LTTNG_OK
) {
4762 goto end_unlock_session_list
;
4767 case LTTNG_DOMAIN_NONE
:
4772 ret_code
= LTTNG_OK
;
4774 end_unlock_session_list
:
4775 session_unlock_list();
4779 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4780 const struct lttng_trigger
*trigger
,
4781 struct notification_thread_handle
*notification_thread
)
4783 enum lttng_error_code ret_code
;
4784 const char *trigger_name
;
4785 uid_t trigger_owner
;
4786 enum lttng_trigger_status trigger_status
;
4787 struct lttng_trigger
*sessiond_trigger
= NULL
;
4789 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4790 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4791 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4792 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4794 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4795 trigger_name
, (int) trigger_owner
,
4796 (int) lttng_credentials_get_uid(cmd_creds
));
4799 * Validate the trigger credentials against the command credentials.
4800 * Only the root user can unregister a trigger with non-matching
4803 if (!lttng_credentials_is_equal_uid(
4804 lttng_trigger_get_credentials(trigger
),
4806 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4807 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4808 trigger_name
, (int) trigger_owner
,
4809 (int) lttng_credentials_get_uid(cmd_creds
));
4810 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4815 /* Fetch the sessiond side trigger object. */
4816 ret_code
= notification_thread_command_get_trigger(
4817 notification_thread
, trigger
, &sessiond_trigger
);
4818 if (ret_code
!= LTTNG_OK
) {
4819 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4820 trigger_name
, (int) trigger_owner
, ret_code
);
4824 LTTNG_ASSERT(sessiond_trigger
);
4827 * From this point on, no matter what, consider the trigger
4830 * We set the unregistered state of the sessiond side trigger object in
4831 * the client thread since we want to minimize the possibility of the
4832 * notification thread being stalled due to a long execution of an
4833 * action that required the trigger lock.
4835 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4837 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4839 if (ret_code
!= LTTNG_OK
) {
4840 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4841 trigger_name
, (int) trigger_owner
, ret_code
);
4846 * Synchronize tracers if the trigger removes an event notifier.
4847 * Do this even if the trigger unregistration failed to at least stop
4848 * the tracers from producing notifications associated with this
4851 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4852 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4853 if (ret_code
!= LTTNG_OK
) {
4854 ERR("Error unregistering trigger to tracer.");
4861 lttng_trigger_put(sessiond_trigger
);
4865 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4866 struct notification_thread_handle
*notification_thread
,
4867 struct lttng_triggers
**return_triggers
)
4870 enum lttng_error_code ret_code
;
4871 struct lttng_triggers
*triggers
= NULL
;
4873 /* Get the set of triggers from the notification thread. */
4874 ret_code
= notification_thread_command_list_triggers(
4875 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4876 if (ret_code
!= LTTNG_OK
) {
4880 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4882 ret_code
= LTTNG_ERR_UNK
;
4886 *return_triggers
= triggers
;
4888 ret_code
= LTTNG_OK
;
4890 lttng_triggers_destroy(triggers
);
4894 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4895 const struct lttng_error_query
*query
,
4896 struct lttng_error_query_results
**_results
,
4897 struct notification_thread_handle
*notification_thread
)
4899 enum lttng_error_code ret_code
;
4900 const struct lttng_trigger
*query_target_trigger
;
4901 const struct lttng_action
*query_target_action
= NULL
;
4902 struct lttng_trigger
*matching_trigger
= NULL
;
4903 const char *trigger_name
;
4904 uid_t trigger_owner
;
4905 enum lttng_trigger_status trigger_status
;
4906 struct lttng_error_query_results
*results
= NULL
;
4908 switch (lttng_error_query_get_target_type(query
)) {
4909 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4910 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4912 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4913 query_target_trigger
=
4914 lttng_error_query_condition_borrow_target(query
);
4916 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4917 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4924 LTTNG_ASSERT(query_target_trigger
);
4926 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4927 query_target_trigger
, &matching_trigger
);
4928 if (ret_code
!= LTTNG_OK
) {
4932 /* No longer needed. */
4933 query_target_trigger
= NULL
;
4935 if (lttng_error_query_get_target_type(query
) ==
4936 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4937 /* Get the sessiond-side version of the target action. */
4938 query_target_action
=
4939 lttng_error_query_action_borrow_action_target(
4940 query
, matching_trigger
);
4943 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4944 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4945 trigger_name
: "(anonymous)";
4946 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4948 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4950 results
= lttng_error_query_results_create();
4952 ret_code
= LTTNG_ERR_NOMEM
;
4956 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4957 trigger_name
, (int) trigger_owner
,
4958 (int) lttng_credentials_get_uid(cmd_creds
));
4961 * Validate the trigger credentials against the command credentials.
4962 * Only the root user can target a trigger with non-matching
4965 if (!lttng_credentials_is_equal_uid(
4966 lttng_trigger_get_credentials(matching_trigger
),
4968 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4969 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4970 trigger_name
, (int) trigger_owner
,
4971 (int) lttng_credentials_get_uid(cmd_creds
));
4972 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4977 switch (lttng_error_query_get_target_type(query
)) {
4978 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4979 trigger_status
= lttng_trigger_add_error_results(
4980 matching_trigger
, results
);
4982 switch (trigger_status
) {
4983 case LTTNG_TRIGGER_STATUS_OK
:
4986 ret_code
= LTTNG_ERR_UNK
;
4991 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4993 trigger_status
= lttng_trigger_condition_add_error_results(
4994 matching_trigger
, results
);
4996 switch (trigger_status
) {
4997 case LTTNG_TRIGGER_STATUS_OK
:
5000 ret_code
= LTTNG_ERR_UNK
;
5006 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
5008 const enum lttng_action_status action_status
=
5009 lttng_action_add_error_query_results(
5010 query_target_action
, results
);
5012 switch (action_status
) {
5013 case LTTNG_ACTION_STATUS_OK
:
5016 ret_code
= LTTNG_ERR_UNK
;
5027 *_results
= results
;
5029 ret_code
= LTTNG_OK
;
5031 lttng_trigger_put(matching_trigger
);
5032 lttng_error_query_results_destroy(results
);
5037 * Send relayd sockets from snapshot output to consumer. Ignore request if the
5038 * snapshot output is *not* set with a remote destination.
5040 * Return LTTNG_OK on success or a LTTNG_ERR code.
5042 static enum lttng_error_code
set_relayd_for_snapshot(
5043 struct consumer_output
*output
,
5044 const struct ltt_session
*session
)
5046 enum lttng_error_code status
= LTTNG_OK
;
5047 struct lttng_ht_iter iter
;
5048 struct consumer_socket
*socket
;
5049 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
5050 const char *base_path
;
5052 LTTNG_ASSERT(output
);
5053 LTTNG_ASSERT(session
);
5055 DBG2("Set relayd object from snapshot output");
5057 if (session
->current_trace_chunk
) {
5058 enum lttng_trace_chunk_status chunk_status
=
5059 lttng_trace_chunk_get_id(
5060 session
->current_trace_chunk
,
5061 ¤t_chunk_id
.value
);
5063 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5064 current_chunk_id
.is_set
= true;
5066 ERR("Failed to get current trace chunk id");
5067 status
= LTTNG_ERR_UNK
;
5072 /* Ignore if snapshot consumer output is not network. */
5073 if (output
->type
!= CONSUMER_DST_NET
) {
5078 * The snapshot record URI base path overrides the session
5081 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5082 base_path
= output
->dst
.net
.control
.subdir
;
5084 base_path
= session
->base_path
;
5088 * For each consumer socket, create and send the relayd object of the
5092 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
5093 pthread_mutex_lock(socket
->lock
);
5094 status
= send_consumer_relayd_sockets(*session
, output
, socket
, base_path
,
5095 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
5096 pthread_mutex_unlock(socket
->lock
);
5097 if (status
!= LTTNG_OK
) {
5109 * Record a kernel snapshot.
5111 * Return LTTNG_OK on success or a LTTNG_ERR code.
5113 static enum lttng_error_code
record_kernel_snapshot(
5114 struct ltt_kernel_session
*ksess
,
5115 const struct consumer_output
*output
,
5116 const struct ltt_session
*session
,
5117 uint64_t nb_packets_per_stream
)
5119 enum lttng_error_code status
;
5121 LTTNG_ASSERT(ksess
);
5122 LTTNG_ASSERT(output
);
5123 LTTNG_ASSERT(session
);
5125 status
= kernel_snapshot_record(
5126 ksess
, output
, nb_packets_per_stream
);
5131 * Record a UST snapshot.
5133 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5135 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5136 const struct consumer_output
*output
,
5137 const struct ltt_session
*session
,
5138 uint64_t nb_packets_per_stream
)
5140 enum lttng_error_code status
;
5142 LTTNG_ASSERT(usess
);
5143 LTTNG_ASSERT(output
);
5144 LTTNG_ASSERT(session
);
5146 status
= ust_app_snapshot_record(
5147 usess
, output
, nb_packets_per_stream
);
5152 uint64_t get_session_size_one_more_packet_per_stream(
5153 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5155 uint64_t tot_size
= 0;
5157 if (session
->kernel_session
) {
5158 struct ltt_kernel_channel
*chan
;
5159 const struct ltt_kernel_session
*ksess
=
5160 session
->kernel_session
;
5162 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5163 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5165 * Don't take channel into account if we
5166 * already grab all its packets.
5170 tot_size
+= chan
->channel
->attr
.subbuf_size
5171 * chan
->stream_count
;
5175 if (session
->ust_session
) {
5176 const struct ltt_ust_session
*usess
= session
->ust_session
;
5178 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5186 * Calculate the number of packets we can grab from each stream that
5187 * fits within the overall snapshot max size.
5189 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5190 * the number of packets per stream.
5192 * TODO: this approach is not perfect: we consider the worse case
5193 * (packet filling the sub-buffers) as an upper bound, but we could do
5194 * better if we do this calculation while we actually grab the packet
5195 * content: we would know how much padding we don't actually store into
5198 * This algorithm is currently bounded by the number of packets per
5201 * Since we call this algorithm before actually grabbing the data, it's
5202 * an approximation: for instance, applications could appear/disappear
5203 * in between this call and actually grabbing data.
5206 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5210 uint64_t cur_nb_packets
= 0;
5213 return 0; /* Infinite */
5216 size_left
= max_size
;
5218 uint64_t one_more_packet_tot_size
;
5220 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5221 session
, cur_nb_packets
);
5222 if (!one_more_packet_tot_size
) {
5223 /* We are already grabbing all packets. */
5226 size_left
-= one_more_packet_tot_size
;
5227 if (size_left
< 0) {
5232 if (!cur_nb_packets
&& size_left
!= max_size
) {
5233 /* Not enough room to grab one packet of each stream, error. */
5236 return cur_nb_packets
;
5240 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5241 const struct snapshot_output
*snapshot_output
)
5243 int64_t nb_packets_per_stream
;
5244 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5246 enum lttng_error_code ret_code
= LTTNG_OK
;
5247 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5248 struct consumer_output
*original_ust_consumer_output
= NULL
;
5249 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5250 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5251 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5253 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5255 snapshot_output
->name
,
5256 snapshot_output
->datetime
,
5257 snapshot_output
->nb_snapshot
);
5258 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5259 ERR("Failed to format snapshot name");
5260 ret_code
= LTTNG_ERR_INVALID
;
5263 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5264 snapshot_output
->name
, session
->name
,
5265 snapshot_chunk_name
);
5266 if (!session
->kernel_session
&& !session
->ust_session
) {
5267 ERR("Failed to record snapshot as no channels exist");
5268 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5272 if (session
->kernel_session
) {
5273 original_kernel_consumer_output
=
5274 session
->kernel_session
->consumer
;
5275 snapshot_kernel_consumer_output
=
5276 consumer_copy_output(snapshot_output
->consumer
);
5277 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5278 snapshot_chunk_name
);
5280 /* Copy the original domain subdir. */
5281 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5282 original_kernel_consumer_output
->domain_subdir
);
5284 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5285 original_kernel_consumer_output
);
5287 ERR("Failed to copy consumer sockets from snapshot output configuration");
5288 ret_code
= LTTNG_ERR_NOMEM
;
5291 ret_code
= set_relayd_for_snapshot(
5292 snapshot_kernel_consumer_output
, session
);
5293 if (ret_code
!= LTTNG_OK
) {
5294 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5297 session
->kernel_session
->consumer
=
5298 snapshot_kernel_consumer_output
;
5300 if (session
->ust_session
) {
5301 original_ust_consumer_output
= session
->ust_session
->consumer
;
5302 snapshot_ust_consumer_output
=
5303 consumer_copy_output(snapshot_output
->consumer
);
5304 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5305 snapshot_chunk_name
);
5307 /* Copy the original domain subdir. */
5308 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5309 original_ust_consumer_output
->domain_subdir
);
5311 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5312 original_ust_consumer_output
);
5314 ERR("Failed to copy consumer sockets from snapshot output configuration");
5315 ret_code
= LTTNG_ERR_NOMEM
;
5318 ret_code
= set_relayd_for_snapshot(
5319 snapshot_ust_consumer_output
, session
);
5320 if (ret_code
!= LTTNG_OK
) {
5321 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5324 session
->ust_session
->consumer
=
5325 snapshot_ust_consumer_output
;
5328 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5329 snapshot_kernel_consumer_output
?:
5330 snapshot_ust_consumer_output
,
5331 consumer_output_get_base_path(
5332 snapshot_output
->consumer
),
5333 snapshot_chunk_name
);
5334 if (!snapshot_trace_chunk
) {
5335 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5337 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5340 LTTNG_ASSERT(!session
->current_trace_chunk
);
5341 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5342 lttng_trace_chunk_put(snapshot_trace_chunk
);
5343 snapshot_trace_chunk
= NULL
;
5345 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5347 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5351 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5352 snapshot_output
->max_size
);
5353 if (nb_packets_per_stream
< 0) {
5354 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5355 goto error_close_trace_chunk
;
5358 if (session
->kernel_session
) {
5359 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5360 snapshot_kernel_consumer_output
, session
,
5361 nb_packets_per_stream
);
5362 if (ret_code
!= LTTNG_OK
) {
5363 goto error_close_trace_chunk
;
5367 if (session
->ust_session
) {
5368 ret_code
= record_ust_snapshot(session
->ust_session
,
5369 snapshot_ust_consumer_output
, session
,
5370 nb_packets_per_stream
);
5371 if (ret_code
!= LTTNG_OK
) {
5372 goto error_close_trace_chunk
;
5376 error_close_trace_chunk
:
5377 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5378 ERR("Failed to release the current trace chunk of session \"%s\"",
5380 ret_code
= LTTNG_ERR_UNK
;
5383 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5384 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5386 * Don't goto end; make sure the chunk is closed for the session
5387 * to allow future snapshots.
5389 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5391 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5394 lttng_trace_chunk_put(snapshot_trace_chunk
);
5395 snapshot_trace_chunk
= NULL
;
5397 if (original_ust_consumer_output
) {
5398 session
->ust_session
->consumer
= original_ust_consumer_output
;
5400 if (original_kernel_consumer_output
) {
5401 session
->kernel_session
->consumer
=
5402 original_kernel_consumer_output
;
5404 consumer_output_put(snapshot_ust_consumer_output
);
5405 consumer_output_put(snapshot_kernel_consumer_output
);
5410 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5412 * The wait parameter is ignored so this call always wait for the snapshot to
5413 * complete before returning.
5415 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5417 int cmd_snapshot_record(struct ltt_session
*session
,
5418 const struct lttng_snapshot_output
*output
,
5419 int wait
__attribute__((unused
)))
5421 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5423 unsigned int snapshot_success
= 0;
5425 struct snapshot_output
*tmp_output
= NULL
;
5427 LTTNG_ASSERT(session
);
5428 LTTNG_ASSERT(output
);
5430 DBG("Cmd snapshot record for session %s", session
->name
);
5432 /* Get the datetime for the snapshot output directory. */
5433 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5436 cmd_ret
= LTTNG_ERR_INVALID
;
5441 * Permission denied to create an output if the session is not
5442 * set in no output mode.
5444 if (session
->output_traces
) {
5445 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5449 /* The session needs to be started at least once. */
5450 if (!session
->has_been_started
) {
5451 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5455 /* Use temporary output for the session. */
5456 if (*output
->ctrl_url
!= '\0') {
5457 tmp_output
= snapshot_output_alloc();
5459 cmd_ret
= LTTNG_ERR_NOMEM
;
5463 ret
= snapshot_output_init(session
, output
->max_size
,
5465 output
->ctrl_url
, output
->data_url
,
5469 if (ret
== -ENOMEM
) {
5470 cmd_ret
= LTTNG_ERR_NOMEM
;
5472 cmd_ret
= LTTNG_ERR_INVALID
;
5476 /* Use the global session count for the temporary snapshot. */
5477 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5479 /* Use the global datetime */
5480 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5481 cmd_ret
= snapshot_record(session
, tmp_output
);
5482 if (cmd_ret
!= LTTNG_OK
) {
5485 snapshot_success
= 1;
5487 struct snapshot_output
*sout
;
5488 struct lttng_ht_iter iter
;
5491 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5492 &iter
.iter
, sout
, node
.node
) {
5493 struct snapshot_output output_copy
;
5496 * Make a local copy of the output and override output
5497 * parameters with those provided as part of the
5500 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5502 if (output
->max_size
!= (uint64_t) -1ULL) {
5503 output_copy
.max_size
= output
->max_size
;
5506 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5507 memcpy(output_copy
.datetime
, datetime
,
5510 /* Use temporary name. */
5511 if (*output
->name
!= '\0') {
5512 if (lttng_strncpy(output_copy
.name
,
5514 sizeof(output_copy
.name
))) {
5515 cmd_ret
= LTTNG_ERR_INVALID
;
5521 cmd_ret
= snapshot_record(session
, &output_copy
);
5522 if (cmd_ret
!= LTTNG_OK
) {
5526 snapshot_success
= 1;
5531 if (snapshot_success
) {
5532 session
->snapshot
.nb_snapshot
++;
5534 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5539 snapshot_output_destroy(tmp_output
);
5545 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5547 int cmd_set_session_shm_path(struct ltt_session
*session
,
5548 const char *shm_path
)
5551 LTTNG_ASSERT(session
);
5554 * Can only set shm path before session is started.
5556 if (session
->has_been_started
) {
5557 return LTTNG_ERR_SESSION_STARTED
;
5560 strncpy(session
->shm_path
, shm_path
,
5561 sizeof(session
->shm_path
));
5562 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5568 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5570 * Ask the consumer to rotate the session output directory.
5571 * The session lock must be held.
5573 * Returns LTTNG_OK on success or else a negative LTTng error code.
5575 int cmd_rotate_session(struct ltt_session
*session
,
5576 struct lttng_rotate_session_return
*rotate_return
,
5577 bool quiet_rotation
,
5578 enum lttng_trace_chunk_command_type command
)
5581 uint64_t ongoing_rotation_chunk_id
;
5582 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5583 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5584 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5585 enum lttng_trace_chunk_status chunk_status
;
5586 bool failed_to_rotate
= false;
5587 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5589 LTTNG_ASSERT(session
);
5591 if (!session
->has_been_started
) {
5592 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5597 * Explicit rotation is not supported for live sessions.
5598 * However, live sessions can perform a quiet rotation on
5600 * Rotation is not supported for snapshot traces (no output).
5602 if ((!quiet_rotation
&& session
->live_timer
) ||
5603 !session
->output_traces
) {
5604 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5608 /* Unsupported feature in lttng-relayd before 2.11. */
5609 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5610 (session
->consumer
->relay_major_version
== 2 &&
5611 session
->consumer
->relay_minor_version
< 11)) {
5612 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5616 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5617 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5618 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5622 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5623 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5625 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5630 * After a stop, we only allow one rotation to occur, the other ones are
5631 * useless until a new start.
5633 if (session
->rotated_after_last_stop
) {
5634 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5636 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5641 * After a stop followed by a clear, disallow following rotations a they would
5642 * generate empty chunks.
5644 if (session
->cleared_after_last_stop
) {
5645 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5647 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5651 if (session
->active
) {
5652 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5654 if (!new_trace_chunk
) {
5655 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5661 * The current trace chunk becomes the chunk being archived.
5663 * After this point, "chunk_being_archived" must absolutely
5664 * be closed on the consumer(s), otherwise it will never be
5665 * cleaned-up, which will result in a leak.
5667 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5668 &chunk_being_archived
);
5670 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5674 if (session
->kernel_session
) {
5675 cmd_ret
= kernel_rotate_session(session
);
5676 if (cmd_ret
!= LTTNG_OK
) {
5677 failed_to_rotate
= true;
5678 rotation_fail_code
= cmd_ret
;
5681 if (session
->ust_session
) {
5682 cmd_ret
= ust_app_rotate_session(session
);
5683 if (cmd_ret
!= LTTNG_OK
) {
5684 failed_to_rotate
= true;
5685 rotation_fail_code
= cmd_ret
;
5689 if (!session
->active
) {
5690 session
->rotated_after_last_stop
= true;
5693 if (!chunk_being_archived
) {
5694 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5696 if (failed_to_rotate
) {
5697 cmd_ret
= rotation_fail_code
;
5704 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5705 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5706 &ongoing_rotation_chunk_id
);
5707 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5709 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5710 command
, session
->last_chunk_path
);
5712 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5716 if (failed_to_rotate
) {
5717 cmd_ret
= rotation_fail_code
;
5721 session
->quiet_rotation
= quiet_rotation
;
5722 ret
= timer_session_rotation_pending_check_start(session
,
5723 DEFAULT_ROTATE_PENDING_TIMER
);
5725 cmd_ret
= LTTNG_ERR_UNK
;
5729 if (rotate_return
) {
5730 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5733 session
->chunk_being_archived
= chunk_being_archived
;
5734 chunk_being_archived
= NULL
;
5735 if (!quiet_rotation
) {
5736 ret
= notification_thread_command_session_rotation_ongoing(
5737 the_notification_thread_handle
, session
->id
,
5738 ongoing_rotation_chunk_id
);
5739 if (ret
!= LTTNG_OK
) {
5740 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5742 cmd_ret
= (lttng_error_code
) ret
;
5746 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5747 session
->name
, ongoing_rotation_chunk_id
);
5749 lttng_trace_chunk_put(new_trace_chunk
);
5750 lttng_trace_chunk_put(chunk_being_archived
);
5751 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5754 if (session_reset_rotation_state(session
,
5755 LTTNG_ROTATION_STATE_ERROR
)) {
5756 ERR("Failed to reset rotation state of session \"%s\"",
5763 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5765 * Check if the session has finished its rotation.
5767 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5769 int cmd_rotate_get_info(struct ltt_session
*session
,
5770 struct lttng_rotation_get_info_return
*info_return
,
5771 uint64_t rotation_id
)
5773 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5774 enum lttng_rotation_state rotation_state
;
5776 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5777 session
->most_recent_chunk_id
.value
);
5779 if (session
->chunk_being_archived
) {
5780 enum lttng_trace_chunk_status chunk_status
;
5783 chunk_status
= lttng_trace_chunk_get_id(
5784 session
->chunk_being_archived
,
5786 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5788 rotation_state
= rotation_id
== chunk_id
?
5789 LTTNG_ROTATION_STATE_ONGOING
:
5790 LTTNG_ROTATION_STATE_EXPIRED
;
5792 if (session
->last_archived_chunk_id
.is_set
&&
5793 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5794 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5796 rotation_state
= session
->rotation_state
;
5800 switch (rotation_state
) {
5801 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5802 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5805 case LTTNG_ROTATION_STATE_EXPIRED
:
5806 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5807 rotation_id
, session
->name
);
5809 case LTTNG_ROTATION_STATE_ONGOING
:
5810 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5811 rotation_id
, session
->name
);
5813 case LTTNG_ROTATION_STATE_COMPLETED
:
5817 char *current_tracing_path_reply
;
5818 size_t current_tracing_path_reply_len
;
5820 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5821 rotation_id
, session
->name
);
5823 switch (session_get_consumer_destination_type(session
)) {
5824 case CONSUMER_DST_LOCAL
:
5825 current_tracing_path_reply
=
5826 info_return
->location
.local
.absolute_path
;
5827 current_tracing_path_reply_len
=
5828 sizeof(info_return
->location
.local
.absolute_path
);
5829 info_return
->location_type
=
5830 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5831 fmt_ret
= asprintf(&chunk_path
,
5832 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5833 session_get_base_path(session
),
5834 session
->last_archived_chunk_name
);
5835 if (fmt_ret
== -1) {
5836 PERROR("Failed to format the path of the last archived trace chunk");
5837 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5838 cmd_ret
= LTTNG_ERR_UNK
;
5842 case CONSUMER_DST_NET
:
5844 uint16_t ctrl_port
, data_port
;
5846 current_tracing_path_reply
=
5847 info_return
->location
.relay
.relative_path
;
5848 current_tracing_path_reply_len
=
5849 sizeof(info_return
->location
.relay
.relative_path
);
5850 /* Currently the only supported relay protocol. */
5851 info_return
->location
.relay
.protocol
=
5852 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5854 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5855 session_get_net_consumer_hostname(session
),
5856 sizeof(info_return
->location
.relay
.host
));
5858 ERR("Failed to copy host name to rotate_get_info reply");
5859 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5860 cmd_ret
= LTTNG_ERR_SET_URL
;
5864 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5865 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5866 info_return
->location
.relay
.ports
.data
= data_port
;
5867 info_return
->location_type
=
5868 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5869 chunk_path
= strdup(session
->last_chunk_path
);
5871 ERR("Failed to allocate the path of the last archived trace chunk");
5872 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5873 cmd_ret
= LTTNG_ERR_UNK
;
5882 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5883 chunk_path
, current_tracing_path_reply_len
);
5886 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5887 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5888 cmd_ret
= LTTNG_ERR_UNK
;
5894 case LTTNG_ROTATION_STATE_ERROR
:
5895 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5896 rotation_id
, session
->name
);
5904 info_return
->status
= (int32_t) rotation_state
;
5909 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5911 * Configure the automatic rotation parameters.
5912 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5913 * 'activate' to false means deactivate the rotation schedule and validate that
5914 * 'new_value' has the same value as the currently active value.
5916 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5918 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5919 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5921 struct notification_thread_handle
*notification_thread_handle
)
5924 uint64_t *parameter_value
;
5926 LTTNG_ASSERT(session
);
5928 DBG("Cmd rotate set schedule session %s", session
->name
);
5930 if (session
->live_timer
|| !session
->output_traces
) {
5931 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5932 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5936 switch (schedule_type
) {
5937 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5938 parameter_value
= &session
->rotate_size
;
5940 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5941 parameter_value
= &session
->rotate_timer_period
;
5942 if (new_value
>= UINT_MAX
) {
5943 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5944 new_value
, UINT_MAX
);
5945 ret
= LTTNG_ERR_INVALID
;
5950 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5951 ret
= LTTNG_ERR_INVALID
;
5955 /* Improper use of the API. */
5956 if (new_value
== -1ULL) {
5957 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5958 ret
= LTTNG_ERR_INVALID
;
5963 * As indicated in struct ltt_session's comments, a value of == 0 means
5964 * this schedule rotation type is not in use.
5966 * Reject the command if we were asked to activate a schedule that was
5969 if (activate
&& *parameter_value
!= 0) {
5970 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5971 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5976 * Reject the command if we were asked to deactivate a schedule that was
5979 if (!activate
&& *parameter_value
== 0) {
5980 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5981 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5986 * Reject the command if we were asked to deactivate a schedule that
5989 if (!activate
&& *parameter_value
!= new_value
) {
5990 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5991 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5995 *parameter_value
= activate
? new_value
: 0;
5997 switch (schedule_type
) {
5998 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5999 if (activate
&& session
->active
) {
6001 * Only start the timer if the session is active,
6002 * otherwise it will be started when the session starts.
6004 ret
= timer_session_rotation_schedule_timer_start(
6005 session
, new_value
);
6007 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
6008 ret
= LTTNG_ERR_UNK
;
6012 ret
= timer_session_rotation_schedule_timer_stop(
6015 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
6016 ret
= LTTNG_ERR_UNK
;
6021 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
6023 ret
= subscribe_session_consumed_size_rotation(session
,
6024 new_value
, notification_thread_handle
);
6026 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
6027 ret
= LTTNG_ERR_UNK
;
6031 ret
= unsubscribe_session_consumed_size_rotation(session
,
6032 notification_thread_handle
);
6034 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
6035 ret
= LTTNG_ERR_UNK
;
6042 /* Would have been caught before. */
6054 /* Wait for a given path to be removed before continuing. */
6055 static enum lttng_error_code
wait_on_path(void *path_data
)
6057 const char *shm_path
= (const char *) path_data
;
6059 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6065 ret
= stat(shm_path
, &st
);
6067 if (errno
!= ENOENT
) {
6068 PERROR("stat() returned an error while checking for the existence of the shm path");
6070 DBG("shm path no longer exists, completing the destruction of session");
6074 if (!S_ISDIR(st
.st_mode
)) {
6075 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6080 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6086 * Returns a pointer to a handler to run on completion of a command.
6087 * Returns NULL if no handler has to be run for the last command executed.
6089 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6091 struct cmd_completion_handler
*handler
= current_completion_handler
;
6093 current_completion_handler
= NULL
;
6098 * Init command subsystem.
6103 * Set network sequence index to 1 for streams to match a relayd
6104 * socket on the consumer side.
6106 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6107 relayd_net_seq_idx
= 1;
6108 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6110 DBG("Command subsystem initialized");