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 defined(X86_64) || defined(POWERPC64) || defined(TILE) || defined(AARCH64) || \
181 defined(LINUX_MIPSN64) || defined(SPARC64)
182 if (current_personality == 0) {
183 /* Technically, format expects "long long",
184 * but we supply "long". We expect that
185 * on this arch, they are the same.
187 tprintf(format, tcp->u_arg[arg_no]);
190 # if defined(AARCH64) || defined(POWERPC64)
191 /* Align arg_no to the next even number. */
192 arg_no = (arg_no + 1) & 0xe;
194 tprintf(format, LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]));
197 #elif defined IA64 || defined ALPHA || defined S390X
198 /* Technically, format expects "long long",
199 * but we supply "long". We expect that
200 * on this arch, they are the same.
202 tprintf(format, tcp->u_arg[arg_no]);
205 if (current_personality == 0) {
206 tprintf(format, tcp->ext_arg[arg_no]);
209 tprintf(format, LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]));
212 #elif defined LINUX_MIPSN32
213 tprintf(format, tcp->ext_arg[arg_no]);
217 # error BUG: must not combine two args for long long on this arch
219 #if defined(ARM) || defined(POWERPC)
220 /* Align arg_no to the next even number. */
221 arg_no = (arg_no + 1) & 0xe;
223 tprintf(format, LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]));
230 * Interpret `xlat' as an array of flags
231 * print the entries whose bits are on in `flags'
232 * return # of flags printed.
235 addflags(const struct xlat *xlat, int flags)
237 for (; xlat->str; xlat++) {
238 if (xlat->val && (flags & xlat->val) == xlat->val) {
239 tprintf("|%s", xlat->str);
244 tprintf("|%#x", flags);
249 * Interpret `xlat' as an array of flags.
250 * Print to static string the entries whose bits are on in `flags'
251 * Return static string.
254 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
256 static char outstr[1024];
260 outptr = stpcpy(outstr, prefix);
262 for (; xlat->str; xlat++) {
263 if ((flags & xlat->val) == xlat->val) {
266 outptr = stpcpy(outptr, xlat->str);
276 outptr += sprintf(outptr, "%#x", flags);
283 printflags(const struct xlat *xlat, int flags, const char *dflt)
288 if (flags == 0 && xlat->val == 0) {
294 for (n = 0; xlat->str; xlat++) {
295 if (xlat->val && (flags & xlat->val) == xlat->val) {
296 tprintf("%s%s", sep, xlat->str);
305 tprintf("%s%#x", sep, flags);
310 tprintf("%#x", flags);
312 tprintf(" /* %s */", dflt);
323 printnum(struct tcb *tcp, long addr, const char *fmt)
331 if (umove(tcp, addr, &num) < 0) {
332 tprintf("%#lx", addr);
341 printnum_int(struct tcb *tcp, long addr, const char *fmt)
349 if (umove(tcp, addr, &num) < 0) {
350 tprintf("%#lx", addr);
359 printfd(struct tcb *tcp, int fd)
361 char path[PATH_MAX + 1];
363 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0)
364 tprintf("%d<%s>", fd, path);
370 printuid(const char *text, unsigned long uid)
372 tprintf((uid == -1) ? "%s%ld" : "%s%lu", text, uid);
376 * Quote string `instr' of length `size'
377 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
378 * If `len' is -1, treat `instr' as a NUL-terminated string
379 * and quote at most (`size' - 1) bytes.
381 * Returns 0 if len == -1 and NUL was seen, 1 otherwise.
382 * Note that if len >= 0, always returns 1.
385 string_quote(const char *instr, char *outstr, long len, int size)
387 const unsigned char *ustr = (const unsigned char *) instr;
389 int usehex, c, i, eol;
391 eol = 0x100; /* this can never match a char */
401 /* Check for presence of symbol which require
402 to hex-quote the whole string. */
403 for (i = 0; i < size; ++i) {
405 /* Check for NUL-terminated string. */
409 /* Force hex unless c is printable or whitespace */
414 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
415 * They happen to have ASCII codes 9,10,11,12,13.
417 if (c < ' ' && (unsigned)(c - 9) >= 5) {
427 /* Hex-quote the whole string. */
428 for (i = 0; i < size; ++i) {
430 /* Check for NUL-terminated string. */
435 *s++ = "0123456789abcdef"[c >> 4];
436 *s++ = "0123456789abcdef"[c & 0xf];
439 for (i = 0; i < size; ++i) {
441 /* Check for NUL-terminated string. */
445 case '\"': case '\\':
470 if (c >= ' ' && c <= 0x7e)
476 && ustr[i + 1] >= '0'
477 && ustr[i + 1] <= '9'
480 *s++ = '0' + (c >> 6);
481 *s++ = '0' + ((c >> 3) & 0x7);
486 *s++ = '0' + (c >> 6);
487 *s++ = '0' + ((c >> 3) & 0x7);
490 *s++ = '0' + (c & 0x7);
500 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
501 if (len == -1 && ustr[i] == '\0') {
502 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
503 * but next char is NUL.
513 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
518 * Print path string specified by address `addr' and length `n'.
519 * If path length exceeds `n', append `...' to the output.
522 printpathn(struct tcb *tcp, long addr, int n)
524 char path[MAXPATHLEN + 1];
532 /* Cap path length to the path buffer size */
533 if (n > sizeof path - 1)
536 /* Fetch one byte more to find out whether path length > n. */
537 nul_seen = umovestr(tcp, addr, n + 1, path);
539 tprintf("%#lx", addr);
545 outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */
546 string_quote(path, outstr, -1, n);
554 printpath(struct tcb *tcp, long addr)
556 /* Size must correspond to char path[] size in printpathn */
557 printpathn(tcp, addr, MAXPATHLEN);
561 * Print string specified by address `addr' and length `len'.
562 * If `len' < 0, treat the string as a NUL-terminated string.
563 * If string length exceeds `max_strlen', append `...' to the output.
566 printstr(struct tcb *tcp, long addr, long len)
568 static char *str = NULL;
577 /* Allocate static buffers if they are not allocated yet. */
579 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
581 if (outstr_size / 4 != max_strlen)
583 str = malloc(max_strlen + 1);
586 outstr = malloc(outstr_size);
593 * Treat as a NUL-terminated string: fetch one byte more
594 * because string_quote() quotes one byte less.
596 size = max_strlen + 1;
597 if (umovestr(tcp, addr, size, str) < 0) {
598 tprintf("%#lx", addr);
604 if (size > (unsigned long)len)
605 size = (unsigned long)len;
606 if (umoven(tcp, addr, size, str) < 0) {
607 tprintf("%#lx", addr);
612 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
613 * or we were requested to print more than -s NUM chars)...
615 ellipsis = (string_quote(str, outstr, len, size) &&
616 (len < 0 || len > max_strlen));
625 dumpiov(struct tcb *tcp, int len, long addr)
627 #if SUPPORTED_PERSONALITIES > 1
629 struct { u_int32_t base; u_int32_t len; } *iov32;
630 struct { u_int64_t base; u_int64_t len; } *iov64;
632 #define iov iovu.iov64
634 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
635 #define iov_iov_base(i) \
636 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
637 #define iov_iov_len(i) \
638 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
641 #define sizeof_iov sizeof(*iov)
642 #define iov_iov_base(i) iov[i].iov_base
643 #define iov_iov_len(i) iov[i].iov_len
648 size = sizeof_iov * len;
649 /* Assuming no sane program has millions of iovs */
650 if ((unsigned)len > 1024*1024 /* insane or negative size? */
651 || (iov = malloc(size)) == NULL) {
652 fprintf(stderr, "Out of memory\n");
655 if (umoven(tcp, addr, size, (char *) iov) >= 0) {
656 for (i = 0; i < len; i++) {
657 /* include the buffer number to make it easy to
658 * match up the trace with the source */
659 tprintf(" * %lu bytes in buffer %d\n",
660 (unsigned long)iov_iov_len(i), i);
661 dumpstr(tcp, (long) iov_iov_base(i),
674 dumpstr(struct tcb *tcp, long addr, int len)
676 static int strsize = -1;
677 static unsigned char *str;
682 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
683 "1234567890123456") + /*in case I'm off by few:*/ 4)
684 /*align to 8 to make memset easier:*/ + 7) & -8
686 const unsigned char *src;
689 memset(outbuf, ' ', sizeof(outbuf));
691 if (strsize < len + 16) {
693 str = malloc(len + 16);
696 fprintf(stderr, "Out of memory\n");
702 if (umoven(tcp, addr, len, (char *) str) < 0)
705 /* Space-pad to 16 bytes */
717 *dst++ = "0123456789abcdef"[*src >> 4];
718 *dst++ = "0123456789abcdef"[*src & 0xf];
724 dst++; /* space is there by memset */
727 dst++; /* space is there by memset */
734 if (*src >= ' ' && *src < 0x7f)
741 tprintf(" | %05x %s |\n", i - 16, outbuf);
745 #ifdef HAVE_PROCESS_VM_READV
746 /* C library supports this, but the kernel might not. */
747 static bool process_vm_readv_not_supported = 0;
750 /* Need to do this since process_vm_readv() is not yet available in libc.
751 * When libc is be updated, only "static bool process_vm_readv_not_supported"
752 * line should remain.
754 #if !defined(__NR_process_vm_readv)
756 # define __NR_process_vm_readv 347
757 # elif defined(X86_64)
758 # define __NR_process_vm_readv 310
759 # elif defined(POWERPC)
760 # define __NR_process_vm_readv 351
764 #if defined(__NR_process_vm_readv)
765 static bool process_vm_readv_not_supported = 0;
766 /* Have to avoid duplicating with the C library headers. */
767 static ssize_t strace_process_vm_readv(pid_t pid,
768 const struct iovec *lvec,
769 unsigned long liovcnt,
770 const struct iovec *rvec,
771 unsigned long riovcnt,
774 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
776 #define process_vm_readv strace_process_vm_readv
778 static bool process_vm_readv_not_supported = 1;
779 # define process_vm_readv(...) (errno = ENOSYS, -1)
782 #endif /* end of hack */
784 #define PAGMASK (~(PAGSIZ - 1))
786 * move `len' bytes of data from process `pid'
787 * at address `addr' to our space at `laddr'
790 umoven(struct tcb *tcp, long addr, int len, char *laddr)
796 char x[sizeof(long)];
799 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
800 if (current_wordsize < sizeof(addr))
801 addr &= (1ul << 8 * current_wordsize) - 1;
804 if (!process_vm_readv_not_supported) {
805 struct iovec local[1], remote[1];
808 local[0].iov_base = laddr;
809 remote[0].iov_base = (void*)addr;
810 local[0].iov_len = remote[0].iov_len = len;
811 r = process_vm_readv(pid, local, 1, remote, 1, 0);
815 error_msg("umoven: short read (%d < %d) @0x%lx",
821 process_vm_readv_not_supported = 1;
824 /* the process is gone */
826 case EFAULT: case EIO: case EPERM:
827 /* address space is inaccessible */
830 /* all the rest is strange and should be reported */
831 perror_msg("process_vm_readv");
837 if (addr & (sizeof(long) - 1)) {
838 /* addr not a multiple of sizeof(long) */
839 n = addr - (addr & -sizeof(long)); /* residue */
840 addr &= -sizeof(long); /* residue */
842 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
846 case ESRCH: case EINVAL:
847 /* these could be seen if the process is gone */
849 case EFAULT: case EIO: case EPERM:
850 /* address space is inaccessible */
853 /* all the rest is strange and should be reported */
854 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
858 m = MIN(sizeof(long) - n, len);
859 memcpy(laddr, &u.x[n], m);
860 addr += sizeof(long);
867 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
871 case ESRCH: case EINVAL:
872 /* these could be seen if the process is gone */
874 case EFAULT: case EIO: case EPERM:
875 /* address space is inaccessible */
877 perror_msg("umoven: short read (%d < %d) @0x%lx",
878 nread, nread + len, addr - nread);
882 /* all the rest is strange and should be reported */
883 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
887 m = MIN(sizeof(long), len);
888 memcpy(laddr, u.x, m);
889 addr += sizeof(long);
899 * Like `umove' but make the additional effort of looking
900 * for a terminating zero byte.
902 * Returns < 0 on error, > 0 if NUL was seen,
903 * (TODO if useful: return count of bytes including NUL),
904 * else 0 if len bytes were read but no NUL byte seen.
906 * Note: there is no guarantee we won't overwrite some bytes
907 * in laddr[] _after_ terminating NUL (but, of course,
908 * we never write past laddr[len-1]).
911 umovestr(struct tcb *tcp, long addr, int len, char *laddr)
914 const unsigned long x01010101 = 0x01010101ul;
915 const unsigned long x80808080 = 0x80808080ul;
916 #elif SIZEOF_LONG == 8
917 const unsigned long x01010101 = 0x0101010101010101ul;
918 const unsigned long x80808080 = 0x8080808080808080ul;
920 # error SIZEOF_LONG > 8
927 char x[sizeof(long)];
930 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
931 if (current_wordsize < sizeof(addr))
932 addr &= (1ul << 8 * current_wordsize) - 1;
936 if (!process_vm_readv_not_supported) {
937 struct iovec local[1], remote[1];
939 local[0].iov_base = laddr;
940 remote[0].iov_base = (void*)addr;
947 /* Don't read kilobytes: most strings are short */
951 /* Don't cross pages. I guess otherwise we can get EFAULT
952 * and fail to notice that terminating NUL lies
953 * in the existing (first) page.
954 * (I hope there aren't arches with pages < 4K)
956 end_in_page = ((addr + chunk_len) & 4095);
957 r = chunk_len - end_in_page;
958 if (r > 0) /* if chunk_len > end_in_page */
959 chunk_len = r; /* chunk_len -= end_in_page */
961 local[0].iov_len = remote[0].iov_len = chunk_len;
962 r = process_vm_readv(pid, local, 1, remote, 1, 0);
964 if (memchr(local[0].iov_base, '\0', r))
966 local[0].iov_base += r;
967 remote[0].iov_base += r;
974 process_vm_readv_not_supported = 1;
975 goto vm_readv_didnt_work;
977 /* the process is gone */
979 case EFAULT: case EIO: case EPERM:
980 /* address space is inaccessible */
982 perror_msg("umovestr: short read (%d < %d) @0x%lx",
983 nread, nread + len, addr);
987 /* all the rest is strange and should be reported */
988 perror_msg("process_vm_readv");
996 if (addr & (sizeof(long) - 1)) {
997 /* addr not a multiple of sizeof(long) */
998 n = addr - (addr & -sizeof(long)); /* residue */
999 addr &= -sizeof(long); /* residue */
1001 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
1005 case ESRCH: case EINVAL:
1006 /* these could be seen if the process is gone */
1008 case EFAULT: case EIO: case EPERM:
1009 /* address space is inaccessible */
1012 /* all the rest is strange and should be reported */
1013 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1017 m = MIN(sizeof(long) - n, len);
1018 memcpy(laddr, &u.x[n], m);
1019 while (n & (sizeof(long) - 1))
1020 if (u.x[n++] == '\0')
1022 addr += sizeof(long);
1030 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
1034 case ESRCH: case EINVAL:
1035 /* these could be seen if the process is gone */
1037 case EFAULT: case EIO: case EPERM:
1038 /* address space is inaccessible */
1040 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1041 nread, nread + len, addr - nread);
1045 /* all the rest is strange and should be reported */
1046 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1050 m = MIN(sizeof(long), len);
1051 memcpy(laddr, u.x, m);
1052 /* "If a NUL char exists in this word" */
1053 if ((u.val - x01010101) & ~u.val & x80808080)
1055 addr += sizeof(long);
1064 upeek(struct tcb *tcp, long off, long *res)
1069 val = ptrace(PTRACE_PEEKUSER, tcp->pid, (char *) off, 0);
1070 if (val == -1 && errno) {
1071 if (errno != ESRCH) {
1072 perror_msg("upeek: PTRACE_PEEKUSER pid:%d @0x%lx)", tcp->pid, off);
1080 /* Note! On new kernels (about 2.5.46+), we use PTRACE_O_TRACECLONE
1081 * and PTRACE_O_TRACE[V]FORK for tracing children.
1082 * If you are adding a new arch which is only supported by newer kernels,
1083 * you most likely don't need to add any code below
1084 * beside a dummy "return 0" block in change_syscall().
1088 * These #if's are huge, please indent them correctly.
1089 * It's easy to get confused otherwise.
1092 #include "syscall.h"
1094 #ifndef CLONE_PTRACE
1095 # define CLONE_PTRACE 0x00002000
1098 # define CLONE_VFORK 0x00004000
1101 # define CLONE_VM 0x00000100
1106 typedef unsigned long *arg_setup_state;
1109 arg_setup(struct tcb *tcp, arg_setup_state *state)
1111 unsigned long cfm, sof, sol;
1115 /* Satisfy a false GCC warning. */
1120 if (upeek(tcp, PT_AR_BSP, &bsp) < 0)
1122 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1125 sof = (cfm >> 0) & 0x7f;
1126 sol = (cfm >> 7) & 0x7f;
1127 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
1129 *state = (unsigned long *) bsp;
1133 # define arg_finish_change(tcp, state) 0
1136 get_arg0(struct tcb *tcp, arg_setup_state *state, long *valp)
1141 ret = upeek(tcp, PT_R11, valp);
1144 (unsigned long) ia64_rse_skip_regs(*state, 0),
1145 sizeof(long), (void *) valp);
1150 get_arg1(struct tcb *tcp, arg_setup_state *state, long *valp)
1155 ret = upeek(tcp, PT_R9, valp);
1158 (unsigned long) ia64_rse_skip_regs(*state, 1),
1159 sizeof(long), (void *) valp);
1164 set_arg0(struct tcb *tcp, arg_setup_state *state, long val)
1166 int req = PTRACE_POKEDATA;
1170 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1171 req = PTRACE_POKEUSER;
1173 ap = ia64_rse_skip_regs(*state, 0);
1175 ptrace(req, tcp->pid, ap, val);
1176 return errno ? -1 : 0;
1180 set_arg1(struct tcb *tcp, arg_setup_state *state, long val)
1182 int req = PTRACE_POKEDATA;
1186 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1187 req = PTRACE_POKEUSER;
1189 ap = ia64_rse_skip_regs(*state, 1);
1191 ptrace(req, tcp->pid, ap, val);
1192 return errno ? -1 : 0;
1195 /* ia64 does not return the input arguments from functions (and syscalls)
1196 according to ia64 RSE (Register Stack Engine) behavior. */
1198 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1199 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1201 #elif defined(SPARC) || defined(SPARC64)
1203 # if defined(SPARC64)
1204 # undef PTRACE_GETREGS
1205 # define PTRACE_GETREGS PTRACE_GETREGS64
1206 # undef PTRACE_SETREGS
1207 # define PTRACE_SETREGS PTRACE_SETREGS64
1210 typedef struct pt_regs arg_setup_state;
1212 # define arg_setup(tcp, state) \
1213 (ptrace(PTRACE_GETREGS, (tcp)->pid, (char *) (state), 0))
1214 # define arg_finish_change(tcp, state) \
1215 (ptrace(PTRACE_SETREGS, (tcp)->pid, (char *) (state), 0))
1217 # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0)
1218 # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0)
1219 # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0)
1220 # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0)
1221 # define restore_arg0(tcp, state, val) 0
1223 #else /* other architectures */
1225 # if defined S390 || defined S390X
1226 /* Note: this is only true for the `clone' system call, which handles
1227 arguments specially. We could as well say that its first two arguments
1228 are swapped relative to other architectures, but that would just be
1229 another #ifdef in the calls. */
1230 # define arg0_offset PT_GPR3
1231 # define arg1_offset PT_ORIGGPR2
1232 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1233 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1234 # define arg0_index 1
1235 # define arg1_index 0
1236 # elif defined(ALPHA) || defined(MIPS)
1237 # define arg0_offset REG_A0
1238 # define arg1_offset (REG_A0+1)
1239 # elif defined(POWERPC)
1240 # define arg0_offset (sizeof(unsigned long)*PT_R3)
1241 # define arg1_offset (sizeof(unsigned long)*PT_R4)
1242 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1243 # elif defined(HPPA)
1244 # define arg0_offset PT_GR26
1245 # define arg1_offset (PT_GR26-4)
1246 # elif defined(X86_64) || defined(X32)
1247 # define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1248 # define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1250 # define arg0_offset (4*(REG_REG0+4))
1251 # define arg1_offset (4*(REG_REG0+5))
1252 # elif defined(SH64)
1253 /* ABI defines arg0 & 1 in r2 & r3 */
1254 # define arg0_offset (REG_OFFSET+16)
1255 # define arg1_offset (REG_OFFSET+24)
1256 # define restore_arg0(tcp, state, val) 0
1257 # elif defined CRISV10 || defined CRISV32
1258 # define arg0_offset (4*PT_R11)
1259 # define arg1_offset (4*PT_ORIG_R10)
1260 # define restore_arg0(tcp, state, val) 0
1261 # define restore_arg1(tcp, state, val) 0
1262 # define arg0_index 1
1263 # define arg1_index 0
1265 # define arg0_offset 0
1266 # define arg1_offset 4
1268 # define restore_arg0(tcp, state, val) 0
1272 typedef int arg_setup_state;
1274 # define arg_setup(tcp, state) (0)
1275 # define arg_finish_change(tcp, state) 0
1276 # define get_arg0(tcp, cookie, valp) (upeek((tcp), arg0_offset, (valp)))
1277 # define get_arg1(tcp, cookie, valp) (upeek((tcp), arg1_offset, (valp)))
1280 set_arg0(struct tcb *tcp, void *cookie, long val)
1282 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val);
1286 set_arg1(struct tcb *tcp, void *cookie, long val)
1288 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val);
1291 #endif /* architectures */
1293 #ifndef restore_arg0
1294 # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1296 #ifndef restore_arg1
1297 # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1301 # define arg0_index 0
1302 # define arg1_index 1
1306 change_syscall(struct tcb *tcp, arg_setup_state *state, int new)
1309 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_EAX * 4), new) < 0)
1312 #elif defined(X86_64)
1313 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_RAX * 8), new) < 0)
1317 /* setbpt/clearbpt never used: */
1318 /* X32 is only supported since about linux-3.0.30 */
1319 #elif defined(POWERPC)
1320 if (ptrace(PTRACE_POKEUSER, tcp->pid,
1321 (char*)(sizeof(unsigned long)*PT_R0), new) < 0)
1324 #elif defined(S390) || defined(S390X)
1325 /* s390 linux after 2.4.7 has a hook in entry.S to allow this */
1326 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GPR2), new) < 0)
1330 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_ORIG_D0), new) < 0)
1333 #elif defined(SPARC) || defined(SPARC64)
1334 state->u_regs[U_REG_G1] = new;
1337 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), new) < 0)
1340 #elif defined(ALPHA)
1341 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), new) < 0)
1344 #elif defined(AVR32)
1345 /* setbpt/clearbpt never used: */
1346 /* AVR32 is only supported since about linux-2.6.19 */
1348 /* setbpt/clearbpt never used: */
1349 /* Blackfin is only supported since about linux-2.6.23 */
1354 break; /* x86 SYS_fork */
1359 fprintf(stderr, "%s: unexpected syscall %d\n",
1363 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R1), new) < 0)
1365 } else if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R15), new) < 0)
1369 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR20), new) < 0)
1373 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*(REG_REG0+3)), new) < 0)
1377 /* Top half of reg encodes the no. of args n as 0x1n.
1378 Assume 0 args as kernel never actually checks... */
1379 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_SYSCALL),
1380 0x100000 | new) < 0)
1383 #elif defined(CRISV10) || defined(CRISV32)
1384 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_R9), new) < 0)
1388 /* Some kernels support this, some (pre-2.6.16 or so) don't. */
1389 # ifndef PTRACE_SET_SYSCALL
1390 # define PTRACE_SET_SYSCALL 23
1392 if (ptrace(PTRACE_SET_SYSCALL, tcp->pid, 0, new & 0xffff) != 0)
1395 #elif defined(AARCH64)
1396 /* setbpt/clearbpt never used: */
1397 /* AARCH64 is only supported since about linux-3.0.31 */
1399 /* setbpt/clearbpt never used: */
1400 /* Tilera CPUs are only supported since about linux-2.6.34 */
1401 #elif defined(MICROBLAZE)
1402 /* setbpt/clearbpt never used: */
1403 /* microblaze is only supported since about linux-2.6.30 */
1405 /* never reached; OR1K is only supported by kernels since 3.1.0. */
1406 #elif defined(METAG)
1407 /* setbpt/clearbpt never used: */
1408 /* Meta is only supported since linux-3.7 */
1409 #elif defined(XTENSA)
1410 /* setbpt/clearbpt never used: */
1411 /* Xtensa is only supported since linux 2.6.13 */
1413 #warning Do not know how to handle change_syscall for this architecture
1414 #endif /* architecture */
1419 setbpt(struct tcb *tcp)
1421 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
1422 arg_setup_state state;
1424 if (tcp->flags & TCB_BPTSET) {
1425 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1430 * It's a silly kludge to initialize this with a search at runtime.
1431 * But it's better than maintaining another magic thing in the
1432 * godforsaken tables.
1434 if (clone_scno[current_personality] == 0) {
1436 for (i = 0; i < nsyscalls; ++i)
1437 if (sysent[i].sys_func == sys_clone) {
1438 clone_scno[current_personality] = i;
1443 if (tcp->s_ent->sys_func == sys_fork ||
1444 tcp->s_ent->sys_func == sys_vfork) {
1445 if (arg_setup(tcp, &state) < 0
1446 || get_arg0(tcp, &state, &tcp->inst[0]) < 0
1447 || get_arg1(tcp, &state, &tcp->inst[1]) < 0
1448 || change_syscall(tcp, &state,
1449 clone_scno[current_personality]) < 0
1450 || set_arg0(tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1451 || set_arg1(tcp, &state, 0) < 0
1452 || arg_finish_change(tcp, &state) < 0)
1454 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1455 tcp->u_arg[arg1_index] = 0;
1456 tcp->flags |= TCB_BPTSET;
1460 if (tcp->s_ent->sys_func == sys_clone) {
1461 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1462 contrary to x86 vfork above. Even on x86 we turn the
1463 vfork semantics into plain fork - each application must not
1464 depend on the vfork specifics according to POSIX. We would
1465 hang waiting for the parent resume otherwise. We need to
1466 clear also CLONE_VM but only in the CLONE_VFORK case as
1467 otherwise we would break pthread_create. */
1469 long new_arg0 = (tcp->u_arg[arg0_index] | CLONE_PTRACE);
1470 if (new_arg0 & CLONE_VFORK)
1471 new_arg0 &= ~(unsigned long)(CLONE_VFORK | CLONE_VM);
1472 if (arg_setup(tcp, &state) < 0
1473 || set_arg0(tcp, &state, new_arg0) < 0
1474 || arg_finish_change(tcp, &state) < 0)
1476 tcp->inst[0] = tcp->u_arg[arg0_index];
1477 tcp->inst[1] = tcp->u_arg[arg1_index];
1478 tcp->flags |= TCB_BPTSET;
1482 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1483 tcp->scno, tcp->pid);
1488 clearbpt(struct tcb *tcp)
1490 arg_setup_state state;
1491 if (arg_setup(tcp, &state) < 0
1492 || change_syscall(tcp, &state, tcp->scno) < 0
1493 || restore_arg0(tcp, &state, tcp->inst[0]) < 0
1494 || restore_arg1(tcp, &state, tcp->inst[1]) < 0
1495 || arg_finish_change(tcp, &state))
1498 tcp->flags &= ~TCB_BPTSET;