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>
9 * Copyright (c) 1999-2017 The strace developers.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/param.h>
39 #ifdef HAVE_SYS_XATTR_H
40 # include <sys/xattr.h>
43 #include <asm/unistd.h>
50 string_to_uint_ex(const char *const str, char **const endptr,
51 const unsigned int max_val, const char *const accepted_ending)
60 val = strtol(str, &end, 10);
62 if (str == end || val < 0 || (unsigned long) val > max_val
63 || (val == LONG_MAX && errno == ERANGE))
66 if (*end && (!accepted_ending || !strchr(accepted_ending, *end)))
76 string_to_uint(const char *const str)
78 return string_to_uint_upto(str, INT_MAX);
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;
143 #if !defined HAVE_STPCPY
145 stpcpy(char *dst, const char *src)
147 while ((*dst = *src++) != '\0')
153 /* Find a next bit which is set.
154 * Starts testing at cur_bit.
155 * Returns -1 if no more bits are set.
157 * We never touch bytes we don't need to.
158 * On big-endian, array is assumed to consist of
159 * current_wordsize wide words: for example, is current_wordsize is 4,
160 * the bytes are walked in 3,2,1,0, 7,6,5,4, 11,10,9,8 ... sequence.
161 * On little-endian machines, word size is immaterial.
164 next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits)
166 const unsigned endian = 1;
167 int little_endian = *(char *) (void *) &endian;
169 const uint8_t *array = bit_array;
170 unsigned pos = cur_bit / 8;
171 unsigned pos_xor_mask = little_endian ? 0 : current_wordsize-1;
177 if (cur_bit >= size_bits)
179 cur_byte = array[pos ^ pos_xor_mask];
181 cur_bit = (cur_bit + 8) & (-8);
185 bitmask = 1 << (cur_bit & 7);
187 if (cur_byte & bitmask)
190 if (cur_bit >= size_bits)
193 /* This check *can't be* optimized out: */
202 * Fetch 64bit argument at position arg_no and
203 * return the index of the next argument.
206 getllval(struct tcb *tcp, unsigned long long *val, int arg_no)
208 #if SIZEOF_KERNEL_LONG_T > 4
209 # ifndef current_klongsize
210 if (current_klongsize < SIZEOF_KERNEL_LONG_T) {
211 # if defined(AARCH64) || defined(POWERPC64)
212 /* Align arg_no to the next even number. */
213 arg_no = (arg_no + 1) & 0xe;
214 # endif /* AARCH64 || POWERPC64 */
215 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
218 # endif /* !current_klongsize */
220 *val = tcp->u_arg[arg_no];
223 #else /* SIZEOF_KERNEL_LONG_T == 4 */
224 # if defined __ARM_EABI__ \
225 || defined LINUX_MIPSO32 \
228 /* Align arg_no to the next even number. */
229 arg_no = (arg_no + 1) & 0xe;
232 * The SH4 ABI does allow long longs in odd-numbered registers, but
233 * does not allow them to be split between registers and memory - and
234 * there are only four argument registers for normal functions. As a
235 * result, pread, for example, takes an extra padding argument before
236 * the offset. This was changed late in the 2.4 series (around 2.4.20).
240 # endif /* __ARM_EABI__ || LINUX_MIPSO32 || POWERPC || XTENSA || SH */
241 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
249 * Print 64bit argument at position arg_no and
250 * return the index of the next argument.
253 printllval(struct tcb *tcp, const char *format, int arg_no)
255 unsigned long long val = 0;
257 arg_no = getllval(tcp, &val, arg_no);
258 tprintf(format, val);
263 printaddr(const kernel_ulong_t addr)
268 tprintf("%#" PRI_klx, addr);
271 #define DEF_PRINTNUM(name, type) \
273 printnum_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
274 const char *const fmt) \
277 if (umove_or_printaddr(tcp, addr, &num)) \
285 #define DEF_PRINTNUM_ADDR(name, type) \
287 printnum_addr_ ## name(struct tcb *tcp, const kernel_ulong_t addr) \
290 if (umove_or_printaddr(tcp, addr, &num)) \
298 #define DEF_PRINTPAIR(name, type) \
300 printpair_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
301 const char *const fmt) \
304 if (umove_or_printaddr(tcp, addr, &pair)) \
307 tprintf(fmt, pair[0]); \
309 tprintf(fmt, pair[1]); \
314 DEF_PRINTNUM(int, int)
315 DEF_PRINTNUM_ADDR(int, unsigned int)
316 DEF_PRINTPAIR(int, int)
317 DEF_PRINTNUM(short, short)
318 DEF_PRINTNUM(int64, uint64_t)
319 DEF_PRINTNUM_ADDR(int64, uint64_t)
320 DEF_PRINTPAIR(int64, uint64_t)
322 #ifndef current_wordsize
324 printnum_long_int(struct tcb *const tcp, const kernel_ulong_t addr,
325 const char *const fmt_long, const char *const fmt_int)
327 if (current_wordsize > sizeof(int)) {
328 return printnum_int64(tcp, addr, fmt_long);
330 return printnum_int(tcp, addr, fmt_int);
335 printnum_addr_long_int(struct tcb *tcp, const kernel_ulong_t addr)
337 if (current_wordsize > sizeof(int)) {
338 return printnum_addr_int64(tcp, addr);
340 return printnum_addr_int(tcp, addr);
343 #endif /* !current_wordsize */
345 #ifndef current_klongsize
347 printnum_addr_klong_int(struct tcb *tcp, const kernel_ulong_t addr)
349 if (current_klongsize > sizeof(int)) {
350 return printnum_addr_int64(tcp, addr);
352 return printnum_addr_int(tcp, addr);
355 #endif /* !current_klongsize */
358 * Prints time to a (static internal) buffer and returns pointer to it.
360 * @param sec Seconds since epoch.
361 * @param part_sec Amount of second parts since the start of a second.
362 * @param max_part_sec Maximum value of a valid part_sec.
363 * @param width 1 + floor(log10(max_part_sec)).
366 sprinttime_ex(const long long sec, const unsigned long long part_sec,
367 const unsigned int max_part_sec, const int width)
369 static char buf[sizeof(int) * 3 * 6 + sizeof(part_sec) * 3
372 if ((sec == 0 && part_sec == 0) || part_sec > max_part_sec)
375 time_t t = (time_t) sec;
376 struct tm *tmp = (sec == t) ? localtime(&t) : NULL;
380 size_t pos = strftime(buf, sizeof(buf), "%FT%T", tmp);
385 int ret = snprintf(buf + pos, sizeof(buf) - pos, ".%0*llu",
388 if (ret < 0 || (size_t) ret >= sizeof(buf) - pos)
394 return strftime(buf + pos, sizeof(buf) - pos, "%z", tmp) ? buf : NULL;
398 sprinttime(long long sec)
400 return sprinttime_ex(sec, 0, 0, 0);
404 sprinttime_usec(long long sec, unsigned long long usec)
406 return sprinttime_ex(sec, usec, 999999, 6);
410 sprinttime_nsec(long long sec, unsigned long long nsec)
412 return sprinttime_ex(sec, nsec, 999999999, 9);
416 getfdproto(struct tcb *tcp, int fd)
418 #ifdef HAVE_SYS_XATTR_H
419 size_t bufsize = 256;
422 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
425 return SOCK_PROTO_UNKNOWN;
427 sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
428 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
430 return SOCK_PROTO_UNKNOWN;
433 * This is a protection for the case when the kernel
434 * side does not append a null byte to the buffer.
438 return get_proto_by_name(buf);
441 return SOCK_PROTO_UNKNOWN;
446 getfdinode(struct tcb *tcp, int fd)
448 char path[PATH_MAX + 1];
450 if (getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
451 const char *str = STR_STRIP_PREFIX(path, "socket:[");
454 const size_t str_len = strlen(str);
455 if (str_len && str[str_len - 1] == ']')
456 return strtoul(str, NULL, 10);
464 printfd(struct tcb *tcp, int fd)
466 char path[PATH_MAX + 1];
467 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
473 if (show_fd_path <= 1
474 || (str = STR_STRIP_PREFIX(path, "socket:[")) == path
475 || !(len = strlen(str))
476 || str[len - 1] != ']'
477 || !(inode = strtoul(str, NULL, 10))
478 || !print_sockaddr_by_inode(tcp, fd, inode)) {
479 print_quoted_string(path, strlen(path),
480 QUOTE_OMIT_LEADING_TRAILING_QUOTES);
488 * Quote string `instr' of length `size'
489 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
491 * If QUOTE_0_TERMINATED `style' flag is set,
492 * treat `instr' as a NUL-terminated string,
493 * checking up to (`size' + 1) bytes of `instr'.
495 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
496 * do not add leading and trailing quoting symbols.
498 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
499 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
502 string_quote(const char *instr, char *outstr, const unsigned int size,
503 const unsigned int style)
505 const unsigned char *ustr = (const unsigned char *) instr;
510 if (style & QUOTE_0_TERMINATED)
513 eol = 0x100; /* this can never match a char */
516 if ((xflag > 1) || (style & QUOTE_FORCE_HEX)) {
519 /* Check for presence of symbol which require
520 to hex-quote the whole string. */
521 for (i = 0; i < size; ++i) {
523 /* Check for NUL-terminated string. */
527 /* Force hex unless c is printable or whitespace */
532 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
533 * They happen to have ASCII codes 9,10,11,12,13.
535 if (c < ' ' && (unsigned)(c - 9) >= 5) {
542 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
546 /* Hex-quote the whole string. */
547 for (i = 0; i < size; ++i) {
549 /* Check for NUL-terminated string. */
554 *s++ = "0123456789abcdef"[c >> 4];
555 *s++ = "0123456789abcdef"[c & 0xf];
558 for (i = 0; i < size; ++i) {
560 /* Check for NUL-terminated string. */
563 if ((i == (size - 1)) &&
564 (style & QUOTE_OMIT_TRAILING_0) && (c == '\0'))
567 case '\"': case '\\':
592 if (c >= ' ' && c <= 0x7e)
598 && ustr[i + 1] >= '0'
599 && ustr[i + 1] <= '9'
602 *s++ = '0' + (c >> 6);
603 *s++ = '0' + ((c >> 3) & 0x7);
608 *s++ = '0' + (c >> 6);
609 *s++ = '0' + ((c >> 3) & 0x7);
612 *s++ = '0' + (c & 0x7);
619 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
623 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
624 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
625 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
626 * but next char is NUL.
634 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
637 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
641 #ifndef ALLOCA_CUTOFF
642 # define ALLOCA_CUTOFF 4032
644 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
647 * Quote string `str' of length `size' and print the result.
649 * If QUOTE_0_TERMINATED `style' flag is set,
650 * treat `str' as a NUL-terminated string and
651 * quote at most (`size' - 1) bytes.
653 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
654 * do not add leading and trailing quoting symbols.
656 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
657 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
660 print_quoted_string(const char *str, unsigned int size,
661 const unsigned int style)
665 unsigned int alloc_size;
668 if (size && style & QUOTE_0_TERMINATED)
671 alloc_size = 4 * size;
672 if (alloc_size / 4 != size) {
673 error_msg("Out of memory");
677 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2);
679 if (use_alloca(alloc_size)) {
680 outstr = alloca(alloc_size);
683 outstr = buf = malloc(alloc_size);
685 error_msg("Out of memory");
691 rc = string_quote(str, outstr, size, style);
699 * Print path string specified by address `addr' and length `n'.
700 * If path length exceeds `n', append `...' to the output.
703 printpathn(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int n)
705 char path[PATH_MAX + 1];
713 /* Cap path length to the path buffer size */
714 if (n > sizeof(path) - 1)
715 n = sizeof(path) - 1;
717 /* Fetch one byte more to find out whether path length > n. */
718 nul_seen = umovestr(tcp, addr, n + 1, path);
723 print_quoted_string(path, n, QUOTE_0_TERMINATED);
730 printpath(struct tcb *const tcp, const kernel_ulong_t addr)
732 /* Size must correspond to char path[] size in printpathn */
733 printpathn(tcp, addr, PATH_MAX);
737 * Print string specified by address `addr' and length `len'.
738 * If `user_style' has QUOTE_0_TERMINATED bit set, treat the string
739 * as a NUL-terminated string.
740 * Pass `user_style' on to `string_quote'.
741 * Append `...' to the output if either the string length exceeds `max_strlen',
742 * or QUOTE_0_TERMINATED bit is set and the string length exceeds `len'.
745 printstr_ex(struct tcb *const tcp, const kernel_ulong_t addr,
746 const kernel_ulong_t len, const unsigned int user_style)
752 unsigned int style = user_style;
760 /* Allocate static buffers if they are not allocated yet. */
762 const unsigned int outstr_size =
763 4 * max_strlen + /* for quotes and NUL */ 3;
765 * We can assume that outstr_size / 4 == max_strlen
766 * since we have a guarantee that max_strlen <= -1U / 4.
769 str = xmalloc(max_strlen + 1);
770 outstr = xmalloc(outstr_size);
773 /* Fetch one byte more because string_quote may look one byte ahead. */
774 size = max_strlen + 1;
778 if (style & QUOTE_0_TERMINATED)
779 rc = umovestr(tcp, addr, size, str);
781 rc = umoven(tcp, addr, size, str);
788 if (size > max_strlen)
793 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
794 * or we were requested to print more than -s NUM chars)...
796 ellipsis = string_quote(str, outstr, size, style)
798 && ((style & QUOTE_0_TERMINATED)
799 || len > max_strlen);
807 dumpiov_upto(struct tcb *const tcp, const int len, const kernel_ulong_t addr,
808 kernel_ulong_t data_size)
810 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
812 struct { uint32_t base; uint32_t len; } *iov32;
813 struct { uint64_t base; uint64_t len; } *iov64;
815 #define iov iovu.iov64
817 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
818 #define iov_iov_base(i) \
819 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
820 #define iov_iov_len(i) \
821 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
824 #define sizeof_iov sizeof(*iov)
825 #define iov_iov_base(i) ptr_to_kulong(iov[i].iov_base)
826 #define iov_iov_len(i) iov[i].iov_len
831 size = sizeof_iov * len;
832 /* Assuming no sane program has millions of iovs */
833 if ((unsigned)len > 1024*1024 /* insane or negative size? */
834 || (iov = malloc(size)) == NULL) {
835 error_msg("Out of memory");
838 if (umoven(tcp, addr, size, iov) >= 0) {
839 for (i = 0; i < len; i++) {
840 kernel_ulong_t iov_len = iov_iov_len(i);
841 if (iov_len > data_size)
845 data_size -= iov_len;
846 /* include the buffer number to make it easy to
847 * match up the trace with the source */
848 tprintf(" * %" PRI_klu " bytes in buffer %d\n", iov_len, i);
849 dumpstr(tcp, iov_iov_base(i), iov_len);
860 dumpstr(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
862 static int strsize = -1;
863 static unsigned char *str;
868 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
869 "1234567890123456") + /*in case I'm off by few:*/ 4)
870 /*align to 8 to make memset easier:*/ + 7) & -8
872 const unsigned char *src;
875 memset(outbuf, ' ', sizeof(outbuf));
877 if (strsize < len + 16) {
879 str = malloc(len + 16);
882 error_msg("Out of memory");
888 if (umoven(tcp, addr, len, str) < 0)
891 /* Space-pad to 16 bytes */
903 *dst++ = "0123456789abcdef"[*src >> 4];
904 *dst++ = "0123456789abcdef"[*src & 0xf];
909 dst++; /* space is there by memset */
912 dst++; /* space is there by memset */
919 if (*src >= ' ' && *src < 0x7f)
926 tprintf(" | %05x %s |\n", i - 16, outbuf);
930 static bool process_vm_readv_not_supported;
932 #ifndef HAVE_PROCESS_VM_READV
934 * Need to do this since process_vm_readv() is not yet available in libc.
935 * When libc is be updated, only "static bool process_vm_readv_not_supported"
936 * line should remain.
938 /* Have to avoid duplicating with the C library headers. */
939 static ssize_t strace_process_vm_readv(pid_t pid,
940 const struct iovec *lvec,
941 unsigned long liovcnt,
942 const struct iovec *rvec,
943 unsigned long riovcnt,
946 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
948 # define process_vm_readv strace_process_vm_readv
949 #endif /* !HAVE_PROCESS_VM_READV */
952 vm_read_mem(const pid_t pid, void *const laddr,
953 const kernel_ulong_t raddr, const size_t len)
955 const unsigned long truncated_raddr = raddr;
957 if (raddr != (kernel_ulong_t) truncated_raddr) {
962 const struct iovec local = {
966 const struct iovec remote = {
967 .iov_base = (void *) truncated_raddr,
971 return process_vm_readv(pid, &local, 1, &remote, 1, 0);
975 * move `len' bytes of data from process `pid'
976 * at address `addr' to our space at `our_addr'
979 umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
980 void *const our_addr)
982 char *laddr = our_addr;
984 unsigned int n, m, nread;
987 char x[sizeof(long)];
990 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
991 if (current_wordsize < sizeof(addr)
992 && (addr & (~(kernel_ulong_t) -1U))) {
997 if (!process_vm_readv_not_supported) {
998 int r = vm_read_mem(pid, laddr, addr, len);
999 if ((unsigned int) r == len)
1002 error_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
1003 (unsigned int) r, len, addr);
1008 process_vm_readv_not_supported = 1;
1011 /* operation not permitted, try PTRACE_PEEKDATA */
1014 /* the process is gone */
1016 case EFAULT: case EIO:
1017 /* address space is inaccessible */
1020 /* all the rest is strange and should be reported */
1021 perror_msg("process_vm_readv");
1027 if (addr & (sizeof(long) - 1)) {
1028 /* addr not a multiple of sizeof(long) */
1029 n = addr & (sizeof(long) - 1); /* residue */
1030 addr &= -sizeof(long); /* aligned address */
1032 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1036 case ESRCH: case EINVAL:
1037 /* these could be seen if the process is gone */
1039 case EFAULT: case EIO: case EPERM:
1040 /* address space is inaccessible */
1043 /* all the rest is strange and should be reported */
1044 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1048 m = MIN(sizeof(long) - n, len);
1049 memcpy(laddr, &u.x[n], m);
1050 addr += sizeof(long);
1057 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1061 case ESRCH: case EINVAL:
1062 /* these could be seen if the process is gone */
1064 case EFAULT: case EIO: case EPERM:
1065 /* address space is inaccessible */
1067 perror_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
1068 nread, nread + len, addr - nread);
1072 /* all the rest is strange and should be reported */
1073 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1077 m = MIN(sizeof(long), len);
1078 memcpy(laddr, u.x, m);
1079 addr += sizeof(long);
1089 umoven_or_printaddr(struct tcb *const tcp, const kernel_ulong_t addr,
1090 const unsigned int len, void *const our_addr)
1092 if (!addr || !verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
1093 umoven(tcp, addr, len, our_addr) < 0) {
1101 umoven_or_printaddr_ignore_syserror(struct tcb *const tcp,
1102 const kernel_ulong_t addr,
1103 const unsigned int len,
1104 void *const our_addr)
1106 if (!addr || !verbose(tcp) || umoven(tcp, addr, len, our_addr) < 0) {
1114 * Like `umove' but make the additional effort of looking
1115 * for a terminating zero byte.
1117 * Returns < 0 on error, > 0 if NUL was seen,
1118 * (TODO if useful: return count of bytes including NUL),
1119 * else 0 if len bytes were read but no NUL byte seen.
1121 * Note: there is no guarantee we won't overwrite some bytes
1122 * in laddr[] _after_ terminating NUL (but, of course,
1123 * we never write past laddr[len-1]).
1126 umovestr(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len, char *laddr)
1128 const unsigned long x01010101 = (unsigned long) 0x0101010101010101ULL;
1129 const unsigned long x80808080 = (unsigned long) 0x8080808080808080ULL;
1132 unsigned int n, m, nread;
1135 char x[sizeof(long)];
1138 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1139 if (current_wordsize < sizeof(addr)
1140 && (addr & (~(kernel_ulong_t) -1U))) {
1146 if (!process_vm_readv_not_supported) {
1147 const size_t page_size = get_pagesize();
1148 const size_t page_mask = page_size - 1;
1151 unsigned int chunk_len;
1152 unsigned int end_in_page;
1155 * Don't cross pages, otherwise we can get EFAULT
1156 * and fail to notice that terminating NUL lies
1157 * in the existing (first) page.
1159 chunk_len = len > page_size ? page_size : len;
1160 end_in_page = (addr + chunk_len) & page_mask;
1161 if (chunk_len > end_in_page) /* crosses to the next page */
1162 chunk_len -= end_in_page;
1164 int r = vm_read_mem(pid, laddr, addr, chunk_len);
1166 if (memchr(laddr, '\0', r))
1176 process_vm_readv_not_supported = 1;
1177 goto vm_readv_didnt_work;
1179 /* the process is gone */
1182 /* operation not permitted, try PTRACE_PEEKDATA */
1184 goto vm_readv_didnt_work;
1186 case EFAULT: case EIO:
1187 /* address space is inaccessible */
1189 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
1190 nread, nread + len, addr - nread);
1194 /* all the rest is strange and should be reported */
1195 perror_msg("process_vm_readv");
1201 vm_readv_didnt_work:
1203 if (addr & (sizeof(long) - 1)) {
1204 /* addr not a multiple of sizeof(long) */
1205 n = addr & (sizeof(long) - 1); /* residue */
1206 addr &= -sizeof(long); /* aligned address */
1208 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1212 case ESRCH: case EINVAL:
1213 /* these could be seen if the process is gone */
1215 case EFAULT: case EIO: case EPERM:
1216 /* address space is inaccessible */
1219 /* all the rest is strange and should be reported */
1220 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1224 m = MIN(sizeof(long) - n, len);
1225 memcpy(laddr, &u.x[n], m);
1226 while (n & (sizeof(long) - 1))
1227 if (u.x[n++] == '\0')
1229 addr += sizeof(long);
1237 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1241 case ESRCH: case EINVAL:
1242 /* these could be seen if the process is gone */
1244 case EFAULT: case EIO: case EPERM:
1245 /* address space is inaccessible */
1247 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
1248 nread, nread + len, addr - nread);
1252 /* all the rest is strange and should be reported */
1253 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1257 m = MIN(sizeof(long), len);
1258 memcpy(laddr, u.x, m);
1259 /* "If a NUL char exists in this word" */
1260 if ((u.val - x01010101) & ~u.val & x80808080)
1262 addr += sizeof(long);
1271 * Iteratively fetch and print up to nmemb elements of elem_size size
1272 * from the array that starts at tracee's address start_addr.
1274 * Array elements are being fetched to the address specified by elem_buf.
1276 * The fetcher callback function specified by umoven_func should follow
1277 * the same semantics as umoven_or_printaddr function.
1279 * The printer callback function specified by print_func is expected
1280 * to print something; if it returns false, no more iterations will be made.
1282 * The pointer specified by opaque_data is passed to each invocation
1283 * of print_func callback function.
1285 * This function prints:
1286 * - "NULL", if start_addr is NULL;
1287 * - "[]", if nmemb is 0;
1288 * - start_addr, if nmemb * elem_size overflows or wraps around;
1289 * - nothing, if the first element cannot be fetched
1290 * (if umoven_func returns non-zero), but it is assumed that
1291 * umoven_func has printed the address it failed to fetch data from;
1292 * - elements of the array, delimited by ", ", with the array itself
1293 * enclosed with [] brackets.
1295 * If abbrev(tcp) is true, then
1296 * - the maximum number of elements printed equals to max_strlen;
1297 * - "..." is printed instead of max_strlen+1 element
1298 * and no more iterations will be made.
1300 * This function returns true only if
1301 * - umoven_func has been called at least once AND
1302 * - umoven_func has not returned false.
1305 print_array(struct tcb *const tcp,
1306 const kernel_ulong_t start_addr,
1308 void *const elem_buf,
1309 const size_t elem_size,
1310 int (*const umoven_func)(struct tcb *,
1314 bool (*const print_func)(struct tcb *,
1318 void *const opaque_data)
1330 const size_t size = nmemb * elem_size;
1331 const kernel_ulong_t end_addr = start_addr + size;
1333 if (end_addr <= start_addr || size / elem_size != nmemb) {
1334 printaddr(start_addr);
1338 const kernel_ulong_t abbrev_end =
1339 (abbrev(tcp) && max_strlen < nmemb) ?
1340 start_addr + elem_size * max_strlen : end_addr;
1343 for (cur = start_addr; cur < end_addr; cur += elem_size) {
1344 if (cur != start_addr)
1347 if (umoven_func(tcp, cur, elem_size, elem_buf))
1350 if (cur == start_addr)
1353 if (cur >= abbrev_end) {
1359 if (!print_func(tcp, elem_buf, elem_size, opaque_data)) {
1364 if (cur != start_addr)
1367 return cur >= end_addr;
1371 printargs(struct tcb *tcp)
1373 const int n = tcp->s_ent->nargs;
1375 for (i = 0; i < n; ++i)
1376 tprintf("%s%#" PRI_klx, i ? ", " : "", tcp->u_arg[i]);
1377 return RVAL_DECODED;
1381 printargs_u(struct tcb *tcp)
1383 const int n = tcp->s_ent->nargs;
1385 for (i = 0; i < n; ++i)
1386 tprintf("%s%u", i ? ", " : "",
1387 (unsigned int) tcp->u_arg[i]);
1388 return RVAL_DECODED;
1392 printargs_d(struct tcb *tcp)
1394 const int n = tcp->s_ent->nargs;
1396 for (i = 0; i < n; ++i)
1397 tprintf("%s%d", i ? ", " : "",
1398 (int) tcp->u_arg[i]);
1399 return RVAL_DECODED;
1402 /* Print abnormal high bits of a kernel_ulong_t value. */
1404 print_abnormal_hi(const kernel_ulong_t val)
1406 if (current_klongsize > 4) {
1407 const unsigned int hi = (unsigned int) ((uint64_t) val >> 32);
1409 tprintf("%#x<<32|", hi);
1413 #if defined _LARGEFILE64_SOURCE && defined HAVE_OPEN64
1414 # define open_file open64
1416 # define open_file open
1420 read_int_from_file(const char *const fname, int *const pvalue)
1422 const int fd = open_file(fname, O_RDONLY);
1427 char buf[sizeof(lval) * 3];
1428 int n = read(fd, buf, sizeof(buf) - 1);
1429 int saved_errno = errno;
1433 errno = saved_errno;
1440 lval = strtol(buf, &endptr, 10);
1441 if (!endptr || (*endptr && '\n' != *endptr)
1442 #if INT_MAX < LONG_MAX
1443 || lval > INT_MAX || lval < INT_MIN
1445 || ERANGE == errno) {
1451 *pvalue = (int) lval;