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 tv_nz(const struct timeval *a)
52 return a->tv_sec || a->tv_usec;
56 tv_cmp(const struct timeval *a, const struct timeval *b)
58 if (a->tv_sec < b->tv_sec
59 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
61 if (a->tv_sec > b->tv_sec
62 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
68 tv_float(const struct timeval *tv)
70 return tv->tv_sec + tv->tv_usec/1000000.0;
74 tv_add(struct timeval *tv, const struct timeval *a, const struct timeval *b)
76 tv->tv_sec = a->tv_sec + b->tv_sec;
77 tv->tv_usec = a->tv_usec + b->tv_usec;
78 if (tv->tv_usec >= 1000000) {
80 tv->tv_usec -= 1000000;
85 tv_sub(struct timeval *tv, const struct timeval *a, const struct timeval *b)
87 tv->tv_sec = a->tv_sec - b->tv_sec;
88 tv->tv_usec = a->tv_usec - b->tv_usec;
89 if (((long) tv->tv_usec) < 0) {
91 tv->tv_usec += 1000000;
96 tv_div(struct timeval *tv, const struct timeval *a, int n)
98 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
99 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
100 tv->tv_usec %= 1000000;
104 tv_mul(struct timeval *tv, const struct timeval *a, int n)
106 tv->tv_usec = a->tv_usec * n;
107 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
108 tv->tv_usec %= 1000000;
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.
334 * @param sec Seconds since epoch.
335 * @param part_sec Amount of second parts since the start of a second.
336 * @param max_part_sec Maximum value of a valid part_sec.
337 * @param width 1 + floor(log10(max_part_sec)).
340 sprinttime_ex(const long long sec, const unsigned long long part_sec,
341 const unsigned int max_part_sec, const int width)
343 static char buf[sizeof(int) * 3 * 6 + sizeof(part_sec) * 3
346 if ((sec == 0 && part_sec == 0) || part_sec > max_part_sec)
349 time_t t = (time_t) sec;
350 struct tm *tmp = (sec == t) ? localtime(&t) : NULL;
354 size_t pos = strftime(buf, sizeof(buf), "%FT%T", tmp);
359 pos += xsnprintf(buf + pos, sizeof(buf) - pos, ".%0*llu",
362 return strftime(buf + pos, sizeof(buf) - pos, "%z", tmp) ? buf : NULL;
366 sprinttime(long long sec)
368 return sprinttime_ex(sec, 0, 0, 0);
372 sprinttime_usec(long long sec, unsigned long long usec)
374 return sprinttime_ex(sec, usec, 999999, 6);
378 sprinttime_nsec(long long sec, unsigned long long nsec)
380 return sprinttime_ex(sec, nsec, 999999999, 9);
384 getfdproto(struct tcb *tcp, int fd)
386 #ifdef HAVE_SYS_XATTR_H
387 size_t bufsize = 256;
390 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
393 return SOCK_PROTO_UNKNOWN;
395 xsprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
396 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
398 return SOCK_PROTO_UNKNOWN;
401 * This is a protection for the case when the kernel
402 * side does not append a null byte to the buffer.
406 return get_proto_by_name(buf);
409 return SOCK_PROTO_UNKNOWN;
414 getfdinode(struct tcb *tcp, int fd)
416 char path[PATH_MAX + 1];
418 if (getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
419 const char *str = STR_STRIP_PREFIX(path, "socket:[");
422 const size_t str_len = strlen(str);
423 if (str_len && str[str_len - 1] == ']')
424 return strtoul(str, NULL, 10);
432 printsocket(struct tcb *tcp, int fd, const char *path)
434 const char *str = STR_STRIP_PREFIX(path, "socket:[");
439 && (len = strlen(str))
440 && (str[len - 1] == ']')
441 && (inode = strtoul(str, NULL, 10))
442 && print_sockaddr_by_inode(tcp, fd, inode);
446 printdev(struct tcb *tcp, int fd, const char *path)
453 if (stat_file(path, &st)) {
454 debug_func_perror_msg("stat(\"%s\")", path);
458 switch (st.st_mode & S_IFMT) {
461 print_quoted_string_ex(path, strlen(path),
462 QUOTE_OMIT_LEADING_TRAILING_QUOTES,
464 tprintf("<%s %u:%u>",
465 S_ISBLK(st.st_mode)? "block" : "char",
466 major(st.st_rdev), minor(st.st_rdev));
474 printfd(struct tcb *tcp, int fd)
476 char path[PATH_MAX + 1];
477 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
479 if (show_fd_path <= 1
480 || (!printsocket(tcp, fd, path)
481 && !printdev(tcp, fd, path))) {
482 print_quoted_string_ex(path, strlen(path),
483 QUOTE_OMIT_LEADING_TRAILING_QUOTES, "<>");
491 * Quote string `instr' of length `size'
492 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
494 * `escape_chars' specifies characters (in addition to characters with
495 * codes 0..31, 127..255, single and double quotes) that should be escaped.
497 * If QUOTE_0_TERMINATED `style' flag is set,
498 * treat `instr' as a NUL-terminated string,
499 * checking up to (`size' + 1) bytes of `instr'.
501 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
502 * do not add leading and trailing quoting symbols.
504 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
505 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
508 string_quote(const char *instr, char *outstr, const unsigned int size,
509 const unsigned int style, const char *escape_chars)
511 const unsigned char *ustr = (const unsigned char *) instr;
517 if (style & QUOTE_0_TERMINATED)
520 eol = 0x100; /* this can never match a char */
523 if ((xflag > 1) || (style & QUOTE_FORCE_HEX)) {
526 /* Check for presence of symbol which require
527 to hex-quote the whole string. */
528 for (i = 0; i < size; ++i) {
530 /* Check for NUL-terminated string. */
534 /* Force hex unless c is printable or whitespace */
539 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
540 * They happen to have ASCII codes 9,10,11,12,13.
542 if (c < ' ' && (unsigned)(c - 9) >= 5) {
549 if (style & QUOTE_EMIT_COMMENT)
550 s = stpcpy(s, " /* ");
551 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
555 /* Hex-quote the whole string. */
556 for (i = 0; i < size; ++i) {
558 /* Check for NUL-terminated string. */
563 *s++ = "0123456789abcdef"[c >> 4];
564 *s++ = "0123456789abcdef"[c & 0xf];
570 for (i = 0; i < size; ++i) {
572 /* Check for NUL-terminated string. */
575 if ((i == (size - 1)) &&
576 (style & QUOTE_OMIT_TRAILING_0) && (c == '\0'))
579 case '\"': case '\\':
604 escape = (c < ' ') || (c > 0x7e);
606 if (!escape && escape_chars)
607 escape = !!strchr(escape_chars, c);
615 && ustr[i + 1] >= '0'
616 && ustr[i + 1] <= '7'
619 *s++ = '0' + (c >> 6);
620 *s++ = '0' + ((c >> 3) & 0x7);
625 *s++ = '0' + (c >> 6);
626 *s++ = '0' + ((c >> 3) & 0x7);
629 *s++ = '0' + (c & 0x7);
635 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
637 if (style & QUOTE_EMIT_COMMENT)
638 s = stpcpy(s, " */");
641 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
642 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
643 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
644 * but next char is NUL.
652 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
654 if (style & QUOTE_EMIT_COMMENT)
655 s = stpcpy(s, " */");
657 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
661 #ifndef ALLOCA_CUTOFF
662 # define ALLOCA_CUTOFF 4032
664 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
667 * Quote string `str' of length `size' and print the result.
669 * If QUOTE_0_TERMINATED `style' flag is set,
670 * treat `str' as a NUL-terminated string and
671 * quote at most (`size' - 1) bytes.
673 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
674 * do not add leading and trailing quoting symbols.
676 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
677 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
680 print_quoted_string_ex(const char *str, unsigned int size,
681 const unsigned int style, const char *escape_chars)
685 unsigned int alloc_size;
688 if (size && style & QUOTE_0_TERMINATED)
691 alloc_size = 4 * size;
692 if (alloc_size / 4 != size) {
693 error_msg("Out of memory");
697 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2) +
698 (style & QUOTE_EMIT_COMMENT ? 7 : 0);
700 if (use_alloca(alloc_size)) {
701 outstr = alloca(alloc_size);
704 outstr = buf = malloc(alloc_size);
706 error_msg("Out of memory");
712 rc = string_quote(str, outstr, size, style, escape_chars);
720 print_quoted_string(const char *str, unsigned int size,
721 const unsigned int style)
723 return print_quoted_string_ex(str, size, style, NULL);
727 * Quote a NUL-terminated string `str' of length up to `size' - 1
728 * and print the result.
730 * Returns 0 if NUL was seen, 1 otherwise.
733 print_quoted_cstring(const char *str, unsigned int size)
736 print_quoted_string(str, size, QUOTE_0_TERMINATED);
745 * Print path string specified by address `addr' and length `n'.
746 * If path length exceeds `n', append `...' to the output.
748 * Returns the result of umovenstr.
751 printpathn(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int n)
761 /* Cap path length to the path buffer size */
762 if (n > sizeof(path) - 1)
763 n = sizeof(path) - 1;
765 /* Fetch one byte more to find out whether path length > n. */
766 nul_seen = umovestr(tcp, addr, n + 1, path);
770 path[n++] = !nul_seen;
771 print_quoted_cstring(path, n);
778 printpath(struct tcb *const tcp, const kernel_ulong_t addr)
780 /* Size must correspond to char path[] size in printpathn */
781 return printpathn(tcp, addr, PATH_MAX - 1);
785 * Print string specified by address `addr' and length `len'.
786 * If `user_style' has QUOTE_0_TERMINATED bit set, treat the string
787 * as a NUL-terminated string.
788 * Pass `user_style' on to `string_quote'.
789 * Append `...' to the output if either the string length exceeds `max_strlen',
790 * or QUOTE_0_TERMINATED bit is set and the string length exceeds `len'.
792 * Returns the result of umovenstr if style has QUOTE_0_TERMINATED,
793 * or the result of umoven otherwise.
796 printstr_ex(struct tcb *const tcp, const kernel_ulong_t addr,
797 const kernel_ulong_t len, const unsigned int user_style)
803 unsigned int style = user_style;
811 /* Allocate static buffers if they are not allocated yet. */
813 const unsigned int outstr_size =
814 4 * max_strlen + /* for quotes and NUL */ 3;
816 * We can assume that outstr_size / 4 == max_strlen
817 * since we have a guarantee that max_strlen <= -1U / 4.
820 str = xmalloc(max_strlen + 1);
821 outstr = xmalloc(outstr_size);
824 /* Fetch one byte more because string_quote may look one byte ahead. */
825 size = max_strlen + 1;
829 if (style & QUOTE_0_TERMINATED)
830 rc = umovestr(tcp, addr, size, str);
832 rc = umoven(tcp, addr, size, str);
839 if (size > max_strlen)
844 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
845 * or we were requested to print more than -s NUM chars)...
847 ellipsis = string_quote(str, outstr, size, style, NULL)
849 && ((style & QUOTE_0_TERMINATED)
850 || len > max_strlen);
860 dumpiov_upto(struct tcb *const tcp, const int len, const kernel_ulong_t addr,
861 kernel_ulong_t data_size)
863 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
865 struct { uint32_t base; uint32_t len; } *iov32;
866 struct { uint64_t base; uint64_t len; } *iov64;
868 #define iov iovu.iov64
870 (current_wordsize == 4 ? sizeof(*iovu.iov32) : 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 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
884 size = sizeof_iov * len;
885 /* Assuming no sane program has millions of iovs */
886 if ((unsigned)len > 1024*1024 /* insane or negative size? */
887 || (iov = malloc(size)) == NULL) {
888 error_msg("Out of memory");
891 if (umoven(tcp, addr, size, iov) >= 0) {
892 for (i = 0; i < len; i++) {
893 kernel_ulong_t iov_len = iov_iov_len(i);
894 if (iov_len > data_size)
898 data_size -= iov_len;
899 /* include the buffer number to make it easy to
900 * match up the trace with the source */
901 tprintf(" * %" PRI_klu " bytes in buffer %d\n", iov_len, i);
902 dumpstr(tcp, iov_iov_base(i), iov_len);
913 dumpstr(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
915 static int strsize = -1;
916 static unsigned char *str;
921 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
922 "1234567890123456") + /*in case I'm off by few:*/ 4)
923 /*align to 8 to make memset easier:*/ + 7) & -8
925 const unsigned char *src;
928 memset(outbuf, ' ', sizeof(outbuf));
930 if (strsize < len + 16) {
932 str = malloc(len + 16);
935 error_msg("Out of memory");
941 if (umoven(tcp, addr, len, str) < 0)
944 /* Space-pad to 16 bytes */
956 *dst++ = "0123456789abcdef"[*src >> 4];
957 *dst++ = "0123456789abcdef"[*src & 0xf];
962 dst++; /* space is there by memset */
965 dst++; /* space is there by memset */
972 if (*src >= ' ' && *src < 0x7f)
979 tprintf(" | %05x %s |\n", i - 16, outbuf);
984 umoven_or_printaddr(struct tcb *const tcp, const kernel_ulong_t addr,
985 const unsigned int len, void *const our_addr)
987 if (!addr || !verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
988 umoven(tcp, addr, len, our_addr) < 0) {
996 umoven_or_printaddr_ignore_syserror(struct tcb *const tcp,
997 const kernel_ulong_t addr,
998 const unsigned int len,
999 void *const our_addr)
1001 if (!addr || !verbose(tcp) || umoven(tcp, addr, len, our_addr) < 0) {
1009 * Iteratively fetch and print up to nmemb elements of elem_size size
1010 * from the array that starts at tracee's address start_addr.
1012 * Array elements are being fetched to the address specified by elem_buf.
1014 * The fetcher callback function specified by umoven_func should follow
1015 * the same semantics as umoven_or_printaddr function.
1017 * The printer callback function specified by print_func is expected
1018 * to print something; if it returns false, no more iterations will be made.
1020 * The pointer specified by opaque_data is passed to each invocation
1021 * of print_func callback function.
1023 * This function prints:
1024 * - "NULL", if start_addr is NULL;
1025 * - "[]", if nmemb is 0;
1026 * - start_addr, if nmemb * elem_size overflows or wraps around;
1027 * - nothing, if the first element cannot be fetched
1028 * (if umoven_func returns non-zero), but it is assumed that
1029 * umoven_func has printed the address it failed to fetch data from;
1030 * - elements of the array, delimited by ", ", with the array itself
1031 * enclosed with [] brackets.
1033 * If abbrev(tcp) is true, then
1034 * - the maximum number of elements printed equals to max_strlen;
1035 * - "..." is printed instead of max_strlen+1 element
1036 * and no more iterations will be made.
1038 * This function returns true only if
1039 * - umoven_func has been called at least once AND
1040 * - umoven_func has not returned false.
1043 print_array(struct tcb *const tcp,
1044 const kernel_ulong_t start_addr,
1046 void *const elem_buf,
1047 const size_t elem_size,
1048 int (*const umoven_func)(struct tcb *,
1052 bool (*const print_func)(struct tcb *,
1056 void *const opaque_data)
1068 const size_t size = nmemb * elem_size;
1069 const kernel_ulong_t end_addr = start_addr + size;
1071 if (end_addr <= start_addr || size / elem_size != nmemb) {
1072 printaddr(start_addr);
1076 const kernel_ulong_t abbrev_end =
1077 (abbrev(tcp) && max_strlen < nmemb) ?
1078 start_addr + elem_size * max_strlen : end_addr;
1081 for (cur = start_addr; cur < end_addr; cur += elem_size) {
1082 if (cur != start_addr)
1085 if (umoven_func(tcp, cur, elem_size, elem_buf))
1088 if (cur == start_addr)
1091 if (cur >= abbrev_end) {
1097 if (!print_func(tcp, elem_buf, elem_size, opaque_data)) {
1102 if (cur != start_addr)
1105 return cur >= end_addr;
1109 printargs(struct tcb *tcp)
1111 const int n = tcp->s_ent->nargs;
1113 for (i = 0; i < n; ++i)
1114 tprintf("%s%#" PRI_klx, i ? ", " : "", tcp->u_arg[i]);
1115 return RVAL_DECODED;
1119 printargs_u(struct tcb *tcp)
1121 const int n = tcp->s_ent->nargs;
1123 for (i = 0; i < n; ++i)
1124 tprintf("%s%u", i ? ", " : "",
1125 (unsigned int) tcp->u_arg[i]);
1126 return RVAL_DECODED;
1130 printargs_d(struct tcb *tcp)
1132 const int n = tcp->s_ent->nargs;
1134 for (i = 0; i < n; ++i)
1135 tprintf("%s%d", i ? ", " : "",
1136 (int) tcp->u_arg[i]);
1137 return RVAL_DECODED;
1140 /* Print abnormal high bits of a kernel_ulong_t value. */
1142 print_abnormal_hi(const kernel_ulong_t val)
1144 if (current_klongsize > 4) {
1145 const unsigned int hi = (unsigned int) ((uint64_t) val >> 32);
1147 tprintf("%#x<<32|", hi);
1151 #if defined _LARGEFILE64_SOURCE && defined HAVE_OPEN64
1152 # define open_file open64
1154 # define open_file open
1158 read_int_from_file(struct tcb *tcp, const char *const fname, int *const pvalue)
1160 const int fd = open_file(fname, O_RDONLY);
1165 char buf[sizeof(lval) * 3];
1166 int n = read(fd, buf, sizeof(buf) - 1);
1167 int saved_errno = errno;
1171 errno = saved_errno;
1178 lval = strtol(buf, &endptr, 10);
1179 if (!endptr || (*endptr && '\n' != *endptr)
1180 #if INT_MAX < LONG_MAX
1181 || lval > INT_MAX || lval < INT_MIN
1183 || ERANGE == errno) {
1189 *pvalue = (int) lval;