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"
44 * Used to keep a unique index for each relayd socket created where this value
45 * is associated with streams on the consumer so it can match the right relayd
46 * to send to. It must be accessed with the relayd_net_seq_idx_lock
49 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
50 static uint64_t relayd_net_seq_idx
;
53 * Both functions below are special case for the Kernel domain when
54 * enabling/disabling all events.
57 int enable_kevent_all(struct ltt_session
*session
,
58 struct lttng_domain
*domain
, char *channel_name
,
59 struct lttng_event
*event
,
60 char *filter_expression
,
61 struct lttng_filter_bytecode
*filter
, int wpipe
);
63 int disable_kevent_all(struct ltt_session
*session
, int domain
,
65 struct lttng_event
*event
);
68 * Create a session path used by list_lttng_sessions for the case that the
69 * session consumer is on the network.
71 static int build_network_session_path(char *dst
, size_t size
,
72 struct ltt_session
*session
)
74 int ret
, kdata_port
, udata_port
;
75 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
76 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
81 memset(tmp_urls
, 0, sizeof(tmp_urls
));
82 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
84 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
86 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
87 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
88 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
91 if (session
->ust_session
&& session
->ust_session
->consumer
) {
92 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
93 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
96 if (uuri
== NULL
&& kuri
== NULL
) {
97 uri
= &session
->consumer
->dst
.net
.control
;
98 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
99 } else if (kuri
&& uuri
) {
100 ret
= uri_compare(kuri
, uuri
);
104 /* Build uuri URL string */
105 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
112 } else if (kuri
&& uuri
== NULL
) {
114 } else if (uuri
&& kuri
== NULL
) {
118 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
124 * Do we have a UST url set. If yes, this means we have both kernel and UST
127 if (*tmp_uurl
!= '\0') {
128 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
129 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
132 if (kuri
|| (!kuri
&& !uuri
)) {
135 /* No kernel URI, use the UST port. */
138 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
146 * Fill lttng_channel array of all channels.
148 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
149 struct lttng_channel
*channels
)
152 struct ltt_kernel_channel
*kchan
;
154 DBG("Listing channels for session %s", session
->name
);
157 case LTTNG_DOMAIN_KERNEL
:
158 /* Kernel channels */
159 if (session
->kernel_session
!= NULL
) {
160 cds_list_for_each_entry(kchan
,
161 &session
->kernel_session
->channel_list
.head
, list
) {
162 /* Copy lttng_channel struct to array */
163 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
164 channels
[i
].enabled
= kchan
->enabled
;
169 case LTTNG_DOMAIN_UST
:
171 struct lttng_ht_iter iter
;
172 struct ltt_ust_channel
*uchan
;
175 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
176 &iter
.iter
, uchan
, node
.node
) {
177 if (lttng_strncpy(channels
[i
].name
, uchan
->name
,
178 LTTNG_SYMBOL_NAME_LEN
)) {
181 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
182 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
183 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
184 channels
[i
].attr
.switch_timer_interval
=
185 uchan
->attr
.switch_timer_interval
;
186 channels
[i
].attr
.read_timer_interval
=
187 uchan
->attr
.read_timer_interval
;
188 channels
[i
].enabled
= uchan
->enabled
;
189 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
190 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
191 switch (uchan
->attr
.output
) {
194 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
208 * Create a list of agent domain events.
210 * Return number of events in list on success or else a negative value.
212 static int list_lttng_agent_events(struct agent
*agt
,
213 struct lttng_event
**events
)
216 unsigned int nb_event
= 0;
217 struct agent_event
*event
;
218 struct lttng_event
*tmp_events
;
219 struct lttng_ht_iter iter
;
224 DBG3("Listing agent events");
227 nb_event
= lttng_ht_get_count(agt
->events
);
234 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
236 PERROR("zmalloc agent events session");
237 ret
= -LTTNG_ERR_FATAL
;
242 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
243 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
244 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
245 tmp_events
[i
].enabled
= event
->enabled
;
246 tmp_events
[i
].loglevel
= event
->loglevel
;
247 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
252 *events
= tmp_events
;
256 assert(nb_event
== i
);
261 * Create a list of ust global domain events.
263 static int list_lttng_ust_global_events(char *channel_name
,
264 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
267 unsigned int nb_event
= 0;
268 struct lttng_ht_iter iter
;
269 struct lttng_ht_node_str
*node
;
270 struct ltt_ust_channel
*uchan
;
271 struct ltt_ust_event
*uevent
;
272 struct lttng_event
*tmp
;
274 DBG("Listing UST global events for channel %s", channel_name
);
278 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
279 node
= lttng_ht_iter_get_node_str(&iter
);
281 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
285 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
287 nb_event
+= lttng_ht_get_count(uchan
->events
);
294 DBG3("Listing UST global %d events", nb_event
);
296 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
298 ret
= LTTNG_ERR_FATAL
;
302 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
303 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
304 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
305 tmp
[i
].enabled
= uevent
->enabled
;
307 switch (uevent
->attr
.instrumentation
) {
308 case LTTNG_UST_TRACEPOINT
:
309 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
311 case LTTNG_UST_PROBE
:
312 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
314 case LTTNG_UST_FUNCTION
:
315 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
319 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
320 switch (uevent
->attr
.loglevel_type
) {
321 case LTTNG_UST_LOGLEVEL_ALL
:
322 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
324 case LTTNG_UST_LOGLEVEL_RANGE
:
325 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
327 case LTTNG_UST_LOGLEVEL_SINGLE
:
328 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
331 if (uevent
->filter
) {
334 if (uevent
->exclusion
) {
335 tmp
[i
].exclusion
= 1;
349 * Fill lttng_event array of all kernel events in the channel.
351 static int list_lttng_kernel_events(char *channel_name
,
352 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
355 unsigned int nb_event
;
356 struct ltt_kernel_event
*event
;
357 struct ltt_kernel_channel
*kchan
;
359 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
361 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
365 nb_event
= kchan
->event_count
;
367 DBG("Listing events for channel %s", kchan
->channel
->name
);
374 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
375 if (*events
== NULL
) {
376 ret
= LTTNG_ERR_FATAL
;
380 /* Kernel channels */
381 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
382 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
383 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
384 (*events
)[i
].enabled
= event
->enabled
;
386 switch (event
->event
->instrumentation
) {
387 case LTTNG_KERNEL_TRACEPOINT
:
388 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
390 case LTTNG_KERNEL_KRETPROBE
:
391 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
392 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
393 sizeof(struct lttng_kernel_kprobe
));
395 case LTTNG_KERNEL_KPROBE
:
396 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
397 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
398 sizeof(struct lttng_kernel_kprobe
));
400 case LTTNG_KERNEL_FUNCTION
:
401 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
402 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
403 sizeof(struct lttng_kernel_function
));
405 case LTTNG_KERNEL_NOOP
:
406 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
408 case LTTNG_KERNEL_SYSCALL
:
409 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
411 case LTTNG_KERNEL_ALL
:
422 new_size
= syscall_list_channel(kchan
, events
, nb_event
);
434 /* Negate the error code to differentiate the size from an error */
439 * Add URI so the consumer output object. Set the correct path depending on the
440 * domain adding the default trace directory.
442 static int add_uri_to_consumer(struct consumer_output
*consumer
,
443 struct lttng_uri
*uri
, int domain
, const char *session_name
)
446 const char *default_trace_dir
;
450 if (consumer
== NULL
) {
451 DBG("No consumer detected. Don't add URI. Stopping.");
452 ret
= LTTNG_ERR_NO_CONSUMER
;
457 case LTTNG_DOMAIN_KERNEL
:
458 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
460 case LTTNG_DOMAIN_UST
:
461 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
465 * This case is possible is we try to add the URI to the global tracing
466 * session consumer object which in this case there is no subdir.
468 default_trace_dir
= "";
471 switch (uri
->dtype
) {
474 DBG2("Setting network URI to consumer");
476 if (consumer
->type
== CONSUMER_DST_NET
) {
477 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
478 consumer
->dst
.net
.control_isset
) ||
479 (uri
->stype
== LTTNG_STREAM_DATA
&&
480 consumer
->dst
.net
.data_isset
)) {
481 ret
= LTTNG_ERR_URL_EXIST
;
485 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
488 consumer
->type
= CONSUMER_DST_NET
;
490 /* Set URI into consumer output object */
491 ret
= consumer_set_network_uri(consumer
, uri
);
495 } else if (ret
== 1) {
497 * URI was the same in the consumer so we do not append the subdir
498 * again so to not duplicate output dir.
504 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
505 ret
= consumer_set_subdir(consumer
, session_name
);
507 ret
= LTTNG_ERR_FATAL
;
512 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
513 /* On a new subdir, reappend the default trace dir. */
514 strncat(consumer
->subdir
, default_trace_dir
,
515 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
516 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
521 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
522 memset(consumer
->dst
.trace_path
, 0,
523 sizeof(consumer
->dst
.trace_path
));
524 /* Explicit length checks for strcpy and strcat. */
525 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
526 >= sizeof(consumer
->dst
.trace_path
)) {
527 ret
= LTTNG_ERR_FATAL
;
530 strcpy(consumer
->dst
.trace_path
, uri
->dst
.path
);
531 /* Append default trace dir */
532 strcat(consumer
->dst
.trace_path
, default_trace_dir
);
533 /* Flag consumer as local. */
534 consumer
->type
= CONSUMER_DST_LOCAL
;
545 * Init tracing by creating trace directory and sending fds kernel consumer.
547 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
550 struct lttng_ht_iter iter
;
551 struct consumer_socket
*socket
;
557 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
558 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
560 pthread_mutex_lock(socket
->lock
);
561 ret
= kernel_consumer_send_session(socket
, session
);
562 pthread_mutex_unlock(socket
->lock
);
564 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
576 * Create a socket to the relayd using the URI.
578 * On success, the relayd_sock pointer is set to the created socket.
579 * Else, it's stays untouched and a lttcomm error code is returned.
581 static int create_connect_relayd(struct lttng_uri
*uri
,
582 struct lttcomm_relayd_sock
**relayd_sock
)
585 struct lttcomm_relayd_sock
*rsock
;
587 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
588 RELAYD_VERSION_COMM_MINOR
);
590 ret
= LTTNG_ERR_FATAL
;
595 * Connect to relayd so we can proceed with a session creation. This call
596 * can possibly block for an arbitrary amount of time to set the health
597 * state to be in poll execution.
600 ret
= relayd_connect(rsock
);
603 ERR("Unable to reach lttng-relayd");
604 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
608 /* Create socket for control stream. */
609 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
610 DBG3("Creating relayd stream socket from URI");
612 /* Check relayd version */
613 ret
= relayd_version_check(rsock
);
615 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
618 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
619 DBG3("Creating relayd data socket from URI");
621 /* Command is not valid */
622 ERR("Relayd invalid stream type: %d", uri
->stype
);
623 ret
= LTTNG_ERR_INVALID
;
627 *relayd_sock
= rsock
;
632 /* The returned value is not useful since we are on an error path. */
633 (void) relayd_close(rsock
);
641 * Connect to the relayd using URI and send the socket to the right consumer.
643 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
644 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
645 struct consumer_socket
*consumer_sock
,
646 char *session_name
, char *hostname
, int session_live_timer
)
649 struct lttcomm_relayd_sock
*rsock
= NULL
;
651 /* Connect to relayd and make version check if uri is the control. */
652 ret
= create_connect_relayd(relayd_uri
, &rsock
);
653 if (ret
!= LTTNG_OK
) {
658 /* Set the network sequence index if not set. */
659 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
660 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
662 * Increment net_seq_idx because we are about to transfer the
663 * new relayd socket to the consumer.
664 * Assign unique key so the consumer can match streams.
666 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
667 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
670 /* Send relayd socket to consumer. */
671 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
672 relayd_uri
->stype
, session_id
,
673 session_name
, hostname
, session_live_timer
);
675 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
679 /* Flag that the corresponding socket was sent. */
680 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
681 consumer_sock
->control_sock_sent
= 1;
682 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
683 consumer_sock
->data_sock_sent
= 1;
689 * Close socket which was dup on the consumer side. The session daemon does
690 * NOT keep track of the relayd socket(s) once transfer to the consumer.
694 (void) relayd_close(rsock
);
698 if (ret
!= LTTNG_OK
) {
700 * The consumer output for this session should not be used anymore
701 * since the relayd connection failed thus making any tracing or/and
702 * streaming not usable.
704 consumer
->enabled
= 0;
710 * Send both relayd sockets to a specific consumer and domain. This is a
711 * helper function to facilitate sending the information to the consumer for a
714 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
715 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
716 char *session_name
, char *hostname
, int session_live_timer
)
723 /* Sending control relayd socket. */
724 if (!sock
->control_sock_sent
) {
725 ret
= send_consumer_relayd_socket(domain
, session_id
,
726 &consumer
->dst
.net
.control
, consumer
, sock
,
727 session_name
, hostname
, session_live_timer
);
728 if (ret
!= LTTNG_OK
) {
733 /* Sending data relayd socket. */
734 if (!sock
->data_sock_sent
) {
735 ret
= send_consumer_relayd_socket(domain
, session_id
,
736 &consumer
->dst
.net
.data
, consumer
, sock
,
737 session_name
, hostname
, session_live_timer
);
738 if (ret
!= LTTNG_OK
) {
748 * Setup relayd connections for a tracing session. First creates the socket to
749 * the relayd and send them to the right domain consumer. Consumer type MUST be
752 int cmd_setup_relayd(struct ltt_session
*session
)
755 struct ltt_ust_session
*usess
;
756 struct ltt_kernel_session
*ksess
;
757 struct consumer_socket
*socket
;
758 struct lttng_ht_iter iter
;
762 usess
= session
->ust_session
;
763 ksess
= session
->kernel_session
;
765 DBG("Setting relayd for session %s", session
->name
);
769 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
770 && usess
->consumer
->enabled
) {
771 /* For each consumer socket, send relayd sockets */
772 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
774 pthread_mutex_lock(socket
->lock
);
775 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
776 usess
->consumer
, socket
,
777 session
->name
, session
->hostname
,
778 session
->live_timer
);
779 pthread_mutex_unlock(socket
->lock
);
780 if (ret
!= LTTNG_OK
) {
783 /* Session is now ready for network streaming. */
784 session
->net_handle
= 1;
788 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
789 && ksess
->consumer
->enabled
) {
790 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
792 pthread_mutex_lock(socket
->lock
);
793 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
794 ksess
->consumer
, socket
,
795 session
->name
, session
->hostname
,
796 session
->live_timer
);
797 pthread_mutex_unlock(socket
->lock
);
798 if (ret
!= LTTNG_OK
) {
801 /* Session is now ready for network streaming. */
802 session
->net_handle
= 1;
812 * Start a kernel session by opening all necessary streams.
814 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
817 struct ltt_kernel_channel
*kchan
;
819 /* Open kernel metadata */
820 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
821 ret
= kernel_open_metadata(ksess
);
823 ret
= LTTNG_ERR_KERN_META_FAIL
;
828 /* Open kernel metadata stream */
829 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
830 ret
= kernel_open_metadata_stream(ksess
);
832 ERR("Kernel create metadata stream failed");
833 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
838 /* For each channel */
839 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
840 if (kchan
->stream_count
== 0) {
841 ret
= kernel_open_channel_stream(kchan
);
843 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
846 /* Update the stream global counter */
847 ksess
->stream_count_global
+= ret
;
851 /* Setup kernel consumer socket and send fds to it */
852 ret
= init_kernel_tracing(ksess
);
854 ret
= LTTNG_ERR_KERN_START_FAIL
;
858 /* This start the kernel tracing */
859 ret
= kernel_start_session(ksess
);
861 ret
= LTTNG_ERR_KERN_START_FAIL
;
865 /* Quiescent wait after starting trace */
866 kernel_wait_quiescent(kernel_tracer_fd
);
877 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
879 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
883 struct ltt_ust_session
*usess
;
885 usess
= session
->ust_session
;
890 case LTTNG_DOMAIN_KERNEL
:
892 ret
= channel_kernel_disable(session
->kernel_session
,
894 if (ret
!= LTTNG_OK
) {
898 kernel_wait_quiescent(kernel_tracer_fd
);
901 case LTTNG_DOMAIN_UST
:
903 struct ltt_ust_channel
*uchan
;
904 struct lttng_ht
*chan_ht
;
906 chan_ht
= usess
->domain_global
.channels
;
908 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
910 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
914 ret
= channel_ust_disable(usess
, uchan
);
915 if (ret
!= LTTNG_OK
) {
921 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
922 case LTTNG_DOMAIN_UST_EXEC_NAME
:
923 case LTTNG_DOMAIN_UST_PID
:
926 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
938 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
940 * The wpipe arguments is used as a notifier for the kernel thread.
942 int cmd_enable_channel(struct ltt_session
*session
,
943 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
946 struct ltt_ust_session
*usess
= session
->ust_session
;
947 struct lttng_ht
*chan_ht
;
954 len
= strnlen(attr
->name
, sizeof(attr
->name
));
956 /* Validate channel name */
957 if (attr
->name
[0] == '.' ||
958 memchr(attr
->name
, '/', len
) != NULL
) {
959 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
963 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
968 * Don't try to enable a channel if the session has been started at
969 * some point in time before. The tracer does not allow it.
971 if (session
->has_been_started
) {
972 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
977 * If the session is a live session, remove the switch timer, the
978 * live timer does the same thing but sends also synchronisation
979 * beacons for inactive streams.
981 if (session
->live_timer
> 0) {
982 attr
->attr
.live_timer_interval
= session
->live_timer
;
983 attr
->attr
.switch_timer_interval
= 0;
987 * The ringbuffer (both in user space and kernel) behave badly in overwrite
988 * mode and with less than 2 subbuf so block it right away and send back an
989 * invalid attribute error.
991 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
992 ret
= LTTNG_ERR_INVALID
;
996 switch (domain
->type
) {
997 case LTTNG_DOMAIN_KERNEL
:
999 struct ltt_kernel_channel
*kchan
;
1001 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1002 session
->kernel_session
);
1003 if (kchan
== NULL
) {
1004 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1005 if (attr
->name
[0] != '\0') {
1006 session
->kernel_session
->has_non_default_channel
= 1;
1009 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1012 if (ret
!= LTTNG_OK
) {
1016 kernel_wait_quiescent(kernel_tracer_fd
);
1019 case LTTNG_DOMAIN_UST
:
1021 struct ltt_ust_channel
*uchan
;
1023 chan_ht
= usess
->domain_global
.channels
;
1025 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1026 if (uchan
== NULL
) {
1027 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1028 if (attr
->name
[0] != '\0') {
1029 usess
->has_non_default_channel
= 1;
1032 ret
= channel_ust_enable(usess
, uchan
);
1037 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1049 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1051 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1053 struct lttng_event
*event
)
1058 DBG("Disable event command for event \'%s\'", event
->name
);
1060 event_name
= event
->name
;
1062 /* Error out on unhandled search criteria */
1063 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1064 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1065 return LTTNG_ERR_UNK
;
1067 /* Special handling for kernel domain all events. */
1068 if (domain
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event_name
, "*")) {
1069 return disable_kevent_all(session
, domain
, channel_name
, event
);
1075 case LTTNG_DOMAIN_KERNEL
:
1077 struct ltt_kernel_channel
*kchan
;
1078 struct ltt_kernel_session
*ksess
;
1080 ksess
= session
->kernel_session
;
1083 * If a non-default channel has been created in the
1084 * session, explicitely require that -c chan_name needs
1087 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1088 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1092 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1093 if (kchan
== NULL
) {
1094 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1098 switch (event
->type
) {
1099 case LTTNG_EVENT_ALL
:
1100 case LTTNG_EVENT_TRACEPOINT
:
1101 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1102 if (ret
!= LTTNG_OK
) {
1106 case LTTNG_EVENT_SYSCALL
:
1107 ret
= event_kernel_disable_syscall(kchan
, event_name
);
1108 if (ret
!= LTTNG_OK
) {
1113 ret
= LTTNG_ERR_UNK
;
1117 kernel_wait_quiescent(kernel_tracer_fd
);
1120 case LTTNG_DOMAIN_UST
:
1122 struct ltt_ust_channel
*uchan
;
1123 struct ltt_ust_session
*usess
;
1125 usess
= session
->ust_session
;
1128 * If a non-default channel has been created in the
1129 * session, explicitely require that -c chan_name needs
1132 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1133 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1137 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1139 if (uchan
== NULL
) {
1140 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1144 switch (event
->type
) {
1145 case LTTNG_EVENT_ALL
:
1146 if (strlen(event
->name
) == 1 &&
1147 !strncmp(event
->name
, "*", 1)) {
1148 ret
= event_ust_disable_all_tracepoints(usess
,
1151 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1154 if (ret
!= LTTNG_OK
) {
1159 ret
= LTTNG_ERR_UNK
;
1163 DBG3("Disable UST event %s in channel %s completed", event_name
,
1167 case LTTNG_DOMAIN_LOG4J
:
1168 case LTTNG_DOMAIN_JUL
:
1171 struct ltt_ust_session
*usess
= session
->ust_session
;
1175 switch (event
->type
) {
1176 case LTTNG_EVENT_ALL
:
1179 ret
= LTTNG_ERR_UNK
;
1183 agt
= trace_ust_find_agent(usess
, domain
);
1185 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1188 /* The wild card * means that everything should be disabled. */
1189 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1190 ret
= event_agent_disable_all(usess
, agt
);
1192 ret
= event_agent_disable(usess
, agt
, event_name
);
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_DISABLE_EVENT for event "*" processed by the client thread.
1221 int disable_kevent_all(struct ltt_session
*session
, int domain
,
1223 struct lttng_event
*event
)
1230 case LTTNG_DOMAIN_KERNEL
:
1232 struct ltt_kernel_session
*ksess
;
1233 struct ltt_kernel_channel
*kchan
;
1235 ksess
= session
->kernel_session
;
1238 * If a non-default channel has been created in the
1239 * session, explicitely require that -c chan_name needs
1242 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1243 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1247 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1248 if (kchan
== NULL
) {
1249 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1253 switch (event
->type
) {
1254 case LTTNG_EVENT_ALL
:
1255 ret
= event_kernel_disable_all(kchan
);
1256 if (ret
!= LTTNG_OK
) {
1260 case LTTNG_EVENT_SYSCALL
:
1261 ret
= event_kernel_disable_syscall(kchan
, "");
1262 if (ret
!= LTTNG_OK
) {
1267 ret
= LTTNG_ERR_UNK
;
1271 kernel_wait_quiescent(kernel_tracer_fd
);
1275 ret
= LTTNG_ERR_UND
;
1287 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1289 int cmd_add_context(struct ltt_session
*session
, int domain
,
1290 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1292 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1295 case LTTNG_DOMAIN_KERNEL
:
1296 assert(session
->kernel_session
);
1298 if (session
->kernel_session
->channel_count
== 0) {
1299 /* Create default channel */
1300 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1301 if (ret
!= LTTNG_OK
) {
1304 chan_kern_created
= 1;
1306 /* Add kernel context to kernel tracer */
1307 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1308 if (ret
!= LTTNG_OK
) {
1312 case LTTNG_DOMAIN_UST
:
1314 struct ltt_ust_session
*usess
= session
->ust_session
;
1315 unsigned int chan_count
;
1319 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1320 if (chan_count
== 0) {
1321 struct lttng_channel
*attr
;
1322 /* Create default channel */
1323 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1325 ret
= LTTNG_ERR_FATAL
;
1329 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1330 if (ret
!= LTTNG_OK
) {
1335 chan_ust_created
= 1;
1338 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1339 if (ret
!= LTTNG_OK
) {
1345 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1346 case LTTNG_DOMAIN_UST_PID
:
1347 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1350 ret
= LTTNG_ERR_UND
;
1357 if (chan_kern_created
) {
1358 struct ltt_kernel_channel
*kchan
=
1359 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1360 session
->kernel_session
);
1361 /* Created previously, this should NOT fail. */
1363 kernel_destroy_channel(kchan
);
1366 if (chan_ust_created
) {
1367 struct ltt_ust_channel
*uchan
=
1368 trace_ust_find_channel_by_name(
1369 session
->ust_session
->domain_global
.channels
,
1370 DEFAULT_CHANNEL_NAME
);
1371 /* Created previously, this should NOT fail. */
1373 /* Remove from the channel list of the session. */
1374 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1376 trace_ust_destroy_channel(uchan
);
1381 static int validate_event_name(const char *name
)
1384 const char *c
= name
;
1385 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1388 * Make sure that unescaped wildcards are only used as the last
1389 * character of the event name.
1391 while (c
< event_name_end
) {
1399 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1400 /* Wildcard is not the last character */
1401 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1414 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1415 * We own filter, exclusion, and filter_expression.
1417 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1418 char *channel_name
, struct lttng_event
*event
,
1419 char *filter_expression
,
1420 struct lttng_filter_bytecode
*filter
,
1421 struct lttng_event_exclusion
*exclusion
,
1424 int ret
, channel_created
= 0;
1425 struct lttng_channel
*attr
;
1429 assert(channel_name
);
1431 DBG("Enable event command for event \'%s\'", event
->name
);
1433 /* Special handling for kernel domain all events. */
1434 if (domain
->type
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event
->name
, "*")) {
1435 return enable_kevent_all(session
, domain
, channel_name
, event
,
1436 filter_expression
, filter
, wpipe
);
1439 ret
= validate_event_name(event
->name
);
1446 switch (domain
->type
) {
1447 case LTTNG_DOMAIN_KERNEL
:
1449 struct ltt_kernel_channel
*kchan
;
1452 * If a non-default channel has been created in the
1453 * session, explicitely require that -c chan_name needs
1456 if (session
->kernel_session
->has_non_default_channel
1457 && channel_name
[0] == '\0') {
1458 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1462 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1463 session
->kernel_session
);
1464 if (kchan
== NULL
) {
1465 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1466 LTTNG_BUFFER_GLOBAL
);
1468 ret
= LTTNG_ERR_FATAL
;
1471 if (lttng_strncpy(attr
->name
, channel_name
,
1472 sizeof(attr
->name
))) {
1473 ret
= LTTNG_ERR_INVALID
;
1478 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1479 if (ret
!= LTTNG_OK
) {
1485 channel_created
= 1;
1488 /* Get the newly created kernel channel pointer */
1489 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1490 session
->kernel_session
);
1491 if (kchan
== NULL
) {
1492 /* This sould not happen... */
1493 ret
= LTTNG_ERR_FATAL
;
1497 switch (event
->type
) {
1498 case LTTNG_EVENT_ALL
:
1499 case LTTNG_EVENT_PROBE
:
1500 case LTTNG_EVENT_FUNCTION
:
1501 case LTTNG_EVENT_FUNCTION_ENTRY
:
1502 case LTTNG_EVENT_TRACEPOINT
:
1503 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1504 if (ret
!= LTTNG_OK
) {
1505 if (channel_created
) {
1506 /* Let's not leak a useless channel. */
1507 kernel_destroy_channel(kchan
);
1512 case LTTNG_EVENT_SYSCALL
:
1513 ret
= event_kernel_enable_syscall(kchan
, event
->name
);
1514 if (ret
!= LTTNG_OK
) {
1519 ret
= LTTNG_ERR_UNK
;
1523 kernel_wait_quiescent(kernel_tracer_fd
);
1526 case LTTNG_DOMAIN_UST
:
1528 struct ltt_ust_channel
*uchan
;
1529 struct ltt_ust_session
*usess
= session
->ust_session
;
1534 * If a non-default channel has been created in the
1535 * session, explicitely require that -c chan_name needs
1538 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1539 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1543 /* Get channel from global UST domain */
1544 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1546 if (uchan
== NULL
) {
1547 /* Create default channel */
1548 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1549 usess
->buffer_type
);
1551 ret
= LTTNG_ERR_FATAL
;
1554 if (lttng_strncpy(attr
->name
, channel_name
,
1555 sizeof(attr
->name
))) {
1556 ret
= LTTNG_ERR_INVALID
;
1561 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1562 if (ret
!= LTTNG_OK
) {
1568 /* Get the newly created channel reference back */
1569 uchan
= trace_ust_find_channel_by_name(
1570 usess
->domain_global
.channels
, channel_name
);
1574 /* At this point, the session and channel exist on the tracer */
1575 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1576 filter_expression
, filter
, exclusion
);
1577 /* We have passed ownership */
1578 filter_expression
= NULL
;
1581 if (ret
!= LTTNG_OK
) {
1586 case LTTNG_DOMAIN_LOG4J
:
1587 case LTTNG_DOMAIN_JUL
:
1589 const char *default_event_name
, *default_chan_name
;
1591 struct lttng_event uevent
;
1592 struct lttng_domain tmp_dom
;
1593 struct ltt_ust_session
*usess
= session
->ust_session
;
1597 agt
= trace_ust_find_agent(usess
, domain
->type
);
1599 agt
= agent_create(domain
->type
);
1601 ret
= LTTNG_ERR_NOMEM
;
1604 agent_add(agt
, usess
->agents
);
1607 /* Create the default tracepoint. */
1608 memset(&uevent
, 0, sizeof(uevent
));
1609 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1610 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1611 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1612 if (!default_event_name
) {
1613 ret
= LTTNG_ERR_FATAL
;
1616 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1617 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1620 * The domain type is changed because we are about to enable the
1621 * default channel and event for the JUL domain that are hardcoded.
1622 * This happens in the UST domain.
1624 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1625 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1627 if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1628 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1630 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1634 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1635 char *filter_expression_copy
= NULL
;
1638 filter_copy
= zmalloc(
1639 sizeof(struct lttng_filter_bytecode
)
1642 ret
= LTTNG_ERR_NOMEM
;
1646 memcpy(filter_copy
, filter
,
1647 sizeof(struct lttng_filter_bytecode
)
1651 if (filter_expression
) {
1652 filter_expression_copy
=
1653 strdup(filter_expression
);
1654 if (!filter_expression
) {
1655 ret
= LTTNG_ERR_NOMEM
;
1656 goto error_free_copy
;
1660 ret
= cmd_enable_event(session
, &tmp_dom
,
1661 (char *) default_chan_name
,
1662 &uevent
, filter_expression_copy
,
1663 filter_copy
, NULL
, wpipe
);
1665 filter_expression_copy
= NULL
;
1668 free(filter_expression_copy
);
1671 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1675 /* The wild card * means that everything should be enabled. */
1676 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1677 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
1681 ret
= event_agent_enable(usess
, agt
, event
, filter
,
1685 if (ret
!= LTTNG_OK
) {
1692 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1693 case LTTNG_DOMAIN_UST_PID
:
1694 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1697 ret
= LTTNG_ERR_UND
;
1704 free(filter_expression
);
1712 * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread.
1715 int enable_kevent_all(struct ltt_session
*session
,
1716 struct lttng_domain
*domain
, char *channel_name
,
1717 struct lttng_event
*event
,
1718 char *filter_expression
,
1719 struct lttng_filter_bytecode
*filter
, int wpipe
)
1722 struct lttng_channel
*attr
;
1725 assert(channel_name
);
1729 switch (domain
->type
) {
1730 case LTTNG_DOMAIN_KERNEL
:
1732 struct ltt_kernel_channel
*kchan
;
1734 assert(session
->kernel_session
);
1737 * If a non-default channel has been created in the
1738 * session, explicitely require that -c chan_name needs
1741 if (session
->kernel_session
->has_non_default_channel
1742 && channel_name
[0] == '\0') {
1743 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1747 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1748 session
->kernel_session
);
1749 if (kchan
== NULL
) {
1750 /* Create default channel */
1751 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1752 LTTNG_BUFFER_GLOBAL
);
1754 ret
= LTTNG_ERR_FATAL
;
1757 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1759 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1760 if (ret
!= LTTNG_OK
) {
1766 /* Get the newly created kernel channel pointer */
1767 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1768 session
->kernel_session
);
1772 switch (event
->type
) {
1773 case LTTNG_EVENT_SYSCALL
:
1774 ret
= event_kernel_enable_syscall(kchan
, "");
1775 if (ret
!= LTTNG_OK
) {
1779 case LTTNG_EVENT_TRACEPOINT
:
1781 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1782 * events already registered to the channel.
1784 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1786 case LTTNG_EVENT_ALL
:
1787 /* Enable syscalls and tracepoints */
1788 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1791 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1795 /* Manage return value */
1796 if (ret
!= LTTNG_OK
) {
1798 * On error, cmd_enable_channel call will take care of destroying
1799 * the created channel if it was needed.
1804 kernel_wait_quiescent(kernel_tracer_fd
);
1808 ret
= LTTNG_ERR_UND
;
1821 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1823 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1826 ssize_t nb_events
= 0;
1829 case LTTNG_DOMAIN_KERNEL
:
1830 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1831 if (nb_events
< 0) {
1832 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1836 case LTTNG_DOMAIN_UST
:
1837 nb_events
= ust_app_list_events(events
);
1838 if (nb_events
< 0) {
1839 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1843 case LTTNG_DOMAIN_LOG4J
:
1844 case LTTNG_DOMAIN_JUL
:
1845 nb_events
= agent_list_events(events
, domain
);
1846 if (nb_events
< 0) {
1847 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1852 ret
= LTTNG_ERR_UND
;
1859 /* Return negative value to differentiate return code */
1864 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1866 ssize_t
cmd_list_tracepoint_fields(int domain
,
1867 struct lttng_event_field
**fields
)
1870 ssize_t nb_fields
= 0;
1873 case LTTNG_DOMAIN_UST
:
1874 nb_fields
= ust_app_list_event_fields(fields
);
1875 if (nb_fields
< 0) {
1876 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1880 case LTTNG_DOMAIN_KERNEL
:
1881 default: /* fall-through */
1882 ret
= LTTNG_ERR_UND
;
1889 /* Return negative value to differentiate return code */
1893 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1895 return syscall_table_list(events
);
1899 * Command LTTNG_START_TRACE processed by the client thread.
1901 int cmd_start_trace(struct ltt_session
*session
)
1904 unsigned long nb_chan
= 0;
1905 struct ltt_kernel_session
*ksession
;
1906 struct ltt_ust_session
*usess
;
1910 /* Ease our life a bit ;) */
1911 ksession
= session
->kernel_session
;
1912 usess
= session
->ust_session
;
1914 /* Is the session already started? */
1915 if (session
->active
) {
1916 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1921 * Starting a session without channel is useless since after that it's not
1922 * possible to enable channel thus inform the client.
1924 if (usess
&& usess
->domain_global
.channels
) {
1926 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1930 nb_chan
+= ksession
->channel_count
;
1933 ret
= LTTNG_ERR_NO_CHANNEL
;
1937 /* Kernel tracing */
1938 if (ksession
!= NULL
) {
1939 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1940 if (ret
!= LTTNG_OK
) {
1945 /* Flag session that trace should start automatically */
1948 * Even though the start trace might fail, flag this session active so
1949 * other application coming in are started by default.
1953 ret
= ust_app_start_trace_all(usess
);
1955 ret
= LTTNG_ERR_UST_START_FAIL
;
1960 /* Flag this after a successful start. */
1961 session
->has_been_started
= 1;
1962 session
->active
= 1;
1971 * Command LTTNG_STOP_TRACE processed by the client thread.
1973 int cmd_stop_trace(struct ltt_session
*session
)
1976 struct ltt_kernel_channel
*kchan
;
1977 struct ltt_kernel_session
*ksession
;
1978 struct ltt_ust_session
*usess
;
1983 ksession
= session
->kernel_session
;
1984 usess
= session
->ust_session
;
1986 /* Session is not active. Skip everythong and inform the client. */
1987 if (!session
->active
) {
1988 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1993 if (ksession
&& ksession
->active
) {
1994 DBG("Stop kernel tracing");
1996 ret
= kernel_stop_session(ksession
);
1998 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2002 kernel_wait_quiescent(kernel_tracer_fd
);
2004 /* Flush metadata after stopping (if exists) */
2005 if (ksession
->metadata_stream_fd
>= 0) {
2006 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2008 ERR("Kernel metadata flush failed");
2012 /* Flush all buffers after stopping */
2013 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2014 ret
= kernel_flush_buffer(kchan
);
2016 ERR("Kernel flush buffer error");
2020 ksession
->active
= 0;
2023 if (usess
&& usess
->active
) {
2025 * Even though the stop trace might fail, flag this session inactive so
2026 * other application coming in are not started by default.
2030 ret
= ust_app_stop_trace_all(usess
);
2032 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2037 /* Flag inactive after a successful stop. */
2038 session
->active
= 0;
2046 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2048 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2049 struct lttng_uri
*uris
)
2052 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2053 struct ltt_ust_session
*usess
= session
->ust_session
;
2059 /* Can't set consumer URI if the session is active. */
2060 if (session
->active
) {
2061 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2065 /* Set the "global" consumer URIs */
2066 for (i
= 0; i
< nb_uri
; i
++) {
2067 ret
= add_uri_to_consumer(session
->consumer
,
2068 &uris
[i
], 0, session
->name
);
2069 if (ret
!= LTTNG_OK
) {
2074 /* Set UST session URIs */
2075 if (session
->ust_session
) {
2076 for (i
= 0; i
< nb_uri
; i
++) {
2077 ret
= add_uri_to_consumer(
2078 session
->ust_session
->consumer
,
2079 &uris
[i
], LTTNG_DOMAIN_UST
,
2081 if (ret
!= LTTNG_OK
) {
2087 /* Set kernel session URIs */
2088 if (session
->kernel_session
) {
2089 for (i
= 0; i
< nb_uri
; i
++) {
2090 ret
= add_uri_to_consumer(
2091 session
->kernel_session
->consumer
,
2092 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2094 if (ret
!= LTTNG_OK
) {
2101 * Make sure to set the session in output mode after we set URI since a
2102 * session can be created without URL (thus flagged in no output mode).
2104 session
->output_traces
= 1;
2106 ksess
->output_traces
= 1;
2110 usess
->output_traces
= 1;
2121 * Command LTTNG_CREATE_SESSION processed by the client thread.
2123 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2124 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2127 struct ltt_session
*session
;
2133 * Verify if the session already exist
2135 * XXX: There is no need for the session lock list here since the caller
2136 * (process_client_msg) is holding it. We might want to change that so a
2137 * single command does not lock the entire session list.
2139 session
= session_find_by_name(name
);
2140 if (session
!= NULL
) {
2141 ret
= LTTNG_ERR_EXIST_SESS
;
2145 /* Create tracing session in the registry */
2146 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2147 LTTNG_SOCK_GET_GID_CRED(creds
));
2148 if (ret
!= LTTNG_OK
) {
2153 * Get the newly created session pointer back
2155 * XXX: There is no need for the session lock list here since the caller
2156 * (process_client_msg) is holding it. We might want to change that so a
2157 * single command does not lock the entire session list.
2159 session
= session_find_by_name(name
);
2162 session
->live_timer
= live_timer
;
2163 /* Create default consumer output for the session not yet created. */
2164 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2165 if (session
->consumer
== NULL
) {
2166 ret
= LTTNG_ERR_FATAL
;
2167 goto consumer_error
;
2171 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2172 if (ret
!= LTTNG_OK
) {
2173 goto consumer_error
;
2175 session
->output_traces
= 1;
2177 session
->output_traces
= 0;
2178 DBG2("Session %s created with no output", session
->name
);
2181 session
->consumer
->enabled
= 1;
2186 session_destroy(session
);
2193 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2195 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2196 size_t nb_uri
, lttng_sock_cred
*creds
)
2199 struct ltt_session
*session
;
2200 struct snapshot_output
*new_output
= NULL
;
2206 * Create session in no output mode with URIs set to NULL. The uris we've
2207 * received are for a default snapshot output if one.
2209 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2210 if (ret
!= LTTNG_OK
) {
2214 /* Get the newly created session pointer back. This should NEVER fail. */
2215 session
= session_find_by_name(name
);
2218 /* Flag session for snapshot mode. */
2219 session
->snapshot_mode
= 1;
2221 /* Skip snapshot output creation if no URI is given. */
2226 new_output
= snapshot_output_alloc();
2228 ret
= LTTNG_ERR_NOMEM
;
2229 goto error_snapshot_alloc
;
2232 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2233 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2235 if (ret
== -ENOMEM
) {
2236 ret
= LTTNG_ERR_NOMEM
;
2238 ret
= LTTNG_ERR_INVALID
;
2240 goto error_snapshot
;
2244 snapshot_add_output(&session
->snapshot
, new_output
);
2251 snapshot_output_destroy(new_output
);
2252 error_snapshot_alloc
:
2253 session_destroy(session
);
2259 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2261 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2264 struct ltt_ust_session
*usess
;
2265 struct ltt_kernel_session
*ksess
;
2270 usess
= session
->ust_session
;
2271 ksess
= session
->kernel_session
;
2273 /* Clean kernel session teardown */
2274 kernel_destroy_session(ksess
);
2276 /* UST session teardown */
2278 /* Close any relayd session */
2279 consumer_output_send_destroy_relayd(usess
->consumer
);
2281 /* Destroy every UST application related to this session. */
2282 ret
= ust_app_destroy_trace_all(usess
);
2284 ERR("Error in ust_app_destroy_trace_all");
2287 /* Clean up the rest. */
2288 trace_ust_destroy_session(usess
);
2292 * Must notify the kernel thread here to update it's poll set in order to
2293 * remove the channel(s)' fd just destroyed.
2295 ret
= notify_thread_pipe(wpipe
);
2297 PERROR("write kernel poll pipe");
2300 ret
= session_destroy(session
);
2306 * Command LTTNG_CALIBRATE processed by the client thread.
2308 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2313 case LTTNG_DOMAIN_KERNEL
:
2315 struct lttng_kernel_calibrate kcalibrate
;
2317 switch (calibrate
->type
) {
2318 case LTTNG_CALIBRATE_FUNCTION
:
2320 /* Default and only possible calibrate option. */
2321 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2325 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2327 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2332 case LTTNG_DOMAIN_UST
:
2334 struct lttng_ust_calibrate ucalibrate
;
2336 switch (calibrate
->type
) {
2337 case LTTNG_CALIBRATE_FUNCTION
:
2339 /* Default and only possible calibrate option. */
2340 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2344 ret
= ust_app_calibrate_glb(&ucalibrate
);
2346 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2352 ret
= LTTNG_ERR_UND
;
2363 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2365 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2366 const char *sock_path
, struct consumer_data
*cdata
)
2369 struct consumer_socket
*socket
= NULL
;
2376 case LTTNG_DOMAIN_KERNEL
:
2378 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2382 /* Can't register a consumer if there is already one */
2383 if (ksess
->consumer_fds_sent
!= 0) {
2384 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2388 sock
= lttcomm_connect_unix_sock(sock_path
);
2390 ret
= LTTNG_ERR_CONNECT_FAIL
;
2393 cdata
->cmd_sock
= sock
;
2395 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2396 if (socket
== NULL
) {
2399 PERROR("close register consumer");
2401 cdata
->cmd_sock
= -1;
2402 ret
= LTTNG_ERR_FATAL
;
2406 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2407 if (socket
->lock
== NULL
) {
2408 PERROR("zmalloc pthread mutex");
2409 ret
= LTTNG_ERR_FATAL
;
2412 pthread_mutex_init(socket
->lock
, NULL
);
2413 socket
->registered
= 1;
2416 consumer_add_socket(socket
, ksess
->consumer
);
2419 pthread_mutex_lock(&cdata
->pid_mutex
);
2421 pthread_mutex_unlock(&cdata
->pid_mutex
);
2426 /* TODO: Userspace tracing */
2427 ret
= LTTNG_ERR_UND
;
2435 consumer_destroy_socket(socket
);
2441 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2443 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2444 struct lttng_domain
**domains
)
2449 struct lttng_ht_iter iter
;
2451 if (session
->kernel_session
!= NULL
) {
2452 DBG3("Listing domains found kernel domain");
2456 if (session
->ust_session
!= NULL
) {
2457 DBG3("Listing domains found UST global domain");
2461 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2463 if (agt
->being_used
) {
2470 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2471 if (*domains
== NULL
) {
2472 ret
= LTTNG_ERR_FATAL
;
2476 if (session
->kernel_session
!= NULL
) {
2477 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2479 /* Kernel session buffer type is always GLOBAL */
2480 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2485 if (session
->ust_session
!= NULL
) {
2486 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2487 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2491 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2493 if (agt
->being_used
) {
2494 (*domains
)[index
].type
= agt
->domain
;
2495 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2505 /* Return negative value to differentiate return code */
2511 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2513 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2514 struct lttng_channel
**channels
)
2517 ssize_t nb_chan
= 0;
2520 case LTTNG_DOMAIN_KERNEL
:
2521 if (session
->kernel_session
!= NULL
) {
2522 nb_chan
= session
->kernel_session
->channel_count
;
2524 DBG3("Number of kernel channels %zd", nb_chan
);
2526 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2529 case LTTNG_DOMAIN_UST
:
2530 if (session
->ust_session
!= NULL
) {
2532 nb_chan
= lttng_ht_get_count(
2533 session
->ust_session
->domain_global
.channels
);
2536 DBG3("Number of UST global channels %zd", nb_chan
);
2538 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2543 ret
= LTTNG_ERR_UND
;
2548 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2549 if (*channels
== NULL
) {
2550 ret
= LTTNG_ERR_FATAL
;
2554 list_lttng_channels(domain
, session
, *channels
);
2560 /* Return negative value to differentiate return code */
2565 * Command LTTNG_LIST_EVENTS processed by the client thread.
2567 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2568 char *channel_name
, struct lttng_event
**events
)
2571 ssize_t nb_event
= 0;
2574 case LTTNG_DOMAIN_KERNEL
:
2575 if (session
->kernel_session
!= NULL
) {
2576 nb_event
= list_lttng_kernel_events(channel_name
,
2577 session
->kernel_session
, events
);
2580 case LTTNG_DOMAIN_UST
:
2582 if (session
->ust_session
!= NULL
) {
2583 nb_event
= list_lttng_ust_global_events(channel_name
,
2584 &session
->ust_session
->domain_global
, events
);
2588 case LTTNG_DOMAIN_LOG4J
:
2589 case LTTNG_DOMAIN_JUL
:
2590 if (session
->ust_session
) {
2591 struct lttng_ht_iter iter
;
2595 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2596 &iter
.iter
, agt
, node
.node
) {
2597 nb_event
= list_lttng_agent_events(agt
, events
);
2603 ret
= LTTNG_ERR_UND
;
2610 /* Return negative value to differentiate return code */
2615 * Using the session list, filled a lttng_session array to send back to the
2616 * client for session listing.
2618 * The session list lock MUST be acquired before calling this function. Use
2619 * session_lock_list() and session_unlock_list().
2621 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2626 struct ltt_session
*session
;
2627 struct ltt_session_list
*list
= session_get_list();
2629 DBG("Getting all available session for UID %d GID %d",
2632 * Iterate over session list and append data after the control struct in
2635 cds_list_for_each_entry(session
, &list
->head
, list
) {
2637 * Only list the sessions the user can control.
2639 if (!session_access_ok(session
, uid
, gid
)) {
2643 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2644 struct ltt_ust_session
*usess
= session
->ust_session
;
2646 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2647 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2648 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2649 ret
= build_network_session_path(sessions
[i
].path
,
2650 sizeof(sessions
[i
].path
), session
);
2652 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2653 session
->consumer
->dst
.trace_path
);
2656 PERROR("snprintf session path");
2660 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2661 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2662 sessions
[i
].enabled
= session
->active
;
2663 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2664 sessions
[i
].live_timer_interval
= session
->live_timer
;
2670 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2671 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2673 int cmd_data_pending(struct ltt_session
*session
)
2676 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2677 struct ltt_ust_session
*usess
= session
->ust_session
;
2681 /* Session MUST be stopped to ask for data availability. */
2682 if (session
->active
) {
2683 ret
= LTTNG_ERR_SESSION_STARTED
;
2687 * If stopped, just make sure we've started before else the above call
2688 * will always send that there is data pending.
2690 * The consumer assumes that when the data pending command is received,
2691 * the trace has been started before or else no output data is written
2692 * by the streams which is a condition for data pending. So, this is
2693 * *VERY* important that we don't ask the consumer before a start
2696 if (!session
->has_been_started
) {
2702 if (ksess
&& ksess
->consumer
) {
2703 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2705 /* Data is still being extracted for the kernel. */
2710 if (usess
&& usess
->consumer
) {
2711 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2713 /* Data is still being extracted for the kernel. */
2718 /* Data is ready to be read by a viewer */
2726 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2728 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2730 int cmd_snapshot_add_output(struct ltt_session
*session
,
2731 struct lttng_snapshot_output
*output
, uint32_t *id
)
2734 struct snapshot_output
*new_output
;
2739 DBG("Cmd snapshot add output for session %s", session
->name
);
2742 * Permission denied to create an output if the session is not
2743 * set in no output mode.
2745 if (session
->output_traces
) {
2746 ret
= LTTNG_ERR_EPERM
;
2750 /* Only one output is allowed until we have the "tee" feature. */
2751 if (session
->snapshot
.nb_output
== 1) {
2752 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2756 new_output
= snapshot_output_alloc();
2758 ret
= LTTNG_ERR_NOMEM
;
2762 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2763 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2764 &session
->snapshot
);
2766 if (ret
== -ENOMEM
) {
2767 ret
= LTTNG_ERR_NOMEM
;
2769 ret
= LTTNG_ERR_INVALID
;
2775 snapshot_add_output(&session
->snapshot
, new_output
);
2777 *id
= new_output
->id
;
2784 snapshot_output_destroy(new_output
);
2790 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2792 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2794 int cmd_snapshot_del_output(struct ltt_session
*session
,
2795 struct lttng_snapshot_output
*output
)
2798 struct snapshot_output
*sout
= NULL
;
2806 * Permission denied to create an output if the session is not
2807 * set in no output mode.
2809 if (session
->output_traces
) {
2810 ret
= LTTNG_ERR_EPERM
;
2815 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2817 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2818 } else if (*output
->name
!= '\0') {
2819 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2821 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2824 ret
= LTTNG_ERR_INVALID
;
2828 snapshot_delete_output(&session
->snapshot
, sout
);
2829 snapshot_output_destroy(sout
);
2838 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2840 * If no output is available, outputs is untouched and 0 is returned.
2842 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2844 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2845 struct lttng_snapshot_output
**outputs
)
2848 struct lttng_snapshot_output
*list
= NULL
;
2849 struct lttng_ht_iter iter
;
2850 struct snapshot_output
*output
;
2855 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2858 * Permission denied to create an output if the session is not
2859 * set in no output mode.
2861 if (session
->output_traces
) {
2862 ret
= -LTTNG_ERR_EPERM
;
2866 if (session
->snapshot
.nb_output
== 0) {
2871 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2873 ret
= -LTTNG_ERR_NOMEM
;
2877 /* Copy list from session to the new list object. */
2879 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2880 output
, node
.node
) {
2881 assert(output
->consumer
);
2882 list
[idx
].id
= output
->id
;
2883 list
[idx
].max_size
= output
->max_size
;
2884 if (lttng_strncpy(list
[idx
].name
, output
->name
,
2885 sizeof(list
[idx
].name
))) {
2886 ret
= -LTTNG_ERR_INVALID
;
2889 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2890 if (lttng_strncpy(list
[idx
].ctrl_url
,
2891 output
->consumer
->dst
.trace_path
,
2892 sizeof(list
[idx
].ctrl_url
))) {
2893 ret
= -LTTNG_ERR_INVALID
;
2898 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2899 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2901 ret
= -LTTNG_ERR_NOMEM
;
2906 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2907 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2909 ret
= -LTTNG_ERR_NOMEM
;
2918 ret
= session
->snapshot
.nb_output
;
2926 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2927 * snapshot output is *not* set with a remote destination.
2929 * Return 0 on success or a LTTNG_ERR code.
2931 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2932 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2935 struct lttng_ht_iter iter
;
2936 struct consumer_socket
*socket
;
2939 assert(snap_output
);
2942 DBG2("Set relayd object from snapshot output");
2944 /* Ignore if snapshot consumer output is not network. */
2945 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2950 * For each consumer socket, create and send the relayd object of the
2954 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2955 socket
, node
.node
) {
2956 ret
= send_consumer_relayd_sockets(0, session
->id
,
2957 snap_output
->consumer
, socket
,
2958 session
->name
, session
->hostname
,
2959 session
->live_timer
);
2960 if (ret
!= LTTNG_OK
) {
2972 * Record a kernel snapshot.
2974 * Return LTTNG_OK on success or a LTTNG_ERR code.
2976 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2977 struct snapshot_output
*output
, struct ltt_session
*session
,
2978 int wait
, uint64_t nb_packets_per_stream
)
2988 * Copy kernel session sockets so we can communicate with the right
2989 * consumer for the snapshot record command.
2991 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2993 ret
= LTTNG_ERR_NOMEM
;
2997 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2998 if (ret
!= LTTNG_OK
) {
2999 goto error_snapshot
;
3002 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3003 if (ret
!= LTTNG_OK
) {
3004 goto error_snapshot
;
3011 /* Clean up copied sockets so this output can use some other later on. */
3012 consumer_destroy_output_sockets(output
->consumer
);
3019 * Record a UST snapshot.
3021 * Return 0 on success or a LTTNG_ERR error code.
3023 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3024 struct snapshot_output
*output
, struct ltt_session
*session
,
3025 int wait
, uint64_t nb_packets_per_stream
)
3034 * Copy UST session sockets so we can communicate with the right
3035 * consumer for the snapshot record command.
3037 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3039 ret
= LTTNG_ERR_NOMEM
;
3043 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3044 if (ret
!= LTTNG_OK
) {
3045 goto error_snapshot
;
3048 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3052 ret
= LTTNG_ERR_INVALID
;
3055 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3058 goto error_snapshot
;
3064 /* Clean up copied sockets so this output can use some other later on. */
3065 consumer_destroy_output_sockets(output
->consumer
);
3071 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3072 uint64_t cur_nr_packets
)
3074 uint64_t tot_size
= 0;
3076 if (session
->kernel_session
) {
3077 struct ltt_kernel_channel
*chan
;
3078 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3080 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3081 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3083 * Don't take channel into account if we
3084 * already grab all its packets.
3088 tot_size
+= chan
->channel
->attr
.subbuf_size
3089 * chan
->stream_count
;
3093 if (session
->ust_session
) {
3094 struct ltt_ust_session
*usess
= session
->ust_session
;
3096 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3104 * Calculate the number of packets we can grab from each stream that
3105 * fits within the overall snapshot max size.
3107 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3108 * the number of packets per stream.
3110 * TODO: this approach is not perfect: we consider the worse case
3111 * (packet filling the sub-buffers) as an upper bound, but we could do
3112 * better if we do this calculation while we actually grab the packet
3113 * content: we would know how much padding we don't actually store into
3116 * This algorithm is currently bounded by the number of packets per
3119 * Since we call this algorithm before actually grabbing the data, it's
3120 * an approximation: for instance, applications could appear/disappear
3121 * in between this call and actually grabbing data.
3124 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3127 uint64_t cur_nb_packets
= 0;
3130 return 0; /* Infinite */
3133 size_left
= max_size
;
3135 uint64_t one_more_packet_tot_size
;
3137 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3139 if (!one_more_packet_tot_size
) {
3140 /* We are already grabbing all packets. */
3143 size_left
-= one_more_packet_tot_size
;
3144 if (size_left
< 0) {
3149 if (!cur_nb_packets
) {
3150 /* Not enough room to grab one packet of each stream, error. */
3153 return cur_nb_packets
;
3157 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3159 * The wait parameter is ignored so this call always wait for the snapshot to
3160 * complete before returning.
3162 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3164 int cmd_snapshot_record(struct ltt_session
*session
,
3165 struct lttng_snapshot_output
*output
, int wait
)
3168 unsigned int use_tmp_output
= 0;
3169 struct snapshot_output tmp_output
;
3170 unsigned int snapshot_success
= 0;
3176 DBG("Cmd snapshot record for session %s", session
->name
);
3178 /* Get the datetime for the snapshot output directory. */
3179 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
3182 ret
= LTTNG_ERR_INVALID
;
3187 * Permission denied to create an output if the session is not
3188 * set in no output mode.
3190 if (session
->output_traces
) {
3191 ret
= LTTNG_ERR_EPERM
;
3195 /* The session needs to be started at least once. */
3196 if (!session
->has_been_started
) {
3197 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3201 /* Use temporary output for the session. */
3202 if (*output
->ctrl_url
!= '\0') {
3203 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3204 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3207 if (ret
== -ENOMEM
) {
3208 ret
= LTTNG_ERR_NOMEM
;
3210 ret
= LTTNG_ERR_INVALID
;
3214 /* Use the global session count for the temporary snapshot. */
3215 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3217 /* Use the global datetime */
3218 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3222 if (use_tmp_output
) {
3223 int64_t nb_packets_per_stream
;
3225 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3226 tmp_output
.max_size
);
3227 if (nb_packets_per_stream
< 0) {
3228 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3232 if (session
->kernel_session
) {
3233 ret
= record_kernel_snapshot(session
->kernel_session
,
3234 &tmp_output
, session
,
3235 wait
, nb_packets_per_stream
);
3236 if (ret
!= LTTNG_OK
) {
3241 if (session
->ust_session
) {
3242 ret
= record_ust_snapshot(session
->ust_session
,
3243 &tmp_output
, session
,
3244 wait
, nb_packets_per_stream
);
3245 if (ret
!= LTTNG_OK
) {
3250 snapshot_success
= 1;
3252 struct snapshot_output
*sout
;
3253 struct lttng_ht_iter iter
;
3256 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3257 &iter
.iter
, sout
, node
.node
) {
3258 int64_t nb_packets_per_stream
;
3261 * Make a local copy of the output and assign the possible
3262 * temporary value given by the caller.
3264 memset(&tmp_output
, 0, sizeof(tmp_output
));
3265 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3267 if (output
->max_size
!= (uint64_t) -1ULL) {
3268 tmp_output
.max_size
= output
->max_size
;
3271 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3272 tmp_output
.max_size
);
3273 if (nb_packets_per_stream
< 0) {
3274 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3279 /* Use temporary name. */
3280 if (*output
->name
!= '\0') {
3281 if (lttng_strncpy(tmp_output
.name
, output
->name
,
3282 sizeof(tmp_output
.name
))) {
3283 ret
= LTTNG_ERR_INVALID
;
3289 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3290 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3292 if (session
->kernel_session
) {
3293 ret
= record_kernel_snapshot(session
->kernel_session
,
3294 &tmp_output
, session
,
3295 wait
, nb_packets_per_stream
);
3296 if (ret
!= LTTNG_OK
) {
3302 if (session
->ust_session
) {
3303 ret
= record_ust_snapshot(session
->ust_session
,
3304 &tmp_output
, session
,
3305 wait
, nb_packets_per_stream
);
3306 if (ret
!= LTTNG_OK
) {
3311 snapshot_success
= 1;
3316 if (snapshot_success
) {
3317 session
->snapshot
.nb_snapshot
++;
3319 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3327 * Init command subsystem.
3332 * Set network sequence index to 1 for streams to match a relayd
3333 * socket on the consumer side.
3335 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3336 relayd_net_seq_idx
= 1;
3337 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3339 DBG("Command subsystem initialized");