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 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
355 tmp
= zmalloc(*total_size
);
357 ret
= LTTNG_ERR_FATAL
;
361 extended_at
= ((uint8_t *) tmp
) + nb_event
* sizeof(struct lttng_event
);
363 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
364 if (uevent
->internal
) {
365 /* This event should remain hidden from clients */
368 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
369 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
370 tmp
[i
].enabled
= uevent
->enabled
;
372 switch (uevent
->attr
.instrumentation
) {
373 case LTTNG_UST_TRACEPOINT
:
374 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
376 case LTTNG_UST_PROBE
:
377 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
379 case LTTNG_UST_FUNCTION
:
380 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
384 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
385 switch (uevent
->attr
.loglevel_type
) {
386 case LTTNG_UST_LOGLEVEL_ALL
:
387 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
389 case LTTNG_UST_LOGLEVEL_RANGE
:
390 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
392 case LTTNG_UST_LOGLEVEL_SINGLE
:
393 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
396 if (uevent
->filter
) {
399 if (uevent
->exclusion
) {
400 tmp
[i
].exclusion
= 1;
404 /* Append extended info */
405 append_extended_info(uevent
->filter_expression
, &extended_at
);
417 * Fill lttng_event array of all kernel events in the channel.
419 static int list_lttng_kernel_events(char *channel_name
,
420 struct ltt_kernel_session
*kernel_session
,
421 struct lttng_event
**events
, size_t *total_size
)
424 unsigned int nb_event
;
425 struct ltt_kernel_event
*event
;
426 struct ltt_kernel_channel
*kchan
;
427 size_t extended_len
= 0;
430 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
432 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
436 nb_event
= kchan
->event_count
;
438 DBG("Listing events for channel %s", kchan
->channel
->name
);
446 /* Compute required extended infos size */
447 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
448 increment_extended_len(event
->filter_expression
, &extended_len
);
451 *total_size
= nb_event
* sizeof(struct lttng_event
) + extended_len
;
452 *events
= zmalloc(*total_size
);
453 if (*events
== NULL
) {
454 ret
= LTTNG_ERR_FATAL
;
458 extended_at
= ((uint8_t *) events
) +
459 nb_event
* sizeof(struct lttng_event
);
461 /* Kernel channels */
462 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
463 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
464 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
465 (*events
)[i
].enabled
= event
->enabled
;
466 (*events
)[i
].filter
=
467 (unsigned char) !!event
->filter_expression
;
469 switch (event
->event
->instrumentation
) {
470 case LTTNG_KERNEL_TRACEPOINT
:
471 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
473 case LTTNG_KERNEL_KRETPROBE
:
474 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
475 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
476 sizeof(struct lttng_kernel_kprobe
));
478 case LTTNG_KERNEL_KPROBE
:
479 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
480 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
481 sizeof(struct lttng_kernel_kprobe
));
483 case LTTNG_KERNEL_FUNCTION
:
484 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
485 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
486 sizeof(struct lttng_kernel_function
));
488 case LTTNG_KERNEL_NOOP
:
489 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
491 case LTTNG_KERNEL_SYSCALL
:
492 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
494 case LTTNG_KERNEL_ALL
:
500 /* Append extended info */
501 append_extended_info(event
->filter_expression
, &extended_at
);
508 new_size
= syscall_list_channel(kchan
, events
, nb_event
);
520 /* Negate the error code to differentiate the size from an error */
525 * Add URI so the consumer output object. Set the correct path depending on the
526 * domain adding the default trace directory.
528 static int add_uri_to_consumer(struct consumer_output
*consumer
,
529 struct lttng_uri
*uri
, enum lttng_domain_type domain
,
530 const char *session_name
)
533 const char *default_trace_dir
;
537 if (consumer
== NULL
) {
538 DBG("No consumer detected. Don't add URI. Stopping.");
539 ret
= LTTNG_ERR_NO_CONSUMER
;
544 case LTTNG_DOMAIN_KERNEL
:
545 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
547 case LTTNG_DOMAIN_UST
:
548 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
552 * This case is possible is we try to add the URI to the global tracing
553 * session consumer object which in this case there is no subdir.
555 default_trace_dir
= "";
558 switch (uri
->dtype
) {
561 DBG2("Setting network URI to consumer");
563 if (consumer
->type
== CONSUMER_DST_NET
) {
564 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
565 consumer
->dst
.net
.control_isset
) ||
566 (uri
->stype
== LTTNG_STREAM_DATA
&&
567 consumer
->dst
.net
.data_isset
)) {
568 ret
= LTTNG_ERR_URL_EXIST
;
572 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
575 consumer
->type
= CONSUMER_DST_NET
;
577 /* Set URI into consumer output object */
578 ret
= consumer_set_network_uri(consumer
, uri
);
582 } else if (ret
== 1) {
584 * URI was the same in the consumer so we do not append the subdir
585 * again so to not duplicate output dir.
591 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
592 ret
= consumer_set_subdir(consumer
, session_name
);
594 ret
= LTTNG_ERR_FATAL
;
599 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
600 /* On a new subdir, reappend the default trace dir. */
601 strncat(consumer
->subdir
, default_trace_dir
,
602 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
603 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
608 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
609 memset(consumer
->dst
.trace_path
, 0,
610 sizeof(consumer
->dst
.trace_path
));
611 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
612 sizeof(consumer
->dst
.trace_path
));
613 /* Append default trace dir */
614 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
615 sizeof(consumer
->dst
.trace_path
) -
616 strlen(consumer
->dst
.trace_path
) - 1);
617 /* Flag consumer as local. */
618 consumer
->type
= CONSUMER_DST_LOCAL
;
629 * Init tracing by creating trace directory and sending fds kernel consumer.
631 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
634 struct lttng_ht_iter iter
;
635 struct consumer_socket
*socket
;
641 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
642 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
644 pthread_mutex_lock(socket
->lock
);
645 ret
= kernel_consumer_send_session(socket
, session
);
646 pthread_mutex_unlock(socket
->lock
);
648 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
660 * Create a socket to the relayd using the URI.
662 * On success, the relayd_sock pointer is set to the created socket.
663 * Else, it's stays untouched and a lttcomm error code is returned.
665 static int create_connect_relayd(struct lttng_uri
*uri
,
666 struct lttcomm_relayd_sock
**relayd_sock
)
669 struct lttcomm_relayd_sock
*rsock
;
671 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
672 RELAYD_VERSION_COMM_MINOR
);
674 ret
= LTTNG_ERR_FATAL
;
679 * Connect to relayd so we can proceed with a session creation. This call
680 * can possibly block for an arbitrary amount of time to set the health
681 * state to be in poll execution.
684 ret
= relayd_connect(rsock
);
687 ERR("Unable to reach lttng-relayd");
688 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
692 /* Create socket for control stream. */
693 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
694 DBG3("Creating relayd stream socket from URI");
696 /* Check relayd version */
697 ret
= relayd_version_check(rsock
);
699 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
702 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
703 DBG3("Creating relayd data socket from URI");
705 /* Command is not valid */
706 ERR("Relayd invalid stream type: %d", uri
->stype
);
707 ret
= LTTNG_ERR_INVALID
;
711 *relayd_sock
= rsock
;
716 /* The returned value is not useful since we are on an error path. */
717 (void) relayd_close(rsock
);
725 * Connect to the relayd using URI and send the socket to the right consumer.
727 static int send_consumer_relayd_socket(enum lttng_domain_type domain
,
728 unsigned int session_id
, struct lttng_uri
*relayd_uri
,
729 struct consumer_output
*consumer
,
730 struct consumer_socket
*consumer_sock
,
731 char *session_name
, char *hostname
, int session_live_timer
)
734 struct lttcomm_relayd_sock
*rsock
= NULL
;
736 /* Connect to relayd and make version check if uri is the control. */
737 ret
= create_connect_relayd(relayd_uri
, &rsock
);
738 if (ret
!= LTTNG_OK
) {
743 /* Set the network sequence index if not set. */
744 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
745 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
747 * Increment net_seq_idx because we are about to transfer the
748 * new relayd socket to the consumer.
749 * Assign unique key so the consumer can match streams.
751 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
752 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
755 /* Send relayd socket to consumer. */
756 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
757 relayd_uri
->stype
, session_id
,
758 session_name
, hostname
, session_live_timer
);
760 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
764 /* Flag that the corresponding socket was sent. */
765 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
766 consumer_sock
->control_sock_sent
= 1;
767 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
768 consumer_sock
->data_sock_sent
= 1;
774 * Close socket which was dup on the consumer side. The session daemon does
775 * NOT keep track of the relayd socket(s) once transfer to the consumer.
779 (void) relayd_close(rsock
);
783 if (ret
!= LTTNG_OK
) {
785 * The consumer output for this session should not be used anymore
786 * since the relayd connection failed thus making any tracing or/and
787 * streaming not usable.
789 consumer
->enabled
= 0;
795 * Send both relayd sockets to a specific consumer and domain. This is a
796 * helper function to facilitate sending the information to the consumer for a
799 static int send_consumer_relayd_sockets(enum lttng_domain_type domain
,
800 unsigned int session_id
, struct consumer_output
*consumer
,
801 struct consumer_socket
*sock
, char *session_name
,
802 char *hostname
, int session_live_timer
)
809 /* Sending control relayd socket. */
810 if (!sock
->control_sock_sent
) {
811 ret
= send_consumer_relayd_socket(domain
, session_id
,
812 &consumer
->dst
.net
.control
, consumer
, sock
,
813 session_name
, hostname
, session_live_timer
);
814 if (ret
!= LTTNG_OK
) {
819 /* Sending data relayd socket. */
820 if (!sock
->data_sock_sent
) {
821 ret
= send_consumer_relayd_socket(domain
, session_id
,
822 &consumer
->dst
.net
.data
, consumer
, sock
,
823 session_name
, hostname
, session_live_timer
);
824 if (ret
!= LTTNG_OK
) {
834 * Setup relayd connections for a tracing session. First creates the socket to
835 * the relayd and send them to the right domain consumer. Consumer type MUST be
838 int cmd_setup_relayd(struct ltt_session
*session
)
841 struct ltt_ust_session
*usess
;
842 struct ltt_kernel_session
*ksess
;
843 struct consumer_socket
*socket
;
844 struct lttng_ht_iter iter
;
848 usess
= session
->ust_session
;
849 ksess
= session
->kernel_session
;
851 DBG("Setting relayd for session %s", session
->name
);
855 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
856 && usess
->consumer
->enabled
) {
857 /* For each consumer socket, send relayd sockets */
858 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
860 pthread_mutex_lock(socket
->lock
);
861 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
862 usess
->consumer
, socket
,
863 session
->name
, session
->hostname
,
864 session
->live_timer
);
865 pthread_mutex_unlock(socket
->lock
);
866 if (ret
!= LTTNG_OK
) {
869 /* Session is now ready for network streaming. */
870 session
->net_handle
= 1;
874 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
875 && ksess
->consumer
->enabled
) {
876 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
878 pthread_mutex_lock(socket
->lock
);
879 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
880 ksess
->consumer
, socket
,
881 session
->name
, session
->hostname
,
882 session
->live_timer
);
883 pthread_mutex_unlock(socket
->lock
);
884 if (ret
!= LTTNG_OK
) {
887 /* Session is now ready for network streaming. */
888 session
->net_handle
= 1;
898 * Start a kernel session by opening all necessary streams.
900 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
903 struct ltt_kernel_channel
*kchan
;
905 /* Open kernel metadata */
906 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
907 ret
= kernel_open_metadata(ksess
);
909 ret
= LTTNG_ERR_KERN_META_FAIL
;
914 /* Open kernel metadata stream */
915 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
916 ret
= kernel_open_metadata_stream(ksess
);
918 ERR("Kernel create metadata stream failed");
919 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
924 /* For each channel */
925 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
926 if (kchan
->stream_count
== 0) {
927 ret
= kernel_open_channel_stream(kchan
);
929 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
932 /* Update the stream global counter */
933 ksess
->stream_count_global
+= ret
;
937 /* Setup kernel consumer socket and send fds to it */
938 ret
= init_kernel_tracing(ksess
);
940 ret
= LTTNG_ERR_KERN_START_FAIL
;
944 /* This start the kernel tracing */
945 ret
= kernel_start_session(ksess
);
947 ret
= LTTNG_ERR_KERN_START_FAIL
;
951 /* Quiescent wait after starting trace */
952 kernel_wait_quiescent(kernel_tracer_fd
);
963 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
965 int cmd_disable_channel(struct ltt_session
*session
,
966 enum lttng_domain_type domain
, char *channel_name
)
969 struct ltt_ust_session
*usess
;
971 usess
= session
->ust_session
;
976 case LTTNG_DOMAIN_KERNEL
:
978 ret
= channel_kernel_disable(session
->kernel_session
,
980 if (ret
!= LTTNG_OK
) {
984 kernel_wait_quiescent(kernel_tracer_fd
);
987 case LTTNG_DOMAIN_UST
:
989 struct ltt_ust_channel
*uchan
;
990 struct lttng_ht
*chan_ht
;
992 chan_ht
= usess
->domain_global
.channels
;
994 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
996 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1000 ret
= channel_ust_disable(usess
, uchan
);
1001 if (ret
!= LTTNG_OK
) {
1007 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1019 * Command LTTNG_TRACK_PID processed by the client thread.
1021 * Called with session lock held.
1023 int cmd_track_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1031 case LTTNG_DOMAIN_KERNEL
:
1033 struct ltt_kernel_session
*ksess
;
1035 ksess
= session
->kernel_session
;
1037 ret
= kernel_track_pid(ksess
, pid
);
1038 if (ret
!= LTTNG_OK
) {
1042 kernel_wait_quiescent(kernel_tracer_fd
);
1045 case LTTNG_DOMAIN_UST
:
1047 struct ltt_ust_session
*usess
;
1049 usess
= session
->ust_session
;
1051 ret
= trace_ust_track_pid(usess
, pid
);
1052 if (ret
!= LTTNG_OK
) {
1058 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1070 * Command LTTNG_UNTRACK_PID processed by the client thread.
1072 * Called with session lock held.
1074 int cmd_untrack_pid(struct ltt_session
*session
, enum lttng_domain_type domain
,
1082 case LTTNG_DOMAIN_KERNEL
:
1084 struct ltt_kernel_session
*ksess
;
1086 ksess
= session
->kernel_session
;
1088 ret
= kernel_untrack_pid(ksess
, pid
);
1089 if (ret
!= LTTNG_OK
) {
1093 kernel_wait_quiescent(kernel_tracer_fd
);
1096 case LTTNG_DOMAIN_UST
:
1098 struct ltt_ust_session
*usess
;
1100 usess
= session
->ust_session
;
1102 ret
= trace_ust_untrack_pid(usess
, pid
);
1103 if (ret
!= LTTNG_OK
) {
1109 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1121 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1123 * The wpipe arguments is used as a notifier for the kernel thread.
1125 int cmd_enable_channel(struct ltt_session
*session
,
1126 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1129 struct ltt_ust_session
*usess
= session
->ust_session
;
1130 struct lttng_ht
*chan_ht
;
1137 len
= lttng_strnlen(attr
->name
, sizeof(attr
->name
));
1139 /* Validate channel name */
1140 if (attr
->name
[0] == '.' ||
1141 memchr(attr
->name
, '/', len
) != NULL
) {
1142 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1146 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1151 * Don't try to enable a channel if the session has been started at
1152 * some point in time before. The tracer does not allow it.
1154 if (session
->has_been_started
) {
1155 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1160 * If the session is a live session, remove the switch timer, the
1161 * live timer does the same thing but sends also synchronisation
1162 * beacons for inactive streams.
1164 if (session
->live_timer
> 0) {
1165 attr
->attr
.live_timer_interval
= session
->live_timer
;
1166 attr
->attr
.switch_timer_interval
= 0;
1170 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1171 * mode and with less than 2 subbuf so block it right away and send back an
1172 * invalid attribute error.
1174 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
1175 ret
= LTTNG_ERR_INVALID
;
1179 switch (domain
->type
) {
1180 case LTTNG_DOMAIN_KERNEL
:
1182 struct ltt_kernel_channel
*kchan
;
1184 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1185 session
->kernel_session
);
1186 if (kchan
== NULL
) {
1187 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1188 if (attr
->name
[0] != '\0') {
1189 session
->kernel_session
->has_non_default_channel
= 1;
1192 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1195 if (ret
!= LTTNG_OK
) {
1199 kernel_wait_quiescent(kernel_tracer_fd
);
1202 case LTTNG_DOMAIN_UST
:
1203 case LTTNG_DOMAIN_JUL
:
1204 case LTTNG_DOMAIN_LOG4J
:
1205 case LTTNG_DOMAIN_PYTHON
:
1207 struct ltt_ust_channel
*uchan
;
1212 * Current agent implementation limitations force us to allow
1213 * only one channel at once in "agent" subdomains. Each
1214 * subdomain has a default channel name which must be strictly
1217 if (domain
->type
== LTTNG_DOMAIN_JUL
) {
1218 if (strncmp(attr
->name
, DEFAULT_JUL_CHANNEL_NAME
,
1219 LTTNG_SYMBOL_NAME_LEN
)) {
1220 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1223 } else if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1224 if (strncmp(attr
->name
, DEFAULT_LOG4J_CHANNEL_NAME
,
1225 LTTNG_SYMBOL_NAME_LEN
)) {
1226 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1229 } else if (domain
->type
== LTTNG_DOMAIN_PYTHON
) {
1230 if (strncmp(attr
->name
, DEFAULT_PYTHON_CHANNEL_NAME
,
1231 LTTNG_SYMBOL_NAME_LEN
)) {
1232 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1237 chan_ht
= usess
->domain_global
.channels
;
1239 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1240 if (uchan
== NULL
) {
1241 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1242 if (attr
->name
[0] != '\0') {
1243 usess
->has_non_default_channel
= 1;
1246 ret
= channel_ust_enable(usess
, uchan
);
1251 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1262 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1264 int cmd_disable_event(struct ltt_session
*session
,
1265 enum lttng_domain_type domain
, char *channel_name
,
1266 struct lttng_event
*event
)
1271 DBG("Disable event command for event \'%s\'", event
->name
);
1273 event_name
= event
->name
;
1274 if (validate_event_name(event_name
)) {
1275 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1279 /* Error out on unhandled search criteria */
1280 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1281 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1282 ret
= LTTNG_ERR_UNK
;
1289 case LTTNG_DOMAIN_KERNEL
:
1291 struct ltt_kernel_channel
*kchan
;
1292 struct ltt_kernel_session
*ksess
;
1294 ksess
= session
->kernel_session
;
1297 * If a non-default channel has been created in the
1298 * session, explicitely require that -c chan_name needs
1301 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1302 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1306 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1307 if (kchan
== NULL
) {
1308 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1312 switch (event
->type
) {
1313 case LTTNG_EVENT_ALL
:
1314 case LTTNG_EVENT_TRACEPOINT
:
1315 case LTTNG_EVENT_SYSCALL
:
1316 case LTTNG_EVENT_PROBE
:
1317 case LTTNG_EVENT_FUNCTION
:
1318 case LTTNG_EVENT_FUNCTION_ENTRY
:/* fall-through */
1319 if (event_name
[0] == '\0') {
1320 ret
= event_kernel_disable_event(kchan
,
1323 ret
= event_kernel_disable_event(kchan
,
1324 event_name
, event
->type
);
1326 if (ret
!= LTTNG_OK
) {
1331 ret
= LTTNG_ERR_UNK
;
1335 kernel_wait_quiescent(kernel_tracer_fd
);
1338 case LTTNG_DOMAIN_UST
:
1340 struct ltt_ust_channel
*uchan
;
1341 struct ltt_ust_session
*usess
;
1343 usess
= session
->ust_session
;
1345 if (validate_ust_event_name(event_name
)) {
1346 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1351 * If a non-default channel has been created in the
1352 * session, explicitly require that -c chan_name needs
1355 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1356 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1360 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1362 if (uchan
== NULL
) {
1363 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1367 switch (event
->type
) {
1368 case LTTNG_EVENT_ALL
:
1370 * An empty event name means that everything
1371 * should be disabled.
1373 if (event
->name
[0] == '\0') {
1374 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1376 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1379 if (ret
!= LTTNG_OK
) {
1384 ret
= LTTNG_ERR_UNK
;
1388 DBG3("Disable UST event %s in channel %s completed", event_name
,
1392 case LTTNG_DOMAIN_LOG4J
:
1393 case LTTNG_DOMAIN_JUL
:
1394 case LTTNG_DOMAIN_PYTHON
:
1397 struct ltt_ust_session
*usess
= session
->ust_session
;
1401 switch (event
->type
) {
1402 case LTTNG_EVENT_ALL
:
1405 ret
= LTTNG_ERR_UNK
;
1409 agt
= trace_ust_find_agent(usess
, domain
);
1411 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1415 * An empty event name means that everything
1416 * should be disabled.
1418 if (event
->name
[0] == '\0') {
1419 ret
= event_agent_disable_all(usess
, agt
);
1421 ret
= event_agent_disable(usess
, agt
, event_name
);
1423 if (ret
!= LTTNG_OK
) {
1430 ret
= LTTNG_ERR_UND
;
1443 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1445 int cmd_add_context(struct ltt_session
*session
, enum lttng_domain_type domain
,
1446 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1448 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1449 char *app_ctx_provider_name
= NULL
, *app_ctx_name
= NULL
;
1451 if (ctx
->ctx
== LTTNG_EVENT_CONTEXT_APP_CONTEXT
) {
1452 app_ctx_provider_name
= ctx
->u
.app_ctx
.provider_name
;
1453 app_ctx_name
= ctx
->u
.app_ctx
.ctx_name
;
1457 case LTTNG_DOMAIN_KERNEL
:
1458 assert(session
->kernel_session
);
1460 if (session
->kernel_session
->channel_count
== 0) {
1461 /* Create default channel */
1462 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1463 if (ret
!= LTTNG_OK
) {
1466 chan_kern_created
= 1;
1468 /* Add kernel context to kernel tracer */
1469 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1470 if (ret
!= LTTNG_OK
) {
1474 case LTTNG_DOMAIN_JUL
:
1475 case LTTNG_DOMAIN_LOG4J
:
1478 * Validate channel name.
1479 * If no channel name is given and the domain is JUL or LOG4J,
1480 * set it to the appropriate domain-specific channel name. If
1481 * a name is provided but does not match the expexted channel
1482 * name, return an error.
1484 if (domain
== LTTNG_DOMAIN_JUL
&& *channel_name
&&
1485 strcmp(channel_name
,
1486 DEFAULT_JUL_CHANNEL_NAME
)) {
1487 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1489 } else if (domain
== LTTNG_DOMAIN_LOG4J
&& *channel_name
&&
1490 strcmp(channel_name
,
1491 DEFAULT_LOG4J_CHANNEL_NAME
)) {
1492 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1495 /* break is _not_ missing here. */
1497 case LTTNG_DOMAIN_UST
:
1499 struct ltt_ust_session
*usess
= session
->ust_session
;
1500 unsigned int chan_count
;
1504 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1505 if (chan_count
== 0) {
1506 struct lttng_channel
*attr
;
1507 /* Create default channel */
1508 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1510 ret
= LTTNG_ERR_FATAL
;
1514 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1515 if (ret
!= LTTNG_OK
) {
1520 chan_ust_created
= 1;
1523 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1524 free(app_ctx_provider_name
);
1526 app_ctx_name
= NULL
;
1527 app_ctx_provider_name
= NULL
;
1528 if (ret
!= LTTNG_OK
) {
1534 ret
= LTTNG_ERR_UND
;
1542 if (chan_kern_created
) {
1543 struct ltt_kernel_channel
*kchan
=
1544 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1545 session
->kernel_session
);
1546 /* Created previously, this should NOT fail. */
1548 kernel_destroy_channel(kchan
);
1551 if (chan_ust_created
) {
1552 struct ltt_ust_channel
*uchan
=
1553 trace_ust_find_channel_by_name(
1554 session
->ust_session
->domain_global
.channels
,
1555 DEFAULT_CHANNEL_NAME
);
1556 /* Created previously, this should NOT fail. */
1558 /* Remove from the channel list of the session. */
1559 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1561 trace_ust_destroy_channel(uchan
);
1564 free(app_ctx_provider_name
);
1569 static int validate_event_name(const char *name
)
1572 const char *c
= name
;
1573 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1574 bool null_terminated
= false;
1577 * Make sure that unescaped wildcards are only used as the last
1578 * character of the event name.
1580 while (c
< event_name_end
) {
1583 null_terminated
= true;
1589 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1590 /* Wildcard is not the last character */
1591 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1600 if (!ret
&& !null_terminated
) {
1601 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1606 static inline bool name_starts_with(const char *name
, const char *prefix
)
1608 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1610 return !strncmp(name
, prefix
, max_cmp_len
);
1613 /* Perform userspace-specific event name validation */
1614 static int validate_ust_event_name(const char *name
)
1624 * Check name against all internal UST event component namespaces used
1627 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1628 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1629 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1638 * Internal version of cmd_enable_event() with a supplemental
1639 * "internal_event" flag which is used to enable internal events which should
1640 * be hidden from clients. Such events are used in the agent implementation to
1641 * enable the events through which all "agent" events are funeled.
1643 static int _cmd_enable_event(struct ltt_session
*session
,
1644 struct lttng_domain
*domain
,
1645 char *channel_name
, struct lttng_event
*event
,
1646 char *filter_expression
,
1647 struct lttng_filter_bytecode
*filter
,
1648 struct lttng_event_exclusion
*exclusion
,
1649 int wpipe
, bool internal_event
)
1651 int ret
, channel_created
= 0;
1652 struct lttng_channel
*attr
;
1656 assert(channel_name
);
1658 /* If we have a filter, we must have its filter expression */
1659 assert(!(!!filter_expression
^ !!filter
));
1661 DBG("Enable event command for event \'%s\'", event
->name
);
1665 ret
= validate_event_name(event
->name
);
1670 switch (domain
->type
) {
1671 case LTTNG_DOMAIN_KERNEL
:
1673 struct ltt_kernel_channel
*kchan
;
1676 * If a non-default channel has been created in the
1677 * session, explicitely require that -c chan_name needs
1680 if (session
->kernel_session
->has_non_default_channel
1681 && channel_name
[0] == '\0') {
1682 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1686 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1687 session
->kernel_session
);
1688 if (kchan
== NULL
) {
1689 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1690 LTTNG_BUFFER_GLOBAL
);
1692 ret
= LTTNG_ERR_FATAL
;
1695 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1697 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1698 if (ret
!= LTTNG_OK
) {
1704 channel_created
= 1;
1707 /* Get the newly created kernel channel pointer */
1708 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1709 session
->kernel_session
);
1710 if (kchan
== NULL
) {
1711 /* This sould not happen... */
1712 ret
= LTTNG_ERR_FATAL
;
1716 switch (event
->type
) {
1717 case LTTNG_EVENT_ALL
:
1719 char *filter_expression_a
= NULL
;
1720 struct lttng_filter_bytecode
*filter_a
= NULL
;
1723 * We need to duplicate filter_expression and filter,
1724 * because ownership is passed to first enable
1727 if (filter_expression
) {
1728 filter_expression_a
= strdup(filter_expression
);
1729 if (!filter_expression_a
) {
1730 ret
= LTTNG_ERR_FATAL
;
1735 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1737 free(filter_expression_a
);
1738 ret
= LTTNG_ERR_FATAL
;
1741 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1743 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1744 ret
= event_kernel_enable_event(kchan
, event
,
1745 filter_expression
, filter
);
1746 /* We have passed ownership */
1747 filter_expression
= NULL
;
1749 if (ret
!= LTTNG_OK
) {
1750 if (channel_created
) {
1751 /* Let's not leak a useless channel. */
1752 kernel_destroy_channel(kchan
);
1754 free(filter_expression_a
);
1758 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1759 ret
= event_kernel_enable_event(kchan
, event
,
1760 filter_expression_a
, filter_a
);
1761 /* We have passed ownership */
1762 filter_expression_a
= NULL
;
1764 if (ret
!= LTTNG_OK
) {
1769 case LTTNG_EVENT_PROBE
:
1770 case LTTNG_EVENT_FUNCTION
:
1771 case LTTNG_EVENT_FUNCTION_ENTRY
:
1772 case LTTNG_EVENT_TRACEPOINT
:
1773 ret
= event_kernel_enable_event(kchan
, event
,
1774 filter_expression
, filter
);
1775 /* We have passed ownership */
1776 filter_expression
= NULL
;
1778 if (ret
!= LTTNG_OK
) {
1779 if (channel_created
) {
1780 /* Let's not leak a useless channel. */
1781 kernel_destroy_channel(kchan
);
1786 case LTTNG_EVENT_SYSCALL
:
1787 ret
= event_kernel_enable_event(kchan
, event
,
1788 filter_expression
, filter
);
1789 /* We have passed ownership */
1790 filter_expression
= NULL
;
1792 if (ret
!= LTTNG_OK
) {
1797 ret
= LTTNG_ERR_UNK
;
1801 kernel_wait_quiescent(kernel_tracer_fd
);
1804 case LTTNG_DOMAIN_UST
:
1806 struct ltt_ust_channel
*uchan
;
1807 struct ltt_ust_session
*usess
= session
->ust_session
;
1812 * If a non-default channel has been created in the
1813 * session, explicitely require that -c chan_name needs
1816 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1817 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1821 /* Get channel from global UST domain */
1822 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1824 if (uchan
== NULL
) {
1825 /* Create default channel */
1826 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1827 usess
->buffer_type
);
1829 ret
= LTTNG_ERR_FATAL
;
1832 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1834 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1835 if (ret
!= LTTNG_OK
) {
1841 /* Get the newly created channel reference back */
1842 uchan
= trace_ust_find_channel_by_name(
1843 usess
->domain_global
.channels
, channel_name
);
1847 if (uchan
->domain
!= LTTNG_DOMAIN_UST
&& !internal_event
) {
1849 * Don't allow users to add UST events to channels which
1850 * are assigned to a userspace subdomain (JUL, Log4J,
1853 ret
= LTTNG_ERR_INVALID_CHANNEL_DOMAIN
;
1857 if (!internal_event
) {
1859 * Ensure the event name is not reserved for internal
1862 ret
= validate_ust_event_name(event
->name
);
1864 WARN("Userspace event name %s failed validation.",
1866 event
->name
: "NULL");
1867 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1872 /* At this point, the session and channel exist on the tracer */
1873 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1874 filter_expression
, filter
, exclusion
,
1876 /* We have passed ownership */
1877 filter_expression
= NULL
;
1880 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
1881 goto already_enabled
;
1882 } else if (ret
!= LTTNG_OK
) {
1887 case LTTNG_DOMAIN_LOG4J
:
1888 case LTTNG_DOMAIN_JUL
:
1889 case LTTNG_DOMAIN_PYTHON
:
1891 const char *default_event_name
, *default_chan_name
;
1893 struct lttng_event uevent
;
1894 struct lttng_domain tmp_dom
;
1895 struct ltt_ust_session
*usess
= session
->ust_session
;
1899 agt
= trace_ust_find_agent(usess
, domain
->type
);
1901 agt
= agent_create(domain
->type
);
1903 ret
= LTTNG_ERR_NOMEM
;
1906 agent_add(agt
, usess
->agents
);
1909 /* Create the default tracepoint. */
1910 memset(&uevent
, 0, sizeof(uevent
));
1911 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1912 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1913 default_event_name
= event_get_default_agent_ust_name(
1915 if (!default_event_name
) {
1916 ret
= LTTNG_ERR_FATAL
;
1919 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1920 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1923 * The domain type is changed because we are about to enable the
1924 * default channel and event for the JUL domain that are hardcoded.
1925 * This happens in the UST domain.
1927 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1928 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1930 switch (domain
->type
) {
1931 case LTTNG_DOMAIN_LOG4J
:
1932 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1934 case LTTNG_DOMAIN_JUL
:
1935 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1937 case LTTNG_DOMAIN_PYTHON
:
1938 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
1941 /* The switch/case we are in makes this impossible */
1946 char *filter_expression_copy
= NULL
;
1947 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1950 const size_t filter_size
= sizeof(
1951 struct lttng_filter_bytecode
)
1954 filter_copy
= zmalloc(filter_size
);
1956 ret
= LTTNG_ERR_NOMEM
;
1959 memcpy(filter_copy
, filter
, filter_size
);
1961 filter_expression_copy
=
1962 strdup(filter_expression
);
1963 if (!filter_expression
) {
1964 ret
= LTTNG_ERR_NOMEM
;
1967 if (!filter_expression_copy
|| !filter_copy
) {
1968 free(filter_expression_copy
);
1974 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
1975 (char *) default_chan_name
,
1976 &uevent
, filter_expression_copy
,
1977 filter_copy
, NULL
, wpipe
);
1980 if (ret
== LTTNG_ERR_UST_EVENT_ENABLED
) {
1981 goto already_enabled
;
1982 } else if (ret
!= LTTNG_OK
) {
1986 /* The wild card * means that everything should be enabled. */
1987 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1988 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
1991 ret
= event_agent_enable(usess
, agt
, event
, filter
,
1995 filter_expression
= NULL
;
1996 if (ret
!= LTTNG_OK
) {
2003 ret
= LTTNG_ERR_UND
;
2011 free(filter_expression
);
2019 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2020 * We own filter, exclusion, and filter_expression.
2022 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
2023 char *channel_name
, struct lttng_event
*event
,
2024 char *filter_expression
,
2025 struct lttng_filter_bytecode
*filter
,
2026 struct lttng_event_exclusion
*exclusion
,
2029 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2030 filter_expression
, filter
, exclusion
, wpipe
, false);
2034 * Enable an event which is internal to LTTng. An internal should
2035 * never be made visible to clients and are immune to checks such as
2038 static int cmd_enable_event_internal(struct ltt_session
*session
,
2039 struct lttng_domain
*domain
,
2040 char *channel_name
, struct lttng_event
*event
,
2041 char *filter_expression
,
2042 struct lttng_filter_bytecode
*filter
,
2043 struct lttng_event_exclusion
*exclusion
,
2046 return _cmd_enable_event(session
, domain
, channel_name
, event
,
2047 filter_expression
, filter
, exclusion
, wpipe
, true);
2051 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2053 ssize_t
cmd_list_tracepoints(enum lttng_domain_type domain
,
2054 struct lttng_event
**events
)
2057 ssize_t nb_events
= 0;
2060 case LTTNG_DOMAIN_KERNEL
:
2061 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2062 if (nb_events
< 0) {
2063 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2067 case LTTNG_DOMAIN_UST
:
2068 nb_events
= ust_app_list_events(events
);
2069 if (nb_events
< 0) {
2070 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2074 case LTTNG_DOMAIN_LOG4J
:
2075 case LTTNG_DOMAIN_JUL
:
2076 case LTTNG_DOMAIN_PYTHON
:
2077 nb_events
= agent_list_events(events
, domain
);
2078 if (nb_events
< 0) {
2079 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2084 ret
= LTTNG_ERR_UND
;
2091 /* Return negative value to differentiate return code */
2096 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2098 ssize_t
cmd_list_tracepoint_fields(enum lttng_domain_type domain
,
2099 struct lttng_event_field
**fields
)
2102 ssize_t nb_fields
= 0;
2105 case LTTNG_DOMAIN_UST
:
2106 nb_fields
= ust_app_list_event_fields(fields
);
2107 if (nb_fields
< 0) {
2108 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2112 case LTTNG_DOMAIN_KERNEL
:
2113 default: /* fall-through */
2114 ret
= LTTNG_ERR_UND
;
2121 /* Return negative value to differentiate return code */
2125 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
2127 return syscall_table_list(events
);
2131 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2133 * Called with session lock held.
2135 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
2136 enum lttng_domain_type domain
, int32_t **pids
)
2139 ssize_t nr_pids
= 0;
2142 case LTTNG_DOMAIN_KERNEL
:
2144 struct ltt_kernel_session
*ksess
;
2146 ksess
= session
->kernel_session
;
2147 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
2149 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
2154 case LTTNG_DOMAIN_UST
:
2156 struct ltt_ust_session
*usess
;
2158 usess
= session
->ust_session
;
2159 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
2161 ret
= LTTNG_ERR_UST_LIST_FAIL
;
2166 case LTTNG_DOMAIN_LOG4J
:
2167 case LTTNG_DOMAIN_JUL
:
2168 case LTTNG_DOMAIN_PYTHON
:
2170 ret
= LTTNG_ERR_UND
;
2177 /* Return negative value to differentiate return code */
2182 * Command LTTNG_START_TRACE processed by the client thread.
2184 * Called with session mutex held.
2186 int cmd_start_trace(struct ltt_session
*session
)
2189 unsigned long nb_chan
= 0;
2190 struct ltt_kernel_session
*ksession
;
2191 struct ltt_ust_session
*usess
;
2195 /* Ease our life a bit ;) */
2196 ksession
= session
->kernel_session
;
2197 usess
= session
->ust_session
;
2199 /* Is the session already started? */
2200 if (session
->active
) {
2201 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2206 * Starting a session without channel is useless since after that it's not
2207 * possible to enable channel thus inform the client.
2209 if (usess
&& usess
->domain_global
.channels
) {
2210 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2213 nb_chan
+= ksession
->channel_count
;
2216 ret
= LTTNG_ERR_NO_CHANNEL
;
2220 /* Kernel tracing */
2221 if (ksession
!= NULL
) {
2222 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2223 if (ret
!= LTTNG_OK
) {
2228 /* Flag session that trace should start automatically */
2231 * Even though the start trace might fail, flag this session active so
2232 * other application coming in are started by default.
2236 ret
= ust_app_start_trace_all(usess
);
2238 ret
= LTTNG_ERR_UST_START_FAIL
;
2243 /* Flag this after a successful start. */
2244 session
->has_been_started
= 1;
2245 session
->active
= 1;
2254 * Command LTTNG_STOP_TRACE processed by the client thread.
2256 int cmd_stop_trace(struct ltt_session
*session
)
2259 struct ltt_kernel_channel
*kchan
;
2260 struct ltt_kernel_session
*ksession
;
2261 struct ltt_ust_session
*usess
;
2266 ksession
= session
->kernel_session
;
2267 usess
= session
->ust_session
;
2269 /* Session is not active. Skip everythong and inform the client. */
2270 if (!session
->active
) {
2271 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2276 if (ksession
&& ksession
->active
) {
2277 DBG("Stop kernel tracing");
2279 /* Flush metadata if exist */
2280 if (ksession
->metadata_stream_fd
>= 0) {
2281 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2283 ERR("Kernel metadata flush failed");
2287 /* Flush all buffers before stopping */
2288 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2289 ret
= kernel_flush_buffer(kchan
);
2291 ERR("Kernel flush buffer error");
2295 ret
= kernel_stop_session(ksession
);
2297 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2301 kernel_wait_quiescent(kernel_tracer_fd
);
2303 ksession
->active
= 0;
2306 if (usess
&& usess
->active
) {
2308 * Even though the stop trace might fail, flag this session inactive so
2309 * other application coming in are not started by default.
2313 ret
= ust_app_stop_trace_all(usess
);
2315 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2320 /* Flag inactive after a successful stop. */
2321 session
->active
= 0;
2329 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2331 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2332 struct lttng_uri
*uris
)
2335 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2336 struct ltt_ust_session
*usess
= session
->ust_session
;
2342 /* Can't set consumer URI if the session is active. */
2343 if (session
->active
) {
2344 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2348 /* Set the "global" consumer URIs */
2349 for (i
= 0; i
< nb_uri
; i
++) {
2350 ret
= add_uri_to_consumer(session
->consumer
,
2351 &uris
[i
], 0, session
->name
);
2352 if (ret
!= LTTNG_OK
) {
2357 /* Set UST session URIs */
2358 if (session
->ust_session
) {
2359 for (i
= 0; i
< nb_uri
; i
++) {
2360 ret
= add_uri_to_consumer(
2361 session
->ust_session
->consumer
,
2362 &uris
[i
], LTTNG_DOMAIN_UST
,
2364 if (ret
!= LTTNG_OK
) {
2370 /* Set kernel session URIs */
2371 if (session
->kernel_session
) {
2372 for (i
= 0; i
< nb_uri
; i
++) {
2373 ret
= add_uri_to_consumer(
2374 session
->kernel_session
->consumer
,
2375 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2377 if (ret
!= LTTNG_OK
) {
2384 * Make sure to set the session in output mode after we set URI since a
2385 * session can be created without URL (thus flagged in no output mode).
2387 session
->output_traces
= 1;
2389 ksess
->output_traces
= 1;
2393 usess
->output_traces
= 1;
2404 * Command LTTNG_CREATE_SESSION processed by the client thread.
2406 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2407 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2410 struct ltt_session
*session
;
2416 * Verify if the session already exist
2418 * XXX: There is no need for the session lock list here since the caller
2419 * (process_client_msg) is holding it. We might want to change that so a
2420 * single command does not lock the entire session list.
2422 session
= session_find_by_name(name
);
2423 if (session
!= NULL
) {
2424 ret
= LTTNG_ERR_EXIST_SESS
;
2428 /* Create tracing session in the registry */
2429 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2430 LTTNG_SOCK_GET_GID_CRED(creds
));
2431 if (ret
!= LTTNG_OK
) {
2436 * Get the newly created session pointer back
2438 * XXX: There is no need for the session lock list here since the caller
2439 * (process_client_msg) is holding it. We might want to change that so a
2440 * single command does not lock the entire session list.
2442 session
= session_find_by_name(name
);
2445 session
->live_timer
= live_timer
;
2446 /* Create default consumer output for the session not yet created. */
2447 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2448 if (session
->consumer
== NULL
) {
2449 ret
= LTTNG_ERR_FATAL
;
2450 goto consumer_error
;
2454 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2455 if (ret
!= LTTNG_OK
) {
2456 goto consumer_error
;
2458 session
->output_traces
= 1;
2460 session
->output_traces
= 0;
2461 DBG2("Session %s created with no output", session
->name
);
2464 session
->consumer
->enabled
= 1;
2469 session_destroy(session
);
2476 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2478 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2479 size_t nb_uri
, lttng_sock_cred
*creds
)
2482 struct ltt_session
*session
;
2483 struct snapshot_output
*new_output
= NULL
;
2489 * Create session in no output mode with URIs set to NULL. The uris we've
2490 * received are for a default snapshot output if one.
2492 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2493 if (ret
!= LTTNG_OK
) {
2497 /* Get the newly created session pointer back. This should NEVER fail. */
2498 session
= session_find_by_name(name
);
2501 /* Flag session for snapshot mode. */
2502 session
->snapshot_mode
= 1;
2504 /* Skip snapshot output creation if no URI is given. */
2509 new_output
= snapshot_output_alloc();
2511 ret
= LTTNG_ERR_NOMEM
;
2512 goto error_snapshot_alloc
;
2515 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2516 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2518 if (ret
== -ENOMEM
) {
2519 ret
= LTTNG_ERR_NOMEM
;
2521 ret
= LTTNG_ERR_INVALID
;
2523 goto error_snapshot
;
2527 snapshot_add_output(&session
->snapshot
, new_output
);
2534 snapshot_output_destroy(new_output
);
2535 error_snapshot_alloc
:
2536 session_destroy(session
);
2542 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2544 * Called with session lock held.
2546 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2549 struct ltt_ust_session
*usess
;
2550 struct ltt_kernel_session
*ksess
;
2555 usess
= session
->ust_session
;
2556 ksess
= session
->kernel_session
;
2558 /* Clean kernel session teardown */
2559 kernel_destroy_session(ksess
);
2561 /* UST session teardown */
2563 /* Close any relayd session */
2564 consumer_output_send_destroy_relayd(usess
->consumer
);
2566 /* Destroy every UST application related to this session. */
2567 ret
= ust_app_destroy_trace_all(usess
);
2569 ERR("Error in ust_app_destroy_trace_all");
2572 /* Clean up the rest. */
2573 trace_ust_destroy_session(usess
);
2577 * Must notify the kernel thread here to update it's poll set in order to
2578 * remove the channel(s)' fd just destroyed.
2580 ret
= notify_thread_pipe(wpipe
);
2582 PERROR("write kernel poll pipe");
2585 ret
= session_destroy(session
);
2591 * Command LTTNG_CALIBRATE processed by the client thread.
2593 int cmd_calibrate(enum lttng_domain_type domain
,
2594 struct lttng_calibrate
*calibrate
)
2599 case LTTNG_DOMAIN_KERNEL
:
2601 struct lttng_kernel_calibrate kcalibrate
;
2603 switch (calibrate
->type
) {
2604 case LTTNG_CALIBRATE_FUNCTION
:
2606 /* Default and only possible calibrate option. */
2607 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2611 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2613 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2618 case LTTNG_DOMAIN_UST
:
2620 struct lttng_ust_calibrate ucalibrate
;
2622 switch (calibrate
->type
) {
2623 case LTTNG_CALIBRATE_FUNCTION
:
2625 /* Default and only possible calibrate option. */
2626 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2630 ret
= ust_app_calibrate_glb(&ucalibrate
);
2632 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2638 ret
= LTTNG_ERR_UND
;
2649 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2651 int cmd_register_consumer(struct ltt_session
*session
,
2652 enum lttng_domain_type domain
, const char *sock_path
,
2653 struct consumer_data
*cdata
)
2656 struct consumer_socket
*socket
= NULL
;
2663 case LTTNG_DOMAIN_KERNEL
:
2665 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2669 /* Can't register a consumer if there is already one */
2670 if (ksess
->consumer_fds_sent
!= 0) {
2671 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2675 sock
= lttcomm_connect_unix_sock(sock_path
);
2677 ret
= LTTNG_ERR_CONNECT_FAIL
;
2680 cdata
->cmd_sock
= sock
;
2682 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2683 if (socket
== NULL
) {
2686 PERROR("close register consumer");
2688 cdata
->cmd_sock
= -1;
2689 ret
= LTTNG_ERR_FATAL
;
2693 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2694 if (socket
->lock
== NULL
) {
2695 PERROR("zmalloc pthread mutex");
2696 ret
= LTTNG_ERR_FATAL
;
2699 pthread_mutex_init(socket
->lock
, NULL
);
2700 socket
->registered
= 1;
2703 consumer_add_socket(socket
, ksess
->consumer
);
2706 pthread_mutex_lock(&cdata
->pid_mutex
);
2708 pthread_mutex_unlock(&cdata
->pid_mutex
);
2713 /* TODO: Userspace tracing */
2714 ret
= LTTNG_ERR_UND
;
2722 consumer_destroy_socket(socket
);
2728 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2730 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2731 struct lttng_domain
**domains
)
2736 struct lttng_ht_iter iter
;
2738 if (session
->kernel_session
!= NULL
) {
2739 DBG3("Listing domains found kernel domain");
2743 if (session
->ust_session
!= NULL
) {
2744 DBG3("Listing domains found UST global domain");
2748 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2750 if (agt
->being_used
) {
2761 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2762 if (*domains
== NULL
) {
2763 ret
= LTTNG_ERR_FATAL
;
2767 if (session
->kernel_session
!= NULL
) {
2768 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2770 /* Kernel session buffer type is always GLOBAL */
2771 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2776 if (session
->ust_session
!= NULL
) {
2777 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2778 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2782 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2784 if (agt
->being_used
) {
2785 (*domains
)[index
].type
= agt
->domain
;
2786 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2796 /* Return negative value to differentiate return code */
2802 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2804 ssize_t
cmd_list_channels(enum lttng_domain_type domain
,
2805 struct ltt_session
*session
, struct lttng_channel
**channels
)
2808 ssize_t nb_chan
= 0;
2811 case LTTNG_DOMAIN_KERNEL
:
2812 if (session
->kernel_session
!= NULL
) {
2813 nb_chan
= session
->kernel_session
->channel_count
;
2815 DBG3("Number of kernel channels %zd", nb_chan
);
2817 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2820 case LTTNG_DOMAIN_UST
:
2821 if (session
->ust_session
!= NULL
) {
2823 nb_chan
= lttng_ht_get_count(
2824 session
->ust_session
->domain_global
.channels
);
2827 DBG3("Number of UST global channels %zd", nb_chan
);
2829 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2834 ret
= LTTNG_ERR_UND
;
2839 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2840 if (*channels
== NULL
) {
2841 ret
= LTTNG_ERR_FATAL
;
2845 list_lttng_channels(domain
, session
, *channels
);
2851 /* Return negative value to differentiate return code */
2856 * Command LTTNG_LIST_EVENTS processed by the client thread.
2858 ssize_t
cmd_list_events(enum lttng_domain_type domain
,
2859 struct ltt_session
*session
, char *channel_name
,
2860 struct lttng_event
**events
, size_t *total_size
)
2863 ssize_t nb_event
= 0;
2866 case LTTNG_DOMAIN_KERNEL
:
2867 if (session
->kernel_session
!= NULL
) {
2868 nb_event
= list_lttng_kernel_events(channel_name
,
2869 session
->kernel_session
, events
,
2873 case LTTNG_DOMAIN_UST
:
2875 if (session
->ust_session
!= NULL
) {
2876 nb_event
= list_lttng_ust_global_events(channel_name
,
2877 &session
->ust_session
->domain_global
, events
,
2882 case LTTNG_DOMAIN_LOG4J
:
2883 case LTTNG_DOMAIN_JUL
:
2884 case LTTNG_DOMAIN_PYTHON
:
2885 if (session
->ust_session
) {
2886 struct lttng_ht_iter iter
;
2890 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2891 &iter
.iter
, agt
, node
.node
) {
2892 if (agt
->domain
== domain
) {
2893 nb_event
= list_lttng_agent_events(
2903 ret
= LTTNG_ERR_UND
;
2910 /* Return negative value to differentiate return code */
2915 * Using the session list, filled a lttng_session array to send back to the
2916 * client for session listing.
2918 * The session list lock MUST be acquired before calling this function. Use
2919 * session_lock_list() and session_unlock_list().
2921 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2926 struct ltt_session
*session
;
2927 struct ltt_session_list
*list
= session_get_list();
2929 DBG("Getting all available session for UID %d GID %d",
2932 * Iterate over session list and append data after the control struct in
2935 cds_list_for_each_entry(session
, &list
->head
, list
) {
2937 * Only list the sessions the user can control.
2939 if (!session_access_ok(session
, uid
, gid
)) {
2943 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2944 struct ltt_ust_session
*usess
= session
->ust_session
;
2946 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2947 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2948 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2949 ret
= build_network_session_path(sessions
[i
].path
,
2950 sizeof(sessions
[i
].path
), session
);
2952 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2953 session
->consumer
->dst
.trace_path
);
2956 PERROR("snprintf session path");
2960 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2961 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2962 sessions
[i
].enabled
= session
->active
;
2963 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2964 sessions
[i
].live_timer_interval
= session
->live_timer
;
2970 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2971 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2973 int cmd_data_pending(struct ltt_session
*session
)
2976 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2977 struct ltt_ust_session
*usess
= session
->ust_session
;
2981 /* Session MUST be stopped to ask for data availability. */
2982 if (session
->active
) {
2983 ret
= LTTNG_ERR_SESSION_STARTED
;
2987 * If stopped, just make sure we've started before else the above call
2988 * will always send that there is data pending.
2990 * The consumer assumes that when the data pending command is received,
2991 * the trace has been started before or else no output data is written
2992 * by the streams which is a condition for data pending. So, this is
2993 * *VERY* important that we don't ask the consumer before a start
2996 if (!session
->has_been_started
) {
3002 if (ksess
&& ksess
->consumer
) {
3003 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
3005 /* Data is still being extracted for the kernel. */
3010 if (usess
&& usess
->consumer
) {
3011 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
3013 /* Data is still being extracted for the kernel. */
3018 /* Data is ready to be read by a viewer */
3026 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3028 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3030 int cmd_snapshot_add_output(struct ltt_session
*session
,
3031 struct lttng_snapshot_output
*output
, uint32_t *id
)
3034 struct snapshot_output
*new_output
;
3039 DBG("Cmd snapshot add output for session %s", session
->name
);
3042 * Permission denied to create an output if the session is not
3043 * set in no output mode.
3045 if (session
->output_traces
) {
3046 ret
= LTTNG_ERR_EPERM
;
3050 /* Only one output is allowed until we have the "tee" feature. */
3051 if (session
->snapshot
.nb_output
== 1) {
3052 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
3056 new_output
= snapshot_output_alloc();
3058 ret
= LTTNG_ERR_NOMEM
;
3062 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3063 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
3064 &session
->snapshot
);
3066 if (ret
== -ENOMEM
) {
3067 ret
= LTTNG_ERR_NOMEM
;
3069 ret
= LTTNG_ERR_INVALID
;
3075 snapshot_add_output(&session
->snapshot
, new_output
);
3077 *id
= new_output
->id
;
3084 snapshot_output_destroy(new_output
);
3090 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3092 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3094 int cmd_snapshot_del_output(struct ltt_session
*session
,
3095 struct lttng_snapshot_output
*output
)
3098 struct snapshot_output
*sout
= NULL
;
3106 * Permission denied to create an output if the session is not
3107 * set in no output mode.
3109 if (session
->output_traces
) {
3110 ret
= LTTNG_ERR_EPERM
;
3115 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
3117 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
3118 } else if (*output
->name
!= '\0') {
3119 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
3121 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
3124 ret
= LTTNG_ERR_INVALID
;
3128 snapshot_delete_output(&session
->snapshot
, sout
);
3129 snapshot_output_destroy(sout
);
3138 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3140 * If no output is available, outputs is untouched and 0 is returned.
3142 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3144 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
3145 struct lttng_snapshot_output
**outputs
)
3148 struct lttng_snapshot_output
*list
= NULL
;
3149 struct lttng_ht_iter iter
;
3150 struct snapshot_output
*output
;
3155 DBG("Cmd snapshot list outputs for session %s", session
->name
);
3158 * Permission denied to create an output if the session is not
3159 * set in no output mode.
3161 if (session
->output_traces
) {
3162 ret
= -LTTNG_ERR_EPERM
;
3166 if (session
->snapshot
.nb_output
== 0) {
3171 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
3173 ret
= -LTTNG_ERR_NOMEM
;
3177 /* Copy list from session to the new list object. */
3179 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
3180 output
, node
.node
) {
3181 assert(output
->consumer
);
3182 list
[idx
].id
= output
->id
;
3183 list
[idx
].max_size
= output
->max_size
;
3184 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
3185 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
3186 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
3187 sizeof(list
[idx
].ctrl_url
));
3190 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3191 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3193 ret
= -LTTNG_ERR_NOMEM
;
3198 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3199 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3201 ret
= -LTTNG_ERR_NOMEM
;
3210 ret
= session
->snapshot
.nb_output
;
3218 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3219 * snapshot output is *not* set with a remote destination.
3221 * Return 0 on success or a LTTNG_ERR code.
3223 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3224 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3227 struct lttng_ht_iter iter
;
3228 struct consumer_socket
*socket
;
3231 assert(snap_output
);
3234 DBG2("Set relayd object from snapshot output");
3236 /* Ignore if snapshot consumer output is not network. */
3237 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3242 * For each consumer socket, create and send the relayd object of the
3246 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3247 socket
, node
.node
) {
3248 ret
= send_consumer_relayd_sockets(0, session
->id
,
3249 snap_output
->consumer
, socket
,
3250 session
->name
, session
->hostname
,
3251 session
->live_timer
);
3252 if (ret
!= LTTNG_OK
) {
3264 * Record a kernel snapshot.
3266 * Return LTTNG_OK on success or a LTTNG_ERR code.
3268 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3269 struct snapshot_output
*output
, struct ltt_session
*session
,
3270 int wait
, uint64_t nb_packets_per_stream
)
3278 /* Get the datetime for the snapshot output directory. */
3279 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3280 sizeof(output
->datetime
));
3282 ret
= LTTNG_ERR_INVALID
;
3287 * Copy kernel session sockets so we can communicate with the right
3288 * consumer for the snapshot record command.
3290 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3292 ret
= LTTNG_ERR_NOMEM
;
3296 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3297 if (ret
!= LTTNG_OK
) {
3298 goto error_snapshot
;
3301 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3302 if (ret
!= LTTNG_OK
) {
3303 goto error_snapshot
;
3310 /* Clean up copied sockets so this output can use some other later on. */
3311 consumer_destroy_output_sockets(output
->consumer
);
3318 * Record a UST snapshot.
3320 * Return 0 on success or a LTTNG_ERR error code.
3322 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3323 struct snapshot_output
*output
, struct ltt_session
*session
,
3324 int wait
, uint64_t nb_packets_per_stream
)
3332 /* Get the datetime for the snapshot output directory. */
3333 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3334 sizeof(output
->datetime
));
3336 ret
= LTTNG_ERR_INVALID
;
3341 * Copy UST session sockets so we can communicate with the right
3342 * consumer for the snapshot record command.
3344 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3346 ret
= LTTNG_ERR_NOMEM
;
3350 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3351 if (ret
!= LTTNG_OK
) {
3352 goto error_snapshot
;
3355 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3359 ret
= LTTNG_ERR_INVALID
;
3362 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3365 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3368 goto error_snapshot
;
3374 /* Clean up copied sockets so this output can use some other later on. */
3375 consumer_destroy_output_sockets(output
->consumer
);
3381 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3382 uint64_t cur_nr_packets
)
3384 uint64_t tot_size
= 0;
3386 if (session
->kernel_session
) {
3387 struct ltt_kernel_channel
*chan
;
3388 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3390 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3391 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3393 * Don't take channel into account if we
3394 * already grab all its packets.
3398 tot_size
+= chan
->channel
->attr
.subbuf_size
3399 * chan
->stream_count
;
3403 if (session
->ust_session
) {
3404 struct ltt_ust_session
*usess
= session
->ust_session
;
3406 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3414 * Calculate the number of packets we can grab from each stream that
3415 * fits within the overall snapshot max size.
3417 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3418 * the number of packets per stream.
3420 * TODO: this approach is not perfect: we consider the worse case
3421 * (packet filling the sub-buffers) as an upper bound, but we could do
3422 * better if we do this calculation while we actually grab the packet
3423 * content: we would know how much padding we don't actually store into
3426 * This algorithm is currently bounded by the number of packets per
3429 * Since we call this algorithm before actually grabbing the data, it's
3430 * an approximation: for instance, applications could appear/disappear
3431 * in between this call and actually grabbing data.
3434 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3437 uint64_t cur_nb_packets
= 0;
3440 return 0; /* Infinite */
3443 size_left
= max_size
;
3445 uint64_t one_more_packet_tot_size
;
3447 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3449 if (!one_more_packet_tot_size
) {
3450 /* We are already grabbing all packets. */
3453 size_left
-= one_more_packet_tot_size
;
3454 if (size_left
< 0) {
3459 if (!cur_nb_packets
) {
3460 /* Not enough room to grab one packet of each stream, error. */
3463 return cur_nb_packets
;
3467 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3469 * The wait parameter is ignored so this call always wait for the snapshot to
3470 * complete before returning.
3472 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3474 int cmd_snapshot_record(struct ltt_session
*session
,
3475 struct lttng_snapshot_output
*output
, int wait
)
3478 unsigned int use_tmp_output
= 0;
3479 struct snapshot_output tmp_output
;
3480 unsigned int snapshot_success
= 0;
3485 DBG("Cmd snapshot record for session %s", session
->name
);
3488 * Permission denied to create an output if the session is not
3489 * set in no output mode.
3491 if (session
->output_traces
) {
3492 ret
= LTTNG_ERR_EPERM
;
3496 /* The session needs to be started at least once. */
3497 if (!session
->has_been_started
) {
3498 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3502 /* Use temporary output for the session. */
3503 if (*output
->ctrl_url
!= '\0') {
3504 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3505 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3508 if (ret
== -ENOMEM
) {
3509 ret
= LTTNG_ERR_NOMEM
;
3511 ret
= LTTNG_ERR_INVALID
;
3515 /* Use the global session count for the temporary snapshot. */
3516 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3520 if (session
->kernel_session
) {
3521 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3523 if (use_tmp_output
) {
3524 int64_t nb_packets_per_stream
;
3526 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3527 tmp_output
.max_size
);
3528 if (nb_packets_per_stream
< 0) {
3529 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3532 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3533 wait
, nb_packets_per_stream
);
3534 if (ret
!= LTTNG_OK
) {
3537 snapshot_success
= 1;
3539 struct snapshot_output
*sout
;
3540 struct lttng_ht_iter iter
;
3543 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3544 &iter
.iter
, sout
, node
.node
) {
3545 int64_t nb_packets_per_stream
;
3548 * Make a local copy of the output and assign the possible
3549 * temporary value given by the caller.
3551 memset(&tmp_output
, 0, sizeof(tmp_output
));
3552 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3554 if (output
->max_size
!= (uint64_t) -1ULL) {
3555 tmp_output
.max_size
= output
->max_size
;
3558 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3559 tmp_output
.max_size
);
3560 if (nb_packets_per_stream
< 0) {
3561 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3565 /* Use temporary name. */
3566 if (*output
->name
!= '\0') {
3567 strncpy(tmp_output
.name
, output
->name
,
3568 sizeof(tmp_output
.name
));
3571 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3573 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3574 session
, wait
, nb_packets_per_stream
);
3575 if (ret
!= LTTNG_OK
) {
3579 snapshot_success
= 1;
3585 if (session
->ust_session
) {
3586 struct ltt_ust_session
*usess
= session
->ust_session
;
3588 if (use_tmp_output
) {
3589 int64_t nb_packets_per_stream
;
3591 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3592 tmp_output
.max_size
);
3593 if (nb_packets_per_stream
< 0) {
3594 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3597 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3598 wait
, nb_packets_per_stream
);
3599 if (ret
!= LTTNG_OK
) {
3602 snapshot_success
= 1;
3604 struct snapshot_output
*sout
;
3605 struct lttng_ht_iter iter
;
3608 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3609 &iter
.iter
, sout
, node
.node
) {
3610 int64_t nb_packets_per_stream
;
3613 * Make a local copy of the output and assign the possible
3614 * temporary value given by the caller.
3616 memset(&tmp_output
, 0, sizeof(tmp_output
));
3617 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3619 if (output
->max_size
!= (uint64_t) -1ULL) {
3620 tmp_output
.max_size
= output
->max_size
;
3623 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3624 tmp_output
.max_size
);
3625 if (nb_packets_per_stream
< 0) {
3626 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3631 /* Use temporary name. */
3632 if (*output
->name
!= '\0') {
3633 strncpy(tmp_output
.name
, output
->name
,
3634 sizeof(tmp_output
.name
));
3637 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3639 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3640 wait
, nb_packets_per_stream
);
3641 if (ret
!= LTTNG_OK
) {
3645 snapshot_success
= 1;
3651 if (snapshot_success
) {
3652 session
->snapshot
.nb_snapshot
++;
3654 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3662 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3664 int cmd_set_session_shm_path(struct ltt_session
*session
,
3665 const char *shm_path
)
3671 * Can only set shm path before session is started.
3673 if (session
->has_been_started
) {
3674 return LTTNG_ERR_SESSION_STARTED
;
3677 strncpy(session
->shm_path
, shm_path
,
3678 sizeof(session
->shm_path
));
3679 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3685 * Init command subsystem.
3690 * Set network sequence index to 1 for streams to match a relayd
3691 * socket on the consumer side.
3693 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3694 relayd_net_seq_idx
= 1;
3695 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3697 DBG("Command subsystem initialized");