2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <urcu/list.h>
24 #include <urcu/uatomic.h>
26 #include <common/defaults.h>
27 #include <common/common.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
29 #include <common/relayd/relayd.h>
30 #include <common/utils.h>
35 #include "health-sessiond.h"
37 #include "kernel-consumer.h"
38 #include "lttng-sessiond.h"
46 * Used to keep a unique index for each relayd socket created where this value
47 * is associated with streams on the consumer so it can match the right relayd
48 * to send to. It must be accessed with the relayd_net_seq_idx_lock
51 static pthread_mutex_t relayd_net_seq_idx_lock
= PTHREAD_MUTEX_INITIALIZER
;
52 static uint64_t relayd_net_seq_idx
;
55 * Create a session path used by list_lttng_sessions for the case that the
56 * session consumer is on the network.
58 static int build_network_session_path(char *dst
, size_t size
,
59 struct ltt_session
*session
)
61 int ret
, kdata_port
, udata_port
;
62 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
63 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
68 memset(tmp_urls
, 0, sizeof(tmp_urls
));
69 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
71 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
73 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
74 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
75 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
78 if (session
->ust_session
&& session
->ust_session
->consumer
) {
79 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
80 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
83 if (uuri
== NULL
&& kuri
== NULL
) {
84 uri
= &session
->consumer
->dst
.net
.control
;
85 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
86 } else if (kuri
&& uuri
) {
87 ret
= uri_compare(kuri
, uuri
);
91 /* Build uuri URL string */
92 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
99 } else if (kuri
&& uuri
== NULL
) {
101 } else if (uuri
&& kuri
== NULL
) {
105 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
111 * Do we have a UST url set. If yes, this means we have both kernel and UST
114 if (*tmp_uurl
!= '\0') {
115 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
116 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
119 if (kuri
|| (!kuri
&& !uuri
)) {
122 /* No kernel URI, use the UST port. */
125 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
133 * Fill lttng_channel array of all channels.
135 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
136 struct lttng_channel
*channels
)
139 struct ltt_kernel_channel
*kchan
;
141 DBG("Listing channels for session %s", session
->name
);
144 case LTTNG_DOMAIN_KERNEL
:
145 /* Kernel channels */
146 if (session
->kernel_session
!= NULL
) {
147 cds_list_for_each_entry(kchan
,
148 &session
->kernel_session
->channel_list
.head
, list
) {
149 /* Copy lttng_channel struct to array */
150 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
151 channels
[i
].enabled
= kchan
->enabled
;
156 case LTTNG_DOMAIN_UST
:
158 struct lttng_ht_iter iter
;
159 struct ltt_ust_channel
*uchan
;
162 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
163 &iter
.iter
, uchan
, node
.node
) {
164 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
165 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
166 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
167 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
168 channels
[i
].attr
.switch_timer_interval
=
169 uchan
->attr
.switch_timer_interval
;
170 channels
[i
].attr
.read_timer_interval
=
171 uchan
->attr
.read_timer_interval
;
172 channels
[i
].enabled
= uchan
->enabled
;
173 channels
[i
].attr
.tracefile_size
= uchan
->tracefile_size
;
174 channels
[i
].attr
.tracefile_count
= uchan
->tracefile_count
;
175 switch (uchan
->attr
.output
) {
178 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
192 * Create a list of agent domain events.
194 * Return number of events in list on success or else a negative value.
196 static int list_lttng_agent_events(struct agent
*agt
,
197 struct lttng_event
**events
)
200 unsigned int nb_event
= 0;
201 struct agent_event
*event
;
202 struct lttng_event
*tmp_events
;
203 struct lttng_ht_iter iter
;
208 DBG3("Listing agent events");
211 nb_event
= lttng_ht_get_count(agt
->events
);
218 tmp_events
= zmalloc(nb_event
* sizeof(*tmp_events
));
220 PERROR("zmalloc agent events session");
221 ret
= -LTTNG_ERR_FATAL
;
226 cds_lfht_for_each_entry(agt
->events
->ht
, &iter
.iter
, event
, node
.node
) {
227 strncpy(tmp_events
[i
].name
, event
->name
, sizeof(tmp_events
[i
].name
));
228 tmp_events
[i
].name
[sizeof(tmp_events
[i
].name
) - 1] = '\0';
229 tmp_events
[i
].enabled
= event
->enabled
;
230 tmp_events
[i
].loglevel
= event
->loglevel
;
231 tmp_events
[i
].loglevel_type
= event
->loglevel_type
;
236 *events
= tmp_events
;
240 assert(nb_event
== i
);
245 * Create a list of ust global domain events.
247 static int list_lttng_ust_global_events(char *channel_name
,
248 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
251 unsigned int nb_event
= 0;
252 struct lttng_ht_iter iter
;
253 struct lttng_ht_node_str
*node
;
254 struct ltt_ust_channel
*uchan
;
255 struct ltt_ust_event
*uevent
;
256 struct lttng_event
*tmp
;
258 DBG("Listing UST global events for channel %s", channel_name
);
262 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
263 node
= lttng_ht_iter_get_node_str(&iter
);
265 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
269 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
271 nb_event
+= lttng_ht_get_count(uchan
->events
);
278 DBG3("Listing UST global %d events", nb_event
);
280 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
282 ret
= LTTNG_ERR_FATAL
;
286 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
287 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
288 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
289 tmp
[i
].enabled
= uevent
->enabled
;
291 switch (uevent
->attr
.instrumentation
) {
292 case LTTNG_UST_TRACEPOINT
:
293 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
295 case LTTNG_UST_PROBE
:
296 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
298 case LTTNG_UST_FUNCTION
:
299 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
303 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
304 switch (uevent
->attr
.loglevel_type
) {
305 case LTTNG_UST_LOGLEVEL_ALL
:
306 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
308 case LTTNG_UST_LOGLEVEL_RANGE
:
309 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
311 case LTTNG_UST_LOGLEVEL_SINGLE
:
312 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
315 if (uevent
->filter
) {
318 if (uevent
->exclusion
) {
319 tmp
[i
].exclusion
= 1;
333 * Fill lttng_event array of all kernel events in the channel.
335 static int list_lttng_kernel_events(char *channel_name
,
336 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
339 unsigned int nb_event
;
340 struct ltt_kernel_event
*event
;
341 struct ltt_kernel_channel
*kchan
;
343 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
345 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
349 nb_event
= kchan
->event_count
;
351 DBG("Listing events for channel %s", kchan
->channel
->name
);
358 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
359 if (*events
== NULL
) {
360 ret
= LTTNG_ERR_FATAL
;
364 /* Kernel channels */
365 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
366 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
367 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
368 (*events
)[i
].enabled
= event
->enabled
;
370 switch (event
->event
->instrumentation
) {
371 case LTTNG_KERNEL_TRACEPOINT
:
372 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
374 case LTTNG_KERNEL_KRETPROBE
:
375 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
376 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
377 sizeof(struct lttng_kernel_kprobe
));
379 case LTTNG_KERNEL_KPROBE
:
380 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
381 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
382 sizeof(struct lttng_kernel_kprobe
));
384 case LTTNG_KERNEL_FUNCTION
:
385 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
386 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
387 sizeof(struct lttng_kernel_function
));
389 case LTTNG_KERNEL_NOOP
:
390 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
392 case LTTNG_KERNEL_SYSCALL
:
393 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
395 case LTTNG_KERNEL_ALL
:
406 new_size
= syscall_list_channel(kchan
, events
, nb_event
);
418 /* Negate the error code to differentiate the size from an error */
423 * Add URI so the consumer output object. Set the correct path depending on the
424 * domain adding the default trace directory.
426 static int add_uri_to_consumer(struct consumer_output
*consumer
,
427 struct lttng_uri
*uri
, int domain
, const char *session_name
)
430 const char *default_trace_dir
;
434 if (consumer
== NULL
) {
435 DBG("No consumer detected. Don't add URI. Stopping.");
436 ret
= LTTNG_ERR_NO_CONSUMER
;
441 case LTTNG_DOMAIN_KERNEL
:
442 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
444 case LTTNG_DOMAIN_UST
:
445 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
449 * This case is possible is we try to add the URI to the global tracing
450 * session consumer object which in this case there is no subdir.
452 default_trace_dir
= "";
455 switch (uri
->dtype
) {
458 DBG2("Setting network URI to consumer");
460 if (consumer
->type
== CONSUMER_DST_NET
) {
461 if ((uri
->stype
== LTTNG_STREAM_CONTROL
&&
462 consumer
->dst
.net
.control_isset
) ||
463 (uri
->stype
== LTTNG_STREAM_DATA
&&
464 consumer
->dst
.net
.data_isset
)) {
465 ret
= LTTNG_ERR_URL_EXIST
;
469 memset(&consumer
->dst
.net
, 0, sizeof(consumer
->dst
.net
));
472 consumer
->type
= CONSUMER_DST_NET
;
474 /* Set URI into consumer output object */
475 ret
= consumer_set_network_uri(consumer
, uri
);
479 } else if (ret
== 1) {
481 * URI was the same in the consumer so we do not append the subdir
482 * again so to not duplicate output dir.
488 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
489 ret
= consumer_set_subdir(consumer
, session_name
);
491 ret
= LTTNG_ERR_FATAL
;
496 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
497 /* On a new subdir, reappend the default trace dir. */
498 strncat(consumer
->subdir
, default_trace_dir
,
499 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
500 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
505 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
506 memset(consumer
->dst
.trace_path
, 0,
507 sizeof(consumer
->dst
.trace_path
));
508 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
509 sizeof(consumer
->dst
.trace_path
));
510 /* Append default trace dir */
511 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
512 sizeof(consumer
->dst
.trace_path
) -
513 strlen(consumer
->dst
.trace_path
) - 1);
514 /* Flag consumer as local. */
515 consumer
->type
= CONSUMER_DST_LOCAL
;
526 * Init tracing by creating trace directory and sending fds kernel consumer.
528 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
531 struct lttng_ht_iter iter
;
532 struct consumer_socket
*socket
;
538 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
539 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
541 pthread_mutex_lock(socket
->lock
);
542 ret
= kernel_consumer_send_session(socket
, session
);
543 pthread_mutex_unlock(socket
->lock
);
545 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
557 * Create a socket to the relayd using the URI.
559 * On success, the relayd_sock pointer is set to the created socket.
560 * Else, it's stays untouched and a lttcomm error code is returned.
562 static int create_connect_relayd(struct lttng_uri
*uri
,
563 struct lttcomm_relayd_sock
**relayd_sock
)
566 struct lttcomm_relayd_sock
*rsock
;
568 rsock
= lttcomm_alloc_relayd_sock(uri
, RELAYD_VERSION_COMM_MAJOR
,
569 RELAYD_VERSION_COMM_MINOR
);
571 ret
= LTTNG_ERR_FATAL
;
576 * Connect to relayd so we can proceed with a session creation. This call
577 * can possibly block for an arbitrary amount of time to set the health
578 * state to be in poll execution.
581 ret
= relayd_connect(rsock
);
584 ERR("Unable to reach lttng-relayd");
585 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
589 /* Create socket for control stream. */
590 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
591 DBG3("Creating relayd stream socket from URI");
593 /* Check relayd version */
594 ret
= relayd_version_check(rsock
);
596 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
599 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
600 DBG3("Creating relayd data socket from URI");
602 /* Command is not valid */
603 ERR("Relayd invalid stream type: %d", uri
->stype
);
604 ret
= LTTNG_ERR_INVALID
;
608 *relayd_sock
= rsock
;
613 /* The returned value is not useful since we are on an error path. */
614 (void) relayd_close(rsock
);
622 * Connect to the relayd using URI and send the socket to the right consumer.
624 static int send_consumer_relayd_socket(int domain
, unsigned int session_id
,
625 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
626 struct consumer_socket
*consumer_sock
,
627 char *session_name
, char *hostname
, int session_live_timer
)
630 struct lttcomm_relayd_sock
*rsock
= NULL
;
632 /* Connect to relayd and make version check if uri is the control. */
633 ret
= create_connect_relayd(relayd_uri
, &rsock
);
634 if (ret
!= LTTNG_OK
) {
639 /* Set the network sequence index if not set. */
640 if (consumer
->net_seq_index
== (uint64_t) -1ULL) {
641 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
643 * Increment net_seq_idx because we are about to transfer the
644 * new relayd socket to the consumer.
645 * Assign unique key so the consumer can match streams.
647 consumer
->net_seq_index
= ++relayd_net_seq_idx
;
648 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
651 /* Send relayd socket to consumer. */
652 ret
= consumer_send_relayd_socket(consumer_sock
, rsock
, consumer
,
653 relayd_uri
->stype
, session_id
,
654 session_name
, hostname
, session_live_timer
);
656 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
660 /* Flag that the corresponding socket was sent. */
661 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
662 consumer_sock
->control_sock_sent
= 1;
663 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
664 consumer_sock
->data_sock_sent
= 1;
670 * Close socket which was dup on the consumer side. The session daemon does
671 * NOT keep track of the relayd socket(s) once transfer to the consumer.
675 (void) relayd_close(rsock
);
679 if (ret
!= LTTNG_OK
) {
681 * The consumer output for this session should not be used anymore
682 * since the relayd connection failed thus making any tracing or/and
683 * streaming not usable.
685 consumer
->enabled
= 0;
691 * Send both relayd sockets to a specific consumer and domain. This is a
692 * helper function to facilitate sending the information to the consumer for a
695 static int send_consumer_relayd_sockets(int domain
, unsigned int session_id
,
696 struct consumer_output
*consumer
, struct consumer_socket
*sock
,
697 char *session_name
, char *hostname
, int session_live_timer
)
704 /* Sending control relayd socket. */
705 if (!sock
->control_sock_sent
) {
706 ret
= send_consumer_relayd_socket(domain
, session_id
,
707 &consumer
->dst
.net
.control
, consumer
, sock
,
708 session_name
, hostname
, session_live_timer
);
709 if (ret
!= LTTNG_OK
) {
714 /* Sending data relayd socket. */
715 if (!sock
->data_sock_sent
) {
716 ret
= send_consumer_relayd_socket(domain
, session_id
,
717 &consumer
->dst
.net
.data
, consumer
, sock
,
718 session_name
, hostname
, session_live_timer
);
719 if (ret
!= LTTNG_OK
) {
729 * Setup relayd connections for a tracing session. First creates the socket to
730 * the relayd and send them to the right domain consumer. Consumer type MUST be
733 int cmd_setup_relayd(struct ltt_session
*session
)
736 struct ltt_ust_session
*usess
;
737 struct ltt_kernel_session
*ksess
;
738 struct consumer_socket
*socket
;
739 struct lttng_ht_iter iter
;
743 usess
= session
->ust_session
;
744 ksess
= session
->kernel_session
;
746 DBG("Setting relayd for session %s", session
->name
);
750 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
751 && usess
->consumer
->enabled
) {
752 /* For each consumer socket, send relayd sockets */
753 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
755 pthread_mutex_lock(socket
->lock
);
756 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
->id
,
757 usess
->consumer
, socket
,
758 session
->name
, session
->hostname
,
759 session
->live_timer
);
760 pthread_mutex_unlock(socket
->lock
);
761 if (ret
!= LTTNG_OK
) {
764 /* Session is now ready for network streaming. */
765 session
->net_handle
= 1;
769 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
770 && ksess
->consumer
->enabled
) {
771 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
773 pthread_mutex_lock(socket
->lock
);
774 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
->id
,
775 ksess
->consumer
, socket
,
776 session
->name
, session
->hostname
,
777 session
->live_timer
);
778 pthread_mutex_unlock(socket
->lock
);
779 if (ret
!= LTTNG_OK
) {
782 /* Session is now ready for network streaming. */
783 session
->net_handle
= 1;
793 * Start a kernel session by opening all necessary streams.
795 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
798 struct ltt_kernel_channel
*kchan
;
800 /* Open kernel metadata */
801 if (ksess
->metadata
== NULL
&& ksess
->output_traces
) {
802 ret
= kernel_open_metadata(ksess
);
804 ret
= LTTNG_ERR_KERN_META_FAIL
;
809 /* Open kernel metadata stream */
810 if (ksess
->metadata
&& ksess
->metadata_stream_fd
< 0) {
811 ret
= kernel_open_metadata_stream(ksess
);
813 ERR("Kernel create metadata stream failed");
814 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
819 /* For each channel */
820 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
821 if (kchan
->stream_count
== 0) {
822 ret
= kernel_open_channel_stream(kchan
);
824 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
827 /* Update the stream global counter */
828 ksess
->stream_count_global
+= ret
;
832 /* Setup kernel consumer socket and send fds to it */
833 ret
= init_kernel_tracing(ksess
);
835 ret
= LTTNG_ERR_KERN_START_FAIL
;
839 /* This start the kernel tracing */
840 ret
= kernel_start_session(ksess
);
842 ret
= LTTNG_ERR_KERN_START_FAIL
;
846 /* Quiescent wait after starting trace */
847 kernel_wait_quiescent(kernel_tracer_fd
);
858 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
860 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
864 struct ltt_ust_session
*usess
;
866 usess
= session
->ust_session
;
871 case LTTNG_DOMAIN_KERNEL
:
873 ret
= channel_kernel_disable(session
->kernel_session
,
875 if (ret
!= LTTNG_OK
) {
879 kernel_wait_quiescent(kernel_tracer_fd
);
882 case LTTNG_DOMAIN_UST
:
884 struct ltt_ust_channel
*uchan
;
885 struct lttng_ht
*chan_ht
;
887 chan_ht
= usess
->domain_global
.channels
;
889 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
891 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
895 ret
= channel_ust_disable(usess
, uchan
);
896 if (ret
!= LTTNG_OK
) {
902 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
914 * Command LTTNG_TRACK_PID processed by the client thread.
916 * Called with session lock held.
918 int cmd_track_pid(struct ltt_session
*session
, int domain
, int pid
)
925 case LTTNG_DOMAIN_KERNEL
:
927 struct ltt_kernel_session
*ksess
;
929 ksess
= session
->kernel_session
;
931 ret
= kernel_track_pid(ksess
, pid
);
932 if (ret
!= LTTNG_OK
) {
936 kernel_wait_quiescent(kernel_tracer_fd
);
939 case LTTNG_DOMAIN_UST
:
941 struct ltt_ust_session
*usess
;
943 usess
= session
->ust_session
;
945 ret
= trace_ust_track_pid(usess
, pid
);
946 if (ret
!= LTTNG_OK
) {
952 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
964 * Command LTTNG_UNTRACK_PID processed by the client thread.
966 * Called with session lock held.
968 int cmd_untrack_pid(struct ltt_session
*session
, int domain
, int pid
)
975 case LTTNG_DOMAIN_KERNEL
:
977 struct ltt_kernel_session
*ksess
;
979 ksess
= session
->kernel_session
;
981 ret
= kernel_untrack_pid(ksess
, pid
);
982 if (ret
!= LTTNG_OK
) {
986 kernel_wait_quiescent(kernel_tracer_fd
);
989 case LTTNG_DOMAIN_UST
:
991 struct ltt_ust_session
*usess
;
993 usess
= session
->ust_session
;
995 ret
= trace_ust_untrack_pid(usess
, pid
);
996 if (ret
!= LTTNG_OK
) {
1002 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1014 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1016 * The wpipe arguments is used as a notifier for the kernel thread.
1018 int cmd_enable_channel(struct ltt_session
*session
,
1019 struct lttng_domain
*domain
, struct lttng_channel
*attr
, int wpipe
)
1022 struct ltt_ust_session
*usess
= session
->ust_session
;
1023 struct lttng_ht
*chan_ht
;
1030 len
= strnlen(attr
->name
, sizeof(attr
->name
));
1032 /* Validate channel name */
1033 if (attr
->name
[0] == '.' ||
1034 memchr(attr
->name
, '/', len
) != NULL
) {
1035 ret
= LTTNG_ERR_INVALID_CHANNEL_NAME
;
1039 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
1044 * Don't try to enable a channel if the session has been started at
1045 * some point in time before. The tracer does not allow it.
1047 if (session
->has_been_started
) {
1048 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1053 * If the session is a live session, remove the switch timer, the
1054 * live timer does the same thing but sends also synchronisation
1055 * beacons for inactive streams.
1057 if (session
->live_timer
> 0) {
1058 attr
->attr
.live_timer_interval
= session
->live_timer
;
1059 attr
->attr
.switch_timer_interval
= 0;
1063 * The ringbuffer (both in user space and kernel) behave badly in overwrite
1064 * mode and with less than 2 subbuf so block it right away and send back an
1065 * invalid attribute error.
1067 if (attr
->attr
.overwrite
&& attr
->attr
.num_subbuf
< 2) {
1068 ret
= LTTNG_ERR_INVALID
;
1072 switch (domain
->type
) {
1073 case LTTNG_DOMAIN_KERNEL
:
1075 struct ltt_kernel_channel
*kchan
;
1077 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
1078 session
->kernel_session
);
1079 if (kchan
== NULL
) {
1080 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
1081 if (attr
->name
[0] != '\0') {
1082 session
->kernel_session
->has_non_default_channel
= 1;
1085 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
1088 if (ret
!= LTTNG_OK
) {
1092 kernel_wait_quiescent(kernel_tracer_fd
);
1095 case LTTNG_DOMAIN_UST
:
1097 struct ltt_ust_channel
*uchan
;
1099 chan_ht
= usess
->domain_global
.channels
;
1101 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
1102 if (uchan
== NULL
) {
1103 ret
= channel_ust_create(usess
, attr
, domain
->buf_type
);
1104 if (attr
->name
[0] != '\0') {
1105 usess
->has_non_default_channel
= 1;
1108 ret
= channel_ust_enable(usess
, uchan
);
1113 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
1124 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1126 int cmd_disable_event(struct ltt_session
*session
, int domain
,
1128 struct lttng_event
*event
)
1133 DBG("Disable event command for event \'%s\'", event
->name
);
1135 event_name
= event
->name
;
1137 /* Error out on unhandled search criteria */
1138 if (event
->loglevel_type
|| event
->loglevel
!= -1 || event
->enabled
1139 || event
->pid
|| event
->filter
|| event
->exclusion
) {
1140 return LTTNG_ERR_UNK
;
1146 case LTTNG_DOMAIN_KERNEL
:
1148 struct ltt_kernel_channel
*kchan
;
1149 struct ltt_kernel_session
*ksess
;
1151 ksess
= session
->kernel_session
;
1154 * If a non-default channel has been created in the
1155 * session, explicitely require that -c chan_name needs
1158 if (ksess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1159 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1163 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
1164 if (kchan
== NULL
) {
1165 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
1169 switch (event
->type
) {
1170 case LTTNG_EVENT_ALL
:
1171 ret
= event_kernel_disable_event_all(kchan
);
1172 if (ret
!= LTTNG_OK
) {
1176 case LTTNG_EVENT_TRACEPOINT
: /* fall-through */
1177 case LTTNG_EVENT_SYSCALL
:
1178 if (!strcmp(event_name
, "*")) {
1179 ret
= event_kernel_disable_event_type(kchan
,
1182 ret
= event_kernel_disable_event(kchan
,
1185 if (ret
!= LTTNG_OK
) {
1189 case LTTNG_EVENT_PROBE
:
1190 case LTTNG_EVENT_FUNCTION
:
1191 case LTTNG_EVENT_FUNCTION_ENTRY
:
1192 ret
= event_kernel_disable_event(kchan
, event_name
);
1193 if (ret
!= LTTNG_OK
) {
1198 ret
= LTTNG_ERR_UNK
;
1202 kernel_wait_quiescent(kernel_tracer_fd
);
1205 case LTTNG_DOMAIN_UST
:
1207 struct ltt_ust_channel
*uchan
;
1208 struct ltt_ust_session
*usess
;
1210 usess
= session
->ust_session
;
1213 * If a non-default channel has been created in the
1214 * session, explicitely require that -c chan_name needs
1217 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1218 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1222 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1224 if (uchan
== NULL
) {
1225 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1229 switch (event
->type
) {
1230 case LTTNG_EVENT_ALL
:
1231 ret
= event_ust_disable_tracepoint(usess
, uchan
, event_name
);
1232 if (ret
!= LTTNG_OK
) {
1237 ret
= LTTNG_ERR_UNK
;
1241 DBG3("Disable UST event %s in channel %s completed", event_name
,
1245 case LTTNG_DOMAIN_LOG4J
:
1246 case LTTNG_DOMAIN_JUL
:
1247 case LTTNG_DOMAIN_PYTHON
:
1250 struct ltt_ust_session
*usess
= session
->ust_session
;
1254 switch (event
->type
) {
1255 case LTTNG_EVENT_ALL
:
1258 ret
= LTTNG_ERR_UNK
;
1262 agt
= trace_ust_find_agent(usess
, domain
);
1264 ret
= -LTTNG_ERR_UST_EVENT_NOT_FOUND
;
1267 /* The wild card * means that everything should be disabled. */
1268 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1269 ret
= event_agent_disable_all(usess
, agt
);
1271 ret
= event_agent_disable(usess
, agt
, event_name
);
1273 if (ret
!= LTTNG_OK
) {
1280 ret
= LTTNG_ERR_UND
;
1292 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1294 int cmd_add_context(struct ltt_session
*session
, int domain
,
1295 char *channel_name
, struct lttng_event_context
*ctx
, int kwpipe
)
1297 int ret
, chan_kern_created
= 0, chan_ust_created
= 0;
1300 case LTTNG_DOMAIN_KERNEL
:
1301 assert(session
->kernel_session
);
1303 if (session
->kernel_session
->channel_count
== 0) {
1304 /* Create default channel */
1305 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1306 if (ret
!= LTTNG_OK
) {
1309 chan_kern_created
= 1;
1311 /* Add kernel context to kernel tracer */
1312 ret
= context_kernel_add(session
->kernel_session
, ctx
, channel_name
);
1313 if (ret
!= LTTNG_OK
) {
1317 case LTTNG_DOMAIN_UST
:
1319 struct ltt_ust_session
*usess
= session
->ust_session
;
1320 unsigned int chan_count
;
1324 chan_count
= lttng_ht_get_count(usess
->domain_global
.channels
);
1325 if (chan_count
== 0) {
1326 struct lttng_channel
*attr
;
1327 /* Create default channel */
1328 attr
= channel_new_default_attr(domain
, usess
->buffer_type
);
1330 ret
= LTTNG_ERR_FATAL
;
1334 ret
= channel_ust_create(usess
, attr
, usess
->buffer_type
);
1335 if (ret
!= LTTNG_OK
) {
1340 chan_ust_created
= 1;
1343 ret
= context_ust_add(usess
, domain
, ctx
, channel_name
);
1344 if (ret
!= LTTNG_OK
) {
1350 ret
= LTTNG_ERR_UND
;
1357 if (chan_kern_created
) {
1358 struct ltt_kernel_channel
*kchan
=
1359 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME
,
1360 session
->kernel_session
);
1361 /* Created previously, this should NOT fail. */
1363 kernel_destroy_channel(kchan
);
1366 if (chan_ust_created
) {
1367 struct ltt_ust_channel
*uchan
=
1368 trace_ust_find_channel_by_name(
1369 session
->ust_session
->domain_global
.channels
,
1370 DEFAULT_CHANNEL_NAME
);
1371 /* Created previously, this should NOT fail. */
1373 /* Remove from the channel list of the session. */
1374 trace_ust_delete_channel(session
->ust_session
->domain_global
.channels
,
1376 trace_ust_destroy_channel(uchan
);
1381 static int validate_event_name(const char *name
)
1384 const char *c
= name
;
1385 const char *event_name_end
= c
+ LTTNG_SYMBOL_NAME_LEN
;
1386 bool null_terminated
= false;
1389 * Make sure that unescaped wildcards are only used as the last
1390 * character of the event name.
1392 while (c
< event_name_end
) {
1395 null_terminated
= true;
1401 if ((c
+ 1) < event_name_end
&& *(c
+ 1)) {
1402 /* Wildcard is not the last character */
1403 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1412 if (!ret
&& !null_terminated
) {
1413 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1418 static inline bool name_starts_with(const char *name
, const char *prefix
)
1420 const size_t max_cmp_len
= min(strlen(prefix
), LTTNG_SYMBOL_NAME_LEN
);
1422 return !strncmp(name
, prefix
, max_cmp_len
);
1425 /* Perform userspace-specific event name validation */
1426 static int validate_ust_event_name(const char *name
)
1436 * Check name against all internal UST event component namespaces used
1439 if (name_starts_with(name
, DEFAULT_JUL_EVENT_COMPONENT
) ||
1440 name_starts_with(name
, DEFAULT_LOG4J_EVENT_COMPONENT
) ||
1441 name_starts_with(name
, DEFAULT_PYTHON_EVENT_COMPONENT
)) {
1449 static int cmd_enable_event_internal(struct ltt_session
*session
,
1450 struct lttng_domain
*domain
,
1451 char *channel_name
, struct lttng_event
*event
,
1452 char *filter_expression
,
1453 struct lttng_filter_bytecode
*filter
,
1454 struct lttng_event_exclusion
*exclusion
,
1458 * Internal version of cmd_enable_event() with a supplemental
1459 * "internal_event" flag which is used to enable internal events which should
1460 * be hidden from clients. Such events are used in the agent implementation to
1461 * enable the events through which all "agent" events are funeled.
1463 static int _cmd_enable_event(struct ltt_session
*session
,
1464 struct lttng_domain
*domain
,
1465 char *channel_name
, struct lttng_event
*event
,
1466 char *filter_expression
,
1467 struct lttng_filter_bytecode
*filter
,
1468 struct lttng_event_exclusion
*exclusion
,
1469 int wpipe
, bool internal_event
)
1471 int ret
, channel_created
= 0;
1472 struct lttng_channel
*attr
;
1476 assert(channel_name
);
1478 DBG("Enable event command for event \'%s\'", event
->name
);
1480 ret
= validate_event_name(event
->name
);
1487 switch (domain
->type
) {
1488 case LTTNG_DOMAIN_KERNEL
:
1490 struct ltt_kernel_channel
*kchan
;
1493 * If a non-default channel has been created in the
1494 * session, explicitely require that -c chan_name needs
1497 if (session
->kernel_session
->has_non_default_channel
1498 && channel_name
[0] == '\0') {
1499 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1503 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1504 session
->kernel_session
);
1505 if (kchan
== NULL
) {
1506 attr
= channel_new_default_attr(LTTNG_DOMAIN_KERNEL
,
1507 LTTNG_BUFFER_GLOBAL
);
1509 ret
= LTTNG_ERR_FATAL
;
1512 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1514 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1515 if (ret
!= LTTNG_OK
) {
1521 channel_created
= 1;
1524 /* Get the newly created kernel channel pointer */
1525 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1526 session
->kernel_session
);
1527 if (kchan
== NULL
) {
1528 /* This sould not happen... */
1529 ret
= LTTNG_ERR_FATAL
;
1533 switch (event
->type
) {
1534 case LTTNG_EVENT_ALL
:
1536 char *filter_expression_a
= NULL
;
1537 struct lttng_filter_bytecode
*filter_a
= NULL
;
1540 * We need to duplicate filter_expression and filter,
1541 * because ownership is passed to first enable
1544 if (filter_expression
) {
1545 filter_expression_a
= strdup(filter_expression
);
1546 if (!filter_expression_a
) {
1547 ret
= LTTNG_ERR_FATAL
;
1552 filter_a
= zmalloc(sizeof(*filter_a
) + filter
->len
);
1554 free(filter_expression_a
);
1555 ret
= LTTNG_ERR_FATAL
;
1558 memcpy(filter_a
, filter
, sizeof(*filter_a
) + filter
->len
);
1560 event
->type
= LTTNG_EVENT_TRACEPOINT
; /* Hack */
1561 ret
= event_kernel_enable_event(kchan
, event
,
1562 filter_expression
, filter
);
1563 /* We have passed ownership */
1564 filter_expression
= NULL
;
1566 if (ret
!= LTTNG_OK
) {
1567 if (channel_created
) {
1568 /* Let's not leak a useless channel. */
1569 kernel_destroy_channel(kchan
);
1571 free(filter_expression_a
);
1575 event
->type
= LTTNG_EVENT_SYSCALL
; /* Hack */
1576 ret
= event_kernel_enable_event(kchan
, event
,
1577 filter_expression_a
, filter_a
);
1578 if (ret
!= LTTNG_OK
) {
1579 free(filter_expression_a
);
1585 case LTTNG_EVENT_PROBE
:
1586 case LTTNG_EVENT_FUNCTION
:
1587 case LTTNG_EVENT_FUNCTION_ENTRY
:
1588 case LTTNG_EVENT_TRACEPOINT
:
1589 ret
= event_kernel_enable_event(kchan
, event
,
1590 filter_expression
, filter
);
1591 /* We have passed ownership */
1592 filter_expression
= NULL
;
1594 if (ret
!= LTTNG_OK
) {
1595 if (channel_created
) {
1596 /* Let's not leak a useless channel. */
1597 kernel_destroy_channel(kchan
);
1602 case LTTNG_EVENT_SYSCALL
:
1603 ret
= event_kernel_enable_event(kchan
, event
,
1604 filter_expression
, filter
);
1605 /* We have passed ownership */
1606 filter_expression
= NULL
;
1608 if (ret
!= LTTNG_OK
) {
1613 ret
= LTTNG_ERR_UNK
;
1617 kernel_wait_quiescent(kernel_tracer_fd
);
1620 case LTTNG_DOMAIN_UST
:
1622 struct ltt_ust_channel
*uchan
;
1623 struct ltt_ust_session
*usess
= session
->ust_session
;
1628 * If a non-default channel has been created in the
1629 * session, explicitely require that -c chan_name needs
1632 if (usess
->has_non_default_channel
&& channel_name
[0] == '\0') {
1633 ret
= LTTNG_ERR_NEED_CHANNEL_NAME
;
1637 /* Get channel from global UST domain */
1638 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1640 if (uchan
== NULL
) {
1641 /* Create default channel */
1642 attr
= channel_new_default_attr(LTTNG_DOMAIN_UST
,
1643 usess
->buffer_type
);
1645 ret
= LTTNG_ERR_FATAL
;
1648 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1650 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1651 if (ret
!= LTTNG_OK
) {
1657 /* Get the newly created channel reference back */
1658 uchan
= trace_ust_find_channel_by_name(
1659 usess
->domain_global
.channels
, channel_name
);
1663 if (!internal_event
) {
1665 * Ensure the event name is not reserved for internal
1668 ret
= validate_ust_event_name(event
->name
);
1670 WARN("Userspace event name %s failed validation.",
1672 event
->name
: "NULL");
1673 ret
= LTTNG_ERR_INVALID_EVENT_NAME
;
1678 /* At this point, the session and channel exist on the tracer */
1679 ret
= event_ust_enable_tracepoint(usess
, uchan
, event
,
1680 filter_expression
, filter
, exclusion
,
1682 /* We have passed ownership */
1683 filter_expression
= NULL
;
1686 if (ret
!= LTTNG_OK
) {
1691 case LTTNG_DOMAIN_LOG4J
:
1692 case LTTNG_DOMAIN_JUL
:
1693 case LTTNG_DOMAIN_PYTHON
:
1695 const char *default_event_name
, *default_chan_name
;
1697 struct lttng_event uevent
;
1698 struct lttng_domain tmp_dom
;
1699 struct ltt_ust_session
*usess
= session
->ust_session
;
1703 agt
= trace_ust_find_agent(usess
, domain
->type
);
1705 agt
= agent_create(domain
->type
);
1707 ret
= -LTTNG_ERR_NOMEM
;
1710 agent_add(agt
, usess
->agents
);
1713 /* Create the default tracepoint. */
1714 memset(&uevent
, 0, sizeof(uevent
));
1715 uevent
.type
= LTTNG_EVENT_TRACEPOINT
;
1716 uevent
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
1717 default_event_name
= event_get_default_agent_ust_name(domain
->type
);
1718 if (!default_event_name
) {
1719 ret
= -LTTNG_ERR_FATAL
;
1722 strncpy(uevent
.name
, default_event_name
, sizeof(uevent
.name
));
1723 uevent
.name
[sizeof(uevent
.name
) - 1] = '\0';
1726 * The domain type is changed because we are about to enable the
1727 * default channel and event for the JUL domain that are hardcoded.
1728 * This happens in the UST domain.
1730 memcpy(&tmp_dom
, domain
, sizeof(tmp_dom
));
1731 tmp_dom
.type
= LTTNG_DOMAIN_UST
;
1733 switch (domain
->type
) {
1734 case LTTNG_DOMAIN_LOG4J
:
1735 default_chan_name
= DEFAULT_LOG4J_CHANNEL_NAME
;
1737 case LTTNG_DOMAIN_JUL
:
1738 default_chan_name
= DEFAULT_JUL_CHANNEL_NAME
;
1740 case LTTNG_DOMAIN_PYTHON
:
1741 default_chan_name
= DEFAULT_PYTHON_CHANNEL_NAME
;
1744 /* The switch/case we are in should avoid this else big problem */
1749 struct lttng_filter_bytecode
*filter_copy
= NULL
;
1752 filter_copy
= zmalloc(
1753 sizeof(struct lttng_filter_bytecode
)
1759 memcpy(filter_copy
, filter
,
1760 sizeof(struct lttng_filter_bytecode
)
1764 ret
= cmd_enable_event_internal(session
, &tmp_dom
,
1765 (char *) default_chan_name
,
1766 &uevent
, filter_expression
, filter_copy
,
1768 /* We have passed ownership */
1769 filter_expression
= NULL
;
1772 if (ret
!= LTTNG_OK
&& ret
!= LTTNG_ERR_UST_EVENT_ENABLED
) {
1776 /* The wild card * means that everything should be enabled. */
1777 if (strncmp(event
->name
, "*", 1) == 0 && strlen(event
->name
) == 1) {
1778 ret
= event_agent_enable_all(usess
, agt
, event
, filter
);
1781 ret
= event_agent_enable(usess
, agt
, event
, filter
);
1784 if (ret
!= LTTNG_OK
) {
1791 ret
= LTTNG_ERR_UND
;
1798 free(filter_expression
);
1806 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1807 * We own filter, exclusion, and filter_expression.
1809 int cmd_enable_event(struct ltt_session
*session
, struct lttng_domain
*domain
,
1810 char *channel_name
, struct lttng_event
*event
,
1811 char *filter_expression
,
1812 struct lttng_filter_bytecode
*filter
,
1813 struct lttng_event_exclusion
*exclusion
,
1816 return _cmd_enable_event(session
, domain
, channel_name
, event
,
1817 filter_expression
, filter
, exclusion
, wpipe
, false);
1821 * Enable an event which is internal to LTTng. An internal should
1822 * never be made visible to clients and are immune to checks such as
1825 static int cmd_enable_event_internal(struct ltt_session
*session
,
1826 struct lttng_domain
*domain
,
1827 char *channel_name
, struct lttng_event
*event
,
1828 char *filter_expression
,
1829 struct lttng_filter_bytecode
*filter
,
1830 struct lttng_event_exclusion
*exclusion
,
1833 return _cmd_enable_event(session
, domain
, channel_name
, event
,
1834 filter_expression
, filter
, exclusion
, wpipe
, true);
1838 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1840 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1843 ssize_t nb_events
= 0;
1846 case LTTNG_DOMAIN_KERNEL
:
1847 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1848 if (nb_events
< 0) {
1849 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1853 case LTTNG_DOMAIN_UST
:
1854 nb_events
= ust_app_list_events(events
);
1855 if (nb_events
< 0) {
1856 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1860 case LTTNG_DOMAIN_LOG4J
:
1861 case LTTNG_DOMAIN_JUL
:
1862 case LTTNG_DOMAIN_PYTHON
:
1863 nb_events
= agent_list_events(events
, domain
);
1864 if (nb_events
< 0) {
1865 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1870 ret
= LTTNG_ERR_UND
;
1877 /* Return negative value to differentiate return code */
1882 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1884 ssize_t
cmd_list_tracepoint_fields(int domain
,
1885 struct lttng_event_field
**fields
)
1888 ssize_t nb_fields
= 0;
1891 case LTTNG_DOMAIN_UST
:
1892 nb_fields
= ust_app_list_event_fields(fields
);
1893 if (nb_fields
< 0) {
1894 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1898 case LTTNG_DOMAIN_KERNEL
:
1899 default: /* fall-through */
1900 ret
= LTTNG_ERR_UND
;
1907 /* Return negative value to differentiate return code */
1911 ssize_t
cmd_list_syscalls(struct lttng_event
**events
)
1913 return syscall_table_list(events
);
1917 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
1919 * Called with session lock held.
1921 ssize_t
cmd_list_tracker_pids(struct ltt_session
*session
,
1922 int domain
, int32_t **pids
)
1925 ssize_t nr_pids
= 0;
1928 case LTTNG_DOMAIN_KERNEL
:
1930 struct ltt_kernel_session
*ksess
;
1932 ksess
= session
->kernel_session
;
1933 nr_pids
= kernel_list_tracker_pids(ksess
, pids
);
1935 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1940 case LTTNG_DOMAIN_UST
:
1942 struct ltt_ust_session
*usess
;
1944 usess
= session
->ust_session
;
1945 nr_pids
= trace_ust_list_tracker_pids(usess
, pids
);
1947 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1952 case LTTNG_DOMAIN_LOG4J
:
1953 case LTTNG_DOMAIN_JUL
:
1954 case LTTNG_DOMAIN_PYTHON
:
1956 ret
= LTTNG_ERR_UND
;
1963 /* Return negative value to differentiate return code */
1968 * Command LTTNG_START_TRACE processed by the client thread.
1970 * Called with session mutex held.
1972 int cmd_start_trace(struct ltt_session
*session
)
1975 unsigned long nb_chan
= 0;
1976 struct ltt_kernel_session
*ksession
;
1977 struct ltt_ust_session
*usess
;
1981 /* Ease our life a bit ;) */
1982 ksession
= session
->kernel_session
;
1983 usess
= session
->ust_session
;
1985 /* Is the session already started? */
1986 if (session
->active
) {
1987 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1992 * Starting a session without channel is useless since after that it's not
1993 * possible to enable channel thus inform the client.
1995 if (usess
&& usess
->domain_global
.channels
) {
1996 nb_chan
+= lttng_ht_get_count(usess
->domain_global
.channels
);
1999 nb_chan
+= ksession
->channel_count
;
2002 ret
= LTTNG_ERR_NO_CHANNEL
;
2006 /* Kernel tracing */
2007 if (ksession
!= NULL
) {
2008 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
2009 if (ret
!= LTTNG_OK
) {
2014 /* Flag session that trace should start automatically */
2017 * Even though the start trace might fail, flag this session active so
2018 * other application coming in are started by default.
2022 ret
= ust_app_start_trace_all(usess
);
2024 ret
= LTTNG_ERR_UST_START_FAIL
;
2029 /* Flag this after a successful start. */
2030 session
->has_been_started
= 1;
2031 session
->active
= 1;
2040 * Command LTTNG_STOP_TRACE processed by the client thread.
2042 int cmd_stop_trace(struct ltt_session
*session
)
2045 struct ltt_kernel_channel
*kchan
;
2046 struct ltt_kernel_session
*ksession
;
2047 struct ltt_ust_session
*usess
;
2052 ksession
= session
->kernel_session
;
2053 usess
= session
->ust_session
;
2055 /* Session is not active. Skip everythong and inform the client. */
2056 if (!session
->active
) {
2057 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
2062 if (ksession
&& ksession
->active
) {
2063 DBG("Stop kernel tracing");
2065 /* Flush metadata if exist */
2066 if (ksession
->metadata_stream_fd
>= 0) {
2067 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
2069 ERR("Kernel metadata flush failed");
2073 /* Flush all buffers before stopping */
2074 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
2075 ret
= kernel_flush_buffer(kchan
);
2077 ERR("Kernel flush buffer error");
2081 ret
= kernel_stop_session(ksession
);
2083 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
2087 kernel_wait_quiescent(kernel_tracer_fd
);
2089 ksession
->active
= 0;
2092 if (usess
&& usess
->active
) {
2094 * Even though the stop trace might fail, flag this session inactive so
2095 * other application coming in are not started by default.
2099 ret
= ust_app_stop_trace_all(usess
);
2101 ret
= LTTNG_ERR_UST_STOP_FAIL
;
2106 /* Flag inactive after a successful stop. */
2107 session
->active
= 0;
2115 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2117 int cmd_set_consumer_uri(struct ltt_session
*session
, size_t nb_uri
,
2118 struct lttng_uri
*uris
)
2121 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2122 struct ltt_ust_session
*usess
= session
->ust_session
;
2128 /* Can't set consumer URI if the session is active. */
2129 if (session
->active
) {
2130 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2134 /* Set the "global" consumer URIs */
2135 for (i
= 0; i
< nb_uri
; i
++) {
2136 ret
= add_uri_to_consumer(session
->consumer
,
2137 &uris
[i
], 0, session
->name
);
2138 if (ret
!= LTTNG_OK
) {
2143 /* Set UST session URIs */
2144 if (session
->ust_session
) {
2145 for (i
= 0; i
< nb_uri
; i
++) {
2146 ret
= add_uri_to_consumer(
2147 session
->ust_session
->consumer
,
2148 &uris
[i
], LTTNG_DOMAIN_UST
,
2150 if (ret
!= LTTNG_OK
) {
2156 /* Set kernel session URIs */
2157 if (session
->kernel_session
) {
2158 for (i
= 0; i
< nb_uri
; i
++) {
2159 ret
= add_uri_to_consumer(
2160 session
->kernel_session
->consumer
,
2161 &uris
[i
], LTTNG_DOMAIN_KERNEL
,
2163 if (ret
!= LTTNG_OK
) {
2170 * Make sure to set the session in output mode after we set URI since a
2171 * session can be created without URL (thus flagged in no output mode).
2173 session
->output_traces
= 1;
2175 ksess
->output_traces
= 1;
2179 usess
->output_traces
= 1;
2190 * Command LTTNG_CREATE_SESSION processed by the client thread.
2192 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
2193 size_t nb_uri
, lttng_sock_cred
*creds
, unsigned int live_timer
)
2196 struct ltt_session
*session
;
2202 * Verify if the session already exist
2204 * XXX: There is no need for the session lock list here since the caller
2205 * (process_client_msg) is holding it. We might want to change that so a
2206 * single command does not lock the entire session list.
2208 session
= session_find_by_name(name
);
2209 if (session
!= NULL
) {
2210 ret
= LTTNG_ERR_EXIST_SESS
;
2214 /* Create tracing session in the registry */
2215 ret
= session_create(name
, LTTNG_SOCK_GET_UID_CRED(creds
),
2216 LTTNG_SOCK_GET_GID_CRED(creds
));
2217 if (ret
!= LTTNG_OK
) {
2222 * Get the newly created session pointer back
2224 * XXX: There is no need for the session lock list here since the caller
2225 * (process_client_msg) is holding it. We might want to change that so a
2226 * single command does not lock the entire session list.
2228 session
= session_find_by_name(name
);
2231 session
->live_timer
= live_timer
;
2232 /* Create default consumer output for the session not yet created. */
2233 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
2234 if (session
->consumer
== NULL
) {
2235 ret
= LTTNG_ERR_FATAL
;
2236 goto consumer_error
;
2240 ret
= cmd_set_consumer_uri(session
, nb_uri
, uris
);
2241 if (ret
!= LTTNG_OK
) {
2242 goto consumer_error
;
2244 session
->output_traces
= 1;
2246 session
->output_traces
= 0;
2247 DBG2("Session %s created with no output", session
->name
);
2250 session
->consumer
->enabled
= 1;
2255 session_destroy(session
);
2262 * Command LTTNG_CREATE_SESSION_SNAPSHOT processed by the client thread.
2264 int cmd_create_session_snapshot(char *name
, struct lttng_uri
*uris
,
2265 size_t nb_uri
, lttng_sock_cred
*creds
)
2268 struct ltt_session
*session
;
2269 struct snapshot_output
*new_output
= NULL
;
2275 * Create session in no output mode with URIs set to NULL. The uris we've
2276 * received are for a default snapshot output if one.
2278 ret
= cmd_create_session_uri(name
, NULL
, 0, creds
, -1);
2279 if (ret
!= LTTNG_OK
) {
2283 /* Get the newly created session pointer back. This should NEVER fail. */
2284 session
= session_find_by_name(name
);
2287 /* Flag session for snapshot mode. */
2288 session
->snapshot_mode
= 1;
2290 /* Skip snapshot output creation if no URI is given. */
2295 new_output
= snapshot_output_alloc();
2297 ret
= LTTNG_ERR_NOMEM
;
2298 goto error_snapshot_alloc
;
2301 ret
= snapshot_output_init_with_uri(DEFAULT_SNAPSHOT_MAX_SIZE
, NULL
,
2302 uris
, nb_uri
, session
->consumer
, new_output
, &session
->snapshot
);
2304 if (ret
== -ENOMEM
) {
2305 ret
= LTTNG_ERR_NOMEM
;
2307 ret
= LTTNG_ERR_INVALID
;
2309 goto error_snapshot
;
2313 snapshot_add_output(&session
->snapshot
, new_output
);
2320 snapshot_output_destroy(new_output
);
2321 error_snapshot_alloc
:
2322 session_destroy(session
);
2328 * Command LTTNG_DESTROY_SESSION processed by the client thread.
2330 * Called with session lock held.
2332 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
2335 struct ltt_ust_session
*usess
;
2336 struct ltt_kernel_session
*ksess
;
2341 usess
= session
->ust_session
;
2342 ksess
= session
->kernel_session
;
2344 /* Clean kernel session teardown */
2345 kernel_destroy_session(ksess
);
2347 /* UST session teardown */
2349 /* Close any relayd session */
2350 consumer_output_send_destroy_relayd(usess
->consumer
);
2352 /* Destroy every UST application related to this session. */
2353 ret
= ust_app_destroy_trace_all(usess
);
2355 ERR("Error in ust_app_destroy_trace_all");
2358 /* Clean up the rest. */
2359 trace_ust_destroy_session(usess
);
2363 * Must notify the kernel thread here to update it's poll set in order to
2364 * remove the channel(s)' fd just destroyed.
2366 ret
= notify_thread_pipe(wpipe
);
2368 PERROR("write kernel poll pipe");
2371 ret
= session_destroy(session
);
2377 * Command LTTNG_CALIBRATE processed by the client thread.
2379 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
2384 case LTTNG_DOMAIN_KERNEL
:
2386 struct lttng_kernel_calibrate kcalibrate
;
2388 switch (calibrate
->type
) {
2389 case LTTNG_CALIBRATE_FUNCTION
:
2391 /* Default and only possible calibrate option. */
2392 kcalibrate
.type
= LTTNG_KERNEL_CALIBRATE_KRETPROBE
;
2396 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2398 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2403 case LTTNG_DOMAIN_UST
:
2405 struct lttng_ust_calibrate ucalibrate
;
2407 switch (calibrate
->type
) {
2408 case LTTNG_CALIBRATE_FUNCTION
:
2410 /* Default and only possible calibrate option. */
2411 ucalibrate
.type
= LTTNG_UST_CALIBRATE_TRACEPOINT
;
2415 ret
= ust_app_calibrate_glb(&ucalibrate
);
2417 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
2423 ret
= LTTNG_ERR_UND
;
2434 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
2436 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
2437 const char *sock_path
, struct consumer_data
*cdata
)
2440 struct consumer_socket
*socket
= NULL
;
2447 case LTTNG_DOMAIN_KERNEL
:
2449 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2453 /* Can't register a consumer if there is already one */
2454 if (ksess
->consumer_fds_sent
!= 0) {
2455 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
2459 sock
= lttcomm_connect_unix_sock(sock_path
);
2461 ret
= LTTNG_ERR_CONNECT_FAIL
;
2464 cdata
->cmd_sock
= sock
;
2466 socket
= consumer_allocate_socket(&cdata
->cmd_sock
);
2467 if (socket
== NULL
) {
2470 PERROR("close register consumer");
2472 cdata
->cmd_sock
= -1;
2473 ret
= LTTNG_ERR_FATAL
;
2477 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
2478 if (socket
->lock
== NULL
) {
2479 PERROR("zmalloc pthread mutex");
2480 ret
= LTTNG_ERR_FATAL
;
2483 pthread_mutex_init(socket
->lock
, NULL
);
2484 socket
->registered
= 1;
2487 consumer_add_socket(socket
, ksess
->consumer
);
2490 pthread_mutex_lock(&cdata
->pid_mutex
);
2492 pthread_mutex_unlock(&cdata
->pid_mutex
);
2497 /* TODO: Userspace tracing */
2498 ret
= LTTNG_ERR_UND
;
2506 consumer_destroy_socket(socket
);
2512 * Command LTTNG_LIST_DOMAINS processed by the client thread.
2514 ssize_t
cmd_list_domains(struct ltt_session
*session
,
2515 struct lttng_domain
**domains
)
2520 struct lttng_ht_iter iter
;
2522 if (session
->kernel_session
!= NULL
) {
2523 DBG3("Listing domains found kernel domain");
2527 if (session
->ust_session
!= NULL
) {
2528 DBG3("Listing domains found UST global domain");
2532 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2534 if (agt
->being_used
) {
2545 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
2546 if (*domains
== NULL
) {
2547 ret
= LTTNG_ERR_FATAL
;
2551 if (session
->kernel_session
!= NULL
) {
2552 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
2554 /* Kernel session buffer type is always GLOBAL */
2555 (*domains
)[index
].buf_type
= LTTNG_BUFFER_GLOBAL
;
2560 if (session
->ust_session
!= NULL
) {
2561 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
2562 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2566 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
, &iter
.iter
,
2568 if (agt
->being_used
) {
2569 (*domains
)[index
].type
= agt
->domain
;
2570 (*domains
)[index
].buf_type
= session
->ust_session
->buffer_type
;
2580 /* Return negative value to differentiate return code */
2586 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2588 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2589 struct lttng_channel
**channels
)
2592 ssize_t nb_chan
= 0;
2595 case LTTNG_DOMAIN_KERNEL
:
2596 if (session
->kernel_session
!= NULL
) {
2597 nb_chan
= session
->kernel_session
->channel_count
;
2599 DBG3("Number of kernel channels %zd", nb_chan
);
2601 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2604 case LTTNG_DOMAIN_UST
:
2605 if (session
->ust_session
!= NULL
) {
2607 nb_chan
= lttng_ht_get_count(
2608 session
->ust_session
->domain_global
.channels
);
2611 DBG3("Number of UST global channels %zd", nb_chan
);
2613 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2618 ret
= LTTNG_ERR_UND
;
2623 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2624 if (*channels
== NULL
) {
2625 ret
= LTTNG_ERR_FATAL
;
2629 list_lttng_channels(domain
, session
, *channels
);
2635 /* Return negative value to differentiate return code */
2640 * Command LTTNG_LIST_EVENTS processed by the client thread.
2642 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2643 char *channel_name
, struct lttng_event
**events
)
2646 ssize_t nb_event
= 0;
2649 case LTTNG_DOMAIN_KERNEL
:
2650 if (session
->kernel_session
!= NULL
) {
2651 nb_event
= list_lttng_kernel_events(channel_name
,
2652 session
->kernel_session
, events
);
2655 case LTTNG_DOMAIN_UST
:
2657 if (session
->ust_session
!= NULL
) {
2658 nb_event
= list_lttng_ust_global_events(channel_name
,
2659 &session
->ust_session
->domain_global
, events
);
2663 case LTTNG_DOMAIN_LOG4J
:
2664 case LTTNG_DOMAIN_JUL
:
2665 case LTTNG_DOMAIN_PYTHON
:
2666 if (session
->ust_session
) {
2667 struct lttng_ht_iter iter
;
2671 cds_lfht_for_each_entry(session
->ust_session
->agents
->ht
,
2672 &iter
.iter
, agt
, node
.node
) {
2673 nb_event
= list_lttng_agent_events(agt
, events
);
2679 ret
= LTTNG_ERR_UND
;
2686 /* Return negative value to differentiate return code */
2691 * Using the session list, filled a lttng_session array to send back to the
2692 * client for session listing.
2694 * The session list lock MUST be acquired before calling this function. Use
2695 * session_lock_list() and session_unlock_list().
2697 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2702 struct ltt_session
*session
;
2703 struct ltt_session_list
*list
= session_get_list();
2705 DBG("Getting all available session for UID %d GID %d",
2708 * Iterate over session list and append data after the control struct in
2711 cds_list_for_each_entry(session
, &list
->head
, list
) {
2713 * Only list the sessions the user can control.
2715 if (!session_access_ok(session
, uid
, gid
)) {
2719 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2720 struct ltt_ust_session
*usess
= session
->ust_session
;
2722 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2723 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2724 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2725 ret
= build_network_session_path(sessions
[i
].path
,
2726 sizeof(sessions
[i
].path
), session
);
2728 ret
= snprintf(sessions
[i
].path
, sizeof(sessions
[i
].path
), "%s",
2729 session
->consumer
->dst
.trace_path
);
2732 PERROR("snprintf session path");
2736 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2737 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2738 sessions
[i
].enabled
= session
->active
;
2739 sessions
[i
].snapshot_mode
= session
->snapshot_mode
;
2740 sessions
[i
].live_timer_interval
= session
->live_timer
;
2746 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2747 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
2749 int cmd_data_pending(struct ltt_session
*session
)
2752 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2753 struct ltt_ust_session
*usess
= session
->ust_session
;
2757 /* Session MUST be stopped to ask for data availability. */
2758 if (session
->active
) {
2759 ret
= LTTNG_ERR_SESSION_STARTED
;
2763 * If stopped, just make sure we've started before else the above call
2764 * will always send that there is data pending.
2766 * The consumer assumes that when the data pending command is received,
2767 * the trace has been started before or else no output data is written
2768 * by the streams which is a condition for data pending. So, this is
2769 * *VERY* important that we don't ask the consumer before a start
2772 if (!session
->has_been_started
) {
2778 if (ksess
&& ksess
->consumer
) {
2779 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2781 /* Data is still being extracted for the kernel. */
2786 if (usess
&& usess
->consumer
) {
2787 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2789 /* Data is still being extracted for the kernel. */
2794 /* Data is ready to be read by a viewer */
2802 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
2804 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2806 int cmd_snapshot_add_output(struct ltt_session
*session
,
2807 struct lttng_snapshot_output
*output
, uint32_t *id
)
2810 struct snapshot_output
*new_output
;
2815 DBG("Cmd snapshot add output for session %s", session
->name
);
2818 * Permission denied to create an output if the session is not
2819 * set in no output mode.
2821 if (session
->output_traces
) {
2822 ret
= LTTNG_ERR_EPERM
;
2826 /* Only one output is allowed until we have the "tee" feature. */
2827 if (session
->snapshot
.nb_output
== 1) {
2828 ret
= LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST
;
2832 new_output
= snapshot_output_alloc();
2834 ret
= LTTNG_ERR_NOMEM
;
2838 ret
= snapshot_output_init(output
->max_size
, output
->name
,
2839 output
->ctrl_url
, output
->data_url
, session
->consumer
, new_output
,
2840 &session
->snapshot
);
2842 if (ret
== -ENOMEM
) {
2843 ret
= LTTNG_ERR_NOMEM
;
2845 ret
= LTTNG_ERR_INVALID
;
2851 snapshot_add_output(&session
->snapshot
, new_output
);
2853 *id
= new_output
->id
;
2860 snapshot_output_destroy(new_output
);
2866 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
2868 * Return LTTNG_OK on success or else a LTTNG_ERR code.
2870 int cmd_snapshot_del_output(struct ltt_session
*session
,
2871 struct lttng_snapshot_output
*output
)
2874 struct snapshot_output
*sout
= NULL
;
2882 * Permission denied to create an output if the session is not
2883 * set in no output mode.
2885 if (session
->output_traces
) {
2886 ret
= LTTNG_ERR_EPERM
;
2891 DBG("Cmd snapshot del output id %" PRIu32
" for session %s", output
->id
,
2893 sout
= snapshot_find_output_by_id(output
->id
, &session
->snapshot
);
2894 } else if (*output
->name
!= '\0') {
2895 DBG("Cmd snapshot del output name %s for session %s", output
->name
,
2897 sout
= snapshot_find_output_by_name(output
->name
, &session
->snapshot
);
2900 ret
= LTTNG_ERR_INVALID
;
2904 snapshot_delete_output(&session
->snapshot
, sout
);
2905 snapshot_output_destroy(sout
);
2914 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
2916 * If no output is available, outputs is untouched and 0 is returned.
2918 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
2920 ssize_t
cmd_snapshot_list_outputs(struct ltt_session
*session
,
2921 struct lttng_snapshot_output
**outputs
)
2924 struct lttng_snapshot_output
*list
= NULL
;
2925 struct lttng_ht_iter iter
;
2926 struct snapshot_output
*output
;
2931 DBG("Cmd snapshot list outputs for session %s", session
->name
);
2934 * Permission denied to create an output if the session is not
2935 * set in no output mode.
2937 if (session
->output_traces
) {
2938 ret
= -LTTNG_ERR_EPERM
;
2942 if (session
->snapshot
.nb_output
== 0) {
2947 list
= zmalloc(session
->snapshot
.nb_output
* sizeof(*list
));
2949 ret
= -LTTNG_ERR_NOMEM
;
2953 /* Copy list from session to the new list object. */
2955 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
, &iter
.iter
,
2956 output
, node
.node
) {
2957 assert(output
->consumer
);
2958 list
[idx
].id
= output
->id
;
2959 list
[idx
].max_size
= output
->max_size
;
2960 strncpy(list
[idx
].name
, output
->name
, sizeof(list
[idx
].name
));
2961 if (output
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2962 strncpy(list
[idx
].ctrl_url
, output
->consumer
->dst
.trace_path
,
2963 sizeof(list
[idx
].ctrl_url
));
2966 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.control
,
2967 list
[idx
].ctrl_url
, sizeof(list
[idx
].ctrl_url
));
2969 ret
= -LTTNG_ERR_NOMEM
;
2974 ret
= uri_to_str_url(&output
->consumer
->dst
.net
.data
,
2975 list
[idx
].data_url
, sizeof(list
[idx
].data_url
));
2977 ret
= -LTTNG_ERR_NOMEM
;
2986 ret
= session
->snapshot
.nb_output
;
2994 * Send relayd sockets from snapshot output to consumer. Ignore request if the
2995 * snapshot output is *not* set with a remote destination.
2997 * Return 0 on success or a LTTNG_ERR code.
2999 static int set_relayd_for_snapshot(struct consumer_output
*consumer
,
3000 struct snapshot_output
*snap_output
, struct ltt_session
*session
)
3003 struct lttng_ht_iter iter
;
3004 struct consumer_socket
*socket
;
3007 assert(snap_output
);
3010 DBG2("Set relayd object from snapshot output");
3012 /* Ignore if snapshot consumer output is not network. */
3013 if (snap_output
->consumer
->type
!= CONSUMER_DST_NET
) {
3018 * For each consumer socket, create and send the relayd object of the
3022 cds_lfht_for_each_entry(snap_output
->consumer
->socks
->ht
, &iter
.iter
,
3023 socket
, node
.node
) {
3024 ret
= send_consumer_relayd_sockets(0, session
->id
,
3025 snap_output
->consumer
, socket
,
3026 session
->name
, session
->hostname
,
3027 session
->live_timer
);
3028 if (ret
!= LTTNG_OK
) {
3040 * Record a kernel snapshot.
3042 * Return LTTNG_OK on success or a LTTNG_ERR code.
3044 static int record_kernel_snapshot(struct ltt_kernel_session
*ksess
,
3045 struct snapshot_output
*output
, struct ltt_session
*session
,
3046 int wait
, uint64_t nb_packets_per_stream
)
3054 /* Get the datetime for the snapshot output directory. */
3055 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3056 sizeof(output
->datetime
));
3058 ret
= LTTNG_ERR_INVALID
;
3063 * Copy kernel session sockets so we can communicate with the right
3064 * consumer for the snapshot record command.
3066 ret
= consumer_copy_sockets(output
->consumer
, ksess
->consumer
);
3068 ret
= LTTNG_ERR_NOMEM
;
3072 ret
= set_relayd_for_snapshot(ksess
->consumer
, output
, session
);
3073 if (ret
!= LTTNG_OK
) {
3074 goto error_snapshot
;
3077 ret
= kernel_snapshot_record(ksess
, output
, wait
, nb_packets_per_stream
);
3078 if (ret
!= LTTNG_OK
) {
3079 goto error_snapshot
;
3086 /* Clean up copied sockets so this output can use some other later on. */
3087 consumer_destroy_output_sockets(output
->consumer
);
3094 * Record a UST snapshot.
3096 * Return 0 on success or a LTTNG_ERR error code.
3098 static int record_ust_snapshot(struct ltt_ust_session
*usess
,
3099 struct snapshot_output
*output
, struct ltt_session
*session
,
3100 int wait
, uint64_t nb_packets_per_stream
)
3108 /* Get the datetime for the snapshot output directory. */
3109 ret
= utils_get_current_time_str("%Y%m%d-%H%M%S", output
->datetime
,
3110 sizeof(output
->datetime
));
3112 ret
= LTTNG_ERR_INVALID
;
3117 * Copy UST session sockets so we can communicate with the right
3118 * consumer for the snapshot record command.
3120 ret
= consumer_copy_sockets(output
->consumer
, usess
->consumer
);
3122 ret
= LTTNG_ERR_NOMEM
;
3126 ret
= set_relayd_for_snapshot(usess
->consumer
, output
, session
);
3127 if (ret
!= LTTNG_OK
) {
3128 goto error_snapshot
;
3131 ret
= ust_app_snapshot_record(usess
, output
, wait
, nb_packets_per_stream
);
3135 ret
= LTTNG_ERR_INVALID
;
3138 ret
= LTTNG_ERR_SNAPSHOT_NODATA
;
3141 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3144 goto error_snapshot
;
3150 /* Clean up copied sockets so this output can use some other later on. */
3151 consumer_destroy_output_sockets(output
->consumer
);
3157 uint64_t get_session_size_one_more_packet_per_stream(struct ltt_session
*session
,
3158 uint64_t cur_nr_packets
)
3160 uint64_t tot_size
= 0;
3162 if (session
->kernel_session
) {
3163 struct ltt_kernel_channel
*chan
;
3164 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3166 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
3167 if (cur_nr_packets
>= chan
->channel
->attr
.num_subbuf
) {
3169 * Don't take channel into account if we
3170 * already grab all its packets.
3174 tot_size
+= chan
->channel
->attr
.subbuf_size
3175 * chan
->stream_count
;
3179 if (session
->ust_session
) {
3180 struct ltt_ust_session
*usess
= session
->ust_session
;
3182 tot_size
+= ust_app_get_size_one_more_packet_per_stream(usess
,
3190 * Calculate the number of packets we can grab from each stream that
3191 * fits within the overall snapshot max size.
3193 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
3194 * the number of packets per stream.
3196 * TODO: this approach is not perfect: we consider the worse case
3197 * (packet filling the sub-buffers) as an upper bound, but we could do
3198 * better if we do this calculation while we actually grab the packet
3199 * content: we would know how much padding we don't actually store into
3202 * This algorithm is currently bounded by the number of packets per
3205 * Since we call this algorithm before actually grabbing the data, it's
3206 * an approximation: for instance, applications could appear/disappear
3207 * in between this call and actually grabbing data.
3210 int64_t get_session_nb_packets_per_stream(struct ltt_session
*session
, uint64_t max_size
)
3213 uint64_t cur_nb_packets
= 0;
3216 return 0; /* Infinite */
3219 size_left
= max_size
;
3221 uint64_t one_more_packet_tot_size
;
3223 one_more_packet_tot_size
= get_session_size_one_more_packet_per_stream(session
,
3225 if (!one_more_packet_tot_size
) {
3226 /* We are already grabbing all packets. */
3229 size_left
-= one_more_packet_tot_size
;
3230 if (size_left
< 0) {
3235 if (!cur_nb_packets
) {
3236 /* Not enough room to grab one packet of each stream, error. */
3239 return cur_nb_packets
;
3243 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
3245 * The wait parameter is ignored so this call always wait for the snapshot to
3246 * complete before returning.
3248 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3250 int cmd_snapshot_record(struct ltt_session
*session
,
3251 struct lttng_snapshot_output
*output
, int wait
)
3254 unsigned int use_tmp_output
= 0;
3255 struct snapshot_output tmp_output
;
3256 unsigned int snapshot_success
= 0;
3261 DBG("Cmd snapshot record for session %s", session
->name
);
3264 * Permission denied to create an output if the session is not
3265 * set in no output mode.
3267 if (session
->output_traces
) {
3268 ret
= LTTNG_ERR_EPERM
;
3272 /* The session needs to be started at least once. */
3273 if (!session
->has_been_started
) {
3274 ret
= LTTNG_ERR_START_SESSION_ONCE
;
3278 /* Use temporary output for the session. */
3279 if (*output
->ctrl_url
!= '\0') {
3280 ret
= snapshot_output_init(output
->max_size
, output
->name
,
3281 output
->ctrl_url
, output
->data_url
, session
->consumer
,
3284 if (ret
== -ENOMEM
) {
3285 ret
= LTTNG_ERR_NOMEM
;
3287 ret
= LTTNG_ERR_INVALID
;
3291 /* Use the global session count for the temporary snapshot. */
3292 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3296 if (session
->kernel_session
) {
3297 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
3299 if (use_tmp_output
) {
3300 int64_t nb_packets_per_stream
;
3302 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3303 tmp_output
.max_size
);
3304 if (nb_packets_per_stream
< 0) {
3305 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3308 ret
= record_kernel_snapshot(ksess
, &tmp_output
, session
,
3309 wait
, nb_packets_per_stream
);
3310 if (ret
!= LTTNG_OK
) {
3313 snapshot_success
= 1;
3315 struct snapshot_output
*sout
;
3316 struct lttng_ht_iter iter
;
3319 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3320 &iter
.iter
, sout
, node
.node
) {
3321 int64_t nb_packets_per_stream
;
3324 * Make a local copy of the output and assign the possible
3325 * temporary value given by the caller.
3327 memset(&tmp_output
, 0, sizeof(tmp_output
));
3328 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3330 if (output
->max_size
!= (uint64_t) -1ULL) {
3331 tmp_output
.max_size
= output
->max_size
;
3334 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3335 tmp_output
.max_size
);
3336 if (nb_packets_per_stream
< 0) {
3337 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3341 /* Use temporary name. */
3342 if (*output
->name
!= '\0') {
3343 strncpy(tmp_output
.name
, output
->name
,
3344 sizeof(tmp_output
.name
));
3347 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3349 ret
= record_kernel_snapshot(ksess
, &tmp_output
,
3350 session
, wait
, nb_packets_per_stream
);
3351 if (ret
!= LTTNG_OK
) {
3355 snapshot_success
= 1;
3361 if (session
->ust_session
) {
3362 struct ltt_ust_session
*usess
= session
->ust_session
;
3364 if (use_tmp_output
) {
3365 int64_t nb_packets_per_stream
;
3367 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3368 tmp_output
.max_size
);
3369 if (nb_packets_per_stream
< 0) {
3370 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3373 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3374 wait
, nb_packets_per_stream
);
3375 if (ret
!= LTTNG_OK
) {
3378 snapshot_success
= 1;
3380 struct snapshot_output
*sout
;
3381 struct lttng_ht_iter iter
;
3384 cds_lfht_for_each_entry(session
->snapshot
.output_ht
->ht
,
3385 &iter
.iter
, sout
, node
.node
) {
3386 int64_t nb_packets_per_stream
;
3389 * Make a local copy of the output and assign the possible
3390 * temporary value given by the caller.
3392 memset(&tmp_output
, 0, sizeof(tmp_output
));
3393 memcpy(&tmp_output
, sout
, sizeof(tmp_output
));
3395 if (output
->max_size
!= (uint64_t) -1ULL) {
3396 tmp_output
.max_size
= output
->max_size
;
3399 nb_packets_per_stream
= get_session_nb_packets_per_stream(session
,
3400 tmp_output
.max_size
);
3401 if (nb_packets_per_stream
< 0) {
3402 ret
= LTTNG_ERR_MAX_SIZE_INVALID
;
3407 /* Use temporary name. */
3408 if (*output
->name
!= '\0') {
3409 strncpy(tmp_output
.name
, output
->name
,
3410 sizeof(tmp_output
.name
));
3413 tmp_output
.nb_snapshot
= session
->snapshot
.nb_snapshot
;
3415 ret
= record_ust_snapshot(usess
, &tmp_output
, session
,
3416 wait
, nb_packets_per_stream
);
3417 if (ret
!= LTTNG_OK
) {
3421 snapshot_success
= 1;
3427 if (snapshot_success
) {
3428 session
->snapshot
.nb_snapshot
++;
3430 ret
= LTTNG_ERR_SNAPSHOT_FAIL
;
3438 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
3440 int cmd_set_session_shm_path(struct ltt_session
*session
,
3441 const char *shm_path
)
3447 * Can only set shm path before session is started.
3449 if (session
->has_been_started
) {
3450 return LTTNG_ERR_SESSION_STARTED
;
3453 strncpy(session
->shm_path
, shm_path
,
3454 sizeof(session
->shm_path
));
3455 session
->shm_path
[sizeof(session
->shm_path
) - 1] = '\0';
3461 * Init command subsystem.
3466 * Set network sequence index to 1 for streams to match a relayd
3467 * socket on the consumer side.
3469 pthread_mutex_lock(&relayd_net_seq_idx_lock
);
3470 relayd_net_seq_idx
= 1;
3471 pthread_mutex_unlock(&relayd_net_seq_idx_lock
);
3473 DBG("Command subsystem initialized");