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 #elif defined(HAVE_LINUX_PTRACE_H)
50 # undef PTRACE_SYSCALL
51 # ifdef HAVE_STRUCT_IA64_FPREG
52 # define ia64_fpreg XXX_ia64_fpreg
54 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
55 # define pt_all_user_regs XXX_pt_all_user_regs
57 # ifdef HAVE_STRUCT_PTRACE_PEEKSIGINFO_ARGS
58 # define ptrace_peeksiginfo_args XXX_ptrace_peeksiginfo_args
60 # include <linux/ptrace.h>
61 # undef ptrace_peeksiginfo_args
63 # undef pt_all_user_regs
67 string_to_uint(const char *str)
75 value = strtol(str, &error, 10);
76 if (errno || *error || value < 0 || (long)(int)value != value)
82 tv_nz(const struct timeval *a)
84 return a->tv_sec || a->tv_usec;
88 tv_cmp(const struct timeval *a, const struct timeval *b)
90 if (a->tv_sec < b->tv_sec
91 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
93 if (a->tv_sec > b->tv_sec
94 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
100 tv_float(const struct timeval *tv)
102 return tv->tv_sec + tv->tv_usec/1000000.0;
106 tv_add(struct timeval *tv, const struct timeval *a, const struct timeval *b)
108 tv->tv_sec = a->tv_sec + b->tv_sec;
109 tv->tv_usec = a->tv_usec + b->tv_usec;
110 if (tv->tv_usec >= 1000000) {
112 tv->tv_usec -= 1000000;
117 tv_sub(struct timeval *tv, const struct timeval *a, const struct timeval *b)
119 tv->tv_sec = a->tv_sec - b->tv_sec;
120 tv->tv_usec = a->tv_usec - b->tv_usec;
121 if (((long) tv->tv_usec) < 0) {
123 tv->tv_usec += 1000000;
128 tv_div(struct timeval *tv, const struct timeval *a, int n)
130 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
131 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
132 tv->tv_usec %= 1000000;
136 tv_mul(struct timeval *tv, const struct timeval *a, int n)
138 tv->tv_usec = a->tv_usec * n;
139 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
140 tv->tv_usec %= 1000000;
144 xlookup(const struct xlat *xlat, const unsigned int val)
146 for (; xlat->str != NULL; xlat++)
147 if (xlat->val == val)
153 xlat_bsearch_compare(const void *a, const void *b)
155 const unsigned int val1 = (const unsigned long) a;
156 const unsigned int val2 = ((const struct xlat *) b)->val;
157 return (val1 > val2) ? 1 : (val1 < val2) ? -1 : 0;
161 xlat_search(const struct xlat *xlat, const size_t nmemb, const unsigned int val)
163 const struct xlat *e =
164 bsearch((const void*) (const unsigned long) val,
165 xlat, nmemb, sizeof(*xlat), xlat_bsearch_compare);
167 return e ? e->str : NULL;
170 #if !defined HAVE_STPCPY
172 stpcpy(char *dst, const char *src)
174 while ((*dst = *src++) != '\0')
180 /* Find a next bit which is set.
181 * Starts testing at cur_bit.
182 * Returns -1 if no more bits are set.
184 * We never touch bytes we don't need to.
185 * On big-endian, array is assumed to consist of
186 * current_wordsize wide words: for example, is current_wordsize is 4,
187 * the bytes are walked in 3,2,1,0, 7,6,5,4, 11,10,9,8 ... sequence.
188 * On little-endian machines, word size is immaterial.
191 next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits)
193 const unsigned endian = 1;
194 int little_endian = *(char*)&endian;
196 const uint8_t *array = bit_array;
197 unsigned pos = cur_bit / 8;
198 unsigned pos_xor_mask = little_endian ? 0 : current_wordsize-1;
204 if (cur_bit >= size_bits)
206 cur_byte = array[pos ^ pos_xor_mask];
208 cur_bit = (cur_bit + 8) & (-8);
212 bitmask = 1 << (cur_bit & 7);
214 if (cur_byte & bitmask)
217 if (cur_bit >= size_bits)
220 /* This check *can't be* optimized out: */
228 * Print entry in struct xlat table, if there.
231 printxval(const struct xlat *xlat, const unsigned int val, const char *dflt)
233 const char *str = xlookup(xlat, val);
238 tprintf("%#x /* %s */", val, dflt);
242 * Print 64bit argument at position arg_no and return the index of the next
246 printllval(struct tcb *tcp, const char *format, int arg_no)
248 #if SIZEOF_LONG > 4 && SIZEOF_LONG == SIZEOF_LONG_LONG
249 # if SUPPORTED_PERSONALITIES > 1
250 if (current_wordsize > 4) {
252 tprintf(format, tcp->u_arg[arg_no]);
254 # if SUPPORTED_PERSONALITIES > 1
256 # if defined(AARCH64) || defined(POWERPC64)
257 /* Align arg_no to the next even number. */
258 arg_no = (arg_no + 1) & 0xe;
260 tprintf(format, LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]));
263 # endif /* SUPPORTED_PERSONALITIES */
264 #elif SIZEOF_LONG > 4
265 # error Unsupported configuration: SIZEOF_LONG > 4 && SIZEOF_LONG_LONG > SIZEOF_LONG
266 #elif defined LINUX_MIPSN32
267 tprintf(format, tcp->ext_arg[arg_no]);
270 if (current_personality == 0) {
271 tprintf(format, tcp->ext_arg[arg_no]);
274 tprintf(format, LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]));
278 # if defined __ARM_EABI__ || \
279 defined LINUX_MIPSO32 || \
282 /* Align arg_no to the next even number. */
283 arg_no = (arg_no + 1) & 0xe;
285 tprintf(format, LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]));
293 * Interpret `xlat' as an array of flags
294 * print the entries whose bits are on in `flags'
295 * return # of flags printed.
298 addflags(const struct xlat *xlat, int flags)
300 for (; xlat->str; xlat++) {
301 if (xlat->val && (flags & xlat->val) == xlat->val) {
302 tprintf("|%s", xlat->str);
307 tprintf("|%#x", flags);
312 * Interpret `xlat' as an array of flags.
313 * Print to static string the entries whose bits are on in `flags'
314 * Return static string.
317 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
319 static char outstr[1024];
323 outptr = stpcpy(outstr, prefix);
325 for (; xlat->str; xlat++) {
326 if ((flags & xlat->val) == xlat->val) {
329 outptr = stpcpy(outptr, xlat->str);
339 outptr += sprintf(outptr, "%#x", flags);
346 printflags(const struct xlat *xlat, int flags, const char *dflt)
351 if (flags == 0 && xlat->val == 0) {
357 for (n = 0; xlat->str; xlat++) {
358 if (xlat->val && (flags & xlat->val) == xlat->val) {
359 tprintf("%s%s", sep, xlat->str);
368 tprintf("%s%#x", sep, flags);
373 tprintf("%#x", flags);
375 tprintf(" /* %s */", dflt);
386 printnum(struct tcb *tcp, long addr, const char *fmt)
394 if (umove(tcp, addr, &num) < 0) {
395 tprintf("%#lx", addr);
404 printnum_int(struct tcb *tcp, long addr, const char *fmt)
412 if (umove(tcp, addr, &num) < 0) {
413 tprintf("%#lx", addr);
422 printfd(struct tcb *tcp, int fd)
424 char path[PATH_MAX + 1];
425 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
426 static const char socket_prefix[] = "socket:[";
427 const size_t socket_prefix_len = sizeof(socket_prefix) - 1;
430 if (show_fd_path > 1 &&
431 strncmp(path, socket_prefix, socket_prefix_len) == 0 &&
432 path[(path_len = strlen(path)) - 1] == ']') {
433 unsigned long inodenr;
434 inodenr = strtoul(path + socket_prefix_len, NULL, 10);
436 if (!print_sockaddr_by_inode(inodenr))
440 tprintf("%d<%s>", fd, path);
447 printuid(const char *text, unsigned long uid)
449 tprintf(((long) uid == -1) ? "%s%ld" : "%s%lu", text, uid);
453 * Quote string `instr' of length `size'
454 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
455 * If `len' is -1, treat `instr' as a NUL-terminated string
456 * and quote at most (`size' - 1) bytes.
458 * Returns 0 if len == -1 and NUL was seen, 1 otherwise.
459 * Note that if len >= 0, always returns 1.
462 string_quote(const char *instr, char *outstr, long len, int size)
464 const unsigned char *ustr = (const unsigned char *) instr;
466 int usehex, c, i, eol;
468 eol = 0x100; /* this can never match a char */
478 /* Check for presence of symbol which require
479 to hex-quote the whole string. */
480 for (i = 0; i < size; ++i) {
482 /* Check for NUL-terminated string. */
486 /* Force hex unless c is printable or whitespace */
491 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
492 * They happen to have ASCII codes 9,10,11,12,13.
494 if (c < ' ' && (unsigned)(c - 9) >= 5) {
504 /* Hex-quote the whole string. */
505 for (i = 0; i < size; ++i) {
507 /* Check for NUL-terminated string. */
512 *s++ = "0123456789abcdef"[c >> 4];
513 *s++ = "0123456789abcdef"[c & 0xf];
516 for (i = 0; i < size; ++i) {
518 /* Check for NUL-terminated string. */
522 case '\"': case '\\':
547 if (c >= ' ' && c <= 0x7e)
553 && ustr[i + 1] >= '0'
554 && ustr[i + 1] <= '9'
557 *s++ = '0' + (c >> 6);
558 *s++ = '0' + ((c >> 3) & 0x7);
563 *s++ = '0' + (c >> 6);
564 *s++ = '0' + ((c >> 3) & 0x7);
567 *s++ = '0' + (c & 0x7);
577 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
578 if (len == -1 && ustr[i] == '\0') {
579 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
580 * but next char is NUL.
590 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
595 * Print path string specified by address `addr' and length `n'.
596 * If path length exceeds `n', append `...' to the output.
599 printpathn(struct tcb *tcp, long addr, unsigned int n)
601 char path[MAXPATHLEN + 1];
609 /* Cap path length to the path buffer size */
610 if (n > sizeof path - 1)
613 /* Fetch one byte more to find out whether path length > n. */
614 nul_seen = umovestr(tcp, addr, n + 1, path);
616 tprintf("%#lx", addr);
622 outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */
623 string_quote(path, outstr, -1, n);
631 printpath(struct tcb *tcp, long addr)
633 /* Size must correspond to char path[] size in printpathn */
634 printpathn(tcp, addr, MAXPATHLEN);
638 * Print string specified by address `addr' and length `len'.
639 * If `len' < 0, treat the string as a NUL-terminated string.
640 * If string length exceeds `max_strlen', append `...' to the output.
643 printstr(struct tcb *tcp, long addr, long len)
645 static char *str = NULL;
654 /* Allocate static buffers if they are not allocated yet. */
656 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
658 if (outstr_size / 4 != max_strlen)
660 str = malloc(max_strlen + 1);
663 outstr = malloc(outstr_size);
670 * Treat as a NUL-terminated string: fetch one byte more
671 * because string_quote() quotes one byte less.
673 size = max_strlen + 1;
674 if (umovestr(tcp, addr, size, str) < 0) {
675 tprintf("%#lx", addr);
681 if (size > (unsigned long)len)
682 size = (unsigned long)len;
683 if (umoven(tcp, addr, size, str) < 0) {
684 tprintf("%#lx", addr);
689 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
690 * or we were requested to print more than -s NUM chars)...
692 ellipsis = (string_quote(str, outstr, len, size) &&
693 (len < 0 || (unsigned long) len > max_strlen));
702 dumpiov(struct tcb *tcp, int len, long addr)
704 #if SUPPORTED_PERSONALITIES > 1
706 struct { u_int32_t base; u_int32_t len; } *iov32;
707 struct { u_int64_t base; u_int64_t len; } *iov64;
709 #define iov iovu.iov64
711 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
712 #define iov_iov_base(i) \
713 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
714 #define iov_iov_len(i) \
715 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
718 #define sizeof_iov sizeof(*iov)
719 #define iov_iov_base(i) iov[i].iov_base
720 #define iov_iov_len(i) iov[i].iov_len
725 size = sizeof_iov * len;
726 /* Assuming no sane program has millions of iovs */
727 if ((unsigned)len > 1024*1024 /* insane or negative size? */
728 || (iov = malloc(size)) == NULL) {
729 fprintf(stderr, "Out of memory\n");
732 if (umoven(tcp, addr, size, (char *) iov) >= 0) {
733 for (i = 0; i < len; i++) {
734 /* include the buffer number to make it easy to
735 * match up the trace with the source */
736 tprintf(" * %lu bytes in buffer %d\n",
737 (unsigned long)iov_iov_len(i), i);
738 dumpstr(tcp, (long) iov_iov_base(i),
751 dumpstr(struct tcb *tcp, long addr, int len)
753 static int strsize = -1;
754 static unsigned char *str;
759 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
760 "1234567890123456") + /*in case I'm off by few:*/ 4)
761 /*align to 8 to make memset easier:*/ + 7) & -8
763 const unsigned char *src;
766 memset(outbuf, ' ', sizeof(outbuf));
768 if (strsize < len + 16) {
770 str = malloc(len + 16);
773 fprintf(stderr, "Out of memory\n");
779 if (umoven(tcp, addr, len, (char *) str) < 0)
782 /* Space-pad to 16 bytes */
794 *dst++ = "0123456789abcdef"[*src >> 4];
795 *dst++ = "0123456789abcdef"[*src & 0xf];
801 dst++; /* space is there by memset */
804 dst++; /* space is there by memset */
811 if (*src >= ' ' && *src < 0x7f)
818 tprintf(" | %05x %s |\n", i - 16, outbuf);
822 #ifdef HAVE_PROCESS_VM_READV
823 /* C library supports this, but the kernel might not. */
824 static bool process_vm_readv_not_supported = 0;
827 /* Need to do this since process_vm_readv() is not yet available in libc.
828 * When libc is be updated, only "static bool process_vm_readv_not_supported"
829 * line should remain.
831 #if !defined(__NR_process_vm_readv)
833 # define __NR_process_vm_readv 347
834 # elif defined(X86_64)
835 # define __NR_process_vm_readv 310
836 # elif defined(POWERPC)
837 # define __NR_process_vm_readv 351
841 #if defined(__NR_process_vm_readv)
842 static bool process_vm_readv_not_supported = 0;
843 /* Have to avoid duplicating with the C library headers. */
844 static ssize_t strace_process_vm_readv(pid_t pid,
845 const struct iovec *lvec,
846 unsigned long liovcnt,
847 const struct iovec *rvec,
848 unsigned long riovcnt,
851 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
853 #define process_vm_readv strace_process_vm_readv
855 static bool process_vm_readv_not_supported = 1;
856 # define process_vm_readv(...) (errno = ENOSYS, -1)
859 #endif /* end of hack */
861 #define PAGMASK (~(PAGSIZ - 1))
863 * move `len' bytes of data from process `pid'
864 * at address `addr' to our space at `laddr'
867 umoven(struct tcb *tcp, long addr, int len, char *laddr)
873 char x[sizeof(long)];
876 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
877 if (current_wordsize < sizeof(addr))
878 addr &= (1ul << 8 * current_wordsize) - 1;
881 if (!process_vm_readv_not_supported) {
882 struct iovec local[1], remote[1];
885 local[0].iov_base = laddr;
886 remote[0].iov_base = (void*)addr;
887 local[0].iov_len = remote[0].iov_len = len;
888 r = process_vm_readv(pid, local, 1, remote, 1, 0);
892 error_msg("umoven: short read (%d < %d) @0x%lx",
898 process_vm_readv_not_supported = 1;
901 /* the process is gone */
903 case EFAULT: case EIO: case EPERM:
904 /* address space is inaccessible */
907 /* all the rest is strange and should be reported */
908 perror_msg("process_vm_readv");
914 if (addr & (sizeof(long) - 1)) {
915 /* addr not a multiple of sizeof(long) */
916 n = addr - (addr & -sizeof(long)); /* residue */
917 addr &= -sizeof(long); /* residue */
919 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
923 case ESRCH: case EINVAL:
924 /* these could be seen if the process is gone */
926 case EFAULT: case EIO: case EPERM:
927 /* address space is inaccessible */
930 /* all the rest is strange and should be reported */
931 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
935 m = MIN(sizeof(long) - n, len);
936 memcpy(laddr, &u.x[n], m);
937 addr += sizeof(long);
944 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
948 case ESRCH: case EINVAL:
949 /* these could be seen if the process is gone */
951 case EFAULT: case EIO: case EPERM:
952 /* address space is inaccessible */
954 perror_msg("umoven: short read (%d < %d) @0x%lx",
955 nread, nread + len, addr - nread);
959 /* all the rest is strange and should be reported */
960 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
964 m = MIN(sizeof(long), len);
965 memcpy(laddr, u.x, m);
966 addr += sizeof(long);
976 * Like `umove' but make the additional effort of looking
977 * for a terminating zero byte.
979 * Returns < 0 on error, > 0 if NUL was seen,
980 * (TODO if useful: return count of bytes including NUL),
981 * else 0 if len bytes were read but no NUL byte seen.
983 * Note: there is no guarantee we won't overwrite some bytes
984 * in laddr[] _after_ terminating NUL (but, of course,
985 * we never write past laddr[len-1]).
988 umovestr(struct tcb *tcp, long addr, int len, char *laddr)
991 const unsigned long x01010101 = 0x01010101ul;
992 const unsigned long x80808080 = 0x80808080ul;
993 #elif SIZEOF_LONG == 8
994 const unsigned long x01010101 = 0x0101010101010101ul;
995 const unsigned long x80808080 = 0x8080808080808080ul;
997 # error SIZEOF_LONG > 8
1004 char x[sizeof(long)];
1007 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1008 if (current_wordsize < sizeof(addr))
1009 addr &= (1ul << 8 * current_wordsize) - 1;
1013 if (!process_vm_readv_not_supported) {
1014 struct iovec local[1], remote[1];
1016 local[0].iov_base = laddr;
1017 remote[0].iov_base = (void*)addr;
1024 /* Don't read kilobytes: most strings are short */
1026 if (chunk_len > 256)
1028 /* Don't cross pages. I guess otherwise we can get EFAULT
1029 * and fail to notice that terminating NUL lies
1030 * in the existing (first) page.
1031 * (I hope there aren't arches with pages < 4K)
1033 end_in_page = ((addr + chunk_len) & 4095);
1034 r = chunk_len - end_in_page;
1035 if (r > 0) /* if chunk_len > end_in_page */
1036 chunk_len = r; /* chunk_len -= end_in_page */
1038 local[0].iov_len = remote[0].iov_len = chunk_len;
1039 r = process_vm_readv(pid, local, 1, remote, 1, 0);
1041 if (memchr(local[0].iov_base, '\0', r))
1043 local[0].iov_base += r;
1044 remote[0].iov_base += r;
1051 process_vm_readv_not_supported = 1;
1052 goto vm_readv_didnt_work;
1054 /* the process is gone */
1056 case EFAULT: case EIO: case EPERM:
1057 /* address space is inaccessible */
1059 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1060 nread, nread + len, addr);
1064 /* all the rest is strange and should be reported */
1065 perror_msg("process_vm_readv");
1071 vm_readv_didnt_work:
1073 if (addr & (sizeof(long) - 1)) {
1074 /* addr not a multiple of sizeof(long) */
1075 n = addr - (addr & -sizeof(long)); /* residue */
1076 addr &= -sizeof(long); /* residue */
1078 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
1082 case ESRCH: case EINVAL:
1083 /* these could be seen if the process is gone */
1085 case EFAULT: case EIO: case EPERM:
1086 /* address space is inaccessible */
1089 /* all the rest is strange and should be reported */
1090 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1094 m = MIN(sizeof(long) - n, len);
1095 memcpy(laddr, &u.x[n], m);
1096 while (n & (sizeof(long) - 1))
1097 if (u.x[n++] == '\0')
1099 addr += sizeof(long);
1107 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
1111 case ESRCH: case EINVAL:
1112 /* these could be seen if the process is gone */
1114 case EFAULT: case EIO: case EPERM:
1115 /* address space is inaccessible */
1117 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1118 nread, nread + len, addr - nread);
1122 /* all the rest is strange and should be reported */
1123 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1127 m = MIN(sizeof(long), len);
1128 memcpy(laddr, u.x, m);
1129 /* "If a NUL char exists in this word" */
1130 if ((u.val - x01010101) & ~u.val & x80808080)
1132 addr += sizeof(long);
1141 upeek(int pid, long off, long *res)
1146 val = ptrace(PTRACE_PEEKUSER, (pid_t)pid, (char *) off, 0);
1147 if (val == -1 && errno) {
1148 if (errno != ESRCH) {
1149 perror_msg("upeek: PTRACE_PEEKUSER pid:%d @0x%lx)", pid, off);
1157 /* Note! On new kernels (about 2.5.46+), we use PTRACE_O_TRACECLONE
1158 * and PTRACE_O_TRACE[V]FORK for tracing children.
1159 * If you are adding a new arch which is only supported by newer kernels,
1160 * you most likely don't need to add any code below
1161 * beside a dummy "return 0" block in change_syscall().
1165 * These #if's are huge, please indent them correctly.
1166 * It's easy to get confused otherwise.
1169 #include "syscall.h"
1171 #ifndef CLONE_PTRACE
1172 # define CLONE_PTRACE 0x00002000
1175 # define CLONE_VFORK 0x00004000
1178 # define CLONE_VM 0x00000100
1183 typedef unsigned long *arg_setup_state;
1186 arg_setup(struct tcb *tcp, arg_setup_state *state)
1188 unsigned long cfm, sof, sol;
1191 if (ia64_ia32mode) {
1192 /* Satisfy a false GCC warning. */
1197 if (upeek(tcp->pid, PT_AR_BSP, &bsp) < 0)
1199 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
1202 sof = (cfm >> 0) & 0x7f;
1203 sol = (cfm >> 7) & 0x7f;
1204 bsp = (long) ia64_rse_skip_regs((unsigned long *) bsp, -sof + sol);
1206 *state = (unsigned long *) bsp;
1210 # define arg_finish_change(tcp, state) 0
1213 get_arg0(struct tcb *tcp, arg_setup_state *state, long *valp)
1218 ret = upeek(tcp->pid, PT_R11, valp);
1221 (unsigned long) ia64_rse_skip_regs(*state, 0),
1222 sizeof(long), (void *) valp);
1227 get_arg1(struct tcb *tcp, arg_setup_state *state, long *valp)
1232 ret = upeek(tcp->pid, PT_R9, valp);
1235 (unsigned long) ia64_rse_skip_regs(*state, 1),
1236 sizeof(long), (void *) valp);
1241 set_arg0(struct tcb *tcp, arg_setup_state *state, long val)
1243 int req = PTRACE_POKEDATA;
1246 if (ia64_ia32mode) {
1247 ap = (void *) (intptr_t) PT_R11; /* r11 == EBX */
1248 req = PTRACE_POKEUSER;
1250 ap = ia64_rse_skip_regs(*state, 0);
1252 ptrace(req, tcp->pid, ap, val);
1253 return errno ? -1 : 0;
1257 set_arg1(struct tcb *tcp, arg_setup_state *state, long val)
1259 int req = PTRACE_POKEDATA;
1262 if (ia64_ia32mode) {
1263 ap = (void *) (intptr_t) PT_R9; /* r9 == ECX */
1264 req = PTRACE_POKEUSER;
1266 ap = ia64_rse_skip_regs(*state, 1);
1268 ptrace(req, tcp->pid, ap, val);
1269 return errno ? -1 : 0;
1272 /* ia64 does not return the input arguments from functions (and syscalls)
1273 according to ia64 RSE (Register Stack Engine) behavior. */
1275 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1276 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1278 #elif defined(SPARC) || defined(SPARC64)
1280 # if defined(SPARC64)
1281 # undef PTRACE_GETREGS
1282 # define PTRACE_GETREGS PTRACE_GETREGS64
1283 # undef PTRACE_SETREGS
1284 # define PTRACE_SETREGS PTRACE_SETREGS64
1287 typedef struct pt_regs arg_setup_state;
1289 # define arg_setup(tcp, state) \
1290 (ptrace(PTRACE_GETREGS, (tcp)->pid, (char *) (state), 0))
1291 # define arg_finish_change(tcp, state) \
1292 (ptrace(PTRACE_SETREGS, (tcp)->pid, (char *) (state), 0))
1294 # define get_arg0(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O0], 0)
1295 # define get_arg1(tcp, state, valp) (*(valp) = (state)->u_regs[U_REG_O1], 0)
1296 # define set_arg0(tcp, state, val) ((state)->u_regs[U_REG_O0] = (val), 0)
1297 # define set_arg1(tcp, state, val) ((state)->u_regs[U_REG_O1] = (val), 0)
1298 # define restore_arg0(tcp, state, val) 0
1300 #else /* other architectures */
1302 # if defined S390 || defined S390X
1303 /* Note: this is only true for the `clone' system call, which handles
1304 arguments specially. We could as well say that its first two arguments
1305 are swapped relative to other architectures, but that would just be
1306 another #ifdef in the calls. */
1307 # define arg0_offset PT_GPR3
1308 # define arg1_offset PT_ORIGGPR2
1309 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1310 # define restore_arg1(tcp, state, val) ((void) (state), 0)
1311 # define arg0_index 1
1312 # define arg1_index 0
1313 # elif defined(ALPHA) || defined(MIPS)
1314 # define arg0_offset REG_A0
1315 # define arg1_offset (REG_A0+1)
1316 # elif defined(POWERPC)
1317 # define arg0_offset (sizeof(unsigned long)*PT_R3)
1318 # define arg1_offset (sizeof(unsigned long)*PT_R4)
1319 # define restore_arg0(tcp, state, val) ((void) (state), 0)
1320 # elif defined(HPPA)
1321 # define arg0_offset PT_GR26
1322 # define arg1_offset (PT_GR26-4)
1323 # elif defined(X86_64) || defined(X32)
1324 # define arg0_offset ((long)(8*(current_personality ? RBX : RDI)))
1325 # define arg1_offset ((long)(8*(current_personality ? RCX : RSI)))
1327 # define arg0_offset (4*(REG_REG0+4))
1328 # define arg1_offset (4*(REG_REG0+5))
1329 # elif defined(SH64)
1330 /* ABI defines arg0 & 1 in r2 & r3 */
1331 # define arg0_offset (REG_OFFSET+16)
1332 # define arg1_offset (REG_OFFSET+24)
1333 # define restore_arg0(tcp, state, val) 0
1334 # elif defined CRISV10 || defined CRISV32
1335 # define arg0_offset (4*PT_R11)
1336 # define arg1_offset (4*PT_ORIG_R10)
1337 # define restore_arg0(tcp, state, val) 0
1338 # define restore_arg1(tcp, state, val) 0
1339 # define arg0_index 1
1340 # define arg1_index 0
1342 # define arg0_offset 0
1343 # define arg1_offset 4
1345 # define restore_arg0(tcp, state, val) 0
1349 typedef int arg_setup_state;
1351 # define arg_setup(tcp, state) (0)
1352 # define arg_finish_change(tcp, state) 0
1353 # define get_arg0(tcp, cookie, valp) (upeek((tcp)->pid, arg0_offset, (valp)))
1354 # define get_arg1(tcp, cookie, valp) (upeek((tcp)->pid, arg1_offset, (valp)))
1357 set_arg0(struct tcb *tcp, void *cookie, long val)
1359 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg0_offset, val);
1363 set_arg1(struct tcb *tcp, void *cookie, long val)
1365 return ptrace(PTRACE_POKEUSER, tcp->pid, (char*)arg1_offset, val);
1368 #endif /* architectures */
1370 #ifndef restore_arg0
1371 # define restore_arg0(tcp, state, val) set_arg0((tcp), (state), (val))
1373 #ifndef restore_arg1
1374 # define restore_arg1(tcp, state, val) set_arg1((tcp), (state), (val))
1378 # define arg0_index 0
1379 # define arg1_index 1
1383 change_syscall(struct tcb *tcp, arg_setup_state *state, int new)
1386 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_EAX * 4), new) < 0)
1389 #elif defined(X86_64)
1390 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(ORIG_RAX * 8), new) < 0)
1394 /* setbpt/clearbpt never used: */
1395 /* X32 is only supported since about linux-3.0.30 */
1396 #elif defined(POWERPC)
1397 if (ptrace(PTRACE_POKEUSER, tcp->pid,
1398 (char*)(sizeof(unsigned long)*PT_R0), new) < 0)
1401 #elif defined(S390) || defined(S390X)
1402 /* s390 linux after 2.4.7 has a hook in entry.S to allow this */
1403 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GPR2), new) < 0)
1407 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_ORIG_D0), new) < 0)
1410 #elif defined(SPARC) || defined(SPARC64)
1411 state->u_regs[U_REG_G1] = new;
1414 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), new) < 0)
1417 #elif defined(ALPHA)
1418 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), new) < 0)
1421 #elif defined(AVR32)
1422 /* setbpt/clearbpt never used: */
1423 /* AVR32 is only supported since about linux-2.6.19 */
1425 /* setbpt/clearbpt never used: */
1426 /* Blackfin is only supported since about linux-2.6.23 */
1428 if (ia64_ia32mode) {
1431 break; /* x86 SYS_fork */
1436 fprintf(stderr, "%s: unexpected syscall %d\n",
1440 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R1), new) < 0)
1442 } else if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R15), new) < 0)
1446 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR20), new) < 0)
1450 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*(REG_REG0+3)), new) < 0)
1454 /* Top half of reg encodes the no. of args n as 0x1n.
1455 Assume 0 args as kernel never actually checks... */
1456 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_SYSCALL),
1457 0x100000 | new) < 0)
1460 #elif defined(CRISV10) || defined(CRISV32)
1461 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_R9), new) < 0)
1465 /* Some kernels support this, some (pre-2.6.16 or so) don't. */
1466 # ifndef PTRACE_SET_SYSCALL
1467 # define PTRACE_SET_SYSCALL 23
1469 if (ptrace(PTRACE_SET_SYSCALL, tcp->pid, 0, new & 0xffff) != 0)
1472 #elif defined(AARCH64)
1473 /* setbpt/clearbpt never used: */
1474 /* AARCH64 is only supported since about linux-3.0.31 */
1476 /* setbpt/clearbpt never used: */
1477 /* Tilera CPUs are only supported since about linux-2.6.34 */
1478 #elif defined(MICROBLAZE)
1479 /* setbpt/clearbpt never used: */
1480 /* microblaze is only supported since about linux-2.6.30 */
1482 /* never reached; OR1K is only supported by kernels since 3.1.0. */
1483 #elif defined(METAG)
1484 /* setbpt/clearbpt never used: */
1485 /* Meta is only supported since linux-3.7 */
1486 #elif defined(XTENSA)
1487 /* setbpt/clearbpt never used: */
1488 /* Xtensa is only supported since linux 2.6.13 */
1490 /* setbpt/clearbpt never used: */
1491 /* ARC only supported since 3.9 */
1493 #warning Do not know how to handle change_syscall for this architecture
1494 #endif /* architecture */
1499 setbpt(struct tcb *tcp)
1501 static int clone_scno[SUPPORTED_PERSONALITIES] = { SYS_clone };
1502 arg_setup_state state;
1504 if (tcp->flags & TCB_BPTSET) {
1505 fprintf(stderr, "PANIC: TCB already set in pid %u\n", tcp->pid);
1510 * It's a silly kludge to initialize this with a search at runtime.
1511 * But it's better than maintaining another magic thing in the
1512 * godforsaken tables.
1514 if (clone_scno[current_personality] == 0) {
1516 for (i = 0; i < nsyscalls; ++i)
1517 if (sysent[i].sys_func == sys_clone) {
1518 clone_scno[current_personality] = i;
1523 if (tcp->s_ent->sys_func == sys_fork) {
1524 if (arg_setup(tcp, &state) < 0
1525 || get_arg0(tcp, &state, &tcp->inst[0]) < 0
1526 || get_arg1(tcp, &state, &tcp->inst[1]) < 0
1527 || change_syscall(tcp, &state,
1528 clone_scno[current_personality]) < 0
1529 || set_arg0(tcp, &state, CLONE_PTRACE|SIGCHLD) < 0
1530 || set_arg1(tcp, &state, 0) < 0
1531 || arg_finish_change(tcp, &state) < 0)
1533 tcp->u_arg[arg0_index] = CLONE_PTRACE|SIGCHLD;
1534 tcp->u_arg[arg1_index] = 0;
1535 tcp->flags |= TCB_BPTSET;
1539 if (tcp->s_ent->sys_func == sys_clone) {
1540 /* ia64 calls directly `clone (CLONE_VFORK | CLONE_VM)'
1541 contrary to x86 vfork above. Even on x86 we turn the
1542 vfork semantics into plain fork - each application must not
1543 depend on the vfork specifics according to POSIX. We would
1544 hang waiting for the parent resume otherwise. We need to
1545 clear also CLONE_VM but only in the CLONE_VFORK case as
1546 otherwise we would break pthread_create. */
1548 long new_arg0 = (tcp->u_arg[arg0_index] | CLONE_PTRACE);
1549 if (new_arg0 & CLONE_VFORK)
1550 new_arg0 &= ~(unsigned long)(CLONE_VFORK | CLONE_VM);
1551 if (arg_setup(tcp, &state) < 0
1552 || set_arg0(tcp, &state, new_arg0) < 0
1553 || arg_finish_change(tcp, &state) < 0)
1555 tcp->inst[0] = tcp->u_arg[arg0_index];
1556 tcp->inst[1] = tcp->u_arg[arg1_index];
1557 tcp->flags |= TCB_BPTSET;
1561 fprintf(stderr, "PANIC: setbpt for syscall %ld on %u???\n",
1562 tcp->scno, tcp->pid);
1567 clearbpt(struct tcb *tcp)
1569 arg_setup_state state;
1570 if (arg_setup(tcp, &state) < 0
1571 || change_syscall(tcp, &state, tcp->scno) < 0
1572 || restore_arg0(tcp, &state, tcp->inst[0]) < 0
1573 || restore_arg1(tcp, &state, tcp->inst[1]) < 0
1574 || arg_finish_change(tcp, &state))
1577 tcp->flags &= ~TCB_BPTSET;