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 <lttng/ust-error.h>
44 #include "../../libringbuffer/backend.h"
45 #include "../../libringbuffer/frontend.h"
46 #include "../../liblttng-ust/compat.h" /* For ENODATA */
48 #define MAX_NR_STREAMS 64
49 #define MAX_NR_EVENTS 128
51 static int session_handle
;
52 static struct lttng_ust_object_data metadata_stream_data
;
53 static struct lttng_ust_object_data metadata_data
;
54 static struct lttng_ust_object_data channel_data
;
55 static struct lttng_ust_object_data stream_data
[MAX_NR_STREAMS
];
56 static int event_handle
[MAX_NR_EVENTS
];
57 static int context_handle
;
59 static int apps_socket
= -1;
60 static char apps_sock_path
[PATH_MAX
];
61 static char local_apps_wait_shm_path
[PATH_MAX
];
63 static volatile int quit_program
;
65 static void handle_signals(int signo
)
71 int open_streams(int sock
, int channel_handle
, struct lttng_ust_object_data
*stream_datas
,
77 struct ustcomm_ust_msg lum
;
78 struct ustcomm_ust_reply lur
;
80 memset(&lum
, 0, sizeof(lum
));
81 lum
.handle
= channel_handle
;
82 lum
.cmd
= LTTNG_UST_STREAM
;
83 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
86 stream_datas
[k
].handle
= lur
.ret_val
;
87 printf("received stream handle %u\n",
88 stream_datas
[k
].handle
);
89 if (lur
.ret_code
== LTTNG_UST_OK
) {
92 stream_datas
[k
].memory_map_size
= lur
.u
.stream
.memory_map_size
;
94 len
= ustcomm_recv_fd(sock
);
97 stream_datas
[k
].shm_fd
= len
;
99 len
= ustcomm_recv_fd(sock
);
102 stream_datas
[k
].wait_fd
= len
;
106 if (ret
== -LTTNG_UST_ERR_NOENT
)
115 int close_streams(int sock
, struct lttng_ust_object_data
*stream_datas
, int nr_check
)
119 for (k
= 0; k
< nr_check
; k
++) {
120 struct ustcomm_ust_msg lum
;
121 struct ustcomm_ust_reply lur
;
123 if (!stream_datas
[k
].handle
)
125 memset(&lum
, 0, sizeof(lum
));
126 lum
.handle
= stream_datas
[k
].handle
;
127 lum
.cmd
= LTTNG_UST_RELEASE
;
128 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
130 printf("Error closing stream\n");
133 if (stream_datas
[k
].shm_fd
>= 0) {
134 ret
= close(stream_datas
[k
].shm_fd
);
136 printf("Error closing stream shm_fd\n");
140 if (stream_datas
[k
].wait_fd
>= 0) {
141 ret
= close(stream_datas
[k
].wait_fd
);
143 printf("Error closing stream wait_fd\n");
152 struct lttng_ust_shm_handle
*map_channel(struct lttng_ust_object_data
*chan_data
,
153 struct lttng_ust_object_data
*stream_datas
, int nr_check
)
155 struct lttng_ust_shm_handle
*handle
;
156 struct channel
*chan
;
159 /* map metadata channel */
160 handle
= channel_handle_create(chan_data
->shm_fd
,
162 chan_data
->memory_map_size
);
164 printf("create handle error\n");
167 chan_data
->shm_fd
= -1;
168 chan_data
->wait_fd
= -1;
169 chan
= shmp(handle
, handle
->chan
);
171 for (k
= 0; k
< nr_check
; k
++) {
172 struct lttng_ust_object_data
*stream_data
= &stream_datas
[k
];
174 if (!stream_data
->handle
)
177 ret
= channel_handle_add_stream(handle
,
179 stream_data
->wait_fd
,
180 stream_data
->memory_map_size
);
182 printf("add stream error\n");
185 stream_data
->shm_fd
= -1;
186 stream_data
->wait_fd
= -1;
191 channel_destroy(chan
, handle
, 1);
196 void unmap_channel(struct lttng_ust_shm_handle
*handle
)
198 struct channel
*chan
;
200 chan
= shmp(handle
, handle
->chan
);
202 channel_destroy(chan
, handle
, 1);
206 int consume_stream(struct lttng_ust_shm_handle
*handle
, int cpu
, char *outfile
)
208 struct channel
*chan
;
209 struct lttng_ust_lib_ring_buffer
*buf
;
211 int *shm_fd
, *wait_fd
;
212 uint64_t *memory_map_size
;
214 chan
= shmp(handle
, handle
->chan
);
217 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
218 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
221 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
227 outfd
= open(outfile
, O_WRONLY
| O_CREAT
| O_TRUNC
,
228 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
230 perror("open output");
234 printf("Waiting for buffer data for %s\n", outfile
);
236 unsigned long read_size
;
237 unsigned long copy_size
;
240 ret
= lib_ring_buffer_get_next_subbuf(buf
, handle
);
241 printf("get next ret %d\n", ret
);
244 if (ret
== -EAGAIN
) {
249 printf("Error %d in lib_ring_buffer_get_next_subbuf\n", ret
);
252 read_size
= lib_ring_buffer_get_read_data_size(
253 &chan
->backend
.config
, buf
, handle
);
254 read_size
= PAGE_ALIGN(read_size
);
255 ptr
= lib_ring_buffer_read_offset_address(
256 &buf
->backend
, 0, handle
);
257 printf("WRITE: copy %lu bytes\n", read_size
);
258 copy_size
= write(outfd
, ptr
, read_size
);
259 if (copy_size
< read_size
) {
260 printf("write issue: copied %lu, expected %lu\n", copy_size
, read_size
);
262 lib_ring_buffer_put_next_subbuf(buf
, handle
);
272 lib_ring_buffer_release_read(buf
, handle
, 1);
277 int consume_buffers(const char *outputpath
)
281 char pathname
[PATH_MAX
];
282 struct lttng_ust_shm_handle
*handle
;
284 snprintf(pathname
, PATH_MAX
- 1, "%s", outputpath
);
285 old_umask
= umask(0);
286 ret
= mkdir(pathname
, S_IRWXU
| S_IRWXG
);
287 if (ret
&& errno
!= EEXIST
) {
295 handle
= map_channel(&metadata_data
,
296 &metadata_stream_data
, 1);
299 snprintf(pathname
, PATH_MAX
- 1,
300 "%s/metadata", outputpath
);
301 ret
= consume_stream(handle
, -1, pathname
);
302 if (ret
&& ret
!= -ENOENT
) {
303 printf("Error in consume_stream\n");
306 unmap_channel(handle
);
308 /* copy binary data */
309 handle
= map_channel(&channel_data
,
310 stream_data
, MAX_NR_STREAMS
);
313 for (k
= 0; k
< MAX_NR_STREAMS
; k
++) {
314 snprintf(pathname
, PATH_MAX
- 1,
315 "%s/data_%u", outputpath
, k
);
316 ret
= consume_stream(handle
, k
, pathname
);
317 if (ret
&& ret
!= -ENOENT
) {
318 printf("Error in consume_stream\n");
322 unmap_channel(handle
);
328 int send_app_msgs(int sock
, const char *outputpath
,
329 unsigned int nr_events
, const char **event_names
)
331 struct ustcomm_ust_msg lum
;
332 struct ustcomm_ust_reply lur
;
336 memset(&lum
, 0, sizeof(lum
));
337 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
338 lum
.cmd
= LTTNG_UST_SESSION
;
339 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
342 session_handle
= lur
.ret_val
;
343 printf("received session handle %u\n", session_handle
);
345 /* Create metadata channel */
346 memset(&lum
, 0, sizeof(lum
));
347 lum
.handle
= session_handle
;
348 lum
.cmd
= LTTNG_UST_METADATA
;
349 lum
.u
.channel
.overwrite
= 0;
350 lum
.u
.channel
.subbuf_size
= 32768;
351 lum
.u
.channel
.num_subbuf
= 4;
352 lum
.u
.channel
.switch_timer_interval
= 0;
353 lum
.u
.channel
.read_timer_interval
= 0;
354 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
355 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
358 metadata_data
.handle
= lur
.ret_val
;
359 printf("received metadata handle %u\n", metadata_data
.handle
);
360 if (lur
.ret_code
== LTTNG_UST_OK
) {
363 metadata_data
.memory_map_size
= lur
.u
.channel
.memory_map_size
;
365 len
= ustcomm_recv_fd(sock
);
368 metadata_data
.shm_fd
= len
;
370 len
= ustcomm_recv_fd(sock
);
373 metadata_data
.wait_fd
= len
;
376 ret
= open_streams(sock
, metadata_data
.handle
,
377 &metadata_stream_data
, 1);
379 printf("Error in open_streams\n");
383 /* Create data channel */
384 memset(&lum
, 0, sizeof(lum
));
385 lum
.handle
= session_handle
;
386 lum
.cmd
= LTTNG_UST_CHANNEL
;
387 //lum.u.channel.overwrite = 0;
388 lum
.u
.channel
.overwrite
= 1;
389 lum
.u
.channel
.subbuf_size
= 32768;
390 lum
.u
.channel
.num_subbuf
= 8;
391 //lum.u.channel.num_subbuf = 4;
392 //lum.u.channel.num_subbuf = 2;
393 lum
.u
.channel
.switch_timer_interval
= 0;
394 lum
.u
.channel
.read_timer_interval
= 0;
395 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
396 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
399 channel_data
.handle
= lur
.ret_val
;
400 printf("received channel handle %u\n", channel_data
.handle
);
401 if (lur
.ret_code
== LTTNG_UST_OK
) {
404 channel_data
.memory_map_size
= lur
.u
.channel
.memory_map_size
;
406 len
= ustcomm_recv_fd(sock
);
409 channel_data
.shm_fd
= len
;
411 len
= ustcomm_recv_fd(sock
);
414 channel_data
.wait_fd
= len
;
418 for (k
= 0; k
< nr_events
; k
++) {
419 memset(&lum
, 0, sizeof(lum
));
420 lum
.handle
= channel_data
.handle
;
421 lum
.cmd
= LTTNG_UST_EVENT
;
422 strncpy(lum
.u
.event
.name
, event_names
[k
],
423 LTTNG_UST_SYM_NAME_LEN
);
424 lum
.u
.event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
425 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
428 event_handle
[k
] = lur
.ret_val
;
429 printf("received event handle %u\n", event_handle
[k
]);
432 /* Attach vtid context */
433 memset(&lum
, 0, sizeof(lum
));
434 lum
.handle
= channel_data
.handle
;
435 lum
.cmd
= LTTNG_UST_CONTEXT
;
436 lum
.u
.context
.ctx
= LTTNG_UST_CONTEXT_VTID
;
437 //lum.u.context.ctx = LTTNG_UST_CONTEXT_PTHREAD_ID;
438 //lum.u.context.ctx = LTTNG_UST_CONTEXT_VPID;
439 //lum.u.context.ctx = LTTNG_UST_CONTEXT_PROCNAME;
440 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
443 context_handle
= lur
.ret_val
;
444 printf("received context handle %u\n", context_handle
);
446 /* Get references to channel streams */
447 ret
= open_streams(sock
, channel_data
.handle
,
448 stream_data
, MAX_NR_STREAMS
);
450 printf("Error in open_streams\n");
454 memset(&lum
, 0, sizeof(lum
));
455 lum
.handle
= session_handle
;
456 lum
.cmd
= LTTNG_UST_SESSION_START
;
457 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
460 printf("Session handle %u started.\n", session_handle
);
462 /* Tell application registration is done */
463 memset(&lum
, 0, sizeof(lum
));
464 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
465 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
466 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
469 printf("Registration done acknowledged.\n");
473 ret
= consume_buffers(outputpath
);
475 printf("Error in consume_buffers\n");
479 /* Release data channel */
480 /* Release streams */
481 ret
= close_streams(sock
, stream_data
,
486 /* Release context */
487 memset(&lum
, 0, sizeof(lum
));
488 lum
.handle
= context_handle
;
489 lum
.cmd
= LTTNG_UST_RELEASE
;
490 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
495 for (k
= 0; k
< nr_events
; k
++) {
496 memset(&lum
, 0, sizeof(lum
));
497 lum
.handle
= event_handle
[k
];
498 lum
.cmd
= LTTNG_UST_RELEASE
;
499 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
503 memset(&lum
, 0, sizeof(lum
));
504 lum
.handle
= channel_data
.handle
;
505 lum
.cmd
= LTTNG_UST_RELEASE
;
506 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
509 if (channel_data
.shm_fd
>= 0) {
510 ret
= close(channel_data
.shm_fd
);
514 if (channel_data
.wait_fd
>= 0) {
515 ret
= close(channel_data
.wait_fd
);
520 /* Release metadata channel */
521 ret
= close_streams(sock
, &metadata_stream_data
, 1);
525 memset(&lum
, 0, sizeof(lum
));
526 lum
.handle
= metadata_data
.handle
;
527 lum
.cmd
= LTTNG_UST_RELEASE
;
528 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
531 if (metadata_data
.shm_fd
>= 0) {
532 ret
= close(metadata_data
.shm_fd
);
536 if (metadata_data
.wait_fd
>= 0) {
537 ret
= close(metadata_data
.wait_fd
);
542 /* Release session */
543 memset(&lum
, 0, sizeof(lum
));
544 lum
.handle
= session_handle
;
545 lum
.cmd
= LTTNG_UST_RELEASE
;
546 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
554 * Using fork to set umask in the child process (not multi-thread safe). We
555 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
556 * the shm and does not let everybody modify it, to ensure safety against
557 * shm_unlink) by simply letting the mmap fail and retrying after a few
558 * seconds. For global shm, everybody has rw access to it until the sessiond
561 static int get_wait_shm(char *shm_path
, size_t mmap_size
, int global
)
563 int wait_shm_fd
, ret
;
566 /* Default permissions */
567 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
569 /* Change owner of the shm path */
571 ret
= chown(shm_path
, 0, 0);
573 if (errno
!= ENOENT
) {
574 perror("chown wait shm");
580 * If global session daemon, any application can register so the shm
581 * needs to be set in read-only mode for others.
585 ret
= chown(shm_path
, getuid(), getgid());
587 if (errno
!= ENOENT
) {
588 perror("chown wait shm");
595 * Set permissions to the shm even if we did not create the shm.
597 ret
= chmod(shm_path
, mode
);
599 if (errno
!= ENOENT
) {
600 perror("chmod wait shm");
606 * We're alone in a child process, so we can modify the process-wide
612 * Try creating shm (or get rw access). We don't do an exclusive open,
613 * because we allow other processes to create+ftruncate it concurrently.
615 wait_shm_fd
= shm_open(shm_path
, O_RDWR
| O_CREAT
, mode
);
616 if (wait_shm_fd
< 0) {
617 perror("shm_open wait shm");
621 ret
= ftruncate(wait_shm_fd
, mmap_size
);
623 perror("ftruncate wait shm");
627 ret
= fchmod(wait_shm_fd
, mode
);
633 printf("Got the wait shm fd %d\n", wait_shm_fd
);
638 printf("Failing to get the wait shm fd\n");
643 int update_futex(int fd
, int active
)
649 page_size
= sysconf(_SC_PAGE_SIZE
);
653 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
| PROT_WRITE
,
655 if (wait_shm_mmap
== MAP_FAILED
) {
661 uatomic_set((int32_t *) wait_shm_mmap
, 1);
662 futex_async((int32_t *) wait_shm_mmap
, FUTEX_WAKE
,
663 INT_MAX
, NULL
, NULL
, 0);
665 uatomic_set((int32_t *) wait_shm_mmap
, 0);
667 ret
= munmap(wait_shm_mmap
, page_size
);
669 perror("Error unmapping wait shm");
678 * Set open files limit to unlimited. This daemon can open a large number of
679 * file descriptors in order to consumer multiple kernel traces.
681 static void set_ulimit(void)
687 * If not root, we cannot increase our max open files. But our
688 * scope is then limited to processes from a single user.
692 /* The kernel does not allowed an infinite limit for open files */
693 lim
.rlim_cur
= 65535;
694 lim
.rlim_max
= 65535;
696 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
698 perror("failed to set open files limit");
704 * ./ust-basic-tracing outputpath event_name1 event_name2 ....
706 int main(int argc
, const char **argv
)
708 const char *home_dir
;
709 char home_rundir
[PATH_MAX
];
711 int ret
, wait_shm_fd
;
712 struct sigaction act
;
713 mode_t old_umask
= 0;
714 const char *outputpath
;
715 const char **event_names
;
716 unsigned int nr_events
;
720 printf("%s outputpath event_name1 event_name2 ...\n",
724 outputpath
= argv
[1];
725 event_names
= &argv
[2];
726 nr_events
= argc
- 2;
731 memset(&act
, 0, sizeof(act
));
732 ret
= sigemptyset(&act
.sa_mask
);
734 perror("sigemptyset");
738 act
.sa_handler
= SIG_IGN
;
739 ret
= sigaction(SIGPIPE
, &act
, NULL
);
746 act
.sa_handler
= handle_signals
;
747 ret
= sigaction(SIGTERM
, &act
, NULL
);
753 ret
= sigaction(SIGINT
, &act
, NULL
);
759 if (geteuid() == 0) {
760 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
761 if (ret
&& errno
!= EEXIST
) {
765 wait_shm_fd
= get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
766 sysconf(_SC_PAGE_SIZE
), 1);
767 if (wait_shm_fd
< 0) {
768 perror("global wait shm error");
771 strcpy(apps_sock_path
, DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
772 old_umask
= umask(0);
774 home_dir
= (const char *) getenv("HOME");
776 perror("getenv error");
780 snprintf(home_rundir
, PATH_MAX
,
781 LTTNG_HOME_RUNDIR
, home_dir
);
783 ret
= mkdir(home_rundir
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
784 if (ret
&& errno
!= EEXIST
) {
789 snprintf(local_apps_wait_shm_path
, PATH_MAX
,
790 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
791 wait_shm_fd
= get_wait_shm(local_apps_wait_shm_path
,
792 sysconf(_SC_PAGE_SIZE
), 0);
793 if (wait_shm_fd
< 0) {
794 perror("local wait shm error");
797 snprintf(apps_sock_path
, PATH_MAX
,
798 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
801 ret
= ustcomm_create_unix_sock(apps_sock_path
);
803 perror("create error");
809 /* File permission MUST be 666 */
810 ret
= chmod(apps_sock_path
,
811 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
813 printf("Set file permissions failed: %s\n", apps_sock_path
);
819 ret
= ustcomm_listen_unix_sock(apps_socket
);
821 perror("listen error");
825 /* wake up futexes */
826 ret
= update_futex(wait_shm_fd
, 1);
828 fprintf(stderr
, "Error wakeup futex\n");
842 uint32_t bits_per_long
;
843 char name
[16]; /* Process name */
850 printf("Accepting application registration\n");
851 sock
= ustcomm_accept_unix_sock(apps_socket
);
853 perror("accept error");
858 * Basic recv here to handle the very simple data
859 * that the libust send to register (reg_msg).
861 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
862 if (len
< 0 || len
!= sizeof(reg_msg
)) {
863 perror("ustcomm_recv_unix_sock");
866 memcpy(bufname
, reg_msg
.name
, 16);
868 printf("Application %s pid %u ppid %u uid %u gid %u has registered (version : %u.%u)\n",
869 bufname
, reg_msg
.pid
, reg_msg
.ppid
, reg_msg
.uid
,
870 reg_msg
.gid
, reg_msg
.major
, reg_msg
.minor
);
871 ret
= send_app_msgs(sock
, outputpath
, nr_events
, event_names
);
873 printf("Error in send_app_msgs.\n");
880 printf("quitting.\n");
881 /* Let applications know we are not responding anymore */
882 ret
= update_futex(wait_shm_fd
, 0);
884 fprintf(stderr
, "Error wakeup futex\n");
889 printf("Removing %s directory\n", home_rundir
);
890 ret
= asprintf(&cmd
, "rm -rf %s", home_rundir
);
892 printf("asprintf failed. Something is really wrong!\n");
896 /* Remove lttng run directory */
899 printf("Unable to clean %s\n", home_rundir
);