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
) {
308 if (uevent
->exclusion
) {
309 tmp
[i
].exclusion
= 1;
323 * Fill lttng_event array of all kernel events in the channel.
325 static int list_lttng_kernel_events(char *channel_name
,
326 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
329 unsigned int nb_event
;
330 struct ltt_kernel_event
*event
;
331 struct ltt_kernel_channel
*kchan
;
333 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
335 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
339 nb_event
= kchan
->event_count
;
341 DBG("Listing events for channel %s", kchan
->channel
->name
);
347 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
348 if (*events
== NULL
) {
349 ret
= LTTNG_ERR_FATAL
;
353 /* Kernel channels */
354 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
355 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
356 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
357 (*events
)[i
].enabled
= event
->enabled
;
359 switch (event
->event
->instrumentation
) {
360 case LTTNG_KERNEL_TRACEPOINT
:
361 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
363 case LTTNG_KERNEL_KRETPROBE
:
364 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
365 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
366 sizeof(struct lttng_kernel_kprobe
));
368 case LTTNG_KERNEL_KPROBE
:
369 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
370 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
371 sizeof(struct lttng_kernel_kprobe
));
373 case LTTNG_KERNEL_FUNCTION
:
374 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
375 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
376 sizeof(struct lttng_kernel_function
));
378 case LTTNG_KERNEL_NOOP
:
379 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
381 case LTTNG_KERNEL_SYSCALL
:
382 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
384 case LTTNG_KERNEL_ALL
:
395 /* Negate the error code to differentiate the size from an error */
400 * Add URI so the consumer output object. Set the correct path depending on the
401 * domain adding the default trace directory.
403 static int add_uri_to_consumer(struct consumer_output
*consumer
,
404 struct lttng_uri
*uri
, int domain
, const char *session_name
)
407 const char *default_trace_dir
;
411 if (consumer
== NULL
) {
412 DBG("No consumer detected. Don't add URI. Stopping.");
413 ret
= LTTNG_ERR_NO_CONSUMER
;
418 case LTTNG_DOMAIN_KERNEL
:
419 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
421 case LTTNG_DOMAIN_UST
:
422 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
426 * This case is possible is we try to add the URI to the global tracing
427 * session consumer object which in this case there is no subdir.
429 default_trace_dir
= "";
432 switch (uri
->dtype
) {
435 DBG2("Setting network URI to consumer");
437 if (consumer
->type
== CONSUMER_DST_NET
) {
438 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
439 consumer
->dst
.net
.control_isset
) ||
440 (uri
->stype
== LTTNG_STREAM_DATA
&&
441 consumer
->dst
.net
.data_isset
)) {
442 ret
= LTTNG_ERR_URL_EXIST
;
446 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
449 consumer
->type
= CONSUMER_DST_NET
;
451 /* Set URI into consumer output object */
452 ret
= consumer_set_network_uri(consumer
, uri
);
456 } else if (ret
== 1) {
458 * URI was the same in the consumer so we do not append the subdir
459 * again so to not duplicate output dir.
464 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
465 ret
= consumer_set_subdir(consumer
, session_name
);
467 ret
= LTTNG_ERR_FATAL
;
472 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
473 /* On a new subdir, reappend the default trace dir. */
474 strncat(consumer
->subdir
, default_trace_dir
,
475 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
476 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
481 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
482 memset(consumer
->dst
.trace_path
, 0,
483 sizeof(consumer
->dst
.trace_path
));
484 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
485 sizeof(consumer
->dst
.trace_path
));
486 /* Append default trace dir */
487 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
488 sizeof(consumer
->dst
.trace_path
) -
489 strlen(consumer
->dst
.trace_path
) - 1);
490 /* Flag consumer as local. */
491 consumer
->type
= CONSUMER_DST_LOCAL
;
502 * Init tracing by creating trace directory and sending fds kernel consumer.
504 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
507 struct lttng_ht_iter iter
;
508 struct consumer_socket
*socket
;
514 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
515 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
517 pthread_mutex_lock(socket
->lock
);
518 ret
= kernel_consumer_send_session(socket
, session
);
519 pthread_mutex_unlock(socket
->lock
);
521 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
533 * Create a socket to the relayd using the URI.
535 * On success, the relayd_sock pointer is set to the created socket.
536 * Else, it's stays untouched and a lttcomm error code is returned.
538 static int create_connect_relayd(struct lttng_uri
*uri
,
539 struct lttcomm_relayd_sock
**relayd_sock
)
542 struct lttcomm_relayd_sock
*rsock
;
544 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
545 RELAYD_VERSION_COMM_MINOR
);
547 ret
= LTTNG_ERR_FATAL
;
552 * Connect to relayd so we can proceed with a session creation. This call
553 * can possibly block for an arbitrary amount of time to set the health
554 * state to be in poll execution.
557 ret
= relayd_connect(rsock
);
560 ERR("Unable to reach lttng-relayd");
561 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
565 /* Create socket for control stream. */
566 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
567 DBG3("Creating relayd stream socket from URI");
569 /* Check relayd version */
570 ret
= relayd_version_check(rsock
);
572 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
575 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
576 DBG3("Creating relayd data socket from URI");
578 /* Command is not valid */
579 ERR("Relayd invalid stream type: %d", uri
->stype
);
580 ret
= LTTNG_ERR_INVALID
;
584 *relayd_sock
= rsock
;
589 /* The returned value is not useful since we are on an error path. */
590 (void) relayd_close(rsock
);
598 * Connect to the relayd using URI and send the socket to the right consumer.
600 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
601 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
602 struct consumer_socket
*consumer_sock
,
603 char *session_name
, char *hostname
, int session_live_timer
)
606 struct lttcomm_relayd_sock
*rsock
= NULL
;
608 /* Connect to relayd and make version check if uri is the control. */
609 ret
= create_connect_relayd(relayd_uri
, &rsock
);
610 if (ret
!= LTTNG_OK
) {
615 /* Set the network sequence index if not set. */
616 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
617 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
619 * Increment net_seq_idx because we are about to transfer the
620 * new relayd socket to the consumer.
621 * Assign unique key so the consumer can match streams.
623 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
624 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
627 /* Send relayd socket to consumer. */
628 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
629 relayd_uri
->stype
, session_id
,
630 session_name
, hostname
, session_live_timer
);
632 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
636 /* Flag that the corresponding socket was sent. */
637 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
638 consumer_sock
->control_sock_sent
= 1;
639 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
640 consumer_sock
->data_sock_sent
= 1;
646 * Close socket which was dup on the consumer side. The session daemon does
647 * NOT keep track of the relayd socket(s) once transfer to the consumer.
651 (void) relayd_close(rsock
);
655 if (ret
!= LTTNG_OK
) {
657 * The consumer output for this session should not be used anymore
658 * since the relayd connection failed thus making any tracing or/and
659 * streaming not usable.
661 consumer
->enabled
= 0;
667 * Send both relayd sockets to a specific consumer and domain. This is a
668 * helper function to facilitate sending the information to the consumer for a
671 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
672 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
673 char *session_name
, char *hostname
, int session_live_timer
)
680 /* Sending control relayd socket. */
681 if (!sock
->control_sock_sent
) {
682 ret
= send_consumer_relayd_socket(domain
, session_id
,
683 &consumer
->dst
.net
.control
, consumer
, sock
,
684 session_name
, hostname
, session_live_timer
);
685 if (ret
!= LTTNG_OK
) {
690 /* Sending data relayd socket. */
691 if (!sock
->data_sock_sent
) {
692 ret
= send_consumer_relayd_socket(domain
, session_id
,
693 &consumer
->dst
.net
.data
, consumer
, sock
,
694 session_name
, hostname
, session_live_timer
);
695 if (ret
!= LTTNG_OK
) {
705 * Setup relayd connections for a tracing session. First creates the socket to
706 * the relayd and send them to the right domain consumer. Consumer type MUST be
709 int cmd_setup_relayd(struct ltt_session
*session
)
712 struct ltt_ust_session
*usess
;
713 struct ltt_kernel_session
*ksess
;
714 struct consumer_socket
*socket
;
715 struct lttng_ht_iter iter
;
719 usess
= session
->ust_session
;
720 ksess
= session
->kernel_session
;
722 DBG("Setting relayd for session %s", session
->name
);
726 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
727 && usess
->consumer
->enabled
) {
728 /* For each consumer socket, send relayd sockets */
729 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
731 pthread_mutex_lock(socket
->lock
);
732 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
733 usess
->consumer
, socket
,
734 session
->name
, session
->hostname
,
735 session
->live_timer
);
736 pthread_mutex_unlock(socket
->lock
);
737 if (ret
!= LTTNG_OK
) {
740 /* Session is now ready for network streaming. */
741 session
->net_handle
= 1;
745 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
746 && ksess
->consumer
->enabled
) {
747 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
749 pthread_mutex_lock(socket
->lock
);
750 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
751 ksess
->consumer
, socket
,
752 session
->name
, session
->hostname
,
753 session
->live_timer
);
754 pthread_mutex_unlock(socket
->lock
);
755 if (ret
!= LTTNG_OK
) {
758 /* Session is now ready for network streaming. */
759 session
->net_handle
= 1;
769 * Start a kernel session by opening all necessary streams.
771 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
774 struct ltt_kernel_channel
*kchan
;
776 /* Open kernel metadata */
777 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
778 ret
= kernel_open_metadata(ksess
);
780 ret
= LTTNG_ERR_KERN_META_FAIL
;
785 /* Open kernel metadata stream */
786 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
787 ret
= kernel_open_metadata_stream(ksess
);
789 ERR("Kernel create metadata stream failed");
790 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
795 /* For each channel */
796 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
797 if (kchan
->stream_count
== 0) {
798 ret
= kernel_open_channel_stream(kchan
);
800 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
803 /* Update the stream global counter */
804 ksess
->stream_count_global
+= ret
;
808 /* Setup kernel consumer socket and send fds to it */
809 ret
= init_kernel_tracing(ksess
);
811 ret
= LTTNG_ERR_KERN_START_FAIL
;
815 /* This start the kernel tracing */
816 ret
= kernel_start_session(ksess
);
818 ret
= LTTNG_ERR_KERN_START_FAIL
;
822 /* Quiescent wait after starting trace */
823 kernel_wait_quiescent(kernel_tracer_fd
);
834 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
836 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
840 struct ltt_ust_session
*usess
;
842 usess
= session
->ust_session
;
847 case LTTNG_DOMAIN_KERNEL
:
849 ret
= channel_kernel_disable(session
->kernel_session
,
851 if (ret
!= LTTNG_OK
) {
855 kernel_wait_quiescent(kernel_tracer_fd
);
858 case LTTNG_DOMAIN_UST
:
860 struct ltt_ust_channel
*uchan
;
861 struct lttng_ht
*chan_ht
;
863 chan_ht
= usess
->domain_global
.channels
;
865 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
867 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
871 ret
= channel_ust_disable(usess
, uchan
);
872 if (ret
!= LTTNG_OK
) {
878 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
879 case LTTNG_DOMAIN_UST_EXEC_NAME
:
880 case LTTNG_DOMAIN_UST_PID
:
883 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
895 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
897 * The wpipe arguments is used as a notifier for the kernel thread.
899 int cmd_enable_channel(struct ltt_session
*session
,
900 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
903 struct ltt_ust_session
*usess
= session
->ust_session
;
904 struct lttng_ht
*chan_ht
;
910 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
915 * Don't try to enable a channel if the session has been started at
916 * some point in time before. The tracer does not allow it.
918 if (session
->started
) {
919 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
924 * If the session is a live session, remove the switch timer, the
925 * live timer does the same thing but sends also synchronisation
926 * beacons for inactive streams.
928 if (session
->live_timer
> 0) {
929 attr
->attr
.live_timer_interval
= session
->live_timer
;
930 attr
->attr
.switch_timer_interval
= 0;
933 switch (domain
->type
) {
934 case LTTNG_DOMAIN_KERNEL
:
936 struct ltt_kernel_channel
*kchan
;
938 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
939 session
->kernel_session
);
942 * Don't try to create a channel if the session
943 * has been started at some point in time
944 * before. The tracer does not allow it.
946 if (session
->started
) {
947 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
950 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
951 if (attr
->name
[0] != '\0') {
952 session
->kernel_session
->has_non_default_channel
= 1;
955 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
958 if (ret
!= LTTNG_OK
) {
962 kernel_wait_quiescent(kernel_tracer_fd
);
965 case LTTNG_DOMAIN_UST
:
967 struct ltt_ust_channel
*uchan
;
969 chan_ht
= usess
->domain_global
.channels
;
971 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
974 * Don't try to create a channel if the session
975 * has been started at some point in time
976 * before. The tracer does not allow it.
978 if (session
->started
) {
979 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
982 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
983 if (attr
->name
[0] != '\0') {
984 usess
->has_non_default_channel
= 1;
987 ret
= channel_ust_enable(usess
, uchan
);
992 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1003 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1005 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1006 char *channel_name
, char *event_name
)
1013 case LTTNG_DOMAIN_KERNEL
:
1015 struct ltt_kernel_channel
*kchan
;
1016 struct ltt_kernel_session
*ksess
;
1018 ksess
= session
->kernel_session
;
1021 * If a non-default channel has been created in the
1022 * session, explicitely require that -c chan_name needs
1025 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1026 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1030 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1031 if (kchan
== NULL
) {
1032 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1036 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1037 if (ret
!= LTTNG_OK
) {
1041 kernel_wait_quiescent(kernel_tracer_fd
);
1044 case LTTNG_DOMAIN_UST
:
1046 struct ltt_ust_channel
*uchan
;
1047 struct ltt_ust_session
*usess
;
1049 usess
= session
->ust_session
;
1052 * If a non-default channel has been created in the
1053 * session, explicitely require that -c chan_name needs
1056 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1057 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1061 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1063 if (uchan
== NULL
) {
1064 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1068 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1069 if (ret
!= LTTNG_OK
) {
1073 DBG3("Disable UST event %s in channel %s completed", event_name
,
1077 case LTTNG_DOMAIN_JUL
:
1079 struct ltt_ust_session
*usess
= session
->ust_session
;
1083 ret
= event_jul_disable(usess
, event_name
);
1084 if (ret
!= LTTNG_OK
) {
1091 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1092 case LTTNG_DOMAIN_UST_PID
:
1093 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1096 ret
= LTTNG_ERR_UND
;
1108 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
1110 int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
1118 case LTTNG_DOMAIN_KERNEL
:
1120 struct ltt_kernel_session
*ksess
;
1121 struct ltt_kernel_channel
*kchan
;
1123 ksess
= session
->kernel_session
;
1126 * If a non-default channel has been created in the
1127 * session, explicitely require that -c chan_name needs
1130 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1131 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1135 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1136 if (kchan
== NULL
) {
1137 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1141 ret
= event_kernel_disable_all(kchan
);
1142 if (ret
!= LTTNG_OK
) {
1146 kernel_wait_quiescent(kernel_tracer_fd
);
1149 case LTTNG_DOMAIN_UST
:
1151 struct ltt_ust_session
*usess
;
1152 struct ltt_ust_channel
*uchan
;
1154 usess
= session
->ust_session
;
1157 * If a non-default channel has been created in the
1158 * session, explicitely require that -c chan_name needs
1161 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1162 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1166 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1168 if (uchan
== NULL
) {
1169 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1173 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1178 DBG3("Disable all UST events in channel %s completed", channel_name
);
1182 case LTTNG_DOMAIN_JUL
:
1184 struct ltt_ust_session
*usess
= session
->ust_session
;
1188 ret
= event_jul_disable_all(usess
);
1189 if (ret
!= LTTNG_OK
) {
1196 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1197 case LTTNG_DOMAIN_UST_PID
:
1198 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1201 ret
= LTTNG_ERR_UND
;
1213 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1215 int cmd_add_context(struct ltt_session
*session
, int domain
,
1216 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1218 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1221 case LTTNG_DOMAIN_KERNEL
:
1222 assert(session
->kernel_session
);
1224 if (session
->kernel_session
->channel_count
== 0) {
1225 /* Create default channel */
1226 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1227 if (ret
!= LTTNG_OK
) {
1230 chan_kern_created
= 1;
1232 /* Add kernel context to kernel tracer */
1233 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1234 if (ret
!= LTTNG_OK
) {
1238 case LTTNG_DOMAIN_UST
:
1240 struct ltt_ust_session
*usess
= session
->ust_session
;
1241 unsigned int chan_count
;
1245 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1246 if (chan_count
== 0) {
1247 struct lttng_channel
*attr
;
1248 /* Create default channel */
1249 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1251 ret
= LTTNG_ERR_FATAL
;
1255 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1256 if (ret
!= LTTNG_OK
) {
1261 chan_ust_created
= 1;
1264 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1265 if (ret
!= LTTNG_OK
) {
1271 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1272 case LTTNG_DOMAIN_UST_PID
:
1273 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1276 ret
= LTTNG_ERR_UND
;
1283 if (chan_kern_created
) {
1284 struct ltt_kernel_channel
*kchan
=
1285 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1286 session
->kernel_session
);
1287 /* Created previously, this should NOT fail. */
1289 kernel_destroy_channel(kchan
);
1292 if (chan_ust_created
) {
1293 struct ltt_ust_channel
*uchan
=
1294 trace_ust_find_channel_by_name(
1295 session
->ust_session
->domain_global
.channels
,
1296 DEFAULT_CHANNEL_NAME
);
1297 /* Created previously, this should NOT fail. */
1299 /* Remove from the channel list of the session. */
1300 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1302 trace_ust_destroy_channel(uchan
);
1308 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1310 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1311 char *channel_name
, struct lttng_event
*event
,
1312 struct lttng_filter_bytecode
*filter
,
1313 struct lttng_event_exclusion
*exclusion
,
1316 int ret
, channel_created
= 0;
1317 struct lttng_channel
*attr
;
1321 assert(channel_name
);
1325 switch (domain
->type
) {
1326 case LTTNG_DOMAIN_KERNEL
:
1328 struct ltt_kernel_channel
*kchan
;
1331 * If a non-default channel has been created in the
1332 * session, explicitely require that -c chan_name needs
1335 if (session
->kernel_session
->has_non_default_channel
1336 && channel_name
[0] == '\0') {
1337 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1341 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1342 session
->kernel_session
);
1343 if (kchan
== NULL
) {
1344 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1345 LTTNG_BUFFER_GLOBAL
);
1347 ret
= LTTNG_ERR_FATAL
;
1350 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1352 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1353 if (ret
!= LTTNG_OK
) {
1359 channel_created
= 1;
1362 /* Get the newly created kernel channel pointer */
1363 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1364 session
->kernel_session
);
1365 if (kchan
== NULL
) {
1366 /* This sould not happen... */
1367 ret
= LTTNG_ERR_FATAL
;
1371 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1372 if (ret
!= LTTNG_OK
) {
1373 if (channel_created
) {
1374 /* Let's not leak a useless channel. */
1375 kernel_destroy_channel(kchan
);
1380 kernel_wait_quiescent(kernel_tracer_fd
);
1383 case LTTNG_DOMAIN_UST
:
1385 struct ltt_ust_channel
*uchan
;
1386 struct ltt_ust_session
*usess
= session
->ust_session
;
1391 * If a non-default channel has been created in the
1392 * session, explicitely require that -c chan_name needs
1395 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1396 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1400 /* Get channel from global UST domain */
1401 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1403 if (uchan
== NULL
) {
1404 /* Create default channel */
1405 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1406 usess
->buffer_type
);
1408 ret
= LTTNG_ERR_FATAL
;
1411 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1413 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1414 if (ret
!= LTTNG_OK
) {
1420 /* Get the newly created channel reference back */
1421 uchan
= trace_ust_find_channel_by_name(
1422 usess
->domain_global
.channels
, channel_name
);
1426 /* At this point, the session and channel exist on the tracer */
1427 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
, filter
, exclusion
);
1428 if (ret
!= LTTNG_OK
) {
1433 case LTTNG_DOMAIN_JUL
:
1435 struct lttng_event uevent
;
1436 struct lttng_domain tmp_dom
;
1437 struct ltt_ust_session
*usess
= session
->ust_session
;
1441 /* Create the default JUL tracepoint. */
1442 memset(&uevent
, 0, sizeof(uevent
));
1443 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1444 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1445 strncpy(uevent
.name
, DEFAULT_JUL_EVENT_NAME
, sizeof(uevent
.name
));
1446 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1449 * The domain type is changed because we are about to enable the
1450 * default channel and event for the JUL domain that are hardcoded.
1451 * This happens in the UST domain.
1453 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1454 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1456 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1457 &uevent
, NULL
, NULL
, wpipe
);
1458 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1462 /* The wild card * means that everything should be enabled. */
1463 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1464 ret
= event_jul_enable_all(usess
, event
);
1466 ret
= event_jul_enable(usess
, event
);
1468 if (ret
!= LTTNG_OK
) {
1475 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1476 case LTTNG_DOMAIN_UST_PID
:
1477 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1480 ret
= LTTNG_ERR_UND
;
1492 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1494 int cmd_enable_event_all(struct ltt_session
*session
,
1495 struct lttng_domain
*domain
, char *channel_name
, int event_type
,
1496 struct lttng_filter_bytecode
*filter
, int wpipe
)
1499 struct lttng_channel
*attr
;
1502 assert(channel_name
);
1506 switch (domain
->type
) {
1507 case LTTNG_DOMAIN_KERNEL
:
1509 struct ltt_kernel_channel
*kchan
;
1511 assert(session
->kernel_session
);
1514 * If a non-default channel has been created in the
1515 * session, explicitely require that -c chan_name needs
1518 if (session
->kernel_session
->has_non_default_channel
1519 && channel_name
[0] == '\0') {
1520 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1524 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1525 session
->kernel_session
);
1526 if (kchan
== NULL
) {
1527 /* Create default channel */
1528 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1529 LTTNG_BUFFER_GLOBAL
);
1531 ret
= LTTNG_ERR_FATAL
;
1534 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1536 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1537 if (ret
!= LTTNG_OK
) {
1543 /* Get the newly created kernel channel pointer */
1544 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1545 session
->kernel_session
);
1549 switch (event_type
) {
1550 case LTTNG_EVENT_SYSCALL
:
1551 ret
= event_kernel_enable_all_syscalls(kchan
, kernel_tracer_fd
);
1553 case LTTNG_EVENT_TRACEPOINT
:
1555 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1556 * events already registered to the channel.
1558 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1560 case LTTNG_EVENT_ALL
:
1561 /* Enable syscalls and tracepoints */
1562 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1565 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1569 /* Manage return value */
1570 if (ret
!= LTTNG_OK
) {
1572 * On error, cmd_enable_channel call will take care of destroying
1573 * the created channel if it was needed.
1578 kernel_wait_quiescent(kernel_tracer_fd
);
1581 case LTTNG_DOMAIN_UST
:
1583 struct ltt_ust_channel
*uchan
;
1584 struct ltt_ust_session
*usess
= session
->ust_session
;
1589 * If a non-default channel has been created in the
1590 * session, explicitely require that -c chan_name needs
1593 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1594 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1598 /* Get channel from global UST domain */
1599 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1601 if (uchan
== NULL
) {
1602 /* Create default channel */
1603 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1604 usess
->buffer_type
);
1606 ret
= LTTNG_ERR_FATAL
;
1609 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1611 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1612 if (ret
!= LTTNG_OK
) {
1618 /* Get the newly created channel reference back */
1619 uchan
= trace_ust_find_channel_by_name(
1620 usess
->domain_global
.channels
, channel_name
);
1624 /* At this point, the session and channel exist on the tracer */
1626 switch (event_type
) {
1627 case LTTNG_EVENT_ALL
:
1628 case LTTNG_EVENT_TRACEPOINT
:
1629 ret
= event_ust_enable_all_tracepoints(usess
, uchan
, filter
);
1630 if (ret
!= LTTNG_OK
) {
1635 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
1639 /* Manage return value */
1640 if (ret
!= LTTNG_OK
) {
1646 case LTTNG_DOMAIN_JUL
:
1648 struct lttng_event uevent
, event
;
1649 struct lttng_domain tmp_dom
;
1650 struct ltt_ust_session
*usess
= session
->ust_session
;
1654 /* Create the default JUL tracepoint. */
1655 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1656 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1657 strncpy(uevent
.name
, DEFAULT_JUL_EVENT_NAME
, sizeof(uevent
.name
));
1658 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1661 * The domain type is changed because we are about to enable the
1662 * default channel and event for the JUL domain that are hardcoded.
1663 * This happens in the UST domain.
1665 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1666 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1668 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1669 &uevent
, NULL
, NULL
, wpipe
);
1670 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1674 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1675 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1676 strncpy(event
.name
, "*", sizeof(event
.name
));
1677 event
.name
[sizeof(event
.name
) - 1] = '\0';
1679 ret
= event_jul_enable_all(usess
, &event
);
1680 if (ret
!= LTTNG_OK
) {
1687 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1688 case LTTNG_DOMAIN_UST_PID
:
1689 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1692 ret
= LTTNG_ERR_UND
;
1705 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1707 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1710 ssize_t nb_events
= 0;
1713 case LTTNG_DOMAIN_KERNEL
:
1714 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1715 if (nb_events
< 0) {
1716 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1720 case LTTNG_DOMAIN_UST
:
1721 nb_events
= ust_app_list_events(events
);
1722 if (nb_events
< 0) {
1723 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1727 case LTTNG_DOMAIN_JUL
:
1728 nb_events
= jul_list_events(events
);
1729 if (nb_events
< 0) {
1730 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1735 ret
= LTTNG_ERR_UND
;
1742 /* Return negative value to differentiate return code */
1747 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1749 ssize_t
cmd_list_tracepoint_fields(int domain
,
1750 struct lttng_event_field
**fields
)
1753 ssize_t nb_fields
= 0;
1756 case LTTNG_DOMAIN_UST
:
1757 nb_fields
= ust_app_list_event_fields(fields
);
1758 if (nb_fields
< 0) {
1759 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1763 case LTTNG_DOMAIN_KERNEL
:
1764 default: /* fall-through */
1765 ret
= LTTNG_ERR_UND
;
1772 /* Return negative value to differentiate return code */
1777 * Command LTTNG_START_TRACE processed by the client thread.
1779 int cmd_start_trace(struct ltt_session
*session
)
1782 struct ltt_kernel_session
*ksession
;
1783 struct ltt_ust_session
*usess
;
1787 /* Ease our life a bit ;) */
1788 ksession
= session
->kernel_session
;
1789 usess
= session
->ust_session
;
1791 if (session
->enabled
) {
1792 /* Already started. */
1793 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1797 session
->enabled
= 1;
1799 /* Kernel tracing */
1800 if (ksession
!= NULL
) {
1801 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1802 if (ret
!= LTTNG_OK
) {
1807 /* Flag session that trace should start automatically */
1809 usess
->start_trace
= 1;
1811 ret
= ust_app_start_trace_all(usess
);
1813 ret
= LTTNG_ERR_UST_START_FAIL
;
1818 session
->started
= 1;
1827 * Command LTTNG_STOP_TRACE processed by the client thread.
1829 int cmd_stop_trace(struct ltt_session
*session
)
1832 struct ltt_kernel_channel
*kchan
;
1833 struct ltt_kernel_session
*ksession
;
1834 struct ltt_ust_session
*usess
;
1839 ksession
= session
->kernel_session
;
1840 usess
= session
->ust_session
;
1842 if (!session
->enabled
) {
1843 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1847 session
->enabled
= 0;
1850 if (ksession
&& ksession
->started
) {
1851 DBG("Stop kernel tracing");
1853 /* Flush metadata if exist */
1854 if (ksession
->metadata_stream_fd
>= 0) {
1855 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1857 ERR("Kernel metadata flush failed");
1861 /* Flush all buffers before stopping */
1862 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1863 ret
= kernel_flush_buffer(kchan
);
1865 ERR("Kernel flush buffer error");
1869 ret
= kernel_stop_session(ksession
);
1871 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1875 kernel_wait_quiescent(kernel_tracer_fd
);
1877 ksession
->started
= 0;
1880 if (usess
&& usess
->start_trace
) {
1881 usess
->start_trace
= 0;
1883 ret
= ust_app_stop_trace_all(usess
);
1885 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1897 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1899 int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
1900 size_t nb_uri
, struct lttng_uri
*uris
)
1903 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1904 struct ltt_ust_session
*usess
= session
->ust_session
;
1905 struct consumer_output
*consumer
= NULL
;
1911 /* Can't enable consumer after session started. */
1912 if (session
->enabled
) {
1913 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1918 * This case switch makes sure the domain session has a temporary consumer
1919 * so the URL can be set.
1923 /* Code flow error. A session MUST always have a consumer object */
1924 assert(session
->consumer
);
1926 * The URL will be added to the tracing session consumer instead of a
1927 * specific domain consumer.
1929 consumer
= session
->consumer
;
1931 case LTTNG_DOMAIN_KERNEL
:
1932 /* Code flow error if we don't have a kernel session here. */
1934 assert(ksess
->consumer
);
1935 consumer
= ksess
->consumer
;
1937 case LTTNG_DOMAIN_UST
:
1938 /* Code flow error if we don't have a kernel session here. */
1940 assert(usess
->consumer
);
1941 consumer
= usess
->consumer
;
1945 for (i
= 0; i
< nb_uri
; i
++) {
1946 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
1947 if (ret
!= LTTNG_OK
) {
1960 * Command LTTNG_CREATE_SESSION processed by the client thread.
1962 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
1963 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
1966 struct ltt_session
*session
;
1972 * Verify if the session already exist
1974 * XXX: There is no need for the session lock list here since the caller
1975 * (process_client_msg) is holding it. We might want to change that so a
1976 * single command does not lock the entire session list.
1978 session
= session_find_by_name(name
);
1979 if (session
!= NULL
) {
1980 ret
= LTTNG_ERR_EXIST_SESS
;
1984 /* Create tracing session in the registry */
1985 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
1986 LTTNG_SOCK_GET_GID_CRED(creds
));
1987 if (ret
!= LTTNG_OK
) {
1992 * Get the newly created session pointer back
1994 * XXX: There is no need for the session lock list here since the caller
1995 * (process_client_msg) is holding it. We might want to change that so a
1996 * single command does not lock the entire session list.
1998 session
= session_find_by_name(name
);
2001 session
->live_timer
= live_timer
;
2002 /* Create default consumer output for the session not yet created. */
2003 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2004 if (session
->consumer
== NULL
) {
2005 ret
= LTTNG_ERR_FATAL
;
2006 goto consumer_error
;
2010 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
2011 if (ret
!= LTTNG_OK
) {
2012 goto consumer_error
;
2014 session
->output_traces
= 1;
2016 session
->output_traces
= 0;
2017 DBG2("Session %s created with no output", session
->name
);
2020 session
->consumer
->enabled
= 1;
2025 session_destroy(session
);
2032 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2034 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2035 size_t nb_uri
, lttng_sock_cred
*creds
)
2038 struct ltt_session
*session
;
2039 struct snapshot_output
*new_output
= NULL
;
2045 * Create session in no output mode with URIs set to NULL. The uris we've
2046 * received are for a default snapshot output if one.
2048 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2049 if (ret
!= LTTNG_OK
) {
2053 /* Get the newly created session pointer back. This should NEVER fail. */
2054 session
= session_find_by_name(name
);
2057 /* Flag session for snapshot mode. */
2058 session
->snapshot_mode
= 1;
2060 /* Skip snapshot output creation if no URI is given. */
2065 new_output
= snapshot_output_alloc();
2067 ret
= LTTNG_ERR_NOMEM
;
2068 goto error_snapshot_alloc
;
2071 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2072 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2074 if (ret
== -ENOMEM
) {
2075 ret
= LTTNG_ERR_NOMEM
;
2077 ret
= LTTNG_ERR_INVALID
;
2079 goto error_snapshot
;
2083 snapshot_add_output(&session
->snapshot
, new_output
);
2090 snapshot_output_destroy(new_output
);
2091 error_snapshot_alloc
:
2092 session_destroy(session
);
2098 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2100 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2103 struct ltt_ust_session
*usess
;
2104 struct ltt_kernel_session
*ksess
;
2109 usess
= session
->ust_session
;
2110 ksess
= session
->kernel_session
;
2112 /* Clean kernel session teardown */
2113 kernel_destroy_session(ksess
);
2115 /* UST session teardown */
2117 /* Close any relayd session */
2118 consumer_output_send_destroy_relayd(usess
->consumer
);
2120 /* Destroy every UST application related to this session. */
2121 ret
= ust_app_destroy_trace_all(usess
);
2123 ERR("Error in ust_app_destroy_trace_all");
2126 /* Clean up the rest. */
2127 trace_ust_destroy_session(usess
);
2131 * Must notify the kernel thread here to update it's poll set in order to
2132 * remove the channel(s)' fd just destroyed.
2134 ret
= notify_thread_pipe(wpipe
);
2136 PERROR("write kernel poll pipe");
2139 ret
= session_destroy(session
);
2145 * Command LTTNG_CALIBRATE processed by the client thread.
2147 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2152 case LTTNG_DOMAIN_KERNEL
:
2154 struct lttng_kernel_calibrate kcalibrate
;
2156 switch (calibrate
->type
) {
2157 case LTTNG_CALIBRATE_FUNCTION
:
2159 /* Default and only possible calibrate option. */
2160 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2164 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2166 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2171 case LTTNG_DOMAIN_UST
:
2173 struct lttng_ust_calibrate ucalibrate
;
2175 switch (calibrate
->type
) {
2176 case LTTNG_CALIBRATE_FUNCTION
:
2178 /* Default and only possible calibrate option. */
2179 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2183 ret
= ust_app_calibrate_glb(&ucalibrate
);
2185 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2191 ret
= LTTNG_ERR_UND
;
2202 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2204 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2205 const char *sock_path
, struct consumer_data
*cdata
)
2208 struct consumer_socket
*socket
= NULL
;
2215 case LTTNG_DOMAIN_KERNEL
:
2217 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2221 /* Can't register a consumer if there is already one */
2222 if (ksess
->consumer_fds_sent
!= 0) {
2223 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2227 sock
= lttcomm_connect_unix_sock(sock_path
);
2229 ret
= LTTNG_ERR_CONNECT_FAIL
;
2232 cdata
->cmd_sock
= sock
;
2234 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2235 if (socket
== NULL
) {
2238 PERROR("close register consumer");
2240 cdata
->cmd_sock
= -1;
2241 ret
= LTTNG_ERR_FATAL
;
2245 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2246 if (socket
->lock
== NULL
) {
2247 PERROR("zmalloc pthread mutex");
2248 ret
= LTTNG_ERR_FATAL
;
2251 pthread_mutex_init(socket
->lock
, NULL
);
2252 socket
->registered
= 1;
2255 consumer_add_socket(socket
, ksess
->consumer
);
2258 pthread_mutex_lock(&cdata
->pid_mutex
);
2260 pthread_mutex_unlock(&cdata
->pid_mutex
);
2265 /* TODO: Userspace tracing */
2266 ret
= LTTNG_ERR_UND
;
2274 consumer_destroy_socket(socket
);
2280 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2282 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2283 struct lttng_domain
**domains
)
2288 if (session
->kernel_session
!= NULL
) {
2289 DBG3("Listing domains found kernel domain");
2293 if (session
->ust_session
!= NULL
) {
2294 DBG3("Listing domains found UST global domain");
2297 if (session
->ust_session
->domain_jul
.being_used
) {
2302 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2303 if (*domains
== NULL
) {
2304 ret
= LTTNG_ERR_FATAL
;
2308 if (session
->kernel_session
!= NULL
) {
2309 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2313 if (session
->ust_session
!= NULL
) {
2314 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2315 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2318 if (session
->ust_session
->domain_jul
.being_used
) {
2319 (*domains
)[index
].type
= LTTNG_DOMAIN_JUL
;
2320 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2328 /* Return negative value to differentiate return code */
2334 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2336 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2337 struct lttng_channel
**channels
)
2340 ssize_t nb_chan
= 0;
2343 case LTTNG_DOMAIN_KERNEL
:
2344 if (session
->kernel_session
!= NULL
) {
2345 nb_chan
= session
->kernel_session
->channel_count
;
2347 DBG3("Number of kernel channels %zd", nb_chan
);
2349 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2352 case LTTNG_DOMAIN_UST
:
2353 if (session
->ust_session
!= NULL
) {
2354 nb_chan
= lttng_ht_get_count(
2355 session
->ust_session
->domain_global
.channels
);
2357 DBG3("Number of UST global channels %zd", nb_chan
);
2359 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2364 ret
= LTTNG_ERR_UND
;
2369 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2370 if (*channels
== NULL
) {
2371 ret
= LTTNG_ERR_FATAL
;
2375 list_lttng_channels(domain
, session
, *channels
);
2378 /* Ret value was set in the domain switch case */
2385 /* Return negative value to differentiate return code */
2390 * Command LTTNG_LIST_EVENTS processed by the client thread.
2392 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2393 char *channel_name
, struct lttng_event
**events
)
2396 ssize_t nb_event
= 0;
2399 case LTTNG_DOMAIN_KERNEL
:
2400 if (session
->kernel_session
!= NULL
) {
2401 nb_event
= list_lttng_kernel_events(channel_name
,
2402 session
->kernel_session
, events
);
2405 case LTTNG_DOMAIN_UST
:
2407 if (session
->ust_session
!= NULL
) {
2408 nb_event
= list_lttng_ust_global_events(channel_name
,
2409 &session
->ust_session
->domain_global
, events
);
2413 case LTTNG_DOMAIN_JUL
:
2414 if (session
->ust_session
) {
2415 nb_event
= list_lttng_jul_events(
2416 &session
->ust_session
->domain_jul
, events
);
2420 ret
= LTTNG_ERR_UND
;
2427 /* Return negative value to differentiate return code */
2432 * Using the session list, filled a lttng_session array to send back to the
2433 * client for session listing.
2435 * The session list lock MUST be acquired before calling this function. Use
2436 * session_lock_list() and session_unlock_list().
2438 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2443 struct ltt_session
*session
;
2444 struct ltt_session_list
*list
= session_get_list();
2446 DBG("Getting all available session for UID %d GID %d",
2449 * Iterate over session list and append data after the control struct in
2452 cds_list_for_each_entry(session
, &list
->head
, list
) {
2454 * Only list the sessions the user can control.
2456 if (!session_access_ok(session
, uid
, gid
)) {
2460 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2461 struct ltt_ust_session
*usess
= session
->ust_session
;
2463 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2464 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2465 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2466 ret
= build_network_session_path(sessions
[i
].path
,
2467 sizeof(sessions
[i
].path
), session
);
2469 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2470 session
->consumer
->dst
.trace_path
);
2473 PERROR("snprintf session path");
2477 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2478 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2479 sessions
[i
].enabled
= session
->enabled
;
2480 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2486 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2487 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2489 int cmd_data_pending(struct ltt_session
*session
)
2492 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2493 struct ltt_ust_session
*usess
= session
->ust_session
;
2497 /* Session MUST be stopped to ask for data availability. */
2498 if (session
->enabled
) {
2499 ret
= LTTNG_ERR_SESSION_STARTED
;
2503 if (ksess
&& ksess
->consumer
) {
2504 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2506 /* Data is still being extracted for the kernel. */
2511 if (usess
&& usess
->consumer
) {
2512 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2514 /* Data is still being extracted for the kernel. */
2519 /* Data is ready to be read by a viewer */
2527 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2529 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2531 int cmd_snapshot_add_output(struct ltt_session
*session
,
2532 struct lttng_snapshot_output
*output
, uint32_t *id
)
2535 struct snapshot_output
*new_output
;
2540 DBG("Cmd snapshot add output for session %s", session
->name
);
2543 * Permission denied to create an output if the session is not
2544 * set in no output mode.
2546 if (session
->output_traces
) {
2547 ret
= LTTNG_ERR_EPERM
;
2551 /* Only one output is allowed until we have the "tee" feature. */
2552 if (session
->snapshot
.nb_output
== 1) {
2553 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2557 new_output
= snapshot_output_alloc();
2559 ret
= LTTNG_ERR_NOMEM
;
2563 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2564 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2565 &session
->snapshot
);
2567 if (ret
== -ENOMEM
) {
2568 ret
= LTTNG_ERR_NOMEM
;
2570 ret
= LTTNG_ERR_INVALID
;
2576 snapshot_add_output(&session
->snapshot
, new_output
);
2578 *id
= new_output
->id
;
2585 snapshot_output_destroy(new_output
);
2591 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2593 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2595 int cmd_snapshot_del_output(struct ltt_session
*session
,
2596 struct lttng_snapshot_output
*output
)
2599 struct snapshot_output
*sout
= NULL
;
2607 * Permission denied to create an output if the session is not
2608 * set in no output mode.
2610 if (session
->output_traces
) {
2611 ret
= LTTNG_ERR_EPERM
;
2616 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2618 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2619 } else if (*output
->name
!= '\0') {
2620 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2622 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2625 ret
= LTTNG_ERR_INVALID
;
2629 snapshot_delete_output(&session
->snapshot
, sout
);
2630 snapshot_output_destroy(sout
);
2639 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2641 * If no output is available, outputs is untouched and 0 is returned.
2643 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2645 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2646 struct lttng_snapshot_output
**outputs
)
2649 struct lttng_snapshot_output
*list
;
2650 struct lttng_ht_iter iter
;
2651 struct snapshot_output
*output
;
2656 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2659 * Permission denied to create an output if the session is not
2660 * set in no output mode.
2662 if (session
->output_traces
) {
2663 ret
= LTTNG_ERR_EPERM
;
2667 if (session
->snapshot
.nb_output
== 0) {
2672 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2674 ret
= LTTNG_ERR_NOMEM
;
2678 /* Copy list from session to the new list object. */
2679 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2680 output
, node
.node
) {
2681 assert(output
->consumer
);
2682 list
[idx
].id
= output
->id
;
2683 list
[idx
].max_size
= output
->max_size
;
2684 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2685 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2686 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2687 sizeof(list
[idx
].ctrl_url
));
2690 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2691 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2693 ret
= LTTNG_ERR_NOMEM
;
2698 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2699 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2701 ret
= LTTNG_ERR_NOMEM
;
2709 return session
->snapshot
.nb_output
;
2718 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2719 * snapshot output is *not* set with a remote destination.
2721 * Return 0 on success or a LTTNG_ERR code.
2723 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2724 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2727 struct lttng_ht_iter iter
;
2728 struct consumer_socket
*socket
;
2731 assert(snap_output
);
2734 DBG2("Set relayd object from snapshot output");
2736 /* Ignore if snapshot consumer output is not network. */
2737 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2742 * For each consumer socket, create and send the relayd object of the
2746 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2747 socket
, node
.node
) {
2748 ret
= send_consumer_relayd_sockets(0, session
->id
,
2749 snap_output
->consumer
, socket
,
2750 session
->name
, session
->hostname
,
2751 session
->live_timer
);
2752 if (ret
!= LTTNG_OK
) {
2764 * Record a kernel snapshot.
2766 * Return LTTNG_OK on success or a LTTNG_ERR code.
2768 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2769 struct snapshot_output
*output
, struct ltt_session
*session
,
2770 int wait
, int nb_streams
)
2778 /* Get the datetime for the snapshot output directory. */
2779 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2780 sizeof(output
->datetime
));
2782 ret
= LTTNG_ERR_INVALID
;
2787 * Copy kernel session sockets so we can communicate with the right
2788 * consumer for the snapshot record command.
2790 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2792 ret
= LTTNG_ERR_NOMEM
;
2796 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2797 if (ret
!= LTTNG_OK
) {
2798 goto error_snapshot
;
2801 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_streams
);
2802 if (ret
!= LTTNG_OK
) {
2803 goto error_snapshot
;
2809 /* Clean up copied sockets so this output can use some other later on. */
2810 consumer_destroy_output_sockets(output
->consumer
);
2816 * Record a UST snapshot.
2818 * Return 0 on success or a LTTNG_ERR error code.
2820 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2821 struct snapshot_output
*output
, struct ltt_session
*session
,
2822 int wait
, int nb_streams
)
2830 /* Get the datetime for the snapshot output directory. */
2831 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2832 sizeof(output
->datetime
));
2834 ret
= LTTNG_ERR_INVALID
;
2839 * Copy UST session sockets so we can communicate with the right
2840 * consumer for the snapshot record command.
2842 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
2844 ret
= LTTNG_ERR_NOMEM
;
2848 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
2849 if (ret
!= LTTNG_OK
) {
2850 goto error_snapshot
;
2853 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_streams
);
2857 ret
= LTTNG_ERR_INVALID
;
2860 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
2863 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
2866 goto error_snapshot
;
2872 /* Clean up copied sockets so this output can use some other later on. */
2873 consumer_destroy_output_sockets(output
->consumer
);
2879 * Returns the total number of streams for a session or a negative value
2882 static unsigned int get_total_nb_stream(struct ltt_session
*session
)
2884 unsigned int total_streams
= 0;
2886 if (session
->kernel_session
) {
2887 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2889 total_streams
+= ksess
->stream_count_global
;
2892 if (session
->ust_session
) {
2893 struct ltt_ust_session
*usess
= session
->ust_session
;
2895 total_streams
+= ust_app_get_nb_stream(usess
);
2898 return total_streams
;
2902 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
2904 * The wait parameter is ignored so this call always wait for the snapshot to
2905 * complete before returning.
2907 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2909 int cmd_snapshot_record(struct ltt_session
*session
,
2910 struct lttng_snapshot_output
*output
, int wait
)
2913 unsigned int use_tmp_output
= 0;
2914 struct snapshot_output tmp_output
;
2915 unsigned int nb_streams
, snapshot_success
= 0;
2919 DBG("Cmd snapshot record for session %s", session
->name
);
2922 * Permission denied to create an output if the session is not
2923 * set in no output mode.
2925 if (session
->output_traces
) {
2926 ret
= LTTNG_ERR_EPERM
;
2930 /* The session needs to be started at least once. */
2931 if (!session
->started
) {
2932 ret
= LTTNG_ERR_START_SESSION_ONCE
;
2936 /* Use temporary output for the session. */
2937 if (output
&& *output
->ctrl_url
!= '\0') {
2938 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2939 output
->ctrl_url
, output
->data_url
, session
->consumer
,
2942 if (ret
== -ENOMEM
) {
2943 ret
= LTTNG_ERR_NOMEM
;
2945 ret
= LTTNG_ERR_INVALID
;
2949 /* Use the global session count for the temporary snapshot. */
2950 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
2955 * Get the total number of stream of that session which is used by the
2956 * maximum size of the snapshot feature.
2958 nb_streams
= get_total_nb_stream(session
);
2960 if (session
->kernel_session
) {
2961 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2963 if (use_tmp_output
) {
2964 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
2966 if (ret
!= LTTNG_OK
) {
2969 snapshot_success
= 1;
2971 struct snapshot_output
*sout
;
2972 struct lttng_ht_iter iter
;
2975 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
2976 &iter
.iter
, sout
, node
.node
) {
2978 * Make a local copy of the output and assign the possible
2979 * temporary value given by the caller.
2981 memset(&tmp_output
, 0, sizeof(tmp_output
));
2982 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
2984 /* Use temporary max size. */
2985 if (output
->max_size
!= (uint64_t) -1ULL) {
2986 tmp_output
.max_size
= output
->max_size
;
2989 /* Use temporary name. */
2990 if (*output
->name
!= '\0') {
2991 strncpy(tmp_output
.name
, output
->name
,
2992 sizeof(tmp_output
.name
));
2995 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
2997 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
2998 session
, wait
, nb_streams
);
2999 if (ret
!= LTTNG_OK
) {
3003 snapshot_success
= 1;
3009 if (session
->ust_session
) {
3010 struct ltt_ust_session
*usess
= session
->ust_session
;
3012 if (use_tmp_output
) {
3013 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3015 if (ret
!= LTTNG_OK
) {
3018 snapshot_success
= 1;
3020 struct snapshot_output
*sout
;
3021 struct lttng_ht_iter iter
;
3024 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3025 &iter
.iter
, sout
, node
.node
) {
3027 * Make a local copy of the output and assign the possible
3028 * temporary value given by the caller.
3030 memset(&tmp_output
, 0, sizeof(tmp_output
));
3031 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3033 /* Use temporary max size. */
3034 if (output
->max_size
!= (uint64_t) -1ULL) {
3035 tmp_output
.max_size
= output
->max_size
;
3038 /* Use temporary name. */
3039 if (*output
->name
!= '\0') {
3040 strncpy(tmp_output
.name
, output
->name
,
3041 sizeof(tmp_output
.name
));
3044 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3046 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3048 if (ret
!= LTTNG_OK
) {
3052 snapshot_success
= 1;
3058 if (snapshot_success
) {
3059 session
->snapshot
.nb_snapshot
++;
3061 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3069 * Init command subsystem.
3074 * Set network sequence index to 1 for streams to match a relayd
3075 * socket on the consumer side.
3077 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3078 relayd_net_seq_idx
= 1;
3079 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3081 DBG("Command subsystem initialized");