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"
42 * Used to keep a unique index for each relayd socket created where this value
43 * is associated with streams on the consumer so it can match the right relayd
44 * to send to. It must be accessed with the relayd_net_seq_idx_lock
47 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
48 static uint64_t relayd_net_seq_idx
;
51 * Create a session path used by list_lttng_sessions for the case that the
52 * session consumer is on the network.
54 static int build_network_session_path(char *dst
, size_t size
,
55 struct ltt_session
*session
)
57 int ret
, kdata_port
, udata_port
;
58 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
59 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
64 memset(tmp_urls
, 0, sizeof(tmp_urls
));
65 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
67 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
69 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
70 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
71 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
74 if (session
->ust_session
&& session
->ust_session
->consumer
) {
75 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
76 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
79 if (uuri
== NULL
&& kuri
== NULL
) {
80 uri
= &session
->consumer
->dst
.net
.control
;
81 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
82 } else if (kuri
&& uuri
) {
83 ret
= uri_compare(kuri
, uuri
);
87 /* Build uuri URL string */
88 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
95 } else if (kuri
&& uuri
== NULL
) {
97 } else if (uuri
&& kuri
== NULL
) {
101 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
107 * Do we have a UST url set. If yes, this means we have both kernel and UST
110 if (*tmp_uurl
!= '\0') {
111 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
112 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
115 if (kuri
|| (!kuri
&& !uuri
)) {
118 /* No kernel URI, use the UST port. */
121 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
129 * Fill lttng_channel array of all channels.
131 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
132 struct lttng_channel
*channels
)
135 struct ltt_kernel_channel
*kchan
;
137 DBG("Listing channels for session %s", session
->name
);
140 case LTTNG_DOMAIN_KERNEL
:
141 /* Kernel channels */
142 if (session
->kernel_session
!= NULL
) {
143 cds_list_for_each_entry(kchan
,
144 &session
->kernel_session
->channel_list
.head
, list
) {
145 /* Copy lttng_channel struct to array */
146 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
147 channels
[i
].enabled
= kchan
->enabled
;
152 case LTTNG_DOMAIN_UST
:
154 struct lttng_ht_iter iter
;
155 struct ltt_ust_channel
*uchan
;
158 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
159 &iter
.iter
, uchan
, node
.node
) {
160 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
161 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
162 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
163 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
164 channels
[i
].attr
.switch_timer_interval
=
165 uchan
->attr
.switch_timer_interval
;
166 channels
[i
].attr
.read_timer_interval
=
167 uchan
->attr
.read_timer_interval
;
168 channels
[i
].enabled
= uchan
->enabled
;
169 switch (uchan
->attr
.output
) {
172 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
186 * Create a list of JUL domain events.
188 * Return number of events in list on success or else a negative value.
190 static int list_lttng_jul_events(struct jul_domain
*dom
,
191 struct lttng_event
**events
)
194 unsigned int nb_event
= 0;
195 struct jul_event
*event
;
196 struct lttng_event
*tmp_events
;
197 struct lttng_ht_iter iter
;
202 DBG3("Listing JUL events");
204 nb_event
= lttng_ht_get_count(dom
->events
);
210 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
212 PERROR("zmalloc JUL events session");
213 ret
= -LTTNG_ERR_FATAL
;
218 cds_lfht_for_each_entry(dom
->events
->ht
, &iter
.iter
, event
, node
.node
) {
219 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
220 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
221 tmp_events
[i
].enabled
= event
->enabled
;
226 *events
= tmp_events
;
230 assert(nb_event
== i
);
235 * Create a list of ust global domain events.
237 static int list_lttng_ust_global_events(char *channel_name
,
238 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
241 unsigned int nb_event
= 0;
242 struct lttng_ht_iter iter
;
243 struct lttng_ht_node_str
*node
;
244 struct ltt_ust_channel
*uchan
;
245 struct ltt_ust_event
*uevent
;
246 struct lttng_event
*tmp
;
248 DBG("Listing UST global events for channel %s", channel_name
);
252 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
253 node
= lttng_ht_iter_get_node_str(&iter
);
255 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
259 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
261 nb_event
+= lttng_ht_get_count(uchan
->events
);
268 DBG3("Listing UST global %d events", nb_event
);
270 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
272 ret
= LTTNG_ERR_FATAL
;
276 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
277 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
278 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
279 tmp
[i
].enabled
= uevent
->enabled
;
281 switch (uevent
->attr
.instrumentation
) {
282 case LTTNG_UST_TRACEPOINT
:
283 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
285 case LTTNG_UST_PROBE
:
286 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
288 case LTTNG_UST_FUNCTION
:
289 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
293 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
294 switch (uevent
->attr
.loglevel_type
) {
295 case LTTNG_UST_LOGLEVEL_ALL
:
296 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
298 case LTTNG_UST_LOGLEVEL_RANGE
:
299 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
301 case LTTNG_UST_LOGLEVEL_SINGLE
:
302 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
305 if (uevent
->filter
) {
320 * Fill lttng_event array of all kernel events in the channel.
322 static int list_lttng_kernel_events(char *channel_name
,
323 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
326 unsigned int nb_event
;
327 struct ltt_kernel_event
*event
;
328 struct ltt_kernel_channel
*kchan
;
330 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
332 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
336 nb_event
= kchan
->event_count
;
338 DBG("Listing events for channel %s", kchan
->channel
->name
);
344 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
345 if (*events
== NULL
) {
346 ret
= LTTNG_ERR_FATAL
;
350 /* Kernel channels */
351 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
352 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
353 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
354 (*events
)[i
].enabled
= event
->enabled
;
356 switch (event
->event
->instrumentation
) {
357 case LTTNG_KERNEL_TRACEPOINT
:
358 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
360 case LTTNG_KERNEL_KRETPROBE
:
361 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
362 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
363 sizeof(struct lttng_kernel_kprobe
));
365 case LTTNG_KERNEL_KPROBE
:
366 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
367 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
368 sizeof(struct lttng_kernel_kprobe
));
370 case LTTNG_KERNEL_FUNCTION
:
371 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
372 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
373 sizeof(struct lttng_kernel_function
));
375 case LTTNG_KERNEL_NOOP
:
376 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
378 case LTTNG_KERNEL_SYSCALL
:
379 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
381 case LTTNG_KERNEL_ALL
:
392 /* Negate the error code to differentiate the size from an error */
397 * Add URI so the consumer output object. Set the correct path depending on the
398 * domain adding the default trace directory.
400 static int add_uri_to_consumer(struct consumer_output
*consumer
,
401 struct lttng_uri
*uri
, int domain
, const char *session_name
)
404 const char *default_trace_dir
;
408 if (consumer
== NULL
) {
409 DBG("No consumer detected. Don't add URI. Stopping.");
410 ret
= LTTNG_ERR_NO_CONSUMER
;
415 case LTTNG_DOMAIN_KERNEL
:
416 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
418 case LTTNG_DOMAIN_UST
:
419 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
423 * This case is possible is we try to add the URI to the global tracing
424 * session consumer object which in this case there is no subdir.
426 default_trace_dir
= "";
429 switch (uri
->dtype
) {
432 DBG2("Setting network URI to consumer");
434 if (consumer
->type
== CONSUMER_DST_NET
) {
435 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
436 consumer
->dst
.net
.control_isset
) ||
437 (uri
->stype
== LTTNG_STREAM_DATA
&&
438 consumer
->dst
.net
.data_isset
)) {
439 ret
= LTTNG_ERR_URL_EXIST
;
443 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
446 consumer
->type
= CONSUMER_DST_NET
;
448 /* Set URI into consumer output object */
449 ret
= consumer_set_network_uri(consumer
, uri
);
453 } else if (ret
== 1) {
455 * URI was the same in the consumer so we do not append the subdir
456 * again so to not duplicate output dir.
461 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
462 ret
= consumer_set_subdir(consumer
, session_name
);
464 ret
= LTTNG_ERR_FATAL
;
469 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
470 /* On a new subdir, reappend the default trace dir. */
471 strncat(consumer
->subdir
, default_trace_dir
,
472 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
473 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
478 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
479 memset(consumer
->dst
.trace_path
, 0,
480 sizeof(consumer
->dst
.trace_path
));
481 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
482 sizeof(consumer
->dst
.trace_path
));
483 /* Append default trace dir */
484 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
485 sizeof(consumer
->dst
.trace_path
) -
486 strlen(consumer
->dst
.trace_path
) - 1);
487 /* Flag consumer as local. */
488 consumer
->type
= CONSUMER_DST_LOCAL
;
499 * Init tracing by creating trace directory and sending fds kernel consumer.
501 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
504 struct lttng_ht_iter iter
;
505 struct consumer_socket
*socket
;
511 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
512 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
514 pthread_mutex_lock(socket
->lock
);
515 ret
= kernel_consumer_send_session(socket
, session
);
516 pthread_mutex_unlock(socket
->lock
);
518 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
530 * Create a socket to the relayd using the URI.
532 * On success, the relayd_sock pointer is set to the created socket.
533 * Else, it's stays untouched and a lttcomm error code is returned.
535 static int create_connect_relayd(struct lttng_uri
*uri
,
536 struct lttcomm_relayd_sock
**relayd_sock
)
539 struct lttcomm_relayd_sock
*rsock
;
541 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
542 RELAYD_VERSION_COMM_MINOR
);
544 ret
= LTTNG_ERR_FATAL
;
549 * Connect to relayd so we can proceed with a session creation. This call
550 * can possibly block for an arbitrary amount of time to set the health
551 * state to be in poll execution.
554 ret
= relayd_connect(rsock
);
557 ERR("Unable to reach lttng-relayd");
558 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
562 /* Create socket for control stream. */
563 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
564 DBG3("Creating relayd stream socket from URI");
566 /* Check relayd version */
567 ret
= relayd_version_check(rsock
);
569 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
572 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
573 DBG3("Creating relayd data socket from URI");
575 /* Command is not valid */
576 ERR("Relayd invalid stream type: %d", uri
->stype
);
577 ret
= LTTNG_ERR_INVALID
;
581 *relayd_sock
= rsock
;
586 /* The returned value is not useful since we are on an error path. */
587 (void) relayd_close(rsock
);
595 * Connect to the relayd using URI and send the socket to the right consumer.
597 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
598 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
599 struct consumer_socket
*consumer_sock
,
600 char *session_name
, char *hostname
, int session_live_timer
)
603 struct lttcomm_relayd_sock
*rsock
= NULL
;
605 /* Connect to relayd and make version check if uri is the control. */
606 ret
= create_connect_relayd(relayd_uri
, &rsock
);
607 if (ret
!= LTTNG_OK
) {
612 /* Set the network sequence index if not set. */
613 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
614 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
616 * Increment net_seq_idx because we are about to transfer the
617 * new relayd socket to the consumer.
618 * Assign unique key so the consumer can match streams.
620 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
621 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
624 /* Send relayd socket to consumer. */
625 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
626 relayd_uri
->stype
, session_id
,
627 session_name
, hostname
, session_live_timer
);
629 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
633 /* Flag that the corresponding socket was sent. */
634 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
635 consumer_sock
->control_sock_sent
= 1;
636 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
637 consumer_sock
->data_sock_sent
= 1;
643 * Close socket which was dup on the consumer side. The session daemon does
644 * NOT keep track of the relayd socket(s) once transfer to the consumer.
648 (void) relayd_close(rsock
);
652 if (ret
!= LTTNG_OK
) {
654 * The consumer output for this session should not be used anymore
655 * since the relayd connection failed thus making any tracing or/and
656 * streaming not usable.
658 consumer
->enabled
= 0;
664 * Send both relayd sockets to a specific consumer and domain. This is a
665 * helper function to facilitate sending the information to the consumer for a
668 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
669 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
670 char *session_name
, char *hostname
, int session_live_timer
)
677 /* Sending control relayd socket. */
678 if (!sock
->control_sock_sent
) {
679 ret
= send_consumer_relayd_socket(domain
, session_id
,
680 &consumer
->dst
.net
.control
, consumer
, sock
,
681 session_name
, hostname
, session_live_timer
);
682 if (ret
!= LTTNG_OK
) {
687 /* Sending data relayd socket. */
688 if (!sock
->data_sock_sent
) {
689 ret
= send_consumer_relayd_socket(domain
, session_id
,
690 &consumer
->dst
.net
.data
, consumer
, sock
,
691 session_name
, hostname
, session_live_timer
);
692 if (ret
!= LTTNG_OK
) {
702 * Setup relayd connections for a tracing session. First creates the socket to
703 * the relayd and send them to the right domain consumer. Consumer type MUST be
706 int cmd_setup_relayd(struct ltt_session
*session
)
709 struct ltt_ust_session
*usess
;
710 struct ltt_kernel_session
*ksess
;
711 struct consumer_socket
*socket
;
712 struct lttng_ht_iter iter
;
716 usess
= session
->ust_session
;
717 ksess
= session
->kernel_session
;
719 DBG("Setting relayd for session %s", session
->name
);
723 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
724 && usess
->consumer
->enabled
) {
725 /* For each consumer socket, send relayd sockets */
726 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
728 pthread_mutex_lock(socket
->lock
);
729 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
730 usess
->consumer
, socket
,
731 session
->name
, session
->hostname
,
732 session
->live_timer
);
733 pthread_mutex_unlock(socket
->lock
);
734 if (ret
!= LTTNG_OK
) {
737 /* Session is now ready for network streaming. */
738 session
->net_handle
= 1;
742 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
743 && ksess
->consumer
->enabled
) {
744 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
746 pthread_mutex_lock(socket
->lock
);
747 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
748 ksess
->consumer
, socket
,
749 session
->name
, session
->hostname
,
750 session
->live_timer
);
751 pthread_mutex_unlock(socket
->lock
);
752 if (ret
!= LTTNG_OK
) {
755 /* Session is now ready for network streaming. */
756 session
->net_handle
= 1;
766 * Start a kernel session by opening all necessary streams.
768 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
771 struct ltt_kernel_channel
*kchan
;
773 /* Open kernel metadata */
774 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
775 ret
= kernel_open_metadata(ksess
);
777 ret
= LTTNG_ERR_KERN_META_FAIL
;
782 /* Open kernel metadata stream */
783 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
784 ret
= kernel_open_metadata_stream(ksess
);
786 ERR("Kernel create metadata stream failed");
787 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
792 /* For each channel */
793 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
794 if (kchan
->stream_count
== 0) {
795 ret
= kernel_open_channel_stream(kchan
);
797 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
800 /* Update the stream global counter */
801 ksess
->stream_count_global
+= ret
;
805 /* Setup kernel consumer socket and send fds to it */
806 ret
= init_kernel_tracing(ksess
);
808 ret
= LTTNG_ERR_KERN_START_FAIL
;
812 /* This start the kernel tracing */
813 ret
= kernel_start_session(ksess
);
815 ret
= LTTNG_ERR_KERN_START_FAIL
;
819 /* Quiescent wait after starting trace */
820 kernel_wait_quiescent(kernel_tracer_fd
);
831 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
833 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
837 struct ltt_ust_session
*usess
;
839 usess
= session
->ust_session
;
844 case LTTNG_DOMAIN_KERNEL
:
846 ret
= channel_kernel_disable(session
->kernel_session
,
848 if (ret
!= LTTNG_OK
) {
852 kernel_wait_quiescent(kernel_tracer_fd
);
855 case LTTNG_DOMAIN_UST
:
857 struct ltt_ust_channel
*uchan
;
858 struct lttng_ht
*chan_ht
;
860 chan_ht
= usess
->domain_global
.channels
;
862 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
864 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
868 ret
= channel_ust_disable(usess
, uchan
);
869 if (ret
!= LTTNG_OK
) {
875 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
876 case LTTNG_DOMAIN_UST_EXEC_NAME
:
877 case LTTNG_DOMAIN_UST_PID
:
880 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
892 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
894 * The wpipe arguments is used as a notifier for the kernel thread.
896 int cmd_enable_channel(struct ltt_session
*session
,
897 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
900 struct ltt_ust_session
*usess
= session
->ust_session
;
901 struct lttng_ht
*chan_ht
;
907 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
912 * Don't try to enable a channel if the session has been started at
913 * some point in time before. The tracer does not allow it.
915 if (session
->started
) {
916 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
921 * If the session is a live session, remove the switch timer, the
922 * live timer does the same thing but sends also synchronisation
923 * beacons for inactive streams.
925 if (session
->live_timer
> 0) {
926 attr
->attr
.live_timer_interval
= session
->live_timer
;
927 attr
->attr
.switch_timer_interval
= 0;
930 switch (domain
->type
) {
931 case LTTNG_DOMAIN_KERNEL
:
933 struct ltt_kernel_channel
*kchan
;
935 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
936 session
->kernel_session
);
939 * Don't try to create a channel if the session
940 * has been started at some point in time
941 * before. The tracer does not allow it.
943 if (session
->started
) {
944 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
947 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
948 if (attr
->name
[0] != '\0') {
949 session
->kernel_session
->has_non_default_channel
= 1;
952 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
955 if (ret
!= LTTNG_OK
) {
959 kernel_wait_quiescent(kernel_tracer_fd
);
962 case LTTNG_DOMAIN_UST
:
964 struct ltt_ust_channel
*uchan
;
966 chan_ht
= usess
->domain_global
.channels
;
968 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
971 * Don't try to create a channel if the session
972 * has been started at some point in time
973 * before. The tracer does not allow it.
975 if (session
->started
) {
976 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
979 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
980 if (attr
->name
[0] != '\0') {
981 usess
->has_non_default_channel
= 1;
984 ret
= channel_ust_enable(usess
, uchan
);
989 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1000 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1002 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1003 char *channel_name
, char *event_name
)
1010 case LTTNG_DOMAIN_KERNEL
:
1012 struct ltt_kernel_channel
*kchan
;
1013 struct ltt_kernel_session
*ksess
;
1015 ksess
= session
->kernel_session
;
1018 * If a non-default channel has been created in the
1019 * session, explicitely require that -c chan_name needs
1022 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1023 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1027 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1028 if (kchan
== NULL
) {
1029 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1033 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1034 if (ret
!= LTTNG_OK
) {
1038 kernel_wait_quiescent(kernel_tracer_fd
);
1041 case LTTNG_DOMAIN_UST
:
1043 struct ltt_ust_channel
*uchan
;
1044 struct ltt_ust_session
*usess
;
1046 usess
= session
->ust_session
;
1049 * If a non-default channel has been created in the
1050 * session, explicitely require that -c chan_name needs
1053 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1054 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1058 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1060 if (uchan
== NULL
) {
1061 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1065 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1066 if (ret
!= LTTNG_OK
) {
1070 DBG3("Disable UST event %s in channel %s completed", event_name
,
1074 case LTTNG_DOMAIN_JUL
:
1076 struct ltt_ust_session
*usess
= session
->ust_session
;
1080 ret
= event_jul_disable(usess
, event_name
);
1081 if (ret
!= LTTNG_OK
) {
1088 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1089 case LTTNG_DOMAIN_UST_PID
:
1090 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1093 ret
= LTTNG_ERR_UND
;
1105 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
1107 int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
1115 case LTTNG_DOMAIN_KERNEL
:
1117 struct ltt_kernel_session
*ksess
;
1118 struct ltt_kernel_channel
*kchan
;
1120 ksess
= session
->kernel_session
;
1123 * If a non-default channel has been created in the
1124 * session, explicitely require that -c chan_name needs
1127 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1128 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1132 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1133 if (kchan
== NULL
) {
1134 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1138 ret
= event_kernel_disable_all(kchan
);
1139 if (ret
!= LTTNG_OK
) {
1143 kernel_wait_quiescent(kernel_tracer_fd
);
1146 case LTTNG_DOMAIN_UST
:
1148 struct ltt_ust_session
*usess
;
1149 struct ltt_ust_channel
*uchan
;
1151 usess
= session
->ust_session
;
1154 * If a non-default channel has been created in the
1155 * session, explicitely require that -c chan_name needs
1158 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1159 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1163 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1165 if (uchan
== NULL
) {
1166 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1170 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1175 DBG3("Disable all UST events in channel %s completed", channel_name
);
1179 case LTTNG_DOMAIN_JUL
:
1181 struct ltt_ust_session
*usess
= session
->ust_session
;
1185 ret
= event_jul_disable_all(usess
);
1186 if (ret
!= LTTNG_OK
) {
1193 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1194 case LTTNG_DOMAIN_UST_PID
:
1195 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1198 ret
= LTTNG_ERR_UND
;
1210 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1212 int cmd_add_context(struct ltt_session
*session
, int domain
,
1213 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1215 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1218 case LTTNG_DOMAIN_KERNEL
:
1219 assert(session
->kernel_session
);
1221 if (session
->kernel_session
->channel_count
== 0) {
1222 /* Create default channel */
1223 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1224 if (ret
!= LTTNG_OK
) {
1227 chan_kern_created
= 1;
1229 /* Add kernel context to kernel tracer */
1230 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1231 if (ret
!= LTTNG_OK
) {
1235 case LTTNG_DOMAIN_UST
:
1237 struct ltt_ust_session
*usess
= session
->ust_session
;
1238 unsigned int chan_count
;
1242 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1243 if (chan_count
== 0) {
1244 struct lttng_channel
*attr
;
1245 /* Create default channel */
1246 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1248 ret
= LTTNG_ERR_FATAL
;
1252 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1253 if (ret
!= LTTNG_OK
) {
1258 chan_ust_created
= 1;
1261 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1262 if (ret
!= LTTNG_OK
) {
1268 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1269 case LTTNG_DOMAIN_UST_PID
:
1270 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1273 ret
= LTTNG_ERR_UND
;
1280 if (chan_kern_created
) {
1281 struct ltt_kernel_channel
*kchan
=
1282 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1283 session
->kernel_session
);
1284 /* Created previously, this should NOT fail. */
1286 kernel_destroy_channel(kchan
);
1289 if (chan_ust_created
) {
1290 struct ltt_ust_channel
*uchan
=
1291 trace_ust_find_channel_by_name(
1292 session
->ust_session
->domain_global
.channels
,
1293 DEFAULT_CHANNEL_NAME
);
1294 /* Created previously, this should NOT fail. */
1296 /* Remove from the channel list of the session. */
1297 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1299 trace_ust_destroy_channel(uchan
);
1305 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1307 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1308 char *channel_name
, struct lttng_event
*event
,
1309 struct lttng_filter_bytecode
*filter
, int wpipe
)
1311 int ret
, channel_created
= 0;
1312 struct lttng_channel
*attr
;
1316 assert(channel_name
);
1320 switch (domain
->type
) {
1321 case LTTNG_DOMAIN_KERNEL
:
1323 struct ltt_kernel_channel
*kchan
;
1326 * If a non-default channel has been created in the
1327 * session, explicitely require that -c chan_name needs
1330 if (session
->kernel_session
->has_non_default_channel
1331 && channel_name
[0] == '\0') {
1332 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1336 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1337 session
->kernel_session
);
1338 if (kchan
== NULL
) {
1339 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1340 LTTNG_BUFFER_GLOBAL
);
1342 ret
= LTTNG_ERR_FATAL
;
1345 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1347 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1348 if (ret
!= LTTNG_OK
) {
1354 channel_created
= 1;
1357 /* Get the newly created kernel channel pointer */
1358 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1359 session
->kernel_session
);
1360 if (kchan
== NULL
) {
1361 /* This sould not happen... */
1362 ret
= LTTNG_ERR_FATAL
;
1366 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1367 if (ret
!= LTTNG_OK
) {
1368 if (channel_created
) {
1369 /* Let's not leak a useless channel. */
1370 kernel_destroy_channel(kchan
);
1375 kernel_wait_quiescent(kernel_tracer_fd
);
1378 case LTTNG_DOMAIN_UST
:
1380 struct ltt_ust_channel
*uchan
;
1381 struct ltt_ust_session
*usess
= session
->ust_session
;
1386 * If a non-default channel has been created in the
1387 * session, explicitely require that -c chan_name needs
1390 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1391 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1395 /* Get channel from global UST domain */
1396 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1398 if (uchan
== NULL
) {
1399 /* Create default channel */
1400 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1401 usess
->buffer_type
);
1403 ret
= LTTNG_ERR_FATAL
;
1406 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1408 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1409 if (ret
!= LTTNG_OK
) {
1415 /* Get the newly created channel reference back */
1416 uchan
= trace_ust_find_channel_by_name(
1417 usess
->domain_global
.channels
, channel_name
);
1421 /* At this point, the session and channel exist on the tracer */
1422 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
, filter
);
1423 if (ret
!= LTTNG_OK
) {
1428 case LTTNG_DOMAIN_JUL
:
1430 struct lttng_event uevent
;
1431 struct lttng_domain tmp_dom
;
1432 struct ltt_ust_session
*usess
= session
->ust_session
;
1436 /* Create the default JUL tracepoint. */
1437 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1438 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1439 strncpy(uevent
.name
, DEFAULT_JUL_EVENT_NAME
, sizeof(uevent
.name
));
1440 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1443 * The domain type is changed because we are about to enable the
1444 * default channel and event for the JUL domain that are hardcoded.
1445 * This happens in the UST domain.
1447 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1448 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1450 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1451 &uevent
, NULL
, wpipe
);
1452 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1456 /* The wild card * means that everything should be enabled. */
1457 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1458 ret
= event_jul_enable_all(usess
);
1460 ret
= event_jul_enable(usess
, event
);
1462 if (ret
!= LTTNG_OK
) {
1469 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1470 case LTTNG_DOMAIN_UST_PID
:
1471 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1474 ret
= LTTNG_ERR_UND
;
1486 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1488 int cmd_enable_event_all(struct ltt_session
*session
,
1489 struct lttng_domain
*domain
, char *channel_name
, int event_type
,
1490 struct lttng_filter_bytecode
*filter
, int wpipe
)
1493 struct lttng_channel
*attr
;
1496 assert(channel_name
);
1500 switch (domain
->type
) {
1501 case LTTNG_DOMAIN_KERNEL
:
1503 struct ltt_kernel_channel
*kchan
;
1505 assert(session
->kernel_session
);
1508 * If a non-default channel has been created in the
1509 * session, explicitely require that -c chan_name needs
1512 if (session
->kernel_session
->has_non_default_channel
1513 && channel_name
[0] == '\0') {
1514 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1518 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1519 session
->kernel_session
);
1520 if (kchan
== NULL
) {
1521 /* Create default channel */
1522 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1523 LTTNG_BUFFER_GLOBAL
);
1525 ret
= LTTNG_ERR_FATAL
;
1528 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1530 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1531 if (ret
!= LTTNG_OK
) {
1537 /* Get the newly created kernel channel pointer */
1538 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1539 session
->kernel_session
);
1543 switch (event_type
) {
1544 case LTTNG_EVENT_SYSCALL
:
1545 ret
= event_kernel_enable_all_syscalls(kchan
, kernel_tracer_fd
);
1547 case LTTNG_EVENT_TRACEPOINT
:
1549 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1550 * events already registered to the channel.
1552 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1554 case LTTNG_EVENT_ALL
:
1555 /* Enable syscalls and tracepoints */
1556 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1559 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1563 /* Manage return value */
1564 if (ret
!= LTTNG_OK
) {
1566 * On error, cmd_enable_channel call will take care of destroying
1567 * the created channel if it was needed.
1572 kernel_wait_quiescent(kernel_tracer_fd
);
1575 case LTTNG_DOMAIN_UST
:
1577 struct ltt_ust_channel
*uchan
;
1578 struct ltt_ust_session
*usess
= session
->ust_session
;
1583 * If a non-default channel has been created in the
1584 * session, explicitely require that -c chan_name needs
1587 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1588 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1592 /* Get channel from global UST domain */
1593 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1595 if (uchan
== NULL
) {
1596 /* Create default channel */
1597 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1598 usess
->buffer_type
);
1600 ret
= LTTNG_ERR_FATAL
;
1603 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1605 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1606 if (ret
!= LTTNG_OK
) {
1612 /* Get the newly created channel reference back */
1613 uchan
= trace_ust_find_channel_by_name(
1614 usess
->domain_global
.channels
, channel_name
);
1618 /* At this point, the session and channel exist on the tracer */
1620 switch (event_type
) {
1621 case LTTNG_EVENT_ALL
:
1622 case LTTNG_EVENT_TRACEPOINT
:
1623 ret
= event_ust_enable_all_tracepoints(usess
, uchan
, filter
);
1624 if (ret
!= LTTNG_OK
) {
1629 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
1633 /* Manage return value */
1634 if (ret
!= LTTNG_OK
) {
1640 case LTTNG_DOMAIN_JUL
:
1642 struct lttng_event uevent
;
1643 struct lttng_domain tmp_dom
;
1644 struct ltt_ust_session
*usess
= session
->ust_session
;
1648 /* Create the default JUL tracepoint. */
1649 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1650 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1651 strncpy(uevent
.name
, DEFAULT_JUL_EVENT_NAME
, sizeof(uevent
.name
));
1652 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1655 * The domain type is changed because we are about to enable the
1656 * default channel and event for the JUL domain that are hardcoded.
1657 * This happens in the UST domain.
1659 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1660 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1662 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1663 &uevent
, NULL
, wpipe
);
1664 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1668 ret
= event_jul_enable_all(usess
);
1669 if (ret
!= LTTNG_OK
) {
1676 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1677 case LTTNG_DOMAIN_UST_PID
:
1678 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1681 ret
= LTTNG_ERR_UND
;
1694 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1696 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1699 ssize_t nb_events
= 0;
1702 case LTTNG_DOMAIN_KERNEL
:
1703 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1704 if (nb_events
< 0) {
1705 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1709 case LTTNG_DOMAIN_UST
:
1710 nb_events
= ust_app_list_events(events
);
1711 if (nb_events
< 0) {
1712 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1716 case LTTNG_DOMAIN_JUL
:
1717 nb_events
= jul_list_events(events
);
1718 if (nb_events
< 0) {
1719 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1724 ret
= LTTNG_ERR_UND
;
1731 /* Return negative value to differentiate return code */
1736 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1738 ssize_t
cmd_list_tracepoint_fields(int domain
,
1739 struct lttng_event_field
**fields
)
1742 ssize_t nb_fields
= 0;
1745 case LTTNG_DOMAIN_UST
:
1746 nb_fields
= ust_app_list_event_fields(fields
);
1747 if (nb_fields
< 0) {
1748 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1752 case LTTNG_DOMAIN_KERNEL
:
1753 default: /* fall-through */
1754 ret
= LTTNG_ERR_UND
;
1761 /* Return negative value to differentiate return code */
1766 * Command LTTNG_START_TRACE processed by the client thread.
1768 int cmd_start_trace(struct ltt_session
*session
)
1771 struct ltt_kernel_session
*ksession
;
1772 struct ltt_ust_session
*usess
;
1776 /* Ease our life a bit ;) */
1777 ksession
= session
->kernel_session
;
1778 usess
= session
->ust_session
;
1780 if (session
->enabled
) {
1781 /* Already started. */
1782 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1786 session
->enabled
= 1;
1788 /* Kernel tracing */
1789 if (ksession
!= NULL
) {
1790 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1791 if (ret
!= LTTNG_OK
) {
1796 /* Flag session that trace should start automatically */
1798 usess
->start_trace
= 1;
1800 ret
= ust_app_start_trace_all(usess
);
1802 ret
= LTTNG_ERR_UST_START_FAIL
;
1807 session
->started
= 1;
1816 * Command LTTNG_STOP_TRACE processed by the client thread.
1818 int cmd_stop_trace(struct ltt_session
*session
)
1821 struct ltt_kernel_channel
*kchan
;
1822 struct ltt_kernel_session
*ksession
;
1823 struct ltt_ust_session
*usess
;
1828 ksession
= session
->kernel_session
;
1829 usess
= session
->ust_session
;
1831 if (!session
->enabled
) {
1832 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1836 session
->enabled
= 0;
1839 if (ksession
&& ksession
->started
) {
1840 DBG("Stop kernel tracing");
1842 /* Flush metadata if exist */
1843 if (ksession
->metadata_stream_fd
>= 0) {
1844 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1846 ERR("Kernel metadata flush failed");
1850 /* Flush all buffers before stopping */
1851 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1852 ret
= kernel_flush_buffer(kchan
);
1854 ERR("Kernel flush buffer error");
1858 ret
= kernel_stop_session(ksession
);
1860 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1864 kernel_wait_quiescent(kernel_tracer_fd
);
1866 ksession
->started
= 0;
1869 if (usess
&& usess
->start_trace
) {
1870 usess
->start_trace
= 0;
1872 ret
= ust_app_stop_trace_all(usess
);
1874 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1886 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1888 int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
1889 size_t nb_uri
, struct lttng_uri
*uris
)
1892 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1893 struct ltt_ust_session
*usess
= session
->ust_session
;
1894 struct consumer_output
*consumer
= NULL
;
1900 /* Can't enable consumer after session started. */
1901 if (session
->enabled
) {
1902 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1907 * This case switch makes sure the domain session has a temporary consumer
1908 * so the URL can be set.
1912 /* Code flow error. A session MUST always have a consumer object */
1913 assert(session
->consumer
);
1915 * The URL will be added to the tracing session consumer instead of a
1916 * specific domain consumer.
1918 consumer
= session
->consumer
;
1920 case LTTNG_DOMAIN_KERNEL
:
1921 /* Code flow error if we don't have a kernel session here. */
1923 assert(ksess
->consumer
);
1924 consumer
= ksess
->consumer
;
1926 case LTTNG_DOMAIN_UST
:
1927 /* Code flow error if we don't have a kernel session here. */
1929 assert(usess
->consumer
);
1930 consumer
= usess
->consumer
;
1934 for (i
= 0; i
< nb_uri
; i
++) {
1935 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
1936 if (ret
!= LTTNG_OK
) {
1949 * Command LTTNG_CREATE_SESSION processed by the client thread.
1951 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
1952 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
1955 struct ltt_session
*session
;
1961 * Verify if the session already exist
1963 * XXX: There is no need for the session lock list here since the caller
1964 * (process_client_msg) is holding it. We might want to change that so a
1965 * single command does not lock the entire session list.
1967 session
= session_find_by_name(name
);
1968 if (session
!= NULL
) {
1969 ret
= LTTNG_ERR_EXIST_SESS
;
1973 /* Create tracing session in the registry */
1974 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
1975 LTTNG_SOCK_GET_GID_CRED(creds
));
1976 if (ret
!= LTTNG_OK
) {
1981 * Get the newly created session pointer back
1983 * XXX: There is no need for the session lock list here since the caller
1984 * (process_client_msg) is holding it. We might want to change that so a
1985 * single command does not lock the entire session list.
1987 session
= session_find_by_name(name
);
1990 session
->live_timer
= live_timer
;
1991 /* Create default consumer output for the session not yet created. */
1992 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
1993 if (session
->consumer
== NULL
) {
1994 ret
= LTTNG_ERR_FATAL
;
1995 goto consumer_error
;
1999 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
2000 if (ret
!= LTTNG_OK
) {
2001 goto consumer_error
;
2003 session
->output_traces
= 1;
2005 session
->output_traces
= 0;
2006 DBG2("Session %s created with no output", session
->name
);
2009 session
->consumer
->enabled
= 1;
2014 session_destroy(session
);
2021 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2023 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2024 size_t nb_uri
, lttng_sock_cred
*creds
)
2027 struct ltt_session
*session
;
2028 struct snapshot_output
*new_output
= NULL
;
2034 * Create session in no output mode with URIs set to NULL. The uris we've
2035 * received are for a default snapshot output if one.
2037 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2038 if (ret
!= LTTNG_OK
) {
2042 /* Get the newly created session pointer back. This should NEVER fail. */
2043 session
= session_find_by_name(name
);
2046 /* Flag session for snapshot mode. */
2047 session
->snapshot_mode
= 1;
2049 /* Skip snapshot output creation if no URI is given. */
2054 new_output
= snapshot_output_alloc();
2056 ret
= LTTNG_ERR_NOMEM
;
2057 goto error_snapshot_alloc
;
2060 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2061 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2063 if (ret
== -ENOMEM
) {
2064 ret
= LTTNG_ERR_NOMEM
;
2066 ret
= LTTNG_ERR_INVALID
;
2068 goto error_snapshot
;
2072 snapshot_add_output(&session
->snapshot
, new_output
);
2079 snapshot_output_destroy(new_output
);
2080 error_snapshot_alloc
:
2081 session_destroy(session
);
2087 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2089 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2092 struct ltt_ust_session
*usess
;
2093 struct ltt_kernel_session
*ksess
;
2098 usess
= session
->ust_session
;
2099 ksess
= session
->kernel_session
;
2101 /* Clean kernel session teardown */
2102 kernel_destroy_session(ksess
);
2104 /* UST session teardown */
2106 /* Close any relayd session */
2107 consumer_output_send_destroy_relayd(usess
->consumer
);
2109 /* Destroy every UST application related to this session. */
2110 ret
= ust_app_destroy_trace_all(usess
);
2112 ERR("Error in ust_app_destroy_trace_all");
2115 /* Clean up the rest. */
2116 trace_ust_destroy_session(usess
);
2120 * Must notify the kernel thread here to update it's poll set in order to
2121 * remove the channel(s)' fd just destroyed.
2123 ret
= notify_thread_pipe(wpipe
);
2125 PERROR("write kernel poll pipe");
2128 ret
= session_destroy(session
);
2134 * Command LTTNG_CALIBRATE processed by the client thread.
2136 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2141 case LTTNG_DOMAIN_KERNEL
:
2143 struct lttng_kernel_calibrate kcalibrate
;
2145 kcalibrate
.type
= calibrate
->type
;
2146 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2148 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2153 case LTTNG_DOMAIN_UST
:
2155 struct lttng_ust_calibrate ucalibrate
;
2157 ucalibrate
.type
= calibrate
->type
;
2158 ret
= ust_app_calibrate_glb(&ucalibrate
);
2160 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2166 ret
= LTTNG_ERR_UND
;
2177 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2179 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2180 const char *sock_path
, struct consumer_data
*cdata
)
2183 struct consumer_socket
*socket
= NULL
;
2190 case LTTNG_DOMAIN_KERNEL
:
2192 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2196 /* Can't register a consumer if there is already one */
2197 if (ksess
->consumer_fds_sent
!= 0) {
2198 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2202 sock
= lttcomm_connect_unix_sock(sock_path
);
2204 ret
= LTTNG_ERR_CONNECT_FAIL
;
2207 cdata
->cmd_sock
= sock
;
2209 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2210 if (socket
== NULL
) {
2213 PERROR("close register consumer");
2215 cdata
->cmd_sock
= -1;
2216 ret
= LTTNG_ERR_FATAL
;
2220 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2221 if (socket
->lock
== NULL
) {
2222 PERROR("zmalloc pthread mutex");
2223 ret
= LTTNG_ERR_FATAL
;
2226 pthread_mutex_init(socket
->lock
, NULL
);
2227 socket
->registered
= 1;
2230 consumer_add_socket(socket
, ksess
->consumer
);
2233 pthread_mutex_lock(&cdata
->pid_mutex
);
2235 pthread_mutex_unlock(&cdata
->pid_mutex
);
2240 /* TODO: Userspace tracing */
2241 ret
= LTTNG_ERR_UND
;
2249 consumer_destroy_socket(socket
);
2255 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2257 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2258 struct lttng_domain
**domains
)
2263 if (session
->kernel_session
!= NULL
) {
2264 DBG3("Listing domains found kernel domain");
2268 if (session
->ust_session
!= NULL
) {
2269 DBG3("Listing domains found UST global domain");
2272 if (session
->ust_session
->domain_jul
.being_used
) {
2277 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2278 if (*domains
== NULL
) {
2279 ret
= LTTNG_ERR_FATAL
;
2283 if (session
->kernel_session
!= NULL
) {
2284 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2288 if (session
->ust_session
!= NULL
) {
2289 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2290 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2293 if (session
->ust_session
->domain_jul
.being_used
) {
2294 (*domains
)[index
].type
= LTTNG_DOMAIN_JUL
;
2295 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2303 /* Return negative value to differentiate return code */
2309 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2311 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2312 struct lttng_channel
**channels
)
2315 ssize_t nb_chan
= 0;
2318 case LTTNG_DOMAIN_KERNEL
:
2319 if (session
->kernel_session
!= NULL
) {
2320 nb_chan
= session
->kernel_session
->channel_count
;
2322 DBG3("Number of kernel channels %zd", nb_chan
);
2324 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2327 case LTTNG_DOMAIN_UST
:
2328 if (session
->ust_session
!= NULL
) {
2329 nb_chan
= lttng_ht_get_count(
2330 session
->ust_session
->domain_global
.channels
);
2332 DBG3("Number of UST global channels %zd", nb_chan
);
2334 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2339 ret
= LTTNG_ERR_UND
;
2344 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2345 if (*channels
== NULL
) {
2346 ret
= LTTNG_ERR_FATAL
;
2350 list_lttng_channels(domain
, session
, *channels
);
2353 /* Ret value was set in the domain switch case */
2360 /* Return negative value to differentiate return code */
2365 * Command LTTNG_LIST_EVENTS processed by the client thread.
2367 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2368 char *channel_name
, struct lttng_event
**events
)
2371 ssize_t nb_event
= 0;
2374 case LTTNG_DOMAIN_KERNEL
:
2375 if (session
->kernel_session
!= NULL
) {
2376 nb_event
= list_lttng_kernel_events(channel_name
,
2377 session
->kernel_session
, events
);
2380 case LTTNG_DOMAIN_UST
:
2382 if (session
->ust_session
!= NULL
) {
2383 nb_event
= list_lttng_ust_global_events(channel_name
,
2384 &session
->ust_session
->domain_global
, events
);
2388 case LTTNG_DOMAIN_JUL
:
2389 if (session
->ust_session
) {
2390 nb_event
= list_lttng_jul_events(
2391 &session
->ust_session
->domain_jul
, events
);
2395 ret
= LTTNG_ERR_UND
;
2402 /* Return negative value to differentiate return code */
2407 * Using the session list, filled a lttng_session array to send back to the
2408 * client for session listing.
2410 * The session list lock MUST be acquired before calling this function. Use
2411 * session_lock_list() and session_unlock_list().
2413 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2418 struct ltt_session
*session
;
2419 struct ltt_session_list
*list
= session_get_list();
2421 DBG("Getting all available session for UID %d GID %d",
2424 * Iterate over session list and append data after the control struct in
2427 cds_list_for_each_entry(session
, &list
->head
, list
) {
2429 * Only list the sessions the user can control.
2431 if (!session_access_ok(session
, uid
, gid
)) {
2435 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2436 struct ltt_ust_session
*usess
= session
->ust_session
;
2438 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2439 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2440 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2441 ret
= build_network_session_path(sessions
[i
].path
,
2442 sizeof(sessions
[i
].path
), session
);
2444 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2445 session
->consumer
->dst
.trace_path
);
2448 PERROR("snprintf session path");
2452 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2453 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2454 sessions
[i
].enabled
= session
->enabled
;
2455 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2461 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2462 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2464 int cmd_data_pending(struct ltt_session
*session
)
2467 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2468 struct ltt_ust_session
*usess
= session
->ust_session
;
2472 /* Session MUST be stopped to ask for data availability. */
2473 if (session
->enabled
) {
2474 ret
= LTTNG_ERR_SESSION_STARTED
;
2478 if (ksess
&& ksess
->consumer
) {
2479 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2481 /* Data is still being extracted for the kernel. */
2486 if (usess
&& usess
->consumer
) {
2487 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2489 /* Data is still being extracted for the kernel. */
2494 /* Data is ready to be read by a viewer */
2502 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2504 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2506 int cmd_snapshot_add_output(struct ltt_session
*session
,
2507 struct lttng_snapshot_output
*output
, uint32_t *id
)
2510 struct snapshot_output
*new_output
;
2515 DBG("Cmd snapshot add output for session %s", session
->name
);
2518 * Permission denied to create an output if the session is not
2519 * set in no output mode.
2521 if (session
->output_traces
) {
2522 ret
= LTTNG_ERR_EPERM
;
2526 /* Only one output is allowed until we have the "tee" feature. */
2527 if (session
->snapshot
.nb_output
== 1) {
2528 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2532 new_output
= snapshot_output_alloc();
2534 ret
= LTTNG_ERR_NOMEM
;
2538 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2539 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2540 &session
->snapshot
);
2542 if (ret
== -ENOMEM
) {
2543 ret
= LTTNG_ERR_NOMEM
;
2545 ret
= LTTNG_ERR_INVALID
;
2551 snapshot_add_output(&session
->snapshot
, new_output
);
2553 *id
= new_output
->id
;
2560 snapshot_output_destroy(new_output
);
2566 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2568 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2570 int cmd_snapshot_del_output(struct ltt_session
*session
,
2571 struct lttng_snapshot_output
*output
)
2574 struct snapshot_output
*sout
= NULL
;
2582 * Permission denied to create an output if the session is not
2583 * set in no output mode.
2585 if (session
->output_traces
) {
2586 ret
= LTTNG_ERR_EPERM
;
2591 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2593 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2594 } else if (*output
->name
!= '\0') {
2595 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2597 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2600 ret
= LTTNG_ERR_INVALID
;
2604 snapshot_delete_output(&session
->snapshot
, sout
);
2605 snapshot_output_destroy(sout
);
2614 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2616 * If no output is available, outputs is untouched and 0 is returned.
2618 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2620 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2621 struct lttng_snapshot_output
**outputs
)
2624 struct lttng_snapshot_output
*list
;
2625 struct lttng_ht_iter iter
;
2626 struct snapshot_output
*output
;
2631 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2634 * Permission denied to create an output if the session is not
2635 * set in no output mode.
2637 if (session
->output_traces
) {
2638 ret
= LTTNG_ERR_EPERM
;
2642 if (session
->snapshot
.nb_output
== 0) {
2647 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2649 ret
= LTTNG_ERR_NOMEM
;
2653 /* Copy list from session to the new list object. */
2654 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2655 output
, node
.node
) {
2656 assert(output
->consumer
);
2657 list
[idx
].id
= output
->id
;
2658 list
[idx
].max_size
= output
->max_size
;
2659 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2660 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2661 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2662 sizeof(list
[idx
].ctrl_url
));
2665 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2666 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2668 ret
= LTTNG_ERR_NOMEM
;
2673 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2674 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2676 ret
= LTTNG_ERR_NOMEM
;
2684 return session
->snapshot
.nb_output
;
2693 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2694 * snapshot output is *not* set with a remote destination.
2696 * Return 0 on success or a LTTNG_ERR code.
2698 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2699 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2702 struct lttng_ht_iter iter
;
2703 struct consumer_socket
*socket
;
2706 assert(snap_output
);
2709 DBG2("Set relayd object from snapshot output");
2711 /* Ignore if snapshot consumer output is not network. */
2712 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2717 * For each consumer socket, create and send the relayd object of the
2721 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2722 socket
, node
.node
) {
2723 ret
= send_consumer_relayd_sockets(0, session
->id
,
2724 snap_output
->consumer
, socket
,
2725 session
->name
, session
->hostname
,
2726 session
->live_timer
);
2727 if (ret
!= LTTNG_OK
) {
2739 * Record a kernel snapshot.
2741 * Return LTTNG_OK on success or a LTTNG_ERR code.
2743 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2744 struct snapshot_output
*output
, struct ltt_session
*session
,
2745 int wait
, int nb_streams
)
2753 /* Get the datetime for the snapshot output directory. */
2754 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2755 sizeof(output
->datetime
));
2757 ret
= LTTNG_ERR_INVALID
;
2762 * Copy kernel session sockets so we can communicate with the right
2763 * consumer for the snapshot record command.
2765 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2767 ret
= LTTNG_ERR_NOMEM
;
2771 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2772 if (ret
!= LTTNG_OK
) {
2773 goto error_snapshot
;
2776 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_streams
);
2777 if (ret
!= LTTNG_OK
) {
2778 goto error_snapshot
;
2784 /* Clean up copied sockets so this output can use some other later on. */
2785 consumer_destroy_output_sockets(output
->consumer
);
2791 * Record a UST snapshot.
2793 * Return 0 on success or a LTTNG_ERR error code.
2795 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2796 struct snapshot_output
*output
, struct ltt_session
*session
,
2797 int wait
, int nb_streams
)
2805 /* Get the datetime for the snapshot output directory. */
2806 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2807 sizeof(output
->datetime
));
2809 ret
= LTTNG_ERR_INVALID
;
2814 * Copy UST session sockets so we can communicate with the right
2815 * consumer for the snapshot record command.
2817 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
2819 ret
= LTTNG_ERR_NOMEM
;
2823 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
2824 if (ret
!= LTTNG_OK
) {
2825 goto error_snapshot
;
2828 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_streams
);
2830 if (ret
== -EINVAL
) {
2831 ret
= LTTNG_ERR_INVALID
;
2832 goto error_snapshot
;
2835 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
2836 goto error_snapshot
;
2842 /* Clean up copied sockets so this output can use some other later on. */
2843 consumer_destroy_output_sockets(output
->consumer
);
2849 * Returns the total number of streams for a session or a negative value
2852 static unsigned int get_total_nb_stream(struct ltt_session
*session
)
2854 unsigned int total_streams
= 0;
2856 if (session
->kernel_session
) {
2857 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2859 total_streams
+= ksess
->stream_count_global
;
2862 if (session
->ust_session
) {
2863 struct ltt_ust_session
*usess
= session
->ust_session
;
2865 total_streams
+= ust_app_get_nb_stream(usess
);
2868 return total_streams
;
2872 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
2874 * The wait parameter is ignored so this call always wait for the snapshot to
2875 * complete before returning.
2877 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2879 int cmd_snapshot_record(struct ltt_session
*session
,
2880 struct lttng_snapshot_output
*output
, int wait
)
2883 unsigned int use_tmp_output
= 0;
2884 struct snapshot_output tmp_output
;
2885 unsigned int nb_streams
, snapshot_success
= 0;
2889 DBG("Cmd snapshot record for session %s", session
->name
);
2892 * Permission denied to create an output if the session is not
2893 * set in no output mode.
2895 if (session
->output_traces
) {
2896 ret
= LTTNG_ERR_EPERM
;
2900 /* The session needs to be started at least once. */
2901 if (!session
->started
) {
2902 ret
= LTTNG_ERR_START_SESSION_ONCE
;
2906 /* Use temporary output for the session. */
2907 if (output
&& *output
->ctrl_url
!= '\0') {
2908 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2909 output
->ctrl_url
, output
->data_url
, session
->consumer
,
2912 if (ret
== -ENOMEM
) {
2913 ret
= LTTNG_ERR_NOMEM
;
2915 ret
= LTTNG_ERR_INVALID
;
2919 /* Use the global session count for the temporary snapshot. */
2920 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
2925 * Get the total number of stream of that session which is used by the
2926 * maximum size of the snapshot feature.
2928 nb_streams
= get_total_nb_stream(session
);
2930 if (session
->kernel_session
) {
2931 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2933 if (use_tmp_output
) {
2934 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
2936 if (ret
!= LTTNG_OK
) {
2939 snapshot_success
= 1;
2941 struct snapshot_output
*sout
;
2942 struct lttng_ht_iter iter
;
2945 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
2946 &iter
.iter
, sout
, node
.node
) {
2948 * Make a local copy of the output and assign the possible
2949 * temporary value given by the caller.
2951 memset(&tmp_output
, 0, sizeof(tmp_output
));
2952 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
2954 /* Use temporary max size. */
2955 if (output
->max_size
!= (uint64_t) -1ULL) {
2956 tmp_output
.max_size
= output
->max_size
;
2959 /* Use temporary name. */
2960 if (*output
->name
!= '\0') {
2961 strncpy(tmp_output
.name
, output
->name
,
2962 sizeof(tmp_output
.name
));
2965 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
2967 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
2968 session
, wait
, nb_streams
);
2969 if (ret
!= LTTNG_OK
) {
2973 snapshot_success
= 1;
2979 if (session
->ust_session
) {
2980 struct ltt_ust_session
*usess
= session
->ust_session
;
2982 if (use_tmp_output
) {
2983 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
2985 if (ret
!= LTTNG_OK
) {
2988 snapshot_success
= 1;
2990 struct snapshot_output
*sout
;
2991 struct lttng_ht_iter iter
;
2994 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
2995 &iter
.iter
, sout
, node
.node
) {
2997 * Make a local copy of the output and assign the possible
2998 * temporary value given by the caller.
3000 memset(&tmp_output
, 0, sizeof(tmp_output
));
3001 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3003 /* Use temporary max size. */
3004 if (output
->max_size
!= (uint64_t) -1ULL) {
3005 tmp_output
.max_size
= output
->max_size
;
3008 /* Use temporary name. */
3009 if (*output
->name
!= '\0') {
3010 strncpy(tmp_output
.name
, output
->name
,
3011 sizeof(tmp_output
.name
));
3014 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3016 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3018 if (ret
!= LTTNG_OK
) {
3022 snapshot_success
= 1;
3028 if (snapshot_success
) {
3029 session
->snapshot
.nb_snapshot
++;
3031 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3039 * Init command subsystem.
3044 * Set network sequence index to 1 for streams to match a relayd
3045 * socket on the consumer side.
3047 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3048 relayd_net_seq_idx
= 1;
3049 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3051 DBG("Command subsystem initialized");