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 * Called with session lock held.
932 int cmd_track_pid(struct ltt_session
*session
, int domain
, int pid
)
939 case LTTNG_DOMAIN_KERNEL
:
941 struct ltt_kernel_session
*ksess
;
943 ksess
= session
->kernel_session
;
945 ret
= kernel_track_pid(ksess
, pid
);
946 if (ret
!= LTTNG_OK
) {
950 kernel_wait_quiescent(kernel_tracer_fd
);
953 case LTTNG_DOMAIN_UST
:
955 struct ltt_ust_session
*usess
;
957 usess
= session
->ust_session
;
959 ret
= trace_ust_track_pid(usess
, pid
);
960 if (ret
!= LTTNG_OK
) {
966 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
978 * Command LTTNG_UNTRACK_PID processed by the client thread.
980 * Called with session lock held.
982 int cmd_untrack_pid(struct ltt_session
*session
, int domain
, int pid
)
989 case LTTNG_DOMAIN_KERNEL
:
991 struct ltt_kernel_session
*ksess
;
993 ksess
= session
->kernel_session
;
995 ret
= kernel_untrack_pid(ksess
, pid
);
996 if (ret
!= LTTNG_OK
) {
1000 kernel_wait_quiescent(kernel_tracer_fd
);
1003 case LTTNG_DOMAIN_UST
:
1005 struct ltt_ust_session
*usess
;
1007 usess
= session
->ust_session
;
1009 ret
= trace_ust_untrack_pid(usess
, pid
);
1010 if (ret
!= LTTNG_OK
) {
1016 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1028 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1030 * The wpipe arguments is used as a notifier for the kernel thread.
1032 int cmd_enable_channel(struct ltt_session
*session
,
1033 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1036 struct ltt_ust_session
*usess
= session
->ust_session
;
1037 struct lttng_ht
*chan_ht
;
1044 len
= strnlen(attr
->name
, sizeof(attr
->name
));
1046 /* Validate channel name */
1047 if (attr
->name
[0] == '.' ||
1048 memchr(attr
->name
, '/', len
) != NULL
) {
1049 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1053 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1058 * Don't try to enable a channel if the session has been started at
1059 * some point in time before. The tracer does not allow it.
1061 if (session
->has_been_started
) {
1062 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1067 * If the session is a live session, remove the switch timer, the
1068 * live timer does the same thing but sends also synchronisation
1069 * beacons for inactive streams.
1071 if (session
->live_timer
> 0) {
1072 attr
->attr
.live_timer_interval
= session
->live_timer
;
1073 attr
->attr
.switch_timer_interval
= 0;
1077 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1078 * mode and with less than 2 subbuf so block it right away and send back an
1079 * invalid attribute error.
1081 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
1082 ret
= LTTNG_ERR_INVALID
;
1086 switch (domain
->type
) {
1087 case LTTNG_DOMAIN_KERNEL
:
1089 struct ltt_kernel_channel
*kchan
;
1091 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1092 session
->kernel_session
);
1093 if (kchan
== NULL
) {
1094 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1095 if (attr
->name
[0] != '\0') {
1096 session
->kernel_session
->has_non_default_channel
= 1;
1099 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1102 if (ret
!= LTTNG_OK
) {
1106 kernel_wait_quiescent(kernel_tracer_fd
);
1109 case LTTNG_DOMAIN_UST
:
1111 struct ltt_ust_channel
*uchan
;
1113 chan_ht
= usess
->domain_global
.channels
;
1115 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1116 if (uchan
== NULL
) {
1117 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1118 if (attr
->name
[0] != '\0') {
1119 usess
->has_non_default_channel
= 1;
1122 ret
= channel_ust_enable(usess
, uchan
);
1127 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1139 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1141 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1143 struct lttng_event
*event
)
1148 DBG("Disable event command for event \'%s\'", event
->name
);
1150 event_name
= event
->name
;
1152 /* Error out on unhandled search criteria */
1153 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1154 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1155 return LTTNG_ERR_UNK
;
1157 /* Special handling for kernel domain all events. */
1158 if (domain
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event_name
, "*")) {
1159 return disable_kevent_all(session
, domain
, channel_name
, event
);
1165 case LTTNG_DOMAIN_KERNEL
:
1167 struct ltt_kernel_channel
*kchan
;
1168 struct ltt_kernel_session
*ksess
;
1170 ksess
= session
->kernel_session
;
1173 * If a non-default channel has been created in the
1174 * session, explicitely require that -c chan_name needs
1177 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1178 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1182 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1183 if (kchan
== NULL
) {
1184 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1188 switch (event
->type
) {
1189 case LTTNG_EVENT_ALL
:
1190 case LTTNG_EVENT_TRACEPOINT
:
1191 ret
= event_kernel_disable_tracepoint(kchan
, event_name
);
1192 if (ret
!= LTTNG_OK
) {
1196 case LTTNG_EVENT_SYSCALL
:
1197 ret
= event_kernel_disable_syscall(kchan
, event_name
);
1198 if (ret
!= LTTNG_OK
) {
1203 ret
= LTTNG_ERR_UNK
;
1207 kernel_wait_quiescent(kernel_tracer_fd
);
1210 case LTTNG_DOMAIN_UST
:
1212 struct ltt_ust_channel
*uchan
;
1213 struct ltt_ust_session
*usess
;
1215 usess
= session
->ust_session
;
1218 * If a non-default channel has been created in the
1219 * session, explicitely require that -c chan_name needs
1222 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1223 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1227 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1229 if (uchan
== NULL
) {
1230 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1234 switch (event
->type
) {
1235 case LTTNG_EVENT_ALL
:
1236 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1237 if (ret
!= LTTNG_OK
) {
1242 ret
= LTTNG_ERR_UNK
;
1246 DBG3("Disable UST event %s in channel %s completed", event_name
,
1250 case LTTNG_DOMAIN_LOG4J
:
1251 case LTTNG_DOMAIN_JUL
:
1252 case LTTNG_DOMAIN_PYTHON
:
1255 struct ltt_ust_session
*usess
= session
->ust_session
;
1259 switch (event
->type
) {
1260 case LTTNG_EVENT_ALL
:
1263 ret
= LTTNG_ERR_UNK
;
1267 agt
= trace_ust_find_agent(usess
, domain
);
1269 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1272 /* The wild card * means that everything should be disabled. */
1273 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1274 ret
= event_agent_disable_all(usess
, agt
);
1276 ret
= event_agent_disable(usess
, agt
, event_name
);
1278 if (ret
!= LTTNG_OK
) {
1285 ret
= LTTNG_ERR_UND
;
1297 * Command LTTNG_DISABLE_EVENT for event "*" processed by the client thread.
1300 int disable_kevent_all(struct ltt_session
*session
, int domain
,
1302 struct lttng_event
*event
)
1309 case LTTNG_DOMAIN_KERNEL
:
1311 struct ltt_kernel_session
*ksess
;
1312 struct ltt_kernel_channel
*kchan
;
1314 ksess
= session
->kernel_session
;
1317 * If a non-default channel has been created in the
1318 * session, explicitely require that -c chan_name needs
1321 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1322 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1326 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1327 if (kchan
== NULL
) {
1328 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1332 switch (event
->type
) {
1333 case LTTNG_EVENT_ALL
:
1334 ret
= event_kernel_disable_all(kchan
);
1335 if (ret
!= LTTNG_OK
) {
1339 case LTTNG_EVENT_SYSCALL
:
1340 ret
= event_kernel_disable_syscall(kchan
, "");
1341 if (ret
!= LTTNG_OK
) {
1346 ret
= LTTNG_ERR_UNK
;
1350 kernel_wait_quiescent(kernel_tracer_fd
);
1354 ret
= LTTNG_ERR_UND
;
1366 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1368 int cmd_add_context(struct ltt_session
*session
, int domain
,
1369 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1371 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1374 case LTTNG_DOMAIN_KERNEL
:
1375 assert(session
->kernel_session
);
1377 if (session
->kernel_session
->channel_count
== 0) {
1378 /* Create default channel */
1379 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1380 if (ret
!= LTTNG_OK
) {
1383 chan_kern_created
= 1;
1385 /* Add kernel context to kernel tracer */
1386 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1387 if (ret
!= LTTNG_OK
) {
1391 case LTTNG_DOMAIN_UST
:
1393 struct ltt_ust_session
*usess
= session
->ust_session
;
1394 unsigned int chan_count
;
1398 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1399 if (chan_count
== 0) {
1400 struct lttng_channel
*attr
;
1401 /* Create default channel */
1402 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1404 ret
= LTTNG_ERR_FATAL
;
1408 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1409 if (ret
!= LTTNG_OK
) {
1414 chan_ust_created
= 1;
1417 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1418 if (ret
!= LTTNG_OK
) {
1424 ret
= LTTNG_ERR_UND
;
1431 if (chan_kern_created
) {
1432 struct ltt_kernel_channel
*kchan
=
1433 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1434 session
->kernel_session
);
1435 /* Created previously, this should NOT fail. */
1437 kernel_destroy_channel(kchan
);
1440 if (chan_ust_created
) {
1441 struct ltt_ust_channel
*uchan
=
1442 trace_ust_find_channel_by_name(
1443 session
->ust_session
->domain_global
.channels
,
1444 DEFAULT_CHANNEL_NAME
);
1445 /* Created previously, this should NOT fail. */
1447 /* Remove from the channel list of the session. */
1448 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1450 trace_ust_destroy_channel(uchan
);
1455 static int validate_event_name(const char *name
)
1458 const char *c
= name
;
1459 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1462 * Make sure that unescaped wildcards are only used as the last
1463 * character of the event name.
1465 while (c
< event_name_end
) {
1473 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1474 /* Wildcard is not the last character */
1475 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1488 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1489 * We own filter, exclusion, and filter_expression.
1491 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1492 char *channel_name
, struct lttng_event
*event
,
1493 char *filter_expression
,
1494 struct lttng_filter_bytecode
*filter
,
1495 struct lttng_event_exclusion
*exclusion
,
1498 int ret
, channel_created
= 0;
1499 struct lttng_channel
*attr
;
1503 assert(channel_name
);
1505 DBG("Enable event command for event \'%s\'", event
->name
);
1507 /* Special handling for kernel domain all events. */
1508 if (domain
->type
== LTTNG_DOMAIN_KERNEL
&& !strcmp(event
->name
, "*")) {
1509 return enable_kevent_all(session
, domain
, channel_name
, event
,
1510 filter_expression
, filter
, wpipe
);
1513 ret
= validate_event_name(event
->name
);
1520 switch (domain
->type
) {
1521 case LTTNG_DOMAIN_KERNEL
:
1523 struct ltt_kernel_channel
*kchan
;
1526 * If a non-default channel has been created in the
1527 * session, explicitely require that -c chan_name needs
1530 if (session
->kernel_session
->has_non_default_channel
1531 && channel_name
[0] == '\0') {
1532 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1536 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1537 session
->kernel_session
);
1538 if (kchan
== NULL
) {
1539 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1540 LTTNG_BUFFER_GLOBAL
);
1542 ret
= LTTNG_ERR_FATAL
;
1545 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1547 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1548 if (ret
!= LTTNG_OK
) {
1554 channel_created
= 1;
1557 /* Get the newly created kernel channel pointer */
1558 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1559 session
->kernel_session
);
1560 if (kchan
== NULL
) {
1561 /* This sould not happen... */
1562 ret
= LTTNG_ERR_FATAL
;
1566 switch (event
->type
) {
1567 case LTTNG_EVENT_ALL
:
1568 case LTTNG_EVENT_PROBE
:
1569 case LTTNG_EVENT_FUNCTION
:
1570 case LTTNG_EVENT_FUNCTION_ENTRY
:
1571 case LTTNG_EVENT_TRACEPOINT
:
1572 ret
= event_kernel_enable_tracepoint(kchan
, event
);
1573 if (ret
!= LTTNG_OK
) {
1574 if (channel_created
) {
1575 /* Let's not leak a useless channel. */
1576 kernel_destroy_channel(kchan
);
1581 case LTTNG_EVENT_SYSCALL
:
1582 ret
= event_kernel_enable_syscall(kchan
, event
->name
);
1583 if (ret
!= LTTNG_OK
) {
1588 ret
= LTTNG_ERR_UNK
;
1592 kernel_wait_quiescent(kernel_tracer_fd
);
1595 case LTTNG_DOMAIN_UST
:
1597 struct ltt_ust_channel
*uchan
;
1598 struct ltt_ust_session
*usess
= session
->ust_session
;
1603 * If a non-default channel has been created in the
1604 * session, explicitely require that -c chan_name needs
1607 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1608 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1612 /* Get channel from global UST domain */
1613 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1615 if (uchan
== NULL
) {
1616 /* Create default channel */
1617 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1618 usess
->buffer_type
);
1620 ret
= LTTNG_ERR_FATAL
;
1623 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1625 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1626 if (ret
!= LTTNG_OK
) {
1632 /* Get the newly created channel reference back */
1633 uchan
= trace_ust_find_channel_by_name(
1634 usess
->domain_global
.channels
, channel_name
);
1638 /* At this point, the session and channel exist on the tracer */
1639 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1640 filter_expression
, filter
, exclusion
);
1641 /* We have passed ownership */
1642 filter_expression
= NULL
;
1645 if (ret
!= LTTNG_OK
) {
1650 case LTTNG_DOMAIN_LOG4J
:
1651 case LTTNG_DOMAIN_JUL
:
1652 case LTTNG_DOMAIN_PYTHON
:
1654 const char *default_event_name
, *default_chan_name
;
1656 struct lttng_event uevent
;
1657 struct lttng_domain tmp_dom
;
1658 struct ltt_ust_session
*usess
= session
->ust_session
;
1662 agt
= trace_ust_find_agent(usess
, domain
->type
);
1664 agt
= agent_create(domain
->type
);
1666 ret
= -LTTNG_ERR_NOMEM
;
1669 agent_add(agt
, usess
->agents
);
1672 /* Create the default tracepoint. */
1673 memset(&uevent
, 0, sizeof(uevent
));
1674 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1675 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1676 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1677 if (!default_event_name
) {
1678 ret
= -LTTNG_ERR_FATAL
;
1681 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1682 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1685 * The domain type is changed because we are about to enable the
1686 * default channel and event for the JUL domain that are hardcoded.
1687 * This happens in the UST domain.
1689 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1690 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1692 switch (domain
->type
) {
1693 case LTTNG_DOMAIN_LOG4J
:
1694 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1696 case LTTNG_DOMAIN_JUL
:
1697 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1699 case LTTNG_DOMAIN_PYTHON
:
1700 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
1703 /* The switch/case we are in should avoid this else big problem */
1708 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1711 filter_copy
= zmalloc(
1712 sizeof(struct lttng_filter_bytecode
)
1718 memcpy(filter_copy
, filter
,
1719 sizeof(struct lttng_filter_bytecode
)
1723 ret
= cmd_enable_event(session
, &tmp_dom
,
1724 (char *) default_chan_name
,
1725 &uevent
, filter_expression
, filter_copy
,
1727 /* We have passed ownership */
1728 filter_expression
= NULL
;
1731 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1735 /* The wild card * means that everything should be enabled. */
1736 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1737 ret
= event_agent_enable_all(usess
, agt
, event
, filter
);
1740 ret
= event_agent_enable(usess
, agt
, event
, filter
);
1743 if (ret
!= LTTNG_OK
) {
1750 ret
= LTTNG_ERR_UND
;
1757 free(filter_expression
);
1765 * Command LTTNG_ENABLE_EVENT for event "*" processed by the client thread.
1768 int enable_kevent_all(struct ltt_session
*session
,
1769 struct lttng_domain
*domain
, char *channel_name
,
1770 struct lttng_event
*event
,
1771 char *filter_expression
,
1772 struct lttng_filter_bytecode
*filter
, int wpipe
)
1775 struct lttng_channel
*attr
;
1778 assert(channel_name
);
1782 switch (domain
->type
) {
1783 case LTTNG_DOMAIN_KERNEL
:
1785 struct ltt_kernel_channel
*kchan
;
1787 assert(session
->kernel_session
);
1790 * If a non-default channel has been created in the
1791 * session, explicitely require that -c chan_name needs
1794 if (session
->kernel_session
->has_non_default_channel
1795 && channel_name
[0] == '\0') {
1796 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1800 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1801 session
->kernel_session
);
1802 if (kchan
== NULL
) {
1803 /* Create default channel */
1804 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1805 LTTNG_BUFFER_GLOBAL
);
1807 ret
= LTTNG_ERR_FATAL
;
1810 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1812 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1813 if (ret
!= LTTNG_OK
) {
1819 /* Get the newly created kernel channel pointer */
1820 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1821 session
->kernel_session
);
1825 switch (event
->type
) {
1826 case LTTNG_EVENT_SYSCALL
:
1827 ret
= event_kernel_enable_syscall(kchan
, "");
1828 if (ret
!= LTTNG_OK
) {
1832 case LTTNG_EVENT_TRACEPOINT
:
1834 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1835 * events already registered to the channel.
1837 ret
= event_kernel_enable_all_tracepoints(kchan
, kernel_tracer_fd
);
1839 case LTTNG_EVENT_ALL
:
1840 /* Enable syscalls and tracepoints */
1841 ret
= event_kernel_enable_all(kchan
, kernel_tracer_fd
);
1844 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1848 /* Manage return value */
1849 if (ret
!= LTTNG_OK
) {
1851 * On error, cmd_enable_channel call will take care of destroying
1852 * the created channel if it was needed.
1857 kernel_wait_quiescent(kernel_tracer_fd
);
1861 ret
= LTTNG_ERR_UND
;
1874 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1876 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1879 ssize_t nb_events
= 0;
1882 case LTTNG_DOMAIN_KERNEL
:
1883 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1884 if (nb_events
< 0) {
1885 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1889 case LTTNG_DOMAIN_UST
:
1890 nb_events
= ust_app_list_events(events
);
1891 if (nb_events
< 0) {
1892 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1896 case LTTNG_DOMAIN_LOG4J
:
1897 case LTTNG_DOMAIN_JUL
:
1898 case LTTNG_DOMAIN_PYTHON
:
1899 nb_events
= agent_list_events(events
, domain
);
1900 if (nb_events
< 0) {
1901 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1906 ret
= LTTNG_ERR_UND
;
1913 /* Return negative value to differentiate return code */
1918 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1920 ssize_t
cmd_list_tracepoint_fields(int domain
,
1921 struct lttng_event_field
**fields
)
1924 ssize_t nb_fields
= 0;
1927 case LTTNG_DOMAIN_UST
:
1928 nb_fields
= ust_app_list_event_fields(fields
);
1929 if (nb_fields
< 0) {
1930 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1934 case LTTNG_DOMAIN_KERNEL
:
1935 default: /* fall-through */
1936 ret
= LTTNG_ERR_UND
;
1943 /* Return negative value to differentiate return code */
1947 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1949 return syscall_table_list(events
);
1953 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
1955 * Called with session lock held.
1957 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
1958 int domain
, int32_t **pids
)
1961 ssize_t nr_pids
= 0;
1964 case LTTNG_DOMAIN_KERNEL
:
1966 struct ltt_kernel_session
*ksess
;
1968 ksess
= session
->kernel_session
;
1969 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
1971 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1976 case LTTNG_DOMAIN_UST
:
1978 struct ltt_ust_session
*usess
;
1980 usess
= session
->ust_session
;
1981 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
1983 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1988 case LTTNG_DOMAIN_LOG4J
:
1989 case LTTNG_DOMAIN_JUL
:
1990 case LTTNG_DOMAIN_PYTHON
:
1992 ret
= LTTNG_ERR_UND
;
1999 /* Return negative value to differentiate return code */
2004 * Command LTTNG_START_TRACE processed by the client thread.
2006 * Called with session mutex held.
2008 int cmd_start_trace(struct ltt_session
*session
)
2011 unsigned long nb_chan
= 0;
2012 struct ltt_kernel_session
*ksession
;
2013 struct ltt_ust_session
*usess
;
2017 /* Ease our life a bit ;) */
2018 ksession
= session
->kernel_session
;
2019 usess
= session
->ust_session
;
2021 /* Is the session already started? */
2022 if (session
->active
) {
2023 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2028 * Starting a session without channel is useless since after that it's not
2029 * possible to enable channel thus inform the client.
2031 if (usess
&& usess
->domain_global
.channels
) {
2033 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
2037 nb_chan
+= ksession
->channel_count
;
2040 ret
= LTTNG_ERR_NO_CHANNEL
;
2044 /* Kernel tracing */
2045 if (ksession
!= NULL
) {
2046 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2047 if (ret
!= LTTNG_OK
) {
2052 /* Flag session that trace should start automatically */
2055 * Even though the start trace might fail, flag this session active so
2056 * other application coming in are started by default.
2060 ret
= ust_app_start_trace_all(usess
);
2062 ret
= LTTNG_ERR_UST_START_FAIL
;
2067 /* Flag this after a successful start. */
2068 session
->has_been_started
= 1;
2069 session
->active
= 1;
2078 * Command LTTNG_STOP_TRACE processed by the client thread.
2080 int cmd_stop_trace(struct ltt_session
*session
)
2083 struct ltt_kernel_channel
*kchan
;
2084 struct ltt_kernel_session
*ksession
;
2085 struct ltt_ust_session
*usess
;
2090 ksession
= session
->kernel_session
;
2091 usess
= session
->ust_session
;
2093 /* Session is not active. Skip everythong and inform the client. */
2094 if (!session
->active
) {
2095 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2100 if (ksession
&& ksession
->active
) {
2101 DBG("Stop kernel tracing");
2103 /* Flush metadata if exist */
2104 if (ksession
->metadata_stream_fd
>= 0) {
2105 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2107 ERR("Kernel metadata flush failed");
2111 /* Flush all buffers before stopping */
2112 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2113 ret
= kernel_flush_buffer(kchan
);
2115 ERR("Kernel flush buffer error");
2119 ret
= kernel_stop_session(ksession
);
2121 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2125 kernel_wait_quiescent(kernel_tracer_fd
);
2127 ksession
->active
= 0;
2130 if (usess
&& usess
->active
) {
2132 * Even though the stop trace might fail, flag this session inactive so
2133 * other application coming in are not started by default.
2137 ret
= ust_app_stop_trace_all(usess
);
2139 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2144 /* Flag inactive after a successful stop. */
2145 session
->active
= 0;
2153 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2155 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2156 struct lttng_uri
*uris
)
2159 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2160 struct ltt_ust_session
*usess
= session
->ust_session
;
2166 /* Can't set consumer URI if the session is active. */
2167 if (session
->active
) {
2168 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2172 /* Set the "global" consumer URIs */
2173 for (i
= 0; i
< nb_uri
; i
++) {
2174 ret
= add_uri_to_consumer(session
->consumer
,
2175 &uris
[i
], 0, session
->name
);
2176 if (ret
!= LTTNG_OK
) {
2181 /* Set UST session URIs */
2182 if (session
->ust_session
) {
2183 for (i
= 0; i
< nb_uri
; i
++) {
2184 ret
= add_uri_to_consumer(
2185 session
->ust_session
->consumer
,
2186 &uris
[i
], LTTNG_DOMAIN_UST
,
2188 if (ret
!= LTTNG_OK
) {
2194 /* Set kernel session URIs */
2195 if (session
->kernel_session
) {
2196 for (i
= 0; i
< nb_uri
; i
++) {
2197 ret
= add_uri_to_consumer(
2198 session
->kernel_session
->consumer
,
2199 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2201 if (ret
!= LTTNG_OK
) {
2208 * Make sure to set the session in output mode after we set URI since a
2209 * session can be created without URL (thus flagged in no output mode).
2211 session
->output_traces
= 1;
2213 ksess
->output_traces
= 1;
2217 usess
->output_traces
= 1;
2228 * Command LTTNG_CREATE_SESSION processed by the client thread.
2230 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2231 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2234 struct ltt_session
*session
;
2240 * Verify if the session already exist
2242 * XXX: There is no need for the session lock list here since the caller
2243 * (process_client_msg) is holding it. We might want to change that so a
2244 * single command does not lock the entire session list.
2246 session
= session_find_by_name(name
);
2247 if (session
!= NULL
) {
2248 ret
= LTTNG_ERR_EXIST_SESS
;
2252 /* Create tracing session in the registry */
2253 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2254 LTTNG_SOCK_GET_GID_CRED(creds
));
2255 if (ret
!= LTTNG_OK
) {
2260 * Get the newly created session pointer back
2262 * XXX: There is no need for the session lock list here since the caller
2263 * (process_client_msg) is holding it. We might want to change that so a
2264 * single command does not lock the entire session list.
2266 session
= session_find_by_name(name
);
2269 session
->live_timer
= live_timer
;
2270 /* Create default consumer output for the session not yet created. */
2271 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2272 if (session
->consumer
== NULL
) {
2273 ret
= LTTNG_ERR_FATAL
;
2274 goto consumer_error
;
2278 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2279 if (ret
!= LTTNG_OK
) {
2280 goto consumer_error
;
2282 session
->output_traces
= 1;
2284 session
->output_traces
= 0;
2285 DBG2("Session %s created with no output", session
->name
);
2288 session
->consumer
->enabled
= 1;
2293 session_destroy(session
);
2300 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2302 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2303 size_t nb_uri
, lttng_sock_cred
*creds
)
2306 struct ltt_session
*session
;
2307 struct snapshot_output
*new_output
= NULL
;
2313 * Create session in no output mode with URIs set to NULL. The uris we've
2314 * received are for a default snapshot output if one.
2316 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2317 if (ret
!= LTTNG_OK
) {
2321 /* Get the newly created session pointer back. This should NEVER fail. */
2322 session
= session_find_by_name(name
);
2325 /* Flag session for snapshot mode. */
2326 session
->snapshot_mode
= 1;
2328 /* Skip snapshot output creation if no URI is given. */
2333 new_output
= snapshot_output_alloc();
2335 ret
= LTTNG_ERR_NOMEM
;
2336 goto error_snapshot_alloc
;
2339 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2340 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2342 if (ret
== -ENOMEM
) {
2343 ret
= LTTNG_ERR_NOMEM
;
2345 ret
= LTTNG_ERR_INVALID
;
2347 goto error_snapshot
;
2351 snapshot_add_output(&session
->snapshot
, new_output
);
2358 snapshot_output_destroy(new_output
);
2359 error_snapshot_alloc
:
2360 session_destroy(session
);
2366 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2368 * Called with session lock held.
2370 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2373 struct ltt_ust_session
*usess
;
2374 struct ltt_kernel_session
*ksess
;
2379 usess
= session
->ust_session
;
2380 ksess
= session
->kernel_session
;
2382 /* Clean kernel session teardown */
2383 kernel_destroy_session(ksess
);
2385 /* UST session teardown */
2387 /* Close any relayd session */
2388 consumer_output_send_destroy_relayd(usess
->consumer
);
2390 /* Destroy every UST application related to this session. */
2391 ret
= ust_app_destroy_trace_all(usess
);
2393 ERR("Error in ust_app_destroy_trace_all");
2396 /* Clean up the rest. */
2397 trace_ust_destroy_session(usess
);
2401 * Must notify the kernel thread here to update it's poll set in order to
2402 * remove the channel(s)' fd just destroyed.
2404 ret
= notify_thread_pipe(wpipe
);
2406 PERROR("write kernel poll pipe");
2409 ret
= session_destroy(session
);
2415 * Command LTTNG_CALIBRATE processed by the client thread.
2417 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2422 case LTTNG_DOMAIN_KERNEL
:
2424 struct lttng_kernel_calibrate kcalibrate
;
2426 switch (calibrate
->type
) {
2427 case LTTNG_CALIBRATE_FUNCTION
:
2429 /* Default and only possible calibrate option. */
2430 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2434 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2436 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2441 case LTTNG_DOMAIN_UST
:
2443 struct lttng_ust_calibrate ucalibrate
;
2445 switch (calibrate
->type
) {
2446 case LTTNG_CALIBRATE_FUNCTION
:
2448 /* Default and only possible calibrate option. */
2449 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2453 ret
= ust_app_calibrate_glb(&ucalibrate
);
2455 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2461 ret
= LTTNG_ERR_UND
;
2472 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2474 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2475 const char *sock_path
, struct consumer_data
*cdata
)
2478 struct consumer_socket
*socket
= NULL
;
2485 case LTTNG_DOMAIN_KERNEL
:
2487 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2491 /* Can't register a consumer if there is already one */
2492 if (ksess
->consumer_fds_sent
!= 0) {
2493 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2497 sock
= lttcomm_connect_unix_sock(sock_path
);
2499 ret
= LTTNG_ERR_CONNECT_FAIL
;
2502 cdata
->cmd_sock
= sock
;
2504 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2505 if (socket
== NULL
) {
2508 PERROR("close register consumer");
2510 cdata
->cmd_sock
= -1;
2511 ret
= LTTNG_ERR_FATAL
;
2515 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2516 if (socket
->lock
== NULL
) {
2517 PERROR("zmalloc pthread mutex");
2518 ret
= LTTNG_ERR_FATAL
;
2521 pthread_mutex_init(socket
->lock
, NULL
);
2522 socket
->registered
= 1;
2525 consumer_add_socket(socket
, ksess
->consumer
);
2528 pthread_mutex_lock(&cdata
->pid_mutex
);
2530 pthread_mutex_unlock(&cdata
->pid_mutex
);
2535 /* TODO: Userspace tracing */
2536 ret
= LTTNG_ERR_UND
;
2544 consumer_destroy_socket(socket
);
2550 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2552 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2553 struct lttng_domain
**domains
)
2558 struct lttng_ht_iter iter
;
2560 if (session
->kernel_session
!= NULL
) {
2561 DBG3("Listing domains found kernel domain");
2565 if (session
->ust_session
!= NULL
) {
2566 DBG3("Listing domains found UST global domain");
2570 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2572 if (agt
->being_used
) {
2583 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2584 if (*domains
== NULL
) {
2585 ret
= LTTNG_ERR_FATAL
;
2589 if (session
->kernel_session
!= NULL
) {
2590 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2594 if (session
->ust_session
!= NULL
) {
2595 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2596 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2600 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2602 if (agt
->being_used
) {
2603 (*domains
)[index
].type
= agt
->domain
;
2604 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2614 /* Return negative value to differentiate return code */
2620 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2622 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2623 struct lttng_channel
**channels
)
2626 ssize_t nb_chan
= 0;
2629 case LTTNG_DOMAIN_KERNEL
:
2630 if (session
->kernel_session
!= NULL
) {
2631 nb_chan
= session
->kernel_session
->channel_count
;
2633 DBG3("Number of kernel channels %zd", nb_chan
);
2635 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2638 case LTTNG_DOMAIN_UST
:
2639 if (session
->ust_session
!= NULL
) {
2641 nb_chan
= lttng_ht_get_count(
2642 session
->ust_session
->domain_global
.channels
);
2645 DBG3("Number of UST global channels %zd", nb_chan
);
2647 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2652 ret
= LTTNG_ERR_UND
;
2657 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2658 if (*channels
== NULL
) {
2659 ret
= LTTNG_ERR_FATAL
;
2663 list_lttng_channels(domain
, session
, *channels
);
2669 /* Return negative value to differentiate return code */
2674 * Command LTTNG_LIST_EVENTS processed by the client thread.
2676 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2677 char *channel_name
, struct lttng_event
**events
)
2680 ssize_t nb_event
= 0;
2683 case LTTNG_DOMAIN_KERNEL
:
2684 if (session
->kernel_session
!= NULL
) {
2685 nb_event
= list_lttng_kernel_events(channel_name
,
2686 session
->kernel_session
, events
);
2689 case LTTNG_DOMAIN_UST
:
2691 if (session
->ust_session
!= NULL
) {
2692 nb_event
= list_lttng_ust_global_events(channel_name
,
2693 &session
->ust_session
->domain_global
, events
);
2697 case LTTNG_DOMAIN_LOG4J
:
2698 case LTTNG_DOMAIN_JUL
:
2699 case LTTNG_DOMAIN_PYTHON
:
2700 if (session
->ust_session
) {
2701 struct lttng_ht_iter iter
;
2705 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2706 &iter
.iter
, agt
, node
.node
) {
2707 nb_event
= list_lttng_agent_events(agt
, events
);
2713 ret
= LTTNG_ERR_UND
;
2720 /* Return negative value to differentiate return code */
2725 * Using the session list, filled a lttng_session array to send back to the
2726 * client for session listing.
2728 * The session list lock MUST be acquired before calling this function. Use
2729 * session_lock_list() and session_unlock_list().
2731 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2736 struct ltt_session
*session
;
2737 struct ltt_session_list
*list
= session_get_list();
2739 DBG("Getting all available session for UID %d GID %d",
2742 * Iterate over session list and append data after the control struct in
2745 cds_list_for_each_entry(session
, &list
->head
, list
) {
2747 * Only list the sessions the user can control.
2749 if (!session_access_ok(session
, uid
, gid
)) {
2753 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2754 struct ltt_ust_session
*usess
= session
->ust_session
;
2756 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2757 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2758 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2759 ret
= build_network_session_path(sessions
[i
].path
,
2760 sizeof(sessions
[i
].path
), session
);
2762 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2763 session
->consumer
->dst
.trace_path
);
2766 PERROR("snprintf session path");
2770 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2771 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2772 sessions
[i
].enabled
= session
->active
;
2773 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2774 sessions
[i
].live_timer_interval
= session
->live_timer
;
2780 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2781 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2783 int cmd_data_pending(struct ltt_session
*session
)
2786 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2787 struct ltt_ust_session
*usess
= session
->ust_session
;
2791 /* Session MUST be stopped to ask for data availability. */
2792 if (session
->active
) {
2793 ret
= LTTNG_ERR_SESSION_STARTED
;
2797 * If stopped, just make sure we've started before else the above call
2798 * will always send that there is data pending.
2800 * The consumer assumes that when the data pending command is received,
2801 * the trace has been started before or else no output data is written
2802 * by the streams which is a condition for data pending. So, this is
2803 * *VERY* important that we don't ask the consumer before a start
2806 if (!session
->has_been_started
) {
2812 if (ksess
&& ksess
->consumer
) {
2813 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2815 /* Data is still being extracted for the kernel. */
2820 if (usess
&& usess
->consumer
) {
2821 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2823 /* Data is still being extracted for the kernel. */
2828 /* Data is ready to be read by a viewer */
2836 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2838 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2840 int cmd_snapshot_add_output(struct ltt_session
*session
,
2841 struct lttng_snapshot_output
*output
, uint32_t *id
)
2844 struct snapshot_output
*new_output
;
2849 DBG("Cmd snapshot add output for session %s", session
->name
);
2852 * Permission denied to create an output if the session is not
2853 * set in no output mode.
2855 if (session
->output_traces
) {
2856 ret
= LTTNG_ERR_EPERM
;
2860 /* Only one output is allowed until we have the "tee" feature. */
2861 if (session
->snapshot
.nb_output
== 1) {
2862 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2866 new_output
= snapshot_output_alloc();
2868 ret
= LTTNG_ERR_NOMEM
;
2872 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2873 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2874 &session
->snapshot
);
2876 if (ret
== -ENOMEM
) {
2877 ret
= LTTNG_ERR_NOMEM
;
2879 ret
= LTTNG_ERR_INVALID
;
2885 snapshot_add_output(&session
->snapshot
, new_output
);
2887 *id
= new_output
->id
;
2894 snapshot_output_destroy(new_output
);
2900 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2902 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2904 int cmd_snapshot_del_output(struct ltt_session
*session
,
2905 struct lttng_snapshot_output
*output
)
2908 struct snapshot_output
*sout
= NULL
;
2916 * Permission denied to create an output if the session is not
2917 * set in no output mode.
2919 if (session
->output_traces
) {
2920 ret
= LTTNG_ERR_EPERM
;
2925 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2927 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2928 } else if (*output
->name
!= '\0') {
2929 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2931 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2934 ret
= LTTNG_ERR_INVALID
;
2938 snapshot_delete_output(&session
->snapshot
, sout
);
2939 snapshot_output_destroy(sout
);
2948 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2950 * If no output is available, outputs is untouched and 0 is returned.
2952 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2954 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2955 struct lttng_snapshot_output
**outputs
)
2958 struct lttng_snapshot_output
*list
= NULL
;
2959 struct lttng_ht_iter iter
;
2960 struct snapshot_output
*output
;
2965 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2968 * Permission denied to create an output if the session is not
2969 * set in no output mode.
2971 if (session
->output_traces
) {
2972 ret
= -LTTNG_ERR_EPERM
;
2976 if (session
->snapshot
.nb_output
== 0) {
2981 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2983 ret
= -LTTNG_ERR_NOMEM
;
2987 /* Copy list from session to the new list object. */
2989 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2990 output
, node
.node
) {
2991 assert(output
->consumer
);
2992 list
[idx
].id
= output
->id
;
2993 list
[idx
].max_size
= output
->max_size
;
2994 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2995 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2996 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2997 sizeof(list
[idx
].ctrl_url
));
3000 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
3001 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
3003 ret
= -LTTNG_ERR_NOMEM
;
3008 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
3009 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
3011 ret
= -LTTNG_ERR_NOMEM
;
3020 ret
= session
->snapshot
.nb_output
;
3028 * Send relayd sockets from snapshot output to consumer. Ignore request if the
3029 * snapshot output is *not* set with a remote destination.
3031 * Return 0 on success or a LTTNG_ERR code.
3033 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3034 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3037 struct lttng_ht_iter iter
;
3038 struct consumer_socket
*socket
;
3041 assert(snap_output
);
3044 DBG2("Set relayd object from snapshot output");
3046 /* Ignore if snapshot consumer output is not network. */
3047 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3052 * For each consumer socket, create and send the relayd object of the
3056 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3057 socket
, node
.node
) {
3058 ret
= send_consumer_relayd_sockets(0, session
->id
,
3059 snap_output
->consumer
, socket
,
3060 session
->name
, session
->hostname
,
3061 session
->live_timer
);
3062 if (ret
!= LTTNG_OK
) {
3074 * Record a kernel snapshot.
3076 * Return LTTNG_OK on success or a LTTNG_ERR code.
3078 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3079 struct snapshot_output
*output
, struct ltt_session
*session
,
3080 int wait
, uint64_t nb_packets_per_stream
)
3088 /* Get the datetime for the snapshot output directory. */
3089 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3090 sizeof(output
->datetime
));
3092 ret
= LTTNG_ERR_INVALID
;
3097 * Copy kernel session sockets so we can communicate with the right
3098 * consumer for the snapshot record command.
3100 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3102 ret
= LTTNG_ERR_NOMEM
;
3106 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3107 if (ret
!= LTTNG_OK
) {
3108 goto error_snapshot
;
3111 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3112 if (ret
!= LTTNG_OK
) {
3113 goto error_snapshot
;
3120 /* Clean up copied sockets so this output can use some other later on. */
3121 consumer_destroy_output_sockets(output
->consumer
);
3128 * Record a UST snapshot.
3130 * Return 0 on success or a LTTNG_ERR error code.
3132 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3133 struct snapshot_output
*output
, struct ltt_session
*session
,
3134 int wait
, uint64_t nb_packets_per_stream
)
3142 /* Get the datetime for the snapshot output directory. */
3143 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3144 sizeof(output
->datetime
));
3146 ret
= LTTNG_ERR_INVALID
;
3151 * Copy UST session sockets so we can communicate with the right
3152 * consumer for the snapshot record command.
3154 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3156 ret
= LTTNG_ERR_NOMEM
;
3160 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3161 if (ret
!= LTTNG_OK
) {
3162 goto error_snapshot
;
3165 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3169 ret
= LTTNG_ERR_INVALID
;
3172 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3175 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3178 goto error_snapshot
;
3184 /* Clean up copied sockets so this output can use some other later on. */
3185 consumer_destroy_output_sockets(output
->consumer
);
3191 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3192 uint64_t cur_nr_packets
)
3194 uint64_t tot_size
= 0;
3196 if (session
->kernel_session
) {
3197 struct ltt_kernel_channel
*chan
;
3198 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3200 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3201 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3203 * Don't take channel into account if we
3204 * already grab all its packets.
3208 tot_size
+= chan
->channel
->attr
.subbuf_size
3209 * chan
->stream_count
;
3213 if (session
->ust_session
) {
3214 struct ltt_ust_session
*usess
= session
->ust_session
;
3216 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3224 * Calculate the number of packets we can grab from each stream that
3225 * fits within the overall snapshot max size.
3227 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3228 * the number of packets per stream.
3230 * TODO: this approach is not perfect: we consider the worse case
3231 * (packet filling the sub-buffers) as an upper bound, but we could do
3232 * better if we do this calculation while we actually grab the packet
3233 * content: we would know how much padding we don't actually store into
3236 * This algorithm is currently bounded by the number of packets per
3239 * Since we call this algorithm before actually grabbing the data, it's
3240 * an approximation: for instance, applications could appear/disappear
3241 * in between this call and actually grabbing data.
3244 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3247 uint64_t cur_nb_packets
= 0;
3250 return 0; /* Infinite */
3253 size_left
= max_size
;
3255 uint64_t one_more_packet_tot_size
;
3257 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3259 if (!one_more_packet_tot_size
) {
3260 /* We are already grabbing all packets. */
3263 size_left
-= one_more_packet_tot_size
;
3264 if (size_left
< 0) {
3269 if (!cur_nb_packets
) {
3270 /* Not enough room to grab one packet of each stream, error. */
3273 return cur_nb_packets
;
3277 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3279 * The wait parameter is ignored so this call always wait for the snapshot to
3280 * complete before returning.
3282 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3284 int cmd_snapshot_record(struct ltt_session
*session
,
3285 struct lttng_snapshot_output
*output
, int wait
)
3288 unsigned int use_tmp_output
= 0;
3289 struct snapshot_output tmp_output
;
3290 unsigned int snapshot_success
= 0;
3295 DBG("Cmd snapshot record for session %s", session
->name
);
3298 * Permission denied to create an output if the session is not
3299 * set in no output mode.
3301 if (session
->output_traces
) {
3302 ret
= LTTNG_ERR_EPERM
;
3306 /* The session needs to be started at least once. */
3307 if (!session
->has_been_started
) {
3308 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3312 /* Use temporary output for the session. */
3313 if (*output
->ctrl_url
!= '\0') {
3314 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3315 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3318 if (ret
== -ENOMEM
) {
3319 ret
= LTTNG_ERR_NOMEM
;
3321 ret
= LTTNG_ERR_INVALID
;
3325 /* Use the global session count for the temporary snapshot. */
3326 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3330 if (session
->kernel_session
) {
3331 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3333 if (use_tmp_output
) {
3334 int64_t nb_packets_per_stream
;
3336 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3337 tmp_output
.max_size
);
3338 if (nb_packets_per_stream
< 0) {
3339 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3342 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3343 wait
, nb_packets_per_stream
);
3344 if (ret
!= LTTNG_OK
) {
3347 snapshot_success
= 1;
3349 struct snapshot_output
*sout
;
3350 struct lttng_ht_iter iter
;
3353 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3354 &iter
.iter
, sout
, node
.node
) {
3355 int64_t nb_packets_per_stream
;
3358 * Make a local copy of the output and assign the possible
3359 * temporary value given by the caller.
3361 memset(&tmp_output
, 0, sizeof(tmp_output
));
3362 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3364 if (output
->max_size
!= (uint64_t) -1ULL) {
3365 tmp_output
.max_size
= output
->max_size
;
3368 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3369 tmp_output
.max_size
);
3370 if (nb_packets_per_stream
< 0) {
3371 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3375 /* Use temporary name. */
3376 if (*output
->name
!= '\0') {
3377 strncpy(tmp_output
.name
, output
->name
,
3378 sizeof(tmp_output
.name
));
3381 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3383 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3384 session
, wait
, nb_packets_per_stream
);
3385 if (ret
!= LTTNG_OK
) {
3389 snapshot_success
= 1;
3395 if (session
->ust_session
) {
3396 struct ltt_ust_session
*usess
= session
->ust_session
;
3398 if (use_tmp_output
) {
3399 int64_t nb_packets_per_stream
;
3401 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3402 tmp_output
.max_size
);
3403 if (nb_packets_per_stream
< 0) {
3404 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3407 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3408 wait
, nb_packets_per_stream
);
3409 if (ret
!= LTTNG_OK
) {
3412 snapshot_success
= 1;
3414 struct snapshot_output
*sout
;
3415 struct lttng_ht_iter iter
;
3418 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3419 &iter
.iter
, sout
, node
.node
) {
3420 int64_t nb_packets_per_stream
;
3423 * Make a local copy of the output and assign the possible
3424 * temporary value given by the caller.
3426 memset(&tmp_output
, 0, sizeof(tmp_output
));
3427 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3429 if (output
->max_size
!= (uint64_t) -1ULL) {
3430 tmp_output
.max_size
= output
->max_size
;
3433 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3434 tmp_output
.max_size
);
3435 if (nb_packets_per_stream
< 0) {
3436 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3441 /* Use temporary name. */
3442 if (*output
->name
!= '\0') {
3443 strncpy(tmp_output
.name
, output
->name
,
3444 sizeof(tmp_output
.name
));
3447 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3449 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3450 wait
, nb_packets_per_stream
);
3451 if (ret
!= LTTNG_OK
) {
3455 snapshot_success
= 1;
3461 if (snapshot_success
) {
3462 session
->snapshot
.nb_snapshot
++;
3464 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3472 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3474 int cmd_set_session_shm_path(struct ltt_session
*session
,
3475 const char *shm_path
)
3481 * Can only set shm path before session is started.
3483 if (session
->has_been_started
) {
3484 return LTTNG_ERR_SESSION_STARTED
;
3487 strncpy(session
->shm_path
, shm_path
,
3488 sizeof(session
->shm_path
));
3489 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3495 * Init command subsystem.
3500 * Set network sequence index to 1 for streams to match a relayd
3501 * socket on the consumer side.
3503 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3504 relayd_net_seq_idx
= 1;
3505 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3507 DBG("Command subsystem initialized");