| 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 | |
| Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 11 | #include "common.h" |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 12 | |
| 13 | /* If the system headers did not provide the constants, hard-code the normal |
| 14 | values. */ |
| 15 | #ifndef PTRACE_EVENT_FORK |
| 16 | |
| 17 | #define PTRACE_OLDSETOPTIONS 21 |
| 18 | #define PTRACE_SETOPTIONS 0x4200 |
| 19 | #define PTRACE_GETEVENTMSG 0x4201 |
| 20 | |
| 21 | /* options set using PTRACE_SETOPTIONS */ |
| 22 | #define PTRACE_O_TRACESYSGOOD 0x00000001 |
| 23 | #define PTRACE_O_TRACEFORK 0x00000002 |
| 24 | #define PTRACE_O_TRACEVFORK 0x00000004 |
| 25 | #define PTRACE_O_TRACECLONE 0x00000008 |
| 26 | #define PTRACE_O_TRACEEXEC 0x00000010 |
| 27 | #define PTRACE_O_TRACEVFORKDONE 0x00000020 |
| 28 | #define PTRACE_O_TRACEEXIT 0x00000040 |
| 29 | |
| 30 | /* Wait extended result codes for the above trace options. */ |
| 31 | #define PTRACE_EVENT_FORK 1 |
| 32 | #define PTRACE_EVENT_VFORK 2 |
| 33 | #define PTRACE_EVENT_CLONE 3 |
| 34 | #define PTRACE_EVENT_EXEC 4 |
| 35 | #define PTRACE_EVENT_VFORK_DONE 5 |
| 36 | #define PTRACE_EVENT_EXIT 6 |
| 37 | |
| 38 | #endif /* PTRACE_EVENT_FORK */ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 39 | |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 40 | #ifdef ARCH_HAVE_UMOVELONG |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 41 | extern int arch_umovelong (Process *, void *, long *, arg_type_info *); |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 42 | int |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 43 | umovelong (Process *proc, void *addr, long *result, arg_type_info *info) { |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 44 | return arch_umovelong (proc, addr, result, info); |
| 45 | } |
| 46 | #else |
| 47 | /* Read a single long from the process's memory address 'addr' */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 48 | int |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 49 | umovelong (Process *proc, void *addr, long *result, arg_type_info *info) { |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 50 | long pointed_to; |
| 51 | |
| 52 | errno = 0; |
| 53 | pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0); |
| 54 | if (pointed_to == -1 && errno) |
| 55 | return -errno; |
| 56 | |
| 57 | *result = pointed_to; |
| Arnaud Patard | f16fcff | 2010-01-08 08:40:19 -0500 | [diff] [blame] | 58 | if (info) { |
| 59 | switch(info->type) { |
| 60 | case ARGTYPE_INT: |
| 61 | *result &= 0x00000000ffffffffUL; |
| 62 | default: |
| 63 | break; |
| 64 | }; |
| 65 | } |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | #endif |
| 69 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 70 | void |
| 71 | trace_me(void) { |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame^] | 72 | debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid()); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 73 | if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 74 | perror("PTRACE_TRACEME"); |
| 75 | exit(1); |
| 76 | } |
| 77 | } |
| 78 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 79 | int |
| 80 | trace_pid(pid_t pid) { |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame^] | 81 | debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 82 | if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) { |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 83 | return -1; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 84 | } |
| Petr Machata | 89a5360 | 2007-01-25 18:05:44 +0100 | [diff] [blame] | 85 | |
| Juan Cespedes | 714ee9d | 2009-04-07 13:28:54 +0200 | [diff] [blame] | 86 | /* man ptrace: PTRACE_ATTACH attaches to the process specified |
| 87 | in pid. The child is sent a SIGSTOP, but will not |
| 88 | necessarily have stopped by the completion of this call; |
| 89 | use wait() to wait for the child to stop. */ |
| 90 | if (waitpid (pid, NULL, 0) != pid) { |
| 91 | perror ("trace_pid: waitpid"); |
| 92 | exit (1); |
| 93 | } |
| 94 | |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 98 | void |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 99 | trace_set_options(Process *proc, pid_t pid) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 100 | if (proc->tracesysgood & 0x80) |
| 101 | return; |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 102 | |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame^] | 103 | debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 104 | |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 105 | long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK | |
| 106 | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE | |
| 107 | PTRACE_O_TRACEEXEC; |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 108 | if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 && |
| 109 | ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 110 | perror("PTRACE_SETOPTIONS"); |
| 111 | return; |
| 112 | } |
| 113 | proc->tracesysgood |= 0x80; |
| 114 | } |
| 115 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 116 | void |
| 117 | untrace_pid(pid_t pid) { |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame^] | 118 | debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid); |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 119 | ptrace(PTRACE_DETACH, pid, 1, 0); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 122 | void |
| 123 | continue_after_signal(pid_t pid, int signum) { |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 124 | Process *proc; |
| Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 125 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 126 | debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum); |
| 127 | |
| Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 128 | proc = pid2proc(pid); |
| 129 | if (proc && proc->breakpoint_being_enabled) { |
| Arnaud Patard | f3d1c53 | 2010-01-08 08:40:04 -0500 | [diff] [blame] | 130 | #if defined __sparc__ || defined __ia64___ || defined __mips__ |
| Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 131 | ptrace(PTRACE_SYSCALL, pid, 0, signum); |
| 132 | #else |
| 133 | ptrace(PTRACE_SINGLESTEP, pid, 0, signum); |
| 134 | #endif |
| 135 | } else { |
| 136 | ptrace(PTRACE_SYSCALL, pid, 0, signum); |
| 137 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 140 | void |
| 141 | continue_process(pid_t pid) { |
| Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 142 | /* We always trace syscalls to control fork(), clone(), execve()... */ |
| 143 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 144 | debug(DEBUG_PROCESS, "continue_process: pid=%d", pid); |
| 145 | |
| Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 146 | ptrace(PTRACE_SYSCALL, pid, 0, 0); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 149 | void |
| Juan Cespedes | 1dec217 | 2009-05-07 10:12:10 +0200 | [diff] [blame] | 150 | continue_enabling_breakpoint(pid_t pid, Breakpoint *sbp) { |
| Juan Cespedes | 5b3ffdf | 2001-07-02 00:52:45 +0200 | [diff] [blame] | 151 | enable_breakpoint(pid, sbp); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 152 | continue_process(pid); |
| 153 | } |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 154 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 155 | void |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame^] | 156 | continue_after_breakpoint(Process *proc, Breakpoint *sbp) |
| 157 | { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 158 | if (sbp->enabled) |
| 159 | disable_breakpoint(proc->pid, sbp); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 160 | set_instruction_pointer(proc, sbp->addr); |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 161 | if (sbp->enabled == 0) { |
| 162 | continue_process(proc->pid); |
| 163 | } else { |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame^] | 164 | debug(DEBUG_PROCESS, |
| 165 | "continue_after_breakpoint: pid=%d, addr=%p", |
| 166 | proc->pid, sbp->addr); |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 167 | proc->breakpoint_being_enabled = sbp; |
| Arnaud Patard | f3d1c53 | 2010-01-08 08:40:04 -0500 | [diff] [blame] | 168 | #if defined __sparc__ || defined __ia64___ || defined __mips__ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 169 | /* we don't want to singlestep here */ |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 170 | continue_process(proc->pid); |
| 171 | #else |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 172 | ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 173 | #endif |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| Joe Damato | dfa3fa3 | 2010-11-08 15:47:35 -0800 | [diff] [blame] | 177 | size_t |
| 178 | umovebytes(Process *proc, void *addr, void *laddr, size_t len) { |
| 179 | |
| 180 | union { |
| 181 | long a; |
| 182 | char c[sizeof(long)]; |
| 183 | } a; |
| Zachary T Welch | ba6aca2 | 2010-12-08 18:55:09 -0800 | [diff] [blame] | 184 | int started = 0; |
| 185 | size_t offset = 0, bytes_read = 0; |
| Joe Damato | dfa3fa3 | 2010-11-08 15:47:35 -0800 | [diff] [blame] | 186 | |
| 187 | while (offset < len) { |
| 188 | a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0); |
| 189 | if (a.a == -1 && errno) { |
| 190 | if (started && errno == EIO) |
| 191 | return bytes_read; |
| 192 | else |
| 193 | return -1; |
| 194 | } |
| 195 | started = 1; |
| 196 | |
| 197 | if (len - offset >= sizeof(long)) { |
| 198 | memcpy(laddr + offset, &a.c[0], sizeof(long)); |
| 199 | bytes_read += sizeof(long); |
| 200 | } |
| 201 | else { |
| 202 | memcpy(laddr + offset, &a.c[0], len - offset); |
| 203 | bytes_read += (len - offset); |
| 204 | } |
| 205 | offset += sizeof(long); |
| 206 | } |
| 207 | |
| 208 | return bytes_read; |
| 209 | } |
| 210 | |
| Steve Fink | 7bafff0 | 2006-08-07 04:50:42 +0200 | [diff] [blame] | 211 | /* Read a series of bytes starting at the process's memory address |
| 212 | 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes |
| 213 | have been read. |
| 214 | */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 215 | int |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 216 | umovestr(Process *proc, void *addr, int len, void *laddr) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 217 | union { |
| 218 | long a; |
| 219 | char c[sizeof(long)]; |
| 220 | } a; |
| Zachary T Welch | ba6aca2 | 2010-12-08 18:55:09 -0800 | [diff] [blame] | 221 | unsigned i; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 222 | int offset = 0; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 223 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 224 | while (offset < len) { |
| 225 | a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0); |
| 226 | for (i = 0; i < sizeof(long); i++) { |
| Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 227 | if (a.c[i] && offset + (signed)i < len) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 228 | *(char *)(laddr + offset + i) = a.c[i]; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 229 | } else { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 230 | *(char *)(laddr + offset + i) = '\0'; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | } |
| 234 | offset += sizeof(long); |
| 235 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 236 | *(char *)(laddr + offset) = '\0'; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 237 | return 0; |
| 238 | } |