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 | |
| 38 | #include <sys/user.h> |
| 39 | #include <sys/param.h> |
| 40 | #include <fcntl.h> |
| 41 | #ifdef SUNOS4 |
| 42 | #include <machine/reg.h> |
| 43 | #include <a.out.h> |
| 44 | #include <link.h> |
| 45 | #endif /* SUNOS4 */ |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 46 | |
Wichert Akkerman | 43a7482 | 2000-06-27 17:33:32 +0000 | [diff] [blame] | 47 | #if defined(linux) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 1)) |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 48 | #include <linux/ptrace.h> |
| 49 | #endif |
| 50 | |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 51 | #if defined(LINUX) && defined(IA64) |
| 52 | #include <asm/ptrace_offsets.h> |
| 53 | #endif |
| 54 | |
Wichert Akkerman | 36915a1 | 1999-07-13 15:45:02 +0000 | [diff] [blame] | 55 | #ifdef HAVE_SYS_REG_H |
| 56 | #include <sys/reg.h> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 57 | # define PTRACE_PEEKUSR PTRACE_PEEKUSER |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 58 | #elif defined(HAVE_LINUX_PTRACE_H) |
| 59 | #undef PTRACE_SYSCALL |
| 60 | #include <linux/ptrace.h> |
Wichert Akkerman | 2e2553a | 1999-05-09 00:29:58 +0000 | [diff] [blame] | 61 | #endif |
| 62 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 63 | #ifdef SUNOS4_KERNEL_ARCH_KLUDGE |
| 64 | #include <sys/utsname.h> |
| 65 | #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */ |
| 66 | |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 67 | #if defined(LINUX) && defined(SPARC) |
| 68 | |
| 69 | #include <asm/reg.h> |
| 70 | |
| 71 | #if !defined(__GLIBC__) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 72 | |
| 73 | #include <linux/unistd.h> |
| 74 | |
| 75 | #define _hack_syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,\ |
| 76 | type5,arg5,syscall) \ |
| 77 | type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \ |
| 78 | { \ |
| 79 | long __res; \ |
| 80 | \ |
| 81 | __asm__ volatile ("or %%g0, %1, %%o0\n\t" \ |
| 82 | "or %%g0, %2, %%o1\n\t" \ |
| 83 | "or %%g0, %3, %%o2\n\t" \ |
| 84 | "or %%g0, %4, %%o3\n\t" \ |
| 85 | "or %%g0, %5, %%o4\n\t" \ |
| 86 | "or %%g0, %6, %%g1\n\t" \ |
| 87 | "t 0x10\n\t" \ |
| 88 | "bcc 1f\n\t" \ |
| 89 | "or %%g0, %%o0, %0\n\t" \ |
| 90 | "sub %%g0, %%o0, %0\n\t" \ |
| 91 | "1:\n\t" \ |
| 92 | : "=r" (__res) \ |
| 93 | : "0" ((long)(arg1)),"1" ((long)(arg2)), \ |
| 94 | "2" ((long)(arg3)),"3" ((long)(arg4)),"4" ((long)(arg5)), \ |
| 95 | "i" (__NR_##syscall) \ |
| 96 | : "g1", "o0", "o1", "o2", "o3", "o4"); \ |
| 97 | if (__res>=0) \ |
| 98 | return (type) __res; \ |
| 99 | errno = -__res; \ |
| 100 | return -1; \ |
| 101 | } |
| 102 | |
| 103 | static _hack_syscall5(int,_ptrace,int,__request,int,__pid,int,__addr,int,__data,int,__addr2,ptrace) |
| 104 | |
| 105 | #define _ptrace |
| 106 | |
| 107 | #endif |
| 108 | |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 109 | #endif |
| 110 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 111 | /* macros */ |
| 112 | #ifndef MAX |
| 113 | #define MAX(a,b) (((a) > (b)) ? (a) : (b)) |
| 114 | #endif |
| 115 | #ifndef MIN |
| 116 | #define MIN(a,b) (((a) < (b)) ? (a) : (b)) |
| 117 | #endif |
| 118 | |
| 119 | void |
| 120 | tv_tv(tv, a, b) |
| 121 | struct timeval *tv; |
| 122 | int a; |
| 123 | int b; |
| 124 | { |
| 125 | tv->tv_sec = a; |
| 126 | tv->tv_usec = b; |
| 127 | } |
| 128 | |
| 129 | int |
| 130 | tv_nz(a) |
| 131 | struct timeval *a; |
| 132 | { |
| 133 | return a->tv_sec || a->tv_usec; |
| 134 | } |
| 135 | |
| 136 | int |
| 137 | tv_cmp(a, b) |
| 138 | struct timeval *a, *b; |
| 139 | { |
| 140 | if (a->tv_sec < b->tv_sec |
| 141 | || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec)) |
| 142 | return -1; |
| 143 | if (a->tv_sec > b->tv_sec |
| 144 | || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec)) |
| 145 | return 1; |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | double |
| 150 | tv_float(tv) |
| 151 | struct timeval *tv; |
| 152 | { |
| 153 | return tv->tv_sec + tv->tv_usec/1000000.0; |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | tv_add(tv, a, b) |
| 158 | struct timeval *tv, *a, *b; |
| 159 | { |
| 160 | tv->tv_sec = a->tv_sec + b->tv_sec; |
| 161 | tv->tv_usec = a->tv_usec + b->tv_usec; |
| 162 | if (tv->tv_usec > 1000000) { |
| 163 | tv->tv_sec++; |
| 164 | tv->tv_usec -= 1000000; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | tv_sub(tv, a, b) |
| 170 | struct timeval *tv, *a, *b; |
| 171 | { |
| 172 | tv->tv_sec = a->tv_sec - b->tv_sec; |
| 173 | tv->tv_usec = a->tv_usec - b->tv_usec; |
| 174 | if (((long) tv->tv_usec) < 0) { |
| 175 | tv->tv_sec--; |
| 176 | tv->tv_usec += 1000000; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | tv_div(tv, a, n) |
| 182 | struct timeval *tv, *a; |
| 183 | int n; |
| 184 | { |
| 185 | tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n; |
| 186 | tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000; |
| 187 | tv->tv_usec %= 1000000; |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | tv_mul(tv, a, n) |
| 192 | struct timeval *tv, *a; |
| 193 | int n; |
| 194 | { |
| 195 | tv->tv_usec = a->tv_usec * n; |
| 196 | tv->tv_sec = a->tv_sec * n + a->tv_usec / 1000000; |
| 197 | tv->tv_usec %= 1000000; |
| 198 | } |
| 199 | |
| 200 | char * |
| 201 | xlookup(xlat, val) |
| 202 | struct xlat *xlat; |
| 203 | int val; |
| 204 | { |
| 205 | for (; xlat->str != NULL; xlat++) |
| 206 | if (xlat->val == val) |
| 207 | return xlat->str; |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Print entry in struct xlat table, if there. |
| 213 | */ |
| 214 | void |
| 215 | printxval(xlat, val, dflt) |
| 216 | struct xlat *xlat; |
| 217 | int val; |
| 218 | char *dflt; |
| 219 | { |
| 220 | char *str = xlookup(xlat, val); |
| 221 | |
| 222 | if (str) |
| 223 | tprintf("%s", str); |
| 224 | else |
| 225 | tprintf("%#x /* %s */", val, dflt); |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Interpret `xlat' as an array of flags |
| 230 | * print the entries whose bits are on in `flags' |
| 231 | * return # of flags printed. |
| 232 | */ |
| 233 | int |
| 234 | addflags(xlat, flags) |
| 235 | struct xlat *xlat; |
| 236 | int flags; |
| 237 | { |
| 238 | int n; |
| 239 | |
| 240 | for (n = 0; xlat->str; xlat++) { |
| 241 | if (xlat->val && (flags & xlat->val) == xlat->val) { |
| 242 | tprintf("|%s", xlat->str); |
| 243 | flags &= ~xlat->val; |
| 244 | n++; |
| 245 | } |
| 246 | } |
| 247 | if (flags) { |
| 248 | tprintf("|%#x", flags); |
| 249 | n++; |
| 250 | } |
| 251 | return n; |
| 252 | } |
| 253 | |
| 254 | int |
| 255 | printflags(xlat, flags) |
| 256 | struct xlat *xlat; |
| 257 | int flags; |
| 258 | { |
| 259 | int n; |
| 260 | char *sep; |
| 261 | |
| 262 | if (flags == 0 && xlat->val == 0) { |
| 263 | tprintf("%s", xlat->str); |
| 264 | return 1; |
| 265 | } |
| 266 | |
| 267 | sep = ""; |
| 268 | for (n = 0; xlat->str; xlat++) { |
| 269 | if (xlat->val && (flags & xlat->val) == xlat->val) { |
| 270 | tprintf("%s%s", sep, xlat->str); |
| 271 | flags &= ~xlat->val; |
| 272 | sep = "|"; |
| 273 | n++; |
| 274 | } |
| 275 | } |
| 276 | if (flags) { |
| 277 | tprintf("%s%#x", sep, flags); |
| 278 | n++; |
| 279 | } |
| 280 | return n; |
| 281 | } |
| 282 | |
| 283 | void |
| 284 | printnum(tcp, addr, fmt) |
| 285 | struct tcb *tcp; |
| 286 | long addr; |
| 287 | char *fmt; |
| 288 | { |
| 289 | int num; |
| 290 | |
| 291 | if (!addr) { |
| 292 | tprintf("NULL"); |
| 293 | return; |
| 294 | } |
| 295 | if (umove(tcp, addr, &num) < 0) { |
| 296 | tprintf("%#lx", addr); |
| 297 | return; |
| 298 | } |
| 299 | tprintf("["); |
| 300 | tprintf(fmt, num); |
| 301 | tprintf("]"); |
| 302 | } |
| 303 | |
| 304 | static char path[MAXPATHLEN + 1]; |
| 305 | |
| 306 | void |
| 307 | string_quote(str) |
| 308 | char *str; |
| 309 | { |
| 310 | char buf[2 * MAXPATHLEN + 1]; |
| 311 | char *s; |
| 312 | |
| 313 | if (!strpbrk(str, "\"\'\\")) { |
| 314 | tprintf("\"%s\"", str); |
| 315 | return; |
| 316 | } |
| 317 | for (s = buf; *str; str++) { |
| 318 | switch (*str) { |
| 319 | case '\"': case '\'': case '\\': |
| 320 | *s++ = '\\'; *s++ = *str; break; |
| 321 | default: |
| 322 | *s++ = *str; break; |
| 323 | } |
| 324 | } |
| 325 | *s = '\0'; |
| 326 | tprintf("\"%s\"", buf); |
| 327 | } |
| 328 | |
| 329 | void |
| 330 | printpath(tcp, addr) |
| 331 | struct tcb *tcp; |
| 332 | long addr; |
| 333 | { |
| 334 | if (umovestr(tcp, addr, MAXPATHLEN, path) < 0) |
| 335 | tprintf("%#lx", addr); |
| 336 | else |
| 337 | string_quote(path); |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | void |
| 342 | printpathn(tcp, addr, n) |
| 343 | struct tcb *tcp; |
| 344 | long addr; |
| 345 | int n; |
| 346 | { |
| 347 | if (umovestr(tcp, addr, n, path) < 0) |
| 348 | tprintf("%#lx", addr); |
| 349 | else { |
| 350 | path[n] = '\0'; |
| 351 | string_quote(path); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | void |
| 356 | printstr(tcp, addr, len) |
| 357 | struct tcb *tcp; |
| 358 | long addr; |
| 359 | int len; |
| 360 | { |
| 361 | static unsigned char *str = NULL; |
| 362 | static char *outstr; |
| 363 | int i, n, c, usehex; |
| 364 | char *s, *outend; |
| 365 | |
| 366 | if (!addr) { |
| 367 | tprintf("NULL"); |
| 368 | return; |
| 369 | } |
| 370 | if (!str) { |
| 371 | if ((str = malloc(max_strlen)) == NULL |
| 372 | || (outstr = malloc(2*max_strlen)) == NULL) { |
| 373 | fprintf(stderr, "printstr: no memory\n"); |
| 374 | tprintf("%#lx", addr); |
| 375 | return; |
| 376 | } |
| 377 | } |
Wichert Akkerman | 2e2553a | 1999-05-09 00:29:58 +0000 | [diff] [blame] | 378 | outend = outstr + max_strlen * 2 - 10; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 379 | if (len < 0) { |
| 380 | n = max_strlen; |
| 381 | if (umovestr(tcp, addr, n, (char *) str) < 0) { |
| 382 | tprintf("%#lx", addr); |
| 383 | return; |
| 384 | } |
| 385 | } |
| 386 | else { |
| 387 | n = MIN(len, max_strlen); |
| 388 | if (umoven(tcp, addr, n, (char *) str) < 0) { |
| 389 | tprintf("%#lx", addr); |
| 390 | return; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | usehex = 0; |
| 395 | if (xflag > 1) |
| 396 | usehex = 1; |
| 397 | else if (xflag) { |
| 398 | for (i = 0; i < n; i++) { |
| 399 | c = str[i]; |
| 400 | if (len < 0 && c == '\0') |
| 401 | break; |
| 402 | if (!isprint(c) && !isspace(c)) { |
| 403 | usehex = 1; |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | s = outstr; |
| 410 | *s++ = '\"'; |
| 411 | |
| 412 | if (usehex) { |
| 413 | for (i = 0; i < n; i++) { |
| 414 | c = str[i]; |
| 415 | if (len < 0 && c == '\0') |
| 416 | break; |
| 417 | sprintf(s, "\\x%02x", c); |
| 418 | s += 4; |
| 419 | if (s > outend) |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | else { |
| 424 | for (i = 0; i < n; i++) { |
| 425 | c = str[i]; |
| 426 | if (len < 0 && c == '\0') |
| 427 | break; |
| 428 | switch (c) { |
| 429 | case '\"': case '\'': case '\\': |
| 430 | *s++ = '\\'; *s++ = c; break; |
| 431 | case '\f': |
| 432 | *s++ = '\\'; *s++ = 'f'; break; |
| 433 | case '\n': |
| 434 | *s++ = '\\'; *s++ = 'n'; break; |
| 435 | case '\r': |
| 436 | *s++ = '\\'; *s++ = 'r'; break; |
| 437 | case '\t': |
| 438 | *s++ = '\\'; *s++ = 't'; break; |
| 439 | case '\v': |
| 440 | *s++ = '\\'; *s++ = 'v'; break; |
| 441 | default: |
| 442 | if (isprint(c)) |
| 443 | *s++ = c; |
| 444 | else if (i < n - 1 && isdigit(str[i + 1])) { |
| 445 | sprintf(s, "\\%03o", c); |
| 446 | s += 4; |
| 447 | } |
| 448 | else { |
| 449 | sprintf(s, "\\%o", c); |
| 450 | s += strlen(s); |
| 451 | } |
| 452 | break; |
| 453 | } |
| 454 | if (s > outend) |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | *s++ = '\"'; |
| 460 | if (i < len || (len < 0 && (i == n || s > outend))) { |
| 461 | *s++ = '.'; *s++ = '.'; *s++ = '.'; |
| 462 | } |
| 463 | *s = '\0'; |
| 464 | tprintf("%s", outstr); |
| 465 | } |
| 466 | |
| 467 | void |
| 468 | dumpstr(tcp, addr, len) |
| 469 | struct tcb *tcp; |
| 470 | long addr; |
| 471 | int len; |
| 472 | { |
| 473 | static int strsize = -1; |
| 474 | static unsigned char *str; |
| 475 | static char outstr[80]; |
| 476 | char *s; |
| 477 | int i, j; |
| 478 | |
| 479 | if (strsize < len) { |
| 480 | if (str) |
| 481 | free(str); |
| 482 | if ((str = malloc(len)) == NULL) { |
| 483 | fprintf(stderr, "dump: no memory\n"); |
| 484 | return; |
| 485 | } |
| 486 | strsize = len; |
| 487 | } |
| 488 | |
| 489 | if (umoven(tcp, addr, len, (char *) str) < 0) |
| 490 | return; |
| 491 | |
| 492 | for (i = 0; i < len; i += 16) { |
| 493 | s = outstr; |
| 494 | sprintf(s, " | %05x ", i); |
| 495 | s += 9; |
| 496 | for (j = 0; j < 16; j++) { |
| 497 | if (j == 8) |
| 498 | *s++ = ' '; |
| 499 | if (i + j < len) { |
| 500 | sprintf(s, " %02x", str[i + j]); |
| 501 | s += 3; |
| 502 | } |
| 503 | else { |
| 504 | *s++ = ' '; *s++ = ' '; *s++ = ' '; |
| 505 | } |
| 506 | } |
| 507 | *s++ = ' '; *s++ = ' '; |
| 508 | for (j = 0; j < 16; j++) { |
| 509 | if (j == 8) |
| 510 | *s++ = ' '; |
| 511 | if (i + j < len) { |
| 512 | if (isprint(str[i + j])) |
| 513 | *s++ = str[i + j]; |
| 514 | else |
| 515 | *s++ = '.'; |
| 516 | } |
| 517 | else |
| 518 | *s++ = ' '; |
| 519 | } |
| 520 | tprintf("%s |\n", outstr); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | #define PAGMASK (~(PAGSIZ - 1)) |
| 525 | /* |
| 526 | * move `len' bytes of data from process `pid' |
| 527 | * at address `addr' to our space at `laddr' |
| 528 | */ |
| 529 | int |
| 530 | umoven(tcp, addr, len, laddr) |
| 531 | struct tcb *tcp; |
| 532 | long addr; |
| 533 | int len; |
| 534 | char *laddr; |
| 535 | { |
| 536 | |
| 537 | #ifdef LINUX |
| 538 | int pid = tcp->pid; |
| 539 | int n, m; |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 540 | int started = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 541 | union { |
| 542 | long val; |
| 543 | char x[sizeof(long)]; |
| 544 | } u; |
| 545 | |
| 546 | if (addr & (sizeof(long) - 1)) { |
| 547 | /* addr not a multiple of sizeof(long) */ |
| 548 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 549 | addr &= -sizeof(long); /* residue */ |
| 550 | errno = 0; |
| 551 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 552 | if (errno) { |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 553 | if (started && (errno==EPERM || errno==EIO)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 554 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 555 | return 0; |
| 556 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 557 | /* But if not started, we had a bogus address. */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 558 | perror("ptrace: umoven"); |
| 559 | return -1; |
| 560 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 561 | started = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 562 | memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len)); |
| 563 | addr += sizeof(long), laddr += m, len -= m; |
| 564 | } |
| 565 | while (len) { |
| 566 | errno = 0; |
| 567 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 568 | if (errno) { |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 569 | if (started && (errno==EPERM || errno==EIO)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 570 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 571 | return 0; |
| 572 | } |
| 573 | perror("ptrace: umoven"); |
| 574 | return -1; |
| 575 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 576 | started = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 577 | memcpy(laddr, u.x, m = MIN(sizeof(long), len)); |
| 578 | addr += sizeof(long), laddr += m, len -= m; |
| 579 | } |
| 580 | #endif /* LINUX */ |
| 581 | |
| 582 | #ifdef SUNOS4 |
| 583 | int pid = tcp->pid; |
| 584 | #if 0 |
| 585 | int n, m; |
| 586 | union { |
| 587 | long val; |
| 588 | char x[sizeof(long)]; |
| 589 | } u; |
| 590 | |
| 591 | if (addr & (sizeof(long) - 1)) { |
| 592 | /* addr not a multiple of sizeof(long) */ |
| 593 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 594 | addr &= -sizeof(long); /* residue */ |
| 595 | errno = 0; |
| 596 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 597 | if (errno) { |
| 598 | perror("umoven"); |
| 599 | return -1; |
| 600 | } |
| 601 | memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len)); |
| 602 | addr += sizeof(long), laddr += m, len -= m; |
| 603 | } |
| 604 | while (len) { |
| 605 | errno = 0; |
| 606 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0); |
| 607 | if (errno) { |
| 608 | perror("umoven"); |
| 609 | return -1; |
| 610 | } |
| 611 | memcpy(laddr, u.x, m = MIN(sizeof(long), len)); |
| 612 | addr += sizeof(long), laddr += m, len -= m; |
| 613 | } |
| 614 | #else /* !oldway */ |
| 615 | int n; |
| 616 | |
| 617 | while (len) { |
| 618 | n = MIN(len, PAGSIZ); |
| 619 | n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr); |
| 620 | if (ptrace(PTRACE_READDATA, pid, |
| 621 | (char *) addr, len, laddr) < 0) { |
| 622 | perror("umoven: ptrace(PTRACE_READDATA, ...)"); |
| 623 | abort(); |
| 624 | return -1; |
| 625 | } |
| 626 | len -= n; |
| 627 | addr += n; |
| 628 | laddr += n; |
| 629 | } |
| 630 | #endif /* !oldway */ |
| 631 | #endif /* SUNOS4 */ |
| 632 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 633 | #ifdef USE_PROCFS |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 634 | #ifdef HAVE_MP_PROCFS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 635 | if (pread(tcp->pfd_as, laddr, len, addr) == -1) |
| 636 | return -1; |
| 637 | #else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 638 | /* |
| 639 | * We would like to use pread preferentially for speed |
| 640 | * but even though SGI has it in their library, it no longer works. |
| 641 | */ |
| 642 | #ifdef MIPS |
| 643 | #undef HAVE_PREAD |
| 644 | #endif |
| 645 | #ifdef HAVE_PREAD |
| 646 | if (pread(tcp->pfd, laddr, len, addr) == -1) |
| 647 | return -1; |
| 648 | #else /* !HAVE_PREAD */ |
| 649 | lseek(tcp->pfd, addr, SEEK_SET); |
| 650 | if (read(tcp->pfd, laddr, len) == -1) |
| 651 | return -1; |
| 652 | #endif /* !HAVE_PREAD */ |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 653 | #endif /* HAVE_MP_PROCFS */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 654 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | /* |
| 660 | * like `umove' but make the additional effort of looking |
| 661 | * for a terminating zero byte. |
| 662 | */ |
| 663 | int |
| 664 | umovestr(tcp, addr, len, laddr) |
| 665 | struct tcb *tcp; |
| 666 | long addr; |
| 667 | int len; |
| 668 | char *laddr; |
| 669 | { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 670 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 671 | return umoven(tcp, addr, len, laddr); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 672 | #else /* !USE_PROCFS */ |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 673 | int started = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 674 | int pid = tcp->pid; |
| 675 | int i, n, m; |
| 676 | union { |
| 677 | long val; |
| 678 | char x[sizeof(long)]; |
| 679 | } u; |
| 680 | |
| 681 | if (addr & (sizeof(long) - 1)) { |
| 682 | /* addr not a multiple of sizeof(long) */ |
| 683 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 684 | addr &= -sizeof(long); /* residue */ |
| 685 | errno = 0; |
| 686 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0); |
| 687 | if (errno) { |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 688 | if (started && (errno==EPERM || errno==EIO)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 689 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 690 | return 0; |
| 691 | } |
| 692 | perror("umovestr"); |
| 693 | return -1; |
| 694 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 695 | started = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 696 | memcpy(laddr, &u.x[n], m = MIN(sizeof(long)-n,len)); |
| 697 | while (n & (sizeof(long) - 1)) |
| 698 | if (u.x[n++] == '\0') |
| 699 | return 0; |
| 700 | addr += sizeof(long), laddr += m, len -= m; |
| 701 | } |
| 702 | while (len) { |
| 703 | errno = 0; |
| 704 | u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0); |
| 705 | if (errno) { |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 706 | if (started && (errno==EPERM || errno==EIO)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 707 | /* Ran into 'end of memory' - stupid "printpath" */ |
| 708 | return 0; |
| 709 | } |
| 710 | perror("umovestr"); |
| 711 | return -1; |
| 712 | } |
Wichert Akkerman | 5daa028 | 1999-03-15 19:49:42 +0000 | [diff] [blame] | 713 | started = 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 714 | memcpy(laddr, u.x, m = MIN(sizeof(long), len)); |
| 715 | for (i = 0; i < sizeof(long); i++) |
| 716 | if (u.x[i] == '\0') |
| 717 | return 0; |
| 718 | |
| 719 | addr += sizeof(long), laddr += m, len -= m; |
| 720 | } |
| 721 | return 0; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 722 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | #ifdef LINUX |
| 726 | #ifndef SPARC |
| 727 | #define PTRACE_WRITETEXT 101 |
| 728 | #define PTRACE_WRITEDATA 102 |
| 729 | #endif /* !SPARC */ |
| 730 | #endif /* LINUX */ |
| 731 | |
| 732 | #ifdef SUNOS4 |
| 733 | |
| 734 | static int |
| 735 | uload(cmd, pid, addr, len, laddr) |
| 736 | int cmd; |
| 737 | int pid; |
| 738 | long addr; |
| 739 | int len; |
| 740 | char *laddr; |
| 741 | { |
| 742 | #if 0 |
| 743 | int n; |
| 744 | |
| 745 | while (len) { |
| 746 | n = MIN(len, PAGSIZ); |
| 747 | n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr); |
| 748 | if (ptrace(cmd, pid, (char *)addr, n, laddr) < 0) { |
| 749 | perror("uload: ptrace(PTRACE_WRITE, ...)"); |
| 750 | return -1; |
| 751 | } |
| 752 | len -= n; |
| 753 | addr += n; |
| 754 | laddr += n; |
| 755 | } |
| 756 | #else |
| 757 | int peek, poke; |
| 758 | int n, m; |
| 759 | union { |
| 760 | long val; |
| 761 | char x[sizeof(long)]; |
| 762 | } u; |
| 763 | |
| 764 | if (cmd == PTRACE_WRITETEXT) { |
| 765 | peek = PTRACE_PEEKTEXT; |
| 766 | poke = PTRACE_POKETEXT; |
| 767 | } |
| 768 | else { |
| 769 | peek = PTRACE_PEEKDATA; |
| 770 | poke = PTRACE_POKEDATA; |
| 771 | } |
| 772 | if (addr & (sizeof(long) - 1)) { |
| 773 | /* addr not a multiple of sizeof(long) */ |
| 774 | n = addr - (addr & -sizeof(long)); /* residue */ |
| 775 | addr &= -sizeof(long); |
| 776 | errno = 0; |
| 777 | u.val = ptrace(peek, pid, (char *) addr, 0); |
| 778 | if (errno) { |
| 779 | perror("uload: POKE"); |
| 780 | return -1; |
| 781 | } |
| 782 | memcpy(&u.x[n], laddr, m = MIN(sizeof(long) - n, len)); |
| 783 | if (ptrace(poke, pid, (char *)addr, u.val) < 0) { |
| 784 | perror("uload: POKE"); |
| 785 | return -1; |
| 786 | } |
| 787 | addr += sizeof(long), laddr += m, len -= m; |
| 788 | } |
| 789 | while (len) { |
| 790 | if (len < sizeof(long)) |
| 791 | u.val = ptrace(peek, pid, (char *) addr, 0); |
| 792 | memcpy(u.x, laddr, m = MIN(sizeof(long), len)); |
| 793 | if (ptrace(poke, pid, (char *) addr, u.val) < 0) { |
| 794 | perror("uload: POKE"); |
| 795 | return -1; |
| 796 | } |
| 797 | addr += sizeof(long), laddr += m, len -= m; |
| 798 | } |
| 799 | #endif |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | int |
| 804 | tload(pid, addr, len, laddr) |
| 805 | int pid; |
| 806 | int addr, len; |
| 807 | char *laddr; |
| 808 | { |
| 809 | return uload(PTRACE_WRITETEXT, pid, addr, len, laddr); |
| 810 | } |
| 811 | |
| 812 | int |
| 813 | dload(pid, addr, len, laddr) |
| 814 | int pid; |
| 815 | int addr; |
| 816 | int len; |
| 817 | char *laddr; |
| 818 | { |
| 819 | return uload(PTRACE_WRITEDATA, pid, addr, len, laddr); |
| 820 | } |
| 821 | |
| 822 | #endif /* SUNOS4 */ |
| 823 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 824 | #ifndef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 825 | |
| 826 | int |
| 827 | upeek(pid, off, res) |
| 828 | int pid; |
| 829 | long off; |
| 830 | long *res; |
| 831 | { |
| 832 | long val; |
| 833 | |
| 834 | #ifdef SUNOS4_KERNEL_ARCH_KLUDGE |
| 835 | { |
| 836 | static int is_sun4m = -1; |
| 837 | struct utsname name; |
| 838 | |
| 839 | /* Round up the usual suspects. */ |
| 840 | if (is_sun4m == -1) { |
| 841 | if (uname(&name) < 0) { |
| 842 | perror("upeek: uname?"); |
| 843 | exit(1); |
| 844 | } |
| 845 | is_sun4m = strcmp(name.machine, "sun4m") == 0; |
| 846 | if (is_sun4m) { |
| 847 | extern struct xlat struct_user_offsets[]; |
| 848 | struct xlat *x; |
| 849 | |
| 850 | for (x = struct_user_offsets; x->str; x++) |
| 851 | x->val += 1024; |
| 852 | } |
| 853 | } |
| 854 | if (is_sun4m) |
| 855 | off += 1024; |
| 856 | } |
| 857 | #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */ |
| 858 | errno = 0; |
| 859 | val = ptrace(PTRACE_PEEKUSER, pid, (char *) off, 0); |
| 860 | if (val == -1 && errno) { |
| 861 | perror("upeek: ptrace(PTRACE_PEEKUSER, ... )"); |
| 862 | return -1; |
| 863 | } |
| 864 | *res = val; |
| 865 | return 0; |
| 866 | } |
| 867 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 868 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 869 | |
| 870 | long |
| 871 | getpc(tcp) |
| 872 | struct tcb *tcp; |
| 873 | { |
| 874 | |
| 875 | #ifdef LINUX |
| 876 | long pc; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 877 | #if defined(I386) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 878 | if (upeek(tcp->pid, 4*EIP, &pc) < 0) |
| 879 | return -1; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 880 | #elif defined(IA64) |
| 881 | if (upeek(tcp->pid, PT_B0, &pc) < 0) |
| 882 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 883 | #elif defined(ARM) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 884 | if (upeek(tcp->pid, 4*15, &pc) < 0) |
| 885 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 886 | #elif defined(POWERPC) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 887 | if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0) |
| 888 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 889 | #elif defined(M68k) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 890 | if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) |
| 891 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 892 | #elif defined(ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 893 | if (upeek(tcp->pid, REG_PC, &pc) < 0) |
| 894 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 895 | #elif defined(MIPS) |
| 896 | if (upeek(tcp->pid, REG_EPC, &pc) < 0) |
| 897 | return -1; |
| 898 | #elif defined(SPARC) |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 899 | struct regs regs; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 900 | if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) |
| 901 | return -1; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 902 | pc = regs.r_pc; |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 903 | #elif defined(S390) |
| 904 | if(upeek(tcp->pid,PT_PSWADDR,&pc) < 0) |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 905 | #elif defined(HPPA) |
| 906 | if(upeek(tcp->pid,PT_IAOQ0,&pc) < 0) |
| 907 | return -1; |
Wichert Akkerman | 7a0b649 | 1999-12-23 15:08:17 +0000 | [diff] [blame] | 908 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 909 | return pc; |
| 910 | #endif /* LINUX */ |
| 911 | |
| 912 | #ifdef SUNOS4 |
| 913 | /* |
| 914 | * Return current program counter for `pid' |
| 915 | * Assumes PC is never 0xffffffff |
| 916 | */ |
| 917 | struct regs regs; |
| 918 | |
| 919 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) { |
| 920 | perror("getpc: ptrace(PTRACE_GETREGS, ...)"); |
| 921 | return -1; |
| 922 | } |
| 923 | return regs.r_pc; |
| 924 | #endif /* SUNOS4 */ |
| 925 | |
| 926 | #ifdef SVR4 |
| 927 | /* XXX */ |
| 928 | return 0; |
| 929 | #endif /* SVR4 */ |
| 930 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 931 | #ifdef FREEBSD |
| 932 | struct reg regs; |
| 933 | pread(tcp->pfd_reg, ®s, sizeof(regs), 0); |
| 934 | return regs.r_eip; |
| 935 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | void |
| 939 | printcall(tcp) |
| 940 | struct tcb *tcp; |
| 941 | { |
| 942 | |
| 943 | #ifdef LINUX |
| 944 | #ifdef I386 |
| 945 | long eip; |
| 946 | |
| 947 | if (upeek(tcp->pid, 4*EIP, &eip) < 0) { |
| 948 | tprintf("[????????] "); |
| 949 | return; |
| 950 | } |
| 951 | tprintf("[%08lx] ", eip); |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 952 | #elif defined(IA62) |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 953 | long ip; |
| 954 | |
| 955 | if (upeek(tcp->pid, PT_B0, &ip) < 0) { |
| 956 | tprintf("[????????] "); |
| 957 | return; |
| 958 | } |
| 959 | tprintf("[%08lx] ", ip); |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 960 | #elif defined(POWERPC) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 961 | long pc; |
| 962 | |
| 963 | if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0) { |
| 964 | tprintf ("[????????] "); |
| 965 | return; |
| 966 | } |
| 967 | tprintf("[%08lx] ", pc); |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 968 | #elif defined(M68k) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 969 | long pc; |
| 970 | |
| 971 | if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) { |
| 972 | tprintf ("[????????] "); |
| 973 | return; |
| 974 | } |
| 975 | tprintf("[%08lx] ", pc); |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 976 | #elif defined(ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 977 | long pc; |
| 978 | |
| 979 | if (upeek(tcp->pid, REG_PC, &pc) < 0) { |
| 980 | tprintf ("[????????] "); |
| 981 | return; |
| 982 | } |
| 983 | tprintf("[%08lx] ", pc); |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 984 | #elif defined(SPARC) |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 985 | struct regs regs; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 986 | if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) { |
| 987 | tprintf("[????????] "); |
| 988 | return; |
| 989 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 990 | tprintf("[%08lx] ", regs.r_pc); |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 991 | #elif defined(HPPA) |
| 992 | long pc; |
| 993 | |
| 994 | if(upeek(tcp->pid,PT_IAOQ0,&pc) < 0) { |
| 995 | tprintf ("[????????] "); |
| 996 | return; |
| 997 | } |
| 998 | tprintf("[%08lx] ", pc); |
Wichert Akkerman | 75c422b | 2001-04-10 10:22:50 +0000 | [diff] [blame^] | 999 | #elif defined(MIPS) |
| 1000 | long pc; |
| 1001 | |
| 1002 | if (upeek(tcp->pid, REG_EPC, &pc) < 0) { |
| 1003 | tprintf ("[????????] "); |
| 1004 | return; |
| 1005 | } |
| 1006 | tprintf("[%08lx] ", pc); |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1007 | #endif /* !architecture */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1008 | #endif /* LINUX */ |
| 1009 | |
| 1010 | #ifdef SUNOS4 |
| 1011 | struct regs regs; |
| 1012 | |
| 1013 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) { |
| 1014 | perror("printcall: ptrace(PTRACE_GETREGS, ...)"); |
| 1015 | tprintf("[????????] "); |
| 1016 | return; |
| 1017 | } |
| 1018 | tprintf("[%08x] ", regs.r_o7); |
| 1019 | #endif /* SUNOS4 */ |
| 1020 | |
| 1021 | #ifdef SVR4 |
| 1022 | /* XXX */ |
| 1023 | tprintf("[????????] "); |
| 1024 | #endif |
| 1025 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1026 | #ifdef FREEBSD |
| 1027 | struct reg regs; |
| 1028 | pread(tcp->pfd_reg, ®s, sizeof(regs), 0); |
| 1029 | tprintf("[%08x] ", regs.r_eip); |
| 1030 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1033 | #ifndef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1034 | |
| 1035 | int |
| 1036 | setbpt(tcp) |
| 1037 | struct tcb *tcp; |
| 1038 | { |
| 1039 | |
| 1040 | #ifdef LINUX |
| 1041 | #ifdef SPARC |
| 1042 | /* We simply use the SunOS breakpoint code. */ |
| 1043 | |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1044 | struct regs regs; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1045 | #define LOOPA 0x30800000 /* ba,a 0 */ |
| 1046 | |
| 1047 | if (tcp->flags & TCB_BPTSET) { |
| 1048 | fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid); |
| 1049 | return -1; |
| 1050 | } |
| 1051 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 1052 | perror("setbpt: ptrace(PTRACE_GETREGS, ...)"); |
| 1053 | return -1; |
| 1054 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1055 | tcp->baddr = regs.r_o7 + 8; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1056 | errno = 0; |
| 1057 | tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)tcp->baddr, 0); |
| 1058 | if(errno) { |
| 1059 | perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)"); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | |
| 1063 | /* |
| 1064 | * XXX - BRUTAL MODE ON |
| 1065 | * We cannot set a real BPT in the child, since it will not be |
| 1066 | * traced at the moment it will reach the trap and would probably |
| 1067 | * die with a core dump. |
| 1068 | * Thus, we are force our way in by taking out two instructions |
| 1069 | * and insert an eternal loop instead, in expectance of the SIGSTOP |
| 1070 | * generated by out PTRACE_ATTACH. |
| 1071 | * Of cause, if we evaporate ourselves in the middle of all this... |
| 1072 | */ |
| 1073 | errno = 0; |
| 1074 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOPA); |
| 1075 | if(errno) { |
| 1076 | perror("setbpt: ptrace(PTRACE_POKETEXT, ...)"); |
| 1077 | return -1; |
| 1078 | } |
| 1079 | tcp->flags |= TCB_BPTSET; |
| 1080 | |
| 1081 | #else /* !SPARC */ |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1082 | #ifdef IA64 |
| 1083 | /* |
| 1084 | * Our strategy here is to replace the bundle that contained |
| 1085 | * the clone() syscall with a bundle of the form: |
| 1086 | * |
| 1087 | * { 1: br 1b; br 1b; br 1b } |
| 1088 | * |
| 1089 | * This ensures that the newly forked child will loop |
| 1090 | * endlessly until we've got a chance to attach to it. |
| 1091 | */ |
| 1092 | { |
| 1093 | # define LOOP0 0x0000100000000017 |
| 1094 | # define LOOP1 0x4000000000200000 |
| 1095 | unsigned long addr, ipsr; |
| 1096 | pid_t pid; |
| 1097 | |
| 1098 | pid = tcp->pid; |
| 1099 | if (upeek(pid, PT_CR_IPSR, &ipsr) < 0) |
| 1100 | return -1; |
| 1101 | if (upeek(pid, PT_CR_IIP, &addr) < 0) |
| 1102 | return -1; |
| 1103 | tcp->baddr = addr | ((ipsr >> 41) & 0x3); /* store "ri" in low two bits */ |
| 1104 | |
| 1105 | errno = 0; |
| 1106 | tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 0, 0); |
| 1107 | tcp->inst[1] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 8, 0); |
| 1108 | if (errno) { |
| 1109 | perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)"); |
| 1110 | return -1; |
| 1111 | } |
| 1112 | |
| 1113 | errno = 0; |
| 1114 | ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, LOOP0); |
| 1115 | ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, LOOP1); |
| 1116 | if (errno) { |
| 1117 | perror("setbpt: ptrace(PTRACE_POKETEXT, ...)"); |
| 1118 | return -1; |
| 1119 | } |
| 1120 | tcp->flags |= TCB_BPTSET; |
| 1121 | } |
| 1122 | #else /* !IA64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1123 | |
| 1124 | #if defined (I386) |
| 1125 | #define LOOP 0x0000feeb |
| 1126 | #elif defined (M68K) |
| 1127 | #define LOOP 0x60fe0000 |
| 1128 | #elif defined (ALPHA) |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1129 | #define LOOP 0xc3ffffff |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1130 | #elif defined (POWERPC) |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1131 | #define LOOP 0x0000feeb |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1132 | #elif defined(ARM) |
Wichert Akkerman | 9a8a37f | 1999-12-24 23:19:31 +0000 | [diff] [blame] | 1133 | #define LOOP 0xEAFFFFFE |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1134 | #elif defined(MIPS) |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1135 | #define LOOP 0x1000ffff |
| 1136 | #elif defined(S390) |
| 1137 | #define LOOP 0xa7f40000 /* BRC 15,0 */ |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1138 | #elif defined(HPPA) |
| 1139 | #define LOOP 0xe81f1ff7 /* b,l,n <loc>,r0 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1140 | #else |
| 1141 | #error unknown architecture |
| 1142 | #endif |
| 1143 | |
| 1144 | if (tcp->flags & TCB_BPTSET) { |
| 1145 | fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid); |
| 1146 | return -1; |
| 1147 | } |
| 1148 | #if defined (I386) |
| 1149 | if (upeek(tcp->pid, 4*EIP, &tcp->baddr) < 0) |
| 1150 | return -1; |
| 1151 | #elif defined (M68K) |
| 1152 | if (upeek(tcp->pid, 4*PT_PC, &tcp->baddr) < 0) |
| 1153 | return -1; |
| 1154 | #elif defined (ALPHA) |
| 1155 | return -1; |
| 1156 | #elif defined (ARM) |
| 1157 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1158 | #elif defined (MIPS) |
| 1159 | return -1; /* FIXME: I do not know what i do - Flo */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1160 | #elif defined (POWERPC) |
| 1161 | if (upeek(tcp->pid, 4*PT_NIP, &tcp->baddr) < 0) |
| 1162 | return -1; |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1163 | #elif defined(S390) |
| 1164 | if (upeek(tcp->pid,PT_PSWADDR, &tcp->baddr) < 0) |
| 1165 | return -1; |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1166 | #elif defined(HPPA) |
| 1167 | if (upeek(tcp->pid, PT_IAOQ0, &tcp->baddr) < 0) |
| 1168 | return -1; |
| 1169 | tcp->baddr &= ~0x03; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1170 | #else |
| 1171 | #error unknown architecture |
| 1172 | #endif |
| 1173 | if (debug) |
| 1174 | fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr); |
| 1175 | tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *) tcp->baddr, 0); |
| 1176 | if (errno) { |
| 1177 | perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)"); |
| 1178 | return -1; |
| 1179 | } |
| 1180 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP); |
| 1181 | if (errno) { |
| 1182 | perror("setbpt: ptrace(PTRACE_POKETEXT, ...)"); |
| 1183 | return -1; |
| 1184 | } |
| 1185 | tcp->flags |= TCB_BPTSET; |
| 1186 | |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1187 | #endif /* !IA64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1188 | #endif /* SPARC */ |
| 1189 | #endif /* LINUX */ |
| 1190 | |
| 1191 | #ifdef SUNOS4 |
| 1192 | #ifdef SPARC /* This code is slightly sparc specific */ |
| 1193 | |
Wichert Akkerman | e6f876c | 1999-06-22 15:28:30 +0000 | [diff] [blame] | 1194 | struct regs regs; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1195 | #define BPT 0x91d02001 /* ta 1 */ |
| 1196 | #define LOOP 0x10800000 /* ba 0 */ |
| 1197 | #define LOOPA 0x30800000 /* ba,a 0 */ |
| 1198 | #define NOP 0x01000000 |
| 1199 | #if LOOPA |
| 1200 | static int loopdeloop[1] = {LOOPA}; |
| 1201 | #else |
| 1202 | static int loopdeloop[2] = {LOOP, NOP}; |
| 1203 | #endif |
| 1204 | |
| 1205 | if (tcp->flags & TCB_BPTSET) { |
| 1206 | fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid); |
| 1207 | return -1; |
| 1208 | } |
| 1209 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 1210 | perror("setbpt: ptrace(PTRACE_GETREGS, ...)"); |
| 1211 | return -1; |
| 1212 | } |
| 1213 | tcp->baddr = regs.r_o7 + 8; |
| 1214 | if (ptrace(PTRACE_READTEXT, tcp->pid, (char *)tcp->baddr, |
| 1215 | sizeof tcp->inst, (char *)tcp->inst) < 0) { |
| 1216 | perror("setbpt: ptrace(PTRACE_READTEXT, ...)"); |
| 1217 | return -1; |
| 1218 | } |
| 1219 | |
| 1220 | /* |
| 1221 | * XXX - BRUTAL MODE ON |
| 1222 | * We cannot set a real BPT in the child, since it will not be |
| 1223 | * traced at the moment it will reach the trap and would probably |
| 1224 | * die with a core dump. |
| 1225 | * Thus, we are force our way in by taking out two instructions |
| 1226 | * and insert an eternal loop in stead, in expectance of the SIGSTOP |
| 1227 | * generated by out PTRACE_ATTACH. |
| 1228 | * Of cause, if we evaporate ourselves in the middle of all this... |
| 1229 | */ |
| 1230 | if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr, |
| 1231 | sizeof loopdeloop, (char *) loopdeloop) < 0) { |
| 1232 | perror("setbpt: ptrace(PTRACE_WRITETEXT, ...)"); |
| 1233 | return -1; |
| 1234 | } |
| 1235 | tcp->flags |= TCB_BPTSET; |
| 1236 | |
| 1237 | #endif /* SPARC */ |
| 1238 | #endif /* SUNOS4 */ |
| 1239 | |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | int |
| 1244 | clearbpt(tcp) |
| 1245 | struct tcb *tcp; |
| 1246 | { |
| 1247 | |
| 1248 | #ifdef LINUX |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1249 | #if defined(I386) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1250 | long eip; |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1251 | #elif defined(POWERPC) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1252 | long pc; |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1253 | #elif defined(M68K) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1254 | long pc; |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1255 | #elif defined(ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1256 | long pc; |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1257 | #elif defined(HPPA) |
| 1258 | long iaoq; |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1259 | #endif /* architecture */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1260 | |
| 1261 | #ifdef SPARC |
| 1262 | /* Again, we borrow the SunOS breakpoint code. */ |
| 1263 | if (!(tcp->flags & TCB_BPTSET)) { |
| 1264 | fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid); |
| 1265 | return -1; |
| 1266 | } |
| 1267 | errno = 0; |
| 1268 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]); |
| 1269 | if(errno) { |
| 1270 | perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)"); |
| 1271 | return -1; |
| 1272 | } |
| 1273 | tcp->flags &= ~TCB_BPTSET; |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1274 | #elif defined(IA64) |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1275 | { |
| 1276 | unsigned long addr, ipsr; |
| 1277 | pid_t pid; |
| 1278 | |
| 1279 | pid = tcp->pid; |
| 1280 | |
| 1281 | if (upeek(pid, PT_CR_IPSR, &ipsr) < 0) |
| 1282 | return -1; |
| 1283 | if (upeek(pid, PT_CR_IIP, &addr) < 0) |
| 1284 | return -1; |
| 1285 | |
| 1286 | /* restore original bundle: */ |
| 1287 | errno = 0; |
| 1288 | ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, tcp->inst[0]); |
| 1289 | ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, tcp->inst[1]); |
| 1290 | if (errno) { |
| 1291 | perror("clearbpt: ptrace(PTRACE_POKETEXT, ...)"); |
| 1292 | return -1; |
| 1293 | } |
| 1294 | |
| 1295 | /* restore original "ri" in ipsr: */ |
| 1296 | ipsr = (ipsr & ~(0x3ul << 41)) | ((tcp->baddr & 0x3) << 41); |
| 1297 | errno = 0; |
| 1298 | ptrace(PTRACE_POKEUSER, pid, (char *) PT_CR_IPSR, ipsr); |
| 1299 | if (errno) { |
| 1300 | perror("clrbpt: ptrace(PTRACE_POKEUSER, ...)"); |
| 1301 | return -1; |
| 1302 | } |
| 1303 | |
| 1304 | tcp->flags &= ~TCB_BPTSET; |
| 1305 | |
| 1306 | if (addr != (tcp->baddr & ~0x3)) { |
| 1307 | /* the breakpoint has not been reached yet. */ |
| 1308 | if (debug) |
| 1309 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 1310 | addr, tcp->baddr); |
| 1311 | return 0; |
| 1312 | } |
| 1313 | } |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1314 | #else /* !IA64 && ! SPARC */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1315 | |
| 1316 | if (debug) |
| 1317 | fprintf(stderr, "[%d] clearing bpt\n", tcp->pid); |
| 1318 | if (!(tcp->flags & TCB_BPTSET)) { |
| 1319 | fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid); |
| 1320 | return -1; |
| 1321 | } |
| 1322 | errno = 0; |
| 1323 | ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]); |
| 1324 | if (errno) { |
| 1325 | perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)"); |
| 1326 | return -1; |
| 1327 | } |
| 1328 | tcp->flags &= ~TCB_BPTSET; |
| 1329 | |
| 1330 | #ifdef I386 |
| 1331 | if (upeek(tcp->pid, 4*EIP, &eip) < 0) |
| 1332 | return -1; |
| 1333 | if (eip != tcp->baddr) { |
| 1334 | /* The breakpoint has not been reached yet. */ |
| 1335 | if (debug) |
| 1336 | fprintf(stderr, |
| 1337 | "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 1338 | eip, tcp->baddr); |
| 1339 | return 0; |
| 1340 | } |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 1341 | #elif defined(POWERPC) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1342 | if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0) |
| 1343 | return -1; |
| 1344 | if (pc != tcp->baddr) { |
| 1345 | /* The breakpoint has not been reached yet. */ |
| 1346 | if (debug) |
| 1347 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 1348 | pc, tcp->baddr); |
| 1349 | return 0; |
| 1350 | } |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1351 | #elif defined(M68K) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1352 | if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) |
| 1353 | return -1; |
| 1354 | if (pc != tcp->baddr) { |
| 1355 | /* The breakpoint has not been reached yet. */ |
| 1356 | if (debug) |
| 1357 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 1358 | pc, tcp->baddr); |
| 1359 | return 0; |
| 1360 | } |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1361 | #elif defined(ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1362 | if (upeek(tcp->pid, REG_PC, &pc) < 0) |
| 1363 | return -1; |
| 1364 | if (pc != tcp->baddr) { |
| 1365 | /* The breakpoint has not been reached yet. */ |
| 1366 | if (debug) |
| 1367 | fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n", |
| 1368 | pc, tcp->baddr); |
| 1369 | return 0; |
| 1370 | } |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1371 | #elif defined(HPPA) |
| 1372 | if (upeek(tcp->pid, PT_IAOQ0, &iaoq) < 0) |
| 1373 | return -1; |
| 1374 | iaoq &= ~0x03; |
| 1375 | if (iaoq != tcp->baddr && iaoq != tcp->baddr + 4) { |
| 1376 | /* The breakpoint has not been reached yet. */ |
| 1377 | if (debug) |
| 1378 | fprintf(stderr, "NOTE: PC not at bpt (iaoq %#lx baddr %#lx)\n", |
| 1379 | iaoq, tcp->baddr); |
| 1380 | return 0; |
| 1381 | } |
| 1382 | iaoq = tcp->baddr | 3; |
| 1383 | /* We should be pointing at a 'ldi -1000,r1' in glibc, so it is |
| 1384 | * safe to set both IAOQ0 and IAOQ1 to that so the PSW N bit |
| 1385 | * has no significant effect. |
| 1386 | */ |
| 1387 | ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ0, iaoq); |
| 1388 | ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ1, iaoq); |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1389 | #endif /* arch */ |
| 1390 | #endif /* !SPARC && !IA64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1391 | #endif /* LINUX */ |
| 1392 | |
| 1393 | #ifdef SUNOS4 |
| 1394 | #ifdef SPARC |
| 1395 | |
| 1396 | #if !LOOPA |
Wichert Akkerman | e6f876c | 1999-06-22 15:28:30 +0000 | [diff] [blame] | 1397 | struct regs regs; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1398 | #endif |
| 1399 | |
| 1400 | if (!(tcp->flags & TCB_BPTSET)) { |
| 1401 | fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid); |
| 1402 | return -1; |
| 1403 | } |
| 1404 | if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr, |
| 1405 | sizeof tcp->inst, (char *) tcp->inst) < 0) { |
| 1406 | perror("clearbtp: ptrace(PTRACE_WRITETEXT, ...)"); |
| 1407 | return -1; |
| 1408 | } |
| 1409 | tcp->flags &= ~TCB_BPTSET; |
| 1410 | |
| 1411 | #if !LOOPA |
| 1412 | /* |
| 1413 | * Since we don't have a single instruction breakpoint, we may have |
| 1414 | * to adjust the program counter after removing the our `breakpoint'. |
| 1415 | */ |
| 1416 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 1417 | perror("clearbpt: ptrace(PTRACE_GETREGS, ...)"); |
| 1418 | return -1; |
| 1419 | } |
| 1420 | if ((regs.r_pc < tcp->baddr) || |
| 1421 | (regs.r_pc > tcp->baddr + 4)) { |
| 1422 | /* The breakpoint has not been reached yet */ |
| 1423 | if (debug) |
| 1424 | fprintf(stderr, |
| 1425 | "NOTE: PC not at bpt (pc %#x baddr %#x)\n", |
| 1426 | regs.r_pc, tcp->parent->baddr); |
| 1427 | return 0; |
| 1428 | } |
| 1429 | if (regs.r_pc != tcp->baddr) |
| 1430 | if (debug) |
| 1431 | fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n", |
| 1432 | regs.r_pc, tcp->baddr); |
| 1433 | |
| 1434 | regs.r_pc = tcp->baddr; |
| 1435 | if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) { |
| 1436 | perror("clearbpt: ptrace(PTRACE_SETREGS, ...)"); |
| 1437 | return -1; |
| 1438 | } |
| 1439 | #endif /* LOOPA */ |
| 1440 | #endif /* SPARC */ |
| 1441 | #endif /* SUNOS4 */ |
| 1442 | |
| 1443 | return 0; |
| 1444 | } |
| 1445 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1446 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1447 | |
| 1448 | #ifdef SUNOS4 |
| 1449 | |
| 1450 | static int |
| 1451 | getex(pid, hdr) |
| 1452 | int pid; |
| 1453 | struct exec *hdr; |
| 1454 | { |
| 1455 | int n; |
| 1456 | |
| 1457 | for (n = 0; n < sizeof *hdr; n += 4) { |
| 1458 | long res; |
| 1459 | if (upeek(pid, uoff(u_exdata) + n, &res) < 0) |
| 1460 | return -1; |
| 1461 | memcpy(((char *) hdr) + n, &res, 4); |
| 1462 | } |
| 1463 | if (debug) { |
| 1464 | fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n", |
| 1465 | hdr->a_magic, hdr->a_toolversion, hdr->a_machtype); |
| 1466 | fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n", |
| 1467 | hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry); |
| 1468 | } |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
| 1472 | int |
| 1473 | fixvfork(tcp) |
| 1474 | struct tcb *tcp; |
| 1475 | { |
| 1476 | int pid = tcp->pid; |
| 1477 | /* |
| 1478 | * Change `vfork' in a freshly exec'ed dynamically linked |
| 1479 | * executable's (internal) symbol table to plain old `fork' |
| 1480 | */ |
| 1481 | |
| 1482 | struct exec hdr; |
| 1483 | struct link_dynamic dyn; |
| 1484 | struct link_dynamic_2 ld; |
| 1485 | char *strtab, *cp; |
| 1486 | |
| 1487 | if (getex(pid, &hdr) < 0) |
| 1488 | return -1; |
| 1489 | if (!hdr.a_dynamic) |
| 1490 | return -1; |
| 1491 | |
| 1492 | if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) { |
| 1493 | fprintf(stderr, "Cannot read DYNAMIC\n"); |
| 1494 | return -1; |
| 1495 | } |
| 1496 | if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) { |
| 1497 | fprintf(stderr, "Cannot read link_dynamic_2\n"); |
| 1498 | return -1; |
| 1499 | } |
| 1500 | if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) { |
| 1501 | fprintf(stderr, "fixvfork: out of memory\n"); |
| 1502 | return -1; |
| 1503 | } |
| 1504 | if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr), |
| 1505 | (int)ld.ld_symb_size, strtab) < 0) |
| 1506 | goto err; |
| 1507 | |
| 1508 | #if 0 |
| 1509 | for (cp = strtab; cp < strtab + ld.ld_symb_size; ) { |
| 1510 | fprintf(stderr, "[symbol: %s]\n", cp); |
| 1511 | cp += strlen(cp)+1; |
| 1512 | } |
| 1513 | return 0; |
| 1514 | #endif |
| 1515 | for (cp = strtab; cp < strtab + ld.ld_symb_size; ) { |
| 1516 | if (strcmp(cp, "_vfork") == 0) { |
| 1517 | if (debug) |
| 1518 | fprintf(stderr, "fixvfork: FOUND _vfork\n"); |
| 1519 | strcpy(cp, "_fork"); |
| 1520 | break; |
| 1521 | } |
| 1522 | cp += strlen(cp)+1; |
| 1523 | } |
| 1524 | if (cp < strtab + ld.ld_symb_size) |
| 1525 | /* |
| 1526 | * Write entire symbol table back to avoid |
| 1527 | * memory alignment bugs in ptrace |
| 1528 | */ |
| 1529 | if (tload(pid, (int)ld.ld_symbols+(int)N_TXTADDR(hdr), |
| 1530 | (int)ld.ld_symb_size, strtab) < 0) |
| 1531 | goto err; |
| 1532 | |
| 1533 | free(strtab); |
| 1534 | return 0; |
| 1535 | |
| 1536 | err: |
| 1537 | free(strtab); |
| 1538 | return -1; |
| 1539 | } |
| 1540 | |
| 1541 | #endif /* SUNOS4 */ |