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>
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)
64 #if !defined(__GLIBC__)
66 #include <linux/unistd.h>
68 #define _hack_syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,\
70 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
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" \
82 "or %%g0, %%o0, %0\n\t" \
83 "sub %%g0, %%o0, %0\n\t" \
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"); \
91 return (type) __res; \
96 static _hack_syscall5(int,_ptrace,int,__request,int,__pid,int,__addr,int,__data,int,__addr2,ptrace)
106 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
109 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
126 return a->tv_sec || a->tv_usec;
131 struct timeval *a, *b;
133 if (a->tv_sec < b->tv_sec
134 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
136 if (a->tv_sec > b->tv_sec
137 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
146 return tv->tv_sec + tv->tv_usec/1000000.0;
151 struct timeval *tv, *a, *b;
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) {
157 tv->tv_usec -= 1000000;
163 struct timeval *tv, *a, *b;
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) {
169 tv->tv_usec += 1000000;
175 struct timeval *tv, *a;
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;
185 struct timeval *tv, *a;
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;
198 for (; xlat->str != NULL; xlat++)
199 if (xlat->val == val)
205 * Print entry in struct xlat table, if there.
208 printxval(xlat, val, dflt)
213 char *str = xlookup(xlat, val);
218 tprintf("%#x /* %s */", val, dflt);
222 * Interpret `xlat' as an array of flags
223 * print the entries whose bits are on in `flags'
224 * return # of flags printed.
227 addflags(xlat, flags)
233 for (n = 0; xlat->str; xlat++) {
234 if (xlat->val && (flags & xlat->val) == xlat->val) {
235 tprintf("|%s", xlat->str);
241 tprintf("|%#x", flags);
248 printflags(xlat, flags)
255 if (flags == 0 && xlat->val == 0) {
256 tprintf("%s", xlat->str);
261 for (n = 0; xlat->str; xlat++) {
262 if (xlat->val && (flags & xlat->val) == xlat->val) {
263 tprintf("%s%s", sep, xlat->str);
270 tprintf("%s%#x", sep, flags);
277 printnum(tcp, addr, fmt)
288 if (umove(tcp, addr, &num) < 0) {
289 tprintf("%#lx", addr);
297 static char path[MAXPATHLEN + 1];
303 char buf[2 * MAXPATHLEN + 1];
306 if (!strpbrk(str, "\"\'\\")) {
307 tprintf("\"%s\"", str);
310 for (s = buf; *str; str++) {
312 case '\"': case '\'': case '\\':
313 *s++ = '\\'; *s++ = *str; break;
319 tprintf("\"%s\"", buf);
327 if (umovestr(tcp, addr, MAXPATHLEN, path) < 0)
328 tprintf("%#lx", addr);
335 printpathn(tcp, addr, n)
340 if (umovestr(tcp, addr, n, path) < 0)
341 tprintf("%#lx", addr);
349 printstr(tcp, addr, len)
354 static unsigned char *str = NULL;
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);
371 outend = outstr + max_strlen * 2 - 10;
374 if (umovestr(tcp, addr, n, (char *) str) < 0) {
375 tprintf("%#lx", addr);
380 n = MIN(len, max_strlen);
381 if (umoven(tcp, addr, n, (char *) str) < 0) {
382 tprintf("%#lx", addr);
391 for (i = 0; i < n; i++) {
393 if (len < 0 && c == '\0')
395 if (!isprint(c) && !isspace(c)) {
406 for (i = 0; i < n; i++) {
408 if (len < 0 && c == '\0')
410 sprintf(s, "\\x%02x", c);
417 for (i = 0; i < n; i++) {
419 if (len < 0 && c == '\0')
422 case '\"': case '\'': case '\\':
423 *s++ = '\\'; *s++ = c; break;
425 *s++ = '\\'; *s++ = 'f'; break;
427 *s++ = '\\'; *s++ = 'n'; break;
429 *s++ = '\\'; *s++ = 'r'; break;
431 *s++ = '\\'; *s++ = 't'; break;
433 *s++ = '\\'; *s++ = 'v'; break;
437 else if (i < n - 1 && isdigit(str[i + 1])) {
438 sprintf(s, "\\%03o", c);
442 sprintf(s, "\\%o", c);
453 if (i < len || (len < 0 && (i == n || s > outend))) {
454 *s++ = '.'; *s++ = '.'; *s++ = '.';
457 tprintf("%s", outstr);
461 dumpstr(tcp, addr, len)
466 static int strsize = -1;
467 static unsigned char *str;
468 static char outstr[80];
475 if ((str = malloc(len)) == NULL) {
476 fprintf(stderr, "dump: no memory\n");
482 if (umoven(tcp, addr, len, (char *) str) < 0)
485 for (i = 0; i < len; i += 16) {
487 sprintf(s, " | %05x ", i);
489 for (j = 0; j < 16; j++) {
493 sprintf(s, " %02x", str[i + j]);
497 *s++ = ' '; *s++ = ' '; *s++ = ' ';
500 *s++ = ' '; *s++ = ' ';
501 for (j = 0; j < 16; j++) {
505 if (isprint(str[i + j]))
513 tprintf("%s |\n", outstr);
517 #define PAGMASK (~(PAGSIZ - 1))
519 * move `len' bytes of data from process `pid'
520 * at address `addr' to our space at `laddr'
523 umoven(tcp, addr, len, laddr)
536 char x[sizeof(long)];
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 */
544 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
546 if (started && (errno==EPERM || errno==EIO)) {
547 /* Ran into 'end of memory' - stupid "printpath" */
550 /* But if not started, we had a bogus address. */
551 perror("ptrace: umoven");
555 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
556 addr += sizeof(long), laddr += m, len -= m;
560 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
562 if (started && (errno==EPERM || errno==EIO)) {
563 /* Ran into 'end of memory' - stupid "printpath" */
566 perror("ptrace: umoven");
570 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
571 addr += sizeof(long), laddr += m, len -= m;
581 char x[sizeof(long)];
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 */
589 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
594 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
595 addr += sizeof(long), laddr += m, len -= m;
599 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
604 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
605 addr += sizeof(long), laddr += m, len -= m;
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, ...)");
628 if (pread(tcp->pfd_as, laddr, len, addr) == -1)
632 * We would like to use pread preferentially for speed
633 * but even though SGI has it in their library, it no longer works.
639 if (pread(tcp->pfd, laddr, len, addr) == -1)
641 #else /* !HAVE_PREAD */
642 lseek(tcp->pfd, addr, SEEK_SET);
643 if (read(tcp->pfd, laddr, len) == -1)
645 #endif /* !HAVE_PREAD */
653 * like `umove' but make the additional effort of looking
654 * for a terminating zero byte.
657 umovestr(tcp, addr, len, laddr)
664 return umoven(tcp, addr, len, laddr);
671 char x[sizeof(long)];
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 */
679 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
681 if (started && (errno==EPERM || errno==EIO)) {
682 /* Ran into 'end of memory' - stupid "printpath" */
689 memcpy(laddr, &u.x[n], m = MIN(sizeof(long)-n,len));
690 while (n & (sizeof(long) - 1))
691 if (u.x[n++] == '\0')
693 addr += sizeof(long), laddr += m, len -= m;
697 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
699 if (started && (errno==EPERM || errno==EIO)) {
700 /* Ran into 'end of memory' - stupid "printpath" */
707 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
708 for (i = 0; i < sizeof(long); i++)
712 addr += sizeof(long), laddr += m, len -= m;
720 #define PTRACE_WRITETEXT 101
721 #define PTRACE_WRITEDATA 102
728 uload(cmd, pid, addr, len, laddr)
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, ...)");
754 char x[sizeof(long)];
757 if (cmd == PTRACE_WRITETEXT) {
758 peek = PTRACE_PEEKTEXT;
759 poke = PTRACE_POKETEXT;
762 peek = PTRACE_PEEKDATA;
763 poke = PTRACE_POKEDATA;
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);
770 u.val = ptrace(peek, pid, (char *) addr, 0);
772 perror("uload: POKE");
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");
780 addr += sizeof(long), laddr += m, len -= m;
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");
790 addr += sizeof(long), laddr += m, len -= m;
797 tload(pid, addr, len, laddr)
802 return uload(PTRACE_WRITETEXT, pid, addr, len, laddr);
806 dload(pid, addr, len, laddr)
812 return uload(PTRACE_WRITEDATA, pid, addr, len, laddr);
827 #ifdef SUNOS4_KERNEL_ARCH_KLUDGE
829 static int is_sun4m = -1;
832 /* Round up the usual suspects. */
833 if (is_sun4m == -1) {
834 if (uname(&name) < 0) {
835 perror("upeek: uname?");
838 is_sun4m = strcmp(name.machine, "sun4m") == 0;
840 extern struct xlat struct_user_offsets[];
843 for (x = struct_user_offsets; x->str; x++)
850 #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
852 val = ptrace(PTRACE_PEEKUSER, pid, (char *) off, 0);
853 if (val == -1 && errno) {
854 perror("upeek: ptrace(PTRACE_PEEKUSER, ... )");
872 if (upeek(tcp->pid, 4*EIP, &pc) < 0)
875 if (upeek(tcp->pid, 4*15, &pc) < 0)
877 #elif defined(POWERPC)
878 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0)
881 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0)
884 if (upeek(tcp->pid, REG_PC, &pc) < 0)
887 if (upeek(tcp->pid, REG_EPC, &pc) < 0)
891 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
900 * Return current program counter for `pid'
901 * Assumes PC is never 0xffffffff
905 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
906 perror("getpc: ptrace(PTRACE_GETREGS, ...)");
928 if (upeek(tcp->pid, 4*EIP, &eip) < 0) {
929 tprintf("[????????] ");
932 tprintf("[%08lx] ", eip);
937 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0) {
938 tprintf ("[????????] ");
941 tprintf("[%08lx] ", pc);
946 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
947 tprintf ("[????????] ");
950 tprintf("[%08lx] ", pc);
955 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
956 tprintf ("[????????] ");
959 tprintf("[%08lx] ", pc);
963 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) {
964 tprintf("[????????] ");
967 tprintf("[%08lx] ", regs.r_pc);
971 #endif /* !POWERPC */
978 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
979 perror("printcall: ptrace(PTRACE_GETREGS, ...)");
980 tprintf("[????????] ");
983 tprintf("[%08x] ", regs.r_o7);
988 tprintf("[????????] ");
1002 /* We simply use the SunOS breakpoint code. */
1005 #define LOOPA 0x30800000 /* ba,a 0 */
1007 if (tcp->flags & TCB_BPTSET) {
1008 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1011 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1012 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1015 tcp->baddr = regs.r_o7 + 8;
1017 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)tcp->baddr, 0);
1019 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1024 * XXX - BRUTAL MODE ON
1025 * We cannot set a real BPT in the child, since it will not be
1026 * traced at the moment it will reach the trap and would probably
1027 * die with a core dump.
1028 * Thus, we are force our way in by taking out two instructions
1029 * and insert an eternal loop instead, in expectance of the SIGSTOP
1030 * generated by out PTRACE_ATTACH.
1031 * Of cause, if we evaporate ourselves in the middle of all this...
1034 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOPA);
1036 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1039 tcp->flags |= TCB_BPTSET;
1044 #define LOOP 0x0000feeb
1045 #elif defined (M68K)
1046 #define LOOP 0x60fe0000
1047 #elif defined (ALPHA)
1048 #define LOOP 0xc3ffffff
1049 #elif defined (POWERPC)
1050 #define LOOP 0x0000feeb
1052 #define LOOP -1 /* almost certainly wrong, jws */
1054 #define LOOP 0x1000ffff
1056 #error unknown architecture
1059 if (tcp->flags & TCB_BPTSET) {
1060 fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid);
1064 if (upeek(tcp->pid, 4*EIP, &tcp->baddr) < 0)
1066 #elif defined (M68K)
1067 if (upeek(tcp->pid, 4*PT_PC, &tcp->baddr) < 0)
1069 #elif defined (ALPHA)
1073 #elif defined (MIPS)
1074 return -1; /* FIXME: I do not know what i do - Flo */
1075 #elif defined (POWERPC)
1076 if (upeek(tcp->pid, 4*PT_NIP, &tcp->baddr) < 0)
1079 #error unknown architecture
1082 fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr);
1083 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *) tcp->baddr, 0);
1085 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1088 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP);
1090 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1093 tcp->flags |= TCB_BPTSET;
1099 #ifdef SPARC /* This code is slightly sparc specific */
1102 #define BPT 0x91d02001 /* ta 1 */
1103 #define LOOP 0x10800000 /* ba 0 */
1104 #define LOOPA 0x30800000 /* ba,a 0 */
1105 #define NOP 0x01000000
1107 static int loopdeloop[1] = {LOOPA};
1109 static int loopdeloop[2] = {LOOP, NOP};
1112 if (tcp->flags & TCB_BPTSET) {
1113 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1116 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1117 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1120 tcp->baddr = regs.r_o7 + 8;
1121 if (ptrace(PTRACE_READTEXT, tcp->pid, (char *)tcp->baddr,
1122 sizeof tcp->inst, (char *)tcp->inst) < 0) {
1123 perror("setbpt: ptrace(PTRACE_READTEXT, ...)");
1128 * XXX - BRUTAL MODE ON
1129 * We cannot set a real BPT in the child, since it will not be
1130 * traced at the moment it will reach the trap and would probably
1131 * die with a core dump.
1132 * Thus, we are force our way in by taking out two instructions
1133 * and insert an eternal loop in stead, in expectance of the SIGSTOP
1134 * generated by out PTRACE_ATTACH.
1135 * Of cause, if we evaporate ourselves in the middle of all this...
1137 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1138 sizeof loopdeloop, (char *) loopdeloop) < 0) {
1139 perror("setbpt: ptrace(PTRACE_WRITETEXT, ...)");
1142 tcp->flags |= TCB_BPTSET;
1161 #else /* !POWERPC */
1169 #endif /* !POWERPC */
1173 /* Again, we borrow the SunOS breakpoint code. */
1174 if (!(tcp->flags & TCB_BPTSET)) {
1175 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1179 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1181 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1184 tcp->flags &= ~TCB_BPTSET;
1188 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
1189 if (!(tcp->flags & TCB_BPTSET)) {
1190 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1194 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1196 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1199 tcp->flags &= ~TCB_BPTSET;
1202 if (upeek(tcp->pid, 4*EIP, &eip) < 0)
1204 if (eip != tcp->baddr) {
1205 /* The breakpoint has not been reached yet. */
1208 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1214 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0)
1216 if (pc != tcp->baddr) {
1217 /* The breakpoint has not been reached yet. */
1219 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1223 #else /* !POWERPC */
1225 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0)
1227 if (pc != tcp->baddr) {
1228 /* The breakpoint has not been reached yet. */
1230 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1236 if (upeek(tcp->pid, REG_PC, &pc) < 0)
1238 if (pc != tcp->baddr) {
1239 /* The breakpoint has not been reached yet. */
1241 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1247 #endif /* !POWERPC */
1259 if (!(tcp->flags & TCB_BPTSET)) {
1260 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1263 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1264 sizeof tcp->inst, (char *) tcp->inst) < 0) {
1265 perror("clearbtp: ptrace(PTRACE_WRITETEXT, ...)");
1268 tcp->flags &= ~TCB_BPTSET;
1272 * Since we don't have a single instruction breakpoint, we may have
1273 * to adjust the program counter after removing the our `breakpoint'.
1275 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1276 perror("clearbpt: ptrace(PTRACE_GETREGS, ...)");
1279 if ((regs.r_pc < tcp->baddr) ||
1280 (regs.r_pc > tcp->baddr + 4)) {
1281 /* The breakpoint has not been reached yet */
1284 "NOTE: PC not at bpt (pc %#x baddr %#x)\n",
1285 regs.r_pc, tcp->parent->baddr);
1288 if (regs.r_pc != tcp->baddr)
1290 fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n",
1291 regs.r_pc, tcp->baddr);
1293 regs.r_pc = tcp->baddr;
1294 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) {
1295 perror("clearbpt: ptrace(PTRACE_SETREGS, ...)");
1316 for (n = 0; n < sizeof *hdr; n += 4) {
1318 if (upeek(pid, uoff(u_exdata) + n, &res) < 0)
1320 memcpy(((char *) hdr) + n, &res, 4);
1323 fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n",
1324 hdr->a_magic, hdr->a_toolversion, hdr->a_machtype);
1325 fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n",
1326 hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry);
1337 * Change `vfork' in a freshly exec'ed dynamically linked
1338 * executable's (internal) symbol table to plain old `fork'
1342 struct link_dynamic dyn;
1343 struct link_dynamic_2 ld;
1346 if (getex(pid, &hdr) < 0)
1351 if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) {
1352 fprintf(stderr, "Cannot read DYNAMIC\n");
1355 if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) {
1356 fprintf(stderr, "Cannot read link_dynamic_2\n");
1359 if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) {
1360 fprintf(stderr, "fixvfork: out of memory\n");
1363 if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
1364 (int)ld.ld_symb_size, strtab) < 0)
1368 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
1369 fprintf(stderr, "[symbol: %s]\n", cp);
1374 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
1375 if (strcmp(cp, "_vfork") == 0) {
1377 fprintf(stderr, "fixvfork: FOUND _vfork\n");
1378 strcpy(cp, "_fork");
1383 if (cp < strtab + ld.ld_symb_size)
1385 * Write entire symbol table back to avoid
1386 * memory alignment bugs in ptrace
1388 if (tload(pid, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
1389 (int)ld.ld_symb_size, strtab) < 0)