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
},
901 {LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_2
,
902 LTTCOMM_RELAYD_CONFIGURATION_TRACE_FORMAT_SUPPORTED_CTF2
},
905 auto it
= mapping
.find(trace_format
.type());
906 if (it
== mapping
.end()) {
910 if (!(supported_trace_format
& it
->second
)) {
918 * Send both relayd sockets to a specific consumer and domain. This is a
919 * helper function to facilitate sending the information to the consumer for a
922 * The consumer socket lock must be held by the caller.
924 * Returns LTTNG_OK, or an LTTng error code on failure.
926 static lttng_error_code
send_consumer_relayd_sockets(const ltt_session
& session
,
927 struct consumer_output
*consumer
,
928 struct consumer_socket
*sock
,
929 const char *base_path
,
930 const uint64_t *current_chunk_id
)
932 enum lttng_error_code status
= LTTNG_OK
;
933 struct lttcomm_relayd_sock
*control_sock
= nullptr;
934 struct lttcomm_relayd_sock
*data_sock
= nullptr;
936 LTTNG_ASSERT(consumer
);
939 /* Sending control relayd socket. */
940 if (!sock
->control_sock_sent
) {
942 uint64_t result_flags
= 0;
943 uint64_t supported_trace_format
= 0;
944 /* Connect to relayd and make version check if uri is the control. */
945 status
= create_connect_relayd(&consumer
->dst
.net
.control
, &control_sock
);
946 if (status
!= LTTNG_OK
) {
949 LTTNG_ASSERT(control_sock
);
951 consumer
->relay_major_version
= control_sock
->major
;
952 consumer
->relay_minor_version
= control_sock
->minor
;
954 ret
= relayd_get_configuration(control_sock
, 0, result_flags
, nullptr);
956 ERR("Unable to get relayd configuration");
957 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
961 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
962 consumer
->relay_allows_clear
= true;
965 if (!is_trace_format_configuration_supported(
966 supported_trace_format
, *session
.trace_format
)) {
967 ERR("Relayd does not support the requested trace format");
968 status
= LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_RELAY_DAEMON
;
972 status
= send_consumer_relayd_socket(session
.id
, &consumer
->dst
.net
.control
,
973 consumer
, sock
, session
.name
, session
.hostname
, base_path
,
974 session
.live_timer
, current_chunk_id
, session
.creation_time
,
975 session
.name_contains_creation_time
, *control_sock
);
977 if (status
!= LTTNG_OK
) {
982 /* Sending data relayd socket. */
983 if (!sock
->data_sock_sent
) {
984 /* Connect to relayd and make version check if uri is the control. */
985 status
= create_connect_relayd(&consumer
->dst
.net
.data
, &data_sock
);
986 if (status
!= LTTNG_OK
) {
989 LTTNG_ASSERT(data_sock
);
991 status
= send_consumer_relayd_socket(session
.id
, &consumer
->dst
.net
.data
, consumer
,
992 sock
, session
.name
, session
.hostname
, base_path
, session
.live_timer
,
993 current_chunk_id
, session
.creation_time
,
994 session
.name_contains_creation_time
, *data_sock
);
996 if (status
!= LTTNG_OK
) {
1002 if (control_sock
!= nullptr) {
1003 relayd_close(control_sock
);
1007 if (data_sock
!= nullptr) {
1008 relayd_close(data_sock
);
1016 * Setup relayd connections for a tracing session. First creates the socket to
1017 * the relayd and send them to the right domain consumer. Consumer type MUST be
1020 int cmd_setup_relayd(struct ltt_session
*session
)
1023 struct ltt_ust_session
*usess
;
1024 struct ltt_kernel_session
*ksess
;
1025 struct consumer_socket
*socket
;
1026 struct lttng_ht_iter iter
;
1027 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1029 LTTNG_ASSERT(session
);
1031 usess
= session
->ust_session
;
1032 ksess
= session
->kernel_session
;
1034 DBG("Setting relayd for session %s", session
->name
);
1037 if (session
->current_trace_chunk
) {
1038 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1039 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1041 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1042 current_chunk_id
.is_set
= true;
1044 ERR("Failed to get current trace chunk id");
1045 ret
= LTTNG_ERR_UNK
;
1050 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1051 && usess
->consumer
->enabled
) {
1052 /* For each consumer socket, send relayd sockets */
1053 cds_lfht_for_each_entry (
1054 usess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1055 pthread_mutex_lock(socket
->lock
);
1056 ret
= send_consumer_relayd_sockets(*session
, usess
->consumer
, socket
,
1058 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
1059 pthread_mutex_unlock(socket
->lock
);
1060 if (ret
!= LTTNG_OK
) {
1063 /* Session is now ready for network streaming. */
1064 session
->net_handle
= 1;
1066 session
->consumer
->relay_major_version
=
1067 usess
->consumer
->relay_major_version
;
1068 session
->consumer
->relay_minor_version
=
1069 usess
->consumer
->relay_minor_version
;
1070 session
->consumer
->relay_allows_clear
=
1071 usess
->consumer
->relay_allows_clear
;
1074 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1075 && ksess
->consumer
->enabled
) {
1076 cds_lfht_for_each_entry (
1077 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1078 pthread_mutex_lock(socket
->lock
);
1079 ret
= send_consumer_relayd_sockets(*session
, ksess
->consumer
, socket
,
1081 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
1082 pthread_mutex_unlock(socket
->lock
);
1083 if (ret
!= LTTNG_OK
) {
1086 /* Session is now ready for network streaming. */
1087 session
->net_handle
= 1;
1089 session
->consumer
->relay_major_version
=
1090 ksess
->consumer
->relay_major_version
;
1091 session
->consumer
->relay_minor_version
=
1092 ksess
->consumer
->relay_minor_version
;
1093 session
->consumer
->relay_allows_clear
=
1094 ksess
->consumer
->relay_allows_clear
;
1103 * Start a kernel session by opening all necessary streams.
1105 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1108 struct ltt_kernel_channel
*kchan
;
1110 /* Open kernel metadata */
1111 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1112 ret
= kernel_open_metadata(ksess
);
1114 ret
= LTTNG_ERR_KERN_META_FAIL
;
1119 /* Open kernel metadata stream */
1120 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1121 ret
= kernel_open_metadata_stream(ksess
);
1123 ERR("Kernel create metadata stream failed");
1124 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1129 /* For each channel */
1130 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1131 if (kchan
->stream_count
== 0) {
1132 ret
= kernel_open_channel_stream(kchan
);
1134 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1137 /* Update the stream global counter */
1138 ksess
->stream_count_global
+= ret
;
1142 /* Setup kernel consumer socket and send fds to it */
1143 ret
= init_kernel_tracing(ksess
);
1145 ret
= LTTNG_ERR_KERN_START_FAIL
;
1149 /* This start the kernel tracing */
1150 ret
= kernel_start_session(ksess
);
1152 ret
= LTTNG_ERR_KERN_START_FAIL
;
1156 /* Quiescent wait after starting trace */
1157 kernel_wait_quiescent();
1167 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1169 struct ltt_kernel_channel
*kchan
;
1170 bool error_occurred
= false;
1173 if (!ksess
|| !ksess
->active
) {
1176 DBG("Stopping kernel tracing");
1178 ret
= kernel_stop_session(ksess
);
1180 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1184 kernel_wait_quiescent();
1186 /* Flush metadata after stopping (if exists) */
1187 if (ksess
->metadata_stream_fd
>= 0) {
1188 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1190 ERR("Kernel metadata flush failed");
1191 error_occurred
= true;
1195 /* Flush all buffers after stopping */
1196 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1197 ret
= kernel_flush_buffer(kchan
);
1199 ERR("Kernel flush buffer error");
1200 error_occurred
= true;
1205 if (error_occurred
) {
1206 ret
= LTTNG_ERR_UNK
;
1215 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1217 int cmd_disable_channel(struct ltt_session
*session
,
1218 enum lttng_domain_type domain
, char *channel_name
)
1221 struct ltt_ust_session
*usess
;
1223 usess
= session
->ust_session
;
1228 case LTTNG_DOMAIN_KERNEL
:
1230 ret
= channel_kernel_disable(session
->kernel_session
,
1232 if (ret
!= LTTNG_OK
) {
1236 kernel_wait_quiescent();
1239 case LTTNG_DOMAIN_UST
:
1241 struct ltt_ust_channel
*uchan
;
1242 struct lttng_ht
*chan_ht
;
1244 chan_ht
= usess
->domain_global
.channels
;
1246 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1247 if (uchan
== NULL
) {
1248 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1252 ret
= channel_ust_disable(usess
, uchan
);
1253 if (ret
!= LTTNG_OK
) {
1259 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1271 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1273 * The wpipe arguments is used as a notifier for the kernel thread.
1275 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1279 ssize_t sock_recv_len
;
1280 struct lttng_channel
*channel
= NULL
;
1281 struct lttng_buffer_view view
;
1282 struct lttng_dynamic_buffer channel_buffer
;
1283 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1285 lttng_dynamic_buffer_init(&channel_buffer
);
1286 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1287 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1289 ret
= LTTNG_ERR_NOMEM
;
1293 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1295 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1296 ERR("Failed to receive \"enable channel\" command payload");
1297 ret
= LTTNG_ERR_INVALID
;
1301 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1302 if (!lttng_buffer_view_is_valid(&view
)) {
1303 ret
= LTTNG_ERR_INVALID
;
1307 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1308 ERR("Invalid channel payload received in \"enable channel\" command");
1309 ret
= LTTNG_ERR_INVALID
;
1313 ret
= cmd_enable_channel_internal(
1314 cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1317 lttng_dynamic_buffer_reset(&channel_buffer
);
1318 lttng_channel_destroy(channel
);
1322 static enum lttng_error_code
kernel_domain_check_trace_format_requirements(
1323 const lttng::trace_format_descriptor
& descriptor
)
1325 enum lttng_error_code ret_code
= LTTNG_OK
;
1326 switch (descriptor
.type()) {
1327 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1
:
1328 /* Supported by all kernel tracer. */
1330 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_2
:
1331 if (!kernel_supports_ctf2()) {
1332 ret_code
= LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_KERNEL_TRACER
;
1341 static enum lttng_error_code
cmd_enable_channel_internal(
1342 struct ltt_session
*session
,
1343 const struct lttng_domain
*domain
,
1344 const struct lttng_channel
*_attr
,
1347 enum lttng_error_code ret_code
;
1348 struct ltt_ust_session
*usess
= session
->ust_session
;
1349 struct lttng_ht
*chan_ht
;
1351 struct lttng_channel
*attr
= NULL
;
1353 LTTNG_ASSERT(session
);
1354 LTTNG_ASSERT(_attr
);
1355 LTTNG_ASSERT(domain
);
1357 attr
= lttng_channel_copy(_attr
);
1359 ret_code
= LTTNG_ERR_NOMEM
;
1363 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1365 /* Validate channel name */
1366 if (attr
->name
[0] == '.' ||
1367 memchr(attr
->name
, '/', len
) != NULL
) {
1368 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1372 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1377 * If the session is a live session, remove the switch timer, the
1378 * live timer does the same thing but sends also synchronisation
1379 * beacons for inactive streams.
1381 if (session
->live_timer
> 0) {
1382 attr
->attr
.live_timer_interval
= session
->live_timer
;
1383 attr
->attr
.switch_timer_interval
= 0;
1386 /* Check for feature support */
1387 switch (domain
->type
) {
1388 case LTTNG_DOMAIN_KERNEL
:
1390 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1391 /* Sampling position of buffer is not supported */
1392 WARN("Kernel tracer does not support buffer monitoring. "
1393 "Setting the monitor interval timer to 0 "
1394 "(disabled) for channel '%s' of session '%s'",
1395 attr
->name
, session
->name
);
1396 lttng_channel_set_monitor_timer_interval(attr
, 0);
1399 ret_code
= kernel_domain_check_trace_format_requirements(*session
->trace_format
);
1400 if (ret_code
!= LTTNG_OK
) {
1401 WARN("Kernel tracer does not support the configured trace format of session '%s'",
1407 case LTTNG_DOMAIN_UST
:
1409 case LTTNG_DOMAIN_JUL
:
1410 case LTTNG_DOMAIN_LOG4J
:
1411 case LTTNG_DOMAIN_PYTHON
:
1412 if (!agent_tracing_is_enabled()) {
1413 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1414 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1419 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1423 switch (domain
->type
) {
1424 case LTTNG_DOMAIN_KERNEL
:
1426 struct ltt_kernel_channel
*kchan
;
1428 kchan
= trace_kernel_get_channel_by_name(
1429 attr
->name
, session
->kernel_session
);
1430 if (kchan
== NULL
) {
1432 * Don't try to create a channel if the session has been started at
1433 * some point in time before. The tracer does not allow it.
1435 if (session
->has_been_started
) {
1436 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1440 if (session
->snapshot
.nb_output
> 0 ||
1441 session
->snapshot_mode
) {
1442 /* Enforce mmap output for snapshot sessions. */
1443 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1445 ret_code
= channel_kernel_create(
1446 session
->kernel_session
, attr
, wpipe
);
1447 if (attr
->name
[0] != '\0') {
1448 session
->kernel_session
->has_non_default_channel
= 1;
1451 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1454 if (ret_code
!= LTTNG_OK
) {
1458 kernel_wait_quiescent();
1461 case LTTNG_DOMAIN_UST
:
1462 case LTTNG_DOMAIN_JUL
:
1463 case LTTNG_DOMAIN_LOG4J
:
1464 case LTTNG_DOMAIN_PYTHON
:
1466 struct ltt_ust_channel
*uchan
;
1471 * Current agent implementation limitations force us to allow
1472 * only one channel at once in "agent" subdomains. Each
1473 * subdomain has a default channel name which must be strictly
1476 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1477 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1478 LTTNG_SYMBOL_NAME_LEN
)) {
1479 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1482 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1483 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1484 LTTNG_SYMBOL_NAME_LEN
)) {
1485 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1488 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1489 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1490 LTTNG_SYMBOL_NAME_LEN
)) {
1491 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1496 chan_ht
= usess
->domain_global
.channels
;
1498 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1499 if (uchan
== NULL
) {
1501 * Don't try to create a channel if the session has been started at
1502 * some point in time before. The tracer does not allow it.
1504 if (session
->has_been_started
) {
1505 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1509 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1510 if (attr
->name
[0] != '\0') {
1511 usess
->has_non_default_channel
= 1;
1514 ret_code
= channel_ust_enable(usess
, uchan
);
1519 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1523 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1524 session
->has_non_mmap_channel
= true;
1529 lttng_channel_destroy(attr
);
1533 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1534 struct ltt_session
*session
,
1535 enum lttng_domain_type domain
,
1536 enum lttng_process_attr process_attr
,
1537 enum lttng_tracking_policy
*policy
)
1539 enum lttng_error_code ret_code
= LTTNG_OK
;
1540 const struct process_attr_tracker
*tracker
;
1543 case LTTNG_DOMAIN_KERNEL
:
1544 if (!session
->kernel_session
) {
1545 ret_code
= LTTNG_ERR_INVALID
;
1548 tracker
= kernel_get_process_attr_tracker(
1549 session
->kernel_session
, process_attr
);
1551 case LTTNG_DOMAIN_UST
:
1552 if (!session
->ust_session
) {
1553 ret_code
= LTTNG_ERR_INVALID
;
1556 tracker
= trace_ust_get_process_attr_tracker(
1557 session
->ust_session
, process_attr
);
1560 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1564 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1566 ret_code
= LTTNG_ERR_INVALID
;
1572 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1573 struct ltt_session
*session
,
1574 enum lttng_domain_type domain
,
1575 enum lttng_process_attr process_attr
,
1576 enum lttng_tracking_policy policy
)
1578 enum lttng_error_code ret_code
= LTTNG_OK
;
1581 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1582 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1583 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1586 ret_code
= LTTNG_ERR_INVALID
;
1591 case LTTNG_DOMAIN_KERNEL
:
1592 if (!session
->kernel_session
) {
1593 ret_code
= LTTNG_ERR_INVALID
;
1596 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1597 session
->kernel_session
, process_attr
, policy
);
1599 case LTTNG_DOMAIN_UST
:
1600 if (!session
->ust_session
) {
1601 ret_code
= LTTNG_ERR_INVALID
;
1604 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1605 session
->ust_session
, process_attr
, policy
);
1608 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1615 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1616 struct ltt_session
*session
,
1617 enum lttng_domain_type domain
,
1618 enum lttng_process_attr process_attr
,
1619 const struct process_attr_value
*value
)
1621 enum lttng_error_code ret_code
= LTTNG_OK
;
1624 case LTTNG_DOMAIN_KERNEL
:
1625 if (!session
->kernel_session
) {
1626 ret_code
= LTTNG_ERR_INVALID
;
1629 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1630 session
->kernel_session
, process_attr
, value
);
1632 case LTTNG_DOMAIN_UST
:
1633 if (!session
->ust_session
) {
1634 ret_code
= LTTNG_ERR_INVALID
;
1637 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1638 session
->ust_session
, process_attr
, value
);
1641 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1648 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1649 struct ltt_session
*session
,
1650 enum lttng_domain_type domain
,
1651 enum lttng_process_attr process_attr
,
1652 const struct process_attr_value
*value
)
1654 enum lttng_error_code ret_code
= LTTNG_OK
;
1657 case LTTNG_DOMAIN_KERNEL
:
1658 if (!session
->kernel_session
) {
1659 ret_code
= LTTNG_ERR_INVALID
;
1662 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1663 session
->kernel_session
, process_attr
, value
);
1665 case LTTNG_DOMAIN_UST
:
1666 if (!session
->ust_session
) {
1667 ret_code
= LTTNG_ERR_INVALID
;
1670 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1671 session
->ust_session
, process_attr
, value
);
1674 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1681 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1682 struct ltt_session
*session
,
1683 enum lttng_domain_type domain
,
1684 enum lttng_process_attr process_attr
,
1685 struct lttng_process_attr_values
**values
)
1687 enum lttng_error_code ret_code
= LTTNG_OK
;
1688 const struct process_attr_tracker
*tracker
;
1689 enum process_attr_tracker_status status
;
1692 case LTTNG_DOMAIN_KERNEL
:
1693 if (!session
->kernel_session
) {
1694 ret_code
= LTTNG_ERR_INVALID
;
1697 tracker
= kernel_get_process_attr_tracker(
1698 session
->kernel_session
, process_attr
);
1700 case LTTNG_DOMAIN_UST
:
1701 if (!session
->ust_session
) {
1702 ret_code
= LTTNG_ERR_INVALID
;
1705 tracker
= trace_ust_get_process_attr_tracker(
1706 session
->ust_session
, process_attr
);
1709 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1714 ret_code
= LTTNG_ERR_INVALID
;
1718 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1720 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1721 ret_code
= LTTNG_OK
;
1723 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1724 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1726 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1727 ret_code
= LTTNG_ERR_NOMEM
;
1730 ret_code
= LTTNG_ERR_UNK
;
1739 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1741 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1742 struct lttng_event
*event
,
1743 char *filter_expression
,
1744 struct lttng_bytecode
*bytecode
,
1745 struct lttng_event_exclusion
*exclusion
)
1748 const char *event_name
;
1749 const struct ltt_session
*session
= cmd_ctx
->session
;
1750 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1751 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1753 DBG("Disable event command for event \'%s\'", event
->name
);
1756 * Filter and exclusions are simply not handled by the
1757 * disable event command at this time.
1761 (void) filter_expression
;
1764 /* Ignore the presence of filter or exclusion for the event */
1766 event
->exclusion
= 0;
1768 event_name
= event
->name
;
1770 /* Error out on unhandled search criteria */
1771 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1772 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1773 ret
= LTTNG_ERR_UNK
;
1780 case LTTNG_DOMAIN_KERNEL
:
1782 struct ltt_kernel_channel
*kchan
;
1783 struct ltt_kernel_session
*ksess
;
1785 ksess
= session
->kernel_session
;
1788 * If a non-default channel has been created in the
1789 * session, explicitely require that -c chan_name needs
1792 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1793 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1797 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1798 if (kchan
== NULL
) {
1799 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1803 switch (event
->type
) {
1804 case LTTNG_EVENT_ALL
:
1805 case LTTNG_EVENT_TRACEPOINT
:
1806 case LTTNG_EVENT_SYSCALL
:
1807 case LTTNG_EVENT_PROBE
:
1808 case LTTNG_EVENT_FUNCTION
:
1809 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1810 if (event_name
[0] == '\0') {
1811 ret
= event_kernel_disable_event(kchan
,
1814 ret
= event_kernel_disable_event(kchan
,
1815 event_name
, event
->type
);
1817 if (ret
!= LTTNG_OK
) {
1822 ret
= LTTNG_ERR_UNK
;
1826 kernel_wait_quiescent();
1829 case LTTNG_DOMAIN_UST
:
1831 struct ltt_ust_channel
*uchan
;
1832 struct ltt_ust_session
*usess
;
1834 usess
= session
->ust_session
;
1836 if (validate_ust_event_name(event_name
)) {
1837 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1842 * If a non-default channel has been created in the
1843 * session, explicitly require that -c chan_name needs
1846 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1847 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1851 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1853 if (uchan
== NULL
) {
1854 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1858 switch (event
->type
) {
1859 case LTTNG_EVENT_ALL
:
1861 * An empty event name means that everything
1862 * should be disabled.
1864 if (event
->name
[0] == '\0') {
1865 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1867 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1870 if (ret
!= LTTNG_OK
) {
1875 ret
= LTTNG_ERR_UNK
;
1879 DBG3("Disable UST event %s in channel %s completed", event_name
,
1883 case LTTNG_DOMAIN_LOG4J
:
1884 case LTTNG_DOMAIN_JUL
:
1885 case LTTNG_DOMAIN_PYTHON
:
1888 struct ltt_ust_session
*usess
= session
->ust_session
;
1890 LTTNG_ASSERT(usess
);
1892 switch (event
->type
) {
1893 case LTTNG_EVENT_ALL
:
1896 ret
= LTTNG_ERR_UNK
;
1900 agt
= trace_ust_find_agent(usess
, domain
);
1902 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1906 * An empty event name means that everything
1907 * should be disabled.
1909 if (event
->name
[0] == '\0') {
1910 ret
= event_agent_disable_all(usess
, agt
);
1912 ret
= event_agent_disable(usess
, agt
, event_name
);
1914 if (ret
!= LTTNG_OK
) {
1921 ret
= LTTNG_ERR_UND
;
1932 free(filter_expression
);
1937 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1939 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1940 const struct lttng_event_context
*event_context
, int kwpipe
)
1942 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1943 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1944 const struct ltt_session
*session
= cmd_ctx
->session
;
1945 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1948 * Don't try to add a context if the session has been started at
1949 * some point in time before. The tracer does not allow it and would
1950 * result in a corrupted trace.
1952 if (cmd_ctx
->session
->has_been_started
) {
1953 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1958 case LTTNG_DOMAIN_KERNEL
:
1959 LTTNG_ASSERT(session
->kernel_session
);
1961 if (session
->kernel_session
->channel_count
== 0) {
1962 /* Create default channel */
1963 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1964 if (ret
!= LTTNG_OK
) {
1967 chan_kern_created
= 1;
1969 /* Add kernel context to kernel tracer */
1970 ret
= context_kernel_add(session
->kernel_session
,
1971 event_context
, channel_name
);
1972 if (ret
!= LTTNG_OK
) {
1976 case LTTNG_DOMAIN_JUL
:
1977 case LTTNG_DOMAIN_LOG4J
:
1980 * Validate channel name.
1981 * If no channel name is given and the domain is JUL or LOG4J,
1982 * set it to the appropriate domain-specific channel name. If
1983 * a name is provided but does not match the expexted channel
1984 * name, return an error.
1986 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1987 strcmp(channel_name
,
1988 DEFAULT_JUL_CHANNEL_NAME
)) {
1989 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1991 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1992 strcmp(channel_name
,
1993 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1994 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1999 case LTTNG_DOMAIN_UST
:
2001 struct ltt_ust_session
*usess
= session
->ust_session
;
2002 unsigned int chan_count
;
2004 LTTNG_ASSERT(usess
);
2006 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
2007 if (chan_count
== 0) {
2008 struct lttng_channel
*attr
;
2009 /* Create default channel */
2010 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
2012 ret
= LTTNG_ERR_FATAL
;
2016 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
2017 if (ret
!= LTTNG_OK
) {
2021 channel_attr_destroy(attr
);
2022 chan_ust_created
= 1;
2025 ret
= context_ust_add(usess
, domain
, event_context
,
2027 if (ret
!= LTTNG_OK
) {
2033 ret
= LTTNG_ERR_UND
;
2041 if (chan_kern_created
) {
2042 struct ltt_kernel_channel
*kchan
=
2043 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2044 session
->kernel_session
);
2045 /* Created previously, this should NOT fail. */
2046 LTTNG_ASSERT(kchan
);
2047 kernel_destroy_channel(kchan
);
2050 if (chan_ust_created
) {
2051 struct ltt_ust_channel
*uchan
=
2052 trace_ust_find_channel_by_name(
2053 session
->ust_session
->domain_global
.channels
,
2054 DEFAULT_CHANNEL_NAME
);
2055 /* Created previously, this should NOT fail. */
2056 LTTNG_ASSERT(uchan
);
2057 /* Remove from the channel list of the session. */
2058 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2060 trace_ust_destroy_channel(uchan
);
2066 static inline bool name_starts_with(const char *name
, const char *prefix
)
2068 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2070 return !strncmp(name
, prefix
, max_cmp_len
);
2073 /* Perform userspace-specific event name validation */
2074 static int validate_ust_event_name(const char *name
)
2084 * Check name against all internal UST event component namespaces used
2087 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2088 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2089 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2098 * Internal version of cmd_enable_event() with a supplemental
2099 * "internal_event" flag which is used to enable internal events which should
2100 * be hidden from clients. Such events are used in the agent implementation to
2101 * enable the events through which all "agent" events are funeled.
2103 static int _cmd_enable_event(struct ltt_session
*session
,
2104 const struct lttng_domain
*domain
,
2105 char *channel_name
, struct lttng_event
*event
,
2106 char *filter_expression
,
2107 struct lttng_bytecode
*filter
,
2108 struct lttng_event_exclusion
*exclusion
,
2109 int wpipe
, bool internal_event
)
2111 int ret
= 0, channel_created
= 0;
2112 struct lttng_channel
*attr
= NULL
;
2114 LTTNG_ASSERT(session
);
2115 LTTNG_ASSERT(event
);
2116 LTTNG_ASSERT(channel_name
);
2118 /* If we have a filter, we must have its filter expression */
2119 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2121 /* Normalize event name as a globbing pattern */
2122 strutils_normalize_star_glob_pattern(event
->name
);
2124 /* Normalize exclusion names as globbing patterns */
2128 for (i
= 0; i
< exclusion
->count
; i
++) {
2129 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2131 strutils_normalize_star_glob_pattern(name
);
2135 DBG("Enable event command for event \'%s\'", event
->name
);
2139 switch (domain
->type
) {
2140 case LTTNG_DOMAIN_KERNEL
:
2142 struct ltt_kernel_channel
*kchan
;
2145 * If a non-default channel has been created in the
2146 * session, explicitely require that -c chan_name needs
2149 if (session
->kernel_session
->has_non_default_channel
2150 && channel_name
[0] == '\0') {
2151 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2155 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2156 session
->kernel_session
);
2157 if (kchan
== NULL
) {
2158 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2159 LTTNG_BUFFER_GLOBAL
);
2161 ret
= LTTNG_ERR_FATAL
;
2164 if (lttng_strncpy(attr
->name
, channel_name
,
2165 sizeof(attr
->name
))) {
2166 ret
= LTTNG_ERR_INVALID
;
2170 ret
= cmd_enable_channel_internal(
2171 session
, domain
, attr
, wpipe
);
2172 if (ret
!= LTTNG_OK
) {
2175 channel_created
= 1;
2178 /* Get the newly created kernel channel pointer */
2179 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2180 session
->kernel_session
);
2181 if (kchan
== NULL
) {
2182 /* This sould not happen... */
2183 ret
= LTTNG_ERR_FATAL
;
2187 switch (event
->type
) {
2188 case LTTNG_EVENT_ALL
:
2190 char *filter_expression_a
= NULL
;
2191 struct lttng_bytecode
*filter_a
= NULL
;
2194 * We need to duplicate filter_expression and filter,
2195 * because ownership is passed to first enable
2198 if (filter_expression
) {
2199 filter_expression_a
= strdup(filter_expression
);
2200 if (!filter_expression_a
) {
2201 ret
= LTTNG_ERR_FATAL
;
2206 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2208 free(filter_expression_a
);
2209 ret
= LTTNG_ERR_FATAL
;
2212 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2214 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2215 ret
= event_kernel_enable_event(kchan
, event
,
2216 filter_expression
, filter
);
2217 /* We have passed ownership */
2218 filter_expression
= NULL
;
2220 if (ret
!= LTTNG_OK
) {
2221 if (channel_created
) {
2222 /* Let's not leak a useless channel. */
2223 kernel_destroy_channel(kchan
);
2225 free(filter_expression_a
);
2229 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2230 ret
= event_kernel_enable_event(kchan
, event
,
2231 filter_expression_a
, filter_a
);
2232 /* We have passed ownership */
2233 filter_expression_a
= NULL
;
2235 if (ret
!= LTTNG_OK
) {
2240 case LTTNG_EVENT_PROBE
:
2241 case LTTNG_EVENT_USERSPACE_PROBE
:
2242 case LTTNG_EVENT_FUNCTION
:
2243 case LTTNG_EVENT_FUNCTION_ENTRY
:
2244 case LTTNG_EVENT_TRACEPOINT
:
2245 ret
= event_kernel_enable_event(kchan
, event
,
2246 filter_expression
, filter
);
2247 /* We have passed ownership */
2248 filter_expression
= NULL
;
2250 if (ret
!= LTTNG_OK
) {
2251 if (channel_created
) {
2252 /* Let's not leak a useless channel. */
2253 kernel_destroy_channel(kchan
);
2258 case LTTNG_EVENT_SYSCALL
:
2259 ret
= event_kernel_enable_event(kchan
, event
,
2260 filter_expression
, filter
);
2261 /* We have passed ownership */
2262 filter_expression
= NULL
;
2264 if (ret
!= LTTNG_OK
) {
2269 ret
= LTTNG_ERR_UNK
;
2273 kernel_wait_quiescent();
2276 case LTTNG_DOMAIN_UST
:
2278 struct ltt_ust_channel
*uchan
;
2279 struct ltt_ust_session
*usess
= session
->ust_session
;
2281 LTTNG_ASSERT(usess
);
2284 * If a non-default channel has been created in the
2285 * session, explicitely require that -c chan_name needs
2288 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2289 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2293 /* Get channel from global UST domain */
2294 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2296 if (uchan
== NULL
) {
2297 /* Create default channel */
2298 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2299 usess
->buffer_type
);
2301 ret
= LTTNG_ERR_FATAL
;
2304 if (lttng_strncpy(attr
->name
, channel_name
,
2305 sizeof(attr
->name
))) {
2306 ret
= LTTNG_ERR_INVALID
;
2310 ret
= cmd_enable_channel_internal(
2311 session
, domain
, attr
, wpipe
);
2312 if (ret
!= LTTNG_OK
) {
2316 /* Get the newly created channel reference back */
2317 uchan
= trace_ust_find_channel_by_name(
2318 usess
->domain_global
.channels
, channel_name
);
2319 LTTNG_ASSERT(uchan
);
2322 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2324 * Don't allow users to add UST events to channels which
2325 * are assigned to a userspace subdomain (JUL, Log4J,
2328 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2332 if (!internal_event
) {
2334 * Ensure the event name is not reserved for internal
2337 ret
= validate_ust_event_name(event
->name
);
2339 WARN("Userspace event name %s failed validation.",
2341 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2346 /* At this point, the session and channel exist on the tracer */
2347 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2348 filter_expression
, filter
, exclusion
,
2350 /* We have passed ownership */
2351 filter_expression
= NULL
;
2354 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2355 goto already_enabled
;
2356 } else if (ret
!= LTTNG_OK
) {
2361 case LTTNG_DOMAIN_LOG4J
:
2362 case LTTNG_DOMAIN_JUL
:
2363 case LTTNG_DOMAIN_PYTHON
:
2365 const char *default_event_name
, *default_chan_name
;
2367 struct lttng_event uevent
;
2368 struct lttng_domain tmp_dom
;
2369 struct ltt_ust_session
*usess
= session
->ust_session
;
2371 LTTNG_ASSERT(usess
);
2373 if (!agent_tracing_is_enabled()) {
2374 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2375 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2379 agt
= trace_ust_find_agent(usess
, domain
->type
);
2381 agt
= agent_create(domain
->type
);
2383 ret
= LTTNG_ERR_NOMEM
;
2386 agent_add(agt
, usess
->agents
);
2389 /* Create the default tracepoint. */
2390 memset(&uevent
, 0, sizeof(uevent
));
2391 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2392 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2393 default_event_name
= event_get_default_agent_ust_name(
2395 if (!default_event_name
) {
2396 ret
= LTTNG_ERR_FATAL
;
2399 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2400 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2403 * The domain type is changed because we are about to enable the
2404 * default channel and event for the JUL domain that are hardcoded.
2405 * This happens in the UST domain.
2407 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2408 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2410 switch (domain
->type
) {
2411 case LTTNG_DOMAIN_LOG4J
:
2412 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2414 case LTTNG_DOMAIN_JUL
:
2415 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2417 case LTTNG_DOMAIN_PYTHON
:
2418 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2421 /* The switch/case we are in makes this impossible */
2426 char *filter_expression_copy
= NULL
;
2427 struct lttng_bytecode
*filter_copy
= NULL
;
2430 const size_t filter_size
= sizeof(
2431 struct lttng_bytecode
)
2434 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2436 ret
= LTTNG_ERR_NOMEM
;
2439 memcpy(filter_copy
, filter
, filter_size
);
2441 filter_expression_copy
=
2442 strdup(filter_expression
);
2443 if (!filter_expression
) {
2444 ret
= LTTNG_ERR_NOMEM
;
2447 if (!filter_expression_copy
|| !filter_copy
) {
2448 free(filter_expression_copy
);
2454 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2455 (char *) default_chan_name
,
2456 &uevent
, filter_expression_copy
,
2457 filter_copy
, NULL
, wpipe
);
2460 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2461 goto already_enabled
;
2462 } else if (ret
!= LTTNG_OK
) {
2466 /* The wild card * means that everything should be enabled. */
2467 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2468 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2471 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2475 filter_expression
= NULL
;
2476 if (ret
!= LTTNG_OK
) {
2483 ret
= LTTNG_ERR_UND
;
2491 free(filter_expression
);
2494 channel_attr_destroy(attr
);
2500 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2501 * We own filter, exclusion, and filter_expression.
2503 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2504 struct lttng_event
*event
,
2505 char *filter_expression
,
2506 struct lttng_event_exclusion
*exclusion
,
2507 struct lttng_bytecode
*bytecode
,
2512 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2514 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2517 * The ownership of the following parameters is transferred to
2518 * _cmd_enable_event:
2520 * - filter_expression,
2524 ret
= _cmd_enable_event(cmd_ctx
->session
,
2526 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2527 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2528 filter_expression
= NULL
;
2535 * Enable an event which is internal to LTTng. An internal should
2536 * never be made visible to clients and are immune to checks such as
2539 static int cmd_enable_event_internal(struct ltt_session
*session
,
2540 const struct lttng_domain
*domain
,
2541 char *channel_name
, struct lttng_event
*event
,
2542 char *filter_expression
,
2543 struct lttng_bytecode
*filter
,
2544 struct lttng_event_exclusion
*exclusion
,
2547 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2548 filter_expression
, filter
, exclusion
, wpipe
, true);
2552 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2554 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2555 struct lttng_payload
*reply_payload
)
2557 enum lttng_error_code ret_code
;
2559 ssize_t i
, nb_events
= 0;
2560 struct lttng_event
*events
= NULL
;
2561 struct lttcomm_list_command_header reply_command_header
= {};
2562 size_t reply_command_header_offset
;
2564 assert(reply_payload
);
2566 /* Reserve space for command reply header. */
2567 reply_command_header_offset
= reply_payload
->buffer
.size
;
2568 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2569 reply_command_header_offset
+
2570 sizeof(struct lttcomm_list_command_header
));
2572 ret_code
= LTTNG_ERR_NOMEM
;
2577 case LTTNG_DOMAIN_KERNEL
:
2578 nb_events
= kernel_list_events(&events
);
2579 if (nb_events
< 0) {
2580 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2584 case LTTNG_DOMAIN_UST
:
2585 nb_events
= ust_app_list_events(&events
);
2586 if (nb_events
< 0) {
2587 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2591 case LTTNG_DOMAIN_LOG4J
:
2592 case LTTNG_DOMAIN_JUL
:
2593 case LTTNG_DOMAIN_PYTHON
:
2594 nb_events
= agent_list_events(&events
, domain
);
2595 if (nb_events
< 0) {
2596 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2601 ret_code
= LTTNG_ERR_UND
;
2605 for (i
= 0; i
< nb_events
; i
++) {
2606 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2609 ret_code
= LTTNG_ERR_NOMEM
;
2614 if (nb_events
> UINT32_MAX
) {
2615 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2616 ret_code
= LTTNG_ERR_OVERFLOW
;
2620 /* Update command reply header. */
2621 reply_command_header
.count
= (uint32_t) nb_events
;
2622 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2623 sizeof(reply_command_header
));
2625 ret_code
= LTTNG_OK
;
2632 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2634 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2635 struct lttng_payload
*reply
)
2637 enum lttng_error_code ret_code
;
2639 unsigned int i
, nb_fields
;
2640 struct lttng_event_field
*fields
= NULL
;
2641 struct lttcomm_list_command_header reply_command_header
= {};
2642 size_t reply_command_header_offset
;
2646 /* Reserve space for command reply header. */
2647 reply_command_header_offset
= reply
->buffer
.size
;
2648 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2649 reply_command_header_offset
+
2650 sizeof(struct lttcomm_list_command_header
));
2652 ret_code
= LTTNG_ERR_NOMEM
;
2657 case LTTNG_DOMAIN_UST
:
2658 ret
= ust_app_list_event_fields(&fields
);
2660 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2665 case LTTNG_DOMAIN_KERNEL
:
2666 default: /* fall-through */
2667 ret_code
= LTTNG_ERR_UND
;
2673 for (i
= 0; i
< nb_fields
; i
++) {
2674 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2676 ret_code
= LTTNG_ERR_NOMEM
;
2681 if (nb_fields
> UINT32_MAX
) {
2682 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2683 ret_code
= LTTNG_ERR_OVERFLOW
;
2687 /* Update command reply header. */
2688 reply_command_header
.count
= (uint32_t) nb_fields
;
2690 memcpy(reply
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2691 sizeof(reply_command_header
));
2693 ret_code
= LTTNG_OK
;
2700 enum lttng_error_code
cmd_list_syscalls(
2701 struct lttng_payload
*reply_payload
)
2703 enum lttng_error_code ret_code
;
2704 ssize_t nb_events
, i
;
2706 struct lttng_event
*events
= NULL
;
2707 struct lttcomm_list_command_header reply_command_header
= {};
2708 size_t reply_command_header_offset
;
2710 assert(reply_payload
);
2712 /* Reserve space for command reply header. */
2713 reply_command_header_offset
= reply_payload
->buffer
.size
;
2714 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2715 reply_command_header_offset
+
2716 sizeof(struct lttcomm_list_command_header
));
2718 ret_code
= LTTNG_ERR_NOMEM
;
2722 nb_events
= syscall_table_list(&events
);
2723 if (nb_events
< 0) {
2724 ret_code
= (enum lttng_error_code
) -nb_events
;
2728 for (i
= 0; i
< nb_events
; i
++) {
2729 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2732 ret_code
= LTTNG_ERR_NOMEM
;
2737 if (nb_events
> UINT32_MAX
) {
2738 ERR("Syscall count would overflow the syscall listing command's reply");
2739 ret_code
= LTTNG_ERR_OVERFLOW
;
2743 /* Update command reply header. */
2744 reply_command_header
.count
= (uint32_t) nb_events
;
2745 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2746 sizeof(reply_command_header
));
2748 ret_code
= LTTNG_OK
;
2755 * Command LTTNG_START_TRACE processed by the client thread.
2757 * Called with session mutex held.
2759 int cmd_start_trace(struct ltt_session
*session
)
2761 enum lttng_error_code ret
;
2762 unsigned long nb_chan
= 0;
2763 struct ltt_kernel_session
*ksession
;
2764 struct ltt_ust_session
*usess
;
2765 const bool session_rotated_after_last_stop
=
2766 session
->rotated_after_last_stop
;
2767 const bool session_cleared_after_last_stop
=
2768 session
->cleared_after_last_stop
;
2770 LTTNG_ASSERT(session
);
2772 /* Ease our life a bit ;) */
2773 ksession
= session
->kernel_session
;
2774 usess
= session
->ust_session
;
2776 /* Is the session already started? */
2777 if (session
->active
) {
2778 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2779 /* Perform nothing */
2783 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2784 !session
->current_trace_chunk
) {
2786 * A rotation was launched while the session was stopped and
2787 * it has not been completed yet. It is not possible to start
2788 * the session since starting the session here would require a
2789 * rotation from "NULL" to a new trace chunk. That rotation
2790 * would overlap with the ongoing rotation, which is not
2793 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2795 ret
= LTTNG_ERR_ROTATION_PENDING
;
2800 * Starting a session without channel is useless since after that it's not
2801 * possible to enable channel thus inform the client.
2803 if (usess
&& usess
->domain_global
.channels
) {
2804 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2807 nb_chan
+= ksession
->channel_count
;
2810 ret
= LTTNG_ERR_NO_CHANNEL
;
2814 session
->active
= true;
2815 session
->rotated_after_last_stop
= false;
2816 session
->cleared_after_last_stop
= false;
2817 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2818 if (!session
->has_been_started
) {
2819 struct lttng_trace_chunk
*trace_chunk
;
2821 DBG("Creating initial trace chunk of session \"%s\"",
2823 trace_chunk
= session_create_new_trace_chunk(
2824 session
, NULL
, NULL
, NULL
);
2826 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2829 LTTNG_ASSERT(!session
->current_trace_chunk
);
2830 ret
= (lttng_error_code
) session_set_trace_chunk(session
, trace_chunk
,
2832 lttng_trace_chunk_put(trace_chunk
);
2834 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2838 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2841 * Rotate existing streams into the new chunk.
2842 * This is a "quiet" rotation has no client has
2843 * explicitly requested this operation.
2845 * There is also no need to wait for the rotation
2846 * to complete as it will happen immediately. No data
2847 * was produced as the session was stopped, so the
2848 * rotation should happen on reception of the command.
2850 ret
= (lttng_error_code
) cmd_rotate_session(session
, NULL
, true,
2851 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2852 if (ret
!= LTTNG_OK
) {
2858 /* Kernel tracing */
2859 if (ksession
!= NULL
) {
2860 DBG("Start kernel tracing session %s", session
->name
);
2861 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2862 if (ret
!= LTTNG_OK
) {
2867 /* Flag session that trace should start automatically */
2869 int int_ret
= ust_app_start_trace_all(usess
);
2872 ret
= LTTNG_ERR_UST_START_FAIL
;
2878 * Open a packet in every stream of the session to ensure that viewers
2879 * can correctly identify the boundaries of the periods during which
2880 * tracing was active for this session.
2882 ret
= session_open_packets(session
);
2883 if (ret
!= LTTNG_OK
) {
2888 * Clear the flag that indicates that a rotation was done while the
2889 * session was stopped.
2891 session
->rotated_after_last_stop
= false;
2893 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2894 int int_ret
= timer_session_rotation_schedule_timer_start(
2895 session
, session
->rotate_timer_period
);
2898 ERR("Failed to enable rotate timer");
2899 ret
= LTTNG_ERR_UNK
;
2907 if (ret
== LTTNG_OK
) {
2908 /* Flag this after a successful start. */
2909 session
->has_been_started
= true;
2911 session
->active
= false;
2912 /* Restore initial state on error. */
2913 session
->rotated_after_last_stop
=
2914 session_rotated_after_last_stop
;
2915 session
->cleared_after_last_stop
=
2916 session_cleared_after_last_stop
;
2923 * Command LTTNG_STOP_TRACE processed by the client thread.
2925 int cmd_stop_trace(struct ltt_session
*session
)
2928 struct ltt_kernel_session
*ksession
;
2929 struct ltt_ust_session
*usess
;
2931 LTTNG_ASSERT(session
);
2933 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2935 ksession
= session
->kernel_session
;
2936 usess
= session
->ust_session
;
2938 /* Session is not active. Skip everything and inform the client. */
2939 if (!session
->active
) {
2940 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2944 ret
= stop_kernel_session(ksession
);
2945 if (ret
!= LTTNG_OK
) {
2949 if (usess
&& usess
->active
) {
2950 ret
= ust_app_stop_trace_all(usess
);
2952 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2957 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2959 /* Flag inactive after a successful stop. */
2960 session
->active
= 0;
2968 * Set the base_path of the session only if subdir of a control uris is set.
2969 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2971 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2973 struct lttng_uri
*uris
)
2978 for (i
= 0; i
< nb_uri
; i
++) {
2979 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2980 uris
[i
].subdir
[0] == '\0') {
2981 /* Not interested in these URIs */
2985 if (session
->base_path
!= NULL
) {
2986 free(session
->base_path
);
2987 session
->base_path
= NULL
;
2990 /* Set session base_path */
2991 session
->base_path
= strdup(uris
[i
].subdir
);
2992 if (!session
->base_path
) {
2993 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2994 uris
[i
].subdir
, session
->name
);
2995 ret
= LTTNG_ERR_NOMEM
;
2998 DBG2("Setting base path \"%s\" for session \"%s\"",
2999 session
->base_path
, session
->name
);
3007 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
3009 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
3010 struct lttng_uri
*uris
)
3013 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3014 struct ltt_ust_session
*usess
= session
->ust_session
;
3016 LTTNG_ASSERT(session
);
3018 LTTNG_ASSERT(nb_uri
> 0);
3020 /* Can't set consumer URI if the session is active. */
3021 if (session
->active
) {
3022 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
3027 * Set the session base path if any. This is done inside
3028 * cmd_set_consumer_uri to preserve backward compatibility of the
3029 * previous session creation api vs the session descriptor api.
3031 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
3032 if (ret
!= LTTNG_OK
) {
3036 /* Set the "global" consumer URIs */
3037 for (i
= 0; i
< nb_uri
; i
++) {
3038 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
3040 if (ret
!= LTTNG_OK
) {
3045 /* Set UST session URIs */
3046 if (session
->ust_session
) {
3047 for (i
= 0; i
< nb_uri
; i
++) {
3048 ret
= add_uri_to_consumer(session
,
3049 session
->ust_session
->consumer
,
3050 &uris
[i
], LTTNG_DOMAIN_UST
);
3051 if (ret
!= LTTNG_OK
) {
3057 /* Set kernel session URIs */
3058 if (session
->kernel_session
) {
3059 for (i
= 0; i
< nb_uri
; i
++) {
3060 ret
= add_uri_to_consumer(session
,
3061 session
->kernel_session
->consumer
,
3062 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3063 if (ret
!= LTTNG_OK
) {
3070 * Make sure to set the session in output mode after we set URI since a
3071 * session can be created without URL (thus flagged in no output mode).
3073 session
->output_traces
= 1;
3075 ksess
->output_traces
= 1;
3079 usess
->output_traces
= 1;
3090 enum lttng_error_code
set_session_output_from_descriptor(
3091 struct ltt_session
*session
,
3092 const struct lttng_session_descriptor
*descriptor
)
3095 enum lttng_error_code ret_code
= LTTNG_OK
;
3096 enum lttng_session_descriptor_type session_type
=
3097 lttng_session_descriptor_get_type(descriptor
);
3098 enum lttng_session_descriptor_output_type output_type
=
3099 lttng_session_descriptor_get_output_type(descriptor
);
3100 struct lttng_uri uris
[2] = {};
3101 size_t uri_count
= 0;
3103 switch (output_type
) {
3104 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3106 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3107 lttng_session_descriptor_get_local_output_uri(descriptor
,
3111 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3112 lttng_session_descriptor_get_network_output_uris(descriptor
,
3113 &uris
[0], &uris
[1]);
3117 ret_code
= LTTNG_ERR_INVALID
;
3121 switch (session_type
) {
3122 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3124 struct snapshot_output
*new_output
= NULL
;
3126 new_output
= snapshot_output_alloc();
3128 ret_code
= LTTNG_ERR_NOMEM
;
3132 ret
= snapshot_output_init_with_uri(session
,
3133 DEFAULT_SNAPSHOT_MAX_SIZE
,
3134 NULL
, uris
, uri_count
, session
->consumer
,
3135 new_output
, &session
->snapshot
);
3137 ret_code
= (ret
== -ENOMEM
) ?
3138 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3139 snapshot_output_destroy(new_output
);
3142 snapshot_add_output(&session
->snapshot
, new_output
);
3145 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3146 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3148 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3152 ret_code
= LTTNG_ERR_INVALID
;
3160 enum lttng_error_code
cmd_create_session_from_descriptor(
3161 struct lttng_session_descriptor
*descriptor
,
3162 const lttng_sock_cred
*creds
,
3163 const char *home_path
)
3166 enum lttng_error_code ret_code
;
3167 const char *session_name
;
3168 struct ltt_session
*new_session
= NULL
;
3169 enum lttng_session_descriptor_status descriptor_status
;
3170 const lttng_trace_format_descriptor
*trace_format_descriptor
= NULL
;
3171 lttng::trace_format_descriptor::uptr trace_format_descriptor_ptr
;
3173 session_lock_list();
3175 if (*home_path
!= '/') {
3176 ERR("Home path provided by client is not absolute");
3177 ret_code
= LTTNG_ERR_INVALID
;
3182 descriptor_status
= lttng_session_descriptor_get_session_name(
3183 descriptor
, &session_name
);
3184 switch (descriptor_status
) {
3185 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3187 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3188 session_name
= NULL
;
3191 ret_code
= LTTNG_ERR_INVALID
;
3195 descriptor_status
= lttng_session_descriptor_get_trace_format_descriptor(
3196 descriptor
, &trace_format_descriptor
);
3197 if (descriptor_status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
3198 ret_code
= LTTNG_ERR_INVALID
;
3203 trace_format_descriptor_ptr
=
3204 reinterpret_cast<const lttng::trace_format_descriptor
*>(
3205 trace_format_descriptor
)
3207 } catch (std::exception
& e
) {
3208 ERR("%s", e
.what());
3209 ret_code
= LTTNG_ERR_UNK
;
3213 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, trace_format_descriptor_ptr
,
3215 if (ret_code
!= LTTNG_OK
) {
3219 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3220 new_session
->id
, new_session
->name
, new_session
->uid
, new_session
->gid
);
3221 if (ret_code
!= LTTNG_OK
) {
3225 /* Announce the session's destruction to the notification thread when it is destroyed. */
3226 ret
= session_add_destroy_notifier(
3228 [](const struct ltt_session
*session
,
3229 void *user_data
__attribute__((unused
))) {
3230 (void) notification_thread_command_remove_session(
3231 the_notification_thread_handle
, session
->id
);
3235 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3237 ret
= LTTNG_ERR_NOMEM
;
3241 if (!session_name
) {
3242 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3245 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3250 if (!lttng_session_descriptor_is_output_destination_initialized(
3253 * Only include the session's creation time in the output
3254 * destination if the name of the session itself was
3255 * not auto-generated.
3257 ret_code
= lttng_session_descriptor_set_default_output(
3259 session_name
? &new_session
->creation_time
: NULL
,
3261 if (ret_code
!= LTTNG_OK
) {
3265 new_session
->has_user_specified_directory
=
3266 lttng_session_descriptor_has_output_directory(
3270 switch (lttng_session_descriptor_get_type(descriptor
)) {
3271 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3272 new_session
->snapshot_mode
= 1;
3274 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3275 new_session
->live_timer
=
3276 lttng_session_descriptor_live_get_timer_interval(
3283 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3284 if (ret_code
!= LTTNG_OK
) {
3287 new_session
->consumer
->enabled
= 1;
3288 ret_code
= LTTNG_OK
;
3290 /* Release reference provided by the session_create function. */
3291 session_put(new_session
);
3292 if (ret_code
!= LTTNG_OK
&& new_session
) {
3293 /* Release the global reference on error. */
3294 session_destroy(new_session
);
3296 session_unlock_list();
3300 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3301 struct lttng_session_descriptor
**return_descriptor
)
3304 size_t payload_size
;
3305 struct lttng_dynamic_buffer payload
;
3306 struct lttng_buffer_view home_dir_view
;
3307 struct lttng_buffer_view session_descriptor_view
;
3308 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3309 enum lttng_error_code ret_code
;
3311 lttng_dynamic_buffer_init(&payload
);
3312 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3314 ret_code
= LTTNG_ERR_INVALID
;
3317 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3318 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3319 ret_code
= LTTNG_ERR_INVALID
;
3323 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3324 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3325 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3327 ret_code
= LTTNG_ERR_NOMEM
;
3331 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3333 ERR("Reception of session descriptor failed, aborting.");
3334 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3338 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3341 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3342 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3343 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3344 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3345 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3349 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3351 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3352 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3353 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3354 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3355 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3359 ret
= lttng_session_descriptor_create_from_buffer(
3360 &session_descriptor_view
, &session_descriptor
);
3362 ERR("Failed to create session descriptor from payload of \"create session\" command");
3363 ret_code
= LTTNG_ERR_INVALID
;
3368 * Sets the descriptor's auto-generated properties (name, output) if
3371 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3373 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3374 if (ret_code
!= LTTNG_OK
) {
3378 ret_code
= LTTNG_OK
;
3379 *return_descriptor
= session_descriptor
;
3380 session_descriptor
= NULL
;
3382 lttng_dynamic_buffer_reset(&payload
);
3383 lttng_session_descriptor_destroy(session_descriptor
);
3388 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3389 void *_reply_context
)
3393 const struct cmd_destroy_session_reply_context
*reply_context
=
3394 (cmd_destroy_session_reply_context
*) _reply_context
;
3395 struct lttng_dynamic_buffer payload
;
3396 struct lttcomm_session_destroy_command_header cmd_header
;
3397 struct lttng_trace_archive_location
*location
= NULL
;
3398 struct lttcomm_lttng_msg llm
= {
3399 .cmd_type
= LTTNG_DESTROY_SESSION
,
3400 .ret_code
= reply_context
->destruction_status
,
3403 sizeof(struct lttcomm_session_destroy_command_header
),
3407 size_t payload_size_before_location
;
3409 lttng_dynamic_buffer_init(&payload
);
3411 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3413 ERR("Failed to append session destruction message");
3417 cmd_header
.rotation_state
=
3418 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3419 session
->rotation_state
:
3420 LTTNG_ROTATION_STATE_NO_ROTATION
);
3421 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3422 sizeof(cmd_header
));
3424 ERR("Failed to append session destruction command header");
3428 if (!reply_context
->implicit_rotation_on_destroy
) {
3429 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3433 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3434 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3439 location
= session_get_trace_archive_location(session
);
3441 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3446 payload_size_before_location
= payload
.size
;
3447 comm_ret
= lttng_trace_archive_location_serialize(location
,
3449 lttng_trace_archive_location_put(location
);
3451 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3455 /* Update the message to indicate the location's length. */
3456 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3457 payload
.size
- payload_size_before_location
;
3459 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3460 payload
.data
, payload
.size
);
3461 if (comm_ret
!= (ssize_t
) payload
.size
) {
3462 ERR("Failed to send result of the destruction of session \"%s\" to client",
3466 ret
= close(reply_context
->reply_sock_fd
);
3468 PERROR("Failed to close client socket in deferred session destroy reply");
3470 lttng_dynamic_buffer_reset(&payload
);
3471 free(_reply_context
);
3475 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3477 * Called with session lock held.
3479 int cmd_destroy_session(struct ltt_session
*session
,
3480 struct notification_thread_handle
*notification_thread_handle
,
3484 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3485 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3488 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3489 if (!reply_context
) {
3490 ret
= LTTNG_ERR_NOMEM
;
3494 reply_context
->reply_sock_fd
= *sock_fd
;
3498 LTTNG_ASSERT(session
);
3500 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3502 if (session
->active
) {
3503 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3505 ret
= cmd_stop_trace(session
);
3506 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3507 /* Carry on with the destruction of the session. */
3508 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3509 session
->name
, lttng_strerror(-ret
));
3510 destruction_last_error
= (lttng_error_code
) ret
;
3514 if (session
->rotation_schedule_timer_enabled
) {
3515 if (timer_session_rotation_schedule_timer_stop(
3517 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3519 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3523 if (session
->rotate_size
) {
3524 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3525 session
->rotate_size
= 0;
3528 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3530 * Perform a last rotation on destruction if rotations have
3531 * occurred during the session's lifetime.
3533 ret
= cmd_rotate_session(session
, NULL
, false,
3534 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3535 if (ret
!= LTTNG_OK
) {
3536 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3537 session
->name
, lttng_strerror(-ret
));
3538 destruction_last_error
= (lttng_error_code
) -ret
;
3540 if (reply_context
) {
3541 reply_context
->implicit_rotation_on_destroy
= true;
3543 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3545 * The user has not triggered a session rotation. However, to
3546 * ensure all data has been consumed, the session is rotated
3547 * to a 'null' trace chunk before it is destroyed.
3549 * This is a "quiet" rotation meaning that no notification is
3550 * emitted and no renaming of the current trace chunk takes
3553 ret
= cmd_rotate_session(session
, NULL
, true,
3554 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3556 * Rotation operations may not be supported by the kernel
3557 * tracer. Hence, do not consider this implicit rotation as
3558 * a session destruction error. The library has already stopped
3559 * the session and waited for pending data; there is nothing
3560 * left to do but complete the destruction of the session.
3562 if (ret
!= LTTNG_OK
&&
3563 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3564 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3565 session
->name
, lttng_strerror(ret
));
3566 destruction_last_error
= (lttng_error_code
) -ret
;
3570 if (session
->shm_path
[0]) {
3572 * When a session is created with an explicit shm_path,
3573 * the consumer daemon will create its shared memory files
3574 * at that location and will *not* unlink them. This is normal
3575 * as the intention of that feature is to make it possible
3576 * to retrieve the content of those files should a crash occur.
3578 * To ensure the content of those files can be used, the
3579 * sessiond daemon will replicate the content of the metadata
3580 * cache in a metadata file.
3582 * On clean-up, it is expected that the consumer daemon will
3583 * unlink the shared memory files and that the session daemon
3584 * will unlink the metadata file. Then, the session's directory
3585 * in the shm path can be removed.
3587 * Unfortunately, a flaw in the design of the sessiond's and
3588 * consumerd's tear down of channels makes it impossible to
3589 * determine when the sessiond _and_ the consumerd have both
3590 * destroyed their representation of a channel. For one, the
3591 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3592 * callbacks in both daemons.
3594 * However, it is also impossible for the sessiond to know when
3595 * the consumer daemon is done destroying its channel(s) since
3596 * it occurs as a reaction to the closing of the channel's file
3597 * descriptor. There is no resulting communication initiated
3598 * from the consumerd to the sessiond to confirm that the
3599 * operation is completed (and was successful).
3601 * Until this is all fixed, the session daemon checks for the
3602 * removal of the session's shm path which makes it possible
3603 * to safely advertise a session as having been destroyed.
3605 * Prior to this fix, it was not possible to reliably save
3606 * a session making use of the --shm-path option, destroy it,
3607 * and load it again. This is because the creation of the
3608 * session would fail upon seeing the session's shm path
3609 * already in existence.
3611 * Note that none of the error paths in the check for the
3612 * directory's existence return an error. This is normal
3613 * as there isn't much that can be done. The session will
3614 * be destroyed properly, except that we can't offer the
3615 * guarantee that the same session can be re-created.
3617 current_completion_handler
= &destroy_completion_handler
.handler
;
3618 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3620 sizeof(destroy_completion_handler
.shm_path
));
3625 * The session is destroyed. However, note that the command context
3626 * still holds a reference to the session, thus delaying its destruction
3627 * _at least_ up to the point when that reference is released.
3629 session_destroy(session
);
3630 if (reply_context
) {
3631 reply_context
->destruction_status
= destruction_last_error
;
3632 ret
= session_add_destroy_notifier(session
,
3633 cmd_destroy_session_reply
,
3634 (void *) reply_context
);
3636 ret
= LTTNG_ERR_FATAL
;
3648 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3650 int cmd_register_consumer(struct ltt_session
*session
,
3651 enum lttng_domain_type domain
, const char *sock_path
,
3652 struct consumer_data
*cdata
)
3655 struct consumer_socket
*socket
= NULL
;
3657 LTTNG_ASSERT(session
);
3658 LTTNG_ASSERT(cdata
);
3659 LTTNG_ASSERT(sock_path
);
3662 case LTTNG_DOMAIN_KERNEL
:
3664 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3666 LTTNG_ASSERT(ksess
);
3668 /* Can't register a consumer if there is already one */
3669 if (ksess
->consumer_fds_sent
!= 0) {
3670 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3674 sock
= lttcomm_connect_unix_sock(sock_path
);
3676 ret
= LTTNG_ERR_CONNECT_FAIL
;
3679 cdata
->cmd_sock
= sock
;
3681 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3682 if (socket
== NULL
) {
3685 PERROR("close register consumer");
3687 cdata
->cmd_sock
= -1;
3688 ret
= LTTNG_ERR_FATAL
;
3692 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3693 if (socket
->lock
== NULL
) {
3694 PERROR("zmalloc pthread mutex");
3695 ret
= LTTNG_ERR_FATAL
;
3699 pthread_mutex_init(socket
->lock
, NULL
);
3700 socket
->registered
= 1;
3703 consumer_add_socket(socket
, ksess
->consumer
);
3706 pthread_mutex_lock(&cdata
->pid_mutex
);
3708 pthread_mutex_unlock(&cdata
->pid_mutex
);
3713 /* TODO: Userspace tracing */
3714 ret
= LTTNG_ERR_UND
;
3722 consumer_destroy_socket(socket
);
3728 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3730 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3731 struct lttng_domain
**domains
)
3736 struct lttng_ht_iter iter
;
3738 if (session
->kernel_session
!= NULL
) {
3739 DBG3("Listing domains found kernel domain");
3743 if (session
->ust_session
!= NULL
) {
3744 DBG3("Listing domains found UST global domain");
3748 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3750 if (agt
->being_used
) {
3761 *domains
= calloc
<lttng_domain
>(nb_dom
);
3762 if (*domains
== NULL
) {
3763 ret
= LTTNG_ERR_FATAL
;
3767 if (session
->kernel_session
!= NULL
) {
3768 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3770 /* Kernel session buffer type is always GLOBAL */
3771 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3776 if (session
->ust_session
!= NULL
) {
3777 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3778 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3782 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3784 if (agt
->being_used
) {
3785 (*domains
)[index
].type
= agt
->domain
;
3786 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3796 /* Return negative value to differentiate return code */
3802 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3804 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3805 struct ltt_session
*session
,
3806 struct lttng_payload
*payload
)
3810 struct lttcomm_list_command_header cmd_header
= {};
3811 size_t cmd_header_offset
;
3812 enum lttng_error_code ret_code
;
3817 DBG("Listing channels for session %s", session
->name
);
3819 cmd_header_offset
= payload
->buffer
.size
;
3821 /* Reserve space for command reply header. */
3822 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3823 cmd_header_offset
+ sizeof(cmd_header
));
3825 ret_code
= LTTNG_ERR_NOMEM
;
3830 case LTTNG_DOMAIN_KERNEL
:
3832 /* Kernel channels */
3833 struct ltt_kernel_channel
*kchan
;
3834 if (session
->kernel_session
!= NULL
) {
3835 cds_list_for_each_entry(kchan
,
3836 &session
->kernel_session
->channel_list
.head
, list
) {
3837 uint64_t discarded_events
, lost_packets
;
3838 struct lttng_channel_extended
*extended
;
3840 extended
= (struct lttng_channel_extended
*)
3841 kchan
->channel
->attr
.extended
.ptr
;
3843 ret
= get_kernel_runtime_stats(session
, kchan
,
3844 &discarded_events
, &lost_packets
);
3846 ret_code
= LTTNG_ERR_UNK
;
3851 * Update the discarded_events and lost_packets
3852 * count for the channel
3854 extended
->discarded_events
= discarded_events
;
3855 extended
->lost_packets
= lost_packets
;
3857 ret
= lttng_channel_serialize(
3858 kchan
->channel
, &payload
->buffer
);
3860 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3861 kchan
->channel
->name
);
3862 ret_code
= LTTNG_ERR_UNK
;
3871 case LTTNG_DOMAIN_UST
:
3873 struct lttng_ht_iter iter
;
3874 struct ltt_ust_channel
*uchan
;
3877 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3878 &iter
.iter
, uchan
, node
.node
) {
3879 uint64_t discarded_events
= 0, lost_packets
= 0;
3880 struct lttng_channel
*channel
= NULL
;
3881 struct lttng_channel_extended
*extended
;
3883 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3885 ret_code
= LTTNG_ERR_NOMEM
;
3889 extended
= (struct lttng_channel_extended
*)
3890 channel
->attr
.extended
.ptr
;
3892 ret
= get_ust_runtime_stats(session
, uchan
,
3893 &discarded_events
, &lost_packets
);
3895 lttng_channel_destroy(channel
);
3896 ret_code
= LTTNG_ERR_UNK
;
3900 extended
->discarded_events
= discarded_events
;
3901 extended
->lost_packets
= lost_packets
;
3903 ret
= lttng_channel_serialize(
3904 channel
, &payload
->buffer
);
3906 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3908 lttng_channel_destroy(channel
);
3909 ret_code
= LTTNG_ERR_UNK
;
3913 lttng_channel_destroy(channel
);
3923 if (i
> UINT32_MAX
) {
3924 ERR("Channel count would overflow the channel listing command's reply");
3925 ret_code
= LTTNG_ERR_OVERFLOW
;
3929 /* Update command reply header. */
3930 cmd_header
.count
= (uint32_t) i
;
3931 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3932 sizeof(cmd_header
));
3933 ret_code
= LTTNG_OK
;
3940 * Command LTTNG_LIST_EVENTS processed by the client thread.
3942 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3943 struct ltt_session
*session
,
3945 struct lttng_payload
*reply_payload
)
3947 int buffer_resize_ret
;
3948 enum lttng_error_code ret_code
= LTTNG_OK
;
3949 struct lttcomm_list_command_header reply_command_header
= {};
3950 size_t reply_command_header_offset
;
3951 unsigned int nb_events
= 0;
3953 assert(reply_payload
);
3955 /* Reserve space for command reply header. */
3956 reply_command_header_offset
= reply_payload
->buffer
.size
;
3957 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3958 reply_command_header_offset
+
3959 sizeof(struct lttcomm_list_command_header
));
3960 if (buffer_resize_ret
) {
3961 ret_code
= LTTNG_ERR_NOMEM
;
3966 case LTTNG_DOMAIN_KERNEL
:
3967 if (session
->kernel_session
!= NULL
) {
3968 ret_code
= list_lttng_kernel_events(channel_name
,
3969 session
->kernel_session
, reply_payload
, &nb_events
);
3973 case LTTNG_DOMAIN_UST
:
3975 if (session
->ust_session
!= NULL
) {
3976 ret_code
= list_lttng_ust_global_events(channel_name
,
3977 &session
->ust_session
->domain_global
,
3978 reply_payload
, &nb_events
);
3983 case LTTNG_DOMAIN_LOG4J
:
3984 case LTTNG_DOMAIN_JUL
:
3985 case LTTNG_DOMAIN_PYTHON
:
3986 if (session
->ust_session
) {
3987 struct lttng_ht_iter iter
;
3991 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3992 &iter
.iter
, agt
, node
.node
) {
3993 if (agt
->domain
== domain
) {
3994 ret_code
= list_lttng_agent_events(
3995 agt
, reply_payload
, &nb_events
);
4004 ret_code
= LTTNG_ERR_UND
;
4008 if (nb_events
> UINT32_MAX
) {
4009 ret_code
= LTTNG_ERR_OVERFLOW
;
4013 /* Update command reply header. */
4014 reply_command_header
.count
= (uint32_t) nb_events
;
4015 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
4016 sizeof(reply_command_header
));
4023 * Using the session list, filled a lttng_session array to send back to the
4024 * client for session listing.
4026 * The session list lock MUST be acquired before calling this function. Use
4027 * session_lock_list() and session_unlock_list().
4029 enum lttng_error_code
cmd_list_lttng_sessions(
4030 struct lttng_payload
*reply_payload
, uid_t uid
, gid_t gid
)
4032 int buffer_resize_ret
;
4033 enum lttng_error_code ret_code
= LTTNG_OK
;
4034 struct lttcomm_list_command_header reply_command_header
= {};
4035 size_t reply_command_header_offset
;
4036 struct ltt_session
*session
;
4037 struct ltt_session_list
*list
= session_get_list();
4041 assert(reply_payload
);
4043 /* Reserve space for command reply header. */
4044 reply_command_header_offset
= reply_payload
->buffer
.size
;
4045 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
4046 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
4047 if (buffer_resize_ret
) {
4048 ret_code
= LTTNG_ERR_NOMEM
;
4052 DBG("Getting all available session for UID %d GID %d",
4055 cds_list_for_each_entry(session
, &list
->head
, list
) {
4056 struct lttng_session tmp_session
= {};
4057 struct lttng_session_extended tmp_extended
= {};
4059 tmp_session
.extended
.ptr
= &tmp_extended
;
4061 if (!session_get(session
)) {
4065 * Only list the sessions the user can control.
4067 if (!session_access_ok(session
, uid
) ||
4068 session
->destroyed
) {
4069 session_put(session
);
4073 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4074 struct ltt_ust_session
*usess
= session
->ust_session
;
4076 if (session
->consumer
->type
== CONSUMER_DST_NET
||
4077 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
4078 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
4079 ret
= build_network_session_path(
4080 tmp_session
.path
, sizeof(tmp_session
.path
), session
);
4082 ret
= snprintf(tmp_session
.path
, sizeof(tmp_session
.path
), "%s",
4083 session
->consumer
->dst
.session_root_path
);
4086 PERROR("snprintf session path");
4087 session_put(session
);
4091 strncpy(tmp_session
.name
, session
->name
, NAME_MAX
);
4092 tmp_session
.name
[NAME_MAX
- 1] = '\0';
4093 tmp_session
.enabled
= session
->active
;
4094 tmp_session
.snapshot_mode
= session
->snapshot_mode
;
4095 tmp_session
.live_timer_interval
= session
->live_timer
;
4096 LTTNG_OPTIONAL_SET(&tmp_extended
.creation_time
, (uint64_t) session
->creation_time
);
4097 ret
= lttng_session_serialize(&tmp_session
, reply_payload
);
4099 ret_code
= LTTNG_ERR_FATAL
;
4103 session_put(session
);
4106 if (i
> UINT32_MAX
) {
4107 ret_code
= LTTNG_ERR_OVERFLOW
;
4111 /* Update command reply header. */
4112 reply_command_header
.count
= (uint32_t) i
;
4113 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
4114 sizeof(reply_command_header
));
4115 ret_code
= LTTNG_OK
;
4121 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4122 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4124 int cmd_data_pending(struct ltt_session
*session
)
4127 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4128 struct ltt_ust_session
*usess
= session
->ust_session
;
4130 LTTNG_ASSERT(session
);
4132 DBG("Data pending for session %s", session
->name
);
4134 /* Session MUST be stopped to ask for data availability. */
4135 if (session
->active
) {
4136 ret
= LTTNG_ERR_SESSION_STARTED
;
4140 * If stopped, just make sure we've started before else the above call
4141 * will always send that there is data pending.
4143 * The consumer assumes that when the data pending command is received,
4144 * the trace has been started before or else no output data is written
4145 * by the streams which is a condition for data pending. So, this is
4146 * *VERY* important that we don't ask the consumer before a start
4149 if (!session
->has_been_started
) {
4155 /* A rotation is still pending, we have to wait. */
4156 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4157 DBG("Rotate still pending for session %s", session
->name
);
4162 if (ksess
&& ksess
->consumer
) {
4163 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4165 /* Data is still being extracted for the kernel. */
4170 if (usess
&& usess
->consumer
) {
4171 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4173 /* Data is still being extracted for the kernel. */
4178 /* Data is ready to be read by a viewer */
4186 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4188 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4190 int cmd_snapshot_add_output(struct ltt_session
*session
,
4191 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4194 struct snapshot_output
*new_output
;
4196 LTTNG_ASSERT(session
);
4197 LTTNG_ASSERT(output
);
4199 DBG("Cmd snapshot add output for session %s", session
->name
);
4202 * Can't create an output if the session is not set in no-output mode.
4204 if (session
->output_traces
) {
4205 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4209 if (session
->has_non_mmap_channel
) {
4210 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4214 /* Only one output is allowed until we have the "tee" feature. */
4215 if (session
->snapshot
.nb_output
== 1) {
4216 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4220 new_output
= snapshot_output_alloc();
4222 ret
= LTTNG_ERR_NOMEM
;
4226 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4227 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4228 &session
->snapshot
);
4230 if (ret
== -ENOMEM
) {
4231 ret
= LTTNG_ERR_NOMEM
;
4233 ret
= LTTNG_ERR_INVALID
;
4239 snapshot_add_output(&session
->snapshot
, new_output
);
4241 *id
= new_output
->id
;
4248 snapshot_output_destroy(new_output
);
4254 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4256 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4258 int cmd_snapshot_del_output(struct ltt_session
*session
,
4259 const struct lttng_snapshot_output
*output
)
4262 struct snapshot_output
*sout
= NULL
;
4264 LTTNG_ASSERT(session
);
4265 LTTNG_ASSERT(output
);
4270 * Permission denied to create an output if the session is not
4271 * set in no output mode.
4273 if (session
->output_traces
) {
4274 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4279 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4281 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4282 } else if (*output
->name
!= '\0') {
4283 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4285 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4288 ret
= LTTNG_ERR_INVALID
;
4292 snapshot_delete_output(&session
->snapshot
, sout
);
4293 snapshot_output_destroy(sout
);
4302 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4304 * If no output is available, outputs is untouched and 0 is returned.
4306 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4308 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4309 struct lttng_snapshot_output
**outputs
)
4312 struct lttng_snapshot_output
*list
= NULL
;
4313 struct lttng_ht_iter iter
;
4314 struct snapshot_output
*output
;
4316 LTTNG_ASSERT(session
);
4317 LTTNG_ASSERT(outputs
);
4319 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4322 * Permission denied to create an output if the session is not
4323 * set in no output mode.
4325 if (session
->output_traces
) {
4326 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4330 if (session
->snapshot
.nb_output
== 0) {
4335 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4337 ret
= -LTTNG_ERR_NOMEM
;
4341 /* Copy list from session to the new list object. */
4343 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4344 output
, node
.node
) {
4345 LTTNG_ASSERT(output
->consumer
);
4346 list
[idx
].id
= output
->id
;
4347 list
[idx
].max_size
= output
->max_size
;
4348 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4349 sizeof(list
[idx
].name
))) {
4350 ret
= -LTTNG_ERR_INVALID
;
4353 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4354 if (lttng_strncpy(list
[idx
].ctrl_url
,
4355 output
->consumer
->dst
.session_root_path
,
4356 sizeof(list
[idx
].ctrl_url
))) {
4357 ret
= -LTTNG_ERR_INVALID
;
4362 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4363 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4365 ret
= -LTTNG_ERR_NOMEM
;
4370 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4371 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4373 ret
= -LTTNG_ERR_NOMEM
;
4382 ret
= session
->snapshot
.nb_output
;
4391 * Check if we can regenerate the metadata for this session.
4392 * Only kernel, UST per-uid and non-live sessions are supported.
4394 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4397 int check_regenerate_metadata_support(struct ltt_session
*session
)
4401 LTTNG_ASSERT(session
);
4403 if (session
->live_timer
!= 0) {
4404 ret
= LTTNG_ERR_LIVE_SESSION
;
4407 if (!session
->active
) {
4408 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4411 if (session
->ust_session
) {
4412 switch (session
->ust_session
->buffer_type
) {
4413 case LTTNG_BUFFER_PER_UID
:
4415 case LTTNG_BUFFER_PER_PID
:
4416 ret
= LTTNG_ERR_PER_PID_SESSION
;
4420 ret
= LTTNG_ERR_UNK
;
4424 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4425 session
->consumer
->relay_minor_version
< 8) {
4426 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4436 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4438 * Ask the consumer to truncate the existing metadata file(s) and
4439 * then regenerate the metadata. Live and per-pid sessions are not
4440 * supported and return an error.
4442 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4444 int cmd_regenerate_metadata(struct ltt_session
*session
)
4448 LTTNG_ASSERT(session
);
4450 ret
= check_regenerate_metadata_support(session
);
4455 if (session
->kernel_session
) {
4456 ret
= kernctl_session_regenerate_metadata(
4457 session
->kernel_session
->fd
);
4459 ERR("Failed to regenerate the kernel metadata");
4464 if (session
->ust_session
) {
4465 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4467 ERR("Failed to regenerate the UST metadata");
4471 DBG("Cmd metadata regenerate for session %s", session
->name
);
4479 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4481 * Ask the tracer to regenerate a new statedump.
4483 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4485 int cmd_regenerate_statedump(struct ltt_session
*session
)
4489 LTTNG_ASSERT(session
);
4491 if (!session
->active
) {
4492 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4496 if (session
->kernel_session
) {
4497 ret
= kernctl_session_regenerate_statedump(
4498 session
->kernel_session
->fd
);
4500 * Currently, the statedump in kernel can only fail if out
4504 if (ret
== -ENOMEM
) {
4505 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4507 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4509 ERR("Failed to regenerate the kernel statedump");
4514 if (session
->ust_session
) {
4515 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4517 * Currently, the statedump in UST always returns 0.
4520 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4521 ERR("Failed to regenerate the UST statedump");
4525 DBG("Cmd regenerate statedump for session %s", session
->name
);
4533 enum lttng_error_code
synchronize_tracer_notifier_register(
4534 struct notification_thread_handle
*notification_thread
,
4535 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4537 enum lttng_error_code ret_code
;
4538 const struct lttng_condition
*condition
=
4539 lttng_trigger_get_const_condition(trigger
);
4540 const char *trigger_name
;
4541 uid_t trigger_owner
;
4542 enum lttng_trigger_status trigger_status
;
4543 const enum lttng_domain_type trigger_domain
=
4544 lttng_trigger_get_underlying_domain_type_restriction(
4547 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4548 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4550 LTTNG_ASSERT(condition
);
4551 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4552 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4554 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4555 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4556 trigger_name
: "(anonymous)";
4558 session_lock_list();
4559 switch (trigger_domain
) {
4560 case LTTNG_DOMAIN_KERNEL
:
4562 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4563 if (ret_code
!= LTTNG_OK
) {
4564 enum lttng_error_code notif_thread_unregister_ret
;
4566 notif_thread_unregister_ret
=
4567 notification_thread_command_unregister_trigger(
4568 notification_thread
, trigger
);
4570 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4571 /* Return the original error code. */
4572 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4574 (int) trigger_owner
,
4580 case LTTNG_DOMAIN_UST
:
4581 ust_app_global_update_all_event_notifier_rules();
4583 case LTTNG_DOMAIN_JUL
:
4584 case LTTNG_DOMAIN_LOG4J
:
4585 case LTTNG_DOMAIN_PYTHON
:
4587 /* Agent domains. */
4588 struct agent
*agt
= agent_find_by_event_notifier_domain(
4592 agt
= agent_create(trigger_domain
);
4594 ret_code
= LTTNG_ERR_NOMEM
;
4595 goto end_unlock_session_list
;
4598 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4601 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4602 if (ret_code
!= LTTNG_OK
) {
4603 goto end_unlock_session_list
;
4608 case LTTNG_DOMAIN_NONE
:
4613 ret_code
= LTTNG_OK
;
4614 end_unlock_session_list
:
4615 session_unlock_list();
4619 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4620 struct lttng_trigger
*trigger
,
4621 bool is_trigger_anonymous
,
4622 struct notification_thread_handle
*notification_thread
,
4623 struct lttng_trigger
**return_trigger
)
4625 enum lttng_error_code ret_code
;
4626 const char *trigger_name
;
4627 uid_t trigger_owner
;
4628 enum lttng_trigger_status trigger_status
;
4630 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4631 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4632 trigger_name
: "(anonymous)";
4634 trigger_status
= lttng_trigger_get_owner_uid(
4635 trigger
, &trigger_owner
);
4636 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4638 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4639 trigger_name
, (int) trigger_owner
,
4640 (int) lttng_credentials_get_uid(cmd_creds
));
4643 * Validate the trigger credentials against the command credentials.
4644 * Only the root user can register a trigger with non-matching
4647 if (!lttng_credentials_is_equal_uid(
4648 lttng_trigger_get_credentials(trigger
),
4650 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4651 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4652 trigger_name
, (int) trigger_owner
,
4653 (int) lttng_credentials_get_uid(cmd_creds
));
4654 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4660 * The bytecode generation also serves as a validation step for the
4661 * bytecode expressions.
4663 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4664 if (ret_code
!= LTTNG_OK
) {
4665 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4666 trigger_name
, (int) trigger_owner
, ret_code
);
4671 * A reference to the trigger is acquired by the notification thread.
4672 * It is safe to return the same trigger to the caller since it the
4673 * other user holds a reference.
4675 * The trigger is modified during the execution of the
4676 * "register trigger" command. However, by the time the command returns,
4677 * it is safe to use without any locking as its properties are
4680 ret_code
= notification_thread_command_register_trigger(
4681 notification_thread
, trigger
, is_trigger_anonymous
);
4682 if (ret_code
!= LTTNG_OK
) {
4683 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4684 trigger_name
, (int) trigger_owner
, ret_code
);
4688 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4689 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4690 trigger_name
: "(anonymous)";
4693 * Synchronize tracers if the trigger adds an event notifier.
4695 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4696 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4697 trigger
, cmd_creds
);
4698 if (ret_code
!= LTTNG_OK
) {
4699 ERR("Error registering tracer notifier: %s",
4700 lttng_strerror(-ret_code
));
4706 * Return an updated trigger to the client.
4708 * Since a modified version of the same trigger is returned, acquire a
4709 * reference to the trigger so the caller doesn't have to care if those
4710 * are distinct instances or not.
4712 if (ret_code
== LTTNG_OK
) {
4713 lttng_trigger_get(trigger
);
4714 *return_trigger
= trigger
;
4715 /* Ownership of trigger was transferred to caller. */
4723 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4724 const struct lttng_trigger
*trigger
)
4726 enum lttng_error_code ret_code
;
4727 const struct lttng_condition
*condition
=
4728 lttng_trigger_get_const_condition(trigger
);
4729 const enum lttng_domain_type trigger_domain
=
4730 lttng_trigger_get_underlying_domain_type_restriction(
4733 LTTNG_ASSERT(condition
);
4734 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4735 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4737 session_lock_list();
4738 switch (trigger_domain
) {
4739 case LTTNG_DOMAIN_KERNEL
:
4740 ret_code
= kernel_unregister_event_notifier(trigger
);
4741 if (ret_code
!= LTTNG_OK
) {
4742 goto end_unlock_session_list
;
4746 case LTTNG_DOMAIN_UST
:
4747 ust_app_global_update_all_event_notifier_rules();
4749 case LTTNG_DOMAIN_JUL
:
4750 case LTTNG_DOMAIN_LOG4J
:
4751 case LTTNG_DOMAIN_PYTHON
:
4753 /* Agent domains. */
4754 struct agent
*agt
= agent_find_by_event_notifier_domain(
4758 * This trigger was never registered in the first place. Calling
4759 * this function under those circumstances is an internal error.
4762 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4763 if (ret_code
!= LTTNG_OK
) {
4764 goto end_unlock_session_list
;
4769 case LTTNG_DOMAIN_NONE
:
4774 ret_code
= LTTNG_OK
;
4776 end_unlock_session_list
:
4777 session_unlock_list();
4781 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4782 const struct lttng_trigger
*trigger
,
4783 struct notification_thread_handle
*notification_thread
)
4785 enum lttng_error_code ret_code
;
4786 const char *trigger_name
;
4787 uid_t trigger_owner
;
4788 enum lttng_trigger_status trigger_status
;
4789 struct lttng_trigger
*sessiond_trigger
= NULL
;
4791 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4792 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4793 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4794 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4796 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4797 trigger_name
, (int) trigger_owner
,
4798 (int) lttng_credentials_get_uid(cmd_creds
));
4801 * Validate the trigger credentials against the command credentials.
4802 * Only the root user can unregister a trigger with non-matching
4805 if (!lttng_credentials_is_equal_uid(
4806 lttng_trigger_get_credentials(trigger
),
4808 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4809 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4810 trigger_name
, (int) trigger_owner
,
4811 (int) lttng_credentials_get_uid(cmd_creds
));
4812 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4817 /* Fetch the sessiond side trigger object. */
4818 ret_code
= notification_thread_command_get_trigger(
4819 notification_thread
, trigger
, &sessiond_trigger
);
4820 if (ret_code
!= LTTNG_OK
) {
4821 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4822 trigger_name
, (int) trigger_owner
, ret_code
);
4826 LTTNG_ASSERT(sessiond_trigger
);
4829 * From this point on, no matter what, consider the trigger
4832 * We set the unregistered state of the sessiond side trigger object in
4833 * the client thread since we want to minimize the possibility of the
4834 * notification thread being stalled due to a long execution of an
4835 * action that required the trigger lock.
4837 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4839 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4841 if (ret_code
!= LTTNG_OK
) {
4842 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4843 trigger_name
, (int) trigger_owner
, ret_code
);
4848 * Synchronize tracers if the trigger removes an event notifier.
4849 * Do this even if the trigger unregistration failed to at least stop
4850 * the tracers from producing notifications associated with this
4853 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4854 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4855 if (ret_code
!= LTTNG_OK
) {
4856 ERR("Error unregistering trigger to tracer.");
4863 lttng_trigger_put(sessiond_trigger
);
4867 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4868 struct notification_thread_handle
*notification_thread
,
4869 struct lttng_triggers
**return_triggers
)
4872 enum lttng_error_code ret_code
;
4873 struct lttng_triggers
*triggers
= NULL
;
4875 /* Get the set of triggers from the notification thread. */
4876 ret_code
= notification_thread_command_list_triggers(
4877 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4878 if (ret_code
!= LTTNG_OK
) {
4882 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4884 ret_code
= LTTNG_ERR_UNK
;
4888 *return_triggers
= triggers
;
4890 ret_code
= LTTNG_OK
;
4892 lttng_triggers_destroy(triggers
);
4896 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4897 const struct lttng_error_query
*query
,
4898 struct lttng_error_query_results
**_results
,
4899 struct notification_thread_handle
*notification_thread
)
4901 enum lttng_error_code ret_code
;
4902 const struct lttng_trigger
*query_target_trigger
;
4903 const struct lttng_action
*query_target_action
= NULL
;
4904 struct lttng_trigger
*matching_trigger
= NULL
;
4905 const char *trigger_name
;
4906 uid_t trigger_owner
;
4907 enum lttng_trigger_status trigger_status
;
4908 struct lttng_error_query_results
*results
= NULL
;
4910 switch (lttng_error_query_get_target_type(query
)) {
4911 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4912 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4914 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4915 query_target_trigger
=
4916 lttng_error_query_condition_borrow_target(query
);
4918 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4919 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4926 LTTNG_ASSERT(query_target_trigger
);
4928 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4929 query_target_trigger
, &matching_trigger
);
4930 if (ret_code
!= LTTNG_OK
) {
4934 /* No longer needed. */
4935 query_target_trigger
= NULL
;
4937 if (lttng_error_query_get_target_type(query
) ==
4938 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4939 /* Get the sessiond-side version of the target action. */
4940 query_target_action
=
4941 lttng_error_query_action_borrow_action_target(
4942 query
, matching_trigger
);
4945 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4946 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4947 trigger_name
: "(anonymous)";
4948 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4950 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4952 results
= lttng_error_query_results_create();
4954 ret_code
= LTTNG_ERR_NOMEM
;
4958 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4959 trigger_name
, (int) trigger_owner
,
4960 (int) lttng_credentials_get_uid(cmd_creds
));
4963 * Validate the trigger credentials against the command credentials.
4964 * Only the root user can target a trigger with non-matching
4967 if (!lttng_credentials_is_equal_uid(
4968 lttng_trigger_get_credentials(matching_trigger
),
4970 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4971 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4972 trigger_name
, (int) trigger_owner
,
4973 (int) lttng_credentials_get_uid(cmd_creds
));
4974 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4979 switch (lttng_error_query_get_target_type(query
)) {
4980 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4981 trigger_status
= lttng_trigger_add_error_results(
4982 matching_trigger
, results
);
4984 switch (trigger_status
) {
4985 case LTTNG_TRIGGER_STATUS_OK
:
4988 ret_code
= LTTNG_ERR_UNK
;
4993 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4995 trigger_status
= lttng_trigger_condition_add_error_results(
4996 matching_trigger
, results
);
4998 switch (trigger_status
) {
4999 case LTTNG_TRIGGER_STATUS_OK
:
5002 ret_code
= LTTNG_ERR_UNK
;
5008 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
5010 const enum lttng_action_status action_status
=
5011 lttng_action_add_error_query_results(
5012 query_target_action
, results
);
5014 switch (action_status
) {
5015 case LTTNG_ACTION_STATUS_OK
:
5018 ret_code
= LTTNG_ERR_UNK
;
5029 *_results
= results
;
5031 ret_code
= LTTNG_OK
;
5033 lttng_trigger_put(matching_trigger
);
5034 lttng_error_query_results_destroy(results
);
5039 * Send relayd sockets from snapshot output to consumer. Ignore request if the
5040 * snapshot output is *not* set with a remote destination.
5042 * Return LTTNG_OK on success or a LTTNG_ERR code.
5044 static enum lttng_error_code
set_relayd_for_snapshot(
5045 struct consumer_output
*output
,
5046 const struct ltt_session
*session
)
5048 enum lttng_error_code status
= LTTNG_OK
;
5049 struct lttng_ht_iter iter
;
5050 struct consumer_socket
*socket
;
5051 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
5052 const char *base_path
;
5054 LTTNG_ASSERT(output
);
5055 LTTNG_ASSERT(session
);
5057 DBG2("Set relayd object from snapshot output");
5059 if (session
->current_trace_chunk
) {
5060 enum lttng_trace_chunk_status chunk_status
=
5061 lttng_trace_chunk_get_id(
5062 session
->current_trace_chunk
,
5063 ¤t_chunk_id
.value
);
5065 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5066 current_chunk_id
.is_set
= true;
5068 ERR("Failed to get current trace chunk id");
5069 status
= LTTNG_ERR_UNK
;
5074 /* Ignore if snapshot consumer output is not network. */
5075 if (output
->type
!= CONSUMER_DST_NET
) {
5080 * The snapshot record URI base path overrides the session
5083 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5084 base_path
= output
->dst
.net
.control
.subdir
;
5086 base_path
= session
->base_path
;
5090 * For each consumer socket, create and send the relayd object of the
5094 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
5095 pthread_mutex_lock(socket
->lock
);
5096 status
= send_consumer_relayd_sockets(*session
, output
, socket
, base_path
,
5097 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
);
5098 pthread_mutex_unlock(socket
->lock
);
5099 if (status
!= LTTNG_OK
) {
5111 * Record a kernel snapshot.
5113 * Return LTTNG_OK on success or a LTTNG_ERR code.
5115 static enum lttng_error_code
record_kernel_snapshot(
5116 struct ltt_kernel_session
*ksess
,
5117 const struct consumer_output
*output
,
5118 const struct ltt_session
*session
,
5119 uint64_t nb_packets_per_stream
)
5121 enum lttng_error_code status
;
5123 LTTNG_ASSERT(ksess
);
5124 LTTNG_ASSERT(output
);
5125 LTTNG_ASSERT(session
);
5127 status
= kernel_snapshot_record(
5128 ksess
, output
, nb_packets_per_stream
);
5133 * Record a UST snapshot.
5135 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5137 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5138 const struct consumer_output
*output
,
5139 const struct ltt_session
*session
,
5140 uint64_t nb_packets_per_stream
)
5142 enum lttng_error_code status
;
5144 LTTNG_ASSERT(usess
);
5145 LTTNG_ASSERT(output
);
5146 LTTNG_ASSERT(session
);
5148 status
= ust_app_snapshot_record(
5149 usess
, output
, nb_packets_per_stream
);
5154 uint64_t get_session_size_one_more_packet_per_stream(
5155 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5157 uint64_t tot_size
= 0;
5159 if (session
->kernel_session
) {
5160 struct ltt_kernel_channel
*chan
;
5161 const struct ltt_kernel_session
*ksess
=
5162 session
->kernel_session
;
5164 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5165 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5167 * Don't take channel into account if we
5168 * already grab all its packets.
5172 tot_size
+= chan
->channel
->attr
.subbuf_size
5173 * chan
->stream_count
;
5177 if (session
->ust_session
) {
5178 const struct ltt_ust_session
*usess
= session
->ust_session
;
5180 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5188 * Calculate the number of packets we can grab from each stream that
5189 * fits within the overall snapshot max size.
5191 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5192 * the number of packets per stream.
5194 * TODO: this approach is not perfect: we consider the worse case
5195 * (packet filling the sub-buffers) as an upper bound, but we could do
5196 * better if we do this calculation while we actually grab the packet
5197 * content: we would know how much padding we don't actually store into
5200 * This algorithm is currently bounded by the number of packets per
5203 * Since we call this algorithm before actually grabbing the data, it's
5204 * an approximation: for instance, applications could appear/disappear
5205 * in between this call and actually grabbing data.
5208 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5212 uint64_t cur_nb_packets
= 0;
5215 return 0; /* Infinite */
5218 size_left
= max_size
;
5220 uint64_t one_more_packet_tot_size
;
5222 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5223 session
, cur_nb_packets
);
5224 if (!one_more_packet_tot_size
) {
5225 /* We are already grabbing all packets. */
5228 size_left
-= one_more_packet_tot_size
;
5229 if (size_left
< 0) {
5234 if (!cur_nb_packets
&& size_left
!= max_size
) {
5235 /* Not enough room to grab one packet of each stream, error. */
5238 return cur_nb_packets
;
5242 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5243 const struct snapshot_output
*snapshot_output
)
5245 int64_t nb_packets_per_stream
;
5246 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5248 enum lttng_error_code ret_code
= LTTNG_OK
;
5249 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5250 struct consumer_output
*original_ust_consumer_output
= NULL
;
5251 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5252 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5253 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5255 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5257 snapshot_output
->name
,
5258 snapshot_output
->datetime
,
5259 snapshot_output
->nb_snapshot
);
5260 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5261 ERR("Failed to format snapshot name");
5262 ret_code
= LTTNG_ERR_INVALID
;
5265 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5266 snapshot_output
->name
, session
->name
,
5267 snapshot_chunk_name
);
5268 if (!session
->kernel_session
&& !session
->ust_session
) {
5269 ERR("Failed to record snapshot as no channels exist");
5270 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5274 if (session
->kernel_session
) {
5275 original_kernel_consumer_output
=
5276 session
->kernel_session
->consumer
;
5277 snapshot_kernel_consumer_output
=
5278 consumer_copy_output(snapshot_output
->consumer
);
5279 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5280 snapshot_chunk_name
);
5282 /* Copy the original domain subdir. */
5283 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5284 original_kernel_consumer_output
->domain_subdir
);
5286 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5287 original_kernel_consumer_output
);
5289 ERR("Failed to copy consumer sockets from snapshot output configuration");
5290 ret_code
= LTTNG_ERR_NOMEM
;
5293 ret_code
= set_relayd_for_snapshot(
5294 snapshot_kernel_consumer_output
, session
);
5295 if (ret_code
!= LTTNG_OK
) {
5296 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5299 session
->kernel_session
->consumer
=
5300 snapshot_kernel_consumer_output
;
5302 if (session
->ust_session
) {
5303 original_ust_consumer_output
= session
->ust_session
->consumer
;
5304 snapshot_ust_consumer_output
=
5305 consumer_copy_output(snapshot_output
->consumer
);
5306 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5307 snapshot_chunk_name
);
5309 /* Copy the original domain subdir. */
5310 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5311 original_ust_consumer_output
->domain_subdir
);
5313 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5314 original_ust_consumer_output
);
5316 ERR("Failed to copy consumer sockets from snapshot output configuration");
5317 ret_code
= LTTNG_ERR_NOMEM
;
5320 ret_code
= set_relayd_for_snapshot(
5321 snapshot_ust_consumer_output
, session
);
5322 if (ret_code
!= LTTNG_OK
) {
5323 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5326 session
->ust_session
->consumer
=
5327 snapshot_ust_consumer_output
;
5330 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5331 snapshot_kernel_consumer_output
?:
5332 snapshot_ust_consumer_output
,
5333 consumer_output_get_base_path(
5334 snapshot_output
->consumer
),
5335 snapshot_chunk_name
);
5336 if (!snapshot_trace_chunk
) {
5337 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5339 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5342 LTTNG_ASSERT(!session
->current_trace_chunk
);
5343 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5344 lttng_trace_chunk_put(snapshot_trace_chunk
);
5345 snapshot_trace_chunk
= NULL
;
5347 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5349 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5353 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5354 snapshot_output
->max_size
);
5355 if (nb_packets_per_stream
< 0) {
5356 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5357 goto error_close_trace_chunk
;
5360 if (session
->kernel_session
) {
5361 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5362 snapshot_kernel_consumer_output
, session
,
5363 nb_packets_per_stream
);
5364 if (ret_code
!= LTTNG_OK
) {
5365 goto error_close_trace_chunk
;
5369 if (session
->ust_session
) {
5370 ret_code
= record_ust_snapshot(session
->ust_session
,
5371 snapshot_ust_consumer_output
, session
,
5372 nb_packets_per_stream
);
5373 if (ret_code
!= LTTNG_OK
) {
5374 goto error_close_trace_chunk
;
5378 error_close_trace_chunk
:
5379 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5380 ERR("Failed to release the current trace chunk of session \"%s\"",
5382 ret_code
= LTTNG_ERR_UNK
;
5385 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5386 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5388 * Don't goto end; make sure the chunk is closed for the session
5389 * to allow future snapshots.
5391 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5393 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5396 lttng_trace_chunk_put(snapshot_trace_chunk
);
5397 snapshot_trace_chunk
= NULL
;
5399 if (original_ust_consumer_output
) {
5400 session
->ust_session
->consumer
= original_ust_consumer_output
;
5402 if (original_kernel_consumer_output
) {
5403 session
->kernel_session
->consumer
=
5404 original_kernel_consumer_output
;
5406 consumer_output_put(snapshot_ust_consumer_output
);
5407 consumer_output_put(snapshot_kernel_consumer_output
);
5412 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5414 * The wait parameter is ignored so this call always wait for the snapshot to
5415 * complete before returning.
5417 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5419 int cmd_snapshot_record(struct ltt_session
*session
,
5420 const struct lttng_snapshot_output
*output
,
5421 int wait
__attribute__((unused
)))
5423 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5425 unsigned int snapshot_success
= 0;
5427 struct snapshot_output
*tmp_output
= NULL
;
5429 LTTNG_ASSERT(session
);
5430 LTTNG_ASSERT(output
);
5432 DBG("Cmd snapshot record for session %s", session
->name
);
5434 /* Get the datetime for the snapshot output directory. */
5435 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5438 cmd_ret
= LTTNG_ERR_INVALID
;
5443 * Permission denied to create an output if the session is not
5444 * set in no output mode.
5446 if (session
->output_traces
) {
5447 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5451 /* The session needs to be started at least once. */
5452 if (!session
->has_been_started
) {
5453 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5457 /* Use temporary output for the session. */
5458 if (*output
->ctrl_url
!= '\0') {
5459 tmp_output
= snapshot_output_alloc();
5461 cmd_ret
= LTTNG_ERR_NOMEM
;
5465 ret
= snapshot_output_init(session
, output
->max_size
,
5467 output
->ctrl_url
, output
->data_url
,
5471 if (ret
== -ENOMEM
) {
5472 cmd_ret
= LTTNG_ERR_NOMEM
;
5474 cmd_ret
= LTTNG_ERR_INVALID
;
5478 /* Use the global session count for the temporary snapshot. */
5479 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5481 /* Use the global datetime */
5482 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5483 cmd_ret
= snapshot_record(session
, tmp_output
);
5484 if (cmd_ret
!= LTTNG_OK
) {
5487 snapshot_success
= 1;
5489 struct snapshot_output
*sout
;
5490 struct lttng_ht_iter iter
;
5493 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5494 &iter
.iter
, sout
, node
.node
) {
5495 struct snapshot_output output_copy
;
5498 * Make a local copy of the output and override output
5499 * parameters with those provided as part of the
5502 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5504 if (output
->max_size
!= (uint64_t) -1ULL) {
5505 output_copy
.max_size
= output
->max_size
;
5508 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5509 memcpy(output_copy
.datetime
, datetime
,
5512 /* Use temporary name. */
5513 if (*output
->name
!= '\0') {
5514 if (lttng_strncpy(output_copy
.name
,
5516 sizeof(output_copy
.name
))) {
5517 cmd_ret
= LTTNG_ERR_INVALID
;
5523 cmd_ret
= snapshot_record(session
, &output_copy
);
5524 if (cmd_ret
!= LTTNG_OK
) {
5528 snapshot_success
= 1;
5533 if (snapshot_success
) {
5534 session
->snapshot
.nb_snapshot
++;
5536 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5541 snapshot_output_destroy(tmp_output
);
5547 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5549 int cmd_set_session_shm_path(struct ltt_session
*session
,
5550 const char *shm_path
)
5553 LTTNG_ASSERT(session
);
5556 * Can only set shm path before session is started.
5558 if (session
->has_been_started
) {
5559 return LTTNG_ERR_SESSION_STARTED
;
5562 strncpy(session
->shm_path
, shm_path
,
5563 sizeof(session
->shm_path
));
5564 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5570 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5572 * Ask the consumer to rotate the session output directory.
5573 * The session lock must be held.
5575 * Returns LTTNG_OK on success or else a negative LTTng error code.
5577 int cmd_rotate_session(struct ltt_session
*session
,
5578 struct lttng_rotate_session_return
*rotate_return
,
5579 bool quiet_rotation
,
5580 enum lttng_trace_chunk_command_type command
)
5583 uint64_t ongoing_rotation_chunk_id
;
5584 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5585 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5586 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5587 enum lttng_trace_chunk_status chunk_status
;
5588 bool failed_to_rotate
= false;
5589 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5591 LTTNG_ASSERT(session
);
5593 if (!session
->has_been_started
) {
5594 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5599 * Explicit rotation is not supported for live sessions.
5600 * However, live sessions can perform a quiet rotation on
5602 * Rotation is not supported for snapshot traces (no output).
5604 if ((!quiet_rotation
&& session
->live_timer
) ||
5605 !session
->output_traces
) {
5606 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5610 /* Unsupported feature in lttng-relayd before 2.11. */
5611 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5612 (session
->consumer
->relay_major_version
== 2 &&
5613 session
->consumer
->relay_minor_version
< 11)) {
5614 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5618 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5619 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5620 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5624 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5625 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5627 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5632 * After a stop, we only allow one rotation to occur, the other ones are
5633 * useless until a new start.
5635 if (session
->rotated_after_last_stop
) {
5636 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5638 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5643 * After a stop followed by a clear, disallow following rotations a they would
5644 * generate empty chunks.
5646 if (session
->cleared_after_last_stop
) {
5647 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5649 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5653 if (session
->active
) {
5654 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5656 if (!new_trace_chunk
) {
5657 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5663 * The current trace chunk becomes the chunk being archived.
5665 * After this point, "chunk_being_archived" must absolutely
5666 * be closed on the consumer(s), otherwise it will never be
5667 * cleaned-up, which will result in a leak.
5669 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5670 &chunk_being_archived
);
5672 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5676 if (session
->kernel_session
) {
5677 cmd_ret
= kernel_rotate_session(session
);
5678 if (cmd_ret
!= LTTNG_OK
) {
5679 failed_to_rotate
= true;
5680 rotation_fail_code
= cmd_ret
;
5683 if (session
->ust_session
) {
5684 cmd_ret
= ust_app_rotate_session(session
);
5685 if (cmd_ret
!= LTTNG_OK
) {
5686 failed_to_rotate
= true;
5687 rotation_fail_code
= cmd_ret
;
5691 if (!session
->active
) {
5692 session
->rotated_after_last_stop
= true;
5695 if (!chunk_being_archived
) {
5696 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5698 if (failed_to_rotate
) {
5699 cmd_ret
= rotation_fail_code
;
5706 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5707 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5708 &ongoing_rotation_chunk_id
);
5709 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5711 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5712 command
, session
->last_chunk_path
);
5714 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5718 if (failed_to_rotate
) {
5719 cmd_ret
= rotation_fail_code
;
5723 session
->quiet_rotation
= quiet_rotation
;
5724 ret
= timer_session_rotation_pending_check_start(session
,
5725 DEFAULT_ROTATE_PENDING_TIMER
);
5727 cmd_ret
= LTTNG_ERR_UNK
;
5731 if (rotate_return
) {
5732 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5735 session
->chunk_being_archived
= chunk_being_archived
;
5736 chunk_being_archived
= NULL
;
5737 if (!quiet_rotation
) {
5738 ret
= notification_thread_command_session_rotation_ongoing(
5739 the_notification_thread_handle
, session
->id
,
5740 ongoing_rotation_chunk_id
);
5741 if (ret
!= LTTNG_OK
) {
5742 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5744 cmd_ret
= (lttng_error_code
) ret
;
5748 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5749 session
->name
, ongoing_rotation_chunk_id
);
5751 lttng_trace_chunk_put(new_trace_chunk
);
5752 lttng_trace_chunk_put(chunk_being_archived
);
5753 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5756 if (session_reset_rotation_state(session
,
5757 LTTNG_ROTATION_STATE_ERROR
)) {
5758 ERR("Failed to reset rotation state of session \"%s\"",
5765 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5767 * Check if the session has finished its rotation.
5769 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5771 int cmd_rotate_get_info(struct ltt_session
*session
,
5772 struct lttng_rotation_get_info_return
*info_return
,
5773 uint64_t rotation_id
)
5775 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5776 enum lttng_rotation_state rotation_state
;
5778 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5779 session
->most_recent_chunk_id
.value
);
5781 if (session
->chunk_being_archived
) {
5782 enum lttng_trace_chunk_status chunk_status
;
5785 chunk_status
= lttng_trace_chunk_get_id(
5786 session
->chunk_being_archived
,
5788 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5790 rotation_state
= rotation_id
== chunk_id
?
5791 LTTNG_ROTATION_STATE_ONGOING
:
5792 LTTNG_ROTATION_STATE_EXPIRED
;
5794 if (session
->last_archived_chunk_id
.is_set
&&
5795 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5796 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5798 rotation_state
= session
->rotation_state
;
5802 switch (rotation_state
) {
5803 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5804 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5807 case LTTNG_ROTATION_STATE_EXPIRED
:
5808 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5809 rotation_id
, session
->name
);
5811 case LTTNG_ROTATION_STATE_ONGOING
:
5812 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5813 rotation_id
, session
->name
);
5815 case LTTNG_ROTATION_STATE_COMPLETED
:
5819 char *current_tracing_path_reply
;
5820 size_t current_tracing_path_reply_len
;
5822 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5823 rotation_id
, session
->name
);
5825 switch (session_get_consumer_destination_type(session
)) {
5826 case CONSUMER_DST_LOCAL
:
5827 current_tracing_path_reply
=
5828 info_return
->location
.local
.absolute_path
;
5829 current_tracing_path_reply_len
=
5830 sizeof(info_return
->location
.local
.absolute_path
);
5831 info_return
->location_type
=
5832 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5833 fmt_ret
= asprintf(&chunk_path
,
5834 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5835 session_get_base_path(session
),
5836 session
->last_archived_chunk_name
);
5837 if (fmt_ret
== -1) {
5838 PERROR("Failed to format the path of the last archived trace chunk");
5839 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5840 cmd_ret
= LTTNG_ERR_UNK
;
5844 case CONSUMER_DST_NET
:
5846 uint16_t ctrl_port
, data_port
;
5848 current_tracing_path_reply
=
5849 info_return
->location
.relay
.relative_path
;
5850 current_tracing_path_reply_len
=
5851 sizeof(info_return
->location
.relay
.relative_path
);
5852 /* Currently the only supported relay protocol. */
5853 info_return
->location
.relay
.protocol
=
5854 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5856 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5857 session_get_net_consumer_hostname(session
),
5858 sizeof(info_return
->location
.relay
.host
));
5860 ERR("Failed to copy host name to rotate_get_info reply");
5861 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5862 cmd_ret
= LTTNG_ERR_SET_URL
;
5866 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5867 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5868 info_return
->location
.relay
.ports
.data
= data_port
;
5869 info_return
->location_type
=
5870 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5871 chunk_path
= strdup(session
->last_chunk_path
);
5873 ERR("Failed to allocate the path of the last archived trace chunk");
5874 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5875 cmd_ret
= LTTNG_ERR_UNK
;
5884 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5885 chunk_path
, current_tracing_path_reply_len
);
5888 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5889 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5890 cmd_ret
= LTTNG_ERR_UNK
;
5896 case LTTNG_ROTATION_STATE_ERROR
:
5897 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5898 rotation_id
, session
->name
);
5906 info_return
->status
= (int32_t) rotation_state
;
5911 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5913 * Configure the automatic rotation parameters.
5914 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5915 * 'activate' to false means deactivate the rotation schedule and validate that
5916 * 'new_value' has the same value as the currently active value.
5918 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5920 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5921 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5923 struct notification_thread_handle
*notification_thread_handle
)
5926 uint64_t *parameter_value
;
5928 LTTNG_ASSERT(session
);
5930 DBG("Cmd rotate set schedule session %s", session
->name
);
5932 if (session
->live_timer
|| !session
->output_traces
) {
5933 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5934 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5938 switch (schedule_type
) {
5939 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5940 parameter_value
= &session
->rotate_size
;
5942 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5943 parameter_value
= &session
->rotate_timer_period
;
5944 if (new_value
>= UINT_MAX
) {
5945 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5946 new_value
, UINT_MAX
);
5947 ret
= LTTNG_ERR_INVALID
;
5952 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5953 ret
= LTTNG_ERR_INVALID
;
5957 /* Improper use of the API. */
5958 if (new_value
== -1ULL) {
5959 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5960 ret
= LTTNG_ERR_INVALID
;
5965 * As indicated in struct ltt_session's comments, a value of == 0 means
5966 * this schedule rotation type is not in use.
5968 * Reject the command if we were asked to activate a schedule that was
5971 if (activate
&& *parameter_value
!= 0) {
5972 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5973 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5978 * Reject the command if we were asked to deactivate a schedule that was
5981 if (!activate
&& *parameter_value
== 0) {
5982 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5983 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5988 * Reject the command if we were asked to deactivate a schedule that
5991 if (!activate
&& *parameter_value
!= new_value
) {
5992 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5993 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5997 *parameter_value
= activate
? new_value
: 0;
5999 switch (schedule_type
) {
6000 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
6001 if (activate
&& session
->active
) {
6003 * Only start the timer if the session is active,
6004 * otherwise it will be started when the session starts.
6006 ret
= timer_session_rotation_schedule_timer_start(
6007 session
, new_value
);
6009 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
6010 ret
= LTTNG_ERR_UNK
;
6014 ret
= timer_session_rotation_schedule_timer_stop(
6017 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
6018 ret
= LTTNG_ERR_UNK
;
6023 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
6025 ret
= subscribe_session_consumed_size_rotation(session
,
6026 new_value
, notification_thread_handle
);
6028 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
6029 ret
= LTTNG_ERR_UNK
;
6033 ret
= unsubscribe_session_consumed_size_rotation(session
,
6034 notification_thread_handle
);
6036 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
6037 ret
= LTTNG_ERR_UNK
;
6044 /* Would have been caught before. */
6056 /* Wait for a given path to be removed before continuing. */
6057 static enum lttng_error_code
wait_on_path(void *path_data
)
6059 const char *shm_path
= (const char *) path_data
;
6061 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6067 ret
= stat(shm_path
, &st
);
6069 if (errno
!= ENOENT
) {
6070 PERROR("stat() returned an error while checking for the existence of the shm path");
6072 DBG("shm path no longer exists, completing the destruction of session");
6076 if (!S_ISDIR(st
.st_mode
)) {
6077 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6082 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6088 * Returns a pointer to a handler to run on completion of a command.
6089 * Returns NULL if no handler has to be run for the last command executed.
6091 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6093 struct cmd_completion_handler
*handler
= current_completion_handler
;
6095 current_completion_handler
= NULL
;
6100 * Init command subsystem.
6105 * Set network sequence index to 1 for streams to match a relayd
6106 * socket on the consumer side.
6108 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6109 relayd_net_seq_idx
= 1;
6110 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6112 DBG("Command subsystem initialized");