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
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <semaphore.h>
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <urcu/futex.h>
41 #include <ltt-kconsumerd.h>
42 #include <lttng-sessiond-comm.h>
43 #include <lttng/lttng-kconsumerd.h>
46 #include "compat/poll.h"
49 #include "kernel-ctl.h"
50 #include "ltt-sessiond.h"
52 #include "traceable-app.h"
57 #include "benchmark.h"
60 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
61 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
62 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
63 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
66 int opt_verbose
; /* Not static for lttngerr.h */
67 int opt_verbose_kconsumerd
; /* Not static for lttngerr.h */
68 int opt_quiet
; /* Not static for lttngerr.h */
71 const char *opt_tracing_group
;
72 static int opt_sig_parent
;
73 static int opt_daemon
;
74 static int is_root
; /* Set to 1 if the daemon is running as root */
75 static pid_t ppid
; /* Parent PID for --sig-parent option */
76 static pid_t kconsumerd_pid
;
77 static int dispatch_thread_exit
;
79 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
80 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
81 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
82 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
83 static char wait_shm_path
[PATH_MAX
]; /* global wait shm path for UST */
86 static int client_sock
;
88 static int kconsumerd_err_sock
;
89 static int kconsumerd_cmd_sock
;
90 static int kernel_tracer_fd
;
91 static int kernel_poll_pipe
[2];
94 * Quit pipe for all threads. This permits a single cancellation point
95 * for all threads when receiving an event on the pipe.
97 static int thread_quit_pipe
[2];
100 * This pipe is used to inform the thread managing application communication
101 * that a command is queued and ready to be processed.
103 static int apps_cmd_pipe
[2];
105 /* Pthread, Mutexes and Semaphores */
106 static pthread_t kconsumerd_thread
;
107 static pthread_t apps_thread
;
108 static pthread_t reg_apps_thread
;
109 static pthread_t client_thread
;
110 static pthread_t kernel_thread
;
111 static pthread_t dispatch_thread
;
112 static sem_t kconsumerd_sem
;
115 /* Mutex to control kconsumerd pid assignation */
116 static pthread_mutex_t kconsumerd_pid_mutex
;
119 * UST registration command queue. This queue is tied with a futex and uses a N
120 * wakers / 1 waiter implemented and detailed in futex.c/.h
122 * The thread_manage_apps and thread_dispatch_ust_registration interact with
123 * this queue and the wait/wake scheme.
125 static struct ust_cmd_queue ust_cmd_queue
;
128 * Pointer initialized before thread creation.
130 * This points to the tracing session list containing the session count and a
131 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
132 * MUST NOT be taken if you call a public function in session.c.
134 * The lock is nested inside the structure: session_list_ptr->lock. Please use
135 * lock_session_list and unlock_session_list for lock acquisition.
137 static struct ltt_session_list
*session_list_ptr
;
140 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
142 static int create_thread_poll_set(struct lttng_poll_event
*events
,
147 if (events
== NULL
|| size
== 0) {
152 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
158 ret
= lttng_poll_add(events
, thread_quit_pipe
[0], LPOLLIN
);
170 * Check if the thread quit pipe was triggered.
172 * Return 1 if it was triggered else 0;
174 static int check_thread_quit_pipe(int fd
, uint32_t events
)
176 if (fd
== thread_quit_pipe
[0] && (events
& LPOLLIN
)) {
184 * Remove modules in reverse load order.
186 static int modprobe_remove_kernel_modules(void)
191 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
192 ret
= snprintf(modprobe
, sizeof(modprobe
),
193 "/sbin/modprobe --remove --quiet %s",
194 kernel_modules_list
[i
].name
);
196 perror("snprintf modprobe --remove");
199 modprobe
[sizeof(modprobe
) - 1] = '\0';
200 ret
= system(modprobe
);
202 ERR("Unable to launch modprobe --remove for module %s",
203 kernel_modules_list
[i
].name
);
204 } else if (kernel_modules_list
[i
].required
205 && WEXITSTATUS(ret
) != 0) {
206 ERR("Unable to remove module %s",
207 kernel_modules_list
[i
].name
);
209 DBG("Modprobe removal successful %s",
210 kernel_modules_list
[i
].name
);
219 * Return group ID of the tracing group or -1 if not found.
221 static gid_t
allowed_group(void)
225 if (opt_tracing_group
) {
226 grp
= getgrnam(opt_tracing_group
);
228 grp
= getgrnam(default_tracing_group
);
238 * Init thread quit pipe.
240 * Return -1 on error or 0 if all pipes are created.
242 static int init_thread_quit_pipe(void)
246 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
248 perror("thread quit pipe");
257 * Complete teardown of a kernel session. This free all data structure related
258 * to a kernel session and update counter.
260 static void teardown_kernel_session(struct ltt_session
*session
)
262 if (session
->kernel_session
!= NULL
) {
263 DBG("Tearing down kernel session");
266 * If a custom kernel consumer was registered, close the socket before
267 * tearing down the complete kernel session structure
269 if (session
->kernel_session
->consumer_fd
!= kconsumerd_cmd_sock
) {
270 lttcomm_close_unix_sock(session
->kernel_session
->consumer_fd
);
273 trace_kernel_destroy_session(session
->kernel_session
);
274 /* Extra precaution */
275 session
->kernel_session
= NULL
;
280 * Stop all threads by closing the thread quit pipe.
282 static void stop_threads(void)
286 /* Stopping all threads */
287 DBG("Terminating all threads");
288 ret
= write(thread_quit_pipe
[1], "!", 1);
290 ERR("write error on thread quit pipe");
293 /* Dispatch thread */
294 dispatch_thread_exit
= 1;
295 futex_nto1_wake(&ust_cmd_queue
.futex
);
301 static void cleanup(void)
305 struct ltt_session
*sess
, *stmp
;
310 MSG("%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
311 "Matthew, BEET driven development works!%c[%dm",
312 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
316 DBG("Removing %s directory", LTTNG_RUNDIR
);
317 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
319 ERR("asprintf failed. Something is really wrong!");
322 /* Remove lttng run directory */
325 ERR("Unable to clean " LTTNG_RUNDIR
);
329 DBG("Cleaning up all session");
331 /* Destroy session list mutex */
332 if (session_list_ptr
!= NULL
) {
333 pthread_mutex_destroy(&session_list_ptr
->lock
);
335 /* Cleanup ALL session */
336 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
337 teardown_kernel_session(sess
);
338 // TODO complete session cleanup (including UST)
342 DBG("Closing all UST sockets");
343 clean_traceable_apps_list();
345 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
347 DBG("Closing kernel fd");
348 close(kernel_tracer_fd
);
351 DBG("Unloading kernel modules");
352 modprobe_remove_kernel_modules();
355 close(thread_quit_pipe
[0]);
356 close(thread_quit_pipe
[1]);
358 /* OUTPUT BENCHMARK RESULTS */
361 if (getenv("BENCH_UST_NOTIFY")) {
362 bench_print_ust_notification();
365 if (getenv("BENCH_UST_REGISTER")) {
366 bench_print_ust_register();
369 if (getenv("BENCH_BOOT_PROCESS")) {
370 bench_print_boot_process();
378 * Send data on a unix socket using the liblttsessiondcomm API.
380 * Return lttcomm error code.
382 static int send_unix_sock(int sock
, void *buf
, size_t len
)
384 /* Check valid length */
389 return lttcomm_send_unix_sock(sock
, buf
, len
);
393 * Free memory of a command context structure.
395 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
397 DBG("Clean command context structure");
399 if ((*cmd_ctx
)->llm
) {
400 free((*cmd_ctx
)->llm
);
402 if ((*cmd_ctx
)->lsm
) {
403 free((*cmd_ctx
)->lsm
);
411 * Send all stream fds of kernel channel to the consumer.
413 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
417 struct ltt_kernel_stream
*stream
;
418 struct lttcomm_kconsumerd_header lkh
;
419 struct lttcomm_kconsumerd_msg lkm
;
421 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
423 nb_fd
= channel
->stream_count
;
426 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
427 lkh
.cmd_type
= ADD_STREAM
;
429 DBG("Sending kconsumerd header");
431 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
433 perror("send kconsumerd header");
437 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
438 if (stream
->fd
!= 0) {
440 lkm
.state
= stream
->state
;
441 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
442 lkm
.output
= channel
->channel
->attr
.output
;
443 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
444 lkm
.path_name
[PATH_MAX
- 1] = '\0';
446 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
448 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
450 perror("send kconsumerd fd");
456 DBG("Kconsumerd channel fds sent");
465 * Send all stream fds of the kernel session to the consumer.
467 static int send_kconsumerd_fds(struct ltt_kernel_session
*session
)
470 struct ltt_kernel_channel
*chan
;
471 struct lttcomm_kconsumerd_header lkh
;
472 struct lttcomm_kconsumerd_msg lkm
;
475 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
476 lkh
.cmd_type
= ADD_STREAM
;
478 DBG("Sending kconsumerd header for metadata");
480 ret
= lttcomm_send_unix_sock(session
->consumer_fd
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
482 perror("send kconsumerd header");
486 DBG("Sending metadata stream fd");
488 /* Extra protection. It's NOT suppose to be set to 0 at this point */
489 if (session
->consumer_fd
== 0) {
490 session
->consumer_fd
= kconsumerd_cmd_sock
;
493 if (session
->metadata_stream_fd
!= 0) {
494 /* Send metadata stream fd first */
495 lkm
.fd
= session
->metadata_stream_fd
;
496 lkm
.state
= ACTIVE_FD
;
497 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
498 lkm
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
499 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
500 lkm
.path_name
[PATH_MAX
- 1] = '\0';
502 ret
= lttcomm_send_fds_unix_sock(session
->consumer_fd
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
504 perror("send kconsumerd fd");
509 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
510 ret
= send_kconsumerd_channel_fds(session
->consumer_fd
, chan
);
516 DBG("Kconsumerd fds (metadata and channel streams) sent");
525 * Notify UST applications using the shm mmap futex.
527 static int notify_ust_apps(int active
)
531 DBG("Notifying applications of session daemon state: %d", active
);
533 tracepoint(ust_notify_apps_start
);
535 /* See shm.c for this call implying mmap, shm and futex calls */
536 wait_shm_mmap
= shm_ust_get_mmap(wait_shm_path
, is_root
);
537 if (wait_shm_mmap
== NULL
) {
541 /* Wake waiting process */
542 futex_wait_update((int32_t *) wait_shm_mmap
, active
);
544 tracepoint(ust_notify_apps_stop
);
546 /* Apps notified successfully */
554 * Setup the outgoing data buffer for the response (llm) by allocating the
555 * right amount of memory and copying the original information from the lsm
558 * Return total size of the buffer pointed by buf.
560 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
566 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
567 if (cmd_ctx
->llm
== NULL
) {
573 /* Copy common data */
574 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
575 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
577 cmd_ctx
->llm
->data_size
= size
;
578 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
587 * Update the kernel poll set of all channel fd available over all tracing
588 * session. Add the wakeup pipe at the end of the set.
590 static int update_kernel_poll(struct lttng_poll_event
*events
)
593 struct ltt_session
*session
;
594 struct ltt_kernel_channel
*channel
;
596 DBG("Updating kernel poll set");
599 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
600 lock_session(session
);
601 if (session
->kernel_session
== NULL
) {
602 unlock_session(session
);
606 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
607 /* Add channel fd to the kernel poll set */
608 ret
= lttng_poll_add(events
, channel
->fd
, LPOLLIN
| LPOLLRDNORM
);
610 unlock_session(session
);
613 DBG("Channel fd %d added to kernel set", channel
->fd
);
615 unlock_session(session
);
617 unlock_session_list();
622 unlock_session_list();
627 * Find the channel fd from 'fd' over all tracing session. When found, check
628 * for new channel stream and send those stream fds to the kernel consumer.
630 * Useful for CPU hotplug feature.
632 static int update_kernel_stream(int fd
)
635 struct ltt_session
*session
;
636 struct ltt_kernel_channel
*channel
;
638 DBG("Updating kernel streams for channel fd %d", fd
);
641 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
642 lock_session(session
);
643 if (session
->kernel_session
== NULL
) {
644 unlock_session(session
);
648 /* This is not suppose to be 0 but this is an extra security check */
649 if (session
->kernel_session
->consumer_fd
== 0) {
650 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
653 cds_list_for_each_entry(channel
,
654 &session
->kernel_session
->channel_list
.head
, list
) {
655 if (channel
->fd
== fd
) {
656 DBG("Channel found, updating kernel streams");
657 ret
= kernel_open_channel_stream(channel
);
663 * Have we already sent fds to the consumer? If yes, it means
664 * that tracing is started so it is safe to send our updated
667 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
668 ret
= send_kconsumerd_channel_fds(
669 session
->kernel_session
->consumer_fd
, channel
);
677 unlock_session(session
);
679 unlock_session_list();
683 unlock_session(session
);
684 unlock_session_list();
689 * This thread manage event coming from the kernel.
691 * Features supported in this thread:
694 static void *thread_manage_kernel(void *data
)
696 int ret
, i
, pollfd
, update_poll_flag
= 1;
697 uint32_t revents
, nb_fd
;
699 struct lttng_poll_event events
;
701 tracepoint(sessiond_th_kern_start
);
703 DBG("Thread manage kernel started");
705 ret
= create_thread_poll_set(&events
, 2);
710 ret
= lttng_poll_add(&events
, kernel_poll_pipe
[0], LPOLLIN
);
716 if (update_poll_flag
== 1) {
717 ret
= update_kernel_poll(&events
);
721 update_poll_flag
= 0;
724 nb_fd
= LTTNG_POLL_GETNB(&events
);
726 DBG("Thread kernel polling on %d fds", nb_fd
);
728 /* Zeroed the poll events */
729 lttng_poll_reset(&events
);
731 tracepoint(sessiond_th_kern_poll
);
733 /* Poll infinite value of time */
734 ret
= lttng_poll_wait(&events
, -1);
737 } else if (ret
== 0) {
738 /* Should not happen since timeout is infinite */
739 ERR("Return value of poll is 0 with an infinite timeout.\n"
740 "This should not have happened! Continuing...");
744 for (i
= 0; i
< nb_fd
; i
++) {
745 /* Fetch once the poll data */
746 revents
= LTTNG_POLL_GETEV(&events
, i
);
747 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
749 /* Thread quit pipe has been closed. Killing thread. */
750 ret
= check_thread_quit_pipe(pollfd
, revents
);
755 /* Check for data on kernel pipe */
756 if (pollfd
== kernel_poll_pipe
[0] && (revents
& LPOLLIN
)) {
757 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
758 update_poll_flag
= 1;
762 * New CPU detected by the kernel. Adding kernel stream to
763 * kernel session and updating the kernel consumer
765 if (revents
& LPOLLIN
) {
766 ret
= update_kernel_stream(pollfd
);
772 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
773 * and unregister kernel stream at this point.
781 DBG("Kernel thread dying");
782 close(kernel_poll_pipe
[0]);
783 close(kernel_poll_pipe
[1]);
785 lttng_poll_clean(&events
);
791 * This thread manage the kconsumerd error sent back to the session daemon.
793 static void *thread_manage_kconsumerd(void *data
)
795 int sock
= 0, i
, ret
, pollfd
;
796 uint32_t revents
, nb_fd
;
797 enum lttcomm_return_code code
;
798 struct lttng_poll_event events
;
800 tracepoint(sessiond_th_kcon_start
);
802 DBG("[thread] Manage kconsumerd started");
804 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
810 * Pass 2 as size here for the thread quit pipe and kconsumerd_err_sock.
811 * Nothing more will be added to this poll set.
813 ret
= create_thread_poll_set(&events
, 2);
818 ret
= lttng_poll_add(&events
, kconsumerd_err_sock
, LPOLLIN
| LPOLLRDHUP
);
823 nb_fd
= LTTNG_POLL_GETNB(&events
);
825 tracepoint(sessiond_th_kcon_poll
);
827 /* Inifinite blocking call, waiting for transmission */
828 ret
= lttng_poll_wait(&events
, -1);
833 for (i
= 0; i
< nb_fd
; i
++) {
834 /* Fetch once the poll data */
835 revents
= LTTNG_POLL_GETEV(&events
, i
);
836 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
838 /* Thread quit pipe has been closed. Killing thread. */
839 ret
= check_thread_quit_pipe(pollfd
, revents
);
844 /* Event on the registration socket */
845 if (pollfd
== kconsumerd_err_sock
) {
846 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
847 ERR("Kconsumerd err socket poll error");
853 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
858 /* Getting status code from kconsumerd */
859 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
864 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
865 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
866 if (kconsumerd_cmd_sock
< 0) {
867 sem_post(&kconsumerd_sem
);
868 perror("kconsumerd connect");
871 /* Signal condition to tell that the kconsumerd is ready */
872 sem_post(&kconsumerd_sem
);
873 DBG("Kconsumerd command socket ready");
875 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
876 lttcomm_get_readable_code(-code
));
880 /* Remove the kconsumerd error socket since we have established a connexion */
881 ret
= lttng_poll_del(&events
, kconsumerd_err_sock
);
886 ret
= lttng_poll_add(&events
, sock
, LPOLLIN
| LPOLLRDHUP
);
891 /* Update number of fd */
892 nb_fd
= LTTNG_POLL_GETNB(&events
);
894 /* Inifinite blocking call, waiting for transmission */
895 ret
= lttng_poll_wait(&events
, -1);
900 for (i
= 0; i
< nb_fd
; i
++) {
901 /* Fetch once the poll data */
902 revents
= LTTNG_POLL_GETEV(&events
, i
);
903 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
905 /* Thread quit pipe has been closed. Killing thread. */
906 ret
= check_thread_quit_pipe(pollfd
, revents
);
911 /* Event on the kconsumerd socket */
912 if (pollfd
== sock
) {
913 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
914 ERR("Kconsumerd err socket second poll error");
920 /* Wait for any kconsumerd error */
921 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
923 ERR("Kconsumerd closed the command socket");
927 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
930 DBG("Kconsumerd thread dying");
931 close(kconsumerd_err_sock
);
932 close(kconsumerd_cmd_sock
);
935 unlink(kconsumerd_err_unix_sock_path
);
936 unlink(kconsumerd_cmd_unix_sock_path
);
939 lttng_poll_clean(&events
);
945 * This thread manage application communication.
947 static void *thread_manage_apps(void *data
)
950 uint32_t revents
, nb_fd
;
951 struct ust_command ust_cmd
;
952 struct lttng_poll_event events
;
954 tracepoint(sessiond_th_apps_start
);
956 DBG("[thread] Manage application started");
958 ret
= create_thread_poll_set(&events
, 2);
963 ret
= lttng_poll_add(&events
, apps_cmd_pipe
[0], LPOLLIN
| LPOLLRDHUP
);
969 /* Zeroed the events structure */
970 lttng_poll_reset(&events
);
972 nb_fd
= LTTNG_POLL_GETNB(&events
);
974 DBG("Apps thread polling on %d fds", nb_fd
);
976 tracepoint(sessiond_th_apps_poll
);
978 /* Inifinite blocking call, waiting for transmission */
979 ret
= lttng_poll_wait(&events
, -1);
984 for (i
= 0; i
< nb_fd
; i
++) {
985 /* Fetch once the poll data */
986 revents
= LTTNG_POLL_GETEV(&events
, i
);
987 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
989 /* Thread quit pipe has been closed. Killing thread. */
990 ret
= check_thread_quit_pipe(pollfd
, revents
);
995 /* Inspect the apps cmd pipe */
996 if (pollfd
== apps_cmd_pipe
[0]) {
997 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
998 ERR("Apps command pipe error");
1000 } else if (revents
& LPOLLIN
) {
1001 tracepoint(ust_register_read_start
);
1003 ret
= read(apps_cmd_pipe
[0], &ust_cmd
, sizeof(ust_cmd
));
1004 if (ret
< 0 || ret
< sizeof(ust_cmd
)) {
1005 perror("read apps cmd pipe");
1009 /* Register applicaton to the session daemon */
1010 ret
= register_traceable_app(&ust_cmd
.reg_msg
, ust_cmd
.sock
);
1012 /* Only critical ENOMEM error can be returned here */
1016 ret
= ustctl_register_done(ust_cmd
.sock
);
1019 * If the registration is not possible, we simply
1020 * unregister the apps and continue
1022 unregister_traceable_app(ust_cmd
.sock
);
1025 * We just need here to monitor the close of the UST
1026 * socket and poll set monitor those by default.
1028 ret
= lttng_poll_add(&events
, ust_cmd
.sock
, 0);
1033 DBG("Apps with sock %d added to poll set", ust_cmd
.sock
);
1039 * At this point, we know that a registered application made the
1040 * event at poll_wait.
1042 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1043 /* Removing from the poll set */
1044 ret
= lttng_poll_del(&events
, pollfd
);
1050 unregister_traceable_app(pollfd
);
1058 DBG("Application communication apps dying");
1059 close(apps_cmd_pipe
[0]);
1060 close(apps_cmd_pipe
[1]);
1062 lttng_poll_clean(&events
);
1068 * Dispatch request from the registration threads to the application
1069 * communication thread.
1071 static void *thread_dispatch_ust_registration(void *data
)
1074 struct cds_wfq_node
*node
;
1075 struct ust_command
*ust_cmd
= NULL
;
1077 tracepoint(sessiond_th_dispatch_start
);
1079 DBG("[thread] Dispatch UST command started");
1081 while (!dispatch_thread_exit
) {
1082 /* Atomically prepare the queue futex */
1083 futex_nto1_prepare(&ust_cmd_queue
.futex
);
1086 tracepoint(sessiond_th_dispatch_block
);
1088 /* Dequeue command for registration */
1089 node
= cds_wfq_dequeue_blocking(&ust_cmd_queue
.queue
);
1091 DBG("Waked up but nothing in the UST command queue");
1092 /* Continue thread execution */
1096 tracepoint(ust_dispatch_register_start
);
1098 ust_cmd
= caa_container_of(node
, struct ust_command
, node
);
1100 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1101 " gid:%d sock:%d name:%s (version %d.%d)",
1102 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1103 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1104 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1105 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1107 * Inform apps thread of the new application registration. This
1108 * call is blocking so we can be assured that the data will be read
1109 * at some point in time or wait to the end of the world :)
1111 ret
= write(apps_cmd_pipe
[1], ust_cmd
,
1112 sizeof(struct ust_command
));
1114 perror("write apps cmd pipe");
1115 if (errno
== EBADF
) {
1117 * We can't inform the application thread to process
1118 * registration. We will exit or else application
1119 * registration will not occur and tracing will never
1126 } while (node
!= NULL
);
1128 tracepoint(ust_dispatch_register_stop
);
1130 /* Futex wait on queue. Blocking call on futex() */
1131 futex_nto1_wait(&ust_cmd_queue
.futex
);
1135 DBG("Dispatch thread dying");
1140 * This thread manage application registration.
1142 static void *thread_registration_apps(void *data
)
1144 int sock
= 0, i
, ret
, pollfd
;
1145 uint32_t revents
, nb_fd
;
1146 struct lttng_poll_event events
;
1148 * Get allocated in this thread, enqueued to a global queue, dequeued and
1149 * freed in the manage apps thread.
1151 struct ust_command
*ust_cmd
= NULL
;
1153 tracepoint(sessiond_th_reg_start
);
1155 DBG("[thread] Manage application registration started");
1157 ret
= lttcomm_listen_unix_sock(apps_sock
);
1163 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1164 * more will be added to this poll set.
1166 ret
= create_thread_poll_set(&events
, 2);
1171 /* Add the application registration socket */
1172 ret
= lttng_poll_add(&events
, apps_sock
, LPOLLIN
| LPOLLRDHUP
);
1177 /* Notify all applications to register */
1178 ret
= notify_ust_apps(1);
1180 ERR("Failed to notify applications or create the wait shared memory.\n"
1181 "Execution continues but there might be problem for already running\n"
1182 "applications that wishes to register.");
1186 DBG("Accepting application registration");
1188 tracepoint(sessiond_th_reg_poll
);
1190 nb_fd
= LTTNG_POLL_GETNB(&events
);
1192 /* Inifinite blocking call, waiting for transmission */
1193 ret
= lttng_poll_wait(&events
, -1);
1198 for (i
= 0; i
< nb_fd
; i
++) {
1199 /* Fetch once the poll data */
1200 revents
= LTTNG_POLL_GETEV(&events
, i
);
1201 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
1203 /* Thread quit pipe has been closed. Killing thread. */
1204 ret
= check_thread_quit_pipe(pollfd
, revents
);
1209 /* Event on the registration socket */
1210 if (pollfd
== apps_sock
) {
1211 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
1212 ERR("Register apps socket poll error");
1214 } else if (revents
& LPOLLIN
) {
1215 /* Registration starts here. Recording cycles */
1216 tracepoint(ust_register_start
);
1218 sock
= lttcomm_accept_unix_sock(apps_sock
);
1223 /* Create UST registration command for enqueuing */
1224 ust_cmd
= malloc(sizeof(struct ust_command
));
1225 if (ust_cmd
== NULL
) {
1226 perror("ust command malloc");
1231 * Using message-based transmissions to ensure we don't
1232 * have to deal with partially received messages.
1234 ret
= lttcomm_recv_unix_sock(sock
, &ust_cmd
->reg_msg
,
1235 sizeof(struct ust_register_msg
));
1236 if (ret
< 0 || ret
< sizeof(struct ust_register_msg
)) {
1238 perror("lttcomm_recv_unix_sock register apps");
1240 ERR("Wrong size received on apps register");
1247 ust_cmd
->sock
= sock
;
1249 DBG("UST registration received with pid:%d ppid:%d uid:%d"
1250 " gid:%d sock:%d name:%s (version %d.%d)",
1251 ust_cmd
->reg_msg
.pid
, ust_cmd
->reg_msg
.ppid
,
1252 ust_cmd
->reg_msg
.uid
, ust_cmd
->reg_msg
.gid
,
1253 ust_cmd
->sock
, ust_cmd
->reg_msg
.name
,
1254 ust_cmd
->reg_msg
.major
, ust_cmd
->reg_msg
.minor
);
1256 * Lock free enqueue the registration request. The red pill
1257 * has been taken! This apps will be part of the *system* :)
1259 cds_wfq_enqueue(&ust_cmd_queue
.queue
, &ust_cmd
->node
);
1262 * Wake the registration queue futex. Implicit memory
1263 * barrier with the exchange in cds_wfq_enqueue.
1265 futex_nto1_wake(&ust_cmd_queue
.futex
);
1267 tracepoint(ust_register_stop
);
1274 DBG("UST Registration thread dying");
1276 /* Notify that the registration thread is gone */
1281 unlink(apps_unix_sock_path
);
1283 lttng_poll_clean(&events
);
1289 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
1290 * exec or it will fails.
1292 static int spawn_kconsumerd_thread(void)
1296 /* Setup semaphore */
1297 sem_init(&kconsumerd_sem
, 0, 0);
1299 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
1301 perror("pthread_create kconsumerd");
1305 /* Wait for the kconsumerd thread to be ready */
1306 sem_wait(&kconsumerd_sem
);
1308 if (kconsumerd_pid
== 0) {
1309 ERR("Kconsumerd did not start");
1316 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1321 * Join kernel consumer thread
1323 static int join_kconsumerd_thread(void)
1328 if (kconsumerd_pid
!= 0) {
1329 ret
= kill(kconsumerd_pid
, SIGTERM
);
1331 ERR("Error killing kconsumerd");
1334 return pthread_join(kconsumerd_thread
, &status
);
1341 * Fork and exec a kernel consumer daemon (kconsumerd).
1343 * Return pid if successful else -1.
1345 static pid_t
spawn_kconsumerd(void)
1349 const char *verbosity
;
1351 DBG("Spawning kconsumerd");
1358 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
1359 verbosity
= "--verbose";
1361 verbosity
= "--quiet";
1363 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
1365 perror("kernel start consumer exec");
1368 } else if (pid
> 0) {
1372 perror("kernel start consumer fork");
1382 * Spawn the kconsumerd daemon and session daemon thread.
1384 static int start_kconsumerd(void)
1388 pthread_mutex_lock(&kconsumerd_pid_mutex
);
1389 if (kconsumerd_pid
!= 0) {
1390 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1394 ret
= spawn_kconsumerd();
1396 ERR("Spawning kconsumerd failed");
1397 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1398 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1402 /* Setting up the global kconsumerd_pid */
1403 kconsumerd_pid
= ret
;
1404 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
1406 DBG("Kconsumerd pid %d", ret
);
1408 DBG("Spawning kconsumerd thread");
1409 ret
= spawn_kconsumerd_thread();
1411 ERR("Fatal error spawning kconsumerd thread");
1423 * modprobe_kernel_modules
1425 static int modprobe_kernel_modules(void)
1430 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
1431 ret
= snprintf(modprobe
, sizeof(modprobe
),
1432 "/sbin/modprobe %s%s",
1433 kernel_modules_list
[i
].required
? "" : "--quiet ",
1434 kernel_modules_list
[i
].name
);
1436 perror("snprintf modprobe");
1439 modprobe
[sizeof(modprobe
) - 1] = '\0';
1440 ret
= system(modprobe
);
1442 ERR("Unable to launch modprobe for module %s",
1443 kernel_modules_list
[i
].name
);
1444 } else if (kernel_modules_list
[i
].required
1445 && WEXITSTATUS(ret
) != 0) {
1446 ERR("Unable to load module %s",
1447 kernel_modules_list
[i
].name
);
1449 DBG("Modprobe successfully %s",
1450 kernel_modules_list
[i
].name
);
1461 static int mount_debugfs(char *path
)
1464 char *type
= "debugfs";
1466 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1471 ret
= mount(type
, path
, type
, 0, NULL
);
1473 perror("mount debugfs");
1477 DBG("Mounted debugfs successfully at %s", path
);
1484 * Setup necessary data for kernel tracer action.
1486 static void init_kernel_tracer(void)
1489 char *proc_mounts
= "/proc/mounts";
1491 char *debugfs_path
= NULL
, *lttng_path
;
1494 /* Detect debugfs */
1495 fp
= fopen(proc_mounts
, "r");
1497 ERR("Unable to probe %s", proc_mounts
);
1501 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1502 if (strstr(line
, "debugfs") != NULL
) {
1503 /* Remove first string */
1505 /* Dup string here so we can reuse line later on */
1506 debugfs_path
= strdup(strtok(NULL
, " "));
1507 DBG("Got debugfs path : %s", debugfs_path
);
1514 /* Mount debugfs if needded */
1515 if (debugfs_path
== NULL
) {
1516 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1518 perror("asprintf debugfs path");
1521 ret
= mount_debugfs(debugfs_path
);
1527 /* Modprobe lttng kernel modules */
1528 ret
= modprobe_kernel_modules();
1533 /* Setup lttng kernel path */
1534 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1536 perror("asprintf lttng path");
1540 /* Open debugfs lttng */
1541 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1542 if (kernel_tracer_fd
< 0) {
1543 DBG("Failed to open %s", lttng_path
);
1549 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1559 WARN("No kernel tracer available");
1560 kernel_tracer_fd
= 0;
1565 * Start tracing by creating trace directory and sending FDs to the kernel
1568 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1572 if (session
->kconsumer_fds_sent
== 0) {
1574 * Assign default kernel consumer if no consumer assigned to the kernel
1575 * session. At this point, it's NOT suppose to be 0 but this is an extra
1578 if (session
->consumer_fd
== 0) {
1579 session
->consumer_fd
= kconsumerd_cmd_sock
;
1582 ret
= send_kconsumerd_fds(session
);
1584 ERR("Send kconsumerd fds failed");
1585 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1589 session
->kconsumer_fds_sent
= 1;
1597 * Notify kernel thread to update it's poll set.
1599 static int notify_kernel_channels_update(void)
1603 /* Inform kernel thread of the new kernel channel */
1604 ret
= write(kernel_poll_pipe
[1], "!", 1);
1606 perror("write kernel poll pipe");
1613 * Allocate a channel structure and fill it.
1615 static struct lttng_channel
*init_default_channel(enum lttng_domain_type domain_type
,
1618 struct lttng_channel
*chan
;
1620 chan
= malloc(sizeof(struct lttng_channel
));
1622 perror("init channel malloc");
1626 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1627 perror("snprintf channel name");
1631 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1632 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1633 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1635 switch (domain_type
) {
1636 case LTTNG_DOMAIN_KERNEL
:
1637 chan
->attr
.subbuf_size
= DEFAULT_KERNEL_CHANNEL_SUBBUF_SIZE
;
1638 chan
->attr
.num_subbuf
= DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM
;
1639 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1643 goto error
; /* Not implemented */
1654 * Create an UST session and add it to the session ust list.
1656 static int create_ust_session(pid_t pid
, struct ltt_session
*session
)
1659 struct ltt_ust_session
*lus
;
1661 DBG("Creating UST session");
1663 lus
= trace_ust_create_session(session
->path
, pid
);
1668 ret
= mkdir_recursive(lus
->path
, S_IRWXU
| S_IRWXG
,
1669 geteuid(), allowed_group());
1671 if (ret
!= -EEXIST
) {
1672 ERR("Trace directory creation error");
1677 /* Create session on the UST tracer */
1678 ret
= ustctl_create_session(lus
);
1691 * Create a kernel tracer session then create the default channel.
1693 static int create_kernel_session(struct ltt_session
*session
)
1697 DBG("Creating kernel session");
1699 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1701 ret
= LTTCOMM_KERN_SESS_FAIL
;
1705 /* Set kernel consumer socket fd */
1706 if (kconsumerd_cmd_sock
) {
1707 session
->kernel_session
->consumer_fd
= kconsumerd_cmd_sock
;
1710 ret
= mkdir_recursive(session
->kernel_session
->trace_path
,
1711 S_IRWXU
| S_IRWXG
, geteuid(), allowed_group());
1713 if (ret
!= -EEXIST
) {
1714 ERR("Trace directory creation error");
1724 * Using the session list, filled a lttng_session array to send back to the
1725 * client for session listing.
1727 * The session list lock MUST be acquired before calling this function. Use
1728 * lock_session_list() and unlock_session_list().
1730 static void list_lttng_sessions(struct lttng_session
*sessions
)
1733 struct ltt_session
*session
;
1735 DBG("Getting all available session");
1737 * Iterate over session list and append data after the control struct in
1740 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1741 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1742 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1743 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1744 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1750 * Fill lttng_channel array of all channels.
1752 static void list_lttng_channels(struct ltt_session
*session
,
1753 struct lttng_channel
*channels
)
1756 struct ltt_kernel_channel
*kchan
;
1758 DBG("Listing channels for session %s", session
->name
);
1760 /* Kernel channels */
1761 if (session
->kernel_session
!= NULL
) {
1762 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1763 /* Copy lttng_channel struct to array */
1764 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1765 channels
[i
].enabled
= kchan
->enabled
;
1770 /* TODO: Missing UST listing */
1774 * Fill lttng_event array of all events in the channel.
1776 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1777 struct lttng_event
*events
)
1780 * TODO: This is ONLY kernel. Need UST support.
1783 struct ltt_kernel_event
*event
;
1785 DBG("Listing events for channel %s", kchan
->channel
->name
);
1787 /* Kernel channels */
1788 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1789 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1790 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1791 events
[i
].enabled
= event
->enabled
;
1792 switch (event
->event
->instrumentation
) {
1793 case LTTNG_KERNEL_TRACEPOINT
:
1794 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1796 case LTTNG_KERNEL_KPROBE
:
1797 case LTTNG_KERNEL_KRETPROBE
:
1798 events
[i
].type
= LTTNG_EVENT_PROBE
;
1799 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1800 sizeof(struct lttng_kernel_kprobe
));
1802 case LTTNG_KERNEL_FUNCTION
:
1803 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1804 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1805 sizeof(struct lttng_kernel_function
));
1813 * Process the command requested by the lttng client within the command
1814 * context structure. This function make sure that the return structure (llm)
1815 * is set and ready for transmission before returning.
1817 * Return any error encountered or 0 for success.
1819 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1821 int ret
= LTTCOMM_OK
;
1823 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1826 * Commands that DO NOT need a session.
1828 switch (cmd_ctx
->lsm
->cmd_type
) {
1829 case LTTNG_CREATE_SESSION
:
1830 case LTTNG_LIST_SESSIONS
:
1831 case LTTNG_LIST_TRACEPOINTS
:
1832 case LTTNG_CALIBRATE
:
1835 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1836 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1837 if (cmd_ctx
->session
== NULL
) {
1838 /* If session name not found */
1839 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1840 ret
= LTTCOMM_SESS_NOT_FOUND
;
1841 } else { /* If no session name specified */
1842 ret
= LTTCOMM_SELECT_SESS
;
1846 /* Acquire lock for the session */
1847 lock_session(cmd_ctx
->session
);
1853 * Check domain type for specific "pre-action".
1855 switch (cmd_ctx
->lsm
->domain
.type
) {
1856 case LTTNG_DOMAIN_KERNEL
:
1857 /* Kernel tracer check */
1858 if (kernel_tracer_fd
== 0) {
1859 init_kernel_tracer();
1860 if (kernel_tracer_fd
== 0) {
1861 ret
= LTTCOMM_KERN_NA
;
1865 /* Need a session for kernel command */
1866 switch (cmd_ctx
->lsm
->cmd_type
) {
1867 case LTTNG_CALIBRATE
:
1868 case LTTNG_CREATE_SESSION
:
1869 case LTTNG_LIST_SESSIONS
:
1870 case LTTNG_LIST_TRACEPOINTS
:
1873 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1874 ret
= create_kernel_session(cmd_ctx
->session
);
1876 ret
= LTTCOMM_KERN_SESS_FAIL
;
1879 /* Start the kernel consumer daemon */
1880 if (kconsumerd_pid
== 0 &&
1881 cmd_ctx
->lsm
->cmd_type
!= LTTNG_REGISTER_CONSUMER
) {
1882 ret
= start_kconsumerd();
1890 case LTTNG_DOMAIN_UST_PID
:
1896 /* Process by command type */
1897 switch (cmd_ctx
->lsm
->cmd_type
) {
1898 case LTTNG_ADD_CONTEXT
:
1900 struct lttng_kernel_context kctx
;
1902 /* Setup lttng message with no payload */
1903 ret
= setup_lttng_msg(cmd_ctx
, 0);
1908 switch (cmd_ctx
->lsm
->domain
.type
) {
1909 case LTTNG_DOMAIN_KERNEL
:
1910 /* Create Kernel context */
1911 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1912 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1913 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1914 strncpy(kctx
.u
.perf_counter
.name
,
1915 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1916 LTTNG_SYMBOL_NAME_LEN
);
1917 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1919 /* Add kernel context to kernel tracer. See context.c */
1920 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1921 cmd_ctx
->lsm
->u
.context
.event_name
,
1922 cmd_ctx
->lsm
->u
.context
.channel_name
);
1923 if (ret
!= LTTCOMM_OK
) {
1928 /* TODO: Userspace tracing */
1929 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1936 case LTTNG_DISABLE_CHANNEL
:
1938 struct ltt_kernel_channel
*kchan
;
1940 /* Setup lttng message with no payload */
1941 ret
= setup_lttng_msg(cmd_ctx
, 0);
1946 switch (cmd_ctx
->lsm
->domain
.type
) {
1947 case LTTNG_DOMAIN_KERNEL
:
1948 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1949 cmd_ctx
->session
->kernel_session
);
1950 if (kchan
== NULL
) {
1951 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1953 } else if (kchan
->enabled
== 1) {
1954 ret
= kernel_disable_channel(kchan
);
1956 if (ret
!= EEXIST
) {
1957 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1962 kernel_wait_quiescent(kernel_tracer_fd
);
1965 /* TODO: Userspace tracing */
1966 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1973 case LTTNG_DISABLE_EVENT
:
1975 struct ltt_kernel_channel
*kchan
;
1976 struct ltt_kernel_event
*kevent
;
1978 /* Setup lttng message with no payload */
1979 ret
= setup_lttng_msg(cmd_ctx
, 0);
1984 switch (cmd_ctx
->lsm
->domain
.type
) {
1985 case LTTNG_DOMAIN_KERNEL
:
1986 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1987 cmd_ctx
->session
->kernel_session
);
1988 if (kchan
== NULL
) {
1989 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1993 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1994 if (kevent
!= NULL
) {
1995 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1996 kchan
->channel
->name
);
1997 ret
= kernel_disable_event(kevent
);
1999 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2004 kernel_wait_quiescent(kernel_tracer_fd
);
2007 /* TODO: Userspace tracing */
2008 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2015 case LTTNG_DISABLE_ALL_EVENT
:
2017 struct ltt_kernel_channel
*kchan
;
2018 struct ltt_kernel_event
*kevent
;
2020 /* Setup lttng message with no payload */
2021 ret
= setup_lttng_msg(cmd_ctx
, 0);
2026 switch (cmd_ctx
->lsm
->domain
.type
) {
2027 case LTTNG_DOMAIN_KERNEL
:
2028 DBG("Disabling all enabled kernel events");
2029 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
2030 cmd_ctx
->session
->kernel_session
);
2031 if (kchan
== NULL
) {
2032 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2036 /* For each event in the kernel session */
2037 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2038 DBG("Disabling kernel event %s for channel %s.",
2039 kevent
->event
->name
, kchan
->channel
->name
);
2040 ret
= kernel_disable_event(kevent
);
2046 /* Quiescent wait after event disable */
2047 kernel_wait_quiescent(kernel_tracer_fd
);
2050 /* TODO: Userspace tracing */
2051 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2058 case LTTNG_ENABLE_CHANNEL
:
2060 struct ltt_kernel_channel
*kchan
;
2062 /* Setup lttng message with no payload */
2063 ret
= setup_lttng_msg(cmd_ctx
, 0);
2068 switch (cmd_ctx
->lsm
->domain
.type
) {
2069 case LTTNG_DOMAIN_KERNEL
:
2070 kchan
= trace_kernel_get_channel_by_name(
2071 cmd_ctx
->lsm
->u
.enable
.channel_name
,
2072 cmd_ctx
->session
->kernel_session
);
2073 if (kchan
== NULL
) {
2074 /* Channel not found, creating it */
2075 DBG("Creating kernel channel %s",
2076 cmd_ctx
->lsm
->u
.enable
.channel_name
);
2078 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2079 &cmd_ctx
->lsm
->u
.channel
.chan
,
2080 cmd_ctx
->session
->kernel_session
->trace_path
);
2082 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2086 /* Notify kernel thread that there is a new channel */
2087 ret
= notify_kernel_channels_update();
2089 ret
= LTTCOMM_FATAL
;
2092 } else if (kchan
->enabled
== 0) {
2093 ret
= kernel_enable_channel(kchan
);
2095 if (ret
!= EEXIST
) {
2096 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
2102 kernel_wait_quiescent(kernel_tracer_fd
);
2104 case LTTNG_DOMAIN_UST_PID
:
2108 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2115 case LTTNG_ENABLE_EVENT
:
2118 struct ltt_kernel_channel
*kchan
;
2119 struct ltt_kernel_event
*kevent
;
2120 struct lttng_channel
*chan
;
2122 /* Setup lttng message with no payload */
2123 ret
= setup_lttng_msg(cmd_ctx
, 0);
2128 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2130 switch (cmd_ctx
->lsm
->domain
.type
) {
2131 case LTTNG_DOMAIN_KERNEL
:
2132 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2133 cmd_ctx
->session
->kernel_session
);
2134 if (kchan
== NULL
) {
2135 DBG("Channel not found. Creating channel %s", channel_name
);
2137 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2139 ret
= LTTCOMM_FATAL
;
2143 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2144 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2146 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2149 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2150 cmd_ctx
->session
->kernel_session
);
2151 if (kchan
== NULL
) {
2152 ERR("Channel %s not found after creation. Internal error, giving up.",
2154 ret
= LTTCOMM_FATAL
;
2158 ret
= notify_kernel_channels_update();
2160 ret
= LTTCOMM_FATAL
;
2165 kevent
= trace_kernel_get_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
2166 if (kevent
== NULL
) {
2167 DBG("Creating kernel event %s for channel %s.",
2168 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
2169 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
2171 DBG("Enabling kernel event %s for channel %s.",
2172 kevent
->event
->name
, channel_name
);
2173 ret
= kernel_enable_event(kevent
);
2174 if (ret
== -EEXIST
) {
2175 ret
= LTTCOMM_KERN_EVENT_EXIST
;
2181 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2185 kernel_wait_quiescent(kernel_tracer_fd
);
2188 /* TODO: Userspace tracing */
2189 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2195 case LTTNG_ENABLE_ALL_EVENT
:
2199 struct ltt_kernel_channel
*kchan
;
2200 struct ltt_kernel_event
*kevent
;
2201 struct lttng_event
*event_list
;
2202 struct lttng_channel
*chan
;
2204 /* Setup lttng message with no payload */
2205 ret
= setup_lttng_msg(cmd_ctx
, 0);
2210 DBG("Enabling all kernel event");
2212 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
2214 switch (cmd_ctx
->lsm
->domain
.type
) {
2215 case LTTNG_DOMAIN_KERNEL
:
2216 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2217 cmd_ctx
->session
->kernel_session
);
2218 if (kchan
== NULL
) {
2219 DBG("Channel not found. Creating channel %s", channel_name
);
2221 chan
= init_default_channel(cmd_ctx
->lsm
->domain
.type
, channel_name
);
2223 ret
= LTTCOMM_FATAL
;
2227 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
2228 chan
, cmd_ctx
->session
->kernel_session
->trace_path
);
2230 ret
= LTTCOMM_KERN_CHAN_FAIL
;
2233 kchan
= trace_kernel_get_channel_by_name(channel_name
,
2234 cmd_ctx
->session
->kernel_session
);
2235 if (kchan
== NULL
) {
2236 ERR("Channel %s not found after creation. Internal error, giving up.",
2238 ret
= LTTCOMM_FATAL
;
2242 ret
= notify_kernel_channels_update();
2244 ret
= LTTCOMM_FATAL
;
2249 /* For each event in the kernel session */
2250 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
2251 DBG("Enabling kernel event %s for channel %s.",
2252 kevent
->event
->name
, channel_name
);
2253 ret
= kernel_enable_event(kevent
);
2259 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
2261 ret
= LTTCOMM_KERN_LIST_FAIL
;
2265 for (i
= 0; i
< size
; i
++) {
2266 kevent
= trace_kernel_get_event_by_name(event_list
[i
].name
, kchan
);
2267 if (kevent
== NULL
) {
2268 /* Default event type for enable all */
2269 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
2270 /* Enable each single tracepoint event */
2271 ret
= kernel_create_event(&event_list
[i
], kchan
);
2273 /* Ignore error here and continue */
2280 /* Quiescent wait after event enable */
2281 kernel_wait_quiescent(kernel_tracer_fd
);
2284 /* TODO: Userspace tracing */
2285 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2292 case LTTNG_LIST_TRACEPOINTS
:
2294 struct lttng_event
*events
;
2295 ssize_t nb_events
= 0;
2297 switch (cmd_ctx
->lsm
->domain
.type
) {
2298 case LTTNG_DOMAIN_KERNEL
:
2299 DBG("Listing kernel events");
2300 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
2301 if (nb_events
< 0) {
2302 ret
= LTTCOMM_KERN_LIST_FAIL
;
2307 /* TODO: Userspace listing */
2308 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2313 * Setup lttng message with payload size set to the event list size in
2314 * bytes and then copy list into the llm payload.
2316 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
2322 /* Copy event list into message payload */
2323 memcpy(cmd_ctx
->llm
->payload
, events
,
2324 sizeof(struct lttng_event
) * nb_events
);
2331 case LTTNG_START_TRACE
:
2333 struct ltt_kernel_channel
*chan
;
2335 /* Setup lttng message with no payload */
2336 ret
= setup_lttng_msg(cmd_ctx
, 0);
2341 /* Kernel tracing */
2342 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2343 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
2344 DBG("Open kernel metadata");
2345 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
2346 cmd_ctx
->session
->kernel_session
->trace_path
);
2348 ret
= LTTCOMM_KERN_META_FAIL
;
2353 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2354 DBG("Opening kernel metadata stream");
2355 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
2356 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
2358 ERR("Kernel create metadata stream failed");
2359 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2365 /* For each channel */
2366 cds_list_for_each_entry(chan
,
2367 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2368 if (chan
->stream_count
== 0) {
2369 ret
= kernel_open_channel_stream(chan
);
2371 ERR("Kernel create channel stream failed");
2372 ret
= LTTCOMM_KERN_STREAM_FAIL
;
2375 /* Update the stream global counter */
2376 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
2380 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
2382 ret
= LTTCOMM_KERN_START_FAIL
;
2386 DBG("Start kernel tracing");
2387 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
2389 ERR("Kernel start session failed");
2390 ret
= LTTCOMM_KERN_START_FAIL
;
2394 /* Quiescent wait after starting trace */
2395 kernel_wait_quiescent(kernel_tracer_fd
);
2398 /* TODO: Start all UST traces */
2403 case LTTNG_STOP_TRACE
:
2405 struct ltt_kernel_channel
*chan
;
2406 /* Setup lttng message with no payload */
2407 ret
= setup_lttng_msg(cmd_ctx
, 0);
2413 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2414 DBG("Stop kernel tracing");
2416 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
2418 ERR("Kernel metadata flush failed");
2421 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
2422 ret
= kernel_flush_buffer(chan
);
2424 ERR("Kernel flush buffer error");
2428 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
2430 ERR("Kernel stop session failed");
2431 ret
= LTTCOMM_KERN_STOP_FAIL
;
2435 /* Quiescent wait after stopping trace */
2436 kernel_wait_quiescent(kernel_tracer_fd
);
2439 /* TODO : User-space tracer */
2444 case LTTNG_CREATE_SESSION
:
2446 /* Setup lttng message with no payload */
2447 ret
= setup_lttng_msg(cmd_ctx
, 0);
2452 tracepoint(create_session_start
);
2453 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
2454 tracepoint(create_session_end
);
2456 if (ret
== -EEXIST
) {
2457 ret
= LTTCOMM_EXIST_SESS
;
2459 ret
= LTTCOMM_FATAL
;
2467 case LTTNG_DESTROY_SESSION
:
2469 /* Setup lttng message with no payload */
2470 ret
= setup_lttng_msg(cmd_ctx
, 0);
2475 /* Clean kernel session teardown */
2476 teardown_kernel_session(cmd_ctx
->session
);
2478 tracepoint(destroy_session_start
);
2479 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
2480 tracepoint(destroy_session_end
);
2482 ret
= LTTCOMM_FATAL
;
2487 * Must notify the kernel thread here to update it's poll setin order
2488 * to remove the channel(s)' fd just destroyed.
2490 ret
= notify_kernel_channels_update();
2492 ret
= LTTCOMM_FATAL
;
2499 case LTTNG_LIST_DOMAINS
:
2503 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2507 nb_dom
+= cmd_ctx
->session
->ust_session_list
.count
;
2509 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2514 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2515 LTTNG_DOMAIN_KERNEL
;
2517 /* TODO: User-space tracer domain support */
2521 case LTTNG_LIST_CHANNELS
:
2524 * TODO: Only kernel channels are listed here. UST listing
2525 * is needed on lttng-ust 2.0 release.
2528 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2529 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2532 ret
= setup_lttng_msg(cmd_ctx
,
2533 sizeof(struct lttng_channel
) * nb_chan
);
2538 list_lttng_channels(cmd_ctx
->session
,
2539 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2544 case LTTNG_LIST_EVENTS
:
2547 * TODO: Only kernel events are listed here. UST listing
2548 * is needed on lttng-ust 2.0 release.
2550 size_t nb_event
= 0;
2551 struct ltt_kernel_channel
*kchan
= NULL
;
2553 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2554 kchan
= trace_kernel_get_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2555 cmd_ctx
->session
->kernel_session
);
2556 if (kchan
== NULL
) {
2557 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2560 nb_event
+= kchan
->event_count
;
2563 ret
= setup_lttng_msg(cmd_ctx
,
2564 sizeof(struct lttng_event
) * nb_event
);
2569 DBG("Listing events (%zu events)", nb_event
);
2571 list_lttng_events(kchan
,
2572 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2577 case LTTNG_LIST_SESSIONS
:
2579 lock_session_list();
2581 if (session_list_ptr
->count
== 0) {
2582 ret
= LTTCOMM_NO_SESSION
;
2583 unlock_session_list();
2587 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2588 session_list_ptr
->count
);
2590 unlock_session_list();
2594 /* Filled the session array */
2595 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2597 unlock_session_list();
2602 case LTTNG_CALIBRATE
:
2604 /* Setup lttng message with no payload */
2605 ret
= setup_lttng_msg(cmd_ctx
, 0);
2610 switch (cmd_ctx
->lsm
->domain
.type
) {
2611 case LTTNG_DOMAIN_KERNEL
:
2613 struct lttng_kernel_calibrate kcalibrate
;
2615 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2616 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2618 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2624 /* TODO: Userspace tracing */
2625 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2631 case LTTNG_REGISTER_CONSUMER
:
2635 /* Setup lttng message with no payload */
2636 ret
= setup_lttng_msg(cmd_ctx
, 0);
2641 switch (cmd_ctx
->lsm
->domain
.type
) {
2642 case LTTNG_DOMAIN_KERNEL
:
2644 /* Can't register a consumer if there is already one */
2645 if (cmd_ctx
->session
->kernel_session
->consumer_fd
!= 0) {
2646 ret
= LTTCOMM_CONNECT_FAIL
;
2650 sock
= lttcomm_connect_unix_sock(cmd_ctx
->lsm
->u
.reg
.path
);
2652 ret
= LTTCOMM_CONNECT_FAIL
;
2656 cmd_ctx
->session
->kernel_session
->consumer_fd
= sock
;
2660 /* TODO: Userspace tracing */
2661 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2670 /* Undefined command */
2671 ret
= setup_lttng_msg(cmd_ctx
, 0);
2680 /* Set return code */
2681 cmd_ctx
->llm
->ret_code
= ret
;
2683 if (cmd_ctx
->session
) {
2684 unlock_session(cmd_ctx
->session
);
2690 if (cmd_ctx
->llm
== NULL
) {
2691 DBG("Missing llm structure. Allocating one.");
2692 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2696 /* Notify client of error */
2697 cmd_ctx
->llm
->ret_code
= ret
;
2700 if (cmd_ctx
->session
) {
2701 unlock_session(cmd_ctx
->session
);
2707 * This thread manage all clients request using the unix client socket for
2710 static void *thread_manage_clients(void *data
)
2712 int sock
= 0, ret
, i
, pollfd
;
2713 uint32_t revents
, nb_fd
;
2714 struct command_ctx
*cmd_ctx
= NULL
;
2715 struct lttng_poll_event events
;
2717 tracepoint(sessiond_th_cli_start
);
2719 DBG("[thread] Manage client started");
2721 ret
= lttcomm_listen_unix_sock(client_sock
);
2727 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
2728 * more will be added to this poll set.
2730 ret
= create_thread_poll_set(&events
, 2);
2735 /* Add the application registration socket */
2736 ret
= lttng_poll_add(&events
, client_sock
, LPOLLIN
| LPOLLPRI
);
2742 * Notify parent pid that we are ready to accept command for client side.
2744 if (opt_sig_parent
) {
2745 kill(ppid
, SIGCHLD
);
2749 DBG("Accepting client command ...");
2751 tracepoint(sessiond_th_cli_poll
);
2753 nb_fd
= LTTNG_POLL_GETNB(&events
);
2755 /* Inifinite blocking call, waiting for transmission */
2756 ret
= lttng_poll_wait(&events
, -1);
2761 for (i
= 0; i
< nb_fd
; i
++) {
2762 /* Fetch once the poll data */
2763 revents
= LTTNG_POLL_GETEV(&events
, i
);
2764 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
2766 /* Thread quit pipe has been closed. Killing thread. */
2767 ret
= check_thread_quit_pipe(pollfd
, revents
);
2772 /* Event on the registration socket */
2773 if (pollfd
== client_sock
) {
2774 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
2775 ERR("Client socket poll error");
2781 DBG("Wait for client response");
2783 sock
= lttcomm_accept_unix_sock(client_sock
);
2788 /* Allocate context command to process the client request */
2789 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2790 if (cmd_ctx
== NULL
) {
2791 perror("malloc cmd_ctx");
2795 /* Allocate data buffer for reception */
2796 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2797 if (cmd_ctx
->lsm
== NULL
) {
2798 perror("malloc cmd_ctx->lsm");
2802 cmd_ctx
->llm
= NULL
;
2803 cmd_ctx
->session
= NULL
;
2806 * Data is received from the lttng client. The struct
2807 * lttcomm_session_msg (lsm) contains the command and data request of
2810 DBG("Receiving data from client ...");
2811 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
,
2812 sizeof(struct lttcomm_session_msg
));
2814 DBG("Nothing recv() from client... continuing");
2820 // TODO: Validate cmd_ctx including sanity check for
2821 // security purpose.
2824 * This function dispatch the work to the kernel or userspace tracer
2825 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2826 * informations for the client. The command context struct contains
2827 * everything this function may needs.
2829 ret
= process_client_msg(cmd_ctx
);
2832 * TODO: Inform client somehow of the fatal error. At
2833 * this point, ret < 0 means that a malloc failed
2834 * (ENOMEM). Error detected but still accept command.
2836 clean_command_ctx(&cmd_ctx
);
2840 DBG("Sending response (size: %d, retcode: %d)",
2841 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2842 ret
= send_unix_sock(sock
, cmd_ctx
->llm
,
2843 cmd_ctx
->lttng_msg_size
);
2845 ERR("Failed to send data back to client");
2848 clean_command_ctx(&cmd_ctx
);
2850 /* End of transmission */
2855 DBG("Client thread dying");
2856 unlink(client_unix_sock_path
);
2860 lttng_poll_clean(&events
);
2861 clean_command_ctx(&cmd_ctx
);
2867 * usage function on stderr
2869 static void usage(void)
2871 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2872 fprintf(stderr
, " -h, --help Display this usage.\n");
2873 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2874 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2875 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2876 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2877 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2878 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2879 fprintf(stderr
, " -V, --version Show version number.\n");
2880 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2881 fprintf(stderr
, " -q, --quiet No output at all.\n");
2882 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2883 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2887 * daemon argument parsing
2889 static int parse_args(int argc
, char **argv
)
2893 static struct option long_options
[] = {
2894 { "client-sock", 1, 0, 'c' },
2895 { "apps-sock", 1, 0, 'a' },
2896 { "kconsumerd-cmd-sock", 1, 0, 0 },
2897 { "kconsumerd-err-sock", 1, 0, 0 },
2898 { "daemonize", 0, 0, 'd' },
2899 { "sig-parent", 0, 0, 'S' },
2900 { "help", 0, 0, 'h' },
2901 { "group", 1, 0, 'g' },
2902 { "version", 0, 0, 'V' },
2903 { "quiet", 0, 0, 'q' },
2904 { "verbose", 0, 0, 'v' },
2905 { "verbose-kconsumerd", 0, 0, 'Z' },
2910 int option_index
= 0;
2911 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2918 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2920 fprintf(stderr
, " with arg %s\n", optarg
);
2924 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2927 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2933 opt_tracing_group
= strdup(optarg
);
2939 fprintf(stdout
, "%s\n", VERSION
);
2945 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2948 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2954 /* Verbose level can increase using multiple -v */
2958 opt_verbose_kconsumerd
+= 1;
2961 /* Unknown option or other error.
2962 * Error is printed by getopt, just return */
2971 * Creates the two needed socket by the daemon.
2972 * apps_sock - The communication socket for all UST apps.
2973 * client_sock - The communication of the cli tool (lttng).
2975 static int init_daemon_socket(void)
2980 old_umask
= umask(0);
2982 /* Create client tool unix socket */
2983 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2984 if (client_sock
< 0) {
2985 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2990 /* File permission MUST be 660 */
2991 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2993 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2998 /* Create the application unix socket */
2999 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
3000 if (apps_sock
< 0) {
3001 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
3006 /* File permission MUST be 666 */
3007 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
3009 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
3020 * Check if the global socket is available, and if a daemon is answering
3021 * at the other side. If yes, error is returned.
3023 static int check_existing_daemon(void)
3025 if (access(client_unix_sock_path
, F_OK
) < 0 &&
3026 access(apps_unix_sock_path
, F_OK
) < 0) {
3029 /* Is there anybody out there ? */
3030 if (lttng_session_daemon_alive()) {
3038 * Set the tracing group gid onto the client socket.
3040 * Race window between mkdir and chown is OK because we are going from more
3041 * permissive (root.root) to les permissive (root.tracing).
3043 static int set_permissions(void)
3048 gid
= allowed_group();
3051 WARN("No tracing group detected");
3054 ERR("Missing tracing group. Aborting execution.");
3060 /* Set lttng run dir */
3061 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
3063 ERR("Unable to set group on " LTTNG_RUNDIR
);
3067 /* lttng client socket path */
3068 ret
= chown(client_unix_sock_path
, 0, gid
);
3070 ERR("Unable to set group on %s", client_unix_sock_path
);
3074 /* kconsumerd error socket path */
3075 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
3077 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
3081 DBG("All permissions are set");
3088 * Create the pipe used to wake up the kernel thread.
3090 static int create_kernel_poll_pipe(void)
3092 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
3096 * Create the application command pipe to wake thread_manage_apps.
3098 static int create_apps_cmd_pipe(void)
3100 return pipe2(apps_cmd_pipe
, O_CLOEXEC
);
3104 * Create the lttng run directory needed for all global sockets and pipe.
3106 static int create_lttng_rundir(void)
3110 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
3112 if (errno
!= EEXIST
) {
3113 ERR("Unable to create " LTTNG_RUNDIR
);
3125 * Setup sockets and directory needed by the kconsumerd communication with the
3128 static int set_kconsumerd_sockets(void)
3132 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
3133 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
3136 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
3137 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
3140 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
3142 if (errno
!= EEXIST
) {
3143 ERR("Failed to create " KCONSUMERD_PATH
);
3149 /* Create the kconsumerd error unix socket */
3150 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
3151 if (kconsumerd_err_sock
< 0) {
3152 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
3157 /* File permission MUST be 660 */
3158 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
3160 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
3170 * Signal handler for the daemon
3172 * Simply stop all worker threads, leaving main() return gracefully
3173 * after joining all threads and calling cleanup().
3175 static void sighandler(int sig
)
3179 DBG("SIGPIPE catched");
3182 DBG("SIGINT catched");
3186 DBG("SIGTERM catched");
3195 * Setup signal handler for :
3196 * SIGINT, SIGTERM, SIGPIPE
3198 static int set_signal_handler(void)
3201 struct sigaction sa
;
3204 if ((ret
= sigemptyset(&sigset
)) < 0) {
3205 perror("sigemptyset");
3209 sa
.sa_handler
= sighandler
;
3210 sa
.sa_mask
= sigset
;
3212 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
3213 perror("sigaction");
3217 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
3218 perror("sigaction");
3222 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
3223 perror("sigaction");
3227 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
3233 * Set open files limit to unlimited. This daemon can open a large number of
3234 * file descriptors in order to consumer multiple kernel traces.
3236 static void set_ulimit(void)
3241 /* The kernel does not allowed an infinite limit for open files */
3242 lim
.rlim_cur
= 65535;
3243 lim
.rlim_max
= 65535;
3245 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
3247 perror("failed to set open files limit");
3254 int main(int argc
, char **argv
)
3258 const char *home_path
;
3260 tracepoint(sessiond_boot_start
);
3262 /* Create thread quit pipe */
3263 if ((ret
= init_thread_quit_pipe()) < 0) {
3267 /* Parse arguments */
3269 if ((ret
= parse_args(argc
, argv
) < 0)) {
3282 /* Check if daemon is UID = 0 */
3283 is_root
= !getuid();
3286 ret
= create_lttng_rundir();
3291 if (strlen(apps_unix_sock_path
) == 0) {
3292 snprintf(apps_unix_sock_path
, PATH_MAX
,
3293 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
3296 if (strlen(client_unix_sock_path
) == 0) {
3297 snprintf(client_unix_sock_path
, PATH_MAX
,
3298 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
3301 /* Set global SHM for ust */
3302 if (strlen(wait_shm_path
) == 0) {
3303 snprintf(wait_shm_path
, PATH_MAX
,
3304 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
);
3307 home_path
= get_home_dir();
3308 if (home_path
== NULL
) {
3309 /* TODO: Add --socket PATH option */
3310 ERR("Can't get HOME directory for sockets creation.");
3315 if (strlen(apps_unix_sock_path
) == 0) {
3316 snprintf(apps_unix_sock_path
, PATH_MAX
,
3317 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
3320 /* Set the cli tool unix socket path */
3321 if (strlen(client_unix_sock_path
) == 0) {
3322 snprintf(client_unix_sock_path
, PATH_MAX
,
3323 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
3326 /* Set global SHM for ust */
3327 if (strlen(wait_shm_path
) == 0) {
3328 snprintf(wait_shm_path
, PATH_MAX
,
3329 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, geteuid());
3333 DBG("Client socket path %s", client_unix_sock_path
);
3334 DBG("Application socket path %s", apps_unix_sock_path
);
3337 * See if daemon already exist.
3339 if ((ret
= check_existing_daemon()) < 0) {
3340 ERR("Already running daemon.\n");
3342 * We do not goto exit because we must not cleanup()
3343 * because a daemon is already running.
3348 /* After this point, we can safely call cleanup() so goto error is used */
3351 * These actions must be executed as root. We do that *after* setting up
3352 * the sockets path because we MUST make the check for another daemon using
3353 * those paths *before* trying to set the kernel consumer sockets and init
3357 ret
= set_kconsumerd_sockets();
3362 /* Setup kernel tracer */
3363 init_kernel_tracer();
3365 /* Set ulimit for open files */
3369 if ((ret
= set_signal_handler()) < 0) {
3373 /* Setup the needed unix socket */
3374 if ((ret
= init_daemon_socket()) < 0) {
3378 /* Set credentials to socket */
3379 if (is_root
&& ((ret
= set_permissions()) < 0)) {
3383 /* Get parent pid if -S, --sig-parent is specified. */
3384 if (opt_sig_parent
) {
3388 /* Setup the kernel pipe for waking up the kernel thread */
3389 if ((ret
= create_kernel_poll_pipe()) < 0) {
3393 /* Setup the thread apps communication pipe. */
3394 if ((ret
= create_apps_cmd_pipe()) < 0) {
3398 /* Init UST command queue. */
3399 cds_wfq_init(&ust_cmd_queue
.queue
);
3402 * Get session list pointer. This pointer MUST NOT be free().
3403 * This list is statically declared in session.c
3405 session_list_ptr
= get_session_list();
3407 /* Set up max poll set size */
3408 lttng_poll_set_max_size();
3410 /* Create thread to manage the client socket */
3411 ret
= pthread_create(&client_thread
, NULL
,
3412 thread_manage_clients
, (void *) NULL
);
3414 perror("pthread_create clients");
3418 /* Create thread to dispatch registration */
3419 ret
= pthread_create(&dispatch_thread
, NULL
,
3420 thread_dispatch_ust_registration
, (void *) NULL
);
3422 perror("pthread_create dispatch");
3426 /* Create thread to manage application registration. */
3427 ret
= pthread_create(®_apps_thread
, NULL
,
3428 thread_registration_apps
, (void *) NULL
);
3430 perror("pthread_create registration");
3434 /* Create thread to manage application socket */
3435 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
3437 perror("pthread_create apps");
3441 /* Create kernel thread to manage kernel event */
3442 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
3444 perror("pthread_create kernel");
3448 tracepoint(sessiond_boot_end
);
3450 ret
= pthread_join(kernel_thread
, &status
);
3452 perror("pthread_join");
3453 goto error
; /* join error, exit without cleanup */
3457 ret
= pthread_join(apps_thread
, &status
);
3459 perror("pthread_join");
3460 goto error
; /* join error, exit without cleanup */
3464 ret
= pthread_join(reg_apps_thread
, &status
);
3466 perror("pthread_join");
3467 goto error
; /* join error, exit without cleanup */
3471 ret
= pthread_join(dispatch_thread
, &status
);
3473 perror("pthread_join");
3474 goto error
; /* join error, exit without cleanup */
3478 ret
= pthread_join(client_thread
, &status
);
3480 perror("pthread_join");
3481 goto error
; /* join error, exit without cleanup */
3484 ret
= join_kconsumerd_thread();
3486 perror("join_kconsumerd");
3487 goto error
; /* join error, exit without cleanup */
3493 * cleanup() is called when no other thread is running.