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>
35 #include "health-sessiond.h"
37 #include "kernel-consumer.h"
38 #include "lttng-sessiond.h"
46 * Used to keep a unique index for each relayd socket created where this value
47 * is associated with streams on the consumer so it can match the right relayd
48 * to send to. It must be accessed with the relayd_net_seq_idx_lock
51 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
52 static uint64_t relayd_net_seq_idx
;
54 static int validate_event_name(const char *);
55 static int validate_ust_event_name(const char *);
56 static int cmd_enable_event_internal(struct ltt_session
*session
,
57 struct lttng_domain
*domain
,
58 char *channel_name
, struct lttng_event
*event
,
59 char *filter_expression
,
60 struct lttng_filter_bytecode
*filter
,
61 struct lttng_event_exclusion
*exclusion
,
65 * Create a session path used by list_lttng_sessions for the case that the
66 * session consumer is on the network.
68 static int build_network_session_path(char *dst
, size_t size
,
69 struct ltt_session
*session
)
71 int ret
, kdata_port
, udata_port
;
72 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
73 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
78 memset(tmp_urls
, 0, sizeof(tmp_urls
));
79 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
81 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
83 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
84 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
85 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
88 if (session
->ust_session
&& session
->ust_session
->consumer
) {
89 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
90 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
93 if (uuri
== NULL
&& kuri
== NULL
) {
94 uri
= &session
->consumer
->dst
.net
.control
;
95 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
96 } else if (kuri
&& uuri
) {
97 ret
= uri_compare(kuri
, uuri
);
101 /* Build uuri URL string */
102 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
109 } else if (kuri
&& uuri
== NULL
) {
111 } else if (uuri
&& kuri
== NULL
) {
115 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
121 * Do we have a UST url set. If yes, this means we have both kernel and UST
124 if (*tmp_uurl
!= '\0') {
125 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
126 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
129 if (kuri
|| (!kuri
&& !uuri
)) {
132 /* No kernel URI, use the UST port. */
135 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
143 * Fill lttng_channel array of all channels.
145 static void list_lttng_channels(enum lttng_domain_type domain
,
146 struct ltt_session
*session
, struct lttng_channel
*channels
)
149 struct ltt_kernel_channel
*kchan
;
151 DBG("Listing channels for session %s", session
->name
);
154 case LTTNG_DOMAIN_KERNEL
:
155 /* Kernel channels */
156 if (session
->kernel_session
!= NULL
) {
157 cds_list_for_each_entry(kchan
,
158 &session
->kernel_session
->channel_list
.head
, list
) {
159 /* Copy lttng_channel struct to array */
160 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
161 channels
[i
].enabled
= kchan
->enabled
;
166 case LTTNG_DOMAIN_UST
:
168 struct lttng_ht_iter iter
;
169 struct ltt_ust_channel
*uchan
;
172 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
173 &iter
.iter
, uchan
, node
.node
) {
174 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
175 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
176 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
177 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
178 channels
[i
].attr
.switch_timer_interval
=
179 uchan
->attr
.switch_timer_interval
;
180 channels
[i
].attr
.read_timer_interval
=
181 uchan
->attr
.read_timer_interval
;
182 channels
[i
].enabled
= uchan
->enabled
;
183 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
184 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
185 switch (uchan
->attr
.output
) {
188 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
201 static void increment_extended_len(const char *filter_expression
,
202 size_t *extended_len
)
204 *extended_len
+= sizeof(struct lttcomm_event_extended_header
);
206 if (filter_expression
) {
207 *extended_len
+= strlen(filter_expression
) + 1;
211 static void append_extended_info(const char *filter_expression
,
214 struct lttcomm_event_extended_header extended_header
;
215 size_t filter_len
= 0;
217 if (filter_expression
) {
218 filter_len
= strlen(filter_expression
) + 1;
221 extended_header
.filter_len
= filter_len
;
222 memcpy(*extended_at
, &extended_header
, sizeof(extended_header
));
223 *extended_at
+= sizeof(extended_header
);
224 memcpy(*extended_at
, filter_expression
, filter_len
);
225 *extended_at
+= filter_len
;
229 * Create a list of agent domain events.
231 * Return number of events in list on success or else a negative value.
233 static int list_lttng_agent_events(struct agent
*agt
,
234 struct lttng_event
**events
, size_t *total_size
)
237 unsigned int nb_event
= 0;
238 struct agent_event
*event
;
239 struct lttng_event
*tmp_events
;
240 struct lttng_ht_iter iter
;
241 size_t extended_len
= 0;
247 DBG3("Listing agent events");
250 nb_event
= lttng_ht_get_count(agt
->events
);
258 /* Compute required extended infos size */
259 extended_len
= nb_event
* sizeof(struct lttcomm_event_extended_header
);
262 * This is only valid because the commands which add events are
263 * processed in the same thread as the listing.
266 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
267 increment_extended_len(event
->filter_expression
, &extended_len
);
271 *total_size
= nb_event
* sizeof(*tmp_events
) + extended_len
;
272 tmp_events
= zmalloc(*total_size
);
274 PERROR("zmalloc agent events session");
275 ret
= -LTTNG_ERR_FATAL
;
279 extended_at
= ((uint8_t *) tmp_events
) +
280 nb_event
* sizeof(struct lttng_event
);
283 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
284 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
285 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
286 tmp_events
[i
].enabled
= event
->enabled
;
287 tmp_events
[i
].loglevel
= event
->loglevel_value
;
288 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
291 /* Append extended info */
292 append_extended_info(event
->filter_expression
, &extended_at
);
296 *events
= tmp_events
;
300 assert(nb_event
== i
);
305 * Create a list of ust global domain events.
307 static int list_lttng_ust_global_events(char *channel_name
,
308 struct ltt_ust_domain_global
*ust_global
,
309 struct lttng_event
**events
, size_t *total_size
)
312 unsigned int nb_event
= 0;
313 struct lttng_ht_iter iter
;
314 struct lttng_ht_node_str
*node
;
315 struct ltt_ust_channel
*uchan
;
316 struct ltt_ust_event
*uevent
;
317 struct lttng_event
*tmp
;
318 size_t extended_len
= 0;
321 DBG("Listing UST global events for channel %s", channel_name
);
325 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
326 node
= lttng_ht_iter_get_node_str(&iter
);
328 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
332 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
334 nb_event
= lttng_ht_get_count(uchan
->events
);
341 DBG3("Listing UST global %d events", nb_event
);
343 /* Compute required extended infos size */
344 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
345 if (uevent
->internal
) {
350 increment_extended_len(uevent
->filter_expression
,
354 /* All events are internal, skip. */
360 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
361 tmp
= zmalloc(*total_size
);
363 ret
= LTTNG_ERR_FATAL
;
367 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
369 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
370 if (uevent
->internal
) {
371 /* This event should remain hidden from clients */
374 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
375 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
376 tmp
[i
].enabled
= uevent
->enabled
;
378 switch (uevent
->attr
.instrumentation
) {
379 case LTTNG_UST_TRACEPOINT
:
380 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
382 case LTTNG_UST_PROBE
:
383 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
385 case LTTNG_UST_FUNCTION
:
386 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
390 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
391 switch (uevent
->attr
.loglevel_type
) {
392 case LTTNG_UST_LOGLEVEL_ALL
:
393 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
395 case LTTNG_UST_LOGLEVEL_RANGE
:
396 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
398 case LTTNG_UST_LOGLEVEL_SINGLE
:
399 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
402 if (uevent
->filter
) {
405 if (uevent
->exclusion
) {
406 tmp
[i
].exclusion
= 1;
410 /* Append extended info */
411 append_extended_info(uevent
->filter_expression
, &extended_at
);
422 * Fill lttng_event array of all kernel events in the channel.
424 static int list_lttng_kernel_events(char *channel_name
,
425 struct ltt_kernel_session
*kernel_session
,
426 struct lttng_event
**events
, size_t *total_size
)
429 unsigned int nb_event
;
430 struct ltt_kernel_event
*event
;
431 struct ltt_kernel_channel
*kchan
;
432 size_t extended_len
= 0;
435 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
437 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
441 nb_event
= kchan
->event_count
;
443 DBG("Listing events for channel %s", kchan
->channel
->name
);
451 /* Compute required extended infos size */
452 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
453 increment_extended_len(event
->filter_expression
, &extended_len
);
456 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
457 *events
= zmalloc(*total_size
);
458 if (*events
== NULL
) {
459 ret
= LTTNG_ERR_FATAL
;
463 extended_at
= ((uint8_t *) events
) +
464 nb_event
* sizeof(struct lttng_event
);
466 /* Kernel channels */
467 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
468 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
469 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
470 (*events
)[i
].enabled
= event
->enabled
;
471 (*events
)[i
].filter
=
472 (unsigned char) !!event
->filter_expression
;
474 switch (event
->event
->instrumentation
) {
475 case LTTNG_KERNEL_TRACEPOINT
:
476 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
478 case LTTNG_KERNEL_KRETPROBE
:
479 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
480 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
481 sizeof(struct lttng_kernel_kprobe
));
483 case LTTNG_KERNEL_KPROBE
:
484 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
485 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
486 sizeof(struct lttng_kernel_kprobe
));
488 case LTTNG_KERNEL_FUNCTION
:
489 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
490 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
491 sizeof(struct lttng_kernel_function
));
493 case LTTNG_KERNEL_NOOP
:
494 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
496 case LTTNG_KERNEL_SYSCALL
:
497 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
499 case LTTNG_KERNEL_ALL
:
505 /* Append extended info */
506 append_extended_info(event
->filter_expression
, &extended_at
);
513 new_size
= syscall_list_channel(kchan
, events
, nb_event
);
525 /* Negate the error code to differentiate the size from an error */
530 * Add URI so the consumer output object. Set the correct path depending on the
531 * domain adding the default trace directory.
533 static int add_uri_to_consumer(struct consumer_output
*consumer
,
534 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
535 const char *session_name
)
538 const char *default_trace_dir
;
542 if (consumer
== NULL
) {
543 DBG("No consumer detected. Don't add URI. Stopping.");
544 ret
= LTTNG_ERR_NO_CONSUMER
;
549 case LTTNG_DOMAIN_KERNEL
:
550 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
552 case LTTNG_DOMAIN_UST
:
553 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
557 * This case is possible is we try to add the URI to the global tracing
558 * session consumer object which in this case there is no subdir.
560 default_trace_dir
= "";
563 switch (uri
->dtype
) {
566 DBG2("Setting network URI to consumer");
568 if (consumer
->type
== CONSUMER_DST_NET
) {
569 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
570 consumer
->dst
.net
.control_isset
) ||
571 (uri
->stype
== LTTNG_STREAM_DATA
&&
572 consumer
->dst
.net
.data_isset
)) {
573 ret
= LTTNG_ERR_URL_EXIST
;
577 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
580 consumer
->type
= CONSUMER_DST_NET
;
582 /* Set URI into consumer output object */
583 ret
= consumer_set_network_uri(consumer
, uri
);
587 } else if (ret
== 1) {
589 * URI was the same in the consumer so we do not append the subdir
590 * again so to not duplicate output dir.
596 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
597 ret
= consumer_set_subdir(consumer
, session_name
);
599 ret
= LTTNG_ERR_FATAL
;
604 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
605 /* On a new subdir, reappend the default trace dir. */
606 strncat(consumer
->subdir
, default_trace_dir
,
607 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
608 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
613 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
614 memset(consumer
->dst
.trace_path
, 0,
615 sizeof(consumer
->dst
.trace_path
));
616 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
617 sizeof(consumer
->dst
.trace_path
));
618 /* Append default trace dir */
619 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
620 sizeof(consumer
->dst
.trace_path
) -
621 strlen(consumer
->dst
.trace_path
) - 1);
622 /* Flag consumer as local. */
623 consumer
->type
= CONSUMER_DST_LOCAL
;
634 * Init tracing by creating trace directory and sending fds kernel consumer.
636 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
639 struct lttng_ht_iter iter
;
640 struct consumer_socket
*socket
;
646 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
647 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
649 pthread_mutex_lock(socket
->lock
);
650 ret
= kernel_consumer_send_session(socket
, session
);
651 pthread_mutex_unlock(socket
->lock
);
653 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
665 * Create a socket to the relayd using the URI.
667 * On success, the relayd_sock pointer is set to the created socket.
668 * Else, it's stays untouched and a lttcomm error code is returned.
670 static int create_connect_relayd(struct lttng_uri
*uri
,
671 struct lttcomm_relayd_sock
**relayd_sock
)
674 struct lttcomm_relayd_sock
*rsock
;
676 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
677 RELAYD_VERSION_COMM_MINOR
);
679 ret
= LTTNG_ERR_FATAL
;
684 * Connect to relayd so we can proceed with a session creation. This call
685 * can possibly block for an arbitrary amount of time to set the health
686 * state to be in poll execution.
689 ret
= relayd_connect(rsock
);
692 ERR("Unable to reach lttng-relayd");
693 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
697 /* Create socket for control stream. */
698 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
699 DBG3("Creating relayd stream socket from URI");
701 /* Check relayd version */
702 ret
= relayd_version_check(rsock
);
704 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
707 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
708 DBG3("Creating relayd data socket from URI");
710 /* Command is not valid */
711 ERR("Relayd invalid stream type: %d", uri
->stype
);
712 ret
= LTTNG_ERR_INVALID
;
716 *relayd_sock
= rsock
;
721 /* The returned value is not useful since we are on an error path. */
722 (void) relayd_close(rsock
);
730 * Connect to the relayd using URI and send the socket to the right consumer.
732 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
733 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
734 struct consumer_output
*consumer
,
735 struct consumer_socket
*consumer_sock
,
736 char *session_name
, char *hostname
, int session_live_timer
)
739 struct lttcomm_relayd_sock
*rsock
= NULL
;
741 /* Connect to relayd and make version check if uri is the control. */
742 ret
= create_connect_relayd(relayd_uri
, &rsock
);
743 if (ret
!= LTTNG_OK
) {
748 /* Set the network sequence index if not set. */
749 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
750 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
752 * Increment net_seq_idx because we are about to transfer the
753 * new relayd socket to the consumer.
754 * Assign unique key so the consumer can match streams.
756 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
757 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
760 /* Send relayd socket to consumer. */
761 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
762 relayd_uri
->stype
, session_id
,
763 session_name
, hostname
, session_live_timer
);
765 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
769 /* Flag that the corresponding socket was sent. */
770 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
771 consumer_sock
->control_sock_sent
= 1;
772 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
773 consumer_sock
->data_sock_sent
= 1;
779 * Close socket which was dup on the consumer side. The session daemon does
780 * NOT keep track of the relayd socket(s) once transfer to the consumer.
784 (void) relayd_close(rsock
);
788 if (ret
!= LTTNG_OK
) {
790 * The consumer output for this session should not be used anymore
791 * since the relayd connection failed thus making any tracing or/and
792 * streaming not usable.
794 consumer
->enabled
= 0;
800 * Send both relayd sockets to a specific consumer and domain. This is a
801 * helper function to facilitate sending the information to the consumer for a
804 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
805 unsigned int session_id
, struct consumer_output
*consumer
,
806 struct consumer_socket
*sock
, char *session_name
,
807 char *hostname
, int session_live_timer
)
814 /* Sending control relayd socket. */
815 if (!sock
->control_sock_sent
) {
816 ret
= send_consumer_relayd_socket(domain
, session_id
,
817 &consumer
->dst
.net
.control
, consumer
, sock
,
818 session_name
, hostname
, session_live_timer
);
819 if (ret
!= LTTNG_OK
) {
824 /* Sending data relayd socket. */
825 if (!sock
->data_sock_sent
) {
826 ret
= send_consumer_relayd_socket(domain
, session_id
,
827 &consumer
->dst
.net
.data
, consumer
, sock
,
828 session_name
, hostname
, session_live_timer
);
829 if (ret
!= LTTNG_OK
) {
839 * Setup relayd connections for a tracing session. First creates the socket to
840 * the relayd and send them to the right domain consumer. Consumer type MUST be
843 int cmd_setup_relayd(struct ltt_session
*session
)
846 struct ltt_ust_session
*usess
;
847 struct ltt_kernel_session
*ksess
;
848 struct consumer_socket
*socket
;
849 struct lttng_ht_iter iter
;
853 usess
= session
->ust_session
;
854 ksess
= session
->kernel_session
;
856 DBG("Setting relayd for session %s", session
->name
);
860 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
861 && usess
->consumer
->enabled
) {
862 /* For each consumer socket, send relayd sockets */
863 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
865 pthread_mutex_lock(socket
->lock
);
866 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
867 usess
->consumer
, socket
,
868 session
->name
, session
->hostname
,
869 session
->live_timer
);
870 pthread_mutex_unlock(socket
->lock
);
871 if (ret
!= LTTNG_OK
) {
874 /* Session is now ready for network streaming. */
875 session
->net_handle
= 1;
879 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
880 && ksess
->consumer
->enabled
) {
881 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
883 pthread_mutex_lock(socket
->lock
);
884 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
885 ksess
->consumer
, socket
,
886 session
->name
, session
->hostname
,
887 session
->live_timer
);
888 pthread_mutex_unlock(socket
->lock
);
889 if (ret
!= LTTNG_OK
) {
892 /* Session is now ready for network streaming. */
893 session
->net_handle
= 1;
903 * Start a kernel session by opening all necessary streams.
905 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
908 struct ltt_kernel_channel
*kchan
;
910 /* Open kernel metadata */
911 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
912 ret
= kernel_open_metadata(ksess
);
914 ret
= LTTNG_ERR_KERN_META_FAIL
;
919 /* Open kernel metadata stream */
920 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
921 ret
= kernel_open_metadata_stream(ksess
);
923 ERR("Kernel create metadata stream failed");
924 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
929 /* For each channel */
930 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
931 if (kchan
->stream_count
== 0) {
932 ret
= kernel_open_channel_stream(kchan
);
934 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
937 /* Update the stream global counter */
938 ksess
->stream_count_global
+= ret
;
942 /* Setup kernel consumer socket and send fds to it */
943 ret
= init_kernel_tracing(ksess
);
945 ret
= LTTNG_ERR_KERN_START_FAIL
;
949 /* This start the kernel tracing */
950 ret
= kernel_start_session(ksess
);
952 ret
= LTTNG_ERR_KERN_START_FAIL
;
956 /* Quiescent wait after starting trace */
957 kernel_wait_quiescent(kernel_tracer_fd
);
968 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
970 int cmd_disable_channel(struct ltt_session
*session
,
971 enum lttng_domain_type domain
, char *channel_name
)
974 struct ltt_ust_session
*usess
;
976 usess
= session
->ust_session
;
981 case LTTNG_DOMAIN_KERNEL
:
983 ret
= channel_kernel_disable(session
->kernel_session
,
985 if (ret
!= LTTNG_OK
) {
989 kernel_wait_quiescent(kernel_tracer_fd
);
992 case LTTNG_DOMAIN_UST
:
994 struct ltt_ust_channel
*uchan
;
995 struct lttng_ht
*chan_ht
;
997 chan_ht
= usess
->domain_global
.channels
;
999 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
1000 if (uchan
== NULL
) {
1001 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1005 ret
= channel_ust_disable(usess
, uchan
);
1006 if (ret
!= LTTNG_OK
) {
1012 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1024 * Command LTTNG_TRACK_PID processed by the client thread.
1026 * Called with session lock held.
1028 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1036 case LTTNG_DOMAIN_KERNEL
:
1038 struct ltt_kernel_session
*ksess
;
1040 ksess
= session
->kernel_session
;
1042 ret
= kernel_track_pid(ksess
, pid
);
1043 if (ret
!= LTTNG_OK
) {
1047 kernel_wait_quiescent(kernel_tracer_fd
);
1050 case LTTNG_DOMAIN_UST
:
1052 struct ltt_ust_session
*usess
;
1054 usess
= session
->ust_session
;
1056 ret
= trace_ust_track_pid(usess
, pid
);
1057 if (ret
!= LTTNG_OK
) {
1063 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1075 * Command LTTNG_UNTRACK_PID processed by the client thread.
1077 * Called with session lock held.
1079 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1087 case LTTNG_DOMAIN_KERNEL
:
1089 struct ltt_kernel_session
*ksess
;
1091 ksess
= session
->kernel_session
;
1093 ret
= kernel_untrack_pid(ksess
, pid
);
1094 if (ret
!= LTTNG_OK
) {
1098 kernel_wait_quiescent(kernel_tracer_fd
);
1101 case LTTNG_DOMAIN_UST
:
1103 struct ltt_ust_session
*usess
;
1105 usess
= session
->ust_session
;
1107 ret
= trace_ust_untrack_pid(usess
, pid
);
1108 if (ret
!= LTTNG_OK
) {
1114 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1126 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1128 * The wpipe arguments is used as a notifier for the kernel thread.
1130 int cmd_enable_channel(struct ltt_session
*session
,
1131 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1134 struct ltt_ust_session
*usess
= session
->ust_session
;
1135 struct lttng_ht
*chan_ht
;
1142 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1144 /* Validate channel name */
1145 if (attr
->name
[0] == '.' ||
1146 memchr(attr
->name
, '/', len
) != NULL
) {
1147 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1151 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1156 * Don't try to enable a channel if the session has been started at
1157 * some point in time before. The tracer does not allow it.
1159 if (session
->has_been_started
) {
1160 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1165 * If the session is a live session, remove the switch timer, the
1166 * live timer does the same thing but sends also synchronisation
1167 * beacons for inactive streams.
1169 if (session
->live_timer
> 0) {
1170 attr
->attr
.live_timer_interval
= session
->live_timer
;
1171 attr
->attr
.switch_timer_interval
= 0;
1175 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1176 * mode and with less than 2 subbuf so block it right away and send back an
1177 * invalid attribute error.
1179 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
1180 ret
= LTTNG_ERR_INVALID
;
1184 switch (domain
->type
) {
1185 case LTTNG_DOMAIN_KERNEL
:
1187 struct ltt_kernel_channel
*kchan
;
1189 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1190 session
->kernel_session
);
1191 if (kchan
== NULL
) {
1192 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1193 if (attr
->name
[0] != '\0') {
1194 session
->kernel_session
->has_non_default_channel
= 1;
1197 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1200 if (ret
!= LTTNG_OK
) {
1204 kernel_wait_quiescent(kernel_tracer_fd
);
1207 case LTTNG_DOMAIN_UST
:
1208 case LTTNG_DOMAIN_JUL
:
1209 case LTTNG_DOMAIN_LOG4J
:
1210 case LTTNG_DOMAIN_PYTHON
:
1212 struct ltt_ust_channel
*uchan
;
1217 * Current agent implementation limitations force us to allow
1218 * only one channel at once in "agent" subdomains. Each
1219 * subdomain has a default channel name which must be strictly
1222 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1223 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1224 LTTNG_SYMBOL_NAME_LEN
)) {
1225 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1228 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1229 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1230 LTTNG_SYMBOL_NAME_LEN
)) {
1231 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1234 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1235 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1236 LTTNG_SYMBOL_NAME_LEN
)) {
1237 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1242 chan_ht
= usess
->domain_global
.channels
;
1244 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1245 if (uchan
== NULL
) {
1246 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1247 if (attr
->name
[0] != '\0') {
1248 usess
->has_non_default_channel
= 1;
1251 ret
= channel_ust_enable(usess
, uchan
);
1256 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1267 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1269 int cmd_disable_event(struct ltt_session
*session
,
1270 enum lttng_domain_type domain
, char *channel_name
,
1271 struct lttng_event
*event
)
1276 DBG("Disable event command for event \'%s\'", event
->name
);
1278 event_name
= event
->name
;
1279 if (validate_event_name(event_name
)) {
1280 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1284 /* Error out on unhandled search criteria */
1285 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1286 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1287 ret
= LTTNG_ERR_UNK
;
1294 case LTTNG_DOMAIN_KERNEL
:
1296 struct ltt_kernel_channel
*kchan
;
1297 struct ltt_kernel_session
*ksess
;
1299 ksess
= session
->kernel_session
;
1302 * If a non-default channel has been created in the
1303 * session, explicitely require that -c chan_name needs
1306 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1307 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1311 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1312 if (kchan
== NULL
) {
1313 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1317 switch (event
->type
) {
1318 case LTTNG_EVENT_ALL
:
1319 case LTTNG_EVENT_TRACEPOINT
:
1320 case LTTNG_EVENT_SYSCALL
:
1321 case LTTNG_EVENT_PROBE
:
1322 case LTTNG_EVENT_FUNCTION
:
1323 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1324 if (event_name
[0] == '\0') {
1325 ret
= event_kernel_disable_event(kchan
,
1328 ret
= event_kernel_disable_event(kchan
,
1329 event_name
, event
->type
);
1331 if (ret
!= LTTNG_OK
) {
1336 ret
= LTTNG_ERR_UNK
;
1340 kernel_wait_quiescent(kernel_tracer_fd
);
1343 case LTTNG_DOMAIN_UST
:
1345 struct ltt_ust_channel
*uchan
;
1346 struct ltt_ust_session
*usess
;
1348 usess
= session
->ust_session
;
1350 if (validate_ust_event_name(event_name
)) {
1351 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1356 * If a non-default channel has been created in the
1357 * session, explicitly require that -c chan_name needs
1360 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1361 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1365 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1367 if (uchan
== NULL
) {
1368 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1372 switch (event
->type
) {
1373 case LTTNG_EVENT_ALL
:
1375 * An empty event name means that everything
1376 * should be disabled.
1378 if (event
->name
[0] == '\0') {
1379 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1381 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1384 if (ret
!= LTTNG_OK
) {
1389 ret
= LTTNG_ERR_UNK
;
1393 DBG3("Disable UST event %s in channel %s completed", event_name
,
1397 case LTTNG_DOMAIN_LOG4J
:
1398 case LTTNG_DOMAIN_JUL
:
1399 case LTTNG_DOMAIN_PYTHON
:
1402 struct ltt_ust_session
*usess
= session
->ust_session
;
1406 switch (event
->type
) {
1407 case LTTNG_EVENT_ALL
:
1410 ret
= LTTNG_ERR_UNK
;
1414 agt
= trace_ust_find_agent(usess
, domain
);
1416 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1420 * An empty event name means that everything
1421 * should be disabled.
1423 if (event
->name
[0] == '\0') {
1424 ret
= event_agent_disable_all(usess
, agt
);
1426 ret
= event_agent_disable(usess
, agt
, event_name
);
1428 if (ret
!= LTTNG_OK
) {
1435 ret
= LTTNG_ERR_UND
;
1448 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1450 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1451 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1453 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1454 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1456 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1457 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1458 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1462 case LTTNG_DOMAIN_KERNEL
:
1463 assert(session
->kernel_session
);
1465 if (session
->kernel_session
->channel_count
== 0) {
1466 /* Create default channel */
1467 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1468 if (ret
!= LTTNG_OK
) {
1471 chan_kern_created
= 1;
1473 /* Add kernel context to kernel tracer */
1474 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1475 if (ret
!= LTTNG_OK
) {
1479 case LTTNG_DOMAIN_JUL
:
1480 case LTTNG_DOMAIN_LOG4J
:
1483 * Validate channel name.
1484 * If no channel name is given and the domain is JUL or LOG4J,
1485 * set it to the appropriate domain-specific channel name. If
1486 * a name is provided but does not match the expexted channel
1487 * name, return an error.
1489 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1490 strcmp(channel_name
,
1491 DEFAULT_JUL_CHANNEL_NAME
)) {
1492 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1494 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1495 strcmp(channel_name
,
1496 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1497 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1500 /* break is _not_ missing here. */
1502 case LTTNG_DOMAIN_UST
:
1504 struct ltt_ust_session
*usess
= session
->ust_session
;
1505 unsigned int chan_count
;
1509 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1510 if (chan_count
== 0) {
1511 struct lttng_channel
*attr
;
1512 /* Create default channel */
1513 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1515 ret
= LTTNG_ERR_FATAL
;
1519 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1520 if (ret
!= LTTNG_OK
) {
1525 chan_ust_created
= 1;
1528 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1529 free(app_ctx_provider_name
);
1531 app_ctx_name
= NULL
;
1532 app_ctx_provider_name
= NULL
;
1533 if (ret
!= LTTNG_OK
) {
1539 ret
= LTTNG_ERR_UND
;
1547 if (chan_kern_created
) {
1548 struct ltt_kernel_channel
*kchan
=
1549 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1550 session
->kernel_session
);
1551 /* Created previously, this should NOT fail. */
1553 kernel_destroy_channel(kchan
);
1556 if (chan_ust_created
) {
1557 struct ltt_ust_channel
*uchan
=
1558 trace_ust_find_channel_by_name(
1559 session
->ust_session
->domain_global
.channels
,
1560 DEFAULT_CHANNEL_NAME
);
1561 /* Created previously, this should NOT fail. */
1563 /* Remove from the channel list of the session. */
1564 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1566 trace_ust_destroy_channel(uchan
);
1569 free(app_ctx_provider_name
);
1574 static int validate_event_name(const char *name
)
1577 const char *c
= name
;
1578 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1579 bool null_terminated
= false;
1582 * Make sure that unescaped wildcards are only used as the last
1583 * character of the event name.
1585 while (c
< event_name_end
) {
1588 null_terminated
= true;
1594 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1595 /* Wildcard is not the last character */
1596 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1605 if (!ret
&& !null_terminated
) {
1606 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1611 static inline bool name_starts_with(const char *name
, const char *prefix
)
1613 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1615 return !strncmp(name
, prefix
, max_cmp_len
);
1618 /* Perform userspace-specific event name validation */
1619 static int validate_ust_event_name(const char *name
)
1629 * Check name against all internal UST event component namespaces used
1632 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1633 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1634 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1643 * Internal version of cmd_enable_event() with a supplemental
1644 * "internal_event" flag which is used to enable internal events which should
1645 * be hidden from clients. Such events are used in the agent implementation to
1646 * enable the events through which all "agent" events are funeled.
1648 static int _cmd_enable_event(struct ltt_session
*session
,
1649 struct lttng_domain
*domain
,
1650 char *channel_name
, struct lttng_event
*event
,
1651 char *filter_expression
,
1652 struct lttng_filter_bytecode
*filter
,
1653 struct lttng_event_exclusion
*exclusion
,
1654 int wpipe
, bool internal_event
)
1656 int ret
, channel_created
= 0;
1657 struct lttng_channel
*attr
;
1661 assert(channel_name
);
1663 /* If we have a filter, we must have its filter expression */
1664 assert(!(!!filter_expression
^ !!filter
));
1666 DBG("Enable event command for event \'%s\'", event
->name
);
1670 ret
= validate_event_name(event
->name
);
1675 switch (domain
->type
) {
1676 case LTTNG_DOMAIN_KERNEL
:
1678 struct ltt_kernel_channel
*kchan
;
1681 * If a non-default channel has been created in the
1682 * session, explicitely require that -c chan_name needs
1685 if (session
->kernel_session
->has_non_default_channel
1686 && channel_name
[0] == '\0') {
1687 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1691 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1692 session
->kernel_session
);
1693 if (kchan
== NULL
) {
1694 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1695 LTTNG_BUFFER_GLOBAL
);
1697 ret
= LTTNG_ERR_FATAL
;
1700 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1702 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1703 if (ret
!= LTTNG_OK
) {
1709 channel_created
= 1;
1712 /* Get the newly created kernel channel pointer */
1713 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1714 session
->kernel_session
);
1715 if (kchan
== NULL
) {
1716 /* This sould not happen... */
1717 ret
= LTTNG_ERR_FATAL
;
1721 switch (event
->type
) {
1722 case LTTNG_EVENT_ALL
:
1724 char *filter_expression_a
= NULL
;
1725 struct lttng_filter_bytecode
*filter_a
= NULL
;
1728 * We need to duplicate filter_expression and filter,
1729 * because ownership is passed to first enable
1732 if (filter_expression
) {
1733 filter_expression_a
= strdup(filter_expression
);
1734 if (!filter_expression_a
) {
1735 ret
= LTTNG_ERR_FATAL
;
1740 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1742 free(filter_expression_a
);
1743 ret
= LTTNG_ERR_FATAL
;
1746 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1748 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1749 ret
= event_kernel_enable_event(kchan
, event
,
1750 filter_expression
, filter
);
1751 /* We have passed ownership */
1752 filter_expression
= NULL
;
1754 if (ret
!= LTTNG_OK
) {
1755 if (channel_created
) {
1756 /* Let's not leak a useless channel. */
1757 kernel_destroy_channel(kchan
);
1759 free(filter_expression_a
);
1763 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1764 ret
= event_kernel_enable_event(kchan
, event
,
1765 filter_expression_a
, filter_a
);
1766 /* We have passed ownership */
1767 filter_expression_a
= NULL
;
1769 if (ret
!= LTTNG_OK
) {
1774 case LTTNG_EVENT_PROBE
:
1775 case LTTNG_EVENT_FUNCTION
:
1776 case LTTNG_EVENT_FUNCTION_ENTRY
:
1777 case LTTNG_EVENT_TRACEPOINT
:
1778 ret
= event_kernel_enable_event(kchan
, event
,
1779 filter_expression
, filter
);
1780 /* We have passed ownership */
1781 filter_expression
= NULL
;
1783 if (ret
!= LTTNG_OK
) {
1784 if (channel_created
) {
1785 /* Let's not leak a useless channel. */
1786 kernel_destroy_channel(kchan
);
1791 case LTTNG_EVENT_SYSCALL
:
1792 ret
= event_kernel_enable_event(kchan
, event
,
1793 filter_expression
, filter
);
1794 /* We have passed ownership */
1795 filter_expression
= NULL
;
1797 if (ret
!= LTTNG_OK
) {
1802 ret
= LTTNG_ERR_UNK
;
1806 kernel_wait_quiescent(kernel_tracer_fd
);
1809 case LTTNG_DOMAIN_UST
:
1811 struct ltt_ust_channel
*uchan
;
1812 struct ltt_ust_session
*usess
= session
->ust_session
;
1817 * If a non-default channel has been created in the
1818 * session, explicitely require that -c chan_name needs
1821 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1822 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1826 /* Get channel from global UST domain */
1827 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1829 if (uchan
== NULL
) {
1830 /* Create default channel */
1831 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1832 usess
->buffer_type
);
1834 ret
= LTTNG_ERR_FATAL
;
1837 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1839 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1840 if (ret
!= LTTNG_OK
) {
1846 /* Get the newly created channel reference back */
1847 uchan
= trace_ust_find_channel_by_name(
1848 usess
->domain_global
.channels
, channel_name
);
1852 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
1854 * Don't allow users to add UST events to channels which
1855 * are assigned to a userspace subdomain (JUL, Log4J,
1858 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
1862 if (!internal_event
) {
1864 * Ensure the event name is not reserved for internal
1867 ret
= validate_ust_event_name(event
->name
);
1869 WARN("Userspace event name %s failed validation.",
1871 event
->name
: "NULL");
1872 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1877 /* At this point, the session and channel exist on the tracer */
1878 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1879 filter_expression
, filter
, exclusion
,
1881 /* We have passed ownership */
1882 filter_expression
= NULL
;
1885 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
1886 goto already_enabled
;
1887 } else if (ret
!= LTTNG_OK
) {
1892 case LTTNG_DOMAIN_LOG4J
:
1893 case LTTNG_DOMAIN_JUL
:
1894 case LTTNG_DOMAIN_PYTHON
:
1896 const char *default_event_name
, *default_chan_name
;
1898 struct lttng_event uevent
;
1899 struct lttng_domain tmp_dom
;
1900 struct ltt_ust_session
*usess
= session
->ust_session
;
1904 agt
= trace_ust_find_agent(usess
, domain
->type
);
1906 agt
= agent_create(domain
->type
);
1908 ret
= LTTNG_ERR_NOMEM
;
1911 agent_add(agt
, usess
->agents
);
1914 /* Create the default tracepoint. */
1915 memset(&uevent
, 0, sizeof(uevent
));
1916 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1917 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1918 default_event_name
= event_get_default_agent_ust_name(
1920 if (!default_event_name
) {
1921 ret
= LTTNG_ERR_FATAL
;
1924 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1925 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1928 * The domain type is changed because we are about to enable the
1929 * default channel and event for the JUL domain that are hardcoded.
1930 * This happens in the UST domain.
1932 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1933 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1935 switch (domain
->type
) {
1936 case LTTNG_DOMAIN_LOG4J
:
1937 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1939 case LTTNG_DOMAIN_JUL
:
1940 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1942 case LTTNG_DOMAIN_PYTHON
:
1943 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
1946 /* The switch/case we are in makes this impossible */
1951 char *filter_expression_copy
= NULL
;
1952 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1955 const size_t filter_size
= sizeof(
1956 struct lttng_filter_bytecode
)
1959 filter_copy
= zmalloc(filter_size
);
1961 ret
= LTTNG_ERR_NOMEM
;
1964 memcpy(filter_copy
, filter
, filter_size
);
1966 filter_expression_copy
=
1967 strdup(filter_expression
);
1968 if (!filter_expression
) {
1969 ret
= LTTNG_ERR_NOMEM
;
1972 if (!filter_expression_copy
|| !filter_copy
) {
1973 free(filter_expression_copy
);
1979 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
1980 (char *) default_chan_name
,
1981 &uevent
, filter_expression_copy
,
1982 filter_copy
, NULL
, wpipe
);
1985 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
1986 goto already_enabled
;
1987 } else if (ret
!= LTTNG_OK
) {
1991 /* The wild card * means that everything should be enabled. */
1992 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1993 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
1996 ret
= event_agent_enable(usess
, agt
, event
, filter
,
2000 filter_expression
= NULL
;
2001 if (ret
!= LTTNG_OK
) {
2008 ret
= LTTNG_ERR_UND
;
2016 free(filter_expression
);
2024 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2025 * We own filter, exclusion, and filter_expression.
2027 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2028 char *channel_name
, struct lttng_event
*event
,
2029 char *filter_expression
,
2030 struct lttng_filter_bytecode
*filter
,
2031 struct lttng_event_exclusion
*exclusion
,
2034 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2035 filter_expression
, filter
, exclusion
, wpipe
, false);
2039 * Enable an event which is internal to LTTng. An internal should
2040 * never be made visible to clients and are immune to checks such as
2043 static int cmd_enable_event_internal(struct ltt_session
*session
,
2044 struct lttng_domain
*domain
,
2045 char *channel_name
, struct lttng_event
*event
,
2046 char *filter_expression
,
2047 struct lttng_filter_bytecode
*filter
,
2048 struct lttng_event_exclusion
*exclusion
,
2051 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2052 filter_expression
, filter
, exclusion
, wpipe
, true);
2056 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2058 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2059 struct lttng_event
**events
)
2062 ssize_t nb_events
= 0;
2065 case LTTNG_DOMAIN_KERNEL
:
2066 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2067 if (nb_events
< 0) {
2068 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2072 case LTTNG_DOMAIN_UST
:
2073 nb_events
= ust_app_list_events(events
);
2074 if (nb_events
< 0) {
2075 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2079 case LTTNG_DOMAIN_LOG4J
:
2080 case LTTNG_DOMAIN_JUL
:
2081 case LTTNG_DOMAIN_PYTHON
:
2082 nb_events
= agent_list_events(events
, domain
);
2083 if (nb_events
< 0) {
2084 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2089 ret
= LTTNG_ERR_UND
;
2096 /* Return negative value to differentiate return code */
2101 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2103 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2104 struct lttng_event_field
**fields
)
2107 ssize_t nb_fields
= 0;
2110 case LTTNG_DOMAIN_UST
:
2111 nb_fields
= ust_app_list_event_fields(fields
);
2112 if (nb_fields
< 0) {
2113 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2117 case LTTNG_DOMAIN_KERNEL
:
2118 default: /* fall-through */
2119 ret
= LTTNG_ERR_UND
;
2126 /* Return negative value to differentiate return code */
2130 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2132 return syscall_table_list(events
);
2136 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2138 * Called with session lock held.
2140 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2141 enum lttng_domain_type domain
, int32_t **pids
)
2144 ssize_t nr_pids
= 0;
2147 case LTTNG_DOMAIN_KERNEL
:
2149 struct ltt_kernel_session
*ksess
;
2151 ksess
= session
->kernel_session
;
2152 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2154 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2159 case LTTNG_DOMAIN_UST
:
2161 struct ltt_ust_session
*usess
;
2163 usess
= session
->ust_session
;
2164 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2166 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2171 case LTTNG_DOMAIN_LOG4J
:
2172 case LTTNG_DOMAIN_JUL
:
2173 case LTTNG_DOMAIN_PYTHON
:
2175 ret
= LTTNG_ERR_UND
;
2182 /* Return negative value to differentiate return code */
2187 * Command LTTNG_START_TRACE processed by the client thread.
2189 * Called with session mutex held.
2191 int cmd_start_trace(struct ltt_session
*session
)
2194 unsigned long nb_chan
= 0;
2195 struct ltt_kernel_session
*ksession
;
2196 struct ltt_ust_session
*usess
;
2200 /* Ease our life a bit ;) */
2201 ksession
= session
->kernel_session
;
2202 usess
= session
->ust_session
;
2204 /* Is the session already started? */
2205 if (session
->active
) {
2206 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2211 * Starting a session without channel is useless since after that it's not
2212 * possible to enable channel thus inform the client.
2214 if (usess
&& usess
->domain_global
.channels
) {
2215 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2218 nb_chan
+= ksession
->channel_count
;
2221 ret
= LTTNG_ERR_NO_CHANNEL
;
2225 /* Kernel tracing */
2226 if (ksession
!= NULL
) {
2227 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2228 if (ret
!= LTTNG_OK
) {
2233 /* Flag session that trace should start automatically */
2236 * Even though the start trace might fail, flag this session active so
2237 * other application coming in are started by default.
2241 ret
= ust_app_start_trace_all(usess
);
2243 ret
= LTTNG_ERR_UST_START_FAIL
;
2248 /* Flag this after a successful start. */
2249 session
->has_been_started
= 1;
2250 session
->active
= 1;
2259 * Command LTTNG_STOP_TRACE processed by the client thread.
2261 int cmd_stop_trace(struct ltt_session
*session
)
2264 struct ltt_kernel_channel
*kchan
;
2265 struct ltt_kernel_session
*ksession
;
2266 struct ltt_ust_session
*usess
;
2271 ksession
= session
->kernel_session
;
2272 usess
= session
->ust_session
;
2274 /* Session is not active. Skip everythong and inform the client. */
2275 if (!session
->active
) {
2276 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2281 if (ksession
&& ksession
->active
) {
2282 DBG("Stop kernel tracing");
2284 /* Flush metadata if exist */
2285 if (ksession
->metadata_stream_fd
>= 0) {
2286 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2288 ERR("Kernel metadata flush failed");
2292 /* Flush all buffers before stopping */
2293 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2294 ret
= kernel_flush_buffer(kchan
);
2296 ERR("Kernel flush buffer error");
2300 ret
= kernel_stop_session(ksession
);
2302 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2306 kernel_wait_quiescent(kernel_tracer_fd
);
2308 ksession
->active
= 0;
2311 if (usess
&& usess
->active
) {
2313 * Even though the stop trace might fail, flag this session inactive so
2314 * other application coming in are not started by default.
2318 ret
= ust_app_stop_trace_all(usess
);
2320 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2325 /* Flag inactive after a successful stop. */
2326 session
->active
= 0;
2334 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2336 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2337 struct lttng_uri
*uris
)
2340 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2341 struct ltt_ust_session
*usess
= session
->ust_session
;
2347 /* Can't set consumer URI if the session is active. */
2348 if (session
->active
) {
2349 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2353 /* Set the "global" consumer URIs */
2354 for (i
= 0; i
< nb_uri
; i
++) {
2355 ret
= add_uri_to_consumer(session
->consumer
,
2356 &uris
[i
], 0, session
->name
);
2357 if (ret
!= LTTNG_OK
) {
2362 /* Set UST session URIs */
2363 if (session
->ust_session
) {
2364 for (i
= 0; i
< nb_uri
; i
++) {
2365 ret
= add_uri_to_consumer(
2366 session
->ust_session
->consumer
,
2367 &uris
[i
], LTTNG_DOMAIN_UST
,
2369 if (ret
!= LTTNG_OK
) {
2375 /* Set kernel session URIs */
2376 if (session
->kernel_session
) {
2377 for (i
= 0; i
< nb_uri
; i
++) {
2378 ret
= add_uri_to_consumer(
2379 session
->kernel_session
->consumer
,
2380 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2382 if (ret
!= LTTNG_OK
) {
2389 * Make sure to set the session in output mode after we set URI since a
2390 * session can be created without URL (thus flagged in no output mode).
2392 session
->output_traces
= 1;
2394 ksess
->output_traces
= 1;
2398 usess
->output_traces
= 1;
2409 * Command LTTNG_CREATE_SESSION processed by the client thread.
2411 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2412 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2415 struct ltt_session
*session
;
2421 * Verify if the session already exist
2423 * XXX: There is no need for the session lock list here since the caller
2424 * (process_client_msg) is holding it. We might want to change that so a
2425 * single command does not lock the entire session list.
2427 session
= session_find_by_name(name
);
2428 if (session
!= NULL
) {
2429 ret
= LTTNG_ERR_EXIST_SESS
;
2433 /* Create tracing session in the registry */
2434 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2435 LTTNG_SOCK_GET_GID_CRED(creds
));
2436 if (ret
!= LTTNG_OK
) {
2441 * Get the newly created session pointer back
2443 * XXX: There is no need for the session lock list here since the caller
2444 * (process_client_msg) is holding it. We might want to change that so a
2445 * single command does not lock the entire session list.
2447 session
= session_find_by_name(name
);
2450 session
->live_timer
= live_timer
;
2451 /* Create default consumer output for the session not yet created. */
2452 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2453 if (session
->consumer
== NULL
) {
2454 ret
= LTTNG_ERR_FATAL
;
2455 goto consumer_error
;
2459 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2460 if (ret
!= LTTNG_OK
) {
2461 goto consumer_error
;
2463 session
->output_traces
= 1;
2465 session
->output_traces
= 0;
2466 DBG2("Session %s created with no output", session
->name
);
2469 session
->consumer
->enabled
= 1;
2474 session_destroy(session
);
2481 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2483 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2484 size_t nb_uri
, lttng_sock_cred
*creds
)
2487 struct ltt_session
*session
;
2488 struct snapshot_output
*new_output
= NULL
;
2494 * Create session in no output mode with URIs set to NULL. The uris we've
2495 * received are for a default snapshot output if one.
2497 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2498 if (ret
!= LTTNG_OK
) {
2502 /* Get the newly created session pointer back. This should NEVER fail. */
2503 session
= session_find_by_name(name
);
2506 /* Flag session for snapshot mode. */
2507 session
->snapshot_mode
= 1;
2509 /* Skip snapshot output creation if no URI is given. */
2514 new_output
= snapshot_output_alloc();
2516 ret
= LTTNG_ERR_NOMEM
;
2517 goto error_snapshot_alloc
;
2520 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2521 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2523 if (ret
== -ENOMEM
) {
2524 ret
= LTTNG_ERR_NOMEM
;
2526 ret
= LTTNG_ERR_INVALID
;
2528 goto error_snapshot
;
2532 snapshot_add_output(&session
->snapshot
, new_output
);
2539 snapshot_output_destroy(new_output
);
2540 error_snapshot_alloc
:
2541 session_destroy(session
);
2547 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2549 * Called with session lock held.
2551 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2554 struct ltt_ust_session
*usess
;
2555 struct ltt_kernel_session
*ksess
;
2560 usess
= session
->ust_session
;
2561 ksess
= session
->kernel_session
;
2563 /* Clean kernel session teardown */
2564 kernel_destroy_session(ksess
);
2566 /* UST session teardown */
2568 /* Close any relayd session */
2569 consumer_output_send_destroy_relayd(usess
->consumer
);
2571 /* Destroy every UST application related to this session. */
2572 ret
= ust_app_destroy_trace_all(usess
);
2574 ERR("Error in ust_app_destroy_trace_all");
2577 /* Clean up the rest. */
2578 trace_ust_destroy_session(usess
);
2582 * Must notify the kernel thread here to update it's poll set in order to
2583 * remove the channel(s)' fd just destroyed.
2585 ret
= notify_thread_pipe(wpipe
);
2587 PERROR("write kernel poll pipe");
2590 ret
= session_destroy(session
);
2596 * Command LTTNG_CALIBRATE processed by the client thread.
2598 int cmd_calibrate(enum lttng_domain_type domain
,
2599 struct lttng_calibrate
*calibrate
)
2604 case LTTNG_DOMAIN_KERNEL
:
2606 struct lttng_kernel_calibrate kcalibrate
;
2608 switch (calibrate
->type
) {
2609 case LTTNG_CALIBRATE_FUNCTION
:
2611 /* Default and only possible calibrate option. */
2612 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2616 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2618 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2623 case LTTNG_DOMAIN_UST
:
2625 struct lttng_ust_calibrate ucalibrate
;
2627 switch (calibrate
->type
) {
2628 case LTTNG_CALIBRATE_FUNCTION
:
2630 /* Default and only possible calibrate option. */
2631 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2635 ret
= ust_app_calibrate_glb(&ucalibrate
);
2637 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2643 ret
= LTTNG_ERR_UND
;
2654 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2656 int cmd_register_consumer(struct ltt_session
*session
,
2657 enum lttng_domain_type domain
, const char *sock_path
,
2658 struct consumer_data
*cdata
)
2661 struct consumer_socket
*socket
= NULL
;
2668 case LTTNG_DOMAIN_KERNEL
:
2670 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2674 /* Can't register a consumer if there is already one */
2675 if (ksess
->consumer_fds_sent
!= 0) {
2676 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2680 sock
= lttcomm_connect_unix_sock(sock_path
);
2682 ret
= LTTNG_ERR_CONNECT_FAIL
;
2685 cdata
->cmd_sock
= sock
;
2687 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2688 if (socket
== NULL
) {
2691 PERROR("close register consumer");
2693 cdata
->cmd_sock
= -1;
2694 ret
= LTTNG_ERR_FATAL
;
2698 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2699 if (socket
->lock
== NULL
) {
2700 PERROR("zmalloc pthread mutex");
2701 ret
= LTTNG_ERR_FATAL
;
2704 pthread_mutex_init(socket
->lock
, NULL
);
2705 socket
->registered
= 1;
2708 consumer_add_socket(socket
, ksess
->consumer
);
2711 pthread_mutex_lock(&cdata
->pid_mutex
);
2713 pthread_mutex_unlock(&cdata
->pid_mutex
);
2718 /* TODO: Userspace tracing */
2719 ret
= LTTNG_ERR_UND
;
2727 consumer_destroy_socket(socket
);
2733 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2735 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2736 struct lttng_domain
**domains
)
2741 struct lttng_ht_iter iter
;
2743 if (session
->kernel_session
!= NULL
) {
2744 DBG3("Listing domains found kernel domain");
2748 if (session
->ust_session
!= NULL
) {
2749 DBG3("Listing domains found UST global domain");
2753 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2755 if (agt
->being_used
) {
2766 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2767 if (*domains
== NULL
) {
2768 ret
= LTTNG_ERR_FATAL
;
2772 if (session
->kernel_session
!= NULL
) {
2773 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2775 /* Kernel session buffer type is always GLOBAL */
2776 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2781 if (session
->ust_session
!= NULL
) {
2782 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2783 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2787 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2789 if (agt
->being_used
) {
2790 (*domains
)[index
].type
= agt
->domain
;
2791 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2801 /* Return negative value to differentiate return code */
2807 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2809 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2810 struct ltt_session
*session
, struct lttng_channel
**channels
)
2813 ssize_t nb_chan
= 0;
2816 case LTTNG_DOMAIN_KERNEL
:
2817 if (session
->kernel_session
!= NULL
) {
2818 nb_chan
= session
->kernel_session
->channel_count
;
2820 DBG3("Number of kernel channels %zd", nb_chan
);
2822 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2825 case LTTNG_DOMAIN_UST
:
2826 if (session
->ust_session
!= NULL
) {
2828 nb_chan
= lttng_ht_get_count(
2829 session
->ust_session
->domain_global
.channels
);
2832 DBG3("Number of UST global channels %zd", nb_chan
);
2834 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2839 ret
= LTTNG_ERR_UND
;
2844 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2845 if (*channels
== NULL
) {
2846 ret
= LTTNG_ERR_FATAL
;
2850 list_lttng_channels(domain
, session
, *channels
);
2856 /* Return negative value to differentiate return code */
2861 * Command LTTNG_LIST_EVENTS processed by the client thread.
2863 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
2864 struct ltt_session
*session
, char *channel_name
,
2865 struct lttng_event
**events
, size_t *total_size
)
2868 ssize_t nb_event
= 0;
2871 case LTTNG_DOMAIN_KERNEL
:
2872 if (session
->kernel_session
!= NULL
) {
2873 nb_event
= list_lttng_kernel_events(channel_name
,
2874 session
->kernel_session
, events
,
2878 case LTTNG_DOMAIN_UST
:
2880 if (session
->ust_session
!= NULL
) {
2881 nb_event
= list_lttng_ust_global_events(channel_name
,
2882 &session
->ust_session
->domain_global
, events
,
2887 case LTTNG_DOMAIN_LOG4J
:
2888 case LTTNG_DOMAIN_JUL
:
2889 case LTTNG_DOMAIN_PYTHON
:
2890 if (session
->ust_session
) {
2891 struct lttng_ht_iter iter
;
2895 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2896 &iter
.iter
, agt
, node
.node
) {
2897 if (agt
->domain
== domain
) {
2898 nb_event
= list_lttng_agent_events(
2908 ret
= LTTNG_ERR_UND
;
2915 /* Return negative value to differentiate return code */
2920 * Using the session list, filled a lttng_session array to send back to the
2921 * client for session listing.
2923 * The session list lock MUST be acquired before calling this function. Use
2924 * session_lock_list() and session_unlock_list().
2926 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2931 struct ltt_session
*session
;
2932 struct ltt_session_list
*list
= session_get_list();
2934 DBG("Getting all available session for UID %d GID %d",
2937 * Iterate over session list and append data after the control struct in
2940 cds_list_for_each_entry(session
, &list
->head
, list
) {
2942 * Only list the sessions the user can control.
2944 if (!session_access_ok(session
, uid
, gid
)) {
2948 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2949 struct ltt_ust_session
*usess
= session
->ust_session
;
2951 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2952 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2953 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2954 ret
= build_network_session_path(sessions
[i
].path
,
2955 sizeof(sessions
[i
].path
), session
);
2957 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2958 session
->consumer
->dst
.trace_path
);
2961 PERROR("snprintf session path");
2965 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2966 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2967 sessions
[i
].enabled
= session
->active
;
2968 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2969 sessions
[i
].live_timer_interval
= session
->live_timer
;
2975 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2976 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2978 int cmd_data_pending(struct ltt_session
*session
)
2981 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2982 struct ltt_ust_session
*usess
= session
->ust_session
;
2986 /* Session MUST be stopped to ask for data availability. */
2987 if (session
->active
) {
2988 ret
= LTTNG_ERR_SESSION_STARTED
;
2992 * If stopped, just make sure we've started before else the above call
2993 * will always send that there is data pending.
2995 * The consumer assumes that when the data pending command is received,
2996 * the trace has been started before or else no output data is written
2997 * by the streams which is a condition for data pending. So, this is
2998 * *VERY* important that we don't ask the consumer before a start
3001 if (!session
->has_been_started
) {
3007 if (ksess
&& ksess
->consumer
) {
3008 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3010 /* Data is still being extracted for the kernel. */
3015 if (usess
&& usess
->consumer
) {
3016 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3018 /* Data is still being extracted for the kernel. */
3023 /* Data is ready to be read by a viewer */
3031 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3033 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3035 int cmd_snapshot_add_output(struct ltt_session
*session
,
3036 struct lttng_snapshot_output
*output
, uint32_t *id
)
3039 struct snapshot_output
*new_output
;
3044 DBG("Cmd snapshot add output for session %s", session
->name
);
3047 * Permission denied to create an output if the session is not
3048 * set in no output mode.
3050 if (session
->output_traces
) {
3051 ret
= LTTNG_ERR_EPERM
;
3055 /* Only one output is allowed until we have the "tee" feature. */
3056 if (session
->snapshot
.nb_output
== 1) {
3057 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3061 new_output
= snapshot_output_alloc();
3063 ret
= LTTNG_ERR_NOMEM
;
3067 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3068 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3069 &session
->snapshot
);
3071 if (ret
== -ENOMEM
) {
3072 ret
= LTTNG_ERR_NOMEM
;
3074 ret
= LTTNG_ERR_INVALID
;
3080 snapshot_add_output(&session
->snapshot
, new_output
);
3082 *id
= new_output
->id
;
3089 snapshot_output_destroy(new_output
);
3095 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3097 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3099 int cmd_snapshot_del_output(struct ltt_session
*session
,
3100 struct lttng_snapshot_output
*output
)
3103 struct snapshot_output
*sout
= NULL
;
3111 * Permission denied to create an output if the session is not
3112 * set in no output mode.
3114 if (session
->output_traces
) {
3115 ret
= LTTNG_ERR_EPERM
;
3120 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3122 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3123 } else if (*output
->name
!= '\0') {
3124 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3126 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3129 ret
= LTTNG_ERR_INVALID
;
3133 snapshot_delete_output(&session
->snapshot
, sout
);
3134 snapshot_output_destroy(sout
);
3143 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3145 * If no output is available, outputs is untouched and 0 is returned.
3147 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3149 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3150 struct lttng_snapshot_output
**outputs
)
3153 struct lttng_snapshot_output
*list
= NULL
;
3154 struct lttng_ht_iter iter
;
3155 struct snapshot_output
*output
;
3160 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3163 * Permission denied to create an output if the session is not
3164 * set in no output mode.
3166 if (session
->output_traces
) {
3167 ret
= -LTTNG_ERR_EPERM
;
3171 if (session
->snapshot
.nb_output
== 0) {
3176 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3178 ret
= -LTTNG_ERR_NOMEM
;
3182 /* Copy list from session to the new list object. */
3184 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3185 output
, node
.node
) {
3186 assert(output
->consumer
);
3187 list
[idx
].id
= output
->id
;
3188 list
[idx
].max_size
= output
->max_size
;
3189 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
3190 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3191 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
3192 sizeof(list
[idx
].ctrl_url
));
3195 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3196 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3198 ret
= -LTTNG_ERR_NOMEM
;
3203 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3204 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3206 ret
= -LTTNG_ERR_NOMEM
;
3215 ret
= session
->snapshot
.nb_output
;
3223 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3224 * snapshot output is *not* set with a remote destination.
3226 * Return 0 on success or a LTTNG_ERR code.
3228 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3229 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3232 struct lttng_ht_iter iter
;
3233 struct consumer_socket
*socket
;
3236 assert(snap_output
);
3239 DBG2("Set relayd object from snapshot output");
3241 /* Ignore if snapshot consumer output is not network. */
3242 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3247 * For each consumer socket, create and send the relayd object of the
3251 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3252 socket
, node
.node
) {
3253 ret
= send_consumer_relayd_sockets(0, session
->id
,
3254 snap_output
->consumer
, socket
,
3255 session
->name
, session
->hostname
,
3256 session
->live_timer
);
3257 if (ret
!= LTTNG_OK
) {
3269 * Record a kernel snapshot.
3271 * Return LTTNG_OK on success or a LTTNG_ERR code.
3273 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3274 struct snapshot_output
*output
, struct ltt_session
*session
,
3275 int wait
, uint64_t nb_packets_per_stream
)
3283 /* Get the datetime for the snapshot output directory. */
3284 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3285 sizeof(output
->datetime
));
3287 ret
= LTTNG_ERR_INVALID
;
3292 * Copy kernel session sockets so we can communicate with the right
3293 * consumer for the snapshot record command.
3295 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3297 ret
= LTTNG_ERR_NOMEM
;
3301 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3302 if (ret
!= LTTNG_OK
) {
3303 goto error_snapshot
;
3306 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3307 if (ret
!= LTTNG_OK
) {
3308 goto error_snapshot
;
3315 /* Clean up copied sockets so this output can use some other later on. */
3316 consumer_destroy_output_sockets(output
->consumer
);
3323 * Record a UST snapshot.
3325 * Return 0 on success or a LTTNG_ERR error code.
3327 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3328 struct snapshot_output
*output
, struct ltt_session
*session
,
3329 int wait
, uint64_t nb_packets_per_stream
)
3337 /* Get the datetime for the snapshot output directory. */
3338 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3339 sizeof(output
->datetime
));
3341 ret
= LTTNG_ERR_INVALID
;
3346 * Copy UST session sockets so we can communicate with the right
3347 * consumer for the snapshot record command.
3349 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3351 ret
= LTTNG_ERR_NOMEM
;
3355 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3356 if (ret
!= LTTNG_OK
) {
3357 goto error_snapshot
;
3360 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3364 ret
= LTTNG_ERR_INVALID
;
3367 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3370 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3373 goto error_snapshot
;
3379 /* Clean up copied sockets so this output can use some other later on. */
3380 consumer_destroy_output_sockets(output
->consumer
);
3386 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3387 uint64_t cur_nr_packets
)
3389 uint64_t tot_size
= 0;
3391 if (session
->kernel_session
) {
3392 struct ltt_kernel_channel
*chan
;
3393 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3395 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3396 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3398 * Don't take channel into account if we
3399 * already grab all its packets.
3403 tot_size
+= chan
->channel
->attr
.subbuf_size
3404 * chan
->stream_count
;
3408 if (session
->ust_session
) {
3409 struct ltt_ust_session
*usess
= session
->ust_session
;
3411 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3419 * Calculate the number of packets we can grab from each stream that
3420 * fits within the overall snapshot max size.
3422 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3423 * the number of packets per stream.
3425 * TODO: this approach is not perfect: we consider the worse case
3426 * (packet filling the sub-buffers) as an upper bound, but we could do
3427 * better if we do this calculation while we actually grab the packet
3428 * content: we would know how much padding we don't actually store into
3431 * This algorithm is currently bounded by the number of packets per
3434 * Since we call this algorithm before actually grabbing the data, it's
3435 * an approximation: for instance, applications could appear/disappear
3436 * in between this call and actually grabbing data.
3439 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3442 uint64_t cur_nb_packets
= 0;
3445 return 0; /* Infinite */
3448 size_left
= max_size
;
3450 uint64_t one_more_packet_tot_size
;
3452 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3454 if (!one_more_packet_tot_size
) {
3455 /* We are already grabbing all packets. */
3458 size_left
-= one_more_packet_tot_size
;
3459 if (size_left
< 0) {
3464 if (!cur_nb_packets
) {
3465 /* Not enough room to grab one packet of each stream, error. */
3468 return cur_nb_packets
;
3472 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3474 * The wait parameter is ignored so this call always wait for the snapshot to
3475 * complete before returning.
3477 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3479 int cmd_snapshot_record(struct ltt_session
*session
,
3480 struct lttng_snapshot_output
*output
, int wait
)
3483 unsigned int use_tmp_output
= 0;
3484 struct snapshot_output tmp_output
;
3485 unsigned int snapshot_success
= 0;
3490 DBG("Cmd snapshot record for session %s", session
->name
);
3493 * Permission denied to create an output if the session is not
3494 * set in no output mode.
3496 if (session
->output_traces
) {
3497 ret
= LTTNG_ERR_EPERM
;
3501 /* The session needs to be started at least once. */
3502 if (!session
->has_been_started
) {
3503 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3507 /* Use temporary output for the session. */
3508 if (*output
->ctrl_url
!= '\0') {
3509 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3510 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3513 if (ret
== -ENOMEM
) {
3514 ret
= LTTNG_ERR_NOMEM
;
3516 ret
= LTTNG_ERR_INVALID
;
3520 /* Use the global session count for the temporary snapshot. */
3521 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3525 if (session
->kernel_session
) {
3526 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3528 if (use_tmp_output
) {
3529 int64_t nb_packets_per_stream
;
3531 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3532 tmp_output
.max_size
);
3533 if (nb_packets_per_stream
< 0) {
3534 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3537 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3538 wait
, nb_packets_per_stream
);
3539 if (ret
!= LTTNG_OK
) {
3542 snapshot_success
= 1;
3544 struct snapshot_output
*sout
;
3545 struct lttng_ht_iter iter
;
3548 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3549 &iter
.iter
, sout
, node
.node
) {
3550 int64_t nb_packets_per_stream
;
3553 * Make a local copy of the output and assign the possible
3554 * temporary value given by the caller.
3556 memset(&tmp_output
, 0, sizeof(tmp_output
));
3557 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3559 if (output
->max_size
!= (uint64_t) -1ULL) {
3560 tmp_output
.max_size
= output
->max_size
;
3563 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3564 tmp_output
.max_size
);
3565 if (nb_packets_per_stream
< 0) {
3566 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3570 /* Use temporary name. */
3571 if (*output
->name
!= '\0') {
3572 strncpy(tmp_output
.name
, output
->name
,
3573 sizeof(tmp_output
.name
));
3576 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3578 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3579 session
, wait
, nb_packets_per_stream
);
3580 if (ret
!= LTTNG_OK
) {
3584 snapshot_success
= 1;
3590 if (session
->ust_session
) {
3591 struct ltt_ust_session
*usess
= session
->ust_session
;
3593 if (use_tmp_output
) {
3594 int64_t nb_packets_per_stream
;
3596 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3597 tmp_output
.max_size
);
3598 if (nb_packets_per_stream
< 0) {
3599 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3602 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3603 wait
, nb_packets_per_stream
);
3604 if (ret
!= LTTNG_OK
) {
3607 snapshot_success
= 1;
3609 struct snapshot_output
*sout
;
3610 struct lttng_ht_iter iter
;
3613 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3614 &iter
.iter
, sout
, node
.node
) {
3615 int64_t nb_packets_per_stream
;
3618 * Make a local copy of the output and assign the possible
3619 * temporary value given by the caller.
3621 memset(&tmp_output
, 0, sizeof(tmp_output
));
3622 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3624 if (output
->max_size
!= (uint64_t) -1ULL) {
3625 tmp_output
.max_size
= output
->max_size
;
3628 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3629 tmp_output
.max_size
);
3630 if (nb_packets_per_stream
< 0) {
3631 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3636 /* Use temporary name. */
3637 if (*output
->name
!= '\0') {
3638 strncpy(tmp_output
.name
, output
->name
,
3639 sizeof(tmp_output
.name
));
3642 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3644 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3645 wait
, nb_packets_per_stream
);
3646 if (ret
!= LTTNG_OK
) {
3650 snapshot_success
= 1;
3656 if (snapshot_success
) {
3657 session
->snapshot
.nb_snapshot
++;
3659 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3667 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3669 int cmd_set_session_shm_path(struct ltt_session
*session
,
3670 const char *shm_path
)
3676 * Can only set shm path before session is started.
3678 if (session
->has_been_started
) {
3679 return LTTNG_ERR_SESSION_STARTED
;
3682 strncpy(session
->shm_path
, shm_path
,
3683 sizeof(session
->shm_path
));
3684 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3690 * Init command subsystem.
3695 * Set network sequence index to 1 for streams to match a relayd
3696 * socket on the consumer side.
3698 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3699 relayd_net_seq_idx
= 1;
3700 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3702 DBG("Command subsystem initialized");