2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <semaphore.h>
32 #include <sys/mount.h>
34 #include <sys/socket.h>
36 #include <sys/types.h>
38 #include <sys/resource.h>
41 #include <urcu/list.h> /* URCU list library (-lurcu) */
42 #include <lttng/lttng.h>
45 #include "liblttsessiondcomm.h"
46 #include "ltt-sessiond.h"
48 #include "kernel-ctl.h"
51 #include "traceable-app.h"
52 #include "lttng-kconsumerd.h"
56 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
57 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
58 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
59 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
62 int opt_verbose
; /* Not static for lttngerr.h */
63 int opt_verbose_kconsumerd
; /* Not static for lttngerr.h */
64 int opt_quiet
; /* Not static for lttngerr.h */
67 const char *opt_tracing_group
;
68 static int opt_sig_parent
;
69 static int opt_daemon
;
70 static int is_root
; /* Set to 1 if the daemon is running as root */
71 static pid_t ppid
; /* Parent PID for --sig-parent option */
72 static pid_t kconsumerd_pid
;
73 static struct pollfd
*kernel_pollfd
;
75 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
76 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
77 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
78 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
81 static int client_sock
;
83 static int kconsumerd_err_sock
;
84 static int kconsumerd_cmd_sock
;
85 static int kernel_tracer_fd
;
86 static int kernel_poll_pipe
[2];
89 * Quit pipe for all threads. This permits a single cancellation point
90 * for all threads when receiving an event on the pipe.
92 static int thread_quit_pipe
[2];
94 /* Pthread, Mutexes and Semaphores */
95 static pthread_t kconsumerd_thread
;
96 static pthread_t apps_thread
;
97 static pthread_t client_thread
;
98 static pthread_t kernel_thread
;
99 static sem_t kconsumerd_sem
;
101 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
103 static int modprobe_remove_kernel_modules(void);
106 * Pointer initialized before thread creation.
108 * This points to the tracing session list containing the session count and a
109 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
110 * MUST NOT be taken if you call a public function in session.c.
112 * The lock is nested inside the structure: session_list_ptr->lock. Please use
113 * lock_session_list and unlock_session_list for lock acquisition.
115 static struct ltt_session_list
*session_list_ptr
;
120 * Return -1 on error or 0 if all pipes are created.
122 static int init_thread_quit_pipe(void)
126 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
128 perror("thread quit pipe");
137 * Complete teardown of a kernel session. This free all data structure related
138 * to a kernel session and update counter.
140 static void teardown_kernel_session(struct ltt_session
*session
)
142 if (session
->kernel_session
!= NULL
) {
143 DBG("Tearing down kernel session");
144 trace_destroy_kernel_session(session
->kernel_session
);
145 /* Extra precaution */
146 session
->kernel_session
= NULL
;
150 static void stop_threads(void)
152 /* Stopping all threads */
153 DBG("Terminating all threads");
154 close(thread_quit_pipe
[0]);
155 close(thread_quit_pipe
[1]);
161 static void cleanup(void)
165 struct ltt_session
*sess
, *stmp
;
170 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
171 "Matthew, BEET driven development works!%c[%dm",
172 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
175 DBG("Removing %s directory", LTTNG_RUNDIR
);
176 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
178 ERR("asprintf failed. Something is really wrong!");
181 /* Remove lttng run directory */
184 ERR("Unable to clean " LTTNG_RUNDIR
);
187 DBG("Cleaning up all session");
189 /* Destroy session list mutex */
190 if (session_list_ptr
!= NULL
) {
191 pthread_mutex_destroy(&session_list_ptr
->lock
);
193 /* Cleanup ALL session */
194 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
195 teardown_kernel_session(sess
);
196 // TODO complete session cleanup (including UST)
200 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
202 DBG("Closing kernel fd");
203 close(kernel_tracer_fd
);
205 DBG("Unloading kernel modules");
206 modprobe_remove_kernel_modules();
210 * Send data on a unix socket using the liblttsessiondcomm API.
212 * Return lttcomm error code.
214 static int send_unix_sock(int sock
, void *buf
, size_t len
)
216 /* Check valid length */
221 return lttcomm_send_unix_sock(sock
, buf
, len
);
225 * Free memory of a command context structure.
227 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
229 DBG("Clean command context structure");
231 if ((*cmd_ctx
)->llm
) {
232 free((*cmd_ctx
)->llm
);
234 if ((*cmd_ctx
)->lsm
) {
235 free((*cmd_ctx
)->lsm
);
243 * Send all stream fds of kernel channel to the consumer.
245 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
249 struct ltt_kernel_stream
*stream
;
250 struct lttcomm_kconsumerd_header lkh
;
251 struct lttcomm_kconsumerd_msg lkm
;
253 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
255 nb_fd
= channel
->stream_count
;
258 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
259 lkh
.cmd_type
= ADD_STREAM
;
261 DBG("Sending kconsumerd header");
263 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
265 perror("send kconsumerd header");
269 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
270 if (stream
->fd
!= 0) {
272 lkm
.state
= stream
->state
;
273 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
274 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
276 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
278 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
280 perror("send kconsumerd fd");
286 DBG("Kconsumerd channel fds sent");
295 * Send all stream fds of the kernel session to the consumer.
297 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
300 struct ltt_kernel_channel
*chan
;
301 struct lttcomm_kconsumerd_header lkh
;
302 struct lttcomm_kconsumerd_msg lkm
;
305 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
306 lkh
.cmd_type
= ADD_STREAM
;
308 DBG("Sending kconsumerd header for metadata");
310 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
312 perror("send kconsumerd header");
316 DBG("Sending metadata stream fd");
318 if (session
->metadata_stream_fd
!= 0) {
319 /* Send metadata stream fd first */
320 lkm
.fd
= session
->metadata_stream_fd
;
321 lkm
.state
= ACTIVE_FD
;
322 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
323 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
325 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
327 perror("send kconsumerd fd");
332 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
333 ret
= send_kconsumerd_channel_fds(sock
, chan
);
339 DBG("Kconsumerd fds (metadata and channel streams) sent");
349 * Return a socket connected to the libust communication socket of the
350 * application identified by the pid.
352 * If the pid is not found in the traceable list, return -1 to indicate error.
354 static int ust_connect_app(pid_t pid
)
357 struct ltt_traceable_app
*lta
;
359 DBG("Connect to application pid %d", pid
);
361 lta
= find_app_by_pid(pid
);
364 DBG("Application pid %d not found", pid
);
368 sock
= ustctl_connect_pid(lta
->pid
);
370 ERR("Fail connecting to the PID %d", pid
);
375 #endif /* DISABLED */
378 * Notify apps by writing 42 to a named pipe using name. Every applications
379 * waiting for a ltt-sessiond will be notified and re-register automatically to
380 * the session daemon.
382 * Return open or write error value.
384 static int notify_apps(const char *name
)
389 DBG("Notify the global application pipe");
391 /* Try opening the global pipe */
392 fd
= open(name
, O_WRONLY
);
397 /* Notify by writing on the pipe */
398 ret
= write(fd
, "42", 2);
408 * Setup the outgoing data buffer for the response (llm) by allocating the
409 * right amount of memory and copying the original information from the lsm
412 * Return total size of the buffer pointed by buf.
414 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
420 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
421 if (cmd_ctx
->llm
== NULL
) {
427 /* Copy common data */
428 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
429 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
431 cmd_ctx
->llm
->data_size
= size
;
432 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
441 * Update the kernel pollfd set of all channel fd available over all tracing
442 * session. Add the wakeup pipe at the end of the set.
444 static int update_kernel_pollfd(void)
448 * The wakup pipe and the quit pipe are needed so the number of fds starts
449 * at 2 for those pipes.
451 unsigned int nb_fd
= 2;
452 struct ltt_session
*session
;
453 struct ltt_kernel_channel
*channel
;
455 DBG("Updating kernel_pollfd");
457 /* Get the number of channel of all kernel session */
459 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
460 lock_session(session
);
461 if (session
->kernel_session
== NULL
) {
462 unlock_session(session
);
465 nb_fd
+= session
->kernel_session
->channel_count
;
466 unlock_session(session
);
469 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
471 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
472 if (kernel_pollfd
== NULL
) {
473 perror("malloc kernel_pollfd");
477 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
478 lock_session(session
);
479 if (session
->kernel_session
== NULL
) {
480 unlock_session(session
);
484 ERR("To much channel for kernel_pollfd size");
485 unlock_session(session
);
488 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
489 kernel_pollfd
[i
].fd
= channel
->fd
;
490 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
493 unlock_session(session
);
495 unlock_session_list();
497 /* Adding wake up pipe */
498 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
499 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
501 /* Adding the quit pipe */
502 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
507 unlock_session_list();
512 * Find the channel fd from 'fd' over all tracing session. When found, check
513 * for new channel stream and send those stream fds to the kernel consumer.
515 * Useful for CPU hotplug feature.
517 static int update_kernel_stream(int fd
)
520 struct ltt_session
*session
;
521 struct ltt_kernel_channel
*channel
;
523 DBG("Updating kernel streams for channel fd %d", fd
);
526 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
527 lock_session(session
);
528 if (session
->kernel_session
== NULL
) {
529 unlock_session(session
);
532 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
533 if (channel
->fd
== fd
) {
534 DBG("Channel found, updating kernel streams");
535 ret
= kernel_open_channel_stream(channel
);
540 * Have we already sent fds to the consumer? If yes, it means that
541 * tracing is started so it is safe to send our updated stream fds.
543 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
544 ret
= send_kconsumerd_channel_fds(kconsumerd_cmd_sock
, channel
);
552 unlock_session(session
);
556 unlock_session_list();
558 unlock_session(session
);
564 * This thread manage event coming from the kernel.
566 * Features supported in this thread:
569 static void *thread_manage_kernel(void *data
)
571 int ret
, i
, nb_fd
= 0;
573 int update_poll_flag
= 1;
575 DBG("Thread manage kernel started");
578 if (update_poll_flag
== 1) {
579 nb_fd
= update_kernel_pollfd();
583 update_poll_flag
= 0;
586 DBG("Polling on %d fds", nb_fd
);
588 /* Poll infinite value of time */
589 ret
= poll(kernel_pollfd
, nb_fd
, -1);
591 perror("poll kernel thread");
593 } else if (ret
== 0) {
594 /* Should not happen since timeout is infinite */
598 /* Thread quit pipe has been closed. Killing thread. */
599 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
603 DBG("Kernel poll event triggered");
606 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
609 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
611 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
612 update_poll_flag
= 1;
620 for (i
= 0; i
< nb_fd
; i
++) {
621 switch (kernel_pollfd
[i
].revents
) {
623 * New CPU detected by the kernel. Adding kernel stream to kernel
624 * session and updating the kernel consumer
626 case POLLIN
| POLLRDNORM
:
627 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
637 DBG("Kernel thread dying");
642 close(kernel_poll_pipe
[0]);
643 close(kernel_poll_pipe
[1]);
648 * This thread manage the kconsumerd error sent back to the session daemon.
650 static void *thread_manage_kconsumerd(void *data
)
653 enum lttcomm_return_code code
;
654 struct pollfd pollfd
[2];
656 DBG("[thread] Manage kconsumerd started");
658 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
663 /* First fd is always the quit pipe */
664 pollfd
[0].fd
= thread_quit_pipe
[0];
667 pollfd
[1].fd
= kconsumerd_err_sock
;
668 pollfd
[1].events
= POLLIN
;
670 /* Inifinite blocking call, waiting for transmission */
671 ret
= poll(pollfd
, 2, -1);
673 perror("poll kconsumerd thread");
677 /* Thread quit pipe has been closed. Killing thread. */
678 if (pollfd
[0].revents
== POLLNVAL
) {
680 } else if (pollfd
[1].revents
== POLLERR
) {
681 ERR("Kconsumerd err socket poll error");
685 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
690 /* Getting status code from kconsumerd */
691 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
696 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
697 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
698 if (kconsumerd_cmd_sock
< 0) {
699 sem_post(&kconsumerd_sem
);
700 perror("kconsumerd connect");
703 /* Signal condition to tell that the kconsumerd is ready */
704 sem_post(&kconsumerd_sem
);
705 DBG("Kconsumerd command socket ready");
707 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
708 lttcomm_get_readable_code(-code
));
712 /* Wait for any kconsumerd error */
713 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
715 ERR("Kconsumerd closed the command socket");
719 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
722 DBG("Kconsumerd thread dying");
723 if (kconsumerd_err_sock
) {
724 close(kconsumerd_err_sock
);
726 if (kconsumerd_cmd_sock
) {
727 close(kconsumerd_cmd_sock
);
733 unlink(kconsumerd_err_unix_sock_path
);
734 unlink(kconsumerd_cmd_unix_sock_path
);
741 * This thread manage the application socket communication
743 static void *thread_manage_apps(void *data
)
746 struct pollfd pollfd
[2];
748 /* TODO: Something more elegant is needed but fine for now */
749 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
750 * for 32-bit vs 64-bit compat processes. */
751 /* replicate in ust with version number */
753 int reg
; /* 1:register, 0:unregister */
758 DBG("[thread] Manage apps started");
760 ret
= lttcomm_listen_unix_sock(apps_sock
);
765 /* First fd is always the quit pipe */
766 pollfd
[0].fd
= thread_quit_pipe
[0];
769 pollfd
[1].fd
= apps_sock
;
770 pollfd
[1].events
= POLLIN
;
772 /* Notify all applications to register */
773 notify_apps(default_global_apps_pipe
);
776 DBG("Accepting application registration");
778 /* Inifinite blocking call, waiting for transmission */
779 ret
= poll(pollfd
, 2, -1);
781 perror("poll apps thread");
785 /* Thread quit pipe has been closed. Killing thread. */
786 if (pollfd
[0].revents
== POLLNVAL
) {
788 } else if (pollfd
[1].revents
== POLLERR
) {
789 ERR("Apps socket poll error");
793 sock
= lttcomm_accept_unix_sock(apps_sock
);
799 * Basic recv here to handle the very simple data
800 * that the libust send to register (reg_msg).
802 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
808 /* Add application to the global traceable list */
809 if (reg_msg
.reg
== 1) {
811 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
813 /* register_traceable_app only return an error with
814 * ENOMEM. At this point, we better stop everything.
820 unregister_traceable_app(reg_msg
.pid
);
825 DBG("Apps thread dying");
833 unlink(apps_unix_sock_path
);
838 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
839 * exec or it will fails.
841 static int spawn_kconsumerd_thread(void)
845 /* Setup semaphore */
846 sem_init(&kconsumerd_sem
, 0, 0);
848 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
850 perror("pthread_create kconsumerd");
854 /* Wait for the kconsumerd thread to be ready */
855 sem_wait(&kconsumerd_sem
);
857 if (kconsumerd_pid
== 0) {
858 ERR("Kconsumerd did not start");
865 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
869 static int join_kconsumerd_thread(void)
874 if (kconsumerd_pid
!= 0) {
875 ret
= kill(kconsumerd_pid
, SIGTERM
);
877 ERR("Error killing kconsumerd");
880 return pthread_join(kconsumerd_thread
, &status
);
887 * Fork and exec a kernel consumer daemon (kconsumerd).
889 * Return pid if successful else -1.
891 static pid_t
spawn_kconsumerd(void)
895 const char *verbosity
;
897 DBG("Spawning kconsumerd");
904 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
905 verbosity
= "--verbose";
907 verbosity
= "--quiet";
909 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
911 perror("kernel start consumer exec");
914 } else if (pid
> 0) {
918 perror("kernel start consumer fork");
928 * Spawn the kconsumerd daemon and session daemon thread.
930 static int start_kconsumerd(void)
934 pthread_mutex_lock(&kconsumerd_pid_mutex
);
935 if (kconsumerd_pid
!= 0) {
936 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
940 ret
= spawn_kconsumerd();
942 ERR("Spawning kconsumerd failed");
943 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
944 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
948 /* Setting up the global kconsumerd_pid */
949 kconsumerd_pid
= ret
;
950 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
952 DBG("Kconsumerd pid %d", ret
);
954 DBG("Spawning kconsumerd thread");
955 ret
= spawn_kconsumerd_thread();
957 ERR("Fatal error spawning kconsumerd thread");
969 * modprobe_kernel_modules
971 static int modprobe_kernel_modules(void)
976 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
977 ret
= snprintf(modprobe
, sizeof(modprobe
),
978 "/sbin/modprobe %s%s",
979 kernel_modules_list
[i
].required
? "" : "--quiet ",
980 kernel_modules_list
[i
].name
);
982 perror("snprintf modprobe");
985 modprobe
[sizeof(modprobe
) - 1] = '\0';
986 ret
= system(modprobe
);
988 ERR("Unable to launch modprobe for module %s",
989 kernel_modules_list
[i
].name
);
990 } else if (kernel_modules_list
[i
].required
991 && WEXITSTATUS(ret
) != 0) {
992 ERR("Unable to load module %s",
993 kernel_modules_list
[i
].name
);
995 DBG("Modprobe successfully %s",
996 kernel_modules_list
[i
].name
);
1005 * modprobe_remove_kernel_modules
1006 * Remove modules in reverse load order.
1008 static int modprobe_remove_kernel_modules(void)
1013 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
1014 ret
= snprintf(modprobe
, sizeof(modprobe
),
1015 "/sbin/modprobe --remove --quiet %s",
1016 kernel_modules_list
[i
].name
);
1018 perror("snprintf modprobe --remove");
1021 modprobe
[sizeof(modprobe
) - 1] = '\0';
1022 ret
= system(modprobe
);
1024 ERR("Unable to launch modprobe --remove for module %s",
1025 kernel_modules_list
[i
].name
);
1026 } else if (kernel_modules_list
[i
].required
1027 && WEXITSTATUS(ret
) != 0) {
1028 ERR("Unable to remove module %s",
1029 kernel_modules_list
[i
].name
);
1031 DBG("Modprobe removal successful %s",
1032 kernel_modules_list
[i
].name
);
1043 static int mount_debugfs(char *path
)
1046 char *type
= "debugfs";
1048 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
);
1053 ret
= mount(type
, path
, type
, 0, NULL
);
1055 perror("mount debugfs");
1059 DBG("Mounted debugfs successfully at %s", path
);
1066 * Setup necessary data for kernel tracer action.
1068 static void init_kernel_tracer(void)
1071 char *proc_mounts
= "/proc/mounts";
1073 char *debugfs_path
= NULL
, *lttng_path
;
1076 /* Detect debugfs */
1077 fp
= fopen(proc_mounts
, "r");
1079 ERR("Unable to probe %s", proc_mounts
);
1083 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1084 if (strstr(line
, "debugfs") != NULL
) {
1085 /* Remove first string */
1087 /* Dup string here so we can reuse line later on */
1088 debugfs_path
= strdup(strtok(NULL
, " "));
1089 DBG("Got debugfs path : %s", debugfs_path
);
1096 /* Mount debugfs if needded */
1097 if (debugfs_path
== NULL
) {
1098 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1100 perror("asprintf debugfs path");
1103 ret
= mount_debugfs(debugfs_path
);
1109 /* Modprobe lttng kernel modules */
1110 ret
= modprobe_kernel_modules();
1115 /* Setup lttng kernel path */
1116 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1118 perror("asprintf lttng path");
1122 /* Open debugfs lttng */
1123 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1124 if (kernel_tracer_fd
< 0) {
1125 DBG("Failed to open %s", lttng_path
);
1131 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1141 WARN("No kernel tracer available");
1142 kernel_tracer_fd
= 0;
1147 * Start tracing by creating trace directory and sending FDs to the kernel
1150 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1154 if (session
->kconsumer_fds_sent
== 0) {
1155 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
1157 ERR("Send kconsumerd fds failed");
1158 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1162 session
->kconsumer_fds_sent
= 1;
1170 * Notify kernel thread to update it's pollfd.
1172 static int notify_kernel_pollfd(void)
1176 /* Inform kernel thread of the new kernel channel */
1177 ret
= write(kernel_poll_pipe
[1], "!", 1);
1179 perror("write kernel poll pipe");
1186 * Allocate a channel structure and fill it.
1188 static struct lttng_channel
*init_default_channel(char *name
)
1190 struct lttng_channel
*chan
;
1192 chan
= malloc(sizeof(struct lttng_channel
));
1194 perror("init channel malloc");
1198 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1199 perror("snprintf channel name");
1203 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1204 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
1205 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
1206 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1207 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1208 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1215 * Create a kernel tracer session then create the default channel.
1217 static int create_kernel_session(struct ltt_session
*session
)
1221 DBG("Creating kernel session");
1223 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1225 ret
= LTTCOMM_KERN_SESS_FAIL
;
1229 ret
= mkdir_recursive(session
->path
, S_IRWXU
| S_IRWXG
);
1231 if (ret
!= EEXIST
) {
1232 ERR("Trace directory creation error");
1242 * Using the session list, filled a lttng_session array to send back to the
1243 * client for session listing.
1245 * The session list lock MUST be acquired before calling this function. Use
1246 * lock_session_list() and unlock_session_list().
1248 static void list_lttng_sessions(struct lttng_session
*sessions
)
1251 struct ltt_session
*session
;
1253 DBG("Getting all available session");
1255 * Iterate over session list and append data after the control struct in
1258 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1259 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1260 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1266 * Fill lttng_channel array of all channels.
1268 static void list_lttng_channels(struct ltt_session
*session
,
1269 struct lttng_channel
*channels
)
1272 struct ltt_kernel_channel
*kchan
;
1274 DBG("Listing channels for session %s", session
->name
);
1276 /* Kernel channels */
1277 if (session
->kernel_session
!= NULL
) {
1278 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1279 /* Copy lttng_channel struct to array */
1280 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1281 channels
[i
].enabled
= kchan
->enabled
;
1286 /* TODO: Missing UST listing */
1290 * Fill lttng_event array of all events in the channel.
1292 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1293 struct lttng_event
*events
)
1296 * TODO: This is ONLY kernel. Need UST support.
1299 struct ltt_kernel_event
*event
;
1301 DBG("Listing events for channel %s", kchan
->channel
->name
);
1303 /* Kernel channels */
1304 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1305 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1306 events
[i
].enabled
= event
->enabled
;
1307 switch (event
->event
->instrumentation
) {
1308 case LTTNG_KERNEL_TRACEPOINT
:
1309 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1311 case LTTNG_KERNEL_KPROBE
:
1312 case LTTNG_KERNEL_KRETPROBE
:
1313 events
[i
].type
= LTTNG_EVENT_PROBE
;
1314 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1315 sizeof(struct lttng_kernel_kprobe
));
1317 case LTTNG_KERNEL_FUNCTION
:
1318 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1319 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1320 sizeof(struct lttng_kernel_function
));
1328 * Process the command requested by the lttng client within the command
1329 * context structure. This function make sure that the return structure (llm)
1330 * is set and ready for transmission before returning.
1332 * Return any error encountered or 0 for success.
1334 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1336 int ret
= LTTCOMM_OK
;
1338 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1340 /* Listing commands don't need a session */
1341 switch (cmd_ctx
->lsm
->cmd_type
) {
1342 case LTTNG_CREATE_SESSION
:
1343 case LTTNG_LIST_SESSIONS
:
1344 case LTTNG_LIST_TRACEPOINTS
:
1345 case LTTNG_CALIBRATE
:
1348 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1349 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1350 if (cmd_ctx
->session
== NULL
) {
1351 /* If session name not found */
1352 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1353 ret
= LTTCOMM_SESS_NOT_FOUND
;
1354 } else { /* If no session name specified */
1355 ret
= LTTCOMM_SELECT_SESS
;
1359 /* Acquire lock for the session */
1360 lock_session(cmd_ctx
->session
);
1366 * Check domain type for specific "pre-action".
1368 switch (cmd_ctx
->lsm
->domain
.type
) {
1369 case LTTNG_DOMAIN_KERNEL
:
1370 /* Kernel tracer check */
1371 if (kernel_tracer_fd
== 0) {
1372 init_kernel_tracer();
1373 if (kernel_tracer_fd
== 0) {
1374 ret
= LTTCOMM_KERN_NA
;
1379 /* Need a session for kernel command */
1380 switch (cmd_ctx
->lsm
->cmd_type
) {
1381 case LTTNG_CREATE_SESSION
:
1382 case LTTNG_LIST_SESSIONS
:
1383 case LTTNG_LIST_TRACEPOINTS
:
1384 case LTTNG_CALIBRATE
:
1387 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1388 ret
= create_kernel_session(cmd_ctx
->session
);
1390 ret
= LTTCOMM_KERN_SESS_FAIL
;
1394 /* Start the kernel consumer daemon */
1395 if (kconsumerd_pid
== 0) {
1396 ret
= start_kconsumerd();
1408 /* Process by command type */
1409 switch (cmd_ctx
->lsm
->cmd_type
) {
1410 case LTTNG_ADD_CONTEXT
:
1412 struct lttng_kernel_context kctx
;
1414 /* Setup lttng message with no payload */
1415 ret
= setup_lttng_msg(cmd_ctx
, 0);
1420 switch (cmd_ctx
->lsm
->domain
.type
) {
1421 case LTTNG_DOMAIN_KERNEL
:
1422 /* Create Kernel context */
1423 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1424 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1425 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1426 strncpy(kctx
.u
.perf_counter
.name
,
1427 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1428 LTTNG_SYMBOL_NAME_LEN
);
1430 /* Add kernel context to kernel tracer. See context.c */
1431 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1432 cmd_ctx
->lsm
->u
.context
.event_name
,
1433 cmd_ctx
->lsm
->u
.context
.channel_name
);
1434 if (ret
!= LTTCOMM_OK
) {
1439 /* TODO: Userspace tracing */
1440 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1447 case LTTNG_DISABLE_CHANNEL
:
1449 struct ltt_kernel_channel
*kchan
;
1451 /* Setup lttng message with no payload */
1452 ret
= setup_lttng_msg(cmd_ctx
, 0);
1457 switch (cmd_ctx
->lsm
->domain
.type
) {
1458 case LTTNG_DOMAIN_KERNEL
:
1459 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1460 cmd_ctx
->session
->kernel_session
);
1461 if (kchan
== NULL
) {
1462 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1464 } else if (kchan
->enabled
== 1) {
1465 ret
= kernel_disable_channel(kchan
);
1467 if (ret
!= EEXIST
) {
1468 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1473 kernel_wait_quiescent(kernel_tracer_fd
);
1476 /* TODO: Userspace tracing */
1477 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1484 case LTTNG_DISABLE_EVENT
:
1486 struct ltt_kernel_channel
*kchan
;
1487 struct ltt_kernel_event
*kevent
;
1489 /* Setup lttng message with no payload */
1490 ret
= setup_lttng_msg(cmd_ctx
, 0);
1495 switch (cmd_ctx
->lsm
->domain
.type
) {
1496 case LTTNG_DOMAIN_KERNEL
:
1497 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1498 cmd_ctx
->session
->kernel_session
);
1499 if (kchan
== NULL
) {
1500 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1504 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1505 if (kevent
!= NULL
) {
1506 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1507 kchan
->channel
->name
);
1508 ret
= kernel_disable_event(kevent
);
1510 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1515 kernel_wait_quiescent(kernel_tracer_fd
);
1518 /* TODO: Userspace tracing */
1519 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1526 case LTTNG_DISABLE_ALL_EVENT
:
1528 struct ltt_kernel_channel
*kchan
;
1529 struct ltt_kernel_event
*kevent
;
1531 /* Setup lttng message with no payload */
1532 ret
= setup_lttng_msg(cmd_ctx
, 0);
1537 switch (cmd_ctx
->lsm
->domain
.type
) {
1538 case LTTNG_DOMAIN_KERNEL
:
1539 DBG("Disabling all enabled kernel events");
1540 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1541 cmd_ctx
->session
->kernel_session
);
1542 if (kchan
== NULL
) {
1543 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1547 /* For each event in the kernel session */
1548 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1549 DBG("Disabling kernel event %s for channel %s.",
1550 kevent
->event
->name
, kchan
->channel
->name
);
1551 ret
= kernel_disable_event(kevent
);
1557 /* Quiescent wait after event disable */
1558 kernel_wait_quiescent(kernel_tracer_fd
);
1561 /* TODO: Userspace tracing */
1562 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1569 case LTTNG_ENABLE_CHANNEL
:
1571 struct ltt_kernel_channel
*kchan
;
1573 /* Setup lttng message with no payload */
1574 ret
= setup_lttng_msg(cmd_ctx
, 0);
1579 switch (cmd_ctx
->lsm
->domain
.type
) {
1580 case LTTNG_DOMAIN_KERNEL
:
1581 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1582 cmd_ctx
->session
->kernel_session
);
1583 if (kchan
== NULL
) {
1584 /* Channel not found, creating it */
1585 DBG("Creating kernel channel");
1587 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1588 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1590 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1594 /* Notify kernel thread that there is a new channel */
1595 ret
= notify_kernel_pollfd();
1597 ret
= LTTCOMM_FATAL
;
1600 } else if (kchan
->enabled
== 0) {
1601 ret
= kernel_enable_channel(kchan
);
1603 if (ret
!= EEXIST
) {
1604 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1610 kernel_wait_quiescent(kernel_tracer_fd
);
1613 /* TODO: Userspace tracing */
1614 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1621 case LTTNG_ENABLE_EVENT
:
1624 struct ltt_kernel_channel
*kchan
;
1625 struct ltt_kernel_event
*kevent
;
1626 struct lttng_channel
*chan
;
1628 /* Setup lttng message with no payload */
1629 ret
= setup_lttng_msg(cmd_ctx
, 0);
1634 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1636 switch (cmd_ctx
->lsm
->domain
.type
) {
1637 case LTTNG_DOMAIN_KERNEL
:
1639 kchan
= get_kernel_channel_by_name(channel_name
,
1640 cmd_ctx
->session
->kernel_session
);
1641 if (kchan
== NULL
) {
1642 DBG("Channel not found. Creating channel %s", channel_name
);
1644 chan
= init_default_channel(channel_name
);
1646 ret
= LTTCOMM_FATAL
;
1650 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1651 chan
, cmd_ctx
->session
->path
);
1653 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1657 } while (kchan
== NULL
);
1659 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
1660 if (kevent
== NULL
) {
1661 DBG("Creating kernel event %s for channel %s.",
1662 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
1663 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
1665 DBG("Enabling kernel event %s for channel %s.",
1666 kevent
->event
->name
, channel_name
);
1667 ret
= kernel_enable_event(kevent
);
1668 if (ret
== -EEXIST
) {
1669 ret
= LTTCOMM_KERN_EVENT_EXIST
;
1675 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1679 kernel_wait_quiescent(kernel_tracer_fd
);
1682 /* TODO: Userspace tracing */
1683 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1689 case LTTNG_ENABLE_ALL_EVENT
:
1693 struct ltt_kernel_channel
*kchan
;
1694 struct ltt_kernel_event
*kevent
;
1695 struct lttng_event
*event_list
;
1696 struct lttng_channel
*chan
;
1698 /* Setup lttng message with no payload */
1699 ret
= setup_lttng_msg(cmd_ctx
, 0);
1704 DBG("Enabling all kernel event");
1706 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1708 switch (cmd_ctx
->lsm
->domain
.type
) {
1709 case LTTNG_DOMAIN_KERNEL
:
1711 kchan
= get_kernel_channel_by_name(channel_name
,
1712 cmd_ctx
->session
->kernel_session
);
1713 if (kchan
== NULL
) {
1714 DBG("Channel not found. Creating channel %s", channel_name
);
1716 chan
= init_default_channel(channel_name
);
1718 ret
= LTTCOMM_FATAL
;
1722 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1723 chan
, cmd_ctx
->session
->path
);
1725 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1729 } while (kchan
== NULL
);
1731 /* For each event in the kernel session */
1732 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1733 DBG("Enabling kernel event %s for channel %s.",
1734 kevent
->event
->name
, channel_name
);
1735 ret
= kernel_enable_event(kevent
);
1741 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1743 ret
= LTTCOMM_KERN_LIST_FAIL
;
1747 for (i
= 0; i
< size
; i
++) {
1748 kevent
= get_kernel_event_by_name(event_list
[i
].name
, kchan
);
1749 if (kevent
== NULL
) {
1750 /* Default event type for enable all */
1751 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1752 /* Enable each single tracepoint event */
1753 ret
= kernel_create_event(&event_list
[i
], kchan
);
1755 /* Ignore error here and continue */
1762 /* Quiescent wait after event enable */
1763 kernel_wait_quiescent(kernel_tracer_fd
);
1766 /* TODO: Userspace tracing */
1767 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1774 case LTTNG_LIST_TRACEPOINTS
:
1776 struct lttng_event
*events
;
1777 ssize_t nb_events
= 0;
1779 switch (cmd_ctx
->lsm
->domain
.type
) {
1780 case LTTNG_DOMAIN_KERNEL
:
1781 DBG("Listing kernel events");
1782 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
1783 if (nb_events
< 0) {
1784 ret
= LTTCOMM_KERN_LIST_FAIL
;
1789 /* TODO: Userspace listing */
1790 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1795 * Setup lttng message with payload size set to the event list size in
1796 * bytes and then copy list into the llm payload.
1798 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
1804 /* Copy event list into message payload */
1805 memcpy(cmd_ctx
->llm
->payload
, events
,
1806 sizeof(struct lttng_event
) * nb_events
);
1813 case LTTNG_START_TRACE
:
1815 struct ltt_kernel_channel
*chan
;
1817 /* Setup lttng message with no payload */
1818 ret
= setup_lttng_msg(cmd_ctx
, 0);
1823 /* Kernel tracing */
1824 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1825 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1826 DBG("Open kernel metadata");
1827 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1828 cmd_ctx
->session
->path
);
1830 ret
= LTTCOMM_KERN_META_FAIL
;
1835 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1836 DBG("Opening kernel metadata stream");
1837 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1838 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1840 ERR("Kernel create metadata stream failed");
1841 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1847 /* For each channel */
1848 cds_list_for_each_entry(chan
,
1849 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1850 if (chan
->stream_count
== 0) {
1851 ret
= kernel_open_channel_stream(chan
);
1853 ERR("Kernel create channel stream failed");
1854 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1857 /* Update the stream global counter */
1858 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1862 DBG("Start kernel tracing");
1863 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1865 ERR("Kernel start session failed");
1866 ret
= LTTCOMM_KERN_START_FAIL
;
1870 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1872 ret
= LTTCOMM_KERN_START_FAIL
;
1876 /* Quiescent wait after starting trace */
1877 kernel_wait_quiescent(kernel_tracer_fd
);
1880 /* TODO: Start all UST traces */
1885 case LTTNG_STOP_TRACE
:
1887 struct ltt_kernel_channel
*chan
;
1888 /* Setup lttng message with no payload */
1889 ret
= setup_lttng_msg(cmd_ctx
, 0);
1895 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1896 DBG("Stop kernel tracing");
1898 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1900 ERR("Kernel metadata flush failed");
1903 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1904 ret
= kernel_flush_buffer(chan
);
1906 ERR("Kernel flush buffer error");
1910 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1912 ERR("Kernel stop session failed");
1913 ret
= LTTCOMM_KERN_STOP_FAIL
;
1917 /* Quiescent wait after stopping trace */
1918 kernel_wait_quiescent(kernel_tracer_fd
);
1921 /* TODO : User-space tracer */
1926 case LTTNG_CREATE_SESSION
:
1928 /* Setup lttng message with no payload */
1929 ret
= setup_lttng_msg(cmd_ctx
, 0);
1934 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
1936 if (ret
== -EEXIST
) {
1937 ret
= LTTCOMM_EXIST_SESS
;
1939 ret
= LTTCOMM_FATAL
;
1947 case LTTNG_DESTROY_SESSION
:
1949 /* Setup lttng message with no payload */
1950 ret
= setup_lttng_msg(cmd_ctx
, 0);
1955 /* Clean kernel session teardown */
1956 teardown_kernel_session(cmd_ctx
->session
);
1958 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
1960 ret
= LTTCOMM_FATAL
;
1965 * Must notify the kernel thread here to update it's pollfd in order to
1966 * remove the channel(s)' fd just destroyed.
1968 ret
= notify_kernel_pollfd();
1970 ret
= LTTCOMM_FATAL
;
1977 case LTTNG_LIST_DOMAINS
:
1981 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1985 nb_dom
+= cmd_ctx
->session
->ust_trace_count
;
1987 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
1992 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
1993 LTTNG_DOMAIN_KERNEL
;
1995 /* TODO: User-space tracer domain support */
1999 case LTTNG_LIST_CHANNELS
:
2002 * TODO: Only kernel channels are listed here. UST listing
2003 * is needed on lttng-ust 2.0 release.
2006 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2007 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2010 ret
= setup_lttng_msg(cmd_ctx
,
2011 sizeof(struct lttng_channel
) * nb_chan
);
2016 list_lttng_channels(cmd_ctx
->session
,
2017 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2022 case LTTNG_LIST_EVENTS
:
2025 * TODO: Only kernel events are listed here. UST listing
2026 * is needed on lttng-ust 2.0 release.
2028 size_t nb_event
= 0;
2029 struct ltt_kernel_channel
*kchan
= NULL
;
2031 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2032 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2033 cmd_ctx
->session
->kernel_session
);
2034 if (kchan
== NULL
) {
2035 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2038 nb_event
+= kchan
->event_count
;
2041 ret
= setup_lttng_msg(cmd_ctx
,
2042 sizeof(struct lttng_event
) * nb_event
);
2047 DBG("Listing events (%ld events)", nb_event
);
2049 list_lttng_events(kchan
,
2050 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2055 case LTTNG_LIST_SESSIONS
:
2057 lock_session_list();
2059 if (session_list_ptr
->count
== 0) {
2060 ret
= LTTCOMM_NO_SESSION
;
2061 unlock_session_list();
2065 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2066 session_list_ptr
->count
);
2068 unlock_session_list();
2072 /* Filled the session array */
2073 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2075 unlock_session_list();
2081 case LTTNG_CALIBRATE
:
2083 /* Setup lttng message with no payload */
2084 ret
= setup_lttng_msg(cmd_ctx
, 0);
2089 switch (cmd_ctx
->lsm
->domain
.type
) {
2090 case LTTNG_DOMAIN_KERNEL
:
2092 struct lttng_kernel_calibrate kcalibrate
;
2094 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2095 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2097 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2103 /* TODO: Userspace tracing */
2104 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2112 /* Undefined command */
2113 ret
= setup_lttng_msg(cmd_ctx
, 0);
2122 /* Set return code */
2123 cmd_ctx
->llm
->ret_code
= ret
;
2125 if (cmd_ctx
->session
) {
2126 unlock_session(cmd_ctx
->session
);
2132 if (cmd_ctx
->llm
== NULL
) {
2133 DBG("Missing llm structure. Allocating one.");
2134 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2138 /* Notify client of error */
2139 cmd_ctx
->llm
->ret_code
= ret
;
2142 if (cmd_ctx
->session
) {
2143 unlock_session(cmd_ctx
->session
);
2149 * This thread manage all clients request using the unix client socket for
2152 static void *thread_manage_clients(void *data
)
2155 struct command_ctx
*cmd_ctx
= NULL
;
2156 struct pollfd pollfd
[2];
2158 DBG("[thread] Manage client started");
2160 ret
= lttcomm_listen_unix_sock(client_sock
);
2165 /* First fd is always the quit pipe */
2166 pollfd
[0].fd
= thread_quit_pipe
[0];
2169 pollfd
[1].fd
= client_sock
;
2170 pollfd
[1].events
= POLLIN
;
2172 /* Notify parent pid that we are ready
2173 * to accept command for client side.
2175 if (opt_sig_parent
) {
2176 kill(ppid
, SIGCHLD
);
2180 DBG("Accepting client command ...");
2182 /* Inifinite blocking call, waiting for transmission */
2183 ret
= poll(pollfd
, 2, -1);
2185 perror("poll client thread");
2189 /* Thread quit pipe has been closed. Killing thread. */
2190 if (pollfd
[0].revents
== POLLNVAL
) {
2192 } else if (pollfd
[1].revents
== POLLERR
) {
2193 ERR("Client socket poll error");
2197 sock
= lttcomm_accept_unix_sock(client_sock
);
2202 /* Allocate context command to process the client request */
2203 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2205 /* Allocate data buffer for reception */
2206 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2207 cmd_ctx
->llm
= NULL
;
2208 cmd_ctx
->session
= NULL
;
2211 * Data is received from the lttng client. The struct
2212 * lttcomm_session_msg (lsm) contains the command and data request of
2215 DBG("Receiving data from client ...");
2216 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2221 // TODO: Validate cmd_ctx including sanity check for security purpose.
2224 * This function dispatch the work to the kernel or userspace tracer
2225 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2226 * informations for the client. The command context struct contains
2227 * everything this function may needs.
2229 ret
= process_client_msg(cmd_ctx
);
2231 /* TODO: Inform client somehow of the fatal error. At this point,
2232 * ret < 0 means that a malloc failed (ENOMEM). */
2233 /* Error detected but still accept command */
2234 clean_command_ctx(&cmd_ctx
);
2238 DBG("Sending response (size: %d, retcode: %d)",
2239 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2240 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2242 ERR("Failed to send data back to client");
2245 clean_command_ctx(&cmd_ctx
);
2247 /* End of transmission */
2252 DBG("Client thread dying");
2260 unlink(client_unix_sock_path
);
2262 clean_command_ctx(&cmd_ctx
);
2268 * usage function on stderr
2270 static void usage(void)
2272 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2273 fprintf(stderr
, " -h, --help Display this usage.\n");
2274 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2275 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2276 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2277 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2278 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2279 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2280 fprintf(stderr
, " -V, --version Show version number.\n");
2281 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2282 fprintf(stderr
, " -q, --quiet No output at all.\n");
2283 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2284 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2288 * daemon argument parsing
2290 static int parse_args(int argc
, char **argv
)
2294 static struct option long_options
[] = {
2295 { "client-sock", 1, 0, 'c' },
2296 { "apps-sock", 1, 0, 'a' },
2297 { "kconsumerd-cmd-sock", 1, 0, 0 },
2298 { "kconsumerd-err-sock", 1, 0, 0 },
2299 { "daemonize", 0, 0, 'd' },
2300 { "sig-parent", 0, 0, 'S' },
2301 { "help", 0, 0, 'h' },
2302 { "group", 1, 0, 'g' },
2303 { "version", 0, 0, 'V' },
2304 { "quiet", 0, 0, 'q' },
2305 { "verbose", 0, 0, 'v' },
2306 { "verbose-kconsumerd", 0, 0, 'Z' },
2311 int option_index
= 0;
2312 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2319 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2321 fprintf(stderr
, " with arg %s\n", optarg
);
2325 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2328 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2334 opt_tracing_group
= strdup(optarg
);
2340 fprintf(stdout
, "%s\n", VERSION
);
2346 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2349 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2355 /* Verbose level can increase using multiple -v */
2359 opt_verbose_kconsumerd
+= 1;
2362 /* Unknown option or other error.
2363 * Error is printed by getopt, just return */
2372 * Creates the two needed socket by the daemon.
2373 * apps_sock - The communication socket for all UST apps.
2374 * client_sock - The communication of the cli tool (lttng).
2376 static int init_daemon_socket(void)
2381 old_umask
= umask(0);
2383 /* Create client tool unix socket */
2384 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2385 if (client_sock
< 0) {
2386 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2391 /* File permission MUST be 660 */
2392 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2394 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2399 /* Create the application unix socket */
2400 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2401 if (apps_sock
< 0) {
2402 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2407 /* File permission MUST be 666 */
2408 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2410 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2421 * Check if the global socket is available. If yes, error is returned.
2423 static int check_existing_daemon(void)
2427 ret
= access(client_unix_sock_path
, F_OK
);
2429 ret
= access(apps_unix_sock_path
, F_OK
);
2436 * Set the tracing group gid onto the client socket.
2438 * Race window between mkdir and chown is OK because we are going from more
2439 * permissive (root.root) to les permissive (root.tracing).
2441 static int set_permissions(void)
2446 /* Decide which group name to use */
2447 (opt_tracing_group
!= NULL
) ?
2448 (grp
= getgrnam(opt_tracing_group
)) :
2449 (grp
= getgrnam(default_tracing_group
));
2453 WARN("No tracing group detected");
2456 ERR("Missing tracing group. Aborting execution.");
2462 /* Set lttng run dir */
2463 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
2465 ERR("Unable to set group on " LTTNG_RUNDIR
);
2469 /* lttng client socket path */
2470 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
2472 ERR("Unable to set group on %s", client_unix_sock_path
);
2476 /* kconsumerd error socket path */
2477 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
2479 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2483 DBG("All permissions are set");
2490 * Create the pipe used to wake up the kernel thread.
2492 static int create_kernel_poll_pipe(void)
2494 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2498 * Create the lttng run directory needed for all global sockets and pipe.
2500 static int create_lttng_rundir(void)
2504 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2506 if (errno
!= EEXIST
) {
2507 ERR("Unable to create " LTTNG_RUNDIR
);
2519 * Setup sockets and directory needed by the kconsumerd communication with the
2522 static int set_kconsumerd_sockets(void)
2526 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2527 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2530 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2531 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2534 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2536 if (errno
!= EEXIST
) {
2537 ERR("Failed to create " KCONSUMERD_PATH
);
2543 /* Create the kconsumerd error unix socket */
2544 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2545 if (kconsumerd_err_sock
< 0) {
2546 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2551 /* File permission MUST be 660 */
2552 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2554 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2564 * Signal handler for the daemon
2566 * Simply stop all worker threads, leaving main() return gracefully
2567 * after joining all threads and calling cleanup().
2569 static void sighandler(int sig
)
2573 DBG("SIGPIPE catched");
2576 DBG("SIGINT catched");
2580 DBG("SIGTERM catched");
2589 * Setup signal handler for :
2590 * SIGINT, SIGTERM, SIGPIPE
2592 static int set_signal_handler(void)
2595 struct sigaction sa
;
2598 if ((ret
= sigemptyset(&sigset
)) < 0) {
2599 perror("sigemptyset");
2603 sa
.sa_handler
= sighandler
;
2604 sa
.sa_mask
= sigset
;
2606 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2607 perror("sigaction");
2611 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2612 perror("sigaction");
2616 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2617 perror("sigaction");
2621 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2627 * Set open files limit to unlimited. This daemon can open a large number of
2628 * file descriptors in order to consumer multiple kernel traces.
2630 static void set_ulimit(void)
2635 /* The kernel does not allowed an infinite limit for open files */
2636 lim
.rlim_cur
= 65535;
2637 lim
.rlim_max
= 65535;
2639 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2641 perror("failed to set open files limit");
2648 int main(int argc
, char **argv
)
2652 const char *home_path
;
2654 /* Create thread quit pipe */
2655 if ((ret
= init_thread_quit_pipe()) < 0) {
2659 /* Parse arguments */
2661 if ((ret
= parse_args(argc
, argv
) < 0)) {
2674 /* Check if daemon is UID = 0 */
2675 is_root
= !getuid();
2678 ret
= create_lttng_rundir();
2683 if (strlen(apps_unix_sock_path
) == 0) {
2684 snprintf(apps_unix_sock_path
, PATH_MAX
,
2685 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2688 if (strlen(client_unix_sock_path
) == 0) {
2689 snprintf(client_unix_sock_path
, PATH_MAX
,
2690 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2693 home_path
= get_home_dir();
2694 if (home_path
== NULL
) {
2695 /* TODO: Add --socket PATH option */
2696 ERR("Can't get HOME directory for sockets creation.");
2701 if (strlen(apps_unix_sock_path
) == 0) {
2702 snprintf(apps_unix_sock_path
, PATH_MAX
,
2703 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2706 /* Set the cli tool unix socket path */
2707 if (strlen(client_unix_sock_path
) == 0) {
2708 snprintf(client_unix_sock_path
, PATH_MAX
,
2709 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2713 DBG("Client socket path %s", client_unix_sock_path
);
2714 DBG("Application socket path %s", apps_unix_sock_path
);
2717 * See if daemon already exist. If any of the two socket needed by the
2718 * daemon are present, this test fails. However, if the daemon is killed
2719 * with a SIGKILL, those unix socket must be unlinked by hand.
2721 if ((ret
= check_existing_daemon()) == 0) {
2722 ERR("Already running daemon.\n");
2724 * We do not goto exit because we must not cleanup()
2725 * because a daemon is already running.
2730 /* After this point, we can safely call cleanup() so goto error is used */
2733 * These actions must be executed as root. We do that *after* setting up
2734 * the sockets path because we MUST make the check for another daemon using
2735 * those paths *before* trying to set the kernel consumer sockets and init
2739 ret
= set_kconsumerd_sockets();
2744 /* Setup kernel tracer */
2745 init_kernel_tracer();
2747 /* Set ulimit for open files */
2751 if ((ret
= set_signal_handler()) < 0) {
2755 /* Setup the needed unix socket */
2756 if ((ret
= init_daemon_socket()) < 0) {
2760 /* Set credentials to socket */
2761 if (is_root
&& ((ret
= set_permissions()) < 0)) {
2765 /* Get parent pid if -S, --sig-parent is specified. */
2766 if (opt_sig_parent
) {
2770 /* Setup the kernel pipe for waking up the kernel thread */
2771 if ((ret
= create_kernel_poll_pipe()) < 0) {
2776 * Get session list pointer. This pointer MUST NOT be free().
2777 * This list is statically declared in session.c
2779 session_list_ptr
= get_session_list();
2781 /* Create thread to manage the client socket */
2782 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2784 perror("pthread_create");
2788 /* Create thread to manage application socket */
2789 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2791 perror("pthread_create");
2795 /* Create kernel thread to manage kernel event */
2796 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2798 perror("pthread_create");
2802 ret
= pthread_join(kernel_thread
, &status
);
2804 perror("pthread_join");
2805 goto error
; /* join error, exit without cleanup */
2809 ret
= pthread_join(apps_thread
, &status
);
2811 perror("pthread_join");
2812 goto error
; /* join error, exit without cleanup */
2816 ret
= pthread_join(client_thread
, &status
);
2818 perror("pthread_join");
2819 goto error
; /* join error, exit without cleanup */
2822 ret
= join_kconsumerd_thread();
2824 perror("join_kconsumerd");
2825 goto error
; /* join error, exit without cleanup */
2831 * cleanup() is called when no other thread is running.