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.
35 #include <sys/param.h>
38 #ifdef HAVE_SYS_XATTR_H
39 # include <sys/xattr.h>
47 string_to_uint(const char *str)
55 value = strtol(str, &error, 10);
56 if (errno || *error || value < 0 || (long)(int)value != value)
62 tv_nz(const struct timeval *a)
64 return a->tv_sec || a->tv_usec;
68 tv_cmp(const struct timeval *a, const struct timeval *b)
70 if (a->tv_sec < b->tv_sec
71 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
73 if (a->tv_sec > b->tv_sec
74 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
80 tv_float(const struct timeval *tv)
82 return tv->tv_sec + tv->tv_usec/1000000.0;
86 tv_add(struct timeval *tv, const struct timeval *a, const struct timeval *b)
88 tv->tv_sec = a->tv_sec + b->tv_sec;
89 tv->tv_usec = a->tv_usec + b->tv_usec;
90 if (tv->tv_usec >= 1000000) {
92 tv->tv_usec -= 1000000;
97 tv_sub(struct timeval *tv, const struct timeval *a, const struct timeval *b)
99 tv->tv_sec = a->tv_sec - b->tv_sec;
100 tv->tv_usec = a->tv_usec - b->tv_usec;
101 if (((long) tv->tv_usec) < 0) {
103 tv->tv_usec += 1000000;
108 tv_div(struct timeval *tv, const struct timeval *a, int n)
110 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
111 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
112 tv->tv_usec %= 1000000;
116 tv_mul(struct timeval *tv, const struct timeval *a, int n)
118 tv->tv_usec = a->tv_usec * n;
119 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
120 tv->tv_usec %= 1000000;
124 xlookup(const struct xlat *xlat, const uint64_t val)
126 for (; xlat->str != NULL; xlat++)
127 if (xlat->val == val)
133 xlat_bsearch_compare(const void *a, const void *b)
135 const uint64_t val1 = *(const uint64_t *) a;
136 const uint64_t val2 = ((const struct xlat *) b)->val;
137 return (val1 > val2) ? 1 : (val1 < val2) ? -1 : 0;
141 xlat_search(const struct xlat *xlat, const size_t nmemb, const uint64_t val)
143 const struct xlat *e =
144 bsearch((const void*) &val,
145 xlat, nmemb, sizeof(*xlat), xlat_bsearch_compare);
147 return e ? e->str : NULL;
150 #if !defined HAVE_STPCPY
152 stpcpy(char *dst, const char *src)
154 while ((*dst = *src++) != '\0')
160 /* Find a next bit which is set.
161 * Starts testing at cur_bit.
162 * Returns -1 if no more bits are set.
164 * We never touch bytes we don't need to.
165 * On big-endian, array is assumed to consist of
166 * current_wordsize wide words: for example, is current_wordsize is 4,
167 * the bytes are walked in 3,2,1,0, 7,6,5,4, 11,10,9,8 ... sequence.
168 * On little-endian machines, word size is immaterial.
171 next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits)
173 const unsigned endian = 1;
174 int little_endian = *(char*)&endian;
176 const uint8_t *array = bit_array;
177 unsigned pos = cur_bit / 8;
178 unsigned pos_xor_mask = little_endian ? 0 : current_wordsize-1;
184 if (cur_bit >= size_bits)
186 cur_byte = array[pos ^ pos_xor_mask];
188 cur_bit = (cur_bit + 8) & (-8);
192 bitmask = 1 << (cur_bit & 7);
194 if (cur_byte & bitmask)
197 if (cur_bit >= size_bits)
200 /* This check *can't be* optimized out: */
208 * Print entry in struct xlat table, if there.
211 printxvals(const uint64_t val, const char *dflt, const struct xlat *xlat, ...)
215 va_start(args, xlat);
216 for (; xlat; xlat = va_arg(args, const struct xlat *)) {
217 const char *str = xlookup(xlat, val);
225 /* No hits -- print raw # instead. */
226 tprintf("%#" PRIx64 " /* %s */", val, dflt);
232 * Fetch 64bit argument at position arg_no and
233 * return the index of the next argument.
236 getllval(struct tcb *tcp, unsigned long long *val, int arg_no)
238 #if SIZEOF_LONG > 4 && SIZEOF_LONG == SIZEOF_LONG_LONG
239 # if SUPPORTED_PERSONALITIES > 1
241 if (current_personality != 1) {
243 if (current_wordsize > 4) {
246 *val = tcp->u_arg[arg_no];
248 # if SUPPORTED_PERSONALITIES > 1
250 # if defined(AARCH64) || defined(POWERPC64)
251 /* Align arg_no to the next even number. */
252 arg_no = (arg_no + 1) & 0xe;
253 # endif /* AARCH64 || POWERPC64 */
254 *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
257 # endif /* SUPPORTED_PERSONALITIES > 1 */
258 #elif SIZEOF_LONG > 4
259 # error Unsupported configuration: SIZEOF_LONG > 4 && SIZEOF_LONG_LONG > SIZEOF_LONG
260 #elif HAVE_STRUCT_TCB_EXT_ARG
261 # if SUPPORTED_PERSONALITIES > 1
262 if (current_personality == 1) {
263 *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
268 *val = tcp->ext_arg[arg_no];
272 # if defined __ARM_EABI__ || \
273 defined LINUX_MIPSO32 || \
276 /* Align arg_no to the next even number. */
277 arg_no = (arg_no + 1) & 0xe;
279 *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
287 * Print 64bit argument at position arg_no and
288 * return the index of the next argument.
291 printllval(struct tcb *tcp, const char *format, int arg_no)
293 unsigned long long val = 0;
295 arg_no = getllval(tcp, &val, arg_no);
296 tprintf(format, val);
301 * Interpret `xlat' as an array of flags
302 * print the entries whose bits are on in `flags'
303 * return # of flags printed.
306 addflags(const struct xlat *xlat, uint64_t flags)
308 for (; xlat->str; xlat++) {
309 if (xlat->val && (flags & xlat->val) == xlat->val) {
310 tprintf("|%s", xlat->str);
315 tprintf("|%#" PRIx64, flags);
320 * Interpret `xlat' as an array of flags.
321 * Print to static string the entries whose bits are on in `flags'
322 * Return static string.
325 sprintflags(const char *prefix, const struct xlat *xlat, uint64_t flags)
327 static char outstr[1024];
331 outptr = stpcpy(outstr, prefix);
333 if (flags == 0 && xlat->val == 0 && xlat->str) {
334 strcpy(outptr, xlat->str);
338 for (; xlat->str; xlat++) {
339 if (xlat->val && (flags & xlat->val) == xlat->val) {
342 outptr = stpcpy(outptr, xlat->str);
352 outptr += sprintf(outptr, "%#" PRIx64, flags);
359 printflags64(const struct xlat *xlat, uint64_t flags, const char *dflt)
364 if (flags == 0 && xlat->val == 0 && xlat->str) {
370 for (n = 0; xlat->str; xlat++) {
371 if (xlat->val && (flags & xlat->val) == xlat->val) {
372 tprintf("%s%s", sep, xlat->str);
381 tprintf("%s%#" PRIx64, sep, flags);
386 tprintf("%#" PRIx64, flags);
388 tprintf(" /* %s */", dflt);
399 printaddr(const long addr)
404 tprintf("%#lx", addr);
407 #define DEF_PRINTNUM(name, type) \
409 printnum_ ## name(struct tcb *tcp, const long addr, const char *fmt) \
412 if (umove_or_printaddr(tcp, addr, &num)) \
420 #define DEF_PRINTPAIR(name, type) \
422 printpair_ ## name(struct tcb *tcp, const long addr, const char *fmt) \
425 if (umove_or_printaddr(tcp, addr, &pair)) \
428 tprintf(fmt, pair[0]); \
430 tprintf(fmt, pair[1]); \
435 DEF_PRINTNUM(int, int)
436 DEF_PRINTPAIR(int, int)
437 DEF_PRINTNUM(short, short)
438 DEF_PRINTNUM(int64, uint64_t)
439 DEF_PRINTPAIR(int64, uint64_t)
441 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
443 printnum_long_int(struct tcb *tcp, const long addr,
444 const char *fmt_long, const char *fmt_int)
446 if (current_wordsize > sizeof(int)) {
447 return printnum_int64(tcp, addr, fmt_long);
449 return printnum_int(tcp, addr, fmt_int);
458 static char buf[sizeof(int) * 3 * 6];
466 snprintf(buf, sizeof buf, "%02d/%02d/%02d-%02d:%02d:%02d",
467 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
468 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
470 snprintf(buf, sizeof buf, "%lu", (unsigned long) t);
475 static enum sock_proto
476 getfdproto(struct tcb *tcp, int fd)
478 #ifdef HAVE_SYS_XATTR_H
479 size_t bufsize = 256;
482 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
485 return SOCK_PROTO_UNKNOWN;
487 sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
488 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
490 return SOCK_PROTO_UNKNOWN;
493 * This is a protection for the case when the kernel
494 * side does not append a null byte to the buffer.
498 return get_proto_by_name(buf);
501 return SOCK_PROTO_UNKNOWN;
506 printfd(struct tcb *tcp, int fd)
508 char path[PATH_MAX + 1];
509 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
510 static const char socket_prefix[] = "socket:[";
511 const size_t socket_prefix_len = sizeof(socket_prefix) - 1;
512 const size_t path_len = strlen(path);
515 if (show_fd_path > 1 &&
516 strncmp(path, socket_prefix, socket_prefix_len) == 0 &&
517 path[path_len - 1] == ']') {
518 unsigned long inode =
519 strtoul(path + socket_prefix_len, NULL, 10);
521 if (!print_sockaddr_by_inode_cached(inode)) {
522 const enum sock_proto proto =
524 if (!print_sockaddr_by_inode(inode, proto))
528 print_quoted_string(path, path_len,
529 QUOTE_OMIT_LEADING_TRAILING_QUOTES);
537 * Quote string `instr' of length `size'
538 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
540 * If QUOTE_0_TERMINATED `style' flag is set,
541 * treat `instr' as a NUL-terminated string,
542 * checking up to (`size' + 1) bytes of `instr'.
544 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
545 * do not add leading and trailing quoting symbols.
547 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
548 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
551 string_quote(const char *instr, char *outstr, const unsigned int size,
552 const unsigned int style)
554 const unsigned char *ustr = (const unsigned char *) instr;
559 if (style & QUOTE_0_TERMINATED)
562 eol = 0x100; /* this can never match a char */
568 /* Check for presence of symbol which require
569 to hex-quote the whole string. */
570 for (i = 0; i < size; ++i) {
572 /* Check for NUL-terminated string. */
576 /* Force hex unless c is printable or whitespace */
581 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
582 * They happen to have ASCII codes 9,10,11,12,13.
584 if (c < ' ' && (unsigned)(c - 9) >= 5) {
591 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
595 /* Hex-quote the whole string. */
596 for (i = 0; i < size; ++i) {
598 /* Check for NUL-terminated string. */
603 *s++ = "0123456789abcdef"[c >> 4];
604 *s++ = "0123456789abcdef"[c & 0xf];
607 for (i = 0; i < size; ++i) {
609 /* Check for NUL-terminated string. */
613 case '\"': case '\\':
638 if (c >= ' ' && c <= 0x7e)
644 && ustr[i + 1] >= '0'
645 && ustr[i + 1] <= '9'
648 *s++ = '0' + (c >> 6);
649 *s++ = '0' + ((c >> 3) & 0x7);
654 *s++ = '0' + (c >> 6);
655 *s++ = '0' + ((c >> 3) & 0x7);
658 *s++ = '0' + (c & 0x7);
665 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
669 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
670 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
671 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
672 * but next char is NUL.
680 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
683 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
687 #ifndef ALLOCA_CUTOFF
688 # define ALLOCA_CUTOFF 4032
690 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
693 * Quote string `str' of length `size' and print the result.
695 * If QUOTE_0_TERMINATED `style' flag is set,
696 * treat `str' as a NUL-terminated string and
697 * quote at most (`size' - 1) bytes.
699 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
700 * do not add leading and trailing quoting symbols.
702 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
703 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
706 print_quoted_string(const char *str, unsigned int size,
707 const unsigned int style)
711 unsigned int alloc_size;
714 if (size && style & QUOTE_0_TERMINATED)
717 alloc_size = 4 * size;
718 if (alloc_size / 4 != size) {
719 error_msg("Out of memory");
723 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2);
725 if (use_alloca(alloc_size)) {
726 outstr = alloca(alloc_size);
729 outstr = buf = malloc(alloc_size);
731 error_msg("Out of memory");
737 rc = string_quote(str, outstr, size, style);
745 * Print path string specified by address `addr' and length `n'.
746 * If path length exceeds `n', append `...' to the output.
749 printpathn(struct tcb *tcp, long addr, unsigned int n)
751 char path[PATH_MAX + 1];
759 /* Cap path length to the path buffer size */
760 if (n > sizeof path - 1)
763 /* Fetch one byte more to find out whether path length > n. */
764 nul_seen = umovestr(tcp, addr, n + 1, path);
769 print_quoted_string(path, n, QUOTE_0_TERMINATED);
776 printpath(struct tcb *tcp, long addr)
778 /* Size must correspond to char path[] size in printpathn */
779 printpathn(tcp, addr, PATH_MAX);
783 * Print string specified by address `addr' and length `len'.
784 * If `len' < 0, treat the string as a NUL-terminated string.
785 * If string length exceeds `max_strlen', append `...' to the output.
788 printstr(struct tcb *tcp, long addr, long len)
790 static char *str = NULL;
800 /* Allocate static buffers if they are not allocated yet. */
802 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
804 if (outstr_size / 4 != max_strlen)
806 str = xmalloc(max_strlen + 1);
807 outstr = xmalloc(outstr_size);
813 * Treat as a NUL-terminated string: fetch one byte more
814 * because string_quote may look one byte ahead.
816 if (umovestr(tcp, addr, size + 1, str) < 0) {
820 style = QUOTE_0_TERMINATED;
823 if (size > (unsigned long)len)
824 size = (unsigned long)len;
825 if (umoven(tcp, addr, size, str) < 0) {
832 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
833 * or we were requested to print more than -s NUM chars)...
835 ellipsis = (string_quote(str, outstr, size, style) &&
836 (len < 0 || (unsigned long) len > max_strlen));
844 dumpiov_upto(struct tcb *tcp, int len, long addr, unsigned long data_size)
846 #if SUPPORTED_PERSONALITIES > 1
848 struct { uint32_t base; uint32_t len; } *iov32;
849 struct { uint64_t base; uint64_t len; } *iov64;
851 #define iov iovu.iov64
853 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
854 #define iov_iov_base(i) \
855 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
856 #define iov_iov_len(i) \
857 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
860 #define sizeof_iov sizeof(*iov)
861 #define iov_iov_base(i) iov[i].iov_base
862 #define iov_iov_len(i) iov[i].iov_len
867 size = sizeof_iov * len;
868 /* Assuming no sane program has millions of iovs */
869 if ((unsigned)len > 1024*1024 /* insane or negative size? */
870 || (iov = malloc(size)) == NULL) {
871 error_msg("Out of memory");
874 if (umoven(tcp, addr, size, iov) >= 0) {
875 for (i = 0; i < len; i++) {
876 unsigned long iov_len = iov_iov_len(i);
877 if (iov_len > data_size)
881 data_size -= iov_len;
882 /* include the buffer number to make it easy to
883 * match up the trace with the source */
884 tprintf(" * %lu bytes in buffer %d\n", iov_len, i);
885 dumpstr(tcp, (long) iov_iov_base(i), iov_len);
896 dumpstr(struct tcb *tcp, long addr, int len)
898 static int strsize = -1;
899 static unsigned char *str;
904 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
905 "1234567890123456") + /*in case I'm off by few:*/ 4)
906 /*align to 8 to make memset easier:*/ + 7) & -8
908 const unsigned char *src;
911 memset(outbuf, ' ', sizeof(outbuf));
913 if (strsize < len + 16) {
915 str = malloc(len + 16);
918 error_msg("Out of memory");
924 if (umoven(tcp, addr, len, str) < 0)
927 /* Space-pad to 16 bytes */
939 *dst++ = "0123456789abcdef"[*src >> 4];
940 *dst++ = "0123456789abcdef"[*src & 0xf];
946 dst++; /* space is there by memset */
949 dst++; /* space is there by memset */
956 if (*src >= ' ' && *src < 0x7f)
963 tprintf(" | %05x %s |\n", i - 16, outbuf);
967 #ifdef HAVE_PROCESS_VM_READV
968 /* C library supports this, but the kernel might not. */
969 static bool process_vm_readv_not_supported = 0;
972 /* Need to do this since process_vm_readv() is not yet available in libc.
973 * When libc is be updated, only "static bool process_vm_readv_not_supported"
974 * line should remain.
976 #if !defined(__NR_process_vm_readv)
978 # define __NR_process_vm_readv 347
979 # elif defined(X86_64)
980 # define __NR_process_vm_readv 310
981 # elif defined(POWERPC)
982 # define __NR_process_vm_readv 351
986 #if defined(__NR_process_vm_readv)
987 static bool process_vm_readv_not_supported = 0;
988 /* Have to avoid duplicating with the C library headers. */
989 static ssize_t strace_process_vm_readv(pid_t pid,
990 const struct iovec *lvec,
991 unsigned long liovcnt,
992 const struct iovec *rvec,
993 unsigned long riovcnt,
996 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
998 #define process_vm_readv strace_process_vm_readv
1000 static bool process_vm_readv_not_supported = 1;
1001 # define process_vm_readv(...) (errno = ENOSYS, -1)
1004 #endif /* end of hack */
1007 vm_read_mem(pid_t pid, void *laddr, long raddr, size_t len)
1009 const struct iovec local = {
1013 const struct iovec remote = {
1014 .iov_base = (void *) raddr,
1018 return process_vm_readv(pid, &local, 1, &remote, 1, 0);
1022 * move `len' bytes of data from process `pid'
1023 * at address `addr' to our space at `our_addr'
1026 umoven(struct tcb *tcp, long addr, unsigned int len, void *our_addr)
1028 char *laddr = our_addr;
1030 unsigned int n, m, nread;
1033 char x[sizeof(long)];
1036 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1037 if (current_wordsize < sizeof(addr))
1038 addr &= (1ul << 8 * current_wordsize) - 1;
1041 if (!process_vm_readv_not_supported) {
1042 int r = vm_read_mem(pid, laddr, addr, len);
1043 if ((unsigned int) r == len)
1046 error_msg("umoven: short read (%u < %u) @0x%lx",
1047 (unsigned int) r, len, addr);
1052 process_vm_readv_not_supported = 1;
1055 /* operation not permitted, try PTRACE_PEEKDATA */
1058 /* the process is gone */
1060 case EFAULT: case EIO:
1061 /* address space is inaccessible */
1064 /* all the rest is strange and should be reported */
1065 perror_msg("process_vm_readv");
1071 if (addr & (sizeof(long) - 1)) {
1072 /* addr not a multiple of sizeof(long) */
1073 n = addr & (sizeof(long) - 1); /* residue */
1074 addr &= -sizeof(long); /* aligned address */
1076 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
1080 case ESRCH: case EINVAL:
1081 /* these could be seen if the process is gone */
1083 case EFAULT: case EIO: case EPERM:
1084 /* address space is inaccessible */
1087 /* all the rest is strange and should be reported */
1088 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
1092 m = MIN(sizeof(long) - n, len);
1093 memcpy(laddr, &u.x[n], m);
1094 addr += sizeof(long);
1101 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
1105 case ESRCH: case EINVAL:
1106 /* these could be seen if the process is gone */
1108 case EFAULT: case EIO: case EPERM:
1109 /* address space is inaccessible */
1111 perror_msg("umoven: short read (%u < %u) @0x%lx",
1112 nread, nread + len, addr - nread);
1116 /* all the rest is strange and should be reported */
1117 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
1121 m = MIN(sizeof(long), len);
1122 memcpy(laddr, u.x, m);
1123 addr += sizeof(long);
1133 umoven_or_printaddr(struct tcb *tcp, const long addr, const unsigned int len,
1136 if (!addr || !verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
1137 umoven(tcp, addr, len, our_addr) < 0) {
1145 * Like `umove' but make the additional effort of looking
1146 * for a terminating zero byte.
1148 * Returns < 0 on error, > 0 if NUL was seen,
1149 * (TODO if useful: return count of bytes including NUL),
1150 * else 0 if len bytes were read but no NUL byte seen.
1152 * Note: there is no guarantee we won't overwrite some bytes
1153 * in laddr[] _after_ terminating NUL (but, of course,
1154 * we never write past laddr[len-1]).
1157 umovestr(struct tcb *tcp, long addr, unsigned int len, char *laddr)
1159 #if SIZEOF_LONG == 4
1160 const unsigned long x01010101 = 0x01010101ul;
1161 const unsigned long x80808080 = 0x80808080ul;
1162 #elif SIZEOF_LONG == 8
1163 const unsigned long x01010101 = 0x0101010101010101ul;
1164 const unsigned long x80808080 = 0x8080808080808080ul;
1166 # error SIZEOF_LONG > 8
1170 unsigned int n, m, nread;
1173 char x[sizeof(long)];
1176 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1177 if (current_wordsize < sizeof(addr))
1178 addr &= (1ul << 8 * current_wordsize) - 1;
1182 if (!process_vm_readv_not_supported) {
1183 const size_t page_size = get_pagesize();
1184 const size_t page_mask = page_size - 1;
1187 unsigned int chunk_len;
1188 unsigned int end_in_page;
1191 * Don't cross pages, otherwise we can get EFAULT
1192 * and fail to notice that terminating NUL lies
1193 * in the existing (first) page.
1195 chunk_len = len > page_size ? page_size : len;
1196 end_in_page = (addr + chunk_len) & page_mask;
1197 if (chunk_len > end_in_page) /* crosses to the next page */
1198 chunk_len -= end_in_page;
1200 int r = vm_read_mem(pid, laddr, addr, chunk_len);
1202 if (memchr(laddr, '\0', r))
1212 process_vm_readv_not_supported = 1;
1213 goto vm_readv_didnt_work;
1215 /* the process is gone */
1218 /* operation not permitted, try PTRACE_PEEKDATA */
1220 goto vm_readv_didnt_work;
1222 case EFAULT: case EIO:
1223 /* address space is inaccessible */
1225 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1226 nread, nread + len, addr - nread);
1230 /* all the rest is strange and should be reported */
1231 perror_msg("process_vm_readv");
1237 vm_readv_didnt_work:
1239 if (addr & (sizeof(long) - 1)) {
1240 /* addr not a multiple of sizeof(long) */
1241 n = addr & (sizeof(long) - 1); /* residue */
1242 addr &= -sizeof(long); /* aligned address */
1244 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
1248 case ESRCH: case EINVAL:
1249 /* these could be seen if the process is gone */
1251 case EFAULT: case EIO: case EPERM:
1252 /* address space is inaccessible */
1255 /* all the rest is strange and should be reported */
1256 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1260 m = MIN(sizeof(long) - n, len);
1261 memcpy(laddr, &u.x[n], m);
1262 while (n & (sizeof(long) - 1))
1263 if (u.x[n++] == '\0')
1265 addr += sizeof(long);
1273 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
1277 case ESRCH: case EINVAL:
1278 /* these could be seen if the process is gone */
1280 case EFAULT: case EIO: case EPERM:
1281 /* address space is inaccessible */
1283 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1284 nread, nread + len, addr - nread);
1288 /* all the rest is strange and should be reported */
1289 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1293 m = MIN(sizeof(long), len);
1294 memcpy(laddr, u.x, m);
1295 /* "If a NUL char exists in this word" */
1296 if ((u.val - x01010101) & ~u.val & x80808080)
1298 addr += sizeof(long);
1307 * Iteratively fetch and print up to nmemb elements of elem_size size
1308 * from the array that starts at tracee's address start_addr.
1310 * Array elements are being fetched to the address specified by elem_buf.
1312 * The fetcher callback function specified by umoven_func should follow
1313 * the same semantics as umoven_or_printaddr function.
1315 * The printer callback function specified by print_func is expected
1316 * to print something; if it returns false, no more iterations will be made.
1318 * The pointer specified by opaque_data is passed to each invocation
1319 * of print_func callback function.
1321 * This function prints:
1322 * - "NULL", if start_addr is NULL;
1323 * - "[]", if nmemb is 0;
1324 * - start_addr, if nmemb * elem_size overflows or wraps around;
1325 * - nothing, if the first element cannot be fetched
1326 * (if umoven_func returns non-zero), but it is assumed that
1327 * umoven_func has printed the address it failed to fetch data from;
1328 * - elements of the array, delimited by ", ", with the array itself
1329 * enclosed with [] brackets.
1331 * If abbrev(tcp) is true, then
1332 * - the maximum number of elements printed equals to max_strlen;
1333 * - "..." is printed instead of max_strlen+1 element
1334 * and no more iterations will be made.
1336 * This function returns true only if
1337 * - umoven_func has been called at least once AND
1338 * - umoven_func has not returned false.
1341 print_array(struct tcb *tcp,
1342 const unsigned long start_addr,
1344 void *const elem_buf,
1345 const size_t elem_size,
1346 int (*const umoven_func)(struct tcb *,
1350 bool (*const print_func)(struct tcb *,
1354 void *const opaque_data)
1366 const size_t size = nmemb * elem_size;
1367 const unsigned long end_addr = start_addr + size;
1369 if (end_addr <= start_addr || size / elem_size != nmemb) {
1370 printaddr(start_addr);
1374 const unsigned long abbrev_end =
1375 (abbrev(tcp) && max_strlen < nmemb) ?
1376 start_addr + elem_size * max_strlen : end_addr;
1379 for (cur = start_addr; cur < end_addr; cur += elem_size) {
1380 if (cur != start_addr)
1383 if (umoven_func(tcp, cur, elem_size, elem_buf))
1386 if (cur == start_addr)
1389 if (cur >= abbrev_end) {
1395 if (!print_func(tcp, elem_buf, elem_size, opaque_data)) {
1400 if (cur != start_addr)
1403 return cur >= end_addr;
1407 printargs(struct tcb *tcp)
1409 if (entering(tcp)) {
1411 int n = tcp->s_ent->nargs;
1412 for (i = 0; i < n; i++)
1413 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1419 printargs_u(struct tcb *tcp)
1421 const int n = tcp->s_ent->nargs;
1423 for (i = 0; i < n; ++i)
1424 tprintf("%s%u", i ? ", " : "",
1425 (unsigned int) tcp->u_arg[i]);
1426 return RVAL_DECODED;
1430 printargs_d(struct tcb *tcp)
1432 const int n = tcp->s_ent->nargs;
1434 for (i = 0; i < n; ++i)
1435 tprintf("%s%d", i ? ", " : "",
1436 (int) tcp->u_arg[i]);
1437 return RVAL_DECODED;