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"
45 * Used to keep a unique index for each relayd socket created where this value
46 * is associated with streams on the consumer so it can match the right relayd
47 * to send to. It must be accessed with the relayd_net_seq_idx_lock
50 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
51 static uint64_t relayd_net_seq_idx
;
54 * Both functions below are special case for the Kernel domain when
55 * enabling/disabling all events.
58 int enable_kevent_all(struct ltt_session
*session
,
59 struct lttng_domain
*domain
, char *channel_name
,
60 struct lttng_event
*event
,
61 char *filter_expression
,
62 struct lttng_filter_bytecode
*filter
, int wpipe
);
64 int disable_kevent_all(struct ltt_session
*session
, int domain
,
66 struct lttng_event
*event
);
69 * Create a session path used by list_lttng_sessions for the case that the
70 * session consumer is on the network.
72 static int build_network_session_path(char *dst
, size_t size
,
73 struct ltt_session
*session
)
75 int ret
, kdata_port
, udata_port
;
76 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
77 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
82 memset(tmp_urls
, 0, sizeof(tmp_urls
));
83 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
85 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
87 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
88 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
89 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
92 if (session
->ust_session
&& session
->ust_session
->consumer
) {
93 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
94 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
97 if (uuri
== NULL
&& kuri
== NULL
) {
98 uri
= &session
->consumer
->dst
.net
.control
;
99 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
100 } else if (kuri
&& uuri
) {
101 ret
= uri_compare(kuri
, uuri
);
105 /* Build uuri URL string */
106 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
113 } else if (kuri
&& uuri
== NULL
) {
115 } else if (uuri
&& kuri
== NULL
) {
119 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
125 * Do we have a UST url set. If yes, this means we have both kernel and UST
128 if (*tmp_uurl
!= '\0') {
129 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
130 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
133 if (kuri
|| (!kuri
&& !uuri
)) {
136 /* No kernel URI, use the UST port. */
139 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
147 * Fill lttng_channel array of all channels.
149 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
150 struct lttng_channel
*channels
)
153 struct ltt_kernel_channel
*kchan
;
155 DBG("Listing channels for session %s", session
->name
);
158 case LTTNG_DOMAIN_KERNEL
:
159 /* Kernel channels */
160 if (session
->kernel_session
!= NULL
) {
161 cds_list_for_each_entry(kchan
,
162 &session
->kernel_session
->channel_list
.head
, list
) {
163 /* Copy lttng_channel struct to array */
164 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
165 channels
[i
].enabled
= kchan
->enabled
;
170 case LTTNG_DOMAIN_UST
:
172 struct lttng_ht_iter iter
;
173 struct ltt_ust_channel
*uchan
;
176 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
177 &iter
.iter
, uchan
, node
.node
) {
178 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
179 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
180 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
181 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
182 channels
[i
].attr
.switch_timer_interval
=
183 uchan
->attr
.switch_timer_interval
;
184 channels
[i
].attr
.read_timer_interval
=
185 uchan
->attr
.read_timer_interval
;
186 channels
[i
].enabled
= uchan
->enabled
;
187 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
188 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
189 switch (uchan
->attr
.output
) {
192 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
206 * Create a list of agent domain events.
208 * Return number of events in list on success or else a negative value.
210 static int list_lttng_agent_events(struct agent
*agt
,
211 struct lttng_event
**events
)
214 unsigned int nb_event
= 0;
215 struct agent_event
*event
;
216 struct lttng_event
*tmp_events
;
217 struct lttng_ht_iter iter
;
222 DBG3("Listing agent events");
225 nb_event
= lttng_ht_get_count(agt
->events
);
232 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
234 PERROR("zmalloc agent events session");
235 ret
= -LTTNG_ERR_FATAL
;
240 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
241 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
242 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
243 tmp_events
[i
].enabled
= event
->enabled
;
244 tmp_events
[i
].loglevel
= event
->loglevel
;
245 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
250 *events
= tmp_events
;
254 assert(nb_event
== i
);
259 * Create a list of ust global domain events.
261 static int list_lttng_ust_global_events(char *channel_name
,
262 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
265 unsigned int nb_event
= 0;
266 struct lttng_ht_iter iter
;
267 struct lttng_ht_node_str
*node
;
268 struct ltt_ust_channel
*uchan
;
269 struct ltt_ust_event
*uevent
;
270 struct lttng_event
*tmp
;
272 DBG("Listing UST global events for channel %s", channel_name
);
276 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
277 node
= lttng_ht_iter_get_node_str(&iter
);
279 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
283 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
285 nb_event
+= lttng_ht_get_count(uchan
->events
);
292 DBG3("Listing UST global %d events", nb_event
);
294 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
296 ret
= LTTNG_ERR_FATAL
;
300 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
301 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
302 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
303 tmp
[i
].enabled
= uevent
->enabled
;
305 switch (uevent
->attr
.instrumentation
) {
306 case LTTNG_UST_TRACEPOINT
:
307 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
309 case LTTNG_UST_PROBE
:
310 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
312 case LTTNG_UST_FUNCTION
:
313 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
317 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
318 switch (uevent
->attr
.loglevel_type
) {
319 case LTTNG_UST_LOGLEVEL_ALL
:
320 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
322 case LTTNG_UST_LOGLEVEL_RANGE
:
323 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
325 case LTTNG_UST_LOGLEVEL_SINGLE
:
326 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
329 if (uevent
->filter
) {
332 if (uevent
->exclusion
) {
333 tmp
[i
].exclusion
= 1;
347 * Fill lttng_event array of all kernel events in the channel.
349 static int list_lttng_kernel_events(char *channel_name
,
350 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
353 unsigned int nb_event
;
354 struct ltt_kernel_event
*event
;
355 struct ltt_kernel_channel
*kchan
;
357 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
359 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
363 nb_event
= kchan
->event_count
;
365 DBG("Listing events for channel %s", kchan
->channel
->name
);
372 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
373 if (*events
== NULL
) {
374 ret
= LTTNG_ERR_FATAL
;
378 /* Kernel channels */
379 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
380 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
381 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
382 (*events
)[i
].enabled
= event
->enabled
;
384 switch (event
->event
->instrumentation
) {
385 case LTTNG_KERNEL_TRACEPOINT
:
386 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
388 case LTTNG_KERNEL_KRETPROBE
:
389 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
390 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
391 sizeof(struct lttng_kernel_kprobe
));
393 case LTTNG_KERNEL_KPROBE
:
394 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
395 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
396 sizeof(struct lttng_kernel_kprobe
));
398 case LTTNG_KERNEL_FUNCTION
:
399 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
400 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
401 sizeof(struct lttng_kernel_function
));
403 case LTTNG_KERNEL_NOOP
:
404 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
406 case LTTNG_KERNEL_SYSCALL
:
407 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
409 case LTTNG_KERNEL_ALL
:
420 new_size
= syscall_list_channel(kchan
, events
, nb_event
);
432 /* Negate the error code to differentiate the size from an error */
437 * Add URI so the consumer output object. Set the correct path depending on the
438 * domain adding the default trace directory.
440 static int add_uri_to_consumer(struct consumer_output
*consumer
,
441 struct lttng_uri
*uri
, int domain
, const char *session_name
)
444 const char *default_trace_dir
;
448 if (consumer
== NULL
) {
449 DBG("No consumer detected. Don't add URI. Stopping.");
450 ret
= LTTNG_ERR_NO_CONSUMER
;
455 case LTTNG_DOMAIN_KERNEL
:
456 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
458 case LTTNG_DOMAIN_UST
:
459 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
463 * This case is possible is we try to add the URI to the global tracing
464 * session consumer object which in this case there is no subdir.
466 default_trace_dir
= "";
469 switch (uri
->dtype
) {
472 DBG2("Setting network URI to consumer");
474 if (consumer
->type
== CONSUMER_DST_NET
) {
475 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
476 consumer
->dst
.net
.control_isset
) ||
477 (uri
->stype
== LTTNG_STREAM_DATA
&&
478 consumer
->dst
.net
.data_isset
)) {
479 ret
= LTTNG_ERR_URL_EXIST
;
483 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
486 consumer
->type
= CONSUMER_DST_NET
;
488 /* Set URI into consumer output object */
489 ret
= consumer_set_network_uri(consumer
, uri
);
493 } else if (ret
== 1) {
495 * URI was the same in the consumer so we do not append the subdir
496 * again so to not duplicate output dir.
502 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
503 ret
= consumer_set_subdir(consumer
, session_name
);
505 ret
= LTTNG_ERR_FATAL
;
510 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
511 /* On a new subdir, reappend the default trace dir. */
512 strncat(consumer
->subdir
, default_trace_dir
,
513 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
514 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
519 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
520 memset(consumer
->dst
.trace_path
, 0,
521 sizeof(consumer
->dst
.trace_path
));
522 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
523 sizeof(consumer
->dst
.trace_path
));
524 /* Append default trace dir */
525 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
526 sizeof(consumer
->dst
.trace_path
) -
527 strlen(consumer
->dst
.trace_path
) - 1);
528 /* Flag consumer as local. */
529 consumer
->type
= CONSUMER_DST_LOCAL
;
540 * Init tracing by creating trace directory and sending fds kernel consumer.
542 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
545 struct lttng_ht_iter iter
;
546 struct consumer_socket
*socket
;
552 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
553 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
555 pthread_mutex_lock(socket
->lock
);
556 ret
= kernel_consumer_send_session(socket
, session
);
557 pthread_mutex_unlock(socket
->lock
);
559 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
571 * Create a socket to the relayd using the URI.
573 * On success, the relayd_sock pointer is set to the created socket.
574 * Else, it's stays untouched and a lttcomm error code is returned.
576 static int create_connect_relayd(struct lttng_uri
*uri
,
577 struct lttcomm_relayd_sock
**relayd_sock
)
580 struct lttcomm_relayd_sock
*rsock
;
582 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
583 RELAYD_VERSION_COMM_MINOR
);
585 ret
= LTTNG_ERR_FATAL
;
590 * Connect to relayd so we can proceed with a session creation. This call
591 * can possibly block for an arbitrary amount of time to set the health
592 * state to be in poll execution.
595 ret
= relayd_connect(rsock
);
598 ERR("Unable to reach lttng-relayd");
599 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
603 /* Create socket for control stream. */
604 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
605 DBG3("Creating relayd stream socket from URI");
607 /* Check relayd version */
608 ret
= relayd_version_check(rsock
);
610 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
613 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
614 DBG3("Creating relayd data socket from URI");
616 /* Command is not valid */
617 ERR("Relayd invalid stream type: %d", uri
->stype
);
618 ret
= LTTNG_ERR_INVALID
;
622 *relayd_sock
= rsock
;
627 /* The returned value is not useful since we are on an error path. */
628 (void) relayd_close(rsock
);
636 * Connect to the relayd using URI and send the socket to the right consumer.
638 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
639 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
640 struct consumer_socket
*consumer_sock
,
641 char *session_name
, char *hostname
, int session_live_timer
)
644 struct lttcomm_relayd_sock
*rsock
= NULL
;
646 /* Connect to relayd and make version check if uri is the control. */
647 ret
= create_connect_relayd(relayd_uri
, &rsock
);
648 if (ret
!= LTTNG_OK
) {
653 /* Set the network sequence index if not set. */
654 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
655 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
657 * Increment net_seq_idx because we are about to transfer the
658 * new relayd socket to the consumer.
659 * Assign unique key so the consumer can match streams.
661 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
662 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
665 /* Send relayd socket to consumer. */
666 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
667 relayd_uri
->stype
, session_id
,
668 session_name
, hostname
, session_live_timer
);
670 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
674 /* Flag that the corresponding socket was sent. */
675 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
676 consumer_sock
->control_sock_sent
= 1;
677 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
678 consumer_sock
->data_sock_sent
= 1;
684 * Close socket which was dup on the consumer side. The session daemon does
685 * NOT keep track of the relayd socket(s) once transfer to the consumer.
689 (void) relayd_close(rsock
);
693 if (ret
!= LTTNG_OK
) {
695 * The consumer output for this session should not be used anymore
696 * since the relayd connection failed thus making any tracing or/and
697 * streaming not usable.
699 consumer
->enabled
= 0;
705 * Send both relayd sockets to a specific consumer and domain. This is a
706 * helper function to facilitate sending the information to the consumer for a
709 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
710 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
711 char *session_name
, char *hostname
, int session_live_timer
)
718 /* Sending control relayd socket. */
719 if (!sock
->control_sock_sent
) {
720 ret
= send_consumer_relayd_socket(domain
, session_id
,
721 &consumer
->dst
.net
.control
, consumer
, sock
,
722 session_name
, hostname
, session_live_timer
);
723 if (ret
!= LTTNG_OK
) {
728 /* Sending data relayd socket. */
729 if (!sock
->data_sock_sent
) {
730 ret
= send_consumer_relayd_socket(domain
, session_id
,
731 &consumer
->dst
.net
.data
, consumer
, sock
,
732 session_name
, hostname
, session_live_timer
);
733 if (ret
!= LTTNG_OK
) {
743 * Setup relayd connections for a tracing session. First creates the socket to
744 * the relayd and send them to the right domain consumer. Consumer type MUST be
747 int cmd_setup_relayd(struct ltt_session
*session
)
750 struct ltt_ust_session
*usess
;
751 struct ltt_kernel_session
*ksess
;
752 struct consumer_socket
*socket
;
753 struct lttng_ht_iter iter
;
757 usess
= session
->ust_session
;
758 ksess
= session
->kernel_session
;
760 DBG("Setting relayd for session %s", session
->name
);
764 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
765 && usess
->consumer
->enabled
) {
766 /* For each consumer socket, send relayd sockets */
767 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
769 pthread_mutex_lock(socket
->lock
);
770 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
771 usess
->consumer
, socket
,
772 session
->name
, session
->hostname
,
773 session
->live_timer
);
774 pthread_mutex_unlock(socket
->lock
);
775 if (ret
!= LTTNG_OK
) {
778 /* Session is now ready for network streaming. */
779 session
->net_handle
= 1;
783 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
784 && ksess
->consumer
->enabled
) {
785 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
787 pthread_mutex_lock(socket
->lock
);
788 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
789 ksess
->consumer
, socket
,
790 session
->name
, session
->hostname
,
791 session
->live_timer
);
792 pthread_mutex_unlock(socket
->lock
);
793 if (ret
!= LTTNG_OK
) {
796 /* Session is now ready for network streaming. */
797 session
->net_handle
= 1;
807 * Start a kernel session by opening all necessary streams.
809 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
812 struct ltt_kernel_channel
*kchan
;
814 /* Open kernel metadata */
815 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
816 ret
= kernel_open_metadata(ksess
);
818 ret
= LTTNG_ERR_KERN_META_FAIL
;
823 /* Open kernel metadata stream */
824 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
825 ret
= kernel_open_metadata_stream(ksess
);
827 ERR("Kernel create metadata stream failed");
828 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
833 /* For each channel */
834 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
835 if (kchan
->stream_count
== 0) {
836 ret
= kernel_open_channel_stream(kchan
);
838 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
841 /* Update the stream global counter */
842 ksess
->stream_count_global
+= ret
;
846 /* Setup kernel consumer socket and send fds to it */
847 ret
= init_kernel_tracing(ksess
);
849 ret
= LTTNG_ERR_KERN_START_FAIL
;
853 /* This start the kernel tracing */
854 ret
= kernel_start_session(ksess
);
856 ret
= LTTNG_ERR_KERN_START_FAIL
;
860 /* Quiescent wait after starting trace */
861 kernel_wait_quiescent(kernel_tracer_fd
);
872 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
874 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
878 struct ltt_ust_session
*usess
;
880 usess
= session
->ust_session
;
885 case LTTNG_DOMAIN_KERNEL
:
887 ret
= channel_kernel_disable(session
->kernel_session
,
889 if (ret
!= LTTNG_OK
) {
893 kernel_wait_quiescent(kernel_tracer_fd
);
896 case LTTNG_DOMAIN_UST
:
898 struct ltt_ust_channel
*uchan
;
899 struct lttng_ht
*chan_ht
;
901 chan_ht
= usess
->domain_global
.channels
;
903 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
905 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
909 ret
= channel_ust_disable(usess
, uchan
);
910 if (ret
!= LTTNG_OK
) {
916 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
917 case LTTNG_DOMAIN_UST_EXEC_NAME
:
918 case LTTNG_DOMAIN_UST_PID
:
921 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
933 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
935 * The wpipe arguments is used as a notifier for the kernel thread.
937 int cmd_enable_channel(struct ltt_session
*session
,
938 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
941 struct ltt_ust_session
*usess
= session
->ust_session
;
942 struct lttng_ht
*chan_ht
;
949 len
= strnlen(attr
->name
, sizeof(attr
->name
));
951 /* Validate channel name */
952 if (attr
->name
[0] == '.' ||
953 memchr(attr
->name
, '/', len
) != NULL
) {
954 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
958 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
963 * Don't try to enable a channel if the session has been started at
964 * some point in time before. The tracer does not allow it.
966 if (session
->has_been_started
) {
967 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
972 * If the session is a live session, remove the switch timer, the
973 * live timer does the same thing but sends also synchronisation
974 * beacons for inactive streams.
976 if (session
->live_timer
> 0) {
977 attr
->attr
.live_timer_interval
= session
->live_timer
;
978 attr
->attr
.switch_timer_interval
= 0;
982 * The ringbuffer (both in user space and kernel) behave badly in overwrite
983 * mode and with less than 2 subbuf so block it right away and send back an
984 * invalid attribute error.
986 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
987 ret
= LTTNG_ERR_INVALID
;
991 switch (domain
->type
) {
992 case LTTNG_DOMAIN_KERNEL
:
994 struct ltt_kernel_channel
*kchan
;
996 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
997 session
->kernel_session
);
999 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1000 if (attr
->name
[0] != '\0') {
1001 session
->kernel_session
->has_non_default_channel
= 1;
1004 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1007 if (ret
!= LTTNG_OK
) {
1011 kernel_wait_quiescent(kernel_tracer_fd
);
1014 case LTTNG_DOMAIN_UST
:
1016 struct ltt_ust_channel
*uchan
;
1018 chan_ht
= usess
->domain_global
.channels
;
1020 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1021 if (uchan
== NULL
) {
1022 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1023 if (attr
->name
[0] != '\0') {
1024 usess
->has_non_default_channel
= 1;
1027 ret
= channel_ust_enable(usess
, uchan
);
1032 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1044 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1046 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1048 struct lttng_event
*event
)
1053 DBG("Disable event command for event \'%s\'", event
->name
);
1055 event_name
= event
->name
;
1057 /* Error out on unhandled search criteria */
1058 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1059 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1060 return LTTNG_ERR_UNK
;
1062 /* Special handling for kernel domain all events. */
1063 if (domain
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event_name
, "*")) {
1064 return disable_kevent_all(session
, domain
, channel_name
, event
);
1070 case LTTNG_DOMAIN_KERNEL
:
1072 struct ltt_kernel_channel
*kchan
;
1073 struct ltt_kernel_session
*ksess
;
1075 ksess
= session
->kernel_session
;
1078 * If a non-default channel has been created in the
1079 * session, explicitely require that -c chan_name needs
1082 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1083 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1087 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1088 if (kchan
== NULL
) {
1089 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1093 switch (event
->type
) {
1094 case LTTNG_EVENT_ALL
:
1095 case LTTNG_EVENT_TRACEPOINT
:
1096 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1097 if (ret
!= LTTNG_OK
) {
1101 case LTTNG_EVENT_SYSCALL
:
1102 ret
= event_kernel_disable_syscall(kchan
, event_name
);
1103 if (ret
!= LTTNG_OK
) {
1108 ret
= LTTNG_ERR_UNK
;
1112 kernel_wait_quiescent(kernel_tracer_fd
);
1115 case LTTNG_DOMAIN_UST
:
1117 struct ltt_ust_channel
*uchan
;
1118 struct ltt_ust_session
*usess
;
1120 usess
= session
->ust_session
;
1123 * If a non-default channel has been created in the
1124 * session, explicitely require that -c chan_name needs
1127 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1128 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1132 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1134 if (uchan
== NULL
) {
1135 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1139 switch (event
->type
) {
1140 case LTTNG_EVENT_ALL
:
1141 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1142 if (ret
!= LTTNG_OK
) {
1147 ret
= LTTNG_ERR_UNK
;
1151 DBG3("Disable UST event %s in channel %s completed", event_name
,
1155 case LTTNG_DOMAIN_LOG4J
:
1156 case LTTNG_DOMAIN_JUL
:
1157 case LTTNG_DOMAIN_PYTHON
:
1160 struct ltt_ust_session
*usess
= session
->ust_session
;
1164 switch (event
->type
) {
1165 case LTTNG_EVENT_ALL
:
1168 ret
= LTTNG_ERR_UNK
;
1172 agt
= trace_ust_find_agent(usess
, domain
);
1174 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1177 /* The wild card * means that everything should be disabled. */
1178 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1179 ret
= event_agent_disable_all(usess
, agt
);
1181 ret
= event_agent_disable(usess
, agt
, event_name
);
1183 if (ret
!= LTTNG_OK
) {
1190 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1191 case LTTNG_DOMAIN_UST_PID
:
1192 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1195 ret
= LTTNG_ERR_UND
;
1207 * Command LTTNG_DISABLE_EVENT for event "*" processed by the client thread.
1210 int disable_kevent_all(struct ltt_session
*session
, int domain
,
1212 struct lttng_event
*event
)
1219 case LTTNG_DOMAIN_KERNEL
:
1221 struct ltt_kernel_session
*ksess
;
1222 struct ltt_kernel_channel
*kchan
;
1224 ksess
= session
->kernel_session
;
1227 * If a non-default channel has been created in the
1228 * session, explicitely require that -c chan_name needs
1231 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1232 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1236 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1237 if (kchan
== NULL
) {
1238 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1242 switch (event
->type
) {
1243 case LTTNG_EVENT_ALL
:
1244 ret
= event_kernel_disable_all(kchan
);
1245 if (ret
!= LTTNG_OK
) {
1249 case LTTNG_EVENT_SYSCALL
:
1250 ret
= event_kernel_disable_syscall(kchan
, "");
1251 if (ret
!= LTTNG_OK
) {
1256 ret
= LTTNG_ERR_UNK
;
1260 kernel_wait_quiescent(kernel_tracer_fd
);
1264 ret
= LTTNG_ERR_UND
;
1276 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1278 int cmd_add_context(struct ltt_session
*session
, int domain
,
1279 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1281 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1284 case LTTNG_DOMAIN_KERNEL
:
1285 assert(session
->kernel_session
);
1287 if (session
->kernel_session
->channel_count
== 0) {
1288 /* Create default channel */
1289 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1290 if (ret
!= LTTNG_OK
) {
1293 chan_kern_created
= 1;
1295 /* Add kernel context to kernel tracer */
1296 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1297 if (ret
!= LTTNG_OK
) {
1301 case LTTNG_DOMAIN_UST
:
1303 struct ltt_ust_session
*usess
= session
->ust_session
;
1304 unsigned int chan_count
;
1308 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1309 if (chan_count
== 0) {
1310 struct lttng_channel
*attr
;
1311 /* Create default channel */
1312 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1314 ret
= LTTNG_ERR_FATAL
;
1318 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1319 if (ret
!= LTTNG_OK
) {
1324 chan_ust_created
= 1;
1327 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1328 if (ret
!= LTTNG_OK
) {
1334 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1335 case LTTNG_DOMAIN_UST_PID
:
1336 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1339 ret
= LTTNG_ERR_UND
;
1346 if (chan_kern_created
) {
1347 struct ltt_kernel_channel
*kchan
=
1348 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1349 session
->kernel_session
);
1350 /* Created previously, this should NOT fail. */
1352 kernel_destroy_channel(kchan
);
1355 if (chan_ust_created
) {
1356 struct ltt_ust_channel
*uchan
=
1357 trace_ust_find_channel_by_name(
1358 session
->ust_session
->domain_global
.channels
,
1359 DEFAULT_CHANNEL_NAME
);
1360 /* Created previously, this should NOT fail. */
1362 /* Remove from the channel list of the session. */
1363 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1365 trace_ust_destroy_channel(uchan
);
1370 static int validate_event_name(const char *name
)
1373 const char *c
= name
;
1374 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1377 * Make sure that unescaped wildcards are only used as the last
1378 * character of the event name.
1380 while (c
< event_name_end
) {
1388 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1389 /* Wildcard is not the last character */
1390 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1403 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1404 * We own filter, exclusion, and filter_expression.
1406 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1407 char *channel_name
, struct lttng_event
*event
,
1408 char *filter_expression
,
1409 struct lttng_filter_bytecode
*filter
,
1410 struct lttng_event_exclusion
*exclusion
,
1413 int ret
, channel_created
= 0;
1414 struct lttng_channel
*attr
;
1418 assert(channel_name
);
1420 DBG("Enable event command for event \'%s\'", event
->name
);
1422 /* Special handling for kernel domain all events. */
1423 if (domain
->type
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event
->name
, "*")) {
1424 return enable_kevent_all(session
, domain
, channel_name
, event
,
1425 filter_expression
, filter
, wpipe
);
1428 ret
= validate_event_name(event
->name
);
1435 switch (domain
->type
) {
1436 case LTTNG_DOMAIN_KERNEL
:
1438 struct ltt_kernel_channel
*kchan
;
1441 * If a non-default channel has been created in the
1442 * session, explicitely require that -c chan_name needs
1445 if (session
->kernel_session
->has_non_default_channel
1446 && channel_name
[0] == '\0') {
1447 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1451 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1452 session
->kernel_session
);
1453 if (kchan
== NULL
) {
1454 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1455 LTTNG_BUFFER_GLOBAL
);
1457 ret
= LTTNG_ERR_FATAL
;
1460 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1462 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1463 if (ret
!= LTTNG_OK
) {
1469 channel_created
= 1;
1472 /* Get the newly created kernel channel pointer */
1473 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1474 session
->kernel_session
);
1475 if (kchan
== NULL
) {
1476 /* This sould not happen... */
1477 ret
= LTTNG_ERR_FATAL
;
1481 switch (event
->type
) {
1482 case LTTNG_EVENT_ALL
:
1483 case LTTNG_EVENT_PROBE
:
1484 case LTTNG_EVENT_FUNCTION
:
1485 case LTTNG_EVENT_FUNCTION_ENTRY
:
1486 case LTTNG_EVENT_TRACEPOINT
:
1487 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1488 if (ret
!= LTTNG_OK
) {
1489 if (channel_created
) {
1490 /* Let's not leak a useless channel. */
1491 kernel_destroy_channel(kchan
);
1496 case LTTNG_EVENT_SYSCALL
:
1497 ret
= event_kernel_enable_syscall(kchan
, event
->name
);
1498 if (ret
!= LTTNG_OK
) {
1503 ret
= LTTNG_ERR_UNK
;
1507 kernel_wait_quiescent(kernel_tracer_fd
);
1510 case LTTNG_DOMAIN_UST
:
1512 struct ltt_ust_channel
*uchan
;
1513 struct ltt_ust_session
*usess
= session
->ust_session
;
1518 * If a non-default channel has been created in the
1519 * session, explicitely require that -c chan_name needs
1522 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1523 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1527 /* Get channel from global UST domain */
1528 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1530 if (uchan
== NULL
) {
1531 /* Create default channel */
1532 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1533 usess
->buffer_type
);
1535 ret
= LTTNG_ERR_FATAL
;
1538 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1540 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1541 if (ret
!= LTTNG_OK
) {
1547 /* Get the newly created channel reference back */
1548 uchan
= trace_ust_find_channel_by_name(
1549 usess
->domain_global
.channels
, channel_name
);
1553 /* At this point, the session and channel exist on the tracer */
1554 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1555 filter_expression
, filter
, exclusion
);
1556 /* We have passed ownership */
1557 filter_expression
= NULL
;
1560 if (ret
!= LTTNG_OK
) {
1565 case LTTNG_DOMAIN_LOG4J
:
1566 case LTTNG_DOMAIN_JUL
:
1567 case LTTNG_DOMAIN_PYTHON
:
1569 const char *default_event_name
, *default_chan_name
;
1571 struct lttng_event uevent
;
1572 struct lttng_domain tmp_dom
;
1573 struct ltt_ust_session
*usess
= session
->ust_session
;
1577 agt
= trace_ust_find_agent(usess
, domain
->type
);
1579 agt
= agent_create(domain
->type
);
1581 ret
= -LTTNG_ERR_NOMEM
;
1584 agent_add(agt
, usess
->agents
);
1587 /* Create the default tracepoint. */
1588 memset(&uevent
, 0, sizeof(uevent
));
1589 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1590 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1591 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1592 if (!default_event_name
) {
1593 ret
= -LTTNG_ERR_FATAL
;
1596 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1597 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1600 * The domain type is changed because we are about to enable the
1601 * default channel and event for the JUL domain that are hardcoded.
1602 * This happens in the UST domain.
1604 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1605 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1607 switch (domain
->type
) {
1608 case LTTNG_DOMAIN_LOG4J
:
1609 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1611 case LTTNG_DOMAIN_JUL
:
1612 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1614 case LTTNG_DOMAIN_PYTHON
:
1615 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
1618 /* The switch/case we are in should avoid this else big problem */
1623 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1626 filter_copy
= zmalloc(
1627 sizeof(struct lttng_filter_bytecode
)
1633 memcpy(filter_copy
, filter
,
1634 sizeof(struct lttng_filter_bytecode
)
1638 ret
= cmd_enable_event(session
, &tmp_dom
,
1639 (char *) default_chan_name
,
1640 &uevent
, filter_expression
, filter_copy
,
1642 /* We have passed ownership */
1643 filter_expression
= NULL
;
1646 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1650 /* The wild card * means that everything should be enabled. */
1651 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1652 ret
= event_agent_enable_all(usess
, agt
, event
, filter
);
1655 ret
= event_agent_enable(usess
, agt
, event
, filter
);
1658 if (ret
!= LTTNG_OK
) {
1665 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1666 case LTTNG_DOMAIN_UST_PID
:
1667 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1670 ret
= LTTNG_ERR_UND
;
1677 free(filter_expression
);
1685 * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread.
1688 int enable_kevent_all(struct ltt_session
*session
,
1689 struct lttng_domain
*domain
, char *channel_name
,
1690 struct lttng_event
*event
,
1691 char *filter_expression
,
1692 struct lttng_filter_bytecode
*filter
, int wpipe
)
1695 struct lttng_channel
*attr
;
1698 assert(channel_name
);
1702 switch (domain
->type
) {
1703 case LTTNG_DOMAIN_KERNEL
:
1705 struct ltt_kernel_channel
*kchan
;
1707 assert(session
->kernel_session
);
1710 * If a non-default channel has been created in the
1711 * session, explicitely require that -c chan_name needs
1714 if (session
->kernel_session
->has_non_default_channel
1715 && channel_name
[0] == '\0') {
1716 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1720 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1721 session
->kernel_session
);
1722 if (kchan
== NULL
) {
1723 /* Create default channel */
1724 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1725 LTTNG_BUFFER_GLOBAL
);
1727 ret
= LTTNG_ERR_FATAL
;
1730 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1732 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1733 if (ret
!= LTTNG_OK
) {
1739 /* Get the newly created kernel channel pointer */
1740 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1741 session
->kernel_session
);
1745 switch (event
->type
) {
1746 case LTTNG_EVENT_SYSCALL
:
1747 ret
= event_kernel_enable_syscall(kchan
, "");
1748 if (ret
!= LTTNG_OK
) {
1752 case LTTNG_EVENT_TRACEPOINT
:
1754 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1755 * events already registered to the channel.
1757 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1759 case LTTNG_EVENT_ALL
:
1760 /* Enable syscalls and tracepoints */
1761 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1764 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1768 /* Manage return value */
1769 if (ret
!= LTTNG_OK
) {
1771 * On error, cmd_enable_channel call will take care of destroying
1772 * the created channel if it was needed.
1777 kernel_wait_quiescent(kernel_tracer_fd
);
1781 ret
= LTTNG_ERR_UND
;
1794 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1796 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1799 ssize_t nb_events
= 0;
1802 case LTTNG_DOMAIN_KERNEL
:
1803 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1804 if (nb_events
< 0) {
1805 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1809 case LTTNG_DOMAIN_UST
:
1810 nb_events
= ust_app_list_events(events
);
1811 if (nb_events
< 0) {
1812 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1816 case LTTNG_DOMAIN_LOG4J
:
1817 case LTTNG_DOMAIN_JUL
:
1818 case LTTNG_DOMAIN_PYTHON
:
1819 nb_events
= agent_list_events(events
, domain
);
1820 if (nb_events
< 0) {
1821 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1826 ret
= LTTNG_ERR_UND
;
1833 /* Return negative value to differentiate return code */
1838 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1840 ssize_t
cmd_list_tracepoint_fields(int domain
,
1841 struct lttng_event_field
**fields
)
1844 ssize_t nb_fields
= 0;
1847 case LTTNG_DOMAIN_UST
:
1848 nb_fields
= ust_app_list_event_fields(fields
);
1849 if (nb_fields
< 0) {
1850 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1854 case LTTNG_DOMAIN_KERNEL
:
1855 default: /* fall-through */
1856 ret
= LTTNG_ERR_UND
;
1863 /* Return negative value to differentiate return code */
1867 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1869 return syscall_table_list(events
);
1873 * Command LTTNG_START_TRACE processed by the client thread.
1875 int cmd_start_trace(struct ltt_session
*session
)
1878 unsigned long nb_chan
= 0;
1879 struct ltt_kernel_session
*ksession
;
1880 struct ltt_ust_session
*usess
;
1884 /* Ease our life a bit ;) */
1885 ksession
= session
->kernel_session
;
1886 usess
= session
->ust_session
;
1888 /* Is the session already started? */
1889 if (session
->active
) {
1890 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1895 * Starting a session without channel is useless since after that it's not
1896 * possible to enable channel thus inform the client.
1898 if (usess
&& usess
->domain_global
.channels
) {
1900 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1904 nb_chan
+= ksession
->channel_count
;
1907 ret
= LTTNG_ERR_NO_CHANNEL
;
1911 /* Kernel tracing */
1912 if (ksession
!= NULL
) {
1913 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1914 if (ret
!= LTTNG_OK
) {
1919 /* Flag session that trace should start automatically */
1922 * Even though the start trace might fail, flag this session active so
1923 * other application coming in are started by default.
1927 ret
= ust_app_start_trace_all(usess
);
1929 ret
= LTTNG_ERR_UST_START_FAIL
;
1934 /* Flag this after a successful start. */
1935 session
->has_been_started
= 1;
1936 session
->active
= 1;
1945 * Command LTTNG_STOP_TRACE processed by the client thread.
1947 int cmd_stop_trace(struct ltt_session
*session
)
1950 struct ltt_kernel_channel
*kchan
;
1951 struct ltt_kernel_session
*ksession
;
1952 struct ltt_ust_session
*usess
;
1957 ksession
= session
->kernel_session
;
1958 usess
= session
->ust_session
;
1960 /* Session is not active. Skip everythong and inform the client. */
1961 if (!session
->active
) {
1962 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1967 if (ksession
&& ksession
->active
) {
1968 DBG("Stop kernel tracing");
1970 /* Flush metadata if exist */
1971 if (ksession
->metadata_stream_fd
>= 0) {
1972 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1974 ERR("Kernel metadata flush failed");
1978 /* Flush all buffers before stopping */
1979 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1980 ret
= kernel_flush_buffer(kchan
);
1982 ERR("Kernel flush buffer error");
1986 ret
= kernel_stop_session(ksession
);
1988 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1992 kernel_wait_quiescent(kernel_tracer_fd
);
1994 ksession
->active
= 0;
1997 if (usess
&& usess
->active
) {
1999 * Even though the stop trace might fail, flag this session inactive so
2000 * other application coming in are not started by default.
2004 ret
= ust_app_stop_trace_all(usess
);
2006 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2011 /* Flag inactive after a successful stop. */
2012 session
->active
= 0;
2020 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2022 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2023 struct lttng_uri
*uris
)
2026 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2027 struct ltt_ust_session
*usess
= session
->ust_session
;
2033 /* Can't set consumer URI if the session is active. */
2034 if (session
->active
) {
2035 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2039 /* Set the "global" consumer URIs */
2040 for (i
= 0; i
< nb_uri
; i
++) {
2041 ret
= add_uri_to_consumer(session
->consumer
,
2042 &uris
[i
], 0, session
->name
);
2043 if (ret
!= LTTNG_OK
) {
2048 /* Set UST session URIs */
2049 if (session
->ust_session
) {
2050 for (i
= 0; i
< nb_uri
; i
++) {
2051 ret
= add_uri_to_consumer(
2052 session
->ust_session
->consumer
,
2053 &uris
[i
], LTTNG_DOMAIN_UST
,
2055 if (ret
!= LTTNG_OK
) {
2061 /* Set kernel session URIs */
2062 if (session
->kernel_session
) {
2063 for (i
= 0; i
< nb_uri
; i
++) {
2064 ret
= add_uri_to_consumer(
2065 session
->kernel_session
->consumer
,
2066 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2068 if (ret
!= LTTNG_OK
) {
2075 * Make sure to set the session in output mode after we set URI since a
2076 * session can be created without URL (thus flagged in no output mode).
2078 session
->output_traces
= 1;
2080 ksess
->output_traces
= 1;
2084 usess
->output_traces
= 1;
2095 * Command LTTNG_CREATE_SESSION processed by the client thread.
2097 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2098 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2101 struct ltt_session
*session
;
2107 * Verify if the session already exist
2109 * XXX: There is no need for the session lock list here since the caller
2110 * (process_client_msg) is holding it. We might want to change that so a
2111 * single command does not lock the entire session list.
2113 session
= session_find_by_name(name
);
2114 if (session
!= NULL
) {
2115 ret
= LTTNG_ERR_EXIST_SESS
;
2119 /* Create tracing session in the registry */
2120 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2121 LTTNG_SOCK_GET_GID_CRED(creds
));
2122 if (ret
!= LTTNG_OK
) {
2127 * Get the newly created session pointer back
2129 * XXX: There is no need for the session lock list here since the caller
2130 * (process_client_msg) is holding it. We might want to change that so a
2131 * single command does not lock the entire session list.
2133 session
= session_find_by_name(name
);
2136 session
->live_timer
= live_timer
;
2137 /* Create default consumer output for the session not yet created. */
2138 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2139 if (session
->consumer
== NULL
) {
2140 ret
= LTTNG_ERR_FATAL
;
2141 goto consumer_error
;
2145 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2146 if (ret
!= LTTNG_OK
) {
2147 goto consumer_error
;
2149 session
->output_traces
= 1;
2151 session
->output_traces
= 0;
2152 DBG2("Session %s created with no output", session
->name
);
2155 session
->consumer
->enabled
= 1;
2160 session_destroy(session
);
2167 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2169 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2170 size_t nb_uri
, lttng_sock_cred
*creds
)
2173 struct ltt_session
*session
;
2174 struct snapshot_output
*new_output
= NULL
;
2180 * Create session in no output mode with URIs set to NULL. The uris we've
2181 * received are for a default snapshot output if one.
2183 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2184 if (ret
!= LTTNG_OK
) {
2188 /* Get the newly created session pointer back. This should NEVER fail. */
2189 session
= session_find_by_name(name
);
2192 /* Flag session for snapshot mode. */
2193 session
->snapshot_mode
= 1;
2195 /* Skip snapshot output creation if no URI is given. */
2200 new_output
= snapshot_output_alloc();
2202 ret
= LTTNG_ERR_NOMEM
;
2203 goto error_snapshot_alloc
;
2206 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2207 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2209 if (ret
== -ENOMEM
) {
2210 ret
= LTTNG_ERR_NOMEM
;
2212 ret
= LTTNG_ERR_INVALID
;
2214 goto error_snapshot
;
2218 snapshot_add_output(&session
->snapshot
, new_output
);
2225 snapshot_output_destroy(new_output
);
2226 error_snapshot_alloc
:
2227 session_destroy(session
);
2233 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2235 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2238 struct ltt_ust_session
*usess
;
2239 struct ltt_kernel_session
*ksess
;
2244 usess
= session
->ust_session
;
2245 ksess
= session
->kernel_session
;
2247 /* Clean kernel session teardown */
2248 kernel_destroy_session(ksess
);
2250 /* UST session teardown */
2252 /* Close any relayd session */
2253 consumer_output_send_destroy_relayd(usess
->consumer
);
2255 /* Destroy every UST application related to this session. */
2256 ret
= ust_app_destroy_trace_all(usess
);
2258 ERR("Error in ust_app_destroy_trace_all");
2261 /* Clean up the rest. */
2262 trace_ust_destroy_session(usess
);
2266 * Must notify the kernel thread here to update it's poll set in order to
2267 * remove the channel(s)' fd just destroyed.
2269 ret
= notify_thread_pipe(wpipe
);
2271 PERROR("write kernel poll pipe");
2274 ret
= session_destroy(session
);
2280 * Command LTTNG_CALIBRATE processed by the client thread.
2282 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2287 case LTTNG_DOMAIN_KERNEL
:
2289 struct lttng_kernel_calibrate kcalibrate
;
2291 switch (calibrate
->type
) {
2292 case LTTNG_CALIBRATE_FUNCTION
:
2294 /* Default and only possible calibrate option. */
2295 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2299 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2301 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2306 case LTTNG_DOMAIN_UST
:
2308 struct lttng_ust_calibrate ucalibrate
;
2310 switch (calibrate
->type
) {
2311 case LTTNG_CALIBRATE_FUNCTION
:
2313 /* Default and only possible calibrate option. */
2314 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2318 ret
= ust_app_calibrate_glb(&ucalibrate
);
2320 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2326 ret
= LTTNG_ERR_UND
;
2337 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2339 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2340 const char *sock_path
, struct consumer_data
*cdata
)
2343 struct consumer_socket
*socket
= NULL
;
2350 case LTTNG_DOMAIN_KERNEL
:
2352 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2356 /* Can't register a consumer if there is already one */
2357 if (ksess
->consumer_fds_sent
!= 0) {
2358 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2362 sock
= lttcomm_connect_unix_sock(sock_path
);
2364 ret
= LTTNG_ERR_CONNECT_FAIL
;
2367 cdata
->cmd_sock
= sock
;
2369 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2370 if (socket
== NULL
) {
2373 PERROR("close register consumer");
2375 cdata
->cmd_sock
= -1;
2376 ret
= LTTNG_ERR_FATAL
;
2380 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2381 if (socket
->lock
== NULL
) {
2382 PERROR("zmalloc pthread mutex");
2383 ret
= LTTNG_ERR_FATAL
;
2386 pthread_mutex_init(socket
->lock
, NULL
);
2387 socket
->registered
= 1;
2390 consumer_add_socket(socket
, ksess
->consumer
);
2393 pthread_mutex_lock(&cdata
->pid_mutex
);
2395 pthread_mutex_unlock(&cdata
->pid_mutex
);
2400 /* TODO: Userspace tracing */
2401 ret
= LTTNG_ERR_UND
;
2409 consumer_destroy_socket(socket
);
2415 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2417 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2418 struct lttng_domain
**domains
)
2423 struct lttng_ht_iter iter
;
2425 if (session
->kernel_session
!= NULL
) {
2426 DBG3("Listing domains found kernel domain");
2430 if (session
->ust_session
!= NULL
) {
2431 DBG3("Listing domains found UST global domain");
2435 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2437 if (agt
->being_used
) {
2448 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2449 if (*domains
== NULL
) {
2450 ret
= LTTNG_ERR_FATAL
;
2454 if (session
->kernel_session
!= NULL
) {
2455 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2459 if (session
->ust_session
!= NULL
) {
2460 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2461 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2465 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2467 if (agt
->being_used
) {
2468 (*domains
)[index
].type
= agt
->domain
;
2469 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2479 /* Return negative value to differentiate return code */
2485 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2487 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2488 struct lttng_channel
**channels
)
2491 ssize_t nb_chan
= 0;
2494 case LTTNG_DOMAIN_KERNEL
:
2495 if (session
->kernel_session
!= NULL
) {
2496 nb_chan
= session
->kernel_session
->channel_count
;
2498 DBG3("Number of kernel channels %zd", nb_chan
);
2500 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2503 case LTTNG_DOMAIN_UST
:
2504 if (session
->ust_session
!= NULL
) {
2506 nb_chan
= lttng_ht_get_count(
2507 session
->ust_session
->domain_global
.channels
);
2510 DBG3("Number of UST global channels %zd", nb_chan
);
2512 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2517 ret
= LTTNG_ERR_UND
;
2522 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2523 if (*channels
== NULL
) {
2524 ret
= LTTNG_ERR_FATAL
;
2528 list_lttng_channels(domain
, session
, *channels
);
2534 /* Return negative value to differentiate return code */
2539 * Command LTTNG_LIST_EVENTS processed by the client thread.
2541 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2542 char *channel_name
, struct lttng_event
**events
)
2545 ssize_t nb_event
= 0;
2548 case LTTNG_DOMAIN_KERNEL
:
2549 if (session
->kernel_session
!= NULL
) {
2550 nb_event
= list_lttng_kernel_events(channel_name
,
2551 session
->kernel_session
, events
);
2554 case LTTNG_DOMAIN_UST
:
2556 if (session
->ust_session
!= NULL
) {
2557 nb_event
= list_lttng_ust_global_events(channel_name
,
2558 &session
->ust_session
->domain_global
, events
);
2562 case LTTNG_DOMAIN_LOG4J
:
2563 case LTTNG_DOMAIN_JUL
:
2564 case LTTNG_DOMAIN_PYTHON
:
2565 if (session
->ust_session
) {
2566 struct lttng_ht_iter iter
;
2570 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2571 &iter
.iter
, agt
, node
.node
) {
2572 nb_event
= list_lttng_agent_events(agt
, events
);
2578 ret
= LTTNG_ERR_UND
;
2585 /* Return negative value to differentiate return code */
2590 * Using the session list, filled a lttng_session array to send back to the
2591 * client for session listing.
2593 * The session list lock MUST be acquired before calling this function. Use
2594 * session_lock_list() and session_unlock_list().
2596 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2601 struct ltt_session
*session
;
2602 struct ltt_session_list
*list
= session_get_list();
2604 DBG("Getting all available session for UID %d GID %d",
2607 * Iterate over session list and append data after the control struct in
2610 cds_list_for_each_entry(session
, &list
->head
, list
) {
2612 * Only list the sessions the user can control.
2614 if (!session_access_ok(session
, uid
, gid
)) {
2618 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2619 struct ltt_ust_session
*usess
= session
->ust_session
;
2621 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2622 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2623 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2624 ret
= build_network_session_path(sessions
[i
].path
,
2625 sizeof(sessions
[i
].path
), session
);
2627 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2628 session
->consumer
->dst
.trace_path
);
2631 PERROR("snprintf session path");
2635 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2636 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2637 sessions
[i
].enabled
= session
->active
;
2638 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2639 sessions
[i
].live_timer_interval
= session
->live_timer
;
2645 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2646 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2648 int cmd_data_pending(struct ltt_session
*session
)
2651 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2652 struct ltt_ust_session
*usess
= session
->ust_session
;
2656 /* Session MUST be stopped to ask for data availability. */
2657 if (session
->active
) {
2658 ret
= LTTNG_ERR_SESSION_STARTED
;
2662 * If stopped, just make sure we've started before else the above call
2663 * will always send that there is data pending.
2665 * The consumer assumes that when the data pending command is received,
2666 * the trace has been started before or else no output data is written
2667 * by the streams which is a condition for data pending. So, this is
2668 * *VERY* important that we don't ask the consumer before a start
2671 if (!session
->has_been_started
) {
2677 if (ksess
&& ksess
->consumer
) {
2678 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2680 /* Data is still being extracted for the kernel. */
2685 if (usess
&& usess
->consumer
) {
2686 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2688 /* Data is still being extracted for the kernel. */
2693 /* Data is ready to be read by a viewer */
2701 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2703 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2705 int cmd_snapshot_add_output(struct ltt_session
*session
,
2706 struct lttng_snapshot_output
*output
, uint32_t *id
)
2709 struct snapshot_output
*new_output
;
2714 DBG("Cmd snapshot add output for session %s", session
->name
);
2717 * Permission denied to create an output if the session is not
2718 * set in no output mode.
2720 if (session
->output_traces
) {
2721 ret
= LTTNG_ERR_EPERM
;
2725 /* Only one output is allowed until we have the "tee" feature. */
2726 if (session
->snapshot
.nb_output
== 1) {
2727 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2731 new_output
= snapshot_output_alloc();
2733 ret
= LTTNG_ERR_NOMEM
;
2737 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2738 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2739 &session
->snapshot
);
2741 if (ret
== -ENOMEM
) {
2742 ret
= LTTNG_ERR_NOMEM
;
2744 ret
= LTTNG_ERR_INVALID
;
2750 snapshot_add_output(&session
->snapshot
, new_output
);
2752 *id
= new_output
->id
;
2759 snapshot_output_destroy(new_output
);
2765 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2767 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2769 int cmd_snapshot_del_output(struct ltt_session
*session
,
2770 struct lttng_snapshot_output
*output
)
2773 struct snapshot_output
*sout
= NULL
;
2781 * Permission denied to create an output if the session is not
2782 * set in no output mode.
2784 if (session
->output_traces
) {
2785 ret
= LTTNG_ERR_EPERM
;
2790 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2792 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2793 } else if (*output
->name
!= '\0') {
2794 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2796 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2799 ret
= LTTNG_ERR_INVALID
;
2803 snapshot_delete_output(&session
->snapshot
, sout
);
2804 snapshot_output_destroy(sout
);
2813 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2815 * If no output is available, outputs is untouched and 0 is returned.
2817 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2819 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2820 struct lttng_snapshot_output
**outputs
)
2823 struct lttng_snapshot_output
*list
= NULL
;
2824 struct lttng_ht_iter iter
;
2825 struct snapshot_output
*output
;
2830 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2833 * Permission denied to create an output if the session is not
2834 * set in no output mode.
2836 if (session
->output_traces
) {
2837 ret
= -LTTNG_ERR_EPERM
;
2841 if (session
->snapshot
.nb_output
== 0) {
2846 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2848 ret
= -LTTNG_ERR_NOMEM
;
2852 /* Copy list from session to the new list object. */
2854 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2855 output
, node
.node
) {
2856 assert(output
->consumer
);
2857 list
[idx
].id
= output
->id
;
2858 list
[idx
].max_size
= output
->max_size
;
2859 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2860 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2861 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2862 sizeof(list
[idx
].ctrl_url
));
2865 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2866 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2868 ret
= -LTTNG_ERR_NOMEM
;
2873 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2874 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2876 ret
= -LTTNG_ERR_NOMEM
;
2885 ret
= session
->snapshot
.nb_output
;
2893 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2894 * snapshot output is *not* set with a remote destination.
2896 * Return 0 on success or a LTTNG_ERR code.
2898 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2899 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2902 struct lttng_ht_iter iter
;
2903 struct consumer_socket
*socket
;
2906 assert(snap_output
);
2909 DBG2("Set relayd object from snapshot output");
2911 /* Ignore if snapshot consumer output is not network. */
2912 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2917 * For each consumer socket, create and send the relayd object of the
2921 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2922 socket
, node
.node
) {
2923 ret
= send_consumer_relayd_sockets(0, session
->id
,
2924 snap_output
->consumer
, socket
,
2925 session
->name
, session
->hostname
,
2926 session
->live_timer
);
2927 if (ret
!= LTTNG_OK
) {
2939 * Record a kernel snapshot.
2941 * Return LTTNG_OK on success or a LTTNG_ERR code.
2943 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
2944 struct snapshot_output
*output
, struct ltt_session
*session
,
2945 int wait
, uint64_t nb_packets_per_stream
)
2953 /* Get the datetime for the snapshot output directory. */
2954 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
2955 sizeof(output
->datetime
));
2957 ret
= LTTNG_ERR_INVALID
;
2962 * Copy kernel session sockets so we can communicate with the right
2963 * consumer for the snapshot record command.
2965 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
2967 ret
= LTTNG_ERR_NOMEM
;
2971 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
2972 if (ret
!= LTTNG_OK
) {
2973 goto error_snapshot
;
2976 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
2977 if (ret
!= LTTNG_OK
) {
2978 goto error_snapshot
;
2985 /* Clean up copied sockets so this output can use some other later on. */
2986 consumer_destroy_output_sockets(output
->consumer
);
2993 * Record a UST snapshot.
2995 * Return 0 on success or a LTTNG_ERR error code.
2997 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
2998 struct snapshot_output
*output
, struct ltt_session
*session
,
2999 int wait
, uint64_t nb_packets_per_stream
)
3007 /* Get the datetime for the snapshot output directory. */
3008 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3009 sizeof(output
->datetime
));
3011 ret
= LTTNG_ERR_INVALID
;
3016 * Copy UST session sockets so we can communicate with the right
3017 * consumer for the snapshot record command.
3019 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3021 ret
= LTTNG_ERR_NOMEM
;
3025 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3026 if (ret
!= LTTNG_OK
) {
3027 goto error_snapshot
;
3030 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3034 ret
= LTTNG_ERR_INVALID
;
3037 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3040 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3043 goto error_snapshot
;
3049 /* Clean up copied sockets so this output can use some other later on. */
3050 consumer_destroy_output_sockets(output
->consumer
);
3056 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3057 uint64_t cur_nr_packets
)
3059 uint64_t tot_size
= 0;
3061 if (session
->kernel_session
) {
3062 struct ltt_kernel_channel
*chan
;
3063 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3065 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3066 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3068 * Don't take channel into account if we
3069 * already grab all its packets.
3073 tot_size
+= chan
->channel
->attr
.subbuf_size
3074 * chan
->stream_count
;
3078 if (session
->ust_session
) {
3079 struct ltt_ust_session
*usess
= session
->ust_session
;
3081 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3089 * Calculate the number of packets we can grab from each stream that
3090 * fits within the overall snapshot max size.
3092 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3093 * the number of packets per stream.
3095 * TODO: this approach is not perfect: we consider the worse case
3096 * (packet filling the sub-buffers) as an upper bound, but we could do
3097 * better if we do this calculation while we actually grab the packet
3098 * content: we would know how much padding we don't actually store into
3101 * This algorithm is currently bounded by the number of packets per
3104 * Since we call this algorithm before actually grabbing the data, it's
3105 * an approximation: for instance, applications could appear/disappear
3106 * in between this call and actually grabbing data.
3109 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3112 uint64_t cur_nb_packets
= 0;
3115 return 0; /* Infinite */
3118 size_left
= max_size
;
3120 uint64_t one_more_packet_tot_size
;
3122 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3124 if (!one_more_packet_tot_size
) {
3125 /* We are already grabbing all packets. */
3128 size_left
-= one_more_packet_tot_size
;
3129 if (size_left
< 0) {
3134 if (!cur_nb_packets
) {
3135 /* Not enough room to grab one packet of each stream, error. */
3138 return cur_nb_packets
;
3142 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3144 * The wait parameter is ignored so this call always wait for the snapshot to
3145 * complete before returning.
3147 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3149 int cmd_snapshot_record(struct ltt_session
*session
,
3150 struct lttng_snapshot_output
*output
, int wait
)
3153 unsigned int use_tmp_output
= 0;
3154 struct snapshot_output tmp_output
;
3155 unsigned int snapshot_success
= 0;
3160 DBG("Cmd snapshot record for session %s", session
->name
);
3163 * Permission denied to create an output if the session is not
3164 * set in no output mode.
3166 if (session
->output_traces
) {
3167 ret
= LTTNG_ERR_EPERM
;
3171 /* The session needs to be started at least once. */
3172 if (!session
->has_been_started
) {
3173 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3177 /* Use temporary output for the session. */
3178 if (*output
->ctrl_url
!= '\0') {
3179 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3180 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3183 if (ret
== -ENOMEM
) {
3184 ret
= LTTNG_ERR_NOMEM
;
3186 ret
= LTTNG_ERR_INVALID
;
3190 /* Use the global session count for the temporary snapshot. */
3191 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3195 if (session
->kernel_session
) {
3196 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3198 if (use_tmp_output
) {
3199 int64_t nb_packets_per_stream
;
3201 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3202 tmp_output
.max_size
);
3203 if (nb_packets_per_stream
< 0) {
3204 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3207 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3208 wait
, nb_packets_per_stream
);
3209 if (ret
!= LTTNG_OK
) {
3212 snapshot_success
= 1;
3214 struct snapshot_output
*sout
;
3215 struct lttng_ht_iter iter
;
3218 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3219 &iter
.iter
, sout
, node
.node
) {
3220 int64_t nb_packets_per_stream
;
3223 * Make a local copy of the output and assign the possible
3224 * temporary value given by the caller.
3226 memset(&tmp_output
, 0, sizeof(tmp_output
));
3227 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3229 if (output
->max_size
!= (uint64_t) -1ULL) {
3230 tmp_output
.max_size
= output
->max_size
;
3233 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3234 tmp_output
.max_size
);
3235 if (nb_packets_per_stream
< 0) {
3236 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3240 /* Use temporary name. */
3241 if (*output
->name
!= '\0') {
3242 strncpy(tmp_output
.name
, output
->name
,
3243 sizeof(tmp_output
.name
));
3246 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3248 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3249 session
, wait
, nb_packets_per_stream
);
3250 if (ret
!= LTTNG_OK
) {
3254 snapshot_success
= 1;
3260 if (session
->ust_session
) {
3261 struct ltt_ust_session
*usess
= session
->ust_session
;
3263 if (use_tmp_output
) {
3264 int64_t nb_packets_per_stream
;
3266 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3267 tmp_output
.max_size
);
3268 if (nb_packets_per_stream
< 0) {
3269 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3272 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3273 wait
, nb_packets_per_stream
);
3274 if (ret
!= LTTNG_OK
) {
3277 snapshot_success
= 1;
3279 struct snapshot_output
*sout
;
3280 struct lttng_ht_iter iter
;
3283 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3284 &iter
.iter
, sout
, node
.node
) {
3285 int64_t nb_packets_per_stream
;
3288 * Make a local copy of the output and assign the possible
3289 * temporary value given by the caller.
3291 memset(&tmp_output
, 0, sizeof(tmp_output
));
3292 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3294 if (output
->max_size
!= (uint64_t) -1ULL) {
3295 tmp_output
.max_size
= output
->max_size
;
3298 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3299 tmp_output
.max_size
);
3300 if (nb_packets_per_stream
< 0) {
3301 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3306 /* Use temporary name. */
3307 if (*output
->name
!= '\0') {
3308 strncpy(tmp_output
.name
, output
->name
,
3309 sizeof(tmp_output
.name
));
3312 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3314 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3315 wait
, nb_packets_per_stream
);
3316 if (ret
!= LTTNG_OK
) {
3320 snapshot_success
= 1;
3326 if (snapshot_success
) {
3327 session
->snapshot
.nb_snapshot
++;
3329 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3337 * Init command subsystem.
3342 * Set network sequence index to 1 for streams to match a relayd
3343 * socket on the consumer side.
3345 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3346 relayd_net_seq_idx
= 1;
3347 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3349 DBG("Command subsystem initialized");