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_quiet
; /* Not static for lttngerr.h */
66 const char *opt_tracing_group
;
67 static int opt_sig_parent
;
68 static int opt_daemon
;
69 static int is_root
; /* Set to 1 if the daemon is running as root */
70 static pid_t ppid
; /* Parent PID for --sig-parent option */
71 static pid_t kconsumerd_pid
;
72 static struct pollfd
*kernel_pollfd
;
74 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
75 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
76 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
77 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
80 static int client_sock
;
82 static int kconsumerd_err_sock
;
83 static int kconsumerd_cmd_sock
;
84 static int kernel_tracer_fd
;
85 static int kernel_poll_pipe
[2];
88 * Quit pipe for all threads. This permits a single cancellation point
89 * for all threads when receiving an event on the pipe.
91 static int thread_quit_pipe
[2];
93 /* Pthread, Mutexes and Semaphores */
94 static pthread_t kconsumerd_thread
;
95 static pthread_t apps_thread
;
96 static pthread_t client_thread
;
97 static pthread_t kernel_thread
;
98 static sem_t kconsumerd_sem
;
100 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
102 static int modprobe_remove_kernel_modules(void);
105 * Pointer initialized before thread creation.
107 * This points to the tracing session list containing the session count and a
108 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
109 * MUST NOT be taken if you call a public function in session.c.
111 * The lock is nested inside the structure: session_list_ptr->lock. Please use
112 * lock_session_list and unlock_session_list for lock acquisition.
114 static struct ltt_session_list
*session_list_ptr
;
119 * Return -1 on error or 0 if all pipes are created.
121 static int init_thread_quit_pipe(void)
125 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
127 perror("thread quit pipe");
136 * Complete teardown of a kernel session. This free all data structure related
137 * to a kernel session and update counter.
139 static void teardown_kernel_session(struct ltt_session
*session
)
141 if (session
->kernel_session
!= NULL
) {
142 DBG("Tearing down kernel session");
143 trace_destroy_kernel_session(session
->kernel_session
);
144 /* Extra precaution */
145 session
->kernel_session
= NULL
;
149 static void stop_threads(void)
151 /* Stopping all threads */
152 DBG("Terminating all threads");
153 close(thread_quit_pipe
[0]);
154 close(thread_quit_pipe
[1]);
160 static void cleanup(void)
164 struct ltt_session
*sess
, *stmp
;
169 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
170 "Matthew, BEET driven development works!%c[%dm",
171 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
174 DBG("Removing %s directory", LTTNG_RUNDIR
);
175 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
177 ERR("asprintf failed. Something is really wrong!");
180 /* Remove lttng run directory */
183 ERR("Unable to clean " LTTNG_RUNDIR
);
186 DBG("Cleaning up all session");
188 /* Destroy session list mutex */
189 if (session_list_ptr
!= NULL
) {
190 pthread_mutex_destroy(&session_list_ptr
->lock
);
192 /* Cleanup ALL session */
193 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
194 teardown_kernel_session(sess
);
195 // TODO complete session cleanup (including UST)
199 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
201 DBG("Closing kernel fd");
202 close(kernel_tracer_fd
);
204 DBG("Unloading kernel modules");
205 modprobe_remove_kernel_modules();
209 * Send data on a unix socket using the liblttsessiondcomm API.
211 * Return lttcomm error code.
213 static int send_unix_sock(int sock
, void *buf
, size_t len
)
215 /* Check valid length */
220 return lttcomm_send_unix_sock(sock
, buf
, len
);
224 * Free memory of a command context structure.
226 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
228 DBG("Clean command context structure");
230 if ((*cmd_ctx
)->llm
) {
231 free((*cmd_ctx
)->llm
);
233 if ((*cmd_ctx
)->lsm
) {
234 free((*cmd_ctx
)->lsm
);
242 * Send all stream fds of kernel channel to the consumer.
244 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
248 struct ltt_kernel_stream
*stream
;
249 struct lttcomm_kconsumerd_header lkh
;
250 struct lttcomm_kconsumerd_msg lkm
;
252 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
254 nb_fd
= channel
->stream_count
;
257 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
258 lkh
.cmd_type
= ADD_STREAM
;
260 DBG("Sending kconsumerd header");
262 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
264 perror("send kconsumerd header");
268 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
269 if (stream
->fd
!= 0) {
271 lkm
.state
= stream
->state
;
272 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
273 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
275 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
277 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
279 perror("send kconsumerd fd");
285 DBG("Kconsumerd channel fds sent");
294 * Send all stream fds of the kernel session to the consumer.
296 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
299 struct ltt_kernel_channel
*chan
;
300 struct lttcomm_kconsumerd_header lkh
;
301 struct lttcomm_kconsumerd_msg lkm
;
304 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
305 lkh
.cmd_type
= ADD_STREAM
;
307 DBG("Sending kconsumerd header for metadata");
309 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
311 perror("send kconsumerd header");
315 DBG("Sending metadata stream fd");
317 if (session
->metadata_stream_fd
!= 0) {
318 /* Send metadata stream fd first */
319 lkm
.fd
= session
->metadata_stream_fd
;
320 lkm
.state
= ACTIVE_FD
;
321 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
322 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
324 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
326 perror("send kconsumerd fd");
331 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
332 ret
= send_kconsumerd_channel_fds(sock
, chan
);
338 DBG("Kconsumerd fds (metadata and channel streams) sent");
348 * Return a socket connected to the libust communication socket of the
349 * application identified by the pid.
351 * If the pid is not found in the traceable list, return -1 to indicate error.
353 static int ust_connect_app(pid_t pid
)
356 struct ltt_traceable_app
*lta
;
358 DBG("Connect to application pid %d", pid
);
360 lta
= find_app_by_pid(pid
);
363 DBG("Application pid %d not found", pid
);
367 sock
= ustctl_connect_pid(lta
->pid
);
369 ERR("Fail connecting to the PID %d", pid
);
374 #endif /* DISABLED */
377 * Notify apps by writing 42 to a named pipe using name. Every applications
378 * waiting for a ltt-sessiond will be notified and re-register automatically to
379 * the session daemon.
381 * Return open or write error value.
383 static int notify_apps(const char *name
)
388 DBG("Notify the global application pipe");
390 /* Try opening the global pipe */
391 fd
= open(name
, O_WRONLY
);
396 /* Notify by writing on the pipe */
397 ret
= write(fd
, "42", 2);
407 * Setup the outgoing data buffer for the response (llm) by allocating the
408 * right amount of memory and copying the original information from the lsm
411 * Return total size of the buffer pointed by buf.
413 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
419 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
420 if (cmd_ctx
->llm
== NULL
) {
426 /* Copy common data */
427 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
428 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
430 cmd_ctx
->llm
->data_size
= size
;
431 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
440 * Update the kernel pollfd set of all channel fd available over all tracing
441 * session. Add the wakeup pipe at the end of the set.
443 static int update_kernel_pollfd(void)
447 * The wakup pipe and the quit pipe are needed so the number of fds starts
448 * at 2 for those pipes.
450 unsigned int nb_fd
= 2;
451 struct ltt_session
*session
;
452 struct ltt_kernel_channel
*channel
;
454 DBG("Updating kernel_pollfd");
456 /* Get the number of channel of all kernel session */
458 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
459 lock_session(session
);
460 if (session
->kernel_session
== NULL
) {
461 unlock_session(session
);
464 nb_fd
+= session
->kernel_session
->channel_count
;
465 unlock_session(session
);
468 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
470 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
471 if (kernel_pollfd
== NULL
) {
472 perror("malloc kernel_pollfd");
476 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
477 lock_session(session
);
478 if (session
->kernel_session
== NULL
) {
479 unlock_session(session
);
483 ERR("To much channel for kernel_pollfd size");
484 unlock_session(session
);
487 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
488 kernel_pollfd
[i
].fd
= channel
->fd
;
489 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
492 unlock_session(session
);
494 unlock_session_list();
496 /* Adding wake up pipe */
497 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
498 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
500 /* Adding the quit pipe */
501 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
506 unlock_session_list();
511 * Find the channel fd from 'fd' over all tracing session. When found, check
512 * for new channel stream and send those stream fds to the kernel consumer.
514 * Useful for CPU hotplug feature.
516 static int update_kernel_stream(int fd
)
519 struct ltt_session
*session
;
520 struct ltt_kernel_channel
*channel
;
522 DBG("Updating kernel streams for channel fd %d", fd
);
525 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
526 lock_session(session
);
527 if (session
->kernel_session
== NULL
) {
528 unlock_session(session
);
531 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
532 if (channel
->fd
== fd
) {
533 DBG("Channel found, updating kernel streams");
534 ret
= kernel_open_channel_stream(channel
);
539 * Have we already sent fds to the consumer? If yes, it means that
540 * tracing is started so it is safe to send our updated stream fds.
542 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
543 ret
= send_kconsumerd_channel_fds(kconsumerd_cmd_sock
, channel
);
551 unlock_session(session
);
555 unlock_session_list();
557 unlock_session(session
);
563 * This thread manage event coming from the kernel.
565 * Features supported in this thread:
568 static void *thread_manage_kernel(void *data
)
570 int ret
, i
, nb_fd
= 0;
572 int update_poll_flag
= 1;
574 DBG("Thread manage kernel started");
577 if (update_poll_flag
== 1) {
578 nb_fd
= update_kernel_pollfd();
582 update_poll_flag
= 0;
585 DBG("Polling on %d fds", nb_fd
);
587 /* Poll infinite value of time */
588 ret
= poll(kernel_pollfd
, nb_fd
, -1);
590 perror("poll kernel thread");
592 } else if (ret
== 0) {
593 /* Should not happen since timeout is infinite */
597 /* Thread quit pipe has been closed. Killing thread. */
598 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
602 DBG("Kernel poll event triggered");
605 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
608 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
610 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
611 update_poll_flag
= 1;
619 for (i
= 0; i
< nb_fd
; i
++) {
620 switch (kernel_pollfd
[i
].revents
) {
622 * New CPU detected by the kernel. Adding kernel stream to kernel
623 * session and updating the kernel consumer
625 case POLLIN
| POLLRDNORM
:
626 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
636 DBG("Kernel thread dying");
641 close(kernel_poll_pipe
[0]);
642 close(kernel_poll_pipe
[1]);
647 * This thread manage the kconsumerd error sent back to the session daemon.
649 static void *thread_manage_kconsumerd(void *data
)
652 enum lttcomm_return_code code
;
653 struct pollfd pollfd
[2];
655 DBG("[thread] Manage kconsumerd started");
657 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
662 /* First fd is always the quit pipe */
663 pollfd
[0].fd
= thread_quit_pipe
[0];
666 pollfd
[1].fd
= kconsumerd_err_sock
;
667 pollfd
[1].events
= POLLIN
;
669 /* Inifinite blocking call, waiting for transmission */
670 ret
= poll(pollfd
, 2, -1);
672 perror("poll kconsumerd thread");
676 /* Thread quit pipe has been closed. Killing thread. */
677 if (pollfd
[0].revents
== POLLNVAL
) {
679 } else if (pollfd
[1].revents
== POLLERR
) {
680 ERR("Kconsumerd err socket poll error");
684 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
689 /* Getting status code from kconsumerd */
690 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
695 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
696 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
697 if (kconsumerd_cmd_sock
< 0) {
698 sem_post(&kconsumerd_sem
);
699 perror("kconsumerd connect");
702 /* Signal condition to tell that the kconsumerd is ready */
703 sem_post(&kconsumerd_sem
);
704 DBG("Kconsumerd command socket ready");
706 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
707 lttcomm_get_readable_code(-code
));
711 /* Wait for any kconsumerd error */
712 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
714 ERR("Kconsumerd closed the command socket");
718 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
721 DBG("Kconsumerd thread dying");
722 if (kconsumerd_err_sock
) {
723 close(kconsumerd_err_sock
);
725 if (kconsumerd_cmd_sock
) {
726 close(kconsumerd_cmd_sock
);
732 unlink(kconsumerd_err_unix_sock_path
);
733 unlink(kconsumerd_cmd_unix_sock_path
);
740 * This thread manage the application socket communication
742 static void *thread_manage_apps(void *data
)
745 struct pollfd pollfd
[2];
747 /* TODO: Something more elegant is needed but fine for now */
748 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
749 * for 32-bit vs 64-bit compat processes. */
750 /* replicate in ust with version number */
752 int reg
; /* 1:register, 0:unregister */
757 DBG("[thread] Manage apps started");
759 ret
= lttcomm_listen_unix_sock(apps_sock
);
764 /* First fd is always the quit pipe */
765 pollfd
[0].fd
= thread_quit_pipe
[0];
768 pollfd
[1].fd
= apps_sock
;
769 pollfd
[1].events
= POLLIN
;
771 /* Notify all applications to register */
772 notify_apps(default_global_apps_pipe
);
775 DBG("Accepting application registration");
777 /* Inifinite blocking call, waiting for transmission */
778 ret
= poll(pollfd
, 2, -1);
780 perror("poll apps thread");
784 /* Thread quit pipe has been closed. Killing thread. */
785 if (pollfd
[0].revents
== POLLNVAL
) {
787 } else if (pollfd
[1].revents
== POLLERR
) {
788 ERR("Apps socket poll error");
792 sock
= lttcomm_accept_unix_sock(apps_sock
);
798 * Basic recv here to handle the very simple data
799 * that the libust send to register (reg_msg).
801 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
807 /* Add application to the global traceable list */
808 if (reg_msg
.reg
== 1) {
810 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
812 /* register_traceable_app only return an error with
813 * ENOMEM. At this point, we better stop everything.
819 unregister_traceable_app(reg_msg
.pid
);
824 DBG("Apps thread dying");
832 unlink(apps_unix_sock_path
);
837 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
838 * exec or it will fails.
840 static int spawn_kconsumerd_thread(void)
844 /* Setup semaphore */
845 sem_init(&kconsumerd_sem
, 0, 0);
847 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
849 perror("pthread_create kconsumerd");
853 /* Wait for the kconsumerd thread to be ready */
854 sem_wait(&kconsumerd_sem
);
856 if (kconsumerd_pid
== 0) {
857 ERR("Kconsumerd did not start");
864 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
868 static int join_kconsumerd_thread(void)
873 if (kconsumerd_pid
!= 0) {
874 ret
= kill(kconsumerd_pid
, SIGTERM
);
876 ERR("Error killing kconsumerd");
879 return pthread_join(kconsumerd_thread
, &status
);
886 * Fork and exec a kernel consumer daemon (kconsumerd).
888 * Return pid if successful else -1.
890 static pid_t
spawn_kconsumerd(void)
894 const char *verbosity
;
896 DBG("Spawning kconsumerd");
903 if (opt_verbose
> 1) {
904 verbosity
= "--verbose";
906 verbosity
= "--quiet";
908 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
910 perror("kernel start consumer exec");
913 } else if (pid
> 0) {
917 perror("kernel start consumer fork");
927 * Spawn the kconsumerd daemon and session daemon thread.
929 static int start_kconsumerd(void)
933 pthread_mutex_lock(&kconsumerd_pid_mutex
);
934 if (kconsumerd_pid
!= 0) {
935 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
939 ret
= spawn_kconsumerd();
941 ERR("Spawning kconsumerd failed");
942 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
943 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
947 /* Setting up the global kconsumerd_pid */
948 kconsumerd_pid
= ret
;
949 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
951 DBG("Kconsumerd pid %d", ret
);
953 DBG("Spawning kconsumerd thread");
954 ret
= spawn_kconsumerd_thread();
956 ERR("Fatal error spawning kconsumerd thread");
968 * modprobe_kernel_modules
970 static int modprobe_kernel_modules(void)
975 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
976 ret
= snprintf(modprobe
, sizeof(modprobe
),
977 "/sbin/modprobe %s%s",
978 kernel_modules_list
[i
].required
? "" : "--quiet ",
979 kernel_modules_list
[i
].name
);
981 perror("snprintf modprobe");
984 modprobe
[sizeof(modprobe
) - 1] = '\0';
985 ret
= system(modprobe
);
987 ERR("Unable to launch modprobe for module %s",
988 kernel_modules_list
[i
].name
);
989 } else if (kernel_modules_list
[i
].required
990 && WEXITSTATUS(ret
) != 0) {
991 ERR("Unable to load module %s",
992 kernel_modules_list
[i
].name
);
994 DBG("Modprobe successfully %s",
995 kernel_modules_list
[i
].name
);
1004 * modprobe_remove_kernel_modules
1005 * Remove modules in reverse load order.
1007 static int modprobe_remove_kernel_modules(void)
1012 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
1013 ret
= snprintf(modprobe
, sizeof(modprobe
),
1014 "/sbin/modprobe --remove --quiet %s",
1015 kernel_modules_list
[i
].name
);
1017 perror("snprintf modprobe --remove");
1020 modprobe
[sizeof(modprobe
) - 1] = '\0';
1021 ret
= system(modprobe
);
1023 ERR("Unable to launch modprobe --remove for module %s",
1024 kernel_modules_list
[i
].name
);
1025 } else if (kernel_modules_list
[i
].required
1026 && WEXITSTATUS(ret
) != 0) {
1027 ERR("Unable to remove module %s",
1028 kernel_modules_list
[i
].name
);
1030 DBG("Modprobe removal successful %s",
1031 kernel_modules_list
[i
].name
);
1042 static int mount_debugfs(char *path
)
1045 char *type
= "debugfs";
1047 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
);
1052 ret
= mount(type
, path
, type
, 0, NULL
);
1054 perror("mount debugfs");
1058 DBG("Mounted debugfs successfully at %s", path
);
1065 * Setup necessary data for kernel tracer action.
1067 static void init_kernel_tracer(void)
1070 char *proc_mounts
= "/proc/mounts";
1072 char *debugfs_path
= NULL
, *lttng_path
;
1075 /* Detect debugfs */
1076 fp
= fopen(proc_mounts
, "r");
1078 ERR("Unable to probe %s", proc_mounts
);
1082 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1083 if (strstr(line
, "debugfs") != NULL
) {
1084 /* Remove first string */
1086 /* Dup string here so we can reuse line later on */
1087 debugfs_path
= strdup(strtok(NULL
, " "));
1088 DBG("Got debugfs path : %s", debugfs_path
);
1095 /* Mount debugfs if needded */
1096 if (debugfs_path
== NULL
) {
1097 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1099 perror("asprintf debugfs path");
1102 ret
= mount_debugfs(debugfs_path
);
1108 /* Modprobe lttng kernel modules */
1109 ret
= modprobe_kernel_modules();
1114 /* Setup lttng kernel path */
1115 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1117 perror("asprintf lttng path");
1121 /* Open debugfs lttng */
1122 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1123 if (kernel_tracer_fd
< 0) {
1124 DBG("Failed to open %s", lttng_path
);
1130 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1140 WARN("No kernel tracer available");
1141 kernel_tracer_fd
= 0;
1146 * Start tracing by creating trace directory and sending FDs to the kernel
1149 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1153 if (session
->kconsumer_fds_sent
== 0) {
1154 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
1156 ERR("Send kconsumerd fds failed");
1157 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1161 session
->kconsumer_fds_sent
= 1;
1169 * Notify kernel thread to update it's pollfd.
1171 static int notify_kernel_pollfd(void)
1175 /* Inform kernel thread of the new kernel channel */
1176 ret
= write(kernel_poll_pipe
[1], "!", 1);
1178 perror("write kernel poll pipe");
1185 * Allocate a channel structure and fill it.
1187 static struct lttng_channel
*init_default_channel(char *name
)
1189 struct lttng_channel
*chan
;
1191 chan
= malloc(sizeof(struct lttng_channel
));
1193 perror("init channel malloc");
1197 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1198 perror("snprintf channel name");
1202 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1203 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
1204 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
1205 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1206 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1207 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1214 * Create a kernel tracer session then create the default channel.
1216 static int create_kernel_session(struct ltt_session
*session
)
1220 DBG("Creating kernel session");
1222 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1224 ret
= LTTCOMM_KERN_SESS_FAIL
;
1228 ret
= mkdir_recursive(session
->path
, S_IRWXU
| S_IRWXG
);
1230 if (ret
!= EEXIST
) {
1231 ERR("Trace directory creation error");
1241 * Using the session list, filled a lttng_session array to send back to the
1242 * client for session listing.
1244 * The session list lock MUST be acquired before calling this function. Use
1245 * lock_session_list() and unlock_session_list().
1247 static void list_lttng_sessions(struct lttng_session
*sessions
)
1250 struct ltt_session
*session
;
1252 DBG("Getting all available session");
1254 * Iterate over session list and append data after the control struct in
1257 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1258 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1259 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1265 * Fill lttng_channel array of all channels.
1267 static void list_lttng_channels(struct ltt_session
*session
,
1268 struct lttng_channel
*channels
)
1271 struct ltt_kernel_channel
*kchan
;
1273 DBG("Listing channels for session %s", session
->name
);
1275 /* Kernel channels */
1276 if (session
->kernel_session
!= NULL
) {
1277 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1278 /* Copy lttng_channel struct to array */
1279 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1280 channels
[i
].enabled
= kchan
->enabled
;
1285 /* TODO: Missing UST listing */
1289 * Fill lttng_event array of all events in the channel.
1291 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1292 struct lttng_event
*events
)
1295 * TODO: This is ONLY kernel. Need UST support.
1298 struct ltt_kernel_event
*event
;
1300 DBG("Listing events for channel %s", kchan
->channel
->name
);
1302 /* Kernel channels */
1303 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1304 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1305 events
[i
].enabled
= event
->enabled
;
1306 switch (event
->event
->instrumentation
) {
1307 case LTTNG_KERNEL_TRACEPOINT
:
1308 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1310 case LTTNG_KERNEL_KPROBE
:
1311 case LTTNG_KERNEL_KRETPROBE
:
1312 events
[i
].type
= LTTNG_EVENT_PROBE
;
1313 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1314 sizeof(struct lttng_kernel_kprobe
));
1316 case LTTNG_KERNEL_FUNCTION
:
1317 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1318 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1319 sizeof(struct lttng_kernel_function
));
1327 * Process the command requested by the lttng client within the command
1328 * context structure. This function make sure that the return structure (llm)
1329 * is set and ready for transmission before returning.
1331 * Return any error encountered or 0 for success.
1333 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1335 int ret
= LTTCOMM_OK
;
1337 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1339 /* Listing commands don't need a session */
1340 switch (cmd_ctx
->lsm
->cmd_type
) {
1341 case LTTNG_CREATE_SESSION
:
1342 case LTTNG_LIST_SESSIONS
:
1343 case LTTNG_LIST_TRACEPOINTS
:
1344 case LTTNG_CALIBRATE
:
1347 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1348 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1349 if (cmd_ctx
->session
== NULL
) {
1350 /* If session name not found */
1351 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1352 ret
= LTTCOMM_SESS_NOT_FOUND
;
1353 } else { /* If no session name specified */
1354 ret
= LTTCOMM_SELECT_SESS
;
1358 /* Acquire lock for the session */
1359 lock_session(cmd_ctx
->session
);
1365 * Check domain type for specific "pre-action".
1367 switch (cmd_ctx
->lsm
->domain
.type
) {
1368 case LTTNG_DOMAIN_KERNEL
:
1369 /* Kernel tracer check */
1370 if (kernel_tracer_fd
== 0) {
1371 init_kernel_tracer();
1372 if (kernel_tracer_fd
== 0) {
1373 ret
= LTTCOMM_KERN_NA
;
1378 /* Need a session for kernel command */
1379 switch (cmd_ctx
->lsm
->cmd_type
) {
1380 case LTTNG_CREATE_SESSION
:
1381 case LTTNG_LIST_SESSIONS
:
1382 case LTTNG_LIST_TRACEPOINTS
:
1383 case LTTNG_CALIBRATE
:
1386 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1387 ret
= create_kernel_session(cmd_ctx
->session
);
1389 ret
= LTTCOMM_KERN_SESS_FAIL
;
1393 /* Start the kernel consumer daemon */
1394 if (kconsumerd_pid
== 0) {
1395 ret
= start_kconsumerd();
1407 /* Process by command type */
1408 switch (cmd_ctx
->lsm
->cmd_type
) {
1409 case LTTNG_ADD_CONTEXT
:
1411 struct lttng_kernel_context kctx
;
1413 /* Setup lttng message with no payload */
1414 ret
= setup_lttng_msg(cmd_ctx
, 0);
1419 switch (cmd_ctx
->lsm
->domain
.type
) {
1420 case LTTNG_DOMAIN_KERNEL
:
1421 /* Create Kernel context */
1422 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1423 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1424 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1425 strncpy(kctx
.u
.perf_counter
.name
,
1426 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1427 LTTNG_SYMBOL_NAME_LEN
);
1429 /* Add kernel context to kernel tracer. See context.c */
1430 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1431 cmd_ctx
->lsm
->u
.context
.event_name
,
1432 cmd_ctx
->lsm
->u
.context
.channel_name
);
1433 if (ret
!= LTTCOMM_OK
) {
1438 /* TODO: Userspace tracing */
1439 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1446 case LTTNG_DISABLE_CHANNEL
:
1448 struct ltt_kernel_channel
*kchan
;
1450 /* Setup lttng message with no payload */
1451 ret
= setup_lttng_msg(cmd_ctx
, 0);
1456 switch (cmd_ctx
->lsm
->domain
.type
) {
1457 case LTTNG_DOMAIN_KERNEL
:
1458 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1459 cmd_ctx
->session
->kernel_session
);
1460 if (kchan
== NULL
) {
1461 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1463 } else if (kchan
->enabled
== 1) {
1464 ret
= kernel_disable_channel(kchan
);
1466 if (ret
!= EEXIST
) {
1467 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1472 kernel_wait_quiescent(kernel_tracer_fd
);
1475 /* TODO: Userspace tracing */
1476 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1483 case LTTNG_DISABLE_EVENT
:
1485 struct ltt_kernel_channel
*kchan
;
1486 struct ltt_kernel_event
*kevent
;
1488 /* Setup lttng message with no payload */
1489 ret
= setup_lttng_msg(cmd_ctx
, 0);
1494 switch (cmd_ctx
->lsm
->domain
.type
) {
1495 case LTTNG_DOMAIN_KERNEL
:
1496 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1497 cmd_ctx
->session
->kernel_session
);
1498 if (kchan
== NULL
) {
1499 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1503 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1504 if (kevent
!= NULL
) {
1505 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1506 kchan
->channel
->name
);
1507 ret
= kernel_disable_event(kevent
);
1509 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1514 kernel_wait_quiescent(kernel_tracer_fd
);
1517 /* TODO: Userspace tracing */
1518 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1525 case LTTNG_DISABLE_ALL_EVENT
:
1527 struct ltt_kernel_channel
*kchan
;
1528 struct ltt_kernel_event
*kevent
;
1530 /* Setup lttng message with no payload */
1531 ret
= setup_lttng_msg(cmd_ctx
, 0);
1536 switch (cmd_ctx
->lsm
->domain
.type
) {
1537 case LTTNG_DOMAIN_KERNEL
:
1538 DBG("Disabling all enabled kernel events");
1539 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1540 cmd_ctx
->session
->kernel_session
);
1541 if (kchan
== NULL
) {
1542 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1546 /* For each event in the kernel session */
1547 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1548 DBG("Disabling kernel event %s for channel %s.",
1549 kevent
->event
->name
, kchan
->channel
->name
);
1550 ret
= kernel_disable_event(kevent
);
1556 /* Quiescent wait after event disable */
1557 kernel_wait_quiescent(kernel_tracer_fd
);
1560 /* TODO: Userspace tracing */
1561 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1568 case LTTNG_ENABLE_CHANNEL
:
1570 struct ltt_kernel_channel
*kchan
;
1572 /* Setup lttng message with no payload */
1573 ret
= setup_lttng_msg(cmd_ctx
, 0);
1578 switch (cmd_ctx
->lsm
->domain
.type
) {
1579 case LTTNG_DOMAIN_KERNEL
:
1580 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1581 cmd_ctx
->session
->kernel_session
);
1582 if (kchan
== NULL
) {
1583 /* Channel not found, creating it */
1584 DBG("Creating kernel channel");
1586 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1587 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1589 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1593 /* Notify kernel thread that there is a new channel */
1594 ret
= notify_kernel_pollfd();
1596 ret
= LTTCOMM_FATAL
;
1599 } else if (kchan
->enabled
== 0) {
1600 ret
= kernel_enable_channel(kchan
);
1602 if (ret
!= EEXIST
) {
1603 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1609 kernel_wait_quiescent(kernel_tracer_fd
);
1612 /* TODO: Userspace tracing */
1613 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1620 case LTTNG_ENABLE_EVENT
:
1623 struct ltt_kernel_channel
*kchan
;
1624 struct ltt_kernel_event
*kevent
;
1625 struct lttng_channel
*chan
;
1627 /* Setup lttng message with no payload */
1628 ret
= setup_lttng_msg(cmd_ctx
, 0);
1633 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1635 switch (cmd_ctx
->lsm
->domain
.type
) {
1636 case LTTNG_DOMAIN_KERNEL
:
1638 kchan
= get_kernel_channel_by_name(channel_name
,
1639 cmd_ctx
->session
->kernel_session
);
1640 if (kchan
== NULL
) {
1641 DBG("Channel not found. Creating channel %s", channel_name
);
1643 chan
= init_default_channel(channel_name
);
1645 ret
= LTTCOMM_FATAL
;
1649 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1650 chan
, cmd_ctx
->session
->path
);
1652 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1656 } while (kchan
== NULL
);
1658 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
1659 if (kevent
== NULL
) {
1660 DBG("Creating kernel event %s for channel %s.",
1661 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
1662 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
1664 DBG("Enabling kernel event %s for channel %s.",
1665 kevent
->event
->name
, channel_name
);
1666 ret
= kernel_enable_event(kevent
);
1667 if (ret
== -EEXIST
) {
1668 ret
= LTTCOMM_KERN_EVENT_EXIST
;
1674 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1678 kernel_wait_quiescent(kernel_tracer_fd
);
1681 /* TODO: Userspace tracing */
1682 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1688 case LTTNG_ENABLE_ALL_EVENT
:
1692 struct ltt_kernel_channel
*kchan
;
1693 struct ltt_kernel_event
*kevent
;
1694 struct lttng_event
*event_list
;
1695 struct lttng_channel
*chan
;
1697 /* Setup lttng message with no payload */
1698 ret
= setup_lttng_msg(cmd_ctx
, 0);
1703 DBG("Enabling all kernel event");
1705 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1707 switch (cmd_ctx
->lsm
->domain
.type
) {
1708 case LTTNG_DOMAIN_KERNEL
:
1710 kchan
= get_kernel_channel_by_name(channel_name
,
1711 cmd_ctx
->session
->kernel_session
);
1712 if (kchan
== NULL
) {
1713 DBG("Channel not found. Creating channel %s", channel_name
);
1715 chan
= init_default_channel(channel_name
);
1717 ret
= LTTCOMM_FATAL
;
1721 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1722 chan
, cmd_ctx
->session
->path
);
1724 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1728 } while (kchan
== NULL
);
1730 /* For each event in the kernel session */
1731 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1732 DBG("Enabling kernel event %s for channel %s.",
1733 kevent
->event
->name
, channel_name
);
1734 ret
= kernel_enable_event(kevent
);
1740 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1742 ret
= LTTCOMM_KERN_LIST_FAIL
;
1746 for (i
= 0; i
< size
; i
++) {
1747 kevent
= get_kernel_event_by_name(event_list
[i
].name
, kchan
);
1748 if (kevent
== NULL
) {
1749 /* Default event type for enable all */
1750 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1751 /* Enable each single tracepoint event */
1752 ret
= kernel_create_event(&event_list
[i
], kchan
);
1754 /* Ignore error here and continue */
1761 /* Quiescent wait after event enable */
1762 kernel_wait_quiescent(kernel_tracer_fd
);
1765 /* TODO: Userspace tracing */
1766 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1773 case LTTNG_LIST_TRACEPOINTS
:
1775 struct lttng_event
*events
;
1776 ssize_t nb_events
= 0;
1778 switch (cmd_ctx
->lsm
->domain
.type
) {
1779 case LTTNG_DOMAIN_KERNEL
:
1780 DBG("Listing kernel events");
1781 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
1782 if (nb_events
< 0) {
1783 ret
= LTTCOMM_KERN_LIST_FAIL
;
1788 /* TODO: Userspace listing */
1789 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1794 * Setup lttng message with payload size set to the event list size in
1795 * bytes and then copy list into the llm payload.
1797 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
1803 /* Copy event list into message payload */
1804 memcpy(cmd_ctx
->llm
->payload
, events
,
1805 sizeof(struct lttng_event
) * nb_events
);
1812 case LTTNG_START_TRACE
:
1814 struct ltt_kernel_channel
*chan
;
1816 /* Setup lttng message with no payload */
1817 ret
= setup_lttng_msg(cmd_ctx
, 0);
1822 /* Kernel tracing */
1823 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1824 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1825 DBG("Open kernel metadata");
1826 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1827 cmd_ctx
->session
->path
);
1829 ret
= LTTCOMM_KERN_META_FAIL
;
1834 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1835 DBG("Opening kernel metadata stream");
1836 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1837 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1839 ERR("Kernel create metadata stream failed");
1840 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1846 /* For each channel */
1847 cds_list_for_each_entry(chan
,
1848 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1849 if (chan
->stream_count
== 0) {
1850 ret
= kernel_open_channel_stream(chan
);
1852 ERR("Kernel create channel stream failed");
1853 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1856 /* Update the stream global counter */
1857 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1861 DBG("Start kernel tracing");
1862 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1864 ERR("Kernel start session failed");
1865 ret
= LTTCOMM_KERN_START_FAIL
;
1869 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1871 ret
= LTTCOMM_KERN_START_FAIL
;
1875 /* Quiescent wait after starting trace */
1876 kernel_wait_quiescent(kernel_tracer_fd
);
1879 /* TODO: Start all UST traces */
1884 case LTTNG_STOP_TRACE
:
1886 struct ltt_kernel_channel
*chan
;
1887 /* Setup lttng message with no payload */
1888 ret
= setup_lttng_msg(cmd_ctx
, 0);
1894 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1895 DBG("Stop kernel tracing");
1897 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1899 ERR("Kernel metadata flush failed");
1902 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1903 ret
= kernel_flush_buffer(chan
);
1905 ERR("Kernel flush buffer error");
1909 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1911 ERR("Kernel stop session failed");
1912 ret
= LTTCOMM_KERN_STOP_FAIL
;
1916 /* Quiescent wait after stopping trace */
1917 kernel_wait_quiescent(kernel_tracer_fd
);
1920 /* TODO : User-space tracer */
1925 case LTTNG_CREATE_SESSION
:
1927 /* Setup lttng message with no payload */
1928 ret
= setup_lttng_msg(cmd_ctx
, 0);
1933 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
1935 if (ret
== -EEXIST
) {
1936 ret
= LTTCOMM_EXIST_SESS
;
1938 ret
= LTTCOMM_FATAL
;
1946 case LTTNG_DESTROY_SESSION
:
1948 /* Setup lttng message with no payload */
1949 ret
= setup_lttng_msg(cmd_ctx
, 0);
1954 /* Clean kernel session teardown */
1955 teardown_kernel_session(cmd_ctx
->session
);
1957 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
1959 ret
= LTTCOMM_FATAL
;
1964 * Must notify the kernel thread here to update it's pollfd in order to
1965 * remove the channel(s)' fd just destroyed.
1967 ret
= notify_kernel_pollfd();
1969 ret
= LTTCOMM_FATAL
;
1976 case LTTNG_LIST_DOMAINS
:
1980 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1984 nb_dom
+= cmd_ctx
->session
->ust_trace_count
;
1986 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
1991 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
1992 LTTNG_DOMAIN_KERNEL
;
1994 /* TODO: User-space tracer domain support */
1998 case LTTNG_LIST_CHANNELS
:
2001 * TODO: Only kernel channels are listed here. UST listing
2002 * is needed on lttng-ust 2.0 release.
2005 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2006 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2009 ret
= setup_lttng_msg(cmd_ctx
,
2010 sizeof(struct lttng_channel
) * nb_chan
);
2015 list_lttng_channels(cmd_ctx
->session
,
2016 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2021 case LTTNG_LIST_EVENTS
:
2024 * TODO: Only kernel events are listed here. UST listing
2025 * is needed on lttng-ust 2.0 release.
2027 size_t nb_event
= 0;
2028 struct ltt_kernel_channel
*kchan
= NULL
;
2030 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2031 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2032 cmd_ctx
->session
->kernel_session
);
2033 if (kchan
== NULL
) {
2034 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2037 nb_event
+= kchan
->event_count
;
2040 ret
= setup_lttng_msg(cmd_ctx
,
2041 sizeof(struct lttng_event
) * nb_event
);
2046 DBG("Listing events (%ld events)", nb_event
);
2048 list_lttng_events(kchan
,
2049 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2054 case LTTNG_LIST_SESSIONS
:
2056 lock_session_list();
2058 if (session_list_ptr
->count
== 0) {
2059 ret
= LTTCOMM_NO_SESSION
;
2060 unlock_session_list();
2064 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2065 session_list_ptr
->count
);
2067 unlock_session_list();
2071 /* Filled the session array */
2072 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2074 unlock_session_list();
2080 case LTTNG_CALIBRATE
:
2082 /* Setup lttng message with no payload */
2083 ret
= setup_lttng_msg(cmd_ctx
, 0);
2088 switch (cmd_ctx
->lsm
->domain
.type
) {
2089 case LTTNG_DOMAIN_KERNEL
:
2091 struct lttng_kernel_calibrate kcalibrate
;
2093 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2094 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2096 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2102 /* TODO: Userspace tracing */
2103 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2111 /* Undefined command */
2112 ret
= setup_lttng_msg(cmd_ctx
, 0);
2121 /* Set return code */
2122 cmd_ctx
->llm
->ret_code
= ret
;
2124 if (cmd_ctx
->session
) {
2125 unlock_session(cmd_ctx
->session
);
2131 if (cmd_ctx
->llm
== NULL
) {
2132 DBG("Missing llm structure. Allocating one.");
2133 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2137 /* Notify client of error */
2138 cmd_ctx
->llm
->ret_code
= ret
;
2141 if (cmd_ctx
->session
) {
2142 unlock_session(cmd_ctx
->session
);
2148 * This thread manage all clients request using the unix client socket for
2151 static void *thread_manage_clients(void *data
)
2154 struct command_ctx
*cmd_ctx
= NULL
;
2155 struct pollfd pollfd
[2];
2157 DBG("[thread] Manage client started");
2159 ret
= lttcomm_listen_unix_sock(client_sock
);
2164 /* First fd is always the quit pipe */
2165 pollfd
[0].fd
= thread_quit_pipe
[0];
2168 pollfd
[1].fd
= client_sock
;
2169 pollfd
[1].events
= POLLIN
;
2171 /* Notify parent pid that we are ready
2172 * to accept command for client side.
2174 if (opt_sig_parent
) {
2175 kill(ppid
, SIGCHLD
);
2179 DBG("Accepting client command ...");
2181 /* Inifinite blocking call, waiting for transmission */
2182 ret
= poll(pollfd
, 2, -1);
2184 perror("poll client thread");
2188 /* Thread quit pipe has been closed. Killing thread. */
2189 if (pollfd
[0].revents
== POLLNVAL
) {
2191 } else if (pollfd
[1].revents
== POLLERR
) {
2192 ERR("Client socket poll error");
2196 sock
= lttcomm_accept_unix_sock(client_sock
);
2201 /* Allocate context command to process the client request */
2202 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2204 /* Allocate data buffer for reception */
2205 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2206 cmd_ctx
->llm
= NULL
;
2207 cmd_ctx
->session
= NULL
;
2210 * Data is received from the lttng client. The struct
2211 * lttcomm_session_msg (lsm) contains the command and data request of
2214 DBG("Receiving data from client ...");
2215 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2220 // TODO: Validate cmd_ctx including sanity check for security purpose.
2223 * This function dispatch the work to the kernel or userspace tracer
2224 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2225 * informations for the client. The command context struct contains
2226 * everything this function may needs.
2228 ret
= process_client_msg(cmd_ctx
);
2230 /* TODO: Inform client somehow of the fatal error. At this point,
2231 * ret < 0 means that a malloc failed (ENOMEM). */
2232 /* Error detected but still accept command */
2233 clean_command_ctx(&cmd_ctx
);
2237 DBG("Sending response (size: %d, retcode: %d)",
2238 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2239 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2241 ERR("Failed to send data back to client");
2244 clean_command_ctx(&cmd_ctx
);
2246 /* End of transmission */
2251 DBG("Client thread dying");
2259 unlink(client_unix_sock_path
);
2261 clean_command_ctx(&cmd_ctx
);
2267 * usage function on stderr
2269 static void usage(void)
2271 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2272 fprintf(stderr
, " -h, --help Display this usage.\n");
2273 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2274 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2275 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2276 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2277 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2278 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2279 fprintf(stderr
, " -V, --version Show version number.\n");
2280 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2281 fprintf(stderr
, " -q, --quiet No output at all.\n");
2282 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2286 * daemon argument parsing
2288 static int parse_args(int argc
, char **argv
)
2292 static struct option long_options
[] = {
2293 { "client-sock", 1, 0, 'c' },
2294 { "apps-sock", 1, 0, 'a' },
2295 { "kconsumerd-cmd-sock", 1, 0, 0 },
2296 { "kconsumerd-err-sock", 1, 0, 0 },
2297 { "daemonize", 0, 0, 'd' },
2298 { "sig-parent", 0, 0, 'S' },
2299 { "help", 0, 0, 'h' },
2300 { "group", 1, 0, 'g' },
2301 { "version", 0, 0, 'V' },
2302 { "quiet", 0, 0, 'q' },
2303 { "verbose", 0, 0, 'v' },
2308 int option_index
= 0;
2309 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
2316 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2318 fprintf(stderr
, " with arg %s\n", optarg
);
2322 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2325 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2331 opt_tracing_group
= strdup(optarg
);
2337 fprintf(stdout
, "%s\n", VERSION
);
2343 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2346 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2352 /* Verbose level can increase using multiple -v */
2356 /* Unknown option or other error.
2357 * Error is printed by getopt, just return */
2366 * Creates the two needed socket by the daemon.
2367 * apps_sock - The communication socket for all UST apps.
2368 * client_sock - The communication of the cli tool (lttng).
2370 static int init_daemon_socket(void)
2375 old_umask
= umask(0);
2377 /* Create client tool unix socket */
2378 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2379 if (client_sock
< 0) {
2380 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2385 /* File permission MUST be 660 */
2386 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2388 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2393 /* Create the application unix socket */
2394 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2395 if (apps_sock
< 0) {
2396 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2401 /* File permission MUST be 666 */
2402 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2404 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2415 * Check if the global socket is available. If yes, error is returned.
2417 static int check_existing_daemon(void)
2421 ret
= access(client_unix_sock_path
, F_OK
);
2423 ret
= access(apps_unix_sock_path
, F_OK
);
2430 * Set the tracing group gid onto the client socket.
2432 * Race window between mkdir and chown is OK because we are going from more
2433 * permissive (root.root) to les permissive (root.tracing).
2435 static int set_permissions(void)
2440 /* Decide which group name to use */
2441 (opt_tracing_group
!= NULL
) ?
2442 (grp
= getgrnam(opt_tracing_group
)) :
2443 (grp
= getgrnam(default_tracing_group
));
2447 WARN("No tracing group detected");
2450 ERR("Missing tracing group. Aborting execution.");
2456 /* Set lttng run dir */
2457 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
2459 ERR("Unable to set group on " LTTNG_RUNDIR
);
2463 /* lttng client socket path */
2464 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
2466 ERR("Unable to set group on %s", client_unix_sock_path
);
2470 /* kconsumerd error socket path */
2471 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
2473 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2477 DBG("All permissions are set");
2484 * Create the pipe used to wake up the kernel thread.
2486 static int create_kernel_poll_pipe(void)
2488 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2492 * Create the lttng run directory needed for all global sockets and pipe.
2494 static int create_lttng_rundir(void)
2498 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2500 if (errno
!= EEXIST
) {
2501 ERR("Unable to create " LTTNG_RUNDIR
);
2513 * Setup sockets and directory needed by the kconsumerd communication with the
2516 static int set_kconsumerd_sockets(void)
2520 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2521 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2524 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2525 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2528 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2530 if (errno
!= EEXIST
) {
2531 ERR("Failed to create " KCONSUMERD_PATH
);
2537 /* Create the kconsumerd error unix socket */
2538 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2539 if (kconsumerd_err_sock
< 0) {
2540 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2545 /* File permission MUST be 660 */
2546 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2548 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2558 * Signal handler for the daemon
2560 * Simply stop all worker threads, leaving main() return gracefully
2561 * after joining all threads and calling cleanup().
2563 static void sighandler(int sig
)
2567 DBG("SIGPIPE catched");
2570 DBG("SIGINT catched");
2574 DBG("SIGTERM catched");
2583 * Setup signal handler for :
2584 * SIGINT, SIGTERM, SIGPIPE
2586 static int set_signal_handler(void)
2589 struct sigaction sa
;
2592 if ((ret
= sigemptyset(&sigset
)) < 0) {
2593 perror("sigemptyset");
2597 sa
.sa_handler
= sighandler
;
2598 sa
.sa_mask
= sigset
;
2600 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2601 perror("sigaction");
2605 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2606 perror("sigaction");
2610 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2611 perror("sigaction");
2615 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2621 * Set open files limit to unlimited. This daemon can open a large number of
2622 * file descriptors in order to consumer multiple kernel traces.
2624 static void set_ulimit(void)
2629 /* The kernel does not allowed an infinite limit for open files */
2630 lim
.rlim_cur
= 65535;
2631 lim
.rlim_max
= 65535;
2633 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2635 perror("failed to set open files limit");
2642 int main(int argc
, char **argv
)
2646 const char *home_path
;
2648 /* Create thread quit pipe */
2649 if ((ret
= init_thread_quit_pipe()) < 0) {
2653 /* Parse arguments */
2655 if ((ret
= parse_args(argc
, argv
) < 0)) {
2668 /* Check if daemon is UID = 0 */
2669 is_root
= !getuid();
2672 ret
= create_lttng_rundir();
2677 if (strlen(apps_unix_sock_path
) == 0) {
2678 snprintf(apps_unix_sock_path
, PATH_MAX
,
2679 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2682 if (strlen(client_unix_sock_path
) == 0) {
2683 snprintf(client_unix_sock_path
, PATH_MAX
,
2684 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2687 home_path
= get_home_dir();
2688 if (home_path
== NULL
) {
2689 /* TODO: Add --socket PATH option */
2690 ERR("Can't get HOME directory for sockets creation.");
2695 if (strlen(apps_unix_sock_path
) == 0) {
2696 snprintf(apps_unix_sock_path
, PATH_MAX
,
2697 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2700 /* Set the cli tool unix socket path */
2701 if (strlen(client_unix_sock_path
) == 0) {
2702 snprintf(client_unix_sock_path
, PATH_MAX
,
2703 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2707 DBG("Client socket path %s", client_unix_sock_path
);
2708 DBG("Application socket path %s", apps_unix_sock_path
);
2711 * See if daemon already exist. If any of the two socket needed by the
2712 * daemon are present, this test fails. However, if the daemon is killed
2713 * with a SIGKILL, those unix socket must be unlinked by hand.
2715 if ((ret
= check_existing_daemon()) == 0) {
2716 ERR("Already running daemon.\n");
2718 * We do not goto exit because we must not cleanup()
2719 * because a daemon is already running.
2724 /* After this point, we can safely call cleanup() so goto error is used */
2727 * These actions must be executed as root. We do that *after* setting up
2728 * the sockets path because we MUST make the check for another daemon using
2729 * those paths *before* trying to set the kernel consumer sockets and init
2733 ret
= set_kconsumerd_sockets();
2738 /* Setup kernel tracer */
2739 init_kernel_tracer();
2741 /* Set ulimit for open files */
2745 if ((ret
= set_signal_handler()) < 0) {
2749 /* Setup the needed unix socket */
2750 if ((ret
= init_daemon_socket()) < 0) {
2754 /* Set credentials to socket */
2755 if (is_root
&& ((ret
= set_permissions()) < 0)) {
2759 /* Get parent pid if -S, --sig-parent is specified. */
2760 if (opt_sig_parent
) {
2764 /* Setup the kernel pipe for waking up the kernel thread */
2765 if ((ret
= create_kernel_poll_pipe()) < 0) {
2770 * Get session list pointer. This pointer MUST NOT be free().
2771 * This list is statically declared in session.c
2773 session_list_ptr
= get_session_list();
2775 /* Create thread to manage the client socket */
2776 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2778 perror("pthread_create");
2782 /* Create thread to manage application socket */
2783 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2785 perror("pthread_create");
2789 /* Create kernel thread to manage kernel event */
2790 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2792 perror("pthread_create");
2796 ret
= pthread_join(kernel_thread
, &status
);
2798 perror("pthread_join");
2799 goto error
; /* join error, exit without cleanup */
2803 ret
= pthread_join(apps_thread
, &status
);
2805 perror("pthread_join");
2806 goto error
; /* join error, exit without cleanup */
2810 ret
= pthread_join(client_thread
, &status
);
2812 perror("pthread_join");
2813 goto error
; /* join error, exit without cleanup */
2816 ret
= join_kconsumerd_thread();
2818 perror("join_kconsumerd");
2819 goto error
; /* join error, exit without cleanup */
2825 * cleanup() is called when no other thread is running.