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 #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 1)
48 #include <linux/ptrace.h>
52 # include <asm/ptrace_offsets.h>
58 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
59 #elif defined(HAVE_LINUX_PTRACE_H)
60 # undef PTRACE_SYSCALL
61 # ifdef HAVE_STRUCT_IA64_FPREG
62 # define ia64_fpreg XXX_ia64_fpreg
64 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
65 # define pt_all_user_regs XXX_pt_all_user_regs
67 # include <linux/ptrace.h>
69 # undef pt_all_user_regs
73 # undef PTRACE_GETREGS
74 # define PTRACE_GETREGS PTRACE_GETREGS64
75 # undef PTRACE_SETREGS
76 # define PTRACE_SETREGS PTRACE_SETREGS64
81 # define MAX(a,b) (((a) > (b)) ? (a) : (b))
84 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
88 tv_nz(struct timeval *a)
90 return a->tv_sec || a->tv_usec;
94 tv_cmp(struct timeval *a, struct timeval *b)
96 if (a->tv_sec < b->tv_sec
97 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
99 if (a->tv_sec > b->tv_sec
100 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
106 tv_float(struct timeval *tv)
108 return tv->tv_sec + tv->tv_usec/1000000.0;
112 tv_add(struct timeval *tv, struct timeval *a, struct timeval *b)
114 tv->tv_sec = a->tv_sec + b->tv_sec;
115 tv->tv_usec = a->tv_usec + b->tv_usec;
116 if (tv->tv_usec >= 1000000) {
118 tv->tv_usec -= 1000000;
123 tv_sub(struct timeval *tv, struct timeval *a, struct timeval *b)
125 tv->tv_sec = a->tv_sec - b->tv_sec;
126 tv->tv_usec = a->tv_usec - b->tv_usec;
127 if (((long) tv->tv_usec) < 0) {
129 tv->tv_usec += 1000000;
134 tv_div(struct timeval *tv, struct timeval *a, int n)
136 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
137 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
138 tv->tv_usec %= 1000000;
142 tv_mul(struct timeval *tv, struct timeval *a, int n)
144 tv->tv_usec = a->tv_usec * n;
145 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
146 tv->tv_usec %= 1000000;
150 xlookup(const struct xlat *xlat, int val)
152 for (; xlat->str != NULL; xlat++)
153 if (xlat->val == val)
158 #if !defined HAVE_STPCPY
160 stpcpy(char *dst, const char *src)
162 while ((*dst = *src++) != '\0')
169 * Used when we want to unblock stopped traced process.
170 * Should be only used with PTRACE_CONT, PTRACE_DETACH and PTRACE_SYSCALL.
171 * Returns 0 on success or if error was ESRCH
172 * (presumably process was killed while we talk to it).
173 * Otherwise prints error message and returns -1.
176 ptrace_restart(int op, struct tcb *tcp, int sig)
182 ptrace(op, tcp->pid, (void *) 0, (long) sig);
184 if (!err || err == ESRCH)
187 tcp->ptrace_errno = err;
189 if (op == PTRACE_CONT)
191 if (op == PTRACE_DETACH)
194 if (op == PTRACE_LISTEN)
197 perror_msg("ptrace(PTRACE_%s,pid:%d,sig:%d)", msg, tcp->pid, sig);
202 * Print entry in struct xlat table, if there.
205 printxval(const struct xlat *xlat, int val, const char *dflt)
207 const char *str = xlookup(xlat, val);
212 tprintf("%#x /* %s */", val, dflt);
217 * Print 64bit argument at position llarg and return the index of the next
221 printllval(struct tcb *tcp, const char *format, int llarg)
223 # if defined(X86_64) || defined(POWERPC64)
224 if (current_personality == 0) {
225 tprintf(format, tcp->u_arg[llarg]);
229 /* Align 64bit argument to 64bit boundary. */
230 llarg = (llarg + 1) & 0x1e;
232 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
235 # elif defined IA64 || defined ALPHA
236 tprintf(format, tcp->u_arg[llarg]);
238 # elif defined LINUX_MIPSN32
239 tprintf(format, tcp->ext_arg[llarg]);
242 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
250 * Interpret `xlat' as an array of flags
251 * print the entries whose bits are on in `flags'
252 * return # of flags printed.
255 addflags(const struct xlat *xlat, int flags)
257 for (; xlat->str; xlat++) {
258 if (xlat->val && (flags & xlat->val) == xlat->val) {
259 tprintf("|%s", xlat->str);
264 tprintf("|%#x", flags);
269 * Interpret `xlat' as an array of flags.
270 * Print to static string the entries whose bits are on in `flags'
271 * Return static string.
274 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
276 static char outstr[1024];
280 outptr = stpcpy(outstr, prefix);
282 for (; xlat->str; xlat++) {
283 if ((flags & xlat->val) == xlat->val) {
286 outptr = stpcpy(outptr, xlat->str);
296 outptr += sprintf(outptr, "%#x", flags);
303 printflags(const struct xlat *xlat, int flags, const char *dflt)
308 if (flags == 0 && xlat->val == 0) {
314 for (n = 0; xlat->str; xlat++) {
315 if (xlat->val && (flags & xlat->val) == xlat->val) {
316 tprintf("%s%s", sep, xlat->str);
325 tprintf("%s%#x", sep, flags);
330 tprintf("%#x", flags);
332 tprintf(" /* %s */", dflt);
343 printnum(struct tcb *tcp, long addr, const char *fmt)
351 if (umove(tcp, addr, &num) < 0) {
352 tprintf("%#lx", addr);
361 printnum_int(struct tcb *tcp, long addr, const char *fmt)
369 if (umove(tcp, addr, &num) < 0) {
370 tprintf("%#lx", addr);
379 printfd(struct tcb *tcp, int fd)
383 if (show_fd_path && (p = getfdpath(tcp, fd)))
384 tprintf("%d<%s>", fd, p);
390 printuid(const char *text, unsigned long uid)
392 tprintf((uid == -1) ? "%s%ld" : "%s%lu", text, uid);
396 * Quote string `instr' of length `size'
397 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
398 * If `len' < 0, treat `instr' as a NUL-terminated string
399 * and quote at most (`size' - 1) bytes.
401 * Returns 0 if len < 0 and NUL was seen, 1 otherwise.
402 * Note that if len >= 0, always returns 1.
405 string_quote(const char *instr, char *outstr, int len, int size)
407 const unsigned char *ustr = (const unsigned char *) instr;
409 int usehex, c, i, eol;
411 eol = 0x100; /* this can never match a char */
421 /* Check for presence of symbol which require
422 to hex-quote the whole string. */
423 for (i = 0; i < size; ++i) {
425 /* Check for NUL-terminated string. */
428 if (!isprint(c) && !isspace(c)) {
438 /* Hex-quote the whole string. */
439 for (i = 0; i < size; ++i) {
441 /* Check for NUL-terminated string. */
446 *s++ = "0123456789abcdef"[c >> 4];
447 *s++ = "0123456789abcdef"[c & 0xf];
450 for (i = 0; i < size; ++i) {
452 /* Check for NUL-terminated string. */
456 case '\"': case '\\':
487 && ustr[i + 1] >= '0'
488 && ustr[i + 1] <= '9'
491 *s++ = '0' + (c >> 6);
492 *s++ = '0' + ((c >> 3) & 0x7);
497 *s++ = '0' + (c >> 6);
498 *s++ = '0' + ((c >> 3) & 0x7);
501 *s++ = '0' + (c & 0x7);
511 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
512 if (len < 0 && ustr[i] == '\0') {
513 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
514 * but next char is NUL.
524 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
529 * Print path string specified by address `addr' and length `n'.
530 * If path length exceeds `n', append `...' to the output.
533 printpathn(struct tcb *tcp, long addr, int n)
535 char path[MAXPATHLEN + 1];
543 /* Cap path length to the path buffer size */
544 if (n > sizeof path - 1)
547 /* Fetch one byte more to find out whether path length > n. */
548 nul_seen = umovestr(tcp, addr, n + 1, path);
550 tprintf("%#lx", addr);
556 outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */
557 string_quote(path, outstr, -1, n);
565 printpath(struct tcb *tcp, long addr)
567 /* Size must correspond to char path[] size in printpathn */
568 printpathn(tcp, addr, MAXPATHLEN);
572 * Print string specified by address `addr' and length `len'.
573 * If `len' < 0, treat the string as a NUL-terminated string.
574 * If string length exceeds `max_strlen', append `...' to the output.
577 printstr(struct tcb *tcp, long addr, int len)
579 static char *str = NULL;
588 /* Allocate static buffers if they are not allocated yet. */
590 str = malloc(max_strlen + 1);
593 outstr = malloc(4 * max_strlen + /*for quotes and NUL:*/ 3);
600 * Treat as a NUL-terminated string: fetch one byte more
601 * because string_quote() quotes one byte less.
603 size = max_strlen + 1;
604 if (umovestr(tcp, addr, size, str) < 0) {
605 tprintf("%#lx", addr);
610 size = MIN(len, max_strlen);
611 if (umoven(tcp, addr, size, str) < 0) {
612 tprintf("%#lx", addr);
617 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
618 * or we were requested to print more than -s NUM chars)...
620 ellipsis = (string_quote(str, outstr, len, size) &&
621 (len < 0 || len > max_strlen));
630 dumpiov(struct tcb *tcp, int len, long addr)
632 #if SUPPORTED_PERSONALITIES > 1
634 struct { u_int32_t base; u_int32_t len; } *iov32;
635 struct { u_int64_t base; u_int64_t len; } *iov64;
637 #define iov iovu.iov64
639 (personality_wordsize[current_personality] == 4 \
640 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
641 #define iov_iov_base(i) \
642 (personality_wordsize[current_personality] == 4 \
643 ? (u_int64_t) iovu.iov32[i].base : iovu.iov64[i].base)
644 #define iov_iov_len(i) \
645 (personality_wordsize[current_personality] == 4 \
646 ? (u_int64_t) iovu.iov32[i].len : iovu.iov64[i].len)
649 #define sizeof_iov sizeof(*iov)
650 #define iov_iov_base(i) iov[i].iov_base
651 #define iov_iov_len(i) iov[i].iov_len
656 size = sizeof_iov * len;
657 /* Assuming no sane program has millions of iovs */
658 if ((unsigned)len > 1024*1024 /* insane or negative size? */
659 || (iov = malloc(size)) == NULL) {
660 fprintf(stderr, "Out of memory\n");
663 if (umoven(tcp, addr, size, (char *) iov) >= 0) {
664 for (i = 0; i < len; i++) {
665 /* include the buffer number to make it easy to
666 * match up the trace with the source */
667 tprintf(" * %lu bytes in buffer %d\n",
668 (unsigned long)iov_iov_len(i), i);
669 dumpstr(tcp, (long) iov_iov_base(i),
682 dumpstr(struct tcb *tcp, long addr, int len)
684 static int strsize = -1;
685 static unsigned char *str;
694 fprintf(stderr, "Out of memory\n");
700 if (umoven(tcp, addr, len, (char *) str) < 0)
703 for (i = 0; i < len; i += 16) {
707 sprintf(s, " | %05x ", i);
709 for (j = 0; j < 16; j++) {
713 sprintf(s, " %02x", str[i + j]);
717 *s++ = ' '; *s++ = ' '; *s++ = ' ';
720 *s++ = ' '; *s++ = ' ';
721 for (j = 0; j < 16; j++) {
725 if (isprint(str[i + j]))
733 tprintf("%s |\n", outstr);
737 #ifdef HAVE_PROCESS_VM_READV
738 /* C library supports this, but the kernel might not. */
739 static bool process_vm_readv_not_supported = 0;
742 /* Need to do this since process_vm_readv() is not yet available in libc.
743 * When libc is be updated, only "static bool process_vm_readv_not_supported"
744 * line should remain.
746 #if !defined(__NR_process_vm_readv)
748 # define __NR_process_vm_readv 347
749 # elif defined(X86_64)
750 # define __NR_process_vm_readv 310
751 # elif defined(POWERPC)
752 # define __NR_process_vm_readv 351
756 #if defined(__NR_process_vm_readv)
757 static bool process_vm_readv_not_supported = 0;
758 static ssize_t process_vm_readv(pid_t pid,
759 const struct iovec *lvec,
760 unsigned long liovcnt,
761 const struct iovec *rvec,
762 unsigned long riovcnt,
765 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
768 static bool process_vm_readv_not_supported = 1;
769 # define process_vm_readv(...) (errno = ENOSYS, -1)
772 #endif /* end of hack */
774 #define PAGMASK (~(PAGSIZ - 1))
776 * move `len' bytes of data from process `pid'
777 * at address `addr' to our space at `laddr'
780 umoven(struct tcb *tcp, long addr, int len, char *laddr)
787 char x[sizeof(long)];
790 #if SUPPORTED_PERSONALITIES > 1
791 if (personality_wordsize[current_personality] < sizeof(addr))
792 addr &= (1ul << 8 * personality_wordsize[current_personality]) - 1;
795 if (!process_vm_readv_not_supported) {
796 struct iovec local[1], remote[1];
799 local[0].iov_base = laddr;
800 remote[0].iov_base = (void*)addr;
801 local[0].iov_len = remote[0].iov_len = len;
802 r = process_vm_readv(pid,
809 process_vm_readv_not_supported = 1;
810 else if (errno != EINVAL) /* EINVAL is seen if process is gone */
812 perror("process_vm_readv");
813 goto vm_readv_didnt_work;
820 if (addr & (sizeof(long) - 1)) {
821 /* addr not a multiple of sizeof(long) */
822 n = addr - (addr & -sizeof(long)); /* residue */
823 addr &= -sizeof(long); /* residue */
825 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
827 /* But if not started, we had a bogus address. */
828 if (addr != 0 && errno != EIO && errno != ESRCH)
829 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
833 m = MIN(sizeof(long) - n, len);
834 memcpy(laddr, &u.x[n], m);
835 addr += sizeof(long), laddr += m, len -= m;
839 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
841 if (started && (errno==EPERM || errno==EIO)) {
842 /* Ran into 'end of memory' - stupid "printpath" */
845 if (addr != 0 && errno != EIO && errno != ESRCH)
846 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
850 m = MIN(sizeof(long), len);
851 memcpy(laddr, u.x, m);
852 addr += sizeof(long), laddr += m, len -= m;
859 * Like `umove' but make the additional effort of looking
860 * for a terminating zero byte.
862 * Returns < 0 on error, > 0 if NUL was seen,
863 * (TODO if useful: return count of bytes including NUL),
864 * else 0 if len bytes were read but no NUL byte seen.
866 * Note: there is no guarantee we won't overwrite some bytes
867 * in laddr[] _after_ terminating NUL (but, of course,
868 * we never write past laddr[len-1]).
871 umovestr(struct tcb *tcp, long addr, int len, char *laddr)
878 char x[sizeof(long)];
881 #if SUPPORTED_PERSONALITIES > 1
882 if (personality_wordsize[current_personality] < sizeof(addr))
883 addr &= (1ul << 8 * personality_wordsize[current_personality]) - 1;
886 if (!process_vm_readv_not_supported) {
887 struct iovec local[1], remote[1];
889 local[0].iov_base = laddr;
890 remote[0].iov_base = (void*)addr;
897 /* Don't read kilobytes: most strings are short */
901 /* Don't cross pages. I guess otherwise we can get EFAULT
902 * and fail to notice that terminating NUL lies
903 * in the existing (first) page.
904 * (I hope there aren't arches with pages < 4K)
906 end_in_page = ((addr + chunk_len) & 4095);
907 r = chunk_len - end_in_page;
908 if (r > 0) /* if chunk_len > end_in_page */
909 chunk_len = r; /* chunk_len -= end_in_page */
911 local[0].iov_len = remote[0].iov_len = chunk_len;
912 r = process_vm_readv(pid,
919 process_vm_readv_not_supported = 1;
920 else if (errno != EINVAL) /* EINVAL is seen if process is gone */
922 perror("process_vm_readv");
923 goto vm_readv_didnt_work;
925 if (memchr(local[0].iov_base, '\0', r))
927 local[0].iov_base += r;
928 remote[0].iov_base += r;
936 if (addr & (sizeof(long) - 1)) {
937 /* addr not a multiple of sizeof(long) */
938 n = addr - (addr & -sizeof(long)); /* residue */
939 addr &= -sizeof(long); /* residue */
941 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
943 if (addr != 0 && errno != EIO && errno != ESRCH)
944 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
948 m = MIN(sizeof(long) - n, len);
949 memcpy(laddr, &u.x[n], m);
950 while (n & (sizeof(long) - 1))
951 if (u.x[n++] == '\0')
953 addr += sizeof(long), laddr += m, len -= m;
957 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
959 if (started && (errno==EPERM || errno==EIO)) {
960 /* Ran into 'end of memory' - stupid "printpath" */
963 if (addr != 0 && errno != EIO && errno != ESRCH)
964 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
968 m = MIN(sizeof(long), len);
969 memcpy(laddr, u.x, m);
970 for (i = 0; i < sizeof(long); i++)
973 addr += sizeof(long), laddr += m, len -= m;
978 #if !defined(SPARC) && !defined(SPARC64)
979 # define PTRACE_WRITETEXT 101
980 # define PTRACE_WRITEDATA 102
981 #endif /* !SPARC && !SPARC64 */
984 upeek(struct tcb *tcp, long off, long *res)
989 val = ptrace(PTRACE_PEEKUSER, tcp->pid, (char *) off, 0);
990 if (val == -1 && errno) {
991 if (errno != ESRCH) {
992 perror_msg("upeek: PTRACE_PEEKUSER pid:%d @0x%lx)", tcp->pid, off);
1001 printcall(struct tcb *tcp)
1003 #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
1004 sizeof(long) == 8 ? "[????????????????] " : \
1010 if (upeek(tcp, 4*EIP, &eip) < 0) {
1014 tprintf("[%08lx] ", eip);
1016 #elif defined(S390) || defined(S390X)
1018 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
1023 tprintf("[%08lx] ", psw);
1025 tprintf("[%16lx] ", psw);
1028 #elif defined(X86_64)
1031 if (upeek(tcp, 8*RIP, &rip) < 0) {
1035 tprintf("[%16lx] ", rip);
1039 if (upeek(tcp, PT_B0, &ip) < 0) {
1043 tprintf("[%08lx] ", ip);
1044 #elif defined(POWERPC)
1047 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
1052 tprintf("[%016lx] ", pc);
1054 tprintf("[%08lx] ", pc);
1059 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
1060 tprints("[????????] ");
1063 tprintf("[%08lx] ", pc);
1064 #elif defined(ALPHA)
1067 if (upeek(tcp, REG_PC, &pc) < 0) {
1068 tprints("[????????????????] ");
1071 tprintf("[%08lx] ", pc);
1072 #elif defined(SPARC) || defined(SPARC64)
1073 struct pt_regs regs;
1074 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1078 # if defined(SPARC64)
1079 tprintf("[%08lx] ", regs.tpc);
1081 tprintf("[%08lx] ", regs.pc);
1086 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
1087 tprints("[????????] ");
1090 tprintf("[%08lx] ", pc);
1094 if (upeek(tcp, REG_EPC, &pc) < 0) {
1095 tprints("[????????] ");
1098 tprintf("[%08lx] ", pc);
1102 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
1103 tprints("[????????] ");
1106 tprintf("[%08lx] ", pc);
1110 if (upeek(tcp, REG_PC, &pc) < 0) {
1111 tprints("[????????????????] ");
1114 tprintf("[%08lx] ", pc);
1118 if (upeek(tcp, 4*15, &pc) < 0) {
1122 tprintf("[%08lx] ", pc);
1123 #elif defined(AVR32)
1126 if (upeek(tcp, REG_PC, &pc) < 0) {
1127 tprints("[????????] ");
1130 tprintf("[%08lx] ", pc);
1134 if (upeek(tcp, PT_PC, &pc) < 0) {
1138 tprintf("[%08lx] ", pc);
1139 #elif defined(CRISV10)
1142 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
1146 tprintf("[%08lx] ", pc);
1147 #elif defined(CRISV32)
1150 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
1154 tprintf("[%08lx] ", pc);
1155 #endif /* architecture */
1159 * These #if's are huge, please indent them correctly.
1160 * It's easy to get confused otherwise.
1163 #include "syscall.h"
1165 #include <sys/syscall.h>
1166 #ifndef CLONE_PTRACE
1167 # define CLONE_PTRACE 0x00002000
1170 # define CLONE_VFORK 0x00004000
1173 # define CLONE_VM 0x00000100
1175 #ifndef CLONE_STOPPED
1176 # define CLONE_STOPPED 0x02000000
1181 typedef unsigned long *arg_setup_state;
1184 arg_setup(struct tcb *tcp, arg_setup_state *state)
1186 unsigned long cfm, sof, sol;
1190 /* Satisfy a false GCC warning. */
1195 if (upeek(tcp, PT_AR_BSP, &bsp) < 0)
1197 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1200 sof = (cfm >> 0) & 0x7f;
1201 sol = (cfm >> 7) & 0x7f;
1202 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
1204 *state = (unsigned long *) bsp;
1208 # define arg_finish_change(tcp, state) 0
1211 get_arg0(struct tcb *tcp, arg_setup_state *state, long *valp)
1216 ret = upeek(tcp, PT_R11, valp);
1219 (unsigned long) ia64_rse_skip_regs(*state, 0),
1220 sizeof(long), (void *) valp);
1225 get_arg1(struct tcb *tcp, arg_setup_state *state, long *valp)
1230 ret = upeek(tcp, PT_R9, valp);
1233 (unsigned long) ia64_rse_skip_regs(*state, 1),
1234 sizeof(long), (void *) valp);
1239 set_arg0(struct tcb *tcp, arg_setup_state *state, long val)
1241 int req = PTRACE_POKEDATA;
1245 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1246 req = PTRACE_POKEUSER;
1248 ap = ia64_rse_skip_regs(*state, 0);
1250 ptrace(req, tcp->pid, ap, val);
1251 return errno ? -1 : 0;
1255 set_arg1(struct tcb *tcp, arg_setup_state *state, long val)
1257 int req = PTRACE_POKEDATA;
1261 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1262 req = PTRACE_POKEUSER;
1264 ap = ia64_rse_skip_regs(*state, 1);
1266 ptrace(req, tcp->pid, ap, val);
1267 return errno ? -1 : 0;
1270 /* ia64 does not return the input arguments from functions (and syscalls)
1271 according to ia64 RSE (Register Stack Engine) behavior. */
1273 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1274 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1276 #elif defined(SPARC) || defined(SPARC64)
1278 typedef struct pt_regs arg_setup_state;
1280 # define arg_setup(tcp, state) \
1281 (ptrace(PTRACE_GETREGS, (tcp)->pid, (char *) (state), 0))
1282 # define arg_finish_change(tcp, state) \
1283 (ptrace(PTRACE_SETREGS, (tcp)->pid, (char *) (state), 0))
1285 # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0)
1286 # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0)
1287 # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0)
1288 # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0)
1289 # define restore_arg0(tcp, state, val) 0
1291 #else /* other architectures */
1293 # if defined S390 || defined S390X
1294 /* Note: this is only true for the `clone' system call, which handles
1295 arguments specially. We could as well say that its first two arguments
1296 are swapped relative to other architectures, but that would just be
1297 another #ifdef in the calls. */
1298 # define arg0_offset PT_GPR3
1299 # define arg1_offset PT_ORIGGPR2
1300 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1301 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1302 # define arg0_index 1
1303 # define arg1_index 0
1304 # elif defined(ALPHA) || defined(MIPS)
1305 # define arg0_offset REG_A0
1306 # define arg1_offset (REG_A0+1)
1307 # elif defined(AVR32)
1308 # define arg0_offset (REG_R12)
1309 # define arg1_offset (REG_R11)
1310 # elif defined(POWERPC)
1311 # define arg0_offset (sizeof(unsigned long)*PT_R3)
1312 # define arg1_offset (sizeof(unsigned long)*PT_R4)
1313 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1314 # elif defined(HPPA)
1315 # define arg0_offset PT_GR26
1316 # define arg1_offset (PT_GR26-4)
1317 # elif defined(X86_64)
1318 # define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1319 # define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1321 # define arg0_offset (4*(REG_REG0+4))
1322 # define arg1_offset (4*(REG_REG0+5))
1323 # elif defined(SH64)
1324 /* ABI defines arg0 & 1 in r2 & r3 */
1325 # define arg0_offset (REG_OFFSET+16)
1326 # define arg1_offset (REG_OFFSET+24)
1327 # define restore_arg0(tcp, state, val) 0
1328 # elif defined CRISV10 || defined CRISV32
1329 # define arg0_offset (4*PT_R11)
1330 # define arg1_offset (4*PT_ORIG_R10)
1331 # define restore_arg0(tcp, state, val) 0
1332 # define restore_arg1(tcp, state, val) 0
1333 # define arg0_index 1
1334 # define arg1_index 0
1336 # define arg0_offset 0
1337 # define arg1_offset 4
1339 # define restore_arg0(tcp, state, val) 0
1343 typedef int arg_setup_state;
1345 # define arg_setup(tcp, state) (0)
1346 # define arg_finish_change(tcp, state) 0
1347 # define get_arg0(tcp, cookie, valp) (upeek((tcp), arg0_offset, (valp)))
1348 # define get_arg1(tcp, cookie, valp) (upeek((tcp), arg1_offset, (valp)))
1351 set_arg0(struct tcb *tcp, void *cookie, long val)
1353 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val);
1357 set_arg1(struct tcb *tcp, void *cookie, long val)
1359 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val);
1362 #endif /* architectures */
1364 #ifndef restore_arg0
1365 # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1367 #ifndef restore_arg1
1368 # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1372 # define arg0_index 0
1373 # define arg1_index 1
1377 setbpt(struct tcb *tcp)
1379 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
1380 arg_setup_state state;
1382 if (tcp->flags & TCB_BPTSET) {
1383 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1388 * It's a silly kludge to initialize this with a search at runtime.
1389 * But it's better than maintaining another magic thing in the
1390 * godforsaken tables.
1392 if (clone_scno[current_personality] == 0) {
1394 for (i = 0; i < nsyscalls; ++i)
1395 if (sysent[i].sys_func == sys_clone) {
1396 clone_scno[current_personality] = i;
1401 if (sysent[tcp->scno].sys_func == sys_fork ||
1402 sysent[tcp->scno].sys_func == sys_vfork) {
1403 if (arg_setup(tcp, &state) < 0
1404 || get_arg0(tcp, &state, &tcp->inst[0]) < 0
1405 || get_arg1(tcp, &state, &tcp->inst[1]) < 0
1406 || change_syscall(tcp, clone_scno[current_personality]) < 0
1407 || set_arg0(tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1408 || set_arg1(tcp, &state, 0) < 0
1409 || arg_finish_change(tcp, &state) < 0)
1411 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1412 tcp->u_arg[arg1_index] = 0;
1413 tcp->flags |= TCB_BPTSET;
1417 if (sysent[tcp->scno].sys_func == sys_clone) {
1418 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1419 contrary to x86 vfork above. Even on x86 we turn the
1420 vfork semantics into plain fork - each application must not
1421 depend on the vfork specifics according to POSIX. We would
1422 hang waiting for the parent resume otherwise. We need to
1423 clear also CLONE_VM but only in the CLONE_VFORK case as
1424 otherwise we would break pthread_create. */
1426 long new_arg0 = (tcp->u_arg[arg0_index] | CLONE_PTRACE);
1427 if (new_arg0 & CLONE_VFORK)
1428 new_arg0 &= ~(unsigned long)(CLONE_VFORK | CLONE_VM);
1429 if (arg_setup(tcp, &state) < 0
1430 || set_arg0(tcp, &state, new_arg0) < 0
1431 || arg_finish_change(tcp, &state) < 0)
1433 tcp->flags |= TCB_BPTSET;
1434 tcp->inst[0] = tcp->u_arg[arg0_index];
1435 tcp->inst[1] = tcp->u_arg[arg1_index];
1439 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1440 tcp->scno, tcp->pid);
1445 clearbpt(struct tcb *tcp)
1447 arg_setup_state state;
1448 if (arg_setup(tcp, &state) < 0
1449 || restore_arg0(tcp, &state, tcp->inst[0]) < 0
1450 || restore_arg1(tcp, &state, tcp->inst[1]) < 0
1451 || arg_finish_change(tcp, &state))
1454 tcp->flags &= ~TCB_BPTSET;