2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <semaphore.h>
30 #include <sys/mount.h>
31 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
36 #include <urcu/uatomic.h>
40 #include <common/common.h>
41 #include <common/compat/poll.h>
42 #include <common/compat/socket.h>
43 #include <common/defaults.h>
44 #include <common/kernel-consumer/kernel-consumer.h>
45 #include <common/futex.h>
46 #include <common/relayd/relayd.h>
48 #include "lttng-sessiond.h"
54 #include "kernel-consumer.h"
58 #include "ust-consumer.h"
62 #define CONSUMERD_FILE "lttng-consumerd"
65 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
66 const char default_tracing_group
[] = DEFAULT_TRACING_GROUP
;
67 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
68 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
71 const char *opt_tracing_group
;
72 static int opt_sig_parent
;
73 static int opt_verbose_consumer
;
74 static int opt_daemon
;
75 static int opt_no_kernel
;
76 static int is_root
; /* Set to 1 if the daemon is running as root */
77 static pid_t ppid
; /* Parent PID for --sig-parent option */
80 /* Consumer daemon specific control data */
81 static struct consumer_data kconsumer_data
= {
82 .type
= LTTNG_CONSUMER_KERNEL
,
83 .err_unix_sock_path
= DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
84 .cmd_unix_sock_path
= DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
88 static struct consumer_data ustconsumer64_data
= {
89 .type
= LTTNG_CONSUMER64_UST
,
90 .err_unix_sock_path
= DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
91 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
95 static struct consumer_data ustconsumer32_data
= {
96 .type
= LTTNG_CONSUMER32_UST
,
97 .err_unix_sock_path
= DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
98 .cmd_unix_sock_path
= DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
103 static int dispatch_thread_exit
;
105 /* Global application Unix socket path */
106 static char apps_unix_sock_path
[PATH_MAX
];
107 /* Global client Unix socket path */
108 static char client_unix_sock_path
[PATH_MAX
];
109 /* global wait shm path for UST */
110 static char wait_shm_path
[PATH_MAX
];
112 /* Sockets and FDs */
113 static int client_sock
= -1;
114 static int apps_sock
= -1;
115 static int kernel_tracer_fd
= -1;
116 static int kernel_poll_pipe
[2] = { -1, -1 };
119 * Quit pipe for all threads. This permits a single cancellation point
120 * for all threads when receiving an event on the pipe.
122 static int thread_quit_pipe
[2] = { -1, -1 };
125 * This pipe is used to inform the thread managing application communication
126 * that a command is queued and ready to be processed.
128 static int apps_cmd_pipe
[2] = { -1, -1 };
130 /* Pthread, Mutexes and Semaphores */
131 static pthread_t apps_thread
;
132 static pthread_t reg_apps_thread
;
133 static pthread_t client_thread
;
134 static pthread_t kernel_thread
;
135 static pthread_t dispatch_thread
;
138 * UST registration command queue. This queue is tied with a futex and uses a N
139 * wakers / 1 waiter implemented and detailed in futex.c/.h
141 * The thread_manage_apps and thread_dispatch_ust_registration interact with
142 * this queue and the wait/wake scheme.
144 static struct ust_cmd_queue ust_cmd_queue
;
147 * Pointer initialized before thread creation.
149 * This points to the tracing session list containing the session count and a
150 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
151 * MUST NOT be taken if you call a public function in session.c.
153 * The lock is nested inside the structure: session_list_ptr->lock. Please use
154 * session_lock_list and session_unlock_list for lock acquisition.
156 static struct ltt_session_list
*session_list_ptr
;
158 int ust_consumerd64_fd
= -1;
159 int ust_consumerd32_fd
= -1;
161 static const char *consumerd32_bin
= CONFIG_CONSUMERD32_BIN
;
162 static const char *consumerd64_bin
= CONFIG_CONSUMERD64_BIN
;
163 static const char *consumerd32_libdir
= CONFIG_CONSUMERD32_LIBDIR
;
164 static const char *consumerd64_libdir
= CONFIG_CONSUMERD64_LIBDIR
;
167 * Consumer daemon state which is changed when spawning it, killing it or in
168 * case of a fatal error.
170 enum consumerd_state
{
171 CONSUMER_STARTED
= 1,
172 CONSUMER_STOPPED
= 2,
177 * This consumer daemon state is used to validate if a client command will be
178 * able to reach the consumer. If not, the client is informed. For instance,
179 * doing a "lttng start" when the consumer state is set to ERROR will return an
180 * error to the client.
182 * The following example shows a possible race condition of this scheme:
184 * consumer thread error happens
186 * client cmd checks state -> still OK
187 * consumer thread exit, sets error
188 * client cmd try to talk to consumer
191 * However, since the consumer is a different daemon, we have no way of making
192 * sure the command will reach it safely even with this state flag. This is why
193 * we consider that up to the state validation during command processing, the
194 * command is safe. After that, we can not guarantee the correctness of the
195 * client request vis-a-vis the consumer.
197 static enum consumerd_state ust_consumerd_state
;
198 static enum consumerd_state kernel_consumerd_state
;
201 * Used to keep a unique index for each relayd socket created where this value
202 * is associated with streams on the consumer so it can match the right relayd
205 * This value should be incremented atomically for safety purposes and future
206 * possible concurrent access.
208 static unsigned int relayd_net_seq_idx
;
211 void setup_consumerd_path(void)
213 const char *bin
, *libdir
;
216 * Allow INSTALL_BIN_PATH to be used as a target path for the
217 * native architecture size consumer if CONFIG_CONSUMER*_PATH
218 * has not been defined.
220 #if (CAA_BITS_PER_LONG == 32)
221 if (!consumerd32_bin
[0]) {
222 consumerd32_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
224 if (!consumerd32_libdir
[0]) {
225 consumerd32_libdir
= INSTALL_LIB_PATH
;
227 #elif (CAA_BITS_PER_LONG == 64)
228 if (!consumerd64_bin
[0]) {
229 consumerd64_bin
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
231 if (!consumerd64_libdir
[0]) {
232 consumerd64_libdir
= INSTALL_LIB_PATH
;
235 #error "Unknown bitness"
239 * runtime env. var. overrides the build default.
241 bin
= getenv("LTTNG_CONSUMERD32_BIN");
243 consumerd32_bin
= bin
;
245 bin
= getenv("LTTNG_CONSUMERD64_BIN");
247 consumerd64_bin
= bin
;
249 libdir
= getenv("LTTNG_CONSUMERD32_LIBDIR");
251 consumerd32_libdir
= libdir
;
253 libdir
= getenv("LTTNG_CONSUMERD64_LIBDIR");
255 consumerd64_libdir
= libdir
;
260 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
262 static int create_thread_poll_set(struct lttng_poll_event
*events
,
267 if (events
== NULL
|| size
== 0) {
272 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
278 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
290 * Check if the thread quit pipe was triggered.
292 * Return 1 if it was triggered else 0;
294 static int check_thread_quit_pipe(int fd
, uint32_t events
)
296 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
304 * Return group ID of the tracing group or -1 if not found.
306 static gid_t
allowed_group(void)
310 if (opt_tracing_group
) {
311 grp
= getgrnam(opt_tracing_group
);
313 grp
= getgrnam(default_tracing_group
);
323 * Init thread quit pipe.
325 * Return -1 on error or 0 if all pipes are created.
327 static int init_thread_quit_pipe(void)
331 ret
= pipe(thread_quit_pipe
);
333 PERROR("thread quit pipe");
337 for (i
= 0; i
< 2; i
++) {
338 ret
= fcntl(thread_quit_pipe
[i
], F_SETFD
, FD_CLOEXEC
);
350 * Complete teardown of a kernel session. This free all data structure related
351 * to a kernel session and update counter.
353 static void teardown_kernel_session(struct ltt_session
*session
)
355 if (!session
->kernel_session
) {
356 DBG3("No kernel session when tearing down session");
360 DBG("Tearing down kernel session");
363 * If a custom kernel consumer was registered, close the socket before
364 * tearing down the complete kernel session structure
366 if (kconsumer_data
.cmd_sock
>= 0 &&
367 session
->kernel_session
->consumer_fd
!= kconsumer_data
.cmd_sock
) {
368 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
371 trace_kernel_destroy_session(session
->kernel_session
);
375 * Complete teardown of all UST sessions. This will free everything on his path
376 * and destroy the core essence of all ust sessions :)
378 static void teardown_ust_session(struct ltt_session
*session
)
382 if (!session
->ust_session
) {
383 DBG3("No UST session when tearing down session");
387 DBG("Tearing down UST session(s)");
389 ret
= ust_app_destroy_trace_all(session
->ust_session
);
391 ERR("Error in ust_app_destroy_trace_all");
394 trace_ust_destroy_session(session
->ust_session
);
398 * Stop all threads by closing the thread quit pipe.
400 static void stop_threads(void)
404 /* Stopping all threads */
405 DBG("Terminating all threads");
406 ret
= notify_thread_pipe(thread_quit_pipe
[1]);
408 ERR("write error on thread quit pipe");
411 /* Dispatch thread */
412 dispatch_thread_exit
= 1;
413 futex_nto1_wake(&ust_cmd_queue
.futex
);
419 static void cleanup(void)
423 struct ltt_session
*sess
, *stmp
;
427 DBG("Removing %s directory", rundir
);
428 ret
= asprintf(&cmd
, "rm -rf %s", rundir
);
430 ERR("asprintf failed. Something is really wrong!");
433 /* Remove lttng run directory */
436 ERR("Unable to clean %s", rundir
);
440 DBG("Cleaning up all sessions");
442 /* Destroy session list mutex */
443 if (session_list_ptr
!= NULL
) {
444 pthread_mutex_destroy(&session_list_ptr
->lock
);
446 /* Cleanup ALL session */
447 cds_list_for_each_entry_safe(sess
, stmp
,
448 &session_list_ptr
->head
, list
) {
449 teardown_kernel_session(sess
);
450 teardown_ust_session(sess
);
455 DBG("Closing all UST sockets");
456 ust_app_clean_list();
458 pthread_mutex_destroy(&kconsumer_data
.pid_mutex
);
460 if (is_root
&& !opt_no_kernel
) {
461 DBG2("Closing kernel fd");
462 if (kernel_tracer_fd
>= 0) {
463 ret
= close(kernel_tracer_fd
);
468 DBG("Unloading kernel modules");
469 modprobe_remove_lttng_all();
471 utils_close_pipe(kernel_poll_pipe
);
472 utils_close_pipe(thread_quit_pipe
);
473 utils_close_pipe(apps_cmd_pipe
);
476 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
477 "Matthew, BEET driven development works!%c[%dm",
478 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
483 * Send data on a unix socket using the liblttsessiondcomm API.
485 * Return lttcomm error code.
487 static int send_unix_sock(int sock
, void *buf
, size_t len
)
489 /* Check valid length */
494 return lttcomm_send_unix_sock(sock
, buf
, len
);
498 * Free memory of a command context structure.
500 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
502 DBG("Clean command context structure");
504 if ((*cmd_ctx
)->llm
) {
505 free((*cmd_ctx
)->llm
);
507 if ((*cmd_ctx
)->lsm
) {
508 free((*cmd_ctx
)->lsm
);
516 * Notify UST applications using the shm mmap futex.
518 static int notify_ust_apps(int active
)
522 DBG("Notifying applications of session daemon state: %d", active
);
524 /* See shm.c for this call implying mmap, shm and futex calls */
525 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
526 if (wait_shm_mmap
== NULL
) {
530 /* Wake waiting process */
531 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
533 /* Apps notified successfully */
541 * Setup the outgoing data buffer for the response (llm) by allocating the
542 * right amount of memory and copying the original information from the lsm
545 * Return total size of the buffer pointed by buf.
547 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
553 cmd_ctx
->llm
= zmalloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
554 if (cmd_ctx
->llm
== NULL
) {
560 /* Copy common data */
561 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
562 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
564 cmd_ctx
->llm
->data_size
= size
;
565 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
574 * Update the kernel poll set of all channel fd available over all tracing
575 * session. Add the wakeup pipe at the end of the set.
577 static int update_kernel_poll(struct lttng_poll_event
*events
)
580 struct ltt_session
*session
;
581 struct ltt_kernel_channel
*channel
;
583 DBG("Updating kernel poll set");
586 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
587 session_lock(session
);
588 if (session
->kernel_session
== NULL
) {
589 session_unlock(session
);
593 cds_list_for_each_entry(channel
,
594 &session
->kernel_session
->channel_list
.head
, list
) {
595 /* Add channel fd to the kernel poll set */
596 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
598 session_unlock(session
);
601 DBG("Channel fd %d added to kernel set", channel
->fd
);
603 session_unlock(session
);
605 session_unlock_list();
610 session_unlock_list();
615 * Find the channel fd from 'fd' over all tracing session. When found, check
616 * for new channel stream and send those stream fds to the kernel consumer.
618 * Useful for CPU hotplug feature.
620 static int update_kernel_stream(struct consumer_data
*consumer_data
, int fd
)
623 struct ltt_session
*session
;
624 struct ltt_kernel_channel
*channel
;
626 DBG("Updating kernel streams for channel fd %d", fd
);
629 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
630 session_lock(session
);
631 if (session
->kernel_session
== NULL
) {
632 session_unlock(session
);
636 /* This is not suppose to be -1 but this is an extra security check */
637 if (session
->kernel_session
->consumer_fd
< 0) {
638 session
->kernel_session
->consumer_fd
= consumer_data
->cmd_sock
;
641 cds_list_for_each_entry(channel
,
642 &session
->kernel_session
->channel_list
.head
, list
) {
643 if (channel
->fd
== fd
) {
644 DBG("Channel found, updating kernel streams");
645 ret
= kernel_open_channel_stream(channel
);
651 * Have we already sent fds to the consumer? If yes, it means
652 * that tracing is started so it is safe to send our updated
655 if (session
->kernel_session
->consumer_fds_sent
== 1 &&
656 session
->kernel_session
->consumer
!= NULL
) {
657 ret
= kernel_consumer_send_channel_stream(
658 session
->kernel_session
->consumer_fd
, channel
,
659 session
->kernel_session
);
667 session_unlock(session
);
669 session_unlock_list();
673 session_unlock(session
);
674 session_unlock_list();
679 * For each tracing session, update newly registered apps.
681 static void update_ust_app(int app_sock
)
683 struct ltt_session
*sess
, *stmp
;
687 /* For all tracing session(s) */
688 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
690 if (sess
->ust_session
) {
691 ust_app_global_update(sess
->ust_session
, app_sock
);
693 session_unlock(sess
);
696 session_unlock_list();
700 * This thread manage event coming from the kernel.
702 * Features supported in this thread:
705 static void *thread_manage_kernel(void *data
)
707 int ret
, i
, pollfd
, update_poll_flag
= 1;
708 uint32_t revents
, nb_fd
;
710 struct lttng_poll_event events
;
712 DBG("Thread manage kernel started");
714 ret
= create_thread_poll_set(&events
, 2);
716 goto error_poll_create
;
719 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
725 if (update_poll_flag
== 1) {
727 * Reset number of fd in the poll set. Always 2 since there is the thread
728 * quit pipe and the kernel pipe.
732 ret
= update_kernel_poll(&events
);
736 update_poll_flag
= 0;
739 nb_fd
= LTTNG_POLL_GETNB(&events
);
741 DBG("Thread kernel polling on %d fds", nb_fd
);
743 /* Zeroed the poll events */
744 lttng_poll_reset(&events
);
746 /* Poll infinite value of time */
748 ret
= lttng_poll_wait(&events
, -1);
751 * Restart interrupted system call.
753 if (errno
== EINTR
) {
757 } else if (ret
== 0) {
758 /* Should not happen since timeout is infinite */
759 ERR("Return value of poll is 0 with an infinite timeout.\n"
760 "This should not have happened! Continuing...");
764 for (i
= 0; i
< nb_fd
; i
++) {
765 /* Fetch once the poll data */
766 revents
= LTTNG_POLL_GETEV(&events
, i
);
767 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
769 /* Thread quit pipe has been closed. Killing thread. */
770 ret
= check_thread_quit_pipe(pollfd
, revents
);
775 /* Check for data on kernel pipe */
776 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
777 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
778 update_poll_flag
= 1;
782 * New CPU detected by the kernel. Adding kernel stream to
783 * kernel session and updating the kernel consumer
785 if (revents
& LPOLLIN
) {
786 ret
= update_kernel_stream(&kconsumer_data
, pollfd
);
792 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
793 * and unregister kernel stream at this point.
801 lttng_poll_clean(&events
);
803 DBG("Kernel thread dying");
808 * This thread manage the consumer error sent back to the session daemon.
810 static void *thread_manage_consumer(void *data
)
812 int sock
= -1, i
, ret
, pollfd
;
813 uint32_t revents
, nb_fd
;
814 enum lttcomm_return_code code
;
815 struct lttng_poll_event events
;
816 struct consumer_data
*consumer_data
= data
;
818 DBG("[thread] Manage consumer started");
820 ret
= lttcomm_listen_unix_sock(consumer_data
->err_sock
);
826 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
827 * Nothing more will be added to this poll set.
829 ret
= create_thread_poll_set(&events
, 2);
834 ret
= lttng_poll_add(&events
, consumer_data
->err_sock
, LPOLLIN
| LPOLLRDHUP
);
839 nb_fd
= LTTNG_POLL_GETNB(&events
);
841 /* Inifinite blocking call, waiting for transmission */
843 ret
= lttng_poll_wait(&events
, -1);
846 * Restart interrupted system call.
848 if (errno
== EINTR
) {
854 for (i
= 0; i
< nb_fd
; i
++) {
855 /* Fetch once the poll data */
856 revents
= LTTNG_POLL_GETEV(&events
, i
);
857 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
859 /* Thread quit pipe has been closed. Killing thread. */
860 ret
= check_thread_quit_pipe(pollfd
, revents
);
865 /* Event on the registration socket */
866 if (pollfd
== consumer_data
->err_sock
) {
867 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
868 ERR("consumer err socket poll error");
874 sock
= lttcomm_accept_unix_sock(consumer_data
->err_sock
);
879 DBG2("Receiving code from consumer err_sock");
881 /* Getting status code from kconsumerd */
882 ret
= lttcomm_recv_unix_sock(sock
, &code
,
883 sizeof(enum lttcomm_return_code
));
888 if (code
== CONSUMERD_COMMAND_SOCK_READY
) {
889 consumer_data
->cmd_sock
=
890 lttcomm_connect_unix_sock(consumer_data
->cmd_unix_sock_path
);
891 if (consumer_data
->cmd_sock
< 0) {
892 sem_post(&consumer_data
->sem
);
893 PERROR("consumer connect");
896 /* Signal condition to tell that the kconsumerd is ready */
897 sem_post(&consumer_data
->sem
);
898 DBG("consumer command socket ready");
900 ERR("consumer error when waiting for SOCK_READY : %s",
901 lttcomm_get_readable_code(-code
));
905 /* Remove the kconsumerd error sock since we've established a connexion */
906 ret
= lttng_poll_del(&events
, consumer_data
->err_sock
);
911 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
916 /* Update number of fd */
917 nb_fd
= LTTNG_POLL_GETNB(&events
);
919 /* Inifinite blocking call, waiting for transmission */
921 ret
= lttng_poll_wait(&events
, -1);
924 * Restart interrupted system call.
926 if (errno
== EINTR
) {
932 for (i
= 0; i
< nb_fd
; i
++) {
933 /* Fetch once the poll data */
934 revents
= LTTNG_POLL_GETEV(&events
, i
);
935 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
937 /* Thread quit pipe has been closed. Killing thread. */
938 ret
= check_thread_quit_pipe(pollfd
, revents
);
943 /* Event on the kconsumerd socket */
944 if (pollfd
== sock
) {
945 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
946 ERR("consumer err socket second poll error");
952 /* Wait for any kconsumerd error */
953 ret
= lttcomm_recv_unix_sock(sock
, &code
,
954 sizeof(enum lttcomm_return_code
));
956 ERR("consumer closed the command socket");
960 ERR("consumer return code : %s", lttcomm_get_readable_code(-code
));
963 /* Immediately set the consumerd state to stopped */
964 if (consumer_data
->type
== LTTNG_CONSUMER_KERNEL
) {
965 uatomic_set(&kernel_consumerd_state
, CONSUMER_ERROR
);
966 } else if (consumer_data
->type
== LTTNG_CONSUMER64_UST
||
967 consumer_data
->type
== LTTNG_CONSUMER32_UST
) {
968 uatomic_set(&ust_consumerd_state
, CONSUMER_ERROR
);
970 /* Code flow error... */
974 if (consumer_data
->err_sock
>= 0) {
975 ret
= close(consumer_data
->err_sock
);
980 if (consumer_data
->cmd_sock
>= 0) {
981 ret
= close(consumer_data
->cmd_sock
);
993 unlink(consumer_data
->err_unix_sock_path
);
994 unlink(consumer_data
->cmd_unix_sock_path
);
995 consumer_data
->pid
= 0;
997 lttng_poll_clean(&events
);
1000 DBG("consumer thread cleanup completed");
1006 * This thread manage application communication.
1008 static void *thread_manage_apps(void *data
)
1011 uint32_t revents
, nb_fd
;
1012 struct ust_command ust_cmd
;
1013 struct lttng_poll_event events
;
1015 DBG("[thread] Manage application started");
1017 rcu_register_thread();
1018 rcu_thread_online();
1020 ret
= create_thread_poll_set(&events
, 2);
1022 goto error_poll_create
;
1025 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
1031 /* Zeroed the events structure */
1032 lttng_poll_reset(&events
);
1034 nb_fd
= LTTNG_POLL_GETNB(&events
);
1036 DBG("Apps thread polling on %d fds", nb_fd
);
1038 /* Inifinite blocking call, waiting for transmission */
1040 ret
= lttng_poll_wait(&events
, -1);
1043 * Restart interrupted system call.
1045 if (errno
== EINTR
) {
1051 for (i
= 0; i
< nb_fd
; i
++) {
1052 /* Fetch once the poll data */
1053 revents
= LTTNG_POLL_GETEV(&events
, i
);
1054 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1056 /* Thread quit pipe has been closed. Killing thread. */
1057 ret
= check_thread_quit_pipe(pollfd
, revents
);
1062 /* Inspect the apps cmd pipe */
1063 if (pollfd
== apps_cmd_pipe
[0]) {
1064 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1065 ERR("Apps command pipe error");
1067 } else if (revents
& LPOLLIN
) {
1069 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
1070 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
1071 PERROR("read apps cmd pipe");
1075 /* Register applicaton to the session daemon */
1076 ret
= ust_app_register(&ust_cmd
.reg_msg
,
1078 if (ret
== -ENOMEM
) {
1080 } else if (ret
< 0) {
1085 * Validate UST version compatibility.
1087 ret
= ust_app_validate_version(ust_cmd
.sock
);
1090 * Add channel(s) and event(s) to newly registered apps
1091 * from lttng global UST domain.
1093 update_ust_app(ust_cmd
.sock
);
1096 ret
= ust_app_register_done(ust_cmd
.sock
);
1099 * If the registration is not possible, we simply
1100 * unregister the apps and continue
1102 ust_app_unregister(ust_cmd
.sock
);
1105 * We just need here to monitor the close of the UST
1106 * socket and poll set monitor those by default.
1107 * Listen on POLLIN (even if we never expect any
1108 * data) to ensure that hangup wakes us.
1110 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, LPOLLIN
);
1115 DBG("Apps with sock %d added to poll set",
1123 * At this point, we know that a registered application made
1124 * the event at poll_wait.
1126 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1127 /* Removing from the poll set */
1128 ret
= lttng_poll_del(&events
, pollfd
);
1133 /* Socket closed on remote end. */
1134 ust_app_unregister(pollfd
);
1142 lttng_poll_clean(&events
);
1144 DBG("Application communication apps thread cleanup complete");
1145 rcu_thread_offline();
1146 rcu_unregister_thread();
1151 * Dispatch request from the registration threads to the application
1152 * communication thread.
1154 static void *thread_dispatch_ust_registration(void *data
)
1157 struct cds_wfq_node
*node
;
1158 struct ust_command
*ust_cmd
= NULL
;
1160 DBG("[thread] Dispatch UST command started");
1162 while (!dispatch_thread_exit
) {
1163 /* Atomically prepare the queue futex */
1164 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1167 /* Dequeue command for registration */
1168 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1170 DBG("Woken up but nothing in the UST command queue");
1171 /* Continue thread execution */
1175 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1177 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1178 " gid:%d sock:%d name:%s (version %d.%d)",
1179 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1180 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1181 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1182 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1184 * Inform apps thread of the new application registration. This
1185 * call is blocking so we can be assured that the data will be read
1186 * at some point in time or wait to the end of the world :)
1188 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1189 sizeof(struct ust_command
));
1191 PERROR("write apps cmd pipe");
1192 if (errno
== EBADF
) {
1194 * We can't inform the application thread to process
1195 * registration. We will exit or else application
1196 * registration will not occur and tracing will never
1203 } while (node
!= NULL
);
1205 /* Futex wait on queue. Blocking call on futex() */
1206 futex_nto1_wait(&ust_cmd_queue
.futex
);
1210 DBG("Dispatch thread dying");
1215 * This thread manage application registration.
1217 static void *thread_registration_apps(void *data
)
1219 int sock
= -1, i
, ret
, pollfd
;
1220 uint32_t revents
, nb_fd
;
1221 struct lttng_poll_event events
;
1223 * Get allocated in this thread, enqueued to a global queue, dequeued and
1224 * freed in the manage apps thread.
1226 struct ust_command
*ust_cmd
= NULL
;
1228 DBG("[thread] Manage application registration started");
1230 ret
= lttcomm_listen_unix_sock(apps_sock
);
1236 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1237 * more will be added to this poll set.
1239 ret
= create_thread_poll_set(&events
, 2);
1241 goto error_create_poll
;
1244 /* Add the application registration socket */
1245 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1247 goto error_poll_add
;
1250 /* Notify all applications to register */
1251 ret
= notify_ust_apps(1);
1253 ERR("Failed to notify applications or create the wait shared memory.\n"
1254 "Execution continues but there might be problem for already\n"
1255 "running applications that wishes to register.");
1259 DBG("Accepting application registration");
1261 nb_fd
= LTTNG_POLL_GETNB(&events
);
1263 /* Inifinite blocking call, waiting for transmission */
1265 ret
= lttng_poll_wait(&events
, -1);
1268 * Restart interrupted system call.
1270 if (errno
== EINTR
) {
1276 for (i
= 0; i
< nb_fd
; i
++) {
1277 /* Fetch once the poll data */
1278 revents
= LTTNG_POLL_GETEV(&events
, i
);
1279 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1281 /* Thread quit pipe has been closed. Killing thread. */
1282 ret
= check_thread_quit_pipe(pollfd
, revents
);
1287 /* Event on the registration socket */
1288 if (pollfd
== apps_sock
) {
1289 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1290 ERR("Register apps socket poll error");
1292 } else if (revents
& LPOLLIN
) {
1293 sock
= lttcomm_accept_unix_sock(apps_sock
);
1298 /* Create UST registration command for enqueuing */
1299 ust_cmd
= zmalloc(sizeof(struct ust_command
));
1300 if (ust_cmd
== NULL
) {
1301 PERROR("ust command zmalloc");
1306 * Using message-based transmissions to ensure we don't
1307 * have to deal with partially received messages.
1309 ret
= lttng_fd_get(LTTNG_FD_APPS
, 1);
1311 ERR("Exhausted file descriptors allowed for applications.");
1320 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1321 sizeof(struct ust_register_msg
));
1322 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1324 PERROR("lttcomm_recv_unix_sock register apps");
1326 ERR("Wrong size received on apps register");
1333 lttng_fd_put(LTTNG_FD_APPS
, 1);
1338 ust_cmd
->sock
= sock
;
1341 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1342 " gid:%d sock:%d name:%s (version %d.%d)",
1343 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1344 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1345 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1346 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1349 * Lock free enqueue the registration request. The red pill
1350 * has been taken! This apps will be part of the *system*.
1352 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1355 * Wake the registration queue futex. Implicit memory
1356 * barrier with the exchange in cds_wfq_enqueue.
1358 futex_nto1_wake(&ust_cmd_queue
.futex
);
1365 /* Notify that the registration thread is gone */
1368 if (apps_sock
>= 0) {
1369 ret
= close(apps_sock
);
1379 lttng_fd_put(LTTNG_FD_APPS
, 1);
1381 unlink(apps_unix_sock_path
);
1384 lttng_poll_clean(&events
);
1387 DBG("UST Registration thread cleanup complete");
1393 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
1394 * exec or it will fails.
1396 static int spawn_consumer_thread(struct consumer_data
*consumer_data
)
1399 struct timespec timeout
;
1401 timeout
.tv_sec
= DEFAULT_SEM_WAIT_TIMEOUT
;
1402 timeout
.tv_nsec
= 0;
1404 /* Setup semaphore */
1405 ret
= sem_init(&consumer_data
->sem
, 0, 0);
1407 PERROR("sem_init consumer semaphore");
1411 ret
= pthread_create(&consumer_data
->thread
, NULL
,
1412 thread_manage_consumer
, consumer_data
);
1414 PERROR("pthread_create consumer");
1419 /* Get time for sem_timedwait absolute timeout */
1420 ret
= clock_gettime(CLOCK_REALTIME
, &timeout
);
1422 PERROR("clock_gettime spawn consumer");
1423 /* Infinite wait for the kconsumerd thread to be ready */
1424 ret
= sem_wait(&consumer_data
->sem
);
1426 /* Normal timeout if the gettime was successful */
1427 timeout
.tv_sec
+= DEFAULT_SEM_WAIT_TIMEOUT
;
1428 ret
= sem_timedwait(&consumer_data
->sem
, &timeout
);
1432 if (errno
== ETIMEDOUT
) {
1434 * Call has timed out so we kill the kconsumerd_thread and return
1437 ERR("The consumer thread was never ready. Killing it");
1438 ret
= pthread_cancel(consumer_data
->thread
);
1440 PERROR("pthread_cancel consumer thread");
1443 PERROR("semaphore wait failed consumer thread");
1448 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1449 if (consumer_data
->pid
== 0) {
1450 ERR("Kconsumerd did not start");
1451 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1454 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1463 * Join consumer thread
1465 static int join_consumer_thread(struct consumer_data
*consumer_data
)
1470 if (consumer_data
->pid
!= 0) {
1471 ret
= kill(consumer_data
->pid
, SIGTERM
);
1473 ERR("Error killing consumer daemon");
1476 return pthread_join(consumer_data
->thread
, &status
);
1483 * Fork and exec a consumer daemon (consumerd).
1485 * Return pid if successful else -1.
1487 static pid_t
spawn_consumerd(struct consumer_data
*consumer_data
)
1491 const char *consumer_to_use
;
1492 const char *verbosity
;
1495 DBG("Spawning consumerd");
1502 if (opt_verbose_consumer
) {
1503 verbosity
= "--verbose";
1505 verbosity
= "--quiet";
1507 switch (consumer_data
->type
) {
1508 case LTTNG_CONSUMER_KERNEL
:
1510 * Find out which consumerd to execute. We will first try the
1511 * 64-bit path, then the sessiond's installation directory, and
1512 * fallback on the 32-bit one,
1514 DBG3("Looking for a kernel consumer at these locations:");
1515 DBG3(" 1) %s", consumerd64_bin
);
1516 DBG3(" 2) %s/%s", INSTALL_BIN_PATH
, CONSUMERD_FILE
);
1517 DBG3(" 3) %s", consumerd32_bin
);
1518 if (stat(consumerd64_bin
, &st
) == 0) {
1519 DBG3("Found location #1");
1520 consumer_to_use
= consumerd64_bin
;
1521 } else if (stat(INSTALL_BIN_PATH
"/" CONSUMERD_FILE
, &st
) == 0) {
1522 DBG3("Found location #2");
1523 consumer_to_use
= INSTALL_BIN_PATH
"/" CONSUMERD_FILE
;
1524 } else if (stat(consumerd32_bin
, &st
) == 0) {
1525 DBG3("Found location #3");
1526 consumer_to_use
= consumerd32_bin
;
1528 DBG("Could not find any valid consumerd executable");
1531 DBG("Using kernel consumer at: %s", consumer_to_use
);
1532 execl(consumer_to_use
,
1533 "lttng-consumerd", verbosity
, "-k",
1534 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1535 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1538 case LTTNG_CONSUMER64_UST
:
1540 char *tmpnew
= NULL
;
1542 if (consumerd64_libdir
[0] != '\0') {
1546 tmp
= getenv("LD_LIBRARY_PATH");
1550 tmplen
= strlen("LD_LIBRARY_PATH=")
1551 + strlen(consumerd64_libdir
) + 1 /* : */ + strlen(tmp
);
1552 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1557 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1558 strcat(tmpnew
, consumerd64_libdir
);
1559 if (tmp
[0] != '\0') {
1560 strcat(tmpnew
, ":");
1561 strcat(tmpnew
, tmp
);
1563 ret
= putenv(tmpnew
);
1569 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin
);
1570 ret
= execl(consumerd64_bin
, "lttng-consumerd", verbosity
, "-u",
1571 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1572 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1574 if (consumerd64_libdir
[0] != '\0') {
1582 case LTTNG_CONSUMER32_UST
:
1584 char *tmpnew
= NULL
;
1586 if (consumerd32_libdir
[0] != '\0') {
1590 tmp
= getenv("LD_LIBRARY_PATH");
1594 tmplen
= strlen("LD_LIBRARY_PATH=")
1595 + strlen(consumerd32_libdir
) + 1 /* : */ + strlen(tmp
);
1596 tmpnew
= zmalloc(tmplen
+ 1 /* \0 */);
1601 strcpy(tmpnew
, "LD_LIBRARY_PATH=");
1602 strcat(tmpnew
, consumerd32_libdir
);
1603 if (tmp
[0] != '\0') {
1604 strcat(tmpnew
, ":");
1605 strcat(tmpnew
, tmp
);
1607 ret
= putenv(tmpnew
);
1613 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin
);
1614 ret
= execl(consumerd32_bin
, "lttng-consumerd", verbosity
, "-u",
1615 "--consumerd-cmd-sock", consumer_data
->cmd_unix_sock_path
,
1616 "--consumerd-err-sock", consumer_data
->err_unix_sock_path
,
1618 if (consumerd32_libdir
[0] != '\0') {
1627 PERROR("unknown consumer type");
1631 PERROR("kernel start consumer exec");
1634 } else if (pid
> 0) {
1637 PERROR("start consumer fork");
1645 * Spawn the consumerd daemon and session daemon thread.
1647 static int start_consumerd(struct consumer_data
*consumer_data
)
1651 pthread_mutex_lock(&consumer_data
->pid_mutex
);
1652 if (consumer_data
->pid
!= 0) {
1653 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1657 ret
= spawn_consumerd(consumer_data
);
1659 ERR("Spawning consumerd failed");
1660 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1664 /* Setting up the consumer_data pid */
1665 consumer_data
->pid
= ret
;
1666 DBG2("Consumer pid %d", consumer_data
->pid
);
1667 pthread_mutex_unlock(&consumer_data
->pid_mutex
);
1669 DBG2("Spawning consumer control thread");
1670 ret
= spawn_consumer_thread(consumer_data
);
1672 ERR("Fatal error spawning consumer control thread");
1684 * Check version of the lttng-modules.
1686 static int validate_lttng_modules_version(void)
1688 return kernel_validate_version(kernel_tracer_fd
);
1692 * Setup necessary data for kernel tracer action.
1694 static int init_kernel_tracer(void)
1698 /* Modprobe lttng kernel modules */
1699 ret
= modprobe_lttng_control();
1704 /* Open debugfs lttng */
1705 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1706 if (kernel_tracer_fd
< 0) {
1707 DBG("Failed to open %s", module_proc_lttng
);
1712 /* Validate kernel version */
1713 ret
= validate_lttng_modules_version();
1718 ret
= modprobe_lttng_data();
1723 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1727 modprobe_remove_lttng_control();
1728 ret
= close(kernel_tracer_fd
);
1732 kernel_tracer_fd
= -1;
1733 return LTTCOMM_KERN_VERSION
;
1736 ret
= close(kernel_tracer_fd
);
1742 modprobe_remove_lttng_control();
1745 WARN("No kernel tracer available");
1746 kernel_tracer_fd
= -1;
1748 return LTTCOMM_NEED_ROOT_SESSIOND
;
1750 return LTTCOMM_KERN_NA
;
1755 * Init tracing by creating trace directory and sending fds kernel consumer.
1757 static int init_kernel_tracing(struct ltt_kernel_session
*session
)
1761 if (session
->consumer_fds_sent
== 0 && session
->consumer
!= NULL
) {
1763 * Assign default kernel consumer socket if no consumer assigned to the
1764 * kernel session. At this point, it's NOT supposed to be -1 but this is
1765 * an extra security check.
1767 if (session
->consumer_fd
< 0) {
1768 session
->consumer_fd
= kconsumer_data
.cmd_sock
;
1771 ret
= kernel_consumer_send_session(session
->consumer_fd
, session
);
1773 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1783 * Create a socket to the relayd using the URI.
1785 * On success, the relayd_sock pointer is set to the created socket.
1786 * Else, it is untouched and an lttcomm error code is returned.
1788 static int create_connect_relayd(struct consumer_output
*output
,
1789 const char *session_name
, struct lttng_uri
*uri
,
1790 struct lttcomm_sock
**relayd_sock
)
1793 struct lttcomm_sock
*sock
;
1795 /* Create socket object from URI */
1796 sock
= lttcomm_alloc_sock_from_uri(uri
);
1798 ret
= LTTCOMM_FATAL
;
1802 ret
= lttcomm_create_sock(sock
);
1804 ret
= LTTCOMM_FATAL
;
1808 /* Connect to relayd so we can proceed with a session creation. */
1809 ret
= relayd_connect(sock
);
1811 ERR("Unable to reach lttng-relayd");
1812 ret
= LTTCOMM_RELAYD_SESSION_FAIL
;
1816 /* Create socket for control stream. */
1817 if (uri
->stype
== LTTNG_STREAM_CONTROL
) {
1818 DBG3("Creating relayd stream socket from URI");
1820 /* Check relayd version */
1821 ret
= relayd_version_check(sock
, LTTNG_UST_COMM_MAJOR
, 0);
1823 ret
= LTTCOMM_RELAYD_VERSION_FAIL
;
1826 } else if (uri
->stype
== LTTNG_STREAM_DATA
) {
1827 DBG3("Creating relayd data socket from URI");
1829 /* Command is not valid */
1830 ERR("Relayd invalid stream type: %d", uri
->stype
);
1831 ret
= LTTCOMM_INVALID
;
1835 *relayd_sock
= sock
;
1841 (void) relayd_close(sock
);
1845 lttcomm_destroy_sock(sock
);
1852 * Connect to the relayd using URI and send the socket to the right consumer.
1854 static int send_socket_relayd_consumer(int domain
, struct ltt_session
*session
,
1855 struct lttng_uri
*relayd_uri
, struct consumer_output
*consumer
,
1859 struct lttcomm_sock
*sock
= NULL
;
1861 /* Set the network sequence index if not set. */
1862 if (consumer
->net_seq_index
== -1) {
1864 * Increment net_seq_idx because we are about to transfer the
1865 * new relayd socket to the consumer.
1867 uatomic_inc(&relayd_net_seq_idx
);
1868 /* Assign unique key so the consumer can match streams */
1869 consumer
->net_seq_index
= uatomic_read(&relayd_net_seq_idx
);
1872 /* Connect to relayd and make version check if uri is the control. */
1873 ret
= create_connect_relayd(consumer
, session
->name
, relayd_uri
, &sock
);
1874 if (ret
!= LTTCOMM_OK
) {
1878 /* If the control socket is connected, network session is ready */
1879 if (relayd_uri
->stype
== LTTNG_STREAM_CONTROL
) {
1880 session
->net_handle
= 1;
1884 case LTTNG_DOMAIN_KERNEL
:
1885 /* Send relayd socket to consumer. */
1886 ret
= kernel_consumer_send_relayd_socket(consumer_fd
, sock
,
1887 consumer
, relayd_uri
->stype
);
1889 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
1893 case LTTNG_DOMAIN_UST
:
1894 /* Send relayd socket to consumer. */
1895 ret
= ust_consumer_send_relayd_socket(consumer_fd
, sock
,
1896 consumer
, relayd_uri
->stype
);
1898 ret
= LTTCOMM_ENABLE_CONSUMER_FAIL
;
1907 * Close socket which was dup on the consumer side. The session daemon does
1908 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1913 (void) relayd_close(sock
);
1914 lttcomm_destroy_sock(sock
);
1921 * Send both relayd sockets to a specific consumer and domain. This is a
1922 * helper function to facilitate sending the information to the consumer for a
1925 static int send_sockets_relayd_consumer(int domain
,
1926 struct ltt_session
*session
, struct consumer_output
*consumer
, int fd
)
1930 /* Sending control relayd socket. */
1931 ret
= send_socket_relayd_consumer(domain
, session
,
1932 &consumer
->dst
.net
.control
, consumer
, fd
);
1933 if (ret
!= LTTCOMM_OK
) {
1937 /* Sending data relayd socket. */
1938 ret
= send_socket_relayd_consumer(domain
, session
,
1939 &consumer
->dst
.net
.data
, consumer
, fd
);
1940 if (ret
!= LTTCOMM_OK
) {
1949 * Setup relayd connections for a tracing session. First creates the socket to
1950 * the relayd and send them to the right domain consumer. Consumer type MUST be
1953 static int setup_relayd(struct ltt_session
*session
)
1955 int ret
= LTTCOMM_OK
;
1956 struct ltt_ust_session
*usess
;
1957 struct ltt_kernel_session
*ksess
;
1961 usess
= session
->ust_session
;
1962 ksess
= session
->kernel_session
;
1964 DBG2("Setting relayd for session %s", session
->name
);
1966 if (usess
&& usess
->consumer
->sock
== -1 &&
1967 usess
->consumer
->type
== CONSUMER_DST_NET
&&
1968 usess
->consumer
->enabled
) {
1969 /* Setup relayd for 64 bits consumer */
1970 if (ust_consumerd64_fd
>= 0) {
1971 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST
, session
,
1972 usess
->consumer
, ust_consumerd64_fd
);
1973 if (ret
!= LTTCOMM_OK
) {
1978 /* Setup relayd for 32 bits consumer */
1979 if (ust_consumerd32_fd
>= 0) {
1980 send_sockets_relayd_consumer(LTTNG_DOMAIN_UST
, session
,
1981 usess
->consumer
, ust_consumerd32_fd
);
1982 if (ret
!= LTTCOMM_OK
) {
1986 } else if (ksess
&& ksess
->consumer
->sock
== -1 &&
1987 ksess
->consumer
->type
== CONSUMER_DST_NET
&&
1988 ksess
->consumer
->enabled
) {
1989 send_sockets_relayd_consumer(LTTNG_DOMAIN_KERNEL
, session
,
1990 ksess
->consumer
, ksess
->consumer_fd
);
1991 if (ret
!= LTTCOMM_OK
) {
2001 * Copy consumer output from the tracing session to the domain session. The
2002 * function also applies the right modification on a per domain basis for the
2003 * trace files destination directory.
2005 static int copy_session_consumer(int domain
, struct ltt_session
*session
)
2008 const char *dir_name
;
2009 struct consumer_output
*consumer
;
2012 case LTTNG_DOMAIN_KERNEL
:
2013 DBG3("Copying tracing session consumer output in kernel session");
2014 session
->kernel_session
->consumer
=
2015 consumer_copy_output(session
->consumer
);
2016 /* Ease our life a bit for the next part */
2017 consumer
= session
->kernel_session
->consumer
;
2018 dir_name
= DEFAULT_KERNEL_TRACE_DIR
;
2020 case LTTNG_DOMAIN_UST
:
2021 DBG3("Copying tracing session consumer output in UST session");
2022 session
->ust_session
->consumer
=
2023 consumer_copy_output(session
->consumer
);
2024 /* Ease our life a bit for the next part */
2025 consumer
= session
->ust_session
->consumer
;
2026 dir_name
= DEFAULT_UST_TRACE_DIR
;
2029 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2033 /* Append correct directory to subdir */
2034 strncat(consumer
->subdir
, dir_name
, sizeof(consumer
->subdir
));
2035 DBG3("Copy session consumer subdir %s", consumer
->subdir
);
2037 /* Add default trace directory name */
2038 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
2039 strncat(consumer
->dst
.trace_path
, dir_name
,
2040 sizeof(consumer
->dst
.trace_path
));
2050 * Create an UST session and add it to the session ust list.
2052 static int create_ust_session(struct ltt_session
*session
,
2053 struct lttng_domain
*domain
)
2056 struct ltt_ust_session
*lus
= NULL
;
2059 assert(session
->consumer
);
2061 switch (domain
->type
) {
2062 case LTTNG_DOMAIN_UST
:
2065 ERR("Unknown UST domain on create session %d", domain
->type
);
2066 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2070 DBG("Creating UST session");
2072 lus
= trace_ust_create_session(session
->path
, session
->id
, domain
);
2074 ret
= LTTCOMM_UST_SESS_FAIL
;
2078 if (session
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2079 ret
= run_as_mkdir_recursive(lus
->pathname
, S_IRWXU
| S_IRWXG
,
2080 session
->uid
, session
->gid
);
2082 if (ret
!= -EEXIST
) {
2083 ERR("Trace directory creation error");
2084 ret
= LTTCOMM_UST_SESS_FAIL
;
2090 lus
->uid
= session
->uid
;
2091 lus
->gid
= session
->gid
;
2092 session
->ust_session
= lus
;
2094 /* Copy session output to the newly created UST session */
2095 ret
= copy_session_consumer(domain
->type
, session
);
2096 if (ret
!= LTTCOMM_OK
) {
2104 session
->ust_session
= NULL
;
2109 * Create a kernel tracer session then create the default channel.
2111 static int create_kernel_session(struct ltt_session
*session
)
2115 DBG("Creating kernel session");
2117 ret
= kernel_create_session(session
, kernel_tracer_fd
);
2119 ret
= LTTCOMM_KERN_SESS_FAIL
;
2123 /* Set kernel consumer socket fd */
2124 if (kconsumer_data
.cmd_sock
>= 0) {
2125 session
->kernel_session
->consumer_fd
= kconsumer_data
.cmd_sock
;
2128 /* Copy session output to the newly created Kernel session */
2129 ret
= copy_session_consumer(LTTNG_DOMAIN_KERNEL
, session
);
2130 if (ret
!= LTTCOMM_OK
) {
2134 /* Create directory(ies) on local filesystem. */
2135 if (session
->consumer
->type
== CONSUMER_DST_LOCAL
) {
2136 ret
= run_as_mkdir_recursive(
2137 session
->kernel_session
->consumer
->dst
.trace_path
,
2138 S_IRWXU
| S_IRWXG
, session
->uid
, session
->gid
);
2140 if (ret
!= -EEXIST
) {
2141 ERR("Trace directory creation error");
2147 session
->kernel_session
->uid
= session
->uid
;
2148 session
->kernel_session
->gid
= session
->gid
;
2153 trace_kernel_destroy_session(session
->kernel_session
);
2154 session
->kernel_session
= NULL
;
2159 * Check if the UID or GID match the session. Root user has access to all
2162 static int session_access_ok(struct ltt_session
*session
, uid_t uid
, gid_t gid
)
2164 if (uid
!= session
->uid
&& gid
!= session
->gid
&& uid
!= 0) {
2172 * Count number of session permitted by uid/gid.
2174 static unsigned int lttng_sessions_count(uid_t uid
, gid_t gid
)
2177 struct ltt_session
*session
;
2179 DBG("Counting number of available session for UID %d GID %d",
2181 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2183 * Only list the sessions the user can control.
2185 if (!session_access_ok(session
, uid
, gid
)) {
2194 * Using the session list, filled a lttng_session array to send back to the
2195 * client for session listing.
2197 * The session list lock MUST be acquired before calling this function. Use
2198 * session_lock_list() and session_unlock_list().
2200 static void list_lttng_sessions(struct lttng_session
*sessions
, uid_t uid
,
2204 struct ltt_session
*session
;
2206 DBG("Getting all available session for UID %d GID %d",
2209 * Iterate over session list and append data after the control struct in
2212 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
2214 * Only list the sessions the user can control.
2216 if (!session_access_ok(session
, uid
, gid
)) {
2219 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
2220 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
2221 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
2222 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
2223 sessions
[i
].enabled
= session
->enabled
;
2229 * Fill lttng_channel array of all channels.
2231 static void list_lttng_channels(int domain
, struct ltt_session
*session
,
2232 struct lttng_channel
*channels
)
2235 struct ltt_kernel_channel
*kchan
;
2237 DBG("Listing channels for session %s", session
->name
);
2240 case LTTNG_DOMAIN_KERNEL
:
2241 /* Kernel channels */
2242 if (session
->kernel_session
!= NULL
) {
2243 cds_list_for_each_entry(kchan
,
2244 &session
->kernel_session
->channel_list
.head
, list
) {
2245 /* Copy lttng_channel struct to array */
2246 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
2247 channels
[i
].enabled
= kchan
->enabled
;
2252 case LTTNG_DOMAIN_UST
:
2254 struct lttng_ht_iter iter
;
2255 struct ltt_ust_channel
*uchan
;
2257 cds_lfht_for_each_entry(session
->ust_session
->domain_global
.channels
->ht
,
2258 &iter
.iter
, uchan
, node
.node
) {
2259 strncpy(channels
[i
].name
, uchan
->name
, LTTNG_SYMBOL_NAME_LEN
);
2260 channels
[i
].attr
.overwrite
= uchan
->attr
.overwrite
;
2261 channels
[i
].attr
.subbuf_size
= uchan
->attr
.subbuf_size
;
2262 channels
[i
].attr
.num_subbuf
= uchan
->attr
.num_subbuf
;
2263 channels
[i
].attr
.switch_timer_interval
=
2264 uchan
->attr
.switch_timer_interval
;
2265 channels
[i
].attr
.read_timer_interval
=
2266 uchan
->attr
.read_timer_interval
;
2267 channels
[i
].enabled
= uchan
->enabled
;
2268 switch (uchan
->attr
.output
) {
2269 case LTTNG_UST_MMAP
:
2271 channels
[i
].attr
.output
= LTTNG_EVENT_MMAP
;
2284 * Create a list of ust global domain events.
2286 static int list_lttng_ust_global_events(char *channel_name
,
2287 struct ltt_ust_domain_global
*ust_global
, struct lttng_event
**events
)
2290 unsigned int nb_event
= 0;
2291 struct lttng_ht_iter iter
;
2292 struct lttng_ht_node_str
*node
;
2293 struct ltt_ust_channel
*uchan
;
2294 struct ltt_ust_event
*uevent
;
2295 struct lttng_event
*tmp
;
2297 DBG("Listing UST global events for channel %s", channel_name
);
2301 lttng_ht_lookup(ust_global
->channels
, (void *)channel_name
, &iter
);
2302 node
= lttng_ht_iter_get_node_str(&iter
);
2304 ret
= -LTTCOMM_UST_CHAN_NOT_FOUND
;
2308 uchan
= caa_container_of(&node
->node
, struct ltt_ust_channel
, node
.node
);
2310 nb_event
+= lttng_ht_get_count(uchan
->events
);
2312 if (nb_event
== 0) {
2317 DBG3("Listing UST global %d events", nb_event
);
2319 tmp
= zmalloc(nb_event
* sizeof(struct lttng_event
));
2321 ret
= -LTTCOMM_FATAL
;
2325 cds_lfht_for_each_entry(uchan
->events
->ht
, &iter
.iter
, uevent
, node
.node
) {
2326 strncpy(tmp
[i
].name
, uevent
->attr
.name
, LTTNG_SYMBOL_NAME_LEN
);
2327 tmp
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
2328 tmp
[i
].enabled
= uevent
->enabled
;
2329 switch (uevent
->attr
.instrumentation
) {
2330 case LTTNG_UST_TRACEPOINT
:
2331 tmp
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2333 case LTTNG_UST_PROBE
:
2334 tmp
[i
].type
= LTTNG_EVENT_PROBE
;
2336 case LTTNG_UST_FUNCTION
:
2337 tmp
[i
].type
= LTTNG_EVENT_FUNCTION
;
2340 tmp
[i
].loglevel
= uevent
->attr
.loglevel
;
2341 switch (uevent
->attr
.loglevel_type
) {
2342 case LTTNG_UST_LOGLEVEL_ALL
:
2343 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
2345 case LTTNG_UST_LOGLEVEL_RANGE
:
2346 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_RANGE
;
2348 case LTTNG_UST_LOGLEVEL_SINGLE
:
2349 tmp
[i
].loglevel_type
= LTTNG_EVENT_LOGLEVEL_SINGLE
;
2364 * Fill lttng_event array of all kernel events in the channel.
2366 static int list_lttng_kernel_events(char *channel_name
,
2367 struct ltt_kernel_session
*kernel_session
, struct lttng_event
**events
)
2370 unsigned int nb_event
;
2371 struct ltt_kernel_event
*event
;
2372 struct ltt_kernel_channel
*kchan
;
2374 kchan
= trace_kernel_get_channel_by_name(channel_name
, kernel_session
);
2375 if (kchan
== NULL
) {
2376 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2380 nb_event
= kchan
->event_count
;
2382 DBG("Listing events for channel %s", kchan
->channel
->name
);
2384 if (nb_event
== 0) {
2389 *events
= zmalloc(nb_event
* sizeof(struct lttng_event
));
2390 if (*events
== NULL
) {
2391 ret
= LTTCOMM_FATAL
;
2395 /* Kernel channels */
2396 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
2397 strncpy((*events
)[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
2398 (*events
)[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
2399 (*events
)[i
].enabled
= event
->enabled
;
2400 switch (event
->event
->instrumentation
) {
2401 case LTTNG_KERNEL_TRACEPOINT
:
2402 (*events
)[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2404 case LTTNG_KERNEL_KPROBE
:
2405 case LTTNG_KERNEL_KRETPROBE
:
2406 (*events
)[i
].type
= LTTNG_EVENT_PROBE
;
2407 memcpy(&(*events
)[i
].attr
.probe
, &event
->event
->u
.kprobe
,
2408 sizeof(struct lttng_kernel_kprobe
));
2410 case LTTNG_KERNEL_FUNCTION
:
2411 (*events
)[i
].type
= LTTNG_EVENT_FUNCTION
;
2412 memcpy(&((*events
)[i
].attr
.ftrace
), &event
->event
->u
.ftrace
,
2413 sizeof(struct lttng_kernel_function
));
2415 case LTTNG_KERNEL_NOOP
:
2416 (*events
)[i
].type
= LTTNG_EVENT_NOOP
;
2418 case LTTNG_KERNEL_SYSCALL
:
2419 (*events
)[i
].type
= LTTNG_EVENT_SYSCALL
;
2421 case LTTNG_KERNEL_ALL
:
2435 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
2437 static int cmd_disable_channel(struct ltt_session
*session
,
2438 int domain
, char *channel_name
)
2441 struct ltt_ust_session
*usess
;
2443 usess
= session
->ust_session
;
2446 case LTTNG_DOMAIN_KERNEL
:
2448 ret
= channel_kernel_disable(session
->kernel_session
,
2450 if (ret
!= LTTCOMM_OK
) {
2454 kernel_wait_quiescent(kernel_tracer_fd
);
2457 case LTTNG_DOMAIN_UST
:
2459 struct ltt_ust_channel
*uchan
;
2460 struct lttng_ht
*chan_ht
;
2462 chan_ht
= usess
->domain_global
.channels
;
2464 uchan
= trace_ust_find_channel_by_name(chan_ht
, channel_name
);
2465 if (uchan
== NULL
) {
2466 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2470 ret
= channel_ust_disable(usess
, domain
, uchan
);
2471 if (ret
!= LTTCOMM_OK
) {
2477 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2478 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2479 case LTTNG_DOMAIN_UST_PID
:
2482 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2493 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
2495 static int cmd_enable_channel(struct ltt_session
*session
,
2496 int domain
, struct lttng_channel
*attr
)
2499 struct ltt_ust_session
*usess
= session
->ust_session
;
2500 struct lttng_ht
*chan_ht
;
2502 DBG("Enabling channel %s for session %s", attr
->name
, session
->name
);
2505 case LTTNG_DOMAIN_KERNEL
:
2507 struct ltt_kernel_channel
*kchan
;
2509 kchan
= trace_kernel_get_channel_by_name(attr
->name
,
2510 session
->kernel_session
);
2511 if (kchan
== NULL
) {
2512 ret
= channel_kernel_create(session
->kernel_session
,
2513 attr
, kernel_poll_pipe
[1]);
2515 ret
= channel_kernel_enable(session
->kernel_session
, kchan
);
2518 if (ret
!= LTTCOMM_OK
) {
2522 kernel_wait_quiescent(kernel_tracer_fd
);
2525 case LTTNG_DOMAIN_UST
:
2527 struct ltt_ust_channel
*uchan
;
2529 chan_ht
= usess
->domain_global
.channels
;
2531 uchan
= trace_ust_find_channel_by_name(chan_ht
, attr
->name
);
2532 if (uchan
== NULL
) {
2533 ret
= channel_ust_create(usess
, domain
, attr
);
2535 ret
= channel_ust_enable(usess
, domain
, uchan
);
2540 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2541 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2542 case LTTNG_DOMAIN_UST_PID
:
2545 ret
= LTTCOMM_UNKNOWN_DOMAIN
;
2554 * Command LTTNG_DISABLE_EVENT processed by the client thread.
2556 static int cmd_disable_event(struct ltt_session
*session
, int domain
,
2557 char *channel_name
, char *event_name
)
2562 case LTTNG_DOMAIN_KERNEL
:
2564 struct ltt_kernel_channel
*kchan
;
2565 struct ltt_kernel_session
*ksess
;
2567 ksess
= session
->kernel_session
;
2569 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
2570 if (kchan
== NULL
) {
2571 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2575 ret
= event_kernel_disable_tracepoint(ksess
, kchan
, event_name
);
2576 if (ret
!= LTTCOMM_OK
) {
2580 kernel_wait_quiescent(kernel_tracer_fd
);
2583 case LTTNG_DOMAIN_UST
:
2585 struct ltt_ust_channel
*uchan
;
2586 struct ltt_ust_session
*usess
;
2588 usess
= session
->ust_session
;
2590 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2592 if (uchan
== NULL
) {
2593 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2597 ret
= event_ust_disable_tracepoint(usess
, domain
, uchan
, event_name
);
2598 if (ret
!= LTTCOMM_OK
) {
2602 DBG3("Disable UST event %s in channel %s completed", event_name
,
2607 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2608 case LTTNG_DOMAIN_UST_PID
:
2609 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2623 * Command LTTNG_DISABLE_ALL_EVENT processed by the client thread.
2625 static int cmd_disable_event_all(struct ltt_session
*session
, int domain
,
2631 case LTTNG_DOMAIN_KERNEL
:
2633 struct ltt_kernel_session
*ksess
;
2634 struct ltt_kernel_channel
*kchan
;
2636 ksess
= session
->kernel_session
;
2638 kchan
= trace_kernel_get_channel_by_name(channel_name
, ksess
);
2639 if (kchan
== NULL
) {
2640 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2644 ret
= event_kernel_disable_all(ksess
, kchan
);
2645 if (ret
!= LTTCOMM_OK
) {
2649 kernel_wait_quiescent(kernel_tracer_fd
);
2652 case LTTNG_DOMAIN_UST
:
2654 struct ltt_ust_session
*usess
;
2655 struct ltt_ust_channel
*uchan
;
2657 usess
= session
->ust_session
;
2659 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2661 if (uchan
== NULL
) {
2662 ret
= LTTCOMM_UST_CHAN_NOT_FOUND
;
2666 ret
= event_ust_disable_all_tracepoints(usess
, domain
, uchan
);
2671 DBG3("Disable all UST events in channel %s completed", channel_name
);
2676 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2677 case LTTNG_DOMAIN_UST_PID
:
2678 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2692 * Command LTTNG_ADD_CONTEXT processed by the client thread.
2694 static int cmd_add_context(struct ltt_session
*session
, int domain
,
2695 char *channel_name
, char *event_name
, struct lttng_event_context
*ctx
)
2700 case LTTNG_DOMAIN_KERNEL
:
2701 /* Add kernel context to kernel tracer */
2702 ret
= context_kernel_add(session
->kernel_session
, ctx
,
2703 event_name
, channel_name
);
2704 if (ret
!= LTTCOMM_OK
) {
2708 case LTTNG_DOMAIN_UST
:
2710 struct ltt_ust_session
*usess
= session
->ust_session
;
2712 ret
= context_ust_add(usess
, domain
, ctx
, event_name
, channel_name
);
2713 if (ret
!= LTTCOMM_OK
) {
2719 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2720 case LTTNG_DOMAIN_UST_PID
:
2721 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2735 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2737 static int cmd_enable_event(struct ltt_session
*session
, int domain
,
2738 char *channel_name
, struct lttng_event
*event
)
2741 struct lttng_channel
*attr
;
2742 struct ltt_ust_session
*usess
= session
->ust_session
;
2745 case LTTNG_DOMAIN_KERNEL
:
2747 struct ltt_kernel_channel
*kchan
;
2749 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2750 session
->kernel_session
);
2751 if (kchan
== NULL
) {
2752 attr
= channel_new_default_attr(domain
);
2754 ret
= LTTCOMM_FATAL
;
2757 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2759 /* This call will notify the kernel thread */
2760 ret
= channel_kernel_create(session
->kernel_session
,
2761 attr
, kernel_poll_pipe
[1]);
2762 if (ret
!= LTTCOMM_OK
) {
2769 /* Get the newly created kernel channel pointer */
2770 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2771 session
->kernel_session
);
2772 if (kchan
== NULL
) {
2773 /* This sould not happen... */
2774 ret
= LTTCOMM_FATAL
;
2778 ret
= event_kernel_enable_tracepoint(session
->kernel_session
, kchan
,
2780 if (ret
!= LTTCOMM_OK
) {
2784 kernel_wait_quiescent(kernel_tracer_fd
);
2787 case LTTNG_DOMAIN_UST
:
2789 struct lttng_channel
*attr
;
2790 struct ltt_ust_channel
*uchan
;
2792 /* Get channel from global UST domain */
2793 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2795 if (uchan
== NULL
) {
2796 /* Create default channel */
2797 attr
= channel_new_default_attr(domain
);
2799 ret
= LTTCOMM_FATAL
;
2802 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2803 attr
->name
[NAME_MAX
- 1] = '\0';
2805 ret
= channel_ust_create(usess
, domain
, attr
);
2806 if (ret
!= LTTCOMM_OK
) {
2812 /* Get the newly created channel reference back */
2813 uchan
= trace_ust_find_channel_by_name(
2814 usess
->domain_global
.channels
, channel_name
);
2815 if (uchan
== NULL
) {
2816 /* Something is really wrong */
2817 ret
= LTTCOMM_FATAL
;
2822 /* At this point, the session and channel exist on the tracer */
2823 ret
= event_ust_enable_tracepoint(usess
, domain
, uchan
, event
);
2824 if (ret
!= LTTCOMM_OK
) {
2830 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2831 case LTTNG_DOMAIN_UST_PID
:
2832 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2846 * Command LTTNG_ENABLE_ALL_EVENT processed by the client thread.
2848 static int cmd_enable_event_all(struct ltt_session
*session
, int domain
,
2849 char *channel_name
, int event_type
)
2852 struct ltt_kernel_channel
*kchan
;
2855 case LTTNG_DOMAIN_KERNEL
:
2856 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2857 session
->kernel_session
);
2858 if (kchan
== NULL
) {
2859 /* This call will notify the kernel thread */
2860 ret
= channel_kernel_create(session
->kernel_session
, NULL
,
2861 kernel_poll_pipe
[1]);
2862 if (ret
!= LTTCOMM_OK
) {
2866 /* Get the newly created kernel channel pointer */
2867 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2868 session
->kernel_session
);
2869 if (kchan
== NULL
) {
2870 /* This sould not happen... */
2871 ret
= LTTCOMM_FATAL
;
2877 switch (event_type
) {
2878 case LTTNG_EVENT_SYSCALL
:
2879 ret
= event_kernel_enable_all_syscalls(session
->kernel_session
,
2880 kchan
, kernel_tracer_fd
);
2882 case LTTNG_EVENT_TRACEPOINT
:
2884 * This call enables all LTTNG_KERNEL_TRACEPOINTS and
2885 * events already registered to the channel.
2887 ret
= event_kernel_enable_all_tracepoints(session
->kernel_session
,
2888 kchan
, kernel_tracer_fd
);
2890 case LTTNG_EVENT_ALL
:
2891 /* Enable syscalls and tracepoints */
2892 ret
= event_kernel_enable_all(session
->kernel_session
,
2893 kchan
, kernel_tracer_fd
);
2896 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2900 /* Manage return value */
2901 if (ret
!= LTTCOMM_OK
) {
2905 kernel_wait_quiescent(kernel_tracer_fd
);
2907 case LTTNG_DOMAIN_UST
:
2909 struct lttng_channel
*attr
;
2910 struct ltt_ust_channel
*uchan
;
2911 struct ltt_ust_session
*usess
= session
->ust_session
;
2913 /* Get channel from global UST domain */
2914 uchan
= trace_ust_find_channel_by_name(usess
->domain_global
.channels
,
2916 if (uchan
== NULL
) {
2917 /* Create default channel */
2918 attr
= channel_new_default_attr(domain
);
2920 ret
= LTTCOMM_FATAL
;
2923 snprintf(attr
->name
, NAME_MAX
, "%s", channel_name
);
2924 attr
->name
[NAME_MAX
- 1] = '\0';
2926 /* Use the internal command enable channel */
2927 ret
= channel_ust_create(usess
, domain
, attr
);
2928 if (ret
!= LTTCOMM_OK
) {
2934 /* Get the newly created channel reference back */
2935 uchan
= trace_ust_find_channel_by_name(
2936 usess
->domain_global
.channels
, channel_name
);
2937 if (uchan
== NULL
) {
2938 /* Something is really wrong */
2939 ret
= LTTCOMM_FATAL
;
2944 /* At this point, the session and channel exist on the tracer */
2946 switch (event_type
) {
2947 case LTTNG_EVENT_ALL
:
2948 case LTTNG_EVENT_TRACEPOINT
:
2949 ret
= event_ust_enable_all_tracepoints(usess
, domain
, uchan
);
2950 if (ret
!= LTTCOMM_OK
) {
2955 ret
= LTTCOMM_UST_ENABLE_FAIL
;
2959 /* Manage return value */
2960 if (ret
!= LTTCOMM_OK
) {
2967 case LTTNG_DOMAIN_UST_EXEC_NAME
:
2968 case LTTNG_DOMAIN_UST_PID
:
2969 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN
:
2983 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2985 static ssize_t
cmd_list_tracepoints(int domain
, struct lttng_event
**events
)
2988 ssize_t nb_events
= 0;
2991 case LTTNG_DOMAIN_KERNEL
:
2992 nb_events
= kernel_list_events(kernel_tracer_fd
, events
);
2993 if (nb_events
< 0) {
2994 ret
= LTTCOMM_KERN_LIST_FAIL
;
2998 case LTTNG_DOMAIN_UST
:
2999 nb_events
= ust_app_list_events(events
);
3000 if (nb_events
< 0) {
3001 ret
= LTTCOMM_UST_LIST_FAIL
;
3013 /* Return negative value to differentiate return code */
3018 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
3020 static ssize_t
cmd_list_tracepoint_fields(int domain
,
3021 struct lttng_event_field
**fields
)
3024 ssize_t nb_fields
= 0;
3027 case LTTNG_DOMAIN_UST
:
3028 nb_fields
= ust_app_list_event_fields(fields
);
3029 if (nb_fields
< 0) {
3030 ret
= LTTCOMM_UST_LIST_FAIL
;
3034 case LTTNG_DOMAIN_KERNEL
:
3035 default: /* fall-through */
3043 /* Return negative value to differentiate return code */
3048 * Command LTTNG_START_TRACE processed by the client thread.
3050 static int cmd_start_trace(struct ltt_session
*session
)
3053 struct ltt_kernel_session
*ksession
;
3054 struct ltt_ust_session
*usess
;
3055 struct ltt_kernel_channel
*kchan
;
3057 /* Ease our life a bit ;) */
3058 ksession
= session
->kernel_session
;
3059 usess
= session
->ust_session
;
3061 if (session
->enabled
) {
3062 /* Already started. */
3063 ret
= LTTCOMM_TRACE_ALREADY_STARTED
;
3067 session
->enabled
= 1;
3069 ret
= setup_relayd(session
);
3070 if (ret
!= LTTCOMM_OK
) {
3071 ERR("Error setting up relayd for session %s", session
->name
);
3075 /* Kernel tracing */
3076 if (ksession
!= NULL
) {
3077 /* Open kernel metadata */
3078 if (ksession
->metadata
== NULL
) {
3079 ret
= kernel_open_metadata(ksession
,
3080 ksession
->consumer
->dst
.trace_path
);
3082 ret
= LTTCOMM_KERN_META_FAIL
;
3087 /* Open kernel metadata stream */
3088 if (ksession
->metadata_stream_fd
< 0) {
3089 ret
= kernel_open_metadata_stream(ksession
);
3091 ERR("Kernel create metadata stream failed");
3092 ret
= LTTCOMM_KERN_STREAM_FAIL
;
3097 /* For each channel */
3098 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
3099 if (kchan
->stream_count
== 0) {
3100 ret
= kernel_open_channel_stream(kchan
);
3102 ret
= LTTCOMM_KERN_STREAM_FAIL
;
3105 /* Update the stream global counter */
3106 ksession
->stream_count_global
+= ret
;
3110 /* Setup kernel consumer socket and send fds to it */
3111 ret
= init_kernel_tracing(ksession
);
3113 ret
= LTTCOMM_KERN_START_FAIL
;
3117 /* This start the kernel tracing */
3118 ret
= kernel_start_session(ksession
);
3120 ret
= LTTCOMM_KERN_START_FAIL
;
3124 /* Quiescent wait after starting trace */
3125 kernel_wait_quiescent(kernel_tracer_fd
);
3128 /* Flag session that trace should start automatically */
3130 usess
->start_trace
= 1;
3132 ret
= ust_app_start_trace_all(usess
);
3134 ret
= LTTCOMM_UST_START_FAIL
;
3146 * Command LTTNG_STOP_TRACE processed by the client thread.
3148 static int cmd_stop_trace(struct ltt_session
*session
)
3151 struct ltt_kernel_channel
*kchan
;
3152 struct ltt_kernel_session
*ksession
;
3153 struct ltt_ust_session
*usess
;
3156 ksession
= session
->kernel_session
;
3157 usess
= session
->ust_session
;
3159 if (!session
->enabled
) {
3160 ret
= LTTCOMM_TRACE_ALREADY_STOPPED
;
3164 session
->enabled
= 0;
3167 if (ksession
!= NULL
) {
3168 DBG("Stop kernel tracing");
3170 /* Flush metadata if exist */
3171 if (ksession
->metadata_stream_fd
>= 0) {
3172 ret
= kernel_metadata_flush_buffer(ksession
->metadata_stream_fd
);
3174 ERR("Kernel metadata flush failed");
3178 /* Flush all buffers before stopping */
3179 cds_list_for_each_entry(kchan
, &ksession
->channel_list
.head
, list
) {
3180 ret
= kernel_flush_buffer(kchan
);
3182 ERR("Kernel flush buffer error");
3186 ret
= kernel_stop_session(ksession
);
3188 ret
= LTTCOMM_KERN_STOP_FAIL
;
3192 kernel_wait_quiescent(kernel_tracer_fd
);
3196 usess
->start_trace
= 0;
3198 ret
= ust_app_stop_trace_all(usess
);
3200 ret
= LTTCOMM_UST_STOP_FAIL
;
3212 * Command LTTNG_CREATE_SESSION_URI processed by the client thread.
3214 static int cmd_create_session_uri(char *name
, struct lttng_uri
*ctrl_uri
,
3215 struct lttng_uri
*data_uri
, unsigned int enable_consumer
,
3216 lttng_sock_cred
*creds
)
3220 struct ltt_session
*session
;
3221 struct consumer_output
*consumer
;
3223 /* Verify if the session already exist */
3224 session
= session_find_by_name(name
);
3225 if (session
!= NULL
) {
3226 ret
= LTTCOMM_EXIST_SESS
;
3230 /* TODO: validate URIs */
3232 /* Create default consumer output */
3233 consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
3234 if (consumer
== NULL
) {
3235 ret
= LTTCOMM_FATAL
;
3238 strncpy(consumer
->subdir
, ctrl_uri
->subdir
, sizeof(consumer
->subdir
));
3239 DBG2("Consumer subdir set to %s", consumer
->subdir
);
3241 switch (ctrl_uri
->dtype
) {
3242 case LTTNG_DST_IPV4
:
3243 case LTTNG_DST_IPV6
:
3244 /* Set control URI into consumer output object */
3245 ret
= consumer_set_network_uri(consumer
, ctrl_uri
);
3247 ret
= LTTCOMM_FATAL
;
3251 /* Set data URI into consumer output object */
3252 ret
= consumer_set_network_uri(consumer
, data_uri
);
3254 ret
= LTTCOMM_FATAL
;
3258 /* Empty path since the session is network */
3261 case LTTNG_DST_PATH
:
3262 /* Very volatile pointer. Only used for the create session. */
3263 path
= ctrl_uri
->dst
.path
;
3264 strncpy(consumer
->dst
.trace_path
, path
,
3265 sizeof(consumer
->dst
.trace_path
));
3269 /* Set if the consumer is enabled or not */
3270 consumer
->enabled
= enable_consumer
;