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>
47 #if defined(linux) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 1))
48 #include <linux/ptrace.h>
51 #if defined(LINUX) && defined(IA64)
52 #include <asm/ptrace_offsets.h>
57 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
58 #elif defined(HAVE_LINUX_PTRACE_H)
60 #include <linux/ptrace.h>
63 #ifdef SUNOS4_KERNEL_ARCH_KLUDGE
64 #include <sys/utsname.h>
65 #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
67 #if defined(LINUX) && defined(SPARC)
71 #if !defined(__GLIBC__)
73 #include <linux/unistd.h>
75 #define _hack_syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,\
77 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
81 __asm__ volatile ("or %%g0, %1, %%o0\n\t" \
82 "or %%g0, %2, %%o1\n\t" \
83 "or %%g0, %3, %%o2\n\t" \
84 "or %%g0, %4, %%o3\n\t" \
85 "or %%g0, %5, %%o4\n\t" \
86 "or %%g0, %6, %%g1\n\t" \
89 "or %%g0, %%o0, %0\n\t" \
90 "sub %%g0, %%o0, %0\n\t" \
93 : "0" ((long)(arg1)),"1" ((long)(arg2)), \
94 "2" ((long)(arg3)),"3" ((long)(arg4)),"4" ((long)(arg5)), \
95 "i" (__NR_##syscall) \
96 : "g1", "o0", "o1", "o2", "o3", "o4"); \
98 return (type) __res; \
103 static _hack_syscall5(int,_ptrace,int,__request,int,__pid,int,__addr,int,__data,int,__addr2,ptrace)
113 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
116 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
133 return a->tv_sec || a->tv_usec;
138 struct timeval *a, *b;
140 if (a->tv_sec < b->tv_sec
141 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
143 if (a->tv_sec > b->tv_sec
144 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
153 return tv->tv_sec + tv->tv_usec/1000000.0;
158 struct timeval *tv, *a, *b;
160 tv->tv_sec = a->tv_sec + b->tv_sec;
161 tv->tv_usec = a->tv_usec + b->tv_usec;
162 if (tv->tv_usec > 1000000) {
164 tv->tv_usec -= 1000000;
170 struct timeval *tv, *a, *b;
172 tv->tv_sec = a->tv_sec - b->tv_sec;
173 tv->tv_usec = a->tv_usec - b->tv_usec;
174 if (((long) tv->tv_usec) < 0) {
176 tv->tv_usec += 1000000;
182 struct timeval *tv, *a;
185 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
186 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
187 tv->tv_usec %= 1000000;
192 struct timeval *tv, *a;
195 tv->tv_usec = a->tv_usec * n;
196 tv->tv_sec = a->tv_sec * n + a->tv_usec / 1000000;
197 tv->tv_usec %= 1000000;
205 for (; xlat->str != NULL; xlat++)
206 if (xlat->val == val)
212 * Print entry in struct xlat table, if there.
215 printxval(xlat, val, dflt)
220 char *str = xlookup(xlat, val);
225 tprintf("%#x /* %s */", val, dflt);
229 * Interpret `xlat' as an array of flags
230 * print the entries whose bits are on in `flags'
231 * return # of flags printed.
234 addflags(xlat, flags)
240 for (n = 0; xlat->str; xlat++) {
241 if (xlat->val && (flags & xlat->val) == xlat->val) {
242 tprintf("|%s", xlat->str);
248 tprintf("|%#x", flags);
255 printflags(xlat, flags)
262 if (flags == 0 && xlat->val == 0) {
263 tprintf("%s", xlat->str);
268 for (n = 0; xlat->str; xlat++) {
269 if (xlat->val && (flags & xlat->val) == xlat->val) {
270 tprintf("%s%s", sep, xlat->str);
277 tprintf("%s%#x", sep, flags);
284 printnum(tcp, addr, fmt)
295 if (umove(tcp, addr, &num) < 0) {
296 tprintf("%#lx", addr);
304 static char path[MAXPATHLEN + 1];
310 char buf[2 * MAXPATHLEN + 1];
313 if (!strpbrk(str, "\"\'\\")) {
314 tprintf("\"%s\"", str);
317 for (s = buf; *str; str++) {
319 case '\"': case '\'': case '\\':
320 *s++ = '\\'; *s++ = *str; break;
326 tprintf("\"%s\"", buf);
334 if (umovestr(tcp, addr, MAXPATHLEN, path) < 0)
335 tprintf("%#lx", addr);
342 printpathn(tcp, addr, n)
347 if (umovestr(tcp, addr, n, path) < 0)
348 tprintf("%#lx", addr);
356 printstr(tcp, addr, len)
361 static unsigned char *str = NULL;
371 if ((str = malloc(max_strlen)) == NULL
372 || (outstr = malloc(2*max_strlen)) == NULL) {
373 fprintf(stderr, "printstr: no memory\n");
374 tprintf("%#lx", addr);
378 outend = outstr + max_strlen * 2 - 10;
381 if (umovestr(tcp, addr, n, (char *) str) < 0) {
382 tprintf("%#lx", addr);
387 n = MIN(len, max_strlen);
388 if (umoven(tcp, addr, n, (char *) str) < 0) {
389 tprintf("%#lx", addr);
398 for (i = 0; i < n; i++) {
400 if (len < 0 && c == '\0')
402 if (!isprint(c) && !isspace(c)) {
413 for (i = 0; i < n; i++) {
415 if (len < 0 && c == '\0')
417 sprintf(s, "\\x%02x", c);
424 for (i = 0; i < n; i++) {
426 if (len < 0 && c == '\0')
429 case '\"': case '\'': case '\\':
430 *s++ = '\\'; *s++ = c; break;
432 *s++ = '\\'; *s++ = 'f'; break;
434 *s++ = '\\'; *s++ = 'n'; break;
436 *s++ = '\\'; *s++ = 'r'; break;
438 *s++ = '\\'; *s++ = 't'; break;
440 *s++ = '\\'; *s++ = 'v'; break;
444 else if (i < n - 1 && isdigit(str[i + 1])) {
445 sprintf(s, "\\%03o", c);
449 sprintf(s, "\\%o", c);
460 if (i < len || (len < 0 && (i == n || s > outend))) {
461 *s++ = '.'; *s++ = '.'; *s++ = '.';
464 tprintf("%s", outstr);
468 dumpstr(tcp, addr, len)
473 static int strsize = -1;
474 static unsigned char *str;
475 static char outstr[80];
482 if ((str = malloc(len)) == NULL) {
483 fprintf(stderr, "dump: no memory\n");
489 if (umoven(tcp, addr, len, (char *) str) < 0)
492 for (i = 0; i < len; i += 16) {
494 sprintf(s, " | %05x ", i);
496 for (j = 0; j < 16; j++) {
500 sprintf(s, " %02x", str[i + j]);
504 *s++ = ' '; *s++ = ' '; *s++ = ' ';
507 *s++ = ' '; *s++ = ' ';
508 for (j = 0; j < 16; j++) {
512 if (isprint(str[i + j]))
520 tprintf("%s |\n", outstr);
524 #define PAGMASK (~(PAGSIZ - 1))
526 * move `len' bytes of data from process `pid'
527 * at address `addr' to our space at `laddr'
530 umoven(tcp, addr, len, laddr)
543 char x[sizeof(long)];
546 if (addr & (sizeof(long) - 1)) {
547 /* addr not a multiple of sizeof(long) */
548 n = addr - (addr & -sizeof(long)); /* residue */
549 addr &= -sizeof(long); /* residue */
551 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
553 if (started && (errno==EPERM || errno==EIO)) {
554 /* Ran into 'end of memory' - stupid "printpath" */
557 /* But if not started, we had a bogus address. */
558 perror("ptrace: umoven");
562 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
563 addr += sizeof(long), laddr += m, len -= m;
567 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
569 if (started && (errno==EPERM || errno==EIO)) {
570 /* Ran into 'end of memory' - stupid "printpath" */
573 perror("ptrace: umoven");
577 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
578 addr += sizeof(long), laddr += m, len -= m;
588 char x[sizeof(long)];
591 if (addr & (sizeof(long) - 1)) {
592 /* addr not a multiple of sizeof(long) */
593 n = addr - (addr & -sizeof(long)); /* residue */
594 addr &= -sizeof(long); /* residue */
596 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
601 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
602 addr += sizeof(long), laddr += m, len -= m;
606 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
611 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
612 addr += sizeof(long), laddr += m, len -= m;
618 n = MIN(len, PAGSIZ);
619 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
620 if (ptrace(PTRACE_READDATA, pid,
621 (char *) addr, len, laddr) < 0) {
622 perror("umoven: ptrace(PTRACE_READDATA, ...)");
634 #ifdef HAVE_MP_PROCFS
635 if (pread(tcp->pfd_as, laddr, len, addr) == -1)
639 * We would like to use pread preferentially for speed
640 * but even though SGI has it in their library, it no longer works.
646 if (pread(tcp->pfd, laddr, len, addr) == -1)
648 #else /* !HAVE_PREAD */
649 lseek(tcp->pfd, addr, SEEK_SET);
650 if (read(tcp->pfd, laddr, len) == -1)
652 #endif /* !HAVE_PREAD */
653 #endif /* HAVE_MP_PROCFS */
654 #endif /* USE_PROCFS */
660 * like `umove' but make the additional effort of looking
661 * for a terminating zero byte.
664 umovestr(tcp, addr, len, laddr)
671 return umoven(tcp, addr, len, laddr);
672 #else /* !USE_PROCFS */
678 char x[sizeof(long)];
681 if (addr & (sizeof(long) - 1)) {
682 /* addr not a multiple of sizeof(long) */
683 n = addr - (addr & -sizeof(long)); /* residue */
684 addr &= -sizeof(long); /* residue */
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[n], m = MIN(sizeof(long)-n,len));
697 while (n & (sizeof(long) - 1))
698 if (u.x[n++] == '\0')
700 addr += sizeof(long), laddr += m, len -= m;
704 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
706 if (started && (errno==EPERM || errno==EIO)) {
707 /* Ran into 'end of memory' - stupid "printpath" */
714 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
715 for (i = 0; i < sizeof(long); i++)
719 addr += sizeof(long), laddr += m, len -= m;
722 #endif /* !USE_PROCFS */
727 #define PTRACE_WRITETEXT 101
728 #define PTRACE_WRITEDATA 102
735 uload(cmd, pid, addr, len, laddr)
746 n = MIN(len, PAGSIZ);
747 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
748 if (ptrace(cmd, pid, (char *)addr, n, laddr) < 0) {
749 perror("uload: ptrace(PTRACE_WRITE, ...)");
761 char x[sizeof(long)];
764 if (cmd == PTRACE_WRITETEXT) {
765 peek = PTRACE_PEEKTEXT;
766 poke = PTRACE_POKETEXT;
769 peek = PTRACE_PEEKDATA;
770 poke = PTRACE_POKEDATA;
772 if (addr & (sizeof(long) - 1)) {
773 /* addr not a multiple of sizeof(long) */
774 n = addr - (addr & -sizeof(long)); /* residue */
775 addr &= -sizeof(long);
777 u.val = ptrace(peek, pid, (char *) addr, 0);
779 perror("uload: POKE");
782 memcpy(&u.x[n], laddr, m = MIN(sizeof(long) - n, len));
783 if (ptrace(poke, pid, (char *)addr, u.val) < 0) {
784 perror("uload: POKE");
787 addr += sizeof(long), laddr += m, len -= m;
790 if (len < sizeof(long))
791 u.val = ptrace(peek, pid, (char *) addr, 0);
792 memcpy(u.x, laddr, m = MIN(sizeof(long), len));
793 if (ptrace(poke, pid, (char *) addr, u.val) < 0) {
794 perror("uload: POKE");
797 addr += sizeof(long), laddr += m, len -= m;
804 tload(pid, addr, len, laddr)
809 return uload(PTRACE_WRITETEXT, pid, addr, len, laddr);
813 dload(pid, addr, len, laddr)
819 return uload(PTRACE_WRITEDATA, pid, addr, len, laddr);
834 #ifdef SUNOS4_KERNEL_ARCH_KLUDGE
836 static int is_sun4m = -1;
839 /* Round up the usual suspects. */
840 if (is_sun4m == -1) {
841 if (uname(&name) < 0) {
842 perror("upeek: uname?");
845 is_sun4m = strcmp(name.machine, "sun4m") == 0;
847 extern struct xlat struct_user_offsets[];
850 for (x = struct_user_offsets; x->str; x++)
857 #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
859 val = ptrace(PTRACE_PEEKUSER, pid, (char *) off, 0);
860 if (val == -1 && errno) {
861 perror("upeek: ptrace(PTRACE_PEEKUSER, ... )");
868 #endif /* !USE_PROCFS */
878 if (upeek(tcp->pid, 4*EIP, &pc) < 0)
881 if (upeek(tcp->pid, PT_B0, &pc) < 0)
884 if (upeek(tcp->pid, 4*15, &pc) < 0)
886 #elif defined(POWERPC)
887 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0)
890 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0)
893 if (upeek(tcp->pid, REG_PC, &pc) < 0)
896 if (upeek(tcp->pid, REG_EPC, &pc) < 0)
900 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
904 if(upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
911 * Return current program counter for `pid'
912 * Assumes PC is never 0xffffffff
916 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
917 perror("getpc: ptrace(PTRACE_GETREGS, ...)");
930 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
944 if (upeek(tcp->pid, 4*EIP, &eip) < 0) {
945 tprintf("[????????] ");
948 tprintf("[%08lx] ", eip);
952 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
953 tprintf("[????????] ");
956 tprintf("[%08lx] ", ip);
957 #elif defined(POWERPC)
960 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0) {
961 tprintf ("[????????] ");
964 tprintf("[%08lx] ", pc);
968 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
969 tprintf ("[????????] ");
972 tprintf("[%08lx] ", pc);
976 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
977 tprintf ("[????????] ");
980 tprintf("[%08lx] ", pc);
983 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) {
984 tprintf("[????????] ");
987 tprintf("[%08lx] ", regs.r_pc);
988 #endif /* !architecture */
994 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
995 perror("printcall: ptrace(PTRACE_GETREGS, ...)");
996 tprintf("[????????] ");
999 tprintf("[%08x] ", regs.r_o7);
1004 tprintf("[????????] ");
1009 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
1010 tprintf("[%08x] ", regs.r_eip);
1011 #endif /* FREEBSD */
1023 /* We simply use the SunOS breakpoint code. */
1026 #define LOOPA 0x30800000 /* ba,a 0 */
1028 if (tcp->flags & TCB_BPTSET) {
1029 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1032 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1033 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1036 tcp->baddr = regs.r_o7 + 8;
1038 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)tcp->baddr, 0);
1040 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1045 * XXX - BRUTAL MODE ON
1046 * We cannot set a real BPT in the child, since it will not be
1047 * traced at the moment it will reach the trap and would probably
1048 * die with a core dump.
1049 * Thus, we are force our way in by taking out two instructions
1050 * and insert an eternal loop instead, in expectance of the SIGSTOP
1051 * generated by out PTRACE_ATTACH.
1052 * Of cause, if we evaporate ourselves in the middle of all this...
1055 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOPA);
1057 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1060 tcp->flags |= TCB_BPTSET;
1065 * Our strategy here is to replace the bundle that contained
1066 * the clone() syscall with a bundle of the form:
1068 * { 1: br 1b; br 1b; br 1b }
1070 * This ensures that the newly forked child will loop
1071 * endlessly until we've got a chance to attach to it.
1074 # define LOOP0 0x0000100000000017
1075 # define LOOP1 0x4000000000200000
1076 unsigned long addr, ipsr;
1080 if (upeek(pid, PT_CR_IPSR, &ipsr) < 0)
1082 if (upeek(pid, PT_CR_IIP, &addr) < 0)
1084 tcp->baddr = addr | ((ipsr >> 41) & 0x3); /* store "ri" in low two bits */
1087 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 0, 0);
1088 tcp->inst[1] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 8, 0);
1090 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1095 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, LOOP0);
1096 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, LOOP1);
1098 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1101 tcp->flags |= TCB_BPTSET;
1106 #define LOOP 0x0000feeb
1107 #elif defined (M68K)
1108 #define LOOP 0x60fe0000
1109 #elif defined (ALPHA)
1110 #define LOOP 0xc3ffffff
1111 #elif defined (POWERPC)
1112 #define LOOP 0x0000feeb
1114 #define LOOP 0xEAFFFFFE
1116 #define LOOP 0x1000ffff
1118 #define LOOP 0xa7f40000 /* BRC 15,0 */
1120 #error unknown architecture
1123 if (tcp->flags & TCB_BPTSET) {
1124 fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid);
1128 if (upeek(tcp->pid, 4*EIP, &tcp->baddr) < 0)
1130 #elif defined (M68K)
1131 if (upeek(tcp->pid, 4*PT_PC, &tcp->baddr) < 0)
1133 #elif defined (ALPHA)
1137 #elif defined (MIPS)
1138 return -1; /* FIXME: I do not know what i do - Flo */
1139 #elif defined (POWERPC)
1140 if (upeek(tcp->pid, 4*PT_NIP, &tcp->baddr) < 0)
1143 if (upeek(tcp->pid,PT_PSWADDR, &tcp->baddr) < 0)
1146 #error unknown architecture
1149 fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr);
1150 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *) tcp->baddr, 0);
1152 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1155 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP);
1157 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1160 tcp->flags |= TCB_BPTSET;
1167 #ifdef SPARC /* This code is slightly sparc specific */
1170 #define BPT 0x91d02001 /* ta 1 */
1171 #define LOOP 0x10800000 /* ba 0 */
1172 #define LOOPA 0x30800000 /* ba,a 0 */
1173 #define NOP 0x01000000
1175 static int loopdeloop[1] = {LOOPA};
1177 static int loopdeloop[2] = {LOOP, NOP};
1180 if (tcp->flags & TCB_BPTSET) {
1181 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1184 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1185 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1188 tcp->baddr = regs.r_o7 + 8;
1189 if (ptrace(PTRACE_READTEXT, tcp->pid, (char *)tcp->baddr,
1190 sizeof tcp->inst, (char *)tcp->inst) < 0) {
1191 perror("setbpt: ptrace(PTRACE_READTEXT, ...)");
1196 * XXX - BRUTAL MODE ON
1197 * We cannot set a real BPT in the child, since it will not be
1198 * traced at the moment it will reach the trap and would probably
1199 * die with a core dump.
1200 * Thus, we are force our way in by taking out two instructions
1201 * and insert an eternal loop in stead, in expectance of the SIGSTOP
1202 * generated by out PTRACE_ATTACH.
1203 * Of cause, if we evaporate ourselves in the middle of all this...
1205 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1206 sizeof loopdeloop, (char *) loopdeloop) < 0) {
1207 perror("setbpt: ptrace(PTRACE_WRITETEXT, ...)");
1210 tcp->flags |= TCB_BPTSET;
1226 #elif defined(POWERPC)
1230 #elif defined(ALPHA)
1232 #endif /* architecture */
1235 /* Again, we borrow the SunOS breakpoint code. */
1236 if (!(tcp->flags & TCB_BPTSET)) {
1237 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1241 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1243 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1246 tcp->flags &= ~TCB_BPTSET;
1249 unsigned long addr, ipsr;
1254 if (upeek(pid, PT_CR_IPSR, &ipsr) < 0)
1256 if (upeek(pid, PT_CR_IIP, &addr) < 0)
1259 /* restore original bundle: */
1261 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, tcp->inst[0]);
1262 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, tcp->inst[1]);
1264 perror("clearbpt: ptrace(PTRACE_POKETEXT, ...)");
1268 /* restore original "ri" in ipsr: */
1269 ipsr = (ipsr & ~(0x3ul << 41)) | ((tcp->baddr & 0x3) << 41);
1271 ptrace(PTRACE_POKEUSER, pid, (char *) PT_CR_IPSR, ipsr);
1273 perror("clrbpt: ptrace(PTRACE_POKEUSER, ...)");
1277 tcp->flags &= ~TCB_BPTSET;
1279 if (addr != (tcp->baddr & ~0x3)) {
1280 /* the breakpoint has not been reached yet. */
1282 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1287 #else /* !IA64 && ! SPARC */
1290 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
1291 if (!(tcp->flags & TCB_BPTSET)) {
1292 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1296 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1298 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1301 tcp->flags &= ~TCB_BPTSET;
1304 if (upeek(tcp->pid, 4*EIP, &eip) < 0)
1306 if (eip != tcp->baddr) {
1307 /* The breakpoint has not been reached yet. */
1310 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1314 #elif defined(POWERPC)
1315 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0)
1317 if (pc != tcp->baddr) {
1318 /* The breakpoint has not been reached yet. */
1320 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1325 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0)
1327 if (pc != tcp->baddr) {
1328 /* The breakpoint has not been reached yet. */
1330 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1334 #elif defined(ALPHA)
1335 if (upeek(tcp->pid, REG_PC, &pc) < 0)
1337 if (pc != tcp->baddr) {
1338 /* The breakpoint has not been reached yet. */
1340 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1345 #endif /* !SPARC && !IA64 */
1355 if (!(tcp->flags & TCB_BPTSET)) {
1356 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1359 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1360 sizeof tcp->inst, (char *) tcp->inst) < 0) {
1361 perror("clearbtp: ptrace(PTRACE_WRITETEXT, ...)");
1364 tcp->flags &= ~TCB_BPTSET;
1368 * Since we don't have a single instruction breakpoint, we may have
1369 * to adjust the program counter after removing the our `breakpoint'.
1371 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1372 perror("clearbpt: ptrace(PTRACE_GETREGS, ...)");
1375 if ((regs.r_pc < tcp->baddr) ||
1376 (regs.r_pc > tcp->baddr + 4)) {
1377 /* The breakpoint has not been reached yet */
1380 "NOTE: PC not at bpt (pc %#x baddr %#x)\n",
1381 regs.r_pc, tcp->parent->baddr);
1384 if (regs.r_pc != tcp->baddr)
1386 fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n",
1387 regs.r_pc, tcp->baddr);
1389 regs.r_pc = tcp->baddr;
1390 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) {
1391 perror("clearbpt: ptrace(PTRACE_SETREGS, ...)");
1401 #endif /* !USE_PROCFS */
1412 for (n = 0; n < sizeof *hdr; n += 4) {
1414 if (upeek(pid, uoff(u_exdata) + n, &res) < 0)
1416 memcpy(((char *) hdr) + n, &res, 4);
1419 fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n",
1420 hdr->a_magic, hdr->a_toolversion, hdr->a_machtype);
1421 fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n",
1422 hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry);
1433 * Change `vfork' in a freshly exec'ed dynamically linked
1434 * executable's (internal) symbol table to plain old `fork'
1438 struct link_dynamic dyn;
1439 struct link_dynamic_2 ld;
1442 if (getex(pid, &hdr) < 0)
1447 if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) {
1448 fprintf(stderr, "Cannot read DYNAMIC\n");
1451 if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) {
1452 fprintf(stderr, "Cannot read link_dynamic_2\n");
1455 if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) {
1456 fprintf(stderr, "fixvfork: out of memory\n");
1459 if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
1460 (int)ld.ld_symb_size, strtab) < 0)
1464 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
1465 fprintf(stderr, "[symbol: %s]\n", cp);
1470 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
1471 if (strcmp(cp, "_vfork") == 0) {
1473 fprintf(stderr, "fixvfork: FOUND _vfork\n");
1474 strcpy(cp, "_fork");
1479 if (cp < strtab + ld.ld_symb_size)
1481 * Write entire symbol table back to avoid
1482 * memory alignment bugs in ptrace
1484 if (tload(pid, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
1485 (int)ld.ld_symb_size, strtab) < 0)