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/condition/condition-internal.h>
33 #include <lttng/condition/event-rule.h>
34 #include <lttng/condition/event-rule-internal.h>
35 #include <lttng/event-rule/event-rule.h>
36 #include <lttng/event-rule/event-rule-internal.h>
37 #include <lttng/action/action.h>
38 #include <lttng/channel.h>
39 #include <lttng/channel-internal.h>
40 #include <lttng/rotate-internal.h>
41 #include <lttng/location-internal.h>
42 #include <lttng/session-internal.h>
43 #include <lttng/userspace-probe-internal.h>
44 #include <lttng/session-descriptor-internal.h>
45 #include <lttng/lttng-error.h>
46 #include <lttng/tracker.h>
47 #include <common/string-utils/string-utils.h>
52 #include "health-sessiond.h"
54 #include "kernel-consumer.h"
55 #include "lttng-sessiond.h"
57 #include "lttng-syscall.h"
59 #include "buffer-registry.h"
60 #include "notification-thread.h"
61 #include "notification-thread-commands.h"
63 #include "rotation-thread.h"
65 #include "agent-thread.h"
70 /* Sleep for 100ms between each check for the shm path's deletion. */
71 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
73 struct cmd_destroy_session_reply_context
{
75 bool implicit_rotation_on_destroy
;
77 * Indicates whether or not an error occurred while launching the
78 * destruction of a session.
80 enum lttng_error_code destruction_status
;
83 static enum lttng_error_code
wait_on_path(void *path
);
86 * Command completion handler that is used by the destroy command
87 * when a session that has a non-default shm_path is being destroyed.
89 * See comment in cmd_destroy_session() for the rationale.
91 static struct destroy_completion_handler
{
92 struct cmd_completion_handler handler
;
93 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
94 } destroy_completion_handler
= {
97 .data
= destroy_completion_handler
.shm_path
102 static struct cmd_completion_handler
*current_completion_handler
;
105 * Used to keep a unique index for each relayd socket created where this value
106 * is associated with streams on the consumer so it can match the right relayd
107 * to send to. It must be accessed with the relayd_net_seq_idx_lock
110 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
111 static uint64_t relayd_net_seq_idx
;
113 static int validate_ust_event_name(const char *);
114 static int cmd_enable_event_internal(struct ltt_session
*session
,
115 const struct lttng_domain
*domain
,
116 char *channel_name
, struct lttng_event
*event
,
117 char *filter_expression
,
118 struct lttng_bytecode
*filter
,
119 struct lttng_event_exclusion
*exclusion
,
123 * Create a session path used by list_lttng_sessions for the case that the
124 * session consumer is on the network.
126 static int build_network_session_path(char *dst
, size_t size
,
127 struct ltt_session
*session
)
129 int ret
, kdata_port
, udata_port
;
130 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
131 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
136 memset(tmp_urls
, 0, sizeof(tmp_urls
));
137 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
139 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
141 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
142 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
143 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
146 if (session
->ust_session
&& session
->ust_session
->consumer
) {
147 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
148 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
151 if (uuri
== NULL
&& kuri
== NULL
) {
152 uri
= &session
->consumer
->dst
.net
.control
;
153 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
154 } else if (kuri
&& uuri
) {
155 ret
= uri_compare(kuri
, uuri
);
159 /* Build uuri URL string */
160 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
167 } else if (kuri
&& uuri
== NULL
) {
169 } else if (uuri
&& kuri
== NULL
) {
173 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
179 * Do we have a UST url set. If yes, this means we have both kernel and UST
182 if (*tmp_uurl
!= '\0') {
183 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
184 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
187 if (kuri
|| (!kuri
&& !uuri
)) {
190 /* No kernel URI, use the UST port. */
193 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
201 * Get run-time attributes if the session has been started (discarded events,
204 static int get_kernel_runtime_stats(struct ltt_session
*session
,
205 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
206 uint64_t *lost_packets
)
210 if (!session
->has_been_started
) {
212 *discarded_events
= 0;
217 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
218 session
->kernel_session
->consumer
,
224 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
225 session
->kernel_session
->consumer
,
236 * Get run-time attributes if the session has been started (discarded events,
239 static int get_ust_runtime_stats(struct ltt_session
*session
,
240 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
241 uint64_t *lost_packets
)
244 struct ltt_ust_session
*usess
;
246 if (!discarded_events
|| !lost_packets
) {
251 usess
= session
->ust_session
;
252 assert(discarded_events
);
253 assert(lost_packets
);
255 if (!usess
|| !session
->has_been_started
) {
256 *discarded_events
= 0;
262 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
263 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
264 &usess
->buffer_reg_uid_list
,
265 usess
->consumer
, uchan
->id
,
266 uchan
->attr
.overwrite
,
269 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
270 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
271 uchan
, usess
->consumer
,
272 uchan
->attr
.overwrite
,
278 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
279 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
281 ERR("Unsupported buffer type");
292 * Fill lttng_channel array of all channels.
294 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
295 struct ltt_session
*session
, struct lttng_channel
*channels
,
296 struct lttng_channel_extended
*chan_exts
)
299 struct ltt_kernel_channel
*kchan
;
301 DBG("Listing channels for session %s", session
->name
);
304 case LTTNG_DOMAIN_KERNEL
:
305 /* Kernel channels */
306 if (session
->kernel_session
!= NULL
) {
307 cds_list_for_each_entry(kchan
,
308 &session
->kernel_session
->channel_list
.head
, list
) {
309 uint64_t discarded_events
, lost_packets
;
310 struct lttng_channel_extended
*extended
;
312 extended
= (struct lttng_channel_extended
*)
313 kchan
->channel
->attr
.extended
.ptr
;
315 ret
= get_kernel_runtime_stats(session
, kchan
,
316 &discarded_events
, &lost_packets
);
320 /* Copy lttng_channel struct to array */
321 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
322 channels
[i
].enabled
= kchan
->enabled
;
323 chan_exts
[i
].discarded_events
=
325 chan_exts
[i
].lost_packets
= lost_packets
;
326 chan_exts
[i
].monitor_timer_interval
=
327 extended
->monitor_timer_interval
;
328 chan_exts
[i
].blocking_timeout
= 0;
333 case LTTNG_DOMAIN_UST
:
335 struct lttng_ht_iter iter
;
336 struct ltt_ust_channel
*uchan
;
339 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
340 &iter
.iter
, uchan
, node
.node
) {
341 uint64_t discarded_events
= 0, lost_packets
= 0;
343 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
344 LTTNG_SYMBOL_NAME_LEN
)) {
347 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
348 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
349 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
350 channels
[i
].attr
.switch_timer_interval
=
351 uchan
->attr
.switch_timer_interval
;
352 channels
[i
].attr
.read_timer_interval
=
353 uchan
->attr
.read_timer_interval
;
354 channels
[i
].enabled
= uchan
->enabled
;
355 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
356 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
359 * Map enum lttng_ust_output to enum lttng_event_output.
361 switch (uchan
->attr
.output
) {
363 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
367 * LTTNG_UST_MMAP is the only supported UST
374 chan_exts
[i
].monitor_timer_interval
=
375 uchan
->monitor_timer_interval
;
376 chan_exts
[i
].blocking_timeout
=
377 uchan
->attr
.u
.s
.blocking_timeout
;
379 ret
= get_ust_runtime_stats(session
, uchan
,
380 &discarded_events
, &lost_packets
);
384 chan_exts
[i
].discarded_events
= discarded_events
;
385 chan_exts
[i
].lost_packets
= lost_packets
;
397 return -LTTNG_ERR_FATAL
;
403 static int append_extended_info(const char *filter_expression
,
404 struct lttng_event_exclusion
*exclusion
,
405 struct lttng_userspace_probe_location
*probe_location
,
406 struct lttng_payload
*payload
)
409 size_t filter_len
= 0;
410 size_t nb_exclusions
= 0;
411 size_t userspace_probe_location_len
= 0;
412 struct lttcomm_event_extended_header extended_header
= {};
413 struct lttcomm_event_extended_header
*p_extended_header
;
414 const size_t original_payload_size
= payload
->buffer
.size
;
416 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &extended_header
,
417 sizeof(extended_header
));
422 if (filter_expression
) {
423 filter_len
= strlen(filter_expression
) + 1;
424 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
425 filter_expression
, filter_len
);
432 const size_t len
= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
434 nb_exclusions
= exclusion
->count
;
436 ret
= lttng_dynamic_buffer_append(
437 &payload
->buffer
, &exclusion
->names
, len
);
443 if (probe_location
) {
444 const size_t size_before_probe
= payload
->buffer
.size
;
446 ret
= lttng_userspace_probe_location_serialize(probe_location
,
453 userspace_probe_location_len
=
454 payload
->buffer
.size
- size_before_probe
;
457 /* Set header fields */
458 p_extended_header
= (struct lttcomm_event_extended_header
*)
459 (payload
->buffer
.data
+ original_payload_size
);
461 p_extended_header
->filter_len
= filter_len
;
462 p_extended_header
->nb_exclusions
= nb_exclusions
;
463 p_extended_header
->userspace_probe_location_len
=
464 userspace_probe_location_len
;
472 * Create a list of agent domain events.
474 * Return number of events in list on success or else a negative value.
476 static int list_lttng_agent_events(struct agent
*agt
,
477 struct lttng_payload
*payload
)
479 int nb_events
= 0, ret
= 0;
480 const struct agent_event
*agent_event
;
481 struct lttng_ht_iter iter
;
485 DBG3("Listing agent events");
488 cds_lfht_for_each_entry (
489 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
490 struct lttng_event event
= {
491 .enabled
= AGENT_EVENT_IS_ENABLED(agent_event
),
492 .loglevel
= agent_event
->loglevel_value
,
493 .loglevel_type
= agent_event
->loglevel_type
,
496 ret
= lttng_strncpy(event
.name
, agent_event
->name
, sizeof(event
.name
));
498 /* Internal error, invalid name. */
499 ERR("Invalid event name while listing agent events: '%s' exceeds the maximal allowed length of %zu bytes",
500 agent_event
->name
, sizeof(event
.name
));
501 ret
= -LTTNG_ERR_UNK
;
505 ret
= lttng_dynamic_buffer_append(
506 &payload
->buffer
, &event
, sizeof(event
));
508 ERR("Failed to append event to payload");
509 ret
= -LTTNG_ERR_NOMEM
;
516 cds_lfht_for_each_entry (
517 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
518 /* Append extended info. */
519 ret
= append_extended_info(agent_event
->filter_expression
, NULL
,
522 ERR("Failed to append extended event info to payload");
523 ret
= -LTTNG_ERR_NOMEM
;
535 * Create a list of ust global domain events.
537 static int list_lttng_ust_global_events(char *channel_name
,
538 struct ltt_ust_domain_global
*ust_global
,
539 struct lttng_payload
*payload
)
542 unsigned int nb_events
= 0;
543 struct lttng_ht_iter iter
;
544 const struct lttng_ht_node_str
*node
;
545 const struct ltt_ust_channel
*uchan
;
546 const struct ltt_ust_event
*uevent
;
548 DBG("Listing UST global events for channel %s", channel_name
);
552 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
553 node
= lttng_ht_iter_get_node_str(&iter
);
555 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
559 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
561 DBG3("Listing UST global events");
563 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
564 struct lttng_event event
= {};
566 if (uevent
->internal
) {
570 ret
= lttng_strncpy(event
.name
, uevent
->attr
.name
, sizeof(event
.name
));
572 /* Internal error, invalid name. */
573 ERR("Invalid event name while listing user space tracer events: '%s' exceeds the maximal allowed length of %zu bytes",
574 uevent
->attr
.name
, sizeof(event
.name
));
575 ret
= -LTTNG_ERR_UNK
;
579 event
.enabled
= uevent
->enabled
;
581 switch (uevent
->attr
.instrumentation
) {
582 case LTTNG_UST_TRACEPOINT
:
583 event
.type
= LTTNG_EVENT_TRACEPOINT
;
585 case LTTNG_UST_PROBE
:
586 event
.type
= LTTNG_EVENT_PROBE
;
588 case LTTNG_UST_FUNCTION
:
589 event
.type
= LTTNG_EVENT_FUNCTION
;
593 event
.loglevel
= uevent
->attr
.loglevel
;
594 switch (uevent
->attr
.loglevel_type
) {
595 case LTTNG_UST_LOGLEVEL_ALL
:
596 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
598 case LTTNG_UST_LOGLEVEL_RANGE
:
599 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
601 case LTTNG_UST_LOGLEVEL_SINGLE
:
602 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
606 if (uevent
->filter
) {
610 if (uevent
->exclusion
) {
614 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &event
, sizeof(event
));
616 ERR("Failed to append event to payload");
617 ret
= -LTTNG_ERR_NOMEM
;
624 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
625 /* Append extended info. */
626 ret
= append_extended_info(uevent
->filter_expression
,
627 uevent
->exclusion
, NULL
, payload
);
629 ERR("Failed to append extended event info to payload");
630 ret
= -LTTNG_ERR_FATAL
;
642 * Fill lttng_event array of all kernel events in the channel.
644 static int list_lttng_kernel_events(char *channel_name
,
645 struct ltt_kernel_session
*kernel_session
,
646 struct lttng_payload
*payload
)
649 unsigned int nb_event
;
650 const struct ltt_kernel_event
*kevent
;
651 const struct ltt_kernel_channel
*kchan
;
653 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
655 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
659 nb_event
= kchan
->event_count
;
661 DBG("Listing events for channel %s", kchan
->channel
->name
);
663 /* Kernel channels */
664 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
665 struct lttng_event event
= {};
667 ret
= lttng_strncpy(event
.name
, kevent
->event
->name
, sizeof(event
.name
));
669 /* Internal error, invalid name. */
670 ERR("Invalid event name while listing kernel events: '%s' exceeds the maximal allowed length of %zu bytes",
673 ret
= -LTTNG_ERR_UNK
;
677 event
.enabled
= kevent
->enabled
;
678 event
.filter
= (unsigned char) !!kevent
->filter_expression
;
680 switch (kevent
->event
->instrumentation
) {
681 case LTTNG_KERNEL_TRACEPOINT
:
682 event
.type
= LTTNG_EVENT_TRACEPOINT
;
684 case LTTNG_KERNEL_KRETPROBE
:
685 event
.type
= LTTNG_EVENT_FUNCTION
;
686 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
687 sizeof(struct lttng_kernel_kprobe
));
689 case LTTNG_KERNEL_KPROBE
:
690 event
.type
= LTTNG_EVENT_PROBE
;
691 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
692 sizeof(struct lttng_kernel_kprobe
));
694 case LTTNG_KERNEL_UPROBE
:
695 event
.type
= LTTNG_EVENT_USERSPACE_PROBE
;
697 case LTTNG_KERNEL_FUNCTION
:
698 event
.type
= LTTNG_EVENT_FUNCTION
;
699 memcpy(&event
.attr
.ftrace
, &kevent
->event
->u
.ftrace
,
700 sizeof(struct lttng_kernel_function
));
702 case LTTNG_KERNEL_NOOP
:
703 event
.type
= LTTNG_EVENT_NOOP
;
705 case LTTNG_KERNEL_SYSCALL
:
706 event
.type
= LTTNG_EVENT_SYSCALL
;
708 case LTTNG_KERNEL_ALL
:
715 ret
= lttng_dynamic_buffer_append(
716 &payload
->buffer
, &event
, sizeof(event
));
718 ERR("Failed to append event to payload");
719 ret
= -LTTNG_ERR_NOMEM
;
724 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
725 /* Append extended info. */
726 ret
= append_extended_info(kevent
->filter_expression
, NULL
,
727 kevent
->userspace_probe_location
, payload
);
729 DBG("Error appending extended info message");
730 ret
= -LTTNG_ERR_FATAL
;
742 * Add URI so the consumer output object. Set the correct path depending on the
743 * domain adding the default trace directory.
745 static enum lttng_error_code
add_uri_to_consumer(
746 const struct ltt_session
*session
,
747 struct consumer_output
*consumer
,
748 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
751 enum lttng_error_code ret_code
= LTTNG_OK
;
755 if (consumer
== NULL
) {
756 DBG("No consumer detected. Don't add URI. Stopping.");
757 ret_code
= LTTNG_ERR_NO_CONSUMER
;
762 case LTTNG_DOMAIN_KERNEL
:
763 ret
= lttng_strncpy(consumer
->domain_subdir
,
764 DEFAULT_KERNEL_TRACE_DIR
,
765 sizeof(consumer
->domain_subdir
));
767 case LTTNG_DOMAIN_UST
:
768 ret
= lttng_strncpy(consumer
->domain_subdir
,
769 DEFAULT_UST_TRACE_DIR
,
770 sizeof(consumer
->domain_subdir
));
774 * This case is possible is we try to add the URI to the global
775 * tracing session consumer object which in this case there is
778 memset(consumer
->domain_subdir
, 0,
779 sizeof(consumer
->domain_subdir
));
783 ERR("Failed to initialize consumer output domain subdirectory");
784 ret_code
= LTTNG_ERR_FATAL
;
788 switch (uri
->dtype
) {
791 DBG2("Setting network URI to consumer");
793 if (consumer
->type
== CONSUMER_DST_NET
) {
794 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
795 consumer
->dst
.net
.control_isset
) ||
796 (uri
->stype
== LTTNG_STREAM_DATA
&&
797 consumer
->dst
.net
.data_isset
)) {
798 ret_code
= LTTNG_ERR_URL_EXIST
;
802 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
805 /* Set URI into consumer output object */
806 ret
= consumer_set_network_uri(session
, consumer
, uri
);
810 } else if (ret
== 1) {
812 * URI was the same in the consumer so we do not append the subdir
813 * again so to not duplicate output dir.
820 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
821 ret_code
= LTTNG_ERR_INVALID
;
824 DBG2("Setting trace directory path from URI to %s",
826 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
828 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
830 sizeof(consumer
->dst
.session_root_path
));
832 ret_code
= LTTNG_ERR_FATAL
;
835 consumer
->type
= CONSUMER_DST_LOCAL
;
845 * Init tracing by creating trace directory and sending fds kernel consumer.
847 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
850 struct lttng_ht_iter iter
;
851 struct consumer_socket
*socket
;
857 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
858 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
860 pthread_mutex_lock(socket
->lock
);
861 ret
= kernel_consumer_send_session(socket
, session
);
862 pthread_mutex_unlock(socket
->lock
);
864 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
876 * Create a socket to the relayd using the URI.
878 * On success, the relayd_sock pointer is set to the created socket.
879 * Else, it remains untouched and an LTTng error code is returned.
881 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
882 struct lttcomm_relayd_sock
**relayd_sock
,
883 struct consumer_output
*consumer
)
886 enum lttng_error_code status
= LTTNG_OK
;
887 struct lttcomm_relayd_sock
*rsock
;
889 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
890 RELAYD_VERSION_COMM_MINOR
);
892 status
= LTTNG_ERR_FATAL
;
897 * Connect to relayd so we can proceed with a session creation. This call
898 * can possibly block for an arbitrary amount of time to set the health
899 * state to be in poll execution.
902 ret
= relayd_connect(rsock
);
905 ERR("Unable to reach lttng-relayd");
906 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
910 /* Create socket for control stream. */
911 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
912 uint64_t result_flags
;
914 DBG3("Creating relayd stream socket from URI");
916 /* Check relayd version */
917 ret
= relayd_version_check(rsock
);
918 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
919 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
921 } else if (ret
< 0) {
922 ERR("Unable to reach lttng-relayd");
923 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
926 consumer
->relay_major_version
= rsock
->major
;
927 consumer
->relay_minor_version
= rsock
->minor
;
928 ret
= relayd_get_configuration(rsock
, 0,
931 ERR("Unable to get relayd configuration");
932 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
935 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
936 consumer
->relay_allows_clear
= true;
938 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
939 DBG3("Creating relayd data socket from URI");
941 /* Command is not valid */
942 ERR("Relayd invalid stream type: %d", uri
->stype
);
943 status
= LTTNG_ERR_INVALID
;
947 *relayd_sock
= rsock
;
952 /* The returned value is not useful since we are on an error path. */
953 (void) relayd_close(rsock
);
961 * Connect to the relayd using URI and send the socket to the right consumer.
963 * The consumer socket lock must be held by the caller.
965 * Returns LTTNG_OK on success or an LTTng error code on failure.
967 static enum lttng_error_code
send_consumer_relayd_socket(
968 unsigned int session_id
,
969 struct lttng_uri
*relayd_uri
,
970 struct consumer_output
*consumer
,
971 struct consumer_socket
*consumer_sock
,
972 const char *session_name
, const char *hostname
,
973 const char *base_path
, int session_live_timer
,
974 const uint64_t *current_chunk_id
,
975 time_t session_creation_time
,
976 bool session_name_contains_creation_time
)
979 struct lttcomm_relayd_sock
*rsock
= NULL
;
980 enum lttng_error_code status
;
982 /* Connect to relayd and make version check if uri is the control. */
983 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
984 if (status
!= LTTNG_OK
) {
985 goto relayd_comm_error
;
989 /* Set the network sequence index if not set. */
990 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
991 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
993 * Increment net_seq_idx because we are about to transfer the
994 * new relayd socket to the consumer.
995 * Assign unique key so the consumer can match streams.
997 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
998 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1001 /* Send relayd socket to consumer. */
1002 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1003 relayd_uri
->stype
, session_id
,
1004 session_name
, hostname
, base_path
,
1005 session_live_timer
, current_chunk_id
,
1006 session_creation_time
, session_name_contains_creation_time
);
1008 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1012 /* Flag that the corresponding socket was sent. */
1013 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1014 consumer_sock
->control_sock_sent
= 1;
1015 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1016 consumer_sock
->data_sock_sent
= 1;
1020 * Close socket which was dup on the consumer side. The session daemon does
1021 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1025 if (status
!= LTTNG_OK
) {
1027 * The consumer output for this session should not be used anymore
1028 * since the relayd connection failed thus making any tracing or/and
1029 * streaming not usable.
1031 consumer
->enabled
= 0;
1033 (void) relayd_close(rsock
);
1041 * Send both relayd sockets to a specific consumer and domain. This is a
1042 * helper function to facilitate sending the information to the consumer for a
1045 * The consumer socket lock must be held by the caller.
1047 * Returns LTTNG_OK, or an LTTng error code on failure.
1049 static enum lttng_error_code
send_consumer_relayd_sockets(
1050 enum lttng_domain_type domain
,
1051 unsigned int session_id
, struct consumer_output
*consumer
,
1052 struct consumer_socket
*sock
, const char *session_name
,
1053 const char *hostname
, const char *base_path
, int session_live_timer
,
1054 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1055 bool session_name_contains_creation_time
)
1057 enum lttng_error_code status
= LTTNG_OK
;
1062 /* Sending control relayd socket. */
1063 if (!sock
->control_sock_sent
) {
1064 status
= send_consumer_relayd_socket(session_id
,
1065 &consumer
->dst
.net
.control
, consumer
, sock
,
1066 session_name
, hostname
, base_path
, session_live_timer
,
1067 current_chunk_id
, session_creation_time
,
1068 session_name_contains_creation_time
);
1069 if (status
!= LTTNG_OK
) {
1074 /* Sending data relayd socket. */
1075 if (!sock
->data_sock_sent
) {
1076 status
= send_consumer_relayd_socket(session_id
,
1077 &consumer
->dst
.net
.data
, consumer
, sock
,
1078 session_name
, hostname
, base_path
, session_live_timer
,
1079 current_chunk_id
, session_creation_time
,
1080 session_name_contains_creation_time
);
1081 if (status
!= LTTNG_OK
) {
1091 * Setup relayd connections for a tracing session. First creates the socket to
1092 * the relayd and send them to the right domain consumer. Consumer type MUST be
1095 int cmd_setup_relayd(struct ltt_session
*session
)
1098 struct ltt_ust_session
*usess
;
1099 struct ltt_kernel_session
*ksess
;
1100 struct consumer_socket
*socket
;
1101 struct lttng_ht_iter iter
;
1102 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1106 usess
= session
->ust_session
;
1107 ksess
= session
->kernel_session
;
1109 DBG("Setting relayd for session %s", session
->name
);
1112 if (session
->current_trace_chunk
) {
1113 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1114 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1116 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1117 current_chunk_id
.is_set
= true;
1119 ERR("Failed to get current trace chunk id");
1120 ret
= LTTNG_ERR_UNK
;
1125 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1126 && usess
->consumer
->enabled
) {
1127 /* For each consumer socket, send relayd sockets */
1128 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1129 socket
, node
.node
) {
1130 pthread_mutex_lock(socket
->lock
);
1131 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1132 usess
->consumer
, socket
,
1133 session
->name
, session
->hostname
,
1135 session
->live_timer
,
1136 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1137 session
->creation_time
,
1138 session
->name_contains_creation_time
);
1139 pthread_mutex_unlock(socket
->lock
);
1140 if (ret
!= LTTNG_OK
) {
1143 /* Session is now ready for network streaming. */
1144 session
->net_handle
= 1;
1146 session
->consumer
->relay_major_version
=
1147 usess
->consumer
->relay_major_version
;
1148 session
->consumer
->relay_minor_version
=
1149 usess
->consumer
->relay_minor_version
;
1150 session
->consumer
->relay_allows_clear
=
1151 usess
->consumer
->relay_allows_clear
;
1154 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1155 && ksess
->consumer
->enabled
) {
1156 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1157 socket
, node
.node
) {
1158 pthread_mutex_lock(socket
->lock
);
1159 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1160 ksess
->consumer
, socket
,
1161 session
->name
, session
->hostname
,
1163 session
->live_timer
,
1164 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1165 session
->creation_time
,
1166 session
->name_contains_creation_time
);
1167 pthread_mutex_unlock(socket
->lock
);
1168 if (ret
!= LTTNG_OK
) {
1171 /* Session is now ready for network streaming. */
1172 session
->net_handle
= 1;
1174 session
->consumer
->relay_major_version
=
1175 ksess
->consumer
->relay_major_version
;
1176 session
->consumer
->relay_minor_version
=
1177 ksess
->consumer
->relay_minor_version
;
1178 session
->consumer
->relay_allows_clear
=
1179 ksess
->consumer
->relay_allows_clear
;
1188 * Start a kernel session by opening all necessary streams.
1190 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1193 struct ltt_kernel_channel
*kchan
;
1195 /* Open kernel metadata */
1196 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1197 ret
= kernel_open_metadata(ksess
);
1199 ret
= LTTNG_ERR_KERN_META_FAIL
;
1204 /* Open kernel metadata stream */
1205 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1206 ret
= kernel_open_metadata_stream(ksess
);
1208 ERR("Kernel create metadata stream failed");
1209 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1214 /* For each channel */
1215 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1216 if (kchan
->stream_count
== 0) {
1217 ret
= kernel_open_channel_stream(kchan
);
1219 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1222 /* Update the stream global counter */
1223 ksess
->stream_count_global
+= ret
;
1227 /* Setup kernel consumer socket and send fds to it */
1228 ret
= init_kernel_tracing(ksess
);
1230 ret
= LTTNG_ERR_KERN_START_FAIL
;
1234 /* This start the kernel tracing */
1235 ret
= kernel_start_session(ksess
);
1237 ret
= LTTNG_ERR_KERN_START_FAIL
;
1241 /* Quiescent wait after starting trace */
1242 kernel_wait_quiescent();
1252 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1254 struct ltt_kernel_channel
*kchan
;
1255 bool error_occurred
= false;
1258 if (!ksess
|| !ksess
->active
) {
1261 DBG("Stopping kernel tracing");
1263 ret
= kernel_stop_session(ksess
);
1265 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1269 kernel_wait_quiescent();
1271 /* Flush metadata after stopping (if exists) */
1272 if (ksess
->metadata_stream_fd
>= 0) {
1273 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1275 ERR("Kernel metadata flush failed");
1276 error_occurred
= true;
1280 /* Flush all buffers after stopping */
1281 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1282 ret
= kernel_flush_buffer(kchan
);
1284 ERR("Kernel flush buffer error");
1285 error_occurred
= true;
1290 if (error_occurred
) {
1291 ret
= LTTNG_ERR_UNK
;
1300 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1302 int cmd_disable_channel(struct ltt_session
*session
,
1303 enum lttng_domain_type domain
, char *channel_name
)
1306 struct ltt_ust_session
*usess
;
1308 usess
= session
->ust_session
;
1313 case LTTNG_DOMAIN_KERNEL
:
1315 ret
= channel_kernel_disable(session
->kernel_session
,
1317 if (ret
!= LTTNG_OK
) {
1321 kernel_wait_quiescent();
1324 case LTTNG_DOMAIN_UST
:
1326 struct ltt_ust_channel
*uchan
;
1327 struct lttng_ht
*chan_ht
;
1329 chan_ht
= usess
->domain_global
.channels
;
1331 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1332 if (uchan
== NULL
) {
1333 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1337 ret
= channel_ust_disable(usess
, uchan
);
1338 if (ret
!= LTTNG_OK
) {
1344 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1356 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1358 * The wpipe arguments is used as a notifier for the kernel thread.
1360 int cmd_enable_channel(struct ltt_session
*session
,
1361 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1364 struct ltt_ust_session
*usess
= session
->ust_session
;
1365 struct lttng_ht
*chan_ht
;
1367 struct lttng_channel attr
;
1374 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1376 /* Validate channel name */
1377 if (attr
.name
[0] == '.' ||
1378 memchr(attr
.name
, '/', len
) != NULL
) {
1379 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1383 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1388 * Don't try to enable a channel if the session has been started at
1389 * some point in time before. The tracer does not allow it.
1391 if (session
->has_been_started
) {
1392 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1397 * If the session is a live session, remove the switch timer, the
1398 * live timer does the same thing but sends also synchronisation
1399 * beacons for inactive streams.
1401 if (session
->live_timer
> 0) {
1402 attr
.attr
.live_timer_interval
= session
->live_timer
;
1403 attr
.attr
.switch_timer_interval
= 0;
1406 /* Check for feature support */
1407 switch (domain
->type
) {
1408 case LTTNG_DOMAIN_KERNEL
:
1410 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1411 /* Sampling position of buffer is not supported */
1412 WARN("Kernel tracer does not support buffer monitoring. "
1413 "Setting the monitor interval timer to 0 "
1414 "(disabled) for channel '%s' of session '%s'",
1415 attr
.name
, session
->name
);
1416 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1420 case LTTNG_DOMAIN_UST
:
1422 case LTTNG_DOMAIN_JUL
:
1423 case LTTNG_DOMAIN_LOG4J
:
1424 case LTTNG_DOMAIN_PYTHON
:
1425 if (!agent_tracing_is_enabled()) {
1426 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1427 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1432 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1436 switch (domain
->type
) {
1437 case LTTNG_DOMAIN_KERNEL
:
1439 struct ltt_kernel_channel
*kchan
;
1441 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1442 session
->kernel_session
);
1443 if (kchan
== NULL
) {
1444 if (session
->snapshot
.nb_output
> 0 ||
1445 session
->snapshot_mode
) {
1446 /* Enforce mmap output for snapshot sessions. */
1447 attr
.attr
.output
= LTTNG_EVENT_MMAP
;
1449 ret
= channel_kernel_create(session
->kernel_session
, &attr
, wpipe
);
1450 if (attr
.name
[0] != '\0') {
1451 session
->kernel_session
->has_non_default_channel
= 1;
1454 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1457 if (ret
!= LTTNG_OK
) {
1461 kernel_wait_quiescent();
1464 case LTTNG_DOMAIN_UST
:
1465 case LTTNG_DOMAIN_JUL
:
1466 case LTTNG_DOMAIN_LOG4J
:
1467 case LTTNG_DOMAIN_PYTHON
:
1469 struct ltt_ust_channel
*uchan
;
1474 * Current agent implementation limitations force us to allow
1475 * only one channel at once in "agent" subdomains. Each
1476 * subdomain has a default channel name which must be strictly
1479 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1480 if (strncmp(attr
.name
, DEFAULT_JUL_CHANNEL_NAME
,
1481 LTTNG_SYMBOL_NAME_LEN
)) {
1482 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1485 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1486 if (strncmp(attr
.name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1487 LTTNG_SYMBOL_NAME_LEN
)) {
1488 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1491 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1492 if (strncmp(attr
.name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1493 LTTNG_SYMBOL_NAME_LEN
)) {
1494 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1499 chan_ht
= usess
->domain_global
.channels
;
1501 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1502 if (uchan
== NULL
) {
1503 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1504 if (attr
.name
[0] != '\0') {
1505 usess
->has_non_default_channel
= 1;
1508 ret
= channel_ust_enable(usess
, uchan
);
1513 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1517 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1518 session
->has_non_mmap_channel
= true;
1526 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1527 struct ltt_session
*session
,
1528 enum lttng_domain_type domain
,
1529 enum lttng_process_attr process_attr
,
1530 enum lttng_tracking_policy
*policy
)
1532 enum lttng_error_code ret_code
= LTTNG_OK
;
1533 const struct process_attr_tracker
*tracker
;
1536 case LTTNG_DOMAIN_KERNEL
:
1537 if (!session
->kernel_session
) {
1538 ret_code
= LTTNG_ERR_INVALID
;
1541 tracker
= kernel_get_process_attr_tracker(
1542 session
->kernel_session
, process_attr
);
1544 case LTTNG_DOMAIN_UST
:
1545 if (!session
->ust_session
) {
1546 ret_code
= LTTNG_ERR_INVALID
;
1549 tracker
= trace_ust_get_process_attr_tracker(
1550 session
->ust_session
, process_attr
);
1553 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1557 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1559 ret_code
= LTTNG_ERR_INVALID
;
1565 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1566 struct ltt_session
*session
,
1567 enum lttng_domain_type domain
,
1568 enum lttng_process_attr process_attr
,
1569 enum lttng_tracking_policy policy
)
1571 enum lttng_error_code ret_code
= LTTNG_OK
;
1574 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1575 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1576 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1579 ret_code
= LTTNG_ERR_INVALID
;
1584 case LTTNG_DOMAIN_KERNEL
:
1585 if (!session
->kernel_session
) {
1586 ret_code
= LTTNG_ERR_INVALID
;
1589 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1590 session
->kernel_session
, process_attr
, policy
);
1592 case LTTNG_DOMAIN_UST
:
1593 if (!session
->ust_session
) {
1594 ret_code
= LTTNG_ERR_INVALID
;
1597 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1598 session
->ust_session
, process_attr
, policy
);
1601 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1608 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1609 struct ltt_session
*session
,
1610 enum lttng_domain_type domain
,
1611 enum lttng_process_attr process_attr
,
1612 const struct process_attr_value
*value
)
1614 enum lttng_error_code ret_code
= LTTNG_OK
;
1617 case LTTNG_DOMAIN_KERNEL
:
1618 if (!session
->kernel_session
) {
1619 ret_code
= LTTNG_ERR_INVALID
;
1622 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1623 session
->kernel_session
, process_attr
, value
);
1625 case LTTNG_DOMAIN_UST
:
1626 if (!session
->ust_session
) {
1627 ret_code
= LTTNG_ERR_INVALID
;
1630 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1631 session
->ust_session
, process_attr
, value
);
1634 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1641 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1642 struct ltt_session
*session
,
1643 enum lttng_domain_type domain
,
1644 enum lttng_process_attr process_attr
,
1645 const struct process_attr_value
*value
)
1647 enum lttng_error_code ret_code
= LTTNG_OK
;
1650 case LTTNG_DOMAIN_KERNEL
:
1651 if (!session
->kernel_session
) {
1652 ret_code
= LTTNG_ERR_INVALID
;
1655 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1656 session
->kernel_session
, process_attr
, value
);
1658 case LTTNG_DOMAIN_UST
:
1659 if (!session
->ust_session
) {
1660 ret_code
= LTTNG_ERR_INVALID
;
1663 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1664 session
->ust_session
, process_attr
, value
);
1667 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1674 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1675 struct ltt_session
*session
,
1676 enum lttng_domain_type domain
,
1677 enum lttng_process_attr process_attr
,
1678 struct lttng_process_attr_values
**values
)
1680 enum lttng_error_code ret_code
= LTTNG_OK
;
1681 const struct process_attr_tracker
*tracker
;
1682 enum process_attr_tracker_status status
;
1685 case LTTNG_DOMAIN_KERNEL
:
1686 if (!session
->kernel_session
) {
1687 ret_code
= LTTNG_ERR_INVALID
;
1690 tracker
= kernel_get_process_attr_tracker(
1691 session
->kernel_session
, process_attr
);
1693 case LTTNG_DOMAIN_UST
:
1694 if (!session
->ust_session
) {
1695 ret_code
= LTTNG_ERR_INVALID
;
1698 tracker
= trace_ust_get_process_attr_tracker(
1699 session
->ust_session
, process_attr
);
1702 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1707 ret_code
= LTTNG_ERR_INVALID
;
1711 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1713 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1714 ret_code
= LTTNG_OK
;
1716 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1717 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1719 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1720 ret_code
= LTTNG_ERR_NOMEM
;
1723 ret_code
= LTTNG_ERR_UNK
;
1732 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1734 int cmd_disable_event(struct ltt_session
*session
,
1735 enum lttng_domain_type domain
, const char *channel_name
,
1736 const struct lttng_event
*event
)
1739 const char *event_name
;
1741 DBG("Disable event command for event \'%s\'", event
->name
);
1743 event_name
= event
->name
;
1745 /* Error out on unhandled search criteria */
1746 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1747 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1748 ret
= LTTNG_ERR_UNK
;
1755 case LTTNG_DOMAIN_KERNEL
:
1757 struct ltt_kernel_channel
*kchan
;
1758 struct ltt_kernel_session
*ksess
;
1760 ksess
= session
->kernel_session
;
1763 * If a non-default channel has been created in the
1764 * session, explicitely require that -c chan_name needs
1767 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1768 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1772 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1773 if (kchan
== NULL
) {
1774 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1778 switch (event
->type
) {
1779 case LTTNG_EVENT_ALL
:
1780 case LTTNG_EVENT_TRACEPOINT
:
1781 case LTTNG_EVENT_SYSCALL
:
1782 case LTTNG_EVENT_PROBE
:
1783 case LTTNG_EVENT_FUNCTION
:
1784 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1785 if (event_name
[0] == '\0') {
1786 ret
= event_kernel_disable_event(kchan
,
1789 ret
= event_kernel_disable_event(kchan
,
1790 event_name
, event
->type
);
1792 if (ret
!= LTTNG_OK
) {
1797 ret
= LTTNG_ERR_UNK
;
1801 kernel_wait_quiescent();
1804 case LTTNG_DOMAIN_UST
:
1806 struct ltt_ust_channel
*uchan
;
1807 struct ltt_ust_session
*usess
;
1809 usess
= session
->ust_session
;
1811 if (validate_ust_event_name(event_name
)) {
1812 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1817 * If a non-default channel has been created in the
1818 * session, explicitly require that -c chan_name needs
1821 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1822 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1826 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1828 if (uchan
== NULL
) {
1829 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1833 switch (event
->type
) {
1834 case LTTNG_EVENT_ALL
:
1836 * An empty event name means that everything
1837 * should be disabled.
1839 if (event
->name
[0] == '\0') {
1840 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1842 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1845 if (ret
!= LTTNG_OK
) {
1850 ret
= LTTNG_ERR_UNK
;
1854 DBG3("Disable UST event %s in channel %s completed", event_name
,
1858 case LTTNG_DOMAIN_LOG4J
:
1859 case LTTNG_DOMAIN_JUL
:
1860 case LTTNG_DOMAIN_PYTHON
:
1863 struct ltt_ust_session
*usess
= session
->ust_session
;
1867 switch (event
->type
) {
1868 case LTTNG_EVENT_ALL
:
1871 ret
= LTTNG_ERR_UNK
;
1875 agt
= trace_ust_find_agent(usess
, domain
);
1877 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1881 * An empty event name means that everything
1882 * should be disabled.
1884 if (event
->name
[0] == '\0') {
1885 ret
= event_agent_disable_all(usess
, agt
);
1887 ret
= event_agent_disable(usess
, agt
, event_name
);
1889 if (ret
!= LTTNG_OK
) {
1896 ret
= LTTNG_ERR_UND
;
1909 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1911 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1912 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1914 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1915 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1918 * Don't try to add a context if the session has been started at
1919 * some point in time before. The tracer does not allow it and would
1920 * result in a corrupted trace.
1922 if (session
->has_been_started
) {
1923 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1927 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1928 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1929 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1933 case LTTNG_DOMAIN_KERNEL
:
1934 assert(session
->kernel_session
);
1936 if (session
->kernel_session
->channel_count
== 0) {
1937 /* Create default channel */
1938 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1939 if (ret
!= LTTNG_OK
) {
1942 chan_kern_created
= 1;
1944 /* Add kernel context to kernel tracer */
1945 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1946 if (ret
!= LTTNG_OK
) {
1950 case LTTNG_DOMAIN_JUL
:
1951 case LTTNG_DOMAIN_LOG4J
:
1954 * Validate channel name.
1955 * If no channel name is given and the domain is JUL or LOG4J,
1956 * set it to the appropriate domain-specific channel name. If
1957 * a name is provided but does not match the expexted channel
1958 * name, return an error.
1960 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1961 strcmp(channel_name
,
1962 DEFAULT_JUL_CHANNEL_NAME
)) {
1963 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1965 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1966 strcmp(channel_name
,
1967 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1968 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1971 /* break is _not_ missing here. */
1973 case LTTNG_DOMAIN_UST
:
1975 struct ltt_ust_session
*usess
= session
->ust_session
;
1976 unsigned int chan_count
;
1980 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1981 if (chan_count
== 0) {
1982 struct lttng_channel
*attr
;
1983 /* Create default channel */
1984 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1986 ret
= LTTNG_ERR_FATAL
;
1990 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1991 if (ret
!= LTTNG_OK
) {
1995 channel_attr_destroy(attr
);
1996 chan_ust_created
= 1;
1999 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2000 free(app_ctx_provider_name
);
2002 app_ctx_name
= NULL
;
2003 app_ctx_provider_name
= NULL
;
2004 if (ret
!= LTTNG_OK
) {
2010 ret
= LTTNG_ERR_UND
;
2018 if (chan_kern_created
) {
2019 struct ltt_kernel_channel
*kchan
=
2020 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2021 session
->kernel_session
);
2022 /* Created previously, this should NOT fail. */
2024 kernel_destroy_channel(kchan
);
2027 if (chan_ust_created
) {
2028 struct ltt_ust_channel
*uchan
=
2029 trace_ust_find_channel_by_name(
2030 session
->ust_session
->domain_global
.channels
,
2031 DEFAULT_CHANNEL_NAME
);
2032 /* Created previously, this should NOT fail. */
2034 /* Remove from the channel list of the session. */
2035 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2037 trace_ust_destroy_channel(uchan
);
2040 free(app_ctx_provider_name
);
2045 static inline bool name_starts_with(const char *name
, const char *prefix
)
2047 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2049 return !strncmp(name
, prefix
, max_cmp_len
);
2052 /* Perform userspace-specific event name validation */
2053 static int validate_ust_event_name(const char *name
)
2063 * Check name against all internal UST event component namespaces used
2066 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2067 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2068 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2077 * Internal version of cmd_enable_event() with a supplemental
2078 * "internal_event" flag which is used to enable internal events which should
2079 * be hidden from clients. Such events are used in the agent implementation to
2080 * enable the events through which all "agent" events are funeled.
2082 static int _cmd_enable_event(struct ltt_session
*session
,
2083 const struct lttng_domain
*domain
,
2084 char *channel_name
, struct lttng_event
*event
,
2085 char *filter_expression
,
2086 struct lttng_bytecode
*filter
,
2087 struct lttng_event_exclusion
*exclusion
,
2088 int wpipe
, bool internal_event
)
2090 int ret
= 0, channel_created
= 0;
2091 struct lttng_channel
*attr
= NULL
;
2095 assert(channel_name
);
2097 /* If we have a filter, we must have its filter expression */
2098 assert(!(!!filter_expression
^ !!filter
));
2100 /* Normalize event name as a globbing pattern */
2101 strutils_normalize_star_glob_pattern(event
->name
);
2103 /* Normalize exclusion names as globbing patterns */
2107 for (i
= 0; i
< exclusion
->count
; i
++) {
2108 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2110 strutils_normalize_star_glob_pattern(name
);
2114 DBG("Enable event command for event \'%s\'", event
->name
);
2118 switch (domain
->type
) {
2119 case LTTNG_DOMAIN_KERNEL
:
2121 struct ltt_kernel_channel
*kchan
;
2124 * If a non-default channel has been created in the
2125 * session, explicitely require that -c chan_name needs
2128 if (session
->kernel_session
->has_non_default_channel
2129 && channel_name
[0] == '\0') {
2130 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2134 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2135 session
->kernel_session
);
2136 if (kchan
== NULL
) {
2137 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2138 LTTNG_BUFFER_GLOBAL
);
2140 ret
= LTTNG_ERR_FATAL
;
2143 if (lttng_strncpy(attr
->name
, channel_name
,
2144 sizeof(attr
->name
))) {
2145 ret
= LTTNG_ERR_INVALID
;
2149 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2150 if (ret
!= LTTNG_OK
) {
2153 channel_created
= 1;
2156 /* Get the newly created kernel channel pointer */
2157 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2158 session
->kernel_session
);
2159 if (kchan
== NULL
) {
2160 /* This sould not happen... */
2161 ret
= LTTNG_ERR_FATAL
;
2165 switch (event
->type
) {
2166 case LTTNG_EVENT_ALL
:
2168 char *filter_expression_a
= NULL
;
2169 struct lttng_bytecode
*filter_a
= NULL
;
2172 * We need to duplicate filter_expression and filter,
2173 * because ownership is passed to first enable
2176 if (filter_expression
) {
2177 filter_expression_a
= strdup(filter_expression
);
2178 if (!filter_expression_a
) {
2179 ret
= LTTNG_ERR_FATAL
;
2184 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2186 free(filter_expression_a
);
2187 ret
= LTTNG_ERR_FATAL
;
2190 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2192 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2193 ret
= event_kernel_enable_event(kchan
, event
,
2194 filter_expression
, filter
);
2195 /* We have passed ownership */
2196 filter_expression
= NULL
;
2198 if (ret
!= LTTNG_OK
) {
2199 if (channel_created
) {
2200 /* Let's not leak a useless channel. */
2201 kernel_destroy_channel(kchan
);
2203 free(filter_expression_a
);
2207 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2208 ret
= event_kernel_enable_event(kchan
, event
,
2209 filter_expression_a
, filter_a
);
2210 /* We have passed ownership */
2211 filter_expression_a
= NULL
;
2213 if (ret
!= LTTNG_OK
) {
2218 case LTTNG_EVENT_PROBE
:
2219 case LTTNG_EVENT_USERSPACE_PROBE
:
2220 case LTTNG_EVENT_FUNCTION
:
2221 case LTTNG_EVENT_FUNCTION_ENTRY
:
2222 case LTTNG_EVENT_TRACEPOINT
:
2223 ret
= event_kernel_enable_event(kchan
, event
,
2224 filter_expression
, filter
);
2225 /* We have passed ownership */
2226 filter_expression
= NULL
;
2228 if (ret
!= LTTNG_OK
) {
2229 if (channel_created
) {
2230 /* Let's not leak a useless channel. */
2231 kernel_destroy_channel(kchan
);
2236 case LTTNG_EVENT_SYSCALL
:
2237 ret
= event_kernel_enable_event(kchan
, event
,
2238 filter_expression
, filter
);
2239 /* We have passed ownership */
2240 filter_expression
= NULL
;
2242 if (ret
!= LTTNG_OK
) {
2247 ret
= LTTNG_ERR_UNK
;
2251 kernel_wait_quiescent();
2254 case LTTNG_DOMAIN_UST
:
2256 struct ltt_ust_channel
*uchan
;
2257 struct ltt_ust_session
*usess
= session
->ust_session
;
2262 * If a non-default channel has been created in the
2263 * session, explicitely require that -c chan_name needs
2266 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2267 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2271 /* Get channel from global UST domain */
2272 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2274 if (uchan
== NULL
) {
2275 /* Create default channel */
2276 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2277 usess
->buffer_type
);
2279 ret
= LTTNG_ERR_FATAL
;
2282 if (lttng_strncpy(attr
->name
, channel_name
,
2283 sizeof(attr
->name
))) {
2284 ret
= LTTNG_ERR_INVALID
;
2288 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2289 if (ret
!= LTTNG_OK
) {
2293 /* Get the newly created channel reference back */
2294 uchan
= trace_ust_find_channel_by_name(
2295 usess
->domain_global
.channels
, channel_name
);
2299 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2301 * Don't allow users to add UST events to channels which
2302 * are assigned to a userspace subdomain (JUL, Log4J,
2305 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2309 if (!internal_event
) {
2311 * Ensure the event name is not reserved for internal
2314 ret
= validate_ust_event_name(event
->name
);
2316 WARN("Userspace event name %s failed validation.",
2318 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2323 /* At this point, the session and channel exist on the tracer */
2324 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2325 filter_expression
, filter
, exclusion
,
2327 /* We have passed ownership */
2328 filter_expression
= NULL
;
2331 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2332 goto already_enabled
;
2333 } else if (ret
!= LTTNG_OK
) {
2338 case LTTNG_DOMAIN_LOG4J
:
2339 case LTTNG_DOMAIN_JUL
:
2340 case LTTNG_DOMAIN_PYTHON
:
2342 const char *default_event_name
, *default_chan_name
;
2344 struct lttng_event uevent
;
2345 struct lttng_domain tmp_dom
;
2346 struct ltt_ust_session
*usess
= session
->ust_session
;
2350 if (!agent_tracing_is_enabled()) {
2351 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2352 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2356 agt
= trace_ust_find_agent(usess
, domain
->type
);
2358 agt
= agent_create(domain
->type
);
2360 ret
= LTTNG_ERR_NOMEM
;
2363 agent_add(agt
, usess
->agents
);
2366 /* Create the default tracepoint. */
2367 memset(&uevent
, 0, sizeof(uevent
));
2368 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2369 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2370 default_event_name
= event_get_default_agent_ust_name(
2372 if (!default_event_name
) {
2373 ret
= LTTNG_ERR_FATAL
;
2376 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2377 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2380 * The domain type is changed because we are about to enable the
2381 * default channel and event for the JUL domain that are hardcoded.
2382 * This happens in the UST domain.
2384 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2385 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2387 switch (domain
->type
) {
2388 case LTTNG_DOMAIN_LOG4J
:
2389 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2391 case LTTNG_DOMAIN_JUL
:
2392 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2394 case LTTNG_DOMAIN_PYTHON
:
2395 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2398 /* The switch/case we are in makes this impossible */
2403 char *filter_expression_copy
= NULL
;
2404 struct lttng_bytecode
*filter_copy
= NULL
;
2407 const size_t filter_size
= sizeof(
2408 struct lttng_bytecode
)
2411 filter_copy
= zmalloc(filter_size
);
2413 ret
= LTTNG_ERR_NOMEM
;
2416 memcpy(filter_copy
, filter
, filter_size
);
2418 filter_expression_copy
=
2419 strdup(filter_expression
);
2420 if (!filter_expression
) {
2421 ret
= LTTNG_ERR_NOMEM
;
2424 if (!filter_expression_copy
|| !filter_copy
) {
2425 free(filter_expression_copy
);
2431 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2432 (char *) default_chan_name
,
2433 &uevent
, filter_expression_copy
,
2434 filter_copy
, NULL
, wpipe
);
2437 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2438 goto already_enabled
;
2439 } else if (ret
!= LTTNG_OK
) {
2443 /* The wild card * means that everything should be enabled. */
2444 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2445 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2448 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2452 filter_expression
= NULL
;
2453 if (ret
!= LTTNG_OK
) {
2460 ret
= LTTNG_ERR_UND
;
2468 free(filter_expression
);
2471 channel_attr_destroy(attr
);
2477 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2478 * We own filter, exclusion, and filter_expression.
2480 int cmd_enable_event(struct ltt_session
*session
,
2481 const struct lttng_domain
*domain
,
2482 char *channel_name
, struct lttng_event
*event
,
2483 char *filter_expression
,
2484 struct lttng_bytecode
*filter
,
2485 struct lttng_event_exclusion
*exclusion
,
2488 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2489 filter_expression
, filter
, exclusion
, wpipe
, false);
2493 * Enable an event which is internal to LTTng. An internal should
2494 * never be made visible to clients and are immune to checks such as
2497 static int cmd_enable_event_internal(struct ltt_session
*session
,
2498 const struct lttng_domain
*domain
,
2499 char *channel_name
, struct lttng_event
*event
,
2500 char *filter_expression
,
2501 struct lttng_bytecode
*filter
,
2502 struct lttng_event_exclusion
*exclusion
,
2505 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2506 filter_expression
, filter
, exclusion
, wpipe
, true);
2510 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2512 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2513 struct lttng_event
**events
)
2516 ssize_t nb_events
= 0;
2519 case LTTNG_DOMAIN_KERNEL
:
2520 nb_events
= kernel_list_events(events
);
2521 if (nb_events
< 0) {
2522 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2526 case LTTNG_DOMAIN_UST
:
2527 nb_events
= ust_app_list_events(events
);
2528 if (nb_events
< 0) {
2529 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2533 case LTTNG_DOMAIN_LOG4J
:
2534 case LTTNG_DOMAIN_JUL
:
2535 case LTTNG_DOMAIN_PYTHON
:
2536 nb_events
= agent_list_events(events
, domain
);
2537 if (nb_events
< 0) {
2538 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2543 ret
= LTTNG_ERR_UND
;
2550 /* Return negative value to differentiate return code */
2555 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2557 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2558 struct lttng_event_field
**fields
)
2561 ssize_t nb_fields
= 0;
2564 case LTTNG_DOMAIN_UST
:
2565 nb_fields
= ust_app_list_event_fields(fields
);
2566 if (nb_fields
< 0) {
2567 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2571 case LTTNG_DOMAIN_KERNEL
:
2572 default: /* fall-through */
2573 ret
= LTTNG_ERR_UND
;
2580 /* Return negative value to differentiate return code */
2584 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2586 return syscall_table_list(events
);
2590 * Command LTTNG_START_TRACE processed by the client thread.
2592 * Called with session mutex held.
2594 int cmd_start_trace(struct ltt_session
*session
)
2596 enum lttng_error_code ret
;
2597 unsigned long nb_chan
= 0;
2598 struct ltt_kernel_session
*ksession
;
2599 struct ltt_ust_session
*usess
;
2600 const bool session_rotated_after_last_stop
=
2601 session
->rotated_after_last_stop
;
2602 const bool session_cleared_after_last_stop
=
2603 session
->cleared_after_last_stop
;
2607 /* Ease our life a bit ;) */
2608 ksession
= session
->kernel_session
;
2609 usess
= session
->ust_session
;
2611 /* Is the session already started? */
2612 if (session
->active
) {
2613 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2614 /* Perform nothing */
2618 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2619 !session
->current_trace_chunk
) {
2621 * A rotation was launched while the session was stopped and
2622 * it has not been completed yet. It is not possible to start
2623 * the session since starting the session here would require a
2624 * rotation from "NULL" to a new trace chunk. That rotation
2625 * would overlap with the ongoing rotation, which is not
2628 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2630 ret
= LTTNG_ERR_ROTATION_PENDING
;
2635 * Starting a session without channel is useless since after that it's not
2636 * possible to enable channel thus inform the client.
2638 if (usess
&& usess
->domain_global
.channels
) {
2639 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2642 nb_chan
+= ksession
->channel_count
;
2645 ret
= LTTNG_ERR_NO_CHANNEL
;
2649 session
->active
= 1;
2650 session
->rotated_after_last_stop
= false;
2651 session
->cleared_after_last_stop
= false;
2652 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2653 if (!session
->has_been_started
) {
2654 struct lttng_trace_chunk
*trace_chunk
;
2656 DBG("Creating initial trace chunk of session \"%s\"",
2658 trace_chunk
= session_create_new_trace_chunk(
2659 session
, NULL
, NULL
, NULL
);
2661 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2664 assert(!session
->current_trace_chunk
);
2665 ret
= session_set_trace_chunk(session
, trace_chunk
,
2667 lttng_trace_chunk_put(trace_chunk
);
2669 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2673 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2676 * Rotate existing streams into the new chunk.
2677 * This is a "quiet" rotation has no client has
2678 * explicitly requested this operation.
2680 * There is also no need to wait for the rotation
2681 * to complete as it will happen immediately. No data
2682 * was produced as the session was stopped, so the
2683 * rotation should happen on reception of the command.
2685 ret
= cmd_rotate_session(session
, NULL
, true,
2686 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2687 if (ret
!= LTTNG_OK
) {
2693 /* Kernel tracing */
2694 if (ksession
!= NULL
) {
2695 DBG("Start kernel tracing session %s", session
->name
);
2696 ret
= start_kernel_session(ksession
);
2697 if (ret
!= LTTNG_OK
) {
2702 /* Flag session that trace should start automatically */
2704 int int_ret
= ust_app_start_trace_all(usess
);
2707 ret
= LTTNG_ERR_UST_START_FAIL
;
2713 * Open a packet in every stream of the session to ensure that viewers
2714 * can correctly identify the boundaries of the periods during which
2715 * tracing was active for this session.
2717 ret
= session_open_packets(session
);
2718 if (ret
!= LTTNG_OK
) {
2723 * Clear the flag that indicates that a rotation was done while the
2724 * session was stopped.
2726 session
->rotated_after_last_stop
= false;
2728 if (session
->rotate_timer_period
) {
2729 int int_ret
= timer_session_rotation_schedule_timer_start(
2730 session
, session
->rotate_timer_period
);
2733 ERR("Failed to enable rotate timer");
2734 ret
= LTTNG_ERR_UNK
;
2742 if (ret
== LTTNG_OK
) {
2743 /* Flag this after a successful start. */
2744 session
->has_been_started
|= 1;
2746 session
->active
= 0;
2747 /* Restore initial state on error. */
2748 session
->rotated_after_last_stop
=
2749 session_rotated_after_last_stop
;
2750 session
->cleared_after_last_stop
=
2751 session_cleared_after_last_stop
;
2758 * Command LTTNG_STOP_TRACE processed by the client thread.
2760 int cmd_stop_trace(struct ltt_session
*session
)
2763 struct ltt_kernel_session
*ksession
;
2764 struct ltt_ust_session
*usess
;
2768 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2770 ksession
= session
->kernel_session
;
2771 usess
= session
->ust_session
;
2773 /* Session is not active. Skip everythong and inform the client. */
2774 if (!session
->active
) {
2775 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2779 ret
= stop_kernel_session(ksession
);
2780 if (ret
!= LTTNG_OK
) {
2784 if (usess
&& usess
->active
) {
2785 ret
= ust_app_stop_trace_all(usess
);
2787 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2792 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2794 /* Flag inactive after a successful stop. */
2795 session
->active
= 0;
2803 * Set the base_path of the session only if subdir of a control uris is set.
2804 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2806 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2808 struct lttng_uri
*uris
)
2813 for (i
= 0; i
< nb_uri
; i
++) {
2814 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2815 uris
[i
].subdir
[0] == '\0') {
2816 /* Not interested in these URIs */
2820 if (session
->base_path
!= NULL
) {
2821 free(session
->base_path
);
2822 session
->base_path
= NULL
;
2825 /* Set session base_path */
2826 session
->base_path
= strdup(uris
[i
].subdir
);
2827 if (!session
->base_path
) {
2828 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2829 uris
[i
].subdir
, session
->name
);
2830 ret
= LTTNG_ERR_NOMEM
;
2833 DBG2("Setting base path \"%s\" for session \"%s\"",
2834 session
->base_path
, session
->name
);
2842 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2844 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2845 struct lttng_uri
*uris
)
2848 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2849 struct ltt_ust_session
*usess
= session
->ust_session
;
2855 /* Can't set consumer URI if the session is active. */
2856 if (session
->active
) {
2857 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2862 * Set the session base path if any. This is done inside
2863 * cmd_set_consumer_uri to preserve backward compatibility of the
2864 * previous session creation api vs the session descriptor api.
2866 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2867 if (ret
!= LTTNG_OK
) {
2871 /* Set the "global" consumer URIs */
2872 for (i
= 0; i
< nb_uri
; i
++) {
2873 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2875 if (ret
!= LTTNG_OK
) {
2880 /* Set UST session URIs */
2881 if (session
->ust_session
) {
2882 for (i
= 0; i
< nb_uri
; i
++) {
2883 ret
= add_uri_to_consumer(session
,
2884 session
->ust_session
->consumer
,
2885 &uris
[i
], LTTNG_DOMAIN_UST
);
2886 if (ret
!= LTTNG_OK
) {
2892 /* Set kernel session URIs */
2893 if (session
->kernel_session
) {
2894 for (i
= 0; i
< nb_uri
; i
++) {
2895 ret
= add_uri_to_consumer(session
,
2896 session
->kernel_session
->consumer
,
2897 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2898 if (ret
!= LTTNG_OK
) {
2905 * Make sure to set the session in output mode after we set URI since a
2906 * session can be created without URL (thus flagged in no output mode).
2908 session
->output_traces
= 1;
2910 ksess
->output_traces
= 1;
2914 usess
->output_traces
= 1;
2925 enum lttng_error_code
set_session_output_from_descriptor(
2926 struct ltt_session
*session
,
2927 const struct lttng_session_descriptor
*descriptor
)
2930 enum lttng_error_code ret_code
= LTTNG_OK
;
2931 enum lttng_session_descriptor_type session_type
=
2932 lttng_session_descriptor_get_type(descriptor
);
2933 enum lttng_session_descriptor_output_type output_type
=
2934 lttng_session_descriptor_get_output_type(descriptor
);
2935 struct lttng_uri uris
[2] = {};
2936 size_t uri_count
= 0;
2938 switch (output_type
) {
2939 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2941 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2942 lttng_session_descriptor_get_local_output_uri(descriptor
,
2946 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2947 lttng_session_descriptor_get_network_output_uris(descriptor
,
2948 &uris
[0], &uris
[1]);
2952 ret_code
= LTTNG_ERR_INVALID
;
2956 switch (session_type
) {
2957 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2959 struct snapshot_output
*new_output
= NULL
;
2961 new_output
= snapshot_output_alloc();
2963 ret_code
= LTTNG_ERR_NOMEM
;
2967 ret
= snapshot_output_init_with_uri(session
,
2968 DEFAULT_SNAPSHOT_MAX_SIZE
,
2969 NULL
, uris
, uri_count
, session
->consumer
,
2970 new_output
, &session
->snapshot
);
2972 ret_code
= (ret
== -ENOMEM
) ?
2973 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2974 snapshot_output_destroy(new_output
);
2977 snapshot_add_output(&session
->snapshot
, new_output
);
2980 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2981 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2983 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2987 ret_code
= LTTNG_ERR_INVALID
;
2995 enum lttng_error_code
cmd_create_session_from_descriptor(
2996 struct lttng_session_descriptor
*descriptor
,
2997 const lttng_sock_cred
*creds
,
2998 const char *home_path
)
3001 enum lttng_error_code ret_code
;
3002 const char *session_name
;
3003 struct ltt_session
*new_session
= NULL
;
3004 enum lttng_session_descriptor_status descriptor_status
;
3006 session_lock_list();
3008 if (*home_path
!= '/') {
3009 ERR("Home path provided by client is not absolute");
3010 ret_code
= LTTNG_ERR_INVALID
;
3015 descriptor_status
= lttng_session_descriptor_get_session_name(
3016 descriptor
, &session_name
);
3017 switch (descriptor_status
) {
3018 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3020 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3021 session_name
= NULL
;
3024 ret_code
= LTTNG_ERR_INVALID
;
3028 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3030 if (ret_code
!= LTTNG_OK
) {
3034 if (!session_name
) {
3035 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3038 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3043 if (!lttng_session_descriptor_is_output_destination_initialized(
3046 * Only include the session's creation time in the output
3047 * destination if the name of the session itself was
3048 * not auto-generated.
3050 ret_code
= lttng_session_descriptor_set_default_output(
3052 session_name
? &new_session
->creation_time
: NULL
,
3054 if (ret_code
!= LTTNG_OK
) {
3058 new_session
->has_user_specified_directory
=
3059 lttng_session_descriptor_has_output_directory(
3063 switch (lttng_session_descriptor_get_type(descriptor
)) {
3064 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3065 new_session
->snapshot_mode
= 1;
3067 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3068 new_session
->live_timer
=
3069 lttng_session_descriptor_live_get_timer_interval(
3076 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3077 if (ret_code
!= LTTNG_OK
) {
3080 new_session
->consumer
->enabled
= 1;
3081 ret_code
= LTTNG_OK
;
3083 /* Release reference provided by the session_create function. */
3084 session_put(new_session
);
3085 if (ret_code
!= LTTNG_OK
&& new_session
) {
3086 /* Release the global reference on error. */
3087 session_destroy(new_session
);
3089 session_unlock_list();
3093 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3094 struct lttng_session_descriptor
**return_descriptor
)
3097 size_t payload_size
;
3098 struct lttng_dynamic_buffer payload
;
3099 struct lttng_buffer_view home_dir_view
;
3100 struct lttng_buffer_view session_descriptor_view
;
3101 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3102 enum lttng_error_code ret_code
;
3104 lttng_dynamic_buffer_init(&payload
);
3105 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3107 ret_code
= LTTNG_ERR_INVALID
;
3110 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3111 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3112 ret_code
= LTTNG_ERR_INVALID
;
3116 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3117 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3118 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3120 ret_code
= LTTNG_ERR_NOMEM
;
3124 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3126 ERR("Reception of session descriptor failed, aborting.");
3127 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3131 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3134 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3135 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3136 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3137 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3138 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3142 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3144 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3145 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3146 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3147 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3148 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3152 ret
= lttng_session_descriptor_create_from_buffer(
3153 &session_descriptor_view
, &session_descriptor
);
3155 ERR("Failed to create session descriptor from payload of \"create session\" command");
3156 ret_code
= LTTNG_ERR_INVALID
;
3161 * Sets the descriptor's auto-generated properties (name, output) if
3164 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3166 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3167 if (ret_code
!= LTTNG_OK
) {
3171 ret_code
= LTTNG_OK
;
3172 *return_descriptor
= session_descriptor
;
3173 session_descriptor
= NULL
;
3175 lttng_dynamic_buffer_reset(&payload
);
3176 lttng_session_descriptor_destroy(session_descriptor
);
3181 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3182 void *_reply_context
)
3186 const struct cmd_destroy_session_reply_context
*reply_context
=
3188 struct lttng_dynamic_buffer payload
;
3189 struct lttcomm_session_destroy_command_header cmd_header
;
3190 struct lttng_trace_archive_location
*location
= NULL
;
3191 struct lttcomm_lttng_msg llm
= {
3192 .cmd_type
= LTTNG_DESTROY_SESSION
,
3193 .ret_code
= reply_context
->destruction_status
,
3196 sizeof(struct lttcomm_session_destroy_command_header
),
3199 size_t payload_size_before_location
;
3201 lttng_dynamic_buffer_init(&payload
);
3203 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3205 ERR("Failed to append session destruction message");
3209 cmd_header
.rotation_state
=
3210 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3211 session
->rotation_state
:
3212 LTTNG_ROTATION_STATE_NO_ROTATION
);
3213 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3214 sizeof(cmd_header
));
3216 ERR("Failed to append session destruction command header");
3220 if (!reply_context
->implicit_rotation_on_destroy
) {
3221 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3225 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3226 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3231 location
= session_get_trace_archive_location(session
);
3233 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3238 payload_size_before_location
= payload
.size
;
3239 comm_ret
= lttng_trace_archive_location_serialize(location
,
3242 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3246 /* Update the message to indicate the location's length. */
3247 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3248 payload
.size
- payload_size_before_location
;
3250 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3251 payload
.data
, payload
.size
);
3252 if (comm_ret
!= (ssize_t
) payload
.size
) {
3253 ERR("Failed to send result of the destruction of session \"%s\" to client",
3257 ret
= close(reply_context
->reply_sock_fd
);
3259 PERROR("Failed to close client socket in deferred session destroy reply");
3261 lttng_dynamic_buffer_reset(&payload
);
3262 free(_reply_context
);
3266 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3268 * Called with session lock held.
3270 int cmd_destroy_session(struct ltt_session
*session
,
3271 struct notification_thread_handle
*notification_thread_handle
,
3275 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3276 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3279 reply_context
= zmalloc(sizeof(*reply_context
));
3280 if (!reply_context
) {
3281 ret
= LTTNG_ERR_NOMEM
;
3284 reply_context
->reply_sock_fd
= *sock_fd
;
3290 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3292 if (session
->active
) {
3293 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3295 ret
= cmd_stop_trace(session
);
3296 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3297 /* Carry on with the destruction of the session. */
3298 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3299 session
->name
, lttng_strerror(-ret
));
3300 destruction_last_error
= ret
;
3304 if (session
->rotation_schedule_timer_enabled
) {
3305 if (timer_session_rotation_schedule_timer_stop(
3307 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3309 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3313 if (session
->rotate_size
) {
3314 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3315 session
->rotate_size
= 0;
3318 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3320 * Perform a last rotation on destruction if rotations have
3321 * occurred during the session's lifetime.
3323 ret
= cmd_rotate_session(session
, NULL
, false,
3324 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3325 if (ret
!= LTTNG_OK
) {
3326 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3327 session
->name
, lttng_strerror(-ret
));
3328 destruction_last_error
= -ret
;
3330 if (reply_context
) {
3331 reply_context
->implicit_rotation_on_destroy
= true;
3333 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3335 * The user has not triggered a session rotation. However, to
3336 * ensure all data has been consumed, the session is rotated
3337 * to a 'null' trace chunk before it is destroyed.
3339 * This is a "quiet" rotation meaning that no notification is
3340 * emitted and no renaming of the current trace chunk takes
3343 ret
= cmd_rotate_session(session
, NULL
, true,
3344 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3346 * Rotation operations may not be supported by the kernel
3347 * tracer. Hence, do not consider this implicit rotation as
3348 * a session destruction error. The library has already stopped
3349 * the session and waited for pending data; there is nothing
3350 * left to do but complete the destruction of the session.
3352 if (ret
!= LTTNG_OK
&&
3353 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3354 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3355 session
->name
, lttng_strerror(ret
));
3356 destruction_last_error
= -ret
;
3360 if (session
->shm_path
[0]) {
3362 * When a session is created with an explicit shm_path,
3363 * the consumer daemon will create its shared memory files
3364 * at that location and will *not* unlink them. This is normal
3365 * as the intention of that feature is to make it possible
3366 * to retrieve the content of those files should a crash occur.
3368 * To ensure the content of those files can be used, the
3369 * sessiond daemon will replicate the content of the metadata
3370 * cache in a metadata file.
3372 * On clean-up, it is expected that the consumer daemon will
3373 * unlink the shared memory files and that the session daemon
3374 * will unlink the metadata file. Then, the session's directory
3375 * in the shm path can be removed.
3377 * Unfortunately, a flaw in the design of the sessiond's and
3378 * consumerd's tear down of channels makes it impossible to
3379 * determine when the sessiond _and_ the consumerd have both
3380 * destroyed their representation of a channel. For one, the
3381 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3382 * callbacks in both daemons.
3384 * However, it is also impossible for the sessiond to know when
3385 * the consumer daemon is done destroying its channel(s) since
3386 * it occurs as a reaction to the closing of the channel's file
3387 * descriptor. There is no resulting communication initiated
3388 * from the consumerd to the sessiond to confirm that the
3389 * operation is completed (and was successful).
3391 * Until this is all fixed, the session daemon checks for the
3392 * removal of the session's shm path which makes it possible
3393 * to safely advertise a session as having been destroyed.
3395 * Prior to this fix, it was not possible to reliably save
3396 * a session making use of the --shm-path option, destroy it,
3397 * and load it again. This is because the creation of the
3398 * session would fail upon seeing the session's shm path
3399 * already in existence.
3401 * Note that none of the error paths in the check for the
3402 * directory's existence return an error. This is normal
3403 * as there isn't much that can be done. The session will
3404 * be destroyed properly, except that we can't offer the
3405 * guarantee that the same session can be re-created.
3407 current_completion_handler
= &destroy_completion_handler
.handler
;
3408 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3410 sizeof(destroy_completion_handler
.shm_path
));
3415 * The session is destroyed. However, note that the command context
3416 * still holds a reference to the session, thus delaying its destruction
3417 * _at least_ up to the point when that reference is released.
3419 session_destroy(session
);
3420 if (reply_context
) {
3421 reply_context
->destruction_status
= destruction_last_error
;
3422 ret
= session_add_destroy_notifier(session
,
3423 cmd_destroy_session_reply
,
3424 (void *) reply_context
);
3426 ret
= LTTNG_ERR_FATAL
;
3438 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3440 int cmd_register_consumer(struct ltt_session
*session
,
3441 enum lttng_domain_type domain
, const char *sock_path
,
3442 struct consumer_data
*cdata
)
3445 struct consumer_socket
*socket
= NULL
;
3452 case LTTNG_DOMAIN_KERNEL
:
3454 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3458 /* Can't register a consumer if there is already one */
3459 if (ksess
->consumer_fds_sent
!= 0) {
3460 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3464 sock
= lttcomm_connect_unix_sock(sock_path
);
3466 ret
= LTTNG_ERR_CONNECT_FAIL
;
3469 cdata
->cmd_sock
= sock
;
3471 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3472 if (socket
== NULL
) {
3475 PERROR("close register consumer");
3477 cdata
->cmd_sock
= -1;
3478 ret
= LTTNG_ERR_FATAL
;
3482 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3483 if (socket
->lock
== NULL
) {
3484 PERROR("zmalloc pthread mutex");
3485 ret
= LTTNG_ERR_FATAL
;
3488 pthread_mutex_init(socket
->lock
, NULL
);
3489 socket
->registered
= 1;
3492 consumer_add_socket(socket
, ksess
->consumer
);
3495 pthread_mutex_lock(&cdata
->pid_mutex
);
3497 pthread_mutex_unlock(&cdata
->pid_mutex
);
3502 /* TODO: Userspace tracing */
3503 ret
= LTTNG_ERR_UND
;
3511 consumer_destroy_socket(socket
);
3517 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3519 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3520 struct lttng_domain
**domains
)
3525 struct lttng_ht_iter iter
;
3527 if (session
->kernel_session
!= NULL
) {
3528 DBG3("Listing domains found kernel domain");
3532 if (session
->ust_session
!= NULL
) {
3533 DBG3("Listing domains found UST global domain");
3537 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3539 if (agt
->being_used
) {
3550 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3551 if (*domains
== NULL
) {
3552 ret
= LTTNG_ERR_FATAL
;
3556 if (session
->kernel_session
!= NULL
) {
3557 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3559 /* Kernel session buffer type is always GLOBAL */
3560 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3565 if (session
->ust_session
!= NULL
) {
3566 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3567 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3571 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3573 if (agt
->being_used
) {
3574 (*domains
)[index
].type
= agt
->domain
;
3575 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3585 /* Return negative value to differentiate return code */
3591 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3593 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3594 struct ltt_session
*session
, struct lttng_channel
**channels
)
3596 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3599 case LTTNG_DOMAIN_KERNEL
:
3600 if (session
->kernel_session
!= NULL
) {
3601 nb_chan
= session
->kernel_session
->channel_count
;
3603 DBG3("Number of kernel channels %zd", nb_chan
);
3605 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3609 case LTTNG_DOMAIN_UST
:
3610 if (session
->ust_session
!= NULL
) {
3612 nb_chan
= lttng_ht_get_count(
3613 session
->ust_session
->domain_global
.channels
);
3616 DBG3("Number of UST global channels %zd", nb_chan
);
3618 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3623 ret
= -LTTNG_ERR_UND
;
3628 const size_t channel_size
= sizeof(struct lttng_channel
) +
3629 sizeof(struct lttng_channel_extended
);
3630 struct lttng_channel_extended
*channel_exts
;
3632 payload_size
= nb_chan
* channel_size
;
3633 *channels
= zmalloc(payload_size
);
3634 if (*channels
== NULL
) {
3635 ret
= -LTTNG_ERR_FATAL
;
3639 channel_exts
= ((void *) *channels
) +
3640 (nb_chan
* sizeof(struct lttng_channel
));
3641 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3642 if (ret
!= LTTNG_OK
) {
3657 * Command LTTNG_LIST_EVENTS processed by the client thread.
3659 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3660 struct ltt_session
*session
, char *channel_name
,
3661 struct lttng_payload
*payload
)
3664 ssize_t nb_events
= 0;
3665 struct lttcomm_event_command_header cmd_header
= {};
3666 const size_t cmd_header_offset
= payload
->buffer
.size
;
3668 ret
= lttng_dynamic_buffer_append(
3669 &payload
->buffer
, &cmd_header
, sizeof(cmd_header
));
3671 ret
= LTTNG_ERR_NOMEM
;
3676 case LTTNG_DOMAIN_KERNEL
:
3677 if (session
->kernel_session
!= NULL
) {
3678 nb_events
= list_lttng_kernel_events(channel_name
,
3679 session
->kernel_session
, payload
);
3682 case LTTNG_DOMAIN_UST
:
3684 if (session
->ust_session
!= NULL
) {
3685 nb_events
= list_lttng_ust_global_events(channel_name
,
3686 &session
->ust_session
->domain_global
,
3691 case LTTNG_DOMAIN_LOG4J
:
3692 case LTTNG_DOMAIN_JUL
:
3693 case LTTNG_DOMAIN_PYTHON
:
3694 if (session
->ust_session
) {
3695 struct lttng_ht_iter iter
;
3699 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3700 &iter
.iter
, agt
, node
.node
) {
3701 if (agt
->domain
== domain
) {
3702 nb_events
= list_lttng_agent_events(
3711 ret
= LTTNG_ERR_UND
;
3715 ((struct lttcomm_event_command_header
*) (payload
->buffer
.data
+
3716 cmd_header_offset
))->nb_events
= (uint32_t) nb_events
;
3721 /* Return negative value to differentiate return code */
3726 * Using the session list, filled a lttng_session array to send back to the
3727 * client for session listing.
3729 * The session list lock MUST be acquired before calling this function. Use
3730 * session_lock_list() and session_unlock_list().
3732 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3733 size_t session_count
, uid_t uid
, gid_t gid
)
3737 struct ltt_session
*session
;
3738 struct ltt_session_list
*list
= session_get_list();
3739 struct lttng_session_extended
*extended
=
3740 (typeof(extended
)) (&sessions
[session_count
]);
3742 DBG("Getting all available session for UID %d GID %d",
3745 * Iterate over session list and append data after the control struct in
3748 cds_list_for_each_entry(session
, &list
->head
, list
) {
3749 if (!session_get(session
)) {
3753 * Only list the sessions the user can control.
3755 if (!session_access_ok(session
, uid
) ||
3756 session
->destroyed
) {
3757 session_put(session
);
3761 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3762 struct ltt_ust_session
*usess
= session
->ust_session
;
3764 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3765 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3766 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3767 ret
= build_network_session_path(sessions
[i
].path
,
3768 sizeof(sessions
[i
].path
), session
);
3770 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3771 session
->consumer
->dst
.session_root_path
);
3774 PERROR("snprintf session path");
3775 session_put(session
);
3779 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3780 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3781 sessions
[i
].enabled
= session
->active
;
3782 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3783 sessions
[i
].live_timer_interval
= session
->live_timer
;
3784 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3785 extended
[i
].creation_time
.is_set
= 1;
3787 session_put(session
);
3792 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3793 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3795 int cmd_data_pending(struct ltt_session
*session
)
3798 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3799 struct ltt_ust_session
*usess
= session
->ust_session
;
3803 DBG("Data pending for session %s", session
->name
);
3805 /* Session MUST be stopped to ask for data availability. */
3806 if (session
->active
) {
3807 ret
= LTTNG_ERR_SESSION_STARTED
;
3811 * If stopped, just make sure we've started before else the above call
3812 * will always send that there is data pending.
3814 * The consumer assumes that when the data pending command is received,
3815 * the trace has been started before or else no output data is written
3816 * by the streams which is a condition for data pending. So, this is
3817 * *VERY* important that we don't ask the consumer before a start
3820 if (!session
->has_been_started
) {
3826 /* A rotation is still pending, we have to wait. */
3827 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3828 DBG("Rotate still pending for session %s", session
->name
);
3833 if (ksess
&& ksess
->consumer
) {
3834 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3836 /* Data is still being extracted for the kernel. */
3841 if (usess
&& usess
->consumer
) {
3842 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3844 /* Data is still being extracted for the kernel. */
3849 /* Data is ready to be read by a viewer */
3857 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3859 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3861 int cmd_snapshot_add_output(struct ltt_session
*session
,
3862 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3865 struct snapshot_output
*new_output
;
3870 DBG("Cmd snapshot add output for session %s", session
->name
);
3873 * Can't create an output if the session is not set in no-output mode.
3875 if (session
->output_traces
) {
3876 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3880 if (session
->has_non_mmap_channel
) {
3881 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3885 /* Only one output is allowed until we have the "tee" feature. */
3886 if (session
->snapshot
.nb_output
== 1) {
3887 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3891 new_output
= snapshot_output_alloc();
3893 ret
= LTTNG_ERR_NOMEM
;
3897 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3898 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3899 &session
->snapshot
);
3901 if (ret
== -ENOMEM
) {
3902 ret
= LTTNG_ERR_NOMEM
;
3904 ret
= LTTNG_ERR_INVALID
;
3910 snapshot_add_output(&session
->snapshot
, new_output
);
3912 *id
= new_output
->id
;
3919 snapshot_output_destroy(new_output
);
3925 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3927 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3929 int cmd_snapshot_del_output(struct ltt_session
*session
,
3930 const struct lttng_snapshot_output
*output
)
3933 struct snapshot_output
*sout
= NULL
;
3941 * Permission denied to create an output if the session is not
3942 * set in no output mode.
3944 if (session
->output_traces
) {
3945 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3950 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3952 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3953 } else if (*output
->name
!= '\0') {
3954 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3956 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3959 ret
= LTTNG_ERR_INVALID
;
3963 snapshot_delete_output(&session
->snapshot
, sout
);
3964 snapshot_output_destroy(sout
);
3973 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3975 * If no output is available, outputs is untouched and 0 is returned.
3977 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3979 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3980 struct lttng_snapshot_output
**outputs
)
3983 struct lttng_snapshot_output
*list
= NULL
;
3984 struct lttng_ht_iter iter
;
3985 struct snapshot_output
*output
;
3990 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3993 * Permission denied to create an output if the session is not
3994 * set in no output mode.
3996 if (session
->output_traces
) {
3997 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4001 if (session
->snapshot
.nb_output
== 0) {
4006 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4008 ret
= -LTTNG_ERR_NOMEM
;
4012 /* Copy list from session to the new list object. */
4014 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4015 output
, node
.node
) {
4016 assert(output
->consumer
);
4017 list
[idx
].id
= output
->id
;
4018 list
[idx
].max_size
= output
->max_size
;
4019 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4020 sizeof(list
[idx
].name
))) {
4021 ret
= -LTTNG_ERR_INVALID
;
4024 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4025 if (lttng_strncpy(list
[idx
].ctrl_url
,
4026 output
->consumer
->dst
.session_root_path
,
4027 sizeof(list
[idx
].ctrl_url
))) {
4028 ret
= -LTTNG_ERR_INVALID
;
4033 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4034 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4036 ret
= -LTTNG_ERR_NOMEM
;
4041 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4042 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4044 ret
= -LTTNG_ERR_NOMEM
;
4053 ret
= session
->snapshot
.nb_output
;
4062 * Check if we can regenerate the metadata for this session.
4063 * Only kernel, UST per-uid and non-live sessions are supported.
4065 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4068 int check_regenerate_metadata_support(struct ltt_session
*session
)
4074 if (session
->live_timer
!= 0) {
4075 ret
= LTTNG_ERR_LIVE_SESSION
;
4078 if (!session
->active
) {
4079 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4082 if (session
->ust_session
) {
4083 switch (session
->ust_session
->buffer_type
) {
4084 case LTTNG_BUFFER_PER_UID
:
4086 case LTTNG_BUFFER_PER_PID
:
4087 ret
= LTTNG_ERR_PER_PID_SESSION
;
4091 ret
= LTTNG_ERR_UNK
;
4095 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4096 session
->consumer
->relay_minor_version
< 8) {
4097 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4107 int clear_metadata_file(int fd
)
4112 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4113 if (lseek_ret
< 0) {
4119 ret
= ftruncate(fd
, 0);
4121 PERROR("ftruncate");
4130 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4133 struct buffer_reg_uid
*uid_reg
= NULL
;
4134 struct buffer_reg_session
*session_reg
= NULL
;
4137 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4138 struct ust_registry_session
*registry
;
4139 struct ust_registry_channel
*chan
;
4140 struct lttng_ht_iter iter_chan
;
4142 session_reg
= uid_reg
->registry
;
4143 registry
= session_reg
->reg
.ust
;
4145 pthread_mutex_lock(®istry
->lock
);
4146 registry
->metadata_len_sent
= 0;
4147 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4148 registry
->metadata_len
= 0;
4149 registry
->metadata_version
++;
4150 if (registry
->metadata_fd
> 0) {
4151 /* Clear the metadata file's content. */
4152 ret
= clear_metadata_file(registry
->metadata_fd
);
4154 pthread_mutex_unlock(®istry
->lock
);
4159 ret
= ust_metadata_session_statedump(registry
, NULL
,
4160 registry
->major
, registry
->minor
);
4162 pthread_mutex_unlock(®istry
->lock
);
4163 ERR("Failed to generate session metadata (err = %d)",
4167 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4169 struct ust_registry_event
*event
;
4170 struct lttng_ht_iter iter_event
;
4172 ret
= ust_metadata_channel_statedump(registry
, chan
);
4174 pthread_mutex_unlock(®istry
->lock
);
4175 ERR("Failed to generate channel metadata "
4179 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4181 ret
= ust_metadata_event_statedump(registry
,
4184 pthread_mutex_unlock(®istry
->lock
);
4185 ERR("Failed to generate event metadata "
4191 pthread_mutex_unlock(®istry
->lock
);
4200 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4202 * Ask the consumer to truncate the existing metadata file(s) and
4203 * then regenerate the metadata. Live and per-pid sessions are not
4204 * supported and return an error.
4206 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4208 int cmd_regenerate_metadata(struct ltt_session
*session
)
4214 ret
= check_regenerate_metadata_support(session
);
4219 if (session
->kernel_session
) {
4220 ret
= kernctl_session_regenerate_metadata(
4221 session
->kernel_session
->fd
);
4223 ERR("Failed to regenerate the kernel metadata");
4228 if (session
->ust_session
) {
4229 ret
= ust_regenerate_metadata(session
->ust_session
);
4231 ERR("Failed to regenerate the UST metadata");
4235 DBG("Cmd metadata regenerate for session %s", session
->name
);
4243 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4245 * Ask the tracer to regenerate a new statedump.
4247 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4249 int cmd_regenerate_statedump(struct ltt_session
*session
)
4255 if (!session
->active
) {
4256 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4260 if (session
->kernel_session
) {
4261 ret
= kernctl_session_regenerate_statedump(
4262 session
->kernel_session
->fd
);
4264 * Currently, the statedump in kernel can only fail if out
4268 if (ret
== -ENOMEM
) {
4269 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4271 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4273 ERR("Failed to regenerate the kernel statedump");
4278 if (session
->ust_session
) {
4279 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4281 * Currently, the statedump in UST always returns 0.
4284 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4285 ERR("Failed to regenerate the UST statedump");
4289 DBG("Cmd regenerate statedump for session %s", session
->name
);
4296 static enum lttng_error_code
trigger_modifies_event_notifier(
4297 const struct lttng_trigger
*trigger
, bool *adds_event_notifier
)
4299 enum lttng_error_code ret_code
= LTTNG_OK
;
4300 const struct lttng_condition
*condition
= NULL
;
4302 condition
= lttng_trigger_get_const_condition(trigger
);
4304 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4308 *adds_event_notifier
= lttng_condition_get_type(condition
) ==
4309 LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
;
4314 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4315 struct lttng_trigger
*trigger
,
4316 struct notification_thread_handle
*notification_thread
,
4317 struct lttng_trigger
**return_trigger
)
4319 enum lttng_error_code ret_code
;
4320 bool must_update_event_notifiers
;
4321 const char *trigger_name
;
4322 uid_t trigger_owner
;
4323 enum lttng_trigger_status trigger_status
;
4325 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4326 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4327 trigger_name
: "(unnamed)";
4329 trigger_status
= lttng_trigger_get_owner_uid(
4330 trigger
, &trigger_owner
);
4331 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4333 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4334 trigger_name
, (int) trigger_owner
,
4335 (int) lttng_credentials_get_uid(cmd_creds
));
4338 * Validate the trigger credentials against the command credentials.
4339 * Only the root user can register a trigger with non-matching
4342 if (!lttng_credentials_is_equal_uid(
4343 lttng_trigger_get_credentials(trigger
),
4345 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4346 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4347 trigger_name
, (int) trigger_owner
,
4348 (int) lttng_credentials_get_uid(cmd_creds
));
4349 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4355 * The bytecode generation also serves as a validation step for the
4356 * bytecode expressions.
4358 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4359 if (ret_code
!= LTTNG_OK
) {
4360 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4361 trigger_name
, (int) trigger_owner
, ret_code
);
4366 * A reference to the trigger is acquired by the notification thread.
4367 * It is safe to return the same trigger to the caller since it the
4368 * other user holds a reference.
4370 * The trigger is modified during the execution of the
4371 * "register trigger" command. However, by the time the command returns,
4372 * it is safe to use without any locking as its properties are
4375 ret_code
= notification_thread_command_register_trigger(notification_thread
,
4377 if (ret_code
!= LTTNG_OK
) {
4378 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4379 trigger_name
, (int) trigger_owner
, ret_code
);
4383 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4384 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4385 trigger_name
: "(unnamed)";
4387 ret_code
= trigger_modifies_event_notifier(trigger
, &must_update_event_notifiers
);
4388 if (ret_code
!= LTTNG_OK
) {
4389 ERR("Failed to determine if event modifies event notifiers: trigger name = '%s', trigger owner uid = %d, error code = %d",
4390 trigger_name
, (int) trigger_owner
, ret_code
);
4395 * Synchronize tracers if the trigger adds an event notifier.
4397 if (must_update_event_notifiers
) {
4398 const enum lttng_domain_type trigger_domain
=
4399 lttng_trigger_get_underlying_domain_type_restriction(trigger
);
4401 session_lock_list();
4402 switch (trigger_domain
) {
4403 case LTTNG_DOMAIN_KERNEL
:
4405 ret_code
= kernel_register_event_notifier(
4406 trigger
, cmd_creds
);
4407 if (ret_code
!= LTTNG_OK
) {
4408 const enum lttng_error_code notif_thread_unregister_ret
=
4409 notification_thread_command_unregister_trigger(
4410 notification_thread
,
4413 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4414 /* Return the original error code. */
4415 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4417 (int) trigger_owner
,
4423 case LTTNG_DOMAIN_UST
:
4424 ust_app_global_update_all_event_notifier_rules();
4426 case LTTNG_DOMAIN_NONE
:
4430 /* Agent domains. */
4431 struct agent
*agt
= agent_find_by_event_notifier_domain(
4435 agt
= agent_create(trigger_domain
);
4437 ret_code
= LTTNG_ERR_NOMEM
;
4438 goto end_unlock_session_list
;
4440 agent_add(agt
, trigger_agents_ht_by_domain
);
4443 ret_code
= trigger_agent_enable(trigger
, agt
);
4444 if (ret_code
!= LTTNG_OK
) {
4445 goto end_unlock_session_list
;
4452 session_unlock_list();
4456 * Return an updated trigger to the client.
4458 * Since a modified version of the same trigger is returned, acquire a
4459 * reference to the trigger so the caller doesn't have to care if those
4460 * are distinct instances or not.
4462 if (ret_code
== LTTNG_OK
) {
4463 lttng_trigger_get(trigger
);
4464 *return_trigger
= trigger
;
4465 /* Ownership of trigger was transferred to caller. */
4470 end_unlock_session_list
:
4471 session_unlock_list();
4475 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4476 const struct lttng_trigger
*trigger
,
4477 struct notification_thread_handle
*notification_thread
)
4479 enum lttng_error_code ret_code
;
4480 bool must_update_event_notifiers
;
4481 const char *trigger_name
;
4482 uid_t trigger_owner
;
4483 enum lttng_trigger_status trigger_status
;
4485 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4486 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(unnamed)";
4487 trigger_status
= lttng_trigger_get_owner_uid(
4488 trigger
, &trigger_owner
);
4489 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4491 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4492 trigger_name
, (int) trigger_owner
,
4493 (int) lttng_credentials_get_uid(cmd_creds
));
4496 * Validate the trigger credentials against the command credentials.
4497 * Only the root user can unregister a trigger with non-matching
4500 if (!lttng_credentials_is_equal_uid(
4501 lttng_trigger_get_credentials(trigger
),
4503 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4504 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4505 trigger_name
, (int) trigger_owner
,
4506 (int) lttng_credentials_get_uid(cmd_creds
));
4507 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4512 ret_code
= trigger_modifies_event_notifier(trigger
, &must_update_event_notifiers
);
4513 if (ret_code
!= LTTNG_OK
) {
4514 ERR("Failed to determine if event modifies event notifiers: trigger name = '%s', trigger owner uid = %d, error code = %d",
4515 trigger_name
, (int) trigger_owner
, ret_code
);
4519 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4521 if (ret_code
!= LTTNG_OK
) {
4522 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4523 trigger_name
, (int) trigger_owner
, ret_code
);
4527 * Synchronize tracers if the trigger removes an event notifier.
4528 * Do this even if the trigger unregistration failed to at least stop
4529 * the tracers from producing notifications associated with this
4532 if (must_update_event_notifiers
) {
4533 const enum lttng_domain_type trigger_domain
=
4534 lttng_trigger_get_underlying_domain_type_restriction(
4537 session_lock_list();
4538 switch (trigger_domain
) {
4539 case LTTNG_DOMAIN_KERNEL
:
4541 ret_code
= kernel_unregister_event_notifier(
4545 case LTTNG_DOMAIN_UST
:
4546 ust_app_global_update_all_event_notifier_rules();
4548 case LTTNG_DOMAIN_NONE
:
4552 /* Agent domains. */
4553 struct agent
*agt
= agent_find_by_event_notifier_domain(
4557 agt
= agent_create(trigger_domain
);
4559 ret_code
= LTTNG_ERR_NOMEM
;
4560 goto end_unlock_session_list
;
4562 agent_add(agt
, trigger_agents_ht_by_domain
);
4565 ret_code
= trigger_agent_disable(trigger
, agt
);
4566 if (ret_code
!= LTTNG_OK
) {
4567 goto end_unlock_session_list
;
4574 session_unlock_list();
4579 end_unlock_session_list
:
4580 session_unlock_list();
4583 int cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4584 struct notification_thread_handle
*notification_thread
,
4585 struct lttng_triggers
**return_triggers
)
4588 enum lttng_error_code ret_code
;
4589 struct lttng_triggers
*triggers
= NULL
;
4591 /* Get the set of triggers from the notification thread. */
4592 ret_code
= notification_thread_command_list_triggers(
4593 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4594 if (ret_code
!= LTTNG_OK
) {
4599 *return_triggers
= triggers
;
4603 lttng_triggers_destroy(triggers
);
4607 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4608 * snapshot output is *not* set with a remote destination.
4610 * Return LTTNG_OK on success or a LTTNG_ERR code.
4612 static enum lttng_error_code
set_relayd_for_snapshot(
4613 struct consumer_output
*output
,
4614 const struct ltt_session
*session
)
4616 enum lttng_error_code status
= LTTNG_OK
;
4617 struct lttng_ht_iter iter
;
4618 struct consumer_socket
*socket
;
4619 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4620 const char *base_path
;
4625 DBG2("Set relayd object from snapshot output");
4627 if (session
->current_trace_chunk
) {
4628 enum lttng_trace_chunk_status chunk_status
=
4629 lttng_trace_chunk_get_id(
4630 session
->current_trace_chunk
,
4631 ¤t_chunk_id
.value
);
4633 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4634 current_chunk_id
.is_set
= true;
4636 ERR("Failed to get current trace chunk id");
4637 status
= LTTNG_ERR_UNK
;
4642 /* Ignore if snapshot consumer output is not network. */
4643 if (output
->type
!= CONSUMER_DST_NET
) {
4648 * The snapshot record URI base path overrides the session
4651 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4652 base_path
= output
->dst
.net
.control
.subdir
;
4654 base_path
= session
->base_path
;
4658 * For each consumer socket, create and send the relayd object of the
4662 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4663 socket
, node
.node
) {
4664 pthread_mutex_lock(socket
->lock
);
4665 status
= send_consumer_relayd_sockets(0, session
->id
,
4667 session
->name
, session
->hostname
,
4669 session
->live_timer
,
4670 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4671 session
->creation_time
,
4672 session
->name_contains_creation_time
);
4673 pthread_mutex_unlock(socket
->lock
);
4674 if (status
!= LTTNG_OK
) {
4686 * Record a kernel snapshot.
4688 * Return LTTNG_OK on success or a LTTNG_ERR code.
4690 static enum lttng_error_code
record_kernel_snapshot(
4691 struct ltt_kernel_session
*ksess
,
4692 const struct consumer_output
*output
,
4693 const struct ltt_session
*session
,
4694 int wait
, uint64_t nb_packets_per_stream
)
4696 enum lttng_error_code status
;
4702 status
= kernel_snapshot_record(
4703 ksess
, output
, wait
, nb_packets_per_stream
);
4708 * Record a UST snapshot.
4710 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4712 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4713 const struct consumer_output
*output
,
4714 const struct ltt_session
*session
,
4715 int wait
, uint64_t nb_packets_per_stream
)
4717 enum lttng_error_code status
;
4723 status
= ust_app_snapshot_record(
4724 usess
, output
, wait
, nb_packets_per_stream
);
4729 uint64_t get_session_size_one_more_packet_per_stream(
4730 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4732 uint64_t tot_size
= 0;
4734 if (session
->kernel_session
) {
4735 struct ltt_kernel_channel
*chan
;
4736 const struct ltt_kernel_session
*ksess
=
4737 session
->kernel_session
;
4739 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4740 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4742 * Don't take channel into account if we
4743 * already grab all its packets.
4747 tot_size
+= chan
->channel
->attr
.subbuf_size
4748 * chan
->stream_count
;
4752 if (session
->ust_session
) {
4753 const struct ltt_ust_session
*usess
= session
->ust_session
;
4755 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4763 * Calculate the number of packets we can grab from each stream that
4764 * fits within the overall snapshot max size.
4766 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4767 * the number of packets per stream.
4769 * TODO: this approach is not perfect: we consider the worse case
4770 * (packet filling the sub-buffers) as an upper bound, but we could do
4771 * better if we do this calculation while we actually grab the packet
4772 * content: we would know how much padding we don't actually store into
4775 * This algorithm is currently bounded by the number of packets per
4778 * Since we call this algorithm before actually grabbing the data, it's
4779 * an approximation: for instance, applications could appear/disappear
4780 * in between this call and actually grabbing data.
4783 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4787 uint64_t cur_nb_packets
= 0;
4790 return 0; /* Infinite */
4793 size_left
= max_size
;
4795 uint64_t one_more_packet_tot_size
;
4797 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4798 session
, cur_nb_packets
);
4799 if (!one_more_packet_tot_size
) {
4800 /* We are already grabbing all packets. */
4803 size_left
-= one_more_packet_tot_size
;
4804 if (size_left
< 0) {
4809 if (!cur_nb_packets
&& size_left
!= max_size
) {
4810 /* Not enough room to grab one packet of each stream, error. */
4813 return cur_nb_packets
;
4817 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4818 const struct snapshot_output
*snapshot_output
, int wait
)
4820 int64_t nb_packets_per_stream
;
4821 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4823 enum lttng_error_code ret_code
= LTTNG_OK
;
4824 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4825 struct consumer_output
*original_ust_consumer_output
= NULL
;
4826 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4827 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4828 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4830 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4832 snapshot_output
->name
,
4833 snapshot_output
->datetime
,
4834 snapshot_output
->nb_snapshot
);
4835 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4836 ERR("Failed to format snapshot name");
4837 ret_code
= LTTNG_ERR_INVALID
;
4840 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4841 snapshot_output
->name
, session
->name
,
4842 snapshot_chunk_name
);
4843 if (!session
->kernel_session
&& !session
->ust_session
) {
4844 ERR("Failed to record snapshot as no channels exist");
4845 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4849 if (session
->kernel_session
) {
4850 original_kernel_consumer_output
=
4851 session
->kernel_session
->consumer
;
4852 snapshot_kernel_consumer_output
=
4853 consumer_copy_output(snapshot_output
->consumer
);
4854 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
4855 snapshot_chunk_name
);
4856 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4857 original_kernel_consumer_output
);
4859 ERR("Failed to copy consumer sockets from snapshot output configuration");
4860 ret_code
= LTTNG_ERR_NOMEM
;
4863 ret_code
= set_relayd_for_snapshot(
4864 snapshot_kernel_consumer_output
, session
);
4865 if (ret_code
!= LTTNG_OK
) {
4866 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4869 session
->kernel_session
->consumer
=
4870 snapshot_kernel_consumer_output
;
4872 if (session
->ust_session
) {
4873 original_ust_consumer_output
= session
->ust_session
->consumer
;
4874 snapshot_ust_consumer_output
=
4875 consumer_copy_output(snapshot_output
->consumer
);
4876 strcpy(snapshot_ust_consumer_output
->chunk_path
,
4877 snapshot_chunk_name
);
4878 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4879 original_ust_consumer_output
);
4881 ERR("Failed to copy consumer sockets from snapshot output configuration");
4882 ret_code
= LTTNG_ERR_NOMEM
;
4885 ret_code
= set_relayd_for_snapshot(
4886 snapshot_ust_consumer_output
, session
);
4887 if (ret_code
!= LTTNG_OK
) {
4888 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4891 session
->ust_session
->consumer
=
4892 snapshot_ust_consumer_output
;
4895 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4896 snapshot_kernel_consumer_output
?:
4897 snapshot_ust_consumer_output
,
4898 consumer_output_get_base_path(
4899 snapshot_output
->consumer
),
4900 snapshot_chunk_name
);
4901 if (!snapshot_trace_chunk
) {
4902 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4904 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4907 assert(!session
->current_trace_chunk
);
4908 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4909 lttng_trace_chunk_put(snapshot_trace_chunk
);
4910 snapshot_trace_chunk
= NULL
;
4912 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4914 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4918 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4919 snapshot_output
->max_size
);
4920 if (nb_packets_per_stream
< 0) {
4921 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4922 goto error_close_trace_chunk
;
4925 if (session
->kernel_session
) {
4926 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4927 snapshot_kernel_consumer_output
, session
,
4928 wait
, nb_packets_per_stream
);
4929 if (ret_code
!= LTTNG_OK
) {
4930 goto error_close_trace_chunk
;
4934 if (session
->ust_session
) {
4935 ret_code
= record_ust_snapshot(session
->ust_session
,
4936 snapshot_ust_consumer_output
, session
,
4937 wait
, nb_packets_per_stream
);
4938 if (ret_code
!= LTTNG_OK
) {
4939 goto error_close_trace_chunk
;
4943 error_close_trace_chunk
:
4944 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
4945 ERR("Failed to release the current trace chunk of session \"%s\"",
4947 ret_code
= LTTNG_ERR_UNK
;
4950 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
4951 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
4953 * Don't goto end; make sure the chunk is closed for the session
4954 * to allow future snapshots.
4956 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4958 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4961 if (original_ust_consumer_output
) {
4962 session
->ust_session
->consumer
= original_ust_consumer_output
;
4964 if (original_kernel_consumer_output
) {
4965 session
->kernel_session
->consumer
=
4966 original_kernel_consumer_output
;
4968 consumer_output_put(snapshot_ust_consumer_output
);
4969 consumer_output_put(snapshot_kernel_consumer_output
);
4974 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4976 * The wait parameter is ignored so this call always wait for the snapshot to
4977 * complete before returning.
4979 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4981 int cmd_snapshot_record(struct ltt_session
*session
,
4982 const struct lttng_snapshot_output
*output
, int wait
)
4984 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4986 unsigned int snapshot_success
= 0;
4988 struct snapshot_output
*tmp_output
= NULL
;
4993 DBG("Cmd snapshot record for session %s", session
->name
);
4995 /* Get the datetime for the snapshot output directory. */
4996 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4999 cmd_ret
= LTTNG_ERR_INVALID
;
5004 * Permission denied to create an output if the session is not
5005 * set in no output mode.
5007 if (session
->output_traces
) {
5008 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5012 /* The session needs to be started at least once. */
5013 if (!session
->has_been_started
) {
5014 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5018 /* Use temporary output for the session. */
5019 if (*output
->ctrl_url
!= '\0') {
5020 tmp_output
= snapshot_output_alloc();
5022 cmd_ret
= LTTNG_ERR_NOMEM
;
5026 ret
= snapshot_output_init(session
, output
->max_size
,
5028 output
->ctrl_url
, output
->data_url
,
5032 if (ret
== -ENOMEM
) {
5033 cmd_ret
= LTTNG_ERR_NOMEM
;
5035 cmd_ret
= LTTNG_ERR_INVALID
;
5039 /* Use the global session count for the temporary snapshot. */
5040 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5042 /* Use the global datetime */
5043 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5044 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
5045 if (cmd_ret
!= LTTNG_OK
) {
5048 snapshot_success
= 1;
5050 struct snapshot_output
*sout
;
5051 struct lttng_ht_iter iter
;
5054 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5055 &iter
.iter
, sout
, node
.node
) {
5056 struct snapshot_output output_copy
;
5059 * Make a local copy of the output and override output
5060 * parameters with those provided as part of the
5063 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5065 if (output
->max_size
!= (uint64_t) -1ULL) {
5066 output_copy
.max_size
= output
->max_size
;
5069 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5070 memcpy(output_copy
.datetime
, datetime
,
5073 /* Use temporary name. */
5074 if (*output
->name
!= '\0') {
5075 if (lttng_strncpy(output_copy
.name
,
5077 sizeof(output_copy
.name
))) {
5078 cmd_ret
= LTTNG_ERR_INVALID
;
5084 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
5085 if (cmd_ret
!= LTTNG_OK
) {
5089 snapshot_success
= 1;
5094 if (snapshot_success
) {
5095 session
->snapshot
.nb_snapshot
++;
5097 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5102 snapshot_output_destroy(tmp_output
);
5108 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5110 int cmd_set_session_shm_path(struct ltt_session
*session
,
5111 const char *shm_path
)
5117 * Can only set shm path before session is started.
5119 if (session
->has_been_started
) {
5120 return LTTNG_ERR_SESSION_STARTED
;
5123 strncpy(session
->shm_path
, shm_path
,
5124 sizeof(session
->shm_path
));
5125 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5131 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5133 * Ask the consumer to rotate the session output directory.
5134 * The session lock must be held.
5136 * Returns LTTNG_OK on success or else a negative LTTng error code.
5138 int cmd_rotate_session(struct ltt_session
*session
,
5139 struct lttng_rotate_session_return
*rotate_return
,
5140 bool quiet_rotation
,
5141 enum lttng_trace_chunk_command_type command
)
5144 uint64_t ongoing_rotation_chunk_id
;
5145 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5146 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5147 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5148 enum lttng_trace_chunk_status chunk_status
;
5149 bool failed_to_rotate
= false;
5150 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5154 if (!session
->has_been_started
) {
5155 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5160 * Explicit rotation is not supported for live sessions.
5161 * However, live sessions can perform a quiet rotation on
5163 * Rotation is not supported for snapshot traces (no output).
5165 if ((!quiet_rotation
&& session
->live_timer
) ||
5166 !session
->output_traces
) {
5167 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5171 /* Unsupported feature in lttng-relayd before 2.11. */
5172 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5173 (session
->consumer
->relay_major_version
== 2 &&
5174 session
->consumer
->relay_minor_version
< 11)) {
5175 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5179 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5180 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5181 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5185 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5186 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5188 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5193 * After a stop, we only allow one rotation to occur, the other ones are
5194 * useless until a new start.
5196 if (session
->rotated_after_last_stop
) {
5197 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5199 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5204 * After a stop followed by a clear, disallow following rotations a they would
5205 * generate empty chunks.
5207 if (session
->cleared_after_last_stop
) {
5208 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5210 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5214 if (session
->active
) {
5215 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5217 if (!new_trace_chunk
) {
5218 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5224 * The current trace chunk becomes the chunk being archived.
5226 * After this point, "chunk_being_archived" must absolutely
5227 * be closed on the consumer(s), otherwise it will never be
5228 * cleaned-up, which will result in a leak.
5230 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5231 &chunk_being_archived
);
5233 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5237 if (session
->kernel_session
) {
5238 cmd_ret
= kernel_rotate_session(session
);
5239 if (cmd_ret
!= LTTNG_OK
) {
5240 failed_to_rotate
= true;
5241 rotation_fail_code
= cmd_ret
;
5244 if (session
->ust_session
) {
5245 cmd_ret
= ust_app_rotate_session(session
);
5246 if (cmd_ret
!= LTTNG_OK
) {
5247 failed_to_rotate
= true;
5248 rotation_fail_code
= cmd_ret
;
5252 if (!session
->active
) {
5253 session
->rotated_after_last_stop
= true;
5256 if (!chunk_being_archived
) {
5257 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5259 if (failed_to_rotate
) {
5260 cmd_ret
= rotation_fail_code
;
5267 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5268 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5269 &ongoing_rotation_chunk_id
);
5270 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5272 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5273 command
, session
->last_chunk_path
);
5275 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5279 if (failed_to_rotate
) {
5280 cmd_ret
= rotation_fail_code
;
5284 session
->quiet_rotation
= quiet_rotation
;
5285 ret
= timer_session_rotation_pending_check_start(session
,
5286 DEFAULT_ROTATE_PENDING_TIMER
);
5288 cmd_ret
= LTTNG_ERR_UNK
;
5292 if (rotate_return
) {
5293 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5296 session
->chunk_being_archived
= chunk_being_archived
;
5297 chunk_being_archived
= NULL
;
5298 if (!quiet_rotation
) {
5299 ret
= notification_thread_command_session_rotation_ongoing(
5300 notification_thread_handle
,
5301 session
->name
, session
->uid
, session
->gid
,
5302 ongoing_rotation_chunk_id
);
5303 if (ret
!= LTTNG_OK
) {
5304 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5310 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5311 session
->name
, ongoing_rotation_chunk_id
);
5313 lttng_trace_chunk_put(new_trace_chunk
);
5314 lttng_trace_chunk_put(chunk_being_archived
);
5315 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5318 if (session_reset_rotation_state(session
,
5319 LTTNG_ROTATION_STATE_ERROR
)) {
5320 ERR("Failed to reset rotation state of session \"%s\"",
5327 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5329 * Check if the session has finished its rotation.
5331 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5333 int cmd_rotate_get_info(struct ltt_session
*session
,
5334 struct lttng_rotation_get_info_return
*info_return
,
5335 uint64_t rotation_id
)
5337 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5338 enum lttng_rotation_state rotation_state
;
5340 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5341 session
->most_recent_chunk_id
.value
);
5343 if (session
->chunk_being_archived
) {
5344 enum lttng_trace_chunk_status chunk_status
;
5347 chunk_status
= lttng_trace_chunk_get_id(
5348 session
->chunk_being_archived
,
5350 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5352 rotation_state
= rotation_id
== chunk_id
?
5353 LTTNG_ROTATION_STATE_ONGOING
:
5354 LTTNG_ROTATION_STATE_EXPIRED
;
5356 if (session
->last_archived_chunk_id
.is_set
&&
5357 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5358 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5360 rotation_state
= session
->rotation_state
;
5364 switch (rotation_state
) {
5365 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5366 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5369 case LTTNG_ROTATION_STATE_EXPIRED
:
5370 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5371 rotation_id
, session
->name
);
5373 case LTTNG_ROTATION_STATE_ONGOING
:
5374 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5375 rotation_id
, session
->name
);
5377 case LTTNG_ROTATION_STATE_COMPLETED
:
5381 char *current_tracing_path_reply
;
5382 size_t current_tracing_path_reply_len
;
5384 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5385 rotation_id
, session
->name
);
5387 switch (session_get_consumer_destination_type(session
)) {
5388 case CONSUMER_DST_LOCAL
:
5389 current_tracing_path_reply
=
5390 info_return
->location
.local
.absolute_path
;
5391 current_tracing_path_reply_len
=
5392 sizeof(info_return
->location
.local
.absolute_path
);
5393 info_return
->location_type
=
5394 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5395 fmt_ret
= asprintf(&chunk_path
,
5396 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5397 session_get_base_path(session
),
5398 session
->last_archived_chunk_name
);
5399 if (fmt_ret
== -1) {
5400 PERROR("Failed to format the path of the last archived trace chunk");
5401 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5402 cmd_ret
= LTTNG_ERR_UNK
;
5406 case CONSUMER_DST_NET
:
5408 uint16_t ctrl_port
, data_port
;
5410 current_tracing_path_reply
=
5411 info_return
->location
.relay
.relative_path
;
5412 current_tracing_path_reply_len
=
5413 sizeof(info_return
->location
.relay
.relative_path
);
5414 /* Currently the only supported relay protocol. */
5415 info_return
->location
.relay
.protocol
=
5416 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5418 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5419 session_get_net_consumer_hostname(session
),
5420 sizeof(info_return
->location
.relay
.host
));
5422 ERR("Failed to copy host name to rotate_get_info reply");
5423 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5424 cmd_ret
= LTTNG_ERR_SET_URL
;
5428 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5429 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5430 info_return
->location
.relay
.ports
.data
= data_port
;
5431 info_return
->location_type
=
5432 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5433 chunk_path
= strdup(session
->last_chunk_path
);
5435 ERR("Failed to allocate the path of the last archived trace chunk");
5436 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5437 cmd_ret
= LTTNG_ERR_UNK
;
5446 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5447 chunk_path
, current_tracing_path_reply_len
);
5450 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5451 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5452 cmd_ret
= LTTNG_ERR_UNK
;
5458 case LTTNG_ROTATION_STATE_ERROR
:
5459 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5460 rotation_id
, session
->name
);
5468 info_return
->status
= (int32_t) rotation_state
;
5473 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5475 * Configure the automatic rotation parameters.
5476 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5477 * 'activate' to false means deactivate the rotation schedule and validate that
5478 * 'new_value' has the same value as the currently active value.
5480 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5482 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5483 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5485 struct notification_thread_handle
*notification_thread_handle
)
5488 uint64_t *parameter_value
;
5492 DBG("Cmd rotate set schedule session %s", session
->name
);
5494 if (session
->live_timer
|| !session
->output_traces
) {
5495 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5496 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5500 switch (schedule_type
) {
5501 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5502 parameter_value
= &session
->rotate_size
;
5504 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5505 parameter_value
= &session
->rotate_timer_period
;
5506 if (new_value
>= UINT_MAX
) {
5507 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5508 new_value
, UINT_MAX
);
5509 ret
= LTTNG_ERR_INVALID
;
5514 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5515 ret
= LTTNG_ERR_INVALID
;
5519 /* Improper use of the API. */
5520 if (new_value
== -1ULL) {
5521 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5522 ret
= LTTNG_ERR_INVALID
;
5527 * As indicated in struct ltt_session's comments, a value of == 0 means
5528 * this schedule rotation type is not in use.
5530 * Reject the command if we were asked to activate a schedule that was
5533 if (activate
&& *parameter_value
!= 0) {
5534 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5535 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5540 * Reject the command if we were asked to deactivate a schedule that was
5543 if (!activate
&& *parameter_value
== 0) {
5544 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5545 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5550 * Reject the command if we were asked to deactivate a schedule that
5553 if (!activate
&& *parameter_value
!= new_value
) {
5554 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5555 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5559 *parameter_value
= activate
? new_value
: 0;
5561 switch (schedule_type
) {
5562 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5563 if (activate
&& session
->active
) {
5565 * Only start the timer if the session is active,
5566 * otherwise it will be started when the session starts.
5568 ret
= timer_session_rotation_schedule_timer_start(
5569 session
, new_value
);
5571 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5572 ret
= LTTNG_ERR_UNK
;
5576 ret
= timer_session_rotation_schedule_timer_stop(
5579 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5580 ret
= LTTNG_ERR_UNK
;
5585 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5587 ret
= subscribe_session_consumed_size_rotation(session
,
5588 new_value
, notification_thread_handle
);
5590 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5591 ret
= LTTNG_ERR_UNK
;
5595 ret
= unsubscribe_session_consumed_size_rotation(session
,
5596 notification_thread_handle
);
5598 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5599 ret
= LTTNG_ERR_UNK
;
5606 /* Would have been caught before. */
5618 /* Wait for a given path to be removed before continuing. */
5619 static enum lttng_error_code
wait_on_path(void *path_data
)
5621 const char *shm_path
= path_data
;
5623 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5629 ret
= stat(shm_path
, &st
);
5631 if (errno
!= ENOENT
) {
5632 PERROR("stat() returned an error while checking for the existence of the shm path");
5634 DBG("shm path no longer exists, completing the destruction of session");
5638 if (!S_ISDIR(st
.st_mode
)) {
5639 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5644 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5650 * Returns a pointer to a handler to run on completion of a command.
5651 * Returns NULL if no handler has to be run for the last command executed.
5653 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5655 struct cmd_completion_handler
*handler
= current_completion_handler
;
5657 current_completion_handler
= NULL
;
5662 * Init command subsystem.
5667 * Set network sequence index to 1 for streams to match a relayd
5668 * socket on the consumer side.
5670 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5671 relayd_net_seq_idx
= 1;
5672 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5674 DBG("Command subsystem initialized");