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>
36 #include "health-sessiond.h"
38 #include "kernel-consumer.h"
39 #include "lttng-sessiond.h"
43 #include "buffer-registry.h"
48 * Used to keep a unique index for each relayd socket created where this value
49 * is associated with streams on the consumer so it can match the right relayd
50 * to send to. It must be accessed with the relayd_net_seq_idx_lock
53 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
54 static uint64_t relayd_net_seq_idx
;
56 static int validate_event_name(const char *);
57 static int validate_ust_event_name(const char *);
58 static int cmd_enable_event_internal(struct ltt_session
*session
,
59 struct lttng_domain
*domain
,
60 char *channel_name
, struct lttng_event
*event
,
61 char *filter_expression
,
62 struct lttng_filter_bytecode
*filter
,
63 struct lttng_event_exclusion
*exclusion
,
67 * Create a session path used by list_lttng_sessions for the case that the
68 * session consumer is on the network.
70 static int build_network_session_path(char *dst
, size_t size
,
71 struct ltt_session
*session
)
73 int ret
, kdata_port
, udata_port
;
74 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
75 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
80 memset(tmp_urls
, 0, sizeof(tmp_urls
));
81 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
83 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
85 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
86 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
87 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
90 if (session
->ust_session
&& session
->ust_session
->consumer
) {
91 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
92 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
95 if (uuri
== NULL
&& kuri
== NULL
) {
96 uri
= &session
->consumer
->dst
.net
.control
;
97 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
98 } else if (kuri
&& uuri
) {
99 ret
= uri_compare(kuri
, uuri
);
103 /* Build uuri URL string */
104 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
111 } else if (kuri
&& uuri
== NULL
) {
113 } else if (uuri
&& kuri
== NULL
) {
117 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
123 * Do we have a UST url set. If yes, this means we have both kernel and UST
126 if (*tmp_uurl
!= '\0') {
127 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
128 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
131 if (kuri
|| (!kuri
&& !uuri
)) {
134 /* No kernel URI, use the UST port. */
137 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
145 * Get run-time attributes if the session has been started (discarded events,
148 static int get_kernel_runtime_stats(struct ltt_session
*session
,
149 struct ltt_kernel_channel
*kchan
, uint64_t *discarded_events
,
150 uint64_t *lost_packets
)
154 if (!session
->has_been_started
) {
156 *discarded_events
= 0;
161 ret
= consumer_get_discarded_events(session
->id
, kchan
->fd
,
162 session
->kernel_session
->consumer
,
168 ret
= consumer_get_lost_packets(session
->id
, kchan
->fd
,
169 session
->kernel_session
->consumer
,
180 * Get run-time attributes if the session has been started (discarded events,
183 static int get_ust_runtime_stats(struct ltt_session
*session
,
184 struct ltt_ust_channel
*uchan
, uint64_t *discarded_events
,
185 uint64_t *lost_packets
)
188 struct ltt_ust_session
*usess
;
190 usess
= session
->ust_session
;
192 if (!usess
|| !session
->has_been_started
) {
193 *discarded_events
= 0;
199 if (usess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
200 ret
= ust_app_uid_get_channel_runtime_stats(usess
->id
,
201 &usess
->buffer_reg_uid_list
,
202 usess
->consumer
, uchan
->id
,
203 uchan
->attr
.overwrite
,
206 } else if (usess
->buffer_type
== LTTNG_BUFFER_PER_PID
) {
207 ret
= ust_app_pid_get_channel_runtime_stats(usess
,
208 uchan
, usess
->consumer
,
209 uchan
->attr
.overwrite
,
215 *discarded_events
+= uchan
->per_pid_closed_app_discarded
;
216 *lost_packets
+= uchan
->per_pid_closed_app_lost
;
218 ERR("Unsupported buffer type");
229 * Fill lttng_channel array of all channels.
231 static void list_lttng_channels(enum lttng_domain_type domain
,
232 struct ltt_session
*session
, struct lttng_channel
*channels
,
233 struct lttcomm_channel_extended
*chan_exts
)
236 struct ltt_kernel_channel
*kchan
;
238 DBG("Listing channels for session %s", session
->name
);
241 case LTTNG_DOMAIN_KERNEL
:
242 /* Kernel channels */
243 if (session
->kernel_session
!= NULL
) {
244 cds_list_for_each_entry(kchan
,
245 &session
->kernel_session
->channel_list
.head
, list
) {
246 uint64_t discarded_events
, lost_packets
;
248 ret
= get_kernel_runtime_stats(session
, kchan
,
249 &discarded_events
, &lost_packets
);
253 /* Copy lttng_channel struct to array */
254 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
255 channels
[i
].enabled
= kchan
->enabled
;
256 chan_exts
[i
].discarded_events
=
258 chan_exts
[i
].lost_packets
= lost_packets
;
263 case LTTNG_DOMAIN_UST
:
265 struct lttng_ht_iter iter
;
266 struct ltt_ust_channel
*uchan
;
269 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
270 &iter
.iter
, uchan
, node
.node
) {
271 uint64_t discarded_events
, lost_packets
;
273 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
274 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
275 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
276 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
277 channels
[i
].attr
.switch_timer_interval
=
278 uchan
->attr
.switch_timer_interval
;
279 channels
[i
].attr
.read_timer_interval
=
280 uchan
->attr
.read_timer_interval
;
281 channels
[i
].enabled
= uchan
->enabled
;
282 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
283 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
286 * Map enum lttng_ust_output to enum lttng_event_output.
288 switch (uchan
->attr
.output
) {
290 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
294 * LTTNG_UST_MMAP is the only supported UST
301 ret
= get_ust_runtime_stats(session
, uchan
,
302 &discarded_events
, &lost_packets
);
306 chan_exts
[i
].discarded_events
= discarded_events
;
307 chan_exts
[i
].lost_packets
= lost_packets
;
321 static void increment_extended_len(const char *filter_expression
,
322 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
324 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
326 if (filter_expression
) {
327 *extended_len
+= strlen(filter_expression
) + 1;
331 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
335 static void append_extended_info(const char *filter_expression
,
336 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
338 struct lttcomm_event_extended_header extended_header
;
339 size_t filter_len
= 0;
340 size_t nb_exclusions
= 0;
342 if (filter_expression
) {
343 filter_len
= strlen(filter_expression
) + 1;
347 nb_exclusions
= exclusion
->count
;
350 /* Set header fields */
351 extended_header
.filter_len
= filter_len
;
352 extended_header
.nb_exclusions
= nb_exclusions
;
355 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
356 *extended_at
+= sizeof(extended_header
);
358 /* Copy filter string */
359 if (filter_expression
) {
360 memcpy(*extended_at
, filter_expression
, filter_len
);
361 *extended_at
+= filter_len
;
364 /* Copy exclusion names */
366 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
368 memcpy(*extended_at
, &exclusion
->names
, len
);
374 * Create a list of agent domain events.
376 * Return number of events in list on success or else a negative value.
378 static int list_lttng_agent_events(struct agent
*agt
,
379 struct lttng_event
**events
, size_t *total_size
)
382 unsigned int nb_event
= 0;
383 struct agent_event
*event
;
384 struct lttng_event
*tmp_events
;
385 struct lttng_ht_iter iter
;
386 size_t extended_len
= 0;
392 DBG3("Listing agent events");
395 nb_event
= lttng_ht_get_count(agt
->events
);
403 /* Compute required extended infos size */
404 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
407 * This is only valid because the commands which add events are
408 * processed in the same thread as the listing.
411 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
412 increment_extended_len(event
->filter_expression
, NULL
,
417 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
418 tmp_events
= zmalloc(*total_size
);
420 PERROR("zmalloc agent events session");
421 ret
= -LTTNG_ERR_FATAL
;
425 extended_at
= ((uint8_t *) tmp_events
) +
426 nb_event
* sizeof(struct lttng_event
);
429 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
430 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
431 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
432 tmp_events
[i
].enabled
= event
->enabled
;
433 tmp_events
[i
].loglevel
= event
->loglevel_value
;
434 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
437 /* Append extended info */
438 append_extended_info(event
->filter_expression
, NULL
,
443 *events
= tmp_events
;
447 assert(nb_event
== i
);
452 * Create a list of ust global domain events.
454 static int list_lttng_ust_global_events(char *channel_name
,
455 struct ltt_ust_domain_global
*ust_global
,
456 struct lttng_event
**events
, size_t *total_size
)
459 unsigned int nb_event
= 0;
460 struct lttng_ht_iter iter
;
461 struct lttng_ht_node_str
*node
;
462 struct ltt_ust_channel
*uchan
;
463 struct ltt_ust_event
*uevent
;
464 struct lttng_event
*tmp
;
465 size_t extended_len
= 0;
468 DBG("Listing UST global events for channel %s", channel_name
);
472 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
473 node
= lttng_ht_iter_get_node_str(&iter
);
475 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
479 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
481 nb_event
= lttng_ht_get_count(uchan
->events
);
488 DBG3("Listing UST global %d events", nb_event
);
490 /* Compute required extended infos size */
491 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
492 if (uevent
->internal
) {
497 increment_extended_len(uevent
->filter_expression
,
498 uevent
->exclusion
, &extended_len
);
501 /* All events are internal, skip. */
507 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
508 tmp
= zmalloc(*total_size
);
510 ret
= -LTTNG_ERR_FATAL
;
514 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
516 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
517 if (uevent
->internal
) {
518 /* This event should remain hidden from clients */
521 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
522 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
523 tmp
[i
].enabled
= uevent
->enabled
;
525 switch (uevent
->attr
.instrumentation
) {
526 case LTTNG_UST_TRACEPOINT
:
527 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
529 case LTTNG_UST_PROBE
:
530 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
532 case LTTNG_UST_FUNCTION
:
533 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
537 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
538 switch (uevent
->attr
.loglevel_type
) {
539 case LTTNG_UST_LOGLEVEL_ALL
:
540 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
542 case LTTNG_UST_LOGLEVEL_RANGE
:
543 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
545 case LTTNG_UST_LOGLEVEL_SINGLE
:
546 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
549 if (uevent
->filter
) {
552 if (uevent
->exclusion
) {
553 tmp
[i
].exclusion
= 1;
557 /* Append extended info */
558 append_extended_info(uevent
->filter_expression
,
559 uevent
->exclusion
, &extended_at
);
570 * Fill lttng_event array of all kernel events in the channel.
572 static int list_lttng_kernel_events(char *channel_name
,
573 struct ltt_kernel_session
*kernel_session
,
574 struct lttng_event
**events
, size_t *total_size
)
577 unsigned int nb_event
;
578 struct ltt_kernel_event
*event
;
579 struct ltt_kernel_channel
*kchan
;
580 size_t extended_len
= 0;
583 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
585 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
589 nb_event
= kchan
->event_count
;
591 DBG("Listing events for channel %s", kchan
->channel
->name
);
599 /* Compute required extended infos size */
600 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
601 increment_extended_len(event
->filter_expression
, NULL
,
605 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
606 *events
= zmalloc(*total_size
);
607 if (*events
== NULL
) {
608 ret
= LTTNG_ERR_FATAL
;
612 extended_at
= ((void *) *events
) +
613 nb_event
* sizeof(struct lttng_event
);
615 /* Kernel channels */
616 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
617 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
618 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
619 (*events
)[i
].enabled
= event
->enabled
;
620 (*events
)[i
].filter
=
621 (unsigned char) !!event
->filter_expression
;
623 switch (event
->event
->instrumentation
) {
624 case LTTNG_KERNEL_TRACEPOINT
:
625 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
627 case LTTNG_KERNEL_KRETPROBE
:
628 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
629 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
630 sizeof(struct lttng_kernel_kprobe
));
632 case LTTNG_KERNEL_KPROBE
:
633 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
634 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
635 sizeof(struct lttng_kernel_kprobe
));
637 case LTTNG_KERNEL_FUNCTION
:
638 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
639 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
640 sizeof(struct lttng_kernel_function
));
642 case LTTNG_KERNEL_NOOP
:
643 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
645 case LTTNG_KERNEL_SYSCALL
:
646 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
648 case LTTNG_KERNEL_ALL
:
654 /* Append extended info */
655 append_extended_info(event
->filter_expression
, NULL
,
663 /* Negate the error code to differentiate the size from an error */
668 * Add URI so the consumer output object. Set the correct path depending on the
669 * domain adding the default trace directory.
671 static int add_uri_to_consumer(struct consumer_output
*consumer
,
672 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
673 const char *session_name
)
676 const char *default_trace_dir
;
680 if (consumer
== NULL
) {
681 DBG("No consumer detected. Don't add URI. Stopping.");
682 ret
= LTTNG_ERR_NO_CONSUMER
;
687 case LTTNG_DOMAIN_KERNEL
:
688 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
690 case LTTNG_DOMAIN_UST
:
691 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
695 * This case is possible is we try to add the URI to the global tracing
696 * session consumer object which in this case there is no subdir.
698 default_trace_dir
= "";
701 switch (uri
->dtype
) {
704 DBG2("Setting network URI to consumer");
706 if (consumer
->type
== CONSUMER_DST_NET
) {
707 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
708 consumer
->dst
.net
.control_isset
) ||
709 (uri
->stype
== LTTNG_STREAM_DATA
&&
710 consumer
->dst
.net
.data_isset
)) {
711 ret
= LTTNG_ERR_URL_EXIST
;
715 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
718 consumer
->type
= CONSUMER_DST_NET
;
720 /* Set URI into consumer output object */
721 ret
= consumer_set_network_uri(consumer
, uri
);
725 } else if (ret
== 1) {
727 * URI was the same in the consumer so we do not append the subdir
728 * again so to not duplicate output dir.
734 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
735 ret
= consumer_set_subdir(consumer
, session_name
);
737 ret
= LTTNG_ERR_FATAL
;
742 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
743 /* On a new subdir, reappend the default trace dir. */
744 strncat(consumer
->subdir
, default_trace_dir
,
745 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
746 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
751 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
752 memset(consumer
->dst
.trace_path
, 0,
753 sizeof(consumer
->dst
.trace_path
));
754 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
755 sizeof(consumer
->dst
.trace_path
));
756 /* Append default trace dir */
757 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
758 sizeof(consumer
->dst
.trace_path
) -
759 strlen(consumer
->dst
.trace_path
) - 1);
760 /* Flag consumer as local. */
761 consumer
->type
= CONSUMER_DST_LOCAL
;
772 * Init tracing by creating trace directory and sending fds kernel consumer.
774 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
777 struct lttng_ht_iter iter
;
778 struct consumer_socket
*socket
;
784 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
785 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
787 pthread_mutex_lock(socket
->lock
);
788 ret
= kernel_consumer_send_session(socket
, session
);
789 pthread_mutex_unlock(socket
->lock
);
791 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
803 * Create a socket to the relayd using the URI.
805 * On success, the relayd_sock pointer is set to the created socket.
806 * Else, it's stays untouched and a lttcomm error code is returned.
808 static int create_connect_relayd(struct lttng_uri
*uri
,
809 struct lttcomm_relayd_sock
**relayd_sock
,
810 struct consumer_output
*consumer
)
813 struct lttcomm_relayd_sock
*rsock
;
815 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
816 RELAYD_VERSION_COMM_MINOR
);
818 ret
= LTTNG_ERR_FATAL
;
823 * Connect to relayd so we can proceed with a session creation. This call
824 * can possibly block for an arbitrary amount of time to set the health
825 * state to be in poll execution.
828 ret
= relayd_connect(rsock
);
831 ERR("Unable to reach lttng-relayd");
832 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
836 /* Create socket for control stream. */
837 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
838 DBG3("Creating relayd stream socket from URI");
840 /* Check relayd version */
841 ret
= relayd_version_check(rsock
);
843 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
846 consumer
->relay_major_version
= rsock
->major
;
847 consumer
->relay_minor_version
= rsock
->minor
;
848 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
849 DBG3("Creating relayd data socket from URI");
851 /* Command is not valid */
852 ERR("Relayd invalid stream type: %d", uri
->stype
);
853 ret
= LTTNG_ERR_INVALID
;
857 *relayd_sock
= rsock
;
862 /* The returned value is not useful since we are on an error path. */
863 (void) relayd_close(rsock
);
871 * Connect to the relayd using URI and send the socket to the right consumer.
873 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
874 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
875 struct consumer_output
*consumer
,
876 struct consumer_socket
*consumer_sock
,
877 char *session_name
, char *hostname
, int session_live_timer
)
880 struct lttcomm_relayd_sock
*rsock
= NULL
;
882 /* Connect to relayd and make version check if uri is the control. */
883 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
884 if (ret
!= LTTNG_OK
) {
889 /* Set the network sequence index if not set. */
890 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
891 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
893 * Increment net_seq_idx because we are about to transfer the
894 * new relayd socket to the consumer.
895 * Assign unique key so the consumer can match streams.
897 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
898 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
901 /* Send relayd socket to consumer. */
902 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
903 relayd_uri
->stype
, session_id
,
904 session_name
, hostname
, session_live_timer
);
906 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
910 /* Flag that the corresponding socket was sent. */
911 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
912 consumer_sock
->control_sock_sent
= 1;
913 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
914 consumer_sock
->data_sock_sent
= 1;
920 * Close socket which was dup on the consumer side. The session daemon does
921 * NOT keep track of the relayd socket(s) once transfer to the consumer.
925 (void) relayd_close(rsock
);
929 if (ret
!= LTTNG_OK
) {
931 * The consumer output for this session should not be used anymore
932 * since the relayd connection failed thus making any tracing or/and
933 * streaming not usable.
935 consumer
->enabled
= 0;
941 * Send both relayd sockets to a specific consumer and domain. This is a
942 * helper function to facilitate sending the information to the consumer for a
945 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
946 unsigned int session_id
, struct consumer_output
*consumer
,
947 struct consumer_socket
*sock
, char *session_name
,
948 char *hostname
, int session_live_timer
)
955 /* Sending control relayd socket. */
956 if (!sock
->control_sock_sent
) {
957 ret
= send_consumer_relayd_socket(domain
, session_id
,
958 &consumer
->dst
.net
.control
, consumer
, sock
,
959 session_name
, hostname
, session_live_timer
);
960 if (ret
!= LTTNG_OK
) {
965 /* Sending data relayd socket. */
966 if (!sock
->data_sock_sent
) {
967 ret
= send_consumer_relayd_socket(domain
, session_id
,
968 &consumer
->dst
.net
.data
, consumer
, sock
,
969 session_name
, hostname
, session_live_timer
);
970 if (ret
!= LTTNG_OK
) {
980 * Setup relayd connections for a tracing session. First creates the socket to
981 * the relayd and send them to the right domain consumer. Consumer type MUST be
984 int cmd_setup_relayd(struct ltt_session
*session
)
987 struct ltt_ust_session
*usess
;
988 struct ltt_kernel_session
*ksess
;
989 struct consumer_socket
*socket
;
990 struct lttng_ht_iter iter
;
994 usess
= session
->ust_session
;
995 ksess
= session
->kernel_session
;
997 DBG("Setting relayd for session %s", session
->name
);
1001 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1002 && usess
->consumer
->enabled
) {
1003 /* For each consumer socket, send relayd sockets */
1004 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1005 socket
, node
.node
) {
1006 pthread_mutex_lock(socket
->lock
);
1007 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1008 usess
->consumer
, socket
,
1009 session
->name
, session
->hostname
,
1010 session
->live_timer
);
1011 pthread_mutex_unlock(socket
->lock
);
1012 if (ret
!= LTTNG_OK
) {
1015 /* Session is now ready for network streaming. */
1016 session
->net_handle
= 1;
1018 session
->consumer
->relay_major_version
=
1019 usess
->consumer
->relay_major_version
;
1020 session
->consumer
->relay_minor_version
=
1021 usess
->consumer
->relay_minor_version
;
1024 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1025 && ksess
->consumer
->enabled
) {
1026 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1027 socket
, node
.node
) {
1028 pthread_mutex_lock(socket
->lock
);
1029 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1030 ksess
->consumer
, socket
,
1031 session
->name
, session
->hostname
,
1032 session
->live_timer
);
1033 pthread_mutex_unlock(socket
->lock
);
1034 if (ret
!= LTTNG_OK
) {
1037 /* Session is now ready for network streaming. */
1038 session
->net_handle
= 1;
1040 session
->consumer
->relay_major_version
=
1041 ksess
->consumer
->relay_major_version
;
1042 session
->consumer
->relay_minor_version
=
1043 ksess
->consumer
->relay_minor_version
;
1052 * Start a kernel session by opening all necessary streams.
1054 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1057 struct ltt_kernel_channel
*kchan
;
1059 /* Open kernel metadata */
1060 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1061 ret
= kernel_open_metadata(ksess
);
1063 ret
= LTTNG_ERR_KERN_META_FAIL
;
1068 /* Open kernel metadata stream */
1069 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1070 ret
= kernel_open_metadata_stream(ksess
);
1072 ERR("Kernel create metadata stream failed");
1073 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1078 /* For each channel */
1079 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1080 if (kchan
->stream_count
== 0) {
1081 ret
= kernel_open_channel_stream(kchan
);
1083 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1086 /* Update the stream global counter */
1087 ksess
->stream_count_global
+= ret
;
1091 /* Setup kernel consumer socket and send fds to it */
1092 ret
= init_kernel_tracing(ksess
);
1094 ret
= LTTNG_ERR_KERN_START_FAIL
;
1098 /* This start the kernel tracing */
1099 ret
= kernel_start_session(ksess
);
1101 ret
= LTTNG_ERR_KERN_START_FAIL
;
1105 /* Quiescent wait after starting trace */
1106 kernel_wait_quiescent(kernel_tracer_fd
);
1117 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1119 int cmd_disable_channel(struct ltt_session
*session
,
1120 enum lttng_domain_type domain
, char *channel_name
)
1123 struct ltt_ust_session
*usess
;
1125 usess
= session
->ust_session
;
1130 case LTTNG_DOMAIN_KERNEL
:
1132 ret
= channel_kernel_disable(session
->kernel_session
,
1134 if (ret
!= LTTNG_OK
) {
1138 kernel_wait_quiescent(kernel_tracer_fd
);
1141 case LTTNG_DOMAIN_UST
:
1143 struct ltt_ust_channel
*uchan
;
1144 struct lttng_ht
*chan_ht
;
1146 chan_ht
= usess
->domain_global
.channels
;
1148 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1149 if (uchan
== NULL
) {
1150 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1154 ret
= channel_ust_disable(usess
, uchan
);
1155 if (ret
!= LTTNG_OK
) {
1161 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1173 * Command LTTNG_TRACK_PID processed by the client thread.
1175 * Called with session lock held.
1177 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1185 case LTTNG_DOMAIN_KERNEL
:
1187 struct ltt_kernel_session
*ksess
;
1189 ksess
= session
->kernel_session
;
1191 ret
= kernel_track_pid(ksess
, pid
);
1192 if (ret
!= LTTNG_OK
) {
1196 kernel_wait_quiescent(kernel_tracer_fd
);
1199 case LTTNG_DOMAIN_UST
:
1201 struct ltt_ust_session
*usess
;
1203 usess
= session
->ust_session
;
1205 ret
= trace_ust_track_pid(usess
, pid
);
1206 if (ret
!= LTTNG_OK
) {
1212 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1224 * Command LTTNG_UNTRACK_PID processed by the client thread.
1226 * Called with session lock held.
1228 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1236 case LTTNG_DOMAIN_KERNEL
:
1238 struct ltt_kernel_session
*ksess
;
1240 ksess
= session
->kernel_session
;
1242 ret
= kernel_untrack_pid(ksess
, pid
);
1243 if (ret
!= LTTNG_OK
) {
1247 kernel_wait_quiescent(kernel_tracer_fd
);
1250 case LTTNG_DOMAIN_UST
:
1252 struct ltt_ust_session
*usess
;
1254 usess
= session
->ust_session
;
1256 ret
= trace_ust_untrack_pid(usess
, pid
);
1257 if (ret
!= LTTNG_OK
) {
1263 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1275 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1277 * The wpipe arguments is used as a notifier for the kernel thread.
1279 int cmd_enable_channel(struct ltt_session
*session
,
1280 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1283 struct ltt_ust_session
*usess
= session
->ust_session
;
1284 struct lttng_ht
*chan_ht
;
1291 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1293 /* Validate channel name */
1294 if (attr
->name
[0] == '.' ||
1295 memchr(attr
->name
, '/', len
) != NULL
) {
1296 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1300 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1305 * Don't try to enable a channel if the session has been started at
1306 * some point in time before. The tracer does not allow it.
1308 if (session
->has_been_started
) {
1309 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1314 * If the session is a live session, remove the switch timer, the
1315 * live timer does the same thing but sends also synchronisation
1316 * beacons for inactive streams.
1318 if (session
->live_timer
> 0) {
1319 attr
->attr
.live_timer_interval
= session
->live_timer
;
1320 attr
->attr
.switch_timer_interval
= 0;
1324 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1325 * mode and with less than 2 subbuf so block it right away and send back an
1326 * invalid attribute error.
1328 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
1329 ret
= LTTNG_ERR_INVALID
;
1333 switch (domain
->type
) {
1334 case LTTNG_DOMAIN_KERNEL
:
1336 struct ltt_kernel_channel
*kchan
;
1338 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1339 session
->kernel_session
);
1340 if (kchan
== NULL
) {
1341 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1342 if (attr
->name
[0] != '\0') {
1343 session
->kernel_session
->has_non_default_channel
= 1;
1346 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1349 if (ret
!= LTTNG_OK
) {
1353 kernel_wait_quiescent(kernel_tracer_fd
);
1356 case LTTNG_DOMAIN_UST
:
1357 case LTTNG_DOMAIN_JUL
:
1358 case LTTNG_DOMAIN_LOG4J
:
1359 case LTTNG_DOMAIN_PYTHON
:
1361 struct ltt_ust_channel
*uchan
;
1366 * Current agent implementation limitations force us to allow
1367 * only one channel at once in "agent" subdomains. Each
1368 * subdomain has a default channel name which must be strictly
1371 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1372 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1373 LTTNG_SYMBOL_NAME_LEN
)) {
1374 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1377 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1378 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1379 LTTNG_SYMBOL_NAME_LEN
)) {
1380 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1383 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1384 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1385 LTTNG_SYMBOL_NAME_LEN
)) {
1386 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1391 chan_ht
= usess
->domain_global
.channels
;
1393 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1394 if (uchan
== NULL
) {
1395 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1396 if (attr
->name
[0] != '\0') {
1397 usess
->has_non_default_channel
= 1;
1400 ret
= channel_ust_enable(usess
, uchan
);
1405 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1416 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1418 int cmd_disable_event(struct ltt_session
*session
,
1419 enum lttng_domain_type domain
, char *channel_name
,
1420 struct lttng_event
*event
)
1425 DBG("Disable event command for event \'%s\'", event
->name
);
1427 event_name
= event
->name
;
1428 if (validate_event_name(event_name
)) {
1429 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1433 /* Error out on unhandled search criteria */
1434 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1435 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1436 ret
= LTTNG_ERR_UNK
;
1443 case LTTNG_DOMAIN_KERNEL
:
1445 struct ltt_kernel_channel
*kchan
;
1446 struct ltt_kernel_session
*ksess
;
1448 ksess
= session
->kernel_session
;
1451 * If a non-default channel has been created in the
1452 * session, explicitely require that -c chan_name needs
1455 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1456 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1460 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1461 if (kchan
== NULL
) {
1462 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1466 switch (event
->type
) {
1467 case LTTNG_EVENT_ALL
:
1468 case LTTNG_EVENT_TRACEPOINT
:
1469 case LTTNG_EVENT_SYSCALL
:
1470 case LTTNG_EVENT_PROBE
:
1471 case LTTNG_EVENT_FUNCTION
:
1472 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1473 if (event_name
[0] == '\0') {
1474 ret
= event_kernel_disable_event(kchan
,
1477 ret
= event_kernel_disable_event(kchan
,
1478 event_name
, event
->type
);
1480 if (ret
!= LTTNG_OK
) {
1485 ret
= LTTNG_ERR_UNK
;
1489 kernel_wait_quiescent(kernel_tracer_fd
);
1492 case LTTNG_DOMAIN_UST
:
1494 struct ltt_ust_channel
*uchan
;
1495 struct ltt_ust_session
*usess
;
1497 usess
= session
->ust_session
;
1499 if (validate_ust_event_name(event_name
)) {
1500 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1505 * If a non-default channel has been created in the
1506 * session, explicitly require that -c chan_name needs
1509 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1510 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1514 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1516 if (uchan
== NULL
) {
1517 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1521 switch (event
->type
) {
1522 case LTTNG_EVENT_ALL
:
1524 * An empty event name means that everything
1525 * should be disabled.
1527 if (event
->name
[0] == '\0') {
1528 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1530 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1533 if (ret
!= LTTNG_OK
) {
1538 ret
= LTTNG_ERR_UNK
;
1542 DBG3("Disable UST event %s in channel %s completed", event_name
,
1546 case LTTNG_DOMAIN_LOG4J
:
1547 case LTTNG_DOMAIN_JUL
:
1548 case LTTNG_DOMAIN_PYTHON
:
1551 struct ltt_ust_session
*usess
= session
->ust_session
;
1555 switch (event
->type
) {
1556 case LTTNG_EVENT_ALL
:
1559 ret
= LTTNG_ERR_UNK
;
1563 agt
= trace_ust_find_agent(usess
, domain
);
1565 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1569 * An empty event name means that everything
1570 * should be disabled.
1572 if (event
->name
[0] == '\0') {
1573 ret
= event_agent_disable_all(usess
, agt
);
1575 ret
= event_agent_disable(usess
, agt
, event_name
);
1577 if (ret
!= LTTNG_OK
) {
1584 ret
= LTTNG_ERR_UND
;
1597 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1599 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1600 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1602 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1603 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1605 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1606 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1607 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1611 case LTTNG_DOMAIN_KERNEL
:
1612 assert(session
->kernel_session
);
1614 if (session
->kernel_session
->channel_count
== 0) {
1615 /* Create default channel */
1616 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1617 if (ret
!= LTTNG_OK
) {
1620 chan_kern_created
= 1;
1622 /* Add kernel context to kernel tracer */
1623 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1624 if (ret
!= LTTNG_OK
) {
1628 case LTTNG_DOMAIN_JUL
:
1629 case LTTNG_DOMAIN_LOG4J
:
1632 * Validate channel name.
1633 * If no channel name is given and the domain is JUL or LOG4J,
1634 * set it to the appropriate domain-specific channel name. If
1635 * a name is provided but does not match the expexted channel
1636 * name, return an error.
1638 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1639 strcmp(channel_name
,
1640 DEFAULT_JUL_CHANNEL_NAME
)) {
1641 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1643 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1644 strcmp(channel_name
,
1645 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1646 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1649 /* break is _not_ missing here. */
1651 case LTTNG_DOMAIN_UST
:
1653 struct ltt_ust_session
*usess
= session
->ust_session
;
1654 unsigned int chan_count
;
1658 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1659 if (chan_count
== 0) {
1660 struct lttng_channel
*attr
;
1661 /* Create default channel */
1662 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1664 ret
= LTTNG_ERR_FATAL
;
1668 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1669 if (ret
!= LTTNG_OK
) {
1674 chan_ust_created
= 1;
1677 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1678 free(app_ctx_provider_name
);
1680 app_ctx_name
= NULL
;
1681 app_ctx_provider_name
= NULL
;
1682 if (ret
!= LTTNG_OK
) {
1688 ret
= LTTNG_ERR_UND
;
1696 if (chan_kern_created
) {
1697 struct ltt_kernel_channel
*kchan
=
1698 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1699 session
->kernel_session
);
1700 /* Created previously, this should NOT fail. */
1702 kernel_destroy_channel(kchan
);
1705 if (chan_ust_created
) {
1706 struct ltt_ust_channel
*uchan
=
1707 trace_ust_find_channel_by_name(
1708 session
->ust_session
->domain_global
.channels
,
1709 DEFAULT_CHANNEL_NAME
);
1710 /* Created previously, this should NOT fail. */
1712 /* Remove from the channel list of the session. */
1713 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1715 trace_ust_destroy_channel(uchan
);
1718 free(app_ctx_provider_name
);
1723 static int validate_event_name(const char *name
)
1726 const char *c
= name
;
1727 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1728 bool null_terminated
= false;
1731 * Make sure that unescaped wildcards are only used as the last
1732 * character of the event name.
1734 while (c
< event_name_end
) {
1737 null_terminated
= true;
1743 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1744 /* Wildcard is not the last character */
1745 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1754 if (!ret
&& !null_terminated
) {
1755 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1760 static inline bool name_starts_with(const char *name
, const char *prefix
)
1762 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1764 return !strncmp(name
, prefix
, max_cmp_len
);
1767 /* Perform userspace-specific event name validation */
1768 static int validate_ust_event_name(const char *name
)
1778 * Check name against all internal UST event component namespaces used
1781 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1782 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1783 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1792 * Internal version of cmd_enable_event() with a supplemental
1793 * "internal_event" flag which is used to enable internal events which should
1794 * be hidden from clients. Such events are used in the agent implementation to
1795 * enable the events through which all "agent" events are funeled.
1797 static int _cmd_enable_event(struct ltt_session
*session
,
1798 struct lttng_domain
*domain
,
1799 char *channel_name
, struct lttng_event
*event
,
1800 char *filter_expression
,
1801 struct lttng_filter_bytecode
*filter
,
1802 struct lttng_event_exclusion
*exclusion
,
1803 int wpipe
, bool internal_event
)
1805 int ret
, channel_created
= 0;
1806 struct lttng_channel
*attr
;
1810 assert(channel_name
);
1812 /* If we have a filter, we must have its filter expression */
1813 assert(!(!!filter_expression
^ !!filter
));
1815 DBG("Enable event command for event \'%s\'", event
->name
);
1819 ret
= validate_event_name(event
->name
);
1824 switch (domain
->type
) {
1825 case LTTNG_DOMAIN_KERNEL
:
1827 struct ltt_kernel_channel
*kchan
;
1830 * If a non-default channel has been created in the
1831 * session, explicitely require that -c chan_name needs
1834 if (session
->kernel_session
->has_non_default_channel
1835 && channel_name
[0] == '\0') {
1836 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1840 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1841 session
->kernel_session
);
1842 if (kchan
== NULL
) {
1843 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1844 LTTNG_BUFFER_GLOBAL
);
1846 ret
= LTTNG_ERR_FATAL
;
1849 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1851 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1852 if (ret
!= LTTNG_OK
) {
1858 channel_created
= 1;
1861 /* Get the newly created kernel channel pointer */
1862 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1863 session
->kernel_session
);
1864 if (kchan
== NULL
) {
1865 /* This sould not happen... */
1866 ret
= LTTNG_ERR_FATAL
;
1870 switch (event
->type
) {
1871 case LTTNG_EVENT_ALL
:
1873 char *filter_expression_a
= NULL
;
1874 struct lttng_filter_bytecode
*filter_a
= NULL
;
1877 * We need to duplicate filter_expression and filter,
1878 * because ownership is passed to first enable
1881 if (filter_expression
) {
1882 filter_expression_a
= strdup(filter_expression
);
1883 if (!filter_expression_a
) {
1884 ret
= LTTNG_ERR_FATAL
;
1889 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1891 free(filter_expression_a
);
1892 ret
= LTTNG_ERR_FATAL
;
1895 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1897 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1898 ret
= event_kernel_enable_event(kchan
, event
,
1899 filter_expression
, filter
);
1900 /* We have passed ownership */
1901 filter_expression
= NULL
;
1903 if (ret
!= LTTNG_OK
) {
1904 if (channel_created
) {
1905 /* Let's not leak a useless channel. */
1906 kernel_destroy_channel(kchan
);
1908 free(filter_expression_a
);
1912 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1913 ret
= event_kernel_enable_event(kchan
, event
,
1914 filter_expression_a
, filter_a
);
1915 /* We have passed ownership */
1916 filter_expression_a
= NULL
;
1918 if (ret
!= LTTNG_OK
) {
1923 case LTTNG_EVENT_PROBE
:
1924 case LTTNG_EVENT_FUNCTION
:
1925 case LTTNG_EVENT_FUNCTION_ENTRY
:
1926 case LTTNG_EVENT_TRACEPOINT
:
1927 ret
= event_kernel_enable_event(kchan
, event
,
1928 filter_expression
, filter
);
1929 /* We have passed ownership */
1930 filter_expression
= NULL
;
1932 if (ret
!= LTTNG_OK
) {
1933 if (channel_created
) {
1934 /* Let's not leak a useless channel. */
1935 kernel_destroy_channel(kchan
);
1940 case LTTNG_EVENT_SYSCALL
:
1941 ret
= event_kernel_enable_event(kchan
, event
,
1942 filter_expression
, filter
);
1943 /* We have passed ownership */
1944 filter_expression
= NULL
;
1946 if (ret
!= LTTNG_OK
) {
1951 ret
= LTTNG_ERR_UNK
;
1955 kernel_wait_quiescent(kernel_tracer_fd
);
1958 case LTTNG_DOMAIN_UST
:
1960 struct ltt_ust_channel
*uchan
;
1961 struct ltt_ust_session
*usess
= session
->ust_session
;
1966 * If a non-default channel has been created in the
1967 * session, explicitely require that -c chan_name needs
1970 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1971 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1975 /* Get channel from global UST domain */
1976 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1978 if (uchan
== NULL
) {
1979 /* Create default channel */
1980 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1981 usess
->buffer_type
);
1983 ret
= LTTNG_ERR_FATAL
;
1986 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1988 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1989 if (ret
!= LTTNG_OK
) {
1995 /* Get the newly created channel reference back */
1996 uchan
= trace_ust_find_channel_by_name(
1997 usess
->domain_global
.channels
, channel_name
);
2001 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2003 * Don't allow users to add UST events to channels which
2004 * are assigned to a userspace subdomain (JUL, Log4J,
2007 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2011 if (!internal_event
) {
2013 * Ensure the event name is not reserved for internal
2016 ret
= validate_ust_event_name(event
->name
);
2018 WARN("Userspace event name %s failed validation.",
2020 event
->name
: "NULL");
2021 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2026 /* At this point, the session and channel exist on the tracer */
2027 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2028 filter_expression
, filter
, exclusion
,
2030 /* We have passed ownership */
2031 filter_expression
= NULL
;
2034 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2035 goto already_enabled
;
2036 } else if (ret
!= LTTNG_OK
) {
2041 case LTTNG_DOMAIN_LOG4J
:
2042 case LTTNG_DOMAIN_JUL
:
2043 case LTTNG_DOMAIN_PYTHON
:
2045 const char *default_event_name
, *default_chan_name
;
2047 struct lttng_event uevent
;
2048 struct lttng_domain tmp_dom
;
2049 struct ltt_ust_session
*usess
= session
->ust_session
;
2053 agt
= trace_ust_find_agent(usess
, domain
->type
);
2055 agt
= agent_create(domain
->type
);
2057 ret
= LTTNG_ERR_NOMEM
;
2060 agent_add(agt
, usess
->agents
);
2063 /* Create the default tracepoint. */
2064 memset(&uevent
, 0, sizeof(uevent
));
2065 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2066 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2067 default_event_name
= event_get_default_agent_ust_name(
2069 if (!default_event_name
) {
2070 ret
= LTTNG_ERR_FATAL
;
2073 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2074 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2077 * The domain type is changed because we are about to enable the
2078 * default channel and event for the JUL domain that are hardcoded.
2079 * This happens in the UST domain.
2081 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2082 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2084 switch (domain
->type
) {
2085 case LTTNG_DOMAIN_LOG4J
:
2086 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2088 case LTTNG_DOMAIN_JUL
:
2089 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2091 case LTTNG_DOMAIN_PYTHON
:
2092 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2095 /* The switch/case we are in makes this impossible */
2100 char *filter_expression_copy
= NULL
;
2101 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2104 const size_t filter_size
= sizeof(
2105 struct lttng_filter_bytecode
)
2108 filter_copy
= zmalloc(filter_size
);
2110 ret
= LTTNG_ERR_NOMEM
;
2113 memcpy(filter_copy
, filter
, filter_size
);
2115 filter_expression_copy
=
2116 strdup(filter_expression
);
2117 if (!filter_expression
) {
2118 ret
= LTTNG_ERR_NOMEM
;
2121 if (!filter_expression_copy
|| !filter_copy
) {
2122 free(filter_expression_copy
);
2128 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2129 (char *) default_chan_name
,
2130 &uevent
, filter_expression_copy
,
2131 filter_copy
, NULL
, wpipe
);
2134 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2135 goto already_enabled
;
2136 } else if (ret
!= LTTNG_OK
) {
2140 /* The wild card * means that everything should be enabled. */
2141 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2142 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2145 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2149 filter_expression
= NULL
;
2150 if (ret
!= LTTNG_OK
) {
2157 ret
= LTTNG_ERR_UND
;
2165 free(filter_expression
);
2173 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2174 * We own filter, exclusion, and filter_expression.
2176 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2177 char *channel_name
, struct lttng_event
*event
,
2178 char *filter_expression
,
2179 struct lttng_filter_bytecode
*filter
,
2180 struct lttng_event_exclusion
*exclusion
,
2183 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2184 filter_expression
, filter
, exclusion
, wpipe
, false);
2188 * Enable an event which is internal to LTTng. An internal should
2189 * never be made visible to clients and are immune to checks such as
2192 static int cmd_enable_event_internal(struct ltt_session
*session
,
2193 struct lttng_domain
*domain
,
2194 char *channel_name
, struct lttng_event
*event
,
2195 char *filter_expression
,
2196 struct lttng_filter_bytecode
*filter
,
2197 struct lttng_event_exclusion
*exclusion
,
2200 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2201 filter_expression
, filter
, exclusion
, wpipe
, true);
2205 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2207 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2208 struct lttng_event
**events
)
2211 ssize_t nb_events
= 0;
2214 case LTTNG_DOMAIN_KERNEL
:
2215 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2216 if (nb_events
< 0) {
2217 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2221 case LTTNG_DOMAIN_UST
:
2222 nb_events
= ust_app_list_events(events
);
2223 if (nb_events
< 0) {
2224 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2228 case LTTNG_DOMAIN_LOG4J
:
2229 case LTTNG_DOMAIN_JUL
:
2230 case LTTNG_DOMAIN_PYTHON
:
2231 nb_events
= agent_list_events(events
, domain
);
2232 if (nb_events
< 0) {
2233 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2238 ret
= LTTNG_ERR_UND
;
2245 /* Return negative value to differentiate return code */
2250 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2252 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2253 struct lttng_event_field
**fields
)
2256 ssize_t nb_fields
= 0;
2259 case LTTNG_DOMAIN_UST
:
2260 nb_fields
= ust_app_list_event_fields(fields
);
2261 if (nb_fields
< 0) {
2262 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2266 case LTTNG_DOMAIN_KERNEL
:
2267 default: /* fall-through */
2268 ret
= LTTNG_ERR_UND
;
2275 /* Return negative value to differentiate return code */
2279 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2281 return syscall_table_list(events
);
2285 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2287 * Called with session lock held.
2289 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2290 enum lttng_domain_type domain
, int32_t **pids
)
2293 ssize_t nr_pids
= 0;
2296 case LTTNG_DOMAIN_KERNEL
:
2298 struct ltt_kernel_session
*ksess
;
2300 ksess
= session
->kernel_session
;
2301 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2303 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2308 case LTTNG_DOMAIN_UST
:
2310 struct ltt_ust_session
*usess
;
2312 usess
= session
->ust_session
;
2313 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2315 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2320 case LTTNG_DOMAIN_LOG4J
:
2321 case LTTNG_DOMAIN_JUL
:
2322 case LTTNG_DOMAIN_PYTHON
:
2324 ret
= LTTNG_ERR_UND
;
2331 /* Return negative value to differentiate return code */
2336 * Command LTTNG_START_TRACE processed by the client thread.
2338 * Called with session mutex held.
2340 int cmd_start_trace(struct ltt_session
*session
)
2343 unsigned long nb_chan
= 0;
2344 struct ltt_kernel_session
*ksession
;
2345 struct ltt_ust_session
*usess
;
2349 /* Ease our life a bit ;) */
2350 ksession
= session
->kernel_session
;
2351 usess
= session
->ust_session
;
2353 /* Is the session already started? */
2354 if (session
->active
) {
2355 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2360 * Starting a session without channel is useless since after that it's not
2361 * possible to enable channel thus inform the client.
2363 if (usess
&& usess
->domain_global
.channels
) {
2364 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2367 nb_chan
+= ksession
->channel_count
;
2370 ret
= LTTNG_ERR_NO_CHANNEL
;
2374 /* Kernel tracing */
2375 if (ksession
!= NULL
) {
2376 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2377 if (ret
!= LTTNG_OK
) {
2382 /* Flag session that trace should start automatically */
2385 * Even though the start trace might fail, flag this session active so
2386 * other application coming in are started by default.
2390 ret
= ust_app_start_trace_all(usess
);
2392 ret
= LTTNG_ERR_UST_START_FAIL
;
2397 /* Flag this after a successful start. */
2398 session
->has_been_started
= 1;
2399 session
->active
= 1;
2408 * Command LTTNG_STOP_TRACE processed by the client thread.
2410 int cmd_stop_trace(struct ltt_session
*session
)
2413 struct ltt_kernel_channel
*kchan
;
2414 struct ltt_kernel_session
*ksession
;
2415 struct ltt_ust_session
*usess
;
2420 ksession
= session
->kernel_session
;
2421 usess
= session
->ust_session
;
2423 /* Session is not active. Skip everythong and inform the client. */
2424 if (!session
->active
) {
2425 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2430 if (ksession
&& ksession
->active
) {
2431 DBG("Stop kernel tracing");
2433 /* Flush metadata if exist */
2434 if (ksession
->metadata_stream_fd
>= 0) {
2435 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2437 ERR("Kernel metadata flush failed");
2441 /* Flush all buffers before stopping */
2442 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2443 ret
= kernel_flush_buffer(kchan
);
2445 ERR("Kernel flush buffer error");
2449 ret
= kernel_stop_session(ksession
);
2451 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2455 kernel_wait_quiescent(kernel_tracer_fd
);
2457 ksession
->active
= 0;
2460 if (usess
&& usess
->active
) {
2462 * Even though the stop trace might fail, flag this session inactive so
2463 * other application coming in are not started by default.
2467 ret
= ust_app_stop_trace_all(usess
);
2469 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2474 /* Flag inactive after a successful stop. */
2475 session
->active
= 0;
2483 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2485 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2486 struct lttng_uri
*uris
)
2489 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2490 struct ltt_ust_session
*usess
= session
->ust_session
;
2496 /* Can't set consumer URI if the session is active. */
2497 if (session
->active
) {
2498 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2502 /* Set the "global" consumer URIs */
2503 for (i
= 0; i
< nb_uri
; i
++) {
2504 ret
= add_uri_to_consumer(session
->consumer
,
2505 &uris
[i
], 0, session
->name
);
2506 if (ret
!= LTTNG_OK
) {
2511 /* Set UST session URIs */
2512 if (session
->ust_session
) {
2513 for (i
= 0; i
< nb_uri
; i
++) {
2514 ret
= add_uri_to_consumer(
2515 session
->ust_session
->consumer
,
2516 &uris
[i
], LTTNG_DOMAIN_UST
,
2518 if (ret
!= LTTNG_OK
) {
2524 /* Set kernel session URIs */
2525 if (session
->kernel_session
) {
2526 for (i
= 0; i
< nb_uri
; i
++) {
2527 ret
= add_uri_to_consumer(
2528 session
->kernel_session
->consumer
,
2529 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2531 if (ret
!= LTTNG_OK
) {
2538 * Make sure to set the session in output mode after we set URI since a
2539 * session can be created without URL (thus flagged in no output mode).
2541 session
->output_traces
= 1;
2543 ksess
->output_traces
= 1;
2547 usess
->output_traces
= 1;
2558 * Command LTTNG_CREATE_SESSION processed by the client thread.
2560 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2561 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2564 struct ltt_session
*session
;
2570 * Verify if the session already exist
2572 * XXX: There is no need for the session lock list here since the caller
2573 * (process_client_msg) is holding it. We might want to change that so a
2574 * single command does not lock the entire session list.
2576 session
= session_find_by_name(name
);
2577 if (session
!= NULL
) {
2578 ret
= LTTNG_ERR_EXIST_SESS
;
2582 /* Create tracing session in the registry */
2583 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2584 LTTNG_SOCK_GET_GID_CRED(creds
));
2585 if (ret
!= LTTNG_OK
) {
2590 * Get the newly created session pointer back
2592 * XXX: There is no need for the session lock list here since the caller
2593 * (process_client_msg) is holding it. We might want to change that so a
2594 * single command does not lock the entire session list.
2596 session
= session_find_by_name(name
);
2599 session
->live_timer
= live_timer
;
2600 /* Create default consumer output for the session not yet created. */
2601 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2602 if (session
->consumer
== NULL
) {
2603 ret
= LTTNG_ERR_FATAL
;
2604 goto consumer_error
;
2608 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2609 if (ret
!= LTTNG_OK
) {
2610 goto consumer_error
;
2612 session
->output_traces
= 1;
2614 session
->output_traces
= 0;
2615 DBG2("Session %s created with no output", session
->name
);
2618 session
->consumer
->enabled
= 1;
2623 session_destroy(session
);
2630 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2632 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2633 size_t nb_uri
, lttng_sock_cred
*creds
)
2636 struct ltt_session
*session
;
2637 struct snapshot_output
*new_output
= NULL
;
2643 * Create session in no output mode with URIs set to NULL. The uris we've
2644 * received are for a default snapshot output if one.
2646 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2647 if (ret
!= LTTNG_OK
) {
2651 /* Get the newly created session pointer back. This should NEVER fail. */
2652 session
= session_find_by_name(name
);
2655 /* Flag session for snapshot mode. */
2656 session
->snapshot_mode
= 1;
2658 /* Skip snapshot output creation if no URI is given. */
2663 new_output
= snapshot_output_alloc();
2665 ret
= LTTNG_ERR_NOMEM
;
2666 goto error_snapshot_alloc
;
2669 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2670 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2672 if (ret
== -ENOMEM
) {
2673 ret
= LTTNG_ERR_NOMEM
;
2675 ret
= LTTNG_ERR_INVALID
;
2677 goto error_snapshot
;
2681 snapshot_add_output(&session
->snapshot
, new_output
);
2688 snapshot_output_destroy(new_output
);
2689 error_snapshot_alloc
:
2690 session_destroy(session
);
2696 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2698 * Called with session lock held.
2700 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2703 struct ltt_ust_session
*usess
;
2704 struct ltt_kernel_session
*ksess
;
2709 usess
= session
->ust_session
;
2710 ksess
= session
->kernel_session
;
2712 /* Clean kernel session teardown */
2713 kernel_destroy_session(ksess
);
2715 /* UST session teardown */
2717 /* Close any relayd session */
2718 consumer_output_send_destroy_relayd(usess
->consumer
);
2720 /* Destroy every UST application related to this session. */
2721 ret
= ust_app_destroy_trace_all(usess
);
2723 ERR("Error in ust_app_destroy_trace_all");
2726 /* Clean up the rest. */
2727 trace_ust_destroy_session(usess
);
2731 * Must notify the kernel thread here to update it's poll set in order to
2732 * remove the channel(s)' fd just destroyed.
2734 ret
= notify_thread_pipe(wpipe
);
2736 PERROR("write kernel poll pipe");
2739 ret
= session_destroy(session
);
2745 * Command LTTNG_CALIBRATE processed by the client thread.
2747 int cmd_calibrate(enum lttng_domain_type domain
,
2748 struct lttng_calibrate
*calibrate
)
2753 case LTTNG_DOMAIN_KERNEL
:
2755 struct lttng_kernel_calibrate kcalibrate
;
2757 switch (calibrate
->type
) {
2758 case LTTNG_CALIBRATE_FUNCTION
:
2760 /* Default and only possible calibrate option. */
2761 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2765 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2767 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2772 case LTTNG_DOMAIN_UST
:
2774 struct lttng_ust_calibrate ucalibrate
;
2776 switch (calibrate
->type
) {
2777 case LTTNG_CALIBRATE_FUNCTION
:
2779 /* Default and only possible calibrate option. */
2780 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2784 ret
= ust_app_calibrate_glb(&ucalibrate
);
2786 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2792 ret
= LTTNG_ERR_UND
;
2803 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2805 int cmd_register_consumer(struct ltt_session
*session
,
2806 enum lttng_domain_type domain
, const char *sock_path
,
2807 struct consumer_data
*cdata
)
2810 struct consumer_socket
*socket
= NULL
;
2817 case LTTNG_DOMAIN_KERNEL
:
2819 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2823 /* Can't register a consumer if there is already one */
2824 if (ksess
->consumer_fds_sent
!= 0) {
2825 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2829 sock
= lttcomm_connect_unix_sock(sock_path
);
2831 ret
= LTTNG_ERR_CONNECT_FAIL
;
2834 cdata
->cmd_sock
= sock
;
2836 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2837 if (socket
== NULL
) {
2840 PERROR("close register consumer");
2842 cdata
->cmd_sock
= -1;
2843 ret
= LTTNG_ERR_FATAL
;
2847 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2848 if (socket
->lock
== NULL
) {
2849 PERROR("zmalloc pthread mutex");
2850 ret
= LTTNG_ERR_FATAL
;
2853 pthread_mutex_init(socket
->lock
, NULL
);
2854 socket
->registered
= 1;
2857 consumer_add_socket(socket
, ksess
->consumer
);
2860 pthread_mutex_lock(&cdata
->pid_mutex
);
2862 pthread_mutex_unlock(&cdata
->pid_mutex
);
2867 /* TODO: Userspace tracing */
2868 ret
= LTTNG_ERR_UND
;
2876 consumer_destroy_socket(socket
);
2882 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2884 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2885 struct lttng_domain
**domains
)
2890 struct lttng_ht_iter iter
;
2892 if (session
->kernel_session
!= NULL
) {
2893 DBG3("Listing domains found kernel domain");
2897 if (session
->ust_session
!= NULL
) {
2898 DBG3("Listing domains found UST global domain");
2902 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2904 if (agt
->being_used
) {
2915 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2916 if (*domains
== NULL
) {
2917 ret
= LTTNG_ERR_FATAL
;
2921 if (session
->kernel_session
!= NULL
) {
2922 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2924 /* Kernel session buffer type is always GLOBAL */
2925 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2930 if (session
->ust_session
!= NULL
) {
2931 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2932 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2936 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2938 if (agt
->being_used
) {
2939 (*domains
)[index
].type
= agt
->domain
;
2940 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2950 /* Return negative value to differentiate return code */
2956 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2958 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2959 struct ltt_session
*session
, struct lttng_channel
**channels
)
2961 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
2964 case LTTNG_DOMAIN_KERNEL
:
2965 if (session
->kernel_session
!= NULL
) {
2966 nb_chan
= session
->kernel_session
->channel_count
;
2968 DBG3("Number of kernel channels %zd", nb_chan
);
2970 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2974 case LTTNG_DOMAIN_UST
:
2975 if (session
->ust_session
!= NULL
) {
2977 nb_chan
= lttng_ht_get_count(
2978 session
->ust_session
->domain_global
.channels
);
2981 DBG3("Number of UST global channels %zd", nb_chan
);
2983 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2988 ret
= -LTTNG_ERR_UND
;
2993 const size_t channel_size
= sizeof(struct lttng_channel
) +
2994 sizeof(struct lttcomm_channel_extended
);
2995 struct lttcomm_channel_extended
*channel_exts
;
2997 payload_size
= nb_chan
* channel_size
;
2998 *channels
= zmalloc(payload_size
);
2999 if (*channels
== NULL
) {
3000 ret
= -LTTNG_ERR_FATAL
;
3004 channel_exts
= ((void *) *channels
) +
3005 (nb_chan
* sizeof(struct lttng_channel
));
3006 list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3017 * Command LTTNG_LIST_EVENTS processed by the client thread.
3019 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3020 struct ltt_session
*session
, char *channel_name
,
3021 struct lttng_event
**events
, size_t *total_size
)
3024 ssize_t nb_event
= 0;
3027 case LTTNG_DOMAIN_KERNEL
:
3028 if (session
->kernel_session
!= NULL
) {
3029 nb_event
= list_lttng_kernel_events(channel_name
,
3030 session
->kernel_session
, events
,
3034 case LTTNG_DOMAIN_UST
:
3036 if (session
->ust_session
!= NULL
) {
3037 nb_event
= list_lttng_ust_global_events(channel_name
,
3038 &session
->ust_session
->domain_global
, events
,
3043 case LTTNG_DOMAIN_LOG4J
:
3044 case LTTNG_DOMAIN_JUL
:
3045 case LTTNG_DOMAIN_PYTHON
:
3046 if (session
->ust_session
) {
3047 struct lttng_ht_iter iter
;
3051 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3052 &iter
.iter
, agt
, node
.node
) {
3053 if (agt
->domain
== domain
) {
3054 nb_event
= list_lttng_agent_events(
3064 ret
= LTTNG_ERR_UND
;
3071 /* Return negative value to differentiate return code */
3076 * Using the session list, filled a lttng_session array to send back to the
3077 * client for session listing.
3079 * The session list lock MUST be acquired before calling this function. Use
3080 * session_lock_list() and session_unlock_list().
3082 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3087 struct ltt_session
*session
;
3088 struct ltt_session_list
*list
= session_get_list();
3090 DBG("Getting all available session for UID %d GID %d",
3093 * Iterate over session list and append data after the control struct in
3096 cds_list_for_each_entry(session
, &list
->head
, list
) {
3098 * Only list the sessions the user can control.
3100 if (!session_access_ok(session
, uid
, gid
)) {
3104 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3105 struct ltt_ust_session
*usess
= session
->ust_session
;
3107 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3108 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3109 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3110 ret
= build_network_session_path(sessions
[i
].path
,
3111 sizeof(sessions
[i
].path
), session
);
3113 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3114 session
->consumer
->dst
.trace_path
);
3117 PERROR("snprintf session path");
3121 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3122 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3123 sessions
[i
].enabled
= session
->active
;
3124 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3125 sessions
[i
].live_timer_interval
= session
->live_timer
;
3131 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3132 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3134 int cmd_data_pending(struct ltt_session
*session
)
3137 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3138 struct ltt_ust_session
*usess
= session
->ust_session
;
3142 /* Session MUST be stopped to ask for data availability. */
3143 if (session
->active
) {
3144 ret
= LTTNG_ERR_SESSION_STARTED
;
3148 * If stopped, just make sure we've started before else the above call
3149 * will always send that there is data pending.
3151 * The consumer assumes that when the data pending command is received,
3152 * the trace has been started before or else no output data is written
3153 * by the streams which is a condition for data pending. So, this is
3154 * *VERY* important that we don't ask the consumer before a start
3157 if (!session
->has_been_started
) {
3163 if (ksess
&& ksess
->consumer
) {
3164 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3166 /* Data is still being extracted for the kernel. */
3171 if (usess
&& usess
->consumer
) {
3172 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3174 /* Data is still being extracted for the kernel. */
3179 /* Data is ready to be read by a viewer */
3187 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3189 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3191 int cmd_snapshot_add_output(struct ltt_session
*session
,
3192 struct lttng_snapshot_output
*output
, uint32_t *id
)
3195 struct snapshot_output
*new_output
;
3200 DBG("Cmd snapshot add output for session %s", session
->name
);
3203 * Permission denied to create an output if the session is not
3204 * set in no output mode.
3206 if (session
->output_traces
) {
3207 ret
= LTTNG_ERR_EPERM
;
3211 /* Only one output is allowed until we have the "tee" feature. */
3212 if (session
->snapshot
.nb_output
== 1) {
3213 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3217 new_output
= snapshot_output_alloc();
3219 ret
= LTTNG_ERR_NOMEM
;
3223 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3224 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3225 &session
->snapshot
);
3227 if (ret
== -ENOMEM
) {
3228 ret
= LTTNG_ERR_NOMEM
;
3230 ret
= LTTNG_ERR_INVALID
;
3236 snapshot_add_output(&session
->snapshot
, new_output
);
3238 *id
= new_output
->id
;
3245 snapshot_output_destroy(new_output
);
3251 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3253 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3255 int cmd_snapshot_del_output(struct ltt_session
*session
,
3256 struct lttng_snapshot_output
*output
)
3259 struct snapshot_output
*sout
= NULL
;
3267 * Permission denied to create an output if the session is not
3268 * set in no output mode.
3270 if (session
->output_traces
) {
3271 ret
= LTTNG_ERR_EPERM
;
3276 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3278 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3279 } else if (*output
->name
!= '\0') {
3280 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3282 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3285 ret
= LTTNG_ERR_INVALID
;
3289 snapshot_delete_output(&session
->snapshot
, sout
);
3290 snapshot_output_destroy(sout
);
3299 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3301 * If no output is available, outputs is untouched and 0 is returned.
3303 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3305 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3306 struct lttng_snapshot_output
**outputs
)
3309 struct lttng_snapshot_output
*list
= NULL
;
3310 struct lttng_ht_iter iter
;
3311 struct snapshot_output
*output
;
3316 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3319 * Permission denied to create an output if the session is not
3320 * set in no output mode.
3322 if (session
->output_traces
) {
3323 ret
= -LTTNG_ERR_EPERM
;
3327 if (session
->snapshot
.nb_output
== 0) {
3332 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3334 ret
= -LTTNG_ERR_NOMEM
;
3338 /* Copy list from session to the new list object. */
3340 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3341 output
, node
.node
) {
3342 assert(output
->consumer
);
3343 list
[idx
].id
= output
->id
;
3344 list
[idx
].max_size
= output
->max_size
;
3345 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
3346 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3347 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
3348 sizeof(list
[idx
].ctrl_url
));
3351 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3352 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3354 ret
= -LTTNG_ERR_NOMEM
;
3359 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3360 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3362 ret
= -LTTNG_ERR_NOMEM
;
3371 ret
= session
->snapshot
.nb_output
;
3379 * Check if we can regenerate the metadata for this session.
3380 * Only kernel, UST per-uid and non-live sessions are supported.
3382 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3385 int check_metadata_regenerate_support(struct ltt_session
*session
)
3391 if (session
->live_timer
!= 0) {
3392 ret
= LTTNG_ERR_LIVE_SESSION
;
3395 if (!session
->active
) {
3396 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3399 if (session
->ust_session
) {
3400 switch (session
->ust_session
->buffer_type
) {
3401 case LTTNG_BUFFER_PER_UID
:
3403 case LTTNG_BUFFER_PER_PID
:
3404 ret
= LTTNG_ERR_PER_PID_SESSION
;
3408 ret
= LTTNG_ERR_UNK
;
3412 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3413 session
->consumer
->relay_minor_version
< 8) {
3414 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3424 int ust_metadata_regenerate(struct ltt_ust_session
*usess
)
3427 struct buffer_reg_uid
*uid_reg
= NULL
;
3428 struct buffer_reg_session
*session_reg
= NULL
;
3431 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3432 struct ust_registry_session
*registry
;
3433 struct ust_registry_channel
*chan
;
3434 struct lttng_ht_iter iter_chan
;
3436 session_reg
= uid_reg
->registry
;
3437 registry
= session_reg
->reg
.ust
;
3439 pthread_mutex_lock(®istry
->lock
);
3440 registry
->metadata_len_sent
= 0;
3441 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3442 registry
->metadata_len
= 0;
3443 registry
->metadata_version
++;
3444 ret
= ust_metadata_session_statedump(registry
, NULL
,
3445 registry
->major
, registry
->minor
);
3447 pthread_mutex_unlock(®istry
->lock
);
3448 ERR("Failed to generate session metadata (err = %d)",
3452 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3454 struct ust_registry_event
*event
;
3455 struct lttng_ht_iter iter_event
;
3457 ret
= ust_metadata_channel_statedump(registry
, chan
);
3459 pthread_mutex_unlock(®istry
->lock
);
3460 ERR("Failed to generate channel metadata "
3464 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3466 ret
= ust_metadata_event_statedump(registry
,
3469 pthread_mutex_unlock(®istry
->lock
);
3470 ERR("Failed to generate event metadata "
3476 pthread_mutex_unlock(®istry
->lock
);
3485 * Command LTTNG_METADATA_REGENERATE from the lttng-ctl library.
3487 * Ask the consumer to truncate the existing metadata file(s) and
3488 * then regenerate the metadata. Live and per-pid sessions are not
3489 * supported and return an error.
3491 * Return 0 on success or else a LTTNG_ERR code.
3493 int cmd_metadata_regenerate(struct ltt_session
*session
)
3499 ret
= check_metadata_regenerate_support(session
);
3504 if (session
->kernel_session
) {
3505 ret
= kernctl_session_metadata_regenerate(
3506 session
->kernel_session
->fd
);
3508 ERR("Failed to regenerate the kernel metadata");
3513 if (session
->ust_session
) {
3514 ret
= ust_metadata_regenerate(session
->ust_session
);
3516 ERR("Failed to regenerate the UST metadata");
3520 DBG("Cmd metadata regenerate for session %s", session
->name
);
3529 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3530 * snapshot output is *not* set with a remote destination.
3532 * Return 0 on success or a LTTNG_ERR code.
3534 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3535 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3538 struct lttng_ht_iter iter
;
3539 struct consumer_socket
*socket
;
3542 assert(snap_output
);
3545 DBG2("Set relayd object from snapshot output");
3547 /* Ignore if snapshot consumer output is not network. */
3548 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3553 * For each consumer socket, create and send the relayd object of the
3557 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3558 socket
, node
.node
) {
3559 ret
= send_consumer_relayd_sockets(0, session
->id
,
3560 snap_output
->consumer
, socket
,
3561 session
->name
, session
->hostname
,
3562 session
->live_timer
);
3563 if (ret
!= LTTNG_OK
) {
3575 * Record a kernel snapshot.
3577 * Return LTTNG_OK on success or a LTTNG_ERR code.
3579 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3580 struct snapshot_output
*output
, struct ltt_session
*session
,
3581 int wait
, uint64_t nb_packets_per_stream
)
3589 /* Get the datetime for the snapshot output directory. */
3590 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3591 sizeof(output
->datetime
));
3593 ret
= LTTNG_ERR_INVALID
;
3598 * Copy kernel session sockets so we can communicate with the right
3599 * consumer for the snapshot record command.
3601 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3603 ret
= LTTNG_ERR_NOMEM
;
3607 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3608 if (ret
!= LTTNG_OK
) {
3609 goto error_snapshot
;
3612 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3613 if (ret
!= LTTNG_OK
) {
3614 goto error_snapshot
;
3621 /* Clean up copied sockets so this output can use some other later on. */
3622 consumer_destroy_output_sockets(output
->consumer
);
3629 * Record a UST snapshot.
3631 * Return 0 on success or a LTTNG_ERR error code.
3633 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3634 struct snapshot_output
*output
, struct ltt_session
*session
,
3635 int wait
, uint64_t nb_packets_per_stream
)
3643 /* Get the datetime for the snapshot output directory. */
3644 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3645 sizeof(output
->datetime
));
3647 ret
= LTTNG_ERR_INVALID
;
3652 * Copy UST session sockets so we can communicate with the right
3653 * consumer for the snapshot record command.
3655 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3657 ret
= LTTNG_ERR_NOMEM
;
3661 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3662 if (ret
!= LTTNG_OK
) {
3663 goto error_snapshot
;
3666 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3670 ret
= LTTNG_ERR_INVALID
;
3673 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3676 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3679 goto error_snapshot
;
3685 /* Clean up copied sockets so this output can use some other later on. */
3686 consumer_destroy_output_sockets(output
->consumer
);
3692 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3693 uint64_t cur_nr_packets
)
3695 uint64_t tot_size
= 0;
3697 if (session
->kernel_session
) {
3698 struct ltt_kernel_channel
*chan
;
3699 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3701 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3702 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3704 * Don't take channel into account if we
3705 * already grab all its packets.
3709 tot_size
+= chan
->channel
->attr
.subbuf_size
3710 * chan
->stream_count
;
3714 if (session
->ust_session
) {
3715 struct ltt_ust_session
*usess
= session
->ust_session
;
3717 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3725 * Calculate the number of packets we can grab from each stream that
3726 * fits within the overall snapshot max size.
3728 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3729 * the number of packets per stream.
3731 * TODO: this approach is not perfect: we consider the worse case
3732 * (packet filling the sub-buffers) as an upper bound, but we could do
3733 * better if we do this calculation while we actually grab the packet
3734 * content: we would know how much padding we don't actually store into
3737 * This algorithm is currently bounded by the number of packets per
3740 * Since we call this algorithm before actually grabbing the data, it's
3741 * an approximation: for instance, applications could appear/disappear
3742 * in between this call and actually grabbing data.
3745 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3748 uint64_t cur_nb_packets
= 0;
3751 return 0; /* Infinite */
3754 size_left
= max_size
;
3756 uint64_t one_more_packet_tot_size
;
3758 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3760 if (!one_more_packet_tot_size
) {
3761 /* We are already grabbing all packets. */
3764 size_left
-= one_more_packet_tot_size
;
3765 if (size_left
< 0) {
3770 if (!cur_nb_packets
) {
3771 /* Not enough room to grab one packet of each stream, error. */
3774 return cur_nb_packets
;
3778 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3780 * The wait parameter is ignored so this call always wait for the snapshot to
3781 * complete before returning.
3783 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3785 int cmd_snapshot_record(struct ltt_session
*session
,
3786 struct lttng_snapshot_output
*output
, int wait
)
3789 unsigned int use_tmp_output
= 0;
3790 struct snapshot_output tmp_output
;
3791 unsigned int snapshot_success
= 0;
3796 DBG("Cmd snapshot record for session %s", session
->name
);
3799 * Permission denied to create an output if the session is not
3800 * set in no output mode.
3802 if (session
->output_traces
) {
3803 ret
= LTTNG_ERR_EPERM
;
3807 /* The session needs to be started at least once. */
3808 if (!session
->has_been_started
) {
3809 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3813 /* Use temporary output for the session. */
3814 if (*output
->ctrl_url
!= '\0') {
3815 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3816 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3819 if (ret
== -ENOMEM
) {
3820 ret
= LTTNG_ERR_NOMEM
;
3822 ret
= LTTNG_ERR_INVALID
;
3826 /* Use the global session count for the temporary snapshot. */
3827 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3831 if (session
->kernel_session
) {
3832 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3834 if (use_tmp_output
) {
3835 int64_t nb_packets_per_stream
;
3837 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3838 tmp_output
.max_size
);
3839 if (nb_packets_per_stream
< 0) {
3840 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3843 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3844 wait
, nb_packets_per_stream
);
3845 if (ret
!= LTTNG_OK
) {
3848 snapshot_success
= 1;
3850 struct snapshot_output
*sout
;
3851 struct lttng_ht_iter iter
;
3854 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3855 &iter
.iter
, sout
, node
.node
) {
3856 int64_t nb_packets_per_stream
;
3859 * Make a local copy of the output and assign the possible
3860 * temporary value given by the caller.
3862 memset(&tmp_output
, 0, sizeof(tmp_output
));
3863 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3865 if (output
->max_size
!= (uint64_t) -1ULL) {
3866 tmp_output
.max_size
= output
->max_size
;
3869 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3870 tmp_output
.max_size
);
3871 if (nb_packets_per_stream
< 0) {
3872 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3876 /* Use temporary name. */
3877 if (*output
->name
!= '\0') {
3878 strncpy(tmp_output
.name
, output
->name
,
3879 sizeof(tmp_output
.name
));
3882 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3884 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3885 session
, wait
, nb_packets_per_stream
);
3886 if (ret
!= LTTNG_OK
) {
3890 snapshot_success
= 1;
3896 if (session
->ust_session
) {
3897 struct ltt_ust_session
*usess
= session
->ust_session
;
3899 if (use_tmp_output
) {
3900 int64_t nb_packets_per_stream
;
3902 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3903 tmp_output
.max_size
);
3904 if (nb_packets_per_stream
< 0) {
3905 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3908 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3909 wait
, nb_packets_per_stream
);
3910 if (ret
!= LTTNG_OK
) {
3913 snapshot_success
= 1;
3915 struct snapshot_output
*sout
;
3916 struct lttng_ht_iter iter
;
3919 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3920 &iter
.iter
, sout
, node
.node
) {
3921 int64_t nb_packets_per_stream
;
3924 * Make a local copy of the output and assign the possible
3925 * temporary value given by the caller.
3927 memset(&tmp_output
, 0, sizeof(tmp_output
));
3928 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3930 if (output
->max_size
!= (uint64_t) -1ULL) {
3931 tmp_output
.max_size
= output
->max_size
;
3934 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3935 tmp_output
.max_size
);
3936 if (nb_packets_per_stream
< 0) {
3937 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3942 /* Use temporary name. */
3943 if (*output
->name
!= '\0') {
3944 strncpy(tmp_output
.name
, output
->name
,
3945 sizeof(tmp_output
.name
));
3948 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3950 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3951 wait
, nb_packets_per_stream
);
3952 if (ret
!= LTTNG_OK
) {
3956 snapshot_success
= 1;
3962 if (snapshot_success
) {
3963 session
->snapshot
.nb_snapshot
++;
3965 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3973 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3975 int cmd_set_session_shm_path(struct ltt_session
*session
,
3976 const char *shm_path
)
3982 * Can only set shm path before session is started.
3984 if (session
->has_been_started
) {
3985 return LTTNG_ERR_SESSION_STARTED
;
3988 strncpy(session
->shm_path
, shm_path
,
3989 sizeof(session
->shm_path
));
3990 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3996 * Init command subsystem.
4001 * Set network sequence index to 1 for streams to match a relayd
4002 * socket on the consumer side.
4004 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4005 relayd_net_seq_idx
= 1;
4006 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4008 DBG("Command subsystem initialized");