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
12 #include <urcu/list.h>
13 #include <urcu/uatomic.h>
17 #include <common/defaults.h>
18 #include <common/common.h>
19 #include <common/sessiond-comm/sessiond-comm.h>
20 #include <common/relayd/relayd.h>
21 #include <common/utils.h>
22 #include <common/compat/string.h>
23 #include <common/kernel-ctl/kernel-ctl.h>
24 #include <common/dynamic-buffer.h>
25 #include <common/buffer-view.h>
26 #include <common/payload.h>
27 #include <common/payload-view.h>
28 #include <common/trace-chunk.h>
29 #include <lttng/location-internal.h>
30 #include <lttng/trigger/trigger-internal.h>
31 #include <lttng/condition/condition.h>
32 #include <lttng/action/action.h>
33 #include <lttng/channel.h>
34 #include <lttng/channel-internal.h>
35 #include <lttng/rotate-internal.h>
36 #include <lttng/location-internal.h>
37 #include <lttng/session-internal.h>
38 #include <lttng/userspace-probe-internal.h>
39 #include <lttng/session-descriptor-internal.h>
40 #include <lttng/lttng-error.h>
41 #include <lttng/tracker.h>
42 #include <common/string-utils/string-utils.h>
47 #include "health-sessiond.h"
49 #include "kernel-consumer.h"
50 #include "lttng-sessiond.h"
52 #include "lttng-syscall.h"
54 #include "buffer-registry.h"
55 #include "notification-thread.h"
56 #include "notification-thread-commands.h"
58 #include "rotation-thread.h"
60 #include "agent-thread.h"
65 /* Sleep for 100ms between each check for the shm path's deletion. */
66 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
68 struct cmd_destroy_session_reply_context
{
70 bool implicit_rotation_on_destroy
;
72 * Indicates whether or not an error occurred while launching the
73 * destruction of a session.
75 enum lttng_error_code destruction_status
;
78 static enum lttng_error_code
wait_on_path(void *path
);
81 * Command completion handler that is used by the destroy command
82 * when a session that has a non-default shm_path is being destroyed.
84 * See comment in cmd_destroy_session() for the rationale.
86 static struct destroy_completion_handler
{
87 struct cmd_completion_handler handler
;
88 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
89 } destroy_completion_handler
= {
92 .data
= destroy_completion_handler
.shm_path
97 static struct cmd_completion_handler
*current_completion_handler
;
100 * Used to keep a unique index for each relayd socket created where this value
101 * is associated with streams on the consumer so it can match the right relayd
102 * to send to. It must be accessed with the relayd_net_seq_idx_lock
105 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
106 static uint64_t relayd_net_seq_idx
;
108 static int validate_ust_event_name(const char *);
109 static int cmd_enable_event_internal(struct ltt_session
*session
,
110 const struct lttng_domain
*domain
,
111 char *channel_name
, struct lttng_event
*event
,
112 char *filter_expression
,
113 struct lttng_filter_bytecode
*filter
,
114 struct lttng_event_exclusion
*exclusion
,
118 * Create a session path used by list_lttng_sessions for the case that the
119 * session consumer is on the network.
121 static int build_network_session_path(char *dst
, size_t size
,
122 struct ltt_session
*session
)
124 int ret
, kdata_port
, udata_port
;
125 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
126 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
131 memset(tmp_urls
, 0, sizeof(tmp_urls
));
132 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
134 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
136 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
137 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
138 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
141 if (session
->ust_session
&& session
->ust_session
->consumer
) {
142 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
143 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
146 if (uuri
== NULL
&& kuri
== NULL
) {
147 uri
= &session
->consumer
->dst
.net
.control
;
148 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
149 } else if (kuri
&& uuri
) {
150 ret
= uri_compare(kuri
, uuri
);
154 /* Build uuri URL string */
155 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
162 } else if (kuri
&& uuri
== NULL
) {
164 } else if (uuri
&& kuri
== NULL
) {
168 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
174 * Do we have a UST url set. If yes, this means we have both kernel and UST
177 if (*tmp_uurl
!= '\0') {
178 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
179 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
182 if (kuri
|| (!kuri
&& !uuri
)) {
185 /* No kernel URI, use the UST port. */
188 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
196 * Get run-time attributes if the session has been started (discarded events,
199 static int get_kernel_runtime_stats(struct ltt_session
*session
,
200 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
201 uint64_t *lost_packets
)
205 if (!session
->has_been_started
) {
207 *discarded_events
= 0;
212 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
213 session
->kernel_session
->consumer
,
219 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
220 session
->kernel_session
->consumer
,
231 * Get run-time attributes if the session has been started (discarded events,
234 static int get_ust_runtime_stats(struct ltt_session
*session
,
235 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
236 uint64_t *lost_packets
)
239 struct ltt_ust_session
*usess
;
241 if (!discarded_events
|| !lost_packets
) {
246 usess
= session
->ust_session
;
247 assert(discarded_events
);
248 assert(lost_packets
);
250 if (!usess
|| !session
->has_been_started
) {
251 *discarded_events
= 0;
257 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
258 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
259 &usess
->buffer_reg_uid_list
,
260 usess
->consumer
, uchan
->id
,
261 uchan
->attr
.overwrite
,
264 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
265 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
266 uchan
, usess
->consumer
,
267 uchan
->attr
.overwrite
,
273 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
274 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
276 ERR("Unsupported buffer type");
287 * Fill lttng_channel array of all channels.
289 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
290 struct ltt_session
*session
, struct lttng_channel
*channels
,
291 struct lttng_channel_extended
*chan_exts
)
294 struct ltt_kernel_channel
*kchan
;
296 DBG("Listing channels for session %s", session
->name
);
299 case LTTNG_DOMAIN_KERNEL
:
300 /* Kernel channels */
301 if (session
->kernel_session
!= NULL
) {
302 cds_list_for_each_entry(kchan
,
303 &session
->kernel_session
->channel_list
.head
, list
) {
304 uint64_t discarded_events
, lost_packets
;
305 struct lttng_channel_extended
*extended
;
307 extended
= (struct lttng_channel_extended
*)
308 kchan
->channel
->attr
.extended
.ptr
;
310 ret
= get_kernel_runtime_stats(session
, kchan
,
311 &discarded_events
, &lost_packets
);
315 /* Copy lttng_channel struct to array */
316 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
317 channels
[i
].enabled
= kchan
->enabled
;
318 chan_exts
[i
].discarded_events
=
320 chan_exts
[i
].lost_packets
= lost_packets
;
321 chan_exts
[i
].monitor_timer_interval
=
322 extended
->monitor_timer_interval
;
323 chan_exts
[i
].blocking_timeout
= 0;
328 case LTTNG_DOMAIN_UST
:
330 struct lttng_ht_iter iter
;
331 struct ltt_ust_channel
*uchan
;
334 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
335 &iter
.iter
, uchan
, node
.node
) {
336 uint64_t discarded_events
= 0, lost_packets
= 0;
338 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
339 LTTNG_SYMBOL_NAME_LEN
)) {
342 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
343 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
344 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
345 channels
[i
].attr
.switch_timer_interval
=
346 uchan
->attr
.switch_timer_interval
;
347 channels
[i
].attr
.read_timer_interval
=
348 uchan
->attr
.read_timer_interval
;
349 channels
[i
].enabled
= uchan
->enabled
;
350 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
351 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
354 * Map enum lttng_ust_output to enum lttng_event_output.
356 switch (uchan
->attr
.output
) {
358 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
362 * LTTNG_UST_MMAP is the only supported UST
369 chan_exts
[i
].monitor_timer_interval
=
370 uchan
->monitor_timer_interval
;
371 chan_exts
[i
].blocking_timeout
=
372 uchan
->attr
.u
.s
.blocking_timeout
;
374 ret
= get_ust_runtime_stats(session
, uchan
,
375 &discarded_events
, &lost_packets
);
379 chan_exts
[i
].discarded_events
= discarded_events
;
380 chan_exts
[i
].lost_packets
= lost_packets
;
392 return -LTTNG_ERR_FATAL
;
398 static int append_extended_info(const char *filter_expression
,
399 struct lttng_event_exclusion
*exclusion
,
400 struct lttng_userspace_probe_location
*probe_location
,
401 struct lttng_payload
*payload
)
404 size_t filter_len
= 0;
405 size_t nb_exclusions
= 0;
406 size_t userspace_probe_location_len
= 0;
407 struct lttcomm_event_extended_header extended_header
= {};
408 struct lttcomm_event_extended_header
*p_extended_header
;
409 const size_t original_payload_size
= payload
->buffer
.size
;
411 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &extended_header
,
412 sizeof(extended_header
));
417 if (filter_expression
) {
418 filter_len
= strlen(filter_expression
) + 1;
419 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
420 filter_expression
, filter_len
);
427 const size_t len
= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
429 nb_exclusions
= exclusion
->count
;
431 ret
= lttng_dynamic_buffer_append(
432 &payload
->buffer
, &exclusion
->names
, len
);
438 if (probe_location
) {
439 const size_t size_before_probe
= payload
->buffer
.size
;
441 ret
= lttng_userspace_probe_location_serialize(probe_location
,
448 userspace_probe_location_len
=
449 payload
->buffer
.size
- size_before_probe
;
452 /* Set header fields */
453 p_extended_header
= (struct lttcomm_event_extended_header
*)
454 (payload
->buffer
.data
+ original_payload_size
);
456 p_extended_header
->filter_len
= filter_len
;
457 p_extended_header
->nb_exclusions
= nb_exclusions
;
458 p_extended_header
->userspace_probe_location_len
=
459 userspace_probe_location_len
;
467 * Create a list of agent domain events.
469 * Return number of events in list on success or else a negative value.
471 static int list_lttng_agent_events(struct agent
*agt
,
472 struct lttng_payload
*payload
)
474 int nb_events
= 0, ret
= 0;
475 const struct agent_event
*agent_event
;
476 struct lttng_ht_iter iter
;
480 DBG3("Listing agent events");
483 cds_lfht_for_each_entry (
484 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
485 struct lttng_event event
= {
486 .enabled
= agent_event
->enabled
,
487 .loglevel
= agent_event
->loglevel_value
,
488 .loglevel_type
= agent_event
->loglevel_type
,
491 strncpy(event
.name
, agent_event
->name
, sizeof(event
.name
));
492 event
.name
[sizeof(event
.name
) - 1] = '\0';
494 ret
= lttng_dynamic_buffer_append(
495 &payload
->buffer
, &event
, sizeof(event
));
497 ERR("Failed to append event to payload");
498 ret
= -LTTNG_ERR_NOMEM
;
505 cds_lfht_for_each_entry (
506 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
507 /* Append extended info. */
508 ret
= append_extended_info(agent_event
->filter_expression
, NULL
,
511 ERR("Failed to append extended event info to payload");
512 ret
= -LTTNG_ERR_NOMEM
;
524 * Create a list of ust global domain events.
526 static int list_lttng_ust_global_events(char *channel_name
,
527 struct ltt_ust_domain_global
*ust_global
,
528 struct lttng_payload
*payload
)
531 unsigned int nb_events
= 0;
532 struct lttng_ht_iter iter
;
533 const struct lttng_ht_node_str
*node
;
534 const struct ltt_ust_channel
*uchan
;
535 const struct ltt_ust_event
*uevent
;
537 DBG("Listing UST global events for channel %s", channel_name
);
541 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
542 node
= lttng_ht_iter_get_node_str(&iter
);
544 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
548 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
550 DBG3("Listing UST global events");
552 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
553 struct lttng_event event
= {};
555 if (uevent
->internal
) {
559 strncpy(event
.name
, uevent
->attr
.name
, sizeof(event
.name
));
560 event
.name
[sizeof(event
.name
) - 1] = '\0';
562 event
.enabled
= uevent
->enabled
;
564 switch (uevent
->attr
.instrumentation
) {
565 case LTTNG_UST_TRACEPOINT
:
566 event
.type
= LTTNG_EVENT_TRACEPOINT
;
568 case LTTNG_UST_PROBE
:
569 event
.type
= LTTNG_EVENT_PROBE
;
571 case LTTNG_UST_FUNCTION
:
572 event
.type
= LTTNG_EVENT_FUNCTION
;
576 event
.loglevel
= uevent
->attr
.loglevel
;
577 switch (uevent
->attr
.loglevel_type
) {
578 case LTTNG_UST_LOGLEVEL_ALL
:
579 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
581 case LTTNG_UST_LOGLEVEL_RANGE
:
582 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
584 case LTTNG_UST_LOGLEVEL_SINGLE
:
585 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
589 if (uevent
->filter
) {
593 if (uevent
->exclusion
) {
597 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &event
, sizeof(event
));
599 ERR("Failed to append event to payload");
600 ret
= -LTTNG_ERR_NOMEM
;
607 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
608 /* Append extended info. */
609 ret
= append_extended_info(uevent
->filter_expression
,
610 uevent
->exclusion
, NULL
, payload
);
612 ERR("Failed to append extended event info to payload");
613 ret
= -LTTNG_ERR_FATAL
;
625 * Fill lttng_event array of all kernel events in the channel.
627 static int list_lttng_kernel_events(char *channel_name
,
628 struct ltt_kernel_session
*kernel_session
,
629 struct lttng_payload
*payload
)
632 unsigned int nb_event
;
633 const struct ltt_kernel_event
*kevent
;
634 const struct ltt_kernel_channel
*kchan
;
636 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
638 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
642 nb_event
= kchan
->event_count
;
644 DBG("Listing events for channel %s", kchan
->channel
->name
);
646 /* Kernel channels */
647 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
648 struct lttng_event event
= {};
650 strncpy(event
.name
, kevent
->event
->name
, sizeof(event
.name
));
651 event
.name
[sizeof(event
.name
) - 1] = '\0';
652 event
.enabled
= kevent
->enabled
;
653 event
.filter
= (unsigned char) !!kevent
->filter_expression
;
655 switch (kevent
->event
->instrumentation
) {
656 case LTTNG_KERNEL_TRACEPOINT
:
657 event
.type
= LTTNG_EVENT_TRACEPOINT
;
659 case LTTNG_KERNEL_KRETPROBE
:
660 event
.type
= LTTNG_EVENT_FUNCTION
;
661 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
662 sizeof(struct lttng_kernel_kprobe
));
664 case LTTNG_KERNEL_KPROBE
:
665 event
.type
= LTTNG_EVENT_PROBE
;
666 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
667 sizeof(struct lttng_kernel_kprobe
));
669 case LTTNG_KERNEL_UPROBE
:
670 event
.type
= LTTNG_EVENT_USERSPACE_PROBE
;
672 case LTTNG_KERNEL_FUNCTION
:
673 event
.type
= LTTNG_EVENT_FUNCTION
;
674 memcpy(&event
.attr
.ftrace
, &kevent
->event
->u
.ftrace
,
675 sizeof(struct lttng_kernel_function
));
677 case LTTNG_KERNEL_NOOP
:
678 event
.type
= LTTNG_EVENT_NOOP
;
680 case LTTNG_KERNEL_SYSCALL
:
681 event
.type
= LTTNG_EVENT_SYSCALL
;
683 case LTTNG_KERNEL_ALL
:
690 ret
= lttng_dynamic_buffer_append(
691 &payload
->buffer
, &event
, sizeof(event
));
693 ERR("Failed to append event to payload");
694 ret
= -LTTNG_ERR_NOMEM
;
699 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
700 /* Append extended info. */
701 ret
= append_extended_info(kevent
->filter_expression
, NULL
,
702 kevent
->userspace_probe_location
, payload
);
704 DBG("Error appending extended info message");
705 ret
= -LTTNG_ERR_FATAL
;
717 * Add URI so the consumer output object. Set the correct path depending on the
718 * domain adding the default trace directory.
720 static enum lttng_error_code
add_uri_to_consumer(
721 const struct ltt_session
*session
,
722 struct consumer_output
*consumer
,
723 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
726 enum lttng_error_code ret_code
= LTTNG_OK
;
730 if (consumer
== NULL
) {
731 DBG("No consumer detected. Don't add URI. Stopping.");
732 ret_code
= LTTNG_ERR_NO_CONSUMER
;
737 case LTTNG_DOMAIN_KERNEL
:
738 ret
= lttng_strncpy(consumer
->domain_subdir
,
739 DEFAULT_KERNEL_TRACE_DIR
,
740 sizeof(consumer
->domain_subdir
));
742 case LTTNG_DOMAIN_UST
:
743 ret
= lttng_strncpy(consumer
->domain_subdir
,
744 DEFAULT_UST_TRACE_DIR
,
745 sizeof(consumer
->domain_subdir
));
749 * This case is possible is we try to add the URI to the global
750 * tracing session consumer object which in this case there is
753 memset(consumer
->domain_subdir
, 0,
754 sizeof(consumer
->domain_subdir
));
758 ERR("Failed to initialize consumer output domain subdirectory");
759 ret_code
= LTTNG_ERR_FATAL
;
763 switch (uri
->dtype
) {
766 DBG2("Setting network URI to consumer");
768 if (consumer
->type
== CONSUMER_DST_NET
) {
769 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
770 consumer
->dst
.net
.control_isset
) ||
771 (uri
->stype
== LTTNG_STREAM_DATA
&&
772 consumer
->dst
.net
.data_isset
)) {
773 ret_code
= LTTNG_ERR_URL_EXIST
;
777 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
780 /* Set URI into consumer output object */
781 ret
= consumer_set_network_uri(session
, consumer
, uri
);
785 } else if (ret
== 1) {
787 * URI was the same in the consumer so we do not append the subdir
788 * again so to not duplicate output dir.
795 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
796 ret_code
= LTTNG_ERR_INVALID
;
799 DBG2("Setting trace directory path from URI to %s",
801 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
803 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
805 sizeof(consumer
->dst
.session_root_path
));
807 ret_code
= LTTNG_ERR_FATAL
;
810 consumer
->type
= CONSUMER_DST_LOCAL
;
820 * Init tracing by creating trace directory and sending fds kernel consumer.
822 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
825 struct lttng_ht_iter iter
;
826 struct consumer_socket
*socket
;
832 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
833 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
835 pthread_mutex_lock(socket
->lock
);
836 ret
= kernel_consumer_send_session(socket
, session
);
837 pthread_mutex_unlock(socket
->lock
);
839 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
851 * Create a socket to the relayd using the URI.
853 * On success, the relayd_sock pointer is set to the created socket.
854 * Else, it remains untouched and an LTTng error code is returned.
856 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
857 struct lttcomm_relayd_sock
**relayd_sock
,
858 struct consumer_output
*consumer
)
861 enum lttng_error_code status
= LTTNG_OK
;
862 struct lttcomm_relayd_sock
*rsock
;
864 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
865 RELAYD_VERSION_COMM_MINOR
);
867 status
= LTTNG_ERR_FATAL
;
872 * Connect to relayd so we can proceed with a session creation. This call
873 * can possibly block for an arbitrary amount of time to set the health
874 * state to be in poll execution.
877 ret
= relayd_connect(rsock
);
880 ERR("Unable to reach lttng-relayd");
881 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
885 /* Create socket for control stream. */
886 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
887 uint64_t result_flags
;
889 DBG3("Creating relayd stream socket from URI");
891 /* Check relayd version */
892 ret
= relayd_version_check(rsock
);
893 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
894 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
896 } else if (ret
< 0) {
897 ERR("Unable to reach lttng-relayd");
898 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
901 consumer
->relay_major_version
= rsock
->major
;
902 consumer
->relay_minor_version
= rsock
->minor
;
903 ret
= relayd_get_configuration(rsock
, 0,
906 ERR("Unable to get relayd configuration");
907 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
910 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
911 consumer
->relay_allows_clear
= true;
913 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
914 DBG3("Creating relayd data socket from URI");
916 /* Command is not valid */
917 ERR("Relayd invalid stream type: %d", uri
->stype
);
918 status
= LTTNG_ERR_INVALID
;
922 *relayd_sock
= rsock
;
927 /* The returned value is not useful since we are on an error path. */
928 (void) relayd_close(rsock
);
936 * Connect to the relayd using URI and send the socket to the right consumer.
938 * The consumer socket lock must be held by the caller.
940 * Returns LTTNG_OK on success or an LTTng error code on failure.
942 static enum lttng_error_code
send_consumer_relayd_socket(
943 unsigned int session_id
,
944 struct lttng_uri
*relayd_uri
,
945 struct consumer_output
*consumer
,
946 struct consumer_socket
*consumer_sock
,
947 const char *session_name
, const char *hostname
,
948 const char *base_path
, int session_live_timer
,
949 const uint64_t *current_chunk_id
,
950 time_t session_creation_time
,
951 bool session_name_contains_creation_time
)
954 struct lttcomm_relayd_sock
*rsock
= NULL
;
955 enum lttng_error_code status
;
957 /* Connect to relayd and make version check if uri is the control. */
958 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
959 if (status
!= LTTNG_OK
) {
960 goto relayd_comm_error
;
964 /* Set the network sequence index if not set. */
965 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
966 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
968 * Increment net_seq_idx because we are about to transfer the
969 * new relayd socket to the consumer.
970 * Assign unique key so the consumer can match streams.
972 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
973 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
976 /* Send relayd socket to consumer. */
977 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
978 relayd_uri
->stype
, session_id
,
979 session_name
, hostname
, base_path
,
980 session_live_timer
, current_chunk_id
,
981 session_creation_time
, session_name_contains_creation_time
);
983 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
987 /* Flag that the corresponding socket was sent. */
988 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
989 consumer_sock
->control_sock_sent
= 1;
990 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
991 consumer_sock
->data_sock_sent
= 1;
995 * Close socket which was dup on the consumer side. The session daemon does
996 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1000 if (status
!= LTTNG_OK
) {
1002 * The consumer output for this session should not be used anymore
1003 * since the relayd connection failed thus making any tracing or/and
1004 * streaming not usable.
1006 consumer
->enabled
= 0;
1008 (void) relayd_close(rsock
);
1016 * Send both relayd sockets to a specific consumer and domain. This is a
1017 * helper function to facilitate sending the information to the consumer for a
1020 * The consumer socket lock must be held by the caller.
1022 * Returns LTTNG_OK, or an LTTng error code on failure.
1024 static enum lttng_error_code
send_consumer_relayd_sockets(
1025 enum lttng_domain_type domain
,
1026 unsigned int session_id
, struct consumer_output
*consumer
,
1027 struct consumer_socket
*sock
, const char *session_name
,
1028 const char *hostname
, const char *base_path
, int session_live_timer
,
1029 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1030 bool session_name_contains_creation_time
)
1032 enum lttng_error_code status
= LTTNG_OK
;
1037 /* Sending control relayd socket. */
1038 if (!sock
->control_sock_sent
) {
1039 status
= send_consumer_relayd_socket(session_id
,
1040 &consumer
->dst
.net
.control
, consumer
, sock
,
1041 session_name
, hostname
, base_path
, session_live_timer
,
1042 current_chunk_id
, session_creation_time
,
1043 session_name_contains_creation_time
);
1044 if (status
!= LTTNG_OK
) {
1049 /* Sending data relayd socket. */
1050 if (!sock
->data_sock_sent
) {
1051 status
= send_consumer_relayd_socket(session_id
,
1052 &consumer
->dst
.net
.data
, consumer
, sock
,
1053 session_name
, hostname
, base_path
, session_live_timer
,
1054 current_chunk_id
, session_creation_time
,
1055 session_name_contains_creation_time
);
1056 if (status
!= LTTNG_OK
) {
1066 * Setup relayd connections for a tracing session. First creates the socket to
1067 * the relayd and send them to the right domain consumer. Consumer type MUST be
1070 int cmd_setup_relayd(struct ltt_session
*session
)
1073 struct ltt_ust_session
*usess
;
1074 struct ltt_kernel_session
*ksess
;
1075 struct consumer_socket
*socket
;
1076 struct lttng_ht_iter iter
;
1077 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1081 usess
= session
->ust_session
;
1082 ksess
= session
->kernel_session
;
1084 DBG("Setting relayd for session %s", session
->name
);
1087 if (session
->current_trace_chunk
) {
1088 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1089 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1091 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1092 current_chunk_id
.is_set
= true;
1094 ERR("Failed to get current trace chunk id");
1095 ret
= LTTNG_ERR_UNK
;
1100 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1101 && usess
->consumer
->enabled
) {
1102 /* For each consumer socket, send relayd sockets */
1103 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1104 socket
, node
.node
) {
1105 pthread_mutex_lock(socket
->lock
);
1106 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1107 usess
->consumer
, socket
,
1108 session
->name
, session
->hostname
,
1110 session
->live_timer
,
1111 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1112 session
->creation_time
,
1113 session
->name_contains_creation_time
);
1114 pthread_mutex_unlock(socket
->lock
);
1115 if (ret
!= LTTNG_OK
) {
1118 /* Session is now ready for network streaming. */
1119 session
->net_handle
= 1;
1121 session
->consumer
->relay_major_version
=
1122 usess
->consumer
->relay_major_version
;
1123 session
->consumer
->relay_minor_version
=
1124 usess
->consumer
->relay_minor_version
;
1125 session
->consumer
->relay_allows_clear
=
1126 usess
->consumer
->relay_allows_clear
;
1129 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1130 && ksess
->consumer
->enabled
) {
1131 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1132 socket
, node
.node
) {
1133 pthread_mutex_lock(socket
->lock
);
1134 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1135 ksess
->consumer
, socket
,
1136 session
->name
, session
->hostname
,
1138 session
->live_timer
,
1139 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1140 session
->creation_time
,
1141 session
->name_contains_creation_time
);
1142 pthread_mutex_unlock(socket
->lock
);
1143 if (ret
!= LTTNG_OK
) {
1146 /* Session is now ready for network streaming. */
1147 session
->net_handle
= 1;
1149 session
->consumer
->relay_major_version
=
1150 ksess
->consumer
->relay_major_version
;
1151 session
->consumer
->relay_minor_version
=
1152 ksess
->consumer
->relay_minor_version
;
1153 session
->consumer
->relay_allows_clear
=
1154 ksess
->consumer
->relay_allows_clear
;
1163 * Start a kernel session by opening all necessary streams.
1165 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1168 struct ltt_kernel_channel
*kchan
;
1170 /* Open kernel metadata */
1171 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1172 ret
= kernel_open_metadata(ksess
);
1174 ret
= LTTNG_ERR_KERN_META_FAIL
;
1179 /* Open kernel metadata stream */
1180 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1181 ret
= kernel_open_metadata_stream(ksess
);
1183 ERR("Kernel create metadata stream failed");
1184 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1189 /* For each channel */
1190 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1191 if (kchan
->stream_count
== 0) {
1192 ret
= kernel_open_channel_stream(kchan
);
1194 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1197 /* Update the stream global counter */
1198 ksess
->stream_count_global
+= ret
;
1202 /* Setup kernel consumer socket and send fds to it */
1203 ret
= init_kernel_tracing(ksess
);
1205 ret
= LTTNG_ERR_KERN_START_FAIL
;
1209 /* This start the kernel tracing */
1210 ret
= kernel_start_session(ksess
);
1212 ret
= LTTNG_ERR_KERN_START_FAIL
;
1216 /* Quiescent wait after starting trace */
1217 kernel_wait_quiescent();
1227 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1229 struct ltt_kernel_channel
*kchan
;
1230 bool error_occurred
= false;
1233 if (!ksess
|| !ksess
->active
) {
1236 DBG("Stopping kernel tracing");
1238 ret
= kernel_stop_session(ksess
);
1240 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1244 kernel_wait_quiescent();
1246 /* Flush metadata after stopping (if exists) */
1247 if (ksess
->metadata_stream_fd
>= 0) {
1248 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1250 ERR("Kernel metadata flush failed");
1251 error_occurred
= true;
1255 /* Flush all buffers after stopping */
1256 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1257 ret
= kernel_flush_buffer(kchan
);
1259 ERR("Kernel flush buffer error");
1260 error_occurred
= true;
1265 if (error_occurred
) {
1266 ret
= LTTNG_ERR_UNK
;
1275 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1277 int cmd_disable_channel(struct ltt_session
*session
,
1278 enum lttng_domain_type domain
, char *channel_name
)
1281 struct ltt_ust_session
*usess
;
1283 usess
= session
->ust_session
;
1288 case LTTNG_DOMAIN_KERNEL
:
1290 ret
= channel_kernel_disable(session
->kernel_session
,
1292 if (ret
!= LTTNG_OK
) {
1296 kernel_wait_quiescent();
1299 case LTTNG_DOMAIN_UST
:
1301 struct ltt_ust_channel
*uchan
;
1302 struct lttng_ht
*chan_ht
;
1304 chan_ht
= usess
->domain_global
.channels
;
1306 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1307 if (uchan
== NULL
) {
1308 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1312 ret
= channel_ust_disable(usess
, uchan
);
1313 if (ret
!= LTTNG_OK
) {
1319 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1331 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1333 * The wpipe arguments is used as a notifier for the kernel thread.
1335 int cmd_enable_channel(struct ltt_session
*session
,
1336 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1339 struct ltt_ust_session
*usess
= session
->ust_session
;
1340 struct lttng_ht
*chan_ht
;
1342 struct lttng_channel attr
;
1349 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1351 /* Validate channel name */
1352 if (attr
.name
[0] == '.' ||
1353 memchr(attr
.name
, '/', len
) != NULL
) {
1354 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1358 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1363 * Don't try to enable a channel if the session has been started at
1364 * some point in time before. The tracer does not allow it.
1366 if (session
->has_been_started
) {
1367 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1372 * If the session is a live session, remove the switch timer, the
1373 * live timer does the same thing but sends also synchronisation
1374 * beacons for inactive streams.
1376 if (session
->live_timer
> 0) {
1377 attr
.attr
.live_timer_interval
= session
->live_timer
;
1378 attr
.attr
.switch_timer_interval
= 0;
1381 /* Check for feature support */
1382 switch (domain
->type
) {
1383 case LTTNG_DOMAIN_KERNEL
:
1385 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1386 /* Sampling position of buffer is not supported */
1387 WARN("Kernel tracer does not support buffer monitoring. "
1388 "Setting the monitor interval timer to 0 "
1389 "(disabled) for channel '%s' of session '%s'",
1390 attr
.name
, session
->name
);
1391 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1395 case LTTNG_DOMAIN_UST
:
1397 case LTTNG_DOMAIN_JUL
:
1398 case LTTNG_DOMAIN_LOG4J
:
1399 case LTTNG_DOMAIN_PYTHON
:
1400 if (!agent_tracing_is_enabled()) {
1401 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1402 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1407 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1411 switch (domain
->type
) {
1412 case LTTNG_DOMAIN_KERNEL
:
1414 struct ltt_kernel_channel
*kchan
;
1416 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1417 session
->kernel_session
);
1418 if (kchan
== NULL
) {
1419 if (session
->snapshot
.nb_output
> 0 ||
1420 session
->snapshot_mode
) {
1421 /* Enforce mmap output for snapshot sessions. */
1422 attr
.attr
.output
= LTTNG_EVENT_MMAP
;
1424 ret
= channel_kernel_create(session
->kernel_session
, &attr
, wpipe
);
1425 if (attr
.name
[0] != '\0') {
1426 session
->kernel_session
->has_non_default_channel
= 1;
1429 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1432 if (ret
!= LTTNG_OK
) {
1436 kernel_wait_quiescent();
1439 case LTTNG_DOMAIN_UST
:
1440 case LTTNG_DOMAIN_JUL
:
1441 case LTTNG_DOMAIN_LOG4J
:
1442 case LTTNG_DOMAIN_PYTHON
:
1444 struct ltt_ust_channel
*uchan
;
1449 * Current agent implementation limitations force us to allow
1450 * only one channel at once in "agent" subdomains. Each
1451 * subdomain has a default channel name which must be strictly
1454 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1455 if (strncmp(attr
.name
, DEFAULT_JUL_CHANNEL_NAME
,
1456 LTTNG_SYMBOL_NAME_LEN
)) {
1457 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1460 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1461 if (strncmp(attr
.name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1462 LTTNG_SYMBOL_NAME_LEN
)) {
1463 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1466 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1467 if (strncmp(attr
.name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1468 LTTNG_SYMBOL_NAME_LEN
)) {
1469 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1474 chan_ht
= usess
->domain_global
.channels
;
1476 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1477 if (uchan
== NULL
) {
1478 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1479 if (attr
.name
[0] != '\0') {
1480 usess
->has_non_default_channel
= 1;
1483 ret
= channel_ust_enable(usess
, uchan
);
1488 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1492 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1493 session
->has_non_mmap_channel
= true;
1501 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1502 struct ltt_session
*session
,
1503 enum lttng_domain_type domain
,
1504 enum lttng_process_attr process_attr
,
1505 enum lttng_tracking_policy
*policy
)
1507 enum lttng_error_code ret_code
= LTTNG_OK
;
1508 const struct process_attr_tracker
*tracker
;
1511 case LTTNG_DOMAIN_KERNEL
:
1512 if (!session
->kernel_session
) {
1513 ret_code
= LTTNG_ERR_INVALID
;
1516 tracker
= kernel_get_process_attr_tracker(
1517 session
->kernel_session
, process_attr
);
1519 case LTTNG_DOMAIN_UST
:
1520 if (!session
->ust_session
) {
1521 ret_code
= LTTNG_ERR_INVALID
;
1524 tracker
= trace_ust_get_process_attr_tracker(
1525 session
->ust_session
, process_attr
);
1528 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1532 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1534 ret_code
= LTTNG_ERR_INVALID
;
1540 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1541 struct ltt_session
*session
,
1542 enum lttng_domain_type domain
,
1543 enum lttng_process_attr process_attr
,
1544 enum lttng_tracking_policy policy
)
1546 enum lttng_error_code ret_code
= LTTNG_OK
;
1549 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1550 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1551 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1554 ret_code
= LTTNG_ERR_INVALID
;
1559 case LTTNG_DOMAIN_KERNEL
:
1560 if (!session
->kernel_session
) {
1561 ret_code
= LTTNG_ERR_INVALID
;
1564 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1565 session
->kernel_session
, process_attr
, policy
);
1567 case LTTNG_DOMAIN_UST
:
1568 if (!session
->ust_session
) {
1569 ret_code
= LTTNG_ERR_INVALID
;
1572 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1573 session
->ust_session
, process_attr
, policy
);
1576 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1583 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1584 struct ltt_session
*session
,
1585 enum lttng_domain_type domain
,
1586 enum lttng_process_attr process_attr
,
1587 const struct process_attr_value
*value
)
1589 enum lttng_error_code ret_code
= LTTNG_OK
;
1592 case LTTNG_DOMAIN_KERNEL
:
1593 if (!session
->kernel_session
) {
1594 ret_code
= LTTNG_ERR_INVALID
;
1597 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1598 session
->kernel_session
, process_attr
, value
);
1600 case LTTNG_DOMAIN_UST
:
1601 if (!session
->ust_session
) {
1602 ret_code
= LTTNG_ERR_INVALID
;
1605 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1606 session
->ust_session
, process_attr
, value
);
1609 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1616 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1617 struct ltt_session
*session
,
1618 enum lttng_domain_type domain
,
1619 enum lttng_process_attr process_attr
,
1620 const struct process_attr_value
*value
)
1622 enum lttng_error_code ret_code
= LTTNG_OK
;
1625 case LTTNG_DOMAIN_KERNEL
:
1626 if (!session
->kernel_session
) {
1627 ret_code
= LTTNG_ERR_INVALID
;
1630 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1631 session
->kernel_session
, process_attr
, value
);
1633 case LTTNG_DOMAIN_UST
:
1634 if (!session
->ust_session
) {
1635 ret_code
= LTTNG_ERR_INVALID
;
1638 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1639 session
->ust_session
, process_attr
, value
);
1642 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1649 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1650 struct ltt_session
*session
,
1651 enum lttng_domain_type domain
,
1652 enum lttng_process_attr process_attr
,
1653 struct lttng_process_attr_values
**values
)
1655 enum lttng_error_code ret_code
= LTTNG_OK
;
1656 const struct process_attr_tracker
*tracker
;
1657 enum process_attr_tracker_status status
;
1660 case LTTNG_DOMAIN_KERNEL
:
1661 if (!session
->kernel_session
) {
1662 ret_code
= LTTNG_ERR_INVALID
;
1665 tracker
= kernel_get_process_attr_tracker(
1666 session
->kernel_session
, process_attr
);
1668 case LTTNG_DOMAIN_UST
:
1669 if (!session
->ust_session
) {
1670 ret_code
= LTTNG_ERR_INVALID
;
1673 tracker
= trace_ust_get_process_attr_tracker(
1674 session
->ust_session
, process_attr
);
1677 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1682 ret_code
= LTTNG_ERR_INVALID
;
1686 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1688 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1689 ret_code
= LTTNG_OK
;
1691 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1692 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1694 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1695 ret_code
= LTTNG_ERR_NOMEM
;
1698 ret_code
= LTTNG_ERR_UNK
;
1707 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1709 int cmd_disable_event(struct ltt_session
*session
,
1710 enum lttng_domain_type domain
, const char *channel_name
,
1711 const struct lttng_event
*event
)
1714 const char *event_name
;
1716 DBG("Disable event command for event \'%s\'", event
->name
);
1718 event_name
= event
->name
;
1720 /* Error out on unhandled search criteria */
1721 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1722 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1723 ret
= LTTNG_ERR_UNK
;
1730 case LTTNG_DOMAIN_KERNEL
:
1732 struct ltt_kernel_channel
*kchan
;
1733 struct ltt_kernel_session
*ksess
;
1735 ksess
= session
->kernel_session
;
1738 * If a non-default channel has been created in the
1739 * session, explicitely require that -c chan_name needs
1742 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1743 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1747 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1748 if (kchan
== NULL
) {
1749 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1753 switch (event
->type
) {
1754 case LTTNG_EVENT_ALL
:
1755 case LTTNG_EVENT_TRACEPOINT
:
1756 case LTTNG_EVENT_SYSCALL
:
1757 case LTTNG_EVENT_PROBE
:
1758 case LTTNG_EVENT_FUNCTION
:
1759 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1760 if (event_name
[0] == '\0') {
1761 ret
= event_kernel_disable_event(kchan
,
1764 ret
= event_kernel_disable_event(kchan
,
1765 event_name
, event
->type
);
1767 if (ret
!= LTTNG_OK
) {
1772 ret
= LTTNG_ERR_UNK
;
1776 kernel_wait_quiescent();
1779 case LTTNG_DOMAIN_UST
:
1781 struct ltt_ust_channel
*uchan
;
1782 struct ltt_ust_session
*usess
;
1784 usess
= session
->ust_session
;
1786 if (validate_ust_event_name(event_name
)) {
1787 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1792 * If a non-default channel has been created in the
1793 * session, explicitly require that -c chan_name needs
1796 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1797 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1801 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1803 if (uchan
== NULL
) {
1804 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1808 switch (event
->type
) {
1809 case LTTNG_EVENT_ALL
:
1811 * An empty event name means that everything
1812 * should be disabled.
1814 if (event
->name
[0] == '\0') {
1815 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1817 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1820 if (ret
!= LTTNG_OK
) {
1825 ret
= LTTNG_ERR_UNK
;
1829 DBG3("Disable UST event %s in channel %s completed", event_name
,
1833 case LTTNG_DOMAIN_LOG4J
:
1834 case LTTNG_DOMAIN_JUL
:
1835 case LTTNG_DOMAIN_PYTHON
:
1838 struct ltt_ust_session
*usess
= session
->ust_session
;
1842 switch (event
->type
) {
1843 case LTTNG_EVENT_ALL
:
1846 ret
= LTTNG_ERR_UNK
;
1850 agt
= trace_ust_find_agent(usess
, domain
);
1852 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1856 * An empty event name means that everything
1857 * should be disabled.
1859 if (event
->name
[0] == '\0') {
1860 ret
= event_agent_disable_all(usess
, agt
);
1862 ret
= event_agent_disable(usess
, agt
, event_name
);
1864 if (ret
!= LTTNG_OK
) {
1871 ret
= LTTNG_ERR_UND
;
1884 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1886 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1887 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1889 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1890 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1893 * Don't try to add a context if the session has been started at
1894 * some point in time before. The tracer does not allow it and would
1895 * result in a corrupted trace.
1897 if (session
->has_been_started
) {
1898 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1902 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1903 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1904 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1908 case LTTNG_DOMAIN_KERNEL
:
1909 assert(session
->kernel_session
);
1911 if (session
->kernel_session
->channel_count
== 0) {
1912 /* Create default channel */
1913 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1914 if (ret
!= LTTNG_OK
) {
1917 chan_kern_created
= 1;
1919 /* Add kernel context to kernel tracer */
1920 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1921 if (ret
!= LTTNG_OK
) {
1925 case LTTNG_DOMAIN_JUL
:
1926 case LTTNG_DOMAIN_LOG4J
:
1929 * Validate channel name.
1930 * If no channel name is given and the domain is JUL or LOG4J,
1931 * set it to the appropriate domain-specific channel name. If
1932 * a name is provided but does not match the expexted channel
1933 * name, return an error.
1935 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1936 strcmp(channel_name
,
1937 DEFAULT_JUL_CHANNEL_NAME
)) {
1938 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1940 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1941 strcmp(channel_name
,
1942 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1943 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1946 /* break is _not_ missing here. */
1948 case LTTNG_DOMAIN_UST
:
1950 struct ltt_ust_session
*usess
= session
->ust_session
;
1951 unsigned int chan_count
;
1955 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1956 if (chan_count
== 0) {
1957 struct lttng_channel
*attr
;
1958 /* Create default channel */
1959 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1961 ret
= LTTNG_ERR_FATAL
;
1965 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1966 if (ret
!= LTTNG_OK
) {
1970 channel_attr_destroy(attr
);
1971 chan_ust_created
= 1;
1974 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1975 free(app_ctx_provider_name
);
1977 app_ctx_name
= NULL
;
1978 app_ctx_provider_name
= NULL
;
1979 if (ret
!= LTTNG_OK
) {
1985 ret
= LTTNG_ERR_UND
;
1993 if (chan_kern_created
) {
1994 struct ltt_kernel_channel
*kchan
=
1995 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1996 session
->kernel_session
);
1997 /* Created previously, this should NOT fail. */
1999 kernel_destroy_channel(kchan
);
2002 if (chan_ust_created
) {
2003 struct ltt_ust_channel
*uchan
=
2004 trace_ust_find_channel_by_name(
2005 session
->ust_session
->domain_global
.channels
,
2006 DEFAULT_CHANNEL_NAME
);
2007 /* Created previously, this should NOT fail. */
2009 /* Remove from the channel list of the session. */
2010 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2012 trace_ust_destroy_channel(uchan
);
2015 free(app_ctx_provider_name
);
2020 static inline bool name_starts_with(const char *name
, const char *prefix
)
2022 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2024 return !strncmp(name
, prefix
, max_cmp_len
);
2027 /* Perform userspace-specific event name validation */
2028 static int validate_ust_event_name(const char *name
)
2038 * Check name against all internal UST event component namespaces used
2041 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2042 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2043 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2052 * Internal version of cmd_enable_event() with a supplemental
2053 * "internal_event" flag which is used to enable internal events which should
2054 * be hidden from clients. Such events are used in the agent implementation to
2055 * enable the events through which all "agent" events are funeled.
2057 static int _cmd_enable_event(struct ltt_session
*session
,
2058 const struct lttng_domain
*domain
,
2059 char *channel_name
, struct lttng_event
*event
,
2060 char *filter_expression
,
2061 struct lttng_filter_bytecode
*filter
,
2062 struct lttng_event_exclusion
*exclusion
,
2063 int wpipe
, bool internal_event
)
2065 int ret
= 0, channel_created
= 0;
2066 struct lttng_channel
*attr
= NULL
;
2070 assert(channel_name
);
2072 /* If we have a filter, we must have its filter expression */
2073 assert(!(!!filter_expression
^ !!filter
));
2075 /* Normalize event name as a globbing pattern */
2076 strutils_normalize_star_glob_pattern(event
->name
);
2078 /* Normalize exclusion names as globbing patterns */
2082 for (i
= 0; i
< exclusion
->count
; i
++) {
2083 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2085 strutils_normalize_star_glob_pattern(name
);
2089 DBG("Enable event command for event \'%s\'", event
->name
);
2093 switch (domain
->type
) {
2094 case LTTNG_DOMAIN_KERNEL
:
2096 struct ltt_kernel_channel
*kchan
;
2099 * If a non-default channel has been created in the
2100 * session, explicitely require that -c chan_name needs
2103 if (session
->kernel_session
->has_non_default_channel
2104 && channel_name
[0] == '\0') {
2105 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2109 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2110 session
->kernel_session
);
2111 if (kchan
== NULL
) {
2112 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2113 LTTNG_BUFFER_GLOBAL
);
2115 ret
= LTTNG_ERR_FATAL
;
2118 if (lttng_strncpy(attr
->name
, channel_name
,
2119 sizeof(attr
->name
))) {
2120 ret
= LTTNG_ERR_INVALID
;
2124 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2125 if (ret
!= LTTNG_OK
) {
2128 channel_created
= 1;
2131 /* Get the newly created kernel channel pointer */
2132 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2133 session
->kernel_session
);
2134 if (kchan
== NULL
) {
2135 /* This sould not happen... */
2136 ret
= LTTNG_ERR_FATAL
;
2140 switch (event
->type
) {
2141 case LTTNG_EVENT_ALL
:
2143 char *filter_expression_a
= NULL
;
2144 struct lttng_filter_bytecode
*filter_a
= NULL
;
2147 * We need to duplicate filter_expression and filter,
2148 * because ownership is passed to first enable
2151 if (filter_expression
) {
2152 filter_expression_a
= strdup(filter_expression
);
2153 if (!filter_expression_a
) {
2154 ret
= LTTNG_ERR_FATAL
;
2159 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2161 free(filter_expression_a
);
2162 ret
= LTTNG_ERR_FATAL
;
2165 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2167 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2168 ret
= event_kernel_enable_event(kchan
, event
,
2169 filter_expression
, filter
);
2170 /* We have passed ownership */
2171 filter_expression
= NULL
;
2173 if (ret
!= LTTNG_OK
) {
2174 if (channel_created
) {
2175 /* Let's not leak a useless channel. */
2176 kernel_destroy_channel(kchan
);
2178 free(filter_expression_a
);
2182 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2183 ret
= event_kernel_enable_event(kchan
, event
,
2184 filter_expression_a
, filter_a
);
2185 /* We have passed ownership */
2186 filter_expression_a
= NULL
;
2188 if (ret
!= LTTNG_OK
) {
2193 case LTTNG_EVENT_PROBE
:
2194 case LTTNG_EVENT_USERSPACE_PROBE
:
2195 case LTTNG_EVENT_FUNCTION
:
2196 case LTTNG_EVENT_FUNCTION_ENTRY
:
2197 case LTTNG_EVENT_TRACEPOINT
:
2198 ret
= event_kernel_enable_event(kchan
, event
,
2199 filter_expression
, filter
);
2200 /* We have passed ownership */
2201 filter_expression
= NULL
;
2203 if (ret
!= LTTNG_OK
) {
2204 if (channel_created
) {
2205 /* Let's not leak a useless channel. */
2206 kernel_destroy_channel(kchan
);
2211 case LTTNG_EVENT_SYSCALL
:
2212 ret
= event_kernel_enable_event(kchan
, event
,
2213 filter_expression
, filter
);
2214 /* We have passed ownership */
2215 filter_expression
= NULL
;
2217 if (ret
!= LTTNG_OK
) {
2222 ret
= LTTNG_ERR_UNK
;
2226 kernel_wait_quiescent();
2229 case LTTNG_DOMAIN_UST
:
2231 struct ltt_ust_channel
*uchan
;
2232 struct ltt_ust_session
*usess
= session
->ust_session
;
2237 * If a non-default channel has been created in the
2238 * session, explicitely require that -c chan_name needs
2241 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2242 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2246 /* Get channel from global UST domain */
2247 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2249 if (uchan
== NULL
) {
2250 /* Create default channel */
2251 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2252 usess
->buffer_type
);
2254 ret
= LTTNG_ERR_FATAL
;
2257 if (lttng_strncpy(attr
->name
, channel_name
,
2258 sizeof(attr
->name
))) {
2259 ret
= LTTNG_ERR_INVALID
;
2263 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2264 if (ret
!= LTTNG_OK
) {
2268 /* Get the newly created channel reference back */
2269 uchan
= trace_ust_find_channel_by_name(
2270 usess
->domain_global
.channels
, channel_name
);
2274 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2276 * Don't allow users to add UST events to channels which
2277 * are assigned to a userspace subdomain (JUL, Log4J,
2280 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2284 if (!internal_event
) {
2286 * Ensure the event name is not reserved for internal
2289 ret
= validate_ust_event_name(event
->name
);
2291 WARN("Userspace event name %s failed validation.",
2293 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2298 /* At this point, the session and channel exist on the tracer */
2299 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2300 filter_expression
, filter
, exclusion
,
2302 /* We have passed ownership */
2303 filter_expression
= NULL
;
2306 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2307 goto already_enabled
;
2308 } else if (ret
!= LTTNG_OK
) {
2313 case LTTNG_DOMAIN_LOG4J
:
2314 case LTTNG_DOMAIN_JUL
:
2315 case LTTNG_DOMAIN_PYTHON
:
2317 const char *default_event_name
, *default_chan_name
;
2319 struct lttng_event uevent
;
2320 struct lttng_domain tmp_dom
;
2321 struct ltt_ust_session
*usess
= session
->ust_session
;
2325 if (!agent_tracing_is_enabled()) {
2326 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2327 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2331 agt
= trace_ust_find_agent(usess
, domain
->type
);
2333 agt
= agent_create(domain
->type
);
2335 ret
= LTTNG_ERR_NOMEM
;
2338 agent_add(agt
, usess
->agents
);
2341 /* Create the default tracepoint. */
2342 memset(&uevent
, 0, sizeof(uevent
));
2343 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2344 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2345 default_event_name
= event_get_default_agent_ust_name(
2347 if (!default_event_name
) {
2348 ret
= LTTNG_ERR_FATAL
;
2351 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2352 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2355 * The domain type is changed because we are about to enable the
2356 * default channel and event for the JUL domain that are hardcoded.
2357 * This happens in the UST domain.
2359 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2360 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2362 switch (domain
->type
) {
2363 case LTTNG_DOMAIN_LOG4J
:
2364 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2366 case LTTNG_DOMAIN_JUL
:
2367 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2369 case LTTNG_DOMAIN_PYTHON
:
2370 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2373 /* The switch/case we are in makes this impossible */
2378 char *filter_expression_copy
= NULL
;
2379 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2382 const size_t filter_size
= sizeof(
2383 struct lttng_filter_bytecode
)
2386 filter_copy
= zmalloc(filter_size
);
2388 ret
= LTTNG_ERR_NOMEM
;
2391 memcpy(filter_copy
, filter
, filter_size
);
2393 filter_expression_copy
=
2394 strdup(filter_expression
);
2395 if (!filter_expression
) {
2396 ret
= LTTNG_ERR_NOMEM
;
2399 if (!filter_expression_copy
|| !filter_copy
) {
2400 free(filter_expression_copy
);
2406 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2407 (char *) default_chan_name
,
2408 &uevent
, filter_expression_copy
,
2409 filter_copy
, NULL
, wpipe
);
2412 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2413 goto already_enabled
;
2414 } else if (ret
!= LTTNG_OK
) {
2418 /* The wild card * means that everything should be enabled. */
2419 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2420 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2423 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2427 filter_expression
= NULL
;
2428 if (ret
!= LTTNG_OK
) {
2435 ret
= LTTNG_ERR_UND
;
2443 free(filter_expression
);
2446 channel_attr_destroy(attr
);
2452 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2453 * We own filter, exclusion, and filter_expression.
2455 int cmd_enable_event(struct ltt_session
*session
,
2456 const struct lttng_domain
*domain
,
2457 char *channel_name
, struct lttng_event
*event
,
2458 char *filter_expression
,
2459 struct lttng_filter_bytecode
*filter
,
2460 struct lttng_event_exclusion
*exclusion
,
2463 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2464 filter_expression
, filter
, exclusion
, wpipe
, false);
2468 * Enable an event which is internal to LTTng. An internal should
2469 * never be made visible to clients and are immune to checks such as
2472 static int cmd_enable_event_internal(struct ltt_session
*session
,
2473 const struct lttng_domain
*domain
,
2474 char *channel_name
, struct lttng_event
*event
,
2475 char *filter_expression
,
2476 struct lttng_filter_bytecode
*filter
,
2477 struct lttng_event_exclusion
*exclusion
,
2480 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2481 filter_expression
, filter
, exclusion
, wpipe
, true);
2485 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2487 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2488 struct lttng_event
**events
)
2491 ssize_t nb_events
= 0;
2494 case LTTNG_DOMAIN_KERNEL
:
2495 nb_events
= kernel_list_events(events
);
2496 if (nb_events
< 0) {
2497 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2501 case LTTNG_DOMAIN_UST
:
2502 nb_events
= ust_app_list_events(events
);
2503 if (nb_events
< 0) {
2504 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2508 case LTTNG_DOMAIN_LOG4J
:
2509 case LTTNG_DOMAIN_JUL
:
2510 case LTTNG_DOMAIN_PYTHON
:
2511 nb_events
= agent_list_events(events
, domain
);
2512 if (nb_events
< 0) {
2513 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2518 ret
= LTTNG_ERR_UND
;
2525 /* Return negative value to differentiate return code */
2530 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2532 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2533 struct lttng_event_field
**fields
)
2536 ssize_t nb_fields
= 0;
2539 case LTTNG_DOMAIN_UST
:
2540 nb_fields
= ust_app_list_event_fields(fields
);
2541 if (nb_fields
< 0) {
2542 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2546 case LTTNG_DOMAIN_KERNEL
:
2547 default: /* fall-through */
2548 ret
= LTTNG_ERR_UND
;
2555 /* Return negative value to differentiate return code */
2559 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2561 return syscall_table_list(events
);
2565 * Command LTTNG_START_TRACE processed by the client thread.
2567 * Called with session mutex held.
2569 int cmd_start_trace(struct ltt_session
*session
)
2571 enum lttng_error_code ret
;
2572 unsigned long nb_chan
= 0;
2573 struct ltt_kernel_session
*ksession
;
2574 struct ltt_ust_session
*usess
;
2575 const bool session_rotated_after_last_stop
=
2576 session
->rotated_after_last_stop
;
2577 const bool session_cleared_after_last_stop
=
2578 session
->cleared_after_last_stop
;
2582 /* Ease our life a bit ;) */
2583 ksession
= session
->kernel_session
;
2584 usess
= session
->ust_session
;
2586 /* Is the session already started? */
2587 if (session
->active
) {
2588 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2589 /* Perform nothing */
2593 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2594 !session
->current_trace_chunk
) {
2596 * A rotation was launched while the session was stopped and
2597 * it has not been completed yet. It is not possible to start
2598 * the session since starting the session here would require a
2599 * rotation from "NULL" to a new trace chunk. That rotation
2600 * would overlap with the ongoing rotation, which is not
2603 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2605 ret
= LTTNG_ERR_ROTATION_PENDING
;
2610 * Starting a session without channel is useless since after that it's not
2611 * possible to enable channel thus inform the client.
2613 if (usess
&& usess
->domain_global
.channels
) {
2614 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2617 nb_chan
+= ksession
->channel_count
;
2620 ret
= LTTNG_ERR_NO_CHANNEL
;
2624 session
->active
= 1;
2625 session
->rotated_after_last_stop
= false;
2626 session
->cleared_after_last_stop
= false;
2627 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2628 if (!session
->has_been_started
) {
2629 struct lttng_trace_chunk
*trace_chunk
;
2631 DBG("Creating initial trace chunk of session \"%s\"",
2633 trace_chunk
= session_create_new_trace_chunk(
2634 session
, NULL
, NULL
, NULL
);
2636 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2639 assert(!session
->current_trace_chunk
);
2640 ret
= session_set_trace_chunk(session
, trace_chunk
,
2642 lttng_trace_chunk_put(trace_chunk
);
2644 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2648 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2651 * Rotate existing streams into the new chunk.
2652 * This is a "quiet" rotation has no client has
2653 * explicitly requested this operation.
2655 * There is also no need to wait for the rotation
2656 * to complete as it will happen immediately. No data
2657 * was produced as the session was stopped, so the
2658 * rotation should happen on reception of the command.
2660 ret
= cmd_rotate_session(session
, NULL
, true,
2661 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2662 if (ret
!= LTTNG_OK
) {
2668 /* Kernel tracing */
2669 if (ksession
!= NULL
) {
2670 DBG("Start kernel tracing session %s", session
->name
);
2671 ret
= start_kernel_session(ksession
);
2672 if (ret
!= LTTNG_OK
) {
2677 /* Flag session that trace should start automatically */
2679 int int_ret
= ust_app_start_trace_all(usess
);
2682 ret
= LTTNG_ERR_UST_START_FAIL
;
2688 * Open a packet in every stream of the session to ensure that viewers
2689 * can correctly identify the boundaries of the periods during which
2690 * tracing was active for this session.
2692 ret
= session_open_packets(session
);
2693 if (ret
!= LTTNG_OK
) {
2698 * Clear the flag that indicates that a rotation was done while the
2699 * session was stopped.
2701 session
->rotated_after_last_stop
= false;
2703 if (session
->rotate_timer_period
) {
2704 int int_ret
= timer_session_rotation_schedule_timer_start(
2705 session
, session
->rotate_timer_period
);
2708 ERR("Failed to enable rotate timer");
2709 ret
= LTTNG_ERR_UNK
;
2717 if (ret
== LTTNG_OK
) {
2718 /* Flag this after a successful start. */
2719 session
->has_been_started
|= 1;
2721 session
->active
= 0;
2722 /* Restore initial state on error. */
2723 session
->rotated_after_last_stop
=
2724 session_rotated_after_last_stop
;
2725 session
->cleared_after_last_stop
=
2726 session_cleared_after_last_stop
;
2733 * Command LTTNG_STOP_TRACE processed by the client thread.
2735 int cmd_stop_trace(struct ltt_session
*session
)
2738 struct ltt_kernel_session
*ksession
;
2739 struct ltt_ust_session
*usess
;
2743 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2745 ksession
= session
->kernel_session
;
2746 usess
= session
->ust_session
;
2748 /* Session is not active. Skip everythong and inform the client. */
2749 if (!session
->active
) {
2750 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2754 ret
= stop_kernel_session(ksession
);
2755 if (ret
!= LTTNG_OK
) {
2759 if (usess
&& usess
->active
) {
2760 ret
= ust_app_stop_trace_all(usess
);
2762 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2767 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2769 /* Flag inactive after a successful stop. */
2770 session
->active
= 0;
2778 * Set the base_path of the session only if subdir of a control uris is set.
2779 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2781 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2783 struct lttng_uri
*uris
)
2788 for (i
= 0; i
< nb_uri
; i
++) {
2789 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2790 uris
[i
].subdir
[0] == '\0') {
2791 /* Not interested in these URIs */
2795 if (session
->base_path
!= NULL
) {
2796 free(session
->base_path
);
2797 session
->base_path
= NULL
;
2800 /* Set session base_path */
2801 session
->base_path
= strdup(uris
[i
].subdir
);
2802 if (!session
->base_path
) {
2803 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2804 uris
[i
].subdir
, session
->name
);
2805 ret
= LTTNG_ERR_NOMEM
;
2808 DBG2("Setting base path \"%s\" for session \"%s\"",
2809 session
->base_path
, session
->name
);
2817 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2819 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2820 struct lttng_uri
*uris
)
2823 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2824 struct ltt_ust_session
*usess
= session
->ust_session
;
2830 /* Can't set consumer URI if the session is active. */
2831 if (session
->active
) {
2832 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2837 * Set the session base path if any. This is done inside
2838 * cmd_set_consumer_uri to preserve backward compatibility of the
2839 * previous session creation api vs the session descriptor api.
2841 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2842 if (ret
!= LTTNG_OK
) {
2846 /* Set the "global" consumer URIs */
2847 for (i
= 0; i
< nb_uri
; i
++) {
2848 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2850 if (ret
!= LTTNG_OK
) {
2855 /* Set UST session URIs */
2856 if (session
->ust_session
) {
2857 for (i
= 0; i
< nb_uri
; i
++) {
2858 ret
= add_uri_to_consumer(session
,
2859 session
->ust_session
->consumer
,
2860 &uris
[i
], LTTNG_DOMAIN_UST
);
2861 if (ret
!= LTTNG_OK
) {
2867 /* Set kernel session URIs */
2868 if (session
->kernel_session
) {
2869 for (i
= 0; i
< nb_uri
; i
++) {
2870 ret
= add_uri_to_consumer(session
,
2871 session
->kernel_session
->consumer
,
2872 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2873 if (ret
!= LTTNG_OK
) {
2880 * Make sure to set the session in output mode after we set URI since a
2881 * session can be created without URL (thus flagged in no output mode).
2883 session
->output_traces
= 1;
2885 ksess
->output_traces
= 1;
2889 usess
->output_traces
= 1;
2900 enum lttng_error_code
set_session_output_from_descriptor(
2901 struct ltt_session
*session
,
2902 const struct lttng_session_descriptor
*descriptor
)
2905 enum lttng_error_code ret_code
= LTTNG_OK
;
2906 enum lttng_session_descriptor_type session_type
=
2907 lttng_session_descriptor_get_type(descriptor
);
2908 enum lttng_session_descriptor_output_type output_type
=
2909 lttng_session_descriptor_get_output_type(descriptor
);
2910 struct lttng_uri uris
[2] = {};
2911 size_t uri_count
= 0;
2913 switch (output_type
) {
2914 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2916 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2917 lttng_session_descriptor_get_local_output_uri(descriptor
,
2921 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2922 lttng_session_descriptor_get_network_output_uris(descriptor
,
2923 &uris
[0], &uris
[1]);
2927 ret_code
= LTTNG_ERR_INVALID
;
2931 switch (session_type
) {
2932 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2934 struct snapshot_output
*new_output
= NULL
;
2936 new_output
= snapshot_output_alloc();
2938 ret_code
= LTTNG_ERR_NOMEM
;
2942 ret
= snapshot_output_init_with_uri(session
,
2943 DEFAULT_SNAPSHOT_MAX_SIZE
,
2944 NULL
, uris
, uri_count
, session
->consumer
,
2945 new_output
, &session
->snapshot
);
2947 ret_code
= (ret
== -ENOMEM
) ?
2948 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2949 snapshot_output_destroy(new_output
);
2952 snapshot_add_output(&session
->snapshot
, new_output
);
2955 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2956 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2958 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2962 ret_code
= LTTNG_ERR_INVALID
;
2970 enum lttng_error_code
cmd_create_session_from_descriptor(
2971 struct lttng_session_descriptor
*descriptor
,
2972 const lttng_sock_cred
*creds
,
2973 const char *home_path
)
2976 enum lttng_error_code ret_code
;
2977 const char *session_name
;
2978 struct ltt_session
*new_session
= NULL
;
2979 enum lttng_session_descriptor_status descriptor_status
;
2981 session_lock_list();
2983 if (*home_path
!= '/') {
2984 ERR("Home path provided by client is not absolute");
2985 ret_code
= LTTNG_ERR_INVALID
;
2990 descriptor_status
= lttng_session_descriptor_get_session_name(
2991 descriptor
, &session_name
);
2992 switch (descriptor_status
) {
2993 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
2995 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
2996 session_name
= NULL
;
2999 ret_code
= LTTNG_ERR_INVALID
;
3003 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3005 if (ret_code
!= LTTNG_OK
) {
3009 if (!session_name
) {
3010 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3013 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3018 if (!lttng_session_descriptor_is_output_destination_initialized(
3021 * Only include the session's creation time in the output
3022 * destination if the name of the session itself was
3023 * not auto-generated.
3025 ret_code
= lttng_session_descriptor_set_default_output(
3027 session_name
? &new_session
->creation_time
: NULL
,
3029 if (ret_code
!= LTTNG_OK
) {
3033 new_session
->has_user_specified_directory
=
3034 lttng_session_descriptor_has_output_directory(
3038 switch (lttng_session_descriptor_get_type(descriptor
)) {
3039 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3040 new_session
->snapshot_mode
= 1;
3042 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3043 new_session
->live_timer
=
3044 lttng_session_descriptor_live_get_timer_interval(
3051 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3052 if (ret_code
!= LTTNG_OK
) {
3055 new_session
->consumer
->enabled
= 1;
3056 ret_code
= LTTNG_OK
;
3058 /* Release reference provided by the session_create function. */
3059 session_put(new_session
);
3060 if (ret_code
!= LTTNG_OK
&& new_session
) {
3061 /* Release the global reference on error. */
3062 session_destroy(new_session
);
3064 session_unlock_list();
3068 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3069 struct lttng_session_descriptor
**return_descriptor
)
3072 size_t payload_size
;
3073 struct lttng_dynamic_buffer payload
;
3074 struct lttng_buffer_view home_dir_view
;
3075 struct lttng_buffer_view session_descriptor_view
;
3076 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3077 enum lttng_error_code ret_code
;
3079 lttng_dynamic_buffer_init(&payload
);
3080 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3082 ret_code
= LTTNG_ERR_INVALID
;
3085 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3086 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3087 ret_code
= LTTNG_ERR_INVALID
;
3091 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3092 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3093 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3095 ret_code
= LTTNG_ERR_NOMEM
;
3099 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3101 ERR("Reception of session descriptor failed, aborting.");
3102 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3106 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3109 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3110 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3111 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3112 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3113 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3117 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3119 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3120 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3121 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3122 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3123 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3127 ret
= lttng_session_descriptor_create_from_buffer(
3128 &session_descriptor_view
, &session_descriptor
);
3130 ERR("Failed to create session descriptor from payload of \"create session\" command");
3131 ret_code
= LTTNG_ERR_INVALID
;
3136 * Sets the descriptor's auto-generated properties (name, output) if
3139 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3141 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3142 if (ret_code
!= LTTNG_OK
) {
3146 ret_code
= LTTNG_OK
;
3147 *return_descriptor
= session_descriptor
;
3148 session_descriptor
= NULL
;
3150 lttng_dynamic_buffer_reset(&payload
);
3151 lttng_session_descriptor_destroy(session_descriptor
);
3156 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3157 void *_reply_context
)
3161 const struct cmd_destroy_session_reply_context
*reply_context
=
3163 struct lttng_dynamic_buffer payload
;
3164 struct lttcomm_session_destroy_command_header cmd_header
;
3165 struct lttng_trace_archive_location
*location
= NULL
;
3166 struct lttcomm_lttng_msg llm
= {
3167 .cmd_type
= LTTNG_DESTROY_SESSION
,
3168 .ret_code
= reply_context
->destruction_status
,
3171 sizeof(struct lttcomm_session_destroy_command_header
),
3174 size_t payload_size_before_location
;
3176 lttng_dynamic_buffer_init(&payload
);
3178 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3180 ERR("Failed to append session destruction message");
3184 cmd_header
.rotation_state
=
3185 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3186 session
->rotation_state
:
3187 LTTNG_ROTATION_STATE_NO_ROTATION
);
3188 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3189 sizeof(cmd_header
));
3191 ERR("Failed to append session destruction command header");
3195 if (!reply_context
->implicit_rotation_on_destroy
) {
3196 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3200 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3201 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3206 location
= session_get_trace_archive_location(session
);
3208 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3213 payload_size_before_location
= payload
.size
;
3214 comm_ret
= lttng_trace_archive_location_serialize(location
,
3217 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3221 /* Update the message to indicate the location's length. */
3222 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3223 payload
.size
- payload_size_before_location
;
3225 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3226 payload
.data
, payload
.size
);
3227 if (comm_ret
!= (ssize_t
) payload
.size
) {
3228 ERR("Failed to send result of the destruction of session \"%s\" to client",
3232 ret
= close(reply_context
->reply_sock_fd
);
3234 PERROR("Failed to close client socket in deferred session destroy reply");
3236 lttng_dynamic_buffer_reset(&payload
);
3237 free(_reply_context
);
3241 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3243 * Called with session lock held.
3245 int cmd_destroy_session(struct ltt_session
*session
,
3246 struct notification_thread_handle
*notification_thread_handle
,
3250 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3251 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3254 reply_context
= zmalloc(sizeof(*reply_context
));
3255 if (!reply_context
) {
3256 ret
= LTTNG_ERR_NOMEM
;
3259 reply_context
->reply_sock_fd
= *sock_fd
;
3265 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3267 if (session
->active
) {
3268 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3270 ret
= cmd_stop_trace(session
);
3271 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3272 /* Carry on with the destruction of the session. */
3273 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3274 session
->name
, lttng_strerror(-ret
));
3275 destruction_last_error
= ret
;
3279 if (session
->rotation_schedule_timer_enabled
) {
3280 if (timer_session_rotation_schedule_timer_stop(
3282 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3284 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3288 if (session
->rotate_size
) {
3289 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3290 session
->rotate_size
= 0;
3293 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3295 * Perform a last rotation on destruction if rotations have
3296 * occurred during the session's lifetime.
3298 ret
= cmd_rotate_session(session
, NULL
, false,
3299 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3300 if (ret
!= LTTNG_OK
) {
3301 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3302 session
->name
, lttng_strerror(-ret
));
3303 destruction_last_error
= -ret
;
3305 if (reply_context
) {
3306 reply_context
->implicit_rotation_on_destroy
= true;
3308 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3310 * The user has not triggered a session rotation. However, to
3311 * ensure all data has been consumed, the session is rotated
3312 * to a 'null' trace chunk before it is destroyed.
3314 * This is a "quiet" rotation meaning that no notification is
3315 * emitted and no renaming of the current trace chunk takes
3318 ret
= cmd_rotate_session(session
, NULL
, true,
3319 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3321 * Rotation operations may not be supported by the kernel
3322 * tracer. Hence, do not consider this implicit rotation as
3323 * a session destruction error. The library has already stopped
3324 * the session and waited for pending data; there is nothing
3325 * left to do but complete the destruction of the session.
3327 if (ret
!= LTTNG_OK
&&
3328 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3329 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3330 session
->name
, lttng_strerror(ret
));
3331 destruction_last_error
= -ret
;
3335 if (session
->shm_path
[0]) {
3337 * When a session is created with an explicit shm_path,
3338 * the consumer daemon will create its shared memory files
3339 * at that location and will *not* unlink them. This is normal
3340 * as the intention of that feature is to make it possible
3341 * to retrieve the content of those files should a crash occur.
3343 * To ensure the content of those files can be used, the
3344 * sessiond daemon will replicate the content of the metadata
3345 * cache in a metadata file.
3347 * On clean-up, it is expected that the consumer daemon will
3348 * unlink the shared memory files and that the session daemon
3349 * will unlink the metadata file. Then, the session's directory
3350 * in the shm path can be removed.
3352 * Unfortunately, a flaw in the design of the sessiond's and
3353 * consumerd's tear down of channels makes it impossible to
3354 * determine when the sessiond _and_ the consumerd have both
3355 * destroyed their representation of a channel. For one, the
3356 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3357 * callbacks in both daemons.
3359 * However, it is also impossible for the sessiond to know when
3360 * the consumer daemon is done destroying its channel(s) since
3361 * it occurs as a reaction to the closing of the channel's file
3362 * descriptor. There is no resulting communication initiated
3363 * from the consumerd to the sessiond to confirm that the
3364 * operation is completed (and was successful).
3366 * Until this is all fixed, the session daemon checks for the
3367 * removal of the session's shm path which makes it possible
3368 * to safely advertise a session as having been destroyed.
3370 * Prior to this fix, it was not possible to reliably save
3371 * a session making use of the --shm-path option, destroy it,
3372 * and load it again. This is because the creation of the
3373 * session would fail upon seeing the session's shm path
3374 * already in existence.
3376 * Note that none of the error paths in the check for the
3377 * directory's existence return an error. This is normal
3378 * as there isn't much that can be done. The session will
3379 * be destroyed properly, except that we can't offer the
3380 * guarantee that the same session can be re-created.
3382 current_completion_handler
= &destroy_completion_handler
.handler
;
3383 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3385 sizeof(destroy_completion_handler
.shm_path
));
3390 * The session is destroyed. However, note that the command context
3391 * still holds a reference to the session, thus delaying its destruction
3392 * _at least_ up to the point when that reference is released.
3394 session_destroy(session
);
3395 if (reply_context
) {
3396 reply_context
->destruction_status
= destruction_last_error
;
3397 ret
= session_add_destroy_notifier(session
,
3398 cmd_destroy_session_reply
,
3399 (void *) reply_context
);
3401 ret
= LTTNG_ERR_FATAL
;
3413 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3415 int cmd_register_consumer(struct ltt_session
*session
,
3416 enum lttng_domain_type domain
, const char *sock_path
,
3417 struct consumer_data
*cdata
)
3420 struct consumer_socket
*socket
= NULL
;
3427 case LTTNG_DOMAIN_KERNEL
:
3429 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3433 /* Can't register a consumer if there is already one */
3434 if (ksess
->consumer_fds_sent
!= 0) {
3435 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3439 sock
= lttcomm_connect_unix_sock(sock_path
);
3441 ret
= LTTNG_ERR_CONNECT_FAIL
;
3444 cdata
->cmd_sock
= sock
;
3446 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3447 if (socket
== NULL
) {
3450 PERROR("close register consumer");
3452 cdata
->cmd_sock
= -1;
3453 ret
= LTTNG_ERR_FATAL
;
3457 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3458 if (socket
->lock
== NULL
) {
3459 PERROR("zmalloc pthread mutex");
3460 ret
= LTTNG_ERR_FATAL
;
3463 pthread_mutex_init(socket
->lock
, NULL
);
3464 socket
->registered
= 1;
3467 consumer_add_socket(socket
, ksess
->consumer
);
3470 pthread_mutex_lock(&cdata
->pid_mutex
);
3472 pthread_mutex_unlock(&cdata
->pid_mutex
);
3477 /* TODO: Userspace tracing */
3478 ret
= LTTNG_ERR_UND
;
3486 consumer_destroy_socket(socket
);
3492 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3494 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3495 struct lttng_domain
**domains
)
3500 struct lttng_ht_iter iter
;
3502 if (session
->kernel_session
!= NULL
) {
3503 DBG3("Listing domains found kernel domain");
3507 if (session
->ust_session
!= NULL
) {
3508 DBG3("Listing domains found UST global domain");
3512 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3514 if (agt
->being_used
) {
3525 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3526 if (*domains
== NULL
) {
3527 ret
= LTTNG_ERR_FATAL
;
3531 if (session
->kernel_session
!= NULL
) {
3532 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3534 /* Kernel session buffer type is always GLOBAL */
3535 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3540 if (session
->ust_session
!= NULL
) {
3541 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3542 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3546 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3548 if (agt
->being_used
) {
3549 (*domains
)[index
].type
= agt
->domain
;
3550 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3560 /* Return negative value to differentiate return code */
3566 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3568 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3569 struct ltt_session
*session
, struct lttng_channel
**channels
)
3571 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3574 case LTTNG_DOMAIN_KERNEL
:
3575 if (session
->kernel_session
!= NULL
) {
3576 nb_chan
= session
->kernel_session
->channel_count
;
3578 DBG3("Number of kernel channels %zd", nb_chan
);
3580 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3584 case LTTNG_DOMAIN_UST
:
3585 if (session
->ust_session
!= NULL
) {
3587 nb_chan
= lttng_ht_get_count(
3588 session
->ust_session
->domain_global
.channels
);
3591 DBG3("Number of UST global channels %zd", nb_chan
);
3593 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3598 ret
= -LTTNG_ERR_UND
;
3603 const size_t channel_size
= sizeof(struct lttng_channel
) +
3604 sizeof(struct lttng_channel_extended
);
3605 struct lttng_channel_extended
*channel_exts
;
3607 payload_size
= nb_chan
* channel_size
;
3608 *channels
= zmalloc(payload_size
);
3609 if (*channels
== NULL
) {
3610 ret
= -LTTNG_ERR_FATAL
;
3614 channel_exts
= ((void *) *channels
) +
3615 (nb_chan
* sizeof(struct lttng_channel
));
3616 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3617 if (ret
!= LTTNG_OK
) {
3632 * Command LTTNG_LIST_EVENTS processed by the client thread.
3634 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3635 struct ltt_session
*session
, char *channel_name
,
3636 struct lttng_payload
*payload
)
3639 ssize_t nb_events
= 0;
3640 struct lttcomm_event_command_header cmd_header
= {};
3641 const size_t cmd_header_offset
= payload
->buffer
.size
;
3643 ret
= lttng_dynamic_buffer_append(
3644 &payload
->buffer
, &cmd_header
, sizeof(cmd_header
));
3646 ret
= LTTNG_ERR_NOMEM
;
3651 case LTTNG_DOMAIN_KERNEL
:
3652 if (session
->kernel_session
!= NULL
) {
3653 nb_events
= list_lttng_kernel_events(channel_name
,
3654 session
->kernel_session
, payload
);
3657 case LTTNG_DOMAIN_UST
:
3659 if (session
->ust_session
!= NULL
) {
3660 nb_events
= list_lttng_ust_global_events(channel_name
,
3661 &session
->ust_session
->domain_global
,
3666 case LTTNG_DOMAIN_LOG4J
:
3667 case LTTNG_DOMAIN_JUL
:
3668 case LTTNG_DOMAIN_PYTHON
:
3669 if (session
->ust_session
) {
3670 struct lttng_ht_iter iter
;
3674 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3675 &iter
.iter
, agt
, node
.node
) {
3676 if (agt
->domain
== domain
) {
3677 nb_events
= list_lttng_agent_events(
3686 ret
= LTTNG_ERR_UND
;
3690 ((struct lttcomm_event_command_header
*) (payload
->buffer
.data
+
3691 cmd_header_offset
))->nb_events
= (uint32_t) nb_events
;
3696 /* Return negative value to differentiate return code */
3701 * Using the session list, filled a lttng_session array to send back to the
3702 * client for session listing.
3704 * The session list lock MUST be acquired before calling this function. Use
3705 * session_lock_list() and session_unlock_list().
3707 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3708 size_t session_count
, uid_t uid
, gid_t gid
)
3712 struct ltt_session
*session
;
3713 struct ltt_session_list
*list
= session_get_list();
3714 struct lttng_session_extended
*extended
=
3715 (typeof(extended
)) (&sessions
[session_count
]);
3717 DBG("Getting all available session for UID %d GID %d",
3720 * Iterate over session list and append data after the control struct in
3723 cds_list_for_each_entry(session
, &list
->head
, list
) {
3724 if (!session_get(session
)) {
3728 * Only list the sessions the user can control.
3730 if (!session_access_ok(session
, uid
) ||
3731 session
->destroyed
) {
3732 session_put(session
);
3736 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3737 struct ltt_ust_session
*usess
= session
->ust_session
;
3739 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3740 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3741 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3742 ret
= build_network_session_path(sessions
[i
].path
,
3743 sizeof(sessions
[i
].path
), session
);
3745 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3746 session
->consumer
->dst
.session_root_path
);
3749 PERROR("snprintf session path");
3750 session_put(session
);
3754 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3755 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3756 sessions
[i
].enabled
= session
->active
;
3757 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3758 sessions
[i
].live_timer_interval
= session
->live_timer
;
3759 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3760 extended
[i
].creation_time
.is_set
= 1;
3762 session_put(session
);
3767 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3768 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3770 int cmd_data_pending(struct ltt_session
*session
)
3773 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3774 struct ltt_ust_session
*usess
= session
->ust_session
;
3778 DBG("Data pending for session %s", session
->name
);
3780 /* Session MUST be stopped to ask for data availability. */
3781 if (session
->active
) {
3782 ret
= LTTNG_ERR_SESSION_STARTED
;
3786 * If stopped, just make sure we've started before else the above call
3787 * will always send that there is data pending.
3789 * The consumer assumes that when the data pending command is received,
3790 * the trace has been started before or else no output data is written
3791 * by the streams which is a condition for data pending. So, this is
3792 * *VERY* important that we don't ask the consumer before a start
3795 if (!session
->has_been_started
) {
3801 /* A rotation is still pending, we have to wait. */
3802 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3803 DBG("Rotate still pending for session %s", session
->name
);
3808 if (ksess
&& ksess
->consumer
) {
3809 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3811 /* Data is still being extracted for the kernel. */
3816 if (usess
&& usess
->consumer
) {
3817 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3819 /* Data is still being extracted for the kernel. */
3824 /* Data is ready to be read by a viewer */
3832 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3834 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3836 int cmd_snapshot_add_output(struct ltt_session
*session
,
3837 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3840 struct snapshot_output
*new_output
;
3845 DBG("Cmd snapshot add output for session %s", session
->name
);
3848 * Can't create an output if the session is not set in no-output mode.
3850 if (session
->output_traces
) {
3851 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3855 if (session
->has_non_mmap_channel
) {
3856 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3860 /* Only one output is allowed until we have the "tee" feature. */
3861 if (session
->snapshot
.nb_output
== 1) {
3862 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3866 new_output
= snapshot_output_alloc();
3868 ret
= LTTNG_ERR_NOMEM
;
3872 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3873 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3874 &session
->snapshot
);
3876 if (ret
== -ENOMEM
) {
3877 ret
= LTTNG_ERR_NOMEM
;
3879 ret
= LTTNG_ERR_INVALID
;
3885 snapshot_add_output(&session
->snapshot
, new_output
);
3887 *id
= new_output
->id
;
3894 snapshot_output_destroy(new_output
);
3900 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3902 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3904 int cmd_snapshot_del_output(struct ltt_session
*session
,
3905 const struct lttng_snapshot_output
*output
)
3908 struct snapshot_output
*sout
= NULL
;
3916 * Permission denied to create an output if the session is not
3917 * set in no output mode.
3919 if (session
->output_traces
) {
3920 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3925 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3927 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3928 } else if (*output
->name
!= '\0') {
3929 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3931 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3934 ret
= LTTNG_ERR_INVALID
;
3938 snapshot_delete_output(&session
->snapshot
, sout
);
3939 snapshot_output_destroy(sout
);
3948 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3950 * If no output is available, outputs is untouched and 0 is returned.
3952 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3954 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3955 struct lttng_snapshot_output
**outputs
)
3958 struct lttng_snapshot_output
*list
= NULL
;
3959 struct lttng_ht_iter iter
;
3960 struct snapshot_output
*output
;
3965 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3968 * Permission denied to create an output if the session is not
3969 * set in no output mode.
3971 if (session
->output_traces
) {
3972 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3976 if (session
->snapshot
.nb_output
== 0) {
3981 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3983 ret
= -LTTNG_ERR_NOMEM
;
3987 /* Copy list from session to the new list object. */
3989 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3990 output
, node
.node
) {
3991 assert(output
->consumer
);
3992 list
[idx
].id
= output
->id
;
3993 list
[idx
].max_size
= output
->max_size
;
3994 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3995 sizeof(list
[idx
].name
))) {
3996 ret
= -LTTNG_ERR_INVALID
;
3999 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4000 if (lttng_strncpy(list
[idx
].ctrl_url
,
4001 output
->consumer
->dst
.session_root_path
,
4002 sizeof(list
[idx
].ctrl_url
))) {
4003 ret
= -LTTNG_ERR_INVALID
;
4008 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4009 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4011 ret
= -LTTNG_ERR_NOMEM
;
4016 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4017 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4019 ret
= -LTTNG_ERR_NOMEM
;
4028 ret
= session
->snapshot
.nb_output
;
4037 * Check if we can regenerate the metadata for this session.
4038 * Only kernel, UST per-uid and non-live sessions are supported.
4040 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4043 int check_regenerate_metadata_support(struct ltt_session
*session
)
4049 if (session
->live_timer
!= 0) {
4050 ret
= LTTNG_ERR_LIVE_SESSION
;
4053 if (!session
->active
) {
4054 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4057 if (session
->ust_session
) {
4058 switch (session
->ust_session
->buffer_type
) {
4059 case LTTNG_BUFFER_PER_UID
:
4061 case LTTNG_BUFFER_PER_PID
:
4062 ret
= LTTNG_ERR_PER_PID_SESSION
;
4066 ret
= LTTNG_ERR_UNK
;
4070 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4071 session
->consumer
->relay_minor_version
< 8) {
4072 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4082 int clear_metadata_file(int fd
)
4087 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4088 if (lseek_ret
< 0) {
4094 ret
= ftruncate(fd
, 0);
4096 PERROR("ftruncate");
4105 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4108 struct buffer_reg_uid
*uid_reg
= NULL
;
4109 struct buffer_reg_session
*session_reg
= NULL
;
4112 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4113 struct ust_registry_session
*registry
;
4114 struct ust_registry_channel
*chan
;
4115 struct lttng_ht_iter iter_chan
;
4117 session_reg
= uid_reg
->registry
;
4118 registry
= session_reg
->reg
.ust
;
4120 pthread_mutex_lock(®istry
->lock
);
4121 registry
->metadata_len_sent
= 0;
4122 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4123 registry
->metadata_len
= 0;
4124 registry
->metadata_version
++;
4125 if (registry
->metadata_fd
> 0) {
4126 /* Clear the metadata file's content. */
4127 ret
= clear_metadata_file(registry
->metadata_fd
);
4129 pthread_mutex_unlock(®istry
->lock
);
4134 ret
= ust_metadata_session_statedump(registry
, NULL
,
4135 registry
->major
, registry
->minor
);
4137 pthread_mutex_unlock(®istry
->lock
);
4138 ERR("Failed to generate session metadata (err = %d)",
4142 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4144 struct ust_registry_event
*event
;
4145 struct lttng_ht_iter iter_event
;
4147 ret
= ust_metadata_channel_statedump(registry
, chan
);
4149 pthread_mutex_unlock(®istry
->lock
);
4150 ERR("Failed to generate channel metadata "
4154 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4156 ret
= ust_metadata_event_statedump(registry
,
4159 pthread_mutex_unlock(®istry
->lock
);
4160 ERR("Failed to generate event metadata "
4166 pthread_mutex_unlock(®istry
->lock
);
4175 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4177 * Ask the consumer to truncate the existing metadata file(s) and
4178 * then regenerate the metadata. Live and per-pid sessions are not
4179 * supported and return an error.
4181 * Return 0 on success or else a LTTNG_ERR code.
4183 int cmd_regenerate_metadata(struct ltt_session
*session
)
4189 ret
= check_regenerate_metadata_support(session
);
4194 if (session
->kernel_session
) {
4195 ret
= kernctl_session_regenerate_metadata(
4196 session
->kernel_session
->fd
);
4198 ERR("Failed to regenerate the kernel metadata");
4203 if (session
->ust_session
) {
4204 ret
= ust_regenerate_metadata(session
->ust_session
);
4206 ERR("Failed to regenerate the UST metadata");
4210 DBG("Cmd metadata regenerate for session %s", session
->name
);
4218 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4220 * Ask the tracer to regenerate a new statedump.
4222 * Return 0 on success or else a LTTNG_ERR code.
4224 int cmd_regenerate_statedump(struct ltt_session
*session
)
4230 if (!session
->active
) {
4231 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4235 if (session
->kernel_session
) {
4236 ret
= kernctl_session_regenerate_statedump(
4237 session
->kernel_session
->fd
);
4239 * Currently, the statedump in kernel can only fail if out
4243 if (ret
== -ENOMEM
) {
4244 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4246 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4248 ERR("Failed to regenerate the kernel statedump");
4253 if (session
->ust_session
) {
4254 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4256 * Currently, the statedump in UST always returns 0.
4259 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4260 ERR("Failed to regenerate the UST statedump");
4264 DBG("Cmd regenerate statedump for session %s", session
->name
);
4271 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4272 struct notification_thread_handle
*notification_thread
,
4273 struct lttng_trigger
**return_trigger
)
4277 ssize_t sock_recv_len
;
4278 struct lttng_trigger
*trigger
= NULL
;
4279 struct lttng_payload trigger_payload
;
4280 struct lttng_credentials cmd_creds
= {
4281 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
4282 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
4285 lttng_payload_init(&trigger_payload
);
4286 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
4287 ret
= lttng_dynamic_buffer_set_size(
4288 &trigger_payload
.buffer
, trigger_len
);
4290 ret
= LTTNG_ERR_NOMEM
;
4294 sock_recv_len
= lttcomm_recv_unix_sock(
4295 sock
, trigger_payload
.buffer
.data
, trigger_len
);
4296 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4297 ERR("Failed to receive \"register trigger\" command payload");
4298 /* TODO: should this be a new error enum ? */
4299 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4303 /* Receive fds, if any. */
4304 if (cmd_ctx
->lsm
.fd_count
> 0) {
4305 ret
= lttcomm_recv_payload_fds_unix_sock(
4306 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
4307 if (ret
> 0 && ret
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
4308 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
4310 } else if (ret
<= 0) {
4311 ret
= LTTNG_ERR_FATAL
;
4316 /* Deserialize trigger. */
4318 struct lttng_payload_view view
=
4319 lttng_payload_view_from_payload(
4320 &trigger_payload
, 0, -1);
4322 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
4324 ERR("Invalid trigger payload received in \"register trigger\" command");
4325 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4331 * Validate the trigger credentials against the command credentials.
4332 * Only the root user can register a trigger with non-matching
4335 if (!lttng_credentials_is_equal_uid(
4336 lttng_trigger_get_credentials(trigger
),
4338 if (lttng_credentials_get_uid(&cmd_creds
) != 0) {
4339 ERR("Trigger credentials do not match the command credentials");
4340 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4346 * A reference to the trigger is acquired by the notification thread.
4347 * It is safe to return the same trigger to the caller since it the
4348 * other user holds a reference.
4350 * The trigger is modified during the execution of the
4351 * "register trigger" command. However, by the time the command returns,
4352 * it is safe to use without any locking as its properties are
4355 ret
= notification_thread_command_register_trigger(notification_thread
,
4357 if (ret
!= LTTNG_OK
) {
4358 goto end_notification_thread
;
4361 /* Return an updated trigger to the client. */
4362 *return_trigger
= trigger
;
4364 end_notification_thread
:
4365 /* Ownership of trigger was transferred. */
4368 lttng_trigger_destroy(trigger
);
4369 lttng_payload_reset(&trigger_payload
);
4373 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4374 struct notification_thread_handle
*notification_thread
)
4378 ssize_t sock_recv_len
;
4379 struct lttng_trigger
*trigger
= NULL
;
4380 struct lttng_payload trigger_payload
;
4381 struct lttng_credentials cmd_creds
= {
4382 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
4383 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
4386 lttng_payload_init(&trigger_payload
);
4387 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
4388 ret
= lttng_dynamic_buffer_set_size(
4389 &trigger_payload
.buffer
, trigger_len
);
4391 ret
= LTTNG_ERR_NOMEM
;
4395 sock_recv_len
= lttcomm_recv_unix_sock(
4396 sock
, trigger_payload
.buffer
.data
, trigger_len
);
4397 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4398 ERR("Failed to receive \"unregister trigger\" command payload");
4399 /* TODO: should this be a new error enum ? */
4400 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4404 /* Receive fds, if any. */
4405 if (cmd_ctx
->lsm
.fd_count
> 0) {
4406 ret
= lttcomm_recv_payload_fds_unix_sock(
4407 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
4408 if (ret
> 0 && ret
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
4409 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
4411 } else if (ret
<= 0) {
4412 ret
= LTTNG_ERR_FATAL
;
4418 struct lttng_payload_view view
=
4419 lttng_payload_view_from_payload(
4420 &trigger_payload
, 0, -1);
4422 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
4424 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4425 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4431 * Validate the trigger credentials against the command credentials.
4432 * Only the root user can unregister a trigger with non-matching
4435 if (!lttng_credentials_is_equal_uid(
4436 lttng_trigger_get_credentials(trigger
),
4438 if (lttng_credentials_get_uid(&cmd_creds
) != 0) {
4439 ERR("Trigger credentials do not match the command credentials");
4440 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4445 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4448 lttng_trigger_destroy(trigger
);
4449 lttng_payload_reset(&trigger_payload
);
4453 int cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4454 struct notification_thread_handle
*notification_thread
,
4455 struct lttng_triggers
**return_triggers
)
4458 enum lttng_error_code ret_code
;
4459 struct lttng_triggers
*triggers
= NULL
;
4461 /* Get the set of triggers from the notification thread. */
4462 ret_code
= notification_thread_command_list_triggers(
4463 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4464 if (ret_code
!= LTTNG_OK
) {
4469 *return_triggers
= triggers
;
4473 lttng_triggers_destroy(triggers
);
4477 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4478 * snapshot output is *not* set with a remote destination.
4480 * Return LTTNG_OK on success or a LTTNG_ERR code.
4482 static enum lttng_error_code
set_relayd_for_snapshot(
4483 struct consumer_output
*output
,
4484 const struct ltt_session
*session
)
4486 enum lttng_error_code status
= LTTNG_OK
;
4487 struct lttng_ht_iter iter
;
4488 struct consumer_socket
*socket
;
4489 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4490 const char *base_path
;
4495 DBG2("Set relayd object from snapshot output");
4497 if (session
->current_trace_chunk
) {
4498 enum lttng_trace_chunk_status chunk_status
=
4499 lttng_trace_chunk_get_id(
4500 session
->current_trace_chunk
,
4501 ¤t_chunk_id
.value
);
4503 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4504 current_chunk_id
.is_set
= true;
4506 ERR("Failed to get current trace chunk id");
4507 status
= LTTNG_ERR_UNK
;
4512 /* Ignore if snapshot consumer output is not network. */
4513 if (output
->type
!= CONSUMER_DST_NET
) {
4518 * The snapshot record URI base path overrides the session
4521 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4522 base_path
= output
->dst
.net
.control
.subdir
;
4524 base_path
= session
->base_path
;
4528 * For each consumer socket, create and send the relayd object of the
4532 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4533 socket
, node
.node
) {
4534 pthread_mutex_lock(socket
->lock
);
4535 status
= send_consumer_relayd_sockets(0, session
->id
,
4537 session
->name
, session
->hostname
,
4539 session
->live_timer
,
4540 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4541 session
->creation_time
,
4542 session
->name_contains_creation_time
);
4543 pthread_mutex_unlock(socket
->lock
);
4544 if (status
!= LTTNG_OK
) {
4556 * Record a kernel snapshot.
4558 * Return LTTNG_OK on success or a LTTNG_ERR code.
4560 static enum lttng_error_code
record_kernel_snapshot(
4561 struct ltt_kernel_session
*ksess
,
4562 const struct consumer_output
*output
,
4563 const struct ltt_session
*session
,
4564 int wait
, uint64_t nb_packets_per_stream
)
4566 enum lttng_error_code status
;
4572 status
= kernel_snapshot_record(
4573 ksess
, output
, wait
, nb_packets_per_stream
);
4578 * Record a UST snapshot.
4580 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4582 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4583 const struct consumer_output
*output
,
4584 const struct ltt_session
*session
,
4585 int wait
, uint64_t nb_packets_per_stream
)
4587 enum lttng_error_code status
;
4593 status
= ust_app_snapshot_record(
4594 usess
, output
, wait
, nb_packets_per_stream
);
4599 uint64_t get_session_size_one_more_packet_per_stream(
4600 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4602 uint64_t tot_size
= 0;
4604 if (session
->kernel_session
) {
4605 struct ltt_kernel_channel
*chan
;
4606 const struct ltt_kernel_session
*ksess
=
4607 session
->kernel_session
;
4609 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4610 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4612 * Don't take channel into account if we
4613 * already grab all its packets.
4617 tot_size
+= chan
->channel
->attr
.subbuf_size
4618 * chan
->stream_count
;
4622 if (session
->ust_session
) {
4623 const struct ltt_ust_session
*usess
= session
->ust_session
;
4625 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4633 * Calculate the number of packets we can grab from each stream that
4634 * fits within the overall snapshot max size.
4636 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4637 * the number of packets per stream.
4639 * TODO: this approach is not perfect: we consider the worse case
4640 * (packet filling the sub-buffers) as an upper bound, but we could do
4641 * better if we do this calculation while we actually grab the packet
4642 * content: we would know how much padding we don't actually store into
4645 * This algorithm is currently bounded by the number of packets per
4648 * Since we call this algorithm before actually grabbing the data, it's
4649 * an approximation: for instance, applications could appear/disappear
4650 * in between this call and actually grabbing data.
4653 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4657 uint64_t cur_nb_packets
= 0;
4660 return 0; /* Infinite */
4663 size_left
= max_size
;
4665 uint64_t one_more_packet_tot_size
;
4667 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4668 session
, cur_nb_packets
);
4669 if (!one_more_packet_tot_size
) {
4670 /* We are already grabbing all packets. */
4673 size_left
-= one_more_packet_tot_size
;
4674 if (size_left
< 0) {
4679 if (!cur_nb_packets
&& size_left
!= max_size
) {
4680 /* Not enough room to grab one packet of each stream, error. */
4683 return cur_nb_packets
;
4687 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4688 const struct snapshot_output
*snapshot_output
, int wait
)
4690 int64_t nb_packets_per_stream
;
4691 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4693 enum lttng_error_code ret_code
= LTTNG_OK
;
4694 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4695 struct consumer_output
*original_ust_consumer_output
= NULL
;
4696 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4697 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4698 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4700 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4702 snapshot_output
->name
,
4703 snapshot_output
->datetime
,
4704 snapshot_output
->nb_snapshot
);
4705 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4706 ERR("Failed to format snapshot name");
4707 ret_code
= LTTNG_ERR_INVALID
;
4710 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4711 snapshot_output
->name
, session
->name
,
4712 snapshot_chunk_name
);
4713 if (!session
->kernel_session
&& !session
->ust_session
) {
4714 ERR("Failed to record snapshot as no channels exist");
4715 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4719 if (session
->kernel_session
) {
4720 original_kernel_consumer_output
=
4721 session
->kernel_session
->consumer
;
4722 snapshot_kernel_consumer_output
=
4723 consumer_copy_output(snapshot_output
->consumer
);
4724 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
4725 snapshot_chunk_name
);
4726 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4727 original_kernel_consumer_output
);
4729 ERR("Failed to copy consumer sockets from snapshot output configuration");
4730 ret_code
= LTTNG_ERR_NOMEM
;
4733 ret_code
= set_relayd_for_snapshot(
4734 snapshot_kernel_consumer_output
, session
);
4735 if (ret_code
!= LTTNG_OK
) {
4736 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4739 session
->kernel_session
->consumer
=
4740 snapshot_kernel_consumer_output
;
4742 if (session
->ust_session
) {
4743 original_ust_consumer_output
= session
->ust_session
->consumer
;
4744 snapshot_ust_consumer_output
=
4745 consumer_copy_output(snapshot_output
->consumer
);
4746 strcpy(snapshot_ust_consumer_output
->chunk_path
,
4747 snapshot_chunk_name
);
4748 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4749 original_ust_consumer_output
);
4751 ERR("Failed to copy consumer sockets from snapshot output configuration");
4752 ret_code
= LTTNG_ERR_NOMEM
;
4755 ret_code
= set_relayd_for_snapshot(
4756 snapshot_ust_consumer_output
, session
);
4757 if (ret_code
!= LTTNG_OK
) {
4758 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4761 session
->ust_session
->consumer
=
4762 snapshot_ust_consumer_output
;
4765 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4766 snapshot_kernel_consumer_output
?:
4767 snapshot_ust_consumer_output
,
4768 consumer_output_get_base_path(
4769 snapshot_output
->consumer
),
4770 snapshot_chunk_name
);
4771 if (!snapshot_trace_chunk
) {
4772 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4774 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4777 assert(!session
->current_trace_chunk
);
4778 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4779 lttng_trace_chunk_put(snapshot_trace_chunk
);
4780 snapshot_trace_chunk
= NULL
;
4782 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4784 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4788 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4789 snapshot_output
->max_size
);
4790 if (nb_packets_per_stream
< 0) {
4791 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4792 goto error_close_trace_chunk
;
4795 if (session
->kernel_session
) {
4796 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4797 snapshot_kernel_consumer_output
, session
,
4798 wait
, nb_packets_per_stream
);
4799 if (ret_code
!= LTTNG_OK
) {
4800 goto error_close_trace_chunk
;
4804 if (session
->ust_session
) {
4805 ret_code
= record_ust_snapshot(session
->ust_session
,
4806 snapshot_ust_consumer_output
, session
,
4807 wait
, nb_packets_per_stream
);
4808 if (ret_code
!= LTTNG_OK
) {
4809 goto error_close_trace_chunk
;
4813 error_close_trace_chunk
:
4814 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
4815 ERR("Failed to release the current trace chunk of session \"%s\"",
4817 ret_code
= LTTNG_ERR_UNK
;
4820 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
4821 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
4823 * Don't goto end; make sure the chunk is closed for the session
4824 * to allow future snapshots.
4826 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4828 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4831 if (original_ust_consumer_output
) {
4832 session
->ust_session
->consumer
= original_ust_consumer_output
;
4834 if (original_kernel_consumer_output
) {
4835 session
->kernel_session
->consumer
=
4836 original_kernel_consumer_output
;
4838 consumer_output_put(snapshot_ust_consumer_output
);
4839 consumer_output_put(snapshot_kernel_consumer_output
);
4844 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4846 * The wait parameter is ignored so this call always wait for the snapshot to
4847 * complete before returning.
4849 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4851 int cmd_snapshot_record(struct ltt_session
*session
,
4852 const struct lttng_snapshot_output
*output
, int wait
)
4854 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4856 unsigned int snapshot_success
= 0;
4858 struct snapshot_output
*tmp_output
= NULL
;
4863 DBG("Cmd snapshot record for session %s", session
->name
);
4865 /* Get the datetime for the snapshot output directory. */
4866 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4869 cmd_ret
= LTTNG_ERR_INVALID
;
4874 * Permission denied to create an output if the session is not
4875 * set in no output mode.
4877 if (session
->output_traces
) {
4878 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4882 /* The session needs to be started at least once. */
4883 if (!session
->has_been_started
) {
4884 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4888 /* Use temporary output for the session. */
4889 if (*output
->ctrl_url
!= '\0') {
4890 tmp_output
= snapshot_output_alloc();
4892 cmd_ret
= LTTNG_ERR_NOMEM
;
4896 ret
= snapshot_output_init(session
, output
->max_size
,
4898 output
->ctrl_url
, output
->data_url
,
4902 if (ret
== -ENOMEM
) {
4903 cmd_ret
= LTTNG_ERR_NOMEM
;
4905 cmd_ret
= LTTNG_ERR_INVALID
;
4909 /* Use the global session count for the temporary snapshot. */
4910 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
4912 /* Use the global datetime */
4913 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
4914 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
4915 if (cmd_ret
!= LTTNG_OK
) {
4918 snapshot_success
= 1;
4920 struct snapshot_output
*sout
;
4921 struct lttng_ht_iter iter
;
4924 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4925 &iter
.iter
, sout
, node
.node
) {
4926 struct snapshot_output output_copy
;
4929 * Make a local copy of the output and override output
4930 * parameters with those provided as part of the
4933 memcpy(&output_copy
, sout
, sizeof(output_copy
));
4935 if (output
->max_size
!= (uint64_t) -1ULL) {
4936 output_copy
.max_size
= output
->max_size
;
4939 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4940 memcpy(output_copy
.datetime
, datetime
,
4943 /* Use temporary name. */
4944 if (*output
->name
!= '\0') {
4945 if (lttng_strncpy(output_copy
.name
,
4947 sizeof(output_copy
.name
))) {
4948 cmd_ret
= LTTNG_ERR_INVALID
;
4954 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
4955 if (cmd_ret
!= LTTNG_OK
) {
4959 snapshot_success
= 1;
4964 if (snapshot_success
) {
4965 session
->snapshot
.nb_snapshot
++;
4967 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4972 snapshot_output_destroy(tmp_output
);
4978 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4980 int cmd_set_session_shm_path(struct ltt_session
*session
,
4981 const char *shm_path
)
4987 * Can only set shm path before session is started.
4989 if (session
->has_been_started
) {
4990 return LTTNG_ERR_SESSION_STARTED
;
4993 strncpy(session
->shm_path
, shm_path
,
4994 sizeof(session
->shm_path
));
4995 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5001 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5003 * Ask the consumer to rotate the session output directory.
5004 * The session lock must be held.
5006 * Returns LTTNG_OK on success or else a negative LTTng error code.
5008 int cmd_rotate_session(struct ltt_session
*session
,
5009 struct lttng_rotate_session_return
*rotate_return
,
5010 bool quiet_rotation
,
5011 enum lttng_trace_chunk_command_type command
)
5014 uint64_t ongoing_rotation_chunk_id
;
5015 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5016 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5017 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5018 enum lttng_trace_chunk_status chunk_status
;
5019 bool failed_to_rotate
= false;
5020 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5024 if (!session
->has_been_started
) {
5025 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5030 * Explicit rotation is not supported for live sessions.
5031 * However, live sessions can perform a quiet rotation on
5033 * Rotation is not supported for snapshot traces (no output).
5035 if ((!quiet_rotation
&& session
->live_timer
) ||
5036 !session
->output_traces
) {
5037 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5041 /* Unsupported feature in lttng-relayd before 2.11. */
5042 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5043 (session
->consumer
->relay_major_version
== 2 &&
5044 session
->consumer
->relay_minor_version
< 11)) {
5045 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5049 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5050 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5051 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5055 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5056 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5058 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5063 * After a stop, we only allow one rotation to occur, the other ones are
5064 * useless until a new start.
5066 if (session
->rotated_after_last_stop
) {
5067 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5069 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5074 * After a stop followed by a clear, disallow following rotations a they would
5075 * generate empty chunks.
5077 if (session
->cleared_after_last_stop
) {
5078 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5080 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5084 if (session
->active
) {
5085 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5087 if (!new_trace_chunk
) {
5088 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5094 * The current trace chunk becomes the chunk being archived.
5096 * After this point, "chunk_being_archived" must absolutely
5097 * be closed on the consumer(s), otherwise it will never be
5098 * cleaned-up, which will result in a leak.
5100 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5101 &chunk_being_archived
);
5103 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5107 if (session
->kernel_session
) {
5108 cmd_ret
= kernel_rotate_session(session
);
5109 if (cmd_ret
!= LTTNG_OK
) {
5110 failed_to_rotate
= true;
5111 rotation_fail_code
= cmd_ret
;
5114 if (session
->ust_session
) {
5115 cmd_ret
= ust_app_rotate_session(session
);
5116 if (cmd_ret
!= LTTNG_OK
) {
5117 failed_to_rotate
= true;
5118 rotation_fail_code
= cmd_ret
;
5122 if (!session
->active
) {
5123 session
->rotated_after_last_stop
= true;
5126 if (!chunk_being_archived
) {
5127 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5129 if (failed_to_rotate
) {
5130 cmd_ret
= rotation_fail_code
;
5137 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5138 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5139 &ongoing_rotation_chunk_id
);
5140 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5142 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5143 command
, session
->last_chunk_path
);
5145 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5149 if (failed_to_rotate
) {
5150 cmd_ret
= rotation_fail_code
;
5154 session
->quiet_rotation
= quiet_rotation
;
5155 ret
= timer_session_rotation_pending_check_start(session
,
5156 DEFAULT_ROTATE_PENDING_TIMER
);
5158 cmd_ret
= LTTNG_ERR_UNK
;
5162 if (rotate_return
) {
5163 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5166 session
->chunk_being_archived
= chunk_being_archived
;
5167 chunk_being_archived
= NULL
;
5168 if (!quiet_rotation
) {
5169 ret
= notification_thread_command_session_rotation_ongoing(
5170 notification_thread_handle
,
5171 session
->name
, session
->uid
, session
->gid
,
5172 ongoing_rotation_chunk_id
);
5173 if (ret
!= LTTNG_OK
) {
5174 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5180 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5181 session
->name
, ongoing_rotation_chunk_id
);
5183 lttng_trace_chunk_put(new_trace_chunk
);
5184 lttng_trace_chunk_put(chunk_being_archived
);
5185 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5188 if (session_reset_rotation_state(session
,
5189 LTTNG_ROTATION_STATE_ERROR
)) {
5190 ERR("Failed to reset rotation state of session \"%s\"",
5197 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5199 * Check if the session has finished its rotation.
5201 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5203 int cmd_rotate_get_info(struct ltt_session
*session
,
5204 struct lttng_rotation_get_info_return
*info_return
,
5205 uint64_t rotation_id
)
5207 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5208 enum lttng_rotation_state rotation_state
;
5210 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5211 session
->most_recent_chunk_id
.value
);
5213 if (session
->chunk_being_archived
) {
5214 enum lttng_trace_chunk_status chunk_status
;
5217 chunk_status
= lttng_trace_chunk_get_id(
5218 session
->chunk_being_archived
,
5220 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5222 rotation_state
= rotation_id
== chunk_id
?
5223 LTTNG_ROTATION_STATE_ONGOING
:
5224 LTTNG_ROTATION_STATE_EXPIRED
;
5226 if (session
->last_archived_chunk_id
.is_set
&&
5227 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5228 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5230 rotation_state
= session
->rotation_state
;
5234 switch (rotation_state
) {
5235 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5236 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5239 case LTTNG_ROTATION_STATE_EXPIRED
:
5240 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5241 rotation_id
, session
->name
);
5243 case LTTNG_ROTATION_STATE_ONGOING
:
5244 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5245 rotation_id
, session
->name
);
5247 case LTTNG_ROTATION_STATE_COMPLETED
:
5251 char *current_tracing_path_reply
;
5252 size_t current_tracing_path_reply_len
;
5254 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5255 rotation_id
, session
->name
);
5257 switch (session_get_consumer_destination_type(session
)) {
5258 case CONSUMER_DST_LOCAL
:
5259 current_tracing_path_reply
=
5260 info_return
->location
.local
.absolute_path
;
5261 current_tracing_path_reply_len
=
5262 sizeof(info_return
->location
.local
.absolute_path
);
5263 info_return
->location_type
=
5264 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5265 fmt_ret
= asprintf(&chunk_path
,
5266 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5267 session_get_base_path(session
),
5268 session
->last_archived_chunk_name
);
5269 if (fmt_ret
== -1) {
5270 PERROR("Failed to format the path of the last archived trace chunk");
5271 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5272 cmd_ret
= LTTNG_ERR_UNK
;
5276 case CONSUMER_DST_NET
:
5278 uint16_t ctrl_port
, data_port
;
5280 current_tracing_path_reply
=
5281 info_return
->location
.relay
.relative_path
;
5282 current_tracing_path_reply_len
=
5283 sizeof(info_return
->location
.relay
.relative_path
);
5284 /* Currently the only supported relay protocol. */
5285 info_return
->location
.relay
.protocol
=
5286 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5288 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5289 session_get_net_consumer_hostname(session
),
5290 sizeof(info_return
->location
.relay
.host
));
5292 ERR("Failed to copy host name to rotate_get_info reply");
5293 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5294 cmd_ret
= LTTNG_ERR_SET_URL
;
5298 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5299 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5300 info_return
->location
.relay
.ports
.data
= data_port
;
5301 info_return
->location_type
=
5302 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5303 chunk_path
= strdup(session
->last_chunk_path
);
5305 ERR("Failed to allocate the path of the last archived trace chunk");
5306 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5307 cmd_ret
= LTTNG_ERR_UNK
;
5316 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5317 chunk_path
, current_tracing_path_reply_len
);
5320 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5321 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5322 cmd_ret
= LTTNG_ERR_UNK
;
5328 case LTTNG_ROTATION_STATE_ERROR
:
5329 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5330 rotation_id
, session
->name
);
5338 info_return
->status
= (int32_t) rotation_state
;
5343 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5345 * Configure the automatic rotation parameters.
5346 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5347 * 'activate' to false means deactivate the rotation schedule and validate that
5348 * 'new_value' has the same value as the currently active value.
5350 * Return 0 on success or else a positive LTTNG_ERR code.
5352 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5353 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5355 struct notification_thread_handle
*notification_thread_handle
)
5358 uint64_t *parameter_value
;
5362 DBG("Cmd rotate set schedule session %s", session
->name
);
5364 if (session
->live_timer
|| !session
->output_traces
) {
5365 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5366 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5370 switch (schedule_type
) {
5371 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5372 parameter_value
= &session
->rotate_size
;
5374 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5375 parameter_value
= &session
->rotate_timer_period
;
5376 if (new_value
>= UINT_MAX
) {
5377 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5378 new_value
, UINT_MAX
);
5379 ret
= LTTNG_ERR_INVALID
;
5384 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5385 ret
= LTTNG_ERR_INVALID
;
5389 /* Improper use of the API. */
5390 if (new_value
== -1ULL) {
5391 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5392 ret
= LTTNG_ERR_INVALID
;
5397 * As indicated in struct ltt_session's comments, a value of == 0 means
5398 * this schedule rotation type is not in use.
5400 * Reject the command if we were asked to activate a schedule that was
5403 if (activate
&& *parameter_value
!= 0) {
5404 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5405 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5410 * Reject the command if we were asked to deactivate a schedule that was
5413 if (!activate
&& *parameter_value
== 0) {
5414 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5415 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5420 * Reject the command if we were asked to deactivate a schedule that
5423 if (!activate
&& *parameter_value
!= new_value
) {
5424 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5425 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5429 *parameter_value
= activate
? new_value
: 0;
5431 switch (schedule_type
) {
5432 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5433 if (activate
&& session
->active
) {
5435 * Only start the timer if the session is active,
5436 * otherwise it will be started when the session starts.
5438 ret
= timer_session_rotation_schedule_timer_start(
5439 session
, new_value
);
5441 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5442 ret
= LTTNG_ERR_UNK
;
5446 ret
= timer_session_rotation_schedule_timer_stop(
5449 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5450 ret
= LTTNG_ERR_UNK
;
5455 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5457 ret
= subscribe_session_consumed_size_rotation(session
,
5458 new_value
, notification_thread_handle
);
5460 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5461 ret
= LTTNG_ERR_UNK
;
5465 ret
= unsubscribe_session_consumed_size_rotation(session
,
5466 notification_thread_handle
);
5468 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5469 ret
= LTTNG_ERR_UNK
;
5476 /* Would have been caught before. */
5488 /* Wait for a given path to be removed before continuing. */
5489 static enum lttng_error_code
wait_on_path(void *path_data
)
5491 const char *shm_path
= path_data
;
5493 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5499 ret
= stat(shm_path
, &st
);
5501 if (errno
!= ENOENT
) {
5502 PERROR("stat() returned an error while checking for the existence of the shm path");
5504 DBG("shm path no longer exists, completing the destruction of session");
5508 if (!S_ISDIR(st
.st_mode
)) {
5509 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5514 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5520 * Returns a pointer to a handler to run on completion of a command.
5521 * Returns NULL if no handler has to be run for the last command executed.
5523 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5525 struct cmd_completion_handler
*handler
= current_completion_handler
;
5527 current_completion_handler
= NULL
;
5532 * Init command subsystem.
5537 * Set network sequence index to 1 for streams to match a relayd
5538 * socket on the consumer side.
5540 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5541 relayd_net_seq_idx
= 1;
5542 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5544 DBG("Command subsystem initialized");