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
cmd_enable_channel_internal(
1290 struct ltt_session
*session
,
1291 const struct lttng_domain
*domain
,
1292 const struct lttng_channel
*_attr
,
1295 enum lttng_error_code ret_code
;
1296 struct ltt_ust_session
*usess
= session
->ust_session
;
1297 struct lttng_ht
*chan_ht
;
1299 struct lttng_channel
*attr
= NULL
;
1301 LTTNG_ASSERT(session
);
1302 LTTNG_ASSERT(_attr
);
1303 LTTNG_ASSERT(domain
);
1305 attr
= lttng_channel_copy(_attr
);
1307 ret_code
= LTTNG_ERR_NOMEM
;
1311 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1313 /* Validate channel name */
1314 if (attr
->name
[0] == '.' ||
1315 memchr(attr
->name
, '/', len
) != NULL
) {
1316 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1320 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1325 * If the session is a live session, remove the switch timer, the
1326 * live timer does the same thing but sends also synchronisation
1327 * beacons for inactive streams.
1329 if (session
->live_timer
> 0) {
1330 attr
->attr
.live_timer_interval
= session
->live_timer
;
1331 attr
->attr
.switch_timer_interval
= 0;
1334 /* Check for feature support */
1335 switch (domain
->type
) {
1336 case LTTNG_DOMAIN_KERNEL
:
1338 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1339 /* Sampling position of buffer is not supported */
1340 WARN("Kernel tracer does not support buffer monitoring. "
1341 "Setting the monitor interval timer to 0 "
1342 "(disabled) for channel '%s' of session '%s'",
1343 attr
->name
, session
->name
);
1344 lttng_channel_set_monitor_timer_interval(attr
, 0);
1348 case LTTNG_DOMAIN_UST
:
1350 case LTTNG_DOMAIN_JUL
:
1351 case LTTNG_DOMAIN_LOG4J
:
1352 case LTTNG_DOMAIN_PYTHON
:
1353 if (!agent_tracing_is_enabled()) {
1354 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1355 ret_code
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1360 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1364 switch (domain
->type
) {
1365 case LTTNG_DOMAIN_KERNEL
:
1367 struct ltt_kernel_channel
*kchan
;
1369 kchan
= trace_kernel_get_channel_by_name(
1370 attr
->name
, session
->kernel_session
);
1371 if (kchan
== NULL
) {
1373 * Don't try to create a channel if the session has been started at
1374 * some point in time before. The tracer does not allow it.
1376 if (session
->has_been_started
) {
1377 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1381 if (session
->snapshot
.nb_output
> 0 ||
1382 session
->snapshot_mode
) {
1383 /* Enforce mmap output for snapshot sessions. */
1384 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1386 ret_code
= channel_kernel_create(
1387 session
->kernel_session
, attr
, wpipe
);
1388 if (attr
->name
[0] != '\0') {
1389 session
->kernel_session
->has_non_default_channel
= 1;
1392 ret_code
= channel_kernel_enable(session
->kernel_session
, kchan
);
1395 if (ret_code
!= LTTNG_OK
) {
1399 kernel_wait_quiescent();
1402 case LTTNG_DOMAIN_UST
:
1403 case LTTNG_DOMAIN_JUL
:
1404 case LTTNG_DOMAIN_LOG4J
:
1405 case LTTNG_DOMAIN_PYTHON
:
1407 struct ltt_ust_channel
*uchan
;
1412 * Current agent implementation limitations force us to allow
1413 * only one channel at once in "agent" subdomains. Each
1414 * subdomain has a default channel name which must be strictly
1417 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1418 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1419 LTTNG_SYMBOL_NAME_LEN
)) {
1420 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1423 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1424 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1425 LTTNG_SYMBOL_NAME_LEN
)) {
1426 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1429 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1430 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1431 LTTNG_SYMBOL_NAME_LEN
)) {
1432 ret_code
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1437 chan_ht
= usess
->domain_global
.channels
;
1439 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1440 if (uchan
== NULL
) {
1442 * Don't try to create a channel if the session has been started at
1443 * some point in time before. The tracer does not allow it.
1445 if (session
->has_been_started
) {
1446 ret_code
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1450 ret_code
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1451 if (attr
->name
[0] != '\0') {
1452 usess
->has_non_default_channel
= 1;
1455 ret_code
= channel_ust_enable(usess
, uchan
);
1460 ret_code
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1464 if (ret_code
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1465 session
->has_non_mmap_channel
= true;
1470 lttng_channel_destroy(attr
);
1474 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1475 struct ltt_session
*session
,
1476 enum lttng_domain_type domain
,
1477 enum lttng_process_attr process_attr
,
1478 enum lttng_tracking_policy
*policy
)
1480 enum lttng_error_code ret_code
= LTTNG_OK
;
1481 const struct process_attr_tracker
*tracker
;
1484 case LTTNG_DOMAIN_KERNEL
:
1485 if (!session
->kernel_session
) {
1486 ret_code
= LTTNG_ERR_INVALID
;
1489 tracker
= kernel_get_process_attr_tracker(
1490 session
->kernel_session
, process_attr
);
1492 case LTTNG_DOMAIN_UST
:
1493 if (!session
->ust_session
) {
1494 ret_code
= LTTNG_ERR_INVALID
;
1497 tracker
= trace_ust_get_process_attr_tracker(
1498 session
->ust_session
, process_attr
);
1501 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1505 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1507 ret_code
= LTTNG_ERR_INVALID
;
1513 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1514 struct ltt_session
*session
,
1515 enum lttng_domain_type domain
,
1516 enum lttng_process_attr process_attr
,
1517 enum lttng_tracking_policy policy
)
1519 enum lttng_error_code ret_code
= LTTNG_OK
;
1522 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1523 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1524 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1527 ret_code
= LTTNG_ERR_INVALID
;
1532 case LTTNG_DOMAIN_KERNEL
:
1533 if (!session
->kernel_session
) {
1534 ret_code
= LTTNG_ERR_INVALID
;
1537 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1538 session
->kernel_session
, process_attr
, policy
);
1540 case LTTNG_DOMAIN_UST
:
1541 if (!session
->ust_session
) {
1542 ret_code
= LTTNG_ERR_INVALID
;
1545 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1546 session
->ust_session
, process_attr
, policy
);
1549 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1556 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1557 struct ltt_session
*session
,
1558 enum lttng_domain_type domain
,
1559 enum lttng_process_attr process_attr
,
1560 const struct process_attr_value
*value
)
1562 enum lttng_error_code ret_code
= LTTNG_OK
;
1565 case LTTNG_DOMAIN_KERNEL
:
1566 if (!session
->kernel_session
) {
1567 ret_code
= LTTNG_ERR_INVALID
;
1570 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1571 session
->kernel_session
, process_attr
, value
);
1573 case LTTNG_DOMAIN_UST
:
1574 if (!session
->ust_session
) {
1575 ret_code
= LTTNG_ERR_INVALID
;
1578 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1579 session
->ust_session
, process_attr
, value
);
1582 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1589 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1590 struct ltt_session
*session
,
1591 enum lttng_domain_type domain
,
1592 enum lttng_process_attr process_attr
,
1593 const struct process_attr_value
*value
)
1595 enum lttng_error_code ret_code
= LTTNG_OK
;
1598 case LTTNG_DOMAIN_KERNEL
:
1599 if (!session
->kernel_session
) {
1600 ret_code
= LTTNG_ERR_INVALID
;
1603 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1604 session
->kernel_session
, process_attr
, value
);
1606 case LTTNG_DOMAIN_UST
:
1607 if (!session
->ust_session
) {
1608 ret_code
= LTTNG_ERR_INVALID
;
1611 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1612 session
->ust_session
, process_attr
, value
);
1615 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1622 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1623 struct ltt_session
*session
,
1624 enum lttng_domain_type domain
,
1625 enum lttng_process_attr process_attr
,
1626 struct lttng_process_attr_values
**values
)
1628 enum lttng_error_code ret_code
= LTTNG_OK
;
1629 const struct process_attr_tracker
*tracker
;
1630 enum process_attr_tracker_status status
;
1633 case LTTNG_DOMAIN_KERNEL
:
1634 if (!session
->kernel_session
) {
1635 ret_code
= LTTNG_ERR_INVALID
;
1638 tracker
= kernel_get_process_attr_tracker(
1639 session
->kernel_session
, process_attr
);
1641 case LTTNG_DOMAIN_UST
:
1642 if (!session
->ust_session
) {
1643 ret_code
= LTTNG_ERR_INVALID
;
1646 tracker
= trace_ust_get_process_attr_tracker(
1647 session
->ust_session
, process_attr
);
1650 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1655 ret_code
= LTTNG_ERR_INVALID
;
1659 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1661 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1662 ret_code
= LTTNG_OK
;
1664 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1665 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1667 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1668 ret_code
= LTTNG_ERR_NOMEM
;
1671 ret_code
= LTTNG_ERR_UNK
;
1680 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1682 int cmd_disable_event(struct command_ctx
*cmd_ctx
,
1683 struct lttng_event
*event
,
1684 char *filter_expression
,
1685 struct lttng_bytecode
*bytecode
,
1686 struct lttng_event_exclusion
*exclusion
)
1689 const char *event_name
;
1690 const struct ltt_session
*session
= cmd_ctx
->session
;
1691 const char *channel_name
= cmd_ctx
->lsm
.u
.disable
.channel_name
;
1692 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1694 DBG("Disable event command for event \'%s\'", event
->name
);
1697 * Filter and exclusions are simply not handled by the
1698 * disable event command at this time.
1702 (void) filter_expression
;
1705 /* Ignore the presence of filter or exclusion for the event */
1707 event
->exclusion
= 0;
1709 event_name
= event
->name
;
1711 /* Error out on unhandled search criteria */
1712 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1713 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1714 ret
= LTTNG_ERR_UNK
;
1721 case LTTNG_DOMAIN_KERNEL
:
1723 struct ltt_kernel_channel
*kchan
;
1724 struct ltt_kernel_session
*ksess
;
1726 ksess
= session
->kernel_session
;
1729 * If a non-default channel has been created in the
1730 * session, explicitely require that -c chan_name needs
1733 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1734 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1738 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1739 if (kchan
== NULL
) {
1740 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1744 switch (event
->type
) {
1745 case LTTNG_EVENT_ALL
:
1746 case LTTNG_EVENT_TRACEPOINT
:
1747 case LTTNG_EVENT_SYSCALL
:
1748 case LTTNG_EVENT_PROBE
:
1749 case LTTNG_EVENT_FUNCTION
:
1750 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1751 if (event_name
[0] == '\0') {
1752 ret
= event_kernel_disable_event(kchan
,
1755 ret
= event_kernel_disable_event(kchan
,
1756 event_name
, event
->type
);
1758 if (ret
!= LTTNG_OK
) {
1763 ret
= LTTNG_ERR_UNK
;
1767 kernel_wait_quiescent();
1770 case LTTNG_DOMAIN_UST
:
1772 struct ltt_ust_channel
*uchan
;
1773 struct ltt_ust_session
*usess
;
1775 usess
= session
->ust_session
;
1777 if (validate_ust_event_name(event_name
)) {
1778 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1783 * If a non-default channel has been created in the
1784 * session, explicitly require that -c chan_name needs
1787 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1788 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1792 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1794 if (uchan
== NULL
) {
1795 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1799 switch (event
->type
) {
1800 case LTTNG_EVENT_ALL
:
1802 * An empty event name means that everything
1803 * should be disabled.
1805 if (event
->name
[0] == '\0') {
1806 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1808 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1811 if (ret
!= LTTNG_OK
) {
1816 ret
= LTTNG_ERR_UNK
;
1820 DBG3("Disable UST event %s in channel %s completed", event_name
,
1824 case LTTNG_DOMAIN_LOG4J
:
1825 case LTTNG_DOMAIN_JUL
:
1826 case LTTNG_DOMAIN_PYTHON
:
1829 struct ltt_ust_session
*usess
= session
->ust_session
;
1831 LTTNG_ASSERT(usess
);
1833 switch (event
->type
) {
1834 case LTTNG_EVENT_ALL
:
1837 ret
= LTTNG_ERR_UNK
;
1841 agt
= trace_ust_find_agent(usess
, domain
);
1843 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1847 * An empty event name means that everything
1848 * should be disabled.
1850 if (event
->name
[0] == '\0') {
1851 ret
= event_agent_disable_all(usess
, agt
);
1853 ret
= event_agent_disable(usess
, agt
, event_name
);
1855 if (ret
!= LTTNG_OK
) {
1862 ret
= LTTNG_ERR_UND
;
1873 free(filter_expression
);
1878 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1880 int cmd_add_context(struct command_ctx
*cmd_ctx
,
1881 const struct lttng_event_context
*event_context
, int kwpipe
)
1883 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1884 const enum lttng_domain_type domain
= cmd_ctx
->lsm
.domain
.type
;
1885 const struct ltt_session
*session
= cmd_ctx
->session
;
1886 const char *channel_name
= cmd_ctx
->lsm
.u
.context
.channel_name
;
1889 * Don't try to add a context if the session has been started at
1890 * some point in time before. The tracer does not allow it and would
1891 * result in a corrupted trace.
1893 if (cmd_ctx
->session
->has_been_started
) {
1894 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1899 case LTTNG_DOMAIN_KERNEL
:
1900 LTTNG_ASSERT(session
->kernel_session
);
1902 if (session
->kernel_session
->channel_count
== 0) {
1903 /* Create default channel */
1904 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1905 if (ret
!= LTTNG_OK
) {
1908 chan_kern_created
= 1;
1910 /* Add kernel context to kernel tracer */
1911 ret
= context_kernel_add(session
->kernel_session
,
1912 event_context
, channel_name
);
1913 if (ret
!= LTTNG_OK
) {
1917 case LTTNG_DOMAIN_JUL
:
1918 case LTTNG_DOMAIN_LOG4J
:
1921 * Validate channel name.
1922 * If no channel name is given and the domain is JUL or LOG4J,
1923 * set it to the appropriate domain-specific channel name. If
1924 * a name is provided but does not match the expexted channel
1925 * name, return an error.
1927 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1928 strcmp(channel_name
,
1929 DEFAULT_JUL_CHANNEL_NAME
)) {
1930 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1932 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1933 strcmp(channel_name
,
1934 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1935 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1940 case LTTNG_DOMAIN_UST
:
1942 struct ltt_ust_session
*usess
= session
->ust_session
;
1943 unsigned int chan_count
;
1945 LTTNG_ASSERT(usess
);
1947 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1948 if (chan_count
== 0) {
1949 struct lttng_channel
*attr
;
1950 /* Create default channel */
1951 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1953 ret
= LTTNG_ERR_FATAL
;
1957 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1958 if (ret
!= LTTNG_OK
) {
1962 channel_attr_destroy(attr
);
1963 chan_ust_created
= 1;
1966 ret
= context_ust_add(usess
, domain
, event_context
,
1968 if (ret
!= LTTNG_OK
) {
1974 ret
= LTTNG_ERR_UND
;
1982 if (chan_kern_created
) {
1983 struct ltt_kernel_channel
*kchan
=
1984 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1985 session
->kernel_session
);
1986 /* Created previously, this should NOT fail. */
1987 LTTNG_ASSERT(kchan
);
1988 kernel_destroy_channel(kchan
);
1991 if (chan_ust_created
) {
1992 struct ltt_ust_channel
*uchan
=
1993 trace_ust_find_channel_by_name(
1994 session
->ust_session
->domain_global
.channels
,
1995 DEFAULT_CHANNEL_NAME
);
1996 /* Created previously, this should NOT fail. */
1997 LTTNG_ASSERT(uchan
);
1998 /* Remove from the channel list of the session. */
1999 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2001 trace_ust_destroy_channel(uchan
);
2007 static inline bool name_starts_with(const char *name
, const char *prefix
)
2009 const size_t max_cmp_len
= std::min(strlen(prefix
), (size_t) LTTNG_SYMBOL_NAME_LEN
);
2011 return !strncmp(name
, prefix
, max_cmp_len
);
2014 /* Perform userspace-specific event name validation */
2015 static int validate_ust_event_name(const char *name
)
2025 * Check name against all internal UST event component namespaces used
2028 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2029 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2030 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2039 * Internal version of cmd_enable_event() with a supplemental
2040 * "internal_event" flag which is used to enable internal events which should
2041 * be hidden from clients. Such events are used in the agent implementation to
2042 * enable the events through which all "agent" events are funeled.
2044 static int _cmd_enable_event(struct ltt_session
*session
,
2045 const struct lttng_domain
*domain
,
2046 char *channel_name
, struct lttng_event
*event
,
2047 char *filter_expression
,
2048 struct lttng_bytecode
*filter
,
2049 struct lttng_event_exclusion
*exclusion
,
2050 int wpipe
, bool internal_event
)
2052 int ret
= 0, channel_created
= 0;
2053 struct lttng_channel
*attr
= NULL
;
2055 LTTNG_ASSERT(session
);
2056 LTTNG_ASSERT(event
);
2057 LTTNG_ASSERT(channel_name
);
2059 /* If we have a filter, we must have its filter expression */
2060 LTTNG_ASSERT(!(!!filter_expression
^ !!filter
));
2062 /* Normalize event name as a globbing pattern */
2063 strutils_normalize_star_glob_pattern(event
->name
);
2065 /* Normalize exclusion names as globbing patterns */
2069 for (i
= 0; i
< exclusion
->count
; i
++) {
2070 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2072 strutils_normalize_star_glob_pattern(name
);
2076 DBG("Enable event command for event \'%s\'", event
->name
);
2080 switch (domain
->type
) {
2081 case LTTNG_DOMAIN_KERNEL
:
2083 struct ltt_kernel_channel
*kchan
;
2086 * If a non-default channel has been created in the
2087 * session, explicitely require that -c chan_name needs
2090 if (session
->kernel_session
->has_non_default_channel
2091 && channel_name
[0] == '\0') {
2092 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2096 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2097 session
->kernel_session
);
2098 if (kchan
== NULL
) {
2099 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2100 LTTNG_BUFFER_GLOBAL
);
2102 ret
= LTTNG_ERR_FATAL
;
2105 if (lttng_strncpy(attr
->name
, channel_name
,
2106 sizeof(attr
->name
))) {
2107 ret
= LTTNG_ERR_INVALID
;
2111 ret
= cmd_enable_channel_internal(
2112 session
, domain
, attr
, wpipe
);
2113 if (ret
!= LTTNG_OK
) {
2116 channel_created
= 1;
2119 /* Get the newly created kernel channel pointer */
2120 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2121 session
->kernel_session
);
2122 if (kchan
== NULL
) {
2123 /* This sould not happen... */
2124 ret
= LTTNG_ERR_FATAL
;
2128 switch (event
->type
) {
2129 case LTTNG_EVENT_ALL
:
2131 char *filter_expression_a
= NULL
;
2132 struct lttng_bytecode
*filter_a
= NULL
;
2135 * We need to duplicate filter_expression and filter,
2136 * because ownership is passed to first enable
2139 if (filter_expression
) {
2140 filter_expression_a
= strdup(filter_expression
);
2141 if (!filter_expression_a
) {
2142 ret
= LTTNG_ERR_FATAL
;
2147 filter_a
= zmalloc
<lttng_bytecode
>(sizeof(*filter_a
) + filter
->len
);
2149 free(filter_expression_a
);
2150 ret
= LTTNG_ERR_FATAL
;
2153 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2155 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2156 ret
= event_kernel_enable_event(kchan
, event
,
2157 filter_expression
, filter
);
2158 /* We have passed ownership */
2159 filter_expression
= NULL
;
2161 if (ret
!= LTTNG_OK
) {
2162 if (channel_created
) {
2163 /* Let's not leak a useless channel. */
2164 kernel_destroy_channel(kchan
);
2166 free(filter_expression_a
);
2170 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2171 ret
= event_kernel_enable_event(kchan
, event
,
2172 filter_expression_a
, filter_a
);
2173 /* We have passed ownership */
2174 filter_expression_a
= NULL
;
2176 if (ret
!= LTTNG_OK
) {
2181 case LTTNG_EVENT_PROBE
:
2182 case LTTNG_EVENT_USERSPACE_PROBE
:
2183 case LTTNG_EVENT_FUNCTION
:
2184 case LTTNG_EVENT_FUNCTION_ENTRY
:
2185 case LTTNG_EVENT_TRACEPOINT
:
2186 ret
= event_kernel_enable_event(kchan
, event
,
2187 filter_expression
, filter
);
2188 /* We have passed ownership */
2189 filter_expression
= NULL
;
2191 if (ret
!= LTTNG_OK
) {
2192 if (channel_created
) {
2193 /* Let's not leak a useless channel. */
2194 kernel_destroy_channel(kchan
);
2199 case LTTNG_EVENT_SYSCALL
:
2200 ret
= event_kernel_enable_event(kchan
, event
,
2201 filter_expression
, filter
);
2202 /* We have passed ownership */
2203 filter_expression
= NULL
;
2205 if (ret
!= LTTNG_OK
) {
2210 ret
= LTTNG_ERR_UNK
;
2214 kernel_wait_quiescent();
2217 case LTTNG_DOMAIN_UST
:
2219 struct ltt_ust_channel
*uchan
;
2220 struct ltt_ust_session
*usess
= session
->ust_session
;
2222 LTTNG_ASSERT(usess
);
2225 * If a non-default channel has been created in the
2226 * session, explicitely require that -c chan_name needs
2229 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2230 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2234 /* Get channel from global UST domain */
2235 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2237 if (uchan
== NULL
) {
2238 /* Create default channel */
2239 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2240 usess
->buffer_type
);
2242 ret
= LTTNG_ERR_FATAL
;
2245 if (lttng_strncpy(attr
->name
, channel_name
,
2246 sizeof(attr
->name
))) {
2247 ret
= LTTNG_ERR_INVALID
;
2251 ret
= cmd_enable_channel_internal(
2252 session
, domain
, attr
, wpipe
);
2253 if (ret
!= LTTNG_OK
) {
2257 /* Get the newly created channel reference back */
2258 uchan
= trace_ust_find_channel_by_name(
2259 usess
->domain_global
.channels
, channel_name
);
2260 LTTNG_ASSERT(uchan
);
2263 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2265 * Don't allow users to add UST events to channels which
2266 * are assigned to a userspace subdomain (JUL, Log4J,
2269 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2273 if (!internal_event
) {
2275 * Ensure the event name is not reserved for internal
2278 ret
= validate_ust_event_name(event
->name
);
2280 WARN("Userspace event name %s failed validation.",
2282 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2287 /* At this point, the session and channel exist on the tracer */
2288 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2289 filter_expression
, filter
, exclusion
,
2291 /* We have passed ownership */
2292 filter_expression
= NULL
;
2295 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2296 goto already_enabled
;
2297 } else if (ret
!= LTTNG_OK
) {
2302 case LTTNG_DOMAIN_LOG4J
:
2303 case LTTNG_DOMAIN_JUL
:
2304 case LTTNG_DOMAIN_PYTHON
:
2306 const char *default_event_name
, *default_chan_name
;
2308 struct lttng_event uevent
;
2309 struct lttng_domain tmp_dom
;
2310 struct ltt_ust_session
*usess
= session
->ust_session
;
2312 LTTNG_ASSERT(usess
);
2314 if (!agent_tracing_is_enabled()) {
2315 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2316 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2320 agt
= trace_ust_find_agent(usess
, domain
->type
);
2322 agt
= agent_create(domain
->type
);
2324 ret
= LTTNG_ERR_NOMEM
;
2327 agent_add(agt
, usess
->agents
);
2330 /* Create the default tracepoint. */
2331 memset(&uevent
, 0, sizeof(uevent
));
2332 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2333 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2334 default_event_name
= event_get_default_agent_ust_name(
2336 if (!default_event_name
) {
2337 ret
= LTTNG_ERR_FATAL
;
2340 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2341 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2344 * The domain type is changed because we are about to enable the
2345 * default channel and event for the JUL domain that are hardcoded.
2346 * This happens in the UST domain.
2348 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2349 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2351 switch (domain
->type
) {
2352 case LTTNG_DOMAIN_LOG4J
:
2353 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2355 case LTTNG_DOMAIN_JUL
:
2356 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2358 case LTTNG_DOMAIN_PYTHON
:
2359 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2362 /* The switch/case we are in makes this impossible */
2367 char *filter_expression_copy
= NULL
;
2368 struct lttng_bytecode
*filter_copy
= NULL
;
2371 const size_t filter_size
= sizeof(
2372 struct lttng_bytecode
)
2375 filter_copy
= zmalloc
<lttng_bytecode
>(filter_size
);
2377 ret
= LTTNG_ERR_NOMEM
;
2380 memcpy(filter_copy
, filter
, filter_size
);
2382 filter_expression_copy
=
2383 strdup(filter_expression
);
2384 if (!filter_expression
) {
2385 ret
= LTTNG_ERR_NOMEM
;
2388 if (!filter_expression_copy
|| !filter_copy
) {
2389 free(filter_expression_copy
);
2395 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2396 (char *) default_chan_name
,
2397 &uevent
, filter_expression_copy
,
2398 filter_copy
, NULL
, wpipe
);
2401 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2402 goto already_enabled
;
2403 } else if (ret
!= LTTNG_OK
) {
2407 /* The wild card * means that everything should be enabled. */
2408 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2409 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2412 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2416 filter_expression
= NULL
;
2417 if (ret
!= LTTNG_OK
) {
2424 ret
= LTTNG_ERR_UND
;
2432 free(filter_expression
);
2435 channel_attr_destroy(attr
);
2441 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2442 * We own filter, exclusion, and filter_expression.
2444 int cmd_enable_event(struct command_ctx
*cmd_ctx
,
2445 struct lttng_event
*event
,
2446 char *filter_expression
,
2447 struct lttng_event_exclusion
*exclusion
,
2448 struct lttng_bytecode
*bytecode
,
2453 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2455 const lttng_domain command_domain
= cmd_ctx
->lsm
.domain
;
2458 * The ownership of the following parameters is transferred to
2459 * _cmd_enable_event:
2461 * - filter_expression,
2465 ret
= _cmd_enable_event(cmd_ctx
->session
,
2467 cmd_ctx
->lsm
.u
.enable
.channel_name
, event
,
2468 filter_expression
, bytecode
, exclusion
, wpipe
, false);
2469 filter_expression
= NULL
;
2476 * Enable an event which is internal to LTTng. An internal should
2477 * never be made visible to clients and are immune to checks such as
2480 static int cmd_enable_event_internal(struct ltt_session
*session
,
2481 const struct lttng_domain
*domain
,
2482 char *channel_name
, struct lttng_event
*event
,
2483 char *filter_expression
,
2484 struct lttng_bytecode
*filter
,
2485 struct lttng_event_exclusion
*exclusion
,
2488 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2489 filter_expression
, filter
, exclusion
, wpipe
, true);
2493 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2495 enum lttng_error_code
cmd_list_tracepoints(enum lttng_domain_type domain
,
2496 struct lttng_payload
*reply_payload
)
2498 enum lttng_error_code ret_code
;
2500 ssize_t i
, nb_events
= 0;
2501 struct lttng_event
*events
= NULL
;
2502 struct lttcomm_list_command_header reply_command_header
= {};
2503 size_t reply_command_header_offset
;
2505 assert(reply_payload
);
2507 /* Reserve space for command reply header. */
2508 reply_command_header_offset
= reply_payload
->buffer
.size
;
2509 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2510 reply_command_header_offset
+
2511 sizeof(struct lttcomm_list_command_header
));
2513 ret_code
= LTTNG_ERR_NOMEM
;
2518 case LTTNG_DOMAIN_KERNEL
:
2519 nb_events
= kernel_list_events(&events
);
2520 if (nb_events
< 0) {
2521 ret_code
= LTTNG_ERR_KERN_LIST_FAIL
;
2525 case LTTNG_DOMAIN_UST
:
2526 nb_events
= ust_app_list_events(&events
);
2527 if (nb_events
< 0) {
2528 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2532 case LTTNG_DOMAIN_LOG4J
:
2533 case LTTNG_DOMAIN_JUL
:
2534 case LTTNG_DOMAIN_PYTHON
:
2535 nb_events
= agent_list_events(&events
, domain
);
2536 if (nb_events
< 0) {
2537 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2542 ret_code
= LTTNG_ERR_UND
;
2546 for (i
= 0; i
< nb_events
; i
++) {
2547 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2550 ret_code
= LTTNG_ERR_NOMEM
;
2555 if (nb_events
> UINT32_MAX
) {
2556 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2557 ret_code
= LTTNG_ERR_OVERFLOW
;
2561 /* Update command reply header. */
2562 reply_command_header
.count
= (uint32_t) nb_events
;
2563 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2564 sizeof(reply_command_header
));
2566 ret_code
= LTTNG_OK
;
2573 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2575 enum lttng_error_code
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2576 struct lttng_payload
*reply
)
2578 enum lttng_error_code ret_code
;
2580 unsigned int i
, nb_fields
;
2581 struct lttng_event_field
*fields
= NULL
;
2582 struct lttcomm_list_command_header reply_command_header
= {};
2583 size_t reply_command_header_offset
;
2587 /* Reserve space for command reply header. */
2588 reply_command_header_offset
= reply
->buffer
.size
;
2589 ret
= lttng_dynamic_buffer_set_size(&reply
->buffer
,
2590 reply_command_header_offset
+
2591 sizeof(struct lttcomm_list_command_header
));
2593 ret_code
= LTTNG_ERR_NOMEM
;
2598 case LTTNG_DOMAIN_UST
:
2599 ret
= ust_app_list_event_fields(&fields
);
2601 ret_code
= LTTNG_ERR_UST_LIST_FAIL
;
2606 case LTTNG_DOMAIN_KERNEL
:
2607 default: /* fall-through */
2608 ret_code
= LTTNG_ERR_UND
;
2614 for (i
= 0; i
< nb_fields
; i
++) {
2615 ret
= lttng_event_field_serialize(&fields
[i
], reply
);
2617 ret_code
= LTTNG_ERR_NOMEM
;
2622 if (nb_fields
> UINT32_MAX
) {
2623 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2624 ret_code
= LTTNG_ERR_OVERFLOW
;
2628 /* Update command reply header. */
2629 reply_command_header
.count
= (uint32_t) nb_fields
;
2631 memcpy(reply
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2632 sizeof(reply_command_header
));
2634 ret_code
= LTTNG_OK
;
2641 enum lttng_error_code
cmd_list_syscalls(
2642 struct lttng_payload
*reply_payload
)
2644 enum lttng_error_code ret_code
;
2645 ssize_t nb_events
, i
;
2647 struct lttng_event
*events
= NULL
;
2648 struct lttcomm_list_command_header reply_command_header
= {};
2649 size_t reply_command_header_offset
;
2651 assert(reply_payload
);
2653 /* Reserve space for command reply header. */
2654 reply_command_header_offset
= reply_payload
->buffer
.size
;
2655 ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
2656 reply_command_header_offset
+
2657 sizeof(struct lttcomm_list_command_header
));
2659 ret_code
= LTTNG_ERR_NOMEM
;
2663 nb_events
= syscall_table_list(&events
);
2664 if (nb_events
< 0) {
2665 ret_code
= (enum lttng_error_code
) -nb_events
;
2669 for (i
= 0; i
< nb_events
; i
++) {
2670 ret
= lttng_event_serialize(&events
[i
], 0, NULL
, NULL
, 0, NULL
,
2673 ret_code
= LTTNG_ERR_NOMEM
;
2678 if (nb_events
> UINT32_MAX
) {
2679 ERR("Syscall count would overflow the syscall listing command's reply");
2680 ret_code
= LTTNG_ERR_OVERFLOW
;
2684 /* Update command reply header. */
2685 reply_command_header
.count
= (uint32_t) nb_events
;
2686 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
2687 sizeof(reply_command_header
));
2689 ret_code
= LTTNG_OK
;
2696 * Command LTTNG_START_TRACE processed by the client thread.
2698 * Called with session mutex held.
2700 int cmd_start_trace(struct ltt_session
*session
)
2702 enum lttng_error_code ret
;
2703 unsigned long nb_chan
= 0;
2704 struct ltt_kernel_session
*ksession
;
2705 struct ltt_ust_session
*usess
;
2706 const bool session_rotated_after_last_stop
=
2707 session
->rotated_after_last_stop
;
2708 const bool session_cleared_after_last_stop
=
2709 session
->cleared_after_last_stop
;
2711 LTTNG_ASSERT(session
);
2713 /* Ease our life a bit ;) */
2714 ksession
= session
->kernel_session
;
2715 usess
= session
->ust_session
;
2717 /* Is the session already started? */
2718 if (session
->active
) {
2719 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2720 /* Perform nothing */
2724 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2725 !session
->current_trace_chunk
) {
2727 * A rotation was launched while the session was stopped and
2728 * it has not been completed yet. It is not possible to start
2729 * the session since starting the session here would require a
2730 * rotation from "NULL" to a new trace chunk. That rotation
2731 * would overlap with the ongoing rotation, which is not
2734 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2736 ret
= LTTNG_ERR_ROTATION_PENDING
;
2741 * Starting a session without channel is useless since after that it's not
2742 * possible to enable channel thus inform the client.
2744 if (usess
&& usess
->domain_global
.channels
) {
2745 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2748 nb_chan
+= ksession
->channel_count
;
2751 ret
= LTTNG_ERR_NO_CHANNEL
;
2755 session
->active
= true;
2756 session
->rotated_after_last_stop
= false;
2757 session
->cleared_after_last_stop
= false;
2758 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2759 if (!session
->has_been_started
) {
2760 struct lttng_trace_chunk
*trace_chunk
;
2762 DBG("Creating initial trace chunk of session \"%s\"",
2764 trace_chunk
= session_create_new_trace_chunk(
2765 session
, NULL
, NULL
, NULL
);
2767 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2770 LTTNG_ASSERT(!session
->current_trace_chunk
);
2771 ret
= (lttng_error_code
) session_set_trace_chunk(session
, trace_chunk
,
2773 lttng_trace_chunk_put(trace_chunk
);
2775 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2779 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2782 * Rotate existing streams into the new chunk.
2783 * This is a "quiet" rotation has no client has
2784 * explicitly requested this operation.
2786 * There is also no need to wait for the rotation
2787 * to complete as it will happen immediately. No data
2788 * was produced as the session was stopped, so the
2789 * rotation should happen on reception of the command.
2791 ret
= (lttng_error_code
) cmd_rotate_session(session
, NULL
, true,
2792 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2793 if (ret
!= LTTNG_OK
) {
2799 /* Kernel tracing */
2800 if (ksession
!= NULL
) {
2801 DBG("Start kernel tracing session %s", session
->name
);
2802 ret
= (lttng_error_code
) start_kernel_session(ksession
);
2803 if (ret
!= LTTNG_OK
) {
2808 /* Flag session that trace should start automatically */
2810 int int_ret
= ust_app_start_trace_all(usess
);
2813 ret
= LTTNG_ERR_UST_START_FAIL
;
2819 * Open a packet in every stream of the session to ensure that viewers
2820 * can correctly identify the boundaries of the periods during which
2821 * tracing was active for this session.
2823 ret
= session_open_packets(session
);
2824 if (ret
!= LTTNG_OK
) {
2829 * Clear the flag that indicates that a rotation was done while the
2830 * session was stopped.
2832 session
->rotated_after_last_stop
= false;
2834 if (session
->rotate_timer_period
&& !session
->rotation_schedule_timer_enabled
) {
2835 int int_ret
= timer_session_rotation_schedule_timer_start(
2836 session
, session
->rotate_timer_period
);
2839 ERR("Failed to enable rotate timer");
2840 ret
= LTTNG_ERR_UNK
;
2848 if (ret
== LTTNG_OK
) {
2849 /* Flag this after a successful start. */
2850 session
->has_been_started
= true;
2852 session
->active
= false;
2853 /* Restore initial state on error. */
2854 session
->rotated_after_last_stop
=
2855 session_rotated_after_last_stop
;
2856 session
->cleared_after_last_stop
=
2857 session_cleared_after_last_stop
;
2864 * Command LTTNG_STOP_TRACE processed by the client thread.
2866 int cmd_stop_trace(struct ltt_session
*session
)
2869 struct ltt_kernel_session
*ksession
;
2870 struct ltt_ust_session
*usess
;
2872 LTTNG_ASSERT(session
);
2874 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2876 ksession
= session
->kernel_session
;
2877 usess
= session
->ust_session
;
2879 /* Session is not active. Skip everything and inform the client. */
2880 if (!session
->active
) {
2881 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2885 ret
= stop_kernel_session(ksession
);
2886 if (ret
!= LTTNG_OK
) {
2890 if (usess
&& usess
->active
) {
2891 ret
= ust_app_stop_trace_all(usess
);
2893 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2898 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2900 /* Flag inactive after a successful stop. */
2901 session
->active
= 0;
2909 * Set the base_path of the session only if subdir of a control uris is set.
2910 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2912 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2914 struct lttng_uri
*uris
)
2919 for (i
= 0; i
< nb_uri
; i
++) {
2920 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2921 uris
[i
].subdir
[0] == '\0') {
2922 /* Not interested in these URIs */
2926 if (session
->base_path
!= NULL
) {
2927 free(session
->base_path
);
2928 session
->base_path
= NULL
;
2931 /* Set session base_path */
2932 session
->base_path
= strdup(uris
[i
].subdir
);
2933 if (!session
->base_path
) {
2934 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2935 uris
[i
].subdir
, session
->name
);
2936 ret
= LTTNG_ERR_NOMEM
;
2939 DBG2("Setting base path \"%s\" for session \"%s\"",
2940 session
->base_path
, session
->name
);
2948 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2950 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2951 struct lttng_uri
*uris
)
2954 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2955 struct ltt_ust_session
*usess
= session
->ust_session
;
2957 LTTNG_ASSERT(session
);
2959 LTTNG_ASSERT(nb_uri
> 0);
2961 /* Can't set consumer URI if the session is active. */
2962 if (session
->active
) {
2963 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2968 * Set the session base path if any. This is done inside
2969 * cmd_set_consumer_uri to preserve backward compatibility of the
2970 * previous session creation api vs the session descriptor api.
2972 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2973 if (ret
!= LTTNG_OK
) {
2977 /* Set the "global" consumer URIs */
2978 for (i
= 0; i
< nb_uri
; i
++) {
2979 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2981 if (ret
!= LTTNG_OK
) {
2986 /* Set UST session URIs */
2987 if (session
->ust_session
) {
2988 for (i
= 0; i
< nb_uri
; i
++) {
2989 ret
= add_uri_to_consumer(session
,
2990 session
->ust_session
->consumer
,
2991 &uris
[i
], LTTNG_DOMAIN_UST
);
2992 if (ret
!= LTTNG_OK
) {
2998 /* Set kernel session URIs */
2999 if (session
->kernel_session
) {
3000 for (i
= 0; i
< nb_uri
; i
++) {
3001 ret
= add_uri_to_consumer(session
,
3002 session
->kernel_session
->consumer
,
3003 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
3004 if (ret
!= LTTNG_OK
) {
3011 * Make sure to set the session in output mode after we set URI since a
3012 * session can be created without URL (thus flagged in no output mode).
3014 session
->output_traces
= 1;
3016 ksess
->output_traces
= 1;
3020 usess
->output_traces
= 1;
3031 enum lttng_error_code
set_session_output_from_descriptor(
3032 struct ltt_session
*session
,
3033 const struct lttng_session_descriptor
*descriptor
)
3036 enum lttng_error_code ret_code
= LTTNG_OK
;
3037 enum lttng_session_descriptor_type session_type
=
3038 lttng_session_descriptor_get_type(descriptor
);
3039 enum lttng_session_descriptor_output_type output_type
=
3040 lttng_session_descriptor_get_output_type(descriptor
);
3041 struct lttng_uri uris
[2] = {};
3042 size_t uri_count
= 0;
3044 switch (output_type
) {
3045 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3047 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3048 lttng_session_descriptor_get_local_output_uri(descriptor
,
3052 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3053 lttng_session_descriptor_get_network_output_uris(descriptor
,
3054 &uris
[0], &uris
[1]);
3058 ret_code
= LTTNG_ERR_INVALID
;
3062 switch (session_type
) {
3063 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3065 struct snapshot_output
*new_output
= NULL
;
3067 new_output
= snapshot_output_alloc();
3069 ret_code
= LTTNG_ERR_NOMEM
;
3073 ret
= snapshot_output_init_with_uri(session
,
3074 DEFAULT_SNAPSHOT_MAX_SIZE
,
3075 NULL
, uris
, uri_count
, session
->consumer
,
3076 new_output
, &session
->snapshot
);
3078 ret_code
= (ret
== -ENOMEM
) ?
3079 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3080 snapshot_output_destroy(new_output
);
3083 snapshot_add_output(&session
->snapshot
, new_output
);
3086 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3087 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3089 ret_code
= (lttng_error_code
) cmd_set_consumer_uri(session
, uri_count
, uris
);
3093 ret_code
= LTTNG_ERR_INVALID
;
3101 enum lttng_error_code
cmd_create_session_from_descriptor(
3102 struct lttng_session_descriptor
*descriptor
,
3103 const lttng_sock_cred
*creds
,
3104 const char *home_path
)
3107 enum lttng_error_code ret_code
;
3108 const char *session_name
;
3109 struct ltt_session
*new_session
= NULL
;
3110 enum lttng_session_descriptor_status descriptor_status
;
3111 const lttng_trace_format_descriptor
*trace_format_descriptor
= NULL
;
3112 lttng::trace_format_descriptor::uptr trace_format_descriptor_ptr
;
3114 session_lock_list();
3116 if (*home_path
!= '/') {
3117 ERR("Home path provided by client is not absolute");
3118 ret_code
= LTTNG_ERR_INVALID
;
3123 descriptor_status
= lttng_session_descriptor_get_session_name(
3124 descriptor
, &session_name
);
3125 switch (descriptor_status
) {
3126 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3128 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3129 session_name
= NULL
;
3132 ret_code
= LTTNG_ERR_INVALID
;
3136 descriptor_status
= lttng_session_descriptor_get_trace_format_descriptor(
3137 descriptor
, &trace_format_descriptor
);
3138 if (descriptor_status
!= LTTNG_SESSION_DESCRIPTOR_STATUS_OK
) {
3139 ret_code
= LTTNG_ERR_INVALID
;
3144 trace_format_descriptor_ptr
=
3145 reinterpret_cast<const lttng::trace_format_descriptor
*>(
3146 trace_format_descriptor
)
3148 } catch (std::exception
& e
) {
3149 ERR("%s", e
.what());
3150 ret_code
= LTTNG_ERR_UNK
;
3154 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
, trace_format_descriptor_ptr
,
3156 if (ret_code
!= LTTNG_OK
) {
3160 ret_code
= notification_thread_command_add_session(the_notification_thread_handle
,
3161 new_session
->id
, new_session
->name
, new_session
->uid
, new_session
->gid
);
3162 if (ret_code
!= LTTNG_OK
) {
3166 /* Announce the session's destruction to the notification thread when it is destroyed. */
3167 ret
= session_add_destroy_notifier(
3169 [](const struct ltt_session
*session
,
3170 void *user_data
__attribute__((unused
))) {
3171 (void) notification_thread_command_remove_session(
3172 the_notification_thread_handle
, session
->id
);
3176 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3178 ret
= LTTNG_ERR_NOMEM
;
3182 if (!session_name
) {
3183 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3186 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3191 if (!lttng_session_descriptor_is_output_destination_initialized(
3194 * Only include the session's creation time in the output
3195 * destination if the name of the session itself was
3196 * not auto-generated.
3198 ret_code
= lttng_session_descriptor_set_default_output(
3200 session_name
? &new_session
->creation_time
: NULL
,
3202 if (ret_code
!= LTTNG_OK
) {
3206 new_session
->has_user_specified_directory
=
3207 lttng_session_descriptor_has_output_directory(
3211 switch (lttng_session_descriptor_get_type(descriptor
)) {
3212 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3213 new_session
->snapshot_mode
= 1;
3215 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3216 new_session
->live_timer
=
3217 lttng_session_descriptor_live_get_timer_interval(
3224 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3225 if (ret_code
!= LTTNG_OK
) {
3228 new_session
->consumer
->enabled
= 1;
3229 ret_code
= LTTNG_OK
;
3231 /* Release reference provided by the session_create function. */
3232 session_put(new_session
);
3233 if (ret_code
!= LTTNG_OK
&& new_session
) {
3234 /* Release the global reference on error. */
3235 session_destroy(new_session
);
3237 session_unlock_list();
3241 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3242 struct lttng_session_descriptor
**return_descriptor
)
3245 size_t payload_size
;
3246 struct lttng_dynamic_buffer payload
;
3247 struct lttng_buffer_view home_dir_view
;
3248 struct lttng_buffer_view session_descriptor_view
;
3249 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3250 enum lttng_error_code ret_code
;
3252 lttng_dynamic_buffer_init(&payload
);
3253 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3255 ret_code
= LTTNG_ERR_INVALID
;
3258 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3259 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3260 ret_code
= LTTNG_ERR_INVALID
;
3264 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3265 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3266 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3268 ret_code
= LTTNG_ERR_NOMEM
;
3272 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3274 ERR("Reception of session descriptor failed, aborting.");
3275 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3279 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3282 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3283 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3284 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3285 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3286 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3290 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3292 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3293 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3294 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3295 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3296 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3300 ret
= lttng_session_descriptor_create_from_buffer(
3301 &session_descriptor_view
, &session_descriptor
);
3303 ERR("Failed to create session descriptor from payload of \"create session\" command");
3304 ret_code
= LTTNG_ERR_INVALID
;
3309 * Sets the descriptor's auto-generated properties (name, output) if
3312 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3314 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3315 if (ret_code
!= LTTNG_OK
) {
3319 ret_code
= LTTNG_OK
;
3320 *return_descriptor
= session_descriptor
;
3321 session_descriptor
= NULL
;
3323 lttng_dynamic_buffer_reset(&payload
);
3324 lttng_session_descriptor_destroy(session_descriptor
);
3329 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3330 void *_reply_context
)
3334 const struct cmd_destroy_session_reply_context
*reply_context
=
3335 (cmd_destroy_session_reply_context
*) _reply_context
;
3336 struct lttng_dynamic_buffer payload
;
3337 struct lttcomm_session_destroy_command_header cmd_header
;
3338 struct lttng_trace_archive_location
*location
= NULL
;
3339 struct lttcomm_lttng_msg llm
= {
3340 .cmd_type
= LTTNG_DESTROY_SESSION
,
3341 .ret_code
= reply_context
->destruction_status
,
3344 sizeof(struct lttcomm_session_destroy_command_header
),
3348 size_t payload_size_before_location
;
3350 lttng_dynamic_buffer_init(&payload
);
3352 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3354 ERR("Failed to append session destruction message");
3358 cmd_header
.rotation_state
=
3359 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3360 session
->rotation_state
:
3361 LTTNG_ROTATION_STATE_NO_ROTATION
);
3362 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3363 sizeof(cmd_header
));
3365 ERR("Failed to append session destruction command header");
3369 if (!reply_context
->implicit_rotation_on_destroy
) {
3370 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3374 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3375 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3380 location
= session_get_trace_archive_location(session
);
3382 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3387 payload_size_before_location
= payload
.size
;
3388 comm_ret
= lttng_trace_archive_location_serialize(location
,
3390 lttng_trace_archive_location_put(location
);
3392 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3396 /* Update the message to indicate the location's length. */
3397 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3398 payload
.size
- payload_size_before_location
;
3400 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3401 payload
.data
, payload
.size
);
3402 if (comm_ret
!= (ssize_t
) payload
.size
) {
3403 ERR("Failed to send result of the destruction of session \"%s\" to client",
3407 ret
= close(reply_context
->reply_sock_fd
);
3409 PERROR("Failed to close client socket in deferred session destroy reply");
3411 lttng_dynamic_buffer_reset(&payload
);
3412 free(_reply_context
);
3416 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3418 * Called with session lock held.
3420 int cmd_destroy_session(struct ltt_session
*session
,
3421 struct notification_thread_handle
*notification_thread_handle
,
3425 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3426 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3429 reply_context
= zmalloc
<cmd_destroy_session_reply_context
>();
3430 if (!reply_context
) {
3431 ret
= LTTNG_ERR_NOMEM
;
3435 reply_context
->reply_sock_fd
= *sock_fd
;
3439 LTTNG_ASSERT(session
);
3441 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3443 if (session
->active
) {
3444 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3446 ret
= cmd_stop_trace(session
);
3447 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3448 /* Carry on with the destruction of the session. */
3449 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3450 session
->name
, lttng_strerror(-ret
));
3451 destruction_last_error
= (lttng_error_code
) ret
;
3455 if (session
->rotation_schedule_timer_enabled
) {
3456 if (timer_session_rotation_schedule_timer_stop(
3458 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3460 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3464 if (session
->rotate_size
) {
3465 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3466 session
->rotate_size
= 0;
3469 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3471 * Perform a last rotation on destruction if rotations have
3472 * occurred during the session's lifetime.
3474 ret
= cmd_rotate_session(session
, NULL
, false,
3475 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3476 if (ret
!= LTTNG_OK
) {
3477 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3478 session
->name
, lttng_strerror(-ret
));
3479 destruction_last_error
= (lttng_error_code
) -ret
;
3481 if (reply_context
) {
3482 reply_context
->implicit_rotation_on_destroy
= true;
3484 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3486 * The user has not triggered a session rotation. However, to
3487 * ensure all data has been consumed, the session is rotated
3488 * to a 'null' trace chunk before it is destroyed.
3490 * This is a "quiet" rotation meaning that no notification is
3491 * emitted and no renaming of the current trace chunk takes
3494 ret
= cmd_rotate_session(session
, NULL
, true,
3495 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3497 * Rotation operations may not be supported by the kernel
3498 * tracer. Hence, do not consider this implicit rotation as
3499 * a session destruction error. The library has already stopped
3500 * the session and waited for pending data; there is nothing
3501 * left to do but complete the destruction of the session.
3503 if (ret
!= LTTNG_OK
&&
3504 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3505 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3506 session
->name
, lttng_strerror(ret
));
3507 destruction_last_error
= (lttng_error_code
) -ret
;
3511 if (session
->shm_path
[0]) {
3513 * When a session is created with an explicit shm_path,
3514 * the consumer daemon will create its shared memory files
3515 * at that location and will *not* unlink them. This is normal
3516 * as the intention of that feature is to make it possible
3517 * to retrieve the content of those files should a crash occur.
3519 * To ensure the content of those files can be used, the
3520 * sessiond daemon will replicate the content of the metadata
3521 * cache in a metadata file.
3523 * On clean-up, it is expected that the consumer daemon will
3524 * unlink the shared memory files and that the session daemon
3525 * will unlink the metadata file. Then, the session's directory
3526 * in the shm path can be removed.
3528 * Unfortunately, a flaw in the design of the sessiond's and
3529 * consumerd's tear down of channels makes it impossible to
3530 * determine when the sessiond _and_ the consumerd have both
3531 * destroyed their representation of a channel. For one, the
3532 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3533 * callbacks in both daemons.
3535 * However, it is also impossible for the sessiond to know when
3536 * the consumer daemon is done destroying its channel(s) since
3537 * it occurs as a reaction to the closing of the channel's file
3538 * descriptor. There is no resulting communication initiated
3539 * from the consumerd to the sessiond to confirm that the
3540 * operation is completed (and was successful).
3542 * Until this is all fixed, the session daemon checks for the
3543 * removal of the session's shm path which makes it possible
3544 * to safely advertise a session as having been destroyed.
3546 * Prior to this fix, it was not possible to reliably save
3547 * a session making use of the --shm-path option, destroy it,
3548 * and load it again. This is because the creation of the
3549 * session would fail upon seeing the session's shm path
3550 * already in existence.
3552 * Note that none of the error paths in the check for the
3553 * directory's existence return an error. This is normal
3554 * as there isn't much that can be done. The session will
3555 * be destroyed properly, except that we can't offer the
3556 * guarantee that the same session can be re-created.
3558 current_completion_handler
= &destroy_completion_handler
.handler
;
3559 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3561 sizeof(destroy_completion_handler
.shm_path
));
3566 * The session is destroyed. However, note that the command context
3567 * still holds a reference to the session, thus delaying its destruction
3568 * _at least_ up to the point when that reference is released.
3570 session_destroy(session
);
3571 if (reply_context
) {
3572 reply_context
->destruction_status
= destruction_last_error
;
3573 ret
= session_add_destroy_notifier(session
,
3574 cmd_destroy_session_reply
,
3575 (void *) reply_context
);
3577 ret
= LTTNG_ERR_FATAL
;
3589 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3591 int cmd_register_consumer(struct ltt_session
*session
,
3592 enum lttng_domain_type domain
, const char *sock_path
,
3593 struct consumer_data
*cdata
)
3596 struct consumer_socket
*socket
= NULL
;
3598 LTTNG_ASSERT(session
);
3599 LTTNG_ASSERT(cdata
);
3600 LTTNG_ASSERT(sock_path
);
3603 case LTTNG_DOMAIN_KERNEL
:
3605 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3607 LTTNG_ASSERT(ksess
);
3609 /* Can't register a consumer if there is already one */
3610 if (ksess
->consumer_fds_sent
!= 0) {
3611 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3615 sock
= lttcomm_connect_unix_sock(sock_path
);
3617 ret
= LTTNG_ERR_CONNECT_FAIL
;
3620 cdata
->cmd_sock
= sock
;
3622 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3623 if (socket
== NULL
) {
3626 PERROR("close register consumer");
3628 cdata
->cmd_sock
= -1;
3629 ret
= LTTNG_ERR_FATAL
;
3633 socket
->lock
= zmalloc
<pthread_mutex_t
>();
3634 if (socket
->lock
== NULL
) {
3635 PERROR("zmalloc pthread mutex");
3636 ret
= LTTNG_ERR_FATAL
;
3640 pthread_mutex_init(socket
->lock
, NULL
);
3641 socket
->registered
= 1;
3644 consumer_add_socket(socket
, ksess
->consumer
);
3647 pthread_mutex_lock(&cdata
->pid_mutex
);
3649 pthread_mutex_unlock(&cdata
->pid_mutex
);
3654 /* TODO: Userspace tracing */
3655 ret
= LTTNG_ERR_UND
;
3663 consumer_destroy_socket(socket
);
3669 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3671 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3672 struct lttng_domain
**domains
)
3677 struct lttng_ht_iter iter
;
3679 if (session
->kernel_session
!= NULL
) {
3680 DBG3("Listing domains found kernel domain");
3684 if (session
->ust_session
!= NULL
) {
3685 DBG3("Listing domains found UST global domain");
3689 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3691 if (agt
->being_used
) {
3702 *domains
= calloc
<lttng_domain
>(nb_dom
);
3703 if (*domains
== NULL
) {
3704 ret
= LTTNG_ERR_FATAL
;
3708 if (session
->kernel_session
!= NULL
) {
3709 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3711 /* Kernel session buffer type is always GLOBAL */
3712 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3717 if (session
->ust_session
!= NULL
) {
3718 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3719 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3723 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3725 if (agt
->being_used
) {
3726 (*domains
)[index
].type
= agt
->domain
;
3727 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3737 /* Return negative value to differentiate return code */
3743 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3745 enum lttng_error_code
cmd_list_channels(enum lttng_domain_type domain
,
3746 struct ltt_session
*session
,
3747 struct lttng_payload
*payload
)
3751 struct lttcomm_list_command_header cmd_header
= {};
3752 size_t cmd_header_offset
;
3753 enum lttng_error_code ret_code
;
3758 DBG("Listing channels for session %s", session
->name
);
3760 cmd_header_offset
= payload
->buffer
.size
;
3762 /* Reserve space for command reply header. */
3763 ret
= lttng_dynamic_buffer_set_size(&payload
->buffer
,
3764 cmd_header_offset
+ sizeof(cmd_header
));
3766 ret_code
= LTTNG_ERR_NOMEM
;
3771 case LTTNG_DOMAIN_KERNEL
:
3773 /* Kernel channels */
3774 struct ltt_kernel_channel
*kchan
;
3775 if (session
->kernel_session
!= NULL
) {
3776 cds_list_for_each_entry(kchan
,
3777 &session
->kernel_session
->channel_list
.head
, list
) {
3778 uint64_t discarded_events
, lost_packets
;
3779 struct lttng_channel_extended
*extended
;
3781 extended
= (struct lttng_channel_extended
*)
3782 kchan
->channel
->attr
.extended
.ptr
;
3784 ret
= get_kernel_runtime_stats(session
, kchan
,
3785 &discarded_events
, &lost_packets
);
3787 ret_code
= LTTNG_ERR_UNK
;
3792 * Update the discarded_events and lost_packets
3793 * count for the channel
3795 extended
->discarded_events
= discarded_events
;
3796 extended
->lost_packets
= lost_packets
;
3798 ret
= lttng_channel_serialize(
3799 kchan
->channel
, &payload
->buffer
);
3801 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3802 kchan
->channel
->name
);
3803 ret_code
= LTTNG_ERR_UNK
;
3812 case LTTNG_DOMAIN_UST
:
3814 struct lttng_ht_iter iter
;
3815 struct ltt_ust_channel
*uchan
;
3818 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
3819 &iter
.iter
, uchan
, node
.node
) {
3820 uint64_t discarded_events
= 0, lost_packets
= 0;
3821 struct lttng_channel
*channel
= NULL
;
3822 struct lttng_channel_extended
*extended
;
3824 channel
= trace_ust_channel_to_lttng_channel(uchan
);
3826 ret_code
= LTTNG_ERR_NOMEM
;
3830 extended
= (struct lttng_channel_extended
*)
3831 channel
->attr
.extended
.ptr
;
3833 ret
= get_ust_runtime_stats(session
, uchan
,
3834 &discarded_events
, &lost_packets
);
3836 lttng_channel_destroy(channel
);
3837 ret_code
= LTTNG_ERR_UNK
;
3841 extended
->discarded_events
= discarded_events
;
3842 extended
->lost_packets
= lost_packets
;
3844 ret
= lttng_channel_serialize(
3845 channel
, &payload
->buffer
);
3847 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3849 lttng_channel_destroy(channel
);
3850 ret_code
= LTTNG_ERR_UNK
;
3854 lttng_channel_destroy(channel
);
3864 if (i
> UINT32_MAX
) {
3865 ERR("Channel count would overflow the channel listing command's reply");
3866 ret_code
= LTTNG_ERR_OVERFLOW
;
3870 /* Update command reply header. */
3871 cmd_header
.count
= (uint32_t) i
;
3872 memcpy(payload
->buffer
.data
+ cmd_header_offset
, &cmd_header
,
3873 sizeof(cmd_header
));
3874 ret_code
= LTTNG_OK
;
3881 * Command LTTNG_LIST_EVENTS processed by the client thread.
3883 enum lttng_error_code
cmd_list_events(enum lttng_domain_type domain
,
3884 struct ltt_session
*session
,
3886 struct lttng_payload
*reply_payload
)
3888 int buffer_resize_ret
;
3889 enum lttng_error_code ret_code
= LTTNG_OK
;
3890 struct lttcomm_list_command_header reply_command_header
= {};
3891 size_t reply_command_header_offset
;
3892 unsigned int nb_events
= 0;
3894 assert(reply_payload
);
3896 /* Reserve space for command reply header. */
3897 reply_command_header_offset
= reply_payload
->buffer
.size
;
3898 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3899 reply_command_header_offset
+
3900 sizeof(struct lttcomm_list_command_header
));
3901 if (buffer_resize_ret
) {
3902 ret_code
= LTTNG_ERR_NOMEM
;
3907 case LTTNG_DOMAIN_KERNEL
:
3908 if (session
->kernel_session
!= NULL
) {
3909 ret_code
= list_lttng_kernel_events(channel_name
,
3910 session
->kernel_session
, reply_payload
, &nb_events
);
3914 case LTTNG_DOMAIN_UST
:
3916 if (session
->ust_session
!= NULL
) {
3917 ret_code
= list_lttng_ust_global_events(channel_name
,
3918 &session
->ust_session
->domain_global
,
3919 reply_payload
, &nb_events
);
3924 case LTTNG_DOMAIN_LOG4J
:
3925 case LTTNG_DOMAIN_JUL
:
3926 case LTTNG_DOMAIN_PYTHON
:
3927 if (session
->ust_session
) {
3928 struct lttng_ht_iter iter
;
3932 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3933 &iter
.iter
, agt
, node
.node
) {
3934 if (agt
->domain
== domain
) {
3935 ret_code
= list_lttng_agent_events(
3936 agt
, reply_payload
, &nb_events
);
3945 ret_code
= LTTNG_ERR_UND
;
3949 if (nb_events
> UINT32_MAX
) {
3950 ret_code
= LTTNG_ERR_OVERFLOW
;
3954 /* Update command reply header. */
3955 reply_command_header
.count
= (uint32_t) nb_events
;
3956 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
3957 sizeof(reply_command_header
));
3964 * Using the session list, filled a lttng_session array to send back to the
3965 * client for session listing.
3967 * The session list lock MUST be acquired before calling this function. Use
3968 * session_lock_list() and session_unlock_list().
3970 enum lttng_error_code
cmd_list_lttng_sessions(
3971 struct lttng_payload
*reply_payload
, uid_t uid
, gid_t gid
)
3973 int buffer_resize_ret
;
3974 enum lttng_error_code ret_code
= LTTNG_OK
;
3975 struct lttcomm_list_command_header reply_command_header
= {};
3976 size_t reply_command_header_offset
;
3977 struct ltt_session
*session
;
3978 struct ltt_session_list
*list
= session_get_list();
3982 assert(reply_payload
);
3984 /* Reserve space for command reply header. */
3985 reply_command_header_offset
= reply_payload
->buffer
.size
;
3986 buffer_resize_ret
= lttng_dynamic_buffer_set_size(&reply_payload
->buffer
,
3987 reply_command_header_offset
+ sizeof(struct lttcomm_list_command_header
));
3988 if (buffer_resize_ret
) {
3989 ret_code
= LTTNG_ERR_NOMEM
;
3993 DBG("Getting all available session for UID %d GID %d",
3996 cds_list_for_each_entry(session
, &list
->head
, list
) {
3997 struct lttng_session tmp_session
= {};
3998 struct lttng_session_extended tmp_extended
= {};
4000 tmp_session
.extended
.ptr
= &tmp_extended
;
4002 if (!session_get(session
)) {
4006 * Only list the sessions the user can control.
4008 if (!session_access_ok(session
, uid
) ||
4009 session
->destroyed
) {
4010 session_put(session
);
4014 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4015 struct ltt_ust_session
*usess
= session
->ust_session
;
4017 if (session
->consumer
->type
== CONSUMER_DST_NET
||
4018 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
4019 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
4020 ret
= build_network_session_path(
4021 tmp_session
.path
, sizeof(tmp_session
.path
), session
);
4023 ret
= snprintf(tmp_session
.path
, sizeof(tmp_session
.path
), "%s",
4024 session
->consumer
->dst
.session_root_path
);
4027 PERROR("snprintf session path");
4028 session_put(session
);
4032 strncpy(tmp_session
.name
, session
->name
, NAME_MAX
);
4033 tmp_session
.name
[NAME_MAX
- 1] = '\0';
4034 tmp_session
.enabled
= session
->active
;
4035 tmp_session
.snapshot_mode
= session
->snapshot_mode
;
4036 tmp_session
.live_timer_interval
= session
->live_timer
;
4037 LTTNG_OPTIONAL_SET(&tmp_extended
.creation_time
, (uint64_t) session
->creation_time
);
4038 ret
= lttng_session_serialize(&tmp_session
, reply_payload
);
4040 ret_code
= LTTNG_ERR_FATAL
;
4044 session_put(session
);
4047 if (i
> UINT32_MAX
) {
4048 ret_code
= LTTNG_ERR_OVERFLOW
;
4052 /* Update command reply header. */
4053 reply_command_header
.count
= (uint32_t) i
;
4054 memcpy(reply_payload
->buffer
.data
+ reply_command_header_offset
, &reply_command_header
,
4055 sizeof(reply_command_header
));
4056 ret_code
= LTTNG_OK
;
4062 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4063 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4065 int cmd_data_pending(struct ltt_session
*session
)
4068 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4069 struct ltt_ust_session
*usess
= session
->ust_session
;
4071 LTTNG_ASSERT(session
);
4073 DBG("Data pending for session %s", session
->name
);
4075 /* Session MUST be stopped to ask for data availability. */
4076 if (session
->active
) {
4077 ret
= LTTNG_ERR_SESSION_STARTED
;
4081 * If stopped, just make sure we've started before else the above call
4082 * will always send that there is data pending.
4084 * The consumer assumes that when the data pending command is received,
4085 * the trace has been started before or else no output data is written
4086 * by the streams which is a condition for data pending. So, this is
4087 * *VERY* important that we don't ask the consumer before a start
4090 if (!session
->has_been_started
) {
4096 /* A rotation is still pending, we have to wait. */
4097 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4098 DBG("Rotate still pending for session %s", session
->name
);
4103 if (ksess
&& ksess
->consumer
) {
4104 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
4106 /* Data is still being extracted for the kernel. */
4111 if (usess
&& usess
->consumer
) {
4112 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
4114 /* Data is still being extracted for the kernel. */
4119 /* Data is ready to be read by a viewer */
4127 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4129 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4131 int cmd_snapshot_add_output(struct ltt_session
*session
,
4132 const struct lttng_snapshot_output
*output
, uint32_t *id
)
4135 struct snapshot_output
*new_output
;
4137 LTTNG_ASSERT(session
);
4138 LTTNG_ASSERT(output
);
4140 DBG("Cmd snapshot add output for session %s", session
->name
);
4143 * Can't create an output if the session is not set in no-output mode.
4145 if (session
->output_traces
) {
4146 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4150 if (session
->has_non_mmap_channel
) {
4151 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
4155 /* Only one output is allowed until we have the "tee" feature. */
4156 if (session
->snapshot
.nb_output
== 1) {
4157 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
4161 new_output
= snapshot_output_alloc();
4163 ret
= LTTNG_ERR_NOMEM
;
4167 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
4168 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
4169 &session
->snapshot
);
4171 if (ret
== -ENOMEM
) {
4172 ret
= LTTNG_ERR_NOMEM
;
4174 ret
= LTTNG_ERR_INVALID
;
4180 snapshot_add_output(&session
->snapshot
, new_output
);
4182 *id
= new_output
->id
;
4189 snapshot_output_destroy(new_output
);
4195 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4197 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4199 int cmd_snapshot_del_output(struct ltt_session
*session
,
4200 const struct lttng_snapshot_output
*output
)
4203 struct snapshot_output
*sout
= NULL
;
4205 LTTNG_ASSERT(session
);
4206 LTTNG_ASSERT(output
);
4211 * Permission denied to create an output if the session is not
4212 * set in no output mode.
4214 if (session
->output_traces
) {
4215 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4220 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4222 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4223 } else if (*output
->name
!= '\0') {
4224 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4226 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4229 ret
= LTTNG_ERR_INVALID
;
4233 snapshot_delete_output(&session
->snapshot
, sout
);
4234 snapshot_output_destroy(sout
);
4243 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4245 * If no output is available, outputs is untouched and 0 is returned.
4247 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4249 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4250 struct lttng_snapshot_output
**outputs
)
4253 struct lttng_snapshot_output
*list
= NULL
;
4254 struct lttng_ht_iter iter
;
4255 struct snapshot_output
*output
;
4257 LTTNG_ASSERT(session
);
4258 LTTNG_ASSERT(outputs
);
4260 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4263 * Permission denied to create an output if the session is not
4264 * set in no output mode.
4266 if (session
->output_traces
) {
4267 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4271 if (session
->snapshot
.nb_output
== 0) {
4276 list
= calloc
<lttng_snapshot_output
>(session
->snapshot
.nb_output
);
4278 ret
= -LTTNG_ERR_NOMEM
;
4282 /* Copy list from session to the new list object. */
4284 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4285 output
, node
.node
) {
4286 LTTNG_ASSERT(output
->consumer
);
4287 list
[idx
].id
= output
->id
;
4288 list
[idx
].max_size
= output
->max_size
;
4289 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4290 sizeof(list
[idx
].name
))) {
4291 ret
= -LTTNG_ERR_INVALID
;
4294 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4295 if (lttng_strncpy(list
[idx
].ctrl_url
,
4296 output
->consumer
->dst
.session_root_path
,
4297 sizeof(list
[idx
].ctrl_url
))) {
4298 ret
= -LTTNG_ERR_INVALID
;
4303 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4304 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4306 ret
= -LTTNG_ERR_NOMEM
;
4311 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4312 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4314 ret
= -LTTNG_ERR_NOMEM
;
4323 ret
= session
->snapshot
.nb_output
;
4332 * Check if we can regenerate the metadata for this session.
4333 * Only kernel, UST per-uid and non-live sessions are supported.
4335 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4338 int check_regenerate_metadata_support(struct ltt_session
*session
)
4342 LTTNG_ASSERT(session
);
4344 if (session
->live_timer
!= 0) {
4345 ret
= LTTNG_ERR_LIVE_SESSION
;
4348 if (!session
->active
) {
4349 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4352 if (session
->ust_session
) {
4353 switch (session
->ust_session
->buffer_type
) {
4354 case LTTNG_BUFFER_PER_UID
:
4356 case LTTNG_BUFFER_PER_PID
:
4357 ret
= LTTNG_ERR_PER_PID_SESSION
;
4361 ret
= LTTNG_ERR_UNK
;
4365 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4366 session
->consumer
->relay_minor_version
< 8) {
4367 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4377 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4379 * Ask the consumer to truncate the existing metadata file(s) and
4380 * then regenerate the metadata. Live and per-pid sessions are not
4381 * supported and return an error.
4383 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4385 int cmd_regenerate_metadata(struct ltt_session
*session
)
4389 LTTNG_ASSERT(session
);
4391 ret
= check_regenerate_metadata_support(session
);
4396 if (session
->kernel_session
) {
4397 ret
= kernctl_session_regenerate_metadata(
4398 session
->kernel_session
->fd
);
4400 ERR("Failed to regenerate the kernel metadata");
4405 if (session
->ust_session
) {
4406 ret
= trace_ust_regenerate_metadata(session
->ust_session
);
4408 ERR("Failed to regenerate the UST metadata");
4412 DBG("Cmd metadata regenerate for session %s", session
->name
);
4420 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4422 * Ask the tracer to regenerate a new statedump.
4424 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4426 int cmd_regenerate_statedump(struct ltt_session
*session
)
4430 LTTNG_ASSERT(session
);
4432 if (!session
->active
) {
4433 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4437 if (session
->kernel_session
) {
4438 ret
= kernctl_session_regenerate_statedump(
4439 session
->kernel_session
->fd
);
4441 * Currently, the statedump in kernel can only fail if out
4445 if (ret
== -ENOMEM
) {
4446 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4448 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4450 ERR("Failed to regenerate the kernel statedump");
4455 if (session
->ust_session
) {
4456 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4458 * Currently, the statedump in UST always returns 0.
4461 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4462 ERR("Failed to regenerate the UST statedump");
4466 DBG("Cmd regenerate statedump for session %s", session
->name
);
4474 enum lttng_error_code
synchronize_tracer_notifier_register(
4475 struct notification_thread_handle
*notification_thread
,
4476 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4478 enum lttng_error_code ret_code
;
4479 const struct lttng_condition
*condition
=
4480 lttng_trigger_get_const_condition(trigger
);
4481 const char *trigger_name
;
4482 uid_t trigger_owner
;
4483 enum lttng_trigger_status trigger_status
;
4484 const enum lttng_domain_type trigger_domain
=
4485 lttng_trigger_get_underlying_domain_type_restriction(
4488 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4489 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4491 LTTNG_ASSERT(condition
);
4492 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4493 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4495 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4496 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4497 trigger_name
: "(anonymous)";
4499 session_lock_list();
4500 switch (trigger_domain
) {
4501 case LTTNG_DOMAIN_KERNEL
:
4503 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4504 if (ret_code
!= LTTNG_OK
) {
4505 enum lttng_error_code notif_thread_unregister_ret
;
4507 notif_thread_unregister_ret
=
4508 notification_thread_command_unregister_trigger(
4509 notification_thread
, trigger
);
4511 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4512 /* Return the original error code. */
4513 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4515 (int) trigger_owner
,
4521 case LTTNG_DOMAIN_UST
:
4522 ust_app_global_update_all_event_notifier_rules();
4524 case LTTNG_DOMAIN_JUL
:
4525 case LTTNG_DOMAIN_LOG4J
:
4526 case LTTNG_DOMAIN_PYTHON
:
4528 /* Agent domains. */
4529 struct agent
*agt
= agent_find_by_event_notifier_domain(
4533 agt
= agent_create(trigger_domain
);
4535 ret_code
= LTTNG_ERR_NOMEM
;
4536 goto end_unlock_session_list
;
4539 agent_add(agt
, the_trigger_agents_ht_by_domain
);
4542 ret_code
= (lttng_error_code
) trigger_agent_enable(trigger
, agt
);
4543 if (ret_code
!= LTTNG_OK
) {
4544 goto end_unlock_session_list
;
4549 case LTTNG_DOMAIN_NONE
:
4554 ret_code
= LTTNG_OK
;
4555 end_unlock_session_list
:
4556 session_unlock_list();
4560 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4561 struct lttng_trigger
*trigger
,
4562 bool is_trigger_anonymous
,
4563 struct notification_thread_handle
*notification_thread
,
4564 struct lttng_trigger
**return_trigger
)
4566 enum lttng_error_code ret_code
;
4567 const char *trigger_name
;
4568 uid_t trigger_owner
;
4569 enum lttng_trigger_status trigger_status
;
4571 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4572 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4573 trigger_name
: "(anonymous)";
4575 trigger_status
= lttng_trigger_get_owner_uid(
4576 trigger
, &trigger_owner
);
4577 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4579 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4580 trigger_name
, (int) trigger_owner
,
4581 (int) lttng_credentials_get_uid(cmd_creds
));
4584 * Validate the trigger credentials against the command credentials.
4585 * Only the root user can register a trigger with non-matching
4588 if (!lttng_credentials_is_equal_uid(
4589 lttng_trigger_get_credentials(trigger
),
4591 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4592 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4593 trigger_name
, (int) trigger_owner
,
4594 (int) lttng_credentials_get_uid(cmd_creds
));
4595 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4601 * The bytecode generation also serves as a validation step for the
4602 * bytecode expressions.
4604 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4605 if (ret_code
!= LTTNG_OK
) {
4606 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4607 trigger_name
, (int) trigger_owner
, ret_code
);
4612 * A reference to the trigger is acquired by the notification thread.
4613 * It is safe to return the same trigger to the caller since it the
4614 * other user holds a reference.
4616 * The trigger is modified during the execution of the
4617 * "register trigger" command. However, by the time the command returns,
4618 * it is safe to use without any locking as its properties are
4621 ret_code
= notification_thread_command_register_trigger(
4622 notification_thread
, trigger
, is_trigger_anonymous
);
4623 if (ret_code
!= LTTNG_OK
) {
4624 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4625 trigger_name
, (int) trigger_owner
, ret_code
);
4629 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4630 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4631 trigger_name
: "(anonymous)";
4634 * Synchronize tracers if the trigger adds an event notifier.
4636 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4637 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4638 trigger
, cmd_creds
);
4639 if (ret_code
!= LTTNG_OK
) {
4640 ERR("Error registering tracer notifier: %s",
4641 lttng_strerror(-ret_code
));
4647 * Return an updated trigger to the client.
4649 * Since a modified version of the same trigger is returned, acquire a
4650 * reference to the trigger so the caller doesn't have to care if those
4651 * are distinct instances or not.
4653 if (ret_code
== LTTNG_OK
) {
4654 lttng_trigger_get(trigger
);
4655 *return_trigger
= trigger
;
4656 /* Ownership of trigger was transferred to caller. */
4664 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4665 const struct lttng_trigger
*trigger
)
4667 enum lttng_error_code ret_code
;
4668 const struct lttng_condition
*condition
=
4669 lttng_trigger_get_const_condition(trigger
);
4670 const enum lttng_domain_type trigger_domain
=
4671 lttng_trigger_get_underlying_domain_type_restriction(
4674 LTTNG_ASSERT(condition
);
4675 LTTNG_ASSERT(lttng_condition_get_type(condition
) ==
4676 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
4678 session_lock_list();
4679 switch (trigger_domain
) {
4680 case LTTNG_DOMAIN_KERNEL
:
4681 ret_code
= kernel_unregister_event_notifier(trigger
);
4682 if (ret_code
!= LTTNG_OK
) {
4683 goto end_unlock_session_list
;
4687 case LTTNG_DOMAIN_UST
:
4688 ust_app_global_update_all_event_notifier_rules();
4690 case LTTNG_DOMAIN_JUL
:
4691 case LTTNG_DOMAIN_LOG4J
:
4692 case LTTNG_DOMAIN_PYTHON
:
4694 /* Agent domains. */
4695 struct agent
*agt
= agent_find_by_event_notifier_domain(
4699 * This trigger was never registered in the first place. Calling
4700 * this function under those circumstances is an internal error.
4703 ret_code
= (lttng_error_code
) trigger_agent_disable(trigger
, agt
);
4704 if (ret_code
!= LTTNG_OK
) {
4705 goto end_unlock_session_list
;
4710 case LTTNG_DOMAIN_NONE
:
4715 ret_code
= LTTNG_OK
;
4717 end_unlock_session_list
:
4718 session_unlock_list();
4722 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4723 const struct lttng_trigger
*trigger
,
4724 struct notification_thread_handle
*notification_thread
)
4726 enum lttng_error_code ret_code
;
4727 const char *trigger_name
;
4728 uid_t trigger_owner
;
4729 enum lttng_trigger_status trigger_status
;
4730 struct lttng_trigger
*sessiond_trigger
= NULL
;
4732 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4733 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(anonymous)";
4734 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4735 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4737 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4738 trigger_name
, (int) trigger_owner
,
4739 (int) lttng_credentials_get_uid(cmd_creds
));
4742 * Validate the trigger credentials against the command credentials.
4743 * Only the root user can unregister a trigger with non-matching
4746 if (!lttng_credentials_is_equal_uid(
4747 lttng_trigger_get_credentials(trigger
),
4749 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4750 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4751 trigger_name
, (int) trigger_owner
,
4752 (int) lttng_credentials_get_uid(cmd_creds
));
4753 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4758 /* Fetch the sessiond side trigger object. */
4759 ret_code
= notification_thread_command_get_trigger(
4760 notification_thread
, trigger
, &sessiond_trigger
);
4761 if (ret_code
!= LTTNG_OK
) {
4762 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4763 trigger_name
, (int) trigger_owner
, ret_code
);
4767 LTTNG_ASSERT(sessiond_trigger
);
4770 * From this point on, no matter what, consider the trigger
4773 * We set the unregistered state of the sessiond side trigger object in
4774 * the client thread since we want to minimize the possibility of the
4775 * notification thread being stalled due to a long execution of an
4776 * action that required the trigger lock.
4778 lttng_trigger_set_as_unregistered(sessiond_trigger
);
4780 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4782 if (ret_code
!= LTTNG_OK
) {
4783 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4784 trigger_name
, (int) trigger_owner
, ret_code
);
4789 * Synchronize tracers if the trigger removes an event notifier.
4790 * Do this even if the trigger unregistration failed to at least stop
4791 * the tracers from producing notifications associated with this
4794 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4795 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4796 if (ret_code
!= LTTNG_OK
) {
4797 ERR("Error unregistering trigger to tracer.");
4804 lttng_trigger_put(sessiond_trigger
);
4808 enum lttng_error_code
cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4809 struct notification_thread_handle
*notification_thread
,
4810 struct lttng_triggers
**return_triggers
)
4813 enum lttng_error_code ret_code
;
4814 struct lttng_triggers
*triggers
= NULL
;
4816 /* Get the set of triggers from the notification thread. */
4817 ret_code
= notification_thread_command_list_triggers(
4818 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4819 if (ret_code
!= LTTNG_OK
) {
4823 ret
= lttng_triggers_remove_hidden_triggers(triggers
);
4825 ret_code
= LTTNG_ERR_UNK
;
4829 *return_triggers
= triggers
;
4831 ret_code
= LTTNG_OK
;
4833 lttng_triggers_destroy(triggers
);
4837 enum lttng_error_code
cmd_execute_error_query(const struct lttng_credentials
*cmd_creds
,
4838 const struct lttng_error_query
*query
,
4839 struct lttng_error_query_results
**_results
,
4840 struct notification_thread_handle
*notification_thread
)
4842 enum lttng_error_code ret_code
;
4843 const struct lttng_trigger
*query_target_trigger
;
4844 const struct lttng_action
*query_target_action
= NULL
;
4845 struct lttng_trigger
*matching_trigger
= NULL
;
4846 const char *trigger_name
;
4847 uid_t trigger_owner
;
4848 enum lttng_trigger_status trigger_status
;
4849 struct lttng_error_query_results
*results
= NULL
;
4851 switch (lttng_error_query_get_target_type(query
)) {
4852 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4853 query_target_trigger
= lttng_error_query_trigger_borrow_target(query
);
4855 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4856 query_target_trigger
=
4857 lttng_error_query_condition_borrow_target(query
);
4859 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4860 query_target_trigger
= lttng_error_query_action_borrow_trigger_target(
4867 LTTNG_ASSERT(query_target_trigger
);
4869 ret_code
= notification_thread_command_get_trigger(notification_thread
,
4870 query_target_trigger
, &matching_trigger
);
4871 if (ret_code
!= LTTNG_OK
) {
4875 /* No longer needed. */
4876 query_target_trigger
= NULL
;
4878 if (lttng_error_query_get_target_type(query
) ==
4879 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
) {
4880 /* Get the sessiond-side version of the target action. */
4881 query_target_action
=
4882 lttng_error_query_action_borrow_action_target(
4883 query
, matching_trigger
);
4886 trigger_status
= lttng_trigger_get_name(matching_trigger
, &trigger_name
);
4887 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4888 trigger_name
: "(anonymous)";
4889 trigger_status
= lttng_trigger_get_owner_uid(matching_trigger
,
4891 LTTNG_ASSERT(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4893 results
= lttng_error_query_results_create();
4895 ret_code
= LTTNG_ERR_NOMEM
;
4899 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4900 trigger_name
, (int) trigger_owner
,
4901 (int) lttng_credentials_get_uid(cmd_creds
));
4904 * Validate the trigger credentials against the command credentials.
4905 * Only the root user can target a trigger with non-matching
4908 if (!lttng_credentials_is_equal_uid(
4909 lttng_trigger_get_credentials(matching_trigger
),
4911 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4912 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4913 trigger_name
, (int) trigger_owner
,
4914 (int) lttng_credentials_get_uid(cmd_creds
));
4915 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4920 switch (lttng_error_query_get_target_type(query
)) {
4921 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER
:
4922 trigger_status
= lttng_trigger_add_error_results(
4923 matching_trigger
, results
);
4925 switch (trigger_status
) {
4926 case LTTNG_TRIGGER_STATUS_OK
:
4929 ret_code
= LTTNG_ERR_UNK
;
4934 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION
:
4936 trigger_status
= lttng_trigger_condition_add_error_results(
4937 matching_trigger
, results
);
4939 switch (trigger_status
) {
4940 case LTTNG_TRIGGER_STATUS_OK
:
4943 ret_code
= LTTNG_ERR_UNK
;
4949 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION
:
4951 const enum lttng_action_status action_status
=
4952 lttng_action_add_error_query_results(
4953 query_target_action
, results
);
4955 switch (action_status
) {
4956 case LTTNG_ACTION_STATUS_OK
:
4959 ret_code
= LTTNG_ERR_UNK
;
4970 *_results
= results
;
4972 ret_code
= LTTNG_OK
;
4974 lttng_trigger_put(matching_trigger
);
4975 lttng_error_query_results_destroy(results
);
4980 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4981 * snapshot output is *not* set with a remote destination.
4983 * Return LTTNG_OK on success or a LTTNG_ERR code.
4985 static enum lttng_error_code
set_relayd_for_snapshot(
4986 struct consumer_output
*output
,
4987 const struct ltt_session
*session
)
4989 enum lttng_error_code status
= LTTNG_OK
;
4990 struct lttng_ht_iter iter
;
4991 struct consumer_socket
*socket
;
4992 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4993 const char *base_path
;
4995 LTTNG_ASSERT(output
);
4996 LTTNG_ASSERT(session
);
4998 DBG2("Set relayd object from snapshot output");
5000 if (session
->current_trace_chunk
) {
5001 enum lttng_trace_chunk_status chunk_status
=
5002 lttng_trace_chunk_get_id(
5003 session
->current_trace_chunk
,
5004 ¤t_chunk_id
.value
);
5006 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
5007 current_chunk_id
.is_set
= true;
5009 ERR("Failed to get current trace chunk id");
5010 status
= LTTNG_ERR_UNK
;
5015 /* Ignore if snapshot consumer output is not network. */
5016 if (output
->type
!= CONSUMER_DST_NET
) {
5021 * The snapshot record URI base path overrides the session
5024 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
5025 base_path
= output
->dst
.net
.control
.subdir
;
5027 base_path
= session
->base_path
;
5031 * For each consumer socket, create and send the relayd object of the
5035 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
5036 socket
, node
.node
) {
5037 pthread_mutex_lock(socket
->lock
);
5038 status
= send_consumer_relayd_sockets(session
->id
,
5040 session
->name
, session
->hostname
,
5042 session
->live_timer
,
5043 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
5044 session
->creation_time
,
5045 session
->name_contains_creation_time
);
5046 pthread_mutex_unlock(socket
->lock
);
5047 if (status
!= LTTNG_OK
) {
5059 * Record a kernel snapshot.
5061 * Return LTTNG_OK on success or a LTTNG_ERR code.
5063 static enum lttng_error_code
record_kernel_snapshot(
5064 struct ltt_kernel_session
*ksess
,
5065 const struct consumer_output
*output
,
5066 const struct ltt_session
*session
,
5067 uint64_t nb_packets_per_stream
)
5069 enum lttng_error_code status
;
5071 LTTNG_ASSERT(ksess
);
5072 LTTNG_ASSERT(output
);
5073 LTTNG_ASSERT(session
);
5075 status
= kernel_snapshot_record(
5076 ksess
, output
, nb_packets_per_stream
);
5081 * Record a UST snapshot.
5083 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5085 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
5086 const struct consumer_output
*output
,
5087 const struct ltt_session
*session
,
5088 uint64_t nb_packets_per_stream
)
5090 enum lttng_error_code status
;
5092 LTTNG_ASSERT(usess
);
5093 LTTNG_ASSERT(output
);
5094 LTTNG_ASSERT(session
);
5096 status
= ust_app_snapshot_record(
5097 usess
, output
, nb_packets_per_stream
);
5102 uint64_t get_session_size_one_more_packet_per_stream(
5103 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
5105 uint64_t tot_size
= 0;
5107 if (session
->kernel_session
) {
5108 struct ltt_kernel_channel
*chan
;
5109 const struct ltt_kernel_session
*ksess
=
5110 session
->kernel_session
;
5112 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
5113 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
5115 * Don't take channel into account if we
5116 * already grab all its packets.
5120 tot_size
+= chan
->channel
->attr
.subbuf_size
5121 * chan
->stream_count
;
5125 if (session
->ust_session
) {
5126 const struct ltt_ust_session
*usess
= session
->ust_session
;
5128 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
5136 * Calculate the number of packets we can grab from each stream that
5137 * fits within the overall snapshot max size.
5139 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5140 * the number of packets per stream.
5142 * TODO: this approach is not perfect: we consider the worse case
5143 * (packet filling the sub-buffers) as an upper bound, but we could do
5144 * better if we do this calculation while we actually grab the packet
5145 * content: we would know how much padding we don't actually store into
5148 * This algorithm is currently bounded by the number of packets per
5151 * Since we call this algorithm before actually grabbing the data, it's
5152 * an approximation: for instance, applications could appear/disappear
5153 * in between this call and actually grabbing data.
5156 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
5160 uint64_t cur_nb_packets
= 0;
5163 return 0; /* Infinite */
5166 size_left
= max_size
;
5168 uint64_t one_more_packet_tot_size
;
5170 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
5171 session
, cur_nb_packets
);
5172 if (!one_more_packet_tot_size
) {
5173 /* We are already grabbing all packets. */
5176 size_left
-= one_more_packet_tot_size
;
5177 if (size_left
< 0) {
5182 if (!cur_nb_packets
&& size_left
!= max_size
) {
5183 /* Not enough room to grab one packet of each stream, error. */
5186 return cur_nb_packets
;
5190 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
5191 const struct snapshot_output
*snapshot_output
)
5193 int64_t nb_packets_per_stream
;
5194 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
5196 enum lttng_error_code ret_code
= LTTNG_OK
;
5197 struct lttng_trace_chunk
*snapshot_trace_chunk
;
5198 struct consumer_output
*original_ust_consumer_output
= NULL
;
5199 struct consumer_output
*original_kernel_consumer_output
= NULL
;
5200 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
5201 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
5203 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
5205 snapshot_output
->name
,
5206 snapshot_output
->datetime
,
5207 snapshot_output
->nb_snapshot
);
5208 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
5209 ERR("Failed to format snapshot name");
5210 ret_code
= LTTNG_ERR_INVALID
;
5213 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5214 snapshot_output
->name
, session
->name
,
5215 snapshot_chunk_name
);
5216 if (!session
->kernel_session
&& !session
->ust_session
) {
5217 ERR("Failed to record snapshot as no channels exist");
5218 ret_code
= LTTNG_ERR_NO_CHANNEL
;
5222 if (session
->kernel_session
) {
5223 original_kernel_consumer_output
=
5224 session
->kernel_session
->consumer
;
5225 snapshot_kernel_consumer_output
=
5226 consumer_copy_output(snapshot_output
->consumer
);
5227 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
5228 snapshot_chunk_name
);
5230 /* Copy the original domain subdir. */
5231 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
5232 original_kernel_consumer_output
->domain_subdir
);
5234 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
5235 original_kernel_consumer_output
);
5237 ERR("Failed to copy consumer sockets from snapshot output configuration");
5238 ret_code
= LTTNG_ERR_NOMEM
;
5241 ret_code
= set_relayd_for_snapshot(
5242 snapshot_kernel_consumer_output
, session
);
5243 if (ret_code
!= LTTNG_OK
) {
5244 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5247 session
->kernel_session
->consumer
=
5248 snapshot_kernel_consumer_output
;
5250 if (session
->ust_session
) {
5251 original_ust_consumer_output
= session
->ust_session
->consumer
;
5252 snapshot_ust_consumer_output
=
5253 consumer_copy_output(snapshot_output
->consumer
);
5254 strcpy(snapshot_ust_consumer_output
->chunk_path
,
5255 snapshot_chunk_name
);
5257 /* Copy the original domain subdir. */
5258 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
5259 original_ust_consumer_output
->domain_subdir
);
5261 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
5262 original_ust_consumer_output
);
5264 ERR("Failed to copy consumer sockets from snapshot output configuration");
5265 ret_code
= LTTNG_ERR_NOMEM
;
5268 ret_code
= set_relayd_for_snapshot(
5269 snapshot_ust_consumer_output
, session
);
5270 if (ret_code
!= LTTNG_OK
) {
5271 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5274 session
->ust_session
->consumer
=
5275 snapshot_ust_consumer_output
;
5278 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
5279 snapshot_kernel_consumer_output
?:
5280 snapshot_ust_consumer_output
,
5281 consumer_output_get_base_path(
5282 snapshot_output
->consumer
),
5283 snapshot_chunk_name
);
5284 if (!snapshot_trace_chunk
) {
5285 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5287 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
5290 LTTNG_ASSERT(!session
->current_trace_chunk
);
5291 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
5292 lttng_trace_chunk_put(snapshot_trace_chunk
);
5293 snapshot_trace_chunk
= NULL
;
5295 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5297 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5301 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
5302 snapshot_output
->max_size
);
5303 if (nb_packets_per_stream
< 0) {
5304 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
5305 goto error_close_trace_chunk
;
5308 if (session
->kernel_session
) {
5309 ret_code
= record_kernel_snapshot(session
->kernel_session
,
5310 snapshot_kernel_consumer_output
, session
,
5311 nb_packets_per_stream
);
5312 if (ret_code
!= LTTNG_OK
) {
5313 goto error_close_trace_chunk
;
5317 if (session
->ust_session
) {
5318 ret_code
= record_ust_snapshot(session
->ust_session
,
5319 snapshot_ust_consumer_output
, session
,
5320 nb_packets_per_stream
);
5321 if (ret_code
!= LTTNG_OK
) {
5322 goto error_close_trace_chunk
;
5326 error_close_trace_chunk
:
5327 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
5328 ERR("Failed to release the current trace chunk of session \"%s\"",
5330 ret_code
= LTTNG_ERR_UNK
;
5333 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
5334 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
5336 * Don't goto end; make sure the chunk is closed for the session
5337 * to allow future snapshots.
5339 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5341 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5344 lttng_trace_chunk_put(snapshot_trace_chunk
);
5345 snapshot_trace_chunk
= NULL
;
5347 if (original_ust_consumer_output
) {
5348 session
->ust_session
->consumer
= original_ust_consumer_output
;
5350 if (original_kernel_consumer_output
) {
5351 session
->kernel_session
->consumer
=
5352 original_kernel_consumer_output
;
5354 consumer_output_put(snapshot_ust_consumer_output
);
5355 consumer_output_put(snapshot_kernel_consumer_output
);
5360 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5362 * The wait parameter is ignored so this call always wait for the snapshot to
5363 * complete before returning.
5365 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5367 int cmd_snapshot_record(struct ltt_session
*session
,
5368 const struct lttng_snapshot_output
*output
,
5369 int wait
__attribute__((unused
)))
5371 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5373 unsigned int snapshot_success
= 0;
5375 struct snapshot_output
*tmp_output
= NULL
;
5377 LTTNG_ASSERT(session
);
5378 LTTNG_ASSERT(output
);
5380 DBG("Cmd snapshot record for session %s", session
->name
);
5382 /* Get the datetime for the snapshot output directory. */
5383 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5386 cmd_ret
= LTTNG_ERR_INVALID
;
5391 * Permission denied to create an output if the session is not
5392 * set in no output mode.
5394 if (session
->output_traces
) {
5395 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5399 /* The session needs to be started at least once. */
5400 if (!session
->has_been_started
) {
5401 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5405 /* Use temporary output for the session. */
5406 if (*output
->ctrl_url
!= '\0') {
5407 tmp_output
= snapshot_output_alloc();
5409 cmd_ret
= LTTNG_ERR_NOMEM
;
5413 ret
= snapshot_output_init(session
, output
->max_size
,
5415 output
->ctrl_url
, output
->data_url
,
5419 if (ret
== -ENOMEM
) {
5420 cmd_ret
= LTTNG_ERR_NOMEM
;
5422 cmd_ret
= LTTNG_ERR_INVALID
;
5426 /* Use the global session count for the temporary snapshot. */
5427 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5429 /* Use the global datetime */
5430 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5431 cmd_ret
= snapshot_record(session
, tmp_output
);
5432 if (cmd_ret
!= LTTNG_OK
) {
5435 snapshot_success
= 1;
5437 struct snapshot_output
*sout
;
5438 struct lttng_ht_iter iter
;
5441 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5442 &iter
.iter
, sout
, node
.node
) {
5443 struct snapshot_output output_copy
;
5446 * Make a local copy of the output and override output
5447 * parameters with those provided as part of the
5450 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5452 if (output
->max_size
!= (uint64_t) -1ULL) {
5453 output_copy
.max_size
= output
->max_size
;
5456 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5457 memcpy(output_copy
.datetime
, datetime
,
5460 /* Use temporary name. */
5461 if (*output
->name
!= '\0') {
5462 if (lttng_strncpy(output_copy
.name
,
5464 sizeof(output_copy
.name
))) {
5465 cmd_ret
= LTTNG_ERR_INVALID
;
5471 cmd_ret
= snapshot_record(session
, &output_copy
);
5472 if (cmd_ret
!= LTTNG_OK
) {
5476 snapshot_success
= 1;
5481 if (snapshot_success
) {
5482 session
->snapshot
.nb_snapshot
++;
5484 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5489 snapshot_output_destroy(tmp_output
);
5495 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5497 int cmd_set_session_shm_path(struct ltt_session
*session
,
5498 const char *shm_path
)
5501 LTTNG_ASSERT(session
);
5504 * Can only set shm path before session is started.
5506 if (session
->has_been_started
) {
5507 return LTTNG_ERR_SESSION_STARTED
;
5510 strncpy(session
->shm_path
, shm_path
,
5511 sizeof(session
->shm_path
));
5512 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5518 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5520 * Ask the consumer to rotate the session output directory.
5521 * The session lock must be held.
5523 * Returns LTTNG_OK on success or else a negative LTTng error code.
5525 int cmd_rotate_session(struct ltt_session
*session
,
5526 struct lttng_rotate_session_return
*rotate_return
,
5527 bool quiet_rotation
,
5528 enum lttng_trace_chunk_command_type command
)
5531 uint64_t ongoing_rotation_chunk_id
;
5532 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5533 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5534 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5535 enum lttng_trace_chunk_status chunk_status
;
5536 bool failed_to_rotate
= false;
5537 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5539 LTTNG_ASSERT(session
);
5541 if (!session
->has_been_started
) {
5542 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5547 * Explicit rotation is not supported for live sessions.
5548 * However, live sessions can perform a quiet rotation on
5550 * Rotation is not supported for snapshot traces (no output).
5552 if ((!quiet_rotation
&& session
->live_timer
) ||
5553 !session
->output_traces
) {
5554 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5558 /* Unsupported feature in lttng-relayd before 2.11. */
5559 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5560 (session
->consumer
->relay_major_version
== 2 &&
5561 session
->consumer
->relay_minor_version
< 11)) {
5562 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5566 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5567 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5568 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5572 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5573 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5575 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5580 * After a stop, we only allow one rotation to occur, the other ones are
5581 * useless until a new start.
5583 if (session
->rotated_after_last_stop
) {
5584 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5586 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5591 * After a stop followed by a clear, disallow following rotations a they would
5592 * generate empty chunks.
5594 if (session
->cleared_after_last_stop
) {
5595 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5597 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5601 if (session
->active
) {
5602 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5604 if (!new_trace_chunk
) {
5605 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5611 * The current trace chunk becomes the chunk being archived.
5613 * After this point, "chunk_being_archived" must absolutely
5614 * be closed on the consumer(s), otherwise it will never be
5615 * cleaned-up, which will result in a leak.
5617 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5618 &chunk_being_archived
);
5620 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5624 if (session
->kernel_session
) {
5625 cmd_ret
= kernel_rotate_session(session
);
5626 if (cmd_ret
!= LTTNG_OK
) {
5627 failed_to_rotate
= true;
5628 rotation_fail_code
= cmd_ret
;
5631 if (session
->ust_session
) {
5632 cmd_ret
= ust_app_rotate_session(session
);
5633 if (cmd_ret
!= LTTNG_OK
) {
5634 failed_to_rotate
= true;
5635 rotation_fail_code
= cmd_ret
;
5639 if (!session
->active
) {
5640 session
->rotated_after_last_stop
= true;
5643 if (!chunk_being_archived
) {
5644 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5646 if (failed_to_rotate
) {
5647 cmd_ret
= rotation_fail_code
;
5654 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5655 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5656 &ongoing_rotation_chunk_id
);
5657 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5659 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5660 command
, session
->last_chunk_path
);
5662 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5666 if (failed_to_rotate
) {
5667 cmd_ret
= rotation_fail_code
;
5671 session
->quiet_rotation
= quiet_rotation
;
5672 ret
= timer_session_rotation_pending_check_start(session
,
5673 DEFAULT_ROTATE_PENDING_TIMER
);
5675 cmd_ret
= LTTNG_ERR_UNK
;
5679 if (rotate_return
) {
5680 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5683 session
->chunk_being_archived
= chunk_being_archived
;
5684 chunk_being_archived
= NULL
;
5685 if (!quiet_rotation
) {
5686 ret
= notification_thread_command_session_rotation_ongoing(
5687 the_notification_thread_handle
, session
->id
,
5688 ongoing_rotation_chunk_id
);
5689 if (ret
!= LTTNG_OK
) {
5690 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5692 cmd_ret
= (lttng_error_code
) ret
;
5696 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5697 session
->name
, ongoing_rotation_chunk_id
);
5699 lttng_trace_chunk_put(new_trace_chunk
);
5700 lttng_trace_chunk_put(chunk_being_archived
);
5701 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5704 if (session_reset_rotation_state(session
,
5705 LTTNG_ROTATION_STATE_ERROR
)) {
5706 ERR("Failed to reset rotation state of session \"%s\"",
5713 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5715 * Check if the session has finished its rotation.
5717 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5719 int cmd_rotate_get_info(struct ltt_session
*session
,
5720 struct lttng_rotation_get_info_return
*info_return
,
5721 uint64_t rotation_id
)
5723 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5724 enum lttng_rotation_state rotation_state
;
5726 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5727 session
->most_recent_chunk_id
.value
);
5729 if (session
->chunk_being_archived
) {
5730 enum lttng_trace_chunk_status chunk_status
;
5733 chunk_status
= lttng_trace_chunk_get_id(
5734 session
->chunk_being_archived
,
5736 LTTNG_ASSERT(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5738 rotation_state
= rotation_id
== chunk_id
?
5739 LTTNG_ROTATION_STATE_ONGOING
:
5740 LTTNG_ROTATION_STATE_EXPIRED
;
5742 if (session
->last_archived_chunk_id
.is_set
&&
5743 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5744 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5746 rotation_state
= session
->rotation_state
;
5750 switch (rotation_state
) {
5751 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5752 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5755 case LTTNG_ROTATION_STATE_EXPIRED
:
5756 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5757 rotation_id
, session
->name
);
5759 case LTTNG_ROTATION_STATE_ONGOING
:
5760 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5761 rotation_id
, session
->name
);
5763 case LTTNG_ROTATION_STATE_COMPLETED
:
5767 char *current_tracing_path_reply
;
5768 size_t current_tracing_path_reply_len
;
5770 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5771 rotation_id
, session
->name
);
5773 switch (session_get_consumer_destination_type(session
)) {
5774 case CONSUMER_DST_LOCAL
:
5775 current_tracing_path_reply
=
5776 info_return
->location
.local
.absolute_path
;
5777 current_tracing_path_reply_len
=
5778 sizeof(info_return
->location
.local
.absolute_path
);
5779 info_return
->location_type
=
5780 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5781 fmt_ret
= asprintf(&chunk_path
,
5782 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5783 session_get_base_path(session
),
5784 session
->last_archived_chunk_name
);
5785 if (fmt_ret
== -1) {
5786 PERROR("Failed to format the path of the last archived trace chunk");
5787 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5788 cmd_ret
= LTTNG_ERR_UNK
;
5792 case CONSUMER_DST_NET
:
5794 uint16_t ctrl_port
, data_port
;
5796 current_tracing_path_reply
=
5797 info_return
->location
.relay
.relative_path
;
5798 current_tracing_path_reply_len
=
5799 sizeof(info_return
->location
.relay
.relative_path
);
5800 /* Currently the only supported relay protocol. */
5801 info_return
->location
.relay
.protocol
=
5802 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5804 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5805 session_get_net_consumer_hostname(session
),
5806 sizeof(info_return
->location
.relay
.host
));
5808 ERR("Failed to copy host name to rotate_get_info reply");
5809 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5810 cmd_ret
= LTTNG_ERR_SET_URL
;
5814 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5815 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5816 info_return
->location
.relay
.ports
.data
= data_port
;
5817 info_return
->location_type
=
5818 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5819 chunk_path
= strdup(session
->last_chunk_path
);
5821 ERR("Failed to allocate the path of the last archived trace chunk");
5822 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5823 cmd_ret
= LTTNG_ERR_UNK
;
5832 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5833 chunk_path
, current_tracing_path_reply_len
);
5836 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5837 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5838 cmd_ret
= LTTNG_ERR_UNK
;
5844 case LTTNG_ROTATION_STATE_ERROR
:
5845 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5846 rotation_id
, session
->name
);
5854 info_return
->status
= (int32_t) rotation_state
;
5859 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5861 * Configure the automatic rotation parameters.
5862 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5863 * 'activate' to false means deactivate the rotation schedule and validate that
5864 * 'new_value' has the same value as the currently active value.
5866 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5868 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5869 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5871 struct notification_thread_handle
*notification_thread_handle
)
5874 uint64_t *parameter_value
;
5876 LTTNG_ASSERT(session
);
5878 DBG("Cmd rotate set schedule session %s", session
->name
);
5880 if (session
->live_timer
|| !session
->output_traces
) {
5881 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5882 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5886 switch (schedule_type
) {
5887 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5888 parameter_value
= &session
->rotate_size
;
5890 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5891 parameter_value
= &session
->rotate_timer_period
;
5892 if (new_value
>= UINT_MAX
) {
5893 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5894 new_value
, UINT_MAX
);
5895 ret
= LTTNG_ERR_INVALID
;
5900 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5901 ret
= LTTNG_ERR_INVALID
;
5905 /* Improper use of the API. */
5906 if (new_value
== -1ULL) {
5907 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5908 ret
= LTTNG_ERR_INVALID
;
5913 * As indicated in struct ltt_session's comments, a value of == 0 means
5914 * this schedule rotation type is not in use.
5916 * Reject the command if we were asked to activate a schedule that was
5919 if (activate
&& *parameter_value
!= 0) {
5920 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5921 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5926 * Reject the command if we were asked to deactivate a schedule that was
5929 if (!activate
&& *parameter_value
== 0) {
5930 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5931 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5936 * Reject the command if we were asked to deactivate a schedule that
5939 if (!activate
&& *parameter_value
!= new_value
) {
5940 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5941 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5945 *parameter_value
= activate
? new_value
: 0;
5947 switch (schedule_type
) {
5948 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5949 if (activate
&& session
->active
) {
5951 * Only start the timer if the session is active,
5952 * otherwise it will be started when the session starts.
5954 ret
= timer_session_rotation_schedule_timer_start(
5955 session
, new_value
);
5957 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5958 ret
= LTTNG_ERR_UNK
;
5962 ret
= timer_session_rotation_schedule_timer_stop(
5965 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5966 ret
= LTTNG_ERR_UNK
;
5971 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5973 ret
= subscribe_session_consumed_size_rotation(session
,
5974 new_value
, notification_thread_handle
);
5976 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5977 ret
= LTTNG_ERR_UNK
;
5981 ret
= unsubscribe_session_consumed_size_rotation(session
,
5982 notification_thread_handle
);
5984 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5985 ret
= LTTNG_ERR_UNK
;
5992 /* Would have been caught before. */
6004 /* Wait for a given path to be removed before continuing. */
6005 static enum lttng_error_code
wait_on_path(void *path_data
)
6007 const char *shm_path
= (const char *) path_data
;
6009 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6015 ret
= stat(shm_path
, &st
);
6017 if (errno
!= ENOENT
) {
6018 PERROR("stat() returned an error while checking for the existence of the shm path");
6020 DBG("shm path no longer exists, completing the destruction of session");
6024 if (!S_ISDIR(st
.st_mode
)) {
6025 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6030 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
6036 * Returns a pointer to a handler to run on completion of a command.
6037 * Returns NULL if no handler has to be run for the last command executed.
6039 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
6041 struct cmd_completion_handler
*handler
= current_completion_handler
;
6043 current_completion_handler
= NULL
;
6048 * Init command subsystem.
6053 * Set network sequence index to 1 for streams to match a relayd
6054 * socket on the consumer side.
6056 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
6057 relayd_net_seq_idx
= 1;
6058 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
6060 DBG("Command subsystem initialized");