2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <urcu/list.h>
22 #include <urcu/uatomic.h>
24 #include <common/defaults.h>
25 #include <common/common.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27 #include <common/relayd/relayd.h>
28 #include <common/utils.h>
33 #include "health-sessiond.h"
35 #include "kernel-consumer.h"
36 #include "lttng-sessiond.h"
43 * Used to keep a unique index for each relayd socket created where this value
44 * is associated with streams on the consumer so it can match the right relayd
45 * to send to. It must be accessed with the relayd_net_seq_idx_lock
48 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
49 static uint64_t relayd_net_seq_idx
;
52 * Both functions below are special case for the Kernel domain when
53 * enabling/disabling all events.
56 int enable_kevent_all(struct ltt_session
*session
,
57 struct lttng_domain
*domain
, char *channel_name
,
58 struct lttng_event
*event
,
59 char *filter_expression
,
60 struct lttng_filter_bytecode
*filter
, int wpipe
);
62 int disable_kevent_all(struct ltt_session
*session
, int domain
,
64 struct lttng_event
*event
);
67 * Create a session path used by list_lttng_sessions for the case that the
68 * session consumer is on the network.
70 static int build_network_session_path(char *dst
, size_t size
,
71 struct ltt_session
*session
)
73 int ret
, kdata_port
, udata_port
;
74 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
75 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
80 memset(tmp_urls
, 0, sizeof(tmp_urls
));
81 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
83 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
85 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
86 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
87 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
90 if (session
->ust_session
&& session
->ust_session
->consumer
) {
91 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
92 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
95 if (uuri
== NULL
&& kuri
== NULL
) {
96 uri
= &session
->consumer
->dst
.net
.control
;
97 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
98 } else if (kuri
&& uuri
) {
99 ret
= uri_compare(kuri
, uuri
);
103 /* Build uuri URL string */
104 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
111 } else if (kuri
&& uuri
== NULL
) {
113 } else if (uuri
&& kuri
== NULL
) {
117 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
123 * Do we have a UST url set. If yes, this means we have both kernel and UST
126 if (*tmp_uurl
!= '\0') {
127 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
128 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
131 if (kuri
|| (!kuri
&& !uuri
)) {
134 /* No kernel URI, use the UST port. */
137 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
145 * Fill lttng_channel array of all channels.
147 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
148 struct lttng_channel
*channels
)
151 struct ltt_kernel_channel
*kchan
;
153 DBG("Listing channels for session %s", session
->name
);
156 case LTTNG_DOMAIN_KERNEL
:
157 /* Kernel channels */
158 if (session
->kernel_session
!= NULL
) {
159 cds_list_for_each_entry(kchan
,
160 &session
->kernel_session
->channel_list
.head
, list
) {
161 /* Copy lttng_channel struct to array */
162 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
163 channels
[i
].enabled
= kchan
->enabled
;
168 case LTTNG_DOMAIN_UST
:
170 struct lttng_ht_iter iter
;
171 struct ltt_ust_channel
*uchan
;
174 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
175 &iter
.iter
, uchan
, node
.node
) {
176 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
177 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
178 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
179 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
180 channels
[i
].attr
.switch_timer_interval
=
181 uchan
->attr
.switch_timer_interval
;
182 channels
[i
].attr
.read_timer_interval
=
183 uchan
->attr
.read_timer_interval
;
184 channels
[i
].enabled
= uchan
->enabled
;
185 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
186 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
187 switch (uchan
->attr
.output
) {
190 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
204 * Create a list of agent domain events.
206 * Return number of events in list on success or else a negative value.
208 static int list_lttng_agent_events(struct agent
*agt
,
209 struct lttng_event
**events
)
212 unsigned int nb_event
= 0;
213 struct agent_event
*event
;
214 struct lttng_event
*tmp_events
;
215 struct lttng_ht_iter iter
;
220 DBG3("Listing agent events");
223 nb_event
= lttng_ht_get_count(agt
->events
);
230 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
232 PERROR("zmalloc agent events session");
233 ret
= -LTTNG_ERR_FATAL
;
238 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
239 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
240 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
241 tmp_events
[i
].enabled
= event
->enabled
;
242 tmp_events
[i
].loglevel
= event
->loglevel
;
243 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
248 *events
= tmp_events
;
252 assert(nb_event
== i
);
257 * Create a list of ust global domain events.
259 static int list_lttng_ust_global_events(char *channel_name
,
260 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
263 unsigned int nb_event
= 0;
264 struct lttng_ht_iter iter
;
265 struct lttng_ht_node_str
*node
;
266 struct ltt_ust_channel
*uchan
;
267 struct ltt_ust_event
*uevent
;
268 struct lttng_event
*tmp
;
270 DBG("Listing UST global events for channel %s", channel_name
);
274 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
275 node
= lttng_ht_iter_get_node_str(&iter
);
277 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
281 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
283 nb_event
+= lttng_ht_get_count(uchan
->events
);
290 DBG3("Listing UST global %d events", nb_event
);
292 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
294 ret
= LTTNG_ERR_FATAL
;
298 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
299 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
300 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
301 tmp
[i
].enabled
= uevent
->enabled
;
303 switch (uevent
->attr
.instrumentation
) {
304 case LTTNG_UST_TRACEPOINT
:
305 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
307 case LTTNG_UST_PROBE
:
308 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
310 case LTTNG_UST_FUNCTION
:
311 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
315 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
316 switch (uevent
->attr
.loglevel_type
) {
317 case LTTNG_UST_LOGLEVEL_ALL
:
318 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
320 case LTTNG_UST_LOGLEVEL_RANGE
:
321 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
323 case LTTNG_UST_LOGLEVEL_SINGLE
:
324 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
327 if (uevent
->filter
) {
330 if (uevent
->exclusion
) {
331 tmp
[i
].exclusion
= 1;
345 * Fill lttng_event array of all kernel events in the channel.
347 static int list_lttng_kernel_events(char *channel_name
,
348 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
351 unsigned int nb_event
;
352 struct ltt_kernel_event
*event
;
353 struct ltt_kernel_channel
*kchan
;
355 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
357 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
361 nb_event
= kchan
->event_count
;
363 DBG("Listing events for channel %s", kchan
->channel
->name
);
370 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
371 if (*events
== NULL
) {
372 ret
= LTTNG_ERR_FATAL
;
376 /* Kernel channels */
377 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
378 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
379 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
380 (*events
)[i
].enabled
= event
->enabled
;
382 switch (event
->event
->instrumentation
) {
383 case LTTNG_KERNEL_TRACEPOINT
:
384 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
386 case LTTNG_KERNEL_KRETPROBE
:
387 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
388 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
389 sizeof(struct lttng_kernel_kprobe
));
391 case LTTNG_KERNEL_KPROBE
:
392 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
393 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
394 sizeof(struct lttng_kernel_kprobe
));
396 case LTTNG_KERNEL_FUNCTION
:
397 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
398 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
399 sizeof(struct lttng_kernel_function
));
401 case LTTNG_KERNEL_NOOP
:
402 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
404 case LTTNG_KERNEL_SYSCALL
:
405 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
407 case LTTNG_KERNEL_ALL
:
418 new_size
= syscall_list_channel(kchan
, events
, nb_event
);
430 /* Negate the error code to differentiate the size from an error */
435 * Add URI so the consumer output object. Set the correct path depending on the
436 * domain adding the default trace directory.
438 static int add_uri_to_consumer(struct consumer_output
*consumer
,
439 struct lttng_uri
*uri
, int domain
, const char *session_name
)
442 const char *default_trace_dir
;
446 if (consumer
== NULL
) {
447 DBG("No consumer detected. Don't add URI. Stopping.");
448 ret
= LTTNG_ERR_NO_CONSUMER
;
453 case LTTNG_DOMAIN_KERNEL
:
454 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
456 case LTTNG_DOMAIN_UST
:
457 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
461 * This case is possible is we try to add the URI to the global tracing
462 * session consumer object which in this case there is no subdir.
464 default_trace_dir
= "";
467 switch (uri
->dtype
) {
470 DBG2("Setting network URI to consumer");
472 if (consumer
->type
== CONSUMER_DST_NET
) {
473 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
474 consumer
->dst
.net
.control_isset
) ||
475 (uri
->stype
== LTTNG_STREAM_DATA
&&
476 consumer
->dst
.net
.data_isset
)) {
477 ret
= LTTNG_ERR_URL_EXIST
;
481 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
484 consumer
->type
= CONSUMER_DST_NET
;
486 /* Set URI into consumer output object */
487 ret
= consumer_set_network_uri(consumer
, uri
);
491 } else if (ret
== 1) {
493 * URI was the same in the consumer so we do not append the subdir
494 * again so to not duplicate output dir.
500 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
501 ret
= consumer_set_subdir(consumer
, session_name
);
503 ret
= LTTNG_ERR_FATAL
;
508 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
509 /* On a new subdir, reappend the default trace dir. */
510 strncat(consumer
->subdir
, default_trace_dir
,
511 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
512 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
517 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
518 memset(consumer
->dst
.trace_path
, 0,
519 sizeof(consumer
->dst
.trace_path
));
520 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
521 sizeof(consumer
->dst
.trace_path
));
522 /* Append default trace dir */
523 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
524 sizeof(consumer
->dst
.trace_path
) -
525 strlen(consumer
->dst
.trace_path
) - 1);
526 /* Flag consumer as local. */
527 consumer
->type
= CONSUMER_DST_LOCAL
;
538 * Init tracing by creating trace directory and sending fds kernel consumer.
540 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
543 struct lttng_ht_iter iter
;
544 struct consumer_socket
*socket
;
550 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
551 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
553 pthread_mutex_lock(socket
->lock
);
554 ret
= kernel_consumer_send_session(socket
, session
);
555 pthread_mutex_unlock(socket
->lock
);
557 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
569 * Create a socket to the relayd using the URI.
571 * On success, the relayd_sock pointer is set to the created socket.
572 * Else, it's stays untouched and a lttcomm error code is returned.
574 static int create_connect_relayd(struct lttng_uri
*uri
,
575 struct lttcomm_relayd_sock
**relayd_sock
)
578 struct lttcomm_relayd_sock
*rsock
;
580 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
581 RELAYD_VERSION_COMM_MINOR
);
583 ret
= LTTNG_ERR_FATAL
;
588 * Connect to relayd so we can proceed with a session creation. This call
589 * can possibly block for an arbitrary amount of time to set the health
590 * state to be in poll execution.
593 ret
= relayd_connect(rsock
);
596 ERR("Unable to reach lttng-relayd");
597 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
601 /* Create socket for control stream. */
602 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
603 DBG3("Creating relayd stream socket from URI");
605 /* Check relayd version */
606 ret
= relayd_version_check(rsock
);
608 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
611 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
612 DBG3("Creating relayd data socket from URI");
614 /* Command is not valid */
615 ERR("Relayd invalid stream type: %d", uri
->stype
);
616 ret
= LTTNG_ERR_INVALID
;
620 *relayd_sock
= rsock
;
625 /* The returned value is not useful since we are on an error path. */
626 (void) relayd_close(rsock
);
634 * Connect to the relayd using URI and send the socket to the right consumer.
636 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
637 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
638 struct consumer_socket
*consumer_sock
,
639 char *session_name
, char *hostname
, int session_live_timer
)
642 struct lttcomm_relayd_sock
*rsock
= NULL
;
644 /* Connect to relayd and make version check if uri is the control. */
645 ret
= create_connect_relayd(relayd_uri
, &rsock
);
646 if (ret
!= LTTNG_OK
) {
651 /* Set the network sequence index if not set. */
652 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
653 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
655 * Increment net_seq_idx because we are about to transfer the
656 * new relayd socket to the consumer.
657 * Assign unique key so the consumer can match streams.
659 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
660 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
663 /* Send relayd socket to consumer. */
664 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
665 relayd_uri
->stype
, session_id
,
666 session_name
, hostname
, session_live_timer
);
668 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
672 /* Flag that the corresponding socket was sent. */
673 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
674 consumer_sock
->control_sock_sent
= 1;
675 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
676 consumer_sock
->data_sock_sent
= 1;
682 * Close socket which was dup on the consumer side. The session daemon does
683 * NOT keep track of the relayd socket(s) once transfer to the consumer.
687 (void) relayd_close(rsock
);
691 if (ret
!= LTTNG_OK
) {
693 * The consumer output for this session should not be used anymore
694 * since the relayd connection failed thus making any tracing or/and
695 * streaming not usable.
697 consumer
->enabled
= 0;
703 * Send both relayd sockets to a specific consumer and domain. This is a
704 * helper function to facilitate sending the information to the consumer for a
707 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
708 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
709 char *session_name
, char *hostname
, int session_live_timer
)
716 /* Sending control relayd socket. */
717 if (!sock
->control_sock_sent
) {
718 ret
= send_consumer_relayd_socket(domain
, session_id
,
719 &consumer
->dst
.net
.control
, consumer
, sock
,
720 session_name
, hostname
, session_live_timer
);
721 if (ret
!= LTTNG_OK
) {
726 /* Sending data relayd socket. */
727 if (!sock
->data_sock_sent
) {
728 ret
= send_consumer_relayd_socket(domain
, session_id
,
729 &consumer
->dst
.net
.data
, consumer
, sock
,
730 session_name
, hostname
, session_live_timer
);
731 if (ret
!= LTTNG_OK
) {
741 * Setup relayd connections for a tracing session. First creates the socket to
742 * the relayd and send them to the right domain consumer. Consumer type MUST be
745 int cmd_setup_relayd(struct ltt_session
*session
)
748 struct ltt_ust_session
*usess
;
749 struct ltt_kernel_session
*ksess
;
750 struct consumer_socket
*socket
;
751 struct lttng_ht_iter iter
;
755 usess
= session
->ust_session
;
756 ksess
= session
->kernel_session
;
758 DBG("Setting relayd for session %s", session
->name
);
762 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
763 && usess
->consumer
->enabled
) {
764 /* For each consumer socket, send relayd sockets */
765 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
767 pthread_mutex_lock(socket
->lock
);
768 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
769 usess
->consumer
, socket
,
770 session
->name
, session
->hostname
,
771 session
->live_timer
);
772 pthread_mutex_unlock(socket
->lock
);
773 if (ret
!= LTTNG_OK
) {
776 /* Session is now ready for network streaming. */
777 session
->net_handle
= 1;
781 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
782 && ksess
->consumer
->enabled
) {
783 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
785 pthread_mutex_lock(socket
->lock
);
786 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
787 ksess
->consumer
, socket
,
788 session
->name
, session
->hostname
,
789 session
->live_timer
);
790 pthread_mutex_unlock(socket
->lock
);
791 if (ret
!= LTTNG_OK
) {
794 /* Session is now ready for network streaming. */
795 session
->net_handle
= 1;
805 * Start a kernel session by opening all necessary streams.
807 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
810 struct ltt_kernel_channel
*kchan
;
812 /* Open kernel metadata */
813 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
814 ret
= kernel_open_metadata(ksess
);
816 ret
= LTTNG_ERR_KERN_META_FAIL
;
821 /* Open kernel metadata stream */
822 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
823 ret
= kernel_open_metadata_stream(ksess
);
825 ERR("Kernel create metadata stream failed");
826 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
831 /* For each channel */
832 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
833 if (kchan
->stream_count
== 0) {
834 ret
= kernel_open_channel_stream(kchan
);
836 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
839 /* Update the stream global counter */
840 ksess
->stream_count_global
+= ret
;
844 /* Setup kernel consumer socket and send fds to it */
845 ret
= init_kernel_tracing(ksess
);
847 ret
= LTTNG_ERR_KERN_START_FAIL
;
851 /* This start the kernel tracing */
852 ret
= kernel_start_session(ksess
);
854 ret
= LTTNG_ERR_KERN_START_FAIL
;
858 /* Quiescent wait after starting trace */
859 kernel_wait_quiescent(kernel_tracer_fd
);
870 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
872 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
876 struct ltt_ust_session
*usess
;
878 usess
= session
->ust_session
;
883 case LTTNG_DOMAIN_KERNEL
:
885 ret
= channel_kernel_disable(session
->kernel_session
,
887 if (ret
!= LTTNG_OK
) {
891 kernel_wait_quiescent(kernel_tracer_fd
);
894 case LTTNG_DOMAIN_UST
:
896 struct ltt_ust_channel
*uchan
;
897 struct lttng_ht
*chan_ht
;
899 chan_ht
= usess
->domain_global
.channels
;
901 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
903 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
907 ret
= channel_ust_disable(usess
, uchan
);
908 if (ret
!= LTTNG_OK
) {
914 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
915 case LTTNG_DOMAIN_UST_EXEC_NAME
:
916 case LTTNG_DOMAIN_UST_PID
:
919 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
931 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
933 * The wpipe arguments is used as a notifier for the kernel thread.
935 int cmd_enable_channel(struct ltt_session
*session
,
936 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
939 struct ltt_ust_session
*usess
= session
->ust_session
;
940 struct lttng_ht
*chan_ht
;
946 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
951 * Don't try to enable a channel if the session has been started at
952 * some point in time before. The tracer does not allow it.
954 if (session
->has_been_started
) {
955 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
960 * If the session is a live session, remove the switch timer, the
961 * live timer does the same thing but sends also synchronisation
962 * beacons for inactive streams.
964 if (session
->live_timer
> 0) {
965 attr
->attr
.live_timer_interval
= session
->live_timer
;
966 attr
->attr
.switch_timer_interval
= 0;
970 * The ringbuffer (both in user space and kernel) behave badly in overwrite
971 * mode and with less than 2 subbuf so block it right away and send back an
972 * invalid attribute error.
974 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
975 ret
= LTTNG_ERR_INVALID
;
979 switch (domain
->type
) {
980 case LTTNG_DOMAIN_KERNEL
:
982 struct ltt_kernel_channel
*kchan
;
984 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
985 session
->kernel_session
);
987 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
988 if (attr
->name
[0] != '\0') {
989 session
->kernel_session
->has_non_default_channel
= 1;
992 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
995 if (ret
!= LTTNG_OK
) {
999 kernel_wait_quiescent(kernel_tracer_fd
);
1002 case LTTNG_DOMAIN_UST
:
1004 struct ltt_ust_channel
*uchan
;
1006 chan_ht
= usess
->domain_global
.channels
;
1008 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1009 if (uchan
== NULL
) {
1010 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1011 if (attr
->name
[0] != '\0') {
1012 usess
->has_non_default_channel
= 1;
1015 ret
= channel_ust_enable(usess
, uchan
);
1020 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1031 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1033 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1035 struct lttng_event
*event
)
1040 DBG("Disable event command for event \'%s\'", event
->name
);
1042 event_name
= event
->name
;
1044 /* Error out on unhandled search criteria */
1045 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1046 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1047 return LTTNG_ERR_UNK
;
1049 /* Special handling for kernel domain all events. */
1050 if (domain
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event_name
, "*")) {
1051 return disable_kevent_all(session
, domain
, channel_name
, event
);
1057 case LTTNG_DOMAIN_KERNEL
:
1059 struct ltt_kernel_channel
*kchan
;
1060 struct ltt_kernel_session
*ksess
;
1062 ksess
= session
->kernel_session
;
1065 * If a non-default channel has been created in the
1066 * session, explicitely require that -c chan_name needs
1069 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1070 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1074 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1075 if (kchan
== NULL
) {
1076 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1080 switch (event
->type
) {
1081 case LTTNG_EVENT_ALL
:
1082 case LTTNG_EVENT_TRACEPOINT
:
1083 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1084 if (ret
!= LTTNG_OK
) {
1088 case LTTNG_EVENT_SYSCALL
:
1089 ret
= event_kernel_disable_syscall(kchan
, event_name
);
1090 if (ret
!= LTTNG_OK
) {
1095 ret
= LTTNG_ERR_UNK
;
1099 kernel_wait_quiescent(kernel_tracer_fd
);
1102 case LTTNG_DOMAIN_UST
:
1104 struct ltt_ust_channel
*uchan
;
1105 struct ltt_ust_session
*usess
;
1107 usess
= session
->ust_session
;
1110 * If a non-default channel has been created in the
1111 * session, explicitely require that -c chan_name needs
1114 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1115 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1119 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1121 if (uchan
== NULL
) {
1122 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1126 switch (event
->type
) {
1127 case LTTNG_EVENT_ALL
:
1128 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1129 if (ret
!= LTTNG_OK
) {
1134 ret
= LTTNG_ERR_UNK
;
1138 DBG3("Disable UST event %s in channel %s completed", event_name
,
1142 case LTTNG_DOMAIN_LOG4J
:
1143 case LTTNG_DOMAIN_JUL
:
1146 struct ltt_ust_session
*usess
= session
->ust_session
;
1150 switch (event
->type
) {
1151 case LTTNG_EVENT_ALL
:
1154 ret
= LTTNG_ERR_UNK
;
1158 agt
= trace_ust_find_agent(usess
, domain
);
1160 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1163 /* The wild card * means that everything should be disabled. */
1164 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1165 ret
= event_agent_disable_all(usess
, agt
);
1167 ret
= event_agent_disable(usess
, agt
, event_name
);
1169 if (ret
!= LTTNG_OK
) {
1176 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1177 case LTTNG_DOMAIN_UST_PID
:
1178 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1181 ret
= LTTNG_ERR_UND
;
1193 * Command LTTNG_DISABLE_EVENT for event "*" processed by the client thread.
1196 int disable_kevent_all(struct ltt_session
*session
, int domain
,
1198 struct lttng_event
*event
)
1205 case LTTNG_DOMAIN_KERNEL
:
1207 struct ltt_kernel_session
*ksess
;
1208 struct ltt_kernel_channel
*kchan
;
1210 ksess
= session
->kernel_session
;
1213 * If a non-default channel has been created in the
1214 * session, explicitely require that -c chan_name needs
1217 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1218 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1222 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1223 if (kchan
== NULL
) {
1224 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1228 switch (event
->type
) {
1229 case LTTNG_EVENT_ALL
:
1230 ret
= event_kernel_disable_all(kchan
);
1231 if (ret
!= LTTNG_OK
) {
1235 case LTTNG_EVENT_SYSCALL
:
1236 ret
= event_kernel_disable_syscall(kchan
, "");
1237 if (ret
!= LTTNG_OK
) {
1242 ret
= LTTNG_ERR_UNK
;
1246 kernel_wait_quiescent(kernel_tracer_fd
);
1250 ret
= LTTNG_ERR_UND
;
1262 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1264 int cmd_add_context(struct ltt_session
*session
, int domain
,
1265 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1267 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1270 case LTTNG_DOMAIN_KERNEL
:
1271 assert(session
->kernel_session
);
1273 if (session
->kernel_session
->channel_count
== 0) {
1274 /* Create default channel */
1275 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1276 if (ret
!= LTTNG_OK
) {
1279 chan_kern_created
= 1;
1281 /* Add kernel context to kernel tracer */
1282 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1283 if (ret
!= LTTNG_OK
) {
1287 case LTTNG_DOMAIN_UST
:
1289 struct ltt_ust_session
*usess
= session
->ust_session
;
1290 unsigned int chan_count
;
1294 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1295 if (chan_count
== 0) {
1296 struct lttng_channel
*attr
;
1297 /* Create default channel */
1298 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1300 ret
= LTTNG_ERR_FATAL
;
1304 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1305 if (ret
!= LTTNG_OK
) {
1310 chan_ust_created
= 1;
1313 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1314 if (ret
!= LTTNG_OK
) {
1320 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1321 case LTTNG_DOMAIN_UST_PID
:
1322 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1325 ret
= LTTNG_ERR_UND
;
1332 if (chan_kern_created
) {
1333 struct ltt_kernel_channel
*kchan
=
1334 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1335 session
->kernel_session
);
1336 /* Created previously, this should NOT fail. */
1338 kernel_destroy_channel(kchan
);
1341 if (chan_ust_created
) {
1342 struct ltt_ust_channel
*uchan
=
1343 trace_ust_find_channel_by_name(
1344 session
->ust_session
->domain_global
.channels
,
1345 DEFAULT_CHANNEL_NAME
);
1346 /* Created previously, this should NOT fail. */
1348 /* Remove from the channel list of the session. */
1349 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1351 trace_ust_destroy_channel(uchan
);
1356 static int validate_event_name(const char *name
)
1359 const char *c
= name
;
1360 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1363 * Make sure that unescaped wildcards are only used as the last
1364 * character of the event name.
1366 while (c
< event_name_end
) {
1374 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1375 /* Wildcard is not the last character */
1376 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1389 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1390 * We own filter, exclusion, and filter_expression.
1392 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1393 char *channel_name
, struct lttng_event
*event
,
1394 char *filter_expression
,
1395 struct lttng_filter_bytecode
*filter
,
1396 struct lttng_event_exclusion
*exclusion
,
1399 int ret
, channel_created
= 0;
1400 struct lttng_channel
*attr
;
1404 assert(channel_name
);
1406 DBG("Enable event command for event \'%s\'", event
->name
);
1408 /* Special handling for kernel domain all events. */
1409 if (domain
->type
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event
->name
, "*")) {
1410 return enable_kevent_all(session
, domain
, channel_name
, event
,
1411 filter_expression
, filter
, wpipe
);
1414 ret
= validate_event_name(event
->name
);
1421 switch (domain
->type
) {
1422 case LTTNG_DOMAIN_KERNEL
:
1424 struct ltt_kernel_channel
*kchan
;
1427 * If a non-default channel has been created in the
1428 * session, explicitely require that -c chan_name needs
1431 if (session
->kernel_session
->has_non_default_channel
1432 && channel_name
[0] == '\0') {
1433 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1437 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1438 session
->kernel_session
);
1439 if (kchan
== NULL
) {
1440 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1441 LTTNG_BUFFER_GLOBAL
);
1443 ret
= LTTNG_ERR_FATAL
;
1446 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1448 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1449 if (ret
!= LTTNG_OK
) {
1455 channel_created
= 1;
1458 /* Get the newly created kernel channel pointer */
1459 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1460 session
->kernel_session
);
1461 if (kchan
== NULL
) {
1462 /* This sould not happen... */
1463 ret
= LTTNG_ERR_FATAL
;
1467 switch (event
->type
) {
1468 case LTTNG_EVENT_ALL
:
1469 case LTTNG_EVENT_PROBE
:
1470 case LTTNG_EVENT_FUNCTION
:
1471 case LTTNG_EVENT_FUNCTION_ENTRY
:
1472 case LTTNG_EVENT_TRACEPOINT
:
1473 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1474 if (ret
!= LTTNG_OK
) {
1475 if (channel_created
) {
1476 /* Let's not leak a useless channel. */
1477 kernel_destroy_channel(kchan
);
1482 case LTTNG_EVENT_SYSCALL
:
1483 ret
= event_kernel_enable_syscall(kchan
, event
->name
);
1484 if (ret
!= LTTNG_OK
) {
1489 ret
= LTTNG_ERR_UNK
;
1493 kernel_wait_quiescent(kernel_tracer_fd
);
1496 case LTTNG_DOMAIN_UST
:
1498 struct ltt_ust_channel
*uchan
;
1499 struct ltt_ust_session
*usess
= session
->ust_session
;
1504 * If a non-default channel has been created in the
1505 * session, explicitely require that -c chan_name needs
1508 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1509 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1513 /* Get channel from global UST domain */
1514 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1516 if (uchan
== NULL
) {
1517 /* Create default channel */
1518 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1519 usess
->buffer_type
);
1521 ret
= LTTNG_ERR_FATAL
;
1524 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1526 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1527 if (ret
!= LTTNG_OK
) {
1533 /* Get the newly created channel reference back */
1534 uchan
= trace_ust_find_channel_by_name(
1535 usess
->domain_global
.channels
, channel_name
);
1539 /* At this point, the session and channel exist on the tracer */
1540 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1541 filter_expression
, filter
, exclusion
);
1542 /* We have passed ownership */
1543 filter_expression
= NULL
;
1546 if (ret
!= LTTNG_OK
) {
1551 case LTTNG_DOMAIN_LOG4J
:
1552 case LTTNG_DOMAIN_JUL
:
1554 const char *default_event_name
, *default_chan_name
;
1556 struct lttng_event uevent
;
1557 struct lttng_domain tmp_dom
;
1558 struct ltt_ust_session
*usess
= session
->ust_session
;
1562 agt
= trace_ust_find_agent(usess
, domain
->type
);
1564 agt
= agent_create(domain
->type
);
1566 ret
= -LTTNG_ERR_NOMEM
;
1569 agent_add(agt
, usess
->agents
);
1572 /* Create the default tracepoint. */
1573 memset(&uevent
, 0, sizeof(uevent
));
1574 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1575 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1576 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1577 if (!default_event_name
) {
1578 ret
= -LTTNG_ERR_FATAL
;
1581 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1582 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1585 * The domain type is changed because we are about to enable the
1586 * default channel and event for the JUL domain that are hardcoded.
1587 * This happens in the UST domain.
1589 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1590 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1592 if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1593 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1595 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1599 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1602 filter_copy
= zmalloc(
1603 sizeof(struct lttng_filter_bytecode
)
1609 memcpy(filter_copy
, filter
,
1610 sizeof(struct lttng_filter_bytecode
)
1614 ret
= cmd_enable_event(session
, &tmp_dom
,
1615 (char *) default_chan_name
,
1616 &uevent
, filter_expression
, filter_copy
,
1618 /* We have passed ownership */
1619 filter_expression
= NULL
;
1622 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1626 /* The wild card * means that everything should be enabled. */
1627 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1628 ret
= event_agent_enable_all(usess
, agt
, event
, filter
);
1631 ret
= event_agent_enable(usess
, agt
, event
, filter
);
1634 if (ret
!= LTTNG_OK
) {
1641 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1642 case LTTNG_DOMAIN_UST_PID
:
1643 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1646 ret
= LTTNG_ERR_UND
;
1653 free(filter_expression
);
1661 * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread.
1664 int enable_kevent_all(struct ltt_session
*session
,
1665 struct lttng_domain
*domain
, char *channel_name
,
1666 struct lttng_event
*event
,
1667 char *filter_expression
,
1668 struct lttng_filter_bytecode
*filter
, int wpipe
)
1671 struct lttng_channel
*attr
;
1674 assert(channel_name
);
1678 switch (domain
->type
) {
1679 case LTTNG_DOMAIN_KERNEL
:
1681 struct ltt_kernel_channel
*kchan
;
1683 assert(session
->kernel_session
);
1686 * If a non-default channel has been created in the
1687 * session, explicitely require that -c chan_name needs
1690 if (session
->kernel_session
->has_non_default_channel
1691 && channel_name
[0] == '\0') {
1692 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1696 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1697 session
->kernel_session
);
1698 if (kchan
== NULL
) {
1699 /* Create default channel */
1700 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1701 LTTNG_BUFFER_GLOBAL
);
1703 ret
= LTTNG_ERR_FATAL
;
1706 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1708 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1709 if (ret
!= LTTNG_OK
) {
1715 /* Get the newly created kernel channel pointer */
1716 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1717 session
->kernel_session
);
1721 switch (event
->type
) {
1722 case LTTNG_EVENT_SYSCALL
:
1723 ret
= event_kernel_enable_syscall(kchan
, "");
1724 if (ret
!= LTTNG_OK
) {
1728 case LTTNG_EVENT_TRACEPOINT
:
1730 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1731 * events already registered to the channel.
1733 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1735 case LTTNG_EVENT_ALL
:
1736 /* Enable syscalls and tracepoints */
1737 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1740 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1744 /* Manage return value */
1745 if (ret
!= LTTNG_OK
) {
1747 * On error, cmd_enable_channel call will take care of destroying
1748 * the created channel if it was needed.
1753 kernel_wait_quiescent(kernel_tracer_fd
);
1757 ret
= LTTNG_ERR_UND
;
1770 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1772 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1775 ssize_t nb_events
= 0;
1778 case LTTNG_DOMAIN_KERNEL
:
1779 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1780 if (nb_events
< 0) {
1781 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1785 case LTTNG_DOMAIN_UST
:
1786 nb_events
= ust_app_list_events(events
);
1787 if (nb_events
< 0) {
1788 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1792 case LTTNG_DOMAIN_LOG4J
:
1793 case LTTNG_DOMAIN_JUL
:
1794 nb_events
= agent_list_events(events
, domain
);
1795 if (nb_events
< 0) {
1796 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1801 ret
= LTTNG_ERR_UND
;
1808 /* Return negative value to differentiate return code */
1813 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1815 ssize_t
cmd_list_tracepoint_fields(int domain
,
1816 struct lttng_event_field
**fields
)
1819 ssize_t nb_fields
= 0;
1822 case LTTNG_DOMAIN_UST
:
1823 nb_fields
= ust_app_list_event_fields(fields
);
1824 if (nb_fields
< 0) {
1825 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1829 case LTTNG_DOMAIN_KERNEL
:
1830 default: /* fall-through */
1831 ret
= LTTNG_ERR_UND
;
1838 /* Return negative value to differentiate return code */
1842 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1844 return syscall_table_list(events
);
1848 * Command LTTNG_START_TRACE processed by the client thread.
1850 int cmd_start_trace(struct ltt_session
*session
)
1853 unsigned long nb_chan
= 0;
1854 struct ltt_kernel_session
*ksession
;
1855 struct ltt_ust_session
*usess
;
1859 /* Ease our life a bit ;) */
1860 ksession
= session
->kernel_session
;
1861 usess
= session
->ust_session
;
1863 /* Is the session already started? */
1864 if (session
->active
) {
1865 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1870 * Starting a session without channel is useless since after that it's not
1871 * possible to enable channel thus inform the client.
1873 if (usess
&& usess
->domain_global
.channels
) {
1875 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1879 nb_chan
+= ksession
->channel_count
;
1882 ret
= LTTNG_ERR_NO_CHANNEL
;
1886 /* Kernel tracing */
1887 if (ksession
!= NULL
) {
1888 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1889 if (ret
!= LTTNG_OK
) {
1894 /* Flag session that trace should start automatically */
1897 * Even though the start trace might fail, flag this session active so
1898 * other application coming in are started by default.
1902 ret
= ust_app_start_trace_all(usess
);
1904 ret
= LTTNG_ERR_UST_START_FAIL
;
1909 /* Flag this after a successful start. */
1910 session
->has_been_started
= 1;
1911 session
->active
= 1;
1920 * Command LTTNG_STOP_TRACE processed by the client thread.
1922 int cmd_stop_trace(struct ltt_session
*session
)
1925 struct ltt_kernel_channel
*kchan
;
1926 struct ltt_kernel_session
*ksession
;
1927 struct ltt_ust_session
*usess
;
1932 ksession
= session
->kernel_session
;
1933 usess
= session
->ust_session
;
1935 /* Session is not active. Skip everythong and inform the client. */
1936 if (!session
->active
) {
1937 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1942 if (ksession
&& ksession
->active
) {
1943 DBG("Stop kernel tracing");
1945 /* Flush metadata if exist */
1946 if (ksession
->metadata_stream_fd
>= 0) {
1947 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1949 ERR("Kernel metadata flush failed");
1953 /* Flush all buffers before stopping */
1954 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1955 ret
= kernel_flush_buffer(kchan
);
1957 ERR("Kernel flush buffer error");
1961 ret
= kernel_stop_session(ksession
);
1963 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1967 kernel_wait_quiescent(kernel_tracer_fd
);
1969 ksession
->active
= 0;
1972 if (usess
&& usess
->active
) {
1974 * Even though the stop trace might fail, flag this session inactive so
1975 * other application coming in are not started by default.
1979 ret
= ust_app_stop_trace_all(usess
);
1981 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1986 /* Flag inactive after a successful stop. */
1987 session
->active
= 0;
1995 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1997 int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
1998 size_t nb_uri
, struct lttng_uri
*uris
)
2001 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2002 struct ltt_ust_session
*usess
= session
->ust_session
;
2003 struct consumer_output
*consumer
= NULL
;
2009 /* Can't set consumer URI if the session is active. */
2010 if (session
->active
) {
2011 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2016 * This case switch makes sure the domain session has a temporary consumer
2017 * so the URL can be set.
2021 /* Code flow error. A session MUST always have a consumer object */
2022 assert(session
->consumer
);
2024 * The URL will be added to the tracing session consumer instead of a
2025 * specific domain consumer.
2027 consumer
= session
->consumer
;
2029 case LTTNG_DOMAIN_KERNEL
:
2030 /* Code flow error if we don't have a kernel session here. */
2032 assert(ksess
->consumer
);
2033 consumer
= ksess
->consumer
;
2035 case LTTNG_DOMAIN_UST
:
2036 /* Code flow error if we don't have a kernel session here. */
2038 assert(usess
->consumer
);
2039 consumer
= usess
->consumer
;
2043 for (i
= 0; i
< nb_uri
; i
++) {
2044 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
2045 if (ret
!= LTTNG_OK
) {
2051 * Make sure to set the session in output mode after we set URI since a
2052 * session can be created without URL (thus flagged in no output mode).
2054 session
->output_traces
= 1;
2056 ksess
->output_traces
= 1;
2058 usess
->output_traces
= 1;
2069 * Command LTTNG_CREATE_SESSION processed by the client thread.
2071 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2072 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2075 struct ltt_session
*session
;
2081 * Verify if the session already exist
2083 * XXX: There is no need for the session lock list here since the caller
2084 * (process_client_msg) is holding it. We might want to change that so a
2085 * single command does not lock the entire session list.
2087 session
= session_find_by_name(name
);
2088 if (session
!= NULL
) {
2089 ret
= LTTNG_ERR_EXIST_SESS
;
2093 /* Create tracing session in the registry */
2094 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2095 LTTNG_SOCK_GET_GID_CRED(creds
));
2096 if (ret
!= LTTNG_OK
) {
2101 * Get the newly created session pointer back
2103 * XXX: There is no need for the session lock list here since the caller
2104 * (process_client_msg) is holding it. We might want to change that so a
2105 * single command does not lock the entire session list.
2107 session
= session_find_by_name(name
);
2110 session
->live_timer
= live_timer
;
2111 /* Create default consumer output for the session not yet created. */
2112 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2113 if (session
->consumer
== NULL
) {
2114 ret
= LTTNG_ERR_FATAL
;
2115 goto consumer_error
;
2119 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
2120 if (ret
!= LTTNG_OK
) {
2121 goto consumer_error
;
2123 session
->output_traces
= 1;
2125 session
->output_traces
= 0;
2126 DBG2("Session %s created with no output", session
->name
);
2129 session
->consumer
->enabled
= 1;
2134 session_destroy(session
);
2141 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2143 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2144 size_t nb_uri
, lttng_sock_cred
*creds
)
2147 struct ltt_session
*session
;
2148 struct snapshot_output
*new_output
= NULL
;
2154 * Create session in no output mode with URIs set to NULL. The uris we've
2155 * received are for a default snapshot output if one.
2157 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2158 if (ret
!= LTTNG_OK
) {
2162 /* Get the newly created session pointer back. This should NEVER fail. */
2163 session
= session_find_by_name(name
);
2166 /* Flag session for snapshot mode. */
2167 session
->snapshot_mode
= 1;
2169 /* Skip snapshot output creation if no URI is given. */
2174 new_output
= snapshot_output_alloc();
2176 ret
= LTTNG_ERR_NOMEM
;
2177 goto error_snapshot_alloc
;
2180 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2181 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2183 if (ret
== -ENOMEM
) {
2184 ret
= LTTNG_ERR_NOMEM
;
2186 ret
= LTTNG_ERR_INVALID
;
2188 goto error_snapshot
;
2192 snapshot_add_output(&session
->snapshot
, new_output
);
2199 snapshot_output_destroy(new_output
);
2200 error_snapshot_alloc
:
2201 session_destroy(session
);
2207 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2209 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2212 struct ltt_ust_session
*usess
;
2213 struct ltt_kernel_session
*ksess
;
2218 usess
= session
->ust_session
;
2219 ksess
= session
->kernel_session
;
2221 /* Clean kernel session teardown */
2222 kernel_destroy_session(ksess
);
2224 /* UST session teardown */
2226 /* Close any relayd session */
2227 consumer_output_send_destroy_relayd(usess
->consumer
);
2229 /* Destroy every UST application related to this session. */
2230 ret
= ust_app_destroy_trace_all(usess
);
2232 ERR("Error in ust_app_destroy_trace_all");
2235 /* Clean up the rest. */
2236 trace_ust_destroy_session(usess
);
2240 * Must notify the kernel thread here to update it's poll set in order to
2241 * remove the channel(s)' fd just destroyed.
2243 ret
= notify_thread_pipe(wpipe
);
2245 PERROR("write kernel poll pipe");
2248 ret
= session_destroy(session
);
2254 * Command LTTNG_CALIBRATE processed by the client thread.
2256 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2261 case LTTNG_DOMAIN_KERNEL
:
2263 struct lttng_kernel_calibrate kcalibrate
;
2265 switch (calibrate
->type
) {
2266 case LTTNG_CALIBRATE_FUNCTION
:
2268 /* Default and only possible calibrate option. */
2269 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2273 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2275 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2280 case LTTNG_DOMAIN_UST
:
2282 struct lttng_ust_calibrate ucalibrate
;
2284 switch (calibrate
->type
) {
2285 case LTTNG_CALIBRATE_FUNCTION
:
2287 /* Default and only possible calibrate option. */
2288 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2292 ret
= ust_app_calibrate_glb(&ucalibrate
);
2294 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2300 ret
= LTTNG_ERR_UND
;
2311 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2313 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2314 const char *sock_path
, struct consumer_data
*cdata
)
2317 struct consumer_socket
*socket
= NULL
;
2324 case LTTNG_DOMAIN_KERNEL
:
2326 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2330 /* Can't register a consumer if there is already one */
2331 if (ksess
->consumer_fds_sent
!= 0) {
2332 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2336 sock
= lttcomm_connect_unix_sock(sock_path
);
2338 ret
= LTTNG_ERR_CONNECT_FAIL
;
2341 cdata
->cmd_sock
= sock
;
2343 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2344 if (socket
== NULL
) {
2347 PERROR("close register consumer");
2349 cdata
->cmd_sock
= -1;
2350 ret
= LTTNG_ERR_FATAL
;
2354 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2355 if (socket
->lock
== NULL
) {
2356 PERROR("zmalloc pthread mutex");
2357 ret
= LTTNG_ERR_FATAL
;
2360 pthread_mutex_init(socket
->lock
, NULL
);
2361 socket
->registered
= 1;
2364 consumer_add_socket(socket
, ksess
->consumer
);
2367 pthread_mutex_lock(&cdata
->pid_mutex
);
2369 pthread_mutex_unlock(&cdata
->pid_mutex
);
2374 /* TODO: Userspace tracing */
2375 ret
= LTTNG_ERR_UND
;
2383 consumer_destroy_socket(socket
);
2389 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2391 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2392 struct lttng_domain
**domains
)
2397 struct lttng_ht_iter iter
;
2399 if (session
->kernel_session
!= NULL
) {
2400 DBG3("Listing domains found kernel domain");
2404 if (session
->ust_session
!= NULL
) {
2405 DBG3("Listing domains found UST global domain");
2409 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2411 if (agt
->being_used
) {
2418 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2419 if (*domains
== NULL
) {
2420 ret
= LTTNG_ERR_FATAL
;
2424 if (session
->kernel_session
!= NULL
) {
2425 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2429 if (session
->ust_session
!= NULL
) {
2430 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2431 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2435 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2437 if (agt
->being_used
) {
2438 (*domains
)[index
].type
= agt
->domain
;
2439 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2449 /* Return negative value to differentiate return code */
2455 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2457 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2458 struct lttng_channel
**channels
)
2461 ssize_t nb_chan
= 0;
2464 case LTTNG_DOMAIN_KERNEL
:
2465 if (session
->kernel_session
!= NULL
) {
2466 nb_chan
= session
->kernel_session
->channel_count
;
2468 DBG3("Number of kernel channels %zd", nb_chan
);
2470 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2473 case LTTNG_DOMAIN_UST
:
2474 if (session
->ust_session
!= NULL
) {
2476 nb_chan
= lttng_ht_get_count(
2477 session
->ust_session
->domain_global
.channels
);
2480 DBG3("Number of UST global channels %zd", nb_chan
);
2482 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2487 ret
= LTTNG_ERR_UND
;
2492 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2493 if (*channels
== NULL
) {
2494 ret
= LTTNG_ERR_FATAL
;
2498 list_lttng_channels(domain
, session
, *channels
);
2504 /* Return negative value to differentiate return code */
2509 * Command LTTNG_LIST_EVENTS processed by the client thread.
2511 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2512 char *channel_name
, struct lttng_event
**events
)
2515 ssize_t nb_event
= 0;
2518 case LTTNG_DOMAIN_KERNEL
:
2519 if (session
->kernel_session
!= NULL
) {
2520 nb_event
= list_lttng_kernel_events(channel_name
,
2521 session
->kernel_session
, events
);
2524 case LTTNG_DOMAIN_UST
:
2526 if (session
->ust_session
!= NULL
) {
2527 nb_event
= list_lttng_ust_global_events(channel_name
,
2528 &session
->ust_session
->domain_global
, events
);
2532 case LTTNG_DOMAIN_LOG4J
:
2533 case LTTNG_DOMAIN_JUL
:
2534 if (session
->ust_session
) {
2535 struct lttng_ht_iter iter
;
2539 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2540 &iter
.iter
, agt
, node
.node
) {
2541 nb_event
= list_lttng_agent_events(agt
, events
);
2547 ret
= LTTNG_ERR_UND
;
2554 /* Return negative value to differentiate return code */
2559 * Using the session list, filled a lttng_session array to send back to the
2560 * client for session listing.
2562 * The session list lock MUST be acquired before calling this function. Use
2563 * session_lock_list() and session_unlock_list().
2565 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2570 struct ltt_session
*session
;
2571 struct ltt_session_list
*list
= session_get_list();
2573 DBG("Getting all available session for UID %d GID %d",
2576 * Iterate over session list and append data after the control struct in
2579 cds_list_for_each_entry(session
, &list
->head
, list
) {
2581 * Only list the sessions the user can control.
2583 if (!session_access_ok(session
, uid
, gid
)) {
2587 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2588 struct ltt_ust_session
*usess
= session
->ust_session
;
2590 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2591 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2592 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2593 ret
= build_network_session_path(sessions
[i
].path
,
2594 sizeof(sessions
[i
].path
), session
);
2596 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2597 session
->consumer
->dst
.trace_path
);
2600 PERROR("snprintf session path");
2604 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2605 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2606 sessions
[i
].enabled
= session
->active
;
2607 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2608 sessions
[i
].live_timer_interval
= session
->live_timer
;
2614 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2615 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2617 int cmd_data_pending(struct ltt_session
*session
)
2620 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2621 struct ltt_ust_session
*usess
= session
->ust_session
;
2625 /* Session MUST be stopped to ask for data availability. */
2626 if (session
->active
) {
2627 ret
= LTTNG_ERR_SESSION_STARTED
;
2631 * If stopped, just make sure we've started before else the above call
2632 * will always send that there is data pending.
2634 * The consumer assumes that when the data pending command is received,
2635 * the trace has been started before or else no output data is written
2636 * by the streams which is a condition for data pending. So, this is
2637 * *VERY* important that we don't ask the consumer before a start
2640 if (!session
->has_been_started
) {
2646 if (ksess
&& ksess
->consumer
) {
2647 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2649 /* Data is still being extracted for the kernel. */
2654 if (usess
&& usess
->consumer
) {
2655 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2657 /* Data is still being extracted for the kernel. */
2662 /* Data is ready to be read by a viewer */
2670 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2672 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2674 int cmd_snapshot_add_output(struct ltt_session
*session
,
2675 struct lttng_snapshot_output
*output
, uint32_t *id
)
2678 struct snapshot_output
*new_output
;
2683 DBG("Cmd snapshot add output for session %s", session
->name
);
2686 * Permission denied to create an output if the session is not
2687 * set in no output mode.
2689 if (session
->output_traces
) {
2690 ret
= LTTNG_ERR_EPERM
;
2694 /* Only one output is allowed until we have the "tee" feature. */
2695 if (session
->snapshot
.nb_output
== 1) {
2696 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2700 new_output
= snapshot_output_alloc();
2702 ret
= LTTNG_ERR_NOMEM
;
2706 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2707 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2708 &session
->snapshot
);
2710 if (ret
== -ENOMEM
) {
2711 ret
= LTTNG_ERR_NOMEM
;
2713 ret
= LTTNG_ERR_INVALID
;
2719 snapshot_add_output(&session
->snapshot
, new_output
);
2721 *id
= new_output
->id
;
2728 snapshot_output_destroy(new_output
);
2734 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2736 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2738 int cmd_snapshot_del_output(struct ltt_session
*session
,
2739 struct lttng_snapshot_output
*output
)
2742 struct snapshot_output
*sout
= NULL
;
2750 * Permission denied to create an output if the session is not
2751 * set in no output mode.
2753 if (session
->output_traces
) {
2754 ret
= LTTNG_ERR_EPERM
;
2759 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2761 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2762 } else if (*output
->name
!= '\0') {
2763 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2765 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2768 ret
= LTTNG_ERR_INVALID
;
2772 snapshot_delete_output(&session
->snapshot
, sout
);
2773 snapshot_output_destroy(sout
);
2782 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2784 * If no output is available, outputs is untouched and 0 is returned.
2786 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2788 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2789 struct lttng_snapshot_output
**outputs
)
2792 struct lttng_snapshot_output
*list
= NULL
;
2793 struct lttng_ht_iter iter
;
2794 struct snapshot_output
*output
;
2799 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2802 * Permission denied to create an output if the session is not
2803 * set in no output mode.
2805 if (session
->output_traces
) {
2806 ret
= -LTTNG_ERR_EPERM
;
2810 if (session
->snapshot
.nb_output
== 0) {
2815 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2817 ret
= -LTTNG_ERR_NOMEM
;
2821 /* Copy list from session to the new list object. */
2823 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2824 output
, node
.node
) {
2825 assert(output
->consumer
);
2826 list
[idx
].id
= output
->id
;
2827 list
[idx
].max_size
= output
->max_size
;
2828 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2829 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2830 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2831 sizeof(list
[idx
].ctrl_url
));
2834 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2835 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2837 ret
= -LTTNG_ERR_NOMEM
;
2842 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2843 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2845 ret
= -LTTNG_ERR_NOMEM
;
2854 ret
= session
->snapshot
.nb_output
;
2862 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2863 * snapshot output is *not* set with a remote destination.
2865 * Return 0 on success or a LTTNG_ERR code.
2867 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2868 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2871 struct lttng_ht_iter iter
;
2872 struct consumer_socket
*socket
;
2875 assert(snap_output
);
2878 DBG2("Set relayd object from snapshot output");
2880 /* Ignore if snapshot consumer output is not network. */
2881 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2886 * For each consumer socket, create and send the relayd object of the
2890 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2891 socket
, node
.node
) {
2892 ret
= send_consumer_relayd_sockets(0, session
->id
,
2893 snap_output
->consumer
, socket
,
2894 session
->name
, session
->hostname
,
2895 session
->live_timer
);
2896 if (ret
!= LTTNG_OK
) {
2908 * Record a kernel snapshot.
2910 * Return LTTNG_OK on success or a LTTNG_ERR code.
2912 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2913 struct snapshot_output
*output
, struct ltt_session
*session
,
2914 int wait
, uint64_t max_stream_size
)
2922 /* Get the datetime for the snapshot output directory. */
2923 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2924 sizeof(output
->datetime
));
2926 ret
= LTTNG_ERR_INVALID
;
2931 * Copy kernel session sockets so we can communicate with the right
2932 * consumer for the snapshot record command.
2934 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2936 ret
= LTTNG_ERR_NOMEM
;
2940 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2941 if (ret
!= LTTNG_OK
) {
2942 goto error_snapshot
;
2945 ret
= kernel_snapshot_record(ksess
, output
, wait
, max_stream_size
);
2946 if (ret
!= LTTNG_OK
) {
2947 goto error_snapshot
;
2953 /* Clean up copied sockets so this output can use some other later on. */
2954 consumer_destroy_output_sockets(output
->consumer
);
2960 * Record a UST snapshot.
2962 * Return 0 on success or a LTTNG_ERR error code.
2964 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2965 struct snapshot_output
*output
, struct ltt_session
*session
,
2966 int wait
, uint64_t max_stream_size
)
2974 /* Get the datetime for the snapshot output directory. */
2975 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2976 sizeof(output
->datetime
));
2978 ret
= LTTNG_ERR_INVALID
;
2983 * Copy UST session sockets so we can communicate with the right
2984 * consumer for the snapshot record command.
2986 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
2988 ret
= LTTNG_ERR_NOMEM
;
2992 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
2993 if (ret
!= LTTNG_OK
) {
2994 goto error_snapshot
;
2997 ret
= ust_app_snapshot_record(usess
, output
, wait
, max_stream_size
);
3001 ret
= LTTNG_ERR_INVALID
;
3004 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3007 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3010 goto error_snapshot
;
3016 /* Clean up copied sockets so this output can use some other later on. */
3017 consumer_destroy_output_sockets(output
->consumer
);
3023 * Return the biggest subbuffer size of all channels in the given session.
3025 static uint64_t get_session_max_subbuf_size(struct ltt_session
*session
)
3027 uint64_t max_size
= 0;
3031 if (session
->kernel_session
) {
3032 struct ltt_kernel_channel
*chan
;
3033 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3036 * For each channel, add to the max size the size of each subbuffer
3037 * multiplied by their sized.
3039 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3040 if (chan
->channel
->attr
.subbuf_size
> max_size
) {
3041 max_size
= chan
->channel
->attr
.subbuf_size
;
3046 if (session
->ust_session
) {
3047 struct lttng_ht_iter iter
;
3048 struct ltt_ust_channel
*uchan
;
3049 struct ltt_ust_session
*usess
= session
->ust_session
;
3052 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
3054 if (uchan
->attr
.subbuf_size
> max_size
) {
3055 max_size
= uchan
->attr
.subbuf_size
;
3065 * Returns the total number of streams for a session or a negative value
3068 static unsigned int get_session_nb_streams(struct ltt_session
*session
)
3070 unsigned int total_streams
= 0;
3072 if (session
->kernel_session
) {
3073 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3075 total_streams
+= ksess
->stream_count_global
;
3078 if (session
->ust_session
) {
3079 struct ltt_ust_session
*usess
= session
->ust_session
;
3081 total_streams
+= ust_app_get_nb_stream(usess
);
3084 return total_streams
;
3088 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3090 * The wait parameter is ignored so this call always wait for the snapshot to
3091 * complete before returning.
3093 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3095 int cmd_snapshot_record(struct ltt_session
*session
,
3096 struct lttng_snapshot_output
*output
, int wait
)
3099 unsigned int use_tmp_output
= 0;
3100 struct snapshot_output tmp_output
;
3101 unsigned int nb_streams
, snapshot_success
= 0;
3102 uint64_t session_max_size
= 0, max_stream_size
= 0;
3107 DBG("Cmd snapshot record for session %s", session
->name
);
3110 * Permission denied to create an output if the session is not
3111 * set in no output mode.
3113 if (session
->output_traces
) {
3114 ret
= LTTNG_ERR_EPERM
;
3118 /* The session needs to be started at least once. */
3119 if (!session
->has_been_started
) {
3120 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3124 /* Use temporary output for the session. */
3125 if (*output
->ctrl_url
!= '\0') {
3126 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3127 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3130 if (ret
== -ENOMEM
) {
3131 ret
= LTTNG_ERR_NOMEM
;
3133 ret
= LTTNG_ERR_INVALID
;
3137 /* Use the global session count for the temporary snapshot. */
3138 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3143 * Get the session maximum size for a snapshot meaning it will compute the
3144 * size of all streams from all domain.
3146 max_stream_size
= get_session_max_subbuf_size(session
);
3148 nb_streams
= get_session_nb_streams(session
);
3151 * The maximum size of the snapshot is the number of streams multiplied
3152 * by the biggest subbuf size of all channels in a session which is the
3153 * maximum stream size available for each stream. The session max size
3154 * is now checked against the snapshot max size value given by the user
3155 * and if lower, an error is returned.
3157 session_max_size
= max_stream_size
* nb_streams
;
3160 DBG3("Snapshot max size is %" PRIu64
" for max stream size of %" PRIu64
,
3161 session_max_size
, max_stream_size
);
3164 * If we use a temporary output, check right away if the max size fits else
3165 * for each output the max size will be checked.
3167 if (use_tmp_output
&&
3168 (tmp_output
.max_size
!= 0 &&
3169 tmp_output
.max_size
< session_max_size
)) {
3170 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3174 if (session
->kernel_session
) {
3175 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3177 if (use_tmp_output
) {
3178 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3179 wait
, max_stream_size
);
3180 if (ret
!= LTTNG_OK
) {
3183 snapshot_success
= 1;
3185 struct snapshot_output
*sout
;
3186 struct lttng_ht_iter iter
;
3189 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3190 &iter
.iter
, sout
, node
.node
) {
3192 * Make a local copy of the output and assign the possible
3193 * temporary value given by the caller.
3195 memset(&tmp_output
, 0, sizeof(tmp_output
));
3196 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3198 /* Use temporary max size. */
3199 if (output
->max_size
!= (uint64_t) -1ULL) {
3200 tmp_output
.max_size
= output
->max_size
;
3203 if (tmp_output
.max_size
!= 0 &&
3204 tmp_output
.max_size
< session_max_size
) {
3206 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3210 /* Use temporary name. */
3211 if (*output
->name
!= '\0') {
3212 strncpy(tmp_output
.name
, output
->name
,
3213 sizeof(tmp_output
.name
));
3216 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3218 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3219 session
, wait
, max_stream_size
);
3220 if (ret
!= LTTNG_OK
) {
3224 snapshot_success
= 1;
3230 if (session
->ust_session
) {
3231 struct ltt_ust_session
*usess
= session
->ust_session
;
3233 if (use_tmp_output
) {
3234 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3235 wait
, max_stream_size
);
3236 if (ret
!= LTTNG_OK
) {
3239 snapshot_success
= 1;
3241 struct snapshot_output
*sout
;
3242 struct lttng_ht_iter iter
;
3245 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3246 &iter
.iter
, sout
, node
.node
) {
3248 * Make a local copy of the output and assign the possible
3249 * temporary value given by the caller.
3251 memset(&tmp_output
, 0, sizeof(tmp_output
));
3252 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3254 /* Use temporary max size. */
3255 if (output
->max_size
!= (uint64_t) -1ULL) {
3256 tmp_output
.max_size
= output
->max_size
;
3259 if (tmp_output
.max_size
!= 0 &&
3260 tmp_output
.max_size
< session_max_size
) {
3262 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3266 /* Use temporary name. */
3267 if (*output
->name
!= '\0') {
3268 strncpy(tmp_output
.name
, output
->name
,
3269 sizeof(tmp_output
.name
));
3272 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3274 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3275 wait
, max_stream_size
);
3276 if (ret
!= LTTNG_OK
) {
3280 snapshot_success
= 1;
3286 if (snapshot_success
) {
3287 session
->snapshot
.nb_snapshot
++;
3289 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3297 * Init command subsystem.
3302 * Set network sequence index to 1 for streams to match a relayd
3303 * socket on the consumer side.
3305 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3306 relayd_net_seq_idx
= 1;
3307 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3309 DBG("Command subsystem initialized");