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"
50 * Used to keep a unique index for each relayd socket created where this value
51 * is associated with streams on the consumer so it can match the right relayd
52 * to send to. It must be accessed with the relayd_net_seq_idx_lock
55 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
56 static uint64_t relayd_net_seq_idx
;
58 static int validate_event_name(const char *);
59 static int validate_ust_event_name(const char *);
60 static int cmd_enable_event_internal(struct ltt_session
*session
,
61 struct lttng_domain
*domain
,
62 char *channel_name
, struct lttng_event
*event
,
63 char *filter_expression
,
64 struct lttng_filter_bytecode
*filter
,
65 struct lttng_event_exclusion
*exclusion
,
69 * Create a session path used by list_lttng_sessions for the case that the
70 * session consumer is on the network.
72 static int build_network_session_path(char *dst
, size_t size
,
73 struct ltt_session
*session
)
75 int ret
, kdata_port
, udata_port
;
76 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
77 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
82 memset(tmp_urls
, 0, sizeof(tmp_urls
));
83 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
85 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
87 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
88 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
89 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
92 if (session
->ust_session
&& session
->ust_session
->consumer
) {
93 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
94 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
97 if (uuri
== NULL
&& kuri
== NULL
) {
98 uri
= &session
->consumer
->dst
.net
.control
;
99 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
100 } else if (kuri
&& uuri
) {
101 ret
= uri_compare(kuri
, uuri
);
105 /* Build uuri URL string */
106 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
113 } else if (kuri
&& uuri
== NULL
) {
115 } else if (uuri
&& kuri
== NULL
) {
119 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
125 * Do we have a UST url set. If yes, this means we have both kernel and UST
128 if (*tmp_uurl
!= '\0') {
129 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
130 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
133 if (kuri
|| (!kuri
&& !uuri
)) {
136 /* No kernel URI, use the UST port. */
139 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
147 * Get run-time attributes if the session has been started (discarded events,
150 static int get_kernel_runtime_stats(struct ltt_session
*session
,
151 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
152 uint64_t *lost_packets
)
156 if (!session
->has_been_started
) {
158 *discarded_events
= 0;
163 ret
= consumer_get_discarded_events(session
->id
, kchan
->fd
,
164 session
->kernel_session
->consumer
,
170 ret
= consumer_get_lost_packets(session
->id
, kchan
->fd
,
171 session
->kernel_session
->consumer
,
182 * Get run-time attributes if the session has been started (discarded events,
185 static int get_ust_runtime_stats(struct ltt_session
*session
,
186 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
187 uint64_t *lost_packets
)
190 struct ltt_ust_session
*usess
;
192 if (!discarded_events
|| !lost_packets
) {
197 usess
= session
->ust_session
;
198 assert(discarded_events
);
199 assert(lost_packets
);
201 if (!usess
|| !session
->has_been_started
) {
202 *discarded_events
= 0;
208 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
209 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
210 &usess
->buffer_reg_uid_list
,
211 usess
->consumer
, uchan
->id
,
212 uchan
->attr
.overwrite
,
215 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
216 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
217 uchan
, usess
->consumer
,
218 uchan
->attr
.overwrite
,
224 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
225 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
227 ERR("Unsupported buffer type");
238 * Fill lttng_channel array of all channels.
240 static void list_lttng_channels(enum lttng_domain_type domain
,
241 struct ltt_session
*session
, struct lttng_channel
*channels
,
242 struct lttcomm_channel_extended
*chan_exts
)
245 struct ltt_kernel_channel
*kchan
;
247 DBG("Listing channels for session %s", session
->name
);
250 case LTTNG_DOMAIN_KERNEL
:
251 /* Kernel channels */
252 if (session
->kernel_session
!= NULL
) {
253 cds_list_for_each_entry(kchan
,
254 &session
->kernel_session
->channel_list
.head
, list
) {
255 uint64_t discarded_events
, lost_packets
;
257 ret
= get_kernel_runtime_stats(session
, kchan
,
258 &discarded_events
, &lost_packets
);
262 /* Copy lttng_channel struct to array */
263 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
264 channels
[i
].enabled
= kchan
->enabled
;
265 chan_exts
[i
].discarded_events
=
267 chan_exts
[i
].lost_packets
= lost_packets
;
272 case LTTNG_DOMAIN_UST
:
274 struct lttng_ht_iter iter
;
275 struct ltt_ust_channel
*uchan
;
278 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
279 &iter
.iter
, uchan
, node
.node
) {
280 uint64_t discarded_events
= 0, lost_packets
= 0;
282 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
283 LTTNG_SYMBOL_NAME_LEN
)) {
286 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
287 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
288 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
289 channels
[i
].attr
.switch_timer_interval
=
290 uchan
->attr
.switch_timer_interval
;
291 channels
[i
].attr
.read_timer_interval
=
292 uchan
->attr
.read_timer_interval
;
293 channels
[i
].enabled
= uchan
->enabled
;
294 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
295 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
298 * Map enum lttng_ust_output to enum lttng_event_output.
300 switch (uchan
->attr
.output
) {
302 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
306 * LTTNG_UST_MMAP is the only supported UST
313 ret
= get_ust_runtime_stats(session
, uchan
,
314 &discarded_events
, &lost_packets
);
318 chan_exts
[i
].discarded_events
= discarded_events
;
319 chan_exts
[i
].lost_packets
= lost_packets
;
333 static void increment_extended_len(const char *filter_expression
,
334 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
336 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
338 if (filter_expression
) {
339 *extended_len
+= strlen(filter_expression
) + 1;
343 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
347 static void append_extended_info(const char *filter_expression
,
348 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
350 struct lttcomm_event_extended_header extended_header
;
351 size_t filter_len
= 0;
352 size_t nb_exclusions
= 0;
354 if (filter_expression
) {
355 filter_len
= strlen(filter_expression
) + 1;
359 nb_exclusions
= exclusion
->count
;
362 /* Set header fields */
363 extended_header
.filter_len
= filter_len
;
364 extended_header
.nb_exclusions
= nb_exclusions
;
367 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
368 *extended_at
+= sizeof(extended_header
);
370 /* Copy filter string */
371 if (filter_expression
) {
372 memcpy(*extended_at
, filter_expression
, filter_len
);
373 *extended_at
+= filter_len
;
376 /* Copy exclusion names */
378 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
380 memcpy(*extended_at
, &exclusion
->names
, len
);
386 * Create a list of agent domain events.
388 * Return number of events in list on success or else a negative value.
390 static int list_lttng_agent_events(struct agent
*agt
,
391 struct lttng_event
**events
, size_t *total_size
)
394 unsigned int nb_event
= 0;
395 struct agent_event
*event
;
396 struct lttng_event
*tmp_events
;
397 struct lttng_ht_iter iter
;
398 size_t extended_len
= 0;
404 DBG3("Listing agent events");
407 nb_event
= lttng_ht_get_count(agt
->events
);
415 /* Compute required extended infos size */
416 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
419 * This is only valid because the commands which add events are
420 * processed in the same thread as the listing.
423 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
424 increment_extended_len(event
->filter_expression
, NULL
,
429 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
430 tmp_events
= zmalloc(*total_size
);
432 PERROR("zmalloc agent events session");
433 ret
= -LTTNG_ERR_FATAL
;
437 extended_at
= ((uint8_t *) tmp_events
) +
438 nb_event
* sizeof(struct lttng_event
);
441 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
442 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
443 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
444 tmp_events
[i
].enabled
= event
->enabled
;
445 tmp_events
[i
].loglevel
= event
->loglevel_value
;
446 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
449 /* Append extended info */
450 append_extended_info(event
->filter_expression
, NULL
,
455 *events
= tmp_events
;
459 assert(nb_event
== i
);
464 * Create a list of ust global domain events.
466 static int list_lttng_ust_global_events(char *channel_name
,
467 struct ltt_ust_domain_global
*ust_global
,
468 struct lttng_event
**events
, size_t *total_size
)
471 unsigned int nb_event
= 0;
472 struct lttng_ht_iter iter
;
473 struct lttng_ht_node_str
*node
;
474 struct ltt_ust_channel
*uchan
;
475 struct ltt_ust_event
*uevent
;
476 struct lttng_event
*tmp
;
477 size_t extended_len
= 0;
480 DBG("Listing UST global events for channel %s", channel_name
);
484 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
485 node
= lttng_ht_iter_get_node_str(&iter
);
487 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
491 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
493 nb_event
= lttng_ht_get_count(uchan
->events
);
500 DBG3("Listing UST global %d events", nb_event
);
502 /* Compute required extended infos size */
503 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
504 if (uevent
->internal
) {
509 increment_extended_len(uevent
->filter_expression
,
510 uevent
->exclusion
, &extended_len
);
513 /* All events are internal, skip. */
519 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
520 tmp
= zmalloc(*total_size
);
522 ret
= -LTTNG_ERR_FATAL
;
526 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
528 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
529 if (uevent
->internal
) {
530 /* This event should remain hidden from clients */
533 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
534 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
535 tmp
[i
].enabled
= uevent
->enabled
;
537 switch (uevent
->attr
.instrumentation
) {
538 case LTTNG_UST_TRACEPOINT
:
539 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
541 case LTTNG_UST_PROBE
:
542 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
544 case LTTNG_UST_FUNCTION
:
545 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
549 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
550 switch (uevent
->attr
.loglevel_type
) {
551 case LTTNG_UST_LOGLEVEL_ALL
:
552 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
554 case LTTNG_UST_LOGLEVEL_RANGE
:
555 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
557 case LTTNG_UST_LOGLEVEL_SINGLE
:
558 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
561 if (uevent
->filter
) {
564 if (uevent
->exclusion
) {
565 tmp
[i
].exclusion
= 1;
569 /* Append extended info */
570 append_extended_info(uevent
->filter_expression
,
571 uevent
->exclusion
, &extended_at
);
582 * Fill lttng_event array of all kernel events in the channel.
584 static int list_lttng_kernel_events(char *channel_name
,
585 struct ltt_kernel_session
*kernel_session
,
586 struct lttng_event
**events
, size_t *total_size
)
589 unsigned int nb_event
;
590 struct ltt_kernel_event
*event
;
591 struct ltt_kernel_channel
*kchan
;
592 size_t extended_len
= 0;
595 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
597 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
601 nb_event
= kchan
->event_count
;
603 DBG("Listing events for channel %s", kchan
->channel
->name
);
611 /* Compute required extended infos size */
612 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
613 increment_extended_len(event
->filter_expression
, NULL
,
617 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
618 *events
= zmalloc(*total_size
);
619 if (*events
== NULL
) {
620 ret
= LTTNG_ERR_FATAL
;
624 extended_at
= ((void *) *events
) +
625 nb_event
* sizeof(struct lttng_event
);
627 /* Kernel channels */
628 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
629 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
630 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
631 (*events
)[i
].enabled
= event
->enabled
;
632 (*events
)[i
].filter
=
633 (unsigned char) !!event
->filter_expression
;
635 switch (event
->event
->instrumentation
) {
636 case LTTNG_KERNEL_TRACEPOINT
:
637 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
639 case LTTNG_KERNEL_KRETPROBE
:
640 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
641 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
642 sizeof(struct lttng_kernel_kprobe
));
644 case LTTNG_KERNEL_KPROBE
:
645 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
646 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
647 sizeof(struct lttng_kernel_kprobe
));
649 case LTTNG_KERNEL_FUNCTION
:
650 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
651 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
652 sizeof(struct lttng_kernel_function
));
654 case LTTNG_KERNEL_NOOP
:
655 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
657 case LTTNG_KERNEL_SYSCALL
:
658 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
660 case LTTNG_KERNEL_ALL
:
666 /* Append extended info */
667 append_extended_info(event
->filter_expression
, NULL
,
675 /* Negate the error code to differentiate the size from an error */
680 * Add URI so the consumer output object. Set the correct path depending on the
681 * domain adding the default trace directory.
683 static int add_uri_to_consumer(struct consumer_output
*consumer
,
684 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
685 const char *session_name
)
688 const char *default_trace_dir
;
692 if (consumer
== NULL
) {
693 DBG("No consumer detected. Don't add URI. Stopping.");
694 ret
= LTTNG_ERR_NO_CONSUMER
;
699 case LTTNG_DOMAIN_KERNEL
:
700 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
702 case LTTNG_DOMAIN_UST
:
703 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
707 * This case is possible is we try to add the URI to the global tracing
708 * session consumer object which in this case there is no subdir.
710 default_trace_dir
= "";
713 switch (uri
->dtype
) {
716 DBG2("Setting network URI to consumer");
718 if (consumer
->type
== CONSUMER_DST_NET
) {
719 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
720 consumer
->dst
.net
.control_isset
) ||
721 (uri
->stype
== LTTNG_STREAM_DATA
&&
722 consumer
->dst
.net
.data_isset
)) {
723 ret
= LTTNG_ERR_URL_EXIST
;
727 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
730 consumer
->type
= CONSUMER_DST_NET
;
732 /* Set URI into consumer output object */
733 ret
= consumer_set_network_uri(consumer
, uri
);
737 } else if (ret
== 1) {
739 * URI was the same in the consumer so we do not append the subdir
740 * again so to not duplicate output dir.
746 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
747 ret
= consumer_set_subdir(consumer
, session_name
);
749 ret
= LTTNG_ERR_FATAL
;
754 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
755 /* On a new subdir, reappend the default trace dir. */
756 strncat(consumer
->subdir
, default_trace_dir
,
757 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
758 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
763 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
764 memset(consumer
->dst
.trace_path
, 0,
765 sizeof(consumer
->dst
.trace_path
));
766 /* Explicit length checks for strcpy and strcat. */
767 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
768 >= sizeof(consumer
->dst
.trace_path
)) {
769 ret
= LTTNG_ERR_FATAL
;
772 strcpy(consumer
->dst
.trace_path
, uri
->dst
.path
);
773 /* Append default trace dir */
774 strcat(consumer
->dst
.trace_path
, default_trace_dir
);
775 /* Flag consumer as local. */
776 consumer
->type
= CONSUMER_DST_LOCAL
;
787 * Init tracing by creating trace directory and sending fds kernel consumer.
789 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
792 struct lttng_ht_iter iter
;
793 struct consumer_socket
*socket
;
799 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
800 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
802 pthread_mutex_lock(socket
->lock
);
803 ret
= kernel_consumer_send_session(socket
, session
);
804 pthread_mutex_unlock(socket
->lock
);
806 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
818 * Create a socket to the relayd using the URI.
820 * On success, the relayd_sock pointer is set to the created socket.
821 * Else, it's stays untouched and a lttcomm error code is returned.
823 static int create_connect_relayd(struct lttng_uri
*uri
,
824 struct lttcomm_relayd_sock
**relayd_sock
,
825 struct consumer_output
*consumer
)
828 struct lttcomm_relayd_sock
*rsock
;
830 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
831 RELAYD_VERSION_COMM_MINOR
);
833 ret
= LTTNG_ERR_FATAL
;
838 * Connect to relayd so we can proceed with a session creation. This call
839 * can possibly block for an arbitrary amount of time to set the health
840 * state to be in poll execution.
843 ret
= relayd_connect(rsock
);
846 ERR("Unable to reach lttng-relayd");
847 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
851 /* Create socket for control stream. */
852 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
853 DBG3("Creating relayd stream socket from URI");
855 /* Check relayd version */
856 ret
= relayd_version_check(rsock
);
858 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
861 consumer
->relay_major_version
= rsock
->major
;
862 consumer
->relay_minor_version
= rsock
->minor
;
863 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
864 DBG3("Creating relayd data socket from URI");
866 /* Command is not valid */
867 ERR("Relayd invalid stream type: %d", uri
->stype
);
868 ret
= LTTNG_ERR_INVALID
;
872 *relayd_sock
= rsock
;
877 /* The returned value is not useful since we are on an error path. */
878 (void) relayd_close(rsock
);
886 * Connect to the relayd using URI and send the socket to the right consumer.
888 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
889 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
890 struct consumer_output
*consumer
,
891 struct consumer_socket
*consumer_sock
,
892 char *session_name
, char *hostname
, int session_live_timer
)
895 struct lttcomm_relayd_sock
*rsock
= NULL
;
897 /* Connect to relayd and make version check if uri is the control. */
898 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
899 if (ret
!= LTTNG_OK
) {
904 /* Set the network sequence index if not set. */
905 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
906 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
908 * Increment net_seq_idx because we are about to transfer the
909 * new relayd socket to the consumer.
910 * Assign unique key so the consumer can match streams.
912 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
913 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
916 /* Send relayd socket to consumer. */
917 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
918 relayd_uri
->stype
, session_id
,
919 session_name
, hostname
, session_live_timer
);
921 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
925 /* Flag that the corresponding socket was sent. */
926 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
927 consumer_sock
->control_sock_sent
= 1;
928 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
929 consumer_sock
->data_sock_sent
= 1;
935 * Close socket which was dup on the consumer side. The session daemon does
936 * NOT keep track of the relayd socket(s) once transfer to the consumer.
940 (void) relayd_close(rsock
);
944 if (ret
!= LTTNG_OK
) {
946 * The consumer output for this session should not be used anymore
947 * since the relayd connection failed thus making any tracing or/and
948 * streaming not usable.
950 consumer
->enabled
= 0;
956 * Send both relayd sockets to a specific consumer and domain. This is a
957 * helper function to facilitate sending the information to the consumer for a
960 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
961 unsigned int session_id
, struct consumer_output
*consumer
,
962 struct consumer_socket
*sock
, char *session_name
,
963 char *hostname
, int session_live_timer
)
970 /* Sending control relayd socket. */
971 if (!sock
->control_sock_sent
) {
972 ret
= send_consumer_relayd_socket(domain
, session_id
,
973 &consumer
->dst
.net
.control
, consumer
, sock
,
974 session_name
, hostname
, session_live_timer
);
975 if (ret
!= LTTNG_OK
) {
980 /* Sending data relayd socket. */
981 if (!sock
->data_sock_sent
) {
982 ret
= send_consumer_relayd_socket(domain
, session_id
,
983 &consumer
->dst
.net
.data
, consumer
, sock
,
984 session_name
, hostname
, session_live_timer
);
985 if (ret
!= LTTNG_OK
) {
995 * Setup relayd connections for a tracing session. First creates the socket to
996 * the relayd and send them to the right domain consumer. Consumer type MUST be
999 int cmd_setup_relayd(struct ltt_session
*session
)
1002 struct ltt_ust_session
*usess
;
1003 struct ltt_kernel_session
*ksess
;
1004 struct consumer_socket
*socket
;
1005 struct lttng_ht_iter iter
;
1009 usess
= session
->ust_session
;
1010 ksess
= session
->kernel_session
;
1012 DBG("Setting relayd for session %s", session
->name
);
1016 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1017 && usess
->consumer
->enabled
) {
1018 /* For each consumer socket, send relayd sockets */
1019 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1020 socket
, node
.node
) {
1021 pthread_mutex_lock(socket
->lock
);
1022 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1023 usess
->consumer
, socket
,
1024 session
->name
, session
->hostname
,
1025 session
->live_timer
);
1026 pthread_mutex_unlock(socket
->lock
);
1027 if (ret
!= LTTNG_OK
) {
1030 /* Session is now ready for network streaming. */
1031 session
->net_handle
= 1;
1033 session
->consumer
->relay_major_version
=
1034 usess
->consumer
->relay_major_version
;
1035 session
->consumer
->relay_minor_version
=
1036 usess
->consumer
->relay_minor_version
;
1039 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1040 && ksess
->consumer
->enabled
) {
1041 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1042 socket
, node
.node
) {
1043 pthread_mutex_lock(socket
->lock
);
1044 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1045 ksess
->consumer
, socket
,
1046 session
->name
, session
->hostname
,
1047 session
->live_timer
);
1048 pthread_mutex_unlock(socket
->lock
);
1049 if (ret
!= LTTNG_OK
) {
1052 /* Session is now ready for network streaming. */
1053 session
->net_handle
= 1;
1055 session
->consumer
->relay_major_version
=
1056 ksess
->consumer
->relay_major_version
;
1057 session
->consumer
->relay_minor_version
=
1058 ksess
->consumer
->relay_minor_version
;
1067 * Start a kernel session by opening all necessary streams.
1069 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1072 struct ltt_kernel_channel
*kchan
;
1074 /* Open kernel metadata */
1075 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1076 ret
= kernel_open_metadata(ksess
);
1078 ret
= LTTNG_ERR_KERN_META_FAIL
;
1083 /* Open kernel metadata stream */
1084 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1085 ret
= kernel_open_metadata_stream(ksess
);
1087 ERR("Kernel create metadata stream failed");
1088 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1093 /* For each channel */
1094 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1095 if (kchan
->stream_count
== 0) {
1096 ret
= kernel_open_channel_stream(kchan
);
1098 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1101 /* Update the stream global counter */
1102 ksess
->stream_count_global
+= ret
;
1106 /* Setup kernel consumer socket and send fds to it */
1107 ret
= init_kernel_tracing(ksess
);
1109 ret
= LTTNG_ERR_KERN_START_FAIL
;
1113 /* This start the kernel tracing */
1114 ret
= kernel_start_session(ksess
);
1116 ret
= LTTNG_ERR_KERN_START_FAIL
;
1120 /* Quiescent wait after starting trace */
1121 kernel_wait_quiescent(kernel_tracer_fd
);
1132 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1134 int cmd_disable_channel(struct ltt_session
*session
,
1135 enum lttng_domain_type domain
, char *channel_name
)
1138 struct ltt_ust_session
*usess
;
1140 usess
= session
->ust_session
;
1145 case LTTNG_DOMAIN_KERNEL
:
1147 ret
= channel_kernel_disable(session
->kernel_session
,
1149 if (ret
!= LTTNG_OK
) {
1153 kernel_wait_quiescent(kernel_tracer_fd
);
1156 case LTTNG_DOMAIN_UST
:
1158 struct ltt_ust_channel
*uchan
;
1159 struct lttng_ht
*chan_ht
;
1161 chan_ht
= usess
->domain_global
.channels
;
1163 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1164 if (uchan
== NULL
) {
1165 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1169 ret
= channel_ust_disable(usess
, uchan
);
1170 if (ret
!= LTTNG_OK
) {
1176 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1188 * Command LTTNG_TRACK_PID processed by the client thread.
1190 * Called with session lock held.
1192 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1200 case LTTNG_DOMAIN_KERNEL
:
1202 struct ltt_kernel_session
*ksess
;
1204 ksess
= session
->kernel_session
;
1206 ret
= kernel_track_pid(ksess
, pid
);
1207 if (ret
!= LTTNG_OK
) {
1211 kernel_wait_quiescent(kernel_tracer_fd
);
1214 case LTTNG_DOMAIN_UST
:
1216 struct ltt_ust_session
*usess
;
1218 usess
= session
->ust_session
;
1220 ret
= trace_ust_track_pid(usess
, pid
);
1221 if (ret
!= LTTNG_OK
) {
1227 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1239 * Command LTTNG_UNTRACK_PID processed by the client thread.
1241 * Called with session lock held.
1243 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1251 case LTTNG_DOMAIN_KERNEL
:
1253 struct ltt_kernel_session
*ksess
;
1255 ksess
= session
->kernel_session
;
1257 ret
= kernel_untrack_pid(ksess
, pid
);
1258 if (ret
!= LTTNG_OK
) {
1262 kernel_wait_quiescent(kernel_tracer_fd
);
1265 case LTTNG_DOMAIN_UST
:
1267 struct ltt_ust_session
*usess
;
1269 usess
= session
->ust_session
;
1271 ret
= trace_ust_untrack_pid(usess
, pid
);
1272 if (ret
!= LTTNG_OK
) {
1278 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1290 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1292 * The wpipe arguments is used as a notifier for the kernel thread.
1294 int cmd_enable_channel(struct ltt_session
*session
,
1295 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1298 struct ltt_ust_session
*usess
= session
->ust_session
;
1299 struct lttng_ht
*chan_ht
;
1306 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1308 /* Validate channel name */
1309 if (attr
->name
[0] == '.' ||
1310 memchr(attr
->name
, '/', len
) != NULL
) {
1311 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1315 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1320 * Don't try to enable a channel if the session has been started at
1321 * some point in time before. The tracer does not allow it.
1323 if (session
->has_been_started
) {
1324 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1329 * If the session is a live session, remove the switch timer, the
1330 * live timer does the same thing but sends also synchronisation
1331 * beacons for inactive streams.
1333 if (session
->live_timer
> 0) {
1334 attr
->attr
.live_timer_interval
= session
->live_timer
;
1335 attr
->attr
.switch_timer_interval
= 0;
1338 switch (domain
->type
) {
1339 case LTTNG_DOMAIN_KERNEL
:
1341 struct ltt_kernel_channel
*kchan
;
1343 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1344 session
->kernel_session
);
1345 if (kchan
== NULL
) {
1346 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1347 if (attr
->name
[0] != '\0') {
1348 session
->kernel_session
->has_non_default_channel
= 1;
1351 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1354 if (ret
!= LTTNG_OK
) {
1358 kernel_wait_quiescent(kernel_tracer_fd
);
1361 case LTTNG_DOMAIN_UST
:
1362 case LTTNG_DOMAIN_JUL
:
1363 case LTTNG_DOMAIN_LOG4J
:
1364 case LTTNG_DOMAIN_PYTHON
:
1366 struct ltt_ust_channel
*uchan
;
1371 * Current agent implementation limitations force us to allow
1372 * only one channel at once in "agent" subdomains. Each
1373 * subdomain has a default channel name which must be strictly
1376 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1377 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1378 LTTNG_SYMBOL_NAME_LEN
)) {
1379 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1382 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1383 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1384 LTTNG_SYMBOL_NAME_LEN
)) {
1385 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1388 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1389 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1390 LTTNG_SYMBOL_NAME_LEN
)) {
1391 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1396 chan_ht
= usess
->domain_global
.channels
;
1398 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1399 if (uchan
== NULL
) {
1400 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1401 if (attr
->name
[0] != '\0') {
1402 usess
->has_non_default_channel
= 1;
1405 ret
= channel_ust_enable(usess
, uchan
);
1410 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1421 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1423 int cmd_disable_event(struct ltt_session
*session
,
1424 enum lttng_domain_type domain
, char *channel_name
,
1425 struct lttng_event
*event
)
1430 DBG("Disable event command for event \'%s\'", event
->name
);
1432 event_name
= event
->name
;
1433 if (validate_event_name(event_name
)) {
1434 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1438 /* Error out on unhandled search criteria */
1439 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1440 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1441 ret
= LTTNG_ERR_UNK
;
1448 case LTTNG_DOMAIN_KERNEL
:
1450 struct ltt_kernel_channel
*kchan
;
1451 struct ltt_kernel_session
*ksess
;
1453 ksess
= session
->kernel_session
;
1456 * If a non-default channel has been created in the
1457 * session, explicitely require that -c chan_name needs
1460 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1461 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1465 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1466 if (kchan
== NULL
) {
1467 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1471 switch (event
->type
) {
1472 case LTTNG_EVENT_ALL
:
1473 case LTTNG_EVENT_TRACEPOINT
:
1474 case LTTNG_EVENT_SYSCALL
:
1475 case LTTNG_EVENT_PROBE
:
1476 case LTTNG_EVENT_FUNCTION
:
1477 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1478 if (event_name
[0] == '\0') {
1479 ret
= event_kernel_disable_event(kchan
,
1482 ret
= event_kernel_disable_event(kchan
,
1483 event_name
, event
->type
);
1485 if (ret
!= LTTNG_OK
) {
1490 ret
= LTTNG_ERR_UNK
;
1494 kernel_wait_quiescent(kernel_tracer_fd
);
1497 case LTTNG_DOMAIN_UST
:
1499 struct ltt_ust_channel
*uchan
;
1500 struct ltt_ust_session
*usess
;
1502 usess
= session
->ust_session
;
1504 if (validate_ust_event_name(event_name
)) {
1505 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1510 * If a non-default channel has been created in the
1511 * session, explicitly require that -c chan_name needs
1514 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1515 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1519 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1521 if (uchan
== NULL
) {
1522 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1526 switch (event
->type
) {
1527 case LTTNG_EVENT_ALL
:
1529 * An empty event name means that everything
1530 * should be disabled.
1532 if (event
->name
[0] == '\0') {
1533 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1535 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1538 if (ret
!= LTTNG_OK
) {
1543 ret
= LTTNG_ERR_UNK
;
1547 DBG3("Disable UST event %s in channel %s completed", event_name
,
1551 case LTTNG_DOMAIN_LOG4J
:
1552 case LTTNG_DOMAIN_JUL
:
1553 case LTTNG_DOMAIN_PYTHON
:
1556 struct ltt_ust_session
*usess
= session
->ust_session
;
1560 switch (event
->type
) {
1561 case LTTNG_EVENT_ALL
:
1564 ret
= LTTNG_ERR_UNK
;
1568 agt
= trace_ust_find_agent(usess
, domain
);
1570 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1574 * An empty event name means that everything
1575 * should be disabled.
1577 if (event
->name
[0] == '\0') {
1578 ret
= event_agent_disable_all(usess
, agt
);
1580 ret
= event_agent_disable(usess
, agt
, event_name
);
1582 if (ret
!= LTTNG_OK
) {
1589 ret
= LTTNG_ERR_UND
;
1602 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1604 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1605 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1607 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1608 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1610 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1611 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1612 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1616 case LTTNG_DOMAIN_KERNEL
:
1617 assert(session
->kernel_session
);
1619 if (session
->kernel_session
->channel_count
== 0) {
1620 /* Create default channel */
1621 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1622 if (ret
!= LTTNG_OK
) {
1625 chan_kern_created
= 1;
1627 /* Add kernel context to kernel tracer */
1628 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1629 if (ret
!= LTTNG_OK
) {
1633 case LTTNG_DOMAIN_JUL
:
1634 case LTTNG_DOMAIN_LOG4J
:
1637 * Validate channel name.
1638 * If no channel name is given and the domain is JUL or LOG4J,
1639 * set it to the appropriate domain-specific channel name. If
1640 * a name is provided but does not match the expexted channel
1641 * name, return an error.
1643 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1644 strcmp(channel_name
,
1645 DEFAULT_JUL_CHANNEL_NAME
)) {
1646 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1648 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1649 strcmp(channel_name
,
1650 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1651 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1654 /* break is _not_ missing here. */
1656 case LTTNG_DOMAIN_UST
:
1658 struct ltt_ust_session
*usess
= session
->ust_session
;
1659 unsigned int chan_count
;
1663 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1664 if (chan_count
== 0) {
1665 struct lttng_channel
*attr
;
1666 /* Create default channel */
1667 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1669 ret
= LTTNG_ERR_FATAL
;
1673 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1674 if (ret
!= LTTNG_OK
) {
1679 chan_ust_created
= 1;
1682 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1683 free(app_ctx_provider_name
);
1685 app_ctx_name
= NULL
;
1686 app_ctx_provider_name
= NULL
;
1687 if (ret
!= LTTNG_OK
) {
1693 ret
= LTTNG_ERR_UND
;
1701 if (chan_kern_created
) {
1702 struct ltt_kernel_channel
*kchan
=
1703 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1704 session
->kernel_session
);
1705 /* Created previously, this should NOT fail. */
1707 kernel_destroy_channel(kchan
);
1710 if (chan_ust_created
) {
1711 struct ltt_ust_channel
*uchan
=
1712 trace_ust_find_channel_by_name(
1713 session
->ust_session
->domain_global
.channels
,
1714 DEFAULT_CHANNEL_NAME
);
1715 /* Created previously, this should NOT fail. */
1717 /* Remove from the channel list of the session. */
1718 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1720 trace_ust_destroy_channel(uchan
);
1723 free(app_ctx_provider_name
);
1728 static int validate_event_name(const char *name
)
1731 const char *c
= name
;
1732 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1733 bool null_terminated
= false;
1736 * Make sure that unescaped wildcards are only used as the last
1737 * character of the event name.
1739 while (c
< event_name_end
) {
1742 null_terminated
= true;
1748 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1749 /* Wildcard is not the last character */
1750 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1759 if (!ret
&& !null_terminated
) {
1760 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1765 static inline bool name_starts_with(const char *name
, const char *prefix
)
1767 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1769 return !strncmp(name
, prefix
, max_cmp_len
);
1772 /* Perform userspace-specific event name validation */
1773 static int validate_ust_event_name(const char *name
)
1783 * Check name against all internal UST event component namespaces used
1786 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1787 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1788 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1797 * Internal version of cmd_enable_event() with a supplemental
1798 * "internal_event" flag which is used to enable internal events which should
1799 * be hidden from clients. Such events are used in the agent implementation to
1800 * enable the events through which all "agent" events are funeled.
1802 static int _cmd_enable_event(struct ltt_session
*session
,
1803 struct lttng_domain
*domain
,
1804 char *channel_name
, struct lttng_event
*event
,
1805 char *filter_expression
,
1806 struct lttng_filter_bytecode
*filter
,
1807 struct lttng_event_exclusion
*exclusion
,
1808 int wpipe
, bool internal_event
)
1810 int ret
, channel_created
= 0;
1811 struct lttng_channel
*attr
= NULL
;
1815 assert(channel_name
);
1817 /* If we have a filter, we must have its filter expression */
1818 assert(!(!!filter_expression
^ !!filter
));
1820 DBG("Enable event command for event \'%s\'", event
->name
);
1824 ret
= validate_event_name(event
->name
);
1829 switch (domain
->type
) {
1830 case LTTNG_DOMAIN_KERNEL
:
1832 struct ltt_kernel_channel
*kchan
;
1835 * If a non-default channel has been created in the
1836 * session, explicitely require that -c chan_name needs
1839 if (session
->kernel_session
->has_non_default_channel
1840 && channel_name
[0] == '\0') {
1841 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1845 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1846 session
->kernel_session
);
1847 if (kchan
== NULL
) {
1848 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1849 LTTNG_BUFFER_GLOBAL
);
1851 ret
= LTTNG_ERR_FATAL
;
1854 if (lttng_strncpy(attr
->name
, channel_name
,
1855 sizeof(attr
->name
))) {
1856 ret
= LTTNG_ERR_INVALID
;
1860 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1861 if (ret
!= LTTNG_OK
) {
1864 channel_created
= 1;
1867 /* Get the newly created kernel channel pointer */
1868 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1869 session
->kernel_session
);
1870 if (kchan
== NULL
) {
1871 /* This sould not happen... */
1872 ret
= LTTNG_ERR_FATAL
;
1876 switch (event
->type
) {
1877 case LTTNG_EVENT_ALL
:
1879 char *filter_expression_a
= NULL
;
1880 struct lttng_filter_bytecode
*filter_a
= NULL
;
1883 * We need to duplicate filter_expression and filter,
1884 * because ownership is passed to first enable
1887 if (filter_expression
) {
1888 filter_expression_a
= strdup(filter_expression
);
1889 if (!filter_expression_a
) {
1890 ret
= LTTNG_ERR_FATAL
;
1895 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1897 free(filter_expression_a
);
1898 ret
= LTTNG_ERR_FATAL
;
1901 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1903 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1904 ret
= event_kernel_enable_event(kchan
, event
,
1905 filter_expression
, filter
);
1906 /* We have passed ownership */
1907 filter_expression
= NULL
;
1909 if (ret
!= LTTNG_OK
) {
1910 if (channel_created
) {
1911 /* Let's not leak a useless channel. */
1912 kernel_destroy_channel(kchan
);
1914 free(filter_expression_a
);
1918 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1919 ret
= event_kernel_enable_event(kchan
, event
,
1920 filter_expression_a
, filter_a
);
1921 /* We have passed ownership */
1922 filter_expression_a
= NULL
;
1924 if (ret
!= LTTNG_OK
) {
1929 case LTTNG_EVENT_PROBE
:
1930 case LTTNG_EVENT_FUNCTION
:
1931 case LTTNG_EVENT_FUNCTION_ENTRY
:
1932 case LTTNG_EVENT_TRACEPOINT
:
1933 ret
= event_kernel_enable_event(kchan
, event
,
1934 filter_expression
, filter
);
1935 /* We have passed ownership */
1936 filter_expression
= NULL
;
1938 if (ret
!= LTTNG_OK
) {
1939 if (channel_created
) {
1940 /* Let's not leak a useless channel. */
1941 kernel_destroy_channel(kchan
);
1946 case LTTNG_EVENT_SYSCALL
:
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
) {
1957 ret
= LTTNG_ERR_UNK
;
1961 kernel_wait_quiescent(kernel_tracer_fd
);
1964 case LTTNG_DOMAIN_UST
:
1966 struct ltt_ust_channel
*uchan
;
1967 struct ltt_ust_session
*usess
= session
->ust_session
;
1972 * If a non-default channel has been created in the
1973 * session, explicitely require that -c chan_name needs
1976 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1977 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1981 /* Get channel from global UST domain */
1982 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1984 if (uchan
== NULL
) {
1985 /* Create default channel */
1986 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1987 usess
->buffer_type
);
1989 ret
= LTTNG_ERR_FATAL
;
1992 if (lttng_strncpy(attr
->name
, channel_name
,
1993 sizeof(attr
->name
))) {
1994 ret
= LTTNG_ERR_INVALID
;
1998 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1999 if (ret
!= LTTNG_OK
) {
2003 /* Get the newly created channel reference back */
2004 uchan
= trace_ust_find_channel_by_name(
2005 usess
->domain_global
.channels
, channel_name
);
2009 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2011 * Don't allow users to add UST events to channels which
2012 * are assigned to a userspace subdomain (JUL, Log4J,
2015 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2019 if (!internal_event
) {
2021 * Ensure the event name is not reserved for internal
2024 ret
= validate_ust_event_name(event
->name
);
2026 WARN("Userspace event name %s failed validation.",
2028 event
->name
: "NULL");
2029 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2034 /* At this point, the session and channel exist on the tracer */
2035 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2036 filter_expression
, filter
, exclusion
,
2038 /* We have passed ownership */
2039 filter_expression
= NULL
;
2042 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2043 goto already_enabled
;
2044 } else if (ret
!= LTTNG_OK
) {
2049 case LTTNG_DOMAIN_LOG4J
:
2050 case LTTNG_DOMAIN_JUL
:
2051 case LTTNG_DOMAIN_PYTHON
:
2053 const char *default_event_name
, *default_chan_name
;
2055 struct lttng_event uevent
;
2056 struct lttng_domain tmp_dom
;
2057 struct ltt_ust_session
*usess
= session
->ust_session
;
2061 agt
= trace_ust_find_agent(usess
, domain
->type
);
2063 agt
= agent_create(domain
->type
);
2065 ret
= LTTNG_ERR_NOMEM
;
2068 agent_add(agt
, usess
->agents
);
2071 /* Create the default tracepoint. */
2072 memset(&uevent
, 0, sizeof(uevent
));
2073 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2074 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2075 default_event_name
= event_get_default_agent_ust_name(
2077 if (!default_event_name
) {
2078 ret
= LTTNG_ERR_FATAL
;
2081 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2082 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2085 * The domain type is changed because we are about to enable the
2086 * default channel and event for the JUL domain that are hardcoded.
2087 * This happens in the UST domain.
2089 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2090 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2092 switch (domain
->type
) {
2093 case LTTNG_DOMAIN_LOG4J
:
2094 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2096 case LTTNG_DOMAIN_JUL
:
2097 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2099 case LTTNG_DOMAIN_PYTHON
:
2100 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2103 /* The switch/case we are in makes this impossible */
2108 char *filter_expression_copy
= NULL
;
2109 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2112 const size_t filter_size
= sizeof(
2113 struct lttng_filter_bytecode
)
2116 filter_copy
= zmalloc(filter_size
);
2118 ret
= LTTNG_ERR_NOMEM
;
2121 memcpy(filter_copy
, filter
, filter_size
);
2123 filter_expression_copy
=
2124 strdup(filter_expression
);
2125 if (!filter_expression
) {
2126 ret
= LTTNG_ERR_NOMEM
;
2129 if (!filter_expression_copy
|| !filter_copy
) {
2130 free(filter_expression_copy
);
2136 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2137 (char *) default_chan_name
,
2138 &uevent
, filter_expression_copy
,
2139 filter_copy
, NULL
, wpipe
);
2142 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2143 goto already_enabled
;
2144 } else if (ret
!= LTTNG_OK
) {
2148 /* The wild card * means that everything should be enabled. */
2149 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2150 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2153 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2157 filter_expression
= NULL
;
2158 if (ret
!= LTTNG_OK
) {
2165 ret
= LTTNG_ERR_UND
;
2173 free(filter_expression
);
2182 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2183 * We own filter, exclusion, and filter_expression.
2185 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2186 char *channel_name
, struct lttng_event
*event
,
2187 char *filter_expression
,
2188 struct lttng_filter_bytecode
*filter
,
2189 struct lttng_event_exclusion
*exclusion
,
2192 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2193 filter_expression
, filter
, exclusion
, wpipe
, false);
2197 * Enable an event which is internal to LTTng. An internal should
2198 * never be made visible to clients and are immune to checks such as
2201 static int cmd_enable_event_internal(struct ltt_session
*session
,
2202 struct lttng_domain
*domain
,
2203 char *channel_name
, struct lttng_event
*event
,
2204 char *filter_expression
,
2205 struct lttng_filter_bytecode
*filter
,
2206 struct lttng_event_exclusion
*exclusion
,
2209 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2210 filter_expression
, filter
, exclusion
, wpipe
, true);
2214 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2216 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2217 struct lttng_event
**events
)
2220 ssize_t nb_events
= 0;
2223 case LTTNG_DOMAIN_KERNEL
:
2224 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2225 if (nb_events
< 0) {
2226 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2230 case LTTNG_DOMAIN_UST
:
2231 nb_events
= ust_app_list_events(events
);
2232 if (nb_events
< 0) {
2233 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2237 case LTTNG_DOMAIN_LOG4J
:
2238 case LTTNG_DOMAIN_JUL
:
2239 case LTTNG_DOMAIN_PYTHON
:
2240 nb_events
= agent_list_events(events
, domain
);
2241 if (nb_events
< 0) {
2242 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2247 ret
= LTTNG_ERR_UND
;
2254 /* Return negative value to differentiate return code */
2259 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2261 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2262 struct lttng_event_field
**fields
)
2265 ssize_t nb_fields
= 0;
2268 case LTTNG_DOMAIN_UST
:
2269 nb_fields
= ust_app_list_event_fields(fields
);
2270 if (nb_fields
< 0) {
2271 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2275 case LTTNG_DOMAIN_KERNEL
:
2276 default: /* fall-through */
2277 ret
= LTTNG_ERR_UND
;
2284 /* Return negative value to differentiate return code */
2288 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2290 return syscall_table_list(events
);
2294 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2296 * Called with session lock held.
2298 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2299 enum lttng_domain_type domain
, int32_t **pids
)
2302 ssize_t nr_pids
= 0;
2305 case LTTNG_DOMAIN_KERNEL
:
2307 struct ltt_kernel_session
*ksess
;
2309 ksess
= session
->kernel_session
;
2310 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2312 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2317 case LTTNG_DOMAIN_UST
:
2319 struct ltt_ust_session
*usess
;
2321 usess
= session
->ust_session
;
2322 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2324 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2329 case LTTNG_DOMAIN_LOG4J
:
2330 case LTTNG_DOMAIN_JUL
:
2331 case LTTNG_DOMAIN_PYTHON
:
2333 ret
= LTTNG_ERR_UND
;
2340 /* Return negative value to differentiate return code */
2345 * Command LTTNG_START_TRACE processed by the client thread.
2347 * Called with session mutex held.
2349 int cmd_start_trace(struct ltt_session
*session
)
2352 unsigned long nb_chan
= 0;
2353 struct ltt_kernel_session
*ksession
;
2354 struct ltt_ust_session
*usess
;
2358 /* Ease our life a bit ;) */
2359 ksession
= session
->kernel_session
;
2360 usess
= session
->ust_session
;
2362 /* Is the session already started? */
2363 if (session
->active
) {
2364 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2369 * Starting a session without channel is useless since after that it's not
2370 * possible to enable channel thus inform the client.
2372 if (usess
&& usess
->domain_global
.channels
) {
2373 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2376 nb_chan
+= ksession
->channel_count
;
2379 ret
= LTTNG_ERR_NO_CHANNEL
;
2383 /* Kernel tracing */
2384 if (ksession
!= NULL
) {
2385 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2386 if (ret
!= LTTNG_OK
) {
2391 /* Flag session that trace should start automatically */
2394 * Even though the start trace might fail, flag this session active so
2395 * other application coming in are started by default.
2399 ret
= ust_app_start_trace_all(usess
);
2401 ret
= LTTNG_ERR_UST_START_FAIL
;
2406 /* Flag this after a successful start. */
2407 session
->has_been_started
= 1;
2408 session
->active
= 1;
2417 * Command LTTNG_STOP_TRACE processed by the client thread.
2419 int cmd_stop_trace(struct ltt_session
*session
)
2422 struct ltt_kernel_channel
*kchan
;
2423 struct ltt_kernel_session
*ksession
;
2424 struct ltt_ust_session
*usess
;
2429 ksession
= session
->kernel_session
;
2430 usess
= session
->ust_session
;
2432 /* Session is not active. Skip everythong and inform the client. */
2433 if (!session
->active
) {
2434 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2439 if (ksession
&& ksession
->active
) {
2440 DBG("Stop kernel tracing");
2442 ret
= kernel_stop_session(ksession
);
2444 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2448 kernel_wait_quiescent(kernel_tracer_fd
);
2450 /* Flush metadata after stopping (if exists) */
2451 if (ksession
->metadata_stream_fd
>= 0) {
2452 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2454 ERR("Kernel metadata flush failed");
2458 /* Flush all buffers after stopping */
2459 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2460 ret
= kernel_flush_buffer(kchan
);
2462 ERR("Kernel flush buffer error");
2466 ksession
->active
= 0;
2469 if (usess
&& usess
->active
) {
2471 * Even though the stop trace might fail, flag this session inactive so
2472 * other application coming in are not started by default.
2476 ret
= ust_app_stop_trace_all(usess
);
2478 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2483 /* Flag inactive after a successful stop. */
2484 session
->active
= 0;
2492 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2494 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2495 struct lttng_uri
*uris
)
2498 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2499 struct ltt_ust_session
*usess
= session
->ust_session
;
2505 /* Can't set consumer URI if the session is active. */
2506 if (session
->active
) {
2507 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2511 /* Set the "global" consumer URIs */
2512 for (i
= 0; i
< nb_uri
; i
++) {
2513 ret
= add_uri_to_consumer(session
->consumer
,
2514 &uris
[i
], 0, session
->name
);
2515 if (ret
!= LTTNG_OK
) {
2520 /* Set UST session URIs */
2521 if (session
->ust_session
) {
2522 for (i
= 0; i
< nb_uri
; i
++) {
2523 ret
= add_uri_to_consumer(
2524 session
->ust_session
->consumer
,
2525 &uris
[i
], LTTNG_DOMAIN_UST
,
2527 if (ret
!= LTTNG_OK
) {
2533 /* Set kernel session URIs */
2534 if (session
->kernel_session
) {
2535 for (i
= 0; i
< nb_uri
; i
++) {
2536 ret
= add_uri_to_consumer(
2537 session
->kernel_session
->consumer
,
2538 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2540 if (ret
!= LTTNG_OK
) {
2547 * Make sure to set the session in output mode after we set URI since a
2548 * session can be created without URL (thus flagged in no output mode).
2550 session
->output_traces
= 1;
2552 ksess
->output_traces
= 1;
2556 usess
->output_traces
= 1;
2567 * Command LTTNG_CREATE_SESSION processed by the client thread.
2569 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2570 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2573 struct ltt_session
*session
;
2579 * Verify if the session already exist
2581 * XXX: There is no need for the session lock list here since the caller
2582 * (process_client_msg) is holding it. We might want to change that so a
2583 * single command does not lock the entire session list.
2585 session
= session_find_by_name(name
);
2586 if (session
!= NULL
) {
2587 ret
= LTTNG_ERR_EXIST_SESS
;
2591 /* Create tracing session in the registry */
2592 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2593 LTTNG_SOCK_GET_GID_CRED(creds
));
2594 if (ret
!= LTTNG_OK
) {
2599 * Get the newly created session pointer back
2601 * XXX: There is no need for the session lock list here since the caller
2602 * (process_client_msg) is holding it. We might want to change that so a
2603 * single command does not lock the entire session list.
2605 session
= session_find_by_name(name
);
2608 session
->live_timer
= live_timer
;
2609 /* Create default consumer output for the session not yet created. */
2610 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2611 if (session
->consumer
== NULL
) {
2612 ret
= LTTNG_ERR_FATAL
;
2613 goto consumer_error
;
2617 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2618 if (ret
!= LTTNG_OK
) {
2619 goto consumer_error
;
2621 session
->output_traces
= 1;
2623 session
->output_traces
= 0;
2624 DBG2("Session %s created with no output", session
->name
);
2627 session
->consumer
->enabled
= 1;
2632 session_destroy(session
);
2639 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2641 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2642 size_t nb_uri
, lttng_sock_cred
*creds
)
2645 struct ltt_session
*session
;
2646 struct snapshot_output
*new_output
= NULL
;
2652 * Create session in no output mode with URIs set to NULL. The uris we've
2653 * received are for a default snapshot output if one.
2655 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2656 if (ret
!= LTTNG_OK
) {
2660 /* Get the newly created session pointer back. This should NEVER fail. */
2661 session
= session_find_by_name(name
);
2664 /* Flag session for snapshot mode. */
2665 session
->snapshot_mode
= 1;
2667 /* Skip snapshot output creation if no URI is given. */
2672 new_output
= snapshot_output_alloc();
2674 ret
= LTTNG_ERR_NOMEM
;
2675 goto error_snapshot_alloc
;
2678 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2679 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2681 if (ret
== -ENOMEM
) {
2682 ret
= LTTNG_ERR_NOMEM
;
2684 ret
= LTTNG_ERR_INVALID
;
2686 goto error_snapshot
;
2690 snapshot_add_output(&session
->snapshot
, new_output
);
2697 snapshot_output_destroy(new_output
);
2698 error_snapshot_alloc
:
2699 session_destroy(session
);
2705 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2707 * Called with session lock held.
2709 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2712 struct ltt_ust_session
*usess
;
2713 struct ltt_kernel_session
*ksess
;
2718 usess
= session
->ust_session
;
2719 ksess
= session
->kernel_session
;
2721 /* Clean kernel session teardown */
2722 kernel_destroy_session(ksess
);
2724 /* UST session teardown */
2726 /* Close any relayd session */
2727 consumer_output_send_destroy_relayd(usess
->consumer
);
2729 /* Destroy every UST application related to this session. */
2730 ret
= ust_app_destroy_trace_all(usess
);
2732 ERR("Error in ust_app_destroy_trace_all");
2735 /* Clean up the rest. */
2736 trace_ust_destroy_session(usess
);
2740 * Must notify the kernel thread here to update it's poll set in order to
2741 * remove the channel(s)' fd just destroyed.
2743 ret
= notify_thread_pipe(wpipe
);
2745 PERROR("write kernel poll pipe");
2748 ret
= session_destroy(session
);
2754 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2756 int cmd_register_consumer(struct ltt_session
*session
,
2757 enum lttng_domain_type domain
, const char *sock_path
,
2758 struct consumer_data
*cdata
)
2761 struct consumer_socket
*socket
= NULL
;
2768 case LTTNG_DOMAIN_KERNEL
:
2770 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2774 /* Can't register a consumer if there is already one */
2775 if (ksess
->consumer_fds_sent
!= 0) {
2776 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2780 sock
= lttcomm_connect_unix_sock(sock_path
);
2782 ret
= LTTNG_ERR_CONNECT_FAIL
;
2785 cdata
->cmd_sock
= sock
;
2787 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2788 if (socket
== NULL
) {
2791 PERROR("close register consumer");
2793 cdata
->cmd_sock
= -1;
2794 ret
= LTTNG_ERR_FATAL
;
2798 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2799 if (socket
->lock
== NULL
) {
2800 PERROR("zmalloc pthread mutex");
2801 ret
= LTTNG_ERR_FATAL
;
2804 pthread_mutex_init(socket
->lock
, NULL
);
2805 socket
->registered
= 1;
2808 consumer_add_socket(socket
, ksess
->consumer
);
2811 pthread_mutex_lock(&cdata
->pid_mutex
);
2813 pthread_mutex_unlock(&cdata
->pid_mutex
);
2818 /* TODO: Userspace tracing */
2819 ret
= LTTNG_ERR_UND
;
2827 consumer_destroy_socket(socket
);
2833 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2835 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2836 struct lttng_domain
**domains
)
2841 struct lttng_ht_iter iter
;
2843 if (session
->kernel_session
!= NULL
) {
2844 DBG3("Listing domains found kernel domain");
2848 if (session
->ust_session
!= NULL
) {
2849 DBG3("Listing domains found UST global domain");
2853 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2855 if (agt
->being_used
) {
2866 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2867 if (*domains
== NULL
) {
2868 ret
= LTTNG_ERR_FATAL
;
2872 if (session
->kernel_session
!= NULL
) {
2873 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2875 /* Kernel session buffer type is always GLOBAL */
2876 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2881 if (session
->ust_session
!= NULL
) {
2882 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2883 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2887 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2889 if (agt
->being_used
) {
2890 (*domains
)[index
].type
= agt
->domain
;
2891 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2901 /* Return negative value to differentiate return code */
2907 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2909 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2910 struct ltt_session
*session
, struct lttng_channel
**channels
)
2912 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
2915 case LTTNG_DOMAIN_KERNEL
:
2916 if (session
->kernel_session
!= NULL
) {
2917 nb_chan
= session
->kernel_session
->channel_count
;
2919 DBG3("Number of kernel channels %zd", nb_chan
);
2921 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2925 case LTTNG_DOMAIN_UST
:
2926 if (session
->ust_session
!= NULL
) {
2928 nb_chan
= lttng_ht_get_count(
2929 session
->ust_session
->domain_global
.channels
);
2932 DBG3("Number of UST global channels %zd", nb_chan
);
2934 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2939 ret
= -LTTNG_ERR_UND
;
2944 const size_t channel_size
= sizeof(struct lttng_channel
) +
2945 sizeof(struct lttcomm_channel_extended
);
2946 struct lttcomm_channel_extended
*channel_exts
;
2948 payload_size
= nb_chan
* channel_size
;
2949 *channels
= zmalloc(payload_size
);
2950 if (*channels
== NULL
) {
2951 ret
= -LTTNG_ERR_FATAL
;
2955 channel_exts
= ((void *) *channels
) +
2956 (nb_chan
* sizeof(struct lttng_channel
));
2957 list_lttng_channels(domain
, session
, *channels
, channel_exts
);
2968 * Command LTTNG_LIST_EVENTS processed by the client thread.
2970 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
2971 struct ltt_session
*session
, char *channel_name
,
2972 struct lttng_event
**events
, size_t *total_size
)
2975 ssize_t nb_event
= 0;
2978 case LTTNG_DOMAIN_KERNEL
:
2979 if (session
->kernel_session
!= NULL
) {
2980 nb_event
= list_lttng_kernel_events(channel_name
,
2981 session
->kernel_session
, events
,
2985 case LTTNG_DOMAIN_UST
:
2987 if (session
->ust_session
!= NULL
) {
2988 nb_event
= list_lttng_ust_global_events(channel_name
,
2989 &session
->ust_session
->domain_global
, events
,
2994 case LTTNG_DOMAIN_LOG4J
:
2995 case LTTNG_DOMAIN_JUL
:
2996 case LTTNG_DOMAIN_PYTHON
:
2997 if (session
->ust_session
) {
2998 struct lttng_ht_iter iter
;
3002 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3003 &iter
.iter
, agt
, node
.node
) {
3004 if (agt
->domain
== domain
) {
3005 nb_event
= list_lttng_agent_events(
3015 ret
= LTTNG_ERR_UND
;
3022 /* Return negative value to differentiate return code */
3027 * Using the session list, filled a lttng_session array to send back to the
3028 * client for session listing.
3030 * The session list lock MUST be acquired before calling this function. Use
3031 * session_lock_list() and session_unlock_list().
3033 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3038 struct ltt_session
*session
;
3039 struct ltt_session_list
*list
= session_get_list();
3041 DBG("Getting all available session for UID %d GID %d",
3044 * Iterate over session list and append data after the control struct in
3047 cds_list_for_each_entry(session
, &list
->head
, list
) {
3049 * Only list the sessions the user can control.
3051 if (!session_access_ok(session
, uid
, gid
)) {
3055 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3056 struct ltt_ust_session
*usess
= session
->ust_session
;
3058 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3059 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3060 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3061 ret
= build_network_session_path(sessions
[i
].path
,
3062 sizeof(sessions
[i
].path
), session
);
3064 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3065 session
->consumer
->dst
.trace_path
);
3068 PERROR("snprintf session path");
3072 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3073 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3074 sessions
[i
].enabled
= session
->active
;
3075 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3076 sessions
[i
].live_timer_interval
= session
->live_timer
;
3082 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3083 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3085 int cmd_data_pending(struct ltt_session
*session
)
3088 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3089 struct ltt_ust_session
*usess
= session
->ust_session
;
3093 /* Session MUST be stopped to ask for data availability. */
3094 if (session
->active
) {
3095 ret
= LTTNG_ERR_SESSION_STARTED
;
3099 * If stopped, just make sure we've started before else the above call
3100 * will always send that there is data pending.
3102 * The consumer assumes that when the data pending command is received,
3103 * the trace has been started before or else no output data is written
3104 * by the streams which is a condition for data pending. So, this is
3105 * *VERY* important that we don't ask the consumer before a start
3108 if (!session
->has_been_started
) {
3114 if (ksess
&& ksess
->consumer
) {
3115 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3117 /* Data is still being extracted for the kernel. */
3122 if (usess
&& usess
->consumer
) {
3123 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3125 /* Data is still being extracted for the kernel. */
3130 /* Data is ready to be read by a viewer */
3138 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3140 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3142 int cmd_snapshot_add_output(struct ltt_session
*session
,
3143 struct lttng_snapshot_output
*output
, uint32_t *id
)
3146 struct snapshot_output
*new_output
;
3151 DBG("Cmd snapshot add output for session %s", session
->name
);
3154 * Can't create an output if the session is not set in no-output mode.
3156 if (session
->output_traces
) {
3157 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3161 /* Only one output is allowed until we have the "tee" feature. */
3162 if (session
->snapshot
.nb_output
== 1) {
3163 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3167 new_output
= snapshot_output_alloc();
3169 ret
= LTTNG_ERR_NOMEM
;
3173 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3174 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3175 &session
->snapshot
);
3177 if (ret
== -ENOMEM
) {
3178 ret
= LTTNG_ERR_NOMEM
;
3180 ret
= LTTNG_ERR_INVALID
;
3186 snapshot_add_output(&session
->snapshot
, new_output
);
3188 *id
= new_output
->id
;
3195 snapshot_output_destroy(new_output
);
3201 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3203 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3205 int cmd_snapshot_del_output(struct ltt_session
*session
,
3206 struct lttng_snapshot_output
*output
)
3209 struct snapshot_output
*sout
= NULL
;
3217 * Permission denied to create an output if the session is not
3218 * set in no output mode.
3220 if (session
->output_traces
) {
3221 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3226 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3228 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3229 } else if (*output
->name
!= '\0') {
3230 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3232 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3235 ret
= LTTNG_ERR_INVALID
;
3239 snapshot_delete_output(&session
->snapshot
, sout
);
3240 snapshot_output_destroy(sout
);
3249 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3251 * If no output is available, outputs is untouched and 0 is returned.
3253 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3255 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3256 struct lttng_snapshot_output
**outputs
)
3259 struct lttng_snapshot_output
*list
= NULL
;
3260 struct lttng_ht_iter iter
;
3261 struct snapshot_output
*output
;
3266 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3269 * Permission denied to create an output if the session is not
3270 * set in no output mode.
3272 if (session
->output_traces
) {
3273 ret
= -LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3277 if (session
->snapshot
.nb_output
== 0) {
3282 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3284 ret
= -LTTNG_ERR_NOMEM
;
3288 /* Copy list from session to the new list object. */
3290 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3291 output
, node
.node
) {
3292 assert(output
->consumer
);
3293 list
[idx
].id
= output
->id
;
3294 list
[idx
].max_size
= output
->max_size
;
3295 if (lttng_strncpy(list
[idx
].name
, output
->name
,
3296 sizeof(list
[idx
].name
))) {
3297 ret
= -LTTNG_ERR_INVALID
;
3300 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3301 if (lttng_strncpy(list
[idx
].ctrl_url
,
3302 output
->consumer
->dst
.trace_path
,
3303 sizeof(list
[idx
].ctrl_url
))) {
3304 ret
= -LTTNG_ERR_INVALID
;
3309 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3310 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3312 ret
= -LTTNG_ERR_NOMEM
;
3317 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3318 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3320 ret
= -LTTNG_ERR_NOMEM
;
3329 ret
= session
->snapshot
.nb_output
;
3338 * Check if we can regenerate the metadata for this session.
3339 * Only kernel, UST per-uid and non-live sessions are supported.
3341 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3344 int check_regenerate_metadata_support(struct ltt_session
*session
)
3350 if (session
->live_timer
!= 0) {
3351 ret
= LTTNG_ERR_LIVE_SESSION
;
3354 if (!session
->active
) {
3355 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3358 if (session
->ust_session
) {
3359 switch (session
->ust_session
->buffer_type
) {
3360 case LTTNG_BUFFER_PER_UID
:
3362 case LTTNG_BUFFER_PER_PID
:
3363 ret
= LTTNG_ERR_PER_PID_SESSION
;
3367 ret
= LTTNG_ERR_UNK
;
3371 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3372 session
->consumer
->relay_minor_version
< 8) {
3373 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3383 int clear_metadata_file(int fd
)
3387 ret
= lseek(fd
, 0, SEEK_SET
);
3393 ret
= ftruncate(fd
, 0);
3395 PERROR("ftruncate");
3404 int ust_regenerate_metadata(struct ltt_ust_session
*usess
)
3407 struct buffer_reg_uid
*uid_reg
= NULL
;
3408 struct buffer_reg_session
*session_reg
= NULL
;
3411 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3412 struct ust_registry_session
*registry
;
3413 struct ust_registry_channel
*chan
;
3414 struct lttng_ht_iter iter_chan
;
3416 session_reg
= uid_reg
->registry
;
3417 registry
= session_reg
->reg
.ust
;
3419 pthread_mutex_lock(®istry
->lock
);
3420 registry
->metadata_len_sent
= 0;
3421 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3422 registry
->metadata_len
= 0;
3423 registry
->metadata_version
++;
3424 if (registry
->metadata_fd
> 0) {
3425 /* Clear the metadata file's content. */
3426 ret
= clear_metadata_file(registry
->metadata_fd
);
3428 pthread_mutex_unlock(®istry
->lock
);
3433 ret
= ust_metadata_session_statedump(registry
, NULL
,
3434 registry
->major
, registry
->minor
);
3436 pthread_mutex_unlock(®istry
->lock
);
3437 ERR("Failed to generate session metadata (err = %d)",
3441 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3443 struct ust_registry_event
*event
;
3444 struct lttng_ht_iter iter_event
;
3446 ret
= ust_metadata_channel_statedump(registry
, chan
);
3448 pthread_mutex_unlock(®istry
->lock
);
3449 ERR("Failed to generate channel metadata "
3453 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3455 ret
= ust_metadata_event_statedump(registry
,
3458 pthread_mutex_unlock(®istry
->lock
);
3459 ERR("Failed to generate event metadata "
3465 pthread_mutex_unlock(®istry
->lock
);
3474 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
3476 * Ask the consumer to truncate the existing metadata file(s) and
3477 * then regenerate the metadata. Live and per-pid sessions are not
3478 * supported and return an error.
3480 * Return 0 on success or else a LTTNG_ERR code.
3482 int cmd_regenerate_metadata(struct ltt_session
*session
)
3488 ret
= check_regenerate_metadata_support(session
);
3493 if (session
->kernel_session
) {
3494 ret
= kernctl_session_regenerate_metadata(
3495 session
->kernel_session
->fd
);
3497 ERR("Failed to regenerate the kernel metadata");
3502 if (session
->ust_session
) {
3503 ret
= ust_regenerate_metadata(session
->ust_session
);
3505 ERR("Failed to regenerate the UST metadata");
3509 DBG("Cmd metadata regenerate for session %s", session
->name
);
3517 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
3519 * Ask the tracer to regenerate a new statedump.
3521 * Return 0 on success or else a LTTNG_ERR code.
3523 int cmd_regenerate_statedump(struct ltt_session
*session
)
3529 if (!session
->active
) {
3530 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3535 if (session
->kernel_session
) {
3536 ret
= kernctl_session_regenerate_statedump(
3537 session
->kernel_session
->fd
);
3539 * Currently, the statedump in kernel can only fail if out
3543 if (ret
== -ENOMEM
) {
3544 ret
= LTTNG_ERR_REGEN_STATEDUMP_NOMEM
;
3546 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3548 ERR("Failed to regenerate the kernel statedump");
3553 if (session
->ust_session
) {
3554 ret
= ust_app_regenerate_statedump_all(session
->ust_session
);
3556 * Currently, the statedump in UST always returns 0.
3559 ret
= LTTNG_ERR_REGEN_STATEDUMP_FAIL
;
3560 ERR("Failed to regenerate the UST statedump");
3564 DBG("Cmd regenerate statedump for session %s", session
->name
);
3571 int cmd_register_trigger(struct command_ctx
*cmd_ctx
, int sock
,
3572 struct notification_thread_data
*notification_thread
)
3576 ssize_t sock_recv_len
;
3577 char *trigger_buffer
= NULL
;
3578 struct lttng_trigger
*trigger
= NULL
;
3579 uint64_t notification_counter
= 1;
3581 trigger_len
= (size_t) cmd_ctx
->lsm
->u
.trigger
.header
.len
;
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");
3595 if (lttng_trigger_create_from_buffer(trigger_buffer
, &trigger
) !=
3597 ERR("Invalid trigger payload received in \"register trigger\" command");
3598 ret
= LTTNG_ERR_INVALID_TRIGGER
;
3602 /* Enqueue and signal. */
3603 pthread_mutex_lock(¬ification_thread
->cmd_queue
.lock
);
3604 cds_list_add(&trigger
->list_node
, ¬ification_thread
->cmd_queue
.list
);
3606 pthread_mutex_unlock(¬ification_thread
->cmd_queue
.lock
);
3607 ret
= write(notification_thread
->cmd_queue
.event_fd
,
3608 ¬ification_counter
, sizeof(notification_counter
));
3610 PERROR("write to notification thread's queue event fd");
3614 DBG("Command register trigger succeeded");
3616 free(trigger_buffer
);
3621 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3622 * snapshot output is *not* set with a remote destination.
3624 * Return 0 on success or a LTTNG_ERR code.
3626 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3627 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3630 struct lttng_ht_iter iter
;
3631 struct consumer_socket
*socket
;
3634 assert(snap_output
);
3637 DBG2("Set relayd object from snapshot output");
3639 /* Ignore if snapshot consumer output is not network. */
3640 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3645 * For each consumer socket, create and send the relayd object of the
3649 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3650 socket
, node
.node
) {
3651 ret
= send_consumer_relayd_sockets(0, session
->id
,
3652 snap_output
->consumer
, socket
,
3653 session
->name
, session
->hostname
,
3654 session
->live_timer
);
3655 if (ret
!= LTTNG_OK
) {
3667 * Record a kernel snapshot.
3669 * Return LTTNG_OK on success or a LTTNG_ERR code.
3671 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3672 struct snapshot_output
*output
, struct ltt_session
*session
,
3673 int wait
, uint64_t nb_packets_per_stream
)
3683 * Copy kernel session sockets so we can communicate with the right
3684 * consumer for the snapshot record command.
3686 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3688 ret
= LTTNG_ERR_NOMEM
;
3692 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3693 if (ret
!= LTTNG_OK
) {
3694 goto error_snapshot
;
3697 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3698 if (ret
!= LTTNG_OK
) {
3699 goto error_snapshot
;
3706 /* Clean up copied sockets so this output can use some other later on. */
3707 consumer_destroy_output_sockets(output
->consumer
);
3714 * Record a UST snapshot.
3716 * Return 0 on success or a LTTNG_ERR error code.
3718 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3719 struct snapshot_output
*output
, struct ltt_session
*session
,
3720 int wait
, uint64_t nb_packets_per_stream
)
3729 * Copy UST session sockets so we can communicate with the right
3730 * consumer for the snapshot record command.
3732 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3734 ret
= LTTNG_ERR_NOMEM
;
3738 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3739 if (ret
!= LTTNG_OK
) {
3740 goto error_snapshot
;
3743 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3747 ret
= LTTNG_ERR_INVALID
;
3750 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3753 goto error_snapshot
;
3759 /* Clean up copied sockets so this output can use some other later on. */
3760 consumer_destroy_output_sockets(output
->consumer
);
3766 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3767 uint64_t cur_nr_packets
)
3769 uint64_t tot_size
= 0;
3771 if (session
->kernel_session
) {
3772 struct ltt_kernel_channel
*chan
;
3773 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3775 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3776 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3778 * Don't take channel into account if we
3779 * already grab all its packets.
3783 tot_size
+= chan
->channel
->attr
.subbuf_size
3784 * chan
->stream_count
;
3788 if (session
->ust_session
) {
3789 struct ltt_ust_session
*usess
= session
->ust_session
;
3791 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3799 * Calculate the number of packets we can grab from each stream that
3800 * fits within the overall snapshot max size.
3802 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3803 * the number of packets per stream.
3805 * TODO: this approach is not perfect: we consider the worse case
3806 * (packet filling the sub-buffers) as an upper bound, but we could do
3807 * better if we do this calculation while we actually grab the packet
3808 * content: we would know how much padding we don't actually store into
3811 * This algorithm is currently bounded by the number of packets per
3814 * Since we call this algorithm before actually grabbing the data, it's
3815 * an approximation: for instance, applications could appear/disappear
3816 * in between this call and actually grabbing data.
3819 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3822 uint64_t cur_nb_packets
= 0;
3825 return 0; /* Infinite */
3828 size_left
= max_size
;
3830 uint64_t one_more_packet_tot_size
;
3832 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3834 if (!one_more_packet_tot_size
) {
3835 /* We are already grabbing all packets. */
3838 size_left
-= one_more_packet_tot_size
;
3839 if (size_left
< 0) {
3844 if (!cur_nb_packets
) {
3845 /* Not enough room to grab one packet of each stream, error. */
3848 return cur_nb_packets
;
3852 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3854 * The wait parameter is ignored so this call always wait for the snapshot to
3855 * complete before returning.
3857 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3859 int cmd_snapshot_record(struct ltt_session
*session
,
3860 struct lttng_snapshot_output
*output
, int wait
)
3863 unsigned int use_tmp_output
= 0;
3864 struct snapshot_output tmp_output
;
3865 unsigned int snapshot_success
= 0;
3871 DBG("Cmd snapshot record for session %s", session
->name
);
3873 /* Get the datetime for the snapshot output directory. */
3874 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
3877 ret
= LTTNG_ERR_INVALID
;
3882 * Permission denied to create an output if the session is not
3883 * set in no output mode.
3885 if (session
->output_traces
) {
3886 ret
= LTTNG_ERR_NOT_SNAPSHOT_SESSION
;
3890 /* The session needs to be started at least once. */
3891 if (!session
->has_been_started
) {
3892 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3896 /* Use temporary output for the session. */
3897 if (*output
->ctrl_url
!= '\0') {
3898 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3899 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3902 if (ret
== -ENOMEM
) {
3903 ret
= LTTNG_ERR_NOMEM
;
3905 ret
= LTTNG_ERR_INVALID
;
3909 /* Use the global session count for the temporary snapshot. */
3910 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3912 /* Use the global datetime */
3913 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3917 if (use_tmp_output
) {
3918 int64_t nb_packets_per_stream
;
3920 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3921 tmp_output
.max_size
);
3922 if (nb_packets_per_stream
< 0) {
3923 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3927 if (session
->kernel_session
) {
3928 ret
= record_kernel_snapshot(session
->kernel_session
,
3929 &tmp_output
, session
,
3930 wait
, nb_packets_per_stream
);
3931 if (ret
!= LTTNG_OK
) {
3936 if (session
->ust_session
) {
3937 ret
= record_ust_snapshot(session
->ust_session
,
3938 &tmp_output
, session
,
3939 wait
, nb_packets_per_stream
);
3940 if (ret
!= LTTNG_OK
) {
3945 snapshot_success
= 1;
3947 struct snapshot_output
*sout
;
3948 struct lttng_ht_iter iter
;
3951 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3952 &iter
.iter
, sout
, node
.node
) {
3953 int64_t nb_packets_per_stream
;
3956 * Make a local copy of the output and assign the possible
3957 * temporary value given by the caller.
3959 memset(&tmp_output
, 0, sizeof(tmp_output
));
3960 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3962 if (output
->max_size
!= (uint64_t) -1ULL) {
3963 tmp_output
.max_size
= output
->max_size
;
3966 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3967 tmp_output
.max_size
);
3968 if (nb_packets_per_stream
< 0) {
3969 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3974 /* Use temporary name. */
3975 if (*output
->name
!= '\0') {
3976 if (lttng_strncpy(tmp_output
.name
, output
->name
,
3977 sizeof(tmp_output
.name
))) {
3978 ret
= LTTNG_ERR_INVALID
;
3984 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3985 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3987 if (session
->kernel_session
) {
3988 ret
= record_kernel_snapshot(session
->kernel_session
,
3989 &tmp_output
, session
,
3990 wait
, nb_packets_per_stream
);
3991 if (ret
!= LTTNG_OK
) {
3997 if (session
->ust_session
) {
3998 ret
= record_ust_snapshot(session
->ust_session
,
3999 &tmp_output
, session
,
4000 wait
, nb_packets_per_stream
);
4001 if (ret
!= LTTNG_OK
) {
4006 snapshot_success
= 1;
4011 if (snapshot_success
) {
4012 session
->snapshot
.nb_snapshot
++;
4014 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
4022 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4024 int cmd_set_session_shm_path(struct ltt_session
*session
,
4025 const char *shm_path
)
4031 * Can only set shm path before session is started.
4033 if (session
->has_been_started
) {
4034 return LTTNG_ERR_SESSION_STARTED
;
4037 strncpy(session
->shm_path
, shm_path
,
4038 sizeof(session
->shm_path
));
4039 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
4045 * Init command subsystem.
4050 * Set network sequence index to 1 for streams to match a relayd
4051 * socket on the consumer side.
4053 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4054 relayd_net_seq_idx
= 1;
4055 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4057 DBG("Command subsystem initialized");