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>
43 # include <asm/ptrace_offsets.h>
49 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
50 #elif defined(HAVE_LINUX_PTRACE_H)
51 # undef PTRACE_SYSCALL
52 # ifdef HAVE_STRUCT_IA64_FPREG
53 # define ia64_fpreg XXX_ia64_fpreg
55 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
56 # define pt_all_user_regs XXX_pt_all_user_regs
58 # include <linux/ptrace.h>
60 # undef pt_all_user_regs
64 string_to_uint(const char *str)
72 value = strtol(str, &error, 10);
73 if (errno || *error || value < 0 || (long)(int)value != value)
79 tv_nz(struct timeval *a)
81 return a->tv_sec || a->tv_usec;
85 tv_cmp(struct timeval *a, struct timeval *b)
87 if (a->tv_sec < b->tv_sec
88 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
90 if (a->tv_sec > b->tv_sec
91 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
97 tv_float(struct timeval *tv)
99 return tv->tv_sec + tv->tv_usec/1000000.0;
103 tv_add(struct timeval *tv, struct timeval *a, struct timeval *b)
105 tv->tv_sec = a->tv_sec + b->tv_sec;
106 tv->tv_usec = a->tv_usec + b->tv_usec;
107 if (tv->tv_usec >= 1000000) {
109 tv->tv_usec -= 1000000;
114 tv_sub(struct timeval *tv, struct timeval *a, struct timeval *b)
116 tv->tv_sec = a->tv_sec - b->tv_sec;
117 tv->tv_usec = a->tv_usec - b->tv_usec;
118 if (((long) tv->tv_usec) < 0) {
120 tv->tv_usec += 1000000;
125 tv_div(struct timeval *tv, struct timeval *a, int n)
127 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
128 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
129 tv->tv_usec %= 1000000;
133 tv_mul(struct timeval *tv, struct timeval *a, int n)
135 tv->tv_usec = a->tv_usec * n;
136 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
137 tv->tv_usec %= 1000000;
141 xlookup(const struct xlat *xlat, int val)
143 for (; xlat->str != NULL; xlat++)
144 if (xlat->val == val)
149 #if !defined HAVE_STPCPY
151 stpcpy(char *dst, const char *src)
153 while ((*dst = *src++) != '\0')
160 * Print entry in struct xlat table, if there.
163 printxval(const struct xlat *xlat, int val, const char *dflt)
165 const char *str = xlookup(xlat, val);
170 tprintf("%#x /* %s */", val, dflt);
174 * Print 64bit argument at position arg_no and return the index of the next
178 printllval(struct tcb *tcp, const char *format, int arg_no)
180 #if SIZEOF_LONG > 4 && SIZEOF_LONG == SIZEOF_LONG_LONG
181 # if SUPPORTED_PERSONALITIES > 1
182 if (current_wordsize > 4) {
184 tprintf(format, tcp->u_arg[arg_no]);
186 # if SUPPORTED_PERSONALITIES > 1
188 # if defined(AARCH64) || defined(POWERPC64)
189 /* Align arg_no to the next even number. */
190 arg_no = (arg_no + 1) & 0xe;
192 tprintf(format, LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]));
195 # endif /* SUPPORTED_PERSONALITIES */
196 #elif SIZEOF_LONG > 4
197 # error Unsupported configuration: SIZEOF_LONG > 4 && SIZEOF_LONG_LONG > SIZEOF_LONG
198 #elif defined LINUX_MIPSN32
199 tprintf(format, tcp->ext_arg[arg_no]);
202 if (current_personality == 0) {
203 tprintf(format, tcp->ext_arg[arg_no]);
206 tprintf(format, LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]));
210 # if defined(ARM) || defined(POWERPC)
211 /* Align arg_no to the next even number. */
212 arg_no = (arg_no + 1) & 0xe;
214 tprintf(format, LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]));
222 * Interpret `xlat' as an array of flags
223 * print the entries whose bits are on in `flags'
224 * return # of flags printed.
227 addflags(const struct xlat *xlat, int flags)
229 for (; xlat->str; xlat++) {
230 if (xlat->val && (flags & xlat->val) == xlat->val) {
231 tprintf("|%s", xlat->str);
236 tprintf("|%#x", flags);
241 * Interpret `xlat' as an array of flags.
242 * Print to static string the entries whose bits are on in `flags'
243 * Return static string.
246 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
248 static char outstr[1024];
252 outptr = stpcpy(outstr, prefix);
254 for (; xlat->str; xlat++) {
255 if ((flags & xlat->val) == xlat->val) {
258 outptr = stpcpy(outptr, xlat->str);
268 outptr += sprintf(outptr, "%#x", flags);
275 printflags(const struct xlat *xlat, int flags, const char *dflt)
280 if (flags == 0 && xlat->val == 0) {
286 for (n = 0; xlat->str; xlat++) {
287 if (xlat->val && (flags & xlat->val) == xlat->val) {
288 tprintf("%s%s", sep, xlat->str);
297 tprintf("%s%#x", sep, flags);
302 tprintf("%#x", flags);
304 tprintf(" /* %s */", dflt);
315 printnum(struct tcb *tcp, long addr, const char *fmt)
323 if (umove(tcp, addr, &num) < 0) {
324 tprintf("%#lx", addr);
333 printnum_int(struct tcb *tcp, long addr, const char *fmt)
341 if (umove(tcp, addr, &num) < 0) {
342 tprintf("%#lx", addr);
351 printfd(struct tcb *tcp, int fd)
353 char path[PATH_MAX + 1];
355 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0)
356 tprintf("%d<%s>", fd, path);
362 printuid(const char *text, unsigned long uid)
364 tprintf((uid == -1) ? "%s%ld" : "%s%lu", text, uid);
368 * Quote string `instr' of length `size'
369 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
370 * If `len' is -1, treat `instr' as a NUL-terminated string
371 * and quote at most (`size' - 1) bytes.
373 * Returns 0 if len == -1 and NUL was seen, 1 otherwise.
374 * Note that if len >= 0, always returns 1.
377 string_quote(const char *instr, char *outstr, long len, int size)
379 const unsigned char *ustr = (const unsigned char *) instr;
381 int usehex, c, i, eol;
383 eol = 0x100; /* this can never match a char */
393 /* Check for presence of symbol which require
394 to hex-quote the whole string. */
395 for (i = 0; i < size; ++i) {
397 /* Check for NUL-terminated string. */
401 /* Force hex unless c is printable or whitespace */
406 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
407 * They happen to have ASCII codes 9,10,11,12,13.
409 if (c < ' ' && (unsigned)(c - 9) >= 5) {
419 /* Hex-quote the whole string. */
420 for (i = 0; i < size; ++i) {
422 /* Check for NUL-terminated string. */
427 *s++ = "0123456789abcdef"[c >> 4];
428 *s++ = "0123456789abcdef"[c & 0xf];
431 for (i = 0; i < size; ++i) {
433 /* Check for NUL-terminated string. */
437 case '\"': case '\\':
462 if (c >= ' ' && c <= 0x7e)
468 && ustr[i + 1] >= '0'
469 && ustr[i + 1] <= '9'
472 *s++ = '0' + (c >> 6);
473 *s++ = '0' + ((c >> 3) & 0x7);
478 *s++ = '0' + (c >> 6);
479 *s++ = '0' + ((c >> 3) & 0x7);
482 *s++ = '0' + (c & 0x7);
492 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
493 if (len == -1 && ustr[i] == '\0') {
494 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
495 * but next char is NUL.
505 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
510 * Print path string specified by address `addr' and length `n'.
511 * If path length exceeds `n', append `...' to the output.
514 printpathn(struct tcb *tcp, long addr, int n)
516 char path[MAXPATHLEN + 1];
524 /* Cap path length to the path buffer size */
525 if (n > sizeof path - 1)
528 /* Fetch one byte more to find out whether path length > n. */
529 nul_seen = umovestr(tcp, addr, n + 1, path);
531 tprintf("%#lx", addr);
537 outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */
538 string_quote(path, outstr, -1, n);
546 printpath(struct tcb *tcp, long addr)
548 /* Size must correspond to char path[] size in printpathn */
549 printpathn(tcp, addr, MAXPATHLEN);
553 * Print string specified by address `addr' and length `len'.
554 * If `len' < 0, treat the string as a NUL-terminated string.
555 * If string length exceeds `max_strlen', append `...' to the output.
558 printstr(struct tcb *tcp, long addr, long len)
560 static char *str = NULL;
569 /* Allocate static buffers if they are not allocated yet. */
571 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
573 if (outstr_size / 4 != max_strlen)
575 str = malloc(max_strlen + 1);
578 outstr = malloc(outstr_size);
585 * Treat as a NUL-terminated string: fetch one byte more
586 * because string_quote() quotes one byte less.
588 size = max_strlen + 1;
589 if (umovestr(tcp, addr, size, str) < 0) {
590 tprintf("%#lx", addr);
596 if (size > (unsigned long)len)
597 size = (unsigned long)len;
598 if (umoven(tcp, addr, size, str) < 0) {
599 tprintf("%#lx", addr);
604 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
605 * or we were requested to print more than -s NUM chars)...
607 ellipsis = (string_quote(str, outstr, len, size) &&
608 (len < 0 || len > max_strlen));
617 dumpiov(struct tcb *tcp, int len, long addr)
619 #if SUPPORTED_PERSONALITIES > 1
621 struct { u_int32_t base; u_int32_t len; } *iov32;
622 struct { u_int64_t base; u_int64_t len; } *iov64;
624 #define iov iovu.iov64
626 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
627 #define iov_iov_base(i) \
628 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
629 #define iov_iov_len(i) \
630 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
633 #define sizeof_iov sizeof(*iov)
634 #define iov_iov_base(i) iov[i].iov_base
635 #define iov_iov_len(i) iov[i].iov_len
640 size = sizeof_iov * len;
641 /* Assuming no sane program has millions of iovs */
642 if ((unsigned)len > 1024*1024 /* insane or negative size? */
643 || (iov = malloc(size)) == NULL) {
644 fprintf(stderr, "Out of memory\n");
647 if (umoven(tcp, addr, size, (char *) iov) >= 0) {
648 for (i = 0; i < len; i++) {
649 /* include the buffer number to make it easy to
650 * match up the trace with the source */
651 tprintf(" * %lu bytes in buffer %d\n",
652 (unsigned long)iov_iov_len(i), i);
653 dumpstr(tcp, (long) iov_iov_base(i),
666 dumpstr(struct tcb *tcp, long addr, int len)
668 static int strsize = -1;
669 static unsigned char *str;
674 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
675 "1234567890123456") + /*in case I'm off by few:*/ 4)
676 /*align to 8 to make memset easier:*/ + 7) & -8
678 const unsigned char *src;
681 memset(outbuf, ' ', sizeof(outbuf));
683 if (strsize < len + 16) {
685 str = malloc(len + 16);
688 fprintf(stderr, "Out of memory\n");
694 if (umoven(tcp, addr, len, (char *) str) < 0)
697 /* Space-pad to 16 bytes */
709 *dst++ = "0123456789abcdef"[*src >> 4];
710 *dst++ = "0123456789abcdef"[*src & 0xf];
716 dst++; /* space is there by memset */
719 dst++; /* space is there by memset */
726 if (*src >= ' ' && *src < 0x7f)
733 tprintf(" | %05x %s |\n", i - 16, outbuf);
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 /* Have to avoid duplicating with the C library headers. */
759 static ssize_t strace_process_vm_readv(pid_t pid,
760 const struct iovec *lvec,
761 unsigned long liovcnt,
762 const struct iovec *rvec,
763 unsigned long riovcnt,
766 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
768 #define process_vm_readv strace_process_vm_readv
770 static bool process_vm_readv_not_supported = 1;
771 # define process_vm_readv(...) (errno = ENOSYS, -1)
774 #endif /* end of hack */
776 #define PAGMASK (~(PAGSIZ - 1))
778 * move `len' bytes of data from process `pid'
779 * at address `addr' to our space at `laddr'
782 umoven(struct tcb *tcp, long addr, int len, char *laddr)
788 char x[sizeof(long)];
791 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
792 if (current_wordsize < sizeof(addr))
793 addr &= (1ul << 8 * current_wordsize) - 1;
796 if (!process_vm_readv_not_supported) {
797 struct iovec local[1], remote[1];
800 local[0].iov_base = laddr;
801 remote[0].iov_base = (void*)addr;
802 local[0].iov_len = remote[0].iov_len = len;
803 r = process_vm_readv(pid, local, 1, remote, 1, 0);
807 error_msg("umoven: short read (%d < %d) @0x%lx",
813 process_vm_readv_not_supported = 1;
816 /* the process is gone */
818 case EFAULT: case EIO: case EPERM:
819 /* address space is inaccessible */
822 /* all the rest is strange and should be reported */
823 perror_msg("process_vm_readv");
829 if (addr & (sizeof(long) - 1)) {
830 /* addr not a multiple of sizeof(long) */
831 n = addr - (addr & -sizeof(long)); /* residue */
832 addr &= -sizeof(long); /* residue */
834 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
838 case ESRCH: case EINVAL:
839 /* these could be seen if the process is gone */
841 case EFAULT: case EIO: case EPERM:
842 /* address space is inaccessible */
845 /* all the rest is strange and should be reported */
846 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
850 m = MIN(sizeof(long) - n, len);
851 memcpy(laddr, &u.x[n], m);
852 addr += sizeof(long);
859 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
863 case ESRCH: case EINVAL:
864 /* these could be seen if the process is gone */
866 case EFAULT: case EIO: case EPERM:
867 /* address space is inaccessible */
869 perror_msg("umoven: short read (%d < %d) @0x%lx",
870 nread, nread + len, addr - nread);
874 /* all the rest is strange and should be reported */
875 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
879 m = MIN(sizeof(long), len);
880 memcpy(laddr, u.x, m);
881 addr += sizeof(long);
891 * Like `umove' but make the additional effort of looking
892 * for a terminating zero byte.
894 * Returns < 0 on error, > 0 if NUL was seen,
895 * (TODO if useful: return count of bytes including NUL),
896 * else 0 if len bytes were read but no NUL byte seen.
898 * Note: there is no guarantee we won't overwrite some bytes
899 * in laddr[] _after_ terminating NUL (but, of course,
900 * we never write past laddr[len-1]).
903 umovestr(struct tcb *tcp, long addr, int len, char *laddr)
906 const unsigned long x01010101 = 0x01010101ul;
907 const unsigned long x80808080 = 0x80808080ul;
908 #elif SIZEOF_LONG == 8
909 const unsigned long x01010101 = 0x0101010101010101ul;
910 const unsigned long x80808080 = 0x8080808080808080ul;
912 # error SIZEOF_LONG > 8
919 char x[sizeof(long)];
922 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
923 if (current_wordsize < sizeof(addr))
924 addr &= (1ul << 8 * current_wordsize) - 1;
928 if (!process_vm_readv_not_supported) {
929 struct iovec local[1], remote[1];
931 local[0].iov_base = laddr;
932 remote[0].iov_base = (void*)addr;
939 /* Don't read kilobytes: most strings are short */
943 /* Don't cross pages. I guess otherwise we can get EFAULT
944 * and fail to notice that terminating NUL lies
945 * in the existing (first) page.
946 * (I hope there aren't arches with pages < 4K)
948 end_in_page = ((addr + chunk_len) & 4095);
949 r = chunk_len - end_in_page;
950 if (r > 0) /* if chunk_len > end_in_page */
951 chunk_len = r; /* chunk_len -= end_in_page */
953 local[0].iov_len = remote[0].iov_len = chunk_len;
954 r = process_vm_readv(pid, local, 1, remote, 1, 0);
956 if (memchr(local[0].iov_base, '\0', r))
958 local[0].iov_base += r;
959 remote[0].iov_base += r;
966 process_vm_readv_not_supported = 1;
967 goto vm_readv_didnt_work;
969 /* the process is gone */
971 case EFAULT: case EIO: case EPERM:
972 /* address space is inaccessible */
974 perror_msg("umovestr: short read (%d < %d) @0x%lx",
975 nread, nread + len, addr);
979 /* all the rest is strange and should be reported */
980 perror_msg("process_vm_readv");
988 if (addr & (sizeof(long) - 1)) {
989 /* addr not a multiple of sizeof(long) */
990 n = addr - (addr & -sizeof(long)); /* residue */
991 addr &= -sizeof(long); /* residue */
993 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
997 case ESRCH: case EINVAL:
998 /* these could be seen if the process is gone */
1000 case EFAULT: case EIO: case EPERM:
1001 /* address space is inaccessible */
1004 /* all the rest is strange and should be reported */
1005 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1009 m = MIN(sizeof(long) - n, len);
1010 memcpy(laddr, &u.x[n], m);
1011 while (n & (sizeof(long) - 1))
1012 if (u.x[n++] == '\0')
1014 addr += sizeof(long);
1022 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
1026 case ESRCH: case EINVAL:
1027 /* these could be seen if the process is gone */
1029 case EFAULT: case EIO: case EPERM:
1030 /* address space is inaccessible */
1032 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1033 nread, nread + len, addr - nread);
1037 /* all the rest is strange and should be reported */
1038 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1042 m = MIN(sizeof(long), len);
1043 memcpy(laddr, u.x, m);
1044 /* "If a NUL char exists in this word" */
1045 if ((u.val - x01010101) & ~u.val & x80808080)
1047 addr += sizeof(long);
1056 upeek(struct tcb *tcp, long off, long *res)
1061 val = ptrace(PTRACE_PEEKUSER, tcp->pid, (char *) off, 0);
1062 if (val == -1 && errno) {
1063 if (errno != ESRCH) {
1064 perror_msg("upeek: PTRACE_PEEKUSER pid:%d @0x%lx)", tcp->pid, off);
1072 /* Note! On new kernels (about 2.5.46+), we use PTRACE_O_TRACECLONE
1073 * and PTRACE_O_TRACE[V]FORK for tracing children.
1074 * If you are adding a new arch which is only supported by newer kernels,
1075 * you most likely don't need to add any code below
1076 * beside a dummy "return 0" block in change_syscall().
1080 * These #if's are huge, please indent them correctly.
1081 * It's easy to get confused otherwise.
1084 #include "syscall.h"
1086 #ifndef CLONE_PTRACE
1087 # define CLONE_PTRACE 0x00002000
1090 # define CLONE_VFORK 0x00004000
1093 # define CLONE_VM 0x00000100
1098 typedef unsigned long *arg_setup_state;
1101 arg_setup(struct tcb *tcp, arg_setup_state *state)
1103 unsigned long cfm, sof, sol;
1107 /* Satisfy a false GCC warning. */
1112 if (upeek(tcp, PT_AR_BSP, &bsp) < 0)
1114 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1117 sof = (cfm >> 0) & 0x7f;
1118 sol = (cfm >> 7) & 0x7f;
1119 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
1121 *state = (unsigned long *) bsp;
1125 # define arg_finish_change(tcp, state) 0
1128 get_arg0(struct tcb *tcp, arg_setup_state *state, long *valp)
1133 ret = upeek(tcp, PT_R11, valp);
1136 (unsigned long) ia64_rse_skip_regs(*state, 0),
1137 sizeof(long), (void *) valp);
1142 get_arg1(struct tcb *tcp, arg_setup_state *state, long *valp)
1147 ret = upeek(tcp, PT_R9, valp);
1150 (unsigned long) ia64_rse_skip_regs(*state, 1),
1151 sizeof(long), (void *) valp);
1156 set_arg0(struct tcb *tcp, arg_setup_state *state, long val)
1158 int req = PTRACE_POKEDATA;
1162 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1163 req = PTRACE_POKEUSER;
1165 ap = ia64_rse_skip_regs(*state, 0);
1167 ptrace(req, tcp->pid, ap, val);
1168 return errno ? -1 : 0;
1172 set_arg1(struct tcb *tcp, arg_setup_state *state, long val)
1174 int req = PTRACE_POKEDATA;
1178 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1179 req = PTRACE_POKEUSER;
1181 ap = ia64_rse_skip_regs(*state, 1);
1183 ptrace(req, tcp->pid, ap, val);
1184 return errno ? -1 : 0;
1187 /* ia64 does not return the input arguments from functions (and syscalls)
1188 according to ia64 RSE (Register Stack Engine) behavior. */
1190 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1191 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1193 #elif defined(SPARC) || defined(SPARC64)
1195 # if defined(SPARC64)
1196 # undef PTRACE_GETREGS
1197 # define PTRACE_GETREGS PTRACE_GETREGS64
1198 # undef PTRACE_SETREGS
1199 # define PTRACE_SETREGS PTRACE_SETREGS64
1202 typedef struct pt_regs arg_setup_state;
1204 # define arg_setup(tcp, state) \
1205 (ptrace(PTRACE_GETREGS, (tcp)->pid, (char *) (state), 0))
1206 # define arg_finish_change(tcp, state) \
1207 (ptrace(PTRACE_SETREGS, (tcp)->pid, (char *) (state), 0))
1209 # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0)
1210 # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0)
1211 # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0)
1212 # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0)
1213 # define restore_arg0(tcp, state, val) 0
1215 #else /* other architectures */
1217 # if defined S390 || defined S390X
1218 /* Note: this is only true for the `clone' system call, which handles
1219 arguments specially. We could as well say that its first two arguments
1220 are swapped relative to other architectures, but that would just be
1221 another #ifdef in the calls. */
1222 # define arg0_offset PT_GPR3
1223 # define arg1_offset PT_ORIGGPR2
1224 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1225 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1226 # define arg0_index 1
1227 # define arg1_index 0
1228 # elif defined(ALPHA) || defined(MIPS)
1229 # define arg0_offset REG_A0
1230 # define arg1_offset (REG_A0+1)
1231 # elif defined(POWERPC)
1232 # define arg0_offset (sizeof(unsigned long)*PT_R3)
1233 # define arg1_offset (sizeof(unsigned long)*PT_R4)
1234 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1235 # elif defined(HPPA)
1236 # define arg0_offset PT_GR26
1237 # define arg1_offset (PT_GR26-4)
1238 # elif defined(X86_64) || defined(X32)
1239 # define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1240 # define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1242 # define arg0_offset (4*(REG_REG0+4))
1243 # define arg1_offset (4*(REG_REG0+5))
1244 # elif defined(SH64)
1245 /* ABI defines arg0 & 1 in r2 & r3 */
1246 # define arg0_offset (REG_OFFSET+16)
1247 # define arg1_offset (REG_OFFSET+24)
1248 # define restore_arg0(tcp, state, val) 0
1249 # elif defined CRISV10 || defined CRISV32
1250 # define arg0_offset (4*PT_R11)
1251 # define arg1_offset (4*PT_ORIG_R10)
1252 # define restore_arg0(tcp, state, val) 0
1253 # define restore_arg1(tcp, state, val) 0
1254 # define arg0_index 1
1255 # define arg1_index 0
1257 # define arg0_offset 0
1258 # define arg1_offset 4
1260 # define restore_arg0(tcp, state, val) 0
1264 typedef int arg_setup_state;
1266 # define arg_setup(tcp, state) (0)
1267 # define arg_finish_change(tcp, state) 0
1268 # define get_arg0(tcp, cookie, valp) (upeek((tcp), arg0_offset, (valp)))
1269 # define get_arg1(tcp, cookie, valp) (upeek((tcp), arg1_offset, (valp)))
1272 set_arg0(struct tcb *tcp, void *cookie, long val)
1274 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val);
1278 set_arg1(struct tcb *tcp, void *cookie, long val)
1280 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val);
1283 #endif /* architectures */
1285 #ifndef restore_arg0
1286 # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1288 #ifndef restore_arg1
1289 # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1293 # define arg0_index 0
1294 # define arg1_index 1
1298 change_syscall(struct tcb *tcp, arg_setup_state *state, int new)
1301 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_EAX * 4), new) < 0)
1304 #elif defined(X86_64)
1305 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_RAX * 8), new) < 0)
1309 /* setbpt/clearbpt never used: */
1310 /* X32 is only supported since about linux-3.0.30 */
1311 #elif defined(POWERPC)
1312 if (ptrace(PTRACE_POKEUSER, tcp->pid,
1313 (char*)(sizeof(unsigned long)*PT_R0), new) < 0)
1316 #elif defined(S390) || defined(S390X)
1317 /* s390 linux after 2.4.7 has a hook in entry.S to allow this */
1318 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GPR2), new) < 0)
1322 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_ORIG_D0), new) < 0)
1325 #elif defined(SPARC) || defined(SPARC64)
1326 state->u_regs[U_REG_G1] = new;
1329 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), new) < 0)
1332 #elif defined(ALPHA)
1333 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), new) < 0)
1336 #elif defined(AVR32)
1337 /* setbpt/clearbpt never used: */
1338 /* AVR32 is only supported since about linux-2.6.19 */
1340 /* setbpt/clearbpt never used: */
1341 /* Blackfin is only supported since about linux-2.6.23 */
1346 break; /* x86 SYS_fork */
1351 fprintf(stderr, "%s: unexpected syscall %d\n",
1355 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R1), new) < 0)
1357 } else if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R15), new) < 0)
1361 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR20), new) < 0)
1365 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*(REG_REG0+3)), new) < 0)
1369 /* Top half of reg encodes the no. of args n as 0x1n.
1370 Assume 0 args as kernel never actually checks... */
1371 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_SYSCALL),
1372 0x100000 | new) < 0)
1375 #elif defined(CRISV10) || defined(CRISV32)
1376 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_R9), new) < 0)
1380 /* Some kernels support this, some (pre-2.6.16 or so) don't. */
1381 # ifndef PTRACE_SET_SYSCALL
1382 # define PTRACE_SET_SYSCALL 23
1384 if (ptrace(PTRACE_SET_SYSCALL, tcp->pid, 0, new & 0xffff) != 0)
1387 #elif defined(AARCH64)
1388 /* setbpt/clearbpt never used: */
1389 /* AARCH64 is only supported since about linux-3.0.31 */
1391 /* setbpt/clearbpt never used: */
1392 /* Tilera CPUs are only supported since about linux-2.6.34 */
1393 #elif defined(MICROBLAZE)
1394 /* setbpt/clearbpt never used: */
1395 /* microblaze is only supported since about linux-2.6.30 */
1397 /* never reached; OR1K is only supported by kernels since 3.1.0. */
1398 #elif defined(METAG)
1399 /* setbpt/clearbpt never used: */
1400 /* Meta is only supported since linux-3.7 */
1401 #elif defined(XTENSA)
1402 /* setbpt/clearbpt never used: */
1403 /* Xtensa is only supported since linux 2.6.13 */
1405 #warning Do not know how to handle change_syscall for this architecture
1406 #endif /* architecture */
1411 setbpt(struct tcb *tcp)
1413 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
1414 arg_setup_state state;
1416 if (tcp->flags & TCB_BPTSET) {
1417 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1422 * It's a silly kludge to initialize this with a search at runtime.
1423 * But it's better than maintaining another magic thing in the
1424 * godforsaken tables.
1426 if (clone_scno[current_personality] == 0) {
1428 for (i = 0; i < nsyscalls; ++i)
1429 if (sysent[i].sys_func == sys_clone) {
1430 clone_scno[current_personality] = i;
1435 if (tcp->s_ent->sys_func == sys_fork ||
1436 tcp->s_ent->sys_func == sys_vfork) {
1437 if (arg_setup(tcp, &state) < 0
1438 || get_arg0(tcp, &state, &tcp->inst[0]) < 0
1439 || get_arg1(tcp, &state, &tcp->inst[1]) < 0
1440 || change_syscall(tcp, &state,
1441 clone_scno[current_personality]) < 0
1442 || set_arg0(tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1443 || set_arg1(tcp, &state, 0) < 0
1444 || arg_finish_change(tcp, &state) < 0)
1446 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1447 tcp->u_arg[arg1_index] = 0;
1448 tcp->flags |= TCB_BPTSET;
1452 if (tcp->s_ent->sys_func == sys_clone) {
1453 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1454 contrary to x86 vfork above. Even on x86 we turn the
1455 vfork semantics into plain fork - each application must not
1456 depend on the vfork specifics according to POSIX. We would
1457 hang waiting for the parent resume otherwise. We need to
1458 clear also CLONE_VM but only in the CLONE_VFORK case as
1459 otherwise we would break pthread_create. */
1461 long new_arg0 = (tcp->u_arg[arg0_index] | CLONE_PTRACE);
1462 if (new_arg0 & CLONE_VFORK)
1463 new_arg0 &= ~(unsigned long)(CLONE_VFORK | CLONE_VM);
1464 if (arg_setup(tcp, &state) < 0
1465 || set_arg0(tcp, &state, new_arg0) < 0
1466 || arg_finish_change(tcp, &state) < 0)
1468 tcp->inst[0] = tcp->u_arg[arg0_index];
1469 tcp->inst[1] = tcp->u_arg[arg1_index];
1470 tcp->flags |= TCB_BPTSET;
1474 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1475 tcp->scno, tcp->pid);
1480 clearbpt(struct tcb *tcp)
1482 arg_setup_state state;
1483 if (arg_setup(tcp, &state) < 0
1484 || change_syscall(tcp, &state, tcp->scno) < 0
1485 || restore_arg0(tcp, &state, tcp->inst[0]) < 0
1486 || restore_arg1(tcp, &state, tcp->inst[1]) < 0
1487 || arg_finish_change(tcp, &state))
1490 tcp->flags &= ~TCB_BPTSET;