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"
46 #define MAX_NR_STREAMS 64
47 #define MAX_NR_EVENTS 128
49 static int session_handle
;
50 static struct lttng_ust_object_data metadata_stream_data
;
51 static struct lttng_ust_object_data metadata_data
;
52 static struct lttng_ust_object_data channel_data
;
53 static struct lttng_ust_object_data stream_data
[MAX_NR_STREAMS
];
54 static int event_handle
[MAX_NR_EVENTS
];
55 static int context_handle
;
57 static int apps_socket
= -1;
58 static char apps_sock_path
[PATH_MAX
];
59 static char local_apps_wait_shm_path
[PATH_MAX
];
61 static volatile int quit_program
;
63 static void handle_signals(int signo
)
69 int open_streams(int sock
, int channel_handle
, struct lttng_ust_object_data
*stream_datas
,
75 struct ustcomm_ust_msg lum
;
76 struct ustcomm_ust_reply lur
;
78 memset(&lum
, 0, sizeof(lum
));
79 lum
.handle
= channel_handle
;
80 lum
.cmd
= LTTNG_UST_STREAM
;
81 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
84 stream_datas
[k
].handle
= lur
.ret_val
;
85 printf("received stream handle %u\n",
86 stream_datas
[k
].handle
);
87 if (lur
.ret_code
== USTCOMM_OK
) {
90 stream_datas
[k
].memory_map_size
= lur
.u
.stream
.memory_map_size
;
92 len
= ustcomm_recv_fd(sock
);
95 stream_datas
[k
].shm_fd
= len
;
97 len
= ustcomm_recv_fd(sock
);
100 stream_datas
[k
].wait_fd
= len
;
113 int close_streams(int sock
, struct lttng_ust_object_data
*stream_datas
, int nr_check
)
117 for (k
= 0; k
< nr_check
; k
++) {
118 struct ustcomm_ust_msg lum
;
119 struct ustcomm_ust_reply lur
;
121 if (!stream_datas
[k
].handle
)
123 memset(&lum
, 0, sizeof(lum
));
124 lum
.handle
= stream_datas
[k
].handle
;
125 lum
.cmd
= LTTNG_UST_RELEASE
;
126 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
128 printf("Error closing stream\n");
131 if (stream_datas
[k
].shm_fd
>= 0) {
132 ret
= close(stream_datas
[k
].shm_fd
);
134 printf("Error closing stream shm_fd\n");
138 if (stream_datas
[k
].wait_fd
>= 0) {
139 ret
= close(stream_datas
[k
].wait_fd
);
141 printf("Error closing stream wait_fd\n");
150 struct lttng_ust_shm_handle
*map_channel(struct lttng_ust_object_data
*chan_data
,
151 struct lttng_ust_object_data
*stream_datas
, int nr_check
)
153 struct lttng_ust_shm_handle
*handle
;
154 struct channel
*chan
;
157 /* map metadata channel */
158 handle
= channel_handle_create(chan_data
->shm_fd
,
160 chan_data
->memory_map_size
);
162 printf("create handle error\n");
165 chan_data
->shm_fd
= -1;
166 chan_data
->wait_fd
= -1;
167 chan
= shmp(handle
, handle
->chan
);
169 for (k
= 0; k
< nr_check
; k
++) {
170 struct lttng_ust_object_data
*stream_data
= &stream_datas
[k
];
172 if (!stream_data
->handle
)
175 ret
= channel_handle_add_stream(handle
,
177 stream_data
->wait_fd
,
178 stream_data
->memory_map_size
);
180 printf("add stream error\n");
183 stream_data
->shm_fd
= -1;
184 stream_data
->wait_fd
= -1;
189 channel_destroy(chan
, handle
, 1);
194 void unmap_channel(struct lttng_ust_shm_handle
*handle
)
196 struct channel
*chan
;
198 chan
= shmp(handle
, handle
->chan
);
200 channel_destroy(chan
, handle
, 1);
204 int consume_stream(struct lttng_ust_shm_handle
*handle
, int cpu
, char *outfile
)
206 struct channel
*chan
;
207 struct lttng_ust_lib_ring_buffer
*buf
;
209 int *shm_fd
, *wait_fd
;
210 uint64_t *memory_map_size
;
212 chan
= shmp(handle
, handle
->chan
);
215 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
216 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
219 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
225 outfd
= open(outfile
, O_WRONLY
| O_CREAT
| O_LARGEFILE
| O_TRUNC
,
226 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
228 perror("open output");
232 printf("Waiting for buffer data for %s\n", outfile
);
234 unsigned long read_size
;
235 unsigned long copy_size
;
238 ret
= lib_ring_buffer_get_next_subbuf(buf
, handle
);
239 printf("get next ret %d\n", ret
);
242 if (ret
== -EAGAIN
) {
247 printf("Error %d in lib_ring_buffer_get_next_subbuf\n", ret
);
250 read_size
= lib_ring_buffer_get_read_data_size(
251 &chan
->backend
.config
, buf
, handle
);
252 read_size
= PAGE_ALIGN(read_size
);
253 ptr
= lib_ring_buffer_read_offset_address(
254 &buf
->backend
, 0, handle
);
255 printf("WRITE: copy %lu bytes\n", read_size
);
256 copy_size
= write(outfd
, ptr
, read_size
);
257 if (copy_size
< read_size
) {
258 printf("write issue: copied %lu, expected %lu\n", copy_size
, read_size
);
260 lib_ring_buffer_put_next_subbuf(buf
, handle
);
270 lib_ring_buffer_release_read(buf
, handle
, 1);
275 int consume_buffers(const char *outputpath
)
279 char pathname
[PATH_MAX
];
280 struct lttng_ust_shm_handle
*handle
;
282 snprintf(pathname
, PATH_MAX
- 1, "%s", outputpath
);
283 old_umask
= umask(0);
284 ret
= mkdir(pathname
, S_IRWXU
| S_IRWXG
);
285 if (ret
&& errno
!= EEXIST
) {
293 handle
= map_channel(&metadata_data
,
294 &metadata_stream_data
, 1);
297 snprintf(pathname
, PATH_MAX
- 1,
298 "%s/metadata", outputpath
);
299 ret
= consume_stream(handle
, -1, pathname
);
300 if (ret
&& ret
!= -ENOENT
) {
301 printf("Error in consume_stream\n");
304 unmap_channel(handle
);
306 /* copy binary data */
307 handle
= map_channel(&channel_data
,
308 stream_data
, MAX_NR_STREAMS
);
311 for (k
= 0; k
< MAX_NR_STREAMS
; k
++) {
312 snprintf(pathname
, PATH_MAX
- 1,
313 "%s/data_%u", outputpath
, k
);
314 ret
= consume_stream(handle
, k
, pathname
);
315 if (ret
&& ret
!= -ENOENT
) {
316 printf("Error in consume_stream\n");
320 unmap_channel(handle
);
326 int send_app_msgs(int sock
, const char *outputpath
,
327 unsigned int nr_events
, const char **event_names
)
329 struct ustcomm_ust_msg lum
;
330 struct ustcomm_ust_reply lur
;
334 memset(&lum
, 0, sizeof(lum
));
335 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
336 lum
.cmd
= LTTNG_UST_SESSION
;
337 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
340 session_handle
= lur
.ret_val
;
341 printf("received session handle %u\n", session_handle
);
343 /* Create metadata channel */
344 memset(&lum
, 0, sizeof(lum
));
345 lum
.handle
= session_handle
;
346 lum
.cmd
= LTTNG_UST_METADATA
;
347 lum
.u
.channel
.overwrite
= 0;
348 lum
.u
.channel
.subbuf_size
= 32768;
349 lum
.u
.channel
.num_subbuf
= 4;
350 lum
.u
.channel
.switch_timer_interval
= 0;
351 lum
.u
.channel
.read_timer_interval
= 0;
352 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
353 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
356 metadata_data
.handle
= lur
.ret_val
;
357 printf("received metadata handle %u\n", metadata_data
.handle
);
358 if (lur
.ret_code
== USTCOMM_OK
) {
361 metadata_data
.memory_map_size
= lur
.u
.channel
.memory_map_size
;
363 len
= ustcomm_recv_fd(sock
);
366 metadata_data
.shm_fd
= len
;
368 len
= ustcomm_recv_fd(sock
);
371 metadata_data
.wait_fd
= len
;
374 ret
= open_streams(sock
, metadata_data
.handle
,
375 &metadata_stream_data
, 1);
377 printf("Error in open_streams\n");
381 /* Create data channel */
382 memset(&lum
, 0, sizeof(lum
));
383 lum
.handle
= session_handle
;
384 lum
.cmd
= LTTNG_UST_CHANNEL
;
385 //lum.u.channel.overwrite = 0;
386 lum
.u
.channel
.overwrite
= 1;
387 lum
.u
.channel
.subbuf_size
= 32768;
388 lum
.u
.channel
.num_subbuf
= 8;
389 //lum.u.channel.num_subbuf = 4;
390 //lum.u.channel.num_subbuf = 2;
391 lum
.u
.channel
.switch_timer_interval
= 0;
392 lum
.u
.channel
.read_timer_interval
= 0;
393 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
394 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
397 channel_data
.handle
= lur
.ret_val
;
398 printf("received channel handle %u\n", channel_data
.handle
);
399 if (lur
.ret_code
== USTCOMM_OK
) {
402 channel_data
.memory_map_size
= lur
.u
.channel
.memory_map_size
;
404 len
= ustcomm_recv_fd(sock
);
407 channel_data
.shm_fd
= len
;
409 len
= ustcomm_recv_fd(sock
);
412 channel_data
.wait_fd
= len
;
416 for (k
= 0; k
< nr_events
; k
++) {
417 memset(&lum
, 0, sizeof(lum
));
418 lum
.handle
= channel_data
.handle
;
419 lum
.cmd
= LTTNG_UST_EVENT
;
420 strncpy(lum
.u
.event
.name
, event_names
[k
],
421 LTTNG_UST_SYM_NAME_LEN
);
422 lum
.u
.event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
423 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
426 event_handle
[k
] = lur
.ret_val
;
427 printf("received event handle %u\n", event_handle
[k
]);
430 /* Attach vtid context */
431 memset(&lum
, 0, sizeof(lum
));
432 lum
.handle
= channel_data
.handle
;
433 lum
.cmd
= LTTNG_UST_CONTEXT
;
434 lum
.u
.context
.ctx
= LTTNG_UST_CONTEXT_VTID
;
435 //lum.u.context.ctx = LTTNG_UST_CONTEXT_PTHREAD_ID;
436 //lum.u.context.ctx = LTTNG_UST_CONTEXT_VPID;
437 //lum.u.context.ctx = LTTNG_UST_CONTEXT_PROCNAME;
438 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
441 context_handle
= lur
.ret_val
;
442 printf("received context handle %u\n", context_handle
);
444 /* Get references to channel streams */
445 ret
= open_streams(sock
, channel_data
.handle
,
446 stream_data
, MAX_NR_STREAMS
);
448 printf("Error in open_streams\n");
452 memset(&lum
, 0, sizeof(lum
));
453 lum
.handle
= session_handle
;
454 lum
.cmd
= LTTNG_UST_SESSION_START
;
455 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
458 printf("Session handle %u started.\n", session_handle
);
460 /* Tell application registration is done */
461 memset(&lum
, 0, sizeof(lum
));
462 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
463 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
464 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
467 printf("Registration done acknowledged.\n");
471 ret
= consume_buffers(outputpath
);
473 printf("Error in consume_buffers\n");
477 /* Release data channel */
478 /* Release streams */
479 ret
= close_streams(sock
, stream_data
,
484 /* Release context */
485 memset(&lum
, 0, sizeof(lum
));
486 lum
.handle
= context_handle
;
487 lum
.cmd
= LTTNG_UST_RELEASE
;
488 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
493 for (k
= 0; k
< nr_events
; k
++) {
494 memset(&lum
, 0, sizeof(lum
));
495 lum
.handle
= event_handle
[k
];
496 lum
.cmd
= LTTNG_UST_RELEASE
;
497 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
501 memset(&lum
, 0, sizeof(lum
));
502 lum
.handle
= channel_data
.handle
;
503 lum
.cmd
= LTTNG_UST_RELEASE
;
504 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
507 if (channel_data
.shm_fd
>= 0) {
508 ret
= close(channel_data
.shm_fd
);
512 if (channel_data
.wait_fd
>= 0) {
513 ret
= close(channel_data
.wait_fd
);
518 /* Release metadata channel */
519 ret
= close_streams(sock
, &metadata_stream_data
, 1);
523 memset(&lum
, 0, sizeof(lum
));
524 lum
.handle
= metadata_data
.handle
;
525 lum
.cmd
= LTTNG_UST_RELEASE
;
526 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
529 if (metadata_data
.shm_fd
>= 0) {
530 ret
= close(metadata_data
.shm_fd
);
534 if (metadata_data
.wait_fd
>= 0) {
535 ret
= close(metadata_data
.wait_fd
);
540 /* Release session */
541 memset(&lum
, 0, sizeof(lum
));
542 lum
.handle
= session_handle
;
543 lum
.cmd
= LTTNG_UST_RELEASE
;
544 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
552 * Using fork to set umask in the child process (not multi-thread safe). We
553 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
554 * the shm and does not let everybody modify it, to ensure safety against
555 * shm_unlink) by simply letting the mmap fail and retrying after a few
556 * seconds. For global shm, everybody has rw access to it until the sessiond
559 static int get_wait_shm(char *shm_path
, size_t mmap_size
, int global
)
561 int wait_shm_fd
, ret
;
564 /* Default permissions */
565 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
567 /* Change owner of the shm path */
569 ret
= chown(shm_path
, 0, 0);
571 if (errno
!= ENOENT
) {
572 perror("chown wait shm");
578 * If global session daemon, any application can register so the shm
579 * needs to be set in read-only mode for others.
583 ret
= chown(shm_path
, getuid(), getgid());
585 if (errno
!= ENOENT
) {
586 perror("chown wait shm");
593 * Set permissions to the shm even if we did not create the shm.
595 ret
= chmod(shm_path
, mode
);
597 if (errno
!= ENOENT
) {
598 perror("chmod wait shm");
604 * We're alone in a child process, so we can modify the process-wide
610 * Try creating shm (or get rw access). We don't do an exclusive open,
611 * because we allow other processes to create+ftruncate it concurrently.
613 wait_shm_fd
= shm_open(shm_path
, O_RDWR
| O_CREAT
, mode
);
614 if (wait_shm_fd
< 0) {
615 perror("shm_open wait shm");
619 ret
= ftruncate(wait_shm_fd
, mmap_size
);
621 perror("ftruncate wait shm");
625 ret
= fchmod(wait_shm_fd
, mode
);
631 printf("Got the wait shm fd %d\n", wait_shm_fd
);
636 printf("Failing to get the wait shm fd\n");
641 int update_futex(int fd
, int active
)
643 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
647 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
,
649 if (wait_shm_mmap
== MAP_FAILED
) {
655 uatomic_set((int32_t *) wait_shm_mmap
, 1);
656 futex_async((int32_t *) wait_shm_mmap
, FUTEX_WAKE
,
657 INT_MAX
, NULL
, NULL
, 0);
659 uatomic_set((int32_t *) wait_shm_mmap
, 0);
661 ret
= munmap(wait_shm_mmap
, mmap_size
);
663 perror("Error unmapping wait shm");
672 * Set open files limit to unlimited. This daemon can open a large number of
673 * file descriptors in order to consumer multiple kernel traces.
675 static void set_ulimit(void)
681 * If not root, we cannot increase our max open files. But our
682 * scope is then limited to processes from a single user.
686 /* The kernel does not allowed an infinite limit for open files */
687 lim
.rlim_cur
= 65535;
688 lim
.rlim_max
= 65535;
690 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
692 perror("failed to set open files limit");
698 * ./ust-basic-tracing outputpath event_name1 event_name2 ....
700 int main(int argc
, const char **argv
)
702 const char *home_dir
;
703 int ret
, wait_shm_fd
;
704 struct sigaction act
;
705 mode_t old_umask
= 0;
706 const char *outputpath
;
707 const char **event_names
;
708 unsigned int nr_events
;
712 printf("%s outputpath event_name1 event_name2 ...\n",
716 outputpath
= argv
[1];
717 event_names
= &argv
[2];
718 nr_events
= argc
- 2;
723 memset(&act
, 0, sizeof(act
));
724 ret
= sigemptyset(&act
.sa_mask
);
726 perror("sigemptyset");
730 act
.sa_handler
= SIG_IGN
;
731 ret
= sigaction(SIGPIPE
, &act
, NULL
);
738 act
.sa_handler
= handle_signals
;
739 ret
= sigaction(SIGTERM
, &act
, NULL
);
745 ret
= sigaction(SIGINT
, &act
, NULL
);
751 if (geteuid() == 0) {
752 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
753 if (ret
&& errno
!= EEXIST
) {
757 wait_shm_fd
= get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
758 sysconf(_SC_PAGE_SIZE
), 1);
759 if (wait_shm_fd
< 0) {
760 perror("global wait shm error");
763 strcpy(apps_sock_path
, DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
764 old_umask
= umask(0);
766 snprintf(local_apps_wait_shm_path
, PATH_MAX
,
767 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
768 wait_shm_fd
= get_wait_shm(local_apps_wait_shm_path
,
769 sysconf(_SC_PAGE_SIZE
), 0);
770 if (wait_shm_fd
< 0) {
771 perror("local wait shm error");
774 home_dir
= (const char *) getenv("HOME");
776 perror("getenv error");
779 snprintf(apps_sock_path
, PATH_MAX
,
780 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
783 ret
= ustcomm_create_unix_sock(apps_sock_path
);
785 perror("create error");
791 /* File permission MUST be 666 */
792 ret
= chmod(apps_sock_path
,
793 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
795 printf("Set file permissions failed: %s\n", apps_sock_path
);
801 ret
= ustcomm_listen_unix_sock(apps_socket
);
803 perror("listen error");
807 /* wake up futexes */
808 ret
= update_futex(wait_shm_fd
, 1);
810 fprintf(stderr
, "Error wakeup futex\n");
824 char name
[16]; /* Process name */
831 printf("Accepting application registration\n");
832 sock
= ustcomm_accept_unix_sock(apps_socket
);
834 perror("accept error");
839 * Basic recv here to handle the very simple data
840 * that the libust send to register (reg_msg).
842 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
843 if (len
< 0 || len
!= sizeof(reg_msg
)) {
844 perror("ustcomm_recv_unix_sock");
847 memcpy(bufname
, reg_msg
.name
, 16);
849 printf("Application %s pid %u ppid %u uid %u gid %u has registered (version : %u.%u)\n",
850 bufname
, reg_msg
.pid
, reg_msg
.ppid
, reg_msg
.uid
,
851 reg_msg
.gid
, reg_msg
.major
, reg_msg
.minor
);
852 ret
= send_app_msgs(sock
, outputpath
, nr_events
, event_names
);
854 printf("Error in send_app_msgs.\n");
861 printf("quitting.\n");
862 /* Let applications know we are not responding anymore */
863 ret
= update_futex(wait_shm_fd
, 0);
865 fprintf(stderr
, "Error wakeup futex\n");