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;
936 switch (domain
->type
) {
937 case LTTNG_DOMAIN_KERNEL
:
939 struct ltt_kernel_channel
*kchan
;
941 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
942 session
->kernel_session
);
944 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
945 if (attr
->name
[0] != '\0') {
946 session
->kernel_session
->has_non_default_channel
= 1;
949 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
952 if (ret
!= LTTNG_OK
) {
956 kernel_wait_quiescent(kernel_tracer_fd
);
959 case LTTNG_DOMAIN_UST
:
961 struct ltt_ust_channel
*uchan
;
963 chan_ht
= usess
->domain_global
.channels
;
965 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
967 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
968 if (attr
->name
[0] != '\0') {
969 usess
->has_non_default_channel
= 1;
972 ret
= channel_ust_enable(usess
, uchan
);
977 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
988 * Command LTTNG_DISABLE_EVENT processed by the client thread.
990 int cmd_disable_event(struct ltt_session
*session
, int domain
,
991 char *channel_name
, char *event_name
)
998 case LTTNG_DOMAIN_KERNEL
:
1000 struct ltt_kernel_channel
*kchan
;
1001 struct ltt_kernel_session
*ksess
;
1003 ksess
= session
->kernel_session
;
1006 * If a non-default channel has been created in the
1007 * session, explicitely require that -c chan_name needs
1010 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1011 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1015 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1016 if (kchan
== NULL
) {
1017 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1021 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1022 if (ret
!= LTTNG_OK
) {
1026 kernel_wait_quiescent(kernel_tracer_fd
);
1029 case LTTNG_DOMAIN_UST
:
1031 struct ltt_ust_channel
*uchan
;
1032 struct ltt_ust_session
*usess
;
1034 usess
= session
->ust_session
;
1037 * If a non-default channel has been created in the
1038 * session, explicitely require that -c chan_name needs
1041 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1042 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1046 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1048 if (uchan
== NULL
) {
1049 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1053 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1054 if (ret
!= LTTNG_OK
) {
1058 DBG3("Disable UST event %s in channel %s completed", event_name
,
1062 case LTTNG_DOMAIN_JUL
:
1064 struct ltt_ust_session
*usess
= session
->ust_session
;
1068 ret
= event_jul_disable(usess
, event_name
);
1069 if (ret
!= LTTNG_OK
) {
1076 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1077 case LTTNG_DOMAIN_UST_PID
:
1078 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1081 ret
= LTTNG_ERR_UND
;
1093 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
1095 int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
1103 case LTTNG_DOMAIN_KERNEL
:
1105 struct ltt_kernel_session
*ksess
;
1106 struct ltt_kernel_channel
*kchan
;
1108 ksess
= session
->kernel_session
;
1111 * If a non-default channel has been created in the
1112 * session, explicitely require that -c chan_name needs
1115 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1116 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1120 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1121 if (kchan
== NULL
) {
1122 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1126 ret
= event_kernel_disable_all(kchan
);
1127 if (ret
!= LTTNG_OK
) {
1131 kernel_wait_quiescent(kernel_tracer_fd
);
1134 case LTTNG_DOMAIN_UST
:
1136 struct ltt_ust_session
*usess
;
1137 struct ltt_ust_channel
*uchan
;
1139 usess
= session
->ust_session
;
1142 * If a non-default channel has been created in the
1143 * session, explicitely require that -c chan_name needs
1146 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1147 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1151 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1153 if (uchan
== NULL
) {
1154 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1158 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1163 DBG3("Disable all UST events in channel %s completed", channel_name
);
1167 case LTTNG_DOMAIN_JUL
:
1169 struct ltt_ust_session
*usess
= session
->ust_session
;
1173 ret
= event_jul_disable_all(usess
);
1174 if (ret
!= LTTNG_OK
) {
1181 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1182 case LTTNG_DOMAIN_UST_PID
:
1183 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1186 ret
= LTTNG_ERR_UND
;
1198 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1200 int cmd_add_context(struct ltt_session
*session
, int domain
,
1201 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1203 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1206 case LTTNG_DOMAIN_KERNEL
:
1207 assert(session
->kernel_session
);
1209 if (session
->kernel_session
->channel_count
== 0) {
1210 /* Create default channel */
1211 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1212 if (ret
!= LTTNG_OK
) {
1215 chan_kern_created
= 1;
1217 /* Add kernel context to kernel tracer */
1218 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1219 if (ret
!= LTTNG_OK
) {
1223 case LTTNG_DOMAIN_UST
:
1225 struct ltt_ust_session
*usess
= session
->ust_session
;
1226 unsigned int chan_count
;
1230 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1231 if (chan_count
== 0) {
1232 struct lttng_channel
*attr
;
1233 /* Create default channel */
1234 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1236 ret
= LTTNG_ERR_FATAL
;
1240 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1241 if (ret
!= LTTNG_OK
) {
1246 chan_ust_created
= 1;
1249 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1250 if (ret
!= LTTNG_OK
) {
1256 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1257 case LTTNG_DOMAIN_UST_PID
:
1258 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1261 ret
= LTTNG_ERR_UND
;
1268 if (chan_kern_created
) {
1269 struct ltt_kernel_channel
*kchan
=
1270 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1271 session
->kernel_session
);
1272 /* Created previously, this should NOT fail. */
1274 kernel_destroy_channel(kchan
);
1277 if (chan_ust_created
) {
1278 struct ltt_ust_channel
*uchan
=
1279 trace_ust_find_channel_by_name(
1280 session
->ust_session
->domain_global
.channels
,
1281 DEFAULT_CHANNEL_NAME
);
1282 /* Created previously, this should NOT fail. */
1284 /* Remove from the channel list of the session. */
1285 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1287 trace_ust_destroy_channel(uchan
);
1293 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1295 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1296 char *channel_name
, struct lttng_event
*event
,
1297 char *filter_expression
,
1298 struct lttng_filter_bytecode
*filter
,
1299 struct lttng_event_exclusion
*exclusion
,
1302 int ret
, channel_created
= 0;
1303 struct lttng_channel
*attr
;
1307 assert(channel_name
);
1311 switch (domain
->type
) {
1312 case LTTNG_DOMAIN_KERNEL
:
1314 struct ltt_kernel_channel
*kchan
;
1317 * If a non-default channel has been created in the
1318 * session, explicitely require that -c chan_name needs
1321 if (session
->kernel_session
->has_non_default_channel
1322 && channel_name
[0] == '\0') {
1323 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1327 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1328 session
->kernel_session
);
1329 if (kchan
== NULL
) {
1330 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1331 LTTNG_BUFFER_GLOBAL
);
1333 ret
= LTTNG_ERR_FATAL
;
1336 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1338 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1339 if (ret
!= LTTNG_OK
) {
1345 channel_created
= 1;
1348 /* Get the newly created kernel channel pointer */
1349 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1350 session
->kernel_session
);
1351 if (kchan
== NULL
) {
1352 /* This sould not happen... */
1353 ret
= LTTNG_ERR_FATAL
;
1357 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1358 if (ret
!= LTTNG_OK
) {
1359 if (channel_created
) {
1360 /* Let's not leak a useless channel. */
1361 kernel_destroy_channel(kchan
);
1366 kernel_wait_quiescent(kernel_tracer_fd
);
1369 case LTTNG_DOMAIN_UST
:
1371 struct ltt_ust_channel
*uchan
;
1372 struct ltt_ust_session
*usess
= session
->ust_session
;
1377 * If a non-default channel has been created in the
1378 * session, explicitely require that -c chan_name needs
1381 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1382 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1386 /* Get channel from global UST domain */
1387 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1389 if (uchan
== NULL
) {
1390 /* Create default channel */
1391 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1392 usess
->buffer_type
);
1394 ret
= LTTNG_ERR_FATAL
;
1397 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1399 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1400 if (ret
!= LTTNG_OK
) {
1406 /* Get the newly created channel reference back */
1407 uchan
= trace_ust_find_channel_by_name(
1408 usess
->domain_global
.channels
, channel_name
);
1412 /* At this point, the session and channel exist on the tracer */
1413 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1414 filter_expression
, filter
, exclusion
);
1415 if (ret
!= LTTNG_OK
) {
1420 case LTTNG_DOMAIN_JUL
:
1422 struct lttng_event uevent
;
1423 struct lttng_domain tmp_dom
;
1424 struct ltt_ust_session
*usess
= session
->ust_session
;
1428 /* Create the default JUL tracepoint. */
1429 memset(&uevent
, 0, sizeof(uevent
));
1430 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1431 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1433 strncpy(uevent
.name
, DEFAULT_SYS_JUL_EVENT_NAME
,
1434 sizeof(uevent
.name
));
1436 strncpy(uevent
.name
, DEFAULT_USER_JUL_EVENT_NAME
,
1437 sizeof(uevent
.name
));
1439 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1442 * The domain type is changed because we are about to enable the
1443 * default channel and event for the JUL domain that are hardcoded.
1444 * This happens in the UST domain.
1446 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1447 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1449 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1450 &uevent
, filter_expression
, filter
, NULL
, wpipe
);
1451 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1455 /* The wild card * means that everything should be enabled. */
1456 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1457 ret
= event_jul_enable_all(usess
, event
);
1459 ret
= event_jul_enable(usess
, event
);
1461 if (ret
!= LTTNG_OK
) {
1468 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1469 case LTTNG_DOMAIN_UST_PID
:
1470 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1473 ret
= LTTNG_ERR_UND
;
1485 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1487 int cmd_enable_event_all(struct ltt_session
*session
,
1488 struct lttng_domain
*domain
, char *channel_name
, int event_type
,
1489 char *filter_expression
,
1490 struct lttng_filter_bytecode
*filter
, int wpipe
)
1493 struct lttng_channel
*attr
;
1496 assert(channel_name
);
1500 switch (domain
->type
) {
1501 case LTTNG_DOMAIN_KERNEL
:
1503 struct ltt_kernel_channel
*kchan
;
1505 assert(session
->kernel_session
);
1508 * If a non-default channel has been created in the
1509 * session, explicitely require that -c chan_name needs
1512 if (session
->kernel_session
->has_non_default_channel
1513 && channel_name
[0] == '\0') {
1514 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1518 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1519 session
->kernel_session
);
1520 if (kchan
== NULL
) {
1521 /* Create default channel */
1522 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1523 LTTNG_BUFFER_GLOBAL
);
1525 ret
= LTTNG_ERR_FATAL
;
1528 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1530 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1531 if (ret
!= LTTNG_OK
) {
1537 /* Get the newly created kernel channel pointer */
1538 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1539 session
->kernel_session
);
1543 switch (event_type
) {
1544 case LTTNG_EVENT_SYSCALL
:
1545 ret
= event_kernel_enable_all_syscalls(kchan
, kernel_tracer_fd
);
1547 case LTTNG_EVENT_TRACEPOINT
:
1549 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1550 * events already registered to the channel.
1552 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1554 case LTTNG_EVENT_ALL
:
1555 /* Enable syscalls and tracepoints */
1556 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1559 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1563 /* Manage return value */
1564 if (ret
!= LTTNG_OK
) {
1566 * On error, cmd_enable_channel call will take care of destroying
1567 * the created channel if it was needed.
1572 kernel_wait_quiescent(kernel_tracer_fd
);
1575 case LTTNG_DOMAIN_UST
:
1577 struct ltt_ust_channel
*uchan
;
1578 struct ltt_ust_session
*usess
= session
->ust_session
;
1583 * If a non-default channel has been created in the
1584 * session, explicitely require that -c chan_name needs
1587 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1588 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1592 /* Get channel from global UST domain */
1593 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1595 if (uchan
== NULL
) {
1596 /* Create default channel */
1597 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1598 usess
->buffer_type
);
1600 ret
= LTTNG_ERR_FATAL
;
1603 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1605 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1606 if (ret
!= LTTNG_OK
) {
1612 /* Get the newly created channel reference back */
1613 uchan
= trace_ust_find_channel_by_name(
1614 usess
->domain_global
.channels
, channel_name
);
1618 /* At this point, the session and channel exist on the tracer */
1620 switch (event_type
) {
1621 case LTTNG_EVENT_ALL
:
1622 case LTTNG_EVENT_TRACEPOINT
:
1623 ret
= event_ust_enable_all_tracepoints(usess
, uchan
,
1624 filter_expression
, filter
);
1625 if (ret
!= LTTNG_OK
) {
1630 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
1634 /* Manage return value */
1635 if (ret
!= LTTNG_OK
) {
1641 case LTTNG_DOMAIN_JUL
:
1643 struct lttng_event uevent
, event
;
1644 struct lttng_domain tmp_dom
;
1645 struct ltt_ust_session
*usess
= session
->ust_session
;
1649 /* Create the default JUL tracepoint. */
1650 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1651 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1653 strncpy(uevent
.name
, DEFAULT_SYS_JUL_EVENT_NAME
,
1654 sizeof(uevent
.name
));
1656 strncpy(uevent
.name
, DEFAULT_USER_JUL_EVENT_NAME
,
1657 sizeof(uevent
.name
));
1659 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1662 * The domain type is changed because we are about to enable the
1663 * default channel and event for the JUL domain that are hardcoded.
1664 * This happens in the UST domain.
1666 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1667 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1669 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1670 &uevent
, NULL
, NULL
, NULL
, wpipe
);
1671 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1675 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1676 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1677 strncpy(event
.name
, "*", sizeof(event
.name
));
1678 event
.name
[sizeof(event
.name
) - 1] = '\0';
1680 ret
= event_jul_enable_all(usess
, &event
);
1681 if (ret
!= LTTNG_OK
) {
1688 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1689 case LTTNG_DOMAIN_UST_PID
:
1690 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1693 ret
= LTTNG_ERR_UND
;
1706 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1708 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1711 ssize_t nb_events
= 0;
1714 case LTTNG_DOMAIN_KERNEL
:
1715 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1716 if (nb_events
< 0) {
1717 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1721 case LTTNG_DOMAIN_UST
:
1722 nb_events
= ust_app_list_events(events
);
1723 if (nb_events
< 0) {
1724 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1728 case LTTNG_DOMAIN_JUL
:
1729 nb_events
= jul_list_events(events
);
1730 if (nb_events
< 0) {
1731 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1736 ret
= LTTNG_ERR_UND
;
1743 /* Return negative value to differentiate return code */
1748 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1750 ssize_t
cmd_list_tracepoint_fields(int domain
,
1751 struct lttng_event_field
**fields
)
1754 ssize_t nb_fields
= 0;
1757 case LTTNG_DOMAIN_UST
:
1758 nb_fields
= ust_app_list_event_fields(fields
);
1759 if (nb_fields
< 0) {
1760 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1764 case LTTNG_DOMAIN_KERNEL
:
1765 default: /* fall-through */
1766 ret
= LTTNG_ERR_UND
;
1773 /* Return negative value to differentiate return code */
1778 * Command LTTNG_START_TRACE processed by the client thread.
1780 int cmd_start_trace(struct ltt_session
*session
)
1783 unsigned long nb_chan
= 0;
1784 struct ltt_kernel_session
*ksession
;
1785 struct ltt_ust_session
*usess
;
1789 /* Ease our life a bit ;) */
1790 ksession
= session
->kernel_session
;
1791 usess
= session
->ust_session
;
1793 /* Is the session already started? */
1794 if (session
->active
) {
1795 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1800 * Starting a session without channel is useless since after that it's not
1801 * possible to enable channel thus inform the client.
1803 if (usess
&& usess
->domain_global
.channels
) {
1804 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1807 nb_chan
+= ksession
->channel_count
;
1810 ret
= LTTNG_ERR_NO_CHANNEL
;
1814 /* Kernel tracing */
1815 if (ksession
!= NULL
) {
1816 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1817 if (ret
!= LTTNG_OK
) {
1822 /* Flag session that trace should start automatically */
1825 * Even though the start trace might fail, flag this session active so
1826 * other application coming in are started by default.
1830 ret
= ust_app_start_trace_all(usess
);
1832 ret
= LTTNG_ERR_UST_START_FAIL
;
1837 /* Flag this after a successful start. */
1838 session
->has_been_started
= 1;
1839 session
->active
= 1;
1848 * Command LTTNG_STOP_TRACE processed by the client thread.
1850 int cmd_stop_trace(struct ltt_session
*session
)
1853 struct ltt_kernel_channel
*kchan
;
1854 struct ltt_kernel_session
*ksession
;
1855 struct ltt_ust_session
*usess
;
1860 ksession
= session
->kernel_session
;
1861 usess
= session
->ust_session
;
1863 /* Session is not active. Skip everythong and inform the client. */
1864 if (!session
->active
) {
1865 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1870 if (ksession
&& ksession
->active
) {
1871 DBG("Stop kernel tracing");
1873 /* Flush metadata if exist */
1874 if (ksession
->metadata_stream_fd
>= 0) {
1875 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1877 ERR("Kernel metadata flush failed");
1881 /* Flush all buffers before stopping */
1882 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1883 ret
= kernel_flush_buffer(kchan
);
1885 ERR("Kernel flush buffer error");
1889 ret
= kernel_stop_session(ksession
);
1891 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1895 kernel_wait_quiescent(kernel_tracer_fd
);
1897 ksession
->active
= 0;
1900 if (usess
&& usess
->active
) {
1902 * Even though the stop trace might fail, flag this session inactive so
1903 * other application coming in are not started by default.
1907 ret
= ust_app_stop_trace_all(usess
);
1909 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1914 /* Flag inactive after a successful stop. */
1915 session
->active
= 0;
1923 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1925 int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
1926 size_t nb_uri
, struct lttng_uri
*uris
)
1929 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1930 struct ltt_ust_session
*usess
= session
->ust_session
;
1931 struct consumer_output
*consumer
= NULL
;
1937 /* Can't set consumer URI if the session is active. */
1938 if (session
->active
) {
1939 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1944 * This case switch makes sure the domain session has a temporary consumer
1945 * so the URL can be set.
1949 /* Code flow error. A session MUST always have a consumer object */
1950 assert(session
->consumer
);
1952 * The URL will be added to the tracing session consumer instead of a
1953 * specific domain consumer.
1955 consumer
= session
->consumer
;
1957 case LTTNG_DOMAIN_KERNEL
:
1958 /* Code flow error if we don't have a kernel session here. */
1960 assert(ksess
->consumer
);
1961 consumer
= ksess
->consumer
;
1963 case LTTNG_DOMAIN_UST
:
1964 /* Code flow error if we don't have a kernel session here. */
1966 assert(usess
->consumer
);
1967 consumer
= usess
->consumer
;
1971 for (i
= 0; i
< nb_uri
; i
++) {
1972 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
1973 if (ret
!= LTTNG_OK
) {
1986 * Command LTTNG_CREATE_SESSION processed by the client thread.
1988 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
1989 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
1992 struct ltt_session
*session
;
1998 * Verify if the session already exist
2000 * XXX: There is no need for the session lock list here since the caller
2001 * (process_client_msg) is holding it. We might want to change that so a
2002 * single command does not lock the entire session list.
2004 session
= session_find_by_name(name
);
2005 if (session
!= NULL
) {
2006 ret
= LTTNG_ERR_EXIST_SESS
;
2010 /* Create tracing session in the registry */
2011 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2012 LTTNG_SOCK_GET_GID_CRED(creds
));
2013 if (ret
!= LTTNG_OK
) {
2018 * Get the newly created session pointer back
2020 * XXX: There is no need for the session lock list here since the caller
2021 * (process_client_msg) is holding it. We might want to change that so a
2022 * single command does not lock the entire session list.
2024 session
= session_find_by_name(name
);
2027 session
->live_timer
= live_timer
;
2028 /* Create default consumer output for the session not yet created. */
2029 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2030 if (session
->consumer
== NULL
) {
2031 ret
= LTTNG_ERR_FATAL
;
2032 goto consumer_error
;
2036 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
2037 if (ret
!= LTTNG_OK
) {
2038 goto consumer_error
;
2040 session
->output_traces
= 1;
2042 session
->output_traces
= 0;
2043 DBG2("Session %s created with no output", session
->name
);
2046 session
->consumer
->enabled
= 1;
2051 session_destroy(session
);
2058 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2060 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2061 size_t nb_uri
, lttng_sock_cred
*creds
)
2064 struct ltt_session
*session
;
2065 struct snapshot_output
*new_output
= NULL
;
2071 * Create session in no output mode with URIs set to NULL. The uris we've
2072 * received are for a default snapshot output if one.
2074 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2075 if (ret
!= LTTNG_OK
) {
2079 /* Get the newly created session pointer back. This should NEVER fail. */
2080 session
= session_find_by_name(name
);
2083 /* Flag session for snapshot mode. */
2084 session
->snapshot_mode
= 1;
2086 /* Skip snapshot output creation if no URI is given. */
2091 new_output
= snapshot_output_alloc();
2093 ret
= LTTNG_ERR_NOMEM
;
2094 goto error_snapshot_alloc
;
2097 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2098 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2100 if (ret
== -ENOMEM
) {
2101 ret
= LTTNG_ERR_NOMEM
;
2103 ret
= LTTNG_ERR_INVALID
;
2105 goto error_snapshot
;
2109 snapshot_add_output(&session
->snapshot
, new_output
);
2116 snapshot_output_destroy(new_output
);
2117 error_snapshot_alloc
:
2118 session_destroy(session
);
2124 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2126 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2129 struct ltt_ust_session
*usess
;
2130 struct ltt_kernel_session
*ksess
;
2135 usess
= session
->ust_session
;
2136 ksess
= session
->kernel_session
;
2138 /* Clean kernel session teardown */
2139 kernel_destroy_session(ksess
);
2141 /* UST session teardown */
2143 /* Close any relayd session */
2144 consumer_output_send_destroy_relayd(usess
->consumer
);
2146 /* Destroy every UST application related to this session. */
2147 ret
= ust_app_destroy_trace_all(usess
);
2149 ERR("Error in ust_app_destroy_trace_all");
2152 /* Clean up the rest. */
2153 trace_ust_destroy_session(usess
);
2157 * Must notify the kernel thread here to update it's poll set in order to
2158 * remove the channel(s)' fd just destroyed.
2160 ret
= notify_thread_pipe(wpipe
);
2162 PERROR("write kernel poll pipe");
2165 ret
= session_destroy(session
);
2171 * Command LTTNG_CALIBRATE processed by the client thread.
2173 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2178 case LTTNG_DOMAIN_KERNEL
:
2180 struct lttng_kernel_calibrate kcalibrate
;
2182 switch (calibrate
->type
) {
2183 case LTTNG_CALIBRATE_FUNCTION
:
2185 /* Default and only possible calibrate option. */
2186 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2190 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2192 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2197 case LTTNG_DOMAIN_UST
:
2199 struct lttng_ust_calibrate ucalibrate
;
2201 switch (calibrate
->type
) {
2202 case LTTNG_CALIBRATE_FUNCTION
:
2204 /* Default and only possible calibrate option. */
2205 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2209 ret
= ust_app_calibrate_glb(&ucalibrate
);
2211 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2217 ret
= LTTNG_ERR_UND
;
2228 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2230 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2231 const char *sock_path
, struct consumer_data
*cdata
)
2234 struct consumer_socket
*socket
= NULL
;
2241 case LTTNG_DOMAIN_KERNEL
:
2243 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2247 /* Can't register a consumer if there is already one */
2248 if (ksess
->consumer_fds_sent
!= 0) {
2249 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2253 sock
= lttcomm_connect_unix_sock(sock_path
);
2255 ret
= LTTNG_ERR_CONNECT_FAIL
;
2258 cdata
->cmd_sock
= sock
;
2260 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2261 if (socket
== NULL
) {
2264 PERROR("close register consumer");
2266 cdata
->cmd_sock
= -1;
2267 ret
= LTTNG_ERR_FATAL
;
2271 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2272 if (socket
->lock
== NULL
) {
2273 PERROR("zmalloc pthread mutex");
2274 ret
= LTTNG_ERR_FATAL
;
2277 pthread_mutex_init(socket
->lock
, NULL
);
2278 socket
->registered
= 1;
2281 consumer_add_socket(socket
, ksess
->consumer
);
2284 pthread_mutex_lock(&cdata
->pid_mutex
);
2286 pthread_mutex_unlock(&cdata
->pid_mutex
);
2291 /* TODO: Userspace tracing */
2292 ret
= LTTNG_ERR_UND
;
2300 consumer_destroy_socket(socket
);
2306 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2308 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2309 struct lttng_domain
**domains
)
2314 if (session
->kernel_session
!= NULL
) {
2315 DBG3("Listing domains found kernel domain");
2319 if (session
->ust_session
!= NULL
) {
2320 DBG3("Listing domains found UST global domain");
2323 if (session
->ust_session
->domain_jul
.being_used
) {
2328 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2329 if (*domains
== NULL
) {
2330 ret
= LTTNG_ERR_FATAL
;
2334 if (session
->kernel_session
!= NULL
) {
2335 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2339 if (session
->ust_session
!= NULL
) {
2340 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2341 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2344 if (session
->ust_session
->domain_jul
.being_used
) {
2345 (*domains
)[index
].type
= LTTNG_DOMAIN_JUL
;
2346 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2354 /* Return negative value to differentiate return code */
2360 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2362 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2363 struct lttng_channel
**channels
)
2366 ssize_t nb_chan
= 0;
2369 case LTTNG_DOMAIN_KERNEL
:
2370 if (session
->kernel_session
!= NULL
) {
2371 nb_chan
= session
->kernel_session
->channel_count
;
2373 DBG3("Number of kernel channels %zd", nb_chan
);
2375 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2378 case LTTNG_DOMAIN_UST
:
2379 if (session
->ust_session
!= NULL
) {
2380 nb_chan
= lttng_ht_get_count(
2381 session
->ust_session
->domain_global
.channels
);
2383 DBG3("Number of UST global channels %zd", nb_chan
);
2385 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2390 ret
= LTTNG_ERR_UND
;
2395 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2396 if (*channels
== NULL
) {
2397 ret
= LTTNG_ERR_FATAL
;
2401 list_lttng_channels(domain
, session
, *channels
);
2404 /* Ret value was set in the domain switch case */
2411 /* Return negative value to differentiate return code */
2416 * Command LTTNG_LIST_EVENTS processed by the client thread.
2418 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2419 char *channel_name
, struct lttng_event
**events
)
2422 ssize_t nb_event
= 0;
2425 case LTTNG_DOMAIN_KERNEL
:
2426 if (session
->kernel_session
!= NULL
) {
2427 nb_event
= list_lttng_kernel_events(channel_name
,
2428 session
->kernel_session
, events
);
2431 case LTTNG_DOMAIN_UST
:
2433 if (session
->ust_session
!= NULL
) {
2434 nb_event
= list_lttng_ust_global_events(channel_name
,
2435 &session
->ust_session
->domain_global
, events
);
2439 case LTTNG_DOMAIN_JUL
:
2440 if (session
->ust_session
) {
2441 nb_event
= list_lttng_jul_events(
2442 &session
->ust_session
->domain_jul
, events
);
2446 ret
= LTTNG_ERR_UND
;
2453 /* Return negative value to differentiate return code */
2458 * Using the session list, filled a lttng_session array to send back to the
2459 * client for session listing.
2461 * The session list lock MUST be acquired before calling this function. Use
2462 * session_lock_list() and session_unlock_list().
2464 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2469 struct ltt_session
*session
;
2470 struct ltt_session_list
*list
= session_get_list();
2472 DBG("Getting all available session for UID %d GID %d",
2475 * Iterate over session list and append data after the control struct in
2478 cds_list_for_each_entry(session
, &list
->head
, list
) {
2480 * Only list the sessions the user can control.
2482 if (!session_access_ok(session
, uid
, gid
)) {
2486 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2487 struct ltt_ust_session
*usess
= session
->ust_session
;
2489 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2490 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2491 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2492 ret
= build_network_session_path(sessions
[i
].path
,
2493 sizeof(sessions
[i
].path
), session
);
2495 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2496 session
->consumer
->dst
.trace_path
);
2499 PERROR("snprintf session path");
2503 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2504 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2505 sessions
[i
].enabled
= session
->active
;
2506 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2507 sessions
[i
].live_timer_interval
= session
->live_timer
;
2513 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2514 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2516 int cmd_data_pending(struct ltt_session
*session
)
2519 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2520 struct ltt_ust_session
*usess
= session
->ust_session
;
2524 /* Session MUST be stopped to ask for data availability. */
2525 if (session
->active
) {
2526 ret
= LTTNG_ERR_SESSION_STARTED
;
2530 * If stopped, just make sure we've started before else the above call
2531 * will always send that there is data pending.
2533 * The consumer assumes that when the data pending command is received,
2534 * the trace has been started before or else no output data is written
2535 * by the streams which is a condition for data pending. So, this is
2536 * *VERY* important that we don't ask the consumer before a start
2539 if (!session
->has_been_started
) {
2545 if (ksess
&& ksess
->consumer
) {
2546 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2548 /* Data is still being extracted for the kernel. */
2553 if (usess
&& usess
->consumer
) {
2554 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2556 /* Data is still being extracted for the kernel. */
2561 /* Data is ready to be read by a viewer */
2569 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2571 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2573 int cmd_snapshot_add_output(struct ltt_session
*session
,
2574 struct lttng_snapshot_output
*output
, uint32_t *id
)
2577 struct snapshot_output
*new_output
;
2582 DBG("Cmd snapshot add output for session %s", session
->name
);
2585 * Permission denied to create an output if the session is not
2586 * set in no output mode.
2588 if (session
->output_traces
) {
2589 ret
= LTTNG_ERR_EPERM
;
2593 /* Only one output is allowed until we have the "tee" feature. */
2594 if (session
->snapshot
.nb_output
== 1) {
2595 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2599 new_output
= snapshot_output_alloc();
2601 ret
= LTTNG_ERR_NOMEM
;
2605 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2606 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2607 &session
->snapshot
);
2609 if (ret
== -ENOMEM
) {
2610 ret
= LTTNG_ERR_NOMEM
;
2612 ret
= LTTNG_ERR_INVALID
;
2618 snapshot_add_output(&session
->snapshot
, new_output
);
2620 *id
= new_output
->id
;
2627 snapshot_output_destroy(new_output
);
2633 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2635 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2637 int cmd_snapshot_del_output(struct ltt_session
*session
,
2638 struct lttng_snapshot_output
*output
)
2641 struct snapshot_output
*sout
= NULL
;
2649 * Permission denied to create an output if the session is not
2650 * set in no output mode.
2652 if (session
->output_traces
) {
2653 ret
= LTTNG_ERR_EPERM
;
2658 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2660 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2661 } else if (*output
->name
!= '\0') {
2662 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2664 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2667 ret
= LTTNG_ERR_INVALID
;
2671 snapshot_delete_output(&session
->snapshot
, sout
);
2672 snapshot_output_destroy(sout
);
2681 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2683 * If no output is available, outputs is untouched and 0 is returned.
2685 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2687 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2688 struct lttng_snapshot_output
**outputs
)
2691 struct lttng_snapshot_output
*list
;
2692 struct lttng_ht_iter iter
;
2693 struct snapshot_output
*output
;
2698 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2701 * Permission denied to create an output if the session is not
2702 * set in no output mode.
2704 if (session
->output_traces
) {
2705 ret
= LTTNG_ERR_EPERM
;
2709 if (session
->snapshot
.nb_output
== 0) {
2714 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2716 ret
= LTTNG_ERR_NOMEM
;
2720 /* Copy list from session to the new list object. */
2721 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2722 output
, node
.node
) {
2723 assert(output
->consumer
);
2724 list
[idx
].id
= output
->id
;
2725 list
[idx
].max_size
= output
->max_size
;
2726 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2727 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2728 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2729 sizeof(list
[idx
].ctrl_url
));
2732 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2733 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2735 ret
= LTTNG_ERR_NOMEM
;
2740 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2741 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2743 ret
= LTTNG_ERR_NOMEM
;
2751 return session
->snapshot
.nb_output
;
2760 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2761 * snapshot output is *not* set with a remote destination.
2763 * Return 0 on success or a LTTNG_ERR code.
2765 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2766 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2769 struct lttng_ht_iter iter
;
2770 struct consumer_socket
*socket
;
2773 assert(snap_output
);
2776 DBG2("Set relayd object from snapshot output");
2778 /* Ignore if snapshot consumer output is not network. */
2779 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2784 * For each consumer socket, create and send the relayd object of the
2788 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2789 socket
, node
.node
) {
2790 ret
= send_consumer_relayd_sockets(0, session
->id
,
2791 snap_output
->consumer
, socket
,
2792 session
->name
, session
->hostname
,
2793 session
->live_timer
);
2794 if (ret
!= LTTNG_OK
) {
2806 * Record a kernel snapshot.
2808 * Return LTTNG_OK on success or a LTTNG_ERR code.
2810 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2811 struct snapshot_output
*output
, struct ltt_session
*session
,
2812 int wait
, int nb_streams
)
2820 /* Get the datetime for the snapshot output directory. */
2821 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2822 sizeof(output
->datetime
));
2824 ret
= LTTNG_ERR_INVALID
;
2829 * Copy kernel session sockets so we can communicate with the right
2830 * consumer for the snapshot record command.
2832 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2834 ret
= LTTNG_ERR_NOMEM
;
2838 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2839 if (ret
!= LTTNG_OK
) {
2840 goto error_snapshot
;
2843 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_streams
);
2844 if (ret
!= LTTNG_OK
) {
2845 goto error_snapshot
;
2851 /* Clean up copied sockets so this output can use some other later on. */
2852 consumer_destroy_output_sockets(output
->consumer
);
2858 * Record a UST snapshot.
2860 * Return 0 on success or a LTTNG_ERR error code.
2862 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2863 struct snapshot_output
*output
, struct ltt_session
*session
,
2864 int wait
, int nb_streams
)
2872 /* Get the datetime for the snapshot output directory. */
2873 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2874 sizeof(output
->datetime
));
2876 ret
= LTTNG_ERR_INVALID
;
2881 * Copy UST session sockets so we can communicate with the right
2882 * consumer for the snapshot record command.
2884 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
2886 ret
= LTTNG_ERR_NOMEM
;
2890 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
2891 if (ret
!= LTTNG_OK
) {
2892 goto error_snapshot
;
2895 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_streams
);
2899 ret
= LTTNG_ERR_INVALID
;
2902 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
2905 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
2908 goto error_snapshot
;
2914 /* Clean up copied sockets so this output can use some other later on. */
2915 consumer_destroy_output_sockets(output
->consumer
);
2921 * Returns the total number of streams for a session or a negative value
2924 static unsigned int get_total_nb_stream(struct ltt_session
*session
)
2926 unsigned int total_streams
= 0;
2928 if (session
->kernel_session
) {
2929 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2931 total_streams
+= ksess
->stream_count_global
;
2934 if (session
->ust_session
) {
2935 struct ltt_ust_session
*usess
= session
->ust_session
;
2937 total_streams
+= ust_app_get_nb_stream(usess
);
2940 return total_streams
;
2944 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
2946 * The wait parameter is ignored so this call always wait for the snapshot to
2947 * complete before returning.
2949 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2951 int cmd_snapshot_record(struct ltt_session
*session
,
2952 struct lttng_snapshot_output
*output
, int wait
)
2955 unsigned int use_tmp_output
= 0;
2956 struct snapshot_output tmp_output
;
2957 unsigned int nb_streams
, snapshot_success
= 0;
2961 DBG("Cmd snapshot record for session %s", session
->name
);
2964 * Permission denied to create an output if the session is not
2965 * set in no output mode.
2967 if (session
->output_traces
) {
2968 ret
= LTTNG_ERR_EPERM
;
2972 /* The session needs to be started at least once. */
2973 if (!session
->has_been_started
) {
2974 ret
= LTTNG_ERR_START_SESSION_ONCE
;
2978 /* Use temporary output for the session. */
2979 if (output
&& *output
->ctrl_url
!= '\0') {
2980 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2981 output
->ctrl_url
, output
->data_url
, session
->consumer
,
2984 if (ret
== -ENOMEM
) {
2985 ret
= LTTNG_ERR_NOMEM
;
2987 ret
= LTTNG_ERR_INVALID
;
2991 /* Use the global session count for the temporary snapshot. */
2992 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
2997 * Get the total number of stream of that session which is used by the
2998 * maximum size of the snapshot feature.
3000 nb_streams
= get_total_nb_stream(session
);
3002 if (session
->kernel_session
) {
3003 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3005 if (use_tmp_output
) {
3006 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3008 if (ret
!= LTTNG_OK
) {
3011 snapshot_success
= 1;
3013 struct snapshot_output
*sout
;
3014 struct lttng_ht_iter iter
;
3017 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3018 &iter
.iter
, sout
, node
.node
) {
3020 * Make a local copy of the output and assign the possible
3021 * temporary value given by the caller.
3023 memset(&tmp_output
, 0, sizeof(tmp_output
));
3024 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3026 /* Use temporary max size. */
3027 if (output
->max_size
!= (uint64_t) -1ULL) {
3028 tmp_output
.max_size
= output
->max_size
;
3031 /* Use temporary name. */
3032 if (*output
->name
!= '\0') {
3033 strncpy(tmp_output
.name
, output
->name
,
3034 sizeof(tmp_output
.name
));
3037 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3039 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3040 session
, wait
, nb_streams
);
3041 if (ret
!= LTTNG_OK
) {
3045 snapshot_success
= 1;
3051 if (session
->ust_session
) {
3052 struct ltt_ust_session
*usess
= session
->ust_session
;
3054 if (use_tmp_output
) {
3055 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3057 if (ret
!= LTTNG_OK
) {
3060 snapshot_success
= 1;
3062 struct snapshot_output
*sout
;
3063 struct lttng_ht_iter iter
;
3066 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3067 &iter
.iter
, sout
, node
.node
) {
3069 * Make a local copy of the output and assign the possible
3070 * temporary value given by the caller.
3072 memset(&tmp_output
, 0, sizeof(tmp_output
));
3073 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3075 /* Use temporary max size. */
3076 if (output
->max_size
!= (uint64_t) -1ULL) {
3077 tmp_output
.max_size
= output
->max_size
;
3080 /* Use temporary name. */
3081 if (*output
->name
!= '\0') {
3082 strncpy(tmp_output
.name
, output
->name
,
3083 sizeof(tmp_output
.name
));
3086 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3088 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3090 if (ret
!= LTTNG_OK
) {
3094 snapshot_success
= 1;
3100 if (snapshot_success
) {
3101 session
->snapshot
.nb_snapshot
++;
3103 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3111 * Init command subsystem.
3116 * Set network sequence index to 1 for streams to match a relayd
3117 * socket on the consumer side.
3119 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3120 relayd_net_seq_idx
= 1;
3121 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3123 DBG("Command subsystem initialized");