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>
40 #include "../../libringbuffer/backend.h"
41 #include "../../libringbuffer/frontend.h"
43 #define MAX_NR_STREAMS 64
44 #define MAX_NR_EVENTS 128
46 static int session_handle
;
47 static struct lttng_ust_object_data metadata_stream_data
;
48 static struct lttng_ust_object_data metadata_data
;
49 static struct lttng_ust_object_data channel_data
;
50 static struct lttng_ust_object_data stream_data
[MAX_NR_STREAMS
];
51 static int event_handle
[MAX_NR_EVENTS
];
52 static int context_handle
;
54 static int apps_socket
= -1;
55 static char apps_sock_path
[PATH_MAX
];
56 static char local_apps_wait_shm_path
[PATH_MAX
];
58 static volatile int quit_program
;
60 static void handle_signals(int signo
)
66 int open_streams(int sock
, int channel_handle
, struct lttng_ust_object_data
*stream_datas
,
72 struct ustcomm_ust_msg lum
;
73 struct ustcomm_ust_reply lur
;
75 memset(&lum
, 0, sizeof(lum
));
76 lum
.handle
= channel_handle
;
77 lum
.cmd
= LTTNG_UST_STREAM
;
78 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
81 stream_datas
[k
].handle
= lur
.ret_val
;
82 printf("received stream handle %u\n",
83 stream_datas
[k
].handle
);
84 if (lur
.ret_code
== USTCOMM_OK
) {
87 stream_datas
[k
].memory_map_size
= lur
.u
.stream
.memory_map_size
;
89 len
= ustcomm_recv_fd(sock
);
92 stream_datas
[k
].shm_fd
= len
;
94 len
= ustcomm_recv_fd(sock
);
97 stream_datas
[k
].wait_fd
= len
;
110 int close_streams(int sock
, struct lttng_ust_object_data
*stream_datas
, int nr_check
)
114 for (k
= 0; k
< nr_check
; k
++) {
115 struct ustcomm_ust_msg lum
;
116 struct ustcomm_ust_reply lur
;
118 if (!stream_datas
[k
].handle
)
120 memset(&lum
, 0, sizeof(lum
));
121 lum
.handle
= stream_datas
[k
].handle
;
122 lum
.cmd
= LTTNG_UST_RELEASE
;
123 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
125 printf("Error closing stream\n");
128 if (stream_datas
[k
].shm_fd
>= 0) {
129 ret
= close(stream_datas
[k
].shm_fd
);
131 printf("Error closing stream shm_fd\n");
135 if (stream_datas
[k
].wait_fd
>= 0) {
136 ret
= close(stream_datas
[k
].wait_fd
);
138 printf("Error closing stream wait_fd\n");
147 struct lttng_ust_shm_handle
*map_channel(struct lttng_ust_object_data
*chan_data
,
148 struct lttng_ust_object_data
*stream_datas
, int nr_check
)
150 struct lttng_ust_shm_handle
*handle
;
151 struct channel
*chan
;
154 /* map metadata channel */
155 handle
= channel_handle_create(chan_data
->shm_fd
,
157 chan_data
->memory_map_size
);
159 printf("create handle error\n");
162 chan_data
->shm_fd
= -1;
163 chan_data
->wait_fd
= -1;
164 chan
= shmp(handle
, handle
->chan
);
166 for (k
= 0; k
< nr_check
; k
++) {
167 struct lttng_ust_object_data
*stream_data
= &stream_datas
[k
];
169 if (!stream_data
->handle
)
172 ret
= channel_handle_add_stream(handle
,
174 stream_data
->wait_fd
,
175 stream_data
->memory_map_size
);
177 printf("add stream error\n");
180 stream_data
->shm_fd
= -1;
181 stream_data
->wait_fd
= -1;
186 channel_destroy(chan
, handle
, 1);
191 void unmap_channel(struct lttng_ust_shm_handle
*handle
)
193 struct channel
*chan
;
195 chan
= shmp(handle
, handle
->chan
);
197 channel_destroy(chan
, handle
, 1);
201 int consume_stream(struct lttng_ust_shm_handle
*handle
, int cpu
, char *outfile
)
203 struct channel
*chan
;
204 struct lttng_ust_lib_ring_buffer
*buf
;
206 int *shm_fd
, *wait_fd
;
207 uint64_t *memory_map_size
;
209 chan
= shmp(handle
, handle
->chan
);
212 buf
= channel_get_ring_buffer(&chan
->backend
.config
,
213 chan
, cpu
, handle
, &shm_fd
, &wait_fd
, &memory_map_size
);
216 ret
= lib_ring_buffer_open_read(buf
, handle
, 1);
222 outfd
= open(outfile
, O_WRONLY
| O_CREAT
| O_LARGEFILE
| O_TRUNC
,
223 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
);
225 perror("open output");
229 printf("Waiting for buffer data for %s\n", outfile
);
231 unsigned long read_size
;
232 unsigned long copy_size
;
235 ret
= lib_ring_buffer_get_next_subbuf(buf
, handle
);
236 printf("get next ret %d\n", ret
);
239 if (ret
== -EAGAIN
) {
244 printf("Error %d in lib_ring_buffer_get_next_subbuf\n", ret
);
247 read_size
= lib_ring_buffer_get_read_data_size(
248 &chan
->backend
.config
, buf
, handle
);
249 read_size
= PAGE_ALIGN(read_size
);
250 ptr
= lib_ring_buffer_read_offset_address(
251 &buf
->backend
, 0, handle
);
252 printf("WRITE: copy %lu bytes\n", read_size
);
253 copy_size
= write(outfd
, ptr
, read_size
);
254 if (copy_size
< read_size
) {
255 printf("write issue: copied %lu, expected %lu\n", copy_size
, read_size
);
257 lib_ring_buffer_put_next_subbuf(buf
, handle
);
267 lib_ring_buffer_release_read(buf
, handle
, 1);
272 int consume_buffers(const char *outputpath
)
276 char pathname
[PATH_MAX
];
277 struct lttng_ust_shm_handle
*handle
;
279 snprintf(pathname
, PATH_MAX
- 1, "%s", outputpath
);
280 old_umask
= umask(0);
281 ret
= mkdir(pathname
, S_IRWXU
| S_IRWXG
);
282 if (ret
&& errno
!= EEXIST
) {
290 handle
= map_channel(&metadata_data
,
291 &metadata_stream_data
, 1);
294 snprintf(pathname
, PATH_MAX
- 1,
295 "%s/metadata", outputpath
);
296 ret
= consume_stream(handle
, -1, pathname
);
297 if (ret
&& ret
!= -ENOENT
) {
298 printf("Error in consume_stream\n");
301 unmap_channel(handle
);
303 /* copy binary data */
304 handle
= map_channel(&channel_data
,
305 stream_data
, MAX_NR_STREAMS
);
308 for (k
= 0; k
< MAX_NR_STREAMS
; k
++) {
309 snprintf(pathname
, PATH_MAX
- 1,
310 "%s/data_%u", outputpath
, k
);
311 ret
= consume_stream(handle
, k
, pathname
);
312 if (ret
&& ret
!= -ENOENT
) {
313 printf("Error in consume_stream\n");
317 unmap_channel(handle
);
323 int send_app_msgs(int sock
, const char *outputpath
,
324 unsigned int nr_events
, const char **event_names
)
326 struct ustcomm_ust_msg lum
;
327 struct ustcomm_ust_reply lur
;
331 memset(&lum
, 0, sizeof(lum
));
332 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
333 lum
.cmd
= LTTNG_UST_SESSION
;
334 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
337 session_handle
= lur
.ret_val
;
338 printf("received session handle %u\n", session_handle
);
340 /* Create metadata channel */
341 memset(&lum
, 0, sizeof(lum
));
342 lum
.handle
= session_handle
;
343 lum
.cmd
= LTTNG_UST_METADATA
;
344 lum
.u
.channel
.overwrite
= 0;
345 lum
.u
.channel
.subbuf_size
= 32768;
346 lum
.u
.channel
.num_subbuf
= 4;
347 lum
.u
.channel
.switch_timer_interval
= 0;
348 lum
.u
.channel
.read_timer_interval
= 0;
349 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
350 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
353 metadata_data
.handle
= lur
.ret_val
;
354 printf("received metadata handle %u\n", metadata_data
.handle
);
355 if (lur
.ret_code
== USTCOMM_OK
) {
358 metadata_data
.memory_map_size
= lur
.u
.channel
.memory_map_size
;
360 len
= ustcomm_recv_fd(sock
);
363 metadata_data
.shm_fd
= len
;
365 len
= ustcomm_recv_fd(sock
);
368 metadata_data
.wait_fd
= len
;
371 ret
= open_streams(sock
, metadata_data
.handle
,
372 &metadata_stream_data
, 1);
374 printf("Error in open_streams\n");
378 /* Create data channel */
379 memset(&lum
, 0, sizeof(lum
));
380 lum
.handle
= session_handle
;
381 lum
.cmd
= LTTNG_UST_CHANNEL
;
382 //lum.u.channel.overwrite = 0;
383 lum
.u
.channel
.overwrite
= 1;
384 lum
.u
.channel
.subbuf_size
= 32768;
385 lum
.u
.channel
.num_subbuf
= 8;
386 //lum.u.channel.num_subbuf = 4;
387 //lum.u.channel.num_subbuf = 2;
388 lum
.u
.channel
.switch_timer_interval
= 0;
389 lum
.u
.channel
.read_timer_interval
= 0;
390 lum
.u
.channel
.output
= LTTNG_UST_MMAP
;
391 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
394 channel_data
.handle
= lur
.ret_val
;
395 printf("received channel handle %u\n", channel_data
.handle
);
396 if (lur
.ret_code
== USTCOMM_OK
) {
399 channel_data
.memory_map_size
= lur
.u
.channel
.memory_map_size
;
401 len
= ustcomm_recv_fd(sock
);
404 channel_data
.shm_fd
= len
;
406 len
= ustcomm_recv_fd(sock
);
409 channel_data
.wait_fd
= len
;
413 for (k
= 0; k
< nr_events
; k
++) {
414 memset(&lum
, 0, sizeof(lum
));
415 lum
.handle
= channel_data
.handle
;
416 lum
.cmd
= LTTNG_UST_EVENT
;
417 strncpy(lum
.u
.event
.name
, event_names
[k
],
418 LTTNG_UST_SYM_NAME_LEN
);
419 lum
.u
.event
.instrumentation
= LTTNG_UST_TRACEPOINT
;
420 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
423 event_handle
[k
] = lur
.ret_val
;
424 printf("received event handle %u\n", event_handle
[k
]);
427 /* Attach vtid context */
428 memset(&lum
, 0, sizeof(lum
));
429 lum
.handle
= channel_data
.handle
;
430 lum
.cmd
= LTTNG_UST_CONTEXT
;
431 lum
.u
.context
.ctx
= LTTNG_UST_CONTEXT_VTID
;
432 //lum.u.context.ctx = LTTNG_UST_CONTEXT_PTHREAD_ID;
433 //lum.u.context.ctx = LTTNG_UST_CONTEXT_VPID;
434 //lum.u.context.ctx = LTTNG_UST_CONTEXT_PROCNAME;
435 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
438 context_handle
= lur
.ret_val
;
439 printf("received context handle %u\n", context_handle
);
441 /* Get references to channel streams */
442 ret
= open_streams(sock
, channel_data
.handle
,
443 stream_data
, MAX_NR_STREAMS
);
445 printf("Error in open_streams\n");
449 memset(&lum
, 0, sizeof(lum
));
450 lum
.handle
= session_handle
;
451 lum
.cmd
= LTTNG_UST_SESSION_START
;
452 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
455 printf("Session handle %u started.\n", session_handle
);
457 /* Tell application registration is done */
458 memset(&lum
, 0, sizeof(lum
));
459 lum
.handle
= LTTNG_UST_ROOT_HANDLE
;
460 lum
.cmd
= LTTNG_UST_REGISTER_DONE
;
461 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
464 printf("Registration done acknowledged.\n");
468 ret
= consume_buffers(outputpath
);
470 printf("Error in consume_buffers\n");
474 /* Release data channel */
475 /* Release streams */
476 ret
= close_streams(sock
, stream_data
,
481 /* Release context */
482 memset(&lum
, 0, sizeof(lum
));
483 lum
.handle
= context_handle
;
484 lum
.cmd
= LTTNG_UST_RELEASE
;
485 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
490 for (k
= 0; k
< nr_events
; k
++) {
491 memset(&lum
, 0, sizeof(lum
));
492 lum
.handle
= event_handle
[k
];
493 lum
.cmd
= LTTNG_UST_RELEASE
;
494 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
498 memset(&lum
, 0, sizeof(lum
));
499 lum
.handle
= channel_data
.handle
;
500 lum
.cmd
= LTTNG_UST_RELEASE
;
501 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
504 if (channel_data
.shm_fd
>= 0) {
505 ret
= close(channel_data
.shm_fd
);
509 if (channel_data
.wait_fd
>= 0) {
510 ret
= close(channel_data
.wait_fd
);
515 /* Release metadata channel */
516 ret
= close_streams(sock
, &metadata_stream_data
, 1);
520 memset(&lum
, 0, sizeof(lum
));
521 lum
.handle
= metadata_data
.handle
;
522 lum
.cmd
= LTTNG_UST_RELEASE
;
523 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
526 if (metadata_data
.shm_fd
>= 0) {
527 ret
= close(metadata_data
.shm_fd
);
531 if (metadata_data
.wait_fd
>= 0) {
532 ret
= close(metadata_data
.wait_fd
);
537 /* Release session */
538 memset(&lum
, 0, sizeof(lum
));
539 lum
.handle
= session_handle
;
540 lum
.cmd
= LTTNG_UST_RELEASE
;
541 ret
= ustcomm_send_app_cmd(sock
, &lum
, &lur
);
549 * Using fork to set umask in the child process (not multi-thread safe). We
550 * deal with the shm_open vs ftruncate race (happening when the sessiond owns
551 * the shm and does not let everybody modify it, to ensure safety against
552 * shm_unlink) by simply letting the mmap fail and retrying after a few
553 * seconds. For global shm, everybody has rw access to it until the sessiond
556 static int get_wait_shm(char *shm_path
, size_t mmap_size
, int global
)
558 int wait_shm_fd
, ret
;
561 /* Default permissions */
562 mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
;
564 /* Change owner of the shm path */
566 ret
= chown(shm_path
, 0, 0);
568 if (errno
!= ENOENT
) {
569 perror("chown wait shm");
575 * If global session daemon, any application can register so the shm
576 * needs to be set in read-only mode for others.
580 ret
= chown(shm_path
, getuid(), getgid());
582 if (errno
!= ENOENT
) {
583 perror("chown wait shm");
590 * Set permissions to the shm even if we did not create the shm.
592 ret
= chmod(shm_path
, mode
);
594 if (errno
!= ENOENT
) {
595 perror("chmod wait shm");
601 * We're alone in a child process, so we can modify the process-wide
607 * Try creating shm (or get rw access). We don't do an exclusive open,
608 * because we allow other processes to create+ftruncate it concurrently.
610 wait_shm_fd
= shm_open(shm_path
, O_RDWR
| O_CREAT
, mode
);
611 if (wait_shm_fd
< 0) {
612 perror("shm_open wait shm");
616 ret
= ftruncate(wait_shm_fd
, mmap_size
);
618 perror("ftruncate wait shm");
622 ret
= fchmod(wait_shm_fd
, mode
);
628 printf("Got the wait shm fd %d\n", wait_shm_fd
);
633 printf("Failing to get the wait shm fd\n");
638 int update_futex(int fd
, int active
)
640 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
644 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
| PROT_WRITE
,
646 if (wait_shm_mmap
== MAP_FAILED
) {
652 uatomic_set((int32_t *) wait_shm_mmap
, 1);
653 futex_async((int32_t *) wait_shm_mmap
, FUTEX_WAKE
,
654 INT_MAX
, NULL
, NULL
, 0);
656 uatomic_set((int32_t *) wait_shm_mmap
, 0);
658 ret
= munmap(wait_shm_mmap
, mmap_size
);
660 perror("Error unmapping wait shm");
669 * Set open files limit to unlimited. This daemon can open a large number of
670 * file descriptors in order to consumer multiple kernel traces.
672 static void set_ulimit(void)
678 * If not root, we cannot increase our max open files. But our
679 * scope is then limited to processes from a single user.
683 /* The kernel does not allowed an infinite limit for open files */
684 lim
.rlim_cur
= 65535;
685 lim
.rlim_max
= 65535;
687 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
689 perror("failed to set open files limit");
695 * ./ust-basic-tracing outputpath event_name1 event_name2 ....
697 int main(int argc
, const char **argv
)
699 const char *home_dir
;
700 int ret
, wait_shm_fd
;
701 struct sigaction act
;
702 mode_t old_umask
= 0;
703 const char *outputpath
;
704 const char **event_names
;
705 unsigned int nr_events
;
709 printf("%s outputpath event_name1 event_name2 ...\n",
713 outputpath
= argv
[1];
714 event_names
= &argv
[2];
715 nr_events
= argc
- 2;
720 memset(&act
, 0, sizeof(act
));
721 ret
= sigemptyset(&act
.sa_mask
);
723 perror("sigemptyset");
727 act
.sa_handler
= SIG_IGN
;
728 ret
= sigaction(SIGPIPE
, &act
, NULL
);
735 act
.sa_handler
= handle_signals
;
736 ret
= sigaction(SIGTERM
, &act
, NULL
);
742 ret
= sigaction(SIGINT
, &act
, NULL
);
748 if (geteuid() == 0) {
749 ret
= mkdir(LTTNG_RUNDIR
, S_IRWXU
| S_IRWXG
| S_IROTH
| S_IXOTH
);
750 if (ret
&& errno
!= EEXIST
) {
754 wait_shm_fd
= get_wait_shm(DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
755 sysconf(_SC_PAGE_SIZE
), 1);
756 if (wait_shm_fd
< 0) {
757 perror("global wait shm error");
760 strcpy(apps_sock_path
, DEFAULT_GLOBAL_APPS_UNIX_SOCK
);
761 old_umask
= umask(0);
763 snprintf(local_apps_wait_shm_path
, PATH_MAX
,
764 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, getuid());
765 wait_shm_fd
= get_wait_shm(local_apps_wait_shm_path
,
766 sysconf(_SC_PAGE_SIZE
), 0);
767 if (wait_shm_fd
< 0) {
768 perror("local wait shm error");
771 home_dir
= (const char *) getenv("HOME");
773 perror("getenv error");
776 snprintf(apps_sock_path
, PATH_MAX
,
777 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
780 ret
= ustcomm_create_unix_sock(apps_sock_path
);
782 perror("create error");
788 /* File permission MUST be 666 */
789 ret
= chmod(apps_sock_path
,
790 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
);
792 printf("Set file permissions failed: %s\n", apps_sock_path
);
798 ret
= ustcomm_listen_unix_sock(apps_socket
);
800 perror("listen error");
804 /* wake up futexes */
805 ret
= update_futex(wait_shm_fd
, 1);
807 fprintf(stderr
, "Error wakeup futex\n");
821 char name
[16]; /* Process name */
828 printf("Accepting application registration\n");
829 sock
= ustcomm_accept_unix_sock(apps_socket
);
831 perror("accept error");
836 * Basic recv here to handle the very simple data
837 * that the libust send to register (reg_msg).
839 len
= ustcomm_recv_unix_sock(sock
, ®_msg
, sizeof(reg_msg
));
840 if (len
< 0 || len
!= sizeof(reg_msg
)) {
841 perror("ustcomm_recv_unix_sock");
844 memcpy(bufname
, reg_msg
.name
, 16);
846 printf("Application %s pid %u ppid %u uid %u gid %u has registered (version : %u.%u)\n",
847 bufname
, reg_msg
.pid
, reg_msg
.ppid
, reg_msg
.uid
,
848 reg_msg
.gid
, reg_msg
.major
, reg_msg
.minor
);
849 ret
= send_app_msgs(sock
, outputpath
, nr_events
, event_names
);
851 printf("Error in send_app_msgs.\n");
858 printf("quitting.\n");
859 /* Let applications know we are not responding anymore */
860 ret
= update_futex(wait_shm_fd
, 0);
862 fprintf(stderr
, "Error wakeup futex\n");