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>
25 #include <common/defaults.h>
26 #include <common/common.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/relayd/relayd.h>
29 #include <common/utils.h>
30 #include <common/compat/string.h>
31 #include <common/kernel-ctl/kernel-ctl.h>
32 #include <common/dynamic-buffer.h>
33 #include <common/buffer-view.h>
34 #include <lttng/trigger/trigger-internal.h>
35 #include <lttng/condition/condition.h>
36 #include <lttng/action/action.h>
37 #include <lttng/channel.h>
38 #include <lttng/channel-internal.h>
39 #include <lttng/rotate-internal.h>
40 #include <common/string-utils/string-utils.h>
45 #include "health-sessiond.h"
47 #include "kernel-consumer.h"
48 #include "lttng-sessiond.h"
50 #include "lttng-syscall.h"
52 #include "buffer-registry.h"
53 #include "notification-thread.h"
54 #include "notification-thread-commands.h"
56 #include "rotation-thread.h"
57 #include "sessiond-timer.h"
62 * Used to keep a unique index for each relayd socket created where this value
63 * is associated with streams on the consumer so it can match the right relayd
64 * to send to. It must be accessed with the relayd_net_seq_idx_lock
67 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
68 static uint64_t relayd_net_seq_idx
;
70 static int validate_ust_event_name(const char *);
71 static int cmd_enable_event_internal(struct ltt_session
*session
,
72 struct lttng_domain
*domain
,
73 char *channel_name
, struct lttng_event
*event
,
74 char *filter_expression
,
75 struct lttng_filter_bytecode
*filter
,
76 struct lttng_event_exclusion
*exclusion
,
80 * Create a session path used by list_lttng_sessions for the case that the
81 * session consumer is on the network.
83 static int build_network_session_path(char *dst
, size_t size
,
84 struct ltt_session
*session
)
86 int ret
, kdata_port
, udata_port
;
87 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
88 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
93 memset(tmp_urls
, 0, sizeof(tmp_urls
));
94 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
96 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
98 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
99 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
100 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
103 if (session
->ust_session
&& session
->ust_session
->consumer
) {
104 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
105 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
108 if (uuri
== NULL
&& kuri
== NULL
) {
109 uri
= &session
->consumer
->dst
.net
.control
;
110 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
111 } else if (kuri
&& uuri
) {
112 ret
= uri_compare(kuri
, uuri
);
116 /* Build uuri URL string */
117 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
124 } else if (kuri
&& uuri
== NULL
) {
126 } else if (uuri
&& kuri
== NULL
) {
130 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
136 * Do we have a UST url set. If yes, this means we have both kernel and UST
139 if (*tmp_uurl
!= '\0') {
140 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
141 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
144 if (kuri
|| (!kuri
&& !uuri
)) {
147 /* No kernel URI, use the UST port. */
150 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
158 * Get run-time attributes if the session has been started (discarded events,
161 static int get_kernel_runtime_stats(struct ltt_session
*session
,
162 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
163 uint64_t *lost_packets
)
167 if (!session
->has_been_started
) {
169 *discarded_events
= 0;
174 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
175 session
->kernel_session
->consumer
,
181 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
182 session
->kernel_session
->consumer
,
193 * Get run-time attributes if the session has been started (discarded events,
196 static int get_ust_runtime_stats(struct ltt_session
*session
,
197 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
198 uint64_t *lost_packets
)
201 struct ltt_ust_session
*usess
;
203 if (!discarded_events
|| !lost_packets
) {
208 usess
= session
->ust_session
;
209 assert(discarded_events
);
210 assert(lost_packets
);
212 if (!usess
|| !session
->has_been_started
) {
213 *discarded_events
= 0;
219 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
220 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
221 &usess
->buffer_reg_uid_list
,
222 usess
->consumer
, uchan
->id
,
223 uchan
->attr
.overwrite
,
226 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
227 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
228 uchan
, usess
->consumer
,
229 uchan
->attr
.overwrite
,
235 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
236 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
238 ERR("Unsupported buffer type");
249 * Fill lttng_channel array of all channels.
251 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
252 struct ltt_session
*session
, struct lttng_channel
*channels
,
253 struct lttng_channel_extended
*chan_exts
)
256 struct ltt_kernel_channel
*kchan
;
258 DBG("Listing channels for session %s", session
->name
);
261 case LTTNG_DOMAIN_KERNEL
:
262 /* Kernel channels */
263 if (session
->kernel_session
!= NULL
) {
264 cds_list_for_each_entry(kchan
,
265 &session
->kernel_session
->channel_list
.head
, list
) {
266 uint64_t discarded_events
, lost_packets
;
267 struct lttng_channel_extended
*extended
;
269 extended
= (struct lttng_channel_extended
*)
270 kchan
->channel
->attr
.extended
.ptr
;
272 ret
= get_kernel_runtime_stats(session
, kchan
,
273 &discarded_events
, &lost_packets
);
277 /* Copy lttng_channel struct to array */
278 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
279 channels
[i
].enabled
= kchan
->enabled
;
280 chan_exts
[i
].discarded_events
=
282 chan_exts
[i
].lost_packets
= lost_packets
;
283 chan_exts
[i
].monitor_timer_interval
=
284 extended
->monitor_timer_interval
;
285 chan_exts
[i
].blocking_timeout
= 0;
290 case LTTNG_DOMAIN_UST
:
292 struct lttng_ht_iter iter
;
293 struct ltt_ust_channel
*uchan
;
296 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
297 &iter
.iter
, uchan
, node
.node
) {
298 uint64_t discarded_events
= 0, lost_packets
= 0;
300 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
301 LTTNG_SYMBOL_NAME_LEN
)) {
304 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
305 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
306 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
307 channels
[i
].attr
.switch_timer_interval
=
308 uchan
->attr
.switch_timer_interval
;
309 channels
[i
].attr
.read_timer_interval
=
310 uchan
->attr
.read_timer_interval
;
311 channels
[i
].enabled
= uchan
->enabled
;
312 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
313 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
316 * Map enum lttng_ust_output to enum lttng_event_output.
318 switch (uchan
->attr
.output
) {
320 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
324 * LTTNG_UST_MMAP is the only supported UST
331 chan_exts
[i
].monitor_timer_interval
=
332 uchan
->monitor_timer_interval
;
333 chan_exts
[i
].blocking_timeout
=
334 uchan
->attr
.u
.s
.blocking_timeout
;
336 ret
= get_ust_runtime_stats(session
, uchan
,
337 &discarded_events
, &lost_packets
);
341 chan_exts
[i
].discarded_events
= discarded_events
;
342 chan_exts
[i
].lost_packets
= lost_packets
;
354 return -LTTNG_ERR_FATAL
;
360 static void increment_extended_len(const char *filter_expression
,
361 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
363 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
365 if (filter_expression
) {
366 *extended_len
+= strlen(filter_expression
) + 1;
370 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
374 static void append_extended_info(const char *filter_expression
,
375 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
377 struct lttcomm_event_extended_header extended_header
;
378 size_t filter_len
= 0;
379 size_t nb_exclusions
= 0;
381 if (filter_expression
) {
382 filter_len
= strlen(filter_expression
) + 1;
386 nb_exclusions
= exclusion
->count
;
389 /* Set header fields */
390 extended_header
.filter_len
= filter_len
;
391 extended_header
.nb_exclusions
= nb_exclusions
;
394 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
395 *extended_at
+= sizeof(extended_header
);
397 /* Copy filter string */
398 if (filter_expression
) {
399 memcpy(*extended_at
, filter_expression
, filter_len
);
400 *extended_at
+= filter_len
;
403 /* Copy exclusion names */
405 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
407 memcpy(*extended_at
, &exclusion
->names
, len
);
413 * Create a list of agent domain events.
415 * Return number of events in list on success or else a negative value.
417 static int list_lttng_agent_events(struct agent
*agt
,
418 struct lttng_event
**events
, size_t *total_size
)
421 unsigned int nb_event
= 0;
422 struct agent_event
*event
;
423 struct lttng_event
*tmp_events
;
424 struct lttng_ht_iter iter
;
425 size_t extended_len
= 0;
431 DBG3("Listing agent events");
434 nb_event
= lttng_ht_get_count(agt
->events
);
442 /* Compute required extended infos size */
443 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
446 * This is only valid because the commands which add events are
447 * processed in the same thread as the listing.
450 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
451 increment_extended_len(event
->filter_expression
, NULL
,
456 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
457 tmp_events
= zmalloc(*total_size
);
459 PERROR("zmalloc agent events session");
460 ret
= -LTTNG_ERR_FATAL
;
464 extended_at
= ((uint8_t *) tmp_events
) +
465 nb_event
* sizeof(struct lttng_event
);
468 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
469 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
470 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
471 tmp_events
[i
].enabled
= event
->enabled
;
472 tmp_events
[i
].loglevel
= event
->loglevel_value
;
473 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
476 /* Append extended info */
477 append_extended_info(event
->filter_expression
, NULL
,
482 *events
= tmp_events
;
486 assert(nb_event
== i
);
491 * Create a list of ust global domain events.
493 static int list_lttng_ust_global_events(char *channel_name
,
494 struct ltt_ust_domain_global
*ust_global
,
495 struct lttng_event
**events
, size_t *total_size
)
498 unsigned int nb_event
= 0;
499 struct lttng_ht_iter iter
;
500 struct lttng_ht_node_str
*node
;
501 struct ltt_ust_channel
*uchan
;
502 struct ltt_ust_event
*uevent
;
503 struct lttng_event
*tmp
;
504 size_t extended_len
= 0;
507 DBG("Listing UST global events for channel %s", channel_name
);
511 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
512 node
= lttng_ht_iter_get_node_str(&iter
);
514 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
518 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
520 nb_event
= lttng_ht_get_count(uchan
->events
);
527 DBG3("Listing UST global %d events", nb_event
);
529 /* Compute required extended infos size */
530 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
531 if (uevent
->internal
) {
536 increment_extended_len(uevent
->filter_expression
,
537 uevent
->exclusion
, &extended_len
);
540 /* All events are internal, skip. */
546 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
547 tmp
= zmalloc(*total_size
);
549 ret
= -LTTNG_ERR_FATAL
;
553 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
555 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
556 if (uevent
->internal
) {
557 /* This event should remain hidden from clients */
560 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
561 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
562 tmp
[i
].enabled
= uevent
->enabled
;
564 switch (uevent
->attr
.instrumentation
) {
565 case LTTNG_UST_TRACEPOINT
:
566 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
568 case LTTNG_UST_PROBE
:
569 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
571 case LTTNG_UST_FUNCTION
:
572 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
576 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
577 switch (uevent
->attr
.loglevel_type
) {
578 case LTTNG_UST_LOGLEVEL_ALL
:
579 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
581 case LTTNG_UST_LOGLEVEL_RANGE
:
582 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
584 case LTTNG_UST_LOGLEVEL_SINGLE
:
585 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
588 if (uevent
->filter
) {
591 if (uevent
->exclusion
) {
592 tmp
[i
].exclusion
= 1;
596 /* Append extended info */
597 append_extended_info(uevent
->filter_expression
,
598 uevent
->exclusion
, &extended_at
);
609 * Fill lttng_event array of all kernel events in the channel.
611 static int list_lttng_kernel_events(char *channel_name
,
612 struct ltt_kernel_session
*kernel_session
,
613 struct lttng_event
**events
, size_t *total_size
)
616 unsigned int nb_event
;
617 struct ltt_kernel_event
*event
;
618 struct ltt_kernel_channel
*kchan
;
619 size_t extended_len
= 0;
622 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
624 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
628 nb_event
= kchan
->event_count
;
630 DBG("Listing events for channel %s", kchan
->channel
->name
);
638 /* Compute required extended infos size */
639 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
640 increment_extended_len(event
->filter_expression
, NULL
,
644 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
645 *events
= zmalloc(*total_size
);
646 if (*events
== NULL
) {
647 ret
= LTTNG_ERR_FATAL
;
651 extended_at
= ((void *) *events
) +
652 nb_event
* sizeof(struct lttng_event
);
654 /* Kernel channels */
655 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
656 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
657 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
658 (*events
)[i
].enabled
= event
->enabled
;
659 (*events
)[i
].filter
=
660 (unsigned char) !!event
->filter_expression
;
662 switch (event
->event
->instrumentation
) {
663 case LTTNG_KERNEL_TRACEPOINT
:
664 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
666 case LTTNG_KERNEL_KRETPROBE
:
667 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
668 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
669 sizeof(struct lttng_kernel_kprobe
));
671 case LTTNG_KERNEL_KPROBE
:
672 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
673 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
674 sizeof(struct lttng_kernel_kprobe
));
676 case LTTNG_KERNEL_FUNCTION
:
677 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
678 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
679 sizeof(struct lttng_kernel_function
));
681 case LTTNG_KERNEL_NOOP
:
682 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
684 case LTTNG_KERNEL_SYSCALL
:
685 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
687 case LTTNG_KERNEL_ALL
:
693 /* Append extended info */
694 append_extended_info(event
->filter_expression
, NULL
,
702 /* Negate the error code to differentiate the size from an error */
707 * Add URI so the consumer output object. Set the correct path depending on the
708 * domain adding the default trace directory.
710 static int add_uri_to_consumer(struct consumer_output
*consumer
,
711 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
712 const char *session_name
)
715 const char *default_trace_dir
;
719 if (consumer
== NULL
) {
720 DBG("No consumer detected. Don't add URI. Stopping.");
721 ret
= LTTNG_ERR_NO_CONSUMER
;
726 case LTTNG_DOMAIN_KERNEL
:
727 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
729 case LTTNG_DOMAIN_UST
:
730 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
734 * This case is possible is we try to add the URI to the global tracing
735 * session consumer object which in this case there is no subdir.
737 default_trace_dir
= "";
740 switch (uri
->dtype
) {
743 DBG2("Setting network URI to consumer");
745 if (consumer
->type
== CONSUMER_DST_NET
) {
746 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
747 consumer
->dst
.net
.control_isset
) ||
748 (uri
->stype
== LTTNG_STREAM_DATA
&&
749 consumer
->dst
.net
.data_isset
)) {
750 ret
= LTTNG_ERR_URL_EXIST
;
754 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
757 consumer
->type
= CONSUMER_DST_NET
;
759 /* Set URI into consumer output object */
760 ret
= consumer_set_network_uri(consumer
, uri
);
764 } else if (ret
== 1) {
766 * URI was the same in the consumer so we do not append the subdir
767 * again so to not duplicate output dir.
773 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
774 ret
= consumer_set_subdir(consumer
, session_name
);
776 ret
= LTTNG_ERR_FATAL
;
781 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
782 /* On a new subdir, reappend the default trace dir. */
783 strncat(consumer
->subdir
, default_trace_dir
,
784 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
785 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
790 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
791 memset(consumer
->dst
.session_root_path
, 0,
792 sizeof(consumer
->dst
.session_root_path
));
793 /* Explicit length checks for strcpy and strcat. */
794 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
795 >= sizeof(consumer
->dst
.session_root_path
)) {
796 ret
= LTTNG_ERR_FATAL
;
799 strcpy(consumer
->dst
.session_root_path
, uri
->dst
.path
);
800 /* Append default trace dir */
801 strcat(consumer
->dst
.session_root_path
, default_trace_dir
);
802 /* Flag consumer as local. */
803 consumer
->type
= CONSUMER_DST_LOCAL
;
814 * Init tracing by creating trace directory and sending fds kernel consumer.
816 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
819 struct lttng_ht_iter iter
;
820 struct consumer_socket
*socket
;
826 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
827 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
829 pthread_mutex_lock(socket
->lock
);
830 ret
= kernel_consumer_send_session(socket
, session
);
831 pthread_mutex_unlock(socket
->lock
);
833 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
845 * Create a socket to the relayd using the URI.
847 * On success, the relayd_sock pointer is set to the created socket.
848 * Else, it's stays untouched and a lttcomm error code is returned.
850 static int create_connect_relayd(struct lttng_uri
*uri
,
851 struct lttcomm_relayd_sock
**relayd_sock
,
852 struct consumer_output
*consumer
)
855 struct lttcomm_relayd_sock
*rsock
;
857 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
858 RELAYD_VERSION_COMM_MINOR
);
860 ret
= LTTNG_ERR_FATAL
;
865 * Connect to relayd so we can proceed with a session creation. This call
866 * can possibly block for an arbitrary amount of time to set the health
867 * state to be in poll execution.
870 ret
= relayd_connect(rsock
);
873 ERR("Unable to reach lttng-relayd");
874 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
878 /* Create socket for control stream. */
879 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
880 DBG3("Creating relayd stream socket from URI");
882 /* Check relayd version */
883 ret
= relayd_version_check(rsock
);
884 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
886 } else if (ret
< 0) {
887 ERR("Unable to reach lttng-relayd");
888 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
891 consumer
->relay_major_version
= rsock
->major
;
892 consumer
->relay_minor_version
= rsock
->minor
;
893 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
894 DBG3("Creating relayd data socket from URI");
896 /* Command is not valid */
897 ERR("Relayd invalid stream type: %d", uri
->stype
);
898 ret
= LTTNG_ERR_INVALID
;
902 *relayd_sock
= rsock
;
907 /* The returned value is not useful since we are on an error path. */
908 (void) relayd_close(rsock
);
916 * Connect to the relayd using URI and send the socket to the right consumer.
918 * The consumer socket lock must be held by the caller.
920 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
921 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
922 struct consumer_output
*consumer
,
923 struct consumer_socket
*consumer_sock
,
924 char *session_name
, char *hostname
, int session_live_timer
)
927 struct lttcomm_relayd_sock
*rsock
= NULL
;
929 /* Connect to relayd and make version check if uri is the control. */
930 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
931 if (ret
!= LTTNG_OK
) {
932 goto relayd_comm_error
;
936 /* Set the network sequence index if not set. */
937 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
938 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
940 * Increment net_seq_idx because we are about to transfer the
941 * new relayd socket to the consumer.
942 * Assign unique key so the consumer can match streams.
944 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
945 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
948 /* Send relayd socket to consumer. */
949 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
950 relayd_uri
->stype
, session_id
,
951 session_name
, hostname
, session_live_timer
);
953 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
957 /* Flag that the corresponding socket was sent. */
958 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
959 consumer_sock
->control_sock_sent
= 1;
960 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
961 consumer_sock
->data_sock_sent
= 1;
967 * Close socket which was dup on the consumer side. The session daemon does
968 * NOT keep track of the relayd socket(s) once transfer to the consumer.
972 if (ret
!= LTTNG_OK
) {
974 * The consumer output for this session should not be used anymore
975 * since the relayd connection failed thus making any tracing or/and
976 * streaming not usable.
978 consumer
->enabled
= 0;
980 (void) relayd_close(rsock
);
988 * Send both relayd sockets to a specific consumer and domain. This is a
989 * helper function to facilitate sending the information to the consumer for a
992 * The consumer socket lock must be held by the caller.
994 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
995 unsigned int session_id
, struct consumer_output
*consumer
,
996 struct consumer_socket
*sock
, char *session_name
,
997 char *hostname
, int session_live_timer
)
1004 /* Sending control relayd socket. */
1005 if (!sock
->control_sock_sent
) {
1006 ret
= send_consumer_relayd_socket(domain
, session_id
,
1007 &consumer
->dst
.net
.control
, consumer
, sock
,
1008 session_name
, hostname
, session_live_timer
);
1009 if (ret
!= LTTNG_OK
) {
1014 /* Sending data relayd socket. */
1015 if (!sock
->data_sock_sent
) {
1016 ret
= send_consumer_relayd_socket(domain
, session_id
,
1017 &consumer
->dst
.net
.data
, consumer
, sock
,
1018 session_name
, hostname
, session_live_timer
);
1019 if (ret
!= LTTNG_OK
) {
1029 * Setup relayd connections for a tracing session. First creates the socket to
1030 * the relayd and send them to the right domain consumer. Consumer type MUST be
1033 int cmd_setup_relayd(struct ltt_session
*session
)
1036 struct ltt_ust_session
*usess
;
1037 struct ltt_kernel_session
*ksess
;
1038 struct consumer_socket
*socket
;
1039 struct lttng_ht_iter iter
;
1043 usess
= session
->ust_session
;
1044 ksess
= session
->kernel_session
;
1046 DBG("Setting relayd for session %s", session
->name
);
1050 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1051 && usess
->consumer
->enabled
) {
1052 /* For each consumer socket, send relayd sockets */
1053 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1054 socket
, node
.node
) {
1055 pthread_mutex_lock(socket
->lock
);
1056 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1057 usess
->consumer
, socket
,
1058 session
->name
, session
->hostname
,
1059 session
->live_timer
);
1060 pthread_mutex_unlock(socket
->lock
);
1061 if (ret
!= LTTNG_OK
) {
1064 /* Session is now ready for network streaming. */
1065 session
->net_handle
= 1;
1067 session
->consumer
->relay_major_version
=
1068 usess
->consumer
->relay_major_version
;
1069 session
->consumer
->relay_minor_version
=
1070 usess
->consumer
->relay_minor_version
;
1073 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1074 && ksess
->consumer
->enabled
) {
1075 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1076 socket
, node
.node
) {
1077 pthread_mutex_lock(socket
->lock
);
1078 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1079 ksess
->consumer
, socket
,
1080 session
->name
, session
->hostname
,
1081 session
->live_timer
);
1082 pthread_mutex_unlock(socket
->lock
);
1083 if (ret
!= LTTNG_OK
) {
1086 /* Session is now ready for network streaming. */
1087 session
->net_handle
= 1;
1089 session
->consumer
->relay_major_version
=
1090 ksess
->consumer
->relay_major_version
;
1091 session
->consumer
->relay_minor_version
=
1092 ksess
->consumer
->relay_minor_version
;
1101 * Start a kernel session by opening all necessary streams.
1103 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1106 struct ltt_kernel_channel
*kchan
;
1108 /* Open kernel metadata */
1109 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1110 ret
= kernel_open_metadata(ksess
);
1112 ret
= LTTNG_ERR_KERN_META_FAIL
;
1117 /* Open kernel metadata stream */
1118 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1119 ret
= kernel_open_metadata_stream(ksess
);
1121 ERR("Kernel create metadata stream failed");
1122 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1127 /* For each channel */
1128 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1129 if (kchan
->stream_count
== 0) {
1130 ret
= kernel_open_channel_stream(kchan
);
1132 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1135 /* Update the stream global counter */
1136 ksess
->stream_count_global
+= ret
;
1140 /* Setup kernel consumer socket and send fds to it */
1141 ret
= init_kernel_tracing(ksess
);
1143 ret
= LTTNG_ERR_KERN_START_FAIL
;
1147 /* This start the kernel tracing */
1148 ret
= kernel_start_session(ksess
);
1150 ret
= LTTNG_ERR_KERN_START_FAIL
;
1154 /* Quiescent wait after starting trace */
1155 kernel_wait_quiescent(kernel_tracer_fd
);
1166 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1168 int cmd_disable_channel(struct ltt_session
*session
,
1169 enum lttng_domain_type domain
, char *channel_name
)
1172 struct ltt_ust_session
*usess
;
1174 usess
= session
->ust_session
;
1179 case LTTNG_DOMAIN_KERNEL
:
1181 ret
= channel_kernel_disable(session
->kernel_session
,
1183 if (ret
!= LTTNG_OK
) {
1187 kernel_wait_quiescent(kernel_tracer_fd
);
1190 case LTTNG_DOMAIN_UST
:
1192 struct ltt_ust_channel
*uchan
;
1193 struct lttng_ht
*chan_ht
;
1195 chan_ht
= usess
->domain_global
.channels
;
1197 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1198 if (uchan
== NULL
) {
1199 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1203 ret
= channel_ust_disable(usess
, uchan
);
1204 if (ret
!= LTTNG_OK
) {
1210 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1222 * Command LTTNG_TRACK_PID processed by the client thread.
1224 * Called with session lock held.
1226 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1234 case LTTNG_DOMAIN_KERNEL
:
1236 struct ltt_kernel_session
*ksess
;
1238 ksess
= session
->kernel_session
;
1240 ret
= kernel_track_pid(ksess
, pid
);
1241 if (ret
!= LTTNG_OK
) {
1245 kernel_wait_quiescent(kernel_tracer_fd
);
1248 case LTTNG_DOMAIN_UST
:
1250 struct ltt_ust_session
*usess
;
1252 usess
= session
->ust_session
;
1254 ret
= trace_ust_track_pid(usess
, pid
);
1255 if (ret
!= LTTNG_OK
) {
1261 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1273 * Command LTTNG_UNTRACK_PID processed by the client thread.
1275 * Called with session lock held.
1277 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1285 case LTTNG_DOMAIN_KERNEL
:
1287 struct ltt_kernel_session
*ksess
;
1289 ksess
= session
->kernel_session
;
1291 ret
= kernel_untrack_pid(ksess
, pid
);
1292 if (ret
!= LTTNG_OK
) {
1296 kernel_wait_quiescent(kernel_tracer_fd
);
1299 case LTTNG_DOMAIN_UST
:
1301 struct ltt_ust_session
*usess
;
1303 usess
= session
->ust_session
;
1305 ret
= trace_ust_untrack_pid(usess
, pid
);
1306 if (ret
!= LTTNG_OK
) {
1312 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1324 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1326 * The wpipe arguments is used as a notifier for the kernel thread.
1328 int cmd_enable_channel(struct ltt_session
*session
,
1329 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1332 struct ltt_ust_session
*usess
= session
->ust_session
;
1333 struct lttng_ht
*chan_ht
;
1340 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1342 /* Validate channel name */
1343 if (attr
->name
[0] == '.' ||
1344 memchr(attr
->name
, '/', len
) != NULL
) {
1345 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1349 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1354 * Don't try to enable a channel if the session has been started at
1355 * some point in time before. The tracer does not allow it.
1357 if (session
->has_been_started
) {
1358 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1363 * If the session is a live session, remove the switch timer, the
1364 * live timer does the same thing but sends also synchronisation
1365 * beacons for inactive streams.
1367 if (session
->live_timer
> 0) {
1368 attr
->attr
.live_timer_interval
= session
->live_timer
;
1369 attr
->attr
.switch_timer_interval
= 0;
1372 /* Check for feature support */
1373 switch (domain
->type
) {
1374 case LTTNG_DOMAIN_KERNEL
:
1376 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd
) != 1) {
1377 /* Sampling position of buffer is not supported */
1378 WARN("Kernel tracer does not support buffer monitoring. "
1379 "Setting the monitor interval timer to 0 "
1380 "(disabled) for channel '%s' of session '%s'",
1381 attr
-> name
, session
->name
);
1382 lttng_channel_set_monitor_timer_interval(attr
, 0);
1386 case LTTNG_DOMAIN_UST
:
1387 case LTTNG_DOMAIN_JUL
:
1388 case LTTNG_DOMAIN_LOG4J
:
1389 case LTTNG_DOMAIN_PYTHON
:
1392 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1396 switch (domain
->type
) {
1397 case LTTNG_DOMAIN_KERNEL
:
1399 struct ltt_kernel_channel
*kchan
;
1401 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1402 session
->kernel_session
);
1403 if (kchan
== NULL
) {
1404 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1405 if (attr
->name
[0] != '\0') {
1406 session
->kernel_session
->has_non_default_channel
= 1;
1409 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1412 if (ret
!= LTTNG_OK
) {
1416 kernel_wait_quiescent(kernel_tracer_fd
);
1419 case LTTNG_DOMAIN_UST
:
1420 case LTTNG_DOMAIN_JUL
:
1421 case LTTNG_DOMAIN_LOG4J
:
1422 case LTTNG_DOMAIN_PYTHON
:
1424 struct ltt_ust_channel
*uchan
;
1429 * Current agent implementation limitations force us to allow
1430 * only one channel at once in "agent" subdomains. Each
1431 * subdomain has a default channel name which must be strictly
1434 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1435 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1436 LTTNG_SYMBOL_NAME_LEN
)) {
1437 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1440 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1441 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1442 LTTNG_SYMBOL_NAME_LEN
)) {
1443 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1446 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1447 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1448 LTTNG_SYMBOL_NAME_LEN
)) {
1449 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1454 chan_ht
= usess
->domain_global
.channels
;
1456 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1457 if (uchan
== NULL
) {
1458 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1459 if (attr
->name
[0] != '\0') {
1460 usess
->has_non_default_channel
= 1;
1463 ret
= channel_ust_enable(usess
, uchan
);
1468 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1479 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1481 int cmd_disable_event(struct ltt_session
*session
,
1482 enum lttng_domain_type domain
, char *channel_name
,
1483 struct lttng_event
*event
)
1488 DBG("Disable event command for event \'%s\'", event
->name
);
1490 event_name
= event
->name
;
1492 /* Error out on unhandled search criteria */
1493 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1494 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1495 ret
= LTTNG_ERR_UNK
;
1502 case LTTNG_DOMAIN_KERNEL
:
1504 struct ltt_kernel_channel
*kchan
;
1505 struct ltt_kernel_session
*ksess
;
1507 ksess
= session
->kernel_session
;
1510 * If a non-default channel has been created in the
1511 * session, explicitely require that -c chan_name needs
1514 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1515 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1519 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1520 if (kchan
== NULL
) {
1521 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1525 switch (event
->type
) {
1526 case LTTNG_EVENT_ALL
:
1527 case LTTNG_EVENT_TRACEPOINT
:
1528 case LTTNG_EVENT_SYSCALL
:
1529 case LTTNG_EVENT_PROBE
:
1530 case LTTNG_EVENT_FUNCTION
:
1531 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1532 if (event_name
[0] == '\0') {
1533 ret
= event_kernel_disable_event(kchan
,
1536 ret
= event_kernel_disable_event(kchan
,
1537 event_name
, event
->type
);
1539 if (ret
!= LTTNG_OK
) {
1544 ret
= LTTNG_ERR_UNK
;
1548 kernel_wait_quiescent(kernel_tracer_fd
);
1551 case LTTNG_DOMAIN_UST
:
1553 struct ltt_ust_channel
*uchan
;
1554 struct ltt_ust_session
*usess
;
1556 usess
= session
->ust_session
;
1558 if (validate_ust_event_name(event_name
)) {
1559 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1564 * If a non-default channel has been created in the
1565 * session, explicitly require that -c chan_name needs
1568 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1569 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1573 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1575 if (uchan
== NULL
) {
1576 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1580 switch (event
->type
) {
1581 case LTTNG_EVENT_ALL
:
1583 * An empty event name means that everything
1584 * should be disabled.
1586 if (event
->name
[0] == '\0') {
1587 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1589 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1592 if (ret
!= LTTNG_OK
) {
1597 ret
= LTTNG_ERR_UNK
;
1601 DBG3("Disable UST event %s in channel %s completed", event_name
,
1605 case LTTNG_DOMAIN_LOG4J
:
1606 case LTTNG_DOMAIN_JUL
:
1607 case LTTNG_DOMAIN_PYTHON
:
1610 struct ltt_ust_session
*usess
= session
->ust_session
;
1614 switch (event
->type
) {
1615 case LTTNG_EVENT_ALL
:
1618 ret
= LTTNG_ERR_UNK
;
1622 agt
= trace_ust_find_agent(usess
, domain
);
1624 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1628 * An empty event name means that everything
1629 * should be disabled.
1631 if (event
->name
[0] == '\0') {
1632 ret
= event_agent_disable_all(usess
, agt
);
1634 ret
= event_agent_disable(usess
, agt
, event_name
);
1636 if (ret
!= LTTNG_OK
) {
1643 ret
= LTTNG_ERR_UND
;
1656 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1658 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1659 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1661 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1662 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1665 * Don't try to add a context if the session has been started at
1666 * some point in time before. The tracer does not allow it and would
1667 * result in a corrupted trace.
1669 if (session
->has_been_started
) {
1670 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1674 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1675 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1676 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1680 case LTTNG_DOMAIN_KERNEL
:
1681 assert(session
->kernel_session
);
1683 if (session
->kernel_session
->channel_count
== 0) {
1684 /* Create default channel */
1685 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1686 if (ret
!= LTTNG_OK
) {
1689 chan_kern_created
= 1;
1691 /* Add kernel context to kernel tracer */
1692 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1693 if (ret
!= LTTNG_OK
) {
1697 case LTTNG_DOMAIN_JUL
:
1698 case LTTNG_DOMAIN_LOG4J
:
1701 * Validate channel name.
1702 * If no channel name is given and the domain is JUL or LOG4J,
1703 * set it to the appropriate domain-specific channel name. If
1704 * a name is provided but does not match the expexted channel
1705 * name, return an error.
1707 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1708 strcmp(channel_name
,
1709 DEFAULT_JUL_CHANNEL_NAME
)) {
1710 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1712 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1713 strcmp(channel_name
,
1714 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1715 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1718 /* break is _not_ missing here. */
1720 case LTTNG_DOMAIN_UST
:
1722 struct ltt_ust_session
*usess
= session
->ust_session
;
1723 unsigned int chan_count
;
1727 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1728 if (chan_count
== 0) {
1729 struct lttng_channel
*attr
;
1730 /* Create default channel */
1731 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1733 ret
= LTTNG_ERR_FATAL
;
1737 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1738 if (ret
!= LTTNG_OK
) {
1742 channel_attr_destroy(attr
);
1743 chan_ust_created
= 1;
1746 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1747 free(app_ctx_provider_name
);
1749 app_ctx_name
= NULL
;
1750 app_ctx_provider_name
= NULL
;
1751 if (ret
!= LTTNG_OK
) {
1757 ret
= LTTNG_ERR_UND
;
1765 if (chan_kern_created
) {
1766 struct ltt_kernel_channel
*kchan
=
1767 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1768 session
->kernel_session
);
1769 /* Created previously, this should NOT fail. */
1771 kernel_destroy_channel(kchan
);
1774 if (chan_ust_created
) {
1775 struct ltt_ust_channel
*uchan
=
1776 trace_ust_find_channel_by_name(
1777 session
->ust_session
->domain_global
.channels
,
1778 DEFAULT_CHANNEL_NAME
);
1779 /* Created previously, this should NOT fail. */
1781 /* Remove from the channel list of the session. */
1782 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1784 trace_ust_destroy_channel(uchan
);
1787 free(app_ctx_provider_name
);
1792 static inline bool name_starts_with(const char *name
, const char *prefix
)
1794 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1796 return !strncmp(name
, prefix
, max_cmp_len
);
1799 /* Perform userspace-specific event name validation */
1800 static int validate_ust_event_name(const char *name
)
1810 * Check name against all internal UST event component namespaces used
1813 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1814 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1815 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1824 * Internal version of cmd_enable_event() with a supplemental
1825 * "internal_event" flag which is used to enable internal events which should
1826 * be hidden from clients. Such events are used in the agent implementation to
1827 * enable the events through which all "agent" events are funeled.
1829 static int _cmd_enable_event(struct ltt_session
*session
,
1830 struct lttng_domain
*domain
,
1831 char *channel_name
, struct lttng_event
*event
,
1832 char *filter_expression
,
1833 struct lttng_filter_bytecode
*filter
,
1834 struct lttng_event_exclusion
*exclusion
,
1835 int wpipe
, bool internal_event
)
1837 int ret
= 0, channel_created
= 0;
1838 struct lttng_channel
*attr
= NULL
;
1842 assert(channel_name
);
1844 /* If we have a filter, we must have its filter expression */
1845 assert(!(!!filter_expression
^ !!filter
));
1847 /* Normalize event name as a globbing pattern */
1848 strutils_normalize_star_glob_pattern(event
->name
);
1850 /* Normalize exclusion names as globbing patterns */
1854 for (i
= 0; i
< exclusion
->count
; i
++) {
1855 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
1857 strutils_normalize_star_glob_pattern(name
);
1861 DBG("Enable event command for event \'%s\'", event
->name
);
1865 switch (domain
->type
) {
1866 case LTTNG_DOMAIN_KERNEL
:
1868 struct ltt_kernel_channel
*kchan
;
1871 * If a non-default channel has been created in the
1872 * session, explicitely require that -c chan_name needs
1875 if (session
->kernel_session
->has_non_default_channel
1876 && channel_name
[0] == '\0') {
1877 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1881 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1882 session
->kernel_session
);
1883 if (kchan
== NULL
) {
1884 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1885 LTTNG_BUFFER_GLOBAL
);
1887 ret
= LTTNG_ERR_FATAL
;
1890 if (lttng_strncpy(attr
->name
, channel_name
,
1891 sizeof(attr
->name
))) {
1892 ret
= LTTNG_ERR_INVALID
;
1896 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1897 if (ret
!= LTTNG_OK
) {
1900 channel_created
= 1;
1903 /* Get the newly created kernel channel pointer */
1904 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1905 session
->kernel_session
);
1906 if (kchan
== NULL
) {
1907 /* This sould not happen... */
1908 ret
= LTTNG_ERR_FATAL
;
1912 switch (event
->type
) {
1913 case LTTNG_EVENT_ALL
:
1915 char *filter_expression_a
= NULL
;
1916 struct lttng_filter_bytecode
*filter_a
= NULL
;
1919 * We need to duplicate filter_expression and filter,
1920 * because ownership is passed to first enable
1923 if (filter_expression
) {
1924 filter_expression_a
= strdup(filter_expression
);
1925 if (!filter_expression_a
) {
1926 ret
= LTTNG_ERR_FATAL
;
1931 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1933 free(filter_expression_a
);
1934 ret
= LTTNG_ERR_FATAL
;
1937 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1939 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1940 ret
= event_kernel_enable_event(kchan
, event
,
1941 filter_expression
, filter
);
1942 /* We have passed ownership */
1943 filter_expression
= NULL
;
1945 if (ret
!= LTTNG_OK
) {
1946 if (channel_created
) {
1947 /* Let's not leak a useless channel. */
1948 kernel_destroy_channel(kchan
);
1950 free(filter_expression_a
);
1954 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1955 ret
= event_kernel_enable_event(kchan
, event
,
1956 filter_expression_a
, filter_a
);
1957 /* We have passed ownership */
1958 filter_expression_a
= NULL
;
1960 if (ret
!= LTTNG_OK
) {
1965 case LTTNG_EVENT_PROBE
:
1966 case LTTNG_EVENT_FUNCTION
:
1967 case LTTNG_EVENT_FUNCTION_ENTRY
:
1968 case LTTNG_EVENT_TRACEPOINT
:
1969 ret
= event_kernel_enable_event(kchan
, event
,
1970 filter_expression
, filter
);
1971 /* We have passed ownership */
1972 filter_expression
= NULL
;
1974 if (ret
!= LTTNG_OK
) {
1975 if (channel_created
) {
1976 /* Let's not leak a useless channel. */
1977 kernel_destroy_channel(kchan
);
1982 case LTTNG_EVENT_SYSCALL
:
1983 ret
= event_kernel_enable_event(kchan
, event
,
1984 filter_expression
, filter
);
1985 /* We have passed ownership */
1986 filter_expression
= NULL
;
1988 if (ret
!= LTTNG_OK
) {
1993 ret
= LTTNG_ERR_UNK
;
1997 kernel_wait_quiescent(kernel_tracer_fd
);
2000 case LTTNG_DOMAIN_UST
:
2002 struct ltt_ust_channel
*uchan
;
2003 struct ltt_ust_session
*usess
= session
->ust_session
;
2008 * If a non-default channel has been created in the
2009 * session, explicitely require that -c chan_name needs
2012 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2013 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2017 /* Get channel from global UST domain */
2018 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2020 if (uchan
== NULL
) {
2021 /* Create default channel */
2022 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2023 usess
->buffer_type
);
2025 ret
= LTTNG_ERR_FATAL
;
2028 if (lttng_strncpy(attr
->name
, channel_name
,
2029 sizeof(attr
->name
))) {
2030 ret
= LTTNG_ERR_INVALID
;
2034 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2035 if (ret
!= LTTNG_OK
) {
2039 /* Get the newly created channel reference back */
2040 uchan
= trace_ust_find_channel_by_name(
2041 usess
->domain_global
.channels
, channel_name
);
2045 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2047 * Don't allow users to add UST events to channels which
2048 * are assigned to a userspace subdomain (JUL, Log4J,
2051 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2055 if (!internal_event
) {
2057 * Ensure the event name is not reserved for internal
2060 ret
= validate_ust_event_name(event
->name
);
2062 WARN("Userspace event name %s failed validation.",
2064 event
->name
: "NULL");
2065 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2070 /* At this point, the session and channel exist on the tracer */
2071 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2072 filter_expression
, filter
, exclusion
,
2074 /* We have passed ownership */
2075 filter_expression
= NULL
;
2078 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2079 goto already_enabled
;
2080 } else if (ret
!= LTTNG_OK
) {
2085 case LTTNG_DOMAIN_LOG4J
:
2086 case LTTNG_DOMAIN_JUL
:
2087 case LTTNG_DOMAIN_PYTHON
:
2089 const char *default_event_name
, *default_chan_name
;
2091 struct lttng_event uevent
;
2092 struct lttng_domain tmp_dom
;
2093 struct ltt_ust_session
*usess
= session
->ust_session
;
2097 agt
= trace_ust_find_agent(usess
, domain
->type
);
2099 agt
= agent_create(domain
->type
);
2101 ret
= LTTNG_ERR_NOMEM
;
2104 agent_add(agt
, usess
->agents
);
2107 /* Create the default tracepoint. */
2108 memset(&uevent
, 0, sizeof(uevent
));
2109 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2110 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2111 default_event_name
= event_get_default_agent_ust_name(
2113 if (!default_event_name
) {
2114 ret
= LTTNG_ERR_FATAL
;
2117 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2118 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2121 * The domain type is changed because we are about to enable the
2122 * default channel and event for the JUL domain that are hardcoded.
2123 * This happens in the UST domain.
2125 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2126 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2128 switch (domain
->type
) {
2129 case LTTNG_DOMAIN_LOG4J
:
2130 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2132 case LTTNG_DOMAIN_JUL
:
2133 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2135 case LTTNG_DOMAIN_PYTHON
:
2136 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2139 /* The switch/case we are in makes this impossible */
2144 char *filter_expression_copy
= NULL
;
2145 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2148 const size_t filter_size
= sizeof(
2149 struct lttng_filter_bytecode
)
2152 filter_copy
= zmalloc(filter_size
);
2154 ret
= LTTNG_ERR_NOMEM
;
2157 memcpy(filter_copy
, filter
, filter_size
);
2159 filter_expression_copy
=
2160 strdup(filter_expression
);
2161 if (!filter_expression
) {
2162 ret
= LTTNG_ERR_NOMEM
;
2165 if (!filter_expression_copy
|| !filter_copy
) {
2166 free(filter_expression_copy
);
2172 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2173 (char *) default_chan_name
,
2174 &uevent
, filter_expression_copy
,
2175 filter_copy
, NULL
, wpipe
);
2178 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2179 goto already_enabled
;
2180 } else if (ret
!= LTTNG_OK
) {
2184 /* The wild card * means that everything should be enabled. */
2185 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2186 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2189 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2193 filter_expression
= NULL
;
2194 if (ret
!= LTTNG_OK
) {
2201 ret
= LTTNG_ERR_UND
;
2209 free(filter_expression
);
2212 channel_attr_destroy(attr
);
2218 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2219 * We own filter, exclusion, and filter_expression.
2221 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2222 char *channel_name
, struct lttng_event
*event
,
2223 char *filter_expression
,
2224 struct lttng_filter_bytecode
*filter
,
2225 struct lttng_event_exclusion
*exclusion
,
2228 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2229 filter_expression
, filter
, exclusion
, wpipe
, false);
2233 * Enable an event which is internal to LTTng. An internal should
2234 * never be made visible to clients and are immune to checks such as
2237 static int cmd_enable_event_internal(struct ltt_session
*session
,
2238 struct lttng_domain
*domain
,
2239 char *channel_name
, struct lttng_event
*event
,
2240 char *filter_expression
,
2241 struct lttng_filter_bytecode
*filter
,
2242 struct lttng_event_exclusion
*exclusion
,
2245 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2246 filter_expression
, filter
, exclusion
, wpipe
, true);
2250 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2252 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2253 struct lttng_event
**events
)
2256 ssize_t nb_events
= 0;
2259 case LTTNG_DOMAIN_KERNEL
:
2260 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2261 if (nb_events
< 0) {
2262 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2266 case LTTNG_DOMAIN_UST
:
2267 nb_events
= ust_app_list_events(events
);
2268 if (nb_events
< 0) {
2269 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2273 case LTTNG_DOMAIN_LOG4J
:
2274 case LTTNG_DOMAIN_JUL
:
2275 case LTTNG_DOMAIN_PYTHON
:
2276 nb_events
= agent_list_events(events
, domain
);
2277 if (nb_events
< 0) {
2278 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2283 ret
= LTTNG_ERR_UND
;
2290 /* Return negative value to differentiate return code */
2295 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2297 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2298 struct lttng_event_field
**fields
)
2301 ssize_t nb_fields
= 0;
2304 case LTTNG_DOMAIN_UST
:
2305 nb_fields
= ust_app_list_event_fields(fields
);
2306 if (nb_fields
< 0) {
2307 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2311 case LTTNG_DOMAIN_KERNEL
:
2312 default: /* fall-through */
2313 ret
= LTTNG_ERR_UND
;
2320 /* Return negative value to differentiate return code */
2324 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2326 return syscall_table_list(events
);
2330 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2332 * Called with session lock held.
2334 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2335 enum lttng_domain_type domain
, int32_t **pids
)
2338 ssize_t nr_pids
= 0;
2341 case LTTNG_DOMAIN_KERNEL
:
2343 struct ltt_kernel_session
*ksess
;
2345 ksess
= session
->kernel_session
;
2346 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2348 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2353 case LTTNG_DOMAIN_UST
:
2355 struct ltt_ust_session
*usess
;
2357 usess
= session
->ust_session
;
2358 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2360 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2365 case LTTNG_DOMAIN_LOG4J
:
2366 case LTTNG_DOMAIN_JUL
:
2367 case LTTNG_DOMAIN_PYTHON
:
2369 ret
= LTTNG_ERR_UND
;
2376 /* Return negative value to differentiate return code */
2381 int domain_mkdir(const struct consumer_output
*output
,
2382 const struct ltt_session
*session
,
2383 uid_t uid
, gid_t gid
)
2385 struct consumer_socket
*socket
;
2386 struct lttng_ht_iter iter
;
2390 if (!output
|| !output
->socks
) {
2391 ERR("No consumer output found");
2396 path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
2398 ERR("Cannot allocate mkdir path");
2403 ret
= snprintf(path
, LTTNG_PATH_MAX
, "%s%s%s",
2404 session_get_base_path(session
),
2405 output
->chunk_path
, output
->subdir
);
2406 if (ret
< 0 || ret
>= LTTNG_PATH_MAX
) {
2412 DBG("Domain mkdir %s for session %" PRIu64
, path
, session
->id
);
2415 * We have to iterate to find a socket, but we only need to send the
2416 * rename command to one consumer, so we break after the first one.
2418 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
2419 pthread_mutex_lock(socket
->lock
);
2420 ret
= consumer_mkdir(socket
, session
->id
, output
, path
, uid
, gid
);
2421 pthread_mutex_unlock(socket
->lock
);
2423 ERR("Consumer mkdir");
2440 int session_mkdir(const struct ltt_session
*session
)
2443 struct consumer_output
*output
;
2448 * Unsupported feature in lttng-relayd before 2.11, not an error since it
2449 * is only needed for session rotation and the user will get an error
2452 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
2453 session
->consumer
->relay_major_version
== 2 &&
2454 session
->consumer
->relay_minor_version
< 11) {
2459 if (session
->kernel_session
) {
2460 output
= session
->kernel_session
->consumer
;
2461 uid
= session
->kernel_session
->uid
;
2462 gid
= session
->kernel_session
->gid
;
2463 ret
= domain_mkdir(output
, session
, uid
, gid
);
2465 ERR("Mkdir kernel");
2470 if (session
->ust_session
) {
2471 output
= session
->ust_session
->consumer
;
2472 uid
= session
->ust_session
->uid
;
2473 gid
= session
->ust_session
->gid
;
2474 ret
= domain_mkdir(output
, session
, uid
, gid
);
2488 * Command LTTNG_START_TRACE processed by the client thread.
2490 * Called with session mutex held.
2492 int cmd_start_trace(struct ltt_session
*session
)
2495 unsigned long nb_chan
= 0;
2496 struct ltt_kernel_session
*ksession
;
2497 struct ltt_ust_session
*usess
;
2501 /* Ease our life a bit ;) */
2502 ksession
= session
->kernel_session
;
2503 usess
= session
->ust_session
;
2505 /* Is the session already started? */
2506 if (session
->active
) {
2507 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2512 * Starting a session without channel is useless since after that it's not
2513 * possible to enable channel thus inform the client.
2515 if (usess
&& usess
->domain_global
.channels
) {
2516 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2519 nb_chan
+= ksession
->channel_count
;
2522 ret
= LTTNG_ERR_NO_CHANNEL
;
2527 * Record the timestamp of the first time the session is started for
2528 * an eventual session rotation call.
2530 if (!session
->has_been_started
) {
2531 session
->current_chunk_start_ts
= time(NULL
);
2532 if (session
->current_chunk_start_ts
== (time_t) -1) {
2533 PERROR("Failed to retrieve the \"%s\" session's start time",
2535 ret
= LTTNG_ERR_FATAL
;
2538 if (!session
->snapshot_mode
&& session
->output_traces
) {
2539 ret
= session_mkdir(session
);
2541 ERR("Failed to create the session directories");
2542 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2548 /* Kernel tracing */
2549 if (ksession
!= NULL
) {
2550 DBG("Start kernel tracing session %s", session
->name
);
2551 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2552 if (ret
!= LTTNG_OK
) {
2557 /* Flag session that trace should start automatically */
2560 * Even though the start trace might fail, flag this session active so
2561 * other application coming in are started by default.
2565 ret
= ust_app_start_trace_all(usess
);
2567 ret
= LTTNG_ERR_UST_START_FAIL
;
2572 /* Flag this after a successful start. */
2573 session
->has_been_started
= 1;
2574 session
->active
= 1;
2577 * Clear the flag that indicates that a rotation was done while the
2578 * session was stopped.
2580 session
->rotated_after_last_stop
= false;
2582 if (session
->rotate_timer_period
) {
2583 ret
= sessiond_rotate_timer_start(session
,
2584 session
->rotate_timer_period
);
2586 ERR("Failed to enable rotate timer");
2587 ret
= LTTNG_ERR_UNK
;
2599 int rename_active_chunk(struct ltt_session
*session
)
2603 session
->rotate_count
++;
2606 * The currently active tracing path is now the folder we
2609 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
2610 session
->rotation_chunk
.active_tracing_path
,
2611 sizeof(session
->rotation_chunk
.current_rotate_path
));
2613 ERR("Failed to copy active tracing path");
2617 ret
= rename_complete_chunk(session
, time(NULL
));
2619 ERR("Failed to rename current rotate path");
2624 * We just renamed, the folder, we didn't do an actual rotation, so
2625 * the active tracing path is now the renamed folder and we have to
2626 * restore the rotate count.
2628 ret
= lttng_strncpy(session
->rotation_chunk
.active_tracing_path
,
2629 session
->rotation_chunk
.current_rotate_path
,
2630 sizeof(session
->rotation_chunk
.active_tracing_path
));
2632 ERR("Failed to rename active session chunk tracing path");
2636 session
->rotate_count
--;
2641 * Command LTTNG_STOP_TRACE processed by the client thread.
2643 int cmd_stop_trace(struct ltt_session
*session
)
2646 struct ltt_kernel_channel
*kchan
;
2647 struct ltt_kernel_session
*ksession
;
2648 struct ltt_ust_session
*usess
;
2649 bool error_occured
= false;
2653 DBG("Begin stop session %s (id %" PRIu64
")", session
->name
, session
->id
);
2655 ksession
= session
->kernel_session
;
2656 usess
= session
->ust_session
;
2658 /* Session is not active. Skip everythong and inform the client. */
2659 if (!session
->active
) {
2660 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2664 if (session
->rotate_relay_pending_timer_enabled
) {
2665 sessiond_timer_rotate_pending_stop(session
);
2668 if (session
->rotate_timer_enabled
) {
2669 sessiond_rotate_timer_stop(session
);
2672 if (session
->rotate_count
> 0 && !session
->rotate_pending
) {
2673 ret
= rename_active_chunk(session
);
2676 * This error should not prevent the user from stopping
2677 * the session. However, it will be reported at the end.
2679 error_occured
= true;
2684 if (ksession
&& ksession
->active
) {
2685 DBG("Stop kernel tracing");
2687 ret
= kernel_stop_session(ksession
);
2689 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2693 kernel_wait_quiescent(kernel_tracer_fd
);
2695 /* Flush metadata after stopping (if exists) */
2696 if (ksession
->metadata_stream_fd
>= 0) {
2697 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2699 ERR("Kernel metadata flush failed");
2703 /* Flush all buffers after stopping */
2704 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2705 ret
= kernel_flush_buffer(kchan
);
2707 ERR("Kernel flush buffer error");
2711 ksession
->active
= 0;
2712 DBG("Kernel session stopped %s (id %" PRIu64
")", session
->name
,
2716 if (usess
&& usess
->active
) {
2718 * Even though the stop trace might fail, flag this session inactive so
2719 * other application coming in are not started by default.
2723 ret
= ust_app_stop_trace_all(usess
);
2725 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2730 /* Flag inactive after a successful stop. */
2731 session
->active
= 0;
2732 ret
= !error_occured
? LTTNG_OK
: LTTNG_ERR_UNK
;
2739 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2741 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2742 struct lttng_uri
*uris
)
2745 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2746 struct ltt_ust_session
*usess
= session
->ust_session
;
2752 /* Can't set consumer URI if the session is active. */
2753 if (session
->active
) {
2754 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2758 /* Set the "global" consumer URIs */
2759 for (i
= 0; i
< nb_uri
; i
++) {
2760 ret
= add_uri_to_consumer(session
->consumer
,
2761 &uris
[i
], 0, session
->name
);
2762 if (ret
!= LTTNG_OK
) {
2767 /* Set UST session URIs */
2768 if (session
->ust_session
) {
2769 for (i
= 0; i
< nb_uri
; i
++) {
2770 ret
= add_uri_to_consumer(
2771 session
->ust_session
->consumer
,
2772 &uris
[i
], LTTNG_DOMAIN_UST
,
2774 if (ret
!= LTTNG_OK
) {
2780 /* Set kernel session URIs */
2781 if (session
->kernel_session
) {
2782 for (i
= 0; i
< nb_uri
; i
++) {
2783 ret
= add_uri_to_consumer(
2784 session
->kernel_session
->consumer
,
2785 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2787 if (ret
!= LTTNG_OK
) {
2794 * Make sure to set the session in output mode after we set URI since a
2795 * session can be created without URL (thus flagged in no output mode).
2797 session
->output_traces
= 1;
2799 ksess
->output_traces
= 1;
2803 usess
->output_traces
= 1;
2814 * Command LTTNG_CREATE_SESSION processed by the client thread.
2816 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2817 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2820 struct ltt_session
*session
;
2826 * Verify if the session already exist
2828 * XXX: There is no need for the session lock list here since the caller
2829 * (process_client_msg) is holding it. We might want to change that so a
2830 * single command does not lock the entire session list.
2832 session
= session_find_by_name(name
);
2833 if (session
!= NULL
) {
2834 ret
= LTTNG_ERR_EXIST_SESS
;
2838 /* Create tracing session in the registry */
2839 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2840 LTTNG_SOCK_GET_GID_CRED(creds
));
2841 if (ret
!= LTTNG_OK
) {
2846 * Get the newly created session pointer back
2848 * XXX: There is no need for the session lock list here since the caller
2849 * (process_client_msg) is holding it. We might want to change that so a
2850 * single command does not lock the entire session list.
2852 session
= session_find_by_name(name
);
2855 session
->live_timer
= live_timer
;
2856 /* Create default consumer output for the session not yet created. */
2857 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2858 if (session
->consumer
== NULL
) {
2859 ret
= LTTNG_ERR_FATAL
;
2860 goto consumer_error
;
2864 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2865 if (ret
!= LTTNG_OK
) {
2866 goto consumer_error
;
2868 session
->output_traces
= 1;
2870 session
->output_traces
= 0;
2871 DBG2("Session %s created with no output", session
->name
);
2874 session
->consumer
->enabled
= 1;
2879 session_destroy(session
);
2886 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2888 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2889 size_t nb_uri
, lttng_sock_cred
*creds
)
2892 struct ltt_session
*session
;
2893 struct snapshot_output
*new_output
= NULL
;
2899 * Create session in no output mode with URIs set to NULL. The uris we've
2900 * received are for a default snapshot output if one.
2902 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2903 if (ret
!= LTTNG_OK
) {
2907 /* Get the newly created session pointer back. This should NEVER fail. */
2908 session
= session_find_by_name(name
);
2911 /* Flag session for snapshot mode. */
2912 session
->snapshot_mode
= 1;
2914 /* Skip snapshot output creation if no URI is given. */
2919 new_output
= snapshot_output_alloc();
2921 ret
= LTTNG_ERR_NOMEM
;
2922 goto error_snapshot_alloc
;
2925 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2926 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2928 if (ret
== -ENOMEM
) {
2929 ret
= LTTNG_ERR_NOMEM
;
2931 ret
= LTTNG_ERR_INVALID
;
2933 goto error_snapshot
;
2937 snapshot_add_output(&session
->snapshot
, new_output
);
2944 snapshot_output_destroy(new_output
);
2945 error_snapshot_alloc
:
2946 session_destroy(session
);
2952 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2954 * Called with session lock held.
2956 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
,
2957 struct notification_thread_handle
*notification_thread_handle
)
2960 struct ltt_ust_session
*usess
;
2961 struct ltt_kernel_session
*ksess
;
2966 usess
= session
->ust_session
;
2967 ksess
= session
->kernel_session
;
2969 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
2971 if (session
->rotate_relay_pending_timer_enabled
) {
2972 sessiond_timer_rotate_pending_stop(session
);
2975 if (session
->rotate_timer_enabled
) {
2976 sessiond_rotate_timer_stop(session
);
2979 if (session
->rotate_size
) {
2980 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
2981 session
->rotate_size
= 0;
2985 * The rename of the current chunk is performed at stop, but if we rotated
2986 * the session after the previous stop command, we need to rename the
2987 * new (and empty) chunk that was started in between.
2989 if (session
->rotated_after_last_stop
) {
2990 rename_active_chunk(session
);
2993 /* Clean kernel session teardown */
2994 kernel_destroy_session(ksess
);
2996 /* UST session teardown */
2998 /* Close any relayd session */
2999 consumer_output_send_destroy_relayd(usess
->consumer
);
3001 /* Destroy every UST application related to this session. */
3002 ret
= ust_app_destroy_trace_all(usess
);
3004 ERR("Error in ust_app_destroy_trace_all");
3007 /* Clean up the rest. */
3008 trace_ust_destroy_session(usess
);
3012 * Must notify the kernel thread here to update it's poll set in order to
3013 * remove the channel(s)' fd just destroyed.
3015 ret
= notify_thread_pipe(wpipe
);
3017 PERROR("write kernel poll pipe");
3020 ret
= session_destroy(session
);
3026 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3028 int cmd_register_consumer(struct ltt_session
*session
,
3029 enum lttng_domain_type domain
, const char *sock_path
,
3030 struct consumer_data
*cdata
)
3033 struct consumer_socket
*socket
= NULL
;
3040 case LTTNG_DOMAIN_KERNEL
:
3042 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3046 /* Can't register a consumer if there is already one */
3047 if (ksess
->consumer_fds_sent
!= 0) {
3048 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3052 sock
= lttcomm_connect_unix_sock(sock_path
);
3054 ret
= LTTNG_ERR_CONNECT_FAIL
;
3057 cdata
->cmd_sock
= sock
;
3059 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3060 if (socket
== NULL
) {
3063 PERROR("close register consumer");
3065 cdata
->cmd_sock
= -1;
3066 ret
= LTTNG_ERR_FATAL
;
3070 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3071 if (socket
->lock
== NULL
) {
3072 PERROR("zmalloc pthread mutex");
3073 ret
= LTTNG_ERR_FATAL
;
3076 pthread_mutex_init(socket
->lock
, NULL
);
3077 socket
->registered
= 1;
3080 consumer_add_socket(socket
, ksess
->consumer
);
3083 pthread_mutex_lock(&cdata
->pid_mutex
);
3085 pthread_mutex_unlock(&cdata
->pid_mutex
);
3090 /* TODO: Userspace tracing */
3091 ret
= LTTNG_ERR_UND
;
3099 consumer_destroy_socket(socket
);
3105 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3107 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3108 struct lttng_domain
**domains
)
3113 struct lttng_ht_iter iter
;
3115 if (session
->kernel_session
!= NULL
) {
3116 DBG3("Listing domains found kernel domain");
3120 if (session
->ust_session
!= NULL
) {
3121 DBG3("Listing domains found UST global domain");
3125 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3127 if (agt
->being_used
) {
3138 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3139 if (*domains
== NULL
) {
3140 ret
= LTTNG_ERR_FATAL
;
3144 if (session
->kernel_session
!= NULL
) {
3145 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3147 /* Kernel session buffer type is always GLOBAL */
3148 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3153 if (session
->ust_session
!= NULL
) {
3154 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3155 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3159 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3161 if (agt
->being_used
) {
3162 (*domains
)[index
].type
= agt
->domain
;
3163 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3173 /* Return negative value to differentiate return code */
3179 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3181 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3182 struct ltt_session
*session
, struct lttng_channel
**channels
)
3184 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3187 case LTTNG_DOMAIN_KERNEL
:
3188 if (session
->kernel_session
!= NULL
) {
3189 nb_chan
= session
->kernel_session
->channel_count
;
3191 DBG3("Number of kernel channels %zd", nb_chan
);
3193 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3197 case LTTNG_DOMAIN_UST
:
3198 if (session
->ust_session
!= NULL
) {
3200 nb_chan
= lttng_ht_get_count(
3201 session
->ust_session
->domain_global
.channels
);
3204 DBG3("Number of UST global channels %zd", nb_chan
);
3206 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3211 ret
= -LTTNG_ERR_UND
;
3216 const size_t channel_size
= sizeof(struct lttng_channel
) +
3217 sizeof(struct lttng_channel_extended
);
3218 struct lttng_channel_extended
*channel_exts
;
3220 payload_size
= nb_chan
* channel_size
;
3221 *channels
= zmalloc(payload_size
);
3222 if (*channels
== NULL
) {
3223 ret
= -LTTNG_ERR_FATAL
;
3227 channel_exts
= ((void *) *channels
) +
3228 (nb_chan
* sizeof(struct lttng_channel
));
3229 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3230 if (ret
!= LTTNG_OK
) {
3245 * Command LTTNG_LIST_EVENTS processed by the client thread.
3247 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3248 struct ltt_session
*session
, char *channel_name
,
3249 struct lttng_event
**events
, size_t *total_size
)
3252 ssize_t nb_event
= 0;
3255 case LTTNG_DOMAIN_KERNEL
:
3256 if (session
->kernel_session
!= NULL
) {
3257 nb_event
= list_lttng_kernel_events(channel_name
,
3258 session
->kernel_session
, events
,
3262 case LTTNG_DOMAIN_UST
:
3264 if (session
->ust_session
!= NULL
) {
3265 nb_event
= list_lttng_ust_global_events(channel_name
,
3266 &session
->ust_session
->domain_global
, events
,
3271 case LTTNG_DOMAIN_LOG4J
:
3272 case LTTNG_DOMAIN_JUL
:
3273 case LTTNG_DOMAIN_PYTHON
:
3274 if (session
->ust_session
) {
3275 struct lttng_ht_iter iter
;
3279 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3280 &iter
.iter
, agt
, node
.node
) {
3281 if (agt
->domain
== domain
) {
3282 nb_event
= list_lttng_agent_events(
3292 ret
= LTTNG_ERR_UND
;
3299 /* Return negative value to differentiate return code */
3304 * Using the session list, filled a lttng_session array to send back to the
3305 * client for session listing.
3307 * The session list lock MUST be acquired before calling this function. Use
3308 * session_lock_list() and session_unlock_list().
3310 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3315 struct ltt_session
*session
;
3316 struct ltt_session_list
*list
= session_get_list();
3318 DBG("Getting all available session for UID %d GID %d",
3321 * Iterate over session list and append data after the control struct in
3324 cds_list_for_each_entry(session
, &list
->head
, list
) {
3326 * Only list the sessions the user can control.
3328 if (!session_access_ok(session
, uid
, gid
)) {
3332 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3333 struct ltt_ust_session
*usess
= session
->ust_session
;
3335 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3336 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3337 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3338 ret
= build_network_session_path(sessions
[i
].path
,
3339 sizeof(sessions
[i
].path
), session
);
3341 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3342 session
->consumer
->dst
.session_root_path
);
3345 PERROR("snprintf session path");
3349 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3350 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3351 sessions
[i
].enabled
= session
->active
;
3352 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3353 sessions
[i
].live_timer_interval
= session
->live_timer
;
3359 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3360 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3362 int cmd_data_pending(struct ltt_session
*session
)
3365 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3366 struct ltt_ust_session
*usess
= session
->ust_session
;
3370 DBG("Data pending for session %s", session
->name
);
3372 /* Session MUST be stopped to ask for data availability. */
3373 if (session
->active
) {
3374 ret
= LTTNG_ERR_SESSION_STARTED
;
3378 * If stopped, just make sure we've started before else the above call
3379 * will always send that there is data pending.
3381 * The consumer assumes that when the data pending command is received,
3382 * the trace has been started before or else no output data is written
3383 * by the streams which is a condition for data pending. So, this is
3384 * *VERY* important that we don't ask the consumer before a start
3387 if (!session
->has_been_started
) {
3394 * A rotation is still pending, we have to wait.
3396 if (session
->rotate_pending
) {
3397 DBG("Rotate still pending for session %s", session
->name
);
3402 if (ksess
&& ksess
->consumer
) {
3403 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3405 /* Data is still being extracted for the kernel. */
3410 if (usess
&& usess
->consumer
) {
3411 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3413 /* Data is still being extracted for the kernel. */
3418 /* Data is ready to be read by a viewer */
3426 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3428 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3430 int cmd_snapshot_add_output(struct ltt_session
*session
,
3431 struct lttng_snapshot_output
*output
, uint32_t *id
)
3434 struct snapshot_output
*new_output
;
3439 DBG("Cmd snapshot add output for session %s", session
->name
);
3442 * Can't create an output if the session is not set in no-output mode.
3444 if (session
->output_traces
) {
3445 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3449 /* Only one output is allowed until we have the "tee" feature. */
3450 if (session
->snapshot
.nb_output
== 1) {
3451 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3455 new_output
= snapshot_output_alloc();
3457 ret
= LTTNG_ERR_NOMEM
;
3461 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3462 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3463 &session
->snapshot
);
3465 if (ret
== -ENOMEM
) {
3466 ret
= LTTNG_ERR_NOMEM
;
3468 ret
= LTTNG_ERR_INVALID
;
3474 snapshot_add_output(&session
->snapshot
, new_output
);
3476 *id
= new_output
->id
;
3483 snapshot_output_destroy(new_output
);
3489 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3491 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3493 int cmd_snapshot_del_output(struct ltt_session
*session
,
3494 struct lttng_snapshot_output
*output
)
3497 struct snapshot_output
*sout
= NULL
;
3505 * Permission denied to create an output if the session is not
3506 * set in no output mode.
3508 if (session
->output_traces
) {
3509 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3514 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3516 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3517 } else if (*output
->name
!= '\0') {
3518 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3520 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3523 ret
= LTTNG_ERR_INVALID
;
3527 snapshot_delete_output(&session
->snapshot
, sout
);
3528 snapshot_output_destroy(sout
);
3537 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3539 * If no output is available, outputs is untouched and 0 is returned.
3541 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3543 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3544 struct lttng_snapshot_output
**outputs
)
3547 struct lttng_snapshot_output
*list
= NULL
;
3548 struct lttng_ht_iter iter
;
3549 struct snapshot_output
*output
;
3554 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3557 * Permission denied to create an output if the session is not
3558 * set in no output mode.
3560 if (session
->output_traces
) {
3561 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3565 if (session
->snapshot
.nb_output
== 0) {
3570 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3572 ret
= -LTTNG_ERR_NOMEM
;
3576 /* Copy list from session to the new list object. */
3578 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3579 output
, node
.node
) {
3580 assert(output
->consumer
);
3581 list
[idx
].id
= output
->id
;
3582 list
[idx
].max_size
= output
->max_size
;
3583 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3584 sizeof(list
[idx
].name
))) {
3585 ret
= -LTTNG_ERR_INVALID
;
3588 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3589 if (lttng_strncpy(list
[idx
].ctrl_url
,
3590 output
->consumer
->dst
.session_root_path
,
3591 sizeof(list
[idx
].ctrl_url
))) {
3592 ret
= -LTTNG_ERR_INVALID
;
3597 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3598 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3600 ret
= -LTTNG_ERR_NOMEM
;
3605 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3606 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3608 ret
= -LTTNG_ERR_NOMEM
;
3617 ret
= session
->snapshot
.nb_output
;
3626 * Check if we can regenerate the metadata for this session.
3627 * Only kernel, UST per-uid and non-live sessions are supported.
3629 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3632 int check_regenerate_metadata_support(struct ltt_session
*session
)
3638 if (session
->live_timer
!= 0) {
3639 ret
= LTTNG_ERR_LIVE_SESSION
;
3642 if (!session
->active
) {
3643 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3646 if (session
->ust_session
) {
3647 switch (session
->ust_session
->buffer_type
) {
3648 case LTTNG_BUFFER_PER_UID
:
3650 case LTTNG_BUFFER_PER_PID
:
3651 ret
= LTTNG_ERR_PER_PID_SESSION
;
3655 ret
= LTTNG_ERR_UNK
;
3659 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3660 session
->consumer
->relay_minor_version
< 8) {
3661 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3671 int clear_metadata_file(int fd
)
3676 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
3677 if (lseek_ret
< 0) {
3683 ret
= ftruncate(fd
, 0);
3685 PERROR("ftruncate");
3694 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3697 struct buffer_reg_uid
*uid_reg
= NULL
;
3698 struct buffer_reg_session
*session_reg
= NULL
;
3701 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3702 struct ust_registry_session
*registry
;
3703 struct ust_registry_channel
*chan
;
3704 struct lttng_ht_iter iter_chan
;
3706 session_reg
= uid_reg
->registry
;
3707 registry
= session_reg
->reg
.ust
;
3709 pthread_mutex_lock(®istry
->lock
);
3710 registry
->metadata_len_sent
= 0;
3711 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3712 registry
->metadata_len
= 0;
3713 registry
->metadata_version
++;
3714 if (registry
->metadata_fd
> 0) {
3715 /* Clear the metadata file's content. */
3716 ret
= clear_metadata_file(registry
->metadata_fd
);
3718 pthread_mutex_unlock(®istry
->lock
);
3723 ret
= ust_metadata_session_statedump(registry
, NULL
,
3724 registry
->major
, registry
->minor
);
3726 pthread_mutex_unlock(®istry
->lock
);
3727 ERR("Failed to generate session metadata (err = %d)",
3731 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3733 struct ust_registry_event
*event
;
3734 struct lttng_ht_iter iter_event
;
3736 ret
= ust_metadata_channel_statedump(registry
, chan
);
3738 pthread_mutex_unlock(®istry
->lock
);
3739 ERR("Failed to generate channel metadata "
3743 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3745 ret
= ust_metadata_event_statedump(registry
,
3748 pthread_mutex_unlock(®istry
->lock
);
3749 ERR("Failed to generate event metadata "
3755 pthread_mutex_unlock(®istry
->lock
);
3764 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3766 * Ask the consumer to truncate the existing metadata file(s) and
3767 * then regenerate the metadata. Live and per-pid sessions are not
3768 * supported and return an error.
3770 * Return 0 on success or else a LTTNG_ERR code.
3772 int cmd_regenerate_metadata(struct ltt_session
*session
)
3778 ret
= check_regenerate_metadata_support(session
);
3783 if (session
->kernel_session
) {
3784 ret
= kernctl_session_regenerate_metadata(
3785 session
->kernel_session
->fd
);
3787 ERR("Failed to regenerate the kernel metadata");
3792 if (session
->ust_session
) {
3793 ret
= ust_regenerate_metadata(session
->ust_session
);
3795 ERR("Failed to regenerate the UST metadata");
3799 DBG("Cmd metadata regenerate for session %s", session
->name
);
3807 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3809 * Ask the tracer to regenerate a new statedump.
3811 * Return 0 on success or else a LTTNG_ERR code.
3813 int cmd_regenerate_statedump(struct ltt_session
*session
)
3819 if (!session
->active
) {
3820 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3824 if (session
->kernel_session
) {
3825 ret
= kernctl_session_regenerate_statedump(
3826 session
->kernel_session
->fd
);
3828 * Currently, the statedump in kernel can only fail if out
3832 if (ret
== -ENOMEM
) {
3833 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3835 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3837 ERR("Failed to regenerate the kernel statedump");
3842 if (session
->ust_session
) {
3843 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3845 * Currently, the statedump in UST always returns 0.
3848 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3849 ERR("Failed to regenerate the UST statedump");
3853 DBG("Cmd regenerate statedump for session %s", session
->name
);
3860 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3861 struct notification_thread_handle
*notification_thread
)
3865 ssize_t sock_recv_len
;
3866 struct lttng_trigger
*trigger
= NULL
;
3867 struct lttng_buffer_view view
;
3868 struct lttng_dynamic_buffer trigger_buffer
;
3870 lttng_dynamic_buffer_init(&trigger_buffer
);
3871 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3872 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3874 ret
= LTTNG_ERR_NOMEM
;
3878 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3880 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3881 ERR("Failed to receive \"register trigger\" command payload");
3882 /* TODO: should this be a new error enum ? */
3883 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3887 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3888 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3890 ERR("Invalid trigger payload received in \"register trigger\" command");
3891 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3895 ret
= notification_thread_command_register_trigger(notification_thread
,
3897 /* Ownership of trigger was transferred. */
3900 lttng_trigger_destroy(trigger
);
3901 lttng_dynamic_buffer_reset(&trigger_buffer
);
3905 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3906 struct notification_thread_handle
*notification_thread
)
3910 ssize_t sock_recv_len
;
3911 struct lttng_trigger
*trigger
= NULL
;
3912 struct lttng_buffer_view view
;
3913 struct lttng_dynamic_buffer trigger_buffer
;
3915 lttng_dynamic_buffer_init(&trigger_buffer
);
3916 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3917 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3919 ret
= LTTNG_ERR_NOMEM
;
3923 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3925 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3926 ERR("Failed to receive \"unregister trigger\" command payload");
3927 /* TODO: should this be a new error enum ? */
3928 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3932 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3933 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3935 ERR("Invalid trigger payload received in \"unregister trigger\" command");
3936 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3940 ret
= notification_thread_command_unregister_trigger(notification_thread
,
3943 lttng_trigger_destroy(trigger
);
3944 lttng_dynamic_buffer_reset(&trigger_buffer
);
3949 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3950 * snapshot output is *not* set with a remote destination.
3952 * Return 0 on success or a LTTNG_ERR code.
3954 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3955 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3958 struct lttng_ht_iter iter
;
3959 struct consumer_socket
*socket
;
3962 assert(snap_output
);
3965 DBG2("Set relayd object from snapshot output");
3967 /* Ignore if snapshot consumer output is not network. */
3968 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3973 * For each consumer socket, create and send the relayd object of the
3977 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3978 socket
, node
.node
) {
3979 pthread_mutex_lock(socket
->lock
);
3980 ret
= send_consumer_relayd_sockets(0, session
->id
,
3981 snap_output
->consumer
, socket
,
3982 session
->name
, session
->hostname
,
3983 session
->live_timer
);
3984 pthread_mutex_unlock(socket
->lock
);
3985 if (ret
!= LTTNG_OK
) {
3997 * Record a kernel snapshot.
3999 * Return LTTNG_OK on success or a LTTNG_ERR code.
4001 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
4002 struct snapshot_output
*output
, struct ltt_session
*session
,
4003 int wait
, uint64_t nb_packets_per_stream
)
4013 * Copy kernel session sockets so we can communicate with the right
4014 * consumer for the snapshot record command.
4016 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
4018 ret
= LTTNG_ERR_NOMEM
;
4022 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
4023 if (ret
!= LTTNG_OK
) {
4024 goto error_snapshot
;
4027 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
4028 if (ret
!= LTTNG_OK
) {
4029 goto error_snapshot
;
4036 /* Clean up copied sockets so this output can use some other later on. */
4037 consumer_destroy_output_sockets(output
->consumer
);
4044 * Record a UST snapshot.
4046 * Return 0 on success or a LTTNG_ERR error code.
4048 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
4049 struct snapshot_output
*output
, struct ltt_session
*session
,
4050 int wait
, uint64_t nb_packets_per_stream
)
4059 * Copy UST session sockets so we can communicate with the right
4060 * consumer for the snapshot record command.
4062 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
4064 ret
= LTTNG_ERR_NOMEM
;
4068 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
4069 if (ret
!= LTTNG_OK
) {
4070 goto error_snapshot
;
4073 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
4077 ret
= LTTNG_ERR_INVALID
;
4080 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4083 goto error_snapshot
;
4089 /* Clean up copied sockets so this output can use some other later on. */
4090 consumer_destroy_output_sockets(output
->consumer
);
4096 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
4097 uint64_t cur_nr_packets
)
4099 uint64_t tot_size
= 0;
4101 if (session
->kernel_session
) {
4102 struct ltt_kernel_channel
*chan
;
4103 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4105 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4106 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4108 * Don't take channel into account if we
4109 * already grab all its packets.
4113 tot_size
+= chan
->channel
->attr
.subbuf_size
4114 * chan
->stream_count
;
4118 if (session
->ust_session
) {
4119 struct ltt_ust_session
*usess
= session
->ust_session
;
4121 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4129 * Calculate the number of packets we can grab from each stream that
4130 * fits within the overall snapshot max size.
4132 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4133 * the number of packets per stream.
4135 * TODO: this approach is not perfect: we consider the worse case
4136 * (packet filling the sub-buffers) as an upper bound, but we could do
4137 * better if we do this calculation while we actually grab the packet
4138 * content: we would know how much padding we don't actually store into
4141 * This algorithm is currently bounded by the number of packets per
4144 * Since we call this algorithm before actually grabbing the data, it's
4145 * an approximation: for instance, applications could appear/disappear
4146 * in between this call and actually grabbing data.
4149 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
4152 uint64_t cur_nb_packets
= 0;
4155 return 0; /* Infinite */
4158 size_left
= max_size
;
4160 uint64_t one_more_packet_tot_size
;
4162 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
4164 if (!one_more_packet_tot_size
) {
4165 /* We are already grabbing all packets. */
4168 size_left
-= one_more_packet_tot_size
;
4169 if (size_left
< 0) {
4174 if (!cur_nb_packets
) {
4175 /* Not enough room to grab one packet of each stream, error. */
4178 return cur_nb_packets
;
4182 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4184 * The wait parameter is ignored so this call always wait for the snapshot to
4185 * complete before returning.
4187 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4189 int cmd_snapshot_record(struct ltt_session
*session
,
4190 struct lttng_snapshot_output
*output
, int wait
)
4193 unsigned int use_tmp_output
= 0;
4194 struct snapshot_output tmp_output
;
4195 unsigned int snapshot_success
= 0;
4201 DBG("Cmd snapshot record for session %s", session
->name
);
4203 /* Get the datetime for the snapshot output directory. */
4204 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4207 ret
= LTTNG_ERR_INVALID
;
4212 * Permission denied to create an output if the session is not
4213 * set in no output mode.
4215 if (session
->output_traces
) {
4216 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4220 /* The session needs to be started at least once. */
4221 if (!session
->has_been_started
) {
4222 ret
= LTTNG_ERR_START_SESSION_ONCE
;
4226 /* Use temporary output for the session. */
4227 if (*output
->ctrl_url
!= '\0') {
4228 ret
= snapshot_output_init(output
->max_size
, output
->name
,
4229 output
->ctrl_url
, output
->data_url
, session
->consumer
,
4232 if (ret
== -ENOMEM
) {
4233 ret
= LTTNG_ERR_NOMEM
;
4235 ret
= LTTNG_ERR_INVALID
;
4239 /* Use the global session count for the temporary snapshot. */
4240 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4242 /* Use the global datetime */
4243 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4247 if (use_tmp_output
) {
4248 int64_t nb_packets_per_stream
;
4250 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4251 tmp_output
.max_size
);
4252 if (nb_packets_per_stream
< 0) {
4253 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4257 if (session
->kernel_session
) {
4258 ret
= record_kernel_snapshot(session
->kernel_session
,
4259 &tmp_output
, session
,
4260 wait
, nb_packets_per_stream
);
4261 if (ret
!= LTTNG_OK
) {
4266 if (session
->ust_session
) {
4267 ret
= record_ust_snapshot(session
->ust_session
,
4268 &tmp_output
, session
,
4269 wait
, nb_packets_per_stream
);
4270 if (ret
!= LTTNG_OK
) {
4275 snapshot_success
= 1;
4277 struct snapshot_output
*sout
;
4278 struct lttng_ht_iter iter
;
4281 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4282 &iter
.iter
, sout
, node
.node
) {
4283 int64_t nb_packets_per_stream
;
4286 * Make a local copy of the output and assign the possible
4287 * temporary value given by the caller.
4289 memset(&tmp_output
, 0, sizeof(tmp_output
));
4290 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
4292 if (output
->max_size
!= (uint64_t) -1ULL) {
4293 tmp_output
.max_size
= output
->max_size
;
4296 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4297 tmp_output
.max_size
);
4298 if (nb_packets_per_stream
< 0) {
4299 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4304 /* Use temporary name. */
4305 if (*output
->name
!= '\0') {
4306 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4307 sizeof(tmp_output
.name
))) {
4308 ret
= LTTNG_ERR_INVALID
;
4314 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4315 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4317 if (session
->kernel_session
) {
4318 ret
= record_kernel_snapshot(session
->kernel_session
,
4319 &tmp_output
, session
,
4320 wait
, nb_packets_per_stream
);
4321 if (ret
!= LTTNG_OK
) {
4327 if (session
->ust_session
) {
4328 ret
= record_ust_snapshot(session
->ust_session
,
4329 &tmp_output
, session
,
4330 wait
, nb_packets_per_stream
);
4331 if (ret
!= LTTNG_OK
) {
4336 snapshot_success
= 1;
4341 if (snapshot_success
) {
4342 session
->snapshot
.nb_snapshot
++;
4344 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4352 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4354 int cmd_set_session_shm_path(struct ltt_session
*session
,
4355 const char *shm_path
)
4361 * Can only set shm path before session is started.
4363 if (session
->has_been_started
) {
4364 return LTTNG_ERR_SESSION_STARTED
;
4367 strncpy(session
->shm_path
, shm_path
,
4368 sizeof(session
->shm_path
));
4369 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4375 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4377 * Ask the consumer to rotate the session output directory.
4378 * The session lock must be held.
4380 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4382 int cmd_rotate_session(struct ltt_session
*session
,
4383 struct lttng_rotate_session_return
*rotate_return
)
4387 struct tm
*timeinfo
;
4390 bool ust_active
= false;
4394 if (!session
->has_been_started
) {
4395 ret
= -LTTNG_ERR_START_SESSION_ONCE
;
4399 if (session
->live_timer
|| session
->snapshot_mode
||
4400 !session
->output_traces
) {
4401 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4406 * Unsupported feature in lttng-relayd before 2.11.
4408 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4409 (session
->consumer
->relay_major_version
== 2 &&
4410 session
->consumer
->relay_minor_version
< 11)) {
4411 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
4415 if (session
->rotate_pending
|| session
->rotate_pending_relay
) {
4416 ret
= -LTTNG_ERR_ROTATION_PENDING
;
4417 DBG("Rotate already in progress");
4422 * After a stop, we only allow one rotation to occur, the other ones are
4423 * useless until a new start.
4425 if (session
->rotated_after_last_stop
) {
4426 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4428 ret
= -LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
4432 /* Special case for the first rotation. */
4433 if (session
->rotate_count
== 0) {
4434 const char *base_path
= NULL
;
4436 /* Either one of the two sessions is enough to get the root path. */
4437 if (session
->kernel_session
) {
4438 base_path
= session_get_base_path(session
);
4439 } else if (session
->ust_session
) {
4440 base_path
= session_get_base_path(session
);
4445 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4447 sizeof(session
->rotation_chunk
.current_rotate_path
));
4449 ERR("Failed to copy session base path to current rotation chunk path");
4450 ret
= -LTTNG_ERR_UNK
;
4455 * The currently active tracing path is now the folder we
4458 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4459 session
->rotation_chunk
.active_tracing_path
,
4460 sizeof(session
->rotation_chunk
.current_rotate_path
));
4462 ERR("Failed to copy the active tracing path to the current rotate path");
4463 ret
= -LTTNG_ERR_UNK
;
4467 DBG("Current rotate path %s", session
->rotation_chunk
.current_rotate_path
);
4469 session
->rotate_count
++;
4470 session
->rotate_pending
= true;
4471 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
4474 * Create the path name for the next chunk.
4477 if (now
== (time_t) -1) {
4478 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4481 session
->last_chunk_start_ts
= session
->current_chunk_start_ts
;
4482 session
->current_chunk_start_ts
= now
;
4484 timeinfo
= localtime(&now
);
4486 PERROR("Failed to sample local time in rotate session command");
4487 ret
= -LTTNG_ERR_UNK
;
4490 strf_ret
= strftime(datetime
, sizeof(datetime
), "%Y%m%dT%H%M%S%z",
4493 ERR("Failed to format local time timestamp in rotate session command");
4494 ret
= -LTTNG_ERR_UNK
;
4497 if (session
->kernel_session
) {
4499 * The active path for the next rotation/destroy.
4500 * Ex: ~/lttng-traces/auto-20170922-111748/20170922-111754-42
4502 ret
= snprintf(session
->rotation_chunk
.active_tracing_path
,
4503 sizeof(session
->rotation_chunk
.active_tracing_path
),
4505 session_get_base_path(session
),
4506 datetime
, session
->rotate_count
+ 1);
4507 if (ret
< 0 || ret
== sizeof(session
->rotation_chunk
.active_tracing_path
)) {
4508 ERR("Failed to format active kernel tracing path in rotate session command");
4509 ret
= -LTTNG_ERR_UNK
;
4513 * The sub-directory for the consumer
4514 * Ex: /20170922-111754-42/kernel
4516 ret
= snprintf(session
->kernel_session
->consumer
->chunk_path
,
4517 sizeof(session
->kernel_session
->consumer
->chunk_path
),
4518 "/%s-%" PRIu64
, datetime
,
4519 session
->rotate_count
+ 1);
4520 if (ret
< 0 || ret
== sizeof(session
->kernel_session
->consumer
->chunk_path
)) {
4521 ERR("Failed to format the kernel consumer's sub-directory in rotate session command");
4522 ret
= -LTTNG_ERR_UNK
;
4526 * Create the new chunk folder, before the rotation begins so we don't
4527 * race with the consumer/tracer activity.
4529 ret
= domain_mkdir(session
->kernel_session
->consumer
, session
,
4530 session
->kernel_session
->uid
,
4531 session
->kernel_session
->gid
);
4533 ERR("Failed to create kernel session tracing path at %s",
4534 session
->kernel_session
->consumer
->chunk_path
);
4535 ret
= -LTTNG_ERR_CREATE_DIR_FAIL
;
4538 ret
= kernel_rotate_session(session
);
4539 if (ret
!= LTTNG_OK
) {
4544 if (session
->ust_session
) {
4545 ret
= snprintf(session
->rotation_chunk
.active_tracing_path
,
4546 PATH_MAX
, "%s/%s-%" PRIu64
,
4547 session_get_base_path(session
),
4548 datetime
, session
->rotate_count
+ 1);
4550 ERR("Failed to format active UST tracing path in rotate session command");
4551 ret
= -LTTNG_ERR_UNK
;
4554 ret
= snprintf(session
->ust_session
->consumer
->chunk_path
,
4555 PATH_MAX
, "/%s-%" PRIu64
, datetime
,
4556 session
->rotate_count
+ 1);
4558 ERR("Failed to format the UST consumer's sub-directory in rotate session command");
4559 ret
= -LTTNG_ERR_UNK
;
4563 * Create the new chunk folder, before the rotation begins so we don't
4564 * race with the consumer/tracer activity.
4566 ret
= domain_mkdir(session
->ust_session
->consumer
, session
,
4567 session
->ust_session
->uid
,
4568 session
->ust_session
->gid
);
4570 ret
= -LTTNG_ERR_CREATE_DIR_FAIL
;
4573 ret
= ust_app_rotate_session(session
, &ust_active
);
4574 if (ret
!= LTTNG_OK
) {
4578 * Handle the case where we did not start a rotation on any channel.
4579 * The consumer will never wake up the rotation thread to perform the
4580 * rename, so we have to do it here while we hold the session and
4581 * session_list locks.
4583 if (!session
->kernel_session
&& !ust_active
) {
4584 ret
= rename_complete_chunk(session
, now
);
4586 ERR("Failed to rename completed rotation chunk");
4589 session
->rotate_pending
= false;
4590 session
->rotation_state
= LTTNG_ROTATION_STATE_COMPLETED
;
4594 if (!session
->active
) {
4595 session
->rotated_after_last_stop
= true;
4598 if (rotate_return
) {
4599 rotate_return
->rotation_id
= session
->rotate_count
;
4602 DBG("Cmd rotate session %s, rotate_id %" PRIu64
" sent", session
->name
,
4603 session
->rotate_count
);
4611 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
4613 * Check if the session has finished its rotation.
4615 * Return 0 on success or else a LTTNG_ERR code.
4617 int cmd_rotate_get_info(struct ltt_session
*session
,
4618 struct lttng_rotation_get_info_return
*info_return
,
4619 uint64_t rotation_id
)
4625 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
4626 session
->rotate_count
);
4628 if (session
->rotate_count
!= rotation_id
) {
4629 info_return
->status
= (int32_t) LTTNG_ROTATION_STATE_EXPIRED
;
4634 switch (session
->rotation_state
) {
4635 case LTTNG_ROTATION_STATE_ONGOING
:
4636 DBG("Reporting that rotation id %" PRIu64
" of session %s is still pending",
4637 rotation_id
, session
->name
);
4639 case LTTNG_ROTATION_STATE_COMPLETED
:
4641 char *current_tracing_path_reply
;
4642 size_t current_tracing_path_reply_len
;
4644 switch (session_get_consumer_destination_type(session
)) {
4645 case CONSUMER_DST_LOCAL
:
4646 current_tracing_path_reply
=
4647 info_return
->location
.local
.absolute_path
;
4648 current_tracing_path_reply_len
=
4649 sizeof(info_return
->location
.local
.absolute_path
);
4650 info_return
->location_type
=
4651 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
4653 case CONSUMER_DST_NET
:
4654 current_tracing_path_reply
=
4655 info_return
->location
.relay
.relative_path
;
4656 current_tracing_path_reply_len
=
4657 sizeof(info_return
->location
.relay
.relative_path
);
4658 /* Currently the only supported relay protocol. */
4659 info_return
->location
.relay
.protocol
=
4660 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
4662 ret
= lttng_strncpy(info_return
->location
.relay
.host
,
4663 session_get_net_consumer_hostname(session
),
4664 sizeof(info_return
->location
.relay
.host
));
4666 ERR("Failed to host name to rotate_get_info reply");
4667 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4668 ret
= -LTTNG_ERR_UNK
;
4672 session_get_net_consumer_ports(session
,
4673 &info_return
->location
.relay
.ports
.control
,
4674 &info_return
->location
.relay
.ports
.data
);
4675 info_return
->location_type
=
4676 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
4681 ret
= lttng_strncpy(current_tracing_path_reply
,
4682 session
->rotation_chunk
.current_rotate_path
,
4683 current_tracing_path_reply_len
);
4685 ERR("Failed to copy current tracing path to rotate_get_info reply");
4686 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4687 ret
= -LTTNG_ERR_UNK
;
4693 case LTTNG_ROTATION_STATE_ERROR
:
4694 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session %s",
4695 rotation_id
, session
->name
);
4701 info_return
->status
= (int32_t) session
->rotation_state
;
4708 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
4710 * Configure the automatic rotation parameters.
4711 * Set to -1ULL to disable them.
4713 * Return 0 on success or else an LTTNG_ERR code.
4715 int cmd_rotation_set_schedule(struct ltt_session
*session
,
4716 uint64_t timer_us
, uint64_t size
,
4717 struct notification_thread_handle
*notification_thread_handle
)
4723 DBG("Cmd rotate set schedule session %s", session
->name
);
4725 if (session
->live_timer
|| session
->snapshot_mode
||
4726 !session
->output_traces
) {
4727 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4731 /* Trying to override an already active timer. */
4732 if (timer_us
&& timer_us
!= -1ULL && session
->rotate_timer_period
) {
4733 ret
= LTTNG_ERR_ROTATION_TIMER_SET
;
4735 /* Trying to disable an inactive timer. */
4736 } else if (timer_us
== -1ULL && !session
->rotate_timer_period
) {
4737 ret
= LTTNG_ERR_ROTATION_NO_TIMER_SET
;
4741 if (size
&& size
!= -1ULL && session
->rotate_size
) {
4742 ret
= LTTNG_ERR_ROTATION_SIZE_SET
;
4744 } else if (size
== -1ULL && !session
->rotate_size
) {
4745 ret
= LTTNG_ERR_ROTATION_NO_SIZE_SET
;
4749 if (timer_us
&& !session
->rotate_timer_period
) {
4750 if (timer_us
> UINT_MAX
) {
4751 ret
= LTTNG_ERR_INVALID
;
4755 session
->rotate_timer_period
= timer_us
;
4757 * Only start the timer if the session is active, otherwise
4758 * it will be started when the session starts.
4760 if (session
->active
) {
4761 ret
= sessiond_rotate_timer_start(session
, timer_us
);
4763 ERR("Failed to enable rotate timer");
4764 ret
= LTTNG_ERR_UNK
;
4768 } else if (timer_us
== -1ULL && session
->rotate_timer_period
> 0) {
4769 sessiond_rotate_timer_stop(session
);
4770 session
->rotate_timer_period
= 0;
4774 if (size
== -1ULL) {
4775 ret
= unsubscribe_session_consumed_size_rotation(session
,
4776 notification_thread_handle
);
4778 ret
= LTTNG_ERR_UNK
;
4781 session
->rotate_size
= 0;
4783 ret
= subscribe_session_consumed_size_rotation(session
,
4784 size
, notification_thread_handle
);
4786 PERROR("Subscribe to session usage");
4787 ret
= LTTNG_ERR_UNK
;
4790 session
->rotate_size
= size
;
4803 * Command ROTATE_GET_CURRENT_PATH from the lttng-ctl library.
4805 * Configure the automatic rotation parameters.
4806 * Set to -1ULL to disable them.
4808 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4810 int cmd_session_get_current_output(struct ltt_session
*session
,
4811 struct lttng_session_get_current_output_return
*output_return
)
4816 if (!session
->snapshot_mode
) {
4817 if (session
->rotate_count
== 0) {
4818 if (session
->kernel_session
) {
4819 path
= session_get_base_path(session
);
4820 } else if (session
->ust_session
) {
4821 path
= session_get_base_path(session
);
4827 path
= session
->rotation_chunk
.active_tracing_path
;
4831 * A snapshot session does not have a "current" trace archive
4837 DBG("Cmd get current output for session %s, returning %s",
4838 session
->name
, path
);
4840 ret
= lttng_strncpy(output_return
->path
,
4842 sizeof(output_return
->path
));
4844 ERR("Failed to copy trace output path to session get current output command reply");
4845 ret
= -LTTNG_ERR_UNK
;
4855 * Init command subsystem.
4860 * Set network sequence index to 1 for streams to match a relayd
4861 * socket on the consumer side.
4863 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4864 relayd_net_seq_idx
= 1;
4865 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4867 DBG("Command subsystem initialized");