| 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" |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 14 | #include "debug.h" |
| 15 | |
| 16 | /* If the system headers did not provide the constants, hard-code the normal |
| 17 | values. */ |
| 18 | #ifndef PTRACE_EVENT_FORK |
| 19 | |
| 20 | #define PTRACE_OLDSETOPTIONS 21 |
| 21 | #define PTRACE_SETOPTIONS 0x4200 |
| 22 | #define PTRACE_GETEVENTMSG 0x4201 |
| 23 | |
| 24 | /* options set using PTRACE_SETOPTIONS */ |
| 25 | #define PTRACE_O_TRACESYSGOOD 0x00000001 |
| 26 | #define PTRACE_O_TRACEFORK 0x00000002 |
| 27 | #define PTRACE_O_TRACEVFORK 0x00000004 |
| 28 | #define PTRACE_O_TRACECLONE 0x00000008 |
| 29 | #define PTRACE_O_TRACEEXEC 0x00000010 |
| 30 | #define PTRACE_O_TRACEVFORKDONE 0x00000020 |
| 31 | #define PTRACE_O_TRACEEXIT 0x00000040 |
| 32 | |
| 33 | /* Wait extended result codes for the above trace options. */ |
| 34 | #define PTRACE_EVENT_FORK 1 |
| 35 | #define PTRACE_EVENT_VFORK 2 |
| 36 | #define PTRACE_EVENT_CLONE 3 |
| 37 | #define PTRACE_EVENT_EXEC 4 |
| 38 | #define PTRACE_EVENT_VFORK_DONE 5 |
| 39 | #define PTRACE_EVENT_EXIT 6 |
| 40 | |
| 41 | #endif /* PTRACE_EVENT_FORK */ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 42 | |
| 43 | static int fork_exec_syscalls[][5] = { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 44 | { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 45 | #ifdef __NR_fork |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 46 | __NR_fork, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 47 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 48 | -1, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 49 | #endif |
| 50 | #ifdef __NR_clone |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 51 | __NR_clone, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 52 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 53 | -1, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 54 | #endif |
| 55 | #ifdef __NR_clone2 |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 56 | __NR_clone2, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 57 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 58 | -1, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 59 | #endif |
| 60 | #ifdef __NR_vfork |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 61 | __NR_vfork, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 62 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 63 | -1, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 64 | #endif |
| 65 | #ifdef __NR_execve |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 66 | __NR_execve, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 67 | #else |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 68 | -1, |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 69 | #endif |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 70 | } |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 71 | #ifdef FORK_EXEC_SYSCALLS |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 72 | FORK_EXEC_SYSCALLS |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 73 | #endif |
| 74 | }; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 75 | |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 76 | #ifdef ARCH_HAVE_UMOVELONG |
| 77 | extern int arch_umovelong (struct process *, void *, long *, arg_type_info *); |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 78 | int |
| 79 | umovelong (struct process *proc, void *addr, long *result, arg_type_info *info) { |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 80 | return arch_umovelong (proc, addr, result, info); |
| 81 | } |
| 82 | #else |
| 83 | /* Read a single long from the process's memory address 'addr' */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 84 | int |
| 85 | umovelong (struct process *proc, void *addr, long *result, arg_type_info *info) { |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 86 | long pointed_to; |
| 87 | |
| 88 | errno = 0; |
| 89 | pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0); |
| 90 | if (pointed_to == -1 && errno) |
| 91 | return -errno; |
| 92 | |
| 93 | *result = pointed_to; |
| 94 | return 0; |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 99 | /* Returns 1 if the sysnum may make a new child to be created |
| 100 | * (ie, with fork() or clone()) |
| 101 | * Returns 0 otherwise. |
| 102 | */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 103 | int |
| 104 | fork_p(struct process *proc, int sysnum) { |
| Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 105 | unsigned int i; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 106 | if (proc->personality |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 107 | >= sizeof fork_exec_syscalls / sizeof(fork_exec_syscalls[0])) |
| 108 | return 0; |
| 109 | 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] | 110 | if (sysnum == fork_exec_syscalls[proc->personality][i]) |
| 111 | return 1; |
| 112 | return 0; |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 115 | /* Returns 1 if the sysnum may make the process exec other program |
| 116 | */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 117 | int |
| 118 | exec_p(struct process *proc, int sysnum) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 119 | int i; |
| 120 | if (proc->personality |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 121 | >= sizeof fork_exec_syscalls / sizeof(fork_exec_syscalls[0])) |
| 122 | return 0; |
| 123 | i = sizeof(fork_exec_syscalls[0]) / sizeof(int) - 1; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 124 | if (sysnum == fork_exec_syscalls[proc->personality][i]) |
| 125 | return 1; |
| 126 | return 0; |
| Juan Cespedes | 81690ef | 1998-03-13 19:31:29 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 129 | /* Check that we just hit an exec. |
| 130 | */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 131 | int |
| 132 | was_exec(struct process *proc, int status) { |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 133 | if (!WIFSTOPPED (status)) |
| 134 | return 0; |
| 135 | |
| 136 | if (WSTOPSIG (status) == SIGTRAP |
| 137 | && (status >> 16) == PTRACE_EVENT_EXEC) { |
| 138 | debug (1, "detected exec (PTRACE_EVENT_EXEC)"); |
| 139 | return 1; |
| 140 | } |
| 141 | |
| 142 | if (WSTOPSIG (status) == SIGTRAP |
| 143 | && proc->callstack_depth > 0) { |
| 144 | /* Check whether this SIGTRAP is received just after |
| 145 | execve is called for this process. Ideally we'd |
| 146 | like to check that the exec succeeded, but e.g. on |
| 147 | s390 we have no way of knowing, because return |
| 148 | value is not set to -1 (as it should). Never mind, |
| 149 | reseting breakpoints for current process doesn't |
| 150 | hurt. */ |
| 151 | struct callstack_element *elem; |
| 152 | elem = &proc->callstack[proc->callstack_depth - 1]; |
| 153 | if (elem && elem->is_syscall && exec_p(proc, elem->c_un.syscall)) { |
| 154 | debug (1, "detected exec (callstack)"); |
| 155 | return 1; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 162 | void |
| 163 | trace_me(void) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 164 | if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 165 | perror("PTRACE_TRACEME"); |
| 166 | exit(1); |
| 167 | } |
| 168 | } |
| 169 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 170 | int |
| 171 | trace_pid(pid_t pid) { |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 172 | if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) { |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 173 | return -1; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 174 | } |
| Petr Machata | 89a5360 | 2007-01-25 18:05:44 +0100 | [diff] [blame] | 175 | |
| Juan Cespedes | 714ee9d | 2009-04-07 13:28:54 +0200 | [diff] [blame^] | 176 | /* man ptrace: PTRACE_ATTACH attaches to the process specified |
| 177 | in pid. The child is sent a SIGSTOP, but will not |
| 178 | necessarily have stopped by the completion of this call; |
| 179 | use wait() to wait for the child to stop. */ |
| 180 | if (waitpid (pid, NULL, 0) != pid) { |
| 181 | perror ("trace_pid: waitpid"); |
| 182 | exit (1); |
| 183 | } |
| 184 | |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 188 | void |
| 189 | trace_set_options(struct process *proc, pid_t pid) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 190 | if (proc->tracesysgood & 0x80) |
| 191 | return; |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 192 | |
| 193 | long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEEXEC; |
| 194 | if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 && |
| 195 | ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 196 | perror("PTRACE_SETOPTIONS"); |
| 197 | return; |
| 198 | } |
| 199 | proc->tracesysgood |= 0x80; |
| 200 | } |
| 201 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 202 | void |
| 203 | untrace_pid(pid_t pid) { |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 204 | ptrace(PTRACE_DETACH, pid, 1, 0); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 207 | void |
| 208 | continue_after_signal(pid_t pid, int signum) { |
| Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 209 | struct process *proc; |
| 210 | |
| 211 | proc = pid2proc(pid); |
| 212 | if (proc && proc->breakpoint_being_enabled) { |
| 213 | #if defined __sparc__ || defined __ia64___ |
| 214 | ptrace(PTRACE_SYSCALL, pid, 0, signum); |
| 215 | #else |
| 216 | ptrace(PTRACE_SINGLESTEP, pid, 0, signum); |
| 217 | #endif |
| 218 | } else { |
| 219 | ptrace(PTRACE_SYSCALL, pid, 0, signum); |
| 220 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 221 | } |
| 222 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 223 | void |
| 224 | continue_process(pid_t pid) { |
| Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 225 | /* We always trace syscalls to control fork(), clone(), execve()... */ |
| 226 | |
| 227 | ptrace(PTRACE_SYSCALL, pid, 0, 0); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 230 | void |
| 231 | continue_enabling_breakpoint(pid_t pid, struct breakpoint *sbp) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 232 | enable_breakpoint(pid, sbp); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 233 | continue_process(pid); |
| 234 | } |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 235 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 236 | void |
| 237 | continue_after_breakpoint(struct process *proc, struct breakpoint *sbp) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 238 | if (sbp->enabled) |
| 239 | disable_breakpoint(proc->pid, sbp); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 240 | set_instruction_pointer(proc, sbp->addr); |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 241 | if (sbp->enabled == 0) { |
| 242 | continue_process(proc->pid); |
| 243 | } else { |
| 244 | proc->breakpoint_being_enabled = sbp; |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 245 | #if defined __sparc__ || defined __ia64___ |
| 246 | /* we don't want to singlestep here */ |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 247 | continue_process(proc->pid); |
| 248 | #else |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 249 | ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 250 | #endif |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| Steve Fink | 7bafff0 | 2006-08-07 04:50:42 +0200 | [diff] [blame] | 254 | /* Read a series of bytes starting at the process's memory address |
| 255 | 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes |
| 256 | have been read. |
| 257 | */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 258 | int |
| 259 | umovestr(struct process *proc, void *addr, int len, void *laddr) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 260 | union { |
| 261 | long a; |
| 262 | char c[sizeof(long)]; |
| 263 | } a; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 264 | int i; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 265 | int offset = 0; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 266 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 267 | while (offset < len) { |
| 268 | a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0); |
| 269 | for (i = 0; i < sizeof(long); i++) { |
| Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 270 | if (a.c[i] && offset + (signed)i < len) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 271 | *(char *)(laddr + offset + i) = a.c[i]; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 272 | } else { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 273 | *(char *)(laddr + offset + i) = '\0'; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | } |
| 277 | offset += sizeof(long); |
| 278 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 279 | *(char *)(laddr + offset) = '\0'; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 280 | return 0; |
| 281 | } |