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 * If the session is a live session, remove the switch timer, the
1512 * live timer does the same thing but sends also synchronisation
1513 * beacons for inactive streams.
1515 if (session
->live_timer
> 0) {
1516 attr
.attr
.live_timer_interval
= session
->live_timer
;
1517 attr
.attr
.switch_timer_interval
= 0;
1520 /* Check for feature support */
1521 switch (domain
->type
) {
1522 case LTTNG_DOMAIN_KERNEL
:
1524 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1525 /* Sampling position of buffer is not supported */
1526 WARN("Kernel tracer does not support buffer monitoring. "
1527 "Setting the monitor interval timer to 0 "
1528 "(disabled) for channel '%s' of session '%s'",
1529 attr
.name
, session
->name
);
1530 lttng_channel_set_monitor_timer_interval(&attr
, 0);
1534 case LTTNG_DOMAIN_UST
:
1536 case LTTNG_DOMAIN_JUL
:
1537 case LTTNG_DOMAIN_LOG4J
:
1538 case LTTNG_DOMAIN_PYTHON
:
1539 if (!agent_tracing_is_enabled()) {
1540 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1541 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1546 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1550 switch (domain
->type
) {
1551 case LTTNG_DOMAIN_KERNEL
:
1553 struct ltt_kernel_channel
*kchan
;
1555 kchan
= trace_kernel_get_channel_by_name(attr
.name
,
1556 session
->kernel_session
);
1557 if (kchan
== NULL
) {
1559 * Don't try to create a channel if the session has been started at
1560 * some point in time before. The tracer does not allow it.
1562 if (session
->has_been_started
) {
1563 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
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
) {
1627 * Don't try to create a channel if the session has been started at
1628 * some point in time before. The tracer does not allow it.
1630 if (session
->has_been_started
) {
1631 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1635 ret
= channel_ust_create(usess
, &attr
, domain
->buf_type
);
1636 if (attr
.name
[0] != '\0') {
1637 usess
->has_non_default_channel
= 1;
1640 ret
= channel_ust_enable(usess
, uchan
);
1645 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1649 if (ret
== LTTNG_OK
&& attr
.attr
.output
!= LTTNG_EVENT_MMAP
) {
1650 session
->has_non_mmap_channel
= true;
1659 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1661 int cmd_disable_event(struct ltt_session
*session
,
1662 enum lttng_domain_type domain
, const char *channel_name
,
1663 const struct lttng_event
*event
)
1666 const char *event_name
;
1668 DBG("Disable event command for event \'%s\'", event
->name
);
1670 event_name
= event
->name
;
1672 /* Error out on unhandled search criteria */
1673 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1674 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1675 ret
= LTTNG_ERR_UNK
;
1682 case LTTNG_DOMAIN_KERNEL
:
1684 struct ltt_kernel_channel
*kchan
;
1685 struct ltt_kernel_session
*ksess
;
1687 ksess
= session
->kernel_session
;
1690 * If a non-default channel has been created in the
1691 * session, explicitely require that -c chan_name needs
1694 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1695 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1699 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1700 if (kchan
== NULL
) {
1701 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1705 switch (event
->type
) {
1706 case LTTNG_EVENT_ALL
:
1707 case LTTNG_EVENT_TRACEPOINT
:
1708 case LTTNG_EVENT_SYSCALL
:
1709 case LTTNG_EVENT_PROBE
:
1710 case LTTNG_EVENT_FUNCTION
:
1711 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1712 if (event_name
[0] == '\0') {
1713 ret
= event_kernel_disable_event(kchan
,
1716 ret
= event_kernel_disable_event(kchan
,
1717 event_name
, event
->type
);
1719 if (ret
!= LTTNG_OK
) {
1724 ret
= LTTNG_ERR_UNK
;
1728 kernel_wait_quiescent();
1731 case LTTNG_DOMAIN_UST
:
1733 struct ltt_ust_channel
*uchan
;
1734 struct ltt_ust_session
*usess
;
1736 usess
= session
->ust_session
;
1738 if (validate_ust_event_name(event_name
)) {
1739 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1744 * If a non-default channel has been created in the
1745 * session, explicitly require that -c chan_name needs
1748 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1749 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1753 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1755 if (uchan
== NULL
) {
1756 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1760 switch (event
->type
) {
1761 case LTTNG_EVENT_ALL
:
1763 * An empty event name means that everything
1764 * should be disabled.
1766 if (event
->name
[0] == '\0') {
1767 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1769 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1772 if (ret
!= LTTNG_OK
) {
1777 ret
= LTTNG_ERR_UNK
;
1781 DBG3("Disable UST event %s in channel %s completed", event_name
,
1785 case LTTNG_DOMAIN_LOG4J
:
1786 case LTTNG_DOMAIN_JUL
:
1787 case LTTNG_DOMAIN_PYTHON
:
1790 struct ltt_ust_session
*usess
= session
->ust_session
;
1794 switch (event
->type
) {
1795 case LTTNG_EVENT_ALL
:
1798 ret
= LTTNG_ERR_UNK
;
1802 agt
= trace_ust_find_agent(usess
, domain
);
1804 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1808 * An empty event name means that everything
1809 * should be disabled.
1811 if (event
->name
[0] == '\0') {
1812 ret
= event_agent_disable_all(usess
, agt
);
1814 ret
= event_agent_disable(usess
, agt
, event_name
);
1816 if (ret
!= LTTNG_OK
) {
1823 ret
= LTTNG_ERR_UND
;
1836 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1838 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1839 char *channel_name
, const struct lttng_event_context
*ctx
, int kwpipe
)
1841 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1842 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1845 * Don't try to add a context if the session has been started at
1846 * some point in time before. The tracer does not allow it and would
1847 * result in a corrupted trace.
1849 if (session
->has_been_started
) {
1850 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1854 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1855 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1856 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1860 case LTTNG_DOMAIN_KERNEL
:
1861 assert(session
->kernel_session
);
1863 if (session
->kernel_session
->channel_count
== 0) {
1864 /* Create default channel */
1865 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1866 if (ret
!= LTTNG_OK
) {
1869 chan_kern_created
= 1;
1871 /* Add kernel context to kernel tracer */
1872 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1873 if (ret
!= LTTNG_OK
) {
1877 case LTTNG_DOMAIN_JUL
:
1878 case LTTNG_DOMAIN_LOG4J
:
1881 * Validate channel name.
1882 * If no channel name is given and the domain is JUL or LOG4J,
1883 * set it to the appropriate domain-specific channel name. If
1884 * a name is provided but does not match the expexted channel
1885 * name, return an error.
1887 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1888 strcmp(channel_name
,
1889 DEFAULT_JUL_CHANNEL_NAME
)) {
1890 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1892 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1893 strcmp(channel_name
,
1894 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1895 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1898 /* break is _not_ missing here. */
1900 case LTTNG_DOMAIN_UST
:
1902 struct ltt_ust_session
*usess
= session
->ust_session
;
1903 unsigned int chan_count
;
1907 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1908 if (chan_count
== 0) {
1909 struct lttng_channel
*attr
;
1910 /* Create default channel */
1911 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1913 ret
= LTTNG_ERR_FATAL
;
1917 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1918 if (ret
!= LTTNG_OK
) {
1922 channel_attr_destroy(attr
);
1923 chan_ust_created
= 1;
1926 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1927 free(app_ctx_provider_name
);
1929 app_ctx_name
= NULL
;
1930 app_ctx_provider_name
= NULL
;
1931 if (ret
!= LTTNG_OK
) {
1937 ret
= LTTNG_ERR_UND
;
1945 if (chan_kern_created
) {
1946 struct ltt_kernel_channel
*kchan
=
1947 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1948 session
->kernel_session
);
1949 /* Created previously, this should NOT fail. */
1951 kernel_destroy_channel(kchan
);
1954 if (chan_ust_created
) {
1955 struct ltt_ust_channel
*uchan
=
1956 trace_ust_find_channel_by_name(
1957 session
->ust_session
->domain_global
.channels
,
1958 DEFAULT_CHANNEL_NAME
);
1959 /* Created previously, this should NOT fail. */
1961 /* Remove from the channel list of the session. */
1962 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1964 trace_ust_destroy_channel(uchan
);
1967 free(app_ctx_provider_name
);
1972 static inline bool name_starts_with(const char *name
, const char *prefix
)
1974 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1976 return !strncmp(name
, prefix
, max_cmp_len
);
1979 /* Perform userspace-specific event name validation */
1980 static int validate_ust_event_name(const char *name
)
1990 * Check name against all internal UST event component namespaces used
1993 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1994 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1995 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
2004 * Internal version of cmd_enable_event() with a supplemental
2005 * "internal_event" flag which is used to enable internal events which should
2006 * be hidden from clients. Such events are used in the agent implementation to
2007 * enable the events through which all "agent" events are funeled.
2009 static int _cmd_enable_event(struct ltt_session
*session
,
2010 const struct lttng_domain
*domain
,
2011 char *channel_name
, struct lttng_event
*event
,
2012 char *filter_expression
,
2013 struct lttng_filter_bytecode
*filter
,
2014 struct lttng_event_exclusion
*exclusion
,
2015 int wpipe
, bool internal_event
)
2017 int ret
= 0, channel_created
= 0;
2018 struct lttng_channel
*attr
= NULL
;
2022 assert(channel_name
);
2024 /* If we have a filter, we must have its filter expression */
2025 assert(!(!!filter_expression
^ !!filter
));
2027 /* Normalize event name as a globbing pattern */
2028 strutils_normalize_star_glob_pattern(event
->name
);
2030 /* Normalize exclusion names as globbing patterns */
2034 for (i
= 0; i
< exclusion
->count
; i
++) {
2035 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
2037 strutils_normalize_star_glob_pattern(name
);
2041 DBG("Enable event command for event \'%s\'", event
->name
);
2045 switch (domain
->type
) {
2046 case LTTNG_DOMAIN_KERNEL
:
2048 struct ltt_kernel_channel
*kchan
;
2051 * If a non-default channel has been created in the
2052 * session, explicitely require that -c chan_name needs
2055 if (session
->kernel_session
->has_non_default_channel
2056 && channel_name
[0] == '\0') {
2057 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2061 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2062 session
->kernel_session
);
2063 if (kchan
== NULL
) {
2064 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2065 LTTNG_BUFFER_GLOBAL
);
2067 ret
= LTTNG_ERR_FATAL
;
2070 if (lttng_strncpy(attr
->name
, channel_name
,
2071 sizeof(attr
->name
))) {
2072 ret
= LTTNG_ERR_INVALID
;
2076 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2077 if (ret
!= LTTNG_OK
) {
2080 channel_created
= 1;
2083 /* Get the newly created kernel channel pointer */
2084 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2085 session
->kernel_session
);
2086 if (kchan
== NULL
) {
2087 /* This sould not happen... */
2088 ret
= LTTNG_ERR_FATAL
;
2092 switch (event
->type
) {
2093 case LTTNG_EVENT_ALL
:
2095 char *filter_expression_a
= NULL
;
2096 struct lttng_filter_bytecode
*filter_a
= NULL
;
2099 * We need to duplicate filter_expression and filter,
2100 * because ownership is passed to first enable
2103 if (filter_expression
) {
2104 filter_expression_a
= strdup(filter_expression
);
2105 if (!filter_expression_a
) {
2106 ret
= LTTNG_ERR_FATAL
;
2111 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2113 free(filter_expression_a
);
2114 ret
= LTTNG_ERR_FATAL
;
2117 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2119 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2120 ret
= event_kernel_enable_event(kchan
, event
,
2121 filter_expression
, filter
);
2122 /* We have passed ownership */
2123 filter_expression
= NULL
;
2125 if (ret
!= LTTNG_OK
) {
2126 if (channel_created
) {
2127 /* Let's not leak a useless channel. */
2128 kernel_destroy_channel(kchan
);
2130 free(filter_expression_a
);
2134 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2135 ret
= event_kernel_enable_event(kchan
, event
,
2136 filter_expression_a
, filter_a
);
2137 /* We have passed ownership */
2138 filter_expression_a
= NULL
;
2140 if (ret
!= LTTNG_OK
) {
2145 case LTTNG_EVENT_PROBE
:
2146 case LTTNG_EVENT_USERSPACE_PROBE
:
2147 case LTTNG_EVENT_FUNCTION
:
2148 case LTTNG_EVENT_FUNCTION_ENTRY
:
2149 case LTTNG_EVENT_TRACEPOINT
:
2150 ret
= event_kernel_enable_event(kchan
, event
,
2151 filter_expression
, filter
);
2152 /* We have passed ownership */
2153 filter_expression
= NULL
;
2155 if (ret
!= LTTNG_OK
) {
2156 if (channel_created
) {
2157 /* Let's not leak a useless channel. */
2158 kernel_destroy_channel(kchan
);
2163 case LTTNG_EVENT_SYSCALL
:
2164 ret
= event_kernel_enable_event(kchan
, event
,
2165 filter_expression
, filter
);
2166 /* We have passed ownership */
2167 filter_expression
= NULL
;
2169 if (ret
!= LTTNG_OK
) {
2174 ret
= LTTNG_ERR_UNK
;
2178 kernel_wait_quiescent();
2181 case LTTNG_DOMAIN_UST
:
2183 struct ltt_ust_channel
*uchan
;
2184 struct ltt_ust_session
*usess
= session
->ust_session
;
2189 * If a non-default channel has been created in the
2190 * session, explicitely require that -c chan_name needs
2193 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2194 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2198 /* Get channel from global UST domain */
2199 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2201 if (uchan
== NULL
) {
2202 /* Create default channel */
2203 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2204 usess
->buffer_type
);
2206 ret
= LTTNG_ERR_FATAL
;
2209 if (lttng_strncpy(attr
->name
, channel_name
,
2210 sizeof(attr
->name
))) {
2211 ret
= LTTNG_ERR_INVALID
;
2215 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2216 if (ret
!= LTTNG_OK
) {
2220 /* Get the newly created channel reference back */
2221 uchan
= trace_ust_find_channel_by_name(
2222 usess
->domain_global
.channels
, channel_name
);
2226 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2228 * Don't allow users to add UST events to channels which
2229 * are assigned to a userspace subdomain (JUL, Log4J,
2232 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2236 if (!internal_event
) {
2238 * Ensure the event name is not reserved for internal
2241 ret
= validate_ust_event_name(event
->name
);
2243 WARN("Userspace event name %s failed validation.",
2245 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2250 /* At this point, the session and channel exist on the tracer */
2251 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2252 filter_expression
, filter
, exclusion
,
2254 /* We have passed ownership */
2255 filter_expression
= NULL
;
2258 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2259 goto already_enabled
;
2260 } else if (ret
!= LTTNG_OK
) {
2265 case LTTNG_DOMAIN_LOG4J
:
2266 case LTTNG_DOMAIN_JUL
:
2267 case LTTNG_DOMAIN_PYTHON
:
2269 const char *default_event_name
, *default_chan_name
;
2271 struct lttng_event uevent
;
2272 struct lttng_domain tmp_dom
;
2273 struct ltt_ust_session
*usess
= session
->ust_session
;
2277 if (!agent_tracing_is_enabled()) {
2278 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2279 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2283 agt
= trace_ust_find_agent(usess
, domain
->type
);
2285 agt
= agent_create(domain
->type
);
2287 ret
= LTTNG_ERR_NOMEM
;
2290 agent_add(agt
, usess
->agents
);
2293 /* Create the default tracepoint. */
2294 memset(&uevent
, 0, sizeof(uevent
));
2295 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2296 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2297 default_event_name
= event_get_default_agent_ust_name(
2299 if (!default_event_name
) {
2300 ret
= LTTNG_ERR_FATAL
;
2303 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2304 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2307 * The domain type is changed because we are about to enable the
2308 * default channel and event for the JUL domain that are hardcoded.
2309 * This happens in the UST domain.
2311 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2312 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2314 switch (domain
->type
) {
2315 case LTTNG_DOMAIN_LOG4J
:
2316 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2318 case LTTNG_DOMAIN_JUL
:
2319 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2321 case LTTNG_DOMAIN_PYTHON
:
2322 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2325 /* The switch/case we are in makes this impossible */
2330 char *filter_expression_copy
= NULL
;
2331 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2334 const size_t filter_size
= sizeof(
2335 struct lttng_filter_bytecode
)
2338 filter_copy
= zmalloc(filter_size
);
2340 ret
= LTTNG_ERR_NOMEM
;
2343 memcpy(filter_copy
, filter
, filter_size
);
2345 filter_expression_copy
=
2346 strdup(filter_expression
);
2347 if (!filter_expression
) {
2348 ret
= LTTNG_ERR_NOMEM
;
2351 if (!filter_expression_copy
|| !filter_copy
) {
2352 free(filter_expression_copy
);
2358 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2359 (char *) default_chan_name
,
2360 &uevent
, filter_expression_copy
,
2361 filter_copy
, NULL
, wpipe
);
2364 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2365 goto already_enabled
;
2366 } else if (ret
!= LTTNG_OK
) {
2370 /* The wild card * means that everything should be enabled. */
2371 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2372 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2375 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2379 filter_expression
= NULL
;
2380 if (ret
!= LTTNG_OK
) {
2387 ret
= LTTNG_ERR_UND
;
2395 free(filter_expression
);
2398 channel_attr_destroy(attr
);
2404 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2405 * We own filter, exclusion, and filter_expression.
2407 int cmd_enable_event(struct ltt_session
*session
,
2408 const struct lttng_domain
*domain
,
2409 char *channel_name
, struct lttng_event
*event
,
2410 char *filter_expression
,
2411 struct lttng_filter_bytecode
*filter
,
2412 struct lttng_event_exclusion
*exclusion
,
2415 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2416 filter_expression
, filter
, exclusion
, wpipe
, false);
2420 * Enable an event which is internal to LTTng. An internal should
2421 * never be made visible to clients and are immune to checks such as
2424 static int cmd_enable_event_internal(struct ltt_session
*session
,
2425 const struct lttng_domain
*domain
,
2426 char *channel_name
, struct lttng_event
*event
,
2427 char *filter_expression
,
2428 struct lttng_filter_bytecode
*filter
,
2429 struct lttng_event_exclusion
*exclusion
,
2432 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2433 filter_expression
, filter
, exclusion
, wpipe
, true);
2437 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2439 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2440 struct lttng_event
**events
)
2443 ssize_t nb_events
= 0;
2446 case LTTNG_DOMAIN_KERNEL
:
2447 nb_events
= kernel_list_events(events
);
2448 if (nb_events
< 0) {
2449 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2453 case LTTNG_DOMAIN_UST
:
2454 nb_events
= ust_app_list_events(events
);
2455 if (nb_events
< 0) {
2456 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2460 case LTTNG_DOMAIN_LOG4J
:
2461 case LTTNG_DOMAIN_JUL
:
2462 case LTTNG_DOMAIN_PYTHON
:
2463 nb_events
= agent_list_events(events
, domain
);
2464 if (nb_events
< 0) {
2465 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2470 ret
= LTTNG_ERR_UND
;
2477 /* Return negative value to differentiate return code */
2482 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2484 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2485 struct lttng_event_field
**fields
)
2488 ssize_t nb_fields
= 0;
2491 case LTTNG_DOMAIN_UST
:
2492 nb_fields
= ust_app_list_event_fields(fields
);
2493 if (nb_fields
< 0) {
2494 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2498 case LTTNG_DOMAIN_KERNEL
:
2499 default: /* fall-through */
2500 ret
= LTTNG_ERR_UND
;
2507 /* Return negative value to differentiate return code */
2511 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2513 return syscall_table_list(events
);
2517 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2519 * Called with session lock held.
2521 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2522 enum lttng_domain_type domain
, int32_t **pids
)
2525 ssize_t nr_pids
= 0;
2528 case LTTNG_DOMAIN_KERNEL
:
2530 struct ltt_kernel_session
*ksess
;
2532 ksess
= session
->kernel_session
;
2533 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2535 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2540 case LTTNG_DOMAIN_UST
:
2542 struct ltt_ust_session
*usess
;
2544 usess
= session
->ust_session
;
2545 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2547 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2552 case LTTNG_DOMAIN_LOG4J
:
2553 case LTTNG_DOMAIN_JUL
:
2554 case LTTNG_DOMAIN_PYTHON
:
2556 ret
= LTTNG_ERR_UND
;
2563 /* Return negative value to differentiate return code */
2568 * Command LTTNG_START_TRACE processed by the client thread.
2570 * Called with session mutex held.
2572 int cmd_start_trace(struct ltt_session
*session
)
2574 enum lttng_error_code ret
;
2575 unsigned long nb_chan
= 0;
2576 struct ltt_kernel_session
*ksession
;
2577 struct ltt_ust_session
*usess
;
2578 const bool session_rotated_after_last_stop
=
2579 session
->rotated_after_last_stop
;
2583 /* Ease our life a bit ;) */
2584 ksession
= session
->kernel_session
;
2585 usess
= session
->ust_session
;
2587 /* Is the session already started? */
2588 if (session
->active
) {
2589 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2593 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
&&
2594 !session
->current_trace_chunk
) {
2596 * A rotation was launched while the session was stopped and
2597 * it has not been completed yet. It is not possible to start
2598 * the session since starting the session here would require a
2599 * rotation from "NULL" to a new trace chunk. That rotation
2600 * would overlap with the ongoing rotation, which is not
2603 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2605 ret
= LTTNG_ERR_ROTATION_PENDING
;
2610 * Starting a session without channel is useless since after that it's not
2611 * possible to enable channel thus inform the client.
2613 if (usess
&& usess
->domain_global
.channels
) {
2614 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2617 nb_chan
+= ksession
->channel_count
;
2620 ret
= LTTNG_ERR_NO_CHANNEL
;
2624 session
->active
= 1;
2625 session
->rotated_after_last_stop
= false;
2626 if (session
->output_traces
&& !session
->current_trace_chunk
) {
2627 if (!session
->has_been_started
) {
2628 struct lttng_trace_chunk
*trace_chunk
;
2630 DBG("Creating initial trace chunk of session \"%s\"",
2632 trace_chunk
= session_create_new_trace_chunk(
2633 session
, NULL
, NULL
, NULL
);
2635 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2638 assert(!session
->current_trace_chunk
);
2639 ret
= session_set_trace_chunk(session
, trace_chunk
,
2641 lttng_trace_chunk_put(trace_chunk
);
2643 ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
2647 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2650 * Rotate existing streams into the new chunk.
2651 * This is a "quiet" rotation has no client has
2652 * explicitly requested this operation.
2654 * There is also no need to wait for the rotation
2655 * to complete as it will happen immediately. No data
2656 * was produced as the session was stopped, so the
2657 * rotation should happen on reception of the command.
2659 ret
= cmd_rotate_session(session
, NULL
, true);
2660 if (ret
!= LTTNG_OK
) {
2666 /* Kernel tracing */
2667 if (ksession
!= NULL
) {
2668 DBG("Start kernel tracing session %s", session
->name
);
2669 ret
= start_kernel_session(ksession
);
2670 if (ret
!= LTTNG_OK
) {
2675 /* Flag session that trace should start automatically */
2677 int int_ret
= ust_app_start_trace_all(usess
);
2680 ret
= LTTNG_ERR_UST_START_FAIL
;
2686 * Clear the flag that indicates that a rotation was done while the
2687 * session was stopped.
2689 session
->rotated_after_last_stop
= false;
2691 if (session
->rotate_timer_period
) {
2692 int int_ret
= timer_session_rotation_schedule_timer_start(
2693 session
, session
->rotate_timer_period
);
2696 ERR("Failed to enable rotate timer");
2697 ret
= LTTNG_ERR_UNK
;
2705 if (ret
== LTTNG_OK
) {
2706 /* Flag this after a successful start. */
2707 session
->has_been_started
|= 1;
2709 session
->active
= 0;
2710 /* Restore initial state on error. */
2711 session
->rotated_after_last_stop
=
2712 session_rotated_after_last_stop
;
2718 * Command LTTNG_STOP_TRACE processed by the client thread.
2720 int cmd_stop_trace(struct ltt_session
*session
)
2723 struct ltt_kernel_channel
*kchan
;
2724 struct ltt_kernel_session
*ksession
;
2725 struct ltt_ust_session
*usess
;
2726 bool error_occurred
= false;
2730 DBG("Begin stop session %s (id %" PRIu64
")", session
->name
, session
->id
);
2732 ksession
= session
->kernel_session
;
2733 usess
= session
->ust_session
;
2735 /* Session is not active. Skip everythong and inform the client. */
2736 if (!session
->active
) {
2737 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2742 if (ksession
&& ksession
->active
) {
2743 DBG("Stop kernel tracing");
2745 ret
= kernel_stop_session(ksession
);
2747 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2751 kernel_wait_quiescent();
2753 /* Flush metadata after stopping (if exists) */
2754 if (ksession
->metadata_stream_fd
>= 0) {
2755 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2757 ERR("Kernel metadata flush failed");
2758 error_occurred
= true;
2762 /* Flush all buffers after stopping */
2763 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2764 ret
= kernel_flush_buffer(kchan
);
2766 ERR("Kernel flush buffer error");
2767 error_occurred
= true;
2771 ksession
->active
= 0;
2772 DBG("Kernel session stopped %s (id %" PRIu64
")", session
->name
,
2776 if (usess
&& usess
->active
) {
2777 ret
= ust_app_stop_trace_all(usess
);
2779 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2784 /* Flag inactive after a successful stop. */
2785 session
->active
= 0;
2786 ret
= !error_occurred
? LTTNG_OK
: LTTNG_ERR_UNK
;
2793 * Set the base_path of the session only if subdir of a control uris is set.
2794 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2796 static int set_session_base_path_from_uris(struct ltt_session
*session
,
2798 struct lttng_uri
*uris
)
2803 for (i
= 0; i
< nb_uri
; i
++) {
2804 if (uris
[i
].stype
!= LTTNG_STREAM_CONTROL
||
2805 uris
[i
].subdir
[0] == '\0') {
2806 /* Not interested in these URIs */
2810 if (session
->base_path
!= NULL
) {
2811 free(session
->base_path
);
2812 session
->base_path
= NULL
;
2815 /* Set session base_path */
2816 session
->base_path
= strdup(uris
[i
].subdir
);
2817 if (!session
->base_path
) {
2818 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2819 uris
[i
].subdir
, session
->name
);
2820 ret
= LTTNG_ERR_NOMEM
;
2823 DBG2("Setting base path \"%s\" for session \"%s\"",
2824 session
->base_path
, session
->name
);
2832 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2834 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2835 struct lttng_uri
*uris
)
2838 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2839 struct ltt_ust_session
*usess
= session
->ust_session
;
2845 /* Can't set consumer URI if the session is active. */
2846 if (session
->active
) {
2847 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2852 * Set the session base path if any. This is done inside
2853 * cmd_set_consumer_uri to preserve backward compatibility of the
2854 * previous session creation api vs the session descriptor api.
2856 ret
= set_session_base_path_from_uris(session
, nb_uri
, uris
);
2857 if (ret
!= LTTNG_OK
) {
2861 /* Set the "global" consumer URIs */
2862 for (i
= 0; i
< nb_uri
; i
++) {
2863 ret
= add_uri_to_consumer(session
, session
->consumer
, &uris
[i
],
2865 if (ret
!= LTTNG_OK
) {
2870 /* Set UST session URIs */
2871 if (session
->ust_session
) {
2872 for (i
= 0; i
< nb_uri
; i
++) {
2873 ret
= add_uri_to_consumer(session
,
2874 session
->ust_session
->consumer
,
2875 &uris
[i
], LTTNG_DOMAIN_UST
);
2876 if (ret
!= LTTNG_OK
) {
2882 /* Set kernel session URIs */
2883 if (session
->kernel_session
) {
2884 for (i
= 0; i
< nb_uri
; i
++) {
2885 ret
= add_uri_to_consumer(session
,
2886 session
->kernel_session
->consumer
,
2887 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2888 if (ret
!= LTTNG_OK
) {
2895 * Make sure to set the session in output mode after we set URI since a
2896 * session can be created without URL (thus flagged in no output mode).
2898 session
->output_traces
= 1;
2900 ksess
->output_traces
= 1;
2904 usess
->output_traces
= 1;
2915 enum lttng_error_code
set_session_output_from_descriptor(
2916 struct ltt_session
*session
,
2917 const struct lttng_session_descriptor
*descriptor
)
2920 enum lttng_error_code ret_code
= LTTNG_OK
;
2921 enum lttng_session_descriptor_type session_type
=
2922 lttng_session_descriptor_get_type(descriptor
);
2923 enum lttng_session_descriptor_output_type output_type
=
2924 lttng_session_descriptor_get_output_type(descriptor
);
2925 struct lttng_uri uris
[2] = {};
2926 size_t uri_count
= 0;
2928 switch (output_type
) {
2929 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2931 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2932 lttng_session_descriptor_get_local_output_uri(descriptor
,
2936 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2937 lttng_session_descriptor_get_network_output_uris(descriptor
,
2938 &uris
[0], &uris
[1]);
2942 ret_code
= LTTNG_ERR_INVALID
;
2946 switch (session_type
) {
2947 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2949 struct snapshot_output
*new_output
= NULL
;
2951 new_output
= snapshot_output_alloc();
2953 ret_code
= LTTNG_ERR_NOMEM
;
2957 ret
= snapshot_output_init_with_uri(session
,
2958 DEFAULT_SNAPSHOT_MAX_SIZE
,
2959 NULL
, uris
, uri_count
, session
->consumer
,
2960 new_output
, &session
->snapshot
);
2962 ret_code
= (ret
== -ENOMEM
) ?
2963 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2964 snapshot_output_destroy(new_output
);
2967 snapshot_add_output(&session
->snapshot
, new_output
);
2970 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2971 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2973 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2977 ret_code
= LTTNG_ERR_INVALID
;
2985 enum lttng_error_code
cmd_create_session_from_descriptor(
2986 struct lttng_session_descriptor
*descriptor
,
2987 const lttng_sock_cred
*creds
,
2988 const char *home_path
)
2991 enum lttng_error_code ret_code
;
2992 const char *session_name
;
2993 struct ltt_session
*new_session
= NULL
;
2994 enum lttng_session_descriptor_status descriptor_status
;
2996 session_lock_list();
2998 if (*home_path
!= '/') {
2999 ERR("Home path provided by client is not absolute");
3000 ret_code
= LTTNG_ERR_INVALID
;
3005 descriptor_status
= lttng_session_descriptor_get_session_name(
3006 descriptor
, &session_name
);
3007 switch (descriptor_status
) {
3008 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
3010 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
3011 session_name
= NULL
;
3014 ret_code
= LTTNG_ERR_INVALID
;
3018 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
3020 if (ret_code
!= LTTNG_OK
) {
3024 if (!session_name
) {
3025 ret
= lttng_session_descriptor_set_session_name(descriptor
,
3028 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3033 if (!lttng_session_descriptor_is_output_destination_initialized(
3036 * Only include the session's creation time in the output
3037 * destination if the name of the session itself was
3038 * not auto-generated.
3040 ret_code
= lttng_session_descriptor_set_default_output(
3042 session_name
? &new_session
->creation_time
: NULL
,
3044 if (ret_code
!= LTTNG_OK
) {
3048 new_session
->has_user_specified_directory
=
3049 lttng_session_descriptor_has_output_directory(
3053 switch (lttng_session_descriptor_get_type(descriptor
)) {
3054 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3055 new_session
->snapshot_mode
= 1;
3057 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3058 new_session
->live_timer
=
3059 lttng_session_descriptor_live_get_timer_interval(
3066 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3067 if (ret_code
!= LTTNG_OK
) {
3070 new_session
->consumer
->enabled
= 1;
3071 ret_code
= LTTNG_OK
;
3073 /* Release reference provided by the session_create function. */
3074 session_put(new_session
);
3075 if (ret_code
!= LTTNG_OK
&& new_session
) {
3076 /* Release the global reference on error. */
3077 session_destroy(new_session
);
3079 session_unlock_list();
3083 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3084 struct lttng_session_descriptor
**return_descriptor
)
3087 size_t payload_size
;
3088 struct lttng_dynamic_buffer payload
;
3089 struct lttng_buffer_view home_dir_view
;
3090 struct lttng_buffer_view session_descriptor_view
;
3091 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3092 enum lttng_error_code ret_code
;
3094 lttng_dynamic_buffer_init(&payload
);
3095 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
3097 ret_code
= LTTNG_ERR_INVALID
;
3100 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
3101 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3102 ret_code
= LTTNG_ERR_INVALID
;
3106 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
3107 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
3108 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3110 ret_code
= LTTNG_ERR_NOMEM
;
3114 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3116 ERR("Reception of session descriptor failed, aborting.");
3117 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3121 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3124 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3125 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3127 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3128 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3130 ret
= lttng_session_descriptor_create_from_buffer(
3131 &session_descriptor_view
, &session_descriptor
);
3133 ERR("Failed to create session descriptor from payload of \"create session\" command");
3134 ret_code
= LTTNG_ERR_INVALID
;
3139 * Sets the descriptor's auto-generated properties (name, output) if
3142 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3144 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3145 if (ret_code
!= LTTNG_OK
) {
3149 ret_code
= LTTNG_OK
;
3150 *return_descriptor
= session_descriptor
;
3151 session_descriptor
= NULL
;
3153 lttng_dynamic_buffer_reset(&payload
);
3154 lttng_session_descriptor_destroy(session_descriptor
);
3159 void cmd_destroy_session_reply(const struct ltt_session
*session
,
3160 void *_reply_context
)
3164 const struct cmd_destroy_session_reply_context
*reply_context
=
3166 struct lttng_dynamic_buffer payload
;
3167 struct lttcomm_session_destroy_command_header cmd_header
;
3168 struct lttng_trace_archive_location
*location
= NULL
;
3169 struct lttcomm_lttng_msg llm
= {
3170 .cmd_type
= LTTNG_DESTROY_SESSION
,
3171 .ret_code
= reply_context
->destruction_status
,
3174 sizeof(struct lttcomm_session_destroy_command_header
),
3177 size_t payload_size_before_location
;
3179 lttng_dynamic_buffer_init(&payload
);
3181 ret
= lttng_dynamic_buffer_append(&payload
, &llm
, sizeof(llm
));
3183 ERR("Failed to append session destruction message");
3187 cmd_header
.rotation_state
=
3188 (int32_t) (reply_context
->implicit_rotation_on_destroy
?
3189 session
->rotation_state
:
3190 LTTNG_ROTATION_STATE_NO_ROTATION
);
3191 ret
= lttng_dynamic_buffer_append(&payload
, &cmd_header
,
3192 sizeof(cmd_header
));
3194 ERR("Failed to append session destruction command header");
3198 if (!reply_context
->implicit_rotation_on_destroy
) {
3199 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3203 if (session
->rotation_state
!= LTTNG_ROTATION_STATE_COMPLETED
) {
3204 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3209 location
= session_get_trace_archive_location(session
);
3211 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3216 payload_size_before_location
= payload
.size
;
3217 comm_ret
= lttng_trace_archive_location_serialize(location
,
3220 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3224 /* Update the message to indicate the location's length. */
3225 ((struct lttcomm_lttng_msg
*) payload
.data
)->data_size
=
3226 payload
.size
- payload_size_before_location
;
3228 comm_ret
= lttcomm_send_unix_sock(reply_context
->reply_sock_fd
,
3229 payload
.data
, payload
.size
);
3230 if (comm_ret
!= (ssize_t
) payload
.size
) {
3231 ERR("Failed to send result of the destruction of session \"%s\" to client",
3235 ret
= close(reply_context
->reply_sock_fd
);
3237 PERROR("Failed to close client socket in deferred session destroy reply");
3239 lttng_dynamic_buffer_reset(&payload
);
3240 free(_reply_context
);
3244 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3246 * Called with session lock held.
3248 int cmd_destroy_session(struct ltt_session
*session
,
3249 struct notification_thread_handle
*notification_thread_handle
,
3253 enum lttng_error_code destruction_last_error
= LTTNG_OK
;
3254 struct cmd_destroy_session_reply_context
*reply_context
= NULL
;
3257 reply_context
= zmalloc(sizeof(*reply_context
));
3258 if (!reply_context
) {
3259 ret
= LTTNG_ERR_NOMEM
;
3262 reply_context
->reply_sock_fd
= *sock_fd
;
3268 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
,
3270 if (session
->active
) {
3271 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3273 ret
= cmd_stop_trace(session
);
3274 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_TRACE_ALREADY_STOPPED
) {
3275 /* Carry on with the destruction of the session. */
3276 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3277 session
->name
, lttng_strerror(-ret
));
3278 destruction_last_error
= ret
;
3282 if (session
->rotation_schedule_timer_enabled
) {
3283 if (timer_session_rotation_schedule_timer_stop(
3285 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3287 destruction_last_error
= LTTNG_ERR_TIMER_STOP_ERROR
;
3291 if (session
->rotate_size
) {
3292 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3293 session
->rotate_size
= 0;
3296 if (session
->most_recent_chunk_id
.is_set
&&
3297 session
->most_recent_chunk_id
.value
!= 0 &&
3298 session
->current_trace_chunk
&& session
->output_traces
) {
3300 * Perform a last rotation on destruction if rotations have
3301 * occurred during the session's lifetime.
3303 ret
= cmd_rotate_session(session
, NULL
, false);
3304 if (ret
!= LTTNG_OK
) {
3305 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3306 session
->name
, lttng_strerror(-ret
));
3307 destruction_last_error
= -ret
;
3309 if (reply_context
) {
3310 reply_context
->implicit_rotation_on_destroy
= true;
3312 } else if (session
->has_been_started
&& session
->current_trace_chunk
) {
3314 * The user has not triggered a session rotation. However, to
3315 * ensure all data has been consumed, the session is rotated
3316 * to a 'null' trace chunk before it is destroyed.
3318 * This is a "quiet" rotation meaning that no notification is
3319 * emitted and no renaming of the current trace chunk takes
3322 ret
= cmd_rotate_session(session
, NULL
, true);
3323 if (ret
!= LTTNG_OK
) {
3324 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3325 session
->name
, lttng_strerror(-ret
));
3326 destruction_last_error
= -ret
;
3330 if (session
->shm_path
[0]) {
3332 * When a session is created with an explicit shm_path,
3333 * the consumer daemon will create its shared memory files
3334 * at that location and will *not* unlink them. This is normal
3335 * as the intention of that feature is to make it possible
3336 * to retrieve the content of those files should a crash occur.
3338 * To ensure the content of those files can be used, the
3339 * sessiond daemon will replicate the content of the metadata
3340 * cache in a metadata file.
3342 * On clean-up, it is expected that the consumer daemon will
3343 * unlink the shared memory files and that the session daemon
3344 * will unlink the metadata file. Then, the session's directory
3345 * in the shm path can be removed.
3347 * Unfortunately, a flaw in the design of the sessiond's and
3348 * consumerd's tear down of channels makes it impossible to
3349 * determine when the sessiond _and_ the consumerd have both
3350 * destroyed their representation of a channel. For one, the
3351 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3352 * callbacks in both daemons.
3354 * However, it is also impossible for the sessiond to know when
3355 * the consumer daemon is done destroying its channel(s) since
3356 * it occurs as a reaction to the closing of the channel's file
3357 * descriptor. There is no resulting communication initiated
3358 * from the consumerd to the sessiond to confirm that the
3359 * operation is completed (and was successful).
3361 * Until this is all fixed, the session daemon checks for the
3362 * removal of the session's shm path which makes it possible
3363 * to safely advertise a session as having been destroyed.
3365 * Prior to this fix, it was not possible to reliably save
3366 * a session making use of the --shm-path option, destroy it,
3367 * and load it again. This is because the creation of the
3368 * session would fail upon seeing the session's shm path
3369 * already in existence.
3371 * Note that none of the error paths in the check for the
3372 * directory's existence return an error. This is normal
3373 * as there isn't much that can be done. The session will
3374 * be destroyed properly, except that we can't offer the
3375 * guarantee that the same session can be re-created.
3377 current_completion_handler
= &destroy_completion_handler
.handler
;
3378 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3380 sizeof(destroy_completion_handler
.shm_path
));
3385 * The session is destroyed. However, note that the command context
3386 * still holds a reference to the session, thus delaying its destruction
3387 * _at least_ up to the point when that reference is released.
3389 session_destroy(session
);
3390 if (reply_context
) {
3391 reply_context
->destruction_status
= destruction_last_error
;
3392 ret
= session_add_destroy_notifier(session
,
3393 cmd_destroy_session_reply
,
3394 (void *) reply_context
);
3396 ret
= LTTNG_ERR_FATAL
;
3408 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3410 int cmd_register_consumer(struct ltt_session
*session
,
3411 enum lttng_domain_type domain
, const char *sock_path
,
3412 struct consumer_data
*cdata
)
3415 struct consumer_socket
*socket
= NULL
;
3422 case LTTNG_DOMAIN_KERNEL
:
3424 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3428 /* Can't register a consumer if there is already one */
3429 if (ksess
->consumer_fds_sent
!= 0) {
3430 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3434 sock
= lttcomm_connect_unix_sock(sock_path
);
3436 ret
= LTTNG_ERR_CONNECT_FAIL
;
3439 cdata
->cmd_sock
= sock
;
3441 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3442 if (socket
== NULL
) {
3445 PERROR("close register consumer");
3447 cdata
->cmd_sock
= -1;
3448 ret
= LTTNG_ERR_FATAL
;
3452 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3453 if (socket
->lock
== NULL
) {
3454 PERROR("zmalloc pthread mutex");
3455 ret
= LTTNG_ERR_FATAL
;
3458 pthread_mutex_init(socket
->lock
, NULL
);
3459 socket
->registered
= 1;
3462 consumer_add_socket(socket
, ksess
->consumer
);
3465 pthread_mutex_lock(&cdata
->pid_mutex
);
3467 pthread_mutex_unlock(&cdata
->pid_mutex
);
3472 /* TODO: Userspace tracing */
3473 ret
= LTTNG_ERR_UND
;
3481 consumer_destroy_socket(socket
);
3487 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3489 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3490 struct lttng_domain
**domains
)
3495 struct lttng_ht_iter iter
;
3497 if (session
->kernel_session
!= NULL
) {
3498 DBG3("Listing domains found kernel domain");
3502 if (session
->ust_session
!= NULL
) {
3503 DBG3("Listing domains found UST global domain");
3507 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3509 if (agt
->being_used
) {
3520 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3521 if (*domains
== NULL
) {
3522 ret
= LTTNG_ERR_FATAL
;
3526 if (session
->kernel_session
!= NULL
) {
3527 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3529 /* Kernel session buffer type is always GLOBAL */
3530 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3535 if (session
->ust_session
!= NULL
) {
3536 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3537 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3541 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3543 if (agt
->being_used
) {
3544 (*domains
)[index
].type
= agt
->domain
;
3545 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3555 /* Return negative value to differentiate return code */
3561 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3563 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3564 struct ltt_session
*session
, struct lttng_channel
**channels
)
3566 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3569 case LTTNG_DOMAIN_KERNEL
:
3570 if (session
->kernel_session
!= NULL
) {
3571 nb_chan
= session
->kernel_session
->channel_count
;
3573 DBG3("Number of kernel channels %zd", nb_chan
);
3575 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3579 case LTTNG_DOMAIN_UST
:
3580 if (session
->ust_session
!= NULL
) {
3582 nb_chan
= lttng_ht_get_count(
3583 session
->ust_session
->domain_global
.channels
);
3586 DBG3("Number of UST global channels %zd", nb_chan
);
3588 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3593 ret
= -LTTNG_ERR_UND
;
3598 const size_t channel_size
= sizeof(struct lttng_channel
) +
3599 sizeof(struct lttng_channel_extended
);
3600 struct lttng_channel_extended
*channel_exts
;
3602 payload_size
= nb_chan
* channel_size
;
3603 *channels
= zmalloc(payload_size
);
3604 if (*channels
== NULL
) {
3605 ret
= -LTTNG_ERR_FATAL
;
3609 channel_exts
= ((void *) *channels
) +
3610 (nb_chan
* sizeof(struct lttng_channel
));
3611 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3612 if (ret
!= LTTNG_OK
) {
3627 * Command LTTNG_LIST_EVENTS processed by the client thread.
3629 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3630 struct ltt_session
*session
, char *channel_name
,
3631 struct lttng_event
**events
, size_t *total_size
)
3634 ssize_t nb_event
= 0;
3637 case LTTNG_DOMAIN_KERNEL
:
3638 if (session
->kernel_session
!= NULL
) {
3639 nb_event
= list_lttng_kernel_events(channel_name
,
3640 session
->kernel_session
, events
,
3644 case LTTNG_DOMAIN_UST
:
3646 if (session
->ust_session
!= NULL
) {
3647 nb_event
= list_lttng_ust_global_events(channel_name
,
3648 &session
->ust_session
->domain_global
, events
,
3653 case LTTNG_DOMAIN_LOG4J
:
3654 case LTTNG_DOMAIN_JUL
:
3655 case LTTNG_DOMAIN_PYTHON
:
3656 if (session
->ust_session
) {
3657 struct lttng_ht_iter iter
;
3661 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3662 &iter
.iter
, agt
, node
.node
) {
3663 if (agt
->domain
== domain
) {
3664 nb_event
= list_lttng_agent_events(
3674 ret
= LTTNG_ERR_UND
;
3681 /* Return negative value to differentiate return code */
3686 * Using the session list, filled a lttng_session array to send back to the
3687 * client for session listing.
3689 * The session list lock MUST be acquired before calling this function. Use
3690 * session_lock_list() and session_unlock_list().
3692 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3693 size_t session_count
, uid_t uid
, gid_t gid
)
3697 struct ltt_session
*session
;
3698 struct ltt_session_list
*list
= session_get_list();
3699 struct lttng_session_extended
*extended
=
3700 (typeof(extended
)) (&sessions
[session_count
]);
3702 DBG("Getting all available session for UID %d GID %d",
3705 * Iterate over session list and append data after the control struct in
3708 cds_list_for_each_entry(session
, &list
->head
, list
) {
3709 if (!session_get(session
)) {
3713 * Only list the sessions the user can control.
3715 if (!session_access_ok(session
, uid
, gid
) ||
3716 session
->destroyed
) {
3717 session_put(session
);
3721 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3722 struct ltt_ust_session
*usess
= session
->ust_session
;
3724 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3725 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3726 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3727 ret
= build_network_session_path(sessions
[i
].path
,
3728 sizeof(sessions
[i
].path
), session
);
3730 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3731 session
->consumer
->dst
.session_root_path
);
3734 PERROR("snprintf session path");
3735 session_put(session
);
3739 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3740 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3741 sessions
[i
].enabled
= session
->active
;
3742 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3743 sessions
[i
].live_timer_interval
= session
->live_timer
;
3744 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3745 extended
[i
].creation_time
.is_set
= 1;
3747 session_put(session
);
3752 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3753 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3755 int cmd_data_pending(struct ltt_session
*session
)
3758 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3759 struct ltt_ust_session
*usess
= session
->ust_session
;
3763 DBG("Data pending for session %s", session
->name
);
3765 /* Session MUST be stopped to ask for data availability. */
3766 if (session
->active
) {
3767 ret
= LTTNG_ERR_SESSION_STARTED
;
3771 * If stopped, just make sure we've started before else the above call
3772 * will always send that there is data pending.
3774 * The consumer assumes that when the data pending command is received,
3775 * the trace has been started before or else no output data is written
3776 * by the streams which is a condition for data pending. So, this is
3777 * *VERY* important that we don't ask the consumer before a start
3780 if (!session
->has_been_started
) {
3786 /* A rotation is still pending, we have to wait. */
3787 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3788 DBG("Rotate still pending for session %s", session
->name
);
3793 if (ksess
&& ksess
->consumer
) {
3794 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3796 /* Data is still being extracted for the kernel. */
3801 if (usess
&& usess
->consumer
) {
3802 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3804 /* Data is still being extracted for the kernel. */
3809 /* Data is ready to be read by a viewer */
3817 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3819 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3821 int cmd_snapshot_add_output(struct ltt_session
*session
,
3822 const struct lttng_snapshot_output
*output
, uint32_t *id
)
3825 struct snapshot_output
*new_output
;
3830 DBG("Cmd snapshot add output for session %s", session
->name
);
3833 * Can't create an output if the session is not set in no-output mode.
3835 if (session
->output_traces
) {
3836 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3840 if (session
->has_non_mmap_channel
) {
3841 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3845 /* Only one output is allowed until we have the "tee" feature. */
3846 if (session
->snapshot
.nb_output
== 1) {
3847 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3851 new_output
= snapshot_output_alloc();
3853 ret
= LTTNG_ERR_NOMEM
;
3857 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3858 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3859 &session
->snapshot
);
3861 if (ret
== -ENOMEM
) {
3862 ret
= LTTNG_ERR_NOMEM
;
3864 ret
= LTTNG_ERR_INVALID
;
3870 snapshot_add_output(&session
->snapshot
, new_output
);
3872 *id
= new_output
->id
;
3879 snapshot_output_destroy(new_output
);
3885 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3887 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3889 int cmd_snapshot_del_output(struct ltt_session
*session
,
3890 const struct lttng_snapshot_output
*output
)
3893 struct snapshot_output
*sout
= NULL
;
3901 * Permission denied to create an output if the session is not
3902 * set in no output mode.
3904 if (session
->output_traces
) {
3905 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3910 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3912 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3913 } else if (*output
->name
!= '\0') {
3914 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3916 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3919 ret
= LTTNG_ERR_INVALID
;
3923 snapshot_delete_output(&session
->snapshot
, sout
);
3924 snapshot_output_destroy(sout
);
3933 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3935 * If no output is available, outputs is untouched and 0 is returned.
3937 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3939 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3940 struct lttng_snapshot_output
**outputs
)
3943 struct lttng_snapshot_output
*list
= NULL
;
3944 struct lttng_ht_iter iter
;
3945 struct snapshot_output
*output
;
3950 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3953 * Permission denied to create an output if the session is not
3954 * set in no output mode.
3956 if (session
->output_traces
) {
3957 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3961 if (session
->snapshot
.nb_output
== 0) {
3966 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3968 ret
= -LTTNG_ERR_NOMEM
;
3972 /* Copy list from session to the new list object. */
3974 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3975 output
, node
.node
) {
3976 assert(output
->consumer
);
3977 list
[idx
].id
= output
->id
;
3978 list
[idx
].max_size
= output
->max_size
;
3979 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3980 sizeof(list
[idx
].name
))) {
3981 ret
= -LTTNG_ERR_INVALID
;
3984 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3985 if (lttng_strncpy(list
[idx
].ctrl_url
,
3986 output
->consumer
->dst
.session_root_path
,
3987 sizeof(list
[idx
].ctrl_url
))) {
3988 ret
= -LTTNG_ERR_INVALID
;
3993 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3994 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3996 ret
= -LTTNG_ERR_NOMEM
;
4001 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
4002 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
4004 ret
= -LTTNG_ERR_NOMEM
;
4013 ret
= session
->snapshot
.nb_output
;
4022 * Check if we can regenerate the metadata for this session.
4023 * Only kernel, UST per-uid and non-live sessions are supported.
4025 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4028 int check_regenerate_metadata_support(struct ltt_session
*session
)
4034 if (session
->live_timer
!= 0) {
4035 ret
= LTTNG_ERR_LIVE_SESSION
;
4038 if (!session
->active
) {
4039 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4042 if (session
->ust_session
) {
4043 switch (session
->ust_session
->buffer_type
) {
4044 case LTTNG_BUFFER_PER_UID
:
4046 case LTTNG_BUFFER_PER_PID
:
4047 ret
= LTTNG_ERR_PER_PID_SESSION
;
4051 ret
= LTTNG_ERR_UNK
;
4055 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4056 session
->consumer
->relay_minor_version
< 8) {
4057 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
4067 int clear_metadata_file(int fd
)
4072 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
4073 if (lseek_ret
< 0) {
4079 ret
= ftruncate(fd
, 0);
4081 PERROR("ftruncate");
4090 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
4093 struct buffer_reg_uid
*uid_reg
= NULL
;
4094 struct buffer_reg_session
*session_reg
= NULL
;
4097 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
4098 struct ust_registry_session
*registry
;
4099 struct ust_registry_channel
*chan
;
4100 struct lttng_ht_iter iter_chan
;
4102 session_reg
= uid_reg
->registry
;
4103 registry
= session_reg
->reg
.ust
;
4105 pthread_mutex_lock(®istry
->lock
);
4106 registry
->metadata_len_sent
= 0;
4107 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
4108 registry
->metadata_len
= 0;
4109 registry
->metadata_version
++;
4110 if (registry
->metadata_fd
> 0) {
4111 /* Clear the metadata file's content. */
4112 ret
= clear_metadata_file(registry
->metadata_fd
);
4114 pthread_mutex_unlock(®istry
->lock
);
4119 ret
= ust_metadata_session_statedump(registry
, NULL
,
4120 registry
->major
, registry
->minor
);
4122 pthread_mutex_unlock(®istry
->lock
);
4123 ERR("Failed to generate session metadata (err = %d)",
4127 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
4129 struct ust_registry_event
*event
;
4130 struct lttng_ht_iter iter_event
;
4132 ret
= ust_metadata_channel_statedump(registry
, chan
);
4134 pthread_mutex_unlock(®istry
->lock
);
4135 ERR("Failed to generate channel metadata "
4139 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
4141 ret
= ust_metadata_event_statedump(registry
,
4144 pthread_mutex_unlock(®istry
->lock
);
4145 ERR("Failed to generate event metadata "
4151 pthread_mutex_unlock(®istry
->lock
);
4160 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4162 * Ask the consumer to truncate the existing metadata file(s) and
4163 * then regenerate the metadata. Live and per-pid sessions are not
4164 * supported and return an error.
4166 * Return 0 on success or else a LTTNG_ERR code.
4168 int cmd_regenerate_metadata(struct ltt_session
*session
)
4174 ret
= check_regenerate_metadata_support(session
);
4179 if (session
->kernel_session
) {
4180 ret
= kernctl_session_regenerate_metadata(
4181 session
->kernel_session
->fd
);
4183 ERR("Failed to regenerate the kernel metadata");
4188 if (session
->ust_session
) {
4189 ret
= ust_regenerate_metadata(session
->ust_session
);
4191 ERR("Failed to regenerate the UST metadata");
4195 DBG("Cmd metadata regenerate for session %s", session
->name
);
4203 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4205 * Ask the tracer to regenerate a new statedump.
4207 * Return 0 on success or else a LTTNG_ERR code.
4209 int cmd_regenerate_statedump(struct ltt_session
*session
)
4215 if (!session
->active
) {
4216 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4220 if (session
->kernel_session
) {
4221 ret
= kernctl_session_regenerate_statedump(
4222 session
->kernel_session
->fd
);
4224 * Currently, the statedump in kernel can only fail if out
4228 if (ret
== -ENOMEM
) {
4229 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4231 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4233 ERR("Failed to regenerate the kernel statedump");
4238 if (session
->ust_session
) {
4239 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4241 * Currently, the statedump in UST always returns 0.
4244 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4245 ERR("Failed to regenerate the UST statedump");
4249 DBG("Cmd regenerate statedump for session %s", session
->name
);
4256 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4257 struct notification_thread_handle
*notification_thread
)
4261 ssize_t sock_recv_len
;
4262 struct lttng_trigger
*trigger
= NULL
;
4263 struct lttng_buffer_view view
;
4264 struct lttng_dynamic_buffer trigger_buffer
;
4266 lttng_dynamic_buffer_init(&trigger_buffer
);
4267 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4268 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4270 ret
= LTTNG_ERR_NOMEM
;
4274 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4276 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4277 ERR("Failed to receive \"register trigger\" command payload");
4278 /* TODO: should this be a new error enum ? */
4279 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4283 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4284 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4286 ERR("Invalid trigger payload received in \"register trigger\" command");
4287 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4291 ret
= notification_thread_command_register_trigger(notification_thread
,
4293 /* Ownership of trigger was transferred. */
4296 lttng_trigger_destroy(trigger
);
4297 lttng_dynamic_buffer_reset(&trigger_buffer
);
4301 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4302 struct notification_thread_handle
*notification_thread
)
4306 ssize_t sock_recv_len
;
4307 struct lttng_trigger
*trigger
= NULL
;
4308 struct lttng_buffer_view view
;
4309 struct lttng_dynamic_buffer trigger_buffer
;
4311 lttng_dynamic_buffer_init(&trigger_buffer
);
4312 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4313 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4315 ret
= LTTNG_ERR_NOMEM
;
4319 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4321 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4322 ERR("Failed to receive \"unregister trigger\" command payload");
4323 /* TODO: should this be a new error enum ? */
4324 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4328 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4329 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4331 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4332 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4336 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4339 lttng_trigger_destroy(trigger
);
4340 lttng_dynamic_buffer_reset(&trigger_buffer
);
4345 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4346 * snapshot output is *not* set with a remote destination.
4348 * Return LTTNG_OK on success or a LTTNG_ERR code.
4350 static enum lttng_error_code
set_relayd_for_snapshot(
4351 struct consumer_output
*output
,
4352 const struct ltt_session
*session
)
4354 enum lttng_error_code status
= LTTNG_OK
;
4355 struct lttng_ht_iter iter
;
4356 struct consumer_socket
*socket
;
4357 LTTNG_OPTIONAL(uint64_t) current_chunk_id
= {};
4358 const char *base_path
;
4363 DBG2("Set relayd object from snapshot output");
4365 if (session
->current_trace_chunk
) {
4366 enum lttng_trace_chunk_status chunk_status
=
4367 lttng_trace_chunk_get_id(
4368 session
->current_trace_chunk
,
4369 ¤t_chunk_id
.value
);
4371 if (chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
) {
4372 current_chunk_id
.is_set
= true;
4374 ERR("Failed to get current trace chunk id");
4375 status
= LTTNG_ERR_UNK
;
4380 /* Ignore if snapshot consumer output is not network. */
4381 if (output
->type
!= CONSUMER_DST_NET
) {
4386 * The snapshot record URI base path overrides the session
4389 if (output
->dst
.net
.control
.subdir
[0] != '\0') {
4390 base_path
= output
->dst
.net
.control
.subdir
;
4392 base_path
= session
->base_path
;
4396 * For each consumer socket, create and send the relayd object of the
4400 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
4401 socket
, node
.node
) {
4402 pthread_mutex_lock(socket
->lock
);
4403 status
= send_consumer_relayd_sockets(0, session
->id
,
4405 session
->name
, session
->hostname
,
4407 session
->live_timer
,
4408 current_chunk_id
.is_set
? ¤t_chunk_id
.value
: NULL
,
4409 session
->creation_time
,
4410 session
->name_contains_creation_time
);
4411 pthread_mutex_unlock(socket
->lock
);
4412 if (status
!= LTTNG_OK
) {
4424 * Record a kernel snapshot.
4426 * Return LTTNG_OK on success or a LTTNG_ERR code.
4428 static enum lttng_error_code
record_kernel_snapshot(
4429 struct ltt_kernel_session
*ksess
,
4430 const struct consumer_output
*output
,
4431 const struct ltt_session
*session
,
4432 int wait
, uint64_t nb_packets_per_stream
)
4434 enum lttng_error_code status
;
4440 status
= kernel_snapshot_record(
4441 ksess
, output
, wait
, nb_packets_per_stream
);
4446 * Record a UST snapshot.
4448 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4450 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4451 const struct consumer_output
*output
,
4452 const struct ltt_session
*session
,
4453 int wait
, uint64_t nb_packets_per_stream
)
4455 enum lttng_error_code status
;
4461 status
= ust_app_snapshot_record(
4462 usess
, output
, wait
, nb_packets_per_stream
);
4467 uint64_t get_session_size_one_more_packet_per_stream(
4468 const struct ltt_session
*session
, uint64_t cur_nr_packets
)
4470 uint64_t tot_size
= 0;
4472 if (session
->kernel_session
) {
4473 struct ltt_kernel_channel
*chan
;
4474 const struct ltt_kernel_session
*ksess
=
4475 session
->kernel_session
;
4477 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4478 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4480 * Don't take channel into account if we
4481 * already grab all its packets.
4485 tot_size
+= chan
->channel
->attr
.subbuf_size
4486 * chan
->stream_count
;
4490 if (session
->ust_session
) {
4491 const struct ltt_ust_session
*usess
= session
->ust_session
;
4493 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4501 * Calculate the number of packets we can grab from each stream that
4502 * fits within the overall snapshot max size.
4504 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4505 * the number of packets per stream.
4507 * TODO: this approach is not perfect: we consider the worse case
4508 * (packet filling the sub-buffers) as an upper bound, but we could do
4509 * better if we do this calculation while we actually grab the packet
4510 * content: we would know how much padding we don't actually store into
4513 * This algorithm is currently bounded by the number of packets per
4516 * Since we call this algorithm before actually grabbing the data, it's
4517 * an approximation: for instance, applications could appear/disappear
4518 * in between this call and actually grabbing data.
4521 int64_t get_session_nb_packets_per_stream(const struct ltt_session
*session
,
4525 uint64_t cur_nb_packets
= 0;
4528 return 0; /* Infinite */
4531 size_left
= max_size
;
4533 uint64_t one_more_packet_tot_size
;
4535 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(
4536 session
, cur_nb_packets
);
4537 if (!one_more_packet_tot_size
) {
4538 /* We are already grabbing all packets. */
4541 size_left
-= one_more_packet_tot_size
;
4542 if (size_left
< 0) {
4547 if (!cur_nb_packets
&& size_left
!= max_size
) {
4548 /* Not enough room to grab one packet of each stream, error. */
4551 return cur_nb_packets
;
4555 enum lttng_error_code
snapshot_record(struct ltt_session
*session
,
4556 const struct snapshot_output
*snapshot_output
, int wait
)
4558 int64_t nb_packets_per_stream
;
4559 char snapshot_chunk_name
[LTTNG_NAME_MAX
];
4561 enum lttng_error_code ret_code
= LTTNG_OK
;
4562 struct lttng_trace_chunk
*snapshot_trace_chunk
;
4563 struct consumer_output
*original_ust_consumer_output
= NULL
;
4564 struct consumer_output
*original_kernel_consumer_output
= NULL
;
4565 struct consumer_output
*snapshot_ust_consumer_output
= NULL
;
4566 struct consumer_output
*snapshot_kernel_consumer_output
= NULL
;
4568 ret
= snprintf(snapshot_chunk_name
, sizeof(snapshot_chunk_name
),
4570 snapshot_output
->name
,
4571 snapshot_output
->datetime
,
4572 snapshot_output
->nb_snapshot
);
4573 if (ret
< 0 || ret
>= sizeof(snapshot_chunk_name
)) {
4574 ERR("Failed to format snapshot name");
4575 ret_code
= LTTNG_ERR_INVALID
;
4578 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4579 snapshot_output
->name
, session
->name
,
4580 snapshot_chunk_name
);
4581 if (!session
->kernel_session
&& !session
->ust_session
) {
4582 ERR("Failed to record snapshot as no channels exist");
4583 ret_code
= LTTNG_ERR_NO_CHANNEL
;
4587 if (session
->kernel_session
) {
4588 original_kernel_consumer_output
=
4589 session
->kernel_session
->consumer
;
4590 snapshot_kernel_consumer_output
=
4591 consumer_copy_output(snapshot_output
->consumer
);
4592 strcpy(snapshot_kernel_consumer_output
->chunk_path
,
4593 snapshot_chunk_name
);
4594 ret
= consumer_copy_sockets(snapshot_kernel_consumer_output
,
4595 original_kernel_consumer_output
);
4597 ERR("Failed to copy consumer sockets from snapshot output configuration");
4598 ret_code
= LTTNG_ERR_NOMEM
;
4601 ret_code
= set_relayd_for_snapshot(
4602 snapshot_kernel_consumer_output
, session
);
4603 if (ret_code
!= LTTNG_OK
) {
4604 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4607 session
->kernel_session
->consumer
=
4608 snapshot_kernel_consumer_output
;
4610 if (session
->ust_session
) {
4611 original_ust_consumer_output
= session
->ust_session
->consumer
;
4612 snapshot_ust_consumer_output
=
4613 consumer_copy_output(snapshot_output
->consumer
);
4614 strcpy(snapshot_ust_consumer_output
->chunk_path
,
4615 snapshot_chunk_name
);
4616 ret
= consumer_copy_sockets(snapshot_ust_consumer_output
,
4617 original_ust_consumer_output
);
4619 ERR("Failed to copy consumer sockets from snapshot output configuration");
4620 ret_code
= LTTNG_ERR_NOMEM
;
4623 ret_code
= set_relayd_for_snapshot(
4624 snapshot_ust_consumer_output
, session
);
4625 if (ret_code
!= LTTNG_OK
) {
4626 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4629 session
->ust_session
->consumer
=
4630 snapshot_ust_consumer_output
;
4633 snapshot_trace_chunk
= session_create_new_trace_chunk(session
,
4634 snapshot_kernel_consumer_output
?:
4635 snapshot_ust_consumer_output
,
4636 consumer_output_get_base_path(
4637 snapshot_output
->consumer
),
4638 snapshot_chunk_name
);
4639 if (!snapshot_trace_chunk
) {
4640 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4642 ret_code
= LTTNG_ERR_CREATE_DIR_FAIL
;
4645 assert(!session
->current_trace_chunk
);
4646 ret
= session_set_trace_chunk(session
, snapshot_trace_chunk
, NULL
);
4647 lttng_trace_chunk_put(snapshot_trace_chunk
);
4648 snapshot_trace_chunk
= NULL
;
4650 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4652 ret_code
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4656 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4657 snapshot_output
->max_size
);
4658 if (nb_packets_per_stream
< 0) {
4659 ret_code
= LTTNG_ERR_MAX_SIZE_INVALID
;
4660 goto error_close_trace_chunk
;
4663 if (session
->kernel_session
) {
4664 ret_code
= record_kernel_snapshot(session
->kernel_session
,
4665 snapshot_kernel_consumer_output
, session
,
4666 wait
, nb_packets_per_stream
);
4667 if (ret_code
!= LTTNG_OK
) {
4668 goto error_close_trace_chunk
;
4672 if (session
->ust_session
) {
4673 ret_code
= record_ust_snapshot(session
->ust_session
,
4674 snapshot_ust_consumer_output
, session
,
4675 wait
, nb_packets_per_stream
);
4676 if (ret_code
!= LTTNG_OK
) {
4677 goto error_close_trace_chunk
;
4680 error_close_trace_chunk
:
4681 if (session_close_trace_chunk(
4682 session
, session
->current_trace_chunk
, NULL
, NULL
)) {
4684 * Don't goto end; make sure the chunk is closed for the session
4685 * to allow future snapshots.
4687 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4689 ret_code
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
4691 if (session_set_trace_chunk(session
, NULL
, NULL
)) {
4692 ERR("Failed to release the current trace chunk of session \"%s\"",
4694 ret_code
= LTTNG_ERR_UNK
;
4697 if (original_ust_consumer_output
) {
4698 session
->ust_session
->consumer
= original_ust_consumer_output
;
4700 if (original_kernel_consumer_output
) {
4701 session
->kernel_session
->consumer
=
4702 original_kernel_consumer_output
;
4704 consumer_output_put(snapshot_ust_consumer_output
);
4705 consumer_output_put(snapshot_kernel_consumer_output
);
4710 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4712 * The wait parameter is ignored so this call always wait for the snapshot to
4713 * complete before returning.
4715 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4717 int cmd_snapshot_record(struct ltt_session
*session
,
4718 const struct lttng_snapshot_output
*output
, int wait
)
4720 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4722 unsigned int snapshot_success
= 0;
4724 struct snapshot_output
*tmp_output
= NULL
;
4729 DBG("Cmd snapshot record for session %s", session
->name
);
4731 /* Get the datetime for the snapshot output directory. */
4732 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4735 cmd_ret
= LTTNG_ERR_INVALID
;
4740 * Permission denied to create an output if the session is not
4741 * set in no output mode.
4743 if (session
->output_traces
) {
4744 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4748 /* The session needs to be started at least once. */
4749 if (!session
->has_been_started
) {
4750 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4754 /* Use temporary output for the session. */
4755 if (*output
->ctrl_url
!= '\0') {
4756 tmp_output
= snapshot_output_alloc();
4758 cmd_ret
= LTTNG_ERR_NOMEM
;
4762 ret
= snapshot_output_init(session
, output
->max_size
,
4764 output
->ctrl_url
, output
->data_url
,
4768 if (ret
== -ENOMEM
) {
4769 cmd_ret
= LTTNG_ERR_NOMEM
;
4771 cmd_ret
= LTTNG_ERR_INVALID
;
4775 /* Use the global session count for the temporary snapshot. */
4776 tmp_output
->nb_snapshot
= session
->snapshot
.nb_snapshot
;
4778 /* Use the global datetime */
4779 memcpy(tmp_output
->datetime
, datetime
, sizeof(datetime
));
4780 cmd_ret
= snapshot_record(session
, tmp_output
, wait
);
4781 if (cmd_ret
!= LTTNG_OK
) {
4784 snapshot_success
= 1;
4786 struct snapshot_output
*sout
;
4787 struct lttng_ht_iter iter
;
4790 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4791 &iter
.iter
, sout
, node
.node
) {
4792 struct snapshot_output output_copy
;
4795 * Make a local copy of the output and override output
4796 * parameters with those provided as part of the
4799 memcpy(&output_copy
, sout
, sizeof(output_copy
));
4801 if (output
->max_size
!= (uint64_t) -1ULL) {
4802 output_copy
.max_size
= output
->max_size
;
4805 output_copy
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4806 memcpy(output_copy
.datetime
, datetime
,
4809 /* Use temporary name. */
4810 if (*output
->name
!= '\0') {
4811 if (lttng_strncpy(output_copy
.name
,
4813 sizeof(output_copy
.name
))) {
4814 cmd_ret
= LTTNG_ERR_INVALID
;
4820 cmd_ret
= snapshot_record(session
, &output_copy
, wait
);
4821 if (cmd_ret
!= LTTNG_OK
) {
4825 snapshot_success
= 1;
4830 if (snapshot_success
) {
4831 session
->snapshot
.nb_snapshot
++;
4833 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4838 snapshot_output_destroy(tmp_output
);
4844 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4846 int cmd_set_session_shm_path(struct ltt_session
*session
,
4847 const char *shm_path
)
4853 * Can only set shm path before session is started.
4855 if (session
->has_been_started
) {
4856 return LTTNG_ERR_SESSION_STARTED
;
4859 strncpy(session
->shm_path
, shm_path
,
4860 sizeof(session
->shm_path
));
4861 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4867 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4869 * Ask the consumer to rotate the session output directory.
4870 * The session lock must be held.
4872 * Returns LTTNG_OK on success or else a negative LTTng error code.
4874 int cmd_rotate_session(struct ltt_session
*session
,
4875 struct lttng_rotate_session_return
*rotate_return
,
4876 bool quiet_rotation
)
4879 uint64_t ongoing_rotation_chunk_id
;
4880 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4881 struct lttng_trace_chunk
*chunk_being_archived
= NULL
;
4882 struct lttng_trace_chunk
*new_trace_chunk
= NULL
;
4883 enum lttng_trace_chunk_status chunk_status
;
4884 bool failed_to_rotate
= false;
4885 enum lttng_error_code rotation_fail_code
= LTTNG_OK
;
4889 if (!session
->has_been_started
) {
4890 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4895 * Explicit rotation is not supported for live sessions.
4896 * However, live sessions can perform a quiet rotation on
4898 * Rotation is not supported for snapshot traces (no output).
4900 if ((!quiet_rotation
&& session
->live_timer
) ||
4901 !session
->output_traces
) {
4902 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4906 /* Unsupported feature in lttng-relayd before 2.11. */
4907 if (!quiet_rotation
&& session
->consumer
->type
== CONSUMER_DST_NET
&&
4908 (session
->consumer
->relay_major_version
== 2 &&
4909 session
->consumer
->relay_minor_version
< 11)) {
4910 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
4914 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
4915 if (session
->kernel_session
&& !kernel_supports_ring_buffer_packet_sequence_number()) {
4916 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL
;
4920 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4921 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
4923 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
4928 * After a stop, we only allow one rotation to occur, the other ones are
4929 * useless until a new start.
4931 if (session
->rotated_after_last_stop
) {
4932 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4934 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
4937 if (session
->active
) {
4938 new_trace_chunk
= session_create_new_trace_chunk(session
, NULL
,
4940 if (!new_trace_chunk
) {
4941 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
4947 * The current trace chunk becomes the chunk being archived.
4949 * After this point, "chunk_being_archived" must absolutely
4950 * be closed on the consumer(s), otherwise it will never be
4951 * cleaned-up, which will result in a leak.
4953 ret
= session_set_trace_chunk(session
, new_trace_chunk
,
4954 &chunk_being_archived
);
4956 cmd_ret
= LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER
;
4960 if (session
->kernel_session
) {
4961 cmd_ret
= kernel_rotate_session(session
);
4962 if (cmd_ret
!= LTTNG_OK
) {
4963 failed_to_rotate
= true;
4964 rotation_fail_code
= cmd_ret
;
4967 if (session
->ust_session
) {
4968 cmd_ret
= ust_app_rotate_session(session
);
4969 if (cmd_ret
!= LTTNG_OK
) {
4970 failed_to_rotate
= true;
4971 rotation_fail_code
= cmd_ret
;
4975 if (!session
->active
) {
4976 session
->rotated_after_last_stop
= true;
4979 if (!chunk_being_archived
) {
4980 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
4982 if (failed_to_rotate
) {
4983 cmd_ret
= rotation_fail_code
;
4990 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
4991 chunk_status
= lttng_trace_chunk_get_id(chunk_being_archived
,
4992 &ongoing_rotation_chunk_id
);
4993 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
4995 ret
= session_close_trace_chunk(session
, chunk_being_archived
,
4998 &((enum lttng_trace_chunk_command_type
){
4999 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED
}),
5000 session
->last_chunk_path
);
5002 cmd_ret
= LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER
;
5006 if (failed_to_rotate
) {
5007 cmd_ret
= rotation_fail_code
;
5011 session
->quiet_rotation
= quiet_rotation
;
5012 ret
= timer_session_rotation_pending_check_start(session
,
5013 DEFAULT_ROTATE_PENDING_TIMER
);
5015 cmd_ret
= LTTNG_ERR_UNK
;
5019 if (rotate_return
) {
5020 rotate_return
->rotation_id
= ongoing_rotation_chunk_id
;
5023 session
->chunk_being_archived
= chunk_being_archived
;
5024 chunk_being_archived
= NULL
;
5025 if (!quiet_rotation
) {
5026 ret
= notification_thread_command_session_rotation_ongoing(
5027 notification_thread_handle
,
5028 session
->name
, session
->uid
, session
->gid
,
5029 ongoing_rotation_chunk_id
);
5030 if (ret
!= LTTNG_OK
) {
5031 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5037 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
5038 session
->name
, ongoing_rotation_chunk_id
);
5040 lttng_trace_chunk_put(new_trace_chunk
);
5041 lttng_trace_chunk_put(chunk_being_archived
);
5042 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
5045 if (session_reset_rotation_state(session
,
5046 LTTNG_ROTATION_STATE_ERROR
)) {
5047 ERR("Failed to reset rotation state of session \"%s\"",
5054 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5056 * Check if the session has finished its rotation.
5058 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5060 int cmd_rotate_get_info(struct ltt_session
*session
,
5061 struct lttng_rotation_get_info_return
*info_return
,
5062 uint64_t rotation_id
)
5064 enum lttng_error_code cmd_ret
= LTTNG_OK
;
5065 enum lttng_rotation_state rotation_state
;
5067 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
5068 session
->most_recent_chunk_id
.value
);
5070 if (session
->chunk_being_archived
) {
5071 enum lttng_trace_chunk_status chunk_status
;
5074 chunk_status
= lttng_trace_chunk_get_id(
5075 session
->chunk_being_archived
,
5077 assert(chunk_status
== LTTNG_TRACE_CHUNK_STATUS_OK
);
5079 rotation_state
= rotation_id
== chunk_id
?
5080 LTTNG_ROTATION_STATE_ONGOING
:
5081 LTTNG_ROTATION_STATE_EXPIRED
;
5083 if (session
->last_archived_chunk_id
.is_set
&&
5084 rotation_id
!= session
->last_archived_chunk_id
.value
) {
5085 rotation_state
= LTTNG_ROTATION_STATE_EXPIRED
;
5087 rotation_state
= session
->rotation_state
;
5091 switch (rotation_state
) {
5092 case LTTNG_ROTATION_STATE_NO_ROTATION
:
5093 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5096 case LTTNG_ROTATION_STATE_EXPIRED
:
5097 DBG("Reporting that the rotation state of rotation id %" PRIu64
" of session \"%s\" has expired",
5098 rotation_id
, session
->name
);
5100 case LTTNG_ROTATION_STATE_ONGOING
:
5101 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is still pending",
5102 rotation_id
, session
->name
);
5104 case LTTNG_ROTATION_STATE_COMPLETED
:
5108 char *current_tracing_path_reply
;
5109 size_t current_tracing_path_reply_len
;
5111 DBG("Reporting that rotation id %" PRIu64
" of session \"%s\" is completed",
5112 rotation_id
, session
->name
);
5114 switch (session_get_consumer_destination_type(session
)) {
5115 case CONSUMER_DST_LOCAL
:
5116 current_tracing_path_reply
=
5117 info_return
->location
.local
.absolute_path
;
5118 current_tracing_path_reply_len
=
5119 sizeof(info_return
->location
.local
.absolute_path
);
5120 info_return
->location_type
=
5121 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
5122 fmt_ret
= asprintf(&chunk_path
,
5123 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY
"/%s",
5124 session_get_base_path(session
),
5125 session
->last_archived_chunk_name
);
5126 if (fmt_ret
== -1) {
5127 PERROR("Failed to format the path of the last archived trace chunk");
5128 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5129 cmd_ret
= LTTNG_ERR_UNK
;
5133 case CONSUMER_DST_NET
:
5135 uint16_t ctrl_port
, data_port
;
5137 current_tracing_path_reply
=
5138 info_return
->location
.relay
.relative_path
;
5139 current_tracing_path_reply_len
=
5140 sizeof(info_return
->location
.relay
.relative_path
);
5141 /* Currently the only supported relay protocol. */
5142 info_return
->location
.relay
.protocol
=
5143 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
5145 fmt_ret
= lttng_strncpy(info_return
->location
.relay
.host
,
5146 session_get_net_consumer_hostname(session
),
5147 sizeof(info_return
->location
.relay
.host
));
5149 ERR("Failed to copy host name to rotate_get_info reply");
5150 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5151 cmd_ret
= LTTNG_ERR_SET_URL
;
5155 session_get_net_consumer_ports(session
, &ctrl_port
, &data_port
);
5156 info_return
->location
.relay
.ports
.control
= ctrl_port
;
5157 info_return
->location
.relay
.ports
.data
= data_port
;
5158 info_return
->location_type
=
5159 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
5160 chunk_path
= strdup(session
->last_chunk_path
);
5162 ERR("Failed to allocate the path of the last archived trace chunk");
5163 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5164 cmd_ret
= LTTNG_ERR_UNK
;
5173 fmt_ret
= lttng_strncpy(current_tracing_path_reply
,
5174 chunk_path
, current_tracing_path_reply_len
);
5177 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5178 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
5179 cmd_ret
= LTTNG_ERR_UNK
;
5185 case LTTNG_ROTATION_STATE_ERROR
:
5186 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session \"%s\"",
5187 rotation_id
, session
->name
);
5195 info_return
->status
= (int32_t) rotation_state
;
5200 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5202 * Configure the automatic rotation parameters.
5203 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5204 * 'activate' to false means deactivate the rotation schedule and validate that
5205 * 'new_value' has the same value as the currently active value.
5207 * Return 0 on success or else a positive LTTNG_ERR code.
5209 int cmd_rotation_set_schedule(struct ltt_session
*session
,
5210 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
5212 struct notification_thread_handle
*notification_thread_handle
)
5215 uint64_t *parameter_value
;
5219 DBG("Cmd rotate set schedule session %s", session
->name
);
5221 if (session
->live_timer
|| !session
->output_traces
) {
5222 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5223 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
5227 switch (schedule_type
) {
5228 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5229 parameter_value
= &session
->rotate_size
;
5231 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5232 parameter_value
= &session
->rotate_timer_period
;
5233 if (new_value
>= UINT_MAX
) {
5234 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
5235 new_value
, UINT_MAX
);
5236 ret
= LTTNG_ERR_INVALID
;
5241 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5242 ret
= LTTNG_ERR_INVALID
;
5246 /* Improper use of the API. */
5247 if (new_value
== -1ULL) {
5248 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5249 ret
= LTTNG_ERR_INVALID
;
5254 * As indicated in struct ltt_session's comments, a value of == 0 means
5255 * this schedule rotation type is not in use.
5257 * Reject the command if we were asked to activate a schedule that was
5260 if (activate
&& *parameter_value
!= 0) {
5261 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5262 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5267 * Reject the command if we were asked to deactivate a schedule that was
5270 if (!activate
&& *parameter_value
== 0) {
5271 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5272 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5277 * Reject the command if we were asked to deactivate a schedule that
5280 if (!activate
&& *parameter_value
!= new_value
) {
5281 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5282 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5286 *parameter_value
= activate
? new_value
: 0;
5288 switch (schedule_type
) {
5289 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5290 if (activate
&& session
->active
) {
5292 * Only start the timer if the session is active,
5293 * otherwise it will be started when the session starts.
5295 ret
= timer_session_rotation_schedule_timer_start(
5296 session
, new_value
);
5298 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5299 ret
= LTTNG_ERR_UNK
;
5303 ret
= timer_session_rotation_schedule_timer_stop(
5306 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5307 ret
= LTTNG_ERR_UNK
;
5312 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5314 ret
= subscribe_session_consumed_size_rotation(session
,
5315 new_value
, notification_thread_handle
);
5317 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5318 ret
= LTTNG_ERR_UNK
;
5322 ret
= unsubscribe_session_consumed_size_rotation(session
,
5323 notification_thread_handle
);
5325 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5326 ret
= LTTNG_ERR_UNK
;
5333 /* Would have been caught before. */
5345 /* Wait for a given path to be removed before continuing. */
5346 static enum lttng_error_code
wait_on_path(void *path_data
)
5348 const char *shm_path
= path_data
;
5350 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5356 ret
= stat(shm_path
, &st
);
5358 if (errno
!= ENOENT
) {
5359 PERROR("stat() returned an error while checking for the existence of the shm path");
5361 DBG("shm path no longer exists, completing the destruction of session");
5365 if (!S_ISDIR(st
.st_mode
)) {
5366 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5371 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5377 * Returns a pointer to a handler to run on completion of a command.
5378 * Returns NULL if no handler has to be run for the last command executed.
5380 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5382 struct cmd_completion_handler
*handler
= current_completion_handler
;
5384 current_completion_handler
= NULL
;
5389 * Init command subsystem.
5394 * Set network sequence index to 1 for streams to match a relayd
5395 * socket on the consumer side.
5397 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5398 relayd_net_seq_idx
= 1;
5399 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5401 DBG("Command subsystem initialized");