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;
894 * Send both relayd sockets to a specific consumer and domain. This is a
895 * helper function to facilitate sending the information to the consumer for a
898 * The consumer socket lock must be held by the caller.
900 * Returns LTTNG_OK, or an LTTng error code on failure.
902 static enum lttng_error_code
send_consumer_relayd_sockets(
903 unsigned int session_id
, struct consumer_output
*consumer
,
904 struct consumer_socket
*sock
, const char *session_name
,
905 const char *hostname
, const char *base_path
, int session_live_timer
,
906 const uint64_t *current_chunk_id
, time_t session_creation_time
,
907 bool session_name_contains_creation_time
)
909 enum lttng_error_code status
= LTTNG_OK
;
910 struct lttcomm_relayd_sock
*control_sock
= nullptr;
911 struct lttcomm_relayd_sock
*data_sock
= nullptr;
913 LTTNG_ASSERT(consumer
);
916 /* Sending control relayd socket. */
917 if (!sock
->control_sock_sent
) {
919 uint64_t result_flags
= 0;
920 /* Connect to relayd and make version check if uri is the control. */
921 status
= create_connect_relayd(&consumer
->dst
.net
.control
, &control_sock
);
922 if (status
!= LTTNG_OK
) {
925 LTTNG_ASSERT(control_sock
);
927 consumer
->relay_major_version
= control_sock
->major
;
928 consumer
->relay_minor_version
= control_sock
->minor
;
930 ret
= relayd_get_configuration(control_sock
, 0, &result_flags
);
932 ERR("Unable to get relayd configuration");
933 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
937 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
938 consumer
->relay_allows_clear
= true;
941 status
= send_consumer_relayd_socket(session_id
, &consumer
->dst
.net
.control
,
942 consumer
, sock
, session_name
, hostname
, base_path
,
943 session_live_timer
, current_chunk_id
, session_creation_time
,
944 session_name_contains_creation_time
, *control_sock
);
946 if (status
!= LTTNG_OK
) {
951 /* Sending data relayd socket. */
952 if (!sock
->data_sock_sent
) {
953 /* Connect to relayd and make version check if uri is the control. */
954 status
= create_connect_relayd(&consumer
->dst
.net
.data
, &data_sock
);
955 if (status
!= LTTNG_OK
) {
958 LTTNG_ASSERT(data_sock
);
960 status
= send_consumer_relayd_socket(session_id
, &consumer
->dst
.net
.data
, consumer
,
961 sock
, session_name
, hostname
, base_path
, session_live_timer
,
962 current_chunk_id
, session_creation_time
,
963 session_name_contains_creation_time
, *data_sock
);
965 if (status
!= LTTNG_OK
) {
971 if (control_sock
!= nullptr) {
972 relayd_close(control_sock
);
976 if (data_sock
!= nullptr) {
977 relayd_close(data_sock
);
985 * Setup relayd connections for a tracing session. First creates the socket to
986 * the relayd and send them to the right domain consumer. Consumer type MUST be
989 int cmd_setup_relayd(struct ltt_session
*session
)
992 struct ltt_ust_session
*usess
;
993 struct ltt_kernel_session
*ksess
;
994 struct consumer_socket
*socket
;
995 struct lttng_ht_iter iter
;
996 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
998 LTTNG_ASSERT(session
);
1000 usess
= session
->ust_session
;
1001 ksess
= session
->kernel_session
;
1003 ERR("Setting relayd for session %s", session
->name
);
1006 if (session
->current_trace_chunk
) {
1007 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1008 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1010 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1011 current_chunk_id
.is_set
= true;
1013 ERR("Failed to get current trace chunk id");
1014 ret
= LTTNG_ERR_UNK
;
1019 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1020 && usess
->consumer
->enabled
) {
1021 /* For each consumer socket, send relayd sockets */
1022 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1023 socket
, node
.node
) {
1024 pthread_mutex_lock(socket
->lock
);
1025 ret
= send_consumer_relayd_sockets(session
->id
,
1026 usess
->consumer
, socket
,
1027 session
->name
, session
->hostname
,
1029 session
->live_timer
,
1030 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1031 session
->creation_time
,
1032 session
->name_contains_creation_time
);
1033 pthread_mutex_unlock(socket
->lock
);
1034 if (ret
!= LTTNG_OK
) {
1037 /* Session is now ready for network streaming. */
1038 session
->net_handle
= 1;
1040 session
->consumer
->relay_major_version
=
1041 usess
->consumer
->relay_major_version
;
1042 session
->consumer
->relay_minor_version
=
1043 usess
->consumer
->relay_minor_version
;
1044 session
->consumer
->relay_allows_clear
=
1045 usess
->consumer
->relay_allows_clear
;
1048 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1049 && ksess
->consumer
->enabled
) {
1050 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1051 socket
, node
.node
) {
1052 pthread_mutex_lock(socket
->lock
);
1053 ret
= send_consumer_relayd_sockets(session
->id
,
1054 ksess
->consumer
, socket
,
1055 session
->name
, session
->hostname
,
1057 session
->live_timer
,
1058 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1059 session
->creation_time
,
1060 session
->name_contains_creation_time
);
1061 pthread_mutex_unlock(socket
->lock
);
1062 if (ret
!= LTTNG_OK
) {
1065 /* Session is now ready for network streaming. */
1066 session
->net_handle
= 1;
1068 session
->consumer
->relay_major_version
=
1069 ksess
->consumer
->relay_major_version
;
1070 session
->consumer
->relay_minor_version
=
1071 ksess
->consumer
->relay_minor_version
;
1072 session
->consumer
->relay_allows_clear
=
1073 ksess
->consumer
->relay_allows_clear
;
1082 * Start a kernel session by opening all necessary streams.
1084 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1087 struct ltt_kernel_channel
*kchan
;
1089 /* Open kernel metadata */
1090 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1091 ret
= kernel_open_metadata(ksess
);
1093 ret
= LTTNG_ERR_KERN_META_FAIL
;
1098 /* Open kernel metadata stream */
1099 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1100 ret
= kernel_open_metadata_stream(ksess
);
1102 ERR("Kernel create metadata stream failed");
1103 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1108 /* For each channel */
1109 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1110 if (kchan
->stream_count
== 0) {
1111 ret
= kernel_open_channel_stream(kchan
);
1113 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1116 /* Update the stream global counter */
1117 ksess
->stream_count_global
+= ret
;
1121 /* Setup kernel consumer socket and send fds to it */
1122 ret
= init_kernel_tracing(ksess
);
1124 ret
= LTTNG_ERR_KERN_START_FAIL
;
1128 /* This start the kernel tracing */
1129 ret
= kernel_start_session(ksess
);
1131 ret
= LTTNG_ERR_KERN_START_FAIL
;
1135 /* Quiescent wait after starting trace */
1136 kernel_wait_quiescent();
1146 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1148 struct ltt_kernel_channel
*kchan
;
1149 bool error_occurred
= false;
1152 if (!ksess
|| !ksess
->active
) {
1155 DBG("Stopping kernel tracing");
1157 ret
= kernel_stop_session(ksess
);
1159 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1163 kernel_wait_quiescent();
1165 /* Flush metadata after stopping (if exists) */
1166 if (ksess
->metadata_stream_fd
>= 0) {
1167 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1169 ERR("Kernel metadata flush failed");
1170 error_occurred
= true;
1174 /* Flush all buffers after stopping */
1175 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1176 ret
= kernel_flush_buffer(kchan
);
1178 ERR("Kernel flush buffer error");
1179 error_occurred
= true;
1184 if (error_occurred
) {
1185 ret
= LTTNG_ERR_UNK
;
1194 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1196 int cmd_disable_channel(struct ltt_session
*session
,
1197 enum lttng_domain_type domain
, char *channel_name
)
1200 struct ltt_ust_session
*usess
;
1202 usess
= session
->ust_session
;
1207 case LTTNG_DOMAIN_KERNEL
:
1209 ret
= channel_kernel_disable(session
->kernel_session
,
1211 if (ret
!= LTTNG_OK
) {
1215 kernel_wait_quiescent();
1218 case LTTNG_DOMAIN_UST
:
1220 struct ltt_ust_channel
*uchan
;
1221 struct lttng_ht
*chan_ht
;
1223 chan_ht
= usess
->domain_global
.channels
;
1225 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1226 if (uchan
== NULL
) {
1227 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1231 ret
= channel_ust_disable(usess
, uchan
);
1232 if (ret
!= LTTNG_OK
) {
1238 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1250 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1252 * The wpipe arguments is used as a notifier for the kernel thread.
1254 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1258 ssize_t sock_recv_len
;
1259 struct lttng_channel
*channel
= NULL
;
1260 struct lttng_buffer_view view
;
1261 struct lttng_dynamic_buffer channel_buffer
;
1262 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1264 lttng_dynamic_buffer_init(&channel_buffer
);
1265 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1266 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1268 ret
= LTTNG_ERR_NOMEM
;
1272 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1274 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1275 ERR("Failed to receive \"enable channel\" command payload");
1276 ret
= LTTNG_ERR_INVALID
;
1280 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1281 if (!lttng_buffer_view_is_valid(&view
)) {
1282 ret
= LTTNG_ERR_INVALID
;
1286 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1287 ERR("Invalid channel payload received in \"enable channel\" command");
1288 ret
= LTTNG_ERR_INVALID
;
1292 ret
= cmd_enable_channel_internal(
1293 cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1296 lttng_dynamic_buffer_reset(&channel_buffer
);
1297 lttng_channel_destroy(channel
);
1301 static enum lttng_error_code
kernel_domain_check_trace_format_requirements(
1302 const lttng::trace_format_descriptor
& descriptor
)
1304 enum lttng_error_code ret_code
= LTTNG_OK
;
1305 switch (descriptor
.type()) {
1306 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1
:
1307 /* Supported by all kernel tracer. */
1309 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_2
:
1310 if (!kernel_supports_ctf2()) {
1311 ret_code
= LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_KERNEL_TRACER
;
1320 static enum lttng_error_code
cmd_enable_channel_internal(
1321 struct ltt_session
*session
,
1322 const struct lttng_domain
*domain
,
1323 const struct lttng_channel
*_attr
,
1326 enum lttng_error_code ret_code
;
1327 struct ltt_ust_session
*usess
= session
->ust_session
;
1328 struct lttng_ht
*chan_ht
;
1330 struct lttng_channel
*attr
= NULL
;
1332 LTTNG_ASSERT(session
);
1333 LTTNG_ASSERT(_attr
);
1334 LTTNG_ASSERT(domain
);
1336 attr
= lttng_channel_copy(_attr
);
1338 ret_code
= LTTNG_ERR_NOMEM
;
1342 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1344 /* Validate channel name */
1345 if (attr
->name
[0] == '.' ||
1346 memchr(attr
->name
, '/', len
) != NULL
) {
1347 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1351 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1356 * If the session is a live session, remove the switch timer, the
1357 * live timer does the same thing but sends also synchronisation
1358 * beacons for inactive streams.
1360 if (session
->live_timer
> 0) {
1361 attr
->attr
.live_timer_interval
= session
->live_timer
;
1362 attr
->attr
.switch_timer_interval
= 0;
1365 /* Check for feature support */
1366 switch (domain
->type
) {
1367 case LTTNG_DOMAIN_KERNEL
:
1369 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1370 /* Sampling position of buffer is not supported */
1371 WARN("Kernel tracer does not support buffer monitoring. "
1372 "Setting the monitor interval timer to 0 "
1373 "(disabled) for channel '%s' of session '%s'",
1374 attr
->name
, session
->name
);
1375 lttng_channel_set_monitor_timer_interval(attr
, 0);
1378 ret_code
= kernel_domain_check_trace_format_requirements(*session
->trace_format
);
1379 if (ret_code
!= LTTNG_OK
) {
1380 WARN("Kernel tracer does not support the configured trace format of session '%s'",
1386 case LTTNG_DOMAIN_UST
:
1388 case LTTNG_DOMAIN_JUL
:
1389 case LTTNG_DOMAIN_LOG4J
:
1390 case LTTNG_DOMAIN_PYTHON
:
1391 if (!agent_tracing_is_enabled()) {
1392 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1393 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1398 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1402 switch (domain
->type
) {
1403 case LTTNG_DOMAIN_KERNEL
:
1405 struct ltt_kernel_channel
*kchan
;
1407 kchan
= trace_kernel_get_channel_by_name(
1408 attr
->name
, session
->kernel_session
);
1409 if (kchan
== NULL
) {
1411 * Don't try to create a channel if the session has been started at
1412 * some point in time before. The tracer does not allow it.
1414 if (session
->has_been_started
) {
1415 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1419 if (session
->snapshot
.nb_output
> 0 ||
1420 session
->snapshot_mode
) {
1421 /* Enforce mmap output for snapshot sessions. */
1422 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1424 ret_code
= channel_kernel_create(
1425 session
->kernel_session
, attr
, wpipe
);
1426 if (attr
->name
[0] != '\0') {
1427 session
->kernel_session
->has_non_default_channel
= 1;
1430 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1433 if (ret_code
!= LTTNG_OK
) {
1437 kernel_wait_quiescent();
1440 case LTTNG_DOMAIN_UST
:
1441 case LTTNG_DOMAIN_JUL
:
1442 case LTTNG_DOMAIN_LOG4J
:
1443 case LTTNG_DOMAIN_PYTHON
:
1445 struct ltt_ust_channel
*uchan
;
1450 * Current agent implementation limitations force us to allow
1451 * only one channel at once in "agent" subdomains. Each
1452 * subdomain has a default channel name which must be strictly
1455 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1456 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1457 LTTNG_SYMBOL_NAME_LEN
)) {
1458 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1461 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1462 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1463 LTTNG_SYMBOL_NAME_LEN
)) {
1464 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1467 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1468 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1469 LTTNG_SYMBOL_NAME_LEN
)) {
1470 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1475 chan_ht
= usess
->domain_global
.channels
;
1477 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1478 if (uchan
== NULL
) {
1480 * Don't try to create a channel if the session has been started at
1481 * some point in time before. The tracer does not allow it.
1483 if (session
->has_been_started
) {
1484 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1488 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1489 if (attr
->name
[0] != '\0') {
1490 usess
->has_non_default_channel
= 1;
1493 ret_code
= channel_ust_enable(usess
, uchan
);
1498 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1502 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1503 session
->has_non_mmap_channel
= true;
1508 lttng_channel_destroy(attr
);
1512 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1513 struct ltt_session
*session
,
1514 enum lttng_domain_type domain
,
1515 enum lttng_process_attr process_attr
,
1516 enum lttng_tracking_policy
*policy
)
1518 enum lttng_error_code ret_code
= LTTNG_OK
;
1519 const struct process_attr_tracker
*tracker
;
1522 case LTTNG_DOMAIN_KERNEL
:
1523 if (!session
->kernel_session
) {
1524 ret_code
= LTTNG_ERR_INVALID
;
1527 tracker
= kernel_get_process_attr_tracker(
1528 session
->kernel_session
, process_attr
);
1530 case LTTNG_DOMAIN_UST
:
1531 if (!session
->ust_session
) {
1532 ret_code
= LTTNG_ERR_INVALID
;
1535 tracker
= trace_ust_get_process_attr_tracker(
1536 session
->ust_session
, process_attr
);
1539 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1543 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1545 ret_code
= LTTNG_ERR_INVALID
;
1551 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1552 struct ltt_session
*session
,
1553 enum lttng_domain_type domain
,
1554 enum lttng_process_attr process_attr
,
1555 enum lttng_tracking_policy policy
)
1557 enum lttng_error_code ret_code
= LTTNG_OK
;
1560 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1561 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1562 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1565 ret_code
= LTTNG_ERR_INVALID
;
1570 case LTTNG_DOMAIN_KERNEL
:
1571 if (!session
->kernel_session
) {
1572 ret_code
= LTTNG_ERR_INVALID
;
1575 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1576 session
->kernel_session
, process_attr
, policy
);
1578 case LTTNG_DOMAIN_UST
:
1579 if (!session
->ust_session
) {
1580 ret_code
= LTTNG_ERR_INVALID
;
1583 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1584 session
->ust_session
, process_attr
, policy
);
1587 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1594 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1595 struct ltt_session
*session
,
1596 enum lttng_domain_type domain
,
1597 enum lttng_process_attr process_attr
,
1598 const struct process_attr_value
*value
)
1600 enum lttng_error_code ret_code
= LTTNG_OK
;
1603 case LTTNG_DOMAIN_KERNEL
:
1604 if (!session
->kernel_session
) {
1605 ret_code
= LTTNG_ERR_INVALID
;
1608 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1609 session
->kernel_session
, process_attr
, value
);
1611 case LTTNG_DOMAIN_UST
:
1612 if (!session
->ust_session
) {
1613 ret_code
= LTTNG_ERR_INVALID
;
1616 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1617 session
->ust_session
, process_attr
, value
);
1620 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1627 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1628 struct ltt_session
*session
,
1629 enum lttng_domain_type domain
,
1630 enum lttng_process_attr process_attr
,
1631 const struct process_attr_value
*value
)
1633 enum lttng_error_code ret_code
= LTTNG_OK
;
1636 case LTTNG_DOMAIN_KERNEL
:
1637 if (!session
->kernel_session
) {
1638 ret_code
= LTTNG_ERR_INVALID
;
1641 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1642 session
->kernel_session
, process_attr
, value
);
1644 case LTTNG_DOMAIN_UST
:
1645 if (!session
->ust_session
) {
1646 ret_code
= LTTNG_ERR_INVALID
;
1649 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1650 session
->ust_session
, process_attr
, value
);
1653 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1660 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1661 struct ltt_session
*session
,
1662 enum lttng_domain_type domain
,
1663 enum lttng_process_attr process_attr
,
1664 struct lttng_process_attr_values
**values
)
1666 enum lttng_error_code ret_code
= LTTNG_OK
;
1667 const struct process_attr_tracker
*tracker
;
1668 enum process_attr_tracker_status status
;
1671 case LTTNG_DOMAIN_KERNEL
:
1672 if (!session
->kernel_session
) {
1673 ret_code
= LTTNG_ERR_INVALID
;
1676 tracker
= kernel_get_process_attr_tracker(
1677 session
->kernel_session
, process_attr
);
1679 case LTTNG_DOMAIN_UST
:
1680 if (!session
->ust_session
) {
1681 ret_code
= LTTNG_ERR_INVALID
;
1684 tracker
= trace_ust_get_process_attr_tracker(
1685 session
->ust_session
, process_attr
);
1688 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1693 ret_code
= LTTNG_ERR_INVALID
;
1697 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1699 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1700 ret_code
= LTTNG_OK
;
1702 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1703 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1705 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1706 ret_code
= LTTNG_ERR_NOMEM
;
1709 ret_code
= LTTNG_ERR_UNK
;
1718 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1720 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1721 struct lttng_event
*event
,
1722 char *filter_expression
,
1723 struct lttng_bytecode
*bytecode
,
1724 struct lttng_event_exclusion
*exclusion
)
1727 const char *event_name
;
1728 const struct ltt_session
*session
= cmd_ctx
->session
;
1729 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1730 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1732 DBG("Disable event command for event \'%s\'", event
->name
);
1735 * Filter and exclusions are simply not handled by the
1736 * disable event command at this time.
1740 (void) filter_expression
;
1743 /* Ignore the presence of filter or exclusion for the event */
1745 event
->exclusion
= 0;
1747 event_name
= event
->name
;
1749 /* Error out on unhandled search criteria */
1750 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1751 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1752 ret
= LTTNG_ERR_UNK
;
1759 case LTTNG_DOMAIN_KERNEL
:
1761 struct ltt_kernel_channel
*kchan
;
1762 struct ltt_kernel_session
*ksess
;
1764 ksess
= session
->kernel_session
;
1767 * If a non-default channel has been created in the
1768 * session, explicitely require that -c chan_name needs
1771 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1772 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1776 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1777 if (kchan
== NULL
) {
1778 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1782 switch (event
->type
) {
1783 case LTTNG_EVENT_ALL
:
1784 case LTTNG_EVENT_TRACEPOINT
:
1785 case LTTNG_EVENT_SYSCALL
:
1786 case LTTNG_EVENT_PROBE
:
1787 case LTTNG_EVENT_FUNCTION
:
1788 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1789 if (event_name
[0] == '\0') {
1790 ret
= event_kernel_disable_event(kchan
,
1793 ret
= event_kernel_disable_event(kchan
,
1794 event_name
, event
->type
);
1796 if (ret
!= LTTNG_OK
) {
1801 ret
= LTTNG_ERR_UNK
;
1805 kernel_wait_quiescent();
1808 case LTTNG_DOMAIN_UST
:
1810 struct ltt_ust_channel
*uchan
;
1811 struct ltt_ust_session
*usess
;
1813 usess
= session
->ust_session
;
1815 if (validate_ust_event_name(event_name
)) {
1816 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1821 * If a non-default channel has been created in the
1822 * session, explicitly require that -c chan_name needs
1825 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1826 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1830 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1832 if (uchan
== NULL
) {
1833 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1837 switch (event
->type
) {
1838 case LTTNG_EVENT_ALL
:
1840 * An empty event name means that everything
1841 * should be disabled.
1843 if (event
->name
[0] == '\0') {
1844 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1846 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1849 if (ret
!= LTTNG_OK
) {
1854 ret
= LTTNG_ERR_UNK
;
1858 DBG3("Disable UST event %s in channel %s completed", event_name
,
1862 case LTTNG_DOMAIN_LOG4J
:
1863 case LTTNG_DOMAIN_JUL
:
1864 case LTTNG_DOMAIN_PYTHON
:
1867 struct ltt_ust_session
*usess
= session
->ust_session
;
1869 LTTNG_ASSERT(usess
);
1871 switch (event
->type
) {
1872 case LTTNG_EVENT_ALL
:
1875 ret
= LTTNG_ERR_UNK
;
1879 agt
= trace_ust_find_agent(usess
, domain
);
1881 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1885 * An empty event name means that everything
1886 * should be disabled.
1888 if (event
->name
[0] == '\0') {
1889 ret
= event_agent_disable_all(usess
, agt
);
1891 ret
= event_agent_disable(usess
, agt
, event_name
);
1893 if (ret
!= LTTNG_OK
) {
1900 ret
= LTTNG_ERR_UND
;
1911 free(filter_expression
);
1916 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1918 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1919 const struct lttng_event_context
*event_context
, int kwpipe
)
1921 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1922 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1923 const struct ltt_session
*session
= cmd_ctx
->session
;
1924 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1927 * Don't try to add a context if the session has been started at
1928 * some point in time before. The tracer does not allow it and would
1929 * result in a corrupted trace.
1931 if (cmd_ctx
->session
->has_been_started
) {
1932 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1937 case LTTNG_DOMAIN_KERNEL
:
1938 LTTNG_ASSERT(session
->kernel_session
);
1940 if (session
->kernel_session
->channel_count
== 0) {
1941 /* Create default channel */
1942 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1943 if (ret
!= LTTNG_OK
) {
1946 chan_kern_created
= 1;
1948 /* Add kernel context to kernel tracer */
1949 ret
= context_kernel_add(session
->kernel_session
,
1950 event_context
, channel_name
);
1951 if (ret
!= LTTNG_OK
) {
1955 case LTTNG_DOMAIN_JUL
:
1956 case LTTNG_DOMAIN_LOG4J
:
1959 * Validate channel name.
1960 * If no channel name is given and the domain is JUL or LOG4J,
1961 * set it to the appropriate domain-specific channel name. If
1962 * a name is provided but does not match the expexted channel
1963 * name, return an error.
1965 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1966 strcmp(channel_name
,
1967 DEFAULT_JUL_CHANNEL_NAME
)) {
1968 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1970 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1971 strcmp(channel_name
,
1972 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1973 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1978 case LTTNG_DOMAIN_UST
:
1980 struct ltt_ust_session
*usess
= session
->ust_session
;
1981 unsigned int chan_count
;
1983 LTTNG_ASSERT(usess
);
1985 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1986 if (chan_count
== 0) {
1987 struct lttng_channel
*attr
;
1988 /* Create default channel */
1989 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1991 ret
= LTTNG_ERR_FATAL
;
1995 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1996 if (ret
!= LTTNG_OK
) {
2000 channel_attr_destroy(attr
);
2001 chan_ust_created
= 1;
2004 ret
= context_ust_add(usess
, domain
, event_context
,
2006 if (ret
!= LTTNG_OK
) {
2012 ret
= LTTNG_ERR_UND
;
2020 if (chan_kern_created
) {
2021 struct ltt_kernel_channel
*kchan
=
2022 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2023 session
->kernel_session
);
2024 /* Created previously, this should NOT fail. */
2025 LTTNG_ASSERT(kchan
);
2026 kernel_destroy_channel(kchan
);
2029 if (chan_ust_created
) {
2030 struct ltt_ust_channel
*uchan
=
2031 trace_ust_find_channel_by_name(
2032 session
->ust_session
->domain_global
.channels
,
2033 DEFAULT_CHANNEL_NAME
);
2034 /* Created previously, this should NOT fail. */
2035 LTTNG_ASSERT(uchan
);
2036 /* Remove from the channel list of the session. */
2037 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2039 trace_ust_destroy_channel(uchan
);
2045 static inline bool name_starts_with(const char *name
, const char *prefix
)
2047 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2049 return !strncmp(name
, prefix
, max_cmp_len
);
2052 /* Perform userspace-specific event name validation */
2053 static int validate_ust_event_name(const char *name
)
2063 * Check name against all internal UST event component namespaces used
2066 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2067 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2068 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2077 * Internal version of cmd_enable_event() with a supplemental
2078 * "internal_event" flag which is used to enable internal events which should
2079 * be hidden from clients. Such events are used in the agent implementation to
2080 * enable the events through which all "agent" events are funeled.
2082 static int _cmd_enable_event(struct ltt_session
*session
,
2083 const struct lttng_domain
*domain
,
2084 char *channel_name
, struct lttng_event
*event
,
2085 char *filter_expression
,
2086 struct lttng_bytecode
*filter
,
2087 struct lttng_event_exclusion
*exclusion
,
2088 int wpipe
, bool internal_event
)
2090 int ret
= 0, channel_created
= 0;
2091 struct lttng_channel
*attr
= NULL
;
2093 LTTNG_ASSERT(session
);
2094 LTTNG_ASSERT(event
);
2095 LTTNG_ASSERT(channel_name
);
2097 /* If we have a filter, we must have its filter expression */
2098 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2100 /* Normalize event name as a globbing pattern */
2101 strutils_normalize_star_glob_pattern(event
->name
);
2103 /* Normalize exclusion names as globbing patterns */
2107 for (i
= 0; i
< exclusion
->count
; i
++) {
2108 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2110 strutils_normalize_star_glob_pattern(name
);
2114 DBG("Enable event command for event \'%s\'", event
->name
);
2118 switch (domain
->type
) {
2119 case LTTNG_DOMAIN_KERNEL
:
2121 struct ltt_kernel_channel
*kchan
;
2124 * If a non-default channel has been created in the
2125 * session, explicitely require that -c chan_name needs
2128 if (session
->kernel_session
->has_non_default_channel
2129 && channel_name
[0] == '\0') {
2130 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2134 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2135 session
->kernel_session
);
2136 if (kchan
== NULL
) {
2137 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2138 LTTNG_BUFFER_GLOBAL
);
2140 ret
= LTTNG_ERR_FATAL
;
2143 if (lttng_strncpy(attr
->name
, channel_name
,
2144 sizeof(attr
->name
))) {
2145 ret
= LTTNG_ERR_INVALID
;
2149 ret
= cmd_enable_channel_internal(
2150 session
, domain
, attr
, wpipe
);
2151 if (ret
!= LTTNG_OK
) {
2154 channel_created
= 1;
2157 /* Get the newly created kernel channel pointer */
2158 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2159 session
->kernel_session
);
2160 if (kchan
== NULL
) {
2161 /* This sould not happen... */
2162 ret
= LTTNG_ERR_FATAL
;
2166 switch (event
->type
) {
2167 case LTTNG_EVENT_ALL
:
2169 char *filter_expression_a
= NULL
;
2170 struct lttng_bytecode
*filter_a
= NULL
;
2173 * We need to duplicate filter_expression and filter,
2174 * because ownership is passed to first enable
2177 if (filter_expression
) {
2178 filter_expression_a
= strdup(filter_expression
);
2179 if (!filter_expression_a
) {
2180 ret
= LTTNG_ERR_FATAL
;
2185 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2187 free(filter_expression_a
);
2188 ret
= LTTNG_ERR_FATAL
;
2191 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2193 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2194 ret
= event_kernel_enable_event(kchan
, event
,
2195 filter_expression
, filter
);
2196 /* We have passed ownership */
2197 filter_expression
= NULL
;
2199 if (ret
!= LTTNG_OK
) {
2200 if (channel_created
) {
2201 /* Let's not leak a useless channel. */
2202 kernel_destroy_channel(kchan
);
2204 free(filter_expression_a
);
2208 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2209 ret
= event_kernel_enable_event(kchan
, event
,
2210 filter_expression_a
, filter_a
);
2211 /* We have passed ownership */
2212 filter_expression_a
= NULL
;
2214 if (ret
!= LTTNG_OK
) {
2219 case LTTNG_EVENT_PROBE
:
2220 case LTTNG_EVENT_USERSPACE_PROBE
:
2221 case LTTNG_EVENT_FUNCTION
:
2222 case LTTNG_EVENT_FUNCTION_ENTRY
:
2223 case LTTNG_EVENT_TRACEPOINT
:
2224 ret
= event_kernel_enable_event(kchan
, event
,
2225 filter_expression
, filter
);
2226 /* We have passed ownership */
2227 filter_expression
= NULL
;
2229 if (ret
!= LTTNG_OK
) {
2230 if (channel_created
) {
2231 /* Let's not leak a useless channel. */
2232 kernel_destroy_channel(kchan
);
2237 case LTTNG_EVENT_SYSCALL
:
2238 ret
= event_kernel_enable_event(kchan
, event
,
2239 filter_expression
, filter
);
2240 /* We have passed ownership */
2241 filter_expression
= NULL
;
2243 if (ret
!= LTTNG_OK
) {
2248 ret
= LTTNG_ERR_UNK
;
2252 kernel_wait_quiescent();
2255 case LTTNG_DOMAIN_UST
:
2257 struct ltt_ust_channel
*uchan
;
2258 struct ltt_ust_session
*usess
= session
->ust_session
;
2260 LTTNG_ASSERT(usess
);
2263 * If a non-default channel has been created in the
2264 * session, explicitely require that -c chan_name needs
2267 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2268 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2272 /* Get channel from global UST domain */
2273 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2275 if (uchan
== NULL
) {
2276 /* Create default channel */
2277 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2278 usess
->buffer_type
);
2280 ret
= LTTNG_ERR_FATAL
;
2283 if (lttng_strncpy(attr
->name
, channel_name
,
2284 sizeof(attr
->name
))) {
2285 ret
= LTTNG_ERR_INVALID
;
2289 ret
= cmd_enable_channel_internal(
2290 session
, domain
, attr
, wpipe
);
2291 if (ret
!= LTTNG_OK
) {
2295 /* Get the newly created channel reference back */
2296 uchan
= trace_ust_find_channel_by_name(
2297 usess
->domain_global
.channels
, channel_name
);
2298 LTTNG_ASSERT(uchan
);
2301 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2303 * Don't allow users to add UST events to channels which
2304 * are assigned to a userspace subdomain (JUL, Log4J,
2307 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2311 if (!internal_event
) {
2313 * Ensure the event name is not reserved for internal
2316 ret
= validate_ust_event_name(event
->name
);
2318 WARN("Userspace event name %s failed validation.",
2320 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2325 /* At this point, the session and channel exist on the tracer */
2326 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2327 filter_expression
, filter
, exclusion
,
2329 /* We have passed ownership */
2330 filter_expression
= NULL
;
2333 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2334 goto already_enabled
;
2335 } else if (ret
!= LTTNG_OK
) {
2340 case LTTNG_DOMAIN_LOG4J
:
2341 case LTTNG_DOMAIN_JUL
:
2342 case LTTNG_DOMAIN_PYTHON
:
2344 const char *default_event_name
, *default_chan_name
;
2346 struct lttng_event uevent
;
2347 struct lttng_domain tmp_dom
;
2348 struct ltt_ust_session
*usess
= session
->ust_session
;
2350 LTTNG_ASSERT(usess
);
2352 if (!agent_tracing_is_enabled()) {
2353 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2354 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2358 agt
= trace_ust_find_agent(usess
, domain
->type
);
2360 agt
= agent_create(domain
->type
);
2362 ret
= LTTNG_ERR_NOMEM
;
2365 agent_add(agt
, usess
->agents
);
2368 /* Create the default tracepoint. */
2369 memset(&uevent
, 0, sizeof(uevent
));
2370 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2371 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2372 default_event_name
= event_get_default_agent_ust_name(
2374 if (!default_event_name
) {
2375 ret
= LTTNG_ERR_FATAL
;
2378 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2379 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2382 * The domain type is changed because we are about to enable the
2383 * default channel and event for the JUL domain that are hardcoded.
2384 * This happens in the UST domain.
2386 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2387 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2389 switch (domain
->type
) {
2390 case LTTNG_DOMAIN_LOG4J
:
2391 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2393 case LTTNG_DOMAIN_JUL
:
2394 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2396 case LTTNG_DOMAIN_PYTHON
:
2397 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2400 /* The switch/case we are in makes this impossible */
2405 char *filter_expression_copy
= NULL
;
2406 struct lttng_bytecode
*filter_copy
= NULL
;
2409 const size_t filter_size
= sizeof(
2410 struct lttng_bytecode
)
2413 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2415 ret
= LTTNG_ERR_NOMEM
;
2418 memcpy(filter_copy
, filter
, filter_size
);
2420 filter_expression_copy
=
2421 strdup(filter_expression
);
2422 if (!filter_expression
) {
2423 ret
= LTTNG_ERR_NOMEM
;
2426 if (!filter_expression_copy
|| !filter_copy
) {
2427 free(filter_expression_copy
);
2433 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2434 (char *) default_chan_name
,
2435 &uevent
, filter_expression_copy
,
2436 filter_copy
, NULL
, wpipe
);
2439 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2440 goto already_enabled
;
2441 } else if (ret
!= LTTNG_OK
) {
2445 /* The wild card * means that everything should be enabled. */
2446 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2447 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2450 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2454 filter_expression
= NULL
;
2455 if (ret
!= LTTNG_OK
) {
2462 ret
= LTTNG_ERR_UND
;
2470 free(filter_expression
);
2473 channel_attr_destroy(attr
);
2479 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2480 * We own filter, exclusion, and filter_expression.
2482 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2483 struct lttng_event
*event
,
2484 char *filter_expression
,
2485 struct lttng_event_exclusion
*exclusion
,
2486 struct lttng_bytecode
*bytecode
,
2491 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2493 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2496 * The ownership of the following parameters is transferred to
2497 * _cmd_enable_event:
2499 * - filter_expression,
2503 ret
= _cmd_enable_event(cmd_ctx
->session
,
2505 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2506 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2507 filter_expression
= NULL
;
2514 * Enable an event which is internal to LTTng. An internal should
2515 * never be made visible to clients and are immune to checks such as
2518 static int cmd_enable_event_internal(struct ltt_session
*session
,
2519 const struct lttng_domain
*domain
,
2520 char *channel_name
, struct lttng_event
*event
,
2521 char *filter_expression
,
2522 struct lttng_bytecode
*filter
,
2523 struct lttng_event_exclusion
*exclusion
,
2526 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2527 filter_expression
, filter
, exclusion
, wpipe
, true);
2531 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2533 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2534 struct lttng_payload
*reply_payload
)
2536 enum lttng_error_code ret_code
;
2538 ssize_t i
, nb_events
= 0;
2539 struct lttng_event
*events
= NULL
;
2540 struct lttcomm_list_command_header reply_command_header
= {};
2541 size_t reply_command_header_offset
;
2543 assert(reply_payload
);
2545 /* Reserve space for command reply header. */
2546 reply_command_header_offset
= reply_payload
->buffer
.size
;
2547 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2548 reply_command_header_offset
+
2549 sizeof(struct lttcomm_list_command_header
));
2551 ret_code
= LTTNG_ERR_NOMEM
;
2556 case LTTNG_DOMAIN_KERNEL
:
2557 nb_events
= kernel_list_events(&events
);
2558 if (nb_events
< 0) {
2559 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2563 case LTTNG_DOMAIN_UST
:
2564 nb_events
= ust_app_list_events(&events
);
2565 if (nb_events
< 0) {
2566 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2570 case LTTNG_DOMAIN_LOG4J
:
2571 case LTTNG_DOMAIN_JUL
:
2572 case LTTNG_DOMAIN_PYTHON
:
2573 nb_events
= agent_list_events(&events
, domain
);
2574 if (nb_events
< 0) {
2575 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2580 ret_code
= LTTNG_ERR_UND
;
2584 for (i
= 0; i
< nb_events
; i
++) {
2585 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2588 ret_code
= LTTNG_ERR_NOMEM
;
2593 if (nb_events
> UINT32_MAX
) {
2594 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2595 ret_code
= LTTNG_ERR_OVERFLOW
;
2599 /* Update command reply header. */
2600 reply_command_header
.count
= (uint32_t) nb_events
;
2601 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2602 sizeof(reply_command_header
));
2604 ret_code
= LTTNG_OK
;
2611 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2613 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2614 struct lttng_payload
*reply
)
2616 enum lttng_error_code ret_code
;
2618 unsigned int i
, nb_fields
;
2619 struct lttng_event_field
*fields
= NULL
;
2620 struct lttcomm_list_command_header reply_command_header
= {};
2621 size_t reply_command_header_offset
;
2625 /* Reserve space for command reply header. */
2626 reply_command_header_offset
= reply
->buffer
.size
;
2627 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2628 reply_command_header_offset
+
2629 sizeof(struct lttcomm_list_command_header
));
2631 ret_code
= LTTNG_ERR_NOMEM
;
2636 case LTTNG_DOMAIN_UST
:
2637 ret
= ust_app_list_event_fields(&fields
);
2639 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2644 case LTTNG_DOMAIN_KERNEL
:
2645 default: /* fall-through */
2646 ret_code
= LTTNG_ERR_UND
;
2652 for (i
= 0; i
< nb_fields
; i
++) {
2653 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2655 ret_code
= LTTNG_ERR_NOMEM
;
2660 if (nb_fields
> UINT32_MAX
) {
2661 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2662 ret_code
= LTTNG_ERR_OVERFLOW
;
2666 /* Update command reply header. */
2667 reply_command_header
.count
= (uint32_t) nb_fields
;
2669 memcpy(reply
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2670 sizeof(reply_command_header
));
2672 ret_code
= LTTNG_OK
;
2679 enum lttng_error_code
cmd_list_syscalls(
2680 struct lttng_payload
*reply_payload
)
2682 enum lttng_error_code ret_code
;
2683 ssize_t nb_events
, i
;
2685 struct lttng_event
*events
= NULL
;
2686 struct lttcomm_list_command_header reply_command_header
= {};
2687 size_t reply_command_header_offset
;
2689 assert(reply_payload
);
2691 /* Reserve space for command reply header. */
2692 reply_command_header_offset
= reply_payload
->buffer
.size
;
2693 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2694 reply_command_header_offset
+
2695 sizeof(struct lttcomm_list_command_header
));
2697 ret_code
= LTTNG_ERR_NOMEM
;
2701 nb_events
= syscall_table_list(&events
);
2702 if (nb_events
< 0) {
2703 ret_code
= (enum lttng_error_code
) -nb_events
;
2707 for (i
= 0; i
< nb_events
; i
++) {
2708 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2711 ret_code
= LTTNG_ERR_NOMEM
;
2716 if (nb_events
> UINT32_MAX
) {
2717 ERR("Syscall count would overflow the syscall listing command's reply");
2718 ret_code
= LTTNG_ERR_OVERFLOW
;
2722 /* Update command reply header. */
2723 reply_command_header
.count
= (uint32_t) nb_events
;
2724 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2725 sizeof(reply_command_header
));
2727 ret_code
= LTTNG_OK
;
2734 * Command LTTNG_START_TRACE processed by the client thread.
2736 * Called with session mutex held.
2738 int cmd_start_trace(struct ltt_session
*session
)
2740 enum lttng_error_code ret
;
2741 unsigned long nb_chan
= 0;
2742 struct ltt_kernel_session
*ksession
;
2743 struct ltt_ust_session
*usess
;
2744 const bool session_rotated_after_last_stop
=
2745 session
->rotated_after_last_stop
;
2746 const bool session_cleared_after_last_stop
=
2747 session
->cleared_after_last_stop
;
2749 LTTNG_ASSERT(session
);
2751 /* Ease our life a bit ;) */
2752 ksession
= session
->kernel_session
;
2753 usess
= session
->ust_session
;
2755 /* Is the session already started? */
2756 if (session
->active
) {
2757 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2758 /* Perform nothing */
2762 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2763 !session
->current_trace_chunk
) {
2765 * A rotation was launched while the session was stopped and
2766 * it has not been completed yet. It is not possible to start
2767 * the session since starting the session here would require a
2768 * rotation from "NULL" to a new trace chunk. That rotation
2769 * would overlap with the ongoing rotation, which is not
2772 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2774 ret
= LTTNG_ERR_ROTATION_PENDING
;
2779 * Starting a session without channel is useless since after that it's not
2780 * possible to enable channel thus inform the client.
2782 if (usess
&& usess
->domain_global
.channels
) {
2783 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2786 nb_chan
+= ksession
->channel_count
;
2789 ret
= LTTNG_ERR_NO_CHANNEL
;
2793 session
->active
= true;
2794 session
->rotated_after_last_stop
= false;
2795 session
->cleared_after_last_stop
= false;
2796 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2797 if (!session
->has_been_started
) {
2798 struct lttng_trace_chunk
*trace_chunk
;
2800 DBG("Creating initial trace chunk of session \"%s\"",
2802 trace_chunk
= session_create_new_trace_chunk(
2803 session
, NULL
, NULL
, NULL
);
2805 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2808 LTTNG_ASSERT(!session
->current_trace_chunk
);
2809 ret
= (lttng_error_code
) session_set_trace_chunk(session
, trace_chunk
,
2811 lttng_trace_chunk_put(trace_chunk
);
2813 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2817 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2820 * Rotate existing streams into the new chunk.
2821 * This is a "quiet" rotation has no client has
2822 * explicitly requested this operation.
2824 * There is also no need to wait for the rotation
2825 * to complete as it will happen immediately. No data
2826 * was produced as the session was stopped, so the
2827 * rotation should happen on reception of the command.
2829 ret
= (lttng_error_code
) cmd_rotate_session(session
, NULL
, true,
2830 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2831 if (ret
!= LTTNG_OK
) {
2837 /* Kernel tracing */
2838 if (ksession
!= NULL
) {
2839 DBG("Start kernel tracing session %s", session
->name
);
2840 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2841 if (ret
!= LTTNG_OK
) {
2846 /* Flag session that trace should start automatically */
2848 int int_ret
= ust_app_start_trace_all(usess
);
2851 ret
= LTTNG_ERR_UST_START_FAIL
;
2857 * Open a packet in every stream of the session to ensure that viewers
2858 * can correctly identify the boundaries of the periods during which
2859 * tracing was active for this session.
2861 ret
= session_open_packets(session
);
2862 if (ret
!= LTTNG_OK
) {
2867 * Clear the flag that indicates that a rotation was done while the
2868 * session was stopped.
2870 session
->rotated_after_last_stop
= false;
2872 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2873 int int_ret
= timer_session_rotation_schedule_timer_start(
2874 session
, session
->rotate_timer_period
);
2877 ERR("Failed to enable rotate timer");
2878 ret
= LTTNG_ERR_UNK
;
2886 if (ret
== LTTNG_OK
) {
2887 /* Flag this after a successful start. */
2888 session
->has_been_started
= true;
2890 session
->active
= false;
2891 /* Restore initial state on error. */
2892 session
->rotated_after_last_stop
=
2893 session_rotated_after_last_stop
;
2894 session
->cleared_after_last_stop
=
2895 session_cleared_after_last_stop
;
2902 * Command LTTNG_STOP_TRACE processed by the client thread.
2904 int cmd_stop_trace(struct ltt_session
*session
)
2907 struct ltt_kernel_session
*ksession
;
2908 struct ltt_ust_session
*usess
;
2910 LTTNG_ASSERT(session
);
2912 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2914 ksession
= session
->kernel_session
;
2915 usess
= session
->ust_session
;
2917 /* Session is not active. Skip everything and inform the client. */
2918 if (!session
->active
) {
2919 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2923 ret
= stop_kernel_session(ksession
);
2924 if (ret
!= LTTNG_OK
) {
2928 if (usess
&& usess
->active
) {
2929 ret
= ust_app_stop_trace_all(usess
);
2931 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2936 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2938 /* Flag inactive after a successful stop. */
2939 session
->active
= 0;
2947 * Set the base_path of the session only if subdir of a control uris is set.
2948 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2950 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2952 struct lttng_uri
*uris
)
2957 for (i
= 0; i
< nb_uri
; i
++) {
2958 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2959 uris
[i
].subdir
[0] == '\0') {
2960 /* Not interested in these URIs */
2964 if (session
->base_path
!= NULL
) {
2965 free(session
->base_path
);
2966 session
->base_path
= NULL
;
2969 /* Set session base_path */
2970 session
->base_path
= strdup(uris
[i
].subdir
);
2971 if (!session
->base_path
) {
2972 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2973 uris
[i
].subdir
, session
->name
);
2974 ret
= LTTNG_ERR_NOMEM
;
2977 DBG2("Setting base path \"%s\" for session \"%s\"",
2978 session
->base_path
, session
->name
);
2986 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2988 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2989 struct lttng_uri
*uris
)
2992 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2993 struct ltt_ust_session
*usess
= session
->ust_session
;
2995 LTTNG_ASSERT(session
);
2997 LTTNG_ASSERT(nb_uri
> 0);
2999 /* Can't set consumer URI if the session is active. */
3000 if (session
->active
) {
3001 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
3006 * Set the session base path if any. This is done inside
3007 * cmd_set_consumer_uri to preserve backward compatibility of the
3008 * previous session creation api vs the session descriptor api.
3010 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
3011 if (ret
!= LTTNG_OK
) {
3015 /* Set the "global" consumer URIs */
3016 for (i
= 0; i
< nb_uri
; i
++) {
3017 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
3019 if (ret
!= LTTNG_OK
) {
3024 /* Set UST session URIs */
3025 if (session
->ust_session
) {
3026 for (i
= 0; i
< nb_uri
; i
++) {
3027 ret
= add_uri_to_consumer(session
,
3028 session
->ust_session
->consumer
,
3029 &uris
[i
], LTTNG_DOMAIN_UST
);
3030 if (ret
!= LTTNG_OK
) {
3036 /* Set kernel session URIs */
3037 if (session
->kernel_session
) {
3038 for (i
= 0; i
< nb_uri
; i
++) {
3039 ret
= add_uri_to_consumer(session
,
3040 session
->kernel_session
->consumer
,
3041 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3042 if (ret
!= LTTNG_OK
) {
3049 * Make sure to set the session in output mode after we set URI since a
3050 * session can be created without URL (thus flagged in no output mode).
3052 session
->output_traces
= 1;
3054 ksess
->output_traces
= 1;
3058 usess
->output_traces
= 1;
3069 enum lttng_error_code
set_session_output_from_descriptor(
3070 struct ltt_session
*session
,
3071 const struct lttng_session_descriptor
*descriptor
)
3074 enum lttng_error_code ret_code
= LTTNG_OK
;
3075 enum lttng_session_descriptor_type session_type
=
3076 lttng_session_descriptor_get_type(descriptor
);
3077 enum lttng_session_descriptor_output_type output_type
=
3078 lttng_session_descriptor_get_output_type(descriptor
);
3079 struct lttng_uri uris
[2] = {};
3080 size_t uri_count
= 0;
3082 switch (output_type
) {
3083 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3085 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3086 lttng_session_descriptor_get_local_output_uri(descriptor
,
3090 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3091 lttng_session_descriptor_get_network_output_uris(descriptor
,
3092 &uris
[0], &uris
[1]);
3096 ret_code
= LTTNG_ERR_INVALID
;
3100 switch (session_type
) {
3101 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3103 struct snapshot_output
*new_output
= NULL
;
3105 new_output
= snapshot_output_alloc();
3107 ret_code
= LTTNG_ERR_NOMEM
;
3111 ret
= snapshot_output_init_with_uri(session
,
3112 DEFAULT_SNAPSHOT_MAX_SIZE
,
3113 NULL
, uris
, uri_count
, session
->consumer
,
3114 new_output
, &session
->snapshot
);
3116 ret_code
= (ret
== -ENOMEM
) ?
3117 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3118 snapshot_output_destroy(new_output
);
3121 snapshot_add_output(&session
->snapshot
, new_output
);
3124 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3125 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3127 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3131 ret_code
= LTTNG_ERR_INVALID
;
3139 enum lttng_error_code
cmd_create_session_from_descriptor(
3140 struct lttng_session_descriptor
*descriptor
,
3141 const lttng_sock_cred
*creds
,
3142 const char *home_path
)
3145 enum lttng_error_code ret_code
;
3146 const char *session_name
;
3147 struct ltt_session
*new_session
= NULL
;
3148 enum lttng_session_descriptor_status descriptor_status
;
3149 const lttng_trace_format_descriptor
*trace_format_descriptor
= NULL
;
3150 lttng::trace_format_descriptor::uptr trace_format_descriptor_ptr
;
3152 session_lock_list();
3154 if (*home_path
!= '/') {
3155 ERR("Home path provided by client is not absolute");
3156 ret_code
= LTTNG_ERR_INVALID
;
3161 descriptor_status
= lttng_session_descriptor_get_session_name(
3162 descriptor
, &session_name
);
3163 switch (descriptor_status
) {
3164 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3166 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3167 session_name
= NULL
;
3170 ret_code
= LTTNG_ERR_INVALID
;
3174 descriptor_status
= lttng_session_descriptor_get_trace_format_descriptor(
3175 descriptor
, &trace_format_descriptor
);
3176 if (descriptor_status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
3177 ret_code
= LTTNG_ERR_INVALID
;
3182 trace_format_descriptor_ptr
=
3183 reinterpret_cast<const lttng::trace_format_descriptor
*>(
3184 trace_format_descriptor
)
3186 } catch (std::exception
& e
) {
3187 ERR("%s", e
.what());
3188 ret_code
= LTTNG_ERR_UNK
;
3192 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, trace_format_descriptor_ptr
,
3194 if (ret_code
!= LTTNG_OK
) {
3198 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3199 new_session
->id
, new_session
->name
, new_session
->uid
, new_session
->gid
);
3200 if (ret_code
!= LTTNG_OK
) {
3204 /* Announce the session's destruction to the notification thread when it is destroyed. */
3205 ret
= session_add_destroy_notifier(
3207 [](const struct ltt_session
*session
,
3208 void *user_data
__attribute__((unused
))) {
3209 (void) notification_thread_command_remove_session(
3210 the_notification_thread_handle
, session
->id
);
3214 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3216 ret
= LTTNG_ERR_NOMEM
;
3220 if (!session_name
) {
3221 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3224 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3229 if (!lttng_session_descriptor_is_output_destination_initialized(
3232 * Only include the session's creation time in the output
3233 * destination if the name of the session itself was
3234 * not auto-generated.
3236 ret_code
= lttng_session_descriptor_set_default_output(
3238 session_name
? &new_session
->creation_time
: NULL
,
3240 if (ret_code
!= LTTNG_OK
) {
3244 new_session
->has_user_specified_directory
=
3245 lttng_session_descriptor_has_output_directory(
3249 switch (lttng_session_descriptor_get_type(descriptor
)) {
3250 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3251 new_session
->snapshot_mode
= 1;
3253 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3254 new_session
->live_timer
=
3255 lttng_session_descriptor_live_get_timer_interval(
3262 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3263 if (ret_code
!= LTTNG_OK
) {
3266 new_session
->consumer
->enabled
= 1;
3267 ret_code
= LTTNG_OK
;
3269 /* Release reference provided by the session_create function. */
3270 session_put(new_session
);
3271 if (ret_code
!= LTTNG_OK
&& new_session
) {
3272 /* Release the global reference on error. */
3273 session_destroy(new_session
);
3275 session_unlock_list();
3279 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3280 struct lttng_session_descriptor
**return_descriptor
)
3283 size_t payload_size
;
3284 struct lttng_dynamic_buffer payload
;
3285 struct lttng_buffer_view home_dir_view
;
3286 struct lttng_buffer_view session_descriptor_view
;
3287 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3288 enum lttng_error_code ret_code
;
3290 lttng_dynamic_buffer_init(&payload
);
3291 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3293 ret_code
= LTTNG_ERR_INVALID
;
3296 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3297 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3298 ret_code
= LTTNG_ERR_INVALID
;
3302 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3303 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3304 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3306 ret_code
= LTTNG_ERR_NOMEM
;
3310 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3312 ERR("Reception of session descriptor failed, aborting.");
3313 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3317 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3320 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3321 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3322 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3323 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3324 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3328 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3330 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3331 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3332 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3333 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3334 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3338 ret
= lttng_session_descriptor_create_from_buffer(
3339 &session_descriptor_view
, &session_descriptor
);
3341 ERR("Failed to create session descriptor from payload of \"create session\" command");
3342 ret_code
= LTTNG_ERR_INVALID
;
3347 * Sets the descriptor's auto-generated properties (name, output) if
3350 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3352 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3353 if (ret_code
!= LTTNG_OK
) {
3357 ret_code
= LTTNG_OK
;
3358 *return_descriptor
= session_descriptor
;
3359 session_descriptor
= NULL
;
3361 lttng_dynamic_buffer_reset(&payload
);
3362 lttng_session_descriptor_destroy(session_descriptor
);
3367 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3368 void *_reply_context
)
3372 const struct cmd_destroy_session_reply_context
*reply_context
=
3373 (cmd_destroy_session_reply_context
*) _reply_context
;
3374 struct lttng_dynamic_buffer payload
;
3375 struct lttcomm_session_destroy_command_header cmd_header
;
3376 struct lttng_trace_archive_location
*location
= NULL
;
3377 struct lttcomm_lttng_msg llm
= {
3378 .cmd_type
= LTTNG_DESTROY_SESSION
,
3379 .ret_code
= reply_context
->destruction_status
,
3382 sizeof(struct lttcomm_session_destroy_command_header
),
3386 size_t payload_size_before_location
;
3388 lttng_dynamic_buffer_init(&payload
);
3390 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3392 ERR("Failed to append session destruction message");
3396 cmd_header
.rotation_state
=
3397 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3398 session
->rotation_state
:
3399 LTTNG_ROTATION_STATE_NO_ROTATION
);
3400 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3401 sizeof(cmd_header
));
3403 ERR("Failed to append session destruction command header");
3407 if (!reply_context
->implicit_rotation_on_destroy
) {
3408 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3412 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3413 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3418 location
= session_get_trace_archive_location(session
);
3420 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3425 payload_size_before_location
= payload
.size
;
3426 comm_ret
= lttng_trace_archive_location_serialize(location
,
3428 lttng_trace_archive_location_put(location
);
3430 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3434 /* Update the message to indicate the location's length. */
3435 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3436 payload
.size
- payload_size_before_location
;
3438 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3439 payload
.data
, payload
.size
);
3440 if (comm_ret
!= (ssize_t
) payload
.size
) {
3441 ERR("Failed to send result of the destruction of session \"%s\" to client",
3445 ret
= close(reply_context
->reply_sock_fd
);
3447 PERROR("Failed to close client socket in deferred session destroy reply");
3449 lttng_dynamic_buffer_reset(&payload
);
3450 free(_reply_context
);
3454 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3456 * Called with session lock held.
3458 int cmd_destroy_session(struct ltt_session
*session
,
3459 struct notification_thread_handle
*notification_thread_handle
,
3463 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3464 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3467 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3468 if (!reply_context
) {
3469 ret
= LTTNG_ERR_NOMEM
;
3473 reply_context
->reply_sock_fd
= *sock_fd
;
3477 LTTNG_ASSERT(session
);
3479 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3481 if (session
->active
) {
3482 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3484 ret
= cmd_stop_trace(session
);
3485 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3486 /* Carry on with the destruction of the session. */
3487 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3488 session
->name
, lttng_strerror(-ret
));
3489 destruction_last_error
= (lttng_error_code
) ret
;
3493 if (session
->rotation_schedule_timer_enabled
) {
3494 if (timer_session_rotation_schedule_timer_stop(
3496 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3498 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3502 if (session
->rotate_size
) {
3503 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3504 session
->rotate_size
= 0;
3507 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3509 * Perform a last rotation on destruction if rotations have
3510 * occurred during the session's lifetime.
3512 ret
= cmd_rotate_session(session
, NULL
, false,
3513 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3514 if (ret
!= LTTNG_OK
) {
3515 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3516 session
->name
, lttng_strerror(-ret
));
3517 destruction_last_error
= (lttng_error_code
) -ret
;
3519 if (reply_context
) {
3520 reply_context
->implicit_rotation_on_destroy
= true;
3522 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3524 * The user has not triggered a session rotation. However, to
3525 * ensure all data has been consumed, the session is rotated
3526 * to a 'null' trace chunk before it is destroyed.
3528 * This is a "quiet" rotation meaning that no notification is
3529 * emitted and no renaming of the current trace chunk takes
3532 ret
= cmd_rotate_session(session
, NULL
, true,
3533 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3535 * Rotation operations may not be supported by the kernel
3536 * tracer. Hence, do not consider this implicit rotation as
3537 * a session destruction error. The library has already stopped
3538 * the session and waited for pending data; there is nothing
3539 * left to do but complete the destruction of the session.
3541 if (ret
!= LTTNG_OK
&&
3542 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3543 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3544 session
->name
, lttng_strerror(ret
));
3545 destruction_last_error
= (lttng_error_code
) -ret
;
3549 if (session
->shm_path
[0]) {
3551 * When a session is created with an explicit shm_path,
3552 * the consumer daemon will create its shared memory files
3553 * at that location and will *not* unlink them. This is normal
3554 * as the intention of that feature is to make it possible
3555 * to retrieve the content of those files should a crash occur.
3557 * To ensure the content of those files can be used, the
3558 * sessiond daemon will replicate the content of the metadata
3559 * cache in a metadata file.
3561 * On clean-up, it is expected that the consumer daemon will
3562 * unlink the shared memory files and that the session daemon
3563 * will unlink the metadata file. Then, the session's directory
3564 * in the shm path can be removed.
3566 * Unfortunately, a flaw in the design of the sessiond's and
3567 * consumerd's tear down of channels makes it impossible to
3568 * determine when the sessiond _and_ the consumerd have both
3569 * destroyed their representation of a channel. For one, the
3570 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3571 * callbacks in both daemons.
3573 * However, it is also impossible for the sessiond to know when
3574 * the consumer daemon is done destroying its channel(s) since
3575 * it occurs as a reaction to the closing of the channel's file
3576 * descriptor. There is no resulting communication initiated
3577 * from the consumerd to the sessiond to confirm that the
3578 * operation is completed (and was successful).
3580 * Until this is all fixed, the session daemon checks for the
3581 * removal of the session's shm path which makes it possible
3582 * to safely advertise a session as having been destroyed.
3584 * Prior to this fix, it was not possible to reliably save
3585 * a session making use of the --shm-path option, destroy it,
3586 * and load it again. This is because the creation of the
3587 * session would fail upon seeing the session's shm path
3588 * already in existence.
3590 * Note that none of the error paths in the check for the
3591 * directory's existence return an error. This is normal
3592 * as there isn't much that can be done. The session will
3593 * be destroyed properly, except that we can't offer the
3594 * guarantee that the same session can be re-created.
3596 current_completion_handler
= &destroy_completion_handler
.handler
;
3597 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3599 sizeof(destroy_completion_handler
.shm_path
));
3604 * The session is destroyed. However, note that the command context
3605 * still holds a reference to the session, thus delaying its destruction
3606 * _at least_ up to the point when that reference is released.
3608 session_destroy(session
);
3609 if (reply_context
) {
3610 reply_context
->destruction_status
= destruction_last_error
;
3611 ret
= session_add_destroy_notifier(session
,
3612 cmd_destroy_session_reply
,
3613 (void *) reply_context
);
3615 ret
= LTTNG_ERR_FATAL
;
3627 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3629 int cmd_register_consumer(struct ltt_session
*session
,
3630 enum lttng_domain_type domain
, const char *sock_path
,
3631 struct consumer_data
*cdata
)
3634 struct consumer_socket
*socket
= NULL
;
3636 LTTNG_ASSERT(session
);
3637 LTTNG_ASSERT(cdata
);
3638 LTTNG_ASSERT(sock_path
);
3641 case LTTNG_DOMAIN_KERNEL
:
3643 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3645 LTTNG_ASSERT(ksess
);
3647 /* Can't register a consumer if there is already one */
3648 if (ksess
->consumer_fds_sent
!= 0) {
3649 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3653 sock
= lttcomm_connect_unix_sock(sock_path
);
3655 ret
= LTTNG_ERR_CONNECT_FAIL
;
3658 cdata
->cmd_sock
= sock
;
3660 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3661 if (socket
== NULL
) {
3664 PERROR("close register consumer");
3666 cdata
->cmd_sock
= -1;
3667 ret
= LTTNG_ERR_FATAL
;
3671 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3672 if (socket
->lock
== NULL
) {
3673 PERROR("zmalloc pthread mutex");
3674 ret
= LTTNG_ERR_FATAL
;
3678 pthread_mutex_init(socket
->lock
, NULL
);
3679 socket
->registered
= 1;
3682 consumer_add_socket(socket
, ksess
->consumer
);
3685 pthread_mutex_lock(&cdata
->pid_mutex
);
3687 pthread_mutex_unlock(&cdata
->pid_mutex
);
3692 /* TODO: Userspace tracing */
3693 ret
= LTTNG_ERR_UND
;
3701 consumer_destroy_socket(socket
);
3707 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3709 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3710 struct lttng_domain
**domains
)
3715 struct lttng_ht_iter iter
;
3717 if (session
->kernel_session
!= NULL
) {
3718 DBG3("Listing domains found kernel domain");
3722 if (session
->ust_session
!= NULL
) {
3723 DBG3("Listing domains found UST global domain");
3727 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3729 if (agt
->being_used
) {
3740 *domains
= calloc
<lttng_domain
>(nb_dom
);
3741 if (*domains
== NULL
) {
3742 ret
= LTTNG_ERR_FATAL
;
3746 if (session
->kernel_session
!= NULL
) {
3747 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3749 /* Kernel session buffer type is always GLOBAL */
3750 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3755 if (session
->ust_session
!= NULL
) {
3756 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3757 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3761 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3763 if (agt
->being_used
) {
3764 (*domains
)[index
].type
= agt
->domain
;
3765 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3775 /* Return negative value to differentiate return code */
3781 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3783 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3784 struct ltt_session
*session
,
3785 struct lttng_payload
*payload
)
3789 struct lttcomm_list_command_header cmd_header
= {};
3790 size_t cmd_header_offset
;
3791 enum lttng_error_code ret_code
;
3796 DBG("Listing channels for session %s", session
->name
);
3798 cmd_header_offset
= payload
->buffer
.size
;
3800 /* Reserve space for command reply header. */
3801 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3802 cmd_header_offset
+ sizeof(cmd_header
));
3804 ret_code
= LTTNG_ERR_NOMEM
;
3809 case LTTNG_DOMAIN_KERNEL
:
3811 /* Kernel channels */
3812 struct ltt_kernel_channel
*kchan
;
3813 if (session
->kernel_session
!= NULL
) {
3814 cds_list_for_each_entry(kchan
,
3815 &session
->kernel_session
->channel_list
.head
, list
) {
3816 uint64_t discarded_events
, lost_packets
;
3817 struct lttng_channel_extended
*extended
;
3819 extended
= (struct lttng_channel_extended
*)
3820 kchan
->channel
->attr
.extended
.ptr
;
3822 ret
= get_kernel_runtime_stats(session
, kchan
,
3823 &discarded_events
, &lost_packets
);
3825 ret_code
= LTTNG_ERR_UNK
;
3830 * Update the discarded_events and lost_packets
3831 * count for the channel
3833 extended
->discarded_events
= discarded_events
;
3834 extended
->lost_packets
= lost_packets
;
3836 ret
= lttng_channel_serialize(
3837 kchan
->channel
, &payload
->buffer
);
3839 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3840 kchan
->channel
->name
);
3841 ret_code
= LTTNG_ERR_UNK
;
3850 case LTTNG_DOMAIN_UST
:
3852 struct lttng_ht_iter iter
;
3853 struct ltt_ust_channel
*uchan
;
3856 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3857 &iter
.iter
, uchan
, node
.node
) {
3858 uint64_t discarded_events
= 0, lost_packets
= 0;
3859 struct lttng_channel
*channel
= NULL
;
3860 struct lttng_channel_extended
*extended
;
3862 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3864 ret_code
= LTTNG_ERR_NOMEM
;
3868 extended
= (struct lttng_channel_extended
*)
3869 channel
->attr
.extended
.ptr
;
3871 ret
= get_ust_runtime_stats(session
, uchan
,
3872 &discarded_events
, &lost_packets
);
3874 lttng_channel_destroy(channel
);
3875 ret_code
= LTTNG_ERR_UNK
;
3879 extended
->discarded_events
= discarded_events
;
3880 extended
->lost_packets
= lost_packets
;
3882 ret
= lttng_channel_serialize(
3883 channel
, &payload
->buffer
);
3885 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3887 lttng_channel_destroy(channel
);
3888 ret_code
= LTTNG_ERR_UNK
;
3892 lttng_channel_destroy(channel
);
3902 if (i
> UINT32_MAX
) {
3903 ERR("Channel count would overflow the channel listing command's reply");
3904 ret_code
= LTTNG_ERR_OVERFLOW
;
3908 /* Update command reply header. */
3909 cmd_header
.count
= (uint32_t) i
;
3910 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3911 sizeof(cmd_header
));
3912 ret_code
= LTTNG_OK
;
3919 * Command LTTNG_LIST_EVENTS processed by the client thread.
3921 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3922 struct ltt_session
*session
,
3924 struct lttng_payload
*reply_payload
)
3926 int buffer_resize_ret
;
3927 enum lttng_error_code ret_code
= LTTNG_OK
;
3928 struct lttcomm_list_command_header reply_command_header
= {};
3929 size_t reply_command_header_offset
;
3930 unsigned int nb_events
= 0;
3932 assert(reply_payload
);
3934 /* Reserve space for command reply header. */
3935 reply_command_header_offset
= reply_payload
->buffer
.size
;
3936 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3937 reply_command_header_offset
+
3938 sizeof(struct lttcomm_list_command_header
));
3939 if (buffer_resize_ret
) {
3940 ret_code
= LTTNG_ERR_NOMEM
;
3945 case LTTNG_DOMAIN_KERNEL
:
3946 if (session
->kernel_session
!= NULL
) {
3947 ret_code
= list_lttng_kernel_events(channel_name
,
3948 session
->kernel_session
, reply_payload
, &nb_events
);
3952 case LTTNG_DOMAIN_UST
:
3954 if (session
->ust_session
!= NULL
) {
3955 ret_code
= list_lttng_ust_global_events(channel_name
,
3956 &session
->ust_session
->domain_global
,
3957 reply_payload
, &nb_events
);
3962 case LTTNG_DOMAIN_LOG4J
:
3963 case LTTNG_DOMAIN_JUL
:
3964 case LTTNG_DOMAIN_PYTHON
:
3965 if (session
->ust_session
) {
3966 struct lttng_ht_iter iter
;
3970 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3971 &iter
.iter
, agt
, node
.node
) {
3972 if (agt
->domain
== domain
) {
3973 ret_code
= list_lttng_agent_events(
3974 agt
, reply_payload
, &nb_events
);
3983 ret_code
= LTTNG_ERR_UND
;
3987 if (nb_events
> UINT32_MAX
) {
3988 ret_code
= LTTNG_ERR_OVERFLOW
;
3992 /* Update command reply header. */
3993 reply_command_header
.count
= (uint32_t) nb_events
;
3994 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
3995 sizeof(reply_command_header
));
4002 * Using the session list, filled a lttng_session array to send back to the
4003 * client for session listing.
4005 * The session list lock MUST be acquired before calling this function. Use
4006 * session_lock_list() and session_unlock_list().
4008 enum lttng_error_code
cmd_list_lttng_sessions(
4009 struct lttng_payload
*reply_payload
, uid_t uid
, gid_t gid
)
4011 int buffer_resize_ret
;
4012 enum lttng_error_code ret_code
= LTTNG_OK
;
4013 struct lttcomm_list_command_header reply_command_header
= {};
4014 size_t reply_command_header_offset
;
4015 struct ltt_session
*session
;
4016 struct ltt_session_list
*list
= session_get_list();
4020 assert(reply_payload
);
4022 /* Reserve space for command reply header. */
4023 reply_command_header_offset
= reply_payload
->buffer
.size
;
4024 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
4025 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
4026 if (buffer_resize_ret
) {
4027 ret_code
= LTTNG_ERR_NOMEM
;
4031 DBG("Getting all available session for UID %d GID %d",
4034 cds_list_for_each_entry(session
, &list
->head
, list
) {
4035 struct lttng_session tmp_session
= {};
4036 struct lttng_session_extended tmp_extended
= {};
4038 tmp_session
.extended
.ptr
= &tmp_extended
;
4040 if (!session_get(session
)) {
4044 * Only list the sessions the user can control.
4046 if (!session_access_ok(session
, uid
) ||
4047 session
->destroyed
) {
4048 session_put(session
);
4052 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4053 struct ltt_ust_session
*usess
= session
->ust_session
;
4055 if (session
->consumer
->type
== CONSUMER_DST_NET
||
4056 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
4057 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
4058 ret
= build_network_session_path(
4059 tmp_session
.path
, sizeof(tmp_session
.path
), session
);
4061 ret
= snprintf(tmp_session
.path
, sizeof(tmp_session
.path
), "%s",
4062 session
->consumer
->dst
.session_root_path
);
4065 PERROR("snprintf session path");
4066 session_put(session
);
4070 strncpy(tmp_session
.name
, session
->name
, NAME_MAX
);
4071 tmp_session
.name
[NAME_MAX
- 1] = '\0';
4072 tmp_session
.enabled
= session
->active
;
4073 tmp_session
.snapshot_mode
= session
->snapshot_mode
;
4074 tmp_session
.live_timer_interval
= session
->live_timer
;
4075 LTTNG_OPTIONAL_SET(&tmp_extended
.creation_time
, (uint64_t) session
->creation_time
);
4076 ret
= lttng_session_serialize(&tmp_session
, reply_payload
);
4078 ret_code
= LTTNG_ERR_FATAL
;
4082 session_put(session
);
4085 if (i
> UINT32_MAX
) {
4086 ret_code
= LTTNG_ERR_OVERFLOW
;
4090 /* Update command reply header. */
4091 reply_command_header
.count
= (uint32_t) i
;
4092 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
4093 sizeof(reply_command_header
));
4094 ret_code
= LTTNG_OK
;
4100 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4101 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4103 int cmd_data_pending(struct ltt_session
*session
)
4106 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4107 struct ltt_ust_session
*usess
= session
->ust_session
;
4109 LTTNG_ASSERT(session
);
4111 DBG("Data pending for session %s", session
->name
);
4113 /* Session MUST be stopped to ask for data availability. */
4114 if (session
->active
) {
4115 ret
= LTTNG_ERR_SESSION_STARTED
;
4119 * If stopped, just make sure we've started before else the above call
4120 * will always send that there is data pending.
4122 * The consumer assumes that when the data pending command is received,
4123 * the trace has been started before or else no output data is written
4124 * by the streams which is a condition for data pending. So, this is
4125 * *VERY* important that we don't ask the consumer before a start
4128 if (!session
->has_been_started
) {
4134 /* A rotation is still pending, we have to wait. */
4135 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4136 DBG("Rotate still pending for session %s", session
->name
);
4141 if (ksess
&& ksess
->consumer
) {
4142 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4144 /* Data is still being extracted for the kernel. */
4149 if (usess
&& usess
->consumer
) {
4150 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4152 /* Data is still being extracted for the kernel. */
4157 /* Data is ready to be read by a viewer */
4165 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4167 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4169 int cmd_snapshot_add_output(struct ltt_session
*session
,
4170 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4173 struct snapshot_output
*new_output
;
4175 LTTNG_ASSERT(session
);
4176 LTTNG_ASSERT(output
);
4178 DBG("Cmd snapshot add output for session %s", session
->name
);
4181 * Can't create an output if the session is not set in no-output mode.
4183 if (session
->output_traces
) {
4184 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4188 if (session
->has_non_mmap_channel
) {
4189 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4193 /* Only one output is allowed until we have the "tee" feature. */
4194 if (session
->snapshot
.nb_output
== 1) {
4195 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4199 new_output
= snapshot_output_alloc();
4201 ret
= LTTNG_ERR_NOMEM
;
4205 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4206 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4207 &session
->snapshot
);
4209 if (ret
== -ENOMEM
) {
4210 ret
= LTTNG_ERR_NOMEM
;
4212 ret
= LTTNG_ERR_INVALID
;
4218 snapshot_add_output(&session
->snapshot
, new_output
);
4220 *id
= new_output
->id
;
4227 snapshot_output_destroy(new_output
);
4233 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4235 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4237 int cmd_snapshot_del_output(struct ltt_session
*session
,
4238 const struct lttng_snapshot_output
*output
)
4241 struct snapshot_output
*sout
= NULL
;
4243 LTTNG_ASSERT(session
);
4244 LTTNG_ASSERT(output
);
4249 * Permission denied to create an output if the session is not
4250 * set in no output mode.
4252 if (session
->output_traces
) {
4253 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4258 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4260 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4261 } else if (*output
->name
!= '\0') {
4262 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4264 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4267 ret
= LTTNG_ERR_INVALID
;
4271 snapshot_delete_output(&session
->snapshot
, sout
);
4272 snapshot_output_destroy(sout
);
4281 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4283 * If no output is available, outputs is untouched and 0 is returned.
4285 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4287 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4288 struct lttng_snapshot_output
**outputs
)
4291 struct lttng_snapshot_output
*list
= NULL
;
4292 struct lttng_ht_iter iter
;
4293 struct snapshot_output
*output
;
4295 LTTNG_ASSERT(session
);
4296 LTTNG_ASSERT(outputs
);
4298 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4301 * Permission denied to create an output if the session is not
4302 * set in no output mode.
4304 if (session
->output_traces
) {
4305 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4309 if (session
->snapshot
.nb_output
== 0) {
4314 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4316 ret
= -LTTNG_ERR_NOMEM
;
4320 /* Copy list from session to the new list object. */
4322 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4323 output
, node
.node
) {
4324 LTTNG_ASSERT(output
->consumer
);
4325 list
[idx
].id
= output
->id
;
4326 list
[idx
].max_size
= output
->max_size
;
4327 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4328 sizeof(list
[idx
].name
))) {
4329 ret
= -LTTNG_ERR_INVALID
;
4332 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4333 if (lttng_strncpy(list
[idx
].ctrl_url
,
4334 output
->consumer
->dst
.session_root_path
,
4335 sizeof(list
[idx
].ctrl_url
))) {
4336 ret
= -LTTNG_ERR_INVALID
;
4341 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4342 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4344 ret
= -LTTNG_ERR_NOMEM
;
4349 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4350 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4352 ret
= -LTTNG_ERR_NOMEM
;
4361 ret
= session
->snapshot
.nb_output
;
4370 * Check if we can regenerate the metadata for this session.
4371 * Only kernel, UST per-uid and non-live sessions are supported.
4373 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4376 int check_regenerate_metadata_support(struct ltt_session
*session
)
4380 LTTNG_ASSERT(session
);
4382 if (session
->live_timer
!= 0) {
4383 ret
= LTTNG_ERR_LIVE_SESSION
;
4386 if (!session
->active
) {
4387 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4390 if (session
->ust_session
) {
4391 switch (session
->ust_session
->buffer_type
) {
4392 case LTTNG_BUFFER_PER_UID
:
4394 case LTTNG_BUFFER_PER_PID
:
4395 ret
= LTTNG_ERR_PER_PID_SESSION
;
4399 ret
= LTTNG_ERR_UNK
;
4403 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4404 session
->consumer
->relay_minor_version
< 8) {
4405 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4415 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4417 * Ask the consumer to truncate the existing metadata file(s) and
4418 * then regenerate the metadata. Live and per-pid sessions are not
4419 * supported and return an error.
4421 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4423 int cmd_regenerate_metadata(struct ltt_session
*session
)
4427 LTTNG_ASSERT(session
);
4429 ret
= check_regenerate_metadata_support(session
);
4434 if (session
->kernel_session
) {
4435 ret
= kernctl_session_regenerate_metadata(
4436 session
->kernel_session
->fd
);
4438 ERR("Failed to regenerate the kernel metadata");
4443 if (session
->ust_session
) {
4444 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4446 ERR("Failed to regenerate the UST metadata");
4450 DBG("Cmd metadata regenerate for session %s", session
->name
);
4458 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4460 * Ask the tracer to regenerate a new statedump.
4462 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4464 int cmd_regenerate_statedump(struct ltt_session
*session
)
4468 LTTNG_ASSERT(session
);
4470 if (!session
->active
) {
4471 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4475 if (session
->kernel_session
) {
4476 ret
= kernctl_session_regenerate_statedump(
4477 session
->kernel_session
->fd
);
4479 * Currently, the statedump in kernel can only fail if out
4483 if (ret
== -ENOMEM
) {
4484 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4486 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4488 ERR("Failed to regenerate the kernel statedump");
4493 if (session
->ust_session
) {
4494 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4496 * Currently, the statedump in UST always returns 0.
4499 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4500 ERR("Failed to regenerate the UST statedump");
4504 DBG("Cmd regenerate statedump for session %s", session
->name
);
4512 enum lttng_error_code
synchronize_tracer_notifier_register(
4513 struct notification_thread_handle
*notification_thread
,
4514 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4516 enum lttng_error_code ret_code
;
4517 const struct lttng_condition
*condition
=
4518 lttng_trigger_get_const_condition(trigger
);
4519 const char *trigger_name
;
4520 uid_t trigger_owner
;
4521 enum lttng_trigger_status trigger_status
;
4522 const enum lttng_domain_type trigger_domain
=
4523 lttng_trigger_get_underlying_domain_type_restriction(
4526 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4527 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4529 LTTNG_ASSERT(condition
);
4530 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4531 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4533 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4534 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4535 trigger_name
: "(anonymous)";
4537 session_lock_list();
4538 switch (trigger_domain
) {
4539 case LTTNG_DOMAIN_KERNEL
:
4541 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4542 if (ret_code
!= LTTNG_OK
) {
4543 enum lttng_error_code notif_thread_unregister_ret
;
4545 notif_thread_unregister_ret
=
4546 notification_thread_command_unregister_trigger(
4547 notification_thread
, trigger
);
4549 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4550 /* Return the original error code. */
4551 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4553 (int) trigger_owner
,
4559 case LTTNG_DOMAIN_UST
:
4560 ust_app_global_update_all_event_notifier_rules();
4562 case LTTNG_DOMAIN_JUL
:
4563 case LTTNG_DOMAIN_LOG4J
:
4564 case LTTNG_DOMAIN_PYTHON
:
4566 /* Agent domains. */
4567 struct agent
*agt
= agent_find_by_event_notifier_domain(
4571 agt
= agent_create(trigger_domain
);
4573 ret_code
= LTTNG_ERR_NOMEM
;
4574 goto end_unlock_session_list
;
4577 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4580 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4581 if (ret_code
!= LTTNG_OK
) {
4582 goto end_unlock_session_list
;
4587 case LTTNG_DOMAIN_NONE
:
4592 ret_code
= LTTNG_OK
;
4593 end_unlock_session_list
:
4594 session_unlock_list();
4598 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4599 struct lttng_trigger
*trigger
,
4600 bool is_trigger_anonymous
,
4601 struct notification_thread_handle
*notification_thread
,
4602 struct lttng_trigger
**return_trigger
)
4604 enum lttng_error_code ret_code
;
4605 const char *trigger_name
;
4606 uid_t trigger_owner
;
4607 enum lttng_trigger_status trigger_status
;
4609 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4610 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4611 trigger_name
: "(anonymous)";
4613 trigger_status
= lttng_trigger_get_owner_uid(
4614 trigger
, &trigger_owner
);
4615 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4617 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4618 trigger_name
, (int) trigger_owner
,
4619 (int) lttng_credentials_get_uid(cmd_creds
));
4622 * Validate the trigger credentials against the command credentials.
4623 * Only the root user can register a trigger with non-matching
4626 if (!lttng_credentials_is_equal_uid(
4627 lttng_trigger_get_credentials(trigger
),
4629 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4630 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4631 trigger_name
, (int) trigger_owner
,
4632 (int) lttng_credentials_get_uid(cmd_creds
));
4633 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4639 * The bytecode generation also serves as a validation step for the
4640 * bytecode expressions.
4642 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4643 if (ret_code
!= LTTNG_OK
) {
4644 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4645 trigger_name
, (int) trigger_owner
, ret_code
);
4650 * A reference to the trigger is acquired by the notification thread.
4651 * It is safe to return the same trigger to the caller since it the
4652 * other user holds a reference.
4654 * The trigger is modified during the execution of the
4655 * "register trigger" command. However, by the time the command returns,
4656 * it is safe to use without any locking as its properties are
4659 ret_code
= notification_thread_command_register_trigger(
4660 notification_thread
, trigger
, is_trigger_anonymous
);
4661 if (ret_code
!= LTTNG_OK
) {
4662 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4663 trigger_name
, (int) trigger_owner
, ret_code
);
4667 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4668 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4669 trigger_name
: "(anonymous)";
4672 * Synchronize tracers if the trigger adds an event notifier.
4674 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4675 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4676 trigger
, cmd_creds
);
4677 if (ret_code
!= LTTNG_OK
) {
4678 ERR("Error registering tracer notifier: %s",
4679 lttng_strerror(-ret_code
));
4685 * Return an updated trigger to the client.
4687 * Since a modified version of the same trigger is returned, acquire a
4688 * reference to the trigger so the caller doesn't have to care if those
4689 * are distinct instances or not.
4691 if (ret_code
== LTTNG_OK
) {
4692 lttng_trigger_get(trigger
);
4693 *return_trigger
= trigger
;
4694 /* Ownership of trigger was transferred to caller. */
4702 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4703 const struct lttng_trigger
*trigger
)
4705 enum lttng_error_code ret_code
;
4706 const struct lttng_condition
*condition
=
4707 lttng_trigger_get_const_condition(trigger
);
4708 const enum lttng_domain_type trigger_domain
=
4709 lttng_trigger_get_underlying_domain_type_restriction(
4712 LTTNG_ASSERT(condition
);
4713 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4714 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4716 session_lock_list();
4717 switch (trigger_domain
) {
4718 case LTTNG_DOMAIN_KERNEL
:
4719 ret_code
= kernel_unregister_event_notifier(trigger
);
4720 if (ret_code
!= LTTNG_OK
) {
4721 goto end_unlock_session_list
;
4725 case LTTNG_DOMAIN_UST
:
4726 ust_app_global_update_all_event_notifier_rules();
4728 case LTTNG_DOMAIN_JUL
:
4729 case LTTNG_DOMAIN_LOG4J
:
4730 case LTTNG_DOMAIN_PYTHON
:
4732 /* Agent domains. */
4733 struct agent
*agt
= agent_find_by_event_notifier_domain(
4737 * This trigger was never registered in the first place. Calling
4738 * this function under those circumstances is an internal error.
4741 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4742 if (ret_code
!= LTTNG_OK
) {
4743 goto end_unlock_session_list
;
4748 case LTTNG_DOMAIN_NONE
:
4753 ret_code
= LTTNG_OK
;
4755 end_unlock_session_list
:
4756 session_unlock_list();
4760 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4761 const struct lttng_trigger
*trigger
,
4762 struct notification_thread_handle
*notification_thread
)
4764 enum lttng_error_code ret_code
;
4765 const char *trigger_name
;
4766 uid_t trigger_owner
;
4767 enum lttng_trigger_status trigger_status
;
4768 struct lttng_trigger
*sessiond_trigger
= NULL
;
4770 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4771 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4772 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4773 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4775 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4776 trigger_name
, (int) trigger_owner
,
4777 (int) lttng_credentials_get_uid(cmd_creds
));
4780 * Validate the trigger credentials against the command credentials.
4781 * Only the root user can unregister a trigger with non-matching
4784 if (!lttng_credentials_is_equal_uid(
4785 lttng_trigger_get_credentials(trigger
),
4787 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4788 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4789 trigger_name
, (int) trigger_owner
,
4790 (int) lttng_credentials_get_uid(cmd_creds
));
4791 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4796 /* Fetch the sessiond side trigger object. */
4797 ret_code
= notification_thread_command_get_trigger(
4798 notification_thread
, trigger
, &sessiond_trigger
);
4799 if (ret_code
!= LTTNG_OK
) {
4800 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4801 trigger_name
, (int) trigger_owner
, ret_code
);
4805 LTTNG_ASSERT(sessiond_trigger
);
4808 * From this point on, no matter what, consider the trigger
4811 * We set the unregistered state of the sessiond side trigger object in
4812 * the client thread since we want to minimize the possibility of the
4813 * notification thread being stalled due to a long execution of an
4814 * action that required the trigger lock.
4816 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4818 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4820 if (ret_code
!= LTTNG_OK
) {
4821 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4822 trigger_name
, (int) trigger_owner
, ret_code
);
4827 * Synchronize tracers if the trigger removes an event notifier.
4828 * Do this even if the trigger unregistration failed to at least stop
4829 * the tracers from producing notifications associated with this
4832 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4833 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4834 if (ret_code
!= LTTNG_OK
) {
4835 ERR("Error unregistering trigger to tracer.");
4842 lttng_trigger_put(sessiond_trigger
);
4846 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4847 struct notification_thread_handle
*notification_thread
,
4848 struct lttng_triggers
**return_triggers
)
4851 enum lttng_error_code ret_code
;
4852 struct lttng_triggers
*triggers
= NULL
;
4854 /* Get the set of triggers from the notification thread. */
4855 ret_code
= notification_thread_command_list_triggers(
4856 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4857 if (ret_code
!= LTTNG_OK
) {
4861 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4863 ret_code
= LTTNG_ERR_UNK
;
4867 *return_triggers
= triggers
;
4869 ret_code
= LTTNG_OK
;
4871 lttng_triggers_destroy(triggers
);
4875 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4876 const struct lttng_error_query
*query
,
4877 struct lttng_error_query_results
**_results
,
4878 struct notification_thread_handle
*notification_thread
)
4880 enum lttng_error_code ret_code
;
4881 const struct lttng_trigger
*query_target_trigger
;
4882 const struct lttng_action
*query_target_action
= NULL
;
4883 struct lttng_trigger
*matching_trigger
= NULL
;
4884 const char *trigger_name
;
4885 uid_t trigger_owner
;
4886 enum lttng_trigger_status trigger_status
;
4887 struct lttng_error_query_results
*results
= NULL
;
4889 switch (lttng_error_query_get_target_type(query
)) {
4890 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4891 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4893 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4894 query_target_trigger
=
4895 lttng_error_query_condition_borrow_target(query
);
4897 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4898 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4905 LTTNG_ASSERT(query_target_trigger
);
4907 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4908 query_target_trigger
, &matching_trigger
);
4909 if (ret_code
!= LTTNG_OK
) {
4913 /* No longer needed. */
4914 query_target_trigger
= NULL
;
4916 if (lttng_error_query_get_target_type(query
) ==
4917 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4918 /* Get the sessiond-side version of the target action. */
4919 query_target_action
=
4920 lttng_error_query_action_borrow_action_target(
4921 query
, matching_trigger
);
4924 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4925 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4926 trigger_name
: "(anonymous)";
4927 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4929 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4931 results
= lttng_error_query_results_create();
4933 ret_code
= LTTNG_ERR_NOMEM
;
4937 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4938 trigger_name
, (int) trigger_owner
,
4939 (int) lttng_credentials_get_uid(cmd_creds
));
4942 * Validate the trigger credentials against the command credentials.
4943 * Only the root user can target a trigger with non-matching
4946 if (!lttng_credentials_is_equal_uid(
4947 lttng_trigger_get_credentials(matching_trigger
),
4949 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4950 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4951 trigger_name
, (int) trigger_owner
,
4952 (int) lttng_credentials_get_uid(cmd_creds
));
4953 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4958 switch (lttng_error_query_get_target_type(query
)) {
4959 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4960 trigger_status
= lttng_trigger_add_error_results(
4961 matching_trigger
, results
);
4963 switch (trigger_status
) {
4964 case LTTNG_TRIGGER_STATUS_OK
:
4967 ret_code
= LTTNG_ERR_UNK
;
4972 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4974 trigger_status
= lttng_trigger_condition_add_error_results(
4975 matching_trigger
, results
);
4977 switch (trigger_status
) {
4978 case LTTNG_TRIGGER_STATUS_OK
:
4981 ret_code
= LTTNG_ERR_UNK
;
4987 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4989 const enum lttng_action_status action_status
=
4990 lttng_action_add_error_query_results(
4991 query_target_action
, results
);
4993 switch (action_status
) {
4994 case LTTNG_ACTION_STATUS_OK
:
4997 ret_code
= LTTNG_ERR_UNK
;
5008 *_results
= results
;
5010 ret_code
= LTTNG_OK
;
5012 lttng_trigger_put(matching_trigger
);
5013 lttng_error_query_results_destroy(results
);
5018 * Send relayd sockets from snapshot output to consumer. Ignore request if the
5019 * snapshot output is *not* set with a remote destination.
5021 * Return LTTNG_OK on success or a LTTNG_ERR code.
5023 static enum lttng_error_code
set_relayd_for_snapshot(
5024 struct consumer_output
*output
,
5025 const struct ltt_session
*session
)
5027 enum lttng_error_code status
= LTTNG_OK
;
5028 struct lttng_ht_iter iter
;
5029 struct consumer_socket
*socket
;
5030 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
5031 const char *base_path
;
5033 LTTNG_ASSERT(output
);
5034 LTTNG_ASSERT(session
);
5036 DBG2("Set relayd object from snapshot output");
5038 if (session
->current_trace_chunk
) {
5039 enum lttng_trace_chunk_status chunk_status
=
5040 lttng_trace_chunk_get_id(
5041 session
->current_trace_chunk
,
5042 ¤t_chunk_id
.value
);
5044 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5045 current_chunk_id
.is_set
= true;
5047 ERR("Failed to get current trace chunk id");
5048 status
= LTTNG_ERR_UNK
;
5053 /* Ignore if snapshot consumer output is not network. */
5054 if (output
->type
!= CONSUMER_DST_NET
) {
5059 * The snapshot record URI base path overrides the session
5062 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5063 base_path
= output
->dst
.net
.control
.subdir
;
5065 base_path
= session
->base_path
;
5069 * For each consumer socket, create and send the relayd object of the
5073 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
5074 socket
, node
.node
) {
5075 pthread_mutex_lock(socket
->lock
);
5076 status
= send_consumer_relayd_sockets(session
->id
,
5078 session
->name
, session
->hostname
,
5080 session
->live_timer
,
5081 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
5082 session
->creation_time
,
5083 session
->name_contains_creation_time
);
5084 pthread_mutex_unlock(socket
->lock
);
5085 if (status
!= LTTNG_OK
) {
5097 * Record a kernel snapshot.
5099 * Return LTTNG_OK on success or a LTTNG_ERR code.
5101 static enum lttng_error_code
record_kernel_snapshot(
5102 struct ltt_kernel_session
*ksess
,
5103 const struct consumer_output
*output
,
5104 const struct ltt_session
*session
,
5105 uint64_t nb_packets_per_stream
)
5107 enum lttng_error_code status
;
5109 LTTNG_ASSERT(ksess
);
5110 LTTNG_ASSERT(output
);
5111 LTTNG_ASSERT(session
);
5113 status
= kernel_snapshot_record(
5114 ksess
, output
, nb_packets_per_stream
);
5119 * Record a UST snapshot.
5121 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5123 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5124 const struct consumer_output
*output
,
5125 const struct ltt_session
*session
,
5126 uint64_t nb_packets_per_stream
)
5128 enum lttng_error_code status
;
5130 LTTNG_ASSERT(usess
);
5131 LTTNG_ASSERT(output
);
5132 LTTNG_ASSERT(session
);
5134 status
= ust_app_snapshot_record(
5135 usess
, output
, nb_packets_per_stream
);
5140 uint64_t get_session_size_one_more_packet_per_stream(
5141 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5143 uint64_t tot_size
= 0;
5145 if (session
->kernel_session
) {
5146 struct ltt_kernel_channel
*chan
;
5147 const struct ltt_kernel_session
*ksess
=
5148 session
->kernel_session
;
5150 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5151 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5153 * Don't take channel into account if we
5154 * already grab all its packets.
5158 tot_size
+= chan
->channel
->attr
.subbuf_size
5159 * chan
->stream_count
;
5163 if (session
->ust_session
) {
5164 const struct ltt_ust_session
*usess
= session
->ust_session
;
5166 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5174 * Calculate the number of packets we can grab from each stream that
5175 * fits within the overall snapshot max size.
5177 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5178 * the number of packets per stream.
5180 * TODO: this approach is not perfect: we consider the worse case
5181 * (packet filling the sub-buffers) as an upper bound, but we could do
5182 * better if we do this calculation while we actually grab the packet
5183 * content: we would know how much padding we don't actually store into
5186 * This algorithm is currently bounded by the number of packets per
5189 * Since we call this algorithm before actually grabbing the data, it's
5190 * an approximation: for instance, applications could appear/disappear
5191 * in between this call and actually grabbing data.
5194 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5198 uint64_t cur_nb_packets
= 0;
5201 return 0; /* Infinite */
5204 size_left
= max_size
;
5206 uint64_t one_more_packet_tot_size
;
5208 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5209 session
, cur_nb_packets
);
5210 if (!one_more_packet_tot_size
) {
5211 /* We are already grabbing all packets. */
5214 size_left
-= one_more_packet_tot_size
;
5215 if (size_left
< 0) {
5220 if (!cur_nb_packets
&& size_left
!= max_size
) {
5221 /* Not enough room to grab one packet of each stream, error. */
5224 return cur_nb_packets
;
5228 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5229 const struct snapshot_output
*snapshot_output
)
5231 int64_t nb_packets_per_stream
;
5232 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5234 enum lttng_error_code ret_code
= LTTNG_OK
;
5235 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5236 struct consumer_output
*original_ust_consumer_output
= NULL
;
5237 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5238 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5239 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5241 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5243 snapshot_output
->name
,
5244 snapshot_output
->datetime
,
5245 snapshot_output
->nb_snapshot
);
5246 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5247 ERR("Failed to format snapshot name");
5248 ret_code
= LTTNG_ERR_INVALID
;
5251 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5252 snapshot_output
->name
, session
->name
,
5253 snapshot_chunk_name
);
5254 if (!session
->kernel_session
&& !session
->ust_session
) {
5255 ERR("Failed to record snapshot as no channels exist");
5256 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5260 if (session
->kernel_session
) {
5261 original_kernel_consumer_output
=
5262 session
->kernel_session
->consumer
;
5263 snapshot_kernel_consumer_output
=
5264 consumer_copy_output(snapshot_output
->consumer
);
5265 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5266 snapshot_chunk_name
);
5268 /* Copy the original domain subdir. */
5269 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5270 original_kernel_consumer_output
->domain_subdir
);
5272 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5273 original_kernel_consumer_output
);
5275 ERR("Failed to copy consumer sockets from snapshot output configuration");
5276 ret_code
= LTTNG_ERR_NOMEM
;
5279 ret_code
= set_relayd_for_snapshot(
5280 snapshot_kernel_consumer_output
, session
);
5281 if (ret_code
!= LTTNG_OK
) {
5282 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5285 session
->kernel_session
->consumer
=
5286 snapshot_kernel_consumer_output
;
5288 if (session
->ust_session
) {
5289 original_ust_consumer_output
= session
->ust_session
->consumer
;
5290 snapshot_ust_consumer_output
=
5291 consumer_copy_output(snapshot_output
->consumer
);
5292 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5293 snapshot_chunk_name
);
5295 /* Copy the original domain subdir. */
5296 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5297 original_ust_consumer_output
->domain_subdir
);
5299 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5300 original_ust_consumer_output
);
5302 ERR("Failed to copy consumer sockets from snapshot output configuration");
5303 ret_code
= LTTNG_ERR_NOMEM
;
5306 ret_code
= set_relayd_for_snapshot(
5307 snapshot_ust_consumer_output
, session
);
5308 if (ret_code
!= LTTNG_OK
) {
5309 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5312 session
->ust_session
->consumer
=
5313 snapshot_ust_consumer_output
;
5316 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5317 snapshot_kernel_consumer_output
?:
5318 snapshot_ust_consumer_output
,
5319 consumer_output_get_base_path(
5320 snapshot_output
->consumer
),
5321 snapshot_chunk_name
);
5322 if (!snapshot_trace_chunk
) {
5323 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5325 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5328 LTTNG_ASSERT(!session
->current_trace_chunk
);
5329 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5330 lttng_trace_chunk_put(snapshot_trace_chunk
);
5331 snapshot_trace_chunk
= NULL
;
5333 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5335 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5339 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5340 snapshot_output
->max_size
);
5341 if (nb_packets_per_stream
< 0) {
5342 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5343 goto error_close_trace_chunk
;
5346 if (session
->kernel_session
) {
5347 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5348 snapshot_kernel_consumer_output
, session
,
5349 nb_packets_per_stream
);
5350 if (ret_code
!= LTTNG_OK
) {
5351 goto error_close_trace_chunk
;
5355 if (session
->ust_session
) {
5356 ret_code
= record_ust_snapshot(session
->ust_session
,
5357 snapshot_ust_consumer_output
, session
,
5358 nb_packets_per_stream
);
5359 if (ret_code
!= LTTNG_OK
) {
5360 goto error_close_trace_chunk
;
5364 error_close_trace_chunk
:
5365 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5366 ERR("Failed to release the current trace chunk of session \"%s\"",
5368 ret_code
= LTTNG_ERR_UNK
;
5371 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5372 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5374 * Don't goto end; make sure the chunk is closed for the session
5375 * to allow future snapshots.
5377 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5379 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5382 lttng_trace_chunk_put(snapshot_trace_chunk
);
5383 snapshot_trace_chunk
= NULL
;
5385 if (original_ust_consumer_output
) {
5386 session
->ust_session
->consumer
= original_ust_consumer_output
;
5388 if (original_kernel_consumer_output
) {
5389 session
->kernel_session
->consumer
=
5390 original_kernel_consumer_output
;
5392 consumer_output_put(snapshot_ust_consumer_output
);
5393 consumer_output_put(snapshot_kernel_consumer_output
);
5398 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5400 * The wait parameter is ignored so this call always wait for the snapshot to
5401 * complete before returning.
5403 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5405 int cmd_snapshot_record(struct ltt_session
*session
,
5406 const struct lttng_snapshot_output
*output
,
5407 int wait
__attribute__((unused
)))
5409 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5411 unsigned int snapshot_success
= 0;
5413 struct snapshot_output
*tmp_output
= NULL
;
5415 LTTNG_ASSERT(session
);
5416 LTTNG_ASSERT(output
);
5418 DBG("Cmd snapshot record for session %s", session
->name
);
5420 /* Get the datetime for the snapshot output directory. */
5421 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5424 cmd_ret
= LTTNG_ERR_INVALID
;
5429 * Permission denied to create an output if the session is not
5430 * set in no output mode.
5432 if (session
->output_traces
) {
5433 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5437 /* The session needs to be started at least once. */
5438 if (!session
->has_been_started
) {
5439 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5443 /* Use temporary output for the session. */
5444 if (*output
->ctrl_url
!= '\0') {
5445 tmp_output
= snapshot_output_alloc();
5447 cmd_ret
= LTTNG_ERR_NOMEM
;
5451 ret
= snapshot_output_init(session
, output
->max_size
,
5453 output
->ctrl_url
, output
->data_url
,
5457 if (ret
== -ENOMEM
) {
5458 cmd_ret
= LTTNG_ERR_NOMEM
;
5460 cmd_ret
= LTTNG_ERR_INVALID
;
5464 /* Use the global session count for the temporary snapshot. */
5465 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5467 /* Use the global datetime */
5468 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5469 cmd_ret
= snapshot_record(session
, tmp_output
);
5470 if (cmd_ret
!= LTTNG_OK
) {
5473 snapshot_success
= 1;
5475 struct snapshot_output
*sout
;
5476 struct lttng_ht_iter iter
;
5479 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5480 &iter
.iter
, sout
, node
.node
) {
5481 struct snapshot_output output_copy
;
5484 * Make a local copy of the output and override output
5485 * parameters with those provided as part of the
5488 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5490 if (output
->max_size
!= (uint64_t) -1ULL) {
5491 output_copy
.max_size
= output
->max_size
;
5494 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5495 memcpy(output_copy
.datetime
, datetime
,
5498 /* Use temporary name. */
5499 if (*output
->name
!= '\0') {
5500 if (lttng_strncpy(output_copy
.name
,
5502 sizeof(output_copy
.name
))) {
5503 cmd_ret
= LTTNG_ERR_INVALID
;
5509 cmd_ret
= snapshot_record(session
, &output_copy
);
5510 if (cmd_ret
!= LTTNG_OK
) {
5514 snapshot_success
= 1;
5519 if (snapshot_success
) {
5520 session
->snapshot
.nb_snapshot
++;
5522 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5527 snapshot_output_destroy(tmp_output
);
5533 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5535 int cmd_set_session_shm_path(struct ltt_session
*session
,
5536 const char *shm_path
)
5539 LTTNG_ASSERT(session
);
5542 * Can only set shm path before session is started.
5544 if (session
->has_been_started
) {
5545 return LTTNG_ERR_SESSION_STARTED
;
5548 strncpy(session
->shm_path
, shm_path
,
5549 sizeof(session
->shm_path
));
5550 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5556 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5558 * Ask the consumer to rotate the session output directory.
5559 * The session lock must be held.
5561 * Returns LTTNG_OK on success or else a negative LTTng error code.
5563 int cmd_rotate_session(struct ltt_session
*session
,
5564 struct lttng_rotate_session_return
*rotate_return
,
5565 bool quiet_rotation
,
5566 enum lttng_trace_chunk_command_type command
)
5569 uint64_t ongoing_rotation_chunk_id
;
5570 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5571 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5572 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5573 enum lttng_trace_chunk_status chunk_status
;
5574 bool failed_to_rotate
= false;
5575 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5577 LTTNG_ASSERT(session
);
5579 if (!session
->has_been_started
) {
5580 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5585 * Explicit rotation is not supported for live sessions.
5586 * However, live sessions can perform a quiet rotation on
5588 * Rotation is not supported for snapshot traces (no output).
5590 if ((!quiet_rotation
&& session
->live_timer
) ||
5591 !session
->output_traces
) {
5592 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5596 /* Unsupported feature in lttng-relayd before 2.11. */
5597 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5598 (session
->consumer
->relay_major_version
== 2 &&
5599 session
->consumer
->relay_minor_version
< 11)) {
5600 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5604 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5605 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5606 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5610 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5611 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5613 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5618 * After a stop, we only allow one rotation to occur, the other ones are
5619 * useless until a new start.
5621 if (session
->rotated_after_last_stop
) {
5622 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5624 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5629 * After a stop followed by a clear, disallow following rotations a they would
5630 * generate empty chunks.
5632 if (session
->cleared_after_last_stop
) {
5633 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5635 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5639 if (session
->active
) {
5640 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5642 if (!new_trace_chunk
) {
5643 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5649 * The current trace chunk becomes the chunk being archived.
5651 * After this point, "chunk_being_archived" must absolutely
5652 * be closed on the consumer(s), otherwise it will never be
5653 * cleaned-up, which will result in a leak.
5655 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5656 &chunk_being_archived
);
5658 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5662 if (session
->kernel_session
) {
5663 cmd_ret
= kernel_rotate_session(session
);
5664 if (cmd_ret
!= LTTNG_OK
) {
5665 failed_to_rotate
= true;
5666 rotation_fail_code
= cmd_ret
;
5669 if (session
->ust_session
) {
5670 cmd_ret
= ust_app_rotate_session(session
);
5671 if (cmd_ret
!= LTTNG_OK
) {
5672 failed_to_rotate
= true;
5673 rotation_fail_code
= cmd_ret
;
5677 if (!session
->active
) {
5678 session
->rotated_after_last_stop
= true;
5681 if (!chunk_being_archived
) {
5682 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5684 if (failed_to_rotate
) {
5685 cmd_ret
= rotation_fail_code
;
5692 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5693 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5694 &ongoing_rotation_chunk_id
);
5695 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5697 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5698 command
, session
->last_chunk_path
);
5700 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5704 if (failed_to_rotate
) {
5705 cmd_ret
= rotation_fail_code
;
5709 session
->quiet_rotation
= quiet_rotation
;
5710 ret
= timer_session_rotation_pending_check_start(session
,
5711 DEFAULT_ROTATE_PENDING_TIMER
);
5713 cmd_ret
= LTTNG_ERR_UNK
;
5717 if (rotate_return
) {
5718 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5721 session
->chunk_being_archived
= chunk_being_archived
;
5722 chunk_being_archived
= NULL
;
5723 if (!quiet_rotation
) {
5724 ret
= notification_thread_command_session_rotation_ongoing(
5725 the_notification_thread_handle
, session
->id
,
5726 ongoing_rotation_chunk_id
);
5727 if (ret
!= LTTNG_OK
) {
5728 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5730 cmd_ret
= (lttng_error_code
) ret
;
5734 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5735 session
->name
, ongoing_rotation_chunk_id
);
5737 lttng_trace_chunk_put(new_trace_chunk
);
5738 lttng_trace_chunk_put(chunk_being_archived
);
5739 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5742 if (session_reset_rotation_state(session
,
5743 LTTNG_ROTATION_STATE_ERROR
)) {
5744 ERR("Failed to reset rotation state of session \"%s\"",
5751 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5753 * Check if the session has finished its rotation.
5755 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5757 int cmd_rotate_get_info(struct ltt_session
*session
,
5758 struct lttng_rotation_get_info_return
*info_return
,
5759 uint64_t rotation_id
)
5761 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5762 enum lttng_rotation_state rotation_state
;
5764 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5765 session
->most_recent_chunk_id
.value
);
5767 if (session
->chunk_being_archived
) {
5768 enum lttng_trace_chunk_status chunk_status
;
5771 chunk_status
= lttng_trace_chunk_get_id(
5772 session
->chunk_being_archived
,
5774 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5776 rotation_state
= rotation_id
== chunk_id
?
5777 LTTNG_ROTATION_STATE_ONGOING
:
5778 LTTNG_ROTATION_STATE_EXPIRED
;
5780 if (session
->last_archived_chunk_id
.is_set
&&
5781 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5782 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5784 rotation_state
= session
->rotation_state
;
5788 switch (rotation_state
) {
5789 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5790 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5793 case LTTNG_ROTATION_STATE_EXPIRED
:
5794 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5795 rotation_id
, session
->name
);
5797 case LTTNG_ROTATION_STATE_ONGOING
:
5798 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5799 rotation_id
, session
->name
);
5801 case LTTNG_ROTATION_STATE_COMPLETED
:
5805 char *current_tracing_path_reply
;
5806 size_t current_tracing_path_reply_len
;
5808 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5809 rotation_id
, session
->name
);
5811 switch (session_get_consumer_destination_type(session
)) {
5812 case CONSUMER_DST_LOCAL
:
5813 current_tracing_path_reply
=
5814 info_return
->location
.local
.absolute_path
;
5815 current_tracing_path_reply_len
=
5816 sizeof(info_return
->location
.local
.absolute_path
);
5817 info_return
->location_type
=
5818 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5819 fmt_ret
= asprintf(&chunk_path
,
5820 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5821 session_get_base_path(session
),
5822 session
->last_archived_chunk_name
);
5823 if (fmt_ret
== -1) {
5824 PERROR("Failed to format the path of the last archived trace chunk");
5825 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5826 cmd_ret
= LTTNG_ERR_UNK
;
5830 case CONSUMER_DST_NET
:
5832 uint16_t ctrl_port
, data_port
;
5834 current_tracing_path_reply
=
5835 info_return
->location
.relay
.relative_path
;
5836 current_tracing_path_reply_len
=
5837 sizeof(info_return
->location
.relay
.relative_path
);
5838 /* Currently the only supported relay protocol. */
5839 info_return
->location
.relay
.protocol
=
5840 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5842 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5843 session_get_net_consumer_hostname(session
),
5844 sizeof(info_return
->location
.relay
.host
));
5846 ERR("Failed to copy host name to rotate_get_info reply");
5847 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5848 cmd_ret
= LTTNG_ERR_SET_URL
;
5852 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5853 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5854 info_return
->location
.relay
.ports
.data
= data_port
;
5855 info_return
->location_type
=
5856 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5857 chunk_path
= strdup(session
->last_chunk_path
);
5859 ERR("Failed to allocate the path of the last archived trace chunk");
5860 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5861 cmd_ret
= LTTNG_ERR_UNK
;
5870 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5871 chunk_path
, current_tracing_path_reply_len
);
5874 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5875 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5876 cmd_ret
= LTTNG_ERR_UNK
;
5882 case LTTNG_ROTATION_STATE_ERROR
:
5883 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5884 rotation_id
, session
->name
);
5892 info_return
->status
= (int32_t) rotation_state
;
5897 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5899 * Configure the automatic rotation parameters.
5900 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5901 * 'activate' to false means deactivate the rotation schedule and validate that
5902 * 'new_value' has the same value as the currently active value.
5904 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5906 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5907 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5909 struct notification_thread_handle
*notification_thread_handle
)
5912 uint64_t *parameter_value
;
5914 LTTNG_ASSERT(session
);
5916 DBG("Cmd rotate set schedule session %s", session
->name
);
5918 if (session
->live_timer
|| !session
->output_traces
) {
5919 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5920 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5924 switch (schedule_type
) {
5925 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5926 parameter_value
= &session
->rotate_size
;
5928 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5929 parameter_value
= &session
->rotate_timer_period
;
5930 if (new_value
>= UINT_MAX
) {
5931 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5932 new_value
, UINT_MAX
);
5933 ret
= LTTNG_ERR_INVALID
;
5938 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5939 ret
= LTTNG_ERR_INVALID
;
5943 /* Improper use of the API. */
5944 if (new_value
== -1ULL) {
5945 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5946 ret
= LTTNG_ERR_INVALID
;
5951 * As indicated in struct ltt_session's comments, a value of == 0 means
5952 * this schedule rotation type is not in use.
5954 * Reject the command if we were asked to activate a schedule that was
5957 if (activate
&& *parameter_value
!= 0) {
5958 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5959 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5964 * Reject the command if we were asked to deactivate a schedule that was
5967 if (!activate
&& *parameter_value
== 0) {
5968 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5969 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5974 * Reject the command if we were asked to deactivate a schedule that
5977 if (!activate
&& *parameter_value
!= new_value
) {
5978 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5979 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5983 *parameter_value
= activate
? new_value
: 0;
5985 switch (schedule_type
) {
5986 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5987 if (activate
&& session
->active
) {
5989 * Only start the timer if the session is active,
5990 * otherwise it will be started when the session starts.
5992 ret
= timer_session_rotation_schedule_timer_start(
5993 session
, new_value
);
5995 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5996 ret
= LTTNG_ERR_UNK
;
6000 ret
= timer_session_rotation_schedule_timer_stop(
6003 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
6004 ret
= LTTNG_ERR_UNK
;
6009 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
6011 ret
= subscribe_session_consumed_size_rotation(session
,
6012 new_value
, notification_thread_handle
);
6014 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
6015 ret
= LTTNG_ERR_UNK
;
6019 ret
= unsubscribe_session_consumed_size_rotation(session
,
6020 notification_thread_handle
);
6022 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
6023 ret
= LTTNG_ERR_UNK
;
6030 /* Would have been caught before. */
6042 /* Wait for a given path to be removed before continuing. */
6043 static enum lttng_error_code
wait_on_path(void *path_data
)
6045 const char *shm_path
= (const char *) path_data
;
6047 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6053 ret
= stat(shm_path
, &st
);
6055 if (errno
!= ENOENT
) {
6056 PERROR("stat() returned an error while checking for the existence of the shm path");
6058 DBG("shm path no longer exists, completing the destruction of session");
6062 if (!S_ISDIR(st
.st_mode
)) {
6063 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6068 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6074 * Returns a pointer to a handler to run on completion of a command.
6075 * Returns NULL if no handler has to be run for the last command executed.
6077 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6079 struct cmd_completion_handler
*handler
= current_completion_handler
;
6081 current_completion_handler
= NULL
;
6086 * Init command subsystem.
6091 * Set network sequence index to 1 for streams to match a relayd
6092 * socket on the consumer side.
6094 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6095 relayd_net_seq_idx
= 1;
6096 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6098 DBG("Command subsystem initialized");