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 grp
= (opt_tracing_group
!= NULL
) ?
123 (grp
= getgrnam(opt_tracing_group
)) :
124 (grp
= getgrnam(default_tracing_group
));
135 * Return -1 on error or 0 if all pipes are created.
137 static int init_thread_quit_pipe(void)
141 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
143 perror("thread quit pipe");
152 * Complete teardown of a kernel session. This free all data structure related
153 * to a kernel session and update counter.
155 static void teardown_kernel_session(struct ltt_session
*session
)
157 if (session
->kernel_session
!= NULL
) {
158 DBG("Tearing down kernel session");
159 trace_destroy_kernel_session(session
->kernel_session
);
160 /* Extra precaution */
161 session
->kernel_session
= NULL
;
165 static void stop_threads(void)
167 /* Stopping all threads */
168 DBG("Terminating all threads");
169 close(thread_quit_pipe
[0]);
170 close(thread_quit_pipe
[1]);
176 static void cleanup(void)
180 struct ltt_session
*sess
, *stmp
;
185 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
186 "Matthew, BEET driven development works!%c[%dm",
187 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
190 DBG("Removing %s directory", LTTNG_RUNDIR
);
191 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
193 ERR("asprintf failed. Something is really wrong!");
196 /* Remove lttng run directory */
199 ERR("Unable to clean " LTTNG_RUNDIR
);
202 DBG("Cleaning up all session");
204 /* Destroy session list mutex */
205 if (session_list_ptr
!= NULL
) {
206 pthread_mutex_destroy(&session_list_ptr
->lock
);
208 /* Cleanup ALL session */
209 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
210 teardown_kernel_session(sess
);
211 // TODO complete session cleanup (including UST)
215 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
217 DBG("Closing kernel fd");
218 close(kernel_tracer_fd
);
220 DBG("Unloading kernel modules");
221 modprobe_remove_kernel_modules();
225 * Send data on a unix socket using the liblttsessiondcomm API.
227 * Return lttcomm error code.
229 static int send_unix_sock(int sock
, void *buf
, size_t len
)
231 /* Check valid length */
236 return lttcomm_send_unix_sock(sock
, buf
, len
);
240 * Free memory of a command context structure.
242 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
244 DBG("Clean command context structure");
246 if ((*cmd_ctx
)->llm
) {
247 free((*cmd_ctx
)->llm
);
249 if ((*cmd_ctx
)->lsm
) {
250 free((*cmd_ctx
)->lsm
);
258 * Send all stream fds of kernel channel to the consumer.
260 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
264 struct ltt_kernel_stream
*stream
;
265 struct lttcomm_kconsumerd_header lkh
;
266 struct lttcomm_kconsumerd_msg lkm
;
268 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
270 nb_fd
= channel
->stream_count
;
273 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
274 lkh
.cmd_type
= ADD_STREAM
;
276 DBG("Sending kconsumerd header");
278 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
280 perror("send kconsumerd header");
284 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
285 if (stream
->fd
!= 0) {
287 lkm
.state
= stream
->state
;
288 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
289 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
291 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
293 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
295 perror("send kconsumerd fd");
301 DBG("Kconsumerd channel fds sent");
310 * Send all stream fds of the kernel session to the consumer.
312 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
315 struct ltt_kernel_channel
*chan
;
316 struct lttcomm_kconsumerd_header lkh
;
317 struct lttcomm_kconsumerd_msg lkm
;
320 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
321 lkh
.cmd_type
= ADD_STREAM
;
323 DBG("Sending kconsumerd header for metadata");
325 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
327 perror("send kconsumerd header");
331 DBG("Sending metadata stream fd");
333 if (session
->metadata_stream_fd
!= 0) {
334 /* Send metadata stream fd first */
335 lkm
.fd
= session
->metadata_stream_fd
;
336 lkm
.state
= ACTIVE_FD
;
337 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
338 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
340 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
342 perror("send kconsumerd fd");
347 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
348 ret
= send_kconsumerd_channel_fds(sock
, chan
);
354 DBG("Kconsumerd fds (metadata and channel streams) sent");
364 * Return a socket connected to the libust communication socket of the
365 * application identified by the pid.
367 * If the pid is not found in the traceable list, return -1 to indicate error.
369 static int ust_connect_app(pid_t pid
)
372 struct ltt_traceable_app
*lta
;
374 DBG("Connect to application pid %d", pid
);
376 lta
= find_app_by_pid(pid
);
379 DBG("Application pid %d not found", pid
);
383 sock
= ustctl_connect_pid(lta
->pid
);
385 ERR("Fail connecting to the PID %d", pid
);
390 #endif /* DISABLED */
393 * Notify apps by writing 42 to a named pipe using name. Every applications
394 * waiting for a ltt-sessiond will be notified and re-register automatically to
395 * the session daemon.
397 * Return open or write error value.
399 static int notify_apps(const char *name
)
404 DBG("Notify the global application pipe");
406 /* Try opening the global pipe */
407 fd
= open(name
, O_WRONLY
);
412 /* Notify by writing on the pipe */
413 ret
= write(fd
, "42", 2);
423 * Setup the outgoing data buffer for the response (llm) by allocating the
424 * right amount of memory and copying the original information from the lsm
427 * Return total size of the buffer pointed by buf.
429 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
435 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
436 if (cmd_ctx
->llm
== NULL
) {
442 /* Copy common data */
443 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
444 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
446 cmd_ctx
->llm
->data_size
= size
;
447 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
456 * Update the kernel pollfd set of all channel fd available over all tracing
457 * session. Add the wakeup pipe at the end of the set.
459 static int update_kernel_pollfd(void)
463 * The wakup pipe and the quit pipe are needed so the number of fds starts
464 * at 2 for those pipes.
466 unsigned int nb_fd
= 2;
467 struct ltt_session
*session
;
468 struct ltt_kernel_channel
*channel
;
470 DBG("Updating kernel_pollfd");
472 /* Get the number of channel of all kernel session */
474 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
475 lock_session(session
);
476 if (session
->kernel_session
== NULL
) {
477 unlock_session(session
);
480 nb_fd
+= session
->kernel_session
->channel_count
;
481 unlock_session(session
);
484 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
486 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
487 if (kernel_pollfd
== NULL
) {
488 perror("malloc kernel_pollfd");
492 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
493 lock_session(session
);
494 if (session
->kernel_session
== NULL
) {
495 unlock_session(session
);
499 ERR("To much channel for kernel_pollfd size");
500 unlock_session(session
);
503 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
504 kernel_pollfd
[i
].fd
= channel
->fd
;
505 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
508 unlock_session(session
);
510 unlock_session_list();
512 /* Adding wake up pipe */
513 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
514 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
516 /* Adding the quit pipe */
517 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
522 unlock_session_list();
527 * Find the channel fd from 'fd' over all tracing session. When found, check
528 * for new channel stream and send those stream fds to the kernel consumer.
530 * Useful for CPU hotplug feature.
532 static int update_kernel_stream(int fd
)
535 struct ltt_session
*session
;
536 struct ltt_kernel_channel
*channel
;
538 DBG("Updating kernel streams for channel fd %d", fd
);
541 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
542 lock_session(session
);
543 if (session
->kernel_session
== NULL
) {
544 unlock_session(session
);
547 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
548 if (channel
->fd
== fd
) {
549 DBG("Channel found, updating kernel streams");
550 ret
= kernel_open_channel_stream(channel
);
555 * Have we already sent fds to the consumer? If yes, it means that
556 * tracing is started so it is safe to send our updated stream fds.
558 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
559 ret
= send_kconsumerd_channel_fds(kconsumerd_cmd_sock
, channel
);
567 unlock_session(session
);
571 unlock_session_list();
573 unlock_session(session
);
579 * This thread manage event coming from the kernel.
581 * Features supported in this thread:
584 static void *thread_manage_kernel(void *data
)
586 int ret
, i
, nb_fd
= 0;
588 int update_poll_flag
= 1;
590 DBG("Thread manage kernel started");
593 if (update_poll_flag
== 1) {
594 nb_fd
= update_kernel_pollfd();
598 update_poll_flag
= 0;
601 DBG("Polling on %d fds", nb_fd
);
603 /* Poll infinite value of time */
604 ret
= poll(kernel_pollfd
, nb_fd
, -1);
606 perror("poll kernel thread");
608 } else if (ret
== 0) {
609 /* Should not happen since timeout is infinite */
613 /* Thread quit pipe has been closed. Killing thread. */
614 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
618 DBG("Kernel poll event triggered");
621 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
624 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
626 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
627 update_poll_flag
= 1;
635 for (i
= 0; i
< nb_fd
; i
++) {
636 switch (kernel_pollfd
[i
].revents
) {
638 * New CPU detected by the kernel. Adding kernel stream to kernel
639 * session and updating the kernel consumer
641 case POLLIN
| POLLRDNORM
:
642 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
652 DBG("Kernel thread dying");
657 close(kernel_poll_pipe
[0]);
658 close(kernel_poll_pipe
[1]);
663 * This thread manage the kconsumerd error sent back to the session daemon.
665 static void *thread_manage_kconsumerd(void *data
)
668 enum lttcomm_return_code code
;
669 struct pollfd pollfd
[2];
671 DBG("[thread] Manage kconsumerd started");
673 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
678 /* First fd is always the quit pipe */
679 pollfd
[0].fd
= thread_quit_pipe
[0];
682 pollfd
[1].fd
= kconsumerd_err_sock
;
683 pollfd
[1].events
= POLLIN
;
685 /* Inifinite blocking call, waiting for transmission */
686 ret
= poll(pollfd
, 2, -1);
688 perror("poll kconsumerd thread");
692 /* Thread quit pipe has been closed. Killing thread. */
693 if (pollfd
[0].revents
== POLLNVAL
) {
695 } else if (pollfd
[1].revents
== POLLERR
) {
696 ERR("Kconsumerd err socket poll error");
700 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
705 /* Getting status code from kconsumerd */
706 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
711 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
712 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
713 if (kconsumerd_cmd_sock
< 0) {
714 sem_post(&kconsumerd_sem
);
715 perror("kconsumerd connect");
718 /* Signal condition to tell that the kconsumerd is ready */
719 sem_post(&kconsumerd_sem
);
720 DBG("Kconsumerd command socket ready");
722 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
723 lttcomm_get_readable_code(-code
));
727 /* Wait for any kconsumerd error */
728 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
730 ERR("Kconsumerd closed the command socket");
734 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
737 DBG("Kconsumerd thread dying");
738 if (kconsumerd_err_sock
) {
739 close(kconsumerd_err_sock
);
741 if (kconsumerd_cmd_sock
) {
742 close(kconsumerd_cmd_sock
);
748 unlink(kconsumerd_err_unix_sock_path
);
749 unlink(kconsumerd_cmd_unix_sock_path
);
756 * This thread manage the application socket communication
758 static void *thread_manage_apps(void *data
)
761 struct pollfd pollfd
[2];
763 /* TODO: Something more elegant is needed but fine for now */
764 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
765 * for 32-bit vs 64-bit compat processes. */
766 /* replicate in ust with version number */
768 int reg
; /* 1:register, 0:unregister */
773 DBG("[thread] Manage apps started");
775 ret
= lttcomm_listen_unix_sock(apps_sock
);
780 /* First fd is always the quit pipe */
781 pollfd
[0].fd
= thread_quit_pipe
[0];
784 pollfd
[1].fd
= apps_sock
;
785 pollfd
[1].events
= POLLIN
;
787 /* Notify all applications to register */
788 notify_apps(default_global_apps_pipe
);
791 DBG("Accepting application registration");
793 /* Inifinite blocking call, waiting for transmission */
794 ret
= poll(pollfd
, 2, -1);
796 perror("poll apps thread");
800 /* Thread quit pipe has been closed. Killing thread. */
801 if (pollfd
[0].revents
== POLLNVAL
) {
803 } else if (pollfd
[1].revents
== POLLERR
) {
804 ERR("Apps socket poll error");
808 sock
= lttcomm_accept_unix_sock(apps_sock
);
814 * Basic recv here to handle the very simple data
815 * that the libust send to register (reg_msg).
817 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
823 /* Add application to the global traceable list */
824 if (reg_msg
.reg
== 1) {
826 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
828 /* register_traceable_app only return an error with
829 * ENOMEM. At this point, we better stop everything.
835 unregister_traceable_app(reg_msg
.pid
);
840 DBG("Apps thread dying");
848 unlink(apps_unix_sock_path
);
853 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
854 * exec or it will fails.
856 static int spawn_kconsumerd_thread(void)
860 /* Setup semaphore */
861 sem_init(&kconsumerd_sem
, 0, 0);
863 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
865 perror("pthread_create kconsumerd");
869 /* Wait for the kconsumerd thread to be ready */
870 sem_wait(&kconsumerd_sem
);
872 if (kconsumerd_pid
== 0) {
873 ERR("Kconsumerd did not start");
880 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
884 static int join_kconsumerd_thread(void)
889 if (kconsumerd_pid
!= 0) {
890 ret
= kill(kconsumerd_pid
, SIGTERM
);
892 ERR("Error killing kconsumerd");
895 return pthread_join(kconsumerd_thread
, &status
);
902 * Fork and exec a kernel consumer daemon (kconsumerd).
904 * Return pid if successful else -1.
906 static pid_t
spawn_kconsumerd(void)
910 const char *verbosity
;
912 DBG("Spawning kconsumerd");
919 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
920 verbosity
= "--verbose";
922 verbosity
= "--quiet";
924 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
926 perror("kernel start consumer exec");
929 } else if (pid
> 0) {
933 perror("kernel start consumer fork");
943 * Spawn the kconsumerd daemon and session daemon thread.
945 static int start_kconsumerd(void)
949 pthread_mutex_lock(&kconsumerd_pid_mutex
);
950 if (kconsumerd_pid
!= 0) {
951 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
955 ret
= spawn_kconsumerd();
957 ERR("Spawning kconsumerd failed");
958 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
959 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
963 /* Setting up the global kconsumerd_pid */
964 kconsumerd_pid
= ret
;
965 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
967 DBG("Kconsumerd pid %d", ret
);
969 DBG("Spawning kconsumerd thread");
970 ret
= spawn_kconsumerd_thread();
972 ERR("Fatal error spawning kconsumerd thread");
984 * modprobe_kernel_modules
986 static int modprobe_kernel_modules(void)
991 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
992 ret
= snprintf(modprobe
, sizeof(modprobe
),
993 "/sbin/modprobe %s%s",
994 kernel_modules_list
[i
].required
? "" : "--quiet ",
995 kernel_modules_list
[i
].name
);
997 perror("snprintf modprobe");
1000 modprobe
[sizeof(modprobe
) - 1] = '\0';
1001 ret
= system(modprobe
);
1003 ERR("Unable to launch modprobe for module %s",
1004 kernel_modules_list
[i
].name
);
1005 } else if (kernel_modules_list
[i
].required
1006 && WEXITSTATUS(ret
) != 0) {
1007 ERR("Unable to load module %s",
1008 kernel_modules_list
[i
].name
);
1010 DBG("Modprobe successfully %s",
1011 kernel_modules_list
[i
].name
);
1020 * modprobe_remove_kernel_modules
1021 * Remove modules in reverse load order.
1023 static int modprobe_remove_kernel_modules(void)
1028 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
1029 ret
= snprintf(modprobe
, sizeof(modprobe
),
1030 "/sbin/modprobe --remove --quiet %s",
1031 kernel_modules_list
[i
].name
);
1033 perror("snprintf modprobe --remove");
1036 modprobe
[sizeof(modprobe
) - 1] = '\0';
1037 ret
= system(modprobe
);
1039 ERR("Unable to launch modprobe --remove for module %s",
1040 kernel_modules_list
[i
].name
);
1041 } else if (kernel_modules_list
[i
].required
1042 && WEXITSTATUS(ret
) != 0) {
1043 ERR("Unable to remove module %s",
1044 kernel_modules_list
[i
].name
);
1046 DBG("Modprobe removal successful %s",
1047 kernel_modules_list
[i
].name
);
1058 static int mount_debugfs(char *path
)
1061 char *type
= "debugfs";
1063 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1068 ret
= mount(type
, path
, type
, 0, NULL
);
1070 perror("mount debugfs");
1074 DBG("Mounted debugfs successfully at %s", path
);
1081 * Setup necessary data for kernel tracer action.
1083 static void init_kernel_tracer(void)
1086 char *proc_mounts
= "/proc/mounts";
1088 char *debugfs_path
= NULL
, *lttng_path
;
1091 /* Detect debugfs */
1092 fp
= fopen(proc_mounts
, "r");
1094 ERR("Unable to probe %s", proc_mounts
);
1098 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1099 if (strstr(line
, "debugfs") != NULL
) {
1100 /* Remove first string */
1102 /* Dup string here so we can reuse line later on */
1103 debugfs_path
= strdup(strtok(NULL
, " "));
1104 DBG("Got debugfs path : %s", debugfs_path
);
1111 /* Mount debugfs if needded */
1112 if (debugfs_path
== NULL
) {
1113 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1115 perror("asprintf debugfs path");
1118 ret
= mount_debugfs(debugfs_path
);
1124 /* Modprobe lttng kernel modules */
1125 ret
= modprobe_kernel_modules();
1130 /* Setup lttng kernel path */
1131 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1133 perror("asprintf lttng path");
1137 /* Open debugfs lttng */
1138 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1139 if (kernel_tracer_fd
< 0) {
1140 DBG("Failed to open %s", lttng_path
);
1146 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1156 WARN("No kernel tracer available");
1157 kernel_tracer_fd
= 0;
1162 * Start tracing by creating trace directory and sending FDs to the kernel
1165 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1169 if (session
->kconsumer_fds_sent
== 0) {
1170 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
1172 ERR("Send kconsumerd fds failed");
1173 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1177 session
->kconsumer_fds_sent
= 1;
1185 * Notify kernel thread to update it's pollfd.
1187 static int notify_kernel_pollfd(void)
1191 /* Inform kernel thread of the new kernel channel */
1192 ret
= write(kernel_poll_pipe
[1], "!", 1);
1194 perror("write kernel poll pipe");
1201 * Allocate a channel structure and fill it.
1203 static struct lttng_channel
*init_default_channel(char *name
)
1205 struct lttng_channel
*chan
;
1207 chan
= malloc(sizeof(struct lttng_channel
));
1209 perror("init channel malloc");
1213 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1214 perror("snprintf channel name");
1218 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1219 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
1220 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
1221 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1222 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1223 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1230 * Create a kernel tracer session then create the default channel.
1232 static int create_kernel_session(struct ltt_session
*session
)
1236 DBG("Creating kernel session");
1238 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1240 ret
= LTTCOMM_KERN_SESS_FAIL
;
1244 ret
= mkdir_recursive(session
->path
, S_IRWXU
| S_IRWXG
,
1245 geteuid(), allowed_group());
1247 if (ret
!= -EEXIST
) {
1248 ERR("Trace directory creation error");
1258 * Using the session list, filled a lttng_session array to send back to the
1259 * client for session listing.
1261 * The session list lock MUST be acquired before calling this function. Use
1262 * lock_session_list() and unlock_session_list().
1264 static void list_lttng_sessions(struct lttng_session
*sessions
)
1267 struct ltt_session
*session
;
1269 DBG("Getting all available session");
1271 * Iterate over session list and append data after the control struct in
1274 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1275 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1276 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1282 * Fill lttng_channel array of all channels.
1284 static void list_lttng_channels(struct ltt_session
*session
,
1285 struct lttng_channel
*channels
)
1288 struct ltt_kernel_channel
*kchan
;
1290 DBG("Listing channels for session %s", session
->name
);
1292 /* Kernel channels */
1293 if (session
->kernel_session
!= NULL
) {
1294 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1295 /* Copy lttng_channel struct to array */
1296 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1297 channels
[i
].enabled
= kchan
->enabled
;
1302 /* TODO: Missing UST listing */
1306 * Fill lttng_event array of all events in the channel.
1308 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1309 struct lttng_event
*events
)
1312 * TODO: This is ONLY kernel. Need UST support.
1315 struct ltt_kernel_event
*event
;
1317 DBG("Listing events for channel %s", kchan
->channel
->name
);
1319 /* Kernel channels */
1320 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1321 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1322 events
[i
].enabled
= event
->enabled
;
1323 switch (event
->event
->instrumentation
) {
1324 case LTTNG_KERNEL_TRACEPOINT
:
1325 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1327 case LTTNG_KERNEL_KPROBE
:
1328 case LTTNG_KERNEL_KRETPROBE
:
1329 events
[i
].type
= LTTNG_EVENT_PROBE
;
1330 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1331 sizeof(struct lttng_kernel_kprobe
));
1333 case LTTNG_KERNEL_FUNCTION
:
1334 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1335 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1336 sizeof(struct lttng_kernel_function
));
1344 * Process the command requested by the lttng client within the command
1345 * context structure. This function make sure that the return structure (llm)
1346 * is set and ready for transmission before returning.
1348 * Return any error encountered or 0 for success.
1350 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1352 int ret
= LTTCOMM_OK
;
1354 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1356 /* Listing commands don't need a session */
1357 switch (cmd_ctx
->lsm
->cmd_type
) {
1358 case LTTNG_CREATE_SESSION
:
1359 case LTTNG_LIST_SESSIONS
:
1360 case LTTNG_LIST_TRACEPOINTS
:
1361 case LTTNG_CALIBRATE
:
1364 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1365 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1366 if (cmd_ctx
->session
== NULL
) {
1367 /* If session name not found */
1368 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1369 ret
= LTTCOMM_SESS_NOT_FOUND
;
1370 } else { /* If no session name specified */
1371 ret
= LTTCOMM_SELECT_SESS
;
1375 /* Acquire lock for the session */
1376 lock_session(cmd_ctx
->session
);
1382 * Check domain type for specific "pre-action".
1384 switch (cmd_ctx
->lsm
->domain
.type
) {
1385 case LTTNG_DOMAIN_KERNEL
:
1386 /* Kernel tracer check */
1387 if (kernel_tracer_fd
== 0) {
1388 init_kernel_tracer();
1389 if (kernel_tracer_fd
== 0) {
1390 ret
= LTTCOMM_KERN_NA
;
1395 /* Need a session for kernel command */
1396 switch (cmd_ctx
->lsm
->cmd_type
) {
1397 case LTTNG_CREATE_SESSION
:
1398 case LTTNG_LIST_SESSIONS
:
1399 case LTTNG_LIST_TRACEPOINTS
:
1400 case LTTNG_CALIBRATE
:
1403 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1404 ret
= create_kernel_session(cmd_ctx
->session
);
1406 ret
= LTTCOMM_KERN_SESS_FAIL
;
1410 /* Start the kernel consumer daemon */
1411 if (kconsumerd_pid
== 0) {
1412 ret
= start_kconsumerd();
1424 /* Process by command type */
1425 switch (cmd_ctx
->lsm
->cmd_type
) {
1426 case LTTNG_ADD_CONTEXT
:
1428 struct lttng_kernel_context kctx
;
1430 /* Setup lttng message with no payload */
1431 ret
= setup_lttng_msg(cmd_ctx
, 0);
1436 switch (cmd_ctx
->lsm
->domain
.type
) {
1437 case LTTNG_DOMAIN_KERNEL
:
1438 /* Create Kernel context */
1439 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1440 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1441 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1442 strncpy(kctx
.u
.perf_counter
.name
,
1443 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1444 LTTNG_SYMBOL_NAME_LEN
);
1446 /* Add kernel context to kernel tracer. See context.c */
1447 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1448 cmd_ctx
->lsm
->u
.context
.event_name
,
1449 cmd_ctx
->lsm
->u
.context
.channel_name
);
1450 if (ret
!= LTTCOMM_OK
) {
1455 /* TODO: Userspace tracing */
1456 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1463 case LTTNG_DISABLE_CHANNEL
:
1465 struct ltt_kernel_channel
*kchan
;
1467 /* Setup lttng message with no payload */
1468 ret
= setup_lttng_msg(cmd_ctx
, 0);
1473 switch (cmd_ctx
->lsm
->domain
.type
) {
1474 case LTTNG_DOMAIN_KERNEL
:
1475 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1476 cmd_ctx
->session
->kernel_session
);
1477 if (kchan
== NULL
) {
1478 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1480 } else if (kchan
->enabled
== 1) {
1481 ret
= kernel_disable_channel(kchan
);
1483 if (ret
!= EEXIST
) {
1484 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1489 kernel_wait_quiescent(kernel_tracer_fd
);
1492 /* TODO: Userspace tracing */
1493 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1500 case LTTNG_DISABLE_EVENT
:
1502 struct ltt_kernel_channel
*kchan
;
1503 struct ltt_kernel_event
*kevent
;
1505 /* Setup lttng message with no payload */
1506 ret
= setup_lttng_msg(cmd_ctx
, 0);
1511 switch (cmd_ctx
->lsm
->domain
.type
) {
1512 case LTTNG_DOMAIN_KERNEL
:
1513 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1514 cmd_ctx
->session
->kernel_session
);
1515 if (kchan
== NULL
) {
1516 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1520 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1521 if (kevent
!= NULL
) {
1522 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1523 kchan
->channel
->name
);
1524 ret
= kernel_disable_event(kevent
);
1526 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1531 kernel_wait_quiescent(kernel_tracer_fd
);
1534 /* TODO: Userspace tracing */
1535 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1542 case LTTNG_DISABLE_ALL_EVENT
:
1544 struct ltt_kernel_channel
*kchan
;
1545 struct ltt_kernel_event
*kevent
;
1547 /* Setup lttng message with no payload */
1548 ret
= setup_lttng_msg(cmd_ctx
, 0);
1553 switch (cmd_ctx
->lsm
->domain
.type
) {
1554 case LTTNG_DOMAIN_KERNEL
:
1555 DBG("Disabling all enabled kernel events");
1556 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1557 cmd_ctx
->session
->kernel_session
);
1558 if (kchan
== NULL
) {
1559 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1563 /* For each event in the kernel session */
1564 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1565 DBG("Disabling kernel event %s for channel %s.",
1566 kevent
->event
->name
, kchan
->channel
->name
);
1567 ret
= kernel_disable_event(kevent
);
1573 /* Quiescent wait after event disable */
1574 kernel_wait_quiescent(kernel_tracer_fd
);
1577 /* TODO: Userspace tracing */
1578 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1585 case LTTNG_ENABLE_CHANNEL
:
1587 struct ltt_kernel_channel
*kchan
;
1589 /* Setup lttng message with no payload */
1590 ret
= setup_lttng_msg(cmd_ctx
, 0);
1595 switch (cmd_ctx
->lsm
->domain
.type
) {
1596 case LTTNG_DOMAIN_KERNEL
:
1597 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1598 cmd_ctx
->session
->kernel_session
);
1599 if (kchan
== NULL
) {
1600 /* Channel not found, creating it */
1601 DBG("Creating kernel channel");
1603 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1604 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1606 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1610 /* Notify kernel thread that there is a new channel */
1611 ret
= notify_kernel_pollfd();
1613 ret
= LTTCOMM_FATAL
;
1616 } else if (kchan
->enabled
== 0) {
1617 ret
= kernel_enable_channel(kchan
);
1619 if (ret
!= EEXIST
) {
1620 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1626 kernel_wait_quiescent(kernel_tracer_fd
);
1629 /* TODO: Userspace tracing */
1630 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1637 case LTTNG_ENABLE_EVENT
:
1640 struct ltt_kernel_channel
*kchan
;
1641 struct ltt_kernel_event
*kevent
;
1642 struct lttng_channel
*chan
;
1644 /* Setup lttng message with no payload */
1645 ret
= setup_lttng_msg(cmd_ctx
, 0);
1650 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1652 switch (cmd_ctx
->lsm
->domain
.type
) {
1653 case LTTNG_DOMAIN_KERNEL
:
1655 kchan
= get_kernel_channel_by_name(channel_name
,
1656 cmd_ctx
->session
->kernel_session
);
1657 if (kchan
== NULL
) {
1658 DBG("Channel not found. Creating channel %s", channel_name
);
1660 chan
= init_default_channel(channel_name
);
1662 ret
= LTTCOMM_FATAL
;
1666 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1667 chan
, cmd_ctx
->session
->path
);
1669 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1673 } while (kchan
== NULL
);
1675 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
1676 if (kevent
== NULL
) {
1677 DBG("Creating kernel event %s for channel %s.",
1678 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
1679 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
1681 DBG("Enabling kernel event %s for channel %s.",
1682 kevent
->event
->name
, channel_name
);
1683 ret
= kernel_enable_event(kevent
);
1684 if (ret
== -EEXIST
) {
1685 ret
= LTTCOMM_KERN_EVENT_EXIST
;
1691 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1695 kernel_wait_quiescent(kernel_tracer_fd
);
1698 /* TODO: Userspace tracing */
1699 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1705 case LTTNG_ENABLE_ALL_EVENT
:
1709 struct ltt_kernel_channel
*kchan
;
1710 struct ltt_kernel_event
*kevent
;
1711 struct lttng_event
*event_list
;
1712 struct lttng_channel
*chan
;
1714 /* Setup lttng message with no payload */
1715 ret
= setup_lttng_msg(cmd_ctx
, 0);
1720 DBG("Enabling all kernel event");
1722 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1724 switch (cmd_ctx
->lsm
->domain
.type
) {
1725 case LTTNG_DOMAIN_KERNEL
:
1727 kchan
= get_kernel_channel_by_name(channel_name
,
1728 cmd_ctx
->session
->kernel_session
);
1729 if (kchan
== NULL
) {
1730 DBG("Channel not found. Creating channel %s", channel_name
);
1732 chan
= init_default_channel(channel_name
);
1734 ret
= LTTCOMM_FATAL
;
1738 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1739 chan
, cmd_ctx
->session
->path
);
1741 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1745 } while (kchan
== NULL
);
1747 /* For each event in the kernel session */
1748 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1749 DBG("Enabling kernel event %s for channel %s.",
1750 kevent
->event
->name
, channel_name
);
1751 ret
= kernel_enable_event(kevent
);
1757 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1759 ret
= LTTCOMM_KERN_LIST_FAIL
;
1763 for (i
= 0; i
< size
; i
++) {
1764 kevent
= get_kernel_event_by_name(event_list
[i
].name
, kchan
);
1765 if (kevent
== NULL
) {
1766 /* Default event type for enable all */
1767 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1768 /* Enable each single tracepoint event */
1769 ret
= kernel_create_event(&event_list
[i
], kchan
);
1771 /* Ignore error here and continue */
1778 /* Quiescent wait after event enable */
1779 kernel_wait_quiescent(kernel_tracer_fd
);
1782 /* TODO: Userspace tracing */
1783 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1790 case LTTNG_LIST_TRACEPOINTS
:
1792 struct lttng_event
*events
;
1793 ssize_t nb_events
= 0;
1795 switch (cmd_ctx
->lsm
->domain
.type
) {
1796 case LTTNG_DOMAIN_KERNEL
:
1797 DBG("Listing kernel events");
1798 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
1799 if (nb_events
< 0) {
1800 ret
= LTTCOMM_KERN_LIST_FAIL
;
1805 /* TODO: Userspace listing */
1806 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1811 * Setup lttng message with payload size set to the event list size in
1812 * bytes and then copy list into the llm payload.
1814 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
1820 /* Copy event list into message payload */
1821 memcpy(cmd_ctx
->llm
->payload
, events
,
1822 sizeof(struct lttng_event
) * nb_events
);
1829 case LTTNG_START_TRACE
:
1831 struct ltt_kernel_channel
*chan
;
1833 /* Setup lttng message with no payload */
1834 ret
= setup_lttng_msg(cmd_ctx
, 0);
1839 /* Kernel tracing */
1840 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1841 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1842 DBG("Open kernel metadata");
1843 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1844 cmd_ctx
->session
->path
);
1846 ret
= LTTCOMM_KERN_META_FAIL
;
1851 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1852 DBG("Opening kernel metadata stream");
1853 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1854 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1856 ERR("Kernel create metadata stream failed");
1857 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1863 /* For each channel */
1864 cds_list_for_each_entry(chan
,
1865 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1866 if (chan
->stream_count
== 0) {
1867 ret
= kernel_open_channel_stream(chan
);
1869 ERR("Kernel create channel stream failed");
1870 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1873 /* Update the stream global counter */
1874 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1878 DBG("Start kernel tracing");
1879 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1881 ERR("Kernel start session failed");
1882 ret
= LTTCOMM_KERN_START_FAIL
;
1886 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1888 ret
= LTTCOMM_KERN_START_FAIL
;
1892 /* Quiescent wait after starting trace */
1893 kernel_wait_quiescent(kernel_tracer_fd
);
1896 /* TODO: Start all UST traces */
1901 case LTTNG_STOP_TRACE
:
1903 struct ltt_kernel_channel
*chan
;
1904 /* Setup lttng message with no payload */
1905 ret
= setup_lttng_msg(cmd_ctx
, 0);
1911 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1912 DBG("Stop kernel tracing");
1914 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1916 ERR("Kernel metadata flush failed");
1919 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1920 ret
= kernel_flush_buffer(chan
);
1922 ERR("Kernel flush buffer error");
1926 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1928 ERR("Kernel stop session failed");
1929 ret
= LTTCOMM_KERN_STOP_FAIL
;
1933 /* Quiescent wait after stopping trace */
1934 kernel_wait_quiescent(kernel_tracer_fd
);
1937 /* TODO : User-space tracer */
1942 case LTTNG_CREATE_SESSION
:
1944 /* Setup lttng message with no payload */
1945 ret
= setup_lttng_msg(cmd_ctx
, 0);
1950 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
1952 if (ret
== -EEXIST
) {
1953 ret
= LTTCOMM_EXIST_SESS
;
1955 ret
= LTTCOMM_FATAL
;
1963 case LTTNG_DESTROY_SESSION
:
1965 /* Setup lttng message with no payload */
1966 ret
= setup_lttng_msg(cmd_ctx
, 0);
1971 /* Clean kernel session teardown */
1972 teardown_kernel_session(cmd_ctx
->session
);
1974 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
1976 ret
= LTTCOMM_FATAL
;
1981 * Must notify the kernel thread here to update it's pollfd in order to
1982 * remove the channel(s)' fd just destroyed.
1984 ret
= notify_kernel_pollfd();
1986 ret
= LTTCOMM_FATAL
;
1993 case LTTNG_LIST_DOMAINS
:
1997 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2001 nb_dom
+= cmd_ctx
->session
->ust_trace_count
;
2003 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2008 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2009 LTTNG_DOMAIN_KERNEL
;
2011 /* TODO: User-space tracer domain support */
2015 case LTTNG_LIST_CHANNELS
:
2018 * TODO: Only kernel channels are listed here. UST listing
2019 * is needed on lttng-ust 2.0 release.
2022 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2023 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2026 ret
= setup_lttng_msg(cmd_ctx
,
2027 sizeof(struct lttng_channel
) * nb_chan
);
2032 list_lttng_channels(cmd_ctx
->session
,
2033 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2038 case LTTNG_LIST_EVENTS
:
2041 * TODO: Only kernel events are listed here. UST listing
2042 * is needed on lttng-ust 2.0 release.
2044 size_t nb_event
= 0;
2045 struct ltt_kernel_channel
*kchan
= NULL
;
2047 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2048 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2049 cmd_ctx
->session
->kernel_session
);
2050 if (kchan
== NULL
) {
2051 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2054 nb_event
+= kchan
->event_count
;
2057 ret
= setup_lttng_msg(cmd_ctx
,
2058 sizeof(struct lttng_event
) * nb_event
);
2063 DBG("Listing events (%ld events)", nb_event
);
2065 list_lttng_events(kchan
,
2066 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2071 case LTTNG_LIST_SESSIONS
:
2073 lock_session_list();
2075 if (session_list_ptr
->count
== 0) {
2076 ret
= LTTCOMM_NO_SESSION
;
2077 unlock_session_list();
2081 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2082 session_list_ptr
->count
);
2084 unlock_session_list();
2088 /* Filled the session array */
2089 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2091 unlock_session_list();
2097 case LTTNG_CALIBRATE
:
2099 /* Setup lttng message with no payload */
2100 ret
= setup_lttng_msg(cmd_ctx
, 0);
2105 switch (cmd_ctx
->lsm
->domain
.type
) {
2106 case LTTNG_DOMAIN_KERNEL
:
2108 struct lttng_kernel_calibrate kcalibrate
;
2110 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2111 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2113 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2119 /* TODO: Userspace tracing */
2120 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2128 /* Undefined command */
2129 ret
= setup_lttng_msg(cmd_ctx
, 0);
2138 /* Set return code */
2139 cmd_ctx
->llm
->ret_code
= ret
;
2141 if (cmd_ctx
->session
) {
2142 unlock_session(cmd_ctx
->session
);
2148 if (cmd_ctx
->llm
== NULL
) {
2149 DBG("Missing llm structure. Allocating one.");
2150 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2154 /* Notify client of error */
2155 cmd_ctx
->llm
->ret_code
= ret
;
2158 if (cmd_ctx
->session
) {
2159 unlock_session(cmd_ctx
->session
);
2165 * This thread manage all clients request using the unix client socket for
2168 static void *thread_manage_clients(void *data
)
2171 struct command_ctx
*cmd_ctx
= NULL
;
2172 struct pollfd pollfd
[2];
2174 DBG("[thread] Manage client started");
2176 ret
= lttcomm_listen_unix_sock(client_sock
);
2181 /* First fd is always the quit pipe */
2182 pollfd
[0].fd
= thread_quit_pipe
[0];
2185 pollfd
[1].fd
= client_sock
;
2186 pollfd
[1].events
= POLLIN
;
2188 /* Notify parent pid that we are ready
2189 * to accept command for client side.
2191 if (opt_sig_parent
) {
2192 kill(ppid
, SIGCHLD
);
2196 DBG("Accepting client command ...");
2198 /* Inifinite blocking call, waiting for transmission */
2199 ret
= poll(pollfd
, 2, -1);
2201 perror("poll client thread");
2205 /* Thread quit pipe has been closed. Killing thread. */
2206 if (pollfd
[0].revents
== POLLNVAL
) {
2208 } else if (pollfd
[1].revents
== POLLERR
) {
2209 ERR("Client socket poll error");
2213 sock
= lttcomm_accept_unix_sock(client_sock
);
2218 /* Allocate context command to process the client request */
2219 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2221 /* Allocate data buffer for reception */
2222 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2223 cmd_ctx
->llm
= NULL
;
2224 cmd_ctx
->session
= NULL
;
2227 * Data is received from the lttng client. The struct
2228 * lttcomm_session_msg (lsm) contains the command and data request of
2231 DBG("Receiving data from client ...");
2232 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2237 // TODO: Validate cmd_ctx including sanity check for security purpose.
2240 * This function dispatch the work to the kernel or userspace tracer
2241 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2242 * informations for the client. The command context struct contains
2243 * everything this function may needs.
2245 ret
= process_client_msg(cmd_ctx
);
2247 /* TODO: Inform client somehow of the fatal error. At this point,
2248 * ret < 0 means that a malloc failed (ENOMEM). */
2249 /* Error detected but still accept command */
2250 clean_command_ctx(&cmd_ctx
);
2254 DBG("Sending response (size: %d, retcode: %d)",
2255 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2256 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2258 ERR("Failed to send data back to client");
2261 clean_command_ctx(&cmd_ctx
);
2263 /* End of transmission */
2268 DBG("Client thread dying");
2276 unlink(client_unix_sock_path
);
2278 clean_command_ctx(&cmd_ctx
);
2284 * usage function on stderr
2286 static void usage(void)
2288 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2289 fprintf(stderr
, " -h, --help Display this usage.\n");
2290 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2291 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2292 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2293 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2294 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2295 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2296 fprintf(stderr
, " -V, --version Show version number.\n");
2297 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2298 fprintf(stderr
, " -q, --quiet No output at all.\n");
2299 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2300 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2304 * daemon argument parsing
2306 static int parse_args(int argc
, char **argv
)
2310 static struct option long_options
[] = {
2311 { "client-sock", 1, 0, 'c' },
2312 { "apps-sock", 1, 0, 'a' },
2313 { "kconsumerd-cmd-sock", 1, 0, 0 },
2314 { "kconsumerd-err-sock", 1, 0, 0 },
2315 { "daemonize", 0, 0, 'd' },
2316 { "sig-parent", 0, 0, 'S' },
2317 { "help", 0, 0, 'h' },
2318 { "group", 1, 0, 'g' },
2319 { "version", 0, 0, 'V' },
2320 { "quiet", 0, 0, 'q' },
2321 { "verbose", 0, 0, 'v' },
2322 { "verbose-kconsumerd", 0, 0, 'Z' },
2327 int option_index
= 0;
2328 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2335 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2337 fprintf(stderr
, " with arg %s\n", optarg
);
2341 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2344 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2350 opt_tracing_group
= strdup(optarg
);
2356 fprintf(stdout
, "%s\n", VERSION
);
2362 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2365 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2371 /* Verbose level can increase using multiple -v */
2375 opt_verbose_kconsumerd
+= 1;
2378 /* Unknown option or other error.
2379 * Error is printed by getopt, just return */
2388 * Creates the two needed socket by the daemon.
2389 * apps_sock - The communication socket for all UST apps.
2390 * client_sock - The communication of the cli tool (lttng).
2392 static int init_daemon_socket(void)
2397 old_umask
= umask(0);
2399 /* Create client tool unix socket */
2400 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2401 if (client_sock
< 0) {
2402 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2407 /* File permission MUST be 660 */
2408 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2410 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2415 /* Create the application unix socket */
2416 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2417 if (apps_sock
< 0) {
2418 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2423 /* File permission MUST be 666 */
2424 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2426 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2437 * Check if the global socket is available, and if a daemon is answering
2438 * at the other side. If yes, error is returned.
2440 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)
2466 gid
= allowed_group();
2469 WARN("No tracing group detected");
2472 ERR("Missing tracing group. Aborting execution.");
2478 /* Set lttng run dir */
2479 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2481 ERR("Unable to set group on " LTTNG_RUNDIR
);
2485 /* lttng client socket path */
2486 ret
= chown(client_unix_sock_path
, 0, gid
);
2488 ERR("Unable to set group on %s", client_unix_sock_path
);
2492 /* kconsumerd error socket path */
2493 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2495 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2499 DBG("All permissions are set");
2506 * Create the pipe used to wake up the kernel thread.
2508 static int create_kernel_poll_pipe(void)
2510 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2514 * Create the lttng run directory needed for all global sockets and pipe.
2516 static int create_lttng_rundir(void)
2520 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2522 if (errno
!= EEXIST
) {
2523 ERR("Unable to create " LTTNG_RUNDIR
);
2535 * Setup sockets and directory needed by the kconsumerd communication with the
2538 static int set_kconsumerd_sockets(void)
2542 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2543 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2546 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2547 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2550 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2552 if (errno
!= EEXIST
) {
2553 ERR("Failed to create " KCONSUMERD_PATH
);
2559 /* Create the kconsumerd error unix socket */
2560 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2561 if (kconsumerd_err_sock
< 0) {
2562 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2567 /* File permission MUST be 660 */
2568 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2570 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2580 * Signal handler for the daemon
2582 * Simply stop all worker threads, leaving main() return gracefully
2583 * after joining all threads and calling cleanup().
2585 static void sighandler(int sig
)
2589 DBG("SIGPIPE catched");
2592 DBG("SIGINT catched");
2596 DBG("SIGTERM catched");
2605 * Setup signal handler for :
2606 * SIGINT, SIGTERM, SIGPIPE
2608 static int set_signal_handler(void)
2611 struct sigaction sa
;
2614 if ((ret
= sigemptyset(&sigset
)) < 0) {
2615 perror("sigemptyset");
2619 sa
.sa_handler
= sighandler
;
2620 sa
.sa_mask
= sigset
;
2622 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2623 perror("sigaction");
2627 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2628 perror("sigaction");
2632 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2633 perror("sigaction");
2637 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2643 * Set open files limit to unlimited. This daemon can open a large number of
2644 * file descriptors in order to consumer multiple kernel traces.
2646 static void set_ulimit(void)
2651 /* The kernel does not allowed an infinite limit for open files */
2652 lim
.rlim_cur
= 65535;
2653 lim
.rlim_max
= 65535;
2655 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2657 perror("failed to set open files limit");
2664 int main(int argc
, char **argv
)
2668 const char *home_path
;
2670 /* Create thread quit pipe */
2671 if ((ret
= init_thread_quit_pipe()) < 0) {
2675 /* Parse arguments */
2677 if ((ret
= parse_args(argc
, argv
) < 0)) {
2690 /* Check if daemon is UID = 0 */
2691 is_root
= !getuid();
2694 ret
= create_lttng_rundir();
2699 if (strlen(apps_unix_sock_path
) == 0) {
2700 snprintf(apps_unix_sock_path
, PATH_MAX
,
2701 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2704 if (strlen(client_unix_sock_path
) == 0) {
2705 snprintf(client_unix_sock_path
, PATH_MAX
,
2706 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2709 home_path
= get_home_dir();
2710 if (home_path
== NULL
) {
2711 /* TODO: Add --socket PATH option */
2712 ERR("Can't get HOME directory for sockets creation.");
2717 if (strlen(apps_unix_sock_path
) == 0) {
2718 snprintf(apps_unix_sock_path
, PATH_MAX
,
2719 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2722 /* Set the cli tool unix socket path */
2723 if (strlen(client_unix_sock_path
) == 0) {
2724 snprintf(client_unix_sock_path
, PATH_MAX
,
2725 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2729 DBG("Client socket path %s", client_unix_sock_path
);
2730 DBG("Application socket path %s", apps_unix_sock_path
);
2733 * See if daemon already exist.
2735 if ((ret
= check_existing_daemon()) < 0) {
2736 ERR("Already running daemon.\n");
2738 * We do not goto exit because we must not cleanup()
2739 * because a daemon is already running.
2744 /* After this point, we can safely call cleanup() so goto error is used */
2747 * These actions must be executed as root. We do that *after* setting up
2748 * the sockets path because we MUST make the check for another daemon using
2749 * those paths *before* trying to set the kernel consumer sockets and init
2753 ret
= set_kconsumerd_sockets();
2758 /* Setup kernel tracer */
2759 init_kernel_tracer();
2761 /* Set ulimit for open files */
2765 if ((ret
= set_signal_handler()) < 0) {
2769 /* Setup the needed unix socket */
2770 if ((ret
= init_daemon_socket()) < 0) {
2774 /* Set credentials to socket */
2775 if (is_root
&& ((ret
= set_permissions()) < 0)) {
2779 /* Get parent pid if -S, --sig-parent is specified. */
2780 if (opt_sig_parent
) {
2784 /* Setup the kernel pipe for waking up the kernel thread */
2785 if ((ret
= create_kernel_poll_pipe()) < 0) {
2790 * Get session list pointer. This pointer MUST NOT be free().
2791 * This list is statically declared in session.c
2793 session_list_ptr
= get_session_list();
2795 /* Create thread to manage the client socket */
2796 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2798 perror("pthread_create");
2802 /* Create thread to manage application socket */
2803 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2805 perror("pthread_create");
2809 /* Create kernel thread to manage kernel event */
2810 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2812 perror("pthread_create");
2816 ret
= pthread_join(kernel_thread
, &status
);
2818 perror("pthread_join");
2819 goto error
; /* join error, exit without cleanup */
2823 ret
= pthread_join(apps_thread
, &status
);
2825 perror("pthread_join");
2826 goto error
; /* join error, exit without cleanup */
2830 ret
= pthread_join(client_thread
, &status
);
2832 perror("pthread_join");
2833 goto error
; /* join error, exit without cleanup */
2836 ret
= join_kconsumerd_thread();
2838 perror("join_kconsumerd");
2839 goto error
; /* join error, exit without cleanup */
2845 * cleanup() is called when no other thread is running.