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
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License only.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #define _LARGEFILE64_SOURCE
30 #include <sys/types.h>
33 #include <sys/resource.h>
37 #include <urcu/futex.h>
38 #include <urcu/uatomic.h>
40 #include <sys/socket.h>
43 #include "../../libringbuffer/backend.h"
44 #include "../../libringbuffer/frontend.h"
45 #include "../../liblttng-ust/compat.h" /* For ENODATA */
47 #define MAX_NR_STREAMS 64
48 #define MAX_NR_EVENTS 128
50 static int session_handle
;
51 static struct lttng_ust_object_data metadata_stream_data
;
52 static struct lttng_ust_object_data metadata_data
;
53 static struct lttng_ust_object_data channel_data
;
54 static struct lttng_ust_object_data stream_data
[MAX_NR_STREAMS
];
55 static int event_handle
[MAX_NR_EVENTS
];
56 static int context_handle
;
58 static int apps_socket
= -1;
59 static char apps_sock_path
[PATH_MAX
];
60 static char local_apps_wait_shm_path
[PATH_MAX
];
62 static volatile int quit_program
;
64 static void handle_signals(int signo
)
70 int open_streams(int sock
, int channel_handle
, struct lttng_ust_object_data
*stream_datas
,
76 struct ustcomm_ust_msg lum
;
77 struct ustcomm_ust_reply lur
;
79 memset(&lum
, 0, sizeof(lum
));
80 lum
.handle
= channel_handle
;
81 lum
.cmd
= LTTNG_UST_STREAM
;
82 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
85 stream_datas
[k
].handle
= lur
.ret_val
;
86 printf("received stream handle %u\n",
87 stream_datas
[k
].handle
);
88 if (lur
.ret_code
== USTCOMM_OK
) {
91 stream_datas
[k
].memory_map_size
= lur
.u
.stream
.memory_map_size
;
93 len
= ustcomm_recv_fd(sock
);
96 stream_datas
[k
].shm_fd
= len
;
98 len
= ustcomm_recv_fd(sock
);
101 stream_datas
[k
].wait_fd
= len
;
114 int close_streams(int sock
, struct lttng_ust_object_data
*stream_datas
, int nr_check
)
118 for (k
= 0; k
< nr_check
; k
++) {
119 struct ustcomm_ust_msg lum
;
120 struct ustcomm_ust_reply lur
;
122 if (!stream_datas
[k
].handle
)
124 memset(&lum
, 0, sizeof(lum
));
125 lum
.handle
= stream_datas
[k
].handle
;
126 lum
.cmd
= LTTNG_UST_RELEASE
;
127 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
129 printf("Error closing stream\n");
132 if (stream_datas
[k
].shm_fd
>= 0) {
133 ret
= close(stream_datas
[k
].shm_fd
);
135 printf("Error closing stream shm_fd\n");
139 if (stream_datas
[k
].wait_fd
>= 0) {
140 ret
= close(stream_datas
[k
].wait_fd
);
142 printf("Error closing stream wait_fd\n");
151 struct lttng_ust_shm_handle
*map_channel(struct lttng_ust_object_data
*chan_data
,
152 struct lttng_ust_object_data
*stream_datas
, int nr_check
)
154 struct lttng_ust_shm_handle
*handle
;
155 struct channel
*chan
;
158 /* map metadata channel */
159 handle
= channel_handle_create(chan_data
->shm_fd
,
161 chan_data
->memory_map_size
);
163 printf("create handle error\n");
166 chan_data
->shm_fd
= -1;
167 chan_data
->wait_fd
= -1;
168 chan
= shmp(handle
, handle
->chan
);
170 for (k
= 0; k
< nr_check
; k
++) {
171 struct lttng_ust_object_data
*stream_data
= &stream_datas
[k
];
173 if (!stream_data
->handle
)
176 ret
= channel_handle_add_stream(handle
,
178 stream_data
->wait_fd
,
179 stream_data
->memory_map_size
);
181 printf("add stream error\n");
184 stream_data
->shm_fd
= -1;
185 stream_data
->wait_fd
= -1;
190 channel_destroy(chan
, handle
, 1);
195 void unmap_channel(struct lttng_ust_shm_handle
*handle
)
197 struct channel
*chan
;
199 chan
= shmp(handle
, handle
->chan
);
201 channel_destroy(chan
, handle
, 1);
205 int consume_stream(struct lttng_ust_shm_handle
*handle
, int cpu
, char *outfile
)
207 struct channel
*chan
;
208 struct lttng_ust_lib_ring_buffer
*buf
;
210 int *shm_fd
, *wait_fd
;
211 uint64_t *memory_map_size
;
213 chan
= shmp(handle
, handle
->chan
);
216 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
217 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
220 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
226 outfd
= open(outfile
, O_WRONLY
| O_CREAT
| O_TRUNC
,
227 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
229 perror("open output");
233 printf("Waiting for buffer data for %s\n", outfile
);
235 unsigned long read_size
;
236 unsigned long copy_size
;
239 ret
= lib_ring_buffer_get_next_subbuf(buf
, handle
);
240 printf("get next ret %d\n", ret
);
243 if (ret
== -EAGAIN
) {
248 printf("Error %d in lib_ring_buffer_get_next_subbuf\n", ret
);
251 read_size
= lib_ring_buffer_get_read_data_size(
252 &chan
->backend
.config
, buf
, handle
);
253 read_size
= PAGE_ALIGN(read_size
);
254 ptr
= lib_ring_buffer_read_offset_address(
255 &buf
->backend
, 0, handle
);
256 printf("WRITE: copy %lu bytes\n", read_size
);
257 copy_size
= write(outfd
, ptr
, read_size
);
258 if (copy_size
< read_size
) {
259 printf("write issue: copied %lu, expected %lu\n", copy_size
, read_size
);
261 lib_ring_buffer_put_next_subbuf(buf
, handle
);
271 lib_ring_buffer_release_read(buf
, handle
, 1);
276 int consume_buffers(const char *outputpath
)
280 char pathname
[PATH_MAX
];
281 struct lttng_ust_shm_handle
*handle
;
283 snprintf(pathname
, PATH_MAX
- 1, "%s", outputpath
);
284 old_umask
= umask(0);
285 ret
= mkdir(pathname
, S_IRWXU
| S_IRWXG
);
286 if (ret
&& errno
!= EEXIST
) {
294 handle
= map_channel(&metadata_data
,
295 &metadata_stream_data
, 1);
298 snprintf(pathname
, PATH_MAX
- 1,
299 "%s/metadata", outputpath
);
300 ret
= consume_stream(handle
, -1, pathname
);
301 if (ret
&& ret
!= -ENOENT
) {
302 printf("Error in consume_stream\n");
305 unmap_channel(handle
);
307 /* copy binary data */
308 handle
= map_channel(&channel_data
,
309 stream_data
, MAX_NR_STREAMS
);
312 for (k
= 0; k
< MAX_NR_STREAMS
; k
++) {
313 snprintf(pathname
, PATH_MAX
- 1,
314 "%s/data_%u", outputpath
, k
);
315 ret
= consume_stream(handle
, k
, pathname
);
316 if (ret
&& ret
!= -ENOENT
) {
317 printf("Error in consume_stream\n");
321 unmap_channel(handle
);
327 int send_app_msgs(int sock
, const char *outputpath
,
328 unsigned int nr_events
, const char **event_names
)
330 struct ustcomm_ust_msg lum
;
331 struct ustcomm_ust_reply lur
;
335 memset(&lum
, 0, sizeof(lum
));
336 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
337 lum
.cmd
= LTTNG_UST_SESSION
;
338 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
341 session_handle
= lur
.ret_val
;
342 printf("received session handle %u\n", session_handle
);
344 /* Create metadata channel */
345 memset(&lum
, 0, sizeof(lum
));
346 lum
.handle
= session_handle
;
347 lum
.cmd
= LTTNG_UST_METADATA
;
348 lum
.u
.channel
.overwrite
= 0;
349 lum
.u
.channel
.subbuf_size
= 32768;
350 lum
.u
.channel
.num_subbuf
= 4;
351 lum
.u
.channel
.switch_timer_interval
= 0;
352 lum
.u
.channel
.read_timer_interval
= 0;
353 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
354 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
357 metadata_data
.handle
= lur
.ret_val
;
358 printf("received metadata handle %u\n", metadata_data
.handle
);
359 if (lur
.ret_code
== USTCOMM_OK
) {
362 metadata_data
.memory_map_size
= lur
.u
.channel
.memory_map_size
;
364 len
= ustcomm_recv_fd(sock
);
367 metadata_data
.shm_fd
= len
;
369 len
= ustcomm_recv_fd(sock
);
372 metadata_data
.wait_fd
= len
;
375 ret
= open_streams(sock
, metadata_data
.handle
,
376 &metadata_stream_data
, 1);
378 printf("Error in open_streams\n");
382 /* Create data channel */
383 memset(&lum
, 0, sizeof(lum
));
384 lum
.handle
= session_handle
;
385 lum
.cmd
= LTTNG_UST_CHANNEL
;
386 //lum.u.channel.overwrite = 0;
387 lum
.u
.channel
.overwrite
= 1;
388 lum
.u
.channel
.subbuf_size
= 32768;
389 lum
.u
.channel
.num_subbuf
= 8;
390 //lum.u.channel.num_subbuf = 4;
391 //lum.u.channel.num_subbuf = 2;
392 lum
.u
.channel
.switch_timer_interval
= 0;
393 lum
.u
.channel
.read_timer_interval
= 0;
394 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
395 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
398 channel_data
.handle
= lur
.ret_val
;
399 printf("received channel handle %u\n", channel_data
.handle
);
400 if (lur
.ret_code
== USTCOMM_OK
) {
403 channel_data
.memory_map_size
= lur
.u
.channel
.memory_map_size
;
405 len
= ustcomm_recv_fd(sock
);
408 channel_data
.shm_fd
= len
;
410 len
= ustcomm_recv_fd(sock
);
413 channel_data
.wait_fd
= len
;
417 for (k
= 0; k
< nr_events
; k
++) {
418 memset(&lum
, 0, sizeof(lum
));
419 lum
.handle
= channel_data
.handle
;
420 lum
.cmd
= LTTNG_UST_EVENT
;
421 strncpy(lum
.u
.event
.name
, event_names
[k
],
422 LTTNG_UST_SYM_NAME_LEN
);
423 lum
.u
.event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
424 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
427 event_handle
[k
] = lur
.ret_val
;
428 printf("received event handle %u\n", event_handle
[k
]);
431 /* Attach vtid context */
432 memset(&lum
, 0, sizeof(lum
));
433 lum
.handle
= channel_data
.handle
;
434 lum
.cmd
= LTTNG_UST_CONTEXT
;
435 lum
.u
.context
.ctx
= LTTNG_UST_CONTEXT_VTID
;
436 //lum.u.context.ctx = LTTNG_UST_CONTEXT_PTHREAD_ID;
437 //lum.u.context.ctx = LTTNG_UST_CONTEXT_VPID;
438 //lum.u.context.ctx = LTTNG_UST_CONTEXT_PROCNAME;
439 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
442 context_handle
= lur
.ret_val
;
443 printf("received context handle %u\n", context_handle
);
445 /* Get references to channel streams */
446 ret
= open_streams(sock
, channel_data
.handle
,
447 stream_data
, MAX_NR_STREAMS
);
449 printf("Error in open_streams\n");
453 memset(&lum
, 0, sizeof(lum
));
454 lum
.handle
= session_handle
;
455 lum
.cmd
= LTTNG_UST_SESSION_START
;
456 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
459 printf("Session handle %u started.\n", session_handle
);
461 /* Tell application registration is done */
462 memset(&lum
, 0, sizeof(lum
));
463 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
464 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
465 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
468 printf("Registration done acknowledged.\n");
472 ret
= consume_buffers(outputpath
);
474 printf("Error in consume_buffers\n");
478 /* Release data channel */
479 /* Release streams */
480 ret
= close_streams(sock
, stream_data
,
485 /* Release context */
486 memset(&lum
, 0, sizeof(lum
));
487 lum
.handle
= context_handle
;
488 lum
.cmd
= LTTNG_UST_RELEASE
;
489 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
494 for (k
= 0; k
< nr_events
; k
++) {
495 memset(&lum
, 0, sizeof(lum
));
496 lum
.handle
= event_handle
[k
];
497 lum
.cmd
= LTTNG_UST_RELEASE
;
498 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
502 memset(&lum
, 0, sizeof(lum
));
503 lum
.handle
= channel_data
.handle
;
504 lum
.cmd
= LTTNG_UST_RELEASE
;
505 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
508 if (channel_data
.shm_fd
>= 0) {
509 ret
= close(channel_data
.shm_fd
);
513 if (channel_data
.wait_fd
>= 0) {
514 ret
= close(channel_data
.wait_fd
);
519 /* Release metadata channel */
520 ret
= close_streams(sock
, &metadata_stream_data
, 1);
524 memset(&lum
, 0, sizeof(lum
));
525 lum
.handle
= metadata_data
.handle
;
526 lum
.cmd
= LTTNG_UST_RELEASE
;
527 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
530 if (metadata_data
.shm_fd
>= 0) {
531 ret
= close(metadata_data
.shm_fd
);
535 if (metadata_data
.wait_fd
>= 0) {
536 ret
= close(metadata_data
.wait_fd
);
541 /* Release session */
542 memset(&lum
, 0, sizeof(lum
));
543 lum
.handle
= session_handle
;
544 lum
.cmd
= LTTNG_UST_RELEASE
;
545 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
553 * Using fork to set umask in the child process (not multi-thread safe). We
554 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
555 * the shm and does not let everybody modify it, to ensure safety against
556 * shm_unlink) by simply letting the mmap fail and retrying after a few
557 * seconds. For global shm, everybody has rw access to it until the sessiond
560 static int get_wait_shm(char *shm_path
, size_t mmap_size
, int global
)
562 int wait_shm_fd
, ret
;
565 /* Default permissions */
566 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
568 /* Change owner of the shm path */
570 ret
= chown(shm_path
, 0, 0);
572 if (errno
!= ENOENT
) {
573 perror("chown wait shm");
579 * If global session daemon, any application can register so the shm
580 * needs to be set in read-only mode for others.
584 ret
= chown(shm_path
, getuid(), getgid());
586 if (errno
!= ENOENT
) {
587 perror("chown wait shm");
594 * Set permissions to the shm even if we did not create the shm.
596 ret
= chmod(shm_path
, mode
);
598 if (errno
!= ENOENT
) {
599 perror("chmod wait shm");
605 * We're alone in a child process, so we can modify the process-wide
611 * Try creating shm (or get rw access). We don't do an exclusive open,
612 * because we allow other processes to create+ftruncate it concurrently.
614 wait_shm_fd
= shm_open(shm_path
, O_RDWR
| O_CREAT
, mode
);
615 if (wait_shm_fd
< 0) {
616 perror("shm_open wait shm");
620 ret
= ftruncate(wait_shm_fd
, mmap_size
);
622 perror("ftruncate wait shm");
626 ret
= fchmod(wait_shm_fd
, mode
);
632 printf("Got the wait shm fd %d\n", wait_shm_fd
);
637 printf("Failing to get the wait shm fd\n");
642 int update_futex(int fd
, int active
)
644 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
648 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
,
650 if (wait_shm_mmap
== MAP_FAILED
) {
656 uatomic_set((int32_t *) wait_shm_mmap
, 1);
657 futex_async((int32_t *) wait_shm_mmap
, FUTEX_WAKE
,
658 INT_MAX
, NULL
, NULL
, 0);
660 uatomic_set((int32_t *) wait_shm_mmap
, 0);
662 ret
= munmap(wait_shm_mmap
, mmap_size
);
664 perror("Error unmapping wait shm");
673 * Set open files limit to unlimited. This daemon can open a large number of
674 * file descriptors in order to consumer multiple kernel traces.
676 static void set_ulimit(void)
682 * If not root, we cannot increase our max open files. But our
683 * scope is then limited to processes from a single user.
687 /* The kernel does not allowed an infinite limit for open files */
688 lim
.rlim_cur
= 65535;
689 lim
.rlim_max
= 65535;
691 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
693 perror("failed to set open files limit");
699 * ./ust-basic-tracing outputpath event_name1 event_name2 ....
701 int main(int argc
, const char **argv
)
703 const char *home_dir
;
704 int ret
, wait_shm_fd
;
705 struct sigaction act
;
706 mode_t old_umask
= 0;
707 const char *outputpath
;
708 const char **event_names
;
709 unsigned int nr_events
;
713 printf("%s outputpath event_name1 event_name2 ...\n",
717 outputpath
= argv
[1];
718 event_names
= &argv
[2];
719 nr_events
= argc
- 2;
724 memset(&act
, 0, sizeof(act
));
725 ret
= sigemptyset(&act
.sa_mask
);
727 perror("sigemptyset");
731 act
.sa_handler
= SIG_IGN
;
732 ret
= sigaction(SIGPIPE
, &act
, NULL
);
739 act
.sa_handler
= handle_signals
;
740 ret
= sigaction(SIGTERM
, &act
, NULL
);
746 ret
= sigaction(SIGINT
, &act
, NULL
);
752 if (geteuid() == 0) {
753 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
754 if (ret
&& errno
!= EEXIST
) {
758 wait_shm_fd
= get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
759 sysconf(_SC_PAGE_SIZE
), 1);
760 if (wait_shm_fd
< 0) {
761 perror("global wait shm error");
764 strcpy(apps_sock_path
, DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
765 old_umask
= umask(0);
767 snprintf(local_apps_wait_shm_path
, PATH_MAX
,
768 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
769 wait_shm_fd
= get_wait_shm(local_apps_wait_shm_path
,
770 sysconf(_SC_PAGE_SIZE
), 0);
771 if (wait_shm_fd
< 0) {
772 perror("local wait shm error");
775 home_dir
= (const char *) getenv("HOME");
777 perror("getenv error");
780 snprintf(apps_sock_path
, PATH_MAX
,
781 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
784 ret
= ustcomm_create_unix_sock(apps_sock_path
);
786 perror("create error");
792 /* File permission MUST be 666 */
793 ret
= chmod(apps_sock_path
,
794 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
796 printf("Set file permissions failed: %s\n", apps_sock_path
);
802 ret
= ustcomm_listen_unix_sock(apps_socket
);
804 perror("listen error");
808 /* wake up futexes */
809 ret
= update_futex(wait_shm_fd
, 1);
811 fprintf(stderr
, "Error wakeup futex\n");
825 char name
[16]; /* Process name */
832 printf("Accepting application registration\n");
833 sock
= ustcomm_accept_unix_sock(apps_socket
);
835 perror("accept error");
840 * Basic recv here to handle the very simple data
841 * that the libust send to register (reg_msg).
843 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
844 if (len
< 0 || len
!= sizeof(reg_msg
)) {
845 perror("ustcomm_recv_unix_sock");
848 memcpy(bufname
, reg_msg
.name
, 16);
850 printf("Application %s pid %u ppid %u uid %u gid %u has registered (version : %u.%u)\n",
851 bufname
, reg_msg
.pid
, reg_msg
.ppid
, reg_msg
.uid
,
852 reg_msg
.gid
, reg_msg
.major
, reg_msg
.minor
);
853 ret
= send_app_msgs(sock
, outputpath
, nr_events
, event_names
);
855 printf("Error in send_app_msgs.\n");
862 printf("quitting.\n");
863 /* Let applications know we are not responding anymore */
864 ret
= update_futex(wait_shm_fd
, 0);
866 fprintf(stderr
, "Error wakeup futex\n");