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>
45 #include <machine/reg.h>
50 #if defined(linux) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 1))
51 #include <linux/ptrace.h>
54 #if defined(LINUX) && defined(IA64)
55 #include <asm/ptrace_offsets.h>
60 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
61 #elif defined(HAVE_LINUX_PTRACE_H)
63 #include <linux/ptrace.h>
66 #ifdef SUNOS4_KERNEL_ARCH_KLUDGE
67 #include <sys/utsname.h>
68 #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
70 #if defined(LINUX) && defined(SPARC)
74 #if !defined(__GLIBC__)
76 #include <linux/unistd.h>
78 #define _hack_syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,\
80 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
84 __asm__ volatile ("or %%g0, %1, %%o0\n\t" \
85 "or %%g0, %2, %%o1\n\t" \
86 "or %%g0, %3, %%o2\n\t" \
87 "or %%g0, %4, %%o3\n\t" \
88 "or %%g0, %5, %%o4\n\t" \
89 "or %%g0, %6, %%g1\n\t" \
92 "or %%g0, %%o0, %0\n\t" \
93 "sub %%g0, %%o0, %0\n\t" \
96 : "0" ((long)(arg1)),"1" ((long)(arg2)), \
97 "2" ((long)(arg3)),"3" ((long)(arg4)),"4" ((long)(arg5)), \
98 "i" (__NR_##syscall) \
99 : "g1", "o0", "o1", "o2", "o3", "o4"); \
101 return (type) __res; \
106 static _hack_syscall5(int,_ptrace,int,__request,int,__pid,int,__addr,int,__data,int,__addr2,ptrace)
116 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
119 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
136 return a->tv_sec || a->tv_usec;
141 struct timeval *a, *b;
143 if (a->tv_sec < b->tv_sec
144 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
146 if (a->tv_sec > b->tv_sec
147 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
156 return tv->tv_sec + tv->tv_usec/1000000.0;
161 struct timeval *tv, *a, *b;
163 tv->tv_sec = a->tv_sec + b->tv_sec;
164 tv->tv_usec = a->tv_usec + b->tv_usec;
165 if (tv->tv_usec > 1000000) {
167 tv->tv_usec -= 1000000;
173 struct timeval *tv, *a, *b;
175 tv->tv_sec = a->tv_sec - b->tv_sec;
176 tv->tv_usec = a->tv_usec - b->tv_usec;
177 if (((long) tv->tv_usec) < 0) {
179 tv->tv_usec += 1000000;
185 struct timeval *tv, *a;
188 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
189 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
190 tv->tv_usec %= 1000000;
195 struct timeval *tv, *a;
198 tv->tv_usec = a->tv_usec * n;
199 tv->tv_sec = a->tv_sec * n + a->tv_usec / 1000000;
200 tv->tv_usec %= 1000000;
208 for (; xlat->str != NULL; xlat++)
209 if (xlat->val == val)
215 * Print entry in struct xlat table, if there.
218 printxval(xlat, val, dflt)
223 char *str = xlookup(xlat, val);
228 tprintf("%#x /* %s */", val, dflt);
232 * Interpret `xlat' as an array of flags
233 * print the entries whose bits are on in `flags'
234 * return # of flags printed.
237 addflags(xlat, flags)
243 for (n = 0; xlat->str; xlat++) {
244 if (xlat->val && (flags & xlat->val) == xlat->val) {
245 tprintf("|%s", xlat->str);
251 tprintf("|%#x", flags);
258 printflags(xlat, flags)
265 if (flags == 0 && xlat->val == 0) {
266 tprintf("%s", xlat->str);
271 for (n = 0; xlat->str; xlat++) {
272 if (xlat->val && (flags & xlat->val) == xlat->val) {
273 tprintf("%s%s", sep, xlat->str);
280 tprintf("%s%#x", sep, flags);
287 printnum(tcp, addr, fmt)
298 if (umove(tcp, addr, &num) < 0) {
299 tprintf("%#lx", addr);
307 static char path[MAXPATHLEN + 1];
313 char buf[2 * MAXPATHLEN + 1];
316 if (!strpbrk(str, "\"\'\\")) {
317 tprintf("\"%s\"", str);
320 for (s = buf; *str; str++) {
322 case '\"': case '\'': case '\\':
323 *s++ = '\\'; *s++ = *str; break;
329 tprintf("\"%s\"", buf);
337 if (umovestr(tcp, addr, MAXPATHLEN, path) < 0)
338 tprintf("%#lx", addr);
345 printpathn(tcp, addr, n)
350 if (umovestr(tcp, addr, n, path) < 0)
351 tprintf("%#lx", addr);
359 printstr(tcp, addr, len)
364 static unsigned char *str = NULL;
374 if ((str = malloc(max_strlen)) == NULL
375 || (outstr = malloc(2*max_strlen)) == NULL) {
376 fprintf(stderr, "printstr: no memory\n");
377 tprintf("%#lx", addr);
381 outend = outstr + max_strlen * 2 - 10;
384 if (umovestr(tcp, addr, n, (char *) str) < 0) {
385 tprintf("%#lx", addr);
390 n = MIN(len, max_strlen);
391 if (umoven(tcp, addr, n, (char *) str) < 0) {
392 tprintf("%#lx", addr);
401 for (i = 0; i < n; i++) {
403 if (len < 0 && c == '\0')
405 if (!isprint(c) && !isspace(c)) {
416 for (i = 0; i < n; i++) {
418 if (len < 0 && c == '\0')
420 sprintf(s, "\\x%02x", c);
427 for (i = 0; i < n; i++) {
429 if (len < 0 && c == '\0')
432 case '\"': case '\'': case '\\':
433 *s++ = '\\'; *s++ = c; break;
435 *s++ = '\\'; *s++ = 'f'; break;
437 *s++ = '\\'; *s++ = 'n'; break;
439 *s++ = '\\'; *s++ = 'r'; break;
441 *s++ = '\\'; *s++ = 't'; break;
443 *s++ = '\\'; *s++ = 'v'; break;
447 else if (i < n - 1 && isdigit(str[i + 1])) {
448 sprintf(s, "\\%03o", c);
452 sprintf(s, "\\%o", c);
463 if (i < len || (len < 0 && (i == n || s > outend))) {
464 *s++ = '.'; *s++ = '.'; *s++ = '.';
467 tprintf("%s", outstr);
472 dumpiov(tcp, len, addr)
481 if ((iov = (struct iovec *) malloc(len * sizeof *iov)) == NULL) {
482 fprintf(stderr, "dump: No memory");
485 if (umoven(tcp, addr,
486 len * sizeof *iov, (char *) iov) >= 0) {
488 for (i = 0; i < len; i++) {
489 /* include the buffer number to make it easy to
490 * match up the trace with the source */
491 tprintf(" * %lu bytes in buffer %d\n",
492 (unsigned long)iov[i].iov_len, i);
493 dumpstr(tcp, (long) iov[i].iov_base,
503 dumpstr(tcp, addr, len)
508 static int strsize = -1;
509 static unsigned char *str;
510 static char outstr[80];
517 if ((str = malloc(len)) == NULL) {
518 fprintf(stderr, "dump: no memory\n");
524 if (umoven(tcp, addr, len, (char *) str) < 0)
527 for (i = 0; i < len; i += 16) {
529 sprintf(s, " | %05x ", i);
531 for (j = 0; j < 16; j++) {
535 sprintf(s, " %02x", str[i + j]);
539 *s++ = ' '; *s++ = ' '; *s++ = ' ';
542 *s++ = ' '; *s++ = ' ';
543 for (j = 0; j < 16; j++) {
547 if (isprint(str[i + j]))
555 tprintf("%s |\n", outstr);
559 #define PAGMASK (~(PAGSIZ - 1))
561 * move `len' bytes of data from process `pid'
562 * at address `addr' to our space at `laddr'
565 umoven(tcp, addr, len, laddr)
578 char x[sizeof(long)];
581 if (addr & (sizeof(long) - 1)) {
582 /* addr not a multiple of sizeof(long) */
583 n = addr - (addr & -sizeof(long)); /* residue */
584 addr &= -sizeof(long); /* residue */
586 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
588 if (started && (errno==EPERM || errno==EIO)) {
589 /* Ran into 'end of memory' - stupid "printpath" */
592 /* But if not started, we had a bogus address. */
593 perror("ptrace: umoven");
597 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
598 addr += sizeof(long), laddr += m, len -= m;
602 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
604 if (started && (errno==EPERM || errno==EIO)) {
605 /* Ran into 'end of memory' - stupid "printpath" */
608 perror("ptrace: umoven");
612 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
613 addr += sizeof(long), laddr += m, len -= m;
623 char x[sizeof(long)];
626 if (addr & (sizeof(long) - 1)) {
627 /* addr not a multiple of sizeof(long) */
628 n = addr - (addr & -sizeof(long)); /* residue */
629 addr &= -sizeof(long); /* residue */
631 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
636 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
637 addr += sizeof(long), laddr += m, len -= m;
641 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
646 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
647 addr += sizeof(long), laddr += m, len -= m;
653 n = MIN(len, PAGSIZ);
654 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
655 if (ptrace(PTRACE_READDATA, pid,
656 (char *) addr, len, laddr) < 0) {
657 perror("umoven: ptrace(PTRACE_READDATA, ...)");
669 #ifdef HAVE_MP_PROCFS
670 int fd = tcp->pfd_as;
674 lseek(fd, addr, SEEK_SET);
675 if (read(fd, laddr, len) == -1)
677 #endif /* USE_PROCFS */
683 * like `umove' but make the additional effort of looking
684 * for a terminating zero byte.
687 umovestr(tcp, addr, len, laddr)
694 #ifdef HAVE_MP_PROCFS
695 int fd = tcp->pfd_as;
699 /* Some systems (e.g. FreeBSD) can be upset if we read off the
700 end of valid memory, avoid this by trying to read up
701 to page boundaries. But we don't know what a page is (and
702 getpagesize(2) (if it exists) doesn't necessarily return
703 hardware page size). Assume all pages >= 1024 (a-historical
706 int page = 1024; /* How to find this? */
707 int move = page - (addr & (page - 1));
710 lseek(fd, addr, SEEK_SET);
713 if (move > left) move = left;
714 if ((move = read(fd, laddr, move)) <= 0)
715 return left != len ? 0 : -1;
716 if (memchr (laddr, 0, move)) break;
722 #else /* !USE_PROCFS */
728 char x[sizeof(long)];
731 if (addr & (sizeof(long) - 1)) {
732 /* addr not a multiple of sizeof(long) */
733 n = addr - (addr & -sizeof(long)); /* residue */
734 addr &= -sizeof(long); /* residue */
736 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
738 if (started && (errno==EPERM || errno==EIO)) {
739 /* Ran into 'end of memory' - stupid "printpath" */
746 memcpy(laddr, &u.x[n], m = MIN(sizeof(long)-n,len));
747 while (n & (sizeof(long) - 1))
748 if (u.x[n++] == '\0')
750 addr += sizeof(long), laddr += m, len -= m;
754 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
756 if (started && (errno==EPERM || errno==EIO)) {
757 /* Ran into 'end of memory' - stupid "printpath" */
764 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
765 for (i = 0; i < sizeof(long); i++)
769 addr += sizeof(long), laddr += m, len -= m;
771 #endif /* !USE_PROCFS */
777 #define PTRACE_WRITETEXT 101
778 #define PTRACE_WRITEDATA 102
785 uload(cmd, pid, addr, len, laddr)
796 n = MIN(len, PAGSIZ);
797 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
798 if (ptrace(cmd, pid, (char *)addr, n, laddr) < 0) {
799 perror("uload: ptrace(PTRACE_WRITE, ...)");
811 char x[sizeof(long)];
814 if (cmd == PTRACE_WRITETEXT) {
815 peek = PTRACE_PEEKTEXT;
816 poke = PTRACE_POKETEXT;
819 peek = PTRACE_PEEKDATA;
820 poke = PTRACE_POKEDATA;
822 if (addr & (sizeof(long) - 1)) {
823 /* addr not a multiple of sizeof(long) */
824 n = addr - (addr & -sizeof(long)); /* residue */
825 addr &= -sizeof(long);
827 u.val = ptrace(peek, pid, (char *) addr, 0);
829 perror("uload: POKE");
832 memcpy(&u.x[n], laddr, m = MIN(sizeof(long) - n, len));
833 if (ptrace(poke, pid, (char *)addr, u.val) < 0) {
834 perror("uload: POKE");
837 addr += sizeof(long), laddr += m, len -= m;
840 if (len < sizeof(long))
841 u.val = ptrace(peek, pid, (char *) addr, 0);
842 memcpy(u.x, laddr, m = MIN(sizeof(long), len));
843 if (ptrace(poke, pid, (char *) addr, u.val) < 0) {
844 perror("uload: POKE");
847 addr += sizeof(long), laddr += m, len -= m;
854 tload(pid, addr, len, laddr)
859 return uload(PTRACE_WRITETEXT, pid, addr, len, laddr);
863 dload(pid, addr, len, laddr)
869 return uload(PTRACE_WRITEDATA, pid, addr, len, laddr);
884 #ifdef SUNOS4_KERNEL_ARCH_KLUDGE
886 static int is_sun4m = -1;
889 /* Round up the usual suspects. */
890 if (is_sun4m == -1) {
891 if (uname(&name) < 0) {
892 perror("upeek: uname?");
895 is_sun4m = strcmp(name.machine, "sun4m") == 0;
897 extern struct xlat struct_user_offsets[];
900 for (x = struct_user_offsets; x->str; x++)
907 #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
909 val = ptrace(PTRACE_PEEKUSER, pid, (char *) off, 0);
910 if (val == -1 && errno) {
911 perror("upeek: ptrace(PTRACE_PEEKUSER, ... )");
918 #endif /* !USE_PROCFS */
928 if (upeek(tcp->pid, 4*EIP, &pc) < 0)
931 if (upeek(tcp->pid, PT_B0, &pc) < 0)
934 if (upeek(tcp->pid, 4*15, &pc) < 0)
936 #elif defined(POWERPC)
937 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0)
940 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0)
943 if (upeek(tcp->pid, REG_PC, &pc) < 0)
946 if (upeek(tcp->pid, REG_EPC, &pc) < 0)
950 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
954 if(upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
956 if(upeek(tcp->pid,PT_IAOQ0,&pc) < 0)
964 * Return current program counter for `pid'
965 * Assumes PC is never 0xffffffff
969 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
970 perror("getpc: ptrace(PTRACE_GETREGS, ...)");
983 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
997 if (upeek(tcp->pid, 4*EIP, &eip) < 0) {
998 tprintf("[????????] ");
1001 tprintf("[%08lx] ", eip);
1005 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
1006 tprintf("[????????] ");
1009 tprintf("[%08lx] ", ip);
1010 #elif defined(POWERPC)
1013 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0) {
1014 tprintf ("[????????] ");
1017 tprintf("[%08lx] ", pc);
1021 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
1022 tprintf ("[????????] ");
1025 tprintf("[%08lx] ", pc);
1026 #elif defined(ALPHA)
1029 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
1030 tprintf ("[????????] ");
1033 tprintf("[%08lx] ", pc);
1034 #elif defined(SPARC)
1036 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) {
1037 tprintf("[????????] ");
1040 tprintf("[%08lx] ", regs.r_pc);
1044 if(upeek(tcp->pid,PT_IAOQ0,&pc) < 0) {
1045 tprintf ("[????????] ");
1048 tprintf("[%08lx] ", pc);
1052 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
1053 tprintf ("[????????] ");
1056 tprintf("[%08lx] ", pc);
1057 #endif /* !architecture */
1063 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
1064 perror("printcall: ptrace(PTRACE_GETREGS, ...)");
1065 tprintf("[????????] ");
1068 tprintf("[%08x] ", regs.r_o7);
1073 tprintf("[????????] ");
1078 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
1079 tprintf("[%08x] ", regs.r_eip);
1080 #endif /* FREEBSD */
1092 /* We simply use the SunOS breakpoint code. */
1095 #define LOOPA 0x30800000 /* ba,a 0 */
1097 if (tcp->flags & TCB_BPTSET) {
1098 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1101 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1102 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1105 tcp->baddr = regs.r_o7 + 8;
1107 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)tcp->baddr, 0);
1109 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1114 * XXX - BRUTAL MODE ON
1115 * We cannot set a real BPT in the child, since it will not be
1116 * traced at the moment it will reach the trap and would probably
1117 * die with a core dump.
1118 * Thus, we are force our way in by taking out two instructions
1119 * and insert an eternal loop instead, in expectance of the SIGSTOP
1120 * generated by out PTRACE_ATTACH.
1121 * Of cause, if we evaporate ourselves in the middle of all this...
1124 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOPA);
1126 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1129 tcp->flags |= TCB_BPTSET;
1134 # define LOOP 0x0000feeb
1135 if (tcp->flags & TCB_BPTSET) {
1136 fprintf(stderr, "PANIC: bpt already set in pid %u\n",
1140 if (upeek(tcp->pid, PT_CR_IIP, &tcp->baddr) < 0)
1143 fprintf(stderr, "[%d] setting bpt at %lx\n",
1144 tcp->pid, tcp->baddr);
1145 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid,
1146 (char *) tcp->baddr, 0);
1148 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1151 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP);
1153 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1156 tcp->flags |= TCB_BPTSET;
1159 * XXX Use break instead!
1161 * Our strategy here is to replace the bundle that
1162 * contained the clone() syscall with a bundle of the
1165 * { 1: br 1b; br 1b; br 1b }
1167 * This ensures that the newly forked child will loop
1168 * endlessly until we've got a chance to attach to it.
1170 # define LOOP0 0x0000100000000017
1171 # define LOOP1 0x4000000000200000
1172 unsigned long addr, ipsr;
1176 if (upeek(pid, PT_CR_IPSR, &ipsr) < 0)
1178 if (upeek(pid, PT_CR_IIP, &addr) < 0)
1180 /* store "ri" in low two bits */
1181 tcp->baddr = addr | ((ipsr >> 41) & 0x3);
1184 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 0,
1186 tcp->inst[1] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 8,
1189 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1194 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, LOOP0);
1195 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, LOOP1);
1197 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1200 tcp->flags |= TCB_BPTSET;
1205 #define LOOP 0x0000feeb
1206 #elif defined (M68K)
1207 #define LOOP 0x60fe0000
1208 #elif defined (ALPHA)
1209 #define LOOP 0xc3ffffff
1210 #elif defined (POWERPC)
1211 #define LOOP 0x0000feeb
1213 #define LOOP 0xEAFFFFFE
1215 #define LOOP 0x1000ffff
1217 #define LOOP 0xa7f40000 /* BRC 15,0 */
1219 #define LOOP 0xe81f1ff7 /* b,l,n <loc>,r0 */
1221 #error unknown architecture
1224 if (tcp->flags & TCB_BPTSET) {
1225 fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid);
1229 if (upeek(tcp->pid, 4*EIP, &tcp->baddr) < 0)
1231 #elif defined (M68K)
1232 if (upeek(tcp->pid, 4*PT_PC, &tcp->baddr) < 0)
1234 #elif defined (ALPHA)
1238 #elif defined (MIPS)
1239 return -1; /* FIXME: I do not know what i do - Flo */
1240 #elif defined (POWERPC)
1241 if (upeek(tcp->pid, 4*PT_NIP, &tcp->baddr) < 0)
1244 if (upeek(tcp->pid,PT_PSWADDR, &tcp->baddr) < 0)
1247 if (upeek(tcp->pid, PT_IAOQ0, &tcp->baddr) < 0)
1249 tcp->baddr &= ~0x03;
1251 #error unknown architecture
1254 fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr);
1255 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *) tcp->baddr, 0);
1257 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1260 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP);
1262 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1265 tcp->flags |= TCB_BPTSET;
1272 #ifdef SPARC /* This code is slightly sparc specific */
1275 #define BPT 0x91d02001 /* ta 1 */
1276 #define LOOP 0x10800000 /* ba 0 */
1277 #define LOOPA 0x30800000 /* ba,a 0 */
1278 #define NOP 0x01000000
1280 static int loopdeloop[1] = {LOOPA};
1282 static int loopdeloop[2] = {LOOP, NOP};
1285 if (tcp->flags & TCB_BPTSET) {
1286 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1289 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1290 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1293 tcp->baddr = regs.r_o7 + 8;
1294 if (ptrace(PTRACE_READTEXT, tcp->pid, (char *)tcp->baddr,
1295 sizeof tcp->inst, (char *)tcp->inst) < 0) {
1296 perror("setbpt: ptrace(PTRACE_READTEXT, ...)");
1301 * XXX - BRUTAL MODE ON
1302 * We cannot set a real BPT in the child, since it will not be
1303 * traced at the moment it will reach the trap and would probably
1304 * die with a core dump.
1305 * Thus, we are force our way in by taking out two instructions
1306 * and insert an eternal loop in stead, in expectance of the SIGSTOP
1307 * generated by out PTRACE_ATTACH.
1308 * Of cause, if we evaporate ourselves in the middle of all this...
1310 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1311 sizeof loopdeloop, (char *) loopdeloop) < 0) {
1312 perror("setbpt: ptrace(PTRACE_WRITETEXT, ...)");
1315 tcp->flags |= TCB_BPTSET;
1331 #elif defined(POWERPC)
1335 #elif defined(ALPHA)
1339 #endif /* architecture */
1342 /* Again, we borrow the SunOS breakpoint code. */
1343 if (!(tcp->flags & TCB_BPTSET)) {
1344 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1348 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1350 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1353 tcp->flags &= ~TCB_BPTSET;
1359 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
1360 if (!(tcp->flags & TCB_BPTSET)) {
1361 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1365 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1367 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1370 tcp->flags &= ~TCB_BPTSET;
1372 if (upeek(tcp->pid, PT_CR_IIP, &addr) < 0)
1374 if (addr != tcp->baddr) {
1375 /* The breakpoint has not been reached yet. */
1378 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1383 unsigned long addr, ipsr;
1388 if (upeek(pid, PT_CR_IPSR, &ipsr) < 0)
1390 if (upeek(pid, PT_CR_IIP, &addr) < 0)
1393 /* restore original bundle: */
1395 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, tcp->inst[0]);
1396 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, tcp->inst[1]);
1398 perror("clearbpt: ptrace(PTRACE_POKETEXT, ...)");
1402 /* restore original "ri" in ipsr: */
1403 ipsr = (ipsr & ~(0x3ul << 41)) | ((tcp->baddr & 0x3) << 41);
1405 ptrace(PTRACE_POKEUSER, pid, (char *) PT_CR_IPSR, ipsr);
1407 perror("clrbpt: ptrace(PTRACE_POKEUSER, ...)");
1411 tcp->flags &= ~TCB_BPTSET;
1413 if (addr != (tcp->baddr & ~0x3)) {
1414 /* the breakpoint has not been reached yet. */
1416 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1421 #else /* !IA64 && ! SPARC */
1424 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
1425 if (!(tcp->flags & TCB_BPTSET)) {
1426 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1430 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1432 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1435 tcp->flags &= ~TCB_BPTSET;
1438 if (upeek(tcp->pid, 4*EIP, &eip) < 0)
1440 if (eip != tcp->baddr) {
1441 /* The breakpoint has not been reached yet. */
1444 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1448 #elif defined(POWERPC)
1449 if (upeek(tcp->pid, 4*PT_NIP, &pc) < 0)
1451 if (pc != tcp->baddr) {
1452 /* The breakpoint has not been reached yet. */
1454 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1459 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0)
1461 if (pc != tcp->baddr) {
1462 /* The breakpoint has not been reached yet. */
1464 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1468 #elif defined(ALPHA)
1469 if (upeek(tcp->pid, REG_PC, &pc) < 0)
1471 if (pc != tcp->baddr) {
1472 /* The breakpoint has not been reached yet. */
1474 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
1479 if (upeek(tcp->pid, PT_IAOQ0, &iaoq) < 0)
1482 if (iaoq != tcp->baddr && iaoq != tcp->baddr + 4) {
1483 /* The breakpoint has not been reached yet. */
1485 fprintf(stderr, "NOTE: PC not at bpt (iaoq %#lx baddr %#lx)\n",
1489 iaoq = tcp->baddr | 3;
1490 /* We should be pointing at a 'ldi -1000,r1' in glibc, so it is
1491 * safe to set both IAOQ0 and IAOQ1 to that so the PSW N bit
1492 * has no significant effect.
1494 ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ0, iaoq);
1495 ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ1, iaoq);
1497 #endif /* !SPARC && !IA64 */
1507 if (!(tcp->flags & TCB_BPTSET)) {
1508 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1511 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1512 sizeof tcp->inst, (char *) tcp->inst) < 0) {
1513 perror("clearbtp: ptrace(PTRACE_WRITETEXT, ...)");
1516 tcp->flags &= ~TCB_BPTSET;
1520 * Since we don't have a single instruction breakpoint, we may have
1521 * to adjust the program counter after removing the our `breakpoint'.
1523 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1524 perror("clearbpt: ptrace(PTRACE_GETREGS, ...)");
1527 if ((regs.r_pc < tcp->baddr) ||
1528 (regs.r_pc > tcp->baddr + 4)) {
1529 /* The breakpoint has not been reached yet */
1532 "NOTE: PC not at bpt (pc %#x baddr %#x)\n",
1533 regs.r_pc, tcp->parent->baddr);
1536 if (regs.r_pc != tcp->baddr)
1538 fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n",
1539 regs.r_pc, tcp->baddr);
1541 regs.r_pc = tcp->baddr;
1542 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) {
1543 perror("clearbpt: ptrace(PTRACE_SETREGS, ...)");
1553 #endif /* !USE_PROCFS */
1564 for (n = 0; n < sizeof *hdr; n += 4) {
1566 if (upeek(pid, uoff(u_exdata) + n, &res) < 0)
1568 memcpy(((char *) hdr) + n, &res, 4);
1571 fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n",
1572 hdr->a_magic, hdr->a_toolversion, hdr->a_machtype);
1573 fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n",
1574 hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry);
1585 * Change `vfork' in a freshly exec'ed dynamically linked
1586 * executable's (internal) symbol table to plain old `fork'
1590 struct link_dynamic dyn;
1591 struct link_dynamic_2 ld;
1594 if (getex(pid, &hdr) < 0)
1599 if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) {
1600 fprintf(stderr, "Cannot read DYNAMIC\n");
1603 if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) {
1604 fprintf(stderr, "Cannot read link_dynamic_2\n");
1607 if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) {
1608 fprintf(stderr, "fixvfork: out of memory\n");
1611 if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
1612 (int)ld.ld_symb_size, strtab) < 0)
1616 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
1617 fprintf(stderr, "[symbol: %s]\n", cp);
1622 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
1623 if (strcmp(cp, "_vfork") == 0) {
1625 fprintf(stderr, "fixvfork: FOUND _vfork\n");
1626 strcpy(cp, "_fork");
1631 if (cp < strtab + ld.ld_symb_size)
1633 * Write entire symbol table back to avoid
1634 * memory alignment bugs in ptrace
1636 if (tload(pid, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
1637 (int)ld.ld_symb_size, strtab) < 0)