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"
58 #include "agent-thread.h"
63 * Used to keep a unique index for each relayd socket created where this value
64 * is associated with streams on the consumer so it can match the right relayd
65 * to send to. It must be accessed with the relayd_net_seq_idx_lock
68 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
69 static uint64_t relayd_net_seq_idx
;
71 static int validate_ust_event_name(const char *);
72 static int cmd_enable_event_internal(struct ltt_session
*session
,
73 struct lttng_domain
*domain
,
74 char *channel_name
, struct lttng_event
*event
,
75 char *filter_expression
,
76 struct lttng_filter_bytecode
*filter
,
77 struct lttng_event_exclusion
*exclusion
,
81 * Create a session path used by list_lttng_sessions for the case that the
82 * session consumer is on the network.
84 static int build_network_session_path(char *dst
, size_t size
,
85 struct ltt_session
*session
)
87 int ret
, kdata_port
, udata_port
;
88 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
89 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
94 memset(tmp_urls
, 0, sizeof(tmp_urls
));
95 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
97 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
99 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
100 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
101 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
104 if (session
->ust_session
&& session
->ust_session
->consumer
) {
105 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
106 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
109 if (uuri
== NULL
&& kuri
== NULL
) {
110 uri
= &session
->consumer
->dst
.net
.control
;
111 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
112 } else if (kuri
&& uuri
) {
113 ret
= uri_compare(kuri
, uuri
);
117 /* Build uuri URL string */
118 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
125 } else if (kuri
&& uuri
== NULL
) {
127 } else if (uuri
&& kuri
== NULL
) {
131 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
137 * Do we have a UST url set. If yes, this means we have both kernel and UST
140 if (*tmp_uurl
!= '\0') {
141 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
142 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
145 if (kuri
|| (!kuri
&& !uuri
)) {
148 /* No kernel URI, use the UST port. */
151 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
159 * Get run-time attributes if the session has been started (discarded events,
162 static int get_kernel_runtime_stats(struct ltt_session
*session
,
163 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
164 uint64_t *lost_packets
)
168 if (!session
->has_been_started
) {
170 *discarded_events
= 0;
175 ret
= consumer_get_discarded_events(session
->id
, kchan
->key
,
176 session
->kernel_session
->consumer
,
182 ret
= consumer_get_lost_packets(session
->id
, kchan
->key
,
183 session
->kernel_session
->consumer
,
194 * Get run-time attributes if the session has been started (discarded events,
197 static int get_ust_runtime_stats(struct ltt_session
*session
,
198 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
199 uint64_t *lost_packets
)
202 struct ltt_ust_session
*usess
;
204 if (!discarded_events
|| !lost_packets
) {
209 usess
= session
->ust_session
;
210 assert(discarded_events
);
211 assert(lost_packets
);
213 if (!usess
|| !session
->has_been_started
) {
214 *discarded_events
= 0;
220 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
221 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
222 &usess
->buffer_reg_uid_list
,
223 usess
->consumer
, uchan
->id
,
224 uchan
->attr
.overwrite
,
227 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
228 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
229 uchan
, usess
->consumer
,
230 uchan
->attr
.overwrite
,
236 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
237 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
239 ERR("Unsupported buffer type");
250 * Fill lttng_channel array of all channels.
252 static ssize_t
list_lttng_channels(enum lttng_domain_type domain
,
253 struct ltt_session
*session
, struct lttng_channel
*channels
,
254 struct lttng_channel_extended
*chan_exts
)
257 struct ltt_kernel_channel
*kchan
;
259 DBG("Listing channels for session %s", session
->name
);
262 case LTTNG_DOMAIN_KERNEL
:
263 /* Kernel channels */
264 if (session
->kernel_session
!= NULL
) {
265 cds_list_for_each_entry(kchan
,
266 &session
->kernel_session
->channel_list
.head
, list
) {
267 uint64_t discarded_events
, lost_packets
;
268 struct lttng_channel_extended
*extended
;
270 extended
= (struct lttng_channel_extended
*)
271 kchan
->channel
->attr
.extended
.ptr
;
273 ret
= get_kernel_runtime_stats(session
, kchan
,
274 &discarded_events
, &lost_packets
);
278 /* Copy lttng_channel struct to array */
279 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
280 channels
[i
].enabled
= kchan
->enabled
;
281 chan_exts
[i
].discarded_events
=
283 chan_exts
[i
].lost_packets
= lost_packets
;
284 chan_exts
[i
].monitor_timer_interval
=
285 extended
->monitor_timer_interval
;
286 chan_exts
[i
].blocking_timeout
= 0;
291 case LTTNG_DOMAIN_UST
:
293 struct lttng_ht_iter iter
;
294 struct ltt_ust_channel
*uchan
;
297 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
298 &iter
.iter
, uchan
, node
.node
) {
299 uint64_t discarded_events
= 0, lost_packets
= 0;
301 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
302 LTTNG_SYMBOL_NAME_LEN
)) {
305 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
306 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
307 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
308 channels
[i
].attr
.switch_timer_interval
=
309 uchan
->attr
.switch_timer_interval
;
310 channels
[i
].attr
.read_timer_interval
=
311 uchan
->attr
.read_timer_interval
;
312 channels
[i
].enabled
= uchan
->enabled
;
313 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
314 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
317 * Map enum lttng_ust_output to enum lttng_event_output.
319 switch (uchan
->attr
.output
) {
321 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
325 * LTTNG_UST_MMAP is the only supported UST
332 chan_exts
[i
].monitor_timer_interval
=
333 uchan
->monitor_timer_interval
;
334 chan_exts
[i
].blocking_timeout
=
335 uchan
->attr
.u
.s
.blocking_timeout
;
337 ret
= get_ust_runtime_stats(session
, uchan
,
338 &discarded_events
, &lost_packets
);
342 chan_exts
[i
].discarded_events
= discarded_events
;
343 chan_exts
[i
].lost_packets
= lost_packets
;
355 return -LTTNG_ERR_FATAL
;
361 static void increment_extended_len(const char *filter_expression
,
362 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
364 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
366 if (filter_expression
) {
367 *extended_len
+= strlen(filter_expression
) + 1;
371 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
375 static void append_extended_info(const char *filter_expression
,
376 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
378 struct lttcomm_event_extended_header extended_header
;
379 size_t filter_len
= 0;
380 size_t nb_exclusions
= 0;
382 if (filter_expression
) {
383 filter_len
= strlen(filter_expression
) + 1;
387 nb_exclusions
= exclusion
->count
;
390 /* Set header fields */
391 extended_header
.filter_len
= filter_len
;
392 extended_header
.nb_exclusions
= nb_exclusions
;
395 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
396 *extended_at
+= sizeof(extended_header
);
398 /* Copy filter string */
399 if (filter_expression
) {
400 memcpy(*extended_at
, filter_expression
, filter_len
);
401 *extended_at
+= filter_len
;
404 /* Copy exclusion names */
406 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
408 memcpy(*extended_at
, &exclusion
->names
, len
);
414 * Create a list of agent domain events.
416 * Return number of events in list on success or else a negative value.
418 static int list_lttng_agent_events(struct agent
*agt
,
419 struct lttng_event
**events
, size_t *total_size
)
422 unsigned int nb_event
= 0;
423 struct agent_event
*event
;
424 struct lttng_event
*tmp_events
;
425 struct lttng_ht_iter iter
;
426 size_t extended_len
= 0;
432 DBG3("Listing agent events");
435 nb_event
= lttng_ht_get_count(agt
->events
);
443 /* Compute required extended infos size */
444 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
447 * This is only valid because the commands which add events are
448 * processed in the same thread as the listing.
451 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
452 increment_extended_len(event
->filter_expression
, NULL
,
457 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
458 tmp_events
= zmalloc(*total_size
);
460 PERROR("zmalloc agent events session");
461 ret
= -LTTNG_ERR_FATAL
;
465 extended_at
= ((uint8_t *) tmp_events
) +
466 nb_event
* sizeof(struct lttng_event
);
469 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
470 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
471 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
472 tmp_events
[i
].enabled
= event
->enabled
;
473 tmp_events
[i
].loglevel
= event
->loglevel_value
;
474 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
477 /* Append extended info */
478 append_extended_info(event
->filter_expression
, NULL
,
483 *events
= tmp_events
;
487 assert(nb_event
== i
);
492 * Create a list of ust global domain events.
494 static int list_lttng_ust_global_events(char *channel_name
,
495 struct ltt_ust_domain_global
*ust_global
,
496 struct lttng_event
**events
, size_t *total_size
)
499 unsigned int nb_event
= 0;
500 struct lttng_ht_iter iter
;
501 struct lttng_ht_node_str
*node
;
502 struct ltt_ust_channel
*uchan
;
503 struct ltt_ust_event
*uevent
;
504 struct lttng_event
*tmp
;
505 size_t extended_len
= 0;
508 DBG("Listing UST global events for channel %s", channel_name
);
512 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
513 node
= lttng_ht_iter_get_node_str(&iter
);
515 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
519 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
521 nb_event
= lttng_ht_get_count(uchan
->events
);
528 DBG3("Listing UST global %d events", nb_event
);
530 /* Compute required extended infos size */
531 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
532 if (uevent
->internal
) {
537 increment_extended_len(uevent
->filter_expression
,
538 uevent
->exclusion
, &extended_len
);
541 /* All events are internal, skip. */
547 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
548 tmp
= zmalloc(*total_size
);
550 ret
= -LTTNG_ERR_FATAL
;
554 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
556 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
557 if (uevent
->internal
) {
558 /* This event should remain hidden from clients */
561 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
562 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
563 tmp
[i
].enabled
= uevent
->enabled
;
565 switch (uevent
->attr
.instrumentation
) {
566 case LTTNG_UST_TRACEPOINT
:
567 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
569 case LTTNG_UST_PROBE
:
570 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
572 case LTTNG_UST_FUNCTION
:
573 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
577 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
578 switch (uevent
->attr
.loglevel_type
) {
579 case LTTNG_UST_LOGLEVEL_ALL
:
580 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
582 case LTTNG_UST_LOGLEVEL_RANGE
:
583 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
585 case LTTNG_UST_LOGLEVEL_SINGLE
:
586 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
589 if (uevent
->filter
) {
592 if (uevent
->exclusion
) {
593 tmp
[i
].exclusion
= 1;
597 /* Append extended info */
598 append_extended_info(uevent
->filter_expression
,
599 uevent
->exclusion
, &extended_at
);
610 * Fill lttng_event array of all kernel events in the channel.
612 static int list_lttng_kernel_events(char *channel_name
,
613 struct ltt_kernel_session
*kernel_session
,
614 struct lttng_event
**events
, size_t *total_size
)
617 unsigned int nb_event
;
618 struct ltt_kernel_event
*event
;
619 struct ltt_kernel_channel
*kchan
;
620 size_t extended_len
= 0;
623 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
625 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
629 nb_event
= kchan
->event_count
;
631 DBG("Listing events for channel %s", kchan
->channel
->name
);
639 /* Compute required extended infos size */
640 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
641 increment_extended_len(event
->filter_expression
, NULL
,
645 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
646 *events
= zmalloc(*total_size
);
647 if (*events
== NULL
) {
648 ret
= LTTNG_ERR_FATAL
;
652 extended_at
= ((void *) *events
) +
653 nb_event
* sizeof(struct lttng_event
);
655 /* Kernel channels */
656 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
657 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
658 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
659 (*events
)[i
].enabled
= event
->enabled
;
660 (*events
)[i
].filter
=
661 (unsigned char) !!event
->filter_expression
;
663 switch (event
->event
->instrumentation
) {
664 case LTTNG_KERNEL_TRACEPOINT
:
665 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
667 case LTTNG_KERNEL_KRETPROBE
:
668 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
669 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
670 sizeof(struct lttng_kernel_kprobe
));
672 case LTTNG_KERNEL_KPROBE
:
673 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
674 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
675 sizeof(struct lttng_kernel_kprobe
));
677 case LTTNG_KERNEL_FUNCTION
:
678 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
679 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
680 sizeof(struct lttng_kernel_function
));
682 case LTTNG_KERNEL_NOOP
:
683 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
685 case LTTNG_KERNEL_SYSCALL
:
686 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
688 case LTTNG_KERNEL_ALL
:
694 /* Append extended info */
695 append_extended_info(event
->filter_expression
, NULL
,
703 /* Negate the error code to differentiate the size from an error */
708 * Add URI so the consumer output object. Set the correct path depending on the
709 * domain adding the default trace directory.
711 static int add_uri_to_consumer(struct consumer_output
*consumer
,
712 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
713 const char *session_name
)
716 const char *default_trace_dir
;
720 if (consumer
== NULL
) {
721 DBG("No consumer detected. Don't add URI. Stopping.");
722 ret
= LTTNG_ERR_NO_CONSUMER
;
727 case LTTNG_DOMAIN_KERNEL
:
728 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
730 case LTTNG_DOMAIN_UST
:
731 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
735 * This case is possible is we try to add the URI to the global tracing
736 * session consumer object which in this case there is no subdir.
738 default_trace_dir
= "";
741 switch (uri
->dtype
) {
744 DBG2("Setting network URI to consumer");
746 if (consumer
->type
== CONSUMER_DST_NET
) {
747 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
748 consumer
->dst
.net
.control_isset
) ||
749 (uri
->stype
== LTTNG_STREAM_DATA
&&
750 consumer
->dst
.net
.data_isset
)) {
751 ret
= LTTNG_ERR_URL_EXIST
;
755 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
758 consumer
->type
= CONSUMER_DST_NET
;
760 /* Set URI into consumer output object */
761 ret
= consumer_set_network_uri(consumer
, uri
);
765 } else if (ret
== 1) {
767 * URI was the same in the consumer so we do not append the subdir
768 * again so to not duplicate output dir.
774 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
775 ret
= consumer_set_subdir(consumer
, session_name
);
777 ret
= LTTNG_ERR_FATAL
;
782 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
783 /* On a new subdir, reappend the default trace dir. */
784 strncat(consumer
->subdir
, default_trace_dir
,
785 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
786 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
791 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
792 memset(consumer
->dst
.session_root_path
, 0,
793 sizeof(consumer
->dst
.session_root_path
));
794 /* Explicit length checks for strcpy and strcat. */
795 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
796 >= sizeof(consumer
->dst
.session_root_path
)) {
797 ret
= LTTNG_ERR_FATAL
;
800 strcpy(consumer
->dst
.session_root_path
, uri
->dst
.path
);
801 /* Append default trace dir */
802 strcat(consumer
->dst
.session_root_path
, default_trace_dir
);
803 /* Flag consumer as local. */
804 consumer
->type
= CONSUMER_DST_LOCAL
;
815 * Init tracing by creating trace directory and sending fds kernel consumer.
817 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
820 struct lttng_ht_iter iter
;
821 struct consumer_socket
*socket
;
827 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
828 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
830 pthread_mutex_lock(socket
->lock
);
831 ret
= kernel_consumer_send_session(socket
, session
);
832 pthread_mutex_unlock(socket
->lock
);
834 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
846 * Create a socket to the relayd using the URI.
848 * On success, the relayd_sock pointer is set to the created socket.
849 * Else, it's stays untouched and a lttcomm error code is returned.
851 static int create_connect_relayd(struct lttng_uri
*uri
,
852 struct lttcomm_relayd_sock
**relayd_sock
,
853 struct consumer_output
*consumer
)
856 struct lttcomm_relayd_sock
*rsock
;
858 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
859 RELAYD_VERSION_COMM_MINOR
);
861 ret
= LTTNG_ERR_FATAL
;
866 * Connect to relayd so we can proceed with a session creation. This call
867 * can possibly block for an arbitrary amount of time to set the health
868 * state to be in poll execution.
871 ret
= relayd_connect(rsock
);
874 ERR("Unable to reach lttng-relayd");
875 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
879 /* Create socket for control stream. */
880 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
881 DBG3("Creating relayd stream socket from URI");
883 /* Check relayd version */
884 ret
= relayd_version_check(rsock
);
885 if (ret
== LTTNG_ERR_RELAYD_VERSION_FAIL
) {
887 } else if (ret
< 0) {
888 ERR("Unable to reach lttng-relayd");
889 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
892 consumer
->relay_major_version
= rsock
->major
;
893 consumer
->relay_minor_version
= rsock
->minor
;
894 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
895 DBG3("Creating relayd data socket from URI");
897 /* Command is not valid */
898 ERR("Relayd invalid stream type: %d", uri
->stype
);
899 ret
= LTTNG_ERR_INVALID
;
903 *relayd_sock
= rsock
;
908 /* The returned value is not useful since we are on an error path. */
909 (void) relayd_close(rsock
);
917 * Connect to the relayd using URI and send the socket to the right consumer.
919 * The consumer socket lock must be held by the caller.
921 static int send_consumer_relayd_socket(unsigned int session_id
,
922 struct lttng_uri
*relayd_uri
,
923 struct consumer_output
*consumer
,
924 struct consumer_socket
*consumer_sock
,
925 char *session_name
, char *hostname
, int session_live_timer
)
928 struct lttcomm_relayd_sock
*rsock
= NULL
;
930 /* Connect to relayd and make version check if uri is the control. */
931 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
932 if (ret
!= LTTNG_OK
) {
933 goto relayd_comm_error
;
937 /* Set the network sequence index if not set. */
938 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
939 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
941 * Increment net_seq_idx because we are about to transfer the
942 * new relayd socket to the consumer.
943 * Assign unique key so the consumer can match streams.
945 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
946 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
949 /* Send relayd socket to consumer. */
950 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
951 relayd_uri
->stype
, session_id
,
952 session_name
, hostname
, session_live_timer
);
954 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
958 /* Flag that the corresponding socket was sent. */
959 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
960 consumer_sock
->control_sock_sent
= 1;
961 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
962 consumer_sock
->data_sock_sent
= 1;
968 * Close socket which was dup on the consumer side. The session daemon does
969 * NOT keep track of the relayd socket(s) once transfer to the consumer.
973 if (ret
!= LTTNG_OK
) {
975 * The consumer output for this session should not be used anymore
976 * since the relayd connection failed thus making any tracing or/and
977 * streaming not usable.
979 consumer
->enabled
= 0;
981 (void) relayd_close(rsock
);
989 * Send both relayd sockets to a specific consumer and domain. This is a
990 * helper function to facilitate sending the information to the consumer for a
993 * The consumer socket lock must be held by the caller.
995 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
996 unsigned int session_id
, struct consumer_output
*consumer
,
997 struct consumer_socket
*sock
, char *session_name
,
998 char *hostname
, int session_live_timer
)
1005 /* Sending control relayd socket. */
1006 if (!sock
->control_sock_sent
) {
1007 ret
= send_consumer_relayd_socket(session_id
,
1008 &consumer
->dst
.net
.control
, consumer
, sock
,
1009 session_name
, hostname
, session_live_timer
);
1010 if (ret
!= LTTNG_OK
) {
1015 /* Sending data relayd socket. */
1016 if (!sock
->data_sock_sent
) {
1017 ret
= send_consumer_relayd_socket(session_id
,
1018 &consumer
->dst
.net
.data
, consumer
, sock
,
1019 session_name
, hostname
, session_live_timer
);
1020 if (ret
!= LTTNG_OK
) {
1030 * Setup relayd connections for a tracing session. First creates the socket to
1031 * the relayd and send them to the right domain consumer. Consumer type MUST be
1034 int cmd_setup_relayd(struct ltt_session
*session
)
1037 struct ltt_ust_session
*usess
;
1038 struct ltt_kernel_session
*ksess
;
1039 struct consumer_socket
*socket
;
1040 struct lttng_ht_iter iter
;
1044 usess
= session
->ust_session
;
1045 ksess
= session
->kernel_session
;
1047 DBG("Setting relayd for session %s", session
->name
);
1051 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1052 && usess
->consumer
->enabled
) {
1053 /* For each consumer socket, send relayd sockets */
1054 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1055 socket
, node
.node
) {
1056 pthread_mutex_lock(socket
->lock
);
1057 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1058 usess
->consumer
, socket
,
1059 session
->name
, session
->hostname
,
1060 session
->live_timer
);
1061 pthread_mutex_unlock(socket
->lock
);
1062 if (ret
!= LTTNG_OK
) {
1065 /* Session is now ready for network streaming. */
1066 session
->net_handle
= 1;
1068 session
->consumer
->relay_major_version
=
1069 usess
->consumer
->relay_major_version
;
1070 session
->consumer
->relay_minor_version
=
1071 usess
->consumer
->relay_minor_version
;
1074 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1075 && ksess
->consumer
->enabled
) {
1076 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1077 socket
, node
.node
) {
1078 pthread_mutex_lock(socket
->lock
);
1079 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1080 ksess
->consumer
, socket
,
1081 session
->name
, session
->hostname
,
1082 session
->live_timer
);
1083 pthread_mutex_unlock(socket
->lock
);
1084 if (ret
!= LTTNG_OK
) {
1087 /* Session is now ready for network streaming. */
1088 session
->net_handle
= 1;
1090 session
->consumer
->relay_major_version
=
1091 ksess
->consumer
->relay_major_version
;
1092 session
->consumer
->relay_minor_version
=
1093 ksess
->consumer
->relay_minor_version
;
1102 * Start a kernel session by opening all necessary streams.
1104 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1107 struct ltt_kernel_channel
*kchan
;
1109 /* Open kernel metadata */
1110 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1111 ret
= kernel_open_metadata(ksess
);
1113 ret
= LTTNG_ERR_KERN_META_FAIL
;
1118 /* Open kernel metadata stream */
1119 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1120 ret
= kernel_open_metadata_stream(ksess
);
1122 ERR("Kernel create metadata stream failed");
1123 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1128 /* For each channel */
1129 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1130 if (kchan
->stream_count
== 0) {
1131 ret
= kernel_open_channel_stream(kchan
);
1133 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1136 /* Update the stream global counter */
1137 ksess
->stream_count_global
+= ret
;
1141 /* Setup kernel consumer socket and send fds to it */
1142 ret
= init_kernel_tracing(ksess
);
1144 ret
= LTTNG_ERR_KERN_START_FAIL
;
1148 /* This start the kernel tracing */
1149 ret
= kernel_start_session(ksess
);
1151 ret
= LTTNG_ERR_KERN_START_FAIL
;
1155 /* Quiescent wait after starting trace */
1156 kernel_wait_quiescent(wpipe
);
1167 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1169 int cmd_disable_channel(struct ltt_session
*session
,
1170 enum lttng_domain_type domain
, char *channel_name
)
1173 struct ltt_ust_session
*usess
;
1175 usess
= session
->ust_session
;
1180 case LTTNG_DOMAIN_KERNEL
:
1182 ret
= channel_kernel_disable(session
->kernel_session
,
1184 if (ret
!= LTTNG_OK
) {
1188 kernel_wait_quiescent(kernel_tracer_fd
);
1191 case LTTNG_DOMAIN_UST
:
1193 struct ltt_ust_channel
*uchan
;
1194 struct lttng_ht
*chan_ht
;
1196 chan_ht
= usess
->domain_global
.channels
;
1198 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1199 if (uchan
== NULL
) {
1200 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1204 ret
= channel_ust_disable(usess
, uchan
);
1205 if (ret
!= LTTNG_OK
) {
1211 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1223 * Command LTTNG_TRACK_ID processed by the client thread.
1225 * Called with session lock held.
1227 int cmd_track_id(struct ltt_session
*session
,
1228 enum lttng_tracker_type tracker_type
,
1229 enum lttng_domain_type domain
,
1230 struct lttng_tracker_id
*id
)
1237 case LTTNG_DOMAIN_KERNEL
:
1239 struct ltt_kernel_session
*ksess
;
1241 ksess
= session
->kernel_session
;
1243 ret
= kernel_track_id(tracker_type
, ksess
, id
);
1244 if (ret
!= LTTNG_OK
) {
1248 kernel_wait_quiescent(kernel_tracer_fd
);
1251 case LTTNG_DOMAIN_UST
:
1253 struct ltt_ust_session
*usess
;
1255 usess
= session
->ust_session
;
1257 ret
= trace_ust_track_id(tracker_type
, usess
, id
);
1258 if (ret
!= LTTNG_OK
) {
1264 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1276 * Command LTTNG_UNTRACK_ID processed by the client thread.
1278 * Called with session lock held.
1280 int cmd_untrack_id(struct ltt_session
*session
,
1281 enum lttng_tracker_type tracker_type
,
1282 enum lttng_domain_type domain
,
1283 struct lttng_tracker_id
*id
)
1290 case LTTNG_DOMAIN_KERNEL
:
1292 struct ltt_kernel_session
*ksess
;
1294 ksess
= session
->kernel_session
;
1296 ret
= kernel_untrack_id(tracker_type
, ksess
, id
);
1297 if (ret
!= LTTNG_OK
) {
1301 kernel_wait_quiescent(kernel_tracer_fd
);
1304 case LTTNG_DOMAIN_UST
:
1306 struct ltt_ust_session
*usess
;
1308 usess
= session
->ust_session
;
1310 ret
= trace_ust_untrack_id(tracker_type
, usess
, id
);
1311 if (ret
!= LTTNG_OK
) {
1317 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1329 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1331 * The wpipe arguments is used as a notifier for the kernel thread.
1333 int cmd_enable_channel(struct ltt_session
*session
,
1334 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1337 struct ltt_ust_session
*usess
= session
->ust_session
;
1338 struct lttng_ht
*chan_ht
;
1345 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1347 /* Validate channel name */
1348 if (attr
->name
[0] == '.' ||
1349 memchr(attr
->name
, '/', len
) != NULL
) {
1350 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1354 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1359 * Don't try to enable a channel if the session has been started at
1360 * some point in time before. The tracer does not allow it.
1362 if (session
->has_been_started
) {
1363 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1368 * If the session is a live session, remove the switch timer, the
1369 * live timer does the same thing but sends also synchronisation
1370 * beacons for inactive streams.
1372 if (session
->live_timer
> 0) {
1373 attr
->attr
.live_timer_interval
= session
->live_timer
;
1374 attr
->attr
.switch_timer_interval
= 0;
1377 /* Check for feature support */
1378 switch (domain
->type
) {
1379 case LTTNG_DOMAIN_KERNEL
:
1381 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd
) != 1) {
1382 /* Sampling position of buffer is not supported */
1383 WARN("Kernel tracer does not support buffer monitoring. "
1384 "Setting the monitor interval timer to 0 "
1385 "(disabled) for channel '%s' of session '%s'",
1386 attr
-> name
, session
->name
);
1387 lttng_channel_set_monitor_timer_interval(attr
, 0);
1391 case LTTNG_DOMAIN_UST
:
1393 case LTTNG_DOMAIN_JUL
:
1394 case LTTNG_DOMAIN_LOG4J
:
1395 case LTTNG_DOMAIN_PYTHON
:
1396 if (!agent_tracing_is_enabled()) {
1397 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1398 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1403 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1407 switch (domain
->type
) {
1408 case LTTNG_DOMAIN_KERNEL
:
1410 struct ltt_kernel_channel
*kchan
;
1412 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1413 session
->kernel_session
);
1414 if (kchan
== NULL
) {
1415 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1416 if (attr
->name
[0] != '\0') {
1417 session
->kernel_session
->has_non_default_channel
= 1;
1420 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1423 if (ret
!= LTTNG_OK
) {
1427 kernel_wait_quiescent(kernel_tracer_fd
);
1430 case LTTNG_DOMAIN_UST
:
1431 case LTTNG_DOMAIN_JUL
:
1432 case LTTNG_DOMAIN_LOG4J
:
1433 case LTTNG_DOMAIN_PYTHON
:
1435 struct ltt_ust_channel
*uchan
;
1440 * Current agent implementation limitations force us to allow
1441 * only one channel at once in "agent" subdomains. Each
1442 * subdomain has a default channel name which must be strictly
1445 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1446 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1447 LTTNG_SYMBOL_NAME_LEN
)) {
1448 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1451 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1452 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1453 LTTNG_SYMBOL_NAME_LEN
)) {
1454 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1457 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1458 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1459 LTTNG_SYMBOL_NAME_LEN
)) {
1460 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1465 chan_ht
= usess
->domain_global
.channels
;
1467 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1468 if (uchan
== NULL
) {
1469 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1470 if (attr
->name
[0] != '\0') {
1471 usess
->has_non_default_channel
= 1;
1474 ret
= channel_ust_enable(usess
, uchan
);
1479 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1490 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1492 int cmd_disable_event(struct ltt_session
*session
,
1493 enum lttng_domain_type domain
, char *channel_name
,
1494 struct lttng_event
*event
)
1499 DBG("Disable event command for event \'%s\'", event
->name
);
1501 event_name
= event
->name
;
1503 /* Error out on unhandled search criteria */
1504 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1505 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1506 ret
= LTTNG_ERR_UNK
;
1513 case LTTNG_DOMAIN_KERNEL
:
1515 struct ltt_kernel_channel
*kchan
;
1516 struct ltt_kernel_session
*ksess
;
1518 ksess
= session
->kernel_session
;
1521 * If a non-default channel has been created in the
1522 * session, explicitely require that -c chan_name needs
1525 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1526 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1530 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1531 if (kchan
== NULL
) {
1532 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1536 switch (event
->type
) {
1537 case LTTNG_EVENT_ALL
:
1538 case LTTNG_EVENT_TRACEPOINT
:
1539 case LTTNG_EVENT_SYSCALL
:
1540 case LTTNG_EVENT_PROBE
:
1541 case LTTNG_EVENT_FUNCTION
:
1542 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1543 if (event_name
[0] == '\0') {
1544 ret
= event_kernel_disable_event(kchan
,
1547 ret
= event_kernel_disable_event(kchan
,
1548 event_name
, event
->type
);
1550 if (ret
!= LTTNG_OK
) {
1555 ret
= LTTNG_ERR_UNK
;
1559 kernel_wait_quiescent(kernel_tracer_fd
);
1562 case LTTNG_DOMAIN_UST
:
1564 struct ltt_ust_channel
*uchan
;
1565 struct ltt_ust_session
*usess
;
1567 usess
= session
->ust_session
;
1569 if (validate_ust_event_name(event_name
)) {
1570 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1575 * If a non-default channel has been created in the
1576 * session, explicitly require that -c chan_name needs
1579 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1580 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1584 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1586 if (uchan
== NULL
) {
1587 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1591 switch (event
->type
) {
1592 case LTTNG_EVENT_ALL
:
1594 * An empty event name means that everything
1595 * should be disabled.
1597 if (event
->name
[0] == '\0') {
1598 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1600 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1603 if (ret
!= LTTNG_OK
) {
1608 ret
= LTTNG_ERR_UNK
;
1612 DBG3("Disable UST event %s in channel %s completed", event_name
,
1616 case LTTNG_DOMAIN_LOG4J
:
1617 case LTTNG_DOMAIN_JUL
:
1618 case LTTNG_DOMAIN_PYTHON
:
1621 struct ltt_ust_session
*usess
= session
->ust_session
;
1625 switch (event
->type
) {
1626 case LTTNG_EVENT_ALL
:
1629 ret
= LTTNG_ERR_UNK
;
1633 agt
= trace_ust_find_agent(usess
, domain
);
1635 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1639 * An empty event name means that everything
1640 * should be disabled.
1642 if (event
->name
[0] == '\0') {
1643 ret
= event_agent_disable_all(usess
, agt
);
1645 ret
= event_agent_disable(usess
, agt
, event_name
);
1647 if (ret
!= LTTNG_OK
) {
1654 ret
= LTTNG_ERR_UND
;
1667 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1669 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1670 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1672 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1673 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1676 * Don't try to add a context if the session has been started at
1677 * some point in time before. The tracer does not allow it and would
1678 * result in a corrupted trace.
1680 if (session
->has_been_started
) {
1681 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1685 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1686 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1687 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1691 case LTTNG_DOMAIN_KERNEL
:
1692 assert(session
->kernel_session
);
1694 if (session
->kernel_session
->channel_count
== 0) {
1695 /* Create default channel */
1696 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1697 if (ret
!= LTTNG_OK
) {
1700 chan_kern_created
= 1;
1702 /* Add kernel context to kernel tracer */
1703 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1704 if (ret
!= LTTNG_OK
) {
1708 case LTTNG_DOMAIN_JUL
:
1709 case LTTNG_DOMAIN_LOG4J
:
1712 * Validate channel name.
1713 * If no channel name is given and the domain is JUL or LOG4J,
1714 * set it to the appropriate domain-specific channel name. If
1715 * a name is provided but does not match the expexted channel
1716 * name, return an error.
1718 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1719 strcmp(channel_name
,
1720 DEFAULT_JUL_CHANNEL_NAME
)) {
1721 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1723 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1724 strcmp(channel_name
,
1725 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1726 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1729 /* break is _not_ missing here. */
1731 case LTTNG_DOMAIN_UST
:
1733 struct ltt_ust_session
*usess
= session
->ust_session
;
1734 unsigned int chan_count
;
1738 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1739 if (chan_count
== 0) {
1740 struct lttng_channel
*attr
;
1741 /* Create default channel */
1742 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1744 ret
= LTTNG_ERR_FATAL
;
1748 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1749 if (ret
!= LTTNG_OK
) {
1753 channel_attr_destroy(attr
);
1754 chan_ust_created
= 1;
1757 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1758 free(app_ctx_provider_name
);
1760 app_ctx_name
= NULL
;
1761 app_ctx_provider_name
= NULL
;
1762 if (ret
!= LTTNG_OK
) {
1768 ret
= LTTNG_ERR_UND
;
1776 if (chan_kern_created
) {
1777 struct ltt_kernel_channel
*kchan
=
1778 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1779 session
->kernel_session
);
1780 /* Created previously, this should NOT fail. */
1782 kernel_destroy_channel(kchan
);
1785 if (chan_ust_created
) {
1786 struct ltt_ust_channel
*uchan
=
1787 trace_ust_find_channel_by_name(
1788 session
->ust_session
->domain_global
.channels
,
1789 DEFAULT_CHANNEL_NAME
);
1790 /* Created previously, this should NOT fail. */
1792 /* Remove from the channel list of the session. */
1793 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1795 trace_ust_destroy_channel(uchan
);
1798 free(app_ctx_provider_name
);
1803 static inline bool name_starts_with(const char *name
, const char *prefix
)
1805 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1807 return !strncmp(name
, prefix
, max_cmp_len
);
1810 /* Perform userspace-specific event name validation */
1811 static int validate_ust_event_name(const char *name
)
1821 * Check name against all internal UST event component namespaces used
1824 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1825 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1826 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1835 * Internal version of cmd_enable_event() with a supplemental
1836 * "internal_event" flag which is used to enable internal events which should
1837 * be hidden from clients. Such events are used in the agent implementation to
1838 * enable the events through which all "agent" events are funeled.
1840 static int _cmd_enable_event(struct ltt_session
*session
,
1841 struct lttng_domain
*domain
,
1842 char *channel_name
, struct lttng_event
*event
,
1843 char *filter_expression
,
1844 struct lttng_filter_bytecode
*filter
,
1845 struct lttng_event_exclusion
*exclusion
,
1846 int wpipe
, bool internal_event
)
1848 int ret
= 0, channel_created
= 0;
1849 struct lttng_channel
*attr
= NULL
;
1853 assert(channel_name
);
1855 /* If we have a filter, we must have its filter expression */
1856 assert(!(!!filter_expression
^ !!filter
));
1858 /* Normalize event name as a globbing pattern */
1859 strutils_normalize_star_glob_pattern(event
->name
);
1861 /* Normalize exclusion names as globbing patterns */
1865 for (i
= 0; i
< exclusion
->count
; i
++) {
1866 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
1868 strutils_normalize_star_glob_pattern(name
);
1872 DBG("Enable event command for event \'%s\'", event
->name
);
1876 switch (domain
->type
) {
1877 case LTTNG_DOMAIN_KERNEL
:
1879 struct ltt_kernel_channel
*kchan
;
1882 * If a non-default channel has been created in the
1883 * session, explicitely require that -c chan_name needs
1886 if (session
->kernel_session
->has_non_default_channel
1887 && channel_name
[0] == '\0') {
1888 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1892 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1893 session
->kernel_session
);
1894 if (kchan
== NULL
) {
1895 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1896 LTTNG_BUFFER_GLOBAL
);
1898 ret
= LTTNG_ERR_FATAL
;
1901 if (lttng_strncpy(attr
->name
, channel_name
,
1902 sizeof(attr
->name
))) {
1903 ret
= LTTNG_ERR_INVALID
;
1907 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1908 if (ret
!= LTTNG_OK
) {
1911 channel_created
= 1;
1914 /* Get the newly created kernel channel pointer */
1915 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1916 session
->kernel_session
);
1917 if (kchan
== NULL
) {
1918 /* This sould not happen... */
1919 ret
= LTTNG_ERR_FATAL
;
1923 switch (event
->type
) {
1924 case LTTNG_EVENT_ALL
:
1926 char *filter_expression_a
= NULL
;
1927 struct lttng_filter_bytecode
*filter_a
= NULL
;
1930 * We need to duplicate filter_expression and filter,
1931 * because ownership is passed to first enable
1934 if (filter_expression
) {
1935 filter_expression_a
= strdup(filter_expression
);
1936 if (!filter_expression_a
) {
1937 ret
= LTTNG_ERR_FATAL
;
1942 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1944 free(filter_expression_a
);
1945 ret
= LTTNG_ERR_FATAL
;
1948 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1950 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1951 ret
= event_kernel_enable_event(kchan
, event
,
1952 filter_expression
, filter
);
1953 /* We have passed ownership */
1954 filter_expression
= NULL
;
1956 if (ret
!= LTTNG_OK
) {
1957 if (channel_created
) {
1958 /* Let's not leak a useless channel. */
1959 kernel_destroy_channel(kchan
);
1961 free(filter_expression_a
);
1965 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1966 ret
= event_kernel_enable_event(kchan
, event
,
1967 filter_expression_a
, filter_a
);
1968 /* We have passed ownership */
1969 filter_expression_a
= NULL
;
1971 if (ret
!= LTTNG_OK
) {
1976 case LTTNG_EVENT_PROBE
:
1977 case LTTNG_EVENT_FUNCTION
:
1978 case LTTNG_EVENT_FUNCTION_ENTRY
:
1979 case LTTNG_EVENT_TRACEPOINT
:
1980 ret
= event_kernel_enable_event(kchan
, event
,
1981 filter_expression
, filter
);
1982 /* We have passed ownership */
1983 filter_expression
= NULL
;
1985 if (ret
!= LTTNG_OK
) {
1986 if (channel_created
) {
1987 /* Let's not leak a useless channel. */
1988 kernel_destroy_channel(kchan
);
1993 case LTTNG_EVENT_SYSCALL
:
1994 ret
= event_kernel_enable_event(kchan
, event
,
1995 filter_expression
, filter
);
1996 /* We have passed ownership */
1997 filter_expression
= NULL
;
1999 if (ret
!= LTTNG_OK
) {
2004 ret
= LTTNG_ERR_UNK
;
2008 kernel_wait_quiescent(kernel_tracer_fd
);
2011 case LTTNG_DOMAIN_UST
:
2013 struct ltt_ust_channel
*uchan
;
2014 struct ltt_ust_session
*usess
= session
->ust_session
;
2019 * If a non-default channel has been created in the
2020 * session, explicitely require that -c chan_name needs
2023 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2024 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2028 /* Get channel from global UST domain */
2029 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2031 if (uchan
== NULL
) {
2032 /* Create default channel */
2033 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2034 usess
->buffer_type
);
2036 ret
= LTTNG_ERR_FATAL
;
2039 if (lttng_strncpy(attr
->name
, channel_name
,
2040 sizeof(attr
->name
))) {
2041 ret
= LTTNG_ERR_INVALID
;
2045 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2046 if (ret
!= LTTNG_OK
) {
2050 /* Get the newly created channel reference back */
2051 uchan
= trace_ust_find_channel_by_name(
2052 usess
->domain_global
.channels
, channel_name
);
2056 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2058 * Don't allow users to add UST events to channels which
2059 * are assigned to a userspace subdomain (JUL, Log4J,
2062 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2066 if (!internal_event
) {
2068 * Ensure the event name is not reserved for internal
2071 ret
= validate_ust_event_name(event
->name
);
2073 WARN("Userspace event name %s failed validation.",
2075 event
->name
: "NULL");
2076 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2081 /* At this point, the session and channel exist on the tracer */
2082 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2083 filter_expression
, filter
, exclusion
,
2085 /* We have passed ownership */
2086 filter_expression
= NULL
;
2089 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2090 goto already_enabled
;
2091 } else if (ret
!= LTTNG_OK
) {
2096 case LTTNG_DOMAIN_LOG4J
:
2097 case LTTNG_DOMAIN_JUL
:
2098 case LTTNG_DOMAIN_PYTHON
:
2100 const char *default_event_name
, *default_chan_name
;
2102 struct lttng_event uevent
;
2103 struct lttng_domain tmp_dom
;
2104 struct ltt_ust_session
*usess
= session
->ust_session
;
2108 if (!agent_tracing_is_enabled()) {
2109 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2110 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2114 agt
= trace_ust_find_agent(usess
, domain
->type
);
2116 agt
= agent_create(domain
->type
);
2118 ret
= LTTNG_ERR_NOMEM
;
2121 agent_add(agt
, usess
->agents
);
2124 /* Create the default tracepoint. */
2125 memset(&uevent
, 0, sizeof(uevent
));
2126 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2127 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2128 default_event_name
= event_get_default_agent_ust_name(
2130 if (!default_event_name
) {
2131 ret
= LTTNG_ERR_FATAL
;
2134 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2135 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2138 * The domain type is changed because we are about to enable the
2139 * default channel and event for the JUL domain that are hardcoded.
2140 * This happens in the UST domain.
2142 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2143 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2145 switch (domain
->type
) {
2146 case LTTNG_DOMAIN_LOG4J
:
2147 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2149 case LTTNG_DOMAIN_JUL
:
2150 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2152 case LTTNG_DOMAIN_PYTHON
:
2153 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2156 /* The switch/case we are in makes this impossible */
2161 char *filter_expression_copy
= NULL
;
2162 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2165 const size_t filter_size
= sizeof(
2166 struct lttng_filter_bytecode
)
2169 filter_copy
= zmalloc(filter_size
);
2171 ret
= LTTNG_ERR_NOMEM
;
2174 memcpy(filter_copy
, filter
, filter_size
);
2176 filter_expression_copy
=
2177 strdup(filter_expression
);
2178 if (!filter_expression
) {
2179 ret
= LTTNG_ERR_NOMEM
;
2182 if (!filter_expression_copy
|| !filter_copy
) {
2183 free(filter_expression_copy
);
2189 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2190 (char *) default_chan_name
,
2191 &uevent
, filter_expression_copy
,
2192 filter_copy
, NULL
, wpipe
);
2195 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2196 goto already_enabled
;
2197 } else if (ret
!= LTTNG_OK
) {
2201 /* The wild card * means that everything should be enabled. */
2202 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2203 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2206 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2210 filter_expression
= NULL
;
2211 if (ret
!= LTTNG_OK
) {
2218 ret
= LTTNG_ERR_UND
;
2226 free(filter_expression
);
2229 channel_attr_destroy(attr
);
2235 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2236 * We own filter, exclusion, and filter_expression.
2238 int cmd_enable_event(struct ltt_session
*session
, 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
, false);
2250 * Enable an event which is internal to LTTng. An internal should
2251 * never be made visible to clients and are immune to checks such as
2254 static int cmd_enable_event_internal(struct ltt_session
*session
,
2255 struct lttng_domain
*domain
,
2256 char *channel_name
, struct lttng_event
*event
,
2257 char *filter_expression
,
2258 struct lttng_filter_bytecode
*filter
,
2259 struct lttng_event_exclusion
*exclusion
,
2262 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2263 filter_expression
, filter
, exclusion
, wpipe
, true);
2267 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2269 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2270 struct lttng_event
**events
)
2273 ssize_t nb_events
= 0;
2276 case LTTNG_DOMAIN_KERNEL
:
2277 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2278 if (nb_events
< 0) {
2279 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2283 case LTTNG_DOMAIN_UST
:
2284 nb_events
= ust_app_list_events(events
);
2285 if (nb_events
< 0) {
2286 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2290 case LTTNG_DOMAIN_LOG4J
:
2291 case LTTNG_DOMAIN_JUL
:
2292 case LTTNG_DOMAIN_PYTHON
:
2293 nb_events
= agent_list_events(events
, domain
);
2294 if (nb_events
< 0) {
2295 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2300 ret
= LTTNG_ERR_UND
;
2307 /* Return negative value to differentiate return code */
2312 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2314 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2315 struct lttng_event_field
**fields
)
2318 ssize_t nb_fields
= 0;
2321 case LTTNG_DOMAIN_UST
:
2322 nb_fields
= ust_app_list_event_fields(fields
);
2323 if (nb_fields
< 0) {
2324 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2328 case LTTNG_DOMAIN_KERNEL
:
2329 default: /* fall-through */
2330 ret
= LTTNG_ERR_UND
;
2337 /* Return negative value to differentiate return code */
2341 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2343 return syscall_table_list(events
);
2347 * Command LTTNG_LIST_TRACKER_IDS processed by the client thread.
2349 * Called with session lock held.
2351 ssize_t
cmd_list_tracker_ids(enum lttng_tracker_type tracker_type
,
2352 struct ltt_session
*session
,
2353 enum lttng_domain_type domain
,
2354 struct lttng_tracker_id
**ids
)
2357 ssize_t nr_pids
= 0;
2360 case LTTNG_DOMAIN_KERNEL
:
2362 struct ltt_kernel_session
*ksess
;
2364 ksess
= session
->kernel_session
;
2365 nr_pids
= kernel_list_tracker_ids(tracker_type
, ksess
, ids
);
2367 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2372 case LTTNG_DOMAIN_UST
:
2374 struct ltt_ust_session
*usess
;
2376 usess
= session
->ust_session
;
2377 nr_pids
= trace_ust_list_tracker_ids(tracker_type
, usess
, ids
);
2379 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2384 case LTTNG_DOMAIN_LOG4J
:
2385 case LTTNG_DOMAIN_JUL
:
2386 case LTTNG_DOMAIN_PYTHON
:
2388 ret
= LTTNG_ERR_UND
;
2395 /* Return negative value to differentiate return code */
2400 int domain_mkdir(const struct consumer_output
*output
,
2401 const struct ltt_session
*session
,
2402 uid_t uid
, gid_t gid
)
2404 struct consumer_socket
*socket
;
2405 struct lttng_ht_iter iter
;
2409 if (!output
|| !output
->socks
) {
2410 ERR("No consumer output found");
2415 path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
2417 ERR("Cannot allocate mkdir path");
2422 ret
= snprintf(path
, LTTNG_PATH_MAX
, "%s%s%s",
2423 session_get_base_path(session
),
2424 output
->chunk_path
, output
->subdir
);
2425 if (ret
< 0 || ret
>= LTTNG_PATH_MAX
) {
2431 DBG("Domain mkdir %s for session %" PRIu64
, path
, session
->id
);
2434 * We have to iterate to find a socket, but we only need to send the
2435 * rename command to one consumer, so we break after the first one.
2437 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
2438 pthread_mutex_lock(socket
->lock
);
2439 ret
= consumer_mkdir(socket
, session
->id
, output
, path
, uid
, gid
);
2440 pthread_mutex_unlock(socket
->lock
);
2442 ERR("Consumer mkdir");
2459 int session_mkdir(const struct ltt_session
*session
)
2462 struct consumer_output
*output
;
2467 * Unsupported feature in lttng-relayd before 2.11, not an error since it
2468 * is only needed for session rotation and the user will get an error
2471 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
2472 session
->consumer
->relay_major_version
== 2 &&
2473 session
->consumer
->relay_minor_version
< 11) {
2478 if (session
->kernel_session
) {
2479 output
= session
->kernel_session
->consumer
;
2480 uid
= session
->kernel_session
->uid
;
2481 gid
= session
->kernel_session
->gid
;
2482 ret
= domain_mkdir(output
, session
, uid
, gid
);
2484 ERR("Mkdir kernel");
2489 if (session
->ust_session
) {
2490 output
= session
->ust_session
->consumer
;
2491 uid
= session
->ust_session
->uid
;
2492 gid
= session
->ust_session
->gid
;
2493 ret
= domain_mkdir(output
, session
, uid
, gid
);
2507 * Command LTTNG_START_TRACE processed by the client thread.
2509 * Called with session mutex held.
2511 int cmd_start_trace(struct ltt_session
*session
)
2514 unsigned long nb_chan
= 0;
2515 struct ltt_kernel_session
*ksession
;
2516 struct ltt_ust_session
*usess
;
2520 /* Ease our life a bit ;) */
2521 ksession
= session
->kernel_session
;
2522 usess
= session
->ust_session
;
2524 /* Is the session already started? */
2525 if (session
->active
) {
2526 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2531 * Starting a session without channel is useless since after that it's not
2532 * possible to enable channel thus inform the client.
2534 if (usess
&& usess
->domain_global
.channels
) {
2535 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2538 nb_chan
+= ksession
->channel_count
;
2541 ret
= LTTNG_ERR_NO_CHANNEL
;
2546 * Record the timestamp of the first time the session is started for
2547 * an eventual session rotation call.
2549 if (!session
->has_been_started
) {
2550 session
->current_chunk_start_ts
= time(NULL
);
2551 if (session
->current_chunk_start_ts
== (time_t) -1) {
2552 PERROR("Failed to retrieve the \"%s\" session's start time",
2554 ret
= LTTNG_ERR_FATAL
;
2557 if (!session
->snapshot_mode
&& session
->output_traces
) {
2558 ret
= session_mkdir(session
);
2560 ERR("Failed to create the session directories");
2561 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2567 /* Kernel tracing */
2568 if (ksession
!= NULL
) {
2569 DBG("Start kernel tracing session %s", session
->name
);
2570 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2571 if (ret
!= LTTNG_OK
) {
2576 /* Flag session that trace should start automatically */
2579 * Even though the start trace might fail, flag this session active so
2580 * other application coming in are started by default.
2584 ret
= ust_app_start_trace_all(usess
);
2586 ret
= LTTNG_ERR_UST_START_FAIL
;
2591 /* Flag this after a successful start. */
2592 session
->has_been_started
= 1;
2593 session
->active
= 1;
2596 * Clear the flag that indicates that a rotation was done while the
2597 * session was stopped.
2599 session
->rotated_after_last_stop
= false;
2601 if (session
->rotate_timer_period
) {
2602 ret
= sessiond_rotate_timer_start(session
,
2603 session
->rotate_timer_period
);
2605 ERR("Failed to enable rotate timer");
2606 ret
= LTTNG_ERR_UNK
;
2618 int rename_active_chunk(struct ltt_session
*session
)
2622 session
->current_archive_id
++;
2625 * The currently active tracing path is now the folder we
2628 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
2629 session
->rotation_chunk
.active_tracing_path
,
2630 sizeof(session
->rotation_chunk
.current_rotate_path
));
2632 ERR("Failed to copy active tracing path");
2636 ret
= rename_complete_chunk(session
, time(NULL
));
2638 ERR("Failed to rename current rotate path");
2643 * We just renamed, the folder, we didn't do an actual rotation, so
2644 * the active tracing path is now the renamed folder and we have to
2645 * restore the rotate count.
2647 ret
= lttng_strncpy(session
->rotation_chunk
.active_tracing_path
,
2648 session
->rotation_chunk
.current_rotate_path
,
2649 sizeof(session
->rotation_chunk
.active_tracing_path
));
2651 ERR("Failed to rename active session chunk tracing path");
2655 session
->current_archive_id
--;
2660 * Command LTTNG_STOP_TRACE processed by the client thread.
2662 int cmd_stop_trace(struct ltt_session
*session
)
2665 struct ltt_kernel_channel
*kchan
;
2666 struct ltt_kernel_session
*ksession
;
2667 struct ltt_ust_session
*usess
;
2668 bool error_occured
= false;
2672 DBG("Begin stop session %s (id %" PRIu64
")", session
->name
, session
->id
);
2674 ksession
= session
->kernel_session
;
2675 usess
= session
->ust_session
;
2677 /* Session is not active. Skip everythong and inform the client. */
2678 if (!session
->active
) {
2679 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2683 if (session
->rotate_relay_pending_timer_enabled
) {
2684 sessiond_timer_rotate_pending_stop(session
);
2687 if (session
->rotate_timer_enabled
) {
2688 sessiond_rotate_timer_stop(session
);
2691 if (session
->current_archive_id
> 0 && !session
->rotate_pending
) {
2692 ret
= rename_active_chunk(session
);
2695 * This error should not prevent the user from stopping
2696 * the session. However, it will be reported at the end.
2698 error_occured
= true;
2703 if (ksession
&& ksession
->active
) {
2704 DBG("Stop kernel tracing");
2706 ret
= kernel_stop_session(ksession
);
2708 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2712 kernel_wait_quiescent(kernel_tracer_fd
);
2714 /* Flush metadata after stopping (if exists) */
2715 if (ksession
->metadata_stream_fd
>= 0) {
2716 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2718 ERR("Kernel metadata flush failed");
2722 /* Flush all buffers after stopping */
2723 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2724 ret
= kernel_flush_buffer(kchan
);
2726 ERR("Kernel flush buffer error");
2730 ksession
->active
= 0;
2731 DBG("Kernel session stopped %s (id %" PRIu64
")", session
->name
,
2735 if (usess
&& usess
->active
) {
2737 * Even though the stop trace might fail, flag this session inactive so
2738 * other application coming in are not started by default.
2742 ret
= ust_app_stop_trace_all(usess
);
2744 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2749 /* Flag inactive after a successful stop. */
2750 session
->active
= 0;
2751 ret
= !error_occured
? LTTNG_OK
: LTTNG_ERR_UNK
;
2758 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2760 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2761 struct lttng_uri
*uris
)
2764 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2765 struct ltt_ust_session
*usess
= session
->ust_session
;
2771 /* Can't set consumer URI if the session is active. */
2772 if (session
->active
) {
2773 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2777 /* Set the "global" consumer URIs */
2778 for (i
= 0; i
< nb_uri
; i
++) {
2779 ret
= add_uri_to_consumer(session
->consumer
,
2780 &uris
[i
], 0, session
->name
);
2781 if (ret
!= LTTNG_OK
) {
2786 /* Set UST session URIs */
2787 if (session
->ust_session
) {
2788 for (i
= 0; i
< nb_uri
; i
++) {
2789 ret
= add_uri_to_consumer(
2790 session
->ust_session
->consumer
,
2791 &uris
[i
], LTTNG_DOMAIN_UST
,
2793 if (ret
!= LTTNG_OK
) {
2799 /* Set kernel session URIs */
2800 if (session
->kernel_session
) {
2801 for (i
= 0; i
< nb_uri
; i
++) {
2802 ret
= add_uri_to_consumer(
2803 session
->kernel_session
->consumer
,
2804 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2806 if (ret
!= LTTNG_OK
) {
2813 * Make sure to set the session in output mode after we set URI since a
2814 * session can be created without URL (thus flagged in no output mode).
2816 session
->output_traces
= 1;
2818 ksess
->output_traces
= 1;
2822 usess
->output_traces
= 1;
2833 * Command LTTNG_CREATE_SESSION processed by the client thread.
2835 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2836 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2839 struct ltt_session
*session
;
2845 * Verify if the session already exist
2847 * XXX: There is no need for the session lock list here since the caller
2848 * (process_client_msg) is holding it. We might want to change that so a
2849 * single command does not lock the entire session list.
2851 session
= session_find_by_name(name
);
2852 if (session
!= NULL
) {
2853 ret
= LTTNG_ERR_EXIST_SESS
;
2857 /* Create tracing session in the registry */
2858 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2859 LTTNG_SOCK_GET_GID_CRED(creds
));
2860 if (ret
!= LTTNG_OK
) {
2865 * Get the newly created session pointer back
2867 * XXX: There is no need for the session lock list here since the caller
2868 * (process_client_msg) is holding it. We might want to change that so a
2869 * single command does not lock the entire session list.
2871 session
= session_find_by_name(name
);
2874 session
->live_timer
= live_timer
;
2875 /* Create default consumer output for the session not yet created. */
2876 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2877 if (session
->consumer
== NULL
) {
2878 ret
= LTTNG_ERR_FATAL
;
2879 goto consumer_error
;
2883 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2884 if (ret
!= LTTNG_OK
) {
2885 goto consumer_error
;
2887 session
->output_traces
= 1;
2889 session
->output_traces
= 0;
2890 DBG2("Session %s created with no output", session
->name
);
2893 session
->consumer
->enabled
= 1;
2898 session_destroy(session
);
2905 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2907 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2908 size_t nb_uri
, lttng_sock_cred
*creds
)
2911 struct ltt_session
*session
;
2912 struct snapshot_output
*new_output
= NULL
;
2918 * Create session in no output mode with URIs set to NULL. The uris we've
2919 * received are for a default snapshot output if one.
2921 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2922 if (ret
!= LTTNG_OK
) {
2926 /* Get the newly created session pointer back. This should NEVER fail. */
2927 session
= session_find_by_name(name
);
2930 /* Flag session for snapshot mode. */
2931 session
->snapshot_mode
= 1;
2933 /* Skip snapshot output creation if no URI is given. */
2938 new_output
= snapshot_output_alloc();
2940 ret
= LTTNG_ERR_NOMEM
;
2941 goto error_snapshot_alloc
;
2944 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2945 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2947 if (ret
== -ENOMEM
) {
2948 ret
= LTTNG_ERR_NOMEM
;
2950 ret
= LTTNG_ERR_INVALID
;
2952 goto error_snapshot
;
2956 snapshot_add_output(&session
->snapshot
, new_output
);
2963 snapshot_output_destroy(new_output
);
2964 error_snapshot_alloc
:
2965 session_destroy(session
);
2971 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2973 * Called with session lock held.
2975 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
,
2976 struct notification_thread_handle
*notification_thread_handle
)
2979 struct ltt_ust_session
*usess
;
2980 struct ltt_kernel_session
*ksess
;
2985 usess
= session
->ust_session
;
2986 ksess
= session
->kernel_session
;
2988 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
2990 if (session
->rotate_relay_pending_timer_enabled
) {
2991 sessiond_timer_rotate_pending_stop(session
);
2994 if (session
->rotate_timer_enabled
) {
2995 sessiond_rotate_timer_stop(session
);
2998 if (session
->rotate_size
) {
2999 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
3000 session
->rotate_size
= 0;
3004 * The rename of the current chunk is performed at stop, but if we rotated
3005 * the session after the previous stop command, we need to rename the
3006 * new (and empty) chunk that was started in between.
3008 if (session
->rotated_after_last_stop
) {
3009 rename_active_chunk(session
);
3012 /* Clean kernel session teardown */
3013 kernel_destroy_session(ksess
);
3015 /* UST session teardown */
3017 /* Close any relayd session */
3018 consumer_output_send_destroy_relayd(usess
->consumer
);
3020 /* Destroy every UST application related to this session. */
3021 ret
= ust_app_destroy_trace_all(usess
);
3023 ERR("Error in ust_app_destroy_trace_all");
3026 /* Clean up the rest. */
3027 trace_ust_destroy_session(usess
);
3031 * Must notify the kernel thread here to update it's poll set in order to
3032 * remove the channel(s)' fd just destroyed.
3034 ret
= notify_thread_pipe(wpipe
);
3036 PERROR("write kernel poll pipe");
3039 ret
= session_destroy(session
);
3045 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3047 int cmd_register_consumer(struct ltt_session
*session
,
3048 enum lttng_domain_type domain
, const char *sock_path
,
3049 struct consumer_data
*cdata
)
3052 struct consumer_socket
*socket
= NULL
;
3059 case LTTNG_DOMAIN_KERNEL
:
3061 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3065 /* Can't register a consumer if there is already one */
3066 if (ksess
->consumer_fds_sent
!= 0) {
3067 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3071 sock
= lttcomm_connect_unix_sock(sock_path
);
3073 ret
= LTTNG_ERR_CONNECT_FAIL
;
3076 cdata
->cmd_sock
= sock
;
3078 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3079 if (socket
== NULL
) {
3082 PERROR("close register consumer");
3084 cdata
->cmd_sock
= -1;
3085 ret
= LTTNG_ERR_FATAL
;
3089 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3090 if (socket
->lock
== NULL
) {
3091 PERROR("zmalloc pthread mutex");
3092 ret
= LTTNG_ERR_FATAL
;
3095 pthread_mutex_init(socket
->lock
, NULL
);
3096 socket
->registered
= 1;
3099 consumer_add_socket(socket
, ksess
->consumer
);
3102 pthread_mutex_lock(&cdata
->pid_mutex
);
3104 pthread_mutex_unlock(&cdata
->pid_mutex
);
3109 /* TODO: Userspace tracing */
3110 ret
= LTTNG_ERR_UND
;
3118 consumer_destroy_socket(socket
);
3124 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3126 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3127 struct lttng_domain
**domains
)
3132 struct lttng_ht_iter iter
;
3134 if (session
->kernel_session
!= NULL
) {
3135 DBG3("Listing domains found kernel domain");
3139 if (session
->ust_session
!= NULL
) {
3140 DBG3("Listing domains found UST global domain");
3144 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3146 if (agt
->being_used
) {
3157 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3158 if (*domains
== NULL
) {
3159 ret
= LTTNG_ERR_FATAL
;
3163 if (session
->kernel_session
!= NULL
) {
3164 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3166 /* Kernel session buffer type is always GLOBAL */
3167 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3172 if (session
->ust_session
!= NULL
) {
3173 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3174 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3178 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3180 if (agt
->being_used
) {
3181 (*domains
)[index
].type
= agt
->domain
;
3182 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3192 /* Return negative value to differentiate return code */
3198 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3200 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3201 struct ltt_session
*session
, struct lttng_channel
**channels
)
3203 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3206 case LTTNG_DOMAIN_KERNEL
:
3207 if (session
->kernel_session
!= NULL
) {
3208 nb_chan
= session
->kernel_session
->channel_count
;
3210 DBG3("Number of kernel channels %zd", nb_chan
);
3212 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3216 case LTTNG_DOMAIN_UST
:
3217 if (session
->ust_session
!= NULL
) {
3219 nb_chan
= lttng_ht_get_count(
3220 session
->ust_session
->domain_global
.channels
);
3223 DBG3("Number of UST global channels %zd", nb_chan
);
3225 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3230 ret
= -LTTNG_ERR_UND
;
3235 const size_t channel_size
= sizeof(struct lttng_channel
) +
3236 sizeof(struct lttng_channel_extended
);
3237 struct lttng_channel_extended
*channel_exts
;
3239 payload_size
= nb_chan
* channel_size
;
3240 *channels
= zmalloc(payload_size
);
3241 if (*channels
== NULL
) {
3242 ret
= -LTTNG_ERR_FATAL
;
3246 channel_exts
= ((void *) *channels
) +
3247 (nb_chan
* sizeof(struct lttng_channel
));
3248 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3249 if (ret
!= LTTNG_OK
) {
3264 * Command LTTNG_LIST_EVENTS processed by the client thread.
3266 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3267 struct ltt_session
*session
, char *channel_name
,
3268 struct lttng_event
**events
, size_t *total_size
)
3271 ssize_t nb_event
= 0;
3274 case LTTNG_DOMAIN_KERNEL
:
3275 if (session
->kernel_session
!= NULL
) {
3276 nb_event
= list_lttng_kernel_events(channel_name
,
3277 session
->kernel_session
, events
,
3281 case LTTNG_DOMAIN_UST
:
3283 if (session
->ust_session
!= NULL
) {
3284 nb_event
= list_lttng_ust_global_events(channel_name
,
3285 &session
->ust_session
->domain_global
, events
,
3290 case LTTNG_DOMAIN_LOG4J
:
3291 case LTTNG_DOMAIN_JUL
:
3292 case LTTNG_DOMAIN_PYTHON
:
3293 if (session
->ust_session
) {
3294 struct lttng_ht_iter iter
;
3298 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3299 &iter
.iter
, agt
, node
.node
) {
3300 if (agt
->domain
== domain
) {
3301 nb_event
= list_lttng_agent_events(
3311 ret
= LTTNG_ERR_UND
;
3318 /* Return negative value to differentiate return code */
3323 * Using the session list, filled a lttng_session array to send back to the
3324 * client for session listing.
3326 * The session list lock MUST be acquired before calling this function. Use
3327 * session_lock_list() and session_unlock_list().
3329 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3334 struct ltt_session
*session
;
3335 struct ltt_session_list
*list
= session_get_list();
3337 DBG("Getting all available session for UID %d GID %d",
3340 * Iterate over session list and append data after the control struct in
3343 cds_list_for_each_entry(session
, &list
->head
, list
) {
3345 * Only list the sessions the user can control.
3347 if (!session_access_ok(session
, uid
, gid
)) {
3351 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3352 struct ltt_ust_session
*usess
= session
->ust_session
;
3354 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3355 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3356 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3357 ret
= build_network_session_path(sessions
[i
].path
,
3358 sizeof(sessions
[i
].path
), session
);
3360 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3361 session
->consumer
->dst
.session_root_path
);
3364 PERROR("snprintf session path");
3368 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3369 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3370 sessions
[i
].enabled
= session
->active
;
3371 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3372 sessions
[i
].live_timer_interval
= session
->live_timer
;
3378 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3379 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3381 int cmd_data_pending(struct ltt_session
*session
)
3384 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3385 struct ltt_ust_session
*usess
= session
->ust_session
;
3389 DBG("Data pending for session %s", session
->name
);
3391 /* Session MUST be stopped to ask for data availability. */
3392 if (session
->active
) {
3393 ret
= LTTNG_ERR_SESSION_STARTED
;
3397 * If stopped, just make sure we've started before else the above call
3398 * will always send that there is data pending.
3400 * The consumer assumes that when the data pending command is received,
3401 * the trace has been started before or else no output data is written
3402 * by the streams which is a condition for data pending. So, this is
3403 * *VERY* important that we don't ask the consumer before a start
3406 if (!session
->has_been_started
) {
3413 * A rotation is still pending, we have to wait.
3415 if (session
->rotate_pending
) {
3416 DBG("Rotate still pending for session %s", session
->name
);
3421 if (ksess
&& ksess
->consumer
) {
3422 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3424 /* Data is still being extracted for the kernel. */
3429 if (usess
&& usess
->consumer
) {
3430 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3432 /* Data is still being extracted for the kernel. */
3437 /* Data is ready to be read by a viewer */
3445 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3447 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3449 int cmd_snapshot_add_output(struct ltt_session
*session
,
3450 struct lttng_snapshot_output
*output
, uint32_t *id
)
3453 struct snapshot_output
*new_output
;
3458 DBG("Cmd snapshot add output for session %s", session
->name
);
3461 * Can't create an output if the session is not set in no-output mode.
3463 if (session
->output_traces
) {
3464 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3468 /* Only one output is allowed until we have the "tee" feature. */
3469 if (session
->snapshot
.nb_output
== 1) {
3470 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3474 new_output
= snapshot_output_alloc();
3476 ret
= LTTNG_ERR_NOMEM
;
3480 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3481 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3482 &session
->snapshot
);
3484 if (ret
== -ENOMEM
) {
3485 ret
= LTTNG_ERR_NOMEM
;
3487 ret
= LTTNG_ERR_INVALID
;
3493 snapshot_add_output(&session
->snapshot
, new_output
);
3495 *id
= new_output
->id
;
3502 snapshot_output_destroy(new_output
);
3508 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3510 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3512 int cmd_snapshot_del_output(struct ltt_session
*session
,
3513 struct lttng_snapshot_output
*output
)
3516 struct snapshot_output
*sout
= NULL
;
3524 * Permission denied to create an output if the session is not
3525 * set in no output mode.
3527 if (session
->output_traces
) {
3528 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3533 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3535 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3536 } else if (*output
->name
!= '\0') {
3537 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3539 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3542 ret
= LTTNG_ERR_INVALID
;
3546 snapshot_delete_output(&session
->snapshot
, sout
);
3547 snapshot_output_destroy(sout
);
3556 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3558 * If no output is available, outputs is untouched and 0 is returned.
3560 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3562 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3563 struct lttng_snapshot_output
**outputs
)
3566 struct lttng_snapshot_output
*list
= NULL
;
3567 struct lttng_ht_iter iter
;
3568 struct snapshot_output
*output
;
3573 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3576 * Permission denied to create an output if the session is not
3577 * set in no output mode.
3579 if (session
->output_traces
) {
3580 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3584 if (session
->snapshot
.nb_output
== 0) {
3589 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3591 ret
= -LTTNG_ERR_NOMEM
;
3595 /* Copy list from session to the new list object. */
3597 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3598 output
, node
.node
) {
3599 assert(output
->consumer
);
3600 list
[idx
].id
= output
->id
;
3601 list
[idx
].max_size
= output
->max_size
;
3602 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3603 sizeof(list
[idx
].name
))) {
3604 ret
= -LTTNG_ERR_INVALID
;
3607 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3608 if (lttng_strncpy(list
[idx
].ctrl_url
,
3609 output
->consumer
->dst
.session_root_path
,
3610 sizeof(list
[idx
].ctrl_url
))) {
3611 ret
= -LTTNG_ERR_INVALID
;
3616 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3617 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3619 ret
= -LTTNG_ERR_NOMEM
;
3624 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3625 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3627 ret
= -LTTNG_ERR_NOMEM
;
3636 ret
= session
->snapshot
.nb_output
;
3645 * Check if we can regenerate the metadata for this session.
3646 * Only kernel, UST per-uid and non-live sessions are supported.
3648 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3651 int check_regenerate_metadata_support(struct ltt_session
*session
)
3657 if (session
->live_timer
!= 0) {
3658 ret
= LTTNG_ERR_LIVE_SESSION
;
3661 if (!session
->active
) {
3662 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3665 if (session
->ust_session
) {
3666 switch (session
->ust_session
->buffer_type
) {
3667 case LTTNG_BUFFER_PER_UID
:
3669 case LTTNG_BUFFER_PER_PID
:
3670 ret
= LTTNG_ERR_PER_PID_SESSION
;
3674 ret
= LTTNG_ERR_UNK
;
3678 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3679 session
->consumer
->relay_minor_version
< 8) {
3680 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3690 int clear_metadata_file(int fd
)
3695 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
3696 if (lseek_ret
< 0) {
3702 ret
= ftruncate(fd
, 0);
3704 PERROR("ftruncate");
3713 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3716 struct buffer_reg_uid
*uid_reg
= NULL
;
3717 struct buffer_reg_session
*session_reg
= NULL
;
3720 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3721 struct ust_registry_session
*registry
;
3722 struct ust_registry_channel
*chan
;
3723 struct lttng_ht_iter iter_chan
;
3725 session_reg
= uid_reg
->registry
;
3726 registry
= session_reg
->reg
.ust
;
3728 pthread_mutex_lock(®istry
->lock
);
3729 registry
->metadata_len_sent
= 0;
3730 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3731 registry
->metadata_len
= 0;
3732 registry
->metadata_version
++;
3733 if (registry
->metadata_fd
> 0) {
3734 /* Clear the metadata file's content. */
3735 ret
= clear_metadata_file(registry
->metadata_fd
);
3737 pthread_mutex_unlock(®istry
->lock
);
3742 ret
= ust_metadata_session_statedump(registry
, NULL
,
3743 registry
->major
, registry
->minor
);
3745 pthread_mutex_unlock(®istry
->lock
);
3746 ERR("Failed to generate session metadata (err = %d)",
3750 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3752 struct ust_registry_event
*event
;
3753 struct lttng_ht_iter iter_event
;
3755 ret
= ust_metadata_channel_statedump(registry
, chan
);
3757 pthread_mutex_unlock(®istry
->lock
);
3758 ERR("Failed to generate channel metadata "
3762 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3764 ret
= ust_metadata_event_statedump(registry
,
3767 pthread_mutex_unlock(®istry
->lock
);
3768 ERR("Failed to generate event metadata "
3774 pthread_mutex_unlock(®istry
->lock
);
3783 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3785 * Ask the consumer to truncate the existing metadata file(s) and
3786 * then regenerate the metadata. Live and per-pid sessions are not
3787 * supported and return an error.
3789 * Return 0 on success or else a LTTNG_ERR code.
3791 int cmd_regenerate_metadata(struct ltt_session
*session
)
3797 ret
= check_regenerate_metadata_support(session
);
3802 if (session
->kernel_session
) {
3803 ret
= kernctl_session_regenerate_metadata(
3804 session
->kernel_session
->fd
);
3806 ERR("Failed to regenerate the kernel metadata");
3811 if (session
->ust_session
) {
3812 ret
= ust_regenerate_metadata(session
->ust_session
);
3814 ERR("Failed to regenerate the UST metadata");
3818 DBG("Cmd metadata regenerate for session %s", session
->name
);
3826 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3828 * Ask the tracer to regenerate a new statedump.
3830 * Return 0 on success or else a LTTNG_ERR code.
3832 int cmd_regenerate_statedump(struct ltt_session
*session
)
3838 if (!session
->active
) {
3839 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3843 if (session
->kernel_session
) {
3844 ret
= kernctl_session_regenerate_statedump(
3845 session
->kernel_session
->fd
);
3847 * Currently, the statedump in kernel can only fail if out
3851 if (ret
== -ENOMEM
) {
3852 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3854 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3856 ERR("Failed to regenerate the kernel statedump");
3861 if (session
->ust_session
) {
3862 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3864 * Currently, the statedump in UST always returns 0.
3867 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3868 ERR("Failed to regenerate the UST statedump");
3872 DBG("Cmd regenerate statedump for session %s", session
->name
);
3879 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3880 struct notification_thread_handle
*notification_thread
)
3884 ssize_t sock_recv_len
;
3885 struct lttng_trigger
*trigger
= NULL
;
3886 struct lttng_buffer_view view
;
3887 struct lttng_dynamic_buffer trigger_buffer
;
3889 lttng_dynamic_buffer_init(&trigger_buffer
);
3890 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3891 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3893 ret
= LTTNG_ERR_NOMEM
;
3897 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3899 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3900 ERR("Failed to receive \"register trigger\" command payload");
3901 /* TODO: should this be a new error enum ? */
3902 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3906 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3907 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3909 ERR("Invalid trigger payload received in \"register trigger\" command");
3910 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3914 ret
= notification_thread_command_register_trigger(notification_thread
,
3916 /* Ownership of trigger was transferred. */
3919 lttng_trigger_destroy(trigger
);
3920 lttng_dynamic_buffer_reset(&trigger_buffer
);
3924 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3925 struct notification_thread_handle
*notification_thread
)
3929 ssize_t sock_recv_len
;
3930 struct lttng_trigger
*trigger
= NULL
;
3931 struct lttng_buffer_view view
;
3932 struct lttng_dynamic_buffer trigger_buffer
;
3934 lttng_dynamic_buffer_init(&trigger_buffer
);
3935 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3936 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3938 ret
= LTTNG_ERR_NOMEM
;
3942 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3944 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3945 ERR("Failed to receive \"unregister trigger\" command payload");
3946 /* TODO: should this be a new error enum ? */
3947 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3951 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3952 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3954 ERR("Invalid trigger payload received in \"unregister trigger\" command");
3955 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3959 ret
= notification_thread_command_unregister_trigger(notification_thread
,
3962 lttng_trigger_destroy(trigger
);
3963 lttng_dynamic_buffer_reset(&trigger_buffer
);
3968 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3969 * snapshot output is *not* set with a remote destination.
3971 * Return 0 on success or a LTTNG_ERR code.
3973 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3974 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3977 struct lttng_ht_iter iter
;
3978 struct consumer_socket
*socket
;
3981 assert(snap_output
);
3984 DBG2("Set relayd object from snapshot output");
3986 /* Ignore if snapshot consumer output is not network. */
3987 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3992 * For each consumer socket, create and send the relayd object of the
3996 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3997 socket
, node
.node
) {
3998 pthread_mutex_lock(socket
->lock
);
3999 ret
= send_consumer_relayd_sockets(0, session
->id
,
4000 snap_output
->consumer
, socket
,
4001 session
->name
, session
->hostname
,
4002 session
->live_timer
);
4003 pthread_mutex_unlock(socket
->lock
);
4004 if (ret
!= LTTNG_OK
) {
4016 * Record a kernel snapshot.
4018 * Return LTTNG_OK on success or a LTTNG_ERR code.
4020 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
4021 struct snapshot_output
*output
, struct ltt_session
*session
,
4022 int wait
, uint64_t nb_packets_per_stream
)
4032 * Copy kernel session sockets so we can communicate with the right
4033 * consumer for the snapshot record command.
4035 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
4037 ret
= LTTNG_ERR_NOMEM
;
4041 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
4042 if (ret
!= LTTNG_OK
) {
4043 goto error_snapshot
;
4046 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
4047 if (ret
!= LTTNG_OK
) {
4048 goto error_snapshot
;
4055 /* Clean up copied sockets so this output can use some other later on. */
4056 consumer_destroy_output_sockets(output
->consumer
);
4063 * Record a UST snapshot.
4065 * Return 0 on success or a LTTNG_ERR error code.
4067 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
4068 struct snapshot_output
*output
, struct ltt_session
*session
,
4069 int wait
, uint64_t nb_packets_per_stream
)
4078 * Copy UST session sockets so we can communicate with the right
4079 * consumer for the snapshot record command.
4081 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
4083 ret
= LTTNG_ERR_NOMEM
;
4087 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
4088 if (ret
!= LTTNG_OK
) {
4089 goto error_snapshot
;
4092 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
4096 ret
= LTTNG_ERR_INVALID
;
4099 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4102 goto error_snapshot
;
4108 /* Clean up copied sockets so this output can use some other later on. */
4109 consumer_destroy_output_sockets(output
->consumer
);
4115 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
4116 uint64_t cur_nr_packets
)
4118 uint64_t tot_size
= 0;
4120 if (session
->kernel_session
) {
4121 struct ltt_kernel_channel
*chan
;
4122 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4124 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4125 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4127 * Don't take channel into account if we
4128 * already grab all its packets.
4132 tot_size
+= chan
->channel
->attr
.subbuf_size
4133 * chan
->stream_count
;
4137 if (session
->ust_session
) {
4138 struct ltt_ust_session
*usess
= session
->ust_session
;
4140 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4148 * Calculate the number of packets we can grab from each stream that
4149 * fits within the overall snapshot max size.
4151 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4152 * the number of packets per stream.
4154 * TODO: this approach is not perfect: we consider the worse case
4155 * (packet filling the sub-buffers) as an upper bound, but we could do
4156 * better if we do this calculation while we actually grab the packet
4157 * content: we would know how much padding we don't actually store into
4160 * This algorithm is currently bounded by the number of packets per
4163 * Since we call this algorithm before actually grabbing the data, it's
4164 * an approximation: for instance, applications could appear/disappear
4165 * in between this call and actually grabbing data.
4168 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
4171 uint64_t cur_nb_packets
= 0;
4174 return 0; /* Infinite */
4177 size_left
= max_size
;
4179 uint64_t one_more_packet_tot_size
;
4181 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
4183 if (!one_more_packet_tot_size
) {
4184 /* We are already grabbing all packets. */
4187 size_left
-= one_more_packet_tot_size
;
4188 if (size_left
< 0) {
4193 if (!cur_nb_packets
) {
4194 /* Not enough room to grab one packet of each stream, error. */
4197 return cur_nb_packets
;
4201 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4203 * The wait parameter is ignored so this call always wait for the snapshot to
4204 * complete before returning.
4206 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4208 int cmd_snapshot_record(struct ltt_session
*session
,
4209 struct lttng_snapshot_output
*output
, int wait
)
4212 unsigned int use_tmp_output
= 0;
4213 struct snapshot_output tmp_output
;
4214 unsigned int snapshot_success
= 0;
4220 DBG("Cmd snapshot record for session %s", session
->name
);
4222 /* Get the datetime for the snapshot output directory. */
4223 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4226 ret
= LTTNG_ERR_INVALID
;
4231 * Permission denied to create an output if the session is not
4232 * set in no output mode.
4234 if (session
->output_traces
) {
4235 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4239 /* The session needs to be started at least once. */
4240 if (!session
->has_been_started
) {
4241 ret
= LTTNG_ERR_START_SESSION_ONCE
;
4245 /* Use temporary output for the session. */
4246 if (*output
->ctrl_url
!= '\0') {
4247 ret
= snapshot_output_init(output
->max_size
, output
->name
,
4248 output
->ctrl_url
, output
->data_url
, session
->consumer
,
4251 if (ret
== -ENOMEM
) {
4252 ret
= LTTNG_ERR_NOMEM
;
4254 ret
= LTTNG_ERR_INVALID
;
4258 /* Use the global session count for the temporary snapshot. */
4259 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4261 /* Use the global datetime */
4262 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4266 if (use_tmp_output
) {
4267 int64_t nb_packets_per_stream
;
4269 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4270 tmp_output
.max_size
);
4271 if (nb_packets_per_stream
< 0) {
4272 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4276 if (session
->kernel_session
) {
4277 ret
= record_kernel_snapshot(session
->kernel_session
,
4278 &tmp_output
, session
,
4279 wait
, nb_packets_per_stream
);
4280 if (ret
!= LTTNG_OK
) {
4285 if (session
->ust_session
) {
4286 ret
= record_ust_snapshot(session
->ust_session
,
4287 &tmp_output
, session
,
4288 wait
, nb_packets_per_stream
);
4289 if (ret
!= LTTNG_OK
) {
4294 snapshot_success
= 1;
4296 struct snapshot_output
*sout
;
4297 struct lttng_ht_iter iter
;
4300 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4301 &iter
.iter
, sout
, node
.node
) {
4302 int64_t nb_packets_per_stream
;
4305 * Make a local copy of the output and assign the possible
4306 * temporary value given by the caller.
4308 memset(&tmp_output
, 0, sizeof(tmp_output
));
4309 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
4311 if (output
->max_size
!= (uint64_t) -1ULL) {
4312 tmp_output
.max_size
= output
->max_size
;
4315 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4316 tmp_output
.max_size
);
4317 if (nb_packets_per_stream
< 0) {
4318 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4323 /* Use temporary name. */
4324 if (*output
->name
!= '\0') {
4325 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4326 sizeof(tmp_output
.name
))) {
4327 ret
= LTTNG_ERR_INVALID
;
4333 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4334 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4336 if (session
->kernel_session
) {
4337 ret
= record_kernel_snapshot(session
->kernel_session
,
4338 &tmp_output
, session
,
4339 wait
, nb_packets_per_stream
);
4340 if (ret
!= LTTNG_OK
) {
4346 if (session
->ust_session
) {
4347 ret
= record_ust_snapshot(session
->ust_session
,
4348 &tmp_output
, session
,
4349 wait
, nb_packets_per_stream
);
4350 if (ret
!= LTTNG_OK
) {
4355 snapshot_success
= 1;
4360 if (snapshot_success
) {
4361 session
->snapshot
.nb_snapshot
++;
4363 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4371 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4373 int cmd_set_session_shm_path(struct ltt_session
*session
,
4374 const char *shm_path
)
4380 * Can only set shm path before session is started.
4382 if (session
->has_been_started
) {
4383 return LTTNG_ERR_SESSION_STARTED
;
4386 strncpy(session
->shm_path
, shm_path
,
4387 sizeof(session
->shm_path
));
4388 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4394 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4396 * Ask the consumer to rotate the session output directory.
4397 * The session lock must be held.
4399 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4401 int cmd_rotate_session(struct ltt_session
*session
,
4402 struct lttng_rotate_session_return
*rotate_return
)
4406 struct tm
*timeinfo
;
4409 bool ust_active
= false;
4413 if (!session
->has_been_started
) {
4414 ret
= -LTTNG_ERR_START_SESSION_ONCE
;
4418 if (session
->live_timer
|| session
->snapshot_mode
||
4419 !session
->output_traces
) {
4420 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4425 * Unsupported feature in lttng-relayd before 2.11.
4427 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4428 (session
->consumer
->relay_major_version
== 2 &&
4429 session
->consumer
->relay_minor_version
< 11)) {
4430 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
4434 if (session
->rotate_pending
|| session
->rotate_pending_relay
) {
4435 ret
= -LTTNG_ERR_ROTATION_PENDING
;
4436 DBG("Rotate already in progress");
4441 * After a stop, we only allow one rotation to occur, the other ones are
4442 * useless until a new start.
4444 if (session
->rotated_after_last_stop
) {
4445 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4447 ret
= -LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
4451 /* Special case for the first rotation. */
4452 if (session
->current_archive_id
== 0) {
4453 const char *base_path
= NULL
;
4455 /* Either one of the two sessions is enough to get the root path. */
4456 if (session
->kernel_session
) {
4457 base_path
= session_get_base_path(session
);
4458 } else if (session
->ust_session
) {
4459 base_path
= session_get_base_path(session
);
4464 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4466 sizeof(session
->rotation_chunk
.current_rotate_path
));
4468 ERR("Failed to copy session base path to current rotation chunk path");
4469 ret
= -LTTNG_ERR_UNK
;
4474 * The currently active tracing path is now the folder we
4477 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4478 session
->rotation_chunk
.active_tracing_path
,
4479 sizeof(session
->rotation_chunk
.current_rotate_path
));
4481 ERR("Failed to copy the active tracing path to the current rotate path");
4482 ret
= -LTTNG_ERR_UNK
;
4486 DBG("Current rotate path %s", session
->rotation_chunk
.current_rotate_path
);
4488 session
->current_archive_id
++;
4489 session
->rotate_pending
= true;
4490 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
4493 * Create the path name for the next chunk.
4496 if (now
== (time_t) -1) {
4497 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4500 session
->last_chunk_start_ts
= session
->current_chunk_start_ts
;
4501 session
->current_chunk_start_ts
= now
;
4503 timeinfo
= localtime(&now
);
4505 PERROR("Failed to sample local time in rotate session command");
4506 ret
= -LTTNG_ERR_UNK
;
4509 strf_ret
= strftime(datetime
, sizeof(datetime
), "%Y%m%dT%H%M%S%z",
4512 ERR("Failed to format local time timestamp in rotate session command");
4513 ret
= -LTTNG_ERR_UNK
;
4516 if (session
->kernel_session
) {
4518 * The active path for the next rotation/destroy.
4519 * Ex: ~/lttng-traces/auto-20170922-111748/20170922-111754-42
4521 ret
= snprintf(session
->rotation_chunk
.active_tracing_path
,
4522 sizeof(session
->rotation_chunk
.active_tracing_path
),
4524 session_get_base_path(session
),
4525 datetime
, session
->current_archive_id
+ 1);
4526 if (ret
< 0 || ret
== sizeof(session
->rotation_chunk
.active_tracing_path
)) {
4527 ERR("Failed to format active kernel tracing path in rotate session command");
4528 ret
= -LTTNG_ERR_UNK
;
4532 * The sub-directory for the consumer
4533 * Ex: /20170922-111754-42/kernel
4535 ret
= snprintf(session
->kernel_session
->consumer
->chunk_path
,
4536 sizeof(session
->kernel_session
->consumer
->chunk_path
),
4537 "/%s-%" PRIu64
, datetime
,
4538 session
->current_archive_id
+ 1);
4539 if (ret
< 0 || ret
== sizeof(session
->kernel_session
->consumer
->chunk_path
)) {
4540 ERR("Failed to format the kernel consumer's sub-directory in rotate session command");
4541 ret
= -LTTNG_ERR_UNK
;
4545 * Create the new chunk folder, before the rotation begins so we don't
4546 * race with the consumer/tracer activity.
4548 ret
= domain_mkdir(session
->kernel_session
->consumer
, session
,
4549 session
->kernel_session
->uid
,
4550 session
->kernel_session
->gid
);
4552 ERR("Failed to create kernel session tracing path at %s",
4553 session
->kernel_session
->consumer
->chunk_path
);
4554 ret
= -LTTNG_ERR_CREATE_DIR_FAIL
;
4557 ret
= kernel_rotate_session(session
);
4558 if (ret
!= LTTNG_OK
) {
4563 if (session
->ust_session
) {
4564 ret
= snprintf(session
->rotation_chunk
.active_tracing_path
,
4565 PATH_MAX
, "%s/%s-%" PRIu64
,
4566 session_get_base_path(session
),
4567 datetime
, session
->current_archive_id
+ 1);
4569 ERR("Failed to format active UST tracing path in rotate session command");
4570 ret
= -LTTNG_ERR_UNK
;
4573 ret
= snprintf(session
->ust_session
->consumer
->chunk_path
,
4574 PATH_MAX
, "/%s-%" PRIu64
, datetime
,
4575 session
->current_archive_id
+ 1);
4577 ERR("Failed to format the UST consumer's sub-directory in rotate session command");
4578 ret
= -LTTNG_ERR_UNK
;
4582 * Create the new chunk folder, before the rotation begins so we don't
4583 * race with the consumer/tracer activity.
4585 ret
= domain_mkdir(session
->ust_session
->consumer
, session
,
4586 session
->ust_session
->uid
,
4587 session
->ust_session
->gid
);
4589 ret
= -LTTNG_ERR_CREATE_DIR_FAIL
;
4592 ret
= ust_app_rotate_session(session
, &ust_active
);
4593 if (ret
!= LTTNG_OK
) {
4597 * Handle the case where we did not start a rotation on any channel.
4598 * The consumer will never wake up the rotation thread to perform the
4599 * rename, so we have to do it here while we hold the session and
4600 * session_list locks.
4602 if (!session
->kernel_session
&& !ust_active
) {
4603 ret
= rename_complete_chunk(session
, now
);
4605 ERR("Failed to rename completed rotation chunk");
4608 session
->rotate_pending
= false;
4609 session
->rotation_state
= LTTNG_ROTATION_STATE_COMPLETED
;
4613 if (!session
->active
) {
4614 session
->rotated_after_last_stop
= true;
4617 if (rotate_return
) {
4618 rotate_return
->rotation_id
= session
->current_archive_id
;
4621 DBG("Cmd rotate session %s, current_archive_id %" PRIu64
" sent",
4622 session
->name
, session
->current_archive_id
);
4630 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
4632 * Check if the session has finished its rotation.
4634 * Return 0 on success or else a LTTNG_ERR code.
4636 int cmd_rotate_get_info(struct ltt_session
*session
,
4637 struct lttng_rotation_get_info_return
*info_return
,
4638 uint64_t rotation_id
)
4644 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
4645 session
->current_archive_id
);
4647 if (session
->current_archive_id
!= rotation_id
) {
4648 info_return
->status
= (int32_t) LTTNG_ROTATION_STATE_EXPIRED
;
4653 switch (session
->rotation_state
) {
4654 case LTTNG_ROTATION_STATE_ONGOING
:
4655 DBG("Reporting that rotation id %" PRIu64
" of session %s is still pending",
4656 rotation_id
, session
->name
);
4658 case LTTNG_ROTATION_STATE_COMPLETED
:
4660 char *current_tracing_path_reply
;
4661 size_t current_tracing_path_reply_len
;
4663 switch (session_get_consumer_destination_type(session
)) {
4664 case CONSUMER_DST_LOCAL
:
4665 current_tracing_path_reply
=
4666 info_return
->location
.local
.absolute_path
;
4667 current_tracing_path_reply_len
=
4668 sizeof(info_return
->location
.local
.absolute_path
);
4669 info_return
->location_type
=
4670 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
4672 case CONSUMER_DST_NET
:
4673 current_tracing_path_reply
=
4674 info_return
->location
.relay
.relative_path
;
4675 current_tracing_path_reply_len
=
4676 sizeof(info_return
->location
.relay
.relative_path
);
4677 /* Currently the only supported relay protocol. */
4678 info_return
->location
.relay
.protocol
=
4679 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
4681 ret
= lttng_strncpy(info_return
->location
.relay
.host
,
4682 session_get_net_consumer_hostname(session
),
4683 sizeof(info_return
->location
.relay
.host
));
4685 ERR("Failed to host name to rotate_get_info reply");
4686 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4687 ret
= -LTTNG_ERR_UNK
;
4691 session_get_net_consumer_ports(session
,
4692 &info_return
->location
.relay
.ports
.control
,
4693 &info_return
->location
.relay
.ports
.data
);
4694 info_return
->location_type
=
4695 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
4700 ret
= lttng_strncpy(current_tracing_path_reply
,
4701 session
->rotation_chunk
.current_rotate_path
,
4702 current_tracing_path_reply_len
);
4704 ERR("Failed to copy current tracing path to rotate_get_info reply");
4705 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4706 ret
= -LTTNG_ERR_UNK
;
4712 case LTTNG_ROTATION_STATE_ERROR
:
4713 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session %s",
4714 rotation_id
, session
->name
);
4720 info_return
->status
= (int32_t) session
->rotation_state
;
4727 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
4729 * Configure the automatic rotation parameters.
4730 * Set to -1ULL to disable them.
4732 * Return 0 on success or else an LTTNG_ERR code.
4734 int cmd_rotation_set_schedule(struct ltt_session
*session
,
4735 uint64_t timer_us
, uint64_t size
,
4736 struct notification_thread_handle
*notification_thread_handle
)
4742 DBG("Cmd rotate set schedule session %s", session
->name
);
4744 if (session
->live_timer
|| session
->snapshot_mode
||
4745 !session
->output_traces
) {
4746 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4750 /* Trying to override an already active timer. */
4751 if (timer_us
&& timer_us
!= -1ULL && session
->rotate_timer_period
) {
4752 ret
= LTTNG_ERR_ROTATION_TIMER_SET
;
4754 /* Trying to disable an inactive timer. */
4755 } else if (timer_us
== -1ULL && !session
->rotate_timer_period
) {
4756 ret
= LTTNG_ERR_ROTATION_NO_TIMER_SET
;
4760 if (size
&& size
!= -1ULL && session
->rotate_size
) {
4761 ret
= LTTNG_ERR_ROTATION_SIZE_SET
;
4763 } else if (size
== -1ULL && !session
->rotate_size
) {
4764 ret
= LTTNG_ERR_ROTATION_NO_SIZE_SET
;
4768 if (timer_us
&& !session
->rotate_timer_period
) {
4769 if (timer_us
> UINT_MAX
) {
4770 ret
= LTTNG_ERR_INVALID
;
4774 session
->rotate_timer_period
= timer_us
;
4776 * Only start the timer if the session is active, otherwise
4777 * it will be started when the session starts.
4779 if (session
->active
) {
4780 ret
= sessiond_rotate_timer_start(session
, timer_us
);
4782 ERR("Failed to enable rotate timer");
4783 ret
= LTTNG_ERR_UNK
;
4787 } else if (timer_us
== -1ULL && session
->rotate_timer_period
> 0) {
4788 sessiond_rotate_timer_stop(session
);
4789 session
->rotate_timer_period
= 0;
4793 if (size
== -1ULL) {
4794 ret
= unsubscribe_session_consumed_size_rotation(session
,
4795 notification_thread_handle
);
4797 ret
= LTTNG_ERR_UNK
;
4800 session
->rotate_size
= 0;
4802 ret
= subscribe_session_consumed_size_rotation(session
,
4803 size
, notification_thread_handle
);
4805 PERROR("Subscribe to session usage");
4806 ret
= LTTNG_ERR_UNK
;
4809 session
->rotate_size
= size
;
4822 * Command ROTATE_GET_CURRENT_PATH from the lttng-ctl library.
4824 * Configure the automatic rotation parameters.
4825 * Set to -1ULL to disable them.
4827 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4829 int cmd_session_get_current_output(struct ltt_session
*session
,
4830 struct lttng_session_get_current_output_return
*output_return
)
4835 if (!session
->snapshot_mode
) {
4836 if (session
->current_archive_id
== 0) {
4837 if (session
->kernel_session
) {
4838 path
= session_get_base_path(session
);
4839 } else if (session
->ust_session
) {
4840 path
= session_get_base_path(session
);
4846 path
= session
->rotation_chunk
.active_tracing_path
;
4850 * A snapshot session does not have a "current" trace archive
4856 DBG("Cmd get current output for session %s, returning %s",
4857 session
->name
, path
);
4859 ret
= lttng_strncpy(output_return
->path
,
4861 sizeof(output_return
->path
));
4863 ERR("Failed to copy trace output path to session get current output command reply");
4864 ret
= -LTTNG_ERR_UNK
;
4874 * Init command subsystem.
4879 * Set network sequence index to 1 for streams to match a relayd
4880 * socket on the consumer side.
4882 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4883 relayd_net_seq_idx
= 1;
4884 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4886 DBG("Command subsystem initialized");