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>
42 #include <asm/unistd.h>
49 string_to_uint_ex(const char *const str, char **const endptr,
50 const unsigned int max_val, const char *const accepted_ending)
59 val = strtol(str, &end, 10);
61 if (str == end || val < 0 || (unsigned long) val > max_val
62 || (val == LONG_MAX && errno == ERANGE))
65 if (*end && (!accepted_ending || !strchr(accepted_ending, *end)))
75 string_to_uint(const char *const str)
77 return string_to_uint_upto(str, INT_MAX);
81 tv_nz(const struct timeval *a)
83 return a->tv_sec || a->tv_usec;
87 tv_cmp(const struct timeval *a, const struct timeval *b)
89 if (a->tv_sec < b->tv_sec
90 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
92 if (a->tv_sec > b->tv_sec
93 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
99 tv_float(const struct timeval *tv)
101 return tv->tv_sec + tv->tv_usec/1000000.0;
105 tv_add(struct timeval *tv, const struct timeval *a, const struct timeval *b)
107 tv->tv_sec = a->tv_sec + b->tv_sec;
108 tv->tv_usec = a->tv_usec + b->tv_usec;
109 if (tv->tv_usec >= 1000000) {
111 tv->tv_usec -= 1000000;
116 tv_sub(struct timeval *tv, const struct timeval *a, const struct timeval *b)
118 tv->tv_sec = a->tv_sec - b->tv_sec;
119 tv->tv_usec = a->tv_usec - b->tv_usec;
120 if (((long) tv->tv_usec) < 0) {
122 tv->tv_usec += 1000000;
127 tv_div(struct timeval *tv, const struct timeval *a, int n)
129 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
130 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
131 tv->tv_usec %= 1000000;
135 tv_mul(struct timeval *tv, const struct timeval *a, int n)
137 tv->tv_usec = a->tv_usec * n;
138 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
139 tv->tv_usec %= 1000000;
143 xlookup(const struct xlat *xlat, const uint64_t val)
145 for (; xlat->str != NULL; xlat++)
146 if (xlat->val == val)
152 xlat_bsearch_compare(const void *a, const void *b)
154 const uint64_t val1 = *(const uint64_t *) a;
155 const uint64_t val2 = ((const struct xlat *) b)->val;
156 return (val1 > val2) ? 1 : (val1 < val2) ? -1 : 0;
160 xlat_search(const struct xlat *xlat, const size_t nmemb, const uint64_t val)
162 const struct xlat *e =
163 bsearch((const void*) &val,
164 xlat, nmemb, sizeof(*xlat), xlat_bsearch_compare);
166 return e ? e->str : NULL;
169 #if !defined HAVE_STPCPY
171 stpcpy(char *dst, const char *src)
173 while ((*dst = *src++) != '\0')
179 /* Find a next bit which is set.
180 * Starts testing at cur_bit.
181 * Returns -1 if no more bits are set.
183 * We never touch bytes we don't need to.
184 * On big-endian, array is assumed to consist of
185 * current_wordsize wide words: for example, is current_wordsize is 4,
186 * the bytes are walked in 3,2,1,0, 7,6,5,4, 11,10,9,8 ... sequence.
187 * On little-endian machines, word size is immaterial.
190 next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits)
192 const unsigned endian = 1;
193 int little_endian = * (char *) (void *) &endian;
195 const uint8_t *array = bit_array;
196 unsigned pos = cur_bit / 8;
197 unsigned pos_xor_mask = little_endian ? 0 : current_wordsize-1;
203 if (cur_bit >= size_bits)
205 cur_byte = array[pos ^ pos_xor_mask];
207 cur_bit = (cur_bit + 8) & (-8);
211 bitmask = 1 << (cur_bit & 7);
213 if (cur_byte & bitmask)
216 if (cur_bit >= size_bits)
219 /* This check *can't be* optimized out: */
228 * Print entry in struct xlat table, if there.
230 * @param val Value to search a literal representation for.
231 * @param dflt String (abbreviated in comment syntax) which should be emitted
232 * if no appropriate xlat value has been found.
233 * @param xlat (And the following arguments) Pointers to arrays of xlat values.
234 * The last argument should be NULL.
235 * @return 1 if appropriate xlat value has been found, 0 otherwise.
238 printxvals(const uint64_t val, const char *dflt, const struct xlat *xlat, ...)
242 va_start(args, xlat);
243 for (; xlat; xlat = va_arg(args, const struct xlat *)) {
244 const char *str = xlookup(xlat, val);
252 /* No hits -- print raw # instead. */
253 tprintf("%#" PRIx64, val);
255 tprintf(" /* %s */", dflt);
263 * Print entry in sorted struct xlat table, if it is there.
265 * @param xlat Pointer to an array of xlat values (not terminated with
267 * @param xlat_size Number of xlat elements present in array (usually ARRAY_SIZE
268 * if array is declared in the unit's scope and not
269 * terminated with XLAT_END).
270 * @param val Value to search literal representation for.
271 * @param dflt String (abbreviated in comment syntax) which should be
272 * emitted if no appropriate xlat value has been found.
273 * @return 1 if appropriate xlat value has been found, 0
277 printxval_searchn(const struct xlat *xlat, size_t xlat_size, uint64_t val,
280 const char *s = xlat_search(xlat, xlat_size, val);
287 tprintf("%#" PRIx64, val);
289 tprintf(" /* %s */", dflt);
295 * Fetch 64bit argument at position arg_no and
296 * return the index of the next argument.
299 getllval(struct tcb *tcp, unsigned long long *val, int arg_no)
301 #if SIZEOF_KERNEL_LONG_T > 4
302 # ifndef current_klongsize
303 if (current_klongsize < SIZEOF_KERNEL_LONG_T) {
304 # if defined(AARCH64) || defined(POWERPC64)
305 /* Align arg_no to the next even number. */
306 arg_no = (arg_no + 1) & 0xe;
307 # endif /* AARCH64 || POWERPC64 */
308 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
311 # endif /* !current_klongsize */
313 *val = tcp->u_arg[arg_no];
316 #else /* SIZEOF_KERNEL_LONG_T == 4 */
317 # if defined __ARM_EABI__ || \
318 defined LINUX_MIPSO32 || \
321 /* Align arg_no to the next even number. */
322 arg_no = (arg_no + 1) & 0xe;
325 * The SH4 ABI does allow long longs in odd-numbered registers, but
326 * does not allow them to be split between registers and memory - and
327 * there are only four argument registers for normal functions. As a
328 * result, pread, for example, takes an extra padding argument before
329 * the offset. This was changed late in the 2.4 series (around 2.4.20).
333 # endif /* __ARM_EABI__ || LINUX_MIPSO32 || POWERPC || XTENSA || SH */
334 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
342 * Print 64bit argument at position arg_no and
343 * return the index of the next argument.
346 printllval(struct tcb *tcp, const char *format, int arg_no)
348 unsigned long long val = 0;
350 arg_no = getllval(tcp, &val, arg_no);
351 tprintf(format, val);
356 * Interpret `xlat' as an array of flags
357 * print the entries whose bits are on in `flags'
358 * return # of flags printed.
361 addflags(const struct xlat *xlat, uint64_t flags)
363 for (; xlat->str; xlat++) {
364 if (xlat->val && (flags & xlat->val) == xlat->val) {
365 tprintf("|%s", xlat->str);
370 tprintf("|%#" PRIx64, flags);
375 * Interpret `xlat' as an array of flags.
376 * Print to static string the entries whose bits are on in `flags'
377 * Return static string.
380 sprintflags(const char *prefix, const struct xlat *xlat, uint64_t flags)
382 static char outstr[1024];
386 outptr = stpcpy(outstr, prefix);
388 if (flags == 0 && xlat->val == 0 && xlat->str) {
389 strcpy(outptr, xlat->str);
393 for (; xlat->str; xlat++) {
394 if (xlat->val && (flags & xlat->val) == xlat->val) {
397 outptr = stpcpy(outptr, xlat->str);
407 outptr += sprintf(outptr, "%#" PRIx64, flags);
414 printflags64(const struct xlat *xlat, uint64_t flags, const char *dflt)
419 if (flags == 0 && xlat->val == 0 && xlat->str) {
425 for (n = 0; xlat->str; xlat++) {
426 if (xlat->val && (flags & xlat->val) == xlat->val) {
427 tprintf("%s%s", sep, xlat->str);
436 tprintf("%s%#" PRIx64, sep, flags);
441 tprintf("%#" PRIx64, flags);
443 tprintf(" /* %s */", dflt);
454 printaddr(const kernel_ulong_t addr)
459 tprintf("%#" PRI_klx, addr);
462 #define DEF_PRINTNUM(name, type) \
464 printnum_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
465 const char *const fmt) \
468 if (umove_or_printaddr(tcp, addr, &num)) \
476 #define DEF_PRINTNUM_ADDR(name, type) \
478 printnum_addr_ ## name(struct tcb *tcp, const kernel_ulong_t addr) \
481 if (umove_or_printaddr(tcp, addr, &num)) \
489 #define DEF_PRINTPAIR(name, type) \
491 printpair_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
492 const char *const fmt) \
495 if (umove_or_printaddr(tcp, addr, &pair)) \
498 tprintf(fmt, pair[0]); \
500 tprintf(fmt, pair[1]); \
505 DEF_PRINTNUM(int, int)
506 DEF_PRINTNUM_ADDR(int, unsigned int)
507 DEF_PRINTPAIR(int, int)
508 DEF_PRINTNUM(short, short)
509 DEF_PRINTNUM(int64, uint64_t)
510 DEF_PRINTNUM_ADDR(int64, uint64_t)
511 DEF_PRINTPAIR(int64, uint64_t)
513 #ifndef current_wordsize
515 printnum_long_int(struct tcb *const tcp, const kernel_ulong_t addr,
516 const char *const fmt_long, const char *const fmt_int)
518 if (current_wordsize > sizeof(int)) {
519 return printnum_int64(tcp, addr, fmt_long);
521 return printnum_int(tcp, addr, fmt_int);
526 printnum_addr_long_int(struct tcb *tcp, const kernel_ulong_t addr)
528 if (current_wordsize > sizeof(int)) {
529 return printnum_addr_int64(tcp, addr);
531 return printnum_addr_int(tcp, addr);
534 #endif /* !current_wordsize */
536 #ifndef current_klongsize
538 printnum_addr_klong_int(struct tcb *tcp, const kernel_ulong_t addr)
540 if (current_klongsize > sizeof(int)) {
541 return printnum_addr_int64(tcp, addr);
543 return printnum_addr_int(tcp, addr);
546 #endif /* !current_klongsize */
552 static char buf[sizeof(int) * 3 * 6 + sizeof("+0000")];
560 strftime(buf, sizeof(buf), "%FT%T%z", tmp);
562 snprintf(buf, sizeof(buf), "%lu", (unsigned long) t);
568 getfdproto(struct tcb *tcp, int fd)
570 #ifdef HAVE_SYS_XATTR_H
571 size_t bufsize = 256;
574 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
577 return SOCK_PROTO_UNKNOWN;
579 sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
580 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
582 return SOCK_PROTO_UNKNOWN;
585 * This is a protection for the case when the kernel
586 * side does not append a null byte to the buffer.
590 return get_proto_by_name(buf);
593 return SOCK_PROTO_UNKNOWN;
598 printfd(struct tcb *tcp, int fd)
600 char path[PATH_MAX + 1];
601 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
602 static const char socket_prefix[] = "socket:[";
603 const size_t socket_prefix_len = sizeof(socket_prefix) - 1;
604 const size_t path_len = strlen(path);
607 if (show_fd_path > 1 &&
608 strncmp(path, socket_prefix, socket_prefix_len) == 0 &&
609 path[path_len - 1] == ']') {
610 unsigned long inode =
611 strtoul(path + socket_prefix_len, NULL, 10);
613 if (!print_sockaddr_by_inode_cached(inode)) {
614 const enum sock_proto proto =
616 if (!print_sockaddr_by_inode(inode, proto))
620 print_quoted_string(path, path_len,
621 QUOTE_OMIT_LEADING_TRAILING_QUOTES);
629 * Quote string `instr' of length `size'
630 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
632 * If QUOTE_0_TERMINATED `style' flag is set,
633 * treat `instr' as a NUL-terminated string,
634 * checking up to (`size' + 1) bytes of `instr'.
636 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
637 * do not add leading and trailing quoting symbols.
639 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
640 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
643 string_quote(const char *instr, char *outstr, const unsigned int size,
644 const unsigned int style)
646 const unsigned char *ustr = (const unsigned char *) instr;
651 if (style & QUOTE_0_TERMINATED)
654 eol = 0x100; /* this can never match a char */
657 if ((xflag > 1) || (style & QUOTE_FORCE_HEX)) {
660 /* Check for presence of symbol which require
661 to hex-quote the whole string. */
662 for (i = 0; i < size; ++i) {
664 /* Check for NUL-terminated string. */
668 /* Force hex unless c is printable or whitespace */
673 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
674 * They happen to have ASCII codes 9,10,11,12,13.
676 if (c < ' ' && (unsigned)(c - 9) >= 5) {
683 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
687 /* Hex-quote the whole string. */
688 for (i = 0; i < size; ++i) {
690 /* Check for NUL-terminated string. */
695 *s++ = "0123456789abcdef"[c >> 4];
696 *s++ = "0123456789abcdef"[c & 0xf];
699 for (i = 0; i < size; ++i) {
701 /* Check for NUL-terminated string. */
704 if ((i == (size - 1)) &&
705 (style & QUOTE_OMIT_TRAILING_0) && (c == '\0'))
708 case '\"': case '\\':
733 if (c >= ' ' && c <= 0x7e)
739 && ustr[i + 1] >= '0'
740 && ustr[i + 1] <= '9'
743 *s++ = '0' + (c >> 6);
744 *s++ = '0' + ((c >> 3) & 0x7);
749 *s++ = '0' + (c >> 6);
750 *s++ = '0' + ((c >> 3) & 0x7);
753 *s++ = '0' + (c & 0x7);
760 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
764 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
765 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
766 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
767 * but next char is NUL.
775 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
778 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
782 #ifndef ALLOCA_CUTOFF
783 # define ALLOCA_CUTOFF 4032
785 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
788 * Quote string `str' of length `size' and print the result.
790 * If QUOTE_0_TERMINATED `style' flag is set,
791 * treat `str' as a NUL-terminated string and
792 * quote at most (`size' - 1) bytes.
794 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
795 * do not add leading and trailing quoting symbols.
797 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
798 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
801 print_quoted_string(const char *str, unsigned int size,
802 const unsigned int style)
806 unsigned int alloc_size;
809 if (size && style & QUOTE_0_TERMINATED)
812 alloc_size = 4 * size;
813 if (alloc_size / 4 != size) {
814 error_msg("Out of memory");
818 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2);
820 if (use_alloca(alloc_size)) {
821 outstr = alloca(alloc_size);
824 outstr = buf = malloc(alloc_size);
826 error_msg("Out of memory");
832 rc = string_quote(str, outstr, size, style);
840 * Print path string specified by address `addr' and length `n'.
841 * If path length exceeds `n', append `...' to the output.
844 printpathn(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int n)
846 char path[PATH_MAX + 1];
854 /* Cap path length to the path buffer size */
855 if (n > sizeof path - 1)
858 /* Fetch one byte more to find out whether path length > n. */
859 nul_seen = umovestr(tcp, addr, n + 1, path);
864 print_quoted_string(path, n, QUOTE_0_TERMINATED);
871 printpath(struct tcb *const tcp, const kernel_ulong_t addr)
873 /* Size must correspond to char path[] size in printpathn */
874 printpathn(tcp, addr, PATH_MAX);
878 * Print string specified by address `addr' and length `len'.
879 * If `user_style' has QUOTE_0_TERMINATED bit set, treat the string
880 * as a NUL-terminated string.
881 * Pass `user_style' on to `string_quote'.
882 * Append `...' to the output if either the string length exceeds `max_strlen',
883 * or QUOTE_0_TERMINATED bit is set and the string length exceeds `len'.
886 printstr_ex(struct tcb *const tcp, const kernel_ulong_t addr,
887 const kernel_ulong_t len, const unsigned int user_style)
889 static char *str = NULL;
892 unsigned int style = user_style;
900 /* Allocate static buffers if they are not allocated yet. */
902 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
904 if (outstr_size / 4 != max_strlen)
906 str = xmalloc(max_strlen + 1);
907 outstr = xmalloc(outstr_size);
910 /* Fetch one byte more because string_quote may look one byte ahead. */
911 size = max_strlen + 1;
915 if (style & QUOTE_0_TERMINATED)
916 rc = umovestr(tcp, addr, size, str);
918 rc = umoven(tcp, addr, size, str);
925 if (size > max_strlen)
930 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
931 * or we were requested to print more than -s NUM chars)...
933 ellipsis = string_quote(str, outstr, size, style)
935 && ((style & QUOTE_0_TERMINATED)
936 || len > max_strlen);
944 dumpiov_upto(struct tcb *const tcp, const int len, const kernel_ulong_t addr,
945 kernel_ulong_t data_size)
947 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
949 struct { uint32_t base; uint32_t len; } *iov32;
950 struct { uint64_t base; uint64_t len; } *iov64;
952 #define iov iovu.iov64
954 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
955 #define iov_iov_base(i) \
956 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
957 #define iov_iov_len(i) \
958 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
961 #define sizeof_iov sizeof(*iov)
962 #define iov_iov_base(i) ptr_to_kulong(iov[i].iov_base)
963 #define iov_iov_len(i) iov[i].iov_len
968 size = sizeof_iov * len;
969 /* Assuming no sane program has millions of iovs */
970 if ((unsigned)len > 1024*1024 /* insane or negative size? */
971 || (iov = malloc(size)) == NULL) {
972 error_msg("Out of memory");
975 if (umoven(tcp, addr, size, iov) >= 0) {
976 for (i = 0; i < len; i++) {
977 kernel_ulong_t iov_len = iov_iov_len(i);
978 if (iov_len > data_size)
982 data_size -= iov_len;
983 /* include the buffer number to make it easy to
984 * match up the trace with the source */
985 tprintf(" * %" PRI_klu " bytes in buffer %d\n", iov_len, i);
986 dumpstr(tcp, iov_iov_base(i), iov_len);
997 dumpstr(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
999 static int strsize = -1;
1000 static unsigned char *str;
1005 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
1006 "1234567890123456") + /*in case I'm off by few:*/ 4)
1007 /*align to 8 to make memset easier:*/ + 7) & -8
1009 const unsigned char *src;
1012 memset(outbuf, ' ', sizeof(outbuf));
1014 if (strsize < len + 16) {
1016 str = malloc(len + 16);
1019 error_msg("Out of memory");
1025 if (umoven(tcp, addr, len, str) < 0)
1028 /* Space-pad to 16 bytes */
1040 *dst++ = "0123456789abcdef"[*src >> 4];
1041 *dst++ = "0123456789abcdef"[*src & 0xf];
1047 dst++; /* space is there by memset */
1050 dst++; /* space is there by memset */
1057 if (*src >= ' ' && *src < 0x7f)
1062 } while (++i & 0xf);
1064 tprintf(" | %05x %s |\n", i - 16, outbuf);
1068 static bool process_vm_readv_not_supported = 0;
1069 #ifndef HAVE_PROCESS_VM_READV
1071 * Need to do this since process_vm_readv() is not yet available in libc.
1072 * When libc is be updated, only "static bool process_vm_readv_not_supported"
1073 * line should remain.
1075 /* Have to avoid duplicating with the C library headers. */
1076 static ssize_t strace_process_vm_readv(pid_t pid,
1077 const struct iovec *lvec,
1078 unsigned long liovcnt,
1079 const struct iovec *rvec,
1080 unsigned long riovcnt,
1081 unsigned long flags)
1083 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
1085 # define process_vm_readv strace_process_vm_readv
1086 #endif /* !HAVE_PROCESS_VM_READV */
1089 vm_read_mem(const pid_t pid, void *const laddr,
1090 const kernel_ulong_t raddr, const size_t len)
1092 const unsigned long truncated_raddr = raddr;
1094 if (raddr != (kernel_ulong_t) truncated_raddr) {
1099 const struct iovec local = {
1103 const struct iovec remote = {
1104 .iov_base = (void *) truncated_raddr,
1108 return process_vm_readv(pid, &local, 1, &remote, 1, 0);
1112 * move `len' bytes of data from process `pid'
1113 * at address `addr' to our space at `our_addr'
1116 umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
1117 void *const our_addr)
1119 char *laddr = our_addr;
1121 unsigned int n, m, nread;
1124 char x[sizeof(long)];
1127 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1128 if (current_wordsize < sizeof(addr)
1129 && (addr & (~ (kernel_ulong_t) -1U))) {
1134 if (!process_vm_readv_not_supported) {
1135 int r = vm_read_mem(pid, laddr, addr, len);
1136 if ((unsigned int) r == len)
1139 error_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
1140 (unsigned int) r, len, addr);
1145 process_vm_readv_not_supported = 1;
1148 /* operation not permitted, try PTRACE_PEEKDATA */
1151 /* the process is gone */
1153 case EFAULT: case EIO:
1154 /* address space is inaccessible */
1157 /* all the rest is strange and should be reported */
1158 perror_msg("process_vm_readv");
1164 if (addr & (sizeof(long) - 1)) {
1165 /* addr not a multiple of sizeof(long) */
1166 n = addr & (sizeof(long) - 1); /* residue */
1167 addr &= -sizeof(long); /* aligned address */
1169 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1173 case ESRCH: case EINVAL:
1174 /* these could be seen if the process is gone */
1176 case EFAULT: case EIO: case EPERM:
1177 /* address space is inaccessible */
1180 /* all the rest is strange and should be reported */
1181 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1185 m = MIN(sizeof(long) - n, len);
1186 memcpy(laddr, &u.x[n], m);
1187 addr += sizeof(long);
1194 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1198 case ESRCH: case EINVAL:
1199 /* these could be seen if the process is gone */
1201 case EFAULT: case EIO: case EPERM:
1202 /* address space is inaccessible */
1204 perror_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
1205 nread, nread + len, addr - nread);
1209 /* all the rest is strange and should be reported */
1210 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1214 m = MIN(sizeof(long), len);
1215 memcpy(laddr, u.x, m);
1216 addr += sizeof(long);
1226 umoven_or_printaddr(struct tcb *const tcp, const kernel_ulong_t addr,
1227 const unsigned int len, void *const our_addr)
1229 if (!addr || !verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
1230 umoven(tcp, addr, len, our_addr) < 0) {
1238 umoven_or_printaddr_ignore_syserror(struct tcb *const tcp,
1239 const kernel_ulong_t addr,
1240 const unsigned int len,
1241 void *const our_addr)
1243 if (!addr || !verbose(tcp) || umoven(tcp, addr, len, our_addr) < 0) {
1251 * Like `umove' but make the additional effort of looking
1252 * for a terminating zero byte.
1254 * Returns < 0 on error, > 0 if NUL was seen,
1255 * (TODO if useful: return count of bytes including NUL),
1256 * else 0 if len bytes were read but no NUL byte seen.
1258 * Note: there is no guarantee we won't overwrite some bytes
1259 * in laddr[] _after_ terminating NUL (but, of course,
1260 * we never write past laddr[len-1]).
1263 umovestr(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len, char *laddr)
1265 const unsigned long x01010101 = (unsigned long) 0x0101010101010101ULL;
1266 const unsigned long x80808080 = (unsigned long) 0x8080808080808080ULL;
1269 unsigned int n, m, nread;
1272 char x[sizeof(long)];
1275 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1276 if (current_wordsize < sizeof(addr)
1277 && (addr & (~ (kernel_ulong_t) -1U))) {
1283 if (!process_vm_readv_not_supported) {
1284 const size_t page_size = get_pagesize();
1285 const size_t page_mask = page_size - 1;
1288 unsigned int chunk_len;
1289 unsigned int end_in_page;
1292 * Don't cross pages, otherwise we can get EFAULT
1293 * and fail to notice that terminating NUL lies
1294 * in the existing (first) page.
1296 chunk_len = len > page_size ? page_size : len;
1297 end_in_page = (addr + chunk_len) & page_mask;
1298 if (chunk_len > end_in_page) /* crosses to the next page */
1299 chunk_len -= end_in_page;
1301 int r = vm_read_mem(pid, laddr, addr, chunk_len);
1303 if (memchr(laddr, '\0', r))
1313 process_vm_readv_not_supported = 1;
1314 goto vm_readv_didnt_work;
1316 /* the process is gone */
1319 /* operation not permitted, try PTRACE_PEEKDATA */
1321 goto vm_readv_didnt_work;
1323 case EFAULT: case EIO:
1324 /* address space is inaccessible */
1326 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
1327 nread, nread + len, addr - nread);
1331 /* all the rest is strange and should be reported */
1332 perror_msg("process_vm_readv");
1338 vm_readv_didnt_work:
1340 if (addr & (sizeof(long) - 1)) {
1341 /* addr not a multiple of sizeof(long) */
1342 n = addr & (sizeof(long) - 1); /* residue */
1343 addr &= -sizeof(long); /* aligned address */
1345 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1349 case ESRCH: case EINVAL:
1350 /* these could be seen if the process is gone */
1352 case EFAULT: case EIO: case EPERM:
1353 /* address space is inaccessible */
1356 /* all the rest is strange and should be reported */
1357 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1361 m = MIN(sizeof(long) - n, len);
1362 memcpy(laddr, &u.x[n], m);
1363 while (n & (sizeof(long) - 1))
1364 if (u.x[n++] == '\0')
1366 addr += sizeof(long);
1374 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1378 case ESRCH: case EINVAL:
1379 /* these could be seen if the process is gone */
1381 case EFAULT: case EIO: case EPERM:
1382 /* address space is inaccessible */
1384 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
1385 nread, nread + len, addr - nread);
1389 /* all the rest is strange and should be reported */
1390 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1394 m = MIN(sizeof(long), len);
1395 memcpy(laddr, u.x, m);
1396 /* "If a NUL char exists in this word" */
1397 if ((u.val - x01010101) & ~u.val & x80808080)
1399 addr += sizeof(long);
1408 * Iteratively fetch and print up to nmemb elements of elem_size size
1409 * from the array that starts at tracee's address start_addr.
1411 * Array elements are being fetched to the address specified by elem_buf.
1413 * The fetcher callback function specified by umoven_func should follow
1414 * the same semantics as umoven_or_printaddr function.
1416 * The printer callback function specified by print_func is expected
1417 * to print something; if it returns false, no more iterations will be made.
1419 * The pointer specified by opaque_data is passed to each invocation
1420 * of print_func callback function.
1422 * This function prints:
1423 * - "NULL", if start_addr is NULL;
1424 * - "[]", if nmemb is 0;
1425 * - start_addr, if nmemb * elem_size overflows or wraps around;
1426 * - nothing, if the first element cannot be fetched
1427 * (if umoven_func returns non-zero), but it is assumed that
1428 * umoven_func has printed the address it failed to fetch data from;
1429 * - elements of the array, delimited by ", ", with the array itself
1430 * enclosed with [] brackets.
1432 * If abbrev(tcp) is true, then
1433 * - the maximum number of elements printed equals to max_strlen;
1434 * - "..." is printed instead of max_strlen+1 element
1435 * and no more iterations will be made.
1437 * This function returns true only if
1438 * - umoven_func has been called at least once AND
1439 * - umoven_func has not returned false.
1442 print_array(struct tcb *const tcp,
1443 const kernel_ulong_t start_addr,
1445 void *const elem_buf,
1446 const size_t elem_size,
1447 int (*const umoven_func)(struct tcb *,
1451 bool (*const print_func)(struct tcb *,
1455 void *const opaque_data)
1467 const size_t size = nmemb * elem_size;
1468 const kernel_ulong_t end_addr = start_addr + size;
1470 if (end_addr <= start_addr || size / elem_size != nmemb) {
1471 printaddr(start_addr);
1475 const kernel_ulong_t abbrev_end =
1476 (abbrev(tcp) && max_strlen < nmemb) ?
1477 start_addr + elem_size * max_strlen : end_addr;
1480 for (cur = start_addr; cur < end_addr; cur += elem_size) {
1481 if (cur != start_addr)
1484 if (umoven_func(tcp, cur, elem_size, elem_buf))
1487 if (cur == start_addr)
1490 if (cur >= abbrev_end) {
1496 if (!print_func(tcp, elem_buf, elem_size, opaque_data)) {
1501 if (cur != start_addr)
1504 return cur >= end_addr;
1508 printargs(struct tcb *tcp)
1510 const int n = tcp->s_ent->nargs;
1512 for (i = 0; i < n; ++i)
1513 tprintf("%s%#" PRI_klx, i ? ", " : "", tcp->u_arg[i]);
1514 return RVAL_DECODED;
1518 printargs_u(struct tcb *tcp)
1520 const int n = tcp->s_ent->nargs;
1522 for (i = 0; i < n; ++i)
1523 tprintf("%s%u", i ? ", " : "",
1524 (unsigned int) tcp->u_arg[i]);
1525 return RVAL_DECODED;
1529 printargs_d(struct tcb *tcp)
1531 const int n = tcp->s_ent->nargs;
1533 for (i = 0; i < n; ++i)
1534 tprintf("%s%d", i ? ", " : "",
1535 (int) tcp->u_arg[i]);
1536 return RVAL_DECODED;
1539 #if defined _LARGEFILE64_SOURCE && defined HAVE_OPEN64
1540 # define open_file open64
1542 # define open_file open
1546 read_int_from_file(const char *const fname, int *const pvalue)
1548 const int fd = open_file(fname, O_RDONLY);
1553 char buf[sizeof(lval) * 3];
1554 int n = read(fd, buf, sizeof(buf) - 1);
1555 int saved_errno = errno;
1559 errno = saved_errno;
1566 lval = strtol(buf, &endptr, 10);
1567 if (!endptr || (*endptr && '\n' != *endptr)
1568 #if INT_MAX < LONG_MAX
1569 || lval > INT_MAX || lval < INT_MIN
1571 || ERANGE == errno) {
1577 *pvalue = (int) lval;