1 #define clock_gettime __real_clock_gettime
2 #define open __real_open
3 #define __syscall __real___syscall
4 #define mmap __real_mmap
5 #define ftruncate __real_ftruncate
6 #define dprintf __real_dprintf
7 #define _exit __real__exit
14 #include <sys/syscall.h>
24 #define T(i,f) {#f "\n", sizeof #f},
34 static void tab_init(void)
36 // todo: argv[0] ".ldtrace"
37 int fd = open("/tmp/wrap", O_CREAT|O_TRUNC|O_RDWR|O_NOFOLLOW|O_CLOEXEC|O_NONBLOCK, 0666);
39 dprintf(2, "open /tmp/wrap failed: %m\n");
42 if (ftruncate(fd, 2000 * sizeof *tab)) {
43 dprintf(2, "ftruncate: %m\n");
46 tab = mmap(0, 2000 * sizeof *tab, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
47 if (tab == MAP_FAILED) {
48 dprintf(2, "mmap: %m\n");
53 static int show_trace=1;
54 static int log_time=1;
62 static struct entry *sp;
64 long __syscall(long,...);
65 static void after(void);
68 static void enter(int n, void **retaddr)
75 __syscall(SYS_write, 2, nametab[n].s, nametab[n].len);
82 clock_gettime(CLOCK_REALTIME, &ts);
83 sp->t = ts.tv_sec*1000000000ULL + ts.tv_nsec;
89 static void *leave(void)
94 clock_gettime(CLOCK_REALTIME, &ts);
95 tab[sp->n].t += ts.tv_sec*1000000000ULL + ts.tv_nsec - sp->t;