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 * 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>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
39 #include <sys/param.h>
42 #include <machine/reg.h>
48 #include <linux/ptrace.h>
53 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
56 #ifdef HAVE_SYS_PTRACE_H
57 #include <sys/ptrace.h>
60 #ifdef SUNOS4_KERNEL_ARCH_KLUDGE
61 #include <sys/utsname.h>
62 #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
64 #if defined(LINUX) && defined(SPARC)
68 #if !defined(__GLIBC__)
70 #include <linux/unistd.h>
72 #define _hack_syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,\
74 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
78 __asm__ volatile ("or %%g0, %1, %%o0\n\t" \
79 "or %%g0, %2, %%o1\n\t" \
80 "or %%g0, %3, %%o2\n\t" \
81 "or %%g0, %4, %%o3\n\t" \
82 "or %%g0, %5, %%o4\n\t" \
83 "or %%g0, %6, %%g1\n\t" \
86 "or %%g0, %%o0, %0\n\t" \
87 "sub %%g0, %%o0, %0\n\t" \
90 : "0" ((long)(arg1)),"1" ((long)(arg2)), \
91 "2" ((long)(arg3)),"3" ((long)(arg4)),"4" ((long)(arg5)), \
92 "i" (__NR_##syscall) \
93 : "g1", "o0", "o1", "o2", "o3", "o4"); \
95 return (type) __res; \
100 static _hack_syscall5(int,_ptrace,int,__request,int,__pid,int,__addr,int,__data,int,__addr2,ptrace)
110 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
113 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
130 return a->tv_sec || a->tv_usec;
135 struct timeval *a, *b;
137 if (a->tv_sec < b->tv_sec
138 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
140 if (a->tv_sec > b->tv_sec
141 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
150 return tv->tv_sec + tv->tv_usec/1000000.0;
155 struct timeval *tv, *a, *b;
157 tv->tv_sec = a->tv_sec + b->tv_sec;
158 tv->tv_usec = a->tv_usec + b->tv_usec;
159 if (tv->tv_usec > 1000000) {
161 tv->tv_usec -= 1000000;
167 struct timeval *tv, *a, *b;
169 tv->tv_sec = a->tv_sec - b->tv_sec;
170 tv->tv_usec = a->tv_usec - b->tv_usec;
171 if (((long) tv->tv_usec) < 0) {
173 tv->tv_usec += 1000000;
179 struct timeval *tv, *a;
182 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
183 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
184 tv->tv_usec %= 1000000;
189 struct timeval *tv, *a;
192 tv->tv_usec = a->tv_usec * n;
193 tv->tv_sec = a->tv_sec * n + a->tv_usec / 1000000;
194 tv->tv_usec %= 1000000;
202 for (; xlat->str != NULL; xlat++)
203 if (xlat->val == val)
209 * Print entry in struct xlat table, if there.
212 printxval(xlat, val, dflt)
217 char *str = xlookup(xlat, val);
222 tprintf("%#x /* %s */", val, dflt);
226 * Interpret `xlat' as an array of flags
227 * print the entries whose bits are on in `flags'
228 * return # of flags printed.
231 addflags(xlat, flags)
237 for (n = 0; xlat->str; xlat++) {
238 if (xlat->val && (flags & xlat->val) == xlat->val) {
239 tprintf("|%s", xlat->str);
245 tprintf("|%#x", flags);
252 printflags(xlat, flags)
259 if (flags == 0 && xlat->val == 0) {
260 tprintf("%s", xlat->str);
265 for (n = 0; xlat->str; xlat++) {
266 if (xlat->val && (flags & xlat->val) == xlat->val) {
267 tprintf("%s%s", sep, xlat->str);
274 tprintf("%s%#x", sep, flags);
281 printnum(tcp, addr, fmt)
292 if (umove(tcp, addr, &num) < 0) {
293 tprintf("%#lx", addr);
301 static char path[MAXPATHLEN + 1];
307 char buf[2 * MAXPATHLEN + 1];
310 if (!strpbrk(str, "\"\'\\")) {
311 tprintf("\"%s\"", str);
314 for (s = buf; *str; str++) {
316 case '\"': case '\'': case '\\':
317 *s++ = '\\'; *s++ = *str; break;
323 tprintf("\"%s\"", buf);
331 if (umovestr(tcp, addr, MAXPATHLEN, path) < 0)
332 tprintf("%#lx", addr);
339 printpathn(tcp, addr, n)
344 if (umovestr(tcp, addr, n, path) < 0)
345 tprintf("%#lx", addr);
353 printstr(tcp, addr, len)
358 static unsigned char *str = NULL;
368 if ((str = malloc(max_strlen)) == NULL
369 || (outstr = malloc(2*max_strlen)) == NULL) {
370 fprintf(stderr, "printstr: no memory\n");
371 tprintf("%#lx", addr);
375 outend = outstr + max_strlen * 2 - 10;
378 if (umovestr(tcp, addr, n, (char *) str) < 0) {
379 tprintf("%#lx", addr);
384 n = MIN(len, max_strlen);
385 if (umoven(tcp, addr, n, (char *) str) < 0) {
386 tprintf("%#lx", addr);
395 for (i = 0; i < n; i++) {
397 if (len < 0 && c == '\0')
399 if (!isprint(c) && !isspace(c)) {
410 for (i = 0; i < n; i++) {
412 if (len < 0 && c == '\0')
414 sprintf(s, "\\x%02x", c);
421 for (i = 0; i < n; i++) {
423 if (len < 0 && c == '\0')
426 case '\"': case '\'': case '\\':
427 *s++ = '\\'; *s++ = c; break;
429 *s++ = '\\'; *s++ = 'f'; break;
431 *s++ = '\\'; *s++ = 'n'; break;
433 *s++ = '\\'; *s++ = 'r'; break;
435 *s++ = '\\'; *s++ = 't'; break;
437 *s++ = '\\'; *s++ = 'v'; break;
441 else if (i < n - 1 && isdigit(str[i + 1])) {
442 sprintf(s, "\\%03o", c);
446 sprintf(s, "\\%o", c);
457 if (i < len || (len < 0 && (i == n || s > outend))) {
458 *s++ = '.'; *s++ = '.'; *s++ = '.';
461 tprintf("%s", outstr);
465 dumpstr(tcp, addr, len)
470 static int strsize = -1;
471 static unsigned char *str;
472 static char outstr[80];
479 if ((str = malloc(len)) == NULL) {
480 fprintf(stderr, "dump: no memory\n");
486 if (umoven(tcp, addr, len, (char *) str) < 0)
489 for (i = 0; i < len; i += 16) {
491 sprintf(s, " | %05x ", i);
493 for (j = 0; j < 16; j++) {
497 sprintf(s, " %02x", str[i + j]);
501 *s++ = ' '; *s++ = ' '; *s++ = ' ';
504 *s++ = ' '; *s++ = ' ';
505 for (j = 0; j < 16; j++) {
509 if (isprint(str[i + j]))
517 tprintf("%s |\n", outstr);
521 #define PAGMASK (~(PAGSIZ - 1))
523 * move `len' bytes of data from process `pid'
524 * at address `addr' to our space at `laddr'
527 umoven(tcp, addr, len, laddr)
540 char x[sizeof(long)];
543 if (addr & (sizeof(long) - 1)) {
544 /* addr not a multiple of sizeof(long) */
545 n = addr - (addr & -sizeof(long)); /* residue */
546 addr &= -sizeof(long); /* residue */
548 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
550 if (started && (errno==EPERM || errno==EIO)) {
551 /* Ran into 'end of memory' - stupid "printpath" */
554 /* But if not started, we had a bogus address. */
555 perror("ptrace: umoven");
559 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
560 addr += sizeof(long), laddr += m, len -= m;
564 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
566 if (started && (errno==EPERM || errno==EIO)) {
567 /* Ran into 'end of memory' - stupid "printpath" */
570 perror("ptrace: umoven");
574 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
575 addr += sizeof(long), laddr += m, len -= m;
585 char x[sizeof(long)];
588 if (addr & (sizeof(long) - 1)) {
589 /* addr not a multiple of sizeof(long) */
590 n = addr - (addr & -sizeof(long)); /* residue */
591 addr &= -sizeof(long); /* residue */
593 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
598 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
599 addr += sizeof(long), laddr += m, len -= m;
603 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
608 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
609 addr += sizeof(long), laddr += m, len -= m;
615 n = MIN(len, PAGSIZ);
616 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
617 if (ptrace(PTRACE_READDATA, pid,
618 (char *) addr, len, laddr) < 0) {
619 perror("umoven: ptrace(PTRACE_READDATA, ...)");
631 #ifdef HAVE_MP_PROCFS
632 if (pread(tcp->pfd_as, laddr, len, addr) == -1)
636 * We would like to use pread preferentially for speed
637 * but even though SGI has it in their library, it no longer works.
643 if (pread(tcp->pfd, laddr, len, addr) == -1)
645 #else /* !HAVE_PREAD */
646 lseek(tcp->pfd, addr, SEEK_SET);
647 if (read(tcp->pfd, laddr, len) == -1)
649 #endif /* !HAVE_PREAD */
650 #endif /* HAVE_MP_PROCFS */
657 * like `umove' but make the additional effort of looking
658 * for a terminating zero byte.
661 umovestr(tcp, addr, len, laddr)
668 return umoven(tcp, addr, len, laddr);
675 char x[sizeof(long)];
678 if (addr & (sizeof(long) - 1)) {
679 /* addr not a multiple of sizeof(long) */
680 n = addr - (addr & -sizeof(long)); /* residue */
681 addr &= -sizeof(long); /* residue */
683 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
685 if (started && (errno==EPERM || errno==EIO)) {
686 /* Ran into 'end of memory' - stupid "printpath" */
693 memcpy(laddr, &u.x[n], m = MIN(sizeof(long)-n,len));
694 while (n & (sizeof(long) - 1))
695 if (u.x[n++] == '\0')
697 addr += sizeof(long), laddr += m, len -= m;
701 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
703 if (started && (errno==EPERM || errno==EIO)) {
704 /* Ran into 'end of memory' - stupid "printpath" */
711 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
712 for (i = 0; i < sizeof(long); i++)
716 addr += sizeof(long), laddr += m, len -= m;
724 #define PTRACE_WRITETEXT 101
725 #define PTRACE_WRITEDATA 102
732 uload(cmd, pid, addr, len, laddr)
743 n = MIN(len, PAGSIZ);
744 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
745 if (ptrace(cmd, pid, (char *)addr, n, laddr) < 0) {
746 perror("uload: ptrace(PTRACE_WRITE, ...)");
758 char x[sizeof(long)];
761 if (cmd == PTRACE_WRITETEXT) {
762 peek = PTRACE_PEEKTEXT;
763 poke = PTRACE_POKETEXT;
766 peek = PTRACE_PEEKDATA;
767 poke = PTRACE_POKEDATA;
769 if (addr & (sizeof(long) - 1)) {
770 /* addr not a multiple of sizeof(long) */
771 n = addr - (addr & -sizeof(long)); /* residue */
772 addr &= -sizeof(long);
774 u.val = ptrace(peek, pid, (char *) addr, 0);
776 perror("uload: POKE");
779 memcpy(&u.x[n], laddr, m = MIN(sizeof(long) - n, len));
780 if (ptrace(poke, pid, (char *)addr, u.val) < 0) {
781 perror("uload: POKE");
784 addr += sizeof(long), laddr += m, len -= m;
787 if (len < sizeof(long))
788 u.val = ptrace(peek, pid, (char *) addr, 0);
789 memcpy(u.x, laddr, m = MIN(sizeof(long), len));
790 if (ptrace(poke, pid, (char *) addr, u.val) < 0) {
791 perror("uload: POKE");
794 addr += sizeof(long), laddr += m, len -= m;
801 tload(pid, addr, len, laddr)
806 return uload(PTRACE_WRITETEXT, pid, addr, len, laddr);
810 dload(pid, addr, len, laddr)
816 return uload(PTRACE_WRITEDATA, pid, addr, len, laddr);
831 #ifdef SUNOS4_KERNEL_ARCH_KLUDGE
833 static int is_sun4m = -1;
836 /* Round up the usual suspects. */
837 if (is_sun4m == -1) {
838 if (uname(&name) < 0) {
839 perror("upeek: uname?");
842 is_sun4m = strcmp(name.machine, "sun4m") == 0;
844 extern struct xlat struct_user_offsets[];
847 for (x = struct_user_offsets; x->str; x++)
854 #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
856 val = ptrace(PTRACE_PEEKUSER, pid, (char *) off, 0);
857 if (val == -1 && errno) {
858 perror("upeek: ptrace(PTRACE_PEEKUSER, ... )");
875 if (upeek(tcp->pid, 4*EIP, &pc) < 0)
878 if (upeek(tcp->pid, 4*15, &pc) < 0)
880 #elif defined(POWERPC)
881 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0)
884 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0)
887 if (upeek(tcp->pid, REG_PC, &pc) < 0)
890 if (upeek(tcp->pid, REG_EPC, &pc) < 0)
894 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
898 if(upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
905 * Return current program counter for `pid'
906 * Assumes PC is never 0xffffffff
910 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
911 perror("getpc: ptrace(PTRACE_GETREGS, ...)");
933 if (upeek(tcp->pid, 4*EIP, &eip) < 0) {
934 tprintf("[????????] ");
937 tprintf("[%08lx] ", eip);
942 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0) {
943 tprintf ("[????????] ");
946 tprintf("[%08lx] ", pc);
951 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
952 tprintf ("[????????] ");
955 tprintf("[%08lx] ", pc);
960 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
961 tprintf ("[????????] ");
964 tprintf("[%08lx] ", pc);
968 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) {
969 tprintf("[????????] ");
972 tprintf("[%08lx] ", regs.r_pc);
976 #endif /* !POWERPC */
983 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
984 perror("printcall: ptrace(PTRACE_GETREGS, ...)");
985 tprintf("[????????] ");
988 tprintf("[%08x] ", regs.r_o7);
993 tprintf("[????????] ");
1007 /* We simply use the SunOS breakpoint code. */
1010 #define LOOPA 0x30800000 /* ba,a 0 */
1012 if (tcp->flags & TCB_BPTSET) {
1013 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1016 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1017 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1020 tcp->baddr = regs.r_o7 + 8;
1022 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)tcp->baddr, 0);
1024 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1029 * XXX - BRUTAL MODE ON
1030 * We cannot set a real BPT in the child, since it will not be
1031 * traced at the moment it will reach the trap and would probably
1032 * die with a core dump.
1033 * Thus, we are force our way in by taking out two instructions
1034 * and insert an eternal loop instead, in expectance of the SIGSTOP
1035 * generated by out PTRACE_ATTACH.
1036 * Of cause, if we evaporate ourselves in the middle of all this...
1039 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOPA);
1041 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1044 tcp->flags |= TCB_BPTSET;
1049 #define LOOP 0x0000feeb
1050 #elif defined (M68K)
1051 #define LOOP 0x60fe0000
1052 #elif defined (ALPHA)
1053 #define LOOP 0xc3ffffff
1054 #elif defined (POWERPC)
1055 #define LOOP 0x0000feeb
1057 #define LOOP -1 /* almost certainly wrong, jws */
1059 #define LOOP 0x1000ffff
1061 #define LOOP 0xa7f40000 /* BRC 15,0 */
1063 #error unknown architecture
1066 if (tcp->flags & TCB_BPTSET) {
1067 fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid);
1071 if (upeek(tcp->pid, 4*EIP, &tcp->baddr) < 0)
1073 #elif defined (M68K)
1074 if (upeek(tcp->pid, 4*PT_PC, &tcp->baddr) < 0)
1076 #elif defined (ALPHA)
1080 #elif defined (MIPS)
1081 return -1; /* FIXME: I do not know what i do - Flo */
1082 #elif defined (POWERPC)
1083 if (upeek(tcp->pid, 4*PT_NIP, &tcp->baddr) < 0)
1086 if (upeek(tcp->pid,PT_PSWADDR, &tcp->baddr) < 0)
1089 #error unknown architecture
1092 fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr);
1093 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *) tcp->baddr, 0);
1095 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1098 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP);
1100 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1103 tcp->flags |= TCB_BPTSET;
1109 #ifdef SPARC /* This code is slightly sparc specific */
1112 #define BPT 0x91d02001 /* ta 1 */
1113 #define LOOP 0x10800000 /* ba 0 */
1114 #define LOOPA 0x30800000 /* ba,a 0 */
1115 #define NOP 0x01000000
1117 static int loopdeloop[1] = {LOOPA};
1119 static int loopdeloop[2] = {LOOP, NOP};
1122 if (tcp->flags & TCB_BPTSET) {
1123 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1126 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1127 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1130 tcp->baddr = regs.r_o7 + 8;
1131 if (ptrace(PTRACE_READTEXT, tcp->pid, (char *)tcp->baddr,
1132 sizeof tcp->inst, (char *)tcp->inst) < 0) {
1133 perror("setbpt: ptrace(PTRACE_READTEXT, ...)");
1138 * XXX - BRUTAL MODE ON
1139 * We cannot set a real BPT in the child, since it will not be
1140 * traced at the moment it will reach the trap and would probably
1141 * die with a core dump.
1142 * Thus, we are force our way in by taking out two instructions
1143 * and insert an eternal loop in stead, in expectance of the SIGSTOP
1144 * generated by out PTRACE_ATTACH.
1145 * Of cause, if we evaporate ourselves in the middle of all this...
1147 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1148 sizeof loopdeloop, (char *) loopdeloop) < 0) {
1149 perror("setbpt: ptrace(PTRACE_WRITETEXT, ...)");
1152 tcp->flags |= TCB_BPTSET;
1171 #else /* !POWERPC */
1179 #endif /* !POWERPC */
1183 /* Again, we borrow the SunOS breakpoint code. */
1184 if (!(tcp->flags & TCB_BPTSET)) {
1185 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1189 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1191 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1194 tcp->flags &= ~TCB_BPTSET;
1198 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
1199 if (!(tcp->flags & TCB_BPTSET)) {
1200 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1204 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1206 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1209 tcp->flags &= ~TCB_BPTSET;
1212 if (upeek(tcp->pid, 4*EIP, &eip) < 0)
1214 if (eip != tcp->baddr) {
1215 /* The breakpoint has not been reached yet. */
1218 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1224 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0)
1226 if (pc != tcp->baddr) {
1227 /* The breakpoint has not been reached yet. */
1229 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1233 #else /* !POWERPC */
1235 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0)
1237 if (pc != tcp->baddr) {
1238 /* The breakpoint has not been reached yet. */
1240 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1246 if (upeek(tcp->pid, REG_PC, &pc) < 0)
1248 if (pc != tcp->baddr) {
1249 /* The breakpoint has not been reached yet. */
1251 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1257 #endif /* !POWERPC */
1269 if (!(tcp->flags & TCB_BPTSET)) {
1270 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1273 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1274 sizeof tcp->inst, (char *) tcp->inst) < 0) {
1275 perror("clearbtp: ptrace(PTRACE_WRITETEXT, ...)");
1278 tcp->flags &= ~TCB_BPTSET;
1282 * Since we don't have a single instruction breakpoint, we may have
1283 * to adjust the program counter after removing the our `breakpoint'.
1285 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1286 perror("clearbpt: ptrace(PTRACE_GETREGS, ...)");
1289 if ((regs.r_pc < tcp->baddr) ||
1290 (regs.r_pc > tcp->baddr + 4)) {
1291 /* The breakpoint has not been reached yet */
1294 "NOTE: PC not at bpt (pc %#x baddr %#x)\n",
1295 regs.r_pc, tcp->parent->baddr);
1298 if (regs.r_pc != tcp->baddr)
1300 fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n",
1301 regs.r_pc, tcp->baddr);
1303 regs.r_pc = tcp->baddr;
1304 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) {
1305 perror("clearbpt: ptrace(PTRACE_SETREGS, ...)");
1326 for (n = 0; n < sizeof *hdr; n += 4) {
1328 if (upeek(pid, uoff(u_exdata) + n, &res) < 0)
1330 memcpy(((char *) hdr) + n, &res, 4);
1333 fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n",
1334 hdr->a_magic, hdr->a_toolversion, hdr->a_machtype);
1335 fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n",
1336 hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry);
1347 * Change `vfork' in a freshly exec'ed dynamically linked
1348 * executable's (internal) symbol table to plain old `fork'
1352 struct link_dynamic dyn;
1353 struct link_dynamic_2 ld;
1356 if (getex(pid, &hdr) < 0)
1361 if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) {
1362 fprintf(stderr, "Cannot read DYNAMIC\n");
1365 if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) {
1366 fprintf(stderr, "Cannot read link_dynamic_2\n");
1369 if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) {
1370 fprintf(stderr, "fixvfork: out of memory\n");
1373 if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
1374 (int)ld.ld_symb_size, strtab) < 0)
1378 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
1379 fprintf(stderr, "[symbol: %s]\n", cp);
1384 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
1385 if (strcmp(cp, "_vfork") == 0) {
1387 fprintf(stderr, "fixvfork: FOUND _vfork\n");
1388 strcpy(cp, "_fork");
1393 if (cp < strtab + ld.ld_symb_size)
1395 * Write entire symbol table back to avoid
1396 * memory alignment bugs in ptrace
1398 if (tload(pid, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
1399 (int)ld.ld_symb_size, strtab) < 0)