Skip to content

Commit

Permalink
Use modern interposing
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.macosforge.org/repository/darwinbuild/branches/PR-8817822@925 10a61168-4876-4dac-953b-31e694342555
  • Loading branch information
wsiegrist@apple.com committed Feb 8, 2011
1 parent f094edd commit 341e263
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions darwintrace/darwintrace.c
Expand Up @@ -55,6 +55,10 @@
#define dprintf(...) #define dprintf(...)
#endif #endif


#define DARWINTRACE_INTERPOSE(_replacement,_replacee) \
__attribute__((used)) static struct{ const void* replacement; const void* replacee; } _interpose_##_replacee \
__attribute__ ((section ("__DATA,__interpose"))) = { (const void*)(unsigned long)&_replacement, (const void*)(unsigned long)&_replacee };

static int __darwintrace_fd = -2; static int __darwintrace_fd = -2;
static char __darwintrace_progname[BUFFER_SIZE]; static char __darwintrace_progname[BUFFER_SIZE];
static pid_t __darwintrace_pid = -1; static pid_t __darwintrace_pid = -1;
Expand Down Expand Up @@ -156,14 +160,13 @@ static inline void __darwintrace_setup() {


/* __darwintrace_setup must have been called already */ /* __darwintrace_setup must have been called already */
static inline void __darwintrace_logpath(int fd, const char *procname, char *tag, const char *path) { static inline void __darwintrace_logpath(int fd, const char *procname, char *tag, const char *path) {
#pragma unused(procname)
char __darwintrace_buf[BUFFER_SIZE]; char __darwintrace_buf[BUFFER_SIZE];
int size; int size;


size = snprintf(__darwintrace_buf, sizeof(__darwintrace_buf), size = snprintf(__darwintrace_buf, sizeof(__darwintrace_buf),
"%s[%d]\t%s\t%s\n", "%s[%d]\t%s\t%s\n",
procname ? procname : __darwintrace_progname, __darwintrace_pid, procname ? procname : __darwintrace_progname, __darwintrace_pid,
tag, path ); tag, path );


write(fd, __darwintrace_buf, size); write(fd, __darwintrace_buf, size);
fsync(fd); fsync(fd);
Expand Down Expand Up @@ -212,16 +215,13 @@ static inline void __darwintrace_cleanup_path(char *path) {
dprintf("darwintrace: cleanup resulted in %s\n", path); dprintf("darwintrace: cleanup resulted in %s\n", path);
} }


/* Log calls to open(2) into the file specified by DARWINTRACE_LOG. /*
Only logs if the DARWINTRACE_LOG environment variable is set.
Only logs files where the open succeeds. Only logs files where the open succeeds.
Only logs files opened for read access, without the O_CREAT flag set. Only logs files opened for read access, without the O_CREAT flag set.
The assumption is that any file that can be created isn't necessary The assumption is that any file that can be created isn't necessary
to build the project. to build the project.
*/ */

int darwintrace_open(const char* path, int flags, ...) {
int open(const char* path, int flags, ...) {
#define open(x,y,z) syscall(SYS_open, (x), (y), (z))
mode_t mode; mode_t mode;
int result; int result;
va_list args; va_list args;
Expand Down Expand Up @@ -273,14 +273,13 @@ int open(const char* path, int flags, ...) {
__free_path(redirpath, path); __free_path(redirpath, path);
return result; return result;
} }
DARWINTRACE_INTERPOSE(darwintrace_open, open)


/* Log calls to readlink(2) into the file specified by DARWINTRACE_LOG.
Only logs if the DARWINTRACE_LOG environment variable is set. /*
Only logs files where the readlink succeeds. Only logs files where the readlink succeeds.
*/ */

ssize_t darwintrace_readlink(const char * path, char * buf, size_t bufsiz) {
ssize_t readlink(const char * path, char * buf, size_t bufsiz) {
#define readlink(x,y,z) syscall(SYS_readlink, (x), (y), (z))
ssize_t result; ssize_t result;


char* redirpath = __redirect_path(path); char* redirpath = __redirect_path(path);
Expand All @@ -304,9 +303,10 @@ ssize_t readlink(const char * path, char * buf, size_t bufsiz) {
__free_path(redirpath, path); __free_path(redirpath, path);
return result; return result;
} }
DARWINTRACE_INTERPOSE(darwintrace_readlink, readlink)


int execve(const char* path, char* const argv[], char* const envp[]) {
#define execve(x,y,z) syscall(SYS_execve, (x), (y), (z)) int darwintrace_execve(const char* path, char* const argv[], char* const envp[]) {
int result; int result;


char* redirpath = __redirect_path(path); char* redirpath = __redirect_path(path);
Expand Down Expand Up @@ -432,17 +432,19 @@ int execve(const char* path, char* const argv[], char* const envp[]) {
__free_path(redirpath, path); __free_path(redirpath, path);
return result; return result;
} }
DARWINTRACE_INTERPOSE(darwintrace_execve, execve)



/* if darwintrace has been initialized, trap /*
if darwintrace has been initialized, trap
attempts to close our file descriptor attempts to close our file descriptor
*/ */
int close(int fd) { int darwintrace_close(int fd) {
#define close(x) syscall(SYS_close, (x))

if(__darwintrace_fd != -2 && fd == __darwintrace_fd) { if(__darwintrace_fd != -2 && fd == __darwintrace_fd) {
errno = EBADF; errno = EBADF;
return -1; return -1;
} }


return close(fd); return close(fd);
} }
DARWINTRACE_INTERPOSE(darwintrace_close, close)

0 comments on commit 341e263

Please sign in to comment.