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
;
1483 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1487 /* The wild card * means that everything should be enabled. */
1488 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1489 ret
= event_jul_enable_all(usess
, event
, filter
);
1491 ret
= event_jul_enable(usess
, event
, filter
);
1493 if (ret
!= LTTNG_OK
) {
1500 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1501 case LTTNG_DOMAIN_UST_PID
:
1502 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1505 ret
= LTTNG_ERR_UND
;
1512 free(filter_expression
);
1520 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1522 int cmd_enable_event_all(struct ltt_session
*session
,
1523 struct lttng_domain
*domain
, char *channel_name
, int event_type
,
1524 char *filter_expression
,
1525 struct lttng_filter_bytecode
*filter
, int wpipe
)
1528 struct lttng_channel
*attr
;
1531 assert(channel_name
);
1535 switch (domain
->type
) {
1536 case LTTNG_DOMAIN_KERNEL
:
1538 struct ltt_kernel_channel
*kchan
;
1540 assert(session
->kernel_session
);
1543 * If a non-default channel has been created in the
1544 * session, explicitely require that -c chan_name needs
1547 if (session
->kernel_session
->has_non_default_channel
1548 && channel_name
[0] == '\0') {
1549 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1553 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1554 session
->kernel_session
);
1555 if (kchan
== NULL
) {
1556 /* Create default channel */
1557 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1558 LTTNG_BUFFER_GLOBAL
);
1560 ret
= LTTNG_ERR_FATAL
;
1563 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1565 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1566 if (ret
!= LTTNG_OK
) {
1572 /* Get the newly created kernel channel pointer */
1573 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1574 session
->kernel_session
);
1578 switch (event_type
) {
1579 case LTTNG_EVENT_SYSCALL
:
1580 ret
= event_kernel_enable_all_syscalls(kchan
, kernel_tracer_fd
);
1582 case LTTNG_EVENT_TRACEPOINT
:
1584 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1585 * events already registered to the channel.
1587 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1589 case LTTNG_EVENT_ALL
:
1590 /* Enable syscalls and tracepoints */
1591 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1594 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1598 /* Manage return value */
1599 if (ret
!= LTTNG_OK
) {
1601 * On error, cmd_enable_channel call will take care of destroying
1602 * the created channel if it was needed.
1607 kernel_wait_quiescent(kernel_tracer_fd
);
1610 case LTTNG_DOMAIN_UST
:
1612 struct ltt_ust_channel
*uchan
;
1613 struct ltt_ust_session
*usess
= session
->ust_session
;
1618 * If a non-default channel has been created in the
1619 * session, explicitely require that -c chan_name needs
1622 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1623 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1627 /* Get channel from global UST domain */
1628 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1630 if (uchan
== NULL
) {
1631 /* Create default channel */
1632 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1633 usess
->buffer_type
);
1635 ret
= LTTNG_ERR_FATAL
;
1638 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1640 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1641 if (ret
!= LTTNG_OK
) {
1647 /* Get the newly created channel reference back */
1648 uchan
= trace_ust_find_channel_by_name(
1649 usess
->domain_global
.channels
, channel_name
);
1653 /* At this point, the session and channel exist on the tracer */
1655 switch (event_type
) {
1656 case LTTNG_EVENT_ALL
:
1657 case LTTNG_EVENT_TRACEPOINT
:
1658 ret
= event_ust_enable_all_tracepoints(usess
, uchan
,
1659 filter_expression
, filter
);
1660 if (ret
!= LTTNG_OK
) {
1665 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
1669 /* Manage return value */
1670 if (ret
!= LTTNG_OK
) {
1676 case LTTNG_DOMAIN_JUL
:
1678 struct lttng_event uevent
, event
;
1679 struct lttng_domain tmp_dom
;
1680 struct ltt_ust_session
*usess
= session
->ust_session
;
1684 /* Create the default JUL tracepoint. */
1685 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1686 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1688 strncpy(uevent
.name
, DEFAULT_SYS_JUL_EVENT_NAME
,
1689 sizeof(uevent
.name
));
1691 strncpy(uevent
.name
, DEFAULT_USER_JUL_EVENT_NAME
,
1692 sizeof(uevent
.name
));
1694 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1697 * The domain type is changed because we are about to enable the
1698 * default channel and event for the JUL domain that are hardcoded.
1699 * This happens in the UST domain.
1701 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1702 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1704 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1705 &uevent
, NULL
, NULL
, NULL
, wpipe
);
1706 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1710 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1711 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1712 strncpy(event
.name
, "*", sizeof(event
.name
));
1713 event
.name
[sizeof(event
.name
) - 1] = '\0';
1715 ret
= event_jul_enable_all(usess
, &event
, filter
);
1716 if (ret
!= LTTNG_OK
) {
1723 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1724 case LTTNG_DOMAIN_UST_PID
:
1725 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1728 ret
= LTTNG_ERR_UND
;
1741 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1743 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1746 ssize_t nb_events
= 0;
1749 case LTTNG_DOMAIN_KERNEL
:
1750 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1751 if (nb_events
< 0) {
1752 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1756 case LTTNG_DOMAIN_UST
:
1757 nb_events
= ust_app_list_events(events
);
1758 if (nb_events
< 0) {
1759 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1763 case LTTNG_DOMAIN_JUL
:
1764 nb_events
= jul_list_events(events
);
1765 if (nb_events
< 0) {
1766 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1771 ret
= LTTNG_ERR_UND
;
1778 /* Return negative value to differentiate return code */
1783 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1785 ssize_t
cmd_list_tracepoint_fields(int domain
,
1786 struct lttng_event_field
**fields
)
1789 ssize_t nb_fields
= 0;
1792 case LTTNG_DOMAIN_UST
:
1793 nb_fields
= ust_app_list_event_fields(fields
);
1794 if (nb_fields
< 0) {
1795 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1799 case LTTNG_DOMAIN_KERNEL
:
1800 default: /* fall-through */
1801 ret
= LTTNG_ERR_UND
;
1808 /* Return negative value to differentiate return code */
1813 * Command LTTNG_START_TRACE processed by the client thread.
1815 int cmd_start_trace(struct ltt_session
*session
)
1818 unsigned long nb_chan
= 0;
1819 struct ltt_kernel_session
*ksession
;
1820 struct ltt_ust_session
*usess
;
1824 /* Ease our life a bit ;) */
1825 ksession
= session
->kernel_session
;
1826 usess
= session
->ust_session
;
1828 /* Is the session already started? */
1829 if (session
->active
) {
1830 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1835 * Starting a session without channel is useless since after that it's not
1836 * possible to enable channel thus inform the client.
1838 if (usess
&& usess
->domain_global
.channels
) {
1840 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1844 nb_chan
+= ksession
->channel_count
;
1847 ret
= LTTNG_ERR_NO_CHANNEL
;
1851 /* Kernel tracing */
1852 if (ksession
!= NULL
) {
1853 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1854 if (ret
!= LTTNG_OK
) {
1859 /* Flag session that trace should start automatically */
1862 * Even though the start trace might fail, flag this session active so
1863 * other application coming in are started by default.
1867 ret
= ust_app_start_trace_all(usess
);
1869 ret
= LTTNG_ERR_UST_START_FAIL
;
1874 /* Flag this after a successful start. */
1875 session
->has_been_started
= 1;
1876 session
->active
= 1;
1885 * Command LTTNG_STOP_TRACE processed by the client thread.
1887 int cmd_stop_trace(struct ltt_session
*session
)
1890 struct ltt_kernel_channel
*kchan
;
1891 struct ltt_kernel_session
*ksession
;
1892 struct ltt_ust_session
*usess
;
1897 ksession
= session
->kernel_session
;
1898 usess
= session
->ust_session
;
1900 /* Session is not active. Skip everythong and inform the client. */
1901 if (!session
->active
) {
1902 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1907 if (ksession
&& ksession
->active
) {
1908 DBG("Stop kernel tracing");
1910 /* Flush metadata if exist */
1911 if (ksession
->metadata_stream_fd
>= 0) {
1912 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1914 ERR("Kernel metadata flush failed");
1918 /* Flush all buffers before stopping */
1919 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1920 ret
= kernel_flush_buffer(kchan
);
1922 ERR("Kernel flush buffer error");
1926 ret
= kernel_stop_session(ksession
);
1928 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1932 kernel_wait_quiescent(kernel_tracer_fd
);
1934 ksession
->active
= 0;
1937 if (usess
&& usess
->active
) {
1939 * Even though the stop trace might fail, flag this session inactive so
1940 * other application coming in are not started by default.
1944 ret
= ust_app_stop_trace_all(usess
);
1946 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1951 /* Flag inactive after a successful stop. */
1952 session
->active
= 0;
1960 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1962 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
1963 struct lttng_uri
*uris
)
1966 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1967 struct ltt_ust_session
*usess
= session
->ust_session
;
1973 /* Can't set consumer URI if the session is active. */
1974 if (session
->active
) {
1975 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1979 /* Set the "global" consumer URIs */
1980 for (i
= 0; i
< nb_uri
; i
++) {
1981 ret
= add_uri_to_consumer(session
->consumer
,
1982 &uris
[i
], 0, session
->name
);
1983 if (ret
!= LTTNG_OK
) {
1988 /* Set UST session URIs */
1989 if (session
->ust_session
) {
1990 for (i
= 0; i
< nb_uri
; i
++) {
1991 ret
= add_uri_to_consumer(
1992 session
->ust_session
->consumer
,
1993 &uris
[i
], LTTNG_DOMAIN_UST
,
1995 if (ret
!= LTTNG_OK
) {
2001 /* Set kernel session URIs */
2002 if (session
->kernel_session
) {
2003 for (i
= 0; i
< nb_uri
; i
++) {
2004 ret
= add_uri_to_consumer(
2005 session
->kernel_session
->consumer
,
2006 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2008 if (ret
!= LTTNG_OK
) {
2015 * Make sure to set the session in output mode after we set URI since a
2016 * session can be created without URL (thus flagged in no output mode).
2018 session
->output_traces
= 1;
2020 ksess
->output_traces
= 1;
2024 usess
->output_traces
= 1;
2035 * Command LTTNG_CREATE_SESSION processed by the client thread.
2037 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2038 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2041 struct ltt_session
*session
;
2047 * Verify if the session already exist
2049 * XXX: There is no need for the session lock list here since the caller
2050 * (process_client_msg) is holding it. We might want to change that so a
2051 * single command does not lock the entire session list.
2053 session
= session_find_by_name(name
);
2054 if (session
!= NULL
) {
2055 ret
= LTTNG_ERR_EXIST_SESS
;
2059 /* Create tracing session in the registry */
2060 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2061 LTTNG_SOCK_GET_GID_CRED(creds
));
2062 if (ret
!= LTTNG_OK
) {
2067 * Get the newly created session pointer back
2069 * XXX: There is no need for the session lock list here since the caller
2070 * (process_client_msg) is holding it. We might want to change that so a
2071 * single command does not lock the entire session list.
2073 session
= session_find_by_name(name
);
2076 session
->live_timer
= live_timer
;
2077 /* Create default consumer output for the session not yet created. */
2078 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2079 if (session
->consumer
== NULL
) {
2080 ret
= LTTNG_ERR_FATAL
;
2081 goto consumer_error
;
2085 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2086 if (ret
!= LTTNG_OK
) {
2087 goto consumer_error
;
2089 session
->output_traces
= 1;
2091 session
->output_traces
= 0;
2092 DBG2("Session %s created with no output", session
->name
);
2095 session
->consumer
->enabled
= 1;
2100 session_destroy(session
);
2107 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2109 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2110 size_t nb_uri
, lttng_sock_cred
*creds
)
2113 struct ltt_session
*session
;
2114 struct snapshot_output
*new_output
= NULL
;
2120 * Create session in no output mode with URIs set to NULL. The uris we've
2121 * received are for a default snapshot output if one.
2123 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2124 if (ret
!= LTTNG_OK
) {
2128 /* Get the newly created session pointer back. This should NEVER fail. */
2129 session
= session_find_by_name(name
);
2132 /* Flag session for snapshot mode. */
2133 session
->snapshot_mode
= 1;
2135 /* Skip snapshot output creation if no URI is given. */
2140 new_output
= snapshot_output_alloc();
2142 ret
= LTTNG_ERR_NOMEM
;
2143 goto error_snapshot_alloc
;
2146 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2147 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2149 if (ret
== -ENOMEM
) {
2150 ret
= LTTNG_ERR_NOMEM
;
2152 ret
= LTTNG_ERR_INVALID
;
2154 goto error_snapshot
;
2158 snapshot_add_output(&session
->snapshot
, new_output
);
2165 snapshot_output_destroy(new_output
);
2166 error_snapshot_alloc
:
2167 session_destroy(session
);
2173 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2175 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2178 struct ltt_ust_session
*usess
;
2179 struct ltt_kernel_session
*ksess
;
2184 usess
= session
->ust_session
;
2185 ksess
= session
->kernel_session
;
2187 /* Clean kernel session teardown */
2188 kernel_destroy_session(ksess
);
2190 /* UST session teardown */
2192 /* Close any relayd session */
2193 consumer_output_send_destroy_relayd(usess
->consumer
);
2195 /* Destroy every UST application related to this session. */
2196 ret
= ust_app_destroy_trace_all(usess
);
2198 ERR("Error in ust_app_destroy_trace_all");
2201 /* Clean up the rest. */
2202 trace_ust_destroy_session(usess
);
2206 * Must notify the kernel thread here to update it's poll set in order to
2207 * remove the channel(s)' fd just destroyed.
2209 ret
= notify_thread_pipe(wpipe
);
2211 PERROR("write kernel poll pipe");
2214 ret
= session_destroy(session
);
2220 * Command LTTNG_CALIBRATE processed by the client thread.
2222 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2227 case LTTNG_DOMAIN_KERNEL
:
2229 struct lttng_kernel_calibrate kcalibrate
;
2231 switch (calibrate
->type
) {
2232 case LTTNG_CALIBRATE_FUNCTION
:
2234 /* Default and only possible calibrate option. */
2235 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2239 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2241 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2246 case LTTNG_DOMAIN_UST
:
2248 struct lttng_ust_calibrate ucalibrate
;
2250 switch (calibrate
->type
) {
2251 case LTTNG_CALIBRATE_FUNCTION
:
2253 /* Default and only possible calibrate option. */
2254 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2258 ret
= ust_app_calibrate_glb(&ucalibrate
);
2260 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2266 ret
= LTTNG_ERR_UND
;
2277 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2279 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2280 const char *sock_path
, struct consumer_data
*cdata
)
2283 struct consumer_socket
*socket
= NULL
;
2290 case LTTNG_DOMAIN_KERNEL
:
2292 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2296 /* Can't register a consumer if there is already one */
2297 if (ksess
->consumer_fds_sent
!= 0) {
2298 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2302 sock
= lttcomm_connect_unix_sock(sock_path
);
2304 ret
= LTTNG_ERR_CONNECT_FAIL
;
2307 cdata
->cmd_sock
= sock
;
2309 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2310 if (socket
== NULL
) {
2313 PERROR("close register consumer");
2315 cdata
->cmd_sock
= -1;
2316 ret
= LTTNG_ERR_FATAL
;
2320 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2321 if (socket
->lock
== NULL
) {
2322 PERROR("zmalloc pthread mutex");
2323 ret
= LTTNG_ERR_FATAL
;
2326 pthread_mutex_init(socket
->lock
, NULL
);
2327 socket
->registered
= 1;
2330 consumer_add_socket(socket
, ksess
->consumer
);
2333 pthread_mutex_lock(&cdata
->pid_mutex
);
2335 pthread_mutex_unlock(&cdata
->pid_mutex
);
2340 /* TODO: Userspace tracing */
2341 ret
= LTTNG_ERR_UND
;
2349 consumer_destroy_socket(socket
);
2355 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2357 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2358 struct lttng_domain
**domains
)
2363 if (session
->kernel_session
!= NULL
) {
2364 DBG3("Listing domains found kernel domain");
2368 if (session
->ust_session
!= NULL
) {
2369 DBG3("Listing domains found UST global domain");
2372 if (session
->ust_session
->domain_jul
.being_used
) {
2377 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2378 if (*domains
== NULL
) {
2379 ret
= LTTNG_ERR_FATAL
;
2383 if (session
->kernel_session
!= NULL
) {
2384 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2388 if (session
->ust_session
!= NULL
) {
2389 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2390 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2393 if (session
->ust_session
->domain_jul
.being_used
) {
2394 (*domains
)[index
].type
= LTTNG_DOMAIN_JUL
;
2395 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2403 /* Return negative value to differentiate return code */
2409 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2411 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2412 struct lttng_channel
**channels
)
2415 ssize_t nb_chan
= 0;
2418 case LTTNG_DOMAIN_KERNEL
:
2419 if (session
->kernel_session
!= NULL
) {
2420 nb_chan
= session
->kernel_session
->channel_count
;
2422 DBG3("Number of kernel channels %zd", nb_chan
);
2424 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2427 case LTTNG_DOMAIN_UST
:
2428 if (session
->ust_session
!= NULL
) {
2430 nb_chan
= lttng_ht_get_count(
2431 session
->ust_session
->domain_global
.channels
);
2434 DBG3("Number of UST global channels %zd", nb_chan
);
2436 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2441 ret
= LTTNG_ERR_UND
;
2446 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2447 if (*channels
== NULL
) {
2448 ret
= LTTNG_ERR_FATAL
;
2452 list_lttng_channels(domain
, session
, *channels
);
2458 /* Return negative value to differentiate return code */
2463 * Command LTTNG_LIST_EVENTS processed by the client thread.
2465 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2466 char *channel_name
, struct lttng_event
**events
)
2469 ssize_t nb_event
= 0;
2472 case LTTNG_DOMAIN_KERNEL
:
2473 if (session
->kernel_session
!= NULL
) {
2474 nb_event
= list_lttng_kernel_events(channel_name
,
2475 session
->kernel_session
, events
);
2478 case LTTNG_DOMAIN_UST
:
2480 if (session
->ust_session
!= NULL
) {
2481 nb_event
= list_lttng_ust_global_events(channel_name
,
2482 &session
->ust_session
->domain_global
, events
);
2486 case LTTNG_DOMAIN_JUL
:
2487 if (session
->ust_session
) {
2488 nb_event
= list_lttng_jul_events(
2489 &session
->ust_session
->domain_jul
, events
);
2493 ret
= LTTNG_ERR_UND
;
2500 /* Return negative value to differentiate return code */
2505 * Using the session list, filled a lttng_session array to send back to the
2506 * client for session listing.
2508 * The session list lock MUST be acquired before calling this function. Use
2509 * session_lock_list() and session_unlock_list().
2511 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2516 struct ltt_session
*session
;
2517 struct ltt_session_list
*list
= session_get_list();
2519 DBG("Getting all available session for UID %d GID %d",
2522 * Iterate over session list and append data after the control struct in
2525 cds_list_for_each_entry(session
, &list
->head
, list
) {
2527 * Only list the sessions the user can control.
2529 if (!session_access_ok(session
, uid
, gid
)) {
2533 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2534 struct ltt_ust_session
*usess
= session
->ust_session
;
2536 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2537 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2538 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2539 ret
= build_network_session_path(sessions
[i
].path
,
2540 sizeof(sessions
[i
].path
), session
);
2542 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2543 session
->consumer
->dst
.trace_path
);
2546 PERROR("snprintf session path");
2550 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2551 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2552 sessions
[i
].enabled
= session
->active
;
2553 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2554 sessions
[i
].live_timer_interval
= session
->live_timer
;
2560 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2561 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2563 int cmd_data_pending(struct ltt_session
*session
)
2566 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2567 struct ltt_ust_session
*usess
= session
->ust_session
;
2571 /* Session MUST be stopped to ask for data availability. */
2572 if (session
->active
) {
2573 ret
= LTTNG_ERR_SESSION_STARTED
;
2577 * If stopped, just make sure we've started before else the above call
2578 * will always send that there is data pending.
2580 * The consumer assumes that when the data pending command is received,
2581 * the trace has been started before or else no output data is written
2582 * by the streams which is a condition for data pending. So, this is
2583 * *VERY* important that we don't ask the consumer before a start
2586 if (!session
->has_been_started
) {
2592 if (ksess
&& ksess
->consumer
) {
2593 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2595 /* Data is still being extracted for the kernel. */
2600 if (usess
&& usess
->consumer
) {
2601 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2603 /* Data is still being extracted for the kernel. */
2608 /* Data is ready to be read by a viewer */
2616 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2618 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2620 int cmd_snapshot_add_output(struct ltt_session
*session
,
2621 struct lttng_snapshot_output
*output
, uint32_t *id
)
2624 struct snapshot_output
*new_output
;
2629 DBG("Cmd snapshot add output for session %s", session
->name
);
2632 * Permission denied to create an output if the session is not
2633 * set in no output mode.
2635 if (session
->output_traces
) {
2636 ret
= LTTNG_ERR_EPERM
;
2640 /* Only one output is allowed until we have the "tee" feature. */
2641 if (session
->snapshot
.nb_output
== 1) {
2642 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2646 new_output
= snapshot_output_alloc();
2648 ret
= LTTNG_ERR_NOMEM
;
2652 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2653 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2654 &session
->snapshot
);
2656 if (ret
== -ENOMEM
) {
2657 ret
= LTTNG_ERR_NOMEM
;
2659 ret
= LTTNG_ERR_INVALID
;
2665 snapshot_add_output(&session
->snapshot
, new_output
);
2667 *id
= new_output
->id
;
2674 snapshot_output_destroy(new_output
);
2680 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2682 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2684 int cmd_snapshot_del_output(struct ltt_session
*session
,
2685 struct lttng_snapshot_output
*output
)
2688 struct snapshot_output
*sout
= NULL
;
2696 * Permission denied to create an output if the session is not
2697 * set in no output mode.
2699 if (session
->output_traces
) {
2700 ret
= LTTNG_ERR_EPERM
;
2705 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2707 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2708 } else if (*output
->name
!= '\0') {
2709 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2711 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2714 ret
= LTTNG_ERR_INVALID
;
2718 snapshot_delete_output(&session
->snapshot
, sout
);
2719 snapshot_output_destroy(sout
);
2728 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2730 * If no output is available, outputs is untouched and 0 is returned.
2732 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2734 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2735 struct lttng_snapshot_output
**outputs
)
2738 struct lttng_snapshot_output
*list
= NULL
;
2739 struct lttng_ht_iter iter
;
2740 struct snapshot_output
*output
;
2745 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2748 * Permission denied to create an output if the session is not
2749 * set in no output mode.
2751 if (session
->output_traces
) {
2752 ret
= -LTTNG_ERR_EPERM
;
2756 if (session
->snapshot
.nb_output
== 0) {
2761 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2763 ret
= -LTTNG_ERR_NOMEM
;
2767 /* Copy list from session to the new list object. */
2769 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2770 output
, node
.node
) {
2771 assert(output
->consumer
);
2772 list
[idx
].id
= output
->id
;
2773 list
[idx
].max_size
= output
->max_size
;
2774 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2775 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2776 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2777 sizeof(list
[idx
].ctrl_url
));
2780 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2781 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2783 ret
= -LTTNG_ERR_NOMEM
;
2788 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2789 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2791 ret
= -LTTNG_ERR_NOMEM
;
2800 ret
= session
->snapshot
.nb_output
;
2808 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2809 * snapshot output is *not* set with a remote destination.
2811 * Return 0 on success or a LTTNG_ERR code.
2813 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2814 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2817 struct lttng_ht_iter iter
;
2818 struct consumer_socket
*socket
;
2821 assert(snap_output
);
2824 DBG2("Set relayd object from snapshot output");
2826 /* Ignore if snapshot consumer output is not network. */
2827 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2832 * For each consumer socket, create and send the relayd object of the
2836 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2837 socket
, node
.node
) {
2838 ret
= send_consumer_relayd_sockets(0, session
->id
,
2839 snap_output
->consumer
, socket
,
2840 session
->name
, session
->hostname
,
2841 session
->live_timer
);
2842 if (ret
!= LTTNG_OK
) {
2854 * Record a kernel snapshot.
2856 * Return LTTNG_OK on success or a LTTNG_ERR code.
2858 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2859 struct snapshot_output
*output
, struct ltt_session
*session
,
2860 int wait
, uint64_t nb_packets_per_stream
)
2868 /* Get the datetime for the snapshot output directory. */
2869 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2870 sizeof(output
->datetime
));
2872 ret
= LTTNG_ERR_INVALID
;
2877 * Copy kernel session sockets so we can communicate with the right
2878 * consumer for the snapshot record command.
2880 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2882 ret
= LTTNG_ERR_NOMEM
;
2886 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2887 if (ret
!= LTTNG_OK
) {
2888 goto error_snapshot
;
2891 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
2892 if (ret
!= LTTNG_OK
) {
2893 goto error_snapshot
;
2900 /* Clean up copied sockets so this output can use some other later on. */
2901 consumer_destroy_output_sockets(output
->consumer
);
2908 * Record a UST snapshot.
2910 * Return 0 on success or a LTTNG_ERR error code.
2912 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2913 struct snapshot_output
*output
, struct ltt_session
*session
,
2914 int wait
, uint64_t nb_packets_per_stream
)
2922 /* Get the datetime for the snapshot output directory. */
2923 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2924 sizeof(output
->datetime
));
2926 ret
= LTTNG_ERR_INVALID
;
2931 * Copy UST session sockets so we can communicate with the right
2932 * consumer for the snapshot record command.
2934 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
2936 ret
= LTTNG_ERR_NOMEM
;
2940 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
2941 if (ret
!= LTTNG_OK
) {
2942 goto error_snapshot
;
2945 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
2949 ret
= LTTNG_ERR_INVALID
;
2952 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
2955 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
2958 goto error_snapshot
;
2964 /* Clean up copied sockets so this output can use some other later on. */
2965 consumer_destroy_output_sockets(output
->consumer
);
2971 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
2972 uint64_t cur_nr_packets
)
2974 uint64_t tot_size
= 0;
2976 if (session
->kernel_session
) {
2977 struct ltt_kernel_channel
*chan
;
2978 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2980 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
2981 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
2983 * Don't take channel into account if we
2984 * already grab all its packets.
2988 tot_size
+= chan
->channel
->attr
.subbuf_size
2989 * chan
->stream_count
;
2993 if (session
->ust_session
) {
2994 struct ltt_ust_session
*usess
= session
->ust_session
;
2996 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3004 * Calculate the number of packets we can grab from each stream that
3005 * fits within the overall snapshot max size.
3007 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3008 * the number of packets per stream.
3010 * TODO: this approach is not perfect: we consider the worse case
3011 * (packet filling the sub-buffers) as an upper bound, but we could do
3012 * better if we do this calculation while we actually grab the packet
3013 * content: we would know how much padding we don't actually store into
3016 * This algorithm is currently bounded by the number of packets per
3019 * Since we call this algorithm before actually grabbing the data, it's
3020 * an approximation: for instance, applications could appear/disappear
3021 * in between this call and actually grabbing data.
3024 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3027 uint64_t cur_nb_packets
= 0;
3030 return 0; /* Infinite */
3033 size_left
= max_size
;
3035 uint64_t one_more_packet_tot_size
;
3037 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3039 if (!one_more_packet_tot_size
) {
3040 /* We are already grabbing all packets. */
3043 size_left
-= one_more_packet_tot_size
;
3044 if (size_left
< 0) {
3049 if (!cur_nb_packets
) {
3050 /* Not enough room to grab one packet of each stream, error. */
3053 return cur_nb_packets
;
3057 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3059 * The wait parameter is ignored so this call always wait for the snapshot to
3060 * complete before returning.
3062 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3064 int cmd_snapshot_record(struct ltt_session
*session
,
3065 struct lttng_snapshot_output
*output
, int wait
)
3068 unsigned int use_tmp_output
= 0;
3069 struct snapshot_output tmp_output
;
3070 unsigned int snapshot_success
= 0;
3074 DBG("Cmd snapshot record for session %s", session
->name
);
3077 * Permission denied to create an output if the session is not
3078 * set in no output mode.
3080 if (session
->output_traces
) {
3081 ret
= LTTNG_ERR_EPERM
;
3085 /* The session needs to be started at least once. */
3086 if (!session
->has_been_started
) {
3087 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3091 /* Use temporary output for the session. */
3092 if (output
&& *output
->ctrl_url
!= '\0') {
3093 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3094 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3097 if (ret
== -ENOMEM
) {
3098 ret
= LTTNG_ERR_NOMEM
;
3100 ret
= LTTNG_ERR_INVALID
;
3104 /* Use the global session count for the temporary snapshot. */
3105 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3109 if (session
->kernel_session
) {
3110 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3112 if (use_tmp_output
) {
3113 int64_t nb_packets_per_stream
;
3115 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3116 tmp_output
.max_size
);
3117 if (nb_packets_per_stream
< 0) {
3118 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3121 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3122 wait
, nb_packets_per_stream
);
3123 if (ret
!= LTTNG_OK
) {
3126 snapshot_success
= 1;
3128 struct snapshot_output
*sout
;
3129 struct lttng_ht_iter iter
;
3132 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3133 &iter
.iter
, sout
, node
.node
) {
3134 int64_t nb_packets_per_stream
;
3137 * Make a local copy of the output and assign the possible
3138 * temporary value given by the caller.
3140 memset(&tmp_output
, 0, sizeof(tmp_output
));
3141 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3143 if (output
->max_size
!= (uint64_t) -1ULL) {
3144 tmp_output
.max_size
= output
->max_size
;
3147 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3148 tmp_output
.max_size
);
3149 if (nb_packets_per_stream
< 0) {
3150 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3154 /* Use temporary name. */
3155 if (*output
->name
!= '\0') {
3156 strncpy(tmp_output
.name
, output
->name
,
3157 sizeof(tmp_output
.name
));
3160 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3162 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3163 session
, wait
, nb_packets_per_stream
);
3164 if (ret
!= LTTNG_OK
) {
3168 snapshot_success
= 1;
3174 if (session
->ust_session
) {
3175 struct ltt_ust_session
*usess
= session
->ust_session
;
3177 if (use_tmp_output
) {
3178 int64_t nb_packets_per_stream
;
3180 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3181 tmp_output
.max_size
);
3182 if (nb_packets_per_stream
< 0) {
3183 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3186 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3187 wait
, nb_packets_per_stream
);
3188 if (ret
!= LTTNG_OK
) {
3191 snapshot_success
= 1;
3193 struct snapshot_output
*sout
;
3194 struct lttng_ht_iter iter
;
3197 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3198 &iter
.iter
, sout
, node
.node
) {
3199 int64_t nb_packets_per_stream
;
3202 * Make a local copy of the output and assign the possible
3203 * temporary value given by the caller.
3205 memset(&tmp_output
, 0, sizeof(tmp_output
));
3206 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3208 if (output
->max_size
!= (uint64_t) -1ULL) {
3209 tmp_output
.max_size
= output
->max_size
;
3212 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3213 tmp_output
.max_size
);
3214 if (nb_packets_per_stream
< 0) {
3215 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3220 /* Use temporary name. */
3221 if (*output
->name
!= '\0') {
3222 strncpy(tmp_output
.name
, output
->name
,
3223 sizeof(tmp_output
.name
));
3226 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3228 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3229 wait
, nb_packets_per_stream
);
3230 if (ret
!= LTTNG_OK
) {
3234 snapshot_success
= 1;
3240 if (snapshot_success
) {
3241 session
->snapshot
.nb_snapshot
++;
3243 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3251 * Init command subsystem.
3256 * Set network sequence index to 1 for streams to match a relayd
3257 * socket on the consumer side.
3259 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3260 relayd_net_seq_idx
= 1;
3261 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3263 DBG("Command subsystem initialized");