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
;
77 * Indicates whether or not an error occurred while launching the
78 * destruction of a session.
80 enum lttng_error_code destruction_status
;
83 static enum lttng_error_code
wait_on_path(void *path
);
86 * Command completion handler that is used by the destroy command
87 * when a session that has a non-default shm_path is being destroyed.
89 * See comment in cmd_destroy_session() for the rationale.
91 static struct destroy_completion_handler
{
92 struct cmd_completion_handler handler
;
93 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
94 } destroy_completion_handler
= {
97 .data
= destroy_completion_handler
.shm_path
102 static struct cmd_completion_handler
*current_completion_handler
;
105 * Used to keep a unique index for each relayd socket created where this value
106 * is associated with streams on the consumer so it can match the right relayd
107 * to send to. It must be accessed with the relayd_net_seq_idx_lock
110 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
111 static uint64_t relayd_net_seq_idx
;
113 static int validate_ust_event_name(const char *);
114 static int cmd_enable_event_internal(struct ltt_session
*session
,
115 const struct lttng_domain
*domain
,
116 char *channel_name
, struct lttng_event
*event
,
117 char *filter_expression
,
118 struct lttng_filter_bytecode
*filter
,
119 struct lttng_event_exclusion
*exclusion
,
123 * Create a session path used by list_lttng_sessions for the case that the
124 * session consumer is on the network.
126 static int build_network_session_path(char *dst
, size_t size
,
127 struct ltt_session
*session
)
129 int ret
, kdata_port
, udata_port
;
130 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
131 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
136 memset(tmp_urls
, 0, sizeof(tmp_urls
));
137 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
139 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
141 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
142 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
143 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
146 if (session
->ust_session
&& session
->ust_session
->consumer
) {
147 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
148 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
151 if (uuri
== NULL
&& kuri
== NULL
) {
152 uri
= &session
->consumer
->dst
.net
.control
;
153 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
154 } else if (kuri
&& uuri
) {
155 ret
= uri_compare(kuri
, uuri
);
159 /* Build uuri URL string */
160 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
167 } else if (kuri
&& uuri
== NULL
) {
169 } else if (uuri
&& kuri
== NULL
) {
173 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
179 * Do we have a UST url set. If yes, this means we have both kernel and UST
182 if (*tmp_uurl
!= '\0') {
183 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
184 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
187 if (kuri
|| (!kuri
&& !uuri
)) {
190 /* No kernel URI, use the UST port. */
193 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
201 * Get run-time attributes if the session has been started (discarded events,
204 static int get_kernel_runtime_stats(struct ltt_session
*session
,
205 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
206 uint64_t *lost_packets
)
210 if (!session
->has_been_started
) {
212 *discarded_events
= 0;
217 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
218 session
->kernel_session
->consumer
,
224 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
225 session
->kernel_session
->consumer
,
236 * Get run-time attributes if the session has been started (discarded events,
239 static int get_ust_runtime_stats(struct ltt_session
*session
,
240 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
241 uint64_t *lost_packets
)
244 struct ltt_ust_session
*usess
;
246 if (!discarded_events
|| !lost_packets
) {
251 usess
= session
->ust_session
;
252 assert(discarded_events
);
253 assert(lost_packets
);
255 if (!usess
|| !session
->has_been_started
) {
256 *discarded_events
= 0;
262 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
263 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
264 &usess
->buffer_reg_uid_list
,
265 usess
->consumer
, uchan
->id
,
266 uchan
->attr
.overwrite
,
269 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
270 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
271 uchan
, usess
->consumer
,
272 uchan
->attr
.overwrite
,
278 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
279 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
281 ERR("Unsupported buffer type");
292 * Fill lttng_channel array of all channels.
294 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
295 struct ltt_session
*session
, struct lttng_channel
*channels
,
296 struct lttng_channel_extended
*chan_exts
)
299 struct ltt_kernel_channel
*kchan
;
301 DBG("Listing channels for session %s", session
->name
);
304 case LTTNG_DOMAIN_KERNEL
:
305 /* Kernel channels */
306 if (session
->kernel_session
!= NULL
) {
307 cds_list_for_each_entry(kchan
,
308 &session
->kernel_session
->channel_list
.head
, list
) {
309 uint64_t discarded_events
, lost_packets
;
310 struct lttng_channel_extended
*extended
;
312 extended
= (struct lttng_channel_extended
*)
313 kchan
->channel
->attr
.extended
.ptr
;
315 ret
= get_kernel_runtime_stats(session
, kchan
,
316 &discarded_events
, &lost_packets
);
320 /* Copy lttng_channel struct to array */
321 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
322 channels
[i
].enabled
= kchan
->enabled
;
323 chan_exts
[i
].discarded_events
=
325 chan_exts
[i
].lost_packets
= lost_packets
;
326 chan_exts
[i
].monitor_timer_interval
=
327 extended
->monitor_timer_interval
;
328 chan_exts
[i
].blocking_timeout
= 0;
333 case LTTNG_DOMAIN_UST
:
335 struct lttng_ht_iter iter
;
336 struct ltt_ust_channel
*uchan
;
339 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
340 &iter
.iter
, uchan
, node
.node
) {
341 uint64_t discarded_events
= 0, lost_packets
= 0;
343 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
344 LTTNG_SYMBOL_NAME_LEN
)) {
347 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
348 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
349 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
350 channels
[i
].attr
.switch_timer_interval
=
351 uchan
->attr
.switch_timer_interval
;
352 channels
[i
].attr
.read_timer_interval
=
353 uchan
->attr
.read_timer_interval
;
354 channels
[i
].enabled
= uchan
->enabled
;
355 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
356 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
359 * Map enum lttng_ust_output to enum lttng_event_output.
361 switch (uchan
->attr
.output
) {
363 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
367 * LTTNG_UST_MMAP is the only supported UST
374 chan_exts
[i
].monitor_timer_interval
=
375 uchan
->monitor_timer_interval
;
376 chan_exts
[i
].blocking_timeout
=
377 uchan
->attr
.u
.s
.blocking_timeout
;
379 ret
= get_ust_runtime_stats(session
, uchan
,
380 &discarded_events
, &lost_packets
);
384 chan_exts
[i
].discarded_events
= discarded_events
;
385 chan_exts
[i
].lost_packets
= lost_packets
;
397 return -LTTNG_ERR_FATAL
;
403 static int increment_extended_len(const char *filter_expression
,
404 struct lttng_event_exclusion
*exclusion
,
405 const struct lttng_userspace_probe_location
*probe_location
,
406 size_t *extended_len
)
410 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
412 if (filter_expression
) {
413 *extended_len
+= strlen(filter_expression
) + 1;
417 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
420 if (probe_location
) {
421 ret
= lttng_userspace_probe_location_serialize(probe_location
,
426 *extended_len
+= ret
;
433 static int append_extended_info(const char *filter_expression
,
434 struct lttng_event_exclusion
*exclusion
,
435 struct lttng_userspace_probe_location
*probe_location
,
439 size_t filter_len
= 0;
440 size_t nb_exclusions
= 0;
441 size_t userspace_probe_location_len
= 0;
442 struct lttng_dynamic_buffer location_buffer
;
443 struct lttcomm_event_extended_header extended_header
;
445 if (filter_expression
) {
446 filter_len
= strlen(filter_expression
) + 1;
450 nb_exclusions
= exclusion
->count
;
453 if (probe_location
) {
454 lttng_dynamic_buffer_init(&location_buffer
);
455 ret
= lttng_userspace_probe_location_serialize(probe_location
,
456 &location_buffer
, NULL
);
461 userspace_probe_location_len
= location_buffer
.size
;
464 /* Set header fields */
465 extended_header
.filter_len
= filter_len
;
466 extended_header
.nb_exclusions
= nb_exclusions
;
467 extended_header
.userspace_probe_location_len
= userspace_probe_location_len
;
470 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
471 *extended_at
+= sizeof(extended_header
);
473 /* Copy filter string */
474 if (filter_expression
) {
475 memcpy(*extended_at
, filter_expression
, filter_len
);
476 *extended_at
+= filter_len
;
479 /* Copy exclusion names */
481 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
483 memcpy(*extended_at
, &exclusion
->names
, len
);
487 if (probe_location
) {
488 memcpy(*extended_at
, location_buffer
.data
, location_buffer
.size
);
489 *extended_at
+= location_buffer
.size
;
490 lttng_dynamic_buffer_reset(&location_buffer
);
498 * Create a list of agent domain events.
500 * Return number of events in list on success or else a negative value.
502 static int list_lttng_agent_events(struct agent
*agt
,
503 struct lttng_event
**events
, size_t *total_size
)
506 unsigned int nb_event
= 0;
507 struct agent_event
*event
;
508 struct lttng_event
*tmp_events
= NULL
;
509 struct lttng_ht_iter iter
;
510 size_t extended_len
= 0;
516 DBG3("Listing agent events");
519 nb_event
= lttng_ht_get_count(agt
->events
);
526 /* Compute required extended infos size */
527 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
530 * This is only valid because the commands which add events are
531 * processed in the same thread as the listing.
533 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
534 ret
= increment_extended_len(event
->filter_expression
, NULL
, NULL
,
537 DBG("Error computing the length of extended info message");
538 ret
= -LTTNG_ERR_FATAL
;
543 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
544 tmp_events
= zmalloc(*total_size
);
546 PERROR("zmalloc agent events session");
547 ret
= -LTTNG_ERR_FATAL
;
551 extended_at
= ((uint8_t *) tmp_events
) +
552 nb_event
* sizeof(struct lttng_event
);
554 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
555 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
556 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
557 tmp_events
[i
].enabled
= event
->enabled
;
558 tmp_events
[i
].loglevel
= event
->loglevel_value
;
559 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
562 /* Append extended info */
563 ret
= append_extended_info(event
->filter_expression
, NULL
, NULL
,
566 DBG("Error appending extended info message");
567 ret
= -LTTNG_ERR_FATAL
;
572 *events
= tmp_events
;
574 assert(nb_event
== i
);
585 * Create a list of ust global domain events.
587 static int list_lttng_ust_global_events(char *channel_name
,
588 struct ltt_ust_domain_global
*ust_global
,
589 struct lttng_event
**events
, size_t *total_size
)
592 unsigned int nb_event
= 0;
593 struct lttng_ht_iter iter
;
594 struct lttng_ht_node_str
*node
;
595 struct ltt_ust_channel
*uchan
;
596 struct ltt_ust_event
*uevent
;
597 struct lttng_event
*tmp
;
598 size_t extended_len
= 0;
601 DBG("Listing UST global events for channel %s", channel_name
);
605 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
606 node
= lttng_ht_iter_get_node_str(&iter
);
608 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
612 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
614 nb_event
= lttng_ht_get_count(uchan
->events
);
621 DBG3("Listing UST global %d events", nb_event
);
623 /* Compute required extended infos size */
624 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
625 if (uevent
->internal
) {
630 ret
= increment_extended_len(uevent
->filter_expression
,
631 uevent
->exclusion
, NULL
, &extended_len
);
633 DBG("Error computing the length of extended info message");
634 ret
= -LTTNG_ERR_FATAL
;
639 /* All events are internal, skip. */
645 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
646 tmp
= zmalloc(*total_size
);
648 ret
= -LTTNG_ERR_FATAL
;
652 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
654 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
655 if (uevent
->internal
) {
656 /* This event should remain hidden from clients */
659 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
660 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
661 tmp
[i
].enabled
= uevent
->enabled
;
663 switch (uevent
->attr
.instrumentation
) {
664 case LTTNG_UST_TRACEPOINT
:
665 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
667 case LTTNG_UST_PROBE
:
668 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
670 case LTTNG_UST_FUNCTION
:
671 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
675 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
676 switch (uevent
->attr
.loglevel_type
) {
677 case LTTNG_UST_LOGLEVEL_ALL
:
678 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
680 case LTTNG_UST_LOGLEVEL_RANGE
:
681 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
683 case LTTNG_UST_LOGLEVEL_SINGLE
:
684 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
687 if (uevent
->filter
) {
690 if (uevent
->exclusion
) {
691 tmp
[i
].exclusion
= 1;
695 /* Append extended info */
696 ret
= append_extended_info(uevent
->filter_expression
,
697 uevent
->exclusion
, NULL
, &extended_at
);
699 DBG("Error appending extended info message");
700 ret
= -LTTNG_ERR_FATAL
;
713 * Fill lttng_event array of all kernel events in the channel.
715 static int list_lttng_kernel_events(char *channel_name
,
716 struct ltt_kernel_session
*kernel_session
,
717 struct lttng_event
**events
, size_t *total_size
)
720 unsigned int nb_event
;
721 struct ltt_kernel_event
*event
;
722 struct ltt_kernel_channel
*kchan
;
723 size_t extended_len
= 0;
726 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
728 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
732 nb_event
= kchan
->event_count
;
734 DBG("Listing events for channel %s", kchan
->channel
->name
);
742 /* Compute required extended infos size */
743 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
744 ret
= increment_extended_len(event
->filter_expression
, NULL
,
745 event
->userspace_probe_location
,
748 DBG("Error computing the length of extended info message");
749 ret
= -LTTNG_ERR_FATAL
;
754 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
755 *events
= zmalloc(*total_size
);
756 if (*events
== NULL
) {
757 ret
= -LTTNG_ERR_FATAL
;
761 extended_at
= ((void *) *events
) +
762 nb_event
* sizeof(struct lttng_event
);
764 /* Kernel channels */
765 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
766 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
767 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
768 (*events
)[i
].enabled
= event
->enabled
;
769 (*events
)[i
].filter
=
770 (unsigned char) !!event
->filter_expression
;
772 switch (event
->event
->instrumentation
) {
773 case LTTNG_KERNEL_TRACEPOINT
:
774 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
776 case LTTNG_KERNEL_KRETPROBE
:
777 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
778 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
779 sizeof(struct lttng_kernel_kprobe
));
781 case LTTNG_KERNEL_KPROBE
:
782 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
783 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
784 sizeof(struct lttng_kernel_kprobe
));
786 case LTTNG_KERNEL_UPROBE
:
787 (*events
)[i
].type
= LTTNG_EVENT_USERSPACE_PROBE
;
789 case LTTNG_KERNEL_FUNCTION
:
790 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
791 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
792 sizeof(struct lttng_kernel_function
));
794 case LTTNG_KERNEL_NOOP
:
795 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
797 case LTTNG_KERNEL_SYSCALL
:
798 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
800 case LTTNG_KERNEL_ALL
:
808 /* Append extended info */
809 ret
= append_extended_info(event
->filter_expression
, NULL
,
810 event
->userspace_probe_location
, &extended_at
);
812 DBG("Error appending extended info message");
813 ret
= -LTTNG_ERR_FATAL
;
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
));
916 ret_code
= LTTNG_ERR_FATAL
;
919 consumer
->type
= CONSUMER_DST_LOCAL
;
929 * Init tracing by creating trace directory and sending fds kernel consumer.
931 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
934 struct lttng_ht_iter iter
;
935 struct consumer_socket
*socket
;
941 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
942 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
944 pthread_mutex_lock(socket
->lock
);
945 ret
= kernel_consumer_send_session(socket
, session
);
946 pthread_mutex_unlock(socket
->lock
);
948 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
960 * Create a socket to the relayd using the URI.
962 * On success, the relayd_sock pointer is set to the created socket.
963 * Else, it remains untouched and an LTTng error code is returned.
965 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
966 struct lttcomm_relayd_sock
**relayd_sock
,
967 struct consumer_output
*consumer
)
970 enum lttng_error_code status
= LTTNG_OK
;
971 struct lttcomm_relayd_sock
*rsock
;
973 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
974 RELAYD_VERSION_COMM_MINOR
);
976 status
= LTTNG_ERR_FATAL
;
981 * Connect to relayd so we can proceed with a session creation. This call
982 * can possibly block for an arbitrary amount of time to set the health
983 * state to be in poll execution.
986 ret
= relayd_connect(rsock
);
989 ERR("Unable to reach lttng-relayd");
990 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
994 /* Create socket for control stream. */
995 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
996 DBG3("Creating relayd stream socket from URI");
998 /* Check relayd version */
999 ret
= relayd_version_check(rsock
);
1000 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
1001 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
1003 } else if (ret
< 0) {
1004 ERR("Unable to reach lttng-relayd");
1005 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
1008 consumer
->relay_major_version
= rsock
->major
;
1009 consumer
->relay_minor_version
= rsock
->minor
;
1010 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1011 DBG3("Creating relayd data socket from URI");
1013 /* Command is not valid */
1014 ERR("Relayd invalid stream type: %d", uri
->stype
);
1015 status
= LTTNG_ERR_INVALID
;
1019 *relayd_sock
= rsock
;
1024 /* The returned value is not useful since we are on an error path. */
1025 (void) relayd_close(rsock
);
1033 * Connect to the relayd using URI and send the socket to the right consumer.
1035 * The consumer socket lock must be held by the caller.
1037 * Returns LTTNG_OK on success or an LTTng error code on failure.
1039 static enum lttng_error_code
send_consumer_relayd_socket(
1040 unsigned int session_id
,
1041 struct lttng_uri
*relayd_uri
,
1042 struct consumer_output
*consumer
,
1043 struct consumer_socket
*consumer_sock
,
1044 const char *session_name
, const char *hostname
,
1045 const char *base_path
, int session_live_timer
,
1046 const uint64_t *current_chunk_id
,
1047 time_t session_creation_time
,
1048 bool session_name_contains_creation_time
)
1051 struct lttcomm_relayd_sock
*rsock
= NULL
;
1052 enum lttng_error_code status
;
1054 /* Connect to relayd and make version check if uri is the control. */
1055 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1056 if (status
!= LTTNG_OK
) {
1057 goto relayd_comm_error
;
1061 /* Set the network sequence index if not set. */
1062 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1063 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1065 * Increment net_seq_idx because we are about to transfer the
1066 * new relayd socket to the consumer.
1067 * Assign unique key so the consumer can match streams.
1069 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1070 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1073 /* Send relayd socket to consumer. */
1074 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1075 relayd_uri
->stype
, session_id
,
1076 session_name
, hostname
, base_path
,
1077 session_live_timer
, current_chunk_id
,
1078 session_creation_time
, session_name_contains_creation_time
);
1080 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1084 /* Flag that the corresponding socket was sent. */
1085 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1086 consumer_sock
->control_sock_sent
= 1;
1087 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1088 consumer_sock
->data_sock_sent
= 1;
1092 * Close socket which was dup on the consumer side. The session daemon does
1093 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1097 if (status
!= LTTNG_OK
) {
1099 * The consumer output for this session should not be used anymore
1100 * since the relayd connection failed thus making any tracing or/and
1101 * streaming not usable.
1103 consumer
->enabled
= 0;
1105 (void) relayd_close(rsock
);
1113 * Send both relayd sockets to a specific consumer and domain. This is a
1114 * helper function to facilitate sending the information to the consumer for a
1117 * The consumer socket lock must be held by the caller.
1119 * Returns LTTNG_OK, or an LTTng error code on failure.
1121 static enum lttng_error_code
send_consumer_relayd_sockets(
1122 enum lttng_domain_type domain
,
1123 unsigned int session_id
, struct consumer_output
*consumer
,
1124 struct consumer_socket
*sock
, const char *session_name
,
1125 const char *hostname
, const char *base_path
, int session_live_timer
,
1126 const uint64_t *current_chunk_id
, time_t session_creation_time
,
1127 bool session_name_contains_creation_time
)
1129 enum lttng_error_code status
= LTTNG_OK
;
1134 /* Sending control relayd socket. */
1135 if (!sock
->control_sock_sent
) {
1136 status
= send_consumer_relayd_socket(session_id
,
1137 &consumer
->dst
.net
.control
, consumer
, sock
,
1138 session_name
, hostname
, base_path
, session_live_timer
,
1139 current_chunk_id
, session_creation_time
,
1140 session_name_contains_creation_time
);
1141 if (status
!= LTTNG_OK
) {
1146 /* Sending data relayd socket. */
1147 if (!sock
->data_sock_sent
) {
1148 status
= send_consumer_relayd_socket(session_id
,
1149 &consumer
->dst
.net
.data
, consumer
, sock
,
1150 session_name
, hostname
, base_path
, session_live_timer
,
1151 current_chunk_id
, session_creation_time
,
1152 session_name_contains_creation_time
);
1153 if (status
!= LTTNG_OK
) {
1163 * Setup relayd connections for a tracing session. First creates the socket to
1164 * the relayd and send them to the right domain consumer. Consumer type MUST be
1167 int cmd_setup_relayd(struct ltt_session
*session
)
1170 struct ltt_ust_session
*usess
;
1171 struct ltt_kernel_session
*ksess
;
1172 struct consumer_socket
*socket
;
1173 struct lttng_ht_iter iter
;
1174 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
1178 usess
= session
->ust_session
;
1179 ksess
= session
->kernel_session
;
1181 DBG("Setting relayd for session %s", session
->name
);
1184 if (session
->current_trace_chunk
) {
1185 enum lttng_trace_chunk_status status
= lttng_trace_chunk_get_id(
1186 session
->current_trace_chunk
, ¤t_chunk_id
.value
);
1188 if (status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
1189 current_chunk_id
.is_set
= true;
1191 ERR("Failed to get current trace chunk id");
1192 ret
= LTTNG_ERR_UNK
;
1197 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1198 && usess
->consumer
->enabled
) {
1199 /* For each consumer socket, send relayd sockets */
1200 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1201 socket
, node
.node
) {
1202 pthread_mutex_lock(socket
->lock
);
1203 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1204 usess
->consumer
, socket
,
1205 session
->name
, session
->hostname
,
1207 session
->live_timer
,
1208 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1209 session
->creation_time
,
1210 session
->name_contains_creation_time
);
1211 pthread_mutex_unlock(socket
->lock
);
1212 if (ret
!= LTTNG_OK
) {
1215 /* Session is now ready for network streaming. */
1216 session
->net_handle
= 1;
1218 session
->consumer
->relay_major_version
=
1219 usess
->consumer
->relay_major_version
;
1220 session
->consumer
->relay_minor_version
=
1221 usess
->consumer
->relay_minor_version
;
1224 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1225 && ksess
->consumer
->enabled
) {
1226 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1227 socket
, node
.node
) {
1228 pthread_mutex_lock(socket
->lock
);
1229 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1230 ksess
->consumer
, socket
,
1231 session
->name
, session
->hostname
,
1233 session
->live_timer
,
1234 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
1235 session
->creation_time
,
1236 session
->name_contains_creation_time
);
1237 pthread_mutex_unlock(socket
->lock
);
1238 if (ret
!= LTTNG_OK
) {
1241 /* Session is now ready for network streaming. */
1242 session
->net_handle
= 1;
1244 session
->consumer
->relay_major_version
=
1245 ksess
->consumer
->relay_major_version
;
1246 session
->consumer
->relay_minor_version
=
1247 ksess
->consumer
->relay_minor_version
;
1256 * Start a kernel session by opening all necessary streams.
1258 static int start_kernel_session(struct ltt_kernel_session
*ksess
)
1261 struct ltt_kernel_channel
*kchan
;
1263 /* Open kernel metadata */
1264 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1265 ret
= kernel_open_metadata(ksess
);
1267 ret
= LTTNG_ERR_KERN_META_FAIL
;
1272 /* Open kernel metadata stream */
1273 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1274 ret
= kernel_open_metadata_stream(ksess
);
1276 ERR("Kernel create metadata stream failed");
1277 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1282 /* For each channel */
1283 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1284 if (kchan
->stream_count
== 0) {
1285 ret
= kernel_open_channel_stream(kchan
);
1287 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1290 /* Update the stream global counter */
1291 ksess
->stream_count_global
+= ret
;
1295 /* Setup kernel consumer socket and send fds to it */
1296 ret
= init_kernel_tracing(ksess
);
1298 ret
= LTTNG_ERR_KERN_START_FAIL
;
1302 /* This start the kernel tracing */
1303 ret
= kernel_start_session(ksess
);
1305 ret
= LTTNG_ERR_KERN_START_FAIL
;
1309 /* Quiescent wait after starting trace */
1310 kernel_wait_quiescent();
1321 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1323 int cmd_disable_channel(struct ltt_session
*session
,
1324 enum lttng_domain_type domain
, char *channel_name
)
1327 struct ltt_ust_session
*usess
;
1329 usess
= session
->ust_session
;
1334 case LTTNG_DOMAIN_KERNEL
:
1336 ret
= channel_kernel_disable(session
->kernel_session
,
1338 if (ret
!= LTTNG_OK
) {
1342 kernel_wait_quiescent();
1345 case LTTNG_DOMAIN_UST
:
1347 struct ltt_ust_channel
*uchan
;
1348 struct lttng_ht
*chan_ht
;
1350 chan_ht
= usess
->domain_global
.channels
;
1352 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1353 if (uchan
== NULL
) {
1354 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1358 ret
= channel_ust_disable(usess
, uchan
);
1359 if (ret
!= LTTNG_OK
) {
1365 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1377 * Command LTTNG_TRACK_PID processed by the client thread.
1379 * Called with session lock held.
1381 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1389 case LTTNG_DOMAIN_KERNEL
:
1391 struct ltt_kernel_session
*ksess
;
1393 ksess
= session
->kernel_session
;
1395 ret
= kernel_track_pid(ksess
, pid
);
1396 if (ret
!= LTTNG_OK
) {
1400 kernel_wait_quiescent();
1403 case LTTNG_DOMAIN_UST
:
1405 struct ltt_ust_session
*usess
;
1407 usess
= session
->ust_session
;
1409 ret
= trace_ust_track_pid(usess
, pid
);
1410 if (ret
!= LTTNG_OK
) {
1416 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1428 * Command LTTNG_UNTRACK_PID processed by the client thread.
1430 * Called with session lock held.
1432 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1440 case LTTNG_DOMAIN_KERNEL
:
1442 struct ltt_kernel_session
*ksess
;
1444 ksess
= session
->kernel_session
;
1446 ret
= kernel_untrack_pid(ksess
, pid
);
1447 if (ret
!= LTTNG_OK
) {
1451 kernel_wait_quiescent();
1454 case LTTNG_DOMAIN_UST
:
1456 struct ltt_ust_session
*usess
;
1458 usess
= session
->ust_session
;
1460 ret
= trace_ust_untrack_pid(usess
, pid
);
1461 if (ret
!= LTTNG_OK
) {
1467 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1479 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1481 * The wpipe arguments is used as a notifier for the kernel thread.
1483 int cmd_enable_channel(struct ltt_session
*session
,
1484 const struct lttng_domain
*domain
, const struct lttng_channel
*_attr
, int wpipe
)
1487 struct ltt_ust_session
*usess
= session
->ust_session
;
1488 struct lttng_ht
*chan_ht
;
1490 struct lttng_channel attr
;
1497 len
= lttng_strnlen(attr
.name
, sizeof(attr
.name
));
1499 /* Validate channel name */
1500 if (attr
.name
[0] == '.' ||
1501 memchr(attr
.name
, '/', len
) != NULL
) {
1502 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1506 DBG("Enabling channel %s for session %s", attr
.name
, session
->name
);
1511 * Don't try to enable a channel if the session has been started at
1512 * some point in time before. The tracer does not allow it.
1514 if (session
->has_been_started
) {
1515 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1520 * If the session is a live session, remove the switch timer, the
1521 * live timer does the same thing but sends also synchronisation
1522 * beacons for inactive streams.
1524 if (session
->live_timer
> 0) {
1525 attr
.attr
.live_timer_interval
= session
->live_timer
;
1526 attr
.attr
.switch_timer_interval
= 0;
1529 /* Check for feature support */
1530 switch (domain
->type
) {
1531 case LTTNG_DOMAIN_KERNEL
:
1533 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1534 /* Sampling position of buffer is not supported */
1535 WARN("Kernel tracer does not support buffer monitoring. "
1536 "Setting the monitor interval timer to 0 "
1537 "(disabled) for channel '%s' of session '%s'",
1538 attr
.name
, session
->name
);
1539 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1543 case LTTNG_DOMAIN_UST
:
1545 case LTTNG_DOMAIN_JUL
:
1546 case LTTNG_DOMAIN_LOG4J
:
1547 case LTTNG_DOMAIN_PYTHON
:
1548 if (!agent_tracing_is_enabled()) {
1549 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1550 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1555 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1559 switch (domain
->type
) {
1560 case LTTNG_DOMAIN_KERNEL
:
1562 struct ltt_kernel_channel
*kchan
;
1564 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1565 session
->kernel_session
);
1566 if (kchan
== NULL
) {
1567 if (session
->snapshot
.nb_output
> 0 ||
1568 session
->snapshot_mode
) {
1569 /* Enforce mmap output for snapshot sessions. */
1570 attr
.attr
.output
= LTTNG_EVENT_MMAP
;
1572 ret
= channel_kernel_create(session
->kernel_session
, &attr
, wpipe
);
1573 if (attr
.name
[0] != '\0') {
1574 session
->kernel_session
->has_non_default_channel
= 1;
1577 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1580 if (ret
!= LTTNG_OK
) {
1584 kernel_wait_quiescent();
1587 case LTTNG_DOMAIN_UST
:
1588 case LTTNG_DOMAIN_JUL
:
1589 case LTTNG_DOMAIN_LOG4J
:
1590 case LTTNG_DOMAIN_PYTHON
:
1592 struct ltt_ust_channel
*uchan
;
1597 * Current agent implementation limitations force us to allow
1598 * only one channel at once in "agent" subdomains. Each
1599 * subdomain has a default channel name which must be strictly
1602 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1603 if (strncmp(attr
.name
, DEFAULT_JUL_CHANNEL_NAME
,
1604 LTTNG_SYMBOL_NAME_LEN
)) {
1605 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1608 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1609 if (strncmp(attr
.name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1610 LTTNG_SYMBOL_NAME_LEN
)) {
1611 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1614 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1615 if (strncmp(attr
.name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1616 LTTNG_SYMBOL_NAME_LEN
)) {
1617 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1622 chan_ht
= usess
->domain_global
.channels
;
1624 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
.name
);
1625 if (uchan
== NULL
) {
1626 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1627 if (attr
.name
[0] != '\0') {
1628 usess
->has_non_default_channel
= 1;
1631 ret
= channel_ust_enable(usess
, uchan
);
1636 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1640 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1641 session
->has_non_mmap_channel
= true;
1650 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1652 int cmd_disable_event(struct ltt_session
*session
,
1653 enum lttng_domain_type domain
, const char *channel_name
,
1654 const struct lttng_event
*event
)
1657 const char *event_name
;
1659 DBG("Disable event command for event \'%s\'", event
->name
);
1661 event_name
= event
->name
;
1663 /* Error out on unhandled search criteria */
1664 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1665 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1666 ret
= LTTNG_ERR_UNK
;
1673 case LTTNG_DOMAIN_KERNEL
:
1675 struct ltt_kernel_channel
*kchan
;
1676 struct ltt_kernel_session
*ksess
;
1678 ksess
= session
->kernel_session
;
1681 * If a non-default channel has been created in the
1682 * session, explicitely require that -c chan_name needs
1685 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1686 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1690 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1691 if (kchan
== NULL
) {
1692 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1696 switch (event
->type
) {
1697 case LTTNG_EVENT_ALL
:
1698 case LTTNG_EVENT_TRACEPOINT
:
1699 case LTTNG_EVENT_SYSCALL
:
1700 case LTTNG_EVENT_PROBE
:
1701 case LTTNG_EVENT_FUNCTION
:
1702 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1703 if (event_name
[0] == '\0') {
1704 ret
= event_kernel_disable_event(kchan
,
1707 ret
= event_kernel_disable_event(kchan
,
1708 event_name
, event
->type
);
1710 if (ret
!= LTTNG_OK
) {
1715 ret
= LTTNG_ERR_UNK
;
1719 kernel_wait_quiescent();
1722 case LTTNG_DOMAIN_UST
:
1724 struct ltt_ust_channel
*uchan
;
1725 struct ltt_ust_session
*usess
;
1727 usess
= session
->ust_session
;
1729 if (validate_ust_event_name(event_name
)) {
1730 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1735 * If a non-default channel has been created in the
1736 * session, explicitly require that -c chan_name needs
1739 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1740 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1744 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1746 if (uchan
== NULL
) {
1747 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1751 switch (event
->type
) {
1752 case LTTNG_EVENT_ALL
:
1754 * An empty event name means that everything
1755 * should be disabled.
1757 if (event
->name
[0] == '\0') {
1758 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1760 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1763 if (ret
!= LTTNG_OK
) {
1768 ret
= LTTNG_ERR_UNK
;
1772 DBG3("Disable UST event %s in channel %s completed", event_name
,
1776 case LTTNG_DOMAIN_LOG4J
:
1777 case LTTNG_DOMAIN_JUL
:
1778 case LTTNG_DOMAIN_PYTHON
:
1781 struct ltt_ust_session
*usess
= session
->ust_session
;
1785 switch (event
->type
) {
1786 case LTTNG_EVENT_ALL
:
1789 ret
= LTTNG_ERR_UNK
;
1793 agt
= trace_ust_find_agent(usess
, domain
);
1795 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1799 * An empty event name means that everything
1800 * should be disabled.
1802 if (event
->name
[0] == '\0') {
1803 ret
= event_agent_disable_all(usess
, agt
);
1805 ret
= event_agent_disable(usess
, agt
, event_name
);
1807 if (ret
!= LTTNG_OK
) {
1814 ret
= LTTNG_ERR_UND
;
1827 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1829 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1830 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1832 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1833 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1836 * Don't try to add a context if the session has been started at
1837 * some point in time before. The tracer does not allow it and would
1838 * result in a corrupted trace.
1840 if (session
->has_been_started
) {
1841 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1845 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1846 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1847 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1851 case LTTNG_DOMAIN_KERNEL
:
1852 assert(session
->kernel_session
);
1854 if (session
->kernel_session
->channel_count
== 0) {
1855 /* Create default channel */
1856 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1857 if (ret
!= LTTNG_OK
) {
1860 chan_kern_created
= 1;
1862 /* Add kernel context to kernel tracer */
1863 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1864 if (ret
!= LTTNG_OK
) {
1868 case LTTNG_DOMAIN_JUL
:
1869 case LTTNG_DOMAIN_LOG4J
:
1872 * Validate channel name.
1873 * If no channel name is given and the domain is JUL or LOG4J,
1874 * set it to the appropriate domain-specific channel name. If
1875 * a name is provided but does not match the expexted channel
1876 * name, return an error.
1878 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1879 strcmp(channel_name
,
1880 DEFAULT_JUL_CHANNEL_NAME
)) {
1881 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1883 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1884 strcmp(channel_name
,
1885 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1886 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1889 /* break is _not_ missing here. */
1891 case LTTNG_DOMAIN_UST
:
1893 struct ltt_ust_session
*usess
= session
->ust_session
;
1894 unsigned int chan_count
;
1898 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1899 if (chan_count
== 0) {
1900 struct lttng_channel
*attr
;
1901 /* Create default channel */
1902 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1904 ret
= LTTNG_ERR_FATAL
;
1908 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1909 if (ret
!= LTTNG_OK
) {
1913 channel_attr_destroy(attr
);
1914 chan_ust_created
= 1;
1917 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1918 free(app_ctx_provider_name
);
1920 app_ctx_name
= NULL
;
1921 app_ctx_provider_name
= NULL
;
1922 if (ret
!= LTTNG_OK
) {
1928 ret
= LTTNG_ERR_UND
;
1936 if (chan_kern_created
) {
1937 struct ltt_kernel_channel
*kchan
=
1938 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1939 session
->kernel_session
);
1940 /* Created previously, this should NOT fail. */
1942 kernel_destroy_channel(kchan
);
1945 if (chan_ust_created
) {
1946 struct ltt_ust_channel
*uchan
=
1947 trace_ust_find_channel_by_name(
1948 session
->ust_session
->domain_global
.channels
,
1949 DEFAULT_CHANNEL_NAME
);
1950 /* Created previously, this should NOT fail. */
1952 /* Remove from the channel list of the session. */
1953 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1955 trace_ust_destroy_channel(uchan
);
1958 free(app_ctx_provider_name
);
1963 static inline bool name_starts_with(const char *name
, const char *prefix
)
1965 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1967 return !strncmp(name
, prefix
, max_cmp_len
);
1970 /* Perform userspace-specific event name validation */
1971 static int validate_ust_event_name(const char *name
)
1981 * Check name against all internal UST event component namespaces used
1984 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1985 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1986 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1995 * Internal version of cmd_enable_event() with a supplemental
1996 * "internal_event" flag which is used to enable internal events which should
1997 * be hidden from clients. Such events are used in the agent implementation to
1998 * enable the events through which all "agent" events are funeled.
2000 static int _cmd_enable_event(struct ltt_session
*session
,
2001 const struct lttng_domain
*domain
,
2002 char *channel_name
, struct lttng_event
*event
,
2003 char *filter_expression
,
2004 struct lttng_filter_bytecode
*filter
,
2005 struct lttng_event_exclusion
*exclusion
,
2006 int wpipe
, bool internal_event
)
2008 int ret
= 0, channel_created
= 0;
2009 struct lttng_channel
*attr
= NULL
;
2013 assert(channel_name
);
2015 /* If we have a filter, we must have its filter expression */
2016 assert(!(!!filter_expression
^ !!filter
));
2018 /* Normalize event name as a globbing pattern */
2019 strutils_normalize_star_glob_pattern(event
->name
);
2021 /* Normalize exclusion names as globbing patterns */
2025 for (i
= 0; i
< exclusion
->count
; i
++) {
2026 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2028 strutils_normalize_star_glob_pattern(name
);
2032 DBG("Enable event command for event \'%s\'", event
->name
);
2036 switch (domain
->type
) {
2037 case LTTNG_DOMAIN_KERNEL
:
2039 struct ltt_kernel_channel
*kchan
;
2042 * If a non-default channel has been created in the
2043 * session, explicitely require that -c chan_name needs
2046 if (session
->kernel_session
->has_non_default_channel
2047 && channel_name
[0] == '\0') {
2048 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2052 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2053 session
->kernel_session
);
2054 if (kchan
== NULL
) {
2055 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2056 LTTNG_BUFFER_GLOBAL
);
2058 ret
= LTTNG_ERR_FATAL
;
2061 if (lttng_strncpy(attr
->name
, channel_name
,
2062 sizeof(attr
->name
))) {
2063 ret
= LTTNG_ERR_INVALID
;
2067 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2068 if (ret
!= LTTNG_OK
) {
2071 channel_created
= 1;
2074 /* Get the newly created kernel channel pointer */
2075 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2076 session
->kernel_session
);
2077 if (kchan
== NULL
) {
2078 /* This sould not happen... */
2079 ret
= LTTNG_ERR_FATAL
;
2083 switch (event
->type
) {
2084 case LTTNG_EVENT_ALL
:
2086 char *filter_expression_a
= NULL
;
2087 struct lttng_filter_bytecode
*filter_a
= NULL
;
2090 * We need to duplicate filter_expression and filter,
2091 * because ownership is passed to first enable
2094 if (filter_expression
) {
2095 filter_expression_a
= strdup(filter_expression
);
2096 if (!filter_expression_a
) {
2097 ret
= LTTNG_ERR_FATAL
;
2102 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2104 free(filter_expression_a
);
2105 ret
= LTTNG_ERR_FATAL
;
2108 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2110 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2111 ret
= event_kernel_enable_event(kchan
, event
,
2112 filter_expression
, filter
);
2113 /* We have passed ownership */
2114 filter_expression
= NULL
;
2116 if (ret
!= LTTNG_OK
) {
2117 if (channel_created
) {
2118 /* Let's not leak a useless channel. */
2119 kernel_destroy_channel(kchan
);
2121 free(filter_expression_a
);
2125 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2126 ret
= event_kernel_enable_event(kchan
, event
,
2127 filter_expression_a
, filter_a
);
2128 /* We have passed ownership */
2129 filter_expression_a
= NULL
;
2131 if (ret
!= LTTNG_OK
) {
2136 case LTTNG_EVENT_PROBE
:
2137 case LTTNG_EVENT_USERSPACE_PROBE
:
2138 case LTTNG_EVENT_FUNCTION
:
2139 case LTTNG_EVENT_FUNCTION_ENTRY
:
2140 case LTTNG_EVENT_TRACEPOINT
:
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
) {
2147 if (channel_created
) {
2148 /* Let's not leak a useless channel. */
2149 kernel_destroy_channel(kchan
);
2154 case LTTNG_EVENT_SYSCALL
:
2155 ret
= event_kernel_enable_event(kchan
, event
,
2156 filter_expression
, filter
);
2157 /* We have passed ownership */
2158 filter_expression
= NULL
;
2160 if (ret
!= LTTNG_OK
) {
2165 ret
= LTTNG_ERR_UNK
;
2169 kernel_wait_quiescent();
2172 case LTTNG_DOMAIN_UST
:
2174 struct ltt_ust_channel
*uchan
;
2175 struct ltt_ust_session
*usess
= session
->ust_session
;
2180 * If a non-default channel has been created in the
2181 * session, explicitely require that -c chan_name needs
2184 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2185 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2189 /* Get channel from global UST domain */
2190 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2192 if (uchan
== NULL
) {
2193 /* Create default channel */
2194 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2195 usess
->buffer_type
);
2197 ret
= LTTNG_ERR_FATAL
;
2200 if (lttng_strncpy(attr
->name
, channel_name
,
2201 sizeof(attr
->name
))) {
2202 ret
= LTTNG_ERR_INVALID
;
2206 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2207 if (ret
!= LTTNG_OK
) {
2211 /* Get the newly created channel reference back */
2212 uchan
= trace_ust_find_channel_by_name(
2213 usess
->domain_global
.channels
, channel_name
);
2217 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2219 * Don't allow users to add UST events to channels which
2220 * are assigned to a userspace subdomain (JUL, Log4J,
2223 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2227 if (!internal_event
) {
2229 * Ensure the event name is not reserved for internal
2232 ret
= validate_ust_event_name(event
->name
);
2234 WARN("Userspace event name %s failed validation.",
2236 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2241 /* At this point, the session and channel exist on the tracer */
2242 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2243 filter_expression
, filter
, exclusion
,
2245 /* We have passed ownership */
2246 filter_expression
= NULL
;
2249 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2250 goto already_enabled
;
2251 } else if (ret
!= LTTNG_OK
) {
2256 case LTTNG_DOMAIN_LOG4J
:
2257 case LTTNG_DOMAIN_JUL
:
2258 case LTTNG_DOMAIN_PYTHON
:
2260 const char *default_event_name
, *default_chan_name
;
2262 struct lttng_event uevent
;
2263 struct lttng_domain tmp_dom
;
2264 struct ltt_ust_session
*usess
= session
->ust_session
;
2268 if (!agent_tracing_is_enabled()) {
2269 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2270 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2274 agt
= trace_ust_find_agent(usess
, domain
->type
);
2276 agt
= agent_create(domain
->type
);
2278 ret
= LTTNG_ERR_NOMEM
;
2281 agent_add(agt
, usess
->agents
);
2284 /* Create the default tracepoint. */
2285 memset(&uevent
, 0, sizeof(uevent
));
2286 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2287 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2288 default_event_name
= event_get_default_agent_ust_name(
2290 if (!default_event_name
) {
2291 ret
= LTTNG_ERR_FATAL
;
2294 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2295 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2298 * The domain type is changed because we are about to enable the
2299 * default channel and event for the JUL domain that are hardcoded.
2300 * This happens in the UST domain.
2302 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2303 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2305 switch (domain
->type
) {
2306 case LTTNG_DOMAIN_LOG4J
:
2307 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2309 case LTTNG_DOMAIN_JUL
:
2310 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2312 case LTTNG_DOMAIN_PYTHON
:
2313 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2316 /* The switch/case we are in makes this impossible */
2321 char *filter_expression_copy
= NULL
;
2322 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2325 const size_t filter_size
= sizeof(
2326 struct lttng_filter_bytecode
)
2329 filter_copy
= zmalloc(filter_size
);
2331 ret
= LTTNG_ERR_NOMEM
;
2334 memcpy(filter_copy
, filter
, filter_size
);
2336 filter_expression_copy
=
2337 strdup(filter_expression
);
2338 if (!filter_expression
) {
2339 ret
= LTTNG_ERR_NOMEM
;
2342 if (!filter_expression_copy
|| !filter_copy
) {
2343 free(filter_expression_copy
);
2349 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2350 (char *) default_chan_name
,
2351 &uevent
, filter_expression_copy
,
2352 filter_copy
, NULL
, wpipe
);
2355 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2356 goto already_enabled
;
2357 } else if (ret
!= LTTNG_OK
) {
2361 /* The wild card * means that everything should be enabled. */
2362 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2363 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2366 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2370 filter_expression
= NULL
;
2371 if (ret
!= LTTNG_OK
) {
2378 ret
= LTTNG_ERR_UND
;
2386 free(filter_expression
);
2389 channel_attr_destroy(attr
);
2395 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2396 * We own filter, exclusion, and filter_expression.
2398 int cmd_enable_event(struct ltt_session
*session
,
2399 const struct lttng_domain
*domain
,
2400 char *channel_name
, struct lttng_event
*event
,
2401 char *filter_expression
,
2402 struct lttng_filter_bytecode
*filter
,
2403 struct lttng_event_exclusion
*exclusion
,
2406 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2407 filter_expression
, filter
, exclusion
, wpipe
, false);
2411 * Enable an event which is internal to LTTng. An internal should
2412 * never be made visible to clients and are immune to checks such as
2415 static int cmd_enable_event_internal(struct ltt_session
*session
,
2416 const struct lttng_domain
*domain
,
2417 char *channel_name
, struct lttng_event
*event
,
2418 char *filter_expression
,
2419 struct lttng_filter_bytecode
*filter
,
2420 struct lttng_event_exclusion
*exclusion
,
2423 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2424 filter_expression
, filter
, exclusion
, wpipe
, true);
2428 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2430 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2431 struct lttng_event
**events
)
2434 ssize_t nb_events
= 0;
2437 case LTTNG_DOMAIN_KERNEL
:
2438 nb_events
= kernel_list_events(events
);
2439 if (nb_events
< 0) {
2440 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2444 case LTTNG_DOMAIN_UST
:
2445 nb_events
= ust_app_list_events(events
);
2446 if (nb_events
< 0) {
2447 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2451 case LTTNG_DOMAIN_LOG4J
:
2452 case LTTNG_DOMAIN_JUL
:
2453 case LTTNG_DOMAIN_PYTHON
:
2454 nb_events
= agent_list_events(events
, domain
);
2455 if (nb_events
< 0) {
2456 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2461 ret
= LTTNG_ERR_UND
;
2468 /* Return negative value to differentiate return code */
2473 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2475 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2476 struct lttng_event_field
**fields
)
2479 ssize_t nb_fields
= 0;
2482 case LTTNG_DOMAIN_UST
:
2483 nb_fields
= ust_app_list_event_fields(fields
);
2484 if (nb_fields
< 0) {
2485 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2489 case LTTNG_DOMAIN_KERNEL
:
2490 default: /* fall-through */
2491 ret
= LTTNG_ERR_UND
;
2498 /* Return negative value to differentiate return code */
2502 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2504 return syscall_table_list(events
);
2508 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2510 * Called with session lock held.
2512 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2513 enum lttng_domain_type domain
, int32_t **pids
)
2516 ssize_t nr_pids
= 0;
2519 case LTTNG_DOMAIN_KERNEL
:
2521 struct ltt_kernel_session
*ksess
;
2523 ksess
= session
->kernel_session
;
2524 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2526 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2531 case LTTNG_DOMAIN_UST
:
2533 struct ltt_ust_session
*usess
;
2535 usess
= session
->ust_session
;
2536 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2538 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2543 case LTTNG_DOMAIN_LOG4J
:
2544 case LTTNG_DOMAIN_JUL
:
2545 case LTTNG_DOMAIN_PYTHON
:
2547 ret
= LTTNG_ERR_UND
;
2554 /* Return negative value to differentiate return code */
2559 * Command LTTNG_START_TRACE processed by the client thread.
2561 * Called with session mutex held.
2563 int cmd_start_trace(struct ltt_session
*session
)
2565 enum lttng_error_code ret
;
2566 unsigned long nb_chan
= 0;
2567 struct ltt_kernel_session
*ksession
;
2568 struct ltt_ust_session
*usess
;
2569 const bool session_rotated_after_last_stop
=
2570 session
->rotated_after_last_stop
;
2574 /* Ease our life a bit ;) */
2575 ksession
= session
->kernel_session
;
2576 usess
= session
->ust_session
;
2578 /* Is the session already started? */
2579 if (session
->active
) {
2580 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2584 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2585 !session
->current_trace_chunk
) {
2587 * A rotation was launched while the session was stopped and
2588 * it has not been completed yet. It is not possible to start
2589 * the session since starting the session here would require a
2590 * rotation from "NULL" to a new trace chunk. That rotation
2591 * would overlap with the ongoing rotation, which is not
2594 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2596 ret
= LTTNG_ERR_ROTATION_PENDING
;
2601 * Starting a session without channel is useless since after that it's not
2602 * possible to enable channel thus inform the client.
2604 if (usess
&& usess
->domain_global
.channels
) {
2605 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2608 nb_chan
+= ksession
->channel_count
;
2611 ret
= LTTNG_ERR_NO_CHANNEL
;
2615 session
->active
= 1;
2616 session
->rotated_after_last_stop
= false;
2617 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2618 if (!session
->has_been_started
) {
2619 struct lttng_trace_chunk
*trace_chunk
;
2621 DBG("Creating initial trace chunk of session \"%s\"",
2623 trace_chunk
= session_create_new_trace_chunk(
2624 session
, NULL
, NULL
, NULL
);
2626 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2629 assert(!session
->current_trace_chunk
);
2630 ret
= session_set_trace_chunk(session
, trace_chunk
,
2632 lttng_trace_chunk_put(trace_chunk
);
2634 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2638 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2641 * Rotate existing streams into the new chunk.
2642 * This is a "quiet" rotation has no client has
2643 * explicitly requested this operation.
2645 * There is also no need to wait for the rotation
2646 * to complete as it will happen immediately. No data
2647 * was produced as the session was stopped, so the
2648 * rotation should happen on reception of the command.
2650 ret
= cmd_rotate_session(session
, NULL
, true);
2651 if (ret
!= LTTNG_OK
) {
2657 /* Kernel tracing */
2658 if (ksession
!= NULL
) {
2659 DBG("Start kernel tracing session %s", session
->name
);
2660 ret
= start_kernel_session(ksession
);
2661 if (ret
!= LTTNG_OK
) {
2666 /* Flag session that trace should start automatically */
2668 int int_ret
= ust_app_start_trace_all(usess
);
2671 ret
= LTTNG_ERR_UST_START_FAIL
;
2677 * Clear the flag that indicates that a rotation was done while the
2678 * session was stopped.
2680 session
->rotated_after_last_stop
= false;
2682 if (session
->rotate_timer_period
) {
2683 int int_ret
= timer_session_rotation_schedule_timer_start(
2684 session
, session
->rotate_timer_period
);
2687 ERR("Failed to enable rotate timer");
2688 ret
= LTTNG_ERR_UNK
;
2696 if (ret
== LTTNG_OK
) {
2697 /* Flag this after a successful start. */
2698 session
->has_been_started
|= 1;
2700 session
->active
= 0;
2701 /* Restore initial state on error. */
2702 session
->rotated_after_last_stop
=
2703 session_rotated_after_last_stop
;
2709 * Command LTTNG_STOP_TRACE processed by the client thread.
2711 int cmd_stop_trace(struct ltt_session
*session
)
2714 struct ltt_kernel_channel
*kchan
;
2715 struct ltt_kernel_session
*ksession
;
2716 struct ltt_ust_session
*usess
;
2717 bool error_occurred
= false;
2721 DBG("Begin stop session %s (id %" PRIu64
")", session
->name
, session
->id
);
2723 ksession
= session
->kernel_session
;
2724 usess
= session
->ust_session
;
2726 /* Session is not active. Skip everythong and inform the client. */
2727 if (!session
->active
) {
2728 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2733 if (ksession
&& ksession
->active
) {
2734 DBG("Stop kernel tracing");
2736 ret
= kernel_stop_session(ksession
);
2738 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2742 kernel_wait_quiescent();
2744 /* Flush metadata after stopping (if exists) */
2745 if (ksession
->metadata_stream_fd
>= 0) {
2746 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2748 ERR("Kernel metadata flush failed");
2749 error_occurred
= true;
2753 /* Flush all buffers after stopping */
2754 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2755 ret
= kernel_flush_buffer(kchan
);
2757 ERR("Kernel flush buffer error");
2758 error_occurred
= true;
2762 ksession
->active
= 0;
2763 DBG("Kernel session stopped %s (id %" PRIu64
")", session
->name
,
2767 if (usess
&& usess
->active
) {
2768 ret
= ust_app_stop_trace_all(usess
);
2770 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2775 /* Flag inactive after a successful stop. */
2776 session
->active
= 0;
2777 ret
= !error_occurred
? LTTNG_OK
: LTTNG_ERR_UNK
;
2784 * Set the base_path of the session only if subdir of a control uris is set.
2785 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2787 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2789 struct lttng_uri
*uris
)
2794 for (i
= 0; i
< nb_uri
; i
++) {
2795 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2796 uris
[i
].subdir
[0] == '\0') {
2797 /* Not interested in these URIs */
2801 if (session
->base_path
!= NULL
) {
2802 free(session
->base_path
);
2803 session
->base_path
= NULL
;
2806 /* Set session base_path */
2807 session
->base_path
= strdup(uris
[i
].subdir
);
2808 if (!session
->base_path
) {
2809 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2810 uris
[i
].subdir
, session
->name
);
2811 ret
= LTTNG_ERR_NOMEM
;
2814 DBG2("Setting base path \"%s\" for session \"%s\"",
2815 session
->base_path
, session
->name
);
2823 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2825 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2826 struct lttng_uri
*uris
)
2829 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2830 struct ltt_ust_session
*usess
= session
->ust_session
;
2836 /* Can't set consumer URI if the session is active. */
2837 if (session
->active
) {
2838 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2843 * Set the session base path if any. This is done inside
2844 * cmd_set_consumer_uri to preserve backward compatibility of the
2845 * previous session creation api vs the session descriptor api.
2847 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2848 if (ret
!= LTTNG_OK
) {
2852 /* Set the "global" consumer URIs */
2853 for (i
= 0; i
< nb_uri
; i
++) {
2854 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2856 if (ret
!= LTTNG_OK
) {
2861 /* Set UST session URIs */
2862 if (session
->ust_session
) {
2863 for (i
= 0; i
< nb_uri
; i
++) {
2864 ret
= add_uri_to_consumer(session
,
2865 session
->ust_session
->consumer
,
2866 &uris
[i
], LTTNG_DOMAIN_UST
);
2867 if (ret
!= LTTNG_OK
) {
2873 /* Set kernel session URIs */
2874 if (session
->kernel_session
) {
2875 for (i
= 0; i
< nb_uri
; i
++) {
2876 ret
= add_uri_to_consumer(session
,
2877 session
->kernel_session
->consumer
,
2878 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2879 if (ret
!= LTTNG_OK
) {
2886 * Make sure to set the session in output mode after we set URI since a
2887 * session can be created without URL (thus flagged in no output mode).
2889 session
->output_traces
= 1;
2891 ksess
->output_traces
= 1;
2895 usess
->output_traces
= 1;
2906 enum lttng_error_code
set_session_output_from_descriptor(
2907 struct ltt_session
*session
,
2908 const struct lttng_session_descriptor
*descriptor
)
2911 enum lttng_error_code ret_code
= LTTNG_OK
;
2912 enum lttng_session_descriptor_type session_type
=
2913 lttng_session_descriptor_get_type(descriptor
);
2914 enum lttng_session_descriptor_output_type output_type
=
2915 lttng_session_descriptor_get_output_type(descriptor
);
2916 struct lttng_uri uris
[2] = {};
2917 size_t uri_count
= 0;
2919 switch (output_type
) {
2920 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2922 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2923 lttng_session_descriptor_get_local_output_uri(descriptor
,
2927 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2928 lttng_session_descriptor_get_network_output_uris(descriptor
,
2929 &uris
[0], &uris
[1]);
2933 ret_code
= LTTNG_ERR_INVALID
;
2937 switch (session_type
) {
2938 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2940 struct snapshot_output
*new_output
= NULL
;
2942 new_output
= snapshot_output_alloc();
2944 ret_code
= LTTNG_ERR_NOMEM
;
2948 ret
= snapshot_output_init_with_uri(session
,
2949 DEFAULT_SNAPSHOT_MAX_SIZE
,
2950 NULL
, uris
, uri_count
, session
->consumer
,
2951 new_output
, &session
->snapshot
);
2953 ret_code
= (ret
== -ENOMEM
) ?
2954 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2955 snapshot_output_destroy(new_output
);
2958 snapshot_add_output(&session
->snapshot
, new_output
);
2961 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2962 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2964 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2968 ret_code
= LTTNG_ERR_INVALID
;
2976 enum lttng_error_code
cmd_create_session_from_descriptor(
2977 struct lttng_session_descriptor
*descriptor
,
2978 const lttng_sock_cred
*creds
,
2979 const char *home_path
)
2982 enum lttng_error_code ret_code
;
2983 const char *session_name
;
2984 struct ltt_session
*new_session
= NULL
;
2985 enum lttng_session_descriptor_status descriptor_status
;
2987 session_lock_list();
2989 if (*home_path
!= '/') {
2990 ERR("Home path provided by client is not absolute");
2991 ret_code
= LTTNG_ERR_INVALID
;
2996 descriptor_status
= lttng_session_descriptor_get_session_name(
2997 descriptor
, &session_name
);
2998 switch (descriptor_status
) {
2999 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3001 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3002 session_name
= NULL
;
3005 ret_code
= LTTNG_ERR_INVALID
;
3009 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3011 if (ret_code
!= LTTNG_OK
) {
3015 if (!session_name
) {
3016 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3019 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3024 if (!lttng_session_descriptor_is_output_destination_initialized(
3027 * Only include the session's creation time in the output
3028 * destination if the name of the session itself was
3029 * not auto-generated.
3031 ret_code
= lttng_session_descriptor_set_default_output(
3033 session_name
? &new_session
->creation_time
: NULL
,
3035 if (ret_code
!= LTTNG_OK
) {
3039 new_session
->has_user_specified_directory
=
3040 lttng_session_descriptor_has_output_directory(
3044 switch (lttng_session_descriptor_get_type(descriptor
)) {
3045 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3046 new_session
->snapshot_mode
= 1;
3048 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3049 new_session
->live_timer
=
3050 lttng_session_descriptor_live_get_timer_interval(
3057 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3058 if (ret_code
!= LTTNG_OK
) {
3061 new_session
->consumer
->enabled
= 1;
3062 ret_code
= LTTNG_OK
;
3064 /* Release reference provided by the session_create function. */
3065 session_put(new_session
);
3066 if (ret_code
!= LTTNG_OK
&& new_session
) {
3067 /* Release the global reference on error. */
3068 session_destroy(new_session
);
3070 session_unlock_list();
3074 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3075 struct lttng_session_descriptor
**return_descriptor
)
3078 size_t payload_size
;
3079 struct lttng_dynamic_buffer payload
;
3080 struct lttng_buffer_view home_dir_view
;
3081 struct lttng_buffer_view session_descriptor_view
;
3082 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3083 enum lttng_error_code ret_code
;
3085 lttng_dynamic_buffer_init(&payload
);
3086 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
3088 ret_code
= LTTNG_ERR_INVALID
;
3091 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
3092 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3093 ret_code
= LTTNG_ERR_INVALID
;
3097 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
3098 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
3099 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3101 ret_code
= LTTNG_ERR_NOMEM
;
3105 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3107 ERR("Reception of session descriptor failed, aborting.");
3108 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3112 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3115 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3116 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3118 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3119 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3121 ret
= lttng_session_descriptor_create_from_buffer(
3122 &session_descriptor_view
, &session_descriptor
);
3124 ERR("Failed to create session descriptor from payload of \"create session\" command");
3125 ret_code
= LTTNG_ERR_INVALID
;
3130 * Sets the descriptor's auto-generated properties (name, output) if
3133 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3135 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3136 if (ret_code
!= LTTNG_OK
) {
3140 ret_code
= LTTNG_OK
;
3141 *return_descriptor
= session_descriptor
;
3142 session_descriptor
= NULL
;
3144 lttng_dynamic_buffer_reset(&payload
);
3145 lttng_session_descriptor_destroy(session_descriptor
);
3150 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3151 void *_reply_context
)
3155 const struct cmd_destroy_session_reply_context
*reply_context
=
3157 struct lttng_dynamic_buffer payload
;
3158 struct lttcomm_session_destroy_command_header cmd_header
;
3159 struct lttng_trace_archive_location
*location
= NULL
;
3160 struct lttcomm_lttng_msg llm
= {
3161 .cmd_type
= LTTNG_DESTROY_SESSION
,
3162 .ret_code
= reply_context
->destruction_status
,
3165 sizeof(struct lttcomm_session_destroy_command_header
),
3168 size_t payload_size_before_location
;
3170 lttng_dynamic_buffer_init(&payload
);
3172 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3174 ERR("Failed to append session destruction message");
3178 cmd_header
.rotation_state
=
3179 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3180 session
->rotation_state
:
3181 LTTNG_ROTATION_STATE_NO_ROTATION
);
3182 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3183 sizeof(cmd_header
));
3185 ERR("Failed to append session destruction command header");
3189 if (!reply_context
->implicit_rotation_on_destroy
) {
3190 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3194 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3195 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3200 location
= session_get_trace_archive_location(session
);
3202 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3207 payload_size_before_location
= payload
.size
;
3208 comm_ret
= lttng_trace_archive_location_serialize(location
,
3211 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3215 /* Update the message to indicate the location's length. */
3216 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3217 payload
.size
- payload_size_before_location
;
3219 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3220 payload
.data
, payload
.size
);
3221 if (comm_ret
!= (ssize_t
) payload
.size
) {
3222 ERR("Failed to send result of the destruction of session \"%s\" to client",
3226 ret
= close(reply_context
->reply_sock_fd
);
3228 PERROR("Failed to close client socket in deferred session destroy reply");
3230 lttng_dynamic_buffer_reset(&payload
);
3231 free(_reply_context
);
3235 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3237 * Called with session lock held.
3239 int cmd_destroy_session(struct ltt_session
*session
,
3240 struct notification_thread_handle
*notification_thread_handle
,
3244 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3245 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3248 reply_context
= zmalloc(sizeof(*reply_context
));
3249 if (!reply_context
) {
3250 ret
= LTTNG_ERR_NOMEM
;
3253 reply_context
->reply_sock_fd
= *sock_fd
;
3259 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3261 if (session
->active
) {
3262 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3264 ret
= cmd_stop_trace(session
);
3265 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3266 /* Carry on with the destruction of the session. */
3267 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3268 session
->name
, lttng_strerror(-ret
));
3269 destruction_last_error
= ret
;
3273 if (session
->rotation_schedule_timer_enabled
) {
3274 if (timer_session_rotation_schedule_timer_stop(
3276 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3278 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3282 if (session
->rotate_size
) {
3283 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3284 session
->rotate_size
= 0;
3287 if (session
->most_recent_chunk_id
.is_set
&&
3288 session
->most_recent_chunk_id
.value
!= 0 &&
3289 session
->current_trace_chunk
&& session
->output_traces
) {
3291 * Perform a last rotation on destruction if rotations have
3292 * occurred during the session's lifetime.
3294 ret
= cmd_rotate_session(session
, NULL
, false);
3295 if (ret
!= LTTNG_OK
) {
3296 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3297 session
->name
, lttng_strerror(-ret
));
3298 destruction_last_error
= -ret
;
3300 if (reply_context
) {
3301 reply_context
->implicit_rotation_on_destroy
= true;
3303 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3305 * The user has not triggered a session rotation. However, to
3306 * ensure all data has been consumed, the session is rotated
3307 * to a 'null' trace chunk before it is destroyed.
3309 * This is a "quiet" rotation meaning that no notification is
3310 * emitted and no renaming of the current trace chunk takes
3313 ret
= cmd_rotate_session(session
, NULL
, true);
3314 if (ret
!= LTTNG_OK
) {
3315 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3316 session
->name
, lttng_strerror(-ret
));
3317 destruction_last_error
= -ret
;
3321 if (session
->shm_path
[0]) {
3323 * When a session is created with an explicit shm_path,
3324 * the consumer daemon will create its shared memory files
3325 * at that location and will *not* unlink them. This is normal
3326 * as the intention of that feature is to make it possible
3327 * to retrieve the content of those files should a crash occur.
3329 * To ensure the content of those files can be used, the
3330 * sessiond daemon will replicate the content of the metadata
3331 * cache in a metadata file.
3333 * On clean-up, it is expected that the consumer daemon will
3334 * unlink the shared memory files and that the session daemon
3335 * will unlink the metadata file. Then, the session's directory
3336 * in the shm path can be removed.
3338 * Unfortunately, a flaw in the design of the sessiond's and
3339 * consumerd's tear down of channels makes it impossible to
3340 * determine when the sessiond _and_ the consumerd have both
3341 * destroyed their representation of a channel. For one, the
3342 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3343 * callbacks in both daemons.
3345 * However, it is also impossible for the sessiond to know when
3346 * the consumer daemon is done destroying its channel(s) since
3347 * it occurs as a reaction to the closing of the channel's file
3348 * descriptor. There is no resulting communication initiated
3349 * from the consumerd to the sessiond to confirm that the
3350 * operation is completed (and was successful).
3352 * Until this is all fixed, the session daemon checks for the
3353 * removal of the session's shm path which makes it possible
3354 * to safely advertise a session as having been destroyed.
3356 * Prior to this fix, it was not possible to reliably save
3357 * a session making use of the --shm-path option, destroy it,
3358 * and load it again. This is because the creation of the
3359 * session would fail upon seeing the session's shm path
3360 * already in existence.
3362 * Note that none of the error paths in the check for the
3363 * directory's existence return an error. This is normal
3364 * as there isn't much that can be done. The session will
3365 * be destroyed properly, except that we can't offer the
3366 * guarantee that the same session can be re-created.
3368 current_completion_handler
= &destroy_completion_handler
.handler
;
3369 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3371 sizeof(destroy_completion_handler
.shm_path
));
3376 * The session is destroyed. However, note that the command context
3377 * still holds a reference to the session, thus delaying its destruction
3378 * _at least_ up to the point when that reference is released.
3380 session_destroy(session
);
3381 if (reply_context
) {
3382 reply_context
->destruction_status
= destruction_last_error
;
3383 ret
= session_add_destroy_notifier(session
,
3384 cmd_destroy_session_reply
,
3385 (void *) reply_context
);
3387 ret
= LTTNG_ERR_FATAL
;
3399 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3401 int cmd_register_consumer(struct ltt_session
*session
,
3402 enum lttng_domain_type domain
, const char *sock_path
,
3403 struct consumer_data
*cdata
)
3406 struct consumer_socket
*socket
= NULL
;
3413 case LTTNG_DOMAIN_KERNEL
:
3415 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3419 /* Can't register a consumer if there is already one */
3420 if (ksess
->consumer_fds_sent
!= 0) {
3421 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3425 sock
= lttcomm_connect_unix_sock(sock_path
);
3427 ret
= LTTNG_ERR_CONNECT_FAIL
;
3430 cdata
->cmd_sock
= sock
;
3432 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3433 if (socket
== NULL
) {
3436 PERROR("close register consumer");
3438 cdata
->cmd_sock
= -1;
3439 ret
= LTTNG_ERR_FATAL
;
3443 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3444 if (socket
->lock
== NULL
) {
3445 PERROR("zmalloc pthread mutex");
3446 ret
= LTTNG_ERR_FATAL
;
3449 pthread_mutex_init(socket
->lock
, NULL
);
3450 socket
->registered
= 1;
3453 consumer_add_socket(socket
, ksess
->consumer
);
3456 pthread_mutex_lock(&cdata
->pid_mutex
);
3458 pthread_mutex_unlock(&cdata
->pid_mutex
);
3463 /* TODO: Userspace tracing */
3464 ret
= LTTNG_ERR_UND
;
3472 consumer_destroy_socket(socket
);
3478 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3480 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3481 struct lttng_domain
**domains
)
3486 struct lttng_ht_iter iter
;
3488 if (session
->kernel_session
!= NULL
) {
3489 DBG3("Listing domains found kernel domain");
3493 if (session
->ust_session
!= NULL
) {
3494 DBG3("Listing domains found UST global domain");
3498 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3500 if (agt
->being_used
) {
3511 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3512 if (*domains
== NULL
) {
3513 ret
= LTTNG_ERR_FATAL
;
3517 if (session
->kernel_session
!= NULL
) {
3518 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3520 /* Kernel session buffer type is always GLOBAL */
3521 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3526 if (session
->ust_session
!= NULL
) {
3527 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3528 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3532 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3534 if (agt
->being_used
) {
3535 (*domains
)[index
].type
= agt
->domain
;
3536 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3546 /* Return negative value to differentiate return code */
3552 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3554 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3555 struct ltt_session
*session
, struct lttng_channel
**channels
)
3557 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3560 case LTTNG_DOMAIN_KERNEL
:
3561 if (session
->kernel_session
!= NULL
) {
3562 nb_chan
= session
->kernel_session
->channel_count
;
3564 DBG3("Number of kernel channels %zd", nb_chan
);
3566 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3570 case LTTNG_DOMAIN_UST
:
3571 if (session
->ust_session
!= NULL
) {
3573 nb_chan
= lttng_ht_get_count(
3574 session
->ust_session
->domain_global
.channels
);
3577 DBG3("Number of UST global channels %zd", nb_chan
);
3579 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3584 ret
= -LTTNG_ERR_UND
;
3589 const size_t channel_size
= sizeof(struct lttng_channel
) +
3590 sizeof(struct lttng_channel_extended
);
3591 struct lttng_channel_extended
*channel_exts
;
3593 payload_size
= nb_chan
* channel_size
;
3594 *channels
= zmalloc(payload_size
);
3595 if (*channels
== NULL
) {
3596 ret
= -LTTNG_ERR_FATAL
;
3600 channel_exts
= ((void *) *channels
) +
3601 (nb_chan
* sizeof(struct lttng_channel
));
3602 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3603 if (ret
!= LTTNG_OK
) {
3618 * Command LTTNG_LIST_EVENTS processed by the client thread.
3620 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3621 struct ltt_session
*session
, char *channel_name
,
3622 struct lttng_event
**events
, size_t *total_size
)
3625 ssize_t nb_event
= 0;
3628 case LTTNG_DOMAIN_KERNEL
:
3629 if (session
->kernel_session
!= NULL
) {
3630 nb_event
= list_lttng_kernel_events(channel_name
,
3631 session
->kernel_session
, events
,
3635 case LTTNG_DOMAIN_UST
:
3637 if (session
->ust_session
!= NULL
) {
3638 nb_event
= list_lttng_ust_global_events(channel_name
,
3639 &session
->ust_session
->domain_global
, events
,
3644 case LTTNG_DOMAIN_LOG4J
:
3645 case LTTNG_DOMAIN_JUL
:
3646 case LTTNG_DOMAIN_PYTHON
:
3647 if (session
->ust_session
) {
3648 struct lttng_ht_iter iter
;
3652 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3653 &iter
.iter
, agt
, node
.node
) {
3654 if (agt
->domain
== domain
) {
3655 nb_event
= list_lttng_agent_events(
3665 ret
= LTTNG_ERR_UND
;
3672 /* Return negative value to differentiate return code */
3677 * Using the session list, filled a lttng_session array to send back to the
3678 * client for session listing.
3680 * The session list lock MUST be acquired before calling this function. Use
3681 * session_lock_list() and session_unlock_list().
3683 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3684 size_t session_count
, uid_t uid
, gid_t gid
)
3688 struct ltt_session
*session
;
3689 struct ltt_session_list
*list
= session_get_list();
3690 struct lttng_session_extended
*extended
=
3691 (typeof(extended
)) (&sessions
[session_count
]);
3693 DBG("Getting all available session for UID %d GID %d",
3696 * Iterate over session list and append data after the control struct in
3699 cds_list_for_each_entry(session
, &list
->head
, list
) {
3700 if (!session_get(session
)) {
3704 * Only list the sessions the user can control.
3706 if (!session_access_ok(session
, uid
, gid
) ||
3707 session
->destroyed
) {
3708 session_put(session
);
3712 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3713 struct ltt_ust_session
*usess
= session
->ust_session
;
3715 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3716 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3717 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3718 ret
= build_network_session_path(sessions
[i
].path
,
3719 sizeof(sessions
[i
].path
), session
);
3721 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3722 session
->consumer
->dst
.session_root_path
);
3725 PERROR("snprintf session path");
3726 session_put(session
);
3730 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3731 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3732 sessions
[i
].enabled
= session
->active
;
3733 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3734 sessions
[i
].live_timer_interval
= session
->live_timer
;
3735 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3736 extended
[i
].creation_time
.is_set
= 1;
3738 session_put(session
);
3743 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3744 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3746 int cmd_data_pending(struct ltt_session
*session
)
3749 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3750 struct ltt_ust_session
*usess
= session
->ust_session
;
3754 DBG("Data pending for session %s", session
->name
);
3756 /* Session MUST be stopped to ask for data availability. */
3757 if (session
->active
) {
3758 ret
= LTTNG_ERR_SESSION_STARTED
;
3762 * If stopped, just make sure we've started before else the above call
3763 * will always send that there is data pending.
3765 * The consumer assumes that when the data pending command is received,
3766 * the trace has been started before or else no output data is written
3767 * by the streams which is a condition for data pending. So, this is
3768 * *VERY* important that we don't ask the consumer before a start
3771 if (!session
->has_been_started
) {
3777 /* A rotation is still pending, we have to wait. */
3778 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3779 DBG("Rotate still pending for session %s", session
->name
);
3784 if (ksess
&& ksess
->consumer
) {
3785 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3787 /* Data is still being extracted for the kernel. */
3792 if (usess
&& usess
->consumer
) {
3793 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3795 /* Data is still being extracted for the kernel. */
3800 /* Data is ready to be read by a viewer */
3808 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3810 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3812 int cmd_snapshot_add_output(struct ltt_session
*session
,
3813 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3816 struct snapshot_output
*new_output
;
3821 DBG("Cmd snapshot add output for session %s", session
->name
);
3824 * Can't create an output if the session is not set in no-output mode.
3826 if (session
->output_traces
) {
3827 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3831 if (session
->has_non_mmap_channel
) {
3832 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3836 /* Only one output is allowed until we have the "tee" feature. */
3837 if (session
->snapshot
.nb_output
== 1) {
3838 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3842 new_output
= snapshot_output_alloc();
3844 ret
= LTTNG_ERR_NOMEM
;
3848 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3849 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3850 &session
->snapshot
);
3852 if (ret
== -ENOMEM
) {
3853 ret
= LTTNG_ERR_NOMEM
;
3855 ret
= LTTNG_ERR_INVALID
;
3861 snapshot_add_output(&session
->snapshot
, new_output
);
3863 *id
= new_output
->id
;
3870 snapshot_output_destroy(new_output
);
3876 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3878 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3880 int cmd_snapshot_del_output(struct ltt_session
*session
,
3881 const struct lttng_snapshot_output
*output
)
3884 struct snapshot_output
*sout
= NULL
;
3892 * Permission denied to create an output if the session is not
3893 * set in no output mode.
3895 if (session
->output_traces
) {
3896 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3901 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3903 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3904 } else if (*output
->name
!= '\0') {
3905 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3907 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3910 ret
= LTTNG_ERR_INVALID
;
3914 snapshot_delete_output(&session
->snapshot
, sout
);
3915 snapshot_output_destroy(sout
);
3924 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3926 * If no output is available, outputs is untouched and 0 is returned.
3928 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3930 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3931 struct lttng_snapshot_output
**outputs
)
3934 struct lttng_snapshot_output
*list
= NULL
;
3935 struct lttng_ht_iter iter
;
3936 struct snapshot_output
*output
;
3941 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3944 * Permission denied to create an output if the session is not
3945 * set in no output mode.
3947 if (session
->output_traces
) {
3948 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3952 if (session
->snapshot
.nb_output
== 0) {
3957 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3959 ret
= -LTTNG_ERR_NOMEM
;
3963 /* Copy list from session to the new list object. */
3965 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3966 output
, node
.node
) {
3967 assert(output
->consumer
);
3968 list
[idx
].id
= output
->id
;
3969 list
[idx
].max_size
= output
->max_size
;
3970 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3971 sizeof(list
[idx
].name
))) {
3972 ret
= -LTTNG_ERR_INVALID
;
3975 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3976 if (lttng_strncpy(list
[idx
].ctrl_url
,
3977 output
->consumer
->dst
.session_root_path
,
3978 sizeof(list
[idx
].ctrl_url
))) {
3979 ret
= -LTTNG_ERR_INVALID
;
3984 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3985 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3987 ret
= -LTTNG_ERR_NOMEM
;
3992 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3993 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3995 ret
= -LTTNG_ERR_NOMEM
;
4004 ret
= session
->snapshot
.nb_output
;
4013 * Check if we can regenerate the metadata for this session.
4014 * Only kernel, UST per-uid and non-live sessions are supported.
4016 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4019 int check_regenerate_metadata_support(struct ltt_session
*session
)
4025 if (session
->live_timer
!= 0) {
4026 ret
= LTTNG_ERR_LIVE_SESSION
;
4029 if (!session
->active
) {
4030 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4033 if (session
->ust_session
) {
4034 switch (session
->ust_session
->buffer_type
) {
4035 case LTTNG_BUFFER_PER_UID
:
4037 case LTTNG_BUFFER_PER_PID
:
4038 ret
= LTTNG_ERR_PER_PID_SESSION
;
4042 ret
= LTTNG_ERR_UNK
;
4046 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4047 session
->consumer
->relay_minor_version
< 8) {
4048 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4058 int clear_metadata_file(int fd
)
4063 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4064 if (lseek_ret
< 0) {
4070 ret
= ftruncate(fd
, 0);
4072 PERROR("ftruncate");
4081 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4084 struct buffer_reg_uid
*uid_reg
= NULL
;
4085 struct buffer_reg_session
*session_reg
= NULL
;
4088 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4089 struct ust_registry_session
*registry
;
4090 struct ust_registry_channel
*chan
;
4091 struct lttng_ht_iter iter_chan
;
4093 session_reg
= uid_reg
->registry
;
4094 registry
= session_reg
->reg
.ust
;
4096 pthread_mutex_lock(®istry
->lock
);
4097 registry
->metadata_len_sent
= 0;
4098 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4099 registry
->metadata_len
= 0;
4100 registry
->metadata_version
++;
4101 if (registry
->metadata_fd
> 0) {
4102 /* Clear the metadata file's content. */
4103 ret
= clear_metadata_file(registry
->metadata_fd
);
4105 pthread_mutex_unlock(®istry
->lock
);
4110 ret
= ust_metadata_session_statedump(registry
, NULL
,
4111 registry
->major
, registry
->minor
);
4113 pthread_mutex_unlock(®istry
->lock
);
4114 ERR("Failed to generate session metadata (err = %d)",
4118 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4120 struct ust_registry_event
*event
;
4121 struct lttng_ht_iter iter_event
;
4123 ret
= ust_metadata_channel_statedump(registry
, chan
);
4125 pthread_mutex_unlock(®istry
->lock
);
4126 ERR("Failed to generate channel metadata "
4130 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4132 ret
= ust_metadata_event_statedump(registry
,
4135 pthread_mutex_unlock(®istry
->lock
);
4136 ERR("Failed to generate event metadata "
4142 pthread_mutex_unlock(®istry
->lock
);
4151 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4153 * Ask the consumer to truncate the existing metadata file(s) and
4154 * then regenerate the metadata. Live and per-pid sessions are not
4155 * supported and return an error.
4157 * Return 0 on success or else a LTTNG_ERR code.
4159 int cmd_regenerate_metadata(struct ltt_session
*session
)
4165 ret
= check_regenerate_metadata_support(session
);
4170 if (session
->kernel_session
) {
4171 ret
= kernctl_session_regenerate_metadata(
4172 session
->kernel_session
->fd
);
4174 ERR("Failed to regenerate the kernel metadata");
4179 if (session
->ust_session
) {
4180 ret
= ust_regenerate_metadata(session
->ust_session
);
4182 ERR("Failed to regenerate the UST metadata");
4186 DBG("Cmd metadata regenerate for session %s", session
->name
);
4194 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4196 * Ask the tracer to regenerate a new statedump.
4198 * Return 0 on success or else a LTTNG_ERR code.
4200 int cmd_regenerate_statedump(struct ltt_session
*session
)
4206 if (!session
->active
) {
4207 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4211 if (session
->kernel_session
) {
4212 ret
= kernctl_session_regenerate_statedump(
4213 session
->kernel_session
->fd
);
4215 * Currently, the statedump in kernel can only fail if out
4219 if (ret
== -ENOMEM
) {
4220 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4222 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4224 ERR("Failed to regenerate the kernel statedump");
4229 if (session
->ust_session
) {
4230 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4232 * Currently, the statedump in UST always returns 0.
4235 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4236 ERR("Failed to regenerate the UST statedump");
4240 DBG("Cmd regenerate statedump for session %s", session
->name
);
4247 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4248 struct notification_thread_handle
*notification_thread
)
4252 ssize_t sock_recv_len
;
4253 struct lttng_trigger
*trigger
= NULL
;
4254 struct lttng_buffer_view view
;
4255 struct lttng_dynamic_buffer trigger_buffer
;
4257 lttng_dynamic_buffer_init(&trigger_buffer
);
4258 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4259 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4261 ret
= LTTNG_ERR_NOMEM
;
4265 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4267 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4268 ERR("Failed to receive \"register trigger\" command payload");
4269 /* TODO: should this be a new error enum ? */
4270 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4274 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4275 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4277 ERR("Invalid trigger payload received in \"register trigger\" command");
4278 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4282 ret
= notification_thread_command_register_trigger(notification_thread
,
4284 /* Ownership of trigger was transferred. */
4287 lttng_trigger_destroy(trigger
);
4288 lttng_dynamic_buffer_reset(&trigger_buffer
);
4292 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4293 struct notification_thread_handle
*notification_thread
)
4297 ssize_t sock_recv_len
;
4298 struct lttng_trigger
*trigger
= NULL
;
4299 struct lttng_buffer_view view
;
4300 struct lttng_dynamic_buffer trigger_buffer
;
4302 lttng_dynamic_buffer_init(&trigger_buffer
);
4303 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4304 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4306 ret
= LTTNG_ERR_NOMEM
;
4310 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4312 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4313 ERR("Failed to receive \"unregister trigger\" command payload");
4314 /* TODO: should this be a new error enum ? */
4315 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4319 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4320 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4322 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4323 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4327 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4330 lttng_trigger_destroy(trigger
);
4331 lttng_dynamic_buffer_reset(&trigger_buffer
);
4336 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4337 * snapshot output is *not* set with a remote destination.
4339 * Return LTTNG_OK on success or a LTTNG_ERR code.
4341 static enum lttng_error_code
set_relayd_for_snapshot(
4342 struct consumer_output
*output
,
4343 const struct ltt_session
*session
)
4345 enum lttng_error_code status
= LTTNG_OK
;
4346 struct lttng_ht_iter iter
;
4347 struct consumer_socket
*socket
;
4348 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4349 const char *base_path
;
4354 DBG2("Set relayd object from snapshot output");
4356 if (session
->current_trace_chunk
) {
4357 enum lttng_trace_chunk_status chunk_status
=
4358 lttng_trace_chunk_get_id(
4359 session
->current_trace_chunk
,
4360 ¤t_chunk_id
.value
);
4362 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4363 current_chunk_id
.is_set
= true;
4365 ERR("Failed to get current trace chunk id");
4366 status
= LTTNG_ERR_UNK
;
4371 /* Ignore if snapshot consumer output is not network. */
4372 if (output
->type
!= CONSUMER_DST_NET
) {
4377 * The snapshot record URI base path overrides the session
4380 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4381 base_path
= output
->dst
.net
.control
.subdir
;
4383 base_path
= session
->base_path
;
4387 * For each consumer socket, create and send the relayd object of the
4391 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4392 socket
, node
.node
) {
4393 pthread_mutex_lock(socket
->lock
);
4394 status
= send_consumer_relayd_sockets(0, session
->id
,
4396 session
->name
, session
->hostname
,
4398 session
->live_timer
,
4399 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4400 session
->creation_time
,
4401 session
->name_contains_creation_time
);
4402 pthread_mutex_unlock(socket
->lock
);
4403 if (status
!= LTTNG_OK
) {
4415 * Record a kernel snapshot.
4417 * Return LTTNG_OK on success or a LTTNG_ERR code.
4419 static enum lttng_error_code
record_kernel_snapshot(
4420 struct ltt_kernel_session
*ksess
,
4421 const struct consumer_output
*output
,
4422 const struct ltt_session
*session
,
4423 int wait
, uint64_t nb_packets_per_stream
)
4425 enum lttng_error_code status
;
4431 status
= kernel_snapshot_record(
4432 ksess
, output
, wait
, nb_packets_per_stream
);
4437 * Record a UST snapshot.
4439 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4441 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4442 const struct consumer_output
*output
,
4443 const struct ltt_session
*session
,
4444 int wait
, uint64_t nb_packets_per_stream
)
4446 enum lttng_error_code status
;
4452 status
= ust_app_snapshot_record(
4453 usess
, output
, wait
, nb_packets_per_stream
);
4458 uint64_t get_session_size_one_more_packet_per_stream(
4459 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4461 uint64_t tot_size
= 0;
4463 if (session
->kernel_session
) {
4464 struct ltt_kernel_channel
*chan
;
4465 const struct ltt_kernel_session
*ksess
=
4466 session
->kernel_session
;
4468 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4469 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4471 * Don't take channel into account if we
4472 * already grab all its packets.
4476 tot_size
+= chan
->channel
->attr
.subbuf_size
4477 * chan
->stream_count
;
4481 if (session
->ust_session
) {
4482 const struct ltt_ust_session
*usess
= session
->ust_session
;
4484 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4492 * Calculate the number of packets we can grab from each stream that
4493 * fits within the overall snapshot max size.
4495 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4496 * the number of packets per stream.
4498 * TODO: this approach is not perfect: we consider the worse case
4499 * (packet filling the sub-buffers) as an upper bound, but we could do
4500 * better if we do this calculation while we actually grab the packet
4501 * content: we would know how much padding we don't actually store into
4504 * This algorithm is currently bounded by the number of packets per
4507 * Since we call this algorithm before actually grabbing the data, it's
4508 * an approximation: for instance, applications could appear/disappear
4509 * in between this call and actually grabbing data.
4512 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4516 uint64_t cur_nb_packets
= 0;
4519 return 0; /* Infinite */
4522 size_left
= max_size
;
4524 uint64_t one_more_packet_tot_size
;
4526 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4527 session
, cur_nb_packets
);
4528 if (!one_more_packet_tot_size
) {
4529 /* We are already grabbing all packets. */
4532 size_left
-= one_more_packet_tot_size
;
4533 if (size_left
< 0) {
4538 if (!cur_nb_packets
&& size_left
!= max_size
) {
4539 /* Not enough room to grab one packet of each stream, error. */
4542 return cur_nb_packets
;
4546 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4547 const struct snapshot_output
*snapshot_output
, int wait
)
4549 int64_t nb_packets_per_stream
;
4550 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4552 enum lttng_error_code ret_code
= LTTNG_OK
;
4553 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4554 struct consumer_output
*original_ust_consumer_output
= NULL
;
4555 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4556 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4557 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4559 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4561 snapshot_output
->name
,
4562 snapshot_output
->datetime
,
4563 snapshot_output
->nb_snapshot
);
4564 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4565 ERR("Failed to format snapshot name");
4566 ret_code
= LTTNG_ERR_INVALID
;
4569 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4570 snapshot_output
->name
, session
->name
,
4571 snapshot_chunk_name
);
4572 if (!session
->kernel_session
&& !session
->ust_session
) {
4573 ERR("Failed to record snapshot as no channels exist");
4574 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4578 if (session
->kernel_session
) {
4579 original_kernel_consumer_output
=
4580 session
->kernel_session
->consumer
;
4581 snapshot_kernel_consumer_output
=
4582 consumer_copy_output(snapshot_output
->consumer
);
4583 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
4584 snapshot_chunk_name
);
4585 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4586 original_kernel_consumer_output
);
4588 ERR("Failed to copy consumer sockets from snapshot output configuration");
4589 ret_code
= LTTNG_ERR_NOMEM
;
4592 ret_code
= set_relayd_for_snapshot(
4593 snapshot_kernel_consumer_output
, session
);
4594 if (ret_code
!= LTTNG_OK
) {
4595 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4598 session
->kernel_session
->consumer
=
4599 snapshot_kernel_consumer_output
;
4601 if (session
->ust_session
) {
4602 original_ust_consumer_output
= session
->ust_session
->consumer
;
4603 snapshot_ust_consumer_output
=
4604 consumer_copy_output(snapshot_output
->consumer
);
4605 strcpy(snapshot_ust_consumer_output
->chunk_path
,
4606 snapshot_chunk_name
);
4607 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4608 original_ust_consumer_output
);
4610 ERR("Failed to copy consumer sockets from snapshot output configuration");
4611 ret_code
= LTTNG_ERR_NOMEM
;
4614 ret_code
= set_relayd_for_snapshot(
4615 snapshot_ust_consumer_output
, session
);
4616 if (ret_code
!= LTTNG_OK
) {
4617 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4620 session
->ust_session
->consumer
=
4621 snapshot_ust_consumer_output
;
4624 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4625 snapshot_kernel_consumer_output
?:
4626 snapshot_ust_consumer_output
,
4627 consumer_output_get_base_path(
4628 snapshot_output
->consumer
),
4629 snapshot_chunk_name
);
4630 if (!snapshot_trace_chunk
) {
4631 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4633 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4636 assert(!session
->current_trace_chunk
);
4637 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4638 lttng_trace_chunk_put(snapshot_trace_chunk
);
4639 snapshot_trace_chunk
= NULL
;
4641 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4643 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4647 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4648 snapshot_output
->max_size
);
4649 if (nb_packets_per_stream
< 0) {
4650 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4651 goto error_close_trace_chunk
;
4654 if (session
->kernel_session
) {
4655 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4656 snapshot_kernel_consumer_output
, session
,
4657 wait
, nb_packets_per_stream
);
4658 if (ret_code
!= LTTNG_OK
) {
4659 goto error_close_trace_chunk
;
4663 if (session
->ust_session
) {
4664 ret_code
= record_ust_snapshot(session
->ust_session
,
4665 snapshot_ust_consumer_output
, session
,
4666 wait
, nb_packets_per_stream
);
4667 if (ret_code
!= LTTNG_OK
) {
4668 goto error_close_trace_chunk
;
4671 error_close_trace_chunk
:
4672 if (session_close_trace_chunk(
4673 session
, session
->current_trace_chunk
, NULL
, NULL
)) {
4675 * Don't goto end; make sure the chunk is closed for the session
4676 * to allow future snapshots.
4678 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4680 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4682 if (session_set_trace_chunk(session
, NULL
, NULL
)) {
4683 ERR("Failed to release the current trace chunk of session \"%s\"",
4685 ret_code
= LTTNG_ERR_UNK
;
4688 if (original_ust_consumer_output
) {
4689 session
->ust_session
->consumer
= original_ust_consumer_output
;
4691 if (original_kernel_consumer_output
) {
4692 session
->kernel_session
->consumer
=
4693 original_kernel_consumer_output
;
4695 consumer_output_put(snapshot_ust_consumer_output
);
4696 consumer_output_put(snapshot_kernel_consumer_output
);
4701 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4703 * The wait parameter is ignored so this call always wait for the snapshot to
4704 * complete before returning.
4706 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4708 int cmd_snapshot_record(struct ltt_session
*session
,
4709 const struct lttng_snapshot_output
*output
, int wait
)
4711 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4713 unsigned int snapshot_success
= 0;
4715 struct snapshot_output
*tmp_output
= NULL
;
4720 DBG("Cmd snapshot record for session %s", session
->name
);
4722 /* Get the datetime for the snapshot output directory. */
4723 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4726 cmd_ret
= LTTNG_ERR_INVALID
;
4731 * Permission denied to create an output if the session is not
4732 * set in no output mode.
4734 if (session
->output_traces
) {
4735 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4739 /* The session needs to be started at least once. */
4740 if (!session
->has_been_started
) {
4741 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4745 /* Use temporary output for the session. */
4746 if (*output
->ctrl_url
!= '\0') {
4747 tmp_output
= snapshot_output_alloc();
4749 cmd_ret
= LTTNG_ERR_NOMEM
;
4753 ret
= snapshot_output_init(session
, output
->max_size
,
4755 output
->ctrl_url
, output
->data_url
,
4759 if (ret
== -ENOMEM
) {
4760 cmd_ret
= LTTNG_ERR_NOMEM
;
4762 cmd_ret
= LTTNG_ERR_INVALID
;
4766 /* Use the global session count for the temporary snapshot. */
4767 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
4769 /* Use the global datetime */
4770 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
4771 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
4772 if (cmd_ret
!= LTTNG_OK
) {
4775 snapshot_success
= 1;
4777 struct snapshot_output
*sout
;
4778 struct lttng_ht_iter iter
;
4781 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4782 &iter
.iter
, sout
, node
.node
) {
4783 struct snapshot_output output_copy
;
4786 * Make a local copy of the output and override output
4787 * parameters with those provided as part of the
4790 memcpy(&output_copy
, sout
, sizeof(output_copy
));
4792 if (output
->max_size
!= (uint64_t) -1ULL) {
4793 output_copy
.max_size
= output
->max_size
;
4796 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4797 memcpy(output_copy
.datetime
, datetime
,
4800 /* Use temporary name. */
4801 if (*output
->name
!= '\0') {
4802 if (lttng_strncpy(output_copy
.name
,
4804 sizeof(output_copy
.name
))) {
4805 cmd_ret
= LTTNG_ERR_INVALID
;
4811 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
4812 if (cmd_ret
!= LTTNG_OK
) {
4816 snapshot_success
= 1;
4821 if (snapshot_success
) {
4822 session
->snapshot
.nb_snapshot
++;
4824 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4829 snapshot_output_destroy(tmp_output
);
4835 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4837 int cmd_set_session_shm_path(struct ltt_session
*session
,
4838 const char *shm_path
)
4844 * Can only set shm path before session is started.
4846 if (session
->has_been_started
) {
4847 return LTTNG_ERR_SESSION_STARTED
;
4850 strncpy(session
->shm_path
, shm_path
,
4851 sizeof(session
->shm_path
));
4852 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4858 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4860 * Ask the consumer to rotate the session output directory.
4861 * The session lock must be held.
4863 * Returns LTTNG_OK on success or else a negative LTTng error code.
4865 int cmd_rotate_session(struct ltt_session
*session
,
4866 struct lttng_rotate_session_return
*rotate_return
,
4867 bool quiet_rotation
)
4870 uint64_t ongoing_rotation_chunk_id
;
4871 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4872 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
4873 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
4874 enum lttng_trace_chunk_status chunk_status
;
4875 bool failed_to_rotate
= false;
4876 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
4880 if (!session
->has_been_started
) {
4881 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4886 * Explicit rotation is not supported for live sessions.
4887 * However, live sessions can perform a quiet rotation on
4889 * Rotation is not supported for snapshot traces (no output).
4891 if ((!quiet_rotation
&& session
->live_timer
) ||
4892 !session
->output_traces
) {
4893 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4897 /* Unsupported feature in lttng-relayd before 2.11. */
4898 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
4899 (session
->consumer
->relay_major_version
== 2 &&
4900 session
->consumer
->relay_minor_version
< 11)) {
4901 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
4905 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
4906 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
4907 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
4911 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4912 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
4914 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
4919 * After a stop, we only allow one rotation to occur, the other ones are
4920 * useless until a new start.
4922 if (session
->rotated_after_last_stop
) {
4923 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4925 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
4928 if (session
->active
) {
4929 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
4931 if (!new_trace_chunk
) {
4932 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
4938 * The current trace chunk becomes the chunk being archived.
4940 * After this point, "chunk_being_archived" must absolutely
4941 * be closed on the consumer(s), otherwise it will never be
4942 * cleaned-up, which will result in a leak.
4944 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
4945 &chunk_being_archived
);
4947 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4951 if (session
->kernel_session
) {
4952 cmd_ret
= kernel_rotate_session(session
);
4953 if (cmd_ret
!= LTTNG_OK
) {
4954 failed_to_rotate
= true;
4955 rotation_fail_code
= cmd_ret
;
4958 if (session
->ust_session
) {
4959 cmd_ret
= ust_app_rotate_session(session
);
4960 if (cmd_ret
!= LTTNG_OK
) {
4961 failed_to_rotate
= true;
4962 rotation_fail_code
= cmd_ret
;
4966 if (!session
->active
) {
4967 session
->rotated_after_last_stop
= true;
4970 if (!chunk_being_archived
) {
4971 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
4973 if (failed_to_rotate
) {
4974 cmd_ret
= rotation_fail_code
;
4981 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
4982 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
4983 &ongoing_rotation_chunk_id
);
4984 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
4986 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
4989 &((enum lttng_trace_chunk_command_type
){
4990 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
}),
4991 session
->last_chunk_path
);
4993 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4997 if (failed_to_rotate
) {
4998 cmd_ret
= rotation_fail_code
;
5002 session
->quiet_rotation
= quiet_rotation
;
5003 ret
= timer_session_rotation_pending_check_start(session
,
5004 DEFAULT_ROTATE_PENDING_TIMER
);
5006 cmd_ret
= LTTNG_ERR_UNK
;
5010 if (rotate_return
) {
5011 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5014 session
->chunk_being_archived
= chunk_being_archived
;
5015 chunk_being_archived
= NULL
;
5016 if (!quiet_rotation
) {
5017 ret
= notification_thread_command_session_rotation_ongoing(
5018 notification_thread_handle
,
5019 session
->name
, session
->uid
, session
->gid
,
5020 ongoing_rotation_chunk_id
);
5021 if (ret
!= LTTNG_OK
) {
5022 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5028 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5029 session
->name
, ongoing_rotation_chunk_id
);
5031 lttng_trace_chunk_put(new_trace_chunk
);
5032 lttng_trace_chunk_put(chunk_being_archived
);
5033 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5036 if (session_reset_rotation_state(session
,
5037 LTTNG_ROTATION_STATE_ERROR
)) {
5038 ERR("Failed to reset rotation state of session \"%s\"",
5045 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5047 * Check if the session has finished its rotation.
5049 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5051 int cmd_rotate_get_info(struct ltt_session
*session
,
5052 struct lttng_rotation_get_info_return
*info_return
,
5053 uint64_t rotation_id
)
5055 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5056 enum lttng_rotation_state rotation_state
;
5058 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5059 session
->most_recent_chunk_id
.value
);
5061 if (session
->chunk_being_archived
) {
5062 enum lttng_trace_chunk_status chunk_status
;
5065 chunk_status
= lttng_trace_chunk_get_id(
5066 session
->chunk_being_archived
,
5068 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5070 rotation_state
= rotation_id
== chunk_id
?
5071 LTTNG_ROTATION_STATE_ONGOING
:
5072 LTTNG_ROTATION_STATE_EXPIRED
;
5074 if (session
->last_archived_chunk_id
.is_set
&&
5075 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5076 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5078 rotation_state
= session
->rotation_state
;
5082 switch (rotation_state
) {
5083 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5084 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5087 case LTTNG_ROTATION_STATE_EXPIRED
:
5088 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5089 rotation_id
, session
->name
);
5091 case LTTNG_ROTATION_STATE_ONGOING
:
5092 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5093 rotation_id
, session
->name
);
5095 case LTTNG_ROTATION_STATE_COMPLETED
:
5099 char *current_tracing_path_reply
;
5100 size_t current_tracing_path_reply_len
;
5102 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5103 rotation_id
, session
->name
);
5105 switch (session_get_consumer_destination_type(session
)) {
5106 case CONSUMER_DST_LOCAL
:
5107 current_tracing_path_reply
=
5108 info_return
->location
.local
.absolute_path
;
5109 current_tracing_path_reply_len
=
5110 sizeof(info_return
->location
.local
.absolute_path
);
5111 info_return
->location_type
=
5112 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5113 fmt_ret
= asprintf(&chunk_path
,
5114 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5115 session_get_base_path(session
),
5116 session
->last_archived_chunk_name
);
5117 if (fmt_ret
== -1) {
5118 PERROR("Failed to format the path of the last archived trace chunk");
5119 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5120 cmd_ret
= LTTNG_ERR_UNK
;
5124 case CONSUMER_DST_NET
:
5126 uint16_t ctrl_port
, data_port
;
5128 current_tracing_path_reply
=
5129 info_return
->location
.relay
.relative_path
;
5130 current_tracing_path_reply_len
=
5131 sizeof(info_return
->location
.relay
.relative_path
);
5132 /* Currently the only supported relay protocol. */
5133 info_return
->location
.relay
.protocol
=
5134 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5136 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5137 session_get_net_consumer_hostname(session
),
5138 sizeof(info_return
->location
.relay
.host
));
5140 ERR("Failed to copy host name to rotate_get_info reply");
5141 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5142 cmd_ret
= LTTNG_ERR_SET_URL
;
5146 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5147 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5148 info_return
->location
.relay
.ports
.data
= data_port
;
5149 info_return
->location_type
=
5150 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5151 chunk_path
= strdup(session
->last_chunk_path
);
5153 ERR("Failed to allocate the path of the last archived trace chunk");
5154 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5155 cmd_ret
= LTTNG_ERR_UNK
;
5164 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5165 chunk_path
, current_tracing_path_reply_len
);
5168 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5169 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5170 cmd_ret
= LTTNG_ERR_UNK
;
5176 case LTTNG_ROTATION_STATE_ERROR
:
5177 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5178 rotation_id
, session
->name
);
5186 info_return
->status
= (int32_t) rotation_state
;
5191 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5193 * Configure the automatic rotation parameters.
5194 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5195 * 'activate' to false means deactivate the rotation schedule and validate that
5196 * 'new_value' has the same value as the currently active value.
5198 * Return 0 on success or else a positive LTTNG_ERR code.
5200 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5201 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5203 struct notification_thread_handle
*notification_thread_handle
)
5206 uint64_t *parameter_value
;
5210 DBG("Cmd rotate set schedule session %s", session
->name
);
5212 if (session
->live_timer
|| !session
->output_traces
) {
5213 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5214 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5218 switch (schedule_type
) {
5219 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5220 parameter_value
= &session
->rotate_size
;
5222 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5223 parameter_value
= &session
->rotate_timer_period
;
5224 if (new_value
>= UINT_MAX
) {
5225 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5226 new_value
, UINT_MAX
);
5227 ret
= LTTNG_ERR_INVALID
;
5232 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5233 ret
= LTTNG_ERR_INVALID
;
5237 /* Improper use of the API. */
5238 if (new_value
== -1ULL) {
5239 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5240 ret
= LTTNG_ERR_INVALID
;
5245 * As indicated in struct ltt_session's comments, a value of == 0 means
5246 * this schedule rotation type is not in use.
5248 * Reject the command if we were asked to activate a schedule that was
5251 if (activate
&& *parameter_value
!= 0) {
5252 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5253 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5258 * Reject the command if we were asked to deactivate a schedule that was
5261 if (!activate
&& *parameter_value
== 0) {
5262 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5263 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5268 * Reject the command if we were asked to deactivate a schedule that
5271 if (!activate
&& *parameter_value
!= new_value
) {
5272 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5273 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5277 *parameter_value
= activate
? new_value
: 0;
5279 switch (schedule_type
) {
5280 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5281 if (activate
&& session
->active
) {
5283 * Only start the timer if the session is active,
5284 * otherwise it will be started when the session starts.
5286 ret
= timer_session_rotation_schedule_timer_start(
5287 session
, new_value
);
5289 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5290 ret
= LTTNG_ERR_UNK
;
5294 ret
= timer_session_rotation_schedule_timer_stop(
5297 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5298 ret
= LTTNG_ERR_UNK
;
5303 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5305 ret
= subscribe_session_consumed_size_rotation(session
,
5306 new_value
, notification_thread_handle
);
5308 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5309 ret
= LTTNG_ERR_UNK
;
5313 ret
= unsubscribe_session_consumed_size_rotation(session
,
5314 notification_thread_handle
);
5316 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5317 ret
= LTTNG_ERR_UNK
;
5324 /* Would have been caught before. */
5336 /* Wait for a given path to be removed before continuing. */
5337 static enum lttng_error_code
wait_on_path(void *path_data
)
5339 const char *shm_path
= path_data
;
5341 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5347 ret
= stat(shm_path
, &st
);
5349 if (errno
!= ENOENT
) {
5350 PERROR("stat() returned an error while checking for the existence of the shm path");
5352 DBG("shm path no longer exists, completing the destruction of session");
5356 if (!S_ISDIR(st
.st_mode
)) {
5357 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5362 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5368 * Returns a pointer to a handler to run on completion of a command.
5369 * Returns NULL if no handler has to be run for the last command executed.
5371 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5373 struct cmd_completion_handler
*handler
= current_completion_handler
;
5375 current_completion_handler
= NULL
;
5380 * Init command subsystem.
5385 * Set network sequence index to 1 for streams to match a relayd
5386 * socket on the consumer side.
5388 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5389 relayd_net_seq_idx
= 1;
5390 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5392 DBG("Command subsystem initialized");