11 #define _SDT_HAS_SEMAPHORES 1
15 #include "bpf/libbpf_internal.h"
17 #define SEC(name) __attribute__((section(name), used))
21 /* defined in urandom_read_aux.c */
22 void urand_read_without_sema(int iter_num, int iter_cnt, int read_sz);
23 /* these are coming from urandom_read_lib{1,2}.c */
24 void urandlib_read_with_sema(int iter_num, int iter_cnt, int read_sz);
25 void urandlib_read_without_sema(int iter_num, int iter_cnt, int read_sz);
27 int urandlib_api(void);
28 COMPAT_VERSION(urandlib_api_old, urandlib_api, LIBURANDOM_READ_1.0.0)
29 int urandlib_api_old(void);
30 int urandlib_api_sameoffset(void);
32 unsigned short urand_read_with_sema_semaphore SEC(".probes");
34 static noinline void urandom_read(int fd, int count)
39 for (i = 0; i < count; ++i) {
40 read(fd, buf, BUF_SIZE);
42 /* trigger USDTs defined in executable itself */
43 urand_read_without_sema(i, count, BUF_SIZE);
44 STAP_PROBE3(urand, read_with_sema, i, count, BUF_SIZE);
46 /* trigger USDTs defined in shared lib */
47 urandlib_read_without_sema(i, count, BUF_SIZE);
48 urandlib_read_with_sema(i, count, BUF_SIZE);
52 static volatile bool parent_ready;
54 static void handle_sigpipe(int sig)
59 int main(int argc, char *argv[])
61 int fd = open("/dev/urandom", O_RDONLY);
63 bool report_pid = false;
69 count = atoi(argv[1]);
72 /* install SIGPIPE handler to catch when parent closes their
73 * end of the pipe (on the other side of our stdout)
75 signal(SIGPIPE, handle_sigpipe);
78 /* report PID and wait for parent process to send us "signal" by
82 while (!parent_ready) {
83 fprintf(stdout, "%d\n", getpid());
86 /* at this point stdout is closed, parent process knows our
87 * PID and is ready to trace us
91 urandom_read(fd, count);
95 urandlib_api_sameoffset();