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-2018 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.
40 #include <sys/sysmacros.h>
41 #ifdef HAVE_SYS_XATTR_H
42 # include <sys/xattr.h>
46 #include "largefile_wrappers.h"
50 ts_nz(const struct timespec *a)
52 return a->tv_sec || a->tv_nsec;
56 ts_cmp(const struct timespec *a, const struct timespec *b)
58 if (a->tv_sec < b->tv_sec
59 || (a->tv_sec == b->tv_sec && a->tv_nsec < b->tv_nsec))
61 if (a->tv_sec > b->tv_sec
62 || (a->tv_sec == b->tv_sec && a->tv_nsec > b->tv_nsec))
68 ts_float(const struct timespec *tv)
70 return tv->tv_sec + tv->tv_nsec/1000000000.0;
74 ts_add(struct timespec *tv, const struct timespec *a, const struct timespec *b)
76 tv->tv_sec = a->tv_sec + b->tv_sec;
77 tv->tv_nsec = a->tv_nsec + b->tv_nsec;
78 if (tv->tv_nsec >= 1000000000) {
80 tv->tv_nsec -= 1000000000;
85 ts_sub(struct timespec *tv, const struct timespec *a, const struct timespec *b)
87 tv->tv_sec = a->tv_sec - b->tv_sec;
88 tv->tv_nsec = a->tv_nsec - b->tv_nsec;
89 if (tv->tv_nsec < 0) {
91 tv->tv_nsec += 1000000000;
96 ts_div(struct timespec *tv, const struct timespec *a, int n)
98 long long nsec = (a->tv_sec % n * 1000000000LL + a->tv_nsec + n / 2) / n;
99 tv->tv_sec = a->tv_sec / n + nsec / 1000000000;
100 tv->tv_nsec = nsec % 1000000000;
104 ts_mul(struct timespec *tv, const struct timespec *a, int n)
106 long long nsec = a->tv_nsec * n;
107 tv->tv_sec = a->tv_sec * n + nsec / 1000000000;
108 tv->tv_nsec = nsec % 1000000000;
111 #if !defined HAVE_STPCPY
113 stpcpy(char *dst, const char *src)
115 while ((*dst = *src++) != '\0')
121 /* Find a next bit which is set.
122 * Starts testing at cur_bit.
123 * Returns -1 if no more bits are set.
125 * We never touch bytes we don't need to.
126 * On big-endian, array is assumed to consist of
127 * current_wordsize wide words: for example, is current_wordsize is 4,
128 * the bytes are walked in 3,2,1,0, 7,6,5,4, 11,10,9,8 ... sequence.
129 * On little-endian machines, word size is immaterial.
132 next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits)
134 const unsigned endian = 1;
135 int little_endian = *(char *) (void *) &endian;
137 const uint8_t *array = bit_array;
138 unsigned pos = cur_bit / 8;
139 unsigned pos_xor_mask = little_endian ? 0 : current_wordsize-1;
145 if (cur_bit >= size_bits)
147 cur_byte = array[pos ^ pos_xor_mask];
149 cur_bit = (cur_bit + 8) & (-8);
153 bitmask = 1 << (cur_bit & 7);
155 if (cur_byte & bitmask)
158 if (cur_bit >= size_bits)
161 /* This check *can't be* optimized out: */
170 * Fetch 64bit argument at position arg_no and
171 * return the index of the next argument.
174 getllval(struct tcb *tcp, unsigned long long *val, int arg_no)
176 #if SIZEOF_KERNEL_LONG_T > 4
177 # ifndef current_klongsize
178 if (current_klongsize < SIZEOF_KERNEL_LONG_T) {
179 # if defined(AARCH64) || defined(POWERPC64)
180 /* Align arg_no to the next even number. */
181 arg_no = (arg_no + 1) & 0xe;
182 # endif /* AARCH64 || POWERPC64 */
183 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
186 # endif /* !current_klongsize */
188 *val = tcp->u_arg[arg_no];
191 #else /* SIZEOF_KERNEL_LONG_T == 4 */
192 # if defined __ARM_EABI__ \
193 || defined LINUX_MIPSO32 \
196 /* Align arg_no to the next even number. */
197 arg_no = (arg_no + 1) & 0xe;
200 * The SH4 ABI does allow long longs in odd-numbered registers, but
201 * does not allow them to be split between registers and memory - and
202 * there are only four argument registers for normal functions. As a
203 * result, pread, for example, takes an extra padding argument before
204 * the offset. This was changed late in the 2.4 series (around 2.4.20).
208 # endif /* __ARM_EABI__ || LINUX_MIPSO32 || POWERPC || XTENSA || SH */
209 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
217 * Print 64bit argument at position arg_no and
218 * return the index of the next argument.
221 printllval(struct tcb *tcp, const char *format, int arg_no)
223 unsigned long long val = 0;
225 arg_no = getllval(tcp, &val, arg_no);
226 tprintf(format, val);
231 printaddr64(const uint64_t addr)
236 tprintf("%#" PRIx64, addr);
239 #define DEF_PRINTNUM(name, type) \
241 printnum_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
242 const char *const fmt) \
245 if (umove_or_printaddr(tcp, addr, &num)) \
253 #define DEF_PRINTNUM_ADDR(name, type) \
255 printnum_addr_ ## name(struct tcb *tcp, const kernel_ulong_t addr) \
258 if (umove_or_printaddr(tcp, addr, &num)) \
266 #define DEF_PRINTPAIR(name, type) \
268 printpair_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
269 const char *const fmt) \
272 if (umove_or_printaddr(tcp, addr, &pair)) \
275 tprintf(fmt, pair[0]); \
277 tprintf(fmt, pair[1]); \
282 DEF_PRINTNUM(int, int)
283 DEF_PRINTNUM_ADDR(int, unsigned int)
284 DEF_PRINTPAIR(int, int)
285 DEF_PRINTNUM(short, short)
286 DEF_PRINTNUM(int64, uint64_t)
287 DEF_PRINTNUM_ADDR(int64, uint64_t)
288 DEF_PRINTPAIR(int64, uint64_t)
290 #ifndef current_wordsize
292 printnum_long_int(struct tcb *const tcp, const kernel_ulong_t addr,
293 const char *const fmt_long, const char *const fmt_int)
295 if (current_wordsize > sizeof(int)) {
296 return printnum_int64(tcp, addr, fmt_long);
298 return printnum_int(tcp, addr, fmt_int);
303 printnum_addr_long_int(struct tcb *tcp, const kernel_ulong_t addr)
305 if (current_wordsize > sizeof(int)) {
306 return printnum_addr_int64(tcp, addr);
308 return printnum_addr_int(tcp, addr);
311 #endif /* !current_wordsize */
313 #ifndef current_klongsize
315 printnum_addr_klong_int(struct tcb *tcp, const kernel_ulong_t addr)
317 if (current_klongsize > sizeof(int)) {
318 return printnum_addr_int64(tcp, addr);
320 return printnum_addr_int(tcp, addr);
323 #endif /* !current_klongsize */
326 * Prints time to a (static internal) buffer and returns pointer to it.
327 * Returns NULL if the provided time specification is not correct.
329 * @param sec Seconds since epoch.
330 * @param part_sec Amount of second parts since the start of a second.
331 * @param max_part_sec Maximum value of a valid part_sec.
332 * @param width 1 + floor(log10(max_part_sec)).
333 * @return Pointer to a statically allocated string on success,
337 sprinttime_ex(const long long sec, const unsigned long long part_sec,
338 const unsigned int max_part_sec, const int width)
340 static char buf[sizeof(int) * 3 * 6 + sizeof(part_sec) * 3
343 if ((sec == 0 && part_sec == 0) || part_sec > max_part_sec)
346 time_t t = (time_t) sec;
347 struct tm *tmp = (sec == t) ? localtime(&t) : NULL;
351 size_t pos = strftime(buf, sizeof(buf), "%FT%T", tmp);
356 pos += xsnprintf(buf + pos, sizeof(buf) - pos, ".%0*llu",
359 return strftime(buf + pos, sizeof(buf) - pos, "%z", tmp) ? buf : NULL;
363 sprinttime(long long sec)
365 return sprinttime_ex(sec, 0, 0, 0);
369 sprinttime_usec(long long sec, unsigned long long usec)
371 return sprinttime_ex(sec, usec, 999999, 6);
375 sprinttime_nsec(long long sec, unsigned long long nsec)
377 return sprinttime_ex(sec, nsec, 999999999, 9);
381 getfdproto(struct tcb *tcp, int fd)
383 #ifdef HAVE_SYS_XATTR_H
384 size_t bufsize = 256;
387 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
390 return SOCK_PROTO_UNKNOWN;
392 xsprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
393 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
395 return SOCK_PROTO_UNKNOWN;
398 * This is a protection for the case when the kernel
399 * side does not append a null byte to the buffer.
403 return get_proto_by_name(buf);
406 return SOCK_PROTO_UNKNOWN;
411 getfdinode(struct tcb *tcp, int fd)
413 char path[PATH_MAX + 1];
415 if (getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
416 const char *str = STR_STRIP_PREFIX(path, "socket:[");
419 const size_t str_len = strlen(str);
420 if (str_len && str[str_len - 1] == ']')
421 return strtoul(str, NULL, 10);
429 printsocket(struct tcb *tcp, int fd, const char *path)
431 const char *str = STR_STRIP_PREFIX(path, "socket:[");
436 && (len = strlen(str))
437 && (str[len - 1] == ']')
438 && (inode = strtoul(str, NULL, 10))
439 && print_sockaddr_by_inode(tcp, fd, inode);
443 printdev(struct tcb *tcp, int fd, const char *path)
450 if (stat_file(path, &st)) {
451 debug_func_perror_msg("stat(\"%s\")", path);
455 switch (st.st_mode & S_IFMT) {
458 print_quoted_string_ex(path, strlen(path),
459 QUOTE_OMIT_LEADING_TRAILING_QUOTES,
461 tprintf("<%s %u:%u>",
462 S_ISBLK(st.st_mode)? "block" : "char",
463 major(st.st_rdev), minor(st.st_rdev));
471 printfd(struct tcb *tcp, int fd)
473 char path[PATH_MAX + 1];
474 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
476 if (show_fd_path <= 1
477 || (!printsocket(tcp, fd, path)
478 && !printdev(tcp, fd, path))) {
479 print_quoted_string_ex(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 * `escape_chars' specifies characters (in addition to characters with
492 * codes 0..31, 127..255, single and double quotes) that should be escaped.
494 * If QUOTE_0_TERMINATED `style' flag is set,
495 * treat `instr' as a NUL-terminated string,
496 * checking up to (`size' + 1) bytes of `instr'.
498 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
499 * do not add leading and trailing quoting symbols.
501 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
502 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
505 string_quote(const char *instr, char *outstr, const unsigned int size,
506 const unsigned int style, const char *escape_chars)
508 const unsigned char *ustr = (const unsigned char *) instr;
514 if (style & QUOTE_0_TERMINATED)
517 eol = 0x100; /* this can never match a char */
520 if ((xflag > 1) || (style & QUOTE_FORCE_HEX)) {
523 /* Check for presence of symbol which require
524 to hex-quote the whole string. */
525 for (i = 0; i < size; ++i) {
527 /* Check for NUL-terminated string. */
531 /* Force hex unless c is printable or whitespace */
536 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
537 * They happen to have ASCII codes 9,10,11,12,13.
539 if (c < ' ' && (unsigned)(c - 9) >= 5) {
546 if (style & QUOTE_EMIT_COMMENT)
547 s = stpcpy(s, " /* ");
548 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
552 /* Hex-quote the whole string. */
553 for (i = 0; i < size; ++i) {
555 /* Check for NUL-terminated string. */
560 *s++ = "0123456789abcdef"[c >> 4];
561 *s++ = "0123456789abcdef"[c & 0xf];
567 for (i = 0; i < size; ++i) {
569 /* Check for NUL-terminated string. */
572 if ((i == (size - 1)) &&
573 (style & QUOTE_OMIT_TRAILING_0) && (c == '\0'))
576 case '\"': case '\\':
601 escape = (c < ' ') || (c > 0x7e);
603 if (!escape && escape_chars)
604 escape = !!strchr(escape_chars, c);
612 && ustr[i + 1] >= '0'
613 && ustr[i + 1] <= '7'
616 *s++ = '0' + (c >> 6);
617 *s++ = '0' + ((c >> 3) & 0x7);
622 *s++ = '0' + (c >> 6);
623 *s++ = '0' + ((c >> 3) & 0x7);
626 *s++ = '0' + (c & 0x7);
632 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
634 if (style & QUOTE_EMIT_COMMENT)
635 s = stpcpy(s, " */");
638 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
639 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
640 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
641 * but next char is NUL.
649 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
651 if (style & QUOTE_EMIT_COMMENT)
652 s = stpcpy(s, " */");
654 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
658 #ifndef ALLOCA_CUTOFF
659 # define ALLOCA_CUTOFF 4032
661 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
664 * Quote string `str' of length `size' and print the result.
666 * If QUOTE_0_TERMINATED `style' flag is set,
667 * treat `str' as a NUL-terminated string and
668 * quote at most (`size' - 1) bytes.
670 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
671 * do not add leading and trailing quoting symbols.
673 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
674 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
677 print_quoted_string_ex(const char *str, unsigned int size,
678 const unsigned int style, const char *escape_chars)
682 unsigned int alloc_size;
685 if (size && style & QUOTE_0_TERMINATED)
688 alloc_size = 4 * size;
689 if (alloc_size / 4 != size) {
690 error_func_msg("requested %u bytes exceeds %u bytes limit",
695 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2) +
696 (style & QUOTE_EMIT_COMMENT ? 7 : 0);
698 if (use_alloca(alloc_size)) {
699 outstr = alloca(alloc_size);
702 outstr = buf = malloc(alloc_size);
704 error_func_msg("memory exhausted when tried to allocate"
705 " %u bytes", alloc_size);
711 rc = string_quote(str, outstr, size, style, escape_chars);
719 print_quoted_string(const char *str, unsigned int size,
720 const unsigned int style)
722 return print_quoted_string_ex(str, size, style, NULL);
726 * Quote a NUL-terminated string `str' of length up to `size' - 1
727 * and print the result.
729 * Returns 0 if NUL was seen, 1 otherwise.
732 print_quoted_cstring(const char *str, unsigned int size)
735 print_quoted_string(str, size, QUOTE_0_TERMINATED);
744 * Print path string specified by address `addr' and length `n'.
745 * If path length exceeds `n', append `...' to the output.
747 * Returns the result of umovenstr.
750 printpathn(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int n)
760 /* Cap path length to the path buffer size */
761 if (n > sizeof(path) - 1)
762 n = sizeof(path) - 1;
764 /* Fetch one byte more to find out whether path length > n. */
765 nul_seen = umovestr(tcp, addr, n + 1, path);
769 path[n++] = !nul_seen;
770 print_quoted_cstring(path, n);
777 printpath(struct tcb *const tcp, const kernel_ulong_t addr)
779 /* Size must correspond to char path[] size in printpathn */
780 return printpathn(tcp, addr, PATH_MAX - 1);
784 * Print string specified by address `addr' and length `len'.
785 * If `user_style' has QUOTE_0_TERMINATED bit set, treat the string
786 * as a NUL-terminated string.
787 * Pass `user_style' on to `string_quote'.
788 * Append `...' to the output if either the string length exceeds `max_strlen',
789 * or QUOTE_0_TERMINATED bit is set and the string length exceeds `len'.
791 * Returns the result of umovenstr if style has QUOTE_0_TERMINATED,
792 * or the result of umoven otherwise.
795 printstr_ex(struct tcb *const tcp, const kernel_ulong_t addr,
796 const kernel_ulong_t len, const unsigned int user_style)
802 unsigned int style = user_style;
810 /* Allocate static buffers if they are not allocated yet. */
812 const unsigned int outstr_size =
813 4 * max_strlen + /* for quotes and NUL */ 3;
815 * We can assume that outstr_size / 4 == max_strlen
816 * since we have a guarantee that max_strlen <= -1U / 4.
819 str = xmalloc(max_strlen + 1);
820 outstr = xmalloc(outstr_size);
823 /* Fetch one byte more because string_quote may look one byte ahead. */
824 size = max_strlen + 1;
828 if (style & QUOTE_0_TERMINATED)
829 rc = umovestr(tcp, addr, size, str);
831 rc = umoven(tcp, addr, size, str);
838 if (size > max_strlen)
843 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
844 * or we were requested to print more than -s NUM chars)...
846 ellipsis = string_quote(str, outstr, size, style, NULL)
848 && ((style & QUOTE_0_TERMINATED)
849 || len > max_strlen);
859 dumpiov_upto(struct tcb *const tcp, const int len, const kernel_ulong_t addr,
860 kernel_ulong_t data_size)
862 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
864 struct { uint32_t base; uint32_t len; } *iov32;
865 struct { uint64_t base; uint64_t len; } *iov64;
867 #define iov iovu.iov64
869 (current_wordsize == 4 ? (unsigned int) sizeof(*iovu.iov32) \
870 : (unsigned int) sizeof(*iovu.iov64))
871 #define iov_iov_base(i) \
872 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
873 #define iov_iov_len(i) \
874 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
877 #define sizeof_iov ((unsigned int) sizeof(*iov))
878 #define iov_iov_base(i) ptr_to_kulong(iov[i].iov_base)
879 #define iov_iov_len(i) iov[i].iov_len
882 unsigned int size = sizeof_iov * len;
883 if (size / sizeof_iov != (unsigned int) len) {
884 error_func_msg("requested %u iovec elements exceeds"
885 " %u iovec limit", len, -1U / sizeof_iov);
891 error_func_msg("memory exhausted when tried to allocate"
895 if (umoven(tcp, addr, size, iov) >= 0) {
896 for (i = 0; i < len; i++) {
897 kernel_ulong_t iov_len = iov_iov_len(i);
898 if (iov_len > data_size)
902 data_size -= iov_len;
903 /* include the buffer number to make it easy to
904 * match up the trace with the source */
905 tprintf(" * %" PRI_klu " bytes in buffer %d\n", iov_len, i);
906 dumpstr(tcp, iov_iov_base(i), iov_len);
917 dumpstr(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
919 static int strsize = -1;
920 static unsigned char *str;
925 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
926 "1234567890123456") + /*in case I'm off by few:*/ 4)
927 /*align to 8 to make memset easier:*/ + 7) & -8
929 const unsigned char *src;
932 if ((len < 0) || (len > INT_MAX - 16))
935 memset(outbuf, ' ', sizeof(outbuf));
937 if (strsize < len + 16) {
939 str = malloc(len + 16);
942 error_func_msg("memory exhausted when tried to allocate"
943 " %zu bytes", (size_t) (len + 16));
949 if (umoven(tcp, addr, len, str) < 0)
952 /* Space-pad to 16 bytes */
964 *dst++ = "0123456789abcdef"[*src >> 4];
965 *dst++ = "0123456789abcdef"[*src & 0xf];
970 dst++; /* space is there by memset */
973 dst++; /* space is there by memset */
980 if (*src >= ' ' && *src < 0x7f)
987 tprintf(" | %05x %s |\n", i - 16, outbuf);
992 tfetch_mem64(struct tcb *const tcp, const uint64_t addr,
993 const unsigned int len, void *const our_addr)
995 return addr && verbose(tcp) &&
996 (entering(tcp) || !syserror(tcp)) &&
997 !umoven(tcp, addr, len, our_addr);
1001 tfetch_mem64_ignore_syserror(struct tcb *const tcp, const uint64_t addr,
1002 const unsigned int len, void *const our_addr)
1004 return addr && verbose(tcp) &&
1005 !umoven(tcp, addr, len, our_addr);
1009 umoven_or_printaddr64(struct tcb *const tcp, const uint64_t addr,
1010 const unsigned int len, void *const our_addr)
1012 if (tfetch_mem64(tcp, addr, len, our_addr))
1019 umoven_or_printaddr64_ignore_syserror(struct tcb *const tcp,
1020 const uint64_t addr,
1021 const unsigned int len,
1022 void *const our_addr)
1024 if (tfetch_mem64_ignore_syserror(tcp, addr, len, our_addr))
1031 print_int32_array_member(struct tcb *tcp, void *elem_buf, size_t elem_size,
1034 tprintf("%" PRId32, *(int32_t *) elem_buf);
1040 print_uint32_array_member(struct tcb *tcp, void *elem_buf, size_t elem_size,
1043 tprintf("%" PRIu32, *(uint32_t *) elem_buf);
1049 print_uint64_array_member(struct tcb *tcp, void *elem_buf, size_t elem_size,
1052 tprintf("%" PRIu64, *(uint64_t *) elem_buf);
1058 * Iteratively fetch and print up to nmemb elements of elem_size size
1059 * from the array that starts at tracee's address start_addr.
1061 * Array elements are being fetched to the address specified by elem_buf.
1063 * The fetcher callback function specified by umoven_func should follow
1064 * the same semantics as umoven_or_printaddr function.
1066 * The printer callback function specified by print_func is expected
1067 * to print something; if it returns false, no more iterations will be made.
1069 * The pointer specified by opaque_data is passed to each invocation
1070 * of print_func callback function.
1072 * This function prints:
1073 * - "NULL", if start_addr is NULL;
1074 * - "[]", if nmemb is 0;
1075 * - start_addr, if nmemb * elem_size overflows or wraps around;
1076 * - nothing, if the first element cannot be fetched
1077 * (if umoven_func returns non-zero), but it is assumed that
1078 * umoven_func has printed the address it failed to fetch data from;
1079 * - elements of the array, delimited by ", ", with the array itself
1080 * enclosed with [] brackets.
1082 * If abbrev(tcp) is true, then
1083 * - the maximum number of elements printed equals to max_strlen;
1084 * - "..." is printed instead of max_strlen+1 element
1085 * and no more iterations will be made.
1087 * This function returns true only if
1088 * - umoven_func has been called at least once AND
1089 * - umoven_func has not returned false.
1092 print_array(struct tcb *const tcp,
1093 const kernel_ulong_t start_addr,
1095 void *const elem_buf,
1096 const size_t elem_size,
1097 int (*const umoven_func)(struct tcb *,
1101 bool (*const print_func)(struct tcb *,
1105 void *const opaque_data)
1117 const size_t size = nmemb * elem_size;
1118 const kernel_ulong_t end_addr = start_addr + size;
1120 if (end_addr <= start_addr || size / elem_size != nmemb) {
1121 printaddr(start_addr);
1125 const kernel_ulong_t abbrev_end =
1126 (abbrev(tcp) && max_strlen < nmemb) ?
1127 start_addr + elem_size * max_strlen : end_addr;
1130 for (cur = start_addr; cur < end_addr; cur += elem_size) {
1131 if (cur != start_addr)
1134 if (umoven_func(tcp, cur, elem_size, elem_buf))
1137 if (cur == start_addr)
1140 if (cur >= abbrev_end) {
1146 if (!print_func(tcp, elem_buf, elem_size, opaque_data)) {
1151 if (cur != start_addr)
1154 return cur >= end_addr;
1158 printargs(struct tcb *tcp)
1160 const int n = tcp->s_ent->nargs;
1162 for (i = 0; i < n; ++i)
1163 tprintf("%s%#" PRI_klx, i ? ", " : "", tcp->u_arg[i]);
1164 return RVAL_DECODED;
1168 printargs_u(struct tcb *tcp)
1170 const int n = tcp->s_ent->nargs;
1172 for (i = 0; i < n; ++i)
1173 tprintf("%s%u", i ? ", " : "",
1174 (unsigned int) tcp->u_arg[i]);
1175 return RVAL_DECODED;
1179 printargs_d(struct tcb *tcp)
1181 const int n = tcp->s_ent->nargs;
1183 for (i = 0; i < n; ++i)
1184 tprintf("%s%d", i ? ", " : "",
1185 (int) tcp->u_arg[i]);
1186 return RVAL_DECODED;
1189 /* Print abnormal high bits of a kernel_ulong_t value. */
1191 print_abnormal_hi(const kernel_ulong_t val)
1193 if (current_klongsize > 4) {
1194 const unsigned int hi = (unsigned int) ((uint64_t) val >> 32);
1196 tprintf("%#x<<32|", hi);
1200 #if defined _LARGEFILE64_SOURCE && defined HAVE_OPEN64
1201 # define open_file open64
1203 # define open_file open
1207 read_int_from_file(struct tcb *tcp, const char *const fname, int *const pvalue)
1209 const int fd = open_file(fname, O_RDONLY);
1214 char buf[sizeof(lval) * 3];
1215 int n = read(fd, buf, sizeof(buf) - 1);
1216 int saved_errno = errno;
1220 errno = saved_errno;
1227 lval = strtol(buf, &endptr, 10);
1228 if (!endptr || (*endptr && '\n' != *endptr)
1229 #if INT_MAX < LONG_MAX
1230 || lval > INT_MAX || lval < INT_MIN
1232 || ERANGE == errno) {
1238 *pvalue = (int) lval;