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 enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
765 struct lttcomm_relayd_sock
**relayd_sock
,
766 struct consumer_output
*consumer
)
769 enum lttng_error_code status
= LTTNG_OK
;
770 struct lttcomm_relayd_sock
*rsock
;
772 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
773 RELAYD_VERSION_COMM_MINOR
);
775 status
= LTTNG_ERR_FATAL
;
780 * Connect to relayd so we can proceed with a session creation. This call
781 * can possibly block for an arbitrary amount of time to set the health
782 * state to be in poll execution.
785 ret
= relayd_connect(rsock
);
788 ERR("Unable to reach lttng-relayd");
789 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
793 /* Create socket for control stream. */
794 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
795 uint64_t result_flags
;
797 DBG3("Creating relayd stream socket from URI");
799 /* Check relayd version */
800 ret
= relayd_version_check(rsock
);
801 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
802 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
804 } else if (ret
< 0) {
805 ERR("Unable to reach lttng-relayd");
806 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
809 consumer
->relay_major_version
= rsock
->major
;
810 consumer
->relay_minor_version
= rsock
->minor
;
811 ret
= relayd_get_configuration(rsock
, 0,
814 ERR("Unable to get relayd configuration");
815 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
818 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
819 consumer
->relay_allows_clear
= true;
821 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
822 DBG3("Creating relayd data socket from URI");
824 /* Command is not valid */
825 ERR("Relayd invalid stream type: %d", uri
->stype
);
826 status
= LTTNG_ERR_INVALID
;
830 *relayd_sock
= rsock
;
835 /* The returned value is not useful since we are on an error path. */
836 (void) relayd_close(rsock
);
844 * Connect to the relayd using URI and send the socket to the right consumer.
846 * The consumer socket lock must be held by the caller.
848 * Returns LTTNG_OK on success or an LTTng error code on failure.
850 static enum lttng_error_code
send_consumer_relayd_socket(
851 unsigned int session_id
,
852 struct lttng_uri
*relayd_uri
,
853 struct consumer_output
*consumer
,
854 struct consumer_socket
*consumer_sock
,
855 const char *session_name
, const char *hostname
,
856 const char *base_path
, int session_live_timer
,
857 const uint64_t *current_chunk_id
,
858 time_t session_creation_time
,
859 bool session_name_contains_creation_time
)
862 struct lttcomm_relayd_sock
*rsock
= NULL
;
863 enum lttng_error_code status
;
865 /* Connect to relayd and make version check if uri is the control. */
866 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
867 if (status
!= LTTNG_OK
) {
868 goto relayd_comm_error
;
872 /* Set the network sequence index if not set. */
873 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
874 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
876 * Increment net_seq_idx because we are about to transfer the
877 * new relayd socket to the consumer.
878 * Assign unique key so the consumer can match streams.
880 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
881 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
884 /* Send relayd socket to consumer. */
885 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
886 relayd_uri
->stype
, session_id
,
887 session_name
, hostname
, base_path
,
888 session_live_timer
, current_chunk_id
,
889 session_creation_time
, session_name_contains_creation_time
);
891 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
895 /* Flag that the corresponding socket was sent. */
896 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
897 consumer_sock
->control_sock_sent
= 1;
898 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
899 consumer_sock
->data_sock_sent
= 1;
903 * Close socket which was dup on the consumer side. The session daemon does
904 * NOT keep track of the relayd socket(s) once transfer to the consumer.
908 if (status
!= LTTNG_OK
) {
910 * The consumer output for this session should not be used anymore
911 * since the relayd connection failed thus making any tracing or/and
912 * streaming not usable.
914 consumer
->enabled
= 0;
916 (void) relayd_close(rsock
);
924 * Send both relayd sockets to a specific consumer and domain. This is a
925 * helper function to facilitate sending the information to the consumer for a
928 * The consumer socket lock must be held by the caller.
930 * Returns LTTNG_OK, or an LTTng error code on failure.
932 static enum lttng_error_code
send_consumer_relayd_sockets(
933 unsigned int session_id
, struct consumer_output
*consumer
,
934 struct consumer_socket
*sock
, const char *session_name
,
935 const char *hostname
, const char *base_path
, int session_live_timer
,
936 const uint64_t *current_chunk_id
, time_t session_creation_time
,
937 bool session_name_contains_creation_time
)
939 enum lttng_error_code status
= LTTNG_OK
;
941 LTTNG_ASSERT(consumer
);
944 /* Sending control relayd socket. */
945 if (!sock
->control_sock_sent
) {
946 status
= send_consumer_relayd_socket(session_id
,
947 &consumer
->dst
.net
.control
, consumer
, sock
,
948 session_name
, hostname
, base_path
, session_live_timer
,
949 current_chunk_id
, session_creation_time
,
950 session_name_contains_creation_time
);
951 if (status
!= LTTNG_OK
) {
956 /* Sending data relayd socket. */
957 if (!sock
->data_sock_sent
) {
958 status
= send_consumer_relayd_socket(session_id
,
959 &consumer
->dst
.net
.data
, consumer
, sock
,
960 session_name
, hostname
, base_path
, session_live_timer
,
961 current_chunk_id
, session_creation_time
,
962 session_name_contains_creation_time
);
963 if (status
!= LTTNG_OK
) {
973 * Setup relayd connections for a tracing session. First creates the socket to
974 * the relayd and send them to the right domain consumer. Consumer type MUST be
977 int cmd_setup_relayd(struct ltt_session
*session
)
980 struct ltt_ust_session
*usess
;
981 struct ltt_kernel_session
*ksess
;
982 struct consumer_socket
*socket
;
983 struct lttng_ht_iter iter
;
984 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
986 LTTNG_ASSERT(session
);
988 usess
= session
->ust_session
;
989 ksess
= session
->kernel_session
;
991 DBG("Setting relayd for session %s", session
->name
);
994 if (session
->current_trace_chunk
) {
995 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
996 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
998 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
999 current_chunk_id
.is_set
= true;
1001 ERR("Failed to get current trace chunk id");
1002 ret
= LTTNG_ERR_UNK
;
1007 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1008 && usess
->consumer
->enabled
) {
1009 /* For each consumer socket, send relayd sockets */
1010 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1011 socket
, node
.node
) {
1012 pthread_mutex_lock(socket
->lock
);
1013 ret
= send_consumer_relayd_sockets(session
->id
,
1014 usess
->consumer
, socket
,
1015 session
->name
, session
->hostname
,
1017 session
->live_timer
,
1018 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1019 session
->creation_time
,
1020 session
->name_contains_creation_time
);
1021 pthread_mutex_unlock(socket
->lock
);
1022 if (ret
!= LTTNG_OK
) {
1025 /* Session is now ready for network streaming. */
1026 session
->net_handle
= 1;
1028 session
->consumer
->relay_major_version
=
1029 usess
->consumer
->relay_major_version
;
1030 session
->consumer
->relay_minor_version
=
1031 usess
->consumer
->relay_minor_version
;
1032 session
->consumer
->relay_allows_clear
=
1033 usess
->consumer
->relay_allows_clear
;
1036 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1037 && ksess
->consumer
->enabled
) {
1038 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1039 socket
, node
.node
) {
1040 pthread_mutex_lock(socket
->lock
);
1041 ret
= send_consumer_relayd_sockets(session
->id
,
1042 ksess
->consumer
, socket
,
1043 session
->name
, session
->hostname
,
1045 session
->live_timer
,
1046 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1047 session
->creation_time
,
1048 session
->name_contains_creation_time
);
1049 pthread_mutex_unlock(socket
->lock
);
1050 if (ret
!= LTTNG_OK
) {
1053 /* Session is now ready for network streaming. */
1054 session
->net_handle
= 1;
1056 session
->consumer
->relay_major_version
=
1057 ksess
->consumer
->relay_major_version
;
1058 session
->consumer
->relay_minor_version
=
1059 ksess
->consumer
->relay_minor_version
;
1060 session
->consumer
->relay_allows_clear
=
1061 ksess
->consumer
->relay_allows_clear
;
1070 * Start a kernel session by opening all necessary streams.
1072 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1075 struct ltt_kernel_channel
*kchan
;
1077 /* Open kernel metadata */
1078 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1079 ret
= kernel_open_metadata(ksess
);
1081 ret
= LTTNG_ERR_KERN_META_FAIL
;
1086 /* Open kernel metadata stream */
1087 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1088 ret
= kernel_open_metadata_stream(ksess
);
1090 ERR("Kernel create metadata stream failed");
1091 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1096 /* For each channel */
1097 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1098 if (kchan
->stream_count
== 0) {
1099 ret
= kernel_open_channel_stream(kchan
);
1101 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1104 /* Update the stream global counter */
1105 ksess
->stream_count_global
+= ret
;
1109 /* Setup kernel consumer socket and send fds to it */
1110 ret
= init_kernel_tracing(ksess
);
1112 ret
= LTTNG_ERR_KERN_START_FAIL
;
1116 /* This start the kernel tracing */
1117 ret
= kernel_start_session(ksess
);
1119 ret
= LTTNG_ERR_KERN_START_FAIL
;
1123 /* Quiescent wait after starting trace */
1124 kernel_wait_quiescent();
1134 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1136 struct ltt_kernel_channel
*kchan
;
1137 bool error_occurred
= false;
1140 if (!ksess
|| !ksess
->active
) {
1143 DBG("Stopping kernel tracing");
1145 ret
= kernel_stop_session(ksess
);
1147 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1151 kernel_wait_quiescent();
1153 /* Flush metadata after stopping (if exists) */
1154 if (ksess
->metadata_stream_fd
>= 0) {
1155 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1157 ERR("Kernel metadata flush failed");
1158 error_occurred
= true;
1162 /* Flush all buffers after stopping */
1163 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1164 ret
= kernel_flush_buffer(kchan
);
1166 ERR("Kernel flush buffer error");
1167 error_occurred
= true;
1172 if (error_occurred
) {
1173 ret
= LTTNG_ERR_UNK
;
1182 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1184 int cmd_disable_channel(struct ltt_session
*session
,
1185 enum lttng_domain_type domain
, char *channel_name
)
1188 struct ltt_ust_session
*usess
;
1190 usess
= session
->ust_session
;
1195 case LTTNG_DOMAIN_KERNEL
:
1197 ret
= channel_kernel_disable(session
->kernel_session
,
1199 if (ret
!= LTTNG_OK
) {
1203 kernel_wait_quiescent();
1206 case LTTNG_DOMAIN_UST
:
1208 struct ltt_ust_channel
*uchan
;
1209 struct lttng_ht
*chan_ht
;
1211 chan_ht
= usess
->domain_global
.channels
;
1213 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1214 if (uchan
== NULL
) {
1215 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1219 ret
= channel_ust_disable(usess
, uchan
);
1220 if (ret
!= LTTNG_OK
) {
1226 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1238 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1240 * The wpipe arguments is used as a notifier for the kernel thread.
1242 int cmd_enable_channel(struct command_ctx
*cmd_ctx
, int sock
, int wpipe
)
1246 ssize_t sock_recv_len
;
1247 struct lttng_channel
*channel
= NULL
;
1248 struct lttng_buffer_view view
;
1249 struct lttng_dynamic_buffer channel_buffer
;
1250 const struct lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
1252 lttng_dynamic_buffer_init(&channel_buffer
);
1253 channel_len
= (size_t) cmd_ctx
->lsm
.u
.channel
.length
;
1254 ret
= lttng_dynamic_buffer_set_size(&channel_buffer
, channel_len
);
1256 ret
= LTTNG_ERR_NOMEM
;
1260 sock_recv_len
= lttcomm_recv_unix_sock(sock
, channel_buffer
.data
,
1262 if (sock_recv_len
< 0 || sock_recv_len
!= channel_len
) {
1263 ERR("Failed to receive \"enable channel\" command payload");
1264 ret
= LTTNG_ERR_INVALID
;
1268 view
= lttng_buffer_view_from_dynamic_buffer(&channel_buffer
, 0, channel_len
);
1269 if (!lttng_buffer_view_is_valid(&view
)) {
1270 ret
= LTTNG_ERR_INVALID
;
1274 if (lttng_channel_create_from_buffer(&view
, &channel
) != channel_len
) {
1275 ERR("Invalid channel payload received in \"enable channel\" command");
1276 ret
= LTTNG_ERR_INVALID
;
1280 ret
= cmd_enable_channel_internal(
1281 cmd_ctx
->session
, &command_domain
, channel
, wpipe
);
1284 lttng_dynamic_buffer_reset(&channel_buffer
);
1285 lttng_channel_destroy(channel
);
1289 static enum lttng_error_code
kernel_domain_check_trace_format_requirements(
1290 const lttng::trace_format_descriptor
& descriptor
)
1292 enum lttng_error_code ret_code
;
1293 switch (descriptor
.type()) {
1294 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_1
:
1295 /* Supported by all kernel tracer. */
1296 ret_code
= LTTNG_OK
;
1298 case LTTNG_TRACE_FORMAT_DESCRIPTOR_TYPE_CTF_2
:
1299 if (!kernel_supports_ctf2()) {
1300 ret_code
= LTTNG_ERR_TRACE_FORMAT_UNSUPPORTED_KERNEL_TRACER
;
1306 static enum lttng_error_code
cmd_enable_channel_internal(
1307 struct ltt_session
*session
,
1308 const struct lttng_domain
*domain
,
1309 const struct lttng_channel
*_attr
,
1312 enum lttng_error_code ret_code
;
1313 struct ltt_ust_session
*usess
= session
->ust_session
;
1314 struct lttng_ht
*chan_ht
;
1316 struct lttng_channel
*attr
= NULL
;
1318 LTTNG_ASSERT(session
);
1319 LTTNG_ASSERT(_attr
);
1320 LTTNG_ASSERT(domain
);
1322 attr
= lttng_channel_copy(_attr
);
1324 ret_code
= LTTNG_ERR_NOMEM
;
1328 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1330 /* Validate channel name */
1331 if (attr
->name
[0] == '.' ||
1332 memchr(attr
->name
, '/', len
) != NULL
) {
1333 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1337 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1342 * If the session is a live session, remove the switch timer, the
1343 * live timer does the same thing but sends also synchronisation
1344 * beacons for inactive streams.
1346 if (session
->live_timer
> 0) {
1347 attr
->attr
.live_timer_interval
= session
->live_timer
;
1348 attr
->attr
.switch_timer_interval
= 0;
1351 /* Check for feature support */
1352 switch (domain
->type
) {
1353 case LTTNG_DOMAIN_KERNEL
:
1355 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1356 /* Sampling position of buffer is not supported */
1357 WARN("Kernel tracer does not support buffer monitoring. "
1358 "Setting the monitor interval timer to 0 "
1359 "(disabled) for channel '%s' of session '%s'",
1360 attr
->name
, session
->name
);
1361 lttng_channel_set_monitor_timer_interval(attr
, 0);
1364 ret_code
= kernel_domain_check_trace_format_requirements(*session
->trace_format
);
1365 if (ret_code
!= LTTNG_OK
) {
1366 WARN("Kernel tracer does not support the configured trace format of session '%s'",
1372 case LTTNG_DOMAIN_UST
:
1374 case LTTNG_DOMAIN_JUL
:
1375 case LTTNG_DOMAIN_LOG4J
:
1376 case LTTNG_DOMAIN_PYTHON
:
1377 if (!agent_tracing_is_enabled()) {
1378 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1379 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1384 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1388 switch (domain
->type
) {
1389 case LTTNG_DOMAIN_KERNEL
:
1391 struct ltt_kernel_channel
*kchan
;
1393 kchan
= trace_kernel_get_channel_by_name(
1394 attr
->name
, session
->kernel_session
);
1395 if (kchan
== NULL
) {
1397 * Don't try to create a channel if the session has been started at
1398 * some point in time before. The tracer does not allow it.
1400 if (session
->has_been_started
) {
1401 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1405 if (session
->snapshot
.nb_output
> 0 ||
1406 session
->snapshot_mode
) {
1407 /* Enforce mmap output for snapshot sessions. */
1408 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1410 ret_code
= channel_kernel_create(
1411 session
->kernel_session
, attr
, wpipe
);
1412 if (attr
->name
[0] != '\0') {
1413 session
->kernel_session
->has_non_default_channel
= 1;
1416 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1419 if (ret_code
!= LTTNG_OK
) {
1423 kernel_wait_quiescent();
1426 case LTTNG_DOMAIN_UST
:
1427 case LTTNG_DOMAIN_JUL
:
1428 case LTTNG_DOMAIN_LOG4J
:
1429 case LTTNG_DOMAIN_PYTHON
:
1431 struct ltt_ust_channel
*uchan
;
1436 * Current agent implementation limitations force us to allow
1437 * only one channel at once in "agent" subdomains. Each
1438 * subdomain has a default channel name which must be strictly
1441 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1442 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1443 LTTNG_SYMBOL_NAME_LEN
)) {
1444 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1447 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1448 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1449 LTTNG_SYMBOL_NAME_LEN
)) {
1450 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1453 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1454 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1455 LTTNG_SYMBOL_NAME_LEN
)) {
1456 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1461 chan_ht
= usess
->domain_global
.channels
;
1463 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1464 if (uchan
== NULL
) {
1466 * Don't try to create a channel if the session has been started at
1467 * some point in time before. The tracer does not allow it.
1469 if (session
->has_been_started
) {
1470 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1474 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1475 if (attr
->name
[0] != '\0') {
1476 usess
->has_non_default_channel
= 1;
1479 ret_code
= channel_ust_enable(usess
, uchan
);
1484 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1488 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1489 session
->has_non_mmap_channel
= true;
1494 lttng_channel_destroy(attr
);
1498 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1499 struct ltt_session
*session
,
1500 enum lttng_domain_type domain
,
1501 enum lttng_process_attr process_attr
,
1502 enum lttng_tracking_policy
*policy
)
1504 enum lttng_error_code ret_code
= LTTNG_OK
;
1505 const struct process_attr_tracker
*tracker
;
1508 case LTTNG_DOMAIN_KERNEL
:
1509 if (!session
->kernel_session
) {
1510 ret_code
= LTTNG_ERR_INVALID
;
1513 tracker
= kernel_get_process_attr_tracker(
1514 session
->kernel_session
, process_attr
);
1516 case LTTNG_DOMAIN_UST
:
1517 if (!session
->ust_session
) {
1518 ret_code
= LTTNG_ERR_INVALID
;
1521 tracker
= trace_ust_get_process_attr_tracker(
1522 session
->ust_session
, process_attr
);
1525 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1529 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1531 ret_code
= LTTNG_ERR_INVALID
;
1537 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1538 struct ltt_session
*session
,
1539 enum lttng_domain_type domain
,
1540 enum lttng_process_attr process_attr
,
1541 enum lttng_tracking_policy policy
)
1543 enum lttng_error_code ret_code
= LTTNG_OK
;
1546 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1547 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1548 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1551 ret_code
= LTTNG_ERR_INVALID
;
1556 case LTTNG_DOMAIN_KERNEL
:
1557 if (!session
->kernel_session
) {
1558 ret_code
= LTTNG_ERR_INVALID
;
1561 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1562 session
->kernel_session
, process_attr
, policy
);
1564 case LTTNG_DOMAIN_UST
:
1565 if (!session
->ust_session
) {
1566 ret_code
= LTTNG_ERR_INVALID
;
1569 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1570 session
->ust_session
, process_attr
, policy
);
1573 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1580 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1581 struct ltt_session
*session
,
1582 enum lttng_domain_type domain
,
1583 enum lttng_process_attr process_attr
,
1584 const struct process_attr_value
*value
)
1586 enum lttng_error_code ret_code
= LTTNG_OK
;
1589 case LTTNG_DOMAIN_KERNEL
:
1590 if (!session
->kernel_session
) {
1591 ret_code
= LTTNG_ERR_INVALID
;
1594 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1595 session
->kernel_session
, process_attr
, value
);
1597 case LTTNG_DOMAIN_UST
:
1598 if (!session
->ust_session
) {
1599 ret_code
= LTTNG_ERR_INVALID
;
1602 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1603 session
->ust_session
, process_attr
, value
);
1606 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1613 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1614 struct ltt_session
*session
,
1615 enum lttng_domain_type domain
,
1616 enum lttng_process_attr process_attr
,
1617 const struct process_attr_value
*value
)
1619 enum lttng_error_code ret_code
= LTTNG_OK
;
1622 case LTTNG_DOMAIN_KERNEL
:
1623 if (!session
->kernel_session
) {
1624 ret_code
= LTTNG_ERR_INVALID
;
1627 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1628 session
->kernel_session
, process_attr
, value
);
1630 case LTTNG_DOMAIN_UST
:
1631 if (!session
->ust_session
) {
1632 ret_code
= LTTNG_ERR_INVALID
;
1635 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1636 session
->ust_session
, process_attr
, value
);
1639 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1646 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1647 struct ltt_session
*session
,
1648 enum lttng_domain_type domain
,
1649 enum lttng_process_attr process_attr
,
1650 struct lttng_process_attr_values
**values
)
1652 enum lttng_error_code ret_code
= LTTNG_OK
;
1653 const struct process_attr_tracker
*tracker
;
1654 enum process_attr_tracker_status status
;
1657 case LTTNG_DOMAIN_KERNEL
:
1658 if (!session
->kernel_session
) {
1659 ret_code
= LTTNG_ERR_INVALID
;
1662 tracker
= kernel_get_process_attr_tracker(
1663 session
->kernel_session
, process_attr
);
1665 case LTTNG_DOMAIN_UST
:
1666 if (!session
->ust_session
) {
1667 ret_code
= LTTNG_ERR_INVALID
;
1670 tracker
= trace_ust_get_process_attr_tracker(
1671 session
->ust_session
, process_attr
);
1674 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1679 ret_code
= LTTNG_ERR_INVALID
;
1683 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1685 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1686 ret_code
= LTTNG_OK
;
1688 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1689 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1691 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1692 ret_code
= LTTNG_ERR_NOMEM
;
1695 ret_code
= LTTNG_ERR_UNK
;
1704 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1706 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1707 struct lttng_event
*event
,
1708 char *filter_expression
,
1709 struct lttng_bytecode
*bytecode
,
1710 struct lttng_event_exclusion
*exclusion
)
1713 const char *event_name
;
1714 const struct ltt_session
*session
= cmd_ctx
->session
;
1715 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1716 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1718 DBG("Disable event command for event \'%s\'", event
->name
);
1721 * Filter and exclusions are simply not handled by the
1722 * disable event command at this time.
1726 (void) filter_expression
;
1729 /* Ignore the presence of filter or exclusion for the event */
1731 event
->exclusion
= 0;
1733 event_name
= event
->name
;
1735 /* Error out on unhandled search criteria */
1736 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1737 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1738 ret
= LTTNG_ERR_UNK
;
1745 case LTTNG_DOMAIN_KERNEL
:
1747 struct ltt_kernel_channel
*kchan
;
1748 struct ltt_kernel_session
*ksess
;
1750 ksess
= session
->kernel_session
;
1753 * If a non-default channel has been created in the
1754 * session, explicitely require that -c chan_name needs
1757 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1758 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1762 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1763 if (kchan
== NULL
) {
1764 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1768 switch (event
->type
) {
1769 case LTTNG_EVENT_ALL
:
1770 case LTTNG_EVENT_TRACEPOINT
:
1771 case LTTNG_EVENT_SYSCALL
:
1772 case LTTNG_EVENT_PROBE
:
1773 case LTTNG_EVENT_FUNCTION
:
1774 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1775 if (event_name
[0] == '\0') {
1776 ret
= event_kernel_disable_event(kchan
,
1779 ret
= event_kernel_disable_event(kchan
,
1780 event_name
, event
->type
);
1782 if (ret
!= LTTNG_OK
) {
1787 ret
= LTTNG_ERR_UNK
;
1791 kernel_wait_quiescent();
1794 case LTTNG_DOMAIN_UST
:
1796 struct ltt_ust_channel
*uchan
;
1797 struct ltt_ust_session
*usess
;
1799 usess
= session
->ust_session
;
1801 if (validate_ust_event_name(event_name
)) {
1802 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1807 * If a non-default channel has been created in the
1808 * session, explicitly require that -c chan_name needs
1811 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1812 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1816 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1818 if (uchan
== NULL
) {
1819 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1823 switch (event
->type
) {
1824 case LTTNG_EVENT_ALL
:
1826 * An empty event name means that everything
1827 * should be disabled.
1829 if (event
->name
[0] == '\0') {
1830 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1832 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1835 if (ret
!= LTTNG_OK
) {
1840 ret
= LTTNG_ERR_UNK
;
1844 DBG3("Disable UST event %s in channel %s completed", event_name
,
1848 case LTTNG_DOMAIN_LOG4J
:
1849 case LTTNG_DOMAIN_JUL
:
1850 case LTTNG_DOMAIN_PYTHON
:
1853 struct ltt_ust_session
*usess
= session
->ust_session
;
1855 LTTNG_ASSERT(usess
);
1857 switch (event
->type
) {
1858 case LTTNG_EVENT_ALL
:
1861 ret
= LTTNG_ERR_UNK
;
1865 agt
= trace_ust_find_agent(usess
, domain
);
1867 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1871 * An empty event name means that everything
1872 * should be disabled.
1874 if (event
->name
[0] == '\0') {
1875 ret
= event_agent_disable_all(usess
, agt
);
1877 ret
= event_agent_disable(usess
, agt
, event_name
);
1879 if (ret
!= LTTNG_OK
) {
1886 ret
= LTTNG_ERR_UND
;
1897 free(filter_expression
);
1902 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1904 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1905 const struct lttng_event_context
*event_context
, int kwpipe
)
1907 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1908 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1909 const struct ltt_session
*session
= cmd_ctx
->session
;
1910 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1913 * Don't try to add a context if the session has been started at
1914 * some point in time before. The tracer does not allow it and would
1915 * result in a corrupted trace.
1917 if (cmd_ctx
->session
->has_been_started
) {
1918 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1923 case LTTNG_DOMAIN_KERNEL
:
1924 LTTNG_ASSERT(session
->kernel_session
);
1926 if (session
->kernel_session
->channel_count
== 0) {
1927 /* Create default channel */
1928 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1929 if (ret
!= LTTNG_OK
) {
1932 chan_kern_created
= 1;
1934 /* Add kernel context to kernel tracer */
1935 ret
= context_kernel_add(session
->kernel_session
,
1936 event_context
, channel_name
);
1937 if (ret
!= LTTNG_OK
) {
1941 case LTTNG_DOMAIN_JUL
:
1942 case LTTNG_DOMAIN_LOG4J
:
1945 * Validate channel name.
1946 * If no channel name is given and the domain is JUL or LOG4J,
1947 * set it to the appropriate domain-specific channel name. If
1948 * a name is provided but does not match the expexted channel
1949 * name, return an error.
1951 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1952 strcmp(channel_name
,
1953 DEFAULT_JUL_CHANNEL_NAME
)) {
1954 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1956 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1957 strcmp(channel_name
,
1958 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1959 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1964 case LTTNG_DOMAIN_UST
:
1966 struct ltt_ust_session
*usess
= session
->ust_session
;
1967 unsigned int chan_count
;
1969 LTTNG_ASSERT(usess
);
1971 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1972 if (chan_count
== 0) {
1973 struct lttng_channel
*attr
;
1974 /* Create default channel */
1975 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1977 ret
= LTTNG_ERR_FATAL
;
1981 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1982 if (ret
!= LTTNG_OK
) {
1986 channel_attr_destroy(attr
);
1987 chan_ust_created
= 1;
1990 ret
= context_ust_add(usess
, domain
, event_context
,
1992 if (ret
!= LTTNG_OK
) {
1998 ret
= LTTNG_ERR_UND
;
2006 if (chan_kern_created
) {
2007 struct ltt_kernel_channel
*kchan
=
2008 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2009 session
->kernel_session
);
2010 /* Created previously, this should NOT fail. */
2011 LTTNG_ASSERT(kchan
);
2012 kernel_destroy_channel(kchan
);
2015 if (chan_ust_created
) {
2016 struct ltt_ust_channel
*uchan
=
2017 trace_ust_find_channel_by_name(
2018 session
->ust_session
->domain_global
.channels
,
2019 DEFAULT_CHANNEL_NAME
);
2020 /* Created previously, this should NOT fail. */
2021 LTTNG_ASSERT(uchan
);
2022 /* Remove from the channel list of the session. */
2023 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2025 trace_ust_destroy_channel(uchan
);
2031 static inline bool name_starts_with(const char *name
, const char *prefix
)
2033 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2035 return !strncmp(name
, prefix
, max_cmp_len
);
2038 /* Perform userspace-specific event name validation */
2039 static int validate_ust_event_name(const char *name
)
2049 * Check name against all internal UST event component namespaces used
2052 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2053 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2054 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2063 * Internal version of cmd_enable_event() with a supplemental
2064 * "internal_event" flag which is used to enable internal events which should
2065 * be hidden from clients. Such events are used in the agent implementation to
2066 * enable the events through which all "agent" events are funeled.
2068 static int _cmd_enable_event(struct ltt_session
*session
,
2069 const struct lttng_domain
*domain
,
2070 char *channel_name
, struct lttng_event
*event
,
2071 char *filter_expression
,
2072 struct lttng_bytecode
*filter
,
2073 struct lttng_event_exclusion
*exclusion
,
2074 int wpipe
, bool internal_event
)
2076 int ret
= 0, channel_created
= 0;
2077 struct lttng_channel
*attr
= NULL
;
2079 LTTNG_ASSERT(session
);
2080 LTTNG_ASSERT(event
);
2081 LTTNG_ASSERT(channel_name
);
2083 /* If we have a filter, we must have its filter expression */
2084 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2086 /* Normalize event name as a globbing pattern */
2087 strutils_normalize_star_glob_pattern(event
->name
);
2089 /* Normalize exclusion names as globbing patterns */
2093 for (i
= 0; i
< exclusion
->count
; i
++) {
2094 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2096 strutils_normalize_star_glob_pattern(name
);
2100 DBG("Enable event command for event \'%s\'", event
->name
);
2104 switch (domain
->type
) {
2105 case LTTNG_DOMAIN_KERNEL
:
2107 struct ltt_kernel_channel
*kchan
;
2110 * If a non-default channel has been created in the
2111 * session, explicitely require that -c chan_name needs
2114 if (session
->kernel_session
->has_non_default_channel
2115 && channel_name
[0] == '\0') {
2116 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2120 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2121 session
->kernel_session
);
2122 if (kchan
== NULL
) {
2123 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2124 LTTNG_BUFFER_GLOBAL
);
2126 ret
= LTTNG_ERR_FATAL
;
2129 if (lttng_strncpy(attr
->name
, channel_name
,
2130 sizeof(attr
->name
))) {
2131 ret
= LTTNG_ERR_INVALID
;
2135 ret
= cmd_enable_channel_internal(
2136 session
, domain
, attr
, wpipe
);
2137 if (ret
!= LTTNG_OK
) {
2140 channel_created
= 1;
2143 /* Get the newly created kernel channel pointer */
2144 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2145 session
->kernel_session
);
2146 if (kchan
== NULL
) {
2147 /* This sould not happen... */
2148 ret
= LTTNG_ERR_FATAL
;
2152 switch (event
->type
) {
2153 case LTTNG_EVENT_ALL
:
2155 char *filter_expression_a
= NULL
;
2156 struct lttng_bytecode
*filter_a
= NULL
;
2159 * We need to duplicate filter_expression and filter,
2160 * because ownership is passed to first enable
2163 if (filter_expression
) {
2164 filter_expression_a
= strdup(filter_expression
);
2165 if (!filter_expression_a
) {
2166 ret
= LTTNG_ERR_FATAL
;
2171 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2173 free(filter_expression_a
);
2174 ret
= LTTNG_ERR_FATAL
;
2177 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2179 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2180 ret
= event_kernel_enable_event(kchan
, event
,
2181 filter_expression
, filter
);
2182 /* We have passed ownership */
2183 filter_expression
= NULL
;
2185 if (ret
!= LTTNG_OK
) {
2186 if (channel_created
) {
2187 /* Let's not leak a useless channel. */
2188 kernel_destroy_channel(kchan
);
2190 free(filter_expression_a
);
2194 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2195 ret
= event_kernel_enable_event(kchan
, event
,
2196 filter_expression_a
, filter_a
);
2197 /* We have passed ownership */
2198 filter_expression_a
= NULL
;
2200 if (ret
!= LTTNG_OK
) {
2205 case LTTNG_EVENT_PROBE
:
2206 case LTTNG_EVENT_USERSPACE_PROBE
:
2207 case LTTNG_EVENT_FUNCTION
:
2208 case LTTNG_EVENT_FUNCTION_ENTRY
:
2209 case LTTNG_EVENT_TRACEPOINT
:
2210 ret
= event_kernel_enable_event(kchan
, event
,
2211 filter_expression
, filter
);
2212 /* We have passed ownership */
2213 filter_expression
= NULL
;
2215 if (ret
!= LTTNG_OK
) {
2216 if (channel_created
) {
2217 /* Let's not leak a useless channel. */
2218 kernel_destroy_channel(kchan
);
2223 case LTTNG_EVENT_SYSCALL
:
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
) {
2234 ret
= LTTNG_ERR_UNK
;
2238 kernel_wait_quiescent();
2241 case LTTNG_DOMAIN_UST
:
2243 struct ltt_ust_channel
*uchan
;
2244 struct ltt_ust_session
*usess
= session
->ust_session
;
2246 LTTNG_ASSERT(usess
);
2249 * If a non-default channel has been created in the
2250 * session, explicitely require that -c chan_name needs
2253 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2254 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2258 /* Get channel from global UST domain */
2259 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2261 if (uchan
== NULL
) {
2262 /* Create default channel */
2263 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2264 usess
->buffer_type
);
2266 ret
= LTTNG_ERR_FATAL
;
2269 if (lttng_strncpy(attr
->name
, channel_name
,
2270 sizeof(attr
->name
))) {
2271 ret
= LTTNG_ERR_INVALID
;
2275 ret
= cmd_enable_channel_internal(
2276 session
, domain
, attr
, wpipe
);
2277 if (ret
!= LTTNG_OK
) {
2281 /* Get the newly created channel reference back */
2282 uchan
= trace_ust_find_channel_by_name(
2283 usess
->domain_global
.channels
, channel_name
);
2284 LTTNG_ASSERT(uchan
);
2287 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2289 * Don't allow users to add UST events to channels which
2290 * are assigned to a userspace subdomain (JUL, Log4J,
2293 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2297 if (!internal_event
) {
2299 * Ensure the event name is not reserved for internal
2302 ret
= validate_ust_event_name(event
->name
);
2304 WARN("Userspace event name %s failed validation.",
2306 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2311 /* At this point, the session and channel exist on the tracer */
2312 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2313 filter_expression
, filter
, exclusion
,
2315 /* We have passed ownership */
2316 filter_expression
= NULL
;
2319 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2320 goto already_enabled
;
2321 } else if (ret
!= LTTNG_OK
) {
2326 case LTTNG_DOMAIN_LOG4J
:
2327 case LTTNG_DOMAIN_JUL
:
2328 case LTTNG_DOMAIN_PYTHON
:
2330 const char *default_event_name
, *default_chan_name
;
2332 struct lttng_event uevent
;
2333 struct lttng_domain tmp_dom
;
2334 struct ltt_ust_session
*usess
= session
->ust_session
;
2336 LTTNG_ASSERT(usess
);
2338 if (!agent_tracing_is_enabled()) {
2339 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2340 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2344 agt
= trace_ust_find_agent(usess
, domain
->type
);
2346 agt
= agent_create(domain
->type
);
2348 ret
= LTTNG_ERR_NOMEM
;
2351 agent_add(agt
, usess
->agents
);
2354 /* Create the default tracepoint. */
2355 memset(&uevent
, 0, sizeof(uevent
));
2356 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2357 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2358 default_event_name
= event_get_default_agent_ust_name(
2360 if (!default_event_name
) {
2361 ret
= LTTNG_ERR_FATAL
;
2364 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2365 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2368 * The domain type is changed because we are about to enable the
2369 * default channel and event for the JUL domain that are hardcoded.
2370 * This happens in the UST domain.
2372 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2373 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2375 switch (domain
->type
) {
2376 case LTTNG_DOMAIN_LOG4J
:
2377 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2379 case LTTNG_DOMAIN_JUL
:
2380 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2382 case LTTNG_DOMAIN_PYTHON
:
2383 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2386 /* The switch/case we are in makes this impossible */
2391 char *filter_expression_copy
= NULL
;
2392 struct lttng_bytecode
*filter_copy
= NULL
;
2395 const size_t filter_size
= sizeof(
2396 struct lttng_bytecode
)
2399 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2401 ret
= LTTNG_ERR_NOMEM
;
2404 memcpy(filter_copy
, filter
, filter_size
);
2406 filter_expression_copy
=
2407 strdup(filter_expression
);
2408 if (!filter_expression
) {
2409 ret
= LTTNG_ERR_NOMEM
;
2412 if (!filter_expression_copy
|| !filter_copy
) {
2413 free(filter_expression_copy
);
2419 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2420 (char *) default_chan_name
,
2421 &uevent
, filter_expression_copy
,
2422 filter_copy
, NULL
, wpipe
);
2425 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2426 goto already_enabled
;
2427 } else if (ret
!= LTTNG_OK
) {
2431 /* The wild card * means that everything should be enabled. */
2432 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2433 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2436 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2440 filter_expression
= NULL
;
2441 if (ret
!= LTTNG_OK
) {
2448 ret
= LTTNG_ERR_UND
;
2456 free(filter_expression
);
2459 channel_attr_destroy(attr
);
2465 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2466 * We own filter, exclusion, and filter_expression.
2468 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2469 struct lttng_event
*event
,
2470 char *filter_expression
,
2471 struct lttng_event_exclusion
*exclusion
,
2472 struct lttng_bytecode
*bytecode
,
2477 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2479 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2482 * The ownership of the following parameters is transferred to
2483 * _cmd_enable_event:
2485 * - filter_expression,
2489 ret
= _cmd_enable_event(cmd_ctx
->session
,
2491 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2492 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2493 filter_expression
= NULL
;
2500 * Enable an event which is internal to LTTng. An internal should
2501 * never be made visible to clients and are immune to checks such as
2504 static int cmd_enable_event_internal(struct ltt_session
*session
,
2505 const struct lttng_domain
*domain
,
2506 char *channel_name
, struct lttng_event
*event
,
2507 char *filter_expression
,
2508 struct lttng_bytecode
*filter
,
2509 struct lttng_event_exclusion
*exclusion
,
2512 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2513 filter_expression
, filter
, exclusion
, wpipe
, true);
2517 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2519 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2520 struct lttng_payload
*reply_payload
)
2522 enum lttng_error_code ret_code
;
2524 ssize_t i
, nb_events
= 0;
2525 struct lttng_event
*events
= NULL
;
2526 struct lttcomm_list_command_header reply_command_header
= {};
2527 size_t reply_command_header_offset
;
2529 assert(reply_payload
);
2531 /* Reserve space for command reply header. */
2532 reply_command_header_offset
= reply_payload
->buffer
.size
;
2533 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2534 reply_command_header_offset
+
2535 sizeof(struct lttcomm_list_command_header
));
2537 ret_code
= LTTNG_ERR_NOMEM
;
2542 case LTTNG_DOMAIN_KERNEL
:
2543 nb_events
= kernel_list_events(&events
);
2544 if (nb_events
< 0) {
2545 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2549 case LTTNG_DOMAIN_UST
:
2550 nb_events
= ust_app_list_events(&events
);
2551 if (nb_events
< 0) {
2552 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2556 case LTTNG_DOMAIN_LOG4J
:
2557 case LTTNG_DOMAIN_JUL
:
2558 case LTTNG_DOMAIN_PYTHON
:
2559 nb_events
= agent_list_events(&events
, domain
);
2560 if (nb_events
< 0) {
2561 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2566 ret_code
= LTTNG_ERR_UND
;
2570 for (i
= 0; i
< nb_events
; i
++) {
2571 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2574 ret_code
= LTTNG_ERR_NOMEM
;
2579 if (nb_events
> UINT32_MAX
) {
2580 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2581 ret_code
= LTTNG_ERR_OVERFLOW
;
2585 /* Update command reply header. */
2586 reply_command_header
.count
= (uint32_t) nb_events
;
2587 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2588 sizeof(reply_command_header
));
2590 ret_code
= LTTNG_OK
;
2597 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2599 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2600 struct lttng_payload
*reply
)
2602 enum lttng_error_code ret_code
;
2604 unsigned int i
, nb_fields
;
2605 struct lttng_event_field
*fields
= NULL
;
2606 struct lttcomm_list_command_header reply_command_header
= {};
2607 size_t reply_command_header_offset
;
2611 /* Reserve space for command reply header. */
2612 reply_command_header_offset
= reply
->buffer
.size
;
2613 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2614 reply_command_header_offset
+
2615 sizeof(struct lttcomm_list_command_header
));
2617 ret_code
= LTTNG_ERR_NOMEM
;
2622 case LTTNG_DOMAIN_UST
:
2623 ret
= ust_app_list_event_fields(&fields
);
2625 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2630 case LTTNG_DOMAIN_KERNEL
:
2631 default: /* fall-through */
2632 ret_code
= LTTNG_ERR_UND
;
2638 for (i
= 0; i
< nb_fields
; i
++) {
2639 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2641 ret_code
= LTTNG_ERR_NOMEM
;
2646 if (nb_fields
> UINT32_MAX
) {
2647 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2648 ret_code
= LTTNG_ERR_OVERFLOW
;
2652 /* Update command reply header. */
2653 reply_command_header
.count
= (uint32_t) nb_fields
;
2655 memcpy(reply
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2656 sizeof(reply_command_header
));
2658 ret_code
= LTTNG_OK
;
2665 enum lttng_error_code
cmd_list_syscalls(
2666 struct lttng_payload
*reply_payload
)
2668 enum lttng_error_code ret_code
;
2669 ssize_t nb_events
, i
;
2671 struct lttng_event
*events
= NULL
;
2672 struct lttcomm_list_command_header reply_command_header
= {};
2673 size_t reply_command_header_offset
;
2675 assert(reply_payload
);
2677 /* Reserve space for command reply header. */
2678 reply_command_header_offset
= reply_payload
->buffer
.size
;
2679 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2680 reply_command_header_offset
+
2681 sizeof(struct lttcomm_list_command_header
));
2683 ret_code
= LTTNG_ERR_NOMEM
;
2687 nb_events
= syscall_table_list(&events
);
2688 if (nb_events
< 0) {
2689 ret_code
= (enum lttng_error_code
) -nb_events
;
2693 for (i
= 0; i
< nb_events
; i
++) {
2694 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2697 ret_code
= LTTNG_ERR_NOMEM
;
2702 if (nb_events
> UINT32_MAX
) {
2703 ERR("Syscall count would overflow the syscall listing command's reply");
2704 ret_code
= LTTNG_ERR_OVERFLOW
;
2708 /* Update command reply header. */
2709 reply_command_header
.count
= (uint32_t) nb_events
;
2710 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2711 sizeof(reply_command_header
));
2713 ret_code
= LTTNG_OK
;
2720 * Command LTTNG_START_TRACE processed by the client thread.
2722 * Called with session mutex held.
2724 int cmd_start_trace(struct ltt_session
*session
)
2726 enum lttng_error_code ret
;
2727 unsigned long nb_chan
= 0;
2728 struct ltt_kernel_session
*ksession
;
2729 struct ltt_ust_session
*usess
;
2730 const bool session_rotated_after_last_stop
=
2731 session
->rotated_after_last_stop
;
2732 const bool session_cleared_after_last_stop
=
2733 session
->cleared_after_last_stop
;
2735 LTTNG_ASSERT(session
);
2737 /* Ease our life a bit ;) */
2738 ksession
= session
->kernel_session
;
2739 usess
= session
->ust_session
;
2741 /* Is the session already started? */
2742 if (session
->active
) {
2743 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2744 /* Perform nothing */
2748 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2749 !session
->current_trace_chunk
) {
2751 * A rotation was launched while the session was stopped and
2752 * it has not been completed yet. It is not possible to start
2753 * the session since starting the session here would require a
2754 * rotation from "NULL" to a new trace chunk. That rotation
2755 * would overlap with the ongoing rotation, which is not
2758 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2760 ret
= LTTNG_ERR_ROTATION_PENDING
;
2765 * Starting a session without channel is useless since after that it's not
2766 * possible to enable channel thus inform the client.
2768 if (usess
&& usess
->domain_global
.channels
) {
2769 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2772 nb_chan
+= ksession
->channel_count
;
2775 ret
= LTTNG_ERR_NO_CHANNEL
;
2779 session
->active
= true;
2780 session
->rotated_after_last_stop
= false;
2781 session
->cleared_after_last_stop
= false;
2782 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2783 if (!session
->has_been_started
) {
2784 struct lttng_trace_chunk
*trace_chunk
;
2786 DBG("Creating initial trace chunk of session \"%s\"",
2788 trace_chunk
= session_create_new_trace_chunk(
2789 session
, NULL
, NULL
, NULL
);
2791 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2794 LTTNG_ASSERT(!session
->current_trace_chunk
);
2795 ret
= (lttng_error_code
) session_set_trace_chunk(session
, trace_chunk
,
2797 lttng_trace_chunk_put(trace_chunk
);
2799 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2803 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2806 * Rotate existing streams into the new chunk.
2807 * This is a "quiet" rotation has no client has
2808 * explicitly requested this operation.
2810 * There is also no need to wait for the rotation
2811 * to complete as it will happen immediately. No data
2812 * was produced as the session was stopped, so the
2813 * rotation should happen on reception of the command.
2815 ret
= (lttng_error_code
) cmd_rotate_session(session
, NULL
, true,
2816 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2817 if (ret
!= LTTNG_OK
) {
2823 /* Kernel tracing */
2824 if (ksession
!= NULL
) {
2825 DBG("Start kernel tracing session %s", session
->name
);
2826 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2827 if (ret
!= LTTNG_OK
) {
2832 /* Flag session that trace should start automatically */
2834 int int_ret
= ust_app_start_trace_all(usess
);
2837 ret
= LTTNG_ERR_UST_START_FAIL
;
2843 * Open a packet in every stream of the session to ensure that viewers
2844 * can correctly identify the boundaries of the periods during which
2845 * tracing was active for this session.
2847 ret
= session_open_packets(session
);
2848 if (ret
!= LTTNG_OK
) {
2853 * Clear the flag that indicates that a rotation was done while the
2854 * session was stopped.
2856 session
->rotated_after_last_stop
= false;
2858 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2859 int int_ret
= timer_session_rotation_schedule_timer_start(
2860 session
, session
->rotate_timer_period
);
2863 ERR("Failed to enable rotate timer");
2864 ret
= LTTNG_ERR_UNK
;
2872 if (ret
== LTTNG_OK
) {
2873 /* Flag this after a successful start. */
2874 session
->has_been_started
= true;
2876 session
->active
= false;
2877 /* Restore initial state on error. */
2878 session
->rotated_after_last_stop
=
2879 session_rotated_after_last_stop
;
2880 session
->cleared_after_last_stop
=
2881 session_cleared_after_last_stop
;
2888 * Command LTTNG_STOP_TRACE processed by the client thread.
2890 int cmd_stop_trace(struct ltt_session
*session
)
2893 struct ltt_kernel_session
*ksession
;
2894 struct ltt_ust_session
*usess
;
2896 LTTNG_ASSERT(session
);
2898 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2900 ksession
= session
->kernel_session
;
2901 usess
= session
->ust_session
;
2903 /* Session is not active. Skip everything and inform the client. */
2904 if (!session
->active
) {
2905 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2909 ret
= stop_kernel_session(ksession
);
2910 if (ret
!= LTTNG_OK
) {
2914 if (usess
&& usess
->active
) {
2915 ret
= ust_app_stop_trace_all(usess
);
2917 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2922 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2924 /* Flag inactive after a successful stop. */
2925 session
->active
= 0;
2933 * Set the base_path of the session only if subdir of a control uris is set.
2934 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2936 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2938 struct lttng_uri
*uris
)
2943 for (i
= 0; i
< nb_uri
; i
++) {
2944 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2945 uris
[i
].subdir
[0] == '\0') {
2946 /* Not interested in these URIs */
2950 if (session
->base_path
!= NULL
) {
2951 free(session
->base_path
);
2952 session
->base_path
= NULL
;
2955 /* Set session base_path */
2956 session
->base_path
= strdup(uris
[i
].subdir
);
2957 if (!session
->base_path
) {
2958 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2959 uris
[i
].subdir
, session
->name
);
2960 ret
= LTTNG_ERR_NOMEM
;
2963 DBG2("Setting base path \"%s\" for session \"%s\"",
2964 session
->base_path
, session
->name
);
2972 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2974 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2975 struct lttng_uri
*uris
)
2978 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2979 struct ltt_ust_session
*usess
= session
->ust_session
;
2981 LTTNG_ASSERT(session
);
2983 LTTNG_ASSERT(nb_uri
> 0);
2985 /* Can't set consumer URI if the session is active. */
2986 if (session
->active
) {
2987 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2992 * Set the session base path if any. This is done inside
2993 * cmd_set_consumer_uri to preserve backward compatibility of the
2994 * previous session creation api vs the session descriptor api.
2996 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2997 if (ret
!= LTTNG_OK
) {
3001 /* Set the "global" consumer URIs */
3002 for (i
= 0; i
< nb_uri
; i
++) {
3003 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
3005 if (ret
!= LTTNG_OK
) {
3010 /* Set UST session URIs */
3011 if (session
->ust_session
) {
3012 for (i
= 0; i
< nb_uri
; i
++) {
3013 ret
= add_uri_to_consumer(session
,
3014 session
->ust_session
->consumer
,
3015 &uris
[i
], LTTNG_DOMAIN_UST
);
3016 if (ret
!= LTTNG_OK
) {
3022 /* Set kernel session URIs */
3023 if (session
->kernel_session
) {
3024 for (i
= 0; i
< nb_uri
; i
++) {
3025 ret
= add_uri_to_consumer(session
,
3026 session
->kernel_session
->consumer
,
3027 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3028 if (ret
!= LTTNG_OK
) {
3035 * Make sure to set the session in output mode after we set URI since a
3036 * session can be created without URL (thus flagged in no output mode).
3038 session
->output_traces
= 1;
3040 ksess
->output_traces
= 1;
3044 usess
->output_traces
= 1;
3055 enum lttng_error_code
set_session_output_from_descriptor(
3056 struct ltt_session
*session
,
3057 const struct lttng_session_descriptor
*descriptor
)
3060 enum lttng_error_code ret_code
= LTTNG_OK
;
3061 enum lttng_session_descriptor_type session_type
=
3062 lttng_session_descriptor_get_type(descriptor
);
3063 enum lttng_session_descriptor_output_type output_type
=
3064 lttng_session_descriptor_get_output_type(descriptor
);
3065 struct lttng_uri uris
[2] = {};
3066 size_t uri_count
= 0;
3068 switch (output_type
) {
3069 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3071 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3072 lttng_session_descriptor_get_local_output_uri(descriptor
,
3076 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3077 lttng_session_descriptor_get_network_output_uris(descriptor
,
3078 &uris
[0], &uris
[1]);
3082 ret_code
= LTTNG_ERR_INVALID
;
3086 switch (session_type
) {
3087 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3089 struct snapshot_output
*new_output
= NULL
;
3091 new_output
= snapshot_output_alloc();
3093 ret_code
= LTTNG_ERR_NOMEM
;
3097 ret
= snapshot_output_init_with_uri(session
,
3098 DEFAULT_SNAPSHOT_MAX_SIZE
,
3099 NULL
, uris
, uri_count
, session
->consumer
,
3100 new_output
, &session
->snapshot
);
3102 ret_code
= (ret
== -ENOMEM
) ?
3103 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3104 snapshot_output_destroy(new_output
);
3107 snapshot_add_output(&session
->snapshot
, new_output
);
3110 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3111 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3113 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3117 ret_code
= LTTNG_ERR_INVALID
;
3125 enum lttng_error_code
cmd_create_session_from_descriptor(
3126 struct lttng_session_descriptor
*descriptor
,
3127 const lttng_sock_cred
*creds
,
3128 const char *home_path
)
3131 enum lttng_error_code ret_code
;
3132 const char *session_name
;
3133 struct ltt_session
*new_session
= NULL
;
3134 enum lttng_session_descriptor_status descriptor_status
;
3135 const lttng_trace_format_descriptor
*trace_format_descriptor
= NULL
;
3136 lttng::trace_format_descriptor::uptr trace_format_descriptor_ptr
;
3138 session_lock_list();
3140 if (*home_path
!= '/') {
3141 ERR("Home path provided by client is not absolute");
3142 ret_code
= LTTNG_ERR_INVALID
;
3147 descriptor_status
= lttng_session_descriptor_get_session_name(
3148 descriptor
, &session_name
);
3149 switch (descriptor_status
) {
3150 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3152 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3153 session_name
= NULL
;
3156 ret_code
= LTTNG_ERR_INVALID
;
3160 descriptor_status
= lttng_session_descriptor_get_trace_format_descriptor(
3161 descriptor
, &trace_format_descriptor
);
3162 if (descriptor_status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
3163 ret_code
= LTTNG_ERR_INVALID
;
3168 trace_format_descriptor_ptr
=
3169 reinterpret_cast<const lttng::trace_format_descriptor
*>(
3170 trace_format_descriptor
)
3172 } catch (std::exception
& e
) {
3173 ERR("%s", e
.what());
3174 ret_code
= LTTNG_ERR_UNK
;
3178 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, trace_format_descriptor_ptr
,
3180 if (ret_code
!= LTTNG_OK
) {
3184 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3185 new_session
->id
, new_session
->name
, new_session
->uid
, new_session
->gid
);
3186 if (ret_code
!= LTTNG_OK
) {
3190 /* Announce the session's destruction to the notification thread when it is destroyed. */
3191 ret
= session_add_destroy_notifier(
3193 [](const struct ltt_session
*session
,
3194 void *user_data
__attribute__((unused
))) {
3195 (void) notification_thread_command_remove_session(
3196 the_notification_thread_handle
, session
->id
);
3200 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3202 ret
= LTTNG_ERR_NOMEM
;
3206 if (!session_name
) {
3207 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3210 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3215 if (!lttng_session_descriptor_is_output_destination_initialized(
3218 * Only include the session's creation time in the output
3219 * destination if the name of the session itself was
3220 * not auto-generated.
3222 ret_code
= lttng_session_descriptor_set_default_output(
3224 session_name
? &new_session
->creation_time
: NULL
,
3226 if (ret_code
!= LTTNG_OK
) {
3230 new_session
->has_user_specified_directory
=
3231 lttng_session_descriptor_has_output_directory(
3235 switch (lttng_session_descriptor_get_type(descriptor
)) {
3236 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3237 new_session
->snapshot_mode
= 1;
3239 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3240 new_session
->live_timer
=
3241 lttng_session_descriptor_live_get_timer_interval(
3248 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3249 if (ret_code
!= LTTNG_OK
) {
3252 new_session
->consumer
->enabled
= 1;
3253 ret_code
= LTTNG_OK
;
3255 /* Release reference provided by the session_create function. */
3256 session_put(new_session
);
3257 if (ret_code
!= LTTNG_OK
&& new_session
) {
3258 /* Release the global reference on error. */
3259 session_destroy(new_session
);
3261 session_unlock_list();
3265 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3266 struct lttng_session_descriptor
**return_descriptor
)
3269 size_t payload_size
;
3270 struct lttng_dynamic_buffer payload
;
3271 struct lttng_buffer_view home_dir_view
;
3272 struct lttng_buffer_view session_descriptor_view
;
3273 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3274 enum lttng_error_code ret_code
;
3276 lttng_dynamic_buffer_init(&payload
);
3277 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3279 ret_code
= LTTNG_ERR_INVALID
;
3282 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3283 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3284 ret_code
= LTTNG_ERR_INVALID
;
3288 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3289 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3290 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3292 ret_code
= LTTNG_ERR_NOMEM
;
3296 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3298 ERR("Reception of session descriptor failed, aborting.");
3299 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3303 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3306 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3307 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3308 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3309 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3310 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3314 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3316 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3317 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3318 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3319 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3320 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3324 ret
= lttng_session_descriptor_create_from_buffer(
3325 &session_descriptor_view
, &session_descriptor
);
3327 ERR("Failed to create session descriptor from payload of \"create session\" command");
3328 ret_code
= LTTNG_ERR_INVALID
;
3333 * Sets the descriptor's auto-generated properties (name, output) if
3336 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3338 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3339 if (ret_code
!= LTTNG_OK
) {
3343 ret_code
= LTTNG_OK
;
3344 *return_descriptor
= session_descriptor
;
3345 session_descriptor
= NULL
;
3347 lttng_dynamic_buffer_reset(&payload
);
3348 lttng_session_descriptor_destroy(session_descriptor
);
3353 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3354 void *_reply_context
)
3358 const struct cmd_destroy_session_reply_context
*reply_context
=
3359 (cmd_destroy_session_reply_context
*) _reply_context
;
3360 struct lttng_dynamic_buffer payload
;
3361 struct lttcomm_session_destroy_command_header cmd_header
;
3362 struct lttng_trace_archive_location
*location
= NULL
;
3363 struct lttcomm_lttng_msg llm
= {
3364 .cmd_type
= LTTNG_DESTROY_SESSION
,
3365 .ret_code
= reply_context
->destruction_status
,
3368 sizeof(struct lttcomm_session_destroy_command_header
),
3372 size_t payload_size_before_location
;
3374 lttng_dynamic_buffer_init(&payload
);
3376 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3378 ERR("Failed to append session destruction message");
3382 cmd_header
.rotation_state
=
3383 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3384 session
->rotation_state
:
3385 LTTNG_ROTATION_STATE_NO_ROTATION
);
3386 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3387 sizeof(cmd_header
));
3389 ERR("Failed to append session destruction command header");
3393 if (!reply_context
->implicit_rotation_on_destroy
) {
3394 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3398 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3399 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3404 location
= session_get_trace_archive_location(session
);
3406 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3411 payload_size_before_location
= payload
.size
;
3412 comm_ret
= lttng_trace_archive_location_serialize(location
,
3414 lttng_trace_archive_location_put(location
);
3416 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3420 /* Update the message to indicate the location's length. */
3421 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3422 payload
.size
- payload_size_before_location
;
3424 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3425 payload
.data
, payload
.size
);
3426 if (comm_ret
!= (ssize_t
) payload
.size
) {
3427 ERR("Failed to send result of the destruction of session \"%s\" to client",
3431 ret
= close(reply_context
->reply_sock_fd
);
3433 PERROR("Failed to close client socket in deferred session destroy reply");
3435 lttng_dynamic_buffer_reset(&payload
);
3436 free(_reply_context
);
3440 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3442 * Called with session lock held.
3444 int cmd_destroy_session(struct ltt_session
*session
,
3445 struct notification_thread_handle
*notification_thread_handle
,
3449 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3450 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3453 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3454 if (!reply_context
) {
3455 ret
= LTTNG_ERR_NOMEM
;
3459 reply_context
->reply_sock_fd
= *sock_fd
;
3463 LTTNG_ASSERT(session
);
3465 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3467 if (session
->active
) {
3468 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3470 ret
= cmd_stop_trace(session
);
3471 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3472 /* Carry on with the destruction of the session. */
3473 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3474 session
->name
, lttng_strerror(-ret
));
3475 destruction_last_error
= (lttng_error_code
) ret
;
3479 if (session
->rotation_schedule_timer_enabled
) {
3480 if (timer_session_rotation_schedule_timer_stop(
3482 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3484 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3488 if (session
->rotate_size
) {
3489 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3490 session
->rotate_size
= 0;
3493 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3495 * Perform a last rotation on destruction if rotations have
3496 * occurred during the session's lifetime.
3498 ret
= cmd_rotate_session(session
, NULL
, false,
3499 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3500 if (ret
!= LTTNG_OK
) {
3501 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3502 session
->name
, lttng_strerror(-ret
));
3503 destruction_last_error
= (lttng_error_code
) -ret
;
3505 if (reply_context
) {
3506 reply_context
->implicit_rotation_on_destroy
= true;
3508 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3510 * The user has not triggered a session rotation. However, to
3511 * ensure all data has been consumed, the session is rotated
3512 * to a 'null' trace chunk before it is destroyed.
3514 * This is a "quiet" rotation meaning that no notification is
3515 * emitted and no renaming of the current trace chunk takes
3518 ret
= cmd_rotate_session(session
, NULL
, true,
3519 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3521 * Rotation operations may not be supported by the kernel
3522 * tracer. Hence, do not consider this implicit rotation as
3523 * a session destruction error. The library has already stopped
3524 * the session and waited for pending data; there is nothing
3525 * left to do but complete the destruction of the session.
3527 if (ret
!= LTTNG_OK
&&
3528 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3529 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3530 session
->name
, lttng_strerror(ret
));
3531 destruction_last_error
= (lttng_error_code
) -ret
;
3535 if (session
->shm_path
[0]) {
3537 * When a session is created with an explicit shm_path,
3538 * the consumer daemon will create its shared memory files
3539 * at that location and will *not* unlink them. This is normal
3540 * as the intention of that feature is to make it possible
3541 * to retrieve the content of those files should a crash occur.
3543 * To ensure the content of those files can be used, the
3544 * sessiond daemon will replicate the content of the metadata
3545 * cache in a metadata file.
3547 * On clean-up, it is expected that the consumer daemon will
3548 * unlink the shared memory files and that the session daemon
3549 * will unlink the metadata file. Then, the session's directory
3550 * in the shm path can be removed.
3552 * Unfortunately, a flaw in the design of the sessiond's and
3553 * consumerd's tear down of channels makes it impossible to
3554 * determine when the sessiond _and_ the consumerd have both
3555 * destroyed their representation of a channel. For one, the
3556 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3557 * callbacks in both daemons.
3559 * However, it is also impossible for the sessiond to know when
3560 * the consumer daemon is done destroying its channel(s) since
3561 * it occurs as a reaction to the closing of the channel's file
3562 * descriptor. There is no resulting communication initiated
3563 * from the consumerd to the sessiond to confirm that the
3564 * operation is completed (and was successful).
3566 * Until this is all fixed, the session daemon checks for the
3567 * removal of the session's shm path which makes it possible
3568 * to safely advertise a session as having been destroyed.
3570 * Prior to this fix, it was not possible to reliably save
3571 * a session making use of the --shm-path option, destroy it,
3572 * and load it again. This is because the creation of the
3573 * session would fail upon seeing the session's shm path
3574 * already in existence.
3576 * Note that none of the error paths in the check for the
3577 * directory's existence return an error. This is normal
3578 * as there isn't much that can be done. The session will
3579 * be destroyed properly, except that we can't offer the
3580 * guarantee that the same session can be re-created.
3582 current_completion_handler
= &destroy_completion_handler
.handler
;
3583 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3585 sizeof(destroy_completion_handler
.shm_path
));
3590 * The session is destroyed. However, note that the command context
3591 * still holds a reference to the session, thus delaying its destruction
3592 * _at least_ up to the point when that reference is released.
3594 session_destroy(session
);
3595 if (reply_context
) {
3596 reply_context
->destruction_status
= destruction_last_error
;
3597 ret
= session_add_destroy_notifier(session
,
3598 cmd_destroy_session_reply
,
3599 (void *) reply_context
);
3601 ret
= LTTNG_ERR_FATAL
;
3613 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3615 int cmd_register_consumer(struct ltt_session
*session
,
3616 enum lttng_domain_type domain
, const char *sock_path
,
3617 struct consumer_data
*cdata
)
3620 struct consumer_socket
*socket
= NULL
;
3622 LTTNG_ASSERT(session
);
3623 LTTNG_ASSERT(cdata
);
3624 LTTNG_ASSERT(sock_path
);
3627 case LTTNG_DOMAIN_KERNEL
:
3629 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3631 LTTNG_ASSERT(ksess
);
3633 /* Can't register a consumer if there is already one */
3634 if (ksess
->consumer_fds_sent
!= 0) {
3635 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3639 sock
= lttcomm_connect_unix_sock(sock_path
);
3641 ret
= LTTNG_ERR_CONNECT_FAIL
;
3644 cdata
->cmd_sock
= sock
;
3646 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3647 if (socket
== NULL
) {
3650 PERROR("close register consumer");
3652 cdata
->cmd_sock
= -1;
3653 ret
= LTTNG_ERR_FATAL
;
3657 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3658 if (socket
->lock
== NULL
) {
3659 PERROR("zmalloc pthread mutex");
3660 ret
= LTTNG_ERR_FATAL
;
3664 pthread_mutex_init(socket
->lock
, NULL
);
3665 socket
->registered
= 1;
3668 consumer_add_socket(socket
, ksess
->consumer
);
3671 pthread_mutex_lock(&cdata
->pid_mutex
);
3673 pthread_mutex_unlock(&cdata
->pid_mutex
);
3678 /* TODO: Userspace tracing */
3679 ret
= LTTNG_ERR_UND
;
3687 consumer_destroy_socket(socket
);
3693 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3695 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3696 struct lttng_domain
**domains
)
3701 struct lttng_ht_iter iter
;
3703 if (session
->kernel_session
!= NULL
) {
3704 DBG3("Listing domains found kernel domain");
3708 if (session
->ust_session
!= NULL
) {
3709 DBG3("Listing domains found UST global domain");
3713 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3715 if (agt
->being_used
) {
3726 *domains
= calloc
<lttng_domain
>(nb_dom
);
3727 if (*domains
== NULL
) {
3728 ret
= LTTNG_ERR_FATAL
;
3732 if (session
->kernel_session
!= NULL
) {
3733 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3735 /* Kernel session buffer type is always GLOBAL */
3736 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3741 if (session
->ust_session
!= NULL
) {
3742 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3743 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3747 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3749 if (agt
->being_used
) {
3750 (*domains
)[index
].type
= agt
->domain
;
3751 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3761 /* Return negative value to differentiate return code */
3767 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3769 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3770 struct ltt_session
*session
,
3771 struct lttng_payload
*payload
)
3775 struct lttcomm_list_command_header cmd_header
= {};
3776 size_t cmd_header_offset
;
3777 enum lttng_error_code ret_code
;
3782 DBG("Listing channels for session %s", session
->name
);
3784 cmd_header_offset
= payload
->buffer
.size
;
3786 /* Reserve space for command reply header. */
3787 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3788 cmd_header_offset
+ sizeof(cmd_header
));
3790 ret_code
= LTTNG_ERR_NOMEM
;
3795 case LTTNG_DOMAIN_KERNEL
:
3797 /* Kernel channels */
3798 struct ltt_kernel_channel
*kchan
;
3799 if (session
->kernel_session
!= NULL
) {
3800 cds_list_for_each_entry(kchan
,
3801 &session
->kernel_session
->channel_list
.head
, list
) {
3802 uint64_t discarded_events
, lost_packets
;
3803 struct lttng_channel_extended
*extended
;
3805 extended
= (struct lttng_channel_extended
*)
3806 kchan
->channel
->attr
.extended
.ptr
;
3808 ret
= get_kernel_runtime_stats(session
, kchan
,
3809 &discarded_events
, &lost_packets
);
3811 ret_code
= LTTNG_ERR_UNK
;
3816 * Update the discarded_events and lost_packets
3817 * count for the channel
3819 extended
->discarded_events
= discarded_events
;
3820 extended
->lost_packets
= lost_packets
;
3822 ret
= lttng_channel_serialize(
3823 kchan
->channel
, &payload
->buffer
);
3825 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3826 kchan
->channel
->name
);
3827 ret_code
= LTTNG_ERR_UNK
;
3836 case LTTNG_DOMAIN_UST
:
3838 struct lttng_ht_iter iter
;
3839 struct ltt_ust_channel
*uchan
;
3842 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3843 &iter
.iter
, uchan
, node
.node
) {
3844 uint64_t discarded_events
= 0, lost_packets
= 0;
3845 struct lttng_channel
*channel
= NULL
;
3846 struct lttng_channel_extended
*extended
;
3848 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3850 ret_code
= LTTNG_ERR_NOMEM
;
3854 extended
= (struct lttng_channel_extended
*)
3855 channel
->attr
.extended
.ptr
;
3857 ret
= get_ust_runtime_stats(session
, uchan
,
3858 &discarded_events
, &lost_packets
);
3860 lttng_channel_destroy(channel
);
3861 ret_code
= LTTNG_ERR_UNK
;
3865 extended
->discarded_events
= discarded_events
;
3866 extended
->lost_packets
= lost_packets
;
3868 ret
= lttng_channel_serialize(
3869 channel
, &payload
->buffer
);
3871 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3873 lttng_channel_destroy(channel
);
3874 ret_code
= LTTNG_ERR_UNK
;
3878 lttng_channel_destroy(channel
);
3888 if (i
> UINT32_MAX
) {
3889 ERR("Channel count would overflow the channel listing command's reply");
3890 ret_code
= LTTNG_ERR_OVERFLOW
;
3894 /* Update command reply header. */
3895 cmd_header
.count
= (uint32_t) i
;
3896 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3897 sizeof(cmd_header
));
3898 ret_code
= LTTNG_OK
;
3905 * Command LTTNG_LIST_EVENTS processed by the client thread.
3907 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3908 struct ltt_session
*session
,
3910 struct lttng_payload
*reply_payload
)
3912 int buffer_resize_ret
;
3913 enum lttng_error_code ret_code
= LTTNG_OK
;
3914 struct lttcomm_list_command_header reply_command_header
= {};
3915 size_t reply_command_header_offset
;
3916 unsigned int nb_events
= 0;
3918 assert(reply_payload
);
3920 /* Reserve space for command reply header. */
3921 reply_command_header_offset
= reply_payload
->buffer
.size
;
3922 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3923 reply_command_header_offset
+
3924 sizeof(struct lttcomm_list_command_header
));
3925 if (buffer_resize_ret
) {
3926 ret_code
= LTTNG_ERR_NOMEM
;
3931 case LTTNG_DOMAIN_KERNEL
:
3932 if (session
->kernel_session
!= NULL
) {
3933 ret_code
= list_lttng_kernel_events(channel_name
,
3934 session
->kernel_session
, reply_payload
, &nb_events
);
3938 case LTTNG_DOMAIN_UST
:
3940 if (session
->ust_session
!= NULL
) {
3941 ret_code
= list_lttng_ust_global_events(channel_name
,
3942 &session
->ust_session
->domain_global
,
3943 reply_payload
, &nb_events
);
3948 case LTTNG_DOMAIN_LOG4J
:
3949 case LTTNG_DOMAIN_JUL
:
3950 case LTTNG_DOMAIN_PYTHON
:
3951 if (session
->ust_session
) {
3952 struct lttng_ht_iter iter
;
3956 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3957 &iter
.iter
, agt
, node
.node
) {
3958 if (agt
->domain
== domain
) {
3959 ret_code
= list_lttng_agent_events(
3960 agt
, reply_payload
, &nb_events
);
3969 ret_code
= LTTNG_ERR_UND
;
3973 if (nb_events
> UINT32_MAX
) {
3974 ret_code
= LTTNG_ERR_OVERFLOW
;
3978 /* Update command reply header. */
3979 reply_command_header
.count
= (uint32_t) nb_events
;
3980 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
3981 sizeof(reply_command_header
));
3988 * Using the session list, filled a lttng_session array to send back to the
3989 * client for session listing.
3991 * The session list lock MUST be acquired before calling this function. Use
3992 * session_lock_list() and session_unlock_list().
3994 enum lttng_error_code
cmd_list_lttng_sessions(
3995 struct lttng_payload
*reply_payload
, uid_t uid
, gid_t gid
)
3997 int buffer_resize_ret
;
3998 enum lttng_error_code ret_code
= LTTNG_OK
;
3999 struct lttcomm_list_command_header reply_command_header
= {};
4000 size_t reply_command_header_offset
;
4001 struct ltt_session
*session
;
4002 struct ltt_session_list
*list
= session_get_list();
4006 assert(reply_payload
);
4008 /* Reserve space for command reply header. */
4009 reply_command_header_offset
= reply_payload
->buffer
.size
;
4010 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
4011 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
4012 if (buffer_resize_ret
) {
4013 ret_code
= LTTNG_ERR_NOMEM
;
4017 DBG("Getting all available session for UID %d GID %d",
4020 cds_list_for_each_entry(session
, &list
->head
, list
) {
4021 struct lttng_session tmp_session
= {};
4022 struct lttng_session_extended tmp_extended
= {};
4024 tmp_session
.extended
.ptr
= &tmp_extended
;
4026 if (!session_get(session
)) {
4030 * Only list the sessions the user can control.
4032 if (!session_access_ok(session
, uid
) ||
4033 session
->destroyed
) {
4034 session_put(session
);
4038 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4039 struct ltt_ust_session
*usess
= session
->ust_session
;
4041 if (session
->consumer
->type
== CONSUMER_DST_NET
||
4042 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
4043 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
4044 ret
= build_network_session_path(
4045 tmp_session
.path
, sizeof(tmp_session
.path
), session
);
4047 ret
= snprintf(tmp_session
.path
, sizeof(tmp_session
.path
), "%s",
4048 session
->consumer
->dst
.session_root_path
);
4051 PERROR("snprintf session path");
4052 session_put(session
);
4056 strncpy(tmp_session
.name
, session
->name
, NAME_MAX
);
4057 tmp_session
.name
[NAME_MAX
- 1] = '\0';
4058 tmp_session
.enabled
= session
->active
;
4059 tmp_session
.snapshot_mode
= session
->snapshot_mode
;
4060 tmp_session
.live_timer_interval
= session
->live_timer
;
4061 LTTNG_OPTIONAL_SET(&tmp_extended
.creation_time
, (uint64_t) session
->creation_time
);
4062 ret
= lttng_session_serialize(&tmp_session
, reply_payload
);
4064 ret_code
= LTTNG_ERR_FATAL
;
4068 session_put(session
);
4071 if (i
> UINT32_MAX
) {
4072 ret_code
= LTTNG_ERR_OVERFLOW
;
4076 /* Update command reply header. */
4077 reply_command_header
.count
= (uint32_t) i
;
4078 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
4079 sizeof(reply_command_header
));
4080 ret_code
= LTTNG_OK
;
4086 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4087 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4089 int cmd_data_pending(struct ltt_session
*session
)
4092 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4093 struct ltt_ust_session
*usess
= session
->ust_session
;
4095 LTTNG_ASSERT(session
);
4097 DBG("Data pending for session %s", session
->name
);
4099 /* Session MUST be stopped to ask for data availability. */
4100 if (session
->active
) {
4101 ret
= LTTNG_ERR_SESSION_STARTED
;
4105 * If stopped, just make sure we've started before else the above call
4106 * will always send that there is data pending.
4108 * The consumer assumes that when the data pending command is received,
4109 * the trace has been started before or else no output data is written
4110 * by the streams which is a condition for data pending. So, this is
4111 * *VERY* important that we don't ask the consumer before a start
4114 if (!session
->has_been_started
) {
4120 /* A rotation is still pending, we have to wait. */
4121 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4122 DBG("Rotate still pending for session %s", session
->name
);
4127 if (ksess
&& ksess
->consumer
) {
4128 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4130 /* Data is still being extracted for the kernel. */
4135 if (usess
&& usess
->consumer
) {
4136 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4138 /* Data is still being extracted for the kernel. */
4143 /* Data is ready to be read by a viewer */
4151 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4153 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4155 int cmd_snapshot_add_output(struct ltt_session
*session
,
4156 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4159 struct snapshot_output
*new_output
;
4161 LTTNG_ASSERT(session
);
4162 LTTNG_ASSERT(output
);
4164 DBG("Cmd snapshot add output for session %s", session
->name
);
4167 * Can't create an output if the session is not set in no-output mode.
4169 if (session
->output_traces
) {
4170 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4174 if (session
->has_non_mmap_channel
) {
4175 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4179 /* Only one output is allowed until we have the "tee" feature. */
4180 if (session
->snapshot
.nb_output
== 1) {
4181 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4185 new_output
= snapshot_output_alloc();
4187 ret
= LTTNG_ERR_NOMEM
;
4191 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4192 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4193 &session
->snapshot
);
4195 if (ret
== -ENOMEM
) {
4196 ret
= LTTNG_ERR_NOMEM
;
4198 ret
= LTTNG_ERR_INVALID
;
4204 snapshot_add_output(&session
->snapshot
, new_output
);
4206 *id
= new_output
->id
;
4213 snapshot_output_destroy(new_output
);
4219 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4221 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4223 int cmd_snapshot_del_output(struct ltt_session
*session
,
4224 const struct lttng_snapshot_output
*output
)
4227 struct snapshot_output
*sout
= NULL
;
4229 LTTNG_ASSERT(session
);
4230 LTTNG_ASSERT(output
);
4235 * Permission denied to create an output if the session is not
4236 * set in no output mode.
4238 if (session
->output_traces
) {
4239 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4244 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4246 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4247 } else if (*output
->name
!= '\0') {
4248 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4250 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4253 ret
= LTTNG_ERR_INVALID
;
4257 snapshot_delete_output(&session
->snapshot
, sout
);
4258 snapshot_output_destroy(sout
);
4267 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4269 * If no output is available, outputs is untouched and 0 is returned.
4271 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4273 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4274 struct lttng_snapshot_output
**outputs
)
4277 struct lttng_snapshot_output
*list
= NULL
;
4278 struct lttng_ht_iter iter
;
4279 struct snapshot_output
*output
;
4281 LTTNG_ASSERT(session
);
4282 LTTNG_ASSERT(outputs
);
4284 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4287 * Permission denied to create an output if the session is not
4288 * set in no output mode.
4290 if (session
->output_traces
) {
4291 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4295 if (session
->snapshot
.nb_output
== 0) {
4300 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4302 ret
= -LTTNG_ERR_NOMEM
;
4306 /* Copy list from session to the new list object. */
4308 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4309 output
, node
.node
) {
4310 LTTNG_ASSERT(output
->consumer
);
4311 list
[idx
].id
= output
->id
;
4312 list
[idx
].max_size
= output
->max_size
;
4313 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4314 sizeof(list
[idx
].name
))) {
4315 ret
= -LTTNG_ERR_INVALID
;
4318 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4319 if (lttng_strncpy(list
[idx
].ctrl_url
,
4320 output
->consumer
->dst
.session_root_path
,
4321 sizeof(list
[idx
].ctrl_url
))) {
4322 ret
= -LTTNG_ERR_INVALID
;
4327 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4328 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4330 ret
= -LTTNG_ERR_NOMEM
;
4335 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4336 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4338 ret
= -LTTNG_ERR_NOMEM
;
4347 ret
= session
->snapshot
.nb_output
;
4356 * Check if we can regenerate the metadata for this session.
4357 * Only kernel, UST per-uid and non-live sessions are supported.
4359 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4362 int check_regenerate_metadata_support(struct ltt_session
*session
)
4366 LTTNG_ASSERT(session
);
4368 if (session
->live_timer
!= 0) {
4369 ret
= LTTNG_ERR_LIVE_SESSION
;
4372 if (!session
->active
) {
4373 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4376 if (session
->ust_session
) {
4377 switch (session
->ust_session
->buffer_type
) {
4378 case LTTNG_BUFFER_PER_UID
:
4380 case LTTNG_BUFFER_PER_PID
:
4381 ret
= LTTNG_ERR_PER_PID_SESSION
;
4385 ret
= LTTNG_ERR_UNK
;
4389 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4390 session
->consumer
->relay_minor_version
< 8) {
4391 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4401 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4403 * Ask the consumer to truncate the existing metadata file(s) and
4404 * then regenerate the metadata. Live and per-pid sessions are not
4405 * supported and return an error.
4407 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4409 int cmd_regenerate_metadata(struct ltt_session
*session
)
4413 LTTNG_ASSERT(session
);
4415 ret
= check_regenerate_metadata_support(session
);
4420 if (session
->kernel_session
) {
4421 ret
= kernctl_session_regenerate_metadata(
4422 session
->kernel_session
->fd
);
4424 ERR("Failed to regenerate the kernel metadata");
4429 if (session
->ust_session
) {
4430 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4432 ERR("Failed to regenerate the UST metadata");
4436 DBG("Cmd metadata regenerate for session %s", session
->name
);
4444 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4446 * Ask the tracer to regenerate a new statedump.
4448 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4450 int cmd_regenerate_statedump(struct ltt_session
*session
)
4454 LTTNG_ASSERT(session
);
4456 if (!session
->active
) {
4457 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4461 if (session
->kernel_session
) {
4462 ret
= kernctl_session_regenerate_statedump(
4463 session
->kernel_session
->fd
);
4465 * Currently, the statedump in kernel can only fail if out
4469 if (ret
== -ENOMEM
) {
4470 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4472 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4474 ERR("Failed to regenerate the kernel statedump");
4479 if (session
->ust_session
) {
4480 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4482 * Currently, the statedump in UST always returns 0.
4485 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4486 ERR("Failed to regenerate the UST statedump");
4490 DBG("Cmd regenerate statedump for session %s", session
->name
);
4498 enum lttng_error_code
synchronize_tracer_notifier_register(
4499 struct notification_thread_handle
*notification_thread
,
4500 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4502 enum lttng_error_code ret_code
;
4503 const struct lttng_condition
*condition
=
4504 lttng_trigger_get_const_condition(trigger
);
4505 const char *trigger_name
;
4506 uid_t trigger_owner
;
4507 enum lttng_trigger_status trigger_status
;
4508 const enum lttng_domain_type trigger_domain
=
4509 lttng_trigger_get_underlying_domain_type_restriction(
4512 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4513 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4515 LTTNG_ASSERT(condition
);
4516 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4517 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4519 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4520 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4521 trigger_name
: "(anonymous)";
4523 session_lock_list();
4524 switch (trigger_domain
) {
4525 case LTTNG_DOMAIN_KERNEL
:
4527 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4528 if (ret_code
!= LTTNG_OK
) {
4529 enum lttng_error_code notif_thread_unregister_ret
;
4531 notif_thread_unregister_ret
=
4532 notification_thread_command_unregister_trigger(
4533 notification_thread
, trigger
);
4535 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4536 /* Return the original error code. */
4537 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4539 (int) trigger_owner
,
4545 case LTTNG_DOMAIN_UST
:
4546 ust_app_global_update_all_event_notifier_rules();
4548 case LTTNG_DOMAIN_JUL
:
4549 case LTTNG_DOMAIN_LOG4J
:
4550 case LTTNG_DOMAIN_PYTHON
:
4552 /* Agent domains. */
4553 struct agent
*agt
= agent_find_by_event_notifier_domain(
4557 agt
= agent_create(trigger_domain
);
4559 ret_code
= LTTNG_ERR_NOMEM
;
4560 goto end_unlock_session_list
;
4563 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4566 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4567 if (ret_code
!= LTTNG_OK
) {
4568 goto end_unlock_session_list
;
4573 case LTTNG_DOMAIN_NONE
:
4578 ret_code
= LTTNG_OK
;
4579 end_unlock_session_list
:
4580 session_unlock_list();
4584 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4585 struct lttng_trigger
*trigger
,
4586 bool is_trigger_anonymous
,
4587 struct notification_thread_handle
*notification_thread
,
4588 struct lttng_trigger
**return_trigger
)
4590 enum lttng_error_code ret_code
;
4591 const char *trigger_name
;
4592 uid_t trigger_owner
;
4593 enum lttng_trigger_status trigger_status
;
4595 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4596 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4597 trigger_name
: "(anonymous)";
4599 trigger_status
= lttng_trigger_get_owner_uid(
4600 trigger
, &trigger_owner
);
4601 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4603 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4604 trigger_name
, (int) trigger_owner
,
4605 (int) lttng_credentials_get_uid(cmd_creds
));
4608 * Validate the trigger credentials against the command credentials.
4609 * Only the root user can register a trigger with non-matching
4612 if (!lttng_credentials_is_equal_uid(
4613 lttng_trigger_get_credentials(trigger
),
4615 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4616 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4617 trigger_name
, (int) trigger_owner
,
4618 (int) lttng_credentials_get_uid(cmd_creds
));
4619 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4625 * The bytecode generation also serves as a validation step for the
4626 * bytecode expressions.
4628 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4629 if (ret_code
!= LTTNG_OK
) {
4630 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4631 trigger_name
, (int) trigger_owner
, ret_code
);
4636 * A reference to the trigger is acquired by the notification thread.
4637 * It is safe to return the same trigger to the caller since it the
4638 * other user holds a reference.
4640 * The trigger is modified during the execution of the
4641 * "register trigger" command. However, by the time the command returns,
4642 * it is safe to use without any locking as its properties are
4645 ret_code
= notification_thread_command_register_trigger(
4646 notification_thread
, trigger
, is_trigger_anonymous
);
4647 if (ret_code
!= LTTNG_OK
) {
4648 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4649 trigger_name
, (int) trigger_owner
, ret_code
);
4653 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4654 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4655 trigger_name
: "(anonymous)";
4658 * Synchronize tracers if the trigger adds an event notifier.
4660 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4661 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4662 trigger
, cmd_creds
);
4663 if (ret_code
!= LTTNG_OK
) {
4664 ERR("Error registering tracer notifier: %s",
4665 lttng_strerror(-ret_code
));
4671 * Return an updated trigger to the client.
4673 * Since a modified version of the same trigger is returned, acquire a
4674 * reference to the trigger so the caller doesn't have to care if those
4675 * are distinct instances or not.
4677 if (ret_code
== LTTNG_OK
) {
4678 lttng_trigger_get(trigger
);
4679 *return_trigger
= trigger
;
4680 /* Ownership of trigger was transferred to caller. */
4688 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4689 const struct lttng_trigger
*trigger
)
4691 enum lttng_error_code ret_code
;
4692 const struct lttng_condition
*condition
=
4693 lttng_trigger_get_const_condition(trigger
);
4694 const enum lttng_domain_type trigger_domain
=
4695 lttng_trigger_get_underlying_domain_type_restriction(
4698 LTTNG_ASSERT(condition
);
4699 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4700 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4702 session_lock_list();
4703 switch (trigger_domain
) {
4704 case LTTNG_DOMAIN_KERNEL
:
4705 ret_code
= kernel_unregister_event_notifier(trigger
);
4706 if (ret_code
!= LTTNG_OK
) {
4707 goto end_unlock_session_list
;
4711 case LTTNG_DOMAIN_UST
:
4712 ust_app_global_update_all_event_notifier_rules();
4714 case LTTNG_DOMAIN_JUL
:
4715 case LTTNG_DOMAIN_LOG4J
:
4716 case LTTNG_DOMAIN_PYTHON
:
4718 /* Agent domains. */
4719 struct agent
*agt
= agent_find_by_event_notifier_domain(
4723 * This trigger was never registered in the first place. Calling
4724 * this function under those circumstances is an internal error.
4727 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4728 if (ret_code
!= LTTNG_OK
) {
4729 goto end_unlock_session_list
;
4734 case LTTNG_DOMAIN_NONE
:
4739 ret_code
= LTTNG_OK
;
4741 end_unlock_session_list
:
4742 session_unlock_list();
4746 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4747 const struct lttng_trigger
*trigger
,
4748 struct notification_thread_handle
*notification_thread
)
4750 enum lttng_error_code ret_code
;
4751 const char *trigger_name
;
4752 uid_t trigger_owner
;
4753 enum lttng_trigger_status trigger_status
;
4754 struct lttng_trigger
*sessiond_trigger
= NULL
;
4756 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4757 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4758 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4759 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4761 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4762 trigger_name
, (int) trigger_owner
,
4763 (int) lttng_credentials_get_uid(cmd_creds
));
4766 * Validate the trigger credentials against the command credentials.
4767 * Only the root user can unregister a trigger with non-matching
4770 if (!lttng_credentials_is_equal_uid(
4771 lttng_trigger_get_credentials(trigger
),
4773 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4774 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4775 trigger_name
, (int) trigger_owner
,
4776 (int) lttng_credentials_get_uid(cmd_creds
));
4777 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4782 /* Fetch the sessiond side trigger object. */
4783 ret_code
= notification_thread_command_get_trigger(
4784 notification_thread
, trigger
, &sessiond_trigger
);
4785 if (ret_code
!= LTTNG_OK
) {
4786 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4787 trigger_name
, (int) trigger_owner
, ret_code
);
4791 LTTNG_ASSERT(sessiond_trigger
);
4794 * From this point on, no matter what, consider the trigger
4797 * We set the unregistered state of the sessiond side trigger object in
4798 * the client thread since we want to minimize the possibility of the
4799 * notification thread being stalled due to a long execution of an
4800 * action that required the trigger lock.
4802 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4804 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4806 if (ret_code
!= LTTNG_OK
) {
4807 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4808 trigger_name
, (int) trigger_owner
, ret_code
);
4813 * Synchronize tracers if the trigger removes an event notifier.
4814 * Do this even if the trigger unregistration failed to at least stop
4815 * the tracers from producing notifications associated with this
4818 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4819 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4820 if (ret_code
!= LTTNG_OK
) {
4821 ERR("Error unregistering trigger to tracer.");
4828 lttng_trigger_put(sessiond_trigger
);
4832 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4833 struct notification_thread_handle
*notification_thread
,
4834 struct lttng_triggers
**return_triggers
)
4837 enum lttng_error_code ret_code
;
4838 struct lttng_triggers
*triggers
= NULL
;
4840 /* Get the set of triggers from the notification thread. */
4841 ret_code
= notification_thread_command_list_triggers(
4842 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4843 if (ret_code
!= LTTNG_OK
) {
4847 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4849 ret_code
= LTTNG_ERR_UNK
;
4853 *return_triggers
= triggers
;
4855 ret_code
= LTTNG_OK
;
4857 lttng_triggers_destroy(triggers
);
4861 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4862 const struct lttng_error_query
*query
,
4863 struct lttng_error_query_results
**_results
,
4864 struct notification_thread_handle
*notification_thread
)
4866 enum lttng_error_code ret_code
;
4867 const struct lttng_trigger
*query_target_trigger
;
4868 const struct lttng_action
*query_target_action
= NULL
;
4869 struct lttng_trigger
*matching_trigger
= NULL
;
4870 const char *trigger_name
;
4871 uid_t trigger_owner
;
4872 enum lttng_trigger_status trigger_status
;
4873 struct lttng_error_query_results
*results
= NULL
;
4875 switch (lttng_error_query_get_target_type(query
)) {
4876 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4877 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4879 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4880 query_target_trigger
=
4881 lttng_error_query_condition_borrow_target(query
);
4883 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4884 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4891 LTTNG_ASSERT(query_target_trigger
);
4893 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4894 query_target_trigger
, &matching_trigger
);
4895 if (ret_code
!= LTTNG_OK
) {
4899 /* No longer needed. */
4900 query_target_trigger
= NULL
;
4902 if (lttng_error_query_get_target_type(query
) ==
4903 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4904 /* Get the sessiond-side version of the target action. */
4905 query_target_action
=
4906 lttng_error_query_action_borrow_action_target(
4907 query
, matching_trigger
);
4910 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4911 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4912 trigger_name
: "(anonymous)";
4913 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4915 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4917 results
= lttng_error_query_results_create();
4919 ret_code
= LTTNG_ERR_NOMEM
;
4923 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4924 trigger_name
, (int) trigger_owner
,
4925 (int) lttng_credentials_get_uid(cmd_creds
));
4928 * Validate the trigger credentials against the command credentials.
4929 * Only the root user can target a trigger with non-matching
4932 if (!lttng_credentials_is_equal_uid(
4933 lttng_trigger_get_credentials(matching_trigger
),
4935 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4936 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4937 trigger_name
, (int) trigger_owner
,
4938 (int) lttng_credentials_get_uid(cmd_creds
));
4939 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4944 switch (lttng_error_query_get_target_type(query
)) {
4945 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4946 trigger_status
= lttng_trigger_add_error_results(
4947 matching_trigger
, results
);
4949 switch (trigger_status
) {
4950 case LTTNG_TRIGGER_STATUS_OK
:
4953 ret_code
= LTTNG_ERR_UNK
;
4958 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4960 trigger_status
= lttng_trigger_condition_add_error_results(
4961 matching_trigger
, results
);
4963 switch (trigger_status
) {
4964 case LTTNG_TRIGGER_STATUS_OK
:
4967 ret_code
= LTTNG_ERR_UNK
;
4973 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4975 const enum lttng_action_status action_status
=
4976 lttng_action_add_error_query_results(
4977 query_target_action
, results
);
4979 switch (action_status
) {
4980 case LTTNG_ACTION_STATUS_OK
:
4983 ret_code
= LTTNG_ERR_UNK
;
4994 *_results
= results
;
4996 ret_code
= LTTNG_OK
;
4998 lttng_trigger_put(matching_trigger
);
4999 lttng_error_query_results_destroy(results
);
5004 * Send relayd sockets from snapshot output to consumer. Ignore request if the
5005 * snapshot output is *not* set with a remote destination.
5007 * Return LTTNG_OK on success or a LTTNG_ERR code.
5009 static enum lttng_error_code
set_relayd_for_snapshot(
5010 struct consumer_output
*output
,
5011 const struct ltt_session
*session
)
5013 enum lttng_error_code status
= LTTNG_OK
;
5014 struct lttng_ht_iter iter
;
5015 struct consumer_socket
*socket
;
5016 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
5017 const char *base_path
;
5019 LTTNG_ASSERT(output
);
5020 LTTNG_ASSERT(session
);
5022 DBG2("Set relayd object from snapshot output");
5024 if (session
->current_trace_chunk
) {
5025 enum lttng_trace_chunk_status chunk_status
=
5026 lttng_trace_chunk_get_id(
5027 session
->current_trace_chunk
,
5028 ¤t_chunk_id
.value
);
5030 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5031 current_chunk_id
.is_set
= true;
5033 ERR("Failed to get current trace chunk id");
5034 status
= LTTNG_ERR_UNK
;
5039 /* Ignore if snapshot consumer output is not network. */
5040 if (output
->type
!= CONSUMER_DST_NET
) {
5045 * The snapshot record URI base path overrides the session
5048 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5049 base_path
= output
->dst
.net
.control
.subdir
;
5051 base_path
= session
->base_path
;
5055 * For each consumer socket, create and send the relayd object of the
5059 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
5060 socket
, node
.node
) {
5061 pthread_mutex_lock(socket
->lock
);
5062 status
= send_consumer_relayd_sockets(session
->id
,
5064 session
->name
, session
->hostname
,
5066 session
->live_timer
,
5067 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
5068 session
->creation_time
,
5069 session
->name_contains_creation_time
);
5070 pthread_mutex_unlock(socket
->lock
);
5071 if (status
!= LTTNG_OK
) {
5083 * Record a kernel snapshot.
5085 * Return LTTNG_OK on success or a LTTNG_ERR code.
5087 static enum lttng_error_code
record_kernel_snapshot(
5088 struct ltt_kernel_session
*ksess
,
5089 const struct consumer_output
*output
,
5090 const struct ltt_session
*session
,
5091 uint64_t nb_packets_per_stream
)
5093 enum lttng_error_code status
;
5095 LTTNG_ASSERT(ksess
);
5096 LTTNG_ASSERT(output
);
5097 LTTNG_ASSERT(session
);
5099 status
= kernel_snapshot_record(
5100 ksess
, output
, nb_packets_per_stream
);
5105 * Record a UST snapshot.
5107 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5109 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5110 const struct consumer_output
*output
,
5111 const struct ltt_session
*session
,
5112 uint64_t nb_packets_per_stream
)
5114 enum lttng_error_code status
;
5116 LTTNG_ASSERT(usess
);
5117 LTTNG_ASSERT(output
);
5118 LTTNG_ASSERT(session
);
5120 status
= ust_app_snapshot_record(
5121 usess
, output
, nb_packets_per_stream
);
5126 uint64_t get_session_size_one_more_packet_per_stream(
5127 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5129 uint64_t tot_size
= 0;
5131 if (session
->kernel_session
) {
5132 struct ltt_kernel_channel
*chan
;
5133 const struct ltt_kernel_session
*ksess
=
5134 session
->kernel_session
;
5136 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5137 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5139 * Don't take channel into account if we
5140 * already grab all its packets.
5144 tot_size
+= chan
->channel
->attr
.subbuf_size
5145 * chan
->stream_count
;
5149 if (session
->ust_session
) {
5150 const struct ltt_ust_session
*usess
= session
->ust_session
;
5152 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5160 * Calculate the number of packets we can grab from each stream that
5161 * fits within the overall snapshot max size.
5163 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5164 * the number of packets per stream.
5166 * TODO: this approach is not perfect: we consider the worse case
5167 * (packet filling the sub-buffers) as an upper bound, but we could do
5168 * better if we do this calculation while we actually grab the packet
5169 * content: we would know how much padding we don't actually store into
5172 * This algorithm is currently bounded by the number of packets per
5175 * Since we call this algorithm before actually grabbing the data, it's
5176 * an approximation: for instance, applications could appear/disappear
5177 * in between this call and actually grabbing data.
5180 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5184 uint64_t cur_nb_packets
= 0;
5187 return 0; /* Infinite */
5190 size_left
= max_size
;
5192 uint64_t one_more_packet_tot_size
;
5194 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5195 session
, cur_nb_packets
);
5196 if (!one_more_packet_tot_size
) {
5197 /* We are already grabbing all packets. */
5200 size_left
-= one_more_packet_tot_size
;
5201 if (size_left
< 0) {
5206 if (!cur_nb_packets
&& size_left
!= max_size
) {
5207 /* Not enough room to grab one packet of each stream, error. */
5210 return cur_nb_packets
;
5214 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5215 const struct snapshot_output
*snapshot_output
)
5217 int64_t nb_packets_per_stream
;
5218 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5220 enum lttng_error_code ret_code
= LTTNG_OK
;
5221 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5222 struct consumer_output
*original_ust_consumer_output
= NULL
;
5223 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5224 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5225 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5227 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5229 snapshot_output
->name
,
5230 snapshot_output
->datetime
,
5231 snapshot_output
->nb_snapshot
);
5232 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5233 ERR("Failed to format snapshot name");
5234 ret_code
= LTTNG_ERR_INVALID
;
5237 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5238 snapshot_output
->name
, session
->name
,
5239 snapshot_chunk_name
);
5240 if (!session
->kernel_session
&& !session
->ust_session
) {
5241 ERR("Failed to record snapshot as no channels exist");
5242 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5246 if (session
->kernel_session
) {
5247 original_kernel_consumer_output
=
5248 session
->kernel_session
->consumer
;
5249 snapshot_kernel_consumer_output
=
5250 consumer_copy_output(snapshot_output
->consumer
);
5251 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5252 snapshot_chunk_name
);
5254 /* Copy the original domain subdir. */
5255 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5256 original_kernel_consumer_output
->domain_subdir
);
5258 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5259 original_kernel_consumer_output
);
5261 ERR("Failed to copy consumer sockets from snapshot output configuration");
5262 ret_code
= LTTNG_ERR_NOMEM
;
5265 ret_code
= set_relayd_for_snapshot(
5266 snapshot_kernel_consumer_output
, session
);
5267 if (ret_code
!= LTTNG_OK
) {
5268 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5271 session
->kernel_session
->consumer
=
5272 snapshot_kernel_consumer_output
;
5274 if (session
->ust_session
) {
5275 original_ust_consumer_output
= session
->ust_session
->consumer
;
5276 snapshot_ust_consumer_output
=
5277 consumer_copy_output(snapshot_output
->consumer
);
5278 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5279 snapshot_chunk_name
);
5281 /* Copy the original domain subdir. */
5282 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5283 original_ust_consumer_output
->domain_subdir
);
5285 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5286 original_ust_consumer_output
);
5288 ERR("Failed to copy consumer sockets from snapshot output configuration");
5289 ret_code
= LTTNG_ERR_NOMEM
;
5292 ret_code
= set_relayd_for_snapshot(
5293 snapshot_ust_consumer_output
, session
);
5294 if (ret_code
!= LTTNG_OK
) {
5295 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5298 session
->ust_session
->consumer
=
5299 snapshot_ust_consumer_output
;
5302 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5303 snapshot_kernel_consumer_output
?:
5304 snapshot_ust_consumer_output
,
5305 consumer_output_get_base_path(
5306 snapshot_output
->consumer
),
5307 snapshot_chunk_name
);
5308 if (!snapshot_trace_chunk
) {
5309 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5311 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5314 LTTNG_ASSERT(!session
->current_trace_chunk
);
5315 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5316 lttng_trace_chunk_put(snapshot_trace_chunk
);
5317 snapshot_trace_chunk
= NULL
;
5319 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5321 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5325 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5326 snapshot_output
->max_size
);
5327 if (nb_packets_per_stream
< 0) {
5328 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5329 goto error_close_trace_chunk
;
5332 if (session
->kernel_session
) {
5333 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5334 snapshot_kernel_consumer_output
, session
,
5335 nb_packets_per_stream
);
5336 if (ret_code
!= LTTNG_OK
) {
5337 goto error_close_trace_chunk
;
5341 if (session
->ust_session
) {
5342 ret_code
= record_ust_snapshot(session
->ust_session
,
5343 snapshot_ust_consumer_output
, session
,
5344 nb_packets_per_stream
);
5345 if (ret_code
!= LTTNG_OK
) {
5346 goto error_close_trace_chunk
;
5350 error_close_trace_chunk
:
5351 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5352 ERR("Failed to release the current trace chunk of session \"%s\"",
5354 ret_code
= LTTNG_ERR_UNK
;
5357 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5358 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5360 * Don't goto end; make sure the chunk is closed for the session
5361 * to allow future snapshots.
5363 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5365 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5368 lttng_trace_chunk_put(snapshot_trace_chunk
);
5369 snapshot_trace_chunk
= NULL
;
5371 if (original_ust_consumer_output
) {
5372 session
->ust_session
->consumer
= original_ust_consumer_output
;
5374 if (original_kernel_consumer_output
) {
5375 session
->kernel_session
->consumer
=
5376 original_kernel_consumer_output
;
5378 consumer_output_put(snapshot_ust_consumer_output
);
5379 consumer_output_put(snapshot_kernel_consumer_output
);
5384 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5386 * The wait parameter is ignored so this call always wait for the snapshot to
5387 * complete before returning.
5389 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5391 int cmd_snapshot_record(struct ltt_session
*session
,
5392 const struct lttng_snapshot_output
*output
,
5393 int wait
__attribute__((unused
)))
5395 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5397 unsigned int snapshot_success
= 0;
5399 struct snapshot_output
*tmp_output
= NULL
;
5401 LTTNG_ASSERT(session
);
5402 LTTNG_ASSERT(output
);
5404 DBG("Cmd snapshot record for session %s", session
->name
);
5406 /* Get the datetime for the snapshot output directory. */
5407 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5410 cmd_ret
= LTTNG_ERR_INVALID
;
5415 * Permission denied to create an output if the session is not
5416 * set in no output mode.
5418 if (session
->output_traces
) {
5419 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5423 /* The session needs to be started at least once. */
5424 if (!session
->has_been_started
) {
5425 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5429 /* Use temporary output for the session. */
5430 if (*output
->ctrl_url
!= '\0') {
5431 tmp_output
= snapshot_output_alloc();
5433 cmd_ret
= LTTNG_ERR_NOMEM
;
5437 ret
= snapshot_output_init(session
, output
->max_size
,
5439 output
->ctrl_url
, output
->data_url
,
5443 if (ret
== -ENOMEM
) {
5444 cmd_ret
= LTTNG_ERR_NOMEM
;
5446 cmd_ret
= LTTNG_ERR_INVALID
;
5450 /* Use the global session count for the temporary snapshot. */
5451 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5453 /* Use the global datetime */
5454 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5455 cmd_ret
= snapshot_record(session
, tmp_output
);
5456 if (cmd_ret
!= LTTNG_OK
) {
5459 snapshot_success
= 1;
5461 struct snapshot_output
*sout
;
5462 struct lttng_ht_iter iter
;
5465 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5466 &iter
.iter
, sout
, node
.node
) {
5467 struct snapshot_output output_copy
;
5470 * Make a local copy of the output and override output
5471 * parameters with those provided as part of the
5474 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5476 if (output
->max_size
!= (uint64_t) -1ULL) {
5477 output_copy
.max_size
= output
->max_size
;
5480 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5481 memcpy(output_copy
.datetime
, datetime
,
5484 /* Use temporary name. */
5485 if (*output
->name
!= '\0') {
5486 if (lttng_strncpy(output_copy
.name
,
5488 sizeof(output_copy
.name
))) {
5489 cmd_ret
= LTTNG_ERR_INVALID
;
5495 cmd_ret
= snapshot_record(session
, &output_copy
);
5496 if (cmd_ret
!= LTTNG_OK
) {
5500 snapshot_success
= 1;
5505 if (snapshot_success
) {
5506 session
->snapshot
.nb_snapshot
++;
5508 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5513 snapshot_output_destroy(tmp_output
);
5519 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5521 int cmd_set_session_shm_path(struct ltt_session
*session
,
5522 const char *shm_path
)
5525 LTTNG_ASSERT(session
);
5528 * Can only set shm path before session is started.
5530 if (session
->has_been_started
) {
5531 return LTTNG_ERR_SESSION_STARTED
;
5534 strncpy(session
->shm_path
, shm_path
,
5535 sizeof(session
->shm_path
));
5536 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5542 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5544 * Ask the consumer to rotate the session output directory.
5545 * The session lock must be held.
5547 * Returns LTTNG_OK on success or else a negative LTTng error code.
5549 int cmd_rotate_session(struct ltt_session
*session
,
5550 struct lttng_rotate_session_return
*rotate_return
,
5551 bool quiet_rotation
,
5552 enum lttng_trace_chunk_command_type command
)
5555 uint64_t ongoing_rotation_chunk_id
;
5556 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5557 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5558 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5559 enum lttng_trace_chunk_status chunk_status
;
5560 bool failed_to_rotate
= false;
5561 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5563 LTTNG_ASSERT(session
);
5565 if (!session
->has_been_started
) {
5566 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5571 * Explicit rotation is not supported for live sessions.
5572 * However, live sessions can perform a quiet rotation on
5574 * Rotation is not supported for snapshot traces (no output).
5576 if ((!quiet_rotation
&& session
->live_timer
) ||
5577 !session
->output_traces
) {
5578 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5582 /* Unsupported feature in lttng-relayd before 2.11. */
5583 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5584 (session
->consumer
->relay_major_version
== 2 &&
5585 session
->consumer
->relay_minor_version
< 11)) {
5586 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5590 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5591 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5592 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5596 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5597 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5599 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5604 * After a stop, we only allow one rotation to occur, the other ones are
5605 * useless until a new start.
5607 if (session
->rotated_after_last_stop
) {
5608 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5610 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5615 * After a stop followed by a clear, disallow following rotations a they would
5616 * generate empty chunks.
5618 if (session
->cleared_after_last_stop
) {
5619 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5621 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5625 if (session
->active
) {
5626 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5628 if (!new_trace_chunk
) {
5629 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5635 * The current trace chunk becomes the chunk being archived.
5637 * After this point, "chunk_being_archived" must absolutely
5638 * be closed on the consumer(s), otherwise it will never be
5639 * cleaned-up, which will result in a leak.
5641 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5642 &chunk_being_archived
);
5644 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5648 if (session
->kernel_session
) {
5649 cmd_ret
= kernel_rotate_session(session
);
5650 if (cmd_ret
!= LTTNG_OK
) {
5651 failed_to_rotate
= true;
5652 rotation_fail_code
= cmd_ret
;
5655 if (session
->ust_session
) {
5656 cmd_ret
= ust_app_rotate_session(session
);
5657 if (cmd_ret
!= LTTNG_OK
) {
5658 failed_to_rotate
= true;
5659 rotation_fail_code
= cmd_ret
;
5663 if (!session
->active
) {
5664 session
->rotated_after_last_stop
= true;
5667 if (!chunk_being_archived
) {
5668 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5670 if (failed_to_rotate
) {
5671 cmd_ret
= rotation_fail_code
;
5678 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5679 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5680 &ongoing_rotation_chunk_id
);
5681 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5683 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5684 command
, session
->last_chunk_path
);
5686 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5690 if (failed_to_rotate
) {
5691 cmd_ret
= rotation_fail_code
;
5695 session
->quiet_rotation
= quiet_rotation
;
5696 ret
= timer_session_rotation_pending_check_start(session
,
5697 DEFAULT_ROTATE_PENDING_TIMER
);
5699 cmd_ret
= LTTNG_ERR_UNK
;
5703 if (rotate_return
) {
5704 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5707 session
->chunk_being_archived
= chunk_being_archived
;
5708 chunk_being_archived
= NULL
;
5709 if (!quiet_rotation
) {
5710 ret
= notification_thread_command_session_rotation_ongoing(
5711 the_notification_thread_handle
, session
->id
,
5712 ongoing_rotation_chunk_id
);
5713 if (ret
!= LTTNG_OK
) {
5714 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5716 cmd_ret
= (lttng_error_code
) ret
;
5720 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5721 session
->name
, ongoing_rotation_chunk_id
);
5723 lttng_trace_chunk_put(new_trace_chunk
);
5724 lttng_trace_chunk_put(chunk_being_archived
);
5725 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5728 if (session_reset_rotation_state(session
,
5729 LTTNG_ROTATION_STATE_ERROR
)) {
5730 ERR("Failed to reset rotation state of session \"%s\"",
5737 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5739 * Check if the session has finished its rotation.
5741 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5743 int cmd_rotate_get_info(struct ltt_session
*session
,
5744 struct lttng_rotation_get_info_return
*info_return
,
5745 uint64_t rotation_id
)
5747 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5748 enum lttng_rotation_state rotation_state
;
5750 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5751 session
->most_recent_chunk_id
.value
);
5753 if (session
->chunk_being_archived
) {
5754 enum lttng_trace_chunk_status chunk_status
;
5757 chunk_status
= lttng_trace_chunk_get_id(
5758 session
->chunk_being_archived
,
5760 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5762 rotation_state
= rotation_id
== chunk_id
?
5763 LTTNG_ROTATION_STATE_ONGOING
:
5764 LTTNG_ROTATION_STATE_EXPIRED
;
5766 if (session
->last_archived_chunk_id
.is_set
&&
5767 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5768 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5770 rotation_state
= session
->rotation_state
;
5774 switch (rotation_state
) {
5775 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5776 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5779 case LTTNG_ROTATION_STATE_EXPIRED
:
5780 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5781 rotation_id
, session
->name
);
5783 case LTTNG_ROTATION_STATE_ONGOING
:
5784 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5785 rotation_id
, session
->name
);
5787 case LTTNG_ROTATION_STATE_COMPLETED
:
5791 char *current_tracing_path_reply
;
5792 size_t current_tracing_path_reply_len
;
5794 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5795 rotation_id
, session
->name
);
5797 switch (session_get_consumer_destination_type(session
)) {
5798 case CONSUMER_DST_LOCAL
:
5799 current_tracing_path_reply
=
5800 info_return
->location
.local
.absolute_path
;
5801 current_tracing_path_reply_len
=
5802 sizeof(info_return
->location
.local
.absolute_path
);
5803 info_return
->location_type
=
5804 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5805 fmt_ret
= asprintf(&chunk_path
,
5806 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5807 session_get_base_path(session
),
5808 session
->last_archived_chunk_name
);
5809 if (fmt_ret
== -1) {
5810 PERROR("Failed to format the path of the last archived trace chunk");
5811 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5812 cmd_ret
= LTTNG_ERR_UNK
;
5816 case CONSUMER_DST_NET
:
5818 uint16_t ctrl_port
, data_port
;
5820 current_tracing_path_reply
=
5821 info_return
->location
.relay
.relative_path
;
5822 current_tracing_path_reply_len
=
5823 sizeof(info_return
->location
.relay
.relative_path
);
5824 /* Currently the only supported relay protocol. */
5825 info_return
->location
.relay
.protocol
=
5826 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5828 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5829 session_get_net_consumer_hostname(session
),
5830 sizeof(info_return
->location
.relay
.host
));
5832 ERR("Failed to copy host name to rotate_get_info reply");
5833 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5834 cmd_ret
= LTTNG_ERR_SET_URL
;
5838 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5839 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5840 info_return
->location
.relay
.ports
.data
= data_port
;
5841 info_return
->location_type
=
5842 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5843 chunk_path
= strdup(session
->last_chunk_path
);
5845 ERR("Failed to allocate the path of the last archived trace chunk");
5846 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5847 cmd_ret
= LTTNG_ERR_UNK
;
5856 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5857 chunk_path
, current_tracing_path_reply_len
);
5860 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5861 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5862 cmd_ret
= LTTNG_ERR_UNK
;
5868 case LTTNG_ROTATION_STATE_ERROR
:
5869 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5870 rotation_id
, session
->name
);
5878 info_return
->status
= (int32_t) rotation_state
;
5883 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5885 * Configure the automatic rotation parameters.
5886 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5887 * 'activate' to false means deactivate the rotation schedule and validate that
5888 * 'new_value' has the same value as the currently active value.
5890 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5892 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5893 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5895 struct notification_thread_handle
*notification_thread_handle
)
5898 uint64_t *parameter_value
;
5900 LTTNG_ASSERT(session
);
5902 DBG("Cmd rotate set schedule session %s", session
->name
);
5904 if (session
->live_timer
|| !session
->output_traces
) {
5905 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5906 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5910 switch (schedule_type
) {
5911 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5912 parameter_value
= &session
->rotate_size
;
5914 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5915 parameter_value
= &session
->rotate_timer_period
;
5916 if (new_value
>= UINT_MAX
) {
5917 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5918 new_value
, UINT_MAX
);
5919 ret
= LTTNG_ERR_INVALID
;
5924 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5925 ret
= LTTNG_ERR_INVALID
;
5929 /* Improper use of the API. */
5930 if (new_value
== -1ULL) {
5931 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5932 ret
= LTTNG_ERR_INVALID
;
5937 * As indicated in struct ltt_session's comments, a value of == 0 means
5938 * this schedule rotation type is not in use.
5940 * Reject the command if we were asked to activate a schedule that was
5943 if (activate
&& *parameter_value
!= 0) {
5944 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5945 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5950 * Reject the command if we were asked to deactivate a schedule that was
5953 if (!activate
&& *parameter_value
== 0) {
5954 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5955 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5960 * Reject the command if we were asked to deactivate a schedule that
5963 if (!activate
&& *parameter_value
!= new_value
) {
5964 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5965 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5969 *parameter_value
= activate
? new_value
: 0;
5971 switch (schedule_type
) {
5972 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5973 if (activate
&& session
->active
) {
5975 * Only start the timer if the session is active,
5976 * otherwise it will be started when the session starts.
5978 ret
= timer_session_rotation_schedule_timer_start(
5979 session
, new_value
);
5981 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5982 ret
= LTTNG_ERR_UNK
;
5986 ret
= timer_session_rotation_schedule_timer_stop(
5989 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5990 ret
= LTTNG_ERR_UNK
;
5995 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5997 ret
= subscribe_session_consumed_size_rotation(session
,
5998 new_value
, notification_thread_handle
);
6000 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
6001 ret
= LTTNG_ERR_UNK
;
6005 ret
= unsubscribe_session_consumed_size_rotation(session
,
6006 notification_thread_handle
);
6008 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
6009 ret
= LTTNG_ERR_UNK
;
6016 /* Would have been caught before. */
6028 /* Wait for a given path to be removed before continuing. */
6029 static enum lttng_error_code
wait_on_path(void *path_data
)
6031 const char *shm_path
= (const char *) path_data
;
6033 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6039 ret
= stat(shm_path
, &st
);
6041 if (errno
!= ENOENT
) {
6042 PERROR("stat() returned an error while checking for the existence of the shm path");
6044 DBG("shm path no longer exists, completing the destruction of session");
6048 if (!S_ISDIR(st
.st_mode
)) {
6049 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6054 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6060 * Returns a pointer to a handler to run on completion of a command.
6061 * Returns NULL if no handler has to be run for the last command executed.
6063 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6065 struct cmd_completion_handler
*handler
= current_completion_handler
;
6067 current_completion_handler
= NULL
;
6072 * Init command subsystem.
6077 * Set network sequence index to 1 for streams to match a relayd
6078 * socket on the consumer side.
6080 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6081 relayd_net_seq_idx
= 1;
6082 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6084 DBG("Command subsystem initialized");