2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/param.h>
38 #ifdef HAVE_SYS_XATTR_H
39 # include <sys/xattr.h>
47 string_to_uint_ex(const char *const str, char **const endptr,
48 const unsigned int max_val, const char *const accepted_ending)
57 val = strtol(str, &end, 10);
59 if (str == end || val < 0 || (unsigned long) val > max_val
60 || (val == LONG_MAX && errno == ERANGE))
63 if (*end && (!accepted_ending || !strchr(accepted_ending, *end)))
73 string_to_uint(const char *const str)
75 return string_to_uint_upto(str, INT_MAX);
79 tv_nz(const struct timeval *a)
81 return a->tv_sec || a->tv_usec;
85 tv_cmp(const struct timeval *a, const struct timeval *b)
87 if (a->tv_sec < b->tv_sec
88 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
90 if (a->tv_sec > b->tv_sec
91 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
97 tv_float(const struct timeval *tv)
99 return tv->tv_sec + tv->tv_usec/1000000.0;
103 tv_add(struct timeval *tv, const struct timeval *a, const struct timeval *b)
105 tv->tv_sec = a->tv_sec + b->tv_sec;
106 tv->tv_usec = a->tv_usec + b->tv_usec;
107 if (tv->tv_usec >= 1000000) {
109 tv->tv_usec -= 1000000;
114 tv_sub(struct timeval *tv, const struct timeval *a, const struct timeval *b)
116 tv->tv_sec = a->tv_sec - b->tv_sec;
117 tv->tv_usec = a->tv_usec - b->tv_usec;
118 if (((long) tv->tv_usec) < 0) {
120 tv->tv_usec += 1000000;
125 tv_div(struct timeval *tv, const struct timeval *a, int n)
127 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
128 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
129 tv->tv_usec %= 1000000;
133 tv_mul(struct timeval *tv, const struct timeval *a, int n)
135 tv->tv_usec = a->tv_usec * n;
136 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
137 tv->tv_usec %= 1000000;
141 xlookup(const struct xlat *xlat, const uint64_t val)
143 for (; xlat->str != NULL; xlat++)
144 if (xlat->val == val)
150 xlat_bsearch_compare(const void *a, const void *b)
152 const uint64_t val1 = *(const uint64_t *) a;
153 const uint64_t val2 = ((const struct xlat *) b)->val;
154 return (val1 > val2) ? 1 : (val1 < val2) ? -1 : 0;
158 xlat_search(const struct xlat *xlat, const size_t nmemb, const uint64_t val)
160 const struct xlat *e =
161 bsearch((const void*) &val,
162 xlat, nmemb, sizeof(*xlat), xlat_bsearch_compare);
164 return e ? e->str : NULL;
167 #if !defined HAVE_STPCPY
169 stpcpy(char *dst, const char *src)
171 while ((*dst = *src++) != '\0')
177 /* Find a next bit which is set.
178 * Starts testing at cur_bit.
179 * Returns -1 if no more bits are set.
181 * We never touch bytes we don't need to.
182 * On big-endian, array is assumed to consist of
183 * current_wordsize wide words: for example, is current_wordsize is 4,
184 * the bytes are walked in 3,2,1,0, 7,6,5,4, 11,10,9,8 ... sequence.
185 * On little-endian machines, word size is immaterial.
188 next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits)
190 const unsigned endian = 1;
191 int little_endian = * (char *) (void *) &endian;
193 const uint8_t *array = bit_array;
194 unsigned pos = cur_bit / 8;
195 unsigned pos_xor_mask = little_endian ? 0 : current_wordsize-1;
201 if (cur_bit >= size_bits)
203 cur_byte = array[pos ^ pos_xor_mask];
205 cur_bit = (cur_bit + 8) & (-8);
209 bitmask = 1 << (cur_bit & 7);
211 if (cur_byte & bitmask)
214 if (cur_bit >= size_bits)
217 /* This check *can't be* optimized out: */
226 * Print entry in struct xlat table, if there.
228 * @param val Value to search a literal representation for.
229 * @param dflt String (abbreviated in comment syntax) which should be emitted
230 * if no appropriate xlat value has been found.
231 * @param xlat (And the following arguments) Pointers to arrays of xlat values.
232 * The last argument should be NULL.
233 * @return 1 if appropriate xlat value has been found, 0 otherwise.
236 printxvals(const uint64_t val, const char *dflt, const struct xlat *xlat, ...)
240 va_start(args, xlat);
241 for (; xlat; xlat = va_arg(args, const struct xlat *)) {
242 const char *str = xlookup(xlat, val);
250 /* No hits -- print raw # instead. */
251 tprintf("%#" PRIx64, val);
253 tprintf(" /* %s */", dflt);
261 * Print entry in sorted struct xlat table, if it is there.
263 * @param xlat Pointer to an array of xlat values (not terminated with
265 * @param xlat_size Number of xlat elements present in array (usually ARRAY_SIZE
266 * if array is declared in the unit's scope and not
267 * terminated with XLAT_END).
268 * @param val Value to search literal representation for.
269 * @param dflt String (abbreviated in comment syntax) which should be
270 * emitted if no appropriate xlat value has been found.
271 * @return 1 if appropriate xlat value has been found, 0
275 printxval_searchn(const struct xlat *xlat, size_t xlat_size, uint64_t val,
278 const char *s = xlat_search(xlat, xlat_size, val);
285 tprintf("%#" PRIx64, val);
287 tprintf(" /* %s */", dflt);
293 * Fetch 64bit argument at position arg_no and
294 * return the index of the next argument.
297 getllval(struct tcb *tcp, unsigned long long *val, int arg_no)
299 #if SIZEOF_KERNEL_LONG_T > 4
300 # ifndef current_klongsize
301 if (current_klongsize < SIZEOF_KERNEL_LONG_T) {
302 # if defined(AARCH64) || defined(POWERPC64)
303 /* Align arg_no to the next even number. */
304 arg_no = (arg_no + 1) & 0xe;
305 # endif /* AARCH64 || POWERPC64 */
306 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
309 # endif /* !current_klongsize */
311 *val = tcp->u_arg[arg_no];
314 #else /* SIZEOF_KERNEL_LONG_T == 4 */
315 # if defined __ARM_EABI__ || \
316 defined LINUX_MIPSO32 || \
319 /* Align arg_no to the next even number. */
320 arg_no = (arg_no + 1) & 0xe;
323 * The SH4 ABI does allow long longs in odd-numbered registers, but
324 * does not allow them to be split between registers and memory - and
325 * there are only four argument registers for normal functions. As a
326 * result, pread, for example, takes an extra padding argument before
327 * the offset. This was changed late in the 2.4 series (around 2.4.20).
331 # endif /* __ARM_EABI__ || LINUX_MIPSO32 || POWERPC || XTENSA || SH */
332 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
340 * Print 64bit argument at position arg_no and
341 * return the index of the next argument.
344 printllval(struct tcb *tcp, const char *format, int arg_no)
346 unsigned long long val = 0;
348 arg_no = getllval(tcp, &val, arg_no);
349 tprintf(format, val);
354 * Interpret `xlat' as an array of flags
355 * print the entries whose bits are on in `flags'
356 * return # of flags printed.
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);
441 tprintf(" /* %s */", dflt);
452 printaddr(const kernel_ulong_t addr)
457 tprintf("%#" PRI_klx, addr);
460 #define DEF_PRINTNUM(name, type) \
462 printnum_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
463 const char *const fmt) \
466 if (umove_or_printaddr(tcp, addr, &num)) \
474 #define DEF_PRINTNUM_ADDR(name, type) \
476 printnum_addr_ ## name(struct tcb *tcp, const kernel_ulong_t addr) \
479 if (umove_or_printaddr(tcp, addr, &num)) \
487 #define DEF_PRINTPAIR(name, type) \
489 printpair_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
490 const char *const fmt) \
493 if (umove_or_printaddr(tcp, addr, &pair)) \
496 tprintf(fmt, pair[0]); \
498 tprintf(fmt, pair[1]); \
503 DEF_PRINTNUM(int, int)
504 DEF_PRINTNUM_ADDR(int, unsigned int)
505 DEF_PRINTPAIR(int, int)
506 DEF_PRINTNUM(short, short)
507 DEF_PRINTNUM(int64, uint64_t)
508 DEF_PRINTNUM_ADDR(int64, uint64_t)
509 DEF_PRINTPAIR(int64, uint64_t)
511 #ifndef current_wordsize
513 printnum_long_int(struct tcb *const tcp, const kernel_ulong_t addr,
514 const char *const fmt_long, const char *const fmt_int)
516 if (current_wordsize > sizeof(int)) {
517 return printnum_int64(tcp, addr, fmt_long);
519 return printnum_int(tcp, addr, fmt_int);
524 printnum_addr_long_int(struct tcb *tcp, const kernel_ulong_t addr)
526 if (current_wordsize > sizeof(int)) {
527 return printnum_addr_int64(tcp, addr);
529 return printnum_addr_int(tcp, addr);
532 #endif /* !current_wordsize */
534 #ifndef current_klongsize
536 printnum_addr_klong_int(struct tcb *tcp, const kernel_ulong_t addr)
538 if (current_klongsize > sizeof(int)) {
539 return printnum_addr_int64(tcp, addr);
541 return printnum_addr_int(tcp, addr);
544 #endif /* !current_klongsize */
550 static char buf[sizeof(int) * 3 * 6 + sizeof("+0000")];
558 strftime(buf, sizeof(buf), "%FT%T%z", tmp);
560 snprintf(buf, sizeof(buf), "%lu", (unsigned long) t);
566 getfdproto(struct tcb *tcp, int fd)
568 #ifdef HAVE_SYS_XATTR_H
569 size_t bufsize = 256;
572 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
575 return SOCK_PROTO_UNKNOWN;
577 sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
578 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
580 return SOCK_PROTO_UNKNOWN;
583 * This is a protection for the case when the kernel
584 * side does not append a null byte to the buffer.
588 return get_proto_by_name(buf);
591 return SOCK_PROTO_UNKNOWN;
596 printfd(struct tcb *tcp, int fd)
598 char path[PATH_MAX + 1];
599 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
600 static const char socket_prefix[] = "socket:[";
601 const size_t socket_prefix_len = sizeof(socket_prefix) - 1;
602 const size_t path_len = strlen(path);
605 if (show_fd_path > 1 &&
606 strncmp(path, socket_prefix, socket_prefix_len) == 0 &&
607 path[path_len - 1] == ']') {
608 unsigned long inode =
609 strtoul(path + socket_prefix_len, NULL, 10);
611 if (!print_sockaddr_by_inode_cached(inode)) {
612 const enum sock_proto proto =
614 if (!print_sockaddr_by_inode(inode, proto))
618 print_quoted_string(path, path_len,
619 QUOTE_OMIT_LEADING_TRAILING_QUOTES);
627 * Quote string `instr' of length `size'
628 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
630 * If QUOTE_0_TERMINATED `style' flag is set,
631 * treat `instr' as a NUL-terminated string,
632 * checking up to (`size' + 1) bytes of `instr'.
634 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
635 * do not add leading and trailing quoting symbols.
637 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
638 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
641 string_quote(const char *instr, char *outstr, const unsigned int size,
642 const unsigned int style)
644 const unsigned char *ustr = (const unsigned char *) instr;
649 if (style & QUOTE_0_TERMINATED)
652 eol = 0x100; /* this can never match a char */
658 /* Check for presence of symbol which require
659 to hex-quote the whole string. */
660 for (i = 0; i < size; ++i) {
662 /* Check for NUL-terminated string. */
666 /* Force hex unless c is printable or whitespace */
671 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
672 * They happen to have ASCII codes 9,10,11,12,13.
674 if (c < ' ' && (unsigned)(c - 9) >= 5) {
681 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
685 /* Hex-quote the whole string. */
686 for (i = 0; i < size; ++i) {
688 /* Check for NUL-terminated string. */
693 *s++ = "0123456789abcdef"[c >> 4];
694 *s++ = "0123456789abcdef"[c & 0xf];
697 for (i = 0; i < size; ++i) {
699 /* Check for NUL-terminated string. */
702 if ((i == (size - 1)) &&
703 (style & QUOTE_OMIT_TRAILING_0) && (c == '\0'))
706 case '\"': case '\\':
731 if (c >= ' ' && c <= 0x7e)
737 && ustr[i + 1] >= '0'
738 && ustr[i + 1] <= '9'
741 *s++ = '0' + (c >> 6);
742 *s++ = '0' + ((c >> 3) & 0x7);
747 *s++ = '0' + (c >> 6);
748 *s++ = '0' + ((c >> 3) & 0x7);
751 *s++ = '0' + (c & 0x7);
758 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
762 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
763 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
764 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
765 * but next char is NUL.
773 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
776 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
780 #ifndef ALLOCA_CUTOFF
781 # define ALLOCA_CUTOFF 4032
783 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
786 * Quote string `str' of length `size' and print the result.
788 * If QUOTE_0_TERMINATED `style' flag is set,
789 * treat `str' as a NUL-terminated string and
790 * quote at most (`size' - 1) bytes.
792 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
793 * do not add leading and trailing quoting symbols.
795 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
796 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
799 print_quoted_string(const char *str, unsigned int size,
800 const unsigned int style)
804 unsigned int alloc_size;
807 if (size && style & QUOTE_0_TERMINATED)
810 alloc_size = 4 * size;
811 if (alloc_size / 4 != size) {
812 error_msg("Out of memory");
816 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2);
818 if (use_alloca(alloc_size)) {
819 outstr = alloca(alloc_size);
822 outstr = buf = malloc(alloc_size);
824 error_msg("Out of memory");
830 rc = string_quote(str, outstr, size, style);
838 * Print path string specified by address `addr' and length `n'.
839 * If path length exceeds `n', append `...' to the output.
842 printpathn(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int n)
844 char path[PATH_MAX + 1];
852 /* Cap path length to the path buffer size */
853 if (n > sizeof path - 1)
856 /* Fetch one byte more to find out whether path length > n. */
857 nul_seen = umovestr(tcp, addr, n + 1, path);
862 print_quoted_string(path, n, QUOTE_0_TERMINATED);
869 printpath(struct tcb *const tcp, const kernel_ulong_t addr)
871 /* Size must correspond to char path[] size in printpathn */
872 printpathn(tcp, addr, PATH_MAX);
876 * Print string specified by address `addr' and length `len'.
877 * If `user_style' has QUOTE_0_TERMINATED bit set, treat the string
878 * as a NUL-terminated string.
879 * Pass `user_style' on to `string_quote'.
880 * Append `...' to the output if either the string length exceeds `max_strlen',
881 * or QUOTE_0_TERMINATED bit is set and the string length exceeds `len'.
884 printstr_ex(struct tcb *const tcp, const kernel_ulong_t addr,
885 const kernel_ulong_t len, const unsigned int user_style)
887 static char *str = NULL;
890 unsigned int style = user_style;
898 /* Allocate static buffers if they are not allocated yet. */
900 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
902 if (outstr_size / 4 != max_strlen)
904 str = xmalloc(max_strlen + 1);
905 outstr = xmalloc(outstr_size);
908 /* Fetch one byte more because string_quote may look one byte ahead. */
909 size = max_strlen + 1;
913 if (style & QUOTE_0_TERMINATED)
914 rc = umovestr(tcp, addr, size, str);
916 rc = umoven(tcp, addr, size, str);
923 if (size > max_strlen)
928 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
929 * or we were requested to print more than -s NUM chars)...
931 ellipsis = string_quote(str, outstr, size, style)
933 && ((style & QUOTE_0_TERMINATED)
934 || len > max_strlen);
942 dumpiov_upto(struct tcb *const tcp, const int len, const kernel_ulong_t addr,
943 kernel_ulong_t data_size)
945 #if SUPPORTED_PERSONALITIES > 1
947 struct { uint32_t base; uint32_t len; } *iov32;
948 struct { uint64_t base; uint64_t len; } *iov64;
950 #define iov iovu.iov64
952 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
953 #define iov_iov_base(i) \
954 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
955 #define iov_iov_len(i) \
956 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
959 #define sizeof_iov sizeof(*iov)
960 #define iov_iov_base(i) ptr_to_kulong(iov[i].iov_base)
961 #define iov_iov_len(i) iov[i].iov_len
966 size = sizeof_iov * len;
967 /* Assuming no sane program has millions of iovs */
968 if ((unsigned)len > 1024*1024 /* insane or negative size? */
969 || (iov = malloc(size)) == NULL) {
970 error_msg("Out of memory");
973 if (umoven(tcp, addr, size, iov) >= 0) {
974 for (i = 0; i < len; i++) {
975 kernel_ulong_t iov_len = iov_iov_len(i);
976 if (iov_len > data_size)
980 data_size -= iov_len;
981 /* include the buffer number to make it easy to
982 * match up the trace with the source */
983 tprintf(" * %" PRI_klu " bytes in buffer %d\n", iov_len, i);
984 dumpstr(tcp, iov_iov_base(i), iov_len);
995 dumpstr(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
997 static int strsize = -1;
998 static unsigned char *str;
1003 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
1004 "1234567890123456") + /*in case I'm off by few:*/ 4)
1005 /*align to 8 to make memset easier:*/ + 7) & -8
1007 const unsigned char *src;
1010 memset(outbuf, ' ', sizeof(outbuf));
1012 if (strsize < len + 16) {
1014 str = malloc(len + 16);
1017 error_msg("Out of memory");
1023 if (umoven(tcp, addr, len, str) < 0)
1026 /* Space-pad to 16 bytes */
1038 *dst++ = "0123456789abcdef"[*src >> 4];
1039 *dst++ = "0123456789abcdef"[*src & 0xf];
1045 dst++; /* space is there by memset */
1048 dst++; /* space is there by memset */
1055 if (*src >= ' ' && *src < 0x7f)
1060 } while (++i & 0xf);
1062 tprintf(" | %05x %s |\n", i - 16, outbuf);
1066 #ifdef HAVE_PROCESS_VM_READV
1067 /* C library supports this, but the kernel might not. */
1068 static bool process_vm_readv_not_supported = 0;
1071 /* Need to do this since process_vm_readv() is not yet available in libc.
1072 * When libc is be updated, only "static bool process_vm_readv_not_supported"
1073 * line should remain.
1075 #if !defined(__NR_process_vm_readv)
1077 # define __NR_process_vm_readv 347
1078 # elif defined(X86_64)
1079 # define __NR_process_vm_readv 310
1080 # elif defined(POWERPC)
1081 # define __NR_process_vm_readv 351
1085 #if defined(__NR_process_vm_readv)
1086 static bool process_vm_readv_not_supported = 0;
1087 /* Have to avoid duplicating with the C library headers. */
1088 static ssize_t strace_process_vm_readv(pid_t pid,
1089 const struct iovec *lvec,
1090 unsigned long liovcnt,
1091 const struct iovec *rvec,
1092 unsigned long riovcnt,
1093 unsigned long flags)
1095 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
1097 #define process_vm_readv strace_process_vm_readv
1099 static bool process_vm_readv_not_supported = 1;
1100 # define process_vm_readv(...) (errno = ENOSYS, -1)
1103 #endif /* end of hack */
1106 vm_read_mem(const pid_t pid, void *const laddr,
1107 const kernel_ulong_t raddr, const size_t len)
1109 const unsigned long truncated_raddr = raddr;
1111 if (raddr != (kernel_ulong_t) truncated_raddr) {
1116 const struct iovec local = {
1120 const struct iovec remote = {
1121 .iov_base = (void *) truncated_raddr,
1125 return process_vm_readv(pid, &local, 1, &remote, 1, 0);
1129 * move `len' bytes of data from process `pid'
1130 * at address `addr' to our space at `our_addr'
1133 umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
1134 void *const our_addr)
1136 char *laddr = our_addr;
1138 unsigned int n, m, nread;
1141 char x[sizeof(long)];
1144 #if SIZEOF_KERNEL_LONG_T > 4 \
1145 && (SIZEOF_LONG < SIZEOF_KERNEL_LONG_T || !defined current_wordsize)
1146 if (current_wordsize < sizeof(addr)
1147 && (addr & (~ (kernel_ulong_t) -1U))) {
1152 if (!process_vm_readv_not_supported) {
1153 int r = vm_read_mem(pid, laddr, addr, len);
1154 if ((unsigned int) r == len)
1157 error_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
1158 (unsigned int) r, len, addr);
1163 process_vm_readv_not_supported = 1;
1166 /* operation not permitted, try PTRACE_PEEKDATA */
1169 /* the process is gone */
1171 case EFAULT: case EIO:
1172 /* address space is inaccessible */
1175 /* all the rest is strange and should be reported */
1176 perror_msg("process_vm_readv");
1182 if (addr & (sizeof(long) - 1)) {
1183 /* addr not a multiple of sizeof(long) */
1184 n = addr & (sizeof(long) - 1); /* residue */
1185 addr &= -sizeof(long); /* aligned address */
1187 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1191 case ESRCH: case EINVAL:
1192 /* these could be seen if the process is gone */
1194 case EFAULT: case EIO: case EPERM:
1195 /* address space is inaccessible */
1198 /* all the rest is strange and should be reported */
1199 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1203 m = MIN(sizeof(long) - n, len);
1204 memcpy(laddr, &u.x[n], m);
1205 addr += sizeof(long);
1212 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1216 case ESRCH: case EINVAL:
1217 /* these could be seen if the process is gone */
1219 case EFAULT: case EIO: case EPERM:
1220 /* address space is inaccessible */
1222 perror_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
1223 nread, nread + len, addr - nread);
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), len);
1233 memcpy(laddr, u.x, m);
1234 addr += sizeof(long);
1244 umoven_or_printaddr(struct tcb *const tcp, const kernel_ulong_t addr,
1245 const unsigned int len, void *const our_addr)
1247 if (!addr || !verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
1248 umoven(tcp, addr, len, our_addr) < 0) {
1256 umoven_or_printaddr_ignore_syserror(struct tcb *const tcp,
1257 const kernel_ulong_t addr,
1258 const unsigned int len,
1259 void *const our_addr)
1261 if (!addr || !verbose(tcp) || umoven(tcp, addr, len, our_addr) < 0) {
1269 * Like `umove' but make the additional effort of looking
1270 * for a terminating zero byte.
1272 * Returns < 0 on error, > 0 if NUL was seen,
1273 * (TODO if useful: return count of bytes including NUL),
1274 * else 0 if len bytes were read but no NUL byte seen.
1276 * Note: there is no guarantee we won't overwrite some bytes
1277 * in laddr[] _after_ terminating NUL (but, of course,
1278 * we never write past laddr[len-1]).
1281 umovestr(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len, char *laddr)
1283 const unsigned long x01010101 = (unsigned long) 0x0101010101010101ULL;
1284 const unsigned long x80808080 = (unsigned long) 0x8080808080808080ULL;
1287 unsigned int n, m, nread;
1290 char x[sizeof(long)];
1293 #if SIZEOF_KERNEL_LONG_T > 4 \
1294 && (SIZEOF_LONG < SIZEOF_KERNEL_LONG_T || !defined current_wordsize)
1295 if (current_wordsize < sizeof(addr)
1296 && (addr & (~ (kernel_ulong_t) -1U))) {
1302 if (!process_vm_readv_not_supported) {
1303 const size_t page_size = get_pagesize();
1304 const size_t page_mask = page_size - 1;
1307 unsigned int chunk_len;
1308 unsigned int end_in_page;
1311 * Don't cross pages, otherwise we can get EFAULT
1312 * and fail to notice that terminating NUL lies
1313 * in the existing (first) page.
1315 chunk_len = len > page_size ? page_size : len;
1316 end_in_page = (addr + chunk_len) & page_mask;
1317 if (chunk_len > end_in_page) /* crosses to the next page */
1318 chunk_len -= end_in_page;
1320 int r = vm_read_mem(pid, laddr, addr, chunk_len);
1322 if (memchr(laddr, '\0', r))
1332 process_vm_readv_not_supported = 1;
1333 goto vm_readv_didnt_work;
1335 /* the process is gone */
1338 /* operation not permitted, try PTRACE_PEEKDATA */
1340 goto vm_readv_didnt_work;
1342 case EFAULT: case EIO:
1343 /* address space is inaccessible */
1345 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
1346 nread, nread + len, addr - nread);
1350 /* all the rest is strange and should be reported */
1351 perror_msg("process_vm_readv");
1357 vm_readv_didnt_work:
1359 if (addr & (sizeof(long) - 1)) {
1360 /* addr not a multiple of sizeof(long) */
1361 n = addr & (sizeof(long) - 1); /* residue */
1362 addr &= -sizeof(long); /* aligned address */
1364 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1368 case ESRCH: case EINVAL:
1369 /* these could be seen if the process is gone */
1371 case EFAULT: case EIO: case EPERM:
1372 /* address space is inaccessible */
1375 /* all the rest is strange and should be reported */
1376 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1380 m = MIN(sizeof(long) - n, len);
1381 memcpy(laddr, &u.x[n], m);
1382 while (n & (sizeof(long) - 1))
1383 if (u.x[n++] == '\0')
1385 addr += sizeof(long);
1393 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1397 case ESRCH: case EINVAL:
1398 /* these could be seen if the process is gone */
1400 case EFAULT: case EIO: case EPERM:
1401 /* address space is inaccessible */
1403 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
1404 nread, nread + len, addr - nread);
1408 /* all the rest is strange and should be reported */
1409 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1413 m = MIN(sizeof(long), len);
1414 memcpy(laddr, u.x, m);
1415 /* "If a NUL char exists in this word" */
1416 if ((u.val - x01010101) & ~u.val & x80808080)
1418 addr += sizeof(long);
1427 * Iteratively fetch and print up to nmemb elements of elem_size size
1428 * from the array that starts at tracee's address start_addr.
1430 * Array elements are being fetched to the address specified by elem_buf.
1432 * The fetcher callback function specified by umoven_func should follow
1433 * the same semantics as umoven_or_printaddr function.
1435 * The printer callback function specified by print_func is expected
1436 * to print something; if it returns false, no more iterations will be made.
1438 * The pointer specified by opaque_data is passed to each invocation
1439 * of print_func callback function.
1441 * This function prints:
1442 * - "NULL", if start_addr is NULL;
1443 * - "[]", if nmemb is 0;
1444 * - start_addr, if nmemb * elem_size overflows or wraps around;
1445 * - nothing, if the first element cannot be fetched
1446 * (if umoven_func returns non-zero), but it is assumed that
1447 * umoven_func has printed the address it failed to fetch data from;
1448 * - elements of the array, delimited by ", ", with the array itself
1449 * enclosed with [] brackets.
1451 * If abbrev(tcp) is true, then
1452 * - the maximum number of elements printed equals to max_strlen;
1453 * - "..." is printed instead of max_strlen+1 element
1454 * and no more iterations will be made.
1456 * This function returns true only if
1457 * - umoven_func has been called at least once AND
1458 * - umoven_func has not returned false.
1461 print_array(struct tcb *const tcp,
1462 const kernel_ulong_t start_addr,
1464 void *const elem_buf,
1465 const size_t elem_size,
1466 int (*const umoven_func)(struct tcb *,
1470 bool (*const print_func)(struct tcb *,
1474 void *const opaque_data)
1486 const size_t size = nmemb * elem_size;
1487 const kernel_ulong_t end_addr = start_addr + size;
1489 if (end_addr <= start_addr || size / elem_size != nmemb) {
1490 printaddr(start_addr);
1494 const kernel_ulong_t abbrev_end =
1495 (abbrev(tcp) && max_strlen < nmemb) ?
1496 start_addr + elem_size * max_strlen : end_addr;
1499 for (cur = start_addr; cur < end_addr; cur += elem_size) {
1500 if (cur != start_addr)
1503 if (umoven_func(tcp, cur, elem_size, elem_buf))
1506 if (cur == start_addr)
1509 if (cur >= abbrev_end) {
1515 if (!print_func(tcp, elem_buf, elem_size, opaque_data)) {
1520 if (cur != start_addr)
1523 return cur >= end_addr;
1527 printargs(struct tcb *tcp)
1529 const int n = tcp->s_ent->nargs;
1531 for (i = 0; i < n; ++i)
1532 tprintf("%s%#" PRI_klx, i ? ", " : "", tcp->u_arg[i]);
1533 return RVAL_DECODED;
1537 printargs_u(struct tcb *tcp)
1539 const int n = tcp->s_ent->nargs;
1541 for (i = 0; i < n; ++i)
1542 tprintf("%s%u", i ? ", " : "",
1543 (unsigned int) tcp->u_arg[i]);
1544 return RVAL_DECODED;
1548 printargs_d(struct tcb *tcp)
1550 const int n = tcp->s_ent->nargs;
1552 for (i = 0; i < n; ++i)
1553 tprintf("%s%d", i ? ", " : "",
1554 (int) tcp->u_arg[i]);
1555 return RVAL_DECODED;
1558 #if defined _LARGEFILE64_SOURCE && defined HAVE_OPEN64
1559 # define open_file open64
1561 # define open_file open
1565 read_int_from_file(const char *const fname, int *const pvalue)
1567 const int fd = open_file(fname, O_RDONLY);
1572 char buf[sizeof(lval) * 3];
1573 int n = read(fd, buf, sizeof(buf) - 1);
1574 int saved_errno = errno;
1578 errno = saved_errno;
1585 lval = strtol(buf, &endptr, 10);
1586 if (!endptr || (*endptr && '\n' != *endptr)
1587 #if INT_MAX < LONG_MAX
1588 || lval > INT_MAX || lval < INT_MIN
1590 || ERANGE == errno) {
1596 *pvalue = (int) lval;