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) && defined (SPARC64)
82 # undef PTRACE_GETREGS
83 # define PTRACE_GETREGS PTRACE_GETREGS64
84 # undef PTRACE_SETREGS
85 # define PTRACE_SETREGS PTRACE_SETREGS64
90 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
93 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
112 return a->tv_sec || a->tv_usec;
117 struct timeval *a, *b;
119 if (a->tv_sec < b->tv_sec
120 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
122 if (a->tv_sec > b->tv_sec
123 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
132 return tv->tv_sec + tv->tv_usec/1000000.0;
137 struct timeval *tv, *a, *b;
139 tv->tv_sec = a->tv_sec + b->tv_sec;
140 tv->tv_usec = a->tv_usec + b->tv_usec;
141 if (tv->tv_usec >= 1000000) {
143 tv->tv_usec -= 1000000;
149 struct timeval *tv, *a, *b;
151 tv->tv_sec = a->tv_sec - b->tv_sec;
152 tv->tv_usec = a->tv_usec - b->tv_usec;
153 if (((long) tv->tv_usec) < 0) {
155 tv->tv_usec += 1000000;
161 struct timeval *tv, *a;
164 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
165 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
166 tv->tv_usec %= 1000000;
171 struct timeval *tv, *a;
174 tv->tv_usec = a->tv_usec * n;
175 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
176 tv->tv_usec %= 1000000;
180 xlookup(const struct xlat *xlat, int val)
182 for (; xlat->str != NULL; xlat++)
183 if (xlat->val == val)
189 * Generic ptrace wrapper which tracks ESRCH errors
190 * by setting tcp->ptrace_errno to ESRCH.
192 * We assume that ESRCH indicates likely process death (SIGKILL?),
193 * modulo bugs where process somehow ended up not stopped.
194 * Unfortunately kernel uses ESRCH for that case too. Oh well.
196 * Currently used by upeek() only.
197 * TODO: use this in all other ptrace() calls while decoding.
200 do_ptrace(int request, struct tcb *tcp, void *addr, void *data)
205 l = ptrace(request, tcp->pid, addr, (long) data);
206 /* Non-ESRCH errors might be our invalid reg/mem accesses,
207 * we do not record them. */
209 tcp->ptrace_errno = ESRCH;
214 * Used when we want to unblock stopped traced process.
215 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
216 * Returns 0 on success or if error was ESRCH
217 * (presumably process was killed while we talk to it).
218 * Otherwise prints error message and returns -1.
221 ptrace_restart(int op, struct tcb *tcp, int sig)
227 ptrace(op, tcp->pid, (void *) 1, (long) sig);
229 if (!err || err == ESRCH)
232 tcp->ptrace_errno = err;
234 if (op == PTRACE_CONT)
236 if (op == PTRACE_DETACH)
238 fprintf(stderr, "strace: ptrace(PTRACE_%s,1,%d): %s\n",
239 msg, sig, strerror(err));
244 * Print entry in struct xlat table, if there.
247 printxval(const struct xlat *xlat, int val, const char *dflt)
249 const char *str = xlookup(xlat, val);
254 tprintf("%#x /* %s */", val, dflt);
259 * Print 64bit argument at position llarg and return the index of the next
263 printllval(struct tcb *tcp, const char *format, int llarg)
265 # if defined(FREEBSD) \
266 || (defined(LINUX) && defined(POWERPC) && !defined(__powerpc64__)) \
267 || defined (LINUX_MIPSO32)
268 /* Align 64bit argument to 64bit boundary. */
269 if (llarg % 2) llarg++;
271 # if defined LINUX && defined X86_64
272 if (current_personality == 0) {
273 tprintf(format, tcp->u_arg[llarg]);
276 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
279 # elif defined IA64 || defined ALPHA || (defined POWERPC && defined __powerpc64__)
280 tprintf(format, tcp->u_arg[llarg]);
282 # elif defined LINUX_MIPSN32
283 tprintf(format, tcp->ext_arg[llarg]);
286 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
294 * Interpret `xlat' as an array of flags
295 * print the entries whose bits are on in `flags'
296 * return # of flags printed.
299 addflags(xlat, flags)
300 const struct xlat *xlat;
305 for (n = 0; xlat->str; xlat++) {
306 if (xlat->val && (flags & xlat->val) == xlat->val) {
307 tprintf("|%s", xlat->str);
313 tprintf("|%#x", flags);
320 * Interpret `xlat' as an array of flags/
321 * Print to static string the entries whose bits are on in `flags'
322 * Return static string.
325 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
327 static char outstr[1024];
330 strcpy(outstr, prefix);
332 for (; xlat->str; xlat++) {
333 if ((flags & xlat->val) == xlat->val) {
336 strcat(outstr, xlat->str);
344 sprintf(outstr + strlen(outstr), "%#x", flags);
351 printflags(xlat, flags, dflt)
352 const struct xlat *xlat;
359 if (flags == 0 && xlat->val == 0) {
360 tprintf("%s", xlat->str);
365 for (n = 0; xlat->str; xlat++) {
366 if (xlat->val && (flags & xlat->val) == xlat->val) {
367 tprintf("%s%s", sep, xlat->str);
376 tprintf("%s%#x", sep, flags);
381 tprintf("%#x", flags);
383 tprintf(" /* %s */", dflt);
394 printnum(tcp, addr, fmt)
405 if (umove(tcp, addr, &num) < 0) {
406 tprintf("%#lx", addr);
415 printnum_int(tcp, addr, fmt)
426 if (umove(tcp, addr, &num) < 0) {
427 tprintf("%#lx", addr);
441 tprintf((uid == -1) ? "%ld" : "%lu", uid);
444 static char path[MAXPATHLEN + 1];
447 * Quote string `instr' of length `size'
448 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
449 * If `len' < 0, treat `instr' as a NUL-terminated string
450 * and quote at most (`size' - 1) bytes.
453 string_quote(const char *instr, char *outstr, int len, int size)
455 const unsigned char *ustr = (const unsigned char *) instr;
457 int usehex = 0, c, i;
462 /* Check for presence of symbol which require
463 to hex-quote the whole string. */
464 for (i = 0; i < size; ++i) {
466 /* Check for NUL-terminated string. */
470 /* Quote at most size - 1 bytes. */
474 if (!isprint(c) && !isspace(c)) {
484 /* Hex-quote the whole string. */
485 for (i = 0; i < size; ++i) {
487 /* Check for NUL-terminated string. */
491 /* Quote at most size - 1 bytes. */
495 sprintf(s, "\\x%02x", c);
499 for (i = 0; i < size; ++i) {
501 /* Check for NUL-terminated string. */
505 /* Quote at most size - 1 bytes. */
510 case '\"': case '\\':
537 else if (i + 1 < size
538 && isdigit(ustr[i + 1])) {
539 sprintf(s, "\\%03o", c);
542 sprintf(s, "\\%o", c);
553 /* Return nonzero if the string was unterminated. */
558 * Print path string specified by address `addr' and length `n'.
559 * If path length exceeds `n', append `...' to the output.
562 printpathn(struct tcb *tcp, long addr, int n)
569 /* Cap path length to the path buffer size,
570 and NUL-terminate the buffer. */
571 if (n > sizeof path - 1)
575 /* Fetch one byte more to find out whether path length > n. */
576 if (umovestr(tcp, addr, n + 1, path) < 0)
577 tprintf("%#lx", addr);
579 static char outstr[4*(sizeof path - 1) + sizeof "\"...\""];
580 int trunc = (path[n] != '\0');
584 (void) string_quote(path, outstr, -1, n + 1);
586 strcat(outstr, "...");
587 tprintf("%s", outstr);
592 printpath(struct tcb *tcp, long addr)
594 printpathn(tcp, addr, sizeof path - 1);
598 * Print string specified by address `addr' and length `len'.
599 * If `len' < 0, treat the string as a NUL-terminated string.
600 * If string length exceeds `max_strlen', append `...' to the output.
603 printstr(struct tcb *tcp, long addr, int len)
605 static char *str = NULL;
613 /* Allocate static buffers if they are not allocated yet. */
615 str = malloc(max_strlen + 1);
617 outstr = malloc(4 * max_strlen + sizeof "\"...\"");
618 if (!str || !outstr) {
619 fprintf(stderr, "out of memory\n");
620 tprintf("%#lx", addr);
626 * Treat as a NUL-terminated string: fetch one byte more
627 * because string_quote() quotes one byte less.
629 size = max_strlen + 1;
630 str[max_strlen] = '\0';
631 if (umovestr(tcp, addr, size, str) < 0) {
632 tprintf("%#lx", addr);
637 size = MIN(len, max_strlen);
638 if (umoven(tcp, addr, size, str) < 0) {
639 tprintf("%#lx", addr);
644 if (string_quote(str, outstr, len, size) &&
645 (len < 0 || len > max_strlen))
646 strcat(outstr, "...");
648 tprintf("%s", outstr);
653 dumpiov(tcp, len, addr)
658 #if defined(LINUX) && SUPPORTED_PERSONALITIES > 1
660 struct { u_int32_t base; u_int32_t len; } *iov32;
661 struct { u_int64_t base; u_int64_t len; } *iov64;
663 #define iov iovu.iov64
665 (personality_wordsize[current_personality] == 4 \
666 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
667 #define iov_iov_base(i) \
668 (personality_wordsize[current_personality] == 4 \
669 ? (u_int64_t) iovu.iov32[i].base : iovu.iov64[i].base)
670 #define iov_iov_len(i) \
671 (personality_wordsize[current_personality] == 4 \
672 ? (u_int64_t) iovu.iov32[i].len : iovu.iov64[i].len)
675 #define sizeof_iov sizeof(*iov)
676 #define iov_iov_base(i) iov[i].iov_base
677 #define iov_iov_len(i) iov[i].iov_len
682 size = sizeof_iov * (unsigned long) len;
683 if (size / sizeof_iov != len
684 || (iov = malloc(size)) == NULL) {
685 fprintf(stderr, "out of memory\n");
688 if (umoven(tcp, addr, size, (char *) iov) >= 0) {
689 for (i = 0; i < len; i++) {
690 /* include the buffer number to make it easy to
691 * match up the trace with the source */
692 tprintf(" * %lu bytes in buffer %d\n",
693 (unsigned long)iov_iov_len(i), i);
694 dumpstr(tcp, (long) iov_iov_base(i),
707 dumpstr(tcp, addr, len)
712 static int strsize = -1;
713 static unsigned char *str;
714 static char outstr[80];
721 if ((str = malloc(len)) == NULL) {
722 fprintf(stderr, "out of memory\n");
728 if (umoven(tcp, addr, len, (char *) str) < 0)
731 for (i = 0; i < len; i += 16) {
733 sprintf(s, " | %05x ", i);
735 for (j = 0; j < 16; j++) {
739 sprintf(s, " %02x", str[i + j]);
743 *s++ = ' '; *s++ = ' '; *s++ = ' ';
746 *s++ = ' '; *s++ = ' ';
747 for (j = 0; j < 16; j++) {
751 if (isprint(str[i + j]))
759 tprintf("%s |\n", outstr);
763 #define PAGMASK (~(PAGSIZ - 1))
765 * move `len' bytes of data from process `pid'
766 * at address `addr' to our space at `laddr'
769 umoven(struct tcb *tcp, long addr, int len, char *laddr)
777 char x[sizeof(long)];
780 if (addr & (sizeof(long) - 1)) {
781 /* addr not a multiple of sizeof(long) */
782 n = addr - (addr & -sizeof(long)); /* residue */
783 addr &= -sizeof(long); /* residue */
785 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
787 if (started && (errno==EPERM || errno==EIO)) {
788 /* Ran into 'end of memory' - stupid "printpath" */
791 /* But if not started, we had a bogus address. */
792 if (addr != 0 && errno != EIO && errno != ESRCH)
793 perror("ptrace: umoven");
797 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
798 addr += sizeof(long), laddr += m, len -= m;
802 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
804 if (started && (errno==EPERM || errno==EIO)) {
805 /* Ran into 'end of memory' - stupid "printpath" */
808 if (addr != 0 && errno != EIO && errno != ESRCH)
809 perror("ptrace: umoven");
813 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
814 addr += sizeof(long), laddr += m, len -= m;
824 char x[sizeof(long)];
827 if (addr & (sizeof(long) - 1)) {
828 /* addr not a multiple of sizeof(long) */
829 n = addr - (addr & -sizeof(long)); /* residue */
830 addr &= -sizeof(long); /* residue */
832 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
838 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
839 addr += sizeof(long), laddr += m, len -= m;
843 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
849 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
850 addr += sizeof(long), laddr += m, len -= m;
856 n = MIN(len, PAGSIZ);
857 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
858 if (ptrace(PTRACE_READDATA, pid,
859 (char *) addr, len, laddr) < 0) {
860 if (errno != ESRCH) {
861 perror("umoven: ptrace(PTRACE_READDATA, ...)");
874 #ifdef HAVE_MP_PROCFS
875 int fd = tcp->pfd_as;
879 lseek(fd, addr, SEEK_SET);
880 if (read(fd, laddr, len) == -1)
882 #endif /* USE_PROCFS */
888 * like `umove' but make the additional effort of looking
889 * for a terminating zero byte.
892 umovestr(struct tcb *tcp, long addr, int len, char *laddr)
895 #ifdef HAVE_MP_PROCFS
896 int fd = tcp->pfd_as;
900 /* Some systems (e.g. FreeBSD) can be upset if we read off the
901 end of valid memory, avoid this by trying to read up
902 to page boundaries. But we don't know what a page is (and
903 getpagesize(2) (if it exists) doesn't necessarily return
904 hardware page size). Assume all pages >= 1024 (a-historical
907 int page = 1024; /* How to find this? */
908 int move = page - (addr & (page - 1));
911 lseek(fd, addr, SEEK_SET);
914 if (move > left) move = left;
915 if ((move = read(fd, laddr, move)) <= 0)
916 return left != len ? 0 : -1;
917 if (memchr (laddr, 0, move)) break;
923 #else /* !USE_PROCFS */
929 char x[sizeof(long)];
932 if (addr & (sizeof(long) - 1)) {
933 /* addr not a multiple of sizeof(long) */
934 n = addr - (addr & -sizeof(long)); /* residue */
935 addr &= -sizeof(long); /* residue */
937 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
939 if (started && (errno==EPERM || errno==EIO)) {
940 /* Ran into 'end of memory' - stupid "printpath" */
943 if (addr != 0 && errno != EIO && errno != ESRCH)
948 memcpy(laddr, &u.x[n], m = MIN(sizeof(long)-n,len));
949 while (n & (sizeof(long) - 1))
950 if (u.x[n++] == '\0')
952 addr += sizeof(long), laddr += m, len -= m;
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, m = MIN(sizeof(long), len));
968 for (i = 0; i < sizeof(long); i++)
972 addr += sizeof(long), laddr += m, len -= m;
974 #endif /* !USE_PROCFS */
979 # if !defined (SPARC) && !defined(SPARC64)
980 # define PTRACE_WRITETEXT 101
981 # define PTRACE_WRITEDATA 102
982 # endif /* !SPARC && !SPARC64 */
988 uload(cmd, pid, addr, len, laddr)
999 n = MIN(len, PAGSIZ);
1000 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
1001 if (ptrace(cmd, pid, (char *)addr, n, laddr) < 0) {
1002 perror("uload: ptrace(PTRACE_WRITE, ...)");
1014 char x[sizeof(long)];
1017 if (cmd == PTRACE_WRITETEXT) {
1018 peek = PTRACE_PEEKTEXT;
1019 poke = PTRACE_POKETEXT;
1022 peek = PTRACE_PEEKDATA;
1023 poke = PTRACE_POKEDATA;
1025 if (addr & (sizeof(long) - 1)) {
1026 /* addr not a multiple of sizeof(long) */
1027 n = addr - (addr & -sizeof(long)); /* residue */
1028 addr &= -sizeof(long);
1030 u.val = ptrace(peek, pid, (char *) addr, 0);
1032 perror("uload: POKE");
1035 memcpy(&u.x[n], laddr, m = MIN(sizeof(long) - n, len));
1036 if (ptrace(poke, pid, (char *)addr, u.val) < 0) {
1037 perror("uload: POKE");
1040 addr += sizeof(long), laddr += m, len -= m;
1043 if (len < sizeof(long))
1044 u.val = ptrace(peek, pid, (char *) addr, 0);
1045 memcpy(u.x, laddr, m = MIN(sizeof(long), len));
1046 if (ptrace(poke, pid, (char *) addr, u.val) < 0) {
1047 perror("uload: POKE");
1050 addr += sizeof(long), laddr += m, len -= m;
1057 tload(pid, addr, len, laddr)
1062 return uload(PTRACE_WRITETEXT, pid, addr, len, laddr);
1066 dload(pid, addr, len, laddr)
1072 return uload(PTRACE_WRITEDATA, pid, addr, len, laddr);
1080 upeek(tcp, off, res)
1087 # ifdef SUNOS4_KERNEL_ARCH_KLUDGE
1089 static int is_sun4m = -1;
1090 struct utsname name;
1092 /* Round up the usual suspects. */
1093 if (is_sun4m == -1) {
1094 if (uname(&name) < 0) {
1095 perror("upeek: uname?");
1098 is_sun4m = strcmp(name.machine, "sun4m") == 0;
1100 const struct xlat *x;
1102 for (x = struct_user_offsets; x->str; x++)
1109 # endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
1111 val = do_ptrace(PTRACE_PEEKUSER, tcp, (char *) off, 0);
1112 if (val == -1 && errno) {
1113 if (errno != ESRCH) {
1115 sprintf(buf,"upeek: ptrace(PTRACE_PEEKUSER,%d,%lu,0)", tcp->pid, off);
1124 #endif /* !USE_PROCFS */
1128 getpc(struct tcb *tcp)
1134 if (upeek(tcp, 4*EIP, &pc) < 0)
1136 # elif defined(X86_64)
1137 if (upeek(tcp, 8*RIP, &pc) < 0)
1139 # elif defined(IA64)
1140 if (upeek(tcp, PT_B0, &pc) < 0)
1143 if (upeek(tcp, 4*15, &pc) < 0)
1145 # elif defined(AVR32)
1146 if (upeek(tcp, REG_PC, &pc) < 0)
1148 # elif defined(BFIN)
1149 if (upeek(tcp, REG_PC, &pc) < 0)
1151 # elif defined(POWERPC)
1152 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0)
1154 # elif defined(M68K)
1155 if (upeek(tcp, 4*PT_PC, &pc) < 0)
1157 # elif defined(ALPHA)
1158 if (upeek(tcp, REG_PC, &pc) < 0)
1160 # elif defined(MIPS)
1161 if (upeek(tcp, REG_EPC, &pc) < 0)
1163 # elif defined(SPARC) || defined(SPARC64)
1164 struct pt_regs regs;
1165 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1167 # if defined(SPARC64)
1172 # elif defined(S390) || defined(S390X)
1173 if(upeek(tcp,PT_PSWADDR,&pc) < 0)
1175 # elif defined(HPPA)
1176 if(upeek(tcp,PT_IAOQ0,&pc) < 0)
1179 if (upeek(tcp, 4*REG_PC ,&pc) < 0)
1181 # elif defined(SH64)
1182 if (upeek(tcp, REG_PC ,&pc) < 0)
1190 * Return current program counter for `pid'
1191 * Assumes PC is never 0xffffffff
1195 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
1196 perror("getpc: ptrace(PTRACE_GETREGS, ...)");
1209 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
1211 #endif /* FREEBSD */
1216 printcall(struct tcb *tcp)
1218 #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
1219 sizeof(long) == 8 ? "[????????????????] " : \
1226 if (upeek(tcp, 4*EIP, &eip) < 0) {
1230 tprintf("[%08lx] ", eip);
1232 # elif defined(S390) || defined(S390X)
1234 if(upeek(tcp,PT_PSWADDR,&psw) < 0) {
1239 tprintf("[%08lx] ", psw);
1241 tprintf("[%16lx] ", psw);
1244 # elif defined(X86_64)
1247 if (upeek(tcp, 8*RIP, &rip) < 0) {
1251 tprintf("[%16lx] ", rip);
1252 # elif defined(IA64)
1255 if (upeek(tcp, PT_B0, &ip) < 0) {
1259 tprintf("[%08lx] ", ip);
1260 # elif defined(POWERPC)
1263 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
1264 tprintf ("[????????] ");
1267 tprintf("[%08lx] ", pc);
1268 # elif defined(M68K)
1271 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
1272 tprintf ("[????????] ");
1275 tprintf("[%08lx] ", pc);
1276 # elif defined(ALPHA)
1279 if (upeek(tcp, REG_PC, &pc) < 0) {
1280 tprintf ("[????????????????] ");
1283 tprintf("[%08lx] ", pc);
1284 # elif defined(SPARC) || defined(SPARC64)
1285 struct pt_regs regs;
1286 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) {
1290 # if defined(SPARC64)
1291 tprintf("[%08lx] ", regs.tpc);
1293 tprintf("[%08lx] ", regs.pc);
1295 # elif defined(HPPA)
1298 if(upeek(tcp,PT_IAOQ0,&pc) < 0) {
1299 tprintf ("[????????] ");
1302 tprintf("[%08lx] ", pc);
1303 # elif defined(MIPS)
1306 if (upeek(tcp, REG_EPC, &pc) < 0) {
1307 tprintf ("[????????] ");
1310 tprintf("[%08lx] ", pc);
1314 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
1315 tprintf ("[????????] ");
1318 tprintf("[%08lx] ", pc);
1319 # elif defined(SH64)
1322 if (upeek(tcp, REG_PC, &pc) < 0) {
1323 tprintf ("[????????????????] ");
1326 tprintf("[%08lx] ", pc);
1330 if (upeek(tcp, 4*15, &pc) < 0) {
1334 tprintf("[%08lx] ", pc);
1335 # elif defined(AVR32)
1338 if (upeek(tcp, REG_PC, &pc) < 0) {
1339 tprintf("[????????] ");
1342 tprintf("[%08lx] ", pc);
1343 # elif defined(BFIN)
1346 if (upeek(tcp, PT_PC, &pc) < 0) {
1350 tprintf("[%08lx] ", pc);
1351 #elif defined(CRISV10)
1354 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
1358 tprintf("[%08lx] ", pc);
1359 #elif defined(CRISV32)
1362 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
1366 tprintf("[%08lx] ", pc);
1367 # endif /* architecture */
1373 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
1374 perror("printcall: ptrace(PTRACE_GETREGS, ...)");
1378 tprintf("[%08x] ", regs.r_o7);
1388 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
1389 tprintf("[%08x] ", regs.r_eip);
1390 #endif /* FREEBSD */
1395 * These #if's are huge, please indent them correctly.
1396 * It's easy to get confused otherwise.
1402 # include "syscall.h"
1404 # include <sys/syscall.h>
1405 # ifndef CLONE_PTRACE
1406 # define CLONE_PTRACE 0x00002000
1408 # ifndef CLONE_VFORK
1409 # define CLONE_VFORK 0x00004000
1412 # define CLONE_VM 0x00000100
1414 # ifndef CLONE_STOPPED
1415 # define CLONE_STOPPED 0x02000000
1420 /* We don't have fork()/vfork() syscalls on ia64 itself, but the ia32
1421 subsystem has them for x86... */
1423 # define SYS_vfork 190
1425 typedef unsigned long *arg_setup_state;
1428 arg_setup(struct tcb *tcp, arg_setup_state *state)
1430 unsigned long cfm, sof, sol;
1434 /* Satisfy a false GCC warning. */
1439 if (upeek(tcp, PT_AR_BSP, &bsp) < 0)
1441 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1444 sof = (cfm >> 0) & 0x7f;
1445 sol = (cfm >> 7) & 0x7f;
1446 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
1448 *state = (unsigned long *) bsp;
1452 # define arg_finish_change(tcp, state) 0
1456 get_arg0 (struct tcb *tcp, arg_setup_state *state, long *valp)
1461 ret = upeek (tcp, PT_R11, valp);
1464 (unsigned long) ia64_rse_skip_regs(*state, 0),
1465 sizeof(long), (void *) valp);
1470 get_arg1 (struct tcb *tcp, arg_setup_state *state, long *valp)
1475 ret = upeek (tcp, PT_R9, valp);
1478 (unsigned long) ia64_rse_skip_regs(*state, 1),
1479 sizeof(long), (void *) valp);
1485 set_arg0 (struct tcb *tcp, arg_setup_state *state, long val)
1487 int req = PTRACE_POKEDATA;
1491 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1492 req = PTRACE_POKEUSER;
1494 ap = ia64_rse_skip_regs(*state, 0);
1496 ptrace(req, tcp->pid, ap, val);
1497 return errno ? -1 : 0;
1501 set_arg1 (struct tcb *tcp, arg_setup_state *state, long val)
1503 int req = PTRACE_POKEDATA;
1507 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1508 req = PTRACE_POKEUSER;
1510 ap = ia64_rse_skip_regs(*state, 1);
1512 ptrace(req, tcp->pid, ap, val);
1513 return errno ? -1 : 0;
1516 /* ia64 does not return the input arguments from functions (and syscalls)
1517 according to ia64 RSE (Register Stack Engine) behavior. */
1519 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1520 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1522 # elif defined (SPARC) || defined (SPARC64)
1524 typedef struct pt_regs arg_setup_state;
1526 # define arg_setup(tcp, state) \
1527 (ptrace (PTRACE_GETREGS, tcp->pid, (char *) (state), 0))
1528 # define arg_finish_change(tcp, state) \
1529 (ptrace (PTRACE_SETREGS, tcp->pid, (char *) (state), 0))
1531 # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0)
1532 # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0)
1533 # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0)
1534 # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0)
1535 # define restore_arg0(tcp, state, val) 0
1537 # else /* other architectures */
1539 # if defined S390 || defined S390X
1540 /* Note: this is only true for the `clone' system call, which handles
1541 arguments specially. We could as well say that its first two arguments
1542 are swapped relative to other architectures, but that would just be
1543 another #ifdef in the calls. */
1544 # define arg0_offset PT_GPR3
1545 # define arg1_offset PT_ORIGGPR2
1546 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1547 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1548 # define arg0_index 1
1549 # define arg1_index 0
1550 # elif defined (ALPHA) || defined (MIPS)
1551 # define arg0_offset REG_A0
1552 # define arg1_offset (REG_A0+1)
1553 # elif defined (AVR32)
1554 # define arg0_offset (REG_R12)
1555 # define arg1_offset (REG_R11)
1556 # elif defined (POWERPC)
1557 # define arg0_offset (sizeof(unsigned long)*PT_R3)
1558 # define arg1_offset (sizeof(unsigned long)*PT_R4)
1559 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1560 # elif defined (HPPA)
1561 # define arg0_offset PT_GR26
1562 # define arg1_offset (PT_GR26-4)
1563 # elif defined (X86_64)
1564 # define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1565 # define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1567 # define arg0_offset (4*(REG_REG0+4))
1568 # define arg1_offset (4*(REG_REG0+5))
1569 # elif defined (SH64)
1570 /* ABI defines arg0 & 1 in r2 & r3 */
1571 # define arg0_offset (REG_OFFSET+16)
1572 # define arg1_offset (REG_OFFSET+24)
1573 # define restore_arg0(tcp, state, val) 0
1574 # elif defined CRISV10 || defined CRISV32
1575 # define arg0_offset (4*PT_R11)
1576 # define arg1_offset (4*PT_ORIG_R10)
1577 # define restore_arg0(tcp, state, val) 0
1578 # define restore_arg1(tcp, state, val) 0
1579 # define arg0_index 1
1580 # define arg1_index 0
1582 # define arg0_offset 0
1583 # define arg1_offset 4
1585 # define restore_arg0(tcp, state, val) 0
1589 typedef int arg_setup_state;
1591 # define arg_setup(tcp, state) (0)
1592 # define arg_finish_change(tcp, state) 0
1593 # define get_arg0(tcp, cookie, valp) \
1594 (upeek ((tcp), arg0_offset, (valp)))
1595 # define get_arg1(tcp, cookie, valp) \
1596 (upeek ((tcp), arg1_offset, (valp)))
1599 set_arg0 (struct tcb *tcp, void *cookie, long val)
1601 return ptrace (PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val);
1605 set_arg1 (struct tcb *tcp, void *cookie, long val)
1607 return ptrace (PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val);
1610 # endif /* architectures */
1612 # ifndef restore_arg0
1613 # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1615 # ifndef restore_arg1
1616 # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1620 # define arg0_index 0
1621 # define arg1_index 1
1625 setbpt(struct tcb *tcp)
1627 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
1628 arg_setup_state state;
1630 if (tcp->flags & TCB_BPTSET) {
1631 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1636 * It's a silly kludge to initialize this with a search at runtime.
1637 * But it's better than maintaining another magic thing in the
1638 * godforsaken tables.
1640 if (clone_scno[current_personality] == 0) {
1642 for (i = 0; i < nsyscalls; ++i)
1643 if (sysent[i].sys_func == sys_clone) {
1644 clone_scno[current_personality] = i;
1649 switch (known_scno(tcp)) {
1656 # if defined SYS_fork || defined SYS_vfork
1657 if (arg_setup (tcp, &state) < 0
1658 || get_arg0 (tcp, &state, &tcp->inst[0]) < 0
1659 || get_arg1 (tcp, &state, &tcp->inst[1]) < 0
1660 || change_syscall(tcp, clone_scno[current_personality]) < 0
1661 || set_arg0 (tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1662 || set_arg1 (tcp, &state, 0) < 0
1663 || arg_finish_change (tcp, &state) < 0)
1665 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1666 tcp->u_arg[arg1_index] = 0;
1667 tcp->flags |= TCB_BPTSET;
1675 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1676 contrary to x86 SYS_vfork above. Even on x86 we turn the
1677 vfork semantics into plain fork - each application must not
1678 depend on the vfork specifics according to POSIX. We would
1679 hang waiting for the parent resume otherwise. We need to
1680 clear also CLONE_VM but only in the CLONE_VFORK case as
1681 otherwise we would break pthread_create. */
1683 if ((arg_setup (tcp, &state) < 0
1684 || set_arg0 (tcp, &state,
1685 (tcp->u_arg[arg0_index] | CLONE_PTRACE)
1686 & ~(tcp->u_arg[arg0_index] & CLONE_VFORK
1687 ? CLONE_VFORK | CLONE_VM : 0)) < 0
1688 || arg_finish_change (tcp, &state) < 0))
1690 tcp->flags |= TCB_BPTSET;
1691 tcp->inst[0] = tcp->u_arg[arg0_index];
1692 tcp->inst[1] = tcp->u_arg[arg1_index];
1696 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1697 tcp->scno, tcp->pid);
1708 arg_setup_state state;
1709 if (arg_setup (tcp, &state) < 0
1710 || restore_arg0 (tcp, &state, tcp->inst[0]) < 0
1711 || restore_arg1 (tcp, &state, tcp->inst[1]) < 0
1712 || arg_finish_change (tcp, &state))
1713 if (errno != ESRCH) return -1;
1714 tcp->flags &= ~TCB_BPTSET;
1718 # else /* !defined LINUX */
1725 DEAD CODE HERE? WE ARE IN 'else !defined LINUX'
1726 # if defined (SPARC) || defined (SPARC64)
1727 /* We simply use the SunOS breakpoint code. */
1731 # define LOOPA 0x30800000 /* ba,a 0 */
1733 if (tcp->flags & TCB_BPTSET) {
1734 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1737 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1738 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1741 tcp->baddr = regs.r_o7 + 8;
1743 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)tcp->baddr, 0);
1745 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1750 * XXX - BRUTAL MODE ON
1751 * We cannot set a real BPT in the child, since it will not be
1752 * traced at the moment it will reach the trap and would probably
1753 * die with a core dump.
1754 * Thus, we are force our way in by taking out two instructions
1755 * and insert an eternal loop instead, in expectance of the SIGSTOP
1756 * generated by our PTRACE_ATTACH.
1757 * Of cause, if we evaporate ourselves in the middle of all this...
1761 # if defined (SPARC64)
1763 inst |= (tcp->inst[0] & 0xffffffffUL);
1765 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, inst);
1767 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1770 tcp->flags |= TCB_BPTSET;
1772 # else /* !SPARC && !SPARC64 */
1775 # define LOOP 0x0000feeb
1776 if (tcp->flags & TCB_BPTSET) {
1777 fprintf(stderr, "PANIC: bpt already set in pid %u\n",
1781 if (upeek(tcp, PT_CR_IIP, &tcp->baddr) < 0)
1784 fprintf(stderr, "[%d] setting bpt at %lx\n",
1785 tcp->pid, tcp->baddr);
1786 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid,
1787 (char *) tcp->baddr, 0);
1789 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1792 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP);
1794 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1797 tcp->flags |= TCB_BPTSET;
1800 * Our strategy here is to replace the bundle that
1801 * contained the clone() syscall with a bundle of the
1804 * { 1: br 1b; br 1b; br 1b }
1806 * This ensures that the newly forked child will loop
1807 * endlessly until we've got a chance to attach to it.
1809 # define LOOP0 0x0000100000000017
1810 # define LOOP1 0x4000000000200000
1811 unsigned long addr, ipsr;
1815 if (upeek(tcp, PT_CR_IPSR, &ipsr) < 0)
1817 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
1819 /* store "ri" in low two bits */
1820 tcp->baddr = addr | ((ipsr >> 41) & 0x3);
1823 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 0,
1825 tcp->inst[1] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 8,
1828 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1833 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, LOOP0);
1834 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, LOOP1);
1836 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1839 tcp->flags |= TCB_BPTSET;
1843 # if defined (I386) || defined(X86_64)
1844 # define LOOP 0x0000feeb
1845 # elif defined (M68K)
1846 # define LOOP 0x60fe0000
1847 # elif defined (ALPHA)
1848 # define LOOP 0xc3ffffff
1849 # elif defined (POWERPC)
1850 # define LOOP 0x48000000
1852 # define LOOP 0xEAFFFFFE
1853 # elif defined(MIPS)
1854 # define LOOP 0x1000ffff
1855 # elif defined(S390)
1856 # define LOOP 0xa7f40000 /* BRC 15,0 */
1857 # elif defined(S390X)
1858 # define LOOP 0xa7f4000000000000UL /* BRC 15,0 */
1859 # elif defined(HPPA)
1860 # define LOOP 0xe81f1ff7 /* b,l,n <loc>,r0 */
1862 # ifdef __LITTLE_ENDIAN__
1863 # define LOOP 0x0000affe
1865 # define LOOP 0xfeaf0000
1868 # error unknown architecture
1871 if (tcp->flags & TCB_BPTSET) {
1872 fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid);
1876 if (upeek(tcp, 4*EIP, &tcp->baddr) < 0)
1878 # elif defined (X86_64)
1879 if (upeek(tcp, 8*RIP, &tcp->baddr) < 0)
1881 # elif defined (M68K)
1882 if (upeek(tcp, 4*PT_PC, &tcp->baddr) < 0)
1884 # elif defined (ALPHA)
1886 # elif defined (ARM)
1888 # elif defined (MIPS)
1889 return -1; /* FIXME: I do not know what i do - Flo */
1890 # elif defined (POWERPC)
1891 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &tcp->baddr) < 0)
1893 # elif defined(S390) || defined(S390X)
1894 if (upeek(tcp,PT_PSWADDR, &tcp->baddr) < 0)
1896 # elif defined(HPPA)
1897 if (upeek(tcp, PT_IAOQ0, &tcp->baddr) < 0)
1899 tcp->baddr &= ~0x03;
1901 if (upeek(tcp, 4*REG_PC, &tcp->baddr) < 0)
1904 # error unknown architecture
1907 fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr);
1908 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *) tcp->baddr, 0);
1910 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1913 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP);
1915 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1918 tcp->flags |= TCB_BPTSET;
1921 # endif /* !SPARC && !SPARC64 */
1925 # ifdef SPARC /* This code is slightly sparc specific */
1928 # define BPT 0x91d02001 /* ta 1 */
1929 # define LOOP 0x10800000 /* ba 0 */
1930 # define LOOPA 0x30800000 /* ba,a 0 */
1931 # define NOP 0x01000000
1933 static int loopdeloop[1] = {LOOPA};
1935 static int loopdeloop[2] = {LOOP, NOP};
1938 if (tcp->flags & TCB_BPTSET) {
1939 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1942 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1943 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1946 tcp->baddr = regs.r_o7 + 8;
1947 if (ptrace(PTRACE_READTEXT, tcp->pid, (char *)tcp->baddr,
1948 sizeof tcp->inst, (char *)tcp->inst) < 0) {
1949 perror("setbpt: ptrace(PTRACE_READTEXT, ...)");
1954 * XXX - BRUTAL MODE ON
1955 * We cannot set a real BPT in the child, since it will not be
1956 * traced at the moment it will reach the trap and would probably
1957 * die with a core dump.
1958 * Thus, we are force our way in by taking out two instructions
1959 * and insert an eternal loop in stead, in expectance of the SIGSTOP
1960 * generated by out PTRACE_ATTACH.
1961 * Of cause, if we evaporate ourselves in the middle of all this...
1963 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1964 sizeof loopdeloop, (char *) loopdeloop) < 0) {
1965 perror("setbpt: ptrace(PTRACE_WRITETEXT, ...)");
1968 tcp->flags |= TCB_BPTSET;
1971 # endif /* SUNOS4 */
1982 DEAD CODE HERE? WE ARE IN 'else !defined LINUX'
1983 # if defined(I386) || defined(X86_64)
1985 # elif defined(POWERPC)
1987 # elif defined(M68K)
1989 # elif defined(ALPHA)
1991 # elif defined(HPPA)
1995 # endif /* architecture */
1997 # if defined (SPARC) || defined (SPARC64)
1998 /* Again, we borrow the SunOS breakpoint code. */
1999 if (!(tcp->flags & TCB_BPTSET)) {
2000 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2004 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
2006 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
2009 tcp->flags &= ~TCB_BPTSET;
2010 # elif defined(IA64)
2015 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
2016 if (!(tcp->flags & TCB_BPTSET)) {
2017 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2021 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
2023 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
2026 tcp->flags &= ~TCB_BPTSET;
2028 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
2030 if (addr != tcp->baddr) {
2031 /* The breakpoint has not been reached yet. */
2034 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2039 unsigned long addr, ipsr;
2044 if (upeek(tcp, PT_CR_IPSR, &ipsr) < 0)
2046 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
2049 /* restore original bundle: */
2051 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, tcp->inst[0]);
2052 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, tcp->inst[1]);
2054 perror("clearbpt: ptrace(PTRACE_POKETEXT, ...)");
2058 /* restore original "ri" in ipsr: */
2059 ipsr = (ipsr & ~(0x3ul << 41)) | ((tcp->baddr & 0x3) << 41);
2061 ptrace(PTRACE_POKEUSER, pid, (char *) PT_CR_IPSR, ipsr);
2063 perror("clrbpt: ptrace(PTRACE_POKEUSER, ...)");
2067 tcp->flags &= ~TCB_BPTSET;
2069 if (addr != (tcp->baddr & ~0x3)) {
2070 /* the breakpoint has not been reached yet. */
2072 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2077 # else /* !IA64 && !SPARC && !SPARC64 */
2080 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
2081 if (!(tcp->flags & TCB_BPTSET)) {
2082 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2086 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
2088 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
2091 tcp->flags &= ~TCB_BPTSET;
2094 if (upeek(tcp, 4*EIP, &eip) < 0)
2096 if (eip != tcp->baddr) {
2097 /* The breakpoint has not been reached yet. */
2100 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2104 # elif defined(X86_64)
2105 if (upeek(tcp, 8*RIP, &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(POWERPC)
2116 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0)
2118 if (pc != tcp->baddr) {
2119 /* The breakpoint has not been reached yet. */
2121 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2125 # elif defined(M68K)
2126 if (upeek(tcp, 4*PT_PC, &pc) < 0)
2128 if (pc != tcp->baddr) {
2129 /* The breakpoint has not been reached yet. */
2131 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2135 # elif defined(ALPHA)
2136 if (upeek(tcp, REG_PC, &pc) < 0)
2138 if (pc != tcp->baddr) {
2139 /* The breakpoint has not been reached yet. */
2141 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2145 # elif defined(HPPA)
2146 if (upeek(tcp, PT_IAOQ0, &iaoq) < 0)
2149 if (iaoq != tcp->baddr && iaoq != tcp->baddr + 4) {
2150 /* The breakpoint has not been reached yet. */
2152 fprintf(stderr, "NOTE: PC not at bpt (iaoq %#lx baddr %#lx)\n",
2156 iaoq = tcp->baddr | 3;
2157 /* We should be pointing at a 'ldi -1000,r1' in glibc, so it is
2158 * safe to set both IAOQ0 and IAOQ1 to that so the PSW N bit
2159 * has no significant effect.
2161 ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ0, iaoq);
2162 ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ1, iaoq);
2164 if (upeek(tcp, 4*REG_PC, &pc) < 0)
2166 if (pc != tcp->baddr) {
2167 /* The breakpoint has not been reached yet. */
2169 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2175 # endif /* !SPARC && !SPARC64 && !IA64 */
2185 if (!(tcp->flags & TCB_BPTSET)) {
2186 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2189 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
2190 sizeof tcp->inst, (char *) tcp->inst) < 0) {
2191 perror("clearbtp: ptrace(PTRACE_WRITETEXT, ...)");
2194 tcp->flags &= ~TCB_BPTSET;
2198 * Since we don't have a single instruction breakpoint, we may have
2199 * to adjust the program counter after removing our `breakpoint'.
2201 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
2202 perror("clearbpt: ptrace(PTRACE_GETREGS, ...)");
2205 if ((regs.r_pc < tcp->baddr) ||
2206 (regs.r_pc > tcp->baddr + 4)) {
2207 /* The breakpoint has not been reached yet */
2210 "NOTE: PC not at bpt (pc %#x baddr %#x)\n",
2211 regs.r_pc, tcp->baddr);
2214 if (regs.r_pc != tcp->baddr)
2216 fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n",
2217 regs.r_pc, tcp->baddr);
2219 regs.r_pc = tcp->baddr;
2220 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) {
2221 perror("clearbpt: ptrace(PTRACE_SETREGS, ...)");
2226 # endif /* SUNOS4 */
2231 # endif /* !defined LINUX */
2233 #endif /* !USE_PROCFS */
2245 for (n = 0; n < sizeof *hdr; n += 4) {
2247 if (upeek(tcp, uoff(u_exdata) + n, &res) < 0)
2249 memcpy(((char *) hdr) + n, &res, 4);
2252 fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n",
2253 hdr->a_magic, hdr->a_toolversion, hdr->a_machtype);
2254 fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n",
2255 hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry);
2266 * Change `vfork' in a freshly exec'ed dynamically linked
2267 * executable's (internal) symbol table to plain old `fork'
2271 struct link_dynamic dyn;
2272 struct link_dynamic_2 ld;
2275 if (getex(tcp, &hdr) < 0)
2280 if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) {
2281 fprintf(stderr, "Cannot read DYNAMIC\n");
2284 if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) {
2285 fprintf(stderr, "Cannot read link_dynamic_2\n");
2288 if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) {
2289 fprintf(stderr, "out of memory\n");
2292 if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
2293 (int)ld.ld_symb_size, strtab) < 0)
2297 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
2298 fprintf(stderr, "[symbol: %s]\n", cp);
2303 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
2304 if (strcmp(cp, "_vfork") == 0) {
2306 fprintf(stderr, "fixvfork: FOUND _vfork\n");
2307 strcpy(cp, "_fork");
2312 if (cp < strtab + ld.ld_symb_size)
2314 * Write entire symbol table back to avoid
2315 * memory alignment bugs in ptrace
2317 if (tload(pid, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
2318 (int)ld.ld_symb_size, strtab) < 0)