Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 2 | #include <string.h> |
| 3 | #include <errno.h> |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 4 | #include <sys/types.h> |
| 5 | #include <sys/ptrace.h> |
| 6 | #include <asm/unistd.h> |
| 7 | |
| 8 | #include "ltrace.h" |
| 9 | #include "options.h" |
| 10 | |
Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 11 | /* Returns 1 if the sysnum may make a new child to be created |
| 12 | * (ie, with fork() or clone()) |
| 13 | * Returns 0 otherwise. |
| 14 | */ |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 15 | int |
| 16 | fork_p(int sysnum) { |
Juan Cespedes | e3eb9aa | 1999-04-03 03:21:52 +0200 | [diff] [blame] | 17 | return 0 |
| 18 | #if defined(__NR_fork) |
| 19 | || (sysnum == __NR_fork) |
| 20 | #endif |
| 21 | #if defined(__NR_clone) |
| 22 | || (sysnum == __NR_clone) |
| 23 | #endif |
| 24 | #if defined(__NR_vfork) |
| 25 | || (sysnum == __NR_vfork) |
| 26 | #endif |
| 27 | ; |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 28 | } |
| 29 | |
Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 30 | /* Returns 1 if the sysnum may make the process exec other program |
| 31 | */ |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 32 | int |
| 33 | exec_p(int sysnum) { |
Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 34 | return (sysnum == __NR_execve); |
| 35 | } |
| 36 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 37 | void |
| 38 | trace_me(void) { |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 39 | if (ptrace(PTRACE_TRACEME, 0, 1, 0)<0) { |
| 40 | perror("PTRACE_TRACEME"); |
| 41 | exit(1); |
| 42 | } |
| 43 | } |
| 44 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 45 | int |
| 46 | trace_pid(pid_t pid) { |
Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 47 | if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) { |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 48 | return -1; |
Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 49 | } |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 50 | return 0; |
| 51 | } |
| 52 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 53 | void |
| 54 | untrace_pid(pid_t pid) { |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 55 | ptrace(PTRACE_DETACH, pid, 1, 0); |
Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 58 | void |
| 59 | continue_after_signal(pid_t pid, int signum) { |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 60 | /* We should always trace syscalls to be able to control fork(), clone(), execve()... */ |
Juan Cespedes | 5916fda | 2002-02-25 00:19:21 +0100 | [diff] [blame] | 61 | ptrace(PTRACE_SYSCALL, pid, 0, signum); |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 62 | } |
| 63 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 64 | void |
| 65 | continue_process(pid_t pid) { |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 66 | continue_after_signal(pid, 0); |
| 67 | } |
| 68 | |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 69 | void |
| 70 | continue_enabling_breakpoint(pid_t pid, struct breakpoint * sbp) { |
Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 71 | enable_breakpoint(pid, sbp); |
Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 72 | continue_process(pid); |
| 73 | } |
Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame^] | 74 | |
| 75 | int |
| 76 | umovestr(struct process * proc, void * addr, int len, void * laddr) { |
| 77 | long a; |
| 78 | int i; |
| 79 | int offset=0; |
| 80 | |
| 81 | while(offset<len) { |
| 82 | a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr+offset, 0); |
| 83 | for(i=0; i<sizeof(long); i++) { |
| 84 | if (((char*)&a)[i] && offset+i < len) { |
| 85 | *(char *)(laddr+offset+i) = ((char*)&a)[i]; |
| 86 | } else { |
| 87 | *(char *)(laddr+offset+i) = '\0'; |
| 88 | return 0; |
| 89 | } |
| 90 | } |
| 91 | offset += sizeof(long); |
| 92 | } |
| 93 | *(char *)(laddr+offset) = '\0'; |
| 94 | return 0; |
| 95 | } |