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 * Both functions below are special case for the Kernel domain when
52 * enabling/disabling all events.
55 int enable_kevent_all(struct ltt_session
*session
,
56 struct lttng_domain
*domain
, char *channel_name
,
57 struct lttng_event
*event
,
58 char *filter_expression
,
59 struct lttng_filter_bytecode
*filter
, int wpipe
);
61 int disable_kevent_all(struct ltt_session
*session
, int domain
,
63 struct lttng_event
*event
);
66 * Create a session path used by list_lttng_sessions for the case that the
67 * session consumer is on the network.
69 static int build_network_session_path(char *dst
, size_t size
,
70 struct ltt_session
*session
)
72 int ret
, kdata_port
, udata_port
;
73 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
74 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
79 memset(tmp_urls
, 0, sizeof(tmp_urls
));
80 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
82 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
84 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
85 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
86 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
89 if (session
->ust_session
&& session
->ust_session
->consumer
) {
90 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
91 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
94 if (uuri
== NULL
&& kuri
== NULL
) {
95 uri
= &session
->consumer
->dst
.net
.control
;
96 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
97 } else if (kuri
&& uuri
) {
98 ret
= uri_compare(kuri
, uuri
);
102 /* Build uuri URL string */
103 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
110 } else if (kuri
&& uuri
== NULL
) {
112 } else if (uuri
&& kuri
== NULL
) {
116 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
122 * Do we have a UST url set. If yes, this means we have both kernel and UST
125 if (*tmp_uurl
!= '\0') {
126 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
127 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
130 if (kuri
|| (!kuri
&& !uuri
)) {
133 /* No kernel URI, use the UST port. */
136 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
144 * Fill lttng_channel array of all channels.
146 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
147 struct lttng_channel
*channels
)
150 struct ltt_kernel_channel
*kchan
;
152 DBG("Listing channels for session %s", session
->name
);
155 case LTTNG_DOMAIN_KERNEL
:
156 /* Kernel channels */
157 if (session
->kernel_session
!= NULL
) {
158 cds_list_for_each_entry(kchan
,
159 &session
->kernel_session
->channel_list
.head
, list
) {
160 /* Copy lttng_channel struct to array */
161 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
162 channels
[i
].enabled
= kchan
->enabled
;
167 case LTTNG_DOMAIN_UST
:
169 struct lttng_ht_iter iter
;
170 struct ltt_ust_channel
*uchan
;
173 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
174 &iter
.iter
, uchan
, node
.node
) {
175 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
176 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
177 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
178 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
179 channels
[i
].attr
.switch_timer_interval
=
180 uchan
->attr
.switch_timer_interval
;
181 channels
[i
].attr
.read_timer_interval
=
182 uchan
->attr
.read_timer_interval
;
183 channels
[i
].enabled
= uchan
->enabled
;
184 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
185 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
186 switch (uchan
->attr
.output
) {
189 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
203 * Create a list of agent domain events.
205 * Return number of events in list on success or else a negative value.
207 static int list_lttng_agent_events(struct agent
*agt
,
208 struct lttng_event
**events
)
211 unsigned int nb_event
= 0;
212 struct agent_event
*event
;
213 struct lttng_event
*tmp_events
;
214 struct lttng_ht_iter iter
;
219 DBG3("Listing agent events");
221 nb_event
= lttng_ht_get_count(agt
->events
);
227 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
229 PERROR("zmalloc agent events session");
230 ret
= -LTTNG_ERR_FATAL
;
235 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
236 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
237 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
238 tmp_events
[i
].enabled
= event
->enabled
;
239 tmp_events
[i
].loglevel
= event
->loglevel
;
240 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
245 *events
= tmp_events
;
249 assert(nb_event
== i
);
254 * Create a list of ust global domain events.
256 static int list_lttng_ust_global_events(char *channel_name
,
257 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
260 unsigned int nb_event
= 0;
261 struct lttng_ht_iter iter
;
262 struct lttng_ht_node_str
*node
;
263 struct ltt_ust_channel
*uchan
;
264 struct ltt_ust_event
*uevent
;
265 struct lttng_event
*tmp
;
267 DBG("Listing UST global events for channel %s", channel_name
);
271 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
272 node
= lttng_ht_iter_get_node_str(&iter
);
274 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
278 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
280 nb_event
+= lttng_ht_get_count(uchan
->events
);
287 DBG3("Listing UST global %d events", nb_event
);
289 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
291 ret
= LTTNG_ERR_FATAL
;
295 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
296 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
297 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
298 tmp
[i
].enabled
= uevent
->enabled
;
300 switch (uevent
->attr
.instrumentation
) {
301 case LTTNG_UST_TRACEPOINT
:
302 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
304 case LTTNG_UST_PROBE
:
305 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
307 case LTTNG_UST_FUNCTION
:
308 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
312 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
313 switch (uevent
->attr
.loglevel_type
) {
314 case LTTNG_UST_LOGLEVEL_ALL
:
315 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
317 case LTTNG_UST_LOGLEVEL_RANGE
:
318 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
320 case LTTNG_UST_LOGLEVEL_SINGLE
:
321 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
324 if (uevent
->filter
) {
327 if (uevent
->exclusion
) {
328 tmp
[i
].exclusion
= 1;
342 * Fill lttng_event array of all kernel events in the channel.
344 static int list_lttng_kernel_events(char *channel_name
,
345 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
348 unsigned int nb_event
;
349 struct ltt_kernel_event
*event
;
350 struct ltt_kernel_channel
*kchan
;
352 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
354 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
358 nb_event
= kchan
->event_count
;
360 DBG("Listing events for channel %s", kchan
->channel
->name
);
366 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
367 if (*events
== NULL
) {
368 ret
= LTTNG_ERR_FATAL
;
372 /* Kernel channels */
373 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
374 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
375 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
376 (*events
)[i
].enabled
= event
->enabled
;
378 switch (event
->event
->instrumentation
) {
379 case LTTNG_KERNEL_TRACEPOINT
:
380 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
382 case LTTNG_KERNEL_KRETPROBE
:
383 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
384 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
385 sizeof(struct lttng_kernel_kprobe
));
387 case LTTNG_KERNEL_KPROBE
:
388 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
389 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
390 sizeof(struct lttng_kernel_kprobe
));
392 case LTTNG_KERNEL_FUNCTION
:
393 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
394 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
395 sizeof(struct lttng_kernel_function
));
397 case LTTNG_KERNEL_NOOP
:
398 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
400 case LTTNG_KERNEL_SYSCALL
:
401 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
403 case LTTNG_KERNEL_ALL
:
414 /* Negate the error code to differentiate the size from an error */
419 * Add URI so the consumer output object. Set the correct path depending on the
420 * domain adding the default trace directory.
422 static int add_uri_to_consumer(struct consumer_output
*consumer
,
423 struct lttng_uri
*uri
, int domain
, const char *session_name
)
426 const char *default_trace_dir
;
430 if (consumer
== NULL
) {
431 DBG("No consumer detected. Don't add URI. Stopping.");
432 ret
= LTTNG_ERR_NO_CONSUMER
;
437 case LTTNG_DOMAIN_KERNEL
:
438 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
440 case LTTNG_DOMAIN_UST
:
441 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
445 * This case is possible is we try to add the URI to the global tracing
446 * session consumer object which in this case there is no subdir.
448 default_trace_dir
= "";
451 switch (uri
->dtype
) {
454 DBG2("Setting network URI to consumer");
456 if (consumer
->type
== CONSUMER_DST_NET
) {
457 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
458 consumer
->dst
.net
.control_isset
) ||
459 (uri
->stype
== LTTNG_STREAM_DATA
&&
460 consumer
->dst
.net
.data_isset
)) {
461 ret
= LTTNG_ERR_URL_EXIST
;
465 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
468 consumer
->type
= CONSUMER_DST_NET
;
470 /* Set URI into consumer output object */
471 ret
= consumer_set_network_uri(consumer
, uri
);
475 } else if (ret
== 1) {
477 * URI was the same in the consumer so we do not append the subdir
478 * again so to not duplicate output dir.
484 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
485 ret
= consumer_set_subdir(consumer
, session_name
);
487 ret
= LTTNG_ERR_FATAL
;
492 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
493 /* On a new subdir, reappend the default trace dir. */
494 strncat(consumer
->subdir
, default_trace_dir
,
495 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
496 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
501 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
502 memset(consumer
->dst
.trace_path
, 0,
503 sizeof(consumer
->dst
.trace_path
));
504 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
505 sizeof(consumer
->dst
.trace_path
));
506 /* Append default trace dir */
507 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
508 sizeof(consumer
->dst
.trace_path
) -
509 strlen(consumer
->dst
.trace_path
) - 1);
510 /* Flag consumer as local. */
511 consumer
->type
= CONSUMER_DST_LOCAL
;
522 * Init tracing by creating trace directory and sending fds kernel consumer.
524 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
527 struct lttng_ht_iter iter
;
528 struct consumer_socket
*socket
;
534 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
535 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
537 pthread_mutex_lock(socket
->lock
);
538 ret
= kernel_consumer_send_session(socket
, session
);
539 pthread_mutex_unlock(socket
->lock
);
541 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
553 * Create a socket to the relayd using the URI.
555 * On success, the relayd_sock pointer is set to the created socket.
556 * Else, it's stays untouched and a lttcomm error code is returned.
558 static int create_connect_relayd(struct lttng_uri
*uri
,
559 struct lttcomm_relayd_sock
**relayd_sock
)
562 struct lttcomm_relayd_sock
*rsock
;
564 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
565 RELAYD_VERSION_COMM_MINOR
);
567 ret
= LTTNG_ERR_FATAL
;
572 * Connect to relayd so we can proceed with a session creation. This call
573 * can possibly block for an arbitrary amount of time to set the health
574 * state to be in poll execution.
577 ret
= relayd_connect(rsock
);
580 ERR("Unable to reach lttng-relayd");
581 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
585 /* Create socket for control stream. */
586 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
587 DBG3("Creating relayd stream socket from URI");
589 /* Check relayd version */
590 ret
= relayd_version_check(rsock
);
592 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
595 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
596 DBG3("Creating relayd data socket from URI");
598 /* Command is not valid */
599 ERR("Relayd invalid stream type: %d", uri
->stype
);
600 ret
= LTTNG_ERR_INVALID
;
604 *relayd_sock
= rsock
;
609 /* The returned value is not useful since we are on an error path. */
610 (void) relayd_close(rsock
);
618 * Connect to the relayd using URI and send the socket to the right consumer.
620 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
621 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
622 struct consumer_socket
*consumer_sock
,
623 char *session_name
, char *hostname
, int session_live_timer
)
626 struct lttcomm_relayd_sock
*rsock
= NULL
;
628 /* Connect to relayd and make version check if uri is the control. */
629 ret
= create_connect_relayd(relayd_uri
, &rsock
);
630 if (ret
!= LTTNG_OK
) {
635 /* Set the network sequence index if not set. */
636 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
637 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
639 * Increment net_seq_idx because we are about to transfer the
640 * new relayd socket to the consumer.
641 * Assign unique key so the consumer can match streams.
643 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
644 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
647 /* Send relayd socket to consumer. */
648 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
649 relayd_uri
->stype
, session_id
,
650 session_name
, hostname
, session_live_timer
);
652 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
656 /* Flag that the corresponding socket was sent. */
657 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
658 consumer_sock
->control_sock_sent
= 1;
659 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
660 consumer_sock
->data_sock_sent
= 1;
666 * Close socket which was dup on the consumer side. The session daemon does
667 * NOT keep track of the relayd socket(s) once transfer to the consumer.
671 (void) relayd_close(rsock
);
675 if (ret
!= LTTNG_OK
) {
677 * The consumer output for this session should not be used anymore
678 * since the relayd connection failed thus making any tracing or/and
679 * streaming not usable.
681 consumer
->enabled
= 0;
687 * Send both relayd sockets to a specific consumer and domain. This is a
688 * helper function to facilitate sending the information to the consumer for a
691 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
692 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
693 char *session_name
, char *hostname
, int session_live_timer
)
700 /* Sending control relayd socket. */
701 if (!sock
->control_sock_sent
) {
702 ret
= send_consumer_relayd_socket(domain
, session_id
,
703 &consumer
->dst
.net
.control
, consumer
, sock
,
704 session_name
, hostname
, session_live_timer
);
705 if (ret
!= LTTNG_OK
) {
710 /* Sending data relayd socket. */
711 if (!sock
->data_sock_sent
) {
712 ret
= send_consumer_relayd_socket(domain
, session_id
,
713 &consumer
->dst
.net
.data
, consumer
, sock
,
714 session_name
, hostname
, session_live_timer
);
715 if (ret
!= LTTNG_OK
) {
725 * Setup relayd connections for a tracing session. First creates the socket to
726 * the relayd and send them to the right domain consumer. Consumer type MUST be
729 int cmd_setup_relayd(struct ltt_session
*session
)
732 struct ltt_ust_session
*usess
;
733 struct ltt_kernel_session
*ksess
;
734 struct consumer_socket
*socket
;
735 struct lttng_ht_iter iter
;
739 usess
= session
->ust_session
;
740 ksess
= session
->kernel_session
;
742 DBG("Setting relayd for session %s", session
->name
);
746 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
747 && usess
->consumer
->enabled
) {
748 /* For each consumer socket, send relayd sockets */
749 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
751 pthread_mutex_lock(socket
->lock
);
752 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
753 usess
->consumer
, socket
,
754 session
->name
, session
->hostname
,
755 session
->live_timer
);
756 pthread_mutex_unlock(socket
->lock
);
757 if (ret
!= LTTNG_OK
) {
760 /* Session is now ready for network streaming. */
761 session
->net_handle
= 1;
765 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
766 && ksess
->consumer
->enabled
) {
767 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
769 pthread_mutex_lock(socket
->lock
);
770 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
771 ksess
->consumer
, socket
,
772 session
->name
, session
->hostname
,
773 session
->live_timer
);
774 pthread_mutex_unlock(socket
->lock
);
775 if (ret
!= LTTNG_OK
) {
778 /* Session is now ready for network streaming. */
779 session
->net_handle
= 1;
789 * Start a kernel session by opening all necessary streams.
791 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
794 struct ltt_kernel_channel
*kchan
;
796 /* Open kernel metadata */
797 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
798 ret
= kernel_open_metadata(ksess
);
800 ret
= LTTNG_ERR_KERN_META_FAIL
;
805 /* Open kernel metadata stream */
806 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
807 ret
= kernel_open_metadata_stream(ksess
);
809 ERR("Kernel create metadata stream failed");
810 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
815 /* For each channel */
816 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
817 if (kchan
->stream_count
== 0) {
818 ret
= kernel_open_channel_stream(kchan
);
820 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
823 /* Update the stream global counter */
824 ksess
->stream_count_global
+= ret
;
828 /* Setup kernel consumer socket and send fds to it */
829 ret
= init_kernel_tracing(ksess
);
831 ret
= LTTNG_ERR_KERN_START_FAIL
;
835 /* This start the kernel tracing */
836 ret
= kernel_start_session(ksess
);
838 ret
= LTTNG_ERR_KERN_START_FAIL
;
842 /* Quiescent wait after starting trace */
843 kernel_wait_quiescent(kernel_tracer_fd
);
854 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
856 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
860 struct ltt_ust_session
*usess
;
862 usess
= session
->ust_session
;
867 case LTTNG_DOMAIN_KERNEL
:
869 ret
= channel_kernel_disable(session
->kernel_session
,
871 if (ret
!= LTTNG_OK
) {
875 kernel_wait_quiescent(kernel_tracer_fd
);
878 case LTTNG_DOMAIN_UST
:
880 struct ltt_ust_channel
*uchan
;
881 struct lttng_ht
*chan_ht
;
883 chan_ht
= usess
->domain_global
.channels
;
885 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
887 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
891 ret
= channel_ust_disable(usess
, uchan
);
892 if (ret
!= LTTNG_OK
) {
898 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
899 case LTTNG_DOMAIN_UST_EXEC_NAME
:
900 case LTTNG_DOMAIN_UST_PID
:
903 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
915 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
917 * The wpipe arguments is used as a notifier for the kernel thread.
919 int cmd_enable_channel(struct ltt_session
*session
,
920 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
923 struct ltt_ust_session
*usess
= session
->ust_session
;
924 struct lttng_ht
*chan_ht
;
930 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
935 * Don't try to enable a channel if the session has been started at
936 * some point in time before. The tracer does not allow it.
938 if (session
->has_been_started
) {
939 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
944 * If the session is a live session, remove the switch timer, the
945 * live timer does the same thing but sends also synchronisation
946 * beacons for inactive streams.
948 if (session
->live_timer
> 0) {
949 attr
->attr
.live_timer_interval
= session
->live_timer
;
950 attr
->attr
.switch_timer_interval
= 0;
954 * The ringbuffer (both in user space and kernel) behave badly in overwrite
955 * mode and with less than 2 subbuf so block it right away and send back an
956 * invalid attribute error.
958 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
959 ret
= LTTNG_ERR_INVALID
;
963 switch (domain
->type
) {
964 case LTTNG_DOMAIN_KERNEL
:
966 struct ltt_kernel_channel
*kchan
;
968 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
969 session
->kernel_session
);
971 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
972 if (attr
->name
[0] != '\0') {
973 session
->kernel_session
->has_non_default_channel
= 1;
976 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
979 if (ret
!= LTTNG_OK
) {
983 kernel_wait_quiescent(kernel_tracer_fd
);
986 case LTTNG_DOMAIN_UST
:
988 struct ltt_ust_channel
*uchan
;
990 chan_ht
= usess
->domain_global
.channels
;
992 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
994 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
995 if (attr
->name
[0] != '\0') {
996 usess
->has_non_default_channel
= 1;
999 ret
= channel_ust_enable(usess
, uchan
);
1004 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1015 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1017 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1019 struct lttng_event
*event
)
1024 DBG("Disable event command for event \'%s\'", event
->name
);
1026 event_name
= event
->name
;
1028 if (event
->loglevel_type
|| event
->loglevel
|| event
->enabled
1029 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1030 return LTTNG_ERR_UNK
;
1032 /* Special handling for kernel domain all events. */
1033 if (domain
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event_name
, "*")) {
1034 return disable_kevent_all(session
, domain
, channel_name
, event
);
1040 case LTTNG_DOMAIN_KERNEL
:
1042 struct ltt_kernel_channel
*kchan
;
1043 struct ltt_kernel_session
*ksess
;
1045 ksess
= session
->kernel_session
;
1048 * If a non-default channel has been created in the
1049 * session, explicitely require that -c chan_name needs
1052 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1053 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1057 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1058 if (kchan
== NULL
) {
1059 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1063 switch (event
->type
) {
1064 case LTTNG_EVENT_ALL
:
1065 case LTTNG_EVENT_TRACEPOINT
:
1066 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1067 if (ret
!= LTTNG_OK
) {
1071 case LTTNG_EVENT_SYSCALL
:
1072 ret
= event_kernel_disable_syscall(kchan
, event_name
);
1075 ret
= LTTNG_ERR_UNK
;
1079 kernel_wait_quiescent(kernel_tracer_fd
);
1082 case LTTNG_DOMAIN_UST
:
1084 struct ltt_ust_channel
*uchan
;
1085 struct ltt_ust_session
*usess
;
1087 usess
= session
->ust_session
;
1090 * If a non-default channel has been created in the
1091 * session, explicitely require that -c chan_name needs
1094 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1095 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1099 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1101 if (uchan
== NULL
) {
1102 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1106 switch (event
->type
) {
1107 case LTTNG_EVENT_ALL
:
1108 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1109 if (ret
!= LTTNG_OK
) {
1114 ret
= LTTNG_ERR_UNK
;
1118 DBG3("Disable UST event %s in channel %s completed", event_name
,
1122 case LTTNG_DOMAIN_LOG4J
:
1123 case LTTNG_DOMAIN_JUL
:
1126 struct ltt_ust_session
*usess
= session
->ust_session
;
1130 switch (event
->type
) {
1131 case LTTNG_EVENT_ALL
:
1134 ret
= LTTNG_ERR_UNK
;
1138 agt
= trace_ust_find_agent(usess
, domain
);
1140 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1143 /* The wild card * means that everything should be disabled. */
1144 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1145 ret
= event_agent_disable_all(usess
, agt
);
1147 ret
= event_agent_disable(usess
, agt
, event_name
);
1149 if (ret
!= LTTNG_OK
) {
1156 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1157 case LTTNG_DOMAIN_UST_PID
:
1158 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1161 ret
= LTTNG_ERR_UND
;
1173 * Command LTTNG_DISABLE_EVENT for event "*" processed by the client thread.
1176 int disable_kevent_all(struct ltt_session
*session
, int domain
,
1178 struct lttng_event
*event
)
1183 event_name
= event
->name
;
1188 case LTTNG_DOMAIN_KERNEL
:
1190 struct ltt_kernel_session
*ksess
;
1191 struct ltt_kernel_channel
*kchan
;
1193 ksess
= session
->kernel_session
;
1196 * If a non-default channel has been created in the
1197 * session, explicitely require that -c chan_name needs
1200 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1201 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1205 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1206 if (kchan
== NULL
) {
1207 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1211 switch (event
->type
) {
1212 case LTTNG_EVENT_ALL
:
1213 ret
= event_kernel_disable_all(kchan
);
1214 if (ret
!= LTTNG_OK
) {
1218 case LTTNG_EVENT_SYSCALL
:
1219 ret
= event_kernel_disable_syscall(kchan
, event_name
);
1222 ret
= LTTNG_ERR_UNK
;
1226 kernel_wait_quiescent(kernel_tracer_fd
);
1230 ret
= LTTNG_ERR_UND
;
1242 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1244 int cmd_add_context(struct ltt_session
*session
, int domain
,
1245 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1247 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1250 case LTTNG_DOMAIN_KERNEL
:
1251 assert(session
->kernel_session
);
1253 if (session
->kernel_session
->channel_count
== 0) {
1254 /* Create default channel */
1255 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1256 if (ret
!= LTTNG_OK
) {
1259 chan_kern_created
= 1;
1261 /* Add kernel context to kernel tracer */
1262 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1263 if (ret
!= LTTNG_OK
) {
1267 case LTTNG_DOMAIN_UST
:
1269 struct ltt_ust_session
*usess
= session
->ust_session
;
1270 unsigned int chan_count
;
1274 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1275 if (chan_count
== 0) {
1276 struct lttng_channel
*attr
;
1277 /* Create default channel */
1278 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1280 ret
= LTTNG_ERR_FATAL
;
1284 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1285 if (ret
!= LTTNG_OK
) {
1290 chan_ust_created
= 1;
1293 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1294 if (ret
!= LTTNG_OK
) {
1300 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1301 case LTTNG_DOMAIN_UST_PID
:
1302 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1305 ret
= LTTNG_ERR_UND
;
1312 if (chan_kern_created
) {
1313 struct ltt_kernel_channel
*kchan
=
1314 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1315 session
->kernel_session
);
1316 /* Created previously, this should NOT fail. */
1318 kernel_destroy_channel(kchan
);
1321 if (chan_ust_created
) {
1322 struct ltt_ust_channel
*uchan
=
1323 trace_ust_find_channel_by_name(
1324 session
->ust_session
->domain_global
.channels
,
1325 DEFAULT_CHANNEL_NAME
);
1326 /* Created previously, this should NOT fail. */
1328 /* Remove from the channel list of the session. */
1329 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1331 trace_ust_destroy_channel(uchan
);
1336 static int validate_event_name(const char *name
)
1339 const char *c
= name
;
1340 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1343 * Make sure that unescaped wildcards are only used as the last
1344 * character of the event name.
1346 while (c
< event_name_end
) {
1354 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1355 /* Wildcard is not the last character */
1356 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1369 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1371 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1372 char *channel_name
, struct lttng_event
*event
,
1373 char *filter_expression
,
1374 struct lttng_filter_bytecode
*filter
,
1375 struct lttng_event_exclusion
*exclusion
,
1378 int ret
, channel_created
= 0;
1379 struct lttng_channel
*attr
;
1383 assert(channel_name
);
1385 DBG("Enable event command for event \'%s\'", event
->name
);
1387 /* Special handling for kernel domain all events. */
1388 if (domain
->type
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event
->name
, "*")) {
1389 return enable_kevent_all(session
, domain
, channel_name
, event
,
1390 filter_expression
, filter
, wpipe
);
1393 ret
= validate_event_name(event
->name
);
1400 switch (domain
->type
) {
1401 case LTTNG_DOMAIN_KERNEL
:
1403 struct ltt_kernel_channel
*kchan
;
1406 * If a non-default channel has been created in the
1407 * session, explicitely require that -c chan_name needs
1410 if (session
->kernel_session
->has_non_default_channel
1411 && channel_name
[0] == '\0') {
1412 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1416 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1417 session
->kernel_session
);
1418 if (kchan
== NULL
) {
1419 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1420 LTTNG_BUFFER_GLOBAL
);
1422 ret
= LTTNG_ERR_FATAL
;
1425 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1427 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1428 if (ret
!= LTTNG_OK
) {
1434 channel_created
= 1;
1437 /* Get the newly created kernel channel pointer */
1438 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1439 session
->kernel_session
);
1440 if (kchan
== NULL
) {
1441 /* This sould not happen... */
1442 ret
= LTTNG_ERR_FATAL
;
1446 switch (event
->type
) {
1447 case LTTNG_EVENT_ALL
:
1448 case LTTNG_EVENT_TRACEPOINT
:
1449 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1450 if (ret
!= LTTNG_OK
) {
1451 if (channel_created
) {
1452 /* Let's not leak a useless channel. */
1453 kernel_destroy_channel(kchan
);
1458 case LTTNG_EVENT_SYSCALL
:
1459 ret
= event_kernel_enable_syscall(kchan
, event
->name
);
1462 ret
= LTTNG_ERR_UNK
;
1466 kernel_wait_quiescent(kernel_tracer_fd
);
1469 case LTTNG_DOMAIN_UST
:
1471 struct ltt_ust_channel
*uchan
;
1472 struct ltt_ust_session
*usess
= session
->ust_session
;
1477 * If a non-default channel has been created in the
1478 * session, explicitely require that -c chan_name needs
1481 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1482 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1486 /* Get channel from global UST domain */
1487 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1489 if (uchan
== NULL
) {
1490 /* Create default channel */
1491 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1492 usess
->buffer_type
);
1494 ret
= LTTNG_ERR_FATAL
;
1497 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1499 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1500 if (ret
!= LTTNG_OK
) {
1506 /* Get the newly created channel reference back */
1507 uchan
= trace_ust_find_channel_by_name(
1508 usess
->domain_global
.channels
, channel_name
);
1512 /* At this point, the session and channel exist on the tracer */
1513 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1514 filter_expression
, filter
, exclusion
);
1515 if (ret
!= LTTNG_OK
) {
1520 case LTTNG_DOMAIN_LOG4J
:
1521 case LTTNG_DOMAIN_JUL
:
1523 const char *default_event_name
, *default_chan_name
;
1525 struct lttng_event uevent
;
1526 struct lttng_domain tmp_dom
;
1527 struct ltt_ust_session
*usess
= session
->ust_session
;
1531 agt
= trace_ust_find_agent(usess
, domain
->type
);
1533 agt
= agent_create(domain
->type
);
1535 ret
= -LTTNG_ERR_NOMEM
;
1538 agent_add(agt
, usess
->agents
);
1541 /* Create the default tracepoint. */
1542 memset(&uevent
, 0, sizeof(uevent
));
1543 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1544 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1545 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1546 if (!default_event_name
) {
1547 ret
= -LTTNG_ERR_FATAL
;
1550 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1551 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1554 * The domain type is changed because we are about to enable the
1555 * default channel and event for the JUL domain that are hardcoded.
1556 * This happens in the UST domain.
1558 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1559 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1561 if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1562 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1564 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1567 ret
= cmd_enable_event(session
, &tmp_dom
, (char *) default_chan_name
,
1568 &uevent
, filter_expression
, filter
, NULL
, wpipe
);
1569 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1573 /* The wild card * means that everything should be enabled. */
1574 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1575 ret
= event_agent_enable_all(usess
, agt
, event
, filter
);
1577 ret
= event_agent_enable(usess
, agt
, event
, filter
);
1579 if (ret
!= LTTNG_OK
) {
1586 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1587 case LTTNG_DOMAIN_UST_PID
:
1588 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1591 ret
= LTTNG_ERR_UND
;
1603 * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread.
1606 int enable_kevent_all(struct ltt_session
*session
,
1607 struct lttng_domain
*domain
, char *channel_name
,
1608 struct lttng_event
*event
,
1609 char *filter_expression
,
1610 struct lttng_filter_bytecode
*filter
, int wpipe
)
1613 struct lttng_channel
*attr
;
1616 assert(channel_name
);
1620 switch (domain
->type
) {
1621 case LTTNG_DOMAIN_KERNEL
:
1623 struct ltt_kernel_channel
*kchan
;
1625 assert(session
->kernel_session
);
1628 * If a non-default channel has been created in the
1629 * session, explicitely require that -c chan_name needs
1632 if (session
->kernel_session
->has_non_default_channel
1633 && channel_name
[0] == '\0') {
1634 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1638 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1639 session
->kernel_session
);
1640 if (kchan
== NULL
) {
1641 /* Create default channel */
1642 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1643 LTTNG_BUFFER_GLOBAL
);
1645 ret
= LTTNG_ERR_FATAL
;
1648 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1650 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1651 if (ret
!= LTTNG_OK
) {
1657 /* Get the newly created kernel channel pointer */
1658 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1659 session
->kernel_session
);
1663 switch (event
->type
) {
1664 case LTTNG_EVENT_SYSCALL
:
1665 ret
= event_kernel_enable_syscall(kchan
, "");
1667 case LTTNG_EVENT_TRACEPOINT
:
1669 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1670 * events already registered to the channel.
1672 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1674 case LTTNG_EVENT_ALL
:
1675 /* Enable syscalls and tracepoints */
1676 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1679 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1683 /* Manage return value */
1684 if (ret
!= LTTNG_OK
) {
1686 * On error, cmd_enable_channel call will take care of destroying
1687 * the created channel if it was needed.
1692 kernel_wait_quiescent(kernel_tracer_fd
);
1696 ret
= LTTNG_ERR_UND
;
1709 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1711 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1714 ssize_t nb_events
= 0;
1717 case LTTNG_DOMAIN_KERNEL
:
1718 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1719 if (nb_events
< 0) {
1720 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1724 case LTTNG_DOMAIN_UST
:
1725 nb_events
= ust_app_list_events(events
);
1726 if (nb_events
< 0) {
1727 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1731 case LTTNG_DOMAIN_LOG4J
:
1732 case LTTNG_DOMAIN_JUL
:
1733 nb_events
= agent_list_events(events
, domain
);
1734 if (nb_events
< 0) {
1735 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1740 ret
= LTTNG_ERR_UND
;
1747 /* Return negative value to differentiate return code */
1752 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1754 ssize_t
cmd_list_tracepoint_fields(int domain
,
1755 struct lttng_event_field
**fields
)
1758 ssize_t nb_fields
= 0;
1761 case LTTNG_DOMAIN_UST
:
1762 nb_fields
= ust_app_list_event_fields(fields
);
1763 if (nb_fields
< 0) {
1764 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1768 case LTTNG_DOMAIN_KERNEL
:
1769 default: /* fall-through */
1770 ret
= LTTNG_ERR_UND
;
1777 /* Return negative value to differentiate return code */
1782 * Command LTTNG_START_TRACE processed by the client thread.
1784 int cmd_start_trace(struct ltt_session
*session
)
1787 unsigned long nb_chan
= 0;
1788 struct ltt_kernel_session
*ksession
;
1789 struct ltt_ust_session
*usess
;
1793 /* Ease our life a bit ;) */
1794 ksession
= session
->kernel_session
;
1795 usess
= session
->ust_session
;
1797 /* Is the session already started? */
1798 if (session
->active
) {
1799 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1804 * Starting a session without channel is useless since after that it's not
1805 * possible to enable channel thus inform the client.
1807 if (usess
&& usess
->domain_global
.channels
) {
1808 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1811 nb_chan
+= ksession
->channel_count
;
1814 ret
= LTTNG_ERR_NO_CHANNEL
;
1818 /* Kernel tracing */
1819 if (ksession
!= NULL
) {
1820 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1821 if (ret
!= LTTNG_OK
) {
1826 /* Flag session that trace should start automatically */
1829 * Even though the start trace might fail, flag this session active so
1830 * other application coming in are started by default.
1834 ret
= ust_app_start_trace_all(usess
);
1836 ret
= LTTNG_ERR_UST_START_FAIL
;
1841 /* Flag this after a successful start. */
1842 session
->has_been_started
= 1;
1843 session
->active
= 1;
1852 * Command LTTNG_STOP_TRACE processed by the client thread.
1854 int cmd_stop_trace(struct ltt_session
*session
)
1857 struct ltt_kernel_channel
*kchan
;
1858 struct ltt_kernel_session
*ksession
;
1859 struct ltt_ust_session
*usess
;
1864 ksession
= session
->kernel_session
;
1865 usess
= session
->ust_session
;
1867 /* Session is not active. Skip everythong and inform the client. */
1868 if (!session
->active
) {
1869 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1874 if (ksession
&& ksession
->active
) {
1875 DBG("Stop kernel tracing");
1877 /* Flush metadata if exist */
1878 if (ksession
->metadata_stream_fd
>= 0) {
1879 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1881 ERR("Kernel metadata flush failed");
1885 /* Flush all buffers before stopping */
1886 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1887 ret
= kernel_flush_buffer(kchan
);
1889 ERR("Kernel flush buffer error");
1893 ret
= kernel_stop_session(ksession
);
1895 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1899 kernel_wait_quiescent(kernel_tracer_fd
);
1901 ksession
->active
= 0;
1904 if (usess
&& usess
->active
) {
1906 * Even though the stop trace might fail, flag this session inactive so
1907 * other application coming in are not started by default.
1911 ret
= ust_app_stop_trace_all(usess
);
1913 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1918 /* Flag inactive after a successful stop. */
1919 session
->active
= 0;
1927 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1929 int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
1930 size_t nb_uri
, struct lttng_uri
*uris
)
1933 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1934 struct ltt_ust_session
*usess
= session
->ust_session
;
1935 struct consumer_output
*consumer
= NULL
;
1941 /* Can't set consumer URI if the session is active. */
1942 if (session
->active
) {
1943 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1948 * This case switch makes sure the domain session has a temporary consumer
1949 * so the URL can be set.
1953 /* Code flow error. A session MUST always have a consumer object */
1954 assert(session
->consumer
);
1956 * The URL will be added to the tracing session consumer instead of a
1957 * specific domain consumer.
1959 consumer
= session
->consumer
;
1961 case LTTNG_DOMAIN_KERNEL
:
1962 /* Code flow error if we don't have a kernel session here. */
1964 assert(ksess
->consumer
);
1965 consumer
= ksess
->consumer
;
1967 case LTTNG_DOMAIN_UST
:
1968 /* Code flow error if we don't have a kernel session here. */
1970 assert(usess
->consumer
);
1971 consumer
= usess
->consumer
;
1975 for (i
= 0; i
< nb_uri
; i
++) {
1976 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
1977 if (ret
!= LTTNG_OK
) {
1983 * Make sure to set the session in output mode after we set URI since a
1984 * session can be created without URL (thus flagged in no output mode).
1986 session
->output_traces
= 1;
1988 ksess
->output_traces
= 1;
1990 usess
->output_traces
= 1;
2001 * Command LTTNG_CREATE_SESSION processed by the client thread.
2003 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2004 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2007 struct ltt_session
*session
;
2013 * Verify if the session already exist
2015 * XXX: There is no need for the session lock list here since the caller
2016 * (process_client_msg) is holding it. We might want to change that so a
2017 * single command does not lock the entire session list.
2019 session
= session_find_by_name(name
);
2020 if (session
!= NULL
) {
2021 ret
= LTTNG_ERR_EXIST_SESS
;
2025 /* Create tracing session in the registry */
2026 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2027 LTTNG_SOCK_GET_GID_CRED(creds
));
2028 if (ret
!= LTTNG_OK
) {
2033 * Get the newly created session pointer back
2035 * XXX: There is no need for the session lock list here since the caller
2036 * (process_client_msg) is holding it. We might want to change that so a
2037 * single command does not lock the entire session list.
2039 session
= session_find_by_name(name
);
2042 session
->live_timer
= live_timer
;
2043 /* Create default consumer output for the session not yet created. */
2044 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2045 if (session
->consumer
== NULL
) {
2046 ret
= LTTNG_ERR_FATAL
;
2047 goto consumer_error
;
2051 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
2052 if (ret
!= LTTNG_OK
) {
2053 goto consumer_error
;
2055 session
->output_traces
= 1;
2057 session
->output_traces
= 0;
2058 DBG2("Session %s created with no output", session
->name
);
2061 session
->consumer
->enabled
= 1;
2066 session_destroy(session
);
2073 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2075 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2076 size_t nb_uri
, lttng_sock_cred
*creds
)
2079 struct ltt_session
*session
;
2080 struct snapshot_output
*new_output
= NULL
;
2086 * Create session in no output mode with URIs set to NULL. The uris we've
2087 * received are for a default snapshot output if one.
2089 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2090 if (ret
!= LTTNG_OK
) {
2094 /* Get the newly created session pointer back. This should NEVER fail. */
2095 session
= session_find_by_name(name
);
2098 /* Flag session for snapshot mode. */
2099 session
->snapshot_mode
= 1;
2101 /* Skip snapshot output creation if no URI is given. */
2106 new_output
= snapshot_output_alloc();
2108 ret
= LTTNG_ERR_NOMEM
;
2109 goto error_snapshot_alloc
;
2112 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2113 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2115 if (ret
== -ENOMEM
) {
2116 ret
= LTTNG_ERR_NOMEM
;
2118 ret
= LTTNG_ERR_INVALID
;
2120 goto error_snapshot
;
2124 snapshot_add_output(&session
->snapshot
, new_output
);
2131 snapshot_output_destroy(new_output
);
2132 error_snapshot_alloc
:
2133 session_destroy(session
);
2139 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2141 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2144 struct ltt_ust_session
*usess
;
2145 struct ltt_kernel_session
*ksess
;
2150 usess
= session
->ust_session
;
2151 ksess
= session
->kernel_session
;
2153 /* Clean kernel session teardown */
2154 kernel_destroy_session(ksess
);
2156 /* UST session teardown */
2158 /* Close any relayd session */
2159 consumer_output_send_destroy_relayd(usess
->consumer
);
2161 /* Destroy every UST application related to this session. */
2162 ret
= ust_app_destroy_trace_all(usess
);
2164 ERR("Error in ust_app_destroy_trace_all");
2167 /* Clean up the rest. */
2168 trace_ust_destroy_session(usess
);
2172 * Must notify the kernel thread here to update it's poll set in order to
2173 * remove the channel(s)' fd just destroyed.
2175 ret
= notify_thread_pipe(wpipe
);
2177 PERROR("write kernel poll pipe");
2180 ret
= session_destroy(session
);
2186 * Command LTTNG_CALIBRATE processed by the client thread.
2188 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2193 case LTTNG_DOMAIN_KERNEL
:
2195 struct lttng_kernel_calibrate kcalibrate
;
2197 switch (calibrate
->type
) {
2198 case LTTNG_CALIBRATE_FUNCTION
:
2200 /* Default and only possible calibrate option. */
2201 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2205 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2207 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2212 case LTTNG_DOMAIN_UST
:
2214 struct lttng_ust_calibrate ucalibrate
;
2216 switch (calibrate
->type
) {
2217 case LTTNG_CALIBRATE_FUNCTION
:
2219 /* Default and only possible calibrate option. */
2220 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2224 ret
= ust_app_calibrate_glb(&ucalibrate
);
2226 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2232 ret
= LTTNG_ERR_UND
;
2243 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2245 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2246 const char *sock_path
, struct consumer_data
*cdata
)
2249 struct consumer_socket
*socket
= NULL
;
2256 case LTTNG_DOMAIN_KERNEL
:
2258 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2262 /* Can't register a consumer if there is already one */
2263 if (ksess
->consumer_fds_sent
!= 0) {
2264 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2268 sock
= lttcomm_connect_unix_sock(sock_path
);
2270 ret
= LTTNG_ERR_CONNECT_FAIL
;
2273 cdata
->cmd_sock
= sock
;
2275 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2276 if (socket
== NULL
) {
2279 PERROR("close register consumer");
2281 cdata
->cmd_sock
= -1;
2282 ret
= LTTNG_ERR_FATAL
;
2286 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2287 if (socket
->lock
== NULL
) {
2288 PERROR("zmalloc pthread mutex");
2289 ret
= LTTNG_ERR_FATAL
;
2292 pthread_mutex_init(socket
->lock
, NULL
);
2293 socket
->registered
= 1;
2296 consumer_add_socket(socket
, ksess
->consumer
);
2299 pthread_mutex_lock(&cdata
->pid_mutex
);
2301 pthread_mutex_unlock(&cdata
->pid_mutex
);
2306 /* TODO: Userspace tracing */
2307 ret
= LTTNG_ERR_UND
;
2315 consumer_destroy_socket(socket
);
2321 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2323 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2324 struct lttng_domain
**domains
)
2329 struct lttng_ht_iter iter
;
2331 if (session
->kernel_session
!= NULL
) {
2332 DBG3("Listing domains found kernel domain");
2336 if (session
->ust_session
!= NULL
) {
2337 DBG3("Listing domains found UST global domain");
2340 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2342 if (agt
->being_used
) {
2348 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2349 if (*domains
== NULL
) {
2350 ret
= LTTNG_ERR_FATAL
;
2354 if (session
->kernel_session
!= NULL
) {
2355 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2359 if (session
->ust_session
!= NULL
) {
2360 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2361 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2364 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2366 if (agt
->being_used
) {
2367 (*domains
)[index
].type
= agt
->domain
;
2368 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2377 /* Return negative value to differentiate return code */
2383 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2385 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2386 struct lttng_channel
**channels
)
2389 ssize_t nb_chan
= 0;
2392 case LTTNG_DOMAIN_KERNEL
:
2393 if (session
->kernel_session
!= NULL
) {
2394 nb_chan
= session
->kernel_session
->channel_count
;
2396 DBG3("Number of kernel channels %zd", nb_chan
);
2398 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2401 case LTTNG_DOMAIN_UST
:
2402 if (session
->ust_session
!= NULL
) {
2403 nb_chan
= lttng_ht_get_count(
2404 session
->ust_session
->domain_global
.channels
);
2406 DBG3("Number of UST global channels %zd", nb_chan
);
2408 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2413 ret
= LTTNG_ERR_UND
;
2418 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2419 if (*channels
== NULL
) {
2420 ret
= LTTNG_ERR_FATAL
;
2424 list_lttng_channels(domain
, session
, *channels
);
2427 /* Ret value was set in the domain switch case */
2434 /* Return negative value to differentiate return code */
2439 * Command LTTNG_LIST_EVENTS processed by the client thread.
2441 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2442 char *channel_name
, struct lttng_event
**events
)
2445 ssize_t nb_event
= 0;
2448 case LTTNG_DOMAIN_KERNEL
:
2449 if (session
->kernel_session
!= NULL
) {
2450 nb_event
= list_lttng_kernel_events(channel_name
,
2451 session
->kernel_session
, events
);
2454 case LTTNG_DOMAIN_UST
:
2456 if (session
->ust_session
!= NULL
) {
2457 nb_event
= list_lttng_ust_global_events(channel_name
,
2458 &session
->ust_session
->domain_global
, events
);
2462 case LTTNG_DOMAIN_LOG4J
:
2463 case LTTNG_DOMAIN_JUL
:
2464 if (session
->ust_session
) {
2465 struct lttng_ht_iter iter
;
2468 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2469 &iter
.iter
, agt
, node
.node
) {
2470 nb_event
= list_lttng_agent_events(agt
, events
);
2475 ret
= LTTNG_ERR_UND
;
2482 /* Return negative value to differentiate return code */
2487 * Using the session list, filled a lttng_session array to send back to the
2488 * client for session listing.
2490 * The session list lock MUST be acquired before calling this function. Use
2491 * session_lock_list() and session_unlock_list().
2493 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2498 struct ltt_session
*session
;
2499 struct ltt_session_list
*list
= session_get_list();
2501 DBG("Getting all available session for UID %d GID %d",
2504 * Iterate over session list and append data after the control struct in
2507 cds_list_for_each_entry(session
, &list
->head
, list
) {
2509 * Only list the sessions the user can control.
2511 if (!session_access_ok(session
, uid
, gid
)) {
2515 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2516 struct ltt_ust_session
*usess
= session
->ust_session
;
2518 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2519 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2520 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2521 ret
= build_network_session_path(sessions
[i
].path
,
2522 sizeof(sessions
[i
].path
), session
);
2524 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2525 session
->consumer
->dst
.trace_path
);
2528 PERROR("snprintf session path");
2532 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2533 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2534 sessions
[i
].enabled
= session
->active
;
2535 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2536 sessions
[i
].live_timer_interval
= session
->live_timer
;
2542 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2543 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2545 int cmd_data_pending(struct ltt_session
*session
)
2548 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2549 struct ltt_ust_session
*usess
= session
->ust_session
;
2553 /* Session MUST be stopped to ask for data availability. */
2554 if (session
->active
) {
2555 ret
= LTTNG_ERR_SESSION_STARTED
;
2559 * If stopped, just make sure we've started before else the above call
2560 * will always send that there is data pending.
2562 * The consumer assumes that when the data pending command is received,
2563 * the trace has been started before or else no output data is written
2564 * by the streams which is a condition for data pending. So, this is
2565 * *VERY* important that we don't ask the consumer before a start
2568 if (!session
->has_been_started
) {
2574 if (ksess
&& ksess
->consumer
) {
2575 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2577 /* Data is still being extracted for the kernel. */
2582 if (usess
&& usess
->consumer
) {
2583 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2585 /* Data is still being extracted for the kernel. */
2590 /* Data is ready to be read by a viewer */
2598 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2600 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2602 int cmd_snapshot_add_output(struct ltt_session
*session
,
2603 struct lttng_snapshot_output
*output
, uint32_t *id
)
2606 struct snapshot_output
*new_output
;
2611 DBG("Cmd snapshot add output for session %s", session
->name
);
2614 * Permission denied to create an output if the session is not
2615 * set in no output mode.
2617 if (session
->output_traces
) {
2618 ret
= LTTNG_ERR_EPERM
;
2622 /* Only one output is allowed until we have the "tee" feature. */
2623 if (session
->snapshot
.nb_output
== 1) {
2624 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2628 new_output
= snapshot_output_alloc();
2630 ret
= LTTNG_ERR_NOMEM
;
2634 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2635 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2636 &session
->snapshot
);
2638 if (ret
== -ENOMEM
) {
2639 ret
= LTTNG_ERR_NOMEM
;
2641 ret
= LTTNG_ERR_INVALID
;
2647 snapshot_add_output(&session
->snapshot
, new_output
);
2649 *id
= new_output
->id
;
2656 snapshot_output_destroy(new_output
);
2662 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2664 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2666 int cmd_snapshot_del_output(struct ltt_session
*session
,
2667 struct lttng_snapshot_output
*output
)
2670 struct snapshot_output
*sout
= NULL
;
2678 * Permission denied to create an output if the session is not
2679 * set in no output mode.
2681 if (session
->output_traces
) {
2682 ret
= LTTNG_ERR_EPERM
;
2687 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2689 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2690 } else if (*output
->name
!= '\0') {
2691 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2693 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2696 ret
= LTTNG_ERR_INVALID
;
2700 snapshot_delete_output(&session
->snapshot
, sout
);
2701 snapshot_output_destroy(sout
);
2710 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2712 * If no output is available, outputs is untouched and 0 is returned.
2714 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2716 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2717 struct lttng_snapshot_output
**outputs
)
2720 struct lttng_snapshot_output
*list
;
2721 struct lttng_ht_iter iter
;
2722 struct snapshot_output
*output
;
2727 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2730 * Permission denied to create an output if the session is not
2731 * set in no output mode.
2733 if (session
->output_traces
) {
2734 ret
= LTTNG_ERR_EPERM
;
2738 if (session
->snapshot
.nb_output
== 0) {
2743 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2745 ret
= LTTNG_ERR_NOMEM
;
2749 /* Copy list from session to the new list object. */
2750 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2751 output
, node
.node
) {
2752 assert(output
->consumer
);
2753 list
[idx
].id
= output
->id
;
2754 list
[idx
].max_size
= output
->max_size
;
2755 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2756 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2757 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2758 sizeof(list
[idx
].ctrl_url
));
2761 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2762 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2764 ret
= LTTNG_ERR_NOMEM
;
2769 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2770 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2772 ret
= LTTNG_ERR_NOMEM
;
2780 return session
->snapshot
.nb_output
;
2789 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2790 * snapshot output is *not* set with a remote destination.
2792 * Return 0 on success or a LTTNG_ERR code.
2794 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2795 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2798 struct lttng_ht_iter iter
;
2799 struct consumer_socket
*socket
;
2802 assert(snap_output
);
2805 DBG2("Set relayd object from snapshot output");
2807 /* Ignore if snapshot consumer output is not network. */
2808 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2813 * For each consumer socket, create and send the relayd object of the
2817 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2818 socket
, node
.node
) {
2819 ret
= send_consumer_relayd_sockets(0, session
->id
,
2820 snap_output
->consumer
, socket
,
2821 session
->name
, session
->hostname
,
2822 session
->live_timer
);
2823 if (ret
!= LTTNG_OK
) {
2835 * Record a kernel snapshot.
2837 * Return LTTNG_OK on success or a LTTNG_ERR code.
2839 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2840 struct snapshot_output
*output
, struct ltt_session
*session
,
2841 int wait
, uint64_t max_stream_size
)
2849 /* Get the datetime for the snapshot output directory. */
2850 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2851 sizeof(output
->datetime
));
2853 ret
= LTTNG_ERR_INVALID
;
2858 * Copy kernel session sockets so we can communicate with the right
2859 * consumer for the snapshot record command.
2861 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2863 ret
= LTTNG_ERR_NOMEM
;
2867 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2868 if (ret
!= LTTNG_OK
) {
2869 goto error_snapshot
;
2872 ret
= kernel_snapshot_record(ksess
, output
, wait
, max_stream_size
);
2873 if (ret
!= LTTNG_OK
) {
2874 goto error_snapshot
;
2880 /* Clean up copied sockets so this output can use some other later on. */
2881 consumer_destroy_output_sockets(output
->consumer
);
2887 * Record a UST snapshot.
2889 * Return 0 on success or a LTTNG_ERR error code.
2891 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2892 struct snapshot_output
*output
, struct ltt_session
*session
,
2893 int wait
, uint64_t max_stream_size
)
2901 /* Get the datetime for the snapshot output directory. */
2902 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2903 sizeof(output
->datetime
));
2905 ret
= LTTNG_ERR_INVALID
;
2910 * Copy UST session sockets so we can communicate with the right
2911 * consumer for the snapshot record command.
2913 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
2915 ret
= LTTNG_ERR_NOMEM
;
2919 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
2920 if (ret
!= LTTNG_OK
) {
2921 goto error_snapshot
;
2924 ret
= ust_app_snapshot_record(usess
, output
, wait
, max_stream_size
);
2928 ret
= LTTNG_ERR_INVALID
;
2931 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
2934 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
2937 goto error_snapshot
;
2943 /* Clean up copied sockets so this output can use some other later on. */
2944 consumer_destroy_output_sockets(output
->consumer
);
2950 * Return the biggest subbuffer size of all channels in the given session.
2952 static uint64_t get_session_max_subbuf_size(struct ltt_session
*session
)
2954 uint64_t max_size
= 0;
2958 if (session
->kernel_session
) {
2959 struct ltt_kernel_channel
*chan
;
2960 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2963 * For each channel, add to the max size the size of each subbuffer
2964 * multiplied by their sized.
2966 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
2967 if (chan
->channel
->attr
.subbuf_size
> max_size
) {
2968 max_size
= chan
->channel
->attr
.subbuf_size
;
2973 if (session
->ust_session
) {
2974 struct lttng_ht_iter iter
;
2975 struct ltt_ust_channel
*uchan
;
2976 struct ltt_ust_session
*usess
= session
->ust_session
;
2978 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
2980 if (uchan
->attr
.subbuf_size
> max_size
) {
2981 max_size
= uchan
->attr
.subbuf_size
;
2990 * Returns the total number of streams for a session or a negative value
2993 static unsigned int get_session_nb_streams(struct ltt_session
*session
)
2995 unsigned int total_streams
= 0;
2997 if (session
->kernel_session
) {
2998 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3000 total_streams
+= ksess
->stream_count_global
;
3003 if (session
->ust_session
) {
3004 struct ltt_ust_session
*usess
= session
->ust_session
;
3006 total_streams
+= ust_app_get_nb_stream(usess
);
3009 return total_streams
;
3013 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3015 * The wait parameter is ignored so this call always wait for the snapshot to
3016 * complete before returning.
3018 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3020 int cmd_snapshot_record(struct ltt_session
*session
,
3021 struct lttng_snapshot_output
*output
, int wait
)
3024 unsigned int use_tmp_output
= 0;
3025 struct snapshot_output tmp_output
;
3026 unsigned int nb_streams
, snapshot_success
= 0;
3027 uint64_t session_max_size
= 0, max_stream_size
= 0;
3031 DBG("Cmd snapshot record for session %s", session
->name
);
3034 * Permission denied to create an output if the session is not
3035 * set in no output mode.
3037 if (session
->output_traces
) {
3038 ret
= LTTNG_ERR_EPERM
;
3042 /* The session needs to be started at least once. */
3043 if (!session
->has_been_started
) {
3044 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3048 /* Use temporary output for the session. */
3049 if (output
&& *output
->ctrl_url
!= '\0') {
3050 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3051 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3054 if (ret
== -ENOMEM
) {
3055 ret
= LTTNG_ERR_NOMEM
;
3057 ret
= LTTNG_ERR_INVALID
;
3061 /* Use the global session count for the temporary snapshot. */
3062 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3067 * Get the session maximum size for a snapshot meaning it will compute the
3068 * size of all streams from all domain.
3070 max_stream_size
= get_session_max_subbuf_size(session
);
3072 nb_streams
= get_session_nb_streams(session
);
3075 * The maximum size of the snapshot is the number of streams multiplied
3076 * by the biggest subbuf size of all channels in a session which is the
3077 * maximum stream size available for each stream. The session max size
3078 * is now checked against the snapshot max size value given by the user
3079 * and if lower, an error is returned.
3081 session_max_size
= max_stream_size
* nb_streams
;
3084 DBG3("Snapshot max size is %" PRIu64
" for max stream size of %" PRIu64
,
3085 session_max_size
, max_stream_size
);
3088 * If we use a temporary output, check right away if the max size fits else
3089 * for each output the max size will be checked.
3091 if (use_tmp_output
&&
3092 (tmp_output
.max_size
!= 0 &&
3093 tmp_output
.max_size
< session_max_size
)) {
3094 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3098 if (session
->kernel_session
) {
3099 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3101 if (use_tmp_output
) {
3102 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3103 wait
, max_stream_size
);
3104 if (ret
!= LTTNG_OK
) {
3107 snapshot_success
= 1;
3109 struct snapshot_output
*sout
;
3110 struct lttng_ht_iter iter
;
3113 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3114 &iter
.iter
, sout
, node
.node
) {
3116 * Make a local copy of the output and assign the possible
3117 * temporary value given by the caller.
3119 memset(&tmp_output
, 0, sizeof(tmp_output
));
3120 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3122 /* Use temporary max size. */
3123 if (output
->max_size
!= (uint64_t) -1ULL) {
3124 tmp_output
.max_size
= output
->max_size
;
3127 if (tmp_output
.max_size
!= 0 &&
3128 tmp_output
.max_size
< session_max_size
) {
3130 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3134 /* Use temporary name. */
3135 if (*output
->name
!= '\0') {
3136 strncpy(tmp_output
.name
, output
->name
,
3137 sizeof(tmp_output
.name
));
3140 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3142 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3143 session
, wait
, max_stream_size
);
3144 if (ret
!= LTTNG_OK
) {
3148 snapshot_success
= 1;
3154 if (session
->ust_session
) {
3155 struct ltt_ust_session
*usess
= session
->ust_session
;
3157 if (use_tmp_output
) {
3158 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3159 wait
, max_stream_size
);
3160 if (ret
!= LTTNG_OK
) {
3163 snapshot_success
= 1;
3165 struct snapshot_output
*sout
;
3166 struct lttng_ht_iter iter
;
3169 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3170 &iter
.iter
, sout
, node
.node
) {
3172 * Make a local copy of the output and assign the possible
3173 * temporary value given by the caller.
3175 memset(&tmp_output
, 0, sizeof(tmp_output
));
3176 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3178 /* Use temporary max size. */
3179 if (output
->max_size
!= (uint64_t) -1ULL) {
3180 tmp_output
.max_size
= output
->max_size
;
3183 if (tmp_output
.max_size
!= 0 &&
3184 tmp_output
.max_size
< session_max_size
) {
3186 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3190 /* Use temporary name. */
3191 if (*output
->name
!= '\0') {
3192 strncpy(tmp_output
.name
, output
->name
,
3193 sizeof(tmp_output
.name
));
3196 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3198 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3199 wait
, max_stream_size
);
3200 if (ret
!= LTTNG_OK
) {
3204 snapshot_success
= 1;
3210 if (snapshot_success
) {
3211 session
->snapshot
.nb_snapshot
++;
3213 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3221 * Init command subsystem.
3226 * Set network sequence index to 1 for streams to match a relayd
3227 * socket on the consumer side.
3229 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3230 relayd_net_seq_idx
= 1;
3231 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3233 DBG("Command subsystem initialized");