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>
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 */
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);
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;
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, ... )");
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, ...)");
939 if (upeek(tcp->pid, 4*EIP, &eip) < 0) {
940 tprintf("[????????] ");
943 tprintf("[%08lx] ", eip);
947 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
948 tprintf("[????????] ");
951 tprintf("[%08lx] ", ip);
952 #elif defined(POWERPC)
955 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0) {
956 tprintf ("[????????] ");
959 tprintf("[%08lx] ", pc);
963 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
964 tprintf ("[????????] ");
967 tprintf("[%08lx] ", pc);
971 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
972 tprintf ("[????????] ");
975 tprintf("[%08lx] ", pc);
978 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) {
979 tprintf("[????????] ");
982 tprintf("[%08lx] ", regs.r_pc);
983 #endif /* !architecture */
989 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
990 perror("printcall: ptrace(PTRACE_GETREGS, ...)");
991 tprintf("[????????] ");
994 tprintf("[%08x] ", regs.r_o7);
999 tprintf("[????????] ");
1013 /* We simply use the SunOS breakpoint code. */
1016 #define LOOPA 0x30800000 /* ba,a 0 */
1018 if (tcp->flags & TCB_BPTSET) {
1019 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1022 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1023 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1026 tcp->baddr = regs.r_o7 + 8;
1028 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)tcp->baddr, 0);
1030 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1035 * XXX - BRUTAL MODE ON
1036 * We cannot set a real BPT in the child, since it will not be
1037 * traced at the moment it will reach the trap and would probably
1038 * die with a core dump.
1039 * Thus, we are force our way in by taking out two instructions
1040 * and insert an eternal loop instead, in expectance of the SIGSTOP
1041 * generated by out PTRACE_ATTACH.
1042 * Of cause, if we evaporate ourselves in the middle of all this...
1045 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOPA);
1047 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1050 tcp->flags |= TCB_BPTSET;
1055 * Our strategy here is to replace the bundle that contained
1056 * the clone() syscall with a bundle of the form:
1058 * { 1: br 1b; br 1b; br 1b }
1060 * This ensures that the newly forked child will loop
1061 * endlessly until we've got a chance to attach to it.
1064 # define LOOP0 0x0000100000000017
1065 # define LOOP1 0x4000000000200000
1066 unsigned long addr, ipsr;
1070 if (upeek(pid, PT_CR_IPSR, &ipsr) < 0)
1072 if (upeek(pid, PT_CR_IIP, &addr) < 0)
1074 tcp->baddr = addr | ((ipsr >> 41) & 0x3); /* store "ri" in low two bits */
1077 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 0, 0);
1078 tcp->inst[1] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 8, 0);
1080 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1085 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, LOOP0);
1086 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, LOOP1);
1088 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1091 tcp->flags |= TCB_BPTSET;
1096 #define LOOP 0x0000feeb
1097 #elif defined (M68K)
1098 #define LOOP 0x60fe0000
1099 #elif defined (ALPHA)
1100 #define LOOP 0xc3ffffff
1101 #elif defined (POWERPC)
1102 #define LOOP 0x0000feeb
1104 #define LOOP 0xEAFFFFFE
1106 #define LOOP 0x1000ffff
1108 #define LOOP 0xa7f40000 /* BRC 15,0 */
1110 #error unknown architecture
1113 if (tcp->flags & TCB_BPTSET) {
1114 fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid);
1118 if (upeek(tcp->pid, 4*EIP, &tcp->baddr) < 0)
1120 #elif defined (M68K)
1121 if (upeek(tcp->pid, 4*PT_PC, &tcp->baddr) < 0)
1123 #elif defined (ALPHA)
1127 #elif defined (MIPS)
1128 return -1; /* FIXME: I do not know what i do - Flo */
1129 #elif defined (POWERPC)
1130 if (upeek(tcp->pid, 4*PT_NIP, &tcp->baddr) < 0)
1133 if (upeek(tcp->pid,PT_PSWADDR, &tcp->baddr) < 0)
1136 #error unknown architecture
1139 fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr);
1140 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *) tcp->baddr, 0);
1142 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1145 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP);
1147 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1150 tcp->flags |= TCB_BPTSET;
1157 #ifdef SPARC /* This code is slightly sparc specific */
1160 #define BPT 0x91d02001 /* ta 1 */
1161 #define LOOP 0x10800000 /* ba 0 */
1162 #define LOOPA 0x30800000 /* ba,a 0 */
1163 #define NOP 0x01000000
1165 static int loopdeloop[1] = {LOOPA};
1167 static int loopdeloop[2] = {LOOP, NOP};
1170 if (tcp->flags & TCB_BPTSET) {
1171 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1174 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1175 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1178 tcp->baddr = regs.r_o7 + 8;
1179 if (ptrace(PTRACE_READTEXT, tcp->pid, (char *)tcp->baddr,
1180 sizeof tcp->inst, (char *)tcp->inst) < 0) {
1181 perror("setbpt: ptrace(PTRACE_READTEXT, ...)");
1186 * XXX - BRUTAL MODE ON
1187 * We cannot set a real BPT in the child, since it will not be
1188 * traced at the moment it will reach the trap and would probably
1189 * die with a core dump.
1190 * Thus, we are force our way in by taking out two instructions
1191 * and insert an eternal loop in stead, in expectance of the SIGSTOP
1192 * generated by out PTRACE_ATTACH.
1193 * Of cause, if we evaporate ourselves in the middle of all this...
1195 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1196 sizeof loopdeloop, (char *) loopdeloop) < 0) {
1197 perror("setbpt: ptrace(PTRACE_WRITETEXT, ...)");
1200 tcp->flags |= TCB_BPTSET;
1216 #elif defined(POWERPC)
1220 #elif defined(ALPHA)
1222 #endif /* architecture */
1225 /* Again, we borrow the SunOS breakpoint code. */
1226 if (!(tcp->flags & TCB_BPTSET)) {
1227 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1231 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1233 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1236 tcp->flags &= ~TCB_BPTSET;
1239 unsigned long addr, ipsr;
1244 if (upeek(pid, PT_CR_IPSR, &ipsr) < 0)
1246 if (upeek(pid, PT_CR_IIP, &addr) < 0)
1249 /* restore original bundle: */
1251 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, tcp->inst[0]);
1252 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, tcp->inst[1]);
1254 perror("clearbpt: ptrace(PTRACE_POKETEXT, ...)");
1258 /* restore original "ri" in ipsr: */
1259 ipsr = (ipsr & ~(0x3ul << 41)) | ((tcp->baddr & 0x3) << 41);
1261 ptrace(PTRACE_POKEUSER, pid, (char *) PT_CR_IPSR, ipsr);
1263 perror("clrbpt: ptrace(PTRACE_POKEUSER, ...)");
1267 tcp->flags &= ~TCB_BPTSET;
1269 if (addr != (tcp->baddr & ~0x3)) {
1270 /* the breakpoint has not been reached yet. */
1272 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1277 #else /* !IA64 && ! SPARC */
1280 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
1281 if (!(tcp->flags & TCB_BPTSET)) {
1282 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1286 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1288 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1291 tcp->flags &= ~TCB_BPTSET;
1294 if (upeek(tcp->pid, 4*EIP, &eip) < 0)
1296 if (eip != tcp->baddr) {
1297 /* The breakpoint has not been reached yet. */
1300 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1304 #elif defined(POWERPC)
1305 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0)
1307 if (pc != tcp->baddr) {
1308 /* The breakpoint has not been reached yet. */
1310 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1315 if (upeek(tcp->pid, 4*PT_PC, &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",
1324 #elif defined(ALPHA)
1325 if (upeek(tcp->pid, REG_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",
1335 #endif /* !SPARC && !IA64 */
1345 if (!(tcp->flags & TCB_BPTSET)) {
1346 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1349 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1350 sizeof tcp->inst, (char *) tcp->inst) < 0) {
1351 perror("clearbtp: ptrace(PTRACE_WRITETEXT, ...)");
1354 tcp->flags &= ~TCB_BPTSET;
1358 * Since we don't have a single instruction breakpoint, we may have
1359 * to adjust the program counter after removing the our `breakpoint'.
1361 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1362 perror("clearbpt: ptrace(PTRACE_GETREGS, ...)");
1365 if ((regs.r_pc < tcp->baddr) ||
1366 (regs.r_pc > tcp->baddr + 4)) {
1367 /* The breakpoint has not been reached yet */
1370 "NOTE: PC not at bpt (pc %#x baddr %#x)\n",
1371 regs.r_pc, tcp->parent->baddr);
1374 if (regs.r_pc != tcp->baddr)
1376 fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n",
1377 regs.r_pc, tcp->baddr);
1379 regs.r_pc = tcp->baddr;
1380 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) {
1381 perror("clearbpt: ptrace(PTRACE_SETREGS, ...)");
1402 for (n = 0; n < sizeof *hdr; n += 4) {
1404 if (upeek(pid, uoff(u_exdata) + n, &res) < 0)
1406 memcpy(((char *) hdr) + n, &res, 4);
1409 fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n",
1410 hdr->a_magic, hdr->a_toolversion, hdr->a_machtype);
1411 fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n",
1412 hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry);
1423 * Change `vfork' in a freshly exec'ed dynamically linked
1424 * executable's (internal) symbol table to plain old `fork'
1428 struct link_dynamic dyn;
1429 struct link_dynamic_2 ld;
1432 if (getex(pid, &hdr) < 0)
1437 if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) {
1438 fprintf(stderr, "Cannot read DYNAMIC\n");
1441 if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) {
1442 fprintf(stderr, "Cannot read link_dynamic_2\n");
1445 if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) {
1446 fprintf(stderr, "fixvfork: out of memory\n");
1449 if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
1450 (int)ld.ld_symb_size, strtab) < 0)
1454 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
1455 fprintf(stderr, "[symbol: %s]\n", cp);
1460 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
1461 if (strcmp(cp, "_vfork") == 0) {
1463 fprintf(stderr, "fixvfork: FOUND _vfork\n");
1464 strcpy(cp, "_fork");
1469 if (cp < strtab + ld.ld_symb_size)
1471 * Write entire symbol table back to avoid
1472 * memory alignment bugs in ptrace
1474 if (tload(pid, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
1475 (int)ld.ld_symb_size, strtab) < 0)