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 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
928 * Command LTTNG_TRACK_PID processed by the client thread.
930 int cmd_track_pid(struct ltt_session
*session
, int domain
, int pid
)
937 case LTTNG_DOMAIN_KERNEL
:
939 struct ltt_kernel_session
*ksess
;
941 ksess
= session
->kernel_session
;
943 ret
= kernel_track_pid(ksess
, pid
);
944 if (ret
!= LTTNG_OK
) {
948 kernel_wait_quiescent(kernel_tracer_fd
);
951 case LTTNG_DOMAIN_UST
:
953 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
965 * Command LTTNG_UNTRACK_PID processed by the client thread.
967 int cmd_untrack_pid(struct ltt_session
*session
, int domain
, int pid
)
974 case LTTNG_DOMAIN_KERNEL
:
976 struct ltt_kernel_session
*ksess
;
978 ksess
= session
->kernel_session
;
980 ret
= kernel_untrack_pid(ksess
, pid
);
981 if (ret
!= LTTNG_OK
) {
985 kernel_wait_quiescent(kernel_tracer_fd
);
988 case LTTNG_DOMAIN_UST
:
990 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1002 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1004 * The wpipe arguments is used as a notifier for the kernel thread.
1006 int cmd_enable_channel(struct ltt_session
*session
,
1007 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1010 struct ltt_ust_session
*usess
= session
->ust_session
;
1011 struct lttng_ht
*chan_ht
;
1018 len
= strnlen(attr
->name
, sizeof(attr
->name
));
1020 /* Validate channel name */
1021 if (attr
->name
[0] == '.' ||
1022 memchr(attr
->name
, '/', len
) != NULL
) {
1023 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1027 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1032 * Don't try to enable a channel if the session has been started at
1033 * some point in time before. The tracer does not allow it.
1035 if (session
->has_been_started
) {
1036 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1041 * If the session is a live session, remove the switch timer, the
1042 * live timer does the same thing but sends also synchronisation
1043 * beacons for inactive streams.
1045 if (session
->live_timer
> 0) {
1046 attr
->attr
.live_timer_interval
= session
->live_timer
;
1047 attr
->attr
.switch_timer_interval
= 0;
1051 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1052 * mode and with less than 2 subbuf so block it right away and send back an
1053 * invalid attribute error.
1055 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
1056 ret
= LTTNG_ERR_INVALID
;
1060 switch (domain
->type
) {
1061 case LTTNG_DOMAIN_KERNEL
:
1063 struct ltt_kernel_channel
*kchan
;
1065 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1066 session
->kernel_session
);
1067 if (kchan
== NULL
) {
1068 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1069 if (attr
->name
[0] != '\0') {
1070 session
->kernel_session
->has_non_default_channel
= 1;
1073 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1076 if (ret
!= LTTNG_OK
) {
1080 kernel_wait_quiescent(kernel_tracer_fd
);
1083 case LTTNG_DOMAIN_UST
:
1085 struct ltt_ust_channel
*uchan
;
1087 chan_ht
= usess
->domain_global
.channels
;
1089 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1090 if (uchan
== NULL
) {
1091 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1092 if (attr
->name
[0] != '\0') {
1093 usess
->has_non_default_channel
= 1;
1096 ret
= channel_ust_enable(usess
, uchan
);
1101 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1113 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1115 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1117 struct lttng_event
*event
)
1122 DBG("Disable event command for event \'%s\'", event
->name
);
1124 event_name
= event
->name
;
1126 /* Error out on unhandled search criteria */
1127 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1128 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1129 return LTTNG_ERR_UNK
;
1131 /* Special handling for kernel domain all events. */
1132 if (domain
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event_name
, "*")) {
1133 return disable_kevent_all(session
, domain
, channel_name
, event
);
1139 case LTTNG_DOMAIN_KERNEL
:
1141 struct ltt_kernel_channel
*kchan
;
1142 struct ltt_kernel_session
*ksess
;
1144 ksess
= session
->kernel_session
;
1147 * If a non-default channel has been created in the
1148 * session, explicitely require that -c chan_name needs
1151 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1152 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1156 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1157 if (kchan
== NULL
) {
1158 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1162 switch (event
->type
) {
1163 case LTTNG_EVENT_ALL
:
1164 case LTTNG_EVENT_TRACEPOINT
:
1165 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1166 if (ret
!= LTTNG_OK
) {
1170 case LTTNG_EVENT_SYSCALL
:
1171 ret
= event_kernel_disable_syscall(kchan
, event_name
);
1172 if (ret
!= LTTNG_OK
) {
1177 ret
= LTTNG_ERR_UNK
;
1181 kernel_wait_quiescent(kernel_tracer_fd
);
1184 case LTTNG_DOMAIN_UST
:
1186 struct ltt_ust_channel
*uchan
;
1187 struct ltt_ust_session
*usess
;
1189 usess
= session
->ust_session
;
1192 * If a non-default channel has been created in the
1193 * session, explicitely require that -c chan_name needs
1196 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1197 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1201 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1203 if (uchan
== NULL
) {
1204 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1208 switch (event
->type
) {
1209 case LTTNG_EVENT_ALL
:
1210 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1211 if (ret
!= LTTNG_OK
) {
1216 ret
= LTTNG_ERR_UNK
;
1220 DBG3("Disable UST event %s in channel %s completed", event_name
,
1224 case LTTNG_DOMAIN_LOG4J
:
1225 case LTTNG_DOMAIN_JUL
:
1226 case LTTNG_DOMAIN_PYTHON
:
1229 struct ltt_ust_session
*usess
= session
->ust_session
;
1233 switch (event
->type
) {
1234 case LTTNG_EVENT_ALL
:
1237 ret
= LTTNG_ERR_UNK
;
1241 agt
= trace_ust_find_agent(usess
, domain
);
1243 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1246 /* The wild card * means that everything should be disabled. */
1247 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1248 ret
= event_agent_disable_all(usess
, agt
);
1250 ret
= event_agent_disable(usess
, agt
, event_name
);
1252 if (ret
!= LTTNG_OK
) {
1259 ret
= LTTNG_ERR_UND
;
1271 * Command LTTNG_DISABLE_EVENT for event "*" processed by the client thread.
1274 int disable_kevent_all(struct ltt_session
*session
, int domain
,
1276 struct lttng_event
*event
)
1283 case LTTNG_DOMAIN_KERNEL
:
1285 struct ltt_kernel_session
*ksess
;
1286 struct ltt_kernel_channel
*kchan
;
1288 ksess
= session
->kernel_session
;
1291 * If a non-default channel has been created in the
1292 * session, explicitely require that -c chan_name needs
1295 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1296 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1300 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1301 if (kchan
== NULL
) {
1302 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1306 switch (event
->type
) {
1307 case LTTNG_EVENT_ALL
:
1308 ret
= event_kernel_disable_all(kchan
);
1309 if (ret
!= LTTNG_OK
) {
1313 case LTTNG_EVENT_SYSCALL
:
1314 ret
= event_kernel_disable_syscall(kchan
, "");
1315 if (ret
!= LTTNG_OK
) {
1320 ret
= LTTNG_ERR_UNK
;
1324 kernel_wait_quiescent(kernel_tracer_fd
);
1328 ret
= LTTNG_ERR_UND
;
1340 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1342 int cmd_add_context(struct ltt_session
*session
, int domain
,
1343 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1345 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1348 case LTTNG_DOMAIN_KERNEL
:
1349 assert(session
->kernel_session
);
1351 if (session
->kernel_session
->channel_count
== 0) {
1352 /* Create default channel */
1353 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1354 if (ret
!= LTTNG_OK
) {
1357 chan_kern_created
= 1;
1359 /* Add kernel context to kernel tracer */
1360 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1361 if (ret
!= LTTNG_OK
) {
1365 case LTTNG_DOMAIN_UST
:
1367 struct ltt_ust_session
*usess
= session
->ust_session
;
1368 unsigned int chan_count
;
1372 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1373 if (chan_count
== 0) {
1374 struct lttng_channel
*attr
;
1375 /* Create default channel */
1376 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1378 ret
= LTTNG_ERR_FATAL
;
1382 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1383 if (ret
!= LTTNG_OK
) {
1388 chan_ust_created
= 1;
1391 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1392 if (ret
!= LTTNG_OK
) {
1398 ret
= LTTNG_ERR_UND
;
1405 if (chan_kern_created
) {
1406 struct ltt_kernel_channel
*kchan
=
1407 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1408 session
->kernel_session
);
1409 /* Created previously, this should NOT fail. */
1411 kernel_destroy_channel(kchan
);
1414 if (chan_ust_created
) {
1415 struct ltt_ust_channel
*uchan
=
1416 trace_ust_find_channel_by_name(
1417 session
->ust_session
->domain_global
.channels
,
1418 DEFAULT_CHANNEL_NAME
);
1419 /* Created previously, this should NOT fail. */
1421 /* Remove from the channel list of the session. */
1422 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1424 trace_ust_destroy_channel(uchan
);
1429 static int validate_event_name(const char *name
)
1432 const char *c
= name
;
1433 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1436 * Make sure that unescaped wildcards are only used as the last
1437 * character of the event name.
1439 while (c
< event_name_end
) {
1447 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1448 /* Wildcard is not the last character */
1449 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1462 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1463 * We own filter, exclusion, and filter_expression.
1465 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1466 char *channel_name
, struct lttng_event
*event
,
1467 char *filter_expression
,
1468 struct lttng_filter_bytecode
*filter
,
1469 struct lttng_event_exclusion
*exclusion
,
1472 int ret
, channel_created
= 0;
1473 struct lttng_channel
*attr
;
1477 assert(channel_name
);
1479 DBG("Enable event command for event \'%s\'", event
->name
);
1481 /* Special handling for kernel domain all events. */
1482 if (domain
->type
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event
->name
, "*")) {
1483 return enable_kevent_all(session
, domain
, channel_name
, event
,
1484 filter_expression
, filter
, wpipe
);
1487 ret
= validate_event_name(event
->name
);
1494 switch (domain
->type
) {
1495 case LTTNG_DOMAIN_KERNEL
:
1497 struct ltt_kernel_channel
*kchan
;
1500 * If a non-default channel has been created in the
1501 * session, explicitely require that -c chan_name needs
1504 if (session
->kernel_session
->has_non_default_channel
1505 && channel_name
[0] == '\0') {
1506 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1510 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1511 session
->kernel_session
);
1512 if (kchan
== NULL
) {
1513 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1514 LTTNG_BUFFER_GLOBAL
);
1516 ret
= LTTNG_ERR_FATAL
;
1519 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1521 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1522 if (ret
!= LTTNG_OK
) {
1528 channel_created
= 1;
1531 /* Get the newly created kernel channel pointer */
1532 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1533 session
->kernel_session
);
1534 if (kchan
== NULL
) {
1535 /* This sould not happen... */
1536 ret
= LTTNG_ERR_FATAL
;
1540 switch (event
->type
) {
1541 case LTTNG_EVENT_ALL
:
1542 case LTTNG_EVENT_PROBE
:
1543 case LTTNG_EVENT_FUNCTION
:
1544 case LTTNG_EVENT_FUNCTION_ENTRY
:
1545 case LTTNG_EVENT_TRACEPOINT
:
1546 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1547 if (ret
!= LTTNG_OK
) {
1548 if (channel_created
) {
1549 /* Let's not leak a useless channel. */
1550 kernel_destroy_channel(kchan
);
1555 case LTTNG_EVENT_SYSCALL
:
1556 ret
= event_kernel_enable_syscall(kchan
, event
->name
);
1557 if (ret
!= LTTNG_OK
) {
1562 ret
= LTTNG_ERR_UNK
;
1566 kernel_wait_quiescent(kernel_tracer_fd
);
1569 case LTTNG_DOMAIN_UST
:
1571 struct ltt_ust_channel
*uchan
;
1572 struct ltt_ust_session
*usess
= session
->ust_session
;
1577 * If a non-default channel has been created in the
1578 * session, explicitely require that -c chan_name needs
1581 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1582 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1586 /* Get channel from global UST domain */
1587 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1589 if (uchan
== NULL
) {
1590 /* Create default channel */
1591 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1592 usess
->buffer_type
);
1594 ret
= LTTNG_ERR_FATAL
;
1597 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1599 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1600 if (ret
!= LTTNG_OK
) {
1606 /* Get the newly created channel reference back */
1607 uchan
= trace_ust_find_channel_by_name(
1608 usess
->domain_global
.channels
, channel_name
);
1612 /* At this point, the session and channel exist on the tracer */
1613 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1614 filter_expression
, filter
, exclusion
);
1615 /* We have passed ownership */
1616 filter_expression
= NULL
;
1619 if (ret
!= LTTNG_OK
) {
1624 case LTTNG_DOMAIN_LOG4J
:
1625 case LTTNG_DOMAIN_JUL
:
1626 case LTTNG_DOMAIN_PYTHON
:
1628 const char *default_event_name
, *default_chan_name
;
1630 struct lttng_event uevent
;
1631 struct lttng_domain tmp_dom
;
1632 struct ltt_ust_session
*usess
= session
->ust_session
;
1636 agt
= trace_ust_find_agent(usess
, domain
->type
);
1638 agt
= agent_create(domain
->type
);
1640 ret
= -LTTNG_ERR_NOMEM
;
1643 agent_add(agt
, usess
->agents
);
1646 /* Create the default tracepoint. */
1647 memset(&uevent
, 0, sizeof(uevent
));
1648 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1649 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1650 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1651 if (!default_event_name
) {
1652 ret
= -LTTNG_ERR_FATAL
;
1655 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1656 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1659 * The domain type is changed because we are about to enable the
1660 * default channel and event for the JUL domain that are hardcoded.
1661 * This happens in the UST domain.
1663 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1664 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1666 switch (domain
->type
) {
1667 case LTTNG_DOMAIN_LOG4J
:
1668 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1670 case LTTNG_DOMAIN_JUL
:
1671 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1673 case LTTNG_DOMAIN_PYTHON
:
1674 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
1677 /* The switch/case we are in should avoid this else big problem */
1682 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1685 filter_copy
= zmalloc(
1686 sizeof(struct lttng_filter_bytecode
)
1692 memcpy(filter_copy
, filter
,
1693 sizeof(struct lttng_filter_bytecode
)
1697 ret
= cmd_enable_event(session
, &tmp_dom
,
1698 (char *) default_chan_name
,
1699 &uevent
, filter_expression
, filter_copy
,
1701 /* We have passed ownership */
1702 filter_expression
= NULL
;
1705 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1709 /* The wild card * means that everything should be enabled. */
1710 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1711 ret
= event_agent_enable_all(usess
, agt
, event
, filter
);
1714 ret
= event_agent_enable(usess
, agt
, event
, filter
);
1717 if (ret
!= LTTNG_OK
) {
1724 ret
= LTTNG_ERR_UND
;
1731 free(filter_expression
);
1739 * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread.
1742 int enable_kevent_all(struct ltt_session
*session
,
1743 struct lttng_domain
*domain
, char *channel_name
,
1744 struct lttng_event
*event
,
1745 char *filter_expression
,
1746 struct lttng_filter_bytecode
*filter
, int wpipe
)
1749 struct lttng_channel
*attr
;
1752 assert(channel_name
);
1756 switch (domain
->type
) {
1757 case LTTNG_DOMAIN_KERNEL
:
1759 struct ltt_kernel_channel
*kchan
;
1761 assert(session
->kernel_session
);
1764 * If a non-default channel has been created in the
1765 * session, explicitely require that -c chan_name needs
1768 if (session
->kernel_session
->has_non_default_channel
1769 && channel_name
[0] == '\0') {
1770 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1774 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1775 session
->kernel_session
);
1776 if (kchan
== NULL
) {
1777 /* Create default channel */
1778 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1779 LTTNG_BUFFER_GLOBAL
);
1781 ret
= LTTNG_ERR_FATAL
;
1784 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1786 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1787 if (ret
!= LTTNG_OK
) {
1793 /* Get the newly created kernel channel pointer */
1794 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1795 session
->kernel_session
);
1799 switch (event
->type
) {
1800 case LTTNG_EVENT_SYSCALL
:
1801 ret
= event_kernel_enable_syscall(kchan
, "");
1802 if (ret
!= LTTNG_OK
) {
1806 case LTTNG_EVENT_TRACEPOINT
:
1808 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1809 * events already registered to the channel.
1811 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1813 case LTTNG_EVENT_ALL
:
1814 /* Enable syscalls and tracepoints */
1815 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1818 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1822 /* Manage return value */
1823 if (ret
!= LTTNG_OK
) {
1825 * On error, cmd_enable_channel call will take care of destroying
1826 * the created channel if it was needed.
1831 kernel_wait_quiescent(kernel_tracer_fd
);
1835 ret
= LTTNG_ERR_UND
;
1848 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1850 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1853 ssize_t nb_events
= 0;
1856 case LTTNG_DOMAIN_KERNEL
:
1857 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1858 if (nb_events
< 0) {
1859 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1863 case LTTNG_DOMAIN_UST
:
1864 nb_events
= ust_app_list_events(events
);
1865 if (nb_events
< 0) {
1866 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1870 case LTTNG_DOMAIN_LOG4J
:
1871 case LTTNG_DOMAIN_JUL
:
1872 case LTTNG_DOMAIN_PYTHON
:
1873 nb_events
= agent_list_events(events
, domain
);
1874 if (nb_events
< 0) {
1875 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1880 ret
= LTTNG_ERR_UND
;
1887 /* Return negative value to differentiate return code */
1892 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1894 ssize_t
cmd_list_tracepoint_fields(int domain
,
1895 struct lttng_event_field
**fields
)
1898 ssize_t nb_fields
= 0;
1901 case LTTNG_DOMAIN_UST
:
1902 nb_fields
= ust_app_list_event_fields(fields
);
1903 if (nb_fields
< 0) {
1904 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1908 case LTTNG_DOMAIN_KERNEL
:
1909 default: /* fall-through */
1910 ret
= LTTNG_ERR_UND
;
1917 /* Return negative value to differentiate return code */
1921 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1923 return syscall_table_list(events
);
1927 * Command LTTNG_START_TRACE processed by the client thread.
1929 int cmd_start_trace(struct ltt_session
*session
)
1932 unsigned long nb_chan
= 0;
1933 struct ltt_kernel_session
*ksession
;
1934 struct ltt_ust_session
*usess
;
1938 /* Ease our life a bit ;) */
1939 ksession
= session
->kernel_session
;
1940 usess
= session
->ust_session
;
1942 /* Is the session already started? */
1943 if (session
->active
) {
1944 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1949 * Starting a session without channel is useless since after that it's not
1950 * possible to enable channel thus inform the client.
1952 if (usess
&& usess
->domain_global
.channels
) {
1954 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1958 nb_chan
+= ksession
->channel_count
;
1961 ret
= LTTNG_ERR_NO_CHANNEL
;
1965 /* Kernel tracing */
1966 if (ksession
!= NULL
) {
1967 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1968 if (ret
!= LTTNG_OK
) {
1973 /* Flag session that trace should start automatically */
1976 * Even though the start trace might fail, flag this session active so
1977 * other application coming in are started by default.
1981 ret
= ust_app_start_trace_all(usess
);
1983 ret
= LTTNG_ERR_UST_START_FAIL
;
1988 /* Flag this after a successful start. */
1989 session
->has_been_started
= 1;
1990 session
->active
= 1;
1999 * Command LTTNG_STOP_TRACE processed by the client thread.
2001 int cmd_stop_trace(struct ltt_session
*session
)
2004 struct ltt_kernel_channel
*kchan
;
2005 struct ltt_kernel_session
*ksession
;
2006 struct ltt_ust_session
*usess
;
2011 ksession
= session
->kernel_session
;
2012 usess
= session
->ust_session
;
2014 /* Session is not active. Skip everythong and inform the client. */
2015 if (!session
->active
) {
2016 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2021 if (ksession
&& ksession
->active
) {
2022 DBG("Stop kernel tracing");
2024 /* Flush metadata if exist */
2025 if (ksession
->metadata_stream_fd
>= 0) {
2026 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2028 ERR("Kernel metadata flush failed");
2032 /* Flush all buffers before stopping */
2033 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2034 ret
= kernel_flush_buffer(kchan
);
2036 ERR("Kernel flush buffer error");
2040 ret
= kernel_stop_session(ksession
);
2042 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2046 kernel_wait_quiescent(kernel_tracer_fd
);
2048 ksession
->active
= 0;
2051 if (usess
&& usess
->active
) {
2053 * Even though the stop trace might fail, flag this session inactive so
2054 * other application coming in are not started by default.
2058 ret
= ust_app_stop_trace_all(usess
);
2060 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2065 /* Flag inactive after a successful stop. */
2066 session
->active
= 0;
2074 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2076 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2077 struct lttng_uri
*uris
)
2080 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2081 struct ltt_ust_session
*usess
= session
->ust_session
;
2087 /* Can't set consumer URI if the session is active. */
2088 if (session
->active
) {
2089 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2093 /* Set the "global" consumer URIs */
2094 for (i
= 0; i
< nb_uri
; i
++) {
2095 ret
= add_uri_to_consumer(session
->consumer
,
2096 &uris
[i
], 0, session
->name
);
2097 if (ret
!= LTTNG_OK
) {
2102 /* Set UST session URIs */
2103 if (session
->ust_session
) {
2104 for (i
= 0; i
< nb_uri
; i
++) {
2105 ret
= add_uri_to_consumer(
2106 session
->ust_session
->consumer
,
2107 &uris
[i
], LTTNG_DOMAIN_UST
,
2109 if (ret
!= LTTNG_OK
) {
2115 /* Set kernel session URIs */
2116 if (session
->kernel_session
) {
2117 for (i
= 0; i
< nb_uri
; i
++) {
2118 ret
= add_uri_to_consumer(
2119 session
->kernel_session
->consumer
,
2120 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2122 if (ret
!= LTTNG_OK
) {
2129 * Make sure to set the session in output mode after we set URI since a
2130 * session can be created without URL (thus flagged in no output mode).
2132 session
->output_traces
= 1;
2134 ksess
->output_traces
= 1;
2138 usess
->output_traces
= 1;
2149 * Command LTTNG_CREATE_SESSION processed by the client thread.
2151 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2152 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2155 struct ltt_session
*session
;
2161 * Verify if the session already exist
2163 * XXX: There is no need for the session lock list here since the caller
2164 * (process_client_msg) is holding it. We might want to change that so a
2165 * single command does not lock the entire session list.
2167 session
= session_find_by_name(name
);
2168 if (session
!= NULL
) {
2169 ret
= LTTNG_ERR_EXIST_SESS
;
2173 /* Create tracing session in the registry */
2174 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2175 LTTNG_SOCK_GET_GID_CRED(creds
));
2176 if (ret
!= LTTNG_OK
) {
2181 * Get the newly created session pointer back
2183 * XXX: There is no need for the session lock list here since the caller
2184 * (process_client_msg) is holding it. We might want to change that so a
2185 * single command does not lock the entire session list.
2187 session
= session_find_by_name(name
);
2190 session
->live_timer
= live_timer
;
2191 /* Create default consumer output for the session not yet created. */
2192 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2193 if (session
->consumer
== NULL
) {
2194 ret
= LTTNG_ERR_FATAL
;
2195 goto consumer_error
;
2199 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2200 if (ret
!= LTTNG_OK
) {
2201 goto consumer_error
;
2203 session
->output_traces
= 1;
2205 session
->output_traces
= 0;
2206 DBG2("Session %s created with no output", session
->name
);
2209 session
->consumer
->enabled
= 1;
2214 session_destroy(session
);
2221 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2223 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2224 size_t nb_uri
, lttng_sock_cred
*creds
)
2227 struct ltt_session
*session
;
2228 struct snapshot_output
*new_output
= NULL
;
2234 * Create session in no output mode with URIs set to NULL. The uris we've
2235 * received are for a default snapshot output if one.
2237 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2238 if (ret
!= LTTNG_OK
) {
2242 /* Get the newly created session pointer back. This should NEVER fail. */
2243 session
= session_find_by_name(name
);
2246 /* Flag session for snapshot mode. */
2247 session
->snapshot_mode
= 1;
2249 /* Skip snapshot output creation if no URI is given. */
2254 new_output
= snapshot_output_alloc();
2256 ret
= LTTNG_ERR_NOMEM
;
2257 goto error_snapshot_alloc
;
2260 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2261 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2263 if (ret
== -ENOMEM
) {
2264 ret
= LTTNG_ERR_NOMEM
;
2266 ret
= LTTNG_ERR_INVALID
;
2268 goto error_snapshot
;
2272 snapshot_add_output(&session
->snapshot
, new_output
);
2279 snapshot_output_destroy(new_output
);
2280 error_snapshot_alloc
:
2281 session_destroy(session
);
2287 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2289 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2292 struct ltt_ust_session
*usess
;
2293 struct ltt_kernel_session
*ksess
;
2298 usess
= session
->ust_session
;
2299 ksess
= session
->kernel_session
;
2301 /* Clean kernel session teardown */
2302 kernel_destroy_session(ksess
);
2304 /* UST session teardown */
2306 /* Close any relayd session */
2307 consumer_output_send_destroy_relayd(usess
->consumer
);
2309 /* Destroy every UST application related to this session. */
2310 ret
= ust_app_destroy_trace_all(usess
);
2312 ERR("Error in ust_app_destroy_trace_all");
2315 /* Clean up the rest. */
2316 trace_ust_destroy_session(usess
);
2320 * Must notify the kernel thread here to update it's poll set in order to
2321 * remove the channel(s)' fd just destroyed.
2323 ret
= notify_thread_pipe(wpipe
);
2325 PERROR("write kernel poll pipe");
2328 ret
= session_destroy(session
);
2334 * Command LTTNG_CALIBRATE processed by the client thread.
2336 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2341 case LTTNG_DOMAIN_KERNEL
:
2343 struct lttng_kernel_calibrate kcalibrate
;
2345 switch (calibrate
->type
) {
2346 case LTTNG_CALIBRATE_FUNCTION
:
2348 /* Default and only possible calibrate option. */
2349 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2353 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2355 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2360 case LTTNG_DOMAIN_UST
:
2362 struct lttng_ust_calibrate ucalibrate
;
2364 switch (calibrate
->type
) {
2365 case LTTNG_CALIBRATE_FUNCTION
:
2367 /* Default and only possible calibrate option. */
2368 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2372 ret
= ust_app_calibrate_glb(&ucalibrate
);
2374 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2380 ret
= LTTNG_ERR_UND
;
2391 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2393 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2394 const char *sock_path
, struct consumer_data
*cdata
)
2397 struct consumer_socket
*socket
= NULL
;
2404 case LTTNG_DOMAIN_KERNEL
:
2406 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2410 /* Can't register a consumer if there is already one */
2411 if (ksess
->consumer_fds_sent
!= 0) {
2412 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2416 sock
= lttcomm_connect_unix_sock(sock_path
);
2418 ret
= LTTNG_ERR_CONNECT_FAIL
;
2421 cdata
->cmd_sock
= sock
;
2423 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2424 if (socket
== NULL
) {
2427 PERROR("close register consumer");
2429 cdata
->cmd_sock
= -1;
2430 ret
= LTTNG_ERR_FATAL
;
2434 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2435 if (socket
->lock
== NULL
) {
2436 PERROR("zmalloc pthread mutex");
2437 ret
= LTTNG_ERR_FATAL
;
2440 pthread_mutex_init(socket
->lock
, NULL
);
2441 socket
->registered
= 1;
2444 consumer_add_socket(socket
, ksess
->consumer
);
2447 pthread_mutex_lock(&cdata
->pid_mutex
);
2449 pthread_mutex_unlock(&cdata
->pid_mutex
);
2454 /* TODO: Userspace tracing */
2455 ret
= LTTNG_ERR_UND
;
2463 consumer_destroy_socket(socket
);
2469 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2471 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2472 struct lttng_domain
**domains
)
2477 struct lttng_ht_iter iter
;
2479 if (session
->kernel_session
!= NULL
) {
2480 DBG3("Listing domains found kernel domain");
2484 if (session
->ust_session
!= NULL
) {
2485 DBG3("Listing domains found UST global domain");
2489 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2491 if (agt
->being_used
) {
2502 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2503 if (*domains
== NULL
) {
2504 ret
= LTTNG_ERR_FATAL
;
2508 if (session
->kernel_session
!= NULL
) {
2509 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2513 if (session
->ust_session
!= NULL
) {
2514 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2515 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2519 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2521 if (agt
->being_used
) {
2522 (*domains
)[index
].type
= agt
->domain
;
2523 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2533 /* Return negative value to differentiate return code */
2539 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2541 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2542 struct lttng_channel
**channels
)
2545 ssize_t nb_chan
= 0;
2548 case LTTNG_DOMAIN_KERNEL
:
2549 if (session
->kernel_session
!= NULL
) {
2550 nb_chan
= session
->kernel_session
->channel_count
;
2552 DBG3("Number of kernel channels %zd", nb_chan
);
2554 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2557 case LTTNG_DOMAIN_UST
:
2558 if (session
->ust_session
!= NULL
) {
2560 nb_chan
= lttng_ht_get_count(
2561 session
->ust_session
->domain_global
.channels
);
2564 DBG3("Number of UST global channels %zd", nb_chan
);
2566 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2571 ret
= LTTNG_ERR_UND
;
2576 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2577 if (*channels
== NULL
) {
2578 ret
= LTTNG_ERR_FATAL
;
2582 list_lttng_channels(domain
, session
, *channels
);
2588 /* Return negative value to differentiate return code */
2593 * Command LTTNG_LIST_EVENTS processed by the client thread.
2595 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2596 char *channel_name
, struct lttng_event
**events
)
2599 ssize_t nb_event
= 0;
2602 case LTTNG_DOMAIN_KERNEL
:
2603 if (session
->kernel_session
!= NULL
) {
2604 nb_event
= list_lttng_kernel_events(channel_name
,
2605 session
->kernel_session
, events
);
2608 case LTTNG_DOMAIN_UST
:
2610 if (session
->ust_session
!= NULL
) {
2611 nb_event
= list_lttng_ust_global_events(channel_name
,
2612 &session
->ust_session
->domain_global
, events
);
2616 case LTTNG_DOMAIN_LOG4J
:
2617 case LTTNG_DOMAIN_JUL
:
2618 case LTTNG_DOMAIN_PYTHON
:
2619 if (session
->ust_session
) {
2620 struct lttng_ht_iter iter
;
2624 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2625 &iter
.iter
, agt
, node
.node
) {
2626 nb_event
= list_lttng_agent_events(agt
, events
);
2632 ret
= LTTNG_ERR_UND
;
2639 /* Return negative value to differentiate return code */
2644 * Using the session list, filled a lttng_session array to send back to the
2645 * client for session listing.
2647 * The session list lock MUST be acquired before calling this function. Use
2648 * session_lock_list() and session_unlock_list().
2650 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2655 struct ltt_session
*session
;
2656 struct ltt_session_list
*list
= session_get_list();
2658 DBG("Getting all available session for UID %d GID %d",
2661 * Iterate over session list and append data after the control struct in
2664 cds_list_for_each_entry(session
, &list
->head
, list
) {
2666 * Only list the sessions the user can control.
2668 if (!session_access_ok(session
, uid
, gid
)) {
2672 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2673 struct ltt_ust_session
*usess
= session
->ust_session
;
2675 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2676 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2677 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2678 ret
= build_network_session_path(sessions
[i
].path
,
2679 sizeof(sessions
[i
].path
), session
);
2681 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2682 session
->consumer
->dst
.trace_path
);
2685 PERROR("snprintf session path");
2689 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2690 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2691 sessions
[i
].enabled
= session
->active
;
2692 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2693 sessions
[i
].live_timer_interval
= session
->live_timer
;
2699 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2700 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2702 int cmd_data_pending(struct ltt_session
*session
)
2705 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2706 struct ltt_ust_session
*usess
= session
->ust_session
;
2710 /* Session MUST be stopped to ask for data availability. */
2711 if (session
->active
) {
2712 ret
= LTTNG_ERR_SESSION_STARTED
;
2716 * If stopped, just make sure we've started before else the above call
2717 * will always send that there is data pending.
2719 * The consumer assumes that when the data pending command is received,
2720 * the trace has been started before or else no output data is written
2721 * by the streams which is a condition for data pending. So, this is
2722 * *VERY* important that we don't ask the consumer before a start
2725 if (!session
->has_been_started
) {
2731 if (ksess
&& ksess
->consumer
) {
2732 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2734 /* Data is still being extracted for the kernel. */
2739 if (usess
&& usess
->consumer
) {
2740 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2742 /* Data is still being extracted for the kernel. */
2747 /* Data is ready to be read by a viewer */
2755 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2757 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2759 int cmd_snapshot_add_output(struct ltt_session
*session
,
2760 struct lttng_snapshot_output
*output
, uint32_t *id
)
2763 struct snapshot_output
*new_output
;
2768 DBG("Cmd snapshot add output for session %s", session
->name
);
2771 * Permission denied to create an output if the session is not
2772 * set in no output mode.
2774 if (session
->output_traces
) {
2775 ret
= LTTNG_ERR_EPERM
;
2779 /* Only one output is allowed until we have the "tee" feature. */
2780 if (session
->snapshot
.nb_output
== 1) {
2781 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2785 new_output
= snapshot_output_alloc();
2787 ret
= LTTNG_ERR_NOMEM
;
2791 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2792 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2793 &session
->snapshot
);
2795 if (ret
== -ENOMEM
) {
2796 ret
= LTTNG_ERR_NOMEM
;
2798 ret
= LTTNG_ERR_INVALID
;
2804 snapshot_add_output(&session
->snapshot
, new_output
);
2806 *id
= new_output
->id
;
2813 snapshot_output_destroy(new_output
);
2819 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2821 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2823 int cmd_snapshot_del_output(struct ltt_session
*session
,
2824 struct lttng_snapshot_output
*output
)
2827 struct snapshot_output
*sout
= NULL
;
2835 * Permission denied to create an output if the session is not
2836 * set in no output mode.
2838 if (session
->output_traces
) {
2839 ret
= LTTNG_ERR_EPERM
;
2844 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2846 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2847 } else if (*output
->name
!= '\0') {
2848 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2850 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2853 ret
= LTTNG_ERR_INVALID
;
2857 snapshot_delete_output(&session
->snapshot
, sout
);
2858 snapshot_output_destroy(sout
);
2867 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2869 * If no output is available, outputs is untouched and 0 is returned.
2871 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2873 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2874 struct lttng_snapshot_output
**outputs
)
2877 struct lttng_snapshot_output
*list
= NULL
;
2878 struct lttng_ht_iter iter
;
2879 struct snapshot_output
*output
;
2884 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2887 * Permission denied to create an output if the session is not
2888 * set in no output mode.
2890 if (session
->output_traces
) {
2891 ret
= -LTTNG_ERR_EPERM
;
2895 if (session
->snapshot
.nb_output
== 0) {
2900 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2902 ret
= -LTTNG_ERR_NOMEM
;
2906 /* Copy list from session to the new list object. */
2908 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2909 output
, node
.node
) {
2910 assert(output
->consumer
);
2911 list
[idx
].id
= output
->id
;
2912 list
[idx
].max_size
= output
->max_size
;
2913 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2914 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2915 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2916 sizeof(list
[idx
].ctrl_url
));
2919 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2920 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2922 ret
= -LTTNG_ERR_NOMEM
;
2927 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2928 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2930 ret
= -LTTNG_ERR_NOMEM
;
2939 ret
= session
->snapshot
.nb_output
;
2947 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2948 * snapshot output is *not* set with a remote destination.
2950 * Return 0 on success or a LTTNG_ERR code.
2952 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
2953 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
2956 struct lttng_ht_iter iter
;
2957 struct consumer_socket
*socket
;
2960 assert(snap_output
);
2963 DBG2("Set relayd object from snapshot output");
2965 /* Ignore if snapshot consumer output is not network. */
2966 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
2971 * For each consumer socket, create and send the relayd object of the
2975 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
2976 socket
, node
.node
) {
2977 ret
= send_consumer_relayd_sockets(0, session
->id
,
2978 snap_output
->consumer
, socket
,
2979 session
->name
, session
->hostname
,
2980 session
->live_timer
);
2981 if (ret
!= LTTNG_OK
) {
2993 * Record a kernel snapshot.
2995 * Return LTTNG_OK on success or a LTTNG_ERR code.
2997 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
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 kernel session sockets so we can communicate with the right
3017 * consumer for the snapshot record command.
3019 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3021 ret
= LTTNG_ERR_NOMEM
;
3025 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3026 if (ret
!= LTTNG_OK
) {
3027 goto error_snapshot
;
3030 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3031 if (ret
!= LTTNG_OK
) {
3032 goto error_snapshot
;
3039 /* Clean up copied sockets so this output can use some other later on. */
3040 consumer_destroy_output_sockets(output
->consumer
);
3047 * Record a UST snapshot.
3049 * Return 0 on success or a LTTNG_ERR error code.
3051 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3052 struct snapshot_output
*output
, struct ltt_session
*session
,
3053 int wait
, uint64_t nb_packets_per_stream
)
3061 /* Get the datetime for the snapshot output directory. */
3062 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3063 sizeof(output
->datetime
));
3065 ret
= LTTNG_ERR_INVALID
;
3070 * Copy UST session sockets so we can communicate with the right
3071 * consumer for the snapshot record command.
3073 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3075 ret
= LTTNG_ERR_NOMEM
;
3079 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3080 if (ret
!= LTTNG_OK
) {
3081 goto error_snapshot
;
3084 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3088 ret
= LTTNG_ERR_INVALID
;
3091 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3094 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3097 goto error_snapshot
;
3103 /* Clean up copied sockets so this output can use some other later on. */
3104 consumer_destroy_output_sockets(output
->consumer
);
3110 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3111 uint64_t cur_nr_packets
)
3113 uint64_t tot_size
= 0;
3115 if (session
->kernel_session
) {
3116 struct ltt_kernel_channel
*chan
;
3117 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3119 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3120 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3122 * Don't take channel into account if we
3123 * already grab all its packets.
3127 tot_size
+= chan
->channel
->attr
.subbuf_size
3128 * chan
->stream_count
;
3132 if (session
->ust_session
) {
3133 struct ltt_ust_session
*usess
= session
->ust_session
;
3135 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3143 * Calculate the number of packets we can grab from each stream that
3144 * fits within the overall snapshot max size.
3146 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3147 * the number of packets per stream.
3149 * TODO: this approach is not perfect: we consider the worse case
3150 * (packet filling the sub-buffers) as an upper bound, but we could do
3151 * better if we do this calculation while we actually grab the packet
3152 * content: we would know how much padding we don't actually store into
3155 * This algorithm is currently bounded by the number of packets per
3158 * Since we call this algorithm before actually grabbing the data, it's
3159 * an approximation: for instance, applications could appear/disappear
3160 * in between this call and actually grabbing data.
3163 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3166 uint64_t cur_nb_packets
= 0;
3169 return 0; /* Infinite */
3172 size_left
= max_size
;
3174 uint64_t one_more_packet_tot_size
;
3176 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3178 if (!one_more_packet_tot_size
) {
3179 /* We are already grabbing all packets. */
3182 size_left
-= one_more_packet_tot_size
;
3183 if (size_left
< 0) {
3188 if (!cur_nb_packets
) {
3189 /* Not enough room to grab one packet of each stream, error. */
3192 return cur_nb_packets
;
3196 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3198 * The wait parameter is ignored so this call always wait for the snapshot to
3199 * complete before returning.
3201 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3203 int cmd_snapshot_record(struct ltt_session
*session
,
3204 struct lttng_snapshot_output
*output
, int wait
)
3207 unsigned int use_tmp_output
= 0;
3208 struct snapshot_output tmp_output
;
3209 unsigned int snapshot_success
= 0;
3214 DBG("Cmd snapshot record for session %s", session
->name
);
3217 * Permission denied to create an output if the session is not
3218 * set in no output mode.
3220 if (session
->output_traces
) {
3221 ret
= LTTNG_ERR_EPERM
;
3225 /* The session needs to be started at least once. */
3226 if (!session
->has_been_started
) {
3227 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3231 /* Use temporary output for the session. */
3232 if (*output
->ctrl_url
!= '\0') {
3233 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3234 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3237 if (ret
== -ENOMEM
) {
3238 ret
= LTTNG_ERR_NOMEM
;
3240 ret
= LTTNG_ERR_INVALID
;
3244 /* Use the global session count for the temporary snapshot. */
3245 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3249 if (session
->kernel_session
) {
3250 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3252 if (use_tmp_output
) {
3253 int64_t nb_packets_per_stream
;
3255 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3256 tmp_output
.max_size
);
3257 if (nb_packets_per_stream
< 0) {
3258 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3261 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3262 wait
, nb_packets_per_stream
);
3263 if (ret
!= LTTNG_OK
) {
3266 snapshot_success
= 1;
3268 struct snapshot_output
*sout
;
3269 struct lttng_ht_iter iter
;
3272 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3273 &iter
.iter
, sout
, node
.node
) {
3274 int64_t nb_packets_per_stream
;
3277 * Make a local copy of the output and assign the possible
3278 * temporary value given by the caller.
3280 memset(&tmp_output
, 0, sizeof(tmp_output
));
3281 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3283 if (output
->max_size
!= (uint64_t) -1ULL) {
3284 tmp_output
.max_size
= output
->max_size
;
3287 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3288 tmp_output
.max_size
);
3289 if (nb_packets_per_stream
< 0) {
3290 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3294 /* Use temporary name. */
3295 if (*output
->name
!= '\0') {
3296 strncpy(tmp_output
.name
, output
->name
,
3297 sizeof(tmp_output
.name
));
3300 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3302 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3303 session
, wait
, nb_packets_per_stream
);
3304 if (ret
!= LTTNG_OK
) {
3308 snapshot_success
= 1;
3314 if (session
->ust_session
) {
3315 struct ltt_ust_session
*usess
= session
->ust_session
;
3317 if (use_tmp_output
) {
3318 int64_t nb_packets_per_stream
;
3320 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3321 tmp_output
.max_size
);
3322 if (nb_packets_per_stream
< 0) {
3323 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3326 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3327 wait
, nb_packets_per_stream
);
3328 if (ret
!= LTTNG_OK
) {
3331 snapshot_success
= 1;
3333 struct snapshot_output
*sout
;
3334 struct lttng_ht_iter iter
;
3337 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3338 &iter
.iter
, sout
, node
.node
) {
3339 int64_t nb_packets_per_stream
;
3342 * Make a local copy of the output and assign the possible
3343 * temporary value given by the caller.
3345 memset(&tmp_output
, 0, sizeof(tmp_output
));
3346 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3348 if (output
->max_size
!= (uint64_t) -1ULL) {
3349 tmp_output
.max_size
= output
->max_size
;
3352 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3353 tmp_output
.max_size
);
3354 if (nb_packets_per_stream
< 0) {
3355 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3360 /* Use temporary name. */
3361 if (*output
->name
!= '\0') {
3362 strncpy(tmp_output
.name
, output
->name
,
3363 sizeof(tmp_output
.name
));
3366 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3368 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3369 wait
, nb_packets_per_stream
);
3370 if (ret
!= LTTNG_OK
) {
3374 snapshot_success
= 1;
3380 if (snapshot_success
) {
3381 session
->snapshot
.nb_snapshot
++;
3383 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3391 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3393 int cmd_set_session_shm_path(struct ltt_session
*session
,
3394 const char *shm_path
)
3400 * Can only set shm path before session is started.
3402 if (session
->has_been_started
) {
3403 return LTTNG_ERR_SESSION_STARTED
;
3406 strncpy(session
->shm_path
, shm_path
,
3407 sizeof(session
->shm_path
));
3408 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3414 * Init command subsystem.
3419 * Set network sequence index to 1 for streams to match a relayd
3420 * socket on the consumer side.
3422 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3423 relayd_net_seq_idx
= 1;
3424 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3426 DBG("Command subsystem initialized");