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.
23 #include <urcu/list.h>
24 #include <urcu/uatomic.h>
26 #include <common/defaults.h>
27 #include <common/common.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/relayd/relayd.h>
30 #include <common/utils.h>
35 #include "health-sessiond.h"
37 #include "kernel-consumer.h"
38 #include "lttng-sessiond.h"
46 * Used to keep a unique index for each relayd socket created where this value
47 * is associated with streams on the consumer so it can match the right relayd
48 * to send to. It must be accessed with the relayd_net_seq_idx_lock
51 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
52 static uint64_t relayd_net_seq_idx
;
55 * Create a session path used by list_lttng_sessions for the case that the
56 * session consumer is on the network.
58 static int build_network_session_path(char *dst
, size_t size
,
59 struct ltt_session
*session
)
61 int ret
, kdata_port
, udata_port
;
62 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
63 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
68 memset(tmp_urls
, 0, sizeof(tmp_urls
));
69 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
71 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
73 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
74 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
75 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
78 if (session
->ust_session
&& session
->ust_session
->consumer
) {
79 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
80 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
83 if (uuri
== NULL
&& kuri
== NULL
) {
84 uri
= &session
->consumer
->dst
.net
.control
;
85 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
86 } else if (kuri
&& uuri
) {
87 ret
= uri_compare(kuri
, uuri
);
91 /* Build uuri URL string */
92 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
99 } else if (kuri
&& uuri
== NULL
) {
101 } else if (uuri
&& kuri
== NULL
) {
105 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
111 * Do we have a UST url set. If yes, this means we have both kernel and UST
114 if (*tmp_uurl
!= '\0') {
115 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
116 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
119 if (kuri
|| (!kuri
&& !uuri
)) {
122 /* No kernel URI, use the UST port. */
125 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
133 * Fill lttng_channel array of all channels.
135 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
136 struct lttng_channel
*channels
)
139 struct ltt_kernel_channel
*kchan
;
141 DBG("Listing channels for session %s", session
->name
);
144 case LTTNG_DOMAIN_KERNEL
:
145 /* Kernel channels */
146 if (session
->kernel_session
!= NULL
) {
147 cds_list_for_each_entry(kchan
,
148 &session
->kernel_session
->channel_list
.head
, list
) {
149 /* Copy lttng_channel struct to array */
150 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
151 channels
[i
].enabled
= kchan
->enabled
;
156 case LTTNG_DOMAIN_UST
:
158 struct lttng_ht_iter iter
;
159 struct ltt_ust_channel
*uchan
;
162 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
163 &iter
.iter
, uchan
, node
.node
) {
164 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
165 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
166 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
167 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
168 channels
[i
].attr
.switch_timer_interval
=
169 uchan
->attr
.switch_timer_interval
;
170 channels
[i
].attr
.read_timer_interval
=
171 uchan
->attr
.read_timer_interval
;
172 channels
[i
].enabled
= uchan
->enabled
;
173 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
174 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
175 switch (uchan
->attr
.output
) {
178 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
192 * Create a list of agent domain events.
194 * Return number of events in list on success or else a negative value.
196 static int list_lttng_agent_events(struct agent
*agt
,
197 struct lttng_event
**events
)
200 unsigned int nb_event
= 0;
201 struct agent_event
*event
;
202 struct lttng_event
*tmp_events
;
203 struct lttng_ht_iter iter
;
208 DBG3("Listing agent events");
211 nb_event
= lttng_ht_get_count(agt
->events
);
218 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
220 PERROR("zmalloc agent events session");
221 ret
= -LTTNG_ERR_FATAL
;
226 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
227 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
228 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
229 tmp_events
[i
].enabled
= event
->enabled
;
230 tmp_events
[i
].loglevel
= event
->loglevel
;
231 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
236 *events
= tmp_events
;
240 assert(nb_event
== i
);
245 * Create a list of ust global domain events.
247 static int list_lttng_ust_global_events(char *channel_name
,
248 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
251 unsigned int nb_event
= 0;
252 struct lttng_ht_iter iter
;
253 struct lttng_ht_node_str
*node
;
254 struct ltt_ust_channel
*uchan
;
255 struct ltt_ust_event
*uevent
;
256 struct lttng_event
*tmp
;
258 DBG("Listing UST global events for channel %s", channel_name
);
262 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
263 node
= lttng_ht_iter_get_node_str(&iter
);
265 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
269 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
271 nb_event
= lttng_ht_get_count(uchan
->events
);
277 DBG3("Listing UST global %d events", nb_event
);
279 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
281 ret
= LTTNG_ERR_FATAL
;
285 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
286 if (uevent
->internal
) {
287 /* This event should remain hidden from clients */
291 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
292 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
293 tmp
[i
].enabled
= uevent
->enabled
;
295 switch (uevent
->attr
.instrumentation
) {
296 case LTTNG_UST_TRACEPOINT
:
297 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
299 case LTTNG_UST_PROBE
:
300 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
302 case LTTNG_UST_FUNCTION
:
303 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
307 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
308 switch (uevent
->attr
.loglevel_type
) {
309 case LTTNG_UST_LOGLEVEL_ALL
:
310 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
312 case LTTNG_UST_LOGLEVEL_RANGE
:
313 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
315 case LTTNG_UST_LOGLEVEL_SINGLE
:
316 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
319 if (uevent
->filter
) {
322 if (uevent
->exclusion
) {
323 tmp
[i
].exclusion
= 1;
337 * Fill lttng_event array of all kernel events in the channel.
339 static int list_lttng_kernel_events(char *channel_name
,
340 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
343 unsigned int nb_event
;
344 struct ltt_kernel_event
*event
;
345 struct ltt_kernel_channel
*kchan
;
347 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
349 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
353 nb_event
= kchan
->event_count
;
355 DBG("Listing events for channel %s", kchan
->channel
->name
);
362 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
363 if (*events
== NULL
) {
364 ret
= LTTNG_ERR_FATAL
;
368 /* Kernel channels */
369 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
370 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
371 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
372 (*events
)[i
].enabled
= event
->enabled
;
374 switch (event
->event
->instrumentation
) {
375 case LTTNG_KERNEL_TRACEPOINT
:
376 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
378 case LTTNG_KERNEL_KRETPROBE
:
379 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
380 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
381 sizeof(struct lttng_kernel_kprobe
));
383 case LTTNG_KERNEL_KPROBE
:
384 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
385 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
386 sizeof(struct lttng_kernel_kprobe
));
388 case LTTNG_KERNEL_FUNCTION
:
389 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
390 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
391 sizeof(struct lttng_kernel_function
));
393 case LTTNG_KERNEL_NOOP
:
394 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
396 case LTTNG_KERNEL_SYSCALL
:
397 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
399 case LTTNG_KERNEL_ALL
:
410 new_size
= syscall_list_channel(kchan
, events
, nb_event
);
422 /* Negate the error code to differentiate the size from an error */
427 * Add URI so the consumer output object. Set the correct path depending on the
428 * domain adding the default trace directory.
430 static int add_uri_to_consumer(struct consumer_output
*consumer
,
431 struct lttng_uri
*uri
, int domain
, const char *session_name
)
434 const char *default_trace_dir
;
438 if (consumer
== NULL
) {
439 DBG("No consumer detected. Don't add URI. Stopping.");
440 ret
= LTTNG_ERR_NO_CONSUMER
;
445 case LTTNG_DOMAIN_KERNEL
:
446 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
448 case LTTNG_DOMAIN_UST
:
449 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
453 * This case is possible is we try to add the URI to the global tracing
454 * session consumer object which in this case there is no subdir.
456 default_trace_dir
= "";
459 switch (uri
->dtype
) {
462 DBG2("Setting network URI to consumer");
464 if (consumer
->type
== CONSUMER_DST_NET
) {
465 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
466 consumer
->dst
.net
.control_isset
) ||
467 (uri
->stype
== LTTNG_STREAM_DATA
&&
468 consumer
->dst
.net
.data_isset
)) {
469 ret
= LTTNG_ERR_URL_EXIST
;
473 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
476 consumer
->type
= CONSUMER_DST_NET
;
478 /* Set URI into consumer output object */
479 ret
= consumer_set_network_uri(consumer
, uri
);
483 } else if (ret
== 1) {
485 * URI was the same in the consumer so we do not append the subdir
486 * again so to not duplicate output dir.
492 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
493 ret
= consumer_set_subdir(consumer
, session_name
);
495 ret
= LTTNG_ERR_FATAL
;
500 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
501 /* On a new subdir, reappend the default trace dir. */
502 strncat(consumer
->subdir
, default_trace_dir
,
503 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
504 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
509 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
510 memset(consumer
->dst
.trace_path
, 0,
511 sizeof(consumer
->dst
.trace_path
));
512 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
513 sizeof(consumer
->dst
.trace_path
));
514 /* Append default trace dir */
515 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
516 sizeof(consumer
->dst
.trace_path
) -
517 strlen(consumer
->dst
.trace_path
) - 1);
518 /* Flag consumer as local. */
519 consumer
->type
= CONSUMER_DST_LOCAL
;
530 * Init tracing by creating trace directory and sending fds kernel consumer.
532 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
535 struct lttng_ht_iter iter
;
536 struct consumer_socket
*socket
;
542 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
543 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
545 pthread_mutex_lock(socket
->lock
);
546 ret
= kernel_consumer_send_session(socket
, session
);
547 pthread_mutex_unlock(socket
->lock
);
549 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
561 * Create a socket to the relayd using the URI.
563 * On success, the relayd_sock pointer is set to the created socket.
564 * Else, it's stays untouched and a lttcomm error code is returned.
566 static int create_connect_relayd(struct lttng_uri
*uri
,
567 struct lttcomm_relayd_sock
**relayd_sock
)
570 struct lttcomm_relayd_sock
*rsock
;
572 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
573 RELAYD_VERSION_COMM_MINOR
);
575 ret
= LTTNG_ERR_FATAL
;
580 * Connect to relayd so we can proceed with a session creation. This call
581 * can possibly block for an arbitrary amount of time to set the health
582 * state to be in poll execution.
585 ret
= relayd_connect(rsock
);
588 ERR("Unable to reach lttng-relayd");
589 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
593 /* Create socket for control stream. */
594 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
595 DBG3("Creating relayd stream socket from URI");
597 /* Check relayd version */
598 ret
= relayd_version_check(rsock
);
600 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
603 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
604 DBG3("Creating relayd data socket from URI");
606 /* Command is not valid */
607 ERR("Relayd invalid stream type: %d", uri
->stype
);
608 ret
= LTTNG_ERR_INVALID
;
612 *relayd_sock
= rsock
;
617 /* The returned value is not useful since we are on an error path. */
618 (void) relayd_close(rsock
);
626 * Connect to the relayd using URI and send the socket to the right consumer.
628 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
629 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
630 struct consumer_socket
*consumer_sock
,
631 char *session_name
, char *hostname
, int session_live_timer
)
634 struct lttcomm_relayd_sock
*rsock
= NULL
;
636 /* Connect to relayd and make version check if uri is the control. */
637 ret
= create_connect_relayd(relayd_uri
, &rsock
);
638 if (ret
!= LTTNG_OK
) {
643 /* Set the network sequence index if not set. */
644 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
645 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
647 * Increment net_seq_idx because we are about to transfer the
648 * new relayd socket to the consumer.
649 * Assign unique key so the consumer can match streams.
651 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
652 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
655 /* Send relayd socket to consumer. */
656 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
657 relayd_uri
->stype
, session_id
,
658 session_name
, hostname
, session_live_timer
);
660 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
664 /* Flag that the corresponding socket was sent. */
665 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
666 consumer_sock
->control_sock_sent
= 1;
667 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
668 consumer_sock
->data_sock_sent
= 1;
674 * Close socket which was dup on the consumer side. The session daemon does
675 * NOT keep track of the relayd socket(s) once transfer to the consumer.
679 (void) relayd_close(rsock
);
683 if (ret
!= LTTNG_OK
) {
685 * The consumer output for this session should not be used anymore
686 * since the relayd connection failed thus making any tracing or/and
687 * streaming not usable.
689 consumer
->enabled
= 0;
695 * Send both relayd sockets to a specific consumer and domain. This is a
696 * helper function to facilitate sending the information to the consumer for a
699 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
700 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
701 char *session_name
, char *hostname
, int session_live_timer
)
708 /* Sending control relayd socket. */
709 if (!sock
->control_sock_sent
) {
710 ret
= send_consumer_relayd_socket(domain
, session_id
,
711 &consumer
->dst
.net
.control
, consumer
, sock
,
712 session_name
, hostname
, session_live_timer
);
713 if (ret
!= LTTNG_OK
) {
718 /* Sending data relayd socket. */
719 if (!sock
->data_sock_sent
) {
720 ret
= send_consumer_relayd_socket(domain
, session_id
,
721 &consumer
->dst
.net
.data
, consumer
, sock
,
722 session_name
, hostname
, session_live_timer
);
723 if (ret
!= LTTNG_OK
) {
733 * Setup relayd connections for a tracing session. First creates the socket to
734 * the relayd and send them to the right domain consumer. Consumer type MUST be
737 int cmd_setup_relayd(struct ltt_session
*session
)
740 struct ltt_ust_session
*usess
;
741 struct ltt_kernel_session
*ksess
;
742 struct consumer_socket
*socket
;
743 struct lttng_ht_iter iter
;
747 usess
= session
->ust_session
;
748 ksess
= session
->kernel_session
;
750 DBG("Setting relayd for session %s", session
->name
);
754 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
755 && usess
->consumer
->enabled
) {
756 /* For each consumer socket, send relayd sockets */
757 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
759 pthread_mutex_lock(socket
->lock
);
760 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
761 usess
->consumer
, socket
,
762 session
->name
, session
->hostname
,
763 session
->live_timer
);
764 pthread_mutex_unlock(socket
->lock
);
765 if (ret
!= LTTNG_OK
) {
768 /* Session is now ready for network streaming. */
769 session
->net_handle
= 1;
773 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
774 && ksess
->consumer
->enabled
) {
775 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
777 pthread_mutex_lock(socket
->lock
);
778 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
779 ksess
->consumer
, socket
,
780 session
->name
, session
->hostname
,
781 session
->live_timer
);
782 pthread_mutex_unlock(socket
->lock
);
783 if (ret
!= LTTNG_OK
) {
786 /* Session is now ready for network streaming. */
787 session
->net_handle
= 1;
797 * Start a kernel session by opening all necessary streams.
799 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
802 struct ltt_kernel_channel
*kchan
;
804 /* Open kernel metadata */
805 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
806 ret
= kernel_open_metadata(ksess
);
808 ret
= LTTNG_ERR_KERN_META_FAIL
;
813 /* Open kernel metadata stream */
814 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
815 ret
= kernel_open_metadata_stream(ksess
);
817 ERR("Kernel create metadata stream failed");
818 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
823 /* For each channel */
824 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
825 if (kchan
->stream_count
== 0) {
826 ret
= kernel_open_channel_stream(kchan
);
828 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
831 /* Update the stream global counter */
832 ksess
->stream_count_global
+= ret
;
836 /* Setup kernel consumer socket and send fds to it */
837 ret
= init_kernel_tracing(ksess
);
839 ret
= LTTNG_ERR_KERN_START_FAIL
;
843 /* This start the kernel tracing */
844 ret
= kernel_start_session(ksess
);
846 ret
= LTTNG_ERR_KERN_START_FAIL
;
850 /* Quiescent wait after starting trace */
851 kernel_wait_quiescent(kernel_tracer_fd
);
862 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
864 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
868 struct ltt_ust_session
*usess
;
870 usess
= session
->ust_session
;
875 case LTTNG_DOMAIN_KERNEL
:
877 ret
= channel_kernel_disable(session
->kernel_session
,
879 if (ret
!= LTTNG_OK
) {
883 kernel_wait_quiescent(kernel_tracer_fd
);
886 case LTTNG_DOMAIN_UST
:
888 struct ltt_ust_channel
*uchan
;
889 struct lttng_ht
*chan_ht
;
891 chan_ht
= usess
->domain_global
.channels
;
893 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
895 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
899 ret
= channel_ust_disable(usess
, uchan
);
900 if (ret
!= LTTNG_OK
) {
906 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
918 * Command LTTNG_TRACK_PID processed by the client thread.
920 * Called with session lock held.
922 int cmd_track_pid(struct ltt_session
*session
, int domain
, int pid
)
929 case LTTNG_DOMAIN_KERNEL
:
931 struct ltt_kernel_session
*ksess
;
933 ksess
= session
->kernel_session
;
935 ret
= kernel_track_pid(ksess
, pid
);
936 if (ret
!= LTTNG_OK
) {
940 kernel_wait_quiescent(kernel_tracer_fd
);
943 case LTTNG_DOMAIN_UST
:
945 struct ltt_ust_session
*usess
;
947 usess
= session
->ust_session
;
949 ret
= trace_ust_track_pid(usess
, pid
);
950 if (ret
!= LTTNG_OK
) {
956 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
968 * Command LTTNG_UNTRACK_PID processed by the client thread.
970 * Called with session lock held.
972 int cmd_untrack_pid(struct ltt_session
*session
, int domain
, int pid
)
979 case LTTNG_DOMAIN_KERNEL
:
981 struct ltt_kernel_session
*ksess
;
983 ksess
= session
->kernel_session
;
985 ret
= kernel_untrack_pid(ksess
, pid
);
986 if (ret
!= LTTNG_OK
) {
990 kernel_wait_quiescent(kernel_tracer_fd
);
993 case LTTNG_DOMAIN_UST
:
995 struct ltt_ust_session
*usess
;
997 usess
= session
->ust_session
;
999 ret
= trace_ust_untrack_pid(usess
, pid
);
1000 if (ret
!= LTTNG_OK
) {
1006 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1018 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1020 * The wpipe arguments is used as a notifier for the kernel thread.
1022 int cmd_enable_channel(struct ltt_session
*session
,
1023 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1026 struct ltt_ust_session
*usess
= session
->ust_session
;
1027 struct lttng_ht
*chan_ht
;
1034 len
= strnlen(attr
->name
, sizeof(attr
->name
));
1036 /* Validate channel name */
1037 if (attr
->name
[0] == '.' ||
1038 memchr(attr
->name
, '/', len
) != NULL
) {
1039 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1043 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1048 * Don't try to enable a channel if the session has been started at
1049 * some point in time before. The tracer does not allow it.
1051 if (session
->has_been_started
) {
1052 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1057 * If the session is a live session, remove the switch timer, the
1058 * live timer does the same thing but sends also synchronisation
1059 * beacons for inactive streams.
1061 if (session
->live_timer
> 0) {
1062 attr
->attr
.live_timer_interval
= session
->live_timer
;
1063 attr
->attr
.switch_timer_interval
= 0;
1067 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1068 * mode and with less than 2 subbuf so block it right away and send back an
1069 * invalid attribute error.
1071 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
1072 ret
= LTTNG_ERR_INVALID
;
1076 switch (domain
->type
) {
1077 case LTTNG_DOMAIN_KERNEL
:
1079 struct ltt_kernel_channel
*kchan
;
1081 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1082 session
->kernel_session
);
1083 if (kchan
== NULL
) {
1084 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1085 if (attr
->name
[0] != '\0') {
1086 session
->kernel_session
->has_non_default_channel
= 1;
1089 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1092 if (ret
!= LTTNG_OK
) {
1096 kernel_wait_quiescent(kernel_tracer_fd
);
1099 case LTTNG_DOMAIN_UST
:
1101 struct ltt_ust_channel
*uchan
;
1103 chan_ht
= usess
->domain_global
.channels
;
1105 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1106 if (uchan
== NULL
) {
1107 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1108 if (attr
->name
[0] != '\0') {
1109 usess
->has_non_default_channel
= 1;
1112 ret
= channel_ust_enable(usess
, uchan
);
1117 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1128 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1130 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1132 struct lttng_event
*event
)
1137 DBG("Disable event command for event \'%s\'", event
->name
);
1139 event_name
= event
->name
;
1141 /* Error out on unhandled search criteria */
1142 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1143 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1144 return LTTNG_ERR_UNK
;
1150 case LTTNG_DOMAIN_KERNEL
:
1152 struct ltt_kernel_channel
*kchan
;
1153 struct ltt_kernel_session
*ksess
;
1155 ksess
= session
->kernel_session
;
1158 * If a non-default channel has been created in the
1159 * session, explicitely require that -c chan_name needs
1162 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1163 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1167 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1168 if (kchan
== NULL
) {
1169 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1173 switch (event
->type
) {
1174 case LTTNG_EVENT_ALL
:
1175 ret
= event_kernel_disable_event_all(kchan
);
1176 if (ret
!= LTTNG_OK
) {
1180 case LTTNG_EVENT_TRACEPOINT
: /* fall-through */
1181 case LTTNG_EVENT_SYSCALL
:
1182 if (!strcmp(event_name
, "*")) {
1183 ret
= event_kernel_disable_event_type(kchan
,
1186 ret
= event_kernel_disable_event(kchan
,
1189 if (ret
!= LTTNG_OK
) {
1193 case LTTNG_EVENT_PROBE
:
1194 case LTTNG_EVENT_FUNCTION
:
1195 case LTTNG_EVENT_FUNCTION_ENTRY
:
1196 ret
= event_kernel_disable_event(kchan
, event_name
);
1197 if (ret
!= LTTNG_OK
) {
1202 ret
= LTTNG_ERR_UNK
;
1206 kernel_wait_quiescent(kernel_tracer_fd
);
1209 case LTTNG_DOMAIN_UST
:
1211 struct ltt_ust_channel
*uchan
;
1212 struct ltt_ust_session
*usess
;
1214 usess
= session
->ust_session
;
1217 * If a non-default channel has been created in the
1218 * session, explicitely require that -c chan_name needs
1221 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1222 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1226 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1228 if (uchan
== NULL
) {
1229 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1233 switch (event
->type
) {
1234 case LTTNG_EVENT_ALL
:
1235 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1236 if (ret
!= LTTNG_OK
) {
1241 ret
= LTTNG_ERR_UNK
;
1245 DBG3("Disable UST event %s in channel %s completed", event_name
,
1249 case LTTNG_DOMAIN_LOG4J
:
1250 case LTTNG_DOMAIN_JUL
:
1251 case LTTNG_DOMAIN_PYTHON
:
1254 struct ltt_ust_session
*usess
= session
->ust_session
;
1258 switch (event
->type
) {
1259 case LTTNG_EVENT_ALL
:
1262 ret
= LTTNG_ERR_UNK
;
1266 agt
= trace_ust_find_agent(usess
, domain
);
1268 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1271 /* The wild card * means that everything should be disabled. */
1272 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1273 ret
= event_agent_disable_all(usess
, agt
);
1275 ret
= event_agent_disable(usess
, agt
, event_name
);
1277 if (ret
!= LTTNG_OK
) {
1284 ret
= LTTNG_ERR_UND
;
1296 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1298 int cmd_add_context(struct ltt_session
*session
, int domain
,
1299 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1301 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1304 case LTTNG_DOMAIN_KERNEL
:
1305 assert(session
->kernel_session
);
1307 if (session
->kernel_session
->channel_count
== 0) {
1308 /* Create default channel */
1309 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1310 if (ret
!= LTTNG_OK
) {
1313 chan_kern_created
= 1;
1315 /* Add kernel context to kernel tracer */
1316 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1317 if (ret
!= LTTNG_OK
) {
1321 case LTTNG_DOMAIN_UST
:
1323 struct ltt_ust_session
*usess
= session
->ust_session
;
1324 unsigned int chan_count
;
1328 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1329 if (chan_count
== 0) {
1330 struct lttng_channel
*attr
;
1331 /* Create default channel */
1332 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1334 ret
= LTTNG_ERR_FATAL
;
1338 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1339 if (ret
!= LTTNG_OK
) {
1344 chan_ust_created
= 1;
1347 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1348 if (ret
!= LTTNG_OK
) {
1354 ret
= LTTNG_ERR_UND
;
1361 if (chan_kern_created
) {
1362 struct ltt_kernel_channel
*kchan
=
1363 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1364 session
->kernel_session
);
1365 /* Created previously, this should NOT fail. */
1367 kernel_destroy_channel(kchan
);
1370 if (chan_ust_created
) {
1371 struct ltt_ust_channel
*uchan
=
1372 trace_ust_find_channel_by_name(
1373 session
->ust_session
->domain_global
.channels
,
1374 DEFAULT_CHANNEL_NAME
);
1375 /* Created previously, this should NOT fail. */
1377 /* Remove from the channel list of the session. */
1378 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1380 trace_ust_destroy_channel(uchan
);
1385 static int validate_event_name(const char *name
)
1388 const char *c
= name
;
1389 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1390 bool null_terminated
= false;
1393 * Make sure that unescaped wildcards are only used as the last
1394 * character of the event name.
1396 while (c
< event_name_end
) {
1399 null_terminated
= true;
1405 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1406 /* Wildcard is not the last character */
1407 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1416 if (!ret
&& !null_terminated
) {
1417 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1422 static inline bool name_starts_with(const char *name
, const char *prefix
)
1424 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1426 return !strncmp(name
, prefix
, max_cmp_len
);
1429 /* Perform userspace-specific event name validation */
1430 static int validate_ust_event_name(const char *name
)
1440 * Check name against all internal UST event component namespaces used
1443 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1444 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1445 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1453 static int cmd_enable_event_internal(struct ltt_session
*session
,
1454 struct lttng_domain
*domain
,
1455 char *channel_name
, struct lttng_event
*event
,
1456 char *filter_expression
,
1457 struct lttng_filter_bytecode
*filter
,
1458 struct lttng_event_exclusion
*exclusion
,
1462 * Internal version of cmd_enable_event() with a supplemental
1463 * "internal_event" flag which is used to enable internal events which should
1464 * be hidden from clients. Such events are used in the agent implementation to
1465 * enable the events through which all "agent" events are funeled.
1467 static int _cmd_enable_event(struct ltt_session
*session
,
1468 struct lttng_domain
*domain
,
1469 char *channel_name
, struct lttng_event
*event
,
1470 char *filter_expression
,
1471 struct lttng_filter_bytecode
*filter
,
1472 struct lttng_event_exclusion
*exclusion
,
1473 int wpipe
, bool internal_event
)
1475 int ret
, channel_created
= 0;
1476 struct lttng_channel
*attr
;
1480 assert(channel_name
);
1482 DBG("Enable event command for event \'%s\'", event
->name
);
1484 ret
= validate_event_name(event
->name
);
1491 switch (domain
->type
) {
1492 case LTTNG_DOMAIN_KERNEL
:
1494 struct ltt_kernel_channel
*kchan
;
1497 * If a non-default channel has been created in the
1498 * session, explicitely require that -c chan_name needs
1501 if (session
->kernel_session
->has_non_default_channel
1502 && channel_name
[0] == '\0') {
1503 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1507 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1508 session
->kernel_session
);
1509 if (kchan
== NULL
) {
1510 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1511 LTTNG_BUFFER_GLOBAL
);
1513 ret
= LTTNG_ERR_FATAL
;
1516 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1518 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1519 if (ret
!= LTTNG_OK
) {
1525 channel_created
= 1;
1528 /* Get the newly created kernel channel pointer */
1529 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1530 session
->kernel_session
);
1531 if (kchan
== NULL
) {
1532 /* This sould not happen... */
1533 ret
= LTTNG_ERR_FATAL
;
1537 switch (event
->type
) {
1538 case LTTNG_EVENT_ALL
:
1540 char *filter_expression_a
= NULL
;
1541 struct lttng_filter_bytecode
*filter_a
= NULL
;
1544 * We need to duplicate filter_expression and filter,
1545 * because ownership is passed to first enable
1548 if (filter_expression
) {
1549 filter_expression_a
= strdup(filter_expression
);
1550 if (!filter_expression_a
) {
1551 ret
= LTTNG_ERR_FATAL
;
1556 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1558 free(filter_expression_a
);
1559 ret
= LTTNG_ERR_FATAL
;
1562 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1564 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1565 ret
= event_kernel_enable_event(kchan
, event
,
1566 filter_expression
, filter
);
1567 /* We have passed ownership */
1568 filter_expression
= NULL
;
1570 if (ret
!= LTTNG_OK
) {
1571 if (channel_created
) {
1572 /* Let's not leak a useless channel. */
1573 kernel_destroy_channel(kchan
);
1575 free(filter_expression_a
);
1579 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1580 ret
= event_kernel_enable_event(kchan
, event
,
1581 filter_expression_a
, filter_a
);
1582 if (ret
!= LTTNG_OK
) {
1583 free(filter_expression_a
);
1589 case LTTNG_EVENT_PROBE
:
1590 case LTTNG_EVENT_FUNCTION
:
1591 case LTTNG_EVENT_FUNCTION_ENTRY
:
1592 case LTTNG_EVENT_TRACEPOINT
:
1593 ret
= event_kernel_enable_event(kchan
, event
,
1594 filter_expression
, filter
);
1595 /* We have passed ownership */
1596 filter_expression
= NULL
;
1598 if (ret
!= LTTNG_OK
) {
1599 if (channel_created
) {
1600 /* Let's not leak a useless channel. */
1601 kernel_destroy_channel(kchan
);
1606 case LTTNG_EVENT_SYSCALL
:
1607 ret
= event_kernel_enable_event(kchan
, event
,
1608 filter_expression
, filter
);
1609 /* We have passed ownership */
1610 filter_expression
= NULL
;
1612 if (ret
!= LTTNG_OK
) {
1617 ret
= LTTNG_ERR_UNK
;
1621 kernel_wait_quiescent(kernel_tracer_fd
);
1624 case LTTNG_DOMAIN_UST
:
1626 struct ltt_ust_channel
*uchan
;
1627 struct ltt_ust_session
*usess
= session
->ust_session
;
1632 * If a non-default channel has been created in the
1633 * session, explicitely require that -c chan_name needs
1636 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1637 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1641 /* Get channel from global UST domain */
1642 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1644 if (uchan
== NULL
) {
1645 /* Create default channel */
1646 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1647 usess
->buffer_type
);
1649 ret
= LTTNG_ERR_FATAL
;
1652 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1654 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1655 if (ret
!= LTTNG_OK
) {
1661 /* Get the newly created channel reference back */
1662 uchan
= trace_ust_find_channel_by_name(
1663 usess
->domain_global
.channels
, channel_name
);
1667 if (!internal_event
) {
1669 * Ensure the event name is not reserved for internal
1672 ret
= validate_ust_event_name(event
->name
);
1674 WARN("Userspace event name %s failed validation.",
1676 event
->name
: "NULL");
1677 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1682 /* At this point, the session and channel exist on the tracer */
1683 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1684 filter_expression
, filter
, exclusion
,
1686 /* We have passed ownership */
1687 filter_expression
= NULL
;
1690 if (ret
!= LTTNG_OK
) {
1695 case LTTNG_DOMAIN_LOG4J
:
1696 case LTTNG_DOMAIN_JUL
:
1697 case LTTNG_DOMAIN_PYTHON
:
1699 const char *default_event_name
, *default_chan_name
;
1701 struct lttng_event uevent
;
1702 struct lttng_domain tmp_dom
;
1703 struct ltt_ust_session
*usess
= session
->ust_session
;
1707 agt
= trace_ust_find_agent(usess
, domain
->type
);
1709 agt
= agent_create(domain
->type
);
1711 ret
= -LTTNG_ERR_NOMEM
;
1714 agent_add(agt
, usess
->agents
);
1717 /* Create the default tracepoint. */
1718 memset(&uevent
, 0, sizeof(uevent
));
1719 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1720 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1721 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1722 if (!default_event_name
) {
1723 ret
= -LTTNG_ERR_FATAL
;
1726 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1727 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1730 * The domain type is changed because we are about to enable the
1731 * default channel and event for the JUL domain that are hardcoded.
1732 * This happens in the UST domain.
1734 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1735 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1737 switch (domain
->type
) {
1738 case LTTNG_DOMAIN_LOG4J
:
1739 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1741 case LTTNG_DOMAIN_JUL
:
1742 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1744 case LTTNG_DOMAIN_PYTHON
:
1745 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
1748 /* The switch/case we are in should avoid this else big problem */
1753 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1756 filter_copy
= zmalloc(
1757 sizeof(struct lttng_filter_bytecode
)
1763 memcpy(filter_copy
, filter
,
1764 sizeof(struct lttng_filter_bytecode
)
1768 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
1769 (char *) default_chan_name
,
1770 &uevent
, filter_expression
, filter_copy
,
1772 /* We have passed ownership */
1773 filter_expression
= NULL
;
1776 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1780 /* The wild card * means that everything should be enabled. */
1781 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1782 ret
= event_agent_enable_all(usess
, agt
, event
, filter
);
1785 ret
= event_agent_enable(usess
, agt
, event
, filter
);
1788 if (ret
!= LTTNG_OK
) {
1795 ret
= LTTNG_ERR_UND
;
1802 free(filter_expression
);
1810 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1811 * We own filter, exclusion, and filter_expression.
1813 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1814 char *channel_name
, struct lttng_event
*event
,
1815 char *filter_expression
,
1816 struct lttng_filter_bytecode
*filter
,
1817 struct lttng_event_exclusion
*exclusion
,
1820 return _cmd_enable_event(session
, domain
, channel_name
, event
,
1821 filter_expression
, filter
, exclusion
, wpipe
, false);
1825 * Enable an event which is internal to LTTng. An internal should
1826 * never be made visible to clients and are immune to checks such as
1829 static int cmd_enable_event_internal(struct ltt_session
*session
,
1830 struct lttng_domain
*domain
,
1831 char *channel_name
, struct lttng_event
*event
,
1832 char *filter_expression
,
1833 struct lttng_filter_bytecode
*filter
,
1834 struct lttng_event_exclusion
*exclusion
,
1837 return _cmd_enable_event(session
, domain
, channel_name
, event
,
1838 filter_expression
, filter
, exclusion
, wpipe
, true);
1842 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1844 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1847 ssize_t nb_events
= 0;
1850 case LTTNG_DOMAIN_KERNEL
:
1851 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1852 if (nb_events
< 0) {
1853 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1857 case LTTNG_DOMAIN_UST
:
1858 nb_events
= ust_app_list_events(events
);
1859 if (nb_events
< 0) {
1860 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1864 case LTTNG_DOMAIN_LOG4J
:
1865 case LTTNG_DOMAIN_JUL
:
1866 case LTTNG_DOMAIN_PYTHON
:
1867 nb_events
= agent_list_events(events
, domain
);
1868 if (nb_events
< 0) {
1869 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1874 ret
= LTTNG_ERR_UND
;
1881 /* Return negative value to differentiate return code */
1886 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1888 ssize_t
cmd_list_tracepoint_fields(int domain
,
1889 struct lttng_event_field
**fields
)
1892 ssize_t nb_fields
= 0;
1895 case LTTNG_DOMAIN_UST
:
1896 nb_fields
= ust_app_list_event_fields(fields
);
1897 if (nb_fields
< 0) {
1898 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1902 case LTTNG_DOMAIN_KERNEL
:
1903 default: /* fall-through */
1904 ret
= LTTNG_ERR_UND
;
1911 /* Return negative value to differentiate return code */
1915 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1917 return syscall_table_list(events
);
1921 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
1923 * Called with session lock held.
1925 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
1926 int domain
, int32_t **pids
)
1929 ssize_t nr_pids
= 0;
1932 case LTTNG_DOMAIN_KERNEL
:
1934 struct ltt_kernel_session
*ksess
;
1936 ksess
= session
->kernel_session
;
1937 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
1939 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1944 case LTTNG_DOMAIN_UST
:
1946 struct ltt_ust_session
*usess
;
1948 usess
= session
->ust_session
;
1949 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
1951 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1956 case LTTNG_DOMAIN_LOG4J
:
1957 case LTTNG_DOMAIN_JUL
:
1958 case LTTNG_DOMAIN_PYTHON
:
1960 ret
= LTTNG_ERR_UND
;
1967 /* Return negative value to differentiate return code */
1972 * Command LTTNG_START_TRACE processed by the client thread.
1974 * Called with session mutex held.
1976 int cmd_start_trace(struct ltt_session
*session
)
1979 unsigned long nb_chan
= 0;
1980 struct ltt_kernel_session
*ksession
;
1981 struct ltt_ust_session
*usess
;
1985 /* Ease our life a bit ;) */
1986 ksession
= session
->kernel_session
;
1987 usess
= session
->ust_session
;
1989 /* Is the session already started? */
1990 if (session
->active
) {
1991 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1996 * Starting a session without channel is useless since after that it's not
1997 * possible to enable channel thus inform the client.
1999 if (usess
&& usess
->domain_global
.channels
) {
2000 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2003 nb_chan
+= ksession
->channel_count
;
2006 ret
= LTTNG_ERR_NO_CHANNEL
;
2010 /* Kernel tracing */
2011 if (ksession
!= NULL
) {
2012 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2013 if (ret
!= LTTNG_OK
) {
2018 /* Flag session that trace should start automatically */
2021 * Even though the start trace might fail, flag this session active so
2022 * other application coming in are started by default.
2026 ret
= ust_app_start_trace_all(usess
);
2028 ret
= LTTNG_ERR_UST_START_FAIL
;
2033 /* Flag this after a successful start. */
2034 session
->has_been_started
= 1;
2035 session
->active
= 1;
2044 * Command LTTNG_STOP_TRACE processed by the client thread.
2046 int cmd_stop_trace(struct ltt_session
*session
)
2049 struct ltt_kernel_channel
*kchan
;
2050 struct ltt_kernel_session
*ksession
;
2051 struct ltt_ust_session
*usess
;
2056 ksession
= session
->kernel_session
;
2057 usess
= session
->ust_session
;
2059 /* Session is not active. Skip everythong and inform the client. */
2060 if (!session
->active
) {
2061 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2066 if (ksession
&& ksession
->active
) {
2067 DBG("Stop kernel tracing");
2069 /* Flush metadata if exist */
2070 if (ksession
->metadata_stream_fd
>= 0) {
2071 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2073 ERR("Kernel metadata flush failed");
2077 /* Flush all buffers before stopping */
2078 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2079 ret
= kernel_flush_buffer(kchan
);
2081 ERR("Kernel flush buffer error");
2085 ret
= kernel_stop_session(ksession
);
2087 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2091 kernel_wait_quiescent(kernel_tracer_fd
);
2093 ksession
->active
= 0;
2096 if (usess
&& usess
->active
) {
2098 * Even though the stop trace might fail, flag this session inactive so
2099 * other application coming in are not started by default.
2103 ret
= ust_app_stop_trace_all(usess
);
2105 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2110 /* Flag inactive after a successful stop. */
2111 session
->active
= 0;
2119 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2121 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2122 struct lttng_uri
*uris
)
2125 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2126 struct ltt_ust_session
*usess
= session
->ust_session
;
2132 /* Can't set consumer URI if the session is active. */
2133 if (session
->active
) {
2134 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2138 /* Set the "global" consumer URIs */
2139 for (i
= 0; i
< nb_uri
; i
++) {
2140 ret
= add_uri_to_consumer(session
->consumer
,
2141 &uris
[i
], 0, session
->name
);
2142 if (ret
!= LTTNG_OK
) {
2147 /* Set UST session URIs */
2148 if (session
->ust_session
) {
2149 for (i
= 0; i
< nb_uri
; i
++) {
2150 ret
= add_uri_to_consumer(
2151 session
->ust_session
->consumer
,
2152 &uris
[i
], LTTNG_DOMAIN_UST
,
2154 if (ret
!= LTTNG_OK
) {
2160 /* Set kernel session URIs */
2161 if (session
->kernel_session
) {
2162 for (i
= 0; i
< nb_uri
; i
++) {
2163 ret
= add_uri_to_consumer(
2164 session
->kernel_session
->consumer
,
2165 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2167 if (ret
!= LTTNG_OK
) {
2174 * Make sure to set the session in output mode after we set URI since a
2175 * session can be created without URL (thus flagged in no output mode).
2177 session
->output_traces
= 1;
2179 ksess
->output_traces
= 1;
2183 usess
->output_traces
= 1;
2194 * Command LTTNG_CREATE_SESSION processed by the client thread.
2196 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2197 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2200 struct ltt_session
*session
;
2206 * Verify if the session already exist
2208 * XXX: There is no need for the session lock list here since the caller
2209 * (process_client_msg) is holding it. We might want to change that so a
2210 * single command does not lock the entire session list.
2212 session
= session_find_by_name(name
);
2213 if (session
!= NULL
) {
2214 ret
= LTTNG_ERR_EXIST_SESS
;
2218 /* Create tracing session in the registry */
2219 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2220 LTTNG_SOCK_GET_GID_CRED(creds
));
2221 if (ret
!= LTTNG_OK
) {
2226 * Get the newly created session pointer back
2228 * XXX: There is no need for the session lock list here since the caller
2229 * (process_client_msg) is holding it. We might want to change that so a
2230 * single command does not lock the entire session list.
2232 session
= session_find_by_name(name
);
2235 session
->live_timer
= live_timer
;
2236 /* Create default consumer output for the session not yet created. */
2237 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2238 if (session
->consumer
== NULL
) {
2239 ret
= LTTNG_ERR_FATAL
;
2240 goto consumer_error
;
2244 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2245 if (ret
!= LTTNG_OK
) {
2246 goto consumer_error
;
2248 session
->output_traces
= 1;
2250 session
->output_traces
= 0;
2251 DBG2("Session %s created with no output", session
->name
);
2254 session
->consumer
->enabled
= 1;
2259 session_destroy(session
);
2266 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2268 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2269 size_t nb_uri
, lttng_sock_cred
*creds
)
2272 struct ltt_session
*session
;
2273 struct snapshot_output
*new_output
= NULL
;
2279 * Create session in no output mode with URIs set to NULL. The uris we've
2280 * received are for a default snapshot output if one.
2282 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2283 if (ret
!= LTTNG_OK
) {
2287 /* Get the newly created session pointer back. This should NEVER fail. */
2288 session
= session_find_by_name(name
);
2291 /* Flag session for snapshot mode. */
2292 session
->snapshot_mode
= 1;
2294 /* Skip snapshot output creation if no URI is given. */
2299 new_output
= snapshot_output_alloc();
2301 ret
= LTTNG_ERR_NOMEM
;
2302 goto error_snapshot_alloc
;
2305 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2306 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2308 if (ret
== -ENOMEM
) {
2309 ret
= LTTNG_ERR_NOMEM
;
2311 ret
= LTTNG_ERR_INVALID
;
2313 goto error_snapshot
;
2317 snapshot_add_output(&session
->snapshot
, new_output
);
2324 snapshot_output_destroy(new_output
);
2325 error_snapshot_alloc
:
2326 session_destroy(session
);
2332 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2334 * Called with session lock held.
2336 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2339 struct ltt_ust_session
*usess
;
2340 struct ltt_kernel_session
*ksess
;
2345 usess
= session
->ust_session
;
2346 ksess
= session
->kernel_session
;
2348 /* Clean kernel session teardown */
2349 kernel_destroy_session(ksess
);
2351 /* UST session teardown */
2353 /* Close any relayd session */
2354 consumer_output_send_destroy_relayd(usess
->consumer
);
2356 /* Destroy every UST application related to this session. */
2357 ret
= ust_app_destroy_trace_all(usess
);
2359 ERR("Error in ust_app_destroy_trace_all");
2362 /* Clean up the rest. */
2363 trace_ust_destroy_session(usess
);
2367 * Must notify the kernel thread here to update it's poll set in order to
2368 * remove the channel(s)' fd just destroyed.
2370 ret
= notify_thread_pipe(wpipe
);
2372 PERROR("write kernel poll pipe");
2375 ret
= session_destroy(session
);
2381 * Command LTTNG_CALIBRATE processed by the client thread.
2383 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2388 case LTTNG_DOMAIN_KERNEL
:
2390 struct lttng_kernel_calibrate kcalibrate
;
2392 switch (calibrate
->type
) {
2393 case LTTNG_CALIBRATE_FUNCTION
:
2395 /* Default and only possible calibrate option. */
2396 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2400 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2402 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2407 case LTTNG_DOMAIN_UST
:
2409 struct lttng_ust_calibrate ucalibrate
;
2411 switch (calibrate
->type
) {
2412 case LTTNG_CALIBRATE_FUNCTION
:
2414 /* Default and only possible calibrate option. */
2415 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2419 ret
= ust_app_calibrate_glb(&ucalibrate
);
2421 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2427 ret
= LTTNG_ERR_UND
;
2438 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2440 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2441 const char *sock_path
, struct consumer_data
*cdata
)
2444 struct consumer_socket
*socket
= NULL
;
2451 case LTTNG_DOMAIN_KERNEL
:
2453 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2457 /* Can't register a consumer if there is already one */
2458 if (ksess
->consumer_fds_sent
!= 0) {
2459 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2463 sock
= lttcomm_connect_unix_sock(sock_path
);
2465 ret
= LTTNG_ERR_CONNECT_FAIL
;
2468 cdata
->cmd_sock
= sock
;
2470 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2471 if (socket
== NULL
) {
2474 PERROR("close register consumer");
2476 cdata
->cmd_sock
= -1;
2477 ret
= LTTNG_ERR_FATAL
;
2481 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2482 if (socket
->lock
== NULL
) {
2483 PERROR("zmalloc pthread mutex");
2484 ret
= LTTNG_ERR_FATAL
;
2487 pthread_mutex_init(socket
->lock
, NULL
);
2488 socket
->registered
= 1;
2491 consumer_add_socket(socket
, ksess
->consumer
);
2494 pthread_mutex_lock(&cdata
->pid_mutex
);
2496 pthread_mutex_unlock(&cdata
->pid_mutex
);
2501 /* TODO: Userspace tracing */
2502 ret
= LTTNG_ERR_UND
;
2510 consumer_destroy_socket(socket
);
2516 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2518 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2519 struct lttng_domain
**domains
)
2524 struct lttng_ht_iter iter
;
2526 if (session
->kernel_session
!= NULL
) {
2527 DBG3("Listing domains found kernel domain");
2531 if (session
->ust_session
!= NULL
) {
2532 DBG3("Listing domains found UST global domain");
2536 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2538 if (agt
->being_used
) {
2549 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2550 if (*domains
== NULL
) {
2551 ret
= LTTNG_ERR_FATAL
;
2555 if (session
->kernel_session
!= NULL
) {
2556 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2558 /* Kernel session buffer type is always GLOBAL */
2559 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2564 if (session
->ust_session
!= NULL
) {
2565 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2566 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2570 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2572 if (agt
->being_used
) {
2573 (*domains
)[index
].type
= agt
->domain
;
2574 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2584 /* Return negative value to differentiate return code */
2590 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2592 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2593 struct lttng_channel
**channels
)
2596 ssize_t nb_chan
= 0;
2599 case LTTNG_DOMAIN_KERNEL
:
2600 if (session
->kernel_session
!= NULL
) {
2601 nb_chan
= session
->kernel_session
->channel_count
;
2603 DBG3("Number of kernel channels %zd", nb_chan
);
2605 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2608 case LTTNG_DOMAIN_UST
:
2609 if (session
->ust_session
!= NULL
) {
2611 nb_chan
= lttng_ht_get_count(
2612 session
->ust_session
->domain_global
.channels
);
2615 DBG3("Number of UST global channels %zd", nb_chan
);
2617 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2622 ret
= LTTNG_ERR_UND
;
2627 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2628 if (*channels
== NULL
) {
2629 ret
= LTTNG_ERR_FATAL
;
2633 list_lttng_channels(domain
, session
, *channels
);
2639 /* Return negative value to differentiate return code */
2644 * Command LTTNG_LIST_EVENTS processed by the client thread.
2646 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2647 char *channel_name
, struct lttng_event
**events
)
2650 ssize_t nb_event
= 0;
2653 case LTTNG_DOMAIN_KERNEL
:
2654 if (session
->kernel_session
!= NULL
) {
2655 nb_event
= list_lttng_kernel_events(channel_name
,
2656 session
->kernel_session
, events
);
2659 case LTTNG_DOMAIN_UST
:
2661 if (session
->ust_session
!= NULL
) {
2662 nb_event
= list_lttng_ust_global_events(channel_name
,
2663 &session
->ust_session
->domain_global
, events
);
2667 case LTTNG_DOMAIN_LOG4J
:
2668 case LTTNG_DOMAIN_JUL
:
2669 case LTTNG_DOMAIN_PYTHON
:
2670 if (session
->ust_session
) {
2671 struct lttng_ht_iter iter
;
2675 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2676 &iter
.iter
, agt
, node
.node
) {
2677 nb_event
= list_lttng_agent_events(agt
, events
);
2683 ret
= LTTNG_ERR_UND
;
2690 /* Return negative value to differentiate return code */
2695 * Using the session list, filled a lttng_session array to send back to the
2696 * client for session listing.
2698 * The session list lock MUST be acquired before calling this function. Use
2699 * session_lock_list() and session_unlock_list().
2701 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2706 struct ltt_session
*session
;
2707 struct ltt_session_list
*list
= session_get_list();
2709 DBG("Getting all available session for UID %d GID %d",
2712 * Iterate over session list and append data after the control struct in
2715 cds_list_for_each_entry(session
, &list
->head
, list
) {
2717 * Only list the sessions the user can control.
2719 if (!session_access_ok(session
, uid
, gid
)) {
2723 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2724 struct ltt_ust_session
*usess
= session
->ust_session
;
2726 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2727 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2728 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2729 ret
= build_network_session_path(sessions
[i
].path
,
2730 sizeof(sessions
[i
].path
), session
);
2732 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2733 session
->consumer
->dst
.trace_path
);
2736 PERROR("snprintf session path");
2740 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2741 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2742 sessions
[i
].enabled
= session
->active
;
2743 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2744 sessions
[i
].live_timer_interval
= session
->live_timer
;
2750 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2751 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2753 int cmd_data_pending(struct ltt_session
*session
)
2756 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2757 struct ltt_ust_session
*usess
= session
->ust_session
;
2761 /* Session MUST be stopped to ask for data availability. */
2762 if (session
->active
) {
2763 ret
= LTTNG_ERR_SESSION_STARTED
;
2767 * If stopped, just make sure we've started before else the above call
2768 * will always send that there is data pending.
2770 * The consumer assumes that when the data pending command is received,
2771 * the trace has been started before or else no output data is written
2772 * by the streams which is a condition for data pending. So, this is
2773 * *VERY* important that we don't ask the consumer before a start
2776 if (!session
->has_been_started
) {
2782 if (ksess
&& ksess
->consumer
) {
2783 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2785 /* Data is still being extracted for the kernel. */
2790 if (usess
&& usess
->consumer
) {
2791 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2793 /* Data is still being extracted for the kernel. */
2798 /* Data is ready to be read by a viewer */
2806 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2808 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2810 int cmd_snapshot_add_output(struct ltt_session
*session
,
2811 struct lttng_snapshot_output
*output
, uint32_t *id
)
2814 struct snapshot_output
*new_output
;
2819 DBG("Cmd snapshot add output for session %s", session
->name
);
2822 * Permission denied to create an output if the session is not
2823 * set in no output mode.
2825 if (session
->output_traces
) {
2826 ret
= LTTNG_ERR_EPERM
;
2830 /* Only one output is allowed until we have the "tee" feature. */
2831 if (session
->snapshot
.nb_output
== 1) {
2832 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2836 new_output
= snapshot_output_alloc();
2838 ret
= LTTNG_ERR_NOMEM
;
2842 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2843 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2844 &session
->snapshot
);
2846 if (ret
== -ENOMEM
) {
2847 ret
= LTTNG_ERR_NOMEM
;
2849 ret
= LTTNG_ERR_INVALID
;
2855 snapshot_add_output(&session
->snapshot
, new_output
);
2857 *id
= new_output
->id
;
2864 snapshot_output_destroy(new_output
);
2870 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2872 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2874 int cmd_snapshot_del_output(struct ltt_session
*session
,
2875 struct lttng_snapshot_output
*output
)
2878 struct snapshot_output
*sout
= NULL
;
2886 * Permission denied to create an output if the session is not
2887 * set in no output mode.
2889 if (session
->output_traces
) {
2890 ret
= LTTNG_ERR_EPERM
;
2895 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2897 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2898 } else if (*output
->name
!= '\0') {
2899 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2901 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2904 ret
= LTTNG_ERR_INVALID
;
2908 snapshot_delete_output(&session
->snapshot
, sout
);
2909 snapshot_output_destroy(sout
);
2918 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2920 * If no output is available, outputs is untouched and 0 is returned.
2922 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2924 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2925 struct lttng_snapshot_output
**outputs
)
2928 struct lttng_snapshot_output
*list
= NULL
;
2929 struct lttng_ht_iter iter
;
2930 struct snapshot_output
*output
;
2935 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2938 * Permission denied to create an output if the session is not
2939 * set in no output mode.
2941 if (session
->output_traces
) {
2942 ret
= -LTTNG_ERR_EPERM
;
2946 if (session
->snapshot
.nb_output
== 0) {
2951 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2953 ret
= -LTTNG_ERR_NOMEM
;
2957 /* Copy list from session to the new list object. */
2959 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2960 output
, node
.node
) {
2961 assert(output
->consumer
);
2962 list
[idx
].id
= output
->id
;
2963 list
[idx
].max_size
= output
->max_size
;
2964 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2965 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2966 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2967 sizeof(list
[idx
].ctrl_url
));
2970 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2971 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2973 ret
= -LTTNG_ERR_NOMEM
;
2978 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2979 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2981 ret
= -LTTNG_ERR_NOMEM
;
2990 ret
= session
->snapshot
.nb_output
;
2998 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2999 * snapshot output is *not* set with a remote destination.
3001 * Return 0 on success or a LTTNG_ERR code.
3003 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3004 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3007 struct lttng_ht_iter iter
;
3008 struct consumer_socket
*socket
;
3011 assert(snap_output
);
3014 DBG2("Set relayd object from snapshot output");
3016 /* Ignore if snapshot consumer output is not network. */
3017 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3022 * For each consumer socket, create and send the relayd object of the
3026 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3027 socket
, node
.node
) {
3028 ret
= send_consumer_relayd_sockets(0, session
->id
,
3029 snap_output
->consumer
, socket
,
3030 session
->name
, session
->hostname
,
3031 session
->live_timer
);
3032 if (ret
!= LTTNG_OK
) {
3044 * Record a kernel snapshot.
3046 * Return LTTNG_OK on success or a LTTNG_ERR code.
3048 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3049 struct snapshot_output
*output
, struct ltt_session
*session
,
3050 int wait
, uint64_t nb_packets_per_stream
)
3058 /* Get the datetime for the snapshot output directory. */
3059 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3060 sizeof(output
->datetime
));
3062 ret
= LTTNG_ERR_INVALID
;
3067 * Copy kernel session sockets so we can communicate with the right
3068 * consumer for the snapshot record command.
3070 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3072 ret
= LTTNG_ERR_NOMEM
;
3076 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3077 if (ret
!= LTTNG_OK
) {
3078 goto error_snapshot
;
3081 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3082 if (ret
!= LTTNG_OK
) {
3083 goto error_snapshot
;
3090 /* Clean up copied sockets so this output can use some other later on. */
3091 consumer_destroy_output_sockets(output
->consumer
);
3098 * Record a UST snapshot.
3100 * Return 0 on success or a LTTNG_ERR error code.
3102 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3103 struct snapshot_output
*output
, struct ltt_session
*session
,
3104 int wait
, uint64_t nb_packets_per_stream
)
3112 /* Get the datetime for the snapshot output directory. */
3113 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3114 sizeof(output
->datetime
));
3116 ret
= LTTNG_ERR_INVALID
;
3121 * Copy UST session sockets so we can communicate with the right
3122 * consumer for the snapshot record command.
3124 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3126 ret
= LTTNG_ERR_NOMEM
;
3130 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3131 if (ret
!= LTTNG_OK
) {
3132 goto error_snapshot
;
3135 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3139 ret
= LTTNG_ERR_INVALID
;
3142 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3145 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3148 goto error_snapshot
;
3154 /* Clean up copied sockets so this output can use some other later on. */
3155 consumer_destroy_output_sockets(output
->consumer
);
3161 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3162 uint64_t cur_nr_packets
)
3164 uint64_t tot_size
= 0;
3166 if (session
->kernel_session
) {
3167 struct ltt_kernel_channel
*chan
;
3168 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3170 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3171 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3173 * Don't take channel into account if we
3174 * already grab all its packets.
3178 tot_size
+= chan
->channel
->attr
.subbuf_size
3179 * chan
->stream_count
;
3183 if (session
->ust_session
) {
3184 struct ltt_ust_session
*usess
= session
->ust_session
;
3186 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3194 * Calculate the number of packets we can grab from each stream that
3195 * fits within the overall snapshot max size.
3197 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3198 * the number of packets per stream.
3200 * TODO: this approach is not perfect: we consider the worse case
3201 * (packet filling the sub-buffers) as an upper bound, but we could do
3202 * better if we do this calculation while we actually grab the packet
3203 * content: we would know how much padding we don't actually store into
3206 * This algorithm is currently bounded by the number of packets per
3209 * Since we call this algorithm before actually grabbing the data, it's
3210 * an approximation: for instance, applications could appear/disappear
3211 * in between this call and actually grabbing data.
3214 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3217 uint64_t cur_nb_packets
= 0;
3220 return 0; /* Infinite */
3223 size_left
= max_size
;
3225 uint64_t one_more_packet_tot_size
;
3227 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3229 if (!one_more_packet_tot_size
) {
3230 /* We are already grabbing all packets. */
3233 size_left
-= one_more_packet_tot_size
;
3234 if (size_left
< 0) {
3239 if (!cur_nb_packets
) {
3240 /* Not enough room to grab one packet of each stream, error. */
3243 return cur_nb_packets
;
3247 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3249 * The wait parameter is ignored so this call always wait for the snapshot to
3250 * complete before returning.
3252 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3254 int cmd_snapshot_record(struct ltt_session
*session
,
3255 struct lttng_snapshot_output
*output
, int wait
)
3258 unsigned int use_tmp_output
= 0;
3259 struct snapshot_output tmp_output
;
3260 unsigned int snapshot_success
= 0;
3265 DBG("Cmd snapshot record for session %s", session
->name
);
3268 * Permission denied to create an output if the session is not
3269 * set in no output mode.
3271 if (session
->output_traces
) {
3272 ret
= LTTNG_ERR_EPERM
;
3276 /* The session needs to be started at least once. */
3277 if (!session
->has_been_started
) {
3278 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3282 /* Use temporary output for the session. */
3283 if (*output
->ctrl_url
!= '\0') {
3284 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3285 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3288 if (ret
== -ENOMEM
) {
3289 ret
= LTTNG_ERR_NOMEM
;
3291 ret
= LTTNG_ERR_INVALID
;
3295 /* Use the global session count for the temporary snapshot. */
3296 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3300 if (session
->kernel_session
) {
3301 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3303 if (use_tmp_output
) {
3304 int64_t nb_packets_per_stream
;
3306 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3307 tmp_output
.max_size
);
3308 if (nb_packets_per_stream
< 0) {
3309 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3312 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3313 wait
, nb_packets_per_stream
);
3314 if (ret
!= LTTNG_OK
) {
3317 snapshot_success
= 1;
3319 struct snapshot_output
*sout
;
3320 struct lttng_ht_iter iter
;
3323 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3324 &iter
.iter
, sout
, node
.node
) {
3325 int64_t nb_packets_per_stream
;
3328 * Make a local copy of the output and assign the possible
3329 * temporary value given by the caller.
3331 memset(&tmp_output
, 0, sizeof(tmp_output
));
3332 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3334 if (output
->max_size
!= (uint64_t) -1ULL) {
3335 tmp_output
.max_size
= output
->max_size
;
3338 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3339 tmp_output
.max_size
);
3340 if (nb_packets_per_stream
< 0) {
3341 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3345 /* Use temporary name. */
3346 if (*output
->name
!= '\0') {
3347 strncpy(tmp_output
.name
, output
->name
,
3348 sizeof(tmp_output
.name
));
3351 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3353 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3354 session
, wait
, nb_packets_per_stream
);
3355 if (ret
!= LTTNG_OK
) {
3359 snapshot_success
= 1;
3365 if (session
->ust_session
) {
3366 struct ltt_ust_session
*usess
= session
->ust_session
;
3368 if (use_tmp_output
) {
3369 int64_t nb_packets_per_stream
;
3371 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3372 tmp_output
.max_size
);
3373 if (nb_packets_per_stream
< 0) {
3374 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3377 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3378 wait
, nb_packets_per_stream
);
3379 if (ret
!= LTTNG_OK
) {
3382 snapshot_success
= 1;
3384 struct snapshot_output
*sout
;
3385 struct lttng_ht_iter iter
;
3388 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3389 &iter
.iter
, sout
, node
.node
) {
3390 int64_t nb_packets_per_stream
;
3393 * Make a local copy of the output and assign the possible
3394 * temporary value given by the caller.
3396 memset(&tmp_output
, 0, sizeof(tmp_output
));
3397 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3399 if (output
->max_size
!= (uint64_t) -1ULL) {
3400 tmp_output
.max_size
= output
->max_size
;
3403 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3404 tmp_output
.max_size
);
3405 if (nb_packets_per_stream
< 0) {
3406 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3411 /* Use temporary name. */
3412 if (*output
->name
!= '\0') {
3413 strncpy(tmp_output
.name
, output
->name
,
3414 sizeof(tmp_output
.name
));
3417 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3419 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3420 wait
, nb_packets_per_stream
);
3421 if (ret
!= LTTNG_OK
) {
3425 snapshot_success
= 1;
3431 if (snapshot_success
) {
3432 session
->snapshot
.nb_snapshot
++;
3434 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3442 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3444 int cmd_set_session_shm_path(struct ltt_session
*session
,
3445 const char *shm_path
)
3451 * Can only set shm path before session is started.
3453 if (session
->has_been_started
) {
3454 return LTTNG_ERR_SESSION_STARTED
;
3457 strncpy(session
->shm_path
, shm_path
,
3458 sizeof(session
->shm_path
));
3459 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3465 * Init command subsystem.
3470 * Set network sequence index to 1 for streams to match a relayd
3471 * socket on the consumer side.
3473 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3474 relayd_net_seq_idx
= 1;
3475 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3477 DBG("Command subsystem initialized");