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
9 #include "bin/lttng-sessiond/session.h"
13 #include <urcu/list.h>
14 #include <urcu/uatomic.h>
18 #include <common/defaults.h>
19 #include <common/common.h>
20 #include <common/sessiond-comm/sessiond-comm.h>
21 #include <common/relayd/relayd.h>
22 #include <common/utils.h>
23 #include <common/compat/string.h>
24 #include <common/kernel-ctl/kernel-ctl.h>
25 #include <common/dynamic-buffer.h>
26 #include <common/buffer-view.h>
27 #include <common/payload.h>
28 #include <common/payload-view.h>
29 #include <common/trace-chunk.h>
30 #include <lttng/location-internal.h>
31 #include <lttng/trigger/trigger-internal.h>
32 #include <lttng/condition/condition.h>
33 #include <lttng/condition/condition-internal.h>
34 #include <lttng/condition/on-event.h>
35 #include <lttng/condition/on-event-internal.h>
36 #include <lttng/event-rule/event-rule.h>
37 #include <lttng/event-rule/event-rule-internal.h>
38 #include <lttng/action/action.h>
39 #include <lttng/channel.h>
40 #include <lttng/channel-internal.h>
41 #include <lttng/rotate-internal.h>
42 #include <lttng/location-internal.h>
43 #include <lttng/session-internal.h>
44 #include <lttng/userspace-probe-internal.h>
45 #include <lttng/session-descriptor-internal.h>
46 #include <lttng/lttng-error.h>
47 #include <lttng/tracker.h>
48 #include <common/string-utils/string-utils.h>
53 #include "health-sessiond.h"
55 #include "kernel-consumer.h"
56 #include "lttng-sessiond.h"
58 #include "lttng-syscall.h"
60 #include "buffer-registry.h"
61 #include "notification-thread.h"
62 #include "notification-thread-commands.h"
64 #include "rotation-thread.h"
66 #include "agent-thread.h"
71 /* Sleep for 100ms between each check for the shm path's deletion. */
72 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
74 struct cmd_destroy_session_reply_context
{
76 bool implicit_rotation_on_destroy
;
78 * Indicates whether or not an error occurred while launching the
79 * destruction of a session.
81 enum lttng_error_code destruction_status
;
84 static enum lttng_error_code
wait_on_path(void *path
);
87 * Command completion handler that is used by the destroy command
88 * when a session that has a non-default shm_path is being destroyed.
90 * See comment in cmd_destroy_session() for the rationale.
92 static struct destroy_completion_handler
{
93 struct cmd_completion_handler handler
;
94 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
95 } destroy_completion_handler
= {
98 .data
= destroy_completion_handler
.shm_path
103 static struct cmd_completion_handler
*current_completion_handler
;
106 * Used to keep a unique index for each relayd socket created where this value
107 * is associated with streams on the consumer so it can match the right relayd
108 * to send to. It must be accessed with the relayd_net_seq_idx_lock
111 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
112 static uint64_t relayd_net_seq_idx
;
114 static int validate_ust_event_name(const char *);
115 static int cmd_enable_event_internal(struct ltt_session
*session
,
116 const struct lttng_domain
*domain
,
117 char *channel_name
, struct lttng_event
*event
,
118 char *filter_expression
,
119 struct lttng_bytecode
*filter
,
120 struct lttng_event_exclusion
*exclusion
,
124 * Create a session path used by list_lttng_sessions for the case that the
125 * session consumer is on the network.
127 static int build_network_session_path(char *dst
, size_t size
,
128 struct ltt_session
*session
)
130 int ret
, kdata_port
, udata_port
;
131 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
132 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
137 memset(tmp_urls
, 0, sizeof(tmp_urls
));
138 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
140 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
142 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
143 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
144 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
147 if (session
->ust_session
&& session
->ust_session
->consumer
) {
148 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
149 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
152 if (uuri
== NULL
&& kuri
== NULL
) {
153 uri
= &session
->consumer
->dst
.net
.control
;
154 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
155 } else if (kuri
&& uuri
) {
156 ret
= uri_compare(kuri
, uuri
);
160 /* Build uuri URL string */
161 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
168 } else if (kuri
&& uuri
== NULL
) {
170 } else if (uuri
&& kuri
== NULL
) {
174 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
180 * Do we have a UST url set. If yes, this means we have both kernel and UST
183 if (*tmp_uurl
!= '\0') {
184 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
185 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
188 if (kuri
|| (!kuri
&& !uuri
)) {
191 /* No kernel URI, use the UST port. */
194 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
202 * Get run-time attributes if the session has been started (discarded events,
205 static int get_kernel_runtime_stats(struct ltt_session
*session
,
206 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
207 uint64_t *lost_packets
)
211 if (!session
->has_been_started
) {
213 *discarded_events
= 0;
218 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
219 session
->kernel_session
->consumer
,
225 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
226 session
->kernel_session
->consumer
,
237 * Get run-time attributes if the session has been started (discarded events,
240 static int get_ust_runtime_stats(struct ltt_session
*session
,
241 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
242 uint64_t *lost_packets
)
245 struct ltt_ust_session
*usess
;
247 if (!discarded_events
|| !lost_packets
) {
252 usess
= session
->ust_session
;
253 assert(discarded_events
);
254 assert(lost_packets
);
256 if (!usess
|| !session
->has_been_started
) {
257 *discarded_events
= 0;
263 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
264 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
265 &usess
->buffer_reg_uid_list
,
266 usess
->consumer
, uchan
->id
,
267 uchan
->attr
.overwrite
,
270 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
271 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
272 uchan
, usess
->consumer
,
273 uchan
->attr
.overwrite
,
279 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
280 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
282 ERR("Unsupported buffer type");
293 * Fill lttng_channel array of all channels.
295 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
296 struct ltt_session
*session
, struct lttng_channel
*channels
,
297 struct lttng_channel_extended
*chan_exts
)
300 struct ltt_kernel_channel
*kchan
;
302 DBG("Listing channels for session %s", session
->name
);
305 case LTTNG_DOMAIN_KERNEL
:
306 /* Kernel channels */
307 if (session
->kernel_session
!= NULL
) {
308 cds_list_for_each_entry(kchan
,
309 &session
->kernel_session
->channel_list
.head
, list
) {
310 uint64_t discarded_events
, lost_packets
;
311 struct lttng_channel_extended
*extended
;
313 extended
= (struct lttng_channel_extended
*)
314 kchan
->channel
->attr
.extended
.ptr
;
316 ret
= get_kernel_runtime_stats(session
, kchan
,
317 &discarded_events
, &lost_packets
);
321 /* Copy lttng_channel struct to array */
322 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
323 channels
[i
].enabled
= kchan
->enabled
;
324 chan_exts
[i
].discarded_events
=
326 chan_exts
[i
].lost_packets
= lost_packets
;
327 chan_exts
[i
].monitor_timer_interval
=
328 extended
->monitor_timer_interval
;
329 chan_exts
[i
].blocking_timeout
= 0;
334 case LTTNG_DOMAIN_UST
:
336 struct lttng_ht_iter iter
;
337 struct ltt_ust_channel
*uchan
;
340 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
341 &iter
.iter
, uchan
, node
.node
) {
342 uint64_t discarded_events
= 0, lost_packets
= 0;
344 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
345 LTTNG_SYMBOL_NAME_LEN
)) {
348 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
349 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
350 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
351 channels
[i
].attr
.switch_timer_interval
=
352 uchan
->attr
.switch_timer_interval
;
353 channels
[i
].attr
.read_timer_interval
=
354 uchan
->attr
.read_timer_interval
;
355 channels
[i
].enabled
= uchan
->enabled
;
356 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
357 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
360 * Map enum lttng_ust_output to enum lttng_event_output.
362 switch (uchan
->attr
.output
) {
363 case LTTNG_UST_ABI_MMAP
:
364 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
368 * LTTNG_UST_MMAP is the only supported UST
375 chan_exts
[i
].monitor_timer_interval
=
376 uchan
->monitor_timer_interval
;
377 chan_exts
[i
].blocking_timeout
=
378 uchan
->attr
.u
.s
.blocking_timeout
;
380 ret
= get_ust_runtime_stats(session
, uchan
,
381 &discarded_events
, &lost_packets
);
385 chan_exts
[i
].discarded_events
= discarded_events
;
386 chan_exts
[i
].lost_packets
= lost_packets
;
398 return -LTTNG_ERR_FATAL
;
404 static int append_extended_info(const char *filter_expression
,
405 struct lttng_event_exclusion
*exclusion
,
406 struct lttng_userspace_probe_location
*probe_location
,
407 struct lttng_payload
*payload
)
410 size_t filter_len
= 0;
411 size_t nb_exclusions
= 0;
412 size_t userspace_probe_location_len
= 0;
413 struct lttcomm_event_extended_header extended_header
= {};
414 struct lttcomm_event_extended_header
*p_extended_header
;
415 const size_t original_payload_size
= payload
->buffer
.size
;
417 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &extended_header
,
418 sizeof(extended_header
));
423 if (filter_expression
) {
424 filter_len
= strlen(filter_expression
) + 1;
425 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
426 filter_expression
, filter_len
);
433 const size_t len
= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
435 nb_exclusions
= exclusion
->count
;
437 ret
= lttng_dynamic_buffer_append(
438 &payload
->buffer
, &exclusion
->names
, len
);
444 if (probe_location
) {
445 const size_t size_before_probe
= payload
->buffer
.size
;
447 ret
= lttng_userspace_probe_location_serialize(probe_location
,
454 userspace_probe_location_len
=
455 payload
->buffer
.size
- size_before_probe
;
458 /* Set header fields */
459 p_extended_header
= (struct lttcomm_event_extended_header
*)
460 (payload
->buffer
.data
+ original_payload_size
);
462 p_extended_header
->filter_len
= filter_len
;
463 p_extended_header
->nb_exclusions
= nb_exclusions
;
464 p_extended_header
->userspace_probe_location_len
=
465 userspace_probe_location_len
;
473 * Create a list of agent domain events.
475 * Return number of events in list on success or else a negative value.
477 static int list_lttng_agent_events(struct agent
*agt
,
478 struct lttng_payload
*payload
)
480 int nb_events
= 0, ret
= 0;
481 const struct agent_event
*agent_event
;
482 struct lttng_ht_iter iter
;
486 DBG3("Listing agent events");
489 cds_lfht_for_each_entry (
490 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
491 struct lttng_event event
= {
492 .enabled
= AGENT_EVENT_IS_ENABLED(agent_event
),
493 .loglevel
= agent_event
->loglevel_value
,
494 .loglevel_type
= agent_event
->loglevel_type
,
497 ret
= lttng_strncpy(event
.name
, agent_event
->name
, sizeof(event
.name
));
499 /* Internal error, invalid name. */
500 ERR("Invalid event name while listing agent events: '%s' exceeds the maximal allowed length of %zu bytes",
501 agent_event
->name
, sizeof(event
.name
));
502 ret
= -LTTNG_ERR_UNK
;
506 ret
= lttng_dynamic_buffer_append(
507 &payload
->buffer
, &event
, sizeof(event
));
509 ERR("Failed to append event to payload");
510 ret
= -LTTNG_ERR_NOMEM
;
517 cds_lfht_for_each_entry (
518 agt
->events
->ht
, &iter
.iter
, agent_event
, node
.node
) {
519 /* Append extended info. */
520 ret
= append_extended_info(agent_event
->filter_expression
, NULL
,
523 ERR("Failed to append extended event info to payload");
524 ret
= -LTTNG_ERR_NOMEM
;
536 * Create a list of ust global domain events.
538 static int list_lttng_ust_global_events(char *channel_name
,
539 struct ltt_ust_domain_global
*ust_global
,
540 struct lttng_payload
*payload
)
543 unsigned int nb_events
= 0;
544 struct lttng_ht_iter iter
;
545 const struct lttng_ht_node_str
*node
;
546 const struct ltt_ust_channel
*uchan
;
547 const struct ltt_ust_event
*uevent
;
549 DBG("Listing UST global events for channel %s", channel_name
);
553 lttng_ht_lookup(ust_global
->channels
, (void *) channel_name
, &iter
);
554 node
= lttng_ht_iter_get_node_str(&iter
);
556 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
560 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
562 DBG3("Listing UST global events");
564 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
565 struct lttng_event event
= {};
567 if (uevent
->internal
) {
571 ret
= lttng_strncpy(event
.name
, uevent
->attr
.name
, sizeof(event
.name
));
573 /* Internal error, invalid name. */
574 ERR("Invalid event name while listing user space tracer events: '%s' exceeds the maximal allowed length of %zu bytes",
575 uevent
->attr
.name
, sizeof(event
.name
));
576 ret
= -LTTNG_ERR_UNK
;
580 event
.enabled
= uevent
->enabled
;
582 switch (uevent
->attr
.instrumentation
) {
583 case LTTNG_UST_ABI_TRACEPOINT
:
584 event
.type
= LTTNG_EVENT_TRACEPOINT
;
586 case LTTNG_UST_ABI_PROBE
:
587 event
.type
= LTTNG_EVENT_PROBE
;
589 case LTTNG_UST_ABI_FUNCTION
:
590 event
.type
= LTTNG_EVENT_FUNCTION
;
594 event
.loglevel
= uevent
->attr
.loglevel
;
595 switch (uevent
->attr
.loglevel_type
) {
596 case LTTNG_UST_ABI_LOGLEVEL_ALL
:
597 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
599 case LTTNG_UST_ABI_LOGLEVEL_RANGE
:
600 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
602 case LTTNG_UST_ABI_LOGLEVEL_SINGLE
:
603 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
607 if (uevent
->filter
) {
611 if (uevent
->exclusion
) {
615 ret
= lttng_dynamic_buffer_append(&payload
->buffer
, &event
, sizeof(event
));
617 ERR("Failed to append event to payload");
618 ret
= -LTTNG_ERR_NOMEM
;
625 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
626 /* Append extended info. */
627 ret
= append_extended_info(uevent
->filter_expression
,
628 uevent
->exclusion
, NULL
, payload
);
630 ERR("Failed to append extended event info to payload");
631 ret
= -LTTNG_ERR_FATAL
;
643 * Fill lttng_event array of all kernel events in the channel.
645 static int list_lttng_kernel_events(char *channel_name
,
646 struct ltt_kernel_session
*kernel_session
,
647 struct lttng_payload
*payload
)
650 unsigned int nb_event
;
651 const struct ltt_kernel_event
*kevent
;
652 const struct ltt_kernel_channel
*kchan
;
654 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
656 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
660 nb_event
= kchan
->event_count
;
662 DBG("Listing events for channel %s", kchan
->channel
->name
);
664 /* Kernel channels */
665 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
666 struct lttng_event event
= {};
668 ret
= lttng_strncpy(event
.name
, kevent
->event
->name
, sizeof(event
.name
));
670 /* Internal error, invalid name. */
671 ERR("Invalid event name while listing kernel events: '%s' exceeds the maximal allowed length of %zu bytes",
674 ret
= -LTTNG_ERR_UNK
;
678 event
.enabled
= kevent
->enabled
;
679 event
.filter
= (unsigned char) !!kevent
->filter_expression
;
681 switch (kevent
->event
->instrumentation
) {
682 case LTTNG_KERNEL_TRACEPOINT
:
683 event
.type
= LTTNG_EVENT_TRACEPOINT
;
685 case LTTNG_KERNEL_KRETPROBE
:
686 event
.type
= LTTNG_EVENT_FUNCTION
;
687 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
688 sizeof(struct lttng_kernel_kprobe
));
690 case LTTNG_KERNEL_KPROBE
:
691 event
.type
= LTTNG_EVENT_PROBE
;
692 memcpy(&event
.attr
.probe
, &kevent
->event
->u
.kprobe
,
693 sizeof(struct lttng_kernel_kprobe
));
695 case LTTNG_KERNEL_UPROBE
:
696 event
.type
= LTTNG_EVENT_USERSPACE_PROBE
;
698 case LTTNG_KERNEL_FUNCTION
:
699 event
.type
= LTTNG_EVENT_FUNCTION
;
700 memcpy(&event
.attr
.ftrace
, &kevent
->event
->u
.ftrace
,
701 sizeof(struct lttng_kernel_function
));
703 case LTTNG_KERNEL_NOOP
:
704 event
.type
= LTTNG_EVENT_NOOP
;
706 case LTTNG_KERNEL_SYSCALL
:
707 event
.type
= LTTNG_EVENT_SYSCALL
;
709 case LTTNG_KERNEL_ALL
:
716 ret
= lttng_dynamic_buffer_append(
717 &payload
->buffer
, &event
, sizeof(event
));
719 ERR("Failed to append event to payload");
720 ret
= -LTTNG_ERR_NOMEM
;
725 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
726 /* Append extended info. */
727 ret
= append_extended_info(kevent
->filter_expression
, NULL
,
728 kevent
->userspace_probe_location
, payload
);
730 DBG("Error appending extended info message");
731 ret
= -LTTNG_ERR_FATAL
;
743 * Add URI so the consumer output object. Set the correct path depending on the
744 * domain adding the default trace directory.
746 static enum lttng_error_code
add_uri_to_consumer(
747 const struct ltt_session
*session
,
748 struct consumer_output
*consumer
,
749 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
752 enum lttng_error_code ret_code
= LTTNG_OK
;
756 if (consumer
== NULL
) {
757 DBG("No consumer detected. Don't add URI. Stopping.");
758 ret_code
= LTTNG_ERR_NO_CONSUMER
;
763 case LTTNG_DOMAIN_KERNEL
:
764 ret
= lttng_strncpy(consumer
->domain_subdir
,
765 DEFAULT_KERNEL_TRACE_DIR
,
766 sizeof(consumer
->domain_subdir
));
768 case LTTNG_DOMAIN_UST
:
769 ret
= lttng_strncpy(consumer
->domain_subdir
,
770 DEFAULT_UST_TRACE_DIR
,
771 sizeof(consumer
->domain_subdir
));
775 * This case is possible is we try to add the URI to the global
776 * tracing session consumer object which in this case there is
779 memset(consumer
->domain_subdir
, 0,
780 sizeof(consumer
->domain_subdir
));
784 ERR("Failed to initialize consumer output domain subdirectory");
785 ret_code
= LTTNG_ERR_FATAL
;
789 switch (uri
->dtype
) {
792 DBG2("Setting network URI to consumer");
794 if (consumer
->type
== CONSUMER_DST_NET
) {
795 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
796 consumer
->dst
.net
.control_isset
) ||
797 (uri
->stype
== LTTNG_STREAM_DATA
&&
798 consumer
->dst
.net
.data_isset
)) {
799 ret_code
= LTTNG_ERR_URL_EXIST
;
803 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
806 /* Set URI into consumer output object */
807 ret
= consumer_set_network_uri(session
, consumer
, uri
);
811 } else if (ret
== 1) {
813 * URI was the same in the consumer so we do not append the subdir
814 * again so to not duplicate output dir.
821 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
822 ret_code
= LTTNG_ERR_INVALID
;
825 DBG2("Setting trace directory path from URI to %s",
827 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
829 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
831 sizeof(consumer
->dst
.session_root_path
));
833 ret_code
= LTTNG_ERR_FATAL
;
836 consumer
->type
= CONSUMER_DST_LOCAL
;
846 * Init tracing by creating trace directory and sending fds kernel consumer.
848 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
851 struct lttng_ht_iter iter
;
852 struct consumer_socket
*socket
;
858 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
859 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
861 pthread_mutex_lock(socket
->lock
);
862 ret
= kernel_consumer_send_session(socket
, session
);
863 pthread_mutex_unlock(socket
->lock
);
865 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
877 * Create a socket to the relayd using the URI.
879 * On success, the relayd_sock pointer is set to the created socket.
880 * Else, it remains untouched and an LTTng error code is returned.
882 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
883 struct lttcomm_relayd_sock
**relayd_sock
,
884 struct consumer_output
*consumer
)
887 enum lttng_error_code status
= LTTNG_OK
;
888 struct lttcomm_relayd_sock
*rsock
;
890 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
891 RELAYD_VERSION_COMM_MINOR
);
893 status
= LTTNG_ERR_FATAL
;
898 * Connect to relayd so we can proceed with a session creation. This call
899 * can possibly block for an arbitrary amount of time to set the health
900 * state to be in poll execution.
903 ret
= relayd_connect(rsock
);
906 ERR("Unable to reach lttng-relayd");
907 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
911 /* Create socket for control stream. */
912 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
913 uint64_t result_flags
;
915 DBG3("Creating relayd stream socket from URI");
917 /* Check relayd version */
918 ret
= relayd_version_check(rsock
);
919 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
920 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
922 } else if (ret
< 0) {
923 ERR("Unable to reach lttng-relayd");
924 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
927 consumer
->relay_major_version
= rsock
->major
;
928 consumer
->relay_minor_version
= rsock
->minor
;
929 ret
= relayd_get_configuration(rsock
, 0,
932 ERR("Unable to get relayd configuration");
933 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
936 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
937 consumer
->relay_allows_clear
= true;
939 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
940 DBG3("Creating relayd data socket from URI");
942 /* Command is not valid */
943 ERR("Relayd invalid stream type: %d", uri
->stype
);
944 status
= LTTNG_ERR_INVALID
;
948 *relayd_sock
= rsock
;
953 /* The returned value is not useful since we are on an error path. */
954 (void) relayd_close(rsock
);
962 * Connect to the relayd using URI and send the socket to the right consumer.
964 * The consumer socket lock must be held by the caller.
966 * Returns LTTNG_OK on success or an LTTng error code on failure.
968 static enum lttng_error_code
send_consumer_relayd_socket(
969 unsigned int session_id
,
970 struct lttng_uri
*relayd_uri
,
971 struct consumer_output
*consumer
,
972 struct consumer_socket
*consumer_sock
,
973 const char *session_name
, const char *hostname
,
974 const char *base_path
, int session_live_timer
,
975 const uint64_t *current_chunk_id
,
976 time_t session_creation_time
,
977 bool session_name_contains_creation_time
)
980 struct lttcomm_relayd_sock
*rsock
= NULL
;
981 enum lttng_error_code status
;
983 /* Connect to relayd and make version check if uri is the control. */
984 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
985 if (status
!= LTTNG_OK
) {
986 goto relayd_comm_error
;
990 /* Set the network sequence index if not set. */
991 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
992 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
994 * Increment net_seq_idx because we are about to transfer the
995 * new relayd socket to the consumer.
996 * Assign unique key so the consumer can match streams.
998 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
999 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1002 /* Send relayd socket to consumer. */
1003 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1004 relayd_uri
->stype
, session_id
,
1005 session_name
, hostname
, base_path
,
1006 session_live_timer
, current_chunk_id
,
1007 session_creation_time
, session_name_contains_creation_time
);
1009 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1013 /* Flag that the corresponding socket was sent. */
1014 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1015 consumer_sock
->control_sock_sent
= 1;
1016 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1017 consumer_sock
->data_sock_sent
= 1;
1021 * Close socket which was dup on the consumer side. The session daemon does
1022 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1026 if (status
!= LTTNG_OK
) {
1028 * The consumer output for this session should not be used anymore
1029 * since the relayd connection failed thus making any tracing or/and
1030 * streaming not usable.
1032 consumer
->enabled
= 0;
1034 (void) relayd_close(rsock
);
1042 * Send both relayd sockets to a specific consumer and domain. This is a
1043 * helper function to facilitate sending the information to the consumer for a
1046 * The consumer socket lock must be held by the caller.
1048 * Returns LTTNG_OK, or an LTTng error code on failure.
1050 static enum lttng_error_code
send_consumer_relayd_sockets(
1051 enum lttng_domain_type domain
,
1052 unsigned int session_id
, struct consumer_output
*consumer
,
1053 struct consumer_socket
*sock
, const char *session_name
,
1054 const char *hostname
, const char *base_path
, int session_live_timer
,
1055 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1056 bool session_name_contains_creation_time
)
1058 enum lttng_error_code status
= LTTNG_OK
;
1063 /* Sending control relayd socket. */
1064 if (!sock
->control_sock_sent
) {
1065 status
= send_consumer_relayd_socket(session_id
,
1066 &consumer
->dst
.net
.control
, consumer
, sock
,
1067 session_name
, hostname
, base_path
, session_live_timer
,
1068 current_chunk_id
, session_creation_time
,
1069 session_name_contains_creation_time
);
1070 if (status
!= LTTNG_OK
) {
1075 /* Sending data relayd socket. */
1076 if (!sock
->data_sock_sent
) {
1077 status
= send_consumer_relayd_socket(session_id
,
1078 &consumer
->dst
.net
.data
, consumer
, sock
,
1079 session_name
, hostname
, base_path
, session_live_timer
,
1080 current_chunk_id
, session_creation_time
,
1081 session_name_contains_creation_time
);
1082 if (status
!= LTTNG_OK
) {
1092 * Setup relayd connections for a tracing session. First creates the socket to
1093 * the relayd and send them to the right domain consumer. Consumer type MUST be
1096 int cmd_setup_relayd(struct ltt_session
*session
)
1099 struct ltt_ust_session
*usess
;
1100 struct ltt_kernel_session
*ksess
;
1101 struct consumer_socket
*socket
;
1102 struct lttng_ht_iter iter
;
1103 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1107 usess
= session
->ust_session
;
1108 ksess
= session
->kernel_session
;
1110 DBG("Setting relayd for session %s", session
->name
);
1113 if (session
->current_trace_chunk
) {
1114 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1115 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1117 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1118 current_chunk_id
.is_set
= true;
1120 ERR("Failed to get current trace chunk id");
1121 ret
= LTTNG_ERR_UNK
;
1126 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1127 && usess
->consumer
->enabled
) {
1128 /* For each consumer socket, send relayd sockets */
1129 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1130 socket
, node
.node
) {
1131 pthread_mutex_lock(socket
->lock
);
1132 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1133 usess
->consumer
, socket
,
1134 session
->name
, session
->hostname
,
1136 session
->live_timer
,
1137 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1138 session
->creation_time
,
1139 session
->name_contains_creation_time
);
1140 pthread_mutex_unlock(socket
->lock
);
1141 if (ret
!= LTTNG_OK
) {
1144 /* Session is now ready for network streaming. */
1145 session
->net_handle
= 1;
1147 session
->consumer
->relay_major_version
=
1148 usess
->consumer
->relay_major_version
;
1149 session
->consumer
->relay_minor_version
=
1150 usess
->consumer
->relay_minor_version
;
1151 session
->consumer
->relay_allows_clear
=
1152 usess
->consumer
->relay_allows_clear
;
1155 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1156 && ksess
->consumer
->enabled
) {
1157 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1158 socket
, node
.node
) {
1159 pthread_mutex_lock(socket
->lock
);
1160 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1161 ksess
->consumer
, socket
,
1162 session
->name
, session
->hostname
,
1164 session
->live_timer
,
1165 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1166 session
->creation_time
,
1167 session
->name_contains_creation_time
);
1168 pthread_mutex_unlock(socket
->lock
);
1169 if (ret
!= LTTNG_OK
) {
1172 /* Session is now ready for network streaming. */
1173 session
->net_handle
= 1;
1175 session
->consumer
->relay_major_version
=
1176 ksess
->consumer
->relay_major_version
;
1177 session
->consumer
->relay_minor_version
=
1178 ksess
->consumer
->relay_minor_version
;
1179 session
->consumer
->relay_allows_clear
=
1180 ksess
->consumer
->relay_allows_clear
;
1189 * Start a kernel session by opening all necessary streams.
1191 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1194 struct ltt_kernel_channel
*kchan
;
1196 /* Open kernel metadata */
1197 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1198 ret
= kernel_open_metadata(ksess
);
1200 ret
= LTTNG_ERR_KERN_META_FAIL
;
1205 /* Open kernel metadata stream */
1206 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1207 ret
= kernel_open_metadata_stream(ksess
);
1209 ERR("Kernel create metadata stream failed");
1210 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1215 /* For each channel */
1216 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1217 if (kchan
->stream_count
== 0) {
1218 ret
= kernel_open_channel_stream(kchan
);
1220 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1223 /* Update the stream global counter */
1224 ksess
->stream_count_global
+= ret
;
1228 /* Setup kernel consumer socket and send fds to it */
1229 ret
= init_kernel_tracing(ksess
);
1231 ret
= LTTNG_ERR_KERN_START_FAIL
;
1235 /* This start the kernel tracing */
1236 ret
= kernel_start_session(ksess
);
1238 ret
= LTTNG_ERR_KERN_START_FAIL
;
1242 /* Quiescent wait after starting trace */
1243 kernel_wait_quiescent();
1253 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1255 struct ltt_kernel_channel
*kchan
;
1256 bool error_occurred
= false;
1259 if (!ksess
|| !ksess
->active
) {
1262 DBG("Stopping kernel tracing");
1264 ret
= kernel_stop_session(ksess
);
1266 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1270 kernel_wait_quiescent();
1272 /* Flush metadata after stopping (if exists) */
1273 if (ksess
->metadata_stream_fd
>= 0) {
1274 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1276 ERR("Kernel metadata flush failed");
1277 error_occurred
= true;
1281 /* Flush all buffers after stopping */
1282 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1283 ret
= kernel_flush_buffer(kchan
);
1285 ERR("Kernel flush buffer error");
1286 error_occurred
= true;
1291 if (error_occurred
) {
1292 ret
= LTTNG_ERR_UNK
;
1301 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1303 int cmd_disable_channel(struct ltt_session
*session
,
1304 enum lttng_domain_type domain
, char *channel_name
)
1307 struct ltt_ust_session
*usess
;
1309 usess
= session
->ust_session
;
1314 case LTTNG_DOMAIN_KERNEL
:
1316 ret
= channel_kernel_disable(session
->kernel_session
,
1318 if (ret
!= LTTNG_OK
) {
1322 kernel_wait_quiescent();
1325 case LTTNG_DOMAIN_UST
:
1327 struct ltt_ust_channel
*uchan
;
1328 struct lttng_ht
*chan_ht
;
1330 chan_ht
= usess
->domain_global
.channels
;
1332 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1333 if (uchan
== NULL
) {
1334 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1338 ret
= channel_ust_disable(usess
, uchan
);
1339 if (ret
!= LTTNG_OK
) {
1345 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1357 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1359 * The wpipe arguments is used as a notifier for the kernel thread.
1361 int cmd_enable_channel(struct ltt_session
*session
,
1362 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1365 struct ltt_ust_session
*usess
= session
->ust_session
;
1366 struct lttng_ht
*chan_ht
;
1368 struct lttng_channel attr
;
1375 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1377 /* Validate channel name */
1378 if (attr
.name
[0] == '.' ||
1379 memchr(attr
.name
, '/', len
) != NULL
) {
1380 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1384 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1389 * Don't try to enable a channel if the session has been started at
1390 * some point in time before. The tracer does not allow it.
1392 if (session
->has_been_started
) {
1393 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1398 * If the session is a live session, remove the switch timer, the
1399 * live timer does the same thing but sends also synchronisation
1400 * beacons for inactive streams.
1402 if (session
->live_timer
> 0) {
1403 attr
.attr
.live_timer_interval
= session
->live_timer
;
1404 attr
.attr
.switch_timer_interval
= 0;
1407 /* Check for feature support */
1408 switch (domain
->type
) {
1409 case LTTNG_DOMAIN_KERNEL
:
1411 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1412 /* Sampling position of buffer is not supported */
1413 WARN("Kernel tracer does not support buffer monitoring. "
1414 "Setting the monitor interval timer to 0 "
1415 "(disabled) for channel '%s' of session '%s'",
1416 attr
.name
, session
->name
);
1417 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1421 case LTTNG_DOMAIN_UST
:
1423 case LTTNG_DOMAIN_JUL
:
1424 case LTTNG_DOMAIN_LOG4J
:
1425 case LTTNG_DOMAIN_PYTHON
:
1426 if (!agent_tracing_is_enabled()) {
1427 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1428 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1433 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1437 switch (domain
->type
) {
1438 case LTTNG_DOMAIN_KERNEL
:
1440 struct ltt_kernel_channel
*kchan
;
1442 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1443 session
->kernel_session
);
1444 if (kchan
== NULL
) {
1445 if (session
->snapshot
.nb_output
> 0 ||
1446 session
->snapshot_mode
) {
1447 /* Enforce mmap output for snapshot sessions. */
1448 attr
.attr
.output
= LTTNG_EVENT_MMAP
;
1450 ret
= channel_kernel_create(session
->kernel_session
, &attr
, wpipe
);
1451 if (attr
.name
[0] != '\0') {
1452 session
->kernel_session
->has_non_default_channel
= 1;
1455 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1458 if (ret
!= LTTNG_OK
) {
1462 kernel_wait_quiescent();
1465 case LTTNG_DOMAIN_UST
:
1466 case LTTNG_DOMAIN_JUL
:
1467 case LTTNG_DOMAIN_LOG4J
:
1468 case LTTNG_DOMAIN_PYTHON
:
1470 struct ltt_ust_channel
*uchan
;
1475 * Current agent implementation limitations force us to allow
1476 * only one channel at once in "agent" subdomains. Each
1477 * subdomain has a default channel name which must be strictly
1480 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1481 if (strncmp(attr
.name
, DEFAULT_JUL_CHANNEL_NAME
,
1482 LTTNG_SYMBOL_NAME_LEN
)) {
1483 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1486 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1487 if (strncmp(attr
.name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1488 LTTNG_SYMBOL_NAME_LEN
)) {
1489 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1492 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1493 if (strncmp(attr
.name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1494 LTTNG_SYMBOL_NAME_LEN
)) {
1495 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1500 chan_ht
= usess
->domain_global
.channels
;
1502 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1503 if (uchan
== NULL
) {
1504 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1505 if (attr
.name
[0] != '\0') {
1506 usess
->has_non_default_channel
= 1;
1509 ret
= channel_ust_enable(usess
, uchan
);
1514 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1518 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1519 session
->has_non_mmap_channel
= true;
1527 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1528 struct ltt_session
*session
,
1529 enum lttng_domain_type domain
,
1530 enum lttng_process_attr process_attr
,
1531 enum lttng_tracking_policy
*policy
)
1533 enum lttng_error_code ret_code
= LTTNG_OK
;
1534 const struct process_attr_tracker
*tracker
;
1537 case LTTNG_DOMAIN_KERNEL
:
1538 if (!session
->kernel_session
) {
1539 ret_code
= LTTNG_ERR_INVALID
;
1542 tracker
= kernel_get_process_attr_tracker(
1543 session
->kernel_session
, process_attr
);
1545 case LTTNG_DOMAIN_UST
:
1546 if (!session
->ust_session
) {
1547 ret_code
= LTTNG_ERR_INVALID
;
1550 tracker
= trace_ust_get_process_attr_tracker(
1551 session
->ust_session
, process_attr
);
1554 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1558 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1560 ret_code
= LTTNG_ERR_INVALID
;
1566 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1567 struct ltt_session
*session
,
1568 enum lttng_domain_type domain
,
1569 enum lttng_process_attr process_attr
,
1570 enum lttng_tracking_policy policy
)
1572 enum lttng_error_code ret_code
= LTTNG_OK
;
1575 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1576 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1577 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1580 ret_code
= LTTNG_ERR_INVALID
;
1585 case LTTNG_DOMAIN_KERNEL
:
1586 if (!session
->kernel_session
) {
1587 ret_code
= LTTNG_ERR_INVALID
;
1590 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1591 session
->kernel_session
, process_attr
, policy
);
1593 case LTTNG_DOMAIN_UST
:
1594 if (!session
->ust_session
) {
1595 ret_code
= LTTNG_ERR_INVALID
;
1598 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1599 session
->ust_session
, process_attr
, policy
);
1602 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1609 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1610 struct ltt_session
*session
,
1611 enum lttng_domain_type domain
,
1612 enum lttng_process_attr process_attr
,
1613 const struct process_attr_value
*value
)
1615 enum lttng_error_code ret_code
= LTTNG_OK
;
1618 case LTTNG_DOMAIN_KERNEL
:
1619 if (!session
->kernel_session
) {
1620 ret_code
= LTTNG_ERR_INVALID
;
1623 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1624 session
->kernel_session
, process_attr
, value
);
1626 case LTTNG_DOMAIN_UST
:
1627 if (!session
->ust_session
) {
1628 ret_code
= LTTNG_ERR_INVALID
;
1631 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1632 session
->ust_session
, process_attr
, value
);
1635 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1642 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1643 struct ltt_session
*session
,
1644 enum lttng_domain_type domain
,
1645 enum lttng_process_attr process_attr
,
1646 const struct process_attr_value
*value
)
1648 enum lttng_error_code ret_code
= LTTNG_OK
;
1651 case LTTNG_DOMAIN_KERNEL
:
1652 if (!session
->kernel_session
) {
1653 ret_code
= LTTNG_ERR_INVALID
;
1656 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1657 session
->kernel_session
, process_attr
, value
);
1659 case LTTNG_DOMAIN_UST
:
1660 if (!session
->ust_session
) {
1661 ret_code
= LTTNG_ERR_INVALID
;
1664 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1665 session
->ust_session
, process_attr
, value
);
1668 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1675 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1676 struct ltt_session
*session
,
1677 enum lttng_domain_type domain
,
1678 enum lttng_process_attr process_attr
,
1679 struct lttng_process_attr_values
**values
)
1681 enum lttng_error_code ret_code
= LTTNG_OK
;
1682 const struct process_attr_tracker
*tracker
;
1683 enum process_attr_tracker_status status
;
1686 case LTTNG_DOMAIN_KERNEL
:
1687 if (!session
->kernel_session
) {
1688 ret_code
= LTTNG_ERR_INVALID
;
1691 tracker
= kernel_get_process_attr_tracker(
1692 session
->kernel_session
, process_attr
);
1694 case LTTNG_DOMAIN_UST
:
1695 if (!session
->ust_session
) {
1696 ret_code
= LTTNG_ERR_INVALID
;
1699 tracker
= trace_ust_get_process_attr_tracker(
1700 session
->ust_session
, process_attr
);
1703 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1708 ret_code
= LTTNG_ERR_INVALID
;
1712 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1714 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1715 ret_code
= LTTNG_OK
;
1717 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1718 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1720 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1721 ret_code
= LTTNG_ERR_NOMEM
;
1724 ret_code
= LTTNG_ERR_UNK
;
1733 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1735 int cmd_disable_event(struct ltt_session
*session
,
1736 enum lttng_domain_type domain
, const char *channel_name
,
1737 const struct lttng_event
*event
)
1740 const char *event_name
;
1742 DBG("Disable event command for event \'%s\'", event
->name
);
1744 event_name
= event
->name
;
1746 /* Error out on unhandled search criteria */
1747 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1748 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1749 ret
= LTTNG_ERR_UNK
;
1756 case LTTNG_DOMAIN_KERNEL
:
1758 struct ltt_kernel_channel
*kchan
;
1759 struct ltt_kernel_session
*ksess
;
1761 ksess
= session
->kernel_session
;
1764 * If a non-default channel has been created in the
1765 * session, explicitely require that -c chan_name needs
1768 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1769 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1773 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1774 if (kchan
== NULL
) {
1775 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1779 switch (event
->type
) {
1780 case LTTNG_EVENT_ALL
:
1781 case LTTNG_EVENT_TRACEPOINT
:
1782 case LTTNG_EVENT_SYSCALL
:
1783 case LTTNG_EVENT_PROBE
:
1784 case LTTNG_EVENT_FUNCTION
:
1785 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1786 if (event_name
[0] == '\0') {
1787 ret
= event_kernel_disable_event(kchan
,
1790 ret
= event_kernel_disable_event(kchan
,
1791 event_name
, event
->type
);
1793 if (ret
!= LTTNG_OK
) {
1798 ret
= LTTNG_ERR_UNK
;
1802 kernel_wait_quiescent();
1805 case LTTNG_DOMAIN_UST
:
1807 struct ltt_ust_channel
*uchan
;
1808 struct ltt_ust_session
*usess
;
1810 usess
= session
->ust_session
;
1812 if (validate_ust_event_name(event_name
)) {
1813 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1818 * If a non-default channel has been created in the
1819 * session, explicitly require that -c chan_name needs
1822 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1823 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1827 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1829 if (uchan
== NULL
) {
1830 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1834 switch (event
->type
) {
1835 case LTTNG_EVENT_ALL
:
1837 * An empty event name means that everything
1838 * should be disabled.
1840 if (event
->name
[0] == '\0') {
1841 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1843 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1846 if (ret
!= LTTNG_OK
) {
1851 ret
= LTTNG_ERR_UNK
;
1855 DBG3("Disable UST event %s in channel %s completed", event_name
,
1859 case LTTNG_DOMAIN_LOG4J
:
1860 case LTTNG_DOMAIN_JUL
:
1861 case LTTNG_DOMAIN_PYTHON
:
1864 struct ltt_ust_session
*usess
= session
->ust_session
;
1868 switch (event
->type
) {
1869 case LTTNG_EVENT_ALL
:
1872 ret
= LTTNG_ERR_UNK
;
1876 agt
= trace_ust_find_agent(usess
, domain
);
1878 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1882 * An empty event name means that everything
1883 * should be disabled.
1885 if (event
->name
[0] == '\0') {
1886 ret
= event_agent_disable_all(usess
, agt
);
1888 ret
= event_agent_disable(usess
, agt
, event_name
);
1890 if (ret
!= LTTNG_OK
) {
1897 ret
= LTTNG_ERR_UND
;
1910 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1912 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1913 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1915 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1916 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1919 * Don't try to add a context if the session has been started at
1920 * some point in time before. The tracer does not allow it and would
1921 * result in a corrupted trace.
1923 if (session
->has_been_started
) {
1924 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1928 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1929 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1930 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1934 case LTTNG_DOMAIN_KERNEL
:
1935 assert(session
->kernel_session
);
1937 if (session
->kernel_session
->channel_count
== 0) {
1938 /* Create default channel */
1939 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1940 if (ret
!= LTTNG_OK
) {
1943 chan_kern_created
= 1;
1945 /* Add kernel context to kernel tracer */
1946 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1947 if (ret
!= LTTNG_OK
) {
1951 case LTTNG_DOMAIN_JUL
:
1952 case LTTNG_DOMAIN_LOG4J
:
1955 * Validate channel name.
1956 * If no channel name is given and the domain is JUL or LOG4J,
1957 * set it to the appropriate domain-specific channel name. If
1958 * a name is provided but does not match the expexted channel
1959 * name, return an error.
1961 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1962 strcmp(channel_name
,
1963 DEFAULT_JUL_CHANNEL_NAME
)) {
1964 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1966 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1967 strcmp(channel_name
,
1968 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1969 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1972 /* break is _not_ missing here. */
1974 case LTTNG_DOMAIN_UST
:
1976 struct ltt_ust_session
*usess
= session
->ust_session
;
1977 unsigned int chan_count
;
1981 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1982 if (chan_count
== 0) {
1983 struct lttng_channel
*attr
;
1984 /* Create default channel */
1985 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1987 ret
= LTTNG_ERR_FATAL
;
1991 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1992 if (ret
!= LTTNG_OK
) {
1996 channel_attr_destroy(attr
);
1997 chan_ust_created
= 1;
2000 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2001 free(app_ctx_provider_name
);
2003 app_ctx_name
= NULL
;
2004 app_ctx_provider_name
= NULL
;
2005 if (ret
!= LTTNG_OK
) {
2011 ret
= LTTNG_ERR_UND
;
2019 if (chan_kern_created
) {
2020 struct ltt_kernel_channel
*kchan
=
2021 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2022 session
->kernel_session
);
2023 /* Created previously, this should NOT fail. */
2025 kernel_destroy_channel(kchan
);
2028 if (chan_ust_created
) {
2029 struct ltt_ust_channel
*uchan
=
2030 trace_ust_find_channel_by_name(
2031 session
->ust_session
->domain_global
.channels
,
2032 DEFAULT_CHANNEL_NAME
);
2033 /* Created previously, this should NOT fail. */
2035 /* Remove from the channel list of the session. */
2036 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2038 trace_ust_destroy_channel(uchan
);
2041 free(app_ctx_provider_name
);
2046 static inline bool name_starts_with(const char *name
, const char *prefix
)
2048 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2050 return !strncmp(name
, prefix
, max_cmp_len
);
2053 /* Perform userspace-specific event name validation */
2054 static int validate_ust_event_name(const char *name
)
2064 * Check name against all internal UST event component namespaces used
2067 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2068 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2069 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2078 * Internal version of cmd_enable_event() with a supplemental
2079 * "internal_event" flag which is used to enable internal events which should
2080 * be hidden from clients. Such events are used in the agent implementation to
2081 * enable the events through which all "agent" events are funeled.
2083 static int _cmd_enable_event(struct ltt_session
*session
,
2084 const struct lttng_domain
*domain
,
2085 char *channel_name
, struct lttng_event
*event
,
2086 char *filter_expression
,
2087 struct lttng_bytecode
*filter
,
2088 struct lttng_event_exclusion
*exclusion
,
2089 int wpipe
, bool internal_event
)
2091 int ret
= 0, channel_created
= 0;
2092 struct lttng_channel
*attr
= NULL
;
2096 assert(channel_name
);
2098 /* If we have a filter, we must have its filter expression */
2099 assert(!(!!filter_expression
^ !!filter
));
2101 /* Normalize event name as a globbing pattern */
2102 strutils_normalize_star_glob_pattern(event
->name
);
2104 /* Normalize exclusion names as globbing patterns */
2108 for (i
= 0; i
< exclusion
->count
; i
++) {
2109 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2111 strutils_normalize_star_glob_pattern(name
);
2115 DBG("Enable event command for event \'%s\'", event
->name
);
2119 switch (domain
->type
) {
2120 case LTTNG_DOMAIN_KERNEL
:
2122 struct ltt_kernel_channel
*kchan
;
2125 * If a non-default channel has been created in the
2126 * session, explicitely require that -c chan_name needs
2129 if (session
->kernel_session
->has_non_default_channel
2130 && channel_name
[0] == '\0') {
2131 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2135 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2136 session
->kernel_session
);
2137 if (kchan
== NULL
) {
2138 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2139 LTTNG_BUFFER_GLOBAL
);
2141 ret
= LTTNG_ERR_FATAL
;
2144 if (lttng_strncpy(attr
->name
, channel_name
,
2145 sizeof(attr
->name
))) {
2146 ret
= LTTNG_ERR_INVALID
;
2150 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2151 if (ret
!= LTTNG_OK
) {
2154 channel_created
= 1;
2157 /* Get the newly created kernel channel pointer */
2158 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2159 session
->kernel_session
);
2160 if (kchan
== NULL
) {
2161 /* This sould not happen... */
2162 ret
= LTTNG_ERR_FATAL
;
2166 switch (event
->type
) {
2167 case LTTNG_EVENT_ALL
:
2169 char *filter_expression_a
= NULL
;
2170 struct lttng_bytecode
*filter_a
= NULL
;
2173 * We need to duplicate filter_expression and filter,
2174 * because ownership is passed to first enable
2177 if (filter_expression
) {
2178 filter_expression_a
= strdup(filter_expression
);
2179 if (!filter_expression_a
) {
2180 ret
= LTTNG_ERR_FATAL
;
2185 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2187 free(filter_expression_a
);
2188 ret
= LTTNG_ERR_FATAL
;
2191 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2193 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2194 ret
= event_kernel_enable_event(kchan
, event
,
2195 filter_expression
, filter
);
2196 /* We have passed ownership */
2197 filter_expression
= NULL
;
2199 if (ret
!= LTTNG_OK
) {
2200 if (channel_created
) {
2201 /* Let's not leak a useless channel. */
2202 kernel_destroy_channel(kchan
);
2204 free(filter_expression_a
);
2208 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2209 ret
= event_kernel_enable_event(kchan
, event
,
2210 filter_expression_a
, filter_a
);
2211 /* We have passed ownership */
2212 filter_expression_a
= NULL
;
2214 if (ret
!= LTTNG_OK
) {
2219 case LTTNG_EVENT_PROBE
:
2220 case LTTNG_EVENT_USERSPACE_PROBE
:
2221 case LTTNG_EVENT_FUNCTION
:
2222 case LTTNG_EVENT_FUNCTION_ENTRY
:
2223 case LTTNG_EVENT_TRACEPOINT
:
2224 ret
= event_kernel_enable_event(kchan
, event
,
2225 filter_expression
, filter
);
2226 /* We have passed ownership */
2227 filter_expression
= NULL
;
2229 if (ret
!= LTTNG_OK
) {
2230 if (channel_created
) {
2231 /* Let's not leak a useless channel. */
2232 kernel_destroy_channel(kchan
);
2237 case LTTNG_EVENT_SYSCALL
:
2238 ret
= event_kernel_enable_event(kchan
, event
,
2239 filter_expression
, filter
);
2240 /* We have passed ownership */
2241 filter_expression
= NULL
;
2243 if (ret
!= LTTNG_OK
) {
2248 ret
= LTTNG_ERR_UNK
;
2252 kernel_wait_quiescent();
2255 case LTTNG_DOMAIN_UST
:
2257 struct ltt_ust_channel
*uchan
;
2258 struct ltt_ust_session
*usess
= session
->ust_session
;
2263 * If a non-default channel has been created in the
2264 * session, explicitely require that -c chan_name needs
2267 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2268 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2272 /* Get channel from global UST domain */
2273 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2275 if (uchan
== NULL
) {
2276 /* Create default channel */
2277 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2278 usess
->buffer_type
);
2280 ret
= LTTNG_ERR_FATAL
;
2283 if (lttng_strncpy(attr
->name
, channel_name
,
2284 sizeof(attr
->name
))) {
2285 ret
= LTTNG_ERR_INVALID
;
2289 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2290 if (ret
!= LTTNG_OK
) {
2294 /* Get the newly created channel reference back */
2295 uchan
= trace_ust_find_channel_by_name(
2296 usess
->domain_global
.channels
, channel_name
);
2300 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2302 * Don't allow users to add UST events to channels which
2303 * are assigned to a userspace subdomain (JUL, Log4J,
2306 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2310 if (!internal_event
) {
2312 * Ensure the event name is not reserved for internal
2315 ret
= validate_ust_event_name(event
->name
);
2317 WARN("Userspace event name %s failed validation.",
2319 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2324 /* At this point, the session and channel exist on the tracer */
2325 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2326 filter_expression
, filter
, exclusion
,
2328 /* We have passed ownership */
2329 filter_expression
= NULL
;
2332 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2333 goto already_enabled
;
2334 } else if (ret
!= LTTNG_OK
) {
2339 case LTTNG_DOMAIN_LOG4J
:
2340 case LTTNG_DOMAIN_JUL
:
2341 case LTTNG_DOMAIN_PYTHON
:
2343 const char *default_event_name
, *default_chan_name
;
2345 struct lttng_event uevent
;
2346 struct lttng_domain tmp_dom
;
2347 struct ltt_ust_session
*usess
= session
->ust_session
;
2351 if (!agent_tracing_is_enabled()) {
2352 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2353 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2357 agt
= trace_ust_find_agent(usess
, domain
->type
);
2359 agt
= agent_create(domain
->type
);
2361 ret
= LTTNG_ERR_NOMEM
;
2364 agent_add(agt
, usess
->agents
);
2367 /* Create the default tracepoint. */
2368 memset(&uevent
, 0, sizeof(uevent
));
2369 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2370 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2371 default_event_name
= event_get_default_agent_ust_name(
2373 if (!default_event_name
) {
2374 ret
= LTTNG_ERR_FATAL
;
2377 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2378 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2381 * The domain type is changed because we are about to enable the
2382 * default channel and event for the JUL domain that are hardcoded.
2383 * This happens in the UST domain.
2385 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2386 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2388 switch (domain
->type
) {
2389 case LTTNG_DOMAIN_LOG4J
:
2390 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2392 case LTTNG_DOMAIN_JUL
:
2393 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2395 case LTTNG_DOMAIN_PYTHON
:
2396 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2399 /* The switch/case we are in makes this impossible */
2404 char *filter_expression_copy
= NULL
;
2405 struct lttng_bytecode
*filter_copy
= NULL
;
2408 const size_t filter_size
= sizeof(
2409 struct lttng_bytecode
)
2412 filter_copy
= zmalloc(filter_size
);
2414 ret
= LTTNG_ERR_NOMEM
;
2417 memcpy(filter_copy
, filter
, filter_size
);
2419 filter_expression_copy
=
2420 strdup(filter_expression
);
2421 if (!filter_expression
) {
2422 ret
= LTTNG_ERR_NOMEM
;
2425 if (!filter_expression_copy
|| !filter_copy
) {
2426 free(filter_expression_copy
);
2432 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2433 (char *) default_chan_name
,
2434 &uevent
, filter_expression_copy
,
2435 filter_copy
, NULL
, wpipe
);
2438 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2439 goto already_enabled
;
2440 } else if (ret
!= LTTNG_OK
) {
2444 /* The wild card * means that everything should be enabled. */
2445 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2446 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2449 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2453 filter_expression
= NULL
;
2454 if (ret
!= LTTNG_OK
) {
2461 ret
= LTTNG_ERR_UND
;
2469 free(filter_expression
);
2472 channel_attr_destroy(attr
);
2478 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2479 * We own filter, exclusion, and filter_expression.
2481 int cmd_enable_event(struct ltt_session
*session
,
2482 const struct lttng_domain
*domain
,
2483 char *channel_name
, struct lttng_event
*event
,
2484 char *filter_expression
,
2485 struct lttng_bytecode
*filter
,
2486 struct lttng_event_exclusion
*exclusion
,
2489 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2490 filter_expression
, filter
, exclusion
, wpipe
, false);
2494 * Enable an event which is internal to LTTng. An internal should
2495 * never be made visible to clients and are immune to checks such as
2498 static int cmd_enable_event_internal(struct ltt_session
*session
,
2499 const struct lttng_domain
*domain
,
2500 char *channel_name
, struct lttng_event
*event
,
2501 char *filter_expression
,
2502 struct lttng_bytecode
*filter
,
2503 struct lttng_event_exclusion
*exclusion
,
2506 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2507 filter_expression
, filter
, exclusion
, wpipe
, true);
2511 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2513 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2514 struct lttng_event
**events
)
2517 ssize_t nb_events
= 0;
2520 case LTTNG_DOMAIN_KERNEL
:
2521 nb_events
= kernel_list_events(events
);
2522 if (nb_events
< 0) {
2523 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2527 case LTTNG_DOMAIN_UST
:
2528 nb_events
= ust_app_list_events(events
);
2529 if (nb_events
< 0) {
2530 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2534 case LTTNG_DOMAIN_LOG4J
:
2535 case LTTNG_DOMAIN_JUL
:
2536 case LTTNG_DOMAIN_PYTHON
:
2537 nb_events
= agent_list_events(events
, domain
);
2538 if (nb_events
< 0) {
2539 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2544 ret
= LTTNG_ERR_UND
;
2551 /* Return negative value to differentiate return code */
2556 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2558 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2559 struct lttng_event_field
**fields
)
2562 ssize_t nb_fields
= 0;
2565 case LTTNG_DOMAIN_UST
:
2566 nb_fields
= ust_app_list_event_fields(fields
);
2567 if (nb_fields
< 0) {
2568 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2572 case LTTNG_DOMAIN_KERNEL
:
2573 default: /* fall-through */
2574 ret
= LTTNG_ERR_UND
;
2581 /* Return negative value to differentiate return code */
2585 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2587 return syscall_table_list(events
);
2591 * Command LTTNG_START_TRACE processed by the client thread.
2593 * Called with session mutex held.
2595 int cmd_start_trace(struct ltt_session
*session
)
2597 enum lttng_error_code ret
;
2598 unsigned long nb_chan
= 0;
2599 struct ltt_kernel_session
*ksession
;
2600 struct ltt_ust_session
*usess
;
2601 const bool session_rotated_after_last_stop
=
2602 session
->rotated_after_last_stop
;
2603 const bool session_cleared_after_last_stop
=
2604 session
->cleared_after_last_stop
;
2608 /* Ease our life a bit ;) */
2609 ksession
= session
->kernel_session
;
2610 usess
= session
->ust_session
;
2612 /* Is the session already started? */
2613 if (session
->active
) {
2614 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2615 /* Perform nothing */
2619 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2620 !session
->current_trace_chunk
) {
2622 * A rotation was launched while the session was stopped and
2623 * it has not been completed yet. It is not possible to start
2624 * the session since starting the session here would require a
2625 * rotation from "NULL" to a new trace chunk. That rotation
2626 * would overlap with the ongoing rotation, which is not
2629 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2631 ret
= LTTNG_ERR_ROTATION_PENDING
;
2636 * Starting a session without channel is useless since after that it's not
2637 * possible to enable channel thus inform the client.
2639 if (usess
&& usess
->domain_global
.channels
) {
2640 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2643 nb_chan
+= ksession
->channel_count
;
2646 ret
= LTTNG_ERR_NO_CHANNEL
;
2650 session
->active
= 1;
2651 session
->rotated_after_last_stop
= false;
2652 session
->cleared_after_last_stop
= false;
2653 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2654 if (!session
->has_been_started
) {
2655 struct lttng_trace_chunk
*trace_chunk
;
2657 DBG("Creating initial trace chunk of session \"%s\"",
2659 trace_chunk
= session_create_new_trace_chunk(
2660 session
, NULL
, NULL
, NULL
);
2662 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2665 assert(!session
->current_trace_chunk
);
2666 ret
= session_set_trace_chunk(session
, trace_chunk
,
2668 lttng_trace_chunk_put(trace_chunk
);
2670 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2674 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2677 * Rotate existing streams into the new chunk.
2678 * This is a "quiet" rotation has no client has
2679 * explicitly requested this operation.
2681 * There is also no need to wait for the rotation
2682 * to complete as it will happen immediately. No data
2683 * was produced as the session was stopped, so the
2684 * rotation should happen on reception of the command.
2686 ret
= cmd_rotate_session(session
, NULL
, true,
2687 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2688 if (ret
!= LTTNG_OK
) {
2694 /* Kernel tracing */
2695 if (ksession
!= NULL
) {
2696 DBG("Start kernel tracing session %s", session
->name
);
2697 ret
= start_kernel_session(ksession
);
2698 if (ret
!= LTTNG_OK
) {
2703 /* Flag session that trace should start automatically */
2705 int int_ret
= ust_app_start_trace_all(usess
);
2708 ret
= LTTNG_ERR_UST_START_FAIL
;
2714 * Open a packet in every stream of the session to ensure that viewers
2715 * can correctly identify the boundaries of the periods during which
2716 * tracing was active for this session.
2718 ret
= session_open_packets(session
);
2719 if (ret
!= LTTNG_OK
) {
2724 * Clear the flag that indicates that a rotation was done while the
2725 * session was stopped.
2727 session
->rotated_after_last_stop
= false;
2729 if (session
->rotate_timer_period
) {
2730 int int_ret
= timer_session_rotation_schedule_timer_start(
2731 session
, session
->rotate_timer_period
);
2734 ERR("Failed to enable rotate timer");
2735 ret
= LTTNG_ERR_UNK
;
2743 if (ret
== LTTNG_OK
) {
2744 /* Flag this after a successful start. */
2745 session
->has_been_started
|= 1;
2747 session
->active
= 0;
2748 /* Restore initial state on error. */
2749 session
->rotated_after_last_stop
=
2750 session_rotated_after_last_stop
;
2751 session
->cleared_after_last_stop
=
2752 session_cleared_after_last_stop
;
2759 * Command LTTNG_STOP_TRACE processed by the client thread.
2761 int cmd_stop_trace(struct ltt_session
*session
)
2764 struct ltt_kernel_session
*ksession
;
2765 struct ltt_ust_session
*usess
;
2769 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2771 ksession
= session
->kernel_session
;
2772 usess
= session
->ust_session
;
2774 /* Session is not active. Skip everythong and inform the client. */
2775 if (!session
->active
) {
2776 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2780 ret
= stop_kernel_session(ksession
);
2781 if (ret
!= LTTNG_OK
) {
2785 if (usess
&& usess
->active
) {
2786 ret
= ust_app_stop_trace_all(usess
);
2788 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2793 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2795 /* Flag inactive after a successful stop. */
2796 session
->active
= 0;
2804 * Set the base_path of the session only if subdir of a control uris is set.
2805 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2807 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2809 struct lttng_uri
*uris
)
2814 for (i
= 0; i
< nb_uri
; i
++) {
2815 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2816 uris
[i
].subdir
[0] == '\0') {
2817 /* Not interested in these URIs */
2821 if (session
->base_path
!= NULL
) {
2822 free(session
->base_path
);
2823 session
->base_path
= NULL
;
2826 /* Set session base_path */
2827 session
->base_path
= strdup(uris
[i
].subdir
);
2828 if (!session
->base_path
) {
2829 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2830 uris
[i
].subdir
, session
->name
);
2831 ret
= LTTNG_ERR_NOMEM
;
2834 DBG2("Setting base path \"%s\" for session \"%s\"",
2835 session
->base_path
, session
->name
);
2843 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2845 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2846 struct lttng_uri
*uris
)
2849 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2850 struct ltt_ust_session
*usess
= session
->ust_session
;
2856 /* Can't set consumer URI if the session is active. */
2857 if (session
->active
) {
2858 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2863 * Set the session base path if any. This is done inside
2864 * cmd_set_consumer_uri to preserve backward compatibility of the
2865 * previous session creation api vs the session descriptor api.
2867 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2868 if (ret
!= LTTNG_OK
) {
2872 /* Set the "global" consumer URIs */
2873 for (i
= 0; i
< nb_uri
; i
++) {
2874 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2876 if (ret
!= LTTNG_OK
) {
2881 /* Set UST session URIs */
2882 if (session
->ust_session
) {
2883 for (i
= 0; i
< nb_uri
; i
++) {
2884 ret
= add_uri_to_consumer(session
,
2885 session
->ust_session
->consumer
,
2886 &uris
[i
], LTTNG_DOMAIN_UST
);
2887 if (ret
!= LTTNG_OK
) {
2893 /* Set kernel session URIs */
2894 if (session
->kernel_session
) {
2895 for (i
= 0; i
< nb_uri
; i
++) {
2896 ret
= add_uri_to_consumer(session
,
2897 session
->kernel_session
->consumer
,
2898 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2899 if (ret
!= LTTNG_OK
) {
2906 * Make sure to set the session in output mode after we set URI since a
2907 * session can be created without URL (thus flagged in no output mode).
2909 session
->output_traces
= 1;
2911 ksess
->output_traces
= 1;
2915 usess
->output_traces
= 1;
2926 enum lttng_error_code
set_session_output_from_descriptor(
2927 struct ltt_session
*session
,
2928 const struct lttng_session_descriptor
*descriptor
)
2931 enum lttng_error_code ret_code
= LTTNG_OK
;
2932 enum lttng_session_descriptor_type session_type
=
2933 lttng_session_descriptor_get_type(descriptor
);
2934 enum lttng_session_descriptor_output_type output_type
=
2935 lttng_session_descriptor_get_output_type(descriptor
);
2936 struct lttng_uri uris
[2] = {};
2937 size_t uri_count
= 0;
2939 switch (output_type
) {
2940 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2942 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2943 lttng_session_descriptor_get_local_output_uri(descriptor
,
2947 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2948 lttng_session_descriptor_get_network_output_uris(descriptor
,
2949 &uris
[0], &uris
[1]);
2953 ret_code
= LTTNG_ERR_INVALID
;
2957 switch (session_type
) {
2958 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2960 struct snapshot_output
*new_output
= NULL
;
2962 new_output
= snapshot_output_alloc();
2964 ret_code
= LTTNG_ERR_NOMEM
;
2968 ret
= snapshot_output_init_with_uri(session
,
2969 DEFAULT_SNAPSHOT_MAX_SIZE
,
2970 NULL
, uris
, uri_count
, session
->consumer
,
2971 new_output
, &session
->snapshot
);
2973 ret_code
= (ret
== -ENOMEM
) ?
2974 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2975 snapshot_output_destroy(new_output
);
2978 snapshot_add_output(&session
->snapshot
, new_output
);
2981 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2982 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2984 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2988 ret_code
= LTTNG_ERR_INVALID
;
2996 enum lttng_error_code
cmd_create_session_from_descriptor(
2997 struct lttng_session_descriptor
*descriptor
,
2998 const lttng_sock_cred
*creds
,
2999 const char *home_path
)
3002 enum lttng_error_code ret_code
;
3003 const char *session_name
;
3004 struct ltt_session
*new_session
= NULL
;
3005 enum lttng_session_descriptor_status descriptor_status
;
3007 session_lock_list();
3009 if (*home_path
!= '/') {
3010 ERR("Home path provided by client is not absolute");
3011 ret_code
= LTTNG_ERR_INVALID
;
3016 descriptor_status
= lttng_session_descriptor_get_session_name(
3017 descriptor
, &session_name
);
3018 switch (descriptor_status
) {
3019 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3021 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3022 session_name
= NULL
;
3025 ret_code
= LTTNG_ERR_INVALID
;
3029 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3031 if (ret_code
!= LTTNG_OK
) {
3035 if (!session_name
) {
3036 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3039 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3044 if (!lttng_session_descriptor_is_output_destination_initialized(
3047 * Only include the session's creation time in the output
3048 * destination if the name of the session itself was
3049 * not auto-generated.
3051 ret_code
= lttng_session_descriptor_set_default_output(
3053 session_name
? &new_session
->creation_time
: NULL
,
3055 if (ret_code
!= LTTNG_OK
) {
3059 new_session
->has_user_specified_directory
=
3060 lttng_session_descriptor_has_output_directory(
3064 switch (lttng_session_descriptor_get_type(descriptor
)) {
3065 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3066 new_session
->snapshot_mode
= 1;
3068 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3069 new_session
->live_timer
=
3070 lttng_session_descriptor_live_get_timer_interval(
3077 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3078 if (ret_code
!= LTTNG_OK
) {
3081 new_session
->consumer
->enabled
= 1;
3082 ret_code
= LTTNG_OK
;
3084 /* Release reference provided by the session_create function. */
3085 session_put(new_session
);
3086 if (ret_code
!= LTTNG_OK
&& new_session
) {
3087 /* Release the global reference on error. */
3088 session_destroy(new_session
);
3090 session_unlock_list();
3094 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3095 struct lttng_session_descriptor
**return_descriptor
)
3098 size_t payload_size
;
3099 struct lttng_dynamic_buffer payload
;
3100 struct lttng_buffer_view home_dir_view
;
3101 struct lttng_buffer_view session_descriptor_view
;
3102 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3103 enum lttng_error_code ret_code
;
3105 lttng_dynamic_buffer_init(&payload
);
3106 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
>=
3108 ret_code
= LTTNG_ERR_INVALID
;
3111 if (cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
>
3112 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3113 ret_code
= LTTNG_ERR_INVALID
;
3117 payload_size
= cmd_ctx
->lsm
.u
.create_session
.home_dir_size
+
3118 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
;
3119 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3121 ret_code
= LTTNG_ERR_NOMEM
;
3125 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3127 ERR("Reception of session descriptor failed, aborting.");
3128 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3132 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3135 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
);
3136 if (cmd_ctx
->lsm
.u
.create_session
.home_dir_size
> 0 &&
3137 !lttng_buffer_view_is_valid(&home_dir_view
)) {
3138 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3139 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3143 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3145 cmd_ctx
->lsm
.u
.create_session
.home_dir_size
,
3146 cmd_ctx
->lsm
.u
.create_session
.session_descriptor_size
);
3147 if (!lttng_buffer_view_is_valid(&session_descriptor_view
)) {
3148 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3149 ret_code
= LTTNG_ERR_INVALID_PROTOCOL
;
3153 ret
= lttng_session_descriptor_create_from_buffer(
3154 &session_descriptor_view
, &session_descriptor
);
3156 ERR("Failed to create session descriptor from payload of \"create session\" command");
3157 ret_code
= LTTNG_ERR_INVALID
;
3162 * Sets the descriptor's auto-generated properties (name, output) if
3165 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3167 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3168 if (ret_code
!= LTTNG_OK
) {
3172 ret_code
= LTTNG_OK
;
3173 *return_descriptor
= session_descriptor
;
3174 session_descriptor
= NULL
;
3176 lttng_dynamic_buffer_reset(&payload
);
3177 lttng_session_descriptor_destroy(session_descriptor
);
3182 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3183 void *_reply_context
)
3187 const struct cmd_destroy_session_reply_context
*reply_context
=
3189 struct lttng_dynamic_buffer payload
;
3190 struct lttcomm_session_destroy_command_header cmd_header
;
3191 struct lttng_trace_archive_location
*location
= NULL
;
3192 struct lttcomm_lttng_msg llm
= {
3193 .cmd_type
= LTTNG_DESTROY_SESSION
,
3194 .ret_code
= reply_context
->destruction_status
,
3197 sizeof(struct lttcomm_session_destroy_command_header
),
3200 size_t payload_size_before_location
;
3202 lttng_dynamic_buffer_init(&payload
);
3204 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3206 ERR("Failed to append session destruction message");
3210 cmd_header
.rotation_state
=
3211 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3212 session
->rotation_state
:
3213 LTTNG_ROTATION_STATE_NO_ROTATION
);
3214 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3215 sizeof(cmd_header
));
3217 ERR("Failed to append session destruction command header");
3221 if (!reply_context
->implicit_rotation_on_destroy
) {
3222 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3226 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3227 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3232 location
= session_get_trace_archive_location(session
);
3234 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3239 payload_size_before_location
= payload
.size
;
3240 comm_ret
= lttng_trace_archive_location_serialize(location
,
3243 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3247 /* Update the message to indicate the location's length. */
3248 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3249 payload
.size
- payload_size_before_location
;
3251 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3252 payload
.data
, payload
.size
);
3253 if (comm_ret
!= (ssize_t
) payload
.size
) {
3254 ERR("Failed to send result of the destruction of session \"%s\" to client",
3258 ret
= close(reply_context
->reply_sock_fd
);
3260 PERROR("Failed to close client socket in deferred session destroy reply");
3262 lttng_dynamic_buffer_reset(&payload
);
3263 free(_reply_context
);
3267 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3269 * Called with session lock held.
3271 int cmd_destroy_session(struct ltt_session
*session
,
3272 struct notification_thread_handle
*notification_thread_handle
,
3276 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3277 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3280 reply_context
= zmalloc(sizeof(*reply_context
));
3281 if (!reply_context
) {
3282 ret
= LTTNG_ERR_NOMEM
;
3285 reply_context
->reply_sock_fd
= *sock_fd
;
3291 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3293 if (session
->active
) {
3294 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3296 ret
= cmd_stop_trace(session
);
3297 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3298 /* Carry on with the destruction of the session. */
3299 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3300 session
->name
, lttng_strerror(-ret
));
3301 destruction_last_error
= ret
;
3305 if (session
->rotation_schedule_timer_enabled
) {
3306 if (timer_session_rotation_schedule_timer_stop(
3308 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3310 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3314 if (session
->rotate_size
) {
3315 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3316 session
->rotate_size
= 0;
3319 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3321 * Perform a last rotation on destruction if rotations have
3322 * occurred during the session's lifetime.
3324 ret
= cmd_rotate_session(session
, NULL
, false,
3325 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3326 if (ret
!= LTTNG_OK
) {
3327 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3328 session
->name
, lttng_strerror(-ret
));
3329 destruction_last_error
= -ret
;
3331 if (reply_context
) {
3332 reply_context
->implicit_rotation_on_destroy
= true;
3334 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3336 * The user has not triggered a session rotation. However, to
3337 * ensure all data has been consumed, the session is rotated
3338 * to a 'null' trace chunk before it is destroyed.
3340 * This is a "quiet" rotation meaning that no notification is
3341 * emitted and no renaming of the current trace chunk takes
3344 ret
= cmd_rotate_session(session
, NULL
, true,
3345 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3347 * Rotation operations may not be supported by the kernel
3348 * tracer. Hence, do not consider this implicit rotation as
3349 * a session destruction error. The library has already stopped
3350 * the session and waited for pending data; there is nothing
3351 * left to do but complete the destruction of the session.
3353 if (ret
!= LTTNG_OK
&&
3354 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3355 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3356 session
->name
, lttng_strerror(ret
));
3357 destruction_last_error
= -ret
;
3361 if (session
->shm_path
[0]) {
3363 * When a session is created with an explicit shm_path,
3364 * the consumer daemon will create its shared memory files
3365 * at that location and will *not* unlink them. This is normal
3366 * as the intention of that feature is to make it possible
3367 * to retrieve the content of those files should a crash occur.
3369 * To ensure the content of those files can be used, the
3370 * sessiond daemon will replicate the content of the metadata
3371 * cache in a metadata file.
3373 * On clean-up, it is expected that the consumer daemon will
3374 * unlink the shared memory files and that the session daemon
3375 * will unlink the metadata file. Then, the session's directory
3376 * in the shm path can be removed.
3378 * Unfortunately, a flaw in the design of the sessiond's and
3379 * consumerd's tear down of channels makes it impossible to
3380 * determine when the sessiond _and_ the consumerd have both
3381 * destroyed their representation of a channel. For one, the
3382 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3383 * callbacks in both daemons.
3385 * However, it is also impossible for the sessiond to know when
3386 * the consumer daemon is done destroying its channel(s) since
3387 * it occurs as a reaction to the closing of the channel's file
3388 * descriptor. There is no resulting communication initiated
3389 * from the consumerd to the sessiond to confirm that the
3390 * operation is completed (and was successful).
3392 * Until this is all fixed, the session daemon checks for the
3393 * removal of the session's shm path which makes it possible
3394 * to safely advertise a session as having been destroyed.
3396 * Prior to this fix, it was not possible to reliably save
3397 * a session making use of the --shm-path option, destroy it,
3398 * and load it again. This is because the creation of the
3399 * session would fail upon seeing the session's shm path
3400 * already in existence.
3402 * Note that none of the error paths in the check for the
3403 * directory's existence return an error. This is normal
3404 * as there isn't much that can be done. The session will
3405 * be destroyed properly, except that we can't offer the
3406 * guarantee that the same session can be re-created.
3408 current_completion_handler
= &destroy_completion_handler
.handler
;
3409 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3411 sizeof(destroy_completion_handler
.shm_path
));
3416 * The session is destroyed. However, note that the command context
3417 * still holds a reference to the session, thus delaying its destruction
3418 * _at least_ up to the point when that reference is released.
3420 session_destroy(session
);
3421 if (reply_context
) {
3422 reply_context
->destruction_status
= destruction_last_error
;
3423 ret
= session_add_destroy_notifier(session
,
3424 cmd_destroy_session_reply
,
3425 (void *) reply_context
);
3427 ret
= LTTNG_ERR_FATAL
;
3439 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3441 int cmd_register_consumer(struct ltt_session
*session
,
3442 enum lttng_domain_type domain
, const char *sock_path
,
3443 struct consumer_data
*cdata
)
3446 struct consumer_socket
*socket
= NULL
;
3453 case LTTNG_DOMAIN_KERNEL
:
3455 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3459 /* Can't register a consumer if there is already one */
3460 if (ksess
->consumer_fds_sent
!= 0) {
3461 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3465 sock
= lttcomm_connect_unix_sock(sock_path
);
3467 ret
= LTTNG_ERR_CONNECT_FAIL
;
3470 cdata
->cmd_sock
= sock
;
3472 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3473 if (socket
== NULL
) {
3476 PERROR("close register consumer");
3478 cdata
->cmd_sock
= -1;
3479 ret
= LTTNG_ERR_FATAL
;
3483 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3484 if (socket
->lock
== NULL
) {
3485 PERROR("zmalloc pthread mutex");
3486 ret
= LTTNG_ERR_FATAL
;
3489 pthread_mutex_init(socket
->lock
, NULL
);
3490 socket
->registered
= 1;
3493 consumer_add_socket(socket
, ksess
->consumer
);
3496 pthread_mutex_lock(&cdata
->pid_mutex
);
3498 pthread_mutex_unlock(&cdata
->pid_mutex
);
3503 /* TODO: Userspace tracing */
3504 ret
= LTTNG_ERR_UND
;
3512 consumer_destroy_socket(socket
);
3518 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3520 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3521 struct lttng_domain
**domains
)
3526 struct lttng_ht_iter iter
;
3528 if (session
->kernel_session
!= NULL
) {
3529 DBG3("Listing domains found kernel domain");
3533 if (session
->ust_session
!= NULL
) {
3534 DBG3("Listing domains found UST global domain");
3538 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3540 if (agt
->being_used
) {
3551 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3552 if (*domains
== NULL
) {
3553 ret
= LTTNG_ERR_FATAL
;
3557 if (session
->kernel_session
!= NULL
) {
3558 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3560 /* Kernel session buffer type is always GLOBAL */
3561 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3566 if (session
->ust_session
!= NULL
) {
3567 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3568 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3572 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3574 if (agt
->being_used
) {
3575 (*domains
)[index
].type
= agt
->domain
;
3576 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3586 /* Return negative value to differentiate return code */
3592 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3594 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3595 struct ltt_session
*session
, struct lttng_channel
**channels
)
3597 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3600 case LTTNG_DOMAIN_KERNEL
:
3601 if (session
->kernel_session
!= NULL
) {
3602 nb_chan
= session
->kernel_session
->channel_count
;
3604 DBG3("Number of kernel channels %zd", nb_chan
);
3606 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3610 case LTTNG_DOMAIN_UST
:
3611 if (session
->ust_session
!= NULL
) {
3613 nb_chan
= lttng_ht_get_count(
3614 session
->ust_session
->domain_global
.channels
);
3617 DBG3("Number of UST global channels %zd", nb_chan
);
3619 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3624 ret
= -LTTNG_ERR_UND
;
3629 const size_t channel_size
= sizeof(struct lttng_channel
) +
3630 sizeof(struct lttng_channel_extended
);
3631 struct lttng_channel_extended
*channel_exts
;
3633 payload_size
= nb_chan
* channel_size
;
3634 *channels
= zmalloc(payload_size
);
3635 if (*channels
== NULL
) {
3636 ret
= -LTTNG_ERR_FATAL
;
3640 channel_exts
= ((void *) *channels
) +
3641 (nb_chan
* sizeof(struct lttng_channel
));
3642 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3643 if (ret
!= LTTNG_OK
) {
3658 * Command LTTNG_LIST_EVENTS processed by the client thread.
3660 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3661 struct ltt_session
*session
, char *channel_name
,
3662 struct lttng_payload
*payload
)
3665 ssize_t nb_events
= 0;
3666 struct lttcomm_event_command_header cmd_header
= {};
3667 const size_t cmd_header_offset
= payload
->buffer
.size
;
3669 ret
= lttng_dynamic_buffer_append(
3670 &payload
->buffer
, &cmd_header
, sizeof(cmd_header
));
3672 ret
= LTTNG_ERR_NOMEM
;
3677 case LTTNG_DOMAIN_KERNEL
:
3678 if (session
->kernel_session
!= NULL
) {
3679 nb_events
= list_lttng_kernel_events(channel_name
,
3680 session
->kernel_session
, payload
);
3683 case LTTNG_DOMAIN_UST
:
3685 if (session
->ust_session
!= NULL
) {
3686 nb_events
= list_lttng_ust_global_events(channel_name
,
3687 &session
->ust_session
->domain_global
,
3692 case LTTNG_DOMAIN_LOG4J
:
3693 case LTTNG_DOMAIN_JUL
:
3694 case LTTNG_DOMAIN_PYTHON
:
3695 if (session
->ust_session
) {
3696 struct lttng_ht_iter iter
;
3700 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3701 &iter
.iter
, agt
, node
.node
) {
3702 if (agt
->domain
== domain
) {
3703 nb_events
= list_lttng_agent_events(
3712 ret
= LTTNG_ERR_UND
;
3716 ((struct lttcomm_event_command_header
*) (payload
->buffer
.data
+
3717 cmd_header_offset
))->nb_events
= (uint32_t) nb_events
;
3722 /* Return negative value to differentiate return code */
3727 * Using the session list, filled a lttng_session array to send back to the
3728 * client for session listing.
3730 * The session list lock MUST be acquired before calling this function. Use
3731 * session_lock_list() and session_unlock_list().
3733 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3734 size_t session_count
, uid_t uid
, gid_t gid
)
3738 struct ltt_session
*session
;
3739 struct ltt_session_list
*list
= session_get_list();
3740 struct lttng_session_extended
*extended
=
3741 (typeof(extended
)) (&sessions
[session_count
]);
3743 DBG("Getting all available session for UID %d GID %d",
3746 * Iterate over session list and append data after the control struct in
3749 cds_list_for_each_entry(session
, &list
->head
, list
) {
3750 if (!session_get(session
)) {
3754 * Only list the sessions the user can control.
3756 if (!session_access_ok(session
, uid
) ||
3757 session
->destroyed
) {
3758 session_put(session
);
3762 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3763 struct ltt_ust_session
*usess
= session
->ust_session
;
3765 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3766 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3767 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3768 ret
= build_network_session_path(sessions
[i
].path
,
3769 sizeof(sessions
[i
].path
), session
);
3771 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3772 session
->consumer
->dst
.session_root_path
);
3775 PERROR("snprintf session path");
3776 session_put(session
);
3780 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3781 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3782 sessions
[i
].enabled
= session
->active
;
3783 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3784 sessions
[i
].live_timer_interval
= session
->live_timer
;
3785 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3786 extended
[i
].creation_time
.is_set
= 1;
3788 session_put(session
);
3793 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3794 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3796 int cmd_data_pending(struct ltt_session
*session
)
3799 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3800 struct ltt_ust_session
*usess
= session
->ust_session
;
3804 DBG("Data pending for session %s", session
->name
);
3806 /* Session MUST be stopped to ask for data availability. */
3807 if (session
->active
) {
3808 ret
= LTTNG_ERR_SESSION_STARTED
;
3812 * If stopped, just make sure we've started before else the above call
3813 * will always send that there is data pending.
3815 * The consumer assumes that when the data pending command is received,
3816 * the trace has been started before or else no output data is written
3817 * by the streams which is a condition for data pending. So, this is
3818 * *VERY* important that we don't ask the consumer before a start
3821 if (!session
->has_been_started
) {
3827 /* A rotation is still pending, we have to wait. */
3828 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3829 DBG("Rotate still pending for session %s", session
->name
);
3834 if (ksess
&& ksess
->consumer
) {
3835 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3837 /* Data is still being extracted for the kernel. */
3842 if (usess
&& usess
->consumer
) {
3843 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3845 /* Data is still being extracted for the kernel. */
3850 /* Data is ready to be read by a viewer */
3858 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3860 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3862 int cmd_snapshot_add_output(struct ltt_session
*session
,
3863 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3866 struct snapshot_output
*new_output
;
3871 DBG("Cmd snapshot add output for session %s", session
->name
);
3874 * Can't create an output if the session is not set in no-output mode.
3876 if (session
->output_traces
) {
3877 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3881 if (session
->has_non_mmap_channel
) {
3882 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3886 /* Only one output is allowed until we have the "tee" feature. */
3887 if (session
->snapshot
.nb_output
== 1) {
3888 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3892 new_output
= snapshot_output_alloc();
3894 ret
= LTTNG_ERR_NOMEM
;
3898 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3899 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3900 &session
->snapshot
);
3902 if (ret
== -ENOMEM
) {
3903 ret
= LTTNG_ERR_NOMEM
;
3905 ret
= LTTNG_ERR_INVALID
;
3911 snapshot_add_output(&session
->snapshot
, new_output
);
3913 *id
= new_output
->id
;
3920 snapshot_output_destroy(new_output
);
3926 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3928 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3930 int cmd_snapshot_del_output(struct ltt_session
*session
,
3931 const struct lttng_snapshot_output
*output
)
3934 struct snapshot_output
*sout
= NULL
;
3942 * Permission denied to create an output if the session is not
3943 * set in no output mode.
3945 if (session
->output_traces
) {
3946 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3951 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3953 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3954 } else if (*output
->name
!= '\0') {
3955 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3957 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3960 ret
= LTTNG_ERR_INVALID
;
3964 snapshot_delete_output(&session
->snapshot
, sout
);
3965 snapshot_output_destroy(sout
);
3974 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3976 * If no output is available, outputs is untouched and 0 is returned.
3978 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3980 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3981 struct lttng_snapshot_output
**outputs
)
3984 struct lttng_snapshot_output
*list
= NULL
;
3985 struct lttng_ht_iter iter
;
3986 struct snapshot_output
*output
;
3991 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3994 * Permission denied to create an output if the session is not
3995 * set in no output mode.
3997 if (session
->output_traces
) {
3998 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4002 if (session
->snapshot
.nb_output
== 0) {
4007 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4009 ret
= -LTTNG_ERR_NOMEM
;
4013 /* Copy list from session to the new list object. */
4015 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4016 output
, node
.node
) {
4017 assert(output
->consumer
);
4018 list
[idx
].id
= output
->id
;
4019 list
[idx
].max_size
= output
->max_size
;
4020 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4021 sizeof(list
[idx
].name
))) {
4022 ret
= -LTTNG_ERR_INVALID
;
4025 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4026 if (lttng_strncpy(list
[idx
].ctrl_url
,
4027 output
->consumer
->dst
.session_root_path
,
4028 sizeof(list
[idx
].ctrl_url
))) {
4029 ret
= -LTTNG_ERR_INVALID
;
4034 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4035 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4037 ret
= -LTTNG_ERR_NOMEM
;
4042 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4043 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4045 ret
= -LTTNG_ERR_NOMEM
;
4054 ret
= session
->snapshot
.nb_output
;
4063 * Check if we can regenerate the metadata for this session.
4064 * Only kernel, UST per-uid and non-live sessions are supported.
4066 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4069 int check_regenerate_metadata_support(struct ltt_session
*session
)
4075 if (session
->live_timer
!= 0) {
4076 ret
= LTTNG_ERR_LIVE_SESSION
;
4079 if (!session
->active
) {
4080 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4083 if (session
->ust_session
) {
4084 switch (session
->ust_session
->buffer_type
) {
4085 case LTTNG_BUFFER_PER_UID
:
4087 case LTTNG_BUFFER_PER_PID
:
4088 ret
= LTTNG_ERR_PER_PID_SESSION
;
4092 ret
= LTTNG_ERR_UNK
;
4096 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4097 session
->consumer
->relay_minor_version
< 8) {
4098 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4108 int clear_metadata_file(int fd
)
4113 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4114 if (lseek_ret
< 0) {
4120 ret
= ftruncate(fd
, 0);
4122 PERROR("ftruncate");
4131 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4134 struct buffer_reg_uid
*uid_reg
= NULL
;
4135 struct buffer_reg_session
*session_reg
= NULL
;
4138 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4139 struct ust_registry_session
*registry
;
4140 struct ust_registry_channel
*chan
;
4141 struct lttng_ht_iter iter_chan
;
4143 session_reg
= uid_reg
->registry
;
4144 registry
= session_reg
->reg
.ust
;
4146 pthread_mutex_lock(®istry
->lock
);
4147 registry
->metadata_len_sent
= 0;
4148 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4149 registry
->metadata_len
= 0;
4150 registry
->metadata_version
++;
4151 if (registry
->metadata_fd
> 0) {
4152 /* Clear the metadata file's content. */
4153 ret
= clear_metadata_file(registry
->metadata_fd
);
4155 pthread_mutex_unlock(®istry
->lock
);
4160 ret
= ust_metadata_session_statedump(registry
, NULL
,
4161 registry
->major
, registry
->minor
);
4163 pthread_mutex_unlock(®istry
->lock
);
4164 ERR("Failed to generate session metadata (err = %d)",
4168 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4170 struct ust_registry_event
*event
;
4171 struct lttng_ht_iter iter_event
;
4173 ret
= ust_metadata_channel_statedump(registry
, chan
);
4175 pthread_mutex_unlock(®istry
->lock
);
4176 ERR("Failed to generate channel metadata "
4180 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4182 ret
= ust_metadata_event_statedump(registry
,
4185 pthread_mutex_unlock(®istry
->lock
);
4186 ERR("Failed to generate event metadata "
4192 pthread_mutex_unlock(®istry
->lock
);
4201 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4203 * Ask the consumer to truncate the existing metadata file(s) and
4204 * then regenerate the metadata. Live and per-pid sessions are not
4205 * supported and return an error.
4207 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4209 int cmd_regenerate_metadata(struct ltt_session
*session
)
4215 ret
= check_regenerate_metadata_support(session
);
4220 if (session
->kernel_session
) {
4221 ret
= kernctl_session_regenerate_metadata(
4222 session
->kernel_session
->fd
);
4224 ERR("Failed to regenerate the kernel metadata");
4229 if (session
->ust_session
) {
4230 ret
= ust_regenerate_metadata(session
->ust_session
);
4232 ERR("Failed to regenerate the UST metadata");
4236 DBG("Cmd metadata regenerate for session %s", session
->name
);
4244 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4246 * Ask the tracer to regenerate a new statedump.
4248 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4250 int cmd_regenerate_statedump(struct ltt_session
*session
)
4256 if (!session
->active
) {
4257 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4261 if (session
->kernel_session
) {
4262 ret
= kernctl_session_regenerate_statedump(
4263 session
->kernel_session
->fd
);
4265 * Currently, the statedump in kernel can only fail if out
4269 if (ret
== -ENOMEM
) {
4270 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4272 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4274 ERR("Failed to regenerate the kernel statedump");
4279 if (session
->ust_session
) {
4280 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4282 * Currently, the statedump in UST always returns 0.
4285 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4286 ERR("Failed to regenerate the UST statedump");
4290 DBG("Cmd regenerate statedump for session %s", session
->name
);
4298 enum lttng_error_code
synchronize_tracer_notifier_register(
4299 struct notification_thread_handle
*notification_thread
,
4300 struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
4302 enum lttng_error_code ret_code
;
4303 const struct lttng_condition
*condition
=
4304 lttng_trigger_get_const_condition(trigger
);
4305 const char *trigger_name
;
4306 uid_t trigger_owner
;
4307 enum lttng_trigger_status trigger_status
;
4308 const enum lttng_domain_type trigger_domain
=
4309 lttng_trigger_get_underlying_domain_type_restriction(
4312 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4313 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4316 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_ON_EVENT
);
4318 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4319 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4323 session_lock_list();
4324 switch (trigger_domain
) {
4325 case LTTNG_DOMAIN_KERNEL
:
4327 ret_code
= kernel_register_event_notifier(trigger
, cmd_creds
);
4328 if (ret_code
!= LTTNG_OK
) {
4329 enum lttng_error_code notif_thread_unregister_ret
;
4331 notif_thread_unregister_ret
=
4332 notification_thread_command_unregister_trigger(
4333 notification_thread
, trigger
);
4335 if (notif_thread_unregister_ret
!= LTTNG_OK
) {
4336 /* Return the original error code. */
4337 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4339 (int) trigger_owner
,
4345 case LTTNG_DOMAIN_UST
:
4346 ust_app_global_update_all_event_notifier_rules();
4348 case LTTNG_DOMAIN_JUL
:
4349 case LTTNG_DOMAIN_LOG4J
:
4350 case LTTNG_DOMAIN_PYTHON
:
4352 /* Agent domains. */
4353 struct agent
*agt
= agent_find_by_event_notifier_domain(
4357 agt
= agent_create(trigger_domain
);
4359 ret_code
= LTTNG_ERR_NOMEM
;
4360 goto end_unlock_session_list
;
4363 agent_add(agt
, trigger_agents_ht_by_domain
);
4366 ret_code
= trigger_agent_enable(trigger
, agt
);
4367 if (ret_code
!= LTTNG_OK
) {
4368 goto end_unlock_session_list
;
4373 case LTTNG_DOMAIN_NONE
:
4378 ret_code
= LTTNG_OK
;
4379 end_unlock_session_list
:
4380 session_unlock_list();
4384 enum lttng_error_code
cmd_register_trigger(const struct lttng_credentials
*cmd_creds
,
4385 struct lttng_trigger
*trigger
,
4386 struct notification_thread_handle
*notification_thread
,
4387 struct lttng_trigger
**return_trigger
)
4389 enum lttng_error_code ret_code
;
4390 const char *trigger_name
;
4391 uid_t trigger_owner
;
4392 enum lttng_trigger_status trigger_status
;
4394 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4395 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4396 trigger_name
: "(unnamed)";
4398 trigger_status
= lttng_trigger_get_owner_uid(
4399 trigger
, &trigger_owner
);
4400 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4402 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4403 trigger_name
, (int) trigger_owner
,
4404 (int) lttng_credentials_get_uid(cmd_creds
));
4407 * Validate the trigger credentials against the command credentials.
4408 * Only the root user can register a trigger with non-matching
4411 if (!lttng_credentials_is_equal_uid(
4412 lttng_trigger_get_credentials(trigger
),
4414 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4415 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4416 trigger_name
, (int) trigger_owner
,
4417 (int) lttng_credentials_get_uid(cmd_creds
));
4418 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4424 * The bytecode generation also serves as a validation step for the
4425 * bytecode expressions.
4427 ret_code
= lttng_trigger_generate_bytecode(trigger
, cmd_creds
);
4428 if (ret_code
!= LTTNG_OK
) {
4429 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4430 trigger_name
, (int) trigger_owner
, ret_code
);
4435 * A reference to the trigger is acquired by the notification thread.
4436 * It is safe to return the same trigger to the caller since it the
4437 * other user holds a reference.
4439 * The trigger is modified during the execution of the
4440 * "register trigger" command. However, by the time the command returns,
4441 * it is safe to use without any locking as its properties are
4444 ret_code
= notification_thread_command_register_trigger(notification_thread
,
4446 if (ret_code
!= LTTNG_OK
) {
4447 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4448 trigger_name
, (int) trigger_owner
, ret_code
);
4452 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4453 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
?
4454 trigger_name
: "(unnamed)";
4457 * Synchronize tracers if the trigger adds an event notifier.
4459 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4460 ret_code
= synchronize_tracer_notifier_register(notification_thread
,
4461 trigger
, cmd_creds
);
4462 if (ret_code
!= LTTNG_OK
) {
4463 ERR("Error registering tracer notifier: %s",
4464 lttng_strerror(-ret_code
));
4470 * Return an updated trigger to the client.
4472 * Since a modified version of the same trigger is returned, acquire a
4473 * reference to the trigger so the caller doesn't have to care if those
4474 * are distinct instances or not.
4476 if (ret_code
== LTTNG_OK
) {
4477 lttng_trigger_get(trigger
);
4478 *return_trigger
= trigger
;
4479 /* Ownership of trigger was transferred to caller. */
4487 enum lttng_error_code
synchronize_tracer_notifier_unregister(
4488 const struct lttng_trigger
*trigger
)
4490 enum lttng_error_code ret_code
;
4491 const struct lttng_condition
*condition
=
4492 lttng_trigger_get_const_condition(trigger
);
4493 const enum lttng_domain_type trigger_domain
=
4494 lttng_trigger_get_underlying_domain_type_restriction(
4498 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_ON_EVENT
);
4500 session_lock_list();
4501 switch (trigger_domain
) {
4502 case LTTNG_DOMAIN_KERNEL
:
4503 ret_code
= kernel_unregister_event_notifier(trigger
);
4505 case LTTNG_DOMAIN_UST
:
4506 ust_app_global_update_all_event_notifier_rules();
4508 case LTTNG_DOMAIN_JUL
:
4509 case LTTNG_DOMAIN_LOG4J
:
4510 case LTTNG_DOMAIN_PYTHON
:
4512 /* Agent domains. */
4513 struct agent
*agt
= agent_find_by_event_notifier_domain(
4517 agt
= agent_create(trigger_domain
);
4519 ret_code
= LTTNG_ERR_NOMEM
;
4520 goto end_unlock_session_list
;
4523 agent_add(agt
, trigger_agents_ht_by_domain
);
4526 ret_code
= trigger_agent_disable(trigger
, agt
);
4527 if (ret_code
!= LTTNG_OK
) {
4528 goto end_unlock_session_list
;
4533 case LTTNG_DOMAIN_NONE
:
4538 ret_code
= LTTNG_OK
;
4540 end_unlock_session_list
:
4541 session_unlock_list();
4545 enum lttng_error_code
cmd_unregister_trigger(const struct lttng_credentials
*cmd_creds
,
4546 const struct lttng_trigger
*trigger
,
4547 struct notification_thread_handle
*notification_thread
)
4549 enum lttng_error_code ret_code
;
4550 const char *trigger_name
;
4551 uid_t trigger_owner
;
4552 enum lttng_trigger_status trigger_status
;
4554 trigger_status
= lttng_trigger_get_name(trigger
, &trigger_name
);
4555 trigger_name
= trigger_status
== LTTNG_TRIGGER_STATUS_OK
? trigger_name
: "(unnamed)";
4556 trigger_status
= lttng_trigger_get_owner_uid(trigger
, &trigger_owner
);
4557 assert(trigger_status
== LTTNG_TRIGGER_STATUS_OK
);
4559 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4560 trigger_name
, (int) trigger_owner
,
4561 (int) lttng_credentials_get_uid(cmd_creds
));
4564 * Validate the trigger credentials against the command credentials.
4565 * Only the root user can unregister a trigger with non-matching
4568 if (!lttng_credentials_is_equal_uid(
4569 lttng_trigger_get_credentials(trigger
),
4571 if (lttng_credentials_get_uid(cmd_creds
) != 0) {
4572 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4573 trigger_name
, (int) trigger_owner
,
4574 (int) lttng_credentials_get_uid(cmd_creds
));
4575 ret_code
= LTTNG_ERR_INVALID_TRIGGER
;
4580 ret_code
= notification_thread_command_unregister_trigger(notification_thread
,
4582 if (ret_code
!= LTTNG_OK
) {
4583 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4584 trigger_name
, (int) trigger_owner
, ret_code
);
4589 * Synchronize tracers if the trigger removes an event notifier.
4590 * Do this even if the trigger unregistration failed to at least stop
4591 * the tracers from producing notifications associated with this
4594 if (lttng_trigger_needs_tracer_notifier(trigger
)) {
4595 ret_code
= synchronize_tracer_notifier_unregister(trigger
);
4596 if (ret_code
!= LTTNG_OK
) {
4597 ERR("Error unregistering trigger to tracer.");
4607 int cmd_list_triggers(struct command_ctx
*cmd_ctx
,
4608 struct notification_thread_handle
*notification_thread
,
4609 struct lttng_triggers
**return_triggers
)
4612 enum lttng_error_code ret_code
;
4613 struct lttng_triggers
*triggers
= NULL
;
4615 /* Get the set of triggers from the notification thread. */
4616 ret_code
= notification_thread_command_list_triggers(
4617 notification_thread
, cmd_ctx
->creds
.uid
, &triggers
);
4618 if (ret_code
!= LTTNG_OK
) {
4623 *return_triggers
= triggers
;
4627 lttng_triggers_destroy(triggers
);
4631 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4632 * snapshot output is *not* set with a remote destination.
4634 * Return LTTNG_OK on success or a LTTNG_ERR code.
4636 static enum lttng_error_code
set_relayd_for_snapshot(
4637 struct consumer_output
*output
,
4638 const struct ltt_session
*session
)
4640 enum lttng_error_code status
= LTTNG_OK
;
4641 struct lttng_ht_iter iter
;
4642 struct consumer_socket
*socket
;
4643 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4644 const char *base_path
;
4649 DBG2("Set relayd object from snapshot output");
4651 if (session
->current_trace_chunk
) {
4652 enum lttng_trace_chunk_status chunk_status
=
4653 lttng_trace_chunk_get_id(
4654 session
->current_trace_chunk
,
4655 ¤t_chunk_id
.value
);
4657 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4658 current_chunk_id
.is_set
= true;
4660 ERR("Failed to get current trace chunk id");
4661 status
= LTTNG_ERR_UNK
;
4666 /* Ignore if snapshot consumer output is not network. */
4667 if (output
->type
!= CONSUMER_DST_NET
) {
4672 * The snapshot record URI base path overrides the session
4675 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4676 base_path
= output
->dst
.net
.control
.subdir
;
4678 base_path
= session
->base_path
;
4682 * For each consumer socket, create and send the relayd object of the
4686 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4687 socket
, node
.node
) {
4688 pthread_mutex_lock(socket
->lock
);
4689 status
= send_consumer_relayd_sockets(0, session
->id
,
4691 session
->name
, session
->hostname
,
4693 session
->live_timer
,
4694 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4695 session
->creation_time
,
4696 session
->name_contains_creation_time
);
4697 pthread_mutex_unlock(socket
->lock
);
4698 if (status
!= LTTNG_OK
) {
4710 * Record a kernel snapshot.
4712 * Return LTTNG_OK on success or a LTTNG_ERR code.
4714 static enum lttng_error_code
record_kernel_snapshot(
4715 struct ltt_kernel_session
*ksess
,
4716 const struct consumer_output
*output
,
4717 const struct ltt_session
*session
,
4718 int wait
, uint64_t nb_packets_per_stream
)
4720 enum lttng_error_code status
;
4726 status
= kernel_snapshot_record(
4727 ksess
, output
, wait
, nb_packets_per_stream
);
4732 * Record a UST snapshot.
4734 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4736 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4737 const struct consumer_output
*output
,
4738 const struct ltt_session
*session
,
4739 int wait
, uint64_t nb_packets_per_stream
)
4741 enum lttng_error_code status
;
4747 status
= ust_app_snapshot_record(
4748 usess
, output
, wait
, nb_packets_per_stream
);
4753 uint64_t get_session_size_one_more_packet_per_stream(
4754 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4756 uint64_t tot_size
= 0;
4758 if (session
->kernel_session
) {
4759 struct ltt_kernel_channel
*chan
;
4760 const struct ltt_kernel_session
*ksess
=
4761 session
->kernel_session
;
4763 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4764 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4766 * Don't take channel into account if we
4767 * already grab all its packets.
4771 tot_size
+= chan
->channel
->attr
.subbuf_size
4772 * chan
->stream_count
;
4776 if (session
->ust_session
) {
4777 const struct ltt_ust_session
*usess
= session
->ust_session
;
4779 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4787 * Calculate the number of packets we can grab from each stream that
4788 * fits within the overall snapshot max size.
4790 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4791 * the number of packets per stream.
4793 * TODO: this approach is not perfect: we consider the worse case
4794 * (packet filling the sub-buffers) as an upper bound, but we could do
4795 * better if we do this calculation while we actually grab the packet
4796 * content: we would know how much padding we don't actually store into
4799 * This algorithm is currently bounded by the number of packets per
4802 * Since we call this algorithm before actually grabbing the data, it's
4803 * an approximation: for instance, applications could appear/disappear
4804 * in between this call and actually grabbing data.
4807 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4811 uint64_t cur_nb_packets
= 0;
4814 return 0; /* Infinite */
4817 size_left
= max_size
;
4819 uint64_t one_more_packet_tot_size
;
4821 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4822 session
, cur_nb_packets
);
4823 if (!one_more_packet_tot_size
) {
4824 /* We are already grabbing all packets. */
4827 size_left
-= one_more_packet_tot_size
;
4828 if (size_left
< 0) {
4833 if (!cur_nb_packets
&& size_left
!= max_size
) {
4834 /* Not enough room to grab one packet of each stream, error. */
4837 return cur_nb_packets
;
4841 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4842 const struct snapshot_output
*snapshot_output
, int wait
)
4844 int64_t nb_packets_per_stream
;
4845 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4847 enum lttng_error_code ret_code
= LTTNG_OK
;
4848 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4849 struct consumer_output
*original_ust_consumer_output
= NULL
;
4850 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4851 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4852 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4854 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4856 snapshot_output
->name
,
4857 snapshot_output
->datetime
,
4858 snapshot_output
->nb_snapshot
);
4859 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4860 ERR("Failed to format snapshot name");
4861 ret_code
= LTTNG_ERR_INVALID
;
4864 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4865 snapshot_output
->name
, session
->name
,
4866 snapshot_chunk_name
);
4867 if (!session
->kernel_session
&& !session
->ust_session
) {
4868 ERR("Failed to record snapshot as no channels exist");
4869 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4873 if (session
->kernel_session
) {
4874 original_kernel_consumer_output
=
4875 session
->kernel_session
->consumer
;
4876 snapshot_kernel_consumer_output
=
4877 consumer_copy_output(snapshot_output
->consumer
);
4878 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
4879 snapshot_chunk_name
);
4880 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4881 original_kernel_consumer_output
);
4883 ERR("Failed to copy consumer sockets from snapshot output configuration");
4884 ret_code
= LTTNG_ERR_NOMEM
;
4887 ret_code
= set_relayd_for_snapshot(
4888 snapshot_kernel_consumer_output
, session
);
4889 if (ret_code
!= LTTNG_OK
) {
4890 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4893 session
->kernel_session
->consumer
=
4894 snapshot_kernel_consumer_output
;
4896 if (session
->ust_session
) {
4897 original_ust_consumer_output
= session
->ust_session
->consumer
;
4898 snapshot_ust_consumer_output
=
4899 consumer_copy_output(snapshot_output
->consumer
);
4900 strcpy(snapshot_ust_consumer_output
->chunk_path
,
4901 snapshot_chunk_name
);
4902 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4903 original_ust_consumer_output
);
4905 ERR("Failed to copy consumer sockets from snapshot output configuration");
4906 ret_code
= LTTNG_ERR_NOMEM
;
4909 ret_code
= set_relayd_for_snapshot(
4910 snapshot_ust_consumer_output
, session
);
4911 if (ret_code
!= LTTNG_OK
) {
4912 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4915 session
->ust_session
->consumer
=
4916 snapshot_ust_consumer_output
;
4919 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4920 snapshot_kernel_consumer_output
?:
4921 snapshot_ust_consumer_output
,
4922 consumer_output_get_base_path(
4923 snapshot_output
->consumer
),
4924 snapshot_chunk_name
);
4925 if (!snapshot_trace_chunk
) {
4926 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4928 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4931 assert(!session
->current_trace_chunk
);
4932 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4933 lttng_trace_chunk_put(snapshot_trace_chunk
);
4934 snapshot_trace_chunk
= NULL
;
4936 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4938 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4942 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4943 snapshot_output
->max_size
);
4944 if (nb_packets_per_stream
< 0) {
4945 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4946 goto error_close_trace_chunk
;
4949 if (session
->kernel_session
) {
4950 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4951 snapshot_kernel_consumer_output
, session
,
4952 wait
, nb_packets_per_stream
);
4953 if (ret_code
!= LTTNG_OK
) {
4954 goto error_close_trace_chunk
;
4958 if (session
->ust_session
) {
4959 ret_code
= record_ust_snapshot(session
->ust_session
,
4960 snapshot_ust_consumer_output
, session
,
4961 wait
, nb_packets_per_stream
);
4962 if (ret_code
!= LTTNG_OK
) {
4963 goto error_close_trace_chunk
;
4967 error_close_trace_chunk
:
4968 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
4969 ERR("Failed to release the current trace chunk of session \"%s\"",
4971 ret_code
= LTTNG_ERR_UNK
;
4974 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
4975 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
4977 * Don't goto end; make sure the chunk is closed for the session
4978 * to allow future snapshots.
4980 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4982 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4985 if (original_ust_consumer_output
) {
4986 session
->ust_session
->consumer
= original_ust_consumer_output
;
4988 if (original_kernel_consumer_output
) {
4989 session
->kernel_session
->consumer
=
4990 original_kernel_consumer_output
;
4992 consumer_output_put(snapshot_ust_consumer_output
);
4993 consumer_output_put(snapshot_kernel_consumer_output
);
4998 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5000 * The wait parameter is ignored so this call always wait for the snapshot to
5001 * complete before returning.
5003 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5005 int cmd_snapshot_record(struct ltt_session
*session
,
5006 const struct lttng_snapshot_output
*output
, int wait
)
5008 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5010 unsigned int snapshot_success
= 0;
5012 struct snapshot_output
*tmp_output
= NULL
;
5017 DBG("Cmd snapshot record for session %s", session
->name
);
5019 /* Get the datetime for the snapshot output directory. */
5020 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
5023 cmd_ret
= LTTNG_ERR_INVALID
;
5028 * Permission denied to create an output if the session is not
5029 * set in no output mode.
5031 if (session
->output_traces
) {
5032 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
5036 /* The session needs to be started at least once. */
5037 if (!session
->has_been_started
) {
5038 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5042 /* Use temporary output for the session. */
5043 if (*output
->ctrl_url
!= '\0') {
5044 tmp_output
= snapshot_output_alloc();
5046 cmd_ret
= LTTNG_ERR_NOMEM
;
5050 ret
= snapshot_output_init(session
, output
->max_size
,
5052 output
->ctrl_url
, output
->data_url
,
5056 if (ret
== -ENOMEM
) {
5057 cmd_ret
= LTTNG_ERR_NOMEM
;
5059 cmd_ret
= LTTNG_ERR_INVALID
;
5063 /* Use the global session count for the temporary snapshot. */
5064 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
5066 /* Use the global datetime */
5067 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
5068 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
5069 if (cmd_ret
!= LTTNG_OK
) {
5072 snapshot_success
= 1;
5074 struct snapshot_output
*sout
;
5075 struct lttng_ht_iter iter
;
5078 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
5079 &iter
.iter
, sout
, node
.node
) {
5080 struct snapshot_output output_copy
;
5083 * Make a local copy of the output and override output
5084 * parameters with those provided as part of the
5087 memcpy(&output_copy
, sout
, sizeof(output_copy
));
5089 if (output
->max_size
!= (uint64_t) -1ULL) {
5090 output_copy
.max_size
= output
->max_size
;
5093 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
5094 memcpy(output_copy
.datetime
, datetime
,
5097 /* Use temporary name. */
5098 if (*output
->name
!= '\0') {
5099 if (lttng_strncpy(output_copy
.name
,
5101 sizeof(output_copy
.name
))) {
5102 cmd_ret
= LTTNG_ERR_INVALID
;
5108 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
5109 if (cmd_ret
!= LTTNG_OK
) {
5113 snapshot_success
= 1;
5118 if (snapshot_success
) {
5119 session
->snapshot
.nb_snapshot
++;
5121 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
5126 snapshot_output_destroy(tmp_output
);
5132 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5134 int cmd_set_session_shm_path(struct ltt_session
*session
,
5135 const char *shm_path
)
5141 * Can only set shm path before session is started.
5143 if (session
->has_been_started
) {
5144 return LTTNG_ERR_SESSION_STARTED
;
5147 strncpy(session
->shm_path
, shm_path
,
5148 sizeof(session
->shm_path
));
5149 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
5155 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5157 * Ask the consumer to rotate the session output directory.
5158 * The session lock must be held.
5160 * Returns LTTNG_OK on success or else a negative LTTng error code.
5162 int cmd_rotate_session(struct ltt_session
*session
,
5163 struct lttng_rotate_session_return
*rotate_return
,
5164 bool quiet_rotation
,
5165 enum lttng_trace_chunk_command_type command
)
5168 uint64_t ongoing_rotation_chunk_id
;
5169 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5170 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
5171 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
5172 enum lttng_trace_chunk_status chunk_status
;
5173 bool failed_to_rotate
= false;
5174 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
5178 if (!session
->has_been_started
) {
5179 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5184 * Explicit rotation is not supported for live sessions.
5185 * However, live sessions can perform a quiet rotation on
5187 * Rotation is not supported for snapshot traces (no output).
5189 if ((!quiet_rotation
&& session
->live_timer
) ||
5190 !session
->output_traces
) {
5191 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5195 /* Unsupported feature in lttng-relayd before 2.11. */
5196 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5197 (session
->consumer
->relay_major_version
== 2 &&
5198 session
->consumer
->relay_minor_version
< 11)) {
5199 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5203 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5204 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5205 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5209 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5210 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5212 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5217 * After a stop, we only allow one rotation to occur, the other ones are
5218 * useless until a new start.
5220 if (session
->rotated_after_last_stop
) {
5221 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5223 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5228 * After a stop followed by a clear, disallow following rotations a they would
5229 * generate empty chunks.
5231 if (session
->cleared_after_last_stop
) {
5232 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5234 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5238 if (session
->active
) {
5239 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5241 if (!new_trace_chunk
) {
5242 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5248 * The current trace chunk becomes the chunk being archived.
5250 * After this point, "chunk_being_archived" must absolutely
5251 * be closed on the consumer(s), otherwise it will never be
5252 * cleaned-up, which will result in a leak.
5254 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5255 &chunk_being_archived
);
5257 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5261 if (session
->kernel_session
) {
5262 cmd_ret
= kernel_rotate_session(session
);
5263 if (cmd_ret
!= LTTNG_OK
) {
5264 failed_to_rotate
= true;
5265 rotation_fail_code
= cmd_ret
;
5268 if (session
->ust_session
) {
5269 cmd_ret
= ust_app_rotate_session(session
);
5270 if (cmd_ret
!= LTTNG_OK
) {
5271 failed_to_rotate
= true;
5272 rotation_fail_code
= cmd_ret
;
5276 if (!session
->active
) {
5277 session
->rotated_after_last_stop
= true;
5280 if (!chunk_being_archived
) {
5281 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5283 if (failed_to_rotate
) {
5284 cmd_ret
= rotation_fail_code
;
5291 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5292 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5293 &ongoing_rotation_chunk_id
);
5294 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5296 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5297 command
, session
->last_chunk_path
);
5299 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5303 if (failed_to_rotate
) {
5304 cmd_ret
= rotation_fail_code
;
5308 session
->quiet_rotation
= quiet_rotation
;
5309 ret
= timer_session_rotation_pending_check_start(session
,
5310 DEFAULT_ROTATE_PENDING_TIMER
);
5312 cmd_ret
= LTTNG_ERR_UNK
;
5316 if (rotate_return
) {
5317 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5320 session
->chunk_being_archived
= chunk_being_archived
;
5321 chunk_being_archived
= NULL
;
5322 if (!quiet_rotation
) {
5323 ret
= notification_thread_command_session_rotation_ongoing(
5324 notification_thread_handle
,
5325 session
->name
, session
->uid
, session
->gid
,
5326 ongoing_rotation_chunk_id
);
5327 if (ret
!= LTTNG_OK
) {
5328 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5334 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5335 session
->name
, ongoing_rotation_chunk_id
);
5337 lttng_trace_chunk_put(new_trace_chunk
);
5338 lttng_trace_chunk_put(chunk_being_archived
);
5339 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5342 if (session_reset_rotation_state(session
,
5343 LTTNG_ROTATION_STATE_ERROR
)) {
5344 ERR("Failed to reset rotation state of session \"%s\"",
5351 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5353 * Check if the session has finished its rotation.
5355 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5357 int cmd_rotate_get_info(struct ltt_session
*session
,
5358 struct lttng_rotation_get_info_return
*info_return
,
5359 uint64_t rotation_id
)
5361 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5362 enum lttng_rotation_state rotation_state
;
5364 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5365 session
->most_recent_chunk_id
.value
);
5367 if (session
->chunk_being_archived
) {
5368 enum lttng_trace_chunk_status chunk_status
;
5371 chunk_status
= lttng_trace_chunk_get_id(
5372 session
->chunk_being_archived
,
5374 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5376 rotation_state
= rotation_id
== chunk_id
?
5377 LTTNG_ROTATION_STATE_ONGOING
:
5378 LTTNG_ROTATION_STATE_EXPIRED
;
5380 if (session
->last_archived_chunk_id
.is_set
&&
5381 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5382 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5384 rotation_state
= session
->rotation_state
;
5388 switch (rotation_state
) {
5389 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5390 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5393 case LTTNG_ROTATION_STATE_EXPIRED
:
5394 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5395 rotation_id
, session
->name
);
5397 case LTTNG_ROTATION_STATE_ONGOING
:
5398 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5399 rotation_id
, session
->name
);
5401 case LTTNG_ROTATION_STATE_COMPLETED
:
5405 char *current_tracing_path_reply
;
5406 size_t current_tracing_path_reply_len
;
5408 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5409 rotation_id
, session
->name
);
5411 switch (session_get_consumer_destination_type(session
)) {
5412 case CONSUMER_DST_LOCAL
:
5413 current_tracing_path_reply
=
5414 info_return
->location
.local
.absolute_path
;
5415 current_tracing_path_reply_len
=
5416 sizeof(info_return
->location
.local
.absolute_path
);
5417 info_return
->location_type
=
5418 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5419 fmt_ret
= asprintf(&chunk_path
,
5420 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5421 session_get_base_path(session
),
5422 session
->last_archived_chunk_name
);
5423 if (fmt_ret
== -1) {
5424 PERROR("Failed to format the path of the last archived trace chunk");
5425 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5426 cmd_ret
= LTTNG_ERR_UNK
;
5430 case CONSUMER_DST_NET
:
5432 uint16_t ctrl_port
, data_port
;
5434 current_tracing_path_reply
=
5435 info_return
->location
.relay
.relative_path
;
5436 current_tracing_path_reply_len
=
5437 sizeof(info_return
->location
.relay
.relative_path
);
5438 /* Currently the only supported relay protocol. */
5439 info_return
->location
.relay
.protocol
=
5440 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5442 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5443 session_get_net_consumer_hostname(session
),
5444 sizeof(info_return
->location
.relay
.host
));
5446 ERR("Failed to copy host name to rotate_get_info reply");
5447 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5448 cmd_ret
= LTTNG_ERR_SET_URL
;
5452 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5453 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5454 info_return
->location
.relay
.ports
.data
= data_port
;
5455 info_return
->location_type
=
5456 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5457 chunk_path
= strdup(session
->last_chunk_path
);
5459 ERR("Failed to allocate the path of the last archived trace chunk");
5460 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5461 cmd_ret
= LTTNG_ERR_UNK
;
5470 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5471 chunk_path
, current_tracing_path_reply_len
);
5474 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5475 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5476 cmd_ret
= LTTNG_ERR_UNK
;
5482 case LTTNG_ROTATION_STATE_ERROR
:
5483 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5484 rotation_id
, session
->name
);
5492 info_return
->status
= (int32_t) rotation_state
;
5497 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5499 * Configure the automatic rotation parameters.
5500 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5501 * 'activate' to false means deactivate the rotation schedule and validate that
5502 * 'new_value' has the same value as the currently active value.
5504 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5506 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5507 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5509 struct notification_thread_handle
*notification_thread_handle
)
5512 uint64_t *parameter_value
;
5516 DBG("Cmd rotate set schedule session %s", session
->name
);
5518 if (session
->live_timer
|| !session
->output_traces
) {
5519 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5520 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5524 switch (schedule_type
) {
5525 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5526 parameter_value
= &session
->rotate_size
;
5528 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5529 parameter_value
= &session
->rotate_timer_period
;
5530 if (new_value
>= UINT_MAX
) {
5531 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5532 new_value
, UINT_MAX
);
5533 ret
= LTTNG_ERR_INVALID
;
5538 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5539 ret
= LTTNG_ERR_INVALID
;
5543 /* Improper use of the API. */
5544 if (new_value
== -1ULL) {
5545 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5546 ret
= LTTNG_ERR_INVALID
;
5551 * As indicated in struct ltt_session's comments, a value of == 0 means
5552 * this schedule rotation type is not in use.
5554 * Reject the command if we were asked to activate a schedule that was
5557 if (activate
&& *parameter_value
!= 0) {
5558 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5559 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5564 * Reject the command if we were asked to deactivate a schedule that was
5567 if (!activate
&& *parameter_value
== 0) {
5568 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5569 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5574 * Reject the command if we were asked to deactivate a schedule that
5577 if (!activate
&& *parameter_value
!= new_value
) {
5578 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5579 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5583 *parameter_value
= activate
? new_value
: 0;
5585 switch (schedule_type
) {
5586 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5587 if (activate
&& session
->active
) {
5589 * Only start the timer if the session is active,
5590 * otherwise it will be started when the session starts.
5592 ret
= timer_session_rotation_schedule_timer_start(
5593 session
, new_value
);
5595 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5596 ret
= LTTNG_ERR_UNK
;
5600 ret
= timer_session_rotation_schedule_timer_stop(
5603 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5604 ret
= LTTNG_ERR_UNK
;
5609 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5611 ret
= subscribe_session_consumed_size_rotation(session
,
5612 new_value
, notification_thread_handle
);
5614 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5615 ret
= LTTNG_ERR_UNK
;
5619 ret
= unsubscribe_session_consumed_size_rotation(session
,
5620 notification_thread_handle
);
5622 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5623 ret
= LTTNG_ERR_UNK
;
5630 /* Would have been caught before. */
5642 /* Wait for a given path to be removed before continuing. */
5643 static enum lttng_error_code
wait_on_path(void *path_data
)
5645 const char *shm_path
= path_data
;
5647 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5653 ret
= stat(shm_path
, &st
);
5655 if (errno
!= ENOENT
) {
5656 PERROR("stat() returned an error while checking for the existence of the shm path");
5658 DBG("shm path no longer exists, completing the destruction of session");
5662 if (!S_ISDIR(st
.st_mode
)) {
5663 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5668 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5674 * Returns a pointer to a handler to run on completion of a command.
5675 * Returns NULL if no handler has to be run for the last command executed.
5677 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5679 struct cmd_completion_handler
*handler
= current_completion_handler
;
5681 current_completion_handler
= NULL
;
5686 * Init command subsystem.
5691 * Set network sequence index to 1 for streams to match a relayd
5692 * socket on the consumer side.
5694 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5695 relayd_net_seq_idx
= 1;
5696 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5698 DBG("Command subsystem initialized");