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.
20 #include <urcu/list.h>
21 #include <urcu/uatomic.h>
23 #include <common/defaults.h>
24 #include <common/common.h>
25 #include <common/sessiond-comm/sessiond-comm.h>
26 #include <common/relayd/relayd.h>
32 #include "kernel-consumer.h"
33 #include "lttng-sessiond.h"
39 * Used to keep a unique index for each relayd socket created where this value
40 * is associated with streams on the consumer so it can match the right relayd
43 * This value should be incremented atomically for safety purposes and future
44 * possible concurrent access.
46 static unsigned int relayd_net_seq_idx
;
49 * Create a session path used by list_lttng_sessions for the case that the
50 * session consumer is on the network.
52 static int build_network_session_path(char *dst
, size_t size
,
53 struct ltt_session
*session
)
55 int ret
, kdata_port
, udata_port
;
56 struct lttng_uri
*kuri
= NULL
, *uuri
= NULL
, *uri
= NULL
;
57 char tmp_uurl
[PATH_MAX
], tmp_urls
[PATH_MAX
];
62 memset(tmp_urls
, 0, sizeof(tmp_urls
));
63 memset(tmp_uurl
, 0, sizeof(tmp_uurl
));
65 kdata_port
= udata_port
= DEFAULT_NETWORK_DATA_PORT
;
67 if (session
->kernel_session
&& session
->kernel_session
->consumer
) {
68 kuri
= &session
->kernel_session
->consumer
->dst
.net
.control
;
69 kdata_port
= session
->kernel_session
->consumer
->dst
.net
.data
.port
;
72 if (session
->ust_session
&& session
->ust_session
->consumer
) {
73 uuri
= &session
->ust_session
->consumer
->dst
.net
.control
;
74 udata_port
= session
->ust_session
->consumer
->dst
.net
.data
.port
;
77 if (uuri
== NULL
&& kuri
== NULL
) {
78 uri
= &session
->consumer
->dst
.net
.control
;
79 kdata_port
= session
->consumer
->dst
.net
.data
.port
;
80 } else if (kuri
&& uuri
) {
81 ret
= uri_compare(kuri
, uuri
);
85 /* Build uuri URL string */
86 ret
= uri_to_str_url(uuri
, tmp_uurl
, sizeof(tmp_uurl
));
93 } else if (kuri
&& uuri
== NULL
) {
95 } else if (uuri
&& kuri
== NULL
) {
99 ret
= uri_to_str_url(uri
, tmp_urls
, sizeof(tmp_urls
));
105 * Do we have a UST url set. If yes, this means we have both kernel and UST
108 if (strlen(tmp_uurl
) > 0) {
109 ret
= snprintf(dst
, size
, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
110 tmp_urls
, kdata_port
, tmp_uurl
, udata_port
);
116 /* No kernel URI, use the UST port. */
119 ret
= snprintf(dst
, size
, "%s [data: %d]", tmp_urls
, dport
);
127 * Fill lttng_channel array of all channels.
129 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
130 struct lttng_channel
*channels
)
133 struct ltt_kernel_channel
*kchan
;
135 DBG("Listing channels for session %s", session
->name
);
138 case LTTNG_DOMAIN_KERNEL
:
139 /* Kernel channels */
140 if (session
->kernel_session
!= NULL
) {
141 cds_list_for_each_entry(kchan
,
142 &session
->kernel_session
->channel_list
.head
, list
) {
143 /* Copy lttng_channel struct to array */
144 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
145 channels
[i
].enabled
= kchan
->enabled
;
150 case LTTNG_DOMAIN_UST
:
152 struct lttng_ht_iter iter
;
153 struct ltt_ust_channel
*uchan
;
155 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
156 &iter
.iter
, uchan
, node
.node
) {
157 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
158 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
159 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
160 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
161 channels
[i
].attr
.switch_timer_interval
=
162 uchan
->attr
.switch_timer_interval
;
163 channels
[i
].attr
.read_timer_interval
=
164 uchan
->attr
.read_timer_interval
;
165 channels
[i
].enabled
= uchan
->enabled
;
166 switch (uchan
->attr
.output
) {
169 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
182 * Create a list of ust global domain events.
184 static int list_lttng_ust_global_events(char *channel_name
,
185 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
188 unsigned int nb_event
= 0;
189 struct lttng_ht_iter iter
;
190 struct lttng_ht_node_str
*node
;
191 struct ltt_ust_channel
*uchan
;
192 struct ltt_ust_event
*uevent
;
193 struct lttng_event
*tmp
;
195 DBG("Listing UST global events for channel %s", channel_name
);
199 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
200 node
= lttng_ht_iter_get_node_str(&iter
);
202 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
206 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
208 nb_event
+= lttng_ht_get_count(uchan
->events
);
215 DBG3("Listing UST global %d events", nb_event
);
217 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
219 ret
= LTTNG_ERR_FATAL
;
223 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
224 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
225 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
226 tmp
[i
].enabled
= uevent
->enabled
;
228 switch (uevent
->attr
.instrumentation
) {
229 case LTTNG_UST_TRACEPOINT
:
230 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
232 case LTTNG_UST_PROBE
:
233 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
235 case LTTNG_UST_FUNCTION
:
236 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
240 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
241 switch (uevent
->attr
.loglevel_type
) {
242 case LTTNG_UST_LOGLEVEL_ALL
:
243 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
245 case LTTNG_UST_LOGLEVEL_RANGE
:
246 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
248 case LTTNG_UST_LOGLEVEL_SINGLE
:
249 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
252 if (uevent
->filter
) {
267 * Fill lttng_event array of all kernel events in the channel.
269 static int list_lttng_kernel_events(char *channel_name
,
270 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
273 unsigned int nb_event
;
274 struct ltt_kernel_event
*event
;
275 struct ltt_kernel_channel
*kchan
;
277 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
279 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
283 nb_event
= kchan
->event_count
;
285 DBG("Listing events for channel %s", kchan
->channel
->name
);
291 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
292 if (*events
== NULL
) {
293 ret
= LTTNG_ERR_FATAL
;
297 /* Kernel channels */
298 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
299 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
300 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
301 (*events
)[i
].enabled
= event
->enabled
;
303 switch (event
->event
->instrumentation
) {
304 case LTTNG_KERNEL_TRACEPOINT
:
305 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
307 case LTTNG_KERNEL_KPROBE
:
308 case LTTNG_KERNEL_KRETPROBE
:
309 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
310 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
311 sizeof(struct lttng_kernel_kprobe
));
313 case LTTNG_KERNEL_FUNCTION
:
314 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
315 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
316 sizeof(struct lttng_kernel_function
));
318 case LTTNG_KERNEL_NOOP
:
319 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
321 case LTTNG_KERNEL_SYSCALL
:
322 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
324 case LTTNG_KERNEL_ALL
:
335 /* Negate the error code to differentiate the size from an error */
340 * Add URI so the consumer output object. Set the correct path depending on the
341 * domain adding the default trace directory.
343 static int add_uri_to_consumer(struct consumer_output
*consumer
,
344 struct lttng_uri
*uri
, int domain
, const char *session_name
)
347 const char *default_trace_dir
;
351 if (consumer
== NULL
) {
352 DBG("No consumer detected. Don't add URI. Stopping.");
353 ret
= LTTNG_ERR_NO_CONSUMER
;
358 case LTTNG_DOMAIN_KERNEL
:
359 default_trace_dir
= DEFAULT_KERNEL_TRACE_DIR
;
361 case LTTNG_DOMAIN_UST
:
362 default_trace_dir
= DEFAULT_UST_TRACE_DIR
;
366 * This case is possible is we try to add the URI to the global tracing
367 * session consumer object which in this case there is no subdir.
369 default_trace_dir
= "";
372 switch (uri
->dtype
) {
375 DBG2("Setting network URI to consumer");
377 /* Set URI into consumer output object */
378 ret
= consumer_set_network_uri(consumer
, uri
);
380 ret
= LTTNG_ERR_FATAL
;
382 } else if (ret
== 1) {
384 * URI was the same in the consumer so we do not append the subdir
385 * again so to not duplicate output dir.
390 if (uri
->stype
== LTTNG_STREAM_CONTROL
&& strlen(uri
->subdir
) == 0) {
391 ret
= consumer_set_subdir(consumer
, session_name
);
393 ret
= LTTNG_ERR_FATAL
;
398 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
399 /* On a new subdir, reappend the default trace dir. */
400 strncat(consumer
->subdir
, default_trace_dir
,
401 sizeof(consumer
->subdir
) - strlen(consumer
->subdir
) - 1);
402 DBG3("Append domain trace name to subdir %s", consumer
->subdir
);
407 DBG2("Setting trace directory path from URI to %s", uri
->dst
.path
);
408 memset(consumer
->dst
.trace_path
, 0,
409 sizeof(consumer
->dst
.trace_path
));
410 strncpy(consumer
->dst
.trace_path
, uri
->dst
.path
,
411 sizeof(consumer
->dst
.trace_path
));
412 /* Append default trace dir */
413 strncat(consumer
->dst
.trace_path
, default_trace_dir
,
414 sizeof(consumer
->dst
.trace_path
) -
415 strlen(consumer
->dst
.trace_path
) - 1);
416 /* Flag consumer as local. */
417 consumer
->type
= CONSUMER_DST_LOCAL
;
426 * Init tracing by creating trace directory and sending fds kernel consumer.
428 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
431 struct lttng_ht_iter iter
;
432 struct consumer_socket
*socket
;
436 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
437 cds_lfht_for_each_entry(session
->consumer
->socks
->ht
, &iter
.iter
,
439 /* Code flow error */
440 assert(socket
->fd
>= 0);
442 pthread_mutex_lock(socket
->lock
);
443 ret
= kernel_consumer_send_session(socket
->fd
, session
);
444 pthread_mutex_unlock(socket
->lock
);
446 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
457 * Create a socket to the relayd using the URI.
459 * On success, the relayd_sock pointer is set to the created socket.
460 * Else, it's stays untouched and a lttcomm error code is returned.
462 static int create_connect_relayd(struct consumer_output
*output
,
463 const char *session_name
, struct lttng_uri
*uri
,
464 struct lttcomm_sock
**relayd_sock
)
467 struct lttcomm_sock
*sock
;
469 /* Create socket object from URI */
470 sock
= lttcomm_alloc_sock_from_uri(uri
);
472 ret
= LTTNG_ERR_FATAL
;
476 ret
= lttcomm_create_sock(sock
);
478 ret
= LTTNG_ERR_FATAL
;
482 /* Connect to relayd so we can proceed with a session creation. */
483 ret
= relayd_connect(sock
);
485 ERR("Unable to reach lttng-relayd");
486 ret
= LTTNG_ERR_RELAYD_CONNECT_FAIL
;
490 /* Create socket for control stream. */
491 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
492 DBG3("Creating relayd stream socket from URI");
494 /* Check relayd version */
495 ret
= relayd_version_check(sock
, RELAYD_VERSION_COMM_MAJOR
,
496 RELAYD_VERSION_COMM_MINOR
);
498 ret
= LTTNG_ERR_RELAYD_VERSION_FAIL
;
501 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
502 DBG3("Creating relayd data socket from URI");
504 /* Command is not valid */
505 ERR("Relayd invalid stream type: %d", uri
->stype
);
506 ret
= LTTNG_ERR_INVALID
;
516 (void) relayd_close(sock
);
520 lttcomm_destroy_sock(sock
);
527 * Connect to the relayd using URI and send the socket to the right consumer.
529 static int send_consumer_relayd_socket(int domain
, struct ltt_session
*session
,
530 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
534 struct lttcomm_sock
*sock
= NULL
;
536 /* Set the network sequence index if not set. */
537 if (consumer
->net_seq_index
== -1) {
539 * Increment net_seq_idx because we are about to transfer the
540 * new relayd socket to the consumer.
542 uatomic_inc(&relayd_net_seq_idx
);
543 /* Assign unique key so the consumer can match streams */
544 uatomic_set(&consumer
->net_seq_index
,
545 uatomic_read(&relayd_net_seq_idx
));
548 /* Connect to relayd and make version check if uri is the control. */
549 ret
= create_connect_relayd(consumer
, session
->name
, relayd_uri
, &sock
);
550 if (ret
!= LTTNG_OK
) {
554 /* If the control socket is connected, network session is ready */
555 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
556 session
->net_handle
= 1;
559 /* Send relayd socket to consumer. */
560 ret
= consumer_send_relayd_socket(consumer_fd
, sock
,
561 consumer
, relayd_uri
->stype
);
563 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
567 /* Flag that the corresponding socket was sent. */
568 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
569 consumer
->dst
.net
.control_sock_sent
= 1;
570 } else if (relayd_uri
->stype
== LTTNG_STREAM_DATA
) {
571 consumer
->dst
.net
.data_sock_sent
= 1;
577 * Close socket which was dup on the consumer side. The session daemon does
578 * NOT keep track of the relayd socket(s) once transfer to the consumer.
583 (void) relayd_close(sock
);
584 lttcomm_destroy_sock(sock
);
591 * Send both relayd sockets to a specific consumer and domain. This is a
592 * helper function to facilitate sending the information to the consumer for a
595 static int send_consumer_relayd_sockets(int domain
,
596 struct ltt_session
*session
, struct consumer_output
*consumer
, int fd
)
603 /* Sending control relayd socket. */
604 if (!consumer
->dst
.net
.control_sock_sent
) {
605 ret
= send_consumer_relayd_socket(domain
, session
,
606 &consumer
->dst
.net
.control
, consumer
, fd
);
607 if (ret
!= LTTNG_OK
) {
612 /* Sending data relayd socket. */
613 if (!consumer
->dst
.net
.data_sock_sent
) {
614 ret
= send_consumer_relayd_socket(domain
, session
,
615 &consumer
->dst
.net
.data
, consumer
, fd
);
616 if (ret
!= LTTNG_OK
) {
626 * Setup relayd connections for a tracing session. First creates the socket to
627 * the relayd and send them to the right domain consumer. Consumer type MUST be
630 static int setup_relayd(struct ltt_session
*session
)
633 struct ltt_ust_session
*usess
;
634 struct ltt_kernel_session
*ksess
;
635 struct consumer_socket
*socket
;
636 struct lttng_ht_iter iter
;
640 usess
= session
->ust_session
;
641 ksess
= session
->kernel_session
;
643 DBG2("Setting relayd for session %s", session
->name
);
645 if (usess
&& usess
->consumer
&& usess
->consumer
->type
== CONSUMER_DST_NET
646 && usess
->consumer
->enabled
) {
647 /* For each consumer socket, send relayd sockets */
648 cds_lfht_for_each_entry(usess
->consumer
->socks
->ht
, &iter
.iter
,
650 /* Code flow error */
651 assert(socket
->fd
>= 0);
653 pthread_mutex_lock(socket
->lock
);
654 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_UST
, session
,
655 usess
->consumer
, socket
->fd
);
656 pthread_mutex_unlock(socket
->lock
);
657 if (ret
!= LTTNG_OK
) {
663 if (ksess
&& ksess
->consumer
&& ksess
->consumer
->type
== CONSUMER_DST_NET
664 && ksess
->consumer
->enabled
) {
665 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
667 /* Code flow error */
668 assert(socket
->fd
>= 0);
670 pthread_mutex_lock(socket
->lock
);
671 ret
= send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL
, session
,
672 ksess
->consumer
, socket
->fd
);
673 pthread_mutex_unlock(socket
->lock
);
674 if (ret
!= LTTNG_OK
) {
685 * Start a kernel session by opening all necessary streams.
687 static int start_kernel_session(struct ltt_kernel_session
*ksess
, int wpipe
)
690 struct ltt_kernel_channel
*kchan
;
692 /* Open kernel metadata */
693 if (ksess
->metadata
== NULL
) {
694 ret
= kernel_open_metadata(ksess
);
696 ret
= LTTNG_ERR_KERN_META_FAIL
;
701 /* Open kernel metadata stream */
702 if (ksess
->metadata_stream_fd
< 0) {
703 ret
= kernel_open_metadata_stream(ksess
);
705 ERR("Kernel create metadata stream failed");
706 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
711 /* For each channel */
712 cds_list_for_each_entry(kchan
, &ksess
->channel_list
.head
, list
) {
713 if (kchan
->stream_count
== 0) {
714 ret
= kernel_open_channel_stream(kchan
);
716 ret
= LTTNG_ERR_KERN_STREAM_FAIL
;
719 /* Update the stream global counter */
720 ksess
->stream_count_global
+= ret
;
724 /* Setup kernel consumer socket and send fds to it */
725 ret
= init_kernel_tracing(ksess
);
727 ret
= LTTNG_ERR_KERN_START_FAIL
;
731 /* This start the kernel tracing */
732 ret
= kernel_start_session(ksess
);
734 ret
= LTTNG_ERR_KERN_START_FAIL
;
738 /* Quiescent wait after starting trace */
739 kernel_wait_quiescent(kernel_tracer_fd
);
750 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
752 int cmd_disable_channel(struct ltt_session
*session
, int domain
,
756 struct ltt_ust_session
*usess
;
758 usess
= session
->ust_session
;
761 case LTTNG_DOMAIN_KERNEL
:
763 ret
= channel_kernel_disable(session
->kernel_session
,
765 if (ret
!= LTTNG_OK
) {
769 kernel_wait_quiescent(kernel_tracer_fd
);
772 case LTTNG_DOMAIN_UST
:
774 struct ltt_ust_channel
*uchan
;
775 struct lttng_ht
*chan_ht
;
777 chan_ht
= usess
->domain_global
.channels
;
779 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
781 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
785 ret
= channel_ust_disable(usess
, domain
, uchan
);
786 if (ret
!= LTTNG_OK
) {
792 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
793 case LTTNG_DOMAIN_UST_EXEC_NAME
:
794 case LTTNG_DOMAIN_UST_PID
:
797 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
808 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
810 * The wpipe arguments is used as a notifier for the kernel thread.
812 int cmd_enable_channel(struct ltt_session
*session
,
813 int domain
, struct lttng_channel
*attr
, int wpipe
)
816 struct ltt_ust_session
*usess
= session
->ust_session
;
817 struct lttng_ht
*chan_ht
;
822 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
825 case LTTNG_DOMAIN_KERNEL
:
827 struct ltt_kernel_channel
*kchan
;
829 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
830 session
->kernel_session
);
832 ret
= channel_kernel_create(session
->kernel_session
, attr
, wpipe
);
834 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
837 if (ret
!= LTTNG_OK
) {
841 kernel_wait_quiescent(kernel_tracer_fd
);
844 * If the session was previously started, start as well this newly
845 * created kernel session so the events/channels enabled *after* the
846 * start actually work.
848 if (session
->started
&& !session
->kernel_session
->started
) {
849 ret
= start_kernel_session(session
->kernel_session
, wpipe
);
850 if (ret
!= LTTNG_OK
) {
856 case LTTNG_DOMAIN_UST
:
858 struct ltt_ust_channel
*uchan
;
860 chan_ht
= usess
->domain_global
.channels
;
862 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
864 ret
= channel_ust_create(usess
, domain
, attr
);
866 ret
= channel_ust_enable(usess
, domain
, uchan
);
869 /* Start the UST session if the session was already started. */
870 if (session
->started
&& !usess
->start_trace
) {
871 ret
= ust_app_start_trace_all(usess
);
873 ret
= LTTNG_ERR_UST_START_FAIL
;
877 usess
->start_trace
= 1;
882 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
883 case LTTNG_DOMAIN_UST_EXEC_NAME
:
884 case LTTNG_DOMAIN_UST_PID
:
887 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
897 * Command LTTNG_DISABLE_EVENT processed by the client thread.
899 int cmd_disable_event(struct ltt_session
*session
, int domain
,
900 char *channel_name
, char *event_name
)
905 case LTTNG_DOMAIN_KERNEL
:
907 struct ltt_kernel_channel
*kchan
;
908 struct ltt_kernel_session
*ksess
;
910 ksess
= session
->kernel_session
;
912 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
914 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
918 ret
= event_kernel_disable_tracepoint(ksess
, kchan
, event_name
);
919 if (ret
!= LTTNG_OK
) {
923 kernel_wait_quiescent(kernel_tracer_fd
);
926 case LTTNG_DOMAIN_UST
:
928 struct ltt_ust_channel
*uchan
;
929 struct ltt_ust_session
*usess
;
931 usess
= session
->ust_session
;
933 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
936 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
940 ret
= event_ust_disable_tracepoint(usess
, domain
, uchan
, event_name
);
941 if (ret
!= LTTNG_OK
) {
945 DBG3("Disable UST event %s in channel %s completed", event_name
,
950 case LTTNG_DOMAIN_UST_EXEC_NAME
:
951 case LTTNG_DOMAIN_UST_PID
:
952 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
966 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
968 int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
974 case LTTNG_DOMAIN_KERNEL
:
976 struct ltt_kernel_session
*ksess
;
977 struct ltt_kernel_channel
*kchan
;
979 ksess
= session
->kernel_session
;
981 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
983 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
987 ret
= event_kernel_disable_all(ksess
, kchan
);
988 if (ret
!= LTTNG_OK
) {
992 kernel_wait_quiescent(kernel_tracer_fd
);
995 case LTTNG_DOMAIN_UST
:
997 struct ltt_ust_session
*usess
;
998 struct ltt_ust_channel
*uchan
;
1000 usess
= session
->ust_session
;
1002 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1004 if (uchan
== NULL
) {
1005 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
1009 ret
= event_ust_disable_all_tracepoints(usess
, domain
, uchan
);
1014 DBG3("Disable all UST events in channel %s completed", channel_name
);
1019 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1020 case LTTNG_DOMAIN_UST_PID
:
1021 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1024 ret
= LTTNG_ERR_UND
;
1035 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1037 int cmd_add_context(struct ltt_session
*session
, int domain
,
1038 char *channel_name
, char *event_name
, struct lttng_event_context
*ctx
,
1044 case LTTNG_DOMAIN_KERNEL
:
1045 assert(session
->kernel_session
);
1047 if (session
->kernel_session
->channel_count
== 0) {
1048 /* Create default channel */
1049 ret
= channel_kernel_create(session
->kernel_session
, NULL
, kwpipe
);
1050 if (ret
!= LTTNG_OK
) {
1055 /* Add kernel context to kernel tracer */
1056 ret
= context_kernel_add(session
->kernel_session
, ctx
,
1057 event_name
, channel_name
);
1058 if (ret
!= LTTNG_OK
) {
1062 case LTTNG_DOMAIN_UST
:
1064 struct ltt_ust_session
*usess
= session
->ust_session
;
1067 unsigned int chan_count
=
1068 lttng_ht_get_count(usess
->domain_global
.channels
);
1069 if (chan_count
== 0) {
1070 struct lttng_channel
*attr
;
1071 /* Create default channel */
1072 attr
= channel_new_default_attr(domain
);
1074 ret
= LTTNG_ERR_FATAL
;
1078 ret
= channel_ust_create(usess
, domain
, attr
);
1079 if (ret
!= LTTNG_OK
) {
1087 ret
= context_ust_add(usess
, domain
, ctx
, event_name
, channel_name
);
1088 if (ret
!= LTTNG_OK
) {
1094 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1095 case LTTNG_DOMAIN_UST_PID
:
1096 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1099 ret
= LTTNG_ERR_UND
;
1110 * Command LTTNG_SET_FILTER processed by the client thread.
1112 int cmd_set_filter(struct ltt_session
*session
, int domain
,
1113 char *channel_name
, char *event_name
,
1114 struct lttng_filter_bytecode
*bytecode
)
1119 case LTTNG_DOMAIN_KERNEL
:
1120 ret
= LTTNG_ERR_FATAL
;
1122 case LTTNG_DOMAIN_UST
:
1124 struct ltt_ust_session
*usess
= session
->ust_session
;
1126 ret
= filter_ust_set(usess
, domain
, bytecode
, event_name
, channel_name
);
1127 if (ret
!= LTTNG_OK
) {
1133 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1134 case LTTNG_DOMAIN_UST_PID
:
1135 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1138 ret
= LTTNG_ERR_UND
;
1151 * Command LTTNG_ENABLE_EVENT processed by the client thread.
1153 int cmd_enable_event(struct ltt_session
*session
, int domain
,
1154 char *channel_name
, struct lttng_event
*event
, int wpipe
)
1157 struct lttng_channel
*attr
;
1161 assert(channel_name
);
1164 case LTTNG_DOMAIN_KERNEL
:
1166 struct ltt_kernel_channel
*kchan
;
1168 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1169 session
->kernel_session
);
1170 if (kchan
== NULL
) {
1171 attr
= channel_new_default_attr(domain
);
1173 ret
= LTTNG_ERR_FATAL
;
1176 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1178 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1179 if (ret
!= LTTNG_OK
) {
1186 /* Get the newly created kernel channel pointer */
1187 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1188 session
->kernel_session
);
1189 if (kchan
== NULL
) {
1190 /* This sould not happen... */
1191 ret
= LTTNG_ERR_FATAL
;
1195 ret
= event_kernel_enable_tracepoint(session
->kernel_session
, kchan
,
1197 if (ret
!= LTTNG_OK
) {
1201 kernel_wait_quiescent(kernel_tracer_fd
);
1204 case LTTNG_DOMAIN_UST
:
1206 struct ltt_ust_channel
*uchan
;
1207 struct ltt_ust_session
*usess
= session
->ust_session
;
1211 /* Get channel from global UST domain */
1212 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1214 if (uchan
== NULL
) {
1215 /* Create default channel */
1216 attr
= channel_new_default_attr(domain
);
1218 ret
= LTTNG_ERR_FATAL
;
1221 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1223 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1224 if (ret
!= LTTNG_OK
) {
1230 /* Get the newly created channel reference back */
1231 uchan
= trace_ust_find_channel_by_name(
1232 usess
->domain_global
.channels
, channel_name
);
1236 /* At this point, the session and channel exist on the tracer */
1237 ret
= event_ust_enable_tracepoint(usess
, domain
, uchan
, event
);
1238 if (ret
!= LTTNG_OK
) {
1244 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1245 case LTTNG_DOMAIN_UST_PID
:
1246 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1249 ret
= LTTNG_ERR_UND
;
1260 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
1262 int cmd_enable_event_all(struct ltt_session
*session
, int domain
,
1263 char *channel_name
, int event_type
, int wpipe
)
1266 struct lttng_channel
*attr
;
1269 assert(channel_name
);
1272 case LTTNG_DOMAIN_KERNEL
:
1274 struct ltt_kernel_channel
*kchan
;
1276 assert(session
->kernel_session
);
1278 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1279 session
->kernel_session
);
1280 if (kchan
== NULL
) {
1281 /* Create default channel */
1282 attr
= channel_new_default_attr(domain
);
1284 ret
= LTTNG_ERR_FATAL
;
1287 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1289 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1290 if (ret
!= LTTNG_OK
) {
1296 /* Get the newly created kernel channel pointer */
1297 kchan
= trace_kernel_get_channel_by_name(channel_name
,
1298 session
->kernel_session
);
1302 switch (event_type
) {
1303 case LTTNG_EVENT_SYSCALL
:
1304 ret
= event_kernel_enable_all_syscalls(session
->kernel_session
,
1305 kchan
, kernel_tracer_fd
);
1307 case LTTNG_EVENT_TRACEPOINT
:
1309 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
1310 * events already registered to the channel.
1312 ret
= event_kernel_enable_all_tracepoints(session
->kernel_session
,
1313 kchan
, kernel_tracer_fd
);
1315 case LTTNG_EVENT_ALL
:
1316 /* Enable syscalls and tracepoints */
1317 ret
= event_kernel_enable_all(session
->kernel_session
,
1318 kchan
, kernel_tracer_fd
);
1321 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1325 /* Manage return value */
1326 if (ret
!= LTTNG_OK
) {
1330 kernel_wait_quiescent(kernel_tracer_fd
);
1333 case LTTNG_DOMAIN_UST
:
1335 struct ltt_ust_channel
*uchan
;
1336 struct ltt_ust_session
*usess
= session
->ust_session
;
1340 /* Get channel from global UST domain */
1341 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
1343 if (uchan
== NULL
) {
1344 /* Create default channel */
1345 attr
= channel_new_default_attr(domain
);
1347 ret
= LTTNG_ERR_FATAL
;
1350 strncpy(attr
->name
, channel_name
, sizeof(attr
->name
));
1352 ret
= cmd_enable_channel(session
, domain
, attr
, wpipe
);
1353 if (ret
!= LTTNG_OK
) {
1359 /* Get the newly created channel reference back */
1360 uchan
= trace_ust_find_channel_by_name(
1361 usess
->domain_global
.channels
, channel_name
);
1365 /* At this point, the session and channel exist on the tracer */
1367 switch (event_type
) {
1368 case LTTNG_EVENT_ALL
:
1369 case LTTNG_EVENT_TRACEPOINT
:
1370 ret
= event_ust_enable_all_tracepoints(usess
, domain
, uchan
);
1371 if (ret
!= LTTNG_OK
) {
1376 ret
= LTTNG_ERR_UST_ENABLE_FAIL
;
1380 /* Manage return value */
1381 if (ret
!= LTTNG_OK
) {
1388 case LTTNG_DOMAIN_UST_EXEC_NAME
:
1389 case LTTNG_DOMAIN_UST_PID
:
1390 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
1393 ret
= LTTNG_ERR_UND
;
1405 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
1407 ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
1410 ssize_t nb_events
= 0;
1413 case LTTNG_DOMAIN_KERNEL
:
1414 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
1415 if (nb_events
< 0) {
1416 ret
= LTTNG_ERR_KERN_LIST_FAIL
;
1420 case LTTNG_DOMAIN_UST
:
1421 nb_events
= ust_app_list_events(events
);
1422 if (nb_events
< 0) {
1423 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1428 ret
= LTTNG_ERR_UND
;
1435 /* Return negative value to differentiate return code */
1440 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
1442 ssize_t
cmd_list_tracepoint_fields(int domain
,
1443 struct lttng_event_field
**fields
)
1446 ssize_t nb_fields
= 0;
1449 case LTTNG_DOMAIN_UST
:
1450 nb_fields
= ust_app_list_event_fields(fields
);
1451 if (nb_fields
< 0) {
1452 ret
= LTTNG_ERR_UST_LIST_FAIL
;
1456 case LTTNG_DOMAIN_KERNEL
:
1457 default: /* fall-through */
1458 ret
= LTTNG_ERR_UND
;
1465 /* Return negative value to differentiate return code */
1470 * Command LTTNG_START_TRACE processed by the client thread.
1472 int cmd_start_trace(struct ltt_session
*session
)
1475 struct ltt_kernel_session
*ksession
;
1476 struct ltt_ust_session
*usess
;
1480 /* Ease our life a bit ;) */
1481 ksession
= session
->kernel_session
;
1482 usess
= session
->ust_session
;
1484 if (session
->enabled
) {
1485 /* Already started. */
1486 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1490 session
->enabled
= 1;
1492 ret
= setup_relayd(session
);
1493 if (ret
!= LTTNG_OK
) {
1494 ERR("Error setting up relayd for session %s", session
->name
);
1498 /* Kernel tracing */
1499 if (ksession
!= NULL
) {
1500 ret
= start_kernel_session(ksession
, kernel_tracer_fd
);
1501 if (ret
!= LTTNG_OK
) {
1506 /* Flag session that trace should start automatically */
1508 usess
->start_trace
= 1;
1510 ret
= ust_app_start_trace_all(usess
);
1512 ret
= LTTNG_ERR_UST_START_FAIL
;
1517 session
->started
= 1;
1526 * Command LTTNG_STOP_TRACE processed by the client thread.
1528 int cmd_stop_trace(struct ltt_session
*session
)
1531 struct ltt_kernel_channel
*kchan
;
1532 struct ltt_kernel_session
*ksession
;
1533 struct ltt_ust_session
*usess
;
1538 ksession
= session
->kernel_session
;
1539 usess
= session
->ust_session
;
1541 if (!session
->enabled
) {
1542 ret
= LTTNG_ERR_TRACE_ALREADY_STOPPED
;
1546 session
->enabled
= 0;
1550 DBG("Stop kernel tracing");
1552 /* Flush metadata if exist */
1553 if (ksession
->metadata_stream_fd
>= 0) {
1554 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
1556 ERR("Kernel metadata flush failed");
1560 /* Flush all buffers before stopping */
1561 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
1562 ret
= kernel_flush_buffer(kchan
);
1564 ERR("Kernel flush buffer error");
1568 ret
= kernel_stop_session(ksession
);
1570 ret
= LTTNG_ERR_KERN_STOP_FAIL
;
1574 kernel_wait_quiescent(kernel_tracer_fd
);
1576 ksession
->started
= 0;
1580 usess
->start_trace
= 0;
1582 ret
= ust_app_stop_trace_all(usess
);
1584 ret
= LTTNG_ERR_UST_STOP_FAIL
;
1589 session
->started
= 0;
1598 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
1600 int cmd_set_consumer_uri(int domain
, struct ltt_session
*session
,
1601 size_t nb_uri
, struct lttng_uri
*uris
)
1604 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1605 struct ltt_ust_session
*usess
= session
->ust_session
;
1606 struct consumer_output
*consumer
= NULL
;
1612 /* Can't enable consumer after session started. */
1613 if (session
->enabled
) {
1614 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
1619 * This case switch makes sure the domain session has a temporary consumer
1620 * so the URL can be set.
1624 /* Code flow error. A session MUST always have a consumer object */
1625 assert(session
->consumer
);
1627 * The URL will be added to the tracing session consumer instead of a
1628 * specific domain consumer.
1630 consumer
= session
->consumer
;
1632 case LTTNG_DOMAIN_KERNEL
:
1633 /* Code flow error if we don't have a kernel session here. */
1636 /* Create consumer output if none exists */
1637 consumer
= ksess
->tmp_consumer
;
1638 if (consumer
== NULL
) {
1639 consumer
= consumer_copy_output(ksess
->consumer
);
1640 if (consumer
== NULL
) {
1641 ret
= LTTNG_ERR_FATAL
;
1644 /* Trash the consumer subdir, we are about to set a new one. */
1645 memset(consumer
->subdir
, 0, sizeof(consumer
->subdir
));
1646 ksess
->tmp_consumer
= consumer
;
1650 case LTTNG_DOMAIN_UST
:
1651 /* Code flow error if we don't have a kernel session here. */
1654 /* Create consumer output if none exists */
1655 consumer
= usess
->tmp_consumer
;
1656 if (consumer
== NULL
) {
1657 consumer
= consumer_copy_output(usess
->consumer
);
1658 if (consumer
== NULL
) {
1659 ret
= LTTNG_ERR_FATAL
;
1662 /* Trash the consumer subdir, we are about to set a new one. */
1663 memset(consumer
->subdir
, 0, sizeof(consumer
->subdir
));
1664 usess
->tmp_consumer
= consumer
;
1670 for (i
= 0; i
< nb_uri
; i
++) {
1671 struct consumer_socket
*socket
;
1672 struct lttng_ht_iter iter
;
1674 ret
= add_uri_to_consumer(consumer
, &uris
[i
], domain
, session
->name
);
1680 * Don't send relayd socket if URI is NOT remote or if the relayd
1681 * socket for the session was already sent.
1683 if (uris
[i
].dtype
== LTTNG_DST_PATH
||
1684 (uris
[i
].stype
== LTTNG_STREAM_CONTROL
&&
1685 consumer
->dst
.net
.control_sock_sent
) ||
1686 (uris
[i
].stype
== LTTNG_STREAM_DATA
&&
1687 consumer
->dst
.net
.data_sock_sent
)) {
1691 /* Try to send relayd URI to the consumer if exist. */
1693 cds_lfht_for_each_entry(consumer
->socks
->ht
, &iter
.iter
,
1694 socket
, node
.node
) {
1696 /* A socket in the HT should never have a negative fd */
1697 assert(socket
->fd
>= 0);
1699 pthread_mutex_lock(socket
->lock
);
1700 ret
= send_consumer_relayd_socket(domain
, session
, &uris
[i
],
1701 consumer
, socket
->fd
);
1702 pthread_mutex_unlock(socket
->lock
);
1703 if (ret
!= LTTNG_OK
) {
1719 * Command LTTNG_CREATE_SESSION processed by the client thread.
1721 int cmd_create_session_uri(char *name
, struct lttng_uri
*uris
,
1722 size_t nb_uri
, lttng_sock_cred
*creds
)
1726 struct ltt_session
*session
;
1731 * Verify if the session already exist
1733 * XXX: There is no need for the session lock list here since the caller
1734 * (process_client_msg) is holding it. We might want to change that so a
1735 * single command does not lock the entire session list.
1737 session
= session_find_by_name(name
);
1738 if (session
!= NULL
) {
1739 ret
= LTTNG_ERR_EXIST_SESS
;
1743 /* Create tracing session in the registry */
1744 ret
= session_create(name
, path
, LTTNG_SOCK_GET_UID_CRED(creds
),
1745 LTTNG_SOCK_GET_GID_CRED(creds
));
1746 if (ret
!= LTTNG_OK
) {
1751 * Get the newly created session pointer back
1753 * XXX: There is no need for the session lock list here since the caller
1754 * (process_client_msg) is holding it. We might want to change that so a
1755 * single command does not lock the entire session list.
1757 session
= session_find_by_name(name
);
1760 /* Create default consumer output for the session not yet created. */
1761 session
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
1762 if (session
->consumer
== NULL
) {
1763 ret
= LTTNG_ERR_FATAL
;
1764 goto consumer_error
;
1768 * This means that the lttng_create_session call was called with the _path_
1769 * argument set to NULL.
1773 * At this point, we'll skip the consumer URI setup and create a
1774 * session with a NULL path which will flag the session to NOT spawn a
1777 DBG("Create session %s with NO uri, skipping consumer setup", name
);
1781 session
->start_consumer
= 1;
1783 ret
= cmd_set_consumer_uri(0, session
, nb_uri
, uris
);
1784 if (ret
!= LTTNG_OK
) {
1785 goto consumer_error
;
1788 session
->consumer
->enabled
= 1;
1794 session_destroy(session
);
1801 * Command LTTNG_DESTROY_SESSION processed by the client thread.
1803 int cmd_destroy_session(struct ltt_session
*session
, int wpipe
)
1806 struct ltt_ust_session
*usess
;
1807 struct ltt_kernel_session
*ksess
;
1812 usess
= session
->ust_session
;
1813 ksess
= session
->kernel_session
;
1815 /* Clean kernel session teardown */
1816 kernel_destroy_session(ksess
);
1818 /* UST session teardown */
1820 /* Close any relayd session */
1821 consumer_output_send_destroy_relayd(usess
->consumer
);
1823 /* Destroy every UST application related to this session. */
1824 ret
= ust_app_destroy_trace_all(usess
);
1826 ERR("Error in ust_app_destroy_trace_all");
1829 /* Clean up the rest. */
1830 trace_ust_destroy_session(usess
);
1834 * Must notify the kernel thread here to update it's poll set in order to
1835 * remove the channel(s)' fd just destroyed.
1837 ret
= notify_thread_pipe(wpipe
);
1839 PERROR("write kernel poll pipe");
1842 ret
= session_destroy(session
);
1848 * Command LTTNG_CALIBRATE processed by the client thread.
1850 int cmd_calibrate(int domain
, struct lttng_calibrate
*calibrate
)
1855 case LTTNG_DOMAIN_KERNEL
:
1857 struct lttng_kernel_calibrate kcalibrate
;
1859 kcalibrate
.type
= calibrate
->type
;
1860 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
1862 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
1867 case LTTNG_DOMAIN_UST
:
1869 struct lttng_ust_calibrate ucalibrate
;
1871 ucalibrate
.type
= calibrate
->type
;
1872 ret
= ust_app_calibrate_glb(&ucalibrate
);
1874 ret
= LTTNG_ERR_UST_CALIBRATE_FAIL
;
1880 ret
= LTTNG_ERR_UND
;
1891 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
1893 int cmd_register_consumer(struct ltt_session
*session
, int domain
,
1894 const char *sock_path
, struct consumer_data
*cdata
)
1897 struct consumer_socket
*socket
;
1904 case LTTNG_DOMAIN_KERNEL
:
1906 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1910 /* Can't register a consumer if there is already one */
1911 if (ksess
->consumer_fds_sent
!= 0) {
1912 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1916 sock
= lttcomm_connect_unix_sock(sock_path
);
1918 ret
= LTTNG_ERR_CONNECT_FAIL
;
1922 socket
= consumer_allocate_socket(sock
);
1923 if (socket
== NULL
) {
1924 ret
= LTTNG_ERR_FATAL
;
1929 socket
->lock
= zmalloc(sizeof(pthread_mutex_t
));
1930 if (socket
->lock
== NULL
) {
1931 PERROR("zmalloc pthread mutex");
1932 ret
= LTTNG_ERR_FATAL
;
1935 pthread_mutex_init(socket
->lock
, NULL
);
1936 socket
->registered
= 1;
1939 consumer_add_socket(socket
, ksess
->consumer
);
1942 pthread_mutex_lock(&cdata
->pid_mutex
);
1944 pthread_mutex_unlock(&cdata
->pid_mutex
);
1949 /* TODO: Userspace tracing */
1950 ret
= LTTNG_ERR_UND
;
1961 * Command LTTNG_LIST_DOMAINS processed by the client thread.
1963 ssize_t
cmd_list_domains(struct ltt_session
*session
,
1964 struct lttng_domain
**domains
)
1969 if (session
->kernel_session
!= NULL
) {
1970 DBG3("Listing domains found kernel domain");
1974 if (session
->ust_session
!= NULL
) {
1975 DBG3("Listing domains found UST global domain");
1979 *domains
= zmalloc(nb_dom
* sizeof(struct lttng_domain
));
1980 if (*domains
== NULL
) {
1981 ret
= LTTNG_ERR_FATAL
;
1985 if (session
->kernel_session
!= NULL
) {
1986 (*domains
)[index
].type
= LTTNG_DOMAIN_KERNEL
;
1990 if (session
->ust_session
!= NULL
) {
1991 (*domains
)[index
].type
= LTTNG_DOMAIN_UST
;
1998 /* Return negative value to differentiate return code */
2004 * Command LTTNG_LIST_CHANNELS processed by the client thread.
2006 ssize_t
cmd_list_channels(int domain
, struct ltt_session
*session
,
2007 struct lttng_channel
**channels
)
2010 ssize_t nb_chan
= 0;
2013 case LTTNG_DOMAIN_KERNEL
:
2014 if (session
->kernel_session
!= NULL
) {
2015 nb_chan
= session
->kernel_session
->channel_count
;
2017 DBG3("Number of kernel channels %zd", nb_chan
);
2019 ret
= LTTNG_ERR_KERN_CHAN_NOT_FOUND
;
2022 case LTTNG_DOMAIN_UST
:
2023 if (session
->ust_session
!= NULL
) {
2024 nb_chan
= lttng_ht_get_count(
2025 session
->ust_session
->domain_global
.channels
);
2027 DBG3("Number of UST global channels %zd", nb_chan
);
2029 ret
= LTTNG_ERR_UST_CHAN_NOT_FOUND
;
2034 ret
= LTTNG_ERR_UND
;
2039 *channels
= zmalloc(nb_chan
* sizeof(struct lttng_channel
));
2040 if (*channels
== NULL
) {
2041 ret
= LTTNG_ERR_FATAL
;
2045 list_lttng_channels(domain
, session
, *channels
);
2048 /* Ret value was set in the domain switch case */
2055 /* Return negative value to differentiate return code */
2060 * Command LTTNG_LIST_EVENTS processed by the client thread.
2062 ssize_t
cmd_list_events(int domain
, struct ltt_session
*session
,
2063 char *channel_name
, struct lttng_event
**events
)
2066 ssize_t nb_event
= 0;
2069 case LTTNG_DOMAIN_KERNEL
:
2070 if (session
->kernel_session
!= NULL
) {
2071 nb_event
= list_lttng_kernel_events(channel_name
,
2072 session
->kernel_session
, events
);
2075 case LTTNG_DOMAIN_UST
:
2077 if (session
->ust_session
!= NULL
) {
2078 nb_event
= list_lttng_ust_global_events(channel_name
,
2079 &session
->ust_session
->domain_global
, events
);
2084 ret
= LTTNG_ERR_UND
;
2091 /* Return negative value to differentiate return code */
2096 * Using the session list, filled a lttng_session array to send back to the
2097 * client for session listing.
2099 * The session list lock MUST be acquired before calling this function. Use
2100 * session_lock_list() and session_unlock_list().
2102 void cmd_list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2107 struct ltt_session
*session
;
2108 struct ltt_session_list
*list
= session_get_list();
2110 DBG("Getting all available session for UID %d GID %d",
2113 * Iterate over session list and append data after the control struct in
2116 cds_list_for_each_entry(session
, &list
->head
, list
) {
2118 * Only list the sessions the user can control.
2120 if (!session_access_ok(session
, uid
, gid
)) {
2124 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2125 struct ltt_ust_session
*usess
= session
->ust_session
;
2127 if (session
->consumer
->type
== CONSUMER_DST_NET
||
2128 (ksess
&& ksess
->consumer
->type
== CONSUMER_DST_NET
) ||
2129 (usess
&& usess
->consumer
->type
== CONSUMER_DST_NET
)) {
2130 ret
= build_network_session_path(sessions
[i
].path
,
2131 sizeof(session
[i
].path
), session
);
2133 ret
= snprintf(sessions
[i
].path
, sizeof(session
[i
].path
), "%s",
2134 session
->consumer
->dst
.trace_path
);
2137 PERROR("snprintf session path");
2141 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2142 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2143 sessions
[i
].enabled
= session
->enabled
;
2149 * Command LTTNG_DISABLE_CONSUMER processed by the client thread.
2151 int cmd_disable_consumer(int domain
, struct ltt_session
*session
)
2154 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2155 struct ltt_ust_session
*usess
= session
->ust_session
;
2156 struct consumer_output
*consumer
;
2160 if (session
->enabled
) {
2161 /* Can't disable consumer on an already started session */
2162 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2166 if (!session
->start_consumer
) {
2167 ret
= LTTNG_ERR_NO_CONSUMER
;
2173 DBG("Disable tracing session %s consumer", session
->name
);
2174 consumer
= session
->consumer
;
2176 case LTTNG_DOMAIN_KERNEL
:
2177 /* Code flow error if we don't have a kernel session here. */
2180 DBG("Disabling kernel consumer");
2181 consumer
= ksess
->consumer
;
2184 case LTTNG_DOMAIN_UST
:
2185 /* Code flow error if we don't have a UST session here. */
2188 DBG("Disabling UST consumer");
2189 consumer
= usess
->consumer
;
2193 ret
= LTTNG_ERR_UNKNOWN_DOMAIN
;
2198 consumer
->enabled
= 0;
2199 /* Success at this point */
2202 ret
= LTTNG_ERR_NO_CONSUMER
;
2210 * Command LTTNG_ENABLE_CONSUMER processed by the client thread.
2212 int cmd_enable_consumer(int domain
, struct ltt_session
*session
)
2215 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2216 struct ltt_ust_session
*usess
= session
->ust_session
;
2217 struct consumer_output
*consumer
= NULL
;
2221 /* Can't enable consumer after session started. */
2222 if (session
->enabled
) {
2223 ret
= LTTNG_ERR_TRACE_ALREADY_STARTED
;
2229 assert(session
->consumer
);
2230 consumer
= session
->consumer
;
2232 case LTTNG_DOMAIN_KERNEL
:
2233 /* Code flow error if we don't have a kernel session here. */
2237 * Check if we have already sent fds to the consumer. In that case,
2238 * the enable-consumer command can't be used because a start trace
2239 * had previously occured.
2241 if (ksess
->consumer_fds_sent
) {
2242 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
2246 consumer
= ksess
->tmp_consumer
;
2247 if (consumer
== NULL
) {
2249 /* No temp. consumer output exists. Using the current one. */
2250 DBG3("No temporary consumer. Using default");
2251 consumer
= ksess
->consumer
;
2255 switch (consumer
->type
) {
2256 case CONSUMER_DST_LOCAL
:
2257 DBG2("Consumer output is local. Creating directory(ies)");
2259 /* Create directory(ies) */
2260 ret
= run_as_mkdir_recursive(consumer
->dst
.trace_path
,
2261 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2263 if (ret
!= -EEXIST
) {
2264 ERR("Trace directory creation error");
2265 ret
= LTTNG_ERR_FATAL
;
2270 case CONSUMER_DST_NET
:
2271 DBG2("Consumer output is network. Validating URIs");
2272 /* Validate if we have both control and data path set. */
2273 if (!consumer
->dst
.net
.control_isset
) {
2274 ret
= LTTNG_ERR_URL_CTRL_MISS
;
2278 if (!consumer
->dst
.net
.data_isset
) {
2279 ret
= LTTNG_ERR_URL_DATA_MISS
;
2283 /* Check established network session state */
2284 if (session
->net_handle
== 0) {
2285 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
2286 ERR("Session network handle is not set on enable-consumer");
2295 * This is race free for now since the session lock is acquired before
2296 * ending up in this function. No other threads can access this kernel
2297 * session without this lock hence freeing the consumer output object
2301 /* Destroy current consumer. We are about to replace it */
2302 consumer_destroy_output(ksess
->consumer
);
2304 ksess
->consumer
= consumer
;
2305 ksess
->tmp_consumer
= NULL
;
2308 case LTTNG_DOMAIN_UST
:
2309 /* Code flow error if we don't have a UST session here. */
2313 * Check if we have already sent fds to the consumer. In that case,
2314 * the enable-consumer command can't be used because a start trace
2315 * had previously occured.
2317 if (usess
->start_trace
) {
2318 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
2322 consumer
= usess
->tmp_consumer
;
2323 if (consumer
== NULL
) {
2325 /* No temp. consumer output exists. Using the current one. */
2326 DBG3("No temporary consumer. Using default");
2327 consumer
= usess
->consumer
;
2331 switch (consumer
->type
) {
2332 case CONSUMER_DST_LOCAL
:
2333 DBG2("Consumer output is local. Creating directory(ies)");
2335 /* Create directory(ies) */
2336 ret
= run_as_mkdir_recursive(consumer
->dst
.trace_path
,
2337 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2339 if (ret
!= -EEXIST
) {
2340 ERR("Trace directory creation error");
2341 ret
= LTTNG_ERR_FATAL
;
2346 case CONSUMER_DST_NET
:
2347 DBG2("Consumer output is network. Validating URIs");
2348 /* Validate if we have both control and data path set. */
2349 if (!consumer
->dst
.net
.control_isset
) {
2350 ret
= LTTNG_ERR_URL_CTRL_MISS
;
2354 if (!consumer
->dst
.net
.data_isset
) {
2355 ret
= LTTNG_ERR_URL_DATA_MISS
;
2359 /* Check established network session state */
2360 if (session
->net_handle
== 0) {
2361 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
2362 DBG2("Session network handle is not set on enable-consumer");
2366 if (consumer
->net_seq_index
== -1) {
2367 ret
= LTTNG_ERR_ENABLE_CONSUMER_FAIL
;
2368 DBG2("Network index is not set on the consumer");
2377 * This is race free for now since the session lock is acquired before
2378 * ending up in this function. No other threads can access this kernel
2379 * session without this lock hence freeing the consumer output object
2383 /* Destroy current consumer. We are about to replace it */
2384 consumer_destroy_output(usess
->consumer
);
2386 usess
->consumer
= consumer
;
2387 usess
->tmp_consumer
= NULL
;
2392 session
->start_consumer
= 1;
2396 consumer
->enabled
= 1;
2397 /* Success at this point */
2400 /* Should not really happend... */
2401 ret
= LTTNG_ERR_NO_CONSUMER
;
2409 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
2410 * ready for trace analysis (or anykind of reader) or else 1 for pending data.
2412 int cmd_data_pending(struct ltt_session
*session
)
2415 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2416 struct ltt_ust_session
*usess
= session
->ust_session
;
2420 /* Session MUST be stopped to ask for data availability. */
2421 if (session
->enabled
) {
2422 ret
= LTTNG_ERR_SESSION_STARTED
;
2426 if (ksess
&& ksess
->consumer
) {
2427 ret
= consumer_is_data_pending(ksess
->id
, ksess
->consumer
);
2429 /* Data is still being extracted for the kernel. */
2434 if (usess
&& usess
->consumer
) {
2435 ret
= consumer_is_data_pending(usess
->id
, usess
->consumer
);
2437 /* Data is still being extracted for the kernel. */
2442 /* Data is ready to be read by a viewer */
2450 * Init command subsystem.
2455 * Set network sequence index to 1 for streams to match a relayd socket on
2456 * the consumer side.
2458 uatomic_set(&relayd_net_seq_idx
, 1);
2460 DBG("Command subsystem initialized");