|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <dlfcn.h> |
| 4 | +#include <string.h> |
| 5 | + |
| 6 | +#include "itable.h" |
| 7 | + |
| 8 | +typedef FILE *(*fopen_t)(const char *p, const char *m); |
| 9 | +typedef int (*open_t)(const char *p, int f); |
| 10 | +typedef ssize_t(*rw_t)(int fd, void *b, ssize_t c); |
| 11 | +typedef int (*stat_t)(const char *p, void *s); |
| 12 | +typedef int (*fstat_t)(int fd, void *s); |
| 13 | + |
| 14 | +typedef struct { |
| 15 | + char *pathname; |
| 16 | + int num_open; |
| 17 | + int bytes_read; |
| 18 | + int bytes_write; |
| 19 | + int num_reads; |
| 20 | + int num_writes; |
| 21 | + int num_stat; |
| 22 | +} table_entry; |
| 23 | + |
| 24 | + |
| 25 | +struct itable *file_table; /* maps fd -> table_entry */ |
| 26 | + |
| 27 | +void print_file_table(struct itable *t); |
| 28 | + |
| 29 | +void __attribute__((constructor)) filetrace_init() { |
| 30 | + file_table = itable_create(0); |
| 31 | +} |
| 32 | + |
| 33 | +void __attribute__((destructor)) filetrace_exit() { |
| 34 | + print_file_table(file_table); |
| 35 | + itable_clear(file_table, 0); |
| 36 | + itable_delete(file_table); |
| 37 | +} |
| 38 | + |
| 39 | +void file_table_event_open(int fd, const char *pathname, int flags) { |
| 40 | + if(!itable_lookup(file_table, fd)){ |
| 41 | + table_entry *e = malloc(sizeof(table_entry)); |
| 42 | + e->num_open = 1; |
| 43 | + e->pathname = strdup(pathname); |
| 44 | + itable_insert(file_table, fd, e); |
| 45 | + } else { |
| 46 | + table_entry *e = itable_lookup(file_table, fd); |
| 47 | + e->num_open += 1; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +FILE *fopen(const char *pathname, const char *mode) { |
| 52 | + fopen_t real_fopen = dlsym(RTLD_NEXT, "fopen"); |
| 53 | + return real_fopen(pathname,mode); |
| 54 | +} |
| 55 | + |
| 56 | +int open(const char *pathname, int flags) { |
| 57 | + open_t real_open = dlsym(RTLD_NEXT, "open"); |
| 58 | + int fd = real_open(pathname,flags); |
| 59 | + file_table_event_open(fd, pathname, flags); |
| 60 | + return fd; |
| 61 | +} |
| 62 | + |
| 63 | +ssize_t read(int fd, void *buf, size_t count){ |
| 64 | + rw_t real_read = dlsym(RTLD_NEXT, "read"); |
| 65 | + return real_read(fd, buf, count); |
| 66 | +} |
| 67 | + |
| 68 | +ssize_t write(int fd, void *buf, size_t count){ |
| 69 | + rw_t real_write = dlsym(RTLD_NEXT, "write"); |
| 70 | + return real_write(fd, buf, count); |
| 71 | +} |
| 72 | + |
| 73 | +int stat(const char *pathname, void *statbuf){ |
| 74 | + stat_t real_stat = dlsym(RTLD_NEXT, "stat"); |
| 75 | + return real_stat(pathname, statbuf); |
| 76 | +} |
| 77 | + |
| 78 | +int fstat(int fd, void *statbuf){ |
| 79 | + fstat_t real_fstat = dlsym(RTLD_NEXT, "fstat"); |
| 80 | + return real_fstat(fd, statbuf); |
| 81 | +} |
| 82 | + |
| 83 | +void print_file_table(struct itable *t){ |
| 84 | + printf("Filetrace Summary\n"); |
| 85 | + |
| 86 | + uint64_t fd; |
| 87 | + table_entry *e; |
| 88 | + ITABLE_ITERATE(t, fd, e){ |
| 89 | + printf("File Descriptor: %d\n", fd); |
| 90 | + printf("\tpathname: %s\n", e->pathname); |
| 91 | + printf("\tnum_open: %d\n\n", e->num_open); |
| 92 | + } |
| 93 | +} |
0 commit comments