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.
22 #include <urcu/list.h>
23 #include <urcu/uatomic.h>
25 #include <common/defaults.h>
26 #include <common/common.h>
27 #include <common/sessiond-comm/sessiond-comm.h>
28 #include <common/relayd/relayd.h>
29 #include <common/utils.h>
34 #include "health-sessiond.h"
36 #include "kernel-consumer.h"
37 #include "lttng-sessiond.h"
43 * Used to keep a unique index for each relayd socket created where this value
44 * is associated with streams on the consumer so it can match the right relayd
45 * to send to. It must be accessed with the relayd_net_seq_idx_lock
48 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
49 static uint64_t relayd_net_seq_idx
;
52 * Create a session path used by list_lttng_sessions for the case that the
53 * session consumer is on the network.
55 static int build_network_session_path(char *dst
, size_t size
,
56 struct ltt_session
*session
)
58 int ret
, kdata_port
, udata_port
;
59 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
60 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
65 memset(tmp_urls
, 0, sizeof(tmp_urls
));
66 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
68 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
70 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
71 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
72 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
75 if (session
->ust_session
&& session
->ust_session
->consumer
) {
76 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
77 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
80 if (uuri
== NULL
&& kuri
== NULL
) {
81 uri
= &session
->consumer
->dst
.net
.control
;
82 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
83 } else if (kuri
&& uuri
) {
84 ret
= uri_compare(kuri
, uuri
);
88 /* Build uuri URL string */
89 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
96 } else if (kuri
&& uuri
== NULL
) {
98 } else if (uuri
&& kuri
== NULL
) {
102 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
108 * Do we have a UST url set. If yes, this means we have both kernel and UST
111 if (*tmp_uurl
!= '\0') {
112 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
113 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
116 if (kuri
|| (!kuri
&& !uuri
)) {
119 /* No kernel URI, use the UST port. */
122 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
130 * Fill lttng_channel array of all channels.
132 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
133 struct lttng_channel
*channels
)
136 struct ltt_kernel_channel
*kchan
;
138 DBG("Listing channels for session %s", session
->name
);
141 case LTTNG_DOMAIN_KERNEL
:
142 /* Kernel channels */
143 if (session
->kernel_session
!= NULL
) {
144 cds_list_for_each_entry(kchan
,
145 &session
->kernel_session
->channel_list
.head
, list
) {
146 /* Copy lttng_channel struct to array */
147 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
148 channels
[i
].enabled
= kchan
->enabled
;
153 case LTTNG_DOMAIN_UST
:
155 struct lttng_ht_iter iter
;
156 struct ltt_ust_channel
*uchan
;
159 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
160 &iter
.iter
, uchan
, node
.node
) {
161 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
162 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
163 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
164 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
165 channels
[i
].attr
.switch_timer_interval
=
166 uchan
->attr
.switch_timer_interval
;
167 channels
[i
].attr
.read_timer_interval
=
168 uchan
->attr
.read_timer_interval
;
169 channels
[i
].enabled
= uchan
->enabled
;
170 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
171 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
172 switch (uchan
->attr
.output
) {
175 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
189 * Create a list of JUL domain events.
191 * Return number of events in list on success or else a negative value.
193 static int list_lttng_jul_events(struct jul_domain
*dom
,
194 struct lttng_event
**events
)
197 unsigned int nb_event
= 0;
198 struct jul_event
*event
;
199 struct lttng_event
*tmp_events
;
200 struct lttng_ht_iter iter
;
205 DBG3("Listing JUL events");
207 nb_event
= lttng_ht_get_count(dom
->events
);
213 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
215 PERROR("zmalloc JUL events session");
216 ret
= -LTTNG_ERR_FATAL
;
221 cds_lfht_for_each_entry(dom
->events
->ht
, &iter
.iter
, event
, node
.node
) {
222 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
223 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
224 tmp_events
[i
].enabled
= event
->enabled
;
225 tmp_events
[i
].loglevel
= event
->loglevel
;
226 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
231 *events
= tmp_events
;
235 assert(nb_event
== i
);
240 * Create a list of ust global domain events.
242 static int list_lttng_ust_global_events(char *channel_name
,
243 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
246 unsigned int nb_event
= 0;
247 struct lttng_ht_iter iter
;
248 struct lttng_ht_node_str
*node
;
249 struct ltt_ust_channel
*uchan
;
250 struct ltt_ust_event
*uevent
;
251 struct lttng_event
*tmp
;
253 DBG("Listing UST global events for channel %s", channel_name
);
257 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
258 node
= lttng_ht_iter_get_node_str(&iter
);
260 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
264 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
266 nb_event
+= lttng_ht_get_count(uchan
->events
);
273 DBG3("Listing UST global %d events", nb_event
);
275 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
277 ret
= LTTNG_ERR_FATAL
;
281 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
282 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
283 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
284 tmp
[i
].enabled
= uevent
->enabled
;
286 switch (uevent
->attr
.instrumentation
) {
287 case LTTNG_UST_TRACEPOINT
:
288 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
290 case LTTNG_UST_PROBE
:
291 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
293 case LTTNG_UST_FUNCTION
:
294 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
298 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
299 switch (uevent
->attr
.loglevel_type
) {
300 case LTTNG_UST_LOGLEVEL_ALL
:
301 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
303 case LTTNG_UST_LOGLEVEL_RANGE
:
304 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
306 case LTTNG_UST_LOGLEVEL_SINGLE
:
307 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
310 if (uevent
->filter
) {
313 if (uevent
->exclusion
) {
314 tmp
[i
].exclusion
= 1;
328 * Fill lttng_event array of all kernel events in the channel.
330 static int list_lttng_kernel_events(char *channel_name
,
331 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
334 unsigned int nb_event
;
335 struct ltt_kernel_event
*event
;
336 struct ltt_kernel_channel
*kchan
;
338 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
340 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
344 nb_event
= kchan
->event_count
;
346 DBG("Listing events for channel %s", kchan
->channel
->name
);
352 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
353 if (*events
== NULL
) {
354 ret
= LTTNG_ERR_FATAL
;
358 /* Kernel channels */
359 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
360 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
361 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
362 (*events
)[i
].enabled
= event
->enabled
;
364 switch (event
->event
->instrumentation
) {
365 case LTTNG_KERNEL_TRACEPOINT
:
366 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
368 case LTTNG_KERNEL_KRETPROBE
:
369 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
370 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
371 sizeof(struct lttng_kernel_kprobe
));
373 case LTTNG_KERNEL_KPROBE
:
374 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
375 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
376 sizeof(struct lttng_kernel_kprobe
));
378 case LTTNG_KERNEL_FUNCTION
:
379 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
380 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
381 sizeof(struct lttng_kernel_function
));
383 case LTTNG_KERNEL_NOOP
:
384 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
386 case LTTNG_KERNEL_SYSCALL
:
387 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
389 case LTTNG_KERNEL_ALL
:
400 /* Negate the error code to differentiate the size from an error */
405 * Add URI so the consumer output object. Set the correct path depending on the
406 * domain adding the default trace directory.
408 static int add_uri_to_consumer(struct consumer_output
*consumer
,
409 struct lttng_uri
*uri
, int domain
, const char *session_name
)
412 const char *default_trace_dir
;
416 if (consumer
== NULL
) {
417 DBG("No consumer detected. Don't add URI. Stopping.");
418 ret
= LTTNG_ERR_NO_CONSUMER
;
423 case LTTNG_DOMAIN_KERNEL
:
424 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
426 case LTTNG_DOMAIN_UST
:
427 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
431 * This case is possible is we try to add the URI to the global tracing
432 * session consumer object which in this case there is no subdir.
434 default_trace_dir
= "";
437 switch (uri
->dtype
) {
440 DBG2("Setting network URI to consumer");
442 if (consumer
->type
== CONSUMER_DST_NET
) {
443 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
444 consumer
->dst
.net
.control_isset
) ||
445 (uri
->stype
== LTTNG_STREAM_DATA
&&
446 consumer
->dst
.net
.data_isset
)) {
447 ret
= LTTNG_ERR_URL_EXIST
;
451 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
454 consumer
->type
= CONSUMER_DST_NET
;
456 /* Set URI into consumer output object */
457 ret
= consumer_set_network_uri(consumer
, uri
);
461 } else if (ret
== 1) {
463 * URI was the same in the consumer so we do not append the subdir
464 * again so to not duplicate output dir.
470 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
471 ret
= consumer_set_subdir(consumer
, session_name
);
473 ret
= LTTNG_ERR_FATAL
;
478 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
479 /* On a new subdir, reappend the default trace dir. */
480 strncat(consumer
->subdir
, default_trace_dir
,
481 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
482 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
487 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
488 memset(consumer
->dst
.trace_path
, 0,
489 sizeof(consumer
->dst
.trace_path
));
490 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
491 sizeof(consumer
->dst
.trace_path
));
492 /* Append default trace dir */
493 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
494 sizeof(consumer
->dst
.trace_path
) -
495 strlen(consumer
->dst
.trace_path
) - 1);
496 /* Flag consumer as local. */
497 consumer
->type
= CONSUMER_DST_LOCAL
;
508 * Init tracing by creating trace directory and sending fds kernel consumer.
510 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
513 struct lttng_ht_iter iter
;
514 struct consumer_socket
*socket
;
520 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
521 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
523 pthread_mutex_lock(socket
->lock
);
524 ret
= kernel_consumer_send_session(socket
, session
);
525 pthread_mutex_unlock(socket
->lock
);
527 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
539 * Create a socket to the relayd using the URI.
541 * On success, the relayd_sock pointer is set to the created socket.
542 * Else, it's stays untouched and a lttcomm error code is returned.
544 static int create_connect_relayd(struct lttng_uri
*uri
,
545 struct lttcomm_relayd_sock
**relayd_sock
)
548 struct lttcomm_relayd_sock
*rsock
;
550 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
551 RELAYD_VERSION_COMM_MINOR
);
553 ret
= LTTNG_ERR_FATAL
;
558 * Connect to relayd so we can proceed with a session creation. This call
559 * can possibly block for an arbitrary amount of time to set the health
560 * state to be in poll execution.
563 ret
= relayd_connect(rsock
);
566 ERR("Unable to reach lttng-relayd");
567 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
571 /* Create socket for control stream. */
572 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
573 DBG3("Creating relayd stream socket from URI");
575 /* Check relayd version */
576 ret
= relayd_version_check(rsock
);
578 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
581 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
582 DBG3("Creating relayd data socket from URI");
584 /* Command is not valid */
585 ERR("Relayd invalid stream type: %d", uri
->stype
);
586 ret
= LTTNG_ERR_INVALID
;
590 *relayd_sock
= rsock
;
595 /* The returned value is not useful since we are on an error path. */
596 (void) relayd_close(rsock
);
604 * Connect to the relayd using URI and send the socket to the right consumer.
606 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
607 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
608 struct consumer_socket
*consumer_sock
,
609 char *session_name
, char *hostname
, int session_live_timer
)
612 struct lttcomm_relayd_sock
*rsock
= NULL
;
614 /* Connect to relayd and make version check if uri is the control. */
615 ret
= create_connect_relayd(relayd_uri
, &rsock
);
616 if (ret
!= LTTNG_OK
) {
621 /* Set the network sequence index if not set. */
622 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
623 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
625 * Increment net_seq_idx because we are about to transfer the
626 * new relayd socket to the consumer.
627 * Assign unique key so the consumer can match streams.
629 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
630 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
633 /* Send relayd socket to consumer. */
634 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
635 relayd_uri
->stype
, session_id
,
636 session_name
, hostname
, session_live_timer
);
638 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
642 /* Flag that the corresponding socket was sent. */
643 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
644 consumer_sock
->control_sock_sent
= 1;
645 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
646 consumer_sock
->data_sock_sent
= 1;
652 * Close socket which was dup on the consumer side. The session daemon does
653 * NOT keep track of the relayd socket(s) once transfer to the consumer.
657 (void) relayd_close(rsock
);
661 if (ret
!= LTTNG_OK
) {
663 * The consumer output for this session should not be used anymore
664 * since the relayd connection failed thus making any tracing or/and
665 * streaming not usable.
667 consumer
->enabled
= 0;
673 * Send both relayd sockets to a specific consumer and domain. This is a
674 * helper function to facilitate sending the information to the consumer for a
677 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
678 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
679 char *session_name
, char *hostname
, int session_live_timer
)
686 /* Sending control relayd socket. */
687 if (!sock
->control_sock_sent
) {
688 ret
= send_consumer_relayd_socket(domain
, session_id
,
689 &consumer
->dst
.net
.control
, consumer
, sock
,
690 session_name
, hostname
, session_live_timer
);
691 if (ret
!= LTTNG_OK
) {
696 /* Sending data relayd socket. */
697 if (!sock
->data_sock_sent
) {
698 ret
= send_consumer_relayd_socket(domain
, session_id
,
699 &consumer
->dst
.net
.data
, consumer
, sock
,
700 session_name
, hostname
, session_live_timer
);
701 if (ret
!= LTTNG_OK
) {
711 * Setup relayd connections for a tracing session. First creates the socket to
712 * the relayd and send them to the right domain consumer. Consumer type MUST be
715 int cmd_setup_relayd(struct ltt_session
*session
)
718 struct ltt_ust_session
*usess
;
719 struct ltt_kernel_session
*ksess
;
720 struct consumer_socket
*socket
;
721 struct lttng_ht_iter iter
;
725 usess
= session
->ust_session
;
726 ksess
= session
->kernel_session
;
728 DBG("Setting relayd for session %s", session
->name
);
732 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
733 && usess
->consumer
->enabled
) {
734 /* For each consumer socket, send relayd sockets */
735 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
737 pthread_mutex_lock(socket
->lock
);
738 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
739 usess
->consumer
, socket
,
740 session
->name
, session
->hostname
,
741 session
->live_timer
);
742 pthread_mutex_unlock(socket
->lock
);
743 if (ret
!= LTTNG_OK
) {
746 /* Session is now ready for network streaming. */
747 session
->net_handle
= 1;
751 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
752 && ksess
->consumer
->enabled
) {
753 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
755 pthread_mutex_lock(socket
->lock
);
756 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
757 ksess
->consumer
, socket
,
758 session
->name
, session
->hostname
,
759 session
->live_timer
);
760 pthread_mutex_unlock(socket
->lock
);
761 if (ret
!= LTTNG_OK
) {
764 /* Session is now ready for network streaming. */
765 session
->net_handle
= 1;
775 * Start a kernel session by opening all necessary streams.
777 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
780 struct ltt_kernel_channel
*kchan
;
782 /* Open kernel metadata */
783 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
784 ret
= kernel_open_metadata(ksess
);
786 ret
= LTTNG_ERR_KERN_META_FAIL
;
791 /* Open kernel metadata stream */
792 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
793 ret
= kernel_open_metadata_stream(ksess
);
795 ERR("Kernel create metadata stream failed");
796 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
801 /* For each channel */
802 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
803 if (kchan
->stream_count
== 0) {
804 ret
= kernel_open_channel_stream(kchan
);
806 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
809 /* Update the stream global counter */
810 ksess
->stream_count_global
+= ret
;
814 /* Setup kernel consumer socket and send fds to it */
815 ret
= init_kernel_tracing(ksess
);
817 ret
= LTTNG_ERR_KERN_START_FAIL
;
821 /* This start the kernel tracing */
822 ret
= kernel_start_session(ksess
);
824 ret
= LTTNG_ERR_KERN_START_FAIL
;
828 /* Quiescent wait after starting trace */
829 kernel_wait_quiescent(kernel_tracer_fd
);
840 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
842 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
846 struct ltt_ust_session
*usess
;
848 usess
= session
->ust_session
;
853 case LTTNG_DOMAIN_KERNEL
:
855 ret
= channel_kernel_disable(session
->kernel_session
,
857 if (ret
!= LTTNG_OK
) {
861 kernel_wait_quiescent(kernel_tracer_fd
);
864 case LTTNG_DOMAIN_UST
:
866 struct ltt_ust_channel
*uchan
;
867 struct lttng_ht
*chan_ht
;
869 chan_ht
= usess
->domain_global
.channels
;
871 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
873 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
877 ret
= channel_ust_disable(usess
, uchan
);
878 if (ret
!= LTTNG_OK
) {
884 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
885 case LTTNG_DOMAIN_UST_EXEC_NAME
:
886 case LTTNG_DOMAIN_UST_PID
:
889 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
901 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
903 * The wpipe arguments is used as a notifier for the kernel thread.
905 int cmd_enable_channel(struct ltt_session
*session
,
906 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
909 struct ltt_ust_session
*usess
= session
->ust_session
;
910 struct lttng_ht
*chan_ht
;
917 len
= strnlen(attr
->name
, sizeof(attr
->name
));
919 /* Validate channel name */
920 if (attr
->name
[0] == '.' ||
921 memchr(attr
->name
, '/', len
) != NULL
) {
922 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
926 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
931 * Don't try to enable a channel if the session has been started at
932 * some point in time before. The tracer does not allow it.
934 if (session
->has_been_started
) {
935 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
940 * If the session is a live session, remove the switch timer, the
941 * live timer does the same thing but sends also synchronisation
942 * beacons for inactive streams.
944 if (session
->live_timer
> 0) {
945 attr
->attr
.live_timer_interval
= session
->live_timer
;
946 attr
->attr
.switch_timer_interval
= 0;
950 * The ringbuffer (both in user space and kernel) behave badly in overwrite
951 * mode and with less than 2 subbuf so block it right away and send back an
952 * invalid attribute error.
954 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
955 ret
= LTTNG_ERR_INVALID
;
959 switch (domain
->type
) {
960 case LTTNG_DOMAIN_KERNEL
:
962 struct ltt_kernel_channel
*kchan
;
964 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
965 session
->kernel_session
);
967 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
968 if (attr
->name
[0] != '\0') {
969 session
->kernel_session
->has_non_default_channel
= 1;
972 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
975 if (ret
!= LTTNG_OK
) {
979 kernel_wait_quiescent(kernel_tracer_fd
);
982 case LTTNG_DOMAIN_UST
:
984 struct ltt_ust_channel
*uchan
;
986 chan_ht
= usess
->domain_global
.channels
;
988 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
990 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
991 if (attr
->name
[0] != '\0') {
992 usess
->has_non_default_channel
= 1;
995 ret
= channel_ust_enable(usess
, uchan
);
1000 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1012 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1014 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1015 char *channel_name
, char *event_name
)
1022 case LTTNG_DOMAIN_KERNEL
:
1024 struct ltt_kernel_channel
*kchan
;
1025 struct ltt_kernel_session
*ksess
;
1027 ksess
= session
->kernel_session
;
1030 * If a non-default channel has been created in the
1031 * session, explicitely require that -c chan_name needs
1034 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1035 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1039 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1040 if (kchan
== NULL
) {
1041 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1045 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1046 if (ret
!= LTTNG_OK
) {
1050 kernel_wait_quiescent(kernel_tracer_fd
);
1053 case LTTNG_DOMAIN_UST
:
1055 struct ltt_ust_channel
*uchan
;
1056 struct ltt_ust_session
*usess
;
1058 usess
= session
->ust_session
;
1061 * If a non-default channel has been created in the
1062 * session, explicitely require that -c chan_name needs
1065 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1066 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1070 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1072 if (uchan
== NULL
) {
1073 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1077 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1078 if (ret
!= LTTNG_OK
) {
1082 DBG3("Disable UST event %s in channel %s completed", event_name
,
1086 case LTTNG_DOMAIN_JUL
:
1088 struct ltt_ust_session
*usess
= session
->ust_session
;
1092 ret
= event_jul_disable(usess
, event_name
);
1093 if (ret
!= LTTNG_OK
) {
1100 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1101 case LTTNG_DOMAIN_UST_PID
:
1102 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1105 ret
= LTTNG_ERR_UND
;
1117 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
1119 int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
1127 case LTTNG_DOMAIN_KERNEL
:
1129 struct ltt_kernel_session
*ksess
;
1130 struct ltt_kernel_channel
*kchan
;
1132 ksess
= session
->kernel_session
;
1135 * If a non-default channel has been created in the
1136 * session, explicitely require that -c chan_name needs
1139 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1140 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1144 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1145 if (kchan
== NULL
) {
1146 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1150 ret
= event_kernel_disable_all(kchan
);
1151 if (ret
!= LTTNG_OK
) {
1155 kernel_wait_quiescent(kernel_tracer_fd
);
1158 case LTTNG_DOMAIN_UST
:
1160 struct ltt_ust_session
*usess
;
1161 struct ltt_ust_channel
*uchan
;
1163 usess
= session
->ust_session
;
1166 * If a non-default channel has been created in the
1167 * session, explicitely require that -c chan_name needs
1170 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1171 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1175 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1177 if (uchan
== NULL
) {
1178 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1182 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1187 DBG3("Disable all UST events in channel %s completed", channel_name
);
1191 case LTTNG_DOMAIN_JUL
:
1193 struct ltt_ust_session
*usess
= session
->ust_session
;
1197 ret
= event_jul_disable_all(usess
);
1198 if (ret
!= LTTNG_OK
) {
1205 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1206 case LTTNG_DOMAIN_UST_PID
:
1207 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1210 ret
= LTTNG_ERR_UND
;
1222 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1224 int cmd_add_context(struct ltt_session
*session
, int domain
,
1225 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1227 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1230 case LTTNG_DOMAIN_KERNEL
:
1231 assert(session
->kernel_session
);
1233 if (session
->kernel_session
->channel_count
== 0) {
1234 /* Create default channel */
1235 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1236 if (ret
!= LTTNG_OK
) {
1239 chan_kern_created
= 1;
1241 /* Add kernel context to kernel tracer */
1242 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1243 if (ret
!= LTTNG_OK
) {
1247 case LTTNG_DOMAIN_UST
:
1249 struct ltt_ust_session
*usess
= session
->ust_session
;
1250 unsigned int chan_count
;
1254 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1255 if (chan_count
== 0) {
1256 struct lttng_channel
*attr
;
1257 /* Create default channel */
1258 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1260 ret
= LTTNG_ERR_FATAL
;
1264 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1265 if (ret
!= LTTNG_OK
) {
1270 chan_ust_created
= 1;
1273 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1274 if (ret
!= LTTNG_OK
) {
1280 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1281 case LTTNG_DOMAIN_UST_PID
:
1282 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1285 ret
= LTTNG_ERR_UND
;
1292 if (chan_kern_created
) {
1293 struct ltt_kernel_channel
*kchan
=
1294 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1295 session
->kernel_session
);
1296 /* Created previously, this should NOT fail. */
1298 kernel_destroy_channel(kchan
);
1301 if (chan_ust_created
) {
1302 struct ltt_ust_channel
*uchan
=
1303 trace_ust_find_channel_by_name(
1304 session
->ust_session
->domain_global
.channels
,
1305 DEFAULT_CHANNEL_NAME
);
1306 /* Created previously, this should NOT fail. */
1308 /* Remove from the channel list of the session. */
1309 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1311 trace_ust_destroy_channel(uchan
);
1317 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1318 * We own filter, exclusion, and filter_expression.
1320 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1321 char *channel_name
, struct lttng_event
*event
,
1322 char *filter_expression
,
1323 struct lttng_filter_bytecode
*filter
,
1324 struct lttng_event_exclusion
*exclusion
,
1327 int ret
, channel_created
= 0;
1328 struct lttng_channel
*attr
;
1332 assert(channel_name
);
1336 switch (domain
->type
) {
1337 case LTTNG_DOMAIN_KERNEL
:
1339 struct ltt_kernel_channel
*kchan
;
1342 * If a non-default channel has been created in the
1343 * session, explicitely require that -c chan_name needs
1346 if (session
->kernel_session
->has_non_default_channel
1347 && channel_name
[0] == '\0') {
1348 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1352 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1353 session
->kernel_session
);
1354 if (kchan
== NULL
) {
1355 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1356 LTTNG_BUFFER_GLOBAL
);
1358 ret
= LTTNG_ERR_FATAL
;
1361 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1363 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1364 if (ret
!= LTTNG_OK
) {
1370 channel_created
= 1;
1373 /* Get the newly created kernel channel pointer */
1374 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1375 session
->kernel_session
);
1376 if (kchan
== NULL
) {
1377 /* This sould not happen... */
1378 ret
= LTTNG_ERR_FATAL
;
1382 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1383 if (ret
!= LTTNG_OK
) {
1384 if (channel_created
) {
1385 /* Let's not leak a useless channel. */
1386 kernel_destroy_channel(kchan
);
1391 kernel_wait_quiescent(kernel_tracer_fd
);
1394 case LTTNG_DOMAIN_UST
:
1396 struct ltt_ust_channel
*uchan
;
1397 struct ltt_ust_session
*usess
= session
->ust_session
;
1402 * If a non-default channel has been created in the
1403 * session, explicitely require that -c chan_name needs
1406 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1407 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1411 /* Get channel from global UST domain */
1412 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1414 if (uchan
== NULL
) {
1415 /* Create default channel */
1416 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1417 usess
->buffer_type
);
1419 ret
= LTTNG_ERR_FATAL
;
1422 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1424 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1425 if (ret
!= LTTNG_OK
) {
1431 /* Get the newly created channel reference back */
1432 uchan
= trace_ust_find_channel_by_name(
1433 usess
->domain_global
.channels
, channel_name
);
1437 /* At this point, the session and channel exist on the tracer */
1438 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1439 filter_expression
, filter
, exclusion
);
1440 /* We have passed ownership */
1441 filter_expression
= NULL
;
1444 if (ret
!= LTTNG_OK
) {
1449 case LTTNG_DOMAIN_JUL
:
1451 struct lttng_event uevent
;
1452 struct lttng_domain tmp_dom
;
1453 struct ltt_ust_session
*usess
= session
->ust_session
;
1457 /* Create the default JUL tracepoint. */
1458 memset(&uevent
, 0, sizeof(uevent
));
1459 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1460 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1462 strncpy(uevent
.name
, DEFAULT_SYS_JUL_EVENT_NAME
,
1463 sizeof(uevent
.name
));
1465 strncpy(uevent
.name
, DEFAULT_USER_JUL_EVENT_NAME
,
1466 sizeof(uevent
.name
));
1468 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1471 * The domain type is changed because we are about to enable the
1472 * default channel and event for the JUL domain that are hardcoded.
1473 * This happens in the UST domain.
1475 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1476 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1478 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1479 &uevent
, filter_expression
, filter
, NULL
, wpipe
);
1480 /* We have passed ownership */
1481 filter_expression
= NULL
;
1482 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1486 /* The wild card * means that everything should be enabled. */
1487 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1488 ret
= event_jul_enable_all(usess
, event
, filter
);
1491 ret
= event_jul_enable(usess
, event
, filter
);
1494 if (ret
!= LTTNG_OK
) {
1501 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1502 case LTTNG_DOMAIN_UST_PID
:
1503 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1506 ret
= LTTNG_ERR_UND
;
1513 free(filter_expression
);
1521 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1523 int cmd_enable_event_all(struct ltt_session
*session
,
1524 struct lttng_domain
*domain
, char *channel_name
, int event_type
,
1525 char *filter_expression
,
1526 struct lttng_filter_bytecode
*filter
, int wpipe
)
1529 struct lttng_channel
*attr
;
1532 assert(channel_name
);
1536 switch (domain
->type
) {
1537 case LTTNG_DOMAIN_KERNEL
:
1539 struct ltt_kernel_channel
*kchan
;
1541 assert(session
->kernel_session
);
1544 * If a non-default channel has been created in the
1545 * session, explicitely require that -c chan_name needs
1548 if (session
->kernel_session
->has_non_default_channel
1549 && channel_name
[0] == '\0') {
1550 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1554 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1555 session
->kernel_session
);
1556 if (kchan
== NULL
) {
1557 /* Create default channel */
1558 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1559 LTTNG_BUFFER_GLOBAL
);
1561 ret
= LTTNG_ERR_FATAL
;
1564 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1566 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1567 if (ret
!= LTTNG_OK
) {
1573 /* Get the newly created kernel channel pointer */
1574 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1575 session
->kernel_session
);
1579 switch (event_type
) {
1580 case LTTNG_EVENT_SYSCALL
:
1581 ret
= event_kernel_enable_all_syscalls(kchan
, kernel_tracer_fd
);
1583 case LTTNG_EVENT_TRACEPOINT
:
1585 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1586 * events already registered to the channel.
1588 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1590 case LTTNG_EVENT_ALL
:
1591 /* Enable syscalls and tracepoints */
1592 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1595 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1599 /* Manage return value */
1600 if (ret
!= LTTNG_OK
) {
1602 * On error, cmd_enable_channel call will take care of destroying
1603 * the created channel if it was needed.
1608 kernel_wait_quiescent(kernel_tracer_fd
);
1611 case LTTNG_DOMAIN_UST
:
1613 struct ltt_ust_channel
*uchan
;
1614 struct ltt_ust_session
*usess
= session
->ust_session
;
1619 * If a non-default channel has been created in the
1620 * session, explicitely require that -c chan_name needs
1623 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1624 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1628 /* Get channel from global UST domain */
1629 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1631 if (uchan
== NULL
) {
1632 /* Create default channel */
1633 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1634 usess
->buffer_type
);
1636 ret
= LTTNG_ERR_FATAL
;
1639 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1641 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1642 if (ret
!= LTTNG_OK
) {
1648 /* Get the newly created channel reference back */
1649 uchan
= trace_ust_find_channel_by_name(
1650 usess
->domain_global
.channels
, channel_name
);
1654 /* At this point, the session and channel exist on the tracer */
1656 switch (event_type
) {
1657 case LTTNG_EVENT_ALL
:
1658 case LTTNG_EVENT_TRACEPOINT
:
1659 ret
= event_ust_enable_all_tracepoints(usess
, uchan
,
1660 filter_expression
, filter
);
1661 if (ret
!= LTTNG_OK
) {
1666 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
1670 /* Manage return value */
1671 if (ret
!= LTTNG_OK
) {
1677 case LTTNG_DOMAIN_JUL
:
1679 struct lttng_event uevent
, event
;
1680 struct lttng_domain tmp_dom
;
1681 struct ltt_ust_session
*usess
= session
->ust_session
;
1685 /* Create the default JUL tracepoint. */
1686 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1687 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1689 strncpy(uevent
.name
, DEFAULT_SYS_JUL_EVENT_NAME
,
1690 sizeof(uevent
.name
));
1692 strncpy(uevent
.name
, DEFAULT_USER_JUL_EVENT_NAME
,
1693 sizeof(uevent
.name
));
1695 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1698 * The domain type is changed because we are about to enable the
1699 * default channel and event for the JUL domain that are hardcoded.
1700 * This happens in the UST domain.
1702 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1703 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1705 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1706 &uevent
, NULL
, NULL
, NULL
, wpipe
);
1707 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1711 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1712 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1713 strncpy(event
.name
, "*", sizeof(event
.name
));
1714 event
.name
[sizeof(event
.name
) - 1] = '\0';
1716 ret
= event_jul_enable_all(usess
, &event
, filter
);
1717 if (ret
!= LTTNG_OK
) {
1724 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1725 case LTTNG_DOMAIN_UST_PID
:
1726 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1729 ret
= LTTNG_ERR_UND
;
1742 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1744 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1747 ssize_t nb_events
= 0;
1750 case LTTNG_DOMAIN_KERNEL
:
1751 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1752 if (nb_events
< 0) {
1753 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1757 case LTTNG_DOMAIN_UST
:
1758 nb_events
= ust_app_list_events(events
);
1759 if (nb_events
< 0) {
1760 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1764 case LTTNG_DOMAIN_JUL
:
1765 nb_events
= jul_list_events(events
);
1766 if (nb_events
< 0) {
1767 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1772 ret
= LTTNG_ERR_UND
;
1779 /* Return negative value to differentiate return code */
1784 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1786 ssize_t
cmd_list_tracepoint_fields(int domain
,
1787 struct lttng_event_field
**fields
)
1790 ssize_t nb_fields
= 0;
1793 case LTTNG_DOMAIN_UST
:
1794 nb_fields
= ust_app_list_event_fields(fields
);
1795 if (nb_fields
< 0) {
1796 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1800 case LTTNG_DOMAIN_KERNEL
:
1801 default: /* fall-through */
1802 ret
= LTTNG_ERR_UND
;
1809 /* Return negative value to differentiate return code */
1814 * Command LTTNG_START_TRACE processed by the client thread.
1816 int cmd_start_trace(struct ltt_session
*session
)
1819 unsigned long nb_chan
= 0;
1820 struct ltt_kernel_session
*ksession
;
1821 struct ltt_ust_session
*usess
;
1825 /* Ease our life a bit ;) */
1826 ksession
= session
->kernel_session
;
1827 usess
= session
->ust_session
;
1829 /* Is the session already started? */
1830 if (session
->active
) {
1831 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1836 * Starting a session without channel is useless since after that it's not
1837 * possible to enable channel thus inform the client.
1839 if (usess
&& usess
->domain_global
.channels
) {
1841 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1845 nb_chan
+= ksession
->channel_count
;
1848 ret
= LTTNG_ERR_NO_CHANNEL
;
1852 /* Kernel tracing */
1853 if (ksession
!= NULL
) {
1854 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1855 if (ret
!= LTTNG_OK
) {
1860 /* Flag session that trace should start automatically */
1863 * Even though the start trace might fail, flag this session active so
1864 * other application coming in are started by default.
1868 ret
= ust_app_start_trace_all(usess
);
1870 ret
= LTTNG_ERR_UST_START_FAIL
;
1875 /* Flag this after a successful start. */
1876 session
->has_been_started
= 1;
1877 session
->active
= 1;
1886 * Command LTTNG_STOP_TRACE processed by the client thread.
1888 int cmd_stop_trace(struct ltt_session
*session
)
1891 struct ltt_kernel_channel
*kchan
;
1892 struct ltt_kernel_session
*ksession
;
1893 struct ltt_ust_session
*usess
;
1898 ksession
= session
->kernel_session
;
1899 usess
= session
->ust_session
;
1901 /* Session is not active. Skip everythong and inform the client. */
1902 if (!session
->active
) {
1903 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1908 if (ksession
&& ksession
->active
) {
1909 DBG("Stop kernel tracing");
1911 /* Flush metadata if exist */
1912 if (ksession
->metadata_stream_fd
>= 0) {
1913 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1915 ERR("Kernel metadata flush failed");
1919 /* Flush all buffers before stopping */
1920 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1921 ret
= kernel_flush_buffer(kchan
);
1923 ERR("Kernel flush buffer error");
1927 ret
= kernel_stop_session(ksession
);
1929 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1933 kernel_wait_quiescent(kernel_tracer_fd
);
1935 ksession
->active
= 0;
1938 if (usess
&& usess
->active
) {
1940 * Even though the stop trace might fail, flag this session inactive so
1941 * other application coming in are not started by default.
1945 ret
= ust_app_stop_trace_all(usess
);
1947 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1952 /* Flag inactive after a successful stop. */
1953 session
->active
= 0;
1961 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1963 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
1964 struct lttng_uri
*uris
)
1967 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1968 struct ltt_ust_session
*usess
= session
->ust_session
;
1974 /* Can't set consumer URI if the session is active. */
1975 if (session
->active
) {
1976 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1980 /* Set the "global" consumer URIs */
1981 for (i
= 0; i
< nb_uri
; i
++) {
1982 ret
= add_uri_to_consumer(session
->consumer
,
1983 &uris
[i
], 0, session
->name
);
1984 if (ret
!= LTTNG_OK
) {
1989 /* Set UST session URIs */
1990 if (session
->ust_session
) {
1991 for (i
= 0; i
< nb_uri
; i
++) {
1992 ret
= add_uri_to_consumer(
1993 session
->ust_session
->consumer
,
1994 &uris
[i
], LTTNG_DOMAIN_UST
,
1996 if (ret
!= LTTNG_OK
) {
2002 /* Set kernel session URIs */
2003 if (session
->kernel_session
) {
2004 for (i
= 0; i
< nb_uri
; i
++) {
2005 ret
= add_uri_to_consumer(
2006 session
->kernel_session
->consumer
,
2007 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2009 if (ret
!= LTTNG_OK
) {
2016 * Make sure to set the session in output mode after we set URI since a
2017 * session can be created without URL (thus flagged in no output mode).
2019 session
->output_traces
= 1;
2021 ksess
->output_traces
= 1;
2025 usess
->output_traces
= 1;
2036 * Command LTTNG_CREATE_SESSION processed by the client thread.
2038 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2039 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2042 struct ltt_session
*session
;
2048 * Verify if the session already exist
2050 * XXX: There is no need for the session lock list here since the caller
2051 * (process_client_msg) is holding it. We might want to change that so a
2052 * single command does not lock the entire session list.
2054 session
= session_find_by_name(name
);
2055 if (session
!= NULL
) {
2056 ret
= LTTNG_ERR_EXIST_SESS
;
2060 /* Create tracing session in the registry */
2061 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2062 LTTNG_SOCK_GET_GID_CRED(creds
));
2063 if (ret
!= LTTNG_OK
) {
2068 * Get the newly created session pointer back
2070 * XXX: There is no need for the session lock list here since the caller
2071 * (process_client_msg) is holding it. We might want to change that so a
2072 * single command does not lock the entire session list.
2074 session
= session_find_by_name(name
);
2077 session
->live_timer
= live_timer
;
2078 /* Create default consumer output for the session not yet created. */
2079 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2080 if (session
->consumer
== NULL
) {
2081 ret
= LTTNG_ERR_FATAL
;
2082 goto consumer_error
;
2086 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2087 if (ret
!= LTTNG_OK
) {
2088 goto consumer_error
;
2090 session
->output_traces
= 1;
2092 session
->output_traces
= 0;
2093 DBG2("Session %s created with no output", session
->name
);
2096 session
->consumer
->enabled
= 1;
2101 session_destroy(session
);
2108 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2110 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2111 size_t nb_uri
, lttng_sock_cred
*creds
)
2114 struct ltt_session
*session
;
2115 struct snapshot_output
*new_output
= NULL
;
2121 * Create session in no output mode with URIs set to NULL. The uris we've
2122 * received are for a default snapshot output if one.
2124 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2125 if (ret
!= LTTNG_OK
) {
2129 /* Get the newly created session pointer back. This should NEVER fail. */
2130 session
= session_find_by_name(name
);
2133 /* Flag session for snapshot mode. */
2134 session
->snapshot_mode
= 1;
2136 /* Skip snapshot output creation if no URI is given. */
2141 new_output
= snapshot_output_alloc();
2143 ret
= LTTNG_ERR_NOMEM
;
2144 goto error_snapshot_alloc
;
2147 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2148 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2150 if (ret
== -ENOMEM
) {
2151 ret
= LTTNG_ERR_NOMEM
;
2153 ret
= LTTNG_ERR_INVALID
;
2155 goto error_snapshot
;
2159 snapshot_add_output(&session
->snapshot
, new_output
);
2166 snapshot_output_destroy(new_output
);
2167 error_snapshot_alloc
:
2168 session_destroy(session
);
2174 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2176 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2179 struct ltt_ust_session
*usess
;
2180 struct ltt_kernel_session
*ksess
;
2185 usess
= session
->ust_session
;
2186 ksess
= session
->kernel_session
;
2188 /* Clean kernel session teardown */
2189 kernel_destroy_session(ksess
);
2191 /* UST session teardown */
2193 /* Close any relayd session */
2194 consumer_output_send_destroy_relayd(usess
->consumer
);
2196 /* Destroy every UST application related to this session. */
2197 ret
= ust_app_destroy_trace_all(usess
);
2199 ERR("Error in ust_app_destroy_trace_all");
2202 /* Clean up the rest. */
2203 trace_ust_destroy_session(usess
);
2207 * Must notify the kernel thread here to update it's poll set in order to
2208 * remove the channel(s)' fd just destroyed.
2210 ret
= notify_thread_pipe(wpipe
);
2212 PERROR("write kernel poll pipe");
2215 ret
= session_destroy(session
);
2221 * Command LTTNG_CALIBRATE processed by the client thread.
2223 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2228 case LTTNG_DOMAIN_KERNEL
:
2230 struct lttng_kernel_calibrate kcalibrate
;
2232 switch (calibrate
->type
) {
2233 case LTTNG_CALIBRATE_FUNCTION
:
2235 /* Default and only possible calibrate option. */
2236 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2240 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2242 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2247 case LTTNG_DOMAIN_UST
:
2249 struct lttng_ust_calibrate ucalibrate
;
2251 switch (calibrate
->type
) {
2252 case LTTNG_CALIBRATE_FUNCTION
:
2254 /* Default and only possible calibrate option. */
2255 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2259 ret
= ust_app_calibrate_glb(&ucalibrate
);
2261 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2267 ret
= LTTNG_ERR_UND
;
2278 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2280 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2281 const char *sock_path
, struct consumer_data
*cdata
)
2284 struct consumer_socket
*socket
= NULL
;
2291 case LTTNG_DOMAIN_KERNEL
:
2293 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2297 /* Can't register a consumer if there is already one */
2298 if (ksess
->consumer_fds_sent
!= 0) {
2299 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2303 sock
= lttcomm_connect_unix_sock(sock_path
);
2305 ret
= LTTNG_ERR_CONNECT_FAIL
;
2308 cdata
->cmd_sock
= sock
;
2310 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2311 if (socket
== NULL
) {
2314 PERROR("close register consumer");
2316 cdata
->cmd_sock
= -1;
2317 ret
= LTTNG_ERR_FATAL
;
2321 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2322 if (socket
->lock
== NULL
) {
2323 PERROR("zmalloc pthread mutex");
2324 ret
= LTTNG_ERR_FATAL
;
2327 pthread_mutex_init(socket
->lock
, NULL
);
2328 socket
->registered
= 1;
2331 consumer_add_socket(socket
, ksess
->consumer
);
2334 pthread_mutex_lock(&cdata
->pid_mutex
);
2336 pthread_mutex_unlock(&cdata
->pid_mutex
);
2341 /* TODO: Userspace tracing */
2342 ret
= LTTNG_ERR_UND
;
2350 consumer_destroy_socket(socket
);
2356 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2358 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2359 struct lttng_domain
**domains
)
2364 if (session
->kernel_session
!= NULL
) {
2365 DBG3("Listing domains found kernel domain");
2369 if (session
->ust_session
!= NULL
) {
2370 DBG3("Listing domains found UST global domain");
2373 if (session
->ust_session
->domain_jul
.being_used
) {
2378 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2379 if (*domains
== NULL
) {
2380 ret
= LTTNG_ERR_FATAL
;
2384 if (session
->kernel_session
!= NULL
) {
2385 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2389 if (session
->ust_session
!= NULL
) {
2390 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2391 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2394 if (session
->ust_session
->domain_jul
.being_used
) {
2395 (*domains
)[index
].type
= LTTNG_DOMAIN_JUL
;
2396 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2404 /* Return negative value to differentiate return code */
2410 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2412 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2413 struct lttng_channel
**channels
)
2416 ssize_t nb_chan
= 0;
2419 case LTTNG_DOMAIN_KERNEL
:
2420 if (session
->kernel_session
!= NULL
) {
2421 nb_chan
= session
->kernel_session
->channel_count
;
2423 DBG3("Number of kernel channels %zd", nb_chan
);
2425 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2428 case LTTNG_DOMAIN_UST
:
2429 if (session
->ust_session
!= NULL
) {
2431 nb_chan
= lttng_ht_get_count(
2432 session
->ust_session
->domain_global
.channels
);
2435 DBG3("Number of UST global channels %zd", nb_chan
);
2437 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2442 ret
= LTTNG_ERR_UND
;
2447 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2448 if (*channels
== NULL
) {
2449 ret
= LTTNG_ERR_FATAL
;
2453 list_lttng_channels(domain
, session
, *channels
);
2459 /* Return negative value to differentiate return code */
2464 * Command LTTNG_LIST_EVENTS processed by the client thread.
2466 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2467 char *channel_name
, struct lttng_event
**events
)
2470 ssize_t nb_event
= 0;
2473 case LTTNG_DOMAIN_KERNEL
:
2474 if (session
->kernel_session
!= NULL
) {
2475 nb_event
= list_lttng_kernel_events(channel_name
,
2476 session
->kernel_session
, events
);
2479 case LTTNG_DOMAIN_UST
:
2481 if (session
->ust_session
!= NULL
) {
2482 nb_event
= list_lttng_ust_global_events(channel_name
,
2483 &session
->ust_session
->domain_global
, events
);
2487 case LTTNG_DOMAIN_JUL
:
2488 if (session
->ust_session
) {
2489 nb_event
= list_lttng_jul_events(
2490 &session
->ust_session
->domain_jul
, events
);
2494 ret
= LTTNG_ERR_UND
;
2501 /* Return negative value to differentiate return code */
2506 * Using the session list, filled a lttng_session array to send back to the
2507 * client for session listing.
2509 * The session list lock MUST be acquired before calling this function. Use
2510 * session_lock_list() and session_unlock_list().
2512 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2517 struct ltt_session
*session
;
2518 struct ltt_session_list
*list
= session_get_list();
2520 DBG("Getting all available session for UID %d GID %d",
2523 * Iterate over session list and append data after the control struct in
2526 cds_list_for_each_entry(session
, &list
->head
, list
) {
2528 * Only list the sessions the user can control.
2530 if (!session_access_ok(session
, uid
, gid
)) {
2534 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2535 struct ltt_ust_session
*usess
= session
->ust_session
;
2537 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2538 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2539 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2540 ret
= build_network_session_path(sessions
[i
].path
,
2541 sizeof(sessions
[i
].path
), session
);
2543 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2544 session
->consumer
->dst
.trace_path
);
2547 PERROR("snprintf session path");
2551 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2552 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2553 sessions
[i
].enabled
= session
->active
;
2554 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2555 sessions
[i
].live_timer_interval
= session
->live_timer
;
2561 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2562 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2564 int cmd_data_pending(struct ltt_session
*session
)
2567 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2568 struct ltt_ust_session
*usess
= session
->ust_session
;
2572 /* Session MUST be stopped to ask for data availability. */
2573 if (session
->active
) {
2574 ret
= LTTNG_ERR_SESSION_STARTED
;
2578 * If stopped, just make sure we've started before else the above call
2579 * will always send that there is data pending.
2581 * The consumer assumes that when the data pending command is received,
2582 * the trace has been started before or else no output data is written
2583 * by the streams which is a condition for data pending. So, this is
2584 * *VERY* important that we don't ask the consumer before a start
2587 if (!session
->has_been_started
) {
2593 if (ksess
&& ksess
->consumer
) {
2594 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2596 /* Data is still being extracted for the kernel. */
2601 if (usess
&& usess
->consumer
) {
2602 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2604 /* Data is still being extracted for the kernel. */
2609 /* Data is ready to be read by a viewer */
2617 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2619 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2621 int cmd_snapshot_add_output(struct ltt_session
*session
,
2622 struct lttng_snapshot_output
*output
, uint32_t *id
)
2625 struct snapshot_output
*new_output
;
2630 DBG("Cmd snapshot add output for session %s", session
->name
);
2633 * Permission denied to create an output if the session is not
2634 * set in no output mode.
2636 if (session
->output_traces
) {
2637 ret
= LTTNG_ERR_EPERM
;
2641 /* Only one output is allowed until we have the "tee" feature. */
2642 if (session
->snapshot
.nb_output
== 1) {
2643 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2647 new_output
= snapshot_output_alloc();
2649 ret
= LTTNG_ERR_NOMEM
;
2653 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2654 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2655 &session
->snapshot
);
2657 if (ret
== -ENOMEM
) {
2658 ret
= LTTNG_ERR_NOMEM
;
2660 ret
= LTTNG_ERR_INVALID
;
2666 snapshot_add_output(&session
->snapshot
, new_output
);
2668 *id
= new_output
->id
;
2675 snapshot_output_destroy(new_output
);
2681 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2683 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2685 int cmd_snapshot_del_output(struct ltt_session
*session
,
2686 struct lttng_snapshot_output
*output
)
2689 struct snapshot_output
*sout
= NULL
;
2697 * Permission denied to create an output if the session is not
2698 * set in no output mode.
2700 if (session
->output_traces
) {
2701 ret
= LTTNG_ERR_EPERM
;
2706 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2708 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2709 } else if (*output
->name
!= '\0') {
2710 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2712 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2715 ret
= LTTNG_ERR_INVALID
;
2719 snapshot_delete_output(&session
->snapshot
, sout
);
2720 snapshot_output_destroy(sout
);
2729 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2731 * If no output is available, outputs is untouched and 0 is returned.
2733 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2735 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2736 struct lttng_snapshot_output
**outputs
)
2739 struct lttng_snapshot_output
*list
= NULL
;
2740 struct lttng_ht_iter iter
;
2741 struct snapshot_output
*output
;
2746 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2749 * Permission denied to create an output if the session is not
2750 * set in no output mode.
2752 if (session
->output_traces
) {
2753 ret
= -LTTNG_ERR_EPERM
;
2757 if (session
->snapshot
.nb_output
== 0) {
2762 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2764 ret
= -LTTNG_ERR_NOMEM
;
2768 /* Copy list from session to the new list object. */
2770 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2771 output
, node
.node
) {
2772 assert(output
->consumer
);
2773 list
[idx
].id
= output
->id
;
2774 list
[idx
].max_size
= output
->max_size
;
2775 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2776 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2777 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2778 sizeof(list
[idx
].ctrl_url
));
2781 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2782 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2784 ret
= -LTTNG_ERR_NOMEM
;
2789 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2790 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2792 ret
= -LTTNG_ERR_NOMEM
;
2801 ret
= session
->snapshot
.nb_output
;
2809 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2810 * snapshot output is *not* set with a remote destination.
2812 * Return 0 on success or a LTTNG_ERR code.
2814 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2815 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2818 struct lttng_ht_iter iter
;
2819 struct consumer_socket
*socket
;
2822 assert(snap_output
);
2825 DBG2("Set relayd object from snapshot output");
2827 /* Ignore if snapshot consumer output is not network. */
2828 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2833 * For each consumer socket, create and send the relayd object of the
2837 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2838 socket
, node
.node
) {
2839 ret
= send_consumer_relayd_sockets(0, session
->id
,
2840 snap_output
->consumer
, socket
,
2841 session
->name
, session
->hostname
,
2842 session
->live_timer
);
2843 if (ret
!= LTTNG_OK
) {
2855 * Record a kernel snapshot.
2857 * Return LTTNG_OK on success or a LTTNG_ERR code.
2859 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2860 struct snapshot_output
*output
, struct ltt_session
*session
,
2861 int wait
, uint64_t nb_packets_per_stream
)
2869 /* Get the datetime for the snapshot output directory. */
2870 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2871 sizeof(output
->datetime
));
2873 ret
= LTTNG_ERR_INVALID
;
2878 * Copy kernel session sockets so we can communicate with the right
2879 * consumer for the snapshot record command.
2881 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2883 ret
= LTTNG_ERR_NOMEM
;
2887 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2888 if (ret
!= LTTNG_OK
) {
2889 goto error_snapshot
;
2892 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
2893 if (ret
!= LTTNG_OK
) {
2894 goto error_snapshot
;
2901 /* Clean up copied sockets so this output can use some other later on. */
2902 consumer_destroy_output_sockets(output
->consumer
);
2909 * Record a UST snapshot.
2911 * Return 0 on success or a LTTNG_ERR error code.
2913 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2914 struct snapshot_output
*output
, struct ltt_session
*session
,
2915 int wait
, uint64_t nb_packets_per_stream
)
2923 /* Get the datetime for the snapshot output directory. */
2924 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2925 sizeof(output
->datetime
));
2927 ret
= LTTNG_ERR_INVALID
;
2932 * Copy UST session sockets so we can communicate with the right
2933 * consumer for the snapshot record command.
2935 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
2937 ret
= LTTNG_ERR_NOMEM
;
2941 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
2942 if (ret
!= LTTNG_OK
) {
2943 goto error_snapshot
;
2946 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
2950 ret
= LTTNG_ERR_INVALID
;
2953 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
2956 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
2959 goto error_snapshot
;
2965 /* Clean up copied sockets so this output can use some other later on. */
2966 consumer_destroy_output_sockets(output
->consumer
);
2972 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
2973 uint64_t cur_nr_packets
)
2975 uint64_t tot_size
= 0;
2977 if (session
->kernel_session
) {
2978 struct ltt_kernel_channel
*chan
;
2979 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2981 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
2982 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
2984 * Don't take channel into account if we
2985 * already grab all its packets.
2989 tot_size
+= chan
->channel
->attr
.subbuf_size
2990 * chan
->stream_count
;
2994 if (session
->ust_session
) {
2995 struct ltt_ust_session
*usess
= session
->ust_session
;
2997 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3005 * Calculate the number of packets we can grab from each stream that
3006 * fits within the overall snapshot max size.
3008 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3009 * the number of packets per stream.
3011 * TODO: this approach is not perfect: we consider the worse case
3012 * (packet filling the sub-buffers) as an upper bound, but we could do
3013 * better if we do this calculation while we actually grab the packet
3014 * content: we would know how much padding we don't actually store into
3017 * This algorithm is currently bounded by the number of packets per
3020 * Since we call this algorithm before actually grabbing the data, it's
3021 * an approximation: for instance, applications could appear/disappear
3022 * in between this call and actually grabbing data.
3025 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3028 uint64_t cur_nb_packets
= 0;
3031 return 0; /* Infinite */
3034 size_left
= max_size
;
3036 uint64_t one_more_packet_tot_size
;
3038 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3040 if (!one_more_packet_tot_size
) {
3041 /* We are already grabbing all packets. */
3044 size_left
-= one_more_packet_tot_size
;
3045 if (size_left
< 0) {
3050 if (!cur_nb_packets
) {
3051 /* Not enough room to grab one packet of each stream, error. */
3054 return cur_nb_packets
;
3058 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3060 * The wait parameter is ignored so this call always wait for the snapshot to
3061 * complete before returning.
3063 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3065 int cmd_snapshot_record(struct ltt_session
*session
,
3066 struct lttng_snapshot_output
*output
, int wait
)
3069 unsigned int use_tmp_output
= 0;
3070 struct snapshot_output tmp_output
;
3071 unsigned int snapshot_success
= 0;
3075 DBG("Cmd snapshot record for session %s", session
->name
);
3078 * Permission denied to create an output if the session is not
3079 * set in no output mode.
3081 if (session
->output_traces
) {
3082 ret
= LTTNG_ERR_EPERM
;
3086 /* The session needs to be started at least once. */
3087 if (!session
->has_been_started
) {
3088 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3092 /* Use temporary output for the session. */
3093 if (output
&& *output
->ctrl_url
!= '\0') {
3094 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3095 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3098 if (ret
== -ENOMEM
) {
3099 ret
= LTTNG_ERR_NOMEM
;
3101 ret
= LTTNG_ERR_INVALID
;
3105 /* Use the global session count for the temporary snapshot. */
3106 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3110 if (session
->kernel_session
) {
3111 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3113 if (use_tmp_output
) {
3114 int64_t nb_packets_per_stream
;
3116 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3117 tmp_output
.max_size
);
3118 if (nb_packets_per_stream
< 0) {
3119 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3122 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3123 wait
, nb_packets_per_stream
);
3124 if (ret
!= LTTNG_OK
) {
3127 snapshot_success
= 1;
3129 struct snapshot_output
*sout
;
3130 struct lttng_ht_iter iter
;
3133 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3134 &iter
.iter
, sout
, node
.node
) {
3135 int64_t nb_packets_per_stream
;
3138 * Make a local copy of the output and assign the possible
3139 * temporary value given by the caller.
3141 memset(&tmp_output
, 0, sizeof(tmp_output
));
3142 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3144 if (output
->max_size
!= (uint64_t) -1ULL) {
3145 tmp_output
.max_size
= output
->max_size
;
3148 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3149 tmp_output
.max_size
);
3150 if (nb_packets_per_stream
< 0) {
3151 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3155 /* Use temporary name. */
3156 if (*output
->name
!= '\0') {
3157 strncpy(tmp_output
.name
, output
->name
,
3158 sizeof(tmp_output
.name
));
3161 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3163 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3164 session
, wait
, nb_packets_per_stream
);
3165 if (ret
!= LTTNG_OK
) {
3169 snapshot_success
= 1;
3175 if (session
->ust_session
) {
3176 struct ltt_ust_session
*usess
= session
->ust_session
;
3178 if (use_tmp_output
) {
3179 int64_t nb_packets_per_stream
;
3181 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3182 tmp_output
.max_size
);
3183 if (nb_packets_per_stream
< 0) {
3184 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3187 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3188 wait
, nb_packets_per_stream
);
3189 if (ret
!= LTTNG_OK
) {
3192 snapshot_success
= 1;
3194 struct snapshot_output
*sout
;
3195 struct lttng_ht_iter iter
;
3198 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3199 &iter
.iter
, sout
, node
.node
) {
3200 int64_t nb_packets_per_stream
;
3203 * Make a local copy of the output and assign the possible
3204 * temporary value given by the caller.
3206 memset(&tmp_output
, 0, sizeof(tmp_output
));
3207 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3209 if (output
->max_size
!= (uint64_t) -1ULL) {
3210 tmp_output
.max_size
= output
->max_size
;
3213 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3214 tmp_output
.max_size
);
3215 if (nb_packets_per_stream
< 0) {
3216 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3221 /* Use temporary name. */
3222 if (*output
->name
!= '\0') {
3223 strncpy(tmp_output
.name
, output
->name
,
3224 sizeof(tmp_output
.name
));
3227 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3229 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3230 wait
, nb_packets_per_stream
);
3231 if (ret
!= LTTNG_OK
) {
3235 snapshot_success
= 1;
3241 if (snapshot_success
) {
3242 session
->snapshot
.nb_snapshot
++;
3244 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3252 * Init command subsystem.
3257 * Set network sequence index to 1 for streams to match a relayd
3258 * socket on the consumer side.
3260 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3261 relayd_net_seq_idx
= 1;
3262 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3264 DBG("Command subsystem initialized");