2 * ust-basic-tracing.c - Basic single-session, single-channel, single-process UST tracing
4 * Copyright (C) 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; only version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 * Place - Suite 330, Boston, MA 02111-1307, USA.
21 #define _LARGEFILE64_SOURCE
29 #include <sys/types.h>
34 #include <urcu/futex.h>
35 #include <urcu/uatomic.h>
37 #include <sys/socket.h>
39 #include "lttng-ust-comm.h"
40 #include "../../libringbuffer/backend.h"
41 #include "../../libringbuffer/frontend.h"
43 #define MAX_NR_STREAMS 64
44 #define MAX_NR_EVENTS 128
50 uint64_t memory_map_size
;
53 static int session_handle
;
54 static struct object_data metadata_stream_data
;
55 static struct object_data metadata_data
;
56 static struct object_data channel_data
;
57 static struct object_data stream_data
[MAX_NR_STREAMS
];
58 static int event_handle
[MAX_NR_EVENTS
];
60 static int apps_socket
= -1;
61 static char apps_sock_path
[PATH_MAX
];
62 static char local_apps_wait_shm_path
[PATH_MAX
];
64 static volatile int quit_program
;
66 static void handle_signals(int signo
)
71 static int send_app_msg(int sock
, struct lttcomm_ust_msg
*lum
)
75 len
= lttcomm_send_unix_sock(sock
, lum
, sizeof(*lum
));
78 printf("message successfully sent\n");
81 if (errno
== ECONNRESET
) {
82 printf("remote end closed connection\n");
87 printf("incorrect message size: %zd\n", len
);
93 static int recv_app_reply(int sock
, struct lttcomm_ust_reply
*lur
,
94 uint32_t expected_handle
, uint32_t expected_cmd
)
98 memset(lur
, 0, sizeof(*lur
));
99 len
= lttcomm_recv_unix_sock(sock
, lur
, sizeof(*lur
));
101 case 0: /* orderly shutdown */
102 printf("Application has performed an orderly shutdown\n");
105 printf("result message received\n");
106 if (lur
->handle
!= expected_handle
) {
107 printf("Unexpected result message handle\n");
111 if (lur
->cmd
!= expected_cmd
) {
112 printf("Unexpected result message command\n");
115 if (lur
->ret_code
!= LTTCOMM_OK
) {
116 printf("remote operation failed with code %d.\n",
118 return lur
->ret_code
;
122 if (errno
== ECONNRESET
) {
123 printf("remote end closed connection\n");
128 printf("incorrect message size: %zd\n", len
);
129 return len
> 0 ? -1 : len
;
133 static int send_app_cmd(int sock
,
134 struct lttcomm_ust_msg
*lum
,
135 struct lttcomm_ust_reply
*lur
)
139 ret
= send_app_msg(sock
, lum
);
142 ret
= recv_app_reply(sock
, lur
, lum
->handle
, lum
->cmd
);
150 * Receives a single fd from socket.
152 * Returns the size of received data
154 static int lttcomm_recv_fd(int sock
)
159 struct cmsghdr
*cmsg
;
160 char recv_fd
[CMSG_SPACE(sizeof(int))];
161 struct msghdr msg
= { 0 };
163 /* Prepare to receive the structures */
164 iov
[0].iov_base
= &data_fd
;
165 iov
[0].iov_len
= sizeof(data_fd
);
168 msg
.msg_control
= recv_fd
;
169 msg
.msg_controllen
= sizeof(recv_fd
);
171 printf("Waiting to receive fd\n");
172 if ((ret
= recvmsg(sock
, &msg
, 0)) < 0) {
176 if (ret
!= sizeof(data_fd
)) {
177 printf("Received %d bytes, expected %ld", ret
, sizeof(data_fd
));
180 cmsg
= CMSG_FIRSTHDR(&msg
);
182 printf("Invalid control message header\n");
186 if (cmsg
->cmsg_level
!= SOL_SOCKET
|| cmsg
->cmsg_type
!= SCM_RIGHTS
) {
187 printf("Didn't received any fd\n");
192 ret
= ((int *) CMSG_DATA(cmsg
))[0];
193 printf("received fd %d\n", ret
);
200 int open_streams(int sock
, int channel_handle
, struct object_data
*stream_datas
,
206 struct lttcomm_ust_msg lum
;
207 struct lttcomm_ust_reply lur
;
209 memset(&lum
, 0, sizeof(lum
));
210 lum
.handle
= channel_handle
;
211 lum
.cmd
= LTTNG_UST_STREAM
;
212 ret
= send_app_cmd(sock
, &lum
, &lur
);
214 assert(k
< nr_check
);
215 stream_datas
[k
].handle
= lur
.ret_val
;
216 printf("received stream handle %u\n",
217 stream_datas
[k
].handle
);
218 if (lur
.ret_code
== LTTCOMM_OK
) {
221 stream_datas
[k
].memory_map_size
= lur
.u
.stream
.memory_map_size
;
223 len
= lttcomm_recv_fd(sock
);
226 stream_datas
[k
].shm_fd
= len
;
228 len
= lttcomm_recv_fd(sock
);
231 stream_datas
[k
].wait_fd
= len
;
244 int close_streams(int sock
, struct object_data
*stream_datas
, int nr_check
)
248 for (k
= 0; k
< nr_check
; k
++) {
249 struct lttcomm_ust_msg lum
;
250 struct lttcomm_ust_reply lur
;
252 if (!stream_datas
[k
].handle
)
254 memset(&lum
, 0, sizeof(lum
));
255 lum
.handle
= stream_datas
[k
].handle
;
256 lum
.cmd
= LTTNG_UST_RELEASE
;
257 ret
= send_app_cmd(sock
, &lum
, &lur
);
259 printf("Error closing stream\n");
262 if (stream_datas
[k
].shm_fd
>= 0) {
263 ret
= close(stream_datas
[k
].shm_fd
);
265 printf("Error closing stream shm_fd\n");
269 if (stream_datas
[k
].wait_fd
>= 0) {
270 ret
= close(stream_datas
[k
].wait_fd
);
272 printf("Error closing stream wait_fd\n");
281 struct shm_handle
*map_channel(struct object_data
*chan_data
,
282 struct object_data
*stream_datas
, int nr_check
)
284 struct shm_handle
*handle
;
285 struct channel
*chan
;
288 /* map metadata channel */
289 handle
= channel_handle_create(chan_data
->shm_fd
,
291 chan_data
->memory_map_size
);
293 printf("create handle error\n");
296 chan_data
->shm_fd
= -1;
297 chan_data
->wait_fd
= -1;
298 chan
= shmp(handle
, handle
->chan
);
300 for (k
= 0; k
< nr_check
; k
++) {
301 struct object_data
*stream_data
= &stream_datas
[k
];
303 if (!stream_data
->handle
)
306 ret
= channel_handle_add_stream(handle
,
308 stream_data
->wait_fd
,
309 stream_data
->memory_map_size
);
311 printf("add stream error\n");
314 stream_data
->shm_fd
= -1;
315 stream_data
->wait_fd
= -1;
320 channel_destroy(chan
, handle
, 1);
325 void unmap_channel(struct shm_handle
*handle
)
327 struct channel
*chan
;
329 chan
= shmp(handle
, handle
->chan
);
331 channel_destroy(chan
, handle
, 1);
335 int consume_stream(struct shm_handle
*handle
, int cpu
, char *outfile
)
337 struct channel
*chan
;
338 struct lib_ring_buffer
*buf
;
341 uint64_t memory_map_size
;
343 chan
= shmp(handle
, handle
->chan
);
346 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
347 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
350 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
356 outfd
= open(outfile
, O_WRONLY
| O_CREAT
| O_LARGEFILE
| O_TRUNC
,
357 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
359 perror("open output");
363 printf("Waiting for buffer data for %s\n", outfile
);
365 unsigned long read_size
;
366 unsigned long copy_size
;
369 ret
= lib_ring_buffer_get_next_subbuf(buf
, handle
);
370 printf("get next ret %d\n", ret
);
373 if (ret
== -EAGAIN
) {
378 printf("Error %d in lib_ring_buffer_get_next_subbuf\n", ret
);
381 read_size
= lib_ring_buffer_get_read_data_size(
382 &chan
->backend
.config
, buf
, handle
);
383 read_size
= PAGE_ALIGN(read_size
);
384 ptr
= lib_ring_buffer_read_offset_address(
385 &buf
->backend
, 0, handle
);
386 printf("WRITE: copy %lu bytes\n", read_size
);
387 copy_size
= write(outfd
, ptr
, read_size
);
388 if (copy_size
< read_size
) {
389 printf("write issue: copied %zd, expected %lu\n", copy_size
, read_size
);
391 lib_ring_buffer_put_next_subbuf(buf
, handle
);
401 lib_ring_buffer_release_read(buf
, handle
, 1);
406 int consume_buffers(const char *outputpath
)
410 char pathname
[PATH_MAX
];
411 struct shm_handle
*handle
;
413 snprintf(pathname
, PATH_MAX
- 1, "%s", outputpath
);
414 old_umask
= umask(0);
415 ret
= mkdir(pathname
, S_IRWXU
| S_IRWXG
);
416 if (ret
&& errno
!= EEXIST
) {
424 handle
= map_channel(&metadata_data
,
425 &metadata_stream_data
, 1);
428 snprintf(pathname
, PATH_MAX
- 1,
429 "%s/metadata", outputpath
);
430 ret
= consume_stream(handle
, -1, pathname
);
431 if (ret
&& ret
!= -ENOENT
) {
432 printf("Error in consume_stream\n");
435 unmap_channel(handle
);
437 /* copy binary data */
438 handle
= map_channel(&channel_data
,
439 stream_data
, MAX_NR_STREAMS
);
442 for (k
= 0; k
< MAX_NR_STREAMS
; k
++) {
443 snprintf(pathname
, PATH_MAX
- 1,
444 "%s/data_%u", outputpath
, k
);
445 ret
= consume_stream(handle
, k
, pathname
);
446 if (ret
&& ret
!= -ENOENT
) {
447 printf("Error in consume_stream\n");
451 unmap_channel(handle
);
456 int send_app_msgs(int sock
, const char *outputpath
,
457 unsigned int nr_events
, const char **event_names
)
459 struct lttcomm_ust_msg lum
;
460 struct lttcomm_ust_reply lur
;
464 memset(&lum
, 0, sizeof(lum
));
465 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
466 lum
.cmd
= LTTNG_UST_SESSION
;
467 ret
= send_app_cmd(sock
, &lum
, &lur
);
470 session_handle
= lur
.ret_val
;
471 printf("received session handle %u\n", session_handle
);
473 /* Create metadata channel */
474 memset(&lum
, 0, sizeof(lum
));
475 lum
.handle
= session_handle
;
476 lum
.cmd
= LTTNG_UST_METADATA
;
477 lum
.u
.channel
.overwrite
= 0;
478 lum
.u
.channel
.subbuf_size
= 32768;
479 lum
.u
.channel
.num_subbuf
= 4;
480 lum
.u
.channel
.switch_timer_interval
= 0;
481 lum
.u
.channel
.read_timer_interval
= 0;
482 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
483 ret
= send_app_cmd(sock
, &lum
, &lur
);
486 metadata_data
.handle
= lur
.ret_val
;
487 printf("received metadata handle %u\n", metadata_data
.handle
);
488 if (lur
.ret_code
== LTTCOMM_OK
) {
491 metadata_data
.memory_map_size
= lur
.u
.channel
.memory_map_size
;
493 len
= lttcomm_recv_fd(sock
);
496 metadata_data
.shm_fd
= len
;
498 len
= lttcomm_recv_fd(sock
);
501 metadata_data
.wait_fd
= len
;
504 ret
= open_streams(sock
, metadata_data
.handle
,
505 &metadata_stream_data
, 1);
507 printf("Error in open_streams\n");
511 /* Create data channel */
512 memset(&lum
, 0, sizeof(lum
));
513 lum
.handle
= session_handle
;
514 lum
.cmd
= LTTNG_UST_CHANNEL
;
515 //lum.u.channel.overwrite = 0;
516 lum
.u
.channel
.overwrite
= 1;
517 lum
.u
.channel
.subbuf_size
= 32768;
518 lum
.u
.channel
.num_subbuf
= 8;
519 //lum.u.channel.num_subbuf = 4;
520 //lum.u.channel.num_subbuf = 2;
521 lum
.u
.channel
.switch_timer_interval
= 0;
522 lum
.u
.channel
.read_timer_interval
= 0;
523 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
524 ret
= send_app_cmd(sock
, &lum
, &lur
);
527 channel_data
.handle
= lur
.ret_val
;
528 printf("received channel handle %u\n", channel_data
.handle
);
529 if (lur
.ret_code
== LTTCOMM_OK
) {
532 channel_data
.memory_map_size
= lur
.u
.channel
.memory_map_size
;
534 len
= lttcomm_recv_fd(sock
);
537 channel_data
.shm_fd
= len
;
539 len
= lttcomm_recv_fd(sock
);
542 channel_data
.wait_fd
= len
;
546 for (k
= 0; k
< nr_events
; k
++) {
547 memset(&lum
, 0, sizeof(lum
));
548 lum
.handle
= channel_data
.handle
;
549 lum
.cmd
= LTTNG_UST_EVENT
;
550 strncpy(lum
.u
.event
.name
, event_names
[k
],
551 LTTNG_UST_SYM_NAME_LEN
);
552 lum
.u
.event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
553 ret
= send_app_cmd(sock
, &lum
, &lur
);
556 event_handle
[k
] = lur
.ret_val
;
557 printf("received event handle %u\n", event_handle
[k
]);
560 /* Get references to channel streams */
561 ret
= open_streams(sock
, channel_data
.handle
,
562 stream_data
, MAX_NR_STREAMS
);
564 printf("Error in open_streams\n");
568 memset(&lum
, 0, sizeof(lum
));
569 lum
.handle
= session_handle
;
570 lum
.cmd
= LTTNG_UST_SESSION_START
;
571 ret
= send_app_cmd(sock
, &lum
, &lur
);
574 printf("Session handle %u started.\n", session_handle
);
576 /* Tell application registration is done */
577 memset(&lum
, 0, sizeof(lum
));
578 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
579 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
580 ret
= send_app_cmd(sock
, &lum
, &lur
);
583 printf("Registration done acknowledged.\n");
587 ret
= consume_buffers(outputpath
);
589 printf("Error in consume_buffers\n");
593 /* Release data channel */
594 /* Release streams */
595 ret
= close_streams(sock
, stream_data
,
601 for (k
= 0; k
< nr_events
; k
++) {
602 memset(&lum
, 0, sizeof(lum
));
603 lum
.handle
= event_handle
[k
];
604 lum
.cmd
= LTTNG_UST_RELEASE
;
605 ret
= send_app_cmd(sock
, &lum
, &lur
);
609 memset(&lum
, 0, sizeof(lum
));
610 lum
.handle
= channel_data
.handle
;
611 lum
.cmd
= LTTNG_UST_RELEASE
;
612 ret
= send_app_cmd(sock
, &lum
, &lur
);
615 if (channel_data
.shm_fd
>= 0) {
616 ret
= close(channel_data
.shm_fd
);
620 if (channel_data
.wait_fd
>= 0) {
621 ret
= close(channel_data
.wait_fd
);
626 /* Release metadata channel */
627 ret
= close_streams(sock
, &metadata_stream_data
, 1);
631 memset(&lum
, 0, sizeof(lum
));
632 lum
.handle
= metadata_data
.handle
;
633 lum
.cmd
= LTTNG_UST_RELEASE
;
634 ret
= send_app_cmd(sock
, &lum
, &lur
);
637 if (metadata_data
.shm_fd
>= 0) {
638 ret
= close(metadata_data
.shm_fd
);
642 if (metadata_data
.wait_fd
>= 0) {
643 ret
= close(metadata_data
.wait_fd
);
648 /* Release session */
649 memset(&lum
, 0, sizeof(lum
));
650 lum
.handle
= session_handle
;
651 lum
.cmd
= LTTNG_UST_RELEASE
;
652 ret
= send_app_cmd(sock
, &lum
, &lur
);
660 * Using fork to set umask in the child process (not multi-thread safe). We
661 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
662 * the shm and does not let everybody modify it, to ensure safety against
663 * shm_unlink) by simply letting the mmap fail and retrying after a few
664 * seconds. For global shm, everybody has rw access to it until the sessiond
667 static int get_wait_shm(char *shm_path
, size_t mmap_size
, int global
)
669 int wait_shm_fd
, ret
;
672 /* Default permissions */
673 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
675 /* Change owner of the shm path */
677 ret
= chown(shm_path
, 0, 0);
679 if (errno
!= ENOENT
) {
680 perror("chown wait shm");
686 * If global session daemon, any application can register so the shm
687 * needs to be set in read-only mode for others.
691 ret
= chown(shm_path
, getuid(), getgid());
693 if (errno
!= ENOENT
) {
694 perror("chown wait shm");
701 * Set permissions to the shm even if we did not create the shm.
703 ret
= chmod(shm_path
, mode
);
705 if (errno
!= ENOENT
) {
706 perror("chmod wait shm");
712 * We're alone in a child process, so we can modify the process-wide
718 * Try creating shm (or get rw access). We don't do an exclusive open,
719 * because we allow other processes to create+ftruncate it concurrently.
721 wait_shm_fd
= shm_open(shm_path
, O_RDWR
| O_CREAT
, mode
);
722 if (wait_shm_fd
< 0) {
723 perror("shm_open wait shm");
727 ret
= ftruncate(wait_shm_fd
, mmap_size
);
729 perror("ftruncate wait shm");
733 ret
= fchmod(wait_shm_fd
, mode
);
739 printf("Got the wait shm fd %d\n", wait_shm_fd
);
744 printf("Failing to get the wait shm fd\n");
749 int update_futex(int fd
, int active
)
751 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
755 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
,
757 if (wait_shm_mmap
== MAP_FAILED
) {
763 uatomic_set((int32_t *) wait_shm_mmap
, 1);
764 futex_async((int32_t *) wait_shm_mmap
, FUTEX_WAKE
,
765 INT_MAX
, NULL
, NULL
, 0);
767 uatomic_set((int32_t *) wait_shm_mmap
, 0);
769 ret
= munmap(wait_shm_mmap
, mmap_size
);
771 perror("Error unmapping wait shm");
780 * Set open files limit to unlimited. This daemon can open a large number of
781 * file descriptors in order to consumer multiple kernel traces.
783 static void set_ulimit(void)
789 * If not root, we cannot increase our max open files. But our
790 * scope is then limited to processes from a single user.
794 /* The kernel does not allowed an infinite limit for open files */
795 lim
.rlim_cur
= 65535;
796 lim
.rlim_max
= 65535;
798 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
800 perror("failed to set open files limit");
806 * ./ust-basic-tracing outputpath event_name1 event_name2 ....
808 int main(int argc
, const char **argv
)
810 const char *home_dir
;
811 int ret
, wait_shm_fd
;
812 struct sigaction act
;
814 const char *outputpath
;
815 const char **event_names
;
816 unsigned int nr_events
;
820 printf("%s outputpath event_name1 event_name2 ...\n",
824 outputpath
= argv
[1];
825 event_names
= &argv
[2];
826 nr_events
= argc
- 2;
831 memset(&act
, 0, sizeof(act
));
832 ret
= sigemptyset(&act
.sa_mask
);
834 perror("sigemptyset");
838 act
.sa_handler
= SIG_IGN
;
839 ret
= sigaction(SIGPIPE
, &act
, NULL
);
846 act
.sa_handler
= handle_signals
;
847 ret
= sigaction(SIGTERM
, &act
, NULL
);
853 ret
= sigaction(SIGINT
, &act
, NULL
);
859 if (geteuid() == 0) {
860 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
861 if (ret
&& errno
!= EEXIST
) {
865 wait_shm_fd
= get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
866 sysconf(_SC_PAGE_SIZE
), 1);
867 if (wait_shm_fd
< 0) {
868 perror("global wait shm error");
871 strcpy(apps_sock_path
, DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
872 old_umask
= umask(0);
874 snprintf(local_apps_wait_shm_path
, PATH_MAX
,
875 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
876 wait_shm_fd
= get_wait_shm(local_apps_wait_shm_path
,
877 sysconf(_SC_PAGE_SIZE
), 0);
878 if (wait_shm_fd
< 0) {
879 perror("local wait shm error");
882 home_dir
= (const char *) getenv("HOME");
884 perror("getenv error");
887 snprintf(apps_sock_path
, PATH_MAX
,
888 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
891 ret
= lttcomm_create_unix_sock(apps_sock_path
);
893 perror("create error");
899 /* File permission MUST be 666 */
900 ret
= chmod(apps_sock_path
,
901 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
903 printf("Set file permissions failed: %s\n", apps_sock_path
);
909 ret
= lttcomm_listen_unix_sock(apps_socket
);
911 perror("listen error");
915 /* wake up futexes */
916 ret
= update_futex(wait_shm_fd
, 1);
918 fprintf(stderr
, "Error wakeup futex\n");
932 char name
[16]; /* Process name */
939 printf("Accepting application registration\n");
940 sock
= lttcomm_accept_unix_sock(apps_socket
);
942 perror("accept error");
947 * Basic recv here to handle the very simple data
948 * that the libust send to register (reg_msg).
950 len
= lttcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
951 if (len
< 0 || len
!= sizeof(reg_msg
)) {
952 perror("lttcomm_recv_unix_sock");
955 memcpy(bufname
, reg_msg
.name
, 16);
957 printf("Application %s pid %u ppid %u uid %u gid %u has registered (version : %u.%u)\n",
958 bufname
, reg_msg
.pid
, reg_msg
.ppid
, reg_msg
.uid
,
959 reg_msg
.gid
, reg_msg
.major
, reg_msg
.minor
);
960 ret
= send_app_msgs(sock
, outputpath
, nr_events
, event_names
);
962 printf("Error in send_app_msgs.\n");
969 printf("quitting.\n");
970 /* Let applications know we are not responding anymore */
971 ret
= update_futex(wait_shm_fd
, 0);
973 fprintf(stderr
, "Error wakeup futex\n");