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 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
170 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
171 switch (uchan
->attr
.output
) {
174 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
188 * Create a list of JUL domain events.
190 * Return number of events in list on success or else a negative value.
192 static int list_lttng_jul_events(struct jul_domain
*dom
,
193 struct lttng_event
**events
)
196 unsigned int nb_event
= 0;
197 struct jul_event
*event
;
198 struct lttng_event
*tmp_events
;
199 struct lttng_ht_iter iter
;
204 DBG3("Listing JUL events");
206 nb_event
= lttng_ht_get_count(dom
->events
);
212 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
214 PERROR("zmalloc JUL events session");
215 ret
= -LTTNG_ERR_FATAL
;
220 cds_lfht_for_each_entry(dom
->events
->ht
, &iter
.iter
, event
, node
.node
) {
221 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
222 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
223 tmp_events
[i
].enabled
= event
->enabled
;
228 *events
= tmp_events
;
232 assert(nb_event
== i
);
237 * Create a list of ust global domain events.
239 static int list_lttng_ust_global_events(char *channel_name
,
240 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
243 unsigned int nb_event
= 0;
244 struct lttng_ht_iter iter
;
245 struct lttng_ht_node_str
*node
;
246 struct ltt_ust_channel
*uchan
;
247 struct ltt_ust_event
*uevent
;
248 struct lttng_event
*tmp
;
250 DBG("Listing UST global events for channel %s", channel_name
);
254 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
255 node
= lttng_ht_iter_get_node_str(&iter
);
257 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
261 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
263 nb_event
+= lttng_ht_get_count(uchan
->events
);
270 DBG3("Listing UST global %d events", nb_event
);
272 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
274 ret
= LTTNG_ERR_FATAL
;
278 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
279 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
280 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
281 tmp
[i
].enabled
= uevent
->enabled
;
283 switch (uevent
->attr
.instrumentation
) {
284 case LTTNG_UST_TRACEPOINT
:
285 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
287 case LTTNG_UST_PROBE
:
288 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
290 case LTTNG_UST_FUNCTION
:
291 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
295 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
296 switch (uevent
->attr
.loglevel_type
) {
297 case LTTNG_UST_LOGLEVEL_ALL
:
298 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
300 case LTTNG_UST_LOGLEVEL_RANGE
:
301 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
303 case LTTNG_UST_LOGLEVEL_SINGLE
:
304 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
307 if (uevent
->filter
) {
310 if (uevent
->exclusion
) {
311 tmp
[i
].exclusion
= 1;
325 * Fill lttng_event array of all kernel events in the channel.
327 static int list_lttng_kernel_events(char *channel_name
,
328 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
331 unsigned int nb_event
;
332 struct ltt_kernel_event
*event
;
333 struct ltt_kernel_channel
*kchan
;
335 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
337 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
341 nb_event
= kchan
->event_count
;
343 DBG("Listing events for channel %s", kchan
->channel
->name
);
349 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
350 if (*events
== NULL
) {
351 ret
= LTTNG_ERR_FATAL
;
355 /* Kernel channels */
356 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
357 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
358 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
359 (*events
)[i
].enabled
= event
->enabled
;
361 switch (event
->event
->instrumentation
) {
362 case LTTNG_KERNEL_TRACEPOINT
:
363 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
365 case LTTNG_KERNEL_KRETPROBE
:
366 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
367 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
368 sizeof(struct lttng_kernel_kprobe
));
370 case LTTNG_KERNEL_KPROBE
:
371 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
372 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
373 sizeof(struct lttng_kernel_kprobe
));
375 case LTTNG_KERNEL_FUNCTION
:
376 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
377 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
378 sizeof(struct lttng_kernel_function
));
380 case LTTNG_KERNEL_NOOP
:
381 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
383 case LTTNG_KERNEL_SYSCALL
:
384 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
386 case LTTNG_KERNEL_ALL
:
397 /* Negate the error code to differentiate the size from an error */
402 * Add URI so the consumer output object. Set the correct path depending on the
403 * domain adding the default trace directory.
405 static int add_uri_to_consumer(struct consumer_output
*consumer
,
406 struct lttng_uri
*uri
, int domain
, const char *session_name
)
409 const char *default_trace_dir
;
413 if (consumer
== NULL
) {
414 DBG("No consumer detected. Don't add URI. Stopping.");
415 ret
= LTTNG_ERR_NO_CONSUMER
;
420 case LTTNG_DOMAIN_KERNEL
:
421 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
423 case LTTNG_DOMAIN_UST
:
424 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
428 * This case is possible is we try to add the URI to the global tracing
429 * session consumer object which in this case there is no subdir.
431 default_trace_dir
= "";
434 switch (uri
->dtype
) {
437 DBG2("Setting network URI to consumer");
439 if (consumer
->type
== CONSUMER_DST_NET
) {
440 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
441 consumer
->dst
.net
.control_isset
) ||
442 (uri
->stype
== LTTNG_STREAM_DATA
&&
443 consumer
->dst
.net
.data_isset
)) {
444 ret
= LTTNG_ERR_URL_EXIST
;
448 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
451 consumer
->type
= CONSUMER_DST_NET
;
453 /* Set URI into consumer output object */
454 ret
= consumer_set_network_uri(consumer
, uri
);
458 } else if (ret
== 1) {
460 * URI was the same in the consumer so we do not append the subdir
461 * again so to not duplicate output dir.
467 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
468 ret
= consumer_set_subdir(consumer
, session_name
);
470 ret
= LTTNG_ERR_FATAL
;
475 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
476 /* On a new subdir, reappend the default trace dir. */
477 strncat(consumer
->subdir
, default_trace_dir
,
478 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
479 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
484 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
485 memset(consumer
->dst
.trace_path
, 0,
486 sizeof(consumer
->dst
.trace_path
));
487 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
488 sizeof(consumer
->dst
.trace_path
));
489 /* Append default trace dir */
490 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
491 sizeof(consumer
->dst
.trace_path
) -
492 strlen(consumer
->dst
.trace_path
) - 1);
493 /* Flag consumer as local. */
494 consumer
->type
= CONSUMER_DST_LOCAL
;
505 * Init tracing by creating trace directory and sending fds kernel consumer.
507 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
510 struct lttng_ht_iter iter
;
511 struct consumer_socket
*socket
;
517 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
518 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
520 pthread_mutex_lock(socket
->lock
);
521 ret
= kernel_consumer_send_session(socket
, session
);
522 pthread_mutex_unlock(socket
->lock
);
524 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
536 * Create a socket to the relayd using the URI.
538 * On success, the relayd_sock pointer is set to the created socket.
539 * Else, it's stays untouched and a lttcomm error code is returned.
541 static int create_connect_relayd(struct lttng_uri
*uri
,
542 struct lttcomm_relayd_sock
**relayd_sock
)
545 struct lttcomm_relayd_sock
*rsock
;
547 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
548 RELAYD_VERSION_COMM_MINOR
);
550 ret
= LTTNG_ERR_FATAL
;
555 * Connect to relayd so we can proceed with a session creation. This call
556 * can possibly block for an arbitrary amount of time to set the health
557 * state to be in poll execution.
560 ret
= relayd_connect(rsock
);
563 ERR("Unable to reach lttng-relayd");
564 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
568 /* Create socket for control stream. */
569 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
570 DBG3("Creating relayd stream socket from URI");
572 /* Check relayd version */
573 ret
= relayd_version_check(rsock
);
575 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
578 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
579 DBG3("Creating relayd data socket from URI");
581 /* Command is not valid */
582 ERR("Relayd invalid stream type: %d", uri
->stype
);
583 ret
= LTTNG_ERR_INVALID
;
587 *relayd_sock
= rsock
;
592 /* The returned value is not useful since we are on an error path. */
593 (void) relayd_close(rsock
);
601 * Connect to the relayd using URI and send the socket to the right consumer.
603 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
604 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
605 struct consumer_socket
*consumer_sock
,
606 char *session_name
, char *hostname
, int session_live_timer
)
609 struct lttcomm_relayd_sock
*rsock
= NULL
;
611 /* Connect to relayd and make version check if uri is the control. */
612 ret
= create_connect_relayd(relayd_uri
, &rsock
);
613 if (ret
!= LTTNG_OK
) {
618 /* Set the network sequence index if not set. */
619 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
620 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
622 * Increment net_seq_idx because we are about to transfer the
623 * new relayd socket to the consumer.
624 * Assign unique key so the consumer can match streams.
626 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
627 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
630 /* Send relayd socket to consumer. */
631 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
632 relayd_uri
->stype
, session_id
,
633 session_name
, hostname
, session_live_timer
);
635 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
639 /* Flag that the corresponding socket was sent. */
640 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
641 consumer_sock
->control_sock_sent
= 1;
642 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
643 consumer_sock
->data_sock_sent
= 1;
649 * Close socket which was dup on the consumer side. The session daemon does
650 * NOT keep track of the relayd socket(s) once transfer to the consumer.
654 (void) relayd_close(rsock
);
658 if (ret
!= LTTNG_OK
) {
660 * The consumer output for this session should not be used anymore
661 * since the relayd connection failed thus making any tracing or/and
662 * streaming not usable.
664 consumer
->enabled
= 0;
670 * Send both relayd sockets to a specific consumer and domain. This is a
671 * helper function to facilitate sending the information to the consumer for a
674 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
675 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
676 char *session_name
, char *hostname
, int session_live_timer
)
683 /* Sending control relayd socket. */
684 if (!sock
->control_sock_sent
) {
685 ret
= send_consumer_relayd_socket(domain
, session_id
,
686 &consumer
->dst
.net
.control
, consumer
, sock
,
687 session_name
, hostname
, session_live_timer
);
688 if (ret
!= LTTNG_OK
) {
693 /* Sending data relayd socket. */
694 if (!sock
->data_sock_sent
) {
695 ret
= send_consumer_relayd_socket(domain
, session_id
,
696 &consumer
->dst
.net
.data
, consumer
, sock
,
697 session_name
, hostname
, session_live_timer
);
698 if (ret
!= LTTNG_OK
) {
708 * Setup relayd connections for a tracing session. First creates the socket to
709 * the relayd and send them to the right domain consumer. Consumer type MUST be
712 int cmd_setup_relayd(struct ltt_session
*session
)
715 struct ltt_ust_session
*usess
;
716 struct ltt_kernel_session
*ksess
;
717 struct consumer_socket
*socket
;
718 struct lttng_ht_iter iter
;
722 usess
= session
->ust_session
;
723 ksess
= session
->kernel_session
;
725 DBG("Setting relayd for session %s", session
->name
);
729 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
730 && usess
->consumer
->enabled
) {
731 /* For each consumer socket, send relayd sockets */
732 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
734 pthread_mutex_lock(socket
->lock
);
735 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
736 usess
->consumer
, socket
,
737 session
->name
, session
->hostname
,
738 session
->live_timer
);
739 pthread_mutex_unlock(socket
->lock
);
740 if (ret
!= LTTNG_OK
) {
743 /* Session is now ready for network streaming. */
744 session
->net_handle
= 1;
748 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
749 && ksess
->consumer
->enabled
) {
750 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
752 pthread_mutex_lock(socket
->lock
);
753 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
754 ksess
->consumer
, socket
,
755 session
->name
, session
->hostname
,
756 session
->live_timer
);
757 pthread_mutex_unlock(socket
->lock
);
758 if (ret
!= LTTNG_OK
) {
761 /* Session is now ready for network streaming. */
762 session
->net_handle
= 1;
772 * Start a kernel session by opening all necessary streams.
774 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
777 struct ltt_kernel_channel
*kchan
;
779 /* Open kernel metadata */
780 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
781 ret
= kernel_open_metadata(ksess
);
783 ret
= LTTNG_ERR_KERN_META_FAIL
;
788 /* Open kernel metadata stream */
789 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
790 ret
= kernel_open_metadata_stream(ksess
);
792 ERR("Kernel create metadata stream failed");
793 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
798 /* For each channel */
799 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
800 if (kchan
->stream_count
== 0) {
801 ret
= kernel_open_channel_stream(kchan
);
803 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
806 /* Update the stream global counter */
807 ksess
->stream_count_global
+= ret
;
811 /* Setup kernel consumer socket and send fds to it */
812 ret
= init_kernel_tracing(ksess
);
814 ret
= LTTNG_ERR_KERN_START_FAIL
;
818 /* This start the kernel tracing */
819 ret
= kernel_start_session(ksess
);
821 ret
= LTTNG_ERR_KERN_START_FAIL
;
825 /* Quiescent wait after starting trace */
826 kernel_wait_quiescent(kernel_tracer_fd
);
837 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
839 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
843 struct ltt_ust_session
*usess
;
845 usess
= session
->ust_session
;
850 case LTTNG_DOMAIN_KERNEL
:
852 ret
= channel_kernel_disable(session
->kernel_session
,
854 if (ret
!= LTTNG_OK
) {
858 kernel_wait_quiescent(kernel_tracer_fd
);
861 case LTTNG_DOMAIN_UST
:
863 struct ltt_ust_channel
*uchan
;
864 struct lttng_ht
*chan_ht
;
866 chan_ht
= usess
->domain_global
.channels
;
868 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
870 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
874 ret
= channel_ust_disable(usess
, uchan
);
875 if (ret
!= LTTNG_OK
) {
881 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
882 case LTTNG_DOMAIN_UST_EXEC_NAME
:
883 case LTTNG_DOMAIN_UST_PID
:
886 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
898 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
900 * The wpipe arguments is used as a notifier for the kernel thread.
902 int cmd_enable_channel(struct ltt_session
*session
,
903 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
906 struct ltt_ust_session
*usess
= session
->ust_session
;
907 struct lttng_ht
*chan_ht
;
913 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
918 * Don't try to enable a channel if the session has been started at
919 * some point in time before. The tracer does not allow it.
921 if (session
->has_been_started
) {
922 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
927 * If the session is a live session, remove the switch timer, the
928 * live timer does the same thing but sends also synchronisation
929 * beacons for inactive streams.
931 if (session
->live_timer
> 0) {
932 attr
->attr
.live_timer_interval
= session
->live_timer
;
933 attr
->attr
.switch_timer_interval
= 0;
937 * The ringbuffer (both in user space and kernel) behave badly in overwrite
938 * mode and with less than 2 subbuf so block it right away and send back an
939 * invalid attribute error.
941 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
942 ret
= LTTNG_ERR_INVALID
;
946 switch (domain
->type
) {
947 case LTTNG_DOMAIN_KERNEL
:
949 struct ltt_kernel_channel
*kchan
;
951 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
952 session
->kernel_session
);
954 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
955 if (attr
->name
[0] != '\0') {
956 session
->kernel_session
->has_non_default_channel
= 1;
959 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
962 if (ret
!= LTTNG_OK
) {
966 kernel_wait_quiescent(kernel_tracer_fd
);
969 case LTTNG_DOMAIN_UST
:
971 struct ltt_ust_channel
*uchan
;
973 chan_ht
= usess
->domain_global
.channels
;
975 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
977 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
978 if (attr
->name
[0] != '\0') {
979 usess
->has_non_default_channel
= 1;
982 ret
= channel_ust_enable(usess
, uchan
);
987 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
998 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1000 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1001 char *channel_name
, char *event_name
)
1008 case LTTNG_DOMAIN_KERNEL
:
1010 struct ltt_kernel_channel
*kchan
;
1011 struct ltt_kernel_session
*ksess
;
1013 ksess
= session
->kernel_session
;
1016 * If a non-default channel has been created in the
1017 * session, explicitely require that -c chan_name needs
1020 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1021 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1025 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1026 if (kchan
== NULL
) {
1027 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1031 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1032 if (ret
!= LTTNG_OK
) {
1036 kernel_wait_quiescent(kernel_tracer_fd
);
1039 case LTTNG_DOMAIN_UST
:
1041 struct ltt_ust_channel
*uchan
;
1042 struct ltt_ust_session
*usess
;
1044 usess
= session
->ust_session
;
1047 * If a non-default channel has been created in the
1048 * session, explicitely require that -c chan_name needs
1051 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1052 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1056 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1058 if (uchan
== NULL
) {
1059 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1063 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1064 if (ret
!= LTTNG_OK
) {
1068 DBG3("Disable UST event %s in channel %s completed", event_name
,
1072 case LTTNG_DOMAIN_JUL
:
1074 struct ltt_ust_session
*usess
= session
->ust_session
;
1078 ret
= event_jul_disable(usess
, event_name
);
1079 if (ret
!= LTTNG_OK
) {
1086 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1087 case LTTNG_DOMAIN_UST_PID
:
1088 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1091 ret
= LTTNG_ERR_UND
;
1103 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
1105 int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
1113 case LTTNG_DOMAIN_KERNEL
:
1115 struct ltt_kernel_session
*ksess
;
1116 struct ltt_kernel_channel
*kchan
;
1118 ksess
= session
->kernel_session
;
1121 * If a non-default channel has been created in the
1122 * session, explicitely require that -c chan_name needs
1125 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1126 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1130 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1131 if (kchan
== NULL
) {
1132 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1136 ret
= event_kernel_disable_all(kchan
);
1137 if (ret
!= LTTNG_OK
) {
1141 kernel_wait_quiescent(kernel_tracer_fd
);
1144 case LTTNG_DOMAIN_UST
:
1146 struct ltt_ust_session
*usess
;
1147 struct ltt_ust_channel
*uchan
;
1149 usess
= session
->ust_session
;
1152 * If a non-default channel has been created in the
1153 * session, explicitely require that -c chan_name needs
1156 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1157 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1161 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1163 if (uchan
== NULL
) {
1164 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1168 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1173 DBG3("Disable all UST events in channel %s completed", channel_name
);
1177 case LTTNG_DOMAIN_JUL
:
1179 struct ltt_ust_session
*usess
= session
->ust_session
;
1183 ret
= event_jul_disable_all(usess
);
1184 if (ret
!= LTTNG_OK
) {
1191 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1192 case LTTNG_DOMAIN_UST_PID
:
1193 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1196 ret
= LTTNG_ERR_UND
;
1208 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1210 int cmd_add_context(struct ltt_session
*session
, int domain
,
1211 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1213 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1216 case LTTNG_DOMAIN_KERNEL
:
1217 assert(session
->kernel_session
);
1219 if (session
->kernel_session
->channel_count
== 0) {
1220 /* Create default channel */
1221 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1222 if (ret
!= LTTNG_OK
) {
1225 chan_kern_created
= 1;
1227 /* Add kernel context to kernel tracer */
1228 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1229 if (ret
!= LTTNG_OK
) {
1233 case LTTNG_DOMAIN_UST
:
1235 struct ltt_ust_session
*usess
= session
->ust_session
;
1236 unsigned int chan_count
;
1240 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1241 if (chan_count
== 0) {
1242 struct lttng_channel
*attr
;
1243 /* Create default channel */
1244 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1246 ret
= LTTNG_ERR_FATAL
;
1250 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1251 if (ret
!= LTTNG_OK
) {
1256 chan_ust_created
= 1;
1259 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1260 if (ret
!= LTTNG_OK
) {
1266 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1267 case LTTNG_DOMAIN_UST_PID
:
1268 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1271 ret
= LTTNG_ERR_UND
;
1278 if (chan_kern_created
) {
1279 struct ltt_kernel_channel
*kchan
=
1280 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1281 session
->kernel_session
);
1282 /* Created previously, this should NOT fail. */
1284 kernel_destroy_channel(kchan
);
1287 if (chan_ust_created
) {
1288 struct ltt_ust_channel
*uchan
=
1289 trace_ust_find_channel_by_name(
1290 session
->ust_session
->domain_global
.channels
,
1291 DEFAULT_CHANNEL_NAME
);
1292 /* Created previously, this should NOT fail. */
1294 /* Remove from the channel list of the session. */
1295 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1297 trace_ust_destroy_channel(uchan
);
1303 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1305 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1306 char *channel_name
, struct lttng_event
*event
,
1307 char *filter_expression
,
1308 struct lttng_filter_bytecode
*filter
,
1309 struct lttng_event_exclusion
*exclusion
,
1312 int ret
, channel_created
= 0;
1313 struct lttng_channel
*attr
;
1317 assert(channel_name
);
1321 switch (domain
->type
) {
1322 case LTTNG_DOMAIN_KERNEL
:
1324 struct ltt_kernel_channel
*kchan
;
1327 * If a non-default channel has been created in the
1328 * session, explicitely require that -c chan_name needs
1331 if (session
->kernel_session
->has_non_default_channel
1332 && channel_name
[0] == '\0') {
1333 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1337 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1338 session
->kernel_session
);
1339 if (kchan
== NULL
) {
1340 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1341 LTTNG_BUFFER_GLOBAL
);
1343 ret
= LTTNG_ERR_FATAL
;
1346 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1348 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1349 if (ret
!= LTTNG_OK
) {
1355 channel_created
= 1;
1358 /* Get the newly created kernel channel pointer */
1359 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1360 session
->kernel_session
);
1361 if (kchan
== NULL
) {
1362 /* This sould not happen... */
1363 ret
= LTTNG_ERR_FATAL
;
1367 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1368 if (ret
!= LTTNG_OK
) {
1369 if (channel_created
) {
1370 /* Let's not leak a useless channel. */
1371 kernel_destroy_channel(kchan
);
1376 kernel_wait_quiescent(kernel_tracer_fd
);
1379 case LTTNG_DOMAIN_UST
:
1381 struct ltt_ust_channel
*uchan
;
1382 struct ltt_ust_session
*usess
= session
->ust_session
;
1387 * If a non-default channel has been created in the
1388 * session, explicitely require that -c chan_name needs
1391 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1392 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1396 /* Get channel from global UST domain */
1397 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1399 if (uchan
== NULL
) {
1400 /* Create default channel */
1401 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1402 usess
->buffer_type
);
1404 ret
= LTTNG_ERR_FATAL
;
1407 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1409 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1410 if (ret
!= LTTNG_OK
) {
1416 /* Get the newly created channel reference back */
1417 uchan
= trace_ust_find_channel_by_name(
1418 usess
->domain_global
.channels
, channel_name
);
1422 /* At this point, the session and channel exist on the tracer */
1423 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1424 filter_expression
, filter
, exclusion
);
1425 if (ret
!= LTTNG_OK
) {
1430 case LTTNG_DOMAIN_JUL
:
1432 struct lttng_event uevent
;
1433 struct lttng_domain tmp_dom
;
1434 struct ltt_ust_session
*usess
= session
->ust_session
;
1438 /* Create the default JUL tracepoint. */
1439 memset(&uevent
, 0, sizeof(uevent
));
1440 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1441 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1443 strncpy(uevent
.name
, DEFAULT_SYS_JUL_EVENT_NAME
,
1444 sizeof(uevent
.name
));
1446 strncpy(uevent
.name
, DEFAULT_USER_JUL_EVENT_NAME
,
1447 sizeof(uevent
.name
));
1449 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1452 * The domain type is changed because we are about to enable the
1453 * default channel and event for the JUL domain that are hardcoded.
1454 * This happens in the UST domain.
1456 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1457 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1459 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1460 &uevent
, filter_expression
, filter
, NULL
, wpipe
);
1461 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1465 /* The wild card * means that everything should be enabled. */
1466 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1467 ret
= event_jul_enable_all(usess
, event
);
1469 ret
= event_jul_enable(usess
, event
);
1471 if (ret
!= LTTNG_OK
) {
1478 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1479 case LTTNG_DOMAIN_UST_PID
:
1480 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1483 ret
= LTTNG_ERR_UND
;
1495 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1497 int cmd_enable_event_all(struct ltt_session
*session
,
1498 struct lttng_domain
*domain
, char *channel_name
, int event_type
,
1499 char *filter_expression
,
1500 struct lttng_filter_bytecode
*filter
, int wpipe
)
1503 struct lttng_channel
*attr
;
1506 assert(channel_name
);
1510 switch (domain
->type
) {
1511 case LTTNG_DOMAIN_KERNEL
:
1513 struct ltt_kernel_channel
*kchan
;
1515 assert(session
->kernel_session
);
1518 * If a non-default channel has been created in the
1519 * session, explicitely require that -c chan_name needs
1522 if (session
->kernel_session
->has_non_default_channel
1523 && channel_name
[0] == '\0') {
1524 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1528 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1529 session
->kernel_session
);
1530 if (kchan
== NULL
) {
1531 /* Create default channel */
1532 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1533 LTTNG_BUFFER_GLOBAL
);
1535 ret
= LTTNG_ERR_FATAL
;
1538 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1540 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1541 if (ret
!= LTTNG_OK
) {
1547 /* Get the newly created kernel channel pointer */
1548 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1549 session
->kernel_session
);
1553 switch (event_type
) {
1554 case LTTNG_EVENT_SYSCALL
:
1555 ret
= event_kernel_enable_all_syscalls(kchan
, kernel_tracer_fd
);
1557 case LTTNG_EVENT_TRACEPOINT
:
1559 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1560 * events already registered to the channel.
1562 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1564 case LTTNG_EVENT_ALL
:
1565 /* Enable syscalls and tracepoints */
1566 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1569 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1573 /* Manage return value */
1574 if (ret
!= LTTNG_OK
) {
1576 * On error, cmd_enable_channel call will take care of destroying
1577 * the created channel if it was needed.
1582 kernel_wait_quiescent(kernel_tracer_fd
);
1585 case LTTNG_DOMAIN_UST
:
1587 struct ltt_ust_channel
*uchan
;
1588 struct ltt_ust_session
*usess
= session
->ust_session
;
1593 * If a non-default channel has been created in the
1594 * session, explicitely require that -c chan_name needs
1597 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1598 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1602 /* Get channel from global UST domain */
1603 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1605 if (uchan
== NULL
) {
1606 /* Create default channel */
1607 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1608 usess
->buffer_type
);
1610 ret
= LTTNG_ERR_FATAL
;
1613 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1615 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1616 if (ret
!= LTTNG_OK
) {
1622 /* Get the newly created channel reference back */
1623 uchan
= trace_ust_find_channel_by_name(
1624 usess
->domain_global
.channels
, channel_name
);
1628 /* At this point, the session and channel exist on the tracer */
1630 switch (event_type
) {
1631 case LTTNG_EVENT_ALL
:
1632 case LTTNG_EVENT_TRACEPOINT
:
1633 ret
= event_ust_enable_all_tracepoints(usess
, uchan
,
1634 filter_expression
, filter
);
1635 if (ret
!= LTTNG_OK
) {
1640 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
1644 /* Manage return value */
1645 if (ret
!= LTTNG_OK
) {
1651 case LTTNG_DOMAIN_JUL
:
1653 struct lttng_event uevent
, event
;
1654 struct lttng_domain tmp_dom
;
1655 struct ltt_ust_session
*usess
= session
->ust_session
;
1659 /* Create the default JUL tracepoint. */
1660 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1661 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1663 strncpy(uevent
.name
, DEFAULT_SYS_JUL_EVENT_NAME
,
1664 sizeof(uevent
.name
));
1666 strncpy(uevent
.name
, DEFAULT_USER_JUL_EVENT_NAME
,
1667 sizeof(uevent
.name
));
1669 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1672 * The domain type is changed because we are about to enable the
1673 * default channel and event for the JUL domain that are hardcoded.
1674 * This happens in the UST domain.
1676 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1677 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1679 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1680 &uevent
, NULL
, NULL
, NULL
, wpipe
);
1681 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1685 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1686 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1687 strncpy(event
.name
, "*", sizeof(event
.name
));
1688 event
.name
[sizeof(event
.name
) - 1] = '\0';
1690 ret
= event_jul_enable_all(usess
, &event
);
1691 if (ret
!= LTTNG_OK
) {
1698 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1699 case LTTNG_DOMAIN_UST_PID
:
1700 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1703 ret
= LTTNG_ERR_UND
;
1716 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1718 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1721 ssize_t nb_events
= 0;
1724 case LTTNG_DOMAIN_KERNEL
:
1725 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1726 if (nb_events
< 0) {
1727 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1731 case LTTNG_DOMAIN_UST
:
1732 nb_events
= ust_app_list_events(events
);
1733 if (nb_events
< 0) {
1734 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1738 case LTTNG_DOMAIN_JUL
:
1739 nb_events
= jul_list_events(events
);
1740 if (nb_events
< 0) {
1741 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1746 ret
= LTTNG_ERR_UND
;
1753 /* Return negative value to differentiate return code */
1758 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1760 ssize_t
cmd_list_tracepoint_fields(int domain
,
1761 struct lttng_event_field
**fields
)
1764 ssize_t nb_fields
= 0;
1767 case LTTNG_DOMAIN_UST
:
1768 nb_fields
= ust_app_list_event_fields(fields
);
1769 if (nb_fields
< 0) {
1770 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1774 case LTTNG_DOMAIN_KERNEL
:
1775 default: /* fall-through */
1776 ret
= LTTNG_ERR_UND
;
1783 /* Return negative value to differentiate return code */
1788 * Command LTTNG_START_TRACE processed by the client thread.
1790 int cmd_start_trace(struct ltt_session
*session
)
1793 unsigned long nb_chan
= 0;
1794 struct ltt_kernel_session
*ksession
;
1795 struct ltt_ust_session
*usess
;
1799 /* Ease our life a bit ;) */
1800 ksession
= session
->kernel_session
;
1801 usess
= session
->ust_session
;
1803 /* Is the session already started? */
1804 if (session
->active
) {
1805 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1810 * Starting a session without channel is useless since after that it's not
1811 * possible to enable channel thus inform the client.
1813 if (usess
&& usess
->domain_global
.channels
) {
1814 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1817 nb_chan
+= ksession
->channel_count
;
1820 ret
= LTTNG_ERR_NO_CHANNEL
;
1824 /* Kernel tracing */
1825 if (ksession
!= NULL
) {
1826 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1827 if (ret
!= LTTNG_OK
) {
1832 /* Flag session that trace should start automatically */
1835 * Even though the start trace might fail, flag this session active so
1836 * other application coming in are started by default.
1840 ret
= ust_app_start_trace_all(usess
);
1842 ret
= LTTNG_ERR_UST_START_FAIL
;
1847 /* Flag this after a successful start. */
1848 session
->has_been_started
= 1;
1849 session
->active
= 1;
1858 * Command LTTNG_STOP_TRACE processed by the client thread.
1860 int cmd_stop_trace(struct ltt_session
*session
)
1863 struct ltt_kernel_channel
*kchan
;
1864 struct ltt_kernel_session
*ksession
;
1865 struct ltt_ust_session
*usess
;
1870 ksession
= session
->kernel_session
;
1871 usess
= session
->ust_session
;
1873 /* Session is not active. Skip everythong and inform the client. */
1874 if (!session
->active
) {
1875 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1880 if (ksession
&& ksession
->active
) {
1881 DBG("Stop kernel tracing");
1883 /* Flush metadata if exist */
1884 if (ksession
->metadata_stream_fd
>= 0) {
1885 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1887 ERR("Kernel metadata flush failed");
1891 /* Flush all buffers before stopping */
1892 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1893 ret
= kernel_flush_buffer(kchan
);
1895 ERR("Kernel flush buffer error");
1899 ret
= kernel_stop_session(ksession
);
1901 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1905 kernel_wait_quiescent(kernel_tracer_fd
);
1907 ksession
->active
= 0;
1910 if (usess
&& usess
->active
) {
1912 * Even though the stop trace might fail, flag this session inactive so
1913 * other application coming in are not started by default.
1917 ret
= ust_app_stop_trace_all(usess
);
1919 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1924 /* Flag inactive after a successful stop. */
1925 session
->active
= 0;
1933 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1935 int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
1936 size_t nb_uri
, struct lttng_uri
*uris
)
1939 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1940 struct ltt_ust_session
*usess
= session
->ust_session
;
1941 struct consumer_output
*consumer
= NULL
;
1947 /* Can't set consumer URI if the session is active. */
1948 if (session
->active
) {
1949 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1954 * This case switch makes sure the domain session has a temporary consumer
1955 * so the URL can be set.
1959 /* Code flow error. A session MUST always have a consumer object */
1960 assert(session
->consumer
);
1962 * The URL will be added to the tracing session consumer instead of a
1963 * specific domain consumer.
1965 consumer
= session
->consumer
;
1967 case LTTNG_DOMAIN_KERNEL
:
1968 /* Code flow error if we don't have a kernel session here. */
1970 assert(ksess
->consumer
);
1971 consumer
= ksess
->consumer
;
1973 case LTTNG_DOMAIN_UST
:
1974 /* Code flow error if we don't have a kernel session here. */
1976 assert(usess
->consumer
);
1977 consumer
= usess
->consumer
;
1981 for (i
= 0; i
< nb_uri
; i
++) {
1982 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
1983 if (ret
!= LTTNG_OK
) {
1996 * Command LTTNG_CREATE_SESSION processed by the client thread.
1998 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
1999 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2002 struct ltt_session
*session
;
2008 * Verify if the session already exist
2010 * XXX: There is no need for the session lock list here since the caller
2011 * (process_client_msg) is holding it. We might want to change that so a
2012 * single command does not lock the entire session list.
2014 session
= session_find_by_name(name
);
2015 if (session
!= NULL
) {
2016 ret
= LTTNG_ERR_EXIST_SESS
;
2020 /* Create tracing session in the registry */
2021 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2022 LTTNG_SOCK_GET_GID_CRED(creds
));
2023 if (ret
!= LTTNG_OK
) {
2028 * Get the newly created session pointer back
2030 * XXX: There is no need for the session lock list here since the caller
2031 * (process_client_msg) is holding it. We might want to change that so a
2032 * single command does not lock the entire session list.
2034 session
= session_find_by_name(name
);
2037 session
->live_timer
= live_timer
;
2038 /* Create default consumer output for the session not yet created. */
2039 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2040 if (session
->consumer
== NULL
) {
2041 ret
= LTTNG_ERR_FATAL
;
2042 goto consumer_error
;
2046 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
2047 if (ret
!= LTTNG_OK
) {
2048 goto consumer_error
;
2050 session
->output_traces
= 1;
2052 session
->output_traces
= 0;
2053 DBG2("Session %s created with no output", session
->name
);
2056 session
->consumer
->enabled
= 1;
2061 session_destroy(session
);
2068 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2070 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2071 size_t nb_uri
, lttng_sock_cred
*creds
)
2074 struct ltt_session
*session
;
2075 struct snapshot_output
*new_output
= NULL
;
2081 * Create session in no output mode with URIs set to NULL. The uris we've
2082 * received are for a default snapshot output if one.
2084 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2085 if (ret
!= LTTNG_OK
) {
2089 /* Get the newly created session pointer back. This should NEVER fail. */
2090 session
= session_find_by_name(name
);
2093 /* Flag session for snapshot mode. */
2094 session
->snapshot_mode
= 1;
2096 /* Skip snapshot output creation if no URI is given. */
2101 new_output
= snapshot_output_alloc();
2103 ret
= LTTNG_ERR_NOMEM
;
2104 goto error_snapshot_alloc
;
2107 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2108 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2110 if (ret
== -ENOMEM
) {
2111 ret
= LTTNG_ERR_NOMEM
;
2113 ret
= LTTNG_ERR_INVALID
;
2115 goto error_snapshot
;
2119 snapshot_add_output(&session
->snapshot
, new_output
);
2126 snapshot_output_destroy(new_output
);
2127 error_snapshot_alloc
:
2128 session_destroy(session
);
2134 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2136 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2139 struct ltt_ust_session
*usess
;
2140 struct ltt_kernel_session
*ksess
;
2145 usess
= session
->ust_session
;
2146 ksess
= session
->kernel_session
;
2148 /* Clean kernel session teardown */
2149 kernel_destroy_session(ksess
);
2151 /* UST session teardown */
2153 /* Close any relayd session */
2154 consumer_output_send_destroy_relayd(usess
->consumer
);
2156 /* Destroy every UST application related to this session. */
2157 ret
= ust_app_destroy_trace_all(usess
);
2159 ERR("Error in ust_app_destroy_trace_all");
2162 /* Clean up the rest. */
2163 trace_ust_destroy_session(usess
);
2167 * Must notify the kernel thread here to update it's poll set in order to
2168 * remove the channel(s)' fd just destroyed.
2170 ret
= notify_thread_pipe(wpipe
);
2172 PERROR("write kernel poll pipe");
2175 ret
= session_destroy(session
);
2181 * Command LTTNG_CALIBRATE processed by the client thread.
2183 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2188 case LTTNG_DOMAIN_KERNEL
:
2190 struct lttng_kernel_calibrate kcalibrate
;
2192 switch (calibrate
->type
) {
2193 case LTTNG_CALIBRATE_FUNCTION
:
2195 /* Default and only possible calibrate option. */
2196 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2200 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2202 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2207 case LTTNG_DOMAIN_UST
:
2209 struct lttng_ust_calibrate ucalibrate
;
2211 switch (calibrate
->type
) {
2212 case LTTNG_CALIBRATE_FUNCTION
:
2214 /* Default and only possible calibrate option. */
2215 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2219 ret
= ust_app_calibrate_glb(&ucalibrate
);
2221 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2227 ret
= LTTNG_ERR_UND
;
2238 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2240 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2241 const char *sock_path
, struct consumer_data
*cdata
)
2244 struct consumer_socket
*socket
= NULL
;
2251 case LTTNG_DOMAIN_KERNEL
:
2253 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2257 /* Can't register a consumer if there is already one */
2258 if (ksess
->consumer_fds_sent
!= 0) {
2259 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2263 sock
= lttcomm_connect_unix_sock(sock_path
);
2265 ret
= LTTNG_ERR_CONNECT_FAIL
;
2268 cdata
->cmd_sock
= sock
;
2270 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2271 if (socket
== NULL
) {
2274 PERROR("close register consumer");
2276 cdata
->cmd_sock
= -1;
2277 ret
= LTTNG_ERR_FATAL
;
2281 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2282 if (socket
->lock
== NULL
) {
2283 PERROR("zmalloc pthread mutex");
2284 ret
= LTTNG_ERR_FATAL
;
2287 pthread_mutex_init(socket
->lock
, NULL
);
2288 socket
->registered
= 1;
2291 consumer_add_socket(socket
, ksess
->consumer
);
2294 pthread_mutex_lock(&cdata
->pid_mutex
);
2296 pthread_mutex_unlock(&cdata
->pid_mutex
);
2301 /* TODO: Userspace tracing */
2302 ret
= LTTNG_ERR_UND
;
2310 consumer_destroy_socket(socket
);
2316 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2318 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2319 struct lttng_domain
**domains
)
2324 if (session
->kernel_session
!= NULL
) {
2325 DBG3("Listing domains found kernel domain");
2329 if (session
->ust_session
!= NULL
) {
2330 DBG3("Listing domains found UST global domain");
2333 if (session
->ust_session
->domain_jul
.being_used
) {
2338 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2339 if (*domains
== NULL
) {
2340 ret
= LTTNG_ERR_FATAL
;
2344 if (session
->kernel_session
!= NULL
) {
2345 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2349 if (session
->ust_session
!= NULL
) {
2350 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2351 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2354 if (session
->ust_session
->domain_jul
.being_used
) {
2355 (*domains
)[index
].type
= LTTNG_DOMAIN_JUL
;
2356 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2364 /* Return negative value to differentiate return code */
2370 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2372 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2373 struct lttng_channel
**channels
)
2376 ssize_t nb_chan
= 0;
2379 case LTTNG_DOMAIN_KERNEL
:
2380 if (session
->kernel_session
!= NULL
) {
2381 nb_chan
= session
->kernel_session
->channel_count
;
2383 DBG3("Number of kernel channels %zd", nb_chan
);
2385 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2388 case LTTNG_DOMAIN_UST
:
2389 if (session
->ust_session
!= NULL
) {
2390 nb_chan
= lttng_ht_get_count(
2391 session
->ust_session
->domain_global
.channels
);
2393 DBG3("Number of UST global channels %zd", nb_chan
);
2395 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2400 ret
= LTTNG_ERR_UND
;
2405 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2406 if (*channels
== NULL
) {
2407 ret
= LTTNG_ERR_FATAL
;
2411 list_lttng_channels(domain
, session
, *channels
);
2414 /* Ret value was set in the domain switch case */
2421 /* Return negative value to differentiate return code */
2426 * Command LTTNG_LIST_EVENTS processed by the client thread.
2428 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2429 char *channel_name
, struct lttng_event
**events
)
2432 ssize_t nb_event
= 0;
2435 case LTTNG_DOMAIN_KERNEL
:
2436 if (session
->kernel_session
!= NULL
) {
2437 nb_event
= list_lttng_kernel_events(channel_name
,
2438 session
->kernel_session
, events
);
2441 case LTTNG_DOMAIN_UST
:
2443 if (session
->ust_session
!= NULL
) {
2444 nb_event
= list_lttng_ust_global_events(channel_name
,
2445 &session
->ust_session
->domain_global
, events
);
2449 case LTTNG_DOMAIN_JUL
:
2450 if (session
->ust_session
) {
2451 nb_event
= list_lttng_jul_events(
2452 &session
->ust_session
->domain_jul
, events
);
2456 ret
= LTTNG_ERR_UND
;
2463 /* Return negative value to differentiate return code */
2468 * Using the session list, filled a lttng_session array to send back to the
2469 * client for session listing.
2471 * The session list lock MUST be acquired before calling this function. Use
2472 * session_lock_list() and session_unlock_list().
2474 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2479 struct ltt_session
*session
;
2480 struct ltt_session_list
*list
= session_get_list();
2482 DBG("Getting all available session for UID %d GID %d",
2485 * Iterate over session list and append data after the control struct in
2488 cds_list_for_each_entry(session
, &list
->head
, list
) {
2490 * Only list the sessions the user can control.
2492 if (!session_access_ok(session
, uid
, gid
)) {
2496 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2497 struct ltt_ust_session
*usess
= session
->ust_session
;
2499 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2500 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2501 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2502 ret
= build_network_session_path(sessions
[i
].path
,
2503 sizeof(sessions
[i
].path
), session
);
2505 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2506 session
->consumer
->dst
.trace_path
);
2509 PERROR("snprintf session path");
2513 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2514 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2515 sessions
[i
].enabled
= session
->active
;
2516 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2517 sessions
[i
].live_timer_interval
= session
->live_timer
;
2523 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2524 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2526 int cmd_data_pending(struct ltt_session
*session
)
2529 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2530 struct ltt_ust_session
*usess
= session
->ust_session
;
2534 /* Session MUST be stopped to ask for data availability. */
2535 if (session
->active
) {
2536 ret
= LTTNG_ERR_SESSION_STARTED
;
2540 * If stopped, just make sure we've started before else the above call
2541 * will always send that there is data pending.
2543 * The consumer assumes that when the data pending command is received,
2544 * the trace has been started before or else no output data is written
2545 * by the streams which is a condition for data pending. So, this is
2546 * *VERY* important that we don't ask the consumer before a start
2549 if (!session
->has_been_started
) {
2555 if (ksess
&& ksess
->consumer
) {
2556 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2558 /* Data is still being extracted for the kernel. */
2563 if (usess
&& usess
->consumer
) {
2564 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2566 /* Data is still being extracted for the kernel. */
2571 /* Data is ready to be read by a viewer */
2579 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2581 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2583 int cmd_snapshot_add_output(struct ltt_session
*session
,
2584 struct lttng_snapshot_output
*output
, uint32_t *id
)
2587 struct snapshot_output
*new_output
;
2592 DBG("Cmd snapshot add output for session %s", session
->name
);
2595 * Permission denied to create an output if the session is not
2596 * set in no output mode.
2598 if (session
->output_traces
) {
2599 ret
= LTTNG_ERR_EPERM
;
2603 /* Only one output is allowed until we have the "tee" feature. */
2604 if (session
->snapshot
.nb_output
== 1) {
2605 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2609 new_output
= snapshot_output_alloc();
2611 ret
= LTTNG_ERR_NOMEM
;
2615 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2616 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2617 &session
->snapshot
);
2619 if (ret
== -ENOMEM
) {
2620 ret
= LTTNG_ERR_NOMEM
;
2622 ret
= LTTNG_ERR_INVALID
;
2628 snapshot_add_output(&session
->snapshot
, new_output
);
2630 *id
= new_output
->id
;
2637 snapshot_output_destroy(new_output
);
2643 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2645 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2647 int cmd_snapshot_del_output(struct ltt_session
*session
,
2648 struct lttng_snapshot_output
*output
)
2651 struct snapshot_output
*sout
= NULL
;
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
;
2668 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2670 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2671 } else if (*output
->name
!= '\0') {
2672 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2674 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2677 ret
= LTTNG_ERR_INVALID
;
2681 snapshot_delete_output(&session
->snapshot
, sout
);
2682 snapshot_output_destroy(sout
);
2691 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2693 * If no output is available, outputs is untouched and 0 is returned.
2695 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2697 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2698 struct lttng_snapshot_output
**outputs
)
2701 struct lttng_snapshot_output
*list
;
2702 struct lttng_ht_iter iter
;
2703 struct snapshot_output
*output
;
2708 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2711 * Permission denied to create an output if the session is not
2712 * set in no output mode.
2714 if (session
->output_traces
) {
2715 ret
= LTTNG_ERR_EPERM
;
2719 if (session
->snapshot
.nb_output
== 0) {
2724 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2726 ret
= LTTNG_ERR_NOMEM
;
2730 /* Copy list from session to the new list object. */
2731 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2732 output
, node
.node
) {
2733 assert(output
->consumer
);
2734 list
[idx
].id
= output
->id
;
2735 list
[idx
].max_size
= output
->max_size
;
2736 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2737 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2738 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2739 sizeof(list
[idx
].ctrl_url
));
2742 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2743 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2745 ret
= LTTNG_ERR_NOMEM
;
2750 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2751 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2753 ret
= LTTNG_ERR_NOMEM
;
2761 return session
->snapshot
.nb_output
;
2770 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2771 * snapshot output is *not* set with a remote destination.
2773 * Return 0 on success or a LTTNG_ERR code.
2775 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2776 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2779 struct lttng_ht_iter iter
;
2780 struct consumer_socket
*socket
;
2783 assert(snap_output
);
2786 DBG2("Set relayd object from snapshot output");
2788 /* Ignore if snapshot consumer output is not network. */
2789 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2794 * For each consumer socket, create and send the relayd object of the
2798 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2799 socket
, node
.node
) {
2800 ret
= send_consumer_relayd_sockets(0, session
->id
,
2801 snap_output
->consumer
, socket
,
2802 session
->name
, session
->hostname
,
2803 session
->live_timer
);
2804 if (ret
!= LTTNG_OK
) {
2816 * Record a kernel snapshot.
2818 * Return LTTNG_OK on success or a LTTNG_ERR code.
2820 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
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 kernel session sockets so we can communicate with the right
2840 * consumer for the snapshot record command.
2842 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2844 ret
= LTTNG_ERR_NOMEM
;
2848 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2849 if (ret
!= LTTNG_OK
) {
2850 goto error_snapshot
;
2853 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_streams
);
2854 if (ret
!= LTTNG_OK
) {
2855 goto error_snapshot
;
2861 /* Clean up copied sockets so this output can use some other later on. */
2862 consumer_destroy_output_sockets(output
->consumer
);
2868 * Record a UST snapshot.
2870 * Return 0 on success or a LTTNG_ERR error code.
2872 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2873 struct snapshot_output
*output
, struct ltt_session
*session
,
2874 int wait
, int nb_streams
)
2882 /* Get the datetime for the snapshot output directory. */
2883 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2884 sizeof(output
->datetime
));
2886 ret
= LTTNG_ERR_INVALID
;
2891 * Copy UST session sockets so we can communicate with the right
2892 * consumer for the snapshot record command.
2894 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
2896 ret
= LTTNG_ERR_NOMEM
;
2900 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
2901 if (ret
!= LTTNG_OK
) {
2902 goto error_snapshot
;
2905 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_streams
);
2909 ret
= LTTNG_ERR_INVALID
;
2912 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
2915 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
2918 goto error_snapshot
;
2924 /* Clean up copied sockets so this output can use some other later on. */
2925 consumer_destroy_output_sockets(output
->consumer
);
2931 * Returns the total number of streams for a session or a negative value
2934 static unsigned int get_total_nb_stream(struct ltt_session
*session
)
2936 unsigned int total_streams
= 0;
2938 if (session
->kernel_session
) {
2939 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2941 total_streams
+= ksess
->stream_count_global
;
2944 if (session
->ust_session
) {
2945 struct ltt_ust_session
*usess
= session
->ust_session
;
2947 total_streams
+= ust_app_get_nb_stream(usess
);
2950 return total_streams
;
2954 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
2956 * The wait parameter is ignored so this call always wait for the snapshot to
2957 * complete before returning.
2959 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2961 int cmd_snapshot_record(struct ltt_session
*session
,
2962 struct lttng_snapshot_output
*output
, int wait
)
2965 unsigned int use_tmp_output
= 0;
2966 struct snapshot_output tmp_output
;
2967 unsigned int nb_streams
, snapshot_success
= 0;
2971 DBG("Cmd snapshot record for session %s", session
->name
);
2974 * Permission denied to create an output if the session is not
2975 * set in no output mode.
2977 if (session
->output_traces
) {
2978 ret
= LTTNG_ERR_EPERM
;
2982 /* The session needs to be started at least once. */
2983 if (!session
->has_been_started
) {
2984 ret
= LTTNG_ERR_START_SESSION_ONCE
;
2988 /* Use temporary output for the session. */
2989 if (output
&& *output
->ctrl_url
!= '\0') {
2990 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2991 output
->ctrl_url
, output
->data_url
, session
->consumer
,
2994 if (ret
== -ENOMEM
) {
2995 ret
= LTTNG_ERR_NOMEM
;
2997 ret
= LTTNG_ERR_INVALID
;
3001 /* Use the global session count for the temporary snapshot. */
3002 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3007 * Get the total number of stream of that session which is used by the
3008 * maximum size of the snapshot feature.
3010 nb_streams
= get_total_nb_stream(session
);
3012 if (session
->kernel_session
) {
3013 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3015 if (use_tmp_output
) {
3016 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3018 if (ret
!= LTTNG_OK
) {
3021 snapshot_success
= 1;
3023 struct snapshot_output
*sout
;
3024 struct lttng_ht_iter iter
;
3027 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3028 &iter
.iter
, sout
, node
.node
) {
3030 * Make a local copy of the output and assign the possible
3031 * temporary value given by the caller.
3033 memset(&tmp_output
, 0, sizeof(tmp_output
));
3034 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3036 /* Use temporary max size. */
3037 if (output
->max_size
!= (uint64_t) -1ULL) {
3038 tmp_output
.max_size
= output
->max_size
;
3041 /* Use temporary name. */
3042 if (*output
->name
!= '\0') {
3043 strncpy(tmp_output
.name
, output
->name
,
3044 sizeof(tmp_output
.name
));
3047 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3049 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3050 session
, wait
, nb_streams
);
3051 if (ret
!= LTTNG_OK
) {
3055 snapshot_success
= 1;
3061 if (session
->ust_session
) {
3062 struct ltt_ust_session
*usess
= session
->ust_session
;
3064 if (use_tmp_output
) {
3065 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3067 if (ret
!= LTTNG_OK
) {
3070 snapshot_success
= 1;
3072 struct snapshot_output
*sout
;
3073 struct lttng_ht_iter iter
;
3076 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3077 &iter
.iter
, sout
, node
.node
) {
3079 * Make a local copy of the output and assign the possible
3080 * temporary value given by the caller.
3082 memset(&tmp_output
, 0, sizeof(tmp_output
));
3083 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3085 /* Use temporary max size. */
3086 if (output
->max_size
!= (uint64_t) -1ULL) {
3087 tmp_output
.max_size
= output
->max_size
;
3090 /* Use temporary name. */
3091 if (*output
->name
!= '\0') {
3092 strncpy(tmp_output
.name
, output
->name
,
3093 sizeof(tmp_output
.name
));
3096 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3098 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3100 if (ret
!= LTTNG_OK
) {
3104 snapshot_success
= 1;
3110 if (snapshot_success
) {
3111 session
->snapshot
.nb_snapshot
++;
3113 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3121 * Init command subsystem.
3126 * Set network sequence index to 1 for streams to match a relayd
3127 * socket on the consumer side.
3129 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3130 relayd_net_seq_idx
= 1;
3131 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3133 DBG("Command subsystem initialized");