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);
258 * Interpret `xlat' as an array of flags
259 * print the entries whose bits are on in `flags'
260 * return # of flags printed.
263 addflags(xlat, flags)
264 const struct xlat *xlat;
269 for (n = 0; xlat->str; xlat++) {
270 if (xlat->val && (flags & xlat->val) == xlat->val) {
271 tprintf("|%s", xlat->str);
277 tprintf("|%#x", flags);
284 * Interpret `xlat' as an array of flags/
285 * Print to static string the entries whose bits are on in `flags'
286 * Return static string.
289 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
291 static char outstr[1024];
294 strcpy(outstr, prefix);
296 for (; xlat->str; xlat++) {
297 if ((flags & xlat->val) == xlat->val) {
300 strcat(outstr, xlat->str);
308 sprintf(outstr + strlen(outstr), "%#x", flags);
315 printflags(xlat, flags, dflt)
316 const struct xlat *xlat;
323 if (flags == 0 && xlat->val == 0) {
324 tprintf("%s", xlat->str);
329 for (n = 0; xlat->str; xlat++) {
330 if (xlat->val && (flags & xlat->val) == xlat->val) {
331 tprintf("%s%s", sep, xlat->str);
340 tprintf("%s%#x", sep, flags);
345 tprintf("%#x", flags);
347 tprintf(" /* %s */", dflt);
358 printnum(tcp, addr, fmt)
369 if (umove(tcp, addr, &num) < 0) {
370 tprintf("%#lx", addr);
379 printnum_int(tcp, addr, fmt)
390 if (umove(tcp, addr, &num) < 0) {
391 tprintf("%#lx", addr);
405 tprintf((uid == -1) ? "%ld" : "%lu", uid);
408 static char path[MAXPATHLEN + 1];
411 * Quote string `instr' of length `size'
412 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
413 * If `len' < 0, treat `instr' as a NUL-terminated string
414 * and quote at most (`size' - 1) bytes.
417 string_quote(const char *instr, char *outstr, int len, int size)
419 const unsigned char *ustr = (const unsigned char *) instr;
421 int usehex = 0, c, i;
426 /* Check for presence of symbol which require
427 to hex-quote the whole string. */
428 for (i = 0; i < size; ++i) {
430 /* Check for NUL-terminated string. */
434 /* Quote at most size - 1 bytes. */
438 if (!isprint(c) && !isspace(c)) {
448 /* Hex-quote the whole string. */
449 for (i = 0; i < size; ++i) {
451 /* Check for NUL-terminated string. */
455 /* Quote at most size - 1 bytes. */
459 sprintf(s, "\\x%02x", c);
463 for (i = 0; i < size; ++i) {
465 /* Check for NUL-terminated string. */
469 /* Quote at most size - 1 bytes. */
474 case '\"': case '\\':
501 else if (i + 1 < size
502 && isdigit(ustr[i + 1])) {
503 sprintf(s, "\\%03o", c);
506 sprintf(s, "\\%o", c);
517 /* Return nonzero if the string was unterminated. */
522 * Print path string specified by address `addr' and length `n'.
523 * If path length exceeds `n', append `...' to the output.
526 printpathn(struct tcb *tcp, long addr, int n)
533 /* Cap path length to the path buffer size,
534 and NUL-terminate the buffer. */
535 if (n > sizeof path - 1)
539 /* Fetch one byte more to find out whether path length > n. */
540 if (umovestr(tcp, addr, n + 1, path) < 0)
541 tprintf("%#lx", addr);
543 static char outstr[4*(sizeof path - 1) + sizeof "\"...\""];
544 int trunc = (path[n] != '\0');
548 (void) string_quote(path, outstr, -1, n + 1);
550 strcat(outstr, "...");
551 tprintf("%s", outstr);
556 printpath(struct tcb *tcp, long addr)
558 printpathn(tcp, addr, sizeof path - 1);
562 * Print string specified by address `addr' and length `len'.
563 * If `len' < 0, treat the string as a NUL-terminated string.
564 * If string length exceeds `max_strlen', append `...' to the output.
567 printstr(struct tcb *tcp, long addr, int len)
569 static char *str = NULL;
577 /* Allocate static buffers if they are not allocated yet. */
579 str = malloc(max_strlen + 1);
581 outstr = malloc(4 * max_strlen + sizeof "\"...\"");
582 if (!str || !outstr) {
583 fprintf(stderr, "out of memory\n");
584 tprintf("%#lx", addr);
590 * Treat as a NUL-terminated string: fetch one byte more
591 * because string_quote() quotes one byte less.
593 size = max_strlen + 1;
594 str[max_strlen] = '\0';
595 if (umovestr(tcp, addr, size, str) < 0) {
596 tprintf("%#lx", addr);
601 size = MIN(len, max_strlen);
602 if (umoven(tcp, addr, size, str) < 0) {
603 tprintf("%#lx", addr);
608 if (string_quote(str, outstr, len, size) &&
609 (len < 0 || len > max_strlen))
610 strcat(outstr, "...");
612 tprintf("%s", outstr);
617 dumpiov(tcp, len, addr)
622 #if defined(LINUX) && SUPPORTED_PERSONALITIES > 1
624 struct { u_int32_t base; u_int32_t len; } *iov32;
625 struct { u_int64_t base; u_int64_t len; } *iov64;
627 #define iov iovu.iov64
629 (personality_wordsize[current_personality] == 4 \
630 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
631 #define iov_iov_base(i) \
632 (personality_wordsize[current_personality] == 4 \
633 ? (u_int64_t) iovu.iov32[i].base : iovu.iov64[i].base)
634 #define iov_iov_len(i) \
635 (personality_wordsize[current_personality] == 4 \
636 ? (u_int64_t) iovu.iov32[i].len : iovu.iov64[i].len)
639 #define sizeof_iov sizeof(*iov)
640 #define iov_iov_base(i) iov[i].iov_base
641 #define iov_iov_len(i) iov[i].iov_len
646 size = sizeof_iov * (unsigned long) len;
647 if (size / sizeof_iov != len
648 || (iov = malloc(size)) == NULL) {
649 fprintf(stderr, "out of memory\n");
652 if (umoven(tcp, addr, size, (char *) iov) >= 0) {
653 for (i = 0; i < len; i++) {
654 /* include the buffer number to make it easy to
655 * match up the trace with the source */
656 tprintf(" * %lu bytes in buffer %d\n",
657 (unsigned long)iov_iov_len(i), i);
658 dumpstr(tcp, (long) iov_iov_base(i),
671 dumpstr(tcp, addr, len)
676 static int strsize = -1;
677 static unsigned char *str;
678 static char outstr[80];
685 if ((str = malloc(len)) == NULL) {
686 fprintf(stderr, "out of memory\n");
692 if (umoven(tcp, addr, len, (char *) str) < 0)
695 for (i = 0; i < len; i += 16) {
697 sprintf(s, " | %05x ", i);
699 for (j = 0; j < 16; j++) {
703 sprintf(s, " %02x", str[i + j]);
707 *s++ = ' '; *s++ = ' '; *s++ = ' ';
710 *s++ = ' '; *s++ = ' ';
711 for (j = 0; j < 16; j++) {
715 if (isprint(str[i + j]))
723 tprintf("%s |\n", outstr);
727 #define PAGMASK (~(PAGSIZ - 1))
729 * move `len' bytes of data from process `pid'
730 * at address `addr' to our space at `laddr'
733 umoven(struct tcb *tcp, long addr, int len, char *laddr)
741 char x[sizeof(long)];
744 if (addr & (sizeof(long) - 1)) {
745 /* addr not a multiple of sizeof(long) */
746 n = addr - (addr & -sizeof(long)); /* residue */
747 addr &= -sizeof(long); /* residue */
749 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
751 if (started && (errno==EPERM || errno==EIO)) {
752 /* Ran into 'end of memory' - stupid "printpath" */
755 /* But if not started, we had a bogus address. */
756 if (addr != 0 && errno != EIO && errno != ESRCH)
757 perror("ptrace: umoven");
761 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
762 addr += sizeof(long), laddr += m, len -= m;
766 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
768 if (started && (errno==EPERM || errno==EIO)) {
769 /* Ran into 'end of memory' - stupid "printpath" */
772 if (addr != 0 && errno != EIO && errno != ESRCH)
773 perror("ptrace: umoven");
777 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
778 addr += sizeof(long), laddr += m, len -= m;
788 char x[sizeof(long)];
791 if (addr & (sizeof(long) - 1)) {
792 /* addr not a multiple of sizeof(long) */
793 n = addr - (addr & -sizeof(long)); /* residue */
794 addr &= -sizeof(long); /* residue */
796 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
802 memcpy(laddr, &u.x[n], m = MIN(sizeof(long) - n, len));
803 addr += sizeof(long), laddr += m, len -= m;
807 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
813 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
814 addr += sizeof(long), laddr += m, len -= m;
820 n = MIN(len, PAGSIZ);
821 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
822 if (ptrace(PTRACE_READDATA, pid,
823 (char *) addr, len, laddr) < 0) {
824 if (errno != ESRCH) {
825 perror("umoven: ptrace(PTRACE_READDATA, ...)");
838 #ifdef HAVE_MP_PROCFS
839 int fd = tcp->pfd_as;
843 lseek(fd, addr, SEEK_SET);
844 if (read(fd, laddr, len) == -1)
846 #endif /* USE_PROCFS */
852 * like `umove' but make the additional effort of looking
853 * for a terminating zero byte.
856 umovestr(struct tcb *tcp, long addr, int len, char *laddr)
859 #ifdef HAVE_MP_PROCFS
860 int fd = tcp->pfd_as;
864 /* Some systems (e.g. FreeBSD) can be upset if we read off the
865 end of valid memory, avoid this by trying to read up
866 to page boundaries. But we don't know what a page is (and
867 getpagesize(2) (if it exists) doesn't necessarily return
868 hardware page size). Assume all pages >= 1024 (a-historical
871 int page = 1024; /* How to find this? */
872 int move = page - (addr & (page - 1));
875 lseek(fd, addr, SEEK_SET);
878 if (move > left) move = left;
879 if ((move = read(fd, laddr, move)) <= 0)
880 return left != len ? 0 : -1;
881 if (memchr (laddr, 0, move)) break;
887 #else /* !USE_PROCFS */
893 char x[sizeof(long)];
896 if (addr & (sizeof(long) - 1)) {
897 /* addr not a multiple of sizeof(long) */
898 n = addr - (addr & -sizeof(long)); /* residue */
899 addr &= -sizeof(long); /* residue */
901 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
903 if (started && (errno==EPERM || errno==EIO)) {
904 /* Ran into 'end of memory' - stupid "printpath" */
907 if (addr != 0 && errno != EIO && errno != ESRCH)
912 memcpy(laddr, &u.x[n], m = MIN(sizeof(long)-n,len));
913 while (n & (sizeof(long) - 1))
914 if (u.x[n++] == '\0')
916 addr += sizeof(long), laddr += m, len -= m;
920 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
922 if (started && (errno==EPERM || errno==EIO)) {
923 /* Ran into 'end of memory' - stupid "printpath" */
926 if (addr != 0 && errno != EIO && errno != ESRCH)
931 memcpy(laddr, u.x, m = MIN(sizeof(long), len));
932 for (i = 0; i < sizeof(long); i++)
936 addr += sizeof(long), laddr += m, len -= m;
938 #endif /* !USE_PROCFS */
943 # if !defined (SPARC) && !defined(SPARC64)
944 # define PTRACE_WRITETEXT 101
945 # define PTRACE_WRITEDATA 102
946 # endif /* !SPARC && !SPARC64 */
952 uload(cmd, pid, addr, len, laddr)
963 n = MIN(len, PAGSIZ);
964 n = MIN(n, ((addr + PAGSIZ) & PAGMASK) - addr);
965 if (ptrace(cmd, pid, (char *)addr, n, laddr) < 0) {
966 perror("uload: ptrace(PTRACE_WRITE, ...)");
978 char x[sizeof(long)];
981 if (cmd == PTRACE_WRITETEXT) {
982 peek = PTRACE_PEEKTEXT;
983 poke = PTRACE_POKETEXT;
986 peek = PTRACE_PEEKDATA;
987 poke = PTRACE_POKEDATA;
989 if (addr & (sizeof(long) - 1)) {
990 /* addr not a multiple of sizeof(long) */
991 n = addr - (addr & -sizeof(long)); /* residue */
992 addr &= -sizeof(long);
994 u.val = ptrace(peek, pid, (char *) addr, 0);
996 perror("uload: POKE");
999 memcpy(&u.x[n], laddr, m = MIN(sizeof(long) - n, len));
1000 if (ptrace(poke, pid, (char *)addr, u.val) < 0) {
1001 perror("uload: POKE");
1004 addr += sizeof(long), laddr += m, len -= m;
1007 if (len < sizeof(long))
1008 u.val = ptrace(peek, pid, (char *) addr, 0);
1009 memcpy(u.x, laddr, m = MIN(sizeof(long), len));
1010 if (ptrace(poke, pid, (char *) addr, u.val) < 0) {
1011 perror("uload: POKE");
1014 addr += sizeof(long), laddr += m, len -= m;
1021 tload(pid, addr, len, laddr)
1026 return uload(PTRACE_WRITETEXT, pid, addr, len, laddr);
1030 dload(pid, addr, len, laddr)
1036 return uload(PTRACE_WRITEDATA, pid, addr, len, laddr);
1044 upeek(tcp, off, res)
1051 # ifdef SUNOS4_KERNEL_ARCH_KLUDGE
1053 static int is_sun4m = -1;
1054 struct utsname name;
1056 /* Round up the usual suspects. */
1057 if (is_sun4m == -1) {
1058 if (uname(&name) < 0) {
1059 perror("upeek: uname?");
1062 is_sun4m = strcmp(name.machine, "sun4m") == 0;
1064 const struct xlat *x;
1066 for (x = struct_user_offsets; x->str; x++)
1073 # endif /* SUNOS4_KERNEL_ARCH_KLUDGE */
1075 val = do_ptrace(PTRACE_PEEKUSER, tcp, (char *) off, 0);
1076 if (val == -1 && errno) {
1077 if (errno != ESRCH) {
1079 sprintf(buf,"upeek: ptrace(PTRACE_PEEKUSER,%d,%lu,0)", tcp->pid, off);
1088 #endif /* !USE_PROCFS */
1092 getpc(struct tcb *tcp)
1098 if (upeek(tcp, 4*EIP, &pc) < 0)
1100 # elif defined(X86_64)
1101 if (upeek(tcp, 8*RIP, &pc) < 0)
1103 # elif defined(IA64)
1104 if (upeek(tcp, PT_B0, &pc) < 0)
1107 if (upeek(tcp, 4*15, &pc) < 0)
1109 # elif defined(AVR32)
1110 if (upeek(tcp, REG_PC, &pc) < 0)
1112 # elif defined(BFIN)
1113 if (upeek(tcp, REG_PC, &pc) < 0)
1115 # elif defined(POWERPC)
1116 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0)
1118 # elif defined(M68K)
1119 if (upeek(tcp, 4*PT_PC, &pc) < 0)
1121 # elif defined(ALPHA)
1122 if (upeek(tcp, REG_PC, &pc) < 0)
1124 # elif defined(MIPS)
1125 if (upeek(tcp, REG_EPC, &pc) < 0)
1127 # elif defined(SPARC) || defined(SPARC64)
1128 struct pt_regs regs;
1129 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0)
1131 # if defined(SPARC64)
1136 # elif defined(S390) || defined(S390X)
1137 if(upeek(tcp,PT_PSWADDR,&pc) < 0)
1139 # elif defined(HPPA)
1140 if(upeek(tcp,PT_IAOQ0,&pc) < 0)
1143 if (upeek(tcp, 4*REG_PC ,&pc) < 0)
1145 # elif defined(SH64)
1146 if (upeek(tcp, REG_PC ,&pc) < 0)
1154 * Return current program counter for `pid'
1155 * Assumes PC is never 0xffffffff
1159 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
1160 perror("getpc: ptrace(PTRACE_GETREGS, ...)");
1173 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
1175 #endif /* FREEBSD */
1180 printcall(struct tcb *tcp)
1182 #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
1183 sizeof(long) == 8 ? "[????????????????] " : \
1190 if (upeek(tcp, 4*EIP, &eip) < 0) {
1194 tprintf("[%08lx] ", eip);
1196 # elif defined(S390) || defined(S390X)
1198 if(upeek(tcp,PT_PSWADDR,&psw) < 0) {
1203 tprintf("[%08lx] ", psw);
1205 tprintf("[%16lx] ", psw);
1208 # elif defined(X86_64)
1211 if (upeek(tcp, 8*RIP, &rip) < 0) {
1215 tprintf("[%16lx] ", rip);
1216 # elif defined(IA64)
1219 if (upeek(tcp, PT_B0, &ip) < 0) {
1223 tprintf("[%08lx] ", ip);
1224 # elif defined(POWERPC)
1227 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
1228 tprintf ("[????????] ");
1231 tprintf("[%08lx] ", pc);
1232 # elif defined(M68K)
1235 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
1236 tprintf ("[????????] ");
1239 tprintf("[%08lx] ", pc);
1240 # elif defined(ALPHA)
1243 if (upeek(tcp, REG_PC, &pc) < 0) {
1244 tprintf ("[????????????????] ");
1247 tprintf("[%08lx] ", pc);
1248 # elif defined(SPARC) || defined(SPARC64)
1249 struct pt_regs regs;
1250 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) {
1254 # if defined(SPARC64)
1255 tprintf("[%08lx] ", regs.tpc);
1257 tprintf("[%08lx] ", regs.pc);
1259 # elif defined(HPPA)
1262 if(upeek(tcp,PT_IAOQ0,&pc) < 0) {
1263 tprintf ("[????????] ");
1266 tprintf("[%08lx] ", pc);
1267 # elif defined(MIPS)
1270 if (upeek(tcp, REG_EPC, &pc) < 0) {
1271 tprintf ("[????????] ");
1274 tprintf("[%08lx] ", pc);
1278 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
1279 tprintf ("[????????] ");
1282 tprintf("[%08lx] ", pc);
1283 # elif defined(SH64)
1286 if (upeek(tcp, REG_PC, &pc) < 0) {
1287 tprintf ("[????????????????] ");
1290 tprintf("[%08lx] ", pc);
1294 if (upeek(tcp, 4*15, &pc) < 0) {
1298 tprintf("[%08lx] ", pc);
1299 # elif defined(AVR32)
1302 if (upeek(tcp, REG_PC, &pc) < 0) {
1303 tprintf("[????????] ");
1306 tprintf("[%08lx] ", pc);
1307 # elif defined(BFIN)
1310 if (upeek(tcp, PT_PC, &pc) < 0) {
1314 tprintf("[%08lx] ", pc);
1315 #elif defined(CRISV10)
1318 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
1322 tprintf("[%08lx] ", pc);
1323 #elif defined(CRISV32)
1326 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
1330 tprintf("[%08lx] ", pc);
1331 # endif /* architecture */
1337 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *) ®s, 0) < 0) {
1338 perror("printcall: ptrace(PTRACE_GETREGS, ...)");
1342 tprintf("[%08x] ", regs.r_o7);
1352 pread(tcp->pfd_reg, ®s, sizeof(regs), 0);
1353 tprintf("[%08x] ", regs.r_eip);
1354 #endif /* FREEBSD */
1359 * These #if's are huge, please indent them correctly.
1360 * It's easy to get confused otherwise.
1366 # include "syscall.h"
1368 # include <sys/syscall.h>
1369 # ifndef CLONE_PTRACE
1370 # define CLONE_PTRACE 0x00002000
1372 # ifndef CLONE_VFORK
1373 # define CLONE_VFORK 0x00004000
1376 # define CLONE_VM 0x00000100
1378 # ifndef CLONE_STOPPED
1379 # define CLONE_STOPPED 0x02000000
1384 /* We don't have fork()/vfork() syscalls on ia64 itself, but the ia32
1385 subsystem has them for x86... */
1387 # define SYS_vfork 190
1389 typedef unsigned long *arg_setup_state;
1392 arg_setup(struct tcb *tcp, arg_setup_state *state)
1394 unsigned long cfm, sof, sol;
1398 /* Satisfy a false GCC warning. */
1403 if (upeek(tcp, PT_AR_BSP, &bsp) < 0)
1405 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1408 sof = (cfm >> 0) & 0x7f;
1409 sol = (cfm >> 7) & 0x7f;
1410 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
1412 *state = (unsigned long *) bsp;
1416 # define arg_finish_change(tcp, state) 0
1420 get_arg0 (struct tcb *tcp, arg_setup_state *state, long *valp)
1425 ret = upeek (tcp, PT_R11, valp);
1428 (unsigned long) ia64_rse_skip_regs(*state, 0),
1429 sizeof(long), (void *) valp);
1434 get_arg1 (struct tcb *tcp, arg_setup_state *state, long *valp)
1439 ret = upeek (tcp, PT_R9, valp);
1442 (unsigned long) ia64_rse_skip_regs(*state, 1),
1443 sizeof(long), (void *) valp);
1449 set_arg0 (struct tcb *tcp, arg_setup_state *state, long val)
1451 int req = PTRACE_POKEDATA;
1455 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1456 req = PTRACE_POKEUSER;
1458 ap = ia64_rse_skip_regs(*state, 0);
1460 ptrace(req, tcp->pid, ap, val);
1461 return errno ? -1 : 0;
1465 set_arg1 (struct tcb *tcp, arg_setup_state *state, long val)
1467 int req = PTRACE_POKEDATA;
1471 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1472 req = PTRACE_POKEUSER;
1474 ap = ia64_rse_skip_regs(*state, 1);
1476 ptrace(req, tcp->pid, ap, val);
1477 return errno ? -1 : 0;
1480 /* ia64 does not return the input arguments from functions (and syscalls)
1481 according to ia64 RSE (Register Stack Engine) behavior. */
1483 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1484 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1486 # elif defined (SPARC) || defined (SPARC64)
1488 typedef struct pt_regs arg_setup_state;
1490 # define arg_setup(tcp, state) \
1491 (ptrace (PTRACE_GETREGS, tcp->pid, (char *) (state), 0))
1492 # define arg_finish_change(tcp, state) \
1493 (ptrace (PTRACE_SETREGS, tcp->pid, (char *) (state), 0))
1495 # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0)
1496 # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0)
1497 # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0)
1498 # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0)
1499 # define restore_arg0(tcp, state, val) 0
1501 # else /* other architectures */
1503 # if defined S390 || defined S390X
1504 /* Note: this is only true for the `clone' system call, which handles
1505 arguments specially. We could as well say that its first two arguments
1506 are swapped relative to other architectures, but that would just be
1507 another #ifdef in the calls. */
1508 # define arg0_offset PT_GPR3
1509 # define arg1_offset PT_ORIGGPR2
1510 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1511 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1512 # define arg0_index 1
1513 # define arg1_index 0
1514 # elif defined (ALPHA) || defined (MIPS)
1515 # define arg0_offset REG_A0
1516 # define arg1_offset (REG_A0+1)
1517 # elif defined (AVR32)
1518 # define arg0_offset (REG_R12)
1519 # define arg1_offset (REG_R11)
1520 # elif defined (POWERPC)
1521 # define arg0_offset (sizeof(unsigned long)*PT_R3)
1522 # define arg1_offset (sizeof(unsigned long)*PT_R4)
1523 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1524 # elif defined (HPPA)
1525 # define arg0_offset PT_GR26
1526 # define arg1_offset (PT_GR26-4)
1527 # elif defined (X86_64)
1528 # define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1529 # define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1531 # define arg0_offset (4*(REG_REG0+4))
1532 # define arg1_offset (4*(REG_REG0+5))
1533 # elif defined (SH64)
1534 /* ABI defines arg0 & 1 in r2 & r3 */
1535 # define arg0_offset (REG_OFFSET+16)
1536 # define arg1_offset (REG_OFFSET+24)
1537 # define restore_arg0(tcp, state, val) 0
1538 # elif defined CRISV10 || defined CRISV32
1539 # define arg0_offset (4*PT_R11)
1540 # define arg1_offset (4*PT_ORIG_R10)
1541 # define restore_arg0(tcp, state, val) 0
1542 # define restore_arg1(tcp, state, val) 0
1543 # define arg0_index 1
1544 # define arg1_index 0
1546 # define arg0_offset 0
1547 # define arg1_offset 4
1549 # define restore_arg0(tcp, state, val) 0
1553 typedef int arg_setup_state;
1555 # define arg_setup(tcp, state) (0)
1556 # define arg_finish_change(tcp, state) 0
1557 # define get_arg0(tcp, cookie, valp) \
1558 (upeek ((tcp), arg0_offset, (valp)))
1559 # define get_arg1(tcp, cookie, valp) \
1560 (upeek ((tcp), arg1_offset, (valp)))
1563 set_arg0 (struct tcb *tcp, void *cookie, long val)
1565 return ptrace (PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val);
1569 set_arg1 (struct tcb *tcp, void *cookie, long val)
1571 return ptrace (PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val);
1574 # endif /* architectures */
1576 # ifndef restore_arg0
1577 # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1579 # ifndef restore_arg1
1580 # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1584 # define arg0_index 0
1585 # define arg1_index 1
1589 setbpt(struct tcb *tcp)
1591 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
1592 arg_setup_state state;
1594 if (tcp->flags & TCB_BPTSET) {
1595 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1600 * It's a silly kludge to initialize this with a search at runtime.
1601 * But it's better than maintaining another magic thing in the
1602 * godforsaken tables.
1604 if (clone_scno[current_personality] == 0) {
1606 for (i = 0; i < nsyscalls; ++i)
1607 if (sysent[i].sys_func == sys_clone) {
1608 clone_scno[current_personality] = i;
1613 switch (known_scno(tcp)) {
1620 # if defined SYS_fork || defined SYS_vfork
1621 if (arg_setup (tcp, &state) < 0
1622 || get_arg0 (tcp, &state, &tcp->inst[0]) < 0
1623 || get_arg1 (tcp, &state, &tcp->inst[1]) < 0
1624 || change_syscall(tcp, clone_scno[current_personality]) < 0
1625 || set_arg0 (tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1626 || set_arg1 (tcp, &state, 0) < 0
1627 || arg_finish_change (tcp, &state) < 0)
1629 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1630 tcp->u_arg[arg1_index] = 0;
1631 tcp->flags |= TCB_BPTSET;
1639 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1640 contrary to x86 SYS_vfork above. Even on x86 we turn the
1641 vfork semantics into plain fork - each application must not
1642 depend on the vfork specifics according to POSIX. We would
1643 hang waiting for the parent resume otherwise. We need to
1644 clear also CLONE_VM but only in the CLONE_VFORK case as
1645 otherwise we would break pthread_create. */
1647 if ((arg_setup (tcp, &state) < 0
1648 || set_arg0 (tcp, &state,
1649 (tcp->u_arg[arg0_index] | CLONE_PTRACE)
1650 & ~(tcp->u_arg[arg0_index] & CLONE_VFORK
1651 ? CLONE_VFORK | CLONE_VM : 0)) < 0
1652 || arg_finish_change (tcp, &state) < 0))
1654 tcp->flags |= TCB_BPTSET;
1655 tcp->inst[0] = tcp->u_arg[arg0_index];
1656 tcp->inst[1] = tcp->u_arg[arg1_index];
1660 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1661 tcp->scno, tcp->pid);
1672 arg_setup_state state;
1673 if (arg_setup (tcp, &state) < 0
1674 || restore_arg0 (tcp, &state, tcp->inst[0]) < 0
1675 || restore_arg1 (tcp, &state, tcp->inst[1]) < 0
1676 || arg_finish_change (tcp, &state))
1678 tcp->flags &= ~TCB_BPTSET;
1682 # else /* !defined LINUX */
1689 DEAD CODE HERE? WE ARE IN 'else !defined LINUX'
1690 # if defined (SPARC) || defined (SPARC64)
1691 /* We simply use the SunOS breakpoint code. */
1695 # define LOOPA 0x30800000 /* ba,a 0 */
1697 if (tcp->flags & TCB_BPTSET) {
1698 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1701 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1702 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1705 tcp->baddr = regs.r_o7 + 8;
1707 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)tcp->baddr, 0);
1709 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1714 * XXX - BRUTAL MODE ON
1715 * We cannot set a real BPT in the child, since it will not be
1716 * traced at the moment it will reach the trap and would probably
1717 * die with a core dump.
1718 * Thus, we are force our way in by taking out two instructions
1719 * and insert an eternal loop instead, in expectance of the SIGSTOP
1720 * generated by our PTRACE_ATTACH.
1721 * Of cause, if we evaporate ourselves in the middle of all this...
1725 # if defined (SPARC64)
1727 inst |= (tcp->inst[0] & 0xffffffffUL);
1729 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, inst);
1731 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1734 tcp->flags |= TCB_BPTSET;
1736 # else /* !SPARC && !SPARC64 */
1739 # define LOOP 0x0000feeb
1740 if (tcp->flags & TCB_BPTSET) {
1741 fprintf(stderr, "PANIC: bpt already set in pid %u\n",
1745 if (upeek(tcp, PT_CR_IIP, &tcp->baddr) < 0)
1748 fprintf(stderr, "[%d] setting bpt at %lx\n",
1749 tcp->pid, tcp->baddr);
1750 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid,
1751 (char *) tcp->baddr, 0);
1753 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1756 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP);
1758 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1761 tcp->flags |= TCB_BPTSET;
1764 * Our strategy here is to replace the bundle that
1765 * contained the clone() syscall with a bundle of the
1768 * { 1: br 1b; br 1b; br 1b }
1770 * This ensures that the newly forked child will loop
1771 * endlessly until we've got a chance to attach to it.
1773 # define LOOP0 0x0000100000000017
1774 # define LOOP1 0x4000000000200000
1775 unsigned long addr, ipsr;
1779 if (upeek(tcp, PT_CR_IPSR, &ipsr) < 0)
1781 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
1783 /* store "ri" in low two bits */
1784 tcp->baddr = addr | ((ipsr >> 41) & 0x3);
1787 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 0,
1789 tcp->inst[1] = ptrace(PTRACE_PEEKTEXT, pid, (char *) addr + 8,
1792 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1797 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, LOOP0);
1798 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, LOOP1);
1800 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1803 tcp->flags |= TCB_BPTSET;
1807 # if defined (I386) || defined(X86_64)
1808 # define LOOP 0x0000feeb
1809 # elif defined (M68K)
1810 # define LOOP 0x60fe0000
1811 # elif defined (ALPHA)
1812 # define LOOP 0xc3ffffff
1813 # elif defined (POWERPC)
1814 # define LOOP 0x48000000
1816 # define LOOP 0xEAFFFFFE
1817 # elif defined(MIPS)
1818 # define LOOP 0x1000ffff
1819 # elif defined(S390)
1820 # define LOOP 0xa7f40000 /* BRC 15,0 */
1821 # elif defined(S390X)
1822 # define LOOP 0xa7f4000000000000UL /* BRC 15,0 */
1823 # elif defined(HPPA)
1824 # define LOOP 0xe81f1ff7 /* b,l,n <loc>,r0 */
1826 # ifdef __LITTLE_ENDIAN__
1827 # define LOOP 0x0000affe
1829 # define LOOP 0xfeaf0000
1832 # error unknown architecture
1835 if (tcp->flags & TCB_BPTSET) {
1836 fprintf(stderr, "PANIC: bpt already set in pid %u\n", tcp->pid);
1840 if (upeek(tcp, 4*EIP, &tcp->baddr) < 0)
1842 # elif defined (X86_64)
1843 if (upeek(tcp, 8*RIP, &tcp->baddr) < 0)
1845 # elif defined (M68K)
1846 if (upeek(tcp, 4*PT_PC, &tcp->baddr) < 0)
1848 # elif defined (ALPHA)
1850 # elif defined (ARM)
1852 # elif defined (MIPS)
1853 return -1; /* FIXME: I do not know what i do - Flo */
1854 # elif defined (POWERPC)
1855 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &tcp->baddr) < 0)
1857 # elif defined(S390) || defined(S390X)
1858 if (upeek(tcp,PT_PSWADDR, &tcp->baddr) < 0)
1860 # elif defined(HPPA)
1861 if (upeek(tcp, PT_IAOQ0, &tcp->baddr) < 0)
1863 tcp->baddr &= ~0x03;
1865 if (upeek(tcp, 4*REG_PC, &tcp->baddr) < 0)
1868 # error unknown architecture
1871 fprintf(stderr, "[%d] setting bpt at %lx\n", tcp->pid, tcp->baddr);
1872 tcp->inst[0] = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *) tcp->baddr, 0);
1874 perror("setbpt: ptrace(PTRACE_PEEKTEXT, ...)");
1877 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, LOOP);
1879 perror("setbpt: ptrace(PTRACE_POKETEXT, ...)");
1882 tcp->flags |= TCB_BPTSET;
1885 # endif /* !SPARC && !SPARC64 */
1889 # ifdef SPARC /* This code is slightly sparc specific */
1892 # define BPT 0x91d02001 /* ta 1 */
1893 # define LOOP 0x10800000 /* ba 0 */
1894 # define LOOPA 0x30800000 /* ba,a 0 */
1895 # define NOP 0x01000000
1897 static int loopdeloop[1] = {LOOPA};
1899 static int loopdeloop[2] = {LOOP, NOP};
1902 if (tcp->flags & TCB_BPTSET) {
1903 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1906 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1907 perror("setbpt: ptrace(PTRACE_GETREGS, ...)");
1910 tcp->baddr = regs.r_o7 + 8;
1911 if (ptrace(PTRACE_READTEXT, tcp->pid, (char *)tcp->baddr,
1912 sizeof tcp->inst, (char *)tcp->inst) < 0) {
1913 perror("setbpt: ptrace(PTRACE_READTEXT, ...)");
1918 * XXX - BRUTAL MODE ON
1919 * We cannot set a real BPT in the child, since it will not be
1920 * traced at the moment it will reach the trap and would probably
1921 * die with a core dump.
1922 * Thus, we are force our way in by taking out two instructions
1923 * and insert an eternal loop in stead, in expectance of the SIGSTOP
1924 * generated by out PTRACE_ATTACH.
1925 * Of cause, if we evaporate ourselves in the middle of all this...
1927 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
1928 sizeof loopdeloop, (char *) loopdeloop) < 0) {
1929 perror("setbpt: ptrace(PTRACE_WRITETEXT, ...)");
1932 tcp->flags |= TCB_BPTSET;
1935 # endif /* SUNOS4 */
1946 DEAD CODE HERE? WE ARE IN 'else !defined LINUX'
1947 # if defined(I386) || defined(X86_64)
1949 # elif defined(POWERPC)
1951 # elif defined(M68K)
1953 # elif defined(ALPHA)
1955 # elif defined(HPPA)
1959 # endif /* architecture */
1961 # if defined (SPARC) || defined (SPARC64)
1962 /* Again, we borrow the SunOS breakpoint code. */
1963 if (!(tcp->flags & TCB_BPTSET)) {
1964 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1968 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1970 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1973 tcp->flags &= ~TCB_BPTSET;
1974 # elif defined(IA64)
1979 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
1980 if (!(tcp->flags & TCB_BPTSET)) {
1981 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
1985 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
1987 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
1990 tcp->flags &= ~TCB_BPTSET;
1992 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
1994 if (addr != tcp->baddr) {
1995 /* The breakpoint has not been reached yet. */
1998 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2003 unsigned long addr, ipsr;
2008 if (upeek(tcp, PT_CR_IPSR, &ipsr) < 0)
2010 if (upeek(tcp, PT_CR_IIP, &addr) < 0)
2013 /* restore original bundle: */
2015 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 0, tcp->inst[0]);
2016 ptrace(PTRACE_POKETEXT, pid, (char *) addr + 8, tcp->inst[1]);
2018 perror("clearbpt: ptrace(PTRACE_POKETEXT, ...)");
2022 /* restore original "ri" in ipsr: */
2023 ipsr = (ipsr & ~(0x3ul << 41)) | ((tcp->baddr & 0x3) << 41);
2025 ptrace(PTRACE_POKEUSER, pid, (char *) PT_CR_IPSR, ipsr);
2027 perror("clrbpt: ptrace(PTRACE_POKEUSER, ...)");
2031 tcp->flags &= ~TCB_BPTSET;
2033 if (addr != (tcp->baddr & ~0x3)) {
2034 /* the breakpoint has not been reached yet. */
2036 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2041 # else /* !IA64 && !SPARC && !SPARC64 */
2044 fprintf(stderr, "[%d] clearing bpt\n", tcp->pid);
2045 if (!(tcp->flags & TCB_BPTSET)) {
2046 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2050 ptrace(PTRACE_POKETEXT, tcp->pid, (char *) tcp->baddr, tcp->inst[0]);
2052 perror("clearbtp: ptrace(PTRACE_POKETEXT, ...)");
2055 tcp->flags &= ~TCB_BPTSET;
2058 if (upeek(tcp, 4*EIP, &eip) < 0)
2060 if (eip != tcp->baddr) {
2061 /* The breakpoint has not been reached yet. */
2064 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2068 # elif defined(X86_64)
2069 if (upeek(tcp, 8*RIP, &eip) < 0)
2071 if (eip != tcp->baddr) {
2072 /* The breakpoint has not been reached yet. */
2075 "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2079 # elif defined(POWERPC)
2080 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0)
2082 if (pc != tcp->baddr) {
2083 /* The breakpoint has not been reached yet. */
2085 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2089 # elif defined(M68K)
2090 if (upeek(tcp, 4*PT_PC, &pc) < 0)
2092 if (pc != tcp->baddr) {
2093 /* The breakpoint has not been reached yet. */
2095 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2099 # elif defined(ALPHA)
2100 if (upeek(tcp, REG_PC, &pc) < 0)
2102 if (pc != tcp->baddr) {
2103 /* The breakpoint has not been reached yet. */
2105 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2109 # elif defined(HPPA)
2110 if (upeek(tcp, PT_IAOQ0, &iaoq) < 0)
2113 if (iaoq != tcp->baddr && iaoq != tcp->baddr + 4) {
2114 /* The breakpoint has not been reached yet. */
2116 fprintf(stderr, "NOTE: PC not at bpt (iaoq %#lx baddr %#lx)\n",
2120 iaoq = tcp->baddr | 3;
2121 /* We should be pointing at a 'ldi -1000,r1' in glibc, so it is
2122 * safe to set both IAOQ0 and IAOQ1 to that so the PSW N bit
2123 * has no significant effect.
2125 ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ0, iaoq);
2126 ptrace(PTRACE_POKEUSER, tcp->pid, (void *)PT_IAOQ1, iaoq);
2128 if (upeek(tcp, 4*REG_PC, &pc) < 0)
2130 if (pc != tcp->baddr) {
2131 /* The breakpoint has not been reached yet. */
2133 fprintf(stderr, "NOTE: PC not at bpt (pc %#lx baddr %#lx)\n",
2139 # endif /* !SPARC && !SPARC64 && !IA64 */
2149 if (!(tcp->flags & TCB_BPTSET)) {
2150 fprintf(stderr, "PANIC: TCB not set in pid %u\n", tcp->pid);
2153 if (ptrace(PTRACE_WRITETEXT, tcp->pid, (char *) tcp->baddr,
2154 sizeof tcp->inst, (char *) tcp->inst) < 0) {
2155 perror("clearbtp: ptrace(PTRACE_WRITETEXT, ...)");
2158 tcp->flags &= ~TCB_BPTSET;
2162 * Since we don't have a single instruction breakpoint, we may have
2163 * to adjust the program counter after removing our `breakpoint'.
2165 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
2166 perror("clearbpt: ptrace(PTRACE_GETREGS, ...)");
2169 if ((regs.r_pc < tcp->baddr) ||
2170 (regs.r_pc > tcp->baddr + 4)) {
2171 /* The breakpoint has not been reached yet */
2174 "NOTE: PC not at bpt (pc %#x baddr %#x)\n",
2175 regs.r_pc, tcp->baddr);
2178 if (regs.r_pc != tcp->baddr)
2180 fprintf(stderr, "NOTE: PC adjusted (%#x -> %#x\n",
2181 regs.r_pc, tcp->baddr);
2183 regs.r_pc = tcp->baddr;
2184 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) {
2185 perror("clearbpt: ptrace(PTRACE_SETREGS, ...)");
2190 # endif /* SUNOS4 */
2195 # endif /* !defined LINUX */
2197 #endif /* !USE_PROCFS */
2209 for (n = 0; n < sizeof *hdr; n += 4) {
2211 if (upeek(tcp, uoff(u_exdata) + n, &res) < 0)
2213 memcpy(((char *) hdr) + n, &res, 4);
2216 fprintf(stderr, "[struct exec: magic: %o version %u Mach %o\n",
2217 hdr->a_magic, hdr->a_toolversion, hdr->a_machtype);
2218 fprintf(stderr, "Text %lu Data %lu Bss %lu Syms %lu Entry %#lx]\n",
2219 hdr->a_text, hdr->a_data, hdr->a_bss, hdr->a_syms, hdr->a_entry);
2230 * Change `vfork' in a freshly exec'ed dynamically linked
2231 * executable's (internal) symbol table to plain old `fork'
2235 struct link_dynamic dyn;
2236 struct link_dynamic_2 ld;
2239 if (getex(tcp, &hdr) < 0)
2244 if (umove(tcp, (int) N_DATADDR(hdr), &dyn) < 0) {
2245 fprintf(stderr, "Cannot read DYNAMIC\n");
2248 if (umove(tcp, (int) dyn.ld_un.ld_2, &ld) < 0) {
2249 fprintf(stderr, "Cannot read link_dynamic_2\n");
2252 if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) {
2253 fprintf(stderr, "out of memory\n");
2256 if (umoven(tcp, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
2257 (int)ld.ld_symb_size, strtab) < 0)
2261 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
2262 fprintf(stderr, "[symbol: %s]\n", cp);
2267 for (cp = strtab; cp < strtab + ld.ld_symb_size; ) {
2268 if (strcmp(cp, "_vfork") == 0) {
2270 fprintf(stderr, "fixvfork: FOUND _vfork\n");
2271 strcpy(cp, "_fork");
2276 if (cp < strtab + ld.ld_symb_size)
2278 * Write entire symbol table back to avoid
2279 * memory alignment bugs in ptrace
2281 if (tload(pid, (int)ld.ld_symbols+(int)N_TXTADDR(hdr),
2282 (int)ld.ld_symb_size, strtab) < 0)