Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl> |
| 3 | * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl> |
| 4 | * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com> |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 5 | * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl> |
| 6 | * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation |
| 7 | * Linux for s390 port by D.J. Barrow |
| 8 | * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * 1. Redistributions of source code must retain the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer. |
| 16 | * 2. Redistributions in binary form must reproduce the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer in the |
| 18 | * documentation and/or other materials provided with the distribution. |
| 19 | * 3. The name of the author may not be used to endorse or promote products |
| 20 | * derived from this software without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 24 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 25 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 27 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 31 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | * $Id$ |
| 34 | */ |
| 35 | |
| 36 | #include "defs.h" |
| 37 | |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 38 | #include <signal.h> |
| 39 | #include <sys/syscall.h> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 40 | #include <sys/user.h> |
| 41 | #include <sys/param.h> |
| 42 | #include <fcntl.h> |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 43 | #if HAVE_SYS_UIO_H |
| 44 | #include <sys/uio.h> |
| 45 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 46 | #ifdef SUNOS4 |
| 47 | #include <machine/reg.h> |
| 48 | #include <a.out.h> |
| 49 | #include <link.h> |
| 50 | #endif /* SUNOS4 */ |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 51 | |
Wichert Akkerman | 43a7482 | 2000-06-27 17:33:32 +0000 | [diff] [blame] | 52 | #if defined(linux) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 1)) |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 53 | #include <linux/ptrace.h> |
Roland McGrath | 1e85cf9 | 2002-12-16 20:40:54 +0000 | [diff] [blame] | 54 | #endif |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 55 | |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 56 | #if defined(LINUX) && defined(IA64) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 57 | # include <asm/ptrace_offsets.h> |
| 58 | # include <asm/rse.h> |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 61 | #ifdef HAVE_SYS_REG_H |
| 62 | #include <sys/reg.h> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 63 | # define PTRACE_PEEKUSR PTRACE_PEEKUSER |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 64 | #elif defined(HAVE_LINUX_PTRACE_H) |
| 65 | #undef PTRACE_SYSCALL |
Roland McGrath | ce9f074 | 2004-03-01 21:29:22 +0000 | [diff] [blame] | 66 | # ifdef HAVE_STRUCT_IA64_FPREG |
| 67 | # define ia64_fpreg XXX_ia64_fpreg |
| 68 | # endif |
| 69 | # ifdef HAVE_STRUCT_PT_ALL_USER_REGS |
| 70 | # define pt_all_user_regs XXX_pt_all_user_regs |
| 71 | # endif |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 72 | #include <linux/ptrace.h> |
Roland McGrath | ce9f074 | 2004-03-01 21:29:22 +0000 | [diff] [blame] | 73 | # undef ia64_fpreg |
| 74 | # undef pt_all_user_regs |
Wichert Akkerman | 2e2553a | 1999-05-09 00:29:58 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 77 | #ifdef SUNOS4_KERNEL_ARCH_KLUDGE |
| 78 | #include <sys/utsname.h> |
| 79 | #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */ |
| 80 | |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 81 | #if defined(LINUXSPARC) && defined (SPARC64) |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 82 | # undef PTRACE_GETREGS |
| 83 | # define PTRACE_GETREGS PTRACE_GETREGS64 |
| 84 | # undef PTRACE_SETREGS |
| 85 | # define PTRACE_SETREGS PTRACE_SETREGS64 |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 86 | #endif |
| 87 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 88 | /* macros */ |
| 89 | #ifndef MAX |
| 90 | #define MAX(a,b) (((a) > (b)) ? (a) : (b)) |
| 91 | #endif |
| 92 | #ifndef MIN |
| 93 | #define MIN(a,b) (((a) < (b)) ? (a) : (b)) |
| 94 | #endif |
| 95 | |
Roland McGrath | a4d4853 | 2005-06-08 20:45:28 +0000 | [diff] [blame] | 96 | #if 0 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 97 | void |
| 98 | tv_tv(tv, a, b) |
| 99 | struct timeval *tv; |
| 100 | int a; |
| 101 | int b; |
| 102 | { |
| 103 | tv->tv_sec = a; |
| 104 | tv->tv_usec = b; |
| 105 | } |
Roland McGrath | a4d4853 | 2005-06-08 20:45:28 +0000 | [diff] [blame] | 106 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 107 | |
| 108 | int |
| 109 | tv_nz(a) |
| 110 | struct timeval *a; |
| 111 | { |
| 112 | return a->tv_sec || a->tv_usec; |
| 113 | } |
| 114 | |
| 115 | int |
| 116 | tv_cmp(a, b) |
| 117 | struct timeval *a, *b; |
| 118 | { |
| 119 | if (a->tv_sec < b->tv_sec |
| 120 | || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) |
| 121 | return -1; |
| 122 | if (a->tv_sec > b->tv_sec |
| 123 | || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec)) |
| 124 | return 1; |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | double |
| 129 | tv_float(tv) |
| 130 | struct timeval *tv; |
| 131 | { |
| 132 | return tv->tv_sec + tv->tv_usec/1000000.0; |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | tv_add(tv, a, b) |
| 137 | struct timeval *tv, *a, *b; |
| 138 | { |
| 139 | tv->tv_sec = a->tv_sec + b->tv_sec; |
| 140 | tv->tv_usec = a->tv_usec + b->tv_usec; |
Roland McGrath | 58372f5 | 2007-07-24 01:38:22 +0000 | [diff] [blame] | 141 | if (tv->tv_usec >= 1000000) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 142 | tv->tv_sec++; |
| 143 | tv->tv_usec -= 1000000; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | tv_sub(tv, a, b) |
| 149 | struct timeval *tv, *a, *b; |
| 150 | { |
| 151 | tv->tv_sec = a->tv_sec - b->tv_sec; |
| 152 | tv->tv_usec = a->tv_usec - b->tv_usec; |
| 153 | if (((long) tv->tv_usec) < 0) { |
| 154 | tv->tv_sec--; |
| 155 | tv->tv_usec += 1000000; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | tv_div(tv, a, n) |
| 161 | struct timeval *tv, *a; |
| 162 | int n; |
| 163 | { |
| 164 | tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n; |
| 165 | tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000; |
| 166 | tv->tv_usec %= 1000000; |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | tv_mul(tv, a, n) |
| 171 | struct timeval *tv, *a; |
| 172 | int n; |
| 173 | { |
| 174 | tv->tv_usec = a->tv_usec * n; |
Dmitry V. Levin | fefdd97 | 2007-06-29 21:25:56 +0000 | [diff] [blame] | 175 | tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 176 | tv->tv_usec %= 1000000; |
| 177 | } |
| 178 | |
Dmitry V. Levin | ab9008b | 2007-01-11 22:05:04 +0000 | [diff] [blame] | 179 | const char * |
| 180 | xlookup(const struct xlat *xlat, int val) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 181 | { |
| 182 | for (; xlat->str != NULL; xlat++) |
| 183 | if (xlat->val == val) |
| 184 | return xlat->str; |
| 185 | return NULL; |
| 186 | } |
| 187 | |
| 188 | /* |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 189 | * Generic ptrace wrapper which tracks ESRCH errors |
| 190 | * by setting tcp->ptrace_errno to ESRCH. |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 191 | * |
| 192 | * We assume that ESRCH indicates likely process death (SIGKILL?), |
| 193 | * modulo bugs where process somehow ended up not stopped. |
| 194 | * Unfortunately kernel uses ESRCH for that case too. Oh well. |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 195 | * |
| 196 | * Currently used by upeek() only. |
| 197 | * TODO: use this in all other ptrace() calls while decoding. |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 198 | */ |
Denys Vlasenko | 4dedd56 | 2009-02-24 15:17:53 +0000 | [diff] [blame] | 199 | long |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 200 | do_ptrace(int request, struct tcb *tcp, void *addr, void *data) |
Denys Vlasenko | 4dedd56 | 2009-02-24 15:17:53 +0000 | [diff] [blame] | 201 | { |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 202 | long l; |
| 203 | |
| 204 | errno = 0; |
Mike Frysinger | 09a13c2 | 2009-10-07 01:13:39 -0400 | [diff] [blame] | 205 | l = ptrace(request, tcp->pid, addr, (long) data); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 206 | /* Non-ESRCH errors might be our invalid reg/mem accesses, |
| 207 | * we do not record them. */ |
| 208 | if (errno == ESRCH) |
| 209 | tcp->ptrace_errno = ESRCH; |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 210 | return l; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Used when we want to unblock stopped traced process. |
| 215 | * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL. |
| 216 | * Returns 0 on success or if error was ESRCH |
| 217 | * (presumably process was killed while we talk to it). |
| 218 | * Otherwise prints error message and returns -1. |
| 219 | */ |
| 220 | int |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 221 | ptrace_restart(int op, struct tcb *tcp, int sig) |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 222 | { |
| 223 | int err; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 224 | const char *msg; |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 225 | |
| 226 | errno = 0; |
Mike Frysinger | 09a13c2 | 2009-10-07 01:13:39 -0400 | [diff] [blame] | 227 | ptrace(op, tcp->pid, (void *) 1, (long) sig); |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 228 | err = errno; |
| 229 | if (!err || err == ESRCH) |
| 230 | return 0; |
| 231 | |
| 232 | tcp->ptrace_errno = err; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 233 | msg = "SYSCALL"; |
| 234 | if (op == PTRACE_CONT) |
| 235 | msg = "CONT"; |
| 236 | if (op == PTRACE_DETACH) |
| 237 | msg = "DETACH"; |
| 238 | fprintf(stderr, "strace: ptrace(PTRACE_%s,1,%d): %s\n", |
| 239 | msg, sig, strerror(err)); |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | /* |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 244 | * Print entry in struct xlat table, if there. |
| 245 | */ |
| 246 | void |
Dmitry V. Levin | ab9008b | 2007-01-11 22:05:04 +0000 | [diff] [blame] | 247 | printxval(const struct xlat *xlat, int val, const char *dflt) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 248 | { |
Dmitry V. Levin | ab9008b | 2007-01-11 22:05:04 +0000 | [diff] [blame] | 249 | const char *str = xlookup(xlat, val); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 250 | |
| 251 | if (str) |
| 252 | tprintf("%s", str); |
| 253 | else |
| 254 | tprintf("%#x /* %s */", val, dflt); |
| 255 | } |
| 256 | |
Andreas Schwab | b5600fc | 2009-11-04 17:08:34 +0100 | [diff] [blame] | 257 | #if HAVE_LONG_LONG |
| 258 | /* |
| 259 | * Print 64bit argument at position llarg and return the index of the next |
| 260 | * argument. |
| 261 | */ |
| 262 | int |
| 263 | printllval(struct tcb *tcp, const char *format, int llarg) |
| 264 | { |
| 265 | # if defined(FREEBSD) \ |
| 266 | || (defined(LINUX) && defined(POWERPC) && !defined(__powerpc64__)) \ |
| 267 | || defined (LINUX_MIPSO32) |
| 268 | /* Align 64bit argument to 64bit boundary. */ |
| 269 | if (llarg % 2) llarg++; |
| 270 | # endif |
| 271 | # if defined LINUX && defined X86_64 |
| 272 | if (current_personality == 0) { |
| 273 | tprintf(format, tcp->u_arg[llarg]); |
| 274 | llarg++; |
| 275 | } else { |
| 276 | tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1])); |
| 277 | llarg += 2; |
| 278 | } |
| 279 | # elif defined IA64 || defined ALPHA || (defined POWERPC && defined __powerpc64__) |
| 280 | tprintf(format, tcp->u_arg[llarg]); |
| 281 | llarg++; |
| 282 | # elif defined LINUX_MIPSN32 |
| 283 | tprintf(format, tcp->ext_arg[llarg]); |
| 284 | llarg++; |
| 285 | # else |
| 286 | tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1])); |
| 287 | llarg += 2; |
| 288 | # endif |
| 289 | return llarg; |
| 290 | } |
| 291 | #endif |
| 292 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 293 | /* |
| 294 | * Interpret `xlat' as an array of flags |
| 295 | * print the entries whose bits are on in `flags' |
| 296 | * return # of flags printed. |
| 297 | */ |
| 298 | int |
| 299 | addflags(xlat, flags) |
Roland McGrath | d9f816f | 2004-09-04 03:39:20 +0000 | [diff] [blame] | 300 | const struct xlat *xlat; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 301 | int flags; |
| 302 | { |
| 303 | int n; |
| 304 | |
| 305 | for (n = 0; xlat->str; xlat++) { |
| 306 | if (xlat->val && (flags & xlat->val) == xlat->val) { |
| 307 | tprintf("|%s", xlat->str); |
| 308 | flags &= ~xlat->val; |
| 309 | n++; |
| 310 | } |
| 311 | } |
| 312 | if (flags) { |
| 313 | tprintf("|%#x", flags); |
| 314 | n++; |
| 315 | } |
| 316 | return n; |
| 317 | } |
| 318 | |
Roland McGrath | a6c0d8c | 2007-11-01 21:46:22 +0000 | [diff] [blame] | 319 | /* |
| 320 | * Interpret `xlat' as an array of flags/ |
| 321 | * Print to static string the entries whose bits are on in `flags' |
| 322 | * Return static string. |
| 323 | */ |
| 324 | const char * |
| 325 | sprintflags(const char *prefix, const struct xlat *xlat, int flags) |
| 326 | { |
| 327 | static char outstr[1024]; |
| 328 | int found = 0; |
| 329 | |
| 330 | strcpy(outstr, prefix); |
| 331 | |
| 332 | for (; xlat->str; xlat++) { |
| 333 | if ((flags & xlat->val) == xlat->val) { |
| 334 | if (found) |
| 335 | strcat(outstr, "|"); |
| 336 | strcat(outstr, xlat->str); |
| 337 | flags &= ~xlat->val; |
| 338 | found = 1; |
| 339 | } |
| 340 | } |
| 341 | if (flags) { |
| 342 | if (found) |
| 343 | strcat(outstr, "|"); |
| 344 | sprintf(outstr + strlen(outstr), "%#x", flags); |
| 345 | } |
| 346 | |
| 347 | return outstr; |
| 348 | } |
| 349 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 350 | int |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 351 | printflags(xlat, flags, dflt) |
Roland McGrath | d9f816f | 2004-09-04 03:39:20 +0000 | [diff] [blame] | 352 | const struct xlat *xlat; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 353 | int flags; |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 354 | const char *dflt; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 355 | { |
| 356 | int n; |
| 357 | char *sep; |
| 358 | |
| 359 | if (flags == 0 && xlat->val == 0) { |
| 360 | tprintf("%s", xlat->str); |
| 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | sep = ""; |
| 365 | for (n = 0; xlat->str; xlat++) { |
| 366 | if (xlat->val && (flags & xlat->val) == xlat->val) { |
| 367 | tprintf("%s%s", sep, xlat->str); |
| 368 | flags &= ~xlat->val; |
| 369 | sep = "|"; |
| 370 | n++; |
| 371 | } |
| 372 | } |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 373 | |
| 374 | if (n) { |
| 375 | if (flags) { |
| 376 | tprintf("%s%#x", sep, flags); |
| 377 | n++; |
| 378 | } |
| 379 | } else { |
| 380 | if (flags) { |
| 381 | tprintf("%#x", flags); |
| 382 | if (dflt) |
| 383 | tprintf(" /* %s */", dflt); |
| 384 | } else { |
| 385 | if (dflt) |
| 386 | tprintf("0"); |
| 387 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 388 | } |
Roland McGrath | b2dee13 | 2005-06-01 19:02:36 +0000 | [diff] [blame] | 389 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 390 | return n; |
| 391 | } |
| 392 | |
| 393 | void |
| 394 | printnum(tcp, addr, fmt) |
| 395 | struct tcb *tcp; |
| 396 | long addr; |
| 397 | char *fmt; |
| 398 | { |
Roland McGrath | eb28535 | 2003-01-14 09:59:00 +0000 | [diff] [blame] | 399 | long num; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 400 | |
| 401 | if (!addr) { |
| 402 | tprintf("NULL"); |
| 403 | return; |
| 404 | } |
| 405 | if (umove(tcp, addr, &num) < 0) { |
| 406 | tprintf("%#lx", addr); |
| 407 | return; |
| 408 | } |
| 409 | tprintf("["); |
| 410 | tprintf(fmt, num); |
| 411 | tprintf("]"); |
| 412 | } |
| 413 | |
Roland McGrath | 6bc1220 | 2003-11-13 22:32:27 +0000 | [diff] [blame] | 414 | void |
Roland McGrath | 9814a94 | 2005-07-04 23:28:10 +0000 | [diff] [blame] | 415 | printnum_int(tcp, addr, fmt) |
| 416 | struct tcb *tcp; |
| 417 | long addr; |
| 418 | char *fmt; |
| 419 | { |
| 420 | int num; |
| 421 | |
| 422 | if (!addr) { |
| 423 | tprintf("NULL"); |
| 424 | return; |
| 425 | } |
| 426 | if (umove(tcp, addr, &num) < 0) { |
| 427 | tprintf("%#lx", addr); |
| 428 | return; |
| 429 | } |
| 430 | tprintf("["); |
| 431 | tprintf(fmt, num); |
| 432 | tprintf("]"); |
| 433 | } |
| 434 | |
| 435 | void |
Roland McGrath | 6bc1220 | 2003-11-13 22:32:27 +0000 | [diff] [blame] | 436 | printuid(text, uid) |
| 437 | const char *text; |
| 438 | unsigned long uid; |
| 439 | { |
| 440 | tprintf("%s", text); |
| 441 | tprintf((uid == -1) ? "%ld" : "%lu", uid); |
| 442 | } |
| 443 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 444 | static char path[MAXPATHLEN + 1]; |
| 445 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 446 | /* |
| 447 | * Quote string `instr' of length `size' |
| 448 | * Write up to (3 + `size' * 4) bytes to `outstr' buffer. |
| 449 | * If `len' < 0, treat `instr' as a NUL-terminated string |
| 450 | * and quote at most (`size' - 1) bytes. |
| 451 | */ |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 452 | static int |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 453 | string_quote(const char *instr, char *outstr, int len, int size) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 454 | { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 455 | const unsigned char *ustr = (const unsigned char *) instr; |
| 456 | char *s = outstr; |
| 457 | int usehex = 0, c, i; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 458 | |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 459 | if (xflag > 1) |
| 460 | usehex = 1; |
| 461 | else if (xflag) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 462 | /* Check for presence of symbol which require |
| 463 | to hex-quote the whole string. */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 464 | for (i = 0; i < size; ++i) { |
| 465 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 466 | /* Check for NUL-terminated string. */ |
| 467 | if (len < 0) { |
| 468 | if (c == '\0') |
| 469 | break; |
| 470 | /* Quote at most size - 1 bytes. */ |
| 471 | if (i == size - 1) |
| 472 | continue; |
| 473 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 474 | if (!isprint(c) && !isspace(c)) { |
| 475 | usehex = 1; |
| 476 | break; |
| 477 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 478 | } |
| 479 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 480 | |
| 481 | *s++ = '\"'; |
| 482 | |
| 483 | if (usehex) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 484 | /* Hex-quote the whole string. */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 485 | for (i = 0; i < size; ++i) { |
| 486 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 487 | /* Check for NUL-terminated string. */ |
| 488 | if (len < 0) { |
| 489 | if (c == '\0') |
| 490 | break; |
| 491 | /* Quote at most size - 1 bytes. */ |
| 492 | if (i == size - 1) |
| 493 | continue; |
| 494 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 495 | sprintf(s, "\\x%02x", c); |
| 496 | s += 4; |
| 497 | } |
| 498 | } else { |
| 499 | for (i = 0; i < size; ++i) { |
| 500 | c = ustr[i]; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 501 | /* Check for NUL-terminated string. */ |
| 502 | if (len < 0) { |
| 503 | if (c == '\0') |
| 504 | break; |
| 505 | /* Quote at most size - 1 bytes. */ |
| 506 | if (i == size - 1) |
| 507 | continue; |
| 508 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 509 | switch (c) { |
| 510 | case '\"': case '\\': |
| 511 | *s++ = '\\'; |
| 512 | *s++ = c; |
| 513 | break; |
| 514 | case '\f': |
| 515 | *s++ = '\\'; |
| 516 | *s++ = 'f'; |
| 517 | break; |
| 518 | case '\n': |
| 519 | *s++ = '\\'; |
| 520 | *s++ = 'n'; |
| 521 | break; |
| 522 | case '\r': |
| 523 | *s++ = '\\'; |
| 524 | *s++ = 'r'; |
| 525 | break; |
| 526 | case '\t': |
| 527 | *s++ = '\\'; |
| 528 | *s++ = 't'; |
| 529 | break; |
| 530 | case '\v': |
| 531 | *s++ = '\\'; |
| 532 | *s++ = 'v'; |
| 533 | break; |
| 534 | default: |
| 535 | if (isprint(c)) |
| 536 | *s++ = c; |
| 537 | else if (i + 1 < size |
| 538 | && isdigit(ustr[i + 1])) { |
| 539 | sprintf(s, "\\%03o", c); |
| 540 | s += 4; |
| 541 | } else { |
| 542 | sprintf(s, "\\%o", c); |
| 543 | s += strlen(s); |
| 544 | } |
| 545 | break; |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | *s++ = '\"'; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 551 | *s = '\0'; |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 552 | |
| 553 | /* Return nonzero if the string was unterminated. */ |
| 554 | return i == size; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 557 | /* |
| 558 | * Print path string specified by address `addr' and length `n'. |
| 559 | * If path length exceeds `n', append `...' to the output. |
| 560 | */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 561 | void |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 562 | printpathn(struct tcb *tcp, long addr, int n) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 563 | { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 564 | if (!addr) { |
Roland McGrath | 371ed8f | 2005-02-06 01:55:07 +0000 | [diff] [blame] | 565 | tprintf("NULL"); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 566 | return; |
| 567 | } |
| 568 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 569 | /* Cap path length to the path buffer size, |
| 570 | and NUL-terminate the buffer. */ |
| 571 | if (n > sizeof path - 1) |
| 572 | n = sizeof path - 1; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 573 | path[n] = '\0'; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 574 | |
| 575 | /* Fetch one byte more to find out whether path length > n. */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 576 | if (umovestr(tcp, addr, n + 1, path) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 577 | tprintf("%#lx", addr); |
| 578 | else { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 579 | static char outstr[4*(sizeof path - 1) + sizeof "\"...\""]; |
| 580 | int trunc = (path[n] != '\0'); |
| 581 | |
| 582 | if (trunc) |
| 583 | path[n] = '\0'; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 584 | (void) string_quote(path, outstr, -1, n + 1); |
| 585 | if (trunc) |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 586 | strcat(outstr, "..."); |
| 587 | tprintf("%s", outstr); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | |
| 591 | void |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 592 | printpath(struct tcb *tcp, long addr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 593 | { |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 594 | printpathn(tcp, addr, sizeof path - 1); |
| 595 | } |
| 596 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 597 | /* |
| 598 | * Print string specified by address `addr' and length `len'. |
| 599 | * If `len' < 0, treat the string as a NUL-terminated string. |
| 600 | * If string length exceeds `max_strlen', append `...' to the output. |
| 601 | */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 602 | void |
| 603 | printstr(struct tcb *tcp, long addr, int len) |
| 604 | { |
| 605 | static char *str = NULL; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 606 | static char *outstr; |
Roland McGrath | 6d97032 | 2007-11-01 23:53:59 +0000 | [diff] [blame] | 607 | int size; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 608 | |
| 609 | if (!addr) { |
| 610 | tprintf("NULL"); |
| 611 | return; |
| 612 | } |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 613 | /* Allocate static buffers if they are not allocated yet. */ |
| 614 | if (!str) |
| 615 | str = malloc(max_strlen + 1); |
| 616 | if (!outstr) |
| 617 | outstr = malloc(4 * max_strlen + sizeof "\"...\""); |
| 618 | if (!str || !outstr) { |
| 619 | fprintf(stderr, "out of memory\n"); |
| 620 | tprintf("%#lx", addr); |
| 621 | return; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 622 | } |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 623 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 624 | if (len < 0) { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 625 | /* |
| 626 | * Treat as a NUL-terminated string: fetch one byte more |
| 627 | * because string_quote() quotes one byte less. |
| 628 | */ |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 629 | size = max_strlen + 1; |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 630 | str[max_strlen] = '\0'; |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 631 | if (umovestr(tcp, addr, size, str) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 632 | tprintf("%#lx", addr); |
| 633 | return; |
| 634 | } |
| 635 | } |
| 636 | else { |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 637 | size = MIN(len, max_strlen); |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 638 | if (umoven(tcp, addr, size, str) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 639 | tprintf("%#lx", addr); |
| 640 | return; |
| 641 | } |
| 642 | } |
| 643 | |
Dmitry V. Levin | a501f14 | 2008-11-10 23:19:13 +0000 | [diff] [blame] | 644 | if (string_quote(str, outstr, len, size) && |
| 645 | (len < 0 || len > max_strlen)) |
Dmitry V. Levin | bea0203 | 2007-10-08 21:48:01 +0000 | [diff] [blame] | 646 | strcat(outstr, "..."); |
Roland McGrath | a503dcf | 2007-08-02 02:06:26 +0000 | [diff] [blame] | 647 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 648 | tprintf("%s", outstr); |
| 649 | } |
| 650 | |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 651 | #if HAVE_SYS_UIO_H |
| 652 | void |
| 653 | dumpiov(tcp, len, addr) |
| 654 | struct tcb * tcp; |
| 655 | int len; |
| 656 | long addr; |
| 657 | { |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 658 | #if defined(LINUX) && SUPPORTED_PERSONALITIES > 1 |
| 659 | union { |
| 660 | struct { u_int32_t base; u_int32_t len; } *iov32; |
| 661 | struct { u_int64_t base; u_int64_t len; } *iov64; |
| 662 | } iovu; |
| 663 | #define iov iovu.iov64 |
| 664 | #define sizeof_iov \ |
| 665 | (personality_wordsize[current_personality] == 4 \ |
| 666 | ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64)) |
| 667 | #define iov_iov_base(i) \ |
| 668 | (personality_wordsize[current_personality] == 4 \ |
| 669 | ? (u_int64_t) iovu.iov32[i].base : iovu.iov64[i].base) |
| 670 | #define iov_iov_len(i) \ |
| 671 | (personality_wordsize[current_personality] == 4 \ |
| 672 | ? (u_int64_t) iovu.iov32[i].len : iovu.iov64[i].len) |
| 673 | #else |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 674 | struct iovec *iov; |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 675 | #define sizeof_iov sizeof(*iov) |
| 676 | #define iov_iov_base(i) iov[i].iov_base |
| 677 | #define iov_iov_len(i) iov[i].iov_len |
| 678 | #endif |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 679 | int i; |
Roland McGrath | aa524c8 | 2005-06-01 19:22:06 +0000 | [diff] [blame] | 680 | unsigned long size; |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 681 | |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 682 | size = sizeof_iov * (unsigned long) len; |
| 683 | if (size / sizeof_iov != len |
| 684 | || (iov = malloc(size)) == NULL) { |
Roland McGrath | aa524c8 | 2005-06-01 19:22:06 +0000 | [diff] [blame] | 685 | fprintf(stderr, "out of memory\n"); |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 686 | return; |
| 687 | } |
Roland McGrath | aa524c8 | 2005-06-01 19:22:06 +0000 | [diff] [blame] | 688 | if (umoven(tcp, addr, size, (char *) iov) >= 0) { |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 689 | for (i = 0; i < len; i++) { |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 690 | /* include the buffer number to make it easy to |
| 691 | * match up the trace with the source */ |
| 692 | tprintf(" * %lu bytes in buffer %d\n", |
| 693 | (unsigned long)iov_iov_len(i), i); |
| 694 | dumpstr(tcp, (long) iov_iov_base(i), |
| 695 | iov_iov_len(i)); |
| 696 | } |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 697 | } |
| 698 | free((char *) iov); |
Dmitry V. Levin | 4ebb4e3 | 2006-12-13 17:08:08 +0000 | [diff] [blame] | 699 | #undef sizeof_iov |
| 700 | #undef iov_iov_base |
| 701 | #undef iov_iov_len |
| 702 | #undef iov |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 703 | } |
| 704 | #endif |
| 705 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 706 | void |
| 707 | dumpstr(tcp, addr, len) |
| 708 | struct tcb *tcp; |
| 709 | long addr; |
| 710 | int len; |
| 711 | { |
| 712 | static int strsize = -1; |
| 713 | static unsigned char *str; |
| 714 | static char outstr[80]; |
| 715 | char *s; |
| 716 | int i, j; |
| 717 | |
| 718 | if (strsize < len) { |
| 719 | if (str) |
| 720 | free(str); |
| 721 | if ((str = malloc(len)) == NULL) { |
Roland McGrath | 46100d0 | 2005-06-01 18:55:42 +0000 | [diff] [blame] | 722 | fprintf(stderr, "out of memory\n"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 723 | return; |
| 724 | } |
| 725 | strsize = len; |
| 726 | } |
| 727 | |
| 728 | if (umoven(tcp, addr, len, (char *) str) < 0) |
| 729 | return; |
| 730 | |
| 731 | for (i = 0; i < len; i += 16) { |
| 732 | s = outstr; |
| 733 | sprintf(s, " | %05x ", i); |
| 734 | s += 9; |
| 735 | for (j = 0; j < 16; j++) { |
| 736 | if (j == 8) |
| 737 | *s++ = ' '; |
| 738 | if (i + j < len) { |
| 739 | sprintf(s, " %02x", str[i + j]); |
| 740 | s += 3; |
| 741 | } |
| 742 | else { |
| 743 | *s++ = ' '; *s++ = ' '; *s++ = ' '; |
| 744 | } |
| 745 | } |
| 746 | *s++ = ' '; *s++ = ' '; |
| 747 | for (j = 0; j < 16; j++) { |
| 748 | if (j == 8) |
| 749 | *s++ = ' '; |
| 750 | if (i + j < len) { |
| 751 | if (isprint(str[i + j])) |
| 752 | *s++ = str[i + j]; |
| 753 | else |
| 754 | *s++ = '.'; |
| 755 | } |
| 756 | else |
| 757 | *s++ = ' '; |
| 758 | } |
| 759 | tprintf("%s |\n", outstr); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | #define PAGMASK (~(PAGSIZ - 1)) |
| 764 | /* |
| 765 | * move `len' bytes of data from process `pid' |
| 766 | * at address `addr' to our space at `laddr' |
| 767 | */ |
| 768 | int |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 769 | umoven(struct tcb *tcp, long addr, int len, char *laddr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 770 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 771 | #ifdef LINUX |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 772 | int pid = tcp->pid; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 773 | int n, m; |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 774 | int started = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 775 | union { |
| 776 | long val; |
| 777 | char x[sizeof(long)]; |
| 778 | } u; |
| 779 | |
| 780 | if (addr & (sizeof(long) - 1)) { |
| 781 | /* addr not a multiple of sizeof(long) */ |
| 782 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 783 | addr &= -sizeof(long); /* residue */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 784 | errno = 0; |
| 785 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 786 | if (errno) { |
| 787 | if (started && (errno==EPERM || errno==EIO)) { |
| 788 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 789 | return 0; |
| 790 | } |
| 791 | /* But if not started, we had a bogus address. */ |
| 792 | if (addr != 0 && errno != EIO && errno != ESRCH) |
| 793 | perror("ptrace: umoven"); |
| 794 | return -1; |
| 795 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 796 | started = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 797 | memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len)); |
| 798 | addr += sizeof(long), laddr += m, len -= m; |
| 799 | } |
| 800 | while (len) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 801 | errno = 0; |
| 802 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 803 | if (errno) { |
| 804 | if (started && (errno==EPERM || errno==EIO)) { |
| 805 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 806 | return 0; |
| 807 | } |
| 808 | if (addr != 0 && errno != EIO && errno != ESRCH) |
| 809 | perror("ptrace: umoven"); |
| 810 | return -1; |
| 811 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 812 | started = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 813 | memcpy(laddr, u.x, m = MIN(sizeof(long), len)); |
| 814 | addr += sizeof(long), laddr += m, len -= m; |
| 815 | } |
| 816 | #endif /* LINUX */ |
| 817 | |
| 818 | #ifdef SUNOS4 |
| 819 | int pid = tcp->pid; |
| 820 | #if 0 |
| 821 | int n, m; |
| 822 | union { |
| 823 | long val; |
| 824 | char x[sizeof(long)]; |
| 825 | } u; |
| 826 | |
| 827 | if (addr & (sizeof(long) - 1)) { |
| 828 | /* addr not a multiple of sizeof(long) */ |
| 829 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 830 | addr &= -sizeof(long); /* residue */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 831 | errno = 0; |
| 832 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 833 | if (errno) { |
| 834 | if (errno != ESRCH) |
| 835 | perror("umoven"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 836 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 837 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 838 | memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len)); |
| 839 | addr += sizeof(long), laddr += m, len -= m; |
| 840 | } |
| 841 | while (len) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 842 | errno = 0; |
| 843 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 844 | if (errno) { |
| 845 | if (errno != ESRCH) |
| 846 | perror("umoven"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 847 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 848 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 849 | memcpy(laddr, u.x, m = MIN(sizeof(long), len)); |
| 850 | addr += sizeof(long), laddr += m, len -= m; |
| 851 | } |
| 852 | #else /* !oldway */ |
| 853 | int n; |
| 854 | |
| 855 | while (len) { |
| 856 | n = MIN(len, PAGSIZ); |
| 857 | n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 858 | if (ptrace(PTRACE_READDATA, pid, |
| 859 | (char *) addr, len, laddr) < 0) { |
| 860 | if (errno != ESRCH) { |
| 861 | perror("umoven: ptrace(PTRACE_READDATA, ...)"); |
| 862 | abort(); |
| 863 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 864 | return -1; |
| 865 | } |
| 866 | len -= n; |
| 867 | addr += n; |
| 868 | laddr += n; |
| 869 | } |
| 870 | #endif /* !oldway */ |
| 871 | #endif /* SUNOS4 */ |
| 872 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 873 | #ifdef USE_PROCFS |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 874 | #ifdef HAVE_MP_PROCFS |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 875 | int fd = tcp->pfd_as; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 876 | #else |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 877 | int fd = tcp->pfd; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 878 | #endif |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 879 | lseek(fd, addr, SEEK_SET); |
| 880 | if (read(fd, laddr, len) == -1) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 881 | return -1; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 882 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * like `umove' but make the additional effort of looking |
| 889 | * for a terminating zero byte. |
| 890 | */ |
| 891 | int |
Denys Vlasenko | ef2fbf8 | 2009-01-06 21:45:06 +0000 | [diff] [blame] | 892 | umovestr(struct tcb *tcp, long addr, int len, char *laddr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 893 | { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 894 | #ifdef USE_PROCFS |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 895 | #ifdef HAVE_MP_PROCFS |
| 896 | int fd = tcp->pfd_as; |
| 897 | #else |
| 898 | int fd = tcp->pfd; |
| 899 | #endif |
| 900 | /* Some systems (e.g. FreeBSD) can be upset if we read off the |
| 901 | end of valid memory, avoid this by trying to read up |
| 902 | to page boundaries. But we don't know what a page is (and |
| 903 | getpagesize(2) (if it exists) doesn't necessarily return |
| 904 | hardware page size). Assume all pages >= 1024 (a-historical |
| 905 | I know) */ |
| 906 | |
| 907 | int page = 1024; /* How to find this? */ |
| 908 | int move = page - (addr & (page - 1)); |
| 909 | int left = len; |
| 910 | |
| 911 | lseek(fd, addr, SEEK_SET); |
| 912 | |
| 913 | while (left) { |
| 914 | if (move > left) move = left; |
John Hughes | 9cecf7f | 2001-10-16 10:20:22 +0000 | [diff] [blame] | 915 | if ((move = read(fd, laddr, move)) <= 0) |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 916 | return left != len ? 0 : -1; |
| 917 | if (memchr (laddr, 0, move)) break; |
| 918 | left -= move; |
| 919 | laddr += move; |
| 920 | addr += move; |
| 921 | move = page; |
| 922 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 923 | #else /* !USE_PROCFS */ |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 924 | int started = 0; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 925 | int pid = tcp->pid; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 926 | int i, n, m; |
| 927 | union { |
| 928 | long val; |
| 929 | char x[sizeof(long)]; |
| 930 | } u; |
| 931 | |
| 932 | if (addr & (sizeof(long) - 1)) { |
| 933 | /* addr not a multiple of sizeof(long) */ |
| 934 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 935 | addr &= -sizeof(long); /* residue */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 936 | errno = 0; |
| 937 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0); |
| 938 | if (errno) { |
| 939 | if (started && (errno==EPERM || errno==EIO)) { |
| 940 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 941 | return 0; |
| 942 | } |
| 943 | if (addr != 0 && errno != EIO && errno != ESRCH) |
| 944 | perror("umovestr"); |
| 945 | return -1; |
| 946 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 947 | started = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 948 | memcpy(laddr, &u.x[n], m = MIN(sizeof(long)-n,len)); |
| 949 | while (n & (sizeof(long) - 1)) |
| 950 | if (u.x[n++] == '\0') |
| 951 | return 0; |
| 952 | addr += sizeof(long), laddr += m, len -= m; |
| 953 | } |
| 954 | while (len) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 955 | errno = 0; |
| 956 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0); |
| 957 | if (errno) { |
| 958 | if (started && (errno==EPERM || errno==EIO)) { |
| 959 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 960 | return 0; |
| 961 | } |
| 962 | if (addr != 0 && errno != EIO && errno != ESRCH) |
| 963 | perror("umovestr"); |
| 964 | return -1; |
| 965 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 966 | started = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 967 | memcpy(laddr, u.x, m = MIN(sizeof(long), len)); |
| 968 | for (i = 0; i < sizeof(long); i++) |
| 969 | if (u.x[i] == '\0') |
| 970 | return 0; |
| 971 | |
| 972 | addr += sizeof(long), laddr += m, len -= m; |
| 973 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 974 | #endif /* !USE_PROCFS */ |
John Hughes | aa09c6b | 2001-05-15 14:53:43 +0000 | [diff] [blame] | 975 | return 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | #ifdef LINUX |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 979 | # if !defined (SPARC) && !defined(SPARC64) |
| 980 | # define PTRACE_WRITETEXT 101 |
| 981 | # define PTRACE_WRITEDATA 102 |
| 982 | # endif /* !SPARC && !SPARC64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 983 | #endif /* LINUX */ |
| 984 | |
| 985 | #ifdef SUNOS4 |
| 986 | |
| 987 | static int |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 988 | uload(cmd, pid, addr, len, laddr) |
| 989 | int cmd; |
| 990 | int pid; |
| 991 | long addr; |
| 992 | int len; |
| 993 | char *laddr; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 994 | { |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 995 | # if 0 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 996 | int n; |
| 997 | |
| 998 | while (len) { |
| 999 | n = MIN(len, PAGSIZ); |
| 1000 | n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1001 | if (ptrace(cmd, pid, (char *)addr, n, laddr) < 0) { |
| 1002 | perror("uload: ptrace(PTRACE_WRITE, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1003 | return -1; |
| 1004 | } |
| 1005 | len -= n; |
| 1006 | addr += n; |
| 1007 | laddr += n; |
| 1008 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1009 | # else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1010 | int peek, poke; |
| 1011 | int n, m; |
| 1012 | union { |
| 1013 | long val; |
| 1014 | char x[sizeof(long)]; |
| 1015 | } u; |
| 1016 | |
| 1017 | if (cmd == PTRACE_WRITETEXT) { |
| 1018 | peek = PTRACE_PEEKTEXT; |
| 1019 | poke = PTRACE_POKETEXT; |
| 1020 | } |
| 1021 | else { |
| 1022 | peek = PTRACE_PEEKDATA; |
| 1023 | poke = PTRACE_POKEDATA; |
| 1024 | } |
| 1025 | if (addr & (sizeof(long) - 1)) { |
| 1026 | /* addr not a multiple of sizeof(long) */ |
| 1027 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 1028 | addr &= -sizeof(long); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1029 | errno = 0; |
| 1030 | u.val = ptrace(peek, pid, (char *) addr, 0); |
| 1031 | if (errno) { |
| 1032 | perror("uload: POKE"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1033 | return -1; |
| 1034 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1035 | memcpy(&u.x[n], laddr, m = MIN(sizeof(long) - n, len)); |
| 1036 | if (ptrace(poke, pid, (char *)addr, u.val) < 0) { |
| 1037 | perror("uload: POKE"); |
| 1038 | return -1; |
| 1039 | } |
| 1040 | addr += sizeof(long), laddr += m, len -= m; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1041 | } |
| 1042 | while (len) { |
| 1043 | if (len < sizeof(long)) |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1044 | u.val = ptrace(peek, pid, (char *) addr, 0); |
| 1045 | memcpy(u.x, laddr, m = MIN(sizeof(long), len)); |
| 1046 | if (ptrace(poke, pid, (char *) addr, u.val) < 0) { |
| 1047 | perror("uload: POKE"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1048 | return -1; |
| 1049 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1050 | addr += sizeof(long), laddr += m, len -= m; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1051 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1052 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1053 | return 0; |
| 1054 | } |
| 1055 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1056 | int |
| 1057 | tload(pid, addr, len, laddr) |
| 1058 | int pid; |
| 1059 | int addr, len; |
| 1060 | char *laddr; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1061 | { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1062 | return uload(PTRACE_WRITETEXT, pid, addr, len, laddr); |
| 1063 | } |
| 1064 | |
| 1065 | int |
| 1066 | dload(pid, addr, len, laddr) |
| 1067 | int pid; |
| 1068 | int addr; |
| 1069 | int len; |
| 1070 | char *laddr; |
| 1071 | { |
| 1072 | return uload(PTRACE_WRITEDATA, pid, addr, len, laddr); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
| 1075 | #endif /* SUNOS4 */ |
| 1076 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1077 | #ifndef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1078 | |
| 1079 | int |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1080 | upeek(tcp, off, res) |
| 1081 | struct tcb *tcp; |
| 1082 | long off; |
| 1083 | long *res; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1084 | { |
| 1085 | long val; |
| 1086 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1087 | # ifdef SUNOS4_KERNEL_ARCH_KLUDGE |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1088 | { |
| 1089 | static int is_sun4m = -1; |
| 1090 | struct utsname name; |
| 1091 | |
| 1092 | /* Round up the usual suspects. */ |
| 1093 | if (is_sun4m == -1) { |
| 1094 | if (uname(&name) < 0) { |
| 1095 | perror("upeek: uname?"); |
| 1096 | exit(1); |
| 1097 | } |
| 1098 | is_sun4m = strcmp(name.machine, "sun4m") == 0; |
| 1099 | if (is_sun4m) { |
Roland McGrath | d9f816f | 2004-09-04 03:39:20 +0000 | [diff] [blame] | 1100 | const struct xlat *x; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1101 | |
| 1102 | for (x = struct_user_offsets; x->str; x++) |
| 1103 | x->val += 1024; |
| 1104 | } |
| 1105 | } |
| 1106 | if (is_sun4m) |
| 1107 | off += 1024; |
| 1108 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1109 | # endif /* SUNOS4_KERNEL_ARCH_KLUDGE */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1110 | errno = 0; |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 1111 | val = do_ptrace(PTRACE_PEEKUSER, tcp, (char *) off, 0); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1112 | if (val == -1 && errno) { |
| 1113 | if (errno != ESRCH) { |
| 1114 | char buf[60]; |
| 1115 | sprintf(buf,"upeek: ptrace(PTRACE_PEEKUSER,%d,%lu,0)", tcp->pid, off); |
| 1116 | perror(buf); |
| 1117 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1118 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1119 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1120 | *res = val; |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1124 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1125 | |
Roland McGrath | a4d4853 | 2005-06-08 20:45:28 +0000 | [diff] [blame] | 1126 | #if 0 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1127 | long |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1128 | getpc(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1129 | { |
| 1130 | |
| 1131 | #ifdef LINUX |
| 1132 | long pc; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1133 | # if defined(I386) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1134 | if (upeek(tcp, 4*EIP, &pc) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1135 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1136 | # elif defined(X86_64) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1137 | if (upeek(tcp, 8*RIP, &pc) < 0) |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1138 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1139 | # elif defined(IA64) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1140 | if (upeek(tcp, PT_B0, &pc) < 0) |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1141 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1142 | # elif defined(ARM) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1143 | if (upeek(tcp, 4*15, &pc) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1144 | return -1; |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1145 | # elif defined(AVR32) |
| 1146 | if (upeek(tcp, REG_PC, &pc) < 0) |
| 1147 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1148 | # elif defined(BFIN) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1149 | if (upeek(tcp, REG_PC, &pc) < 0) |
Dmitry V. Levin | 87ea1f4 | 2008-11-10 22:21:41 +0000 | [diff] [blame] | 1150 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1151 | # elif defined(POWERPC) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1152 | if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1153 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1154 | # elif defined(M68K) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1155 | if (upeek(tcp, 4*PT_PC, &pc) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1156 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1157 | # elif defined(ALPHA) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1158 | if (upeek(tcp, REG_PC, &pc) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1159 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1160 | # elif defined(MIPS) |
| 1161 | if (upeek(tcp, REG_EPC, &pc) < 0) |
| 1162 | return -1; |
| 1163 | # elif defined(SPARC) || defined(SPARC64) |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1164 | struct pt_regs regs; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1165 | if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1166 | return -1; |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1167 | # if defined(SPARC64) |
| 1168 | pc = regs.tpc; |
| 1169 | # else |
| 1170 | pc = regs.pc; |
| 1171 | # endif |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1172 | # elif defined(S390) || defined(S390X) |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1173 | if(upeek(tcp,PT_PSWADDR,&pc) < 0) |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 1174 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1175 | # elif defined(HPPA) |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1176 | if(upeek(tcp,PT_IAOQ0,&pc) < 0) |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1177 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1178 | # elif defined(SH) |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1179 | if (upeek(tcp, 4*REG_PC ,&pc) < 0) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1180 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1181 | # elif defined(SH64) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1182 | if (upeek(tcp, REG_PC ,&pc) < 0) |
| 1183 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1184 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1185 | return pc; |
| 1186 | #endif /* LINUX */ |
| 1187 | |
| 1188 | #ifdef SUNOS4 |
| 1189 | /* |
| 1190 | * Return current program counter for `pid' |
| 1191 | * Assumes PC is never 0xffffffff |
| 1192 | */ |
| 1193 | struct regs regs; |
| 1194 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1195 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) { |
| 1196 | perror("getpc: ptrace(PTRACE_GETREGS, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1197 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1198 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1199 | return regs.r_pc; |
| 1200 | #endif /* SUNOS4 */ |
| 1201 | |
| 1202 | #ifdef SVR4 |
| 1203 | /* XXX */ |
| 1204 | return 0; |
| 1205 | #endif /* SVR4 */ |
| 1206 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1207 | #ifdef FREEBSD |
| 1208 | struct reg regs; |
| 1209 | pread(tcp->pfd_reg, ®s, sizeof(regs), 0); |
| 1210 | return regs.r_eip; |
| 1211 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1212 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1213 | #endif /* 0 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1214 | |
| 1215 | void |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1216 | printcall(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1217 | { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1218 | #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \ |
| 1219 | sizeof(long) == 8 ? "[????????????????] " : \ |
| 1220 | NULL /* crash */) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1221 | |
| 1222 | #ifdef LINUX |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1223 | # ifdef I386 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1224 | long eip; |
| 1225 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1226 | if (upeek(tcp, 4*EIP, &eip) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1227 | PRINTBADPC; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1228 | return; |
| 1229 | } |
| 1230 | tprintf("[%08lx] ", eip); |
Roland McGrath | eac26fc | 2005-02-02 02:48:53 +0000 | [diff] [blame] | 1231 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1232 | # elif defined(S390) || defined(S390X) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1233 | long psw; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1234 | if(upeek(tcp,PT_PSWADDR,&psw) < 0) { |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1235 | PRINTBADPC; |
| 1236 | return; |
| 1237 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1238 | # ifdef S390 |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1239 | tprintf("[%08lx] ", psw); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1240 | # elif S390X |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1241 | tprintf("[%16lx] ", psw); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1242 | # endif |
Roland McGrath | eac26fc | 2005-02-02 02:48:53 +0000 | [diff] [blame] | 1243 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1244 | # elif defined(X86_64) |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1245 | long rip; |
| 1246 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1247 | if (upeek(tcp, 8*RIP, &rip) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1248 | PRINTBADPC; |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1249 | return; |
| 1250 | } |
| 1251 | tprintf("[%16lx] ", rip); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1252 | # elif defined(IA64) |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1253 | long ip; |
| 1254 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1255 | if (upeek(tcp, PT_B0, &ip) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1256 | PRINTBADPC; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1257 | return; |
| 1258 | } |
| 1259 | tprintf("[%08lx] ", ip); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1260 | # elif defined(POWERPC) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1261 | long pc; |
| 1262 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1263 | if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1264 | tprintf ("[????????] "); |
| 1265 | return; |
| 1266 | } |
| 1267 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1268 | # elif defined(M68K) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1269 | long pc; |
| 1270 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1271 | if (upeek(tcp, 4*PT_PC, &pc) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1272 | tprintf ("[????????] "); |
| 1273 | return; |
| 1274 | } |
| 1275 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1276 | # elif defined(ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1277 | long pc; |
| 1278 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1279 | if (upeek(tcp, REG_PC, &pc) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1280 | tprintf ("[????????????????] "); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1281 | return; |
| 1282 | } |
| 1283 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1284 | # elif defined(SPARC) || defined(SPARC64) |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1285 | struct pt_regs regs; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1286 | if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1287 | PRINTBADPC; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1288 | return; |
| 1289 | } |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1290 | # if defined(SPARC64) |
| 1291 | tprintf("[%08lx] ", regs.tpc); |
| 1292 | # else |
| 1293 | tprintf("[%08lx] ", regs.pc); |
| 1294 | # endif |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1295 | # elif defined(HPPA) |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1296 | long pc; |
| 1297 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1298 | if(upeek(tcp,PT_IAOQ0,&pc) < 0) { |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1299 | tprintf ("[????????] "); |
| 1300 | return; |
| 1301 | } |
| 1302 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1303 | # elif defined(MIPS) |
Wichert Akkerman | 75c422b | 2001-04-10 10:22:50 +0000 | [diff] [blame] | 1304 | long pc; |
| 1305 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1306 | if (upeek(tcp, REG_EPC, &pc) < 0) { |
Wichert Akkerman | 75c422b | 2001-04-10 10:22:50 +0000 | [diff] [blame] | 1307 | tprintf ("[????????] "); |
| 1308 | return; |
| 1309 | } |
| 1310 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1311 | # elif defined(SH) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1312 | long pc; |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1313 | |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1314 | if (upeek(tcp, 4*REG_PC, &pc) < 0) { |
| 1315 | tprintf ("[????????] "); |
| 1316 | return; |
| 1317 | } |
| 1318 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1319 | # elif defined(SH64) |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1320 | long pc; |
| 1321 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1322 | if (upeek(tcp, REG_PC, &pc) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1323 | tprintf ("[????????????????] "); |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1324 | return; |
| 1325 | } |
| 1326 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1327 | # elif defined(ARM) |
Roland McGrath | ef38868 | 2003-06-03 23:28:59 +0000 | [diff] [blame] | 1328 | long pc; |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1329 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1330 | if (upeek(tcp, 4*15, &pc) < 0) { |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1331 | PRINTBADPC; |
Roland McGrath | ef38868 | 2003-06-03 23:28:59 +0000 | [diff] [blame] | 1332 | return; |
| 1333 | } |
| 1334 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1335 | # elif defined(AVR32) |
| 1336 | long pc; |
| 1337 | |
| 1338 | if (upeek(tcp, REG_PC, &pc) < 0) { |
| 1339 | tprintf("[????????] "); |
| 1340 | return; |
| 1341 | } |
| 1342 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1343 | # elif defined(BFIN) |
Dmitry V. Levin | 87ea1f4 | 2008-11-10 22:21:41 +0000 | [diff] [blame] | 1344 | long pc; |
| 1345 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1346 | if (upeek(tcp, PT_PC, &pc) < 0) { |
Dmitry V. Levin | 87ea1f4 | 2008-11-10 22:21:41 +0000 | [diff] [blame] | 1347 | PRINTBADPC; |
| 1348 | return; |
| 1349 | } |
| 1350 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1351 | #elif defined(CRISV10) |
| 1352 | long pc; |
| 1353 | |
Edgar E. Iglesias | eeb9ce3 | 2009-10-05 14:41:02 +0000 | [diff] [blame] | 1354 | if (upeek(tcp, 4*PT_IRP, &pc) < 0) { |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1355 | PRINTBADPC; |
| 1356 | return; |
| 1357 | } |
| 1358 | tprintf("[%08lx] ", pc); |
| 1359 | #elif defined(CRISV32) |
| 1360 | long pc; |
| 1361 | |
Edgar E. Iglesias | eeb9ce3 | 2009-10-05 14:41:02 +0000 | [diff] [blame] | 1362 | if (upeek(tcp, 4*PT_ERP, &pc) < 0) { |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1363 | PRINTBADPC; |
| 1364 | return; |
| 1365 | } |
| 1366 | tprintf("[%08lx] ", pc); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1367 | # endif /* architecture */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1368 | #endif /* LINUX */ |
| 1369 | |
| 1370 | #ifdef SUNOS4 |
| 1371 | struct regs regs; |
| 1372 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1373 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) { |
| 1374 | perror("printcall: ptrace(PTRACE_GETREGS, ...)"); |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1375 | PRINTBADPC; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1376 | return; |
| 1377 | } |
| 1378 | tprintf("[%08x] ", regs.r_o7); |
| 1379 | #endif /* SUNOS4 */ |
| 1380 | |
| 1381 | #ifdef SVR4 |
| 1382 | /* XXX */ |
Roland McGrath | 7a91883 | 2005-02-02 20:55:23 +0000 | [diff] [blame] | 1383 | PRINTBADPC; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1384 | #endif |
| 1385 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1386 | #ifdef FREEBSD |
| 1387 | struct reg regs; |
| 1388 | pread(tcp->pfd_reg, ®s, sizeof(regs), 0); |
| 1389 | tprintf("[%08x] ", regs.r_eip); |
| 1390 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1393 | |
| 1394 | /* |
| 1395 | * These #if's are huge, please indent them correctly. |
| 1396 | * It's easy to get confused otherwise. |
| 1397 | */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1398 | #ifndef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1399 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1400 | # if defined LINUX |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1401 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1402 | # include "syscall.h" |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1403 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1404 | # include <sys/syscall.h> |
| 1405 | # ifndef CLONE_PTRACE |
| 1406 | # define CLONE_PTRACE 0x00002000 |
| 1407 | # endif |
| 1408 | # ifndef CLONE_VFORK |
| 1409 | # define CLONE_VFORK 0x00004000 |
| 1410 | # endif |
| 1411 | # ifndef CLONE_VM |
| 1412 | # define CLONE_VM 0x00000100 |
| 1413 | # endif |
| 1414 | # ifndef CLONE_STOPPED |
| 1415 | # define CLONE_STOPPED 0x02000000 |
| 1416 | # endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1417 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1418 | # ifdef IA64 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1419 | |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1420 | /* We don't have fork()/vfork() syscalls on ia64 itself, but the ia32 |
| 1421 | subsystem has them for x86... */ |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1422 | # define SYS_fork 2 |
| 1423 | # define SYS_vfork 190 |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1424 | |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1425 | typedef unsigned long *arg_setup_state; |
| 1426 | |
| 1427 | static int |
| 1428 | arg_setup(struct tcb *tcp, arg_setup_state *state) |
| 1429 | { |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1430 | unsigned long cfm, sof, sol; |
| 1431 | long bsp; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1432 | |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1433 | if (ia32) { |
| 1434 | /* Satisfy a false GCC warning. */ |
| 1435 | *state = NULL; |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1436 | return 0; |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1437 | } |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1438 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1439 | if (upeek(tcp, PT_AR_BSP, &bsp) < 0) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1440 | return -1; |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1441 | if (upeek(tcp, PT_CFM, (long *) &cfm) < 0) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1442 | return -1; |
| 1443 | |
| 1444 | sof = (cfm >> 0) & 0x7f; |
| 1445 | sol = (cfm >> 7) & 0x7f; |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1446 | bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1447 | |
Jan Kratochvil | 1f94271 | 2008-08-06 21:38:52 +0000 | [diff] [blame] | 1448 | *state = (unsigned long *) bsp; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1449 | return 0; |
| 1450 | } |
| 1451 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1452 | # define arg_finish_change(tcp, state) 0 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1453 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1454 | # ifdef SYS_fork |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1455 | static int |
| 1456 | get_arg0 (struct tcb *tcp, arg_setup_state *state, long *valp) |
| 1457 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1458 | int ret; |
| 1459 | |
| 1460 | if (ia32) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1461 | ret = upeek (tcp, PT_R11, valp); |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1462 | else |
| 1463 | ret = umoven (tcp, |
| 1464 | (unsigned long) ia64_rse_skip_regs(*state, 0), |
| 1465 | sizeof(long), (void *) valp); |
| 1466 | return ret; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | static int |
| 1470 | get_arg1 (struct tcb *tcp, arg_setup_state *state, long *valp) |
| 1471 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1472 | int ret; |
| 1473 | |
| 1474 | if (ia32) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1475 | ret = upeek (tcp, PT_R9, valp); |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1476 | else |
| 1477 | ret = umoven (tcp, |
| 1478 | (unsigned long) ia64_rse_skip_regs(*state, 1), |
| 1479 | sizeof(long), (void *) valp); |
| 1480 | return ret; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1481 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1482 | # endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1483 | |
| 1484 | static int |
| 1485 | set_arg0 (struct tcb *tcp, arg_setup_state *state, long val) |
| 1486 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1487 | int req = PTRACE_POKEDATA; |
| 1488 | void *ap; |
| 1489 | |
| 1490 | if (ia32) { |
| 1491 | ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */ |
| 1492 | req = PTRACE_POKEUSER; |
| 1493 | } else |
| 1494 | ap = ia64_rse_skip_regs(*state, 0); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1495 | errno = 0; |
| 1496 | ptrace(req, tcp->pid, ap, val); |
| 1497 | return errno ? -1 : 0; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | static int |
| 1501 | set_arg1 (struct tcb *tcp, arg_setup_state *state, long val) |
| 1502 | { |
Roland McGrath | 08267b8 | 2004-02-20 22:56:43 +0000 | [diff] [blame] | 1503 | int req = PTRACE_POKEDATA; |
| 1504 | void *ap; |
| 1505 | |
| 1506 | if (ia32) { |
| 1507 | ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */ |
| 1508 | req = PTRACE_POKEUSER; |
| 1509 | } else |
| 1510 | ap = ia64_rse_skip_regs(*state, 1); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1511 | errno = 0; |
| 1512 | ptrace(req, tcp->pid, ap, val); |
| 1513 | return errno ? -1 : 0; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Roland McGrath | b659f87 | 2008-07-18 01:19:36 +0000 | [diff] [blame] | 1516 | /* ia64 does not return the input arguments from functions (and syscalls) |
| 1517 | according to ia64 RSE (Register Stack Engine) behavior. */ |
| 1518 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1519 | # define restore_arg0(tcp, state, val) ((void) (state), 0) |
| 1520 | # define restore_arg1(tcp, state, val) ((void) (state), 0) |
Roland McGrath | b659f87 | 2008-07-18 01:19:36 +0000 | [diff] [blame] | 1521 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1522 | # elif defined (SPARC) || defined (SPARC64) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1523 | |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1524 | typedef struct pt_regs arg_setup_state; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1525 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1526 | # define arg_setup(tcp, state) \ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1527 | (ptrace (PTRACE_GETREGS, tcp->pid, (char *) (state), 0)) |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1528 | # define arg_finish_change(tcp, state) \ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1529 | (ptrace (PTRACE_SETREGS, tcp->pid, (char *) (state), 0)) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1530 | |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1531 | # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0) |
| 1532 | # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0) |
| 1533 | # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0) |
| 1534 | # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0) |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1535 | # define restore_arg0(tcp, state, val) 0 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1536 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1537 | # else /* other architectures */ |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1538 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1539 | # if defined S390 || defined S390X |
Roland McGrath | 7b30822 | 2003-01-20 09:04:36 +0000 | [diff] [blame] | 1540 | /* Note: this is only true for the `clone' system call, which handles |
| 1541 | arguments specially. We could as well say that its first two arguments |
| 1542 | are swapped relative to other architectures, but that would just be |
| 1543 | another #ifdef in the calls. */ |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1544 | # define arg0_offset PT_GPR3 |
| 1545 | # define arg1_offset PT_ORIGGPR2 |
| 1546 | # define restore_arg0(tcp, state, val) ((void) (state), 0) |
| 1547 | # define restore_arg1(tcp, state, val) ((void) (state), 0) |
| 1548 | # define arg0_index 1 |
| 1549 | # define arg1_index 0 |
| 1550 | # elif defined (ALPHA) || defined (MIPS) |
| 1551 | # define arg0_offset REG_A0 |
| 1552 | # define arg1_offset (REG_A0+1) |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1553 | # elif defined (AVR32) |
| 1554 | # define arg0_offset (REG_R12) |
| 1555 | # define arg1_offset (REG_R11) |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1556 | # elif defined (POWERPC) |
| 1557 | # define arg0_offset (sizeof(unsigned long)*PT_R3) |
| 1558 | # define arg1_offset (sizeof(unsigned long)*PT_R4) |
| 1559 | # define restore_arg0(tcp, state, val) ((void) (state), 0) |
| 1560 | # elif defined (HPPA) |
| 1561 | # define arg0_offset PT_GR26 |
| 1562 | # define arg1_offset (PT_GR26-4) |
| 1563 | # elif defined (X86_64) |
| 1564 | # define arg0_offset ((long)(8*(current_personality ? RBX : RDI))) |
| 1565 | # define arg1_offset ((long)(8*(current_personality ? RCX : RSI))) |
| 1566 | # elif defined (SH) |
| 1567 | # define arg0_offset (4*(REG_REG0+4)) |
| 1568 | # define arg1_offset (4*(REG_REG0+5)) |
| 1569 | # elif defined (SH64) |
| 1570 | /* ABI defines arg0 & 1 in r2 & r3 */ |
| 1571 | # define arg0_offset (REG_OFFSET+16) |
| 1572 | # define arg1_offset (REG_OFFSET+24) |
| 1573 | # define restore_arg0(tcp, state, val) 0 |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1574 | # elif defined CRISV10 || defined CRISV32 |
| 1575 | # define arg0_offset (4*PT_R11) |
| 1576 | # define arg1_offset (4*PT_ORIG_R10) |
| 1577 | # define restore_arg0(tcp, state, val) 0 |
| 1578 | # define restore_arg1(tcp, state, val) 0 |
| 1579 | # define arg0_index 1 |
| 1580 | # define arg1_index 0 |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1581 | # else |
| 1582 | # define arg0_offset 0 |
| 1583 | # define arg1_offset 4 |
| 1584 | # if defined ARM |
| 1585 | # define restore_arg0(tcp, state, val) 0 |
| 1586 | # endif |
| 1587 | # endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1588 | |
| 1589 | typedef int arg_setup_state; |
| 1590 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1591 | # define arg_setup(tcp, state) (0) |
| 1592 | # define arg_finish_change(tcp, state) 0 |
| 1593 | # define get_arg0(tcp, cookie, valp) \ |
| 1594 | (upeek ((tcp), arg0_offset, (valp))) |
| 1595 | # define get_arg1(tcp, cookie, valp) \ |
| 1596 | (upeek ((tcp), arg1_offset, (valp))) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1597 | |
| 1598 | static int |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1599 | set_arg0 (struct tcb *tcp, void *cookie, long val) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1600 | { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1601 | return ptrace (PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | static int |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1605 | set_arg1 (struct tcb *tcp, void *cookie, long val) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1606 | { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1607 | return ptrace (PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val); |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1610 | # endif /* architectures */ |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1611 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1612 | # ifndef restore_arg0 |
| 1613 | # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val)) |
| 1614 | # endif |
| 1615 | # ifndef restore_arg1 |
| 1616 | # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val)) |
| 1617 | # endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1618 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1619 | # ifndef arg0_index |
| 1620 | # define arg0_index 0 |
| 1621 | # define arg1_index 1 |
| 1622 | # endif |
Roland McGrath | 90d0afd | 2004-03-01 21:05:16 +0000 | [diff] [blame] | 1623 | |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1624 | int |
Denys Vlasenko | 418d66a | 2009-01-17 01:52:54 +0000 | [diff] [blame] | 1625 | setbpt(struct tcb *tcp) |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1626 | { |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1627 | static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone }; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1628 | arg_setup_state state; |
| 1629 | |
| 1630 | if (tcp->flags & TCB_BPTSET) { |
| 1631 | fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid); |
| 1632 | return -1; |
| 1633 | } |
| 1634 | |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1635 | /* |
| 1636 | * It's a silly kludge to initialize this with a search at runtime. |
| 1637 | * But it's better than maintaining another magic thing in the |
| 1638 | * godforsaken tables. |
| 1639 | */ |
| 1640 | if (clone_scno[current_personality] == 0) { |
| 1641 | int i; |
| 1642 | for (i = 0; i < nsyscalls; ++i) |
| 1643 | if (sysent[i].sys_func == sys_clone) { |
| 1644 | clone_scno[current_personality] = i; |
| 1645 | break; |
| 1646 | } |
| 1647 | } |
| 1648 | |
Roland McGrath | 76989d7 | 2005-06-07 23:21:31 +0000 | [diff] [blame] | 1649 | switch (known_scno(tcp)) { |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1650 | # ifdef SYS_vfork |
Roland McGrath | 9383c6c | 2003-01-18 00:19:31 +0000 | [diff] [blame] | 1651 | case SYS_vfork: |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1652 | # endif |
| 1653 | # ifdef SYS_fork |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1654 | case SYS_fork: |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1655 | # endif |
| 1656 | # if defined SYS_fork || defined SYS_vfork |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1657 | if (arg_setup (tcp, &state) < 0 |
| 1658 | || get_arg0 (tcp, &state, &tcp->inst[0]) < 0 |
| 1659 | || get_arg1 (tcp, &state, &tcp->inst[1]) < 0 |
Roland McGrath | 3291ef2 | 2008-05-20 00:34:34 +0000 | [diff] [blame] | 1660 | || change_syscall(tcp, clone_scno[current_personality]) < 0 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1661 | || set_arg0 (tcp, &state, CLONE_PTRACE|SIGCHLD) < 0 |
| 1662 | || set_arg1 (tcp, &state, 0) < 0 |
| 1663 | || arg_finish_change (tcp, &state) < 0) |
| 1664 | return -1; |
Roland McGrath | c9dc3c1 | 2004-03-01 20:57:09 +0000 | [diff] [blame] | 1665 | tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD; |
| 1666 | tcp->u_arg[arg1_index] = 0; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1667 | tcp->flags |= TCB_BPTSET; |
| 1668 | return 0; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1669 | # endif |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1670 | |
| 1671 | case SYS_clone: |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1672 | # ifdef SYS_clone2 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1673 | case SYS_clone2: |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1674 | # endif |
Jan Kratochvil | 8fc9575 | 2008-08-06 21:43:35 +0000 | [diff] [blame] | 1675 | /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)' |
| 1676 | contrary to x86 SYS_vfork above. Even on x86 we turn the |
| 1677 | vfork semantics into plain fork - each application must not |
| 1678 | depend on the vfork specifics according to POSIX. We would |
| 1679 | hang waiting for the parent resume otherwise. We need to |
| 1680 | clear also CLONE_VM but only in the CLONE_VFORK case as |
| 1681 | otherwise we would break pthread_create. */ |
| 1682 | |
Roland McGrath | d6ff0d5 | 2008-07-18 01:09:44 +0000 | [diff] [blame] | 1683 | if ((arg_setup (tcp, &state) < 0 |
| 1684 | || set_arg0 (tcp, &state, |
| 1685 | (tcp->u_arg[arg0_index] | CLONE_PTRACE) |
Jan Kratochvil | 8fc9575 | 2008-08-06 21:43:35 +0000 | [diff] [blame] | 1686 | & ~(tcp->u_arg[arg0_index] & CLONE_VFORK |
| 1687 | ? CLONE_VFORK | CLONE_VM : 0)) < 0 |
Roland McGrath | d6ff0d5 | 2008-07-18 01:09:44 +0000 | [diff] [blame] | 1688 | || arg_finish_change (tcp, &state) < 0)) |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1689 | return -1; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1690 | tcp->flags |= TCB_BPTSET; |
Roland McGrath | c9dc3c1 | 2004-03-01 20:57:09 +0000 | [diff] [blame] | 1691 | tcp->inst[0] = tcp->u_arg[arg0_index]; |
| 1692 | tcp->inst[1] = tcp->u_arg[arg1_index]; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1693 | return 0; |
| 1694 | |
| 1695 | default: |
| 1696 | fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n", |
| 1697 | tcp->scno, tcp->pid); |
| 1698 | break; |
| 1699 | } |
| 1700 | |
| 1701 | return -1; |
| 1702 | } |
| 1703 | |
| 1704 | int |
| 1705 | clearbpt(tcp) |
| 1706 | struct tcb *tcp; |
| 1707 | { |
| 1708 | arg_setup_state state; |
| 1709 | if (arg_setup (tcp, &state) < 0 |
Roland McGrath | e1df47f | 2003-01-14 09:46:15 +0000 | [diff] [blame] | 1710 | || restore_arg0 (tcp, &state, tcp->inst[0]) < 0 |
| 1711 | || restore_arg1 (tcp, &state, tcp->inst[1]) < 0 |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1712 | || arg_finish_change (tcp, &state)) |
Andreas Schwab | 46ed50d | 2009-11-11 13:54:04 +0100 | [diff] [blame] | 1713 | if (errno != ESRCH) return -1; |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1714 | tcp->flags &= ~TCB_BPTSET; |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1718 | # else /* !defined LINUX */ |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 1719 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1720 | int |
| 1721 | setbpt(tcp) |
| 1722 | struct tcb *tcp; |
| 1723 | { |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1724 | # ifdef LINUX |
| 1725 | DEAD CODE HERE? WE ARE IN 'else !defined LINUX' |
| 1726 | # if defined (SPARC) || defined (SPARC64) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1727 | /* We simply use the SunOS breakpoint code. */ |
| 1728 | |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1729 | struct regs regs; |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1730 | unsigned long inst; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1731 | # define LOOPA 0x30800000 /* ba,a 0 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1732 | |
| 1733 | if (tcp->flags & TCB_BPTSET) { |
| 1734 | fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid); |
| 1735 | return -1; |
| 1736 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1737 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 1738 | perror("setbpt: ptrace(PTRACE_GETREGS, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1739 | return -1; |
| 1740 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1741 | tcp->baddr = regs.r_o7 + 8; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1742 | errno = 0; |
| 1743 | tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)tcp->baddr, 0); |
| 1744 | if(errno) { |
| 1745 | perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1746 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1747 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1748 | |
| 1749 | /* |
| 1750 | * XXX - BRUTAL MODE ON |
| 1751 | * We cannot set a real BPT in the child, since it will not be |
| 1752 | * traced at the moment it will reach the trap and would probably |
| 1753 | * die with a core dump. |
| 1754 | * Thus, we are force our way in by taking out two instructions |
| 1755 | * and insert an eternal loop instead, in expectance of the SIGSTOP |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1756 | * generated by our PTRACE_ATTACH. |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1757 | * Of cause, if we evaporate ourselves in the middle of all this... |
| 1758 | */ |
| 1759 | errno = 0; |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1760 | inst = LOOPA; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1761 | # if defined (SPARC64) |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1762 | inst <<= 32; |
| 1763 | inst |= (tcp->inst[0] & 0xffffffffUL); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1764 | # endif |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1765 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, inst); |
| 1766 | if(errno) { |
| 1767 | perror("setbpt: ptrace(PTRACE_POKETEXT, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1768 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1769 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1770 | tcp->flags |= TCB_BPTSET; |
| 1771 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1772 | # else /* !SPARC && !SPARC64 */ |
| 1773 | # ifdef IA64 |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1774 | if (ia32) { |
| 1775 | # define LOOP 0x0000feeb |
| 1776 | if (tcp->flags & TCB_BPTSET) { |
| 1777 | fprintf(stderr, "PANIC: bpt already set in pid %u\n", |
| 1778 | tcp->pid); |
| 1779 | return -1; |
| 1780 | } |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1781 | if (upeek(tcp, PT_CR_IIP, &tcp->baddr) < 0) |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1782 | return -1; |
| 1783 | if (debug) |
| 1784 | fprintf(stderr, "[%d] setting bpt at %lx\n", |
| 1785 | tcp->pid, tcp->baddr); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1786 | tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, |
| 1787 | (char *) tcp->baddr, 0); |
| 1788 | if (errno) { |
| 1789 | perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)"); |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1790 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1791 | } |
| 1792 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP); |
| 1793 | if (errno) { |
| 1794 | perror("setbpt: ptrace(PTRACE_POKETEXT, ...)"); |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1795 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1796 | } |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1797 | tcp->flags |= TCB_BPTSET; |
| 1798 | } else { |
| 1799 | /* |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1800 | * Our strategy here is to replace the bundle that |
| 1801 | * contained the clone() syscall with a bundle of the |
| 1802 | * form: |
| 1803 | * |
| 1804 | * { 1: br 1b; br 1b; br 1b } |
| 1805 | * |
| 1806 | * This ensures that the newly forked child will loop |
| 1807 | * endlessly until we've got a chance to attach to it. |
| 1808 | */ |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1809 | # define LOOP0 0x0000100000000017 |
| 1810 | # define LOOP1 0x4000000000200000 |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1811 | unsigned long addr, ipsr; |
| 1812 | pid_t pid; |
| 1813 | |
| 1814 | pid = tcp->pid; |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1815 | if (upeek(tcp, PT_CR_IPSR, &ipsr) < 0) |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1816 | return -1; |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1817 | if (upeek(tcp, PT_CR_IIP, &addr) < 0) |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1818 | return -1; |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1819 | /* store "ri" in low two bits */ |
| 1820 | tcp->baddr = addr | ((ipsr >> 41) & 0x3); |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1821 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1822 | errno = 0; |
| 1823 | tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 0, |
| 1824 | 0); |
| 1825 | tcp->inst[1] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 8, |
| 1826 | 0); |
| 1827 | if (errno) { |
| 1828 | perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)"); |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1829 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1830 | } |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1831 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1832 | errno = 0; |
| 1833 | ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, LOOP0); |
| 1834 | ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, LOOP1); |
| 1835 | if (errno) { |
| 1836 | perror("setbpt: ptrace(PTRACE_POKETEXT, ...)"); |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1837 | return -1; |
| 1838 | } |
| 1839 | tcp->flags |= TCB_BPTSET; |
| 1840 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1841 | # else /* !IA64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1842 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1843 | # if defined (I386) || defined(X86_64) |
| 1844 | # define LOOP 0x0000feeb |
| 1845 | # elif defined (M68K) |
| 1846 | # define LOOP 0x60fe0000 |
| 1847 | # elif defined (ALPHA) |
| 1848 | # define LOOP 0xc3ffffff |
| 1849 | # elif defined (POWERPC) |
| 1850 | # define LOOP 0x48000000 |
| 1851 | # elif defined(ARM) |
| 1852 | # define LOOP 0xEAFFFFFE |
| 1853 | # elif defined(MIPS) |
| 1854 | # define LOOP 0x1000ffff |
| 1855 | # elif defined(S390) |
| 1856 | # define LOOP 0xa7f40000 /* BRC 15,0 */ |
| 1857 | # elif defined(S390X) |
| 1858 | # define LOOP 0xa7f4000000000000UL /* BRC 15,0 */ |
| 1859 | # elif defined(HPPA) |
| 1860 | # define LOOP 0xe81f1ff7 /* b,l,n <loc>,r0 */ |
| 1861 | # elif defined(SH) |
| 1862 | # ifdef __LITTLE_ENDIAN__ |
| 1863 | # define LOOP 0x0000affe |
| 1864 | # else |
| 1865 | # define LOOP 0xfeaf0000 |
| 1866 | # endif |
| 1867 | # else |
| 1868 | # error unknown architecture |
| 1869 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1870 | |
| 1871 | if (tcp->flags & TCB_BPTSET) { |
| 1872 | fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid); |
| 1873 | return -1; |
| 1874 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1875 | # if defined (I386) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1876 | if (upeek(tcp, 4*EIP, &tcp->baddr) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1877 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1878 | # elif defined (X86_64) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1879 | if (upeek(tcp, 8*RIP, &tcp->baddr) < 0) |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1880 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1881 | # elif defined (M68K) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1882 | if (upeek(tcp, 4*PT_PC, &tcp->baddr) < 0) |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1883 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1884 | # elif defined (ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1885 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1886 | # elif defined (ARM) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1887 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1888 | # elif defined (MIPS) |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1889 | return -1; /* FIXME: I do not know what i do - Flo */ |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1890 | # elif defined (POWERPC) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1891 | if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &tcp->baddr) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1892 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1893 | # elif defined(S390) || defined(S390X) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1894 | if (upeek(tcp,PT_PSWADDR, &tcp->baddr) < 0) |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1895 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1896 | # elif defined(HPPA) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1897 | if (upeek(tcp, PT_IAOQ0, &tcp->baddr) < 0) |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1898 | return -1; |
| 1899 | tcp->baddr &= ~0x03; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1900 | # elif defined(SH) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1901 | if (upeek(tcp, 4*REG_PC, &tcp->baddr) < 0) |
| 1902 | return -1; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1903 | # else |
| 1904 | # error unknown architecture |
| 1905 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1906 | if (debug) |
| 1907 | fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1908 | tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *) tcp->baddr, 0); |
| 1909 | if (errno) { |
| 1910 | perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1911 | return -1; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1912 | } |
| 1913 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP); |
| 1914 | if (errno) { |
| 1915 | perror("setbpt: ptrace(PTRACE_POKETEXT, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1916 | return -1; |
| 1917 | } |
| 1918 | tcp->flags |= TCB_BPTSET; |
| 1919 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1920 | # endif /* !IA64 */ |
| 1921 | # endif /* !SPARC && !SPARC64 */ |
| 1922 | # endif /* LINUX */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1923 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1924 | # ifdef SUNOS4 |
| 1925 | # ifdef SPARC /* This code is slightly sparc specific */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1926 | |
Wichert Akkerman | e6f876c | 1999-06-22 15:28:30 +0000 | [diff] [blame] | 1927 | struct regs regs; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1928 | # define BPT 0x91d02001 /* ta 1 */ |
| 1929 | # define LOOP 0x10800000 /* ba 0 */ |
| 1930 | # define LOOPA 0x30800000 /* ba,a 0 */ |
| 1931 | # define NOP 0x01000000 |
| 1932 | # if LOOPA |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1933 | static int loopdeloop[1] = {LOOPA}; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1934 | # else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1935 | static int loopdeloop[2] = {LOOP, NOP}; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1936 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1937 | |
| 1938 | if (tcp->flags & TCB_BPTSET) { |
| 1939 | fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid); |
| 1940 | return -1; |
| 1941 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1942 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 1943 | perror("setbpt: ptrace(PTRACE_GETREGS, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1944 | return -1; |
| 1945 | } |
| 1946 | tcp->baddr = regs.r_o7 + 8; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1947 | if (ptrace(PTRACE_READTEXT, tcp->pid, (char *)tcp->baddr, |
| 1948 | sizeof tcp->inst, (char *)tcp->inst) < 0) { |
| 1949 | perror("setbpt: ptrace(PTRACE_READTEXT, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1950 | return -1; |
| 1951 | } |
| 1952 | |
| 1953 | /* |
| 1954 | * XXX - BRUTAL MODE ON |
| 1955 | * We cannot set a real BPT in the child, since it will not be |
| 1956 | * traced at the moment it will reach the trap and would probably |
| 1957 | * die with a core dump. |
| 1958 | * Thus, we are force our way in by taking out two instructions |
| 1959 | * and insert an eternal loop in stead, in expectance of the SIGSTOP |
| 1960 | * generated by out PTRACE_ATTACH. |
| 1961 | * Of cause, if we evaporate ourselves in the middle of all this... |
| 1962 | */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1963 | if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr, |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1964 | sizeof loopdeloop, (char *) loopdeloop) < 0) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1965 | perror("setbpt: ptrace(PTRACE_WRITETEXT, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1966 | return -1; |
| 1967 | } |
| 1968 | tcp->flags |= TCB_BPTSET; |
| 1969 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1970 | # endif /* SPARC */ |
| 1971 | # endif /* SUNOS4 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1972 | |
| 1973 | return 0; |
| 1974 | } |
| 1975 | |
| 1976 | int |
| 1977 | clearbpt(tcp) |
| 1978 | struct tcb *tcp; |
| 1979 | { |
| 1980 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1981 | # ifdef LINUX |
| 1982 | DEAD CODE HERE? WE ARE IN 'else !defined LINUX' |
| 1983 | # if defined(I386) || defined(X86_64) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1984 | long eip; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1985 | # elif defined(POWERPC) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1986 | long pc; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1987 | # elif defined(M68K) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1988 | long pc; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1989 | # elif defined(ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1990 | long pc; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1991 | # elif defined(HPPA) |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1992 | long iaoq; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1993 | # elif defined(SH) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1994 | long pc; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1995 | # endif /* architecture */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1996 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 1997 | # if defined (SPARC) || defined (SPARC64) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1998 | /* Again, we borrow the SunOS breakpoint code. */ |
| 1999 | if (!(tcp->flags & TCB_BPTSET)) { |
| 2000 | fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid); |
| 2001 | return -1; |
| 2002 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2003 | errno = 0; |
| 2004 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]); |
| 2005 | if(errno) { |
| 2006 | perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2007 | return -1; |
| 2008 | } |
| 2009 | tcp->flags &= ~TCB_BPTSET; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2010 | # elif defined(IA64) |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2011 | if (ia32) { |
| 2012 | unsigned long addr; |
| 2013 | |
| 2014 | if (debug) |
| 2015 | fprintf(stderr, "[%d] clearing bpt\n", tcp->pid); |
| 2016 | if (!(tcp->flags & TCB_BPTSET)) { |
| 2017 | fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid); |
| 2018 | return -1; |
| 2019 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2020 | errno = 0; |
| 2021 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]); |
| 2022 | if (errno) { |
| 2023 | perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)"); |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2024 | return -1; |
| 2025 | } |
| 2026 | tcp->flags &= ~TCB_BPTSET; |
| 2027 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2028 | if (upeek(tcp, PT_CR_IIP, &addr) < 0) |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 2029 | return -1; |
| 2030 | if (addr != tcp->baddr) { |
| 2031 | /* The breakpoint has not been reached yet. */ |
| 2032 | if (debug) |
| 2033 | fprintf(stderr, |
| 2034 | "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 2035 | addr, tcp->baddr); |
| 2036 | return 0; |
| 2037 | } |
| 2038 | } else { |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 2039 | unsigned long addr, ipsr; |
| 2040 | pid_t pid; |
| 2041 | |
| 2042 | pid = tcp->pid; |
| 2043 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2044 | if (upeek(tcp, PT_CR_IPSR, &ipsr) < 0) |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 2045 | return -1; |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2046 | if (upeek(tcp, PT_CR_IIP, &addr) < 0) |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 2047 | return -1; |
| 2048 | |
| 2049 | /* restore original bundle: */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2050 | errno = 0; |
| 2051 | ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, tcp->inst[0]); |
| 2052 | ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, tcp->inst[1]); |
| 2053 | if (errno) { |
| 2054 | perror("clearbpt: ptrace(PTRACE_POKETEXT, ...)"); |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 2055 | return -1; |
| 2056 | } |
| 2057 | |
| 2058 | /* restore original "ri" in ipsr: */ |
| 2059 | ipsr = (ipsr & ~(0x3ul << 41)) | ((tcp->baddr & 0x3) << 41); |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2060 | errno = 0; |
| 2061 | ptrace(PTRACE_POKEUSER, pid, (char *) PT_CR_IPSR, ipsr); |
| 2062 | if (errno) { |
| 2063 | perror("clrbpt: ptrace(PTRACE_POKEUSER, ...)"); |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 2064 | return -1; |
| 2065 | } |
| 2066 | |
| 2067 | tcp->flags &= ~TCB_BPTSET; |
| 2068 | |
| 2069 | if (addr != (tcp->baddr & ~0x3)) { |
| 2070 | /* the breakpoint has not been reached yet. */ |
| 2071 | if (debug) |
| 2072 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 2073 | addr, tcp->baddr); |
| 2074 | return 0; |
| 2075 | } |
| 2076 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2077 | # else /* !IA64 && !SPARC && !SPARC64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2078 | |
| 2079 | if (debug) |
| 2080 | fprintf(stderr, "[%d] clearing bpt\n", tcp->pid); |
| 2081 | if (!(tcp->flags & TCB_BPTSET)) { |
| 2082 | fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid); |
| 2083 | return -1; |
| 2084 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2085 | errno = 0; |
| 2086 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]); |
| 2087 | if (errno) { |
| 2088 | perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2089 | return -1; |
| 2090 | } |
| 2091 | tcp->flags &= ~TCB_BPTSET; |
| 2092 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2093 | # ifdef I386 |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2094 | if (upeek(tcp, 4*EIP, &eip) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2095 | return -1; |
| 2096 | if (eip != tcp->baddr) { |
| 2097 | /* The breakpoint has not been reached yet. */ |
| 2098 | if (debug) |
| 2099 | fprintf(stderr, |
| 2100 | "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 2101 | eip, tcp->baddr); |
| 2102 | return 0; |
| 2103 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2104 | # elif defined(X86_64) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2105 | if (upeek(tcp, 8*RIP, &eip) < 0) |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 2106 | return -1; |
| 2107 | if (eip != tcp->baddr) { |
| 2108 | /* The breakpoint has not been reached yet. */ |
| 2109 | if (debug) |
| 2110 | fprintf(stderr, |
| 2111 | "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 2112 | eip, tcp->baddr); |
| 2113 | return 0; |
| 2114 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2115 | # elif defined(POWERPC) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2116 | if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2117 | return -1; |
| 2118 | if (pc != tcp->baddr) { |
| 2119 | /* The breakpoint has not been reached yet. */ |
| 2120 | if (debug) |
| 2121 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 2122 | pc, tcp->baddr); |
| 2123 | return 0; |
| 2124 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2125 | # elif defined(M68K) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2126 | if (upeek(tcp, 4*PT_PC, &pc) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2127 | return -1; |
| 2128 | if (pc != tcp->baddr) { |
| 2129 | /* The breakpoint has not been reached yet. */ |
| 2130 | if (debug) |
| 2131 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 2132 | pc, tcp->baddr); |
| 2133 | return 0; |
| 2134 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2135 | # elif defined(ALPHA) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2136 | if (upeek(tcp, REG_PC, &pc) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2137 | return -1; |
| 2138 | if (pc != tcp->baddr) { |
| 2139 | /* The breakpoint has not been reached yet. */ |
| 2140 | if (debug) |
| 2141 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 2142 | pc, tcp->baddr); |
| 2143 | return 0; |
| 2144 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2145 | # elif defined(HPPA) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2146 | if (upeek(tcp, PT_IAOQ0, &iaoq) < 0) |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 2147 | return -1; |
| 2148 | iaoq &= ~0x03; |
| 2149 | if (iaoq != tcp->baddr && iaoq != tcp->baddr + 4) { |
| 2150 | /* The breakpoint has not been reached yet. */ |
| 2151 | if (debug) |
| 2152 | fprintf(stderr, "NOTE: PC not at bpt (iaoq %#lx baddr %#lx)\n", |
| 2153 | iaoq, tcp->baddr); |
| 2154 | return 0; |
| 2155 | } |
| 2156 | iaoq = tcp->baddr | 3; |
| 2157 | /* We should be pointing at a 'ldi -1000,r1' in glibc, so it is |
| 2158 | * safe to set both IAOQ0 and IAOQ1 to that so the PSW N bit |
| 2159 | * has no significant effect. |
| 2160 | */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2161 | ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ0, iaoq); |
| 2162 | ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ1, iaoq); |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2163 | # elif defined(SH) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 2164 | if (upeek(tcp, 4*REG_PC, &pc) < 0) |
| 2165 | return -1; |
| 2166 | if (pc != tcp->baddr) { |
| 2167 | /* The breakpoint has not been reached yet. */ |
| 2168 | if (debug) |
| 2169 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 2170 | pc, tcp->baddr); |
| 2171 | return 0; |
| 2172 | } |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 2173 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2174 | # endif /* arch */ |
| 2175 | # endif /* !SPARC && !SPARC64 && !IA64 */ |
| 2176 | # endif /* LINUX */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2177 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2178 | # ifdef SUNOS4 |
| 2179 | # ifdef SPARC |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2180 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2181 | # if !LOOPA |
Wichert Akkerman | e6f876c | 1999-06-22 15:28:30 +0000 | [diff] [blame] | 2182 | struct regs regs; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2183 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2184 | |
| 2185 | if (!(tcp->flags & TCB_BPTSET)) { |
| 2186 | fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid); |
| 2187 | return -1; |
| 2188 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2189 | if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr, |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2190 | sizeof tcp->inst, (char *) tcp->inst) < 0) { |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2191 | perror("clearbtp: ptrace(PTRACE_WRITETEXT, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2192 | return -1; |
| 2193 | } |
| 2194 | tcp->flags &= ~TCB_BPTSET; |
| 2195 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2196 | # if !LOOPA |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2197 | /* |
| 2198 | * Since we don't have a single instruction breakpoint, we may have |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2199 | * to adjust the program counter after removing our `breakpoint'. |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2200 | */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2201 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 2202 | perror("clearbpt: ptrace(PTRACE_GETREGS, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2203 | return -1; |
| 2204 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2205 | if ((regs.r_pc < tcp->baddr) || |
| 2206 | (regs.r_pc > tcp->baddr + 4)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2207 | /* The breakpoint has not been reached yet */ |
| 2208 | if (debug) |
| 2209 | fprintf(stderr, |
| 2210 | "NOTE: PC not at bpt (pc %#x baddr %#x)\n", |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2211 | regs.r_pc, tcp->baddr); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2212 | return 0; |
| 2213 | } |
| 2214 | if (regs.r_pc != tcp->baddr) |
| 2215 | if (debug) |
| 2216 | fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n", |
| 2217 | regs.r_pc, tcp->baddr); |
| 2218 | |
| 2219 | regs.r_pc = tcp->baddr; |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2220 | if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 2221 | perror("clearbpt: ptrace(PTRACE_SETREGS, ...)"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2222 | return -1; |
| 2223 | } |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2224 | # endif /* LOOPA */ |
| 2225 | # endif /* SPARC */ |
| 2226 | # endif /* SUNOS4 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2227 | |
| 2228 | return 0; |
| 2229 | } |
| 2230 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2231 | # endif /* !defined LINUX */ |
Roland McGrath | d81f1d9 | 2003-01-09 06:53:34 +0000 | [diff] [blame] | 2232 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2233 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2234 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2235 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2236 | #ifdef SUNOS4 |
| 2237 | |
| 2238 | static int |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2239 | getex(tcp, hdr) |
| 2240 | struct tcb *tcp; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2241 | struct exec *hdr; |
| 2242 | { |
| 2243 | int n; |
| 2244 | |
| 2245 | for (n = 0; n < sizeof *hdr; n += 4) { |
| 2246 | long res; |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2247 | if (upeek(tcp, uoff(u_exdata) + n, &res) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2248 | return -1; |
| 2249 | memcpy(((char *) hdr) + n, &res, 4); |
| 2250 | } |
| 2251 | if (debug) { |
| 2252 | fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n", |
| 2253 | hdr->a_magic, hdr->a_toolversion, hdr->a_machtype); |
| 2254 | fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n", |
| 2255 | hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry); |
| 2256 | } |
| 2257 | return 0; |
| 2258 | } |
| 2259 | |
| 2260 | int |
| 2261 | fixvfork(tcp) |
| 2262 | struct tcb *tcp; |
| 2263 | { |
| 2264 | int pid = tcp->pid; |
| 2265 | /* |
| 2266 | * Change `vfork' in a freshly exec'ed dynamically linked |
| 2267 | * executable's (internal) symbol table to plain old `fork' |
| 2268 | */ |
| 2269 | |
| 2270 | struct exec hdr; |
| 2271 | struct link_dynamic dyn; |
| 2272 | struct link_dynamic_2 ld; |
| 2273 | char *strtab, *cp; |
| 2274 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2275 | if (getex(tcp, &hdr) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2276 | return -1; |
| 2277 | if (!hdr.a_dynamic) |
| 2278 | return -1; |
| 2279 | |
| 2280 | if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) { |
| 2281 | fprintf(stderr, "Cannot read DYNAMIC\n"); |
| 2282 | return -1; |
| 2283 | } |
| 2284 | if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) { |
| 2285 | fprintf(stderr, "Cannot read link_dynamic_2\n"); |
| 2286 | return -1; |
| 2287 | } |
| 2288 | if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) { |
Roland McGrath | 46100d0 | 2005-06-01 18:55:42 +0000 | [diff] [blame] | 2289 | fprintf(stderr, "out of memory\n"); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2290 | return -1; |
| 2291 | } |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2292 | if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr), |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2293 | (int)ld.ld_symb_size, strtab) < 0) |
| 2294 | goto err; |
| 2295 | |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2296 | # if 0 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2297 | for (cp = strtab; cp < strtab + ld.ld_symb_size; ) { |
| 2298 | fprintf(stderr, "[symbol: %s]\n", cp); |
| 2299 | cp += strlen(cp)+1; |
| 2300 | } |
| 2301 | return 0; |
Denys Vlasenko | 3bb7cd6 | 2009-02-09 18:55:59 +0000 | [diff] [blame] | 2302 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2303 | for (cp = strtab; cp < strtab + ld.ld_symb_size; ) { |
| 2304 | if (strcmp(cp, "_vfork") == 0) { |
| 2305 | if (debug) |
| 2306 | fprintf(stderr, "fixvfork: FOUND _vfork\n"); |
| 2307 | strcpy(cp, "_fork"); |
| 2308 | break; |
| 2309 | } |
| 2310 | cp += strlen(cp)+1; |
| 2311 | } |
| 2312 | if (cp < strtab + ld.ld_symb_size) |
| 2313 | /* |
| 2314 | * Write entire symbol table back to avoid |
| 2315 | * memory alignment bugs in ptrace |
| 2316 | */ |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2317 | if (tload(pid, (int)ld.ld_symbols+(int)N_TXTADDR(hdr), |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2318 | (int)ld.ld_symb_size, strtab) < 0) |
| 2319 | goto err; |
| 2320 | |
| 2321 | free(strtab); |
| 2322 | return 0; |
| 2323 | |
| 2324 | err: |
| 2325 | free(strtab); |
| 2326 | return -1; |
| 2327 | } |
| 2328 | |
| 2329 | #endif /* SUNOS4 */ |