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/tracker.h"
10 #include "lttng/lttng-error.h"
11 #include "lttng/tracker.h"
15 #include <urcu/list.h>
16 #include <urcu/uatomic.h>
20 #include <common/defaults.h>
21 #include <common/common.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/relayd/relayd.h>
24 #include <common/utils.h>
25 #include <common/compat/string.h>
26 #include <common/kernel-ctl/kernel-ctl.h>
27 #include <common/dynamic-buffer.h>
28 #include <common/buffer-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/action/action.h>
34 #include <lttng/channel.h>
35 #include <lttng/channel-internal.h>
36 #include <lttng/rotate-internal.h>
37 #include <lttng/location-internal.h>
38 #include <lttng/session-internal.h>
39 #include <lttng/userspace-probe-internal.h>
40 #include <lttng/session-descriptor-internal.h>
41 #include <common/string-utils/string-utils.h>
46 #include "health-sessiond.h"
48 #include "kernel-consumer.h"
49 #include "lttng-sessiond.h"
51 #include "lttng-syscall.h"
53 #include "buffer-registry.h"
54 #include "notification-thread.h"
55 #include "notification-thread-commands.h"
57 #include "rotation-thread.h"
59 #include "agent-thread.h"
63 /* Sleep for 100ms between each check for the shm path's deletion. */
64 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
66 struct cmd_destroy_session_reply_context
{
68 bool implicit_rotation_on_destroy
;
70 * Indicates whether or not an error occurred while launching the
71 * destruction of a session.
73 enum lttng_error_code destruction_status
;
76 static enum lttng_error_code
wait_on_path(void *path
);
79 * Command completion handler that is used by the destroy command
80 * when a session that has a non-default shm_path is being destroyed.
82 * See comment in cmd_destroy_session() for the rationale.
84 static struct destroy_completion_handler
{
85 struct cmd_completion_handler handler
;
86 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
87 } destroy_completion_handler
= {
90 .data
= destroy_completion_handler
.shm_path
95 static struct cmd_completion_handler
*current_completion_handler
;
98 * Used to keep a unique index for each relayd socket created where this value
99 * is associated with streams on the consumer so it can match the right relayd
100 * to send to. It must be accessed with the relayd_net_seq_idx_lock
103 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
104 static uint64_t relayd_net_seq_idx
;
106 static int validate_ust_event_name(const char *);
107 static int cmd_enable_event_internal(struct ltt_session
*session
,
108 const struct lttng_domain
*domain
,
109 char *channel_name
, struct lttng_event
*event
,
110 char *filter_expression
,
111 struct lttng_filter_bytecode
*filter
,
112 struct lttng_event_exclusion
*exclusion
,
116 * Create a session path used by list_lttng_sessions for the case that the
117 * session consumer is on the network.
119 static int build_network_session_path(char *dst
, size_t size
,
120 struct ltt_session
*session
)
122 int ret
, kdata_port
, udata_port
;
123 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
124 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
129 memset(tmp_urls
, 0, sizeof(tmp_urls
));
130 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
132 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
134 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
135 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
136 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
139 if (session
->ust_session
&& session
->ust_session
->consumer
) {
140 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
141 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
144 if (uuri
== NULL
&& kuri
== NULL
) {
145 uri
= &session
->consumer
->dst
.net
.control
;
146 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
147 } else if (kuri
&& uuri
) {
148 ret
= uri_compare(kuri
, uuri
);
152 /* Build uuri URL string */
153 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
160 } else if (kuri
&& uuri
== NULL
) {
162 } else if (uuri
&& kuri
== NULL
) {
166 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
172 * Do we have a UST url set. If yes, this means we have both kernel and UST
175 if (*tmp_uurl
!= '\0') {
176 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
177 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
180 if (kuri
|| (!kuri
&& !uuri
)) {
183 /* No kernel URI, use the UST port. */
186 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
194 * Get run-time attributes if the session has been started (discarded events,
197 static int get_kernel_runtime_stats(struct ltt_session
*session
,
198 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
199 uint64_t *lost_packets
)
203 if (!session
->has_been_started
) {
205 *discarded_events
= 0;
210 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
211 session
->kernel_session
->consumer
,
217 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
218 session
->kernel_session
->consumer
,
229 * Get run-time attributes if the session has been started (discarded events,
232 static int get_ust_runtime_stats(struct ltt_session
*session
,
233 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
234 uint64_t *lost_packets
)
237 struct ltt_ust_session
*usess
;
239 if (!discarded_events
|| !lost_packets
) {
244 usess
= session
->ust_session
;
245 assert(discarded_events
);
246 assert(lost_packets
);
248 if (!usess
|| !session
->has_been_started
) {
249 *discarded_events
= 0;
255 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
256 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
257 &usess
->buffer_reg_uid_list
,
258 usess
->consumer
, uchan
->id
,
259 uchan
->attr
.overwrite
,
262 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
263 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
264 uchan
, usess
->consumer
,
265 uchan
->attr
.overwrite
,
271 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
272 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
274 ERR("Unsupported buffer type");
285 * Fill lttng_channel array of all channels.
287 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
288 struct ltt_session
*session
, struct lttng_channel
*channels
,
289 struct lttng_channel_extended
*chan_exts
)
292 struct ltt_kernel_channel
*kchan
;
294 DBG("Listing channels for session %s", session
->name
);
297 case LTTNG_DOMAIN_KERNEL
:
298 /* Kernel channels */
299 if (session
->kernel_session
!= NULL
) {
300 cds_list_for_each_entry(kchan
,
301 &session
->kernel_session
->channel_list
.head
, list
) {
302 uint64_t discarded_events
, lost_packets
;
303 struct lttng_channel_extended
*extended
;
305 extended
= (struct lttng_channel_extended
*)
306 kchan
->channel
->attr
.extended
.ptr
;
308 ret
= get_kernel_runtime_stats(session
, kchan
,
309 &discarded_events
, &lost_packets
);
313 /* Copy lttng_channel struct to array */
314 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
315 channels
[i
].enabled
= kchan
->enabled
;
316 chan_exts
[i
].discarded_events
=
318 chan_exts
[i
].lost_packets
= lost_packets
;
319 chan_exts
[i
].monitor_timer_interval
=
320 extended
->monitor_timer_interval
;
321 chan_exts
[i
].blocking_timeout
= 0;
326 case LTTNG_DOMAIN_UST
:
328 struct lttng_ht_iter iter
;
329 struct ltt_ust_channel
*uchan
;
332 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
333 &iter
.iter
, uchan
, node
.node
) {
334 uint64_t discarded_events
= 0, lost_packets
= 0;
336 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
337 LTTNG_SYMBOL_NAME_LEN
)) {
340 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
341 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
342 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
343 channels
[i
].attr
.switch_timer_interval
=
344 uchan
->attr
.switch_timer_interval
;
345 channels
[i
].attr
.read_timer_interval
=
346 uchan
->attr
.read_timer_interval
;
347 channels
[i
].enabled
= uchan
->enabled
;
348 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
349 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
352 * Map enum lttng_ust_output to enum lttng_event_output.
354 switch (uchan
->attr
.output
) {
356 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
360 * LTTNG_UST_MMAP is the only supported UST
367 chan_exts
[i
].monitor_timer_interval
=
368 uchan
->monitor_timer_interval
;
369 chan_exts
[i
].blocking_timeout
=
370 uchan
->attr
.u
.s
.blocking_timeout
;
372 ret
= get_ust_runtime_stats(session
, uchan
,
373 &discarded_events
, &lost_packets
);
377 chan_exts
[i
].discarded_events
= discarded_events
;
378 chan_exts
[i
].lost_packets
= lost_packets
;
390 return -LTTNG_ERR_FATAL
;
396 static int increment_extended_len(const char *filter_expression
,
397 struct lttng_event_exclusion
*exclusion
,
398 const struct lttng_userspace_probe_location
*probe_location
,
399 size_t *extended_len
)
403 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
405 if (filter_expression
) {
406 *extended_len
+= strlen(filter_expression
) + 1;
410 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
413 if (probe_location
) {
414 ret
= lttng_userspace_probe_location_serialize(probe_location
,
419 *extended_len
+= ret
;
426 static int append_extended_info(const char *filter_expression
,
427 struct lttng_event_exclusion
*exclusion
,
428 struct lttng_userspace_probe_location
*probe_location
,
432 size_t filter_len
= 0;
433 size_t nb_exclusions
= 0;
434 size_t userspace_probe_location_len
= 0;
435 struct lttng_dynamic_buffer location_buffer
;
436 struct lttcomm_event_extended_header extended_header
;
438 if (filter_expression
) {
439 filter_len
= strlen(filter_expression
) + 1;
443 nb_exclusions
= exclusion
->count
;
446 if (probe_location
) {
447 lttng_dynamic_buffer_init(&location_buffer
);
448 ret
= lttng_userspace_probe_location_serialize(probe_location
,
449 &location_buffer
, NULL
);
454 userspace_probe_location_len
= location_buffer
.size
;
457 /* Set header fields */
458 extended_header
.filter_len
= filter_len
;
459 extended_header
.nb_exclusions
= nb_exclusions
;
460 extended_header
.userspace_probe_location_len
= userspace_probe_location_len
;
463 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
464 *extended_at
+= sizeof(extended_header
);
466 /* Copy filter string */
467 if (filter_expression
) {
468 memcpy(*extended_at
, filter_expression
, filter_len
);
469 *extended_at
+= filter_len
;
472 /* Copy exclusion names */
474 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
476 memcpy(*extended_at
, &exclusion
->names
, len
);
480 if (probe_location
) {
481 memcpy(*extended_at
, location_buffer
.data
, location_buffer
.size
);
482 *extended_at
+= location_buffer
.size
;
483 lttng_dynamic_buffer_reset(&location_buffer
);
491 * Create a list of agent domain events.
493 * Return number of events in list on success or else a negative value.
495 static int list_lttng_agent_events(struct agent
*agt
,
496 struct lttng_event
**events
, size_t *total_size
)
499 unsigned int nb_event
= 0;
500 struct agent_event
*event
;
501 struct lttng_event
*tmp_events
= NULL
;
502 struct lttng_ht_iter iter
;
503 size_t extended_len
= 0;
509 DBG3("Listing agent events");
512 nb_event
= lttng_ht_get_count(agt
->events
);
519 /* Compute required extended infos size */
520 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
523 * This is only valid because the commands which add events are
524 * processed in the same thread as the listing.
526 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
527 ret
= increment_extended_len(event
->filter_expression
, NULL
, NULL
,
530 DBG("Error computing the length of extended info message");
531 ret
= -LTTNG_ERR_FATAL
;
536 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
537 tmp_events
= zmalloc(*total_size
);
539 PERROR("zmalloc agent events session");
540 ret
= -LTTNG_ERR_FATAL
;
544 extended_at
= ((uint8_t *) tmp_events
) +
545 nb_event
* sizeof(struct lttng_event
);
547 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
548 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
549 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
550 tmp_events
[i
].enabled
= event
->enabled
;
551 tmp_events
[i
].loglevel
= event
->loglevel_value
;
552 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
555 /* Append extended info */
556 ret
= append_extended_info(event
->filter_expression
, NULL
, NULL
,
559 DBG("Error appending extended info message");
560 ret
= -LTTNG_ERR_FATAL
;
565 *events
= tmp_events
;
567 assert(nb_event
== i
);
578 * Create a list of ust global domain events.
580 static int list_lttng_ust_global_events(char *channel_name
,
581 struct ltt_ust_domain_global
*ust_global
,
582 struct lttng_event
**events
, size_t *total_size
)
585 unsigned int nb_event
= 0;
586 struct lttng_ht_iter iter
;
587 struct lttng_ht_node_str
*node
;
588 struct ltt_ust_channel
*uchan
;
589 struct ltt_ust_event
*uevent
;
590 struct lttng_event
*tmp
;
591 size_t extended_len
= 0;
594 DBG("Listing UST global events for channel %s", channel_name
);
598 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
599 node
= lttng_ht_iter_get_node_str(&iter
);
601 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
605 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
607 nb_event
= lttng_ht_get_count(uchan
->events
);
614 DBG3("Listing UST global %d events", nb_event
);
616 /* Compute required extended infos size */
617 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
618 if (uevent
->internal
) {
623 ret
= increment_extended_len(uevent
->filter_expression
,
624 uevent
->exclusion
, NULL
, &extended_len
);
626 DBG("Error computing the length of extended info message");
627 ret
= -LTTNG_ERR_FATAL
;
632 /* All events are internal, skip. */
638 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
639 tmp
= zmalloc(*total_size
);
641 ret
= -LTTNG_ERR_FATAL
;
645 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
647 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
648 if (uevent
->internal
) {
649 /* This event should remain hidden from clients */
652 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
653 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
654 tmp
[i
].enabled
= uevent
->enabled
;
656 switch (uevent
->attr
.instrumentation
) {
657 case LTTNG_UST_TRACEPOINT
:
658 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
660 case LTTNG_UST_PROBE
:
661 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
663 case LTTNG_UST_FUNCTION
:
664 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
668 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
669 switch (uevent
->attr
.loglevel_type
) {
670 case LTTNG_UST_LOGLEVEL_ALL
:
671 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
673 case LTTNG_UST_LOGLEVEL_RANGE
:
674 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
676 case LTTNG_UST_LOGLEVEL_SINGLE
:
677 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
680 if (uevent
->filter
) {
683 if (uevent
->exclusion
) {
684 tmp
[i
].exclusion
= 1;
688 /* Append extended info */
689 ret
= append_extended_info(uevent
->filter_expression
,
690 uevent
->exclusion
, NULL
, &extended_at
);
692 DBG("Error appending extended info message");
693 ret
= -LTTNG_ERR_FATAL
;
706 * Fill lttng_event array of all kernel events in the channel.
708 static int list_lttng_kernel_events(char *channel_name
,
709 struct ltt_kernel_session
*kernel_session
,
710 struct lttng_event
**events
, size_t *total_size
)
713 unsigned int nb_event
;
714 struct ltt_kernel_event
*event
;
715 struct ltt_kernel_channel
*kchan
;
716 size_t extended_len
= 0;
719 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
721 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
725 nb_event
= kchan
->event_count
;
727 DBG("Listing events for channel %s", kchan
->channel
->name
);
735 /* Compute required extended infos size */
736 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
737 ret
= increment_extended_len(event
->filter_expression
, NULL
,
738 event
->userspace_probe_location
,
741 DBG("Error computing the length of extended info message");
742 ret
= -LTTNG_ERR_FATAL
;
747 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
748 *events
= zmalloc(*total_size
);
749 if (*events
== NULL
) {
750 ret
= -LTTNG_ERR_FATAL
;
754 extended_at
= ((void *) *events
) +
755 nb_event
* sizeof(struct lttng_event
);
757 /* Kernel channels */
758 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
759 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
760 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
761 (*events
)[i
].enabled
= event
->enabled
;
762 (*events
)[i
].filter
=
763 (unsigned char) !!event
->filter_expression
;
765 switch (event
->event
->instrumentation
) {
766 case LTTNG_KERNEL_TRACEPOINT
:
767 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
769 case LTTNG_KERNEL_KRETPROBE
:
770 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
771 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
772 sizeof(struct lttng_kernel_kprobe
));
774 case LTTNG_KERNEL_KPROBE
:
775 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
776 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
777 sizeof(struct lttng_kernel_kprobe
));
779 case LTTNG_KERNEL_UPROBE
:
780 (*events
)[i
].type
= LTTNG_EVENT_USERSPACE_PROBE
;
782 case LTTNG_KERNEL_FUNCTION
:
783 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
784 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
785 sizeof(struct lttng_kernel_function
));
787 case LTTNG_KERNEL_NOOP
:
788 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
790 case LTTNG_KERNEL_SYSCALL
:
791 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
793 case LTTNG_KERNEL_ALL
:
801 /* Append extended info */
802 ret
= append_extended_info(event
->filter_expression
, NULL
,
803 event
->userspace_probe_location
, &extended_at
);
805 DBG("Error appending extended info message");
806 ret
= -LTTNG_ERR_FATAL
;
819 * Add URI so the consumer output object. Set the correct path depending on the
820 * domain adding the default trace directory.
822 static enum lttng_error_code
add_uri_to_consumer(
823 const struct ltt_session
*session
,
824 struct consumer_output
*consumer
,
825 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
828 enum lttng_error_code ret_code
= LTTNG_OK
;
832 if (consumer
== NULL
) {
833 DBG("No consumer detected. Don't add URI. Stopping.");
834 ret_code
= LTTNG_ERR_NO_CONSUMER
;
839 case LTTNG_DOMAIN_KERNEL
:
840 ret
= lttng_strncpy(consumer
->domain_subdir
,
841 DEFAULT_KERNEL_TRACE_DIR
,
842 sizeof(consumer
->domain_subdir
));
844 case LTTNG_DOMAIN_UST
:
845 ret
= lttng_strncpy(consumer
->domain_subdir
,
846 DEFAULT_UST_TRACE_DIR
,
847 sizeof(consumer
->domain_subdir
));
851 * This case is possible is we try to add the URI to the global
852 * tracing session consumer object which in this case there is
855 memset(consumer
->domain_subdir
, 0,
856 sizeof(consumer
->domain_subdir
));
860 ERR("Failed to initialize consumer output domain subdirectory");
861 ret_code
= LTTNG_ERR_FATAL
;
865 switch (uri
->dtype
) {
868 DBG2("Setting network URI to consumer");
870 if (consumer
->type
== CONSUMER_DST_NET
) {
871 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
872 consumer
->dst
.net
.control_isset
) ||
873 (uri
->stype
== LTTNG_STREAM_DATA
&&
874 consumer
->dst
.net
.data_isset
)) {
875 ret_code
= LTTNG_ERR_URL_EXIST
;
879 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
882 /* Set URI into consumer output object */
883 ret
= consumer_set_network_uri(session
, consumer
, uri
);
887 } else if (ret
== 1) {
889 * URI was the same in the consumer so we do not append the subdir
890 * again so to not duplicate output dir.
897 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
898 ret_code
= LTTNG_ERR_INVALID
;
901 DBG2("Setting trace directory path from URI to %s",
903 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
905 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
907 sizeof(consumer
->dst
.session_root_path
));
909 ret_code
= LTTNG_ERR_FATAL
;
912 consumer
->type
= CONSUMER_DST_LOCAL
;
922 * Init tracing by creating trace directory and sending fds kernel consumer.
924 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
927 struct lttng_ht_iter iter
;
928 struct consumer_socket
*socket
;
934 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
935 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
937 pthread_mutex_lock(socket
->lock
);
938 ret
= kernel_consumer_send_session(socket
, session
);
939 pthread_mutex_unlock(socket
->lock
);
941 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
953 * Create a socket to the relayd using the URI.
955 * On success, the relayd_sock pointer is set to the created socket.
956 * Else, it remains untouched and an LTTng error code is returned.
958 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
959 struct lttcomm_relayd_sock
**relayd_sock
,
960 struct consumer_output
*consumer
)
963 enum lttng_error_code status
= LTTNG_OK
;
964 struct lttcomm_relayd_sock
*rsock
;
966 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
967 RELAYD_VERSION_COMM_MINOR
);
969 status
= LTTNG_ERR_FATAL
;
974 * Connect to relayd so we can proceed with a session creation. This call
975 * can possibly block for an arbitrary amount of time to set the health
976 * state to be in poll execution.
979 ret
= relayd_connect(rsock
);
982 ERR("Unable to reach lttng-relayd");
983 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
987 /* Create socket for control stream. */
988 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
989 uint64_t result_flags
;
991 DBG3("Creating relayd stream socket from URI");
993 /* Check relayd version */
994 ret
= relayd_version_check(rsock
);
995 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
996 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
998 } else if (ret
< 0) {
999 ERR("Unable to reach lttng-relayd");
1000 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1003 consumer
->relay_major_version
= rsock
->major
;
1004 consumer
->relay_minor_version
= rsock
->minor
;
1005 ret
= relayd_get_configuration(rsock
, 0,
1008 ERR("Unable to get relayd configuration");
1009 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1012 if (result_flags
& LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED
) {
1013 consumer
->relay_allows_clear
= true;
1015 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1016 DBG3("Creating relayd data socket from URI");
1018 /* Command is not valid */
1019 ERR("Relayd invalid stream type: %d", uri
->stype
);
1020 status
= LTTNG_ERR_INVALID
;
1024 *relayd_sock
= rsock
;
1029 /* The returned value is not useful since we are on an error path. */
1030 (void) relayd_close(rsock
);
1038 * Connect to the relayd using URI and send the socket to the right consumer.
1040 * The consumer socket lock must be held by the caller.
1042 * Returns LTTNG_OK on success or an LTTng error code on failure.
1044 static enum lttng_error_code
send_consumer_relayd_socket(
1045 unsigned int session_id
,
1046 struct lttng_uri
*relayd_uri
,
1047 struct consumer_output
*consumer
,
1048 struct consumer_socket
*consumer_sock
,
1049 const char *session_name
, const char *hostname
,
1050 const char *base_path
, int session_live_timer
,
1051 const uint64_t *current_chunk_id
,
1052 time_t session_creation_time
,
1053 bool session_name_contains_creation_time
)
1056 struct lttcomm_relayd_sock
*rsock
= NULL
;
1057 enum lttng_error_code status
;
1059 /* Connect to relayd and make version check if uri is the control. */
1060 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1061 if (status
!= LTTNG_OK
) {
1062 goto relayd_comm_error
;
1066 /* Set the network sequence index if not set. */
1067 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1068 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1070 * Increment net_seq_idx because we are about to transfer the
1071 * new relayd socket to the consumer.
1072 * Assign unique key so the consumer can match streams.
1074 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1075 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1078 /* Send relayd socket to consumer. */
1079 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1080 relayd_uri
->stype
, session_id
,
1081 session_name
, hostname
, base_path
,
1082 session_live_timer
, current_chunk_id
,
1083 session_creation_time
, session_name_contains_creation_time
);
1085 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1089 /* Flag that the corresponding socket was sent. */
1090 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1091 consumer_sock
->control_sock_sent
= 1;
1092 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1093 consumer_sock
->data_sock_sent
= 1;
1097 * Close socket which was dup on the consumer side. The session daemon does
1098 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1102 if (status
!= LTTNG_OK
) {
1104 * The consumer output for this session should not be used anymore
1105 * since the relayd connection failed thus making any tracing or/and
1106 * streaming not usable.
1108 consumer
->enabled
= 0;
1110 (void) relayd_close(rsock
);
1118 * Send both relayd sockets to a specific consumer and domain. This is a
1119 * helper function to facilitate sending the information to the consumer for a
1122 * The consumer socket lock must be held by the caller.
1124 * Returns LTTNG_OK, or an LTTng error code on failure.
1126 static enum lttng_error_code
send_consumer_relayd_sockets(
1127 enum lttng_domain_type domain
,
1128 unsigned int session_id
, struct consumer_output
*consumer
,
1129 struct consumer_socket
*sock
, const char *session_name
,
1130 const char *hostname
, const char *base_path
, int session_live_timer
,
1131 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1132 bool session_name_contains_creation_time
)
1134 enum lttng_error_code status
= LTTNG_OK
;
1139 /* Sending control relayd socket. */
1140 if (!sock
->control_sock_sent
) {
1141 status
= send_consumer_relayd_socket(session_id
,
1142 &consumer
->dst
.net
.control
, consumer
, sock
,
1143 session_name
, hostname
, base_path
, session_live_timer
,
1144 current_chunk_id
, session_creation_time
,
1145 session_name_contains_creation_time
);
1146 if (status
!= LTTNG_OK
) {
1151 /* Sending data relayd socket. */
1152 if (!sock
->data_sock_sent
) {
1153 status
= send_consumer_relayd_socket(session_id
,
1154 &consumer
->dst
.net
.data
, consumer
, sock
,
1155 session_name
, hostname
, base_path
, session_live_timer
,
1156 current_chunk_id
, session_creation_time
,
1157 session_name_contains_creation_time
);
1158 if (status
!= LTTNG_OK
) {
1168 * Setup relayd connections for a tracing session. First creates the socket to
1169 * the relayd and send them to the right domain consumer. Consumer type MUST be
1172 int cmd_setup_relayd(struct ltt_session
*session
)
1175 struct ltt_ust_session
*usess
;
1176 struct ltt_kernel_session
*ksess
;
1177 struct consumer_socket
*socket
;
1178 struct lttng_ht_iter iter
;
1179 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1183 usess
= session
->ust_session
;
1184 ksess
= session
->kernel_session
;
1186 DBG("Setting relayd for session %s", session
->name
);
1189 if (session
->current_trace_chunk
) {
1190 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1191 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1193 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1194 current_chunk_id
.is_set
= true;
1196 ERR("Failed to get current trace chunk id");
1197 ret
= LTTNG_ERR_UNK
;
1202 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1203 && usess
->consumer
->enabled
) {
1204 /* For each consumer socket, send relayd sockets */
1205 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1206 socket
, node
.node
) {
1207 pthread_mutex_lock(socket
->lock
);
1208 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1209 usess
->consumer
, socket
,
1210 session
->name
, session
->hostname
,
1212 session
->live_timer
,
1213 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1214 session
->creation_time
,
1215 session
->name_contains_creation_time
);
1216 pthread_mutex_unlock(socket
->lock
);
1217 if (ret
!= LTTNG_OK
) {
1220 /* Session is now ready for network streaming. */
1221 session
->net_handle
= 1;
1223 session
->consumer
->relay_major_version
=
1224 usess
->consumer
->relay_major_version
;
1225 session
->consumer
->relay_minor_version
=
1226 usess
->consumer
->relay_minor_version
;
1227 session
->consumer
->relay_allows_clear
=
1228 usess
->consumer
->relay_allows_clear
;
1231 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1232 && ksess
->consumer
->enabled
) {
1233 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1234 socket
, node
.node
) {
1235 pthread_mutex_lock(socket
->lock
);
1236 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1237 ksess
->consumer
, socket
,
1238 session
->name
, session
->hostname
,
1240 session
->live_timer
,
1241 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1242 session
->creation_time
,
1243 session
->name_contains_creation_time
);
1244 pthread_mutex_unlock(socket
->lock
);
1245 if (ret
!= LTTNG_OK
) {
1248 /* Session is now ready for network streaming. */
1249 session
->net_handle
= 1;
1251 session
->consumer
->relay_major_version
=
1252 ksess
->consumer
->relay_major_version
;
1253 session
->consumer
->relay_minor_version
=
1254 ksess
->consumer
->relay_minor_version
;
1255 session
->consumer
->relay_allows_clear
=
1256 ksess
->consumer
->relay_allows_clear
;
1265 * Start a kernel session by opening all necessary streams.
1267 int start_kernel_session(struct ltt_kernel_session
*ksess
)
1270 struct ltt_kernel_channel
*kchan
;
1272 /* Open kernel metadata */
1273 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1274 ret
= kernel_open_metadata(ksess
);
1276 ret
= LTTNG_ERR_KERN_META_FAIL
;
1281 /* Open kernel metadata stream */
1282 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1283 ret
= kernel_open_metadata_stream(ksess
);
1285 ERR("Kernel create metadata stream failed");
1286 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1291 /* For each channel */
1292 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1293 if (kchan
->stream_count
== 0) {
1294 ret
= kernel_open_channel_stream(kchan
);
1296 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1299 /* Update the stream global counter */
1300 ksess
->stream_count_global
+= ret
;
1304 /* Setup kernel consumer socket and send fds to it */
1305 ret
= init_kernel_tracing(ksess
);
1307 ret
= LTTNG_ERR_KERN_START_FAIL
;
1311 /* This start the kernel tracing */
1312 ret
= kernel_start_session(ksess
);
1314 ret
= LTTNG_ERR_KERN_START_FAIL
;
1318 /* Quiescent wait after starting trace */
1319 kernel_wait_quiescent();
1329 int stop_kernel_session(struct ltt_kernel_session
*ksess
)
1331 struct ltt_kernel_channel
*kchan
;
1332 bool error_occurred
= false;
1335 if (!ksess
|| !ksess
->active
) {
1338 DBG("Stopping kernel tracing");
1340 ret
= kernel_stop_session(ksess
);
1342 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1346 kernel_wait_quiescent();
1348 /* Flush metadata after stopping (if exists) */
1349 if (ksess
->metadata_stream_fd
>= 0) {
1350 ret
= kernel_metadata_flush_buffer(ksess
->metadata_stream_fd
);
1352 ERR("Kernel metadata flush failed");
1353 error_occurred
= true;
1357 /* Flush all buffers after stopping */
1358 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1359 ret
= kernel_flush_buffer(kchan
);
1361 ERR("Kernel flush buffer error");
1362 error_occurred
= true;
1367 if (error_occurred
) {
1368 ret
= LTTNG_ERR_UNK
;
1377 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1379 int cmd_disable_channel(struct ltt_session
*session
,
1380 enum lttng_domain_type domain
, char *channel_name
)
1383 struct ltt_ust_session
*usess
;
1385 usess
= session
->ust_session
;
1390 case LTTNG_DOMAIN_KERNEL
:
1392 ret
= channel_kernel_disable(session
->kernel_session
,
1394 if (ret
!= LTTNG_OK
) {
1398 kernel_wait_quiescent();
1401 case LTTNG_DOMAIN_UST
:
1403 struct ltt_ust_channel
*uchan
;
1404 struct lttng_ht
*chan_ht
;
1406 chan_ht
= usess
->domain_global
.channels
;
1408 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1409 if (uchan
== NULL
) {
1410 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1414 ret
= channel_ust_disable(usess
, uchan
);
1415 if (ret
!= LTTNG_OK
) {
1421 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1433 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1435 * The wpipe arguments is used as a notifier for the kernel thread.
1437 int cmd_enable_channel(struct ltt_session
*session
,
1438 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1441 struct ltt_ust_session
*usess
= session
->ust_session
;
1442 struct lttng_ht
*chan_ht
;
1444 struct lttng_channel attr
;
1451 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1453 /* Validate channel name */
1454 if (attr
.name
[0] == '.' ||
1455 memchr(attr
.name
, '/', len
) != NULL
) {
1456 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1460 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1465 * Don't try to enable a channel if the session has been started at
1466 * some point in time before. The tracer does not allow it.
1468 if (session
->has_been_started
) {
1469 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1474 * If the session is a live session, remove the switch timer, the
1475 * live timer does the same thing but sends also synchronisation
1476 * beacons for inactive streams.
1478 if (session
->live_timer
> 0) {
1479 attr
.attr
.live_timer_interval
= session
->live_timer
;
1480 attr
.attr
.switch_timer_interval
= 0;
1483 /* Check for feature support */
1484 switch (domain
->type
) {
1485 case LTTNG_DOMAIN_KERNEL
:
1487 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1488 /* Sampling position of buffer is not supported */
1489 WARN("Kernel tracer does not support buffer monitoring. "
1490 "Setting the monitor interval timer to 0 "
1491 "(disabled) for channel '%s' of session '%s'",
1492 attr
.name
, session
->name
);
1493 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1497 case LTTNG_DOMAIN_UST
:
1499 case LTTNG_DOMAIN_JUL
:
1500 case LTTNG_DOMAIN_LOG4J
:
1501 case LTTNG_DOMAIN_PYTHON
:
1502 if (!agent_tracing_is_enabled()) {
1503 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1504 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1509 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1513 switch (domain
->type
) {
1514 case LTTNG_DOMAIN_KERNEL
:
1516 struct ltt_kernel_channel
*kchan
;
1518 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1519 session
->kernel_session
);
1520 if (kchan
== NULL
) {
1521 if (session
->snapshot
.nb_output
> 0 ||
1522 session
->snapshot_mode
) {
1523 /* Enforce mmap output for snapshot sessions. */
1524 attr
.attr
.output
= LTTNG_EVENT_MMAP
;
1526 ret
= channel_kernel_create(session
->kernel_session
, &attr
, wpipe
);
1527 if (attr
.name
[0] != '\0') {
1528 session
->kernel_session
->has_non_default_channel
= 1;
1531 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1534 if (ret
!= LTTNG_OK
) {
1538 kernel_wait_quiescent();
1541 case LTTNG_DOMAIN_UST
:
1542 case LTTNG_DOMAIN_JUL
:
1543 case LTTNG_DOMAIN_LOG4J
:
1544 case LTTNG_DOMAIN_PYTHON
:
1546 struct ltt_ust_channel
*uchan
;
1551 * Current agent implementation limitations force us to allow
1552 * only one channel at once in "agent" subdomains. Each
1553 * subdomain has a default channel name which must be strictly
1556 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1557 if (strncmp(attr
.name
, DEFAULT_JUL_CHANNEL_NAME
,
1558 LTTNG_SYMBOL_NAME_LEN
)) {
1559 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1562 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1563 if (strncmp(attr
.name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1564 LTTNG_SYMBOL_NAME_LEN
)) {
1565 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1568 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1569 if (strncmp(attr
.name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1570 LTTNG_SYMBOL_NAME_LEN
)) {
1571 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1576 chan_ht
= usess
->domain_global
.channels
;
1578 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1579 if (uchan
== NULL
) {
1580 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1581 if (attr
.name
[0] != '\0') {
1582 usess
->has_non_default_channel
= 1;
1585 ret
= channel_ust_enable(usess
, uchan
);
1590 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1594 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1595 session
->has_non_mmap_channel
= true;
1603 enum lttng_error_code
cmd_process_attr_tracker_get_tracking_policy(
1604 struct ltt_session
*session
,
1605 enum lttng_domain_type domain
,
1606 enum lttng_process_attr process_attr
,
1607 enum lttng_tracking_policy
*policy
)
1609 enum lttng_error_code ret_code
= LTTNG_OK
;
1610 const struct process_attr_tracker
*tracker
;
1613 case LTTNG_DOMAIN_KERNEL
:
1614 if (!session
->kernel_session
) {
1615 ret_code
= LTTNG_ERR_INVALID
;
1618 tracker
= kernel_get_process_attr_tracker(
1619 session
->kernel_session
, process_attr
);
1621 case LTTNG_DOMAIN_UST
:
1622 if (!session
->ust_session
) {
1623 ret_code
= LTTNG_ERR_INVALID
;
1626 tracker
= trace_ust_get_process_attr_tracker(
1627 session
->ust_session
, process_attr
);
1630 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1634 *policy
= process_attr_tracker_get_tracking_policy(tracker
);
1636 ret_code
= LTTNG_ERR_INVALID
;
1642 enum lttng_error_code
cmd_process_attr_tracker_set_tracking_policy(
1643 struct ltt_session
*session
,
1644 enum lttng_domain_type domain
,
1645 enum lttng_process_attr process_attr
,
1646 enum lttng_tracking_policy policy
)
1648 enum lttng_error_code ret_code
= LTTNG_OK
;
1651 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
1652 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
1653 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
1656 ret_code
= LTTNG_ERR_INVALID
;
1661 case LTTNG_DOMAIN_KERNEL
:
1662 if (!session
->kernel_session
) {
1663 ret_code
= LTTNG_ERR_INVALID
;
1666 ret_code
= kernel_process_attr_tracker_set_tracking_policy(
1667 session
->kernel_session
, process_attr
, policy
);
1669 case LTTNG_DOMAIN_UST
:
1670 if (!session
->ust_session
) {
1671 ret_code
= LTTNG_ERR_INVALID
;
1674 ret_code
= trace_ust_process_attr_tracker_set_tracking_policy(
1675 session
->ust_session
, process_attr
, policy
);
1678 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1685 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_add_value(
1686 struct ltt_session
*session
,
1687 enum lttng_domain_type domain
,
1688 enum lttng_process_attr process_attr
,
1689 const struct process_attr_value
*value
)
1691 enum lttng_error_code ret_code
= LTTNG_OK
;
1694 case LTTNG_DOMAIN_KERNEL
:
1695 if (!session
->kernel_session
) {
1696 ret_code
= LTTNG_ERR_INVALID
;
1699 ret_code
= kernel_process_attr_tracker_inclusion_set_add_value(
1700 session
->kernel_session
, process_attr
, value
);
1702 case LTTNG_DOMAIN_UST
:
1703 if (!session
->ust_session
) {
1704 ret_code
= LTTNG_ERR_INVALID
;
1707 ret_code
= trace_ust_process_attr_tracker_inclusion_set_add_value(
1708 session
->ust_session
, process_attr
, value
);
1711 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1718 enum lttng_error_code
cmd_process_attr_tracker_inclusion_set_remove_value(
1719 struct ltt_session
*session
,
1720 enum lttng_domain_type domain
,
1721 enum lttng_process_attr process_attr
,
1722 const struct process_attr_value
*value
)
1724 enum lttng_error_code ret_code
= LTTNG_OK
;
1727 case LTTNG_DOMAIN_KERNEL
:
1728 if (!session
->kernel_session
) {
1729 ret_code
= LTTNG_ERR_INVALID
;
1732 ret_code
= kernel_process_attr_tracker_inclusion_set_remove_value(
1733 session
->kernel_session
, process_attr
, value
);
1735 case LTTNG_DOMAIN_UST
:
1736 if (!session
->ust_session
) {
1737 ret_code
= LTTNG_ERR_INVALID
;
1740 ret_code
= trace_ust_process_attr_tracker_inclusion_set_remove_value(
1741 session
->ust_session
, process_attr
, value
);
1744 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1751 enum lttng_error_code
cmd_process_attr_tracker_get_inclusion_set(
1752 struct ltt_session
*session
,
1753 enum lttng_domain_type domain
,
1754 enum lttng_process_attr process_attr
,
1755 struct lttng_process_attr_values
**values
)
1757 enum lttng_error_code ret_code
= LTTNG_OK
;
1758 const struct process_attr_tracker
*tracker
;
1759 enum process_attr_tracker_status status
;
1762 case LTTNG_DOMAIN_KERNEL
:
1763 if (!session
->kernel_session
) {
1764 ret_code
= LTTNG_ERR_INVALID
;
1767 tracker
= kernel_get_process_attr_tracker(
1768 session
->kernel_session
, process_attr
);
1770 case LTTNG_DOMAIN_UST
:
1771 if (!session
->ust_session
) {
1772 ret_code
= LTTNG_ERR_INVALID
;
1775 tracker
= trace_ust_get_process_attr_tracker(
1776 session
->ust_session
, process_attr
);
1779 ret_code
= LTTNG_ERR_UNSUPPORTED_DOMAIN
;
1784 ret_code
= LTTNG_ERR_INVALID
;
1788 status
= process_attr_tracker_get_inclusion_set(tracker
, values
);
1790 case PROCESS_ATTR_TRACKER_STATUS_OK
:
1791 ret_code
= LTTNG_OK
;
1793 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1794 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1796 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1797 ret_code
= LTTNG_ERR_NOMEM
;
1800 ret_code
= LTTNG_ERR_UNK
;
1809 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1811 int cmd_disable_event(struct ltt_session
*session
,
1812 enum lttng_domain_type domain
, const char *channel_name
,
1813 const struct lttng_event
*event
)
1816 const char *event_name
;
1818 DBG("Disable event command for event \'%s\'", event
->name
);
1820 event_name
= event
->name
;
1822 /* Error out on unhandled search criteria */
1823 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1824 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1825 ret
= LTTNG_ERR_UNK
;
1832 case LTTNG_DOMAIN_KERNEL
:
1834 struct ltt_kernel_channel
*kchan
;
1835 struct ltt_kernel_session
*ksess
;
1837 ksess
= session
->kernel_session
;
1840 * If a non-default channel has been created in the
1841 * session, explicitely require that -c chan_name needs
1844 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1845 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1849 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1850 if (kchan
== NULL
) {
1851 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1855 switch (event
->type
) {
1856 case LTTNG_EVENT_ALL
:
1857 case LTTNG_EVENT_TRACEPOINT
:
1858 case LTTNG_EVENT_SYSCALL
:
1859 case LTTNG_EVENT_PROBE
:
1860 case LTTNG_EVENT_FUNCTION
:
1861 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1862 if (event_name
[0] == '\0') {
1863 ret
= event_kernel_disable_event(kchan
,
1866 ret
= event_kernel_disable_event(kchan
,
1867 event_name
, event
->type
);
1869 if (ret
!= LTTNG_OK
) {
1874 ret
= LTTNG_ERR_UNK
;
1878 kernel_wait_quiescent();
1881 case LTTNG_DOMAIN_UST
:
1883 struct ltt_ust_channel
*uchan
;
1884 struct ltt_ust_session
*usess
;
1886 usess
= session
->ust_session
;
1888 if (validate_ust_event_name(event_name
)) {
1889 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1894 * If a non-default channel has been created in the
1895 * session, explicitly require that -c chan_name needs
1898 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1899 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1903 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1905 if (uchan
== NULL
) {
1906 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1910 switch (event
->type
) {
1911 case LTTNG_EVENT_ALL
:
1913 * An empty event name means that everything
1914 * should be disabled.
1916 if (event
->name
[0] == '\0') {
1917 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1919 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1922 if (ret
!= LTTNG_OK
) {
1927 ret
= LTTNG_ERR_UNK
;
1931 DBG3("Disable UST event %s in channel %s completed", event_name
,
1935 case LTTNG_DOMAIN_LOG4J
:
1936 case LTTNG_DOMAIN_JUL
:
1937 case LTTNG_DOMAIN_PYTHON
:
1940 struct ltt_ust_session
*usess
= session
->ust_session
;
1944 switch (event
->type
) {
1945 case LTTNG_EVENT_ALL
:
1948 ret
= LTTNG_ERR_UNK
;
1952 agt
= trace_ust_find_agent(usess
, domain
);
1954 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1958 * An empty event name means that everything
1959 * should be disabled.
1961 if (event
->name
[0] == '\0') {
1962 ret
= event_agent_disable_all(usess
, agt
);
1964 ret
= event_agent_disable(usess
, agt
, event_name
);
1966 if (ret
!= LTTNG_OK
) {
1973 ret
= LTTNG_ERR_UND
;
1986 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1988 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1989 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1991 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1992 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1995 * Don't try to add a context if the session has been started at
1996 * some point in time before. The tracer does not allow it and would
1997 * result in a corrupted trace.
1999 if (session
->has_been_started
) {
2000 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2004 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
2005 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
2006 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
2010 case LTTNG_DOMAIN_KERNEL
:
2011 assert(session
->kernel_session
);
2013 if (session
->kernel_session
->channel_count
== 0) {
2014 /* Create default channel */
2015 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
2016 if (ret
!= LTTNG_OK
) {
2019 chan_kern_created
= 1;
2021 /* Add kernel context to kernel tracer */
2022 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
2023 if (ret
!= LTTNG_OK
) {
2027 case LTTNG_DOMAIN_JUL
:
2028 case LTTNG_DOMAIN_LOG4J
:
2031 * Validate channel name.
2032 * If no channel name is given and the domain is JUL or LOG4J,
2033 * set it to the appropriate domain-specific channel name. If
2034 * a name is provided but does not match the expexted channel
2035 * name, return an error.
2037 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
2038 strcmp(channel_name
,
2039 DEFAULT_JUL_CHANNEL_NAME
)) {
2040 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2042 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
2043 strcmp(channel_name
,
2044 DEFAULT_LOG4J_CHANNEL_NAME
)) {
2045 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2048 /* break is _not_ missing here. */
2050 case LTTNG_DOMAIN_UST
:
2052 struct ltt_ust_session
*usess
= session
->ust_session
;
2053 unsigned int chan_count
;
2057 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
2058 if (chan_count
== 0) {
2059 struct lttng_channel
*attr
;
2060 /* Create default channel */
2061 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
2063 ret
= LTTNG_ERR_FATAL
;
2067 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
2068 if (ret
!= LTTNG_OK
) {
2072 channel_attr_destroy(attr
);
2073 chan_ust_created
= 1;
2076 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
2077 free(app_ctx_provider_name
);
2079 app_ctx_name
= NULL
;
2080 app_ctx_provider_name
= NULL
;
2081 if (ret
!= LTTNG_OK
) {
2087 ret
= LTTNG_ERR_UND
;
2095 if (chan_kern_created
) {
2096 struct ltt_kernel_channel
*kchan
=
2097 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
2098 session
->kernel_session
);
2099 /* Created previously, this should NOT fail. */
2101 kernel_destroy_channel(kchan
);
2104 if (chan_ust_created
) {
2105 struct ltt_ust_channel
*uchan
=
2106 trace_ust_find_channel_by_name(
2107 session
->ust_session
->domain_global
.channels
,
2108 DEFAULT_CHANNEL_NAME
);
2109 /* Created previously, this should NOT fail. */
2111 /* Remove from the channel list of the session. */
2112 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
2114 trace_ust_destroy_channel(uchan
);
2117 free(app_ctx_provider_name
);
2122 static inline bool name_starts_with(const char *name
, const char *prefix
)
2124 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
2126 return !strncmp(name
, prefix
, max_cmp_len
);
2129 /* Perform userspace-specific event name validation */
2130 static int validate_ust_event_name(const char *name
)
2140 * Check name against all internal UST event component namespaces used
2143 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
2144 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
2145 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2154 * Internal version of cmd_enable_event() with a supplemental
2155 * "internal_event" flag which is used to enable internal events which should
2156 * be hidden from clients. Such events are used in the agent implementation to
2157 * enable the events through which all "agent" events are funeled.
2159 static int _cmd_enable_event(struct ltt_session
*session
,
2160 const struct lttng_domain
*domain
,
2161 char *channel_name
, struct lttng_event
*event
,
2162 char *filter_expression
,
2163 struct lttng_filter_bytecode
*filter
,
2164 struct lttng_event_exclusion
*exclusion
,
2165 int wpipe
, bool internal_event
)
2167 int ret
= 0, channel_created
= 0;
2168 struct lttng_channel
*attr
= NULL
;
2172 assert(channel_name
);
2174 /* If we have a filter, we must have its filter expression */
2175 assert(!(!!filter_expression
^ !!filter
));
2177 /* Normalize event name as a globbing pattern */
2178 strutils_normalize_star_glob_pattern(event
->name
);
2180 /* Normalize exclusion names as globbing patterns */
2184 for (i
= 0; i
< exclusion
->count
; i
++) {
2185 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2187 strutils_normalize_star_glob_pattern(name
);
2191 DBG("Enable event command for event \'%s\'", event
->name
);
2195 switch (domain
->type
) {
2196 case LTTNG_DOMAIN_KERNEL
:
2198 struct ltt_kernel_channel
*kchan
;
2201 * If a non-default channel has been created in the
2202 * session, explicitely require that -c chan_name needs
2205 if (session
->kernel_session
->has_non_default_channel
2206 && channel_name
[0] == '\0') {
2207 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2211 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2212 session
->kernel_session
);
2213 if (kchan
== NULL
) {
2214 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2215 LTTNG_BUFFER_GLOBAL
);
2217 ret
= LTTNG_ERR_FATAL
;
2220 if (lttng_strncpy(attr
->name
, channel_name
,
2221 sizeof(attr
->name
))) {
2222 ret
= LTTNG_ERR_INVALID
;
2226 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2227 if (ret
!= LTTNG_OK
) {
2230 channel_created
= 1;
2233 /* Get the newly created kernel channel pointer */
2234 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2235 session
->kernel_session
);
2236 if (kchan
== NULL
) {
2237 /* This sould not happen... */
2238 ret
= LTTNG_ERR_FATAL
;
2242 switch (event
->type
) {
2243 case LTTNG_EVENT_ALL
:
2245 char *filter_expression_a
= NULL
;
2246 struct lttng_filter_bytecode
*filter_a
= NULL
;
2249 * We need to duplicate filter_expression and filter,
2250 * because ownership is passed to first enable
2253 if (filter_expression
) {
2254 filter_expression_a
= strdup(filter_expression
);
2255 if (!filter_expression_a
) {
2256 ret
= LTTNG_ERR_FATAL
;
2261 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2263 free(filter_expression_a
);
2264 ret
= LTTNG_ERR_FATAL
;
2267 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2269 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2270 ret
= event_kernel_enable_event(kchan
, event
,
2271 filter_expression
, filter
);
2272 /* We have passed ownership */
2273 filter_expression
= NULL
;
2275 if (ret
!= LTTNG_OK
) {
2276 if (channel_created
) {
2277 /* Let's not leak a useless channel. */
2278 kernel_destroy_channel(kchan
);
2280 free(filter_expression_a
);
2284 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2285 ret
= event_kernel_enable_event(kchan
, event
,
2286 filter_expression_a
, filter_a
);
2287 /* We have passed ownership */
2288 filter_expression_a
= NULL
;
2290 if (ret
!= LTTNG_OK
) {
2295 case LTTNG_EVENT_PROBE
:
2296 case LTTNG_EVENT_USERSPACE_PROBE
:
2297 case LTTNG_EVENT_FUNCTION
:
2298 case LTTNG_EVENT_FUNCTION_ENTRY
:
2299 case LTTNG_EVENT_TRACEPOINT
:
2300 ret
= event_kernel_enable_event(kchan
, event
,
2301 filter_expression
, filter
);
2302 /* We have passed ownership */
2303 filter_expression
= NULL
;
2305 if (ret
!= LTTNG_OK
) {
2306 if (channel_created
) {
2307 /* Let's not leak a useless channel. */
2308 kernel_destroy_channel(kchan
);
2313 case LTTNG_EVENT_SYSCALL
:
2314 ret
= event_kernel_enable_event(kchan
, event
,
2315 filter_expression
, filter
);
2316 /* We have passed ownership */
2317 filter_expression
= NULL
;
2319 if (ret
!= LTTNG_OK
) {
2324 ret
= LTTNG_ERR_UNK
;
2328 kernel_wait_quiescent();
2331 case LTTNG_DOMAIN_UST
:
2333 struct ltt_ust_channel
*uchan
;
2334 struct ltt_ust_session
*usess
= session
->ust_session
;
2339 * If a non-default channel has been created in the
2340 * session, explicitely require that -c chan_name needs
2343 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2344 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2348 /* Get channel from global UST domain */
2349 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2351 if (uchan
== NULL
) {
2352 /* Create default channel */
2353 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2354 usess
->buffer_type
);
2356 ret
= LTTNG_ERR_FATAL
;
2359 if (lttng_strncpy(attr
->name
, channel_name
,
2360 sizeof(attr
->name
))) {
2361 ret
= LTTNG_ERR_INVALID
;
2365 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2366 if (ret
!= LTTNG_OK
) {
2370 /* Get the newly created channel reference back */
2371 uchan
= trace_ust_find_channel_by_name(
2372 usess
->domain_global
.channels
, channel_name
);
2376 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2378 * Don't allow users to add UST events to channels which
2379 * are assigned to a userspace subdomain (JUL, Log4J,
2382 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2386 if (!internal_event
) {
2388 * Ensure the event name is not reserved for internal
2391 ret
= validate_ust_event_name(event
->name
);
2393 WARN("Userspace event name %s failed validation.",
2395 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2400 /* At this point, the session and channel exist on the tracer */
2401 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2402 filter_expression
, filter
, exclusion
,
2404 /* We have passed ownership */
2405 filter_expression
= NULL
;
2408 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2409 goto already_enabled
;
2410 } else if (ret
!= LTTNG_OK
) {
2415 case LTTNG_DOMAIN_LOG4J
:
2416 case LTTNG_DOMAIN_JUL
:
2417 case LTTNG_DOMAIN_PYTHON
:
2419 const char *default_event_name
, *default_chan_name
;
2421 struct lttng_event uevent
;
2422 struct lttng_domain tmp_dom
;
2423 struct ltt_ust_session
*usess
= session
->ust_session
;
2427 if (!agent_tracing_is_enabled()) {
2428 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2429 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2433 agt
= trace_ust_find_agent(usess
, domain
->type
);
2435 agt
= agent_create(domain
->type
);
2437 ret
= LTTNG_ERR_NOMEM
;
2440 agent_add(agt
, usess
->agents
);
2443 /* Create the default tracepoint. */
2444 memset(&uevent
, 0, sizeof(uevent
));
2445 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2446 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2447 default_event_name
= event_get_default_agent_ust_name(
2449 if (!default_event_name
) {
2450 ret
= LTTNG_ERR_FATAL
;
2453 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2454 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2457 * The domain type is changed because we are about to enable the
2458 * default channel and event for the JUL domain that are hardcoded.
2459 * This happens in the UST domain.
2461 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2462 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2464 switch (domain
->type
) {
2465 case LTTNG_DOMAIN_LOG4J
:
2466 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2468 case LTTNG_DOMAIN_JUL
:
2469 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2471 case LTTNG_DOMAIN_PYTHON
:
2472 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2475 /* The switch/case we are in makes this impossible */
2480 char *filter_expression_copy
= NULL
;
2481 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2484 const size_t filter_size
= sizeof(
2485 struct lttng_filter_bytecode
)
2488 filter_copy
= zmalloc(filter_size
);
2490 ret
= LTTNG_ERR_NOMEM
;
2493 memcpy(filter_copy
, filter
, filter_size
);
2495 filter_expression_copy
=
2496 strdup(filter_expression
);
2497 if (!filter_expression
) {
2498 ret
= LTTNG_ERR_NOMEM
;
2501 if (!filter_expression_copy
|| !filter_copy
) {
2502 free(filter_expression_copy
);
2508 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2509 (char *) default_chan_name
,
2510 &uevent
, filter_expression_copy
,
2511 filter_copy
, NULL
, wpipe
);
2514 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2515 goto already_enabled
;
2516 } else if (ret
!= LTTNG_OK
) {
2520 /* The wild card * means that everything should be enabled. */
2521 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2522 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2525 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2529 filter_expression
= NULL
;
2530 if (ret
!= LTTNG_OK
) {
2537 ret
= LTTNG_ERR_UND
;
2545 free(filter_expression
);
2548 channel_attr_destroy(attr
);
2554 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2555 * We own filter, exclusion, and filter_expression.
2557 int cmd_enable_event(struct ltt_session
*session
,
2558 const struct lttng_domain
*domain
,
2559 char *channel_name
, struct lttng_event
*event
,
2560 char *filter_expression
,
2561 struct lttng_filter_bytecode
*filter
,
2562 struct lttng_event_exclusion
*exclusion
,
2565 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2566 filter_expression
, filter
, exclusion
, wpipe
, false);
2570 * Enable an event which is internal to LTTng. An internal should
2571 * never be made visible to clients and are immune to checks such as
2574 static int cmd_enable_event_internal(struct ltt_session
*session
,
2575 const struct lttng_domain
*domain
,
2576 char *channel_name
, struct lttng_event
*event
,
2577 char *filter_expression
,
2578 struct lttng_filter_bytecode
*filter
,
2579 struct lttng_event_exclusion
*exclusion
,
2582 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2583 filter_expression
, filter
, exclusion
, wpipe
, true);
2587 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2589 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2590 struct lttng_event
**events
)
2593 ssize_t nb_events
= 0;
2596 case LTTNG_DOMAIN_KERNEL
:
2597 nb_events
= kernel_list_events(events
);
2598 if (nb_events
< 0) {
2599 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2603 case LTTNG_DOMAIN_UST
:
2604 nb_events
= ust_app_list_events(events
);
2605 if (nb_events
< 0) {
2606 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2610 case LTTNG_DOMAIN_LOG4J
:
2611 case LTTNG_DOMAIN_JUL
:
2612 case LTTNG_DOMAIN_PYTHON
:
2613 nb_events
= agent_list_events(events
, domain
);
2614 if (nb_events
< 0) {
2615 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2620 ret
= LTTNG_ERR_UND
;
2627 /* Return negative value to differentiate return code */
2632 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2634 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2635 struct lttng_event_field
**fields
)
2638 ssize_t nb_fields
= 0;
2641 case LTTNG_DOMAIN_UST
:
2642 nb_fields
= ust_app_list_event_fields(fields
);
2643 if (nb_fields
< 0) {
2644 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2648 case LTTNG_DOMAIN_KERNEL
:
2649 default: /* fall-through */
2650 ret
= LTTNG_ERR_UND
;
2657 /* Return negative value to differentiate return code */
2661 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2663 return syscall_table_list(events
);
2667 * Command LTTNG_START_TRACE processed by the client thread.
2669 * Called with session mutex held.
2671 int cmd_start_trace(struct ltt_session
*session
)
2673 enum lttng_error_code ret
;
2674 unsigned long nb_chan
= 0;
2675 struct ltt_kernel_session
*ksession
;
2676 struct ltt_ust_session
*usess
;
2677 const bool session_rotated_after_last_stop
=
2678 session
->rotated_after_last_stop
;
2679 const bool session_cleared_after_last_stop
=
2680 session
->cleared_after_last_stop
;
2684 /* Ease our life a bit ;) */
2685 ksession
= session
->kernel_session
;
2686 usess
= session
->ust_session
;
2688 /* Is the session already started? */
2689 if (session
->active
) {
2690 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2691 /* Perform nothing */
2695 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2696 !session
->current_trace_chunk
) {
2698 * A rotation was launched while the session was stopped and
2699 * it has not been completed yet. It is not possible to start
2700 * the session since starting the session here would require a
2701 * rotation from "NULL" to a new trace chunk. That rotation
2702 * would overlap with the ongoing rotation, which is not
2705 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2707 ret
= LTTNG_ERR_ROTATION_PENDING
;
2712 * Starting a session without channel is useless since after that it's not
2713 * possible to enable channel thus inform the client.
2715 if (usess
&& usess
->domain_global
.channels
) {
2716 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2719 nb_chan
+= ksession
->channel_count
;
2722 ret
= LTTNG_ERR_NO_CHANNEL
;
2726 session
->active
= 1;
2727 session
->rotated_after_last_stop
= false;
2728 session
->cleared_after_last_stop
= false;
2729 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2730 if (!session
->has_been_started
) {
2731 struct lttng_trace_chunk
*trace_chunk
;
2733 DBG("Creating initial trace chunk of session \"%s\"",
2735 trace_chunk
= session_create_new_trace_chunk(
2736 session
, NULL
, NULL
, NULL
);
2738 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2741 assert(!session
->current_trace_chunk
);
2742 ret
= session_set_trace_chunk(session
, trace_chunk
,
2744 lttng_trace_chunk_put(trace_chunk
);
2746 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2750 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2753 * Rotate existing streams into the new chunk.
2754 * This is a "quiet" rotation has no client has
2755 * explicitly requested this operation.
2757 * There is also no need to wait for the rotation
2758 * to complete as it will happen immediately. No data
2759 * was produced as the session was stopped, so the
2760 * rotation should happen on reception of the command.
2762 ret
= cmd_rotate_session(session
, NULL
, true,
2763 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
2764 if (ret
!= LTTNG_OK
) {
2770 /* Kernel tracing */
2771 if (ksession
!= NULL
) {
2772 DBG("Start kernel tracing session %s", session
->name
);
2773 ret
= start_kernel_session(ksession
);
2774 if (ret
!= LTTNG_OK
) {
2779 /* Flag session that trace should start automatically */
2781 int int_ret
= ust_app_start_trace_all(usess
);
2784 ret
= LTTNG_ERR_UST_START_FAIL
;
2790 * Open a packet in every stream of the session to ensure that viewers
2791 * can correctly identify the boundaries of the periods during which
2792 * tracing was active for this session.
2794 ret
= session_open_packets(session
);
2795 if (ret
!= LTTNG_OK
) {
2800 * Clear the flag that indicates that a rotation was done while the
2801 * session was stopped.
2803 session
->rotated_after_last_stop
= false;
2805 if (session
->rotate_timer_period
) {
2806 int int_ret
= timer_session_rotation_schedule_timer_start(
2807 session
, session
->rotate_timer_period
);
2810 ERR("Failed to enable rotate timer");
2811 ret
= LTTNG_ERR_UNK
;
2819 if (ret
== LTTNG_OK
) {
2820 /* Flag this after a successful start. */
2821 session
->has_been_started
|= 1;
2823 session
->active
= 0;
2824 /* Restore initial state on error. */
2825 session
->rotated_after_last_stop
=
2826 session_rotated_after_last_stop
;
2827 session
->cleared_after_last_stop
=
2828 session_cleared_after_last_stop
;
2835 * Command LTTNG_STOP_TRACE processed by the client thread.
2837 int cmd_stop_trace(struct ltt_session
*session
)
2840 struct ltt_kernel_session
*ksession
;
2841 struct ltt_ust_session
*usess
;
2845 DBG("Begin stop session \"%s\" (id %" PRIu64
")", session
->name
, session
->id
);
2847 ksession
= session
->kernel_session
;
2848 usess
= session
->ust_session
;
2850 /* Session is not active. Skip everythong and inform the client. */
2851 if (!session
->active
) {
2852 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2856 ret
= stop_kernel_session(ksession
);
2857 if (ret
!= LTTNG_OK
) {
2861 if (usess
&& usess
->active
) {
2862 ret
= ust_app_stop_trace_all(usess
);
2864 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2869 DBG("Completed stop session \"%s\" (id %" PRIu64
")", session
->name
,
2871 /* Flag inactive after a successful stop. */
2872 session
->active
= 0;
2880 * Set the base_path of the session only if subdir of a control uris is set.
2881 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2883 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2885 struct lttng_uri
*uris
)
2890 for (i
= 0; i
< nb_uri
; i
++) {
2891 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2892 uris
[i
].subdir
[0] == '\0') {
2893 /* Not interested in these URIs */
2897 if (session
->base_path
!= NULL
) {
2898 free(session
->base_path
);
2899 session
->base_path
= NULL
;
2902 /* Set session base_path */
2903 session
->base_path
= strdup(uris
[i
].subdir
);
2904 if (!session
->base_path
) {
2905 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2906 uris
[i
].subdir
, session
->name
);
2907 ret
= LTTNG_ERR_NOMEM
;
2910 DBG2("Setting base path \"%s\" for session \"%s\"",
2911 session
->base_path
, session
->name
);
2919 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2921 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2922 struct lttng_uri
*uris
)
2925 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2926 struct ltt_ust_session
*usess
= session
->ust_session
;
2932 /* Can't set consumer URI if the session is active. */
2933 if (session
->active
) {
2934 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2939 * Set the session base path if any. This is done inside
2940 * cmd_set_consumer_uri to preserve backward compatibility of the
2941 * previous session creation api vs the session descriptor api.
2943 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2944 if (ret
!= LTTNG_OK
) {
2948 /* Set the "global" consumer URIs */
2949 for (i
= 0; i
< nb_uri
; i
++) {
2950 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2952 if (ret
!= LTTNG_OK
) {
2957 /* Set UST session URIs */
2958 if (session
->ust_session
) {
2959 for (i
= 0; i
< nb_uri
; i
++) {
2960 ret
= add_uri_to_consumer(session
,
2961 session
->ust_session
->consumer
,
2962 &uris
[i
], LTTNG_DOMAIN_UST
);
2963 if (ret
!= LTTNG_OK
) {
2969 /* Set kernel session URIs */
2970 if (session
->kernel_session
) {
2971 for (i
= 0; i
< nb_uri
; i
++) {
2972 ret
= add_uri_to_consumer(session
,
2973 session
->kernel_session
->consumer
,
2974 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2975 if (ret
!= LTTNG_OK
) {
2982 * Make sure to set the session in output mode after we set URI since a
2983 * session can be created without URL (thus flagged in no output mode).
2985 session
->output_traces
= 1;
2987 ksess
->output_traces
= 1;
2991 usess
->output_traces
= 1;
3002 enum lttng_error_code
set_session_output_from_descriptor(
3003 struct ltt_session
*session
,
3004 const struct lttng_session_descriptor
*descriptor
)
3007 enum lttng_error_code ret_code
= LTTNG_OK
;
3008 enum lttng_session_descriptor_type session_type
=
3009 lttng_session_descriptor_get_type(descriptor
);
3010 enum lttng_session_descriptor_output_type output_type
=
3011 lttng_session_descriptor_get_output_type(descriptor
);
3012 struct lttng_uri uris
[2] = {};
3013 size_t uri_count
= 0;
3015 switch (output_type
) {
3016 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
3018 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
3019 lttng_session_descriptor_get_local_output_uri(descriptor
,
3023 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
3024 lttng_session_descriptor_get_network_output_uris(descriptor
,
3025 &uris
[0], &uris
[1]);
3029 ret_code
= LTTNG_ERR_INVALID
;
3033 switch (session_type
) {
3034 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3036 struct snapshot_output
*new_output
= NULL
;
3038 new_output
= snapshot_output_alloc();
3040 ret_code
= LTTNG_ERR_NOMEM
;
3044 ret
= snapshot_output_init_with_uri(session
,
3045 DEFAULT_SNAPSHOT_MAX_SIZE
,
3046 NULL
, uris
, uri_count
, session
->consumer
,
3047 new_output
, &session
->snapshot
);
3049 ret_code
= (ret
== -ENOMEM
) ?
3050 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
3051 snapshot_output_destroy(new_output
);
3054 snapshot_add_output(&session
->snapshot
, new_output
);
3057 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
3058 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3060 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
3064 ret_code
= LTTNG_ERR_INVALID
;
3072 enum lttng_error_code
cmd_create_session_from_descriptor(
3073 struct lttng_session_descriptor
*descriptor
,
3074 const lttng_sock_cred
*creds
,
3075 const char *home_path
)
3078 enum lttng_error_code ret_code
;
3079 const char *session_name
;
3080 struct ltt_session
*new_session
= NULL
;
3081 enum lttng_session_descriptor_status descriptor_status
;
3083 session_lock_list();
3085 if (*home_path
!= '/') {
3086 ERR("Home path provided by client is not absolute");
3087 ret_code
= LTTNG_ERR_INVALID
;
3092 descriptor_status
= lttng_session_descriptor_get_session_name(
3093 descriptor
, &session_name
);
3094 switch (descriptor_status
) {
3095 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3097 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3098 session_name
= NULL
;
3101 ret_code
= LTTNG_ERR_INVALID
;
3105 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3107 if (ret_code
!= LTTNG_OK
) {
3111 if (!session_name
) {
3112 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3115 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3120 if (!lttng_session_descriptor_is_output_destination_initialized(
3123 * Only include the session's creation time in the output
3124 * destination if the name of the session itself was
3125 * not auto-generated.
3127 ret_code
= lttng_session_descriptor_set_default_output(
3129 session_name
? &new_session
->creation_time
: NULL
,
3131 if (ret_code
!= LTTNG_OK
) {
3135 new_session
->has_user_specified_directory
=
3136 lttng_session_descriptor_has_output_directory(
3140 switch (lttng_session_descriptor_get_type(descriptor
)) {
3141 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3142 new_session
->snapshot_mode
= 1;
3144 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3145 new_session
->live_timer
=
3146 lttng_session_descriptor_live_get_timer_interval(
3153 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3154 if (ret_code
!= LTTNG_OK
) {
3157 new_session
->consumer
->enabled
= 1;
3158 ret_code
= LTTNG_OK
;
3160 /* Release reference provided by the session_create function. */
3161 session_put(new_session
);
3162 if (ret_code
!= LTTNG_OK
&& new_session
) {
3163 /* Release the global reference on error. */
3164 session_destroy(new_session
);
3166 session_unlock_list();
3170 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3171 struct lttng_session_descriptor
**return_descriptor
)
3174 size_t payload_size
;
3175 struct lttng_dynamic_buffer payload
;
3176 struct lttng_buffer_view home_dir_view
;
3177 struct lttng_buffer_view session_descriptor_view
;
3178 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3179 enum lttng_error_code ret_code
;
3181 lttng_dynamic_buffer_init(&payload
);
3182 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
3184 ret_code
= LTTNG_ERR_INVALID
;
3187 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
3188 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3189 ret_code
= LTTNG_ERR_INVALID
;
3193 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
3194 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
3195 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3197 ret_code
= LTTNG_ERR_NOMEM
;
3201 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3203 ERR("Reception of session descriptor failed, aborting.");
3204 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3208 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3211 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3212 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3214 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3215 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3217 ret
= lttng_session_descriptor_create_from_buffer(
3218 &session_descriptor_view
, &session_descriptor
);
3220 ERR("Failed to create session descriptor from payload of \"create session\" command");
3221 ret_code
= LTTNG_ERR_INVALID
;
3226 * Sets the descriptor's auto-generated properties (name, output) if
3229 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3231 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3232 if (ret_code
!= LTTNG_OK
) {
3236 ret_code
= LTTNG_OK
;
3237 *return_descriptor
= session_descriptor
;
3238 session_descriptor
= NULL
;
3240 lttng_dynamic_buffer_reset(&payload
);
3241 lttng_session_descriptor_destroy(session_descriptor
);
3246 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3247 void *_reply_context
)
3251 const struct cmd_destroy_session_reply_context
*reply_context
=
3253 struct lttng_dynamic_buffer payload
;
3254 struct lttcomm_session_destroy_command_header cmd_header
;
3255 struct lttng_trace_archive_location
*location
= NULL
;
3256 struct lttcomm_lttng_msg llm
= {
3257 .cmd_type
= LTTNG_DESTROY_SESSION
,
3258 .ret_code
= reply_context
->destruction_status
,
3261 sizeof(struct lttcomm_session_destroy_command_header
),
3264 size_t payload_size_before_location
;
3266 lttng_dynamic_buffer_init(&payload
);
3268 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3270 ERR("Failed to append session destruction message");
3274 cmd_header
.rotation_state
=
3275 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3276 session
->rotation_state
:
3277 LTTNG_ROTATION_STATE_NO_ROTATION
);
3278 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3279 sizeof(cmd_header
));
3281 ERR("Failed to append session destruction command header");
3285 if (!reply_context
->implicit_rotation_on_destroy
) {
3286 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3290 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3291 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3296 location
= session_get_trace_archive_location(session
);
3298 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3303 payload_size_before_location
= payload
.size
;
3304 comm_ret
= lttng_trace_archive_location_serialize(location
,
3307 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3311 /* Update the message to indicate the location's length. */
3312 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3313 payload
.size
- payload_size_before_location
;
3315 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3316 payload
.data
, payload
.size
);
3317 if (comm_ret
!= (ssize_t
) payload
.size
) {
3318 ERR("Failed to send result of the destruction of session \"%s\" to client",
3322 ret
= close(reply_context
->reply_sock_fd
);
3324 PERROR("Failed to close client socket in deferred session destroy reply");
3326 lttng_dynamic_buffer_reset(&payload
);
3327 free(_reply_context
);
3331 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3333 * Called with session lock held.
3335 int cmd_destroy_session(struct ltt_session
*session
,
3336 struct notification_thread_handle
*notification_thread_handle
,
3340 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3341 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3344 reply_context
= zmalloc(sizeof(*reply_context
));
3345 if (!reply_context
) {
3346 ret
= LTTNG_ERR_NOMEM
;
3349 reply_context
->reply_sock_fd
= *sock_fd
;
3355 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3357 if (session
->active
) {
3358 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3360 ret
= cmd_stop_trace(session
);
3361 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3362 /* Carry on with the destruction of the session. */
3363 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3364 session
->name
, lttng_strerror(-ret
));
3365 destruction_last_error
= ret
;
3369 if (session
->rotation_schedule_timer_enabled
) {
3370 if (timer_session_rotation_schedule_timer_stop(
3372 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3374 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3378 if (session
->rotate_size
) {
3379 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3380 session
->rotate_size
= 0;
3383 if (session
->rotated
&& session
->current_trace_chunk
&& session
->output_traces
) {
3385 * Perform a last rotation on destruction if rotations have
3386 * occurred during the session's lifetime.
3388 ret
= cmd_rotate_session(session
, NULL
, false,
3389 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
);
3390 if (ret
!= LTTNG_OK
) {
3391 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3392 session
->name
, lttng_strerror(-ret
));
3393 destruction_last_error
= -ret
;
3395 if (reply_context
) {
3396 reply_context
->implicit_rotation_on_destroy
= true;
3398 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3400 * The user has not triggered a session rotation. However, to
3401 * ensure all data has been consumed, the session is rotated
3402 * to a 'null' trace chunk before it is destroyed.
3404 * This is a "quiet" rotation meaning that no notification is
3405 * emitted and no renaming of the current trace chunk takes
3408 ret
= cmd_rotate_session(session
, NULL
, true,
3409 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
);
3411 * Rotation operations may not be supported by the kernel
3412 * tracer. Hence, do not consider this implicit rotation as
3413 * a session destruction error. The library has already stopped
3414 * the session and waited for pending data; there is nothing
3415 * left to do but complete the destruction of the session.
3417 if (ret
!= LTTNG_OK
&&
3418 ret
!= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
) {
3419 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3420 session
->name
, lttng_strerror(ret
));
3421 destruction_last_error
= -ret
;
3425 if (session
->shm_path
[0]) {
3427 * When a session is created with an explicit shm_path,
3428 * the consumer daemon will create its shared memory files
3429 * at that location and will *not* unlink them. This is normal
3430 * as the intention of that feature is to make it possible
3431 * to retrieve the content of those files should a crash occur.
3433 * To ensure the content of those files can be used, the
3434 * sessiond daemon will replicate the content of the metadata
3435 * cache in a metadata file.
3437 * On clean-up, it is expected that the consumer daemon will
3438 * unlink the shared memory files and that the session daemon
3439 * will unlink the metadata file. Then, the session's directory
3440 * in the shm path can be removed.
3442 * Unfortunately, a flaw in the design of the sessiond's and
3443 * consumerd's tear down of channels makes it impossible to
3444 * determine when the sessiond _and_ the consumerd have both
3445 * destroyed their representation of a channel. For one, the
3446 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3447 * callbacks in both daemons.
3449 * However, it is also impossible for the sessiond to know when
3450 * the consumer daemon is done destroying its channel(s) since
3451 * it occurs as a reaction to the closing of the channel's file
3452 * descriptor. There is no resulting communication initiated
3453 * from the consumerd to the sessiond to confirm that the
3454 * operation is completed (and was successful).
3456 * Until this is all fixed, the session daemon checks for the
3457 * removal of the session's shm path which makes it possible
3458 * to safely advertise a session as having been destroyed.
3460 * Prior to this fix, it was not possible to reliably save
3461 * a session making use of the --shm-path option, destroy it,
3462 * and load it again. This is because the creation of the
3463 * session would fail upon seeing the session's shm path
3464 * already in existence.
3466 * Note that none of the error paths in the check for the
3467 * directory's existence return an error. This is normal
3468 * as there isn't much that can be done. The session will
3469 * be destroyed properly, except that we can't offer the
3470 * guarantee that the same session can be re-created.
3472 current_completion_handler
= &destroy_completion_handler
.handler
;
3473 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3475 sizeof(destroy_completion_handler
.shm_path
));
3480 * The session is destroyed. However, note that the command context
3481 * still holds a reference to the session, thus delaying its destruction
3482 * _at least_ up to the point when that reference is released.
3484 session_destroy(session
);
3485 if (reply_context
) {
3486 reply_context
->destruction_status
= destruction_last_error
;
3487 ret
= session_add_destroy_notifier(session
,
3488 cmd_destroy_session_reply
,
3489 (void *) reply_context
);
3491 ret
= LTTNG_ERR_FATAL
;
3503 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3505 int cmd_register_consumer(struct ltt_session
*session
,
3506 enum lttng_domain_type domain
, const char *sock_path
,
3507 struct consumer_data
*cdata
)
3510 struct consumer_socket
*socket
= NULL
;
3517 case LTTNG_DOMAIN_KERNEL
:
3519 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3523 /* Can't register a consumer if there is already one */
3524 if (ksess
->consumer_fds_sent
!= 0) {
3525 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3529 sock
= lttcomm_connect_unix_sock(sock_path
);
3531 ret
= LTTNG_ERR_CONNECT_FAIL
;
3534 cdata
->cmd_sock
= sock
;
3536 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3537 if (socket
== NULL
) {
3540 PERROR("close register consumer");
3542 cdata
->cmd_sock
= -1;
3543 ret
= LTTNG_ERR_FATAL
;
3547 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3548 if (socket
->lock
== NULL
) {
3549 PERROR("zmalloc pthread mutex");
3550 ret
= LTTNG_ERR_FATAL
;
3553 pthread_mutex_init(socket
->lock
, NULL
);
3554 socket
->registered
= 1;
3557 consumer_add_socket(socket
, ksess
->consumer
);
3560 pthread_mutex_lock(&cdata
->pid_mutex
);
3562 pthread_mutex_unlock(&cdata
->pid_mutex
);
3567 /* TODO: Userspace tracing */
3568 ret
= LTTNG_ERR_UND
;
3576 consumer_destroy_socket(socket
);
3582 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3584 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3585 struct lttng_domain
**domains
)
3590 struct lttng_ht_iter iter
;
3592 if (session
->kernel_session
!= NULL
) {
3593 DBG3("Listing domains found kernel domain");
3597 if (session
->ust_session
!= NULL
) {
3598 DBG3("Listing domains found UST global domain");
3602 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3604 if (agt
->being_used
) {
3615 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3616 if (*domains
== NULL
) {
3617 ret
= LTTNG_ERR_FATAL
;
3621 if (session
->kernel_session
!= NULL
) {
3622 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3624 /* Kernel session buffer type is always GLOBAL */
3625 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3630 if (session
->ust_session
!= NULL
) {
3631 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3632 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3636 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3638 if (agt
->being_used
) {
3639 (*domains
)[index
].type
= agt
->domain
;
3640 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3650 /* Return negative value to differentiate return code */
3656 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3658 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3659 struct ltt_session
*session
, struct lttng_channel
**channels
)
3661 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3664 case LTTNG_DOMAIN_KERNEL
:
3665 if (session
->kernel_session
!= NULL
) {
3666 nb_chan
= session
->kernel_session
->channel_count
;
3668 DBG3("Number of kernel channels %zd", nb_chan
);
3670 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3674 case LTTNG_DOMAIN_UST
:
3675 if (session
->ust_session
!= NULL
) {
3677 nb_chan
= lttng_ht_get_count(
3678 session
->ust_session
->domain_global
.channels
);
3681 DBG3("Number of UST global channels %zd", nb_chan
);
3683 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3688 ret
= -LTTNG_ERR_UND
;
3693 const size_t channel_size
= sizeof(struct lttng_channel
) +
3694 sizeof(struct lttng_channel_extended
);
3695 struct lttng_channel_extended
*channel_exts
;
3697 payload_size
= nb_chan
* channel_size
;
3698 *channels
= zmalloc(payload_size
);
3699 if (*channels
== NULL
) {
3700 ret
= -LTTNG_ERR_FATAL
;
3704 channel_exts
= ((void *) *channels
) +
3705 (nb_chan
* sizeof(struct lttng_channel
));
3706 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3707 if (ret
!= LTTNG_OK
) {
3722 * Command LTTNG_LIST_EVENTS processed by the client thread.
3724 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3725 struct ltt_session
*session
, char *channel_name
,
3726 struct lttng_event
**events
, size_t *total_size
)
3729 ssize_t nb_event
= 0;
3732 case LTTNG_DOMAIN_KERNEL
:
3733 if (session
->kernel_session
!= NULL
) {
3734 nb_event
= list_lttng_kernel_events(channel_name
,
3735 session
->kernel_session
, events
,
3739 case LTTNG_DOMAIN_UST
:
3741 if (session
->ust_session
!= NULL
) {
3742 nb_event
= list_lttng_ust_global_events(channel_name
,
3743 &session
->ust_session
->domain_global
, events
,
3748 case LTTNG_DOMAIN_LOG4J
:
3749 case LTTNG_DOMAIN_JUL
:
3750 case LTTNG_DOMAIN_PYTHON
:
3751 if (session
->ust_session
) {
3752 struct lttng_ht_iter iter
;
3756 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3757 &iter
.iter
, agt
, node
.node
) {
3758 if (agt
->domain
== domain
) {
3759 nb_event
= list_lttng_agent_events(
3769 ret
= LTTNG_ERR_UND
;
3776 /* Return negative value to differentiate return code */
3781 * Using the session list, filled a lttng_session array to send back to the
3782 * client for session listing.
3784 * The session list lock MUST be acquired before calling this function. Use
3785 * session_lock_list() and session_unlock_list().
3787 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3788 size_t session_count
, uid_t uid
, gid_t gid
)
3792 struct ltt_session
*session
;
3793 struct ltt_session_list
*list
= session_get_list();
3794 struct lttng_session_extended
*extended
=
3795 (typeof(extended
)) (&sessions
[session_count
]);
3797 DBG("Getting all available session for UID %d GID %d",
3800 * Iterate over session list and append data after the control struct in
3803 cds_list_for_each_entry(session
, &list
->head
, list
) {
3804 if (!session_get(session
)) {
3808 * Only list the sessions the user can control.
3810 if (!session_access_ok(session
, uid
, gid
) ||
3811 session
->destroyed
) {
3812 session_put(session
);
3816 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3817 struct ltt_ust_session
*usess
= session
->ust_session
;
3819 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3820 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3821 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3822 ret
= build_network_session_path(sessions
[i
].path
,
3823 sizeof(sessions
[i
].path
), session
);
3825 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3826 session
->consumer
->dst
.session_root_path
);
3829 PERROR("snprintf session path");
3830 session_put(session
);
3834 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3835 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3836 sessions
[i
].enabled
= session
->active
;
3837 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3838 sessions
[i
].live_timer_interval
= session
->live_timer
;
3839 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3840 extended
[i
].creation_time
.is_set
= 1;
3842 session_put(session
);
3847 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3848 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3850 int cmd_data_pending(struct ltt_session
*session
)
3853 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3854 struct ltt_ust_session
*usess
= session
->ust_session
;
3858 DBG("Data pending for session %s", session
->name
);
3860 /* Session MUST be stopped to ask for data availability. */
3861 if (session
->active
) {
3862 ret
= LTTNG_ERR_SESSION_STARTED
;
3866 * If stopped, just make sure we've started before else the above call
3867 * will always send that there is data pending.
3869 * The consumer assumes that when the data pending command is received,
3870 * the trace has been started before or else no output data is written
3871 * by the streams which is a condition for data pending. So, this is
3872 * *VERY* important that we don't ask the consumer before a start
3875 if (!session
->has_been_started
) {
3881 /* A rotation is still pending, we have to wait. */
3882 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3883 DBG("Rotate still pending for session %s", session
->name
);
3888 if (ksess
&& ksess
->consumer
) {
3889 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3891 /* Data is still being extracted for the kernel. */
3896 if (usess
&& usess
->consumer
) {
3897 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3899 /* Data is still being extracted for the kernel. */
3904 /* Data is ready to be read by a viewer */
3912 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3914 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3916 int cmd_snapshot_add_output(struct ltt_session
*session
,
3917 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3920 struct snapshot_output
*new_output
;
3925 DBG("Cmd snapshot add output for session %s", session
->name
);
3928 * Can't create an output if the session is not set in no-output mode.
3930 if (session
->output_traces
) {
3931 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3935 if (session
->has_non_mmap_channel
) {
3936 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3940 /* Only one output is allowed until we have the "tee" feature. */
3941 if (session
->snapshot
.nb_output
== 1) {
3942 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3946 new_output
= snapshot_output_alloc();
3948 ret
= LTTNG_ERR_NOMEM
;
3952 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3953 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3954 &session
->snapshot
);
3956 if (ret
== -ENOMEM
) {
3957 ret
= LTTNG_ERR_NOMEM
;
3959 ret
= LTTNG_ERR_INVALID
;
3965 snapshot_add_output(&session
->snapshot
, new_output
);
3967 *id
= new_output
->id
;
3974 snapshot_output_destroy(new_output
);
3980 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3982 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3984 int cmd_snapshot_del_output(struct ltt_session
*session
,
3985 const struct lttng_snapshot_output
*output
)
3988 struct snapshot_output
*sout
= NULL
;
3996 * Permission denied to create an output if the session is not
3997 * set in no output mode.
3999 if (session
->output_traces
) {
4000 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4005 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
4007 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
4008 } else if (*output
->name
!= '\0') {
4009 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
4011 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
4014 ret
= LTTNG_ERR_INVALID
;
4018 snapshot_delete_output(&session
->snapshot
, sout
);
4019 snapshot_output_destroy(sout
);
4028 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4030 * If no output is available, outputs is untouched and 0 is returned.
4032 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4034 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
4035 struct lttng_snapshot_output
**outputs
)
4038 struct lttng_snapshot_output
*list
= NULL
;
4039 struct lttng_ht_iter iter
;
4040 struct snapshot_output
*output
;
4045 DBG("Cmd snapshot list outputs for session %s", session
->name
);
4048 * Permission denied to create an output if the session is not
4049 * set in no output mode.
4051 if (session
->output_traces
) {
4052 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4056 if (session
->snapshot
.nb_output
== 0) {
4061 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
4063 ret
= -LTTNG_ERR_NOMEM
;
4067 /* Copy list from session to the new list object. */
4069 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
4070 output
, node
.node
) {
4071 assert(output
->consumer
);
4072 list
[idx
].id
= output
->id
;
4073 list
[idx
].max_size
= output
->max_size
;
4074 if (lttng_strncpy(list
[idx
].name
, output
->name
,
4075 sizeof(list
[idx
].name
))) {
4076 ret
= -LTTNG_ERR_INVALID
;
4079 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
4080 if (lttng_strncpy(list
[idx
].ctrl_url
,
4081 output
->consumer
->dst
.session_root_path
,
4082 sizeof(list
[idx
].ctrl_url
))) {
4083 ret
= -LTTNG_ERR_INVALID
;
4088 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
4089 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
4091 ret
= -LTTNG_ERR_NOMEM
;
4096 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4097 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4099 ret
= -LTTNG_ERR_NOMEM
;
4108 ret
= session
->snapshot
.nb_output
;
4117 * Check if we can regenerate the metadata for this session.
4118 * Only kernel, UST per-uid and non-live sessions are supported.
4120 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4123 int check_regenerate_metadata_support(struct ltt_session
*session
)
4129 if (session
->live_timer
!= 0) {
4130 ret
= LTTNG_ERR_LIVE_SESSION
;
4133 if (!session
->active
) {
4134 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4137 if (session
->ust_session
) {
4138 switch (session
->ust_session
->buffer_type
) {
4139 case LTTNG_BUFFER_PER_UID
:
4141 case LTTNG_BUFFER_PER_PID
:
4142 ret
= LTTNG_ERR_PER_PID_SESSION
;
4146 ret
= LTTNG_ERR_UNK
;
4150 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4151 session
->consumer
->relay_minor_version
< 8) {
4152 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4162 int clear_metadata_file(int fd
)
4167 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4168 if (lseek_ret
< 0) {
4174 ret
= ftruncate(fd
, 0);
4176 PERROR("ftruncate");
4185 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4188 struct buffer_reg_uid
*uid_reg
= NULL
;
4189 struct buffer_reg_session
*session_reg
= NULL
;
4192 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4193 struct ust_registry_session
*registry
;
4194 struct ust_registry_channel
*chan
;
4195 struct lttng_ht_iter iter_chan
;
4197 session_reg
= uid_reg
->registry
;
4198 registry
= session_reg
->reg
.ust
;
4200 pthread_mutex_lock(®istry
->lock
);
4201 registry
->metadata_len_sent
= 0;
4202 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4203 registry
->metadata_len
= 0;
4204 registry
->metadata_version
++;
4205 if (registry
->metadata_fd
> 0) {
4206 /* Clear the metadata file's content. */
4207 ret
= clear_metadata_file(registry
->metadata_fd
);
4209 pthread_mutex_unlock(®istry
->lock
);
4214 ret
= ust_metadata_session_statedump(registry
, NULL
,
4215 registry
->major
, registry
->minor
);
4217 pthread_mutex_unlock(®istry
->lock
);
4218 ERR("Failed to generate session metadata (err = %d)",
4222 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4224 struct ust_registry_event
*event
;
4225 struct lttng_ht_iter iter_event
;
4227 ret
= ust_metadata_channel_statedump(registry
, chan
);
4229 pthread_mutex_unlock(®istry
->lock
);
4230 ERR("Failed to generate channel metadata "
4234 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4236 ret
= ust_metadata_event_statedump(registry
,
4239 pthread_mutex_unlock(®istry
->lock
);
4240 ERR("Failed to generate event metadata "
4246 pthread_mutex_unlock(®istry
->lock
);
4255 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4257 * Ask the consumer to truncate the existing metadata file(s) and
4258 * then regenerate the metadata. Live and per-pid sessions are not
4259 * supported and return an error.
4261 * Return 0 on success or else a LTTNG_ERR code.
4263 int cmd_regenerate_metadata(struct ltt_session
*session
)
4269 ret
= check_regenerate_metadata_support(session
);
4274 if (session
->kernel_session
) {
4275 ret
= kernctl_session_regenerate_metadata(
4276 session
->kernel_session
->fd
);
4278 ERR("Failed to regenerate the kernel metadata");
4283 if (session
->ust_session
) {
4284 ret
= ust_regenerate_metadata(session
->ust_session
);
4286 ERR("Failed to regenerate the UST metadata");
4290 DBG("Cmd metadata regenerate for session %s", session
->name
);
4298 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4300 * Ask the tracer to regenerate a new statedump.
4302 * Return 0 on success or else a LTTNG_ERR code.
4304 int cmd_regenerate_statedump(struct ltt_session
*session
)
4310 if (!session
->active
) {
4311 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4315 if (session
->kernel_session
) {
4316 ret
= kernctl_session_regenerate_statedump(
4317 session
->kernel_session
->fd
);
4319 * Currently, the statedump in kernel can only fail if out
4323 if (ret
== -ENOMEM
) {
4324 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4326 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4328 ERR("Failed to regenerate the kernel statedump");
4333 if (session
->ust_session
) {
4334 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4336 * Currently, the statedump in UST always returns 0.
4339 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4340 ERR("Failed to regenerate the UST statedump");
4344 DBG("Cmd regenerate statedump for session %s", session
->name
);
4351 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4352 struct notification_thread_handle
*notification_thread
)
4356 ssize_t sock_recv_len
;
4357 struct lttng_trigger
*trigger
= NULL
;
4358 struct lttng_buffer_view view
;
4359 struct lttng_dynamic_buffer trigger_buffer
;
4361 lttng_dynamic_buffer_init(&trigger_buffer
);
4362 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4363 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4365 ret
= LTTNG_ERR_NOMEM
;
4369 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4371 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4372 ERR("Failed to receive \"register trigger\" command payload");
4373 /* TODO: should this be a new error enum ? */
4374 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4378 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4379 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4381 ERR("Invalid trigger payload received in \"register trigger\" command");
4382 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4386 ret
= notification_thread_command_register_trigger(notification_thread
,
4388 /* Ownership of trigger was transferred. */
4391 lttng_trigger_destroy(trigger
);
4392 lttng_dynamic_buffer_reset(&trigger_buffer
);
4396 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4397 struct notification_thread_handle
*notification_thread
)
4401 ssize_t sock_recv_len
;
4402 struct lttng_trigger
*trigger
= NULL
;
4403 struct lttng_buffer_view view
;
4404 struct lttng_dynamic_buffer trigger_buffer
;
4406 lttng_dynamic_buffer_init(&trigger_buffer
);
4407 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4408 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4410 ret
= LTTNG_ERR_NOMEM
;
4414 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4416 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4417 ERR("Failed to receive \"unregister trigger\" command payload");
4418 /* TODO: should this be a new error enum ? */
4419 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4423 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4424 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4426 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4427 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4431 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4434 lttng_trigger_destroy(trigger
);
4435 lttng_dynamic_buffer_reset(&trigger_buffer
);
4440 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4441 * snapshot output is *not* set with a remote destination.
4443 * Return LTTNG_OK on success or a LTTNG_ERR code.
4445 static enum lttng_error_code
set_relayd_for_snapshot(
4446 struct consumer_output
*output
,
4447 const struct ltt_session
*session
)
4449 enum lttng_error_code status
= LTTNG_OK
;
4450 struct lttng_ht_iter iter
;
4451 struct consumer_socket
*socket
;
4452 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4453 const char *base_path
;
4458 DBG2("Set relayd object from snapshot output");
4460 if (session
->current_trace_chunk
) {
4461 enum lttng_trace_chunk_status chunk_status
=
4462 lttng_trace_chunk_get_id(
4463 session
->current_trace_chunk
,
4464 ¤t_chunk_id
.value
);
4466 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4467 current_chunk_id
.is_set
= true;
4469 ERR("Failed to get current trace chunk id");
4470 status
= LTTNG_ERR_UNK
;
4475 /* Ignore if snapshot consumer output is not network. */
4476 if (output
->type
!= CONSUMER_DST_NET
) {
4481 * The snapshot record URI base path overrides the session
4484 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4485 base_path
= output
->dst
.net
.control
.subdir
;
4487 base_path
= session
->base_path
;
4491 * For each consumer socket, create and send the relayd object of the
4495 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4496 socket
, node
.node
) {
4497 pthread_mutex_lock(socket
->lock
);
4498 status
= send_consumer_relayd_sockets(0, session
->id
,
4500 session
->name
, session
->hostname
,
4502 session
->live_timer
,
4503 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4504 session
->creation_time
,
4505 session
->name_contains_creation_time
);
4506 pthread_mutex_unlock(socket
->lock
);
4507 if (status
!= LTTNG_OK
) {
4519 * Record a kernel snapshot.
4521 * Return LTTNG_OK on success or a LTTNG_ERR code.
4523 static enum lttng_error_code
record_kernel_snapshot(
4524 struct ltt_kernel_session
*ksess
,
4525 const struct consumer_output
*output
,
4526 const struct ltt_session
*session
,
4527 int wait
, uint64_t nb_packets_per_stream
)
4529 enum lttng_error_code status
;
4535 status
= kernel_snapshot_record(
4536 ksess
, output
, wait
, nb_packets_per_stream
);
4541 * Record a UST snapshot.
4543 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4545 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4546 const struct consumer_output
*output
,
4547 const struct ltt_session
*session
,
4548 int wait
, uint64_t nb_packets_per_stream
)
4550 enum lttng_error_code status
;
4556 status
= ust_app_snapshot_record(
4557 usess
, output
, wait
, nb_packets_per_stream
);
4562 uint64_t get_session_size_one_more_packet_per_stream(
4563 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4565 uint64_t tot_size
= 0;
4567 if (session
->kernel_session
) {
4568 struct ltt_kernel_channel
*chan
;
4569 const struct ltt_kernel_session
*ksess
=
4570 session
->kernel_session
;
4572 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4573 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4575 * Don't take channel into account if we
4576 * already grab all its packets.
4580 tot_size
+= chan
->channel
->attr
.subbuf_size
4581 * chan
->stream_count
;
4585 if (session
->ust_session
) {
4586 const struct ltt_ust_session
*usess
= session
->ust_session
;
4588 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4596 * Calculate the number of packets we can grab from each stream that
4597 * fits within the overall snapshot max size.
4599 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4600 * the number of packets per stream.
4602 * TODO: this approach is not perfect: we consider the worse case
4603 * (packet filling the sub-buffers) as an upper bound, but we could do
4604 * better if we do this calculation while we actually grab the packet
4605 * content: we would know how much padding we don't actually store into
4608 * This algorithm is currently bounded by the number of packets per
4611 * Since we call this algorithm before actually grabbing the data, it's
4612 * an approximation: for instance, applications could appear/disappear
4613 * in between this call and actually grabbing data.
4616 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4620 uint64_t cur_nb_packets
= 0;
4623 return 0; /* Infinite */
4626 size_left
= max_size
;
4628 uint64_t one_more_packet_tot_size
;
4630 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4631 session
, cur_nb_packets
);
4632 if (!one_more_packet_tot_size
) {
4633 /* We are already grabbing all packets. */
4636 size_left
-= one_more_packet_tot_size
;
4637 if (size_left
< 0) {
4642 if (!cur_nb_packets
&& size_left
!= max_size
) {
4643 /* Not enough room to grab one packet of each stream, error. */
4646 return cur_nb_packets
;
4650 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4651 const struct snapshot_output
*snapshot_output
, int wait
)
4653 int64_t nb_packets_per_stream
;
4654 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4656 enum lttng_error_code ret_code
= LTTNG_OK
;
4657 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4658 struct consumer_output
*original_ust_consumer_output
= NULL
;
4659 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4660 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4661 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4663 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4665 snapshot_output
->name
,
4666 snapshot_output
->datetime
,
4667 snapshot_output
->nb_snapshot
);
4668 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4669 ERR("Failed to format snapshot name");
4670 ret_code
= LTTNG_ERR_INVALID
;
4673 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4674 snapshot_output
->name
, session
->name
,
4675 snapshot_chunk_name
);
4676 if (!session
->kernel_session
&& !session
->ust_session
) {
4677 ERR("Failed to record snapshot as no channels exist");
4678 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4682 if (session
->kernel_session
) {
4683 original_kernel_consumer_output
=
4684 session
->kernel_session
->consumer
;
4685 snapshot_kernel_consumer_output
=
4686 consumer_copy_output(snapshot_output
->consumer
);
4687 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
4688 snapshot_chunk_name
);
4690 /* Copy the original domain subdir. */
4691 strcpy(snapshot_kernel_consumer_output
->domain_subdir
,
4692 original_kernel_consumer_output
->domain_subdir
);
4694 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4695 original_kernel_consumer_output
);
4697 ERR("Failed to copy consumer sockets from snapshot output configuration");
4698 ret_code
= LTTNG_ERR_NOMEM
;
4701 ret_code
= set_relayd_for_snapshot(
4702 snapshot_kernel_consumer_output
, session
);
4703 if (ret_code
!= LTTNG_OK
) {
4704 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4707 session
->kernel_session
->consumer
=
4708 snapshot_kernel_consumer_output
;
4710 if (session
->ust_session
) {
4711 original_ust_consumer_output
= session
->ust_session
->consumer
;
4712 snapshot_ust_consumer_output
=
4713 consumer_copy_output(snapshot_output
->consumer
);
4714 strcpy(snapshot_ust_consumer_output
->chunk_path
,
4715 snapshot_chunk_name
);
4717 /* Copy the original domain subdir. */
4718 strcpy(snapshot_ust_consumer_output
->domain_subdir
,
4719 original_ust_consumer_output
->domain_subdir
);
4721 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4722 original_ust_consumer_output
);
4724 ERR("Failed to copy consumer sockets from snapshot output configuration");
4725 ret_code
= LTTNG_ERR_NOMEM
;
4728 ret_code
= set_relayd_for_snapshot(
4729 snapshot_ust_consumer_output
, session
);
4730 if (ret_code
!= LTTNG_OK
) {
4731 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4734 session
->ust_session
->consumer
=
4735 snapshot_ust_consumer_output
;
4738 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4739 snapshot_kernel_consumer_output
?:
4740 snapshot_ust_consumer_output
,
4741 consumer_output_get_base_path(
4742 snapshot_output
->consumer
),
4743 snapshot_chunk_name
);
4744 if (!snapshot_trace_chunk
) {
4745 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4747 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4750 assert(!session
->current_trace_chunk
);
4751 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4752 lttng_trace_chunk_put(snapshot_trace_chunk
);
4753 snapshot_trace_chunk
= NULL
;
4755 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4757 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4761 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4762 snapshot_output
->max_size
);
4763 if (nb_packets_per_stream
< 0) {
4764 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4765 goto error_close_trace_chunk
;
4768 if (session
->kernel_session
) {
4769 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4770 snapshot_kernel_consumer_output
, session
,
4771 wait
, nb_packets_per_stream
);
4772 if (ret_code
!= LTTNG_OK
) {
4773 goto error_close_trace_chunk
;
4777 if (session
->ust_session
) {
4778 ret_code
= record_ust_snapshot(session
->ust_session
,
4779 snapshot_ust_consumer_output
, session
,
4780 wait
, nb_packets_per_stream
);
4781 if (ret_code
!= LTTNG_OK
) {
4782 goto error_close_trace_chunk
;
4786 error_close_trace_chunk
:
4787 if (session_set_trace_chunk(session
, NULL
, &snapshot_trace_chunk
)) {
4788 ERR("Failed to release the current trace chunk of session \"%s\"",
4790 ret_code
= LTTNG_ERR_UNK
;
4793 if (session_close_trace_chunk(session
, snapshot_trace_chunk
,
4794 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION
, NULL
)) {
4796 * Don't goto end; make sure the chunk is closed for the session
4797 * to allow future snapshots.
4799 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4801 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4804 if (original_ust_consumer_output
) {
4805 session
->ust_session
->consumer
= original_ust_consumer_output
;
4807 if (original_kernel_consumer_output
) {
4808 session
->kernel_session
->consumer
=
4809 original_kernel_consumer_output
;
4811 consumer_output_put(snapshot_ust_consumer_output
);
4812 consumer_output_put(snapshot_kernel_consumer_output
);
4817 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4819 * The wait parameter is ignored so this call always wait for the snapshot to
4820 * complete before returning.
4822 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4824 int cmd_snapshot_record(struct ltt_session
*session
,
4825 const struct lttng_snapshot_output
*output
, int wait
)
4827 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4829 unsigned int snapshot_success
= 0;
4831 struct snapshot_output
*tmp_output
= NULL
;
4836 DBG("Cmd snapshot record for session %s", session
->name
);
4838 /* Get the datetime for the snapshot output directory. */
4839 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4842 cmd_ret
= LTTNG_ERR_INVALID
;
4847 * Permission denied to create an output if the session is not
4848 * set in no output mode.
4850 if (session
->output_traces
) {
4851 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4855 /* The session needs to be started at least once. */
4856 if (!session
->has_been_started
) {
4857 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4861 /* Use temporary output for the session. */
4862 if (*output
->ctrl_url
!= '\0') {
4863 tmp_output
= snapshot_output_alloc();
4865 cmd_ret
= LTTNG_ERR_NOMEM
;
4869 ret
= snapshot_output_init(session
, output
->max_size
,
4871 output
->ctrl_url
, output
->data_url
,
4875 if (ret
== -ENOMEM
) {
4876 cmd_ret
= LTTNG_ERR_NOMEM
;
4878 cmd_ret
= LTTNG_ERR_INVALID
;
4882 /* Use the global session count for the temporary snapshot. */
4883 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
4885 /* Use the global datetime */
4886 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
4887 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
4888 if (cmd_ret
!= LTTNG_OK
) {
4891 snapshot_success
= 1;
4893 struct snapshot_output
*sout
;
4894 struct lttng_ht_iter iter
;
4897 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4898 &iter
.iter
, sout
, node
.node
) {
4899 struct snapshot_output output_copy
;
4902 * Make a local copy of the output and override output
4903 * parameters with those provided as part of the
4906 memcpy(&output_copy
, sout
, sizeof(output_copy
));
4908 if (output
->max_size
!= (uint64_t) -1ULL) {
4909 output_copy
.max_size
= output
->max_size
;
4912 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4913 memcpy(output_copy
.datetime
, datetime
,
4916 /* Use temporary name. */
4917 if (*output
->name
!= '\0') {
4918 if (lttng_strncpy(output_copy
.name
,
4920 sizeof(output_copy
.name
))) {
4921 cmd_ret
= LTTNG_ERR_INVALID
;
4927 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
4928 if (cmd_ret
!= LTTNG_OK
) {
4932 snapshot_success
= 1;
4937 if (snapshot_success
) {
4938 session
->snapshot
.nb_snapshot
++;
4940 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4945 snapshot_output_destroy(tmp_output
);
4951 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4953 int cmd_set_session_shm_path(struct ltt_session
*session
,
4954 const char *shm_path
)
4960 * Can only set shm path before session is started.
4962 if (session
->has_been_started
) {
4963 return LTTNG_ERR_SESSION_STARTED
;
4966 strncpy(session
->shm_path
, shm_path
,
4967 sizeof(session
->shm_path
));
4968 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4974 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4976 * Ask the consumer to rotate the session output directory.
4977 * The session lock must be held.
4979 * Returns LTTNG_OK on success or else a negative LTTng error code.
4981 int cmd_rotate_session(struct ltt_session
*session
,
4982 struct lttng_rotate_session_return
*rotate_return
,
4983 bool quiet_rotation
,
4984 enum lttng_trace_chunk_command_type command
)
4987 uint64_t ongoing_rotation_chunk_id
;
4988 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4989 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
4990 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
4991 enum lttng_trace_chunk_status chunk_status
;
4992 bool failed_to_rotate
= false;
4993 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
4997 if (!session
->has_been_started
) {
4998 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
5003 * Explicit rotation is not supported for live sessions.
5004 * However, live sessions can perform a quiet rotation on
5006 * Rotation is not supported for snapshot traces (no output).
5008 if ((!quiet_rotation
&& session
->live_timer
) ||
5009 !session
->output_traces
) {
5010 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5014 /* Unsupported feature in lttng-relayd before 2.11. */
5015 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
5016 (session
->consumer
->relay_major_version
== 2 &&
5017 session
->consumer
->relay_minor_version
< 11)) {
5018 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
5022 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5023 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
5024 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
5028 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
5029 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5031 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
5036 * After a stop, we only allow one rotation to occur, the other ones are
5037 * useless until a new start.
5039 if (session
->rotated_after_last_stop
) {
5040 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5042 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
5047 * After a stop followed by a clear, disallow following rotations a they would
5048 * generate empty chunks.
5050 if (session
->cleared_after_last_stop
) {
5051 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5053 cmd_ret
= LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR
;
5057 if (session
->active
) {
5058 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
5060 if (!new_trace_chunk
) {
5061 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
5067 * The current trace chunk becomes the chunk being archived.
5069 * After this point, "chunk_being_archived" must absolutely
5070 * be closed on the consumer(s), otherwise it will never be
5071 * cleaned-up, which will result in a leak.
5073 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
5074 &chunk_being_archived
);
5076 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
5080 if (session
->kernel_session
) {
5081 cmd_ret
= kernel_rotate_session(session
);
5082 if (cmd_ret
!= LTTNG_OK
) {
5083 failed_to_rotate
= true;
5084 rotation_fail_code
= cmd_ret
;
5087 if (session
->ust_session
) {
5088 cmd_ret
= ust_app_rotate_session(session
);
5089 if (cmd_ret
!= LTTNG_OK
) {
5090 failed_to_rotate
= true;
5091 rotation_fail_code
= cmd_ret
;
5095 if (!session
->active
) {
5096 session
->rotated_after_last_stop
= true;
5099 if (!chunk_being_archived
) {
5100 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5102 if (failed_to_rotate
) {
5103 cmd_ret
= rotation_fail_code
;
5110 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
5111 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
5112 &ongoing_rotation_chunk_id
);
5113 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5115 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
5116 command
, session
->last_chunk_path
);
5118 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5122 if (failed_to_rotate
) {
5123 cmd_ret
= rotation_fail_code
;
5127 session
->quiet_rotation
= quiet_rotation
;
5128 ret
= timer_session_rotation_pending_check_start(session
,
5129 DEFAULT_ROTATE_PENDING_TIMER
);
5131 cmd_ret
= LTTNG_ERR_UNK
;
5135 if (rotate_return
) {
5136 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5139 session
->chunk_being_archived
= chunk_being_archived
;
5140 chunk_being_archived
= NULL
;
5141 if (!quiet_rotation
) {
5142 ret
= notification_thread_command_session_rotation_ongoing(
5143 notification_thread_handle
,
5144 session
->name
, session
->uid
, session
->gid
,
5145 ongoing_rotation_chunk_id
);
5146 if (ret
!= LTTNG_OK
) {
5147 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5153 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5154 session
->name
, ongoing_rotation_chunk_id
);
5156 lttng_trace_chunk_put(new_trace_chunk
);
5157 lttng_trace_chunk_put(chunk_being_archived
);
5158 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5161 if (session_reset_rotation_state(session
,
5162 LTTNG_ROTATION_STATE_ERROR
)) {
5163 ERR("Failed to reset rotation state of session \"%s\"",
5170 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5172 * Check if the session has finished its rotation.
5174 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5176 int cmd_rotate_get_info(struct ltt_session
*session
,
5177 struct lttng_rotation_get_info_return
*info_return
,
5178 uint64_t rotation_id
)
5180 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5181 enum lttng_rotation_state rotation_state
;
5183 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5184 session
->most_recent_chunk_id
.value
);
5186 if (session
->chunk_being_archived
) {
5187 enum lttng_trace_chunk_status chunk_status
;
5190 chunk_status
= lttng_trace_chunk_get_id(
5191 session
->chunk_being_archived
,
5193 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5195 rotation_state
= rotation_id
== chunk_id
?
5196 LTTNG_ROTATION_STATE_ONGOING
:
5197 LTTNG_ROTATION_STATE_EXPIRED
;
5199 if (session
->last_archived_chunk_id
.is_set
&&
5200 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5201 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5203 rotation_state
= session
->rotation_state
;
5207 switch (rotation_state
) {
5208 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5209 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5212 case LTTNG_ROTATION_STATE_EXPIRED
:
5213 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5214 rotation_id
, session
->name
);
5216 case LTTNG_ROTATION_STATE_ONGOING
:
5217 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5218 rotation_id
, session
->name
);
5220 case LTTNG_ROTATION_STATE_COMPLETED
:
5224 char *current_tracing_path_reply
;
5225 size_t current_tracing_path_reply_len
;
5227 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5228 rotation_id
, session
->name
);
5230 switch (session_get_consumer_destination_type(session
)) {
5231 case CONSUMER_DST_LOCAL
:
5232 current_tracing_path_reply
=
5233 info_return
->location
.local
.absolute_path
;
5234 current_tracing_path_reply_len
=
5235 sizeof(info_return
->location
.local
.absolute_path
);
5236 info_return
->location_type
=
5237 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5238 fmt_ret
= asprintf(&chunk_path
,
5239 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5240 session_get_base_path(session
),
5241 session
->last_archived_chunk_name
);
5242 if (fmt_ret
== -1) {
5243 PERROR("Failed to format the path of the last archived trace chunk");
5244 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5245 cmd_ret
= LTTNG_ERR_UNK
;
5249 case CONSUMER_DST_NET
:
5251 uint16_t ctrl_port
, data_port
;
5253 current_tracing_path_reply
=
5254 info_return
->location
.relay
.relative_path
;
5255 current_tracing_path_reply_len
=
5256 sizeof(info_return
->location
.relay
.relative_path
);
5257 /* Currently the only supported relay protocol. */
5258 info_return
->location
.relay
.protocol
=
5259 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5261 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5262 session_get_net_consumer_hostname(session
),
5263 sizeof(info_return
->location
.relay
.host
));
5265 ERR("Failed to copy host name to rotate_get_info reply");
5266 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5267 cmd_ret
= LTTNG_ERR_SET_URL
;
5271 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5272 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5273 info_return
->location
.relay
.ports
.data
= data_port
;
5274 info_return
->location_type
=
5275 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5276 chunk_path
= strdup(session
->last_chunk_path
);
5278 ERR("Failed to allocate the path of the last archived trace chunk");
5279 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5280 cmd_ret
= LTTNG_ERR_UNK
;
5289 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5290 chunk_path
, current_tracing_path_reply_len
);
5293 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5294 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5295 cmd_ret
= LTTNG_ERR_UNK
;
5301 case LTTNG_ROTATION_STATE_ERROR
:
5302 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5303 rotation_id
, session
->name
);
5311 info_return
->status
= (int32_t) rotation_state
;
5316 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5318 * Configure the automatic rotation parameters.
5319 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5320 * 'activate' to false means deactivate the rotation schedule and validate that
5321 * 'new_value' has the same value as the currently active value.
5323 * Return 0 on success or else a positive LTTNG_ERR code.
5325 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5326 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5328 struct notification_thread_handle
*notification_thread_handle
)
5331 uint64_t *parameter_value
;
5335 DBG("Cmd rotate set schedule session %s", session
->name
);
5337 if (session
->live_timer
|| !session
->output_traces
) {
5338 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5339 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5343 switch (schedule_type
) {
5344 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5345 parameter_value
= &session
->rotate_size
;
5347 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5348 parameter_value
= &session
->rotate_timer_period
;
5349 if (new_value
>= UINT_MAX
) {
5350 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5351 new_value
, UINT_MAX
);
5352 ret
= LTTNG_ERR_INVALID
;
5357 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5358 ret
= LTTNG_ERR_INVALID
;
5362 /* Improper use of the API. */
5363 if (new_value
== -1ULL) {
5364 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5365 ret
= LTTNG_ERR_INVALID
;
5370 * As indicated in struct ltt_session's comments, a value of == 0 means
5371 * this schedule rotation type is not in use.
5373 * Reject the command if we were asked to activate a schedule that was
5376 if (activate
&& *parameter_value
!= 0) {
5377 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5378 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5383 * Reject the command if we were asked to deactivate a schedule that was
5386 if (!activate
&& *parameter_value
== 0) {
5387 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5388 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5393 * Reject the command if we were asked to deactivate a schedule that
5396 if (!activate
&& *parameter_value
!= new_value
) {
5397 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5398 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5402 *parameter_value
= activate
? new_value
: 0;
5404 switch (schedule_type
) {
5405 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5406 if (activate
&& session
->active
) {
5408 * Only start the timer if the session is active,
5409 * otherwise it will be started when the session starts.
5411 ret
= timer_session_rotation_schedule_timer_start(
5412 session
, new_value
);
5414 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5415 ret
= LTTNG_ERR_UNK
;
5419 ret
= timer_session_rotation_schedule_timer_stop(
5422 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5423 ret
= LTTNG_ERR_UNK
;
5428 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5430 ret
= subscribe_session_consumed_size_rotation(session
,
5431 new_value
, notification_thread_handle
);
5433 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5434 ret
= LTTNG_ERR_UNK
;
5438 ret
= unsubscribe_session_consumed_size_rotation(session
,
5439 notification_thread_handle
);
5441 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5442 ret
= LTTNG_ERR_UNK
;
5449 /* Would have been caught before. */
5461 /* Wait for a given path to be removed before continuing. */
5462 static enum lttng_error_code
wait_on_path(void *path_data
)
5464 const char *shm_path
= path_data
;
5466 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5472 ret
= stat(shm_path
, &st
);
5474 if (errno
!= ENOENT
) {
5475 PERROR("stat() returned an error while checking for the existence of the shm path");
5477 DBG("shm path no longer exists, completing the destruction of session");
5481 if (!S_ISDIR(st
.st_mode
)) {
5482 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5487 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5493 * Returns a pointer to a handler to run on completion of a command.
5494 * Returns NULL if no handler has to be run for the last command executed.
5496 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5498 struct cmd_completion_handler
*handler
= current_completion_handler
;
5500 current_completion_handler
= NULL
;
5505 * Init command subsystem.
5510 * Set network sequence index to 1 for streams to match a relayd
5511 * socket on the consumer side.
5513 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5514 relayd_net_seq_idx
= 1;
5515 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5517 DBG("Command subsystem initialized");