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
30 #include <sys/types.h>
35 #include <urcu/futex.h>
36 #include <urcu/uatomic.h>
38 #include <sys/socket.h>
41 #include "../../libringbuffer/backend.h"
42 #include "../../libringbuffer/frontend.h"
43 #include "../../liblttng-ust/compat.h" /* For ENODATA */
45 #define MAX_NR_STREAMS 64
46 #define MAX_NR_EVENTS 128
48 static int session_handle
;
49 static struct lttng_ust_object_data metadata_stream_data
;
50 static struct lttng_ust_object_data metadata_data
;
51 static struct lttng_ust_object_data channel_data
;
52 static struct lttng_ust_object_data stream_data
[MAX_NR_STREAMS
];
53 static int event_handle
[MAX_NR_EVENTS
];
54 static int context_handle
;
56 static int apps_socket
= -1;
57 static char apps_sock_path
[PATH_MAX
];
58 static char local_apps_wait_shm_path
[PATH_MAX
];
60 static volatile int quit_program
;
62 static void handle_signals(int signo
)
68 int open_streams(int sock
, int channel_handle
, struct lttng_ust_object_data
*stream_datas
,
74 struct ustcomm_ust_msg lum
;
75 struct ustcomm_ust_reply lur
;
77 memset(&lum
, 0, sizeof(lum
));
78 lum
.handle
= channel_handle
;
79 lum
.cmd
= LTTNG_UST_STREAM
;
80 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
83 stream_datas
[k
].handle
= lur
.ret_val
;
84 printf("received stream handle %u\n",
85 stream_datas
[k
].handle
);
86 if (lur
.ret_code
== USTCOMM_OK
) {
89 stream_datas
[k
].memory_map_size
= lur
.u
.stream
.memory_map_size
;
91 len
= ustcomm_recv_fd(sock
);
94 stream_datas
[k
].shm_fd
= len
;
96 len
= ustcomm_recv_fd(sock
);
99 stream_datas
[k
].wait_fd
= len
;
112 int close_streams(int sock
, struct lttng_ust_object_data
*stream_datas
, int nr_check
)
116 for (k
= 0; k
< nr_check
; k
++) {
117 struct ustcomm_ust_msg lum
;
118 struct ustcomm_ust_reply lur
;
120 if (!stream_datas
[k
].handle
)
122 memset(&lum
, 0, sizeof(lum
));
123 lum
.handle
= stream_datas
[k
].handle
;
124 lum
.cmd
= LTTNG_UST_RELEASE
;
125 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
127 printf("Error closing stream\n");
130 if (stream_datas
[k
].shm_fd
>= 0) {
131 ret
= close(stream_datas
[k
].shm_fd
);
133 printf("Error closing stream shm_fd\n");
137 if (stream_datas
[k
].wait_fd
>= 0) {
138 ret
= close(stream_datas
[k
].wait_fd
);
140 printf("Error closing stream wait_fd\n");
149 struct lttng_ust_shm_handle
*map_channel(struct lttng_ust_object_data
*chan_data
,
150 struct lttng_ust_object_data
*stream_datas
, int nr_check
)
152 struct lttng_ust_shm_handle
*handle
;
153 struct channel
*chan
;
156 /* map metadata channel */
157 handle
= channel_handle_create(chan_data
->shm_fd
,
159 chan_data
->memory_map_size
);
161 printf("create handle error\n");
164 chan_data
->shm_fd
= -1;
165 chan_data
->wait_fd
= -1;
166 chan
= shmp(handle
, handle
->chan
);
168 for (k
= 0; k
< nr_check
; k
++) {
169 struct lttng_ust_object_data
*stream_data
= &stream_datas
[k
];
171 if (!stream_data
->handle
)
174 ret
= channel_handle_add_stream(handle
,
176 stream_data
->wait_fd
,
177 stream_data
->memory_map_size
);
179 printf("add stream error\n");
182 stream_data
->shm_fd
= -1;
183 stream_data
->wait_fd
= -1;
188 channel_destroy(chan
, handle
, 1);
193 void unmap_channel(struct lttng_ust_shm_handle
*handle
)
195 struct channel
*chan
;
197 chan
= shmp(handle
, handle
->chan
);
199 channel_destroy(chan
, handle
, 1);
203 int consume_stream(struct lttng_ust_shm_handle
*handle
, int cpu
, char *outfile
)
205 struct channel
*chan
;
206 struct lttng_ust_lib_ring_buffer
*buf
;
208 int *shm_fd
, *wait_fd
;
209 uint64_t *memory_map_size
;
211 chan
= shmp(handle
, handle
->chan
);
214 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
215 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
218 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
224 outfd
= open(outfile
, O_WRONLY
| O_CREAT
| O_LARGEFILE
| O_TRUNC
,
225 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
227 perror("open output");
231 printf("Waiting for buffer data for %s\n", outfile
);
233 unsigned long read_size
;
234 unsigned long copy_size
;
237 ret
= lib_ring_buffer_get_next_subbuf(buf
, handle
);
238 printf("get next ret %d\n", ret
);
241 if (ret
== -EAGAIN
) {
246 printf("Error %d in lib_ring_buffer_get_next_subbuf\n", ret
);
249 read_size
= lib_ring_buffer_get_read_data_size(
250 &chan
->backend
.config
, buf
, handle
);
251 read_size
= PAGE_ALIGN(read_size
);
252 ptr
= lib_ring_buffer_read_offset_address(
253 &buf
->backend
, 0, handle
);
254 printf("WRITE: copy %lu bytes\n", read_size
);
255 copy_size
= write(outfd
, ptr
, read_size
);
256 if (copy_size
< read_size
) {
257 printf("write issue: copied %lu, expected %lu\n", copy_size
, read_size
);
259 lib_ring_buffer_put_next_subbuf(buf
, handle
);
269 lib_ring_buffer_release_read(buf
, handle
, 1);
274 int consume_buffers(const char *outputpath
)
278 char pathname
[PATH_MAX
];
279 struct lttng_ust_shm_handle
*handle
;
281 snprintf(pathname
, PATH_MAX
- 1, "%s", outputpath
);
282 old_umask
= umask(0);
283 ret
= mkdir(pathname
, S_IRWXU
| S_IRWXG
);
284 if (ret
&& errno
!= EEXIST
) {
292 handle
= map_channel(&metadata_data
,
293 &metadata_stream_data
, 1);
296 snprintf(pathname
, PATH_MAX
- 1,
297 "%s/metadata", outputpath
);
298 ret
= consume_stream(handle
, -1, pathname
);
299 if (ret
&& ret
!= -ENOENT
) {
300 printf("Error in consume_stream\n");
303 unmap_channel(handle
);
305 /* copy binary data */
306 handle
= map_channel(&channel_data
,
307 stream_data
, MAX_NR_STREAMS
);
310 for (k
= 0; k
< MAX_NR_STREAMS
; k
++) {
311 snprintf(pathname
, PATH_MAX
- 1,
312 "%s/data_%u", outputpath
, k
);
313 ret
= consume_stream(handle
, k
, pathname
);
314 if (ret
&& ret
!= -ENOENT
) {
315 printf("Error in consume_stream\n");
319 unmap_channel(handle
);
325 int send_app_msgs(int sock
, const char *outputpath
,
326 unsigned int nr_events
, const char **event_names
)
328 struct ustcomm_ust_msg lum
;
329 struct ustcomm_ust_reply lur
;
333 memset(&lum
, 0, sizeof(lum
));
334 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
335 lum
.cmd
= LTTNG_UST_SESSION
;
336 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
339 session_handle
= lur
.ret_val
;
340 printf("received session handle %u\n", session_handle
);
342 /* Create metadata channel */
343 memset(&lum
, 0, sizeof(lum
));
344 lum
.handle
= session_handle
;
345 lum
.cmd
= LTTNG_UST_METADATA
;
346 lum
.u
.channel
.overwrite
= 0;
347 lum
.u
.channel
.subbuf_size
= 32768;
348 lum
.u
.channel
.num_subbuf
= 4;
349 lum
.u
.channel
.switch_timer_interval
= 0;
350 lum
.u
.channel
.read_timer_interval
= 0;
351 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
352 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
355 metadata_data
.handle
= lur
.ret_val
;
356 printf("received metadata handle %u\n", metadata_data
.handle
);
357 if (lur
.ret_code
== USTCOMM_OK
) {
360 metadata_data
.memory_map_size
= lur
.u
.channel
.memory_map_size
;
362 len
= ustcomm_recv_fd(sock
);
365 metadata_data
.shm_fd
= len
;
367 len
= ustcomm_recv_fd(sock
);
370 metadata_data
.wait_fd
= len
;
373 ret
= open_streams(sock
, metadata_data
.handle
,
374 &metadata_stream_data
, 1);
376 printf("Error in open_streams\n");
380 /* Create data channel */
381 memset(&lum
, 0, sizeof(lum
));
382 lum
.handle
= session_handle
;
383 lum
.cmd
= LTTNG_UST_CHANNEL
;
384 //lum.u.channel.overwrite = 0;
385 lum
.u
.channel
.overwrite
= 1;
386 lum
.u
.channel
.subbuf_size
= 32768;
387 lum
.u
.channel
.num_subbuf
= 8;
388 //lum.u.channel.num_subbuf = 4;
389 //lum.u.channel.num_subbuf = 2;
390 lum
.u
.channel
.switch_timer_interval
= 0;
391 lum
.u
.channel
.read_timer_interval
= 0;
392 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
393 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
396 channel_data
.handle
= lur
.ret_val
;
397 printf("received channel handle %u\n", channel_data
.handle
);
398 if (lur
.ret_code
== USTCOMM_OK
) {
401 channel_data
.memory_map_size
= lur
.u
.channel
.memory_map_size
;
403 len
= ustcomm_recv_fd(sock
);
406 channel_data
.shm_fd
= len
;
408 len
= ustcomm_recv_fd(sock
);
411 channel_data
.wait_fd
= len
;
415 for (k
= 0; k
< nr_events
; k
++) {
416 memset(&lum
, 0, sizeof(lum
));
417 lum
.handle
= channel_data
.handle
;
418 lum
.cmd
= LTTNG_UST_EVENT
;
419 strncpy(lum
.u
.event
.name
, event_names
[k
],
420 LTTNG_UST_SYM_NAME_LEN
);
421 lum
.u
.event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
422 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
425 event_handle
[k
] = lur
.ret_val
;
426 printf("received event handle %u\n", event_handle
[k
]);
429 /* Attach vtid context */
430 memset(&lum
, 0, sizeof(lum
));
431 lum
.handle
= channel_data
.handle
;
432 lum
.cmd
= LTTNG_UST_CONTEXT
;
433 lum
.u
.context
.ctx
= LTTNG_UST_CONTEXT_VTID
;
434 //lum.u.context.ctx = LTTNG_UST_CONTEXT_PTHREAD_ID;
435 //lum.u.context.ctx = LTTNG_UST_CONTEXT_VPID;
436 //lum.u.context.ctx = LTTNG_UST_CONTEXT_PROCNAME;
437 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
440 context_handle
= lur
.ret_val
;
441 printf("received context handle %u\n", context_handle
);
443 /* Get references to channel streams */
444 ret
= open_streams(sock
, channel_data
.handle
,
445 stream_data
, MAX_NR_STREAMS
);
447 printf("Error in open_streams\n");
451 memset(&lum
, 0, sizeof(lum
));
452 lum
.handle
= session_handle
;
453 lum
.cmd
= LTTNG_UST_SESSION_START
;
454 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
457 printf("Session handle %u started.\n", session_handle
);
459 /* Tell application registration is done */
460 memset(&lum
, 0, sizeof(lum
));
461 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
462 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
463 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
466 printf("Registration done acknowledged.\n");
470 ret
= consume_buffers(outputpath
);
472 printf("Error in consume_buffers\n");
476 /* Release data channel */
477 /* Release streams */
478 ret
= close_streams(sock
, stream_data
,
483 /* Release context */
484 memset(&lum
, 0, sizeof(lum
));
485 lum
.handle
= context_handle
;
486 lum
.cmd
= LTTNG_UST_RELEASE
;
487 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
492 for (k
= 0; k
< nr_events
; k
++) {
493 memset(&lum
, 0, sizeof(lum
));
494 lum
.handle
= event_handle
[k
];
495 lum
.cmd
= LTTNG_UST_RELEASE
;
496 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
500 memset(&lum
, 0, sizeof(lum
));
501 lum
.handle
= channel_data
.handle
;
502 lum
.cmd
= LTTNG_UST_RELEASE
;
503 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
506 if (channel_data
.shm_fd
>= 0) {
507 ret
= close(channel_data
.shm_fd
);
511 if (channel_data
.wait_fd
>= 0) {
512 ret
= close(channel_data
.wait_fd
);
517 /* Release metadata channel */
518 ret
= close_streams(sock
, &metadata_stream_data
, 1);
522 memset(&lum
, 0, sizeof(lum
));
523 lum
.handle
= metadata_data
.handle
;
524 lum
.cmd
= LTTNG_UST_RELEASE
;
525 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
528 if (metadata_data
.shm_fd
>= 0) {
529 ret
= close(metadata_data
.shm_fd
);
533 if (metadata_data
.wait_fd
>= 0) {
534 ret
= close(metadata_data
.wait_fd
);
539 /* Release session */
540 memset(&lum
, 0, sizeof(lum
));
541 lum
.handle
= session_handle
;
542 lum
.cmd
= LTTNG_UST_RELEASE
;
543 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
551 * Using fork to set umask in the child process (not multi-thread safe). We
552 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
553 * the shm and does not let everybody modify it, to ensure safety against
554 * shm_unlink) by simply letting the mmap fail and retrying after a few
555 * seconds. For global shm, everybody has rw access to it until the sessiond
558 static int get_wait_shm(char *shm_path
, size_t mmap_size
, int global
)
560 int wait_shm_fd
, ret
;
563 /* Default permissions */
564 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
566 /* Change owner of the shm path */
568 ret
= chown(shm_path
, 0, 0);
570 if (errno
!= ENOENT
) {
571 perror("chown wait shm");
577 * If global session daemon, any application can register so the shm
578 * needs to be set in read-only mode for others.
582 ret
= chown(shm_path
, getuid(), getgid());
584 if (errno
!= ENOENT
) {
585 perror("chown wait shm");
592 * Set permissions to the shm even if we did not create the shm.
594 ret
= chmod(shm_path
, mode
);
596 if (errno
!= ENOENT
) {
597 perror("chmod wait shm");
603 * We're alone in a child process, so we can modify the process-wide
609 * Try creating shm (or get rw access). We don't do an exclusive open,
610 * because we allow other processes to create+ftruncate it concurrently.
612 wait_shm_fd
= shm_open(shm_path
, O_RDWR
| O_CREAT
, mode
);
613 if (wait_shm_fd
< 0) {
614 perror("shm_open wait shm");
618 ret
= ftruncate(wait_shm_fd
, mmap_size
);
620 perror("ftruncate wait shm");
624 ret
= fchmod(wait_shm_fd
, mode
);
630 printf("Got the wait shm fd %d\n", wait_shm_fd
);
635 printf("Failing to get the wait shm fd\n");
640 int update_futex(int fd
, int active
)
642 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
646 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
,
648 if (wait_shm_mmap
== MAP_FAILED
) {
654 uatomic_set((int32_t *) wait_shm_mmap
, 1);
655 futex_async((int32_t *) wait_shm_mmap
, FUTEX_WAKE
,
656 INT_MAX
, NULL
, NULL
, 0);
658 uatomic_set((int32_t *) wait_shm_mmap
, 0);
660 ret
= munmap(wait_shm_mmap
, mmap_size
);
662 perror("Error unmapping wait shm");
671 * Set open files limit to unlimited. This daemon can open a large number of
672 * file descriptors in order to consumer multiple kernel traces.
674 static void set_ulimit(void)
680 * If not root, we cannot increase our max open files. But our
681 * scope is then limited to processes from a single user.
685 /* The kernel does not allowed an infinite limit for open files */
686 lim
.rlim_cur
= 65535;
687 lim
.rlim_max
= 65535;
689 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
691 perror("failed to set open files limit");
697 * ./ust-basic-tracing outputpath event_name1 event_name2 ....
699 int main(int argc
, const char **argv
)
701 const char *home_dir
;
702 int ret
, wait_shm_fd
;
703 struct sigaction act
;
704 mode_t old_umask
= 0;
705 const char *outputpath
;
706 const char **event_names
;
707 unsigned int nr_events
;
711 printf("%s outputpath event_name1 event_name2 ...\n",
715 outputpath
= argv
[1];
716 event_names
= &argv
[2];
717 nr_events
= argc
- 2;
722 memset(&act
, 0, sizeof(act
));
723 ret
= sigemptyset(&act
.sa_mask
);
725 perror("sigemptyset");
729 act
.sa_handler
= SIG_IGN
;
730 ret
= sigaction(SIGPIPE
, &act
, NULL
);
737 act
.sa_handler
= handle_signals
;
738 ret
= sigaction(SIGTERM
, &act
, NULL
);
744 ret
= sigaction(SIGINT
, &act
, NULL
);
750 if (geteuid() == 0) {
751 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
752 if (ret
&& errno
!= EEXIST
) {
756 wait_shm_fd
= get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
757 sysconf(_SC_PAGE_SIZE
), 1);
758 if (wait_shm_fd
< 0) {
759 perror("global wait shm error");
762 strcpy(apps_sock_path
, DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
763 old_umask
= umask(0);
765 snprintf(local_apps_wait_shm_path
, PATH_MAX
,
766 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
767 wait_shm_fd
= get_wait_shm(local_apps_wait_shm_path
,
768 sysconf(_SC_PAGE_SIZE
), 0);
769 if (wait_shm_fd
< 0) {
770 perror("local wait shm error");
773 home_dir
= (const char *) getenv("HOME");
775 perror("getenv error");
778 snprintf(apps_sock_path
, PATH_MAX
,
779 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
782 ret
= ustcomm_create_unix_sock(apps_sock_path
);
784 perror("create error");
790 /* File permission MUST be 666 */
791 ret
= chmod(apps_sock_path
,
792 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
794 printf("Set file permissions failed: %s\n", apps_sock_path
);
800 ret
= ustcomm_listen_unix_sock(apps_socket
);
802 perror("listen error");
806 /* wake up futexes */
807 ret
= update_futex(wait_shm_fd
, 1);
809 fprintf(stderr
, "Error wakeup futex\n");
823 char name
[16]; /* Process name */
830 printf("Accepting application registration\n");
831 sock
= ustcomm_accept_unix_sock(apps_socket
);
833 perror("accept error");
838 * Basic recv here to handle the very simple data
839 * that the libust send to register (reg_msg).
841 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
842 if (len
< 0 || len
!= sizeof(reg_msg
)) {
843 perror("ustcomm_recv_unix_sock");
846 memcpy(bufname
, reg_msg
.name
, 16);
848 printf("Application %s pid %u ppid %u uid %u gid %u has registered (version : %u.%u)\n",
849 bufname
, reg_msg
.pid
, reg_msg
.ppid
, reg_msg
.uid
,
850 reg_msg
.gid
, reg_msg
.major
, reg_msg
.minor
);
851 ret
= send_app_msgs(sock
, outputpath
, nr_events
, event_names
);
853 printf("Error in send_app_msgs.\n");
860 printf("quitting.\n");
861 /* Let applications know we are not responding anymore */
862 ret
= update_futex(wait_shm_fd
, 0);
864 fprintf(stderr
, "Error wakeup futex\n");