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>
26 #include <common/defaults.h>
27 #include <common/common.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/relayd/relayd.h>
30 #include <common/utils.h>
31 #include <common/compat/string.h>
32 #include <common/kernel-ctl/kernel-ctl.h>
33 #include <common/dynamic-buffer.h>
34 #include <common/buffer-view.h>
35 #include <lttng/trigger/trigger-internal.h>
36 #include <lttng/condition/condition.h>
37 #include <lttng/action/action.h>
38 #include <lttng/channel.h>
39 #include <lttng/channel-internal.h>
40 #include <lttng/rotate-internal.h>
41 #include <lttng/location-internal.h>
42 #include <lttng/session-internal.h>
43 #include <lttng/userspace-probe-internal.h>
44 #include <lttng/session-descriptor-internal.h>
45 #include <common/string-utils/string-utils.h>
50 #include "health-sessiond.h"
52 #include "kernel-consumer.h"
53 #include "lttng-sessiond.h"
55 #include "lttng-syscall.h"
57 #include "buffer-registry.h"
58 #include "notification-thread.h"
59 #include "notification-thread-commands.h"
61 #include "rotation-thread.h"
63 #include "agent-thread.h"
67 /* Sleep for 100ms between each check for the shm path's deletion. */
68 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
70 static enum lttng_error_code
wait_on_path(void *path
);
73 * Command completion handler that is used by the destroy command
74 * when a session that has a non-default shm_path is being destroyed.
76 * See comment in cmd_destroy_session() for the rationale.
78 static struct destroy_completion_handler
{
79 struct cmd_completion_handler handler
;
80 char shm_path
[member_sizeof(struct ltt_session
, shm_path
)];
81 } destroy_completion_handler
= {
84 .data
= destroy_completion_handler
.shm_path
89 static struct cmd_completion_handler
*current_completion_handler
;
92 * Used to keep a unique index for each relayd socket created where this value
93 * is associated with streams on the consumer so it can match the right relayd
94 * to send to. It must be accessed with the relayd_net_seq_idx_lock
97 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
98 static uint64_t relayd_net_seq_idx
;
100 static int validate_ust_event_name(const char *);
101 static int cmd_enable_event_internal(struct ltt_session
*session
,
102 struct lttng_domain
*domain
,
103 char *channel_name
, struct lttng_event
*event
,
104 char *filter_expression
,
105 struct lttng_filter_bytecode
*filter
,
106 struct lttng_event_exclusion
*exclusion
,
110 * Create a session path used by list_lttng_sessions for the case that the
111 * session consumer is on the network.
113 static int build_network_session_path(char *dst
, size_t size
,
114 struct ltt_session
*session
)
116 int ret
, kdata_port
, udata_port
;
117 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
118 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
123 memset(tmp_urls
, 0, sizeof(tmp_urls
));
124 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
126 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
128 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
129 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
130 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
133 if (session
->ust_session
&& session
->ust_session
->consumer
) {
134 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
135 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
138 if (uuri
== NULL
&& kuri
== NULL
) {
139 uri
= &session
->consumer
->dst
.net
.control
;
140 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
141 } else if (kuri
&& uuri
) {
142 ret
= uri_compare(kuri
, uuri
);
146 /* Build uuri URL string */
147 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
154 } else if (kuri
&& uuri
== NULL
) {
156 } else if (uuri
&& kuri
== NULL
) {
160 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
166 * Do we have a UST url set. If yes, this means we have both kernel and UST
169 if (*tmp_uurl
!= '\0') {
170 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
171 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
174 if (kuri
|| (!kuri
&& !uuri
)) {
177 /* No kernel URI, use the UST port. */
180 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
188 * Get run-time attributes if the session has been started (discarded events,
191 static int get_kernel_runtime_stats(struct ltt_session
*session
,
192 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
193 uint64_t *lost_packets
)
197 if (!session
->has_been_started
) {
199 *discarded_events
= 0;
204 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
205 session
->kernel_session
->consumer
,
211 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
212 session
->kernel_session
->consumer
,
223 * Get run-time attributes if the session has been started (discarded events,
226 static int get_ust_runtime_stats(struct ltt_session
*session
,
227 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
228 uint64_t *lost_packets
)
231 struct ltt_ust_session
*usess
;
233 if (!discarded_events
|| !lost_packets
) {
238 usess
= session
->ust_session
;
239 assert(discarded_events
);
240 assert(lost_packets
);
242 if (!usess
|| !session
->has_been_started
) {
243 *discarded_events
= 0;
249 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
250 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
251 &usess
->buffer_reg_uid_list
,
252 usess
->consumer
, uchan
->id
,
253 uchan
->attr
.overwrite
,
256 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
257 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
258 uchan
, usess
->consumer
,
259 uchan
->attr
.overwrite
,
265 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
266 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
268 ERR("Unsupported buffer type");
279 * Fill lttng_channel array of all channels.
281 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
282 struct ltt_session
*session
, struct lttng_channel
*channels
,
283 struct lttng_channel_extended
*chan_exts
)
286 struct ltt_kernel_channel
*kchan
;
288 DBG("Listing channels for session %s", session
->name
);
291 case LTTNG_DOMAIN_KERNEL
:
292 /* Kernel channels */
293 if (session
->kernel_session
!= NULL
) {
294 cds_list_for_each_entry(kchan
,
295 &session
->kernel_session
->channel_list
.head
, list
) {
296 uint64_t discarded_events
, lost_packets
;
297 struct lttng_channel_extended
*extended
;
299 extended
= (struct lttng_channel_extended
*)
300 kchan
->channel
->attr
.extended
.ptr
;
302 ret
= get_kernel_runtime_stats(session
, kchan
,
303 &discarded_events
, &lost_packets
);
307 /* Copy lttng_channel struct to array */
308 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
309 channels
[i
].enabled
= kchan
->enabled
;
310 chan_exts
[i
].discarded_events
=
312 chan_exts
[i
].lost_packets
= lost_packets
;
313 chan_exts
[i
].monitor_timer_interval
=
314 extended
->monitor_timer_interval
;
315 chan_exts
[i
].blocking_timeout
= 0;
320 case LTTNG_DOMAIN_UST
:
322 struct lttng_ht_iter iter
;
323 struct ltt_ust_channel
*uchan
;
326 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
327 &iter
.iter
, uchan
, node
.node
) {
328 uint64_t discarded_events
= 0, lost_packets
= 0;
330 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
331 LTTNG_SYMBOL_NAME_LEN
)) {
334 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
335 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
336 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
337 channels
[i
].attr
.switch_timer_interval
=
338 uchan
->attr
.switch_timer_interval
;
339 channels
[i
].attr
.read_timer_interval
=
340 uchan
->attr
.read_timer_interval
;
341 channels
[i
].enabled
= uchan
->enabled
;
342 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
343 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
346 * Map enum lttng_ust_output to enum lttng_event_output.
348 switch (uchan
->attr
.output
) {
350 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
354 * LTTNG_UST_MMAP is the only supported UST
361 chan_exts
[i
].monitor_timer_interval
=
362 uchan
->monitor_timer_interval
;
363 chan_exts
[i
].blocking_timeout
=
364 uchan
->attr
.u
.s
.blocking_timeout
;
366 ret
= get_ust_runtime_stats(session
, uchan
,
367 &discarded_events
, &lost_packets
);
371 chan_exts
[i
].discarded_events
= discarded_events
;
372 chan_exts
[i
].lost_packets
= lost_packets
;
384 return -LTTNG_ERR_FATAL
;
390 static int increment_extended_len(const char *filter_expression
,
391 struct lttng_event_exclusion
*exclusion
,
392 const struct lttng_userspace_probe_location
*probe_location
,
393 size_t *extended_len
)
397 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
399 if (filter_expression
) {
400 *extended_len
+= strlen(filter_expression
) + 1;
404 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
407 if (probe_location
) {
408 ret
= lttng_userspace_probe_location_serialize(probe_location
,
413 *extended_len
+= ret
;
420 static int append_extended_info(const char *filter_expression
,
421 struct lttng_event_exclusion
*exclusion
,
422 struct lttng_userspace_probe_location
*probe_location
,
426 size_t filter_len
= 0;
427 size_t nb_exclusions
= 0;
428 size_t userspace_probe_location_len
= 0;
429 struct lttng_dynamic_buffer location_buffer
;
430 struct lttcomm_event_extended_header extended_header
;
432 if (filter_expression
) {
433 filter_len
= strlen(filter_expression
) + 1;
437 nb_exclusions
= exclusion
->count
;
440 if (probe_location
) {
441 lttng_dynamic_buffer_init(&location_buffer
);
442 ret
= lttng_userspace_probe_location_serialize(probe_location
,
443 &location_buffer
, NULL
);
448 userspace_probe_location_len
= location_buffer
.size
;
451 /* Set header fields */
452 extended_header
.filter_len
= filter_len
;
453 extended_header
.nb_exclusions
= nb_exclusions
;
454 extended_header
.userspace_probe_location_len
= userspace_probe_location_len
;
457 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
458 *extended_at
+= sizeof(extended_header
);
460 /* Copy filter string */
461 if (filter_expression
) {
462 memcpy(*extended_at
, filter_expression
, filter_len
);
463 *extended_at
+= filter_len
;
466 /* Copy exclusion names */
468 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
470 memcpy(*extended_at
, &exclusion
->names
, len
);
474 if (probe_location
) {
475 memcpy(*extended_at
, location_buffer
.data
, location_buffer
.size
);
476 *extended_at
+= location_buffer
.size
;
477 lttng_dynamic_buffer_reset(&location_buffer
);
485 * Create a list of agent domain events.
487 * Return number of events in list on success or else a negative value.
489 static int list_lttng_agent_events(struct agent
*agt
,
490 struct lttng_event
**events
, size_t *total_size
)
493 unsigned int nb_event
= 0;
494 struct agent_event
*event
;
495 struct lttng_event
*tmp_events
= NULL
;
496 struct lttng_ht_iter iter
;
497 size_t extended_len
= 0;
503 DBG3("Listing agent events");
506 nb_event
= lttng_ht_get_count(agt
->events
);
514 /* Compute required extended infos size */
515 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
518 * This is only valid because the commands which add events are
519 * processed in the same thread as the listing.
522 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
523 ret
= increment_extended_len(event
->filter_expression
, NULL
, NULL
,
526 DBG("Error computing the length of extended info message");
527 ret
= -LTTNG_ERR_FATAL
;
533 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
534 tmp_events
= zmalloc(*total_size
);
536 PERROR("zmalloc agent events session");
537 ret
= -LTTNG_ERR_FATAL
;
541 extended_at
= ((uint8_t *) tmp_events
) +
542 nb_event
* sizeof(struct lttng_event
);
545 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
546 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
547 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
548 tmp_events
[i
].enabled
= event
->enabled
;
549 tmp_events
[i
].loglevel
= event
->loglevel_value
;
550 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
553 /* Append extended info */
554 ret
= append_extended_info(event
->filter_expression
, NULL
, NULL
,
557 DBG("Error appending extended info message");
558 ret
= -LTTNG_ERR_FATAL
;
563 *events
= tmp_events
;
565 assert(nb_event
== i
);
576 * Create a list of ust global domain events.
578 static int list_lttng_ust_global_events(char *channel_name
,
579 struct ltt_ust_domain_global
*ust_global
,
580 struct lttng_event
**events
, size_t *total_size
)
583 unsigned int nb_event
= 0;
584 struct lttng_ht_iter iter
;
585 struct lttng_ht_node_str
*node
;
586 struct ltt_ust_channel
*uchan
;
587 struct ltt_ust_event
*uevent
;
588 struct lttng_event
*tmp
;
589 size_t extended_len
= 0;
592 DBG("Listing UST global events for channel %s", channel_name
);
596 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
597 node
= lttng_ht_iter_get_node_str(&iter
);
599 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
603 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
605 nb_event
= lttng_ht_get_count(uchan
->events
);
612 DBG3("Listing UST global %d events", nb_event
);
614 /* Compute required extended infos size */
615 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
616 if (uevent
->internal
) {
621 ret
= increment_extended_len(uevent
->filter_expression
,
622 uevent
->exclusion
, NULL
, &extended_len
);
624 DBG("Error computing the length of extended info message");
625 ret
= -LTTNG_ERR_FATAL
;
630 /* All events are internal, skip. */
636 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
637 tmp
= zmalloc(*total_size
);
639 ret
= -LTTNG_ERR_FATAL
;
643 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
645 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
646 if (uevent
->internal
) {
647 /* This event should remain hidden from clients */
650 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
651 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
652 tmp
[i
].enabled
= uevent
->enabled
;
654 switch (uevent
->attr
.instrumentation
) {
655 case LTTNG_UST_TRACEPOINT
:
656 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
658 case LTTNG_UST_PROBE
:
659 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
661 case LTTNG_UST_FUNCTION
:
662 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
666 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
667 switch (uevent
->attr
.loglevel_type
) {
668 case LTTNG_UST_LOGLEVEL_ALL
:
669 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
671 case LTTNG_UST_LOGLEVEL_RANGE
:
672 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
674 case LTTNG_UST_LOGLEVEL_SINGLE
:
675 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
678 if (uevent
->filter
) {
681 if (uevent
->exclusion
) {
682 tmp
[i
].exclusion
= 1;
686 /* Append extended info */
687 ret
= append_extended_info(uevent
->filter_expression
,
688 uevent
->exclusion
, NULL
, &extended_at
);
690 DBG("Error appending extended info message");
691 ret
= -LTTNG_ERR_FATAL
;
704 * Fill lttng_event array of all kernel events in the channel.
706 static int list_lttng_kernel_events(char *channel_name
,
707 struct ltt_kernel_session
*kernel_session
,
708 struct lttng_event
**events
, size_t *total_size
)
711 unsigned int nb_event
;
712 struct ltt_kernel_event
*event
;
713 struct ltt_kernel_channel
*kchan
;
714 size_t extended_len
= 0;
717 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
719 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
723 nb_event
= kchan
->event_count
;
725 DBG("Listing events for channel %s", kchan
->channel
->name
);
733 /* Compute required extended infos size */
734 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
735 ret
= increment_extended_len(event
->filter_expression
, NULL
,
736 event
->userspace_probe_location
,
739 DBG("Error computing the length of extended info message");
740 ret
= -LTTNG_ERR_FATAL
;
745 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
746 *events
= zmalloc(*total_size
);
747 if (*events
== NULL
) {
748 ret
= -LTTNG_ERR_FATAL
;
752 extended_at
= ((void *) *events
) +
753 nb_event
* sizeof(struct lttng_event
);
755 /* Kernel channels */
756 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
757 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
758 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
759 (*events
)[i
].enabled
= event
->enabled
;
760 (*events
)[i
].filter
=
761 (unsigned char) !!event
->filter_expression
;
763 switch (event
->event
->instrumentation
) {
764 case LTTNG_KERNEL_TRACEPOINT
:
765 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
767 case LTTNG_KERNEL_KRETPROBE
:
768 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
769 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
770 sizeof(struct lttng_kernel_kprobe
));
772 case LTTNG_KERNEL_KPROBE
:
773 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
774 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
775 sizeof(struct lttng_kernel_kprobe
));
777 case LTTNG_KERNEL_UPROBE
:
778 (*events
)[i
].type
= LTTNG_EVENT_USERSPACE_PROBE
;
780 case LTTNG_KERNEL_FUNCTION
:
781 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
782 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
783 sizeof(struct lttng_kernel_function
));
785 case LTTNG_KERNEL_NOOP
:
786 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
788 case LTTNG_KERNEL_SYSCALL
:
789 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
791 case LTTNG_KERNEL_ALL
:
799 /* Append extended info */
800 ret
= append_extended_info(event
->filter_expression
, NULL
,
801 event
->userspace_probe_location
, &extended_at
);
803 DBG("Error appending extended info message");
804 ret
= -LTTNG_ERR_FATAL
;
813 /* Negate the error code to differentiate the size from an error */
818 * Add URI so the consumer output object. Set the correct path depending on the
819 * domain adding the default trace directory.
821 static enum lttng_error_code
add_uri_to_consumer(
822 const struct ltt_session
*session
,
823 struct consumer_output
*consumer
,
824 struct lttng_uri
*uri
, enum lttng_domain_type domain
)
827 enum lttng_error_code ret_code
= LTTNG_OK
;
831 if (consumer
== NULL
) {
832 DBG("No consumer detected. Don't add URI. Stopping.");
833 ret_code
= LTTNG_ERR_NO_CONSUMER
;
838 case LTTNG_DOMAIN_KERNEL
:
839 ret
= lttng_strncpy(consumer
->domain_subdir
,
840 DEFAULT_KERNEL_TRACE_DIR
,
841 sizeof(consumer
->domain_subdir
));
843 case LTTNG_DOMAIN_UST
:
844 ret
= lttng_strncpy(consumer
->domain_subdir
,
845 DEFAULT_UST_TRACE_DIR
,
846 sizeof(consumer
->domain_subdir
));
850 * This case is possible is we try to add the URI to the global
851 * tracing session consumer object which in this case there is
854 memset(consumer
->domain_subdir
, 0,
855 sizeof(consumer
->domain_subdir
));
859 ERR("Failed to initialize consumer output domain subdirectory");
860 ret_code
= LTTNG_ERR_FATAL
;
864 switch (uri
->dtype
) {
867 DBG2("Setting network URI to consumer");
869 if (consumer
->type
== CONSUMER_DST_NET
) {
870 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
871 consumer
->dst
.net
.control_isset
) ||
872 (uri
->stype
== LTTNG_STREAM_DATA
&&
873 consumer
->dst
.net
.data_isset
)) {
874 ret_code
= LTTNG_ERR_URL_EXIST
;
878 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
881 /* Set URI into consumer output object */
882 ret
= consumer_set_network_uri(session
, consumer
, uri
);
886 } else if (ret
== 1) {
888 * URI was the same in the consumer so we do not append the subdir
889 * again so to not duplicate output dir.
896 if (*uri
->dst
.path
!= '/' || strstr(uri
->dst
.path
, "../")) {
897 ret_code
= LTTNG_ERR_INVALID
;
900 DBG2("Setting trace directory path from URI to %s",
902 memset(&consumer
->dst
, 0, sizeof(consumer
->dst
));
904 ret
= lttng_strncpy(consumer
->dst
.session_root_path
,
906 sizeof(consumer
->dst
.session_root_path
));
907 consumer
->type
= CONSUMER_DST_LOCAL
;
917 * Init tracing by creating trace directory and sending fds kernel consumer.
919 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
922 struct lttng_ht_iter iter
;
923 struct consumer_socket
*socket
;
929 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
930 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
932 pthread_mutex_lock(socket
->lock
);
933 ret
= kernel_consumer_send_session(socket
, session
);
934 pthread_mutex_unlock(socket
->lock
);
936 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
948 * Create a socket to the relayd using the URI.
950 * On success, the relayd_sock pointer is set to the created socket.
951 * Else, it remains untouched and an LTTng error code is returned.
953 static enum lttng_error_code
create_connect_relayd(struct lttng_uri
*uri
,
954 struct lttcomm_relayd_sock
**relayd_sock
,
955 struct consumer_output
*consumer
)
958 enum lttng_error_code status
= LTTNG_OK
;
959 struct lttcomm_relayd_sock
*rsock
;
961 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
962 RELAYD_VERSION_COMM_MINOR
);
964 status
= LTTNG_ERR_FATAL
;
969 * Connect to relayd so we can proceed with a session creation. This call
970 * can possibly block for an arbitrary amount of time to set the health
971 * state to be in poll execution.
974 ret
= relayd_connect(rsock
);
977 ERR("Unable to reach lttng-relayd");
978 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
982 /* Create socket for control stream. */
983 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
984 DBG3("Creating relayd stream socket from URI");
986 /* Check relayd version */
987 ret
= relayd_version_check(rsock
);
988 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
989 status
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
991 } else if (ret
< 0) {
992 ERR("Unable to reach lttng-relayd");
993 status
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
996 consumer
->relay_major_version
= rsock
->major
;
997 consumer
->relay_minor_version
= rsock
->minor
;
998 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
999 DBG3("Creating relayd data socket from URI");
1001 /* Command is not valid */
1002 ERR("Relayd invalid stream type: %d", uri
->stype
);
1003 status
= LTTNG_ERR_INVALID
;
1007 *relayd_sock
= rsock
;
1012 /* The returned value is not useful since we are on an error path. */
1013 (void) relayd_close(rsock
);
1021 * Connect to the relayd using URI and send the socket to the right consumer.
1023 * The consumer socket lock must be held by the caller.
1025 * Returns LTTNG_OK on success or an LTTng error code on failure.
1027 static enum lttng_error_code
send_consumer_relayd_socket(
1028 unsigned int session_id
,
1029 struct lttng_uri
*relayd_uri
,
1030 struct consumer_output
*consumer
,
1031 struct consumer_socket
*consumer_sock
,
1032 char *session_name
, char *hostname
, int session_live_timer
)
1035 struct lttcomm_relayd_sock
*rsock
= NULL
;
1036 enum lttng_error_code status
;
1038 /* Connect to relayd and make version check if uri is the control. */
1039 status
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
1040 if (status
!= LTTNG_OK
) {
1041 goto relayd_comm_error
;
1045 /* Set the network sequence index if not set. */
1046 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
1047 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
1049 * Increment net_seq_idx because we are about to transfer the
1050 * new relayd socket to the consumer.
1051 * Assign unique key so the consumer can match streams.
1053 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
1054 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
1057 /* Send relayd socket to consumer. */
1058 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
1059 relayd_uri
->stype
, session_id
,
1060 session_name
, hostname
, session_live_timer
);
1062 status
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
1066 /* Flag that the corresponding socket was sent. */
1067 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1068 consumer_sock
->control_sock_sent
= 1;
1069 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
1070 consumer_sock
->data_sock_sent
= 1;
1074 * Close socket which was dup on the consumer side. The session daemon does
1075 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1079 if (status
!= LTTNG_OK
) {
1081 * The consumer output for this session should not be used anymore
1082 * since the relayd connection failed thus making any tracing or/and
1083 * streaming not usable.
1085 consumer
->enabled
= 0;
1087 (void) relayd_close(rsock
);
1095 * Send both relayd sockets to a specific consumer and domain. This is a
1096 * helper function to facilitate sending the information to the consumer for a
1099 * The consumer socket lock must be held by the caller.
1101 * Returns LTTNG_OK, or an LTTng error code on failure.
1103 static enum lttng_error_code
send_consumer_relayd_sockets(
1104 enum lttng_domain_type domain
,
1105 unsigned int session_id
, struct consumer_output
*consumer
,
1106 struct consumer_socket
*sock
, char *session_name
,
1107 char *hostname
, int session_live_timer
)
1109 enum lttng_error_code status
= LTTNG_OK
;
1114 /* Sending control relayd socket. */
1115 if (!sock
->control_sock_sent
) {
1116 status
= send_consumer_relayd_socket(session_id
,
1117 &consumer
->dst
.net
.control
, consumer
, sock
,
1118 session_name
, hostname
, session_live_timer
);
1119 if (status
!= LTTNG_OK
) {
1124 /* Sending data relayd socket. */
1125 if (!sock
->data_sock_sent
) {
1126 status
= send_consumer_relayd_socket(session_id
,
1127 &consumer
->dst
.net
.data
, consumer
, sock
,
1128 session_name
, hostname
, session_live_timer
);
1129 if (status
!= LTTNG_OK
) {
1139 * Setup relayd connections for a tracing session. First creates the socket to
1140 * the relayd and send them to the right domain consumer. Consumer type MUST be
1143 int cmd_setup_relayd(struct ltt_session
*session
)
1146 struct ltt_ust_session
*usess
;
1147 struct ltt_kernel_session
*ksess
;
1148 struct consumer_socket
*socket
;
1149 struct lttng_ht_iter iter
;
1153 usess
= session
->ust_session
;
1154 ksess
= session
->kernel_session
;
1156 DBG("Setting relayd for session %s", session
->name
);
1160 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1161 && usess
->consumer
->enabled
) {
1162 /* For each consumer socket, send relayd sockets */
1163 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1164 socket
, node
.node
) {
1165 pthread_mutex_lock(socket
->lock
);
1166 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1167 usess
->consumer
, socket
,
1168 session
->name
, session
->hostname
,
1169 session
->live_timer
);
1170 pthread_mutex_unlock(socket
->lock
);
1171 if (ret
!= LTTNG_OK
) {
1174 /* Session is now ready for network streaming. */
1175 session
->net_handle
= 1;
1177 session
->consumer
->relay_major_version
=
1178 usess
->consumer
->relay_major_version
;
1179 session
->consumer
->relay_minor_version
=
1180 usess
->consumer
->relay_minor_version
;
1183 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1184 && ksess
->consumer
->enabled
) {
1185 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1186 socket
, node
.node
) {
1187 pthread_mutex_lock(socket
->lock
);
1188 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1189 ksess
->consumer
, socket
,
1190 session
->name
, session
->hostname
,
1191 session
->live_timer
);
1192 pthread_mutex_unlock(socket
->lock
);
1193 if (ret
!= LTTNG_OK
) {
1196 /* Session is now ready for network streaming. */
1197 session
->net_handle
= 1;
1199 session
->consumer
->relay_major_version
=
1200 ksess
->consumer
->relay_major_version
;
1201 session
->consumer
->relay_minor_version
=
1202 ksess
->consumer
->relay_minor_version
;
1211 * Start a kernel session by opening all necessary streams.
1213 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1216 struct ltt_kernel_channel
*kchan
;
1218 /* Open kernel metadata */
1219 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1220 ret
= kernel_open_metadata(ksess
);
1222 ret
= LTTNG_ERR_KERN_META_FAIL
;
1227 /* Open kernel metadata stream */
1228 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1229 ret
= kernel_open_metadata_stream(ksess
);
1231 ERR("Kernel create metadata stream failed");
1232 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1237 /* For each channel */
1238 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1239 if (kchan
->stream_count
== 0) {
1240 ret
= kernel_open_channel_stream(kchan
);
1242 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1245 /* Update the stream global counter */
1246 ksess
->stream_count_global
+= ret
;
1250 /* Setup kernel consumer socket and send fds to it */
1251 ret
= init_kernel_tracing(ksess
);
1253 ret
= LTTNG_ERR_KERN_START_FAIL
;
1257 /* This start the kernel tracing */
1258 ret
= kernel_start_session(ksess
);
1260 ret
= LTTNG_ERR_KERN_START_FAIL
;
1264 /* Quiescent wait after starting trace */
1265 kernel_wait_quiescent(wpipe
);
1276 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1278 int cmd_disable_channel(struct ltt_session
*session
,
1279 enum lttng_domain_type domain
, char *channel_name
)
1282 struct ltt_ust_session
*usess
;
1284 usess
= session
->ust_session
;
1289 case LTTNG_DOMAIN_KERNEL
:
1291 ret
= channel_kernel_disable(session
->kernel_session
,
1293 if (ret
!= LTTNG_OK
) {
1297 kernel_wait_quiescent(kernel_tracer_fd
);
1300 case LTTNG_DOMAIN_UST
:
1302 struct ltt_ust_channel
*uchan
;
1303 struct lttng_ht
*chan_ht
;
1305 chan_ht
= usess
->domain_global
.channels
;
1307 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1308 if (uchan
== NULL
) {
1309 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1313 ret
= channel_ust_disable(usess
, uchan
);
1314 if (ret
!= LTTNG_OK
) {
1320 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1332 * Command LTTNG_TRACK_PID processed by the client thread.
1334 * Called with session lock held.
1336 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1344 case LTTNG_DOMAIN_KERNEL
:
1346 struct ltt_kernel_session
*ksess
;
1348 ksess
= session
->kernel_session
;
1350 ret
= kernel_track_pid(ksess
, pid
);
1351 if (ret
!= LTTNG_OK
) {
1355 kernel_wait_quiescent(kernel_tracer_fd
);
1358 case LTTNG_DOMAIN_UST
:
1360 struct ltt_ust_session
*usess
;
1362 usess
= session
->ust_session
;
1364 ret
= trace_ust_track_pid(usess
, pid
);
1365 if (ret
!= LTTNG_OK
) {
1371 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1383 * Command LTTNG_UNTRACK_PID processed by the client thread.
1385 * Called with session lock held.
1387 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1395 case LTTNG_DOMAIN_KERNEL
:
1397 struct ltt_kernel_session
*ksess
;
1399 ksess
= session
->kernel_session
;
1401 ret
= kernel_untrack_pid(ksess
, pid
);
1402 if (ret
!= LTTNG_OK
) {
1406 kernel_wait_quiescent(kernel_tracer_fd
);
1409 case LTTNG_DOMAIN_UST
:
1411 struct ltt_ust_session
*usess
;
1413 usess
= session
->ust_session
;
1415 ret
= trace_ust_untrack_pid(usess
, pid
);
1416 if (ret
!= LTTNG_OK
) {
1422 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1434 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1436 * The wpipe arguments is used as a notifier for the kernel thread.
1438 int cmd_enable_channel(struct ltt_session
*session
,
1439 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1442 struct ltt_ust_session
*usess
= session
->ust_session
;
1443 struct lttng_ht
*chan_ht
;
1450 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1452 /* Validate channel name */
1453 if (attr
->name
[0] == '.' ||
1454 memchr(attr
->name
, '/', len
) != NULL
) {
1455 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1459 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1464 * Don't try to enable a channel if the session has been started at
1465 * some point in time before. The tracer does not allow it.
1467 if (session
->has_been_started
) {
1468 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1473 * If the session is a live session, remove the switch timer, the
1474 * live timer does the same thing but sends also synchronisation
1475 * beacons for inactive streams.
1477 if (session
->live_timer
> 0) {
1478 attr
->attr
.live_timer_interval
= session
->live_timer
;
1479 attr
->attr
.switch_timer_interval
= 0;
1482 /* Check for feature support */
1483 switch (domain
->type
) {
1484 case LTTNG_DOMAIN_KERNEL
:
1486 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd
) != 1) {
1487 /* Sampling position of buffer is not supported */
1488 WARN("Kernel tracer does not support buffer monitoring. "
1489 "Setting the monitor interval timer to 0 "
1490 "(disabled) for channel '%s' of session '%s'",
1491 attr
-> name
, session
->name
);
1492 lttng_channel_set_monitor_timer_interval(attr
, 0);
1496 case LTTNG_DOMAIN_UST
:
1498 case LTTNG_DOMAIN_JUL
:
1499 case LTTNG_DOMAIN_LOG4J
:
1500 case LTTNG_DOMAIN_PYTHON
:
1501 if (!agent_tracing_is_enabled()) {
1502 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1503 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1508 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1512 switch (domain
->type
) {
1513 case LTTNG_DOMAIN_KERNEL
:
1515 struct ltt_kernel_channel
*kchan
;
1517 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1518 session
->kernel_session
);
1519 if (kchan
== NULL
) {
1520 if (session
->snapshot
.nb_output
> 0 ||
1521 session
->snapshot_mode
) {
1522 /* Enforce mmap output for snapshot sessions. */
1523 attr
->attr
.output
= LTTNG_EVENT_MMAP
;
1525 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1526 if (attr
->name
[0] != '\0') {
1527 session
->kernel_session
->has_non_default_channel
= 1;
1530 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1533 if (ret
!= LTTNG_OK
) {
1537 kernel_wait_quiescent(kernel_tracer_fd
);
1540 case LTTNG_DOMAIN_UST
:
1541 case LTTNG_DOMAIN_JUL
:
1542 case LTTNG_DOMAIN_LOG4J
:
1543 case LTTNG_DOMAIN_PYTHON
:
1545 struct ltt_ust_channel
*uchan
;
1550 * Current agent implementation limitations force us to allow
1551 * only one channel at once in "agent" subdomains. Each
1552 * subdomain has a default channel name which must be strictly
1555 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1556 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1557 LTTNG_SYMBOL_NAME_LEN
)) {
1558 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1561 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1562 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1563 LTTNG_SYMBOL_NAME_LEN
)) {
1564 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1567 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1568 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1569 LTTNG_SYMBOL_NAME_LEN
)) {
1570 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1575 chan_ht
= usess
->domain_global
.channels
;
1577 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1578 if (uchan
== NULL
) {
1579 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1580 if (attr
->name
[0] != '\0') {
1581 usess
->has_non_default_channel
= 1;
1584 ret
= channel_ust_enable(usess
, uchan
);
1589 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1593 if (ret
== LTTNG_OK
&& attr
->attr
.output
!= LTTNG_EVENT_MMAP
) {
1594 session
->has_non_mmap_channel
= true;
1603 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1605 int cmd_disable_event(struct ltt_session
*session
,
1606 enum lttng_domain_type domain
, char *channel_name
,
1607 struct lttng_event
*event
)
1612 DBG("Disable event command for event \'%s\'", event
->name
);
1614 event_name
= event
->name
;
1616 /* Error out on unhandled search criteria */
1617 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1618 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1619 ret
= LTTNG_ERR_UNK
;
1626 case LTTNG_DOMAIN_KERNEL
:
1628 struct ltt_kernel_channel
*kchan
;
1629 struct ltt_kernel_session
*ksess
;
1631 ksess
= session
->kernel_session
;
1634 * If a non-default channel has been created in the
1635 * session, explicitely require that -c chan_name needs
1638 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1639 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1643 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1644 if (kchan
== NULL
) {
1645 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1649 switch (event
->type
) {
1650 case LTTNG_EVENT_ALL
:
1651 case LTTNG_EVENT_TRACEPOINT
:
1652 case LTTNG_EVENT_SYSCALL
:
1653 case LTTNG_EVENT_PROBE
:
1654 case LTTNG_EVENT_FUNCTION
:
1655 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1656 if (event_name
[0] == '\0') {
1657 ret
= event_kernel_disable_event(kchan
,
1660 ret
= event_kernel_disable_event(kchan
,
1661 event_name
, event
->type
);
1663 if (ret
!= LTTNG_OK
) {
1668 ret
= LTTNG_ERR_UNK
;
1672 kernel_wait_quiescent(kernel_tracer_fd
);
1675 case LTTNG_DOMAIN_UST
:
1677 struct ltt_ust_channel
*uchan
;
1678 struct ltt_ust_session
*usess
;
1680 usess
= session
->ust_session
;
1682 if (validate_ust_event_name(event_name
)) {
1683 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1688 * If a non-default channel has been created in the
1689 * session, explicitly require that -c chan_name needs
1692 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1693 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1697 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1699 if (uchan
== NULL
) {
1700 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1704 switch (event
->type
) {
1705 case LTTNG_EVENT_ALL
:
1707 * An empty event name means that everything
1708 * should be disabled.
1710 if (event
->name
[0] == '\0') {
1711 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1713 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1716 if (ret
!= LTTNG_OK
) {
1721 ret
= LTTNG_ERR_UNK
;
1725 DBG3("Disable UST event %s in channel %s completed", event_name
,
1729 case LTTNG_DOMAIN_LOG4J
:
1730 case LTTNG_DOMAIN_JUL
:
1731 case LTTNG_DOMAIN_PYTHON
:
1734 struct ltt_ust_session
*usess
= session
->ust_session
;
1738 switch (event
->type
) {
1739 case LTTNG_EVENT_ALL
:
1742 ret
= LTTNG_ERR_UNK
;
1746 agt
= trace_ust_find_agent(usess
, domain
);
1748 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1752 * An empty event name means that everything
1753 * should be disabled.
1755 if (event
->name
[0] == '\0') {
1756 ret
= event_agent_disable_all(usess
, agt
);
1758 ret
= event_agent_disable(usess
, agt
, event_name
);
1760 if (ret
!= LTTNG_OK
) {
1767 ret
= LTTNG_ERR_UND
;
1780 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1782 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1783 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1785 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1786 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1789 * Don't try to add a context if the session has been started at
1790 * some point in time before. The tracer does not allow it and would
1791 * result in a corrupted trace.
1793 if (session
->has_been_started
) {
1794 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1798 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1799 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1800 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1804 case LTTNG_DOMAIN_KERNEL
:
1805 assert(session
->kernel_session
);
1807 if (session
->kernel_session
->channel_count
== 0) {
1808 /* Create default channel */
1809 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1810 if (ret
!= LTTNG_OK
) {
1813 chan_kern_created
= 1;
1815 /* Add kernel context to kernel tracer */
1816 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1817 if (ret
!= LTTNG_OK
) {
1821 case LTTNG_DOMAIN_JUL
:
1822 case LTTNG_DOMAIN_LOG4J
:
1825 * Validate channel name.
1826 * If no channel name is given and the domain is JUL or LOG4J,
1827 * set it to the appropriate domain-specific channel name. If
1828 * a name is provided but does not match the expexted channel
1829 * name, return an error.
1831 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1832 strcmp(channel_name
,
1833 DEFAULT_JUL_CHANNEL_NAME
)) {
1834 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1836 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1837 strcmp(channel_name
,
1838 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1839 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1842 /* break is _not_ missing here. */
1844 case LTTNG_DOMAIN_UST
:
1846 struct ltt_ust_session
*usess
= session
->ust_session
;
1847 unsigned int chan_count
;
1851 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1852 if (chan_count
== 0) {
1853 struct lttng_channel
*attr
;
1854 /* Create default channel */
1855 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1857 ret
= LTTNG_ERR_FATAL
;
1861 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1862 if (ret
!= LTTNG_OK
) {
1866 channel_attr_destroy(attr
);
1867 chan_ust_created
= 1;
1870 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1871 free(app_ctx_provider_name
);
1873 app_ctx_name
= NULL
;
1874 app_ctx_provider_name
= NULL
;
1875 if (ret
!= LTTNG_OK
) {
1881 ret
= LTTNG_ERR_UND
;
1889 if (chan_kern_created
) {
1890 struct ltt_kernel_channel
*kchan
=
1891 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1892 session
->kernel_session
);
1893 /* Created previously, this should NOT fail. */
1895 kernel_destroy_channel(kchan
);
1898 if (chan_ust_created
) {
1899 struct ltt_ust_channel
*uchan
=
1900 trace_ust_find_channel_by_name(
1901 session
->ust_session
->domain_global
.channels
,
1902 DEFAULT_CHANNEL_NAME
);
1903 /* Created previously, this should NOT fail. */
1905 /* Remove from the channel list of the session. */
1906 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1908 trace_ust_destroy_channel(uchan
);
1911 free(app_ctx_provider_name
);
1916 static inline bool name_starts_with(const char *name
, const char *prefix
)
1918 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1920 return !strncmp(name
, prefix
, max_cmp_len
);
1923 /* Perform userspace-specific event name validation */
1924 static int validate_ust_event_name(const char *name
)
1934 * Check name against all internal UST event component namespaces used
1937 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1938 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1939 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1948 * Internal version of cmd_enable_event() with a supplemental
1949 * "internal_event" flag which is used to enable internal events which should
1950 * be hidden from clients. Such events are used in the agent implementation to
1951 * enable the events through which all "agent" events are funeled.
1953 static int _cmd_enable_event(struct ltt_session
*session
,
1954 struct lttng_domain
*domain
,
1955 char *channel_name
, struct lttng_event
*event
,
1956 char *filter_expression
,
1957 struct lttng_filter_bytecode
*filter
,
1958 struct lttng_event_exclusion
*exclusion
,
1959 int wpipe
, bool internal_event
)
1961 int ret
= 0, channel_created
= 0;
1962 struct lttng_channel
*attr
= NULL
;
1966 assert(channel_name
);
1968 /* If we have a filter, we must have its filter expression */
1969 assert(!(!!filter_expression
^ !!filter
));
1971 /* Normalize event name as a globbing pattern */
1972 strutils_normalize_star_glob_pattern(event
->name
);
1974 /* Normalize exclusion names as globbing patterns */
1978 for (i
= 0; i
< exclusion
->count
; i
++) {
1979 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
1981 strutils_normalize_star_glob_pattern(name
);
1985 DBG("Enable event command for event \'%s\'", event
->name
);
1989 switch (domain
->type
) {
1990 case LTTNG_DOMAIN_KERNEL
:
1992 struct ltt_kernel_channel
*kchan
;
1995 * If a non-default channel has been created in the
1996 * session, explicitely require that -c chan_name needs
1999 if (session
->kernel_session
->has_non_default_channel
2000 && channel_name
[0] == '\0') {
2001 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2005 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2006 session
->kernel_session
);
2007 if (kchan
== NULL
) {
2008 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
2009 LTTNG_BUFFER_GLOBAL
);
2011 ret
= LTTNG_ERR_FATAL
;
2014 if (lttng_strncpy(attr
->name
, channel_name
,
2015 sizeof(attr
->name
))) {
2016 ret
= LTTNG_ERR_INVALID
;
2020 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2021 if (ret
!= LTTNG_OK
) {
2024 channel_created
= 1;
2027 /* Get the newly created kernel channel pointer */
2028 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2029 session
->kernel_session
);
2030 if (kchan
== NULL
) {
2031 /* This sould not happen... */
2032 ret
= LTTNG_ERR_FATAL
;
2036 switch (event
->type
) {
2037 case LTTNG_EVENT_ALL
:
2039 char *filter_expression_a
= NULL
;
2040 struct lttng_filter_bytecode
*filter_a
= NULL
;
2043 * We need to duplicate filter_expression and filter,
2044 * because ownership is passed to first enable
2047 if (filter_expression
) {
2048 filter_expression_a
= strdup(filter_expression
);
2049 if (!filter_expression_a
) {
2050 ret
= LTTNG_ERR_FATAL
;
2055 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
2057 free(filter_expression_a
);
2058 ret
= LTTNG_ERR_FATAL
;
2061 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
2063 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
2064 ret
= event_kernel_enable_event(kchan
, event
,
2065 filter_expression
, filter
);
2066 /* We have passed ownership */
2067 filter_expression
= NULL
;
2069 if (ret
!= LTTNG_OK
) {
2070 if (channel_created
) {
2071 /* Let's not leak a useless channel. */
2072 kernel_destroy_channel(kchan
);
2074 free(filter_expression_a
);
2078 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
2079 ret
= event_kernel_enable_event(kchan
, event
,
2080 filter_expression_a
, filter_a
);
2081 /* We have passed ownership */
2082 filter_expression_a
= NULL
;
2084 if (ret
!= LTTNG_OK
) {
2089 case LTTNG_EVENT_PROBE
:
2090 case LTTNG_EVENT_USERSPACE_PROBE
:
2091 case LTTNG_EVENT_FUNCTION
:
2092 case LTTNG_EVENT_FUNCTION_ENTRY
:
2093 case LTTNG_EVENT_TRACEPOINT
:
2094 ret
= event_kernel_enable_event(kchan
, event
,
2095 filter_expression
, filter
);
2096 /* We have passed ownership */
2097 filter_expression
= NULL
;
2099 if (ret
!= LTTNG_OK
) {
2100 if (channel_created
) {
2101 /* Let's not leak a useless channel. */
2102 kernel_destroy_channel(kchan
);
2107 case LTTNG_EVENT_SYSCALL
:
2108 ret
= event_kernel_enable_event(kchan
, event
,
2109 filter_expression
, filter
);
2110 /* We have passed ownership */
2111 filter_expression
= NULL
;
2113 if (ret
!= LTTNG_OK
) {
2118 ret
= LTTNG_ERR_UNK
;
2122 kernel_wait_quiescent(kernel_tracer_fd
);
2125 case LTTNG_DOMAIN_UST
:
2127 struct ltt_ust_channel
*uchan
;
2128 struct ltt_ust_session
*usess
= session
->ust_session
;
2133 * If a non-default channel has been created in the
2134 * session, explicitely require that -c chan_name needs
2137 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2138 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2142 /* Get channel from global UST domain */
2143 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2145 if (uchan
== NULL
) {
2146 /* Create default channel */
2147 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2148 usess
->buffer_type
);
2150 ret
= LTTNG_ERR_FATAL
;
2153 if (lttng_strncpy(attr
->name
, channel_name
,
2154 sizeof(attr
->name
))) {
2155 ret
= LTTNG_ERR_INVALID
;
2159 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2160 if (ret
!= LTTNG_OK
) {
2164 /* Get the newly created channel reference back */
2165 uchan
= trace_ust_find_channel_by_name(
2166 usess
->domain_global
.channels
, channel_name
);
2170 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2172 * Don't allow users to add UST events to channels which
2173 * are assigned to a userspace subdomain (JUL, Log4J,
2176 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2180 if (!internal_event
) {
2182 * Ensure the event name is not reserved for internal
2185 ret
= validate_ust_event_name(event
->name
);
2187 WARN("Userspace event name %s failed validation.",
2189 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2194 /* At this point, the session and channel exist on the tracer */
2195 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2196 filter_expression
, filter
, exclusion
,
2198 /* We have passed ownership */
2199 filter_expression
= NULL
;
2202 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2203 goto already_enabled
;
2204 } else if (ret
!= LTTNG_OK
) {
2209 case LTTNG_DOMAIN_LOG4J
:
2210 case LTTNG_DOMAIN_JUL
:
2211 case LTTNG_DOMAIN_PYTHON
:
2213 const char *default_event_name
, *default_chan_name
;
2215 struct lttng_event uevent
;
2216 struct lttng_domain tmp_dom
;
2217 struct ltt_ust_session
*usess
= session
->ust_session
;
2221 if (!agent_tracing_is_enabled()) {
2222 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2223 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2227 agt
= trace_ust_find_agent(usess
, domain
->type
);
2229 agt
= agent_create(domain
->type
);
2231 ret
= LTTNG_ERR_NOMEM
;
2234 agent_add(agt
, usess
->agents
);
2237 /* Create the default tracepoint. */
2238 memset(&uevent
, 0, sizeof(uevent
));
2239 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2240 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2241 default_event_name
= event_get_default_agent_ust_name(
2243 if (!default_event_name
) {
2244 ret
= LTTNG_ERR_FATAL
;
2247 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2248 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2251 * The domain type is changed because we are about to enable the
2252 * default channel and event for the JUL domain that are hardcoded.
2253 * This happens in the UST domain.
2255 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2256 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2258 switch (domain
->type
) {
2259 case LTTNG_DOMAIN_LOG4J
:
2260 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2262 case LTTNG_DOMAIN_JUL
:
2263 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2265 case LTTNG_DOMAIN_PYTHON
:
2266 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2269 /* The switch/case we are in makes this impossible */
2274 char *filter_expression_copy
= NULL
;
2275 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2278 const size_t filter_size
= sizeof(
2279 struct lttng_filter_bytecode
)
2282 filter_copy
= zmalloc(filter_size
);
2284 ret
= LTTNG_ERR_NOMEM
;
2287 memcpy(filter_copy
, filter
, filter_size
);
2289 filter_expression_copy
=
2290 strdup(filter_expression
);
2291 if (!filter_expression
) {
2292 ret
= LTTNG_ERR_NOMEM
;
2295 if (!filter_expression_copy
|| !filter_copy
) {
2296 free(filter_expression_copy
);
2302 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2303 (char *) default_chan_name
,
2304 &uevent
, filter_expression_copy
,
2305 filter_copy
, NULL
, wpipe
);
2308 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2309 goto already_enabled
;
2310 } else if (ret
!= LTTNG_OK
) {
2314 /* The wild card * means that everything should be enabled. */
2315 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2316 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2319 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2323 filter_expression
= NULL
;
2324 if (ret
!= LTTNG_OK
) {
2331 ret
= LTTNG_ERR_UND
;
2339 free(filter_expression
);
2342 channel_attr_destroy(attr
);
2348 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2349 * We own filter, exclusion, and filter_expression.
2351 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2352 char *channel_name
, struct lttng_event
*event
,
2353 char *filter_expression
,
2354 struct lttng_filter_bytecode
*filter
,
2355 struct lttng_event_exclusion
*exclusion
,
2358 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2359 filter_expression
, filter
, exclusion
, wpipe
, false);
2363 * Enable an event which is internal to LTTng. An internal should
2364 * never be made visible to clients and are immune to checks such as
2367 static int cmd_enable_event_internal(struct ltt_session
*session
,
2368 struct lttng_domain
*domain
,
2369 char *channel_name
, struct lttng_event
*event
,
2370 char *filter_expression
,
2371 struct lttng_filter_bytecode
*filter
,
2372 struct lttng_event_exclusion
*exclusion
,
2375 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2376 filter_expression
, filter
, exclusion
, wpipe
, true);
2380 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2382 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2383 struct lttng_event
**events
)
2386 ssize_t nb_events
= 0;
2389 case LTTNG_DOMAIN_KERNEL
:
2390 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2391 if (nb_events
< 0) {
2392 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2396 case LTTNG_DOMAIN_UST
:
2397 nb_events
= ust_app_list_events(events
);
2398 if (nb_events
< 0) {
2399 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2403 case LTTNG_DOMAIN_LOG4J
:
2404 case LTTNG_DOMAIN_JUL
:
2405 case LTTNG_DOMAIN_PYTHON
:
2406 nb_events
= agent_list_events(events
, domain
);
2407 if (nb_events
< 0) {
2408 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2413 ret
= LTTNG_ERR_UND
;
2420 /* Return negative value to differentiate return code */
2425 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2427 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2428 struct lttng_event_field
**fields
)
2431 ssize_t nb_fields
= 0;
2434 case LTTNG_DOMAIN_UST
:
2435 nb_fields
= ust_app_list_event_fields(fields
);
2436 if (nb_fields
< 0) {
2437 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2441 case LTTNG_DOMAIN_KERNEL
:
2442 default: /* fall-through */
2443 ret
= LTTNG_ERR_UND
;
2450 /* Return negative value to differentiate return code */
2454 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2456 return syscall_table_list(events
);
2460 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2462 * Called with session lock held.
2464 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2465 enum lttng_domain_type domain
, int32_t **pids
)
2468 ssize_t nr_pids
= 0;
2471 case LTTNG_DOMAIN_KERNEL
:
2473 struct ltt_kernel_session
*ksess
;
2475 ksess
= session
->kernel_session
;
2476 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2478 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2483 case LTTNG_DOMAIN_UST
:
2485 struct ltt_ust_session
*usess
;
2487 usess
= session
->ust_session
;
2488 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2490 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2495 case LTTNG_DOMAIN_LOG4J
:
2496 case LTTNG_DOMAIN_JUL
:
2497 case LTTNG_DOMAIN_PYTHON
:
2499 ret
= LTTNG_ERR_UND
;
2506 /* Return negative value to differentiate return code */
2511 int domain_mkdir(const struct consumer_output
*output
,
2512 const struct ltt_session
*session
,
2513 uid_t uid
, gid_t gid
)
2515 struct consumer_socket
*socket
;
2516 struct lttng_ht_iter iter
;
2518 char path
[LTTNG_PATH_MAX
];
2520 if (!output
|| !output
->socks
) {
2521 ERR("No consumer output found");
2526 ret
= snprintf(path
, sizeof(path
), "%s/%s%s",
2527 session_get_base_path(session
),
2529 output
->domain_subdir
);
2530 if (ret
< 0 || ret
>= LTTNG_PATH_MAX
) {
2531 ERR("Failed to format path new chunk domain path");
2536 DBG("Domain mkdir %s for session %" PRIu64
, path
, session
->id
);
2539 * We have to iterate to find a socket, but we only need to send the
2540 * rename command to one consumer, so we break after the first one.
2542 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
2543 pthread_mutex_lock(socket
->lock
);
2544 ret
= consumer_mkdir(socket
, session
->id
, output
, path
, uid
, gid
);
2545 pthread_mutex_unlock(socket
->lock
);
2547 ERR("Failed to create directory at \"%s\"", path
);
2563 int session_mkdir(const struct ltt_session
*session
)
2566 struct consumer_output
*output
;
2571 * Unsupported feature in lttng-relayd before 2.11, not an error since it
2572 * is only needed for session rotation and the user will get an error
2575 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
2576 session
->consumer
->relay_major_version
== 2 &&
2577 session
->consumer
->relay_minor_version
< 11) {
2582 if (session
->kernel_session
) {
2583 output
= session
->kernel_session
->consumer
;
2584 uid
= session
->kernel_session
->uid
;
2585 gid
= session
->kernel_session
->gid
;
2586 ret
= domain_mkdir(output
, session
, uid
, gid
);
2588 ERR("Mkdir kernel");
2593 if (session
->ust_session
) {
2594 output
= session
->ust_session
->consumer
;
2595 uid
= session
->ust_session
->uid
;
2596 gid
= session
->ust_session
->gid
;
2597 ret
= domain_mkdir(output
, session
, uid
, gid
);
2611 * Command LTTNG_START_TRACE processed by the client thread.
2613 * Called with session mutex held.
2615 int cmd_start_trace(struct ltt_session
*session
)
2618 unsigned long nb_chan
= 0;
2619 struct ltt_kernel_session
*ksession
;
2620 struct ltt_ust_session
*usess
;
2624 /* Ease our life a bit ;) */
2625 ksession
= session
->kernel_session
;
2626 usess
= session
->ust_session
;
2628 /* Is the session already started? */
2629 if (session
->active
) {
2630 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2635 * Starting a session without channel is useless since after that it's not
2636 * possible to enable channel thus inform the client.
2638 if (usess
&& usess
->domain_global
.channels
) {
2639 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2642 nb_chan
+= ksession
->channel_count
;
2645 ret
= LTTNG_ERR_NO_CHANNEL
;
2650 * Record the timestamp of the first time the session is started for
2651 * an eventual session rotation call.
2653 if (!session
->has_been_started
) {
2654 session
->current_chunk_start_ts
= time(NULL
);
2655 if (session
->current_chunk_start_ts
== (time_t) -1) {
2656 PERROR("Failed to retrieve the \"%s\" session's start time",
2658 ret
= LTTNG_ERR_FATAL
;
2661 if (!session
->snapshot_mode
&& session
->output_traces
) {
2662 ret
= session_mkdir(session
);
2664 ERR("Failed to create the session directories");
2665 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2671 /* Kernel tracing */
2672 if (ksession
!= NULL
) {
2673 DBG("Start kernel tracing session %s", session
->name
);
2674 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2675 if (ret
!= LTTNG_OK
) {
2680 /* Flag session that trace should start automatically */
2682 ret
= ust_app_start_trace_all(usess
);
2684 ret
= LTTNG_ERR_UST_START_FAIL
;
2689 /* Flag this after a successful start. */
2690 session
->has_been_started
= 1;
2691 session
->active
= 1;
2694 * Clear the flag that indicates that a rotation was done while the
2695 * session was stopped.
2697 session
->rotated_after_last_stop
= false;
2699 if (session
->rotate_timer_period
) {
2700 ret
= timer_session_rotation_schedule_timer_start(session
,
2701 session
->rotate_timer_period
);
2703 ERR("Failed to enable rotate timer");
2704 ret
= LTTNG_ERR_UNK
;
2716 * Command LTTNG_STOP_TRACE processed by the client thread.
2718 int cmd_stop_trace(struct ltt_session
*session
)
2721 struct ltt_kernel_channel
*kchan
;
2722 struct ltt_kernel_session
*ksession
;
2723 struct ltt_ust_session
*usess
;
2724 bool error_occurred
= false;
2728 DBG("Begin stop session %s (id %" PRIu64
")", session
->name
, session
->id
);
2730 ksession
= session
->kernel_session
;
2731 usess
= session
->ust_session
;
2733 /* Session is not active. Skip everythong and inform the client. */
2734 if (!session
->active
) {
2735 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2740 if (ksession
&& ksession
->active
) {
2741 DBG("Stop kernel tracing");
2743 ret
= kernel_stop_session(ksession
);
2745 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2749 kernel_wait_quiescent(kernel_tracer_fd
);
2751 /* Flush metadata after stopping (if exists) */
2752 if (ksession
->metadata_stream_fd
>= 0) {
2753 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2755 ERR("Kernel metadata flush failed");
2759 /* Flush all buffers after stopping */
2760 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2761 ret
= kernel_flush_buffer(kchan
);
2763 ERR("Kernel flush buffer error");
2767 ksession
->active
= 0;
2768 DBG("Kernel session stopped %s (id %" PRIu64
")", session
->name
,
2772 if (usess
&& usess
->active
) {
2773 ret
= ust_app_stop_trace_all(usess
);
2775 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2780 /* Flag inactive after a successful stop. */
2781 session
->active
= 0;
2782 ret
= !error_occurred
? LTTNG_OK
: LTTNG_ERR_UNK
;
2789 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2791 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2792 struct lttng_uri
*uris
)
2795 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2796 struct ltt_ust_session
*usess
= session
->ust_session
;
2802 /* Can't set consumer URI if the session is active. */
2803 if (session
->active
) {
2804 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2808 /* Set the "global" consumer URIs */
2809 for (i
= 0; i
< nb_uri
; i
++) {
2810 ret
= add_uri_to_consumer(session
,
2812 &uris
[i
], LTTNG_DOMAIN_NONE
);
2813 if (ret
!= LTTNG_OK
) {
2818 /* Set UST session URIs */
2819 if (session
->ust_session
) {
2820 for (i
= 0; i
< nb_uri
; i
++) {
2821 ret
= add_uri_to_consumer(session
,
2822 session
->ust_session
->consumer
,
2823 &uris
[i
], LTTNG_DOMAIN_UST
);
2824 if (ret
!= LTTNG_OK
) {
2830 /* Set kernel session URIs */
2831 if (session
->kernel_session
) {
2832 for (i
= 0; i
< nb_uri
; i
++) {
2833 ret
= add_uri_to_consumer(session
,
2834 session
->kernel_session
->consumer
,
2835 &uris
[i
], LTTNG_DOMAIN_KERNEL
);
2836 if (ret
!= LTTNG_OK
) {
2843 * Make sure to set the session in output mode after we set URI since a
2844 * session can be created without URL (thus flagged in no output mode).
2846 session
->output_traces
= 1;
2848 ksess
->output_traces
= 1;
2852 usess
->output_traces
= 1;
2863 enum lttng_error_code
set_session_output_from_descriptor(
2864 struct ltt_session
*session
,
2865 const struct lttng_session_descriptor
*descriptor
)
2868 enum lttng_error_code ret_code
= LTTNG_OK
;
2869 enum lttng_session_descriptor_type session_type
=
2870 lttng_session_descriptor_get_type(descriptor
);
2871 enum lttng_session_descriptor_output_type output_type
=
2872 lttng_session_descriptor_get_output_type(descriptor
);
2873 struct lttng_uri uris
[2] = {};
2874 size_t uri_count
= 0;
2876 switch (output_type
) {
2877 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE
:
2879 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL
:
2880 lttng_session_descriptor_get_local_output_uri(descriptor
,
2884 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK
:
2885 lttng_session_descriptor_get_network_output_uris(descriptor
,
2886 &uris
[0], &uris
[1]);
2890 ret_code
= LTTNG_ERR_INVALID
;
2894 switch (session_type
) {
2895 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
2897 struct snapshot_output
*new_output
= NULL
;
2899 new_output
= snapshot_output_alloc();
2901 ret_code
= LTTNG_ERR_NOMEM
;
2905 ret
= snapshot_output_init_with_uri(session
,
2906 DEFAULT_SNAPSHOT_MAX_SIZE
,
2907 NULL
, uris
, uri_count
, session
->consumer
,
2908 new_output
, &session
->snapshot
);
2910 ret_code
= (ret
== -ENOMEM
) ?
2911 LTTNG_ERR_NOMEM
: LTTNG_ERR_INVALID
;
2912 snapshot_output_destroy(new_output
);
2915 snapshot_add_output(&session
->snapshot
, new_output
);
2918 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR
:
2919 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
2921 ret_code
= cmd_set_consumer_uri(session
, uri_count
, uris
);
2925 ret_code
= LTTNG_ERR_INVALID
;
2933 enum lttng_error_code
cmd_create_session_from_descriptor(
2934 struct lttng_session_descriptor
*descriptor
,
2935 const lttng_sock_cred
*creds
,
2936 const char *home_path
)
2939 enum lttng_error_code ret_code
;
2940 const char *session_name
;
2941 struct ltt_session
*new_session
= NULL
;
2942 enum lttng_session_descriptor_status descriptor_status
;
2944 session_lock_list();
2946 if (*home_path
!= '/') {
2947 ERR("Home path provided by client is not absolute");
2948 ret_code
= LTTNG_ERR_INVALID
;
2953 descriptor_status
= lttng_session_descriptor_get_session_name(
2954 descriptor
, &session_name
);
2955 switch (descriptor_status
) {
2956 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK
:
2958 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET
:
2959 session_name
= NULL
;
2962 ret_code
= LTTNG_ERR_INVALID
;
2965 ret_code
= session_create(session_name
, creds
->uid
, creds
->gid
,
2967 if (ret_code
!= LTTNG_OK
) {
2971 if (!session_name
) {
2972 ret
= lttng_session_descriptor_set_session_name(descriptor
,
2975 ret_code
= LTTNG_ERR_SESSION_FAIL
;
2980 if (!lttng_session_descriptor_is_output_destination_initialized(
2983 * Only include the session's creation time in the output
2984 * destination if the name of the session itself was
2985 * not auto-generated.
2987 ret_code
= lttng_session_descriptor_set_default_output(
2989 session_name
? &new_session
->creation_time
: NULL
,
2991 if (ret_code
!= LTTNG_OK
) {
2995 new_session
->has_user_specified_directory
=
2996 lttng_session_descriptor_has_output_directory(
3000 switch (lttng_session_descriptor_get_type(descriptor
)) {
3001 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT
:
3002 new_session
->snapshot_mode
= 1;
3004 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE
:
3005 new_session
->live_timer
=
3006 lttng_session_descriptor_live_get_timer_interval(
3013 ret_code
= set_session_output_from_descriptor(new_session
, descriptor
);
3014 if (ret_code
!= LTTNG_OK
) {
3017 new_session
->consumer
->enabled
= 1;
3018 ret_code
= LTTNG_OK
;
3020 /* Release reference provided by the session_create function. */
3021 session_put(new_session
);
3022 if (ret_code
!= LTTNG_OK
&& new_session
) {
3023 /* Release the global reference on error. */
3024 session_destroy(new_session
);
3026 session_unlock_list();
3030 enum lttng_error_code
cmd_create_session(struct command_ctx
*cmd_ctx
, int sock
,
3031 struct lttng_session_descriptor
**return_descriptor
)
3034 size_t payload_size
;
3035 struct lttng_dynamic_buffer payload
;
3036 struct lttng_buffer_view home_dir_view
;
3037 struct lttng_buffer_view session_descriptor_view
;
3038 struct lttng_session_descriptor
*session_descriptor
= NULL
;
3039 enum lttng_error_code ret_code
;
3041 lttng_dynamic_buffer_init(&payload
);
3042 if (cmd_ctx
->lsm
->u
.create_session
.home_dir_size
>=
3044 ret_code
= LTTNG_ERR_INVALID
;
3047 if (cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
>
3048 LTTNG_SESSION_DESCRIPTOR_MAX_LEN
) {
3049 ret_code
= LTTNG_ERR_INVALID
;
3053 payload_size
= cmd_ctx
->lsm
->u
.create_session
.home_dir_size
+
3054 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
;
3055 ret
= lttng_dynamic_buffer_set_size(&payload
, payload_size
);
3057 ret_code
= LTTNG_ERR_NOMEM
;
3061 ret
= lttcomm_recv_unix_sock(sock
, payload
.data
, payload
.size
);
3063 ERR("Reception of session descriptor failed, aborting.");
3064 ret_code
= LTTNG_ERR_SESSION_FAIL
;
3068 home_dir_view
= lttng_buffer_view_from_dynamic_buffer(
3071 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
);
3072 session_descriptor_view
= lttng_buffer_view_from_dynamic_buffer(
3074 cmd_ctx
->lsm
->u
.create_session
.home_dir_size
,
3075 cmd_ctx
->lsm
->u
.create_session
.session_descriptor_size
);
3077 ret
= lttng_session_descriptor_create_from_buffer(
3078 &session_descriptor_view
, &session_descriptor
);
3080 ERR("Failed to create session descriptor from payload of \"create session\" command");
3081 ret_code
= LTTNG_ERR_INVALID
;
3086 * Sets the descriptor's auto-generated properties (name, output) if
3089 ret_code
= cmd_create_session_from_descriptor(session_descriptor
,
3091 home_dir_view
.size
? home_dir_view
.data
: NULL
);
3092 if (ret_code
!= LTTNG_OK
) {
3096 ret_code
= LTTNG_OK
;
3097 *return_descriptor
= session_descriptor
;
3098 session_descriptor
= NULL
;
3100 lttng_dynamic_buffer_reset(&payload
);
3101 lttng_session_descriptor_destroy(session_descriptor
);
3106 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3108 * Called with session lock held.
3110 int cmd_destroy_session(struct ltt_session
*session
,
3111 struct notification_thread_handle
*notification_thread_handle
)
3118 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
3120 if (session
->rotation_schedule_timer_enabled
) {
3121 if (timer_session_rotation_schedule_timer_stop(
3123 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3128 if (session
->rotate_size
) {
3129 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3130 session
->rotate_size
= 0;
3133 if (session
->current_archive_id
!= 0) {
3134 if (!session
->rotated_after_last_stop
) {
3135 ret
= cmd_rotate_session(session
, NULL
);
3136 if (ret
!= LTTNG_OK
) {
3137 ERR("Failed to perform an implicit rotation as part of the rotation: %s", lttng_strerror(-ret
));
3141 * Rename the active chunk to ensure it has a name
3142 * of the form ts_begin-ts_end-id.
3144 * Note that no trace data has been produced since
3145 * the last rotation; the directory should be
3148 ret
= rename_active_chunk(session
);
3150 ERR("Failed to rename active chunk during the destruction of session \"%s\"",
3156 if (session
->shm_path
[0]) {
3158 * When a session is created with an explicit shm_path,
3159 * the consumer daemon will create its shared memory files
3160 * at that location and will *not* unlink them. This is normal
3161 * as the intention of that feature is to make it possible
3162 * to retrieve the content of those files should a crash occur.
3164 * To ensure the content of those files can be used, the
3165 * sessiond daemon will replicate the content of the metadata
3166 * cache in a metadata file.
3168 * On clean-up, it is expected that the consumer daemon will
3169 * unlink the shared memory files and that the session daemon
3170 * will unlink the metadata file. Then, the session's directory
3171 * in the shm path can be removed.
3173 * Unfortunately, a flaw in the design of the sessiond's and
3174 * consumerd's tear down of channels makes it impossible to
3175 * determine when the sessiond _and_ the consumerd have both
3176 * destroyed their representation of a channel. For one, the
3177 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3178 * callbacks in both daemons.
3180 * However, it is also impossible for the sessiond to know when
3181 * the consumer daemon is done destroying its channel(s) since
3182 * it occurs as a reaction to the closing of the channel's file
3183 * descriptor. There is no resulting communication initiated
3184 * from the consumerd to the sessiond to confirm that the
3185 * operation is completed (and was successful).
3187 * Until this is all fixed, the session daemon checks for the
3188 * removal of the session's shm path which makes it possible
3189 * to safely advertise a session as having been destroyed.
3191 * Prior to this fix, it was not possible to reliably save
3192 * a session making use of the --shm-path option, destroy it,
3193 * and load it again. This is because the creation of the
3194 * session would fail upon seeing the session's shm path
3195 * already in existence.
3197 * Note that none of the error paths in the check for the
3198 * directory's existence return an error. This is normal
3199 * as there isn't much that can be done. The session will
3200 * be destroyed properly, except that we can't offer the
3201 * guarantee that the same session can be re-created.
3203 current_completion_handler
= &destroy_completion_handler
.handler
;
3204 ret
= lttng_strncpy(destroy_completion_handler
.shm_path
,
3206 sizeof(destroy_completion_handler
.shm_path
));
3211 * The session is destroyed. However, note that the command context
3212 * still holds a reference to the session, thus delaying its destruction
3213 * _at least_ up to the point when that reference is released.
3215 session_destroy(session
);
3222 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3224 int cmd_register_consumer(struct ltt_session
*session
,
3225 enum lttng_domain_type domain
, const char *sock_path
,
3226 struct consumer_data
*cdata
)
3229 struct consumer_socket
*socket
= NULL
;
3236 case LTTNG_DOMAIN_KERNEL
:
3238 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3242 /* Can't register a consumer if there is already one */
3243 if (ksess
->consumer_fds_sent
!= 0) {
3244 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3248 sock
= lttcomm_connect_unix_sock(sock_path
);
3250 ret
= LTTNG_ERR_CONNECT_FAIL
;
3253 cdata
->cmd_sock
= sock
;
3255 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3256 if (socket
== NULL
) {
3259 PERROR("close register consumer");
3261 cdata
->cmd_sock
= -1;
3262 ret
= LTTNG_ERR_FATAL
;
3266 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3267 if (socket
->lock
== NULL
) {
3268 PERROR("zmalloc pthread mutex");
3269 ret
= LTTNG_ERR_FATAL
;
3272 pthread_mutex_init(socket
->lock
, NULL
);
3273 socket
->registered
= 1;
3276 consumer_add_socket(socket
, ksess
->consumer
);
3279 pthread_mutex_lock(&cdata
->pid_mutex
);
3281 pthread_mutex_unlock(&cdata
->pid_mutex
);
3286 /* TODO: Userspace tracing */
3287 ret
= LTTNG_ERR_UND
;
3295 consumer_destroy_socket(socket
);
3301 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3303 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3304 struct lttng_domain
**domains
)
3309 struct lttng_ht_iter iter
;
3311 if (session
->kernel_session
!= NULL
) {
3312 DBG3("Listing domains found kernel domain");
3316 if (session
->ust_session
!= NULL
) {
3317 DBG3("Listing domains found UST global domain");
3321 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3323 if (agt
->being_used
) {
3334 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3335 if (*domains
== NULL
) {
3336 ret
= LTTNG_ERR_FATAL
;
3340 if (session
->kernel_session
!= NULL
) {
3341 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3343 /* Kernel session buffer type is always GLOBAL */
3344 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3349 if (session
->ust_session
!= NULL
) {
3350 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3351 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3355 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3357 if (agt
->being_used
) {
3358 (*domains
)[index
].type
= agt
->domain
;
3359 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3369 /* Return negative value to differentiate return code */
3375 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3377 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3378 struct ltt_session
*session
, struct lttng_channel
**channels
)
3380 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3383 case LTTNG_DOMAIN_KERNEL
:
3384 if (session
->kernel_session
!= NULL
) {
3385 nb_chan
= session
->kernel_session
->channel_count
;
3387 DBG3("Number of kernel channels %zd", nb_chan
);
3389 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3393 case LTTNG_DOMAIN_UST
:
3394 if (session
->ust_session
!= NULL
) {
3396 nb_chan
= lttng_ht_get_count(
3397 session
->ust_session
->domain_global
.channels
);
3400 DBG3("Number of UST global channels %zd", nb_chan
);
3402 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3407 ret
= -LTTNG_ERR_UND
;
3412 const size_t channel_size
= sizeof(struct lttng_channel
) +
3413 sizeof(struct lttng_channel_extended
);
3414 struct lttng_channel_extended
*channel_exts
;
3416 payload_size
= nb_chan
* channel_size
;
3417 *channels
= zmalloc(payload_size
);
3418 if (*channels
== NULL
) {
3419 ret
= -LTTNG_ERR_FATAL
;
3423 channel_exts
= ((void *) *channels
) +
3424 (nb_chan
* sizeof(struct lttng_channel
));
3425 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3426 if (ret
!= LTTNG_OK
) {
3441 * Command LTTNG_LIST_EVENTS processed by the client thread.
3443 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3444 struct ltt_session
*session
, char *channel_name
,
3445 struct lttng_event
**events
, size_t *total_size
)
3448 ssize_t nb_event
= 0;
3451 case LTTNG_DOMAIN_KERNEL
:
3452 if (session
->kernel_session
!= NULL
) {
3453 nb_event
= list_lttng_kernel_events(channel_name
,
3454 session
->kernel_session
, events
,
3458 case LTTNG_DOMAIN_UST
:
3460 if (session
->ust_session
!= NULL
) {
3461 nb_event
= list_lttng_ust_global_events(channel_name
,
3462 &session
->ust_session
->domain_global
, events
,
3467 case LTTNG_DOMAIN_LOG4J
:
3468 case LTTNG_DOMAIN_JUL
:
3469 case LTTNG_DOMAIN_PYTHON
:
3470 if (session
->ust_session
) {
3471 struct lttng_ht_iter iter
;
3475 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3476 &iter
.iter
, agt
, node
.node
) {
3477 if (agt
->domain
== domain
) {
3478 nb_event
= list_lttng_agent_events(
3488 ret
= LTTNG_ERR_UND
;
3495 /* Return negative value to differentiate return code */
3500 * Using the session list, filled a lttng_session array to send back to the
3501 * client for session listing.
3503 * The session list lock MUST be acquired before calling this function. Use
3504 * session_lock_list() and session_unlock_list().
3506 void cmd_list_lttng_sessions(struct lttng_session
*sessions
,
3507 size_t session_count
, uid_t uid
, gid_t gid
)
3511 struct ltt_session
*session
;
3512 struct ltt_session_list
*list
= session_get_list();
3513 struct lttng_session_extended
*extended
=
3514 (typeof(extended
)) (&sessions
[session_count
]);
3516 DBG("Getting all available session for UID %d GID %d",
3519 * Iterate over session list and append data after the control struct in
3522 cds_list_for_each_entry(session
, &list
->head
, list
) {
3523 if (!session_get(session
)) {
3527 * Only list the sessions the user can control.
3529 if (!session_access_ok(session
, uid
, gid
) ||
3530 session
->destroyed
) {
3531 session_put(session
);
3535 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3536 struct ltt_ust_session
*usess
= session
->ust_session
;
3538 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3539 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3540 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3541 ret
= build_network_session_path(sessions
[i
].path
,
3542 sizeof(sessions
[i
].path
), session
);
3544 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3545 session
->consumer
->dst
.session_root_path
);
3548 PERROR("snprintf session path");
3549 session_put(session
);
3553 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3554 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3555 sessions
[i
].enabled
= session
->active
;
3556 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3557 sessions
[i
].live_timer_interval
= session
->live_timer
;
3558 extended
[i
].creation_time
.value
= (uint64_t) session
->creation_time
;
3559 extended
[i
].creation_time
.is_set
= 1;
3561 session_put(session
);
3566 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3567 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3569 int cmd_data_pending(struct ltt_session
*session
)
3572 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3573 struct ltt_ust_session
*usess
= session
->ust_session
;
3577 DBG("Data pending for session %s", session
->name
);
3579 /* Session MUST be stopped to ask for data availability. */
3580 if (session
->active
) {
3581 ret
= LTTNG_ERR_SESSION_STARTED
;
3585 * If stopped, just make sure we've started before else the above call
3586 * will always send that there is data pending.
3588 * The consumer assumes that when the data pending command is received,
3589 * the trace has been started before or else no output data is written
3590 * by the streams which is a condition for data pending. So, this is
3591 * *VERY* important that we don't ask the consumer before a start
3594 if (!session
->has_been_started
) {
3600 /* A rotation is still pending, we have to wait. */
3601 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
3602 DBG("Rotate still pending for session %s", session
->name
);
3607 if (ksess
&& ksess
->consumer
) {
3608 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3610 /* Data is still being extracted for the kernel. */
3615 if (usess
&& usess
->consumer
) {
3616 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3618 /* Data is still being extracted for the kernel. */
3623 /* Data is ready to be read by a viewer */
3631 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3633 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3635 int cmd_snapshot_add_output(struct ltt_session
*session
,
3636 struct lttng_snapshot_output
*output
, uint32_t *id
)
3639 struct snapshot_output
*new_output
;
3644 DBG("Cmd snapshot add output for session %s", session
->name
);
3647 * Can't create an output if the session is not set in no-output mode.
3649 if (session
->output_traces
) {
3650 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3654 if (session
->has_non_mmap_channel
) {
3655 ret
= LTTNG_ERR_SNAPSHOT_UNSUPPORTED
;
3659 /* Only one output is allowed until we have the "tee" feature. */
3660 if (session
->snapshot
.nb_output
== 1) {
3661 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3665 new_output
= snapshot_output_alloc();
3667 ret
= LTTNG_ERR_NOMEM
;
3671 ret
= snapshot_output_init(session
, output
->max_size
, output
->name
,
3672 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3673 &session
->snapshot
);
3675 if (ret
== -ENOMEM
) {
3676 ret
= LTTNG_ERR_NOMEM
;
3678 ret
= LTTNG_ERR_INVALID
;
3684 snapshot_add_output(&session
->snapshot
, new_output
);
3686 *id
= new_output
->id
;
3693 snapshot_output_destroy(new_output
);
3699 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3701 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3703 int cmd_snapshot_del_output(struct ltt_session
*session
,
3704 struct lttng_snapshot_output
*output
)
3707 struct snapshot_output
*sout
= NULL
;
3715 * Permission denied to create an output if the session is not
3716 * set in no output mode.
3718 if (session
->output_traces
) {
3719 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3724 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3726 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3727 } else if (*output
->name
!= '\0') {
3728 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3730 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3733 ret
= LTTNG_ERR_INVALID
;
3737 snapshot_delete_output(&session
->snapshot
, sout
);
3738 snapshot_output_destroy(sout
);
3747 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3749 * If no output is available, outputs is untouched and 0 is returned.
3751 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3753 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3754 struct lttng_snapshot_output
**outputs
)
3757 struct lttng_snapshot_output
*list
= NULL
;
3758 struct lttng_ht_iter iter
;
3759 struct snapshot_output
*output
;
3764 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3767 * Permission denied to create an output if the session is not
3768 * set in no output mode.
3770 if (session
->output_traces
) {
3771 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3775 if (session
->snapshot
.nb_output
== 0) {
3780 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3782 ret
= -LTTNG_ERR_NOMEM
;
3786 /* Copy list from session to the new list object. */
3788 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3789 output
, node
.node
) {
3790 assert(output
->consumer
);
3791 list
[idx
].id
= output
->id
;
3792 list
[idx
].max_size
= output
->max_size
;
3793 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3794 sizeof(list
[idx
].name
))) {
3795 ret
= -LTTNG_ERR_INVALID
;
3798 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3799 if (lttng_strncpy(list
[idx
].ctrl_url
,
3800 output
->consumer
->dst
.session_root_path
,
3801 sizeof(list
[idx
].ctrl_url
))) {
3802 ret
= -LTTNG_ERR_INVALID
;
3807 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3808 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3810 ret
= -LTTNG_ERR_NOMEM
;
3815 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3816 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3818 ret
= -LTTNG_ERR_NOMEM
;
3827 ret
= session
->snapshot
.nb_output
;
3836 * Check if we can regenerate the metadata for this session.
3837 * Only kernel, UST per-uid and non-live sessions are supported.
3839 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3842 int check_regenerate_metadata_support(struct ltt_session
*session
)
3848 if (session
->live_timer
!= 0) {
3849 ret
= LTTNG_ERR_LIVE_SESSION
;
3852 if (!session
->active
) {
3853 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3856 if (session
->ust_session
) {
3857 switch (session
->ust_session
->buffer_type
) {
3858 case LTTNG_BUFFER_PER_UID
:
3860 case LTTNG_BUFFER_PER_PID
:
3861 ret
= LTTNG_ERR_PER_PID_SESSION
;
3865 ret
= LTTNG_ERR_UNK
;
3869 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3870 session
->consumer
->relay_minor_version
< 8) {
3871 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3881 int clear_metadata_file(int fd
)
3886 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
3887 if (lseek_ret
< 0) {
3893 ret
= ftruncate(fd
, 0);
3895 PERROR("ftruncate");
3904 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3907 struct buffer_reg_uid
*uid_reg
= NULL
;
3908 struct buffer_reg_session
*session_reg
= NULL
;
3911 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3912 struct ust_registry_session
*registry
;
3913 struct ust_registry_channel
*chan
;
3914 struct lttng_ht_iter iter_chan
;
3916 session_reg
= uid_reg
->registry
;
3917 registry
= session_reg
->reg
.ust
;
3919 pthread_mutex_lock(®istry
->lock
);
3920 registry
->metadata_len_sent
= 0;
3921 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3922 registry
->metadata_len
= 0;
3923 registry
->metadata_version
++;
3924 if (registry
->metadata_fd
> 0) {
3925 /* Clear the metadata file's content. */
3926 ret
= clear_metadata_file(registry
->metadata_fd
);
3928 pthread_mutex_unlock(®istry
->lock
);
3933 ret
= ust_metadata_session_statedump(registry
, NULL
,
3934 registry
->major
, registry
->minor
);
3936 pthread_mutex_unlock(®istry
->lock
);
3937 ERR("Failed to generate session metadata (err = %d)",
3941 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3943 struct ust_registry_event
*event
;
3944 struct lttng_ht_iter iter_event
;
3946 ret
= ust_metadata_channel_statedump(registry
, chan
);
3948 pthread_mutex_unlock(®istry
->lock
);
3949 ERR("Failed to generate channel metadata "
3953 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3955 ret
= ust_metadata_event_statedump(registry
,
3958 pthread_mutex_unlock(®istry
->lock
);
3959 ERR("Failed to generate event metadata "
3965 pthread_mutex_unlock(®istry
->lock
);
3974 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3976 * Ask the consumer to truncate the existing metadata file(s) and
3977 * then regenerate the metadata. Live and per-pid sessions are not
3978 * supported and return an error.
3980 * Return 0 on success or else a LTTNG_ERR code.
3982 int cmd_regenerate_metadata(struct ltt_session
*session
)
3988 ret
= check_regenerate_metadata_support(session
);
3993 if (session
->kernel_session
) {
3994 ret
= kernctl_session_regenerate_metadata(
3995 session
->kernel_session
->fd
);
3997 ERR("Failed to regenerate the kernel metadata");
4002 if (session
->ust_session
) {
4003 ret
= ust_regenerate_metadata(session
->ust_session
);
4005 ERR("Failed to regenerate the UST metadata");
4009 DBG("Cmd metadata regenerate for session %s", session
->name
);
4017 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4019 * Ask the tracer to regenerate a new statedump.
4021 * Return 0 on success or else a LTTNG_ERR code.
4023 int cmd_regenerate_statedump(struct ltt_session
*session
)
4029 if (!session
->active
) {
4030 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
4034 if (session
->kernel_session
) {
4035 ret
= kernctl_session_regenerate_statedump(
4036 session
->kernel_session
->fd
);
4038 * Currently, the statedump in kernel can only fail if out
4042 if (ret
== -ENOMEM
) {
4043 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
4045 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4047 ERR("Failed to regenerate the kernel statedump");
4052 if (session
->ust_session
) {
4053 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
4055 * Currently, the statedump in UST always returns 0.
4058 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
4059 ERR("Failed to regenerate the UST statedump");
4063 DBG("Cmd regenerate statedump for session %s", session
->name
);
4070 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4071 struct notification_thread_handle
*notification_thread
)
4075 ssize_t sock_recv_len
;
4076 struct lttng_trigger
*trigger
= NULL
;
4077 struct lttng_buffer_view view
;
4078 struct lttng_dynamic_buffer trigger_buffer
;
4080 lttng_dynamic_buffer_init(&trigger_buffer
);
4081 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4082 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4084 ret
= LTTNG_ERR_NOMEM
;
4088 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4090 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4091 ERR("Failed to receive \"register trigger\" command payload");
4092 /* TODO: should this be a new error enum ? */
4093 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4097 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4098 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4100 ERR("Invalid trigger payload received in \"register trigger\" command");
4101 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4105 ret
= notification_thread_command_register_trigger(notification_thread
,
4107 /* Ownership of trigger was transferred. */
4110 lttng_trigger_destroy(trigger
);
4111 lttng_dynamic_buffer_reset(&trigger_buffer
);
4115 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
4116 struct notification_thread_handle
*notification_thread
)
4120 ssize_t sock_recv_len
;
4121 struct lttng_trigger
*trigger
= NULL
;
4122 struct lttng_buffer_view view
;
4123 struct lttng_dynamic_buffer trigger_buffer
;
4125 lttng_dynamic_buffer_init(&trigger_buffer
);
4126 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
4127 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
4129 ret
= LTTNG_ERR_NOMEM
;
4133 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
4135 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
4136 ERR("Failed to receive \"unregister trigger\" command payload");
4137 /* TODO: should this be a new error enum ? */
4138 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4142 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
4143 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
4145 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4146 ret
= LTTNG_ERR_INVALID_TRIGGER
;
4150 ret
= notification_thread_command_unregister_trigger(notification_thread
,
4153 lttng_trigger_destroy(trigger
);
4154 lttng_dynamic_buffer_reset(&trigger_buffer
);
4159 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4160 * snapshot output is *not* set with a remote destination.
4162 * Return LTTNG_OK on success or a LTTNG_ERR code.
4164 static enum lttng_error_code
set_relayd_for_snapshot(
4165 struct consumer_output
*consumer
,
4166 struct snapshot_output
*snap_output
,
4167 struct ltt_session
*session
)
4169 enum lttng_error_code status
= LTTNG_OK
;
4170 struct lttng_ht_iter iter
;
4171 struct consumer_socket
*socket
;
4174 assert(snap_output
);
4177 DBG2("Set relayd object from snapshot output");
4179 /* Ignore if snapshot consumer output is not network. */
4180 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
4185 * For each consumer socket, create and send the relayd object of the
4189 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
4190 socket
, node
.node
) {
4191 pthread_mutex_lock(socket
->lock
);
4192 status
= send_consumer_relayd_sockets(0, session
->id
,
4193 snap_output
->consumer
, socket
,
4194 session
->name
, session
->hostname
,
4195 session
->live_timer
);
4196 pthread_mutex_unlock(socket
->lock
);
4197 if (status
!= LTTNG_OK
) {
4209 * Record a kernel snapshot.
4211 * Return LTTNG_OK on success or a LTTNG_ERR code.
4213 static enum lttng_error_code
record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
4214 struct snapshot_output
*output
, struct ltt_session
*session
,
4215 int wait
, uint64_t nb_packets_per_stream
)
4218 enum lttng_error_code status
;
4225 * Copy kernel session sockets so we can communicate with the right
4226 * consumer for the snapshot record command.
4228 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
4230 status
= LTTNG_ERR_NOMEM
;
4234 status
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
4235 if (status
!= LTTNG_OK
) {
4236 goto error_snapshot
;
4239 status
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
4240 if (status
!= LTTNG_OK
) {
4241 goto error_snapshot
;
4247 /* Clean up copied sockets so this output can use some other later on. */
4248 consumer_destroy_output_sockets(output
->consumer
);
4255 * Record a UST snapshot.
4257 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4259 static enum lttng_error_code
record_ust_snapshot(struct ltt_ust_session
*usess
,
4260 struct snapshot_output
*output
, struct ltt_session
*session
,
4261 int wait
, uint64_t nb_packets_per_stream
)
4264 enum lttng_error_code status
;
4271 * Copy UST session sockets so we can communicate with the right
4272 * consumer for the snapshot record command.
4274 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
4276 status
= LTTNG_ERR_NOMEM
;
4280 status
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
4281 if (status
!= LTTNG_OK
) {
4282 goto error_snapshot
;
4285 status
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
4286 if (status
!= LTTNG_OK
) {
4287 goto error_snapshot
;
4293 /* Clean up copied sockets so this output can use some other later on. */
4294 consumer_destroy_output_sockets(output
->consumer
);
4301 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
4302 uint64_t cur_nr_packets
)
4304 uint64_t tot_size
= 0;
4306 if (session
->kernel_session
) {
4307 struct ltt_kernel_channel
*chan
;
4308 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4310 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4311 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4313 * Don't take channel into account if we
4314 * already grab all its packets.
4318 tot_size
+= chan
->channel
->attr
.subbuf_size
4319 * chan
->stream_count
;
4323 if (session
->ust_session
) {
4324 struct ltt_ust_session
*usess
= session
->ust_session
;
4326 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4334 * Calculate the number of packets we can grab from each stream that
4335 * fits within the overall snapshot max size.
4337 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4338 * the number of packets per stream.
4340 * TODO: this approach is not perfect: we consider the worse case
4341 * (packet filling the sub-buffers) as an upper bound, but we could do
4342 * better if we do this calculation while we actually grab the packet
4343 * content: we would know how much padding we don't actually store into
4346 * This algorithm is currently bounded by the number of packets per
4349 * Since we call this algorithm before actually grabbing the data, it's
4350 * an approximation: for instance, applications could appear/disappear
4351 * in between this call and actually grabbing data.
4354 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
4357 uint64_t cur_nb_packets
= 0;
4360 return 0; /* Infinite */
4363 size_left
= max_size
;
4365 uint64_t one_more_packet_tot_size
;
4367 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
4369 if (!one_more_packet_tot_size
) {
4370 /* We are already grabbing all packets. */
4373 size_left
-= one_more_packet_tot_size
;
4374 if (size_left
< 0) {
4379 if (!cur_nb_packets
) {
4380 /* Not enough room to grab one packet of each stream, error. */
4383 return cur_nb_packets
;
4387 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4389 * The wait parameter is ignored so this call always wait for the snapshot to
4390 * complete before returning.
4392 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4394 int cmd_snapshot_record(struct ltt_session
*session
,
4395 struct lttng_snapshot_output
*output
, int wait
)
4397 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4399 unsigned int use_tmp_output
= 0;
4400 struct snapshot_output tmp_output
;
4401 unsigned int snapshot_success
= 0;
4407 DBG("Cmd snapshot record for session %s", session
->name
);
4409 /* Get the datetime for the snapshot output directory. */
4410 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4413 cmd_ret
= LTTNG_ERR_INVALID
;
4418 * Permission denied to create an output if the session is not
4419 * set in no output mode.
4421 if (session
->output_traces
) {
4422 cmd_ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4426 /* The session needs to be started at least once. */
4427 if (!session
->has_been_started
) {
4428 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4432 /* Use temporary output for the session. */
4433 if (*output
->ctrl_url
!= '\0') {
4434 ret
= snapshot_output_init(session
, output
->max_size
,
4436 output
->ctrl_url
, output
->data_url
,
4440 if (ret
== -ENOMEM
) {
4441 cmd_ret
= LTTNG_ERR_NOMEM
;
4443 cmd_ret
= LTTNG_ERR_INVALID
;
4447 /* Use the global session count for the temporary snapshot. */
4448 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4450 /* Use the global datetime */
4451 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4455 if (use_tmp_output
) {
4456 int64_t nb_packets_per_stream
;
4458 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4459 tmp_output
.max_size
);
4460 if (nb_packets_per_stream
< 0) {
4461 cmd_ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4465 if (session
->kernel_session
) {
4466 cmd_ret
= record_kernel_snapshot(session
->kernel_session
,
4467 &tmp_output
, session
,
4468 wait
, nb_packets_per_stream
);
4469 if (cmd_ret
!= LTTNG_OK
) {
4474 if (session
->ust_session
) {
4475 cmd_ret
= record_ust_snapshot(session
->ust_session
,
4476 &tmp_output
, session
,
4477 wait
, nb_packets_per_stream
);
4478 if (cmd_ret
!= LTTNG_OK
) {
4483 snapshot_success
= 1;
4485 struct snapshot_output
*sout
;
4486 struct lttng_ht_iter iter
;
4489 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4490 &iter
.iter
, sout
, node
.node
) {
4491 int64_t nb_packets_per_stream
;
4494 * Make a local copy of the output and assign the possible
4495 * temporary value given by the caller.
4497 memset(&tmp_output
, 0, sizeof(tmp_output
));
4498 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
4500 if (output
->max_size
!= (uint64_t) -1ULL) {
4501 tmp_output
.max_size
= output
->max_size
;
4504 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4505 tmp_output
.max_size
);
4506 if (nb_packets_per_stream
< 0) {
4507 cmd_ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4512 /* Use temporary name. */
4513 if (*output
->name
!= '\0') {
4514 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4515 sizeof(tmp_output
.name
))) {
4516 cmd_ret
= LTTNG_ERR_INVALID
;
4522 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4523 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4525 if (session
->kernel_session
) {
4526 cmd_ret
= record_kernel_snapshot(session
->kernel_session
,
4527 &tmp_output
, session
,
4528 wait
, nb_packets_per_stream
);
4529 if (cmd_ret
!= LTTNG_OK
) {
4535 if (session
->ust_session
) {
4536 cmd_ret
= record_ust_snapshot(session
->ust_session
,
4537 &tmp_output
, session
,
4538 wait
, nb_packets_per_stream
);
4539 if (cmd_ret
!= LTTNG_OK
) {
4544 snapshot_success
= 1;
4549 if (snapshot_success
) {
4550 session
->snapshot
.nb_snapshot
++;
4552 cmd_ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4560 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4562 int cmd_set_session_shm_path(struct ltt_session
*session
,
4563 const char *shm_path
)
4569 * Can only set shm path before session is started.
4571 if (session
->has_been_started
) {
4572 return LTTNG_ERR_SESSION_STARTED
;
4575 strncpy(session
->shm_path
, shm_path
,
4576 sizeof(session
->shm_path
));
4577 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4583 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4585 * Ask the consumer to rotate the session output directory.
4586 * The session lock must be held.
4588 * Returns LTTNG_OK on success or else a negative LTTng error code.
4590 int cmd_rotate_session(struct ltt_session
*session
,
4591 struct lttng_rotate_session_return
*rotate_return
)
4594 enum lttng_error_code cmd_ret
= LTTNG_OK
;
4596 struct tm
*timeinfo
;
4600 * Used to roll-back timestamps in case of failure to launch the
4603 time_t original_last_chunk_start_ts
, original_current_chunk_start_ts
;
4607 if (!session
->has_been_started
) {
4608 cmd_ret
= LTTNG_ERR_START_SESSION_ONCE
;
4612 if (session
->live_timer
|| !session
->output_traces
) {
4613 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4618 * Unsupported feature in lttng-relayd before 2.11.
4620 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4621 (session
->consumer
->relay_major_version
== 2 &&
4622 session
->consumer
->relay_minor_version
< 11)) {
4623 cmd_ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
4627 if (session
->rotation_state
== LTTNG_ROTATION_STATE_ONGOING
) {
4628 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
4630 cmd_ret
= LTTNG_ERR_ROTATION_PENDING
;
4635 * After a stop, we only allow one rotation to occur, the other ones are
4636 * useless until a new start.
4638 if (session
->rotated_after_last_stop
) {
4639 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4641 cmd_ret
= LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
4645 /* Special case for the first rotation. */
4646 if (session
->current_archive_id
== 0) {
4647 const char *base_path
= NULL
;
4649 assert(session
->kernel_session
|| session
->ust_session
);
4650 /* Either one of the two sessions is enough to get the root path. */
4651 base_path
= session_get_base_path(session
);
4654 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4656 sizeof(session
->rotation_chunk
.current_rotate_path
));
4658 ERR("Failed to copy session base path to current rotation chunk path");
4659 cmd_ret
= LTTNG_ERR_UNK
;
4664 * The currently active tracing path is now the folder we
4667 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4668 session
->rotation_chunk
.active_tracing_path
,
4669 sizeof(session
->rotation_chunk
.current_rotate_path
));
4671 ERR("Failed to copy the active tracing path to the current rotate path");
4672 cmd_ret
= LTTNG_ERR_UNK
;
4676 DBG("Current rotate path %s", session
->rotation_chunk
.current_rotate_path
);
4679 * Channels created after this point will belong to the next
4682 session
->current_archive_id
++;
4685 if (now
== (time_t) -1) {
4686 cmd_ret
= LTTNG_ERR_UNK
;
4690 /* Sample chunk bounds for roll-back in case of error. */
4691 original_last_chunk_start_ts
= session
->last_chunk_start_ts
;
4692 original_current_chunk_start_ts
= session
->current_chunk_start_ts
;
4694 session
->last_chunk_start_ts
= session
->current_chunk_start_ts
;
4695 session
->current_chunk_start_ts
= now
;
4697 timeinfo
= localtime(&now
);
4699 PERROR("Failed to sample local time in rotate session command");
4700 cmd_ret
= LTTNG_ERR_UNK
;
4703 strf_ret
= strftime(datetime
, sizeof(datetime
), "%Y%m%dT%H%M%S%z",
4706 ERR("Failed to format local time timestamp in rotate session command");
4707 cmd_ret
= LTTNG_ERR_UNK
;
4711 /* Current chunk directory, ex: 20170922-111754-42 */
4712 ret
= snprintf(session
->consumer
->chunk_path
,
4713 sizeof(session
->consumer
->chunk_path
),
4714 "%s-%" PRIu64
, datetime
,
4715 session
->current_archive_id
+ 1);
4716 if (ret
< 0 || ret
>= sizeof(session
->consumer
->chunk_path
)) {
4717 ERR("Failed to format the new chunk's directory in rotate session command");
4718 cmd_ret
= LTTNG_ERR_UNK
;
4723 * The active path for the next rotation/destroy.
4724 * Ex: ~/lttng-traces/auto-20170922-111748/20170922-111754-42
4726 ret
= snprintf(session
->rotation_chunk
.active_tracing_path
,
4727 sizeof(session
->rotation_chunk
.active_tracing_path
),
4729 session_get_base_path(session
),
4730 session
->consumer
->chunk_path
);
4731 if (ret
< 0 || ret
>= sizeof(session
->rotation_chunk
.active_tracing_path
)) {
4732 ERR("Failed to format active tracing path in rotate session command");
4733 cmd_ret
= LTTNG_ERR_UNK
;
4738 * A rotation has a local step even if the destination is a relay
4739 * daemon; the buffers must be consumed by the consumer daemon.
4741 session
->rotation_pending_local
= true;
4742 session
->rotation_pending_relay
=
4743 session_get_consumer_destination_type(session
) == CONSUMER_DST_NET
;
4744 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
4746 if (session
->kernel_session
) {
4747 ret
= lttng_strncpy(
4748 session
->kernel_session
->consumer
->chunk_path
,
4749 session
->consumer
->chunk_path
,
4750 sizeof(session
->kernel_session
->consumer
->chunk_path
));
4752 ERR("Failed to copy current chunk directory to kernel session");
4753 cmd_ret
= LTTNG_ERR_UNK
;
4757 * Create the new chunk folder, before the rotation begins so we
4758 * don't race with the consumer/tracer activity.
4760 ret
= domain_mkdir(session
->kernel_session
->consumer
, session
,
4761 session
->kernel_session
->uid
,
4762 session
->kernel_session
->gid
);
4764 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
4767 cmd_ret
= kernel_rotate_session(session
);
4768 if (cmd_ret
!= LTTNG_OK
) {
4772 if (session
->ust_session
) {
4773 ret
= lttng_strncpy(
4774 session
->ust_session
->consumer
->chunk_path
,
4775 session
->consumer
->chunk_path
,
4776 sizeof(session
->ust_session
->consumer
->chunk_path
));
4778 ERR("Failed to copy current chunk directory to userspace session");
4779 cmd_ret
= LTTNG_ERR_UNK
;
4782 ret
= domain_mkdir(session
->ust_session
->consumer
, session
,
4783 session
->ust_session
->uid
,
4784 session
->ust_session
->gid
);
4786 cmd_ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
4789 cmd_ret
= ust_app_rotate_session(session
);
4790 if (cmd_ret
!= LTTNG_OK
) {
4795 ret
= timer_session_rotation_pending_check_start(session
,
4796 DEFAULT_ROTATE_PENDING_TIMER
);
4798 cmd_ret
= LTTNG_ERR_UNK
;
4802 if (!session
->active
) {
4803 session
->rotated_after_last_stop
= true;
4806 if (rotate_return
) {
4807 rotate_return
->rotation_id
= session
->current_archive_id
;
4810 ret
= notification_thread_command_session_rotation_ongoing(
4811 notification_thread_handle
,
4812 session
->name
, session
->uid
, session
->gid
,
4813 session
->current_archive_id
- 1);
4814 if (ret
!= LTTNG_OK
) {
4815 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
4820 DBG("Cmd rotate session %s, archive_id %" PRIu64
" sent",
4821 session
->name
, session
->current_archive_id
- 1);
4823 ret
= (cmd_ret
== LTTNG_OK
) ? cmd_ret
: -((int) cmd_ret
);
4826 session
->last_chunk_start_ts
= original_last_chunk_start_ts
;
4827 session
->current_archive_id
= original_current_chunk_start_ts
;
4828 if (session_reset_rotation_state(session
,
4829 LTTNG_ROTATION_STATE_NO_ROTATION
)) {
4830 ERR("Failed to reset rotation state of session \"%s\"",
4837 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
4839 * Check if the session has finished its rotation.
4841 * Return 0 on success or else a LTTNG_ERR code.
4843 int cmd_rotate_get_info(struct ltt_session
*session
,
4844 struct lttng_rotation_get_info_return
*info_return
,
4845 uint64_t rotation_id
)
4851 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
4852 session
->current_archive_id
);
4854 if (session
->current_archive_id
!= rotation_id
) {
4855 info_return
->status
= (int32_t) LTTNG_ROTATION_STATE_EXPIRED
;
4860 switch (session
->rotation_state
) {
4861 case LTTNG_ROTATION_STATE_ONGOING
:
4862 DBG("Reporting that rotation id %" PRIu64
" of session %s is still pending",
4863 rotation_id
, session
->name
);
4865 case LTTNG_ROTATION_STATE_COMPLETED
:
4867 char *current_tracing_path_reply
;
4868 size_t current_tracing_path_reply_len
;
4870 switch (session_get_consumer_destination_type(session
)) {
4871 case CONSUMER_DST_LOCAL
:
4872 current_tracing_path_reply
=
4873 info_return
->location
.local
.absolute_path
;
4874 current_tracing_path_reply_len
=
4875 sizeof(info_return
->location
.local
.absolute_path
);
4876 info_return
->location_type
=
4877 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
4879 case CONSUMER_DST_NET
:
4880 current_tracing_path_reply
=
4881 info_return
->location
.relay
.relative_path
;
4882 current_tracing_path_reply_len
=
4883 sizeof(info_return
->location
.relay
.relative_path
);
4884 /* Currently the only supported relay protocol. */
4885 info_return
->location
.relay
.protocol
=
4886 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
4888 ret
= lttng_strncpy(info_return
->location
.relay
.host
,
4889 session_get_net_consumer_hostname(session
),
4890 sizeof(info_return
->location
.relay
.host
));
4892 ERR("Failed to host name to rotate_get_info reply");
4893 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4894 ret
= -LTTNG_ERR_UNK
;
4898 session_get_net_consumer_ports(session
,
4899 &info_return
->location
.relay
.ports
.control
,
4900 &info_return
->location
.relay
.ports
.data
);
4901 info_return
->location_type
=
4902 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
4907 ret
= lttng_strncpy(current_tracing_path_reply
,
4908 session
->rotation_chunk
.current_rotate_path
,
4909 current_tracing_path_reply_len
);
4911 ERR("Failed to copy current tracing path to rotate_get_info reply");
4912 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4913 ret
= -LTTNG_ERR_UNK
;
4919 case LTTNG_ROTATION_STATE_ERROR
:
4920 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session %s",
4921 rotation_id
, session
->name
);
4927 info_return
->status
= (int32_t) session
->rotation_state
;
4934 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
4936 * Configure the automatic rotation parameters.
4937 * 'activate' to true means activate the rotation schedule type with 'new_value'.
4938 * 'activate' to false means deactivate the rotation schedule and validate that
4939 * 'new_value' has the same value as the currently active value.
4941 * Return 0 on success or else a positive LTTNG_ERR code.
4943 int cmd_rotation_set_schedule(struct ltt_session
*session
,
4944 bool activate
, enum lttng_rotation_schedule_type schedule_type
,
4946 struct notification_thread_handle
*notification_thread_handle
)
4949 uint64_t *parameter_value
;
4953 DBG("Cmd rotate set schedule session %s", session
->name
);
4955 if (session
->live_timer
|| !session
->output_traces
) {
4956 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
4957 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4961 switch (schedule_type
) {
4962 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
4963 parameter_value
= &session
->rotate_size
;
4965 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
4966 parameter_value
= &session
->rotate_timer_period
;
4967 if (new_value
>= UINT_MAX
) {
4968 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
" > %u (UINT_MAX)",
4969 new_value
, UINT_MAX
);
4970 ret
= LTTNG_ERR_INVALID
;
4975 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
4976 ret
= LTTNG_ERR_INVALID
;
4980 /* Improper use of the API. */
4981 if (new_value
== -1ULL) {
4982 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
4983 ret
= LTTNG_ERR_INVALID
;
4988 * As indicated in struct ltt_session's comments, a value of == 0 means
4989 * this schedule rotation type is not in use.
4991 * Reject the command if we were asked to activate a schedule that was
4994 if (activate
&& *parameter_value
!= 0) {
4995 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
4996 ret
= LTTNG_ERR_ROTATION_SCHEDULE_SET
;
5001 * Reject the command if we were asked to deactivate a schedule that was
5004 if (!activate
&& *parameter_value
== 0) {
5005 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5006 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5011 * Reject the command if we were asked to deactivate a schedule that
5014 if (!activate
&& *parameter_value
!= new_value
) {
5015 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5016 ret
= LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET
;
5020 *parameter_value
= activate
? new_value
: 0;
5022 switch (schedule_type
) {
5023 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC
:
5024 if (activate
&& session
->active
) {
5026 * Only start the timer if the session is active,
5027 * otherwise it will be started when the session starts.
5029 ret
= timer_session_rotation_schedule_timer_start(
5030 session
, new_value
);
5032 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5033 ret
= LTTNG_ERR_UNK
;
5037 ret
= timer_session_rotation_schedule_timer_stop(
5040 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5041 ret
= LTTNG_ERR_UNK
;
5046 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD
:
5048 ret
= subscribe_session_consumed_size_rotation(session
,
5049 new_value
, notification_thread_handle
);
5051 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5052 ret
= LTTNG_ERR_UNK
;
5056 ret
= unsubscribe_session_consumed_size_rotation(session
,
5057 notification_thread_handle
);
5059 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5060 ret
= LTTNG_ERR_UNK
;
5067 /* Would have been caught before. */
5079 /* Wait for a given path to be removed before continuing. */
5080 static enum lttng_error_code
wait_on_path(void *path_data
)
5082 const char *shm_path
= path_data
;
5084 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5090 ret
= stat(shm_path
, &st
);
5092 if (errno
!= ENOENT
) {
5093 PERROR("stat() returned an error while checking for the existence of the shm path");
5095 DBG("shm path no longer exists, completing the destruction of session");
5099 if (!S_ISDIR(st
.st_mode
)) {
5100 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5105 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US
);
5111 * Returns a pointer to a handler to run on completion of a command.
5112 * Returns NULL if no handler has to be run for the last command executed.
5114 const struct cmd_completion_handler
*cmd_pop_completion_handler(void)
5116 struct cmd_completion_handler
*handler
= current_completion_handler
;
5118 current_completion_handler
= NULL
;
5123 * Init command subsystem.
5128 * Set network sequence index to 1 for streams to match a relayd
5129 * socket on the consumer side.
5131 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
5132 relayd_net_seq_idx
= 1;
5133 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
5135 DBG("Command subsystem initialized");