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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
35 #include <sys/param.h>
38 #include <machine/reg.h>
43 #if defined(linux) && !defined(__GLIBC__)
44 #include <linux/ptrace.h>
49 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
52 #ifdef HAVE_SYS_PTRACE_H
53 #include <sys/ptrace.h>
56 #ifdef SUNOS4_KERNEL_ARCH_KLUDGE
57 #include <sys/utsname.h>
58 #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
60 #if defined(LINUX) && defined(SPARC) && !defined(__GLIBC__)
62 #include <linux/unistd.h>
64 #define _hack_syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,\
66 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
70 __asm__ volatile ("or %%g0, %1, %%o0\n\t" \
71 "or %%g0, %2, %%o1\n\t" \
72 "or %%g0, %3, %%o2\n\t" \
73 "or %%g0, %4, %%o3\n\t" \
74 "or %%g0, %5, %%o4\n\t" \
75 "or %%g0, %6, %%g1\n\t" \
78 "or %%g0, %%o0, %0\n\t" \
79 "sub %%g0, %%o0, %0\n\t" \
82 : "0" ((long)(arg1)),"1" ((long)(arg2)), \
83 "2" ((long)(arg3)),"3" ((long)(arg4)),"4" ((long)(arg5)), \
84 "i" (__NR_##syscall) \
85 : "g1", "o0", "o1", "o2", "o3", "o4"); \
87 return (type) __res; \
92 static _hack_syscall5(int,_ptrace,int,__request,int,__pid,int,__addr,int,__data,int,__addr2,ptrace)
100 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
103 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
120 return a->tv_sec || a->tv_usec;
125 struct timeval *a, *b;
127 if (a->tv_sec < b->tv_sec
128 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
130 if (a->tv_sec > b->tv_sec
131 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
140 return tv->tv_sec + tv->tv_usec/1000000.0;
145 struct timeval *tv, *a, *b;
147 tv->tv_sec = a->tv_sec + b->tv_sec;
148 tv->tv_usec = a->tv_usec + b->tv_usec;
149 if (tv->tv_usec > 1000000) {
151 tv->tv_usec -= 1000000;
157 struct timeval *tv, *a, *b;
159 tv->tv_sec = a->tv_sec - b->tv_sec;
160 tv->tv_usec = a->tv_usec - b->tv_usec;
161 if (((long) tv->tv_usec) < 0) {
163 tv->tv_usec += 1000000;
169 struct timeval *tv, *a;
172 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
173 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
174 tv->tv_usec %= 1000000;
179 struct timeval *tv, *a;
182 tv->tv_usec = a->tv_usec * n;
183 tv->tv_sec = a->tv_sec * n + a->tv_usec / 1000000;
184 tv->tv_usec %= 1000000;
192 for (; xlat->str != NULL; xlat++)
193 if (xlat->val == val)
199 * Print entry in struct xlat table, if there.
202 printxval(xlat, val, dflt)
207 char *str = xlookup(xlat, val);
212 tprintf("%#x /* %s */", val, dflt);
216 * Interpret `xlat' as an array of flags
217 * print the entries whose bits are on in `flags'
218 * return # of flags printed.
221 addflags(xlat, flags)
227 for (n = 0; xlat->str; xlat++) {
228 if (xlat->val && (flags & xlat->val) == xlat->val) {
229 tprintf("|%s", xlat->str);
235 tprintf("|%#x", flags);
242 printflags(xlat, flags)
249 if (flags == 0 && xlat->val == 0) {
250 tprintf("%s", xlat->str);
255 for (n = 0; xlat->str; xlat++) {
256 if (xlat->val && (flags & xlat->val) == xlat->val) {
257 tprintf("%s%s", sep, xlat->str);
264 tprintf("%s%#x", sep, flags);
271 printnum(tcp, addr, fmt)
282 if (umove(tcp, addr, &num) < 0) {
283 tprintf("%#lx", addr);
291 static char path[MAXPATHLEN + 1];
297 char buf[2 * MAXPATHLEN + 1];
300 if (!strpbrk(str, "\"\'\\")) {
301 tprintf("\"%s\"", str);
304 for (s = buf; *str; str++) {
306 case '\"': case '\'': case '\\':
307 *s++ = '\\'; *s++ = *str; break;
313 tprintf("\"%s\"", buf);
321 if (umovestr(tcp, addr, MAXPATHLEN, path) < 0)
322 tprintf("%#lx", addr);
329 printpathn(tcp, addr, n)
334 if (umovestr(tcp, addr, n, path) < 0)
335 tprintf("%#lx", addr);
343 printstr(tcp, addr, len)
348 static unsigned char *str = NULL;
358 if ((str = malloc(max_strlen)) == NULL
359 || (outstr = malloc(2*max_strlen)) == NULL) {
360 fprintf(stderr, "printstr: no memory\n");
361 tprintf("%#lx", addr);
365 outend = outstr + max_strlen * 2 - 10;
368 if (umovestr(tcp, addr, n, (char *) str) < 0) {
369 tprintf("%#lx", addr);
374 n = MIN(len, max_strlen);
375 if (umoven(tcp, addr, n, (char *) str) < 0) {
376 tprintf("%#lx", addr);
385 for (i = 0; i < n; i++) {
387 if (len < 0 && c == '\0')
389 if (!isprint(c) && !isspace(c)) {
400 for (i = 0; i < n; i++) {
402 if (len < 0 && c == '\0')
404 sprintf(s, "\\x%02x", c);
411 for (i = 0; i < n; i++) {
413 if (len < 0 && c == '\0')
416 case '\"': case '\'': case '\\':
417 *s++ = '\\'; *s++ = c; break;
419 *s++ = '\\'; *s++ = 'f'; break;
421 *s++ = '\\'; *s++ = 'n'; break;
423 *s++ = '\\'; *s++ = 'r'; break;
425 *s++ = '\\'; *s++ = 't'; break;
427 *s++ = '\\'; *s++ = 'v'; break;
431 else if (i < n - 1 && isdigit(str[i + 1])) {
432 sprintf(s, "\\%03o", c);
436 sprintf(s, "\\%o", c);
447 if (i < len || (len < 0 && (i == n || s > outend))) {
448 *s++ = '.'; *s++ = '.'; *s++ = '.';
451 tprintf("%s", outstr);
455 dumpstr(tcp, addr, len)
460 static int strsize = -1;
461 static unsigned char *str;
462 static char outstr[80];
469 if ((str = malloc(len)) == NULL) {
470 fprintf(stderr, "dump: no memory\n");
476 if (umoven(tcp, addr, len, (char *) str) < 0)
479 for (i = 0; i < len; i += 16) {
481 sprintf(s, " | %05x ", i);
483 for (j = 0; j < 16; j++) {
487 sprintf(s, " %02x", str[i + j]);
491 *s++ = ' '; *s++ = ' '; *s++ = ' ';
494 *s++ = ' '; *s++ = ' ';
495 for (j = 0; j < 16; j++) {
499 if (isprint(str[i + j]))
507 tprintf("%s |\n", outstr);
511 #define PAGMASK (~(PAGSIZ - 1))
513 * move `len' bytes of data from process `pid'
514 * at address `addr' to our space at `laddr'
517 umoven(tcp, addr, len, laddr)
530 char x[sizeof(long)];
533 if (addr & (sizeof(long) - 1)) {
534 /* addr not a multiple of sizeof(long) */
535 n = addr - (addr & -sizeof(long)); /* residue */
536 addr &= -sizeof(long); /* residue */
538 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
540 if (started && (errno==EPERM || errno==EIO)) {
541 /* Ran into 'end of memory' - stupid "printpath" */
544 /* But if not started, we had a bogus address. */
545 perror("ptrace: umoven");
549 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
550 addr += sizeof(long), laddr += m, len -= m;
554 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
556 if (started && (errno==EPERM || errno==EIO)) {
557 /* Ran into 'end of memory' - stupid "printpath" */
560 perror("ptrace: umoven");
564 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
565 addr += sizeof(long), laddr += m, len -= m;
575 char x[sizeof(long)];
578 if (addr & (sizeof(long) - 1)) {
579 /* addr not a multiple of sizeof(long) */
580 n = addr - (addr & -sizeof(long)); /* residue */
581 addr &= -sizeof(long); /* residue */
583 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
588 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
589 addr += sizeof(long), laddr += m, len -= m;
593 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
598 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
599 addr += sizeof(long), laddr += m, len -= m;
605 n = MIN(len, PAGSIZ);
606 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
607 if (ptrace(PTRACE_READDATA, pid,
608 (char *) addr, len, laddr) < 0) {
609 perror("umoven: ptrace(PTRACE_READDATA, ...)");
622 * We would like to use pread preferentially for speed
623 * but even though SGI has it in their library, it no longer works.
629 if (pread(tcp->pfd, laddr, len, addr) == -1)
631 #else /* !HAVE_PREAD */
632 lseek(tcp->pfd, addr, SEEK_SET);
633 if (read(tcp->pfd, laddr, len) == -1)
635 #endif /* !HAVE_PREAD */
642 * like `umove' but make the additional effort of looking
643 * for a terminating zero byte.
646 umovestr(tcp, addr, len, laddr)
653 return umoven(tcp, addr, len, laddr);
660 char x[sizeof(long)];
663 if (addr & (sizeof(long) - 1)) {
664 /* addr not a multiple of sizeof(long) */
665 n = addr - (addr & -sizeof(long)); /* residue */
666 addr &= -sizeof(long); /* residue */
668 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
670 if (started && (errno==EPERM || errno==EIO)) {
671 /* Ran into 'end of memory' - stupid "printpath" */
678 memcpy(laddr, &u.x[n], m = MIN(sizeof(long)-n,len));
679 while (n & (sizeof(long) - 1))
680 if (u.x[n++] == '\0')
682 addr += sizeof(long), laddr += m, len -= m;
686 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
688 if (started && (errno==EPERM || errno==EIO)) {
689 /* Ran into 'end of memory' - stupid "printpath" */
696 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
697 for (i = 0; i < sizeof(long); i++)
701 addr += sizeof(long), laddr += m, len -= m;
709 #define PTRACE_WRITETEXT 101
710 #define PTRACE_WRITEDATA 102
717 uload(cmd, pid, addr, len, laddr)
728 n = MIN(len, PAGSIZ);
729 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
730 if (ptrace(cmd, pid, (char *)addr, n, laddr) < 0) {
731 perror("uload: ptrace(PTRACE_WRITE, ...)");
743 char x[sizeof(long)];
746 if (cmd == PTRACE_WRITETEXT) {
747 peek = PTRACE_PEEKTEXT;
748 poke = PTRACE_POKETEXT;
751 peek = PTRACE_PEEKDATA;
752 poke = PTRACE_POKEDATA;
754 if (addr & (sizeof(long) - 1)) {
755 /* addr not a multiple of sizeof(long) */
756 n = addr - (addr & -sizeof(long)); /* residue */
757 addr &= -sizeof(long);
759 u.val = ptrace(peek, pid, (char *) addr, 0);
761 perror("uload: POKE");
764 memcpy(&u.x[n], laddr, m = MIN(sizeof(long) - n, len));
765 if (ptrace(poke, pid, (char *)addr, u.val) < 0) {
766 perror("uload: POKE");
769 addr += sizeof(long), laddr += m, len -= m;
772 if (len < sizeof(long))
773 u.val = ptrace(peek, pid, (char *) addr, 0);
774 memcpy(u.x, laddr, m = MIN(sizeof(long), len));
775 if (ptrace(poke, pid, (char *) addr, u.val) < 0) {
776 perror("uload: POKE");
779 addr += sizeof(long), laddr += m, len -= m;
786 tload(pid, addr, len, laddr)
791 return uload(PTRACE_WRITETEXT, pid, addr, len, laddr);
795 dload(pid, addr, len, laddr)
801 return uload(PTRACE_WRITEDATA, pid, addr, len, laddr);
816 #ifdef SUNOS4_KERNEL_ARCH_KLUDGE
818 static int is_sun4m = -1;
821 /* Round up the usual suspects. */
822 if (is_sun4m == -1) {
823 if (uname(&name) < 0) {
824 perror("upeek: uname?");
827 is_sun4m = strcmp(name.machine, "sun4m") == 0;
829 extern struct xlat struct_user_offsets[];
832 for (x = struct_user_offsets; x->str; x++)
839 #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
841 val = ptrace(PTRACE_PEEKUSER, pid, (char *) off, 0);
842 if (val == -1 && errno) {
843 perror("upeek: ptrace(PTRACE_PEEKUSER, ... )");
861 if (upeek(tcp->pid, 4*EIP, &pc) < 0)
865 if (upeek(tcp->pid, 4*15, &pc) < 0)
869 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0)
873 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0)
877 if (upeek(tcp->pid, REG_PC, &pc) < 0)
882 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
888 #endif /* !POWERPC */
896 * Return current program counter for `pid'
897 * Assumes PC is never 0xffffffff
901 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
902 perror("getpc: ptrace(PTRACE_GETREGS, ...)");
924 if (upeek(tcp->pid, 4*EIP, &eip) < 0) {
925 tprintf("[????????] ");
928 tprintf("[%08lx] ", eip);
933 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0) {
934 tprintf ("[????????] ");
937 tprintf("[%08lx] ", pc);
942 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
943 tprintf ("[????????] ");
946 tprintf("[%08lx] ", pc);
951 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
952 tprintf ("[????????] ");
955 tprintf("[%08lx] ", pc);
959 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) {
960 tprintf("[????????] ");
963 tprintf("[%08lx] ", regs.pc);
967 #endif /* !POWERPC */
974 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
975 perror("printcall: ptrace(PTRACE_GETREGS, ...)");
976 tprintf("[????????] ");
979 tprintf("[%08x] ", regs.r_o7);
984 tprintf("[????????] ");
998 /* We simply use the SunOS breakpoint code. */
1000 struct pt_regs regs;
1001 #define LOOPA 0x30800000 /* ba,a 0 */
1003 if (tcp->flags & TCB_BPTSET) {
1004 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1007 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1008 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1011 memmove (®s.u_regs [1], ®s.u_regs [0],
1012 sizeof (regs.u_regs) - sizeof (regs.u_regs [0]));
1013 tcp->baddr = regs.u_regs[UREG_I7] + 8;
1015 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)tcp->baddr, 0);
1017 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1022 * XXX - BRUTAL MODE ON
1023 * We cannot set a real BPT in the child, since it will not be
1024 * traced at the moment it will reach the trap and would probably
1025 * die with a core dump.
1026 * Thus, we are force our way in by taking out two instructions
1027 * and insert an eternal loop instead, in expectance of the SIGSTOP
1028 * generated by out PTRACE_ATTACH.
1029 * Of cause, if we evaporate ourselves in the middle of all this...
1032 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOPA);
1034 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1037 tcp->flags |= TCB_BPTSET;
1042 #define LOOP 0x0000feeb
1043 #elif defined (M68K)
1044 #define LOOP 0x60fe0000
1045 #elif defined (ALPHA)
1046 #define LOOP 0xc3ffffff
1047 #elif defined (POWERPC)
1048 #define LOOP 0x0000feeb
1050 #define LOOP -1 /* almost certainly wrong, jws */
1052 #error unknown architecture
1055 if (tcp->flags & TCB_BPTSET) {
1056 fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid);
1060 if (upeek(tcp->pid, 4*EIP, &tcp->baddr) < 0)
1062 #elif defined (M68K)
1063 if (upeek(tcp->pid, 4*PT_PC, &tcp->baddr) < 0)
1065 #elif defined (ALPHA)
1069 #elif defined (POWERPC)
1070 if (upeek(tcp->pid, 4*PT_NIP, &tcp->baddr) < 0)
1073 #error unknown architecture
1076 fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr);
1077 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *) tcp->baddr, 0);
1079 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1082 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP);
1084 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1087 tcp->flags |= TCB_BPTSET;
1093 #ifdef SPARC /* This code is slightly sparc specific */
1096 #define BPT 0x91d02001 /* ta 1 */
1097 #define LOOP 0x10800000 /* ba 0 */
1098 #define LOOPA 0x30800000 /* ba,a 0 */
1099 #define NOP 0x01000000
1101 static int loopdeloop[1] = {LOOPA};
1103 static int loopdeloop[2] = {LOOP, NOP};
1106 if (tcp->flags & TCB_BPTSET) {
1107 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1110 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1111 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1114 tcp->baddr = regs.r_o7 + 8;
1115 if (ptrace(PTRACE_READTEXT, tcp->pid, (char *)tcp->baddr,
1116 sizeof tcp->inst, (char *)tcp->inst) < 0) {
1117 perror("setbpt: ptrace(PTRACE_READTEXT, ...)");
1122 * XXX - BRUTAL MODE ON
1123 * We cannot set a real BPT in the child, since it will not be
1124 * traced at the moment it will reach the trap and would probably
1125 * die with a core dump.
1126 * Thus, we are force our way in by taking out two instructions
1127 * and insert an eternal loop in stead, in expectance of the SIGSTOP
1128 * generated by out PTRACE_ATTACH.
1129 * Of cause, if we evaporate ourselves in the middle of all this...
1131 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1132 sizeof loopdeloop, (char *) loopdeloop) < 0) {
1133 perror("setbpt: ptrace(PTRACE_WRITETEXT, ...)");
1136 tcp->flags |= TCB_BPTSET;
1155 #else /* !POWERPC */
1163 #endif /* !POWERPC */
1167 /* Again, we borrow the SunOS breakpoint code. */
1168 if (!(tcp->flags & TCB_BPTSET)) {
1169 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1173 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1175 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1178 tcp->flags &= ~TCB_BPTSET;
1182 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
1183 if (!(tcp->flags & TCB_BPTSET)) {
1184 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1188 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1190 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1193 tcp->flags &= ~TCB_BPTSET;
1196 if (upeek(tcp->pid, 4*EIP, &eip) < 0)
1198 if (eip != tcp->baddr) {
1199 /* The breakpoint has not been reached yet. */
1202 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1208 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0)
1210 if (pc != tcp->baddr) {
1211 /* The breakpoint has not been reached yet. */
1213 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1217 #else /* !POWERPC */
1219 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0)
1221 if (pc != tcp->baddr) {
1222 /* The breakpoint has not been reached yet. */
1224 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1230 if (upeek(tcp->pid, REG_PC, &pc) < 0)
1232 if (pc != tcp->baddr) {
1233 /* The breakpoint has not been reached yet. */
1235 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1241 #endif /* !POWERPC */
1253 if (!(tcp->flags & TCB_BPTSET)) {
1254 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1257 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1258 sizeof tcp->inst, (char *) tcp->inst) < 0) {
1259 perror("clearbtp: ptrace(PTRACE_WRITETEXT, ...)");
1262 tcp->flags &= ~TCB_BPTSET;
1266 * Since we don't have a single instruction breakpoint, we may have
1267 * to adjust the program counter after removing the our `breakpoint'.
1269 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1270 perror("clearbpt: ptrace(PTRACE_GETREGS, ...)");
1273 if ((regs.r_pc < tcp->baddr) ||
1274 (regs.r_pc > tcp->baddr + 4)) {
1275 /* The breakpoint has not been reached yet */
1278 "NOTE: PC not at bpt (pc %#x baddr %#x)\n",
1279 regs.r_pc, tcp->parent->baddr);
1282 if (regs.r_pc != tcp->baddr)
1284 fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n",
1285 regs.r_pc, tcp->baddr);
1287 regs.r_pc = tcp->baddr;
1288 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) {
1289 perror("clearbpt: ptrace(PTRACE_SETREGS, ...)");
1310 for (n = 0; n < sizeof *hdr; n += 4) {
1312 if (upeek(pid, uoff(u_exdata) + n, &res) < 0)
1314 memcpy(((char *) hdr) + n, &res, 4);
1317 fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n",
1318 hdr->a_magic, hdr->a_toolversion, hdr->a_machtype);
1319 fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n",
1320 hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry);
1331 * Change `vfork' in a freshly exec'ed dynamically linked
1332 * executable's (internal) symbol table to plain old `fork'
1336 struct link_dynamic dyn;
1337 struct link_dynamic_2 ld;
1340 if (getex(pid, &hdr) < 0)
1345 if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) {
1346 fprintf(stderr, "Cannot read DYNAMIC\n");
1349 if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) {
1350 fprintf(stderr, "Cannot read link_dynamic_2\n");
1353 if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) {
1354 fprintf(stderr, "fixvfork: out of memory\n");
1357 if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
1358 (int)ld.ld_symb_size, strtab) < 0)
1362 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
1363 fprintf(stderr, "[symbol: %s]\n", cp);
1368 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
1369 if (strcmp(cp, "_vfork") == 0) {
1371 fprintf(stderr, "fixvfork: FOUND _vfork\n");
1372 strcpy(cp, "_fork");
1377 if (cp < strtab + ld.ld_symb_size)
1379 * Write entire symbol table back to avoid
1380 * memory alignment bugs in ptrace
1382 if (tload(pid, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
1383 (int)ld.ld_symb_size, strtab) < 0)