| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| Juan Cespedes | 504a385 | 2003-02-04 23:24:38 +0100 | [diff] [blame] | 2 | #include <stdlib.h> |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 3 | #include <string.h> |
| 4 | #include <errno.h> |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 5 | #include <unistd.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 6 | #include <sys/types.h> |
| Petr Machata | 89a5360 | 2007-01-25 18:05:44 +0100 | [diff] [blame] | 7 | #include <sys/wait.h> |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 8 | #include "ptrace.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 9 | #include <asm/unistd.h> |
| 10 | |
| 11 | #include "ltrace.h" |
| 12 | #include "options.h" |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 13 | #include "sysdep.h" |
| 14 | |
| 15 | static int fork_exec_syscalls[][5] = { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 16 | { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 17 | #ifdef __NR_fork |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 18 | __NR_fork, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 19 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 20 | -1, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 21 | #endif |
| 22 | #ifdef __NR_clone |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 23 | __NR_clone, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 24 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 25 | -1, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 26 | #endif |
| 27 | #ifdef __NR_clone2 |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 28 | __NR_clone2, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 29 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 30 | -1, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 31 | #endif |
| 32 | #ifdef __NR_vfork |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 33 | __NR_vfork, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 34 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 35 | -1, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 36 | #endif |
| 37 | #ifdef __NR_execve |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 38 | __NR_execve, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 39 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 40 | -1, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 41 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 42 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 43 | #ifdef FORK_EXEC_SYSCALLS |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 44 | FORK_EXEC_SYSCALLS |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 45 | #endif |
| 46 | }; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 47 | |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 48 | /* Returns 1 if the sysnum may make a new child to be created |
| 49 | * (ie, with fork() or clone()) |
| 50 | * Returns 0 otherwise. |
| 51 | */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 52 | int fork_p(struct process *proc, int sysnum) |
| 53 | { |
| Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 54 | unsigned int i; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 55 | if (proc->personality |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 56 | >= sizeof fork_exec_syscalls / sizeof(fork_exec_syscalls[0])) |
| 57 | return 0; |
| 58 | for (i = 0; i < sizeof(fork_exec_syscalls[0]) / sizeof(int) - 1; ++i) |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 59 | if (sysnum == fork_exec_syscalls[proc->personality][i]) |
| 60 | return 1; |
| 61 | return 0; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 64 | /* Returns 1 if the sysnum may make the process exec other program |
| 65 | */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 66 | int exec_p(struct process *proc, int sysnum) |
| 67 | { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 68 | int i; |
| 69 | if (proc->personality |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 70 | >= sizeof fork_exec_syscalls / sizeof(fork_exec_syscalls[0])) |
| 71 | return 0; |
| 72 | i = sizeof(fork_exec_syscalls[0]) / sizeof(int) - 1; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 73 | if (sysnum == fork_exec_syscalls[proc->personality][i]) |
| 74 | return 1; |
| 75 | return 0; |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 78 | void trace_me(void) |
| 79 | { |
| 80 | if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 81 | perror("PTRACE_TRACEME"); |
| 82 | exit(1); |
| 83 | } |
| 84 | } |
| 85 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 86 | int trace_pid(pid_t pid) |
| 87 | { |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 88 | if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) { |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 89 | return -1; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 90 | } |
| Petr Machata | 89a5360 | 2007-01-25 18:05:44 +0100 | [diff] [blame] | 91 | |
| 92 | /* man ptrace: PTRACE_ATTACH attaches to the process specified |
| 93 | in pid. The child is sent a SIGSTOP, but will not |
| 94 | necessarily have stopped by the completion of this call; |
| 95 | use wait() to wait for the child to stop. */ |
| 96 | if (waitpid (pid, NULL, 0) != pid) { |
| 97 | perror ("trace_pid: waitpid"); |
| 98 | exit (1); |
| 99 | } |
| 100 | |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 104 | void trace_set_options(struct process *proc, pid_t pid) |
| 105 | { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 106 | #ifndef PTRACE_SETOPTIONS |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 107 | #define PTRACE_SETOPTIONS 0x4200 |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 108 | #endif |
| 109 | #ifndef PTRACE_OLDSETOPTIONS |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 110 | #define PTRACE_OLDSETOPTIONS 21 |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 111 | #endif |
| 112 | #ifndef PTRACE_O_TRACESYSGOOD |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 113 | #define PTRACE_O_TRACESYSGOOD 0x00000001 |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 114 | #endif |
| 115 | if (proc->tracesysgood & 0x80) |
| 116 | return; |
| 117 | if (ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACESYSGOOD) < 0 && |
| 118 | ptrace(PTRACE_OLDSETOPTIONS, pid, 0, PTRACE_O_TRACESYSGOOD) < 0) { |
| 119 | perror("PTRACE_SETOPTIONS"); |
| 120 | return; |
| 121 | } |
| 122 | proc->tracesysgood |= 0x80; |
| 123 | } |
| 124 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 125 | void untrace_pid(pid_t pid) |
| 126 | { |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 127 | ptrace(PTRACE_DETACH, pid, 1, 0); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 130 | void continue_after_signal(pid_t pid, int signum) |
| 131 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 132 | /* 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] | 133 | ptrace(PTRACE_SYSCALL, pid, 0, signum); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 136 | void continue_process(pid_t pid) |
| 137 | { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 138 | continue_after_signal(pid, 0); |
| 139 | } |
| 140 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 141 | void continue_enabling_breakpoint(pid_t pid, struct breakpoint *sbp) |
| 142 | { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 143 | enable_breakpoint(pid, sbp); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 144 | continue_process(pid); |
| 145 | } |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 146 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 147 | void continue_after_breakpoint(struct process *proc, struct breakpoint *sbp) |
| 148 | { |
| 149 | if (sbp->enabled) |
| 150 | disable_breakpoint(proc->pid, sbp); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 151 | set_instruction_pointer(proc, sbp->addr); |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 152 | if (sbp->enabled == 0) { |
| 153 | continue_process(proc->pid); |
| 154 | } else { |
| 155 | proc->breakpoint_being_enabled = sbp; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 156 | #if defined __sparc__ || defined __ia64___ |
| 157 | /* we don't want to singlestep here */ |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 158 | continue_process(proc->pid); |
| 159 | #else |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 160 | ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 161 | #endif |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| Steve Fink | 7bafff0 | 2006-08-07 04:50:42 +0200 | [diff] [blame] | 165 | /* Read a single long from the process's memory address 'addr' */ |
| 166 | int umovelong(struct process *proc, void *addr, long *result) |
| 167 | { |
| 168 | long pointed_to; |
| 169 | |
| 170 | errno = 0; |
| 171 | pointed_to = ptrace(PTRACE_PEEKTEXT, proc->pid, addr, 0); |
| 172 | if (pointed_to == -1 && errno) |
| 173 | return -errno; |
| 174 | |
| 175 | *result = pointed_to; |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | /* Read a series of bytes starting at the process's memory address |
| 180 | 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes |
| 181 | have been read. |
| 182 | */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 183 | int umovestr(struct process *proc, void *addr, int len, void *laddr) |
| 184 | { |
| 185 | union { |
| 186 | long a; |
| 187 | char c[sizeof(long)]; |
| 188 | } a; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 189 | int i; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 190 | int offset = 0; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 191 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 192 | while (offset < len) { |
| 193 | a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0); |
| 194 | for (i = 0; i < sizeof(long); i++) { |
| Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 195 | if (a.c[i] && offset + (signed)i < len) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 196 | *(char *)(laddr + offset + i) = a.c[i]; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 197 | } else { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 198 | *(char *)(laddr + offset + i) = '\0'; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | } |
| 202 | offset += sizeof(long); |
| 203 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 204 | *(char *)(laddr + offset) = '\0'; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 205 | return 0; |
| 206 | } |