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(kernel_tracer_fd
);
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_PID processed by the client thread.
1225 * Called with session lock held.
1227 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1235 case LTTNG_DOMAIN_KERNEL
:
1237 struct ltt_kernel_session
*ksess
;
1239 ksess
= session
->kernel_session
;
1241 ret
= kernel_track_pid(ksess
, pid
);
1242 if (ret
!= LTTNG_OK
) {
1246 kernel_wait_quiescent(kernel_tracer_fd
);
1249 case LTTNG_DOMAIN_UST
:
1251 struct ltt_ust_session
*usess
;
1253 usess
= session
->ust_session
;
1255 ret
= trace_ust_track_pid(usess
, pid
);
1256 if (ret
!= LTTNG_OK
) {
1262 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1274 * Command LTTNG_UNTRACK_PID processed by the client thread.
1276 * Called with session lock held.
1278 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1286 case LTTNG_DOMAIN_KERNEL
:
1288 struct ltt_kernel_session
*ksess
;
1290 ksess
= session
->kernel_session
;
1292 ret
= kernel_untrack_pid(ksess
, pid
);
1293 if (ret
!= LTTNG_OK
) {
1297 kernel_wait_quiescent(kernel_tracer_fd
);
1300 case LTTNG_DOMAIN_UST
:
1302 struct ltt_ust_session
*usess
;
1304 usess
= session
->ust_session
;
1306 ret
= trace_ust_untrack_pid(usess
, pid
);
1307 if (ret
!= LTTNG_OK
) {
1313 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1325 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1327 * The wpipe arguments is used as a notifier for the kernel thread.
1329 int cmd_enable_channel(struct ltt_session
*session
,
1330 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1333 struct ltt_ust_session
*usess
= session
->ust_session
;
1334 struct lttng_ht
*chan_ht
;
1341 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1343 /* Validate channel name */
1344 if (attr
->name
[0] == '.' ||
1345 memchr(attr
->name
, '/', len
) != NULL
) {
1346 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1350 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1355 * Don't try to enable a channel if the session has been started at
1356 * some point in time before. The tracer does not allow it.
1358 if (session
->has_been_started
) {
1359 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1364 * If the session is a live session, remove the switch timer, the
1365 * live timer does the same thing but sends also synchronisation
1366 * beacons for inactive streams.
1368 if (session
->live_timer
> 0) {
1369 attr
->attr
.live_timer_interval
= session
->live_timer
;
1370 attr
->attr
.switch_timer_interval
= 0;
1373 /* Check for feature support */
1374 switch (domain
->type
) {
1375 case LTTNG_DOMAIN_KERNEL
:
1377 if (kernel_supports_ring_buffer_snapshot_sample_positions(kernel_tracer_fd
) != 1) {
1378 /* Sampling position of buffer is not supported */
1379 WARN("Kernel tracer does not support buffer monitoring. "
1380 "Setting the monitor interval timer to 0 "
1381 "(disabled) for channel '%s' of session '%s'",
1382 attr
-> name
, session
->name
);
1383 lttng_channel_set_monitor_timer_interval(attr
, 0);
1387 case LTTNG_DOMAIN_UST
:
1389 case LTTNG_DOMAIN_JUL
:
1390 case LTTNG_DOMAIN_LOG4J
:
1391 case LTTNG_DOMAIN_PYTHON
:
1392 if (!agent_tracing_is_enabled()) {
1393 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1394 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
1399 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1403 switch (domain
->type
) {
1404 case LTTNG_DOMAIN_KERNEL
:
1406 struct ltt_kernel_channel
*kchan
;
1408 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1409 session
->kernel_session
);
1410 if (kchan
== NULL
) {
1411 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1412 if (attr
->name
[0] != '\0') {
1413 session
->kernel_session
->has_non_default_channel
= 1;
1416 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1419 if (ret
!= LTTNG_OK
) {
1423 kernel_wait_quiescent(kernel_tracer_fd
);
1426 case LTTNG_DOMAIN_UST
:
1427 case LTTNG_DOMAIN_JUL
:
1428 case LTTNG_DOMAIN_LOG4J
:
1429 case LTTNG_DOMAIN_PYTHON
:
1431 struct ltt_ust_channel
*uchan
;
1436 * Current agent implementation limitations force us to allow
1437 * only one channel at once in "agent" subdomains. Each
1438 * subdomain has a default channel name which must be strictly
1441 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1442 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1443 LTTNG_SYMBOL_NAME_LEN
)) {
1444 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1447 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1448 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1449 LTTNG_SYMBOL_NAME_LEN
)) {
1450 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1453 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1454 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1455 LTTNG_SYMBOL_NAME_LEN
)) {
1456 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1461 chan_ht
= usess
->domain_global
.channels
;
1463 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1464 if (uchan
== NULL
) {
1465 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1466 if (attr
->name
[0] != '\0') {
1467 usess
->has_non_default_channel
= 1;
1470 ret
= channel_ust_enable(usess
, uchan
);
1475 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1486 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1488 int cmd_disable_event(struct ltt_session
*session
,
1489 enum lttng_domain_type domain
, char *channel_name
,
1490 struct lttng_event
*event
)
1495 DBG("Disable event command for event \'%s\'", event
->name
);
1497 event_name
= event
->name
;
1499 /* Error out on unhandled search criteria */
1500 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1501 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1502 ret
= LTTNG_ERR_UNK
;
1509 case LTTNG_DOMAIN_KERNEL
:
1511 struct ltt_kernel_channel
*kchan
;
1512 struct ltt_kernel_session
*ksess
;
1514 ksess
= session
->kernel_session
;
1517 * If a non-default channel has been created in the
1518 * session, explicitely require that -c chan_name needs
1521 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1522 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1526 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1527 if (kchan
== NULL
) {
1528 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1532 switch (event
->type
) {
1533 case LTTNG_EVENT_ALL
:
1534 case LTTNG_EVENT_TRACEPOINT
:
1535 case LTTNG_EVENT_SYSCALL
:
1536 case LTTNG_EVENT_PROBE
:
1537 case LTTNG_EVENT_FUNCTION
:
1538 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1539 if (event_name
[0] == '\0') {
1540 ret
= event_kernel_disable_event(kchan
,
1543 ret
= event_kernel_disable_event(kchan
,
1544 event_name
, event
->type
);
1546 if (ret
!= LTTNG_OK
) {
1551 ret
= LTTNG_ERR_UNK
;
1555 kernel_wait_quiescent(kernel_tracer_fd
);
1558 case LTTNG_DOMAIN_UST
:
1560 struct ltt_ust_channel
*uchan
;
1561 struct ltt_ust_session
*usess
;
1563 usess
= session
->ust_session
;
1565 if (validate_ust_event_name(event_name
)) {
1566 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1571 * If a non-default channel has been created in the
1572 * session, explicitly require that -c chan_name needs
1575 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1576 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1580 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1582 if (uchan
== NULL
) {
1583 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1587 switch (event
->type
) {
1588 case LTTNG_EVENT_ALL
:
1590 * An empty event name means that everything
1591 * should be disabled.
1593 if (event
->name
[0] == '\0') {
1594 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1596 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1599 if (ret
!= LTTNG_OK
) {
1604 ret
= LTTNG_ERR_UNK
;
1608 DBG3("Disable UST event %s in channel %s completed", event_name
,
1612 case LTTNG_DOMAIN_LOG4J
:
1613 case LTTNG_DOMAIN_JUL
:
1614 case LTTNG_DOMAIN_PYTHON
:
1617 struct ltt_ust_session
*usess
= session
->ust_session
;
1621 switch (event
->type
) {
1622 case LTTNG_EVENT_ALL
:
1625 ret
= LTTNG_ERR_UNK
;
1629 agt
= trace_ust_find_agent(usess
, domain
);
1631 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1635 * An empty event name means that everything
1636 * should be disabled.
1638 if (event
->name
[0] == '\0') {
1639 ret
= event_agent_disable_all(usess
, agt
);
1641 ret
= event_agent_disable(usess
, agt
, event_name
);
1643 if (ret
!= LTTNG_OK
) {
1650 ret
= LTTNG_ERR_UND
;
1663 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1665 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1666 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1668 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1669 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1672 * Don't try to add a context if the session has been started at
1673 * some point in time before. The tracer does not allow it and would
1674 * result in a corrupted trace.
1676 if (session
->has_been_started
) {
1677 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1681 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1682 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1683 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1687 case LTTNG_DOMAIN_KERNEL
:
1688 assert(session
->kernel_session
);
1690 if (session
->kernel_session
->channel_count
== 0) {
1691 /* Create default channel */
1692 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1693 if (ret
!= LTTNG_OK
) {
1696 chan_kern_created
= 1;
1698 /* Add kernel context to kernel tracer */
1699 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1700 if (ret
!= LTTNG_OK
) {
1704 case LTTNG_DOMAIN_JUL
:
1705 case LTTNG_DOMAIN_LOG4J
:
1708 * Validate channel name.
1709 * If no channel name is given and the domain is JUL or LOG4J,
1710 * set it to the appropriate domain-specific channel name. If
1711 * a name is provided but does not match the expexted channel
1712 * name, return an error.
1714 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1715 strcmp(channel_name
,
1716 DEFAULT_JUL_CHANNEL_NAME
)) {
1717 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1719 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1720 strcmp(channel_name
,
1721 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1722 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1725 /* break is _not_ missing here. */
1727 case LTTNG_DOMAIN_UST
:
1729 struct ltt_ust_session
*usess
= session
->ust_session
;
1730 unsigned int chan_count
;
1734 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1735 if (chan_count
== 0) {
1736 struct lttng_channel
*attr
;
1737 /* Create default channel */
1738 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1740 ret
= LTTNG_ERR_FATAL
;
1744 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1745 if (ret
!= LTTNG_OK
) {
1749 channel_attr_destroy(attr
);
1750 chan_ust_created
= 1;
1753 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1754 free(app_ctx_provider_name
);
1756 app_ctx_name
= NULL
;
1757 app_ctx_provider_name
= NULL
;
1758 if (ret
!= LTTNG_OK
) {
1764 ret
= LTTNG_ERR_UND
;
1772 if (chan_kern_created
) {
1773 struct ltt_kernel_channel
*kchan
=
1774 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1775 session
->kernel_session
);
1776 /* Created previously, this should NOT fail. */
1778 kernel_destroy_channel(kchan
);
1781 if (chan_ust_created
) {
1782 struct ltt_ust_channel
*uchan
=
1783 trace_ust_find_channel_by_name(
1784 session
->ust_session
->domain_global
.channels
,
1785 DEFAULT_CHANNEL_NAME
);
1786 /* Created previously, this should NOT fail. */
1788 /* Remove from the channel list of the session. */
1789 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1791 trace_ust_destroy_channel(uchan
);
1794 free(app_ctx_provider_name
);
1799 static inline bool name_starts_with(const char *name
, const char *prefix
)
1801 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1803 return !strncmp(name
, prefix
, max_cmp_len
);
1806 /* Perform userspace-specific event name validation */
1807 static int validate_ust_event_name(const char *name
)
1817 * Check name against all internal UST event component namespaces used
1820 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1821 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1822 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1831 * Internal version of cmd_enable_event() with a supplemental
1832 * "internal_event" flag which is used to enable internal events which should
1833 * be hidden from clients. Such events are used in the agent implementation to
1834 * enable the events through which all "agent" events are funeled.
1836 static int _cmd_enable_event(struct ltt_session
*session
,
1837 struct lttng_domain
*domain
,
1838 char *channel_name
, struct lttng_event
*event
,
1839 char *filter_expression
,
1840 struct lttng_filter_bytecode
*filter
,
1841 struct lttng_event_exclusion
*exclusion
,
1842 int wpipe
, bool internal_event
)
1844 int ret
= 0, channel_created
= 0;
1845 struct lttng_channel
*attr
= NULL
;
1849 assert(channel_name
);
1851 /* If we have a filter, we must have its filter expression */
1852 assert(!(!!filter_expression
^ !!filter
));
1854 /* Normalize event name as a globbing pattern */
1855 strutils_normalize_star_glob_pattern(event
->name
);
1857 /* Normalize exclusion names as globbing patterns */
1861 for (i
= 0; i
< exclusion
->count
; i
++) {
1862 char *name
= LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion
, i
);
1864 strutils_normalize_star_glob_pattern(name
);
1868 DBG("Enable event command for event \'%s\'", event
->name
);
1872 switch (domain
->type
) {
1873 case LTTNG_DOMAIN_KERNEL
:
1875 struct ltt_kernel_channel
*kchan
;
1878 * If a non-default channel has been created in the
1879 * session, explicitely require that -c chan_name needs
1882 if (session
->kernel_session
->has_non_default_channel
1883 && channel_name
[0] == '\0') {
1884 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1888 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1889 session
->kernel_session
);
1890 if (kchan
== NULL
) {
1891 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1892 LTTNG_BUFFER_GLOBAL
);
1894 ret
= LTTNG_ERR_FATAL
;
1897 if (lttng_strncpy(attr
->name
, channel_name
,
1898 sizeof(attr
->name
))) {
1899 ret
= LTTNG_ERR_INVALID
;
1903 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1904 if (ret
!= LTTNG_OK
) {
1907 channel_created
= 1;
1910 /* Get the newly created kernel channel pointer */
1911 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1912 session
->kernel_session
);
1913 if (kchan
== NULL
) {
1914 /* This sould not happen... */
1915 ret
= LTTNG_ERR_FATAL
;
1919 switch (event
->type
) {
1920 case LTTNG_EVENT_ALL
:
1922 char *filter_expression_a
= NULL
;
1923 struct lttng_filter_bytecode
*filter_a
= NULL
;
1926 * We need to duplicate filter_expression and filter,
1927 * because ownership is passed to first enable
1930 if (filter_expression
) {
1931 filter_expression_a
= strdup(filter_expression
);
1932 if (!filter_expression_a
) {
1933 ret
= LTTNG_ERR_FATAL
;
1938 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1940 free(filter_expression_a
);
1941 ret
= LTTNG_ERR_FATAL
;
1944 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1946 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1947 ret
= event_kernel_enable_event(kchan
, event
,
1948 filter_expression
, filter
);
1949 /* We have passed ownership */
1950 filter_expression
= NULL
;
1952 if (ret
!= LTTNG_OK
) {
1953 if (channel_created
) {
1954 /* Let's not leak a useless channel. */
1955 kernel_destroy_channel(kchan
);
1957 free(filter_expression_a
);
1961 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1962 ret
= event_kernel_enable_event(kchan
, event
,
1963 filter_expression_a
, filter_a
);
1964 /* We have passed ownership */
1965 filter_expression_a
= NULL
;
1967 if (ret
!= LTTNG_OK
) {
1972 case LTTNG_EVENT_PROBE
:
1973 case LTTNG_EVENT_FUNCTION
:
1974 case LTTNG_EVENT_FUNCTION_ENTRY
:
1975 case LTTNG_EVENT_TRACEPOINT
:
1976 ret
= event_kernel_enable_event(kchan
, event
,
1977 filter_expression
, filter
);
1978 /* We have passed ownership */
1979 filter_expression
= NULL
;
1981 if (ret
!= LTTNG_OK
) {
1982 if (channel_created
) {
1983 /* Let's not leak a useless channel. */
1984 kernel_destroy_channel(kchan
);
1989 case LTTNG_EVENT_SYSCALL
:
1990 ret
= event_kernel_enable_event(kchan
, event
,
1991 filter_expression
, filter
);
1992 /* We have passed ownership */
1993 filter_expression
= NULL
;
1995 if (ret
!= LTTNG_OK
) {
2000 ret
= LTTNG_ERR_UNK
;
2004 kernel_wait_quiescent(kernel_tracer_fd
);
2007 case LTTNG_DOMAIN_UST
:
2009 struct ltt_ust_channel
*uchan
;
2010 struct ltt_ust_session
*usess
= session
->ust_session
;
2015 * If a non-default channel has been created in the
2016 * session, explicitely require that -c chan_name needs
2019 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
2020 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
2024 /* Get channel from global UST domain */
2025 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2027 if (uchan
== NULL
) {
2028 /* Create default channel */
2029 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
2030 usess
->buffer_type
);
2032 ret
= LTTNG_ERR_FATAL
;
2035 if (lttng_strncpy(attr
->name
, channel_name
,
2036 sizeof(attr
->name
))) {
2037 ret
= LTTNG_ERR_INVALID
;
2041 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2042 if (ret
!= LTTNG_OK
) {
2046 /* Get the newly created channel reference back */
2047 uchan
= trace_ust_find_channel_by_name(
2048 usess
->domain_global
.channels
, channel_name
);
2052 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2054 * Don't allow users to add UST events to channels which
2055 * are assigned to a userspace subdomain (JUL, Log4J,
2058 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2062 if (!internal_event
) {
2064 * Ensure the event name is not reserved for internal
2067 ret
= validate_ust_event_name(event
->name
);
2069 WARN("Userspace event name %s failed validation.",
2071 event
->name
: "NULL");
2072 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2077 /* At this point, the session and channel exist on the tracer */
2078 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2079 filter_expression
, filter
, exclusion
,
2081 /* We have passed ownership */
2082 filter_expression
= NULL
;
2085 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2086 goto already_enabled
;
2087 } else if (ret
!= LTTNG_OK
) {
2092 case LTTNG_DOMAIN_LOG4J
:
2093 case LTTNG_DOMAIN_JUL
:
2094 case LTTNG_DOMAIN_PYTHON
:
2096 const char *default_event_name
, *default_chan_name
;
2098 struct lttng_event uevent
;
2099 struct lttng_domain tmp_dom
;
2100 struct ltt_ust_session
*usess
= session
->ust_session
;
2104 if (!agent_tracing_is_enabled()) {
2105 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2106 ret
= LTTNG_ERR_AGENT_TRACING_DISABLED
;
2110 agt
= trace_ust_find_agent(usess
, domain
->type
);
2112 agt
= agent_create(domain
->type
);
2114 ret
= LTTNG_ERR_NOMEM
;
2117 agent_add(agt
, usess
->agents
);
2120 /* Create the default tracepoint. */
2121 memset(&uevent
, 0, sizeof(uevent
));
2122 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2123 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2124 default_event_name
= event_get_default_agent_ust_name(
2126 if (!default_event_name
) {
2127 ret
= LTTNG_ERR_FATAL
;
2130 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2131 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2134 * The domain type is changed because we are about to enable the
2135 * default channel and event for the JUL domain that are hardcoded.
2136 * This happens in the UST domain.
2138 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2139 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2141 switch (domain
->type
) {
2142 case LTTNG_DOMAIN_LOG4J
:
2143 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2145 case LTTNG_DOMAIN_JUL
:
2146 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2148 case LTTNG_DOMAIN_PYTHON
:
2149 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2152 /* The switch/case we are in makes this impossible */
2157 char *filter_expression_copy
= NULL
;
2158 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2161 const size_t filter_size
= sizeof(
2162 struct lttng_filter_bytecode
)
2165 filter_copy
= zmalloc(filter_size
);
2167 ret
= LTTNG_ERR_NOMEM
;
2170 memcpy(filter_copy
, filter
, filter_size
);
2172 filter_expression_copy
=
2173 strdup(filter_expression
);
2174 if (!filter_expression
) {
2175 ret
= LTTNG_ERR_NOMEM
;
2178 if (!filter_expression_copy
|| !filter_copy
) {
2179 free(filter_expression_copy
);
2185 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2186 (char *) default_chan_name
,
2187 &uevent
, filter_expression_copy
,
2188 filter_copy
, NULL
, wpipe
);
2191 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2192 goto already_enabled
;
2193 } else if (ret
!= LTTNG_OK
) {
2197 /* The wild card * means that everything should be enabled. */
2198 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2199 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2202 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2206 filter_expression
= NULL
;
2207 if (ret
!= LTTNG_OK
) {
2214 ret
= LTTNG_ERR_UND
;
2222 free(filter_expression
);
2225 channel_attr_destroy(attr
);
2231 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2232 * We own filter, exclusion, and filter_expression.
2234 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2235 char *channel_name
, struct lttng_event
*event
,
2236 char *filter_expression
,
2237 struct lttng_filter_bytecode
*filter
,
2238 struct lttng_event_exclusion
*exclusion
,
2241 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2242 filter_expression
, filter
, exclusion
, wpipe
, false);
2246 * Enable an event which is internal to LTTng. An internal should
2247 * never be made visible to clients and are immune to checks such as
2250 static int cmd_enable_event_internal(struct ltt_session
*session
,
2251 struct lttng_domain
*domain
,
2252 char *channel_name
, struct lttng_event
*event
,
2253 char *filter_expression
,
2254 struct lttng_filter_bytecode
*filter
,
2255 struct lttng_event_exclusion
*exclusion
,
2258 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2259 filter_expression
, filter
, exclusion
, wpipe
, true);
2263 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2265 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2266 struct lttng_event
**events
)
2269 ssize_t nb_events
= 0;
2272 case LTTNG_DOMAIN_KERNEL
:
2273 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2274 if (nb_events
< 0) {
2275 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2279 case LTTNG_DOMAIN_UST
:
2280 nb_events
= ust_app_list_events(events
);
2281 if (nb_events
< 0) {
2282 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2286 case LTTNG_DOMAIN_LOG4J
:
2287 case LTTNG_DOMAIN_JUL
:
2288 case LTTNG_DOMAIN_PYTHON
:
2289 nb_events
= agent_list_events(events
, domain
);
2290 if (nb_events
< 0) {
2291 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2296 ret
= LTTNG_ERR_UND
;
2303 /* Return negative value to differentiate return code */
2308 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2310 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2311 struct lttng_event_field
**fields
)
2314 ssize_t nb_fields
= 0;
2317 case LTTNG_DOMAIN_UST
:
2318 nb_fields
= ust_app_list_event_fields(fields
);
2319 if (nb_fields
< 0) {
2320 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2324 case LTTNG_DOMAIN_KERNEL
:
2325 default: /* fall-through */
2326 ret
= LTTNG_ERR_UND
;
2333 /* Return negative value to differentiate return code */
2337 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2339 return syscall_table_list(events
);
2343 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2345 * Called with session lock held.
2347 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2348 enum lttng_domain_type domain
, int32_t **pids
)
2351 ssize_t nr_pids
= 0;
2354 case LTTNG_DOMAIN_KERNEL
:
2356 struct ltt_kernel_session
*ksess
;
2358 ksess
= session
->kernel_session
;
2359 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2361 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2366 case LTTNG_DOMAIN_UST
:
2368 struct ltt_ust_session
*usess
;
2370 usess
= session
->ust_session
;
2371 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2373 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2378 case LTTNG_DOMAIN_LOG4J
:
2379 case LTTNG_DOMAIN_JUL
:
2380 case LTTNG_DOMAIN_PYTHON
:
2382 ret
= LTTNG_ERR_UND
;
2389 /* Return negative value to differentiate return code */
2394 int domain_mkdir(const struct consumer_output
*output
,
2395 const struct ltt_session
*session
,
2396 uid_t uid
, gid_t gid
)
2398 struct consumer_socket
*socket
;
2399 struct lttng_ht_iter iter
;
2403 if (!output
|| !output
->socks
) {
2404 ERR("No consumer output found");
2409 path
= zmalloc(LTTNG_PATH_MAX
* sizeof(char));
2411 ERR("Cannot allocate mkdir path");
2416 ret
= snprintf(path
, LTTNG_PATH_MAX
, "%s%s%s",
2417 session_get_base_path(session
),
2418 output
->chunk_path
, output
->subdir
);
2419 if (ret
< 0 || ret
>= LTTNG_PATH_MAX
) {
2425 DBG("Domain mkdir %s for session %" PRIu64
, path
, session
->id
);
2428 * We have to iterate to find a socket, but we only need to send the
2429 * rename command to one consumer, so we break after the first one.
2431 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
2432 pthread_mutex_lock(socket
->lock
);
2433 ret
= consumer_mkdir(socket
, session
->id
, output
, path
, uid
, gid
);
2434 pthread_mutex_unlock(socket
->lock
);
2436 ERR("Consumer mkdir");
2453 int session_mkdir(const struct ltt_session
*session
)
2456 struct consumer_output
*output
;
2461 * Unsupported feature in lttng-relayd before 2.11, not an error since it
2462 * is only needed for session rotation and the user will get an error
2465 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
2466 session
->consumer
->relay_major_version
== 2 &&
2467 session
->consumer
->relay_minor_version
< 11) {
2472 if (session
->kernel_session
) {
2473 output
= session
->kernel_session
->consumer
;
2474 uid
= session
->kernel_session
->uid
;
2475 gid
= session
->kernel_session
->gid
;
2476 ret
= domain_mkdir(output
, session
, uid
, gid
);
2478 ERR("Mkdir kernel");
2483 if (session
->ust_session
) {
2484 output
= session
->ust_session
->consumer
;
2485 uid
= session
->ust_session
->uid
;
2486 gid
= session
->ust_session
->gid
;
2487 ret
= domain_mkdir(output
, session
, uid
, gid
);
2501 * Command LTTNG_START_TRACE processed by the client thread.
2503 * Called with session mutex held.
2505 int cmd_start_trace(struct ltt_session
*session
)
2508 unsigned long nb_chan
= 0;
2509 struct ltt_kernel_session
*ksession
;
2510 struct ltt_ust_session
*usess
;
2514 /* Ease our life a bit ;) */
2515 ksession
= session
->kernel_session
;
2516 usess
= session
->ust_session
;
2518 /* Is the session already started? */
2519 if (session
->active
) {
2520 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2525 * Starting a session without channel is useless since after that it's not
2526 * possible to enable channel thus inform the client.
2528 if (usess
&& usess
->domain_global
.channels
) {
2529 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2532 nb_chan
+= ksession
->channel_count
;
2535 ret
= LTTNG_ERR_NO_CHANNEL
;
2540 * Record the timestamp of the first time the session is started for
2541 * an eventual session rotation call.
2543 if (!session
->has_been_started
) {
2544 session
->current_chunk_start_ts
= time(NULL
);
2545 if (session
->current_chunk_start_ts
== (time_t) -1) {
2546 PERROR("Failed to retrieve the \"%s\" session's start time",
2548 ret
= LTTNG_ERR_FATAL
;
2551 if (!session
->snapshot_mode
&& session
->output_traces
) {
2552 ret
= session_mkdir(session
);
2554 ERR("Failed to create the session directories");
2555 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
2561 /* Kernel tracing */
2562 if (ksession
!= NULL
) {
2563 DBG("Start kernel tracing session %s", session
->name
);
2564 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2565 if (ret
!= LTTNG_OK
) {
2570 /* Flag session that trace should start automatically */
2573 * Even though the start trace might fail, flag this session active so
2574 * other application coming in are started by default.
2578 ret
= ust_app_start_trace_all(usess
);
2580 ret
= LTTNG_ERR_UST_START_FAIL
;
2585 /* Flag this after a successful start. */
2586 session
->has_been_started
= 1;
2587 session
->active
= 1;
2590 * Clear the flag that indicates that a rotation was done while the
2591 * session was stopped.
2593 session
->rotated_after_last_stop
= false;
2595 if (session
->rotate_timer_period
) {
2596 ret
= sessiond_rotate_timer_start(session
,
2597 session
->rotate_timer_period
);
2599 ERR("Failed to enable rotate timer");
2600 ret
= LTTNG_ERR_UNK
;
2612 int rename_active_chunk(struct ltt_session
*session
)
2616 session
->current_archive_id
++;
2619 * The currently active tracing path is now the folder we
2622 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
2623 session
->rotation_chunk
.active_tracing_path
,
2624 sizeof(session
->rotation_chunk
.current_rotate_path
));
2626 ERR("Failed to copy active tracing path");
2630 ret
= rename_complete_chunk(session
, time(NULL
));
2632 ERR("Failed to rename current rotate path");
2637 * We just renamed, the folder, we didn't do an actual rotation, so
2638 * the active tracing path is now the renamed folder and we have to
2639 * restore the rotate count.
2641 ret
= lttng_strncpy(session
->rotation_chunk
.active_tracing_path
,
2642 session
->rotation_chunk
.current_rotate_path
,
2643 sizeof(session
->rotation_chunk
.active_tracing_path
));
2645 ERR("Failed to rename active session chunk tracing path");
2649 session
->current_archive_id
--;
2654 * Command LTTNG_STOP_TRACE processed by the client thread.
2656 int cmd_stop_trace(struct ltt_session
*session
)
2659 struct ltt_kernel_channel
*kchan
;
2660 struct ltt_kernel_session
*ksession
;
2661 struct ltt_ust_session
*usess
;
2662 bool error_occured
= false;
2666 DBG("Begin stop session %s (id %" PRIu64
")", session
->name
, session
->id
);
2668 ksession
= session
->kernel_session
;
2669 usess
= session
->ust_session
;
2671 /* Session is not active. Skip everythong and inform the client. */
2672 if (!session
->active
) {
2673 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2677 if (session
->rotate_relay_pending_timer_enabled
) {
2678 sessiond_timer_rotate_pending_stop(session
);
2681 if (session
->rotate_timer_enabled
) {
2682 sessiond_rotate_timer_stop(session
);
2685 if (session
->current_archive_id
> 0 && !session
->rotate_pending
) {
2686 ret
= rename_active_chunk(session
);
2689 * This error should not prevent the user from stopping
2690 * the session. However, it will be reported at the end.
2692 error_occured
= true;
2697 if (ksession
&& ksession
->active
) {
2698 DBG("Stop kernel tracing");
2700 ret
= kernel_stop_session(ksession
);
2702 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2706 kernel_wait_quiescent(kernel_tracer_fd
);
2708 /* Flush metadata after stopping (if exists) */
2709 if (ksession
->metadata_stream_fd
>= 0) {
2710 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2712 ERR("Kernel metadata flush failed");
2716 /* Flush all buffers after stopping */
2717 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2718 ret
= kernel_flush_buffer(kchan
);
2720 ERR("Kernel flush buffer error");
2724 ksession
->active
= 0;
2725 DBG("Kernel session stopped %s (id %" PRIu64
")", session
->name
,
2729 if (usess
&& usess
->active
) {
2731 * Even though the stop trace might fail, flag this session inactive so
2732 * other application coming in are not started by default.
2736 ret
= ust_app_stop_trace_all(usess
);
2738 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2743 /* Flag inactive after a successful stop. */
2744 session
->active
= 0;
2745 ret
= !error_occured
? LTTNG_OK
: LTTNG_ERR_UNK
;
2752 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2754 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2755 struct lttng_uri
*uris
)
2758 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2759 struct ltt_ust_session
*usess
= session
->ust_session
;
2765 /* Can't set consumer URI if the session is active. */
2766 if (session
->active
) {
2767 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2771 /* Set the "global" consumer URIs */
2772 for (i
= 0; i
< nb_uri
; i
++) {
2773 ret
= add_uri_to_consumer(session
->consumer
,
2774 &uris
[i
], 0, session
->name
);
2775 if (ret
!= LTTNG_OK
) {
2780 /* Set UST session URIs */
2781 if (session
->ust_session
) {
2782 for (i
= 0; i
< nb_uri
; i
++) {
2783 ret
= add_uri_to_consumer(
2784 session
->ust_session
->consumer
,
2785 &uris
[i
], LTTNG_DOMAIN_UST
,
2787 if (ret
!= LTTNG_OK
) {
2793 /* Set kernel session URIs */
2794 if (session
->kernel_session
) {
2795 for (i
= 0; i
< nb_uri
; i
++) {
2796 ret
= add_uri_to_consumer(
2797 session
->kernel_session
->consumer
,
2798 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2800 if (ret
!= LTTNG_OK
) {
2807 * Make sure to set the session in output mode after we set URI since a
2808 * session can be created without URL (thus flagged in no output mode).
2810 session
->output_traces
= 1;
2812 ksess
->output_traces
= 1;
2816 usess
->output_traces
= 1;
2827 * Command LTTNG_CREATE_SESSION processed by the client thread.
2829 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2830 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2833 struct ltt_session
*session
;
2839 * Verify if the session already exist
2841 * XXX: There is no need for the session lock list here since the caller
2842 * (process_client_msg) is holding it. We might want to change that so a
2843 * single command does not lock the entire session list.
2845 session
= session_find_by_name(name
);
2846 if (session
!= NULL
) {
2847 ret
= LTTNG_ERR_EXIST_SESS
;
2851 /* Create tracing session in the registry */
2852 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2853 LTTNG_SOCK_GET_GID_CRED(creds
));
2854 if (ret
!= LTTNG_OK
) {
2859 * Get the newly created session pointer back
2861 * XXX: There is no need for the session lock list here since the caller
2862 * (process_client_msg) is holding it. We might want to change that so a
2863 * single command does not lock the entire session list.
2865 session
= session_find_by_name(name
);
2868 session
->live_timer
= live_timer
;
2869 /* Create default consumer output for the session not yet created. */
2870 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2871 if (session
->consumer
== NULL
) {
2872 ret
= LTTNG_ERR_FATAL
;
2873 goto consumer_error
;
2877 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2878 if (ret
!= LTTNG_OK
) {
2879 goto consumer_error
;
2881 session
->output_traces
= 1;
2883 session
->output_traces
= 0;
2884 DBG2("Session %s created with no output", session
->name
);
2887 session
->consumer
->enabled
= 1;
2892 session_destroy(session
);
2899 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2901 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2902 size_t nb_uri
, lttng_sock_cred
*creds
)
2905 struct ltt_session
*session
;
2906 struct snapshot_output
*new_output
= NULL
;
2912 * Create session in no output mode with URIs set to NULL. The uris we've
2913 * received are for a default snapshot output if one.
2915 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2916 if (ret
!= LTTNG_OK
) {
2920 /* Get the newly created session pointer back. This should NEVER fail. */
2921 session
= session_find_by_name(name
);
2924 /* Flag session for snapshot mode. */
2925 session
->snapshot_mode
= 1;
2927 /* Skip snapshot output creation if no URI is given. */
2932 new_output
= snapshot_output_alloc();
2934 ret
= LTTNG_ERR_NOMEM
;
2935 goto error_snapshot_alloc
;
2938 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2939 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2941 if (ret
== -ENOMEM
) {
2942 ret
= LTTNG_ERR_NOMEM
;
2944 ret
= LTTNG_ERR_INVALID
;
2946 goto error_snapshot
;
2950 snapshot_add_output(&session
->snapshot
, new_output
);
2957 snapshot_output_destroy(new_output
);
2958 error_snapshot_alloc
:
2959 session_destroy(session
);
2965 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2967 * Called with session lock held.
2969 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
,
2970 struct notification_thread_handle
*notification_thread_handle
)
2973 struct ltt_ust_session
*usess
;
2974 struct ltt_kernel_session
*ksess
;
2979 usess
= session
->ust_session
;
2980 ksess
= session
->kernel_session
;
2982 DBG("Begin destroy session %s (id %" PRIu64
")", session
->name
, session
->id
);
2984 if (session
->rotate_relay_pending_timer_enabled
) {
2985 sessiond_timer_rotate_pending_stop(session
);
2988 if (session
->rotate_timer_enabled
) {
2989 sessiond_rotate_timer_stop(session
);
2992 if (session
->rotate_size
) {
2993 unsubscribe_session_consumed_size_rotation(session
, notification_thread_handle
);
2994 session
->rotate_size
= 0;
2998 * The rename of the current chunk is performed at stop, but if we rotated
2999 * the session after the previous stop command, we need to rename the
3000 * new (and empty) chunk that was started in between.
3002 if (session
->rotated_after_last_stop
) {
3003 rename_active_chunk(session
);
3006 /* Clean kernel session teardown */
3007 kernel_destroy_session(ksess
);
3009 /* UST session teardown */
3011 /* Close any relayd session */
3012 consumer_output_send_destroy_relayd(usess
->consumer
);
3014 /* Destroy every UST application related to this session. */
3015 ret
= ust_app_destroy_trace_all(usess
);
3017 ERR("Error in ust_app_destroy_trace_all");
3020 /* Clean up the rest. */
3021 trace_ust_destroy_session(usess
);
3025 * Must notify the kernel thread here to update it's poll set in order to
3026 * remove the channel(s)' fd just destroyed.
3028 ret
= notify_thread_pipe(wpipe
);
3030 PERROR("write kernel poll pipe");
3033 ret
= session_destroy(session
);
3039 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3041 int cmd_register_consumer(struct ltt_session
*session
,
3042 enum lttng_domain_type domain
, const char *sock_path
,
3043 struct consumer_data
*cdata
)
3046 struct consumer_socket
*socket
= NULL
;
3053 case LTTNG_DOMAIN_KERNEL
:
3055 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3059 /* Can't register a consumer if there is already one */
3060 if (ksess
->consumer_fds_sent
!= 0) {
3061 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
3065 sock
= lttcomm_connect_unix_sock(sock_path
);
3067 ret
= LTTNG_ERR_CONNECT_FAIL
;
3070 cdata
->cmd_sock
= sock
;
3072 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
3073 if (socket
== NULL
) {
3076 PERROR("close register consumer");
3078 cdata
->cmd_sock
= -1;
3079 ret
= LTTNG_ERR_FATAL
;
3083 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
3084 if (socket
->lock
== NULL
) {
3085 PERROR("zmalloc pthread mutex");
3086 ret
= LTTNG_ERR_FATAL
;
3089 pthread_mutex_init(socket
->lock
, NULL
);
3090 socket
->registered
= 1;
3093 consumer_add_socket(socket
, ksess
->consumer
);
3096 pthread_mutex_lock(&cdata
->pid_mutex
);
3098 pthread_mutex_unlock(&cdata
->pid_mutex
);
3103 /* TODO: Userspace tracing */
3104 ret
= LTTNG_ERR_UND
;
3112 consumer_destroy_socket(socket
);
3118 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3120 ssize_t
cmd_list_domains(struct ltt_session
*session
,
3121 struct lttng_domain
**domains
)
3126 struct lttng_ht_iter iter
;
3128 if (session
->kernel_session
!= NULL
) {
3129 DBG3("Listing domains found kernel domain");
3133 if (session
->ust_session
!= NULL
) {
3134 DBG3("Listing domains found UST global domain");
3138 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3140 if (agt
->being_used
) {
3151 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
3152 if (*domains
== NULL
) {
3153 ret
= LTTNG_ERR_FATAL
;
3157 if (session
->kernel_session
!= NULL
) {
3158 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
3160 /* Kernel session buffer type is always GLOBAL */
3161 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
3166 if (session
->ust_session
!= NULL
) {
3167 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
3168 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3172 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
3174 if (agt
->being_used
) {
3175 (*domains
)[index
].type
= agt
->domain
;
3176 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
3186 /* Return negative value to differentiate return code */
3192 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3194 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
3195 struct ltt_session
*session
, struct lttng_channel
**channels
)
3197 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
3200 case LTTNG_DOMAIN_KERNEL
:
3201 if (session
->kernel_session
!= NULL
) {
3202 nb_chan
= session
->kernel_session
->channel_count
;
3204 DBG3("Number of kernel channels %zd", nb_chan
);
3206 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
3210 case LTTNG_DOMAIN_UST
:
3211 if (session
->ust_session
!= NULL
) {
3213 nb_chan
= lttng_ht_get_count(
3214 session
->ust_session
->domain_global
.channels
);
3217 DBG3("Number of UST global channels %zd", nb_chan
);
3219 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
3224 ret
= -LTTNG_ERR_UND
;
3229 const size_t channel_size
= sizeof(struct lttng_channel
) +
3230 sizeof(struct lttng_channel_extended
);
3231 struct lttng_channel_extended
*channel_exts
;
3233 payload_size
= nb_chan
* channel_size
;
3234 *channels
= zmalloc(payload_size
);
3235 if (*channels
== NULL
) {
3236 ret
= -LTTNG_ERR_FATAL
;
3240 channel_exts
= ((void *) *channels
) +
3241 (nb_chan
* sizeof(struct lttng_channel
));
3242 ret
= list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3243 if (ret
!= LTTNG_OK
) {
3258 * Command LTTNG_LIST_EVENTS processed by the client thread.
3260 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3261 struct ltt_session
*session
, char *channel_name
,
3262 struct lttng_event
**events
, size_t *total_size
)
3265 ssize_t nb_event
= 0;
3268 case LTTNG_DOMAIN_KERNEL
:
3269 if (session
->kernel_session
!= NULL
) {
3270 nb_event
= list_lttng_kernel_events(channel_name
,
3271 session
->kernel_session
, events
,
3275 case LTTNG_DOMAIN_UST
:
3277 if (session
->ust_session
!= NULL
) {
3278 nb_event
= list_lttng_ust_global_events(channel_name
,
3279 &session
->ust_session
->domain_global
, events
,
3284 case LTTNG_DOMAIN_LOG4J
:
3285 case LTTNG_DOMAIN_JUL
:
3286 case LTTNG_DOMAIN_PYTHON
:
3287 if (session
->ust_session
) {
3288 struct lttng_ht_iter iter
;
3292 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3293 &iter
.iter
, agt
, node
.node
) {
3294 if (agt
->domain
== domain
) {
3295 nb_event
= list_lttng_agent_events(
3305 ret
= LTTNG_ERR_UND
;
3312 /* Return negative value to differentiate return code */
3317 * Using the session list, filled a lttng_session array to send back to the
3318 * client for session listing.
3320 * The session list lock MUST be acquired before calling this function. Use
3321 * session_lock_list() and session_unlock_list().
3323 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3328 struct ltt_session
*session
;
3329 struct ltt_session_list
*list
= session_get_list();
3331 DBG("Getting all available session for UID %d GID %d",
3334 * Iterate over session list and append data after the control struct in
3337 cds_list_for_each_entry(session
, &list
->head
, list
) {
3339 * Only list the sessions the user can control.
3341 if (!session_access_ok(session
, uid
, gid
)) {
3345 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3346 struct ltt_ust_session
*usess
= session
->ust_session
;
3348 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3349 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3350 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3351 ret
= build_network_session_path(sessions
[i
].path
,
3352 sizeof(sessions
[i
].path
), session
);
3354 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3355 session
->consumer
->dst
.session_root_path
);
3358 PERROR("snprintf session path");
3362 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3363 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3364 sessions
[i
].enabled
= session
->active
;
3365 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3366 sessions
[i
].live_timer_interval
= session
->live_timer
;
3372 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3373 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3375 int cmd_data_pending(struct ltt_session
*session
)
3378 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3379 struct ltt_ust_session
*usess
= session
->ust_session
;
3383 DBG("Data pending for session %s", session
->name
);
3385 /* Session MUST be stopped to ask for data availability. */
3386 if (session
->active
) {
3387 ret
= LTTNG_ERR_SESSION_STARTED
;
3391 * If stopped, just make sure we've started before else the above call
3392 * will always send that there is data pending.
3394 * The consumer assumes that when the data pending command is received,
3395 * the trace has been started before or else no output data is written
3396 * by the streams which is a condition for data pending. So, this is
3397 * *VERY* important that we don't ask the consumer before a start
3400 if (!session
->has_been_started
) {
3407 * A rotation is still pending, we have to wait.
3409 if (session
->rotate_pending
) {
3410 DBG("Rotate still pending for session %s", session
->name
);
3415 if (ksess
&& ksess
->consumer
) {
3416 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3418 /* Data is still being extracted for the kernel. */
3423 if (usess
&& usess
->consumer
) {
3424 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3426 /* Data is still being extracted for the kernel. */
3431 /* Data is ready to be read by a viewer */
3439 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3441 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3443 int cmd_snapshot_add_output(struct ltt_session
*session
,
3444 struct lttng_snapshot_output
*output
, uint32_t *id
)
3447 struct snapshot_output
*new_output
;
3452 DBG("Cmd snapshot add output for session %s", session
->name
);
3455 * Can't create an output if the session is not set in no-output mode.
3457 if (session
->output_traces
) {
3458 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3462 /* Only one output is allowed until we have the "tee" feature. */
3463 if (session
->snapshot
.nb_output
== 1) {
3464 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3468 new_output
= snapshot_output_alloc();
3470 ret
= LTTNG_ERR_NOMEM
;
3474 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3475 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3476 &session
->snapshot
);
3478 if (ret
== -ENOMEM
) {
3479 ret
= LTTNG_ERR_NOMEM
;
3481 ret
= LTTNG_ERR_INVALID
;
3487 snapshot_add_output(&session
->snapshot
, new_output
);
3489 *id
= new_output
->id
;
3496 snapshot_output_destroy(new_output
);
3502 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3504 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3506 int cmd_snapshot_del_output(struct ltt_session
*session
,
3507 struct lttng_snapshot_output
*output
)
3510 struct snapshot_output
*sout
= NULL
;
3518 * Permission denied to create an output if the session is not
3519 * set in no output mode.
3521 if (session
->output_traces
) {
3522 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3527 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3529 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3530 } else if (*output
->name
!= '\0') {
3531 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3533 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3536 ret
= LTTNG_ERR_INVALID
;
3540 snapshot_delete_output(&session
->snapshot
, sout
);
3541 snapshot_output_destroy(sout
);
3550 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3552 * If no output is available, outputs is untouched and 0 is returned.
3554 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3556 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3557 struct lttng_snapshot_output
**outputs
)
3560 struct lttng_snapshot_output
*list
= NULL
;
3561 struct lttng_ht_iter iter
;
3562 struct snapshot_output
*output
;
3567 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3570 * Permission denied to create an output if the session is not
3571 * set in no output mode.
3573 if (session
->output_traces
) {
3574 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3578 if (session
->snapshot
.nb_output
== 0) {
3583 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3585 ret
= -LTTNG_ERR_NOMEM
;
3589 /* Copy list from session to the new list object. */
3591 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3592 output
, node
.node
) {
3593 assert(output
->consumer
);
3594 list
[idx
].id
= output
->id
;
3595 list
[idx
].max_size
= output
->max_size
;
3596 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3597 sizeof(list
[idx
].name
))) {
3598 ret
= -LTTNG_ERR_INVALID
;
3601 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3602 if (lttng_strncpy(list
[idx
].ctrl_url
,
3603 output
->consumer
->dst
.session_root_path
,
3604 sizeof(list
[idx
].ctrl_url
))) {
3605 ret
= -LTTNG_ERR_INVALID
;
3610 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3611 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3613 ret
= -LTTNG_ERR_NOMEM
;
3618 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3619 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3621 ret
= -LTTNG_ERR_NOMEM
;
3630 ret
= session
->snapshot
.nb_output
;
3639 * Check if we can regenerate the metadata for this session.
3640 * Only kernel, UST per-uid and non-live sessions are supported.
3642 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3645 int check_regenerate_metadata_support(struct ltt_session
*session
)
3651 if (session
->live_timer
!= 0) {
3652 ret
= LTTNG_ERR_LIVE_SESSION
;
3655 if (!session
->active
) {
3656 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3659 if (session
->ust_session
) {
3660 switch (session
->ust_session
->buffer_type
) {
3661 case LTTNG_BUFFER_PER_UID
:
3663 case LTTNG_BUFFER_PER_PID
:
3664 ret
= LTTNG_ERR_PER_PID_SESSION
;
3668 ret
= LTTNG_ERR_UNK
;
3672 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3673 session
->consumer
->relay_minor_version
< 8) {
3674 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3684 int clear_metadata_file(int fd
)
3689 lseek_ret
= lseek(fd
, 0, SEEK_SET
);
3690 if (lseek_ret
< 0) {
3696 ret
= ftruncate(fd
, 0);
3698 PERROR("ftruncate");
3707 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3710 struct buffer_reg_uid
*uid_reg
= NULL
;
3711 struct buffer_reg_session
*session_reg
= NULL
;
3714 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3715 struct ust_registry_session
*registry
;
3716 struct ust_registry_channel
*chan
;
3717 struct lttng_ht_iter iter_chan
;
3719 session_reg
= uid_reg
->registry
;
3720 registry
= session_reg
->reg
.ust
;
3722 pthread_mutex_lock(®istry
->lock
);
3723 registry
->metadata_len_sent
= 0;
3724 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3725 registry
->metadata_len
= 0;
3726 registry
->metadata_version
++;
3727 if (registry
->metadata_fd
> 0) {
3728 /* Clear the metadata file's content. */
3729 ret
= clear_metadata_file(registry
->metadata_fd
);
3731 pthread_mutex_unlock(®istry
->lock
);
3736 ret
= ust_metadata_session_statedump(registry
, NULL
,
3737 registry
->major
, registry
->minor
);
3739 pthread_mutex_unlock(®istry
->lock
);
3740 ERR("Failed to generate session metadata (err = %d)",
3744 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3746 struct ust_registry_event
*event
;
3747 struct lttng_ht_iter iter_event
;
3749 ret
= ust_metadata_channel_statedump(registry
, chan
);
3751 pthread_mutex_unlock(®istry
->lock
);
3752 ERR("Failed to generate channel metadata "
3756 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3758 ret
= ust_metadata_event_statedump(registry
,
3761 pthread_mutex_unlock(®istry
->lock
);
3762 ERR("Failed to generate event metadata "
3768 pthread_mutex_unlock(®istry
->lock
);
3777 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3779 * Ask the consumer to truncate the existing metadata file(s) and
3780 * then regenerate the metadata. Live and per-pid sessions are not
3781 * supported and return an error.
3783 * Return 0 on success or else a LTTNG_ERR code.
3785 int cmd_regenerate_metadata(struct ltt_session
*session
)
3791 ret
= check_regenerate_metadata_support(session
);
3796 if (session
->kernel_session
) {
3797 ret
= kernctl_session_regenerate_metadata(
3798 session
->kernel_session
->fd
);
3800 ERR("Failed to regenerate the kernel metadata");
3805 if (session
->ust_session
) {
3806 ret
= ust_regenerate_metadata(session
->ust_session
);
3808 ERR("Failed to regenerate the UST metadata");
3812 DBG("Cmd metadata regenerate for session %s", session
->name
);
3820 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3822 * Ask the tracer to regenerate a new statedump.
3824 * Return 0 on success or else a LTTNG_ERR code.
3826 int cmd_regenerate_statedump(struct ltt_session
*session
)
3832 if (!session
->active
) {
3833 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3837 if (session
->kernel_session
) {
3838 ret
= kernctl_session_regenerate_statedump(
3839 session
->kernel_session
->fd
);
3841 * Currently, the statedump in kernel can only fail if out
3845 if (ret
== -ENOMEM
) {
3846 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3848 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3850 ERR("Failed to regenerate the kernel statedump");
3855 if (session
->ust_session
) {
3856 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3858 * Currently, the statedump in UST always returns 0.
3861 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3862 ERR("Failed to regenerate the UST statedump");
3866 DBG("Cmd regenerate statedump for session %s", session
->name
);
3873 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3874 struct notification_thread_handle
*notification_thread
)
3878 ssize_t sock_recv_len
;
3879 struct lttng_trigger
*trigger
= NULL
;
3880 struct lttng_buffer_view view
;
3881 struct lttng_dynamic_buffer trigger_buffer
;
3883 lttng_dynamic_buffer_init(&trigger_buffer
);
3884 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3885 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3887 ret
= LTTNG_ERR_NOMEM
;
3891 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3893 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3894 ERR("Failed to receive \"register trigger\" command payload");
3895 /* TODO: should this be a new error enum ? */
3896 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3900 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3901 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3903 ERR("Invalid trigger payload received in \"register trigger\" command");
3904 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3908 ret
= notification_thread_command_register_trigger(notification_thread
,
3910 /* Ownership of trigger was transferred. */
3913 lttng_trigger_destroy(trigger
);
3914 lttng_dynamic_buffer_reset(&trigger_buffer
);
3918 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3919 struct notification_thread_handle
*notification_thread
)
3923 ssize_t sock_recv_len
;
3924 struct lttng_trigger
*trigger
= NULL
;
3925 struct lttng_buffer_view view
;
3926 struct lttng_dynamic_buffer trigger_buffer
;
3928 lttng_dynamic_buffer_init(&trigger_buffer
);
3929 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3930 ret
= lttng_dynamic_buffer_set_size(&trigger_buffer
, trigger_len
);
3932 ret
= LTTNG_ERR_NOMEM
;
3936 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
.data
,
3938 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3939 ERR("Failed to receive \"unregister trigger\" command payload");
3940 /* TODO: should this be a new error enum ? */
3941 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3945 view
= lttng_buffer_view_from_dynamic_buffer(&trigger_buffer
, 0, -1);
3946 if (lttng_trigger_create_from_buffer(&view
, &trigger
) !=
3948 ERR("Invalid trigger payload received in \"unregister trigger\" command");
3949 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3953 ret
= notification_thread_command_unregister_trigger(notification_thread
,
3956 lttng_trigger_destroy(trigger
);
3957 lttng_dynamic_buffer_reset(&trigger_buffer
);
3962 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3963 * snapshot output is *not* set with a remote destination.
3965 * Return 0 on success or a LTTNG_ERR code.
3967 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3968 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3971 struct lttng_ht_iter iter
;
3972 struct consumer_socket
*socket
;
3975 assert(snap_output
);
3978 DBG2("Set relayd object from snapshot output");
3980 /* Ignore if snapshot consumer output is not network. */
3981 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3986 * For each consumer socket, create and send the relayd object of the
3990 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3991 socket
, node
.node
) {
3992 pthread_mutex_lock(socket
->lock
);
3993 ret
= send_consumer_relayd_sockets(0, session
->id
,
3994 snap_output
->consumer
, socket
,
3995 session
->name
, session
->hostname
,
3996 session
->live_timer
);
3997 pthread_mutex_unlock(socket
->lock
);
3998 if (ret
!= LTTNG_OK
) {
4010 * Record a kernel snapshot.
4012 * Return LTTNG_OK on success or a LTTNG_ERR code.
4014 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
4015 struct snapshot_output
*output
, struct ltt_session
*session
,
4016 int wait
, uint64_t nb_packets_per_stream
)
4026 * Copy kernel session sockets so we can communicate with the right
4027 * consumer for the snapshot record command.
4029 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
4031 ret
= LTTNG_ERR_NOMEM
;
4035 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
4036 if (ret
!= LTTNG_OK
) {
4037 goto error_snapshot
;
4040 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
4041 if (ret
!= LTTNG_OK
) {
4042 goto error_snapshot
;
4049 /* Clean up copied sockets so this output can use some other later on. */
4050 consumer_destroy_output_sockets(output
->consumer
);
4057 * Record a UST snapshot.
4059 * Return 0 on success or a LTTNG_ERR error code.
4061 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
4062 struct snapshot_output
*output
, struct ltt_session
*session
,
4063 int wait
, uint64_t nb_packets_per_stream
)
4072 * Copy UST session sockets so we can communicate with the right
4073 * consumer for the snapshot record command.
4075 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
4077 ret
= LTTNG_ERR_NOMEM
;
4081 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
4082 if (ret
!= LTTNG_OK
) {
4083 goto error_snapshot
;
4086 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
4090 ret
= LTTNG_ERR_INVALID
;
4093 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4096 goto error_snapshot
;
4102 /* Clean up copied sockets so this output can use some other later on. */
4103 consumer_destroy_output_sockets(output
->consumer
);
4109 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
4110 uint64_t cur_nr_packets
)
4112 uint64_t tot_size
= 0;
4114 if (session
->kernel_session
) {
4115 struct ltt_kernel_channel
*chan
;
4116 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
4118 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
4119 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
4121 * Don't take channel into account if we
4122 * already grab all its packets.
4126 tot_size
+= chan
->channel
->attr
.subbuf_size
4127 * chan
->stream_count
;
4131 if (session
->ust_session
) {
4132 struct ltt_ust_session
*usess
= session
->ust_session
;
4134 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
4142 * Calculate the number of packets we can grab from each stream that
4143 * fits within the overall snapshot max size.
4145 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4146 * the number of packets per stream.
4148 * TODO: this approach is not perfect: we consider the worse case
4149 * (packet filling the sub-buffers) as an upper bound, but we could do
4150 * better if we do this calculation while we actually grab the packet
4151 * content: we would know how much padding we don't actually store into
4154 * This algorithm is currently bounded by the number of packets per
4157 * Since we call this algorithm before actually grabbing the data, it's
4158 * an approximation: for instance, applications could appear/disappear
4159 * in between this call and actually grabbing data.
4162 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
4165 uint64_t cur_nb_packets
= 0;
4168 return 0; /* Infinite */
4171 size_left
= max_size
;
4173 uint64_t one_more_packet_tot_size
;
4175 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
4177 if (!one_more_packet_tot_size
) {
4178 /* We are already grabbing all packets. */
4181 size_left
-= one_more_packet_tot_size
;
4182 if (size_left
< 0) {
4187 if (!cur_nb_packets
) {
4188 /* Not enough room to grab one packet of each stream, error. */
4191 return cur_nb_packets
;
4195 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4197 * The wait parameter is ignored so this call always wait for the snapshot to
4198 * complete before returning.
4200 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4202 int cmd_snapshot_record(struct ltt_session
*session
,
4203 struct lttng_snapshot_output
*output
, int wait
)
4206 unsigned int use_tmp_output
= 0;
4207 struct snapshot_output tmp_output
;
4208 unsigned int snapshot_success
= 0;
4214 DBG("Cmd snapshot record for session %s", session
->name
);
4216 /* Get the datetime for the snapshot output directory. */
4217 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
4220 ret
= LTTNG_ERR_INVALID
;
4225 * Permission denied to create an output if the session is not
4226 * set in no output mode.
4228 if (session
->output_traces
) {
4229 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
4233 /* The session needs to be started at least once. */
4234 if (!session
->has_been_started
) {
4235 ret
= LTTNG_ERR_START_SESSION_ONCE
;
4239 /* Use temporary output for the session. */
4240 if (*output
->ctrl_url
!= '\0') {
4241 ret
= snapshot_output_init(output
->max_size
, output
->name
,
4242 output
->ctrl_url
, output
->data_url
, session
->consumer
,
4245 if (ret
== -ENOMEM
) {
4246 ret
= LTTNG_ERR_NOMEM
;
4248 ret
= LTTNG_ERR_INVALID
;
4252 /* Use the global session count for the temporary snapshot. */
4253 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4255 /* Use the global datetime */
4256 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4260 if (use_tmp_output
) {
4261 int64_t nb_packets_per_stream
;
4263 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4264 tmp_output
.max_size
);
4265 if (nb_packets_per_stream
< 0) {
4266 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4270 if (session
->kernel_session
) {
4271 ret
= record_kernel_snapshot(session
->kernel_session
,
4272 &tmp_output
, session
,
4273 wait
, nb_packets_per_stream
);
4274 if (ret
!= LTTNG_OK
) {
4279 if (session
->ust_session
) {
4280 ret
= record_ust_snapshot(session
->ust_session
,
4281 &tmp_output
, session
,
4282 wait
, nb_packets_per_stream
);
4283 if (ret
!= LTTNG_OK
) {
4288 snapshot_success
= 1;
4290 struct snapshot_output
*sout
;
4291 struct lttng_ht_iter iter
;
4294 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
4295 &iter
.iter
, sout
, node
.node
) {
4296 int64_t nb_packets_per_stream
;
4299 * Make a local copy of the output and assign the possible
4300 * temporary value given by the caller.
4302 memset(&tmp_output
, 0, sizeof(tmp_output
));
4303 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
4305 if (output
->max_size
!= (uint64_t) -1ULL) {
4306 tmp_output
.max_size
= output
->max_size
;
4309 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
4310 tmp_output
.max_size
);
4311 if (nb_packets_per_stream
< 0) {
4312 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4317 /* Use temporary name. */
4318 if (*output
->name
!= '\0') {
4319 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4320 sizeof(tmp_output
.name
))) {
4321 ret
= LTTNG_ERR_INVALID
;
4327 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4328 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4330 if (session
->kernel_session
) {
4331 ret
= record_kernel_snapshot(session
->kernel_session
,
4332 &tmp_output
, session
,
4333 wait
, nb_packets_per_stream
);
4334 if (ret
!= LTTNG_OK
) {
4340 if (session
->ust_session
) {
4341 ret
= record_ust_snapshot(session
->ust_session
,
4342 &tmp_output
, session
,
4343 wait
, nb_packets_per_stream
);
4344 if (ret
!= LTTNG_OK
) {
4349 snapshot_success
= 1;
4354 if (snapshot_success
) {
4355 session
->snapshot
.nb_snapshot
++;
4357 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4365 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4367 int cmd_set_session_shm_path(struct ltt_session
*session
,
4368 const char *shm_path
)
4374 * Can only set shm path before session is started.
4376 if (session
->has_been_started
) {
4377 return LTTNG_ERR_SESSION_STARTED
;
4380 strncpy(session
->shm_path
, shm_path
,
4381 sizeof(session
->shm_path
));
4382 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4388 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4390 * Ask the consumer to rotate the session output directory.
4391 * The session lock must be held.
4393 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4395 int cmd_rotate_session(struct ltt_session
*session
,
4396 struct lttng_rotate_session_return
*rotate_return
)
4400 struct tm
*timeinfo
;
4403 bool ust_active
= false;
4407 if (!session
->has_been_started
) {
4408 ret
= -LTTNG_ERR_START_SESSION_ONCE
;
4412 if (session
->live_timer
|| session
->snapshot_mode
||
4413 !session
->output_traces
) {
4414 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4419 * Unsupported feature in lttng-relayd before 2.11.
4421 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
4422 (session
->consumer
->relay_major_version
== 2 &&
4423 session
->consumer
->relay_minor_version
< 11)) {
4424 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY
;
4428 if (session
->rotate_pending
|| session
->rotate_pending_relay
) {
4429 ret
= -LTTNG_ERR_ROTATION_PENDING
;
4430 DBG("Rotate already in progress");
4435 * After a stop, we only allow one rotation to occur, the other ones are
4436 * useless until a new start.
4438 if (session
->rotated_after_last_stop
) {
4439 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4441 ret
= -LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP
;
4445 /* Special case for the first rotation. */
4446 if (session
->current_archive_id
== 0) {
4447 const char *base_path
= NULL
;
4449 /* Either one of the two sessions is enough to get the root path. */
4450 if (session
->kernel_session
) {
4451 base_path
= session_get_base_path(session
);
4452 } else if (session
->ust_session
) {
4453 base_path
= session_get_base_path(session
);
4458 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4460 sizeof(session
->rotation_chunk
.current_rotate_path
));
4462 ERR("Failed to copy session base path to current rotation chunk path");
4463 ret
= -LTTNG_ERR_UNK
;
4468 * The currently active tracing path is now the folder we
4471 ret
= lttng_strncpy(session
->rotation_chunk
.current_rotate_path
,
4472 session
->rotation_chunk
.active_tracing_path
,
4473 sizeof(session
->rotation_chunk
.current_rotate_path
));
4475 ERR("Failed to copy the active tracing path to the current rotate path");
4476 ret
= -LTTNG_ERR_UNK
;
4480 DBG("Current rotate path %s", session
->rotation_chunk
.current_rotate_path
);
4482 session
->current_archive_id
++;
4483 session
->rotate_pending
= true;
4484 session
->rotation_state
= LTTNG_ROTATION_STATE_ONGOING
;
4487 * Create the path name for the next chunk.
4490 if (now
== (time_t) -1) {
4491 ret
= -LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4494 session
->last_chunk_start_ts
= session
->current_chunk_start_ts
;
4495 session
->current_chunk_start_ts
= now
;
4497 timeinfo
= localtime(&now
);
4499 PERROR("Failed to sample local time in rotate session command");
4500 ret
= -LTTNG_ERR_UNK
;
4503 strf_ret
= strftime(datetime
, sizeof(datetime
), "%Y%m%dT%H%M%S%z",
4506 ERR("Failed to format local time timestamp in rotate session command");
4507 ret
= -LTTNG_ERR_UNK
;
4510 if (session
->kernel_session
) {
4512 * The active path for the next rotation/destroy.
4513 * Ex: ~/lttng-traces/auto-20170922-111748/20170922-111754-42
4515 ret
= snprintf(session
->rotation_chunk
.active_tracing_path
,
4516 sizeof(session
->rotation_chunk
.active_tracing_path
),
4518 session_get_base_path(session
),
4519 datetime
, session
->current_archive_id
+ 1);
4520 if (ret
< 0 || ret
== sizeof(session
->rotation_chunk
.active_tracing_path
)) {
4521 ERR("Failed to format active kernel tracing path in rotate session command");
4522 ret
= -LTTNG_ERR_UNK
;
4526 * The sub-directory for the consumer
4527 * Ex: /20170922-111754-42/kernel
4529 ret
= snprintf(session
->kernel_session
->consumer
->chunk_path
,
4530 sizeof(session
->kernel_session
->consumer
->chunk_path
),
4531 "/%s-%" PRIu64
, datetime
,
4532 session
->current_archive_id
+ 1);
4533 if (ret
< 0 || ret
== sizeof(session
->kernel_session
->consumer
->chunk_path
)) {
4534 ERR("Failed to format the kernel consumer's sub-directory in rotate session command");
4535 ret
= -LTTNG_ERR_UNK
;
4539 * Create the new chunk folder, before the rotation begins so we don't
4540 * race with the consumer/tracer activity.
4542 ret
= domain_mkdir(session
->kernel_session
->consumer
, session
,
4543 session
->kernel_session
->uid
,
4544 session
->kernel_session
->gid
);
4546 ERR("Failed to create kernel session tracing path at %s",
4547 session
->kernel_session
->consumer
->chunk_path
);
4548 ret
= -LTTNG_ERR_CREATE_DIR_FAIL
;
4551 ret
= kernel_rotate_session(session
);
4552 if (ret
!= LTTNG_OK
) {
4557 if (session
->ust_session
) {
4558 ret
= snprintf(session
->rotation_chunk
.active_tracing_path
,
4559 PATH_MAX
, "%s/%s-%" PRIu64
,
4560 session_get_base_path(session
),
4561 datetime
, session
->current_archive_id
+ 1);
4563 ERR("Failed to format active UST tracing path in rotate session command");
4564 ret
= -LTTNG_ERR_UNK
;
4567 ret
= snprintf(session
->ust_session
->consumer
->chunk_path
,
4568 PATH_MAX
, "/%s-%" PRIu64
, datetime
,
4569 session
->current_archive_id
+ 1);
4571 ERR("Failed to format the UST consumer's sub-directory in rotate session command");
4572 ret
= -LTTNG_ERR_UNK
;
4576 * Create the new chunk folder, before the rotation begins so we don't
4577 * race with the consumer/tracer activity.
4579 ret
= domain_mkdir(session
->ust_session
->consumer
, session
,
4580 session
->ust_session
->uid
,
4581 session
->ust_session
->gid
);
4583 ret
= -LTTNG_ERR_CREATE_DIR_FAIL
;
4586 ret
= ust_app_rotate_session(session
, &ust_active
);
4587 if (ret
!= LTTNG_OK
) {
4591 * Handle the case where we did not start a rotation on any channel.
4592 * The consumer will never wake up the rotation thread to perform the
4593 * rename, so we have to do it here while we hold the session and
4594 * session_list locks.
4596 if (!session
->kernel_session
&& !ust_active
) {
4597 ret
= rename_complete_chunk(session
, now
);
4599 ERR("Failed to rename completed rotation chunk");
4602 session
->rotate_pending
= false;
4603 session
->rotation_state
= LTTNG_ROTATION_STATE_COMPLETED
;
4607 if (!session
->active
) {
4608 session
->rotated_after_last_stop
= true;
4611 if (rotate_return
) {
4612 rotate_return
->rotation_id
= session
->current_archive_id
;
4615 DBG("Cmd rotate session %s, current_archive_id %" PRIu64
" sent",
4616 session
->name
, session
->current_archive_id
);
4624 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
4626 * Check if the session has finished its rotation.
4628 * Return 0 on success or else a LTTNG_ERR code.
4630 int cmd_rotate_get_info(struct ltt_session
*session
,
4631 struct lttng_rotation_get_info_return
*info_return
,
4632 uint64_t rotation_id
)
4638 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64
, session
->name
,
4639 session
->current_archive_id
);
4641 if (session
->current_archive_id
!= rotation_id
) {
4642 info_return
->status
= (int32_t) LTTNG_ROTATION_STATE_EXPIRED
;
4647 switch (session
->rotation_state
) {
4648 case LTTNG_ROTATION_STATE_ONGOING
:
4649 DBG("Reporting that rotation id %" PRIu64
" of session %s is still pending",
4650 rotation_id
, session
->name
);
4652 case LTTNG_ROTATION_STATE_COMPLETED
:
4654 char *current_tracing_path_reply
;
4655 size_t current_tracing_path_reply_len
;
4657 switch (session_get_consumer_destination_type(session
)) {
4658 case CONSUMER_DST_LOCAL
:
4659 current_tracing_path_reply
=
4660 info_return
->location
.local
.absolute_path
;
4661 current_tracing_path_reply_len
=
4662 sizeof(info_return
->location
.local
.absolute_path
);
4663 info_return
->location_type
=
4664 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL
;
4666 case CONSUMER_DST_NET
:
4667 current_tracing_path_reply
=
4668 info_return
->location
.relay
.relative_path
;
4669 current_tracing_path_reply_len
=
4670 sizeof(info_return
->location
.relay
.relative_path
);
4671 /* Currently the only supported relay protocol. */
4672 info_return
->location
.relay
.protocol
=
4673 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP
;
4675 ret
= lttng_strncpy(info_return
->location
.relay
.host
,
4676 session_get_net_consumer_hostname(session
),
4677 sizeof(info_return
->location
.relay
.host
));
4679 ERR("Failed to host name to rotate_get_info reply");
4680 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4681 ret
= -LTTNG_ERR_UNK
;
4685 session_get_net_consumer_ports(session
,
4686 &info_return
->location
.relay
.ports
.control
,
4687 &info_return
->location
.relay
.ports
.data
);
4688 info_return
->location_type
=
4689 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY
;
4694 ret
= lttng_strncpy(current_tracing_path_reply
,
4695 session
->rotation_chunk
.current_rotate_path
,
4696 current_tracing_path_reply_len
);
4698 ERR("Failed to copy current tracing path to rotate_get_info reply");
4699 info_return
->status
= LTTNG_ROTATION_STATUS_ERROR
;
4700 ret
= -LTTNG_ERR_UNK
;
4706 case LTTNG_ROTATION_STATE_ERROR
:
4707 DBG("Reporting that an error occurred during rotation %" PRIu64
" of session %s",
4708 rotation_id
, session
->name
);
4714 info_return
->status
= (int32_t) session
->rotation_state
;
4721 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
4723 * Configure the automatic rotation parameters.
4724 * Set to -1ULL to disable them.
4726 * Return 0 on success or else an LTTNG_ERR code.
4728 int cmd_rotation_set_schedule(struct ltt_session
*session
,
4729 uint64_t timer_us
, uint64_t size
,
4730 struct notification_thread_handle
*notification_thread_handle
)
4736 DBG("Cmd rotate set schedule session %s", session
->name
);
4738 if (session
->live_timer
|| session
->snapshot_mode
||
4739 !session
->output_traces
) {
4740 ret
= LTTNG_ERR_ROTATION_NOT_AVAILABLE
;
4744 /* Trying to override an already active timer. */
4745 if (timer_us
&& timer_us
!= -1ULL && session
->rotate_timer_period
) {
4746 ret
= LTTNG_ERR_ROTATION_TIMER_SET
;
4748 /* Trying to disable an inactive timer. */
4749 } else if (timer_us
== -1ULL && !session
->rotate_timer_period
) {
4750 ret
= LTTNG_ERR_ROTATION_NO_TIMER_SET
;
4754 if (size
&& size
!= -1ULL && session
->rotate_size
) {
4755 ret
= LTTNG_ERR_ROTATION_SIZE_SET
;
4757 } else if (size
== -1ULL && !session
->rotate_size
) {
4758 ret
= LTTNG_ERR_ROTATION_NO_SIZE_SET
;
4762 if (timer_us
&& !session
->rotate_timer_period
) {
4763 if (timer_us
> UINT_MAX
) {
4764 ret
= LTTNG_ERR_INVALID
;
4768 session
->rotate_timer_period
= timer_us
;
4770 * Only start the timer if the session is active, otherwise
4771 * it will be started when the session starts.
4773 if (session
->active
) {
4774 ret
= sessiond_rotate_timer_start(session
, timer_us
);
4776 ERR("Failed to enable rotate timer");
4777 ret
= LTTNG_ERR_UNK
;
4781 } else if (timer_us
== -1ULL && session
->rotate_timer_period
> 0) {
4782 sessiond_rotate_timer_stop(session
);
4783 session
->rotate_timer_period
= 0;
4787 if (size
== -1ULL) {
4788 ret
= unsubscribe_session_consumed_size_rotation(session
,
4789 notification_thread_handle
);
4791 ret
= LTTNG_ERR_UNK
;
4794 session
->rotate_size
= 0;
4796 ret
= subscribe_session_consumed_size_rotation(session
,
4797 size
, notification_thread_handle
);
4799 PERROR("Subscribe to session usage");
4800 ret
= LTTNG_ERR_UNK
;
4803 session
->rotate_size
= size
;
4816 * Command ROTATE_GET_CURRENT_PATH from the lttng-ctl library.
4818 * Configure the automatic rotation parameters.
4819 * Set to -1ULL to disable them.
4821 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4823 int cmd_session_get_current_output(struct ltt_session
*session
,
4824 struct lttng_session_get_current_output_return
*output_return
)
4829 if (!session
->snapshot_mode
) {
4830 if (session
->current_archive_id
== 0) {
4831 if (session
->kernel_session
) {
4832 path
= session_get_base_path(session
);
4833 } else if (session
->ust_session
) {
4834 path
= session_get_base_path(session
);
4840 path
= session
->rotation_chunk
.active_tracing_path
;
4844 * A snapshot session does not have a "current" trace archive
4850 DBG("Cmd get current output for session %s, returning %s",
4851 session
->name
, path
);
4853 ret
= lttng_strncpy(output_return
->path
,
4855 sizeof(output_return
->path
));
4857 ERR("Failed to copy trace output path to session get current output command reply");
4858 ret
= -LTTNG_ERR_UNK
;
4868 * Init command subsystem.
4873 * Set network sequence index to 1 for streams to match a relayd
4874 * socket on the consumer side.
4876 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4877 relayd_net_seq_idx
= 1;
4878 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4880 DBG("Command subsystem initialized");