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 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
178 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
179 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
180 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
181 channels
[i
].attr
.switch_timer_interval
=
182 uchan
->attr
.switch_timer_interval
;
183 channels
[i
].attr
.read_timer_interval
=
184 uchan
->attr
.read_timer_interval
;
185 channels
[i
].enabled
= uchan
->enabled
;
186 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
187 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
188 switch (uchan
->attr
.output
) {
191 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
205 * Create a list of agent domain events.
207 * Return number of events in list on success or else a negative value.
209 static int list_lttng_agent_events(struct agent
*agt
,
210 struct lttng_event
**events
)
213 unsigned int nb_event
= 0;
214 struct agent_event
*event
;
215 struct lttng_event
*tmp_events
;
216 struct lttng_ht_iter iter
;
221 DBG3("Listing agent events");
224 nb_event
= lttng_ht_get_count(agt
->events
);
231 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
233 PERROR("zmalloc agent events session");
234 ret
= -LTTNG_ERR_FATAL
;
239 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
240 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
241 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
242 tmp_events
[i
].enabled
= event
->enabled
;
243 tmp_events
[i
].loglevel
= event
->loglevel
;
244 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
249 *events
= tmp_events
;
253 assert(nb_event
== i
);
258 * Create a list of ust global domain events.
260 static int list_lttng_ust_global_events(char *channel_name
,
261 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
264 unsigned int nb_event
= 0;
265 struct lttng_ht_iter iter
;
266 struct lttng_ht_node_str
*node
;
267 struct ltt_ust_channel
*uchan
;
268 struct ltt_ust_event
*uevent
;
269 struct lttng_event
*tmp
;
271 DBG("Listing UST global events for channel %s", channel_name
);
275 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
276 node
= lttng_ht_iter_get_node_str(&iter
);
278 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
282 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
284 nb_event
+= lttng_ht_get_count(uchan
->events
);
291 DBG3("Listing UST global %d events", nb_event
);
293 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
295 ret
= LTTNG_ERR_FATAL
;
299 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
300 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
301 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
302 tmp
[i
].enabled
= uevent
->enabled
;
304 switch (uevent
->attr
.instrumentation
) {
305 case LTTNG_UST_TRACEPOINT
:
306 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
308 case LTTNG_UST_PROBE
:
309 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
311 case LTTNG_UST_FUNCTION
:
312 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
316 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
317 switch (uevent
->attr
.loglevel_type
) {
318 case LTTNG_UST_LOGLEVEL_ALL
:
319 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
321 case LTTNG_UST_LOGLEVEL_RANGE
:
322 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
324 case LTTNG_UST_LOGLEVEL_SINGLE
:
325 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
328 if (uevent
->filter
) {
331 if (uevent
->exclusion
) {
332 tmp
[i
].exclusion
= 1;
346 * Fill lttng_event array of all kernel events in the channel.
348 static int list_lttng_kernel_events(char *channel_name
,
349 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
352 unsigned int nb_event
;
353 struct ltt_kernel_event
*event
;
354 struct ltt_kernel_channel
*kchan
;
356 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
358 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
362 nb_event
= kchan
->event_count
;
364 DBG("Listing events for channel %s", kchan
->channel
->name
);
371 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
372 if (*events
== NULL
) {
373 ret
= LTTNG_ERR_FATAL
;
377 /* Kernel channels */
378 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
379 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
380 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
381 (*events
)[i
].enabled
= event
->enabled
;
383 switch (event
->event
->instrumentation
) {
384 case LTTNG_KERNEL_TRACEPOINT
:
385 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
387 case LTTNG_KERNEL_KRETPROBE
:
388 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
389 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
390 sizeof(struct lttng_kernel_kprobe
));
392 case LTTNG_KERNEL_KPROBE
:
393 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
394 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
395 sizeof(struct lttng_kernel_kprobe
));
397 case LTTNG_KERNEL_FUNCTION
:
398 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
399 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
400 sizeof(struct lttng_kernel_function
));
402 case LTTNG_KERNEL_NOOP
:
403 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
405 case LTTNG_KERNEL_SYSCALL
:
406 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
408 case LTTNG_KERNEL_ALL
:
419 new_size
= syscall_list_channel(kchan
, events
, nb_event
);
431 /* Negate the error code to differentiate the size from an error */
436 * Add URI so the consumer output object. Set the correct path depending on the
437 * domain adding the default trace directory.
439 static int add_uri_to_consumer(struct consumer_output
*consumer
,
440 struct lttng_uri
*uri
, int domain
, const char *session_name
)
443 const char *default_trace_dir
;
447 if (consumer
== NULL
) {
448 DBG("No consumer detected. Don't add URI. Stopping.");
449 ret
= LTTNG_ERR_NO_CONSUMER
;
454 case LTTNG_DOMAIN_KERNEL
:
455 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
457 case LTTNG_DOMAIN_UST
:
458 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
462 * This case is possible is we try to add the URI to the global tracing
463 * session consumer object which in this case there is no subdir.
465 default_trace_dir
= "";
468 switch (uri
->dtype
) {
471 DBG2("Setting network URI to consumer");
473 if (consumer
->type
== CONSUMER_DST_NET
) {
474 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
475 consumer
->dst
.net
.control_isset
) ||
476 (uri
->stype
== LTTNG_STREAM_DATA
&&
477 consumer
->dst
.net
.data_isset
)) {
478 ret
= LTTNG_ERR_URL_EXIST
;
482 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
485 consumer
->type
= CONSUMER_DST_NET
;
487 /* Set URI into consumer output object */
488 ret
= consumer_set_network_uri(consumer
, uri
);
492 } else if (ret
== 1) {
494 * URI was the same in the consumer so we do not append the subdir
495 * again so to not duplicate output dir.
501 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
502 ret
= consumer_set_subdir(consumer
, session_name
);
504 ret
= LTTNG_ERR_FATAL
;
509 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
510 /* On a new subdir, reappend the default trace dir. */
511 strncat(consumer
->subdir
, default_trace_dir
,
512 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
513 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
518 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
519 memset(consumer
->dst
.trace_path
, 0,
520 sizeof(consumer
->dst
.trace_path
));
521 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
522 sizeof(consumer
->dst
.trace_path
));
523 /* Append default trace dir */
524 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
525 sizeof(consumer
->dst
.trace_path
) -
526 strlen(consumer
->dst
.trace_path
) - 1);
527 /* Flag consumer as local. */
528 consumer
->type
= CONSUMER_DST_LOCAL
;
539 * Init tracing by creating trace directory and sending fds kernel consumer.
541 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
544 struct lttng_ht_iter iter
;
545 struct consumer_socket
*socket
;
551 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
552 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
554 pthread_mutex_lock(socket
->lock
);
555 ret
= kernel_consumer_send_session(socket
, session
);
556 pthread_mutex_unlock(socket
->lock
);
558 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
570 * Create a socket to the relayd using the URI.
572 * On success, the relayd_sock pointer is set to the created socket.
573 * Else, it's stays untouched and a lttcomm error code is returned.
575 static int create_connect_relayd(struct lttng_uri
*uri
,
576 struct lttcomm_relayd_sock
**relayd_sock
)
579 struct lttcomm_relayd_sock
*rsock
;
581 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
582 RELAYD_VERSION_COMM_MINOR
);
584 ret
= LTTNG_ERR_FATAL
;
589 * Connect to relayd so we can proceed with a session creation. This call
590 * can possibly block for an arbitrary amount of time to set the health
591 * state to be in poll execution.
594 ret
= relayd_connect(rsock
);
597 ERR("Unable to reach lttng-relayd");
598 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
602 /* Create socket for control stream. */
603 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
604 DBG3("Creating relayd stream socket from URI");
606 /* Check relayd version */
607 ret
= relayd_version_check(rsock
);
609 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
612 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
613 DBG3("Creating relayd data socket from URI");
615 /* Command is not valid */
616 ERR("Relayd invalid stream type: %d", uri
->stype
);
617 ret
= LTTNG_ERR_INVALID
;
621 *relayd_sock
= rsock
;
626 /* The returned value is not useful since we are on an error path. */
627 (void) relayd_close(rsock
);
635 * Connect to the relayd using URI and send the socket to the right consumer.
637 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
638 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
639 struct consumer_socket
*consumer_sock
,
640 char *session_name
, char *hostname
, int session_live_timer
)
643 struct lttcomm_relayd_sock
*rsock
= NULL
;
645 /* Connect to relayd and make version check if uri is the control. */
646 ret
= create_connect_relayd(relayd_uri
, &rsock
);
647 if (ret
!= LTTNG_OK
) {
652 /* Set the network sequence index if not set. */
653 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
654 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
656 * Increment net_seq_idx because we are about to transfer the
657 * new relayd socket to the consumer.
658 * Assign unique key so the consumer can match streams.
660 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
661 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
664 /* Send relayd socket to consumer. */
665 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
666 relayd_uri
->stype
, session_id
,
667 session_name
, hostname
, session_live_timer
);
669 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
673 /* Flag that the corresponding socket was sent. */
674 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
675 consumer_sock
->control_sock_sent
= 1;
676 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
677 consumer_sock
->data_sock_sent
= 1;
683 * Close socket which was dup on the consumer side. The session daemon does
684 * NOT keep track of the relayd socket(s) once transfer to the consumer.
688 (void) relayd_close(rsock
);
692 if (ret
!= LTTNG_OK
) {
694 * The consumer output for this session should not be used anymore
695 * since the relayd connection failed thus making any tracing or/and
696 * streaming not usable.
698 consumer
->enabled
= 0;
704 * Send both relayd sockets to a specific consumer and domain. This is a
705 * helper function to facilitate sending the information to the consumer for a
708 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
709 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
710 char *session_name
, char *hostname
, int session_live_timer
)
717 /* Sending control relayd socket. */
718 if (!sock
->control_sock_sent
) {
719 ret
= send_consumer_relayd_socket(domain
, session_id
,
720 &consumer
->dst
.net
.control
, consumer
, sock
,
721 session_name
, hostname
, session_live_timer
);
722 if (ret
!= LTTNG_OK
) {
727 /* Sending data relayd socket. */
728 if (!sock
->data_sock_sent
) {
729 ret
= send_consumer_relayd_socket(domain
, session_id
,
730 &consumer
->dst
.net
.data
, consumer
, sock
,
731 session_name
, hostname
, session_live_timer
);
732 if (ret
!= LTTNG_OK
) {
742 * Setup relayd connections for a tracing session. First creates the socket to
743 * the relayd and send them to the right domain consumer. Consumer type MUST be
746 int cmd_setup_relayd(struct ltt_session
*session
)
749 struct ltt_ust_session
*usess
;
750 struct ltt_kernel_session
*ksess
;
751 struct consumer_socket
*socket
;
752 struct lttng_ht_iter iter
;
756 usess
= session
->ust_session
;
757 ksess
= session
->kernel_session
;
759 DBG("Setting relayd for session %s", session
->name
);
763 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
764 && usess
->consumer
->enabled
) {
765 /* For each consumer socket, send relayd sockets */
766 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
768 pthread_mutex_lock(socket
->lock
);
769 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
770 usess
->consumer
, socket
,
771 session
->name
, session
->hostname
,
772 session
->live_timer
);
773 pthread_mutex_unlock(socket
->lock
);
774 if (ret
!= LTTNG_OK
) {
777 /* Session is now ready for network streaming. */
778 session
->net_handle
= 1;
782 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
783 && ksess
->consumer
->enabled
) {
784 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
786 pthread_mutex_lock(socket
->lock
);
787 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
788 ksess
->consumer
, socket
,
789 session
->name
, session
->hostname
,
790 session
->live_timer
);
791 pthread_mutex_unlock(socket
->lock
);
792 if (ret
!= LTTNG_OK
) {
795 /* Session is now ready for network streaming. */
796 session
->net_handle
= 1;
806 * Start a kernel session by opening all necessary streams.
808 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
811 struct ltt_kernel_channel
*kchan
;
813 /* Open kernel metadata */
814 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
815 ret
= kernel_open_metadata(ksess
);
817 ret
= LTTNG_ERR_KERN_META_FAIL
;
822 /* Open kernel metadata stream */
823 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
824 ret
= kernel_open_metadata_stream(ksess
);
826 ERR("Kernel create metadata stream failed");
827 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
832 /* For each channel */
833 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
834 if (kchan
->stream_count
== 0) {
835 ret
= kernel_open_channel_stream(kchan
);
837 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
840 /* Update the stream global counter */
841 ksess
->stream_count_global
+= ret
;
845 /* Setup kernel consumer socket and send fds to it */
846 ret
= init_kernel_tracing(ksess
);
848 ret
= LTTNG_ERR_KERN_START_FAIL
;
852 /* This start the kernel tracing */
853 ret
= kernel_start_session(ksess
);
855 ret
= LTTNG_ERR_KERN_START_FAIL
;
859 /* Quiescent wait after starting trace */
860 kernel_wait_quiescent(kernel_tracer_fd
);
871 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
873 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
877 struct ltt_ust_session
*usess
;
879 usess
= session
->ust_session
;
884 case LTTNG_DOMAIN_KERNEL
:
886 ret
= channel_kernel_disable(session
->kernel_session
,
888 if (ret
!= LTTNG_OK
) {
892 kernel_wait_quiescent(kernel_tracer_fd
);
895 case LTTNG_DOMAIN_UST
:
897 struct ltt_ust_channel
*uchan
;
898 struct lttng_ht
*chan_ht
;
900 chan_ht
= usess
->domain_global
.channels
;
902 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
904 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
908 ret
= channel_ust_disable(usess
, uchan
);
909 if (ret
!= LTTNG_OK
) {
915 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
916 case LTTNG_DOMAIN_UST_EXEC_NAME
:
917 case LTTNG_DOMAIN_UST_PID
:
920 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
932 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
934 * The wpipe arguments is used as a notifier for the kernel thread.
936 int cmd_enable_channel(struct ltt_session
*session
,
937 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
940 struct ltt_ust_session
*usess
= session
->ust_session
;
941 struct lttng_ht
*chan_ht
;
947 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
952 * Don't try to enable a channel if the session has been started at
953 * some point in time before. The tracer does not allow it.
955 if (session
->has_been_started
) {
956 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
961 * If the session is a live session, remove the switch timer, the
962 * live timer does the same thing but sends also synchronisation
963 * beacons for inactive streams.
965 if (session
->live_timer
> 0) {
966 attr
->attr
.live_timer_interval
= session
->live_timer
;
967 attr
->attr
.switch_timer_interval
= 0;
971 * The ringbuffer (both in user space and kernel) behave badly in overwrite
972 * mode and with less than 2 subbuf so block it right away and send back an
973 * invalid attribute error.
975 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
976 ret
= LTTNG_ERR_INVALID
;
980 switch (domain
->type
) {
981 case LTTNG_DOMAIN_KERNEL
:
983 struct ltt_kernel_channel
*kchan
;
985 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
986 session
->kernel_session
);
988 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
989 if (attr
->name
[0] != '\0') {
990 session
->kernel_session
->has_non_default_channel
= 1;
993 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
996 if (ret
!= LTTNG_OK
) {
1000 kernel_wait_quiescent(kernel_tracer_fd
);
1003 case LTTNG_DOMAIN_UST
:
1005 struct ltt_ust_channel
*uchan
;
1007 chan_ht
= usess
->domain_global
.channels
;
1009 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1010 if (uchan
== NULL
) {
1011 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1012 if (attr
->name
[0] != '\0') {
1013 usess
->has_non_default_channel
= 1;
1016 ret
= channel_ust_enable(usess
, uchan
);
1021 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1032 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1034 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1036 struct lttng_event
*event
)
1041 DBG("Disable event command for event \'%s\'", event
->name
);
1043 event_name
= event
->name
;
1045 /* Error out on unhandled search criteria */
1046 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1047 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1048 return LTTNG_ERR_UNK
;
1050 /* Special handling for kernel domain all events. */
1051 if (domain
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event_name
, "*")) {
1052 return disable_kevent_all(session
, domain
, channel_name
, event
);
1058 case LTTNG_DOMAIN_KERNEL
:
1060 struct ltt_kernel_channel
*kchan
;
1061 struct ltt_kernel_session
*ksess
;
1063 ksess
= session
->kernel_session
;
1066 * If a non-default channel has been created in the
1067 * session, explicitely require that -c chan_name needs
1070 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1071 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1075 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1076 if (kchan
== NULL
) {
1077 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1081 switch (event
->type
) {
1082 case LTTNG_EVENT_ALL
:
1083 case LTTNG_EVENT_TRACEPOINT
:
1084 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1085 if (ret
!= LTTNG_OK
) {
1089 case LTTNG_EVENT_SYSCALL
:
1090 ret
= event_kernel_disable_syscall(kchan
, event_name
);
1091 if (ret
!= LTTNG_OK
) {
1096 ret
= LTTNG_ERR_UNK
;
1100 kernel_wait_quiescent(kernel_tracer_fd
);
1103 case LTTNG_DOMAIN_UST
:
1105 struct ltt_ust_channel
*uchan
;
1106 struct ltt_ust_session
*usess
;
1108 usess
= session
->ust_session
;
1111 * If a non-default channel has been created in the
1112 * session, explicitely require that -c chan_name needs
1115 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1116 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1120 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1122 if (uchan
== NULL
) {
1123 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1127 switch (event
->type
) {
1128 case LTTNG_EVENT_ALL
:
1129 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1130 if (ret
!= LTTNG_OK
) {
1135 ret
= LTTNG_ERR_UNK
;
1139 DBG3("Disable UST event %s in channel %s completed", event_name
,
1143 case LTTNG_DOMAIN_LOG4J
:
1144 case LTTNG_DOMAIN_JUL
:
1145 case LTTNG_DOMAIN_PYTHON
:
1148 struct ltt_ust_session
*usess
= session
->ust_session
;
1152 switch (event
->type
) {
1153 case LTTNG_EVENT_ALL
:
1156 ret
= LTTNG_ERR_UNK
;
1160 agt
= trace_ust_find_agent(usess
, domain
);
1162 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1165 /* The wild card * means that everything should be disabled. */
1166 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1167 ret
= event_agent_disable_all(usess
, agt
);
1169 ret
= event_agent_disable(usess
, agt
, event_name
);
1171 if (ret
!= LTTNG_OK
) {
1178 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1179 case LTTNG_DOMAIN_UST_PID
:
1180 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1183 ret
= LTTNG_ERR_UND
;
1195 * Command LTTNG_DISABLE_EVENT for event "*" processed by the client thread.
1198 int disable_kevent_all(struct ltt_session
*session
, int domain
,
1200 struct lttng_event
*event
)
1207 case LTTNG_DOMAIN_KERNEL
:
1209 struct ltt_kernel_session
*ksess
;
1210 struct ltt_kernel_channel
*kchan
;
1212 ksess
= session
->kernel_session
;
1215 * If a non-default channel has been created in the
1216 * session, explicitely require that -c chan_name needs
1219 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1220 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1224 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1225 if (kchan
== NULL
) {
1226 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1230 switch (event
->type
) {
1231 case LTTNG_EVENT_ALL
:
1232 ret
= event_kernel_disable_all(kchan
);
1233 if (ret
!= LTTNG_OK
) {
1237 case LTTNG_EVENT_SYSCALL
:
1238 ret
= event_kernel_disable_syscall(kchan
, "");
1239 if (ret
!= LTTNG_OK
) {
1244 ret
= LTTNG_ERR_UNK
;
1248 kernel_wait_quiescent(kernel_tracer_fd
);
1252 ret
= LTTNG_ERR_UND
;
1264 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1266 int cmd_add_context(struct ltt_session
*session
, int domain
,
1267 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1269 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1272 case LTTNG_DOMAIN_KERNEL
:
1273 assert(session
->kernel_session
);
1275 if (session
->kernel_session
->channel_count
== 0) {
1276 /* Create default channel */
1277 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1278 if (ret
!= LTTNG_OK
) {
1281 chan_kern_created
= 1;
1283 /* Add kernel context to kernel tracer */
1284 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1285 if (ret
!= LTTNG_OK
) {
1289 case LTTNG_DOMAIN_UST
:
1291 struct ltt_ust_session
*usess
= session
->ust_session
;
1292 unsigned int chan_count
;
1296 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1297 if (chan_count
== 0) {
1298 struct lttng_channel
*attr
;
1299 /* Create default channel */
1300 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1302 ret
= LTTNG_ERR_FATAL
;
1306 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1307 if (ret
!= LTTNG_OK
) {
1312 chan_ust_created
= 1;
1315 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1316 if (ret
!= LTTNG_OK
) {
1322 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1323 case LTTNG_DOMAIN_UST_PID
:
1324 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1327 ret
= LTTNG_ERR_UND
;
1334 if (chan_kern_created
) {
1335 struct ltt_kernel_channel
*kchan
=
1336 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1337 session
->kernel_session
);
1338 /* Created previously, this should NOT fail. */
1340 kernel_destroy_channel(kchan
);
1343 if (chan_ust_created
) {
1344 struct ltt_ust_channel
*uchan
=
1345 trace_ust_find_channel_by_name(
1346 session
->ust_session
->domain_global
.channels
,
1347 DEFAULT_CHANNEL_NAME
);
1348 /* Created previously, this should NOT fail. */
1350 /* Remove from the channel list of the session. */
1351 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1353 trace_ust_destroy_channel(uchan
);
1358 static int validate_event_name(const char *name
)
1361 const char *c
= name
;
1362 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1365 * Make sure that unescaped wildcards are only used as the last
1366 * character of the event name.
1368 while (c
< event_name_end
) {
1376 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1377 /* Wildcard is not the last character */
1378 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1391 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1392 * We own filter, exclusion, and filter_expression.
1394 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1395 char *channel_name
, struct lttng_event
*event
,
1396 char *filter_expression
,
1397 struct lttng_filter_bytecode
*filter
,
1398 struct lttng_event_exclusion
*exclusion
,
1401 int ret
, channel_created
= 0;
1402 struct lttng_channel
*attr
;
1406 assert(channel_name
);
1408 DBG("Enable event command for event \'%s\'", event
->name
);
1410 /* Special handling for kernel domain all events. */
1411 if (domain
->type
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event
->name
, "*")) {
1412 return enable_kevent_all(session
, domain
, channel_name
, event
,
1413 filter_expression
, filter
, wpipe
);
1416 ret
= validate_event_name(event
->name
);
1423 switch (domain
->type
) {
1424 case LTTNG_DOMAIN_KERNEL
:
1426 struct ltt_kernel_channel
*kchan
;
1429 * If a non-default channel has been created in the
1430 * session, explicitely require that -c chan_name needs
1433 if (session
->kernel_session
->has_non_default_channel
1434 && channel_name
[0] == '\0') {
1435 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1439 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1440 session
->kernel_session
);
1441 if (kchan
== NULL
) {
1442 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1443 LTTNG_BUFFER_GLOBAL
);
1445 ret
= LTTNG_ERR_FATAL
;
1448 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1450 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1451 if (ret
!= LTTNG_OK
) {
1457 channel_created
= 1;
1460 /* Get the newly created kernel channel pointer */
1461 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1462 session
->kernel_session
);
1463 if (kchan
== NULL
) {
1464 /* This sould not happen... */
1465 ret
= LTTNG_ERR_FATAL
;
1469 switch (event
->type
) {
1470 case LTTNG_EVENT_ALL
:
1471 case LTTNG_EVENT_PROBE
:
1472 case LTTNG_EVENT_FUNCTION
:
1473 case LTTNG_EVENT_FUNCTION_ENTRY
:
1474 case LTTNG_EVENT_TRACEPOINT
:
1475 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1476 if (ret
!= LTTNG_OK
) {
1477 if (channel_created
) {
1478 /* Let's not leak a useless channel. */
1479 kernel_destroy_channel(kchan
);
1484 case LTTNG_EVENT_SYSCALL
:
1485 ret
= event_kernel_enable_syscall(kchan
, event
->name
);
1486 if (ret
!= LTTNG_OK
) {
1491 ret
= LTTNG_ERR_UNK
;
1495 kernel_wait_quiescent(kernel_tracer_fd
);
1498 case LTTNG_DOMAIN_UST
:
1500 struct ltt_ust_channel
*uchan
;
1501 struct ltt_ust_session
*usess
= session
->ust_session
;
1506 * If a non-default channel has been created in the
1507 * session, explicitely require that -c chan_name needs
1510 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1511 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1515 /* Get channel from global UST domain */
1516 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1518 if (uchan
== NULL
) {
1519 /* Create default channel */
1520 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1521 usess
->buffer_type
);
1523 ret
= LTTNG_ERR_FATAL
;
1526 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1528 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1529 if (ret
!= LTTNG_OK
) {
1535 /* Get the newly created channel reference back */
1536 uchan
= trace_ust_find_channel_by_name(
1537 usess
->domain_global
.channels
, channel_name
);
1541 /* At this point, the session and channel exist on the tracer */
1542 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1543 filter_expression
, filter
, exclusion
);
1544 /* We have passed ownership */
1545 filter_expression
= NULL
;
1548 if (ret
!= LTTNG_OK
) {
1553 case LTTNG_DOMAIN_LOG4J
:
1554 case LTTNG_DOMAIN_JUL
:
1555 case LTTNG_DOMAIN_PYTHON
:
1557 const char *default_event_name
, *default_chan_name
;
1559 struct lttng_event uevent
;
1560 struct lttng_domain tmp_dom
;
1561 struct ltt_ust_session
*usess
= session
->ust_session
;
1565 agt
= trace_ust_find_agent(usess
, domain
->type
);
1567 agt
= agent_create(domain
->type
);
1569 ret
= -LTTNG_ERR_NOMEM
;
1572 agent_add(agt
, usess
->agents
);
1575 /* Create the default tracepoint. */
1576 memset(&uevent
, 0, sizeof(uevent
));
1577 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1578 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1579 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1580 if (!default_event_name
) {
1581 ret
= -LTTNG_ERR_FATAL
;
1584 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1585 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1588 * The domain type is changed because we are about to enable the
1589 * default channel and event for the JUL domain that are hardcoded.
1590 * This happens in the UST domain.
1592 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1593 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1595 switch (domain
->type
) {
1596 case LTTNG_DOMAIN_LOG4J
:
1597 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1599 case LTTNG_DOMAIN_JUL
:
1600 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1602 case LTTNG_DOMAIN_PYTHON
:
1603 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
1606 /* The switch/case we are in should avoid this else big problem */
1611 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1614 filter_copy
= zmalloc(
1615 sizeof(struct lttng_filter_bytecode
)
1621 memcpy(filter_copy
, filter
,
1622 sizeof(struct lttng_filter_bytecode
)
1626 ret
= cmd_enable_event(session
, &tmp_dom
,
1627 (char *) default_chan_name
,
1628 &uevent
, filter_expression
, filter_copy
,
1630 /* We have passed ownership */
1631 filter_expression
= NULL
;
1634 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1638 /* The wild card * means that everything should be enabled. */
1639 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1640 ret
= event_agent_enable_all(usess
, agt
, event
, filter
);
1643 ret
= event_agent_enable(usess
, agt
, event
, filter
);
1646 if (ret
!= LTTNG_OK
) {
1653 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1654 case LTTNG_DOMAIN_UST_PID
:
1655 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1658 ret
= LTTNG_ERR_UND
;
1665 free(filter_expression
);
1673 * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread.
1676 int enable_kevent_all(struct ltt_session
*session
,
1677 struct lttng_domain
*domain
, char *channel_name
,
1678 struct lttng_event
*event
,
1679 char *filter_expression
,
1680 struct lttng_filter_bytecode
*filter
, int wpipe
)
1683 struct lttng_channel
*attr
;
1686 assert(channel_name
);
1690 switch (domain
->type
) {
1691 case LTTNG_DOMAIN_KERNEL
:
1693 struct ltt_kernel_channel
*kchan
;
1695 assert(session
->kernel_session
);
1698 * If a non-default channel has been created in the
1699 * session, explicitely require that -c chan_name needs
1702 if (session
->kernel_session
->has_non_default_channel
1703 && channel_name
[0] == '\0') {
1704 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1708 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1709 session
->kernel_session
);
1710 if (kchan
== NULL
) {
1711 /* Create default channel */
1712 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1713 LTTNG_BUFFER_GLOBAL
);
1715 ret
= LTTNG_ERR_FATAL
;
1718 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1720 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1721 if (ret
!= LTTNG_OK
) {
1727 /* Get the newly created kernel channel pointer */
1728 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1729 session
->kernel_session
);
1733 switch (event
->type
) {
1734 case LTTNG_EVENT_SYSCALL
:
1735 ret
= event_kernel_enable_syscall(kchan
, "");
1736 if (ret
!= LTTNG_OK
) {
1740 case LTTNG_EVENT_TRACEPOINT
:
1742 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1743 * events already registered to the channel.
1745 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1747 case LTTNG_EVENT_ALL
:
1748 /* Enable syscalls and tracepoints */
1749 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1752 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1756 /* Manage return value */
1757 if (ret
!= LTTNG_OK
) {
1759 * On error, cmd_enable_channel call will take care of destroying
1760 * the created channel if it was needed.
1765 kernel_wait_quiescent(kernel_tracer_fd
);
1769 ret
= LTTNG_ERR_UND
;
1782 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1784 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1787 ssize_t nb_events
= 0;
1790 case LTTNG_DOMAIN_KERNEL
:
1791 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1792 if (nb_events
< 0) {
1793 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1797 case LTTNG_DOMAIN_UST
:
1798 nb_events
= ust_app_list_events(events
);
1799 if (nb_events
< 0) {
1800 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1804 case LTTNG_DOMAIN_LOG4J
:
1805 case LTTNG_DOMAIN_JUL
:
1806 case LTTNG_DOMAIN_PYTHON
:
1807 nb_events
= agent_list_events(events
, domain
);
1808 if (nb_events
< 0) {
1809 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1814 ret
= LTTNG_ERR_UND
;
1821 /* Return negative value to differentiate return code */
1826 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1828 ssize_t
cmd_list_tracepoint_fields(int domain
,
1829 struct lttng_event_field
**fields
)
1832 ssize_t nb_fields
= 0;
1835 case LTTNG_DOMAIN_UST
:
1836 nb_fields
= ust_app_list_event_fields(fields
);
1837 if (nb_fields
< 0) {
1838 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1842 case LTTNG_DOMAIN_KERNEL
:
1843 default: /* fall-through */
1844 ret
= LTTNG_ERR_UND
;
1851 /* Return negative value to differentiate return code */
1855 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1857 return syscall_table_list(events
);
1861 * Command LTTNG_START_TRACE processed by the client thread.
1863 int cmd_start_trace(struct ltt_session
*session
)
1866 unsigned long nb_chan
= 0;
1867 struct ltt_kernel_session
*ksession
;
1868 struct ltt_ust_session
*usess
;
1872 /* Ease our life a bit ;) */
1873 ksession
= session
->kernel_session
;
1874 usess
= session
->ust_session
;
1876 /* Is the session already started? */
1877 if (session
->active
) {
1878 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1883 * Starting a session without channel is useless since after that it's not
1884 * possible to enable channel thus inform the client.
1886 if (usess
&& usess
->domain_global
.channels
) {
1888 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1892 nb_chan
+= ksession
->channel_count
;
1895 ret
= LTTNG_ERR_NO_CHANNEL
;
1899 /* Kernel tracing */
1900 if (ksession
!= NULL
) {
1901 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1902 if (ret
!= LTTNG_OK
) {
1907 /* Flag session that trace should start automatically */
1910 * Even though the start trace might fail, flag this session active so
1911 * other application coming in are started by default.
1915 ret
= ust_app_start_trace_all(usess
);
1917 ret
= LTTNG_ERR_UST_START_FAIL
;
1922 /* Flag this after a successful start. */
1923 session
->has_been_started
= 1;
1924 session
->active
= 1;
1933 * Command LTTNG_STOP_TRACE processed by the client thread.
1935 int cmd_stop_trace(struct ltt_session
*session
)
1938 struct ltt_kernel_channel
*kchan
;
1939 struct ltt_kernel_session
*ksession
;
1940 struct ltt_ust_session
*usess
;
1945 ksession
= session
->kernel_session
;
1946 usess
= session
->ust_session
;
1948 /* Session is not active. Skip everythong and inform the client. */
1949 if (!session
->active
) {
1950 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1955 if (ksession
&& ksession
->active
) {
1956 DBG("Stop kernel tracing");
1958 /* Flush metadata if exist */
1959 if (ksession
->metadata_stream_fd
>= 0) {
1960 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1962 ERR("Kernel metadata flush failed");
1966 /* Flush all buffers before stopping */
1967 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1968 ret
= kernel_flush_buffer(kchan
);
1970 ERR("Kernel flush buffer error");
1974 ret
= kernel_stop_session(ksession
);
1976 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1980 kernel_wait_quiescent(kernel_tracer_fd
);
1982 ksession
->active
= 0;
1985 if (usess
&& usess
->active
) {
1987 * Even though the stop trace might fail, flag this session inactive so
1988 * other application coming in are not started by default.
1992 ret
= ust_app_stop_trace_all(usess
);
1994 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1999 /* Flag inactive after a successful stop. */
2000 session
->active
= 0;
2008 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2010 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2011 struct lttng_uri
*uris
)
2014 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2015 struct ltt_ust_session
*usess
= session
->ust_session
;
2021 /* Can't set consumer URI if the session is active. */
2022 if (session
->active
) {
2023 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2027 /* Set the "global" consumer URIs */
2028 for (i
= 0; i
< nb_uri
; i
++) {
2029 ret
= add_uri_to_consumer(session
->consumer
,
2030 &uris
[i
], 0, session
->name
);
2031 if (ret
!= LTTNG_OK
) {
2036 /* Set UST session URIs */
2037 if (session
->ust_session
) {
2038 for (i
= 0; i
< nb_uri
; i
++) {
2039 ret
= add_uri_to_consumer(
2040 session
->ust_session
->consumer
,
2041 &uris
[i
], LTTNG_DOMAIN_UST
,
2043 if (ret
!= LTTNG_OK
) {
2049 /* Set kernel session URIs */
2050 if (session
->kernel_session
) {
2051 for (i
= 0; i
< nb_uri
; i
++) {
2052 ret
= add_uri_to_consumer(
2053 session
->kernel_session
->consumer
,
2054 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2056 if (ret
!= LTTNG_OK
) {
2063 * Make sure to set the session in output mode after we set URI since a
2064 * session can be created without URL (thus flagged in no output mode).
2066 session
->output_traces
= 1;
2068 ksess
->output_traces
= 1;
2072 usess
->output_traces
= 1;
2083 * Command LTTNG_CREATE_SESSION processed by the client thread.
2085 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2086 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2089 struct ltt_session
*session
;
2095 * Verify if the session already exist
2097 * XXX: There is no need for the session lock list here since the caller
2098 * (process_client_msg) is holding it. We might want to change that so a
2099 * single command does not lock the entire session list.
2101 session
= session_find_by_name(name
);
2102 if (session
!= NULL
) {
2103 ret
= LTTNG_ERR_EXIST_SESS
;
2107 /* Create tracing session in the registry */
2108 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2109 LTTNG_SOCK_GET_GID_CRED(creds
));
2110 if (ret
!= LTTNG_OK
) {
2115 * Get the newly created session pointer back
2117 * XXX: There is no need for the session lock list here since the caller
2118 * (process_client_msg) is holding it. We might want to change that so a
2119 * single command does not lock the entire session list.
2121 session
= session_find_by_name(name
);
2124 session
->live_timer
= live_timer
;
2125 /* Create default consumer output for the session not yet created. */
2126 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2127 if (session
->consumer
== NULL
) {
2128 ret
= LTTNG_ERR_FATAL
;
2129 goto consumer_error
;
2133 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2134 if (ret
!= LTTNG_OK
) {
2135 goto consumer_error
;
2137 session
->output_traces
= 1;
2139 session
->output_traces
= 0;
2140 DBG2("Session %s created with no output", session
->name
);
2143 session
->consumer
->enabled
= 1;
2148 session_destroy(session
);
2155 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2157 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2158 size_t nb_uri
, lttng_sock_cred
*creds
)
2161 struct ltt_session
*session
;
2162 struct snapshot_output
*new_output
= NULL
;
2168 * Create session in no output mode with URIs set to NULL. The uris we've
2169 * received are for a default snapshot output if one.
2171 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2172 if (ret
!= LTTNG_OK
) {
2176 /* Get the newly created session pointer back. This should NEVER fail. */
2177 session
= session_find_by_name(name
);
2180 /* Flag session for snapshot mode. */
2181 session
->snapshot_mode
= 1;
2183 /* Skip snapshot output creation if no URI is given. */
2188 new_output
= snapshot_output_alloc();
2190 ret
= LTTNG_ERR_NOMEM
;
2191 goto error_snapshot_alloc
;
2194 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2195 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2197 if (ret
== -ENOMEM
) {
2198 ret
= LTTNG_ERR_NOMEM
;
2200 ret
= LTTNG_ERR_INVALID
;
2202 goto error_snapshot
;
2206 snapshot_add_output(&session
->snapshot
, new_output
);
2213 snapshot_output_destroy(new_output
);
2214 error_snapshot_alloc
:
2215 session_destroy(session
);
2221 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2223 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2226 struct ltt_ust_session
*usess
;
2227 struct ltt_kernel_session
*ksess
;
2232 usess
= session
->ust_session
;
2233 ksess
= session
->kernel_session
;
2235 /* Clean kernel session teardown */
2236 kernel_destroy_session(ksess
);
2238 /* UST session teardown */
2240 /* Close any relayd session */
2241 consumer_output_send_destroy_relayd(usess
->consumer
);
2243 /* Destroy every UST application related to this session. */
2244 ret
= ust_app_destroy_trace_all(usess
);
2246 ERR("Error in ust_app_destroy_trace_all");
2249 /* Clean up the rest. */
2250 trace_ust_destroy_session(usess
);
2254 * Must notify the kernel thread here to update it's poll set in order to
2255 * remove the channel(s)' fd just destroyed.
2257 ret
= notify_thread_pipe(wpipe
);
2259 PERROR("write kernel poll pipe");
2262 ret
= session_destroy(session
);
2268 * Command LTTNG_CALIBRATE processed by the client thread.
2270 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2275 case LTTNG_DOMAIN_KERNEL
:
2277 struct lttng_kernel_calibrate kcalibrate
;
2279 switch (calibrate
->type
) {
2280 case LTTNG_CALIBRATE_FUNCTION
:
2282 /* Default and only possible calibrate option. */
2283 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2287 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2289 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2294 case LTTNG_DOMAIN_UST
:
2296 struct lttng_ust_calibrate ucalibrate
;
2298 switch (calibrate
->type
) {
2299 case LTTNG_CALIBRATE_FUNCTION
:
2301 /* Default and only possible calibrate option. */
2302 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2306 ret
= ust_app_calibrate_glb(&ucalibrate
);
2308 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2314 ret
= LTTNG_ERR_UND
;
2325 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2327 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2328 const char *sock_path
, struct consumer_data
*cdata
)
2331 struct consumer_socket
*socket
= NULL
;
2338 case LTTNG_DOMAIN_KERNEL
:
2340 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2344 /* Can't register a consumer if there is already one */
2345 if (ksess
->consumer_fds_sent
!= 0) {
2346 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2350 sock
= lttcomm_connect_unix_sock(sock_path
);
2352 ret
= LTTNG_ERR_CONNECT_FAIL
;
2355 cdata
->cmd_sock
= sock
;
2357 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2358 if (socket
== NULL
) {
2361 PERROR("close register consumer");
2363 cdata
->cmd_sock
= -1;
2364 ret
= LTTNG_ERR_FATAL
;
2368 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2369 if (socket
->lock
== NULL
) {
2370 PERROR("zmalloc pthread mutex");
2371 ret
= LTTNG_ERR_FATAL
;
2374 pthread_mutex_init(socket
->lock
, NULL
);
2375 socket
->registered
= 1;
2378 consumer_add_socket(socket
, ksess
->consumer
);
2381 pthread_mutex_lock(&cdata
->pid_mutex
);
2383 pthread_mutex_unlock(&cdata
->pid_mutex
);
2388 /* TODO: Userspace tracing */
2389 ret
= LTTNG_ERR_UND
;
2397 consumer_destroy_socket(socket
);
2403 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2405 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2406 struct lttng_domain
**domains
)
2411 struct lttng_ht_iter iter
;
2413 if (session
->kernel_session
!= NULL
) {
2414 DBG3("Listing domains found kernel domain");
2418 if (session
->ust_session
!= NULL
) {
2419 DBG3("Listing domains found UST global domain");
2423 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2425 if (agt
->being_used
) {
2436 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2437 if (*domains
== NULL
) {
2438 ret
= LTTNG_ERR_FATAL
;
2442 if (session
->kernel_session
!= NULL
) {
2443 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2447 if (session
->ust_session
!= NULL
) {
2448 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2449 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2453 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2455 if (agt
->being_used
) {
2456 (*domains
)[index
].type
= agt
->domain
;
2457 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2467 /* Return negative value to differentiate return code */
2473 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2475 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2476 struct lttng_channel
**channels
)
2479 ssize_t nb_chan
= 0;
2482 case LTTNG_DOMAIN_KERNEL
:
2483 if (session
->kernel_session
!= NULL
) {
2484 nb_chan
= session
->kernel_session
->channel_count
;
2486 DBG3("Number of kernel channels %zd", nb_chan
);
2488 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2491 case LTTNG_DOMAIN_UST
:
2492 if (session
->ust_session
!= NULL
) {
2494 nb_chan
= lttng_ht_get_count(
2495 session
->ust_session
->domain_global
.channels
);
2498 DBG3("Number of UST global channels %zd", nb_chan
);
2500 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2505 ret
= LTTNG_ERR_UND
;
2510 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2511 if (*channels
== NULL
) {
2512 ret
= LTTNG_ERR_FATAL
;
2516 list_lttng_channels(domain
, session
, *channels
);
2522 /* Return negative value to differentiate return code */
2527 * Command LTTNG_LIST_EVENTS processed by the client thread.
2529 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2530 char *channel_name
, struct lttng_event
**events
)
2533 ssize_t nb_event
= 0;
2536 case LTTNG_DOMAIN_KERNEL
:
2537 if (session
->kernel_session
!= NULL
) {
2538 nb_event
= list_lttng_kernel_events(channel_name
,
2539 session
->kernel_session
, events
);
2542 case LTTNG_DOMAIN_UST
:
2544 if (session
->ust_session
!= NULL
) {
2545 nb_event
= list_lttng_ust_global_events(channel_name
,
2546 &session
->ust_session
->domain_global
, events
);
2550 case LTTNG_DOMAIN_LOG4J
:
2551 case LTTNG_DOMAIN_JUL
:
2552 case LTTNG_DOMAIN_PYTHON
:
2553 if (session
->ust_session
) {
2554 struct lttng_ht_iter iter
;
2558 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2559 &iter
.iter
, agt
, node
.node
) {
2560 nb_event
= list_lttng_agent_events(agt
, events
);
2566 ret
= LTTNG_ERR_UND
;
2573 /* Return negative value to differentiate return code */
2578 * Using the session list, filled a lttng_session array to send back to the
2579 * client for session listing.
2581 * The session list lock MUST be acquired before calling this function. Use
2582 * session_lock_list() and session_unlock_list().
2584 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2589 struct ltt_session
*session
;
2590 struct ltt_session_list
*list
= session_get_list();
2592 DBG("Getting all available session for UID %d GID %d",
2595 * Iterate over session list and append data after the control struct in
2598 cds_list_for_each_entry(session
, &list
->head
, list
) {
2600 * Only list the sessions the user can control.
2602 if (!session_access_ok(session
, uid
, gid
)) {
2606 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2607 struct ltt_ust_session
*usess
= session
->ust_session
;
2609 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2610 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2611 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2612 ret
= build_network_session_path(sessions
[i
].path
,
2613 sizeof(sessions
[i
].path
), session
);
2615 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2616 session
->consumer
->dst
.trace_path
);
2619 PERROR("snprintf session path");
2623 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2624 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2625 sessions
[i
].enabled
= session
->active
;
2626 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2627 sessions
[i
].live_timer_interval
= session
->live_timer
;
2633 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2634 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2636 int cmd_data_pending(struct ltt_session
*session
)
2639 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2640 struct ltt_ust_session
*usess
= session
->ust_session
;
2644 /* Session MUST be stopped to ask for data availability. */
2645 if (session
->active
) {
2646 ret
= LTTNG_ERR_SESSION_STARTED
;
2650 * If stopped, just make sure we've started before else the above call
2651 * will always send that there is data pending.
2653 * The consumer assumes that when the data pending command is received,
2654 * the trace has been started before or else no output data is written
2655 * by the streams which is a condition for data pending. So, this is
2656 * *VERY* important that we don't ask the consumer before a start
2659 if (!session
->has_been_started
) {
2665 if (ksess
&& ksess
->consumer
) {
2666 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2668 /* Data is still being extracted for the kernel. */
2673 if (usess
&& usess
->consumer
) {
2674 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2676 /* Data is still being extracted for the kernel. */
2681 /* Data is ready to be read by a viewer */
2689 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2691 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2693 int cmd_snapshot_add_output(struct ltt_session
*session
,
2694 struct lttng_snapshot_output
*output
, uint32_t *id
)
2697 struct snapshot_output
*new_output
;
2702 DBG("Cmd snapshot add output for session %s", session
->name
);
2705 * Permission denied to create an output if the session is not
2706 * set in no output mode.
2708 if (session
->output_traces
) {
2709 ret
= LTTNG_ERR_EPERM
;
2713 /* Only one output is allowed until we have the "tee" feature. */
2714 if (session
->snapshot
.nb_output
== 1) {
2715 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2719 new_output
= snapshot_output_alloc();
2721 ret
= LTTNG_ERR_NOMEM
;
2725 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2726 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2727 &session
->snapshot
);
2729 if (ret
== -ENOMEM
) {
2730 ret
= LTTNG_ERR_NOMEM
;
2732 ret
= LTTNG_ERR_INVALID
;
2738 snapshot_add_output(&session
->snapshot
, new_output
);
2740 *id
= new_output
->id
;
2747 snapshot_output_destroy(new_output
);
2753 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2755 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2757 int cmd_snapshot_del_output(struct ltt_session
*session
,
2758 struct lttng_snapshot_output
*output
)
2761 struct snapshot_output
*sout
= NULL
;
2769 * Permission denied to create an output if the session is not
2770 * set in no output mode.
2772 if (session
->output_traces
) {
2773 ret
= LTTNG_ERR_EPERM
;
2778 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2780 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2781 } else if (*output
->name
!= '\0') {
2782 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2784 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2787 ret
= LTTNG_ERR_INVALID
;
2791 snapshot_delete_output(&session
->snapshot
, sout
);
2792 snapshot_output_destroy(sout
);
2801 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2803 * If no output is available, outputs is untouched and 0 is returned.
2805 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2807 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2808 struct lttng_snapshot_output
**outputs
)
2811 struct lttng_snapshot_output
*list
= NULL
;
2812 struct lttng_ht_iter iter
;
2813 struct snapshot_output
*output
;
2818 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2821 * Permission denied to create an output if the session is not
2822 * set in no output mode.
2824 if (session
->output_traces
) {
2825 ret
= -LTTNG_ERR_EPERM
;
2829 if (session
->snapshot
.nb_output
== 0) {
2834 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2836 ret
= -LTTNG_ERR_NOMEM
;
2840 /* Copy list from session to the new list object. */
2842 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2843 output
, node
.node
) {
2844 assert(output
->consumer
);
2845 list
[idx
].id
= output
->id
;
2846 list
[idx
].max_size
= output
->max_size
;
2847 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2848 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2849 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2850 sizeof(list
[idx
].ctrl_url
));
2853 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2854 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2856 ret
= -LTTNG_ERR_NOMEM
;
2861 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2862 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2864 ret
= -LTTNG_ERR_NOMEM
;
2873 ret
= session
->snapshot
.nb_output
;
2881 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2882 * snapshot output is *not* set with a remote destination.
2884 * Return 0 on success or a LTTNG_ERR code.
2886 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2887 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2890 struct lttng_ht_iter iter
;
2891 struct consumer_socket
*socket
;
2894 assert(snap_output
);
2897 DBG2("Set relayd object from snapshot output");
2899 /* Ignore if snapshot consumer output is not network. */
2900 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2905 * For each consumer socket, create and send the relayd object of the
2909 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2910 socket
, node
.node
) {
2911 ret
= send_consumer_relayd_sockets(0, session
->id
,
2912 snap_output
->consumer
, socket
,
2913 session
->name
, session
->hostname
,
2914 session
->live_timer
);
2915 if (ret
!= LTTNG_OK
) {
2927 * Record a kernel snapshot.
2929 * Return LTTNG_OK on success or a LTTNG_ERR code.
2931 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2932 struct snapshot_output
*output
, struct ltt_session
*session
,
2933 int wait
, uint64_t max_stream_size
)
2941 /* Get the datetime for the snapshot output directory. */
2942 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2943 sizeof(output
->datetime
));
2945 ret
= LTTNG_ERR_INVALID
;
2950 * Copy kernel session sockets so we can communicate with the right
2951 * consumer for the snapshot record command.
2953 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2955 ret
= LTTNG_ERR_NOMEM
;
2959 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2960 if (ret
!= LTTNG_OK
) {
2961 goto error_snapshot
;
2964 ret
= kernel_snapshot_record(ksess
, output
, wait
, max_stream_size
);
2965 if (ret
!= LTTNG_OK
) {
2966 goto error_snapshot
;
2972 /* Clean up copied sockets so this output can use some other later on. */
2973 consumer_destroy_output_sockets(output
->consumer
);
2979 * Record a UST snapshot.
2981 * Return 0 on success or a LTTNG_ERR error code.
2983 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2984 struct snapshot_output
*output
, struct ltt_session
*session
,
2985 int wait
, uint64_t max_stream_size
)
2993 /* Get the datetime for the snapshot output directory. */
2994 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2995 sizeof(output
->datetime
));
2997 ret
= LTTNG_ERR_INVALID
;
3002 * Copy UST session sockets so we can communicate with the right
3003 * consumer for the snapshot record command.
3005 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3007 ret
= LTTNG_ERR_NOMEM
;
3011 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3012 if (ret
!= LTTNG_OK
) {
3013 goto error_snapshot
;
3016 ret
= ust_app_snapshot_record(usess
, output
, wait
, max_stream_size
);
3020 ret
= LTTNG_ERR_INVALID
;
3023 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3026 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3029 goto error_snapshot
;
3035 /* Clean up copied sockets so this output can use some other later on. */
3036 consumer_destroy_output_sockets(output
->consumer
);
3042 * Return the biggest subbuffer size of all channels in the given session.
3044 static uint64_t get_session_max_subbuf_size(struct ltt_session
*session
)
3046 uint64_t max_size
= 0;
3050 if (session
->kernel_session
) {
3051 struct ltt_kernel_channel
*chan
;
3052 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3055 * For each channel, add to the max size the size of each subbuffer
3056 * multiplied by their sized.
3058 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3059 if (chan
->channel
->attr
.subbuf_size
> max_size
) {
3060 max_size
= chan
->channel
->attr
.subbuf_size
;
3065 if (session
->ust_session
) {
3066 struct lttng_ht_iter iter
;
3067 struct ltt_ust_channel
*uchan
;
3068 struct ltt_ust_session
*usess
= session
->ust_session
;
3071 cds_lfht_for_each_entry(usess
->domain_global
.channels
->ht
, &iter
.iter
,
3073 if (uchan
->attr
.subbuf_size
> max_size
) {
3074 max_size
= uchan
->attr
.subbuf_size
;
3084 * Returns the total number of streams for a session or a negative value
3087 static unsigned int get_session_nb_streams(struct ltt_session
*session
)
3089 unsigned int total_streams
= 0;
3091 if (session
->kernel_session
) {
3092 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3094 total_streams
+= ksess
->stream_count_global
;
3097 if (session
->ust_session
) {
3098 struct ltt_ust_session
*usess
= session
->ust_session
;
3100 total_streams
+= ust_app_get_nb_stream(usess
);
3103 return total_streams
;
3107 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3109 * The wait parameter is ignored so this call always wait for the snapshot to
3110 * complete before returning.
3112 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3114 int cmd_snapshot_record(struct ltt_session
*session
,
3115 struct lttng_snapshot_output
*output
, int wait
)
3118 unsigned int use_tmp_output
= 0;
3119 struct snapshot_output tmp_output
;
3120 unsigned int nb_streams
, snapshot_success
= 0;
3121 uint64_t session_max_size
= 0, max_stream_size
= 0;
3126 DBG("Cmd snapshot record for session %s", session
->name
);
3129 * Permission denied to create an output if the session is not
3130 * set in no output mode.
3132 if (session
->output_traces
) {
3133 ret
= LTTNG_ERR_EPERM
;
3137 /* The session needs to be started at least once. */
3138 if (!session
->has_been_started
) {
3139 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3143 /* Use temporary output for the session. */
3144 if (*output
->ctrl_url
!= '\0') {
3145 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3146 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3149 if (ret
== -ENOMEM
) {
3150 ret
= LTTNG_ERR_NOMEM
;
3152 ret
= LTTNG_ERR_INVALID
;
3156 /* Use the global session count for the temporary snapshot. */
3157 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3162 * Get the session maximum size for a snapshot meaning it will compute the
3163 * size of all streams from all domain.
3165 max_stream_size
= get_session_max_subbuf_size(session
);
3167 nb_streams
= get_session_nb_streams(session
);
3170 * The maximum size of the snapshot is the number of streams multiplied
3171 * by the biggest subbuf size of all channels in a session which is the
3172 * maximum stream size available for each stream. The session max size
3173 * is now checked against the snapshot max size value given by the user
3174 * and if lower, an error is returned.
3176 session_max_size
= max_stream_size
* nb_streams
;
3179 DBG3("Snapshot max size is %" PRIu64
" for max stream size of %" PRIu64
,
3180 session_max_size
, max_stream_size
);
3183 * If we use a temporary output, check right away if the max size fits else
3184 * for each output the max size will be checked.
3186 if (use_tmp_output
&&
3187 (tmp_output
.max_size
!= 0 &&
3188 tmp_output
.max_size
< session_max_size
)) {
3189 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3193 if (session
->kernel_session
) {
3194 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3196 if (use_tmp_output
) {
3197 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3198 wait
, max_stream_size
);
3199 if (ret
!= LTTNG_OK
) {
3202 snapshot_success
= 1;
3204 struct snapshot_output
*sout
;
3205 struct lttng_ht_iter iter
;
3208 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3209 &iter
.iter
, sout
, node
.node
) {
3211 * Make a local copy of the output and assign the possible
3212 * temporary value given by the caller.
3214 memset(&tmp_output
, 0, sizeof(tmp_output
));
3215 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3217 /* Use temporary max size. */
3218 if (output
->max_size
!= (uint64_t) -1ULL) {
3219 tmp_output
.max_size
= output
->max_size
;
3222 if (tmp_output
.max_size
!= 0 &&
3223 tmp_output
.max_size
< session_max_size
) {
3225 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3229 /* Use temporary name. */
3230 if (*output
->name
!= '\0') {
3231 strncpy(tmp_output
.name
, output
->name
,
3232 sizeof(tmp_output
.name
));
3235 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3237 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3238 session
, wait
, max_stream_size
);
3239 if (ret
!= LTTNG_OK
) {
3243 snapshot_success
= 1;
3249 if (session
->ust_session
) {
3250 struct ltt_ust_session
*usess
= session
->ust_session
;
3252 if (use_tmp_output
) {
3253 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3254 wait
, max_stream_size
);
3255 if (ret
!= LTTNG_OK
) {
3258 snapshot_success
= 1;
3260 struct snapshot_output
*sout
;
3261 struct lttng_ht_iter iter
;
3264 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3265 &iter
.iter
, sout
, node
.node
) {
3267 * Make a local copy of the output and assign the possible
3268 * temporary value given by the caller.
3270 memset(&tmp_output
, 0, sizeof(tmp_output
));
3271 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3273 /* Use temporary max size. */
3274 if (output
->max_size
!= (uint64_t) -1ULL) {
3275 tmp_output
.max_size
= output
->max_size
;
3278 if (tmp_output
.max_size
!= 0 &&
3279 tmp_output
.max_size
< session_max_size
) {
3281 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3285 /* Use temporary name. */
3286 if (*output
->name
!= '\0') {
3287 strncpy(tmp_output
.name
, output
->name
,
3288 sizeof(tmp_output
.name
));
3291 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3293 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3294 wait
, max_stream_size
);
3295 if (ret
!= LTTNG_OK
) {
3299 snapshot_success
= 1;
3305 if (snapshot_success
) {
3306 session
->snapshot
.nb_snapshot
++;
3308 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3316 * Init command subsystem.
3321 * Set network sequence index to 1 for streams to match a relayd
3322 * socket on the consumer side.
3324 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3325 relayd_net_seq_idx
= 1;
3326 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3328 DBG("Command subsystem initialized");