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 switch (uchan
->attr
.output
) {
173 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
187 * Create a list of JUL domain events.
189 * Return number of events in list on success or else a negative value.
191 static int list_lttng_jul_events(struct jul_domain
*dom
,
192 struct lttng_event
**events
)
195 unsigned int nb_event
= 0;
196 struct jul_event
*event
;
197 struct lttng_event
*tmp_events
;
198 struct lttng_ht_iter iter
;
203 DBG3("Listing JUL events");
205 nb_event
= lttng_ht_get_count(dom
->events
);
211 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
213 PERROR("zmalloc JUL events session");
214 ret
= -LTTNG_ERR_FATAL
;
219 cds_lfht_for_each_entry(dom
->events
->ht
, &iter
.iter
, event
, node
.node
) {
220 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
221 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
222 tmp_events
[i
].enabled
= event
->enabled
;
227 *events
= tmp_events
;
231 assert(nb_event
== i
);
236 * Create a list of ust global domain events.
238 static int list_lttng_ust_global_events(char *channel_name
,
239 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
242 unsigned int nb_event
= 0;
243 struct lttng_ht_iter iter
;
244 struct lttng_ht_node_str
*node
;
245 struct ltt_ust_channel
*uchan
;
246 struct ltt_ust_event
*uevent
;
247 struct lttng_event
*tmp
;
249 DBG("Listing UST global events for channel %s", channel_name
);
253 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
254 node
= lttng_ht_iter_get_node_str(&iter
);
256 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
260 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
262 nb_event
+= lttng_ht_get_count(uchan
->events
);
269 DBG3("Listing UST global %d events", nb_event
);
271 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
273 ret
= LTTNG_ERR_FATAL
;
277 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
278 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
279 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
280 tmp
[i
].enabled
= uevent
->enabled
;
282 switch (uevent
->attr
.instrumentation
) {
283 case LTTNG_UST_TRACEPOINT
:
284 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
286 case LTTNG_UST_PROBE
:
287 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
289 case LTTNG_UST_FUNCTION
:
290 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
294 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
295 switch (uevent
->attr
.loglevel_type
) {
296 case LTTNG_UST_LOGLEVEL_ALL
:
297 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
299 case LTTNG_UST_LOGLEVEL_RANGE
:
300 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
302 case LTTNG_UST_LOGLEVEL_SINGLE
:
303 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
306 if (uevent
->filter
) {
309 if (uevent
->exclusion
) {
310 tmp
[i
].exclusion
= 1;
324 * Fill lttng_event array of all kernel events in the channel.
326 static int list_lttng_kernel_events(char *channel_name
,
327 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
330 unsigned int nb_event
;
331 struct ltt_kernel_event
*event
;
332 struct ltt_kernel_channel
*kchan
;
334 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
336 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
340 nb_event
= kchan
->event_count
;
342 DBG("Listing events for channel %s", kchan
->channel
->name
);
348 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
349 if (*events
== NULL
) {
350 ret
= LTTNG_ERR_FATAL
;
354 /* Kernel channels */
355 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
356 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
357 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
358 (*events
)[i
].enabled
= event
->enabled
;
360 switch (event
->event
->instrumentation
) {
361 case LTTNG_KERNEL_TRACEPOINT
:
362 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
364 case LTTNG_KERNEL_KRETPROBE
:
365 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
366 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
367 sizeof(struct lttng_kernel_kprobe
));
369 case LTTNG_KERNEL_KPROBE
:
370 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
371 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
372 sizeof(struct lttng_kernel_kprobe
));
374 case LTTNG_KERNEL_FUNCTION
:
375 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
376 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
377 sizeof(struct lttng_kernel_function
));
379 case LTTNG_KERNEL_NOOP
:
380 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
382 case LTTNG_KERNEL_SYSCALL
:
383 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
385 case LTTNG_KERNEL_ALL
:
396 /* Negate the error code to differentiate the size from an error */
401 * Add URI so the consumer output object. Set the correct path depending on the
402 * domain adding the default trace directory.
404 static int add_uri_to_consumer(struct consumer_output
*consumer
,
405 struct lttng_uri
*uri
, int domain
, const char *session_name
)
408 const char *default_trace_dir
;
412 if (consumer
== NULL
) {
413 DBG("No consumer detected. Don't add URI. Stopping.");
414 ret
= LTTNG_ERR_NO_CONSUMER
;
419 case LTTNG_DOMAIN_KERNEL
:
420 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
422 case LTTNG_DOMAIN_UST
:
423 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
427 * This case is possible is we try to add the URI to the global tracing
428 * session consumer object which in this case there is no subdir.
430 default_trace_dir
= "";
433 switch (uri
->dtype
) {
436 DBG2("Setting network URI to consumer");
438 if (consumer
->type
== CONSUMER_DST_NET
) {
439 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
440 consumer
->dst
.net
.control_isset
) ||
441 (uri
->stype
== LTTNG_STREAM_DATA
&&
442 consumer
->dst
.net
.data_isset
)) {
443 ret
= LTTNG_ERR_URL_EXIST
;
447 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
450 consumer
->type
= CONSUMER_DST_NET
;
452 /* Set URI into consumer output object */
453 ret
= consumer_set_network_uri(consumer
, uri
);
457 } else if (ret
== 1) {
459 * URI was the same in the consumer so we do not append the subdir
460 * again so to not duplicate output dir.
466 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
467 ret
= consumer_set_subdir(consumer
, session_name
);
469 ret
= LTTNG_ERR_FATAL
;
474 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
475 /* On a new subdir, reappend the default trace dir. */
476 strncat(consumer
->subdir
, default_trace_dir
,
477 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
478 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
483 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
484 memset(consumer
->dst
.trace_path
, 0,
485 sizeof(consumer
->dst
.trace_path
));
486 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
487 sizeof(consumer
->dst
.trace_path
));
488 /* Append default trace dir */
489 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
490 sizeof(consumer
->dst
.trace_path
) -
491 strlen(consumer
->dst
.trace_path
) - 1);
492 /* Flag consumer as local. */
493 consumer
->type
= CONSUMER_DST_LOCAL
;
504 * Init tracing by creating trace directory and sending fds kernel consumer.
506 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
509 struct lttng_ht_iter iter
;
510 struct consumer_socket
*socket
;
516 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
517 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
519 pthread_mutex_lock(socket
->lock
);
520 ret
= kernel_consumer_send_session(socket
, session
);
521 pthread_mutex_unlock(socket
->lock
);
523 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
535 * Create a socket to the relayd using the URI.
537 * On success, the relayd_sock pointer is set to the created socket.
538 * Else, it's stays untouched and a lttcomm error code is returned.
540 static int create_connect_relayd(struct lttng_uri
*uri
,
541 struct lttcomm_relayd_sock
**relayd_sock
)
544 struct lttcomm_relayd_sock
*rsock
;
546 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
547 RELAYD_VERSION_COMM_MINOR
);
549 ret
= LTTNG_ERR_FATAL
;
554 * Connect to relayd so we can proceed with a session creation. This call
555 * can possibly block for an arbitrary amount of time to set the health
556 * state to be in poll execution.
559 ret
= relayd_connect(rsock
);
562 ERR("Unable to reach lttng-relayd");
563 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
567 /* Create socket for control stream. */
568 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
569 DBG3("Creating relayd stream socket from URI");
571 /* Check relayd version */
572 ret
= relayd_version_check(rsock
);
574 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
577 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
578 DBG3("Creating relayd data socket from URI");
580 /* Command is not valid */
581 ERR("Relayd invalid stream type: %d", uri
->stype
);
582 ret
= LTTNG_ERR_INVALID
;
586 *relayd_sock
= rsock
;
591 /* The returned value is not useful since we are on an error path. */
592 (void) relayd_close(rsock
);
600 * Connect to the relayd using URI and send the socket to the right consumer.
602 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
603 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
604 struct consumer_socket
*consumer_sock
,
605 char *session_name
, char *hostname
, int session_live_timer
)
608 struct lttcomm_relayd_sock
*rsock
= NULL
;
610 /* Connect to relayd and make version check if uri is the control. */
611 ret
= create_connect_relayd(relayd_uri
, &rsock
);
612 if (ret
!= LTTNG_OK
) {
617 /* Set the network sequence index if not set. */
618 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
619 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
621 * Increment net_seq_idx because we are about to transfer the
622 * new relayd socket to the consumer.
623 * Assign unique key so the consumer can match streams.
625 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
626 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
629 /* Send relayd socket to consumer. */
630 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
631 relayd_uri
->stype
, session_id
,
632 session_name
, hostname
, session_live_timer
);
634 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
638 /* Flag that the corresponding socket was sent. */
639 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
640 consumer_sock
->control_sock_sent
= 1;
641 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
642 consumer_sock
->data_sock_sent
= 1;
648 * Close socket which was dup on the consumer side. The session daemon does
649 * NOT keep track of the relayd socket(s) once transfer to the consumer.
653 (void) relayd_close(rsock
);
657 if (ret
!= LTTNG_OK
) {
659 * The consumer output for this session should not be used anymore
660 * since the relayd connection failed thus making any tracing or/and
661 * streaming not usable.
663 consumer
->enabled
= 0;
669 * Send both relayd sockets to a specific consumer and domain. This is a
670 * helper function to facilitate sending the information to the consumer for a
673 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
674 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
675 char *session_name
, char *hostname
, int session_live_timer
)
682 /* Sending control relayd socket. */
683 if (!sock
->control_sock_sent
) {
684 ret
= send_consumer_relayd_socket(domain
, session_id
,
685 &consumer
->dst
.net
.control
, consumer
, sock
,
686 session_name
, hostname
, session_live_timer
);
687 if (ret
!= LTTNG_OK
) {
692 /* Sending data relayd socket. */
693 if (!sock
->data_sock_sent
) {
694 ret
= send_consumer_relayd_socket(domain
, session_id
,
695 &consumer
->dst
.net
.data
, consumer
, sock
,
696 session_name
, hostname
, session_live_timer
);
697 if (ret
!= LTTNG_OK
) {
707 * Setup relayd connections for a tracing session. First creates the socket to
708 * the relayd and send them to the right domain consumer. Consumer type MUST be
711 int cmd_setup_relayd(struct ltt_session
*session
)
714 struct ltt_ust_session
*usess
;
715 struct ltt_kernel_session
*ksess
;
716 struct consumer_socket
*socket
;
717 struct lttng_ht_iter iter
;
721 usess
= session
->ust_session
;
722 ksess
= session
->kernel_session
;
724 DBG("Setting relayd for session %s", session
->name
);
728 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
729 && usess
->consumer
->enabled
) {
730 /* For each consumer socket, send relayd sockets */
731 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
733 pthread_mutex_lock(socket
->lock
);
734 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
735 usess
->consumer
, socket
,
736 session
->name
, session
->hostname
,
737 session
->live_timer
);
738 pthread_mutex_unlock(socket
->lock
);
739 if (ret
!= LTTNG_OK
) {
742 /* Session is now ready for network streaming. */
743 session
->net_handle
= 1;
747 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
748 && ksess
->consumer
->enabled
) {
749 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
751 pthread_mutex_lock(socket
->lock
);
752 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
753 ksess
->consumer
, socket
,
754 session
->name
, session
->hostname
,
755 session
->live_timer
);
756 pthread_mutex_unlock(socket
->lock
);
757 if (ret
!= LTTNG_OK
) {
760 /* Session is now ready for network streaming. */
761 session
->net_handle
= 1;
771 * Start a kernel session by opening all necessary streams.
773 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
776 struct ltt_kernel_channel
*kchan
;
778 /* Open kernel metadata */
779 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
780 ret
= kernel_open_metadata(ksess
);
782 ret
= LTTNG_ERR_KERN_META_FAIL
;
787 /* Open kernel metadata stream */
788 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
789 ret
= kernel_open_metadata_stream(ksess
);
791 ERR("Kernel create metadata stream failed");
792 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
797 /* For each channel */
798 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
799 if (kchan
->stream_count
== 0) {
800 ret
= kernel_open_channel_stream(kchan
);
802 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
805 /* Update the stream global counter */
806 ksess
->stream_count_global
+= ret
;
810 /* Setup kernel consumer socket and send fds to it */
811 ret
= init_kernel_tracing(ksess
);
813 ret
= LTTNG_ERR_KERN_START_FAIL
;
817 /* This start the kernel tracing */
818 ret
= kernel_start_session(ksess
);
820 ret
= LTTNG_ERR_KERN_START_FAIL
;
824 /* Quiescent wait after starting trace */
825 kernel_wait_quiescent(kernel_tracer_fd
);
836 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
838 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
842 struct ltt_ust_session
*usess
;
844 usess
= session
->ust_session
;
849 case LTTNG_DOMAIN_KERNEL
:
851 ret
= channel_kernel_disable(session
->kernel_session
,
853 if (ret
!= LTTNG_OK
) {
857 kernel_wait_quiescent(kernel_tracer_fd
);
860 case LTTNG_DOMAIN_UST
:
862 struct ltt_ust_channel
*uchan
;
863 struct lttng_ht
*chan_ht
;
865 chan_ht
= usess
->domain_global
.channels
;
867 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
869 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
873 ret
= channel_ust_disable(usess
, uchan
);
874 if (ret
!= LTTNG_OK
) {
880 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
881 case LTTNG_DOMAIN_UST_EXEC_NAME
:
882 case LTTNG_DOMAIN_UST_PID
:
885 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
897 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
899 * The wpipe arguments is used as a notifier for the kernel thread.
901 int cmd_enable_channel(struct ltt_session
*session
,
902 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
905 struct ltt_ust_session
*usess
= session
->ust_session
;
906 struct lttng_ht
*chan_ht
;
913 len
= strnlen(attr
->name
, sizeof(attr
->name
));
915 /* Validate channel name */
916 if (attr
->name
[0] == '.' ||
917 memchr(attr
->name
, '/', len
) != NULL
) {
918 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
922 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
927 * Don't try to enable a channel if the session has been started at
928 * some point in time before. The tracer does not allow it.
930 if (session
->has_been_started
) {
931 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
936 * If the session is a live session, remove the switch timer, the
937 * live timer does the same thing but sends also synchronisation
938 * beacons for inactive streams.
940 if (session
->live_timer
> 0) {
941 attr
->attr
.live_timer_interval
= session
->live_timer
;
942 attr
->attr
.switch_timer_interval
= 0;
946 * The ringbuffer (both in user space and kernel) behave badly in overwrite
947 * mode and with less than 2 subbuf so block it right away and send back an
948 * invalid attribute error.
950 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
951 ret
= LTTNG_ERR_INVALID
;
955 switch (domain
->type
) {
956 case LTTNG_DOMAIN_KERNEL
:
958 struct ltt_kernel_channel
*kchan
;
960 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
961 session
->kernel_session
);
963 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
964 if (attr
->name
[0] != '\0') {
965 session
->kernel_session
->has_non_default_channel
= 1;
968 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
971 if (ret
!= LTTNG_OK
) {
975 kernel_wait_quiescent(kernel_tracer_fd
);
978 case LTTNG_DOMAIN_UST
:
980 struct ltt_ust_channel
*uchan
;
982 chan_ht
= usess
->domain_global
.channels
;
984 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
986 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
987 if (attr
->name
[0] != '\0') {
988 usess
->has_non_default_channel
= 1;
991 ret
= channel_ust_enable(usess
, uchan
);
996 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1008 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1010 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1011 char *channel_name
, char *event_name
)
1018 case LTTNG_DOMAIN_KERNEL
:
1020 struct ltt_kernel_channel
*kchan
;
1021 struct ltt_kernel_session
*ksess
;
1023 ksess
= session
->kernel_session
;
1026 * If a non-default channel has been created in the
1027 * session, explicitely require that -c chan_name needs
1030 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1031 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1035 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1036 if (kchan
== NULL
) {
1037 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1041 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1042 if (ret
!= LTTNG_OK
) {
1046 kernel_wait_quiescent(kernel_tracer_fd
);
1049 case LTTNG_DOMAIN_UST
:
1051 struct ltt_ust_channel
*uchan
;
1052 struct ltt_ust_session
*usess
;
1054 usess
= session
->ust_session
;
1057 * If a non-default channel has been created in the
1058 * session, explicitely require that -c chan_name needs
1061 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1062 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1066 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1068 if (uchan
== NULL
) {
1069 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1073 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1074 if (ret
!= LTTNG_OK
) {
1078 DBG3("Disable UST event %s in channel %s completed", event_name
,
1082 case LTTNG_DOMAIN_JUL
:
1084 struct ltt_ust_session
*usess
= session
->ust_session
;
1088 ret
= event_jul_disable(usess
, event_name
);
1089 if (ret
!= LTTNG_OK
) {
1096 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1097 case LTTNG_DOMAIN_UST_PID
:
1098 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1101 ret
= LTTNG_ERR_UND
;
1113 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
1115 int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
1123 case LTTNG_DOMAIN_KERNEL
:
1125 struct ltt_kernel_session
*ksess
;
1126 struct ltt_kernel_channel
*kchan
;
1128 ksess
= session
->kernel_session
;
1131 * If a non-default channel has been created in the
1132 * session, explicitely require that -c chan_name needs
1135 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1136 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1140 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1141 if (kchan
== NULL
) {
1142 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1146 ret
= event_kernel_disable_all(kchan
);
1147 if (ret
!= LTTNG_OK
) {
1151 kernel_wait_quiescent(kernel_tracer_fd
);
1154 case LTTNG_DOMAIN_UST
:
1156 struct ltt_ust_session
*usess
;
1157 struct ltt_ust_channel
*uchan
;
1159 usess
= session
->ust_session
;
1162 * If a non-default channel has been created in the
1163 * session, explicitely require that -c chan_name needs
1166 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1167 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1171 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1173 if (uchan
== NULL
) {
1174 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1178 ret
= event_ust_disable_all_tracepoints(usess
, uchan
);
1183 DBG3("Disable all UST events in channel %s completed", channel_name
);
1187 case LTTNG_DOMAIN_JUL
:
1189 struct ltt_ust_session
*usess
= session
->ust_session
;
1193 ret
= event_jul_disable_all(usess
);
1194 if (ret
!= LTTNG_OK
) {
1201 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1202 case LTTNG_DOMAIN_UST_PID
:
1203 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1206 ret
= LTTNG_ERR_UND
;
1218 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1220 int cmd_add_context(struct ltt_session
*session
, int domain
,
1221 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1223 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1226 case LTTNG_DOMAIN_KERNEL
:
1227 assert(session
->kernel_session
);
1229 if (session
->kernel_session
->channel_count
== 0) {
1230 /* Create default channel */
1231 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1232 if (ret
!= LTTNG_OK
) {
1235 chan_kern_created
= 1;
1237 /* Add kernel context to kernel tracer */
1238 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1239 if (ret
!= LTTNG_OK
) {
1243 case LTTNG_DOMAIN_UST
:
1245 struct ltt_ust_session
*usess
= session
->ust_session
;
1246 unsigned int chan_count
;
1250 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1251 if (chan_count
== 0) {
1252 struct lttng_channel
*attr
;
1253 /* Create default channel */
1254 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1256 ret
= LTTNG_ERR_FATAL
;
1260 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1261 if (ret
!= LTTNG_OK
) {
1266 chan_ust_created
= 1;
1269 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1270 if (ret
!= LTTNG_OK
) {
1276 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1277 case LTTNG_DOMAIN_UST_PID
:
1278 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1281 ret
= LTTNG_ERR_UND
;
1288 if (chan_kern_created
) {
1289 struct ltt_kernel_channel
*kchan
=
1290 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1291 session
->kernel_session
);
1292 /* Created previously, this should NOT fail. */
1294 kernel_destroy_channel(kchan
);
1297 if (chan_ust_created
) {
1298 struct ltt_ust_channel
*uchan
=
1299 trace_ust_find_channel_by_name(
1300 session
->ust_session
->domain_global
.channels
,
1301 DEFAULT_CHANNEL_NAME
);
1302 /* Created previously, this should NOT fail. */
1304 /* Remove from the channel list of the session. */
1305 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1307 trace_ust_destroy_channel(uchan
);
1313 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1315 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1316 char *channel_name
, struct lttng_event
*event
,
1317 struct lttng_filter_bytecode
*filter
,
1318 struct lttng_event_exclusion
*exclusion
,
1321 int ret
, channel_created
= 0;
1322 struct lttng_channel
*attr
;
1326 assert(channel_name
);
1330 switch (domain
->type
) {
1331 case LTTNG_DOMAIN_KERNEL
:
1333 struct ltt_kernel_channel
*kchan
;
1336 * If a non-default channel has been created in the
1337 * session, explicitely require that -c chan_name needs
1340 if (session
->kernel_session
->has_non_default_channel
1341 && channel_name
[0] == '\0') {
1342 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1346 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1347 session
->kernel_session
);
1348 if (kchan
== NULL
) {
1349 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1350 LTTNG_BUFFER_GLOBAL
);
1352 ret
= LTTNG_ERR_FATAL
;
1355 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1357 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1358 if (ret
!= LTTNG_OK
) {
1364 channel_created
= 1;
1367 /* Get the newly created kernel channel pointer */
1368 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1369 session
->kernel_session
);
1370 if (kchan
== NULL
) {
1371 /* This sould not happen... */
1372 ret
= LTTNG_ERR_FATAL
;
1376 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1377 if (ret
!= LTTNG_OK
) {
1378 if (channel_created
) {
1379 /* Let's not leak a useless channel. */
1380 kernel_destroy_channel(kchan
);
1385 kernel_wait_quiescent(kernel_tracer_fd
);
1388 case LTTNG_DOMAIN_UST
:
1390 struct ltt_ust_channel
*uchan
;
1391 struct ltt_ust_session
*usess
= session
->ust_session
;
1396 * If a non-default channel has been created in the
1397 * session, explicitely require that -c chan_name needs
1400 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1401 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1405 /* Get channel from global UST domain */
1406 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1408 if (uchan
== NULL
) {
1409 /* Create default channel */
1410 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1411 usess
->buffer_type
);
1413 ret
= LTTNG_ERR_FATAL
;
1416 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1418 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1419 if (ret
!= LTTNG_OK
) {
1425 /* Get the newly created channel reference back */
1426 uchan
= trace_ust_find_channel_by_name(
1427 usess
->domain_global
.channels
, channel_name
);
1431 /* At this point, the session and channel exist on the tracer */
1432 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
, filter
, exclusion
);
1433 if (ret
!= LTTNG_OK
) {
1438 case LTTNG_DOMAIN_JUL
:
1440 struct lttng_event uevent
;
1441 struct lttng_domain tmp_dom
;
1442 struct ltt_ust_session
*usess
= session
->ust_session
;
1446 /* Create the default JUL tracepoint. */
1447 memset(&uevent
, 0, sizeof(uevent
));
1448 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1449 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1451 strncpy(uevent
.name
, DEFAULT_SYS_JUL_EVENT_NAME
,
1452 sizeof(uevent
.name
));
1454 strncpy(uevent
.name
, DEFAULT_USER_JUL_EVENT_NAME
,
1455 sizeof(uevent
.name
));
1457 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1460 * The domain type is changed because we are about to enable the
1461 * default channel and event for the JUL domain that are hardcoded.
1462 * This happens in the UST domain.
1464 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1465 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1467 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1468 &uevent
, filter
, NULL
, wpipe
);
1469 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1473 /* The wild card * means that everything should be enabled. */
1474 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1475 ret
= event_jul_enable_all(usess
, event
, filter
);
1477 ret
= event_jul_enable(usess
, event
, filter
);
1479 if (ret
!= LTTNG_OK
) {
1486 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1487 case LTTNG_DOMAIN_UST_PID
:
1488 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1491 ret
= LTTNG_ERR_UND
;
1503 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1505 int cmd_enable_event_all(struct ltt_session
*session
,
1506 struct lttng_domain
*domain
, char *channel_name
, int event_type
,
1507 struct lttng_filter_bytecode
*filter
, int wpipe
)
1510 struct lttng_channel
*attr
;
1513 assert(channel_name
);
1517 switch (domain
->type
) {
1518 case LTTNG_DOMAIN_KERNEL
:
1520 struct ltt_kernel_channel
*kchan
;
1522 assert(session
->kernel_session
);
1525 * If a non-default channel has been created in the
1526 * session, explicitely require that -c chan_name needs
1529 if (session
->kernel_session
->has_non_default_channel
1530 && channel_name
[0] == '\0') {
1531 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1535 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1536 session
->kernel_session
);
1537 if (kchan
== NULL
) {
1538 /* Create default channel */
1539 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1540 LTTNG_BUFFER_GLOBAL
);
1542 ret
= LTTNG_ERR_FATAL
;
1545 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1547 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1548 if (ret
!= LTTNG_OK
) {
1554 /* Get the newly created kernel channel pointer */
1555 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1556 session
->kernel_session
);
1560 switch (event_type
) {
1561 case LTTNG_EVENT_SYSCALL
:
1562 ret
= event_kernel_enable_all_syscalls(kchan
, kernel_tracer_fd
);
1564 case LTTNG_EVENT_TRACEPOINT
:
1566 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1567 * events already registered to the channel.
1569 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1571 case LTTNG_EVENT_ALL
:
1572 /* Enable syscalls and tracepoints */
1573 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1576 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1580 /* Manage return value */
1581 if (ret
!= LTTNG_OK
) {
1583 * On error, cmd_enable_channel call will take care of destroying
1584 * the created channel if it was needed.
1589 kernel_wait_quiescent(kernel_tracer_fd
);
1592 case LTTNG_DOMAIN_UST
:
1594 struct ltt_ust_channel
*uchan
;
1595 struct ltt_ust_session
*usess
= session
->ust_session
;
1600 * If a non-default channel has been created in the
1601 * session, explicitely require that -c chan_name needs
1604 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1605 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1609 /* Get channel from global UST domain */
1610 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1612 if (uchan
== NULL
) {
1613 /* Create default channel */
1614 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1615 usess
->buffer_type
);
1617 ret
= LTTNG_ERR_FATAL
;
1620 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1622 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1623 if (ret
!= LTTNG_OK
) {
1629 /* Get the newly created channel reference back */
1630 uchan
= trace_ust_find_channel_by_name(
1631 usess
->domain_global
.channels
, channel_name
);
1635 /* At this point, the session and channel exist on the tracer */
1637 switch (event_type
) {
1638 case LTTNG_EVENT_ALL
:
1639 case LTTNG_EVENT_TRACEPOINT
:
1640 ret
= event_ust_enable_all_tracepoints(usess
, uchan
, filter
);
1641 if (ret
!= LTTNG_OK
) {
1646 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
1650 /* Manage return value */
1651 if (ret
!= LTTNG_OK
) {
1657 case LTTNG_DOMAIN_JUL
:
1659 struct lttng_event uevent
, event
;
1660 struct lttng_domain tmp_dom
;
1661 struct ltt_ust_session
*usess
= session
->ust_session
;
1665 /* Create the default JUL tracepoint. */
1666 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1667 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1669 strncpy(uevent
.name
, DEFAULT_SYS_JUL_EVENT_NAME
,
1670 sizeof(uevent
.name
));
1672 strncpy(uevent
.name
, DEFAULT_USER_JUL_EVENT_NAME
,
1673 sizeof(uevent
.name
));
1675 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1678 * The domain type is changed because we are about to enable the
1679 * default channel and event for the JUL domain that are hardcoded.
1680 * This happens in the UST domain.
1682 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1683 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1685 ret
= cmd_enable_event(session
, &tmp_dom
, DEFAULT_JUL_CHANNEL_NAME
,
1686 &uevent
, NULL
, NULL
, wpipe
);
1687 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1691 event
.loglevel
= LTTNG_LOGLEVEL_JUL_ALL
;
1692 event
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1693 strncpy(event
.name
, "*", sizeof(event
.name
));
1694 event
.name
[sizeof(event
.name
) - 1] = '\0';
1696 ret
= event_jul_enable_all(usess
, &event
, filter
);
1697 if (ret
!= LTTNG_OK
) {
1704 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1705 case LTTNG_DOMAIN_UST_PID
:
1706 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1709 ret
= LTTNG_ERR_UND
;
1722 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1724 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1727 ssize_t nb_events
= 0;
1730 case LTTNG_DOMAIN_KERNEL
:
1731 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1732 if (nb_events
< 0) {
1733 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1737 case LTTNG_DOMAIN_UST
:
1738 nb_events
= ust_app_list_events(events
);
1739 if (nb_events
< 0) {
1740 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1744 case LTTNG_DOMAIN_JUL
:
1745 nb_events
= jul_list_events(events
);
1746 if (nb_events
< 0) {
1747 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1752 ret
= LTTNG_ERR_UND
;
1759 /* Return negative value to differentiate return code */
1764 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1766 ssize_t
cmd_list_tracepoint_fields(int domain
,
1767 struct lttng_event_field
**fields
)
1770 ssize_t nb_fields
= 0;
1773 case LTTNG_DOMAIN_UST
:
1774 nb_fields
= ust_app_list_event_fields(fields
);
1775 if (nb_fields
< 0) {
1776 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1780 case LTTNG_DOMAIN_KERNEL
:
1781 default: /* fall-through */
1782 ret
= LTTNG_ERR_UND
;
1789 /* Return negative value to differentiate return code */
1794 * Command LTTNG_START_TRACE processed by the client thread.
1796 int cmd_start_trace(struct ltt_session
*session
)
1799 unsigned long nb_chan
= 0;
1800 struct ltt_kernel_session
*ksession
;
1801 struct ltt_ust_session
*usess
;
1805 /* Ease our life a bit ;) */
1806 ksession
= session
->kernel_session
;
1807 usess
= session
->ust_session
;
1809 /* Is the session already started? */
1810 if (session
->active
) {
1811 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1816 * Starting a session without channel is useless since after that it's not
1817 * possible to enable channel thus inform the client.
1819 if (usess
&& usess
->domain_global
.channels
) {
1821 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1825 nb_chan
+= ksession
->channel_count
;
1828 ret
= LTTNG_ERR_NO_CHANNEL
;
1832 /* Kernel tracing */
1833 if (ksession
!= NULL
) {
1834 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1835 if (ret
!= LTTNG_OK
) {
1840 /* Flag session that trace should start automatically */
1842 usess
->start_trace
= 1;
1844 ret
= ust_app_start_trace_all(usess
);
1846 ret
= LTTNG_ERR_UST_START_FAIL
;
1851 /* Flag this after a successful start. */
1852 session
->has_been_started
= 1;
1853 session
->active
= 1;
1862 * Command LTTNG_STOP_TRACE processed by the client thread.
1864 int cmd_stop_trace(struct ltt_session
*session
)
1867 struct ltt_kernel_channel
*kchan
;
1868 struct ltt_kernel_session
*ksession
;
1869 struct ltt_ust_session
*usess
;
1874 ksession
= session
->kernel_session
;
1875 usess
= session
->ust_session
;
1877 /* Session is not active. Skip everythong and inform the client. */
1878 if (!session
->active
) {
1879 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1884 if (ksession
&& ksession
->started
) {
1885 DBG("Stop kernel tracing");
1887 /* Flush metadata if exist */
1888 if (ksession
->metadata_stream_fd
>= 0) {
1889 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1891 ERR("Kernel metadata flush failed");
1895 /* Flush all buffers before stopping */
1896 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1897 ret
= kernel_flush_buffer(kchan
);
1899 ERR("Kernel flush buffer error");
1903 ret
= kernel_stop_session(ksession
);
1905 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1909 kernel_wait_quiescent(kernel_tracer_fd
);
1911 ksession
->started
= 0;
1914 if (usess
&& usess
->start_trace
) {
1915 usess
->start_trace
= 0;
1917 ret
= ust_app_stop_trace_all(usess
);
1919 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1924 /* Flag inactive after a successful stop. */
1925 session
->active
= 0;
1933 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1935 int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
1936 size_t nb_uri
, struct lttng_uri
*uris
)
1939 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1940 struct ltt_ust_session
*usess
= session
->ust_session
;
1941 struct consumer_output
*consumer
= NULL
;
1947 /* Can't set consumer URI if the session is active. */
1948 if (session
->active
) {
1949 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1954 * This case switch makes sure the domain session has a temporary consumer
1955 * so the URL can be set.
1959 /* Code flow error. A session MUST always have a consumer object */
1960 assert(session
->consumer
);
1962 * The URL will be added to the tracing session consumer instead of a
1963 * specific domain consumer.
1965 consumer
= session
->consumer
;
1967 case LTTNG_DOMAIN_KERNEL
:
1968 /* Code flow error if we don't have a kernel session here. */
1970 assert(ksess
->consumer
);
1971 consumer
= ksess
->consumer
;
1973 case LTTNG_DOMAIN_UST
:
1974 /* Code flow error if we don't have a kernel session here. */
1976 assert(usess
->consumer
);
1977 consumer
= usess
->consumer
;
1981 for (i
= 0; i
< nb_uri
; i
++) {
1982 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
1983 if (ret
!= LTTNG_OK
) {
1989 * Make sure to set the session in output mode after we set URI since a
1990 * session can be created without URL (thus flagged in no output mode).
1992 session
->output_traces
= 1;
1994 ksess
->output_traces
= 1;
1996 usess
->output_traces
= 1;
2007 * Command LTTNG_CREATE_SESSION processed by the client thread.
2009 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2010 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2013 struct ltt_session
*session
;
2019 * Verify if the session already exist
2021 * XXX: There is no need for the session lock list here since the caller
2022 * (process_client_msg) is holding it. We might want to change that so a
2023 * single command does not lock the entire session list.
2025 session
= session_find_by_name(name
);
2026 if (session
!= NULL
) {
2027 ret
= LTTNG_ERR_EXIST_SESS
;
2031 /* Create tracing session in the registry */
2032 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2033 LTTNG_SOCK_GET_GID_CRED(creds
));
2034 if (ret
!= LTTNG_OK
) {
2039 * Get the newly created session pointer back
2041 * XXX: There is no need for the session lock list here since the caller
2042 * (process_client_msg) is holding it. We might want to change that so a
2043 * single command does not lock the entire session list.
2045 session
= session_find_by_name(name
);
2048 session
->live_timer
= live_timer
;
2049 /* Create default consumer output for the session not yet created. */
2050 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2051 if (session
->consumer
== NULL
) {
2052 ret
= LTTNG_ERR_FATAL
;
2053 goto consumer_error
;
2057 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
2058 if (ret
!= LTTNG_OK
) {
2059 goto consumer_error
;
2061 session
->output_traces
= 1;
2063 session
->output_traces
= 0;
2064 DBG2("Session %s created with no output", session
->name
);
2067 session
->consumer
->enabled
= 1;
2072 session_destroy(session
);
2079 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2081 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2082 size_t nb_uri
, lttng_sock_cred
*creds
)
2085 struct ltt_session
*session
;
2086 struct snapshot_output
*new_output
= NULL
;
2092 * Create session in no output mode with URIs set to NULL. The uris we've
2093 * received are for a default snapshot output if one.
2095 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2096 if (ret
!= LTTNG_OK
) {
2100 /* Get the newly created session pointer back. This should NEVER fail. */
2101 session
= session_find_by_name(name
);
2104 /* Flag session for snapshot mode. */
2105 session
->snapshot_mode
= 1;
2107 /* Skip snapshot output creation if no URI is given. */
2112 new_output
= snapshot_output_alloc();
2114 ret
= LTTNG_ERR_NOMEM
;
2115 goto error_snapshot_alloc
;
2118 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2119 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2121 if (ret
== -ENOMEM
) {
2122 ret
= LTTNG_ERR_NOMEM
;
2124 ret
= LTTNG_ERR_INVALID
;
2126 goto error_snapshot
;
2130 snapshot_add_output(&session
->snapshot
, new_output
);
2137 snapshot_output_destroy(new_output
);
2138 error_snapshot_alloc
:
2139 session_destroy(session
);
2145 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2147 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2150 struct ltt_ust_session
*usess
;
2151 struct ltt_kernel_session
*ksess
;
2156 usess
= session
->ust_session
;
2157 ksess
= session
->kernel_session
;
2159 /* Clean kernel session teardown */
2160 kernel_destroy_session(ksess
);
2162 /* UST session teardown */
2164 /* Close any relayd session */
2165 consumer_output_send_destroy_relayd(usess
->consumer
);
2167 /* Destroy every UST application related to this session. */
2168 ret
= ust_app_destroy_trace_all(usess
);
2170 ERR("Error in ust_app_destroy_trace_all");
2173 /* Clean up the rest. */
2174 trace_ust_destroy_session(usess
);
2178 * Must notify the kernel thread here to update it's poll set in order to
2179 * remove the channel(s)' fd just destroyed.
2181 ret
= notify_thread_pipe(wpipe
);
2183 PERROR("write kernel poll pipe");
2186 ret
= session_destroy(session
);
2192 * Command LTTNG_CALIBRATE processed by the client thread.
2194 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2199 case LTTNG_DOMAIN_KERNEL
:
2201 struct lttng_kernel_calibrate kcalibrate
;
2203 switch (calibrate
->type
) {
2204 case LTTNG_CALIBRATE_FUNCTION
:
2206 /* Default and only possible calibrate option. */
2207 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2211 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2213 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2218 case LTTNG_DOMAIN_UST
:
2220 struct lttng_ust_calibrate ucalibrate
;
2222 switch (calibrate
->type
) {
2223 case LTTNG_CALIBRATE_FUNCTION
:
2225 /* Default and only possible calibrate option. */
2226 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2230 ret
= ust_app_calibrate_glb(&ucalibrate
);
2232 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2238 ret
= LTTNG_ERR_UND
;
2249 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2251 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2252 const char *sock_path
, struct consumer_data
*cdata
)
2255 struct consumer_socket
*socket
= NULL
;
2262 case LTTNG_DOMAIN_KERNEL
:
2264 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2268 /* Can't register a consumer if there is already one */
2269 if (ksess
->consumer_fds_sent
!= 0) {
2270 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2274 sock
= lttcomm_connect_unix_sock(sock_path
);
2276 ret
= LTTNG_ERR_CONNECT_FAIL
;
2279 cdata
->cmd_sock
= sock
;
2281 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2282 if (socket
== NULL
) {
2285 PERROR("close register consumer");
2287 cdata
->cmd_sock
= -1;
2288 ret
= LTTNG_ERR_FATAL
;
2292 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2293 if (socket
->lock
== NULL
) {
2294 PERROR("zmalloc pthread mutex");
2295 ret
= LTTNG_ERR_FATAL
;
2298 pthread_mutex_init(socket
->lock
, NULL
);
2299 socket
->registered
= 1;
2302 consumer_add_socket(socket
, ksess
->consumer
);
2305 pthread_mutex_lock(&cdata
->pid_mutex
);
2307 pthread_mutex_unlock(&cdata
->pid_mutex
);
2312 /* TODO: Userspace tracing */
2313 ret
= LTTNG_ERR_UND
;
2321 consumer_destroy_socket(socket
);
2327 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2329 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2330 struct lttng_domain
**domains
)
2335 if (session
->kernel_session
!= NULL
) {
2336 DBG3("Listing domains found kernel domain");
2340 if (session
->ust_session
!= NULL
) {
2341 DBG3("Listing domains found UST global domain");
2344 if (session
->ust_session
->domain_jul
.being_used
) {
2349 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2350 if (*domains
== NULL
) {
2351 ret
= LTTNG_ERR_FATAL
;
2355 if (session
->kernel_session
!= NULL
) {
2356 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2360 if (session
->ust_session
!= NULL
) {
2361 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2362 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2365 if (session
->ust_session
->domain_jul
.being_used
) {
2366 (*domains
)[index
].type
= LTTNG_DOMAIN_JUL
;
2367 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2375 /* Return negative value to differentiate return code */
2381 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2383 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2384 struct lttng_channel
**channels
)
2387 ssize_t nb_chan
= 0;
2390 case LTTNG_DOMAIN_KERNEL
:
2391 if (session
->kernel_session
!= NULL
) {
2392 nb_chan
= session
->kernel_session
->channel_count
;
2394 DBG3("Number of kernel channels %zd", nb_chan
);
2396 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2399 case LTTNG_DOMAIN_UST
:
2400 if (session
->ust_session
!= NULL
) {
2402 nb_chan
= lttng_ht_get_count(
2403 session
->ust_session
->domain_global
.channels
);
2406 DBG3("Number of UST global channels %zd", nb_chan
);
2408 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2413 ret
= LTTNG_ERR_UND
;
2418 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2419 if (*channels
== NULL
) {
2420 ret
= LTTNG_ERR_FATAL
;
2424 list_lttng_channels(domain
, session
, *channels
);
2430 /* Return negative value to differentiate return code */
2435 * Command LTTNG_LIST_EVENTS processed by the client thread.
2437 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2438 char *channel_name
, struct lttng_event
**events
)
2441 ssize_t nb_event
= 0;
2444 case LTTNG_DOMAIN_KERNEL
:
2445 if (session
->kernel_session
!= NULL
) {
2446 nb_event
= list_lttng_kernel_events(channel_name
,
2447 session
->kernel_session
, events
);
2450 case LTTNG_DOMAIN_UST
:
2452 if (session
->ust_session
!= NULL
) {
2453 nb_event
= list_lttng_ust_global_events(channel_name
,
2454 &session
->ust_session
->domain_global
, events
);
2458 case LTTNG_DOMAIN_JUL
:
2459 if (session
->ust_session
) {
2460 nb_event
= list_lttng_jul_events(
2461 &session
->ust_session
->domain_jul
, events
);
2465 ret
= LTTNG_ERR_UND
;
2472 /* Return negative value to differentiate return code */
2477 * Using the session list, filled a lttng_session array to send back to the
2478 * client for session listing.
2480 * The session list lock MUST be acquired before calling this function. Use
2481 * session_lock_list() and session_unlock_list().
2483 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2488 struct ltt_session
*session
;
2489 struct ltt_session_list
*list
= session_get_list();
2491 DBG("Getting all available session for UID %d GID %d",
2494 * Iterate over session list and append data after the control struct in
2497 cds_list_for_each_entry(session
, &list
->head
, list
) {
2499 * Only list the sessions the user can control.
2501 if (!session_access_ok(session
, uid
, gid
)) {
2505 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2506 struct ltt_ust_session
*usess
= session
->ust_session
;
2508 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2509 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2510 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2511 ret
= build_network_session_path(sessions
[i
].path
,
2512 sizeof(sessions
[i
].path
), session
);
2514 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2515 session
->consumer
->dst
.trace_path
);
2518 PERROR("snprintf session path");
2522 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2523 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2524 sessions
[i
].enabled
= session
->active
;
2525 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2526 sessions
[i
].live_timer_interval
= session
->live_timer
;
2532 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2533 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2535 int cmd_data_pending(struct ltt_session
*session
)
2538 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2539 struct ltt_ust_session
*usess
= session
->ust_session
;
2543 /* Session MUST be stopped to ask for data availability. */
2544 if (session
->active
) {
2545 ret
= LTTNG_ERR_SESSION_STARTED
;
2549 * If stopped, just make sure we've started before else the above call
2550 * will always send that there is data pending.
2552 * The consumer assumes that when the data pending command is received,
2553 * the trace has been started before or else no output data is written
2554 * by the streams which is a condition for data pending. So, this is
2555 * *VERY* important that we don't ask the consumer before a start
2558 if (!session
->has_been_started
) {
2564 if (ksess
&& ksess
->consumer
) {
2565 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2567 /* Data is still being extracted for the kernel. */
2572 if (usess
&& usess
->consumer
) {
2573 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2575 /* Data is still being extracted for the kernel. */
2580 /* Data is ready to be read by a viewer */
2588 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2590 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2592 int cmd_snapshot_add_output(struct ltt_session
*session
,
2593 struct lttng_snapshot_output
*output
, uint32_t *id
)
2596 struct snapshot_output
*new_output
;
2601 DBG("Cmd snapshot add output for session %s", session
->name
);
2604 * Permission denied to create an output if the session is not
2605 * set in no output mode.
2607 if (session
->output_traces
) {
2608 ret
= LTTNG_ERR_EPERM
;
2612 /* Only one output is allowed until we have the "tee" feature. */
2613 if (session
->snapshot
.nb_output
== 1) {
2614 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2618 new_output
= snapshot_output_alloc();
2620 ret
= LTTNG_ERR_NOMEM
;
2624 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2625 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2626 &session
->snapshot
);
2628 if (ret
== -ENOMEM
) {
2629 ret
= LTTNG_ERR_NOMEM
;
2631 ret
= LTTNG_ERR_INVALID
;
2637 snapshot_add_output(&session
->snapshot
, new_output
);
2639 *id
= new_output
->id
;
2646 snapshot_output_destroy(new_output
);
2652 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2654 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2656 int cmd_snapshot_del_output(struct ltt_session
*session
,
2657 struct lttng_snapshot_output
*output
)
2660 struct snapshot_output
*sout
= NULL
;
2668 * Permission denied to create an output if the session is not
2669 * set in no output mode.
2671 if (session
->output_traces
) {
2672 ret
= LTTNG_ERR_EPERM
;
2677 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2679 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2680 } else if (*output
->name
!= '\0') {
2681 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2683 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2686 ret
= LTTNG_ERR_INVALID
;
2690 snapshot_delete_output(&session
->snapshot
, sout
);
2691 snapshot_output_destroy(sout
);
2700 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2702 * If no output is available, outputs is untouched and 0 is returned.
2704 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2706 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2707 struct lttng_snapshot_output
**outputs
)
2710 struct lttng_snapshot_output
*list
= NULL
;
2711 struct lttng_ht_iter iter
;
2712 struct snapshot_output
*output
;
2717 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2720 * Permission denied to create an output if the session is not
2721 * set in no output mode.
2723 if (session
->output_traces
) {
2724 ret
= -LTTNG_ERR_EPERM
;
2728 if (session
->snapshot
.nb_output
== 0) {
2733 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2735 ret
= -LTTNG_ERR_NOMEM
;
2739 /* Copy list from session to the new list object. */
2741 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2742 output
, node
.node
) {
2743 assert(output
->consumer
);
2744 list
[idx
].id
= output
->id
;
2745 list
[idx
].max_size
= output
->max_size
;
2746 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2747 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2748 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2749 sizeof(list
[idx
].ctrl_url
));
2752 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2753 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2755 ret
= -LTTNG_ERR_NOMEM
;
2760 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2761 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2763 ret
= -LTTNG_ERR_NOMEM
;
2772 ret
= session
->snapshot
.nb_output
;
2780 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2781 * snapshot output is *not* set with a remote destination.
2783 * Return 0 on success or a LTTNG_ERR code.
2785 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2786 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2789 struct lttng_ht_iter iter
;
2790 struct consumer_socket
*socket
;
2793 assert(snap_output
);
2796 DBG2("Set relayd object from snapshot output");
2798 /* Ignore if snapshot consumer output is not network. */
2799 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2804 * For each consumer socket, create and send the relayd object of the
2808 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2809 socket
, node
.node
) {
2810 ret
= send_consumer_relayd_sockets(0, session
->id
,
2811 snap_output
->consumer
, socket
,
2812 session
->name
, session
->hostname
,
2813 session
->live_timer
);
2814 if (ret
!= LTTNG_OK
) {
2826 * Record a kernel snapshot.
2828 * Return LTTNG_OK on success or a LTTNG_ERR code.
2830 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2831 struct snapshot_output
*output
, struct ltt_session
*session
,
2832 int wait
, uint64_t nb_packets_per_stream
)
2840 /* Get the datetime for the snapshot output directory. */
2841 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2842 sizeof(output
->datetime
));
2844 ret
= LTTNG_ERR_INVALID
;
2849 * Copy kernel session sockets so we can communicate with the right
2850 * consumer for the snapshot record command.
2852 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2854 ret
= LTTNG_ERR_NOMEM
;
2858 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2859 if (ret
!= LTTNG_OK
) {
2860 goto error_snapshot
;
2863 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
2864 if (ret
!= LTTNG_OK
) {
2865 goto error_snapshot
;
2872 /* Clean up copied sockets so this output can use some other later on. */
2873 consumer_destroy_output_sockets(output
->consumer
);
2880 * Record a UST snapshot.
2882 * Return 0 on success or a LTTNG_ERR error code.
2884 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2885 struct snapshot_output
*output
, struct ltt_session
*session
,
2886 int wait
, uint64_t nb_packets_per_stream
)
2894 /* Get the datetime for the snapshot output directory. */
2895 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2896 sizeof(output
->datetime
));
2898 ret
= LTTNG_ERR_INVALID
;
2903 * Copy UST session sockets so we can communicate with the right
2904 * consumer for the snapshot record command.
2906 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
2908 ret
= LTTNG_ERR_NOMEM
;
2912 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
2913 if (ret
!= LTTNG_OK
) {
2914 goto error_snapshot
;
2917 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
2921 ret
= LTTNG_ERR_INVALID
;
2924 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
2927 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
2930 goto error_snapshot
;
2936 /* Clean up copied sockets so this output can use some other later on. */
2937 consumer_destroy_output_sockets(output
->consumer
);
2943 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
2944 uint64_t cur_nr_packets
)
2946 uint64_t tot_size
= 0;
2948 if (session
->kernel_session
) {
2949 struct ltt_kernel_channel
*chan
;
2950 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2952 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
2953 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
2955 * Don't take channel into account if we
2956 * already grab all its packets.
2960 tot_size
+= chan
->channel
->attr
.subbuf_size
2961 * chan
->stream_count
;
2965 if (session
->ust_session
) {
2966 struct ltt_ust_session
*usess
= session
->ust_session
;
2968 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
2976 * Calculate the number of packets we can grab from each stream that
2977 * fits within the overall snapshot max size.
2979 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
2980 * the number of packets per stream.
2982 * TODO: this approach is not perfect: we consider the worse case
2983 * (packet filling the sub-buffers) as an upper bound, but we could do
2984 * better if we do this calculation while we actually grab the packet
2985 * content: we would know how much padding we don't actually store into
2988 * This algorithm is currently bounded by the number of packets per
2991 * Since we call this algorithm before actually grabbing the data, it's
2992 * an approximation: for instance, applications could appear/disappear
2993 * in between this call and actually grabbing data.
2996 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
2999 uint64_t cur_nb_packets
= 0;
3002 return 0; /* Infinite */
3005 size_left
= max_size
;
3007 uint64_t one_more_packet_tot_size
;
3009 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3011 if (!one_more_packet_tot_size
) {
3012 /* We are already grabbing all packets. */
3015 size_left
-= one_more_packet_tot_size
;
3016 if (size_left
< 0) {
3021 if (!cur_nb_packets
) {
3022 /* Not enough room to grab one packet of each stream, error. */
3025 return cur_nb_packets
;
3029 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3031 * The wait parameter is ignored so this call always wait for the snapshot to
3032 * complete before returning.
3034 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3036 int cmd_snapshot_record(struct ltt_session
*session
,
3037 struct lttng_snapshot_output
*output
, int wait
)
3040 unsigned int use_tmp_output
= 0;
3041 struct snapshot_output tmp_output
;
3042 unsigned int snapshot_success
= 0;
3046 DBG("Cmd snapshot record for session %s", session
->name
);
3049 * Permission denied to create an output if the session is not
3050 * set in no output mode.
3052 if (session
->output_traces
) {
3053 ret
= LTTNG_ERR_EPERM
;
3057 /* The session needs to be started at least once. */
3058 if (!session
->has_been_started
) {
3059 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3063 /* Use temporary output for the session. */
3064 if (output
&& *output
->ctrl_url
!= '\0') {
3065 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3066 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3069 if (ret
== -ENOMEM
) {
3070 ret
= LTTNG_ERR_NOMEM
;
3072 ret
= LTTNG_ERR_INVALID
;
3076 /* Use the global session count for the temporary snapshot. */
3077 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3081 if (session
->kernel_session
) {
3082 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3084 if (use_tmp_output
) {
3085 int64_t nb_packets_per_stream
;
3087 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3088 tmp_output
.max_size
);
3089 if (nb_packets_per_stream
< 0) {
3090 ret
= LTTNG_ERR_INVALID
;
3093 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3094 wait
, nb_packets_per_stream
);
3095 if (ret
!= LTTNG_OK
) {
3098 snapshot_success
= 1;
3100 struct snapshot_output
*sout
;
3101 struct lttng_ht_iter iter
;
3104 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3105 &iter
.iter
, sout
, node
.node
) {
3106 int64_t nb_packets_per_stream
;
3109 * Make a local copy of the output and assign the possible
3110 * temporary value given by the caller.
3112 memset(&tmp_output
, 0, sizeof(tmp_output
));
3113 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3115 if (output
->max_size
!= (uint64_t) -1ULL) {
3116 tmp_output
.max_size
= output
->max_size
;
3119 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3120 tmp_output
.max_size
);
3121 if (nb_packets_per_stream
< 0) {
3122 ret
= LTTNG_ERR_INVALID
;
3126 /* Use temporary name. */
3127 if (*output
->name
!= '\0') {
3128 strncpy(tmp_output
.name
, output
->name
,
3129 sizeof(tmp_output
.name
));
3132 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3134 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3135 session
, wait
, nb_packets_per_stream
);
3136 if (ret
!= LTTNG_OK
) {
3140 snapshot_success
= 1;
3146 if (session
->ust_session
) {
3147 struct ltt_ust_session
*usess
= session
->ust_session
;
3149 if (use_tmp_output
) {
3150 int64_t nb_packets_per_stream
;
3152 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3153 tmp_output
.max_size
);
3154 if (nb_packets_per_stream
< 0) {
3155 ret
= LTTNG_ERR_INVALID
;
3158 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3159 wait
, nb_packets_per_stream
);
3160 if (ret
!= LTTNG_OK
) {
3163 snapshot_success
= 1;
3165 struct snapshot_output
*sout
;
3166 struct lttng_ht_iter iter
;
3169 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3170 &iter
.iter
, sout
, node
.node
) {
3171 int64_t nb_packets_per_stream
;
3174 * Make a local copy of the output and assign the possible
3175 * temporary value given by the caller.
3177 memset(&tmp_output
, 0, sizeof(tmp_output
));
3178 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3180 if (output
->max_size
!= (uint64_t) -1ULL) {
3181 tmp_output
.max_size
= output
->max_size
;
3184 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3185 tmp_output
.max_size
);
3186 if (nb_packets_per_stream
< 0) {
3187 ret
= LTTNG_ERR_INVALID
;
3192 /* Use temporary name. */
3193 if (*output
->name
!= '\0') {
3194 strncpy(tmp_output
.name
, output
->name
,
3195 sizeof(tmp_output
.name
));
3198 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3200 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3201 wait
, nb_packets_per_stream
);
3202 if (ret
!= LTTNG_OK
) {
3206 snapshot_success
= 1;
3212 if (snapshot_success
) {
3213 session
->snapshot
.nb_snapshot
++;
3215 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3223 * Init command subsystem.
3228 * Set network sequence index to 1 for streams to match a relayd
3229 * socket on the consumer side.
3231 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3232 relayd_net_seq_idx
= 1;
3233 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3235 DBG("Command subsystem initialized");