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);
240 printaddr(const kernel_ulong_t addr)
245 #define DEF_PRINTNUM(name, type) \
247 printnum_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
248 const char *const fmt) \
251 if (umove_or_printaddr(tcp, addr, &num)) \
259 #define DEF_PRINTNUM_ADDR(name, type) \
261 printnum_addr_ ## name(struct tcb *tcp, const kernel_ulong_t addr) \
264 if (umove_or_printaddr(tcp, addr, &num)) \
272 #define DEF_PRINTPAIR(name, type) \
274 printpair_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
275 const char *const fmt) \
278 if (umove_or_printaddr(tcp, addr, &pair)) \
281 tprintf(fmt, pair[0]); \
283 tprintf(fmt, pair[1]); \
288 DEF_PRINTNUM(int, int)
289 DEF_PRINTNUM_ADDR(int, unsigned int)
290 DEF_PRINTPAIR(int, int)
291 DEF_PRINTNUM(short, short)
292 DEF_PRINTNUM(int64, uint64_t)
293 DEF_PRINTNUM_ADDR(int64, uint64_t)
294 DEF_PRINTPAIR(int64, uint64_t)
296 #ifndef current_wordsize
298 printnum_long_int(struct tcb *const tcp, const kernel_ulong_t addr,
299 const char *const fmt_long, const char *const fmt_int)
301 if (current_wordsize > sizeof(int)) {
302 return printnum_int64(tcp, addr, fmt_long);
304 return printnum_int(tcp, addr, fmt_int);
309 printnum_addr_long_int(struct tcb *tcp, const kernel_ulong_t addr)
311 if (current_wordsize > sizeof(int)) {
312 return printnum_addr_int64(tcp, addr);
314 return printnum_addr_int(tcp, addr);
317 #endif /* !current_wordsize */
319 #ifndef current_klongsize
321 printnum_addr_klong_int(struct tcb *tcp, const kernel_ulong_t addr)
323 if (current_klongsize > sizeof(int)) {
324 return printnum_addr_int64(tcp, addr);
326 return printnum_addr_int(tcp, addr);
329 #endif /* !current_klongsize */
332 * Prints time to a (static internal) buffer and returns pointer to it.
333 * Returns NULL if the provided time specification is not correct.
335 * @param sec Seconds since epoch.
336 * @param part_sec Amount of second parts since the start of a second.
337 * @param max_part_sec Maximum value of a valid part_sec.
338 * @param width 1 + floor(log10(max_part_sec)).
339 * @return Pointer to a statically allocated string on success,
343 sprinttime_ex(const long long sec, const unsigned long long part_sec,
344 const unsigned int max_part_sec, const int width)
346 static char buf[sizeof(int) * 3 * 6 + sizeof(part_sec) * 3
349 if ((sec == 0 && part_sec == 0) || part_sec > max_part_sec)
352 time_t t = (time_t) sec;
353 struct tm *tmp = (sec == t) ? localtime(&t) : NULL;
357 size_t pos = strftime(buf, sizeof(buf), "%FT%T", tmp);
362 pos += xsnprintf(buf + pos, sizeof(buf) - pos, ".%0*llu",
365 return strftime(buf + pos, sizeof(buf) - pos, "%z", tmp) ? buf : NULL;
369 sprinttime(long long sec)
371 return sprinttime_ex(sec, 0, 0, 0);
375 sprinttime_usec(long long sec, unsigned long long usec)
377 return sprinttime_ex(sec, usec, 999999, 6);
381 sprinttime_nsec(long long sec, unsigned long long nsec)
383 return sprinttime_ex(sec, nsec, 999999999, 9);
387 getfdproto(struct tcb *tcp, int fd)
389 #ifdef HAVE_SYS_XATTR_H
390 size_t bufsize = 256;
393 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
396 return SOCK_PROTO_UNKNOWN;
398 xsprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
399 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
401 return SOCK_PROTO_UNKNOWN;
404 * This is a protection for the case when the kernel
405 * side does not append a null byte to the buffer.
409 return get_proto_by_name(buf);
412 return SOCK_PROTO_UNKNOWN;
417 getfdinode(struct tcb *tcp, int fd)
419 char path[PATH_MAX + 1];
421 if (getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
422 const char *str = STR_STRIP_PREFIX(path, "socket:[");
425 const size_t str_len = strlen(str);
426 if (str_len && str[str_len - 1] == ']')
427 return strtoul(str, NULL, 10);
435 printsocket(struct tcb *tcp, int fd, const char *path)
437 const char *str = STR_STRIP_PREFIX(path, "socket:[");
442 && (len = strlen(str))
443 && (str[len - 1] == ']')
444 && (inode = strtoul(str, NULL, 10))
445 && print_sockaddr_by_inode(tcp, fd, inode);
449 printdev(struct tcb *tcp, int fd, const char *path)
456 if (stat_file(path, &st)) {
457 debug_func_perror_msg("stat(\"%s\")", path);
461 switch (st.st_mode & S_IFMT) {
464 print_quoted_string_ex(path, strlen(path),
465 QUOTE_OMIT_LEADING_TRAILING_QUOTES,
467 tprintf("<%s %u:%u>",
468 S_ISBLK(st.st_mode)? "block" : "char",
469 major(st.st_rdev), minor(st.st_rdev));
477 printfd(struct tcb *tcp, int fd)
479 char path[PATH_MAX + 1];
480 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
482 if (show_fd_path <= 1
483 || (!printsocket(tcp, fd, path)
484 && !printdev(tcp, fd, path))) {
485 print_quoted_string_ex(path, strlen(path),
486 QUOTE_OMIT_LEADING_TRAILING_QUOTES, "<>");
494 * Quote string `instr' of length `size'
495 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
497 * `escape_chars' specifies characters (in addition to characters with
498 * codes 0..31, 127..255, single and double quotes) that should be escaped.
500 * If QUOTE_0_TERMINATED `style' flag is set,
501 * treat `instr' as a NUL-terminated string,
502 * checking up to (`size' + 1) bytes of `instr'.
504 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
505 * do not add leading and trailing quoting symbols.
507 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
508 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
511 string_quote(const char *instr, char *outstr, const unsigned int size,
512 const unsigned int style, const char *escape_chars)
514 const unsigned char *ustr = (const unsigned char *) instr;
520 if (style & QUOTE_0_TERMINATED)
523 eol = 0x100; /* this can never match a char */
526 if ((xflag > 1) || (style & QUOTE_FORCE_HEX)) {
529 /* Check for presence of symbol which require
530 to hex-quote the whole string. */
531 for (i = 0; i < size; ++i) {
533 /* Check for NUL-terminated string. */
537 /* Force hex unless c is printable or whitespace */
542 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
543 * They happen to have ASCII codes 9,10,11,12,13.
545 if (c < ' ' && (unsigned)(c - 9) >= 5) {
552 if (style & QUOTE_EMIT_COMMENT)
553 s = stpcpy(s, " /* ");
554 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
558 /* Hex-quote the whole string. */
559 for (i = 0; i < size; ++i) {
561 /* Check for NUL-terminated string. */
566 *s++ = "0123456789abcdef"[c >> 4];
567 *s++ = "0123456789abcdef"[c & 0xf];
573 for (i = 0; i < size; ++i) {
575 /* Check for NUL-terminated string. */
578 if ((i == (size - 1)) &&
579 (style & QUOTE_OMIT_TRAILING_0) && (c == '\0'))
582 case '\"': case '\\':
607 escape = (c < ' ') || (c > 0x7e);
609 if (!escape && escape_chars)
610 escape = !!strchr(escape_chars, c);
618 && ustr[i + 1] >= '0'
619 && ustr[i + 1] <= '7'
622 *s++ = '0' + (c >> 6);
623 *s++ = '0' + ((c >> 3) & 0x7);
628 *s++ = '0' + (c >> 6);
629 *s++ = '0' + ((c >> 3) & 0x7);
632 *s++ = '0' + (c & 0x7);
638 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
640 if (style & QUOTE_EMIT_COMMENT)
641 s = stpcpy(s, " */");
644 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
645 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
646 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
647 * but next char is NUL.
655 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
657 if (style & QUOTE_EMIT_COMMENT)
658 s = stpcpy(s, " */");
660 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
664 #ifndef ALLOCA_CUTOFF
665 # define ALLOCA_CUTOFF 4032
667 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
670 * Quote string `str' of length `size' and print the result.
672 * If QUOTE_0_TERMINATED `style' flag is set,
673 * treat `str' as a NUL-terminated string and
674 * quote at most (`size' - 1) bytes.
676 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
677 * do not add leading and trailing quoting symbols.
679 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
680 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
683 print_quoted_string_ex(const char *str, unsigned int size,
684 const unsigned int style, const char *escape_chars)
688 unsigned int alloc_size;
691 if (size && style & QUOTE_0_TERMINATED)
694 alloc_size = 4 * size;
695 if (alloc_size / 4 != size) {
696 error_msg("Out of memory");
700 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2) +
701 (style & QUOTE_EMIT_COMMENT ? 7 : 0);
703 if (use_alloca(alloc_size)) {
704 outstr = alloca(alloc_size);
707 outstr = buf = malloc(alloc_size);
709 error_msg("Out of memory");
715 rc = string_quote(str, outstr, size, style, escape_chars);
723 print_quoted_string(const char *str, unsigned int size,
724 const unsigned int style)
726 return print_quoted_string_ex(str, size, style, NULL);
730 * Quote a NUL-terminated string `str' of length up to `size' - 1
731 * and print the result.
733 * Returns 0 if NUL was seen, 1 otherwise.
736 print_quoted_cstring(const char *str, unsigned int size)
739 print_quoted_string(str, size, QUOTE_0_TERMINATED);
748 * Print path string specified by address `addr' and length `n'.
749 * If path length exceeds `n', append `...' to the output.
751 * Returns the result of umovenstr.
754 printpathn(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int n)
764 /* Cap path length to the path buffer size */
765 if (n > sizeof(path) - 1)
766 n = sizeof(path) - 1;
768 /* Fetch one byte more to find out whether path length > n. */
769 nul_seen = umovestr(tcp, addr, n + 1, path);
773 path[n++] = !nul_seen;
774 print_quoted_cstring(path, n);
781 printpath(struct tcb *const tcp, const kernel_ulong_t addr)
783 /* Size must correspond to char path[] size in printpathn */
784 return printpathn(tcp, addr, PATH_MAX - 1);
788 * Print string specified by address `addr' and length `len'.
789 * If `user_style' has QUOTE_0_TERMINATED bit set, treat the string
790 * as a NUL-terminated string.
791 * Pass `user_style' on to `string_quote'.
792 * Append `...' to the output if either the string length exceeds `max_strlen',
793 * or QUOTE_0_TERMINATED bit is set and the string length exceeds `len'.
795 * Returns the result of umovenstr if style has QUOTE_0_TERMINATED,
796 * or the result of umoven otherwise.
799 printstr_ex(struct tcb *const tcp, const kernel_ulong_t addr,
800 const kernel_ulong_t len, const unsigned int user_style)
806 unsigned int style = user_style;
814 /* Allocate static buffers if they are not allocated yet. */
816 const unsigned int outstr_size =
817 4 * max_strlen + /* for quotes and NUL */ 3;
819 * We can assume that outstr_size / 4 == max_strlen
820 * since we have a guarantee that max_strlen <= -1U / 4.
823 str = xmalloc(max_strlen + 1);
824 outstr = xmalloc(outstr_size);
827 /* Fetch one byte more because string_quote may look one byte ahead. */
828 size = max_strlen + 1;
832 if (style & QUOTE_0_TERMINATED)
833 rc = umovestr(tcp, addr, size, str);
835 rc = umoven(tcp, addr, size, str);
842 if (size > max_strlen)
847 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
848 * or we were requested to print more than -s NUM chars)...
850 ellipsis = string_quote(str, outstr, size, style, NULL)
852 && ((style & QUOTE_0_TERMINATED)
853 || len > max_strlen);
863 dumpiov_upto(struct tcb *const tcp, const int len, const kernel_ulong_t addr,
864 kernel_ulong_t data_size)
866 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
868 struct { uint32_t base; uint32_t len; } *iov32;
869 struct { uint64_t base; uint64_t len; } *iov64;
871 #define iov iovu.iov64
873 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
874 #define iov_iov_base(i) \
875 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
876 #define iov_iov_len(i) \
877 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
880 #define sizeof_iov sizeof(*iov)
881 #define iov_iov_base(i) ptr_to_kulong(iov[i].iov_base)
882 #define iov_iov_len(i) iov[i].iov_len
887 size = sizeof_iov * len;
888 /* Assuming no sane program has millions of iovs */
889 if ((unsigned)len > 1024*1024 /* insane or negative size? */
890 || (iov = malloc(size)) == NULL) {
891 error_msg("Out of memory");
894 if (umoven(tcp, addr, size, iov) >= 0) {
895 for (i = 0; i < len; i++) {
896 kernel_ulong_t iov_len = iov_iov_len(i);
897 if (iov_len > data_size)
901 data_size -= iov_len;
902 /* include the buffer number to make it easy to
903 * match up the trace with the source */
904 tprintf(" * %" PRI_klu " bytes in buffer %d\n", iov_len, i);
905 dumpstr(tcp, iov_iov_base(i), iov_len);
916 dumpstr(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
918 static int strsize = -1;
919 static unsigned char *str;
924 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
925 "1234567890123456") + /*in case I'm off by few:*/ 4)
926 /*align to 8 to make memset easier:*/ + 7) & -8
928 const unsigned char *src;
931 memset(outbuf, ' ', sizeof(outbuf));
933 if (strsize < len + 16) {
935 str = malloc(len + 16);
938 error_msg("Out of memory");
944 if (umoven(tcp, addr, len, str) < 0)
947 /* Space-pad to 16 bytes */
959 *dst++ = "0123456789abcdef"[*src >> 4];
960 *dst++ = "0123456789abcdef"[*src & 0xf];
965 dst++; /* space is there by memset */
968 dst++; /* space is there by memset */
975 if (*src >= ' ' && *src < 0x7f)
982 tprintf(" | %05x %s |\n", i - 16, outbuf);
987 umoven_or_printaddr(struct tcb *const tcp, const kernel_ulong_t addr,
988 const unsigned int len, void *const our_addr)
990 if (!addr || !verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
991 umoven(tcp, addr, len, our_addr) < 0) {
999 umoven_or_printaddr_ignore_syserror(struct tcb *const tcp,
1000 const kernel_ulong_t addr,
1001 const unsigned int len,
1002 void *const our_addr)
1004 if (!addr || !verbose(tcp) || umoven(tcp, addr, len, our_addr) < 0) {
1012 * Iteratively fetch and print up to nmemb elements of elem_size size
1013 * from the array that starts at tracee's address start_addr.
1015 * Array elements are being fetched to the address specified by elem_buf.
1017 * The fetcher callback function specified by umoven_func should follow
1018 * the same semantics as umoven_or_printaddr function.
1020 * The printer callback function specified by print_func is expected
1021 * to print something; if it returns false, no more iterations will be made.
1023 * The pointer specified by opaque_data is passed to each invocation
1024 * of print_func callback function.
1026 * This function prints:
1027 * - "NULL", if start_addr is NULL;
1028 * - "[]", if nmemb is 0;
1029 * - start_addr, if nmemb * elem_size overflows or wraps around;
1030 * - nothing, if the first element cannot be fetched
1031 * (if umoven_func returns non-zero), but it is assumed that
1032 * umoven_func has printed the address it failed to fetch data from;
1033 * - elements of the array, delimited by ", ", with the array itself
1034 * enclosed with [] brackets.
1036 * If abbrev(tcp) is true, then
1037 * - the maximum number of elements printed equals to max_strlen;
1038 * - "..." is printed instead of max_strlen+1 element
1039 * and no more iterations will be made.
1041 * This function returns true only if
1042 * - umoven_func has been called at least once AND
1043 * - umoven_func has not returned false.
1046 print_array(struct tcb *const tcp,
1047 const kernel_ulong_t start_addr,
1049 void *const elem_buf,
1050 const size_t elem_size,
1051 int (*const umoven_func)(struct tcb *,
1055 bool (*const print_func)(struct tcb *,
1059 void *const opaque_data)
1071 const size_t size = nmemb * elem_size;
1072 const kernel_ulong_t end_addr = start_addr + size;
1074 if (end_addr <= start_addr || size / elem_size != nmemb) {
1075 printaddr(start_addr);
1079 const kernel_ulong_t abbrev_end =
1080 (abbrev(tcp) && max_strlen < nmemb) ?
1081 start_addr + elem_size * max_strlen : end_addr;
1084 for (cur = start_addr; cur < end_addr; cur += elem_size) {
1085 if (cur != start_addr)
1088 if (umoven_func(tcp, cur, elem_size, elem_buf))
1091 if (cur == start_addr)
1094 if (cur >= abbrev_end) {
1100 if (!print_func(tcp, elem_buf, elem_size, opaque_data)) {
1105 if (cur != start_addr)
1108 return cur >= end_addr;
1112 printargs(struct tcb *tcp)
1114 const int n = tcp->s_ent->nargs;
1116 for (i = 0; i < n; ++i)
1117 tprintf("%s%#" PRI_klx, i ? ", " : "", tcp->u_arg[i]);
1118 return RVAL_DECODED;
1122 printargs_u(struct tcb *tcp)
1124 const int n = tcp->s_ent->nargs;
1126 for (i = 0; i < n; ++i)
1127 tprintf("%s%u", i ? ", " : "",
1128 (unsigned int) tcp->u_arg[i]);
1129 return RVAL_DECODED;
1133 printargs_d(struct tcb *tcp)
1135 const int n = tcp->s_ent->nargs;
1137 for (i = 0; i < n; ++i)
1138 tprintf("%s%d", i ? ", " : "",
1139 (int) tcp->u_arg[i]);
1140 return RVAL_DECODED;
1143 /* Print abnormal high bits of a kernel_ulong_t value. */
1145 print_abnormal_hi(const kernel_ulong_t val)
1147 if (current_klongsize > 4) {
1148 const unsigned int hi = (unsigned int) ((uint64_t) val >> 32);
1150 tprintf("%#x<<32|", hi);
1154 #if defined _LARGEFILE64_SOURCE && defined HAVE_OPEN64
1155 # define open_file open64
1157 # define open_file open
1161 read_int_from_file(struct tcb *tcp, const char *const fname, int *const pvalue)
1163 const int fd = open_file(fname, O_RDONLY);
1168 char buf[sizeof(lval) * 3];
1169 int n = read(fd, buf, sizeof(buf) - 1);
1170 int saved_errno = errno;
1174 errno = saved_errno;
1181 lval = strtol(buf, &endptr, 10);
1182 if (!endptr || (*endptr && '\n' != *endptr)
1183 #if INT_MAX < LONG_MAX
1184 || lval > INT_MAX || lval < INT_MIN
1186 || ERANGE == errno) {
1192 *pvalue = (int) lval;