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 ret
= lttng_strncpy(event
.name
, agent_event
->name
, sizeof(event
.name
));
493 /* Internal error, invalid name. */
494 ERR("Invalid event name while listing agent events: '%s' exceeds the maximal allowed length of %zu bytes",
495 agent_event
->name
, sizeof(event
.name
));
496 ret
= -LTTNG_ERR_UNK
;
500 ret
= lttng_dynamic_buffer_append(
501 &payload
->buffer
, &event
, sizeof(event
));
503 ERR("Failed to append event to payload");
504 ret
= -LTTNG_ERR_NOMEM
;
511 cds_lfht_for_each_entry (
512 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
513 /* Append extended info. */
514 ret
= append_extended_info(agent_event
->filter_expression
, NULL
,
517 ERR("Failed to append extended event info to payload");
518 ret
= -LTTNG_ERR_NOMEM
;
530 * Create a list of ust global domain events.
532 static int list_lttng_ust_global_events(char *channel_name
,
533 struct ltt_ust_domain_global
*ust_global
,
534 struct lttng_payload
*payload
)
537 unsigned int nb_events
= 0;
538 struct lttng_ht_iter iter
;
539 const struct lttng_ht_node_str
*node
;
540 const struct ltt_ust_channel
*uchan
;
541 const struct ltt_ust_event
*uevent
;
543 DBG("Listing UST global events for channel %s", channel_name
);
547 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
548 node
= lttng_ht_iter_get_node_str(&iter
);
550 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
554 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
556 DBG3("Listing UST global events");
558 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
559 struct lttng_event event
= {};
561 if (uevent
->internal
) {
565 ret
= lttng_strncpy(event
.name
, uevent
->attr
.name
, sizeof(event
.name
));
567 /* Internal error, invalid name. */
568 ERR("Invalid event name while listing user space tracer events: '%s' exceeds the maximal allowed length of %zu bytes",
569 uevent
->attr
.name
, sizeof(event
.name
));
570 ret
= -LTTNG_ERR_UNK
;
574 event
.enabled
= uevent
->enabled
;
576 switch (uevent
->attr
.instrumentation
) {
577 case LTTNG_UST_TRACEPOINT
:
578 event
.type
= LTTNG_EVENT_TRACEPOINT
;
580 case LTTNG_UST_PROBE
:
581 event
.type
= LTTNG_EVENT_PROBE
;
583 case LTTNG_UST_FUNCTION
:
584 event
.type
= LTTNG_EVENT_FUNCTION
;
588 event
.loglevel
= uevent
->attr
.loglevel
;
589 switch (uevent
->attr
.loglevel_type
) {
590 case LTTNG_UST_LOGLEVEL_ALL
:
591 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
593 case LTTNG_UST_LOGLEVEL_RANGE
:
594 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
596 case LTTNG_UST_LOGLEVEL_SINGLE
:
597 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
601 if (uevent
->filter
) {
605 if (uevent
->exclusion
) {
609 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &event
, sizeof(event
));
611 ERR("Failed to append event to payload");
612 ret
= -LTTNG_ERR_NOMEM
;
619 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
620 /* Append extended info. */
621 ret
= append_extended_info(uevent
->filter_expression
,
622 uevent
->exclusion
, NULL
, payload
);
624 ERR("Failed to append extended event info to payload");
625 ret
= -LTTNG_ERR_FATAL
;
637 * Fill lttng_event array of all kernel events in the channel.
639 static int list_lttng_kernel_events(char *channel_name
,
640 struct ltt_kernel_session
*kernel_session
,
641 struct lttng_payload
*payload
)
644 unsigned int nb_event
;
645 const struct ltt_kernel_event
*kevent
;
646 const struct ltt_kernel_channel
*kchan
;
648 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
650 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
654 nb_event
= kchan
->event_count
;
656 DBG("Listing events for channel %s", kchan
->channel
->name
);
658 /* Kernel channels */
659 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
660 struct lttng_event event
= {};
662 ret
= lttng_strncpy(event
.name
, kevent
->event
->name
, sizeof(event
.name
));
664 /* Internal error, invalid name. */
665 ERR("Invalid event name while listing kernel events: '%s' exceeds the maximal allowed length of %zu bytes",
668 ret
= -LTTNG_ERR_UNK
;
672 event
.enabled
= kevent
->enabled
;
673 event
.filter
= (unsigned char) !!kevent
->filter_expression
;
675 switch (kevent
->event
->instrumentation
) {
676 case LTTNG_KERNEL_TRACEPOINT
:
677 event
.type
= LTTNG_EVENT_TRACEPOINT
;
679 case LTTNG_KERNEL_KRETPROBE
:
680 event
.type
= LTTNG_EVENT_FUNCTION
;
681 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
682 sizeof(struct lttng_kernel_kprobe
));
684 case LTTNG_KERNEL_KPROBE
:
685 event
.type
= LTTNG_EVENT_PROBE
;
686 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
687 sizeof(struct lttng_kernel_kprobe
));
689 case LTTNG_KERNEL_UPROBE
:
690 event
.type
= LTTNG_EVENT_USERSPACE_PROBE
;
692 case LTTNG_KERNEL_FUNCTION
:
693 event
.type
= LTTNG_EVENT_FUNCTION
;
694 memcpy(&event
.attr
.ftrace
, &kevent
->event
->u
.ftrace
,
695 sizeof(struct lttng_kernel_function
));
697 case LTTNG_KERNEL_NOOP
:
698 event
.type
= LTTNG_EVENT_NOOP
;
700 case LTTNG_KERNEL_SYSCALL
:
701 event
.type
= LTTNG_EVENT_SYSCALL
;
703 case LTTNG_KERNEL_ALL
:
710 ret
= lttng_dynamic_buffer_append(
711 &payload
->buffer
, &event
, sizeof(event
));
713 ERR("Failed to append event to payload");
714 ret
= -LTTNG_ERR_NOMEM
;
719 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
720 /* Append extended info. */
721 ret
= append_extended_info(kevent
->filter_expression
, NULL
,
722 kevent
->userspace_probe_location
, payload
);
724 DBG("Error appending extended info message");
725 ret
= -LTTNG_ERR_FATAL
;
737 * Add URI so the consumer output object. Set the correct path depending on the
738 * domain adding the default trace directory.
740 static enum lttng_error_code
add_uri_to_consumer(
741 const struct ltt_session
*session
,
742 struct consumer_output
*consumer
,
743 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
746 enum lttng_error_code ret_code
= LTTNG_OK
;
750 if (consumer
== NULL
) {
751 DBG("No consumer detected. Don't add URI. Stopping.");
752 ret_code
= LTTNG_ERR_NO_CONSUMER
;
757 case LTTNG_DOMAIN_KERNEL
:
758 ret
= lttng_strncpy(consumer
->domain_subdir
,
759 DEFAULT_KERNEL_TRACE_DIR
,
760 sizeof(consumer
->domain_subdir
));
762 case LTTNG_DOMAIN_UST
:
763 ret
= lttng_strncpy(consumer
->domain_subdir
,
764 DEFAULT_UST_TRACE_DIR
,
765 sizeof(consumer
->domain_subdir
));
769 * This case is possible is we try to add the URI to the global
770 * tracing session consumer object which in this case there is
773 memset(consumer
->domain_subdir
, 0,
774 sizeof(consumer
->domain_subdir
));
778 ERR("Failed to initialize consumer output domain subdirectory");
779 ret_code
= LTTNG_ERR_FATAL
;
783 switch (uri
->dtype
) {
786 DBG2("Setting network URI to consumer");
788 if (consumer
->type
== CONSUMER_DST_NET
) {
789 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
790 consumer
->dst
.net
.control_isset
) ||
791 (uri
->stype
== LTTNG_STREAM_DATA
&&
792 consumer
->dst
.net
.data_isset
)) {
793 ret_code
= LTTNG_ERR_URL_EXIST
;
797 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
800 /* Set URI into consumer output object */
801 ret
= consumer_set_network_uri(session
, consumer
, uri
);
805 } else if (ret
== 1) {
807 * URI was the same in the consumer so we do not append the subdir
808 * again so to not duplicate output dir.
815 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
816 ret_code
= LTTNG_ERR_INVALID
;
819 DBG2("Setting trace directory path from URI to %s",
821 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
823 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
825 sizeof(consumer
->dst
.session_root_path
));
827 ret_code
= LTTNG_ERR_FATAL
;
830 consumer
->type
= CONSUMER_DST_LOCAL
;
840 * Init tracing by creating trace directory and sending fds kernel consumer.
842 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
845 struct lttng_ht_iter iter
;
846 struct consumer_socket
*socket
;
852 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
853 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
855 pthread_mutex_lock(socket
->lock
);
856 ret
= kernel_consumer_send_session(socket
, session
);
857 pthread_mutex_unlock(socket
->lock
);
859 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
871 * Create a socket to the relayd using the URI.
873 * On success, the relayd_sock pointer is set to the created socket.
874 * Else, it remains untouched and an LTTng error code is returned.
876 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
877 struct lttcomm_relayd_sock
**relayd_sock
,
878 struct consumer_output
*consumer
)
881 enum lttng_error_code status
= LTTNG_OK
;
882 struct lttcomm_relayd_sock
*rsock
;
884 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
885 RELAYD_VERSION_COMM_MINOR
);
887 status
= LTTNG_ERR_FATAL
;
892 * Connect to relayd so we can proceed with a session creation. This call
893 * can possibly block for an arbitrary amount of time to set the health
894 * state to be in poll execution.
897 ret
= relayd_connect(rsock
);
900 ERR("Unable to reach lttng-relayd");
901 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
905 /* Create socket for control stream. */
906 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
907 uint64_t result_flags
;
909 DBG3("Creating relayd stream socket from URI");
911 /* Check relayd version */
912 ret
= relayd_version_check(rsock
);
913 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
914 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
916 } else if (ret
< 0) {
917 ERR("Unable to reach lttng-relayd");
918 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
921 consumer
->relay_major_version
= rsock
->major
;
922 consumer
->relay_minor_version
= rsock
->minor
;
923 ret
= relayd_get_configuration(rsock
, 0,
926 ERR("Unable to get relayd configuration");
927 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
930 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
931 consumer
->relay_allows_clear
= true;
933 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
934 DBG3("Creating relayd data socket from URI");
936 /* Command is not valid */
937 ERR("Relayd invalid stream type: %d", uri
->stype
);
938 status
= LTTNG_ERR_INVALID
;
942 *relayd_sock
= rsock
;
947 /* The returned value is not useful since we are on an error path. */
948 (void) relayd_close(rsock
);
956 * Connect to the relayd using URI and send the socket to the right consumer.
958 * The consumer socket lock must be held by the caller.
960 * Returns LTTNG_OK on success or an LTTng error code on failure.
962 static enum lttng_error_code
send_consumer_relayd_socket(
963 unsigned int session_id
,
964 struct lttng_uri
*relayd_uri
,
965 struct consumer_output
*consumer
,
966 struct consumer_socket
*consumer_sock
,
967 const char *session_name
, const char *hostname
,
968 const char *base_path
, int session_live_timer
,
969 const uint64_t *current_chunk_id
,
970 time_t session_creation_time
,
971 bool session_name_contains_creation_time
)
974 struct lttcomm_relayd_sock
*rsock
= NULL
;
975 enum lttng_error_code status
;
977 /* Connect to relayd and make version check if uri is the control. */
978 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
979 if (status
!= LTTNG_OK
) {
980 goto relayd_comm_error
;
984 /* Set the network sequence index if not set. */
985 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
986 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
988 * Increment net_seq_idx because we are about to transfer the
989 * new relayd socket to the consumer.
990 * Assign unique key so the consumer can match streams.
992 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
993 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
996 /* Send relayd socket to consumer. */
997 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
998 relayd_uri
->stype
, session_id
,
999 session_name
, hostname
, base_path
,
1000 session_live_timer
, current_chunk_id
,
1001 session_creation_time
, session_name_contains_creation_time
);
1003 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1007 /* Flag that the corresponding socket was sent. */
1008 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1009 consumer_sock
->control_sock_sent
= 1;
1010 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1011 consumer_sock
->data_sock_sent
= 1;
1015 * Close socket which was dup on the consumer side. The session daemon does
1016 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1020 if (status
!= LTTNG_OK
) {
1022 * The consumer output for this session should not be used anymore
1023 * since the relayd connection failed thus making any tracing or/and
1024 * streaming not usable.
1026 consumer
->enabled
= 0;
1028 (void) relayd_close(rsock
);
1036 * Send both relayd sockets to a specific consumer and domain. This is a
1037 * helper function to facilitate sending the information to the consumer for a
1040 * The consumer socket lock must be held by the caller.
1042 * Returns LTTNG_OK, or an LTTng error code on failure.
1044 static enum lttng_error_code
send_consumer_relayd_sockets(
1045 enum lttng_domain_type domain
,
1046 unsigned int session_id
, struct consumer_output
*consumer
,
1047 struct consumer_socket
*sock
, const char *session_name
,
1048 const char *hostname
, const char *base_path
, int session_live_timer
,
1049 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1050 bool session_name_contains_creation_time
)
1052 enum lttng_error_code status
= LTTNG_OK
;
1057 /* Sending control relayd socket. */
1058 if (!sock
->control_sock_sent
) {
1059 status
= send_consumer_relayd_socket(session_id
,
1060 &consumer
->dst
.net
.control
, consumer
, sock
,
1061 session_name
, hostname
, base_path
, session_live_timer
,
1062 current_chunk_id
, session_creation_time
,
1063 session_name_contains_creation_time
);
1064 if (status
!= LTTNG_OK
) {
1069 /* Sending data relayd socket. */
1070 if (!sock
->data_sock_sent
) {
1071 status
= send_consumer_relayd_socket(session_id
,
1072 &consumer
->dst
.net
.data
, consumer
, sock
,
1073 session_name
, hostname
, base_path
, session_live_timer
,
1074 current_chunk_id
, session_creation_time
,
1075 session_name_contains_creation_time
);
1076 if (status
!= LTTNG_OK
) {
1086 * Setup relayd connections for a tracing session. First creates the socket to
1087 * the relayd and send them to the right domain consumer. Consumer type MUST be
1090 int cmd_setup_relayd(struct ltt_session
*session
)
1093 struct ltt_ust_session
*usess
;
1094 struct ltt_kernel_session
*ksess
;
1095 struct consumer_socket
*socket
;
1096 struct lttng_ht_iter iter
;
1097 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1101 usess
= session
->ust_session
;
1102 ksess
= session
->kernel_session
;
1104 DBG("Setting relayd for session %s", session
->name
);
1107 if (session
->current_trace_chunk
) {
1108 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1109 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1111 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1112 current_chunk_id
.is_set
= true;
1114 ERR("Failed to get current trace chunk id");
1115 ret
= LTTNG_ERR_UNK
;
1120 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1121 && usess
->consumer
->enabled
) {
1122 /* For each consumer socket, send relayd sockets */
1123 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1124 socket
, node
.node
) {
1125 pthread_mutex_lock(socket
->lock
);
1126 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1127 usess
->consumer
, socket
,
1128 session
->name
, session
->hostname
,
1130 session
->live_timer
,
1131 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1132 session
->creation_time
,
1133 session
->name_contains_creation_time
);
1134 pthread_mutex_unlock(socket
->lock
);
1135 if (ret
!= LTTNG_OK
) {
1138 /* Session is now ready for network streaming. */
1139 session
->net_handle
= 1;
1141 session
->consumer
->relay_major_version
=
1142 usess
->consumer
->relay_major_version
;
1143 session
->consumer
->relay_minor_version
=
1144 usess
->consumer
->relay_minor_version
;
1145 session
->consumer
->relay_allows_clear
=
1146 usess
->consumer
->relay_allows_clear
;
1149 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1150 && ksess
->consumer
->enabled
) {
1151 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1152 socket
, node
.node
) {
1153 pthread_mutex_lock(socket
->lock
);
1154 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1155 ksess
->consumer
, socket
,
1156 session
->name
, session
->hostname
,
1158 session
->live_timer
,
1159 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1160 session
->creation_time
,
1161 session
->name_contains_creation_time
);
1162 pthread_mutex_unlock(socket
->lock
);
1163 if (ret
!= LTTNG_OK
) {
1166 /* Session is now ready for network streaming. */
1167 session
->net_handle
= 1;
1169 session
->consumer
->relay_major_version
=
1170 ksess
->consumer
->relay_major_version
;
1171 session
->consumer
->relay_minor_version
=
1172 ksess
->consumer
->relay_minor_version
;
1173 session
->consumer
->relay_allows_clear
=
1174 ksess
->consumer
->relay_allows_clear
;
1183 * Start a kernel session by opening all necessary streams.
1185 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1188 struct ltt_kernel_channel
*kchan
;
1190 /* Open kernel metadata */
1191 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1192 ret
= kernel_open_metadata(ksess
);
1194 ret
= LTTNG_ERR_KERN_META_FAIL
;
1199 /* Open kernel metadata stream */
1200 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1201 ret
= kernel_open_metadata_stream(ksess
);
1203 ERR("Kernel create metadata stream failed");
1204 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1209 /* For each channel */
1210 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1211 if (kchan
->stream_count
== 0) {
1212 ret
= kernel_open_channel_stream(kchan
);
1214 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1217 /* Update the stream global counter */
1218 ksess
->stream_count_global
+= ret
;
1222 /* Setup kernel consumer socket and send fds to it */
1223 ret
= init_kernel_tracing(ksess
);
1225 ret
= LTTNG_ERR_KERN_START_FAIL
;
1229 /* This start the kernel tracing */
1230 ret
= kernel_start_session(ksess
);
1232 ret
= LTTNG_ERR_KERN_START_FAIL
;
1236 /* Quiescent wait after starting trace */
1237 kernel_wait_quiescent();
1247 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1249 struct ltt_kernel_channel
*kchan
;
1250 bool error_occurred
= false;
1253 if (!ksess
|| !ksess
->active
) {
1256 DBG("Stopping kernel tracing");
1258 ret
= kernel_stop_session(ksess
);
1260 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1264 kernel_wait_quiescent();
1266 /* Flush metadata after stopping (if exists) */
1267 if (ksess
->metadata_stream_fd
>= 0) {
1268 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1270 ERR("Kernel metadata flush failed");
1271 error_occurred
= true;
1275 /* Flush all buffers after stopping */
1276 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1277 ret
= kernel_flush_buffer(kchan
);
1279 ERR("Kernel flush buffer error");
1280 error_occurred
= true;
1285 if (error_occurred
) {
1286 ret
= LTTNG_ERR_UNK
;
1295 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1297 int cmd_disable_channel(struct ltt_session
*session
,
1298 enum lttng_domain_type domain
, char *channel_name
)
1301 struct ltt_ust_session
*usess
;
1303 usess
= session
->ust_session
;
1308 case LTTNG_DOMAIN_KERNEL
:
1310 ret
= channel_kernel_disable(session
->kernel_session
,
1312 if (ret
!= LTTNG_OK
) {
1316 kernel_wait_quiescent();
1319 case LTTNG_DOMAIN_UST
:
1321 struct ltt_ust_channel
*uchan
;
1322 struct lttng_ht
*chan_ht
;
1324 chan_ht
= usess
->domain_global
.channels
;
1326 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1327 if (uchan
== NULL
) {
1328 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1332 ret
= channel_ust_disable(usess
, uchan
);
1333 if (ret
!= LTTNG_OK
) {
1339 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1351 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1353 * The wpipe arguments is used as a notifier for the kernel thread.
1355 int cmd_enable_channel(struct ltt_session
*session
,
1356 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1359 struct ltt_ust_session
*usess
= session
->ust_session
;
1360 struct lttng_ht
*chan_ht
;
1362 struct lttng_channel attr
;
1369 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1371 /* Validate channel name */
1372 if (attr
.name
[0] == '.' ||
1373 memchr(attr
.name
, '/', len
) != NULL
) {
1374 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1378 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1383 * Don't try to enable a channel if the session has been started at
1384 * some point in time before. The tracer does not allow it.
1386 if (session
->has_been_started
) {
1387 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1392 * If the session is a live session, remove the switch timer, the
1393 * live timer does the same thing but sends also synchronisation
1394 * beacons for inactive streams.
1396 if (session
->live_timer
> 0) {
1397 attr
.attr
.live_timer_interval
= session
->live_timer
;
1398 attr
.attr
.switch_timer_interval
= 0;
1401 /* Check for feature support */
1402 switch (domain
->type
) {
1403 case LTTNG_DOMAIN_KERNEL
:
1405 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1406 /* Sampling position of buffer is not supported */
1407 WARN("Kernel tracer does not support buffer monitoring. "
1408 "Setting the monitor interval timer to 0 "
1409 "(disabled) for channel '%s' of session '%s'",
1410 attr
.name
, session
->name
);
1411 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1415 case LTTNG_DOMAIN_UST
:
1417 case LTTNG_DOMAIN_JUL
:
1418 case LTTNG_DOMAIN_LOG4J
:
1419 case LTTNG_DOMAIN_PYTHON
:
1420 if (!agent_tracing_is_enabled()) {
1421 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1422 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1427 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1431 switch (domain
->type
) {
1432 case LTTNG_DOMAIN_KERNEL
:
1434 struct ltt_kernel_channel
*kchan
;
1436 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1437 session
->kernel_session
);
1438 if (kchan
== NULL
) {
1439 if (session
->snapshot
.nb_output
> 0 ||
1440 session
->snapshot_mode
) {
1441 /* Enforce mmap output for snapshot sessions. */
1442 attr
.attr
.output
= LTTNG_EVENT_MMAP
;
1444 ret
= channel_kernel_create(session
->kernel_session
, &attr
, wpipe
);
1445 if (attr
.name
[0] != '\0') {
1446 session
->kernel_session
->has_non_default_channel
= 1;
1449 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1452 if (ret
!= LTTNG_OK
) {
1456 kernel_wait_quiescent();
1459 case LTTNG_DOMAIN_UST
:
1460 case LTTNG_DOMAIN_JUL
:
1461 case LTTNG_DOMAIN_LOG4J
:
1462 case LTTNG_DOMAIN_PYTHON
:
1464 struct ltt_ust_channel
*uchan
;
1469 * Current agent implementation limitations force us to allow
1470 * only one channel at once in "agent" subdomains. Each
1471 * subdomain has a default channel name which must be strictly
1474 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1475 if (strncmp(attr
.name
, DEFAULT_JUL_CHANNEL_NAME
,
1476 LTTNG_SYMBOL_NAME_LEN
)) {
1477 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1480 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1481 if (strncmp(attr
.name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1482 LTTNG_SYMBOL_NAME_LEN
)) {
1483 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1486 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1487 if (strncmp(attr
.name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1488 LTTNG_SYMBOL_NAME_LEN
)) {
1489 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1494 chan_ht
= usess
->domain_global
.channels
;
1496 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1497 if (uchan
== NULL
) {
1498 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1499 if (attr
.name
[0] != '\0') {
1500 usess
->has_non_default_channel
= 1;
1503 ret
= channel_ust_enable(usess
, uchan
);
1508 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1512 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1513 session
->has_non_mmap_channel
= true;
1521 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1522 struct ltt_session
*session
,
1523 enum lttng_domain_type domain
,
1524 enum lttng_process_attr process_attr
,
1525 enum lttng_tracking_policy
*policy
)
1527 enum lttng_error_code ret_code
= LTTNG_OK
;
1528 const struct process_attr_tracker
*tracker
;
1531 case LTTNG_DOMAIN_KERNEL
:
1532 if (!session
->kernel_session
) {
1533 ret_code
= LTTNG_ERR_INVALID
;
1536 tracker
= kernel_get_process_attr_tracker(
1537 session
->kernel_session
, process_attr
);
1539 case LTTNG_DOMAIN_UST
:
1540 if (!session
->ust_session
) {
1541 ret_code
= LTTNG_ERR_INVALID
;
1544 tracker
= trace_ust_get_process_attr_tracker(
1545 session
->ust_session
, process_attr
);
1548 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1552 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1554 ret_code
= LTTNG_ERR_INVALID
;
1560 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1561 struct ltt_session
*session
,
1562 enum lttng_domain_type domain
,
1563 enum lttng_process_attr process_attr
,
1564 enum lttng_tracking_policy policy
)
1566 enum lttng_error_code ret_code
= LTTNG_OK
;
1569 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1570 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1571 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1574 ret_code
= LTTNG_ERR_INVALID
;
1579 case LTTNG_DOMAIN_KERNEL
:
1580 if (!session
->kernel_session
) {
1581 ret_code
= LTTNG_ERR_INVALID
;
1584 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1585 session
->kernel_session
, process_attr
, policy
);
1587 case LTTNG_DOMAIN_UST
:
1588 if (!session
->ust_session
) {
1589 ret_code
= LTTNG_ERR_INVALID
;
1592 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1593 session
->ust_session
, process_attr
, policy
);
1596 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1603 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1604 struct ltt_session
*session
,
1605 enum lttng_domain_type domain
,
1606 enum lttng_process_attr process_attr
,
1607 const struct process_attr_value
*value
)
1609 enum lttng_error_code ret_code
= LTTNG_OK
;
1612 case LTTNG_DOMAIN_KERNEL
:
1613 if (!session
->kernel_session
) {
1614 ret_code
= LTTNG_ERR_INVALID
;
1617 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1618 session
->kernel_session
, process_attr
, value
);
1620 case LTTNG_DOMAIN_UST
:
1621 if (!session
->ust_session
) {
1622 ret_code
= LTTNG_ERR_INVALID
;
1625 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1626 session
->ust_session
, process_attr
, value
);
1629 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1636 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1637 struct ltt_session
*session
,
1638 enum lttng_domain_type domain
,
1639 enum lttng_process_attr process_attr
,
1640 const struct process_attr_value
*value
)
1642 enum lttng_error_code ret_code
= LTTNG_OK
;
1645 case LTTNG_DOMAIN_KERNEL
:
1646 if (!session
->kernel_session
) {
1647 ret_code
= LTTNG_ERR_INVALID
;
1650 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1651 session
->kernel_session
, process_attr
, value
);
1653 case LTTNG_DOMAIN_UST
:
1654 if (!session
->ust_session
) {
1655 ret_code
= LTTNG_ERR_INVALID
;
1658 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1659 session
->ust_session
, process_attr
, value
);
1662 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1669 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1670 struct ltt_session
*session
,
1671 enum lttng_domain_type domain
,
1672 enum lttng_process_attr process_attr
,
1673 struct lttng_process_attr_values
**values
)
1675 enum lttng_error_code ret_code
= LTTNG_OK
;
1676 const struct process_attr_tracker
*tracker
;
1677 enum process_attr_tracker_status status
;
1680 case LTTNG_DOMAIN_KERNEL
:
1681 if (!session
->kernel_session
) {
1682 ret_code
= LTTNG_ERR_INVALID
;
1685 tracker
= kernel_get_process_attr_tracker(
1686 session
->kernel_session
, process_attr
);
1688 case LTTNG_DOMAIN_UST
:
1689 if (!session
->ust_session
) {
1690 ret_code
= LTTNG_ERR_INVALID
;
1693 tracker
= trace_ust_get_process_attr_tracker(
1694 session
->ust_session
, process_attr
);
1697 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1702 ret_code
= LTTNG_ERR_INVALID
;
1706 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1708 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1709 ret_code
= LTTNG_OK
;
1711 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1712 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1714 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1715 ret_code
= LTTNG_ERR_NOMEM
;
1718 ret_code
= LTTNG_ERR_UNK
;
1727 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1729 int cmd_disable_event(struct ltt_session
*session
,
1730 enum lttng_domain_type domain
, const char *channel_name
,
1731 const struct lttng_event
*event
)
1734 const char *event_name
;
1736 DBG("Disable event command for event \'%s\'", event
->name
);
1738 event_name
= event
->name
;
1740 /* Error out on unhandled search criteria */
1741 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1742 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1743 ret
= LTTNG_ERR_UNK
;
1750 case LTTNG_DOMAIN_KERNEL
:
1752 struct ltt_kernel_channel
*kchan
;
1753 struct ltt_kernel_session
*ksess
;
1755 ksess
= session
->kernel_session
;
1758 * If a non-default channel has been created in the
1759 * session, explicitely require that -c chan_name needs
1762 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1763 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1767 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1768 if (kchan
== NULL
) {
1769 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1773 switch (event
->type
) {
1774 case LTTNG_EVENT_ALL
:
1775 case LTTNG_EVENT_TRACEPOINT
:
1776 case LTTNG_EVENT_SYSCALL
:
1777 case LTTNG_EVENT_PROBE
:
1778 case LTTNG_EVENT_FUNCTION
:
1779 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1780 if (event_name
[0] == '\0') {
1781 ret
= event_kernel_disable_event(kchan
,
1784 ret
= event_kernel_disable_event(kchan
,
1785 event_name
, event
->type
);
1787 if (ret
!= LTTNG_OK
) {
1792 ret
= LTTNG_ERR_UNK
;
1796 kernel_wait_quiescent();
1799 case LTTNG_DOMAIN_UST
:
1801 struct ltt_ust_channel
*uchan
;
1802 struct ltt_ust_session
*usess
;
1804 usess
= session
->ust_session
;
1806 if (validate_ust_event_name(event_name
)) {
1807 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1812 * If a non-default channel has been created in the
1813 * session, explicitly require that -c chan_name needs
1816 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1817 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1821 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1823 if (uchan
== NULL
) {
1824 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1828 switch (event
->type
) {
1829 case LTTNG_EVENT_ALL
:
1831 * An empty event name means that everything
1832 * should be disabled.
1834 if (event
->name
[0] == '\0') {
1835 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1837 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1840 if (ret
!= LTTNG_OK
) {
1845 ret
= LTTNG_ERR_UNK
;
1849 DBG3("Disable UST event %s in channel %s completed", event_name
,
1853 case LTTNG_DOMAIN_LOG4J
:
1854 case LTTNG_DOMAIN_JUL
:
1855 case LTTNG_DOMAIN_PYTHON
:
1858 struct ltt_ust_session
*usess
= session
->ust_session
;
1862 switch (event
->type
) {
1863 case LTTNG_EVENT_ALL
:
1866 ret
= LTTNG_ERR_UNK
;
1870 agt
= trace_ust_find_agent(usess
, domain
);
1872 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1876 * An empty event name means that everything
1877 * should be disabled.
1879 if (event
->name
[0] == '\0') {
1880 ret
= event_agent_disable_all(usess
, agt
);
1882 ret
= event_agent_disable(usess
, agt
, event_name
);
1884 if (ret
!= LTTNG_OK
) {
1891 ret
= LTTNG_ERR_UND
;
1904 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1906 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1907 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1909 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1910 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1913 * Don't try to add a context if the session has been started at
1914 * some point in time before. The tracer does not allow it and would
1915 * result in a corrupted trace.
1917 if (session
->has_been_started
) {
1918 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1922 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1923 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1924 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1928 case LTTNG_DOMAIN_KERNEL
:
1929 assert(session
->kernel_session
);
1931 if (session
->kernel_session
->channel_count
== 0) {
1932 /* Create default channel */
1933 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1934 if (ret
!= LTTNG_OK
) {
1937 chan_kern_created
= 1;
1939 /* Add kernel context to kernel tracer */
1940 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1941 if (ret
!= LTTNG_OK
) {
1945 case LTTNG_DOMAIN_JUL
:
1946 case LTTNG_DOMAIN_LOG4J
:
1949 * Validate channel name.
1950 * If no channel name is given and the domain is JUL or LOG4J,
1951 * set it to the appropriate domain-specific channel name. If
1952 * a name is provided but does not match the expexted channel
1953 * name, return an error.
1955 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1956 strcmp(channel_name
,
1957 DEFAULT_JUL_CHANNEL_NAME
)) {
1958 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1960 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1961 strcmp(channel_name
,
1962 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1963 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1966 /* break is _not_ missing here. */
1968 case LTTNG_DOMAIN_UST
:
1970 struct ltt_ust_session
*usess
= session
->ust_session
;
1971 unsigned int chan_count
;
1975 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1976 if (chan_count
== 0) {
1977 struct lttng_channel
*attr
;
1978 /* Create default channel */
1979 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1981 ret
= LTTNG_ERR_FATAL
;
1985 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1986 if (ret
!= LTTNG_OK
) {
1990 channel_attr_destroy(attr
);
1991 chan_ust_created
= 1;
1994 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1995 free(app_ctx_provider_name
);
1997 app_ctx_name
= NULL
;
1998 app_ctx_provider_name
= NULL
;
1999 if (ret
!= LTTNG_OK
) {
2005 ret
= LTTNG_ERR_UND
;
2013 if (chan_kern_created
) {
2014 struct ltt_kernel_channel
*kchan
=
2015 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2016 session
->kernel_session
);
2017 /* Created previously, this should NOT fail. */
2019 kernel_destroy_channel(kchan
);
2022 if (chan_ust_created
) {
2023 struct ltt_ust_channel
*uchan
=
2024 trace_ust_find_channel_by_name(
2025 session
->ust_session
->domain_global
.channels
,
2026 DEFAULT_CHANNEL_NAME
);
2027 /* Created previously, this should NOT fail. */
2029 /* Remove from the channel list of the session. */
2030 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2032 trace_ust_destroy_channel(uchan
);
2035 free(app_ctx_provider_name
);
2040 static inline bool name_starts_with(const char *name
, const char *prefix
)
2042 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2044 return !strncmp(name
, prefix
, max_cmp_len
);
2047 /* Perform userspace-specific event name validation */
2048 static int validate_ust_event_name(const char *name
)
2058 * Check name against all internal UST event component namespaces used
2061 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2062 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2063 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2072 * Internal version of cmd_enable_event() with a supplemental
2073 * "internal_event" flag which is used to enable internal events which should
2074 * be hidden from clients. Such events are used in the agent implementation to
2075 * enable the events through which all "agent" events are funeled.
2077 static int _cmd_enable_event(struct ltt_session
*session
,
2078 const struct lttng_domain
*domain
,
2079 char *channel_name
, struct lttng_event
*event
,
2080 char *filter_expression
,
2081 struct lttng_filter_bytecode
*filter
,
2082 struct lttng_event_exclusion
*exclusion
,
2083 int wpipe
, bool internal_event
)
2085 int ret
= 0, channel_created
= 0;
2086 struct lttng_channel
*attr
= NULL
;
2090 assert(channel_name
);
2092 /* If we have a filter, we must have its filter expression */
2093 assert(!(!!filter_expression
^ !!filter
));
2095 /* Normalize event name as a globbing pattern */
2096 strutils_normalize_star_glob_pattern(event
->name
);
2098 /* Normalize exclusion names as globbing patterns */
2102 for (i
= 0; i
< exclusion
->count
; i
++) {
2103 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2105 strutils_normalize_star_glob_pattern(name
);
2109 DBG("Enable event command for event \'%s\'", event
->name
);
2113 switch (domain
->type
) {
2114 case LTTNG_DOMAIN_KERNEL
:
2116 struct ltt_kernel_channel
*kchan
;
2119 * If a non-default channel has been created in the
2120 * session, explicitely require that -c chan_name needs
2123 if (session
->kernel_session
->has_non_default_channel
2124 && channel_name
[0] == '\0') {
2125 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2129 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2130 session
->kernel_session
);
2131 if (kchan
== NULL
) {
2132 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2133 LTTNG_BUFFER_GLOBAL
);
2135 ret
= LTTNG_ERR_FATAL
;
2138 if (lttng_strncpy(attr
->name
, channel_name
,
2139 sizeof(attr
->name
))) {
2140 ret
= LTTNG_ERR_INVALID
;
2144 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2145 if (ret
!= LTTNG_OK
) {
2148 channel_created
= 1;
2151 /* Get the newly created kernel channel pointer */
2152 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2153 session
->kernel_session
);
2154 if (kchan
== NULL
) {
2155 /* This sould not happen... */
2156 ret
= LTTNG_ERR_FATAL
;
2160 switch (event
->type
) {
2161 case LTTNG_EVENT_ALL
:
2163 char *filter_expression_a
= NULL
;
2164 struct lttng_filter_bytecode
*filter_a
= NULL
;
2167 * We need to duplicate filter_expression and filter,
2168 * because ownership is passed to first enable
2171 if (filter_expression
) {
2172 filter_expression_a
= strdup(filter_expression
);
2173 if (!filter_expression_a
) {
2174 ret
= LTTNG_ERR_FATAL
;
2179 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2181 free(filter_expression_a
);
2182 ret
= LTTNG_ERR_FATAL
;
2185 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2187 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2188 ret
= event_kernel_enable_event(kchan
, event
,
2189 filter_expression
, filter
);
2190 /* We have passed ownership */
2191 filter_expression
= NULL
;
2193 if (ret
!= LTTNG_OK
) {
2194 if (channel_created
) {
2195 /* Let's not leak a useless channel. */
2196 kernel_destroy_channel(kchan
);
2198 free(filter_expression_a
);
2202 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2203 ret
= event_kernel_enable_event(kchan
, event
,
2204 filter_expression_a
, filter_a
);
2205 /* We have passed ownership */
2206 filter_expression_a
= NULL
;
2208 if (ret
!= LTTNG_OK
) {
2213 case LTTNG_EVENT_PROBE
:
2214 case LTTNG_EVENT_USERSPACE_PROBE
:
2215 case LTTNG_EVENT_FUNCTION
:
2216 case LTTNG_EVENT_FUNCTION_ENTRY
:
2217 case LTTNG_EVENT_TRACEPOINT
:
2218 ret
= event_kernel_enable_event(kchan
, event
,
2219 filter_expression
, filter
);
2220 /* We have passed ownership */
2221 filter_expression
= NULL
;
2223 if (ret
!= LTTNG_OK
) {
2224 if (channel_created
) {
2225 /* Let's not leak a useless channel. */
2226 kernel_destroy_channel(kchan
);
2231 case LTTNG_EVENT_SYSCALL
:
2232 ret
= event_kernel_enable_event(kchan
, event
,
2233 filter_expression
, filter
);
2234 /* We have passed ownership */
2235 filter_expression
= NULL
;
2237 if (ret
!= LTTNG_OK
) {
2242 ret
= LTTNG_ERR_UNK
;
2246 kernel_wait_quiescent();
2249 case LTTNG_DOMAIN_UST
:
2251 struct ltt_ust_channel
*uchan
;
2252 struct ltt_ust_session
*usess
= session
->ust_session
;
2257 * If a non-default channel has been created in the
2258 * session, explicitely require that -c chan_name needs
2261 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2262 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2266 /* Get channel from global UST domain */
2267 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2269 if (uchan
== NULL
) {
2270 /* Create default channel */
2271 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2272 usess
->buffer_type
);
2274 ret
= LTTNG_ERR_FATAL
;
2277 if (lttng_strncpy(attr
->name
, channel_name
,
2278 sizeof(attr
->name
))) {
2279 ret
= LTTNG_ERR_INVALID
;
2283 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2284 if (ret
!= LTTNG_OK
) {
2288 /* Get the newly created channel reference back */
2289 uchan
= trace_ust_find_channel_by_name(
2290 usess
->domain_global
.channels
, channel_name
);
2294 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2296 * Don't allow users to add UST events to channels which
2297 * are assigned to a userspace subdomain (JUL, Log4J,
2300 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2304 if (!internal_event
) {
2306 * Ensure the event name is not reserved for internal
2309 ret
= validate_ust_event_name(event
->name
);
2311 WARN("Userspace event name %s failed validation.",
2313 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2318 /* At this point, the session and channel exist on the tracer */
2319 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2320 filter_expression
, filter
, exclusion
,
2322 /* We have passed ownership */
2323 filter_expression
= NULL
;
2326 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2327 goto already_enabled
;
2328 } else if (ret
!= LTTNG_OK
) {
2333 case LTTNG_DOMAIN_LOG4J
:
2334 case LTTNG_DOMAIN_JUL
:
2335 case LTTNG_DOMAIN_PYTHON
:
2337 const char *default_event_name
, *default_chan_name
;
2339 struct lttng_event uevent
;
2340 struct lttng_domain tmp_dom
;
2341 struct ltt_ust_session
*usess
= session
->ust_session
;
2345 if (!agent_tracing_is_enabled()) {
2346 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2347 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2351 agt
= trace_ust_find_agent(usess
, domain
->type
);
2353 agt
= agent_create(domain
->type
);
2355 ret
= LTTNG_ERR_NOMEM
;
2358 agent_add(agt
, usess
->agents
);
2361 /* Create the default tracepoint. */
2362 memset(&uevent
, 0, sizeof(uevent
));
2363 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2364 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2365 default_event_name
= event_get_default_agent_ust_name(
2367 if (!default_event_name
) {
2368 ret
= LTTNG_ERR_FATAL
;
2371 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2372 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2375 * The domain type is changed because we are about to enable the
2376 * default channel and event for the JUL domain that are hardcoded.
2377 * This happens in the UST domain.
2379 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2380 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2382 switch (domain
->type
) {
2383 case LTTNG_DOMAIN_LOG4J
:
2384 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2386 case LTTNG_DOMAIN_JUL
:
2387 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2389 case LTTNG_DOMAIN_PYTHON
:
2390 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2393 /* The switch/case we are in makes this impossible */
2398 char *filter_expression_copy
= NULL
;
2399 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2402 const size_t filter_size
= sizeof(
2403 struct lttng_filter_bytecode
)
2406 filter_copy
= zmalloc(filter_size
);
2408 ret
= LTTNG_ERR_NOMEM
;
2411 memcpy(filter_copy
, filter
, filter_size
);
2413 filter_expression_copy
=
2414 strdup(filter_expression
);
2415 if (!filter_expression
) {
2416 ret
= LTTNG_ERR_NOMEM
;
2419 if (!filter_expression_copy
|| !filter_copy
) {
2420 free(filter_expression_copy
);
2426 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2427 (char *) default_chan_name
,
2428 &uevent
, filter_expression_copy
,
2429 filter_copy
, NULL
, wpipe
);
2432 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2433 goto already_enabled
;
2434 } else if (ret
!= LTTNG_OK
) {
2438 /* The wild card * means that everything should be enabled. */
2439 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2440 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2443 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2447 filter_expression
= NULL
;
2448 if (ret
!= LTTNG_OK
) {
2455 ret
= LTTNG_ERR_UND
;
2463 free(filter_expression
);
2466 channel_attr_destroy(attr
);
2472 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2473 * We own filter, exclusion, and filter_expression.
2475 int cmd_enable_event(struct ltt_session
*session
,
2476 const struct lttng_domain
*domain
,
2477 char *channel_name
, struct lttng_event
*event
,
2478 char *filter_expression
,
2479 struct lttng_filter_bytecode
*filter
,
2480 struct lttng_event_exclusion
*exclusion
,
2483 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2484 filter_expression
, filter
, exclusion
, wpipe
, false);
2488 * Enable an event which is internal to LTTng. An internal should
2489 * never be made visible to clients and are immune to checks such as
2492 static int cmd_enable_event_internal(struct ltt_session
*session
,
2493 const struct lttng_domain
*domain
,
2494 char *channel_name
, struct lttng_event
*event
,
2495 char *filter_expression
,
2496 struct lttng_filter_bytecode
*filter
,
2497 struct lttng_event_exclusion
*exclusion
,
2500 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2501 filter_expression
, filter
, exclusion
, wpipe
, true);
2505 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2507 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2508 struct lttng_event
**events
)
2511 ssize_t nb_events
= 0;
2514 case LTTNG_DOMAIN_KERNEL
:
2515 nb_events
= kernel_list_events(events
);
2516 if (nb_events
< 0) {
2517 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2521 case LTTNG_DOMAIN_UST
:
2522 nb_events
= ust_app_list_events(events
);
2523 if (nb_events
< 0) {
2524 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2528 case LTTNG_DOMAIN_LOG4J
:
2529 case LTTNG_DOMAIN_JUL
:
2530 case LTTNG_DOMAIN_PYTHON
:
2531 nb_events
= agent_list_events(events
, domain
);
2532 if (nb_events
< 0) {
2533 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2538 ret
= LTTNG_ERR_UND
;
2545 /* Return negative value to differentiate return code */
2550 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2552 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2553 struct lttng_event_field
**fields
)
2556 ssize_t nb_fields
= 0;
2559 case LTTNG_DOMAIN_UST
:
2560 nb_fields
= ust_app_list_event_fields(fields
);
2561 if (nb_fields
< 0) {
2562 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2566 case LTTNG_DOMAIN_KERNEL
:
2567 default: /* fall-through */
2568 ret
= LTTNG_ERR_UND
;
2575 /* Return negative value to differentiate return code */
2579 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2581 return syscall_table_list(events
);
2585 * Command LTTNG_START_TRACE processed by the client thread.
2587 * Called with session mutex held.
2589 int cmd_start_trace(struct ltt_session
*session
)
2591 enum lttng_error_code ret
;
2592 unsigned long nb_chan
= 0;
2593 struct ltt_kernel_session
*ksession
;
2594 struct ltt_ust_session
*usess
;
2595 const bool session_rotated_after_last_stop
=
2596 session
->rotated_after_last_stop
;
2597 const bool session_cleared_after_last_stop
=
2598 session
->cleared_after_last_stop
;
2602 /* Ease our life a bit ;) */
2603 ksession
= session
->kernel_session
;
2604 usess
= session
->ust_session
;
2606 /* Is the session already started? */
2607 if (session
->active
) {
2608 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2609 /* Perform nothing */
2613 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2614 !session
->current_trace_chunk
) {
2616 * A rotation was launched while the session was stopped and
2617 * it has not been completed yet. It is not possible to start
2618 * the session since starting the session here would require a
2619 * rotation from "NULL" to a new trace chunk. That rotation
2620 * would overlap with the ongoing rotation, which is not
2623 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2625 ret
= LTTNG_ERR_ROTATION_PENDING
;
2630 * Starting a session without channel is useless since after that it's not
2631 * possible to enable channel thus inform the client.
2633 if (usess
&& usess
->domain_global
.channels
) {
2634 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2637 nb_chan
+= ksession
->channel_count
;
2640 ret
= LTTNG_ERR_NO_CHANNEL
;
2644 session
->active
= 1;
2645 session
->rotated_after_last_stop
= false;
2646 session
->cleared_after_last_stop
= false;
2647 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2648 if (!session
->has_been_started
) {
2649 struct lttng_trace_chunk
*trace_chunk
;
2651 DBG("Creating initial trace chunk of session \"%s\"",
2653 trace_chunk
= session_create_new_trace_chunk(
2654 session
, NULL
, NULL
, NULL
);
2656 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2659 assert(!session
->current_trace_chunk
);
2660 ret
= session_set_trace_chunk(session
, trace_chunk
,
2662 lttng_trace_chunk_put(trace_chunk
);
2664 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2668 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2671 * Rotate existing streams into the new chunk.
2672 * This is a "quiet" rotation has no client has
2673 * explicitly requested this operation.
2675 * There is also no need to wait for the rotation
2676 * to complete as it will happen immediately. No data
2677 * was produced as the session was stopped, so the
2678 * rotation should happen on reception of the command.
2680 ret
= cmd_rotate_session(session
, NULL
, true,
2681 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2682 if (ret
!= LTTNG_OK
) {
2688 /* Kernel tracing */
2689 if (ksession
!= NULL
) {
2690 DBG("Start kernel tracing session %s", session
->name
);
2691 ret
= start_kernel_session(ksession
);
2692 if (ret
!= LTTNG_OK
) {
2697 /* Flag session that trace should start automatically */
2699 int int_ret
= ust_app_start_trace_all(usess
);
2702 ret
= LTTNG_ERR_UST_START_FAIL
;
2708 * Open a packet in every stream of the session to ensure that viewers
2709 * can correctly identify the boundaries of the periods during which
2710 * tracing was active for this session.
2712 ret
= session_open_packets(session
);
2713 if (ret
!= LTTNG_OK
) {
2718 * Clear the flag that indicates that a rotation was done while the
2719 * session was stopped.
2721 session
->rotated_after_last_stop
= false;
2723 if (session
->rotate_timer_period
) {
2724 int int_ret
= timer_session_rotation_schedule_timer_start(
2725 session
, session
->rotate_timer_period
);
2728 ERR("Failed to enable rotate timer");
2729 ret
= LTTNG_ERR_UNK
;
2737 if (ret
== LTTNG_OK
) {
2738 /* Flag this after a successful start. */
2739 session
->has_been_started
|= 1;
2741 session
->active
= 0;
2742 /* Restore initial state on error. */
2743 session
->rotated_after_last_stop
=
2744 session_rotated_after_last_stop
;
2745 session
->cleared_after_last_stop
=
2746 session_cleared_after_last_stop
;
2753 * Command LTTNG_STOP_TRACE processed by the client thread.
2755 int cmd_stop_trace(struct ltt_session
*session
)
2758 struct ltt_kernel_session
*ksession
;
2759 struct ltt_ust_session
*usess
;
2763 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2765 ksession
= session
->kernel_session
;
2766 usess
= session
->ust_session
;
2768 /* Session is not active. Skip everythong and inform the client. */
2769 if (!session
->active
) {
2770 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2774 ret
= stop_kernel_session(ksession
);
2775 if (ret
!= LTTNG_OK
) {
2779 if (usess
&& usess
->active
) {
2780 ret
= ust_app_stop_trace_all(usess
);
2782 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2787 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2789 /* Flag inactive after a successful stop. */
2790 session
->active
= 0;
2798 * Set the base_path of the session only if subdir of a control uris is set.
2799 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2801 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2803 struct lttng_uri
*uris
)
2808 for (i
= 0; i
< nb_uri
; i
++) {
2809 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2810 uris
[i
].subdir
[0] == '\0') {
2811 /* Not interested in these URIs */
2815 if (session
->base_path
!= NULL
) {
2816 free(session
->base_path
);
2817 session
->base_path
= NULL
;
2820 /* Set session base_path */
2821 session
->base_path
= strdup(uris
[i
].subdir
);
2822 if (!session
->base_path
) {
2823 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2824 uris
[i
].subdir
, session
->name
);
2825 ret
= LTTNG_ERR_NOMEM
;
2828 DBG2("Setting base path \"%s\" for session \"%s\"",
2829 session
->base_path
, session
->name
);
2837 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2839 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2840 struct lttng_uri
*uris
)
2843 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2844 struct ltt_ust_session
*usess
= session
->ust_session
;
2850 /* Can't set consumer URI if the session is active. */
2851 if (session
->active
) {
2852 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2857 * Set the session base path if any. This is done inside
2858 * cmd_set_consumer_uri to preserve backward compatibility of the
2859 * previous session creation api vs the session descriptor api.
2861 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2862 if (ret
!= LTTNG_OK
) {
2866 /* Set the "global" consumer URIs */
2867 for (i
= 0; i
< nb_uri
; i
++) {
2868 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2870 if (ret
!= LTTNG_OK
) {
2875 /* Set UST session URIs */
2876 if (session
->ust_session
) {
2877 for (i
= 0; i
< nb_uri
; i
++) {
2878 ret
= add_uri_to_consumer(session
,
2879 session
->ust_session
->consumer
,
2880 &uris
[i
], LTTNG_DOMAIN_UST
);
2881 if (ret
!= LTTNG_OK
) {
2887 /* Set kernel session URIs */
2888 if (session
->kernel_session
) {
2889 for (i
= 0; i
< nb_uri
; i
++) {
2890 ret
= add_uri_to_consumer(session
,
2891 session
->kernel_session
->consumer
,
2892 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2893 if (ret
!= LTTNG_OK
) {
2900 * Make sure to set the session in output mode after we set URI since a
2901 * session can be created without URL (thus flagged in no output mode).
2903 session
->output_traces
= 1;
2905 ksess
->output_traces
= 1;
2909 usess
->output_traces
= 1;
2920 enum lttng_error_code
set_session_output_from_descriptor(
2921 struct ltt_session
*session
,
2922 const struct lttng_session_descriptor
*descriptor
)
2925 enum lttng_error_code ret_code
= LTTNG_OK
;
2926 enum lttng_session_descriptor_type session_type
=
2927 lttng_session_descriptor_get_type(descriptor
);
2928 enum lttng_session_descriptor_output_type output_type
=
2929 lttng_session_descriptor_get_output_type(descriptor
);
2930 struct lttng_uri uris
[2] = {};
2931 size_t uri_count
= 0;
2933 switch (output_type
) {
2934 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2936 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2937 lttng_session_descriptor_get_local_output_uri(descriptor
,
2941 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2942 lttng_session_descriptor_get_network_output_uris(descriptor
,
2943 &uris
[0], &uris
[1]);
2947 ret_code
= LTTNG_ERR_INVALID
;
2951 switch (session_type
) {
2952 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2954 struct snapshot_output
*new_output
= NULL
;
2956 new_output
= snapshot_output_alloc();
2958 ret_code
= LTTNG_ERR_NOMEM
;
2962 ret
= snapshot_output_init_with_uri(session
,
2963 DEFAULT_SNAPSHOT_MAX_SIZE
,
2964 NULL
, uris
, uri_count
, session
->consumer
,
2965 new_output
, &session
->snapshot
);
2967 ret_code
= (ret
== -ENOMEM
) ?
2968 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2969 snapshot_output_destroy(new_output
);
2972 snapshot_add_output(&session
->snapshot
, new_output
);
2975 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2976 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2978 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2982 ret_code
= LTTNG_ERR_INVALID
;
2990 enum lttng_error_code
cmd_create_session_from_descriptor(
2991 struct lttng_session_descriptor
*descriptor
,
2992 const lttng_sock_cred
*creds
,
2993 const char *home_path
)
2996 enum lttng_error_code ret_code
;
2997 const char *session_name
;
2998 struct ltt_session
*new_session
= NULL
;
2999 enum lttng_session_descriptor_status descriptor_status
;
3001 session_lock_list();
3003 if (*home_path
!= '/') {
3004 ERR("Home path provided by client is not absolute");
3005 ret_code
= LTTNG_ERR_INVALID
;
3010 descriptor_status
= lttng_session_descriptor_get_session_name(
3011 descriptor
, &session_name
);
3012 switch (descriptor_status
) {
3013 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3015 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3016 session_name
= NULL
;
3019 ret_code
= LTTNG_ERR_INVALID
;
3023 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3025 if (ret_code
!= LTTNG_OK
) {
3029 if (!session_name
) {
3030 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3033 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3038 if (!lttng_session_descriptor_is_output_destination_initialized(
3041 * Only include the session's creation time in the output
3042 * destination if the name of the session itself was
3043 * not auto-generated.
3045 ret_code
= lttng_session_descriptor_set_default_output(
3047 session_name
? &new_session
->creation_time
: NULL
,
3049 if (ret_code
!= LTTNG_OK
) {
3053 new_session
->has_user_specified_directory
=
3054 lttng_session_descriptor_has_output_directory(
3058 switch (lttng_session_descriptor_get_type(descriptor
)) {
3059 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3060 new_session
->snapshot_mode
= 1;
3062 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3063 new_session
->live_timer
=
3064 lttng_session_descriptor_live_get_timer_interval(
3071 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3072 if (ret_code
!= LTTNG_OK
) {
3075 new_session
->consumer
->enabled
= 1;
3076 ret_code
= LTTNG_OK
;
3078 /* Release reference provided by the session_create function. */
3079 session_put(new_session
);
3080 if (ret_code
!= LTTNG_OK
&& new_session
) {
3081 /* Release the global reference on error. */
3082 session_destroy(new_session
);
3084 session_unlock_list();
3088 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3089 struct lttng_session_descriptor
**return_descriptor
)
3092 size_t payload_size
;
3093 struct lttng_dynamic_buffer payload
;
3094 struct lttng_buffer_view home_dir_view
;
3095 struct lttng_buffer_view session_descriptor_view
;
3096 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3097 enum lttng_error_code ret_code
;
3099 lttng_dynamic_buffer_init(&payload
);
3100 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3102 ret_code
= LTTNG_ERR_INVALID
;
3105 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3106 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3107 ret_code
= LTTNG_ERR_INVALID
;
3111 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3112 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3113 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3115 ret_code
= LTTNG_ERR_NOMEM
;
3119 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3121 ERR("Reception of session descriptor failed, aborting.");
3122 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3126 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3129 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3130 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3131 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3132 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3133 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3137 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3139 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3140 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3141 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3142 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3143 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3147 ret
= lttng_session_descriptor_create_from_buffer(
3148 &session_descriptor_view
, &session_descriptor
);
3150 ERR("Failed to create session descriptor from payload of \"create session\" command");
3151 ret_code
= LTTNG_ERR_INVALID
;
3156 * Sets the descriptor's auto-generated properties (name, output) if
3159 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3161 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3162 if (ret_code
!= LTTNG_OK
) {
3166 ret_code
= LTTNG_OK
;
3167 *return_descriptor
= session_descriptor
;
3168 session_descriptor
= NULL
;
3170 lttng_dynamic_buffer_reset(&payload
);
3171 lttng_session_descriptor_destroy(session_descriptor
);
3176 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3177 void *_reply_context
)
3181 const struct cmd_destroy_session_reply_context
*reply_context
=
3183 struct lttng_dynamic_buffer payload
;
3184 struct lttcomm_session_destroy_command_header cmd_header
;
3185 struct lttng_trace_archive_location
*location
= NULL
;
3186 struct lttcomm_lttng_msg llm
= {
3187 .cmd_type
= LTTNG_DESTROY_SESSION
,
3188 .ret_code
= reply_context
->destruction_status
,
3191 sizeof(struct lttcomm_session_destroy_command_header
),
3194 size_t payload_size_before_location
;
3196 lttng_dynamic_buffer_init(&payload
);
3198 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3200 ERR("Failed to append session destruction message");
3204 cmd_header
.rotation_state
=
3205 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3206 session
->rotation_state
:
3207 LTTNG_ROTATION_STATE_NO_ROTATION
);
3208 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3209 sizeof(cmd_header
));
3211 ERR("Failed to append session destruction command header");
3215 if (!reply_context
->implicit_rotation_on_destroy
) {
3216 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3220 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3221 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3226 location
= session_get_trace_archive_location(session
);
3228 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3233 payload_size_before_location
= payload
.size
;
3234 comm_ret
= lttng_trace_archive_location_serialize(location
,
3237 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3241 /* Update the message to indicate the location's length. */
3242 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3243 payload
.size
- payload_size_before_location
;
3245 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3246 payload
.data
, payload
.size
);
3247 if (comm_ret
!= (ssize_t
) payload
.size
) {
3248 ERR("Failed to send result of the destruction of session \"%s\" to client",
3252 ret
= close(reply_context
->reply_sock_fd
);
3254 PERROR("Failed to close client socket in deferred session destroy reply");
3256 lttng_dynamic_buffer_reset(&payload
);
3257 free(_reply_context
);
3261 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3263 * Called with session lock held.
3265 int cmd_destroy_session(struct ltt_session
*session
,
3266 struct notification_thread_handle
*notification_thread_handle
,
3270 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3271 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3274 reply_context
= zmalloc(sizeof(*reply_context
));
3275 if (!reply_context
) {
3276 ret
= LTTNG_ERR_NOMEM
;
3279 reply_context
->reply_sock_fd
= *sock_fd
;
3285 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3287 if (session
->active
) {
3288 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3290 ret
= cmd_stop_trace(session
);
3291 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3292 /* Carry on with the destruction of the session. */
3293 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3294 session
->name
, lttng_strerror(-ret
));
3295 destruction_last_error
= ret
;
3299 if (session
->rotation_schedule_timer_enabled
) {
3300 if (timer_session_rotation_schedule_timer_stop(
3302 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3304 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3308 if (session
->rotate_size
) {
3309 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3310 session
->rotate_size
= 0;
3313 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3315 * Perform a last rotation on destruction if rotations have
3316 * occurred during the session's lifetime.
3318 ret
= cmd_rotate_session(session
, NULL
, false,
3319 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3320 if (ret
!= LTTNG_OK
) {
3321 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3322 session
->name
, lttng_strerror(-ret
));
3323 destruction_last_error
= -ret
;
3325 if (reply_context
) {
3326 reply_context
->implicit_rotation_on_destroy
= true;
3328 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3330 * The user has not triggered a session rotation. However, to
3331 * ensure all data has been consumed, the session is rotated
3332 * to a 'null' trace chunk before it is destroyed.
3334 * This is a "quiet" rotation meaning that no notification is
3335 * emitted and no renaming of the current trace chunk takes
3338 ret
= cmd_rotate_session(session
, NULL
, true,
3339 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3341 * Rotation operations may not be supported by the kernel
3342 * tracer. Hence, do not consider this implicit rotation as
3343 * a session destruction error. The library has already stopped
3344 * the session and waited for pending data; there is nothing
3345 * left to do but complete the destruction of the session.
3347 if (ret
!= LTTNG_OK
&&
3348 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3349 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3350 session
->name
, lttng_strerror(ret
));
3351 destruction_last_error
= -ret
;
3355 if (session
->shm_path
[0]) {
3357 * When a session is created with an explicit shm_path,
3358 * the consumer daemon will create its shared memory files
3359 * at that location and will *not* unlink them. This is normal
3360 * as the intention of that feature is to make it possible
3361 * to retrieve the content of those files should a crash occur.
3363 * To ensure the content of those files can be used, the
3364 * sessiond daemon will replicate the content of the metadata
3365 * cache in a metadata file.
3367 * On clean-up, it is expected that the consumer daemon will
3368 * unlink the shared memory files and that the session daemon
3369 * will unlink the metadata file. Then, the session's directory
3370 * in the shm path can be removed.
3372 * Unfortunately, a flaw in the design of the sessiond's and
3373 * consumerd's tear down of channels makes it impossible to
3374 * determine when the sessiond _and_ the consumerd have both
3375 * destroyed their representation of a channel. For one, the
3376 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3377 * callbacks in both daemons.
3379 * However, it is also impossible for the sessiond to know when
3380 * the consumer daemon is done destroying its channel(s) since
3381 * it occurs as a reaction to the closing of the channel's file
3382 * descriptor. There is no resulting communication initiated
3383 * from the consumerd to the sessiond to confirm that the
3384 * operation is completed (and was successful).
3386 * Until this is all fixed, the session daemon checks for the
3387 * removal of the session's shm path which makes it possible
3388 * to safely advertise a session as having been destroyed.
3390 * Prior to this fix, it was not possible to reliably save
3391 * a session making use of the --shm-path option, destroy it,
3392 * and load it again. This is because the creation of the
3393 * session would fail upon seeing the session's shm path
3394 * already in existence.
3396 * Note that none of the error paths in the check for the
3397 * directory's existence return an error. This is normal
3398 * as there isn't much that can be done. The session will
3399 * be destroyed properly, except that we can't offer the
3400 * guarantee that the same session can be re-created.
3402 current_completion_handler
= &destroy_completion_handler
.handler
;
3403 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3405 sizeof(destroy_completion_handler
.shm_path
));
3410 * The session is destroyed. However, note that the command context
3411 * still holds a reference to the session, thus delaying its destruction
3412 * _at least_ up to the point when that reference is released.
3414 session_destroy(session
);
3415 if (reply_context
) {
3416 reply_context
->destruction_status
= destruction_last_error
;
3417 ret
= session_add_destroy_notifier(session
,
3418 cmd_destroy_session_reply
,
3419 (void *) reply_context
);
3421 ret
= LTTNG_ERR_FATAL
;
3433 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3435 int cmd_register_consumer(struct ltt_session
*session
,
3436 enum lttng_domain_type domain
, const char *sock_path
,
3437 struct consumer_data
*cdata
)
3440 struct consumer_socket
*socket
= NULL
;
3447 case LTTNG_DOMAIN_KERNEL
:
3449 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3453 /* Can't register a consumer if there is already one */
3454 if (ksess
->consumer_fds_sent
!= 0) {
3455 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3459 sock
= lttcomm_connect_unix_sock(sock_path
);
3461 ret
= LTTNG_ERR_CONNECT_FAIL
;
3464 cdata
->cmd_sock
= sock
;
3466 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3467 if (socket
== NULL
) {
3470 PERROR("close register consumer");
3472 cdata
->cmd_sock
= -1;
3473 ret
= LTTNG_ERR_FATAL
;
3477 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3478 if (socket
->lock
== NULL
) {
3479 PERROR("zmalloc pthread mutex");
3480 ret
= LTTNG_ERR_FATAL
;
3483 pthread_mutex_init(socket
->lock
, NULL
);
3484 socket
->registered
= 1;
3487 consumer_add_socket(socket
, ksess
->consumer
);
3490 pthread_mutex_lock(&cdata
->pid_mutex
);
3492 pthread_mutex_unlock(&cdata
->pid_mutex
);
3497 /* TODO: Userspace tracing */
3498 ret
= LTTNG_ERR_UND
;
3506 consumer_destroy_socket(socket
);
3512 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3514 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3515 struct lttng_domain
**domains
)
3520 struct lttng_ht_iter iter
;
3522 if (session
->kernel_session
!= NULL
) {
3523 DBG3("Listing domains found kernel domain");
3527 if (session
->ust_session
!= NULL
) {
3528 DBG3("Listing domains found UST global domain");
3532 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3534 if (agt
->being_used
) {
3545 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3546 if (*domains
== NULL
) {
3547 ret
= LTTNG_ERR_FATAL
;
3551 if (session
->kernel_session
!= NULL
) {
3552 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3554 /* Kernel session buffer type is always GLOBAL */
3555 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3560 if (session
->ust_session
!= NULL
) {
3561 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3562 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3566 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3568 if (agt
->being_used
) {
3569 (*domains
)[index
].type
= agt
->domain
;
3570 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3580 /* Return negative value to differentiate return code */
3586 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3588 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3589 struct ltt_session
*session
, struct lttng_channel
**channels
)
3591 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3594 case LTTNG_DOMAIN_KERNEL
:
3595 if (session
->kernel_session
!= NULL
) {
3596 nb_chan
= session
->kernel_session
->channel_count
;
3598 DBG3("Number of kernel channels %zd", nb_chan
);
3600 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3604 case LTTNG_DOMAIN_UST
:
3605 if (session
->ust_session
!= NULL
) {
3607 nb_chan
= lttng_ht_get_count(
3608 session
->ust_session
->domain_global
.channels
);
3611 DBG3("Number of UST global channels %zd", nb_chan
);
3613 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3618 ret
= -LTTNG_ERR_UND
;
3623 const size_t channel_size
= sizeof(struct lttng_channel
) +
3624 sizeof(struct lttng_channel_extended
);
3625 struct lttng_channel_extended
*channel_exts
;
3627 payload_size
= nb_chan
* channel_size
;
3628 *channels
= zmalloc(payload_size
);
3629 if (*channels
== NULL
) {
3630 ret
= -LTTNG_ERR_FATAL
;
3634 channel_exts
= ((void *) *channels
) +
3635 (nb_chan
* sizeof(struct lttng_channel
));
3636 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3637 if (ret
!= LTTNG_OK
) {
3652 * Command LTTNG_LIST_EVENTS processed by the client thread.
3654 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3655 struct ltt_session
*session
, char *channel_name
,
3656 struct lttng_payload
*payload
)
3659 ssize_t nb_events
= 0;
3660 struct lttcomm_event_command_header cmd_header
= {};
3661 const size_t cmd_header_offset
= payload
->buffer
.size
;
3663 ret
= lttng_dynamic_buffer_append(
3664 &payload
->buffer
, &cmd_header
, sizeof(cmd_header
));
3666 ret
= LTTNG_ERR_NOMEM
;
3671 case LTTNG_DOMAIN_KERNEL
:
3672 if (session
->kernel_session
!= NULL
) {
3673 nb_events
= list_lttng_kernel_events(channel_name
,
3674 session
->kernel_session
, payload
);
3677 case LTTNG_DOMAIN_UST
:
3679 if (session
->ust_session
!= NULL
) {
3680 nb_events
= list_lttng_ust_global_events(channel_name
,
3681 &session
->ust_session
->domain_global
,
3686 case LTTNG_DOMAIN_LOG4J
:
3687 case LTTNG_DOMAIN_JUL
:
3688 case LTTNG_DOMAIN_PYTHON
:
3689 if (session
->ust_session
) {
3690 struct lttng_ht_iter iter
;
3694 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3695 &iter
.iter
, agt
, node
.node
) {
3696 if (agt
->domain
== domain
) {
3697 nb_events
= list_lttng_agent_events(
3706 ret
= LTTNG_ERR_UND
;
3710 ((struct lttcomm_event_command_header
*) (payload
->buffer
.data
+
3711 cmd_header_offset
))->nb_events
= (uint32_t) nb_events
;
3716 /* Return negative value to differentiate return code */
3721 * Using the session list, filled a lttng_session array to send back to the
3722 * client for session listing.
3724 * The session list lock MUST be acquired before calling this function. Use
3725 * session_lock_list() and session_unlock_list().
3727 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3728 size_t session_count
, uid_t uid
, gid_t gid
)
3732 struct ltt_session
*session
;
3733 struct ltt_session_list
*list
= session_get_list();
3734 struct lttng_session_extended
*extended
=
3735 (typeof(extended
)) (&sessions
[session_count
]);
3737 DBG("Getting all available session for UID %d GID %d",
3740 * Iterate over session list and append data after the control struct in
3743 cds_list_for_each_entry(session
, &list
->head
, list
) {
3744 if (!session_get(session
)) {
3748 * Only list the sessions the user can control.
3750 if (!session_access_ok(session
, uid
) ||
3751 session
->destroyed
) {
3752 session_put(session
);
3756 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3757 struct ltt_ust_session
*usess
= session
->ust_session
;
3759 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3760 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3761 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3762 ret
= build_network_session_path(sessions
[i
].path
,
3763 sizeof(sessions
[i
].path
), session
);
3765 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3766 session
->consumer
->dst
.session_root_path
);
3769 PERROR("snprintf session path");
3770 session_put(session
);
3774 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3775 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3776 sessions
[i
].enabled
= session
->active
;
3777 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3778 sessions
[i
].live_timer_interval
= session
->live_timer
;
3779 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3780 extended
[i
].creation_time
.is_set
= 1;
3782 session_put(session
);
3787 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3788 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3790 int cmd_data_pending(struct ltt_session
*session
)
3793 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3794 struct ltt_ust_session
*usess
= session
->ust_session
;
3798 DBG("Data pending for session %s", session
->name
);
3800 /* Session MUST be stopped to ask for data availability. */
3801 if (session
->active
) {
3802 ret
= LTTNG_ERR_SESSION_STARTED
;
3806 * If stopped, just make sure we've started before else the above call
3807 * will always send that there is data pending.
3809 * The consumer assumes that when the data pending command is received,
3810 * the trace has been started before or else no output data is written
3811 * by the streams which is a condition for data pending. So, this is
3812 * *VERY* important that we don't ask the consumer before a start
3815 if (!session
->has_been_started
) {
3821 /* A rotation is still pending, we have to wait. */
3822 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3823 DBG("Rotate still pending for session %s", session
->name
);
3828 if (ksess
&& ksess
->consumer
) {
3829 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3831 /* Data is still being extracted for the kernel. */
3836 if (usess
&& usess
->consumer
) {
3837 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3839 /* Data is still being extracted for the kernel. */
3844 /* Data is ready to be read by a viewer */
3852 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3854 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3856 int cmd_snapshot_add_output(struct ltt_session
*session
,
3857 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3860 struct snapshot_output
*new_output
;
3865 DBG("Cmd snapshot add output for session %s", session
->name
);
3868 * Can't create an output if the session is not set in no-output mode.
3870 if (session
->output_traces
) {
3871 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3875 if (session
->has_non_mmap_channel
) {
3876 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3880 /* Only one output is allowed until we have the "tee" feature. */
3881 if (session
->snapshot
.nb_output
== 1) {
3882 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3886 new_output
= snapshot_output_alloc();
3888 ret
= LTTNG_ERR_NOMEM
;
3892 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3893 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3894 &session
->snapshot
);
3896 if (ret
== -ENOMEM
) {
3897 ret
= LTTNG_ERR_NOMEM
;
3899 ret
= LTTNG_ERR_INVALID
;
3905 snapshot_add_output(&session
->snapshot
, new_output
);
3907 *id
= new_output
->id
;
3914 snapshot_output_destroy(new_output
);
3920 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3922 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3924 int cmd_snapshot_del_output(struct ltt_session
*session
,
3925 const struct lttng_snapshot_output
*output
)
3928 struct snapshot_output
*sout
= NULL
;
3936 * Permission denied to create an output if the session is not
3937 * set in no output mode.
3939 if (session
->output_traces
) {
3940 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3945 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3947 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3948 } else if (*output
->name
!= '\0') {
3949 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3951 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3954 ret
= LTTNG_ERR_INVALID
;
3958 snapshot_delete_output(&session
->snapshot
, sout
);
3959 snapshot_output_destroy(sout
);
3968 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3970 * If no output is available, outputs is untouched and 0 is returned.
3972 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3974 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3975 struct lttng_snapshot_output
**outputs
)
3978 struct lttng_snapshot_output
*list
= NULL
;
3979 struct lttng_ht_iter iter
;
3980 struct snapshot_output
*output
;
3985 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3988 * Permission denied to create an output if the session is not
3989 * set in no output mode.
3991 if (session
->output_traces
) {
3992 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3996 if (session
->snapshot
.nb_output
== 0) {
4001 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4003 ret
= -LTTNG_ERR_NOMEM
;
4007 /* Copy list from session to the new list object. */
4009 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4010 output
, node
.node
) {
4011 assert(output
->consumer
);
4012 list
[idx
].id
= output
->id
;
4013 list
[idx
].max_size
= output
->max_size
;
4014 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4015 sizeof(list
[idx
].name
))) {
4016 ret
= -LTTNG_ERR_INVALID
;
4019 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4020 if (lttng_strncpy(list
[idx
].ctrl_url
,
4021 output
->consumer
->dst
.session_root_path
,
4022 sizeof(list
[idx
].ctrl_url
))) {
4023 ret
= -LTTNG_ERR_INVALID
;
4028 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4029 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4031 ret
= -LTTNG_ERR_NOMEM
;
4036 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4037 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4039 ret
= -LTTNG_ERR_NOMEM
;
4048 ret
= session
->snapshot
.nb_output
;
4057 * Check if we can regenerate the metadata for this session.
4058 * Only kernel, UST per-uid and non-live sessions are supported.
4060 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4063 int check_regenerate_metadata_support(struct ltt_session
*session
)
4069 if (session
->live_timer
!= 0) {
4070 ret
= LTTNG_ERR_LIVE_SESSION
;
4073 if (!session
->active
) {
4074 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4077 if (session
->ust_session
) {
4078 switch (session
->ust_session
->buffer_type
) {
4079 case LTTNG_BUFFER_PER_UID
:
4081 case LTTNG_BUFFER_PER_PID
:
4082 ret
= LTTNG_ERR_PER_PID_SESSION
;
4086 ret
= LTTNG_ERR_UNK
;
4090 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4091 session
->consumer
->relay_minor_version
< 8) {
4092 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4102 int clear_metadata_file(int fd
)
4107 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4108 if (lseek_ret
< 0) {
4114 ret
= ftruncate(fd
, 0);
4116 PERROR("ftruncate");
4125 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4128 struct buffer_reg_uid
*uid_reg
= NULL
;
4129 struct buffer_reg_session
*session_reg
= NULL
;
4132 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4133 struct ust_registry_session
*registry
;
4134 struct ust_registry_channel
*chan
;
4135 struct lttng_ht_iter iter_chan
;
4137 session_reg
= uid_reg
->registry
;
4138 registry
= session_reg
->reg
.ust
;
4140 pthread_mutex_lock(®istry
->lock
);
4141 registry
->metadata_len_sent
= 0;
4142 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4143 registry
->metadata_len
= 0;
4144 registry
->metadata_version
++;
4145 if (registry
->metadata_fd
> 0) {
4146 /* Clear the metadata file's content. */
4147 ret
= clear_metadata_file(registry
->metadata_fd
);
4149 pthread_mutex_unlock(®istry
->lock
);
4154 ret
= ust_metadata_session_statedump(registry
, NULL
,
4155 registry
->major
, registry
->minor
);
4157 pthread_mutex_unlock(®istry
->lock
);
4158 ERR("Failed to generate session metadata (err = %d)",
4162 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4164 struct ust_registry_event
*event
;
4165 struct lttng_ht_iter iter_event
;
4167 ret
= ust_metadata_channel_statedump(registry
, chan
);
4169 pthread_mutex_unlock(®istry
->lock
);
4170 ERR("Failed to generate channel metadata "
4174 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4176 ret
= ust_metadata_event_statedump(registry
,
4179 pthread_mutex_unlock(®istry
->lock
);
4180 ERR("Failed to generate event metadata "
4186 pthread_mutex_unlock(®istry
->lock
);
4195 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4197 * Ask the consumer to truncate the existing metadata file(s) and
4198 * then regenerate the metadata. Live and per-pid sessions are not
4199 * supported and return an error.
4201 * Return 0 on success or else a LTTNG_ERR code.
4203 int cmd_regenerate_metadata(struct ltt_session
*session
)
4209 ret
= check_regenerate_metadata_support(session
);
4214 if (session
->kernel_session
) {
4215 ret
= kernctl_session_regenerate_metadata(
4216 session
->kernel_session
->fd
);
4218 ERR("Failed to regenerate the kernel metadata");
4223 if (session
->ust_session
) {
4224 ret
= ust_regenerate_metadata(session
->ust_session
);
4226 ERR("Failed to regenerate the UST metadata");
4230 DBG("Cmd metadata regenerate for session %s", session
->name
);
4238 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4240 * Ask the tracer to regenerate a new statedump.
4242 * Return 0 on success or else a LTTNG_ERR code.
4244 int cmd_regenerate_statedump(struct ltt_session
*session
)
4250 if (!session
->active
) {
4251 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4255 if (session
->kernel_session
) {
4256 ret
= kernctl_session_regenerate_statedump(
4257 session
->kernel_session
->fd
);
4259 * Currently, the statedump in kernel can only fail if out
4263 if (ret
== -ENOMEM
) {
4264 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4266 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4268 ERR("Failed to regenerate the kernel statedump");
4273 if (session
->ust_session
) {
4274 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4276 * Currently, the statedump in UST always returns 0.
4279 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4280 ERR("Failed to regenerate the UST statedump");
4284 DBG("Cmd regenerate statedump for session %s", session
->name
);
4291 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4292 struct notification_thread_handle
*notification_thread
,
4293 struct lttng_trigger
**return_trigger
)
4297 ssize_t sock_recv_len
;
4298 struct lttng_trigger
*trigger
= NULL
;
4299 struct lttng_payload trigger_payload
;
4300 struct lttng_credentials cmd_creds
= {
4301 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
4302 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
4305 lttng_payload_init(&trigger_payload
);
4306 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
4307 ret
= lttng_dynamic_buffer_set_size(
4308 &trigger_payload
.buffer
, trigger_len
);
4310 ret
= LTTNG_ERR_NOMEM
;
4314 sock_recv_len
= lttcomm_recv_unix_sock(
4315 sock
, trigger_payload
.buffer
.data
, trigger_len
);
4316 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4317 ERR("Failed to receive \"register trigger\" command payload");
4318 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
4322 /* Receive fds, if any. */
4323 if (cmd_ctx
->lsm
.fd_count
> 0) {
4324 ret
= lttcomm_recv_payload_fds_unix_sock(
4325 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
4326 if (ret
> 0 && ret
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
4327 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
4329 } else if (ret
<= 0) {
4330 ret
= LTTNG_ERR_FATAL
;
4335 /* Deserialize trigger. */
4337 struct lttng_payload_view view
=
4338 lttng_payload_view_from_payload(
4339 &trigger_payload
, 0, -1);
4341 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
4343 ERR("Invalid trigger payload received in \"register trigger\" command");
4344 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4350 * Validate the trigger credentials against the command credentials.
4351 * Only the root user can register a trigger with non-matching
4354 if (!lttng_credentials_is_equal_uid(
4355 lttng_trigger_get_credentials(trigger
),
4357 if (lttng_credentials_get_uid(&cmd_creds
) != 0) {
4358 ERR("Trigger credentials do not match the command credentials");
4359 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4365 * The bytecode generation also serves as a validation step for the
4366 * bytecode expressions.
4368 ret
= lttng_trigger_generate_bytecode(trigger
, &cmd_creds
);
4369 if (ret
!= LTTNG_OK
) {
4374 * A reference to the trigger is acquired by the notification thread.
4375 * It is safe to return the same trigger to the caller since it the
4376 * other user holds a reference.
4378 * The trigger is modified during the execution of the
4379 * "register trigger" command. However, by the time the command returns,
4380 * it is safe to use without any locking as its properties are
4383 ret
= notification_thread_command_register_trigger(notification_thread
,
4385 if (ret
!= LTTNG_OK
) {
4386 goto end_notification_thread
;
4389 /* Return an updated trigger to the client. */
4390 *return_trigger
= trigger
;
4392 end_notification_thread
:
4393 /* Ownership of trigger was transferred. */
4396 lttng_trigger_destroy(trigger
);
4397 lttng_payload_reset(&trigger_payload
);
4401 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4402 struct notification_thread_handle
*notification_thread
)
4406 ssize_t sock_recv_len
;
4407 struct lttng_trigger
*trigger
= NULL
;
4408 struct lttng_payload trigger_payload
;
4409 struct lttng_credentials cmd_creds
= {
4410 .uid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.uid
),
4411 .gid
= LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx
->creds
.gid
),
4414 lttng_payload_init(&trigger_payload
);
4415 trigger_len
= (size_t) cmd_ctx
->lsm
.u
.trigger
.length
;
4416 ret
= lttng_dynamic_buffer_set_size(
4417 &trigger_payload
.buffer
, trigger_len
);
4419 ret
= LTTNG_ERR_NOMEM
;
4423 sock_recv_len
= lttcomm_recv_unix_sock(
4424 sock
, trigger_payload
.buffer
.data
, trigger_len
);
4425 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4426 ERR("Failed to receive \"unregister trigger\" command payload");
4427 /* TODO: should this be a new error enum ? */
4428 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4432 /* Receive fds, if any. */
4433 if (cmd_ctx
->lsm
.fd_count
> 0) {
4434 ret
= lttcomm_recv_payload_fds_unix_sock(
4435 sock
, cmd_ctx
->lsm
.fd_count
, &trigger_payload
);
4436 if (ret
> 0 && ret
!= cmd_ctx
->lsm
.fd_count
* sizeof(int)) {
4437 ret
= LTTNG_ERR_INVALID_PROTOCOL
;
4439 } else if (ret
<= 0) {
4440 ret
= LTTNG_ERR_FATAL
;
4446 struct lttng_payload_view view
=
4447 lttng_payload_view_from_payload(
4448 &trigger_payload
, 0, -1);
4450 if (lttng_trigger_create_from_payload(&view
, &trigger
) !=
4452 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4453 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4459 * Validate the trigger credentials against the command credentials.
4460 * Only the root user can unregister a trigger with non-matching
4463 if (!lttng_credentials_is_equal_uid(
4464 lttng_trigger_get_credentials(trigger
),
4466 if (lttng_credentials_get_uid(&cmd_creds
) != 0) {
4467 ERR("Trigger credentials do not match the command credentials");
4468 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4473 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4476 lttng_trigger_destroy(trigger
);
4477 lttng_payload_reset(&trigger_payload
);
4481 int cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4482 struct notification_thread_handle
*notification_thread
,
4483 struct lttng_triggers
**return_triggers
)
4486 enum lttng_error_code ret_code
;
4487 struct lttng_triggers
*triggers
= NULL
;
4489 /* Get the set of triggers from the notification thread. */
4490 ret_code
= notification_thread_command_list_triggers(
4491 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4492 if (ret_code
!= LTTNG_OK
) {
4497 *return_triggers
= triggers
;
4501 lttng_triggers_destroy(triggers
);
4505 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4506 * snapshot output is *not* set with a remote destination.
4508 * Return LTTNG_OK on success or a LTTNG_ERR code.
4510 static enum lttng_error_code
set_relayd_for_snapshot(
4511 struct consumer_output
*output
,
4512 const struct ltt_session
*session
)
4514 enum lttng_error_code status
= LTTNG_OK
;
4515 struct lttng_ht_iter iter
;
4516 struct consumer_socket
*socket
;
4517 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4518 const char *base_path
;
4523 DBG2("Set relayd object from snapshot output");
4525 if (session
->current_trace_chunk
) {
4526 enum lttng_trace_chunk_status chunk_status
=
4527 lttng_trace_chunk_get_id(
4528 session
->current_trace_chunk
,
4529 ¤t_chunk_id
.value
);
4531 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4532 current_chunk_id
.is_set
= true;
4534 ERR("Failed to get current trace chunk id");
4535 status
= LTTNG_ERR_UNK
;
4540 /* Ignore if snapshot consumer output is not network. */
4541 if (output
->type
!= CONSUMER_DST_NET
) {
4546 * The snapshot record URI base path overrides the session
4549 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4550 base_path
= output
->dst
.net
.control
.subdir
;
4552 base_path
= session
->base_path
;
4556 * For each consumer socket, create and send the relayd object of the
4560 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4561 socket
, node
.node
) {
4562 pthread_mutex_lock(socket
->lock
);
4563 status
= send_consumer_relayd_sockets(0, session
->id
,
4565 session
->name
, session
->hostname
,
4567 session
->live_timer
,
4568 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4569 session
->creation_time
,
4570 session
->name_contains_creation_time
);
4571 pthread_mutex_unlock(socket
->lock
);
4572 if (status
!= LTTNG_OK
) {
4584 * Record a kernel snapshot.
4586 * Return LTTNG_OK on success or a LTTNG_ERR code.
4588 static enum lttng_error_code
record_kernel_snapshot(
4589 struct ltt_kernel_session
*ksess
,
4590 const struct consumer_output
*output
,
4591 const struct ltt_session
*session
,
4592 int wait
, uint64_t nb_packets_per_stream
)
4594 enum lttng_error_code status
;
4600 status
= kernel_snapshot_record(
4601 ksess
, output
, wait
, nb_packets_per_stream
);
4606 * Record a UST snapshot.
4608 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4610 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4611 const struct consumer_output
*output
,
4612 const struct ltt_session
*session
,
4613 int wait
, uint64_t nb_packets_per_stream
)
4615 enum lttng_error_code status
;
4621 status
= ust_app_snapshot_record(
4622 usess
, output
, wait
, nb_packets_per_stream
);
4627 uint64_t get_session_size_one_more_packet_per_stream(
4628 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4630 uint64_t tot_size
= 0;
4632 if (session
->kernel_session
) {
4633 struct ltt_kernel_channel
*chan
;
4634 const struct ltt_kernel_session
*ksess
=
4635 session
->kernel_session
;
4637 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4638 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4640 * Don't take channel into account if we
4641 * already grab all its packets.
4645 tot_size
+= chan
->channel
->attr
.subbuf_size
4646 * chan
->stream_count
;
4650 if (session
->ust_session
) {
4651 const struct ltt_ust_session
*usess
= session
->ust_session
;
4653 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4661 * Calculate the number of packets we can grab from each stream that
4662 * fits within the overall snapshot max size.
4664 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4665 * the number of packets per stream.
4667 * TODO: this approach is not perfect: we consider the worse case
4668 * (packet filling the sub-buffers) as an upper bound, but we could do
4669 * better if we do this calculation while we actually grab the packet
4670 * content: we would know how much padding we don't actually store into
4673 * This algorithm is currently bounded by the number of packets per
4676 * Since we call this algorithm before actually grabbing the data, it's
4677 * an approximation: for instance, applications could appear/disappear
4678 * in between this call and actually grabbing data.
4681 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4685 uint64_t cur_nb_packets
= 0;
4688 return 0; /* Infinite */
4691 size_left
= max_size
;
4693 uint64_t one_more_packet_tot_size
;
4695 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4696 session
, cur_nb_packets
);
4697 if (!one_more_packet_tot_size
) {
4698 /* We are already grabbing all packets. */
4701 size_left
-= one_more_packet_tot_size
;
4702 if (size_left
< 0) {
4707 if (!cur_nb_packets
&& size_left
!= max_size
) {
4708 /* Not enough room to grab one packet of each stream, error. */
4711 return cur_nb_packets
;
4715 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4716 const struct snapshot_output
*snapshot_output
, int wait
)
4718 int64_t nb_packets_per_stream
;
4719 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4721 enum lttng_error_code ret_code
= LTTNG_OK
;
4722 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4723 struct consumer_output
*original_ust_consumer_output
= NULL
;
4724 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4725 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4726 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4728 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4730 snapshot_output
->name
,
4731 snapshot_output
->datetime
,
4732 snapshot_output
->nb_snapshot
);
4733 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4734 ERR("Failed to format snapshot name");
4735 ret_code
= LTTNG_ERR_INVALID
;
4738 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4739 snapshot_output
->name
, session
->name
,
4740 snapshot_chunk_name
);
4741 if (!session
->kernel_session
&& !session
->ust_session
) {
4742 ERR("Failed to record snapshot as no channels exist");
4743 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4747 if (session
->kernel_session
) {
4748 original_kernel_consumer_output
=
4749 session
->kernel_session
->consumer
;
4750 snapshot_kernel_consumer_output
=
4751 consumer_copy_output(snapshot_output
->consumer
);
4752 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
4753 snapshot_chunk_name
);
4754 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4755 original_kernel_consumer_output
);
4757 ERR("Failed to copy consumer sockets from snapshot output configuration");
4758 ret_code
= LTTNG_ERR_NOMEM
;
4761 ret_code
= set_relayd_for_snapshot(
4762 snapshot_kernel_consumer_output
, session
);
4763 if (ret_code
!= LTTNG_OK
) {
4764 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4767 session
->kernel_session
->consumer
=
4768 snapshot_kernel_consumer_output
;
4770 if (session
->ust_session
) {
4771 original_ust_consumer_output
= session
->ust_session
->consumer
;
4772 snapshot_ust_consumer_output
=
4773 consumer_copy_output(snapshot_output
->consumer
);
4774 strcpy(snapshot_ust_consumer_output
->chunk_path
,
4775 snapshot_chunk_name
);
4776 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4777 original_ust_consumer_output
);
4779 ERR("Failed to copy consumer sockets from snapshot output configuration");
4780 ret_code
= LTTNG_ERR_NOMEM
;
4783 ret_code
= set_relayd_for_snapshot(
4784 snapshot_ust_consumer_output
, session
);
4785 if (ret_code
!= LTTNG_OK
) {
4786 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4789 session
->ust_session
->consumer
=
4790 snapshot_ust_consumer_output
;
4793 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4794 snapshot_kernel_consumer_output
?:
4795 snapshot_ust_consumer_output
,
4796 consumer_output_get_base_path(
4797 snapshot_output
->consumer
),
4798 snapshot_chunk_name
);
4799 if (!snapshot_trace_chunk
) {
4800 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4802 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4805 assert(!session
->current_trace_chunk
);
4806 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4807 lttng_trace_chunk_put(snapshot_trace_chunk
);
4808 snapshot_trace_chunk
= NULL
;
4810 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4812 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4816 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4817 snapshot_output
->max_size
);
4818 if (nb_packets_per_stream
< 0) {
4819 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4820 goto error_close_trace_chunk
;
4823 if (session
->kernel_session
) {
4824 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4825 snapshot_kernel_consumer_output
, session
,
4826 wait
, nb_packets_per_stream
);
4827 if (ret_code
!= LTTNG_OK
) {
4828 goto error_close_trace_chunk
;
4832 if (session
->ust_session
) {
4833 ret_code
= record_ust_snapshot(session
->ust_session
,
4834 snapshot_ust_consumer_output
, session
,
4835 wait
, nb_packets_per_stream
);
4836 if (ret_code
!= LTTNG_OK
) {
4837 goto error_close_trace_chunk
;
4841 error_close_trace_chunk
:
4842 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
4843 ERR("Failed to release the current trace chunk of session \"%s\"",
4845 ret_code
= LTTNG_ERR_UNK
;
4848 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
4849 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
4851 * Don't goto end; make sure the chunk is closed for the session
4852 * to allow future snapshots.
4854 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4856 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4859 if (original_ust_consumer_output
) {
4860 session
->ust_session
->consumer
= original_ust_consumer_output
;
4862 if (original_kernel_consumer_output
) {
4863 session
->kernel_session
->consumer
=
4864 original_kernel_consumer_output
;
4866 consumer_output_put(snapshot_ust_consumer_output
);
4867 consumer_output_put(snapshot_kernel_consumer_output
);
4872 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4874 * The wait parameter is ignored so this call always wait for the snapshot to
4875 * complete before returning.
4877 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4879 int cmd_snapshot_record(struct ltt_session
*session
,
4880 const struct lttng_snapshot_output
*output
, int wait
)
4882 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4884 unsigned int snapshot_success
= 0;
4886 struct snapshot_output
*tmp_output
= NULL
;
4891 DBG("Cmd snapshot record for session %s", session
->name
);
4893 /* Get the datetime for the snapshot output directory. */
4894 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4897 cmd_ret
= LTTNG_ERR_INVALID
;
4902 * Permission denied to create an output if the session is not
4903 * set in no output mode.
4905 if (session
->output_traces
) {
4906 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4910 /* The session needs to be started at least once. */
4911 if (!session
->has_been_started
) {
4912 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4916 /* Use temporary output for the session. */
4917 if (*output
->ctrl_url
!= '\0') {
4918 tmp_output
= snapshot_output_alloc();
4920 cmd_ret
= LTTNG_ERR_NOMEM
;
4924 ret
= snapshot_output_init(session
, output
->max_size
,
4926 output
->ctrl_url
, output
->data_url
,
4930 if (ret
== -ENOMEM
) {
4931 cmd_ret
= LTTNG_ERR_NOMEM
;
4933 cmd_ret
= LTTNG_ERR_INVALID
;
4937 /* Use the global session count for the temporary snapshot. */
4938 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
4940 /* Use the global datetime */
4941 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
4942 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
4943 if (cmd_ret
!= LTTNG_OK
) {
4946 snapshot_success
= 1;
4948 struct snapshot_output
*sout
;
4949 struct lttng_ht_iter iter
;
4952 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4953 &iter
.iter
, sout
, node
.node
) {
4954 struct snapshot_output output_copy
;
4957 * Make a local copy of the output and override output
4958 * parameters with those provided as part of the
4961 memcpy(&output_copy
, sout
, sizeof(output_copy
));
4963 if (output
->max_size
!= (uint64_t) -1ULL) {
4964 output_copy
.max_size
= output
->max_size
;
4967 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4968 memcpy(output_copy
.datetime
, datetime
,
4971 /* Use temporary name. */
4972 if (*output
->name
!= '\0') {
4973 if (lttng_strncpy(output_copy
.name
,
4975 sizeof(output_copy
.name
))) {
4976 cmd_ret
= LTTNG_ERR_INVALID
;
4982 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
4983 if (cmd_ret
!= LTTNG_OK
) {
4987 snapshot_success
= 1;
4992 if (snapshot_success
) {
4993 session
->snapshot
.nb_snapshot
++;
4995 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5000 snapshot_output_destroy(tmp_output
);
5006 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5008 int cmd_set_session_shm_path(struct ltt_session
*session
,
5009 const char *shm_path
)
5015 * Can only set shm path before session is started.
5017 if (session
->has_been_started
) {
5018 return LTTNG_ERR_SESSION_STARTED
;
5021 strncpy(session
->shm_path
, shm_path
,
5022 sizeof(session
->shm_path
));
5023 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5029 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5031 * Ask the consumer to rotate the session output directory.
5032 * The session lock must be held.
5034 * Returns LTTNG_OK on success or else a negative LTTng error code.
5036 int cmd_rotate_session(struct ltt_session
*session
,
5037 struct lttng_rotate_session_return
*rotate_return
,
5038 bool quiet_rotation
,
5039 enum lttng_trace_chunk_command_type command
)
5042 uint64_t ongoing_rotation_chunk_id
;
5043 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5044 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5045 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5046 enum lttng_trace_chunk_status chunk_status
;
5047 bool failed_to_rotate
= false;
5048 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5052 if (!session
->has_been_started
) {
5053 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5058 * Explicit rotation is not supported for live sessions.
5059 * However, live sessions can perform a quiet rotation on
5061 * Rotation is not supported for snapshot traces (no output).
5063 if ((!quiet_rotation
&& session
->live_timer
) ||
5064 !session
->output_traces
) {
5065 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5069 /* Unsupported feature in lttng-relayd before 2.11. */
5070 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5071 (session
->consumer
->relay_major_version
== 2 &&
5072 session
->consumer
->relay_minor_version
< 11)) {
5073 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5077 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5078 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5079 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5083 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5084 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5086 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5091 * After a stop, we only allow one rotation to occur, the other ones are
5092 * useless until a new start.
5094 if (session
->rotated_after_last_stop
) {
5095 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5097 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5102 * After a stop followed by a clear, disallow following rotations a they would
5103 * generate empty chunks.
5105 if (session
->cleared_after_last_stop
) {
5106 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5108 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5112 if (session
->active
) {
5113 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5115 if (!new_trace_chunk
) {
5116 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5122 * The current trace chunk becomes the chunk being archived.
5124 * After this point, "chunk_being_archived" must absolutely
5125 * be closed on the consumer(s), otherwise it will never be
5126 * cleaned-up, which will result in a leak.
5128 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5129 &chunk_being_archived
);
5131 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5135 if (session
->kernel_session
) {
5136 cmd_ret
= kernel_rotate_session(session
);
5137 if (cmd_ret
!= LTTNG_OK
) {
5138 failed_to_rotate
= true;
5139 rotation_fail_code
= cmd_ret
;
5142 if (session
->ust_session
) {
5143 cmd_ret
= ust_app_rotate_session(session
);
5144 if (cmd_ret
!= LTTNG_OK
) {
5145 failed_to_rotate
= true;
5146 rotation_fail_code
= cmd_ret
;
5150 if (!session
->active
) {
5151 session
->rotated_after_last_stop
= true;
5154 if (!chunk_being_archived
) {
5155 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5157 if (failed_to_rotate
) {
5158 cmd_ret
= rotation_fail_code
;
5165 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5166 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5167 &ongoing_rotation_chunk_id
);
5168 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5170 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5171 command
, session
->last_chunk_path
);
5173 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5177 if (failed_to_rotate
) {
5178 cmd_ret
= rotation_fail_code
;
5182 session
->quiet_rotation
= quiet_rotation
;
5183 ret
= timer_session_rotation_pending_check_start(session
,
5184 DEFAULT_ROTATE_PENDING_TIMER
);
5186 cmd_ret
= LTTNG_ERR_UNK
;
5190 if (rotate_return
) {
5191 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5194 session
->chunk_being_archived
= chunk_being_archived
;
5195 chunk_being_archived
= NULL
;
5196 if (!quiet_rotation
) {
5197 ret
= notification_thread_command_session_rotation_ongoing(
5198 notification_thread_handle
,
5199 session
->name
, session
->uid
, session
->gid
,
5200 ongoing_rotation_chunk_id
);
5201 if (ret
!= LTTNG_OK
) {
5202 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5208 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5209 session
->name
, ongoing_rotation_chunk_id
);
5211 lttng_trace_chunk_put(new_trace_chunk
);
5212 lttng_trace_chunk_put(chunk_being_archived
);
5213 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5216 if (session_reset_rotation_state(session
,
5217 LTTNG_ROTATION_STATE_ERROR
)) {
5218 ERR("Failed to reset rotation state of session \"%s\"",
5225 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5227 * Check if the session has finished its rotation.
5229 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5231 int cmd_rotate_get_info(struct ltt_session
*session
,
5232 struct lttng_rotation_get_info_return
*info_return
,
5233 uint64_t rotation_id
)
5235 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5236 enum lttng_rotation_state rotation_state
;
5238 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5239 session
->most_recent_chunk_id
.value
);
5241 if (session
->chunk_being_archived
) {
5242 enum lttng_trace_chunk_status chunk_status
;
5245 chunk_status
= lttng_trace_chunk_get_id(
5246 session
->chunk_being_archived
,
5248 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5250 rotation_state
= rotation_id
== chunk_id
?
5251 LTTNG_ROTATION_STATE_ONGOING
:
5252 LTTNG_ROTATION_STATE_EXPIRED
;
5254 if (session
->last_archived_chunk_id
.is_set
&&
5255 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5256 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5258 rotation_state
= session
->rotation_state
;
5262 switch (rotation_state
) {
5263 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5264 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5267 case LTTNG_ROTATION_STATE_EXPIRED
:
5268 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5269 rotation_id
, session
->name
);
5271 case LTTNG_ROTATION_STATE_ONGOING
:
5272 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5273 rotation_id
, session
->name
);
5275 case LTTNG_ROTATION_STATE_COMPLETED
:
5279 char *current_tracing_path_reply
;
5280 size_t current_tracing_path_reply_len
;
5282 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5283 rotation_id
, session
->name
);
5285 switch (session_get_consumer_destination_type(session
)) {
5286 case CONSUMER_DST_LOCAL
:
5287 current_tracing_path_reply
=
5288 info_return
->location
.local
.absolute_path
;
5289 current_tracing_path_reply_len
=
5290 sizeof(info_return
->location
.local
.absolute_path
);
5291 info_return
->location_type
=
5292 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5293 fmt_ret
= asprintf(&chunk_path
,
5294 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5295 session_get_base_path(session
),
5296 session
->last_archived_chunk_name
);
5297 if (fmt_ret
== -1) {
5298 PERROR("Failed to format the path of the last archived trace chunk");
5299 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5300 cmd_ret
= LTTNG_ERR_UNK
;
5304 case CONSUMER_DST_NET
:
5306 uint16_t ctrl_port
, data_port
;
5308 current_tracing_path_reply
=
5309 info_return
->location
.relay
.relative_path
;
5310 current_tracing_path_reply_len
=
5311 sizeof(info_return
->location
.relay
.relative_path
);
5312 /* Currently the only supported relay protocol. */
5313 info_return
->location
.relay
.protocol
=
5314 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5316 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5317 session_get_net_consumer_hostname(session
),
5318 sizeof(info_return
->location
.relay
.host
));
5320 ERR("Failed to copy host name to rotate_get_info reply");
5321 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5322 cmd_ret
= LTTNG_ERR_SET_URL
;
5326 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5327 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5328 info_return
->location
.relay
.ports
.data
= data_port
;
5329 info_return
->location_type
=
5330 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5331 chunk_path
= strdup(session
->last_chunk_path
);
5333 ERR("Failed to allocate the path of the last archived trace chunk");
5334 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5335 cmd_ret
= LTTNG_ERR_UNK
;
5344 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5345 chunk_path
, current_tracing_path_reply_len
);
5348 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5349 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5350 cmd_ret
= LTTNG_ERR_UNK
;
5356 case LTTNG_ROTATION_STATE_ERROR
:
5357 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5358 rotation_id
, session
->name
);
5366 info_return
->status
= (int32_t) rotation_state
;
5371 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5373 * Configure the automatic rotation parameters.
5374 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5375 * 'activate' to false means deactivate the rotation schedule and validate that
5376 * 'new_value' has the same value as the currently active value.
5378 * Return 0 on success or else a positive LTTNG_ERR code.
5380 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5381 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5383 struct notification_thread_handle
*notification_thread_handle
)
5386 uint64_t *parameter_value
;
5390 DBG("Cmd rotate set schedule session %s", session
->name
);
5392 if (session
->live_timer
|| !session
->output_traces
) {
5393 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5394 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5398 switch (schedule_type
) {
5399 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5400 parameter_value
= &session
->rotate_size
;
5402 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5403 parameter_value
= &session
->rotate_timer_period
;
5404 if (new_value
>= UINT_MAX
) {
5405 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5406 new_value
, UINT_MAX
);
5407 ret
= LTTNG_ERR_INVALID
;
5412 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5413 ret
= LTTNG_ERR_INVALID
;
5417 /* Improper use of the API. */
5418 if (new_value
== -1ULL) {
5419 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5420 ret
= LTTNG_ERR_INVALID
;
5425 * As indicated in struct ltt_session's comments, a value of == 0 means
5426 * this schedule rotation type is not in use.
5428 * Reject the command if we were asked to activate a schedule that was
5431 if (activate
&& *parameter_value
!= 0) {
5432 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5433 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5438 * Reject the command if we were asked to deactivate a schedule that was
5441 if (!activate
&& *parameter_value
== 0) {
5442 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5443 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5448 * Reject the command if we were asked to deactivate a schedule that
5451 if (!activate
&& *parameter_value
!= new_value
) {
5452 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5453 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5457 *parameter_value
= activate
? new_value
: 0;
5459 switch (schedule_type
) {
5460 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5461 if (activate
&& session
->active
) {
5463 * Only start the timer if the session is active,
5464 * otherwise it will be started when the session starts.
5466 ret
= timer_session_rotation_schedule_timer_start(
5467 session
, new_value
);
5469 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5470 ret
= LTTNG_ERR_UNK
;
5474 ret
= timer_session_rotation_schedule_timer_stop(
5477 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5478 ret
= LTTNG_ERR_UNK
;
5483 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5485 ret
= subscribe_session_consumed_size_rotation(session
,
5486 new_value
, notification_thread_handle
);
5488 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5489 ret
= LTTNG_ERR_UNK
;
5493 ret
= unsubscribe_session_consumed_size_rotation(session
,
5494 notification_thread_handle
);
5496 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5497 ret
= LTTNG_ERR_UNK
;
5504 /* Would have been caught before. */
5516 /* Wait for a given path to be removed before continuing. */
5517 static enum lttng_error_code
wait_on_path(void *path_data
)
5519 const char *shm_path
= path_data
;
5521 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5527 ret
= stat(shm_path
, &st
);
5529 if (errno
!= ENOENT
) {
5530 PERROR("stat() returned an error while checking for the existence of the shm path");
5532 DBG("shm path no longer exists, completing the destruction of session");
5536 if (!S_ISDIR(st
.st_mode
)) {
5537 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5542 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5548 * Returns a pointer to a handler to run on completion of a command.
5549 * Returns NULL if no handler has to be run for the last command executed.
5551 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5553 struct cmd_completion_handler
*handler
= current_completion_handler
;
5555 current_completion_handler
= NULL
;
5560 * Init command subsystem.
5565 * Set network sequence index to 1 for streams to match a relayd
5566 * socket on the consumer side.
5568 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5569 relayd_net_seq_idx
= 1;
5570 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5572 DBG("Command subsystem initialized");