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 unsigned int val)
126 for (; xlat->str != NULL; xlat++)
127 if (xlat->val == val)
133 xlat_bsearch_compare(const void *a, const void *b)
135 const unsigned int val1 = (const unsigned long) a;
136 const unsigned int 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 unsigned int val)
143 const struct xlat *e =
144 bsearch((const void*) (const unsigned long) 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 unsigned int 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("%#x /* %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
240 if (current_wordsize > 4) {
242 *val = tcp->u_arg[arg_no];
244 # if SUPPORTED_PERSONALITIES > 1
246 # if defined(AARCH64) || defined(POWERPC64)
247 /* Align arg_no to the next even number. */
248 arg_no = (arg_no + 1) & 0xe;
249 # endif /* AARCH64 || POWERPC64 */
250 *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
253 # endif /* SUPPORTED_PERSONALITIES > 1 */
254 #elif SIZEOF_LONG > 4
255 # error Unsupported configuration: SIZEOF_LONG > 4 && SIZEOF_LONG_LONG > SIZEOF_LONG
256 #elif defined LINUX_MIPSN32
257 *val = tcp->ext_arg[arg_no];
260 if (current_personality == 0) {
261 *val = tcp->ext_arg[arg_no];
264 *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
268 # if defined __ARM_EABI__ || \
269 defined LINUX_MIPSO32 || \
272 /* Align arg_no to the next even number. */
273 arg_no = (arg_no + 1) & 0xe;
275 *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
283 * Print 64bit argument at position arg_no and
284 * return the index of the next argument.
287 printllval(struct tcb *tcp, const char *format, int arg_no)
289 unsigned long long val = 0;
291 arg_no = getllval(tcp, &val, arg_no);
292 tprintf(format, val);
297 * Interpret `xlat' as an array of flags
298 * print the entries whose bits are on in `flags'
299 * return # of flags printed.
302 addflags(const struct xlat *xlat, int flags)
304 for (; xlat->str; xlat++) {
305 if (xlat->val && (flags & xlat->val) == xlat->val) {
306 tprintf("|%s", xlat->str);
311 tprintf("|%#x", flags);
316 * Interpret `xlat' as an array of flags.
317 * Print to static string the entries whose bits are on in `flags'
318 * Return static string.
321 sprintflags(const char *prefix, const struct xlat *xlat, int flags)
323 static char outstr[1024];
327 outptr = stpcpy(outstr, prefix);
329 for (; xlat->str; xlat++) {
330 if ((flags & xlat->val) == xlat->val) {
333 outptr = stpcpy(outptr, xlat->str);
343 outptr += sprintf(outptr, "%#x", flags);
350 printflags(const struct xlat *xlat, int flags, const char *dflt)
355 if (flags == 0 && xlat->val == 0) {
361 for (n = 0; xlat->str; xlat++) {
362 if (xlat->val && (flags & xlat->val) == xlat->val) {
363 tprintf("%s%s", sep, xlat->str);
372 tprintf("%s%#x", sep, flags);
377 tprintf("%#x", flags);
379 tprintf(" /* %s */", dflt);
390 printaddr(const long addr)
395 tprintf("%#lx", addr);
398 #define DEF_PRINTNUM(name, type) \
400 printnum_ ## name(struct tcb *tcp, const long addr, const char *fmt) \
403 if (umove_or_printaddr(tcp, addr, &num)) \
411 #define DEF_PRINTPAIR(name, type) \
413 printpair_ ## name(struct tcb *tcp, const long addr, const char *fmt) \
416 if (umove_or_printaddr(tcp, addr, &pair)) \
419 tprintf(fmt, pair[0]); \
421 tprintf(fmt, pair[1]); \
426 DEF_PRINTNUM(int, int)
427 DEF_PRINTPAIR(int, int)
428 DEF_PRINTNUM(short, short)
429 DEF_PRINTNUM(int64, uint64_t)
430 DEF_PRINTPAIR(int64, uint64_t)
432 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
434 printnum_long_int(struct tcb *tcp, const long addr,
435 const char *fmt_long, const char *fmt_int)
437 if (current_wordsize > sizeof(int)) {
438 return printnum_int64(tcp, addr, fmt_long);
440 return printnum_int(tcp, addr, fmt_int);
449 static char buf[sizeof(int) * 3 * 6];
457 snprintf(buf, sizeof buf, "%02d/%02d/%02d-%02d:%02d:%02d",
458 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
459 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
461 snprintf(buf, sizeof buf, "%lu", (unsigned long) t);
467 getfdproto(struct tcb *tcp, int fd, char *buf, unsigned bufsize)
469 #ifdef HAVE_SYS_XATTR_H
471 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
476 sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
477 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
482 * This is a protection for the case when the kernel
483 * side does not append a null byte to the buffer.
494 printfd(struct tcb *tcp, int fd)
496 char path[PATH_MAX + 1];
497 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
498 static const char socket_prefix[] = "socket:[";
499 const size_t socket_prefix_len = sizeof(socket_prefix) - 1;
500 const size_t path_len = strlen(path);
503 if (show_fd_path > 1 &&
504 strncmp(path, socket_prefix, socket_prefix_len) == 0 &&
505 path[path_len - 1] == ']') {
506 unsigned long inodenr;
507 #define PROTO_NAME_LEN 32
508 char proto_buf[PROTO_NAME_LEN];
510 getfdproto(tcp, fd, proto_buf, PROTO_NAME_LEN);
511 inodenr = strtoul(path + socket_prefix_len, NULL, 10);
512 if (!print_sockaddr_by_inode(inodenr, proto)) {
514 tprintf("%s:[%lu]", proto, inodenr);
519 print_quoted_string(path, path_len,
520 QUOTE_OMIT_LEADING_TRAILING_QUOTES);
528 * Quote string `instr' of length `size'
529 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
531 * If QUOTE_0_TERMINATED `style' flag is set,
532 * treat `instr' as a NUL-terminated string,
533 * checking up to (`size' + 1) bytes of `instr'.
535 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
536 * do not add leading and trailing quoting symbols.
538 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
539 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
542 string_quote(const char *instr, char *outstr, const unsigned int size,
543 const unsigned int style)
545 const unsigned char *ustr = (const unsigned char *) instr;
550 if (style & QUOTE_0_TERMINATED)
553 eol = 0x100; /* this can never match a char */
559 /* Check for presence of symbol which require
560 to hex-quote the whole string. */
561 for (i = 0; i < size; ++i) {
563 /* Check for NUL-terminated string. */
567 /* Force hex unless c is printable or whitespace */
572 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
573 * They happen to have ASCII codes 9,10,11,12,13.
575 if (c < ' ' && (unsigned)(c - 9) >= 5) {
582 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
586 /* Hex-quote the whole string. */
587 for (i = 0; i < size; ++i) {
589 /* Check for NUL-terminated string. */
594 *s++ = "0123456789abcdef"[c >> 4];
595 *s++ = "0123456789abcdef"[c & 0xf];
598 for (i = 0; i < size; ++i) {
600 /* Check for NUL-terminated string. */
604 case '\"': case '\\':
629 if (c >= ' ' && c <= 0x7e)
635 && ustr[i + 1] >= '0'
636 && ustr[i + 1] <= '9'
639 *s++ = '0' + (c >> 6);
640 *s++ = '0' + ((c >> 3) & 0x7);
645 *s++ = '0' + (c >> 6);
646 *s++ = '0' + ((c >> 3) & 0x7);
649 *s++ = '0' + (c & 0x7);
656 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
660 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
661 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
662 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
663 * but next char is NUL.
671 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
674 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
678 #ifndef ALLOCA_CUTOFF
679 # define ALLOCA_CUTOFF 4032
681 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
684 * Quote string `str' of length `size' and print the result.
686 * If QUOTE_0_TERMINATED `style' flag is set,
687 * treat `str' as a NUL-terminated string and
688 * quote at most (`size' - 1) bytes.
690 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
691 * do not add leading and trailing quoting symbols.
693 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
694 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
697 print_quoted_string(const char *str, unsigned int size,
698 const unsigned int style)
702 unsigned int alloc_size;
705 if (size && style & QUOTE_0_TERMINATED)
708 alloc_size = 4 * size;
709 if (alloc_size / 4 != size) {
710 error_msg("Out of memory");
714 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2);
716 if (use_alloca(alloc_size)) {
717 outstr = alloca(alloc_size);
720 outstr = buf = malloc(alloc_size);
722 error_msg("Out of memory");
728 rc = string_quote(str, outstr, size, style);
736 * Print path string specified by address `addr' and length `n'.
737 * If path length exceeds `n', append `...' to the output.
740 printpathn(struct tcb *tcp, long addr, unsigned int n)
742 char path[PATH_MAX + 1];
750 /* Cap path length to the path buffer size */
751 if (n > sizeof path - 1)
754 /* Fetch one byte more to find out whether path length > n. */
755 nul_seen = umovestr(tcp, addr, n + 1, path);
757 tprintf("%#lx", addr);
760 print_quoted_string(path, n, QUOTE_0_TERMINATED);
767 printpath(struct tcb *tcp, long addr)
769 /* Size must correspond to char path[] size in printpathn */
770 printpathn(tcp, addr, PATH_MAX);
774 * Print string specified by address `addr' and length `len'.
775 * If `len' < 0, treat the string as a NUL-terminated string.
776 * If string length exceeds `max_strlen', append `...' to the output.
779 printstr(struct tcb *tcp, long addr, long len)
781 static char *str = NULL;
791 /* Allocate static buffers if they are not allocated yet. */
793 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
795 if (outstr_size / 4 != max_strlen)
797 str = xmalloc(max_strlen + 1);
798 outstr = xmalloc(outstr_size);
804 * Treat as a NUL-terminated string: fetch one byte more
805 * because string_quote may look one byte ahead.
807 if (umovestr(tcp, addr, size + 1, str) < 0) {
808 tprintf("%#lx", addr);
811 style = QUOTE_0_TERMINATED;
814 if (size > (unsigned long)len)
815 size = (unsigned long)len;
816 if (umoven(tcp, addr, size, str) < 0) {
817 tprintf("%#lx", addr);
823 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
824 * or we were requested to print more than -s NUM chars)...
826 ellipsis = (string_quote(str, outstr, size, style) &&
827 (len < 0 || (unsigned long) len > max_strlen));
835 dumpiov(struct tcb *tcp, int len, long addr)
837 #if SUPPORTED_PERSONALITIES > 1
839 struct { u_int32_t base; u_int32_t len; } *iov32;
840 struct { u_int64_t base; u_int64_t len; } *iov64;
842 #define iov iovu.iov64
844 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
845 #define iov_iov_base(i) \
846 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
847 #define iov_iov_len(i) \
848 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
851 #define sizeof_iov sizeof(*iov)
852 #define iov_iov_base(i) iov[i].iov_base
853 #define iov_iov_len(i) iov[i].iov_len
858 size = sizeof_iov * len;
859 /* Assuming no sane program has millions of iovs */
860 if ((unsigned)len > 1024*1024 /* insane or negative size? */
861 || (iov = malloc(size)) == NULL) {
862 error_msg("Out of memory");
865 if (umoven(tcp, addr, size, iov) >= 0) {
866 for (i = 0; i < len; i++) {
867 /* include the buffer number to make it easy to
868 * match up the trace with the source */
869 tprintf(" * %lu bytes in buffer %d\n",
870 (unsigned long)iov_iov_len(i), i);
871 dumpstr(tcp, (long) iov_iov_base(i),
883 dumpstr(struct tcb *tcp, long addr, int len)
885 static int strsize = -1;
886 static unsigned char *str;
891 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
892 "1234567890123456") + /*in case I'm off by few:*/ 4)
893 /*align to 8 to make memset easier:*/ + 7) & -8
895 const unsigned char *src;
898 memset(outbuf, ' ', sizeof(outbuf));
900 if (strsize < len + 16) {
902 str = malloc(len + 16);
905 error_msg("Out of memory");
911 if (umoven(tcp, addr, len, str) < 0)
914 /* Space-pad to 16 bytes */
926 *dst++ = "0123456789abcdef"[*src >> 4];
927 *dst++ = "0123456789abcdef"[*src & 0xf];
933 dst++; /* space is there by memset */
936 dst++; /* space is there by memset */
943 if (*src >= ' ' && *src < 0x7f)
950 tprintf(" | %05x %s |\n", i - 16, outbuf);
954 #ifdef HAVE_PROCESS_VM_READV
955 /* C library supports this, but the kernel might not. */
956 static bool process_vm_readv_not_supported = 0;
959 /* Need to do this since process_vm_readv() is not yet available in libc.
960 * When libc is be updated, only "static bool process_vm_readv_not_supported"
961 * line should remain.
963 #if !defined(__NR_process_vm_readv)
965 # define __NR_process_vm_readv 347
966 # elif defined(X86_64)
967 # define __NR_process_vm_readv 310
968 # elif defined(POWERPC)
969 # define __NR_process_vm_readv 351
973 #if defined(__NR_process_vm_readv)
974 static bool process_vm_readv_not_supported = 0;
975 /* Have to avoid duplicating with the C library headers. */
976 static ssize_t strace_process_vm_readv(pid_t pid,
977 const struct iovec *lvec,
978 unsigned long liovcnt,
979 const struct iovec *rvec,
980 unsigned long riovcnt,
983 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
985 #define process_vm_readv strace_process_vm_readv
987 static bool process_vm_readv_not_supported = 1;
988 # define process_vm_readv(...) (errno = ENOSYS, -1)
991 #endif /* end of hack */
994 vm_read_mem(pid_t pid, void *laddr, long raddr, size_t len)
996 const struct iovec local = {
1000 const struct iovec remote = {
1001 .iov_base = (void *) raddr,
1005 return process_vm_readv(pid, &local, 1, &remote, 1, 0);
1009 * move `len' bytes of data from process `pid'
1010 * at address `addr' to our space at `our_addr'
1013 umoven(struct tcb *tcp, long addr, unsigned int len, void *our_addr)
1015 char *laddr = our_addr;
1017 unsigned int n, m, nread;
1020 char x[sizeof(long)];
1023 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1024 if (current_wordsize < sizeof(addr))
1025 addr &= (1ul << 8 * current_wordsize) - 1;
1028 if (!process_vm_readv_not_supported) {
1029 int r = vm_read_mem(pid, laddr, addr, len);
1030 if ((unsigned int) r == len)
1033 error_msg("umoven: short read (%u < %u) @0x%lx",
1034 (unsigned int) r, len, addr);
1039 process_vm_readv_not_supported = 1;
1042 /* operation not permitted, try PTRACE_PEEKDATA */
1045 /* the process is gone */
1047 case EFAULT: case EIO:
1048 /* address space is inaccessible */
1051 /* all the rest is strange and should be reported */
1052 perror_msg("process_vm_readv");
1058 if (addr & (sizeof(long) - 1)) {
1059 /* addr not a multiple of sizeof(long) */
1060 n = addr & (sizeof(long) - 1); /* residue */
1061 addr &= -sizeof(long); /* aligned address */
1063 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
1067 case ESRCH: case EINVAL:
1068 /* these could be seen if the process is gone */
1070 case EFAULT: case EIO: case EPERM:
1071 /* address space is inaccessible */
1074 /* all the rest is strange and should be reported */
1075 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
1079 m = MIN(sizeof(long) - n, len);
1080 memcpy(laddr, &u.x[n], m);
1081 addr += sizeof(long);
1088 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *) addr, 0);
1092 case ESRCH: case EINVAL:
1093 /* these could be seen if the process is gone */
1095 case EFAULT: case EIO: case EPERM:
1096 /* address space is inaccessible */
1098 perror_msg("umoven: short read (%u < %u) @0x%lx",
1099 nread, nread + len, addr - nread);
1103 /* all the rest is strange and should be reported */
1104 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
1108 m = MIN(sizeof(long), len);
1109 memcpy(laddr, u.x, m);
1110 addr += sizeof(long);
1120 umoven_or_printaddr(struct tcb *tcp, const long addr, const unsigned int len,
1127 if (!verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
1128 umoven(tcp, addr, len, our_addr) < 0) {
1129 tprintf("%#lx", addr);
1136 umove_long_or_printaddr(struct tcb *tcp, const long addr, long *ptr)
1138 if (current_wordsize < sizeof(*ptr)) {
1140 int r = umove_or_printaddr(tcp, addr, &val32);
1142 *ptr = (unsigned long) val32;
1145 return umove_or_printaddr(tcp, addr, ptr);
1149 * Like `umove' but make the additional effort of looking
1150 * for a terminating zero byte.
1152 * Returns < 0 on error, > 0 if NUL was seen,
1153 * (TODO if useful: return count of bytes including NUL),
1154 * else 0 if len bytes were read but no NUL byte seen.
1156 * Note: there is no guarantee we won't overwrite some bytes
1157 * in laddr[] _after_ terminating NUL (but, of course,
1158 * we never write past laddr[len-1]).
1161 umovestr(struct tcb *tcp, long addr, unsigned int len, char *laddr)
1163 #if SIZEOF_LONG == 4
1164 const unsigned long x01010101 = 0x01010101ul;
1165 const unsigned long x80808080 = 0x80808080ul;
1166 #elif SIZEOF_LONG == 8
1167 const unsigned long x01010101 = 0x0101010101010101ul;
1168 const unsigned long x80808080 = 0x8080808080808080ul;
1170 # error SIZEOF_LONG > 8
1174 unsigned int n, m, nread;
1177 char x[sizeof(long)];
1180 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1181 if (current_wordsize < sizeof(addr))
1182 addr &= (1ul << 8 * current_wordsize) - 1;
1186 if (!process_vm_readv_not_supported) {
1187 const size_t page_size = get_pagesize();
1188 const size_t page_mask = page_size - 1;
1191 unsigned int chunk_len;
1192 unsigned int end_in_page;
1195 * Don't cross pages, otherwise we can get EFAULT
1196 * and fail to notice that terminating NUL lies
1197 * in the existing (first) page.
1199 chunk_len = len > page_size ? page_size : len;
1200 end_in_page = (addr + chunk_len) & page_mask;
1201 if (chunk_len > end_in_page) /* crosses to the next page */
1202 chunk_len -= end_in_page;
1204 int r = vm_read_mem(pid, laddr, addr, chunk_len);
1206 if (memchr(laddr, '\0', r))
1216 process_vm_readv_not_supported = 1;
1217 goto vm_readv_didnt_work;
1219 /* the process is gone */
1222 /* operation not permitted, try PTRACE_PEEKDATA */
1224 goto vm_readv_didnt_work;
1226 case EFAULT: case EIO:
1227 /* address space is inaccessible */
1229 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1230 nread, nread + len, addr - nread);
1234 /* all the rest is strange and should be reported */
1235 perror_msg("process_vm_readv");
1241 vm_readv_didnt_work:
1243 if (addr & (sizeof(long) - 1)) {
1244 /* addr not a multiple of sizeof(long) */
1245 n = addr & (sizeof(long) - 1); /* residue */
1246 addr &= -sizeof(long); /* aligned address */
1248 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
1252 case ESRCH: case EINVAL:
1253 /* these could be seen if the process is gone */
1255 case EFAULT: case EIO: case EPERM:
1256 /* address space is inaccessible */
1259 /* all the rest is strange and should be reported */
1260 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1264 m = MIN(sizeof(long) - n, len);
1265 memcpy(laddr, &u.x[n], m);
1266 while (n & (sizeof(long) - 1))
1267 if (u.x[n++] == '\0')
1269 addr += sizeof(long);
1277 u.val = ptrace(PTRACE_PEEKDATA, pid, (char *)addr, 0);
1281 case ESRCH: case EINVAL:
1282 /* these could be seen if the process is gone */
1284 case EFAULT: case EIO: case EPERM:
1285 /* address space is inaccessible */
1287 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1288 nread, nread + len, addr - nread);
1292 /* all the rest is strange and should be reported */
1293 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1297 m = MIN(sizeof(long), len);
1298 memcpy(laddr, u.x, m);
1299 /* "If a NUL char exists in this word" */
1300 if ((u.val - x01010101) & ~u.val & x80808080)
1302 addr += sizeof(long);
1311 upeek(int pid, long off, long *res)
1316 val = ptrace(PTRACE_PEEKUSER, (pid_t)pid, (char *) off, 0);
1317 if (val == -1 && errno) {
1318 if (errno != ESRCH) {
1319 perror_msg("upeek: PTRACE_PEEKUSER pid:%d @0x%lx)", pid, off);