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/syscall.h>
41 #include <sys/param.h>
47 #include <machine/reg.h>
52 #if defined(linux) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 1))
53 #include <linux/ptrace.h>
56 #if defined(LINUX) && defined(IA64)
57 # include <asm/ptrace_offsets.h>
63 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
64 #elif defined(HAVE_LINUX_PTRACE_H)
66 # ifdef HAVE_STRUCT_IA64_FPREG
67 # define ia64_fpreg XXX_ia64_fpreg
69 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
70 # define pt_all_user_regs XXX_pt_all_user_regs
72 #include <linux/ptrace.h>
74 # undef pt_all_user_regs
77 #ifdef SUNOS4_KERNEL_ARCH_KLUDGE
78 #include <sys/utsname.h>
79 #endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
81 #if defined(LINUXSPARC)
83 # define fpq kernel_fpq
85 # define fpu kernel_fpu
93 # undef PTRACE_GETREGS
94 # define PTRACE_GETREGS PTRACE_GETREGS64
95 # undef PTRACE_SETREGS
96 # define PTRACE_SETREGS PTRACE_SETREGS64
99 #if !defined(__GLIBC__)
101 #include <linux/unistd.h>
103 #define _hack_syscall5(type,name,type1,arg1,type2,arg2,type3,arg3,type4,arg4,\
104 type5,arg5,syscall) \
105 type name (type1 arg1,type2 arg2,type3 arg3,type4 arg4,type5 arg5) \
109 __asm__ volatile ("or %%g0, %1, %%o0\n\t" \
110 "or %%g0, %2, %%o1\n\t" \
111 "or %%g0, %3, %%o2\n\t" \
112 "or %%g0, %4, %%o3\n\t" \
113 "or %%g0, %5, %%o4\n\t" \
114 "or %%g0, %6, %%g1\n\t" \
115 #if defined (SPARC64)
121 "or %%g0, %%o0, %0\n\t" \
122 "sub %%g0, %%o0, %0\n\t" \
125 : "0" ((long)(arg1)),"1" ((long)(arg2)), \
126 "2" ((long)(arg3)),"3" ((long)(arg4)),"4" ((long)(arg5)), \
127 "i" (__NR_##syscall) \
128 : "g1", "o0", "o1", "o2", "o3", "o4"); \
130 return (type) __res; \
135 static _hack_syscall5(int,_ptrace,int,__request,int,__pid,int,__addr,int,__data,int,__addr2,ptrace)
145 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
148 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
167 return a->tv_sec || a->tv_usec;
172 struct timeval *a, *b;
174 if (a->tv_sec < b->tv_sec
175 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
177 if (a->tv_sec > b->tv_sec
178 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
187 return tv->tv_sec + tv->tv_usec/1000000.0;
192 struct timeval *tv, *a, *b;
194 tv->tv_sec = a->tv_sec + b->tv_sec;
195 tv->tv_usec = a->tv_usec + b->tv_usec;
196 if (tv->tv_usec >= 1000000) {
198 tv->tv_usec -= 1000000;
204 struct timeval *tv, *a, *b;
206 tv->tv_sec = a->tv_sec - b->tv_sec;
207 tv->tv_usec = a->tv_usec - b->tv_usec;
208 if (((long) tv->tv_usec) < 0) {
210 tv->tv_usec += 1000000;
216 struct timeval *tv, *a;
219 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
220 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
221 tv->tv_usec %= 1000000;
226 struct timeval *tv, *a;
229 tv->tv_usec = a->tv_usec * n;
230 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
231 tv->tv_usec %= 1000000;
235 xlookup(const struct xlat *xlat, int val)
237 for (; xlat->str != NULL; xlat++)
238 if (xlat->val == val)
244 * Generic ptrace wrapper which tracks ESRCH errors
245 * by setting tcp->ptrace_errno to ESRCH.
247 * We assume that ESRCH indicates likely process death (SIGKILL?),
248 * modulo bugs where process somehow ended up not stopped.
249 * Unfortunately kernel uses ESRCH for that case too. Oh well.
251 * Currently used by upeek() only.
252 * TODO: use this in all other ptrace() calls while decoding.
255 do_ptrace(int request, struct tcb *tcp, void *addr, void *data)
260 l = ptrace(request, tcp->pid, addr, data);
261 /* Non-ESRCH errors might be our invalid reg/mem accesses,
262 * we do not record them. */
264 tcp->ptrace_errno = ESRCH;
269 * Used when we want to unblock stopped traced process.
270 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
271 * Returns 0 on success or if error was ESRCH
272 * (presumably process was killed while we talk to it).
273 * Otherwise prints error message and returns -1.
276 ptrace_restart(int op, struct tcb *tcp, int sig)
282 ptrace(op, tcp->pid, (void *) 1, (void *) (long) sig);
284 if (!err || err == ESRCH)
287 tcp->ptrace_errno = err;
289 if (op == PTRACE_CONT)
291 if (op == PTRACE_DETACH)
293 fprintf(stderr, "strace: ptrace(PTRACE_%s,1,%d): %s\n",
294 msg, sig, strerror(err));
299 * Print entry in struct xlat table, if there.
302 printxval(const struct xlat *xlat, int val, const char *dflt)
304 const char *str = xlookup(xlat, val);
309 tprintf("%#x /* %s */", val, dflt);
313 * Interpret `xlat' as an array of flags
314 * print the entries whose bits are on in `flags'
315 * return # of flags printed.
318 addflags(xlat, flags)
319 const struct xlat *xlat;
324 for (n = 0; xlat->str; xlat++) {
325 if (xlat->val && (flags & xlat->val) == xlat->val) {
326 tprintf("|%s", xlat->str);
332 tprintf("|%#x", flags);
339 * Interpret `xlat' as an array of flags/
340 * Print to static string the entries whose bits are on in `flags'
341 * Return static string.
344 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
346 static char outstr[1024];
349 strcpy(outstr, prefix);
351 for (; xlat->str; xlat++) {
352 if ((flags & xlat->val) == xlat->val) {
355 strcat(outstr, xlat->str);
363 sprintf(outstr + strlen(outstr), "%#x", flags);
370 printflags(xlat, flags, dflt)
371 const struct xlat *xlat;
378 if (flags == 0 && xlat->val == 0) {
379 tprintf("%s", xlat->str);
384 for (n = 0; xlat->str; xlat++) {
385 if (xlat->val && (flags & xlat->val) == xlat->val) {
386 tprintf("%s%s", sep, xlat->str);
395 tprintf("%s%#x", sep, flags);
400 tprintf("%#x", flags);
402 tprintf(" /* %s */", dflt);
413 printnum(tcp, addr, fmt)
424 if (umove(tcp, addr, &num) < 0) {
425 tprintf("%#lx", addr);
434 printnum_int(tcp, addr, fmt)
445 if (umove(tcp, addr, &num) < 0) {
446 tprintf("%#lx", addr);
460 tprintf((uid == -1) ? "%ld" : "%lu", uid);
463 static char path[MAXPATHLEN + 1];
466 * Quote string `instr' of length `size'
467 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
468 * If `len' < 0, treat `instr' as a NUL-terminated string
469 * and quote at most (`size' - 1) bytes.
472 string_quote(const char *instr, char *outstr, int len, int size)
474 const unsigned char *ustr = (const unsigned char *) instr;
476 int usehex = 0, c, i;
481 /* Check for presence of symbol which require
482 to hex-quote the whole string. */
483 for (i = 0; i < size; ++i) {
485 /* Check for NUL-terminated string. */
489 /* Quote at most size - 1 bytes. */
493 if (!isprint(c) && !isspace(c)) {
503 /* Hex-quote the whole string. */
504 for (i = 0; i < size; ++i) {
506 /* Check for NUL-terminated string. */
510 /* Quote at most size - 1 bytes. */
514 sprintf(s, "\\x%02x", c);
518 for (i = 0; i < size; ++i) {
520 /* Check for NUL-terminated string. */
524 /* Quote at most size - 1 bytes. */
529 case '\"': case '\\':
556 else if (i + 1 < size
557 && isdigit(ustr[i + 1])) {
558 sprintf(s, "\\%03o", c);
561 sprintf(s, "\\%o", c);
572 /* Return nonzero if the string was unterminated. */
577 * Print path string specified by address `addr' and length `n'.
578 * If path length exceeds `n', append `...' to the output.
581 printpathn(struct tcb *tcp, long addr, int n)
588 /* Cap path length to the path buffer size,
589 and NUL-terminate the buffer. */
590 if (n > sizeof path - 1)
594 /* Fetch one byte more to find out whether path length > n. */
595 if (umovestr(tcp, addr, n + 1, path) < 0)
596 tprintf("%#lx", addr);
598 static char outstr[4*(sizeof path - 1) + sizeof "\"...\""];
599 int trunc = (path[n] != '\0');
603 (void) string_quote(path, outstr, -1, n + 1);
605 strcat(outstr, "...");
606 tprintf("%s", outstr);
611 printpath(struct tcb *tcp, long addr)
613 printpathn(tcp, addr, sizeof path - 1);
617 * Print string specified by address `addr' and length `len'.
618 * If `len' < 0, treat the string as a NUL-terminated string.
619 * If string length exceeds `max_strlen', append `...' to the output.
622 printstr(struct tcb *tcp, long addr, int len)
624 static char *str = NULL;
632 /* Allocate static buffers if they are not allocated yet. */
634 str = malloc(max_strlen + 1);
636 outstr = malloc(4 * max_strlen + sizeof "\"...\"");
637 if (!str || !outstr) {
638 fprintf(stderr, "out of memory\n");
639 tprintf("%#lx", addr);
645 * Treat as a NUL-terminated string: fetch one byte more
646 * because string_quote() quotes one byte less.
648 size = max_strlen + 1;
649 str[max_strlen] = '\0';
650 if (umovestr(tcp, addr, size, str) < 0) {
651 tprintf("%#lx", addr);
656 size = MIN(len, max_strlen);
657 if (umoven(tcp, addr, size, str) < 0) {
658 tprintf("%#lx", addr);
663 if (string_quote(str, outstr, len, size) &&
664 (len < 0 || len > max_strlen))
665 strcat(outstr, "...");
667 tprintf("%s", outstr);
672 dumpiov(tcp, len, addr)
677 #if defined(LINUX) && SUPPORTED_PERSONALITIES > 1
679 struct { u_int32_t base; u_int32_t len; } *iov32;
680 struct { u_int64_t base; u_int64_t len; } *iov64;
682 #define iov iovu.iov64
684 (personality_wordsize[current_personality] == 4 \
685 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
686 #define iov_iov_base(i) \
687 (personality_wordsize[current_personality] == 4 \
688 ? (u_int64_t) iovu.iov32[i].base : iovu.iov64[i].base)
689 #define iov_iov_len(i) \
690 (personality_wordsize[current_personality] == 4 \
691 ? (u_int64_t) iovu.iov32[i].len : iovu.iov64[i].len)
694 #define sizeof_iov sizeof(*iov)
695 #define iov_iov_base(i) iov[i].iov_base
696 #define iov_iov_len(i) iov[i].iov_len
701 size = sizeof_iov * (unsigned long) len;
702 if (size / sizeof_iov != len
703 || (iov = malloc(size)) == NULL) {
704 fprintf(stderr, "out of memory\n");
707 if (umoven(tcp, addr, size, (char *) iov) >= 0) {
708 for (i = 0; i < len; i++) {
709 /* include the buffer number to make it easy to
710 * match up the trace with the source */
711 tprintf(" * %lu bytes in buffer %d\n",
712 (unsigned long)iov_iov_len(i), i);
713 dumpstr(tcp, (long) iov_iov_base(i),
726 dumpstr(tcp, addr, len)
731 static int strsize = -1;
732 static unsigned char *str;
733 static char outstr[80];
740 if ((str = malloc(len)) == NULL) {
741 fprintf(stderr, "out of memory\n");
747 if (umoven(tcp, addr, len, (char *) str) < 0)
750 for (i = 0; i < len; i += 16) {
752 sprintf(s, " | %05x ", i);
754 for (j = 0; j < 16; j++) {
758 sprintf(s, " %02x", str[i + j]);
762 *s++ = ' '; *s++ = ' '; *s++ = ' ';
765 *s++ = ' '; *s++ = ' ';
766 for (j = 0; j < 16; j++) {
770 if (isprint(str[i + j]))
778 tprintf("%s |\n", outstr);
782 #define PAGMASK (~(PAGSIZ - 1))
784 * move `len' bytes of data from process `pid'
785 * at address `addr' to our space at `laddr'
788 umoven(struct tcb *tcp, long addr, int len, char *laddr)
796 char x[sizeof(long)];
799 if (addr & (sizeof(long) - 1)) {
800 /* addr not a multiple of sizeof(long) */
801 n = addr - (addr & -sizeof(long)); /* residue */
802 addr &= -sizeof(long); /* residue */
804 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
806 if (started && (errno==EPERM || errno==EIO)) {
807 /* Ran into 'end of memory' - stupid "printpath" */
810 /* But if not started, we had a bogus address. */
811 if (addr != 0 && errno != EIO && errno != ESRCH)
812 perror("ptrace: umoven");
816 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
817 addr += sizeof(long), laddr += m, len -= m;
821 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
823 if (started && (errno==EPERM || errno==EIO)) {
824 /* Ran into 'end of memory' - stupid "printpath" */
827 if (addr != 0 && errno != EIO && errno != ESRCH)
828 perror("ptrace: umoven");
832 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
833 addr += sizeof(long), laddr += m, len -= m;
843 char x[sizeof(long)];
846 if (addr & (sizeof(long) - 1)) {
847 /* addr not a multiple of sizeof(long) */
848 n = addr - (addr & -sizeof(long)); /* residue */
849 addr &= -sizeof(long); /* residue */
851 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
857 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
858 addr += sizeof(long), laddr += m, len -= m;
862 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
868 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
869 addr += sizeof(long), laddr += m, len -= m;
875 n = MIN(len, PAGSIZ);
876 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
877 if (ptrace(PTRACE_READDATA, pid,
878 (char *) addr, len, laddr) < 0) {
879 if (errno != ESRCH) {
880 perror("umoven: ptrace(PTRACE_READDATA, ...)");
893 #ifdef HAVE_MP_PROCFS
894 int fd = tcp->pfd_as;
898 lseek(fd, addr, SEEK_SET);
899 if (read(fd, laddr, len) == -1)
901 #endif /* USE_PROCFS */
907 * like `umove' but make the additional effort of looking
908 * for a terminating zero byte.
911 umovestr(struct tcb *tcp, long addr, int len, char *laddr)
914 #ifdef HAVE_MP_PROCFS
915 int fd = tcp->pfd_as;
919 /* Some systems (e.g. FreeBSD) can be upset if we read off the
920 end of valid memory, avoid this by trying to read up
921 to page boundaries. But we don't know what a page is (and
922 getpagesize(2) (if it exists) doesn't necessarily return
923 hardware page size). Assume all pages >= 1024 (a-historical
926 int page = 1024; /* How to find this? */
927 int move = page - (addr & (page - 1));
930 lseek(fd, addr, SEEK_SET);
933 if (move > left) move = left;
934 if ((move = read(fd, laddr, move)) <= 0)
935 return left != len ? 0 : -1;
936 if (memchr (laddr, 0, move)) break;
942 #else /* !USE_PROCFS */
948 char x[sizeof(long)];
951 if (addr & (sizeof(long) - 1)) {
952 /* addr not a multiple of sizeof(long) */
953 n = addr - (addr & -sizeof(long)); /* residue */
954 addr &= -sizeof(long); /* residue */
956 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
958 if (started && (errno==EPERM || errno==EIO)) {
959 /* Ran into 'end of memory' - stupid "printpath" */
962 if (addr != 0 && errno != EIO && errno != ESRCH)
967 memcpy(laddr, &u.x[n], m = MIN(sizeof(long)-n,len));
968 while (n & (sizeof(long) - 1))
969 if (u.x[n++] == '\0')
971 addr += sizeof(long), laddr += m, len -= m;
975 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
977 if (started && (errno==EPERM || errno==EIO)) {
978 /* Ran into 'end of memory' - stupid "printpath" */
981 if (addr != 0 && errno != EIO && errno != ESRCH)
986 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
987 for (i = 0; i < sizeof(long); i++)
991 addr += sizeof(long), laddr += m, len -= m;
993 #endif /* !USE_PROCFS */
998 # if !defined (SPARC) && !defined(SPARC64)
999 # define PTRACE_WRITETEXT 101
1000 # define PTRACE_WRITEDATA 102
1001 # endif /* !SPARC && !SPARC64 */
1007 uload(cmd, pid, addr, len, laddr)
1018 n = MIN(len, PAGSIZ);
1019 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
1020 if (ptrace(cmd, pid, (char *)addr, n, laddr) < 0) {
1021 perror("uload: ptrace(PTRACE_WRITE, ...)");
1033 char x[sizeof(long)];
1036 if (cmd == PTRACE_WRITETEXT) {
1037 peek = PTRACE_PEEKTEXT;
1038 poke = PTRACE_POKETEXT;
1041 peek = PTRACE_PEEKDATA;
1042 poke = PTRACE_POKEDATA;
1044 if (addr & (sizeof(long) - 1)) {
1045 /* addr not a multiple of sizeof(long) */
1046 n = addr - (addr & -sizeof(long)); /* residue */
1047 addr &= -sizeof(long);
1049 u.val = ptrace(peek, pid, (char *) addr, 0);
1051 perror("uload: POKE");
1054 memcpy(&u.x[n], laddr, m = MIN(sizeof(long) - n, len));
1055 if (ptrace(poke, pid, (char *)addr, u.val) < 0) {
1056 perror("uload: POKE");
1059 addr += sizeof(long), laddr += m, len -= m;
1062 if (len < sizeof(long))
1063 u.val = ptrace(peek, pid, (char *) addr, 0);
1064 memcpy(u.x, laddr, m = MIN(sizeof(long), len));
1065 if (ptrace(poke, pid, (char *) addr, u.val) < 0) {
1066 perror("uload: POKE");
1069 addr += sizeof(long), laddr += m, len -= m;
1076 tload(pid, addr, len, laddr)
1081 return uload(PTRACE_WRITETEXT, pid, addr, len, laddr);
1085 dload(pid, addr, len, laddr)
1091 return uload(PTRACE_WRITEDATA, pid, addr, len, laddr);
1099 upeek(tcp, off, res)
1106 # ifdef SUNOS4_KERNEL_ARCH_KLUDGE
1108 static int is_sun4m = -1;
1109 struct utsname name;
1111 /* Round up the usual suspects. */
1112 if (is_sun4m == -1) {
1113 if (uname(&name) < 0) {
1114 perror("upeek: uname?");
1117 is_sun4m = strcmp(name.machine, "sun4m") == 0;
1119 const struct xlat *x;
1121 for (x = struct_user_offsets; x->str; x++)
1128 # endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
1130 val = do_ptrace(PTRACE_PEEKUSER, tcp, (char *) off, 0);
1131 if (val == -1 && errno) {
1132 if (errno != ESRCH) {
1134 sprintf(buf,"upeek: ptrace(PTRACE_PEEKUSER,%d,%lu,0)", tcp->pid, off);
1143 #endif /* !USE_PROCFS */
1147 getpc(struct tcb *tcp)
1153 if (upeek(tcp, 4*EIP, &pc) < 0)
1155 # elif defined(X86_64)
1156 if (upeek(tcp, 8*RIP, &pc) < 0)
1158 # elif defined(IA64)
1159 if (upeek(tcp, PT_B0, &pc) < 0)
1162 if (upeek(tcp, 4*15, &pc) < 0)
1164 # elif defined(AVR32)
1165 if (upeek(tcp, REG_PC, &pc) < 0)
1167 # elif defined(BFIN)
1168 if (upeek(tcp, REG_PC, &pc) < 0)
1170 # elif defined(POWERPC)
1171 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0)
1173 # elif defined(M68K)
1174 if (upeek(tcp, 4*PT_PC, &pc) < 0)
1176 # elif defined(ALPHA)
1177 if (upeek(tcp, REG_PC, &pc) < 0)
1179 # elif defined(MIPS)
1180 if (upeek(tcp, REG_EPC, &pc) < 0)
1182 # elif defined(SPARC) || defined(SPARC64)
1184 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1187 # elif defined(S390) || defined(S390X)
1188 if(upeek(tcp,PT_PSWADDR,&pc) < 0)
1190 # elif defined(HPPA)
1191 if(upeek(tcp,PT_IAOQ0,&pc) < 0)
1194 if (upeek(tcp, 4*REG_PC ,&pc) < 0)
1196 # elif defined(SH64)
1197 if (upeek(tcp, REG_PC ,&pc) < 0)
1205 * Return current program counter for `pid'
1206 * Assumes PC is never 0xffffffff
1210 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
1211 perror("getpc: ptrace(PTRACE_GETREGS, ...)");
1224 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
1226 #endif /* FREEBSD */
1231 printcall(struct tcb *tcp)
1233 #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
1234 sizeof(long) == 8 ? "[????????????????] " : \
1241 if (upeek(tcp, 4*EIP, &eip) < 0) {
1245 tprintf("[%08lx] ", eip);
1247 # elif defined(S390) || defined(S390X)
1249 if(upeek(tcp,PT_PSWADDR,&psw) < 0) {
1254 tprintf("[%08lx] ", psw);
1256 tprintf("[%16lx] ", psw);
1259 # elif defined(X86_64)
1262 if (upeek(tcp, 8*RIP, &rip) < 0) {
1266 tprintf("[%16lx] ", rip);
1267 # elif defined(IA64)
1270 if (upeek(tcp, PT_B0, &ip) < 0) {
1274 tprintf("[%08lx] ", ip);
1275 # elif defined(POWERPC)
1278 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
1279 tprintf ("[????????] ");
1282 tprintf("[%08lx] ", pc);
1283 # elif defined(M68K)
1286 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
1287 tprintf ("[????????] ");
1290 tprintf("[%08lx] ", pc);
1291 # elif defined(ALPHA)
1294 if (upeek(tcp, REG_PC, &pc) < 0) {
1295 tprintf ("[????????????????] ");
1298 tprintf("[%08lx] ", pc);
1299 # elif defined(SPARC) || defined(SPARC64)
1301 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) {
1305 tprintf("[%08lx] ", regs.r_pc);
1306 # elif defined(HPPA)
1309 if(upeek(tcp,PT_IAOQ0,&pc) < 0) {
1310 tprintf ("[????????] ");
1313 tprintf("[%08lx] ", pc);
1314 # elif defined(MIPS)
1317 if (upeek(tcp, REG_EPC, &pc) < 0) {
1318 tprintf ("[????????] ");
1321 tprintf("[%08lx] ", pc);
1325 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
1326 tprintf ("[????????] ");
1329 tprintf("[%08lx] ", pc);
1330 # elif defined(SH64)
1333 if (upeek(tcp, REG_PC, &pc) < 0) {
1334 tprintf ("[????????????????] ");
1337 tprintf("[%08lx] ", pc);
1341 if (upeek(tcp, 4*15, &pc) < 0) {
1345 tprintf("[%08lx] ", pc);
1346 # elif defined(AVR32)
1349 if (upeek(tcp, REG_PC, &pc) < 0) {
1350 tprintf("[????????] ");
1353 tprintf("[%08lx] ", pc);
1354 # elif defined(BFIN)
1357 if (upeek(tcp, PT_PC, &pc) < 0) {
1361 tprintf("[%08lx] ", pc);
1362 #elif defined(CRISV10)
1365 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
1369 tprintf("[%08lx] ", pc);
1370 #elif defined(CRISV32)
1373 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
1377 tprintf("[%08lx] ", pc);
1378 # endif /* architecture */
1384 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
1385 perror("printcall: ptrace(PTRACE_GETREGS, ...)");
1389 tprintf("[%08x] ", regs.r_o7);
1399 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
1400 tprintf("[%08x] ", regs.r_eip);
1401 #endif /* FREEBSD */
1406 * These #if's are huge, please indent them correctly.
1407 * It's easy to get confused otherwise.
1413 # include "syscall.h"
1415 # include <sys/syscall.h>
1416 # ifndef CLONE_PTRACE
1417 # define CLONE_PTRACE 0x00002000
1419 # ifndef CLONE_VFORK
1420 # define CLONE_VFORK 0x00004000
1423 # define CLONE_VM 0x00000100
1425 # ifndef CLONE_STOPPED
1426 # define CLONE_STOPPED 0x02000000
1431 /* We don't have fork()/vfork() syscalls on ia64 itself, but the ia32
1432 subsystem has them for x86... */
1434 # define SYS_vfork 190
1436 typedef unsigned long *arg_setup_state;
1439 arg_setup(struct tcb *tcp, arg_setup_state *state)
1441 unsigned long cfm, sof, sol;
1445 /* Satisfy a false GCC warning. */
1450 if (upeek(tcp, PT_AR_BSP, &bsp) < 0)
1452 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1455 sof = (cfm >> 0) & 0x7f;
1456 sol = (cfm >> 7) & 0x7f;
1457 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
1459 *state = (unsigned long *) bsp;
1463 # define arg_finish_change(tcp, state) 0
1467 get_arg0 (struct tcb *tcp, arg_setup_state *state, long *valp)
1472 ret = upeek (tcp, PT_R11, valp);
1475 (unsigned long) ia64_rse_skip_regs(*state, 0),
1476 sizeof(long), (void *) valp);
1481 get_arg1 (struct tcb *tcp, arg_setup_state *state, long *valp)
1486 ret = upeek (tcp, PT_R9, valp);
1489 (unsigned long) ia64_rse_skip_regs(*state, 1),
1490 sizeof(long), (void *) valp);
1496 set_arg0 (struct tcb *tcp, arg_setup_state *state, long val)
1498 int req = PTRACE_POKEDATA;
1502 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1503 req = PTRACE_POKEUSER;
1505 ap = ia64_rse_skip_regs(*state, 0);
1507 ptrace(req, tcp->pid, ap, val);
1508 return errno ? -1 : 0;
1512 set_arg1 (struct tcb *tcp, arg_setup_state *state, long val)
1514 int req = PTRACE_POKEDATA;
1518 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1519 req = PTRACE_POKEUSER;
1521 ap = ia64_rse_skip_regs(*state, 1);
1523 ptrace(req, tcp->pid, ap, val);
1524 return errno ? -1 : 0;
1527 /* ia64 does not return the input arguments from functions (and syscalls)
1528 according to ia64 RSE (Register Stack Engine) behavior. */
1530 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1531 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1533 # elif defined (SPARC) || defined (SPARC64)
1535 typedef struct regs arg_setup_state;
1537 # define arg_setup(tcp, state) \
1538 (ptrace (PTRACE_GETREGS, tcp->pid, (char *) (state), 0))
1539 # define arg_finish_change(tcp, state) \
1540 (ptrace (PTRACE_SETREGS, tcp->pid, (char *) (state), 0))
1542 # define get_arg0(tcp, state, valp) (*(valp) = (state)->r_o0, 0)
1543 # define get_arg1(tcp, state, valp) (*(valp) = (state)->r_o1, 0)
1544 # define set_arg0(tcp, state, val) ((state)->r_o0 = (val), 0)
1545 # define set_arg1(tcp, state, val) ((state)->r_o1 = (val), 0)
1546 # define restore_arg0(tcp, state, val) 0
1548 # else /* other architectures */
1550 # if defined S390 || defined S390X
1551 /* Note: this is only true for the `clone' system call, which handles
1552 arguments specially. We could as well say that its first two arguments
1553 are swapped relative to other architectures, but that would just be
1554 another #ifdef in the calls. */
1555 # define arg0_offset PT_GPR3
1556 # define arg1_offset PT_ORIGGPR2
1557 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1558 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1559 # define arg0_index 1
1560 # define arg1_index 0
1561 # elif defined (ALPHA) || defined (MIPS)
1562 # define arg0_offset REG_A0
1563 # define arg1_offset (REG_A0+1)
1564 # elif defined (AVR32)
1565 # define arg0_offset (REG_R12)
1566 # define arg1_offset (REG_R11)
1567 # elif defined (POWERPC)
1568 # define arg0_offset (sizeof(unsigned long)*PT_R3)
1569 # define arg1_offset (sizeof(unsigned long)*PT_R4)
1570 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1571 # elif defined (HPPA)
1572 # define arg0_offset PT_GR26
1573 # define arg1_offset (PT_GR26-4)
1574 # elif defined (X86_64)
1575 # define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1576 # define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1578 # define arg0_offset (4*(REG_REG0+4))
1579 # define arg1_offset (4*(REG_REG0+5))
1580 # elif defined (SH64)
1581 /* ABI defines arg0 & 1 in r2 & r3 */
1582 # define arg0_offset (REG_OFFSET+16)
1583 # define arg1_offset (REG_OFFSET+24)
1584 # define restore_arg0(tcp, state, val) 0
1585 # elif defined CRISV10 || defined CRISV32
1586 # define arg0_offset (4*PT_R11)
1587 # define arg1_offset (4*PT_ORIG_R10)
1588 # define restore_arg0(tcp, state, val) 0
1589 # define restore_arg1(tcp, state, val) 0
1590 # define arg0_index 1
1591 # define arg1_index 0
1593 # define arg0_offset 0
1594 # define arg1_offset 4
1596 # define restore_arg0(tcp, state, val) 0
1600 typedef int arg_setup_state;
1602 # define arg_setup(tcp, state) (0)
1603 # define arg_finish_change(tcp, state) 0
1604 # define get_arg0(tcp, cookie, valp) \
1605 (upeek ((tcp), arg0_offset, (valp)))
1606 # define get_arg1(tcp, cookie, valp) \
1607 (upeek ((tcp), arg1_offset, (valp)))
1610 set_arg0 (struct tcb *tcp, void *cookie, long val)
1612 return ptrace (PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val);
1616 set_arg1 (struct tcb *tcp, void *cookie, long val)
1618 return ptrace (PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val);
1621 # endif /* architectures */
1623 # ifndef restore_arg0
1624 # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1626 # ifndef restore_arg1
1627 # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1631 # define arg0_index 0
1632 # define arg1_index 1
1636 setbpt(struct tcb *tcp)
1638 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
1639 arg_setup_state state;
1641 if (tcp->flags & TCB_BPTSET) {
1642 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1647 * It's a silly kludge to initialize this with a search at runtime.
1648 * But it's better than maintaining another magic thing in the
1649 * godforsaken tables.
1651 if (clone_scno[current_personality] == 0) {
1653 for (i = 0; i < nsyscalls; ++i)
1654 if (sysent[i].sys_func == sys_clone) {
1655 clone_scno[current_personality] = i;
1660 switch (known_scno(tcp)) {
1667 # if defined SYS_fork || defined SYS_vfork
1668 if (arg_setup (tcp, &state) < 0
1669 || get_arg0 (tcp, &state, &tcp->inst[0]) < 0
1670 || get_arg1 (tcp, &state, &tcp->inst[1]) < 0
1671 || change_syscall(tcp, clone_scno[current_personality]) < 0
1672 || set_arg0 (tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1673 || set_arg1 (tcp, &state, 0) < 0
1674 || arg_finish_change (tcp, &state) < 0)
1676 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1677 tcp->u_arg[arg1_index] = 0;
1678 tcp->flags |= TCB_BPTSET;
1686 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1687 contrary to x86 SYS_vfork above. Even on x86 we turn the
1688 vfork semantics into plain fork - each application must not
1689 depend on the vfork specifics according to POSIX. We would
1690 hang waiting for the parent resume otherwise. We need to
1691 clear also CLONE_VM but only in the CLONE_VFORK case as
1692 otherwise we would break pthread_create. */
1694 if ((arg_setup (tcp, &state) < 0
1695 || set_arg0 (tcp, &state,
1696 (tcp->u_arg[arg0_index] | CLONE_PTRACE)
1697 & ~(tcp->u_arg[arg0_index] & CLONE_VFORK
1698 ? CLONE_VFORK | CLONE_VM : 0)) < 0
1699 || arg_finish_change (tcp, &state) < 0))
1701 tcp->flags |= TCB_BPTSET;
1702 tcp->inst[0] = tcp->u_arg[arg0_index];
1703 tcp->inst[1] = tcp->u_arg[arg1_index];
1707 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1708 tcp->scno, tcp->pid);
1719 arg_setup_state state;
1720 if (arg_setup (tcp, &state) < 0
1721 || restore_arg0 (tcp, &state, tcp->inst[0]) < 0
1722 || restore_arg1 (tcp, &state, tcp->inst[1]) < 0
1723 || arg_finish_change (tcp, &state))
1725 tcp->flags &= ~TCB_BPTSET;
1729 # else /* !defined LINUX */
1736 DEAD CODE HERE? WE ARE IN 'else !defined LINUX'
1737 # if defined (SPARC) || defined (SPARC64)
1738 /* We simply use the SunOS breakpoint code. */
1742 # define LOOPA 0x30800000 /* ba,a 0 */
1744 if (tcp->flags & TCB_BPTSET) {
1745 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1748 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1749 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1752 tcp->baddr = regs.r_o7 + 8;
1754 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)tcp->baddr, 0);
1756 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1761 * XXX - BRUTAL MODE ON
1762 * We cannot set a real BPT in the child, since it will not be
1763 * traced at the moment it will reach the trap and would probably
1764 * die with a core dump.
1765 * Thus, we are force our way in by taking out two instructions
1766 * and insert an eternal loop instead, in expectance of the SIGSTOP
1767 * generated by our PTRACE_ATTACH.
1768 * Of cause, if we evaporate ourselves in the middle of all this...
1772 # if defined (SPARC64)
1774 inst |= (tcp->inst[0] & 0xffffffffUL);
1776 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, inst);
1778 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1781 tcp->flags |= TCB_BPTSET;
1783 # else /* !SPARC && !SPARC64 */
1786 # define LOOP 0x0000feeb
1787 if (tcp->flags & TCB_BPTSET) {
1788 fprintf(stderr, "PANIC: bpt already set in pid %u\n",
1792 if (upeek(tcp, PT_CR_IIP, &tcp->baddr) < 0)
1795 fprintf(stderr, "[%d] setting bpt at %lx\n",
1796 tcp->pid, tcp->baddr);
1797 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid,
1798 (char *) tcp->baddr, 0);
1800 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1803 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP);
1805 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1808 tcp->flags |= TCB_BPTSET;
1811 * Our strategy here is to replace the bundle that
1812 * contained the clone() syscall with a bundle of the
1815 * { 1: br 1b; br 1b; br 1b }
1817 * This ensures that the newly forked child will loop
1818 * endlessly until we've got a chance to attach to it.
1820 # define LOOP0 0x0000100000000017
1821 # define LOOP1 0x4000000000200000
1822 unsigned long addr, ipsr;
1826 if (upeek(tcp, PT_CR_IPSR, &ipsr) < 0)
1828 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
1830 /* store "ri" in low two bits */
1831 tcp->baddr = addr | ((ipsr >> 41) & 0x3);
1834 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 0,
1836 tcp->inst[1] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 8,
1839 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1844 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, LOOP0);
1845 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, LOOP1);
1847 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1850 tcp->flags |= TCB_BPTSET;
1854 # if defined (I386) || defined(X86_64)
1855 # define LOOP 0x0000feeb
1856 # elif defined (M68K)
1857 # define LOOP 0x60fe0000
1858 # elif defined (ALPHA)
1859 # define LOOP 0xc3ffffff
1860 # elif defined (POWERPC)
1861 # define LOOP 0x48000000
1863 # define LOOP 0xEAFFFFFE
1864 # elif defined(MIPS)
1865 # define LOOP 0x1000ffff
1866 # elif defined(S390)
1867 # define LOOP 0xa7f40000 /* BRC 15,0 */
1868 # elif defined(S390X)
1869 # define LOOP 0xa7f4000000000000UL /* BRC 15,0 */
1870 # elif defined(HPPA)
1871 # define LOOP 0xe81f1ff7 /* b,l,n <loc>,r0 */
1873 # ifdef __LITTLE_ENDIAN__
1874 # define LOOP 0x0000affe
1876 # define LOOP 0xfeaf0000
1879 # error unknown architecture
1882 if (tcp->flags & TCB_BPTSET) {
1883 fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid);
1887 if (upeek(tcp, 4*EIP, &tcp->baddr) < 0)
1889 # elif defined (X86_64)
1890 if (upeek(tcp, 8*RIP, &tcp->baddr) < 0)
1892 # elif defined (M68K)
1893 if (upeek(tcp, 4*PT_PC, &tcp->baddr) < 0)
1895 # elif defined (ALPHA)
1897 # elif defined (ARM)
1899 # elif defined (MIPS)
1900 return -1; /* FIXME: I do not know what i do - Flo */
1901 # elif defined (POWERPC)
1902 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &tcp->baddr) < 0)
1904 # elif defined(S390) || defined(S390X)
1905 if (upeek(tcp,PT_PSWADDR, &tcp->baddr) < 0)
1907 # elif defined(HPPA)
1908 if (upeek(tcp, PT_IAOQ0, &tcp->baddr) < 0)
1910 tcp->baddr &= ~0x03;
1912 if (upeek(tcp, 4*REG_PC, &tcp->baddr) < 0)
1915 # error unknown architecture
1918 fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr);
1919 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *) tcp->baddr, 0);
1921 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1924 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP);
1926 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1929 tcp->flags |= TCB_BPTSET;
1932 # endif /* !SPARC && !SPARC64 */
1936 # ifdef SPARC /* This code is slightly sparc specific */
1939 # define BPT 0x91d02001 /* ta 1 */
1940 # define LOOP 0x10800000 /* ba 0 */
1941 # define LOOPA 0x30800000 /* ba,a 0 */
1942 # define NOP 0x01000000
1944 static int loopdeloop[1] = {LOOPA};
1946 static int loopdeloop[2] = {LOOP, NOP};
1949 if (tcp->flags & TCB_BPTSET) {
1950 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1953 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1954 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1957 tcp->baddr = regs.r_o7 + 8;
1958 if (ptrace(PTRACE_READTEXT, tcp->pid, (char *)tcp->baddr,
1959 sizeof tcp->inst, (char *)tcp->inst) < 0) {
1960 perror("setbpt: ptrace(PTRACE_READTEXT, ...)");
1965 * XXX - BRUTAL MODE ON
1966 * We cannot set a real BPT in the child, since it will not be
1967 * traced at the moment it will reach the trap and would probably
1968 * die with a core dump.
1969 * Thus, we are force our way in by taking out two instructions
1970 * and insert an eternal loop in stead, in expectance of the SIGSTOP
1971 * generated by out PTRACE_ATTACH.
1972 * Of cause, if we evaporate ourselves in the middle of all this...
1974 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1975 sizeof loopdeloop, (char *) loopdeloop) < 0) {
1976 perror("setbpt: ptrace(PTRACE_WRITETEXT, ...)");
1979 tcp->flags |= TCB_BPTSET;
1982 # endif /* SUNOS4 */
1993 DEAD CODE HERE? WE ARE IN 'else !defined LINUX'
1994 # if defined(I386) || defined(X86_64)
1996 # elif defined(POWERPC)
1998 # elif defined(M68K)
2000 # elif defined(ALPHA)
2002 # elif defined(HPPA)
2006 # endif /* architecture */
2008 # if defined (SPARC) || defined (SPARC64)
2009 /* Again, we borrow the SunOS breakpoint code. */
2010 if (!(tcp->flags & TCB_BPTSET)) {
2011 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2015 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
2017 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
2020 tcp->flags &= ~TCB_BPTSET;
2021 # elif defined(IA64)
2026 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
2027 if (!(tcp->flags & TCB_BPTSET)) {
2028 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2032 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
2034 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
2037 tcp->flags &= ~TCB_BPTSET;
2039 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
2041 if (addr != tcp->baddr) {
2042 /* The breakpoint has not been reached yet. */
2045 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2050 unsigned long addr, ipsr;
2055 if (upeek(tcp, PT_CR_IPSR, &ipsr) < 0)
2057 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
2060 /* restore original bundle: */
2062 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, tcp->inst[0]);
2063 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, tcp->inst[1]);
2065 perror("clearbpt: ptrace(PTRACE_POKETEXT, ...)");
2069 /* restore original "ri" in ipsr: */
2070 ipsr = (ipsr & ~(0x3ul << 41)) | ((tcp->baddr & 0x3) << 41);
2072 ptrace(PTRACE_POKEUSER, pid, (char *) PT_CR_IPSR, ipsr);
2074 perror("clrbpt: ptrace(PTRACE_POKEUSER, ...)");
2078 tcp->flags &= ~TCB_BPTSET;
2080 if (addr != (tcp->baddr & ~0x3)) {
2081 /* the breakpoint has not been reached yet. */
2083 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2088 # else /* !IA64 && !SPARC && !SPARC64 */
2091 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
2092 if (!(tcp->flags & TCB_BPTSET)) {
2093 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2097 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
2099 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
2102 tcp->flags &= ~TCB_BPTSET;
2105 if (upeek(tcp, 4*EIP, &eip) < 0)
2107 if (eip != tcp->baddr) {
2108 /* The breakpoint has not been reached yet. */
2111 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2115 # elif defined(X86_64)
2116 if (upeek(tcp, 8*RIP, &eip) < 0)
2118 if (eip != tcp->baddr) {
2119 /* The breakpoint has not been reached yet. */
2122 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2126 # elif defined(POWERPC)
2127 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0)
2129 if (pc != tcp->baddr) {
2130 /* The breakpoint has not been reached yet. */
2132 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2136 # elif defined(M68K)
2137 if (upeek(tcp, 4*PT_PC, &pc) < 0)
2139 if (pc != tcp->baddr) {
2140 /* The breakpoint has not been reached yet. */
2142 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2146 # elif defined(ALPHA)
2147 if (upeek(tcp, REG_PC, &pc) < 0)
2149 if (pc != tcp->baddr) {
2150 /* The breakpoint has not been reached yet. */
2152 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2156 # elif defined(HPPA)
2157 if (upeek(tcp, PT_IAOQ0, &iaoq) < 0)
2160 if (iaoq != tcp->baddr && iaoq != tcp->baddr + 4) {
2161 /* The breakpoint has not been reached yet. */
2163 fprintf(stderr, "NOTE: PC not at bpt (iaoq %#lx baddr %#lx)\n",
2167 iaoq = tcp->baddr | 3;
2168 /* We should be pointing at a 'ldi -1000,r1' in glibc, so it is
2169 * safe to set both IAOQ0 and IAOQ1 to that so the PSW N bit
2170 * has no significant effect.
2172 ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ0, iaoq);
2173 ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ1, iaoq);
2175 if (upeek(tcp, 4*REG_PC, &pc) < 0)
2177 if (pc != tcp->baddr) {
2178 /* The breakpoint has not been reached yet. */
2180 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2186 # endif /* !SPARC && !SPARC64 && !IA64 */
2196 if (!(tcp->flags & TCB_BPTSET)) {
2197 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2200 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
2201 sizeof tcp->inst, (char *) tcp->inst) < 0) {
2202 perror("clearbtp: ptrace(PTRACE_WRITETEXT, ...)");
2205 tcp->flags &= ~TCB_BPTSET;
2209 * Since we don't have a single instruction breakpoint, we may have
2210 * to adjust the program counter after removing our `breakpoint'.
2212 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
2213 perror("clearbpt: ptrace(PTRACE_GETREGS, ...)");
2216 if ((regs.r_pc < tcp->baddr) ||
2217 (regs.r_pc > tcp->baddr + 4)) {
2218 /* The breakpoint has not been reached yet */
2221 "NOTE: PC not at bpt (pc %#x baddr %#x)\n",
2222 regs.r_pc, tcp->baddr);
2225 if (regs.r_pc != tcp->baddr)
2227 fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n",
2228 regs.r_pc, tcp->baddr);
2230 regs.r_pc = tcp->baddr;
2231 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) {
2232 perror("clearbpt: ptrace(PTRACE_SETREGS, ...)");
2237 # endif /* SUNOS4 */
2242 # endif /* !defined LINUX */
2244 #endif /* !USE_PROCFS */
2256 for (n = 0; n < sizeof *hdr; n += 4) {
2258 if (upeek(tcp, uoff(u_exdata) + n, &res) < 0)
2260 memcpy(((char *) hdr) + n, &res, 4);
2263 fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n",
2264 hdr->a_magic, hdr->a_toolversion, hdr->a_machtype);
2265 fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n",
2266 hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry);
2277 * Change `vfork' in a freshly exec'ed dynamically linked
2278 * executable's (internal) symbol table to plain old `fork'
2282 struct link_dynamic dyn;
2283 struct link_dynamic_2 ld;
2286 if (getex(tcp, &hdr) < 0)
2291 if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) {
2292 fprintf(stderr, "Cannot read DYNAMIC\n");
2295 if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) {
2296 fprintf(stderr, "Cannot read link_dynamic_2\n");
2299 if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) {
2300 fprintf(stderr, "out of memory\n");
2303 if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
2304 (int)ld.ld_symb_size, strtab) < 0)
2308 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
2309 fprintf(stderr, "[symbol: %s]\n", cp);
2314 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
2315 if (strcmp(cp, "_vfork") == 0) {
2317 fprintf(stderr, "fixvfork: FOUND _vfork\n");
2318 strcpy(cp, "_fork");
2323 if (cp < strtab + ld.ld_symb_size)
2325 * Write entire symbol table back to avoid
2326 * memory alignment bugs in ptrace
2328 if (tload(pid, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
2329 (int)ld.ld_symb_size, strtab) < 0)