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;
144 xlookup(const struct xlat *xlat, const uint64_t val)
146 for (; xlat->str != NULL; xlat++)
147 if (xlat->val == val)
153 xlat_bsearch_compare(const void *a, const void *b)
155 const uint64_t val1 = *(const uint64_t *) a;
156 const uint64_t val2 = ((const struct xlat *) b)->val;
157 return (val1 > val2) ? 1 : (val1 < val2) ? -1 : 0;
161 xlat_search(const struct xlat *xlat, const size_t nmemb, const uint64_t val)
163 const struct xlat *e =
164 bsearch((const void*) &val,
165 xlat, nmemb, sizeof(*xlat), xlat_bsearch_compare);
167 return e ? e->str : NULL;
170 #if !defined HAVE_STPCPY
172 stpcpy(char *dst, const char *src)
174 while ((*dst = *src++) != '\0')
180 /* Find a next bit which is set.
181 * Starts testing at cur_bit.
182 * Returns -1 if no more bits are set.
184 * We never touch bytes we don't need to.
185 * On big-endian, array is assumed to consist of
186 * current_wordsize wide words: for example, is current_wordsize is 4,
187 * the bytes are walked in 3,2,1,0, 7,6,5,4, 11,10,9,8 ... sequence.
188 * On little-endian machines, word size is immaterial.
191 next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits)
193 const unsigned endian = 1;
194 int little_endian = * (char *) (void *) &endian;
196 const uint8_t *array = bit_array;
197 unsigned pos = cur_bit / 8;
198 unsigned pos_xor_mask = little_endian ? 0 : current_wordsize-1;
204 if (cur_bit >= size_bits)
206 cur_byte = array[pos ^ pos_xor_mask];
208 cur_bit = (cur_bit + 8) & (-8);
212 bitmask = 1 << (cur_bit & 7);
214 if (cur_byte & bitmask)
217 if (cur_bit >= size_bits)
220 /* This check *can't be* optimized out: */
229 * Print entry in struct xlat table, if there.
231 * @param val Value to search a literal representation for.
232 * @param dflt String (abbreviated in comment syntax) which should be emitted
233 * if no appropriate xlat value has been found.
234 * @param xlat (And the following arguments) Pointers to arrays of xlat values.
235 * The last argument should be NULL.
236 * @return 1 if appropriate xlat value has been found, 0 otherwise.
239 printxvals(const uint64_t val, const char *dflt, const struct xlat *xlat, ...)
243 va_start(args, xlat);
244 for (; xlat; xlat = va_arg(args, const struct xlat *)) {
245 const char *str = xlookup(xlat, val);
253 /* No hits -- print raw # instead. */
254 tprintf("%#" PRIx64, val);
255 tprints_comment(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);
288 tprints_comment(dflt);
294 * Fetch 64bit argument at position arg_no and
295 * return the index of the next argument.
298 getllval(struct tcb *tcp, unsigned long long *val, int arg_no)
300 #if SIZEOF_KERNEL_LONG_T > 4
301 # ifndef current_klongsize
302 if (current_klongsize < SIZEOF_KERNEL_LONG_T) {
303 # if defined(AARCH64) || defined(POWERPC64)
304 /* Align arg_no to the next even number. */
305 arg_no = (arg_no + 1) & 0xe;
306 # endif /* AARCH64 || POWERPC64 */
307 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
310 # endif /* !current_klongsize */
312 *val = tcp->u_arg[arg_no];
315 #else /* SIZEOF_KERNEL_LONG_T == 4 */
316 # if defined __ARM_EABI__ || \
317 defined LINUX_MIPSO32 || \
320 /* Align arg_no to the next even number. */
321 arg_no = (arg_no + 1) & 0xe;
324 * The SH4 ABI does allow long longs in odd-numbered registers, but
325 * does not allow them to be split between registers and memory - and
326 * there are only four argument registers for normal functions. As a
327 * result, pread, for example, takes an extra padding argument before
328 * the offset. This was changed late in the 2.4 series (around 2.4.20).
332 # endif /* __ARM_EABI__ || LINUX_MIPSO32 || POWERPC || XTENSA || SH */
333 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
341 * Print 64bit argument at position arg_no and
342 * return the index of the next argument.
345 printllval(struct tcb *tcp, const char *format, int arg_no)
347 unsigned long long val = 0;
349 arg_no = getllval(tcp, &val, arg_no);
350 tprintf(format, val);
355 * Interpret `xlat' as an array of flags
356 * print the entries whose bits are on in `flags'
359 addflags(const struct xlat *xlat, uint64_t flags)
361 for (; xlat->str; xlat++) {
362 if (xlat->val && (flags & xlat->val) == xlat->val) {
363 tprintf("|%s", xlat->str);
368 tprintf("|%#" PRIx64, flags);
373 * Interpret `xlat' as an array of flags.
374 * Print to static string the entries whose bits are on in `flags'
375 * Return static string.
378 sprintflags(const char *prefix, const struct xlat *xlat, uint64_t flags)
380 static char outstr[1024];
384 outptr = stpcpy(outstr, prefix);
386 if (flags == 0 && xlat->val == 0 && xlat->str) {
387 strcpy(outptr, xlat->str);
391 for (; xlat->str; xlat++) {
392 if (xlat->val && (flags & xlat->val) == xlat->val) {
395 outptr = stpcpy(outptr, xlat->str);
405 outptr += sprintf(outptr, "%#" PRIx64, flags);
412 printflags64(const struct xlat *xlat, uint64_t flags, const char *dflt)
417 if (flags == 0 && xlat->val == 0 && xlat->str) {
423 for (n = 0; xlat->str; xlat++) {
424 if (xlat->val && (flags & xlat->val) == xlat->val) {
425 tprintf("%s%s", sep, xlat->str);
434 tprintf("%s%#" PRIx64, sep, flags);
439 tprintf("%#" PRIx64, flags);
440 tprints_comment(dflt);
451 printaddr(const kernel_ulong_t addr)
456 tprintf("%#" PRI_klx, addr);
459 #define DEF_PRINTNUM(name, type) \
461 printnum_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
462 const char *const fmt) \
465 if (umove_or_printaddr(tcp, addr, &num)) \
473 #define DEF_PRINTNUM_ADDR(name, type) \
475 printnum_addr_ ## name(struct tcb *tcp, const kernel_ulong_t addr) \
478 if (umove_or_printaddr(tcp, addr, &num)) \
486 #define DEF_PRINTPAIR(name, type) \
488 printpair_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
489 const char *const fmt) \
492 if (umove_or_printaddr(tcp, addr, &pair)) \
495 tprintf(fmt, pair[0]); \
497 tprintf(fmt, pair[1]); \
502 DEF_PRINTNUM(int, int)
503 DEF_PRINTNUM_ADDR(int, unsigned int)
504 DEF_PRINTPAIR(int, int)
505 DEF_PRINTNUM(short, short)
506 DEF_PRINTNUM(int64, uint64_t)
507 DEF_PRINTNUM_ADDR(int64, uint64_t)
508 DEF_PRINTPAIR(int64, uint64_t)
510 #ifndef current_wordsize
512 printnum_long_int(struct tcb *const tcp, const kernel_ulong_t addr,
513 const char *const fmt_long, const char *const fmt_int)
515 if (current_wordsize > sizeof(int)) {
516 return printnum_int64(tcp, addr, fmt_long);
518 return printnum_int(tcp, addr, fmt_int);
523 printnum_addr_long_int(struct tcb *tcp, const kernel_ulong_t addr)
525 if (current_wordsize > sizeof(int)) {
526 return printnum_addr_int64(tcp, addr);
528 return printnum_addr_int(tcp, addr);
531 #endif /* !current_wordsize */
533 #ifndef current_klongsize
535 printnum_addr_klong_int(struct tcb *tcp, const kernel_ulong_t addr)
537 if (current_klongsize > sizeof(int)) {
538 return printnum_addr_int64(tcp, addr);
540 return printnum_addr_int(tcp, addr);
543 #endif /* !current_klongsize */
546 * Prints time to a (static internal) buffer and returns pointer to it.
548 * @param sec Seconds since epoch.
549 * @param part_sec Amount of second parts since the start of a second.
550 * @param max_part_sec Maximum value of a valid part_sec.
551 * @param width 1 + floor(log10(max_part_sec)).
554 sprinttime_ex(const long long sec, const unsigned long long part_sec,
555 const unsigned int max_part_sec, const int width)
557 static char buf[sizeof(int) * 3 * 6 + sizeof(part_sec) * 3
560 if ((sec == 0 && part_sec == 0) || part_sec > max_part_sec)
563 time_t t = (time_t) sec;
564 struct tm *tmp = (sec == t) ? localtime(&t) : NULL;
568 size_t pos = strftime(buf, sizeof(buf), "%FT%T", tmp);
573 int ret = snprintf(buf + pos, sizeof(buf) - pos, ".%0*llu",
576 if (ret < 0 || (size_t) ret >= sizeof(buf) - pos)
582 return strftime(buf + pos, sizeof(buf) - pos, "%z", tmp) ? buf : NULL;
586 sprinttime(long long sec)
588 return sprinttime_ex(sec, 0, 0, 0);
592 sprinttime_usec(long long sec, unsigned long long usec)
594 return sprinttime_ex(sec, usec, 999999, 6);
598 sprinttime_nsec(long long sec, unsigned long long nsec)
600 return sprinttime_ex(sec, nsec, 999999999, 9);
604 getfdproto(struct tcb *tcp, int fd)
606 #ifdef HAVE_SYS_XATTR_H
607 size_t bufsize = 256;
610 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
613 return SOCK_PROTO_UNKNOWN;
615 sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
616 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
618 return SOCK_PROTO_UNKNOWN;
621 * This is a protection for the case when the kernel
622 * side does not append a null byte to the buffer.
626 return get_proto_by_name(buf);
629 return SOCK_PROTO_UNKNOWN;
634 getfdinode(struct tcb *tcp, int fd)
636 char path[PATH_MAX + 1];
638 if (getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
639 const char *str = STR_STRIP_PREFIX(path, "socket:[");
642 const size_t str_len = strlen(str);
643 if (str_len && str[str_len - 1] == ']')
644 return strtoul(str, NULL, 10);
652 printfd(struct tcb *tcp, int fd)
654 char path[PATH_MAX + 1];
655 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
661 if (show_fd_path <= 1
662 || (str = STR_STRIP_PREFIX(path, "socket:[")) == path
663 || !(len = strlen(str))
664 || str[len - 1] != ']'
665 || !(inode = strtoul(str, NULL, 10))
666 || !print_sockaddr_by_inode(tcp, fd, inode)) {
667 print_quoted_string(path, strlen(path),
668 QUOTE_OMIT_LEADING_TRAILING_QUOTES);
676 * Quote string `instr' of length `size'
677 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
679 * If QUOTE_0_TERMINATED `style' flag is set,
680 * treat `instr' as a NUL-terminated string,
681 * checking up to (`size' + 1) bytes of `instr'.
683 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
684 * do not add leading and trailing quoting symbols.
686 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
687 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
690 string_quote(const char *instr, char *outstr, const unsigned int size,
691 const unsigned int style)
693 const unsigned char *ustr = (const unsigned char *) instr;
698 if (style & QUOTE_0_TERMINATED)
701 eol = 0x100; /* this can never match a char */
704 if ((xflag > 1) || (style & QUOTE_FORCE_HEX)) {
707 /* Check for presence of symbol which require
708 to hex-quote the whole string. */
709 for (i = 0; i < size; ++i) {
711 /* Check for NUL-terminated string. */
715 /* Force hex unless c is printable or whitespace */
720 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
721 * They happen to have ASCII codes 9,10,11,12,13.
723 if (c < ' ' && (unsigned)(c - 9) >= 5) {
730 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
734 /* Hex-quote the whole string. */
735 for (i = 0; i < size; ++i) {
737 /* Check for NUL-terminated string. */
742 *s++ = "0123456789abcdef"[c >> 4];
743 *s++ = "0123456789abcdef"[c & 0xf];
746 for (i = 0; i < size; ++i) {
748 /* Check for NUL-terminated string. */
751 if ((i == (size - 1)) &&
752 (style & QUOTE_OMIT_TRAILING_0) && (c == '\0'))
755 case '\"': case '\\':
780 if (c >= ' ' && c <= 0x7e)
786 && ustr[i + 1] >= '0'
787 && ustr[i + 1] <= '9'
790 *s++ = '0' + (c >> 6);
791 *s++ = '0' + ((c >> 3) & 0x7);
796 *s++ = '0' + (c >> 6);
797 *s++ = '0' + ((c >> 3) & 0x7);
800 *s++ = '0' + (c & 0x7);
807 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
811 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
812 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
813 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
814 * but next char is NUL.
822 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
825 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
829 #ifndef ALLOCA_CUTOFF
830 # define ALLOCA_CUTOFF 4032
832 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
835 * Quote string `str' of length `size' and print the result.
837 * If QUOTE_0_TERMINATED `style' flag is set,
838 * treat `str' as a NUL-terminated string and
839 * quote at most (`size' - 1) bytes.
841 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
842 * do not add leading and trailing quoting symbols.
844 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
845 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
848 print_quoted_string(const char *str, unsigned int size,
849 const unsigned int style)
853 unsigned int alloc_size;
856 if (size && style & QUOTE_0_TERMINATED)
859 alloc_size = 4 * size;
860 if (alloc_size / 4 != size) {
861 error_msg("Out of memory");
865 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2);
867 if (use_alloca(alloc_size)) {
868 outstr = alloca(alloc_size);
871 outstr = buf = malloc(alloc_size);
873 error_msg("Out of memory");
879 rc = string_quote(str, outstr, size, style);
887 * Print path string specified by address `addr' and length `n'.
888 * If path length exceeds `n', append `...' to the output.
891 printpathn(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int n)
893 char path[PATH_MAX + 1];
901 /* Cap path length to the path buffer size */
902 if (n > sizeof path - 1)
905 /* Fetch one byte more to find out whether path length > n. */
906 nul_seen = umovestr(tcp, addr, n + 1, path);
911 print_quoted_string(path, n, QUOTE_0_TERMINATED);
918 printpath(struct tcb *const tcp, const kernel_ulong_t addr)
920 /* Size must correspond to char path[] size in printpathn */
921 printpathn(tcp, addr, PATH_MAX);
925 * Print string specified by address `addr' and length `len'.
926 * If `user_style' has QUOTE_0_TERMINATED bit set, treat the string
927 * as a NUL-terminated string.
928 * Pass `user_style' on to `string_quote'.
929 * Append `...' to the output if either the string length exceeds `max_strlen',
930 * or QUOTE_0_TERMINATED bit is set and the string length exceeds `len'.
933 printstr_ex(struct tcb *const tcp, const kernel_ulong_t addr,
934 const kernel_ulong_t len, const unsigned int user_style)
936 static char *str = NULL;
939 unsigned int style = user_style;
947 /* Allocate static buffers if they are not allocated yet. */
949 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
951 if (outstr_size / 4 != max_strlen)
953 str = xmalloc(max_strlen + 1);
954 outstr = xmalloc(outstr_size);
957 /* Fetch one byte more because string_quote may look one byte ahead. */
958 size = max_strlen + 1;
962 if (style & QUOTE_0_TERMINATED)
963 rc = umovestr(tcp, addr, size, str);
965 rc = umoven(tcp, addr, size, str);
972 if (size > max_strlen)
977 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
978 * or we were requested to print more than -s NUM chars)...
980 ellipsis = string_quote(str, outstr, size, style)
982 && ((style & QUOTE_0_TERMINATED)
983 || len > max_strlen);
991 dumpiov_upto(struct tcb *const tcp, const int len, const kernel_ulong_t addr,
992 kernel_ulong_t data_size)
994 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
996 struct { uint32_t base; uint32_t len; } *iov32;
997 struct { uint64_t base; uint64_t len; } *iov64;
999 #define iov iovu.iov64
1000 #define sizeof_iov \
1001 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
1002 #define iov_iov_base(i) \
1003 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
1004 #define iov_iov_len(i) \
1005 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
1008 #define sizeof_iov sizeof(*iov)
1009 #define iov_iov_base(i) ptr_to_kulong(iov[i].iov_base)
1010 #define iov_iov_len(i) iov[i].iov_len
1015 size = sizeof_iov * len;
1016 /* Assuming no sane program has millions of iovs */
1017 if ((unsigned)len > 1024*1024 /* insane or negative size? */
1018 || (iov = malloc(size)) == NULL) {
1019 error_msg("Out of memory");
1022 if (umoven(tcp, addr, size, iov) >= 0) {
1023 for (i = 0; i < len; i++) {
1024 kernel_ulong_t iov_len = iov_iov_len(i);
1025 if (iov_len > data_size)
1026 iov_len = data_size;
1029 data_size -= iov_len;
1030 /* include the buffer number to make it easy to
1031 * match up the trace with the source */
1032 tprintf(" * %" PRI_klu " bytes in buffer %d\n", iov_len, i);
1033 dumpstr(tcp, iov_iov_base(i), iov_len);
1044 dumpstr(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
1046 static int strsize = -1;
1047 static unsigned char *str;
1052 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
1053 "1234567890123456") + /*in case I'm off by few:*/ 4)
1054 /*align to 8 to make memset easier:*/ + 7) & -8
1056 const unsigned char *src;
1059 memset(outbuf, ' ', sizeof(outbuf));
1061 if (strsize < len + 16) {
1063 str = malloc(len + 16);
1066 error_msg("Out of memory");
1072 if (umoven(tcp, addr, len, str) < 0)
1075 /* Space-pad to 16 bytes */
1087 *dst++ = "0123456789abcdef"[*src >> 4];
1088 *dst++ = "0123456789abcdef"[*src & 0xf];
1094 dst++; /* space is there by memset */
1097 dst++; /* space is there by memset */
1104 if (*src >= ' ' && *src < 0x7f)
1109 } while (++i & 0xf);
1111 tprintf(" | %05x %s |\n", i - 16, outbuf);
1115 static bool process_vm_readv_not_supported = 0;
1116 #ifndef HAVE_PROCESS_VM_READV
1118 * Need to do this since process_vm_readv() is not yet available in libc.
1119 * When libc is be updated, only "static bool process_vm_readv_not_supported"
1120 * line should remain.
1122 /* Have to avoid duplicating with the C library headers. */
1123 static ssize_t strace_process_vm_readv(pid_t pid,
1124 const struct iovec *lvec,
1125 unsigned long liovcnt,
1126 const struct iovec *rvec,
1127 unsigned long riovcnt,
1128 unsigned long flags)
1130 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
1132 # define process_vm_readv strace_process_vm_readv
1133 #endif /* !HAVE_PROCESS_VM_READV */
1136 vm_read_mem(const pid_t pid, void *const laddr,
1137 const kernel_ulong_t raddr, const size_t len)
1139 const unsigned long truncated_raddr = raddr;
1141 if (raddr != (kernel_ulong_t) truncated_raddr) {
1146 const struct iovec local = {
1150 const struct iovec remote = {
1151 .iov_base = (void *) truncated_raddr,
1155 return process_vm_readv(pid, &local, 1, &remote, 1, 0);
1159 * move `len' bytes of data from process `pid'
1160 * at address `addr' to our space at `our_addr'
1163 umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
1164 void *const our_addr)
1166 char *laddr = our_addr;
1168 unsigned int n, m, nread;
1171 char x[sizeof(long)];
1174 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1175 if (current_wordsize < sizeof(addr)
1176 && (addr & (~ (kernel_ulong_t) -1U))) {
1181 if (!process_vm_readv_not_supported) {
1182 int r = vm_read_mem(pid, laddr, addr, len);
1183 if ((unsigned int) r == len)
1186 error_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
1187 (unsigned int) r, len, addr);
1192 process_vm_readv_not_supported = 1;
1195 /* operation not permitted, try PTRACE_PEEKDATA */
1198 /* the process is gone */
1200 case EFAULT: case EIO:
1201 /* address space is inaccessible */
1204 /* all the rest is strange and should be reported */
1205 perror_msg("process_vm_readv");
1211 if (addr & (sizeof(long) - 1)) {
1212 /* addr not a multiple of sizeof(long) */
1213 n = addr & (sizeof(long) - 1); /* residue */
1214 addr &= -sizeof(long); /* aligned address */
1216 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1220 case ESRCH: case EINVAL:
1221 /* these could be seen if the process is gone */
1223 case EFAULT: case EIO: case EPERM:
1224 /* address space is inaccessible */
1227 /* all the rest is strange and should be reported */
1228 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1232 m = MIN(sizeof(long) - n, len);
1233 memcpy(laddr, &u.x[n], m);
1234 addr += sizeof(long);
1241 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1245 case ESRCH: case EINVAL:
1246 /* these could be seen if the process is gone */
1248 case EFAULT: case EIO: case EPERM:
1249 /* address space is inaccessible */
1251 perror_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
1252 nread, nread + len, addr - nread);
1256 /* all the rest is strange and should be reported */
1257 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1261 m = MIN(sizeof(long), len);
1262 memcpy(laddr, u.x, m);
1263 addr += sizeof(long);
1273 umoven_or_printaddr(struct tcb *const tcp, const kernel_ulong_t addr,
1274 const unsigned int len, void *const our_addr)
1276 if (!addr || !verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
1277 umoven(tcp, addr, len, our_addr) < 0) {
1285 umoven_or_printaddr_ignore_syserror(struct tcb *const tcp,
1286 const kernel_ulong_t addr,
1287 const unsigned int len,
1288 void *const our_addr)
1290 if (!addr || !verbose(tcp) || umoven(tcp, addr, len, our_addr) < 0) {
1298 * Like `umove' but make the additional effort of looking
1299 * for a terminating zero byte.
1301 * Returns < 0 on error, > 0 if NUL was seen,
1302 * (TODO if useful: return count of bytes including NUL),
1303 * else 0 if len bytes were read but no NUL byte seen.
1305 * Note: there is no guarantee we won't overwrite some bytes
1306 * in laddr[] _after_ terminating NUL (but, of course,
1307 * we never write past laddr[len-1]).
1310 umovestr(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len, char *laddr)
1312 const unsigned long x01010101 = (unsigned long) 0x0101010101010101ULL;
1313 const unsigned long x80808080 = (unsigned long) 0x8080808080808080ULL;
1316 unsigned int n, m, nread;
1319 char x[sizeof(long)];
1322 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1323 if (current_wordsize < sizeof(addr)
1324 && (addr & (~ (kernel_ulong_t) -1U))) {
1330 if (!process_vm_readv_not_supported) {
1331 const size_t page_size = get_pagesize();
1332 const size_t page_mask = page_size - 1;
1335 unsigned int chunk_len;
1336 unsigned int end_in_page;
1339 * Don't cross pages, otherwise we can get EFAULT
1340 * and fail to notice that terminating NUL lies
1341 * in the existing (first) page.
1343 chunk_len = len > page_size ? page_size : len;
1344 end_in_page = (addr + chunk_len) & page_mask;
1345 if (chunk_len > end_in_page) /* crosses to the next page */
1346 chunk_len -= end_in_page;
1348 int r = vm_read_mem(pid, laddr, addr, chunk_len);
1350 if (memchr(laddr, '\0', r))
1360 process_vm_readv_not_supported = 1;
1361 goto vm_readv_didnt_work;
1363 /* the process is gone */
1366 /* operation not permitted, try PTRACE_PEEKDATA */
1368 goto vm_readv_didnt_work;
1370 case EFAULT: case EIO:
1371 /* address space is inaccessible */
1373 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
1374 nread, nread + len, addr - nread);
1378 /* all the rest is strange and should be reported */
1379 perror_msg("process_vm_readv");
1385 vm_readv_didnt_work:
1387 if (addr & (sizeof(long) - 1)) {
1388 /* addr not a multiple of sizeof(long) */
1389 n = addr & (sizeof(long) - 1); /* residue */
1390 addr &= -sizeof(long); /* aligned address */
1392 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1396 case ESRCH: case EINVAL:
1397 /* these could be seen if the process is gone */
1399 case EFAULT: case EIO: case EPERM:
1400 /* address space is inaccessible */
1403 /* all the rest is strange and should be reported */
1404 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1408 m = MIN(sizeof(long) - n, len);
1409 memcpy(laddr, &u.x[n], m);
1410 while (n & (sizeof(long) - 1))
1411 if (u.x[n++] == '\0')
1413 addr += sizeof(long);
1421 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1425 case ESRCH: case EINVAL:
1426 /* these could be seen if the process is gone */
1428 case EFAULT: case EIO: case EPERM:
1429 /* address space is inaccessible */
1431 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
1432 nread, nread + len, addr - nread);
1436 /* all the rest is strange and should be reported */
1437 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1441 m = MIN(sizeof(long), len);
1442 memcpy(laddr, u.x, m);
1443 /* "If a NUL char exists in this word" */
1444 if ((u.val - x01010101) & ~u.val & x80808080)
1446 addr += sizeof(long);
1455 * Iteratively fetch and print up to nmemb elements of elem_size size
1456 * from the array that starts at tracee's address start_addr.
1458 * Array elements are being fetched to the address specified by elem_buf.
1460 * The fetcher callback function specified by umoven_func should follow
1461 * the same semantics as umoven_or_printaddr function.
1463 * The printer callback function specified by print_func is expected
1464 * to print something; if it returns false, no more iterations will be made.
1466 * The pointer specified by opaque_data is passed to each invocation
1467 * of print_func callback function.
1469 * This function prints:
1470 * - "NULL", if start_addr is NULL;
1471 * - "[]", if nmemb is 0;
1472 * - start_addr, if nmemb * elem_size overflows or wraps around;
1473 * - nothing, if the first element cannot be fetched
1474 * (if umoven_func returns non-zero), but it is assumed that
1475 * umoven_func has printed the address it failed to fetch data from;
1476 * - elements of the array, delimited by ", ", with the array itself
1477 * enclosed with [] brackets.
1479 * If abbrev(tcp) is true, then
1480 * - the maximum number of elements printed equals to max_strlen;
1481 * - "..." is printed instead of max_strlen+1 element
1482 * and no more iterations will be made.
1484 * This function returns true only if
1485 * - umoven_func has been called at least once AND
1486 * - umoven_func has not returned false.
1489 print_array(struct tcb *const tcp,
1490 const kernel_ulong_t start_addr,
1492 void *const elem_buf,
1493 const size_t elem_size,
1494 int (*const umoven_func)(struct tcb *,
1498 bool (*const print_func)(struct tcb *,
1502 void *const opaque_data)
1514 const size_t size = nmemb * elem_size;
1515 const kernel_ulong_t end_addr = start_addr + size;
1517 if (end_addr <= start_addr || size / elem_size != nmemb) {
1518 printaddr(start_addr);
1522 const kernel_ulong_t abbrev_end =
1523 (abbrev(tcp) && max_strlen < nmemb) ?
1524 start_addr + elem_size * max_strlen : end_addr;
1527 for (cur = start_addr; cur < end_addr; cur += elem_size) {
1528 if (cur != start_addr)
1531 if (umoven_func(tcp, cur, elem_size, elem_buf))
1534 if (cur == start_addr)
1537 if (cur >= abbrev_end) {
1543 if (!print_func(tcp, elem_buf, elem_size, opaque_data)) {
1548 if (cur != start_addr)
1551 return cur >= end_addr;
1555 printargs(struct tcb *tcp)
1557 const int n = tcp->s_ent->nargs;
1559 for (i = 0; i < n; ++i)
1560 tprintf("%s%#" PRI_klx, i ? ", " : "", tcp->u_arg[i]);
1561 return RVAL_DECODED;
1565 printargs_u(struct tcb *tcp)
1567 const int n = tcp->s_ent->nargs;
1569 for (i = 0; i < n; ++i)
1570 tprintf("%s%u", i ? ", " : "",
1571 (unsigned int) tcp->u_arg[i]);
1572 return RVAL_DECODED;
1576 printargs_d(struct tcb *tcp)
1578 const int n = tcp->s_ent->nargs;
1580 for (i = 0; i < n; ++i)
1581 tprintf("%s%d", i ? ", " : "",
1582 (int) tcp->u_arg[i]);
1583 return RVAL_DECODED;
1586 /* Print abnormal high bits of a kernel_ulong_t value. */
1588 print_abnormal_hi(const kernel_ulong_t val)
1590 if (current_klongsize > 4) {
1591 const unsigned int hi = (unsigned int) ((uint64_t) val >> 32);
1593 tprintf("%#x<<32|", hi);
1597 #if defined _LARGEFILE64_SOURCE && defined HAVE_OPEN64
1598 # define open_file open64
1600 # define open_file open
1604 read_int_from_file(const char *const fname, int *const pvalue)
1606 const int fd = open_file(fname, O_RDONLY);
1611 char buf[sizeof(lval) * 3];
1612 int n = read(fd, buf, sizeof(buf) - 1);
1613 int saved_errno = errno;
1617 errno = saved_errno;
1624 lval = strtol(buf, &endptr, 10);
1625 if (!endptr || (*endptr && '\n' != *endptr)
1626 #if INT_MAX < LONG_MAX
1627 || lval > INT_MAX || lval < INT_MIN
1629 || ERANGE == errno) {
1635 *pvalue = (int) lval;