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
);
1073 if (ret
!= LTTNG_OK
) {
1078 ret
= LTTNG_ERR_UNK
;
1082 kernel_wait_quiescent(kernel_tracer_fd
);
1085 case LTTNG_DOMAIN_UST
:
1087 struct ltt_ust_channel
*uchan
;
1088 struct ltt_ust_session
*usess
;
1090 usess
= session
->ust_session
;
1093 * If a non-default channel has been created in the
1094 * session, explicitely require that -c chan_name needs
1097 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1098 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1102 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1104 if (uchan
== NULL
) {
1105 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1109 switch (event
->type
) {
1110 case LTTNG_EVENT_ALL
:
1111 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1112 if (ret
!= LTTNG_OK
) {
1117 ret
= LTTNG_ERR_UNK
;
1121 DBG3("Disable UST event %s in channel %s completed", event_name
,
1125 case LTTNG_DOMAIN_LOG4J
:
1126 case LTTNG_DOMAIN_JUL
:
1129 struct ltt_ust_session
*usess
= session
->ust_session
;
1133 switch (event
->type
) {
1134 case LTTNG_EVENT_ALL
:
1137 ret
= LTTNG_ERR_UNK
;
1141 agt
= trace_ust_find_agent(usess
, domain
);
1143 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1146 /* The wild card * means that everything should be disabled. */
1147 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1148 ret
= event_agent_disable_all(usess
, agt
);
1150 ret
= event_agent_disable(usess
, agt
, event_name
);
1152 if (ret
!= LTTNG_OK
) {
1159 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1160 case LTTNG_DOMAIN_UST_PID
:
1161 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1164 ret
= LTTNG_ERR_UND
;
1176 * Command LTTNG_DISABLE_EVENT for event "*" processed by the client thread.
1179 int disable_kevent_all(struct ltt_session
*session
, int domain
,
1181 struct lttng_event
*event
)
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
, "");
1220 if (ret
!= LTTNG_OK
) {
1225 ret
= LTTNG_ERR_UNK
;
1229 kernel_wait_quiescent(kernel_tracer_fd
);
1233 ret
= LTTNG_ERR_UND
;
1245 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1247 int cmd_add_context(struct ltt_session
*session
, int domain
,
1248 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1250 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1253 case LTTNG_DOMAIN_KERNEL
:
1254 assert(session
->kernel_session
);
1256 if (session
->kernel_session
->channel_count
== 0) {
1257 /* Create default channel */
1258 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1259 if (ret
!= LTTNG_OK
) {
1262 chan_kern_created
= 1;
1264 /* Add kernel context to kernel tracer */
1265 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1266 if (ret
!= LTTNG_OK
) {
1270 case LTTNG_DOMAIN_UST
:
1272 struct ltt_ust_session
*usess
= session
->ust_session
;
1273 unsigned int chan_count
;
1277 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1278 if (chan_count
== 0) {
1279 struct lttng_channel
*attr
;
1280 /* Create default channel */
1281 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1283 ret
= LTTNG_ERR_FATAL
;
1287 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1288 if (ret
!= LTTNG_OK
) {
1293 chan_ust_created
= 1;
1296 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1297 if (ret
!= LTTNG_OK
) {
1303 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1304 case LTTNG_DOMAIN_UST_PID
:
1305 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1308 ret
= LTTNG_ERR_UND
;
1315 if (chan_kern_created
) {
1316 struct ltt_kernel_channel
*kchan
=
1317 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1318 session
->kernel_session
);
1319 /* Created previously, this should NOT fail. */
1321 kernel_destroy_channel(kchan
);
1324 if (chan_ust_created
) {
1325 struct ltt_ust_channel
*uchan
=
1326 trace_ust_find_channel_by_name(
1327 session
->ust_session
->domain_global
.channels
,
1328 DEFAULT_CHANNEL_NAME
);
1329 /* Created previously, this should NOT fail. */
1331 /* Remove from the channel list of the session. */
1332 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1334 trace_ust_destroy_channel(uchan
);
1339 static int validate_event_name(const char *name
)
1342 const char *c
= name
;
1343 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1346 * Make sure that unescaped wildcards are only used as the last
1347 * character of the event name.
1349 while (c
< event_name_end
) {
1357 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1358 /* Wildcard is not the last character */
1359 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1372 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1374 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1375 char *channel_name
, struct lttng_event
*event
,
1376 char *filter_expression
,
1377 struct lttng_filter_bytecode
*filter
,
1378 struct lttng_event_exclusion
*exclusion
,
1381 int ret
, channel_created
= 0;
1382 struct lttng_channel
*attr
;
1386 assert(channel_name
);
1388 DBG("Enable event command for event \'%s\'", event
->name
);
1390 /* Special handling for kernel domain all events. */
1391 if (domain
->type
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event
->name
, "*")) {
1392 return enable_kevent_all(session
, domain
, channel_name
, event
,
1393 filter_expression
, filter
, wpipe
);
1396 ret
= validate_event_name(event
->name
);
1403 switch (domain
->type
) {
1404 case LTTNG_DOMAIN_KERNEL
:
1406 struct ltt_kernel_channel
*kchan
;
1409 * If a non-default channel has been created in the
1410 * session, explicitely require that -c chan_name needs
1413 if (session
->kernel_session
->has_non_default_channel
1414 && channel_name
[0] == '\0') {
1415 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1419 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1420 session
->kernel_session
);
1421 if (kchan
== NULL
) {
1422 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1423 LTTNG_BUFFER_GLOBAL
);
1425 ret
= LTTNG_ERR_FATAL
;
1428 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1430 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1431 if (ret
!= LTTNG_OK
) {
1437 channel_created
= 1;
1440 /* Get the newly created kernel channel pointer */
1441 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1442 session
->kernel_session
);
1443 if (kchan
== NULL
) {
1444 /* This sould not happen... */
1445 ret
= LTTNG_ERR_FATAL
;
1449 switch (event
->type
) {
1450 case LTTNG_EVENT_ALL
:
1451 case LTTNG_EVENT_TRACEPOINT
:
1452 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1453 if (ret
!= LTTNG_OK
) {
1454 if (channel_created
) {
1455 /* Let's not leak a useless channel. */
1456 kernel_destroy_channel(kchan
);
1461 case LTTNG_EVENT_SYSCALL
:
1462 ret
= event_kernel_enable_syscall(kchan
, event
->name
);
1463 if (ret
!= LTTNG_OK
) {
1468 ret
= LTTNG_ERR_UNK
;
1472 kernel_wait_quiescent(kernel_tracer_fd
);
1475 case LTTNG_DOMAIN_UST
:
1477 struct ltt_ust_channel
*uchan
;
1478 struct ltt_ust_session
*usess
= session
->ust_session
;
1483 * If a non-default channel has been created in the
1484 * session, explicitely require that -c chan_name needs
1487 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1488 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1492 /* Get channel from global UST domain */
1493 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1495 if (uchan
== NULL
) {
1496 /* Create default channel */
1497 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1498 usess
->buffer_type
);
1500 ret
= LTTNG_ERR_FATAL
;
1503 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1505 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1506 if (ret
!= LTTNG_OK
) {
1512 /* Get the newly created channel reference back */
1513 uchan
= trace_ust_find_channel_by_name(
1514 usess
->domain_global
.channels
, channel_name
);
1518 /* At this point, the session and channel exist on the tracer */
1519 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1520 filter_expression
, filter
, exclusion
);
1521 if (ret
!= LTTNG_OK
) {
1526 case LTTNG_DOMAIN_LOG4J
:
1527 case LTTNG_DOMAIN_JUL
:
1529 const char *default_event_name
, *default_chan_name
;
1531 struct lttng_event uevent
;
1532 struct lttng_domain tmp_dom
;
1533 struct ltt_ust_session
*usess
= session
->ust_session
;
1537 agt
= trace_ust_find_agent(usess
, domain
->type
);
1539 agt
= agent_create(domain
->type
);
1541 ret
= -LTTNG_ERR_NOMEM
;
1544 agent_add(agt
, usess
->agents
);
1547 /* Create the default tracepoint. */
1548 memset(&uevent
, 0, sizeof(uevent
));
1549 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1550 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1551 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1552 if (!default_event_name
) {
1553 ret
= -LTTNG_ERR_FATAL
;
1556 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1557 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1560 * The domain type is changed because we are about to enable the
1561 * default channel and event for the JUL domain that are hardcoded.
1562 * This happens in the UST domain.
1564 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1565 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1567 if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1568 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1570 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1573 ret
= cmd_enable_event(session
, &tmp_dom
, (char *) default_chan_name
,
1574 &uevent
, filter_expression
, filter
, NULL
, wpipe
);
1575 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1579 /* The wild card * means that everything should be enabled. */
1580 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1581 ret
= event_agent_enable_all(usess
, agt
, event
, filter
);
1583 ret
= event_agent_enable(usess
, agt
, event
, filter
);
1585 if (ret
!= LTTNG_OK
) {
1592 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1593 case LTTNG_DOMAIN_UST_PID
:
1594 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1597 ret
= LTTNG_ERR_UND
;
1609 * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread.
1612 int enable_kevent_all(struct ltt_session
*session
,
1613 struct lttng_domain
*domain
, char *channel_name
,
1614 struct lttng_event
*event
,
1615 char *filter_expression
,
1616 struct lttng_filter_bytecode
*filter
, int wpipe
)
1619 struct lttng_channel
*attr
;
1622 assert(channel_name
);
1626 switch (domain
->type
) {
1627 case LTTNG_DOMAIN_KERNEL
:
1629 struct ltt_kernel_channel
*kchan
;
1631 assert(session
->kernel_session
);
1634 * If a non-default channel has been created in the
1635 * session, explicitely require that -c chan_name needs
1638 if (session
->kernel_session
->has_non_default_channel
1639 && channel_name
[0] == '\0') {
1640 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1644 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1645 session
->kernel_session
);
1646 if (kchan
== NULL
) {
1647 /* Create default channel */
1648 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1649 LTTNG_BUFFER_GLOBAL
);
1651 ret
= LTTNG_ERR_FATAL
;
1654 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1656 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1657 if (ret
!= LTTNG_OK
) {
1663 /* Get the newly created kernel channel pointer */
1664 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1665 session
->kernel_session
);
1669 switch (event
->type
) {
1670 case LTTNG_EVENT_SYSCALL
:
1671 ret
= event_kernel_enable_syscall(kchan
, "");
1672 if (ret
!= LTTNG_OK
) {
1676 case LTTNG_EVENT_TRACEPOINT
:
1678 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1679 * events already registered to the channel.
1681 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1683 case LTTNG_EVENT_ALL
:
1684 /* Enable syscalls and tracepoints */
1685 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1688 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1692 /* Manage return value */
1693 if (ret
!= LTTNG_OK
) {
1695 * On error, cmd_enable_channel call will take care of destroying
1696 * the created channel if it was needed.
1701 kernel_wait_quiescent(kernel_tracer_fd
);
1705 ret
= LTTNG_ERR_UND
;
1718 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1720 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1723 ssize_t nb_events
= 0;
1726 case LTTNG_DOMAIN_KERNEL
:
1727 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1728 if (nb_events
< 0) {
1729 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1733 case LTTNG_DOMAIN_UST
:
1734 nb_events
= ust_app_list_events(events
);
1735 if (nb_events
< 0) {
1736 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1740 case LTTNG_DOMAIN_LOG4J
:
1741 case LTTNG_DOMAIN_JUL
:
1742 nb_events
= agent_list_events(events
, domain
);
1743 if (nb_events
< 0) {
1744 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1749 ret
= LTTNG_ERR_UND
;
1756 /* Return negative value to differentiate return code */
1761 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1763 ssize_t
cmd_list_tracepoint_fields(int domain
,
1764 struct lttng_event_field
**fields
)
1767 ssize_t nb_fields
= 0;
1770 case LTTNG_DOMAIN_UST
:
1771 nb_fields
= ust_app_list_event_fields(fields
);
1772 if (nb_fields
< 0) {
1773 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1777 case LTTNG_DOMAIN_KERNEL
:
1778 default: /* fall-through */
1779 ret
= LTTNG_ERR_UND
;
1786 /* Return negative value to differentiate return code */
1791 * Command LTTNG_START_TRACE processed by the client thread.
1793 int cmd_start_trace(struct ltt_session
*session
)
1796 unsigned long nb_chan
= 0;
1797 struct ltt_kernel_session
*ksession
;
1798 struct ltt_ust_session
*usess
;
1802 /* Ease our life a bit ;) */
1803 ksession
= session
->kernel_session
;
1804 usess
= session
->ust_session
;
1806 /* Is the session already started? */
1807 if (session
->active
) {
1808 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1813 * Starting a session without channel is useless since after that it's not
1814 * possible to enable channel thus inform the client.
1816 if (usess
&& usess
->domain_global
.channels
) {
1817 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1820 nb_chan
+= ksession
->channel_count
;
1823 ret
= LTTNG_ERR_NO_CHANNEL
;
1827 /* Kernel tracing */
1828 if (ksession
!= NULL
) {
1829 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1830 if (ret
!= LTTNG_OK
) {
1835 /* Flag session that trace should start automatically */
1838 * Even though the start trace might fail, flag this session active so
1839 * other application coming in are started by default.
1843 ret
= ust_app_start_trace_all(usess
);
1845 ret
= LTTNG_ERR_UST_START_FAIL
;
1850 /* Flag this after a successful start. */
1851 session
->has_been_started
= 1;
1852 session
->active
= 1;
1861 * Command LTTNG_STOP_TRACE processed by the client thread.
1863 int cmd_stop_trace(struct ltt_session
*session
)
1866 struct ltt_kernel_channel
*kchan
;
1867 struct ltt_kernel_session
*ksession
;
1868 struct ltt_ust_session
*usess
;
1873 ksession
= session
->kernel_session
;
1874 usess
= session
->ust_session
;
1876 /* Session is not active. Skip everythong and inform the client. */
1877 if (!session
->active
) {
1878 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1883 if (ksession
&& ksession
->active
) {
1884 DBG("Stop kernel tracing");
1886 /* Flush metadata if exist */
1887 if (ksession
->metadata_stream_fd
>= 0) {
1888 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1890 ERR("Kernel metadata flush failed");
1894 /* Flush all buffers before stopping */
1895 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1896 ret
= kernel_flush_buffer(kchan
);
1898 ERR("Kernel flush buffer error");
1902 ret
= kernel_stop_session(ksession
);
1904 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1908 kernel_wait_quiescent(kernel_tracer_fd
);
1910 ksession
->active
= 0;
1913 if (usess
&& usess
->active
) {
1915 * Even though the stop trace might fail, flag this session inactive so
1916 * other application coming in are not started by default.
1920 ret
= ust_app_stop_trace_all(usess
);
1922 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1927 /* Flag inactive after a successful stop. */
1928 session
->active
= 0;
1936 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1938 int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
1939 size_t nb_uri
, struct lttng_uri
*uris
)
1942 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1943 struct ltt_ust_session
*usess
= session
->ust_session
;
1944 struct consumer_output
*consumer
= NULL
;
1950 /* Can't set consumer URI if the session is active. */
1951 if (session
->active
) {
1952 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1957 * This case switch makes sure the domain session has a temporary consumer
1958 * so the URL can be set.
1962 /* Code flow error. A session MUST always have a consumer object */
1963 assert(session
->consumer
);
1965 * The URL will be added to the tracing session consumer instead of a
1966 * specific domain consumer.
1968 consumer
= session
->consumer
;
1970 case LTTNG_DOMAIN_KERNEL
:
1971 /* Code flow error if we don't have a kernel session here. */
1973 assert(ksess
->consumer
);
1974 consumer
= ksess
->consumer
;
1976 case LTTNG_DOMAIN_UST
:
1977 /* Code flow error if we don't have a kernel session here. */
1979 assert(usess
->consumer
);
1980 consumer
= usess
->consumer
;
1984 for (i
= 0; i
< nb_uri
; i
++) {
1985 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
1986 if (ret
!= LTTNG_OK
) {
1992 * Make sure to set the session in output mode after we set URI since a
1993 * session can be created without URL (thus flagged in no output mode).
1995 session
->output_traces
= 1;
1997 ksess
->output_traces
= 1;
1999 usess
->output_traces
= 1;
2010 * Command LTTNG_CREATE_SESSION processed by the client thread.
2012 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2013 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2016 struct ltt_session
*session
;
2022 * Verify if the session already exist
2024 * XXX: There is no need for the session lock list here since the caller
2025 * (process_client_msg) is holding it. We might want to change that so a
2026 * single command does not lock the entire session list.
2028 session
= session_find_by_name(name
);
2029 if (session
!= NULL
) {
2030 ret
= LTTNG_ERR_EXIST_SESS
;
2034 /* Create tracing session in the registry */
2035 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2036 LTTNG_SOCK_GET_GID_CRED(creds
));
2037 if (ret
!= LTTNG_OK
) {
2042 * Get the newly created session pointer back
2044 * XXX: There is no need for the session lock list here since the caller
2045 * (process_client_msg) is holding it. We might want to change that so a
2046 * single command does not lock the entire session list.
2048 session
= session_find_by_name(name
);
2051 session
->live_timer
= live_timer
;
2052 /* Create default consumer output for the session not yet created. */
2053 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2054 if (session
->consumer
== NULL
) {
2055 ret
= LTTNG_ERR_FATAL
;
2056 goto consumer_error
;
2060 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
2061 if (ret
!= LTTNG_OK
) {
2062 goto consumer_error
;
2064 session
->output_traces
= 1;
2066 session
->output_traces
= 0;
2067 DBG2("Session %s created with no output", session
->name
);
2070 session
->consumer
->enabled
= 1;
2075 session_destroy(session
);
2082 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2084 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2085 size_t nb_uri
, lttng_sock_cred
*creds
)
2088 struct ltt_session
*session
;
2089 struct snapshot_output
*new_output
= NULL
;
2095 * Create session in no output mode with URIs set to NULL. The uris we've
2096 * received are for a default snapshot output if one.
2098 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2099 if (ret
!= LTTNG_OK
) {
2103 /* Get the newly created session pointer back. This should NEVER fail. */
2104 session
= session_find_by_name(name
);
2107 /* Flag session for snapshot mode. */
2108 session
->snapshot_mode
= 1;
2110 /* Skip snapshot output creation if no URI is given. */
2115 new_output
= snapshot_output_alloc();
2117 ret
= LTTNG_ERR_NOMEM
;
2118 goto error_snapshot_alloc
;
2121 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2122 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2124 if (ret
== -ENOMEM
) {
2125 ret
= LTTNG_ERR_NOMEM
;
2127 ret
= LTTNG_ERR_INVALID
;
2129 goto error_snapshot
;
2133 snapshot_add_output(&session
->snapshot
, new_output
);
2140 snapshot_output_destroy(new_output
);
2141 error_snapshot_alloc
:
2142 session_destroy(session
);
2148 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2150 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2153 struct ltt_ust_session
*usess
;
2154 struct ltt_kernel_session
*ksess
;
2159 usess
= session
->ust_session
;
2160 ksess
= session
->kernel_session
;
2162 /* Clean kernel session teardown */
2163 kernel_destroy_session(ksess
);
2165 /* UST session teardown */
2167 /* Close any relayd session */
2168 consumer_output_send_destroy_relayd(usess
->consumer
);
2170 /* Destroy every UST application related to this session. */
2171 ret
= ust_app_destroy_trace_all(usess
);
2173 ERR("Error in ust_app_destroy_trace_all");
2176 /* Clean up the rest. */
2177 trace_ust_destroy_session(usess
);
2181 * Must notify the kernel thread here to update it's poll set in order to
2182 * remove the channel(s)' fd just destroyed.
2184 ret
= notify_thread_pipe(wpipe
);
2186 PERROR("write kernel poll pipe");
2189 ret
= session_destroy(session
);
2195 * Command LTTNG_CALIBRATE processed by the client thread.
2197 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2202 case LTTNG_DOMAIN_KERNEL
:
2204 struct lttng_kernel_calibrate kcalibrate
;
2206 switch (calibrate
->type
) {
2207 case LTTNG_CALIBRATE_FUNCTION
:
2209 /* Default and only possible calibrate option. */
2210 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2214 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2216 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2221 case LTTNG_DOMAIN_UST
:
2223 struct lttng_ust_calibrate ucalibrate
;
2225 switch (calibrate
->type
) {
2226 case LTTNG_CALIBRATE_FUNCTION
:
2228 /* Default and only possible calibrate option. */
2229 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2233 ret
= ust_app_calibrate_glb(&ucalibrate
);
2235 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2241 ret
= LTTNG_ERR_UND
;
2252 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2254 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2255 const char *sock_path
, struct consumer_data
*cdata
)
2258 struct consumer_socket
*socket
= NULL
;
2265 case LTTNG_DOMAIN_KERNEL
:
2267 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2271 /* Can't register a consumer if there is already one */
2272 if (ksess
->consumer_fds_sent
!= 0) {
2273 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2277 sock
= lttcomm_connect_unix_sock(sock_path
);
2279 ret
= LTTNG_ERR_CONNECT_FAIL
;
2282 cdata
->cmd_sock
= sock
;
2284 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2285 if (socket
== NULL
) {
2288 PERROR("close register consumer");
2290 cdata
->cmd_sock
= -1;
2291 ret
= LTTNG_ERR_FATAL
;
2295 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2296 if (socket
->lock
== NULL
) {
2297 PERROR("zmalloc pthread mutex");
2298 ret
= LTTNG_ERR_FATAL
;
2301 pthread_mutex_init(socket
->lock
, NULL
);
2302 socket
->registered
= 1;
2305 consumer_add_socket(socket
, ksess
->consumer
);
2308 pthread_mutex_lock(&cdata
->pid_mutex
);
2310 pthread_mutex_unlock(&cdata
->pid_mutex
);
2315 /* TODO: Userspace tracing */
2316 ret
= LTTNG_ERR_UND
;
2324 consumer_destroy_socket(socket
);
2330 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2332 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2333 struct lttng_domain
**domains
)
2338 struct lttng_ht_iter iter
;
2340 if (session
->kernel_session
!= NULL
) {
2341 DBG3("Listing domains found kernel domain");
2345 if (session
->ust_session
!= NULL
) {
2346 DBG3("Listing domains found UST global domain");
2349 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2351 if (agt
->being_used
) {
2357 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2358 if (*domains
== NULL
) {
2359 ret
= LTTNG_ERR_FATAL
;
2363 if (session
->kernel_session
!= NULL
) {
2364 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2368 if (session
->ust_session
!= NULL
) {
2369 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2370 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2373 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2375 if (agt
->being_used
) {
2376 (*domains
)[index
].type
= agt
->domain
;
2377 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2386 /* Return negative value to differentiate return code */
2392 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2394 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2395 struct lttng_channel
**channels
)
2398 ssize_t nb_chan
= 0;
2401 case LTTNG_DOMAIN_KERNEL
:
2402 if (session
->kernel_session
!= NULL
) {
2403 nb_chan
= session
->kernel_session
->channel_count
;
2405 DBG3("Number of kernel channels %zd", nb_chan
);
2407 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2410 case LTTNG_DOMAIN_UST
:
2411 if (session
->ust_session
!= NULL
) {
2412 nb_chan
= lttng_ht_get_count(
2413 session
->ust_session
->domain_global
.channels
);
2415 DBG3("Number of UST global channels %zd", nb_chan
);
2417 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2422 ret
= LTTNG_ERR_UND
;
2427 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2428 if (*channels
== NULL
) {
2429 ret
= LTTNG_ERR_FATAL
;
2433 list_lttng_channels(domain
, session
, *channels
);
2436 /* Ret value was set in the domain switch case */
2443 /* Return negative value to differentiate return code */
2448 * Command LTTNG_LIST_EVENTS processed by the client thread.
2450 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2451 char *channel_name
, struct lttng_event
**events
)
2454 ssize_t nb_event
= 0;
2457 case LTTNG_DOMAIN_KERNEL
:
2458 if (session
->kernel_session
!= NULL
) {
2459 nb_event
= list_lttng_kernel_events(channel_name
,
2460 session
->kernel_session
, events
);
2463 case LTTNG_DOMAIN_UST
:
2465 if (session
->ust_session
!= NULL
) {
2466 nb_event
= list_lttng_ust_global_events(channel_name
,
2467 &session
->ust_session
->domain_global
, events
);
2471 case LTTNG_DOMAIN_LOG4J
:
2472 case LTTNG_DOMAIN_JUL
:
2473 if (session
->ust_session
) {
2474 struct lttng_ht_iter iter
;
2477 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2478 &iter
.iter
, agt
, node
.node
) {
2479 nb_event
= list_lttng_agent_events(agt
, events
);
2484 ret
= LTTNG_ERR_UND
;
2491 /* Return negative value to differentiate return code */
2496 * Using the session list, filled a lttng_session array to send back to the
2497 * client for session listing.
2499 * The session list lock MUST be acquired before calling this function. Use
2500 * session_lock_list() and session_unlock_list().
2502 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2507 struct ltt_session
*session
;
2508 struct ltt_session_list
*list
= session_get_list();
2510 DBG("Getting all available session for UID %d GID %d",
2513 * Iterate over session list and append data after the control struct in
2516 cds_list_for_each_entry(session
, &list
->head
, list
) {
2518 * Only list the sessions the user can control.
2520 if (!session_access_ok(session
, uid
, gid
)) {
2524 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2525 struct ltt_ust_session
*usess
= session
->ust_session
;
2527 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2528 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2529 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2530 ret
= build_network_session_path(sessions
[i
].path
,
2531 sizeof(sessions
[i
].path
), session
);
2533 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2534 session
->consumer
->dst
.trace_path
);
2537 PERROR("snprintf session path");
2541 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2542 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2543 sessions
[i
].enabled
= session
->active
;
2544 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2545 sessions
[i
].live_timer_interval
= session
->live_timer
;
2551 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2552 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2554 int cmd_data_pending(struct ltt_session
*session
)
2557 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2558 struct ltt_ust_session
*usess
= session
->ust_session
;
2562 /* Session MUST be stopped to ask for data availability. */
2563 if (session
->active
) {
2564 ret
= LTTNG_ERR_SESSION_STARTED
;
2568 * If stopped, just make sure we've started before else the above call
2569 * will always send that there is data pending.
2571 * The consumer assumes that when the data pending command is received,
2572 * the trace has been started before or else no output data is written
2573 * by the streams which is a condition for data pending. So, this is
2574 * *VERY* important that we don't ask the consumer before a start
2577 if (!session
->has_been_started
) {
2583 if (ksess
&& ksess
->consumer
) {
2584 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2586 /* Data is still being extracted for the kernel. */
2591 if (usess
&& usess
->consumer
) {
2592 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2594 /* Data is still being extracted for the kernel. */
2599 /* Data is ready to be read by a viewer */
2607 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2609 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2611 int cmd_snapshot_add_output(struct ltt_session
*session
,
2612 struct lttng_snapshot_output
*output
, uint32_t *id
)
2615 struct snapshot_output
*new_output
;
2620 DBG("Cmd snapshot add output for session %s", session
->name
);
2623 * Permission denied to create an output if the session is not
2624 * set in no output mode.
2626 if (session
->output_traces
) {
2627 ret
= LTTNG_ERR_EPERM
;
2631 /* Only one output is allowed until we have the "tee" feature. */
2632 if (session
->snapshot
.nb_output
== 1) {
2633 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2637 new_output
= snapshot_output_alloc();
2639 ret
= LTTNG_ERR_NOMEM
;
2643 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2644 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2645 &session
->snapshot
);
2647 if (ret
== -ENOMEM
) {
2648 ret
= LTTNG_ERR_NOMEM
;
2650 ret
= LTTNG_ERR_INVALID
;
2656 snapshot_add_output(&session
->snapshot
, new_output
);
2658 *id
= new_output
->id
;
2665 snapshot_output_destroy(new_output
);
2671 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2673 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2675 int cmd_snapshot_del_output(struct ltt_session
*session
,
2676 struct lttng_snapshot_output
*output
)
2679 struct snapshot_output
*sout
= NULL
;
2687 * Permission denied to create an output if the session is not
2688 * set in no output mode.
2690 if (session
->output_traces
) {
2691 ret
= LTTNG_ERR_EPERM
;
2696 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2698 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2699 } else if (*output
->name
!= '\0') {
2700 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2702 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2705 ret
= LTTNG_ERR_INVALID
;
2709 snapshot_delete_output(&session
->snapshot
, sout
);
2710 snapshot_output_destroy(sout
);
2719 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2721 * If no output is available, outputs is untouched and 0 is returned.
2723 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2725 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2726 struct lttng_snapshot_output
**outputs
)
2729 struct lttng_snapshot_output
*list
;
2730 struct lttng_ht_iter iter
;
2731 struct snapshot_output
*output
;
2736 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2739 * Permission denied to create an output if the session is not
2740 * set in no output mode.
2742 if (session
->output_traces
) {
2743 ret
= LTTNG_ERR_EPERM
;
2747 if (session
->snapshot
.nb_output
== 0) {
2752 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2754 ret
= LTTNG_ERR_NOMEM
;
2758 /* Copy list from session to the new list object. */
2759 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2760 output
, node
.node
) {
2761 assert(output
->consumer
);
2762 list
[idx
].id
= output
->id
;
2763 list
[idx
].max_size
= output
->max_size
;
2764 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2765 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2766 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2767 sizeof(list
[idx
].ctrl_url
));
2770 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2771 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2773 ret
= LTTNG_ERR_NOMEM
;
2778 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2779 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2781 ret
= LTTNG_ERR_NOMEM
;
2789 return session
->snapshot
.nb_output
;
2798 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2799 * snapshot output is *not* set with a remote destination.
2801 * Return 0 on success or a LTTNG_ERR code.
2803 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2804 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2807 struct lttng_ht_iter iter
;
2808 struct consumer_socket
*socket
;
2811 assert(snap_output
);
2814 DBG2("Set relayd object from snapshot output");
2816 /* Ignore if snapshot consumer output is not network. */
2817 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2822 * For each consumer socket, create and send the relayd object of the
2826 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2827 socket
, node
.node
) {
2828 ret
= send_consumer_relayd_sockets(0, session
->id
,
2829 snap_output
->consumer
, socket
,
2830 session
->name
, session
->hostname
,
2831 session
->live_timer
);
2832 if (ret
!= LTTNG_OK
) {
2844 * Record a kernel snapshot.
2846 * Return LTTNG_OK on success or a LTTNG_ERR code.
2848 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2849 struct snapshot_output
*output
, struct ltt_session
*session
,
2850 int wait
, uint64_t max_stream_size
)
2858 /* Get the datetime for the snapshot output directory. */
2859 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2860 sizeof(output
->datetime
));
2862 ret
= LTTNG_ERR_INVALID
;
2867 * Copy kernel session sockets so we can communicate with the right
2868 * consumer for the snapshot record command.
2870 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2872 ret
= LTTNG_ERR_NOMEM
;
2876 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2877 if (ret
!= LTTNG_OK
) {
2878 goto error_snapshot
;
2881 ret
= kernel_snapshot_record(ksess
, output
, wait
, max_stream_size
);
2882 if (ret
!= LTTNG_OK
) {
2883 goto error_snapshot
;
2889 /* Clean up copied sockets so this output can use some other later on. */
2890 consumer_destroy_output_sockets(output
->consumer
);
2896 * Record a UST snapshot.
2898 * Return 0 on success or a LTTNG_ERR error code.
2900 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2901 struct snapshot_output
*output
, struct ltt_session
*session
,
2902 int wait
, uint64_t max_stream_size
)
2910 /* Get the datetime for the snapshot output directory. */
2911 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2912 sizeof(output
->datetime
));
2914 ret
= LTTNG_ERR_INVALID
;
2919 * Copy UST session sockets so we can communicate with the right
2920 * consumer for the snapshot record command.
2922 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
2924 ret
= LTTNG_ERR_NOMEM
;
2928 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
2929 if (ret
!= LTTNG_OK
) {
2930 goto error_snapshot
;
2933 ret
= ust_app_snapshot_record(usess
, output
, wait
, max_stream_size
);
2937 ret
= LTTNG_ERR_INVALID
;
2940 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
2943 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
2946 goto error_snapshot
;
2952 /* Clean up copied sockets so this output can use some other later on. */
2953 consumer_destroy_output_sockets(output
->consumer
);
2959 * Return the biggest subbuffer size of all channels in the given session.
2961 static uint64_t get_session_max_subbuf_size(struct ltt_session
*session
)
2963 uint64_t max_size
= 0;
2967 if (session
->kernel_session
) {
2968 struct ltt_kernel_channel
*chan
;
2969 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2972 * For each channel, add to the max size the size of each subbuffer
2973 * multiplied by their sized.
2975 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
2976 if (chan
->channel
->attr
.subbuf_size
> max_size
) {
2977 max_size
= chan
->channel
->attr
.subbuf_size
;
2982 if (session
->ust_session
) {
2983 struct lttng_ht_iter iter
;
2984 struct ltt_ust_channel
*uchan
;
2985 struct ltt_ust_session
*usess
= session
->ust_session
;
2987 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
2989 if (uchan
->attr
.subbuf_size
> max_size
) {
2990 max_size
= uchan
->attr
.subbuf_size
;
2999 * Returns the total number of streams for a session or a negative value
3002 static unsigned int get_session_nb_streams(struct ltt_session
*session
)
3004 unsigned int total_streams
= 0;
3006 if (session
->kernel_session
) {
3007 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3009 total_streams
+= ksess
->stream_count_global
;
3012 if (session
->ust_session
) {
3013 struct ltt_ust_session
*usess
= session
->ust_session
;
3015 total_streams
+= ust_app_get_nb_stream(usess
);
3018 return total_streams
;
3022 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3024 * The wait parameter is ignored so this call always wait for the snapshot to
3025 * complete before returning.
3027 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3029 int cmd_snapshot_record(struct ltt_session
*session
,
3030 struct lttng_snapshot_output
*output
, int wait
)
3033 unsigned int use_tmp_output
= 0;
3034 struct snapshot_output tmp_output
;
3035 unsigned int nb_streams
, snapshot_success
= 0;
3036 uint64_t session_max_size
= 0, max_stream_size
= 0;
3040 DBG("Cmd snapshot record for session %s", session
->name
);
3043 * Permission denied to create an output if the session is not
3044 * set in no output mode.
3046 if (session
->output_traces
) {
3047 ret
= LTTNG_ERR_EPERM
;
3051 /* The session needs to be started at least once. */
3052 if (!session
->has_been_started
) {
3053 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3057 /* Use temporary output for the session. */
3058 if (output
&& *output
->ctrl_url
!= '\0') {
3059 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3060 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3063 if (ret
== -ENOMEM
) {
3064 ret
= LTTNG_ERR_NOMEM
;
3066 ret
= LTTNG_ERR_INVALID
;
3070 /* Use the global session count for the temporary snapshot. */
3071 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3076 * Get the session maximum size for a snapshot meaning it will compute the
3077 * size of all streams from all domain.
3079 max_stream_size
= get_session_max_subbuf_size(session
);
3081 nb_streams
= get_session_nb_streams(session
);
3084 * The maximum size of the snapshot is the number of streams multiplied
3085 * by the biggest subbuf size of all channels in a session which is the
3086 * maximum stream size available for each stream. The session max size
3087 * is now checked against the snapshot max size value given by the user
3088 * and if lower, an error is returned.
3090 session_max_size
= max_stream_size
* nb_streams
;
3093 DBG3("Snapshot max size is %" PRIu64
" for max stream size of %" PRIu64
,
3094 session_max_size
, max_stream_size
);
3097 * If we use a temporary output, check right away if the max size fits else
3098 * for each output the max size will be checked.
3100 if (use_tmp_output
&&
3101 (tmp_output
.max_size
!= 0 &&
3102 tmp_output
.max_size
< session_max_size
)) {
3103 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3107 if (session
->kernel_session
) {
3108 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3110 if (use_tmp_output
) {
3111 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3112 wait
, max_stream_size
);
3113 if (ret
!= LTTNG_OK
) {
3116 snapshot_success
= 1;
3118 struct snapshot_output
*sout
;
3119 struct lttng_ht_iter iter
;
3122 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3123 &iter
.iter
, sout
, node
.node
) {
3125 * Make a local copy of the output and assign the possible
3126 * temporary value given by the caller.
3128 memset(&tmp_output
, 0, sizeof(tmp_output
));
3129 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3131 /* Use temporary max size. */
3132 if (output
->max_size
!= (uint64_t) -1ULL) {
3133 tmp_output
.max_size
= output
->max_size
;
3136 if (tmp_output
.max_size
!= 0 &&
3137 tmp_output
.max_size
< session_max_size
) {
3139 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3143 /* Use temporary name. */
3144 if (*output
->name
!= '\0') {
3145 strncpy(tmp_output
.name
, output
->name
,
3146 sizeof(tmp_output
.name
));
3149 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3151 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3152 session
, wait
, max_stream_size
);
3153 if (ret
!= LTTNG_OK
) {
3157 snapshot_success
= 1;
3163 if (session
->ust_session
) {
3164 struct ltt_ust_session
*usess
= session
->ust_session
;
3166 if (use_tmp_output
) {
3167 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3168 wait
, max_stream_size
);
3169 if (ret
!= LTTNG_OK
) {
3172 snapshot_success
= 1;
3174 struct snapshot_output
*sout
;
3175 struct lttng_ht_iter iter
;
3178 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3179 &iter
.iter
, sout
, node
.node
) {
3181 * Make a local copy of the output and assign the possible
3182 * temporary value given by the caller.
3184 memset(&tmp_output
, 0, sizeof(tmp_output
));
3185 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3187 /* Use temporary max size. */
3188 if (output
->max_size
!= (uint64_t) -1ULL) {
3189 tmp_output
.max_size
= output
->max_size
;
3192 if (tmp_output
.max_size
!= 0 &&
3193 tmp_output
.max_size
< session_max_size
) {
3195 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3199 /* Use temporary name. */
3200 if (*output
->name
!= '\0') {
3201 strncpy(tmp_output
.name
, output
->name
,
3202 sizeof(tmp_output
.name
));
3205 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3207 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3208 wait
, max_stream_size
);
3209 if (ret
!= LTTNG_OK
) {
3213 snapshot_success
= 1;
3219 if (snapshot_success
) {
3220 session
->snapshot
.nb_snapshot
++;
3222 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3230 * Init command subsystem.
3235 * Set network sequence index to 1 for streams to match a relayd
3236 * socket on the consumer side.
3238 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3239 relayd_net_seq_idx
= 1;
3240 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3242 DBG("Command subsystem initialized");