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.
36 #include <sys/param.h>
42 #if __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 1)
43 # include <linux/ptrace.h>
47 # include <asm/ptrace_offsets.h>
53 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
54 #elif defined(HAVE_LINUX_PTRACE_H)
55 # undef PTRACE_SYSCALL
56 # ifdef HAVE_STRUCT_IA64_FPREG
57 # define ia64_fpreg XXX_ia64_fpreg
59 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
60 # define pt_all_user_regs XXX_pt_all_user_regs
62 # include <linux/ptrace.h>
64 # undef pt_all_user_regs
68 # undef PTRACE_GETREGS
69 # define PTRACE_GETREGS PTRACE_GETREGS64
70 # undef PTRACE_SETREGS
71 # define PTRACE_SETREGS PTRACE_SETREGS64
76 # define MAX(a,b) (((a) > (b)) ? (a) : (b))
79 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
83 string_to_uint(const char *str)
91 value = strtol(str, &error, 10);
92 if (errno || *error || value < 0 || (long)(int)value != value)
98 tv_nz(struct timeval *a)
100 return a->tv_sec || a->tv_usec;
104 tv_cmp(struct timeval *a, struct timeval *b)
106 if (a->tv_sec < b->tv_sec
107 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
109 if (a->tv_sec > b->tv_sec
110 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
116 tv_float(struct timeval *tv)
118 return tv->tv_sec + tv->tv_usec/1000000.0;
122 tv_add(struct timeval *tv, struct timeval *a, struct timeval *b)
124 tv->tv_sec = a->tv_sec + b->tv_sec;
125 tv->tv_usec = a->tv_usec + b->tv_usec;
126 if (tv->tv_usec >= 1000000) {
128 tv->tv_usec -= 1000000;
133 tv_sub(struct timeval *tv, struct timeval *a, struct timeval *b)
135 tv->tv_sec = a->tv_sec - b->tv_sec;
136 tv->tv_usec = a->tv_usec - b->tv_usec;
137 if (((long) tv->tv_usec) < 0) {
139 tv->tv_usec += 1000000;
144 tv_div(struct timeval *tv, struct timeval *a, int n)
146 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
147 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
148 tv->tv_usec %= 1000000;
152 tv_mul(struct timeval *tv, struct timeval *a, int n)
154 tv->tv_usec = a->tv_usec * n;
155 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
156 tv->tv_usec %= 1000000;
160 xlookup(const struct xlat *xlat, int val)
162 for (; xlat->str != NULL; xlat++)
163 if (xlat->val == val)
168 #if !defined HAVE_STPCPY
170 stpcpy(char *dst, const char *src)
172 while ((*dst = *src++) != '\0')
179 * Print entry in struct xlat table, if there.
182 printxval(const struct xlat *xlat, int val, const char *dflt)
184 const char *str = xlookup(xlat, val);
189 tprintf("%#x /* %s */", val, dflt);
194 * Print 64bit argument at position llarg and return the index of the next
198 printllval(struct tcb *tcp, const char *format, int llarg)
200 # if defined(X86_64) || defined(POWERPC64)
201 if (current_personality == 0) {
202 tprintf(format, tcp->u_arg[llarg]);
206 /* Align 64bit argument to 64bit boundary. */
207 llarg = (llarg + 1) & 0x1e;
209 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
212 # elif defined IA64 || defined ALPHA
213 tprintf(format, tcp->u_arg[llarg]);
215 # elif defined LINUX_MIPSN32 || defined X32
216 tprintf(format, tcp->ext_arg[llarg]);
219 tprintf(format, LONG_LONG(tcp->u_arg[llarg], tcp->u_arg[llarg + 1]));
227 * Interpret `xlat' as an array of flags
228 * print the entries whose bits are on in `flags'
229 * return # of flags printed.
232 addflags(const struct xlat *xlat, int flags)
234 for (; xlat->str; xlat++) {
235 if (xlat->val && (flags & xlat->val) == xlat->val) {
236 tprintf("|%s", xlat->str);
241 tprintf("|%#x", flags);
246 * Interpret `xlat' as an array of flags.
247 * Print to static string the entries whose bits are on in `flags'
248 * Return static string.
251 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
253 static char outstr[1024];
257 outptr = stpcpy(outstr, prefix);
259 for (; xlat->str; xlat++) {
260 if ((flags & xlat->val) == xlat->val) {
263 outptr = stpcpy(outptr, xlat->str);
273 outptr += sprintf(outptr, "%#x", flags);
280 printflags(const struct xlat *xlat, int flags, const char *dflt)
285 if (flags == 0 && xlat->val == 0) {
291 for (n = 0; xlat->str; xlat++) {
292 if (xlat->val && (flags & xlat->val) == xlat->val) {
293 tprintf("%s%s", sep, xlat->str);
302 tprintf("%s%#x", sep, flags);
307 tprintf("%#x", flags);
309 tprintf(" /* %s */", dflt);
320 printnum(struct tcb *tcp, long addr, const char *fmt)
328 if (umove(tcp, addr, &num) < 0) {
329 tprintf("%#lx", addr);
338 printnum_int(struct tcb *tcp, long addr, const char *fmt)
346 if (umove(tcp, addr, &num) < 0) {
347 tprintf("%#lx", addr);
356 printfd(struct tcb *tcp, int fd)
360 if (show_fd_path && (p = getfdpath(tcp, fd)))
361 tprintf("%d<%s>", fd, p);
367 printuid(const char *text, unsigned long uid)
369 tprintf((uid == -1) ? "%s%ld" : "%s%lu", text, uid);
373 * Quote string `instr' of length `size'
374 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
375 * If `len' is -1, treat `instr' as a NUL-terminated string
376 * and quote at most (`size' - 1) bytes.
378 * Returns 0 if len == -1 and NUL was seen, 1 otherwise.
379 * Note that if len >= 0, always returns 1.
382 string_quote(const char *instr, char *outstr, long len, int size)
384 const unsigned char *ustr = (const unsigned char *) instr;
386 int usehex, c, i, eol;
388 eol = 0x100; /* this can never match a char */
398 /* Check for presence of symbol which require
399 to hex-quote the whole string. */
400 for (i = 0; i < size; ++i) {
402 /* Check for NUL-terminated string. */
405 if (!isprint(c) && !isspace(c)) {
415 /* Hex-quote the whole string. */
416 for (i = 0; i < size; ++i) {
418 /* Check for NUL-terminated string. */
423 *s++ = "0123456789abcdef"[c >> 4];
424 *s++ = "0123456789abcdef"[c & 0xf];
427 for (i = 0; i < size; ++i) {
429 /* Check for NUL-terminated string. */
433 case '\"': case '\\':
464 && ustr[i + 1] >= '0'
465 && ustr[i + 1] <= '9'
468 *s++ = '0' + (c >> 6);
469 *s++ = '0' + ((c >> 3) & 0x7);
474 *s++ = '0' + (c >> 6);
475 *s++ = '0' + ((c >> 3) & 0x7);
478 *s++ = '0' + (c & 0x7);
488 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
489 if (len == -1 && ustr[i] == '\0') {
490 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
491 * but next char is NUL.
501 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
506 * Print path string specified by address `addr' and length `n'.
507 * If path length exceeds `n', append `...' to the output.
510 printpathn(struct tcb *tcp, long addr, int n)
512 char path[MAXPATHLEN + 1];
520 /* Cap path length to the path buffer size */
521 if (n > sizeof path - 1)
524 /* Fetch one byte more to find out whether path length > n. */
525 nul_seen = umovestr(tcp, addr, n + 1, path);
527 tprintf("%#lx", addr);
533 outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */
534 string_quote(path, outstr, -1, n);
542 printpath(struct tcb *tcp, long addr)
544 /* Size must correspond to char path[] size in printpathn */
545 printpathn(tcp, addr, MAXPATHLEN);
549 * Print string specified by address `addr' and length `len'.
550 * If `len' < 0, treat the string as a NUL-terminated string.
551 * If string length exceeds `max_strlen', append `...' to the output.
554 printstr(struct tcb *tcp, long addr, long len)
556 static char *str = NULL;
565 /* Allocate static buffers if they are not allocated yet. */
567 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
569 if (outstr_size / 4 != max_strlen)
571 str = malloc(max_strlen + 1);
574 outstr = malloc(outstr_size);
581 * Treat as a NUL-terminated string: fetch one byte more
582 * because string_quote() quotes one byte less.
584 size = max_strlen + 1;
585 if (umovestr(tcp, addr, size, str) < 0) {
586 tprintf("%#lx", addr);
592 if (size > (unsigned long)len)
593 size = (unsigned long)len;
594 if (umoven(tcp, addr, size, str) < 0) {
595 tprintf("%#lx", addr);
600 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
601 * or we were requested to print more than -s NUM chars)...
603 ellipsis = (string_quote(str, outstr, len, size) &&
604 (len < 0 || len > max_strlen));
613 dumpiov(struct tcb *tcp, int len, long addr)
615 #if SUPPORTED_PERSONALITIES > 1
617 struct { u_int32_t base; u_int32_t len; } *iov32;
618 struct { u_int64_t base; u_int64_t len; } *iov64;
620 #define iov iovu.iov64
622 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
623 #define iov_iov_base(i) \
624 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
625 #define iov_iov_len(i) \
626 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
629 #define sizeof_iov sizeof(*iov)
630 #define iov_iov_base(i) iov[i].iov_base
631 #define iov_iov_len(i) iov[i].iov_len
636 size = sizeof_iov * len;
637 /* Assuming no sane program has millions of iovs */
638 if ((unsigned)len > 1024*1024 /* insane or negative size? */
639 || (iov = malloc(size)) == NULL) {
640 fprintf(stderr, "Out of memory\n");
643 if (umoven(tcp, addr, size, (char *) iov) >= 0) {
644 for (i = 0; i < len; i++) {
645 /* include the buffer number to make it easy to
646 * match up the trace with the source */
647 tprintf(" * %lu bytes in buffer %d\n",
648 (unsigned long)iov_iov_len(i), i);
649 dumpstr(tcp, (long) iov_iov_base(i),
662 dumpstr(struct tcb *tcp, long addr, int len)
664 static int strsize = -1;
665 static unsigned char *str;
674 fprintf(stderr, "Out of memory\n");
680 if (umoven(tcp, addr, len, (char *) str) < 0)
683 for (i = 0; i < len; i += 16) {
687 sprintf(s, " | %05x ", i);
689 for (j = 0; j < 16; j++) {
693 sprintf(s, " %02x", str[i + j]);
697 *s++ = ' '; *s++ = ' '; *s++ = ' ';
700 *s++ = ' '; *s++ = ' ';
701 for (j = 0; j < 16; j++) {
705 if (isprint(str[i + j]))
713 tprintf("%s |\n", outstr);
717 #ifdef HAVE_PROCESS_VM_READV
718 /* C library supports this, but the kernel might not. */
719 static bool process_vm_readv_not_supported = 0;
722 /* Need to do this since process_vm_readv() is not yet available in libc.
723 * When libc is be updated, only "static bool process_vm_readv_not_supported"
724 * line should remain.
726 #if !defined(__NR_process_vm_readv)
728 # define __NR_process_vm_readv 347
729 # elif defined(X86_64)
730 # define __NR_process_vm_readv 310
731 # elif defined(POWERPC)
732 # define __NR_process_vm_readv 351
736 #if defined(__NR_process_vm_readv)
737 static bool process_vm_readv_not_supported = 0;
738 /* Have to avoid duplicating with the C library headers. */
739 static ssize_t strace_process_vm_readv(pid_t pid,
740 const struct iovec *lvec,
741 unsigned long liovcnt,
742 const struct iovec *rvec,
743 unsigned long riovcnt,
746 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
748 #define process_vm_readv strace_process_vm_readv
750 static bool process_vm_readv_not_supported = 1;
751 # define process_vm_readv(...) (errno = ENOSYS, -1)
754 #endif /* end of hack */
756 #define PAGMASK (~(PAGSIZ - 1))
758 * move `len' bytes of data from process `pid'
759 * at address `addr' to our space at `laddr'
762 umoven(struct tcb *tcp, long addr, int len, char *laddr)
769 char x[sizeof(long)];
772 #if SUPPORTED_PERSONALITIES > 1
773 if (current_wordsize < sizeof(addr))
774 addr &= (1ul << 8 * current_wordsize) - 1;
777 if (!process_vm_readv_not_supported) {
778 struct iovec local[1], remote[1];
781 local[0].iov_base = laddr;
782 remote[0].iov_base = (void*)addr;
783 local[0].iov_len = remote[0].iov_len = len;
784 r = process_vm_readv(pid,
791 process_vm_readv_not_supported = 1;
792 else if (errno != EINVAL && errno != ESRCH)
793 /* EINVAL or ESRCH could be seen if process is gone,
794 * all the rest is strange and should be reported. */
795 perror_msg("%s", "process_vm_readv");
796 goto vm_readv_didnt_work;
803 if (addr & (sizeof(long) - 1)) {
804 /* addr not a multiple of sizeof(long) */
805 n = addr - (addr & -sizeof(long)); /* residue */
806 addr &= -sizeof(long); /* residue */
808 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
810 /* But if not started, we had a bogus address. */
811 if (addr != 0 && errno != EIO && errno != ESRCH)
812 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
816 m = MIN(sizeof(long) - n, len);
817 memcpy(laddr, &u.x[n], m);
818 addr += sizeof(long), laddr += m, len -= m;
822 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
824 if (started && (errno==EPERM || errno==EIO)) {
825 /* Ran into 'end of memory' - stupid "printpath" */
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), len);
834 memcpy(laddr, u.x, m);
835 addr += sizeof(long), laddr += m, len -= m;
842 * Like `umove' but make the additional effort of looking
843 * for a terminating zero byte.
845 * Returns < 0 on error, > 0 if NUL was seen,
846 * (TODO if useful: return count of bytes including NUL),
847 * else 0 if len bytes were read but no NUL byte seen.
849 * Note: there is no guarantee we won't overwrite some bytes
850 * in laddr[] _after_ terminating NUL (but, of course,
851 * we never write past laddr[len-1]).
854 umovestr(struct tcb *tcp, long addr, int len, char *laddr)
861 char x[sizeof(long)];
864 #if SUPPORTED_PERSONALITIES > 1
865 if (current_wordsize < sizeof(addr))
866 addr &= (1ul << 8 * current_wordsize) - 1;
869 if (!process_vm_readv_not_supported) {
870 struct iovec local[1], remote[1];
872 local[0].iov_base = laddr;
873 remote[0].iov_base = (void*)addr;
880 /* Don't read kilobytes: most strings are short */
884 /* Don't cross pages. I guess otherwise we can get EFAULT
885 * and fail to notice that terminating NUL lies
886 * in the existing (first) page.
887 * (I hope there aren't arches with pages < 4K)
889 end_in_page = ((addr + chunk_len) & 4095);
890 r = chunk_len - end_in_page;
891 if (r > 0) /* if chunk_len > end_in_page */
892 chunk_len = r; /* chunk_len -= end_in_page */
894 local[0].iov_len = remote[0].iov_len = chunk_len;
895 r = process_vm_readv(pid,
902 process_vm_readv_not_supported = 1;
903 else if (errno != EINVAL && errno != ESRCH)
904 /* EINVAL or ESRCH could be seen
905 * if process is gone, all the rest
906 * is strange and should be reported. */
907 perror_msg("%s", "process_vm_readv");
908 goto vm_readv_didnt_work;
910 if (memchr(local[0].iov_base, '\0', r))
912 local[0].iov_base += r;
913 remote[0].iov_base += r;
921 if (addr & (sizeof(long) - 1)) {
922 /* addr not a multiple of sizeof(long) */
923 n = addr - (addr & -sizeof(long)); /* residue */
924 addr &= -sizeof(long); /* residue */
926 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
928 if (addr != 0 && errno != EIO && errno != ESRCH)
929 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
933 m = MIN(sizeof(long) - n, len);
934 memcpy(laddr, &u.x[n], m);
935 while (n & (sizeof(long) - 1))
936 if (u.x[n++] == '\0')
938 addr += sizeof(long), laddr += m, len -= m;
942 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
944 if (started && (errno==EPERM || errno==EIO)) {
945 /* Ran into 'end of memory' - stupid "printpath" */
948 if (addr != 0 && errno != EIO && errno != ESRCH)
949 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx", pid, addr);
953 m = MIN(sizeof(long), len);
954 memcpy(laddr, u.x, m);
955 for (i = 0; i < sizeof(long); i++)
958 addr += sizeof(long), laddr += m, len -= m;
964 upeek(struct tcb *tcp, long off, long *res)
969 val = ptrace(PTRACE_PEEKUSER, tcp->pid, (char *) off, 0);
970 if (val == -1 && errno) {
971 if (errno != ESRCH) {
972 perror_msg("upeek: PTRACE_PEEKUSER pid:%d @0x%lx)", tcp->pid, off);
981 printcall(struct tcb *tcp)
983 #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
984 sizeof(long) == 8 ? "[????????????????] " : \
990 if (upeek(tcp, 4*EIP, &eip) < 0) {
994 tprintf("[%08lx] ", eip);
995 #elif defined(S390) || defined(S390X)
997 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
1002 tprintf("[%08lx] ", psw);
1004 tprintf("[%16lx] ", psw);
1007 #elif defined(X86_64) || defined(X32)
1010 if (upeek(tcp, 8*RIP, &rip) < 0) {
1014 tprintf("[%16lx] ", rip);
1018 if (upeek(tcp, PT_B0, &ip) < 0) {
1022 tprintf("[%08lx] ", ip);
1023 #elif defined(POWERPC)
1026 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
1031 tprintf("[%016lx] ", pc);
1033 tprintf("[%08lx] ", pc);
1038 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
1039 tprints("[????????] ");
1042 tprintf("[%08lx] ", pc);
1043 #elif defined(ALPHA)
1046 if (upeek(tcp, REG_PC, &pc) < 0) {
1047 tprints("[????????????????] ");
1050 tprintf("[%08lx] ", pc);
1051 #elif defined(SPARC) || defined(SPARC64)
1052 struct pt_regs regs;
1053 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) {
1057 # if defined(SPARC64)
1058 tprintf("[%08lx] ", regs.tpc);
1060 tprintf("[%08lx] ", regs.pc);
1065 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
1066 tprints("[????????] ");
1069 tprintf("[%08lx] ", pc);
1073 if (upeek(tcp, REG_EPC, &pc) < 0) {
1074 tprints("[????????] ");
1077 tprintf("[%08lx] ", pc);
1081 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
1082 tprints("[????????] ");
1085 tprintf("[%08lx] ", pc);
1089 if (upeek(tcp, REG_PC, &pc) < 0) {
1090 tprints("[????????????????] ");
1093 tprintf("[%08lx] ", pc);
1097 if (upeek(tcp, 4*15, &pc) < 0) {
1101 tprintf("[%08lx] ", pc);
1102 #elif defined(AVR32)
1105 if (upeek(tcp, REG_PC, &pc) < 0) {
1106 tprints("[????????] ");
1109 tprintf("[%08lx] ", pc);
1113 if (upeek(tcp, PT_PC, &pc) < 0) {
1117 tprintf("[%08lx] ", pc);
1118 #elif defined(CRISV10)
1121 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
1125 tprintf("[%08lx] ", pc);
1126 #elif defined(CRISV32)
1129 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
1133 tprintf("[%08lx] ", pc);
1134 #endif /* architecture */
1138 * These #if's are huge, please indent them correctly.
1139 * It's easy to get confused otherwise.
1142 #include "syscall.h"
1144 #ifndef CLONE_PTRACE
1145 # define CLONE_PTRACE 0x00002000
1148 # define CLONE_VFORK 0x00004000
1151 # define CLONE_VM 0x00000100
1156 typedef unsigned long *arg_setup_state;
1159 arg_setup(struct tcb *tcp, arg_setup_state *state)
1161 unsigned long cfm, sof, sol;
1165 /* Satisfy a false GCC warning. */
1170 if (upeek(tcp, PT_AR_BSP, &bsp) < 0)
1172 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1175 sof = (cfm >> 0) & 0x7f;
1176 sol = (cfm >> 7) & 0x7f;
1177 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
1179 *state = (unsigned long *) bsp;
1183 # define arg_finish_change(tcp, state) 0
1186 get_arg0(struct tcb *tcp, arg_setup_state *state, long *valp)
1191 ret = upeek(tcp, PT_R11, valp);
1194 (unsigned long) ia64_rse_skip_regs(*state, 0),
1195 sizeof(long), (void *) valp);
1200 get_arg1(struct tcb *tcp, arg_setup_state *state, long *valp)
1205 ret = upeek(tcp, PT_R9, valp);
1208 (unsigned long) ia64_rse_skip_regs(*state, 1),
1209 sizeof(long), (void *) valp);
1214 set_arg0(struct tcb *tcp, arg_setup_state *state, long val)
1216 int req = PTRACE_POKEDATA;
1220 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1221 req = PTRACE_POKEUSER;
1223 ap = ia64_rse_skip_regs(*state, 0);
1225 ptrace(req, tcp->pid, ap, val);
1226 return errno ? -1 : 0;
1230 set_arg1(struct tcb *tcp, arg_setup_state *state, long val)
1232 int req = PTRACE_POKEDATA;
1236 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1237 req = PTRACE_POKEUSER;
1239 ap = ia64_rse_skip_regs(*state, 1);
1241 ptrace(req, tcp->pid, ap, val);
1242 return errno ? -1 : 0;
1245 /* ia64 does not return the input arguments from functions (and syscalls)
1246 according to ia64 RSE (Register Stack Engine) behavior. */
1248 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1249 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1251 #elif defined(SPARC) || defined(SPARC64)
1253 typedef struct pt_regs arg_setup_state;
1255 # define arg_setup(tcp, state) \
1256 (ptrace(PTRACE_GETREGS, (tcp)->pid, (char *) (state), 0))
1257 # define arg_finish_change(tcp, state) \
1258 (ptrace(PTRACE_SETREGS, (tcp)->pid, (char *) (state), 0))
1260 # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0)
1261 # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0)
1262 # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0)
1263 # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0)
1264 # define restore_arg0(tcp, state, val) 0
1266 #else /* other architectures */
1268 # if defined S390 || defined S390X
1269 /* Note: this is only true for the `clone' system call, which handles
1270 arguments specially. We could as well say that its first two arguments
1271 are swapped relative to other architectures, but that would just be
1272 another #ifdef in the calls. */
1273 # define arg0_offset PT_GPR3
1274 # define arg1_offset PT_ORIGGPR2
1275 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1276 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1277 # define arg0_index 1
1278 # define arg1_index 0
1279 # elif defined(ALPHA) || defined(MIPS)
1280 # define arg0_offset REG_A0
1281 # define arg1_offset (REG_A0+1)
1282 # elif defined(AVR32)
1283 # define arg0_offset (REG_R12)
1284 # define arg1_offset (REG_R11)
1285 # elif defined(POWERPC)
1286 # define arg0_offset (sizeof(unsigned long)*PT_R3)
1287 # define arg1_offset (sizeof(unsigned long)*PT_R4)
1288 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1289 # elif defined(HPPA)
1290 # define arg0_offset PT_GR26
1291 # define arg1_offset (PT_GR26-4)
1292 # elif defined(X86_64) || defined(X32)
1293 # define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1294 # define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1296 # define arg0_offset (4*(REG_REG0+4))
1297 # define arg1_offset (4*(REG_REG0+5))
1298 # elif defined(SH64)
1299 /* ABI defines arg0 & 1 in r2 & r3 */
1300 # define arg0_offset (REG_OFFSET+16)
1301 # define arg1_offset (REG_OFFSET+24)
1302 # define restore_arg0(tcp, state, val) 0
1303 # elif defined CRISV10 || defined CRISV32
1304 # define arg0_offset (4*PT_R11)
1305 # define arg1_offset (4*PT_ORIG_R10)
1306 # define restore_arg0(tcp, state, val) 0
1307 # define restore_arg1(tcp, state, val) 0
1308 # define arg0_index 1
1309 # define arg1_index 0
1311 # define arg0_offset 0
1312 # define arg1_offset 4
1314 # define restore_arg0(tcp, state, val) 0
1318 typedef int arg_setup_state;
1320 # define arg_setup(tcp, state) (0)
1321 # define arg_finish_change(tcp, state) 0
1322 # define get_arg0(tcp, cookie, valp) (upeek((tcp), arg0_offset, (valp)))
1323 # define get_arg1(tcp, cookie, valp) (upeek((tcp), arg1_offset, (valp)))
1326 set_arg0(struct tcb *tcp, void *cookie, long val)
1328 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val);
1332 set_arg1(struct tcb *tcp, void *cookie, long val)
1334 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val);
1337 #endif /* architectures */
1339 #ifndef restore_arg0
1340 # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1342 #ifndef restore_arg1
1343 # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1347 # define arg0_index 0
1348 # define arg1_index 1
1352 change_syscall(struct tcb *tcp, arg_setup_state *state, int new)
1355 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_EAX * 4), new) < 0)
1358 #elif defined(X86_64) || defined(X32)
1359 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_RAX * 8), new) < 0)
1362 #elif defined(POWERPC)
1363 if (ptrace(PTRACE_POKEUSER, tcp->pid,
1364 (char*)(sizeof(unsigned long)*PT_R0), new) < 0)
1367 #elif defined(S390) || defined(S390X)
1368 /* s390 linux after 2.4.7 has a hook in entry.S to allow this */
1369 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GPR2), new) < 0)
1373 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_ORIG_D0), new) < 0)
1376 #elif defined(SPARC) || defined(SPARC64)
1377 state->u_regs[U_REG_G1] = new;
1380 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), new) < 0)
1383 #elif defined(ALPHA)
1384 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), new) < 0)
1387 #elif defined(AVR32)
1388 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R8), new) < 0)
1392 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_P0), new) < 0)
1399 break; /* x86 SYS_fork */
1404 fprintf(stderr, "%s: unexpected syscall %d\n",
1408 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R1), new) < 0)
1410 } else if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R15), new) < 0)
1414 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR20), new) < 0)
1418 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*(REG_REG0+3)), new) < 0)
1422 /* Top half of reg encodes the no. of args n as 0x1n.
1423 Assume 0 args as kernel never actually checks... */
1424 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_SYSCALL),
1425 0x100000 | new) < 0)
1428 #elif defined(CRISV10) || defined(CRISV32)
1429 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_R9), new) < 0)
1433 /* Some kernels support this, some (pre-2.6.16 or so) don't. */
1434 # ifndef PTRACE_SET_SYSCALL
1435 # define PTRACE_SET_SYSCALL 23
1437 if (ptrace(PTRACE_SET_SYSCALL, tcp->pid, 0, new & 0xffff) != 0)
1441 if (ptrace(PTRACE_POKEUSER, tcp->pid,
1442 (char*)PTREGS_OFFSET_REG(0),
1446 #elif defined(MICROBLAZE)
1447 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GPR(0)), new) < 0)
1451 #warning Do not know how to handle change_syscall for this architecture
1452 #endif /* architecture */
1457 setbpt(struct tcb *tcp)
1459 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
1460 arg_setup_state state;
1462 if (tcp->flags & TCB_BPTSET) {
1463 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1468 * It's a silly kludge to initialize this with a search at runtime.
1469 * But it's better than maintaining another magic thing in the
1470 * godforsaken tables.
1472 if (clone_scno[current_personality] == 0) {
1474 for (i = 0; i < nsyscalls; ++i)
1475 if (sysent[i].sys_func == sys_clone) {
1476 clone_scno[current_personality] = i;
1481 if (sysent[tcp->scno].sys_func == sys_fork ||
1482 sysent[tcp->scno].sys_func == sys_vfork) {
1483 if (arg_setup(tcp, &state) < 0
1484 || get_arg0(tcp, &state, &tcp->inst[0]) < 0
1485 || get_arg1(tcp, &state, &tcp->inst[1]) < 0
1486 || change_syscall(tcp, &state,
1487 clone_scno[current_personality]) < 0
1488 || set_arg0(tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1489 || set_arg1(tcp, &state, 0) < 0
1490 || arg_finish_change(tcp, &state) < 0)
1492 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1493 tcp->u_arg[arg1_index] = 0;
1494 tcp->flags |= TCB_BPTSET;
1498 if (sysent[tcp->scno].sys_func == sys_clone) {
1499 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1500 contrary to x86 vfork above. Even on x86 we turn the
1501 vfork semantics into plain fork - each application must not
1502 depend on the vfork specifics according to POSIX. We would
1503 hang waiting for the parent resume otherwise. We need to
1504 clear also CLONE_VM but only in the CLONE_VFORK case as
1505 otherwise we would break pthread_create. */
1507 long new_arg0 = (tcp->u_arg[arg0_index] | CLONE_PTRACE);
1508 if (new_arg0 & CLONE_VFORK)
1509 new_arg0 &= ~(unsigned long)(CLONE_VFORK | CLONE_VM);
1510 if (arg_setup(tcp, &state) < 0
1511 || set_arg0(tcp, &state, new_arg0) < 0
1512 || arg_finish_change(tcp, &state) < 0)
1514 tcp->inst[0] = tcp->u_arg[arg0_index];
1515 tcp->inst[1] = tcp->u_arg[arg1_index];
1516 tcp->flags |= TCB_BPTSET;
1520 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1521 tcp->scno, tcp->pid);
1526 clearbpt(struct tcb *tcp)
1528 arg_setup_state state;
1529 if (arg_setup(tcp, &state) < 0
1530 || change_syscall(tcp, &state, tcp->scno) < 0
1531 || restore_arg0(tcp, &state, tcp->inst[0]) < 0
1532 || restore_arg1(tcp, &state, tcp->inst[1]) < 0
1533 || arg_finish_change(tcp, &state))
1536 tcp->flags &= ~TCB_BPTSET;