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 <lttng/trigger/trigger-internal.h>
37 #include "health-sessiond.h"
39 #include "kernel-consumer.h"
40 #include "lttng-sessiond.h"
44 #include "buffer-registry.h"
45 #include "notification-thread.h"
46 #include "notification-thread-commands.h"
51 * Used to keep a unique index for each relayd socket created where this value
52 * is associated with streams on the consumer so it can match the right relayd
53 * to send to. It must be accessed with the relayd_net_seq_idx_lock
56 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
57 static uint64_t relayd_net_seq_idx
;
59 static int validate_event_name(const char *);
60 static int validate_ust_event_name(const char *);
61 static int cmd_enable_event_internal(struct ltt_session
*session
,
62 struct lttng_domain
*domain
,
63 char *channel_name
, struct lttng_event
*event
,
64 char *filter_expression
,
65 struct lttng_filter_bytecode
*filter
,
66 struct lttng_event_exclusion
*exclusion
,
70 * Create a session path used by list_lttng_sessions for the case that the
71 * session consumer is on the network.
73 static int build_network_session_path(char *dst
, size_t size
,
74 struct ltt_session
*session
)
76 int ret
, kdata_port
, udata_port
;
77 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
78 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
83 memset(tmp_urls
, 0, sizeof(tmp_urls
));
84 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
86 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
88 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
89 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
90 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
93 if (session
->ust_session
&& session
->ust_session
->consumer
) {
94 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
95 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
98 if (uuri
== NULL
&& kuri
== NULL
) {
99 uri
= &session
->consumer
->dst
.net
.control
;
100 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
101 } else if (kuri
&& uuri
) {
102 ret
= uri_compare(kuri
, uuri
);
106 /* Build uuri URL string */
107 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
114 } else if (kuri
&& uuri
== NULL
) {
116 } else if (uuri
&& kuri
== NULL
) {
120 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
126 * Do we have a UST url set. If yes, this means we have both kernel and UST
129 if (*tmp_uurl
!= '\0') {
130 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
131 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
134 if (kuri
|| (!kuri
&& !uuri
)) {
137 /* No kernel URI, use the UST port. */
140 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
148 * Get run-time attributes if the session has been started (discarded events,
151 static int get_kernel_runtime_stats(struct ltt_session
*session
,
152 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
153 uint64_t *lost_packets
)
157 if (!session
->has_been_started
) {
159 *discarded_events
= 0;
164 ret
= consumer_get_discarded_events(session
->id
, kchan
->fd
,
165 session
->kernel_session
->consumer
,
171 ret
= consumer_get_lost_packets(session
->id
, kchan
->fd
,
172 session
->kernel_session
->consumer
,
183 * Get run-time attributes if the session has been started (discarded events,
186 static int get_ust_runtime_stats(struct ltt_session
*session
,
187 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
188 uint64_t *lost_packets
)
191 struct ltt_ust_session
*usess
;
193 if (!discarded_events
|| !lost_packets
) {
198 usess
= session
->ust_session
;
199 assert(discarded_events
);
200 assert(lost_packets
);
202 if (!usess
|| !session
->has_been_started
) {
203 *discarded_events
= 0;
209 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
210 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
211 &usess
->buffer_reg_uid_list
,
212 usess
->consumer
, uchan
->id
,
213 uchan
->attr
.overwrite
,
216 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
217 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
218 uchan
, usess
->consumer
,
219 uchan
->attr
.overwrite
,
225 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
226 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
228 ERR("Unsupported buffer type");
239 * Fill lttng_channel array of all channels.
241 static void list_lttng_channels(enum lttng_domain_type domain
,
242 struct ltt_session
*session
, struct lttng_channel
*channels
,
243 struct lttcomm_channel_extended
*chan_exts
)
246 struct ltt_kernel_channel
*kchan
;
248 DBG("Listing channels for session %s", session
->name
);
251 case LTTNG_DOMAIN_KERNEL
:
252 /* Kernel channels */
253 if (session
->kernel_session
!= NULL
) {
254 cds_list_for_each_entry(kchan
,
255 &session
->kernel_session
->channel_list
.head
, list
) {
256 uint64_t discarded_events
, lost_packets
;
258 ret
= get_kernel_runtime_stats(session
, kchan
,
259 &discarded_events
, &lost_packets
);
263 /* Copy lttng_channel struct to array */
264 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
265 channels
[i
].enabled
= kchan
->enabled
;
266 chan_exts
[i
].discarded_events
=
268 chan_exts
[i
].lost_packets
= lost_packets
;
273 case LTTNG_DOMAIN_UST
:
275 struct lttng_ht_iter iter
;
276 struct ltt_ust_channel
*uchan
;
279 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
280 &iter
.iter
, uchan
, node
.node
) {
281 uint64_t discarded_events
= 0, lost_packets
= 0;
283 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
284 LTTNG_SYMBOL_NAME_LEN
)) {
287 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
288 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
289 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
290 channels
[i
].attr
.switch_timer_interval
=
291 uchan
->attr
.switch_timer_interval
;
292 channels
[i
].attr
.read_timer_interval
=
293 uchan
->attr
.read_timer_interval
;
294 channels
[i
].enabled
= uchan
->enabled
;
295 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
296 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
299 * Map enum lttng_ust_output to enum lttng_event_output.
301 switch (uchan
->attr
.output
) {
303 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
307 * LTTNG_UST_MMAP is the only supported UST
314 ret
= get_ust_runtime_stats(session
, uchan
,
315 &discarded_events
, &lost_packets
);
319 chan_exts
[i
].discarded_events
= discarded_events
;
320 chan_exts
[i
].lost_packets
= lost_packets
;
334 static void increment_extended_len(const char *filter_expression
,
335 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
337 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
339 if (filter_expression
) {
340 *extended_len
+= strlen(filter_expression
) + 1;
344 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
348 static void append_extended_info(const char *filter_expression
,
349 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
351 struct lttcomm_event_extended_header extended_header
;
352 size_t filter_len
= 0;
353 size_t nb_exclusions
= 0;
355 if (filter_expression
) {
356 filter_len
= strlen(filter_expression
) + 1;
360 nb_exclusions
= exclusion
->count
;
363 /* Set header fields */
364 extended_header
.filter_len
= filter_len
;
365 extended_header
.nb_exclusions
= nb_exclusions
;
368 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
369 *extended_at
+= sizeof(extended_header
);
371 /* Copy filter string */
372 if (filter_expression
) {
373 memcpy(*extended_at
, filter_expression
, filter_len
);
374 *extended_at
+= filter_len
;
377 /* Copy exclusion names */
379 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
381 memcpy(*extended_at
, &exclusion
->names
, len
);
387 * Create a list of agent domain events.
389 * Return number of events in list on success or else a negative value.
391 static int list_lttng_agent_events(struct agent
*agt
,
392 struct lttng_event
**events
, size_t *total_size
)
395 unsigned int nb_event
= 0;
396 struct agent_event
*event
;
397 struct lttng_event
*tmp_events
;
398 struct lttng_ht_iter iter
;
399 size_t extended_len
= 0;
405 DBG3("Listing agent events");
408 nb_event
= lttng_ht_get_count(agt
->events
);
416 /* Compute required extended infos size */
417 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
420 * This is only valid because the commands which add events are
421 * processed in the same thread as the listing.
424 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
425 increment_extended_len(event
->filter_expression
, NULL
,
430 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
431 tmp_events
= zmalloc(*total_size
);
433 PERROR("zmalloc agent events session");
434 ret
= -LTTNG_ERR_FATAL
;
438 extended_at
= ((uint8_t *) tmp_events
) +
439 nb_event
* sizeof(struct lttng_event
);
442 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
443 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
444 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
445 tmp_events
[i
].enabled
= event
->enabled
;
446 tmp_events
[i
].loglevel
= event
->loglevel_value
;
447 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
450 /* Append extended info */
451 append_extended_info(event
->filter_expression
, NULL
,
456 *events
= tmp_events
;
460 assert(nb_event
== i
);
465 * Create a list of ust global domain events.
467 static int list_lttng_ust_global_events(char *channel_name
,
468 struct ltt_ust_domain_global
*ust_global
,
469 struct lttng_event
**events
, size_t *total_size
)
472 unsigned int nb_event
= 0;
473 struct lttng_ht_iter iter
;
474 struct lttng_ht_node_str
*node
;
475 struct ltt_ust_channel
*uchan
;
476 struct ltt_ust_event
*uevent
;
477 struct lttng_event
*tmp
;
478 size_t extended_len
= 0;
481 DBG("Listing UST global events for channel %s", channel_name
);
485 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
486 node
= lttng_ht_iter_get_node_str(&iter
);
488 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
492 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
494 nb_event
= lttng_ht_get_count(uchan
->events
);
501 DBG3("Listing UST global %d events", nb_event
);
503 /* Compute required extended infos size */
504 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
505 if (uevent
->internal
) {
510 increment_extended_len(uevent
->filter_expression
,
511 uevent
->exclusion
, &extended_len
);
514 /* All events are internal, skip. */
520 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
521 tmp
= zmalloc(*total_size
);
523 ret
= -LTTNG_ERR_FATAL
;
527 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
529 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
530 if (uevent
->internal
) {
531 /* This event should remain hidden from clients */
534 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
535 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
536 tmp
[i
].enabled
= uevent
->enabled
;
538 switch (uevent
->attr
.instrumentation
) {
539 case LTTNG_UST_TRACEPOINT
:
540 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
542 case LTTNG_UST_PROBE
:
543 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
545 case LTTNG_UST_FUNCTION
:
546 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
550 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
551 switch (uevent
->attr
.loglevel_type
) {
552 case LTTNG_UST_LOGLEVEL_ALL
:
553 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
555 case LTTNG_UST_LOGLEVEL_RANGE
:
556 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
558 case LTTNG_UST_LOGLEVEL_SINGLE
:
559 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
562 if (uevent
->filter
) {
565 if (uevent
->exclusion
) {
566 tmp
[i
].exclusion
= 1;
570 /* Append extended info */
571 append_extended_info(uevent
->filter_expression
,
572 uevent
->exclusion
, &extended_at
);
583 * Fill lttng_event array of all kernel events in the channel.
585 static int list_lttng_kernel_events(char *channel_name
,
586 struct ltt_kernel_session
*kernel_session
,
587 struct lttng_event
**events
, size_t *total_size
)
590 unsigned int nb_event
;
591 struct ltt_kernel_event
*event
;
592 struct ltt_kernel_channel
*kchan
;
593 size_t extended_len
= 0;
596 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
598 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
602 nb_event
= kchan
->event_count
;
604 DBG("Listing events for channel %s", kchan
->channel
->name
);
612 /* Compute required extended infos size */
613 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
614 increment_extended_len(event
->filter_expression
, NULL
,
618 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
619 *events
= zmalloc(*total_size
);
620 if (*events
== NULL
) {
621 ret
= LTTNG_ERR_FATAL
;
625 extended_at
= ((void *) *events
) +
626 nb_event
* sizeof(struct lttng_event
);
628 /* Kernel channels */
629 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
630 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
631 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
632 (*events
)[i
].enabled
= event
->enabled
;
633 (*events
)[i
].filter
=
634 (unsigned char) !!event
->filter_expression
;
636 switch (event
->event
->instrumentation
) {
637 case LTTNG_KERNEL_TRACEPOINT
:
638 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
640 case LTTNG_KERNEL_KRETPROBE
:
641 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
642 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
643 sizeof(struct lttng_kernel_kprobe
));
645 case LTTNG_KERNEL_KPROBE
:
646 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
647 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
648 sizeof(struct lttng_kernel_kprobe
));
650 case LTTNG_KERNEL_FUNCTION
:
651 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
652 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
653 sizeof(struct lttng_kernel_function
));
655 case LTTNG_KERNEL_NOOP
:
656 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
658 case LTTNG_KERNEL_SYSCALL
:
659 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
661 case LTTNG_KERNEL_ALL
:
667 /* Append extended info */
668 append_extended_info(event
->filter_expression
, NULL
,
676 /* Negate the error code to differentiate the size from an error */
681 * Add URI so the consumer output object. Set the correct path depending on the
682 * domain adding the default trace directory.
684 static int add_uri_to_consumer(struct consumer_output
*consumer
,
685 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
686 const char *session_name
)
689 const char *default_trace_dir
;
693 if (consumer
== NULL
) {
694 DBG("No consumer detected. Don't add URI. Stopping.");
695 ret
= LTTNG_ERR_NO_CONSUMER
;
700 case LTTNG_DOMAIN_KERNEL
:
701 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
703 case LTTNG_DOMAIN_UST
:
704 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
708 * This case is possible is we try to add the URI to the global tracing
709 * session consumer object which in this case there is no subdir.
711 default_trace_dir
= "";
714 switch (uri
->dtype
) {
717 DBG2("Setting network URI to consumer");
719 if (consumer
->type
== CONSUMER_DST_NET
) {
720 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
721 consumer
->dst
.net
.control_isset
) ||
722 (uri
->stype
== LTTNG_STREAM_DATA
&&
723 consumer
->dst
.net
.data_isset
)) {
724 ret
= LTTNG_ERR_URL_EXIST
;
728 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
731 consumer
->type
= CONSUMER_DST_NET
;
733 /* Set URI into consumer output object */
734 ret
= consumer_set_network_uri(consumer
, uri
);
738 } else if (ret
== 1) {
740 * URI was the same in the consumer so we do not append the subdir
741 * again so to not duplicate output dir.
747 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
748 ret
= consumer_set_subdir(consumer
, session_name
);
750 ret
= LTTNG_ERR_FATAL
;
755 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
756 /* On a new subdir, reappend the default trace dir. */
757 strncat(consumer
->subdir
, default_trace_dir
,
758 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
759 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
764 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
765 memset(consumer
->dst
.trace_path
, 0,
766 sizeof(consumer
->dst
.trace_path
));
767 /* Explicit length checks for strcpy and strcat. */
768 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
769 >= sizeof(consumer
->dst
.trace_path
)) {
770 ret
= LTTNG_ERR_FATAL
;
773 strcpy(consumer
->dst
.trace_path
, uri
->dst
.path
);
774 /* Append default trace dir */
775 strcat(consumer
->dst
.trace_path
, default_trace_dir
);
776 /* Flag consumer as local. */
777 consumer
->type
= CONSUMER_DST_LOCAL
;
788 * Init tracing by creating trace directory and sending fds kernel consumer.
790 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
793 struct lttng_ht_iter iter
;
794 struct consumer_socket
*socket
;
800 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
801 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
803 pthread_mutex_lock(socket
->lock
);
804 ret
= kernel_consumer_send_session(socket
, session
);
805 pthread_mutex_unlock(socket
->lock
);
807 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
819 * Create a socket to the relayd using the URI.
821 * On success, the relayd_sock pointer is set to the created socket.
822 * Else, it's stays untouched and a lttcomm error code is returned.
824 static int create_connect_relayd(struct lttng_uri
*uri
,
825 struct lttcomm_relayd_sock
**relayd_sock
,
826 struct consumer_output
*consumer
)
829 struct lttcomm_relayd_sock
*rsock
;
831 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
832 RELAYD_VERSION_COMM_MINOR
);
834 ret
= LTTNG_ERR_FATAL
;
839 * Connect to relayd so we can proceed with a session creation. This call
840 * can possibly block for an arbitrary amount of time to set the health
841 * state to be in poll execution.
844 ret
= relayd_connect(rsock
);
847 ERR("Unable to reach lttng-relayd");
848 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
852 /* Create socket for control stream. */
853 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
854 DBG3("Creating relayd stream socket from URI");
856 /* Check relayd version */
857 ret
= relayd_version_check(rsock
);
859 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
862 consumer
->relay_major_version
= rsock
->major
;
863 consumer
->relay_minor_version
= rsock
->minor
;
864 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
865 DBG3("Creating relayd data socket from URI");
867 /* Command is not valid */
868 ERR("Relayd invalid stream type: %d", uri
->stype
);
869 ret
= LTTNG_ERR_INVALID
;
873 *relayd_sock
= rsock
;
878 /* The returned value is not useful since we are on an error path. */
879 (void) relayd_close(rsock
);
887 * Connect to the relayd using URI and send the socket to the right consumer.
889 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
890 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
891 struct consumer_output
*consumer
,
892 struct consumer_socket
*consumer_sock
,
893 char *session_name
, char *hostname
, int session_live_timer
)
896 struct lttcomm_relayd_sock
*rsock
= NULL
;
898 /* Connect to relayd and make version check if uri is the control. */
899 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
900 if (ret
!= LTTNG_OK
) {
905 /* Set the network sequence index if not set. */
906 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
907 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
909 * Increment net_seq_idx because we are about to transfer the
910 * new relayd socket to the consumer.
911 * Assign unique key so the consumer can match streams.
913 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
914 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
917 /* Send relayd socket to consumer. */
918 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
919 relayd_uri
->stype
, session_id
,
920 session_name
, hostname
, session_live_timer
);
922 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
926 /* Flag that the corresponding socket was sent. */
927 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
928 consumer_sock
->control_sock_sent
= 1;
929 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
930 consumer_sock
->data_sock_sent
= 1;
936 * Close socket which was dup on the consumer side. The session daemon does
937 * NOT keep track of the relayd socket(s) once transfer to the consumer.
941 (void) relayd_close(rsock
);
945 if (ret
!= LTTNG_OK
) {
947 * The consumer output for this session should not be used anymore
948 * since the relayd connection failed thus making any tracing or/and
949 * streaming not usable.
951 consumer
->enabled
= 0;
957 * Send both relayd sockets to a specific consumer and domain. This is a
958 * helper function to facilitate sending the information to the consumer for a
961 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
962 unsigned int session_id
, struct consumer_output
*consumer
,
963 struct consumer_socket
*sock
, char *session_name
,
964 char *hostname
, int session_live_timer
)
971 /* Sending control relayd socket. */
972 if (!sock
->control_sock_sent
) {
973 ret
= send_consumer_relayd_socket(domain
, session_id
,
974 &consumer
->dst
.net
.control
, consumer
, sock
,
975 session_name
, hostname
, session_live_timer
);
976 if (ret
!= LTTNG_OK
) {
981 /* Sending data relayd socket. */
982 if (!sock
->data_sock_sent
) {
983 ret
= send_consumer_relayd_socket(domain
, session_id
,
984 &consumer
->dst
.net
.data
, consumer
, sock
,
985 session_name
, hostname
, session_live_timer
);
986 if (ret
!= LTTNG_OK
) {
996 * Setup relayd connections for a tracing session. First creates the socket to
997 * the relayd and send them to the right domain consumer. Consumer type MUST be
1000 int cmd_setup_relayd(struct ltt_session
*session
)
1003 struct ltt_ust_session
*usess
;
1004 struct ltt_kernel_session
*ksess
;
1005 struct consumer_socket
*socket
;
1006 struct lttng_ht_iter iter
;
1010 usess
= session
->ust_session
;
1011 ksess
= session
->kernel_session
;
1013 DBG("Setting relayd for session %s", session
->name
);
1017 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1018 && usess
->consumer
->enabled
) {
1019 /* For each consumer socket, send relayd sockets */
1020 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1021 socket
, node
.node
) {
1022 pthread_mutex_lock(socket
->lock
);
1023 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1024 usess
->consumer
, socket
,
1025 session
->name
, session
->hostname
,
1026 session
->live_timer
);
1027 pthread_mutex_unlock(socket
->lock
);
1028 if (ret
!= LTTNG_OK
) {
1031 /* Session is now ready for network streaming. */
1032 session
->net_handle
= 1;
1034 session
->consumer
->relay_major_version
=
1035 usess
->consumer
->relay_major_version
;
1036 session
->consumer
->relay_minor_version
=
1037 usess
->consumer
->relay_minor_version
;
1040 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1041 && ksess
->consumer
->enabled
) {
1042 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1043 socket
, node
.node
) {
1044 pthread_mutex_lock(socket
->lock
);
1045 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1046 ksess
->consumer
, socket
,
1047 session
->name
, session
->hostname
,
1048 session
->live_timer
);
1049 pthread_mutex_unlock(socket
->lock
);
1050 if (ret
!= LTTNG_OK
) {
1053 /* Session is now ready for network streaming. */
1054 session
->net_handle
= 1;
1056 session
->consumer
->relay_major_version
=
1057 ksess
->consumer
->relay_major_version
;
1058 session
->consumer
->relay_minor_version
=
1059 ksess
->consumer
->relay_minor_version
;
1068 * Start a kernel session by opening all necessary streams.
1070 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1073 struct ltt_kernel_channel
*kchan
;
1075 /* Open kernel metadata */
1076 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1077 ret
= kernel_open_metadata(ksess
);
1079 ret
= LTTNG_ERR_KERN_META_FAIL
;
1084 /* Open kernel metadata stream */
1085 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1086 ret
= kernel_open_metadata_stream(ksess
);
1088 ERR("Kernel create metadata stream failed");
1089 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1094 /* For each channel */
1095 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1096 if (kchan
->stream_count
== 0) {
1097 ret
= kernel_open_channel_stream(kchan
);
1099 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1102 /* Update the stream global counter */
1103 ksess
->stream_count_global
+= ret
;
1107 /* Setup kernel consumer socket and send fds to it */
1108 ret
= init_kernel_tracing(ksess
);
1110 ret
= LTTNG_ERR_KERN_START_FAIL
;
1114 /* This start the kernel tracing */
1115 ret
= kernel_start_session(ksess
);
1117 ret
= LTTNG_ERR_KERN_START_FAIL
;
1121 /* Quiescent wait after starting trace */
1122 kernel_wait_quiescent(kernel_tracer_fd
);
1133 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1135 int cmd_disable_channel(struct ltt_session
*session
,
1136 enum lttng_domain_type domain
, char *channel_name
)
1139 struct ltt_ust_session
*usess
;
1141 usess
= session
->ust_session
;
1146 case LTTNG_DOMAIN_KERNEL
:
1148 ret
= channel_kernel_disable(session
->kernel_session
,
1150 if (ret
!= LTTNG_OK
) {
1154 kernel_wait_quiescent(kernel_tracer_fd
);
1157 case LTTNG_DOMAIN_UST
:
1159 struct ltt_ust_channel
*uchan
;
1160 struct lttng_ht
*chan_ht
;
1162 chan_ht
= usess
->domain_global
.channels
;
1164 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1165 if (uchan
== NULL
) {
1166 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1170 ret
= channel_ust_disable(usess
, uchan
);
1171 if (ret
!= LTTNG_OK
) {
1177 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1189 * Command LTTNG_TRACK_PID processed by the client thread.
1191 * Called with session lock held.
1193 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1201 case LTTNG_DOMAIN_KERNEL
:
1203 struct ltt_kernel_session
*ksess
;
1205 ksess
= session
->kernel_session
;
1207 ret
= kernel_track_pid(ksess
, pid
);
1208 if (ret
!= LTTNG_OK
) {
1212 kernel_wait_quiescent(kernel_tracer_fd
);
1215 case LTTNG_DOMAIN_UST
:
1217 struct ltt_ust_session
*usess
;
1219 usess
= session
->ust_session
;
1221 ret
= trace_ust_track_pid(usess
, pid
);
1222 if (ret
!= LTTNG_OK
) {
1228 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1240 * Command LTTNG_UNTRACK_PID processed by the client thread.
1242 * Called with session lock held.
1244 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1252 case LTTNG_DOMAIN_KERNEL
:
1254 struct ltt_kernel_session
*ksess
;
1256 ksess
= session
->kernel_session
;
1258 ret
= kernel_untrack_pid(ksess
, pid
);
1259 if (ret
!= LTTNG_OK
) {
1263 kernel_wait_quiescent(kernel_tracer_fd
);
1266 case LTTNG_DOMAIN_UST
:
1268 struct ltt_ust_session
*usess
;
1270 usess
= session
->ust_session
;
1272 ret
= trace_ust_untrack_pid(usess
, pid
);
1273 if (ret
!= LTTNG_OK
) {
1279 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1291 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1293 * The wpipe arguments is used as a notifier for the kernel thread.
1295 int cmd_enable_channel(struct ltt_session
*session
,
1296 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1299 struct ltt_ust_session
*usess
= session
->ust_session
;
1300 struct lttng_ht
*chan_ht
;
1307 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1309 /* Validate channel name */
1310 if (attr
->name
[0] == '.' ||
1311 memchr(attr
->name
, '/', len
) != NULL
) {
1312 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1316 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1321 * Don't try to enable a channel if the session has been started at
1322 * some point in time before. The tracer does not allow it.
1324 if (session
->has_been_started
) {
1325 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1330 * If the session is a live session, remove the switch timer, the
1331 * live timer does the same thing but sends also synchronisation
1332 * beacons for inactive streams.
1334 if (session
->live_timer
> 0) {
1335 attr
->attr
.live_timer_interval
= session
->live_timer
;
1336 attr
->attr
.switch_timer_interval
= 0;
1339 switch (domain
->type
) {
1340 case LTTNG_DOMAIN_KERNEL
:
1342 struct ltt_kernel_channel
*kchan
;
1344 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1345 session
->kernel_session
);
1346 if (kchan
== NULL
) {
1347 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1348 if (attr
->name
[0] != '\0') {
1349 session
->kernel_session
->has_non_default_channel
= 1;
1352 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1355 if (ret
!= LTTNG_OK
) {
1359 kernel_wait_quiescent(kernel_tracer_fd
);
1362 case LTTNG_DOMAIN_UST
:
1363 case LTTNG_DOMAIN_JUL
:
1364 case LTTNG_DOMAIN_LOG4J
:
1365 case LTTNG_DOMAIN_PYTHON
:
1367 struct ltt_ust_channel
*uchan
;
1372 * Current agent implementation limitations force us to allow
1373 * only one channel at once in "agent" subdomains. Each
1374 * subdomain has a default channel name which must be strictly
1377 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1378 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1379 LTTNG_SYMBOL_NAME_LEN
)) {
1380 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1383 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1384 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1385 LTTNG_SYMBOL_NAME_LEN
)) {
1386 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1389 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1390 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1391 LTTNG_SYMBOL_NAME_LEN
)) {
1392 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1397 chan_ht
= usess
->domain_global
.channels
;
1399 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1400 if (uchan
== NULL
) {
1401 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1402 if (attr
->name
[0] != '\0') {
1403 usess
->has_non_default_channel
= 1;
1406 ret
= channel_ust_enable(usess
, uchan
);
1411 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1422 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1424 int cmd_disable_event(struct ltt_session
*session
,
1425 enum lttng_domain_type domain
, char *channel_name
,
1426 struct lttng_event
*event
)
1431 DBG("Disable event command for event \'%s\'", event
->name
);
1433 event_name
= event
->name
;
1434 if (validate_event_name(event_name
)) {
1435 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1439 /* Error out on unhandled search criteria */
1440 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1441 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1442 ret
= LTTNG_ERR_UNK
;
1449 case LTTNG_DOMAIN_KERNEL
:
1451 struct ltt_kernel_channel
*kchan
;
1452 struct ltt_kernel_session
*ksess
;
1454 ksess
= session
->kernel_session
;
1457 * If a non-default channel has been created in the
1458 * session, explicitely require that -c chan_name needs
1461 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1462 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1466 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1467 if (kchan
== NULL
) {
1468 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1472 switch (event
->type
) {
1473 case LTTNG_EVENT_ALL
:
1474 case LTTNG_EVENT_TRACEPOINT
:
1475 case LTTNG_EVENT_SYSCALL
:
1476 case LTTNG_EVENT_PROBE
:
1477 case LTTNG_EVENT_FUNCTION
:
1478 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1479 if (event_name
[0] == '\0') {
1480 ret
= event_kernel_disable_event(kchan
,
1483 ret
= event_kernel_disable_event(kchan
,
1484 event_name
, event
->type
);
1486 if (ret
!= LTTNG_OK
) {
1491 ret
= LTTNG_ERR_UNK
;
1495 kernel_wait_quiescent(kernel_tracer_fd
);
1498 case LTTNG_DOMAIN_UST
:
1500 struct ltt_ust_channel
*uchan
;
1501 struct ltt_ust_session
*usess
;
1503 usess
= session
->ust_session
;
1505 if (validate_ust_event_name(event_name
)) {
1506 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1511 * If a non-default channel has been created in the
1512 * session, explicitly require that -c chan_name needs
1515 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1516 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1520 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1522 if (uchan
== NULL
) {
1523 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1527 switch (event
->type
) {
1528 case LTTNG_EVENT_ALL
:
1530 * An empty event name means that everything
1531 * should be disabled.
1533 if (event
->name
[0] == '\0') {
1534 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1536 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1539 if (ret
!= LTTNG_OK
) {
1544 ret
= LTTNG_ERR_UNK
;
1548 DBG3("Disable UST event %s in channel %s completed", event_name
,
1552 case LTTNG_DOMAIN_LOG4J
:
1553 case LTTNG_DOMAIN_JUL
:
1554 case LTTNG_DOMAIN_PYTHON
:
1557 struct ltt_ust_session
*usess
= session
->ust_session
;
1561 switch (event
->type
) {
1562 case LTTNG_EVENT_ALL
:
1565 ret
= LTTNG_ERR_UNK
;
1569 agt
= trace_ust_find_agent(usess
, domain
);
1571 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1575 * An empty event name means that everything
1576 * should be disabled.
1578 if (event
->name
[0] == '\0') {
1579 ret
= event_agent_disable_all(usess
, agt
);
1581 ret
= event_agent_disable(usess
, agt
, event_name
);
1583 if (ret
!= LTTNG_OK
) {
1590 ret
= LTTNG_ERR_UND
;
1603 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1605 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1606 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1608 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1609 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1611 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1612 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1613 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1617 case LTTNG_DOMAIN_KERNEL
:
1618 assert(session
->kernel_session
);
1620 if (session
->kernel_session
->channel_count
== 0) {
1621 /* Create default channel */
1622 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1623 if (ret
!= LTTNG_OK
) {
1626 chan_kern_created
= 1;
1628 /* Add kernel context to kernel tracer */
1629 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1630 if (ret
!= LTTNG_OK
) {
1634 case LTTNG_DOMAIN_JUL
:
1635 case LTTNG_DOMAIN_LOG4J
:
1638 * Validate channel name.
1639 * If no channel name is given and the domain is JUL or LOG4J,
1640 * set it to the appropriate domain-specific channel name. If
1641 * a name is provided but does not match the expexted channel
1642 * name, return an error.
1644 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1645 strcmp(channel_name
,
1646 DEFAULT_JUL_CHANNEL_NAME
)) {
1647 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1649 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1650 strcmp(channel_name
,
1651 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1652 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1655 /* break is _not_ missing here. */
1657 case LTTNG_DOMAIN_UST
:
1659 struct ltt_ust_session
*usess
= session
->ust_session
;
1660 unsigned int chan_count
;
1664 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1665 if (chan_count
== 0) {
1666 struct lttng_channel
*attr
;
1667 /* Create default channel */
1668 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1670 ret
= LTTNG_ERR_FATAL
;
1674 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1675 if (ret
!= LTTNG_OK
) {
1680 chan_ust_created
= 1;
1683 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1684 free(app_ctx_provider_name
);
1686 app_ctx_name
= NULL
;
1687 app_ctx_provider_name
= NULL
;
1688 if (ret
!= LTTNG_OK
) {
1694 ret
= LTTNG_ERR_UND
;
1702 if (chan_kern_created
) {
1703 struct ltt_kernel_channel
*kchan
=
1704 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1705 session
->kernel_session
);
1706 /* Created previously, this should NOT fail. */
1708 kernel_destroy_channel(kchan
);
1711 if (chan_ust_created
) {
1712 struct ltt_ust_channel
*uchan
=
1713 trace_ust_find_channel_by_name(
1714 session
->ust_session
->domain_global
.channels
,
1715 DEFAULT_CHANNEL_NAME
);
1716 /* Created previously, this should NOT fail. */
1718 /* Remove from the channel list of the session. */
1719 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1721 trace_ust_destroy_channel(uchan
);
1724 free(app_ctx_provider_name
);
1729 static int validate_event_name(const char *name
)
1732 const char *c
= name
;
1733 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1734 bool null_terminated
= false;
1737 * Make sure that unescaped wildcards are only used as the last
1738 * character of the event name.
1740 while (c
< event_name_end
) {
1743 null_terminated
= true;
1749 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1750 /* Wildcard is not the last character */
1751 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1760 if (!ret
&& !null_terminated
) {
1761 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1766 static inline bool name_starts_with(const char *name
, const char *prefix
)
1768 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1770 return !strncmp(name
, prefix
, max_cmp_len
);
1773 /* Perform userspace-specific event name validation */
1774 static int validate_ust_event_name(const char *name
)
1784 * Check name against all internal UST event component namespaces used
1787 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1788 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1789 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1798 * Internal version of cmd_enable_event() with a supplemental
1799 * "internal_event" flag which is used to enable internal events which should
1800 * be hidden from clients. Such events are used in the agent implementation to
1801 * enable the events through which all "agent" events are funeled.
1803 static int _cmd_enable_event(struct ltt_session
*session
,
1804 struct lttng_domain
*domain
,
1805 char *channel_name
, struct lttng_event
*event
,
1806 char *filter_expression
,
1807 struct lttng_filter_bytecode
*filter
,
1808 struct lttng_event_exclusion
*exclusion
,
1809 int wpipe
, bool internal_event
)
1811 int ret
, channel_created
= 0;
1812 struct lttng_channel
*attr
= NULL
;
1816 assert(channel_name
);
1818 /* If we have a filter, we must have its filter expression */
1819 assert(!(!!filter_expression
^ !!filter
));
1821 DBG("Enable event command for event \'%s\'", event
->name
);
1825 ret
= validate_event_name(event
->name
);
1830 switch (domain
->type
) {
1831 case LTTNG_DOMAIN_KERNEL
:
1833 struct ltt_kernel_channel
*kchan
;
1836 * If a non-default channel has been created in the
1837 * session, explicitely require that -c chan_name needs
1840 if (session
->kernel_session
->has_non_default_channel
1841 && channel_name
[0] == '\0') {
1842 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1846 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1847 session
->kernel_session
);
1848 if (kchan
== NULL
) {
1849 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1850 LTTNG_BUFFER_GLOBAL
);
1852 ret
= LTTNG_ERR_FATAL
;
1855 if (lttng_strncpy(attr
->name
, channel_name
,
1856 sizeof(attr
->name
))) {
1857 ret
= LTTNG_ERR_INVALID
;
1861 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1862 if (ret
!= LTTNG_OK
) {
1865 channel_created
= 1;
1868 /* Get the newly created kernel channel pointer */
1869 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1870 session
->kernel_session
);
1871 if (kchan
== NULL
) {
1872 /* This sould not happen... */
1873 ret
= LTTNG_ERR_FATAL
;
1877 switch (event
->type
) {
1878 case LTTNG_EVENT_ALL
:
1880 char *filter_expression_a
= NULL
;
1881 struct lttng_filter_bytecode
*filter_a
= NULL
;
1884 * We need to duplicate filter_expression and filter,
1885 * because ownership is passed to first enable
1888 if (filter_expression
) {
1889 filter_expression_a
= strdup(filter_expression
);
1890 if (!filter_expression_a
) {
1891 ret
= LTTNG_ERR_FATAL
;
1896 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1898 free(filter_expression_a
);
1899 ret
= LTTNG_ERR_FATAL
;
1902 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1904 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1905 ret
= event_kernel_enable_event(kchan
, event
,
1906 filter_expression
, filter
);
1907 /* We have passed ownership */
1908 filter_expression
= NULL
;
1910 if (ret
!= LTTNG_OK
) {
1911 if (channel_created
) {
1912 /* Let's not leak a useless channel. */
1913 kernel_destroy_channel(kchan
);
1915 free(filter_expression_a
);
1919 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1920 ret
= event_kernel_enable_event(kchan
, event
,
1921 filter_expression_a
, filter_a
);
1922 /* We have passed ownership */
1923 filter_expression_a
= NULL
;
1925 if (ret
!= LTTNG_OK
) {
1930 case LTTNG_EVENT_PROBE
:
1931 case LTTNG_EVENT_FUNCTION
:
1932 case LTTNG_EVENT_FUNCTION_ENTRY
:
1933 case LTTNG_EVENT_TRACEPOINT
:
1934 ret
= event_kernel_enable_event(kchan
, event
,
1935 filter_expression
, filter
);
1936 /* We have passed ownership */
1937 filter_expression
= NULL
;
1939 if (ret
!= LTTNG_OK
) {
1940 if (channel_created
) {
1941 /* Let's not leak a useless channel. */
1942 kernel_destroy_channel(kchan
);
1947 case LTTNG_EVENT_SYSCALL
:
1948 ret
= event_kernel_enable_event(kchan
, event
,
1949 filter_expression
, filter
);
1950 /* We have passed ownership */
1951 filter_expression
= NULL
;
1953 if (ret
!= LTTNG_OK
) {
1958 ret
= LTTNG_ERR_UNK
;
1962 kernel_wait_quiescent(kernel_tracer_fd
);
1965 case LTTNG_DOMAIN_UST
:
1967 struct ltt_ust_channel
*uchan
;
1968 struct ltt_ust_session
*usess
= session
->ust_session
;
1973 * If a non-default channel has been created in the
1974 * session, explicitely require that -c chan_name needs
1977 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1978 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1982 /* Get channel from global UST domain */
1983 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1985 if (uchan
== NULL
) {
1986 /* Create default channel */
1987 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1988 usess
->buffer_type
);
1990 ret
= LTTNG_ERR_FATAL
;
1993 if (lttng_strncpy(attr
->name
, channel_name
,
1994 sizeof(attr
->name
))) {
1995 ret
= LTTNG_ERR_INVALID
;
1999 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
2000 if (ret
!= LTTNG_OK
) {
2004 /* Get the newly created channel reference back */
2005 uchan
= trace_ust_find_channel_by_name(
2006 usess
->domain_global
.channels
, channel_name
);
2010 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2012 * Don't allow users to add UST events to channels which
2013 * are assigned to a userspace subdomain (JUL, Log4J,
2016 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2020 if (!internal_event
) {
2022 * Ensure the event name is not reserved for internal
2025 ret
= validate_ust_event_name(event
->name
);
2027 WARN("Userspace event name %s failed validation.",
2029 event
->name
: "NULL");
2030 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2035 /* At this point, the session and channel exist on the tracer */
2036 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2037 filter_expression
, filter
, exclusion
,
2039 /* We have passed ownership */
2040 filter_expression
= NULL
;
2043 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2044 goto already_enabled
;
2045 } else if (ret
!= LTTNG_OK
) {
2050 case LTTNG_DOMAIN_LOG4J
:
2051 case LTTNG_DOMAIN_JUL
:
2052 case LTTNG_DOMAIN_PYTHON
:
2054 const char *default_event_name
, *default_chan_name
;
2056 struct lttng_event uevent
;
2057 struct lttng_domain tmp_dom
;
2058 struct ltt_ust_session
*usess
= session
->ust_session
;
2062 agt
= trace_ust_find_agent(usess
, domain
->type
);
2064 agt
= agent_create(domain
->type
);
2066 ret
= LTTNG_ERR_NOMEM
;
2069 agent_add(agt
, usess
->agents
);
2072 /* Create the default tracepoint. */
2073 memset(&uevent
, 0, sizeof(uevent
));
2074 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2075 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2076 default_event_name
= event_get_default_agent_ust_name(
2078 if (!default_event_name
) {
2079 ret
= LTTNG_ERR_FATAL
;
2082 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2083 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2086 * The domain type is changed because we are about to enable the
2087 * default channel and event for the JUL domain that are hardcoded.
2088 * This happens in the UST domain.
2090 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2091 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2093 switch (domain
->type
) {
2094 case LTTNG_DOMAIN_LOG4J
:
2095 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2097 case LTTNG_DOMAIN_JUL
:
2098 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2100 case LTTNG_DOMAIN_PYTHON
:
2101 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2104 /* The switch/case we are in makes this impossible */
2109 char *filter_expression_copy
= NULL
;
2110 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2113 const size_t filter_size
= sizeof(
2114 struct lttng_filter_bytecode
)
2117 filter_copy
= zmalloc(filter_size
);
2119 ret
= LTTNG_ERR_NOMEM
;
2122 memcpy(filter_copy
, filter
, filter_size
);
2124 filter_expression_copy
=
2125 strdup(filter_expression
);
2126 if (!filter_expression
) {
2127 ret
= LTTNG_ERR_NOMEM
;
2130 if (!filter_expression_copy
|| !filter_copy
) {
2131 free(filter_expression_copy
);
2137 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2138 (char *) default_chan_name
,
2139 &uevent
, filter_expression_copy
,
2140 filter_copy
, NULL
, wpipe
);
2143 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2144 goto already_enabled
;
2145 } else if (ret
!= LTTNG_OK
) {
2149 /* The wild card * means that everything should be enabled. */
2150 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2151 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2154 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2158 filter_expression
= NULL
;
2159 if (ret
!= LTTNG_OK
) {
2166 ret
= LTTNG_ERR_UND
;
2174 free(filter_expression
);
2183 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2184 * We own filter, exclusion, and filter_expression.
2186 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2187 char *channel_name
, struct lttng_event
*event
,
2188 char *filter_expression
,
2189 struct lttng_filter_bytecode
*filter
,
2190 struct lttng_event_exclusion
*exclusion
,
2193 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2194 filter_expression
, filter
, exclusion
, wpipe
, false);
2198 * Enable an event which is internal to LTTng. An internal should
2199 * never be made visible to clients and are immune to checks such as
2202 static int cmd_enable_event_internal(struct ltt_session
*session
,
2203 struct lttng_domain
*domain
,
2204 char *channel_name
, struct lttng_event
*event
,
2205 char *filter_expression
,
2206 struct lttng_filter_bytecode
*filter
,
2207 struct lttng_event_exclusion
*exclusion
,
2210 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2211 filter_expression
, filter
, exclusion
, wpipe
, true);
2215 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2217 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2218 struct lttng_event
**events
)
2221 ssize_t nb_events
= 0;
2224 case LTTNG_DOMAIN_KERNEL
:
2225 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2226 if (nb_events
< 0) {
2227 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2231 case LTTNG_DOMAIN_UST
:
2232 nb_events
= ust_app_list_events(events
);
2233 if (nb_events
< 0) {
2234 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2238 case LTTNG_DOMAIN_LOG4J
:
2239 case LTTNG_DOMAIN_JUL
:
2240 case LTTNG_DOMAIN_PYTHON
:
2241 nb_events
= agent_list_events(events
, domain
);
2242 if (nb_events
< 0) {
2243 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2248 ret
= LTTNG_ERR_UND
;
2255 /* Return negative value to differentiate return code */
2260 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2262 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2263 struct lttng_event_field
**fields
)
2266 ssize_t nb_fields
= 0;
2269 case LTTNG_DOMAIN_UST
:
2270 nb_fields
= ust_app_list_event_fields(fields
);
2271 if (nb_fields
< 0) {
2272 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2276 case LTTNG_DOMAIN_KERNEL
:
2277 default: /* fall-through */
2278 ret
= LTTNG_ERR_UND
;
2285 /* Return negative value to differentiate return code */
2289 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2291 return syscall_table_list(events
);
2295 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2297 * Called with session lock held.
2299 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2300 enum lttng_domain_type domain
, int32_t **pids
)
2303 ssize_t nr_pids
= 0;
2306 case LTTNG_DOMAIN_KERNEL
:
2308 struct ltt_kernel_session
*ksess
;
2310 ksess
= session
->kernel_session
;
2311 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2313 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2318 case LTTNG_DOMAIN_UST
:
2320 struct ltt_ust_session
*usess
;
2322 usess
= session
->ust_session
;
2323 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2325 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2330 case LTTNG_DOMAIN_LOG4J
:
2331 case LTTNG_DOMAIN_JUL
:
2332 case LTTNG_DOMAIN_PYTHON
:
2334 ret
= LTTNG_ERR_UND
;
2341 /* Return negative value to differentiate return code */
2346 * Command LTTNG_START_TRACE processed by the client thread.
2348 * Called with session mutex held.
2350 int cmd_start_trace(struct ltt_session
*session
)
2353 unsigned long nb_chan
= 0;
2354 struct ltt_kernel_session
*ksession
;
2355 struct ltt_ust_session
*usess
;
2359 /* Ease our life a bit ;) */
2360 ksession
= session
->kernel_session
;
2361 usess
= session
->ust_session
;
2363 /* Is the session already started? */
2364 if (session
->active
) {
2365 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2370 * Starting a session without channel is useless since after that it's not
2371 * possible to enable channel thus inform the client.
2373 if (usess
&& usess
->domain_global
.channels
) {
2374 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2377 nb_chan
+= ksession
->channel_count
;
2380 ret
= LTTNG_ERR_NO_CHANNEL
;
2384 /* Kernel tracing */
2385 if (ksession
!= NULL
) {
2386 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2387 if (ret
!= LTTNG_OK
) {
2392 /* Flag session that trace should start automatically */
2395 * Even though the start trace might fail, flag this session active so
2396 * other application coming in are started by default.
2400 ret
= ust_app_start_trace_all(usess
);
2402 ret
= LTTNG_ERR_UST_START_FAIL
;
2407 /* Flag this after a successful start. */
2408 session
->has_been_started
= 1;
2409 session
->active
= 1;
2418 * Command LTTNG_STOP_TRACE processed by the client thread.
2420 int cmd_stop_trace(struct ltt_session
*session
)
2423 struct ltt_kernel_channel
*kchan
;
2424 struct ltt_kernel_session
*ksession
;
2425 struct ltt_ust_session
*usess
;
2430 ksession
= session
->kernel_session
;
2431 usess
= session
->ust_session
;
2433 /* Session is not active. Skip everythong and inform the client. */
2434 if (!session
->active
) {
2435 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2440 if (ksession
&& ksession
->active
) {
2441 DBG("Stop kernel tracing");
2443 ret
= kernel_stop_session(ksession
);
2445 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2449 kernel_wait_quiescent(kernel_tracer_fd
);
2451 /* Flush metadata after stopping (if exists) */
2452 if (ksession
->metadata_stream_fd
>= 0) {
2453 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2455 ERR("Kernel metadata flush failed");
2459 /* Flush all buffers after stopping */
2460 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2461 ret
= kernel_flush_buffer(kchan
);
2463 ERR("Kernel flush buffer error");
2467 ksession
->active
= 0;
2470 if (usess
&& usess
->active
) {
2472 * Even though the stop trace might fail, flag this session inactive so
2473 * other application coming in are not started by default.
2477 ret
= ust_app_stop_trace_all(usess
);
2479 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2484 /* Flag inactive after a successful stop. */
2485 session
->active
= 0;
2493 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2495 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2496 struct lttng_uri
*uris
)
2499 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2500 struct ltt_ust_session
*usess
= session
->ust_session
;
2506 /* Can't set consumer URI if the session is active. */
2507 if (session
->active
) {
2508 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2512 /* Set the "global" consumer URIs */
2513 for (i
= 0; i
< nb_uri
; i
++) {
2514 ret
= add_uri_to_consumer(session
->consumer
,
2515 &uris
[i
], 0, session
->name
);
2516 if (ret
!= LTTNG_OK
) {
2521 /* Set UST session URIs */
2522 if (session
->ust_session
) {
2523 for (i
= 0; i
< nb_uri
; i
++) {
2524 ret
= add_uri_to_consumer(
2525 session
->ust_session
->consumer
,
2526 &uris
[i
], LTTNG_DOMAIN_UST
,
2528 if (ret
!= LTTNG_OK
) {
2534 /* Set kernel session URIs */
2535 if (session
->kernel_session
) {
2536 for (i
= 0; i
< nb_uri
; i
++) {
2537 ret
= add_uri_to_consumer(
2538 session
->kernel_session
->consumer
,
2539 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2541 if (ret
!= LTTNG_OK
) {
2548 * Make sure to set the session in output mode after we set URI since a
2549 * session can be created without URL (thus flagged in no output mode).
2551 session
->output_traces
= 1;
2553 ksess
->output_traces
= 1;
2557 usess
->output_traces
= 1;
2568 * Command LTTNG_CREATE_SESSION processed by the client thread.
2570 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2571 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2574 struct ltt_session
*session
;
2580 * Verify if the session already exist
2582 * XXX: There is no need for the session lock list here since the caller
2583 * (process_client_msg) is holding it. We might want to change that so a
2584 * single command does not lock the entire session list.
2586 session
= session_find_by_name(name
);
2587 if (session
!= NULL
) {
2588 ret
= LTTNG_ERR_EXIST_SESS
;
2592 /* Create tracing session in the registry */
2593 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2594 LTTNG_SOCK_GET_GID_CRED(creds
));
2595 if (ret
!= LTTNG_OK
) {
2600 * Get the newly created session pointer back
2602 * XXX: There is no need for the session lock list here since the caller
2603 * (process_client_msg) is holding it. We might want to change that so a
2604 * single command does not lock the entire session list.
2606 session
= session_find_by_name(name
);
2609 session
->live_timer
= live_timer
;
2610 /* Create default consumer output for the session not yet created. */
2611 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2612 if (session
->consumer
== NULL
) {
2613 ret
= LTTNG_ERR_FATAL
;
2614 goto consumer_error
;
2618 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2619 if (ret
!= LTTNG_OK
) {
2620 goto consumer_error
;
2622 session
->output_traces
= 1;
2624 session
->output_traces
= 0;
2625 DBG2("Session %s created with no output", session
->name
);
2628 session
->consumer
->enabled
= 1;
2633 session_destroy(session
);
2640 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2642 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2643 size_t nb_uri
, lttng_sock_cred
*creds
)
2646 struct ltt_session
*session
;
2647 struct snapshot_output
*new_output
= NULL
;
2653 * Create session in no output mode with URIs set to NULL. The uris we've
2654 * received are for a default snapshot output if one.
2656 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2657 if (ret
!= LTTNG_OK
) {
2661 /* Get the newly created session pointer back. This should NEVER fail. */
2662 session
= session_find_by_name(name
);
2665 /* Flag session for snapshot mode. */
2666 session
->snapshot_mode
= 1;
2668 /* Skip snapshot output creation if no URI is given. */
2673 new_output
= snapshot_output_alloc();
2675 ret
= LTTNG_ERR_NOMEM
;
2676 goto error_snapshot_alloc
;
2679 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2680 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2682 if (ret
== -ENOMEM
) {
2683 ret
= LTTNG_ERR_NOMEM
;
2685 ret
= LTTNG_ERR_INVALID
;
2687 goto error_snapshot
;
2691 snapshot_add_output(&session
->snapshot
, new_output
);
2698 snapshot_output_destroy(new_output
);
2699 error_snapshot_alloc
:
2700 session_destroy(session
);
2706 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2708 * Called with session lock held.
2710 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2713 struct ltt_ust_session
*usess
;
2714 struct ltt_kernel_session
*ksess
;
2719 usess
= session
->ust_session
;
2720 ksess
= session
->kernel_session
;
2722 /* Clean kernel session teardown */
2723 kernel_destroy_session(ksess
);
2725 /* UST session teardown */
2727 /* Close any relayd session */
2728 consumer_output_send_destroy_relayd(usess
->consumer
);
2730 /* Destroy every UST application related to this session. */
2731 ret
= ust_app_destroy_trace_all(usess
);
2733 ERR("Error in ust_app_destroy_trace_all");
2736 /* Clean up the rest. */
2737 trace_ust_destroy_session(usess
);
2741 * Must notify the kernel thread here to update it's poll set in order to
2742 * remove the channel(s)' fd just destroyed.
2744 ret
= notify_thread_pipe(wpipe
);
2746 PERROR("write kernel poll pipe");
2749 ret
= session_destroy(session
);
2755 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2757 int cmd_register_consumer(struct ltt_session
*session
,
2758 enum lttng_domain_type domain
, const char *sock_path
,
2759 struct consumer_data
*cdata
)
2762 struct consumer_socket
*socket
= NULL
;
2769 case LTTNG_DOMAIN_KERNEL
:
2771 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2775 /* Can't register a consumer if there is already one */
2776 if (ksess
->consumer_fds_sent
!= 0) {
2777 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2781 sock
= lttcomm_connect_unix_sock(sock_path
);
2783 ret
= LTTNG_ERR_CONNECT_FAIL
;
2786 cdata
->cmd_sock
= sock
;
2788 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2789 if (socket
== NULL
) {
2792 PERROR("close register consumer");
2794 cdata
->cmd_sock
= -1;
2795 ret
= LTTNG_ERR_FATAL
;
2799 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2800 if (socket
->lock
== NULL
) {
2801 PERROR("zmalloc pthread mutex");
2802 ret
= LTTNG_ERR_FATAL
;
2805 pthread_mutex_init(socket
->lock
, NULL
);
2806 socket
->registered
= 1;
2809 consumer_add_socket(socket
, ksess
->consumer
);
2812 pthread_mutex_lock(&cdata
->pid_mutex
);
2814 pthread_mutex_unlock(&cdata
->pid_mutex
);
2819 /* TODO: Userspace tracing */
2820 ret
= LTTNG_ERR_UND
;
2828 consumer_destroy_socket(socket
);
2834 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2836 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2837 struct lttng_domain
**domains
)
2842 struct lttng_ht_iter iter
;
2844 if (session
->kernel_session
!= NULL
) {
2845 DBG3("Listing domains found kernel domain");
2849 if (session
->ust_session
!= NULL
) {
2850 DBG3("Listing domains found UST global domain");
2854 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2856 if (agt
->being_used
) {
2867 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2868 if (*domains
== NULL
) {
2869 ret
= LTTNG_ERR_FATAL
;
2873 if (session
->kernel_session
!= NULL
) {
2874 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2876 /* Kernel session buffer type is always GLOBAL */
2877 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2882 if (session
->ust_session
!= NULL
) {
2883 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2884 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2888 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2890 if (agt
->being_used
) {
2891 (*domains
)[index
].type
= agt
->domain
;
2892 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2902 /* Return negative value to differentiate return code */
2908 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2910 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2911 struct ltt_session
*session
, struct lttng_channel
**channels
)
2913 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
2916 case LTTNG_DOMAIN_KERNEL
:
2917 if (session
->kernel_session
!= NULL
) {
2918 nb_chan
= session
->kernel_session
->channel_count
;
2920 DBG3("Number of kernel channels %zd", nb_chan
);
2922 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2926 case LTTNG_DOMAIN_UST
:
2927 if (session
->ust_session
!= NULL
) {
2929 nb_chan
= lttng_ht_get_count(
2930 session
->ust_session
->domain_global
.channels
);
2933 DBG3("Number of UST global channels %zd", nb_chan
);
2935 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2940 ret
= -LTTNG_ERR_UND
;
2945 const size_t channel_size
= sizeof(struct lttng_channel
) +
2946 sizeof(struct lttcomm_channel_extended
);
2947 struct lttcomm_channel_extended
*channel_exts
;
2949 payload_size
= nb_chan
* channel_size
;
2950 *channels
= zmalloc(payload_size
);
2951 if (*channels
== NULL
) {
2952 ret
= -LTTNG_ERR_FATAL
;
2956 channel_exts
= ((void *) *channels
) +
2957 (nb_chan
* sizeof(struct lttng_channel
));
2958 list_lttng_channels(domain
, session
, *channels
, channel_exts
);
2969 * Command LTTNG_LIST_EVENTS processed by the client thread.
2971 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
2972 struct ltt_session
*session
, char *channel_name
,
2973 struct lttng_event
**events
, size_t *total_size
)
2976 ssize_t nb_event
= 0;
2979 case LTTNG_DOMAIN_KERNEL
:
2980 if (session
->kernel_session
!= NULL
) {
2981 nb_event
= list_lttng_kernel_events(channel_name
,
2982 session
->kernel_session
, events
,
2986 case LTTNG_DOMAIN_UST
:
2988 if (session
->ust_session
!= NULL
) {
2989 nb_event
= list_lttng_ust_global_events(channel_name
,
2990 &session
->ust_session
->domain_global
, events
,
2995 case LTTNG_DOMAIN_LOG4J
:
2996 case LTTNG_DOMAIN_JUL
:
2997 case LTTNG_DOMAIN_PYTHON
:
2998 if (session
->ust_session
) {
2999 struct lttng_ht_iter iter
;
3003 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3004 &iter
.iter
, agt
, node
.node
) {
3005 if (agt
->domain
== domain
) {
3006 nb_event
= list_lttng_agent_events(
3016 ret
= LTTNG_ERR_UND
;
3023 /* Return negative value to differentiate return code */
3028 * Using the session list, filled a lttng_session array to send back to the
3029 * client for session listing.
3031 * The session list lock MUST be acquired before calling this function. Use
3032 * session_lock_list() and session_unlock_list().
3034 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3039 struct ltt_session
*session
;
3040 struct ltt_session_list
*list
= session_get_list();
3042 DBG("Getting all available session for UID %d GID %d",
3045 * Iterate over session list and append data after the control struct in
3048 cds_list_for_each_entry(session
, &list
->head
, list
) {
3050 * Only list the sessions the user can control.
3052 if (!session_access_ok(session
, uid
, gid
)) {
3056 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3057 struct ltt_ust_session
*usess
= session
->ust_session
;
3059 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3060 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3061 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3062 ret
= build_network_session_path(sessions
[i
].path
,
3063 sizeof(sessions
[i
].path
), session
);
3065 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3066 session
->consumer
->dst
.trace_path
);
3069 PERROR("snprintf session path");
3073 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3074 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3075 sessions
[i
].enabled
= session
->active
;
3076 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3077 sessions
[i
].live_timer_interval
= session
->live_timer
;
3083 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3084 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3086 int cmd_data_pending(struct ltt_session
*session
)
3089 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3090 struct ltt_ust_session
*usess
= session
->ust_session
;
3094 /* Session MUST be stopped to ask for data availability. */
3095 if (session
->active
) {
3096 ret
= LTTNG_ERR_SESSION_STARTED
;
3100 * If stopped, just make sure we've started before else the above call
3101 * will always send that there is data pending.
3103 * The consumer assumes that when the data pending command is received,
3104 * the trace has been started before or else no output data is written
3105 * by the streams which is a condition for data pending. So, this is
3106 * *VERY* important that we don't ask the consumer before a start
3109 if (!session
->has_been_started
) {
3115 if (ksess
&& ksess
->consumer
) {
3116 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3118 /* Data is still being extracted for the kernel. */
3123 if (usess
&& usess
->consumer
) {
3124 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3126 /* Data is still being extracted for the kernel. */
3131 /* Data is ready to be read by a viewer */
3139 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3141 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3143 int cmd_snapshot_add_output(struct ltt_session
*session
,
3144 struct lttng_snapshot_output
*output
, uint32_t *id
)
3147 struct snapshot_output
*new_output
;
3152 DBG("Cmd snapshot add output for session %s", session
->name
);
3155 * Can't create an output if the session is not set in no-output mode.
3157 if (session
->output_traces
) {
3158 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3162 /* Only one output is allowed until we have the "tee" feature. */
3163 if (session
->snapshot
.nb_output
== 1) {
3164 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3168 new_output
= snapshot_output_alloc();
3170 ret
= LTTNG_ERR_NOMEM
;
3174 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3175 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3176 &session
->snapshot
);
3178 if (ret
== -ENOMEM
) {
3179 ret
= LTTNG_ERR_NOMEM
;
3181 ret
= LTTNG_ERR_INVALID
;
3187 snapshot_add_output(&session
->snapshot
, new_output
);
3189 *id
= new_output
->id
;
3196 snapshot_output_destroy(new_output
);
3202 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3204 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3206 int cmd_snapshot_del_output(struct ltt_session
*session
,
3207 struct lttng_snapshot_output
*output
)
3210 struct snapshot_output
*sout
= NULL
;
3218 * Permission denied to create an output if the session is not
3219 * set in no output mode.
3221 if (session
->output_traces
) {
3222 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3227 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3229 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3230 } else if (*output
->name
!= '\0') {
3231 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3233 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3236 ret
= LTTNG_ERR_INVALID
;
3240 snapshot_delete_output(&session
->snapshot
, sout
);
3241 snapshot_output_destroy(sout
);
3250 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3252 * If no output is available, outputs is untouched and 0 is returned.
3254 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3256 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3257 struct lttng_snapshot_output
**outputs
)
3260 struct lttng_snapshot_output
*list
= NULL
;
3261 struct lttng_ht_iter iter
;
3262 struct snapshot_output
*output
;
3267 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3270 * Permission denied to create an output if the session is not
3271 * set in no output mode.
3273 if (session
->output_traces
) {
3274 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3278 if (session
->snapshot
.nb_output
== 0) {
3283 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3285 ret
= -LTTNG_ERR_NOMEM
;
3289 /* Copy list from session to the new list object. */
3291 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3292 output
, node
.node
) {
3293 assert(output
->consumer
);
3294 list
[idx
].id
= output
->id
;
3295 list
[idx
].max_size
= output
->max_size
;
3296 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3297 sizeof(list
[idx
].name
))) {
3298 ret
= -LTTNG_ERR_INVALID
;
3301 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3302 if (lttng_strncpy(list
[idx
].ctrl_url
,
3303 output
->consumer
->dst
.trace_path
,
3304 sizeof(list
[idx
].ctrl_url
))) {
3305 ret
= -LTTNG_ERR_INVALID
;
3310 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3311 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3313 ret
= -LTTNG_ERR_NOMEM
;
3318 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3319 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3321 ret
= -LTTNG_ERR_NOMEM
;
3330 ret
= session
->snapshot
.nb_output
;
3339 * Check if we can regenerate the metadata for this session.
3340 * Only kernel, UST per-uid and non-live sessions are supported.
3342 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3345 int check_regenerate_metadata_support(struct ltt_session
*session
)
3351 if (session
->live_timer
!= 0) {
3352 ret
= LTTNG_ERR_LIVE_SESSION
;
3355 if (!session
->active
) {
3356 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3359 if (session
->ust_session
) {
3360 switch (session
->ust_session
->buffer_type
) {
3361 case LTTNG_BUFFER_PER_UID
:
3363 case LTTNG_BUFFER_PER_PID
:
3364 ret
= LTTNG_ERR_PER_PID_SESSION
;
3368 ret
= LTTNG_ERR_UNK
;
3372 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3373 session
->consumer
->relay_minor_version
< 8) {
3374 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3384 int clear_metadata_file(int fd
)
3388 ret
= lseek(fd
, 0, SEEK_SET
);
3394 ret
= ftruncate(fd
, 0);
3396 PERROR("ftruncate");
3405 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3408 struct buffer_reg_uid
*uid_reg
= NULL
;
3409 struct buffer_reg_session
*session_reg
= NULL
;
3412 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3413 struct ust_registry_session
*registry
;
3414 struct ust_registry_channel
*chan
;
3415 struct lttng_ht_iter iter_chan
;
3417 session_reg
= uid_reg
->registry
;
3418 registry
= session_reg
->reg
.ust
;
3420 pthread_mutex_lock(®istry
->lock
);
3421 registry
->metadata_len_sent
= 0;
3422 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3423 registry
->metadata_len
= 0;
3424 registry
->metadata_version
++;
3425 if (registry
->metadata_fd
> 0) {
3426 /* Clear the metadata file's content. */
3427 ret
= clear_metadata_file(registry
->metadata_fd
);
3429 pthread_mutex_unlock(®istry
->lock
);
3434 ret
= ust_metadata_session_statedump(registry
, NULL
,
3435 registry
->major
, registry
->minor
);
3437 pthread_mutex_unlock(®istry
->lock
);
3438 ERR("Failed to generate session metadata (err = %d)",
3442 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3444 struct ust_registry_event
*event
;
3445 struct lttng_ht_iter iter_event
;
3447 ret
= ust_metadata_channel_statedump(registry
, chan
);
3449 pthread_mutex_unlock(®istry
->lock
);
3450 ERR("Failed to generate channel metadata "
3454 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3456 ret
= ust_metadata_event_statedump(registry
,
3459 pthread_mutex_unlock(®istry
->lock
);
3460 ERR("Failed to generate event metadata "
3466 pthread_mutex_unlock(®istry
->lock
);
3475 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3477 * Ask the consumer to truncate the existing metadata file(s) and
3478 * then regenerate the metadata. Live and per-pid sessions are not
3479 * supported and return an error.
3481 * Return 0 on success or else a LTTNG_ERR code.
3483 int cmd_regenerate_metadata(struct ltt_session
*session
)
3489 ret
= check_regenerate_metadata_support(session
);
3494 if (session
->kernel_session
) {
3495 ret
= kernctl_session_regenerate_metadata(
3496 session
->kernel_session
->fd
);
3498 ERR("Failed to regenerate the kernel metadata");
3503 if (session
->ust_session
) {
3504 ret
= ust_regenerate_metadata(session
->ust_session
);
3506 ERR("Failed to regenerate the UST metadata");
3510 DBG("Cmd metadata regenerate for session %s", session
->name
);
3518 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3520 * Ask the tracer to regenerate a new statedump.
3522 * Return 0 on success or else a LTTNG_ERR code.
3524 int cmd_regenerate_statedump(struct ltt_session
*session
)
3530 if (!session
->active
) {
3531 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3536 if (session
->kernel_session
) {
3537 ret
= kernctl_session_regenerate_statedump(
3538 session
->kernel_session
->fd
);
3540 * Currently, the statedump in kernel can only fail if out
3544 if (ret
== -ENOMEM
) {
3545 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3547 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3549 ERR("Failed to regenerate the kernel statedump");
3554 if (session
->ust_session
) {
3555 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3557 * Currently, the statedump in UST always returns 0.
3560 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3561 ERR("Failed to regenerate the UST statedump");
3565 DBG("Cmd regenerate statedump for session %s", session
->name
);
3572 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3573 struct notification_thread_handle
*notification_thread
)
3577 ssize_t sock_recv_len
;
3578 char *trigger_buffer
= NULL
;
3579 struct lttng_trigger
*trigger
= NULL
;
3581 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3582 trigger_buffer
= zmalloc(trigger_len
);
3583 if (!trigger_buffer
) {
3584 ret
= LTTNG_ERR_NOMEM
;
3588 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
,
3590 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3591 ERR("Failed to receive \"register trigger\" command payload");
3592 /* TODO: should this be a new error enum ? */
3593 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3597 if (lttng_trigger_create_from_buffer(trigger_buffer
, &trigger
) !=
3599 ERR("Invalid trigger payload received in \"register trigger\" command");
3600 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3604 ret
= notification_thread_command_register_trigger(notification_thread
,
3607 free(trigger_buffer
);
3611 int cmd_unregister_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3612 struct notification_thread_handle
*notification_thread
)
3616 ssize_t sock_recv_len
;
3617 char *trigger_buffer
= NULL
;
3618 struct lttng_trigger
*trigger
= NULL
;
3620 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.length
;
3621 trigger_buffer
= zmalloc(trigger_len
);
3622 if (!trigger_buffer
) {
3623 ret
= LTTNG_ERR_NOMEM
;
3627 sock_recv_len
= lttcomm_recv_unix_sock(sock
, trigger_buffer
,
3629 if (sock_recv_len
< 0 || sock_recv_len
!= trigger_len
) {
3630 ERR("Failed to receive \"register trigger\" command payload");
3631 /* TODO: should this be a new error enum ? */
3632 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3636 if (lttng_trigger_create_from_buffer(trigger_buffer
, &trigger
) !=
3638 ERR("Invalid trigger payload received in \"register trigger\" command");
3639 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3643 ret
= notification_thread_command_unregister_trigger(notification_thread
,
3646 free(trigger_buffer
);
3651 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3652 * snapshot output is *not* set with a remote destination.
3654 * Return 0 on success or a LTTNG_ERR code.
3656 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3657 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3660 struct lttng_ht_iter iter
;
3661 struct consumer_socket
*socket
;
3664 assert(snap_output
);
3667 DBG2("Set relayd object from snapshot output");
3669 /* Ignore if snapshot consumer output is not network. */
3670 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3675 * For each consumer socket, create and send the relayd object of the
3679 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3680 socket
, node
.node
) {
3681 ret
= send_consumer_relayd_sockets(0, session
->id
,
3682 snap_output
->consumer
, socket
,
3683 session
->name
, session
->hostname
,
3684 session
->live_timer
);
3685 if (ret
!= LTTNG_OK
) {
3697 * Record a kernel snapshot.
3699 * Return LTTNG_OK on success or a LTTNG_ERR code.
3701 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3702 struct snapshot_output
*output
, struct ltt_session
*session
,
3703 int wait
, uint64_t nb_packets_per_stream
)
3713 * Copy kernel session sockets so we can communicate with the right
3714 * consumer for the snapshot record command.
3716 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3718 ret
= LTTNG_ERR_NOMEM
;
3722 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3723 if (ret
!= LTTNG_OK
) {
3724 goto error_snapshot
;
3727 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3728 if (ret
!= LTTNG_OK
) {
3729 goto error_snapshot
;
3736 /* Clean up copied sockets so this output can use some other later on. */
3737 consumer_destroy_output_sockets(output
->consumer
);
3744 * Record a UST snapshot.
3746 * Return 0 on success or a LTTNG_ERR error code.
3748 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3749 struct snapshot_output
*output
, struct ltt_session
*session
,
3750 int wait
, uint64_t nb_packets_per_stream
)
3759 * Copy UST session sockets so we can communicate with the right
3760 * consumer for the snapshot record command.
3762 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3764 ret
= LTTNG_ERR_NOMEM
;
3768 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3769 if (ret
!= LTTNG_OK
) {
3770 goto error_snapshot
;
3773 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3777 ret
= LTTNG_ERR_INVALID
;
3780 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3783 goto error_snapshot
;
3789 /* Clean up copied sockets so this output can use some other later on. */
3790 consumer_destroy_output_sockets(output
->consumer
);
3796 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3797 uint64_t cur_nr_packets
)
3799 uint64_t tot_size
= 0;
3801 if (session
->kernel_session
) {
3802 struct ltt_kernel_channel
*chan
;
3803 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3805 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3806 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3808 * Don't take channel into account if we
3809 * already grab all its packets.
3813 tot_size
+= chan
->channel
->attr
.subbuf_size
3814 * chan
->stream_count
;
3818 if (session
->ust_session
) {
3819 struct ltt_ust_session
*usess
= session
->ust_session
;
3821 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3829 * Calculate the number of packets we can grab from each stream that
3830 * fits within the overall snapshot max size.
3832 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3833 * the number of packets per stream.
3835 * TODO: this approach is not perfect: we consider the worse case
3836 * (packet filling the sub-buffers) as an upper bound, but we could do
3837 * better if we do this calculation while we actually grab the packet
3838 * content: we would know how much padding we don't actually store into
3841 * This algorithm is currently bounded by the number of packets per
3844 * Since we call this algorithm before actually grabbing the data, it's
3845 * an approximation: for instance, applications could appear/disappear
3846 * in between this call and actually grabbing data.
3849 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3852 uint64_t cur_nb_packets
= 0;
3855 return 0; /* Infinite */
3858 size_left
= max_size
;
3860 uint64_t one_more_packet_tot_size
;
3862 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3864 if (!one_more_packet_tot_size
) {
3865 /* We are already grabbing all packets. */
3868 size_left
-= one_more_packet_tot_size
;
3869 if (size_left
< 0) {
3874 if (!cur_nb_packets
) {
3875 /* Not enough room to grab one packet of each stream, error. */
3878 return cur_nb_packets
;
3882 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3884 * The wait parameter is ignored so this call always wait for the snapshot to
3885 * complete before returning.
3887 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3889 int cmd_snapshot_record(struct ltt_session
*session
,
3890 struct lttng_snapshot_output
*output
, int wait
)
3893 unsigned int use_tmp_output
= 0;
3894 struct snapshot_output tmp_output
;
3895 unsigned int snapshot_success
= 0;
3901 DBG("Cmd snapshot record for session %s", session
->name
);
3903 /* Get the datetime for the snapshot output directory. */
3904 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
3907 ret
= LTTNG_ERR_INVALID
;
3912 * Permission denied to create an output if the session is not
3913 * set in no output mode.
3915 if (session
->output_traces
) {
3916 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3920 /* The session needs to be started at least once. */
3921 if (!session
->has_been_started
) {
3922 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3926 /* Use temporary output for the session. */
3927 if (*output
->ctrl_url
!= '\0') {
3928 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3929 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3932 if (ret
== -ENOMEM
) {
3933 ret
= LTTNG_ERR_NOMEM
;
3935 ret
= LTTNG_ERR_INVALID
;
3939 /* Use the global session count for the temporary snapshot. */
3940 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3942 /* Use the global datetime */
3943 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3947 if (use_tmp_output
) {
3948 int64_t nb_packets_per_stream
;
3950 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3951 tmp_output
.max_size
);
3952 if (nb_packets_per_stream
< 0) {
3953 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3957 if (session
->kernel_session
) {
3958 ret
= record_kernel_snapshot(session
->kernel_session
,
3959 &tmp_output
, session
,
3960 wait
, nb_packets_per_stream
);
3961 if (ret
!= LTTNG_OK
) {
3966 if (session
->ust_session
) {
3967 ret
= record_ust_snapshot(session
->ust_session
,
3968 &tmp_output
, session
,
3969 wait
, nb_packets_per_stream
);
3970 if (ret
!= LTTNG_OK
) {
3975 snapshot_success
= 1;
3977 struct snapshot_output
*sout
;
3978 struct lttng_ht_iter iter
;
3981 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3982 &iter
.iter
, sout
, node
.node
) {
3983 int64_t nb_packets_per_stream
;
3986 * Make a local copy of the output and assign the possible
3987 * temporary value given by the caller.
3989 memset(&tmp_output
, 0, sizeof(tmp_output
));
3990 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3992 if (output
->max_size
!= (uint64_t) -1ULL) {
3993 tmp_output
.max_size
= output
->max_size
;
3996 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3997 tmp_output
.max_size
);
3998 if (nb_packets_per_stream
< 0) {
3999 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
4004 /* Use temporary name. */
4005 if (*output
->name
!= '\0') {
4006 if (lttng_strncpy(tmp_output
.name
, output
->name
,
4007 sizeof(tmp_output
.name
))) {
4008 ret
= LTTNG_ERR_INVALID
;
4014 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
4015 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
4017 if (session
->kernel_session
) {
4018 ret
= record_kernel_snapshot(session
->kernel_session
,
4019 &tmp_output
, session
,
4020 wait
, nb_packets_per_stream
);
4021 if (ret
!= LTTNG_OK
) {
4027 if (session
->ust_session
) {
4028 ret
= record_ust_snapshot(session
->ust_session
,
4029 &tmp_output
, session
,
4030 wait
, nb_packets_per_stream
);
4031 if (ret
!= LTTNG_OK
) {
4036 snapshot_success
= 1;
4041 if (snapshot_success
) {
4042 session
->snapshot
.nb_snapshot
++;
4044 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4052 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4054 int cmd_set_session_shm_path(struct ltt_session
*session
,
4055 const char *shm_path
)
4061 * Can only set shm path before session is started.
4063 if (session
->has_been_started
) {
4064 return LTTNG_ERR_SESSION_STARTED
;
4067 strncpy(session
->shm_path
, shm_path
,
4068 sizeof(session
->shm_path
));
4069 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4075 * Init command subsystem.
4080 * Set network sequence index to 1 for streams to match a relayd
4081 * socket on the consumer side.
4083 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4084 relayd_net_seq_idx
= 1;
4085 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4087 DBG("Command subsystem initialized");