2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <urcu/list.h>
23 #include <urcu/uatomic.h>
27 #include <common/defaults.h>
28 #include <common/common.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
30 #include <common/relayd/relayd.h>
31 #include <common/utils.h>
32 #include <common/compat/string.h>
33 #include <common/kernel-ctl/kernel-ctl.h>
34 #include <common/dynamic-buffer.h>
35 #include <common/buffer-view.h>
36 #include <common/trace-chunk.h>
37 #include <lttng/location-internal.h>
38 #include <lttng/trigger/trigger-internal.h>
39 #include <lttng/condition/condition.h>
40 #include <lttng/action/action.h>
41 #include <lttng/channel.h>
42 #include <lttng/channel-internal.h>
43 #include <lttng/rotate-internal.h>
44 #include <lttng/location-internal.h>
45 #include <lttng/session-internal.h>
46 #include <lttng/userspace-probe-internal.h>
47 #include <lttng/session-descriptor-internal.h>
48 #include <common/string-utils/string-utils.h>
53 #include "health-sessiond.h"
55 #include "kernel-consumer.h"
56 #include "lttng-sessiond.h"
58 #include "lttng-syscall.h"
60 #include "buffer-registry.h"
61 #include "notification-thread.h"
62 #include "notification-thread-commands.h"
64 #include "rotation-thread.h"
66 #include "agent-thread.h"
70 /* Sleep for 100ms between each check for the shm path's deletion. */
71 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
73 struct cmd_destroy_session_reply_context
{
75 bool implicit_rotation_on_destroy
;
78 static enum lttng_error_code
wait_on_path(void *path
);
81 * Command completion handler that is used by the destroy command
82 * when a session that has a non-default shm_path is being destroyed.
84 * See comment in cmd_destroy_session() for the rationale.
86 static struct destroy_completion_handler
{
87 struct cmd_completion_handler handler
;
88 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
89 } destroy_completion_handler
= {
92 .data
= destroy_completion_handler
.shm_path
97 static struct cmd_completion_handler
*current_completion_handler
;
100 * Used to keep a unique index for each relayd socket created where this value
101 * is associated with streams on the consumer so it can match the right relayd
102 * to send to. It must be accessed with the relayd_net_seq_idx_lock
105 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
106 static uint64_t relayd_net_seq_idx
;
108 static int validate_ust_event_name(const char *);
109 static int cmd_enable_event_internal(struct ltt_session
*session
,
110 struct lttng_domain
*domain
,
111 char *channel_name
, struct lttng_event
*event
,
112 char *filter_expression
,
113 struct lttng_filter_bytecode
*filter
,
114 struct lttng_event_exclusion
*exclusion
,
118 * Create a session path used by list_lttng_sessions for the case that the
119 * session consumer is on the network.
121 static int build_network_session_path(char *dst
, size_t size
,
122 struct ltt_session
*session
)
124 int ret
, kdata_port
, udata_port
;
125 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
126 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
131 memset(tmp_urls
, 0, sizeof(tmp_urls
));
132 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
134 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
136 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
137 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
138 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
141 if (session
->ust_session
&& session
->ust_session
->consumer
) {
142 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
143 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
146 if (uuri
== NULL
&& kuri
== NULL
) {
147 uri
= &session
->consumer
->dst
.net
.control
;
148 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
149 } else if (kuri
&& uuri
) {
150 ret
= uri_compare(kuri
, uuri
);
154 /* Build uuri URL string */
155 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
162 } else if (kuri
&& uuri
== NULL
) {
164 } else if (uuri
&& kuri
== NULL
) {
168 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
174 * Do we have a UST url set. If yes, this means we have both kernel and UST
177 if (*tmp_uurl
!= '\0') {
178 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
179 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
182 if (kuri
|| (!kuri
&& !uuri
)) {
185 /* No kernel URI, use the UST port. */
188 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
196 * Get run-time attributes if the session has been started (discarded events,
199 static int get_kernel_runtime_stats(struct ltt_session
*session
,
200 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
201 uint64_t *lost_packets
)
205 if (!session
->has_been_started
) {
207 *discarded_events
= 0;
212 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
213 session
->kernel_session
->consumer
,
219 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
220 session
->kernel_session
->consumer
,
231 * Get run-time attributes if the session has been started (discarded events,
234 static int get_ust_runtime_stats(struct ltt_session
*session
,
235 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
236 uint64_t *lost_packets
)
239 struct ltt_ust_session
*usess
;
241 if (!discarded_events
|| !lost_packets
) {
246 usess
= session
->ust_session
;
247 assert(discarded_events
);
248 assert(lost_packets
);
250 if (!usess
|| !session
->has_been_started
) {
251 *discarded_events
= 0;
257 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
258 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
259 &usess
->buffer_reg_uid_list
,
260 usess
->consumer
, uchan
->id
,
261 uchan
->attr
.overwrite
,
264 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
265 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
266 uchan
, usess
->consumer
,
267 uchan
->attr
.overwrite
,
273 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
274 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
276 ERR("Unsupported buffer type");
287 * Fill lttng_channel array of all channels.
289 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
290 struct ltt_session
*session
, struct lttng_channel
*channels
,
291 struct lttng_channel_extended
*chan_exts
)
294 struct ltt_kernel_channel
*kchan
;
296 DBG("Listing channels for session %s", session
->name
);
299 case LTTNG_DOMAIN_KERNEL
:
300 /* Kernel channels */
301 if (session
->kernel_session
!= NULL
) {
302 cds_list_for_each_entry(kchan
,
303 &session
->kernel_session
->channel_list
.head
, list
) {
304 uint64_t discarded_events
, lost_packets
;
305 struct lttng_channel_extended
*extended
;
307 extended
= (struct lttng_channel_extended
*)
308 kchan
->channel
->attr
.extended
.ptr
;
310 ret
= get_kernel_runtime_stats(session
, kchan
,
311 &discarded_events
, &lost_packets
);
315 /* Copy lttng_channel struct to array */
316 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
317 channels
[i
].enabled
= kchan
->enabled
;
318 chan_exts
[i
].discarded_events
=
320 chan_exts
[i
].lost_packets
= lost_packets
;
321 chan_exts
[i
].monitor_timer_interval
=
322 extended
->monitor_timer_interval
;
323 chan_exts
[i
].blocking_timeout
= 0;
328 case LTTNG_DOMAIN_UST
:
330 struct lttng_ht_iter iter
;
331 struct ltt_ust_channel
*uchan
;
334 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
335 &iter
.iter
, uchan
, node
.node
) {
336 uint64_t discarded_events
= 0, lost_packets
= 0;
338 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
339 LTTNG_SYMBOL_NAME_LEN
)) {
342 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
343 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
344 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
345 channels
[i
].attr
.switch_timer_interval
=
346 uchan
->attr
.switch_timer_interval
;
347 channels
[i
].attr
.read_timer_interval
=
348 uchan
->attr
.read_timer_interval
;
349 channels
[i
].enabled
= uchan
->enabled
;
350 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
351 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
354 * Map enum lttng_ust_output to enum lttng_event_output.
356 switch (uchan
->attr
.output
) {
358 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
362 * LTTNG_UST_MMAP is the only supported UST
369 chan_exts
[i
].monitor_timer_interval
=
370 uchan
->monitor_timer_interval
;
371 chan_exts
[i
].blocking_timeout
=
372 uchan
->attr
.u
.s
.blocking_timeout
;
374 ret
= get_ust_runtime_stats(session
, uchan
,
375 &discarded_events
, &lost_packets
);
379 chan_exts
[i
].discarded_events
= discarded_events
;
380 chan_exts
[i
].lost_packets
= lost_packets
;
392 return -LTTNG_ERR_FATAL
;
398 static int increment_extended_len(const char *filter_expression
,
399 struct lttng_event_exclusion
*exclusion
,
400 const struct lttng_userspace_probe_location
*probe_location
,
401 size_t *extended_len
)
405 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
407 if (filter_expression
) {
408 *extended_len
+= strlen(filter_expression
) + 1;
412 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
415 if (probe_location
) {
416 ret
= lttng_userspace_probe_location_serialize(probe_location
,
421 *extended_len
+= ret
;
428 static int append_extended_info(const char *filter_expression
,
429 struct lttng_event_exclusion
*exclusion
,
430 struct lttng_userspace_probe_location
*probe_location
,
434 size_t filter_len
= 0;
435 size_t nb_exclusions
= 0;
436 size_t userspace_probe_location_len
= 0;
437 struct lttng_dynamic_buffer location_buffer
;
438 struct lttcomm_event_extended_header extended_header
;
440 if (filter_expression
) {
441 filter_len
= strlen(filter_expression
) + 1;
445 nb_exclusions
= exclusion
->count
;
448 if (probe_location
) {
449 lttng_dynamic_buffer_init(&location_buffer
);
450 ret
= lttng_userspace_probe_location_serialize(probe_location
,
451 &location_buffer
, NULL
);
456 userspace_probe_location_len
= location_buffer
.size
;
459 /* Set header fields */
460 extended_header
.filter_len
= filter_len
;
461 extended_header
.nb_exclusions
= nb_exclusions
;
462 extended_header
.userspace_probe_location_len
= userspace_probe_location_len
;
465 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
466 *extended_at
+= sizeof(extended_header
);
468 /* Copy filter string */
469 if (filter_expression
) {
470 memcpy(*extended_at
, filter_expression
, filter_len
);
471 *extended_at
+= filter_len
;
474 /* Copy exclusion names */
476 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
478 memcpy(*extended_at
, &exclusion
->names
, len
);
482 if (probe_location
) {
483 memcpy(*extended_at
, location_buffer
.data
, location_buffer
.size
);
484 *extended_at
+= location_buffer
.size
;
485 lttng_dynamic_buffer_reset(&location_buffer
);
493 * Create a list of agent domain events.
495 * Return number of events in list on success or else a negative value.
497 static int list_lttng_agent_events(struct agent
*agt
,
498 struct lttng_event
**events
, size_t *total_size
)
501 unsigned int nb_event
= 0;
502 struct agent_event
*event
;
503 struct lttng_event
*tmp_events
= NULL
;
504 struct lttng_ht_iter iter
;
505 size_t extended_len
= 0;
511 DBG3("Listing agent events");
514 nb_event
= lttng_ht_get_count(agt
->events
);
522 /* Compute required extended infos size */
523 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
526 * This is only valid because the commands which add events are
527 * processed in the same thread as the listing.
530 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
531 ret
= increment_extended_len(event
->filter_expression
, NULL
, NULL
,
534 DBG("Error computing the length of extended info message");
535 ret
= -LTTNG_ERR_FATAL
;
541 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
542 tmp_events
= zmalloc(*total_size
);
544 PERROR("zmalloc agent events session");
545 ret
= -LTTNG_ERR_FATAL
;
549 extended_at
= ((uint8_t *) tmp_events
) +
550 nb_event
* sizeof(struct lttng_event
);
553 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
554 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
555 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
556 tmp_events
[i
].enabled
= event
->enabled
;
557 tmp_events
[i
].loglevel
= event
->loglevel_value
;
558 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
561 /* Append extended info */
562 ret
= append_extended_info(event
->filter_expression
, NULL
, NULL
,
565 DBG("Error appending extended info message");
566 ret
= -LTTNG_ERR_FATAL
;
571 *events
= tmp_events
;
573 assert(nb_event
== i
);
584 * Create a list of ust global domain events.
586 static int list_lttng_ust_global_events(char *channel_name
,
587 struct ltt_ust_domain_global
*ust_global
,
588 struct lttng_event
**events
, size_t *total_size
)
591 unsigned int nb_event
= 0;
592 struct lttng_ht_iter iter
;
593 struct lttng_ht_node_str
*node
;
594 struct ltt_ust_channel
*uchan
;
595 struct ltt_ust_event
*uevent
;
596 struct lttng_event
*tmp
;
597 size_t extended_len
= 0;
600 DBG("Listing UST global events for channel %s", channel_name
);
604 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
605 node
= lttng_ht_iter_get_node_str(&iter
);
607 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
611 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
613 nb_event
= lttng_ht_get_count(uchan
->events
);
620 DBG3("Listing UST global %d events", nb_event
);
622 /* Compute required extended infos size */
623 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
624 if (uevent
->internal
) {
629 ret
= increment_extended_len(uevent
->filter_expression
,
630 uevent
->exclusion
, NULL
, &extended_len
);
632 DBG("Error computing the length of extended info message");
633 ret
= -LTTNG_ERR_FATAL
;
638 /* All events are internal, skip. */
644 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
645 tmp
= zmalloc(*total_size
);
647 ret
= -LTTNG_ERR_FATAL
;
651 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
653 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
654 if (uevent
->internal
) {
655 /* This event should remain hidden from clients */
658 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
659 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
660 tmp
[i
].enabled
= uevent
->enabled
;
662 switch (uevent
->attr
.instrumentation
) {
663 case LTTNG_UST_TRACEPOINT
:
664 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
666 case LTTNG_UST_PROBE
:
667 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
669 case LTTNG_UST_FUNCTION
:
670 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
674 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
675 switch (uevent
->attr
.loglevel_type
) {
676 case LTTNG_UST_LOGLEVEL_ALL
:
677 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
679 case LTTNG_UST_LOGLEVEL_RANGE
:
680 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
682 case LTTNG_UST_LOGLEVEL_SINGLE
:
683 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
686 if (uevent
->filter
) {
689 if (uevent
->exclusion
) {
690 tmp
[i
].exclusion
= 1;
694 /* Append extended info */
695 ret
= append_extended_info(uevent
->filter_expression
,
696 uevent
->exclusion
, NULL
, &extended_at
);
698 DBG("Error appending extended info message");
699 ret
= -LTTNG_ERR_FATAL
;
712 * Fill lttng_event array of all kernel events in the channel.
714 static int list_lttng_kernel_events(char *channel_name
,
715 struct ltt_kernel_session
*kernel_session
,
716 struct lttng_event
**events
, size_t *total_size
)
719 unsigned int nb_event
;
720 struct ltt_kernel_event
*event
;
721 struct ltt_kernel_channel
*kchan
;
722 size_t extended_len
= 0;
725 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
727 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
731 nb_event
= kchan
->event_count
;
733 DBG("Listing events for channel %s", kchan
->channel
->name
);
741 /* Compute required extended infos size */
742 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
743 ret
= increment_extended_len(event
->filter_expression
, NULL
,
744 event
->userspace_probe_location
,
747 DBG("Error computing the length of extended info message");
748 ret
= -LTTNG_ERR_FATAL
;
753 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
754 *events
= zmalloc(*total_size
);
755 if (*events
== NULL
) {
756 ret
= -LTTNG_ERR_FATAL
;
760 extended_at
= ((void *) *events
) +
761 nb_event
* sizeof(struct lttng_event
);
763 /* Kernel channels */
764 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
765 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
766 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
767 (*events
)[i
].enabled
= event
->enabled
;
768 (*events
)[i
].filter
=
769 (unsigned char) !!event
->filter_expression
;
771 switch (event
->event
->instrumentation
) {
772 case LTTNG_KERNEL_TRACEPOINT
:
773 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
775 case LTTNG_KERNEL_KRETPROBE
:
776 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
777 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
778 sizeof(struct lttng_kernel_kprobe
));
780 case LTTNG_KERNEL_KPROBE
:
781 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
782 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
783 sizeof(struct lttng_kernel_kprobe
));
785 case LTTNG_KERNEL_UPROBE
:
786 (*events
)[i
].type
= LTTNG_EVENT_USERSPACE_PROBE
;
788 case LTTNG_KERNEL_FUNCTION
:
789 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
790 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
791 sizeof(struct lttng_kernel_function
));
793 case LTTNG_KERNEL_NOOP
:
794 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
796 case LTTNG_KERNEL_SYSCALL
:
797 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
799 case LTTNG_KERNEL_ALL
:
807 /* Append extended info */
808 ret
= append_extended_info(event
->filter_expression
, NULL
,
809 event
->userspace_probe_location
, &extended_at
);
811 DBG("Error appending extended info message");
812 ret
= -LTTNG_ERR_FATAL
;
821 /* Negate the error code to differentiate the size from an error */
826 * Add URI so the consumer output object. Set the correct path depending on the
827 * domain adding the default trace directory.
829 static enum lttng_error_code
add_uri_to_consumer(
830 const struct ltt_session
*session
,
831 struct consumer_output
*consumer
,
832 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
835 enum lttng_error_code ret_code
= LTTNG_OK
;
839 if (consumer
== NULL
) {
840 DBG("No consumer detected. Don't add URI. Stopping.");
841 ret_code
= LTTNG_ERR_NO_CONSUMER
;
846 case LTTNG_DOMAIN_KERNEL
:
847 ret
= lttng_strncpy(consumer
->domain_subdir
,
848 DEFAULT_KERNEL_TRACE_DIR
,
849 sizeof(consumer
->domain_subdir
));
851 case LTTNG_DOMAIN_UST
:
852 ret
= lttng_strncpy(consumer
->domain_subdir
,
853 DEFAULT_UST_TRACE_DIR
,
854 sizeof(consumer
->domain_subdir
));
858 * This case is possible is we try to add the URI to the global
859 * tracing session consumer object which in this case there is
862 memset(consumer
->domain_subdir
, 0,
863 sizeof(consumer
->domain_subdir
));
867 ERR("Failed to initialize consumer output domain subdirectory");
868 ret_code
= LTTNG_ERR_FATAL
;
872 switch (uri
->dtype
) {
875 DBG2("Setting network URI to consumer");
877 if (consumer
->type
== CONSUMER_DST_NET
) {
878 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
879 consumer
->dst
.net
.control_isset
) ||
880 (uri
->stype
== LTTNG_STREAM_DATA
&&
881 consumer
->dst
.net
.data_isset
)) {
882 ret_code
= LTTNG_ERR_URL_EXIST
;
886 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
889 /* Set URI into consumer output object */
890 ret
= consumer_set_network_uri(session
, consumer
, uri
);
894 } else if (ret
== 1) {
896 * URI was the same in the consumer so we do not append the subdir
897 * again so to not duplicate output dir.
904 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
905 ret_code
= LTTNG_ERR_INVALID
;
908 DBG2("Setting trace directory path from URI to %s",
910 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
912 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
914 sizeof(consumer
->dst
.session_root_path
));
915 consumer
->type
= CONSUMER_DST_LOCAL
;
925 * Init tracing by creating trace directory and sending fds kernel consumer.
927 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
930 struct lttng_ht_iter iter
;
931 struct consumer_socket
*socket
;
937 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
938 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
940 pthread_mutex_lock(socket
->lock
);
941 ret
= kernel_consumer_send_session(socket
, session
);
942 pthread_mutex_unlock(socket
->lock
);
944 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
956 * Create a socket to the relayd using the URI.
958 * On success, the relayd_sock pointer is set to the created socket.
959 * Else, it remains untouched and an LTTng error code is returned.
961 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
962 struct lttcomm_relayd_sock
**relayd_sock
,
963 struct consumer_output
*consumer
)
966 enum lttng_error_code status
= LTTNG_OK
;
967 struct lttcomm_relayd_sock
*rsock
;
969 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
970 RELAYD_VERSION_COMM_MINOR
);
972 status
= LTTNG_ERR_FATAL
;
977 * Connect to relayd so we can proceed with a session creation. This call
978 * can possibly block for an arbitrary amount of time to set the health
979 * state to be in poll execution.
982 ret
= relayd_connect(rsock
);
985 ERR("Unable to reach lttng-relayd");
986 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
990 /* Create socket for control stream. */
991 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
992 DBG3("Creating relayd stream socket from URI");
994 /* Check relayd version */
995 ret
= relayd_version_check(rsock
);
996 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
997 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
999 } else if (ret
< 0) {
1000 ERR("Unable to reach lttng-relayd");
1001 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1004 consumer
->relay_major_version
= rsock
->major
;
1005 consumer
->relay_minor_version
= rsock
->minor
;
1006 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1007 DBG3("Creating relayd data socket from URI");
1009 /* Command is not valid */
1010 ERR("Relayd invalid stream type: %d", uri
->stype
);
1011 status
= LTTNG_ERR_INVALID
;
1015 *relayd_sock
= rsock
;
1020 /* The returned value is not useful since we are on an error path. */
1021 (void) relayd_close(rsock
);
1029 * Connect to the relayd using URI and send the socket to the right consumer.
1031 * The consumer socket lock must be held by the caller.
1033 * Returns LTTNG_OK on success or an LTTng error code on failure.
1035 static enum lttng_error_code
send_consumer_relayd_socket(
1036 unsigned int session_id
,
1037 struct lttng_uri
*relayd_uri
,
1038 struct consumer_output
*consumer
,
1039 struct consumer_socket
*consumer_sock
,
1040 const char *session_name
, const char *hostname
,
1041 int session_live_timer
,
1042 const uint64_t *current_chunk_id
,
1043 time_t session_creation_time
)
1046 struct lttcomm_relayd_sock
*rsock
= NULL
;
1047 enum lttng_error_code status
;
1049 /* Connect to relayd and make version check if uri is the control. */
1050 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1051 if (status
!= LTTNG_OK
) {
1052 goto relayd_comm_error
;
1056 /* Set the network sequence index if not set. */
1057 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1058 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1060 * Increment net_seq_idx because we are about to transfer the
1061 * new relayd socket to the consumer.
1062 * Assign unique key so the consumer can match streams.
1064 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1065 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1068 /* Send relayd socket to consumer. */
1069 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1070 relayd_uri
->stype
, session_id
,
1071 session_name
, hostname
, session_live_timer
,
1072 current_chunk_id
, session_creation_time
);
1074 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1078 /* Flag that the corresponding socket was sent. */
1079 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1080 consumer_sock
->control_sock_sent
= 1;
1081 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1082 consumer_sock
->data_sock_sent
= 1;
1086 * Close socket which was dup on the consumer side. The session daemon does
1087 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1091 if (status
!= LTTNG_OK
) {
1093 * The consumer output for this session should not be used anymore
1094 * since the relayd connection failed thus making any tracing or/and
1095 * streaming not usable.
1097 consumer
->enabled
= 0;
1099 (void) relayd_close(rsock
);
1107 * Send both relayd sockets to a specific consumer and domain. This is a
1108 * helper function to facilitate sending the information to the consumer for a
1111 * The consumer socket lock must be held by the caller.
1113 * Returns LTTNG_OK, or an LTTng error code on failure.
1115 static enum lttng_error_code
send_consumer_relayd_sockets(
1116 enum lttng_domain_type domain
,
1117 unsigned int session_id
, struct consumer_output
*consumer
,
1118 struct consumer_socket
*sock
, const char *session_name
,
1119 const char *hostname
, int session_live_timer
,
1120 const uint64_t *current_chunk_id
, time_t session_creation_time
)
1122 enum lttng_error_code status
= LTTNG_OK
;
1127 /* Sending control relayd socket. */
1128 if (!sock
->control_sock_sent
) {
1129 status
= send_consumer_relayd_socket(session_id
,
1130 &consumer
->dst
.net
.control
, consumer
, sock
,
1131 session_name
, hostname
, session_live_timer
,
1132 current_chunk_id
, session_creation_time
);
1133 if (status
!= LTTNG_OK
) {
1138 /* Sending data relayd socket. */
1139 if (!sock
->data_sock_sent
) {
1140 status
= send_consumer_relayd_socket(session_id
,
1141 &consumer
->dst
.net
.data
, consumer
, sock
,
1142 session_name
, hostname
, session_live_timer
,
1143 current_chunk_id
, session_creation_time
);
1144 if (status
!= LTTNG_OK
) {
1154 * Setup relayd connections for a tracing session. First creates the socket to
1155 * the relayd and send them to the right domain consumer. Consumer type MUST be
1158 int cmd_setup_relayd(struct ltt_session
*session
)
1161 struct ltt_ust_session
*usess
;
1162 struct ltt_kernel_session
*ksess
;
1163 struct consumer_socket
*socket
;
1164 struct lttng_ht_iter iter
;
1165 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1169 usess
= session
->ust_session
;
1170 ksess
= session
->kernel_session
;
1172 DBG("Setting relayd for session %s", session
->name
);
1174 if (session
->current_trace_chunk
) {
1175 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1176 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1178 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1179 current_chunk_id
.is_set
= true;
1181 ERR("Failed to get current trace chunk id");
1182 ret
= LTTNG_ERR_UNK
;
1189 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1190 && usess
->consumer
->enabled
) {
1191 /* For each consumer socket, send relayd sockets */
1192 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1193 socket
, node
.node
) {
1194 pthread_mutex_lock(socket
->lock
);
1195 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1196 usess
->consumer
, socket
,
1197 session
->name
, session
->hostname
,
1198 session
->live_timer
,
1199 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1200 session
->creation_time
);
1201 pthread_mutex_unlock(socket
->lock
);
1202 if (ret
!= LTTNG_OK
) {
1205 /* Session is now ready for network streaming. */
1206 session
->net_handle
= 1;
1208 session
->consumer
->relay_major_version
=
1209 usess
->consumer
->relay_major_version
;
1210 session
->consumer
->relay_minor_version
=
1211 usess
->consumer
->relay_minor_version
;
1214 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1215 && ksess
->consumer
->enabled
) {
1216 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1217 socket
, node
.node
) {
1218 pthread_mutex_lock(socket
->lock
);
1219 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1220 ksess
->consumer
, socket
,
1221 session
->name
, session
->hostname
,
1222 session
->live_timer
,
1223 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1224 session
->creation_time
);
1225 pthread_mutex_unlock(socket
->lock
);
1226 if (ret
!= LTTNG_OK
) {
1229 /* Session is now ready for network streaming. */
1230 session
->net_handle
= 1;
1232 session
->consumer
->relay_major_version
=
1233 ksess
->consumer
->relay_major_version
;
1234 session
->consumer
->relay_minor_version
=
1235 ksess
->consumer
->relay_minor_version
;
1244 * Start a kernel session by opening all necessary streams.
1246 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1249 struct ltt_kernel_channel
*kchan
;
1251 /* Open kernel metadata */
1252 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1253 ret
= kernel_open_metadata(ksess
);
1255 ret
= LTTNG_ERR_KERN_META_FAIL
;
1260 /* Open kernel metadata stream */
1261 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1262 ret
= kernel_open_metadata_stream(ksess
);
1264 ERR("Kernel create metadata stream failed");
1265 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1270 /* For each channel */
1271 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1272 if (kchan
->stream_count
== 0) {
1273 ret
= kernel_open_channel_stream(kchan
);
1275 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1278 /* Update the stream global counter */
1279 ksess
->stream_count_global
+= ret
;
1283 /* Setup kernel consumer socket and send fds to it */
1284 ret
= init_kernel_tracing(ksess
);
1286 ret
= LTTNG_ERR_KERN_START_FAIL
;
1290 /* This start the kernel tracing */
1291 ret
= kernel_start_session(ksess
);
1293 ret
= LTTNG_ERR_KERN_START_FAIL
;
1297 /* Quiescent wait after starting trace */
1298 kernel_wait_quiescent(wpipe
);
1309 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1311 int cmd_disable_channel(struct ltt_session
*session
,
1312 enum lttng_domain_type domain
, char *channel_name
)
1315 struct ltt_ust_session
*usess
;
1317 usess
= session
->ust_session
;
1322 case LTTNG_DOMAIN_KERNEL
:
1324 ret
= channel_kernel_disable(session
->kernel_session
,
1326 if (ret
!= LTTNG_OK
) {
1330 kernel_wait_quiescent(kernel_tracer_fd
);
1333 case LTTNG_DOMAIN_UST
:
1335 struct ltt_ust_channel
*uchan
;
1336 struct lttng_ht
*chan_ht
;
1338 chan_ht
= usess
->domain_global
.channels
;
1340 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1341 if (uchan
== NULL
) {
1342 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1346 ret
= channel_ust_disable(usess
, uchan
);
1347 if (ret
!= LTTNG_OK
) {
1353 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1365 * Command LTTNG_TRACK_PID processed by the client thread.
1367 * Called with session lock held.
1369 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1377 case LTTNG_DOMAIN_KERNEL
:
1379 struct ltt_kernel_session
*ksess
;
1381 ksess
= session
->kernel_session
;
1383 ret
= kernel_track_pid(ksess
, pid
);
1384 if (ret
!= LTTNG_OK
) {
1388 kernel_wait_quiescent(kernel_tracer_fd
);
1391 case LTTNG_DOMAIN_UST
:
1393 struct ltt_ust_session
*usess
;
1395 usess
= session
->ust_session
;
1397 ret
= trace_ust_track_pid(usess
, pid
);
1398 if (ret
!= LTTNG_OK
) {
1404 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1416 * Command LTTNG_UNTRACK_PID processed by the client thread.
1418 * Called with session lock held.
1420 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1428 case LTTNG_DOMAIN_KERNEL
:
1430 struct ltt_kernel_session
*ksess
;
1432 ksess
= session
->kernel_session
;
1434 ret
= kernel_untrack_pid(ksess
, pid
);
1435 if (ret
!= LTTNG_OK
) {
1439 kernel_wait_quiescent(kernel_tracer_fd
);
1442 case LTTNG_DOMAIN_UST
:
1444 struct ltt_ust_session
*usess
;
1446 usess
= session
->ust_session
;
1448 ret
= trace_ust_untrack_pid(usess
, pid
);
1449 if (ret
!= LTTNG_OK
) {
1455 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1467 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1469 * The wpipe arguments is used as a notifier for the kernel thread.
1471 int cmd_enable_channel(struct ltt_session
*session
,
1472 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1475 struct ltt_ust_session
*usess
= session
->ust_session
;
1476 struct lttng_ht
*chan_ht
;
1483 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1485 /* Validate channel name */
1486 if (attr
->name
[0] == '.' ||
1487 memchr(attr
->name
, '/', len
) != NULL
) {
1488 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1492 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1497 * Don't try to enable a channel if the session has been started at
1498 * some point in time before. The tracer does not allow it.
1500 if (session
->has_been_started
) {
1501 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1506 * If the session is a live session, remove the switch timer, the
1507 * live timer does the same thing but sends also synchronisation
1508 * beacons for inactive streams.
1510 if (session
->live_timer
> 0) {
1511 attr
->attr
.live_timer_interval
= session
->live_timer
;
1512 attr
->attr
.switch_timer_interval
= 0;
1515 /* Check for feature support */
1516 switch (domain
->type
) {
1517 case LTTNG_DOMAIN_KERNEL
:
1519 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd
) != 1) {
1520 /* Sampling position of buffer is not supported */
1521 WARN("Kernel tracer does not support buffer monitoring. "
1522 "Setting the monitor interval timer to 0 "
1523 "(disabled) for channel '%s' of session '%s'",
1524 attr
-> name
, session
->name
);
1525 lttng_channel_set_monitor_timer_interval(attr
, 0);
1529 case LTTNG_DOMAIN_UST
:
1531 case LTTNG_DOMAIN_JUL
:
1532 case LTTNG_DOMAIN_LOG4J
:
1533 case LTTNG_DOMAIN_PYTHON
:
1534 if (!agent_tracing_is_enabled()) {
1535 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1536 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1541 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1545 switch (domain
->type
) {
1546 case LTTNG_DOMAIN_KERNEL
:
1548 struct ltt_kernel_channel
*kchan
;
1550 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1551 session
->kernel_session
);
1552 if (kchan
== NULL
) {
1553 if (session
->snapshot
.nb_output
> 0 ||
1554 session
->snapshot_mode
) {
1555 /* Enforce mmap output for snapshot sessions. */
1556 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1558 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1559 if (attr
->name
[0] != '\0') {
1560 session
->kernel_session
->has_non_default_channel
= 1;
1563 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1566 if (ret
!= LTTNG_OK
) {
1570 kernel_wait_quiescent(kernel_tracer_fd
);
1573 case LTTNG_DOMAIN_UST
:
1574 case LTTNG_DOMAIN_JUL
:
1575 case LTTNG_DOMAIN_LOG4J
:
1576 case LTTNG_DOMAIN_PYTHON
:
1578 struct ltt_ust_channel
*uchan
;
1583 * Current agent implementation limitations force us to allow
1584 * only one channel at once in "agent" subdomains. Each
1585 * subdomain has a default channel name which must be strictly
1588 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1589 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1590 LTTNG_SYMBOL_NAME_LEN
)) {
1591 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1594 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1595 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1596 LTTNG_SYMBOL_NAME_LEN
)) {
1597 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1600 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1601 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1602 LTTNG_SYMBOL_NAME_LEN
)) {
1603 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1608 chan_ht
= usess
->domain_global
.channels
;
1610 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1611 if (uchan
== NULL
) {
1612 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1613 if (attr
->name
[0] != '\0') {
1614 usess
->has_non_default_channel
= 1;
1617 ret
= channel_ust_enable(usess
, uchan
);
1622 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1626 if (ret
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1627 session
->has_non_mmap_channel
= true;
1636 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1638 int cmd_disable_event(struct ltt_session
*session
,
1639 enum lttng_domain_type domain
, char *channel_name
,
1640 struct lttng_event
*event
)
1645 DBG("Disable event command for event \'%s\'", event
->name
);
1647 event_name
= event
->name
;
1649 /* Error out on unhandled search criteria */
1650 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1651 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1652 ret
= LTTNG_ERR_UNK
;
1659 case LTTNG_DOMAIN_KERNEL
:
1661 struct ltt_kernel_channel
*kchan
;
1662 struct ltt_kernel_session
*ksess
;
1664 ksess
= session
->kernel_session
;
1667 * If a non-default channel has been created in the
1668 * session, explicitely require that -c chan_name needs
1671 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1672 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1676 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1677 if (kchan
== NULL
) {
1678 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1682 switch (event
->type
) {
1683 case LTTNG_EVENT_ALL
:
1684 case LTTNG_EVENT_TRACEPOINT
:
1685 case LTTNG_EVENT_SYSCALL
:
1686 case LTTNG_EVENT_PROBE
:
1687 case LTTNG_EVENT_FUNCTION
:
1688 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1689 if (event_name
[0] == '\0') {
1690 ret
= event_kernel_disable_event(kchan
,
1693 ret
= event_kernel_disable_event(kchan
,
1694 event_name
, event
->type
);
1696 if (ret
!= LTTNG_OK
) {
1701 ret
= LTTNG_ERR_UNK
;
1705 kernel_wait_quiescent(kernel_tracer_fd
);
1708 case LTTNG_DOMAIN_UST
:
1710 struct ltt_ust_channel
*uchan
;
1711 struct ltt_ust_session
*usess
;
1713 usess
= session
->ust_session
;
1715 if (validate_ust_event_name(event_name
)) {
1716 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1721 * If a non-default channel has been created in the
1722 * session, explicitly require that -c chan_name needs
1725 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1726 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1730 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1732 if (uchan
== NULL
) {
1733 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1737 switch (event
->type
) {
1738 case LTTNG_EVENT_ALL
:
1740 * An empty event name means that everything
1741 * should be disabled.
1743 if (event
->name
[0] == '\0') {
1744 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1746 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1749 if (ret
!= LTTNG_OK
) {
1754 ret
= LTTNG_ERR_UNK
;
1758 DBG3("Disable UST event %s in channel %s completed", event_name
,
1762 case LTTNG_DOMAIN_LOG4J
:
1763 case LTTNG_DOMAIN_JUL
:
1764 case LTTNG_DOMAIN_PYTHON
:
1767 struct ltt_ust_session
*usess
= session
->ust_session
;
1771 switch (event
->type
) {
1772 case LTTNG_EVENT_ALL
:
1775 ret
= LTTNG_ERR_UNK
;
1779 agt
= trace_ust_find_agent(usess
, domain
);
1781 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1785 * An empty event name means that everything
1786 * should be disabled.
1788 if (event
->name
[0] == '\0') {
1789 ret
= event_agent_disable_all(usess
, agt
);
1791 ret
= event_agent_disable(usess
, agt
, event_name
);
1793 if (ret
!= LTTNG_OK
) {
1800 ret
= LTTNG_ERR_UND
;
1813 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1815 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1816 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1818 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1819 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1822 * Don't try to add a context if the session has been started at
1823 * some point in time before. The tracer does not allow it and would
1824 * result in a corrupted trace.
1826 if (session
->has_been_started
) {
1827 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1831 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1832 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1833 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1837 case LTTNG_DOMAIN_KERNEL
:
1838 assert(session
->kernel_session
);
1840 if (session
->kernel_session
->channel_count
== 0) {
1841 /* Create default channel */
1842 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1843 if (ret
!= LTTNG_OK
) {
1846 chan_kern_created
= 1;
1848 /* Add kernel context to kernel tracer */
1849 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1850 if (ret
!= LTTNG_OK
) {
1854 case LTTNG_DOMAIN_JUL
:
1855 case LTTNG_DOMAIN_LOG4J
:
1858 * Validate channel name.
1859 * If no channel name is given and the domain is JUL or LOG4J,
1860 * set it to the appropriate domain-specific channel name. If
1861 * a name is provided but does not match the expexted channel
1862 * name, return an error.
1864 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1865 strcmp(channel_name
,
1866 DEFAULT_JUL_CHANNEL_NAME
)) {
1867 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1869 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1870 strcmp(channel_name
,
1871 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1872 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1875 /* break is _not_ missing here. */
1877 case LTTNG_DOMAIN_UST
:
1879 struct ltt_ust_session
*usess
= session
->ust_session
;
1880 unsigned int chan_count
;
1884 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1885 if (chan_count
== 0) {
1886 struct lttng_channel
*attr
;
1887 /* Create default channel */
1888 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1890 ret
= LTTNG_ERR_FATAL
;
1894 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1895 if (ret
!= LTTNG_OK
) {
1899 channel_attr_destroy(attr
);
1900 chan_ust_created
= 1;
1903 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1904 free(app_ctx_provider_name
);
1906 app_ctx_name
= NULL
;
1907 app_ctx_provider_name
= NULL
;
1908 if (ret
!= LTTNG_OK
) {
1914 ret
= LTTNG_ERR_UND
;
1922 if (chan_kern_created
) {
1923 struct ltt_kernel_channel
*kchan
=
1924 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1925 session
->kernel_session
);
1926 /* Created previously, this should NOT fail. */
1928 kernel_destroy_channel(kchan
);
1931 if (chan_ust_created
) {
1932 struct ltt_ust_channel
*uchan
=
1933 trace_ust_find_channel_by_name(
1934 session
->ust_session
->domain_global
.channels
,
1935 DEFAULT_CHANNEL_NAME
);
1936 /* Created previously, this should NOT fail. */
1938 /* Remove from the channel list of the session. */
1939 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1941 trace_ust_destroy_channel(uchan
);
1944 free(app_ctx_provider_name
);
1949 static inline bool name_starts_with(const char *name
, const char *prefix
)
1951 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1953 return !strncmp(name
, prefix
, max_cmp_len
);
1956 /* Perform userspace-specific event name validation */
1957 static int validate_ust_event_name(const char *name
)
1967 * Check name against all internal UST event component namespaces used
1970 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1971 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1972 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1981 * Internal version of cmd_enable_event() with a supplemental
1982 * "internal_event" flag which is used to enable internal events which should
1983 * be hidden from clients. Such events are used in the agent implementation to
1984 * enable the events through which all "agent" events are funeled.
1986 static int _cmd_enable_event(struct ltt_session
*session
,
1987 struct lttng_domain
*domain
,
1988 char *channel_name
, struct lttng_event
*event
,
1989 char *filter_expression
,
1990 struct lttng_filter_bytecode
*filter
,
1991 struct lttng_event_exclusion
*exclusion
,
1992 int wpipe
, bool internal_event
)
1994 int ret
= 0, channel_created
= 0;
1995 struct lttng_channel
*attr
= NULL
;
1999 assert(channel_name
);
2001 /* If we have a filter, we must have its filter expression */
2002 assert(!(!!filter_expression
^ !!filter
));
2004 /* Normalize event name as a globbing pattern */
2005 strutils_normalize_star_glob_pattern(event
->name
);
2007 /* Normalize exclusion names as globbing patterns */
2011 for (i
= 0; i
< exclusion
->count
; i
++) {
2012 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2014 strutils_normalize_star_glob_pattern(name
);
2018 DBG("Enable event command for event \'%s\'", event
->name
);
2022 switch (domain
->type
) {
2023 case LTTNG_DOMAIN_KERNEL
:
2025 struct ltt_kernel_channel
*kchan
;
2028 * If a non-default channel has been created in the
2029 * session, explicitely require that -c chan_name needs
2032 if (session
->kernel_session
->has_non_default_channel
2033 && channel_name
[0] == '\0') {
2034 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2038 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2039 session
->kernel_session
);
2040 if (kchan
== NULL
) {
2041 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2042 LTTNG_BUFFER_GLOBAL
);
2044 ret
= LTTNG_ERR_FATAL
;
2047 if (lttng_strncpy(attr
->name
, channel_name
,
2048 sizeof(attr
->name
))) {
2049 ret
= LTTNG_ERR_INVALID
;
2053 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2054 if (ret
!= LTTNG_OK
) {
2057 channel_created
= 1;
2060 /* Get the newly created kernel channel pointer */
2061 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2062 session
->kernel_session
);
2063 if (kchan
== NULL
) {
2064 /* This sould not happen... */
2065 ret
= LTTNG_ERR_FATAL
;
2069 switch (event
->type
) {
2070 case LTTNG_EVENT_ALL
:
2072 char *filter_expression_a
= NULL
;
2073 struct lttng_filter_bytecode
*filter_a
= NULL
;
2076 * We need to duplicate filter_expression and filter,
2077 * because ownership is passed to first enable
2080 if (filter_expression
) {
2081 filter_expression_a
= strdup(filter_expression
);
2082 if (!filter_expression_a
) {
2083 ret
= LTTNG_ERR_FATAL
;
2088 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2090 free(filter_expression_a
);
2091 ret
= LTTNG_ERR_FATAL
;
2094 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2096 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2097 ret
= event_kernel_enable_event(kchan
, event
,
2098 filter_expression
, filter
);
2099 /* We have passed ownership */
2100 filter_expression
= NULL
;
2102 if (ret
!= LTTNG_OK
) {
2103 if (channel_created
) {
2104 /* Let's not leak a useless channel. */
2105 kernel_destroy_channel(kchan
);
2107 free(filter_expression_a
);
2111 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2112 ret
= event_kernel_enable_event(kchan
, event
,
2113 filter_expression_a
, filter_a
);
2114 /* We have passed ownership */
2115 filter_expression_a
= NULL
;
2117 if (ret
!= LTTNG_OK
) {
2122 case LTTNG_EVENT_PROBE
:
2123 case LTTNG_EVENT_USERSPACE_PROBE
:
2124 case LTTNG_EVENT_FUNCTION
:
2125 case LTTNG_EVENT_FUNCTION_ENTRY
:
2126 case LTTNG_EVENT_TRACEPOINT
:
2127 ret
= event_kernel_enable_event(kchan
, event
,
2128 filter_expression
, filter
);
2129 /* We have passed ownership */
2130 filter_expression
= NULL
;
2132 if (ret
!= LTTNG_OK
) {
2133 if (channel_created
) {
2134 /* Let's not leak a useless channel. */
2135 kernel_destroy_channel(kchan
);
2140 case LTTNG_EVENT_SYSCALL
:
2141 ret
= event_kernel_enable_event(kchan
, event
,
2142 filter_expression
, filter
);
2143 /* We have passed ownership */
2144 filter_expression
= NULL
;
2146 if (ret
!= LTTNG_OK
) {
2151 ret
= LTTNG_ERR_UNK
;
2155 kernel_wait_quiescent(kernel_tracer_fd
);
2158 case LTTNG_DOMAIN_UST
:
2160 struct ltt_ust_channel
*uchan
;
2161 struct ltt_ust_session
*usess
= session
->ust_session
;
2166 * If a non-default channel has been created in the
2167 * session, explicitely require that -c chan_name needs
2170 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2171 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2175 /* Get channel from global UST domain */
2176 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2178 if (uchan
== NULL
) {
2179 /* Create default channel */
2180 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2181 usess
->buffer_type
);
2183 ret
= LTTNG_ERR_FATAL
;
2186 if (lttng_strncpy(attr
->name
, channel_name
,
2187 sizeof(attr
->name
))) {
2188 ret
= LTTNG_ERR_INVALID
;
2192 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2193 if (ret
!= LTTNG_OK
) {
2197 /* Get the newly created channel reference back */
2198 uchan
= trace_ust_find_channel_by_name(
2199 usess
->domain_global
.channels
, channel_name
);
2203 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2205 * Don't allow users to add UST events to channels which
2206 * are assigned to a userspace subdomain (JUL, Log4J,
2209 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2213 if (!internal_event
) {
2215 * Ensure the event name is not reserved for internal
2218 ret
= validate_ust_event_name(event
->name
);
2220 WARN("Userspace event name %s failed validation.",
2222 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2227 /* At this point, the session and channel exist on the tracer */
2228 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2229 filter_expression
, filter
, exclusion
,
2231 /* We have passed ownership */
2232 filter_expression
= NULL
;
2235 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2236 goto already_enabled
;
2237 } else if (ret
!= LTTNG_OK
) {
2242 case LTTNG_DOMAIN_LOG4J
:
2243 case LTTNG_DOMAIN_JUL
:
2244 case LTTNG_DOMAIN_PYTHON
:
2246 const char *default_event_name
, *default_chan_name
;
2248 struct lttng_event uevent
;
2249 struct lttng_domain tmp_dom
;
2250 struct ltt_ust_session
*usess
= session
->ust_session
;
2254 if (!agent_tracing_is_enabled()) {
2255 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2256 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2260 agt
= trace_ust_find_agent(usess
, domain
->type
);
2262 agt
= agent_create(domain
->type
);
2264 ret
= LTTNG_ERR_NOMEM
;
2267 agent_add(agt
, usess
->agents
);
2270 /* Create the default tracepoint. */
2271 memset(&uevent
, 0, sizeof(uevent
));
2272 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2273 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2274 default_event_name
= event_get_default_agent_ust_name(
2276 if (!default_event_name
) {
2277 ret
= LTTNG_ERR_FATAL
;
2280 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2281 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2284 * The domain type is changed because we are about to enable the
2285 * default channel and event for the JUL domain that are hardcoded.
2286 * This happens in the UST domain.
2288 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2289 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2291 switch (domain
->type
) {
2292 case LTTNG_DOMAIN_LOG4J
:
2293 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2295 case LTTNG_DOMAIN_JUL
:
2296 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2298 case LTTNG_DOMAIN_PYTHON
:
2299 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2302 /* The switch/case we are in makes this impossible */
2307 char *filter_expression_copy
= NULL
;
2308 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2311 const size_t filter_size
= sizeof(
2312 struct lttng_filter_bytecode
)
2315 filter_copy
= zmalloc(filter_size
);
2317 ret
= LTTNG_ERR_NOMEM
;
2320 memcpy(filter_copy
, filter
, filter_size
);
2322 filter_expression_copy
=
2323 strdup(filter_expression
);
2324 if (!filter_expression
) {
2325 ret
= LTTNG_ERR_NOMEM
;
2328 if (!filter_expression_copy
|| !filter_copy
) {
2329 free(filter_expression_copy
);
2335 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2336 (char *) default_chan_name
,
2337 &uevent
, filter_expression_copy
,
2338 filter_copy
, NULL
, wpipe
);
2341 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2342 goto already_enabled
;
2343 } else if (ret
!= LTTNG_OK
) {
2347 /* The wild card * means that everything should be enabled. */
2348 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2349 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2352 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2356 filter_expression
= NULL
;
2357 if (ret
!= LTTNG_OK
) {
2364 ret
= LTTNG_ERR_UND
;
2372 free(filter_expression
);
2375 channel_attr_destroy(attr
);
2381 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2382 * We own filter, exclusion, and filter_expression.
2384 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2385 char *channel_name
, struct lttng_event
*event
,
2386 char *filter_expression
,
2387 struct lttng_filter_bytecode
*filter
,
2388 struct lttng_event_exclusion
*exclusion
,
2391 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2392 filter_expression
, filter
, exclusion
, wpipe
, false);
2396 * Enable an event which is internal to LTTng. An internal should
2397 * never be made visible to clients and are immune to checks such as
2400 static int cmd_enable_event_internal(struct ltt_session
*session
,
2401 struct lttng_domain
*domain
,
2402 char *channel_name
, struct lttng_event
*event
,
2403 char *filter_expression
,
2404 struct lttng_filter_bytecode
*filter
,
2405 struct lttng_event_exclusion
*exclusion
,
2408 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2409 filter_expression
, filter
, exclusion
, wpipe
, true);
2413 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2415 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2416 struct lttng_event
**events
)
2419 ssize_t nb_events
= 0;
2422 case LTTNG_DOMAIN_KERNEL
:
2423 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2424 if (nb_events
< 0) {
2425 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2429 case LTTNG_DOMAIN_UST
:
2430 nb_events
= ust_app_list_events(events
);
2431 if (nb_events
< 0) {
2432 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2436 case LTTNG_DOMAIN_LOG4J
:
2437 case LTTNG_DOMAIN_JUL
:
2438 case LTTNG_DOMAIN_PYTHON
:
2439 nb_events
= agent_list_events(events
, domain
);
2440 if (nb_events
< 0) {
2441 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2446 ret
= LTTNG_ERR_UND
;
2453 /* Return negative value to differentiate return code */
2458 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2460 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2461 struct lttng_event_field
**fields
)
2464 ssize_t nb_fields
= 0;
2467 case LTTNG_DOMAIN_UST
:
2468 nb_fields
= ust_app_list_event_fields(fields
);
2469 if (nb_fields
< 0) {
2470 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2474 case LTTNG_DOMAIN_KERNEL
:
2475 default: /* fall-through */
2476 ret
= LTTNG_ERR_UND
;
2483 /* Return negative value to differentiate return code */
2487 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2489 return syscall_table_list(events
);
2493 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2495 * Called with session lock held.
2497 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2498 enum lttng_domain_type domain
, int32_t **pids
)
2501 ssize_t nr_pids
= 0;
2504 case LTTNG_DOMAIN_KERNEL
:
2506 struct ltt_kernel_session
*ksess
;
2508 ksess
= session
->kernel_session
;
2509 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2511 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2516 case LTTNG_DOMAIN_UST
:
2518 struct ltt_ust_session
*usess
;
2520 usess
= session
->ust_session
;
2521 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2523 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2528 case LTTNG_DOMAIN_LOG4J
:
2529 case LTTNG_DOMAIN_JUL
:
2530 case LTTNG_DOMAIN_PYTHON
:
2532 ret
= LTTNG_ERR_UND
;
2539 /* Return negative value to differentiate return code */
2544 * Command LTTNG_START_TRACE processed by the client thread.
2546 * Called with session mutex held.
2548 int cmd_start_trace(struct ltt_session
*session
)
2550 enum lttng_error_code ret
;
2551 unsigned long nb_chan
= 0;
2552 struct ltt_kernel_session
*ksession
;
2553 struct ltt_ust_session
*usess
;
2557 /* Ease our life a bit ;) */
2558 ksession
= session
->kernel_session
;
2559 usess
= session
->ust_session
;
2561 /* Is the session already started? */
2562 if (session
->active
) {
2563 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2568 * Starting a session without channel is useless since after that it's not
2569 * possible to enable channel thus inform the client.
2571 if (usess
&& usess
->domain_global
.channels
) {
2572 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2575 nb_chan
+= ksession
->channel_count
;
2578 ret
= LTTNG_ERR_NO_CHANNEL
;
2582 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2583 struct lttng_trace_chunk
*trace_chunk
;
2585 trace_chunk
= session_create_new_trace_chunk(
2586 session
, NULL
, NULL
);
2588 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2591 assert(!session
->current_trace_chunk
);
2592 ret
= session_set_trace_chunk(session
, trace_chunk
, NULL
);
2593 lttng_trace_chunk_put(trace_chunk
);
2595 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2600 /* Kernel tracing */
2601 if (ksession
!= NULL
) {
2602 DBG("Start kernel tracing session %s", session
->name
);
2603 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2604 if (ret
!= LTTNG_OK
) {
2609 /* Flag session that trace should start automatically */
2611 int int_ret
= ust_app_start_trace_all(usess
);
2614 ret
= LTTNG_ERR_UST_START_FAIL
;
2619 /* Flag this after a successful start. */
2620 session
->has_been_started
= 1;
2621 session
->active
= 1;
2624 * Clear the flag that indicates that a rotation was done while the
2625 * session was stopped.
2627 session
->rotated_after_last_stop
= false;
2629 if (session
->rotate_timer_period
) {
2630 int int_ret
= timer_session_rotation_schedule_timer_start(
2631 session
, session
->rotate_timer_period
);
2634 ERR("Failed to enable rotate timer");
2635 ret
= LTTNG_ERR_UNK
;
2647 * Command LTTNG_STOP_TRACE processed by the client thread.
2649 int cmd_stop_trace(struct ltt_session
*session
)
2652 struct ltt_kernel_channel
*kchan
;
2653 struct ltt_kernel_session
*ksession
;
2654 struct ltt_ust_session
*usess
;
2655 bool error_occurred
= false;
2659 DBG("Begin stop session %s (id %" PRIu64
")", session
->name
, session
->id
);
2661 ksession
= session
->kernel_session
;
2662 usess
= session
->ust_session
;
2664 /* Session is not active. Skip everythong and inform the client. */
2665 if (!session
->active
) {
2666 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2671 if (ksession
&& ksession
->active
) {
2672 DBG("Stop kernel tracing");
2674 ret
= kernel_stop_session(ksession
);
2676 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2680 kernel_wait_quiescent(kernel_tracer_fd
);
2682 /* Flush metadata after stopping (if exists) */
2683 if (ksession
->metadata_stream_fd
>= 0) {
2684 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2686 ERR("Kernel metadata flush failed");
2690 /* Flush all buffers after stopping */
2691 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2692 ret
= kernel_flush_buffer(kchan
);
2694 ERR("Kernel flush buffer error");
2698 ksession
->active
= 0;
2699 DBG("Kernel session stopped %s (id %" PRIu64
")", session
->name
,
2703 if (usess
&& usess
->active
) {
2704 ret
= ust_app_stop_trace_all(usess
);
2706 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2711 /* Flag inactive after a successful stop. */
2712 session
->active
= 0;
2713 ret
= !error_occurred
? LTTNG_OK
: LTTNG_ERR_UNK
;
2720 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2722 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2723 struct lttng_uri
*uris
)
2726 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2727 struct ltt_ust_session
*usess
= session
->ust_session
;
2733 /* Can't set consumer URI if the session is active. */
2734 if (session
->active
) {
2735 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2739 /* Set the "global" consumer URIs */
2740 for (i
= 0; i
< nb_uri
; i
++) {
2741 ret
= add_uri_to_consumer(session
,
2743 &uris
[i
], LTTNG_DOMAIN_NONE
);
2744 if (ret
!= LTTNG_OK
) {
2749 /* Set UST session URIs */
2750 if (session
->ust_session
) {
2751 for (i
= 0; i
< nb_uri
; i
++) {
2752 ret
= add_uri_to_consumer(session
,
2753 session
->ust_session
->consumer
,
2754 &uris
[i
], LTTNG_DOMAIN_UST
);
2755 if (ret
!= LTTNG_OK
) {
2761 /* Set kernel session URIs */
2762 if (session
->kernel_session
) {
2763 for (i
= 0; i
< nb_uri
; i
++) {
2764 ret
= add_uri_to_consumer(session
,
2765 session
->kernel_session
->consumer
,
2766 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2767 if (ret
!= LTTNG_OK
) {
2774 * Make sure to set the session in output mode after we set URI since a
2775 * session can be created without URL (thus flagged in no output mode).
2777 session
->output_traces
= 1;
2779 ksess
->output_traces
= 1;
2783 usess
->output_traces
= 1;
2794 enum lttng_error_code
set_session_output_from_descriptor(
2795 struct ltt_session
*session
,
2796 const struct lttng_session_descriptor
*descriptor
)
2799 enum lttng_error_code ret_code
= LTTNG_OK
;
2800 enum lttng_session_descriptor_type session_type
=
2801 lttng_session_descriptor_get_type(descriptor
);
2802 enum lttng_session_descriptor_output_type output_type
=
2803 lttng_session_descriptor_get_output_type(descriptor
);
2804 struct lttng_uri uris
[2] = {};
2805 size_t uri_count
= 0;
2807 switch (output_type
) {
2808 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2810 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2811 lttng_session_descriptor_get_local_output_uri(descriptor
,
2815 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2816 lttng_session_descriptor_get_network_output_uris(descriptor
,
2817 &uris
[0], &uris
[1]);
2821 ret_code
= LTTNG_ERR_INVALID
;
2825 switch (session_type
) {
2826 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2828 struct snapshot_output
*new_output
= NULL
;
2830 new_output
= snapshot_output_alloc();
2832 ret_code
= LTTNG_ERR_NOMEM
;
2836 ret
= snapshot_output_init_with_uri(session
,
2837 DEFAULT_SNAPSHOT_MAX_SIZE
,
2838 NULL
, uris
, uri_count
, session
->consumer
,
2839 new_output
, &session
->snapshot
);
2841 ret_code
= (ret
== -ENOMEM
) ?
2842 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2843 snapshot_output_destroy(new_output
);
2846 snapshot_add_output(&session
->snapshot
, new_output
);
2849 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2850 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2852 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2856 ret_code
= LTTNG_ERR_INVALID
;
2864 enum lttng_error_code
cmd_create_session_from_descriptor(
2865 struct lttng_session_descriptor
*descriptor
,
2866 const lttng_sock_cred
*creds
,
2867 const char *home_path
)
2870 enum lttng_error_code ret_code
;
2871 const char *session_name
;
2872 struct ltt_session
*new_session
= NULL
;
2873 enum lttng_session_descriptor_status descriptor_status
;
2875 session_lock_list();
2877 if (*home_path
!= '/') {
2878 ERR("Home path provided by client is not absolute");
2879 ret_code
= LTTNG_ERR_INVALID
;
2884 descriptor_status
= lttng_session_descriptor_get_session_name(
2885 descriptor
, &session_name
);
2886 switch (descriptor_status
) {
2887 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
2889 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
2890 session_name
= NULL
;
2893 ret_code
= LTTNG_ERR_INVALID
;
2896 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
2898 if (ret_code
!= LTTNG_OK
) {
2902 if (!session_name
) {
2903 ret
= lttng_session_descriptor_set_session_name(descriptor
,
2906 ret_code
= LTTNG_ERR_SESSION_FAIL
;
2911 if (!lttng_session_descriptor_is_output_destination_initialized(
2914 * Only include the session's creation time in the output
2915 * destination if the name of the session itself was
2916 * not auto-generated.
2918 ret_code
= lttng_session_descriptor_set_default_output(
2920 session_name
? &new_session
->creation_time
: NULL
,
2922 if (ret_code
!= LTTNG_OK
) {
2926 new_session
->has_user_specified_directory
=
2927 lttng_session_descriptor_has_output_directory(
2931 switch (lttng_session_descriptor_get_type(descriptor
)) {
2932 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2933 new_session
->snapshot_mode
= 1;
2935 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2936 new_session
->live_timer
=
2937 lttng_session_descriptor_live_get_timer_interval(
2944 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
2945 if (ret_code
!= LTTNG_OK
) {
2948 new_session
->consumer
->enabled
= 1;
2949 ret_code
= LTTNG_OK
;
2951 /* Release reference provided by the session_create function. */
2952 session_put(new_session
);
2953 if (ret_code
!= LTTNG_OK
&& new_session
) {
2954 /* Release the global reference on error. */
2955 session_destroy(new_session
);
2957 session_unlock_list();
2961 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
2962 struct lttng_session_descriptor
**return_descriptor
)
2965 size_t payload_size
;
2966 struct lttng_dynamic_buffer payload
;
2967 struct lttng_buffer_view home_dir_view
;
2968 struct lttng_buffer_view session_descriptor_view
;
2969 struct lttng_session_descriptor
*session_descriptor
= NULL
;
2970 enum lttng_error_code ret_code
;
2972 lttng_dynamic_buffer_init(&payload
);
2973 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
2975 ret_code
= LTTNG_ERR_INVALID
;
2978 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
2979 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
2980 ret_code
= LTTNG_ERR_INVALID
;
2984 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
2985 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
2986 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
2988 ret_code
= LTTNG_ERR_NOMEM
;
2992 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
2994 ERR("Reception of session descriptor failed, aborting.");
2995 ret_code
= LTTNG_ERR_SESSION_FAIL
;
2999 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3002 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3003 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3005 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3006 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3008 ret
= lttng_session_descriptor_create_from_buffer(
3009 &session_descriptor_view
, &session_descriptor
);
3011 ERR("Failed to create session descriptor from payload of \"create session\" command");
3012 ret_code
= LTTNG_ERR_INVALID
;
3017 * Sets the descriptor's auto-generated properties (name, output) if
3020 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3022 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3023 if (ret_code
!= LTTNG_OK
) {
3027 ret_code
= LTTNG_OK
;
3028 *return_descriptor
= session_descriptor
;
3029 session_descriptor
= NULL
;
3031 lttng_dynamic_buffer_reset(&payload
);
3032 lttng_session_descriptor_destroy(session_descriptor
);
3037 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3038 void *_reply_context
)
3042 const struct cmd_destroy_session_reply_context
*reply_context
=
3044 struct lttng_dynamic_buffer payload
;
3045 struct lttcomm_session_destroy_command_header cmd_header
;
3046 struct lttng_trace_archive_location
*location
= NULL
;
3047 struct lttcomm_lttng_msg llm
= {
3048 .cmd_type
= LTTNG_DESTROY_SESSION
,
3049 .ret_code
= LTTNG_OK
,
3052 sizeof(struct lttcomm_session_destroy_command_header
),
3055 size_t payload_size_before_location
;
3057 lttng_dynamic_buffer_init(&payload
);
3059 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3061 ERR("Failed to append session destruction message");
3065 cmd_header
.rotation_state
=
3066 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3067 session
->rotation_state
:
3068 LTTNG_ROTATION_STATE_NO_ROTATION
);
3069 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3070 sizeof(cmd_header
));
3072 ERR("Failed to append session destruction command header");
3076 if (!reply_context
->implicit_rotation_on_destroy
) {
3077 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3081 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3082 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3087 location
= session_get_trace_archive_location(session
);
3089 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3094 payload_size_before_location
= payload
.size
;
3095 comm_ret
= lttng_trace_archive_location_serialize(location
,
3098 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3102 /* Update the message to indicate the location's length. */
3103 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3104 payload
.size
- payload_size_before_location
;
3106 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3107 payload
.data
, payload
.size
);
3108 if (comm_ret
!= (ssize_t
) payload
.size
) {
3109 ERR("Failed to send result of the destruction of session \"%s\" to client",
3113 ret
= close(reply_context
->reply_sock_fd
);
3115 PERROR("Failed to close client socket in deferred session destroy reply");
3117 lttng_dynamic_buffer_reset(&payload
);
3118 free(_reply_context
);
3122 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3124 * Called with session lock held.
3126 int cmd_destroy_session(struct ltt_session
*session
,
3127 struct notification_thread_handle
*notification_thread_handle
,
3131 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3134 reply_context
= zmalloc(sizeof(*reply_context
));
3135 if (!reply_context
) {
3136 ret
= LTTNG_ERR_NOMEM
;
3139 reply_context
->reply_sock_fd
= *sock_fd
;
3145 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3147 if (session
->active
) {
3148 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3150 ret
= cmd_stop_trace(session
);
3151 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3152 /* Carry on with the destruction of the session. */
3153 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3154 session
->name
, lttng_strerror(-ret
));
3158 if (session
->rotation_schedule_timer_enabled
) {
3159 if (timer_session_rotation_schedule_timer_stop(
3161 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3166 if (session
->rotate_size
) {
3167 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3168 session
->rotate_size
= 0;
3171 if (session
->most_recent_chunk_id
.is_set
&&
3172 session
->most_recent_chunk_id
.value
!= 0 &&
3173 session
->current_trace_chunk
) {
3174 ret
= cmd_rotate_session(session
, NULL
);
3175 if (ret
!= LTTNG_OK
) {
3176 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3177 session
->name
, lttng_strerror(-ret
));
3179 if (reply_context
) {
3180 reply_context
->implicit_rotation_on_destroy
= true;
3184 if (session
->shm_path
[0]) {
3186 * When a session is created with an explicit shm_path,
3187 * the consumer daemon will create its shared memory files
3188 * at that location and will *not* unlink them. This is normal
3189 * as the intention of that feature is to make it possible
3190 * to retrieve the content of those files should a crash occur.
3192 * To ensure the content of those files can be used, the
3193 * sessiond daemon will replicate the content of the metadata
3194 * cache in a metadata file.
3196 * On clean-up, it is expected that the consumer daemon will
3197 * unlink the shared memory files and that the session daemon
3198 * will unlink the metadata file. Then, the session's directory
3199 * in the shm path can be removed.
3201 * Unfortunately, a flaw in the design of the sessiond's and
3202 * consumerd's tear down of channels makes it impossible to
3203 * determine when the sessiond _and_ the consumerd have both
3204 * destroyed their representation of a channel. For one, the
3205 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3206 * callbacks in both daemons.
3208 * However, it is also impossible for the sessiond to know when
3209 * the consumer daemon is done destroying its channel(s) since
3210 * it occurs as a reaction to the closing of the channel's file
3211 * descriptor. There is no resulting communication initiated
3212 * from the consumerd to the sessiond to confirm that the
3213 * operation is completed (and was successful).
3215 * Until this is all fixed, the session daemon checks for the
3216 * removal of the session's shm path which makes it possible
3217 * to safely advertise a session as having been destroyed.
3219 * Prior to this fix, it was not possible to reliably save
3220 * a session making use of the --shm-path option, destroy it,
3221 * and load it again. This is because the creation of the
3222 * session would fail upon seeing the session's shm path
3223 * already in existence.
3225 * Note that none of the error paths in the check for the
3226 * directory's existence return an error. This is normal
3227 * as there isn't much that can be done. The session will
3228 * be destroyed properly, except that we can't offer the
3229 * guarantee that the same session can be re-created.
3231 current_completion_handler
= &destroy_completion_handler
.handler
;
3232 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3234 sizeof(destroy_completion_handler
.shm_path
));
3239 * The session is destroyed. However, note that the command context
3240 * still holds a reference to the session, thus delaying its destruction
3241 * _at least_ up to the point when that reference is released.
3243 session_destroy(session
);
3244 if (reply_context
) {
3245 ret
= session_add_destroy_notifier(session
,
3246 cmd_destroy_session_reply
,
3247 (void *) reply_context
);
3249 ret
= LTTNG_ERR_FATAL
;
3261 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3263 int cmd_register_consumer(struct ltt_session
*session
,
3264 enum lttng_domain_type domain
, const char *sock_path
,
3265 struct consumer_data
*cdata
)
3268 struct consumer_socket
*socket
= NULL
;
3275 case LTTNG_DOMAIN_KERNEL
:
3277 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3281 /* Can't register a consumer if there is already one */
3282 if (ksess
->consumer_fds_sent
!= 0) {
3283 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3287 sock
= lttcomm_connect_unix_sock(sock_path
);
3289 ret
= LTTNG_ERR_CONNECT_FAIL
;
3292 cdata
->cmd_sock
= sock
;
3294 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3295 if (socket
== NULL
) {
3298 PERROR("close register consumer");
3300 cdata
->cmd_sock
= -1;
3301 ret
= LTTNG_ERR_FATAL
;
3305 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3306 if (socket
->lock
== NULL
) {
3307 PERROR("zmalloc pthread mutex");
3308 ret
= LTTNG_ERR_FATAL
;
3311 pthread_mutex_init(socket
->lock
, NULL
);
3312 socket
->registered
= 1;
3315 consumer_add_socket(socket
, ksess
->consumer
);
3318 pthread_mutex_lock(&cdata
->pid_mutex
);
3320 pthread_mutex_unlock(&cdata
->pid_mutex
);
3325 /* TODO: Userspace tracing */
3326 ret
= LTTNG_ERR_UND
;
3334 consumer_destroy_socket(socket
);
3340 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3342 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3343 struct lttng_domain
**domains
)
3348 struct lttng_ht_iter iter
;
3350 if (session
->kernel_session
!= NULL
) {
3351 DBG3("Listing domains found kernel domain");
3355 if (session
->ust_session
!= NULL
) {
3356 DBG3("Listing domains found UST global domain");
3360 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3362 if (agt
->being_used
) {
3373 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3374 if (*domains
== NULL
) {
3375 ret
= LTTNG_ERR_FATAL
;
3379 if (session
->kernel_session
!= NULL
) {
3380 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3382 /* Kernel session buffer type is always GLOBAL */
3383 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3388 if (session
->ust_session
!= NULL
) {
3389 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3390 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3394 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3396 if (agt
->being_used
) {
3397 (*domains
)[index
].type
= agt
->domain
;
3398 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3408 /* Return negative value to differentiate return code */
3414 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3416 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3417 struct ltt_session
*session
, struct lttng_channel
**channels
)
3419 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3422 case LTTNG_DOMAIN_KERNEL
:
3423 if (session
->kernel_session
!= NULL
) {
3424 nb_chan
= session
->kernel_session
->channel_count
;
3426 DBG3("Number of kernel channels %zd", nb_chan
);
3428 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3432 case LTTNG_DOMAIN_UST
:
3433 if (session
->ust_session
!= NULL
) {
3435 nb_chan
= lttng_ht_get_count(
3436 session
->ust_session
->domain_global
.channels
);
3439 DBG3("Number of UST global channels %zd", nb_chan
);
3441 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3446 ret
= -LTTNG_ERR_UND
;
3451 const size_t channel_size
= sizeof(struct lttng_channel
) +
3452 sizeof(struct lttng_channel_extended
);
3453 struct lttng_channel_extended
*channel_exts
;
3455 payload_size
= nb_chan
* channel_size
;
3456 *channels
= zmalloc(payload_size
);
3457 if (*channels
== NULL
) {
3458 ret
= -LTTNG_ERR_FATAL
;
3462 channel_exts
= ((void *) *channels
) +
3463 (nb_chan
* sizeof(struct lttng_channel
));
3464 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3465 if (ret
!= LTTNG_OK
) {
3480 * Command LTTNG_LIST_EVENTS processed by the client thread.
3482 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3483 struct ltt_session
*session
, char *channel_name
,
3484 struct lttng_event
**events
, size_t *total_size
)
3487 ssize_t nb_event
= 0;
3490 case LTTNG_DOMAIN_KERNEL
:
3491 if (session
->kernel_session
!= NULL
) {
3492 nb_event
= list_lttng_kernel_events(channel_name
,
3493 session
->kernel_session
, events
,
3497 case LTTNG_DOMAIN_UST
:
3499 if (session
->ust_session
!= NULL
) {
3500 nb_event
= list_lttng_ust_global_events(channel_name
,
3501 &session
->ust_session
->domain_global
, events
,
3506 case LTTNG_DOMAIN_LOG4J
:
3507 case LTTNG_DOMAIN_JUL
:
3508 case LTTNG_DOMAIN_PYTHON
:
3509 if (session
->ust_session
) {
3510 struct lttng_ht_iter iter
;
3514 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3515 &iter
.iter
, agt
, node
.node
) {
3516 if (agt
->domain
== domain
) {
3517 nb_event
= list_lttng_agent_events(
3527 ret
= LTTNG_ERR_UND
;
3534 /* Return negative value to differentiate return code */
3539 * Using the session list, filled a lttng_session array to send back to the
3540 * client for session listing.
3542 * The session list lock MUST be acquired before calling this function. Use
3543 * session_lock_list() and session_unlock_list().
3545 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3546 size_t session_count
, uid_t uid
, gid_t gid
)
3550 struct ltt_session
*session
;
3551 struct ltt_session_list
*list
= session_get_list();
3552 struct lttng_session_extended
*extended
=
3553 (typeof(extended
)) (&sessions
[session_count
]);
3555 DBG("Getting all available session for UID %d GID %d",
3558 * Iterate over session list and append data after the control struct in
3561 cds_list_for_each_entry(session
, &list
->head
, list
) {
3562 if (!session_get(session
)) {
3566 * Only list the sessions the user can control.
3568 if (!session_access_ok(session
, uid
, gid
) ||
3569 session
->destroyed
) {
3570 session_put(session
);
3574 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3575 struct ltt_ust_session
*usess
= session
->ust_session
;
3577 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3578 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3579 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3580 ret
= build_network_session_path(sessions
[i
].path
,
3581 sizeof(sessions
[i
].path
), session
);
3583 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3584 session
->consumer
->dst
.session_root_path
);
3587 PERROR("snprintf session path");
3588 session_put(session
);
3592 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3593 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3594 sessions
[i
].enabled
= session
->active
;
3595 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3596 sessions
[i
].live_timer_interval
= session
->live_timer
;
3597 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3598 extended
[i
].creation_time
.is_set
= 1;
3600 session_put(session
);
3605 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3606 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3608 int cmd_data_pending(struct ltt_session
*session
)
3611 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3612 struct ltt_ust_session
*usess
= session
->ust_session
;
3616 DBG("Data pending for session %s", session
->name
);
3618 /* Session MUST be stopped to ask for data availability. */
3619 if (session
->active
) {
3620 ret
= LTTNG_ERR_SESSION_STARTED
;
3624 * If stopped, just make sure we've started before else the above call
3625 * will always send that there is data pending.
3627 * The consumer assumes that when the data pending command is received,
3628 * the trace has been started before or else no output data is written
3629 * by the streams which is a condition for data pending. So, this is
3630 * *VERY* important that we don't ask the consumer before a start
3633 if (!session
->has_been_started
) {
3639 /* A rotation is still pending, we have to wait. */
3640 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3641 DBG("Rotate still pending for session %s", session
->name
);
3646 if (ksess
&& ksess
->consumer
) {
3647 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3649 /* Data is still being extracted for the kernel. */
3654 if (usess
&& usess
->consumer
) {
3655 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3657 /* Data is still being extracted for the kernel. */
3662 /* Data is ready to be read by a viewer */
3670 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3672 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3674 int cmd_snapshot_add_output(struct ltt_session
*session
,
3675 struct lttng_snapshot_output
*output
, uint32_t *id
)
3678 struct snapshot_output
*new_output
;
3683 DBG("Cmd snapshot add output for session %s", session
->name
);
3686 * Can't create an output if the session is not set in no-output mode.
3688 if (session
->output_traces
) {
3689 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3693 if (session
->has_non_mmap_channel
) {
3694 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3698 /* Only one output is allowed until we have the "tee" feature. */
3699 if (session
->snapshot
.nb_output
== 1) {
3700 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3704 new_output
= snapshot_output_alloc();
3706 ret
= LTTNG_ERR_NOMEM
;
3710 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3711 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3712 &session
->snapshot
);
3714 if (ret
== -ENOMEM
) {
3715 ret
= LTTNG_ERR_NOMEM
;
3717 ret
= LTTNG_ERR_INVALID
;
3723 snapshot_add_output(&session
->snapshot
, new_output
);
3725 *id
= new_output
->id
;
3732 snapshot_output_destroy(new_output
);
3738 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3740 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3742 int cmd_snapshot_del_output(struct ltt_session
*session
,
3743 struct lttng_snapshot_output
*output
)
3746 struct snapshot_output
*sout
= NULL
;
3754 * Permission denied to create an output if the session is not
3755 * set in no output mode.
3757 if (session
->output_traces
) {
3758 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3763 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3765 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3766 } else if (*output
->name
!= '\0') {
3767 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3769 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3772 ret
= LTTNG_ERR_INVALID
;
3776 snapshot_delete_output(&session
->snapshot
, sout
);
3777 snapshot_output_destroy(sout
);
3786 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3788 * If no output is available, outputs is untouched and 0 is returned.
3790 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3792 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3793 struct lttng_snapshot_output
**outputs
)
3796 struct lttng_snapshot_output
*list
= NULL
;
3797 struct lttng_ht_iter iter
;
3798 struct snapshot_output
*output
;
3803 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3806 * Permission denied to create an output if the session is not
3807 * set in no output mode.
3809 if (session
->output_traces
) {
3810 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3814 if (session
->snapshot
.nb_output
== 0) {
3819 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3821 ret
= -LTTNG_ERR_NOMEM
;
3825 /* Copy list from session to the new list object. */
3827 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3828 output
, node
.node
) {
3829 assert(output
->consumer
);
3830 list
[idx
].id
= output
->id
;
3831 list
[idx
].max_size
= output
->max_size
;
3832 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3833 sizeof(list
[idx
].name
))) {
3834 ret
= -LTTNG_ERR_INVALID
;
3837 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3838 if (lttng_strncpy(list
[idx
].ctrl_url
,
3839 output
->consumer
->dst
.session_root_path
,
3840 sizeof(list
[idx
].ctrl_url
))) {
3841 ret
= -LTTNG_ERR_INVALID
;
3846 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3847 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3849 ret
= -LTTNG_ERR_NOMEM
;
3854 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3855 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3857 ret
= -LTTNG_ERR_NOMEM
;
3866 ret
= session
->snapshot
.nb_output
;
3875 * Check if we can regenerate the metadata for this session.
3876 * Only kernel, UST per-uid and non-live sessions are supported.
3878 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3881 int check_regenerate_metadata_support(struct ltt_session
*session
)
3887 if (session
->live_timer
!= 0) {
3888 ret
= LTTNG_ERR_LIVE_SESSION
;
3891 if (!session
->active
) {
3892 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3895 if (session
->ust_session
) {
3896 switch (session
->ust_session
->buffer_type
) {
3897 case LTTNG_BUFFER_PER_UID
:
3899 case LTTNG_BUFFER_PER_PID
:
3900 ret
= LTTNG_ERR_PER_PID_SESSION
;
3904 ret
= LTTNG_ERR_UNK
;
3908 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3909 session
->consumer
->relay_minor_version
< 8) {
3910 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3920 int clear_metadata_file(int fd
)
3925 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
3926 if (lseek_ret
< 0) {
3932 ret
= ftruncate(fd
, 0);
3934 PERROR("ftruncate");
3943 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3946 struct buffer_reg_uid
*uid_reg
= NULL
;
3947 struct buffer_reg_session
*session_reg
= NULL
;
3950 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3951 struct ust_registry_session
*registry
;
3952 struct ust_registry_channel
*chan
;
3953 struct lttng_ht_iter iter_chan
;
3955 session_reg
= uid_reg
->registry
;
3956 registry
= session_reg
->reg
.ust
;
3958 pthread_mutex_lock(®istry
->lock
);
3959 registry
->metadata_len_sent
= 0;
3960 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3961 registry
->metadata_len
= 0;
3962 registry
->metadata_version
++;
3963 if (registry
->metadata_fd
> 0) {
3964 /* Clear the metadata file's content. */
3965 ret
= clear_metadata_file(registry
->metadata_fd
);
3967 pthread_mutex_unlock(®istry
->lock
);
3972 ret
= ust_metadata_session_statedump(registry
, NULL
,
3973 registry
->major
, registry
->minor
);
3975 pthread_mutex_unlock(®istry
->lock
);
3976 ERR("Failed to generate session metadata (err = %d)",
3980 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3982 struct ust_registry_event
*event
;
3983 struct lttng_ht_iter iter_event
;
3985 ret
= ust_metadata_channel_statedump(registry
, chan
);
3987 pthread_mutex_unlock(®istry
->lock
);
3988 ERR("Failed to generate channel metadata "
3992 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3994 ret
= ust_metadata_event_statedump(registry
,
3997 pthread_mutex_unlock(®istry
->lock
);
3998 ERR("Failed to generate event metadata "
4004 pthread_mutex_unlock(®istry
->lock
);
4013 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4015 * Ask the consumer to truncate the existing metadata file(s) and
4016 * then regenerate the metadata. Live and per-pid sessions are not
4017 * supported and return an error.
4019 * Return 0 on success or else a LTTNG_ERR code.
4021 int cmd_regenerate_metadata(struct ltt_session
*session
)
4027 ret
= check_regenerate_metadata_support(session
);
4032 if (session
->kernel_session
) {
4033 ret
= kernctl_session_regenerate_metadata(
4034 session
->kernel_session
->fd
);
4036 ERR("Failed to regenerate the kernel metadata");
4041 if (session
->ust_session
) {
4042 ret
= ust_regenerate_metadata(session
->ust_session
);
4044 ERR("Failed to regenerate the UST metadata");
4048 DBG("Cmd metadata regenerate for session %s", session
->name
);
4056 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4058 * Ask the tracer to regenerate a new statedump.
4060 * Return 0 on success or else a LTTNG_ERR code.
4062 int cmd_regenerate_statedump(struct ltt_session
*session
)
4068 if (!session
->active
) {
4069 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4073 if (session
->kernel_session
) {
4074 ret
= kernctl_session_regenerate_statedump(
4075 session
->kernel_session
->fd
);
4077 * Currently, the statedump in kernel can only fail if out
4081 if (ret
== -ENOMEM
) {
4082 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4084 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4086 ERR("Failed to regenerate the kernel statedump");
4091 if (session
->ust_session
) {
4092 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4094 * Currently, the statedump in UST always returns 0.
4097 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4098 ERR("Failed to regenerate the UST statedump");
4102 DBG("Cmd regenerate statedump for session %s", session
->name
);
4109 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4110 struct notification_thread_handle
*notification_thread
)
4114 ssize_t sock_recv_len
;
4115 struct lttng_trigger
*trigger
= NULL
;
4116 struct lttng_buffer_view view
;
4117 struct lttng_dynamic_buffer trigger_buffer
;
4119 lttng_dynamic_buffer_init(&trigger_buffer
);
4120 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4121 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4123 ret
= LTTNG_ERR_NOMEM
;
4127 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4129 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4130 ERR("Failed to receive \"register trigger\" command payload");
4131 /* TODO: should this be a new error enum ? */
4132 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4136 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4137 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4139 ERR("Invalid trigger payload received in \"register trigger\" command");
4140 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4144 ret
= notification_thread_command_register_trigger(notification_thread
,
4146 /* Ownership of trigger was transferred. */
4149 lttng_trigger_destroy(trigger
);
4150 lttng_dynamic_buffer_reset(&trigger_buffer
);
4154 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4155 struct notification_thread_handle
*notification_thread
)
4159 ssize_t sock_recv_len
;
4160 struct lttng_trigger
*trigger
= NULL
;
4161 struct lttng_buffer_view view
;
4162 struct lttng_dynamic_buffer trigger_buffer
;
4164 lttng_dynamic_buffer_init(&trigger_buffer
);
4165 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4166 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4168 ret
= LTTNG_ERR_NOMEM
;
4172 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4174 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4175 ERR("Failed to receive \"unregister trigger\" command payload");
4176 /* TODO: should this be a new error enum ? */
4177 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4181 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4182 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4184 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4185 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4189 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4192 lttng_trigger_destroy(trigger
);
4193 lttng_dynamic_buffer_reset(&trigger_buffer
);
4198 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4199 * snapshot output is *not* set with a remote destination.
4201 * Return LTTNG_OK on success or a LTTNG_ERR code.
4203 static enum lttng_error_code
set_relayd_for_snapshot(
4204 struct consumer_output
*consumer
,
4205 const struct snapshot_output
*snap_output
,
4206 const struct ltt_session
*session
)
4208 enum lttng_error_code status
= LTTNG_OK
;
4209 struct lttng_ht_iter iter
;
4210 struct consumer_socket
*socket
;
4211 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4214 assert(snap_output
);
4217 DBG2("Set relayd object from snapshot output");
4219 if (session
->current_trace_chunk
) {
4220 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
4221 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
4223 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4224 current_chunk_id
.is_set
= true;
4226 ERR("Failed to get current trace chunk id");
4227 status
= LTTNG_ERR_UNK
;
4232 /* Ignore if snapshot consumer output is not network. */
4233 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
4238 * For each consumer socket, create and send the relayd object of the
4242 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
4243 socket
, node
.node
) {
4244 pthread_mutex_lock(socket
->lock
);
4245 status
= send_consumer_relayd_sockets(0, session
->id
,
4246 snap_output
->consumer
, socket
,
4247 session
->name
, session
->hostname
,
4248 session
->live_timer
,
4249 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4250 session
->creation_time
);
4251 pthread_mutex_unlock(socket
->lock
);
4252 if (status
!= LTTNG_OK
) {
4264 * Record a kernel snapshot.
4266 * Return LTTNG_OK on success or a LTTNG_ERR code.
4268 static enum lttng_error_code
record_kernel_snapshot(
4269 struct ltt_kernel_session
*ksess
,
4270 const struct snapshot_output
*output
,
4271 const struct ltt_session
*session
,
4272 int wait
, uint64_t nb_packets_per_stream
)
4275 enum lttng_error_code status
;
4282 * Copy kernel session sockets so we can communicate with the right
4283 * consumer for the snapshot record command.
4285 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
4287 status
= LTTNG_ERR_NOMEM
;
4291 status
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
4292 if (status
!= LTTNG_OK
) {
4293 goto error_snapshot
;
4296 status
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
4297 if (status
!= LTTNG_OK
) {
4298 goto error_snapshot
;
4304 /* Clean up copied sockets so this output can use some other later on. */
4305 consumer_destroy_output_sockets(output
->consumer
);
4312 * Record a UST snapshot.
4314 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4316 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4317 const struct snapshot_output
*output
,
4318 const struct ltt_session
*session
, int wait
,
4319 uint64_t nb_packets_per_stream
)
4322 enum lttng_error_code status
;
4329 * Copy UST session sockets so we can communicate with the right
4330 * consumer for the snapshot record command.
4332 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
4334 status
= LTTNG_ERR_NOMEM
;
4338 status
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
4339 if (status
!= LTTNG_OK
) {
4340 goto error_snapshot
;
4343 status
= ust_app_snapshot_record(usess
, output
, wait
,
4344 nb_packets_per_stream
);
4345 if (status
!= LTTNG_OK
) {
4346 goto error_snapshot
;
4352 /* Clean up copied sockets so this output can use some other later on. */
4353 consumer_destroy_output_sockets(output
->consumer
);
4360 uint64_t get_session_size_one_more_packet_per_stream(
4361 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4363 uint64_t tot_size
= 0;
4365 if (session
->kernel_session
) {
4366 struct ltt_kernel_channel
*chan
;
4367 const struct ltt_kernel_session
*ksess
=
4368 session
->kernel_session
;
4370 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4371 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4373 * Don't take channel into account if we
4374 * already grab all its packets.
4378 tot_size
+= chan
->channel
->attr
.subbuf_size
4379 * chan
->stream_count
;
4383 if (session
->ust_session
) {
4384 const struct ltt_ust_session
*usess
= session
->ust_session
;
4386 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4394 * Calculate the number of packets we can grab from each stream that
4395 * fits within the overall snapshot max size.
4397 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4398 * the number of packets per stream.
4400 * TODO: this approach is not perfect: we consider the worse case
4401 * (packet filling the sub-buffers) as an upper bound, but we could do
4402 * better if we do this calculation while we actually grab the packet
4403 * content: we would know how much padding we don't actually store into
4406 * This algorithm is currently bounded by the number of packets per
4409 * Since we call this algorithm before actually grabbing the data, it's
4410 * an approximation: for instance, applications could appear/disappear
4411 * in between this call and actually grabbing data.
4414 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4418 uint64_t cur_nb_packets
= 0;
4421 return 0; /* Infinite */
4424 size_left
= max_size
;
4426 uint64_t one_more_packet_tot_size
;
4428 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4429 session
, cur_nb_packets
);
4430 if (!one_more_packet_tot_size
) {
4431 /* We are already grabbing all packets. */
4434 size_left
-= one_more_packet_tot_size
;
4435 if (size_left
< 0) {
4440 if (!cur_nb_packets
) {
4441 /* Not enough room to grab one packet of each stream, error. */
4444 return cur_nb_packets
;
4448 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4449 const struct snapshot_output
*snapshot_output
, int wait
)
4452 int64_t nb_packets_per_stream
;
4453 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4454 enum lttng_error_code ret
= LTTNG_OK
;
4455 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4457 fmt_ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4459 snapshot_output
->name
,
4460 snapshot_output
->datetime
,
4461 snapshot_output
->nb_snapshot
);
4462 if (fmt_ret
< 0 || fmt_ret
>= sizeof(snapshot_chunk_name
)) {
4463 ERR("Failed to format snapshot name");
4464 ret
= LTTNG_ERR_INVALID
;
4467 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4468 snapshot_output
->name
, session
->name
,
4469 snapshot_chunk_name
);
4470 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4471 snapshot_output_get_base_path(snapshot_output
),
4472 snapshot_chunk_name
);
4473 if (!snapshot_trace_chunk
) {
4474 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
4477 assert(!session
->current_trace_chunk
);
4478 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4479 lttng_trace_chunk_put(snapshot_trace_chunk
);
4480 snapshot_trace_chunk
= NULL
;
4482 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4486 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4487 snapshot_output
->max_size
);
4488 if (nb_packets_per_stream
< 0) {
4489 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4493 if (session
->kernel_session
) {
4494 ret
= record_kernel_snapshot(session
->kernel_session
,
4495 snapshot_output
, session
,
4496 wait
, nb_packets_per_stream
);
4497 if (ret
!= LTTNG_OK
) {
4502 if (session
->ust_session
) {
4503 ret
= record_ust_snapshot(session
->ust_session
,
4504 snapshot_output
, session
,
4505 wait
, nb_packets_per_stream
);
4506 if (ret
!= LTTNG_OK
) {
4511 if (session_close_trace_chunk(
4512 session
, session
->current_trace_chunk
, NULL
)) {
4514 * Don't goto end; make sure the chunk is closed for the session
4515 * to allow future snapshots.
4517 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4521 if (session_set_trace_chunk(session
, NULL
, NULL
)) {
4522 ERR("Failed to release the current trace chunk of session \"%s\"",
4531 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4533 * The wait parameter is ignored so this call always wait for the snapshot to
4534 * complete before returning.
4536 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4538 int cmd_snapshot_record(struct ltt_session
*session
,
4539 struct lttng_snapshot_output
*output
, int wait
)
4541 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4543 unsigned int use_tmp_output
= 0;
4544 struct snapshot_output tmp_output
;
4545 unsigned int snapshot_success
= 0;
4551 DBG("Cmd snapshot record for session %s", session
->name
);
4553 /* Get the datetime for the snapshot output directory. */
4554 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4557 cmd_ret
= LTTNG_ERR_INVALID
;
4562 * Permission denied to create an output if the session is not
4563 * set in no output mode.
4565 if (session
->output_traces
) {
4566 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4570 /* The session needs to be started at least once. */
4571 if (!session
->has_been_started
) {
4572 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4576 /* Use temporary output for the session. */
4577 if (*output
->ctrl_url
!= '\0') {
4578 ret
= snapshot_output_init(session
, output
->max_size
,
4580 output
->ctrl_url
, output
->data_url
,
4584 if (ret
== -ENOMEM
) {
4585 cmd_ret
= LTTNG_ERR_NOMEM
;
4587 cmd_ret
= LTTNG_ERR_INVALID
;
4591 /* Use the global session count for the temporary snapshot. */
4592 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4594 /* Use the global datetime */
4595 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4599 if (use_tmp_output
) {
4600 cmd_ret
= snapshot_record(session
, &tmp_output
, wait
);
4601 if (cmd_ret
!= LTTNG_OK
) {
4604 snapshot_success
= 1;
4606 struct snapshot_output
*sout
;
4607 struct lttng_ht_iter iter
;
4610 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4611 &iter
.iter
, sout
, node
.node
) {
4613 * Make a local copy of the output and assign the
4614 * possible temporary value given by the caller.
4616 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
4618 if (output
->max_size
!= (uint64_t) -1ULL) {
4619 tmp_output
.max_size
= output
->max_size
;
4622 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4623 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4625 /* Use temporary name. */
4626 if (*output
->name
!= '\0') {
4627 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4628 sizeof(tmp_output
.name
))) {
4629 cmd_ret
= LTTNG_ERR_INVALID
;
4635 cmd_ret
= snapshot_record(session
, &tmp_output
, wait
);
4636 if (cmd_ret
!= LTTNG_OK
) {
4640 snapshot_success
= 1;
4645 if (snapshot_success
) {
4646 session
->snapshot
.nb_snapshot
++;
4648 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4656 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4658 int cmd_set_session_shm_path(struct ltt_session
*session
,
4659 const char *shm_path
)
4665 * Can only set shm path before session is started.
4667 if (session
->has_been_started
) {
4668 return LTTNG_ERR_SESSION_STARTED
;
4671 strncpy(session
->shm_path
, shm_path
,
4672 sizeof(session
->shm_path
));
4673 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4679 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4681 * Ask the consumer to rotate the session output directory.
4682 * The session lock must be held.
4684 * Returns LTTNG_OK on success or else a negative LTTng error code.
4686 int cmd_rotate_session(struct ltt_session
*session
,
4687 struct lttng_rotate_session_return
*rotate_return
)
4690 uint64_t ongoing_rotation_chunk_id
;
4691 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4692 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
4693 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
4694 enum lttng_trace_chunk_status chunk_status
;
4698 if (!session
->has_been_started
) {
4699 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4703 if (session
->live_timer
|| !session
->output_traces
) {
4704 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4708 /* Unsupported feature in lttng-relayd before 2.11. */
4709 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4710 (session
->consumer
->relay_major_version
== 2 &&
4711 session
->consumer
->relay_minor_version
< 11)) {
4712 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
4716 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4717 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
4719 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
4724 * After a stop, we only allow one rotation to occur, the other ones are
4725 * useless until a new start.
4727 if (session
->rotated_after_last_stop
) {
4728 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4730 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
4734 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
4736 if (session
->active
) {
4737 new_trace_chunk
= session_create_new_trace_chunk(session
,
4739 if (!new_trace_chunk
) {
4740 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
4745 /* The current trace chunk becomes the chunk being archived. */
4746 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
4747 &chunk_being_archived
);
4749 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4753 assert(chunk_being_archived
);
4754 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
4755 &ongoing_rotation_chunk_id
);
4756 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
4758 if (session
->kernel_session
) {
4759 cmd_ret
= kernel_rotate_session(session
);
4760 if (cmd_ret
!= LTTNG_OK
) {
4764 if (session
->ust_session
) {
4765 cmd_ret
= ust_app_rotate_session(session
);
4766 if (cmd_ret
!= LTTNG_OK
) {
4771 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
4772 &((enum lttng_trace_chunk_command_type
) {
4773 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
}));
4775 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4779 ret
= timer_session_rotation_pending_check_start(session
,
4780 DEFAULT_ROTATE_PENDING_TIMER
);
4782 cmd_ret
= LTTNG_ERR_UNK
;
4786 if (!session
->active
) {
4787 session
->rotated_after_last_stop
= true;
4790 if (rotate_return
) {
4791 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
4794 session
->chunk_being_archived
= chunk_being_archived
;
4795 chunk_being_archived
= NULL
;
4796 ret
= notification_thread_command_session_rotation_ongoing(
4797 notification_thread_handle
,
4798 session
->name
, session
->uid
, session
->gid
,
4799 ongoing_rotation_chunk_id
);
4800 if (ret
!= LTTNG_OK
) {
4801 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
4806 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
4807 session
->name
, ongoing_rotation_chunk_id
);
4809 lttng_trace_chunk_put(new_trace_chunk
);
4810 lttng_trace_chunk_put(chunk_being_archived
);
4811 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
4814 if (session_reset_rotation_state(session
,
4815 LTTNG_ROTATION_STATE_ERROR
)) {
4816 ERR("Failed to reset rotation state of session \"%s\"",
4823 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
4825 * Check if the session has finished its rotation.
4827 * Return LTTNG_OK on success or else an LTTNG_ERR code.
4829 int cmd_rotate_get_info(struct ltt_session
*session
,
4830 struct lttng_rotation_get_info_return
*info_return
,
4831 uint64_t rotation_id
)
4833 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4834 enum lttng_rotation_state rotation_state
;
4836 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
4837 session
->most_recent_chunk_id
.value
);
4839 if (session
->chunk_being_archived
) {
4840 enum lttng_trace_chunk_status chunk_status
;
4843 chunk_status
= lttng_trace_chunk_get_id(
4844 session
->chunk_being_archived
,
4846 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
4848 rotation_state
= rotation_id
== chunk_id
?
4849 LTTNG_ROTATION_STATE_ONGOING
:
4850 LTTNG_ROTATION_STATE_EXPIRED
;
4852 if (session
->last_archived_chunk_id
.is_set
&&
4853 rotation_id
!= session
->last_archived_chunk_id
.value
) {
4854 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
4856 rotation_state
= session
->rotation_state
;
4860 switch (rotation_state
) {
4861 case LTTNG_ROTATION_STATE_NO_ROTATION
:
4862 DBG("Reporting that no rotation has occured within the lifetime of session \"%s\"",
4865 case LTTNG_ROTATION_STATE_EXPIRED
:
4866 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
4867 rotation_id
, session
->name
);
4869 case LTTNG_ROTATION_STATE_ONGOING
:
4870 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
4871 rotation_id
, session
->name
);
4873 case LTTNG_ROTATION_STATE_COMPLETED
:
4877 char *current_tracing_path_reply
;
4878 size_t current_tracing_path_reply_len
;
4880 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
4881 rotation_id
, session
->name
);
4883 switch (session_get_consumer_destination_type(session
)) {
4884 case CONSUMER_DST_LOCAL
:
4885 current_tracing_path_reply
=
4886 info_return
->location
.local
.absolute_path
;
4887 current_tracing_path_reply_len
=
4888 sizeof(info_return
->location
.local
.absolute_path
);
4889 info_return
->location_type
=
4890 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
4892 case CONSUMER_DST_NET
:
4893 current_tracing_path_reply
=
4894 info_return
->location
.relay
.relative_path
;
4895 current_tracing_path_reply_len
=
4896 sizeof(info_return
->location
.relay
.relative_path
);
4897 /* Currently the only supported relay protocol. */
4898 info_return
->location
.relay
.protocol
=
4899 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
4901 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
4902 session_get_net_consumer_hostname(session
),
4903 sizeof(info_return
->location
.relay
.host
));
4905 ERR("Failed to copy host name to rotate_get_info reply");
4906 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4907 cmd_ret
= LTTNG_ERR_SET_URL
;
4911 session_get_net_consumer_ports(session
,
4912 &info_return
->location
.relay
.ports
.control
,
4913 &info_return
->location
.relay
.ports
.data
);
4914 info_return
->location_type
=
4915 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
4920 fmt_ret
= asprintf(&chunk_path
,
4921 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
4922 session_get_base_path(session
),
4923 session
->last_archived_chunk_name
);
4924 if (fmt_ret
== -1) {
4925 PERROR("Failed to format the path of the last archived trace chunk");
4926 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4927 cmd_ret
= LTTNG_ERR_UNK
;
4931 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
4932 chunk_path
, current_tracing_path_reply_len
);
4935 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
4936 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4937 cmd_ret
= LTTNG_ERR_UNK
;
4943 case LTTNG_ROTATION_STATE_ERROR
:
4944 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
4945 rotation_id
, session
->name
);
4953 info_return
->status
= (int32_t) rotation_state
;
4958 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
4960 * Configure the automatic rotation parameters.
4961 * 'activate' to true means activate the rotation schedule type with 'new_value'.
4962 * 'activate' to false means deactivate the rotation schedule and validate that
4963 * 'new_value' has the same value as the currently active value.
4965 * Return 0 on success or else a positive LTTNG_ERR code.
4967 int cmd_rotation_set_schedule(struct ltt_session
*session
,
4968 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
4970 struct notification_thread_handle
*notification_thread_handle
)
4973 uint64_t *parameter_value
;
4977 DBG("Cmd rotate set schedule session %s", session
->name
);
4979 if (session
->live_timer
|| !session
->output_traces
) {
4980 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
4981 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4985 switch (schedule_type
) {
4986 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
4987 parameter_value
= &session
->rotate_size
;
4989 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
4990 parameter_value
= &session
->rotate_timer_period
;
4991 if (new_value
>= UINT_MAX
) {
4992 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
4993 new_value
, UINT_MAX
);
4994 ret
= LTTNG_ERR_INVALID
;
4999 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5000 ret
= LTTNG_ERR_INVALID
;
5004 /* Improper use of the API. */
5005 if (new_value
== -1ULL) {
5006 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5007 ret
= LTTNG_ERR_INVALID
;
5012 * As indicated in struct ltt_session's comments, a value of == 0 means
5013 * this schedule rotation type is not in use.
5015 * Reject the command if we were asked to activate a schedule that was
5018 if (activate
&& *parameter_value
!= 0) {
5019 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5020 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5025 * Reject the command if we were asked to deactivate a schedule that was
5028 if (!activate
&& *parameter_value
== 0) {
5029 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5030 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5035 * Reject the command if we were asked to deactivate a schedule that
5038 if (!activate
&& *parameter_value
!= new_value
) {
5039 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5040 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5044 *parameter_value
= activate
? new_value
: 0;
5046 switch (schedule_type
) {
5047 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5048 if (activate
&& session
->active
) {
5050 * Only start the timer if the session is active,
5051 * otherwise it will be started when the session starts.
5053 ret
= timer_session_rotation_schedule_timer_start(
5054 session
, new_value
);
5056 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5057 ret
= LTTNG_ERR_UNK
;
5061 ret
= timer_session_rotation_schedule_timer_stop(
5064 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5065 ret
= LTTNG_ERR_UNK
;
5070 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5072 ret
= subscribe_session_consumed_size_rotation(session
,
5073 new_value
, notification_thread_handle
);
5075 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5076 ret
= LTTNG_ERR_UNK
;
5080 ret
= unsubscribe_session_consumed_size_rotation(session
,
5081 notification_thread_handle
);
5083 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5084 ret
= LTTNG_ERR_UNK
;
5091 /* Would have been caught before. */
5103 /* Wait for a given path to be removed before continuing. */
5104 static enum lttng_error_code
wait_on_path(void *path_data
)
5106 const char *shm_path
= path_data
;
5108 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5114 ret
= stat(shm_path
, &st
);
5116 if (errno
!= ENOENT
) {
5117 PERROR("stat() returned an error while checking for the existence of the shm path");
5119 DBG("shm path no longer exists, completing the destruction of session");
5123 if (!S_ISDIR(st
.st_mode
)) {
5124 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5129 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5135 * Returns a pointer to a handler to run on completion of a command.
5136 * Returns NULL if no handler has to be run for the last command executed.
5138 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5140 struct cmd_completion_handler
*handler
= current_completion_handler
;
5142 current_completion_handler
= NULL
;
5147 * Init command subsystem.
5152 * Set network sequence index to 1 for streams to match a relayd
5153 * socket on the consumer side.
5155 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5156 relayd_net_seq_idx
= 1;
5157 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5159 DBG("Command subsystem initialized");