2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <urcu/list.h>
22 #include <urcu/uatomic.h>
24 #include <common/defaults.h>
25 #include <common/common.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
27 #include <common/relayd/relayd.h>
28 #include <common/utils.h>
33 #include "health-sessiond.h"
35 #include "kernel-consumer.h"
36 #include "lttng-sessiond.h"
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 * Both functions below are special case for the Kernel domain when
53 * enabling/disabling all events.
56 int enable_kevent_all(struct ltt_session
*session
,
57 struct lttng_domain
*domain
, char *channel_name
,
58 struct lttng_event
*event
,
59 char *filter_expression
,
60 struct lttng_filter_bytecode
*filter
, int wpipe
);
62 int disable_kevent_all(struct ltt_session
*session
, int domain
,
64 struct lttng_event
*event
);
67 * Create a session path used by list_lttng_sessions for the case that the
68 * session consumer is on the network.
70 static int build_network_session_path(char *dst
, size_t size
,
71 struct ltt_session
*session
)
73 int ret
, kdata_port
, udata_port
;
74 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
75 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
80 memset(tmp_urls
, 0, sizeof(tmp_urls
));
81 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
83 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
85 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
86 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
87 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
90 if (session
->ust_session
&& session
->ust_session
->consumer
) {
91 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
92 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
95 if (uuri
== NULL
&& kuri
== NULL
) {
96 uri
= &session
->consumer
->dst
.net
.control
;
97 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
98 } else if (kuri
&& uuri
) {
99 ret
= uri_compare(kuri
, uuri
);
103 /* Build uuri URL string */
104 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
111 } else if (kuri
&& uuri
== NULL
) {
113 } else if (uuri
&& kuri
== NULL
) {
117 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
123 * Do we have a UST url set. If yes, this means we have both kernel and UST
126 if (*tmp_uurl
!= '\0') {
127 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
128 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
131 if (kuri
|| (!kuri
&& !uuri
)) {
134 /* No kernel URI, use the UST port. */
137 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
145 * Fill lttng_channel array of all channels.
147 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
148 struct lttng_channel
*channels
)
151 struct ltt_kernel_channel
*kchan
;
153 DBG("Listing channels for session %s", session
->name
);
156 case LTTNG_DOMAIN_KERNEL
:
157 /* Kernel channels */
158 if (session
->kernel_session
!= NULL
) {
159 cds_list_for_each_entry(kchan
,
160 &session
->kernel_session
->channel_list
.head
, list
) {
161 /* Copy lttng_channel struct to array */
162 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
163 channels
[i
].enabled
= kchan
->enabled
;
168 case LTTNG_DOMAIN_UST
:
170 struct lttng_ht_iter iter
;
171 struct ltt_ust_channel
*uchan
;
174 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
175 &iter
.iter
, uchan
, node
.node
) {
176 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
177 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
178 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
179 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
180 channels
[i
].attr
.switch_timer_interval
=
181 uchan
->attr
.switch_timer_interval
;
182 channels
[i
].attr
.read_timer_interval
=
183 uchan
->attr
.read_timer_interval
;
184 channels
[i
].enabled
= uchan
->enabled
;
185 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
186 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
187 switch (uchan
->attr
.output
) {
190 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
204 * Create a list of agent domain events.
206 * Return number of events in list on success or else a negative value.
208 static int list_lttng_agent_events(struct agent
*agt
,
209 struct lttng_event
**events
)
212 unsigned int nb_event
= 0;
213 struct agent_event
*event
;
214 struct lttng_event
*tmp_events
;
215 struct lttng_ht_iter iter
;
220 DBG3("Listing agent events");
222 nb_event
= lttng_ht_get_count(agt
->events
);
228 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
230 PERROR("zmalloc agent events session");
231 ret
= -LTTNG_ERR_FATAL
;
236 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
237 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
238 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
239 tmp_events
[i
].enabled
= event
->enabled
;
240 tmp_events
[i
].loglevel
= event
->loglevel
;
241 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
246 *events
= tmp_events
;
250 assert(nb_event
== i
);
255 * Create a list of ust global domain events.
257 static int list_lttng_ust_global_events(char *channel_name
,
258 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
261 unsigned int nb_event
= 0;
262 struct lttng_ht_iter iter
;
263 struct lttng_ht_node_str
*node
;
264 struct ltt_ust_channel
*uchan
;
265 struct ltt_ust_event
*uevent
;
266 struct lttng_event
*tmp
;
268 DBG("Listing UST global events for channel %s", channel_name
);
272 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
273 node
= lttng_ht_iter_get_node_str(&iter
);
275 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
279 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
281 nb_event
+= lttng_ht_get_count(uchan
->events
);
288 DBG3("Listing UST global %d events", nb_event
);
290 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
292 ret
= LTTNG_ERR_FATAL
;
296 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
297 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
298 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
299 tmp
[i
].enabled
= uevent
->enabled
;
301 switch (uevent
->attr
.instrumentation
) {
302 case LTTNG_UST_TRACEPOINT
:
303 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
305 case LTTNG_UST_PROBE
:
306 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
308 case LTTNG_UST_FUNCTION
:
309 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
313 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
314 switch (uevent
->attr
.loglevel_type
) {
315 case LTTNG_UST_LOGLEVEL_ALL
:
316 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
318 case LTTNG_UST_LOGLEVEL_RANGE
:
319 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
321 case LTTNG_UST_LOGLEVEL_SINGLE
:
322 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
325 if (uevent
->filter
) {
328 if (uevent
->exclusion
) {
329 tmp
[i
].exclusion
= 1;
343 * Fill lttng_event array of all kernel events in the channel.
345 static int list_lttng_kernel_events(char *channel_name
,
346 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
349 unsigned int nb_event
;
350 struct ltt_kernel_event
*event
;
351 struct ltt_kernel_channel
*kchan
;
353 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
355 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
359 nb_event
= kchan
->event_count
;
361 DBG("Listing events for channel %s", kchan
->channel
->name
);
368 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
369 if (*events
== NULL
) {
370 ret
= LTTNG_ERR_FATAL
;
374 /* Kernel channels */
375 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
376 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
377 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
378 (*events
)[i
].enabled
= event
->enabled
;
380 switch (event
->event
->instrumentation
) {
381 case LTTNG_KERNEL_TRACEPOINT
:
382 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
384 case LTTNG_KERNEL_KRETPROBE
:
385 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
386 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
387 sizeof(struct lttng_kernel_kprobe
));
389 case LTTNG_KERNEL_KPROBE
:
390 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
391 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
392 sizeof(struct lttng_kernel_kprobe
));
394 case LTTNG_KERNEL_FUNCTION
:
395 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
396 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
397 sizeof(struct lttng_kernel_function
));
399 case LTTNG_KERNEL_NOOP
:
400 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
402 case LTTNG_KERNEL_SYSCALL
:
403 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
405 case LTTNG_KERNEL_ALL
:
416 new_size
= syscall_list_channel(kchan
, events
, nb_event
);
428 /* Negate the error code to differentiate the size from an error */
433 * Add URI so the consumer output object. Set the correct path depending on the
434 * domain adding the default trace directory.
436 static int add_uri_to_consumer(struct consumer_output
*consumer
,
437 struct lttng_uri
*uri
, int domain
, const char *session_name
)
440 const char *default_trace_dir
;
444 if (consumer
== NULL
) {
445 DBG("No consumer detected. Don't add URI. Stopping.");
446 ret
= LTTNG_ERR_NO_CONSUMER
;
451 case LTTNG_DOMAIN_KERNEL
:
452 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
454 case LTTNG_DOMAIN_UST
:
455 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
459 * This case is possible is we try to add the URI to the global tracing
460 * session consumer object which in this case there is no subdir.
462 default_trace_dir
= "";
465 switch (uri
->dtype
) {
468 DBG2("Setting network URI to consumer");
470 if (consumer
->type
== CONSUMER_DST_NET
) {
471 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
472 consumer
->dst
.net
.control_isset
) ||
473 (uri
->stype
== LTTNG_STREAM_DATA
&&
474 consumer
->dst
.net
.data_isset
)) {
475 ret
= LTTNG_ERR_URL_EXIST
;
479 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
482 consumer
->type
= CONSUMER_DST_NET
;
484 /* Set URI into consumer output object */
485 ret
= consumer_set_network_uri(consumer
, uri
);
489 } else if (ret
== 1) {
491 * URI was the same in the consumer so we do not append the subdir
492 * again so to not duplicate output dir.
498 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
499 ret
= consumer_set_subdir(consumer
, session_name
);
501 ret
= LTTNG_ERR_FATAL
;
506 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
507 /* On a new subdir, reappend the default trace dir. */
508 strncat(consumer
->subdir
, default_trace_dir
,
509 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
510 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
515 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
516 memset(consumer
->dst
.trace_path
, 0,
517 sizeof(consumer
->dst
.trace_path
));
518 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
519 sizeof(consumer
->dst
.trace_path
));
520 /* Append default trace dir */
521 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
522 sizeof(consumer
->dst
.trace_path
) -
523 strlen(consumer
->dst
.trace_path
) - 1);
524 /* Flag consumer as local. */
525 consumer
->type
= CONSUMER_DST_LOCAL
;
536 * Init tracing by creating trace directory and sending fds kernel consumer.
538 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
541 struct lttng_ht_iter iter
;
542 struct consumer_socket
*socket
;
548 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
549 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
551 pthread_mutex_lock(socket
->lock
);
552 ret
= kernel_consumer_send_session(socket
, session
);
553 pthread_mutex_unlock(socket
->lock
);
555 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
567 * Create a socket to the relayd using the URI.
569 * On success, the relayd_sock pointer is set to the created socket.
570 * Else, it's stays untouched and a lttcomm error code is returned.
572 static int create_connect_relayd(struct lttng_uri
*uri
,
573 struct lttcomm_relayd_sock
**relayd_sock
)
576 struct lttcomm_relayd_sock
*rsock
;
578 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
579 RELAYD_VERSION_COMM_MINOR
);
581 ret
= LTTNG_ERR_FATAL
;
586 * Connect to relayd so we can proceed with a session creation. This call
587 * can possibly block for an arbitrary amount of time to set the health
588 * state to be in poll execution.
591 ret
= relayd_connect(rsock
);
594 ERR("Unable to reach lttng-relayd");
595 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
599 /* Create socket for control stream. */
600 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
601 DBG3("Creating relayd stream socket from URI");
603 /* Check relayd version */
604 ret
= relayd_version_check(rsock
);
606 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
609 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
610 DBG3("Creating relayd data socket from URI");
612 /* Command is not valid */
613 ERR("Relayd invalid stream type: %d", uri
->stype
);
614 ret
= LTTNG_ERR_INVALID
;
618 *relayd_sock
= rsock
;
623 /* The returned value is not useful since we are on an error path. */
624 (void) relayd_close(rsock
);
632 * Connect to the relayd using URI and send the socket to the right consumer.
634 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
635 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
636 struct consumer_socket
*consumer_sock
,
637 char *session_name
, char *hostname
, int session_live_timer
)
640 struct lttcomm_relayd_sock
*rsock
= NULL
;
642 /* Connect to relayd and make version check if uri is the control. */
643 ret
= create_connect_relayd(relayd_uri
, &rsock
);
644 if (ret
!= LTTNG_OK
) {
649 /* Set the network sequence index if not set. */
650 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
651 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
653 * Increment net_seq_idx because we are about to transfer the
654 * new relayd socket to the consumer.
655 * Assign unique key so the consumer can match streams.
657 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
658 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
661 /* Send relayd socket to consumer. */
662 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
663 relayd_uri
->stype
, session_id
,
664 session_name
, hostname
, session_live_timer
);
666 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
670 /* Flag that the corresponding socket was sent. */
671 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
672 consumer_sock
->control_sock_sent
= 1;
673 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
674 consumer_sock
->data_sock_sent
= 1;
680 * Close socket which was dup on the consumer side. The session daemon does
681 * NOT keep track of the relayd socket(s) once transfer to the consumer.
685 (void) relayd_close(rsock
);
689 if (ret
!= LTTNG_OK
) {
691 * The consumer output for this session should not be used anymore
692 * since the relayd connection failed thus making any tracing or/and
693 * streaming not usable.
695 consumer
->enabled
= 0;
701 * Send both relayd sockets to a specific consumer and domain. This is a
702 * helper function to facilitate sending the information to the consumer for a
705 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
706 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
707 char *session_name
, char *hostname
, int session_live_timer
)
714 /* Sending control relayd socket. */
715 if (!sock
->control_sock_sent
) {
716 ret
= send_consumer_relayd_socket(domain
, session_id
,
717 &consumer
->dst
.net
.control
, consumer
, sock
,
718 session_name
, hostname
, session_live_timer
);
719 if (ret
!= LTTNG_OK
) {
724 /* Sending data relayd socket. */
725 if (!sock
->data_sock_sent
) {
726 ret
= send_consumer_relayd_socket(domain
, session_id
,
727 &consumer
->dst
.net
.data
, consumer
, sock
,
728 session_name
, hostname
, session_live_timer
);
729 if (ret
!= LTTNG_OK
) {
739 * Setup relayd connections for a tracing session. First creates the socket to
740 * the relayd and send them to the right domain consumer. Consumer type MUST be
743 int cmd_setup_relayd(struct ltt_session
*session
)
746 struct ltt_ust_session
*usess
;
747 struct ltt_kernel_session
*ksess
;
748 struct consumer_socket
*socket
;
749 struct lttng_ht_iter iter
;
753 usess
= session
->ust_session
;
754 ksess
= session
->kernel_session
;
756 DBG("Setting relayd for session %s", session
->name
);
760 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
761 && usess
->consumer
->enabled
) {
762 /* For each consumer socket, send relayd sockets */
763 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
765 pthread_mutex_lock(socket
->lock
);
766 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
767 usess
->consumer
, socket
,
768 session
->name
, session
->hostname
,
769 session
->live_timer
);
770 pthread_mutex_unlock(socket
->lock
);
771 if (ret
!= LTTNG_OK
) {
774 /* Session is now ready for network streaming. */
775 session
->net_handle
= 1;
779 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
780 && ksess
->consumer
->enabled
) {
781 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
783 pthread_mutex_lock(socket
->lock
);
784 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
785 ksess
->consumer
, socket
,
786 session
->name
, session
->hostname
,
787 session
->live_timer
);
788 pthread_mutex_unlock(socket
->lock
);
789 if (ret
!= LTTNG_OK
) {
792 /* Session is now ready for network streaming. */
793 session
->net_handle
= 1;
803 * Start a kernel session by opening all necessary streams.
805 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
808 struct ltt_kernel_channel
*kchan
;
810 /* Open kernel metadata */
811 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
812 ret
= kernel_open_metadata(ksess
);
814 ret
= LTTNG_ERR_KERN_META_FAIL
;
819 /* Open kernel metadata stream */
820 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
821 ret
= kernel_open_metadata_stream(ksess
);
823 ERR("Kernel create metadata stream failed");
824 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
829 /* For each channel */
830 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
831 if (kchan
->stream_count
== 0) {
832 ret
= kernel_open_channel_stream(kchan
);
834 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
837 /* Update the stream global counter */
838 ksess
->stream_count_global
+= ret
;
842 /* Setup kernel consumer socket and send fds to it */
843 ret
= init_kernel_tracing(ksess
);
845 ret
= LTTNG_ERR_KERN_START_FAIL
;
849 /* This start the kernel tracing */
850 ret
= kernel_start_session(ksess
);
852 ret
= LTTNG_ERR_KERN_START_FAIL
;
856 /* Quiescent wait after starting trace */
857 kernel_wait_quiescent(kernel_tracer_fd
);
868 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
870 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
874 struct ltt_ust_session
*usess
;
876 usess
= session
->ust_session
;
881 case LTTNG_DOMAIN_KERNEL
:
883 ret
= channel_kernel_disable(session
->kernel_session
,
885 if (ret
!= LTTNG_OK
) {
889 kernel_wait_quiescent(kernel_tracer_fd
);
892 case LTTNG_DOMAIN_UST
:
894 struct ltt_ust_channel
*uchan
;
895 struct lttng_ht
*chan_ht
;
897 chan_ht
= usess
->domain_global
.channels
;
899 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
901 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
905 ret
= channel_ust_disable(usess
, uchan
);
906 if (ret
!= LTTNG_OK
) {
912 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
913 case LTTNG_DOMAIN_UST_EXEC_NAME
:
914 case LTTNG_DOMAIN_UST_PID
:
917 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
929 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
931 * The wpipe arguments is used as a notifier for the kernel thread.
933 int cmd_enable_channel(struct ltt_session
*session
,
934 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
937 struct ltt_ust_session
*usess
= session
->ust_session
;
938 struct lttng_ht
*chan_ht
;
944 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
949 * Don't try to enable a channel if the session has been started at
950 * some point in time before. The tracer does not allow it.
952 if (session
->has_been_started
) {
953 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
958 * If the session is a live session, remove the switch timer, the
959 * live timer does the same thing but sends also synchronisation
960 * beacons for inactive streams.
962 if (session
->live_timer
> 0) {
963 attr
->attr
.live_timer_interval
= session
->live_timer
;
964 attr
->attr
.switch_timer_interval
= 0;
968 * The ringbuffer (both in user space and kernel) behave badly in overwrite
969 * mode and with less than 2 subbuf so block it right away and send back an
970 * invalid attribute error.
972 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
973 ret
= LTTNG_ERR_INVALID
;
977 switch (domain
->type
) {
978 case LTTNG_DOMAIN_KERNEL
:
980 struct ltt_kernel_channel
*kchan
;
982 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
983 session
->kernel_session
);
985 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
986 if (attr
->name
[0] != '\0') {
987 session
->kernel_session
->has_non_default_channel
= 1;
990 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
993 if (ret
!= LTTNG_OK
) {
997 kernel_wait_quiescent(kernel_tracer_fd
);
1000 case LTTNG_DOMAIN_UST
:
1002 struct ltt_ust_channel
*uchan
;
1004 chan_ht
= usess
->domain_global
.channels
;
1006 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1007 if (uchan
== NULL
) {
1008 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1009 if (attr
->name
[0] != '\0') {
1010 usess
->has_non_default_channel
= 1;
1013 ret
= channel_ust_enable(usess
, uchan
);
1018 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1029 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1031 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1033 struct lttng_event
*event
)
1038 DBG("Disable event command for event \'%s\'", event
->name
);
1040 event_name
= event
->name
;
1042 if (event
->loglevel_type
|| event
->loglevel
|| event
->enabled
1043 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1044 return LTTNG_ERR_UNK
;
1046 /* Special handling for kernel domain all events. */
1047 if (domain
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event_name
, "*")) {
1048 return disable_kevent_all(session
, domain
, channel_name
, event
);
1054 case LTTNG_DOMAIN_KERNEL
:
1056 struct ltt_kernel_channel
*kchan
;
1057 struct ltt_kernel_session
*ksess
;
1059 ksess
= session
->kernel_session
;
1062 * If a non-default channel has been created in the
1063 * session, explicitely require that -c chan_name needs
1066 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1067 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1071 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1072 if (kchan
== NULL
) {
1073 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1077 switch (event
->type
) {
1078 case LTTNG_EVENT_ALL
:
1079 case LTTNG_EVENT_TRACEPOINT
:
1080 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1081 if (ret
!= LTTNG_OK
) {
1085 case LTTNG_EVENT_SYSCALL
:
1086 ret
= event_kernel_disable_syscall(kchan
, event_name
);
1087 if (ret
!= LTTNG_OK
) {
1092 ret
= LTTNG_ERR_UNK
;
1096 kernel_wait_quiescent(kernel_tracer_fd
);
1099 case LTTNG_DOMAIN_UST
:
1101 struct ltt_ust_channel
*uchan
;
1102 struct ltt_ust_session
*usess
;
1104 usess
= session
->ust_session
;
1107 * If a non-default channel has been created in the
1108 * session, explicitely require that -c chan_name needs
1111 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1112 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1116 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1118 if (uchan
== NULL
) {
1119 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1123 switch (event
->type
) {
1124 case LTTNG_EVENT_ALL
:
1125 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1126 if (ret
!= LTTNG_OK
) {
1131 ret
= LTTNG_ERR_UNK
;
1135 DBG3("Disable UST event %s in channel %s completed", event_name
,
1139 case LTTNG_DOMAIN_LOG4J
:
1140 case LTTNG_DOMAIN_JUL
:
1143 struct ltt_ust_session
*usess
= session
->ust_session
;
1147 switch (event
->type
) {
1148 case LTTNG_EVENT_ALL
:
1151 ret
= LTTNG_ERR_UNK
;
1155 agt
= trace_ust_find_agent(usess
, domain
);
1157 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1160 /* The wild card * means that everything should be disabled. */
1161 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1162 ret
= event_agent_disable_all(usess
, agt
);
1164 ret
= event_agent_disable(usess
, agt
, event_name
);
1166 if (ret
!= LTTNG_OK
) {
1173 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1174 case LTTNG_DOMAIN_UST_PID
:
1175 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1178 ret
= LTTNG_ERR_UND
;
1190 * Command LTTNG_DISABLE_EVENT for event "*" processed by the client thread.
1193 int disable_kevent_all(struct ltt_session
*session
, int domain
,
1195 struct lttng_event
*event
)
1202 case LTTNG_DOMAIN_KERNEL
:
1204 struct ltt_kernel_session
*ksess
;
1205 struct ltt_kernel_channel
*kchan
;
1207 ksess
= session
->kernel_session
;
1210 * If a non-default channel has been created in the
1211 * session, explicitely require that -c chan_name needs
1214 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1215 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1219 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1220 if (kchan
== NULL
) {
1221 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1225 switch (event
->type
) {
1226 case LTTNG_EVENT_ALL
:
1227 ret
= event_kernel_disable_all(kchan
);
1228 if (ret
!= LTTNG_OK
) {
1232 case LTTNG_EVENT_SYSCALL
:
1233 ret
= event_kernel_disable_syscall(kchan
, "");
1234 if (ret
!= LTTNG_OK
) {
1239 ret
= LTTNG_ERR_UNK
;
1243 kernel_wait_quiescent(kernel_tracer_fd
);
1247 ret
= LTTNG_ERR_UND
;
1259 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1261 int cmd_add_context(struct ltt_session
*session
, int domain
,
1262 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1264 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1267 case LTTNG_DOMAIN_KERNEL
:
1268 assert(session
->kernel_session
);
1270 if (session
->kernel_session
->channel_count
== 0) {
1271 /* Create default channel */
1272 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1273 if (ret
!= LTTNG_OK
) {
1276 chan_kern_created
= 1;
1278 /* Add kernel context to kernel tracer */
1279 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1280 if (ret
!= LTTNG_OK
) {
1284 case LTTNG_DOMAIN_UST
:
1286 struct ltt_ust_session
*usess
= session
->ust_session
;
1287 unsigned int chan_count
;
1291 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1292 if (chan_count
== 0) {
1293 struct lttng_channel
*attr
;
1294 /* Create default channel */
1295 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1297 ret
= LTTNG_ERR_FATAL
;
1301 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1302 if (ret
!= LTTNG_OK
) {
1307 chan_ust_created
= 1;
1310 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1311 if (ret
!= LTTNG_OK
) {
1317 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1318 case LTTNG_DOMAIN_UST_PID
:
1319 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1322 ret
= LTTNG_ERR_UND
;
1329 if (chan_kern_created
) {
1330 struct ltt_kernel_channel
*kchan
=
1331 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1332 session
->kernel_session
);
1333 /* Created previously, this should NOT fail. */
1335 kernel_destroy_channel(kchan
);
1338 if (chan_ust_created
) {
1339 struct ltt_ust_channel
*uchan
=
1340 trace_ust_find_channel_by_name(
1341 session
->ust_session
->domain_global
.channels
,
1342 DEFAULT_CHANNEL_NAME
);
1343 /* Created previously, this should NOT fail. */
1345 /* Remove from the channel list of the session. */
1346 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1348 trace_ust_destroy_channel(uchan
);
1353 static int validate_event_name(const char *name
)
1356 const char *c
= name
;
1357 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1360 * Make sure that unescaped wildcards are only used as the last
1361 * character of the event name.
1363 while (c
< event_name_end
) {
1371 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1372 /* Wildcard is not the last character */
1373 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1386 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1387 * We own filter, exclusion, and filter_expression.
1389 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1390 char *channel_name
, struct lttng_event
*event
,
1391 char *filter_expression
,
1392 struct lttng_filter_bytecode
*filter
,
1393 struct lttng_event_exclusion
*exclusion
,
1396 int ret
, channel_created
= 0;
1397 struct lttng_channel
*attr
;
1401 assert(channel_name
);
1403 DBG("Enable event command for event \'%s\'", event
->name
);
1405 /* Special handling for kernel domain all events. */
1406 if (domain
->type
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event
->name
, "*")) {
1407 return enable_kevent_all(session
, domain
, channel_name
, event
,
1408 filter_expression
, filter
, wpipe
);
1411 ret
= validate_event_name(event
->name
);
1418 switch (domain
->type
) {
1419 case LTTNG_DOMAIN_KERNEL
:
1421 struct ltt_kernel_channel
*kchan
;
1424 * If a non-default channel has been created in the
1425 * session, explicitely require that -c chan_name needs
1428 if (session
->kernel_session
->has_non_default_channel
1429 && channel_name
[0] == '\0') {
1430 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1434 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1435 session
->kernel_session
);
1436 if (kchan
== NULL
) {
1437 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1438 LTTNG_BUFFER_GLOBAL
);
1440 ret
= LTTNG_ERR_FATAL
;
1443 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1445 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1446 if (ret
!= LTTNG_OK
) {
1452 channel_created
= 1;
1455 /* Get the newly created kernel channel pointer */
1456 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1457 session
->kernel_session
);
1458 if (kchan
== NULL
) {
1459 /* This sould not happen... */
1460 ret
= LTTNG_ERR_FATAL
;
1464 switch (event
->type
) {
1465 case LTTNG_EVENT_ALL
:
1466 case LTTNG_EVENT_PROBE
:
1467 case LTTNG_EVENT_FUNCTION
:
1468 case LTTNG_EVENT_FUNCTION_ENTRY
:
1469 case LTTNG_EVENT_TRACEPOINT
:
1470 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1471 if (ret
!= LTTNG_OK
) {
1472 if (channel_created
) {
1473 /* Let's not leak a useless channel. */
1474 kernel_destroy_channel(kchan
);
1479 case LTTNG_EVENT_SYSCALL
:
1480 ret
= event_kernel_enable_syscall(kchan
, event
->name
);
1481 if (ret
!= LTTNG_OK
) {
1486 ret
= LTTNG_ERR_UNK
;
1490 kernel_wait_quiescent(kernel_tracer_fd
);
1493 case LTTNG_DOMAIN_UST
:
1495 struct ltt_ust_channel
*uchan
;
1496 struct ltt_ust_session
*usess
= session
->ust_session
;
1501 * If a non-default channel has been created in the
1502 * session, explicitely require that -c chan_name needs
1505 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1506 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1510 /* Get channel from global UST domain */
1511 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1513 if (uchan
== NULL
) {
1514 /* Create default channel */
1515 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1516 usess
->buffer_type
);
1518 ret
= LTTNG_ERR_FATAL
;
1521 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1523 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1524 if (ret
!= LTTNG_OK
) {
1530 /* Get the newly created channel reference back */
1531 uchan
= trace_ust_find_channel_by_name(
1532 usess
->domain_global
.channels
, channel_name
);
1536 /* At this point, the session and channel exist on the tracer */
1537 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1538 filter_expression
, filter
, exclusion
);
1539 /* We have passed ownership */
1540 filter_expression
= NULL
;
1543 if (ret
!= LTTNG_OK
) {
1548 case LTTNG_DOMAIN_LOG4J
:
1549 case LTTNG_DOMAIN_JUL
:
1551 const char *default_event_name
, *default_chan_name
;
1553 struct lttng_event uevent
;
1554 struct lttng_domain tmp_dom
;
1555 struct ltt_ust_session
*usess
= session
->ust_session
;
1559 agt
= trace_ust_find_agent(usess
, domain
->type
);
1561 agt
= agent_create(domain
->type
);
1563 ret
= -LTTNG_ERR_NOMEM
;
1566 agent_add(agt
, usess
->agents
);
1569 /* Create the default tracepoint. */
1570 memset(&uevent
, 0, sizeof(uevent
));
1571 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1572 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1573 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1574 if (!default_event_name
) {
1575 ret
= -LTTNG_ERR_FATAL
;
1578 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1579 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1582 * The domain type is changed because we are about to enable the
1583 * default channel and event for the JUL domain that are hardcoded.
1584 * This happens in the UST domain.
1586 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1587 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1589 if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1590 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1592 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1596 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1599 filter_copy
= zmalloc(
1600 sizeof(struct lttng_filter_bytecode
)
1606 memcpy(filter_copy
, filter
,
1607 sizeof(struct lttng_filter_bytecode
)
1611 ret
= cmd_enable_event(session
, &tmp_dom
,
1612 (char *) default_chan_name
,
1613 &uevent
, filter_expression
, filter_copy
,
1615 /* We have passed ownership */
1616 filter_expression
= NULL
;
1619 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1623 /* The wild card * means that everything should be enabled. */
1624 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1625 ret
= event_agent_enable_all(usess
, agt
, event
, filter
);
1628 ret
= event_agent_enable(usess
, agt
, event
, filter
);
1631 if (ret
!= LTTNG_OK
) {
1638 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1639 case LTTNG_DOMAIN_UST_PID
:
1640 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1643 ret
= LTTNG_ERR_UND
;
1650 free(filter_expression
);
1658 * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread.
1661 int enable_kevent_all(struct ltt_session
*session
,
1662 struct lttng_domain
*domain
, char *channel_name
,
1663 struct lttng_event
*event
,
1664 char *filter_expression
,
1665 struct lttng_filter_bytecode
*filter
, int wpipe
)
1668 struct lttng_channel
*attr
;
1671 assert(channel_name
);
1675 switch (domain
->type
) {
1676 case LTTNG_DOMAIN_KERNEL
:
1678 struct ltt_kernel_channel
*kchan
;
1680 assert(session
->kernel_session
);
1683 * If a non-default channel has been created in the
1684 * session, explicitely require that -c chan_name needs
1687 if (session
->kernel_session
->has_non_default_channel
1688 && channel_name
[0] == '\0') {
1689 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1693 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1694 session
->kernel_session
);
1695 if (kchan
== NULL
) {
1696 /* Create default channel */
1697 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1698 LTTNG_BUFFER_GLOBAL
);
1700 ret
= LTTNG_ERR_FATAL
;
1703 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1705 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1706 if (ret
!= LTTNG_OK
) {
1712 /* Get the newly created kernel channel pointer */
1713 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1714 session
->kernel_session
);
1718 switch (event
->type
) {
1719 case LTTNG_EVENT_SYSCALL
:
1720 ret
= event_kernel_enable_syscall(kchan
, "");
1721 if (ret
!= LTTNG_OK
) {
1725 case LTTNG_EVENT_TRACEPOINT
:
1727 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1728 * events already registered to the channel.
1730 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1732 case LTTNG_EVENT_ALL
:
1733 /* Enable syscalls and tracepoints */
1734 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1737 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1741 /* Manage return value */
1742 if (ret
!= LTTNG_OK
) {
1744 * On error, cmd_enable_channel call will take care of destroying
1745 * the created channel if it was needed.
1750 kernel_wait_quiescent(kernel_tracer_fd
);
1754 ret
= LTTNG_ERR_UND
;
1767 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1769 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1772 ssize_t nb_events
= 0;
1775 case LTTNG_DOMAIN_KERNEL
:
1776 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1777 if (nb_events
< 0) {
1778 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1782 case LTTNG_DOMAIN_UST
:
1783 nb_events
= ust_app_list_events(events
);
1784 if (nb_events
< 0) {
1785 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1789 case LTTNG_DOMAIN_LOG4J
:
1790 case LTTNG_DOMAIN_JUL
:
1791 nb_events
= agent_list_events(events
, domain
);
1792 if (nb_events
< 0) {
1793 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1798 ret
= LTTNG_ERR_UND
;
1805 /* Return negative value to differentiate return code */
1810 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1812 ssize_t
cmd_list_tracepoint_fields(int domain
,
1813 struct lttng_event_field
**fields
)
1816 ssize_t nb_fields
= 0;
1819 case LTTNG_DOMAIN_UST
:
1820 nb_fields
= ust_app_list_event_fields(fields
);
1821 if (nb_fields
< 0) {
1822 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1826 case LTTNG_DOMAIN_KERNEL
:
1827 default: /* fall-through */
1828 ret
= LTTNG_ERR_UND
;
1835 /* Return negative value to differentiate return code */
1839 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1841 return syscall_table_list(events
);
1845 * Command LTTNG_START_TRACE processed by the client thread.
1847 int cmd_start_trace(struct ltt_session
*session
)
1850 unsigned long nb_chan
= 0;
1851 struct ltt_kernel_session
*ksession
;
1852 struct ltt_ust_session
*usess
;
1856 /* Ease our life a bit ;) */
1857 ksession
= session
->kernel_session
;
1858 usess
= session
->ust_session
;
1860 /* Is the session already started? */
1861 if (session
->active
) {
1862 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1867 * Starting a session without channel is useless since after that it's not
1868 * possible to enable channel thus inform the client.
1870 if (usess
&& usess
->domain_global
.channels
) {
1872 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1876 nb_chan
+= ksession
->channel_count
;
1879 ret
= LTTNG_ERR_NO_CHANNEL
;
1883 /* Kernel tracing */
1884 if (ksession
!= NULL
) {
1885 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1886 if (ret
!= LTTNG_OK
) {
1891 /* Flag session that trace should start automatically */
1894 * Even though the start trace might fail, flag this session active so
1895 * other application coming in are started by default.
1899 ret
= ust_app_start_trace_all(usess
);
1901 ret
= LTTNG_ERR_UST_START_FAIL
;
1906 /* Flag this after a successful start. */
1907 session
->has_been_started
= 1;
1908 session
->active
= 1;
1917 * Command LTTNG_STOP_TRACE processed by the client thread.
1919 int cmd_stop_trace(struct ltt_session
*session
)
1922 struct ltt_kernel_channel
*kchan
;
1923 struct ltt_kernel_session
*ksession
;
1924 struct ltt_ust_session
*usess
;
1929 ksession
= session
->kernel_session
;
1930 usess
= session
->ust_session
;
1932 /* Session is not active. Skip everythong and inform the client. */
1933 if (!session
->active
) {
1934 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1939 if (ksession
&& ksession
->active
) {
1940 DBG("Stop kernel tracing");
1942 /* Flush metadata if exist */
1943 if (ksession
->metadata_stream_fd
>= 0) {
1944 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1946 ERR("Kernel metadata flush failed");
1950 /* Flush all buffers before stopping */
1951 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1952 ret
= kernel_flush_buffer(kchan
);
1954 ERR("Kernel flush buffer error");
1958 ret
= kernel_stop_session(ksession
);
1960 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1964 kernel_wait_quiescent(kernel_tracer_fd
);
1966 ksession
->active
= 0;
1969 if (usess
&& usess
->active
) {
1971 * Even though the stop trace might fail, flag this session inactive so
1972 * other application coming in are not started by default.
1976 ret
= ust_app_stop_trace_all(usess
);
1978 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1983 /* Flag inactive after a successful stop. */
1984 session
->active
= 0;
1992 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1994 int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
1995 size_t nb_uri
, struct lttng_uri
*uris
)
1998 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1999 struct ltt_ust_session
*usess
= session
->ust_session
;
2000 struct consumer_output
*consumer
= NULL
;
2006 /* Can't set consumer URI if the session is active. */
2007 if (session
->active
) {
2008 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2013 * This case switch makes sure the domain session has a temporary consumer
2014 * so the URL can be set.
2018 /* Code flow error. A session MUST always have a consumer object */
2019 assert(session
->consumer
);
2021 * The URL will be added to the tracing session consumer instead of a
2022 * specific domain consumer.
2024 consumer
= session
->consumer
;
2026 case LTTNG_DOMAIN_KERNEL
:
2027 /* Code flow error if we don't have a kernel session here. */
2029 assert(ksess
->consumer
);
2030 consumer
= ksess
->consumer
;
2032 case LTTNG_DOMAIN_UST
:
2033 /* Code flow error if we don't have a kernel session here. */
2035 assert(usess
->consumer
);
2036 consumer
= usess
->consumer
;
2040 for (i
= 0; i
< nb_uri
; i
++) {
2041 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
2042 if (ret
!= LTTNG_OK
) {
2048 * Make sure to set the session in output mode after we set URI since a
2049 * session can be created without URL (thus flagged in no output mode).
2051 session
->output_traces
= 1;
2053 ksess
->output_traces
= 1;
2055 usess
->output_traces
= 1;
2066 * Command LTTNG_CREATE_SESSION processed by the client thread.
2068 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2069 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2072 struct ltt_session
*session
;
2078 * Verify if the session already exist
2080 * XXX: There is no need for the session lock list here since the caller
2081 * (process_client_msg) is holding it. We might want to change that so a
2082 * single command does not lock the entire session list.
2084 session
= session_find_by_name(name
);
2085 if (session
!= NULL
) {
2086 ret
= LTTNG_ERR_EXIST_SESS
;
2090 /* Create tracing session in the registry */
2091 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2092 LTTNG_SOCK_GET_GID_CRED(creds
));
2093 if (ret
!= LTTNG_OK
) {
2098 * Get the newly created session pointer back
2100 * XXX: There is no need for the session lock list here since the caller
2101 * (process_client_msg) is holding it. We might want to change that so a
2102 * single command does not lock the entire session list.
2104 session
= session_find_by_name(name
);
2107 session
->live_timer
= live_timer
;
2108 /* Create default consumer output for the session not yet created. */
2109 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2110 if (session
->consumer
== NULL
) {
2111 ret
= LTTNG_ERR_FATAL
;
2112 goto consumer_error
;
2116 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
2117 if (ret
!= LTTNG_OK
) {
2118 goto consumer_error
;
2120 session
->output_traces
= 1;
2122 session
->output_traces
= 0;
2123 DBG2("Session %s created with no output", session
->name
);
2126 session
->consumer
->enabled
= 1;
2131 session_destroy(session
);
2138 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2140 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2141 size_t nb_uri
, lttng_sock_cred
*creds
)
2144 struct ltt_session
*session
;
2145 struct snapshot_output
*new_output
= NULL
;
2151 * Create session in no output mode with URIs set to NULL. The uris we've
2152 * received are for a default snapshot output if one.
2154 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2155 if (ret
!= LTTNG_OK
) {
2159 /* Get the newly created session pointer back. This should NEVER fail. */
2160 session
= session_find_by_name(name
);
2163 /* Flag session for snapshot mode. */
2164 session
->snapshot_mode
= 1;
2166 /* Skip snapshot output creation if no URI is given. */
2171 new_output
= snapshot_output_alloc();
2173 ret
= LTTNG_ERR_NOMEM
;
2174 goto error_snapshot_alloc
;
2177 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2178 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2180 if (ret
== -ENOMEM
) {
2181 ret
= LTTNG_ERR_NOMEM
;
2183 ret
= LTTNG_ERR_INVALID
;
2185 goto error_snapshot
;
2189 snapshot_add_output(&session
->snapshot
, new_output
);
2196 snapshot_output_destroy(new_output
);
2197 error_snapshot_alloc
:
2198 session_destroy(session
);
2204 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2206 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2209 struct ltt_ust_session
*usess
;
2210 struct ltt_kernel_session
*ksess
;
2215 usess
= session
->ust_session
;
2216 ksess
= session
->kernel_session
;
2218 /* Clean kernel session teardown */
2219 kernel_destroy_session(ksess
);
2221 /* UST session teardown */
2223 /* Close any relayd session */
2224 consumer_output_send_destroy_relayd(usess
->consumer
);
2226 /* Destroy every UST application related to this session. */
2227 ret
= ust_app_destroy_trace_all(usess
);
2229 ERR("Error in ust_app_destroy_trace_all");
2232 /* Clean up the rest. */
2233 trace_ust_destroy_session(usess
);
2237 * Must notify the kernel thread here to update it's poll set in order to
2238 * remove the channel(s)' fd just destroyed.
2240 ret
= notify_thread_pipe(wpipe
);
2242 PERROR("write kernel poll pipe");
2245 ret
= session_destroy(session
);
2251 * Command LTTNG_CALIBRATE processed by the client thread.
2253 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2258 case LTTNG_DOMAIN_KERNEL
:
2260 struct lttng_kernel_calibrate kcalibrate
;
2262 switch (calibrate
->type
) {
2263 case LTTNG_CALIBRATE_FUNCTION
:
2265 /* Default and only possible calibrate option. */
2266 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2270 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2272 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2277 case LTTNG_DOMAIN_UST
:
2279 struct lttng_ust_calibrate ucalibrate
;
2281 switch (calibrate
->type
) {
2282 case LTTNG_CALIBRATE_FUNCTION
:
2284 /* Default and only possible calibrate option. */
2285 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2289 ret
= ust_app_calibrate_glb(&ucalibrate
);
2291 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2297 ret
= LTTNG_ERR_UND
;
2308 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2310 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2311 const char *sock_path
, struct consumer_data
*cdata
)
2314 struct consumer_socket
*socket
= NULL
;
2321 case LTTNG_DOMAIN_KERNEL
:
2323 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2327 /* Can't register a consumer if there is already one */
2328 if (ksess
->consumer_fds_sent
!= 0) {
2329 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2333 sock
= lttcomm_connect_unix_sock(sock_path
);
2335 ret
= LTTNG_ERR_CONNECT_FAIL
;
2338 cdata
->cmd_sock
= sock
;
2340 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2341 if (socket
== NULL
) {
2344 PERROR("close register consumer");
2346 cdata
->cmd_sock
= -1;
2347 ret
= LTTNG_ERR_FATAL
;
2351 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2352 if (socket
->lock
== NULL
) {
2353 PERROR("zmalloc pthread mutex");
2354 ret
= LTTNG_ERR_FATAL
;
2357 pthread_mutex_init(socket
->lock
, NULL
);
2358 socket
->registered
= 1;
2361 consumer_add_socket(socket
, ksess
->consumer
);
2364 pthread_mutex_lock(&cdata
->pid_mutex
);
2366 pthread_mutex_unlock(&cdata
->pid_mutex
);
2371 /* TODO: Userspace tracing */
2372 ret
= LTTNG_ERR_UND
;
2380 consumer_destroy_socket(socket
);
2386 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2388 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2389 struct lttng_domain
**domains
)
2394 struct lttng_ht_iter iter
;
2396 if (session
->kernel_session
!= NULL
) {
2397 DBG3("Listing domains found kernel domain");
2401 if (session
->ust_session
!= NULL
) {
2402 DBG3("Listing domains found UST global domain");
2405 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2407 if (agt
->being_used
) {
2413 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2414 if (*domains
== NULL
) {
2415 ret
= LTTNG_ERR_FATAL
;
2419 if (session
->kernel_session
!= NULL
) {
2420 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2424 if (session
->ust_session
!= NULL
) {
2425 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2426 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2429 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2431 if (agt
->being_used
) {
2432 (*domains
)[index
].type
= agt
->domain
;
2433 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2442 /* Return negative value to differentiate return code */
2448 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2450 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2451 struct lttng_channel
**channels
)
2454 ssize_t nb_chan
= 0;
2457 case LTTNG_DOMAIN_KERNEL
:
2458 if (session
->kernel_session
!= NULL
) {
2459 nb_chan
= session
->kernel_session
->channel_count
;
2461 DBG3("Number of kernel channels %zd", nb_chan
);
2463 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2466 case LTTNG_DOMAIN_UST
:
2467 if (session
->ust_session
!= NULL
) {
2468 nb_chan
= lttng_ht_get_count(
2469 session
->ust_session
->domain_global
.channels
);
2471 DBG3("Number of UST global channels %zd", nb_chan
);
2473 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2478 ret
= LTTNG_ERR_UND
;
2483 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2484 if (*channels
== NULL
) {
2485 ret
= LTTNG_ERR_FATAL
;
2489 list_lttng_channels(domain
, session
, *channels
);
2495 /* Return negative value to differentiate return code */
2500 * Command LTTNG_LIST_EVENTS processed by the client thread.
2502 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2503 char *channel_name
, struct lttng_event
**events
)
2506 ssize_t nb_event
= 0;
2509 case LTTNG_DOMAIN_KERNEL
:
2510 if (session
->kernel_session
!= NULL
) {
2511 nb_event
= list_lttng_kernel_events(channel_name
,
2512 session
->kernel_session
, events
);
2515 case LTTNG_DOMAIN_UST
:
2517 if (session
->ust_session
!= NULL
) {
2518 nb_event
= list_lttng_ust_global_events(channel_name
,
2519 &session
->ust_session
->domain_global
, events
);
2523 case LTTNG_DOMAIN_LOG4J
:
2524 case LTTNG_DOMAIN_JUL
:
2525 if (session
->ust_session
) {
2526 struct lttng_ht_iter iter
;
2529 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2530 &iter
.iter
, agt
, node
.node
) {
2531 nb_event
= list_lttng_agent_events(agt
, events
);
2536 ret
= LTTNG_ERR_UND
;
2543 /* Return negative value to differentiate return code */
2548 * Using the session list, filled a lttng_session array to send back to the
2549 * client for session listing.
2551 * The session list lock MUST be acquired before calling this function. Use
2552 * session_lock_list() and session_unlock_list().
2554 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2559 struct ltt_session
*session
;
2560 struct ltt_session_list
*list
= session_get_list();
2562 DBG("Getting all available session for UID %d GID %d",
2565 * Iterate over session list and append data after the control struct in
2568 cds_list_for_each_entry(session
, &list
->head
, list
) {
2570 * Only list the sessions the user can control.
2572 if (!session_access_ok(session
, uid
, gid
)) {
2576 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2577 struct ltt_ust_session
*usess
= session
->ust_session
;
2579 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2580 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2581 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2582 ret
= build_network_session_path(sessions
[i
].path
,
2583 sizeof(sessions
[i
].path
), session
);
2585 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2586 session
->consumer
->dst
.trace_path
);
2589 PERROR("snprintf session path");
2593 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2594 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2595 sessions
[i
].enabled
= session
->active
;
2596 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2597 sessions
[i
].live_timer_interval
= session
->live_timer
;
2603 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2604 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2606 int cmd_data_pending(struct ltt_session
*session
)
2609 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2610 struct ltt_ust_session
*usess
= session
->ust_session
;
2614 /* Session MUST be stopped to ask for data availability. */
2615 if (session
->active
) {
2616 ret
= LTTNG_ERR_SESSION_STARTED
;
2620 * If stopped, just make sure we've started before else the above call
2621 * will always send that there is data pending.
2623 * The consumer assumes that when the data pending command is received,
2624 * the trace has been started before or else no output data is written
2625 * by the streams which is a condition for data pending. So, this is
2626 * *VERY* important that we don't ask the consumer before a start
2629 if (!session
->has_been_started
) {
2635 if (ksess
&& ksess
->consumer
) {
2636 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2638 /* Data is still being extracted for the kernel. */
2643 if (usess
&& usess
->consumer
) {
2644 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2646 /* Data is still being extracted for the kernel. */
2651 /* Data is ready to be read by a viewer */
2659 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2661 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2663 int cmd_snapshot_add_output(struct ltt_session
*session
,
2664 struct lttng_snapshot_output
*output
, uint32_t *id
)
2667 struct snapshot_output
*new_output
;
2672 DBG("Cmd snapshot add output for session %s", session
->name
);
2675 * Permission denied to create an output if the session is not
2676 * set in no output mode.
2678 if (session
->output_traces
) {
2679 ret
= LTTNG_ERR_EPERM
;
2683 /* Only one output is allowed until we have the "tee" feature. */
2684 if (session
->snapshot
.nb_output
== 1) {
2685 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2689 new_output
= snapshot_output_alloc();
2691 ret
= LTTNG_ERR_NOMEM
;
2695 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2696 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2697 &session
->snapshot
);
2699 if (ret
== -ENOMEM
) {
2700 ret
= LTTNG_ERR_NOMEM
;
2702 ret
= LTTNG_ERR_INVALID
;
2708 snapshot_add_output(&session
->snapshot
, new_output
);
2710 *id
= new_output
->id
;
2717 snapshot_output_destroy(new_output
);
2723 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2725 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2727 int cmd_snapshot_del_output(struct ltt_session
*session
,
2728 struct lttng_snapshot_output
*output
)
2731 struct snapshot_output
*sout
= NULL
;
2739 * Permission denied to create an output if the session is not
2740 * set in no output mode.
2742 if (session
->output_traces
) {
2743 ret
= LTTNG_ERR_EPERM
;
2748 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2750 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2751 } else if (*output
->name
!= '\0') {
2752 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2754 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2757 ret
= LTTNG_ERR_INVALID
;
2761 snapshot_delete_output(&session
->snapshot
, sout
);
2762 snapshot_output_destroy(sout
);
2771 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2773 * If no output is available, outputs is untouched and 0 is returned.
2775 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2777 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2778 struct lttng_snapshot_output
**outputs
)
2781 struct lttng_snapshot_output
*list
;
2782 struct lttng_ht_iter iter
;
2783 struct snapshot_output
*output
;
2788 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2791 * Permission denied to create an output if the session is not
2792 * set in no output mode.
2794 if (session
->output_traces
) {
2795 ret
= LTTNG_ERR_EPERM
;
2799 if (session
->snapshot
.nb_output
== 0) {
2804 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2806 ret
= LTTNG_ERR_NOMEM
;
2810 /* Copy list from session to the new list object. */
2811 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2812 output
, node
.node
) {
2813 assert(output
->consumer
);
2814 list
[idx
].id
= output
->id
;
2815 list
[idx
].max_size
= output
->max_size
;
2816 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2817 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2818 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2819 sizeof(list
[idx
].ctrl_url
));
2822 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2823 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2825 ret
= LTTNG_ERR_NOMEM
;
2830 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2831 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2833 ret
= LTTNG_ERR_NOMEM
;
2841 return session
->snapshot
.nb_output
;
2850 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2851 * snapshot output is *not* set with a remote destination.
2853 * Return 0 on success or a LTTNG_ERR code.
2855 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2856 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2859 struct lttng_ht_iter iter
;
2860 struct consumer_socket
*socket
;
2863 assert(snap_output
);
2866 DBG2("Set relayd object from snapshot output");
2868 /* Ignore if snapshot consumer output is not network. */
2869 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2874 * For each consumer socket, create and send the relayd object of the
2878 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2879 socket
, node
.node
) {
2880 ret
= send_consumer_relayd_sockets(0, session
->id
,
2881 snap_output
->consumer
, socket
,
2882 session
->name
, session
->hostname
,
2883 session
->live_timer
);
2884 if (ret
!= LTTNG_OK
) {
2896 * Record a kernel snapshot.
2898 * Return LTTNG_OK on success or a LTTNG_ERR code.
2900 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2901 struct snapshot_output
*output
, struct ltt_session
*session
,
2902 int wait
, uint64_t max_stream_size
)
2910 /* Get the datetime for the snapshot output directory. */
2911 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2912 sizeof(output
->datetime
));
2914 ret
= LTTNG_ERR_INVALID
;
2919 * Copy kernel session sockets so we can communicate with the right
2920 * consumer for the snapshot record command.
2922 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2924 ret
= LTTNG_ERR_NOMEM
;
2928 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2929 if (ret
!= LTTNG_OK
) {
2930 goto error_snapshot
;
2933 ret
= kernel_snapshot_record(ksess
, output
, wait
, max_stream_size
);
2934 if (ret
!= LTTNG_OK
) {
2935 goto error_snapshot
;
2941 /* Clean up copied sockets so this output can use some other later on. */
2942 consumer_destroy_output_sockets(output
->consumer
);
2948 * Record a UST snapshot.
2950 * Return 0 on success or a LTTNG_ERR error code.
2952 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2953 struct snapshot_output
*output
, struct ltt_session
*session
,
2954 int wait
, uint64_t max_stream_size
)
2962 /* Get the datetime for the snapshot output directory. */
2963 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2964 sizeof(output
->datetime
));
2966 ret
= LTTNG_ERR_INVALID
;
2971 * Copy UST session sockets so we can communicate with the right
2972 * consumer for the snapshot record command.
2974 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
2976 ret
= LTTNG_ERR_NOMEM
;
2980 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
2981 if (ret
!= LTTNG_OK
) {
2982 goto error_snapshot
;
2985 ret
= ust_app_snapshot_record(usess
, output
, wait
, max_stream_size
);
2989 ret
= LTTNG_ERR_INVALID
;
2992 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
2995 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
2998 goto error_snapshot
;
3004 /* Clean up copied sockets so this output can use some other later on. */
3005 consumer_destroy_output_sockets(output
->consumer
);
3011 * Return the biggest subbuffer size of all channels in the given session.
3013 static uint64_t get_session_max_subbuf_size(struct ltt_session
*session
)
3015 uint64_t max_size
= 0;
3019 if (session
->kernel_session
) {
3020 struct ltt_kernel_channel
*chan
;
3021 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3024 * For each channel, add to the max size the size of each subbuffer
3025 * multiplied by their sized.
3027 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3028 if (chan
->channel
->attr
.subbuf_size
> max_size
) {
3029 max_size
= chan
->channel
->attr
.subbuf_size
;
3034 if (session
->ust_session
) {
3035 struct lttng_ht_iter iter
;
3036 struct ltt_ust_channel
*uchan
;
3037 struct ltt_ust_session
*usess
= session
->ust_session
;
3040 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
3042 if (uchan
->attr
.subbuf_size
> max_size
) {
3043 max_size
= uchan
->attr
.subbuf_size
;
3053 * Returns the total number of streams for a session or a negative value
3056 static unsigned int get_session_nb_streams(struct ltt_session
*session
)
3058 unsigned int total_streams
= 0;
3060 if (session
->kernel_session
) {
3061 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3063 total_streams
+= ksess
->stream_count_global
;
3066 if (session
->ust_session
) {
3067 struct ltt_ust_session
*usess
= session
->ust_session
;
3069 total_streams
+= ust_app_get_nb_stream(usess
);
3072 return total_streams
;
3076 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3078 * The wait parameter is ignored so this call always wait for the snapshot to
3079 * complete before returning.
3081 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3083 int cmd_snapshot_record(struct ltt_session
*session
,
3084 struct lttng_snapshot_output
*output
, int wait
)
3087 unsigned int use_tmp_output
= 0;
3088 struct snapshot_output tmp_output
;
3089 unsigned int nb_streams
, snapshot_success
= 0;
3090 uint64_t session_max_size
= 0, max_stream_size
= 0;
3095 DBG("Cmd snapshot record for session %s", session
->name
);
3098 * Permission denied to create an output if the session is not
3099 * set in no output mode.
3101 if (session
->output_traces
) {
3102 ret
= LTTNG_ERR_EPERM
;
3106 /* The session needs to be started at least once. */
3107 if (!session
->has_been_started
) {
3108 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3112 /* Use temporary output for the session. */
3113 if (*output
->ctrl_url
!= '\0') {
3114 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3115 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3118 if (ret
== -ENOMEM
) {
3119 ret
= LTTNG_ERR_NOMEM
;
3121 ret
= LTTNG_ERR_INVALID
;
3125 /* Use the global session count for the temporary snapshot. */
3126 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3131 * Get the session maximum size for a snapshot meaning it will compute the
3132 * size of all streams from all domain.
3134 max_stream_size
= get_session_max_subbuf_size(session
);
3136 nb_streams
= get_session_nb_streams(session
);
3139 * The maximum size of the snapshot is the number of streams multiplied
3140 * by the biggest subbuf size of all channels in a session which is the
3141 * maximum stream size available for each stream. The session max size
3142 * is now checked against the snapshot max size value given by the user
3143 * and if lower, an error is returned.
3145 session_max_size
= max_stream_size
* nb_streams
;
3148 DBG3("Snapshot max size is %" PRIu64
" for max stream size of %" PRIu64
,
3149 session_max_size
, max_stream_size
);
3152 * If we use a temporary output, check right away if the max size fits else
3153 * for each output the max size will be checked.
3155 if (use_tmp_output
&&
3156 (tmp_output
.max_size
!= 0 &&
3157 tmp_output
.max_size
< session_max_size
)) {
3158 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3162 if (session
->kernel_session
) {
3163 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3165 if (use_tmp_output
) {
3166 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3167 wait
, max_stream_size
);
3168 if (ret
!= LTTNG_OK
) {
3171 snapshot_success
= 1;
3173 struct snapshot_output
*sout
;
3174 struct lttng_ht_iter iter
;
3177 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3178 &iter
.iter
, sout
, node
.node
) {
3180 * Make a local copy of the output and assign the possible
3181 * temporary value given by the caller.
3183 memset(&tmp_output
, 0, sizeof(tmp_output
));
3184 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3186 /* Use temporary max size. */
3187 if (output
->max_size
!= (uint64_t) -1ULL) {
3188 tmp_output
.max_size
= output
->max_size
;
3191 if (tmp_output
.max_size
!= 0 &&
3192 tmp_output
.max_size
< session_max_size
) {
3194 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3198 /* Use temporary name. */
3199 if (*output
->name
!= '\0') {
3200 strncpy(tmp_output
.name
, output
->name
,
3201 sizeof(tmp_output
.name
));
3204 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3206 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3207 session
, wait
, max_stream_size
);
3208 if (ret
!= LTTNG_OK
) {
3212 snapshot_success
= 1;
3218 if (session
->ust_session
) {
3219 struct ltt_ust_session
*usess
= session
->ust_session
;
3221 if (use_tmp_output
) {
3222 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3223 wait
, max_stream_size
);
3224 if (ret
!= LTTNG_OK
) {
3227 snapshot_success
= 1;
3229 struct snapshot_output
*sout
;
3230 struct lttng_ht_iter iter
;
3233 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3234 &iter
.iter
, sout
, node
.node
) {
3236 * Make a local copy of the output and assign the possible
3237 * temporary value given by the caller.
3239 memset(&tmp_output
, 0, sizeof(tmp_output
));
3240 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3242 /* Use temporary max size. */
3243 if (output
->max_size
!= (uint64_t) -1ULL) {
3244 tmp_output
.max_size
= output
->max_size
;
3247 if (tmp_output
.max_size
!= 0 &&
3248 tmp_output
.max_size
< session_max_size
) {
3250 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3254 /* Use temporary name. */
3255 if (*output
->name
!= '\0') {
3256 strncpy(tmp_output
.name
, output
->name
,
3257 sizeof(tmp_output
.name
));
3260 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3262 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3263 wait
, max_stream_size
);
3264 if (ret
!= LTTNG_OK
) {
3268 snapshot_success
= 1;
3274 if (snapshot_success
) {
3275 session
->snapshot
.nb_snapshot
++;
3277 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3285 * Init command subsystem.
3290 * Set network sequence index to 1 for streams to match a relayd
3291 * socket on the consumer side.
3293 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3294 relayd_net_seq_idx
= 1;
3295 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3297 DBG("Command subsystem initialized");