2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <semaphore.h>
33 #include <sys/mount.h>
35 #include <sys/socket.h>
37 #include <sys/types.h>
39 #include <sys/resource.h>
42 #include <urcu/list.h> /* URCU list library (-lurcu) */
43 #include <lttng/lttng.h>
44 #include <lttng/lttng-sessiond-comm.h>
47 #include "ltt-sessiond.h"
49 #include "kernel-ctl.h"
52 #include "traceable-app.h"
53 #include "lttng-kconsumerd.h"
57 const char default_home_dir
[] = DEFAULT_HOME_DIR
;
58 const char default_tracing_group
[] = LTTNG_DEFAULT_TRACING_GROUP
;
59 const char default_ust_sock_dir
[] = DEFAULT_UST_SOCK_DIR
;
60 const char default_global_apps_pipe
[] = DEFAULT_GLOBAL_APPS_PIPE
;
63 int opt_verbose
; /* Not static for lttngerr.h */
64 int opt_verbose_kconsumerd
; /* Not static for lttngerr.h */
65 int opt_quiet
; /* Not static for lttngerr.h */
68 const char *opt_tracing_group
;
69 static int opt_sig_parent
;
70 static int opt_daemon
;
71 static int is_root
; /* Set to 1 if the daemon is running as root */
72 static pid_t ppid
; /* Parent PID for --sig-parent option */
73 static pid_t kconsumerd_pid
;
74 static struct pollfd
*kernel_pollfd
;
76 static char apps_unix_sock_path
[PATH_MAX
]; /* Global application Unix socket path */
77 static char client_unix_sock_path
[PATH_MAX
]; /* Global client Unix socket path */
78 static char kconsumerd_err_unix_sock_path
[PATH_MAX
]; /* kconsumerd error Unix socket path */
79 static char kconsumerd_cmd_unix_sock_path
[PATH_MAX
]; /* kconsumerd command Unix socket path */
82 static int client_sock
;
84 static int kconsumerd_err_sock
;
85 static int kconsumerd_cmd_sock
;
86 static int kernel_tracer_fd
;
87 static int kernel_poll_pipe
[2];
90 * Quit pipe for all threads. This permits a single cancellation point
91 * for all threads when receiving an event on the pipe.
93 static int thread_quit_pipe
[2];
95 /* Pthread, Mutexes and Semaphores */
96 static pthread_t kconsumerd_thread
;
97 static pthread_t apps_thread
;
98 static pthread_t client_thread
;
99 static pthread_t kernel_thread
;
100 static sem_t kconsumerd_sem
;
102 static pthread_mutex_t kconsumerd_pid_mutex
; /* Mutex to control kconsumerd pid assignation */
104 static int modprobe_remove_kernel_modules(void);
107 * Pointer initialized before thread creation.
109 * This points to the tracing session list containing the session count and a
110 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
111 * MUST NOT be taken if you call a public function in session.c.
113 * The lock is nested inside the structure: session_list_ptr->lock. Please use
114 * lock_session_list and unlock_session_list for lock acquisition.
116 static struct ltt_session_list
*session_list_ptr
;
118 static gid_t
allowed_group(void)
122 if (opt_tracing_group
) {
123 grp
= getgrnam(opt_tracing_group
);
125 grp
= getgrnam(default_tracing_group
);
137 * Return -1 on error or 0 if all pipes are created.
139 static int init_thread_quit_pipe(void)
143 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
145 perror("thread quit pipe");
154 * Complete teardown of a kernel session. This free all data structure related
155 * to a kernel session and update counter.
157 static void teardown_kernel_session(struct ltt_session
*session
)
159 if (session
->kernel_session
!= NULL
) {
160 DBG("Tearing down kernel session");
161 trace_destroy_kernel_session(session
->kernel_session
);
162 /* Extra precaution */
163 session
->kernel_session
= NULL
;
167 static void stop_threads(void)
169 /* Stopping all threads */
170 DBG("Terminating all threads");
171 close(thread_quit_pipe
[0]);
172 close(thread_quit_pipe
[1]);
178 static void cleanup(void)
182 struct ltt_session
*sess
, *stmp
;
187 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
188 "Matthew, BEET driven development works!%c[%dm",
189 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
192 DBG("Removing %s directory", LTTNG_RUNDIR
);
193 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
195 ERR("asprintf failed. Something is really wrong!");
198 /* Remove lttng run directory */
201 ERR("Unable to clean " LTTNG_RUNDIR
);
204 DBG("Cleaning up all session");
206 /* Destroy session list mutex */
207 if (session_list_ptr
!= NULL
) {
208 pthread_mutex_destroy(&session_list_ptr
->lock
);
210 /* Cleanup ALL session */
211 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
212 teardown_kernel_session(sess
);
213 // TODO complete session cleanup (including UST)
217 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
219 DBG("Closing kernel fd");
220 close(kernel_tracer_fd
);
222 DBG("Unloading kernel modules");
223 modprobe_remove_kernel_modules();
227 * Send data on a unix socket using the liblttsessiondcomm API.
229 * Return lttcomm error code.
231 static int send_unix_sock(int sock
, void *buf
, size_t len
)
233 /* Check valid length */
238 return lttcomm_send_unix_sock(sock
, buf
, len
);
242 * Free memory of a command context structure.
244 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
246 DBG("Clean command context structure");
248 if ((*cmd_ctx
)->llm
) {
249 free((*cmd_ctx
)->llm
);
251 if ((*cmd_ctx
)->lsm
) {
252 free((*cmd_ctx
)->lsm
);
260 * Send all stream fds of kernel channel to the consumer.
262 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
266 struct ltt_kernel_stream
*stream
;
267 struct lttcomm_kconsumerd_header lkh
;
268 struct lttcomm_kconsumerd_msg lkm
;
270 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
272 nb_fd
= channel
->stream_count
;
275 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
276 lkh
.cmd_type
= ADD_STREAM
;
278 DBG("Sending kconsumerd header");
280 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
282 perror("send kconsumerd header");
286 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
287 if (stream
->fd
!= 0) {
289 lkm
.state
= stream
->state
;
290 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
291 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
292 lkm
.path_name
[PATH_MAX
- 1] = '\0';
294 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
296 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
298 perror("send kconsumerd fd");
304 DBG("Kconsumerd channel fds sent");
313 * Send all stream fds of the kernel session to the consumer.
315 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
318 struct ltt_kernel_channel
*chan
;
319 struct lttcomm_kconsumerd_header lkh
;
320 struct lttcomm_kconsumerd_msg lkm
;
323 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
324 lkh
.cmd_type
= ADD_STREAM
;
326 DBG("Sending kconsumerd header for metadata");
328 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
330 perror("send kconsumerd header");
334 DBG("Sending metadata stream fd");
336 if (session
->metadata_stream_fd
!= 0) {
337 /* Send metadata stream fd first */
338 lkm
.fd
= session
->metadata_stream_fd
;
339 lkm
.state
= ACTIVE_FD
;
340 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
341 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
342 lkm
.path_name
[PATH_MAX
- 1] = '\0';
344 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
346 perror("send kconsumerd fd");
351 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
352 ret
= send_kconsumerd_channel_fds(sock
, chan
);
358 DBG("Kconsumerd fds (metadata and channel streams) sent");
368 * Return a socket connected to the libust communication socket of the
369 * application identified by the pid.
371 * If the pid is not found in the traceable list, return -1 to indicate error.
373 static int ust_connect_app(pid_t pid
)
376 struct ltt_traceable_app
*lta
;
378 DBG("Connect to application pid %d", pid
);
380 lta
= find_app_by_pid(pid
);
383 DBG("Application pid %d not found", pid
);
387 sock
= ustctl_connect_pid(lta
->pid
);
389 ERR("Fail connecting to the PID %d", pid
);
394 #endif /* DISABLED */
397 * Notify apps by writing 42 to a named pipe using name. Every applications
398 * waiting for a ltt-sessiond will be notified and re-register automatically to
399 * the session daemon.
401 * Return open or write error value.
403 static int notify_apps(const char *name
)
408 DBG("Notify the global application pipe");
410 /* Try opening the global pipe */
411 fd
= open(name
, O_WRONLY
);
416 /* Notify by writing on the pipe */
417 ret
= write(fd
, "42", 2);
427 * Setup the outgoing data buffer for the response (llm) by allocating the
428 * right amount of memory and copying the original information from the lsm
431 * Return total size of the buffer pointed by buf.
433 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
439 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
440 if (cmd_ctx
->llm
== NULL
) {
446 /* Copy common data */
447 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
448 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
450 cmd_ctx
->llm
->data_size
= size
;
451 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
460 * Update the kernel pollfd set of all channel fd available over all tracing
461 * session. Add the wakeup pipe at the end of the set.
463 static int update_kernel_pollfd(void)
467 * The wakup pipe and the quit pipe are needed so the number of fds starts
468 * at 2 for those pipes.
470 unsigned int nb_fd
= 2;
471 struct ltt_session
*session
;
472 struct ltt_kernel_channel
*channel
;
474 DBG("Updating kernel_pollfd");
476 /* Get the number of channel of all kernel session */
478 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
479 lock_session(session
);
480 if (session
->kernel_session
== NULL
) {
481 unlock_session(session
);
484 nb_fd
+= session
->kernel_session
->channel_count
;
485 unlock_session(session
);
488 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
490 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
491 if (kernel_pollfd
== NULL
) {
492 perror("malloc kernel_pollfd");
496 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
497 lock_session(session
);
498 if (session
->kernel_session
== NULL
) {
499 unlock_session(session
);
503 ERR("To much channel for kernel_pollfd size");
504 unlock_session(session
);
507 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
508 kernel_pollfd
[i
].fd
= channel
->fd
;
509 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
512 unlock_session(session
);
514 unlock_session_list();
516 /* Adding wake up pipe */
517 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
518 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
520 /* Adding the quit pipe */
521 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
526 unlock_session_list();
531 * Find the channel fd from 'fd' over all tracing session. When found, check
532 * for new channel stream and send those stream fds to the kernel consumer.
534 * Useful for CPU hotplug feature.
536 static int update_kernel_stream(int fd
)
539 struct ltt_session
*session
;
540 struct ltt_kernel_channel
*channel
;
542 DBG("Updating kernel streams for channel fd %d", fd
);
545 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
546 lock_session(session
);
547 if (session
->kernel_session
== NULL
) {
548 unlock_session(session
);
551 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
552 if (channel
->fd
== fd
) {
553 DBG("Channel found, updating kernel streams");
554 ret
= kernel_open_channel_stream(channel
);
559 * Have we already sent fds to the consumer? If yes, it means that
560 * tracing is started so it is safe to send our updated stream fds.
562 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
563 ret
= send_kconsumerd_channel_fds(kconsumerd_cmd_sock
, channel
);
571 unlock_session(session
);
575 unlock_session_list();
577 unlock_session(session
);
583 * This thread manage event coming from the kernel.
585 * Features supported in this thread:
588 static void *thread_manage_kernel(void *data
)
590 int ret
, i
, nb_fd
= 0;
592 int update_poll_flag
= 1;
594 DBG("Thread manage kernel started");
597 if (update_poll_flag
== 1) {
598 nb_fd
= update_kernel_pollfd();
602 update_poll_flag
= 0;
605 DBG("Polling on %d fds", nb_fd
);
607 /* Poll infinite value of time */
608 ret
= poll(kernel_pollfd
, nb_fd
, -1);
610 perror("poll kernel thread");
612 } else if (ret
== 0) {
613 /* Should not happen since timeout is infinite */
617 /* Thread quit pipe has been closed. Killing thread. */
618 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
622 DBG("Kernel poll event triggered");
625 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
628 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
630 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
631 update_poll_flag
= 1;
639 for (i
= 0; i
< nb_fd
; i
++) {
640 switch (kernel_pollfd
[i
].revents
) {
642 * New CPU detected by the kernel. Adding kernel stream to kernel
643 * session and updating the kernel consumer
645 case POLLIN
| POLLRDNORM
:
646 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
656 DBG("Kernel thread dying");
661 close(kernel_poll_pipe
[0]);
662 close(kernel_poll_pipe
[1]);
667 * This thread manage the kconsumerd error sent back to the session daemon.
669 static void *thread_manage_kconsumerd(void *data
)
672 enum lttcomm_return_code code
;
673 struct pollfd pollfd
[2];
675 DBG("[thread] Manage kconsumerd started");
677 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
682 /* First fd is always the quit pipe */
683 pollfd
[0].fd
= thread_quit_pipe
[0];
686 pollfd
[1].fd
= kconsumerd_err_sock
;
687 pollfd
[1].events
= POLLIN
;
689 /* Inifinite blocking call, waiting for transmission */
690 ret
= poll(pollfd
, 2, -1);
692 perror("poll kconsumerd thread");
696 /* Thread quit pipe has been closed. Killing thread. */
697 if (pollfd
[0].revents
== POLLNVAL
) {
699 } else if (pollfd
[1].revents
== POLLERR
) {
700 ERR("Kconsumerd err socket poll error");
704 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
709 /* Getting status code from kconsumerd */
710 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
715 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
716 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
717 if (kconsumerd_cmd_sock
< 0) {
718 sem_post(&kconsumerd_sem
);
719 perror("kconsumerd connect");
722 /* Signal condition to tell that the kconsumerd is ready */
723 sem_post(&kconsumerd_sem
);
724 DBG("Kconsumerd command socket ready");
726 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
727 lttcomm_get_readable_code(-code
));
731 /* Wait for any kconsumerd error */
732 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
734 ERR("Kconsumerd closed the command socket");
738 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
741 DBG("Kconsumerd thread dying");
742 if (kconsumerd_err_sock
) {
743 close(kconsumerd_err_sock
);
745 if (kconsumerd_cmd_sock
) {
746 close(kconsumerd_cmd_sock
);
752 unlink(kconsumerd_err_unix_sock_path
);
753 unlink(kconsumerd_cmd_unix_sock_path
);
760 * This thread manage the application socket communication
762 static void *thread_manage_apps(void *data
)
765 struct pollfd pollfd
[2];
767 /* TODO: Something more elegant is needed but fine for now */
768 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
769 * for 32-bit vs 64-bit compat processes. */
770 /* replicate in ust with version number */
772 int reg
; /* 1:register, 0:unregister */
777 DBG("[thread] Manage apps started");
779 ret
= lttcomm_listen_unix_sock(apps_sock
);
784 /* First fd is always the quit pipe */
785 pollfd
[0].fd
= thread_quit_pipe
[0];
788 pollfd
[1].fd
= apps_sock
;
789 pollfd
[1].events
= POLLIN
;
791 /* Notify all applications to register */
792 notify_apps(default_global_apps_pipe
);
795 DBG("Accepting application registration");
797 /* Inifinite blocking call, waiting for transmission */
798 ret
= poll(pollfd
, 2, -1);
800 perror("poll apps thread");
804 /* Thread quit pipe has been closed. Killing thread. */
805 if (pollfd
[0].revents
== POLLNVAL
) {
807 } else if (pollfd
[1].revents
== POLLERR
) {
808 ERR("Apps socket poll error");
812 sock
= lttcomm_accept_unix_sock(apps_sock
);
818 * Basic recv here to handle the very simple data
819 * that the libust send to register (reg_msg).
821 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
827 /* Add application to the global traceable list */
828 if (reg_msg
.reg
== 1) {
830 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
832 /* register_traceable_app only return an error with
833 * ENOMEM. At this point, we better stop everything.
839 unregister_traceable_app(reg_msg
.pid
);
844 DBG("Apps thread dying");
852 unlink(apps_unix_sock_path
);
857 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
858 * exec or it will fails.
860 static int spawn_kconsumerd_thread(void)
864 /* Setup semaphore */
865 sem_init(&kconsumerd_sem
, 0, 0);
867 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
869 perror("pthread_create kconsumerd");
873 /* Wait for the kconsumerd thread to be ready */
874 sem_wait(&kconsumerd_sem
);
876 if (kconsumerd_pid
== 0) {
877 ERR("Kconsumerd did not start");
884 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
888 static int join_kconsumerd_thread(void)
893 if (kconsumerd_pid
!= 0) {
894 ret
= kill(kconsumerd_pid
, SIGTERM
);
896 ERR("Error killing kconsumerd");
899 return pthread_join(kconsumerd_thread
, &status
);
906 * Fork and exec a kernel consumer daemon (kconsumerd).
908 * Return pid if successful else -1.
910 static pid_t
spawn_kconsumerd(void)
914 const char *verbosity
;
916 DBG("Spawning kconsumerd");
923 if (opt_verbose
> 1 || opt_verbose_kconsumerd
) {
924 verbosity
= "--verbose";
926 verbosity
= "--quiet";
928 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
930 perror("kernel start consumer exec");
933 } else if (pid
> 0) {
937 perror("kernel start consumer fork");
947 * Spawn the kconsumerd daemon and session daemon thread.
949 static int start_kconsumerd(void)
953 pthread_mutex_lock(&kconsumerd_pid_mutex
);
954 if (kconsumerd_pid
!= 0) {
955 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
959 ret
= spawn_kconsumerd();
961 ERR("Spawning kconsumerd failed");
962 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
963 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
967 /* Setting up the global kconsumerd_pid */
968 kconsumerd_pid
= ret
;
969 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
971 DBG("Kconsumerd pid %d", ret
);
973 DBG("Spawning kconsumerd thread");
974 ret
= spawn_kconsumerd_thread();
976 ERR("Fatal error spawning kconsumerd thread");
988 * modprobe_kernel_modules
990 static int modprobe_kernel_modules(void)
995 for (i
= 0; i
< ARRAY_SIZE(kernel_modules_list
); i
++) {
996 ret
= snprintf(modprobe
, sizeof(modprobe
),
997 "/sbin/modprobe %s%s",
998 kernel_modules_list
[i
].required
? "" : "--quiet ",
999 kernel_modules_list
[i
].name
);
1001 perror("snprintf modprobe");
1004 modprobe
[sizeof(modprobe
) - 1] = '\0';
1005 ret
= system(modprobe
);
1007 ERR("Unable to launch modprobe for module %s",
1008 kernel_modules_list
[i
].name
);
1009 } else if (kernel_modules_list
[i
].required
1010 && WEXITSTATUS(ret
) != 0) {
1011 ERR("Unable to load module %s",
1012 kernel_modules_list
[i
].name
);
1014 DBG("Modprobe successfully %s",
1015 kernel_modules_list
[i
].name
);
1024 * modprobe_remove_kernel_modules
1025 * Remove modules in reverse load order.
1027 static int modprobe_remove_kernel_modules(void)
1032 for (i
= ARRAY_SIZE(kernel_modules_list
) - 1; i
>= 0; i
--) {
1033 ret
= snprintf(modprobe
, sizeof(modprobe
),
1034 "/sbin/modprobe --remove --quiet %s",
1035 kernel_modules_list
[i
].name
);
1037 perror("snprintf modprobe --remove");
1040 modprobe
[sizeof(modprobe
) - 1] = '\0';
1041 ret
= system(modprobe
);
1043 ERR("Unable to launch modprobe --remove for module %s",
1044 kernel_modules_list
[i
].name
);
1045 } else if (kernel_modules_list
[i
].required
1046 && WEXITSTATUS(ret
) != 0) {
1047 ERR("Unable to remove module %s",
1048 kernel_modules_list
[i
].name
);
1050 DBG("Modprobe removal successful %s",
1051 kernel_modules_list
[i
].name
);
1062 static int mount_debugfs(char *path
)
1065 char *type
= "debugfs";
1067 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
, geteuid(), getegid());
1072 ret
= mount(type
, path
, type
, 0, NULL
);
1074 perror("mount debugfs");
1078 DBG("Mounted debugfs successfully at %s", path
);
1085 * Setup necessary data for kernel tracer action.
1087 static void init_kernel_tracer(void)
1090 char *proc_mounts
= "/proc/mounts";
1092 char *debugfs_path
= NULL
, *lttng_path
;
1095 /* Detect debugfs */
1096 fp
= fopen(proc_mounts
, "r");
1098 ERR("Unable to probe %s", proc_mounts
);
1102 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1103 if (strstr(line
, "debugfs") != NULL
) {
1104 /* Remove first string */
1106 /* Dup string here so we can reuse line later on */
1107 debugfs_path
= strdup(strtok(NULL
, " "));
1108 DBG("Got debugfs path : %s", debugfs_path
);
1115 /* Mount debugfs if needded */
1116 if (debugfs_path
== NULL
) {
1117 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1119 perror("asprintf debugfs path");
1122 ret
= mount_debugfs(debugfs_path
);
1128 /* Modprobe lttng kernel modules */
1129 ret
= modprobe_kernel_modules();
1134 /* Setup lttng kernel path */
1135 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1137 perror("asprintf lttng path");
1141 /* Open debugfs lttng */
1142 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1143 if (kernel_tracer_fd
< 0) {
1144 DBG("Failed to open %s", lttng_path
);
1150 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1160 WARN("No kernel tracer available");
1161 kernel_tracer_fd
= 0;
1166 * Start tracing by creating trace directory and sending FDs to the kernel
1169 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1173 if (session
->kconsumer_fds_sent
== 0) {
1174 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
1176 ERR("Send kconsumerd fds failed");
1177 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1181 session
->kconsumer_fds_sent
= 1;
1189 * Notify kernel thread to update it's pollfd.
1191 static int notify_kernel_pollfd(void)
1195 /* Inform kernel thread of the new kernel channel */
1196 ret
= write(kernel_poll_pipe
[1], "!", 1);
1198 perror("write kernel poll pipe");
1205 * Allocate a channel structure and fill it.
1207 static struct lttng_channel
*init_default_channel(char *name
)
1209 struct lttng_channel
*chan
;
1211 chan
= malloc(sizeof(struct lttng_channel
));
1213 perror("init channel malloc");
1217 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1218 perror("snprintf channel name");
1222 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1223 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
1224 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
1225 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1226 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1227 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1234 * Create a kernel tracer session then create the default channel.
1236 static int create_kernel_session(struct ltt_session
*session
)
1240 DBG("Creating kernel session");
1242 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1244 ret
= LTTCOMM_KERN_SESS_FAIL
;
1248 ret
= mkdir_recursive(session
->path
, S_IRWXU
| S_IRWXG
,
1249 geteuid(), allowed_group());
1251 if (ret
!= -EEXIST
) {
1252 ERR("Trace directory creation error");
1262 * Using the session list, filled a lttng_session array to send back to the
1263 * client for session listing.
1265 * The session list lock MUST be acquired before calling this function. Use
1266 * lock_session_list() and unlock_session_list().
1268 static void list_lttng_sessions(struct lttng_session
*sessions
)
1271 struct ltt_session
*session
;
1273 DBG("Getting all available session");
1275 * Iterate over session list and append data after the control struct in
1278 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1279 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1280 sessions
[i
].path
[PATH_MAX
- 1] = '\0';
1281 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1282 sessions
[i
].name
[NAME_MAX
- 1] = '\0';
1288 * Fill lttng_channel array of all channels.
1290 static void list_lttng_channels(struct ltt_session
*session
,
1291 struct lttng_channel
*channels
)
1294 struct ltt_kernel_channel
*kchan
;
1296 DBG("Listing channels for session %s", session
->name
);
1298 /* Kernel channels */
1299 if (session
->kernel_session
!= NULL
) {
1300 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1301 /* Copy lttng_channel struct to array */
1302 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1303 channels
[i
].enabled
= kchan
->enabled
;
1308 /* TODO: Missing UST listing */
1312 * Fill lttng_event array of all events in the channel.
1314 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1315 struct lttng_event
*events
)
1318 * TODO: This is ONLY kernel. Need UST support.
1321 struct ltt_kernel_event
*event
;
1323 DBG("Listing events for channel %s", kchan
->channel
->name
);
1325 /* Kernel channels */
1326 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1327 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1328 events
[i
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1329 events
[i
].enabled
= event
->enabled
;
1330 switch (event
->event
->instrumentation
) {
1331 case LTTNG_KERNEL_TRACEPOINT
:
1332 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1334 case LTTNG_KERNEL_KPROBE
:
1335 case LTTNG_KERNEL_KRETPROBE
:
1336 events
[i
].type
= LTTNG_EVENT_PROBE
;
1337 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1338 sizeof(struct lttng_kernel_kprobe
));
1340 case LTTNG_KERNEL_FUNCTION
:
1341 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1342 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1343 sizeof(struct lttng_kernel_function
));
1351 * Process the command requested by the lttng client within the command
1352 * context structure. This function make sure that the return structure (llm)
1353 * is set and ready for transmission before returning.
1355 * Return any error encountered or 0 for success.
1357 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1359 int ret
= LTTCOMM_OK
;
1361 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1363 /* Listing commands don't need a session */
1364 switch (cmd_ctx
->lsm
->cmd_type
) {
1365 case LTTNG_CREATE_SESSION
:
1366 case LTTNG_LIST_SESSIONS
:
1367 case LTTNG_LIST_TRACEPOINTS
:
1368 case LTTNG_CALIBRATE
:
1371 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1372 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1373 if (cmd_ctx
->session
== NULL
) {
1374 /* If session name not found */
1375 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1376 ret
= LTTCOMM_SESS_NOT_FOUND
;
1377 } else { /* If no session name specified */
1378 ret
= LTTCOMM_SELECT_SESS
;
1382 /* Acquire lock for the session */
1383 lock_session(cmd_ctx
->session
);
1389 * Check domain type for specific "pre-action".
1391 switch (cmd_ctx
->lsm
->domain
.type
) {
1392 case LTTNG_DOMAIN_KERNEL
:
1393 /* Kernel tracer check */
1394 if (kernel_tracer_fd
== 0) {
1395 init_kernel_tracer();
1396 if (kernel_tracer_fd
== 0) {
1397 ret
= LTTCOMM_KERN_NA
;
1402 /* Need a session for kernel command */
1403 switch (cmd_ctx
->lsm
->cmd_type
) {
1404 case LTTNG_CREATE_SESSION
:
1405 case LTTNG_LIST_SESSIONS
:
1406 case LTTNG_LIST_TRACEPOINTS
:
1407 case LTTNG_CALIBRATE
:
1410 if (cmd_ctx
->session
->kernel_session
== NULL
) {
1411 ret
= create_kernel_session(cmd_ctx
->session
);
1413 ret
= LTTCOMM_KERN_SESS_FAIL
;
1417 /* Start the kernel consumer daemon */
1418 if (kconsumerd_pid
== 0) {
1419 ret
= start_kconsumerd();
1431 /* Process by command type */
1432 switch (cmd_ctx
->lsm
->cmd_type
) {
1433 case LTTNG_ADD_CONTEXT
:
1435 struct lttng_kernel_context kctx
;
1437 /* Setup lttng message with no payload */
1438 ret
= setup_lttng_msg(cmd_ctx
, 0);
1443 switch (cmd_ctx
->lsm
->domain
.type
) {
1444 case LTTNG_DOMAIN_KERNEL
:
1445 /* Create Kernel context */
1446 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1447 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1448 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1449 strncpy(kctx
.u
.perf_counter
.name
,
1450 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1451 LTTNG_SYMBOL_NAME_LEN
);
1452 kctx
.u
.perf_counter
.name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1454 /* Add kernel context to kernel tracer. See context.c */
1455 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1456 cmd_ctx
->lsm
->u
.context
.event_name
,
1457 cmd_ctx
->lsm
->u
.context
.channel_name
);
1458 if (ret
!= LTTCOMM_OK
) {
1463 /* TODO: Userspace tracing */
1464 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1471 case LTTNG_DISABLE_CHANNEL
:
1473 struct ltt_kernel_channel
*kchan
;
1475 /* Setup lttng message with no payload */
1476 ret
= setup_lttng_msg(cmd_ctx
, 0);
1481 switch (cmd_ctx
->lsm
->domain
.type
) {
1482 case LTTNG_DOMAIN_KERNEL
:
1483 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1484 cmd_ctx
->session
->kernel_session
);
1485 if (kchan
== NULL
) {
1486 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1488 } else if (kchan
->enabled
== 1) {
1489 ret
= kernel_disable_channel(kchan
);
1491 if (ret
!= EEXIST
) {
1492 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1497 kernel_wait_quiescent(kernel_tracer_fd
);
1500 /* TODO: Userspace tracing */
1501 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1508 case LTTNG_DISABLE_EVENT
:
1510 struct ltt_kernel_channel
*kchan
;
1511 struct ltt_kernel_event
*kevent
;
1513 /* Setup lttng message with no payload */
1514 ret
= setup_lttng_msg(cmd_ctx
, 0);
1519 switch (cmd_ctx
->lsm
->domain
.type
) {
1520 case LTTNG_DOMAIN_KERNEL
:
1521 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1522 cmd_ctx
->session
->kernel_session
);
1523 if (kchan
== NULL
) {
1524 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1528 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1529 if (kevent
!= NULL
) {
1530 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1531 kchan
->channel
->name
);
1532 ret
= kernel_disable_event(kevent
);
1534 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1539 kernel_wait_quiescent(kernel_tracer_fd
);
1542 /* TODO: Userspace tracing */
1543 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1550 case LTTNG_DISABLE_ALL_EVENT
:
1552 struct ltt_kernel_channel
*kchan
;
1553 struct ltt_kernel_event
*kevent
;
1555 /* Setup lttng message with no payload */
1556 ret
= setup_lttng_msg(cmd_ctx
, 0);
1561 switch (cmd_ctx
->lsm
->domain
.type
) {
1562 case LTTNG_DOMAIN_KERNEL
:
1563 DBG("Disabling all enabled kernel events");
1564 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1565 cmd_ctx
->session
->kernel_session
);
1566 if (kchan
== NULL
) {
1567 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1571 /* For each event in the kernel session */
1572 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1573 DBG("Disabling kernel event %s for channel %s.",
1574 kevent
->event
->name
, kchan
->channel
->name
);
1575 ret
= kernel_disable_event(kevent
);
1581 /* Quiescent wait after event disable */
1582 kernel_wait_quiescent(kernel_tracer_fd
);
1585 /* TODO: Userspace tracing */
1586 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1593 case LTTNG_ENABLE_CHANNEL
:
1595 struct ltt_kernel_channel
*kchan
;
1597 /* Setup lttng message with no payload */
1598 ret
= setup_lttng_msg(cmd_ctx
, 0);
1603 switch (cmd_ctx
->lsm
->domain
.type
) {
1604 case LTTNG_DOMAIN_KERNEL
:
1605 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1606 cmd_ctx
->session
->kernel_session
);
1607 if (kchan
== NULL
) {
1608 /* Channel not found, creating it */
1609 DBG("Creating kernel channel");
1611 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1612 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1614 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1618 /* Notify kernel thread that there is a new channel */
1619 ret
= notify_kernel_pollfd();
1621 ret
= LTTCOMM_FATAL
;
1624 } else if (kchan
->enabled
== 0) {
1625 ret
= kernel_enable_channel(kchan
);
1627 if (ret
!= EEXIST
) {
1628 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1634 kernel_wait_quiescent(kernel_tracer_fd
);
1637 /* TODO: Userspace tracing */
1638 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1645 case LTTNG_ENABLE_EVENT
:
1648 struct ltt_kernel_channel
*kchan
;
1649 struct ltt_kernel_event
*kevent
;
1650 struct lttng_channel
*chan
;
1652 /* Setup lttng message with no payload */
1653 ret
= setup_lttng_msg(cmd_ctx
, 0);
1658 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1660 switch (cmd_ctx
->lsm
->domain
.type
) {
1661 case LTTNG_DOMAIN_KERNEL
:
1663 kchan
= get_kernel_channel_by_name(channel_name
,
1664 cmd_ctx
->session
->kernel_session
);
1665 if (kchan
== NULL
) {
1666 DBG("Channel not found. Creating channel %s", channel_name
);
1668 chan
= init_default_channel(channel_name
);
1670 ret
= LTTCOMM_FATAL
;
1674 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1675 chan
, cmd_ctx
->session
->path
);
1677 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1681 } while (kchan
== NULL
);
1683 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
1684 if (kevent
== NULL
) {
1685 DBG("Creating kernel event %s for channel %s.",
1686 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
1687 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
1689 DBG("Enabling kernel event %s for channel %s.",
1690 kevent
->event
->name
, channel_name
);
1691 ret
= kernel_enable_event(kevent
);
1692 if (ret
== -EEXIST
) {
1693 ret
= LTTCOMM_KERN_EVENT_EXIST
;
1699 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1703 kernel_wait_quiescent(kernel_tracer_fd
);
1706 /* TODO: Userspace tracing */
1707 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1713 case LTTNG_ENABLE_ALL_EVENT
:
1717 struct ltt_kernel_channel
*kchan
;
1718 struct ltt_kernel_event
*kevent
;
1719 struct lttng_event
*event_list
;
1720 struct lttng_channel
*chan
;
1722 /* Setup lttng message with no payload */
1723 ret
= setup_lttng_msg(cmd_ctx
, 0);
1728 DBG("Enabling all kernel event");
1730 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1732 switch (cmd_ctx
->lsm
->domain
.type
) {
1733 case LTTNG_DOMAIN_KERNEL
:
1735 kchan
= get_kernel_channel_by_name(channel_name
,
1736 cmd_ctx
->session
->kernel_session
);
1737 if (kchan
== NULL
) {
1738 DBG("Channel not found. Creating channel %s", channel_name
);
1740 chan
= init_default_channel(channel_name
);
1742 ret
= LTTCOMM_FATAL
;
1746 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1747 chan
, cmd_ctx
->session
->path
);
1749 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1753 } while (kchan
== NULL
);
1755 /* For each event in the kernel session */
1756 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1757 DBG("Enabling kernel event %s for channel %s.",
1758 kevent
->event
->name
, channel_name
);
1759 ret
= kernel_enable_event(kevent
);
1765 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1767 ret
= LTTCOMM_KERN_LIST_FAIL
;
1771 for (i
= 0; i
< size
; i
++) {
1772 kevent
= get_kernel_event_by_name(event_list
[i
].name
, kchan
);
1773 if (kevent
== NULL
) {
1774 /* Default event type for enable all */
1775 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1776 /* Enable each single tracepoint event */
1777 ret
= kernel_create_event(&event_list
[i
], kchan
);
1779 /* Ignore error here and continue */
1786 /* Quiescent wait after event enable */
1787 kernel_wait_quiescent(kernel_tracer_fd
);
1790 /* TODO: Userspace tracing */
1791 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1798 case LTTNG_LIST_TRACEPOINTS
:
1800 struct lttng_event
*events
;
1801 ssize_t nb_events
= 0;
1803 switch (cmd_ctx
->lsm
->domain
.type
) {
1804 case LTTNG_DOMAIN_KERNEL
:
1805 DBG("Listing kernel events");
1806 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
1807 if (nb_events
< 0) {
1808 ret
= LTTCOMM_KERN_LIST_FAIL
;
1813 /* TODO: Userspace listing */
1814 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1819 * Setup lttng message with payload size set to the event list size in
1820 * bytes and then copy list into the llm payload.
1822 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
1828 /* Copy event list into message payload */
1829 memcpy(cmd_ctx
->llm
->payload
, events
,
1830 sizeof(struct lttng_event
) * nb_events
);
1837 case LTTNG_START_TRACE
:
1839 struct ltt_kernel_channel
*chan
;
1841 /* Setup lttng message with no payload */
1842 ret
= setup_lttng_msg(cmd_ctx
, 0);
1847 /* Kernel tracing */
1848 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1849 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1850 DBG("Open kernel metadata");
1851 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1852 cmd_ctx
->session
->path
);
1854 ret
= LTTCOMM_KERN_META_FAIL
;
1859 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1860 DBG("Opening kernel metadata stream");
1861 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1862 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1864 ERR("Kernel create metadata stream failed");
1865 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1871 /* For each channel */
1872 cds_list_for_each_entry(chan
,
1873 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1874 if (chan
->stream_count
== 0) {
1875 ret
= kernel_open_channel_stream(chan
);
1877 ERR("Kernel create channel stream failed");
1878 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1881 /* Update the stream global counter */
1882 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1886 DBG("Start kernel tracing");
1887 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1889 ERR("Kernel start session failed");
1890 ret
= LTTCOMM_KERN_START_FAIL
;
1894 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1896 ret
= LTTCOMM_KERN_START_FAIL
;
1900 /* Quiescent wait after starting trace */
1901 kernel_wait_quiescent(kernel_tracer_fd
);
1904 /* TODO: Start all UST traces */
1909 case LTTNG_STOP_TRACE
:
1911 struct ltt_kernel_channel
*chan
;
1912 /* Setup lttng message with no payload */
1913 ret
= setup_lttng_msg(cmd_ctx
, 0);
1919 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1920 DBG("Stop kernel tracing");
1922 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1924 ERR("Kernel metadata flush failed");
1927 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1928 ret
= kernel_flush_buffer(chan
);
1930 ERR("Kernel flush buffer error");
1934 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1936 ERR("Kernel stop session failed");
1937 ret
= LTTCOMM_KERN_STOP_FAIL
;
1941 /* Quiescent wait after stopping trace */
1942 kernel_wait_quiescent(kernel_tracer_fd
);
1945 /* TODO : User-space tracer */
1950 case LTTNG_CREATE_SESSION
:
1952 /* Setup lttng message with no payload */
1953 ret
= setup_lttng_msg(cmd_ctx
, 0);
1958 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
1960 if (ret
== -EEXIST
) {
1961 ret
= LTTCOMM_EXIST_SESS
;
1963 ret
= LTTCOMM_FATAL
;
1971 case LTTNG_DESTROY_SESSION
:
1973 /* Setup lttng message with no payload */
1974 ret
= setup_lttng_msg(cmd_ctx
, 0);
1979 /* Clean kernel session teardown */
1980 teardown_kernel_session(cmd_ctx
->session
);
1982 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
1984 ret
= LTTCOMM_FATAL
;
1989 * Must notify the kernel thread here to update it's pollfd in order to
1990 * remove the channel(s)' fd just destroyed.
1992 ret
= notify_kernel_pollfd();
1994 ret
= LTTCOMM_FATAL
;
2001 case LTTNG_LIST_DOMAINS
:
2005 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2009 nb_dom
+= cmd_ctx
->session
->ust_trace_count
;
2011 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
2016 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
2017 LTTNG_DOMAIN_KERNEL
;
2019 /* TODO: User-space tracer domain support */
2023 case LTTNG_LIST_CHANNELS
:
2026 * TODO: Only kernel channels are listed here. UST listing
2027 * is needed on lttng-ust 2.0 release.
2030 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2031 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
2034 ret
= setup_lttng_msg(cmd_ctx
,
2035 sizeof(struct lttng_channel
) * nb_chan
);
2040 list_lttng_channels(cmd_ctx
->session
,
2041 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
2046 case LTTNG_LIST_EVENTS
:
2049 * TODO: Only kernel events are listed here. UST listing
2050 * is needed on lttng-ust 2.0 release.
2052 size_t nb_event
= 0;
2053 struct ltt_kernel_channel
*kchan
= NULL
;
2055 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
2056 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
2057 cmd_ctx
->session
->kernel_session
);
2058 if (kchan
== NULL
) {
2059 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
2062 nb_event
+= kchan
->event_count
;
2065 ret
= setup_lttng_msg(cmd_ctx
,
2066 sizeof(struct lttng_event
) * nb_event
);
2071 DBG("Listing events (%zu events)", nb_event
);
2073 list_lttng_events(kchan
,
2074 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
2079 case LTTNG_LIST_SESSIONS
:
2081 lock_session_list();
2083 if (session_list_ptr
->count
== 0) {
2084 ret
= LTTCOMM_NO_SESSION
;
2085 unlock_session_list();
2089 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
2090 session_list_ptr
->count
);
2092 unlock_session_list();
2096 /* Filled the session array */
2097 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
2099 unlock_session_list();
2105 case LTTNG_CALIBRATE
:
2107 /* Setup lttng message with no payload */
2108 ret
= setup_lttng_msg(cmd_ctx
, 0);
2113 switch (cmd_ctx
->lsm
->domain
.type
) {
2114 case LTTNG_DOMAIN_KERNEL
:
2116 struct lttng_kernel_calibrate kcalibrate
;
2118 kcalibrate
.type
= cmd_ctx
->lsm
->u
.calibrate
.type
;
2119 ret
= kernel_calibrate(kernel_tracer_fd
, &kcalibrate
);
2121 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
2127 /* TODO: Userspace tracing */
2128 ret
= LTTCOMM_NOT_IMPLEMENTED
;
2136 /* Undefined command */
2137 ret
= setup_lttng_msg(cmd_ctx
, 0);
2146 /* Set return code */
2147 cmd_ctx
->llm
->ret_code
= ret
;
2149 if (cmd_ctx
->session
) {
2150 unlock_session(cmd_ctx
->session
);
2156 if (cmd_ctx
->llm
== NULL
) {
2157 DBG("Missing llm structure. Allocating one.");
2158 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2162 /* Notify client of error */
2163 cmd_ctx
->llm
->ret_code
= ret
;
2166 if (cmd_ctx
->session
) {
2167 unlock_session(cmd_ctx
->session
);
2173 * This thread manage all clients request using the unix client socket for
2176 static void *thread_manage_clients(void *data
)
2179 struct command_ctx
*cmd_ctx
= NULL
;
2180 struct pollfd pollfd
[2];
2182 DBG("[thread] Manage client started");
2184 ret
= lttcomm_listen_unix_sock(client_sock
);
2189 /* First fd is always the quit pipe */
2190 pollfd
[0].fd
= thread_quit_pipe
[0];
2193 pollfd
[1].fd
= client_sock
;
2194 pollfd
[1].events
= POLLIN
;
2196 /* Notify parent pid that we are ready
2197 * to accept command for client side.
2199 if (opt_sig_parent
) {
2200 kill(ppid
, SIGCHLD
);
2204 DBG("Accepting client command ...");
2206 /* Inifinite blocking call, waiting for transmission */
2207 ret
= poll(pollfd
, 2, -1);
2209 perror("poll client thread");
2213 /* Thread quit pipe has been closed. Killing thread. */
2214 if (pollfd
[0].revents
== POLLNVAL
) {
2216 } else if (pollfd
[1].revents
== POLLERR
) {
2217 ERR("Client socket poll error");
2221 sock
= lttcomm_accept_unix_sock(client_sock
);
2226 /* Allocate context command to process the client request */
2227 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2229 /* Allocate data buffer for reception */
2230 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2231 cmd_ctx
->llm
= NULL
;
2232 cmd_ctx
->session
= NULL
;
2235 * Data is received from the lttng client. The struct
2236 * lttcomm_session_msg (lsm) contains the command and data request of
2239 DBG("Receiving data from client ...");
2240 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2245 // TODO: Validate cmd_ctx including sanity check for security purpose.
2248 * This function dispatch the work to the kernel or userspace tracer
2249 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2250 * informations for the client. The command context struct contains
2251 * everything this function may needs.
2253 ret
= process_client_msg(cmd_ctx
);
2255 /* TODO: Inform client somehow of the fatal error. At this point,
2256 * ret < 0 means that a malloc failed (ENOMEM). */
2257 /* Error detected but still accept command */
2258 clean_command_ctx(&cmd_ctx
);
2262 DBG("Sending response (size: %d, retcode: %d)",
2263 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2264 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2266 ERR("Failed to send data back to client");
2269 clean_command_ctx(&cmd_ctx
);
2271 /* End of transmission */
2276 DBG("Client thread dying");
2284 unlink(client_unix_sock_path
);
2286 clean_command_ctx(&cmd_ctx
);
2292 * usage function on stderr
2294 static void usage(void)
2296 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2297 fprintf(stderr
, " -h, --help Display this usage.\n");
2298 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2299 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2300 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2301 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2302 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2303 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2304 fprintf(stderr
, " -V, --version Show version number.\n");
2305 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2306 fprintf(stderr
, " -q, --quiet No output at all.\n");
2307 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2308 fprintf(stderr
, " --verbose-kconsumerd Verbose mode for kconsumerd. Activate DBG() macro.\n");
2312 * daemon argument parsing
2314 static int parse_args(int argc
, char **argv
)
2318 static struct option long_options
[] = {
2319 { "client-sock", 1, 0, 'c' },
2320 { "apps-sock", 1, 0, 'a' },
2321 { "kconsumerd-cmd-sock", 1, 0, 0 },
2322 { "kconsumerd-err-sock", 1, 0, 0 },
2323 { "daemonize", 0, 0, 'd' },
2324 { "sig-parent", 0, 0, 'S' },
2325 { "help", 0, 0, 'h' },
2326 { "group", 1, 0, 'g' },
2327 { "version", 0, 0, 'V' },
2328 { "quiet", 0, 0, 'q' },
2329 { "verbose", 0, 0, 'v' },
2330 { "verbose-kconsumerd", 0, 0, 'Z' },
2335 int option_index
= 0;
2336 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:Z", long_options
, &option_index
);
2343 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2345 fprintf(stderr
, " with arg %s\n", optarg
);
2349 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2352 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2358 opt_tracing_group
= strdup(optarg
);
2364 fprintf(stdout
, "%s\n", VERSION
);
2370 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2373 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2379 /* Verbose level can increase using multiple -v */
2383 opt_verbose_kconsumerd
+= 1;
2386 /* Unknown option or other error.
2387 * Error is printed by getopt, just return */
2396 * Creates the two needed socket by the daemon.
2397 * apps_sock - The communication socket for all UST apps.
2398 * client_sock - The communication of the cli tool (lttng).
2400 static int init_daemon_socket(void)
2405 old_umask
= umask(0);
2407 /* Create client tool unix socket */
2408 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2409 if (client_sock
< 0) {
2410 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2415 /* File permission MUST be 660 */
2416 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2418 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2423 /* Create the application unix socket */
2424 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2425 if (apps_sock
< 0) {
2426 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2431 /* File permission MUST be 666 */
2432 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2434 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2445 * Check if the global socket is available, and if a daemon is answering
2446 * at the other side. If yes, error is returned.
2448 static int check_existing_daemon(void)
2450 if (access(client_unix_sock_path
, F_OK
) < 0 &&
2451 access(apps_unix_sock_path
, F_OK
) < 0)
2453 /* Is there anybody out there ? */
2454 if (lttng_session_daemon_alive())
2461 * Set the tracing group gid onto the client socket.
2463 * Race window between mkdir and chown is OK because we are going from more
2464 * permissive (root.root) to les permissive (root.tracing).
2466 static int set_permissions(void)
2471 gid
= allowed_group();
2474 WARN("No tracing group detected");
2477 ERR("Missing tracing group. Aborting execution.");
2483 /* Set lttng run dir */
2484 ret
= chown(LTTNG_RUNDIR
, 0, gid
);
2486 ERR("Unable to set group on " LTTNG_RUNDIR
);
2490 /* lttng client socket path */
2491 ret
= chown(client_unix_sock_path
, 0, gid
);
2493 ERR("Unable to set group on %s", client_unix_sock_path
);
2497 /* kconsumerd error socket path */
2498 ret
= chown(kconsumerd_err_unix_sock_path
, 0, gid
);
2500 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2504 DBG("All permissions are set");
2511 * Create the pipe used to wake up the kernel thread.
2513 static int create_kernel_poll_pipe(void)
2515 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2519 * Create the lttng run directory needed for all global sockets and pipe.
2521 static int create_lttng_rundir(void)
2525 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2527 if (errno
!= EEXIST
) {
2528 ERR("Unable to create " LTTNG_RUNDIR
);
2540 * Setup sockets and directory needed by the kconsumerd communication with the
2543 static int set_kconsumerd_sockets(void)
2547 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2548 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2551 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2552 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2555 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2557 if (errno
!= EEXIST
) {
2558 ERR("Failed to create " KCONSUMERD_PATH
);
2564 /* Create the kconsumerd error unix socket */
2565 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2566 if (kconsumerd_err_sock
< 0) {
2567 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2572 /* File permission MUST be 660 */
2573 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2575 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2585 * Signal handler for the daemon
2587 * Simply stop all worker threads, leaving main() return gracefully
2588 * after joining all threads and calling cleanup().
2590 static void sighandler(int sig
)
2594 DBG("SIGPIPE catched");
2597 DBG("SIGINT catched");
2601 DBG("SIGTERM catched");
2610 * Setup signal handler for :
2611 * SIGINT, SIGTERM, SIGPIPE
2613 static int set_signal_handler(void)
2616 struct sigaction sa
;
2619 if ((ret
= sigemptyset(&sigset
)) < 0) {
2620 perror("sigemptyset");
2624 sa
.sa_handler
= sighandler
;
2625 sa
.sa_mask
= sigset
;
2627 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2628 perror("sigaction");
2632 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2633 perror("sigaction");
2637 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2638 perror("sigaction");
2642 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2648 * Set open files limit to unlimited. This daemon can open a large number of
2649 * file descriptors in order to consumer multiple kernel traces.
2651 static void set_ulimit(void)
2656 /* The kernel does not allowed an infinite limit for open files */
2657 lim
.rlim_cur
= 65535;
2658 lim
.rlim_max
= 65535;
2660 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2662 perror("failed to set open files limit");
2669 int main(int argc
, char **argv
)
2673 const char *home_path
;
2675 /* Create thread quit pipe */
2676 if ((ret
= init_thread_quit_pipe()) < 0) {
2680 /* Parse arguments */
2682 if ((ret
= parse_args(argc
, argv
) < 0)) {
2695 /* Check if daemon is UID = 0 */
2696 is_root
= !getuid();
2699 ret
= create_lttng_rundir();
2704 if (strlen(apps_unix_sock_path
) == 0) {
2705 snprintf(apps_unix_sock_path
, PATH_MAX
,
2706 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2709 if (strlen(client_unix_sock_path
) == 0) {
2710 snprintf(client_unix_sock_path
, PATH_MAX
,
2711 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2714 home_path
= get_home_dir();
2715 if (home_path
== NULL
) {
2716 /* TODO: Add --socket PATH option */
2717 ERR("Can't get HOME directory for sockets creation.");
2722 if (strlen(apps_unix_sock_path
) == 0) {
2723 snprintf(apps_unix_sock_path
, PATH_MAX
,
2724 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2727 /* Set the cli tool unix socket path */
2728 if (strlen(client_unix_sock_path
) == 0) {
2729 snprintf(client_unix_sock_path
, PATH_MAX
,
2730 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2734 DBG("Client socket path %s", client_unix_sock_path
);
2735 DBG("Application socket path %s", apps_unix_sock_path
);
2738 * See if daemon already exist.
2740 if ((ret
= check_existing_daemon()) < 0) {
2741 ERR("Already running daemon.\n");
2743 * We do not goto exit because we must not cleanup()
2744 * because a daemon is already running.
2749 /* After this point, we can safely call cleanup() so goto error is used */
2752 * These actions must be executed as root. We do that *after* setting up
2753 * the sockets path because we MUST make the check for another daemon using
2754 * those paths *before* trying to set the kernel consumer sockets and init
2758 ret
= set_kconsumerd_sockets();
2763 /* Setup kernel tracer */
2764 init_kernel_tracer();
2766 /* Set ulimit for open files */
2770 if ((ret
= set_signal_handler()) < 0) {
2774 /* Setup the needed unix socket */
2775 if ((ret
= init_daemon_socket()) < 0) {
2779 /* Set credentials to socket */
2780 if (is_root
&& ((ret
= set_permissions()) < 0)) {
2784 /* Get parent pid if -S, --sig-parent is specified. */
2785 if (opt_sig_parent
) {
2789 /* Setup the kernel pipe for waking up the kernel thread */
2790 if ((ret
= create_kernel_poll_pipe()) < 0) {
2795 * Get session list pointer. This pointer MUST NOT be free().
2796 * This list is statically declared in session.c
2798 session_list_ptr
= get_session_list();
2800 /* Create thread to manage the client socket */
2801 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2803 perror("pthread_create");
2807 /* Create thread to manage application socket */
2808 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2810 perror("pthread_create");
2814 /* Create kernel thread to manage kernel event */
2815 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2817 perror("pthread_create");
2821 ret
= pthread_join(kernel_thread
, &status
);
2823 perror("pthread_join");
2824 goto error
; /* join error, exit without cleanup */
2828 ret
= pthread_join(apps_thread
, &status
);
2830 perror("pthread_join");
2831 goto error
; /* join error, exit without cleanup */
2835 ret
= pthread_join(client_thread
, &status
);
2837 perror("pthread_join");
2838 goto error
; /* join error, exit without cleanup */
2841 ret
= join_kconsumerd_thread();
2843 perror("join_kconsumerd");
2844 goto error
; /* join error, exit without cleanup */
2850 * cleanup() is called when no other thread is running.