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");
228 * Fill lttng_channel array of all channels.
230 static void list_lttng_channels(enum lttng_domain_type domain
,
231 struct ltt_session
*session
, struct lttng_channel
*channels
,
232 struct lttcomm_channel_extended
*chan_exts
)
235 struct ltt_kernel_channel
*kchan
;
237 DBG("Listing channels for session %s", session
->name
);
240 case LTTNG_DOMAIN_KERNEL
:
241 /* Kernel channels */
242 if (session
->kernel_session
!= NULL
) {
243 cds_list_for_each_entry(kchan
,
244 &session
->kernel_session
->channel_list
.head
, list
) {
245 uint64_t discarded_events
, lost_packets
;
247 ret
= get_kernel_runtime_stats(session
, kchan
,
248 &discarded_events
, &lost_packets
);
252 /* Copy lttng_channel struct to array */
253 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
254 channels
[i
].enabled
= kchan
->enabled
;
255 chan_exts
[i
].discarded_events
=
257 chan_exts
[i
].lost_packets
= lost_packets
;
262 case LTTNG_DOMAIN_UST
:
264 struct lttng_ht_iter iter
;
265 struct ltt_ust_channel
*uchan
;
268 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
269 &iter
.iter
, uchan
, node
.node
) {
270 uint64_t discarded_events
, lost_packets
;
272 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
273 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
274 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
275 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
276 channels
[i
].attr
.switch_timer_interval
=
277 uchan
->attr
.switch_timer_interval
;
278 channels
[i
].attr
.read_timer_interval
=
279 uchan
->attr
.read_timer_interval
;
280 channels
[i
].enabled
= uchan
->enabled
;
281 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
282 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
285 * Map enum lttng_ust_output to enum lttng_event_output.
287 switch (uchan
->attr
.output
) {
289 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
293 * LTTNG_UST_MMAP is the only supported UST
300 ret
= get_ust_runtime_stats(session
, uchan
,
301 &discarded_events
, &lost_packets
);
305 chan_exts
[i
].discarded_events
= discarded_events
;
306 chan_exts
[i
].lost_packets
= lost_packets
;
320 static void increment_extended_len(const char *filter_expression
,
321 struct lttng_event_exclusion
*exclusion
, size_t *extended_len
)
323 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
325 if (filter_expression
) {
326 *extended_len
+= strlen(filter_expression
) + 1;
330 *extended_len
+= exclusion
->count
* LTTNG_SYMBOL_NAME_LEN
;
334 static void append_extended_info(const char *filter_expression
,
335 struct lttng_event_exclusion
*exclusion
, void **extended_at
)
337 struct lttcomm_event_extended_header extended_header
;
338 size_t filter_len
= 0;
339 size_t nb_exclusions
= 0;
341 if (filter_expression
) {
342 filter_len
= strlen(filter_expression
) + 1;
346 nb_exclusions
= exclusion
->count
;
349 /* Set header fields */
350 extended_header
.filter_len
= filter_len
;
351 extended_header
.nb_exclusions
= nb_exclusions
;
354 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
355 *extended_at
+= sizeof(extended_header
);
357 /* Copy filter string */
358 if (filter_expression
) {
359 memcpy(*extended_at
, filter_expression
, filter_len
);
360 *extended_at
+= filter_len
;
363 /* Copy exclusion names */
365 size_t len
= nb_exclusions
* LTTNG_SYMBOL_NAME_LEN
;
367 memcpy(*extended_at
, &exclusion
->names
, len
);
373 * Create a list of agent domain events.
375 * Return number of events in list on success or else a negative value.
377 static int list_lttng_agent_events(struct agent
*agt
,
378 struct lttng_event
**events
, size_t *total_size
)
381 unsigned int nb_event
= 0;
382 struct agent_event
*event
;
383 struct lttng_event
*tmp_events
;
384 struct lttng_ht_iter iter
;
385 size_t extended_len
= 0;
391 DBG3("Listing agent events");
394 nb_event
= lttng_ht_get_count(agt
->events
);
402 /* Compute required extended infos size */
403 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
406 * This is only valid because the commands which add events are
407 * processed in the same thread as the listing.
410 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
411 increment_extended_len(event
->filter_expression
, NULL
,
416 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
417 tmp_events
= zmalloc(*total_size
);
419 PERROR("zmalloc agent events session");
420 ret
= -LTTNG_ERR_FATAL
;
424 extended_at
= ((uint8_t *) tmp_events
) +
425 nb_event
* sizeof(struct lttng_event
);
428 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
429 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
430 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
431 tmp_events
[i
].enabled
= event
->enabled
;
432 tmp_events
[i
].loglevel
= event
->loglevel_value
;
433 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
436 /* Append extended info */
437 append_extended_info(event
->filter_expression
, NULL
,
442 *events
= tmp_events
;
446 assert(nb_event
== i
);
451 * Create a list of ust global domain events.
453 static int list_lttng_ust_global_events(char *channel_name
,
454 struct ltt_ust_domain_global
*ust_global
,
455 struct lttng_event
**events
, size_t *total_size
)
458 unsigned int nb_event
= 0;
459 struct lttng_ht_iter iter
;
460 struct lttng_ht_node_str
*node
;
461 struct ltt_ust_channel
*uchan
;
462 struct ltt_ust_event
*uevent
;
463 struct lttng_event
*tmp
;
464 size_t extended_len
= 0;
467 DBG("Listing UST global events for channel %s", channel_name
);
471 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
472 node
= lttng_ht_iter_get_node_str(&iter
);
474 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
478 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
480 nb_event
= lttng_ht_get_count(uchan
->events
);
487 DBG3("Listing UST global %d events", nb_event
);
489 /* Compute required extended infos size */
490 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
491 if (uevent
->internal
) {
496 increment_extended_len(uevent
->filter_expression
,
497 uevent
->exclusion
, &extended_len
);
500 /* All events are internal, skip. */
506 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
507 tmp
= zmalloc(*total_size
);
509 ret
= -LTTNG_ERR_FATAL
;
513 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
515 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
516 if (uevent
->internal
) {
517 /* This event should remain hidden from clients */
520 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
521 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
522 tmp
[i
].enabled
= uevent
->enabled
;
524 switch (uevent
->attr
.instrumentation
) {
525 case LTTNG_UST_TRACEPOINT
:
526 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
528 case LTTNG_UST_PROBE
:
529 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
531 case LTTNG_UST_FUNCTION
:
532 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
536 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
537 switch (uevent
->attr
.loglevel_type
) {
538 case LTTNG_UST_LOGLEVEL_ALL
:
539 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
541 case LTTNG_UST_LOGLEVEL_RANGE
:
542 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
544 case LTTNG_UST_LOGLEVEL_SINGLE
:
545 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
548 if (uevent
->filter
) {
551 if (uevent
->exclusion
) {
552 tmp
[i
].exclusion
= 1;
556 /* Append extended info */
557 append_extended_info(uevent
->filter_expression
,
558 uevent
->exclusion
, &extended_at
);
569 * Fill lttng_event array of all kernel events in the channel.
571 static int list_lttng_kernel_events(char *channel_name
,
572 struct ltt_kernel_session
*kernel_session
,
573 struct lttng_event
**events
, size_t *total_size
)
576 unsigned int nb_event
;
577 struct ltt_kernel_event
*event
;
578 struct ltt_kernel_channel
*kchan
;
579 size_t extended_len
= 0;
582 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
584 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
588 nb_event
= kchan
->event_count
;
590 DBG("Listing events for channel %s", kchan
->channel
->name
);
598 /* Compute required extended infos size */
599 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
600 increment_extended_len(event
->filter_expression
, NULL
,
604 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
605 *events
= zmalloc(*total_size
);
606 if (*events
== NULL
) {
607 ret
= LTTNG_ERR_FATAL
;
611 extended_at
= ((void *) *events
) +
612 nb_event
* sizeof(struct lttng_event
);
614 /* Kernel channels */
615 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
616 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
617 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
618 (*events
)[i
].enabled
= event
->enabled
;
619 (*events
)[i
].filter
=
620 (unsigned char) !!event
->filter_expression
;
622 switch (event
->event
->instrumentation
) {
623 case LTTNG_KERNEL_TRACEPOINT
:
624 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
626 case LTTNG_KERNEL_KRETPROBE
:
627 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
628 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
629 sizeof(struct lttng_kernel_kprobe
));
631 case LTTNG_KERNEL_KPROBE
:
632 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
633 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
634 sizeof(struct lttng_kernel_kprobe
));
636 case LTTNG_KERNEL_FUNCTION
:
637 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
638 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
639 sizeof(struct lttng_kernel_function
));
641 case LTTNG_KERNEL_NOOP
:
642 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
644 case LTTNG_KERNEL_SYSCALL
:
645 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
647 case LTTNG_KERNEL_ALL
:
653 /* Append extended info */
654 append_extended_info(event
->filter_expression
, NULL
,
662 /* Negate the error code to differentiate the size from an error */
667 * Add URI so the consumer output object. Set the correct path depending on the
668 * domain adding the default trace directory.
670 static int add_uri_to_consumer(struct consumer_output
*consumer
,
671 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
672 const char *session_name
)
675 const char *default_trace_dir
;
679 if (consumer
== NULL
) {
680 DBG("No consumer detected. Don't add URI. Stopping.");
681 ret
= LTTNG_ERR_NO_CONSUMER
;
686 case LTTNG_DOMAIN_KERNEL
:
687 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
689 case LTTNG_DOMAIN_UST
:
690 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
694 * This case is possible is we try to add the URI to the global tracing
695 * session consumer object which in this case there is no subdir.
697 default_trace_dir
= "";
700 switch (uri
->dtype
) {
703 DBG2("Setting network URI to consumer");
705 if (consumer
->type
== CONSUMER_DST_NET
) {
706 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
707 consumer
->dst
.net
.control_isset
) ||
708 (uri
->stype
== LTTNG_STREAM_DATA
&&
709 consumer
->dst
.net
.data_isset
)) {
710 ret
= LTTNG_ERR_URL_EXIST
;
714 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
717 consumer
->type
= CONSUMER_DST_NET
;
719 /* Set URI into consumer output object */
720 ret
= consumer_set_network_uri(consumer
, uri
);
724 } else if (ret
== 1) {
726 * URI was the same in the consumer so we do not append the subdir
727 * again so to not duplicate output dir.
733 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
734 ret
= consumer_set_subdir(consumer
, session_name
);
736 ret
= LTTNG_ERR_FATAL
;
741 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
742 /* On a new subdir, reappend the default trace dir. */
743 strncat(consumer
->subdir
, default_trace_dir
,
744 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
745 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
750 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
751 memset(consumer
->dst
.trace_path
, 0,
752 sizeof(consumer
->dst
.trace_path
));
753 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
754 sizeof(consumer
->dst
.trace_path
));
755 /* Append default trace dir */
756 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
757 sizeof(consumer
->dst
.trace_path
) -
758 strlen(consumer
->dst
.trace_path
) - 1);
759 /* Flag consumer as local. */
760 consumer
->type
= CONSUMER_DST_LOCAL
;
771 * Init tracing by creating trace directory and sending fds kernel consumer.
773 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
776 struct lttng_ht_iter iter
;
777 struct consumer_socket
*socket
;
783 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
784 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
786 pthread_mutex_lock(socket
->lock
);
787 ret
= kernel_consumer_send_session(socket
, session
);
788 pthread_mutex_unlock(socket
->lock
);
790 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
802 * Create a socket to the relayd using the URI.
804 * On success, the relayd_sock pointer is set to the created socket.
805 * Else, it's stays untouched and a lttcomm error code is returned.
807 static int create_connect_relayd(struct lttng_uri
*uri
,
808 struct lttcomm_relayd_sock
**relayd_sock
,
809 struct consumer_output
*consumer
)
812 struct lttcomm_relayd_sock
*rsock
;
814 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
815 RELAYD_VERSION_COMM_MINOR
);
817 ret
= LTTNG_ERR_FATAL
;
822 * Connect to relayd so we can proceed with a session creation. This call
823 * can possibly block for an arbitrary amount of time to set the health
824 * state to be in poll execution.
827 ret
= relayd_connect(rsock
);
830 ERR("Unable to reach lttng-relayd");
831 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
835 /* Create socket for control stream. */
836 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
837 DBG3("Creating relayd stream socket from URI");
839 /* Check relayd version */
840 ret
= relayd_version_check(rsock
);
842 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
845 consumer
->relay_major_version
= rsock
->major
;
846 consumer
->relay_minor_version
= rsock
->minor
;
847 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
848 DBG3("Creating relayd data socket from URI");
850 /* Command is not valid */
851 ERR("Relayd invalid stream type: %d", uri
->stype
);
852 ret
= LTTNG_ERR_INVALID
;
856 *relayd_sock
= rsock
;
861 /* The returned value is not useful since we are on an error path. */
862 (void) relayd_close(rsock
);
870 * Connect to the relayd using URI and send the socket to the right consumer.
872 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
873 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
874 struct consumer_output
*consumer
,
875 struct consumer_socket
*consumer_sock
,
876 char *session_name
, char *hostname
, int session_live_timer
)
879 struct lttcomm_relayd_sock
*rsock
= NULL
;
881 /* Connect to relayd and make version check if uri is the control. */
882 ret
= create_connect_relayd(relayd_uri
, &rsock
, consumer
);
883 if (ret
!= LTTNG_OK
) {
888 /* Set the network sequence index if not set. */
889 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
890 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
892 * Increment net_seq_idx because we are about to transfer the
893 * new relayd socket to the consumer.
894 * Assign unique key so the consumer can match streams.
896 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
897 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
900 /* Send relayd socket to consumer. */
901 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
902 relayd_uri
->stype
, session_id
,
903 session_name
, hostname
, session_live_timer
);
905 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
909 /* Flag that the corresponding socket was sent. */
910 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
911 consumer_sock
->control_sock_sent
= 1;
912 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
913 consumer_sock
->data_sock_sent
= 1;
919 * Close socket which was dup on the consumer side. The session daemon does
920 * NOT keep track of the relayd socket(s) once transfer to the consumer.
924 (void) relayd_close(rsock
);
928 if (ret
!= LTTNG_OK
) {
930 * The consumer output for this session should not be used anymore
931 * since the relayd connection failed thus making any tracing or/and
932 * streaming not usable.
934 consumer
->enabled
= 0;
940 * Send both relayd sockets to a specific consumer and domain. This is a
941 * helper function to facilitate sending the information to the consumer for a
944 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
945 unsigned int session_id
, struct consumer_output
*consumer
,
946 struct consumer_socket
*sock
, char *session_name
,
947 char *hostname
, int session_live_timer
)
954 /* Sending control relayd socket. */
955 if (!sock
->control_sock_sent
) {
956 ret
= send_consumer_relayd_socket(domain
, session_id
,
957 &consumer
->dst
.net
.control
, consumer
, sock
,
958 session_name
, hostname
, session_live_timer
);
959 if (ret
!= LTTNG_OK
) {
964 /* Sending data relayd socket. */
965 if (!sock
->data_sock_sent
) {
966 ret
= send_consumer_relayd_socket(domain
, session_id
,
967 &consumer
->dst
.net
.data
, consumer
, sock
,
968 session_name
, hostname
, session_live_timer
);
969 if (ret
!= LTTNG_OK
) {
979 * Setup relayd connections for a tracing session. First creates the socket to
980 * the relayd and send them to the right domain consumer. Consumer type MUST be
983 int cmd_setup_relayd(struct ltt_session
*session
)
986 struct ltt_ust_session
*usess
;
987 struct ltt_kernel_session
*ksess
;
988 struct consumer_socket
*socket
;
989 struct lttng_ht_iter iter
;
993 usess
= session
->ust_session
;
994 ksess
= session
->kernel_session
;
996 DBG("Setting relayd for session %s", session
->name
);
1000 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
1001 && usess
->consumer
->enabled
) {
1002 /* For each consumer socket, send relayd sockets */
1003 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
1004 socket
, node
.node
) {
1005 pthread_mutex_lock(socket
->lock
);
1006 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
1007 usess
->consumer
, socket
,
1008 session
->name
, session
->hostname
,
1009 session
->live_timer
);
1010 pthread_mutex_unlock(socket
->lock
);
1011 if (ret
!= LTTNG_OK
) {
1014 /* Session is now ready for network streaming. */
1015 session
->net_handle
= 1;
1017 session
->consumer
->relay_major_version
=
1018 usess
->consumer
->relay_major_version
;
1019 session
->consumer
->relay_minor_version
=
1020 usess
->consumer
->relay_minor_version
;
1023 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
1024 && ksess
->consumer
->enabled
) {
1025 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1026 socket
, node
.node
) {
1027 pthread_mutex_lock(socket
->lock
);
1028 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
1029 ksess
->consumer
, socket
,
1030 session
->name
, session
->hostname
,
1031 session
->live_timer
);
1032 pthread_mutex_unlock(socket
->lock
);
1033 if (ret
!= LTTNG_OK
) {
1036 /* Session is now ready for network streaming. */
1037 session
->net_handle
= 1;
1039 session
->consumer
->relay_major_version
=
1040 ksess
->consumer
->relay_major_version
;
1041 session
->consumer
->relay_minor_version
=
1042 ksess
->consumer
->relay_minor_version
;
1051 * Start a kernel session by opening all necessary streams.
1053 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
1056 struct ltt_kernel_channel
*kchan
;
1058 /* Open kernel metadata */
1059 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
1060 ret
= kernel_open_metadata(ksess
);
1062 ret
= LTTNG_ERR_KERN_META_FAIL
;
1067 /* Open kernel metadata stream */
1068 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
1069 ret
= kernel_open_metadata_stream(ksess
);
1071 ERR("Kernel create metadata stream failed");
1072 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1077 /* For each channel */
1078 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
1079 if (kchan
->stream_count
== 0) {
1080 ret
= kernel_open_channel_stream(kchan
);
1082 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
1085 /* Update the stream global counter */
1086 ksess
->stream_count_global
+= ret
;
1090 /* Setup kernel consumer socket and send fds to it */
1091 ret
= init_kernel_tracing(ksess
);
1093 ret
= LTTNG_ERR_KERN_START_FAIL
;
1097 /* This start the kernel tracing */
1098 ret
= kernel_start_session(ksess
);
1100 ret
= LTTNG_ERR_KERN_START_FAIL
;
1104 /* Quiescent wait after starting trace */
1105 kernel_wait_quiescent(kernel_tracer_fd
);
1116 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1118 int cmd_disable_channel(struct ltt_session
*session
,
1119 enum lttng_domain_type domain
, char *channel_name
)
1122 struct ltt_ust_session
*usess
;
1124 usess
= session
->ust_session
;
1129 case LTTNG_DOMAIN_KERNEL
:
1131 ret
= channel_kernel_disable(session
->kernel_session
,
1133 if (ret
!= LTTNG_OK
) {
1137 kernel_wait_quiescent(kernel_tracer_fd
);
1140 case LTTNG_DOMAIN_UST
:
1142 struct ltt_ust_channel
*uchan
;
1143 struct lttng_ht
*chan_ht
;
1145 chan_ht
= usess
->domain_global
.channels
;
1147 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1148 if (uchan
== NULL
) {
1149 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1153 ret
= channel_ust_disable(usess
, uchan
);
1154 if (ret
!= LTTNG_OK
) {
1160 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1172 * Command LTTNG_TRACK_PID processed by the client thread.
1174 * Called with session lock held.
1176 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1184 case LTTNG_DOMAIN_KERNEL
:
1186 struct ltt_kernel_session
*ksess
;
1188 ksess
= session
->kernel_session
;
1190 ret
= kernel_track_pid(ksess
, pid
);
1191 if (ret
!= LTTNG_OK
) {
1195 kernel_wait_quiescent(kernel_tracer_fd
);
1198 case LTTNG_DOMAIN_UST
:
1200 struct ltt_ust_session
*usess
;
1202 usess
= session
->ust_session
;
1204 ret
= trace_ust_track_pid(usess
, pid
);
1205 if (ret
!= LTTNG_OK
) {
1211 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1223 * Command LTTNG_UNTRACK_PID processed by the client thread.
1225 * Called with session lock held.
1227 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1235 case LTTNG_DOMAIN_KERNEL
:
1237 struct ltt_kernel_session
*ksess
;
1239 ksess
= session
->kernel_session
;
1241 ret
= kernel_untrack_pid(ksess
, pid
);
1242 if (ret
!= LTTNG_OK
) {
1246 kernel_wait_quiescent(kernel_tracer_fd
);
1249 case LTTNG_DOMAIN_UST
:
1251 struct ltt_ust_session
*usess
;
1253 usess
= session
->ust_session
;
1255 ret
= trace_ust_untrack_pid(usess
, pid
);
1256 if (ret
!= LTTNG_OK
) {
1262 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1274 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1276 * The wpipe arguments is used as a notifier for the kernel thread.
1278 int cmd_enable_channel(struct ltt_session
*session
,
1279 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1282 struct ltt_ust_session
*usess
= session
->ust_session
;
1283 struct lttng_ht
*chan_ht
;
1290 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1292 /* Validate channel name */
1293 if (attr
->name
[0] == '.' ||
1294 memchr(attr
->name
, '/', len
) != NULL
) {
1295 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1299 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1304 * Don't try to enable a channel if the session has been started at
1305 * some point in time before. The tracer does not allow it.
1307 if (session
->has_been_started
) {
1308 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1313 * If the session is a live session, remove the switch timer, the
1314 * live timer does the same thing but sends also synchronisation
1315 * beacons for inactive streams.
1317 if (session
->live_timer
> 0) {
1318 attr
->attr
.live_timer_interval
= session
->live_timer
;
1319 attr
->attr
.switch_timer_interval
= 0;
1323 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1324 * mode and with less than 2 subbuf so block it right away and send back an
1325 * invalid attribute error.
1327 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
1328 ret
= LTTNG_ERR_INVALID
;
1332 switch (domain
->type
) {
1333 case LTTNG_DOMAIN_KERNEL
:
1335 struct ltt_kernel_channel
*kchan
;
1337 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1338 session
->kernel_session
);
1339 if (kchan
== NULL
) {
1340 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1341 if (attr
->name
[0] != '\0') {
1342 session
->kernel_session
->has_non_default_channel
= 1;
1345 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1348 if (ret
!= LTTNG_OK
) {
1352 kernel_wait_quiescent(kernel_tracer_fd
);
1355 case LTTNG_DOMAIN_UST
:
1356 case LTTNG_DOMAIN_JUL
:
1357 case LTTNG_DOMAIN_LOG4J
:
1358 case LTTNG_DOMAIN_PYTHON
:
1360 struct ltt_ust_channel
*uchan
;
1365 * Current agent implementation limitations force us to allow
1366 * only one channel at once in "agent" subdomains. Each
1367 * subdomain has a default channel name which must be strictly
1370 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1371 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1372 LTTNG_SYMBOL_NAME_LEN
)) {
1373 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1376 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1377 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1378 LTTNG_SYMBOL_NAME_LEN
)) {
1379 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1382 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1383 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1384 LTTNG_SYMBOL_NAME_LEN
)) {
1385 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1390 chan_ht
= usess
->domain_global
.channels
;
1392 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1393 if (uchan
== NULL
) {
1394 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1395 if (attr
->name
[0] != '\0') {
1396 usess
->has_non_default_channel
= 1;
1399 ret
= channel_ust_enable(usess
, uchan
);
1404 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1415 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1417 int cmd_disable_event(struct ltt_session
*session
,
1418 enum lttng_domain_type domain
, char *channel_name
,
1419 struct lttng_event
*event
)
1424 DBG("Disable event command for event \'%s\'", event
->name
);
1426 event_name
= event
->name
;
1427 if (validate_event_name(event_name
)) {
1428 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1432 /* Error out on unhandled search criteria */
1433 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1434 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1435 ret
= LTTNG_ERR_UNK
;
1442 case LTTNG_DOMAIN_KERNEL
:
1444 struct ltt_kernel_channel
*kchan
;
1445 struct ltt_kernel_session
*ksess
;
1447 ksess
= session
->kernel_session
;
1450 * If a non-default channel has been created in the
1451 * session, explicitely require that -c chan_name needs
1454 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1455 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1459 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1460 if (kchan
== NULL
) {
1461 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1465 switch (event
->type
) {
1466 case LTTNG_EVENT_ALL
:
1467 case LTTNG_EVENT_TRACEPOINT
:
1468 case LTTNG_EVENT_SYSCALL
:
1469 case LTTNG_EVENT_PROBE
:
1470 case LTTNG_EVENT_FUNCTION
:
1471 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1472 if (event_name
[0] == '\0') {
1473 ret
= event_kernel_disable_event(kchan
,
1476 ret
= event_kernel_disable_event(kchan
,
1477 event_name
, event
->type
);
1479 if (ret
!= LTTNG_OK
) {
1484 ret
= LTTNG_ERR_UNK
;
1488 kernel_wait_quiescent(kernel_tracer_fd
);
1491 case LTTNG_DOMAIN_UST
:
1493 struct ltt_ust_channel
*uchan
;
1494 struct ltt_ust_session
*usess
;
1496 usess
= session
->ust_session
;
1498 if (validate_ust_event_name(event_name
)) {
1499 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1504 * If a non-default channel has been created in the
1505 * session, explicitly require that -c chan_name needs
1508 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1509 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1513 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1515 if (uchan
== NULL
) {
1516 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1520 switch (event
->type
) {
1521 case LTTNG_EVENT_ALL
:
1523 * An empty event name means that everything
1524 * should be disabled.
1526 if (event
->name
[0] == '\0') {
1527 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1529 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1532 if (ret
!= LTTNG_OK
) {
1537 ret
= LTTNG_ERR_UNK
;
1541 DBG3("Disable UST event %s in channel %s completed", event_name
,
1545 case LTTNG_DOMAIN_LOG4J
:
1546 case LTTNG_DOMAIN_JUL
:
1547 case LTTNG_DOMAIN_PYTHON
:
1550 struct ltt_ust_session
*usess
= session
->ust_session
;
1554 switch (event
->type
) {
1555 case LTTNG_EVENT_ALL
:
1558 ret
= LTTNG_ERR_UNK
;
1562 agt
= trace_ust_find_agent(usess
, domain
);
1564 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1568 * An empty event name means that everything
1569 * should be disabled.
1571 if (event
->name
[0] == '\0') {
1572 ret
= event_agent_disable_all(usess
, agt
);
1574 ret
= event_agent_disable(usess
, agt
, event_name
);
1576 if (ret
!= LTTNG_OK
) {
1583 ret
= LTTNG_ERR_UND
;
1596 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1598 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1599 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1601 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1602 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1604 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1605 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1606 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1610 case LTTNG_DOMAIN_KERNEL
:
1611 assert(session
->kernel_session
);
1613 if (session
->kernel_session
->channel_count
== 0) {
1614 /* Create default channel */
1615 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1616 if (ret
!= LTTNG_OK
) {
1619 chan_kern_created
= 1;
1621 /* Add kernel context to kernel tracer */
1622 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1623 if (ret
!= LTTNG_OK
) {
1627 case LTTNG_DOMAIN_JUL
:
1628 case LTTNG_DOMAIN_LOG4J
:
1631 * Validate channel name.
1632 * If no channel name is given and the domain is JUL or LOG4J,
1633 * set it to the appropriate domain-specific channel name. If
1634 * a name is provided but does not match the expexted channel
1635 * name, return an error.
1637 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1638 strcmp(channel_name
,
1639 DEFAULT_JUL_CHANNEL_NAME
)) {
1640 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1642 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1643 strcmp(channel_name
,
1644 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1645 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1648 /* break is _not_ missing here. */
1650 case LTTNG_DOMAIN_UST
:
1652 struct ltt_ust_session
*usess
= session
->ust_session
;
1653 unsigned int chan_count
;
1657 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1658 if (chan_count
== 0) {
1659 struct lttng_channel
*attr
;
1660 /* Create default channel */
1661 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1663 ret
= LTTNG_ERR_FATAL
;
1667 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1668 if (ret
!= LTTNG_OK
) {
1673 chan_ust_created
= 1;
1676 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1677 free(app_ctx_provider_name
);
1679 app_ctx_name
= NULL
;
1680 app_ctx_provider_name
= NULL
;
1681 if (ret
!= LTTNG_OK
) {
1687 ret
= LTTNG_ERR_UND
;
1695 if (chan_kern_created
) {
1696 struct ltt_kernel_channel
*kchan
=
1697 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1698 session
->kernel_session
);
1699 /* Created previously, this should NOT fail. */
1701 kernel_destroy_channel(kchan
);
1704 if (chan_ust_created
) {
1705 struct ltt_ust_channel
*uchan
=
1706 trace_ust_find_channel_by_name(
1707 session
->ust_session
->domain_global
.channels
,
1708 DEFAULT_CHANNEL_NAME
);
1709 /* Created previously, this should NOT fail. */
1711 /* Remove from the channel list of the session. */
1712 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1714 trace_ust_destroy_channel(uchan
);
1717 free(app_ctx_provider_name
);
1722 static int validate_event_name(const char *name
)
1725 const char *c
= name
;
1726 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1727 bool null_terminated
= false;
1730 * Make sure that unescaped wildcards are only used as the last
1731 * character of the event name.
1733 while (c
< event_name_end
) {
1736 null_terminated
= true;
1742 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1743 /* Wildcard is not the last character */
1744 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1753 if (!ret
&& !null_terminated
) {
1754 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1759 static inline bool name_starts_with(const char *name
, const char *prefix
)
1761 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1763 return !strncmp(name
, prefix
, max_cmp_len
);
1766 /* Perform userspace-specific event name validation */
1767 static int validate_ust_event_name(const char *name
)
1777 * Check name against all internal UST event component namespaces used
1780 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1781 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1782 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1791 * Internal version of cmd_enable_event() with a supplemental
1792 * "internal_event" flag which is used to enable internal events which should
1793 * be hidden from clients. Such events are used in the agent implementation to
1794 * enable the events through which all "agent" events are funeled.
1796 static int _cmd_enable_event(struct ltt_session
*session
,
1797 struct lttng_domain
*domain
,
1798 char *channel_name
, struct lttng_event
*event
,
1799 char *filter_expression
,
1800 struct lttng_filter_bytecode
*filter
,
1801 struct lttng_event_exclusion
*exclusion
,
1802 int wpipe
, bool internal_event
)
1804 int ret
, channel_created
= 0;
1805 struct lttng_channel
*attr
;
1809 assert(channel_name
);
1811 /* If we have a filter, we must have its filter expression */
1812 assert(!(!!filter_expression
^ !!filter
));
1814 DBG("Enable event command for event \'%s\'", event
->name
);
1818 ret
= validate_event_name(event
->name
);
1823 switch (domain
->type
) {
1824 case LTTNG_DOMAIN_KERNEL
:
1826 struct ltt_kernel_channel
*kchan
;
1829 * If a non-default channel has been created in the
1830 * session, explicitely require that -c chan_name needs
1833 if (session
->kernel_session
->has_non_default_channel
1834 && channel_name
[0] == '\0') {
1835 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1839 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1840 session
->kernel_session
);
1841 if (kchan
== NULL
) {
1842 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1843 LTTNG_BUFFER_GLOBAL
);
1845 ret
= LTTNG_ERR_FATAL
;
1848 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1850 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1851 if (ret
!= LTTNG_OK
) {
1857 channel_created
= 1;
1860 /* Get the newly created kernel channel pointer */
1861 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1862 session
->kernel_session
);
1863 if (kchan
== NULL
) {
1864 /* This sould not happen... */
1865 ret
= LTTNG_ERR_FATAL
;
1869 switch (event
->type
) {
1870 case LTTNG_EVENT_ALL
:
1872 char *filter_expression_a
= NULL
;
1873 struct lttng_filter_bytecode
*filter_a
= NULL
;
1876 * We need to duplicate filter_expression and filter,
1877 * because ownership is passed to first enable
1880 if (filter_expression
) {
1881 filter_expression_a
= strdup(filter_expression
);
1882 if (!filter_expression_a
) {
1883 ret
= LTTNG_ERR_FATAL
;
1888 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1890 free(filter_expression_a
);
1891 ret
= LTTNG_ERR_FATAL
;
1894 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1896 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1897 ret
= event_kernel_enable_event(kchan
, event
,
1898 filter_expression
, filter
);
1899 /* We have passed ownership */
1900 filter_expression
= NULL
;
1902 if (ret
!= LTTNG_OK
) {
1903 if (channel_created
) {
1904 /* Let's not leak a useless channel. */
1905 kernel_destroy_channel(kchan
);
1907 free(filter_expression_a
);
1911 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1912 ret
= event_kernel_enable_event(kchan
, event
,
1913 filter_expression_a
, filter_a
);
1914 /* We have passed ownership */
1915 filter_expression_a
= NULL
;
1917 if (ret
!= LTTNG_OK
) {
1922 case LTTNG_EVENT_PROBE
:
1923 case LTTNG_EVENT_FUNCTION
:
1924 case LTTNG_EVENT_FUNCTION_ENTRY
:
1925 case LTTNG_EVENT_TRACEPOINT
:
1926 ret
= event_kernel_enable_event(kchan
, event
,
1927 filter_expression
, filter
);
1928 /* We have passed ownership */
1929 filter_expression
= NULL
;
1931 if (ret
!= LTTNG_OK
) {
1932 if (channel_created
) {
1933 /* Let's not leak a useless channel. */
1934 kernel_destroy_channel(kchan
);
1939 case LTTNG_EVENT_SYSCALL
:
1940 ret
= event_kernel_enable_event(kchan
, event
,
1941 filter_expression
, filter
);
1942 /* We have passed ownership */
1943 filter_expression
= NULL
;
1945 if (ret
!= LTTNG_OK
) {
1950 ret
= LTTNG_ERR_UNK
;
1954 kernel_wait_quiescent(kernel_tracer_fd
);
1957 case LTTNG_DOMAIN_UST
:
1959 struct ltt_ust_channel
*uchan
;
1960 struct ltt_ust_session
*usess
= session
->ust_session
;
1965 * If a non-default channel has been created in the
1966 * session, explicitely require that -c chan_name needs
1969 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1970 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1974 /* Get channel from global UST domain */
1975 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1977 if (uchan
== NULL
) {
1978 /* Create default channel */
1979 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1980 usess
->buffer_type
);
1982 ret
= LTTNG_ERR_FATAL
;
1985 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1987 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1988 if (ret
!= LTTNG_OK
) {
1994 /* Get the newly created channel reference back */
1995 uchan
= trace_ust_find_channel_by_name(
1996 usess
->domain_global
.channels
, channel_name
);
2000 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
2002 * Don't allow users to add UST events to channels which
2003 * are assigned to a userspace subdomain (JUL, Log4J,
2006 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
2010 if (!internal_event
) {
2012 * Ensure the event name is not reserved for internal
2015 ret
= validate_ust_event_name(event
->name
);
2017 WARN("Userspace event name %s failed validation.",
2019 event
->name
: "NULL");
2020 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
2025 /* At this point, the session and channel exist on the tracer */
2026 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
2027 filter_expression
, filter
, exclusion
,
2029 /* We have passed ownership */
2030 filter_expression
= NULL
;
2033 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2034 goto already_enabled
;
2035 } else if (ret
!= LTTNG_OK
) {
2040 case LTTNG_DOMAIN_LOG4J
:
2041 case LTTNG_DOMAIN_JUL
:
2042 case LTTNG_DOMAIN_PYTHON
:
2044 const char *default_event_name
, *default_chan_name
;
2046 struct lttng_event uevent
;
2047 struct lttng_domain tmp_dom
;
2048 struct ltt_ust_session
*usess
= session
->ust_session
;
2052 agt
= trace_ust_find_agent(usess
, domain
->type
);
2054 agt
= agent_create(domain
->type
);
2056 ret
= LTTNG_ERR_NOMEM
;
2059 agent_add(agt
, usess
->agents
);
2062 /* Create the default tracepoint. */
2063 memset(&uevent
, 0, sizeof(uevent
));
2064 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
2065 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2066 default_event_name
= event_get_default_agent_ust_name(
2068 if (!default_event_name
) {
2069 ret
= LTTNG_ERR_FATAL
;
2072 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
2073 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
2076 * The domain type is changed because we are about to enable the
2077 * default channel and event for the JUL domain that are hardcoded.
2078 * This happens in the UST domain.
2080 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
2081 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
2083 switch (domain
->type
) {
2084 case LTTNG_DOMAIN_LOG4J
:
2085 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
2087 case LTTNG_DOMAIN_JUL
:
2088 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
2090 case LTTNG_DOMAIN_PYTHON
:
2091 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
2094 /* The switch/case we are in makes this impossible */
2099 char *filter_expression_copy
= NULL
;
2100 struct lttng_filter_bytecode
*filter_copy
= NULL
;
2103 const size_t filter_size
= sizeof(
2104 struct lttng_filter_bytecode
)
2107 filter_copy
= zmalloc(filter_size
);
2109 ret
= LTTNG_ERR_NOMEM
;
2112 memcpy(filter_copy
, filter
, filter_size
);
2114 filter_expression_copy
=
2115 strdup(filter_expression
);
2116 if (!filter_expression
) {
2117 ret
= LTTNG_ERR_NOMEM
;
2120 if (!filter_expression_copy
|| !filter_copy
) {
2121 free(filter_expression_copy
);
2127 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
2128 (char *) default_chan_name
,
2129 &uevent
, filter_expression_copy
,
2130 filter_copy
, NULL
, wpipe
);
2133 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
2134 goto already_enabled
;
2135 } else if (ret
!= LTTNG_OK
) {
2139 /* The wild card * means that everything should be enabled. */
2140 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
2141 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
2144 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2148 filter_expression
= NULL
;
2149 if (ret
!= LTTNG_OK
) {
2156 ret
= LTTNG_ERR_UND
;
2164 free(filter_expression
);
2172 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2173 * We own filter, exclusion, and filter_expression.
2175 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2176 char *channel_name
, struct lttng_event
*event
,
2177 char *filter_expression
,
2178 struct lttng_filter_bytecode
*filter
,
2179 struct lttng_event_exclusion
*exclusion
,
2182 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2183 filter_expression
, filter
, exclusion
, wpipe
, false);
2187 * Enable an event which is internal to LTTng. An internal should
2188 * never be made visible to clients and are immune to checks such as
2191 static int cmd_enable_event_internal(struct ltt_session
*session
,
2192 struct lttng_domain
*domain
,
2193 char *channel_name
, struct lttng_event
*event
,
2194 char *filter_expression
,
2195 struct lttng_filter_bytecode
*filter
,
2196 struct lttng_event_exclusion
*exclusion
,
2199 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2200 filter_expression
, filter
, exclusion
, wpipe
, true);
2204 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2206 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2207 struct lttng_event
**events
)
2210 ssize_t nb_events
= 0;
2213 case LTTNG_DOMAIN_KERNEL
:
2214 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2215 if (nb_events
< 0) {
2216 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2220 case LTTNG_DOMAIN_UST
:
2221 nb_events
= ust_app_list_events(events
);
2222 if (nb_events
< 0) {
2223 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2227 case LTTNG_DOMAIN_LOG4J
:
2228 case LTTNG_DOMAIN_JUL
:
2229 case LTTNG_DOMAIN_PYTHON
:
2230 nb_events
= agent_list_events(events
, domain
);
2231 if (nb_events
< 0) {
2232 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2237 ret
= LTTNG_ERR_UND
;
2244 /* Return negative value to differentiate return code */
2249 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2251 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2252 struct lttng_event_field
**fields
)
2255 ssize_t nb_fields
= 0;
2258 case LTTNG_DOMAIN_UST
:
2259 nb_fields
= ust_app_list_event_fields(fields
);
2260 if (nb_fields
< 0) {
2261 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2265 case LTTNG_DOMAIN_KERNEL
:
2266 default: /* fall-through */
2267 ret
= LTTNG_ERR_UND
;
2274 /* Return negative value to differentiate return code */
2278 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2280 return syscall_table_list(events
);
2284 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2286 * Called with session lock held.
2288 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2289 enum lttng_domain_type domain
, int32_t **pids
)
2292 ssize_t nr_pids
= 0;
2295 case LTTNG_DOMAIN_KERNEL
:
2297 struct ltt_kernel_session
*ksess
;
2299 ksess
= session
->kernel_session
;
2300 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2302 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2307 case LTTNG_DOMAIN_UST
:
2309 struct ltt_ust_session
*usess
;
2311 usess
= session
->ust_session
;
2312 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2314 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2319 case LTTNG_DOMAIN_LOG4J
:
2320 case LTTNG_DOMAIN_JUL
:
2321 case LTTNG_DOMAIN_PYTHON
:
2323 ret
= LTTNG_ERR_UND
;
2330 /* Return negative value to differentiate return code */
2335 * Command LTTNG_START_TRACE processed by the client thread.
2337 * Called with session mutex held.
2339 int cmd_start_trace(struct ltt_session
*session
)
2342 unsigned long nb_chan
= 0;
2343 struct ltt_kernel_session
*ksession
;
2344 struct ltt_ust_session
*usess
;
2348 /* Ease our life a bit ;) */
2349 ksession
= session
->kernel_session
;
2350 usess
= session
->ust_session
;
2352 /* Is the session already started? */
2353 if (session
->active
) {
2354 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2359 * Starting a session without channel is useless since after that it's not
2360 * possible to enable channel thus inform the client.
2362 if (usess
&& usess
->domain_global
.channels
) {
2363 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2366 nb_chan
+= ksession
->channel_count
;
2369 ret
= LTTNG_ERR_NO_CHANNEL
;
2373 /* Kernel tracing */
2374 if (ksession
!= NULL
) {
2375 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2376 if (ret
!= LTTNG_OK
) {
2381 /* Flag session that trace should start automatically */
2384 * Even though the start trace might fail, flag this session active so
2385 * other application coming in are started by default.
2389 ret
= ust_app_start_trace_all(usess
);
2391 ret
= LTTNG_ERR_UST_START_FAIL
;
2396 /* Flag this after a successful start. */
2397 session
->has_been_started
= 1;
2398 session
->active
= 1;
2407 * Command LTTNG_STOP_TRACE processed by the client thread.
2409 int cmd_stop_trace(struct ltt_session
*session
)
2412 struct ltt_kernel_channel
*kchan
;
2413 struct ltt_kernel_session
*ksession
;
2414 struct ltt_ust_session
*usess
;
2419 ksession
= session
->kernel_session
;
2420 usess
= session
->ust_session
;
2422 /* Session is not active. Skip everythong and inform the client. */
2423 if (!session
->active
) {
2424 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2429 if (ksession
&& ksession
->active
) {
2430 DBG("Stop kernel tracing");
2432 /* Flush metadata if exist */
2433 if (ksession
->metadata_stream_fd
>= 0) {
2434 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2436 ERR("Kernel metadata flush failed");
2440 /* Flush all buffers before stopping */
2441 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2442 ret
= kernel_flush_buffer(kchan
);
2444 ERR("Kernel flush buffer error");
2448 ret
= kernel_stop_session(ksession
);
2450 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2454 kernel_wait_quiescent(kernel_tracer_fd
);
2456 ksession
->active
= 0;
2459 if (usess
&& usess
->active
) {
2461 * Even though the stop trace might fail, flag this session inactive so
2462 * other application coming in are not started by default.
2466 ret
= ust_app_stop_trace_all(usess
);
2468 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2473 /* Flag inactive after a successful stop. */
2474 session
->active
= 0;
2482 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2484 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2485 struct lttng_uri
*uris
)
2488 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2489 struct ltt_ust_session
*usess
= session
->ust_session
;
2495 /* Can't set consumer URI if the session is active. */
2496 if (session
->active
) {
2497 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2501 /* Set the "global" consumer URIs */
2502 for (i
= 0; i
< nb_uri
; i
++) {
2503 ret
= add_uri_to_consumer(session
->consumer
,
2504 &uris
[i
], 0, session
->name
);
2505 if (ret
!= LTTNG_OK
) {
2510 /* Set UST session URIs */
2511 if (session
->ust_session
) {
2512 for (i
= 0; i
< nb_uri
; i
++) {
2513 ret
= add_uri_to_consumer(
2514 session
->ust_session
->consumer
,
2515 &uris
[i
], LTTNG_DOMAIN_UST
,
2517 if (ret
!= LTTNG_OK
) {
2523 /* Set kernel session URIs */
2524 if (session
->kernel_session
) {
2525 for (i
= 0; i
< nb_uri
; i
++) {
2526 ret
= add_uri_to_consumer(
2527 session
->kernel_session
->consumer
,
2528 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2530 if (ret
!= LTTNG_OK
) {
2537 * Make sure to set the session in output mode after we set URI since a
2538 * session can be created without URL (thus flagged in no output mode).
2540 session
->output_traces
= 1;
2542 ksess
->output_traces
= 1;
2546 usess
->output_traces
= 1;
2557 * Command LTTNG_CREATE_SESSION processed by the client thread.
2559 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2560 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2563 struct ltt_session
*session
;
2569 * Verify if the session already exist
2571 * XXX: There is no need for the session lock list here since the caller
2572 * (process_client_msg) is holding it. We might want to change that so a
2573 * single command does not lock the entire session list.
2575 session
= session_find_by_name(name
);
2576 if (session
!= NULL
) {
2577 ret
= LTTNG_ERR_EXIST_SESS
;
2581 /* Create tracing session in the registry */
2582 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2583 LTTNG_SOCK_GET_GID_CRED(creds
));
2584 if (ret
!= LTTNG_OK
) {
2589 * Get the newly created session pointer back
2591 * XXX: There is no need for the session lock list here since the caller
2592 * (process_client_msg) is holding it. We might want to change that so a
2593 * single command does not lock the entire session list.
2595 session
= session_find_by_name(name
);
2598 session
->live_timer
= live_timer
;
2599 /* Create default consumer output for the session not yet created. */
2600 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2601 if (session
->consumer
== NULL
) {
2602 ret
= LTTNG_ERR_FATAL
;
2603 goto consumer_error
;
2607 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2608 if (ret
!= LTTNG_OK
) {
2609 goto consumer_error
;
2611 session
->output_traces
= 1;
2613 session
->output_traces
= 0;
2614 DBG2("Session %s created with no output", session
->name
);
2617 session
->consumer
->enabled
= 1;
2622 session_destroy(session
);
2629 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2631 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2632 size_t nb_uri
, lttng_sock_cred
*creds
)
2635 struct ltt_session
*session
;
2636 struct snapshot_output
*new_output
= NULL
;
2642 * Create session in no output mode with URIs set to NULL. The uris we've
2643 * received are for a default snapshot output if one.
2645 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2646 if (ret
!= LTTNG_OK
) {
2650 /* Get the newly created session pointer back. This should NEVER fail. */
2651 session
= session_find_by_name(name
);
2654 /* Flag session for snapshot mode. */
2655 session
->snapshot_mode
= 1;
2657 /* Skip snapshot output creation if no URI is given. */
2662 new_output
= snapshot_output_alloc();
2664 ret
= LTTNG_ERR_NOMEM
;
2665 goto error_snapshot_alloc
;
2668 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2669 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2671 if (ret
== -ENOMEM
) {
2672 ret
= LTTNG_ERR_NOMEM
;
2674 ret
= LTTNG_ERR_INVALID
;
2676 goto error_snapshot
;
2680 snapshot_add_output(&session
->snapshot
, new_output
);
2687 snapshot_output_destroy(new_output
);
2688 error_snapshot_alloc
:
2689 session_destroy(session
);
2695 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2697 * Called with session lock held.
2699 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2702 struct ltt_ust_session
*usess
;
2703 struct ltt_kernel_session
*ksess
;
2708 usess
= session
->ust_session
;
2709 ksess
= session
->kernel_session
;
2711 /* Clean kernel session teardown */
2712 kernel_destroy_session(ksess
);
2714 /* UST session teardown */
2716 /* Close any relayd session */
2717 consumer_output_send_destroy_relayd(usess
->consumer
);
2719 /* Destroy every UST application related to this session. */
2720 ret
= ust_app_destroy_trace_all(usess
);
2722 ERR("Error in ust_app_destroy_trace_all");
2725 /* Clean up the rest. */
2726 trace_ust_destroy_session(usess
);
2730 * Must notify the kernel thread here to update it's poll set in order to
2731 * remove the channel(s)' fd just destroyed.
2733 ret
= notify_thread_pipe(wpipe
);
2735 PERROR("write kernel poll pipe");
2738 ret
= session_destroy(session
);
2744 * Command LTTNG_CALIBRATE processed by the client thread.
2746 int cmd_calibrate(enum lttng_domain_type domain
,
2747 struct lttng_calibrate
*calibrate
)
2752 case LTTNG_DOMAIN_KERNEL
:
2754 struct lttng_kernel_calibrate kcalibrate
;
2756 switch (calibrate
->type
) {
2757 case LTTNG_CALIBRATE_FUNCTION
:
2759 /* Default and only possible calibrate option. */
2760 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2764 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2766 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2771 case LTTNG_DOMAIN_UST
:
2773 struct lttng_ust_calibrate ucalibrate
;
2775 switch (calibrate
->type
) {
2776 case LTTNG_CALIBRATE_FUNCTION
:
2778 /* Default and only possible calibrate option. */
2779 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2783 ret
= ust_app_calibrate_glb(&ucalibrate
);
2785 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2791 ret
= LTTNG_ERR_UND
;
2802 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2804 int cmd_register_consumer(struct ltt_session
*session
,
2805 enum lttng_domain_type domain
, const char *sock_path
,
2806 struct consumer_data
*cdata
)
2809 struct consumer_socket
*socket
= NULL
;
2816 case LTTNG_DOMAIN_KERNEL
:
2818 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2822 /* Can't register a consumer if there is already one */
2823 if (ksess
->consumer_fds_sent
!= 0) {
2824 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2828 sock
= lttcomm_connect_unix_sock(sock_path
);
2830 ret
= LTTNG_ERR_CONNECT_FAIL
;
2833 cdata
->cmd_sock
= sock
;
2835 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2836 if (socket
== NULL
) {
2839 PERROR("close register consumer");
2841 cdata
->cmd_sock
= -1;
2842 ret
= LTTNG_ERR_FATAL
;
2846 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2847 if (socket
->lock
== NULL
) {
2848 PERROR("zmalloc pthread mutex");
2849 ret
= LTTNG_ERR_FATAL
;
2852 pthread_mutex_init(socket
->lock
, NULL
);
2853 socket
->registered
= 1;
2856 consumer_add_socket(socket
, ksess
->consumer
);
2859 pthread_mutex_lock(&cdata
->pid_mutex
);
2861 pthread_mutex_unlock(&cdata
->pid_mutex
);
2866 /* TODO: Userspace tracing */
2867 ret
= LTTNG_ERR_UND
;
2875 consumer_destroy_socket(socket
);
2881 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2883 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2884 struct lttng_domain
**domains
)
2889 struct lttng_ht_iter iter
;
2891 if (session
->kernel_session
!= NULL
) {
2892 DBG3("Listing domains found kernel domain");
2896 if (session
->ust_session
!= NULL
) {
2897 DBG3("Listing domains found UST global domain");
2901 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2903 if (agt
->being_used
) {
2914 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2915 if (*domains
== NULL
) {
2916 ret
= LTTNG_ERR_FATAL
;
2920 if (session
->kernel_session
!= NULL
) {
2921 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2923 /* Kernel session buffer type is always GLOBAL */
2924 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2929 if (session
->ust_session
!= NULL
) {
2930 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2931 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2935 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2937 if (agt
->being_used
) {
2938 (*domains
)[index
].type
= agt
->domain
;
2939 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2949 /* Return negative value to differentiate return code */
2955 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2957 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2958 struct ltt_session
*session
, struct lttng_channel
**channels
)
2960 ssize_t nb_chan
= 0, payload_size
= 0, ret
;
2963 case LTTNG_DOMAIN_KERNEL
:
2964 if (session
->kernel_session
!= NULL
) {
2965 nb_chan
= session
->kernel_session
->channel_count
;
2967 DBG3("Number of kernel channels %zd", nb_chan
);
2969 ret
= -LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2973 case LTTNG_DOMAIN_UST
:
2974 if (session
->ust_session
!= NULL
) {
2976 nb_chan
= lttng_ht_get_count(
2977 session
->ust_session
->domain_global
.channels
);
2980 DBG3("Number of UST global channels %zd", nb_chan
);
2982 ret
= -LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2987 ret
= -LTTNG_ERR_UND
;
2992 const size_t channel_size
= sizeof(struct lttng_channel
) +
2993 sizeof(struct lttcomm_channel_extended
);
2994 struct lttcomm_channel_extended
*channel_exts
;
2996 payload_size
= nb_chan
* channel_size
;
2997 *channels
= zmalloc(payload_size
);
2998 if (*channels
== NULL
) {
2999 ret
= -LTTNG_ERR_FATAL
;
3003 channel_exts
= ((void *) *channels
) +
3004 (nb_chan
* sizeof(struct lttng_channel
));
3005 list_lttng_channels(domain
, session
, *channels
, channel_exts
);
3016 * Command LTTNG_LIST_EVENTS processed by the client thread.
3018 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
3019 struct ltt_session
*session
, char *channel_name
,
3020 struct lttng_event
**events
, size_t *total_size
)
3023 ssize_t nb_event
= 0;
3026 case LTTNG_DOMAIN_KERNEL
:
3027 if (session
->kernel_session
!= NULL
) {
3028 nb_event
= list_lttng_kernel_events(channel_name
,
3029 session
->kernel_session
, events
,
3033 case LTTNG_DOMAIN_UST
:
3035 if (session
->ust_session
!= NULL
) {
3036 nb_event
= list_lttng_ust_global_events(channel_name
,
3037 &session
->ust_session
->domain_global
, events
,
3042 case LTTNG_DOMAIN_LOG4J
:
3043 case LTTNG_DOMAIN_JUL
:
3044 case LTTNG_DOMAIN_PYTHON
:
3045 if (session
->ust_session
) {
3046 struct lttng_ht_iter iter
;
3050 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
3051 &iter
.iter
, agt
, node
.node
) {
3052 if (agt
->domain
== domain
) {
3053 nb_event
= list_lttng_agent_events(
3063 ret
= LTTNG_ERR_UND
;
3070 /* Return negative value to differentiate return code */
3075 * Using the session list, filled a lttng_session array to send back to the
3076 * client for session listing.
3078 * The session list lock MUST be acquired before calling this function. Use
3079 * session_lock_list() and session_unlock_list().
3081 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
3086 struct ltt_session
*session
;
3087 struct ltt_session_list
*list
= session_get_list();
3089 DBG("Getting all available session for UID %d GID %d",
3092 * Iterate over session list and append data after the control struct in
3095 cds_list_for_each_entry(session
, &list
->head
, list
) {
3097 * Only list the sessions the user can control.
3099 if (!session_access_ok(session
, uid
, gid
)) {
3103 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3104 struct ltt_ust_session
*usess
= session
->ust_session
;
3106 if (session
->consumer
->type
== CONSUMER_DST_NET
||
3107 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
3108 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
3109 ret
= build_network_session_path(sessions
[i
].path
,
3110 sizeof(sessions
[i
].path
), session
);
3112 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
3113 session
->consumer
->dst
.trace_path
);
3116 PERROR("snprintf session path");
3120 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
3121 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
3122 sessions
[i
].enabled
= session
->active
;
3123 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
3124 sessions
[i
].live_timer_interval
= session
->live_timer
;
3130 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3131 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3133 int cmd_data_pending(struct ltt_session
*session
)
3136 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3137 struct ltt_ust_session
*usess
= session
->ust_session
;
3141 /* Session MUST be stopped to ask for data availability. */
3142 if (session
->active
) {
3143 ret
= LTTNG_ERR_SESSION_STARTED
;
3147 * If stopped, just make sure we've started before else the above call
3148 * will always send that there is data pending.
3150 * The consumer assumes that when the data pending command is received,
3151 * the trace has been started before or else no output data is written
3152 * by the streams which is a condition for data pending. So, this is
3153 * *VERY* important that we don't ask the consumer before a start
3156 if (!session
->has_been_started
) {
3162 if (ksess
&& ksess
->consumer
) {
3163 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3165 /* Data is still being extracted for the kernel. */
3170 if (usess
&& usess
->consumer
) {
3171 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3173 /* Data is still being extracted for the kernel. */
3178 /* Data is ready to be read by a viewer */
3186 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3188 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3190 int cmd_snapshot_add_output(struct ltt_session
*session
,
3191 struct lttng_snapshot_output
*output
, uint32_t *id
)
3194 struct snapshot_output
*new_output
;
3199 DBG("Cmd snapshot add output for session %s", session
->name
);
3202 * Permission denied to create an output if the session is not
3203 * set in no output mode.
3205 if (session
->output_traces
) {
3206 ret
= LTTNG_ERR_EPERM
;
3210 /* Only one output is allowed until we have the "tee" feature. */
3211 if (session
->snapshot
.nb_output
== 1) {
3212 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3216 new_output
= snapshot_output_alloc();
3218 ret
= LTTNG_ERR_NOMEM
;
3222 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3223 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3224 &session
->snapshot
);
3226 if (ret
== -ENOMEM
) {
3227 ret
= LTTNG_ERR_NOMEM
;
3229 ret
= LTTNG_ERR_INVALID
;
3235 snapshot_add_output(&session
->snapshot
, new_output
);
3237 *id
= new_output
->id
;
3244 snapshot_output_destroy(new_output
);
3250 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3252 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3254 int cmd_snapshot_del_output(struct ltt_session
*session
,
3255 struct lttng_snapshot_output
*output
)
3258 struct snapshot_output
*sout
= NULL
;
3266 * Permission denied to create an output if the session is not
3267 * set in no output mode.
3269 if (session
->output_traces
) {
3270 ret
= LTTNG_ERR_EPERM
;
3275 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3277 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3278 } else if (*output
->name
!= '\0') {
3279 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3281 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3284 ret
= LTTNG_ERR_INVALID
;
3288 snapshot_delete_output(&session
->snapshot
, sout
);
3289 snapshot_output_destroy(sout
);
3298 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3300 * If no output is available, outputs is untouched and 0 is returned.
3302 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3304 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3305 struct lttng_snapshot_output
**outputs
)
3308 struct lttng_snapshot_output
*list
= NULL
;
3309 struct lttng_ht_iter iter
;
3310 struct snapshot_output
*output
;
3315 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3318 * Permission denied to create an output if the session is not
3319 * set in no output mode.
3321 if (session
->output_traces
) {
3322 ret
= -LTTNG_ERR_EPERM
;
3326 if (session
->snapshot
.nb_output
== 0) {
3331 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3333 ret
= -LTTNG_ERR_NOMEM
;
3337 /* Copy list from session to the new list object. */
3339 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3340 output
, node
.node
) {
3341 assert(output
->consumer
);
3342 list
[idx
].id
= output
->id
;
3343 list
[idx
].max_size
= output
->max_size
;
3344 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
3345 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3346 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
3347 sizeof(list
[idx
].ctrl_url
));
3350 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3351 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3353 ret
= -LTTNG_ERR_NOMEM
;
3358 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3359 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3361 ret
= -LTTNG_ERR_NOMEM
;
3370 ret
= session
->snapshot
.nb_output
;
3378 * Check if we can regenerate the metadata for this session.
3379 * Only kernel, UST per-uid and non-live sessions are supported.
3381 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
3384 int check_metadata_regenerate_support(struct ltt_session
*session
)
3390 if (session
->live_timer
!= 0) {
3391 ret
= LTTNG_ERR_LIVE_SESSION
;
3394 if (!session
->active
) {
3395 ret
= LTTNG_ERR_SESSION_NOT_STARTED
;
3398 if (session
->ust_session
) {
3399 switch (session
->ust_session
->buffer_type
) {
3400 case LTTNG_BUFFER_PER_UID
:
3402 case LTTNG_BUFFER_PER_PID
:
3403 ret
= LTTNG_ERR_PER_PID_SESSION
;
3407 ret
= LTTNG_ERR_UNK
;
3411 if (session
->consumer
->type
== CONSUMER_DST_NET
&&
3412 session
->consumer
->relay_minor_version
< 8) {
3413 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
3423 int ust_metadata_regenerate(struct ltt_ust_session
*usess
)
3426 struct buffer_reg_uid
*uid_reg
= NULL
;
3427 struct buffer_reg_session
*session_reg
= NULL
;
3430 cds_list_for_each_entry(uid_reg
, &usess
->buffer_reg_uid_list
, lnode
) {
3431 struct ust_registry_session
*registry
;
3432 struct ust_registry_channel
*chan
;
3433 struct lttng_ht_iter iter_chan
;
3435 session_reg
= uid_reg
->registry
;
3436 registry
= session_reg
->reg
.ust
;
3438 pthread_mutex_lock(®istry
->lock
);
3439 registry
->metadata_len_sent
= 0;
3440 memset(registry
->metadata
, 0, registry
->metadata_alloc_len
);
3441 registry
->metadata_len
= 0;
3442 registry
->metadata_version
++;
3443 ret
= ust_metadata_session_statedump(registry
, NULL
,
3444 registry
->major
, registry
->minor
);
3446 pthread_mutex_unlock(®istry
->lock
);
3447 ERR("Failed to generate session metadata (err = %d)",
3451 cds_lfht_for_each_entry(registry
->channels
->ht
, &iter_chan
.iter
,
3453 struct ust_registry_event
*event
;
3454 struct lttng_ht_iter iter_event
;
3456 ret
= ust_metadata_channel_statedump(registry
, chan
);
3458 pthread_mutex_unlock(®istry
->lock
);
3459 ERR("Failed to generate channel metadata "
3463 cds_lfht_for_each_entry(chan
->ht
->ht
, &iter_event
.iter
,
3465 ret
= ust_metadata_event_statedump(registry
,
3468 pthread_mutex_unlock(®istry
->lock
);
3469 ERR("Failed to generate event metadata "
3475 pthread_mutex_unlock(®istry
->lock
);
3484 * Command LTTNG_METADATA_REGENERATE from the lttng-ctl library.
3486 * Ask the consumer to truncate the existing metadata file(s) and
3487 * then regenerate the metadata. Live and per-pid sessions are not
3488 * supported and return an error.
3490 * Return 0 on success or else a LTTNG_ERR code.
3492 int cmd_metadata_regenerate(struct ltt_session
*session
)
3498 ret
= check_metadata_regenerate_support(session
);
3503 if (session
->kernel_session
) {
3504 ret
= kernctl_session_metadata_regenerate(
3505 session
->kernel_session
->fd
);
3507 ERR("Failed to regenerate the kernel metadata");
3512 if (session
->ust_session
) {
3513 ret
= ust_metadata_regenerate(session
->ust_session
);
3515 ERR("Failed to regenerate the UST metadata");
3519 DBG("Cmd metadata regenerate for session %s", session
->name
);
3528 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3529 * snapshot output is *not* set with a remote destination.
3531 * Return 0 on success or a LTTNG_ERR code.
3533 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3534 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3537 struct lttng_ht_iter iter
;
3538 struct consumer_socket
*socket
;
3541 assert(snap_output
);
3544 DBG2("Set relayd object from snapshot output");
3546 /* Ignore if snapshot consumer output is not network. */
3547 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3552 * For each consumer socket, create and send the relayd object of the
3556 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3557 socket
, node
.node
) {
3558 ret
= send_consumer_relayd_sockets(0, session
->id
,
3559 snap_output
->consumer
, socket
,
3560 session
->name
, session
->hostname
,
3561 session
->live_timer
);
3562 if (ret
!= LTTNG_OK
) {
3574 * Record a kernel snapshot.
3576 * Return LTTNG_OK on success or a LTTNG_ERR code.
3578 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3579 struct snapshot_output
*output
, struct ltt_session
*session
,
3580 int wait
, uint64_t nb_packets_per_stream
)
3588 /* Get the datetime for the snapshot output directory. */
3589 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3590 sizeof(output
->datetime
));
3592 ret
= LTTNG_ERR_INVALID
;
3597 * Copy kernel session sockets so we can communicate with the right
3598 * consumer for the snapshot record command.
3600 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3602 ret
= LTTNG_ERR_NOMEM
;
3606 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3607 if (ret
!= LTTNG_OK
) {
3608 goto error_snapshot
;
3611 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3612 if (ret
!= LTTNG_OK
) {
3613 goto error_snapshot
;
3620 /* Clean up copied sockets so this output can use some other later on. */
3621 consumer_destroy_output_sockets(output
->consumer
);
3628 * Record a UST snapshot.
3630 * Return 0 on success or a LTTNG_ERR error code.
3632 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3633 struct snapshot_output
*output
, struct ltt_session
*session
,
3634 int wait
, uint64_t nb_packets_per_stream
)
3642 /* Get the datetime for the snapshot output directory. */
3643 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3644 sizeof(output
->datetime
));
3646 ret
= LTTNG_ERR_INVALID
;
3651 * Copy UST session sockets so we can communicate with the right
3652 * consumer for the snapshot record command.
3654 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3656 ret
= LTTNG_ERR_NOMEM
;
3660 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3661 if (ret
!= LTTNG_OK
) {
3662 goto error_snapshot
;
3665 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3669 ret
= LTTNG_ERR_INVALID
;
3672 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3675 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3678 goto error_snapshot
;
3684 /* Clean up copied sockets so this output can use some other later on. */
3685 consumer_destroy_output_sockets(output
->consumer
);
3691 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3692 uint64_t cur_nr_packets
)
3694 uint64_t tot_size
= 0;
3696 if (session
->kernel_session
) {
3697 struct ltt_kernel_channel
*chan
;
3698 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3700 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3701 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3703 * Don't take channel into account if we
3704 * already grab all its packets.
3708 tot_size
+= chan
->channel
->attr
.subbuf_size
3709 * chan
->stream_count
;
3713 if (session
->ust_session
) {
3714 struct ltt_ust_session
*usess
= session
->ust_session
;
3716 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3724 * Calculate the number of packets we can grab from each stream that
3725 * fits within the overall snapshot max size.
3727 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3728 * the number of packets per stream.
3730 * TODO: this approach is not perfect: we consider the worse case
3731 * (packet filling the sub-buffers) as an upper bound, but we could do
3732 * better if we do this calculation while we actually grab the packet
3733 * content: we would know how much padding we don't actually store into
3736 * This algorithm is currently bounded by the number of packets per
3739 * Since we call this algorithm before actually grabbing the data, it's
3740 * an approximation: for instance, applications could appear/disappear
3741 * in between this call and actually grabbing data.
3744 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3747 uint64_t cur_nb_packets
= 0;
3750 return 0; /* Infinite */
3753 size_left
= max_size
;
3755 uint64_t one_more_packet_tot_size
;
3757 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3759 if (!one_more_packet_tot_size
) {
3760 /* We are already grabbing all packets. */
3763 size_left
-= one_more_packet_tot_size
;
3764 if (size_left
< 0) {
3769 if (!cur_nb_packets
) {
3770 /* Not enough room to grab one packet of each stream, error. */
3773 return cur_nb_packets
;
3777 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3779 * The wait parameter is ignored so this call always wait for the snapshot to
3780 * complete before returning.
3782 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3784 int cmd_snapshot_record(struct ltt_session
*session
,
3785 struct lttng_snapshot_output
*output
, int wait
)
3788 unsigned int use_tmp_output
= 0;
3789 struct snapshot_output tmp_output
;
3790 unsigned int snapshot_success
= 0;
3795 DBG("Cmd snapshot record for session %s", session
->name
);
3798 * Permission denied to create an output if the session is not
3799 * set in no output mode.
3801 if (session
->output_traces
) {
3802 ret
= LTTNG_ERR_EPERM
;
3806 /* The session needs to be started at least once. */
3807 if (!session
->has_been_started
) {
3808 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3812 /* Use temporary output for the session. */
3813 if (*output
->ctrl_url
!= '\0') {
3814 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3815 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3818 if (ret
== -ENOMEM
) {
3819 ret
= LTTNG_ERR_NOMEM
;
3821 ret
= LTTNG_ERR_INVALID
;
3825 /* Use the global session count for the temporary snapshot. */
3826 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3830 if (session
->kernel_session
) {
3831 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3833 if (use_tmp_output
) {
3834 int64_t nb_packets_per_stream
;
3836 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3837 tmp_output
.max_size
);
3838 if (nb_packets_per_stream
< 0) {
3839 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3842 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3843 wait
, nb_packets_per_stream
);
3844 if (ret
!= LTTNG_OK
) {
3847 snapshot_success
= 1;
3849 struct snapshot_output
*sout
;
3850 struct lttng_ht_iter iter
;
3853 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3854 &iter
.iter
, sout
, node
.node
) {
3855 int64_t nb_packets_per_stream
;
3858 * Make a local copy of the output and assign the possible
3859 * temporary value given by the caller.
3861 memset(&tmp_output
, 0, sizeof(tmp_output
));
3862 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3864 if (output
->max_size
!= (uint64_t) -1ULL) {
3865 tmp_output
.max_size
= output
->max_size
;
3868 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3869 tmp_output
.max_size
);
3870 if (nb_packets_per_stream
< 0) {
3871 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3875 /* Use temporary name. */
3876 if (*output
->name
!= '\0') {
3877 strncpy(tmp_output
.name
, output
->name
,
3878 sizeof(tmp_output
.name
));
3881 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3883 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3884 session
, wait
, nb_packets_per_stream
);
3885 if (ret
!= LTTNG_OK
) {
3889 snapshot_success
= 1;
3895 if (session
->ust_session
) {
3896 struct ltt_ust_session
*usess
= session
->ust_session
;
3898 if (use_tmp_output
) {
3899 int64_t nb_packets_per_stream
;
3901 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3902 tmp_output
.max_size
);
3903 if (nb_packets_per_stream
< 0) {
3904 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3907 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3908 wait
, nb_packets_per_stream
);
3909 if (ret
!= LTTNG_OK
) {
3912 snapshot_success
= 1;
3914 struct snapshot_output
*sout
;
3915 struct lttng_ht_iter iter
;
3918 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3919 &iter
.iter
, sout
, node
.node
) {
3920 int64_t nb_packets_per_stream
;
3923 * Make a local copy of the output and assign the possible
3924 * temporary value given by the caller.
3926 memset(&tmp_output
, 0, sizeof(tmp_output
));
3927 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3929 if (output
->max_size
!= (uint64_t) -1ULL) {
3930 tmp_output
.max_size
= output
->max_size
;
3933 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3934 tmp_output
.max_size
);
3935 if (nb_packets_per_stream
< 0) {
3936 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3941 /* Use temporary name. */
3942 if (*output
->name
!= '\0') {
3943 strncpy(tmp_output
.name
, output
->name
,
3944 sizeof(tmp_output
.name
));
3947 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3949 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3950 wait
, nb_packets_per_stream
);
3951 if (ret
!= LTTNG_OK
) {
3955 snapshot_success
= 1;
3961 if (snapshot_success
) {
3962 session
->snapshot
.nb_snapshot
++;
3964 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3972 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3974 int cmd_set_session_shm_path(struct ltt_session
*session
,
3975 const char *shm_path
)
3981 * Can only set shm path before session is started.
3983 if (session
->has_been_started
) {
3984 return LTTNG_ERR_SESSION_STARTED
;
3987 strncpy(session
->shm_path
, shm_path
,
3988 sizeof(session
->shm_path
));
3989 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3995 * Init command subsystem.
4000 * Set network sequence index to 1 for streams to match a relayd
4001 * socket on the consumer side.
4003 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
4004 relayd_net_seq_idx
= 1;
4005 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
4007 DBG("Command subsystem initialized");