2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 - 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.
27 #include <semaphore.h>
33 #include <sys/mount.h>
35 #include <sys/socket.h>
37 #include <sys/types.h>
39 #include <sys/resource.h>
42 #include <urcu/list.h> /* URCU list library (-lurcu) */
43 #include <lttng/lttng.h>
46 #include "liblttsessiondcomm.h"
47 #include "ltt-sessiond.h"
49 #include "kernel-ctl.h"
52 #include "traceable-app.h"
53 #include "lttng-kconsumerd.h"
57 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
58 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
59 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
60 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
63 int opt_verbose
; /* Not static for lttngerr.h */
64 int opt_verbose_kconsumerd
; /* Not static for lttngerr.h */
65 int opt_quiet
; /* Not static for lttngerr.h */
68 const char *opt_tracing_group
;
69 static int opt_sig_parent
;
70 static int opt_daemon
;
71 static int is_root
; /* Set to 1 if the daemon is running as root */
72 static pid_t ppid
; /* Parent PID for --sig-parent option */
73 static pid_t kconsumerd_pid
;
74 static struct pollfd
*kernel_pollfd
;
76 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
77 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
78 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
79 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
82 static int client_sock
;
84 static int kconsumerd_err_sock
;
85 static int kconsumerd_cmd_sock
;
86 static int kernel_tracer_fd
;
87 static int kernel_poll_pipe
[2];
90 * Quit pipe for all threads. This permits a single cancellation point
91 * for all threads when receiving an event on the pipe.
93 static int thread_quit_pipe
[2];
95 /* Pthread, Mutexes and Semaphores */
96 static pthread_t kconsumerd_thread
;
97 static pthread_t apps_thread
;
98 static pthread_t client_thread
;
99 static pthread_t kernel_thread
;
100 static sem_t kconsumerd_sem
;
102 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
104 static int modprobe_remove_kernel_modules(void);
107 * Pointer initialized before thread creation.
109 * This points to the tracing session list containing the session count and a
110 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
111 * MUST NOT be taken if you call a public function in session.c.
113 * The lock is nested inside the structure: session_list_ptr->lock. Please use
114 * lock_session_list and unlock_session_list for lock acquisition.
116 static struct ltt_session_list
*session_list_ptr
;
118 static gid_t
allowed_group(void)
122 if (opt_tracing_group
) {
123 grp
= getgrnam(opt_tracing_group
);
125 grp
= getgrnam(default_tracing_group
);
137 * Return -1 on error or 0 if all pipes are created.
139 static int init_thread_quit_pipe(void)
143 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
145 perror("thread quit pipe");
154 * Complete teardown of a kernel session. This free all data structure related
155 * to a kernel session and update counter.
157 static void teardown_kernel_session(struct ltt_session
*session
)
159 if (session
->kernel_session
!= NULL
) {
160 DBG("Tearing down kernel session");
161 trace_destroy_kernel_session(session
->kernel_session
);
162 /* Extra precaution */
163 session
->kernel_session
= NULL
;
167 static void stop_threads(void)
169 /* Stopping all threads */
170 DBG("Terminating all threads");
171 close(thread_quit_pipe
[0]);
172 close(thread_quit_pipe
[1]);
178 static void cleanup(void)
182 struct ltt_session
*sess
, *stmp
;
187 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
188 "Matthew, BEET driven development works!%c[%dm",
189 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
192 DBG("Removing %s directory", LTTNG_RUNDIR
);
193 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
195 ERR("asprintf failed. Something is really wrong!");
198 /* Remove lttng run directory */
201 ERR("Unable to clean " LTTNG_RUNDIR
);
204 DBG("Cleaning up all session");
206 /* Destroy session list mutex */
207 if (session_list_ptr
!= NULL
) {
208 pthread_mutex_destroy(&session_list_ptr
->lock
);
210 /* Cleanup ALL session */
211 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
212 teardown_kernel_session(sess
);
213 // TODO complete session cleanup (including UST)
217 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
219 DBG("Closing kernel fd");
220 close(kernel_tracer_fd
);
222 DBG("Unloading kernel modules");
223 modprobe_remove_kernel_modules();
227 * Send data on a unix socket using the liblttsessiondcomm API.
229 * Return lttcomm error code.
231 static int send_unix_sock(int sock
, void *buf
, size_t len
)
233 /* Check valid length */
238 return lttcomm_send_unix_sock(sock
, buf
, len
);
242 * Free memory of a command context structure.
244 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
246 DBG("Clean command context structure");
248 if ((*cmd_ctx
)->llm
) {
249 free((*cmd_ctx
)->llm
);
251 if ((*cmd_ctx
)->lsm
) {
252 free((*cmd_ctx
)->lsm
);
260 * Send all stream fds of kernel channel to the consumer.
262 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
266 struct ltt_kernel_stream
*stream
;
267 struct lttcomm_kconsumerd_header lkh
;
268 struct lttcomm_kconsumerd_msg lkm
;
270 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
272 nb_fd
= channel
->stream_count
;
275 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
276 lkh
.cmd_type
= ADD_STREAM
;
278 DBG("Sending kconsumerd header");
280 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
282 perror("send kconsumerd header");
286 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
287 if (stream
->fd
!= 0) {
289 lkm
.state
= stream
->state
;
290 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
291 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
293 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
295 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
297 perror("send kconsumerd fd");
303 DBG("Kconsumerd channel fds sent");
312 * Send all stream fds of the kernel session to the consumer.
314 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
317 struct ltt_kernel_channel
*chan
;
318 struct lttcomm_kconsumerd_header lkh
;
319 struct lttcomm_kconsumerd_msg lkm
;
322 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
323 lkh
.cmd_type
= ADD_STREAM
;
325 DBG("Sending kconsumerd header for metadata");
327 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
329 perror("send kconsumerd header");
333 DBG("Sending metadata stream fd");
335 if (session
->metadata_stream_fd
!= 0) {
336 /* Send metadata stream fd first */
337 lkm
.fd
= session
->metadata_stream_fd
;
338 lkm
.state
= ACTIVE_FD
;
339 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
340 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
342 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
344 perror("send kconsumerd fd");
349 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
350 ret
= send_kconsumerd_channel_fds(sock
, chan
);
356 DBG("Kconsumerd fds (metadata and channel streams) sent");
366 * Return a socket connected to the libust communication socket of the
367 * application identified by the pid.
369 * If the pid is not found in the traceable list, return -1 to indicate error.
371 static int ust_connect_app(pid_t pid
)
374 struct ltt_traceable_app
*lta
;
376 DBG("Connect to application pid %d", pid
);
378 lta
= find_app_by_pid(pid
);
381 DBG("Application pid %d not found", pid
);
385 sock
= ustctl_connect_pid(lta
->pid
);
387 ERR("Fail connecting to the PID %d", pid
);
392 #endif /* DISABLED */
395 * Notify apps by writing 42 to a named pipe using name. Every applications
396 * waiting for a ltt-sessiond will be notified and re-register automatically to
397 * the session daemon.
399 * Return open or write error value.
401 static int notify_apps(const char *name
)
406 DBG("Notify the global application pipe");
408 /* Try opening the global pipe */
409 fd
= open(name
, O_WRONLY
);
414 /* Notify by writing on the pipe */
415 ret
= write(fd
, "42", 2);
425 * Setup the outgoing data buffer for the response (llm) by allocating the
426 * right amount of memory and copying the original information from the lsm
429 * Return total size of the buffer pointed by buf.
431 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
437 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
438 if (cmd_ctx
->llm
== NULL
) {
444 /* Copy common data */
445 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
446 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
448 cmd_ctx
->llm
->data_size
= size
;
449 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
458 * Update the kernel pollfd set of all channel fd available over all tracing
459 * session. Add the wakeup pipe at the end of the set.
461 static int update_kernel_pollfd(void)
465 * The wakup pipe and the quit pipe are needed so the number of fds starts
466 * at 2 for those pipes.
468 unsigned int nb_fd
= 2;
469 struct ltt_session
*session
;
470 struct ltt_kernel_channel
*channel
;
472 DBG("Updating kernel_pollfd");
474 /* Get the number of channel of all kernel session */
476 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
477 lock_session(session
);
478 if (session
->kernel_session
== NULL
) {
479 unlock_session(session
);
482 nb_fd
+= session
->kernel_session
->channel_count
;
483 unlock_session(session
);
486 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
488 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
489 if (kernel_pollfd
== NULL
) {
490 perror("malloc kernel_pollfd");
494 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
495 lock_session(session
);
496 if (session
->kernel_session
== NULL
) {
497 unlock_session(session
);
501 ERR("To much channel for kernel_pollfd size");
502 unlock_session(session
);
505 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
506 kernel_pollfd
[i
].fd
= channel
->fd
;
507 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
510 unlock_session(session
);
512 unlock_session_list();
514 /* Adding wake up pipe */
515 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
516 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
518 /* Adding the quit pipe */
519 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
524 unlock_session_list();
529 * Find the channel fd from 'fd' over all tracing session. When found, check
530 * for new channel stream and send those stream fds to the kernel consumer.
532 * Useful for CPU hotplug feature.
534 static int update_kernel_stream(int fd
)
537 struct ltt_session
*session
;
538 struct ltt_kernel_channel
*channel
;
540 DBG("Updating kernel streams for channel fd %d", fd
);
543 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
544 lock_session(session
);
545 if (session
->kernel_session
== NULL
) {
546 unlock_session(session
);
549 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
550 if (channel
->fd
== fd
) {
551 DBG("Channel found, updating kernel streams");
552 ret
= kernel_open_channel_stream(channel
);
557 * Have we already sent fds to the consumer? If yes, it means that
558 * tracing is started so it is safe to send our updated stream fds.
560 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
561 ret
= send_kconsumerd_channel_fds(kconsumerd_cmd_sock
, channel
);
569 unlock_session(session
);
573 unlock_session_list();
575 unlock_session(session
);
581 * This thread manage event coming from the kernel.
583 * Features supported in this thread:
586 static void *thread_manage_kernel(void *data
)
588 int ret
, i
, nb_fd
= 0;
590 int update_poll_flag
= 1;
592 DBG("Thread manage kernel started");
595 if (update_poll_flag
== 1) {
596 nb_fd
= update_kernel_pollfd();
600 update_poll_flag
= 0;
603 DBG("Polling on %d fds", nb_fd
);
605 /* Poll infinite value of time */
606 ret
= poll(kernel_pollfd
, nb_fd
, -1);
608 perror("poll kernel thread");
610 } else if (ret
== 0) {
611 /* Should not happen since timeout is infinite */
615 /* Thread quit pipe has been closed. Killing thread. */
616 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
620 DBG("Kernel poll event triggered");
623 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
626 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
628 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
629 update_poll_flag
= 1;
637 for (i
= 0; i
< nb_fd
; i
++) {
638 switch (kernel_pollfd
[i
].revents
) {
640 * New CPU detected by the kernel. Adding kernel stream to kernel
641 * session and updating the kernel consumer
643 case POLLIN
| POLLRDNORM
:
644 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
654 DBG("Kernel thread dying");
659 close(kernel_poll_pipe
[0]);
660 close(kernel_poll_pipe
[1]);
665 * This thread manage the kconsumerd error sent back to the session daemon.
667 static void *thread_manage_kconsumerd(void *data
)
670 enum lttcomm_return_code code
;
671 struct pollfd pollfd
[2];
673 DBG("[thread] Manage kconsumerd started");
675 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
680 /* First fd is always the quit pipe */
681 pollfd
[0].fd
= thread_quit_pipe
[0];
684 pollfd
[1].fd
= kconsumerd_err_sock
;
685 pollfd
[1].events
= POLLIN
;
687 /* Inifinite blocking call, waiting for transmission */
688 ret
= poll(pollfd
, 2, -1);
690 perror("poll kconsumerd thread");
694 /* Thread quit pipe has been closed. Killing thread. */
695 if (pollfd
[0].revents
== POLLNVAL
) {
697 } else if (pollfd
[1].revents
== POLLERR
) {
698 ERR("Kconsumerd err socket poll error");
702 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
707 /* Getting status code from kconsumerd */
708 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
713 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
714 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
715 if (kconsumerd_cmd_sock
< 0) {
716 sem_post(&kconsumerd_sem
);
717 perror("kconsumerd connect");
720 /* Signal condition to tell that the kconsumerd is ready */
721 sem_post(&kconsumerd_sem
);
722 DBG("Kconsumerd command socket ready");
724 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
725 lttcomm_get_readable_code(-code
));
729 /* Wait for any kconsumerd error */
730 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
732 ERR("Kconsumerd closed the command socket");
736 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
739 DBG("Kconsumerd thread dying");
740 if (kconsumerd_err_sock
) {
741 close(kconsumerd_err_sock
);
743 if (kconsumerd_cmd_sock
) {
744 close(kconsumerd_cmd_sock
);
750 unlink(kconsumerd_err_unix_sock_path
);
751 unlink(kconsumerd_cmd_unix_sock_path
);
758 * This thread manage the application socket communication
760 static void *thread_manage_apps(void *data
)
763 struct pollfd pollfd
[2];
765 /* TODO: Something more elegant is needed but fine for now */
766 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
767 * for 32-bit vs 64-bit compat processes. */
768 /* replicate in ust with version number */
770 int reg
; /* 1:register, 0:unregister */
775 DBG("[thread] Manage apps started");
777 ret
= lttcomm_listen_unix_sock(apps_sock
);
782 /* First fd is always the quit pipe */
783 pollfd
[0].fd
= thread_quit_pipe
[0];
786 pollfd
[1].fd
= apps_sock
;
787 pollfd
[1].events
= POLLIN
;
789 /* Notify all applications to register */
790 notify_apps(default_global_apps_pipe
);
793 DBG("Accepting application registration");
795 /* Inifinite blocking call, waiting for transmission */
796 ret
= poll(pollfd
, 2, -1);
798 perror("poll apps thread");
802 /* Thread quit pipe has been closed. Killing thread. */
803 if (pollfd
[0].revents
== POLLNVAL
) {
805 } else if (pollfd
[1].revents
== POLLERR
) {
806 ERR("Apps socket poll error");
810 sock
= lttcomm_accept_unix_sock(apps_sock
);
816 * Basic recv here to handle the very simple data
817 * that the libust send to register (reg_msg).
819 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
825 /* Add application to the global traceable list */
826 if (reg_msg
.reg
== 1) {
828 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
830 /* register_traceable_app only return an error with
831 * ENOMEM. At this point, we better stop everything.
837 unregister_traceable_app(reg_msg
.pid
);
842 DBG("Apps thread dying");
850 unlink(apps_unix_sock_path
);
855 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
856 * exec or it will fails.
858 static int spawn_kconsumerd_thread(void)
862 /* Setup semaphore */
863 sem_init(&kconsumerd_sem
, 0, 0);
865 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
867 perror("pthread_create kconsumerd");
871 /* Wait for the kconsumerd thread to be ready */
872 sem_wait(&kconsumerd_sem
);
874 if (kconsumerd_pid
== 0) {
875 ERR("Kconsumerd did not start");
882 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
886 static int join_kconsumerd_thread(void)
891 if (kconsumerd_pid
!= 0) {
892 ret
= kill(kconsumerd_pid
, SIGTERM
);
894 ERR("Error killing kconsumerd");
897 return pthread_join(kconsumerd_thread
, &status
);
904 * Fork and exec a kernel consumer daemon (kconsumerd).
906 * Return pid if successful else -1.
908 static pid_t
spawn_kconsumerd(void)
912 const char *verbosity
;
914 DBG("Spawning kconsumerd");
921 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
922 verbosity
= "--verbose";
924 verbosity
= "--quiet";
926 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
928 perror("kernel start consumer exec");
931 } else if (pid
> 0) {
935 perror("kernel start consumer fork");
945 * Spawn the kconsumerd daemon and session daemon thread.
947 static int start_kconsumerd(void)
951 pthread_mutex_lock(&kconsumerd_pid_mutex
);
952 if (kconsumerd_pid
!= 0) {
953 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
957 ret
= spawn_kconsumerd();
959 ERR("Spawning kconsumerd failed");
960 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
961 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
965 /* Setting up the global kconsumerd_pid */
966 kconsumerd_pid
= ret
;
967 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
969 DBG("Kconsumerd pid %d", ret
);
971 DBG("Spawning kconsumerd thread");
972 ret
= spawn_kconsumerd_thread();
974 ERR("Fatal error spawning kconsumerd thread");
986 * modprobe_kernel_modules
988 static int modprobe_kernel_modules(void)
993 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
994 ret
= snprintf(modprobe
, sizeof(modprobe
),
995 "/sbin/modprobe %s%s",
996 kernel_modules_list
[i
].required
? "" : "--quiet ",
997 kernel_modules_list
[i
].name
);
999 perror("snprintf modprobe");
1002 modprobe
[sizeof(modprobe
) - 1] = '\0';
1003 ret
= system(modprobe
);
1005 ERR("Unable to launch modprobe for module %s",
1006 kernel_modules_list
[i
].name
);
1007 } else if (kernel_modules_list
[i
].required
1008 && WEXITSTATUS(ret
) != 0) {
1009 ERR("Unable to load module %s",
1010 kernel_modules_list
[i
].name
);
1012 DBG("Modprobe successfully %s",
1013 kernel_modules_list
[i
].name
);
1022 * modprobe_remove_kernel_modules
1023 * Remove modules in reverse load order.
1025 static int modprobe_remove_kernel_modules(void)
1030 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
1031 ret
= snprintf(modprobe
, sizeof(modprobe
),
1032 "/sbin/modprobe --remove --quiet %s",
1033 kernel_modules_list
[i
].name
);
1035 perror("snprintf modprobe --remove");
1038 modprobe
[sizeof(modprobe
) - 1] = '\0';
1039 ret
= system(modprobe
);
1041 ERR("Unable to launch modprobe --remove for module %s",
1042 kernel_modules_list
[i
].name
);
1043 } else if (kernel_modules_list
[i
].required
1044 && WEXITSTATUS(ret
) != 0) {
1045 ERR("Unable to remove module %s",
1046 kernel_modules_list
[i
].name
);
1048 DBG("Modprobe removal successful %s",
1049 kernel_modules_list
[i
].name
);
1060 static int mount_debugfs(char *path
)
1063 char *type
= "debugfs";
1065 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1070 ret
= mount(type
, path
, type
, 0, NULL
);
1072 perror("mount debugfs");
1076 DBG("Mounted debugfs successfully at %s", path
);
1083 * Setup necessary data for kernel tracer action.
1085 static void init_kernel_tracer(void)
1088 char *proc_mounts
= "/proc/mounts";
1090 char *debugfs_path
= NULL
, *lttng_path
;
1093 /* Detect debugfs */
1094 fp
= fopen(proc_mounts
, "r");
1096 ERR("Unable to probe %s", proc_mounts
);
1100 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1101 if (strstr(line
, "debugfs") != NULL
) {
1102 /* Remove first string */
1104 /* Dup string here so we can reuse line later on */
1105 debugfs_path
= strdup(strtok(NULL
, " "));
1106 DBG("Got debugfs path : %s", debugfs_path
);
1113 /* Mount debugfs if needded */
1114 if (debugfs_path
== NULL
) {
1115 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1117 perror("asprintf debugfs path");
1120 ret
= mount_debugfs(debugfs_path
);
1126 /* Modprobe lttng kernel modules */
1127 ret
= modprobe_kernel_modules();
1132 /* Setup lttng kernel path */
1133 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1135 perror("asprintf lttng path");
1139 /* Open debugfs lttng */
1140 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1141 if (kernel_tracer_fd
< 0) {
1142 DBG("Failed to open %s", lttng_path
);
1148 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1158 WARN("No kernel tracer available");
1159 kernel_tracer_fd
= 0;
1164 * Start tracing by creating trace directory and sending FDs to the kernel
1167 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1171 if (session
->kconsumer_fds_sent
== 0) {
1172 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
1174 ERR("Send kconsumerd fds failed");
1175 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1179 session
->kconsumer_fds_sent
= 1;
1187 * Notify kernel thread to update it's pollfd.
1189 static int notify_kernel_pollfd(void)
1193 /* Inform kernel thread of the new kernel channel */
1194 ret
= write(kernel_poll_pipe
[1], "!", 1);
1196 perror("write kernel poll pipe");
1203 * Allocate a channel structure and fill it.
1205 static struct lttng_channel
*init_default_channel(char *name
)
1207 struct lttng_channel
*chan
;
1209 chan
= malloc(sizeof(struct lttng_channel
));
1211 perror("init channel malloc");
1215 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1216 perror("snprintf channel name");
1220 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1221 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
1222 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
1223 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1224 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1225 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1232 * Create a kernel tracer session then create the default channel.
1234 static int create_kernel_session(struct ltt_session
*session
)
1238 DBG("Creating kernel session");
1240 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1242 ret
= LTTCOMM_KERN_SESS_FAIL
;
1246 ret
= mkdir_recursive(session
->path
, S_IRWXU
| S_IRWXG
,
1247 geteuid(), allowed_group());
1249 if (ret
!= -EEXIST
) {
1250 ERR("Trace directory creation error");
1260 * Using the session list, filled a lttng_session array to send back to the
1261 * client for session listing.
1263 * The session list lock MUST be acquired before calling this function. Use
1264 * lock_session_list() and unlock_session_list().
1266 static void list_lttng_sessions(struct lttng_session
*sessions
)
1269 struct ltt_session
*session
;
1271 DBG("Getting all available session");
1273 * Iterate over session list and append data after the control struct in
1276 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1277 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1278 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1284 * Fill lttng_channel array of all channels.
1286 static void list_lttng_channels(struct ltt_session
*session
,
1287 struct lttng_channel
*channels
)
1290 struct ltt_kernel_channel
*kchan
;
1292 DBG("Listing channels for session %s", session
->name
);
1294 /* Kernel channels */
1295 if (session
->kernel_session
!= NULL
) {
1296 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1297 /* Copy lttng_channel struct to array */
1298 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1299 channels
[i
].enabled
= kchan
->enabled
;
1304 /* TODO: Missing UST listing */
1308 * Fill lttng_event array of all events in the channel.
1310 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1311 struct lttng_event
*events
)
1314 * TODO: This is ONLY kernel. Need UST support.
1317 struct ltt_kernel_event
*event
;
1319 DBG("Listing events for channel %s", kchan
->channel
->name
);
1321 /* Kernel channels */
1322 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1323 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1324 events
[i
].enabled
= event
->enabled
;
1325 switch (event
->event
->instrumentation
) {
1326 case LTTNG_KERNEL_TRACEPOINT
:
1327 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1329 case LTTNG_KERNEL_KPROBE
:
1330 case LTTNG_KERNEL_KRETPROBE
:
1331 events
[i
].type
= LTTNG_EVENT_PROBE
;
1332 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1333 sizeof(struct lttng_kernel_kprobe
));
1335 case LTTNG_KERNEL_FUNCTION
:
1336 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1337 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1338 sizeof(struct lttng_kernel_function
));
1346 * Process the command requested by the lttng client within the command
1347 * context structure. This function make sure that the return structure (llm)
1348 * is set and ready for transmission before returning.
1350 * Return any error encountered or 0 for success.
1352 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1354 int ret
= LTTCOMM_OK
;
1356 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1358 /* Listing commands don't need a session */
1359 switch (cmd_ctx
->lsm
->cmd_type
) {
1360 case LTTNG_CREATE_SESSION
:
1361 case LTTNG_LIST_SESSIONS
:
1362 case LTTNG_LIST_TRACEPOINTS
:
1363 case LTTNG_CALIBRATE
:
1366 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1367 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1368 if (cmd_ctx
->session
== NULL
) {
1369 /* If session name not found */
1370 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1371 ret
= LTTCOMM_SESS_NOT_FOUND
;
1372 } else { /* If no session name specified */
1373 ret
= LTTCOMM_SELECT_SESS
;
1377 /* Acquire lock for the session */
1378 lock_session(cmd_ctx
->session
);
1384 * Check domain type for specific "pre-action".
1386 switch (cmd_ctx
->lsm
->domain
.type
) {
1387 case LTTNG_DOMAIN_KERNEL
:
1388 /* Kernel tracer check */
1389 if (kernel_tracer_fd
== 0) {
1390 init_kernel_tracer();
1391 if (kernel_tracer_fd
== 0) {
1392 ret
= LTTCOMM_KERN_NA
;
1397 /* Need a session for kernel command */
1398 switch (cmd_ctx
->lsm
->cmd_type
) {
1399 case LTTNG_CREATE_SESSION
:
1400 case LTTNG_LIST_SESSIONS
:
1401 case LTTNG_LIST_TRACEPOINTS
:
1402 case LTTNG_CALIBRATE
:
1405 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1406 ret
= create_kernel_session(cmd_ctx
->session
);
1408 ret
= LTTCOMM_KERN_SESS_FAIL
;
1412 /* Start the kernel consumer daemon */
1413 if (kconsumerd_pid
== 0) {
1414 ret
= start_kconsumerd();
1426 /* Process by command type */
1427 switch (cmd_ctx
->lsm
->cmd_type
) {
1428 case LTTNG_ADD_CONTEXT
:
1430 struct lttng_kernel_context kctx
;
1432 /* Setup lttng message with no payload */
1433 ret
= setup_lttng_msg(cmd_ctx
, 0);
1438 switch (cmd_ctx
->lsm
->domain
.type
) {
1439 case LTTNG_DOMAIN_KERNEL
:
1440 /* Create Kernel context */
1441 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1442 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1443 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1444 strncpy(kctx
.u
.perf_counter
.name
,
1445 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1446 LTTNG_SYMBOL_NAME_LEN
);
1448 /* Add kernel context to kernel tracer. See context.c */
1449 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1450 cmd_ctx
->lsm
->u
.context
.event_name
,
1451 cmd_ctx
->lsm
->u
.context
.channel_name
);
1452 if (ret
!= LTTCOMM_OK
) {
1457 /* TODO: Userspace tracing */
1458 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1465 case LTTNG_DISABLE_CHANNEL
:
1467 struct ltt_kernel_channel
*kchan
;
1469 /* Setup lttng message with no payload */
1470 ret
= setup_lttng_msg(cmd_ctx
, 0);
1475 switch (cmd_ctx
->lsm
->domain
.type
) {
1476 case LTTNG_DOMAIN_KERNEL
:
1477 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1478 cmd_ctx
->session
->kernel_session
);
1479 if (kchan
== NULL
) {
1480 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1482 } else if (kchan
->enabled
== 1) {
1483 ret
= kernel_disable_channel(kchan
);
1485 if (ret
!= EEXIST
) {
1486 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1491 kernel_wait_quiescent(kernel_tracer_fd
);
1494 /* TODO: Userspace tracing */
1495 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1502 case LTTNG_DISABLE_EVENT
:
1504 struct ltt_kernel_channel
*kchan
;
1505 struct ltt_kernel_event
*kevent
;
1507 /* Setup lttng message with no payload */
1508 ret
= setup_lttng_msg(cmd_ctx
, 0);
1513 switch (cmd_ctx
->lsm
->domain
.type
) {
1514 case LTTNG_DOMAIN_KERNEL
:
1515 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1516 cmd_ctx
->session
->kernel_session
);
1517 if (kchan
== NULL
) {
1518 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1522 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1523 if (kevent
!= NULL
) {
1524 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1525 kchan
->channel
->name
);
1526 ret
= kernel_disable_event(kevent
);
1528 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1533 kernel_wait_quiescent(kernel_tracer_fd
);
1536 /* TODO: Userspace tracing */
1537 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1544 case LTTNG_DISABLE_ALL_EVENT
:
1546 struct ltt_kernel_channel
*kchan
;
1547 struct ltt_kernel_event
*kevent
;
1549 /* Setup lttng message with no payload */
1550 ret
= setup_lttng_msg(cmd_ctx
, 0);
1555 switch (cmd_ctx
->lsm
->domain
.type
) {
1556 case LTTNG_DOMAIN_KERNEL
:
1557 DBG("Disabling all enabled kernel events");
1558 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1559 cmd_ctx
->session
->kernel_session
);
1560 if (kchan
== NULL
) {
1561 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1565 /* For each event in the kernel session */
1566 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1567 DBG("Disabling kernel event %s for channel %s.",
1568 kevent
->event
->name
, kchan
->channel
->name
);
1569 ret
= kernel_disable_event(kevent
);
1575 /* Quiescent wait after event disable */
1576 kernel_wait_quiescent(kernel_tracer_fd
);
1579 /* TODO: Userspace tracing */
1580 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1587 case LTTNG_ENABLE_CHANNEL
:
1589 struct ltt_kernel_channel
*kchan
;
1591 /* Setup lttng message with no payload */
1592 ret
= setup_lttng_msg(cmd_ctx
, 0);
1597 switch (cmd_ctx
->lsm
->domain
.type
) {
1598 case LTTNG_DOMAIN_KERNEL
:
1599 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1600 cmd_ctx
->session
->kernel_session
);
1601 if (kchan
== NULL
) {
1602 /* Channel not found, creating it */
1603 DBG("Creating kernel channel");
1605 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1606 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1608 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1612 /* Notify kernel thread that there is a new channel */
1613 ret
= notify_kernel_pollfd();
1615 ret
= LTTCOMM_FATAL
;
1618 } else if (kchan
->enabled
== 0) {
1619 ret
= kernel_enable_channel(kchan
);
1621 if (ret
!= EEXIST
) {
1622 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1628 kernel_wait_quiescent(kernel_tracer_fd
);
1631 /* TODO: Userspace tracing */
1632 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1639 case LTTNG_ENABLE_EVENT
:
1642 struct ltt_kernel_channel
*kchan
;
1643 struct ltt_kernel_event
*kevent
;
1644 struct lttng_channel
*chan
;
1646 /* Setup lttng message with no payload */
1647 ret
= setup_lttng_msg(cmd_ctx
, 0);
1652 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1654 switch (cmd_ctx
->lsm
->domain
.type
) {
1655 case LTTNG_DOMAIN_KERNEL
:
1657 kchan
= get_kernel_channel_by_name(channel_name
,
1658 cmd_ctx
->session
->kernel_session
);
1659 if (kchan
== NULL
) {
1660 DBG("Channel not found. Creating channel %s", channel_name
);
1662 chan
= init_default_channel(channel_name
);
1664 ret
= LTTCOMM_FATAL
;
1668 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1669 chan
, cmd_ctx
->session
->path
);
1671 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1675 } while (kchan
== NULL
);
1677 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
1678 if (kevent
== NULL
) {
1679 DBG("Creating kernel event %s for channel %s.",
1680 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
1681 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
1683 DBG("Enabling kernel event %s for channel %s.",
1684 kevent
->event
->name
, channel_name
);
1685 ret
= kernel_enable_event(kevent
);
1686 if (ret
== -EEXIST
) {
1687 ret
= LTTCOMM_KERN_EVENT_EXIST
;
1693 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1697 kernel_wait_quiescent(kernel_tracer_fd
);
1700 /* TODO: Userspace tracing */
1701 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1707 case LTTNG_ENABLE_ALL_EVENT
:
1711 struct ltt_kernel_channel
*kchan
;
1712 struct ltt_kernel_event
*kevent
;
1713 struct lttng_event
*event_list
;
1714 struct lttng_channel
*chan
;
1716 /* Setup lttng message with no payload */
1717 ret
= setup_lttng_msg(cmd_ctx
, 0);
1722 DBG("Enabling all kernel event");
1724 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1726 switch (cmd_ctx
->lsm
->domain
.type
) {
1727 case LTTNG_DOMAIN_KERNEL
:
1729 kchan
= get_kernel_channel_by_name(channel_name
,
1730 cmd_ctx
->session
->kernel_session
);
1731 if (kchan
== NULL
) {
1732 DBG("Channel not found. Creating channel %s", channel_name
);
1734 chan
= init_default_channel(channel_name
);
1736 ret
= LTTCOMM_FATAL
;
1740 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1741 chan
, cmd_ctx
->session
->path
);
1743 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1747 } while (kchan
== NULL
);
1749 /* For each event in the kernel session */
1750 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1751 DBG("Enabling kernel event %s for channel %s.",
1752 kevent
->event
->name
, channel_name
);
1753 ret
= kernel_enable_event(kevent
);
1759 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1761 ret
= LTTCOMM_KERN_LIST_FAIL
;
1765 for (i
= 0; i
< size
; i
++) {
1766 kevent
= get_kernel_event_by_name(event_list
[i
].name
, kchan
);
1767 if (kevent
== NULL
) {
1768 /* Default event type for enable all */
1769 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1770 /* Enable each single tracepoint event */
1771 ret
= kernel_create_event(&event_list
[i
], kchan
);
1773 /* Ignore error here and continue */
1780 /* Quiescent wait after event enable */
1781 kernel_wait_quiescent(kernel_tracer_fd
);
1784 /* TODO: Userspace tracing */
1785 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1792 case LTTNG_LIST_TRACEPOINTS
:
1794 struct lttng_event
*events
;
1795 ssize_t nb_events
= 0;
1797 switch (cmd_ctx
->lsm
->domain
.type
) {
1798 case LTTNG_DOMAIN_KERNEL
:
1799 DBG("Listing kernel events");
1800 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
1801 if (nb_events
< 0) {
1802 ret
= LTTCOMM_KERN_LIST_FAIL
;
1807 /* TODO: Userspace listing */
1808 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1813 * Setup lttng message with payload size set to the event list size in
1814 * bytes and then copy list into the llm payload.
1816 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
1822 /* Copy event list into message payload */
1823 memcpy(cmd_ctx
->llm
->payload
, events
,
1824 sizeof(struct lttng_event
) * nb_events
);
1831 case LTTNG_START_TRACE
:
1833 struct ltt_kernel_channel
*chan
;
1835 /* Setup lttng message with no payload */
1836 ret
= setup_lttng_msg(cmd_ctx
, 0);
1841 /* Kernel tracing */
1842 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1843 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1844 DBG("Open kernel metadata");
1845 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1846 cmd_ctx
->session
->path
);
1848 ret
= LTTCOMM_KERN_META_FAIL
;
1853 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1854 DBG("Opening kernel metadata stream");
1855 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1856 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1858 ERR("Kernel create metadata stream failed");
1859 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1865 /* For each channel */
1866 cds_list_for_each_entry(chan
,
1867 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1868 if (chan
->stream_count
== 0) {
1869 ret
= kernel_open_channel_stream(chan
);
1871 ERR("Kernel create channel stream failed");
1872 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1875 /* Update the stream global counter */
1876 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1880 DBG("Start kernel tracing");
1881 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1883 ERR("Kernel start session failed");
1884 ret
= LTTCOMM_KERN_START_FAIL
;
1888 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1890 ret
= LTTCOMM_KERN_START_FAIL
;
1894 /* Quiescent wait after starting trace */
1895 kernel_wait_quiescent(kernel_tracer_fd
);
1898 /* TODO: Start all UST traces */
1903 case LTTNG_STOP_TRACE
:
1905 struct ltt_kernel_channel
*chan
;
1906 /* Setup lttng message with no payload */
1907 ret
= setup_lttng_msg(cmd_ctx
, 0);
1913 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1914 DBG("Stop kernel tracing");
1916 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1918 ERR("Kernel metadata flush failed");
1921 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1922 ret
= kernel_flush_buffer(chan
);
1924 ERR("Kernel flush buffer error");
1928 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1930 ERR("Kernel stop session failed");
1931 ret
= LTTCOMM_KERN_STOP_FAIL
;
1935 /* Quiescent wait after stopping trace */
1936 kernel_wait_quiescent(kernel_tracer_fd
);
1939 /* TODO : User-space tracer */
1944 case LTTNG_CREATE_SESSION
:
1946 /* Setup lttng message with no payload */
1947 ret
= setup_lttng_msg(cmd_ctx
, 0);
1952 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
1954 if (ret
== -EEXIST
) {
1955 ret
= LTTCOMM_EXIST_SESS
;
1957 ret
= LTTCOMM_FATAL
;
1965 case LTTNG_DESTROY_SESSION
:
1967 /* Setup lttng message with no payload */
1968 ret
= setup_lttng_msg(cmd_ctx
, 0);
1973 /* Clean kernel session teardown */
1974 teardown_kernel_session(cmd_ctx
->session
);
1976 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
1978 ret
= LTTCOMM_FATAL
;
1983 * Must notify the kernel thread here to update it's pollfd in order to
1984 * remove the channel(s)' fd just destroyed.
1986 ret
= notify_kernel_pollfd();
1988 ret
= LTTCOMM_FATAL
;
1995 case LTTNG_LIST_DOMAINS
:
1999 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2003 nb_dom
+= cmd_ctx
->session
->ust_trace_count
;
2005 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2010 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2011 LTTNG_DOMAIN_KERNEL
;
2013 /* TODO: User-space tracer domain support */
2017 case LTTNG_LIST_CHANNELS
:
2020 * TODO: Only kernel channels are listed here. UST listing
2021 * is needed on lttng-ust 2.0 release.
2024 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2025 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2028 ret
= setup_lttng_msg(cmd_ctx
,
2029 sizeof(struct lttng_channel
) * nb_chan
);
2034 list_lttng_channels(cmd_ctx
->session
,
2035 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2040 case LTTNG_LIST_EVENTS
:
2043 * TODO: Only kernel events are listed here. UST listing
2044 * is needed on lttng-ust 2.0 release.
2046 size_t nb_event
= 0;
2047 struct ltt_kernel_channel
*kchan
= NULL
;
2049 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2050 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2051 cmd_ctx
->session
->kernel_session
);
2052 if (kchan
== NULL
) {
2053 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2056 nb_event
+= kchan
->event_count
;
2059 ret
= setup_lttng_msg(cmd_ctx
,
2060 sizeof(struct lttng_event
) * nb_event
);
2065 DBG("Listing events (%ld events)", nb_event
);
2067 list_lttng_events(kchan
,
2068 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2073 case LTTNG_LIST_SESSIONS
:
2075 lock_session_list();
2077 if (session_list_ptr
->count
== 0) {
2078 ret
= LTTCOMM_NO_SESSION
;
2079 unlock_session_list();
2083 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2084 session_list_ptr
->count
);
2086 unlock_session_list();
2090 /* Filled the session array */
2091 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2093 unlock_session_list();
2099 case LTTNG_CALIBRATE
:
2101 /* Setup lttng message with no payload */
2102 ret
= setup_lttng_msg(cmd_ctx
, 0);
2107 switch (cmd_ctx
->lsm
->domain
.type
) {
2108 case LTTNG_DOMAIN_KERNEL
:
2110 struct lttng_kernel_calibrate kcalibrate
;
2112 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2113 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2115 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2121 /* TODO: Userspace tracing */
2122 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2130 /* Undefined command */
2131 ret
= setup_lttng_msg(cmd_ctx
, 0);
2140 /* Set return code */
2141 cmd_ctx
->llm
->ret_code
= ret
;
2143 if (cmd_ctx
->session
) {
2144 unlock_session(cmd_ctx
->session
);
2150 if (cmd_ctx
->llm
== NULL
) {
2151 DBG("Missing llm structure. Allocating one.");
2152 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2156 /* Notify client of error */
2157 cmd_ctx
->llm
->ret_code
= ret
;
2160 if (cmd_ctx
->session
) {
2161 unlock_session(cmd_ctx
->session
);
2167 * This thread manage all clients request using the unix client socket for
2170 static void *thread_manage_clients(void *data
)
2173 struct command_ctx
*cmd_ctx
= NULL
;
2174 struct pollfd pollfd
[2];
2176 DBG("[thread] Manage client started");
2178 ret
= lttcomm_listen_unix_sock(client_sock
);
2183 /* First fd is always the quit pipe */
2184 pollfd
[0].fd
= thread_quit_pipe
[0];
2187 pollfd
[1].fd
= client_sock
;
2188 pollfd
[1].events
= POLLIN
;
2190 /* Notify parent pid that we are ready
2191 * to accept command for client side.
2193 if (opt_sig_parent
) {
2194 kill(ppid
, SIGCHLD
);
2198 DBG("Accepting client command ...");
2200 /* Inifinite blocking call, waiting for transmission */
2201 ret
= poll(pollfd
, 2, -1);
2203 perror("poll client thread");
2207 /* Thread quit pipe has been closed. Killing thread. */
2208 if (pollfd
[0].revents
== POLLNVAL
) {
2210 } else if (pollfd
[1].revents
== POLLERR
) {
2211 ERR("Client socket poll error");
2215 sock
= lttcomm_accept_unix_sock(client_sock
);
2220 /* Allocate context command to process the client request */
2221 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2223 /* Allocate data buffer for reception */
2224 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2225 cmd_ctx
->llm
= NULL
;
2226 cmd_ctx
->session
= NULL
;
2229 * Data is received from the lttng client. The struct
2230 * lttcomm_session_msg (lsm) contains the command and data request of
2233 DBG("Receiving data from client ...");
2234 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2239 // TODO: Validate cmd_ctx including sanity check for security purpose.
2242 * This function dispatch the work to the kernel or userspace tracer
2243 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2244 * informations for the client. The command context struct contains
2245 * everything this function may needs.
2247 ret
= process_client_msg(cmd_ctx
);
2249 /* TODO: Inform client somehow of the fatal error. At this point,
2250 * ret < 0 means that a malloc failed (ENOMEM). */
2251 /* Error detected but still accept command */
2252 clean_command_ctx(&cmd_ctx
);
2256 DBG("Sending response (size: %d, retcode: %d)",
2257 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2258 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2260 ERR("Failed to send data back to client");
2263 clean_command_ctx(&cmd_ctx
);
2265 /* End of transmission */
2270 DBG("Client thread dying");
2278 unlink(client_unix_sock_path
);
2280 clean_command_ctx(&cmd_ctx
);
2286 * usage function on stderr
2288 static void usage(void)
2290 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2291 fprintf(stderr
, " -h, --help Display this usage.\n");
2292 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2293 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2294 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2295 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2296 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2297 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2298 fprintf(stderr
, " -V, --version Show version number.\n");
2299 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2300 fprintf(stderr
, " -q, --quiet No output at all.\n");
2301 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2302 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2306 * daemon argument parsing
2308 static int parse_args(int argc
, char **argv
)
2312 static struct option long_options
[] = {
2313 { "client-sock", 1, 0, 'c' },
2314 { "apps-sock", 1, 0, 'a' },
2315 { "kconsumerd-cmd-sock", 1, 0, 0 },
2316 { "kconsumerd-err-sock", 1, 0, 0 },
2317 { "daemonize", 0, 0, 'd' },
2318 { "sig-parent", 0, 0, 'S' },
2319 { "help", 0, 0, 'h' },
2320 { "group", 1, 0, 'g' },
2321 { "version", 0, 0, 'V' },
2322 { "quiet", 0, 0, 'q' },
2323 { "verbose", 0, 0, 'v' },
2324 { "verbose-kconsumerd", 0, 0, 'Z' },
2329 int option_index
= 0;
2330 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2337 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2339 fprintf(stderr
, " with arg %s\n", optarg
);
2343 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2346 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2352 opt_tracing_group
= strdup(optarg
);
2358 fprintf(stdout
, "%s\n", VERSION
);
2364 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2367 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2373 /* Verbose level can increase using multiple -v */
2377 opt_verbose_kconsumerd
+= 1;
2380 /* Unknown option or other error.
2381 * Error is printed by getopt, just return */
2390 * Creates the two needed socket by the daemon.
2391 * apps_sock - The communication socket for all UST apps.
2392 * client_sock - The communication of the cli tool (lttng).
2394 static int init_daemon_socket(void)
2399 old_umask
= umask(0);
2401 /* Create client tool unix socket */
2402 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2403 if (client_sock
< 0) {
2404 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2409 /* File permission MUST be 660 */
2410 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2412 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2417 /* Create the application unix socket */
2418 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2419 if (apps_sock
< 0) {
2420 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2425 /* File permission MUST be 666 */
2426 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2428 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2439 * Check if the global socket is available, and if a daemon is answering
2440 * at the other side. If yes, error is returned.
2442 static int check_existing_daemon(void)
2444 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2445 access(apps_unix_sock_path
, F_OK
) < 0)
2447 /* Is there anybody out there ? */
2448 if (lttng_session_daemon_alive())
2455 * Set the tracing group gid onto the client socket.
2457 * Race window between mkdir and chown is OK because we are going from more
2458 * permissive (root.root) to les permissive (root.tracing).
2460 static int set_permissions(void)
2465 gid
= allowed_group();
2468 WARN("No tracing group detected");
2471 ERR("Missing tracing group. Aborting execution.");
2477 /* Set lttng run dir */
2478 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2480 ERR("Unable to set group on " LTTNG_RUNDIR
);
2484 /* lttng client socket path */
2485 ret
= chown(client_unix_sock_path
, 0, gid
);
2487 ERR("Unable to set group on %s", client_unix_sock_path
);
2491 /* kconsumerd error socket path */
2492 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2494 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2498 DBG("All permissions are set");
2505 * Create the pipe used to wake up the kernel thread.
2507 static int create_kernel_poll_pipe(void)
2509 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2513 * Create the lttng run directory needed for all global sockets and pipe.
2515 static int create_lttng_rundir(void)
2519 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2521 if (errno
!= EEXIST
) {
2522 ERR("Unable to create " LTTNG_RUNDIR
);
2534 * Setup sockets and directory needed by the kconsumerd communication with the
2537 static int set_kconsumerd_sockets(void)
2541 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2542 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2545 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2546 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2549 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2551 if (errno
!= EEXIST
) {
2552 ERR("Failed to create " KCONSUMERD_PATH
);
2558 /* Create the kconsumerd error unix socket */
2559 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2560 if (kconsumerd_err_sock
< 0) {
2561 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2566 /* File permission MUST be 660 */
2567 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2569 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2579 * Signal handler for the daemon
2581 * Simply stop all worker threads, leaving main() return gracefully
2582 * after joining all threads and calling cleanup().
2584 static void sighandler(int sig
)
2588 DBG("SIGPIPE catched");
2591 DBG("SIGINT catched");
2595 DBG("SIGTERM catched");
2604 * Setup signal handler for :
2605 * SIGINT, SIGTERM, SIGPIPE
2607 static int set_signal_handler(void)
2610 struct sigaction sa
;
2613 if ((ret
= sigemptyset(&sigset
)) < 0) {
2614 perror("sigemptyset");
2618 sa
.sa_handler
= sighandler
;
2619 sa
.sa_mask
= sigset
;
2621 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2622 perror("sigaction");
2626 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2627 perror("sigaction");
2631 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2632 perror("sigaction");
2636 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2642 * Set open files limit to unlimited. This daemon can open a large number of
2643 * file descriptors in order to consumer multiple kernel traces.
2645 static void set_ulimit(void)
2650 /* The kernel does not allowed an infinite limit for open files */
2651 lim
.rlim_cur
= 65535;
2652 lim
.rlim_max
= 65535;
2654 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2656 perror("failed to set open files limit");
2663 int main(int argc
, char **argv
)
2667 const char *home_path
;
2669 /* Create thread quit pipe */
2670 if ((ret
= init_thread_quit_pipe()) < 0) {
2674 /* Parse arguments */
2676 if ((ret
= parse_args(argc
, argv
) < 0)) {
2689 /* Check if daemon is UID = 0 */
2690 is_root
= !getuid();
2693 ret
= create_lttng_rundir();
2698 if (strlen(apps_unix_sock_path
) == 0) {
2699 snprintf(apps_unix_sock_path
, PATH_MAX
,
2700 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2703 if (strlen(client_unix_sock_path
) == 0) {
2704 snprintf(client_unix_sock_path
, PATH_MAX
,
2705 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2708 home_path
= get_home_dir();
2709 if (home_path
== NULL
) {
2710 /* TODO: Add --socket PATH option */
2711 ERR("Can't get HOME directory for sockets creation.");
2716 if (strlen(apps_unix_sock_path
) == 0) {
2717 snprintf(apps_unix_sock_path
, PATH_MAX
,
2718 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2721 /* Set the cli tool unix socket path */
2722 if (strlen(client_unix_sock_path
) == 0) {
2723 snprintf(client_unix_sock_path
, PATH_MAX
,
2724 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2728 DBG("Client socket path %s", client_unix_sock_path
);
2729 DBG("Application socket path %s", apps_unix_sock_path
);
2732 * See if daemon already exist.
2734 if ((ret
= check_existing_daemon()) < 0) {
2735 ERR("Already running daemon.\n");
2737 * We do not goto exit because we must not cleanup()
2738 * because a daemon is already running.
2743 /* After this point, we can safely call cleanup() so goto error is used */
2746 * These actions must be executed as root. We do that *after* setting up
2747 * the sockets path because we MUST make the check for another daemon using
2748 * those paths *before* trying to set the kernel consumer sockets and init
2752 ret
= set_kconsumerd_sockets();
2757 /* Setup kernel tracer */
2758 init_kernel_tracer();
2760 /* Set ulimit for open files */
2764 if ((ret
= set_signal_handler()) < 0) {
2768 /* Setup the needed unix socket */
2769 if ((ret
= init_daemon_socket()) < 0) {
2773 /* Set credentials to socket */
2774 if (is_root
&& ((ret
= set_permissions()) < 0)) {
2778 /* Get parent pid if -S, --sig-parent is specified. */
2779 if (opt_sig_parent
) {
2783 /* Setup the kernel pipe for waking up the kernel thread */
2784 if ((ret
= create_kernel_poll_pipe()) < 0) {
2789 * Get session list pointer. This pointer MUST NOT be free().
2790 * This list is statically declared in session.c
2792 session_list_ptr
= get_session_list();
2794 /* Create thread to manage the client socket */
2795 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2797 perror("pthread_create");
2801 /* Create thread to manage application socket */
2802 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2804 perror("pthread_create");
2808 /* Create kernel thread to manage kernel event */
2809 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2811 perror("pthread_create");
2815 ret
= pthread_join(kernel_thread
, &status
);
2817 perror("pthread_join");
2818 goto error
; /* join error, exit without cleanup */
2822 ret
= pthread_join(apps_thread
, &status
);
2824 perror("pthread_join");
2825 goto error
; /* join error, exit without cleanup */
2829 ret
= pthread_join(client_thread
, &status
);
2831 perror("pthread_join");
2832 goto error
; /* join error, exit without cleanup */
2835 ret
= join_kconsumerd_thread();
2837 perror("join_kconsumerd");
2838 goto error
; /* join error, exit without cleanup */
2844 * cleanup() is called when no other thread is running.