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 /* Explicit length checks for strcpy and strcat. */
522 if (strlen(uri
->dst
.path
) + strlen(default_trace_dir
)
523 >= sizeof(consumer
->dst
.trace_path
)) {
524 ret
= LTTNG_ERR_FATAL
;
527 strcpy(consumer
->dst
.trace_path
, uri
->dst
.path
);
528 /* Append default trace dir */
529 strcat(consumer
->dst
.trace_path
, default_trace_dir
);
530 /* Flag consumer as local. */
531 consumer
->type
= CONSUMER_DST_LOCAL
;
542 * Init tracing by creating trace directory and sending fds kernel consumer.
544 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
547 struct lttng_ht_iter iter
;
548 struct consumer_socket
*socket
;
554 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
555 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
557 pthread_mutex_lock(socket
->lock
);
558 ret
= kernel_consumer_send_session(socket
, session
);
559 pthread_mutex_unlock(socket
->lock
);
561 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
573 * Create a socket to the relayd using the URI.
575 * On success, the relayd_sock pointer is set to the created socket.
576 * Else, it's stays untouched and a lttcomm error code is returned.
578 static int create_connect_relayd(struct lttng_uri
*uri
,
579 struct lttcomm_relayd_sock
**relayd_sock
)
582 struct lttcomm_relayd_sock
*rsock
;
584 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
585 RELAYD_VERSION_COMM_MINOR
);
587 ret
= LTTNG_ERR_FATAL
;
592 * Connect to relayd so we can proceed with a session creation. This call
593 * can possibly block for an arbitrary amount of time to set the health
594 * state to be in poll execution.
597 ret
= relayd_connect(rsock
);
600 ERR("Unable to reach lttng-relayd");
601 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
605 /* Create socket for control stream. */
606 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
607 DBG3("Creating relayd stream socket from URI");
609 /* Check relayd version */
610 ret
= relayd_version_check(rsock
);
612 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
615 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
616 DBG3("Creating relayd data socket from URI");
618 /* Command is not valid */
619 ERR("Relayd invalid stream type: %d", uri
->stype
);
620 ret
= LTTNG_ERR_INVALID
;
624 *relayd_sock
= rsock
;
629 /* The returned value is not useful since we are on an error path. */
630 (void) relayd_close(rsock
);
638 * Connect to the relayd using URI and send the socket to the right consumer.
640 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
641 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
642 struct consumer_socket
*consumer_sock
,
643 char *session_name
, char *hostname
, int session_live_timer
)
646 struct lttcomm_relayd_sock
*rsock
= NULL
;
648 /* Connect to relayd and make version check if uri is the control. */
649 ret
= create_connect_relayd(relayd_uri
, &rsock
);
650 if (ret
!= LTTNG_OK
) {
655 /* Set the network sequence index if not set. */
656 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
657 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
659 * Increment net_seq_idx because we are about to transfer the
660 * new relayd socket to the consumer.
661 * Assign unique key so the consumer can match streams.
663 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
664 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
667 /* Send relayd socket to consumer. */
668 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
669 relayd_uri
->stype
, session_id
,
670 session_name
, hostname
, session_live_timer
);
672 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
676 /* Flag that the corresponding socket was sent. */
677 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
678 consumer_sock
->control_sock_sent
= 1;
679 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
680 consumer_sock
->data_sock_sent
= 1;
686 * Close socket which was dup on the consumer side. The session daemon does
687 * NOT keep track of the relayd socket(s) once transfer to the consumer.
691 (void) relayd_close(rsock
);
695 if (ret
!= LTTNG_OK
) {
697 * The consumer output for this session should not be used anymore
698 * since the relayd connection failed thus making any tracing or/and
699 * streaming not usable.
701 consumer
->enabled
= 0;
707 * Send both relayd sockets to a specific consumer and domain. This is a
708 * helper function to facilitate sending the information to the consumer for a
711 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
712 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
713 char *session_name
, char *hostname
, int session_live_timer
)
720 /* Sending control relayd socket. */
721 if (!sock
->control_sock_sent
) {
722 ret
= send_consumer_relayd_socket(domain
, session_id
,
723 &consumer
->dst
.net
.control
, consumer
, sock
,
724 session_name
, hostname
, session_live_timer
);
725 if (ret
!= LTTNG_OK
) {
730 /* Sending data relayd socket. */
731 if (!sock
->data_sock_sent
) {
732 ret
= send_consumer_relayd_socket(domain
, session_id
,
733 &consumer
->dst
.net
.data
, consumer
, sock
,
734 session_name
, hostname
, session_live_timer
);
735 if (ret
!= LTTNG_OK
) {
745 * Setup relayd connections for a tracing session. First creates the socket to
746 * the relayd and send them to the right domain consumer. Consumer type MUST be
749 int cmd_setup_relayd(struct ltt_session
*session
)
752 struct ltt_ust_session
*usess
;
753 struct ltt_kernel_session
*ksess
;
754 struct consumer_socket
*socket
;
755 struct lttng_ht_iter iter
;
759 usess
= session
->ust_session
;
760 ksess
= session
->kernel_session
;
762 DBG("Setting relayd for session %s", session
->name
);
766 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
767 && usess
->consumer
->enabled
) {
768 /* For each consumer socket, send relayd sockets */
769 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
771 pthread_mutex_lock(socket
->lock
);
772 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
773 usess
->consumer
, socket
,
774 session
->name
, session
->hostname
,
775 session
->live_timer
);
776 pthread_mutex_unlock(socket
->lock
);
777 if (ret
!= LTTNG_OK
) {
780 /* Session is now ready for network streaming. */
781 session
->net_handle
= 1;
785 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
786 && ksess
->consumer
->enabled
) {
787 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
789 pthread_mutex_lock(socket
->lock
);
790 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
791 ksess
->consumer
, socket
,
792 session
->name
, session
->hostname
,
793 session
->live_timer
);
794 pthread_mutex_unlock(socket
->lock
);
795 if (ret
!= LTTNG_OK
) {
798 /* Session is now ready for network streaming. */
799 session
->net_handle
= 1;
809 * Start a kernel session by opening all necessary streams.
811 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
814 struct ltt_kernel_channel
*kchan
;
816 /* Open kernel metadata */
817 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
818 ret
= kernel_open_metadata(ksess
);
820 ret
= LTTNG_ERR_KERN_META_FAIL
;
825 /* Open kernel metadata stream */
826 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
827 ret
= kernel_open_metadata_stream(ksess
);
829 ERR("Kernel create metadata stream failed");
830 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
835 /* For each channel */
836 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
837 if (kchan
->stream_count
== 0) {
838 ret
= kernel_open_channel_stream(kchan
);
840 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
843 /* Update the stream global counter */
844 ksess
->stream_count_global
+= ret
;
848 /* Setup kernel consumer socket and send fds to it */
849 ret
= init_kernel_tracing(ksess
);
851 ret
= LTTNG_ERR_KERN_START_FAIL
;
855 /* This start the kernel tracing */
856 ret
= kernel_start_session(ksess
);
858 ret
= LTTNG_ERR_KERN_START_FAIL
;
862 /* Quiescent wait after starting trace */
863 kernel_wait_quiescent(kernel_tracer_fd
);
874 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
876 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
880 struct ltt_ust_session
*usess
;
882 usess
= session
->ust_session
;
887 case LTTNG_DOMAIN_KERNEL
:
889 ret
= channel_kernel_disable(session
->kernel_session
,
891 if (ret
!= LTTNG_OK
) {
895 kernel_wait_quiescent(kernel_tracer_fd
);
898 case LTTNG_DOMAIN_UST
:
900 struct ltt_ust_channel
*uchan
;
901 struct lttng_ht
*chan_ht
;
903 chan_ht
= usess
->domain_global
.channels
;
905 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
907 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
911 ret
= channel_ust_disable(usess
, uchan
);
912 if (ret
!= LTTNG_OK
) {
918 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
919 case LTTNG_DOMAIN_UST_EXEC_NAME
:
920 case LTTNG_DOMAIN_UST_PID
:
923 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
935 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
937 * The wpipe arguments is used as a notifier for the kernel thread.
939 int cmd_enable_channel(struct ltt_session
*session
,
940 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
943 struct ltt_ust_session
*usess
= session
->ust_session
;
944 struct lttng_ht
*chan_ht
;
951 len
= strnlen(attr
->name
, sizeof(attr
->name
));
953 /* Validate channel name */
954 if (attr
->name
[0] == '.' ||
955 memchr(attr
->name
, '/', len
) != NULL
) {
956 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
960 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
965 * Don't try to enable a channel if the session has been started at
966 * some point in time before. The tracer does not allow it.
968 if (session
->has_been_started
) {
969 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
974 * If the session is a live session, remove the switch timer, the
975 * live timer does the same thing but sends also synchronisation
976 * beacons for inactive streams.
978 if (session
->live_timer
> 0) {
979 attr
->attr
.live_timer_interval
= session
->live_timer
;
980 attr
->attr
.switch_timer_interval
= 0;
984 * The ringbuffer (both in user space and kernel) behave badly in overwrite
985 * mode and with less than 2 subbuf so block it right away and send back an
986 * invalid attribute error.
988 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
989 ret
= LTTNG_ERR_INVALID
;
993 switch (domain
->type
) {
994 case LTTNG_DOMAIN_KERNEL
:
996 struct ltt_kernel_channel
*kchan
;
998 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
999 session
->kernel_session
);
1000 if (kchan
== NULL
) {
1001 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1002 if (attr
->name
[0] != '\0') {
1003 session
->kernel_session
->has_non_default_channel
= 1;
1006 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1009 if (ret
!= LTTNG_OK
) {
1013 kernel_wait_quiescent(kernel_tracer_fd
);
1016 case LTTNG_DOMAIN_UST
:
1018 struct ltt_ust_channel
*uchan
;
1020 chan_ht
= usess
->domain_global
.channels
;
1022 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1023 if (uchan
== NULL
) {
1024 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1025 if (attr
->name
[0] != '\0') {
1026 usess
->has_non_default_channel
= 1;
1029 ret
= channel_ust_enable(usess
, uchan
);
1034 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1046 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1048 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1050 struct lttng_event
*event
)
1055 DBG("Disable event command for event \'%s\'", event
->name
);
1057 event_name
= event
->name
;
1059 /* Error out on unhandled search criteria */
1060 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1061 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1062 return LTTNG_ERR_UNK
;
1064 /* Special handling for kernel domain all events. */
1065 if (domain
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event_name
, "*")) {
1066 return disable_kevent_all(session
, domain
, channel_name
, event
);
1072 case LTTNG_DOMAIN_KERNEL
:
1074 struct ltt_kernel_channel
*kchan
;
1075 struct ltt_kernel_session
*ksess
;
1077 ksess
= session
->kernel_session
;
1080 * If a non-default channel has been created in the
1081 * session, explicitely require that -c chan_name needs
1084 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1085 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1089 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1090 if (kchan
== NULL
) {
1091 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1095 switch (event
->type
) {
1096 case LTTNG_EVENT_ALL
:
1097 case LTTNG_EVENT_TRACEPOINT
:
1098 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1099 if (ret
!= LTTNG_OK
) {
1103 case LTTNG_EVENT_SYSCALL
:
1104 ret
= event_kernel_disable_syscall(kchan
, event_name
);
1105 if (ret
!= LTTNG_OK
) {
1110 ret
= LTTNG_ERR_UNK
;
1114 kernel_wait_quiescent(kernel_tracer_fd
);
1117 case LTTNG_DOMAIN_UST
:
1119 struct ltt_ust_channel
*uchan
;
1120 struct ltt_ust_session
*usess
;
1122 usess
= session
->ust_session
;
1125 * If a non-default channel has been created in the
1126 * session, explicitely require that -c chan_name needs
1129 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1130 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1134 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1136 if (uchan
== NULL
) {
1137 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1141 switch (event
->type
) {
1142 case LTTNG_EVENT_ALL
:
1143 if (strlen(event
->name
) == 1 &&
1144 !strncmp(event
->name
, "*", 1)) {
1145 ret
= event_ust_disable_all_tracepoints(usess
,
1148 ret
= event_ust_disable_tracepoint(usess
, uchan
,
1151 if (ret
!= LTTNG_OK
) {
1156 ret
= LTTNG_ERR_UNK
;
1160 DBG3("Disable UST event %s in channel %s completed", event_name
,
1164 case LTTNG_DOMAIN_LOG4J
:
1165 case LTTNG_DOMAIN_JUL
:
1168 struct ltt_ust_session
*usess
= session
->ust_session
;
1172 switch (event
->type
) {
1173 case LTTNG_EVENT_ALL
:
1176 ret
= LTTNG_ERR_UNK
;
1180 agt
= trace_ust_find_agent(usess
, domain
);
1182 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1185 /* The wild card * means that everything should be disabled. */
1186 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1187 ret
= event_agent_disable_all(usess
, agt
);
1189 ret
= event_agent_disable(usess
, agt
, event_name
);
1191 if (ret
!= LTTNG_OK
) {
1198 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1199 case LTTNG_DOMAIN_UST_PID
:
1200 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1203 ret
= LTTNG_ERR_UND
;
1215 * Command LTTNG_DISABLE_EVENT for event "*" processed by the client thread.
1218 int disable_kevent_all(struct ltt_session
*session
, int domain
,
1220 struct lttng_event
*event
)
1227 case LTTNG_DOMAIN_KERNEL
:
1229 struct ltt_kernel_session
*ksess
;
1230 struct ltt_kernel_channel
*kchan
;
1232 ksess
= session
->kernel_session
;
1235 * If a non-default channel has been created in the
1236 * session, explicitely require that -c chan_name needs
1239 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1240 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1244 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1245 if (kchan
== NULL
) {
1246 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1250 switch (event
->type
) {
1251 case LTTNG_EVENT_ALL
:
1252 ret
= event_kernel_disable_all(kchan
);
1253 if (ret
!= LTTNG_OK
) {
1257 case LTTNG_EVENT_SYSCALL
:
1258 ret
= event_kernel_disable_syscall(kchan
, "");
1259 if (ret
!= LTTNG_OK
) {
1264 ret
= LTTNG_ERR_UNK
;
1268 kernel_wait_quiescent(kernel_tracer_fd
);
1272 ret
= LTTNG_ERR_UND
;
1284 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1286 int cmd_add_context(struct ltt_session
*session
, int domain
,
1287 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1289 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1292 case LTTNG_DOMAIN_KERNEL
:
1293 assert(session
->kernel_session
);
1295 if (session
->kernel_session
->channel_count
== 0) {
1296 /* Create default channel */
1297 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1298 if (ret
!= LTTNG_OK
) {
1301 chan_kern_created
= 1;
1303 /* Add kernel context to kernel tracer */
1304 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1305 if (ret
!= LTTNG_OK
) {
1309 case LTTNG_DOMAIN_UST
:
1311 struct ltt_ust_session
*usess
= session
->ust_session
;
1312 unsigned int chan_count
;
1316 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1317 if (chan_count
== 0) {
1318 struct lttng_channel
*attr
;
1319 /* Create default channel */
1320 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1322 ret
= LTTNG_ERR_FATAL
;
1326 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1327 if (ret
!= LTTNG_OK
) {
1332 chan_ust_created
= 1;
1335 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1336 if (ret
!= LTTNG_OK
) {
1342 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1343 case LTTNG_DOMAIN_UST_PID
:
1344 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1347 ret
= LTTNG_ERR_UND
;
1354 if (chan_kern_created
) {
1355 struct ltt_kernel_channel
*kchan
=
1356 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1357 session
->kernel_session
);
1358 /* Created previously, this should NOT fail. */
1360 kernel_destroy_channel(kchan
);
1363 if (chan_ust_created
) {
1364 struct ltt_ust_channel
*uchan
=
1365 trace_ust_find_channel_by_name(
1366 session
->ust_session
->domain_global
.channels
,
1367 DEFAULT_CHANNEL_NAME
);
1368 /* Created previously, this should NOT fail. */
1370 /* Remove from the channel list of the session. */
1371 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1373 trace_ust_destroy_channel(uchan
);
1378 static int validate_event_name(const char *name
)
1381 const char *c
= name
;
1382 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1385 * Make sure that unescaped wildcards are only used as the last
1386 * character of the event name.
1388 while (c
< event_name_end
) {
1396 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1397 /* Wildcard is not the last character */
1398 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1411 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1412 * We own filter, exclusion, and filter_expression.
1414 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1415 char *channel_name
, struct lttng_event
*event
,
1416 char *filter_expression
,
1417 struct lttng_filter_bytecode
*filter
,
1418 struct lttng_event_exclusion
*exclusion
,
1421 int ret
, channel_created
= 0;
1422 struct lttng_channel
*attr
;
1426 assert(channel_name
);
1428 DBG("Enable event command for event \'%s\'", event
->name
);
1430 /* Special handling for kernel domain all events. */
1431 if (domain
->type
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event
->name
, "*")) {
1432 return enable_kevent_all(session
, domain
, channel_name
, event
,
1433 filter_expression
, filter
, wpipe
);
1436 ret
= validate_event_name(event
->name
);
1443 switch (domain
->type
) {
1444 case LTTNG_DOMAIN_KERNEL
:
1446 struct ltt_kernel_channel
*kchan
;
1449 * If a non-default channel has been created in the
1450 * session, explicitely require that -c chan_name needs
1453 if (session
->kernel_session
->has_non_default_channel
1454 && channel_name
[0] == '\0') {
1455 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1459 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1460 session
->kernel_session
);
1461 if (kchan
== NULL
) {
1462 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1463 LTTNG_BUFFER_GLOBAL
);
1465 ret
= LTTNG_ERR_FATAL
;
1468 if (lttng_strncpy(attr
->name
, channel_name
,
1469 sizeof(attr
->name
))) {
1470 ret
= LTTNG_ERR_INVALID
;
1475 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1476 if (ret
!= LTTNG_OK
) {
1482 channel_created
= 1;
1485 /* Get the newly created kernel channel pointer */
1486 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1487 session
->kernel_session
);
1488 if (kchan
== NULL
) {
1489 /* This sould not happen... */
1490 ret
= LTTNG_ERR_FATAL
;
1494 switch (event
->type
) {
1495 case LTTNG_EVENT_ALL
:
1496 case LTTNG_EVENT_PROBE
:
1497 case LTTNG_EVENT_FUNCTION
:
1498 case LTTNG_EVENT_FUNCTION_ENTRY
:
1499 case LTTNG_EVENT_TRACEPOINT
:
1500 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1501 if (ret
!= LTTNG_OK
) {
1502 if (channel_created
) {
1503 /* Let's not leak a useless channel. */
1504 kernel_destroy_channel(kchan
);
1509 case LTTNG_EVENT_SYSCALL
:
1510 ret
= event_kernel_enable_syscall(kchan
, event
->name
);
1511 if (ret
!= LTTNG_OK
) {
1516 ret
= LTTNG_ERR_UNK
;
1520 kernel_wait_quiescent(kernel_tracer_fd
);
1523 case LTTNG_DOMAIN_UST
:
1525 struct ltt_ust_channel
*uchan
;
1526 struct ltt_ust_session
*usess
= session
->ust_session
;
1531 * If a non-default channel has been created in the
1532 * session, explicitely require that -c chan_name needs
1535 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1536 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1540 /* Get channel from global UST domain */
1541 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1543 if (uchan
== NULL
) {
1544 /* Create default channel */
1545 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1546 usess
->buffer_type
);
1548 ret
= LTTNG_ERR_FATAL
;
1551 if (lttng_strncpy(attr
->name
, channel_name
,
1552 sizeof(attr
->name
))) {
1553 ret
= LTTNG_ERR_INVALID
;
1558 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1559 if (ret
!= LTTNG_OK
) {
1565 /* Get the newly created channel reference back */
1566 uchan
= trace_ust_find_channel_by_name(
1567 usess
->domain_global
.channels
, channel_name
);
1571 /* At this point, the session and channel exist on the tracer */
1572 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1573 filter_expression
, filter
, exclusion
);
1574 /* We have passed ownership */
1575 filter_expression
= NULL
;
1578 if (ret
!= LTTNG_OK
) {
1583 case LTTNG_DOMAIN_LOG4J
:
1584 case LTTNG_DOMAIN_JUL
:
1586 const char *default_event_name
, *default_chan_name
;
1588 struct lttng_event uevent
;
1589 struct lttng_domain tmp_dom
;
1590 struct ltt_ust_session
*usess
= session
->ust_session
;
1594 agt
= trace_ust_find_agent(usess
, domain
->type
);
1596 agt
= agent_create(domain
->type
);
1598 ret
= LTTNG_ERR_NOMEM
;
1601 agent_add(agt
, usess
->agents
);
1604 /* Create the default tracepoint. */
1605 memset(&uevent
, 0, sizeof(uevent
));
1606 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1607 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1608 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1609 if (!default_event_name
) {
1610 ret
= LTTNG_ERR_FATAL
;
1613 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1614 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1617 * The domain type is changed because we are about to enable the
1618 * default channel and event for the JUL domain that are hardcoded.
1619 * This happens in the UST domain.
1621 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1622 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1624 if (domain
->type
== LTTNG_DOMAIN_LOG4J
) {
1625 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1627 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1631 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1632 char *filter_expression_copy
= NULL
;
1635 filter_copy
= zmalloc(
1636 sizeof(struct lttng_filter_bytecode
)
1639 ret
= LTTNG_ERR_NOMEM
;
1643 memcpy(filter_copy
, filter
,
1644 sizeof(struct lttng_filter_bytecode
)
1648 if (filter_expression
) {
1649 filter_expression_copy
=
1650 strdup(filter_expression
);
1651 if (!filter_expression
) {
1652 ret
= LTTNG_ERR_NOMEM
;
1653 goto error_free_copy
;
1657 ret
= cmd_enable_event(session
, &tmp_dom
,
1658 (char *) default_chan_name
,
1659 &uevent
, filter_expression_copy
,
1660 filter_copy
, NULL
, wpipe
);
1662 filter_expression_copy
= NULL
;
1665 free(filter_expression_copy
);
1668 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1672 /* The wild card * means that everything should be enabled. */
1673 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1674 ret
= event_agent_enable_all(usess
, agt
, event
, filter
,
1678 ret
= event_agent_enable(usess
, agt
, event
, filter
,
1682 if (ret
!= LTTNG_OK
) {
1689 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1690 case LTTNG_DOMAIN_UST_PID
:
1691 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1694 ret
= LTTNG_ERR_UND
;
1701 free(filter_expression
);
1709 * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread.
1712 int enable_kevent_all(struct ltt_session
*session
,
1713 struct lttng_domain
*domain
, char *channel_name
,
1714 struct lttng_event
*event
,
1715 char *filter_expression
,
1716 struct lttng_filter_bytecode
*filter
, int wpipe
)
1719 struct lttng_channel
*attr
;
1722 assert(channel_name
);
1726 switch (domain
->type
) {
1727 case LTTNG_DOMAIN_KERNEL
:
1729 struct ltt_kernel_channel
*kchan
;
1731 assert(session
->kernel_session
);
1734 * If a non-default channel has been created in the
1735 * session, explicitely require that -c chan_name needs
1738 if (session
->kernel_session
->has_non_default_channel
1739 && channel_name
[0] == '\0') {
1740 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1744 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1745 session
->kernel_session
);
1746 if (kchan
== NULL
) {
1747 /* Create default channel */
1748 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1749 LTTNG_BUFFER_GLOBAL
);
1751 ret
= LTTNG_ERR_FATAL
;
1754 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1756 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1757 if (ret
!= LTTNG_OK
) {
1763 /* Get the newly created kernel channel pointer */
1764 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1765 session
->kernel_session
);
1769 switch (event
->type
) {
1770 case LTTNG_EVENT_SYSCALL
:
1771 ret
= event_kernel_enable_syscall(kchan
, "");
1772 if (ret
!= LTTNG_OK
) {
1776 case LTTNG_EVENT_TRACEPOINT
:
1778 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1779 * events already registered to the channel.
1781 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1783 case LTTNG_EVENT_ALL
:
1784 /* Enable syscalls and tracepoints */
1785 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1788 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1792 /* Manage return value */
1793 if (ret
!= LTTNG_OK
) {
1795 * On error, cmd_enable_channel call will take care of destroying
1796 * the created channel if it was needed.
1801 kernel_wait_quiescent(kernel_tracer_fd
);
1805 ret
= LTTNG_ERR_UND
;
1818 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1820 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1823 ssize_t nb_events
= 0;
1826 case LTTNG_DOMAIN_KERNEL
:
1827 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1828 if (nb_events
< 0) {
1829 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1833 case LTTNG_DOMAIN_UST
:
1834 nb_events
= ust_app_list_events(events
);
1835 if (nb_events
< 0) {
1836 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1840 case LTTNG_DOMAIN_LOG4J
:
1841 case LTTNG_DOMAIN_JUL
:
1842 nb_events
= agent_list_events(events
, domain
);
1843 if (nb_events
< 0) {
1844 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1849 ret
= LTTNG_ERR_UND
;
1856 /* Return negative value to differentiate return code */
1861 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1863 ssize_t
cmd_list_tracepoint_fields(int domain
,
1864 struct lttng_event_field
**fields
)
1867 ssize_t nb_fields
= 0;
1870 case LTTNG_DOMAIN_UST
:
1871 nb_fields
= ust_app_list_event_fields(fields
);
1872 if (nb_fields
< 0) {
1873 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1877 case LTTNG_DOMAIN_KERNEL
:
1878 default: /* fall-through */
1879 ret
= LTTNG_ERR_UND
;
1886 /* Return negative value to differentiate return code */
1890 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1892 return syscall_table_list(events
);
1896 * Command LTTNG_START_TRACE processed by the client thread.
1898 int cmd_start_trace(struct ltt_session
*session
)
1901 unsigned long nb_chan
= 0;
1902 struct ltt_kernel_session
*ksession
;
1903 struct ltt_ust_session
*usess
;
1907 /* Ease our life a bit ;) */
1908 ksession
= session
->kernel_session
;
1909 usess
= session
->ust_session
;
1911 /* Is the session already started? */
1912 if (session
->active
) {
1913 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1918 * Starting a session without channel is useless since after that it's not
1919 * possible to enable channel thus inform the client.
1921 if (usess
&& usess
->domain_global
.channels
) {
1923 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1927 nb_chan
+= ksession
->channel_count
;
1930 ret
= LTTNG_ERR_NO_CHANNEL
;
1934 /* Kernel tracing */
1935 if (ksession
!= NULL
) {
1936 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1937 if (ret
!= LTTNG_OK
) {
1942 /* Flag session that trace should start automatically */
1945 * Even though the start trace might fail, flag this session active so
1946 * other application coming in are started by default.
1950 ret
= ust_app_start_trace_all(usess
);
1952 ret
= LTTNG_ERR_UST_START_FAIL
;
1957 /* Flag this after a successful start. */
1958 session
->has_been_started
= 1;
1959 session
->active
= 1;
1968 * Command LTTNG_STOP_TRACE processed by the client thread.
1970 int cmd_stop_trace(struct ltt_session
*session
)
1973 struct ltt_kernel_channel
*kchan
;
1974 struct ltt_kernel_session
*ksession
;
1975 struct ltt_ust_session
*usess
;
1980 ksession
= session
->kernel_session
;
1981 usess
= session
->ust_session
;
1983 /* Session is not active. Skip everythong and inform the client. */
1984 if (!session
->active
) {
1985 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1990 if (ksession
&& ksession
->active
) {
1991 DBG("Stop kernel tracing");
1993 /* Flush metadata if exist */
1994 if (ksession
->metadata_stream_fd
>= 0) {
1995 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1997 ERR("Kernel metadata flush failed");
2001 /* Flush all buffers before stopping */
2002 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2003 ret
= kernel_flush_buffer(kchan
);
2005 ERR("Kernel flush buffer error");
2009 ret
= kernel_stop_session(ksession
);
2011 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2015 kernel_wait_quiescent(kernel_tracer_fd
);
2017 ksession
->active
= 0;
2020 if (usess
&& usess
->active
) {
2022 * Even though the stop trace might fail, flag this session inactive so
2023 * other application coming in are not started by default.
2027 ret
= ust_app_stop_trace_all(usess
);
2029 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2034 /* Flag inactive after a successful stop. */
2035 session
->active
= 0;
2043 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2045 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2046 struct lttng_uri
*uris
)
2049 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2050 struct ltt_ust_session
*usess
= session
->ust_session
;
2056 /* Can't set consumer URI if the session is active. */
2057 if (session
->active
) {
2058 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2062 /* Set the "global" consumer URIs */
2063 for (i
= 0; i
< nb_uri
; i
++) {
2064 ret
= add_uri_to_consumer(session
->consumer
,
2065 &uris
[i
], 0, session
->name
);
2066 if (ret
!= LTTNG_OK
) {
2071 /* Set UST session URIs */
2072 if (session
->ust_session
) {
2073 for (i
= 0; i
< nb_uri
; i
++) {
2074 ret
= add_uri_to_consumer(
2075 session
->ust_session
->consumer
,
2076 &uris
[i
], LTTNG_DOMAIN_UST
,
2078 if (ret
!= LTTNG_OK
) {
2084 /* Set kernel session URIs */
2085 if (session
->kernel_session
) {
2086 for (i
= 0; i
< nb_uri
; i
++) {
2087 ret
= add_uri_to_consumer(
2088 session
->kernel_session
->consumer
,
2089 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2091 if (ret
!= LTTNG_OK
) {
2098 * Make sure to set the session in output mode after we set URI since a
2099 * session can be created without URL (thus flagged in no output mode).
2101 session
->output_traces
= 1;
2103 ksess
->output_traces
= 1;
2107 usess
->output_traces
= 1;
2118 * Command LTTNG_CREATE_SESSION processed by the client thread.
2120 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2121 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2124 struct ltt_session
*session
;
2130 * Verify if the session already exist
2132 * XXX: There is no need for the session lock list here since the caller
2133 * (process_client_msg) is holding it. We might want to change that so a
2134 * single command does not lock the entire session list.
2136 session
= session_find_by_name(name
);
2137 if (session
!= NULL
) {
2138 ret
= LTTNG_ERR_EXIST_SESS
;
2142 /* Create tracing session in the registry */
2143 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2144 LTTNG_SOCK_GET_GID_CRED(creds
));
2145 if (ret
!= LTTNG_OK
) {
2150 * Get the newly created session pointer back
2152 * XXX: There is no need for the session lock list here since the caller
2153 * (process_client_msg) is holding it. We might want to change that so a
2154 * single command does not lock the entire session list.
2156 session
= session_find_by_name(name
);
2159 session
->live_timer
= live_timer
;
2160 /* Create default consumer output for the session not yet created. */
2161 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2162 if (session
->consumer
== NULL
) {
2163 ret
= LTTNG_ERR_FATAL
;
2164 goto consumer_error
;
2168 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2169 if (ret
!= LTTNG_OK
) {
2170 goto consumer_error
;
2172 session
->output_traces
= 1;
2174 session
->output_traces
= 0;
2175 DBG2("Session %s created with no output", session
->name
);
2178 session
->consumer
->enabled
= 1;
2183 session_destroy(session
);
2190 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2192 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2193 size_t nb_uri
, lttng_sock_cred
*creds
)
2196 struct ltt_session
*session
;
2197 struct snapshot_output
*new_output
= NULL
;
2203 * Create session in no output mode with URIs set to NULL. The uris we've
2204 * received are for a default snapshot output if one.
2206 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, 0);
2207 if (ret
!= LTTNG_OK
) {
2211 /* Get the newly created session pointer back. This should NEVER fail. */
2212 session
= session_find_by_name(name
);
2215 /* Flag session for snapshot mode. */
2216 session
->snapshot_mode
= 1;
2218 /* Skip snapshot output creation if no URI is given. */
2223 new_output
= snapshot_output_alloc();
2225 ret
= LTTNG_ERR_NOMEM
;
2226 goto error_snapshot_alloc
;
2229 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2230 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2232 if (ret
== -ENOMEM
) {
2233 ret
= LTTNG_ERR_NOMEM
;
2235 ret
= LTTNG_ERR_INVALID
;
2237 goto error_snapshot
;
2241 snapshot_add_output(&session
->snapshot
, new_output
);
2248 snapshot_output_destroy(new_output
);
2249 error_snapshot_alloc
:
2250 session_destroy(session
);
2256 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2258 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2261 struct ltt_ust_session
*usess
;
2262 struct ltt_kernel_session
*ksess
;
2267 usess
= session
->ust_session
;
2268 ksess
= session
->kernel_session
;
2270 /* Clean kernel session teardown */
2271 kernel_destroy_session(ksess
);
2273 /* UST session teardown */
2275 /* Close any relayd session */
2276 consumer_output_send_destroy_relayd(usess
->consumer
);
2278 /* Destroy every UST application related to this session. */
2279 ret
= ust_app_destroy_trace_all(usess
);
2281 ERR("Error in ust_app_destroy_trace_all");
2284 /* Clean up the rest. */
2285 trace_ust_destroy_session(usess
);
2289 * Must notify the kernel thread here to update it's poll set in order to
2290 * remove the channel(s)' fd just destroyed.
2292 ret
= notify_thread_pipe(wpipe
);
2294 PERROR("write kernel poll pipe");
2297 ret
= session_destroy(session
);
2303 * Command LTTNG_CALIBRATE processed by the client thread.
2305 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2310 case LTTNG_DOMAIN_KERNEL
:
2312 struct lttng_kernel_calibrate kcalibrate
;
2314 switch (calibrate
->type
) {
2315 case LTTNG_CALIBRATE_FUNCTION
:
2317 /* Default and only possible calibrate option. */
2318 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2322 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2324 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2329 case LTTNG_DOMAIN_UST
:
2331 struct lttng_ust_calibrate ucalibrate
;
2333 switch (calibrate
->type
) {
2334 case LTTNG_CALIBRATE_FUNCTION
:
2336 /* Default and only possible calibrate option. */
2337 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2341 ret
= ust_app_calibrate_glb(&ucalibrate
);
2343 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2349 ret
= LTTNG_ERR_UND
;
2360 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2362 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2363 const char *sock_path
, struct consumer_data
*cdata
)
2366 struct consumer_socket
*socket
= NULL
;
2373 case LTTNG_DOMAIN_KERNEL
:
2375 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2379 /* Can't register a consumer if there is already one */
2380 if (ksess
->consumer_fds_sent
!= 0) {
2381 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2385 sock
= lttcomm_connect_unix_sock(sock_path
);
2387 ret
= LTTNG_ERR_CONNECT_FAIL
;
2390 cdata
->cmd_sock
= sock
;
2392 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2393 if (socket
== NULL
) {
2396 PERROR("close register consumer");
2398 cdata
->cmd_sock
= -1;
2399 ret
= LTTNG_ERR_FATAL
;
2403 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2404 if (socket
->lock
== NULL
) {
2405 PERROR("zmalloc pthread mutex");
2406 ret
= LTTNG_ERR_FATAL
;
2409 pthread_mutex_init(socket
->lock
, NULL
);
2410 socket
->registered
= 1;
2413 consumer_add_socket(socket
, ksess
->consumer
);
2416 pthread_mutex_lock(&cdata
->pid_mutex
);
2418 pthread_mutex_unlock(&cdata
->pid_mutex
);
2423 /* TODO: Userspace tracing */
2424 ret
= LTTNG_ERR_UND
;
2432 consumer_destroy_socket(socket
);
2438 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2440 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2441 struct lttng_domain
**domains
)
2446 struct lttng_ht_iter iter
;
2448 if (session
->kernel_session
!= NULL
) {
2449 DBG3("Listing domains found kernel domain");
2453 if (session
->ust_session
!= NULL
) {
2454 DBG3("Listing domains found UST global domain");
2458 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2460 if (agt
->being_used
) {
2467 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2468 if (*domains
== NULL
) {
2469 ret
= LTTNG_ERR_FATAL
;
2473 if (session
->kernel_session
!= NULL
) {
2474 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2476 /* Kernel session buffer type is always GLOBAL */
2477 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2482 if (session
->ust_session
!= NULL
) {
2483 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2484 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2488 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2490 if (agt
->being_used
) {
2491 (*domains
)[index
].type
= agt
->domain
;
2492 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2502 /* Return negative value to differentiate return code */
2508 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2510 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2511 struct lttng_channel
**channels
)
2514 ssize_t nb_chan
= 0;
2517 case LTTNG_DOMAIN_KERNEL
:
2518 if (session
->kernel_session
!= NULL
) {
2519 nb_chan
= session
->kernel_session
->channel_count
;
2521 DBG3("Number of kernel channels %zd", nb_chan
);
2523 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2526 case LTTNG_DOMAIN_UST
:
2527 if (session
->ust_session
!= NULL
) {
2529 nb_chan
= lttng_ht_get_count(
2530 session
->ust_session
->domain_global
.channels
);
2533 DBG3("Number of UST global channels %zd", nb_chan
);
2535 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2540 ret
= LTTNG_ERR_UND
;
2545 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2546 if (*channels
== NULL
) {
2547 ret
= LTTNG_ERR_FATAL
;
2551 list_lttng_channels(domain
, session
, *channels
);
2557 /* Return negative value to differentiate return code */
2562 * Command LTTNG_LIST_EVENTS processed by the client thread.
2564 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2565 char *channel_name
, struct lttng_event
**events
)
2568 ssize_t nb_event
= 0;
2571 case LTTNG_DOMAIN_KERNEL
:
2572 if (session
->kernel_session
!= NULL
) {
2573 nb_event
= list_lttng_kernel_events(channel_name
,
2574 session
->kernel_session
, events
);
2577 case LTTNG_DOMAIN_UST
:
2579 if (session
->ust_session
!= NULL
) {
2580 nb_event
= list_lttng_ust_global_events(channel_name
,
2581 &session
->ust_session
->domain_global
, events
);
2585 case LTTNG_DOMAIN_LOG4J
:
2586 case LTTNG_DOMAIN_JUL
:
2587 if (session
->ust_session
) {
2588 struct lttng_ht_iter iter
;
2592 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2593 &iter
.iter
, agt
, node
.node
) {
2594 nb_event
= list_lttng_agent_events(agt
, events
);
2600 ret
= LTTNG_ERR_UND
;
2607 /* Return negative value to differentiate return code */
2612 * Using the session list, filled a lttng_session array to send back to the
2613 * client for session listing.
2615 * The session list lock MUST be acquired before calling this function. Use
2616 * session_lock_list() and session_unlock_list().
2618 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2623 struct ltt_session
*session
;
2624 struct ltt_session_list
*list
= session_get_list();
2626 DBG("Getting all available session for UID %d GID %d",
2629 * Iterate over session list and append data after the control struct in
2632 cds_list_for_each_entry(session
, &list
->head
, list
) {
2634 * Only list the sessions the user can control.
2636 if (!session_access_ok(session
, uid
, gid
)) {
2640 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2641 struct ltt_ust_session
*usess
= session
->ust_session
;
2643 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2644 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2645 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2646 ret
= build_network_session_path(sessions
[i
].path
,
2647 sizeof(sessions
[i
].path
), session
);
2649 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2650 session
->consumer
->dst
.trace_path
);
2653 PERROR("snprintf session path");
2657 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2658 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2659 sessions
[i
].enabled
= session
->active
;
2660 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2661 sessions
[i
].live_timer_interval
= session
->live_timer
;
2667 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2668 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2670 int cmd_data_pending(struct ltt_session
*session
)
2673 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2674 struct ltt_ust_session
*usess
= session
->ust_session
;
2678 /* Session MUST be stopped to ask for data availability. */
2679 if (session
->active
) {
2680 ret
= LTTNG_ERR_SESSION_STARTED
;
2684 * If stopped, just make sure we've started before else the above call
2685 * will always send that there is data pending.
2687 * The consumer assumes that when the data pending command is received,
2688 * the trace has been started before or else no output data is written
2689 * by the streams which is a condition for data pending. So, this is
2690 * *VERY* important that we don't ask the consumer before a start
2693 if (!session
->has_been_started
) {
2699 if (ksess
&& ksess
->consumer
) {
2700 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2702 /* Data is still being extracted for the kernel. */
2707 if (usess
&& usess
->consumer
) {
2708 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2710 /* Data is still being extracted for the kernel. */
2715 /* Data is ready to be read by a viewer */
2723 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2725 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2727 int cmd_snapshot_add_output(struct ltt_session
*session
,
2728 struct lttng_snapshot_output
*output
, uint32_t *id
)
2731 struct snapshot_output
*new_output
;
2736 DBG("Cmd snapshot add output for session %s", session
->name
);
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
;
2747 /* Only one output is allowed until we have the "tee" feature. */
2748 if (session
->snapshot
.nb_output
== 1) {
2749 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2753 new_output
= snapshot_output_alloc();
2755 ret
= LTTNG_ERR_NOMEM
;
2759 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2760 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2761 &session
->snapshot
);
2763 if (ret
== -ENOMEM
) {
2764 ret
= LTTNG_ERR_NOMEM
;
2766 ret
= LTTNG_ERR_INVALID
;
2772 snapshot_add_output(&session
->snapshot
, new_output
);
2774 *id
= new_output
->id
;
2781 snapshot_output_destroy(new_output
);
2787 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2789 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2791 int cmd_snapshot_del_output(struct ltt_session
*session
,
2792 struct lttng_snapshot_output
*output
)
2795 struct snapshot_output
*sout
= NULL
;
2803 * Permission denied to create an output if the session is not
2804 * set in no output mode.
2806 if (session
->output_traces
) {
2807 ret
= LTTNG_ERR_EPERM
;
2812 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2814 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2815 } else if (*output
->name
!= '\0') {
2816 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2818 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2821 ret
= LTTNG_ERR_INVALID
;
2825 snapshot_delete_output(&session
->snapshot
, sout
);
2826 snapshot_output_destroy(sout
);
2835 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2837 * If no output is available, outputs is untouched and 0 is returned.
2839 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2841 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2842 struct lttng_snapshot_output
**outputs
)
2845 struct lttng_snapshot_output
*list
= NULL
;
2846 struct lttng_ht_iter iter
;
2847 struct snapshot_output
*output
;
2852 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2855 * Permission denied to create an output if the session is not
2856 * set in no output mode.
2858 if (session
->output_traces
) {
2859 ret
= -LTTNG_ERR_EPERM
;
2863 if (session
->snapshot
.nb_output
== 0) {
2868 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2870 ret
= -LTTNG_ERR_NOMEM
;
2874 /* Copy list from session to the new list object. */
2876 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2877 output
, node
.node
) {
2878 assert(output
->consumer
);
2879 list
[idx
].id
= output
->id
;
2880 list
[idx
].max_size
= output
->max_size
;
2881 if (lttng_strncpy(list
[idx
].name
, output
->name
,
2882 sizeof(list
[idx
].name
))) {
2883 ret
= -LTTNG_ERR_INVALID
;
2886 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2887 if (lttng_strncpy(list
[idx
].ctrl_url
,
2888 output
->consumer
->dst
.trace_path
,
2889 sizeof(list
[idx
].ctrl_url
))) {
2890 ret
= -LTTNG_ERR_INVALID
;
2895 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2896 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2898 ret
= -LTTNG_ERR_NOMEM
;
2903 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2904 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2906 ret
= -LTTNG_ERR_NOMEM
;
2915 ret
= session
->snapshot
.nb_output
;
2923 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2924 * snapshot output is *not* set with a remote destination.
2926 * Return 0 on success or a LTTNG_ERR code.
2928 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2929 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2932 struct lttng_ht_iter iter
;
2933 struct consumer_socket
*socket
;
2936 assert(snap_output
);
2939 DBG2("Set relayd object from snapshot output");
2941 /* Ignore if snapshot consumer output is not network. */
2942 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2947 * For each consumer socket, create and send the relayd object of the
2951 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2952 socket
, node
.node
) {
2953 ret
= send_consumer_relayd_sockets(0, session
->id
,
2954 snap_output
->consumer
, socket
,
2955 session
->name
, session
->hostname
,
2956 session
->live_timer
);
2957 if (ret
!= LTTNG_OK
) {
2969 * Record a kernel snapshot.
2971 * Return LTTNG_OK on success or a LTTNG_ERR code.
2973 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2974 struct snapshot_output
*output
, struct ltt_session
*session
,
2975 int wait
, uint64_t nb_packets_per_stream
)
2985 * Copy kernel session sockets so we can communicate with the right
2986 * consumer for the snapshot record command.
2988 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2990 ret
= LTTNG_ERR_NOMEM
;
2994 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2995 if (ret
!= LTTNG_OK
) {
2996 goto error_snapshot
;
2999 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3000 if (ret
!= LTTNG_OK
) {
3001 goto error_snapshot
;
3008 /* Clean up copied sockets so this output can use some other later on. */
3009 consumer_destroy_output_sockets(output
->consumer
);
3016 * Record a UST snapshot.
3018 * Return 0 on success or a LTTNG_ERR error code.
3020 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3021 struct snapshot_output
*output
, struct ltt_session
*session
,
3022 int wait
, uint64_t nb_packets_per_stream
)
3031 * Copy UST session sockets so we can communicate with the right
3032 * consumer for the snapshot record command.
3034 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3036 ret
= LTTNG_ERR_NOMEM
;
3040 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3041 if (ret
!= LTTNG_OK
) {
3042 goto error_snapshot
;
3045 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3049 ret
= LTTNG_ERR_INVALID
;
3052 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3055 goto error_snapshot
;
3061 /* Clean up copied sockets so this output can use some other later on. */
3062 consumer_destroy_output_sockets(output
->consumer
);
3068 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3069 uint64_t cur_nr_packets
)
3071 uint64_t tot_size
= 0;
3073 if (session
->kernel_session
) {
3074 struct ltt_kernel_channel
*chan
;
3075 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3077 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3078 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3080 * Don't take channel into account if we
3081 * already grab all its packets.
3085 tot_size
+= chan
->channel
->attr
.subbuf_size
3086 * chan
->stream_count
;
3090 if (session
->ust_session
) {
3091 struct ltt_ust_session
*usess
= session
->ust_session
;
3093 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3101 * Calculate the number of packets we can grab from each stream that
3102 * fits within the overall snapshot max size.
3104 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3105 * the number of packets per stream.
3107 * TODO: this approach is not perfect: we consider the worse case
3108 * (packet filling the sub-buffers) as an upper bound, but we could do
3109 * better if we do this calculation while we actually grab the packet
3110 * content: we would know how much padding we don't actually store into
3113 * This algorithm is currently bounded by the number of packets per
3116 * Since we call this algorithm before actually grabbing the data, it's
3117 * an approximation: for instance, applications could appear/disappear
3118 * in between this call and actually grabbing data.
3121 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3124 uint64_t cur_nb_packets
= 0;
3127 return 0; /* Infinite */
3130 size_left
= max_size
;
3132 uint64_t one_more_packet_tot_size
;
3134 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3136 if (!one_more_packet_tot_size
) {
3137 /* We are already grabbing all packets. */
3140 size_left
-= one_more_packet_tot_size
;
3141 if (size_left
< 0) {
3146 if (!cur_nb_packets
) {
3147 /* Not enough room to grab one packet of each stream, error. */
3150 return cur_nb_packets
;
3154 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3156 * The wait parameter is ignored so this call always wait for the snapshot to
3157 * complete before returning.
3159 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3161 int cmd_snapshot_record(struct ltt_session
*session
,
3162 struct lttng_snapshot_output
*output
, int wait
)
3165 unsigned int use_tmp_output
= 0;
3166 struct snapshot_output tmp_output
;
3167 unsigned int snapshot_success
= 0;
3173 DBG("Cmd snapshot record for session %s", session
->name
);
3175 /* Get the datetime for the snapshot output directory. */
3176 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", datetime
,
3179 ret
= LTTNG_ERR_INVALID
;
3184 * Permission denied to create an output if the session is not
3185 * set in no output mode.
3187 if (session
->output_traces
) {
3188 ret
= LTTNG_ERR_EPERM
;
3192 /* The session needs to be started at least once. */
3193 if (!session
->has_been_started
) {
3194 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3198 /* Use temporary output for the session. */
3199 if (*output
->ctrl_url
!= '\0') {
3200 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3201 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3204 if (ret
== -ENOMEM
) {
3205 ret
= LTTNG_ERR_NOMEM
;
3207 ret
= LTTNG_ERR_INVALID
;
3211 /* Use the global session count for the temporary snapshot. */
3212 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3214 /* Use the global datetime */
3215 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3219 if (use_tmp_output
) {
3220 int64_t nb_packets_per_stream
;
3222 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3223 tmp_output
.max_size
);
3224 if (nb_packets_per_stream
< 0) {
3225 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3229 if (session
->kernel_session
) {
3230 ret
= record_kernel_snapshot(session
->kernel_session
,
3231 &tmp_output
, session
,
3232 wait
, nb_packets_per_stream
);
3233 if (ret
!= LTTNG_OK
) {
3238 if (session
->ust_session
) {
3239 ret
= record_ust_snapshot(session
->ust_session
,
3240 &tmp_output
, session
,
3241 wait
, nb_packets_per_stream
);
3242 if (ret
!= LTTNG_OK
) {
3247 snapshot_success
= 1;
3249 struct snapshot_output
*sout
;
3250 struct lttng_ht_iter iter
;
3253 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3254 &iter
.iter
, sout
, node
.node
) {
3255 int64_t nb_packets_per_stream
;
3258 * Make a local copy of the output and assign the possible
3259 * temporary value given by the caller.
3261 memset(&tmp_output
, 0, sizeof(tmp_output
));
3262 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3264 if (output
->max_size
!= (uint64_t) -1ULL) {
3265 tmp_output
.max_size
= output
->max_size
;
3268 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3269 tmp_output
.max_size
);
3270 if (nb_packets_per_stream
< 0) {
3271 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3276 /* Use temporary name. */
3277 if (*output
->name
!= '\0') {
3278 strncpy(tmp_output
.name
, output
->name
,
3279 sizeof(tmp_output
.name
));
3282 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3283 memcpy(tmp_output
.datetime
, datetime
, sizeof(datetime
));
3285 if (session
->kernel_session
) {
3286 ret
= record_kernel_snapshot(session
->kernel_session
,
3287 &tmp_output
, session
,
3288 wait
, nb_packets_per_stream
);
3289 if (ret
!= LTTNG_OK
) {
3295 if (session
->ust_session
) {
3296 ret
= record_ust_snapshot(session
->ust_session
,
3297 &tmp_output
, session
,
3298 wait
, nb_packets_per_stream
);
3299 if (ret
!= LTTNG_OK
) {
3304 snapshot_success
= 1;
3309 if (snapshot_success
) {
3310 session
->snapshot
.nb_snapshot
++;
3312 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3320 * Init command subsystem.
3325 * Set network sequence index to 1 for streams to match a relayd
3326 * socket on the consumer side.
3328 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3329 relayd_net_seq_idx
= 1;
3330 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3332 DBG("Command subsystem initialized");