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