2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <urcu/list.h>
22 #include <urcu/uatomic.h>
24 #include <common/defaults.h>
25 #include <common/common.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27 #include <common/relayd/relayd.h>
28 #include <common/utils.h>
33 #include "health-sessiond.h"
35 #include "kernel-consumer.h"
36 #include "lttng-sessiond.h"
42 * Used to keep a unique index for each relayd socket created where this value
43 * is associated with streams on the consumer so it can match the right relayd
44 * to send to. It must be accessed with the relayd_net_seq_idx_lock
47 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
48 static uint64_t relayd_net_seq_idx
;
51 * Create a session path used by list_lttng_sessions for the case that the
52 * session consumer is on the network.
54 static int build_network_session_path(char *dst
, size_t size
,
55 struct ltt_session
*session
)
57 int ret
, kdata_port
, udata_port
;
58 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
59 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
64 memset(tmp_urls
, 0, sizeof(tmp_urls
));
65 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
67 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
69 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
70 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
71 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
74 if (session
->ust_session
&& session
->ust_session
->consumer
) {
75 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
76 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
79 if (uuri
== NULL
&& kuri
== NULL
) {
80 uri
= &session
->consumer
->dst
.net
.control
;
81 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
82 } else if (kuri
&& uuri
) {
83 ret
= uri_compare(kuri
, uuri
);
87 /* Build uuri URL string */
88 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
95 } else if (kuri
&& uuri
== NULL
) {
97 } else if (uuri
&& kuri
== NULL
) {
101 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
107 * Do we have a UST url set. If yes, this means we have both kernel and UST
110 if (*tmp_uurl
!= '\0') {
111 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
112 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
115 if (kuri
|| (!kuri
&& !uuri
)) {
118 /* No kernel URI, use the UST port. */
121 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
129 * Fill lttng_channel array of all channels.
131 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
132 struct lttng_channel
*channels
)
135 struct ltt_kernel_channel
*kchan
;
137 DBG("Listing channels for session %s", session
->name
);
140 case LTTNG_DOMAIN_KERNEL
:
141 /* Kernel channels */
142 if (session
->kernel_session
!= NULL
) {
143 cds_list_for_each_entry(kchan
,
144 &session
->kernel_session
->channel_list
.head
, list
) {
145 /* Copy lttng_channel struct to array */
146 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
147 channels
[i
].enabled
= kchan
->enabled
;
152 case LTTNG_DOMAIN_UST
:
154 struct lttng_ht_iter iter
;
155 struct ltt_ust_channel
*uchan
;
158 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
159 &iter
.iter
, uchan
, node
.node
) {
160 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
161 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
162 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
163 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
164 channels
[i
].attr
.switch_timer_interval
=
165 uchan
->attr
.switch_timer_interval
;
166 channels
[i
].attr
.read_timer_interval
=
167 uchan
->attr
.read_timer_interval
;
168 channels
[i
].enabled
= uchan
->enabled
;
169 switch (uchan
->attr
.output
) {
172 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
186 * Create a list of JUL domain events.
188 * Return number of events in list on success or else a negative value.
190 static int list_lttng_jul_events(struct jul_domain
*dom
,
191 struct lttng_event
**events
)
194 unsigned int nb_event
= 0;
195 struct jul_event
*event
;
196 struct lttng_event
*tmp_events
;
197 struct lttng_ht_iter iter
;
202 DBG3("Listing JUL events");
204 nb_event
= lttng_ht_get_count(dom
->events
);
210 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
212 PERROR("zmalloc JUL events session");
213 ret
= -LTTNG_ERR_FATAL
;
218 cds_lfht_for_each_entry(dom
->events
->ht
, &iter
.iter
, event
, node
.node
) {
219 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
220 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
221 tmp_events
[i
].enabled
= event
->enabled
;
226 *events
= tmp_events
;
230 assert(nb_event
== i
);
235 * Create a list of ust global domain events.
237 static int list_lttng_ust_global_events(char *channel_name
,
238 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
241 unsigned int nb_event
= 0;
242 struct lttng_ht_iter iter
;
243 struct lttng_ht_node_str
*node
;
244 struct ltt_ust_channel
*uchan
;
245 struct ltt_ust_event
*uevent
;
246 struct lttng_event
*tmp
;
248 DBG("Listing UST global events for channel %s", channel_name
);
252 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
253 node
= lttng_ht_iter_get_node_str(&iter
);
255 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
259 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
261 nb_event
+= lttng_ht_get_count(uchan
->events
);
268 DBG3("Listing UST global %d events", nb_event
);
270 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
272 ret
= LTTNG_ERR_FATAL
;
276 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
277 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
278 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
279 tmp
[i
].enabled
= uevent
->enabled
;
281 switch (uevent
->attr
.instrumentation
) {
282 case LTTNG_UST_TRACEPOINT
:
283 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
285 case LTTNG_UST_PROBE
:
286 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
288 case LTTNG_UST_FUNCTION
:
289 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
293 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
294 switch (uevent
->attr
.loglevel_type
) {
295 case LTTNG_UST_LOGLEVEL_ALL
:
296 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
298 case LTTNG_UST_LOGLEVEL_RANGE
:
299 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
301 case LTTNG_UST_LOGLEVEL_SINGLE
:
302 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
305 if (uevent
->filter
) {
308 if (uevent
->exclusion
) {
309 tmp
[i
].exclusion
= 1;
323 * Fill lttng_event array of all kernel events in the channel.
325 static int list_lttng_kernel_events(char *channel_name
,
326 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
329 unsigned int nb_event
;
330 struct ltt_kernel_event
*event
;
331 struct ltt_kernel_channel
*kchan
;
333 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
335 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
339 nb_event
= kchan
->event_count
;
341 DBG("Listing events for channel %s", kchan
->channel
->name
);
347 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
348 if (*events
== NULL
) {
349 ret
= LTTNG_ERR_FATAL
;
353 /* Kernel channels */
354 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
355 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
356 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
357 (*events
)[i
].enabled
= event
->enabled
;
359 switch (event
->event
->instrumentation
) {
360 case LTTNG_KERNEL_TRACEPOINT
:
361 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
363 case LTTNG_KERNEL_KRETPROBE
:
364 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
365 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
366 sizeof(struct lttng_kernel_kprobe
));
368 case LTTNG_KERNEL_KPROBE
:
369 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
370 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
371 sizeof(struct lttng_kernel_kprobe
));
373 case LTTNG_KERNEL_FUNCTION
:
374 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
375 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
376 sizeof(struct lttng_kernel_function
));
378 case LTTNG_KERNEL_NOOP
:
379 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
381 case LTTNG_KERNEL_SYSCALL
:
382 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
384 case LTTNG_KERNEL_ALL
:
395 /* Negate the error code to differentiate the size from an error */
400 * Add URI so the consumer output object. Set the correct path depending on the
401 * domain adding the default trace directory.
403 static int add_uri_to_consumer(struct consumer_output
*consumer
,
404 struct lttng_uri
*uri
, int domain
, const char *session_name
)
407 const char *default_trace_dir
;
411 if (consumer
== NULL
) {
412 DBG("No consumer detected. Don't add URI. Stopping.");
413 ret
= LTTNG_ERR_NO_CONSUMER
;
418 case LTTNG_DOMAIN_KERNEL
:
419 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
421 case LTTNG_DOMAIN_UST
:
422 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
426 * This case is possible is we try to add the URI to the global tracing
427 * session consumer object which in this case there is no subdir.
429 default_trace_dir
= "";
432 switch (uri
->dtype
) {
435 DBG2("Setting network URI to consumer");
437 if (consumer
->type
== CONSUMER_DST_NET
) {
438 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
439 consumer
->dst
.net
.control_isset
) ||
440 (uri
->stype
== LTTNG_STREAM_DATA
&&
441 consumer
->dst
.net
.data_isset
)) {
442 ret
= LTTNG_ERR_URL_EXIST
;
446 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
449 consumer
->type
= CONSUMER_DST_NET
;
451 /* Set URI into consumer output object */
452 ret
= consumer_set_network_uri(consumer
, uri
);
456 } else if (ret
== 1) {
458 * URI was the same in the consumer so we do not append the subdir
459 * again so to not duplicate output dir.
465 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
466 ret
= consumer_set_subdir(consumer
, session_name
);
468 ret
= LTTNG_ERR_FATAL
;
473 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
474 /* On a new subdir, reappend the default trace dir. */
475 strncat(consumer
->subdir
, default_trace_dir
,
476 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
477 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
482 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
483 memset(consumer
->dst
.trace_path
, 0,
484 sizeof(consumer
->dst
.trace_path
));
485 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
486 sizeof(consumer
->dst
.trace_path
));
487 /* Append default trace dir */
488 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
489 sizeof(consumer
->dst
.trace_path
) -
490 strlen(consumer
->dst
.trace_path
) - 1);
491 /* Flag consumer as local. */
492 consumer
->type
= CONSUMER_DST_LOCAL
;
503 * Init tracing by creating trace directory and sending fds kernel consumer.
505 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
508 struct lttng_ht_iter iter
;
509 struct consumer_socket
*socket
;
515 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
516 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
518 pthread_mutex_lock(socket
->lock
);
519 ret
= kernel_consumer_send_session(socket
, session
);
520 pthread_mutex_unlock(socket
->lock
);
522 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
534 * Create a socket to the relayd using the URI.
536 * On success, the relayd_sock pointer is set to the created socket.
537 * Else, it's stays untouched and a lttcomm error code is returned.
539 static int create_connect_relayd(struct lttng_uri
*uri
,
540 struct lttcomm_relayd_sock
**relayd_sock
)
543 struct lttcomm_relayd_sock
*rsock
;
545 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
546 RELAYD_VERSION_COMM_MINOR
);
548 ret
= LTTNG_ERR_FATAL
;
553 * Connect to relayd so we can proceed with a session creation. This call
554 * can possibly block for an arbitrary amount of time to set the health
555 * state to be in poll execution.
558 ret
= relayd_connect(rsock
);
561 ERR("Unable to reach lttng-relayd");
562 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
566 /* Create socket for control stream. */
567 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
568 DBG3("Creating relayd stream socket from URI");
570 /* Check relayd version */
571 ret
= relayd_version_check(rsock
);
573 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
576 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
577 DBG3("Creating relayd data socket from URI");
579 /* Command is not valid */
580 ERR("Relayd invalid stream type: %d", uri
->stype
);
581 ret
= LTTNG_ERR_INVALID
;
585 *relayd_sock
= rsock
;
590 /* The returned value is not useful since we are on an error path. */
591 (void) relayd_close(rsock
);
599 * Connect to the relayd using URI and send the socket to the right consumer.
601 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
602 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
603 struct consumer_socket
*consumer_sock
,
604 char *session_name
, char *hostname
, int session_live_timer
)
607 struct lttcomm_relayd_sock
*rsock
= NULL
;
609 /* Connect to relayd and make version check if uri is the control. */
610 ret
= create_connect_relayd(relayd_uri
, &rsock
);
611 if (ret
!= LTTNG_OK
) {
616 /* Set the network sequence index if not set. */
617 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
618 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
620 * Increment net_seq_idx because we are about to transfer the
621 * new relayd socket to the consumer.
622 * Assign unique key so the consumer can match streams.
624 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
625 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
628 /* Send relayd socket to consumer. */
629 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
630 relayd_uri
->stype
, session_id
,
631 session_name
, hostname
, session_live_timer
);
633 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
637 /* Flag that the corresponding socket was sent. */
638 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
639 consumer_sock
->control_sock_sent
= 1;
640 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
641 consumer_sock
->data_sock_sent
= 1;
647 * Close socket which was dup on the consumer side. The session daemon does
648 * NOT keep track of the relayd socket(s) once transfer to the consumer.
652 (void) relayd_close(rsock
);
656 if (ret
!= LTTNG_OK
) {
658 * The consumer output for this session should not be used anymore
659 * since the relayd connection failed thus making any tracing or/and
660 * streaming not usable.
662 consumer
->enabled
= 0;
668 * Send both relayd sockets to a specific consumer and domain. This is a
669 * helper function to facilitate sending the information to the consumer for a
672 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
673 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
674 char *session_name
, char *hostname
, int session_live_timer
)
681 /* Sending control relayd socket. */
682 if (!sock
->control_sock_sent
) {
683 ret
= send_consumer_relayd_socket(domain
, session_id
,
684 &consumer
->dst
.net
.control
, consumer
, sock
,
685 session_name
, hostname
, session_live_timer
);
686 if (ret
!= LTTNG_OK
) {
691 /* Sending data relayd socket. */
692 if (!sock
->data_sock_sent
) {
693 ret
= send_consumer_relayd_socket(domain
, session_id
,
694 &consumer
->dst
.net
.data
, consumer
, sock
,
695 session_name
, hostname
, session_live_timer
);
696 if (ret
!= LTTNG_OK
) {
706 * Setup relayd connections for a tracing session. First creates the socket to
707 * the relayd and send them to the right domain consumer. Consumer type MUST be
710 int cmd_setup_relayd(struct ltt_session
*session
)
713 struct ltt_ust_session
*usess
;
714 struct ltt_kernel_session
*ksess
;
715 struct consumer_socket
*socket
;
716 struct lttng_ht_iter iter
;
720 usess
= session
->ust_session
;
721 ksess
= session
->kernel_session
;
723 DBG("Setting relayd for session %s", session
->name
);
727 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
728 && usess
->consumer
->enabled
) {
729 /* For each consumer socket, send relayd sockets */
730 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
732 pthread_mutex_lock(socket
->lock
);
733 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
734 usess
->consumer
, socket
,
735 session
->name
, session
->hostname
,
736 session
->live_timer
);
737 pthread_mutex_unlock(socket
->lock
);
738 if (ret
!= LTTNG_OK
) {
741 /* Session is now ready for network streaming. */
742 session
->net_handle
= 1;
746 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
747 && ksess
->consumer
->enabled
) {
748 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
750 pthread_mutex_lock(socket
->lock
);
751 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
752 ksess
->consumer
, socket
,
753 session
->name
, session
->hostname
,
754 session
->live_timer
);
755 pthread_mutex_unlock(socket
->lock
);
756 if (ret
!= LTTNG_OK
) {
759 /* Session is now ready for network streaming. */
760 session
->net_handle
= 1;
770 * Start a kernel session by opening all necessary streams.
772 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
775 struct ltt_kernel_channel
*kchan
;
777 /* Open kernel metadata */
778 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
779 ret
= kernel_open_metadata(ksess
);
781 ret
= LTTNG_ERR_KERN_META_FAIL
;
786 /* Open kernel metadata stream */
787 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
788 ret
= kernel_open_metadata_stream(ksess
);
790 ERR("Kernel create metadata stream failed");
791 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
796 /* For each channel */
797 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
798 if (kchan
->stream_count
== 0) {
799 ret
= kernel_open_channel_stream(kchan
);
801 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
804 /* Update the stream global counter */
805 ksess
->stream_count_global
+= ret
;
809 /* Setup kernel consumer socket and send fds to it */
810 ret
= init_kernel_tracing(ksess
);
812 ret
= LTTNG_ERR_KERN_START_FAIL
;
816 /* This start the kernel tracing */
817 ret
= kernel_start_session(ksess
);
819 ret
= LTTNG_ERR_KERN_START_FAIL
;
823 /* Quiescent wait after starting trace */
824 kernel_wait_quiescent(kernel_tracer_fd
);
835 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
837 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
841 struct ltt_ust_session
*usess
;
843 usess
= session
->ust_session
;
848 case LTTNG_DOMAIN_KERNEL
:
850 ret
= channel_kernel_disable(session
->kernel_session
,
852 if (ret
!= LTTNG_OK
) {
856 kernel_wait_quiescent(kernel_tracer_fd
);
859 case LTTNG_DOMAIN_UST
:
861 struct ltt_ust_channel
*uchan
;
862 struct lttng_ht
*chan_ht
;
864 chan_ht
= usess
->domain_global
.channels
;
866 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
868 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
872 ret
= channel_ust_disable(usess
, uchan
);
873 if (ret
!= LTTNG_OK
) {
879 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
880 case LTTNG_DOMAIN_UST_EXEC_NAME
:
881 case LTTNG_DOMAIN_UST_PID
:
884 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
896 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
898 * The wpipe arguments is used as a notifier for the kernel thread.
900 int cmd_enable_channel(struct ltt_session
*session
,
901 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
904 struct ltt_ust_session
*usess
= session
->ust_session
;
905 struct lttng_ht
*chan_ht
;
911 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
916 * Don't try to enable a channel if the session has been started at
917 * some point in time before. The tracer does not allow it.
919 if (session
->has_been_started
) {
920 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
925 * If the session is a live session, remove the switch timer, the
926 * live timer does the same thing but sends also synchronisation
927 * beacons for inactive streams.
929 if (session
->live_timer
> 0) {
930 attr
->attr
.live_timer_interval
= session
->live_timer
;
931 attr
->attr
.switch_timer_interval
= 0;
934 switch (domain
->type
) {
935 case LTTNG_DOMAIN_KERNEL
:
937 struct ltt_kernel_channel
*kchan
;
939 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
940 session
->kernel_session
);
942 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
943 if (attr
->name
[0] != '\0') {
944 session
->kernel_session
->has_non_default_channel
= 1;
947 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
950 if (ret
!= LTTNG_OK
) {
954 kernel_wait_quiescent(kernel_tracer_fd
);
957 case LTTNG_DOMAIN_UST
:
959 struct ltt_ust_channel
*uchan
;
961 chan_ht
= usess
->domain_global
.channels
;
963 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
965 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
966 if (attr
->name
[0] != '\0') {
967 usess
->has_non_default_channel
= 1;
970 ret
= channel_ust_enable(usess
, uchan
);
975 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
986 * Command LTTNG_DISABLE_EVENT processed by the client thread.
988 int cmd_disable_event(struct ltt_session
*session
, int domain
,
989 char *channel_name
, char *event_name
)
996 case LTTNG_DOMAIN_KERNEL
:
998 struct ltt_kernel_channel
*kchan
;
999 struct ltt_kernel_session
*ksess
;
1001 ksess
= session
->kernel_session
;
1004 * If a non-default channel has been created in the
1005 * session, explicitely require that -c chan_name needs
1008 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1009 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1013 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1014 if (kchan
== NULL
) {
1015 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1019 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1020 if (ret
!= LTTNG_OK
) {
1024 kernel_wait_quiescent(kernel_tracer_fd
);
1027 case LTTNG_DOMAIN_UST
:
1029 struct ltt_ust_channel
*uchan
;
1030 struct ltt_ust_session
*usess
;
1032 usess
= session
->ust_session
;
1035 * If a non-default channel has been created in the
1036 * session, explicitely require that -c chan_name needs
1039 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1040 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1044 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1046 if (uchan
== NULL
) {
1047 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1051 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1052 if (ret
!= LTTNG_OK
) {
1056 DBG3("Disable UST event %s in channel %s completed", event_name
,
1060 case LTTNG_DOMAIN_JUL
:
1062 struct ltt_ust_session
*usess
= session
->ust_session
;
1066 ret
= event_jul_disable(usess
, event_name
);
1067 if (ret
!= LTTNG_OK
) {
1074 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1075 case LTTNG_DOMAIN_UST_PID
:
1076 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1079 ret
= LTTNG_ERR_UND
;
1091 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
1093 int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
1101 case LTTNG_DOMAIN_KERNEL
:
1103 struct ltt_kernel_session
*ksess
;
1104 struct ltt_kernel_channel
*kchan
;
1106 ksess
= session
->kernel_session
;
1109 * If a non-default channel has been created in the
1110 * session, explicitely require that -c chan_name needs
1113 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1114 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1118 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1119 if (kchan
== NULL
) {
1120 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1124 ret
= event_kernel_disable_all(kchan
);
1125 if (ret
!= LTTNG_OK
) {
1129 kernel_wait_quiescent(kernel_tracer_fd
);
1132 case LTTNG_DOMAIN_UST
:
1134 struct ltt_ust_session
*usess
;
1135 struct ltt_ust_channel
*uchan
;
1137 usess
= session
->ust_session
;
1140 * If a non-default channel has been created in the
1141 * session, explicitely require that -c chan_name needs
1144 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1145 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1149 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1151 if (uchan
== NULL
) {
1152 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1156 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1161 DBG3("Disable all UST events in channel %s completed", channel_name
);
1165 case LTTNG_DOMAIN_JUL
:
1167 struct ltt_ust_session
*usess
= session
->ust_session
;
1171 ret
= event_jul_disable_all(usess
);
1172 if (ret
!= LTTNG_OK
) {
1179 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1180 case LTTNG_DOMAIN_UST_PID
:
1181 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1184 ret
= LTTNG_ERR_UND
;
1196 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1198 int cmd_add_context(struct ltt_session
*session
, int domain
,
1199 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1201 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1204 case LTTNG_DOMAIN_KERNEL
:
1205 assert(session
->kernel_session
);
1207 if (session
->kernel_session
->channel_count
== 0) {
1208 /* Create default channel */
1209 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1210 if (ret
!= LTTNG_OK
) {
1213 chan_kern_created
= 1;
1215 /* Add kernel context to kernel tracer */
1216 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1217 if (ret
!= LTTNG_OK
) {
1221 case LTTNG_DOMAIN_UST
:
1223 struct ltt_ust_session
*usess
= session
->ust_session
;
1224 unsigned int chan_count
;
1228 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1229 if (chan_count
== 0) {
1230 struct lttng_channel
*attr
;
1231 /* Create default channel */
1232 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1234 ret
= LTTNG_ERR_FATAL
;
1238 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1239 if (ret
!= LTTNG_OK
) {
1244 chan_ust_created
= 1;
1247 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1248 if (ret
!= LTTNG_OK
) {
1254 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1255 case LTTNG_DOMAIN_UST_PID
:
1256 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1259 ret
= LTTNG_ERR_UND
;
1266 if (chan_kern_created
) {
1267 struct ltt_kernel_channel
*kchan
=
1268 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1269 session
->kernel_session
);
1270 /* Created previously, this should NOT fail. */
1272 kernel_destroy_channel(kchan
);
1275 if (chan_ust_created
) {
1276 struct ltt_ust_channel
*uchan
=
1277 trace_ust_find_channel_by_name(
1278 session
->ust_session
->domain_global
.channels
,
1279 DEFAULT_CHANNEL_NAME
);
1280 /* Created previously, this should NOT fail. */
1282 /* Remove from the channel list of the session. */
1283 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1285 trace_ust_destroy_channel(uchan
);
1291 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1293 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1294 char *channel_name
, struct lttng_event
*event
,
1295 char *filter_expression
,
1296 struct lttng_filter_bytecode
*filter
,
1297 struct lttng_event_exclusion
*exclusion
,
1300 int ret
, channel_created
= 0;
1301 struct lttng_channel
*attr
;
1305 assert(channel_name
);
1309 switch (domain
->type
) {
1310 case LTTNG_DOMAIN_KERNEL
:
1312 struct ltt_kernel_channel
*kchan
;
1315 * If a non-default channel has been created in the
1316 * session, explicitely require that -c chan_name needs
1319 if (session
->kernel_session
->has_non_default_channel
1320 && channel_name
[0] == '\0') {
1321 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1325 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1326 session
->kernel_session
);
1327 if (kchan
== NULL
) {
1328 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1329 LTTNG_BUFFER_GLOBAL
);
1331 ret
= LTTNG_ERR_FATAL
;
1334 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1336 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1337 if (ret
!= LTTNG_OK
) {
1343 channel_created
= 1;
1346 /* Get the newly created kernel channel pointer */
1347 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1348 session
->kernel_session
);
1349 if (kchan
== NULL
) {
1350 /* This sould not happen... */
1351 ret
= LTTNG_ERR_FATAL
;
1355 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1356 if (ret
!= LTTNG_OK
) {
1357 if (channel_created
) {
1358 /* Let's not leak a useless channel. */
1359 kernel_destroy_channel(kchan
);
1364 kernel_wait_quiescent(kernel_tracer_fd
);
1367 case LTTNG_DOMAIN_UST
:
1369 struct ltt_ust_channel
*uchan
;
1370 struct ltt_ust_session
*usess
= session
->ust_session
;
1375 * If a non-default channel has been created in the
1376 * session, explicitely require that -c chan_name needs
1379 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1380 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1384 /* Get channel from global UST domain */
1385 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1387 if (uchan
== NULL
) {
1388 /* Create default channel */
1389 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1390 usess
->buffer_type
);
1392 ret
= LTTNG_ERR_FATAL
;
1395 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1397 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1398 if (ret
!= LTTNG_OK
) {
1404 /* Get the newly created channel reference back */
1405 uchan
= trace_ust_find_channel_by_name(
1406 usess
->domain_global
.channels
, channel_name
);
1410 /* At this point, the session and channel exist on the tracer */
1411 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1412 filter_expression
, filter
, exclusion
);
1413 if (ret
!= LTTNG_OK
) {
1418 case LTTNG_DOMAIN_JUL
:
1420 struct lttng_event uevent
;
1421 struct lttng_domain tmp_dom
;
1422 struct ltt_ust_session
*usess
= session
->ust_session
;
1426 /* Create the default JUL tracepoint. */
1427 memset(&uevent
, 0, sizeof(uevent
));
1428 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1429 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1431 strncpy(uevent
.name
, DEFAULT_SYS_JUL_EVENT_NAME
,
1432 sizeof(uevent
.name
));
1434 strncpy(uevent
.name
, DEFAULT_USER_JUL_EVENT_NAME
,
1435 sizeof(uevent
.name
));
1437 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1440 * The domain type is changed because we are about to enable the
1441 * default channel and event for the JUL domain that are hardcoded.
1442 * This happens in the UST domain.
1444 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1445 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1447 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1448 &uevent
, NULL
, NULL
, NULL
, wpipe
);
1449 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1453 /* The wild card * means that everything should be enabled. */
1454 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1455 ret
= event_jul_enable_all(usess
, event
);
1457 ret
= event_jul_enable(usess
, event
);
1459 if (ret
!= LTTNG_OK
) {
1466 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1467 case LTTNG_DOMAIN_UST_PID
:
1468 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1471 ret
= LTTNG_ERR_UND
;
1483 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1485 int cmd_enable_event_all(struct ltt_session
*session
,
1486 struct lttng_domain
*domain
, char *channel_name
, int event_type
,
1487 char *filter_expression
,
1488 struct lttng_filter_bytecode
*filter
, int wpipe
)
1491 struct lttng_channel
*attr
;
1494 assert(channel_name
);
1498 switch (domain
->type
) {
1499 case LTTNG_DOMAIN_KERNEL
:
1501 struct ltt_kernel_channel
*kchan
;
1503 assert(session
->kernel_session
);
1506 * If a non-default channel has been created in the
1507 * session, explicitely require that -c chan_name needs
1510 if (session
->kernel_session
->has_non_default_channel
1511 && channel_name
[0] == '\0') {
1512 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1516 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1517 session
->kernel_session
);
1518 if (kchan
== NULL
) {
1519 /* Create default channel */
1520 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1521 LTTNG_BUFFER_GLOBAL
);
1523 ret
= LTTNG_ERR_FATAL
;
1526 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1528 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1529 if (ret
!= LTTNG_OK
) {
1535 /* Get the newly created kernel channel pointer */
1536 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1537 session
->kernel_session
);
1541 switch (event_type
) {
1542 case LTTNG_EVENT_SYSCALL
:
1543 ret
= event_kernel_enable_all_syscalls(kchan
, kernel_tracer_fd
);
1545 case LTTNG_EVENT_TRACEPOINT
:
1547 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1548 * events already registered to the channel.
1550 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1552 case LTTNG_EVENT_ALL
:
1553 /* Enable syscalls and tracepoints */
1554 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1557 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1561 /* Manage return value */
1562 if (ret
!= LTTNG_OK
) {
1564 * On error, cmd_enable_channel call will take care of destroying
1565 * the created channel if it was needed.
1570 kernel_wait_quiescent(kernel_tracer_fd
);
1573 case LTTNG_DOMAIN_UST
:
1575 struct ltt_ust_channel
*uchan
;
1576 struct ltt_ust_session
*usess
= session
->ust_session
;
1581 * If a non-default channel has been created in the
1582 * session, explicitely require that -c chan_name needs
1585 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1586 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1590 /* Get channel from global UST domain */
1591 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1593 if (uchan
== NULL
) {
1594 /* Create default channel */
1595 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1596 usess
->buffer_type
);
1598 ret
= LTTNG_ERR_FATAL
;
1601 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1603 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1604 if (ret
!= LTTNG_OK
) {
1610 /* Get the newly created channel reference back */
1611 uchan
= trace_ust_find_channel_by_name(
1612 usess
->domain_global
.channels
, channel_name
);
1616 /* At this point, the session and channel exist on the tracer */
1618 switch (event_type
) {
1619 case LTTNG_EVENT_ALL
:
1620 case LTTNG_EVENT_TRACEPOINT
:
1621 ret
= event_ust_enable_all_tracepoints(usess
, uchan
,
1622 filter_expression
, filter
);
1623 if (ret
!= LTTNG_OK
) {
1628 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
1632 /* Manage return value */
1633 if (ret
!= LTTNG_OK
) {
1639 case LTTNG_DOMAIN_JUL
:
1641 struct lttng_event uevent
, event
;
1642 struct lttng_domain tmp_dom
;
1643 struct ltt_ust_session
*usess
= session
->ust_session
;
1647 /* Create the default JUL tracepoint. */
1648 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1649 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1651 strncpy(uevent
.name
, DEFAULT_SYS_JUL_EVENT_NAME
,
1652 sizeof(uevent
.name
));
1654 strncpy(uevent
.name
, DEFAULT_USER_JUL_EVENT_NAME
,
1655 sizeof(uevent
.name
));
1657 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1660 * The domain type is changed because we are about to enable the
1661 * default channel and event for the JUL domain that are hardcoded.
1662 * This happens in the UST domain.
1664 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1665 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1667 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1668 &uevent
, NULL
, NULL
, NULL
, wpipe
);
1669 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1673 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1674 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1675 strncpy(event
.name
, "*", sizeof(event
.name
));
1676 event
.name
[sizeof(event
.name
) - 1] = '\0';
1678 ret
= event_jul_enable_all(usess
, &event
);
1679 if (ret
!= LTTNG_OK
) {
1686 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1687 case LTTNG_DOMAIN_UST_PID
:
1688 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1691 ret
= LTTNG_ERR_UND
;
1704 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1706 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1709 ssize_t nb_events
= 0;
1712 case LTTNG_DOMAIN_KERNEL
:
1713 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1714 if (nb_events
< 0) {
1715 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1719 case LTTNG_DOMAIN_UST
:
1720 nb_events
= ust_app_list_events(events
);
1721 if (nb_events
< 0) {
1722 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1726 case LTTNG_DOMAIN_JUL
:
1727 nb_events
= jul_list_events(events
);
1728 if (nb_events
< 0) {
1729 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1734 ret
= LTTNG_ERR_UND
;
1741 /* Return negative value to differentiate return code */
1746 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1748 ssize_t
cmd_list_tracepoint_fields(int domain
,
1749 struct lttng_event_field
**fields
)
1752 ssize_t nb_fields
= 0;
1755 case LTTNG_DOMAIN_UST
:
1756 nb_fields
= ust_app_list_event_fields(fields
);
1757 if (nb_fields
< 0) {
1758 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1762 case LTTNG_DOMAIN_KERNEL
:
1763 default: /* fall-through */
1764 ret
= LTTNG_ERR_UND
;
1771 /* Return negative value to differentiate return code */
1776 * Command LTTNG_START_TRACE processed by the client thread.
1778 int cmd_start_trace(struct ltt_session
*session
)
1781 unsigned long nb_chan
= 0;
1782 struct ltt_kernel_session
*ksession
;
1783 struct ltt_ust_session
*usess
;
1787 /* Ease our life a bit ;) */
1788 ksession
= session
->kernel_session
;
1789 usess
= session
->ust_session
;
1791 /* Is the session already started? */
1792 if (session
->active
) {
1793 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1798 * Starting a session without channel is useless since after that it's not
1799 * possible to enable channel thus inform the client.
1801 if (usess
&& usess
->domain_global
.channels
) {
1802 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1805 nb_chan
+= ksession
->channel_count
;
1808 ret
= LTTNG_ERR_NO_CHANNEL
;
1812 /* Kernel tracing */
1813 if (ksession
!= NULL
) {
1814 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1815 if (ret
!= LTTNG_OK
) {
1820 /* Flag session that trace should start automatically */
1822 usess
->start_trace
= 1;
1824 ret
= ust_app_start_trace_all(usess
);
1826 ret
= LTTNG_ERR_UST_START_FAIL
;
1831 /* Flag this after a successful start. */
1832 session
->has_been_started
= 1;
1833 session
->active
= 1;
1842 * Command LTTNG_STOP_TRACE processed by the client thread.
1844 int cmd_stop_trace(struct ltt_session
*session
)
1847 struct ltt_kernel_channel
*kchan
;
1848 struct ltt_kernel_session
*ksession
;
1849 struct ltt_ust_session
*usess
;
1854 ksession
= session
->kernel_session
;
1855 usess
= session
->ust_session
;
1857 /* Session is not active. Skip everythong and inform the client. */
1858 if (!session
->active
) {
1859 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1864 if (ksession
&& ksession
->started
) {
1865 DBG("Stop kernel tracing");
1867 /* Flush metadata if exist */
1868 if (ksession
->metadata_stream_fd
>= 0) {
1869 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1871 ERR("Kernel metadata flush failed");
1875 /* Flush all buffers before stopping */
1876 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1877 ret
= kernel_flush_buffer(kchan
);
1879 ERR("Kernel flush buffer error");
1883 ret
= kernel_stop_session(ksession
);
1885 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1889 kernel_wait_quiescent(kernel_tracer_fd
);
1891 ksession
->started
= 0;
1894 if (usess
&& usess
->start_trace
) {
1895 usess
->start_trace
= 0;
1897 ret
= ust_app_stop_trace_all(usess
);
1899 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1904 /* Flag inactive after a successful stop. */
1905 session
->active
= 0;
1913 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1915 int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
1916 size_t nb_uri
, struct lttng_uri
*uris
)
1919 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1920 struct ltt_ust_session
*usess
= session
->ust_session
;
1921 struct consumer_output
*consumer
= NULL
;
1927 /* Can't set consumer URI if the session is active. */
1928 if (session
->active
) {
1929 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1934 * This case switch makes sure the domain session has a temporary consumer
1935 * so the URL can be set.
1939 /* Code flow error. A session MUST always have a consumer object */
1940 assert(session
->consumer
);
1942 * The URL will be added to the tracing session consumer instead of a
1943 * specific domain consumer.
1945 consumer
= session
->consumer
;
1947 case LTTNG_DOMAIN_KERNEL
:
1948 /* Code flow error if we don't have a kernel session here. */
1950 assert(ksess
->consumer
);
1951 consumer
= ksess
->consumer
;
1953 case LTTNG_DOMAIN_UST
:
1954 /* Code flow error if we don't have a kernel session here. */
1956 assert(usess
->consumer
);
1957 consumer
= usess
->consumer
;
1961 for (i
= 0; i
< nb_uri
; i
++) {
1962 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
1963 if (ret
!= LTTNG_OK
) {
1976 * Command LTTNG_CREATE_SESSION processed by the client thread.
1978 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
1979 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
1982 struct ltt_session
*session
;
1988 * Verify if the session already exist
1990 * XXX: There is no need for the session lock list here since the caller
1991 * (process_client_msg) is holding it. We might want to change that so a
1992 * single command does not lock the entire session list.
1994 session
= session_find_by_name(name
);
1995 if (session
!= NULL
) {
1996 ret
= LTTNG_ERR_EXIST_SESS
;
2000 /* Create tracing session in the registry */
2001 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2002 LTTNG_SOCK_GET_GID_CRED(creds
));
2003 if (ret
!= LTTNG_OK
) {
2008 * Get the newly created session pointer back
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
);
2017 session
->live_timer
= live_timer
;
2018 /* Create default consumer output for the session not yet created. */
2019 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2020 if (session
->consumer
== NULL
) {
2021 ret
= LTTNG_ERR_FATAL
;
2022 goto consumer_error
;
2026 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
2027 if (ret
!= LTTNG_OK
) {
2028 goto consumer_error
;
2030 session
->output_traces
= 1;
2032 session
->output_traces
= 0;
2033 DBG2("Session %s created with no output", session
->name
);
2036 session
->consumer
->enabled
= 1;
2041 session_destroy(session
);
2048 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2050 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2051 size_t nb_uri
, lttng_sock_cred
*creds
)
2054 struct ltt_session
*session
;
2055 struct snapshot_output
*new_output
= NULL
;
2061 * Create session in no output mode with URIs set to NULL. The uris we've
2062 * received are for a default snapshot output if one.
2064 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2065 if (ret
!= LTTNG_OK
) {
2069 /* Get the newly created session pointer back. This should NEVER fail. */
2070 session
= session_find_by_name(name
);
2073 /* Flag session for snapshot mode. */
2074 session
->snapshot_mode
= 1;
2076 /* Skip snapshot output creation if no URI is given. */
2081 new_output
= snapshot_output_alloc();
2083 ret
= LTTNG_ERR_NOMEM
;
2084 goto error_snapshot_alloc
;
2087 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2088 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2090 if (ret
== -ENOMEM
) {
2091 ret
= LTTNG_ERR_NOMEM
;
2093 ret
= LTTNG_ERR_INVALID
;
2095 goto error_snapshot
;
2099 snapshot_add_output(&session
->snapshot
, new_output
);
2106 snapshot_output_destroy(new_output
);
2107 error_snapshot_alloc
:
2108 session_destroy(session
);
2114 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2116 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2119 struct ltt_ust_session
*usess
;
2120 struct ltt_kernel_session
*ksess
;
2125 usess
= session
->ust_session
;
2126 ksess
= session
->kernel_session
;
2128 /* Clean kernel session teardown */
2129 kernel_destroy_session(ksess
);
2131 /* UST session teardown */
2133 /* Close any relayd session */
2134 consumer_output_send_destroy_relayd(usess
->consumer
);
2136 /* Destroy every UST application related to this session. */
2137 ret
= ust_app_destroy_trace_all(usess
);
2139 ERR("Error in ust_app_destroy_trace_all");
2142 /* Clean up the rest. */
2143 trace_ust_destroy_session(usess
);
2147 * Must notify the kernel thread here to update it's poll set in order to
2148 * remove the channel(s)' fd just destroyed.
2150 ret
= notify_thread_pipe(wpipe
);
2152 PERROR("write kernel poll pipe");
2155 ret
= session_destroy(session
);
2161 * Command LTTNG_CALIBRATE processed by the client thread.
2163 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2168 case LTTNG_DOMAIN_KERNEL
:
2170 struct lttng_kernel_calibrate kcalibrate
;
2172 switch (calibrate
->type
) {
2173 case LTTNG_CALIBRATE_FUNCTION
:
2175 /* Default and only possible calibrate option. */
2176 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2180 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2182 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2187 case LTTNG_DOMAIN_UST
:
2189 struct lttng_ust_calibrate ucalibrate
;
2191 switch (calibrate
->type
) {
2192 case LTTNG_CALIBRATE_FUNCTION
:
2194 /* Default and only possible calibrate option. */
2195 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2199 ret
= ust_app_calibrate_glb(&ucalibrate
);
2201 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2207 ret
= LTTNG_ERR_UND
;
2218 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2220 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2221 const char *sock_path
, struct consumer_data
*cdata
)
2224 struct consumer_socket
*socket
= NULL
;
2231 case LTTNG_DOMAIN_KERNEL
:
2233 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2237 /* Can't register a consumer if there is already one */
2238 if (ksess
->consumer_fds_sent
!= 0) {
2239 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2243 sock
= lttcomm_connect_unix_sock(sock_path
);
2245 ret
= LTTNG_ERR_CONNECT_FAIL
;
2248 cdata
->cmd_sock
= sock
;
2250 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2251 if (socket
== NULL
) {
2254 PERROR("close register consumer");
2256 cdata
->cmd_sock
= -1;
2257 ret
= LTTNG_ERR_FATAL
;
2261 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2262 if (socket
->lock
== NULL
) {
2263 PERROR("zmalloc pthread mutex");
2264 ret
= LTTNG_ERR_FATAL
;
2267 pthread_mutex_init(socket
->lock
, NULL
);
2268 socket
->registered
= 1;
2271 consumer_add_socket(socket
, ksess
->consumer
);
2274 pthread_mutex_lock(&cdata
->pid_mutex
);
2276 pthread_mutex_unlock(&cdata
->pid_mutex
);
2281 /* TODO: Userspace tracing */
2282 ret
= LTTNG_ERR_UND
;
2290 consumer_destroy_socket(socket
);
2296 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2298 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2299 struct lttng_domain
**domains
)
2304 if (session
->kernel_session
!= NULL
) {
2305 DBG3("Listing domains found kernel domain");
2309 if (session
->ust_session
!= NULL
) {
2310 DBG3("Listing domains found UST global domain");
2313 if (session
->ust_session
->domain_jul
.being_used
) {
2318 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2319 if (*domains
== NULL
) {
2320 ret
= LTTNG_ERR_FATAL
;
2324 if (session
->kernel_session
!= NULL
) {
2325 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2329 if (session
->ust_session
!= NULL
) {
2330 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2331 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2334 if (session
->ust_session
->domain_jul
.being_used
) {
2335 (*domains
)[index
].type
= LTTNG_DOMAIN_JUL
;
2336 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2344 /* Return negative value to differentiate return code */
2350 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2352 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2353 struct lttng_channel
**channels
)
2356 ssize_t nb_chan
= 0;
2359 case LTTNG_DOMAIN_KERNEL
:
2360 if (session
->kernel_session
!= NULL
) {
2361 nb_chan
= session
->kernel_session
->channel_count
;
2363 DBG3("Number of kernel channels %zd", nb_chan
);
2365 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2368 case LTTNG_DOMAIN_UST
:
2369 if (session
->ust_session
!= NULL
) {
2370 nb_chan
= lttng_ht_get_count(
2371 session
->ust_session
->domain_global
.channels
);
2373 DBG3("Number of UST global channels %zd", nb_chan
);
2375 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2380 ret
= LTTNG_ERR_UND
;
2385 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2386 if (*channels
== NULL
) {
2387 ret
= LTTNG_ERR_FATAL
;
2391 list_lttng_channels(domain
, session
, *channels
);
2394 /* Ret value was set in the domain switch case */
2401 /* Return negative value to differentiate return code */
2406 * Command LTTNG_LIST_EVENTS processed by the client thread.
2408 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2409 char *channel_name
, struct lttng_event
**events
)
2412 ssize_t nb_event
= 0;
2415 case LTTNG_DOMAIN_KERNEL
:
2416 if (session
->kernel_session
!= NULL
) {
2417 nb_event
= list_lttng_kernel_events(channel_name
,
2418 session
->kernel_session
, events
);
2421 case LTTNG_DOMAIN_UST
:
2423 if (session
->ust_session
!= NULL
) {
2424 nb_event
= list_lttng_ust_global_events(channel_name
,
2425 &session
->ust_session
->domain_global
, events
);
2429 case LTTNG_DOMAIN_JUL
:
2430 if (session
->ust_session
) {
2431 nb_event
= list_lttng_jul_events(
2432 &session
->ust_session
->domain_jul
, events
);
2436 ret
= LTTNG_ERR_UND
;
2443 /* Return negative value to differentiate return code */
2448 * Using the session list, filled a lttng_session array to send back to the
2449 * client for session listing.
2451 * The session list lock MUST be acquired before calling this function. Use
2452 * session_lock_list() and session_unlock_list().
2454 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2459 struct ltt_session
*session
;
2460 struct ltt_session_list
*list
= session_get_list();
2462 DBG("Getting all available session for UID %d GID %d",
2465 * Iterate over session list and append data after the control struct in
2468 cds_list_for_each_entry(session
, &list
->head
, list
) {
2470 * Only list the sessions the user can control.
2472 if (!session_access_ok(session
, uid
, gid
)) {
2476 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2477 struct ltt_ust_session
*usess
= session
->ust_session
;
2479 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2480 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2481 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2482 ret
= build_network_session_path(sessions
[i
].path
,
2483 sizeof(sessions
[i
].path
), session
);
2485 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2486 session
->consumer
->dst
.trace_path
);
2489 PERROR("snprintf session path");
2493 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2494 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2495 sessions
[i
].enabled
= session
->active
;
2496 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2497 sessions
[i
].live_timer_interval
= session
->live_timer
;
2503 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2504 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2506 int cmd_data_pending(struct ltt_session
*session
)
2509 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2510 struct ltt_ust_session
*usess
= session
->ust_session
;
2514 /* Session MUST be stopped to ask for data availability. */
2515 if (session
->active
) {
2516 ret
= LTTNG_ERR_SESSION_STARTED
;
2520 * If stopped, just make sure we've started before else the above call
2521 * will always send that there is data pending.
2523 * The consumer assumes that when the data pending command is received,
2524 * the trace has been started before or else no output data is written
2525 * by the streams which is a condition for data pending. So, this is
2526 * *VERY* important that we don't ask the consumer before a start
2529 if (!session
->has_been_started
) {
2535 if (ksess
&& ksess
->consumer
) {
2536 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2538 /* Data is still being extracted for the kernel. */
2543 if (usess
&& usess
->consumer
) {
2544 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2546 /* Data is still being extracted for the kernel. */
2551 /* Data is ready to be read by a viewer */
2559 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2561 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2563 int cmd_snapshot_add_output(struct ltt_session
*session
,
2564 struct lttng_snapshot_output
*output
, uint32_t *id
)
2567 struct snapshot_output
*new_output
;
2572 DBG("Cmd snapshot add output for session %s", session
->name
);
2575 * Permission denied to create an output if the session is not
2576 * set in no output mode.
2578 if (session
->output_traces
) {
2579 ret
= LTTNG_ERR_EPERM
;
2583 /* Only one output is allowed until we have the "tee" feature. */
2584 if (session
->snapshot
.nb_output
== 1) {
2585 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2589 new_output
= snapshot_output_alloc();
2591 ret
= LTTNG_ERR_NOMEM
;
2595 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2596 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2597 &session
->snapshot
);
2599 if (ret
== -ENOMEM
) {
2600 ret
= LTTNG_ERR_NOMEM
;
2602 ret
= LTTNG_ERR_INVALID
;
2608 snapshot_add_output(&session
->snapshot
, new_output
);
2610 *id
= new_output
->id
;
2617 snapshot_output_destroy(new_output
);
2623 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2625 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2627 int cmd_snapshot_del_output(struct ltt_session
*session
,
2628 struct lttng_snapshot_output
*output
)
2631 struct snapshot_output
*sout
= NULL
;
2639 * Permission denied to create an output if the session is not
2640 * set in no output mode.
2642 if (session
->output_traces
) {
2643 ret
= LTTNG_ERR_EPERM
;
2648 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2650 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2651 } else if (*output
->name
!= '\0') {
2652 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2654 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2657 ret
= LTTNG_ERR_INVALID
;
2661 snapshot_delete_output(&session
->snapshot
, sout
);
2662 snapshot_output_destroy(sout
);
2671 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2673 * If no output is available, outputs is untouched and 0 is returned.
2675 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2677 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2678 struct lttng_snapshot_output
**outputs
)
2681 struct lttng_snapshot_output
*list
;
2682 struct lttng_ht_iter iter
;
2683 struct snapshot_output
*output
;
2688 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2691 * Permission denied to create an output if the session is not
2692 * set in no output mode.
2694 if (session
->output_traces
) {
2695 ret
= LTTNG_ERR_EPERM
;
2699 if (session
->snapshot
.nb_output
== 0) {
2704 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2706 ret
= LTTNG_ERR_NOMEM
;
2710 /* Copy list from session to the new list object. */
2711 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2712 output
, node
.node
) {
2713 assert(output
->consumer
);
2714 list
[idx
].id
= output
->id
;
2715 list
[idx
].max_size
= output
->max_size
;
2716 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2717 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2718 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2719 sizeof(list
[idx
].ctrl_url
));
2722 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2723 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2725 ret
= LTTNG_ERR_NOMEM
;
2730 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2731 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2733 ret
= LTTNG_ERR_NOMEM
;
2741 return session
->snapshot
.nb_output
;
2750 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2751 * snapshot output is *not* set with a remote destination.
2753 * Return 0 on success or a LTTNG_ERR code.
2755 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2756 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2759 struct lttng_ht_iter iter
;
2760 struct consumer_socket
*socket
;
2763 assert(snap_output
);
2766 DBG2("Set relayd object from snapshot output");
2768 /* Ignore if snapshot consumer output is not network. */
2769 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2774 * For each consumer socket, create and send the relayd object of the
2778 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2779 socket
, node
.node
) {
2780 ret
= send_consumer_relayd_sockets(0, session
->id
,
2781 snap_output
->consumer
, socket
,
2782 session
->name
, session
->hostname
,
2783 session
->live_timer
);
2784 if (ret
!= LTTNG_OK
) {
2796 * Record a kernel snapshot.
2798 * Return LTTNG_OK on success or a LTTNG_ERR code.
2800 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2801 struct snapshot_output
*output
, struct ltt_session
*session
,
2802 int wait
, int nb_streams
)
2810 /* Get the datetime for the snapshot output directory. */
2811 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2812 sizeof(output
->datetime
));
2814 ret
= LTTNG_ERR_INVALID
;
2819 * Copy kernel session sockets so we can communicate with the right
2820 * consumer for the snapshot record command.
2822 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2824 ret
= LTTNG_ERR_NOMEM
;
2828 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2829 if (ret
!= LTTNG_OK
) {
2830 goto error_snapshot
;
2833 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_streams
);
2834 if (ret
!= LTTNG_OK
) {
2835 goto error_snapshot
;
2841 /* Clean up copied sockets so this output can use some other later on. */
2842 consumer_destroy_output_sockets(output
->consumer
);
2848 * Record a UST snapshot.
2850 * Return 0 on success or a LTTNG_ERR error code.
2852 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2853 struct snapshot_output
*output
, struct ltt_session
*session
,
2854 int wait
, int nb_streams
)
2862 /* Get the datetime for the snapshot output directory. */
2863 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2864 sizeof(output
->datetime
));
2866 ret
= LTTNG_ERR_INVALID
;
2871 * Copy UST session sockets so we can communicate with the right
2872 * consumer for the snapshot record command.
2874 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
2876 ret
= LTTNG_ERR_NOMEM
;
2880 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
2881 if (ret
!= LTTNG_OK
) {
2882 goto error_snapshot
;
2885 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_streams
);
2889 ret
= LTTNG_ERR_INVALID
;
2892 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
2895 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
2898 goto error_snapshot
;
2904 /* Clean up copied sockets so this output can use some other later on. */
2905 consumer_destroy_output_sockets(output
->consumer
);
2911 * Returns the total number of streams for a session or a negative value
2914 static unsigned int get_total_nb_stream(struct ltt_session
*session
)
2916 unsigned int total_streams
= 0;
2918 if (session
->kernel_session
) {
2919 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2921 total_streams
+= ksess
->stream_count_global
;
2924 if (session
->ust_session
) {
2925 struct ltt_ust_session
*usess
= session
->ust_session
;
2927 total_streams
+= ust_app_get_nb_stream(usess
);
2930 return total_streams
;
2934 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
2936 * The wait parameter is ignored so this call always wait for the snapshot to
2937 * complete before returning.
2939 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2941 int cmd_snapshot_record(struct ltt_session
*session
,
2942 struct lttng_snapshot_output
*output
, int wait
)
2945 unsigned int use_tmp_output
= 0;
2946 struct snapshot_output tmp_output
;
2947 unsigned int nb_streams
, snapshot_success
= 0;
2951 DBG("Cmd snapshot record for session %s", session
->name
);
2954 * Permission denied to create an output if the session is not
2955 * set in no output mode.
2957 if (session
->output_traces
) {
2958 ret
= LTTNG_ERR_EPERM
;
2962 /* The session needs to be started at least once. */
2963 if (!session
->has_been_started
) {
2964 ret
= LTTNG_ERR_START_SESSION_ONCE
;
2968 /* Use temporary output for the session. */
2969 if (output
&& *output
->ctrl_url
!= '\0') {
2970 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2971 output
->ctrl_url
, output
->data_url
, session
->consumer
,
2974 if (ret
== -ENOMEM
) {
2975 ret
= LTTNG_ERR_NOMEM
;
2977 ret
= LTTNG_ERR_INVALID
;
2981 /* Use the global session count for the temporary snapshot. */
2982 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
2987 * Get the total number of stream of that session which is used by the
2988 * maximum size of the snapshot feature.
2990 nb_streams
= get_total_nb_stream(session
);
2992 if (session
->kernel_session
) {
2993 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2995 if (use_tmp_output
) {
2996 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
2998 if (ret
!= LTTNG_OK
) {
3001 snapshot_success
= 1;
3003 struct snapshot_output
*sout
;
3004 struct lttng_ht_iter iter
;
3007 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3008 &iter
.iter
, sout
, node
.node
) {
3010 * Make a local copy of the output and assign the possible
3011 * temporary value given by the caller.
3013 memset(&tmp_output
, 0, sizeof(tmp_output
));
3014 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3016 /* Use temporary max size. */
3017 if (output
->max_size
!= (uint64_t) -1ULL) {
3018 tmp_output
.max_size
= output
->max_size
;
3021 /* Use temporary name. */
3022 if (*output
->name
!= '\0') {
3023 strncpy(tmp_output
.name
, output
->name
,
3024 sizeof(tmp_output
.name
));
3027 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3029 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3030 session
, wait
, nb_streams
);
3031 if (ret
!= LTTNG_OK
) {
3035 snapshot_success
= 1;
3041 if (session
->ust_session
) {
3042 struct ltt_ust_session
*usess
= session
->ust_session
;
3044 if (use_tmp_output
) {
3045 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3047 if (ret
!= LTTNG_OK
) {
3050 snapshot_success
= 1;
3052 struct snapshot_output
*sout
;
3053 struct lttng_ht_iter iter
;
3056 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3057 &iter
.iter
, sout
, node
.node
) {
3059 * Make a local copy of the output and assign the possible
3060 * temporary value given by the caller.
3062 memset(&tmp_output
, 0, sizeof(tmp_output
));
3063 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3065 /* Use temporary max size. */
3066 if (output
->max_size
!= (uint64_t) -1ULL) {
3067 tmp_output
.max_size
= output
->max_size
;
3070 /* Use temporary name. */
3071 if (*output
->name
!= '\0') {
3072 strncpy(tmp_output
.name
, output
->name
,
3073 sizeof(tmp_output
.name
));
3076 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3078 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3080 if (ret
!= LTTNG_OK
) {
3084 snapshot_success
= 1;
3090 if (snapshot_success
) {
3091 session
->snapshot
.nb_snapshot
++;
3093 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3101 * Init command subsystem.
3106 * Set network sequence index to 1 for streams to match a relayd
3107 * socket on the consumer side.
3109 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3110 relayd_net_seq_idx
= 1;
3111 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3113 DBG("Command subsystem initialized");