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>
42 #include <asm/unistd.h>
49 string_to_uint_ex(const char *const str, char **const endptr,
50 const unsigned int max_val, const char *const accepted_ending)
59 val = strtol(str, &end, 10);
61 if (str == end || val < 0 || (unsigned long) val > max_val
62 || (val == LONG_MAX && errno == ERANGE))
65 if (*end && (!accepted_ending || !strchr(accepted_ending, *end)))
75 string_to_uint(const char *const str)
77 return string_to_uint_upto(str, INT_MAX);
81 tv_nz(const struct timeval *a)
83 return a->tv_sec || a->tv_usec;
87 tv_cmp(const struct timeval *a, const struct timeval *b)
89 if (a->tv_sec < b->tv_sec
90 || (a->tv_sec == b->tv_sec && a->tv_usec < b->tv_usec))
92 if (a->tv_sec > b->tv_sec
93 || (a->tv_sec == b->tv_sec && a->tv_usec > b->tv_usec))
99 tv_float(const struct timeval *tv)
101 return tv->tv_sec + tv->tv_usec/1000000.0;
105 tv_add(struct timeval *tv, const struct timeval *a, const struct timeval *b)
107 tv->tv_sec = a->tv_sec + b->tv_sec;
108 tv->tv_usec = a->tv_usec + b->tv_usec;
109 if (tv->tv_usec >= 1000000) {
111 tv->tv_usec -= 1000000;
116 tv_sub(struct timeval *tv, const struct timeval *a, const struct timeval *b)
118 tv->tv_sec = a->tv_sec - b->tv_sec;
119 tv->tv_usec = a->tv_usec - b->tv_usec;
120 if (((long) tv->tv_usec) < 0) {
122 tv->tv_usec += 1000000;
127 tv_div(struct timeval *tv, const struct timeval *a, int n)
129 tv->tv_usec = (a->tv_sec % n * 1000000 + a->tv_usec + n / 2) / n;
130 tv->tv_sec = a->tv_sec / n + tv->tv_usec / 1000000;
131 tv->tv_usec %= 1000000;
135 tv_mul(struct timeval *tv, const struct timeval *a, int n)
137 tv->tv_usec = a->tv_usec * n;
138 tv->tv_sec = a->tv_sec * n + tv->tv_usec / 1000000;
139 tv->tv_usec %= 1000000;
143 xlookup(const struct xlat *xlat, const uint64_t val)
145 for (; xlat->str != NULL; xlat++)
146 if (xlat->val == val)
152 xlat_bsearch_compare(const void *a, const void *b)
154 const uint64_t val1 = *(const uint64_t *) a;
155 const uint64_t val2 = ((const struct xlat *) b)->val;
156 return (val1 > val2) ? 1 : (val1 < val2) ? -1 : 0;
160 xlat_search(const struct xlat *xlat, const size_t nmemb, const uint64_t val)
162 const struct xlat *e =
163 bsearch((const void*) &val,
164 xlat, nmemb, sizeof(*xlat), xlat_bsearch_compare);
166 return e ? e->str : NULL;
169 #if !defined HAVE_STPCPY
171 stpcpy(char *dst, const char *src)
173 while ((*dst = *src++) != '\0')
179 /* Find a next bit which is set.
180 * Starts testing at cur_bit.
181 * Returns -1 if no more bits are set.
183 * We never touch bytes we don't need to.
184 * On big-endian, array is assumed to consist of
185 * current_wordsize wide words: for example, is current_wordsize is 4,
186 * the bytes are walked in 3,2,1,0, 7,6,5,4, 11,10,9,8 ... sequence.
187 * On little-endian machines, word size is immaterial.
190 next_set_bit(const void *bit_array, unsigned cur_bit, unsigned size_bits)
192 const unsigned endian = 1;
193 int little_endian = * (char *) (void *) &endian;
195 const uint8_t *array = bit_array;
196 unsigned pos = cur_bit / 8;
197 unsigned pos_xor_mask = little_endian ? 0 : current_wordsize-1;
203 if (cur_bit >= size_bits)
205 cur_byte = array[pos ^ pos_xor_mask];
207 cur_bit = (cur_bit + 8) & (-8);
211 bitmask = 1 << (cur_bit & 7);
213 if (cur_byte & bitmask)
216 if (cur_bit >= size_bits)
219 /* This check *can't be* optimized out: */
228 * Print entry in struct xlat table, if there.
230 * @param val Value to search a literal representation for.
231 * @param dflt String (abbreviated in comment syntax) which should be emitted
232 * if no appropriate xlat value has been found.
233 * @param xlat (And the following arguments) Pointers to arrays of xlat values.
234 * The last argument should be NULL.
235 * @return 1 if appropriate xlat value has been found, 0 otherwise.
238 printxvals(const uint64_t val, const char *dflt, const struct xlat *xlat, ...)
242 va_start(args, xlat);
243 for (; xlat; xlat = va_arg(args, const struct xlat *)) {
244 const char *str = xlookup(xlat, val);
252 /* No hits -- print raw # instead. */
253 tprintf("%#" PRIx64, val);
254 tprints_comment(dflt);
262 * Print entry in sorted struct xlat table, if it is there.
264 * @param xlat Pointer to an array of xlat values (not terminated with
266 * @param xlat_size Number of xlat elements present in array (usually ARRAY_SIZE
267 * if array is declared in the unit's scope and not
268 * terminated with XLAT_END).
269 * @param val Value to search literal representation for.
270 * @param dflt String (abbreviated in comment syntax) which should be
271 * emitted if no appropriate xlat value has been found.
272 * @return 1 if appropriate xlat value has been found, 0
276 printxval_searchn(const struct xlat *xlat, size_t xlat_size, uint64_t val,
279 const char *s = xlat_search(xlat, xlat_size, val);
286 tprintf("%#" PRIx64, val);
287 tprints_comment(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'
358 addflags(const struct xlat *xlat, uint64_t flags)
360 for (; xlat->str; xlat++) {
361 if (xlat->val && (flags & xlat->val) == xlat->val) {
362 tprintf("|%s", xlat->str);
367 tprintf("|%#" PRIx64, flags);
372 * Interpret `xlat' as an array of flags.
373 * Print to static string the entries whose bits are on in `flags'
374 * Return static string.
377 sprintflags(const char *prefix, const struct xlat *xlat, uint64_t flags)
379 static char outstr[1024];
383 outptr = stpcpy(outstr, prefix);
385 if (flags == 0 && xlat->val == 0 && xlat->str) {
386 strcpy(outptr, xlat->str);
390 for (; xlat->str; xlat++) {
391 if (xlat->val && (flags & xlat->val) == xlat->val) {
394 outptr = stpcpy(outptr, xlat->str);
404 outptr += sprintf(outptr, "%#" PRIx64, flags);
411 printflags64(const struct xlat *xlat, uint64_t flags, const char *dflt)
416 if (flags == 0 && xlat->val == 0 && xlat->str) {
422 for (n = 0; xlat->str; xlat++) {
423 if (xlat->val && (flags & xlat->val) == xlat->val) {
424 tprintf("%s%s", sep, xlat->str);
433 tprintf("%s%#" PRIx64, sep, flags);
438 tprintf("%#" PRIx64, flags);
439 tprints_comment(dflt);
450 printaddr(const kernel_ulong_t addr)
455 tprintf("%#" PRI_klx, addr);
458 #define DEF_PRINTNUM(name, type) \
460 printnum_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
461 const char *const fmt) \
464 if (umove_or_printaddr(tcp, addr, &num)) \
472 #define DEF_PRINTNUM_ADDR(name, type) \
474 printnum_addr_ ## name(struct tcb *tcp, const kernel_ulong_t addr) \
477 if (umove_or_printaddr(tcp, addr, &num)) \
485 #define DEF_PRINTPAIR(name, type) \
487 printpair_ ## name(struct tcb *const tcp, const kernel_ulong_t addr, \
488 const char *const fmt) \
491 if (umove_or_printaddr(tcp, addr, &pair)) \
494 tprintf(fmt, pair[0]); \
496 tprintf(fmt, pair[1]); \
501 DEF_PRINTNUM(int, int)
502 DEF_PRINTNUM_ADDR(int, unsigned int)
503 DEF_PRINTPAIR(int, int)
504 DEF_PRINTNUM(short, short)
505 DEF_PRINTNUM(int64, uint64_t)
506 DEF_PRINTNUM_ADDR(int64, uint64_t)
507 DEF_PRINTPAIR(int64, uint64_t)
509 #ifndef current_wordsize
511 printnum_long_int(struct tcb *const tcp, const kernel_ulong_t addr,
512 const char *const fmt_long, const char *const fmt_int)
514 if (current_wordsize > sizeof(int)) {
515 return printnum_int64(tcp, addr, fmt_long);
517 return printnum_int(tcp, addr, fmt_int);
522 printnum_addr_long_int(struct tcb *tcp, const kernel_ulong_t addr)
524 if (current_wordsize > sizeof(int)) {
525 return printnum_addr_int64(tcp, addr);
527 return printnum_addr_int(tcp, addr);
530 #endif /* !current_wordsize */
532 #ifndef current_klongsize
534 printnum_addr_klong_int(struct tcb *tcp, const kernel_ulong_t addr)
536 if (current_klongsize > sizeof(int)) {
537 return printnum_addr_int64(tcp, addr);
539 return printnum_addr_int(tcp, addr);
542 #endif /* !current_klongsize */
548 static char buf[sizeof(int) * 3 * 6 + sizeof("+0000")];
554 strftime(buf, sizeof(buf), "%FT%T%z", tmp);
556 snprintf(buf, sizeof(buf), "%lu", (unsigned long) t);
562 getfdproto(struct tcb *tcp, int fd)
564 #ifdef HAVE_SYS_XATTR_H
565 size_t bufsize = 256;
568 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
571 return SOCK_PROTO_UNKNOWN;
573 sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
574 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
576 return SOCK_PROTO_UNKNOWN;
579 * This is a protection for the case when the kernel
580 * side does not append a null byte to the buffer.
584 return get_proto_by_name(buf);
587 return SOCK_PROTO_UNKNOWN;
592 printfd(struct tcb *tcp, int fd)
594 char path[PATH_MAX + 1];
595 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
596 static const char socket_prefix[] = "socket:[";
597 const size_t socket_prefix_len = sizeof(socket_prefix) - 1;
598 const size_t path_len = strlen(path);
601 if (show_fd_path > 1 &&
602 strncmp(path, socket_prefix, socket_prefix_len) == 0 &&
603 path[path_len - 1] == ']') {
604 unsigned long inode =
605 strtoul(path + socket_prefix_len, NULL, 10);
607 if (!print_sockaddr_by_inode_cached(inode)) {
608 const enum sock_proto proto =
610 if (!print_sockaddr_by_inode(inode, proto))
614 print_quoted_string(path, path_len,
615 QUOTE_OMIT_LEADING_TRAILING_QUOTES);
623 * Quote string `instr' of length `size'
624 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
626 * If QUOTE_0_TERMINATED `style' flag is set,
627 * treat `instr' as a NUL-terminated string,
628 * checking up to (`size' + 1) bytes of `instr'.
630 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
631 * do not add leading and trailing quoting symbols.
633 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
634 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
637 string_quote(const char *instr, char *outstr, const unsigned int size,
638 const unsigned int style)
640 const unsigned char *ustr = (const unsigned char *) instr;
645 if (style & QUOTE_0_TERMINATED)
648 eol = 0x100; /* this can never match a char */
651 if ((xflag > 1) || (style & QUOTE_FORCE_HEX)) {
654 /* Check for presence of symbol which require
655 to hex-quote the whole string. */
656 for (i = 0; i < size; ++i) {
658 /* Check for NUL-terminated string. */
662 /* Force hex unless c is printable or whitespace */
667 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
668 * They happen to have ASCII codes 9,10,11,12,13.
670 if (c < ' ' && (unsigned)(c - 9) >= 5) {
677 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
681 /* Hex-quote the whole string. */
682 for (i = 0; i < size; ++i) {
684 /* Check for NUL-terminated string. */
689 *s++ = "0123456789abcdef"[c >> 4];
690 *s++ = "0123456789abcdef"[c & 0xf];
693 for (i = 0; i < size; ++i) {
695 /* Check for NUL-terminated string. */
698 if ((i == (size - 1)) &&
699 (style & QUOTE_OMIT_TRAILING_0) && (c == '\0'))
702 case '\"': case '\\':
727 if (c >= ' ' && c <= 0x7e)
733 && ustr[i + 1] >= '0'
734 && ustr[i + 1] <= '9'
737 *s++ = '0' + (c >> 6);
738 *s++ = '0' + ((c >> 3) & 0x7);
743 *s++ = '0' + (c >> 6);
744 *s++ = '0' + ((c >> 3) & 0x7);
747 *s++ = '0' + (c & 0x7);
754 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
758 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
759 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
760 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
761 * but next char is NUL.
769 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
772 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
776 #ifndef ALLOCA_CUTOFF
777 # define ALLOCA_CUTOFF 4032
779 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
782 * Quote string `str' of length `size' and print the result.
784 * If QUOTE_0_TERMINATED `style' flag is set,
785 * treat `str' as a NUL-terminated string and
786 * quote at most (`size' - 1) bytes.
788 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
789 * do not add leading and trailing quoting symbols.
791 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
792 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
795 print_quoted_string(const char *str, unsigned int size,
796 const unsigned int style)
800 unsigned int alloc_size;
803 if (size && style & QUOTE_0_TERMINATED)
806 alloc_size = 4 * size;
807 if (alloc_size / 4 != size) {
808 error_msg("Out of memory");
812 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2);
814 if (use_alloca(alloc_size)) {
815 outstr = alloca(alloc_size);
818 outstr = buf = malloc(alloc_size);
820 error_msg("Out of memory");
826 rc = string_quote(str, outstr, size, style);
834 * Print path string specified by address `addr' and length `n'.
835 * If path length exceeds `n', append `...' to the output.
838 printpathn(struct tcb *const tcp, const kernel_ulong_t addr, unsigned int n)
840 char path[PATH_MAX + 1];
848 /* Cap path length to the path buffer size */
849 if (n > sizeof path - 1)
852 /* Fetch one byte more to find out whether path length > n. */
853 nul_seen = umovestr(tcp, addr, n + 1, path);
858 print_quoted_string(path, n, QUOTE_0_TERMINATED);
865 printpath(struct tcb *const tcp, const kernel_ulong_t addr)
867 /* Size must correspond to char path[] size in printpathn */
868 printpathn(tcp, addr, PATH_MAX);
872 * Print string specified by address `addr' and length `len'.
873 * If `user_style' has QUOTE_0_TERMINATED bit set, treat the string
874 * as a NUL-terminated string.
875 * Pass `user_style' on to `string_quote'.
876 * Append `...' to the output if either the string length exceeds `max_strlen',
877 * or QUOTE_0_TERMINATED bit is set and the string length exceeds `len'.
880 printstr_ex(struct tcb *const tcp, const kernel_ulong_t addr,
881 const kernel_ulong_t len, const unsigned int user_style)
883 static char *str = NULL;
886 unsigned int style = user_style;
894 /* Allocate static buffers if they are not allocated yet. */
896 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
898 if (outstr_size / 4 != max_strlen)
900 str = xmalloc(max_strlen + 1);
901 outstr = xmalloc(outstr_size);
904 /* Fetch one byte more because string_quote may look one byte ahead. */
905 size = max_strlen + 1;
909 if (style & QUOTE_0_TERMINATED)
910 rc = umovestr(tcp, addr, size, str);
912 rc = umoven(tcp, addr, size, str);
919 if (size > max_strlen)
924 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
925 * or we were requested to print more than -s NUM chars)...
927 ellipsis = string_quote(str, outstr, size, style)
929 && ((style & QUOTE_0_TERMINATED)
930 || len > max_strlen);
938 dumpiov_upto(struct tcb *const tcp, const int len, const kernel_ulong_t addr,
939 kernel_ulong_t data_size)
941 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
943 struct { uint32_t base; uint32_t len; } *iov32;
944 struct { uint64_t base; uint64_t len; } *iov64;
946 #define iov iovu.iov64
948 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
949 #define iov_iov_base(i) \
950 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
951 #define iov_iov_len(i) \
952 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
955 #define sizeof_iov sizeof(*iov)
956 #define iov_iov_base(i) ptr_to_kulong(iov[i].iov_base)
957 #define iov_iov_len(i) iov[i].iov_len
962 size = sizeof_iov * len;
963 /* Assuming no sane program has millions of iovs */
964 if ((unsigned)len > 1024*1024 /* insane or negative size? */
965 || (iov = malloc(size)) == NULL) {
966 error_msg("Out of memory");
969 if (umoven(tcp, addr, size, iov) >= 0) {
970 for (i = 0; i < len; i++) {
971 kernel_ulong_t iov_len = iov_iov_len(i);
972 if (iov_len > data_size)
976 data_size -= iov_len;
977 /* include the buffer number to make it easy to
978 * match up the trace with the source */
979 tprintf(" * %" PRI_klu " bytes in buffer %d\n", iov_len, i);
980 dumpstr(tcp, iov_iov_base(i), iov_len);
991 dumpstr(struct tcb *const tcp, const kernel_ulong_t addr, const int len)
993 static int strsize = -1;
994 static unsigned char *str;
999 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
1000 "1234567890123456") + /*in case I'm off by few:*/ 4)
1001 /*align to 8 to make memset easier:*/ + 7) & -8
1003 const unsigned char *src;
1006 memset(outbuf, ' ', sizeof(outbuf));
1008 if (strsize < len + 16) {
1010 str = malloc(len + 16);
1013 error_msg("Out of memory");
1019 if (umoven(tcp, addr, len, str) < 0)
1022 /* Space-pad to 16 bytes */
1034 *dst++ = "0123456789abcdef"[*src >> 4];
1035 *dst++ = "0123456789abcdef"[*src & 0xf];
1041 dst++; /* space is there by memset */
1044 dst++; /* space is there by memset */
1051 if (*src >= ' ' && *src < 0x7f)
1056 } while (++i & 0xf);
1058 tprintf(" | %05x %s |\n", i - 16, outbuf);
1062 static bool process_vm_readv_not_supported = 0;
1063 #ifndef HAVE_PROCESS_VM_READV
1065 * Need to do this since process_vm_readv() is not yet available in libc.
1066 * When libc is be updated, only "static bool process_vm_readv_not_supported"
1067 * line should remain.
1069 /* Have to avoid duplicating with the C library headers. */
1070 static ssize_t strace_process_vm_readv(pid_t pid,
1071 const struct iovec *lvec,
1072 unsigned long liovcnt,
1073 const struct iovec *rvec,
1074 unsigned long riovcnt,
1075 unsigned long flags)
1077 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
1079 # define process_vm_readv strace_process_vm_readv
1080 #endif /* !HAVE_PROCESS_VM_READV */
1083 vm_read_mem(const pid_t pid, void *const laddr,
1084 const kernel_ulong_t raddr, const size_t len)
1086 const unsigned long truncated_raddr = raddr;
1088 if (raddr != (kernel_ulong_t) truncated_raddr) {
1093 const struct iovec local = {
1097 const struct iovec remote = {
1098 .iov_base = (void *) truncated_raddr,
1102 return process_vm_readv(pid, &local, 1, &remote, 1, 0);
1106 * move `len' bytes of data from process `pid'
1107 * at address `addr' to our space at `our_addr'
1110 umoven(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len,
1111 void *const our_addr)
1113 char *laddr = our_addr;
1115 unsigned int n, m, nread;
1118 char x[sizeof(long)];
1121 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1122 if (current_wordsize < sizeof(addr)
1123 && (addr & (~ (kernel_ulong_t) -1U))) {
1128 if (!process_vm_readv_not_supported) {
1129 int r = vm_read_mem(pid, laddr, addr, len);
1130 if ((unsigned int) r == len)
1133 error_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
1134 (unsigned int) r, len, addr);
1139 process_vm_readv_not_supported = 1;
1142 /* operation not permitted, try PTRACE_PEEKDATA */
1145 /* the process is gone */
1147 case EFAULT: case EIO:
1148 /* address space is inaccessible */
1151 /* all the rest is strange and should be reported */
1152 perror_msg("process_vm_readv");
1158 if (addr & (sizeof(long) - 1)) {
1159 /* addr not a multiple of sizeof(long) */
1160 n = addr & (sizeof(long) - 1); /* residue */
1161 addr &= -sizeof(long); /* aligned address */
1163 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1167 case ESRCH: case EINVAL:
1168 /* these could be seen if the process is gone */
1170 case EFAULT: case EIO: case EPERM:
1171 /* address space is inaccessible */
1174 /* all the rest is strange and should be reported */
1175 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1179 m = MIN(sizeof(long) - n, len);
1180 memcpy(laddr, &u.x[n], m);
1181 addr += sizeof(long);
1188 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1192 case ESRCH: case EINVAL:
1193 /* these could be seen if the process is gone */
1195 case EFAULT: case EIO: case EPERM:
1196 /* address space is inaccessible */
1198 perror_msg("umoven: short read (%u < %u) @0x%" PRI_klx,
1199 nread, nread + len, addr - nread);
1203 /* all the rest is strange and should be reported */
1204 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1208 m = MIN(sizeof(long), len);
1209 memcpy(laddr, u.x, m);
1210 addr += sizeof(long);
1220 umoven_or_printaddr(struct tcb *const tcp, const kernel_ulong_t addr,
1221 const unsigned int len, void *const our_addr)
1223 if (!addr || !verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
1224 umoven(tcp, addr, len, our_addr) < 0) {
1232 umoven_or_printaddr_ignore_syserror(struct tcb *const tcp,
1233 const kernel_ulong_t addr,
1234 const unsigned int len,
1235 void *const our_addr)
1237 if (!addr || !verbose(tcp) || umoven(tcp, addr, len, our_addr) < 0) {
1245 * Like `umove' but make the additional effort of looking
1246 * for a terminating zero byte.
1248 * Returns < 0 on error, > 0 if NUL was seen,
1249 * (TODO if useful: return count of bytes including NUL),
1250 * else 0 if len bytes were read but no NUL byte seen.
1252 * Note: there is no guarantee we won't overwrite some bytes
1253 * in laddr[] _after_ terminating NUL (but, of course,
1254 * we never write past laddr[len-1]).
1257 umovestr(struct tcb *const tcp, kernel_ulong_t addr, unsigned int len, char *laddr)
1259 const unsigned long x01010101 = (unsigned long) 0x0101010101010101ULL;
1260 const unsigned long x80808080 = (unsigned long) 0x8080808080808080ULL;
1263 unsigned int n, m, nread;
1266 char x[sizeof(long)];
1269 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
1270 if (current_wordsize < sizeof(addr)
1271 && (addr & (~ (kernel_ulong_t) -1U))) {
1277 if (!process_vm_readv_not_supported) {
1278 const size_t page_size = get_pagesize();
1279 const size_t page_mask = page_size - 1;
1282 unsigned int chunk_len;
1283 unsigned int end_in_page;
1286 * Don't cross pages, otherwise we can get EFAULT
1287 * and fail to notice that terminating NUL lies
1288 * in the existing (first) page.
1290 chunk_len = len > page_size ? page_size : len;
1291 end_in_page = (addr + chunk_len) & page_mask;
1292 if (chunk_len > end_in_page) /* crosses to the next page */
1293 chunk_len -= end_in_page;
1295 int r = vm_read_mem(pid, laddr, addr, chunk_len);
1297 if (memchr(laddr, '\0', r))
1307 process_vm_readv_not_supported = 1;
1308 goto vm_readv_didnt_work;
1310 /* the process is gone */
1313 /* operation not permitted, try PTRACE_PEEKDATA */
1315 goto vm_readv_didnt_work;
1317 case EFAULT: case EIO:
1318 /* address space is inaccessible */
1320 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
1321 nread, nread + len, addr - nread);
1325 /* all the rest is strange and should be reported */
1326 perror_msg("process_vm_readv");
1332 vm_readv_didnt_work:
1334 if (addr & (sizeof(long) - 1)) {
1335 /* addr not a multiple of sizeof(long) */
1336 n = addr & (sizeof(long) - 1); /* residue */
1337 addr &= -sizeof(long); /* aligned address */
1339 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1343 case ESRCH: case EINVAL:
1344 /* these could be seen if the process is gone */
1346 case EFAULT: case EIO: case EPERM:
1347 /* address space is inaccessible */
1350 /* all the rest is strange and should be reported */
1351 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1355 m = MIN(sizeof(long) - n, len);
1356 memcpy(laddr, &u.x[n], m);
1357 while (n & (sizeof(long) - 1))
1358 if (u.x[n++] == '\0')
1360 addr += sizeof(long);
1368 u.val = ptrace(PTRACE_PEEKDATA, pid, addr, 0);
1372 case ESRCH: case EINVAL:
1373 /* these could be seen if the process is gone */
1375 case EFAULT: case EIO: case EPERM:
1376 /* address space is inaccessible */
1378 perror_msg("umovestr: short read (%d < %d) @0x%" PRI_klx,
1379 nread, nread + len, addr - nread);
1383 /* all the rest is strange and should be reported */
1384 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%" PRI_klx,
1388 m = MIN(sizeof(long), len);
1389 memcpy(laddr, u.x, m);
1390 /* "If a NUL char exists in this word" */
1391 if ((u.val - x01010101) & ~u.val & x80808080)
1393 addr += sizeof(long);
1402 * Iteratively fetch and print up to nmemb elements of elem_size size
1403 * from the array that starts at tracee's address start_addr.
1405 * Array elements are being fetched to the address specified by elem_buf.
1407 * The fetcher callback function specified by umoven_func should follow
1408 * the same semantics as umoven_or_printaddr function.
1410 * The printer callback function specified by print_func is expected
1411 * to print something; if it returns false, no more iterations will be made.
1413 * The pointer specified by opaque_data is passed to each invocation
1414 * of print_func callback function.
1416 * This function prints:
1417 * - "NULL", if start_addr is NULL;
1418 * - "[]", if nmemb is 0;
1419 * - start_addr, if nmemb * elem_size overflows or wraps around;
1420 * - nothing, if the first element cannot be fetched
1421 * (if umoven_func returns non-zero), but it is assumed that
1422 * umoven_func has printed the address it failed to fetch data from;
1423 * - elements of the array, delimited by ", ", with the array itself
1424 * enclosed with [] brackets.
1426 * If abbrev(tcp) is true, then
1427 * - the maximum number of elements printed equals to max_strlen;
1428 * - "..." is printed instead of max_strlen+1 element
1429 * and no more iterations will be made.
1431 * This function returns true only if
1432 * - umoven_func has been called at least once AND
1433 * - umoven_func has not returned false.
1436 print_array(struct tcb *const tcp,
1437 const kernel_ulong_t start_addr,
1439 void *const elem_buf,
1440 const size_t elem_size,
1441 int (*const umoven_func)(struct tcb *,
1445 bool (*const print_func)(struct tcb *,
1449 void *const opaque_data)
1461 const size_t size = nmemb * elem_size;
1462 const kernel_ulong_t end_addr = start_addr + size;
1464 if (end_addr <= start_addr || size / elem_size != nmemb) {
1465 printaddr(start_addr);
1469 const kernel_ulong_t abbrev_end =
1470 (abbrev(tcp) && max_strlen < nmemb) ?
1471 start_addr + elem_size * max_strlen : end_addr;
1474 for (cur = start_addr; cur < end_addr; cur += elem_size) {
1475 if (cur != start_addr)
1478 if (umoven_func(tcp, cur, elem_size, elem_buf))
1481 if (cur == start_addr)
1484 if (cur >= abbrev_end) {
1490 if (!print_func(tcp, elem_buf, elem_size, opaque_data)) {
1495 if (cur != start_addr)
1498 return cur >= end_addr;
1502 printargs(struct tcb *tcp)
1504 const int n = tcp->s_ent->nargs;
1506 for (i = 0; i < n; ++i)
1507 tprintf("%s%#" PRI_klx, i ? ", " : "", tcp->u_arg[i]);
1508 return RVAL_DECODED;
1512 printargs_u(struct tcb *tcp)
1514 const int n = tcp->s_ent->nargs;
1516 for (i = 0; i < n; ++i)
1517 tprintf("%s%u", i ? ", " : "",
1518 (unsigned int) tcp->u_arg[i]);
1519 return RVAL_DECODED;
1523 printargs_d(struct tcb *tcp)
1525 const int n = tcp->s_ent->nargs;
1527 for (i = 0; i < n; ++i)
1528 tprintf("%s%d", i ? ", " : "",
1529 (int) tcp->u_arg[i]);
1530 return RVAL_DECODED;
1533 /* Print abnormal high bits of a kernel_ulong_t value. */
1535 print_abnormal_hi(const kernel_ulong_t val)
1537 if (current_klongsize > 4) {
1538 const unsigned int hi = (unsigned int) ((uint64_t) val >> 32);
1540 tprintf("%#x<<32|", hi);
1544 #if defined _LARGEFILE64_SOURCE && defined HAVE_OPEN64
1545 # define open_file open64
1547 # define open_file open
1551 read_int_from_file(const char *const fname, int *const pvalue)
1553 const int fd = open_file(fname, O_RDONLY);
1558 char buf[sizeof(lval) * 3];
1559 int n = read(fd, buf, sizeof(buf) - 1);
1560 int saved_errno = errno;
1564 errno = saved_errno;
1571 lval = strtol(buf, &endptr, 10);
1572 if (!endptr || (*endptr && '\n' != *endptr)
1573 #if INT_MAX < LONG_MAX
1574 || lval > INT_MAX || lval < INT_MIN
1576 || ERANGE == errno) {
1582 *pvalue = (int) lval;