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 */
103 * Pointer initialized before thread creation.
105 * This points to the tracing session list containing the session count and a
106 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
107 * MUST NOT be taken if you call a public function in session.c.
109 * The lock is nested inside the structure: session_list_ptr->lock. Please use
110 * lock_session_list and unlock_session_list for lock acquisition.
112 static struct ltt_session_list
*session_list_ptr
;
117 * Return -1 on error or 0 if all pipes are created.
119 static int init_thread_quit_pipe(void)
123 ret
= pipe2(thread_quit_pipe
, O_CLOEXEC
);
125 perror("thread quit pipe");
134 * Complete teardown of a kernel session. This free all data structure related
135 * to a kernel session and update counter.
137 static void teardown_kernel_session(struct ltt_session
*session
)
139 if (session
->kernel_session
!= NULL
) {
140 DBG("Tearing down kernel session");
141 trace_destroy_kernel_session(session
->kernel_session
);
142 /* Extra precaution */
143 session
->kernel_session
= NULL
;
150 static void cleanup()
154 struct ltt_session
*sess
, *stmp
;
159 MSG("\n%c[%d;%dm*** assert failed *** ==> %c[%dm%c[%d;%dm"
160 "Matthew, BEET driven development works!%c[%dm",
161 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
164 /* Stopping all threads */
165 DBG("Terminating all threads");
166 close(thread_quit_pipe
[0]);
167 close(thread_quit_pipe
[1]);
169 DBG("Removing %s directory", LTTNG_RUNDIR
);
170 ret
= asprintf(&cmd
, "rm -rf " LTTNG_RUNDIR
);
172 ERR("asprintf failed. Something is really wrong!");
175 /* Remove lttng run directory */
178 ERR("Unable to clean " LTTNG_RUNDIR
);
181 DBG("Cleaning up all session");
183 /* Destroy session list mutex */
184 if (session_list_ptr
!= NULL
) {
185 pthread_mutex_destroy(&session_list_ptr
->lock
);
187 /* Cleanup ALL session */
188 cds_list_for_each_entry_safe(sess
, stmp
, &session_list_ptr
->head
, list
) {
189 teardown_kernel_session(sess
);
190 // TODO complete session cleanup (including UST)
194 pthread_mutex_destroy(&kconsumerd_pid_mutex
);
196 DBG("Closing kernel fd");
197 close(kernel_tracer_fd
);
201 * Send data on a unix socket using the liblttsessiondcomm API.
203 * Return lttcomm error code.
205 static int send_unix_sock(int sock
, void *buf
, size_t len
)
207 /* Check valid length */
212 return lttcomm_send_unix_sock(sock
, buf
, len
);
216 * Free memory of a command context structure.
218 static void clean_command_ctx(struct command_ctx
**cmd_ctx
)
220 DBG("Clean command context structure");
222 if ((*cmd_ctx
)->llm
) {
223 free((*cmd_ctx
)->llm
);
225 if ((*cmd_ctx
)->lsm
) {
226 free((*cmd_ctx
)->lsm
);
234 * Send all stream fds of kernel channel to the consumer.
236 static int send_kconsumerd_channel_fds(int sock
, struct ltt_kernel_channel
*channel
)
240 struct ltt_kernel_stream
*stream
;
241 struct lttcomm_kconsumerd_header lkh
;
242 struct lttcomm_kconsumerd_msg lkm
;
244 DBG("Sending fds of channel %s to kernel consumer", channel
->channel
->name
);
246 nb_fd
= channel
->stream_count
;
249 lkh
.payload_size
= nb_fd
* sizeof(struct lttcomm_kconsumerd_msg
);
250 lkh
.cmd_type
= ADD_STREAM
;
252 DBG("Sending kconsumerd header");
254 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
256 perror("send kconsumerd header");
260 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
261 if (stream
->fd
!= 0) {
263 lkm
.state
= stream
->state
;
264 lkm
.max_sb_size
= channel
->channel
->attr
.subbuf_size
;
265 strncpy(lkm
.path_name
, stream
->pathname
, PATH_MAX
);
267 DBG("Sending fd %d to kconsumerd", lkm
.fd
);
269 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
271 perror("send kconsumerd fd");
277 DBG("Kconsumerd channel fds sent");
286 * Send all stream fds of the kernel session to the consumer.
288 static int send_kconsumerd_fds(int sock
, struct ltt_kernel_session
*session
)
291 struct ltt_kernel_channel
*chan
;
292 struct lttcomm_kconsumerd_header lkh
;
293 struct lttcomm_kconsumerd_msg lkm
;
296 lkh
.payload_size
= sizeof(struct lttcomm_kconsumerd_msg
);
297 lkh
.cmd_type
= ADD_STREAM
;
299 DBG("Sending kconsumerd header for metadata");
301 ret
= lttcomm_send_unix_sock(sock
, &lkh
, sizeof(struct lttcomm_kconsumerd_header
));
303 perror("send kconsumerd header");
307 DBG("Sending metadata stream fd");
309 if (session
->metadata_stream_fd
!= 0) {
310 /* Send metadata stream fd first */
311 lkm
.fd
= session
->metadata_stream_fd
;
312 lkm
.state
= ACTIVE_FD
;
313 lkm
.max_sb_size
= session
->metadata
->conf
->attr
.subbuf_size
;
314 strncpy(lkm
.path_name
, session
->metadata
->pathname
, PATH_MAX
);
316 ret
= lttcomm_send_fds_unix_sock(sock
, &lkm
, &lkm
.fd
, 1, sizeof(lkm
));
318 perror("send kconsumerd fd");
323 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
324 ret
= send_kconsumerd_channel_fds(sock
, chan
);
330 DBG("Kconsumerd fds (metadata and channel streams) sent");
340 * Return a socket connected to the libust communication socket of the
341 * application identified by the pid.
343 * If the pid is not found in the traceable list, return -1 to indicate error.
345 static int ust_connect_app(pid_t pid
)
348 struct ltt_traceable_app
*lta
;
350 DBG("Connect to application pid %d", pid
);
352 lta
= find_app_by_pid(pid
);
355 DBG("Application pid %d not found", pid
);
359 sock
= ustctl_connect_pid(lta
->pid
);
361 ERR("Fail connecting to the PID %d", pid
);
366 #endif /* DISABLED */
369 * Notify apps by writing 42 to a named pipe using name. Every applications
370 * waiting for a ltt-sessiond will be notified and re-register automatically to
371 * the session daemon.
373 * Return open or write error value.
375 static int notify_apps(const char *name
)
380 DBG("Notify the global application pipe");
382 /* Try opening the global pipe */
383 fd
= open(name
, O_WRONLY
);
388 /* Notify by writing on the pipe */
389 ret
= write(fd
, "42", 2);
399 * Setup the outgoing data buffer for the response (llm) by allocating the
400 * right amount of memory and copying the original information from the lsm
403 * Return total size of the buffer pointed by buf.
405 static int setup_lttng_msg(struct command_ctx
*cmd_ctx
, size_t size
)
411 cmd_ctx
->llm
= malloc(sizeof(struct lttcomm_lttng_msg
) + buf_size
);
412 if (cmd_ctx
->llm
== NULL
) {
418 /* Copy common data */
419 cmd_ctx
->llm
->cmd_type
= cmd_ctx
->lsm
->cmd_type
;
420 cmd_ctx
->llm
->pid
= cmd_ctx
->lsm
->domain
.attr
.pid
;
422 cmd_ctx
->llm
->data_size
= size
;
423 cmd_ctx
->lttng_msg_size
= sizeof(struct lttcomm_lttng_msg
) + buf_size
;
432 * Update the kernel pollfd set of all channel fd available over all tracing
433 * session. Add the wakeup pipe at the end of the set.
435 static int update_kernel_pollfd(void)
439 * The wakup pipe and the quit pipe are needed so the number of fds starts
440 * at 2 for those pipes.
442 unsigned int nb_fd
= 2;
443 struct ltt_session
*session
;
444 struct ltt_kernel_channel
*channel
;
446 DBG("Updating kernel_pollfd");
448 /* Get the number of channel of all kernel session */
450 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
451 lock_session(session
);
452 if (session
->kernel_session
== NULL
) {
453 unlock_session(session
);
456 nb_fd
+= session
->kernel_session
->channel_count
;
457 unlock_session(session
);
460 DBG("Resizing kernel_pollfd to size %d", nb_fd
);
462 kernel_pollfd
= realloc(kernel_pollfd
, nb_fd
* sizeof(struct pollfd
));
463 if (kernel_pollfd
== NULL
) {
464 perror("malloc kernel_pollfd");
468 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
469 lock_session(session
);
470 if (session
->kernel_session
== NULL
) {
471 unlock_session(session
);
475 ERR("To much channel for kernel_pollfd size");
476 unlock_session(session
);
479 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
480 kernel_pollfd
[i
].fd
= channel
->fd
;
481 kernel_pollfd
[i
].events
= POLLIN
| POLLRDNORM
;
484 unlock_session(session
);
486 unlock_session_list();
488 /* Adding wake up pipe */
489 kernel_pollfd
[nb_fd
- 2].fd
= kernel_poll_pipe
[0];
490 kernel_pollfd
[nb_fd
- 2].events
= POLLIN
;
492 /* Adding the quit pipe */
493 kernel_pollfd
[nb_fd
- 1].fd
= thread_quit_pipe
[0];
498 unlock_session_list();
503 * Find the channel fd from 'fd' over all tracing session. When found, check
504 * for new channel stream and send those stream fds to the kernel consumer.
506 * Useful for CPU hotplug feature.
508 static int update_kernel_stream(int fd
)
511 struct ltt_session
*session
;
512 struct ltt_kernel_channel
*channel
;
514 DBG("Updating kernel streams for channel fd %d", fd
);
517 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
518 lock_session(session
);
519 if (session
->kernel_session
== NULL
) {
520 unlock_session(session
);
523 cds_list_for_each_entry(channel
, &session
->kernel_session
->channel_list
.head
, list
) {
524 if (channel
->fd
== fd
) {
525 DBG("Channel found, updating kernel streams");
526 ret
= kernel_open_channel_stream(channel
);
531 * Have we already sent fds to the consumer? If yes, it means that
532 * tracing is started so it is safe to send our updated stream fds.
534 if (session
->kernel_session
->kconsumer_fds_sent
== 1) {
535 ret
= send_kconsumerd_channel_fds(kconsumerd_cmd_sock
, channel
);
543 unlock_session(session
);
547 unlock_session_list();
549 unlock_session(session
);
555 * This thread manage event coming from the kernel.
557 * Features supported in this thread:
560 static void *thread_manage_kernel(void *data
)
562 int ret
, i
, nb_fd
= 0;
564 int update_poll_flag
= 1;
566 DBG("Thread manage kernel started");
569 if (update_poll_flag
== 1) {
570 nb_fd
= update_kernel_pollfd();
574 update_poll_flag
= 0;
577 DBG("Polling on %d fds", nb_fd
);
579 /* Poll infinite value of time */
580 ret
= poll(kernel_pollfd
, nb_fd
, -1);
582 perror("poll kernel thread");
584 } else if (ret
== 0) {
585 /* Should not happen since timeout is infinite */
589 /* Thread quit pipe has been closed. Killing thread. */
590 if (kernel_pollfd
[nb_fd
- 1].revents
== POLLNVAL
) {
594 DBG("Kernel poll event triggered");
597 * Check if the wake up pipe was triggered. If so, the kernel_pollfd
600 switch (kernel_pollfd
[nb_fd
- 2].revents
) {
602 ret
= read(kernel_poll_pipe
[0], &tmp
, 1);
603 update_poll_flag
= 1;
611 for (i
= 0; i
< nb_fd
; i
++) {
612 switch (kernel_pollfd
[i
].revents
) {
614 * New CPU detected by the kernel. Adding kernel stream to kernel
615 * session and updating the kernel consumer
617 case POLLIN
| POLLRDNORM
:
618 ret
= update_kernel_stream(kernel_pollfd
[i
].fd
);
628 DBG("Kernel thread dying");
633 close(kernel_poll_pipe
[0]);
634 close(kernel_poll_pipe
[1]);
639 * This thread manage the kconsumerd error sent back to the session daemon.
641 static void *thread_manage_kconsumerd(void *data
)
644 enum lttcomm_return_code code
;
645 struct pollfd pollfd
[2];
647 DBG("[thread] Manage kconsumerd started");
649 ret
= lttcomm_listen_unix_sock(kconsumerd_err_sock
);
654 /* First fd is always the quit pipe */
655 pollfd
[0].fd
= thread_quit_pipe
[0];
658 pollfd
[1].fd
= kconsumerd_err_sock
;
659 pollfd
[1].events
= POLLIN
;
661 /* Inifinite blocking call, waiting for transmission */
662 ret
= poll(pollfd
, 2, -1);
664 perror("poll kconsumerd thread");
668 /* Thread quit pipe has been closed. Killing thread. */
669 if (pollfd
[0].revents
== POLLNVAL
) {
671 } else if (pollfd
[1].revents
== POLLERR
) {
672 ERR("Kconsumerd err socket poll error");
676 sock
= lttcomm_accept_unix_sock(kconsumerd_err_sock
);
681 /* Getting status code from kconsumerd */
682 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
687 if (code
== KCONSUMERD_COMMAND_SOCK_READY
) {
688 kconsumerd_cmd_sock
= lttcomm_connect_unix_sock(kconsumerd_cmd_unix_sock_path
);
689 if (kconsumerd_cmd_sock
< 0) {
690 sem_post(&kconsumerd_sem
);
691 perror("kconsumerd connect");
694 /* Signal condition to tell that the kconsumerd is ready */
695 sem_post(&kconsumerd_sem
);
696 DBG("Kconsumerd command socket ready");
698 DBG("Kconsumerd error when waiting for SOCK_READY : %s",
699 lttcomm_get_readable_code(-code
));
703 /* Wait for any kconsumerd error */
704 ret
= lttcomm_recv_unix_sock(sock
, &code
, sizeof(enum lttcomm_return_code
));
706 ERR("Kconsumerd closed the command socket");
710 ERR("Kconsumerd return code : %s", lttcomm_get_readable_code(-code
));
713 DBG("Kconsumerd thread dying");
714 if (kconsumerd_err_sock
) {
715 close(kconsumerd_err_sock
);
717 if (kconsumerd_cmd_sock
) {
718 close(kconsumerd_cmd_sock
);
724 unlink(kconsumerd_err_unix_sock_path
);
725 unlink(kconsumerd_cmd_unix_sock_path
);
732 * This thread manage the application socket communication
734 static void *thread_manage_apps(void *data
)
737 struct pollfd pollfd
[2];
739 /* TODO: Something more elegant is needed but fine for now */
740 /* FIXME: change all types to either uint8_t, uint32_t, uint64_t
741 * for 32-bit vs 64-bit compat processes. */
742 /* replicate in ust with version number */
744 int reg
; /* 1:register, 0:unregister */
749 DBG("[thread] Manage apps started");
751 ret
= lttcomm_listen_unix_sock(apps_sock
);
756 /* First fd is always the quit pipe */
757 pollfd
[0].fd
= thread_quit_pipe
[0];
760 pollfd
[1].fd
= apps_sock
;
761 pollfd
[1].events
= POLLIN
;
763 /* Notify all applications to register */
764 notify_apps(default_global_apps_pipe
);
767 DBG("Accepting application registration");
769 /* Inifinite blocking call, waiting for transmission */
770 ret
= poll(pollfd
, 2, -1);
772 perror("poll apps thread");
776 /* Thread quit pipe has been closed. Killing thread. */
777 if (pollfd
[0].revents
== POLLNVAL
) {
779 } else if (pollfd
[1].revents
== POLLERR
) {
780 ERR("Apps socket poll error");
784 sock
= lttcomm_accept_unix_sock(apps_sock
);
790 * Basic recv here to handle the very simple data
791 * that the libust send to register (reg_msg).
793 ret
= recv(sock
, ®_msg
, sizeof(reg_msg
), 0);
799 /* Add application to the global traceable list */
800 if (reg_msg
.reg
== 1) {
802 ret
= register_traceable_app(reg_msg
.pid
, reg_msg
.uid
);
804 /* register_traceable_app only return an error with
805 * ENOMEM. At this point, we better stop everything.
811 unregister_traceable_app(reg_msg
.pid
);
816 DBG("Apps thread dying");
824 unlink(apps_unix_sock_path
);
829 * Start the thread_manage_kconsumerd. This must be done after a kconsumerd
830 * exec or it will fails.
832 static int spawn_kconsumerd_thread(void)
836 /* Setup semaphore */
837 sem_init(&kconsumerd_sem
, 0, 0);
839 ret
= pthread_create(&kconsumerd_thread
, NULL
, thread_manage_kconsumerd
, (void *) NULL
);
841 perror("pthread_create kconsumerd");
845 /* Wait for the kconsumerd thread to be ready */
846 sem_wait(&kconsumerd_sem
);
848 if (kconsumerd_pid
== 0) {
849 ERR("Kconsumerd did not start");
856 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
861 * Fork and exec a kernel consumer daemon (kconsumerd).
863 * Return pid if successful else -1.
865 static pid_t
spawn_kconsumerd(void)
869 const char *verbosity
;
871 DBG("Spawning kconsumerd");
878 if (opt_verbose
> 1) {
879 verbosity
= "--verbose";
881 verbosity
= "--quiet";
883 execl(INSTALL_BIN_PATH
"/ltt-kconsumerd", "ltt-kconsumerd", verbosity
, NULL
);
885 perror("kernel start consumer exec");
888 } else if (pid
> 0) {
892 perror("kernel start consumer fork");
902 * Spawn the kconsumerd daemon and session daemon thread.
904 static int start_kconsumerd(void)
908 pthread_mutex_lock(&kconsumerd_pid_mutex
);
909 if (kconsumerd_pid
!= 0) {
910 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
914 ret
= spawn_kconsumerd();
916 ERR("Spawning kconsumerd failed");
917 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
918 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
922 /* Setting up the global kconsumerd_pid */
923 kconsumerd_pid
= ret
;
924 pthread_mutex_unlock(&kconsumerd_pid_mutex
);
926 DBG("Kconsumerd pid %d", ret
);
928 DBG("Spawning kconsumerd thread");
929 ret
= spawn_kconsumerd_thread();
931 ERR("Fatal error spawning kconsumerd thread");
943 * modprobe_kernel_modules
945 static int modprobe_kernel_modules(void)
950 while (kernel_modules_list
[i
] != NULL
) {
951 ret
= snprintf(modprobe
, sizeof(modprobe
), "/sbin/modprobe %s",
952 kernel_modules_list
[i
]);
954 perror("snprintf modprobe");
957 ret
= system(modprobe
);
959 ERR("Unable to load module %s", kernel_modules_list
[i
]);
961 DBG("Modprobe successfully %s", kernel_modules_list
[i
]);
972 static int mount_debugfs(char *path
)
975 char *type
= "debugfs";
977 ret
= mkdir_recursive(path
, S_IRWXU
| S_IRWXG
);
982 ret
= mount(type
, path
, type
, 0, NULL
);
984 perror("mount debugfs");
988 DBG("Mounted debugfs successfully at %s", path
);
995 * Setup necessary data for kernel tracer action.
997 static void init_kernel_tracer(void)
1000 char *proc_mounts
= "/proc/mounts";
1002 char *debugfs_path
= NULL
, *lttng_path
;
1005 /* Detect debugfs */
1006 fp
= fopen(proc_mounts
, "r");
1008 ERR("Unable to probe %s", proc_mounts
);
1012 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
1013 if (strstr(line
, "debugfs") != NULL
) {
1014 /* Remove first string */
1016 /* Dup string here so we can reuse line later on */
1017 debugfs_path
= strdup(strtok(NULL
, " "));
1018 DBG("Got debugfs path : %s", debugfs_path
);
1025 /* Mount debugfs if needded */
1026 if (debugfs_path
== NULL
) {
1027 ret
= asprintf(&debugfs_path
, "/mnt/debugfs");
1029 perror("asprintf debugfs path");
1032 ret
= mount_debugfs(debugfs_path
);
1038 /* Modprobe lttng kernel modules */
1039 ret
= modprobe_kernel_modules();
1044 /* Setup lttng kernel path */
1045 ret
= asprintf(<tng_path
, "%s/lttng", debugfs_path
);
1047 perror("asprintf lttng path");
1051 /* Open debugfs lttng */
1052 kernel_tracer_fd
= open(lttng_path
, O_RDWR
);
1053 if (kernel_tracer_fd
< 0) {
1054 DBG("Failed to open %s", lttng_path
);
1060 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1070 WARN("No kernel tracer available");
1071 kernel_tracer_fd
= 0;
1076 * Start tracing by creating trace directory and sending FDs to the kernel
1079 static int start_kernel_trace(struct ltt_kernel_session
*session
)
1083 if (session
->kconsumer_fds_sent
== 0) {
1084 ret
= send_kconsumerd_fds(kconsumerd_cmd_sock
, session
);
1086 ERR("Send kconsumerd fds failed");
1087 ret
= LTTCOMM_KERN_CONSUMER_FAIL
;
1091 session
->kconsumer_fds_sent
= 1;
1099 * Notify kernel thread to update it's pollfd.
1101 static int notify_kernel_pollfd(void)
1105 /* Inform kernel thread of the new kernel channel */
1106 ret
= write(kernel_poll_pipe
[1], "!", 1);
1108 perror("write kernel poll pipe");
1115 * Allocate a channel structure and fill it.
1117 static struct lttng_channel
*init_default_channel(char *name
)
1119 struct lttng_channel
*chan
;
1121 chan
= malloc(sizeof(struct lttng_channel
));
1123 perror("init channel malloc");
1127 if (snprintf(chan
->name
, NAME_MAX
, "%s", name
) < 0) {
1128 perror("snprintf channel name");
1132 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
1133 chan
->attr
.subbuf_size
= DEFAULT_CHANNEL_SUBBUF_SIZE
;
1134 chan
->attr
.num_subbuf
= DEFAULT_CHANNEL_SUBBUF_NUM
;
1135 chan
->attr
.switch_timer_interval
= DEFAULT_CHANNEL_SWITCH_TIMER
;
1136 chan
->attr
.read_timer_interval
= DEFAULT_CHANNEL_READ_TIMER
;
1137 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
1144 * Create a kernel tracer session then create the default channel.
1146 static int create_kernel_session(struct ltt_session
*session
)
1150 DBG("Creating kernel session");
1152 ret
= kernel_create_session(session
, kernel_tracer_fd
);
1154 ret
= LTTCOMM_KERN_SESS_FAIL
;
1158 ret
= mkdir_recursive(session
->path
, S_IRWXU
| S_IRWXG
);
1160 if (ret
!= EEXIST
) {
1161 ERR("Trace directory creation error");
1171 * Using the session list, filled a lttng_session array to send back to the
1172 * client for session listing.
1174 * The session list lock MUST be acquired before calling this function. Use
1175 * lock_session_list() and unlock_session_list().
1177 static void list_lttng_sessions(struct lttng_session
*sessions
)
1180 struct ltt_session
*session
;
1182 DBG("Getting all available session");
1184 * Iterate over session list and append data after the control struct in
1187 cds_list_for_each_entry(session
, &session_list_ptr
->head
, list
) {
1188 strncpy(sessions
[i
].path
, session
->path
, PATH_MAX
);
1189 strncpy(sessions
[i
].name
, session
->name
, NAME_MAX
);
1195 * Fill lttng_channel array of all channels.
1197 static void list_lttng_channels(struct ltt_session
*session
,
1198 struct lttng_channel
*channels
)
1201 struct ltt_kernel_channel
*kchan
;
1203 DBG("Listing channels for session %s", session
->name
);
1205 /* Kernel channels */
1206 if (session
->kernel_session
!= NULL
) {
1207 cds_list_for_each_entry(kchan
, &session
->kernel_session
->channel_list
.head
, list
) {
1208 /* Copy lttng_channel struct to array */
1209 memcpy(&channels
[i
], kchan
->channel
, sizeof(struct lttng_channel
));
1210 channels
[i
].enabled
= kchan
->enabled
;
1215 /* TODO: Missing UST listing */
1219 * Fill lttng_event array of all events in the channel.
1221 static void list_lttng_events(struct ltt_kernel_channel
*kchan
,
1222 struct lttng_event
*events
)
1225 * TODO: This is ONLY kernel. Need UST support.
1228 struct ltt_kernel_event
*event
;
1230 DBG("Listing events for channel %s", kchan
->channel
->name
);
1232 /* Kernel channels */
1233 cds_list_for_each_entry(event
, &kchan
->events_list
.head
, list
) {
1234 strncpy(events
[i
].name
, event
->event
->name
, LTTNG_SYMBOL_NAME_LEN
);
1235 events
[i
].enabled
= event
->enabled
;
1236 switch (event
->event
->instrumentation
) {
1237 case LTTNG_KERNEL_TRACEPOINT
:
1238 events
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1240 case LTTNG_KERNEL_KPROBE
:
1241 case LTTNG_KERNEL_KRETPROBE
:
1242 events
[i
].type
= LTTNG_EVENT_PROBE
;
1243 memcpy(&events
[i
].attr
.probe
, &event
->event
->u
.kprobe
,
1244 sizeof(struct lttng_kernel_kprobe
));
1246 case LTTNG_KERNEL_FUNCTION
:
1247 events
[i
].type
= LTTNG_EVENT_FUNCTION
;
1248 memcpy(&events
[i
].attr
.ftrace
, &event
->event
->u
.ftrace
,
1249 sizeof(struct lttng_kernel_function
));
1257 * Process the command requested by the lttng client within the command
1258 * context structure. This function make sure that the return structure (llm)
1259 * is set and ready for transmission before returning.
1261 * Return any error encountered or 0 for success.
1263 static int process_client_msg(struct command_ctx
*cmd_ctx
)
1265 int ret
= LTTCOMM_OK
;
1267 DBG("Processing client command %d", cmd_ctx
->lsm
->cmd_type
);
1269 /* Listing commands don't need a session */
1270 switch (cmd_ctx
->lsm
->cmd_type
) {
1271 case LTTNG_CREATE_SESSION
:
1272 case LTTNG_LIST_SESSIONS
:
1273 case LTTNG_LIST_TRACEPOINTS
:
1276 DBG("Getting session %s by name", cmd_ctx
->lsm
->session
.name
);
1277 cmd_ctx
->session
= find_session_by_name(cmd_ctx
->lsm
->session
.name
);
1278 if (cmd_ctx
->session
== NULL
) {
1279 /* If session name not found */
1280 if (cmd_ctx
->lsm
->session
.name
!= NULL
) {
1281 ret
= LTTCOMM_SESS_NOT_FOUND
;
1282 } else { /* If no session name specified */
1283 ret
= LTTCOMM_SELECT_SESS
;
1287 /* Acquire lock for the session */
1288 lock_session(cmd_ctx
->session
);
1294 * Check domain type for specifif "pre-action".
1296 switch (cmd_ctx
->lsm
->domain
.type
) {
1297 case LTTNG_DOMAIN_KERNEL
:
1298 /* Kernel tracer check */
1299 if (kernel_tracer_fd
== 0) {
1300 init_kernel_tracer();
1301 if (kernel_tracer_fd
== 0) {
1302 ret
= LTTCOMM_KERN_NA
;
1307 /* Need a session for kernel command */
1308 if (cmd_ctx
->lsm
->cmd_type
!= LTTNG_LIST_TRACEPOINTS
&&
1309 cmd_ctx
->session
->kernel_session
== NULL
) {
1311 ret
= create_kernel_session(cmd_ctx
->session
);
1313 ret
= LTTCOMM_KERN_SESS_FAIL
;
1317 /* Start the kernel consumer daemon */
1318 if (kconsumerd_pid
== 0) {
1319 ret
= start_kconsumerd();
1330 /* Process by command type */
1331 switch (cmd_ctx
->lsm
->cmd_type
) {
1332 case LTTNG_ADD_CONTEXT
:
1334 struct lttng_kernel_context kctx
;
1336 /* Setup lttng message with no payload */
1337 ret
= setup_lttng_msg(cmd_ctx
, 0);
1342 switch (cmd_ctx
->lsm
->domain
.type
) {
1343 case LTTNG_DOMAIN_KERNEL
:
1344 /* Create Kernel context */
1345 kctx
.ctx
= cmd_ctx
->lsm
->u
.context
.ctx
.ctx
;
1346 kctx
.u
.perf_counter
.type
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.type
;
1347 kctx
.u
.perf_counter
.config
= cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.config
;
1348 strncpy(kctx
.u
.perf_counter
.name
,
1349 cmd_ctx
->lsm
->u
.context
.ctx
.u
.perf_counter
.name
,
1350 LTTNG_SYMBOL_NAME_LEN
);
1352 /* Add kernel context to kernel tracer. See context.c */
1353 ret
= add_kernel_context(cmd_ctx
->session
->kernel_session
, &kctx
,
1354 cmd_ctx
->lsm
->u
.context
.event_name
,
1355 cmd_ctx
->lsm
->u
.context
.channel_name
);
1356 if (ret
!= LTTCOMM_OK
) {
1361 /* TODO: Userspace tracing */
1362 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1369 case LTTNG_DISABLE_CHANNEL
:
1371 struct ltt_kernel_channel
*kchan
;
1373 /* Setup lttng message with no payload */
1374 ret
= setup_lttng_msg(cmd_ctx
, 0);
1379 switch (cmd_ctx
->lsm
->domain
.type
) {
1380 case LTTNG_DOMAIN_KERNEL
:
1381 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1382 cmd_ctx
->session
->kernel_session
);
1383 if (kchan
== NULL
) {
1384 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1386 } else if (kchan
->enabled
== 1) {
1387 ret
= kernel_disable_channel(kchan
);
1389 if (ret
!= EEXIST
) {
1390 ret
= LTTCOMM_KERN_CHAN_DISABLE_FAIL
;
1395 kernel_wait_quiescent(kernel_tracer_fd
);
1398 /* TODO: Userspace tracing */
1399 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1406 case LTTNG_DISABLE_EVENT
:
1408 struct ltt_kernel_channel
*kchan
;
1409 struct ltt_kernel_event
*kevent
;
1411 /* Setup lttng message with no payload */
1412 ret
= setup_lttng_msg(cmd_ctx
, 0);
1417 switch (cmd_ctx
->lsm
->domain
.type
) {
1418 case LTTNG_DOMAIN_KERNEL
:
1419 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1420 cmd_ctx
->session
->kernel_session
);
1421 if (kchan
== NULL
) {
1422 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1426 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.disable
.name
, kchan
);
1427 if (kevent
!= NULL
) {
1428 DBG("Disabling kernel event %s for channel %s.", kevent
->event
->name
,
1429 kchan
->channel
->name
);
1430 ret
= kernel_disable_event(kevent
);
1432 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1437 kernel_wait_quiescent(kernel_tracer_fd
);
1440 /* TODO: Userspace tracing */
1441 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1448 case LTTNG_DISABLE_ALL_EVENT
:
1450 struct ltt_kernel_channel
*kchan
;
1451 struct ltt_kernel_event
*kevent
;
1453 /* Setup lttng message with no payload */
1454 ret
= setup_lttng_msg(cmd_ctx
, 0);
1459 switch (cmd_ctx
->lsm
->domain
.type
) {
1460 case LTTNG_DOMAIN_KERNEL
:
1461 DBG("Disabling all enabled kernel events");
1462 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.disable
.channel_name
,
1463 cmd_ctx
->session
->kernel_session
);
1464 if (kchan
== NULL
) {
1465 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1469 /* For each event in the kernel session */
1470 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1471 DBG("Disabling kernel event %s for channel %s.",
1472 kevent
->event
->name
, kchan
->channel
->name
);
1473 ret
= kernel_disable_event(kevent
);
1479 /* Quiescent wait after event disable */
1480 kernel_wait_quiescent(kernel_tracer_fd
);
1483 /* TODO: Userspace tracing */
1484 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1491 case LTTNG_ENABLE_CHANNEL
:
1493 struct ltt_kernel_channel
*kchan
;
1495 /* Setup lttng message with no payload */
1496 ret
= setup_lttng_msg(cmd_ctx
, 0);
1501 switch (cmd_ctx
->lsm
->domain
.type
) {
1502 case LTTNG_DOMAIN_KERNEL
:
1503 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.enable
.channel_name
,
1504 cmd_ctx
->session
->kernel_session
);
1505 if (kchan
== NULL
) {
1506 /* Channel not found, creating it */
1507 DBG("Creating kernel channel");
1509 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1510 &cmd_ctx
->lsm
->u
.channel
.chan
, cmd_ctx
->session
->path
);
1512 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1516 /* Notify kernel thread that there is a new channel */
1517 ret
= notify_kernel_pollfd();
1519 ret
= LTTCOMM_FATAL
;
1522 } else if (kchan
->enabled
== 0) {
1523 ret
= kernel_enable_channel(kchan
);
1525 if (ret
!= EEXIST
) {
1526 ret
= LTTCOMM_KERN_CHAN_ENABLE_FAIL
;
1532 kernel_wait_quiescent(kernel_tracer_fd
);
1535 /* TODO: Userspace tracing */
1536 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1543 case LTTNG_ENABLE_EVENT
:
1546 struct ltt_kernel_channel
*kchan
;
1547 struct ltt_kernel_event
*kevent
;
1548 struct lttng_channel
*chan
;
1550 /* Setup lttng message with no payload */
1551 ret
= setup_lttng_msg(cmd_ctx
, 0);
1556 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1558 switch (cmd_ctx
->lsm
->domain
.type
) {
1559 case LTTNG_DOMAIN_KERNEL
:
1561 kchan
= get_kernel_channel_by_name(channel_name
,
1562 cmd_ctx
->session
->kernel_session
);
1563 if (kchan
== NULL
) {
1564 DBG("Channel not found. Creating channel %s", channel_name
);
1566 chan
= init_default_channel(channel_name
);
1568 ret
= LTTCOMM_FATAL
;
1572 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1573 chan
, cmd_ctx
->session
->path
);
1575 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1579 } while (kchan
== NULL
);
1581 kevent
= get_kernel_event_by_name(cmd_ctx
->lsm
->u
.enable
.event
.name
, kchan
);
1582 if (kevent
== NULL
) {
1583 DBG("Creating kernel event %s for channel %s.",
1584 cmd_ctx
->lsm
->u
.enable
.event
.name
, channel_name
);
1585 ret
= kernel_create_event(&cmd_ctx
->lsm
->u
.enable
.event
, kchan
);
1587 DBG("Enabling kernel event %s for channel %s.",
1588 kevent
->event
->name
, channel_name
);
1589 ret
= kernel_enable_event(kevent
);
1590 if (ret
== -EEXIST
) {
1591 ret
= LTTCOMM_KERN_EVENT_EXIST
;
1597 ret
= LTTCOMM_KERN_ENABLE_FAIL
;
1601 kernel_wait_quiescent(kernel_tracer_fd
);
1604 /* TODO: Userspace tracing */
1605 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1611 case LTTNG_ENABLE_ALL_EVENT
:
1615 struct ltt_kernel_channel
*kchan
;
1616 struct ltt_kernel_event
*kevent
;
1617 struct lttng_event
*event_list
;
1618 struct lttng_channel
*chan
;
1620 /* Setup lttng message with no payload */
1621 ret
= setup_lttng_msg(cmd_ctx
, 0);
1626 DBG("Enabling all kernel event");
1628 channel_name
= cmd_ctx
->lsm
->u
.enable
.channel_name
;
1630 switch (cmd_ctx
->lsm
->domain
.type
) {
1631 case LTTNG_DOMAIN_KERNEL
:
1633 kchan
= get_kernel_channel_by_name(channel_name
,
1634 cmd_ctx
->session
->kernel_session
);
1635 if (kchan
== NULL
) {
1636 DBG("Channel not found. Creating channel %s", channel_name
);
1638 chan
= init_default_channel(channel_name
);
1640 ret
= LTTCOMM_FATAL
;
1644 ret
= kernel_create_channel(cmd_ctx
->session
->kernel_session
,
1645 chan
, cmd_ctx
->session
->path
);
1647 ret
= LTTCOMM_KERN_CHAN_FAIL
;
1651 } while (kchan
== NULL
);
1653 /* For each event in the kernel session */
1654 cds_list_for_each_entry(kevent
, &kchan
->events_list
.head
, list
) {
1655 DBG("Enabling kernel event %s for channel %s.",
1656 kevent
->event
->name
, channel_name
);
1657 ret
= kernel_enable_event(kevent
);
1663 size
= kernel_list_events(kernel_tracer_fd
, &event_list
);
1665 ret
= LTTCOMM_KERN_LIST_FAIL
;
1669 for (i
= 0; i
< size
; i
++) {
1670 kevent
= get_kernel_event_by_name(event_list
[i
].name
, kchan
);
1671 if (kevent
== NULL
) {
1672 /* Default event type for enable all */
1673 event_list
[i
].type
= LTTNG_EVENT_TRACEPOINT
;
1674 /* Enable each single tracepoint event */
1675 ret
= kernel_create_event(&event_list
[i
], kchan
);
1677 /* Ignore error here and continue */
1684 /* Quiescent wait after event enable */
1685 kernel_wait_quiescent(kernel_tracer_fd
);
1688 /* TODO: Userspace tracing */
1689 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1696 case LTTNG_LIST_TRACEPOINTS
:
1698 struct lttng_event
*events
;
1699 ssize_t nb_events
= 0;
1701 switch (cmd_ctx
->lsm
->domain
.type
) {
1702 case LTTNG_DOMAIN_KERNEL
:
1703 DBG("Listing kernel events");
1704 nb_events
= kernel_list_events(kernel_tracer_fd
, &events
);
1705 if (nb_events
< 0) {
1706 ret
= LTTCOMM_KERN_LIST_FAIL
;
1711 /* TODO: Userspace listing */
1712 ret
= LTTCOMM_NOT_IMPLEMENTED
;
1717 * Setup lttng message with payload size set to the event list size in
1718 * bytes and then copy list into the llm payload.
1720 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_event
) * nb_events
);
1726 /* Copy event list into message payload */
1727 memcpy(cmd_ctx
->llm
->payload
, events
,
1728 sizeof(struct lttng_event
) * nb_events
);
1735 case LTTNG_START_TRACE
:
1737 struct ltt_kernel_channel
*chan
;
1739 /* Setup lttng message with no payload */
1740 ret
= setup_lttng_msg(cmd_ctx
, 0);
1745 /* Kernel tracing */
1746 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1747 if (cmd_ctx
->session
->kernel_session
->metadata
== NULL
) {
1748 DBG("Open kernel metadata");
1749 ret
= kernel_open_metadata(cmd_ctx
->session
->kernel_session
,
1750 cmd_ctx
->session
->path
);
1752 ret
= LTTCOMM_KERN_META_FAIL
;
1757 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1758 DBG("Opening kernel metadata stream");
1759 if (cmd_ctx
->session
->kernel_session
->metadata_stream_fd
== 0) {
1760 ret
= kernel_open_metadata_stream(cmd_ctx
->session
->kernel_session
);
1762 ERR("Kernel create metadata stream failed");
1763 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1769 /* For each channel */
1770 cds_list_for_each_entry(chan
,
1771 &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1772 if (chan
->stream_count
== 0) {
1773 ret
= kernel_open_channel_stream(chan
);
1775 ERR("Kernel create channel stream failed");
1776 ret
= LTTCOMM_KERN_STREAM_FAIL
;
1779 /* Update the stream global counter */
1780 cmd_ctx
->session
->kernel_session
->stream_count_global
+= ret
;
1784 DBG("Start kernel tracing");
1785 ret
= kernel_start_session(cmd_ctx
->session
->kernel_session
);
1787 ERR("Kernel start session failed");
1788 ret
= LTTCOMM_KERN_START_FAIL
;
1792 ret
= start_kernel_trace(cmd_ctx
->session
->kernel_session
);
1794 ret
= LTTCOMM_KERN_START_FAIL
;
1798 /* Quiescent wait after starting trace */
1799 kernel_wait_quiescent(kernel_tracer_fd
);
1802 /* TODO: Start all UST traces */
1807 case LTTNG_STOP_TRACE
:
1809 struct ltt_kernel_channel
*chan
;
1810 /* Setup lttng message with no payload */
1811 ret
= setup_lttng_msg(cmd_ctx
, 0);
1817 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1818 DBG("Stop kernel tracing");
1820 ret
= kernel_metadata_flush_buffer(cmd_ctx
->session
->kernel_session
->metadata_stream_fd
);
1822 ERR("Kernel metadata flush failed");
1825 cds_list_for_each_entry(chan
, &cmd_ctx
->session
->kernel_session
->channel_list
.head
, list
) {
1826 ret
= kernel_flush_buffer(chan
);
1828 ERR("Kernel flush buffer error");
1832 ret
= kernel_stop_session(cmd_ctx
->session
->kernel_session
);
1834 ERR("Kernel stop session failed");
1835 ret
= LTTCOMM_KERN_STOP_FAIL
;
1839 /* Quiescent wait after stopping trace */
1840 kernel_wait_quiescent(kernel_tracer_fd
);
1843 /* TODO : User-space tracer */
1848 case LTTNG_CREATE_SESSION
:
1850 /* Setup lttng message with no payload */
1851 ret
= setup_lttng_msg(cmd_ctx
, 0);
1856 ret
= create_session(cmd_ctx
->lsm
->session
.name
, cmd_ctx
->lsm
->session
.path
);
1858 if (ret
== -EEXIST
) {
1859 ret
= LTTCOMM_EXIST_SESS
;
1861 ret
= LTTCOMM_FATAL
;
1869 case LTTNG_DESTROY_SESSION
:
1871 /* Setup lttng message with no payload */
1872 ret
= setup_lttng_msg(cmd_ctx
, 0);
1877 /* Clean kernel session teardown */
1878 teardown_kernel_session(cmd_ctx
->session
);
1880 ret
= destroy_session(cmd_ctx
->lsm
->session
.name
);
1882 ret
= LTTCOMM_FATAL
;
1887 * Must notify the kernel thread here to update it's pollfd in order to
1888 * remove the channel(s)' fd just destroyed.
1890 ret
= notify_kernel_pollfd();
1892 ret
= LTTCOMM_FATAL
;
1899 case LTTNG_LIST_DOMAINS
:
1903 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1907 nb_dom
+= cmd_ctx
->session
->ust_trace_count
;
1909 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_domain
) * nb_dom
);
1914 ((struct lttng_domain
*)(cmd_ctx
->llm
->payload
))[0].type
=
1915 LTTNG_DOMAIN_KERNEL
;
1917 /* TODO: User-space tracer domain support */
1921 case LTTNG_LIST_CHANNELS
:
1924 * TODO: Only kernel channels are listed here. UST listing
1925 * is needed on lttng-ust 2.0 release.
1928 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1929 nb_chan
+= cmd_ctx
->session
->kernel_session
->channel_count
;
1932 ret
= setup_lttng_msg(cmd_ctx
,
1933 sizeof(struct lttng_channel
) * nb_chan
);
1938 list_lttng_channels(cmd_ctx
->session
,
1939 (struct lttng_channel
*)(cmd_ctx
->llm
->payload
));
1944 case LTTNG_LIST_EVENTS
:
1947 * TODO: Only kernel events are listed here. UST listing
1948 * is needed on lttng-ust 2.0 release.
1950 size_t nb_event
= 0;
1951 struct ltt_kernel_channel
*kchan
= NULL
;
1953 if (cmd_ctx
->session
->kernel_session
!= NULL
) {
1954 kchan
= get_kernel_channel_by_name(cmd_ctx
->lsm
->u
.list
.channel_name
,
1955 cmd_ctx
->session
->kernel_session
);
1956 if (kchan
== NULL
) {
1957 ret
= LTTCOMM_KERN_CHAN_NOT_FOUND
;
1960 nb_event
+= kchan
->event_count
;
1963 ret
= setup_lttng_msg(cmd_ctx
,
1964 sizeof(struct lttng_event
) * nb_event
);
1969 DBG("Listing events (%ld events)", nb_event
);
1971 list_lttng_events(kchan
,
1972 (struct lttng_event
*)(cmd_ctx
->llm
->payload
));
1977 case LTTNG_LIST_SESSIONS
:
1979 lock_session_list();
1981 if (session_list_ptr
->count
== 0) {
1982 ret
= LTTCOMM_NO_SESSION
;
1983 unlock_session_list();
1987 ret
= setup_lttng_msg(cmd_ctx
, sizeof(struct lttng_session
) *
1988 session_list_ptr
->count
);
1990 unlock_session_list();
1994 /* Filled the session array */
1995 list_lttng_sessions((struct lttng_session
*)(cmd_ctx
->llm
->payload
));
1997 unlock_session_list();
2003 /* Undefined command */
2004 ret
= setup_lttng_msg(cmd_ctx
, 0);
2013 /* Set return code */
2014 cmd_ctx
->llm
->ret_code
= ret
;
2016 if (cmd_ctx
->session
) {
2017 unlock_session(cmd_ctx
->session
);
2023 if (cmd_ctx
->llm
== NULL
) {
2024 DBG("Missing llm structure. Allocating one.");
2025 if (setup_lttng_msg(cmd_ctx
, 0) < 0) {
2029 /* Notify client of error */
2030 cmd_ctx
->llm
->ret_code
= ret
;
2033 if (cmd_ctx
->session
) {
2034 unlock_session(cmd_ctx
->session
);
2040 * This thread manage all clients request using the unix client socket for
2043 static void *thread_manage_clients(void *data
)
2046 struct command_ctx
*cmd_ctx
= NULL
;
2047 struct pollfd pollfd
[2];
2049 DBG("[thread] Manage client started");
2051 ret
= lttcomm_listen_unix_sock(client_sock
);
2056 /* First fd is always the quit pipe */
2057 pollfd
[0].fd
= thread_quit_pipe
[0];
2060 pollfd
[1].fd
= client_sock
;
2061 pollfd
[1].events
= POLLIN
;
2063 /* Notify parent pid that we are ready
2064 * to accept command for client side.
2066 if (opt_sig_parent
) {
2067 kill(ppid
, SIGCHLD
);
2071 DBG("Accepting client command ...");
2073 /* Inifinite blocking call, waiting for transmission */
2074 ret
= poll(pollfd
, 2, -1);
2076 perror("poll client thread");
2080 /* Thread quit pipe has been closed. Killing thread. */
2081 if (pollfd
[0].revents
== POLLNVAL
) {
2083 } else if (pollfd
[1].revents
== POLLERR
) {
2084 ERR("Client socket poll error");
2088 sock
= lttcomm_accept_unix_sock(client_sock
);
2093 /* Allocate context command to process the client request */
2094 cmd_ctx
= malloc(sizeof(struct command_ctx
));
2096 /* Allocate data buffer for reception */
2097 cmd_ctx
->lsm
= malloc(sizeof(struct lttcomm_session_msg
));
2098 cmd_ctx
->llm
= NULL
;
2099 cmd_ctx
->session
= NULL
;
2102 * Data is received from the lttng client. The struct
2103 * lttcomm_session_msg (lsm) contains the command and data request of
2106 DBG("Receiving data from client ...");
2107 ret
= lttcomm_recv_unix_sock(sock
, cmd_ctx
->lsm
, sizeof(struct lttcomm_session_msg
));
2112 // TODO: Validate cmd_ctx including sanity check for security purpose.
2115 * This function dispatch the work to the kernel or userspace tracer
2116 * libs and fill the lttcomm_lttng_msg data structure of all the needed
2117 * informations for the client. The command context struct contains
2118 * everything this function may needs.
2120 ret
= process_client_msg(cmd_ctx
);
2122 /* TODO: Inform client somehow of the fatal error. At this point,
2123 * ret < 0 means that a malloc failed (ENOMEM). */
2124 /* Error detected but still accept command */
2125 clean_command_ctx(&cmd_ctx
);
2129 DBG("Sending response (size: %d, retcode: %d)",
2130 cmd_ctx
->lttng_msg_size
, cmd_ctx
->llm
->ret_code
);
2131 ret
= send_unix_sock(sock
, cmd_ctx
->llm
, cmd_ctx
->lttng_msg_size
);
2133 ERR("Failed to send data back to client");
2136 clean_command_ctx(&cmd_ctx
);
2138 /* End of transmission */
2143 DBG("Client thread dying");
2151 unlink(client_unix_sock_path
);
2153 clean_command_ctx(&cmd_ctx
);
2159 * usage function on stderr
2161 static void usage(void)
2163 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
2164 fprintf(stderr
, " -h, --help Display this usage.\n");
2165 fprintf(stderr
, " -c, --client-sock PATH Specify path for the client unix socket\n");
2166 fprintf(stderr
, " -a, --apps-sock PATH Specify path for apps unix socket\n");
2167 fprintf(stderr
, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
2168 fprintf(stderr
, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
2169 fprintf(stderr
, " -d, --daemonize Start as a daemon.\n");
2170 fprintf(stderr
, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
2171 fprintf(stderr
, " -V, --version Show version number.\n");
2172 fprintf(stderr
, " -S, --sig-parent Send SIGCHLD to parent pid to notify readiness.\n");
2173 fprintf(stderr
, " -q, --quiet No output at all.\n");
2174 fprintf(stderr
, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
2178 * daemon argument parsing
2180 static int parse_args(int argc
, char **argv
)
2184 static struct option long_options
[] = {
2185 { "client-sock", 1, 0, 'c' },
2186 { "apps-sock", 1, 0, 'a' },
2187 { "kconsumerd-cmd-sock", 1, 0, 0 },
2188 { "kconsumerd-err-sock", 1, 0, 0 },
2189 { "daemonize", 0, 0, 'd' },
2190 { "sig-parent", 0, 0, 'S' },
2191 { "help", 0, 0, 'h' },
2192 { "group", 1, 0, 'g' },
2193 { "version", 0, 0, 'V' },
2194 { "quiet", 0, 0, 'q' },
2195 { "verbose", 0, 0, 'v' },
2200 int option_index
= 0;
2201 c
= getopt_long(argc
, argv
, "dhqvVS" "a:c:g:s:E:C:", long_options
, &option_index
);
2208 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
2210 fprintf(stderr
, " with arg %s\n", optarg
);
2214 snprintf(client_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2217 snprintf(apps_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2223 opt_tracing_group
= strdup(optarg
);
2229 fprintf(stdout
, "%s\n", VERSION
);
2235 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2238 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, "%s", optarg
);
2244 /* Verbose level can increase using multiple -v */
2248 /* Unknown option or other error.
2249 * Error is printed by getopt, just return */
2258 * Creates the two needed socket by the daemon.
2259 * apps_sock - The communication socket for all UST apps.
2260 * client_sock - The communication of the cli tool (lttng).
2262 static int init_daemon_socket()
2267 old_umask
= umask(0);
2269 /* Create client tool unix socket */
2270 client_sock
= lttcomm_create_unix_sock(client_unix_sock_path
);
2271 if (client_sock
< 0) {
2272 ERR("Create unix sock failed: %s", client_unix_sock_path
);
2277 /* File permission MUST be 660 */
2278 ret
= chmod(client_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2280 ERR("Set file permissions failed: %s", client_unix_sock_path
);
2285 /* Create the application unix socket */
2286 apps_sock
= lttcomm_create_unix_sock(apps_unix_sock_path
);
2287 if (apps_sock
< 0) {
2288 ERR("Create unix sock failed: %s", apps_unix_sock_path
);
2293 /* File permission MUST be 666 */
2294 ret
= chmod(apps_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
2296 ERR("Set file permissions failed: %s", apps_unix_sock_path
);
2307 * Check if the global socket is available. If yes, error is returned.
2309 static int check_existing_daemon()
2313 ret
= access(client_unix_sock_path
, F_OK
);
2315 ret
= access(apps_unix_sock_path
, F_OK
);
2322 * Set the tracing group gid onto the client socket.
2324 * Race window between mkdir and chown is OK because we are going from more
2325 * permissive (root.root) to les permissive (root.tracing).
2327 static int set_permissions(void)
2332 /* Decide which group name to use */
2333 (opt_tracing_group
!= NULL
) ?
2334 (grp
= getgrnam(opt_tracing_group
)) :
2335 (grp
= getgrnam(default_tracing_group
));
2339 WARN("No tracing group detected");
2342 ERR("Missing tracing group. Aborting execution.");
2348 /* Set lttng run dir */
2349 ret
= chown(LTTNG_RUNDIR
, 0, grp
->gr_gid
);
2351 ERR("Unable to set group on " LTTNG_RUNDIR
);
2355 /* lttng client socket path */
2356 ret
= chown(client_unix_sock_path
, 0, grp
->gr_gid
);
2358 ERR("Unable to set group on %s", client_unix_sock_path
);
2362 /* kconsumerd error socket path */
2363 ret
= chown(kconsumerd_err_unix_sock_path
, 0, grp
->gr_gid
);
2365 ERR("Unable to set group on %s", kconsumerd_err_unix_sock_path
);
2369 DBG("All permissions are set");
2376 * Create the pipe used to wake up the kernel thread.
2378 static int create_kernel_poll_pipe(void)
2380 return pipe2(kernel_poll_pipe
, O_CLOEXEC
);
2384 * Create the lttng run directory needed for all global sockets and pipe.
2386 static int create_lttng_rundir(void)
2390 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
);
2392 if (errno
!= EEXIST
) {
2393 ERR("Unable to create " LTTNG_RUNDIR
);
2405 * Setup sockets and directory needed by the kconsumerd communication with the
2408 static int set_kconsumerd_sockets(void)
2412 if (strlen(kconsumerd_err_unix_sock_path
) == 0) {
2413 snprintf(kconsumerd_err_unix_sock_path
, PATH_MAX
, KCONSUMERD_ERR_SOCK_PATH
);
2416 if (strlen(kconsumerd_cmd_unix_sock_path
) == 0) {
2417 snprintf(kconsumerd_cmd_unix_sock_path
, PATH_MAX
, KCONSUMERD_CMD_SOCK_PATH
);
2420 ret
= mkdir(KCONSUMERD_PATH
, S_IRWXU
| S_IRWXG
);
2422 if (errno
!= EEXIST
) {
2423 ERR("Failed to create " KCONSUMERD_PATH
);
2429 /* Create the kconsumerd error unix socket */
2430 kconsumerd_err_sock
= lttcomm_create_unix_sock(kconsumerd_err_unix_sock_path
);
2431 if (kconsumerd_err_sock
< 0) {
2432 ERR("Create unix sock failed: %s", kconsumerd_err_unix_sock_path
);
2437 /* File permission MUST be 660 */
2438 ret
= chmod(kconsumerd_err_unix_sock_path
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
2440 ERR("Set file permissions failed: %s", kconsumerd_err_unix_sock_path
);
2450 * Signal handler for the daemon
2452 static void sighandler(int sig
)
2456 DBG("SIGPIPE catched");
2459 DBG("SIGINT catched");
2463 DBG("SIGTERM catched");
2474 * Setup signal handler for :
2475 * SIGINT, SIGTERM, SIGPIPE
2477 static int set_signal_handler(void)
2480 struct sigaction sa
;
2483 if ((ret
= sigemptyset(&sigset
)) < 0) {
2484 perror("sigemptyset");
2488 sa
.sa_handler
= sighandler
;
2489 sa
.sa_mask
= sigset
;
2491 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
2492 perror("sigaction");
2496 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
2497 perror("sigaction");
2501 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
2502 perror("sigaction");
2506 DBG("Signal handler set for SIGTERM, SIGPIPE and SIGINT");
2512 * Set open files limit to unlimited. This daemon can open a large number of
2513 * file descriptors in order to consumer multiple kernel traces.
2515 static void set_ulimit(void)
2520 /* The kernel does not allowed an infinite limit for open files */
2521 lim
.rlim_cur
= 65535;
2522 lim
.rlim_max
= 65535;
2524 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
2526 perror("failed to set open files limit");
2533 int main(int argc
, char **argv
)
2537 const char *home_path
;
2539 /* Create thread quit pipe */
2540 if (init_thread_quit_pipe() < 0) {
2544 /* Parse arguments */
2546 if ((ret
= parse_args(argc
, argv
) < 0)) {
2559 /* Check if daemon is UID = 0 */
2560 is_root
= !getuid();
2563 ret
= create_lttng_rundir();
2568 if (strlen(apps_unix_sock_path
) == 0) {
2569 snprintf(apps_unix_sock_path
, PATH_MAX
,
2570 DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
2573 if (strlen(client_unix_sock_path
) == 0) {
2574 snprintf(client_unix_sock_path
, PATH_MAX
,
2575 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK
);
2578 home_path
= get_home_dir();
2579 if (home_path
== NULL
) {
2580 /* TODO: Add --socket PATH option */
2581 ERR("Can't get HOME directory for sockets creation.");
2585 if (strlen(apps_unix_sock_path
) == 0) {
2586 snprintf(apps_unix_sock_path
, PATH_MAX
,
2587 DEFAULT_HOME_APPS_UNIX_SOCK
, home_path
);
2590 /* Set the cli tool unix socket path */
2591 if (strlen(client_unix_sock_path
) == 0) {
2592 snprintf(client_unix_sock_path
, PATH_MAX
,
2593 DEFAULT_HOME_CLIENT_UNIX_SOCK
, home_path
);
2597 DBG("Client socket path %s", client_unix_sock_path
);
2598 DBG("Application socket path %s", apps_unix_sock_path
);
2601 * See if daemon already exist. If any of the two socket needed by the
2602 * daemon are present, this test fails. However, if the daemon is killed
2603 * with a SIGKILL, those unix socket must be unlinked by hand.
2605 if ((ret
= check_existing_daemon()) == 0) {
2606 ERR("Already running daemon.\n");
2608 * We do not goto error because we must not cleanup() because a daemon
2609 * is already running.
2614 /* After this point, we can safely call cleanup() so goto error is used */
2617 * These actions must be executed as root. We do that *after* setting up
2618 * the sockets path because we MUST make the check for another daemon using
2619 * those paths *before* trying to set the kernel consumer sockets and init
2623 ret
= set_kconsumerd_sockets();
2628 /* Setup kernel tracer */
2629 init_kernel_tracer();
2631 /* Set ulimit for open files */
2635 if (set_signal_handler() < 0) {
2639 /* Setup the needed unix socket */
2640 if (init_daemon_socket() < 0) {
2644 /* Set credentials to socket */
2645 if (is_root
&& (set_permissions() < 0)) {
2649 /* Get parent pid if -S, --sig-parent is specified. */
2650 if (opt_sig_parent
) {
2654 /* Setup the kernel pipe for waking up the kernel thread */
2655 if (create_kernel_poll_pipe() < 0) {
2660 * Get session list pointer. This pointer MUST NOT be free().
2661 * This list is statically declared in session.c
2663 session_list_ptr
= get_session_list();
2666 /* Create thread to manage the client socket */
2667 ret
= pthread_create(&client_thread
, NULL
, thread_manage_clients
, (void *) NULL
);
2669 perror("pthread_create");
2673 /* Create thread to manage application socket */
2674 ret
= pthread_create(&apps_thread
, NULL
, thread_manage_apps
, (void *) NULL
);
2676 perror("pthread_create");
2680 /* Create kernel thread to manage kernel event */
2681 ret
= pthread_create(&kernel_thread
, NULL
, thread_manage_kernel
, (void *) NULL
);
2683 perror("pthread_create");
2687 ret
= pthread_join(client_thread
, &status
);
2689 perror("pthread_join");