diff options
-rw-r--r-- | BUGS | 4 | ||||
-rw-r--r-- | KK | 489 | ||||
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | TODO | 2 | ||||
-rw-r--r-- | debian/changelog | 6 | ||||
-rw-r--r-- | etc/ltrace.conf | 33 | ||||
-rw-r--r-- | ltrace.c | 65 | ||||
-rw-r--r-- | ltrace.h | 2 | ||||
-rw-r--r-- | options.c | 22 | ||||
-rw-r--r-- | process_event.c | 25 | ||||
-rw-r--r-- | sysdeps/Linux/Makefile | 2 | ||||
-rw-r--r-- | sysdeps/Linux/proc.c | 19 | ||||
-rw-r--r-- | sysdeps/Linux/trace.c | 9 |
13 files changed, 148 insertions, 533 deletions
@@ -1,4 +1,6 @@ -* options -p and -f don't work yet +* processes attached with option -p die when the attacher is interrupted +* option -p and -f don't work yet +* option -f doesn't work yet * if a program uses `execve()', later syscalls aren't logged correctly * Manual page is not accurate (config files...) * elf.c only supports elf32 binaries @@ -1,489 +0,0 @@ -Reading config file `/etc/ltrace.conf'... -Reading line 1 of `/etc/ltrace.conf' - Skipping line 1 -Reading line 2 of `/etc/ltrace.conf' - Skipping line 2 -Reading line 3 of `/etc/ltrace.conf' - Skipping line 3 -Reading line 4 of `/etc/ltrace.conf' - Skipping line 4 -Reading line 5 of `/etc/ltrace.conf' - Skipping line 5 -Reading line 6 of `/etc/ltrace.conf' - Skipping line 6 -Reading line 7 of `/etc/ltrace.conf' - Skipping line 7 -Reading line 8 of `/etc/ltrace.conf' - Skipping line 8 -Reading line 9 of `/etc/ltrace.conf' - Skipping line 9 -Reading line 10 of `/etc/ltrace.conf' - Skipping line 10 -Reading line 11 of `/etc/ltrace.conf' - Skipping line 11 -Reading line 12 of `/etc/ltrace.conf' - Skipping line 12 -Reading line 13 of `/etc/ltrace.conf' - Skipping line 13 -Reading line 14 of `/etc/ltrace.conf' - Skipping line 14 -Reading line 15 of `/etc/ltrace.conf' - Skipping line 15 -Reading line 16 of `/etc/ltrace.conf' - Skipping line 16 -Reading line 17 of `/etc/ltrace.conf' - name = __errno_location -New function: `__errno_location' -Reading line 18 of `/etc/ltrace.conf' - Skipping line 18 -Reading line 19 of `/etc/ltrace.conf' - Skipping line 19 -Reading line 20 of `/etc/ltrace.conf' - name = open -New function: `open' -Reading line 21 of `/etc/ltrace.conf' - Skipping line 21 -Reading line 22 of `/etc/ltrace.conf' - Skipping line 22 -Reading line 23 of `/etc/ltrace.conf' - name = getopt_long -New function: `getopt_long' -Reading line 24 of `/etc/ltrace.conf' - Skipping line 24 -Reading line 25 of `/etc/ltrace.conf' - Skipping line 25 -Reading line 26 of `/etc/ltrace.conf' - name = bindtextdomain -New function: `bindtextdomain' -Reading line 27 of `/etc/ltrace.conf' - name = textdomain -New function: `textdomain' -Reading line 28 of `/etc/ltrace.conf' - Skipping line 28 -Reading line 29 of `/etc/ltrace.conf' - Skipping line 29 -Reading line 30 of `/etc/ltrace.conf' - name = _IO_putc -New function: `_IO_putc' -Reading line 31 of `/etc/ltrace.conf' - Skipping line 31 -Reading line 32 of `/etc/ltrace.conf' - Skipping line 32 -Reading line 33 of `/etc/ltrace.conf' - name = setlocale -New function: `setlocale' -Reading line 34 of `/etc/ltrace.conf' - Skipping line 34 -Reading line 35 of `/etc/ltrace.conf' - Skipping line 35 -Reading line 36 of `/etc/ltrace.conf' - name = fclose -New function: `fclose' -Reading line 37 of `/etc/ltrace.conf' - name = fgets -New function: `fgets' -Reading line 38 of `/etc/ltrace.conf' - name = fprintf -New function: `fprintf' -Reading line 39 of `/etc/ltrace.conf' - name = fputs -New function: `fputs' -Reading line 40 of `/etc/ltrace.conf' - name = printf -New function: `printf' -Reading line 41 of `/etc/ltrace.conf' - Skipping line 41 -Reading line 42 of `/etc/ltrace.conf' - Skipping line 42 -Reading line 43 of `/etc/ltrace.conf' - name = close -New function: `close' -Reading line 44 of `/etc/ltrace.conf' - name = fork -New function: `fork' -Reading line 45 of `/etc/ltrace.conf' - name = geteuid -New function: `geteuid' -Reading line 46 of `/etc/ltrace.conf' - name = gethostname -New function: `gethostname' -Reading line 47 of `/etc/ltrace.conf' - name = mkdir -New function: `mkdir' -Reading line 48 of `/etc/ltrace.conf' - name = read -New function: `read' -Reading line 49 of `/etc/ltrace.conf' - name = sethostname -New function: `sethostname' -Reading line 50 of `/etc/ltrace.conf' - name = sleep -New function: `sleep' -Reading line 51 of `/etc/ltrace.conf' - name = sync -New function: `sync' -Reading line 52 of `/etc/ltrace.conf' - name = write -New function: `write' -Reading line 53 of `/etc/ltrace.conf' - Skipping line 53 -Reading line 54 of `/etc/ltrace.conf' - Skipping line 54 -Reading line 55 of `/etc/ltrace.conf' - name = atexit -New function: `atexit' -Reading line 56 of `/etc/ltrace.conf' - name = exit -New function: `exit' -Reading line 57 of `/etc/ltrace.conf' - name = free -New function: `free' -Reading line 58 of `/etc/ltrace.conf' - name = malloc -New function: `malloc' -Reading line 59 of `/etc/ltrace.conf' - Skipping line 59 -Reading line 60 of `/etc/ltrace.conf' - Skipping line 60 -Reading line 61 of `/etc/ltrace.conf' - name = memset -New function: `memset' -Reading line 62 of `/etc/ltrace.conf' - name = rindex -New function: `rindex' -Reading line 63 of `/etc/ltrace.conf' - name = strcmp -New function: `strcmp' -Reading line 64 of `/etc/ltrace.conf' - name = strcpy -New function: `strcpy' -Reading line 65 of `/etc/ltrace.conf' - name = strdup -New function: `strdup' -Reading line 66 of `/etc/ltrace.conf' - name = strncmp -New function: `strncmp' -Reading line 67 of `/etc/ltrace.conf' - name = strncpy -New function: `strncpy' -Reading line 68 of `/etc/ltrace.conf' - name = strrchr -New function: `strrchr' -Reading line 69 of `/etc/ltrace.conf' - name = strstr -New function: `strstr' -Reading line 70 of `/etc/ltrace.conf' - Skipping line 70 -Reading line 71 of `/etc/ltrace.conf' - Skipping line 71 -Reading line 72 of `/etc/ltrace.conf' - name = time -New function: `time' -Reading line 73 of `/etc/ltrace.conf' - Skipping line 73 -Reading line 74 of `/etc/ltrace.conf' - Skipping line 74 -Reading line 75 of `/etc/ltrace.conf' - name = SYS_brk -New function: `SYS_brk' -Reading line 76 of `/etc/ltrace.conf' - name = SYS_close -New function: `SYS_close' -Reading line 77 of `/etc/ltrace.conf' - name = SYS_execve -New function: `SYS_execve' -Reading line 78 of `/etc/ltrace.conf' - name = SYS_exit -New function: `SYS_exit' -Reading line 79 of `/etc/ltrace.conf' - name = SYS_fork -New function: `SYS_fork' -Reading line 80 of `/etc/ltrace.conf' - name = SYS_getpid -New function: `SYS_getpid' -Reading line 81 of `/etc/ltrace.conf' - Skipping line 81 -Reading line 82 of `/etc/ltrace.conf' - name = SYS_munmap -New function: `SYS_munmap' -Reading line 83 of `/etc/ltrace.conf' - name = SYS_open -New function: `SYS_open' -Reading line 84 of `/etc/ltrace.conf' - name = SYS_personality -New function: `SYS_personality' -Reading line 85 of `/etc/ltrace.conf' - name = SYS_read -New function: `SYS_read' -Reading line 86 of `/etc/ltrace.conf' - name = SYS_write -New function: `SYS_write' -Reading line 87 of `/etc/ltrace.conf' - name = SYS_sync -New function: `SYS_sync' -Reading config file `/home/cespedes/.ltrace.conf'... -Reading line 1 of `/home/cespedes/.ltrace.conf' - Skipping line 1 -Reading line 2 of `/home/cespedes/.ltrace.conf' - Skipping line 2 -Reading line 3 of `/home/cespedes/.ltrace.conf' - Skipping line 3 -Reading line 4 of `/home/cespedes/.ltrace.conf' - Skipping line 4 -Reading line 5 of `/home/cespedes/.ltrace.conf' - Skipping line 5 -Reading line 6 of `/home/cespedes/.ltrace.conf' - Skipping line 6 -Reading line 7 of `/home/cespedes/.ltrace.conf' - Skipping line 7 -Reading line 8 of `/home/cespedes/.ltrace.conf' - Skipping line 8 -Reading line 9 of `/home/cespedes/.ltrace.conf' - Skipping line 9 -Reading line 10 of `/home/cespedes/.ltrace.conf' - Skipping line 10 -Reading line 11 of `/home/cespedes/.ltrace.conf' - Skipping line 11 -Reading line 12 of `/home/cespedes/.ltrace.conf' - Skipping line 12 -Reading line 13 of `/home/cespedes/.ltrace.conf' - Skipping line 13 -Reading line 14 of `/home/cespedes/.ltrace.conf' - Skipping line 14 -Reading line 15 of `/home/cespedes/.ltrace.conf' - Skipping line 15 -Reading line 16 of `/home/cespedes/.ltrace.conf' - Skipping line 16 -Reading line 17 of `/home/cespedes/.ltrace.conf' - name = execlp -New function: `execlp' -Reading line 18 of `/home/cespedes/.ltrace.conf' - Skipping line 18 -Reading line 19 of `/home/cespedes/.ltrace.conf' - Skipping line 19 -Reading line 20 of `/home/cespedes/.ltrace.conf' - name = uname -New function: `uname' -Reading line 21 of `/home/cespedes/.ltrace.conf' - Skipping line 21 -Reading line 22 of `/home/cespedes/.ltrace.conf' - Skipping line 22 -Reading line 23 of `/home/cespedes/.ltrace.conf' - name = SYS_uname -New function: `SYS_uname' -Reading symbol table from /bin/arch... -symtab: 0x400b5184 -symtab_len: 320 -strtab: 0x400b52c4 -addr: 0x0804843c, symbol: "__libc_init_first" -addr: 0x0804845c, symbol: "exit" -addr: 0x0804844c, symbol: "atexit" -addr: 0x0804840c, symbol: "printf" -addr: 0x0804841c, symbol: "perror" -addr: 0x0804842c, symbol: "uname" -Executing `/bin/arch'... -PID=943 -signal from pid 943 -Enabling breakpoints for pid 943... -signal from pid 943 -event: syscall (45) -SYS_brk(NULL <unfinished ...> -signal from pid 943 -event: sysret (45) -<... SYS_brk resumed> ) = 0x08049688 -signal from pid 943 -event: syscall (5) -SYS_open("/etc/ld.so.preload", 0, 010000130160 <unfinished ...> -signal from pid 943 -event: sysret (5) -<... SYS_open resumed> ) = -2 -signal from pid 943 -event: syscall (5) -SYS_open("/usr/lib/miniutils/lib/libc.so.6", 0, 016234672457 <unfinished ...> -signal from pid 943 -event: sysret (5) -<... SYS_open resumed> ) = -2 -signal from pid 943 -event: syscall (5) -SYS_open("/etc/ld.so.cache", 0, 010000130160 <unfinished ...> -signal from pid 943 -event: sysret (5) -<... SYS_open resumed> ) = 6 -signal from pid 943 -event: syscall (108) -SYS_fstat(6, 0xbffff64c, 0x4000b070, 0xbffff6ac, 6 <unfinished ...> -signal from pid 943 -event: sysret (108) -<... SYS_fstat resumed> ) = 0 -signal from pid 943 -event: syscall (90) -SYS_mmap(0xbffff688, 0, 0x4000b070, 1, 6 <unfinished ...> -signal from pid 943 -event: sysret (90) -<... SYS_mmap resumed> ) = 0x4000c000 -signal from pid 943 -event: syscall (6) -SYS_close(6 <unfinished ...> -signal from pid 943 -event: sysret (6) -<... SYS_close resumed> ) = 0 -signal from pid 943 -event: syscall (5) -SYS_open("/lib/libc.so.6", 0, 010000130160 <unfinished ...> -signal from pid 943 -event: sysret (5) -<... SYS_open resumed> ) = 6 -signal from pid 943 -event: syscall (90) -SYS_mmap(0xbffff690, 0xbffff71c, 0x4000b070, 0xbffff71c, 52 <unfinished ...> -signal from pid 943 -event: sysret (90) -<... SYS_mmap resumed> ) = 0x4000f000 -signal from pid 943 -event: syscall (91) -SYS_munmap(0x4000f000, 4096 <unfinished ...> -signal from pid 943 -event: sysret (91) -<... SYS_munmap resumed> ) = 0 -signal from pid 943 -event: syscall (90) -SYS_mmap(0xbffff63c, 0, 0x4000b070, 667384, 6 <unfinished ...> -signal from pid 943 -event: sysret (90) -<... SYS_mmap resumed> ) = 0x4000f000 -signal from pid 943 -event: syscall (125) -SYS_mprotect(0x4009f000, 77560, 0, 0x4000f000, 0x4000b810 <unfinished ...> -signal from pid 943 -event: sysret (125) -<... SYS_mprotect resumed> ) = 0 -signal from pid 943 -event: syscall (90) -SYS_mmap(0xbffff63c, 28672, 0x4000b070, 0x4009f000, 6 <unfinished ...> -signal from pid 943 -event: sysret (90) -<... SYS_mmap resumed> ) = 0x4009f000 -signal from pid 943 -event: syscall (90) -SYS_mmap(0xbffff63c, 0, 0x4000b070, 0x400a6000, 48888 <unfinished ...> -signal from pid 943 -event: sysret (90) -<... SYS_mmap resumed> ) = 0x400a6000 -signal from pid 943 -event: syscall (6) -SYS_close(6 <unfinished ...> -signal from pid 943 -event: sysret (6) -<... SYS_close resumed> ) = 0 -signal from pid 943 -event: syscall (125) -SYS_mprotect(0x4000f000, 589824, 3, 0, -4096 <unfinished ...> -signal from pid 943 -event: sysret (125) -<... SYS_mprotect resumed> ) = 0 -signal from pid 943 -event: syscall (125) -SYS_mprotect(0x4000f000, 589824, 5, 4096, 0x4000f000 <unfinished ...> -signal from pid 943 -event: sysret (125) -<... SYS_mprotect resumed> ) = 0 -signal from pid 943 -event: syscall (136) -SYS_personality(0 <unfinished ...> -signal from pid 943 -event: sysret (136) -<... SYS_personality resumed> ) = 0 -signal from pid 943 -event: syscall (20) -SYS_getpid( <unfinished ...> -signal from pid 943 -event: sysret (20) -<... SYS_getpid resumed> ) = 943 -signal from pid 943 -event: breakpoint (0x0804844c) -atexit(0x40006150 <unfinished ...> -signal from pid 943 -event: breakpoint (0x08048451) -signal from pid 943 -event: breakpoint (0x0804847c) -<... atexit resumed> ) = 0 -signal from pid 943 -event: breakpoint (0x0804843c) -__libc_init_first(1, 0xbffffb76, 0, 0xbffffb80, 0xbffffba8 <unfinished ...> -signal from pid 943 -event: breakpoint (0x08048441) -signal from pid 943 -event: breakpoint (0x08048482) -<... __libc_init_first resumed> ) = 0x40006150 -signal from pid 943 -event: breakpoint (0x0804844c) -atexit(0x08048590 <unfinished ...> -signal from pid 943 -event: breakpoint (0x4002c34f) -signal from pid 943 -event: breakpoint (0x080484a4) -<... atexit resumed> ) = 0 -signal from pid 943 -event: breakpoint (0x0804842c) -uname(0xbffff8f4 <unfinished ...> -signal from pid 943 -event: breakpoint (0x08048431) -signal from pid 943 -event: syscall (122) -SYS_uname(0xbffff8f4 <unfinished ...> -signal from pid 943 -event: sysret (122) -<... SYS_uname resumed> ) = 0 -signal from pid 943 -event: breakpoint (0x0804850e) -<... uname resumed> ) = 0 -signal from pid 943 -event: breakpoint (0x0804840c) -printf("%s\n" <unfinished ...> -signal from pid 943 -event: breakpoint (0x08048411) -signal from pid 943 -event: syscall (108) -SYS_fstat(1, 0xbffff14c, 0x400a5254, 0xbffff1c0, -1 <unfinished ...> -signal from pid 943 -event: sysret (108) -<... SYS_fstat resumed> ) = 0 -signal from pid 943 -event: syscall (90) -SYS_mmap(0xbffff19c, 0, 0x400a5254, 4096, 1 <unfinished ...> -signal from pid 943 -event: sysret (90) -<... SYS_mmap resumed> ) = 0x400b2000 -signal from pid 943 -event: syscall (54) -SYS_ioctl(1, 21505, 0xbffff12c, 0xbffff16c, 0 <unfinished ...> -signal from pid 943 -event: sysret (54) -<... SYS_ioctl resumed> ) = 0 -signal from pid 943 -event: syscall (4) -SYS_write(1, "i586\n", 5 <unfinished ...> -signal from pid 943 -event: sysret (4) -<... SYS_write resumed> ) = 5 -signal from pid 943 -event: breakpoint (0x08048542) -<... printf resumed> ) = 5 -signal from pid 943 -event: breakpoint (0x0804845c) -exit(0) = <void> -signal from pid 943 -event: breakpoint (0x08048461) -signal from pid 943 -event: syscall (91) -SYS_munmap(0x400b2000, 4096 <unfinished ...> -signal from pid 943 -event: sysret (91) -<... SYS_munmap resumed> ) = 0 -signal from pid 943 -event: syscall (1) -SYS_exit(0) = <void> -signal from pid 943 -event: exit (0) -+++ exited (status 0) +++ -No more children @@ -18,7 +18,8 @@ ltrace: sysdeps/sysdep.o $(OBJ) clean: $(MAKE) -C sysdeps/$(OS) clean - rm -f ltrace $(OBJ) + $(RM) ltrace $(OBJ) + $(RM) *~ *.bak core KK dist: clean ( cd .. ; tar zcvf ltrace-`date +%y%m%d`.tgz ltrace ) @@ -1,4 +1,4 @@ -* Option -p (trace running process) +* Option -p: disable breakpoints on exit * Option -f (trace children) * Option -u (run command as username handling setuid and/or setgid) * SYSCALLS: diff --git a/debian/changelog b/debian/changelog index c1f2d3c..09099c7 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +ltrace (0.2.4) unstable; urgency=low + + * Option `-p' now works (but programs fail when ltrace is interrupted) + + -- Juan Cespedes <cespedes@debian.org> Fri, 13 Mar 1998 00:29:10 +0100 + ltrace (0.2.3) unstable; urgency=low * Don't display `...' in strings when limit of bytes is reached diff --git a/etc/ltrace.conf b/etc/ltrace.conf index bed0bf1..375d08a 100644 --- a/etc/ltrace.conf +++ b/etc/ltrace.conf @@ -35,25 +35,14 @@ string setlocale(int, string); ; stdio.h int fclose(file); addr fgets(+string, uint, file); +int fileno(file); int fprintf(file,format); int fputs(string,file); int printf(format); -; unistd.h -int close(int); -int execlp(string,string,addr,addr,addr); -int fork(void); -int geteuid(void); -int gethostname(+string2,int); -int mkdir(string,octal); -int read(int, +string0, uint); -int sethostname(+string2,int); -uint sleep(uint); -int sync(void); -int write(int, string3, uint); - ; stdlib.h int atexit(addr); +addr calloc(uint, uint); void exit(int); void free(addr); string getenv(string); @@ -64,6 +53,7 @@ string basename(string); addr memset(addr,char,int); string rindex(string,char); addr stpcpy(addr,string); +string strcat(string, string); int strcmp(string,string); addr strcpy(addr,string); addr strdup(string); @@ -72,6 +62,23 @@ addr strncpy(addr,string3,uint); string strrchr(string,char); string strstr(string,string); +; term.h +int tputs(string, int, addr); + +; unistd.h +int close(int); +int execlp(string,string,addr,addr,addr); +int fork(void); +int geteuid(void); +int gethostname(+string2,int); +int isatty(int); +int mkdir(string,octal); +int read(int, +string0, uint); +int sethostname(+string2,int); +uint sleep(uint); +int sync(void); +int write(int, string3, uint); + ; sys/stat.h octal umask(octal); @@ -11,18 +11,15 @@ #include "config_file.h" #include "options.h" -char * command; +char * command = NULL; struct process * list_of_processes = NULL; -static struct process * open_program(void); +static struct process * open_program(char * filename); +static void open_pid(pid_t pid); int main(int argc, char **argv) { argv = process_options(argc, argv); - if (opt_p || opt_f) { - fprintf(stderr, "ERROR: Options -p and -f don't work yet\n"); - exit(1); - } read_config_file("/etc/ltrace.conf"); if (getenv("HOME")) { char path[PATH_MAX]; @@ -30,31 +27,59 @@ int main(int argc, char **argv) strcat(path, "/.ltrace.conf"); read_config_file(path); } - execute_program(open_program(), argv); + while (opt_p) { + open_pid(opt_p->pid); + opt_p = opt_p->next; + } + if (opt_f) { + fprintf(stderr, "ERROR: Option -f doesn't work yet\n"); + exit(1); + } + if (command) { + execute_program(open_program(command), argv); + } while(1) { process_event(wait_for_something()); } } -static struct process * open_program(void) +static struct process * open_program(char * filename) { - list_of_processes = malloc(sizeof(struct process)); - if (!list_of_processes) { + struct process * proc; + proc = malloc(sizeof(struct process)); + if (!proc) { perror("malloc"); exit(1); } - list_of_processes->filename = command; - list_of_processes->pid = 0; - list_of_processes->breakpoints_enabled = -1; - list_of_processes->current_syscall = -1; - list_of_processes->current_symbol = NULL; - list_of_processes->breakpoint_being_enabled = NULL; - list_of_processes->next = NULL; + proc->filename = filename; + proc->pid = 0; + proc->breakpoints_enabled = -1; + proc->current_syscall = -1; + proc->current_symbol = NULL; + proc->breakpoint_being_enabled = NULL; + proc->next = NULL; if (opt_L) { - list_of_processes->list_of_symbols = read_elf(command); + proc->list_of_symbols = read_elf(filename); } else { - list_of_processes->list_of_symbols = NULL; + proc->list_of_symbols = NULL; + } + + proc->next = list_of_processes; + list_of_processes = proc; + return proc; +} + +static void open_pid(pid_t pid) +{ + struct process * proc; + char * filename = pid2name(pid); + + if (!filename) { + fprintf(stderr, "Cannot trace pid %u: %s\n", pid, strerror(errno)); + return; } - return list_of_processes; + trace_pid(pid); + proc = open_program(filename); + proc->pid = pid; } @@ -113,7 +113,9 @@ void enable_all_breakpoints(struct process * proc); void disable_all_breakpoints(struct process * proc); /* Arch-dependent stuff: */ +extern char * pid2name(pid_t pid); extern void trace_me(void); +extern void trace_pid(pid_t pid); extern void * get_instruction_pointer(int pid); extern void * get_stack_pointer(int pid); extern void * get_return_addr(int pid, void * stack_pointer); @@ -70,12 +70,14 @@ char ** process_options(int argc, char **argv) argc--; argv++; } switch (*nextchar++) { - case 'a': opt_a = atoi(argv[1]); + case 'a': if (!argv[1]) { usage(); exit(1); } + opt_a = atoi(argv[1]); argc--; argv++; break; case 'd': opt_d++; break; - case 'o': output = fopen(argv[1], "w"); + case 'o': if (!argv[1]) { usage(); exit(1); } + output = fopen(argv[1], "w"); if (!output) { fprintf(stderr, "Can't open %s for output: %s\n", argv[1], strerror(errno)); exit(1); @@ -84,7 +86,8 @@ char ** process_options(int argc, char **argv) break; case 'i': opt_i++; break; - case 's': opt_s = atoi(argv[1]); + case 's': if (!argv[1]) { usage(); exit(1); } + opt_s = atoi(argv[1]); argc--; argv++; break; case 'L': opt_L = 0; @@ -93,10 +96,11 @@ char ** process_options(int argc, char **argv) break; case 'f': opt_f = 1; break; - case 'u': opt_u = argv[1]; + case 'u': if (!argv[1]) { usage(); exit(1); } + opt_u = argv[1]; argc--; argv++; break; - case 'p': + case 'p': if (!argv[1]) { usage(); exit(1); } { struct opt_p_t * tmp = malloc(sizeof(struct opt_p_t)); if (!tmp) { @@ -104,6 +108,8 @@ char ** process_options(int argc, char **argv) exit(1); } tmp->pid = atoi(argv[1]); + tmp->next = opt_p; + opt_p = tmp; argc--; argv++; break; } @@ -113,10 +119,12 @@ char ** process_options(int argc, char **argv) } } - if (argc<2) { + if (!opt_p && argc<2) { usage(); exit(1); } - command = search_for_command(argv[1]); + if (argc>1) { + command = search_for_command(argv[1]); + } return &argv[1]; } diff --git a/process_event.c b/process_event.c index 0689a74..170c37d 100644 --- a/process_event.c +++ b/process_event.c @@ -1,6 +1,7 @@ #define _GNU_SOURCE #include <stdio.h> #include <string.h> +#include <stdlib.h> #include "ltrace.h" #include "output.h" @@ -95,16 +96,40 @@ static void process_signal(struct event * event) continue_after_signal(event->proc->pid, event->e_un.signum); } +static void remove_proc(struct process * proc); + static void process_exit(struct event * event) { output_line(event->proc, "+++ exited (status %d) +++", event->e_un.ret_val); + remove_proc(event->proc); } static void process_exit_signal(struct event * event) { output_line(event->proc, "+++ killed by %s +++", shortsignal(event->e_un.signum)); + remove_proc(event->proc); +} + +static void remove_proc(struct process * proc) +{ + struct process *tmp, *tmp2; + + if (list_of_processes == proc) { + tmp = list_of_processes; + list_of_processes = list_of_processes->next; + free(tmp); + return; + } + tmp = list_of_processes; + while(tmp->next) { + if (tmp->next==proc) { + tmp2 = tmp->next; + tmp->next = tmp->next->next; + free(tmp2); + } + } } static void process_syscall(struct event * event) diff --git a/sysdeps/Linux/Makefile b/sysdeps/Linux/Makefile index 2cfb878..279e274 100644 --- a/sysdeps/Linux/Makefile +++ b/sysdeps/Linux/Makefile @@ -2,7 +2,7 @@ ARCH := $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/) CFLAGS += -I$(TOPDIR)/sysdeps/Linux/$(ARCH) -OBJ = trace.o +OBJ = trace.o proc.o all: sysdep.h signalent.h syscallent.h ../sysdep.o diff --git a/sysdeps/Linux/proc.c b/sysdeps/Linux/proc.c new file mode 100644 index 0000000..ae39166 --- /dev/null +++ b/sysdeps/Linux/proc.c @@ -0,0 +1,19 @@ +#include <sys/types.h> +#include <stdio.h> +#include <string.h> +#include <signal.h> + +/* + * Returns a file name corresponding to a running pid + */ +char * pid2name(pid_t pid) +{ + char proc_exe[1024]; + + if (!kill(pid, 0)) { + sprintf(proc_exe, "/proc/%d/exe", pid); + return strdup(proc_exe); + } else { + return NULL; + } +} diff --git a/sysdeps/Linux/trace.c b/sysdeps/Linux/trace.c index 33bf244..1068a84 100644 --- a/sysdeps/Linux/trace.c +++ b/sysdeps/Linux/trace.c @@ -1,4 +1,6 @@ #include <stdio.h> +#include <string.h> +#include <errno.h> #include <sys/types.h> #include <sys/ptrace.h> #include <asm/unistd.h> @@ -22,6 +24,13 @@ void trace_me(void) } } +void trace_pid(pid_t pid) +{ + if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) { + perror("PTRACE_ATTACH"); + } +} + void continue_after_signal(pid_t pid, int signum) { /* We should always trace syscalls to be able to control fork(), clone(), execve()... */ |