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_LONG > 4 && SIZEOF_LONG == SIZEOF_LONG_LONG
300 # ifndef current_klongsize
301 if (current_klongsize < SIZEOF_LONG) {
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 #elif SIZEOF_LONG > 4
315 # error Unsupported configuration: SIZEOF_LONG > 4 && SIZEOF_LONG_LONG > SIZEOF_LONG
316 #elif HAVE_STRUCT_TCB_EXT_ARG
317 # ifndef current_klongsize
318 if (current_klongsize < SIZEOF_LONG_LONG) {
319 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
322 # endif /* !current_klongsize */
324 *val = tcp->ext_arg[arg_no];
328 # if defined __ARM_EABI__ || \
329 defined LINUX_MIPSO32 || \
332 /* Align arg_no to the next even number. */
333 arg_no = (arg_no + 1) & 0xe;
336 * The SH4 ABI does allow long longs in odd-numbered registers, but
337 * does not allow them to be split between registers and memory - and
338 * there are only four argument registers for normal functions. As a
339 * result, pread, for example, takes an extra padding argument before
340 * the offset. This was changed late in the 2.4 series (around 2.4.20).
344 # endif /* __ARM_EABI__ || LINUX_MIPSO32 || POWERPC || XTENSA || SH */
345 *val = ULONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
353 * Print 64bit argument at position arg_no and
354 * return the index of the next argument.
357 printllval(struct tcb *tcp, const char *format, int arg_no)
359 unsigned long long val = 0;
361 arg_no = getllval(tcp, &val, arg_no);
362 tprintf(format, val);
367 * Interpret `xlat' as an array of flags
368 * print the entries whose bits are on in `flags'
369 * return # of flags printed.
372 addflags(const struct xlat *xlat, uint64_t flags)
374 for (; xlat->str; xlat++) {
375 if (xlat->val && (flags & xlat->val) == xlat->val) {
376 tprintf("|%s", xlat->str);
381 tprintf("|%#" PRIx64, flags);
386 * Interpret `xlat' as an array of flags.
387 * Print to static string the entries whose bits are on in `flags'
388 * Return static string.
391 sprintflags(const char *prefix, const struct xlat *xlat, uint64_t flags)
393 static char outstr[1024];
397 outptr = stpcpy(outstr, prefix);
399 if (flags == 0 && xlat->val == 0 && xlat->str) {
400 strcpy(outptr, xlat->str);
404 for (; xlat->str; xlat++) {
405 if (xlat->val && (flags & xlat->val) == xlat->val) {
408 outptr = stpcpy(outptr, xlat->str);
418 outptr += sprintf(outptr, "%#" PRIx64, flags);
425 printflags64(const struct xlat *xlat, uint64_t flags, const char *dflt)
430 if (flags == 0 && xlat->val == 0 && xlat->str) {
436 for (n = 0; xlat->str; xlat++) {
437 if (xlat->val && (flags & xlat->val) == xlat->val) {
438 tprintf("%s%s", sep, xlat->str);
447 tprintf("%s%#" PRIx64, sep, flags);
452 tprintf("%#" PRIx64, flags);
454 tprintf(" /* %s */", dflt);
465 printaddr_klu(const kernel_ulong_t addr)
470 tprintf("%#" PRI_klx, addr);
473 #define DEF_PRINTNUM(name, type) \
475 printnum_ ## name(struct tcb *const tcp, const kernel_ureg_t addr, \
476 const char *const fmt) \
479 if (umove_or_printaddr(tcp, addr, &num)) \
487 #define DEF_PRINTPAIR(name, type) \
489 printpair_ ## name(struct tcb *const tcp, const kernel_ureg_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_PRINTPAIR(int, int)
505 DEF_PRINTNUM(short, short)
506 DEF_PRINTNUM(int64, uint64_t)
507 DEF_PRINTPAIR(int64, uint64_t)
509 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
511 printnum_long_int(struct tcb *const tcp, const kernel_ureg_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);
526 static char buf[sizeof(int) * 3 * 6 + sizeof("+0000")];
534 strftime(buf, sizeof(buf), "%FT%T%z", tmp);
536 snprintf(buf, sizeof(buf), "%lu", (unsigned long) t);
542 getfdproto(struct tcb *tcp, int fd)
544 #ifdef HAVE_SYS_XATTR_H
545 size_t bufsize = 256;
548 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
551 return SOCK_PROTO_UNKNOWN;
553 sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
554 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
556 return SOCK_PROTO_UNKNOWN;
559 * This is a protection for the case when the kernel
560 * side does not append a null byte to the buffer.
564 return get_proto_by_name(buf);
567 return SOCK_PROTO_UNKNOWN;
572 printfd(struct tcb *tcp, int fd)
574 char path[PATH_MAX + 1];
575 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
576 static const char socket_prefix[] = "socket:[";
577 const size_t socket_prefix_len = sizeof(socket_prefix) - 1;
578 const size_t path_len = strlen(path);
581 if (show_fd_path > 1 &&
582 strncmp(path, socket_prefix, socket_prefix_len) == 0 &&
583 path[path_len - 1] == ']') {
584 unsigned long inode =
585 strtoul(path + socket_prefix_len, NULL, 10);
587 if (!print_sockaddr_by_inode_cached(inode)) {
588 const enum sock_proto proto =
590 if (!print_sockaddr_by_inode(inode, proto))
594 print_quoted_string(path, path_len,
595 QUOTE_OMIT_LEADING_TRAILING_QUOTES);
603 * Quote string `instr' of length `size'
604 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
606 * If QUOTE_0_TERMINATED `style' flag is set,
607 * treat `instr' as a NUL-terminated string,
608 * checking up to (`size' + 1) bytes of `instr'.
610 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
611 * do not add leading and trailing quoting symbols.
613 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
614 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
617 string_quote(const char *instr, char *outstr, const unsigned int size,
618 const unsigned int style)
620 const unsigned char *ustr = (const unsigned char *) instr;
625 if (style & QUOTE_0_TERMINATED)
628 eol = 0x100; /* this can never match a char */
634 /* Check for presence of symbol which require
635 to hex-quote the whole string. */
636 for (i = 0; i < size; ++i) {
638 /* Check for NUL-terminated string. */
642 /* Force hex unless c is printable or whitespace */
647 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
648 * They happen to have ASCII codes 9,10,11,12,13.
650 if (c < ' ' && (unsigned)(c - 9) >= 5) {
657 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
661 /* Hex-quote the whole string. */
662 for (i = 0; i < size; ++i) {
664 /* Check for NUL-terminated string. */
669 *s++ = "0123456789abcdef"[c >> 4];
670 *s++ = "0123456789abcdef"[c & 0xf];
673 for (i = 0; i < size; ++i) {
675 /* Check for NUL-terminated string. */
678 if ((i == (size - 1)) &&
679 (style & QUOTE_OMIT_TRAILING_0) && (c == '\0'))
682 case '\"': case '\\':
707 if (c >= ' ' && c <= 0x7e)
713 && ustr[i + 1] >= '0'
714 && ustr[i + 1] <= '9'
717 *s++ = '0' + (c >> 6);
718 *s++ = '0' + ((c >> 3) & 0x7);
723 *s++ = '0' + (c >> 6);
724 *s++ = '0' + ((c >> 3) & 0x7);
727 *s++ = '0' + (c & 0x7);
734 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
738 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
739 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
740 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
741 * but next char is NUL.
749 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
752 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
756 #ifndef ALLOCA_CUTOFF
757 # define ALLOCA_CUTOFF 4032
759 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
762 * Quote string `str' of length `size' and print the result.
764 * If QUOTE_0_TERMINATED `style' flag is set,
765 * treat `str' as a NUL-terminated string and
766 * quote at most (`size' - 1) bytes.
768 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
769 * do not add leading and trailing quoting symbols.
771 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
772 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
775 print_quoted_string(const char *str, unsigned int size,
776 const unsigned int style)
780 unsigned int alloc_size;
783 if (size && style & QUOTE_0_TERMINATED)
786 alloc_size = 4 * size;
787 if (alloc_size / 4 != size) {
788 error_msg("Out of memory");
792 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2);
794 if (use_alloca(alloc_size)) {
795 outstr = alloca(alloc_size);
798 outstr = buf = malloc(alloc_size);
800 error_msg("Out of memory");
806 rc = string_quote(str, outstr, size, style);
814 * Print path string specified by address `addr' and length `n'.
815 * If path length exceeds `n', append `...' to the output.
818 printpathn(struct tcb *const tcp, const kernel_ureg_t addr, unsigned int n)
820 char path[PATH_MAX + 1];
828 /* Cap path length to the path buffer size */
829 if (n > sizeof path - 1)
832 /* Fetch one byte more to find out whether path length > n. */
833 nul_seen = umovestr(tcp, addr, n + 1, path);
838 print_quoted_string(path, n, QUOTE_0_TERMINATED);
845 printpath(struct tcb *const tcp, const kernel_ureg_t addr)
847 /* Size must correspond to char path[] size in printpathn */
848 printpathn(tcp, addr, PATH_MAX);
852 * Print string specified by address `addr' and length `len'.
853 * If `len' == -1, set QUOTE_0_TERMINATED bit in `user_style'.
854 * If `user_style' has QUOTE_0_TERMINATED bit set, treat the string
855 * as a NUL-terminated string.
856 * Pass `user_style' on to `string_quote'.
857 * Append `...' to the output if either the string length exceeds `max_strlen',
858 * or `len' != -1 and the string length exceeds `len'.
861 printstr_ex(struct tcb *const tcp, const kernel_ureg_t addr, const long len,
862 const unsigned int user_style)
864 static char *str = NULL;
867 unsigned int style = user_style;
875 /* Allocate static buffers if they are not allocated yet. */
877 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
879 if (outstr_size / 4 != max_strlen)
881 str = xmalloc(max_strlen + 1);
882 outstr = xmalloc(outstr_size);
885 size = max_strlen + 1;
888 * Treat as a NUL-terminated string: fetch one byte more
889 * because string_quote may look one byte ahead.
891 style |= QUOTE_0_TERMINATED;
892 rc = umovestr(tcp, addr, size, str);
894 if (size > (unsigned long) len)
895 size = (unsigned long) len;
896 if (style & QUOTE_0_TERMINATED)
897 rc = umovestr(tcp, addr, size, str);
899 rc = umoven(tcp, addr, size, str);
906 if (size > max_strlen)
911 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
912 * or we were requested to print more than -s NUM chars)...
914 ellipsis = string_quote(str, outstr, size, style)
916 && ((style & QUOTE_0_TERMINATED)
917 || (unsigned long) len > max_strlen);
925 dumpiov_upto(struct tcb *const tcp, const int len, const kernel_ureg_t addr,
926 unsigned long data_size)
928 #if SUPPORTED_PERSONALITIES > 1
930 struct { uint32_t base; uint32_t len; } *iov32;
931 struct { uint64_t base; uint64_t len; } *iov64;
933 #define iov iovu.iov64
935 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
936 #define iov_iov_base(i) \
937 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
938 #define iov_iov_len(i) \
939 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
942 #define sizeof_iov sizeof(*iov)
943 #define iov_iov_base(i) iov[i].iov_base
944 #define iov_iov_len(i) iov[i].iov_len
949 size = sizeof_iov * len;
950 /* Assuming no sane program has millions of iovs */
951 if ((unsigned)len > 1024*1024 /* insane or negative size? */
952 || (iov = malloc(size)) == NULL) {
953 error_msg("Out of memory");
956 if (umoven(tcp, addr, size, iov) >= 0) {
957 for (i = 0; i < len; i++) {
958 unsigned long iov_len = iov_iov_len(i);
959 if (iov_len > data_size)
963 data_size -= iov_len;
964 /* include the buffer number to make it easy to
965 * match up the trace with the source */
966 tprintf(" * %lu bytes in buffer %d\n", iov_len, i);
967 dumpstr(tcp, (kernel_ureg_t) iov_iov_base(i), iov_len);
978 dumpstr(struct tcb *const tcp, const kernel_ureg_t addr, const int len)
980 static int strsize = -1;
981 static unsigned char *str;
986 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
987 "1234567890123456") + /*in case I'm off by few:*/ 4)
988 /*align to 8 to make memset easier:*/ + 7) & -8
990 const unsigned char *src;
993 memset(outbuf, ' ', sizeof(outbuf));
995 if (strsize < len + 16) {
997 str = malloc(len + 16);
1000 error_msg("Out of memory");
1006 if (umoven(tcp, addr, len, str) < 0)
1009 /* Space-pad to 16 bytes */
1021 *dst++ = "0123456789abcdef"[*src >> 4];
1022 *dst++ = "0123456789abcdef"[*src & 0xf];
1028 dst++; /* space is there by memset */
1031 dst++; /* space is there by memset */
1038 if (*src >= ' ' && *src < 0x7f)
1043 } while (++i & 0xf);
1045 tprintf(" | %05x %s |\n", i - 16, outbuf);
1049 #ifdef HAVE_PROCESS_VM_READV
1050 /* C library supports this, but the kernel might not. */
1051 static bool process_vm_readv_not_supported = 0;
1054 /* Need to do this since process_vm_readv() is not yet available in libc.
1055 * When libc is be updated, only "static bool process_vm_readv_not_supported"
1056 * line should remain.
1058 #if !defined(__NR_process_vm_readv)
1060 # define __NR_process_vm_readv 347
1061 # elif defined(X86_64)
1062 # define __NR_process_vm_readv 310
1063 # elif defined(POWERPC)
1064 # define __NR_process_vm_readv 351
1068 #if defined(__NR_process_vm_readv)
1069 static bool process_vm_readv_not_supported = 0;
1070 /* Have to avoid duplicating with the C library headers. */
1071 static ssize_t strace_process_vm_readv(pid_t pid,
1072 const struct iovec *lvec,
1073 unsigned long liovcnt,
1074 const struct iovec *rvec,
1075 unsigned long riovcnt,
1076 unsigned long flags)
1078 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
1080 #define process_vm_readv strace_process_vm_readv
1082 static bool process_vm_readv_not_supported = 1;
1083 # define process_vm_readv(...) (errno = ENOSYS, -1)
1086 #endif /* end of hack */
1089 vm_read_mem(const pid_t pid, void *const laddr,
1090 const kernel_ureg_t raddr, const size_t len)
1092 const struct iovec local = {
1096 const struct iovec remote = {
1097 .iov_base = (void *) raddr,
1101 return process_vm_readv(pid, &local, 1, &remote, 1, 0);
1105 * move `len' bytes of data from process `pid'
1106 * at address `addr' to our space at `our_addr'
1109 umoven(struct tcb *const tcp, kernel_ureg_t addr, unsigned int len,
1110 void *const our_addr)
1112 char *laddr = our_addr;
1114 unsigned int n, m, nread;
1117 char x[sizeof(long)];
1120 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1121 if (current_wordsize < sizeof(addr))
1122 addr &= (1ul << 8 * current_wordsize) - 1;
1125 if (!process_vm_readv_not_supported) {
1126 int r = vm_read_mem(pid, laddr, addr, len);
1127 if ((unsigned int) r == len)
1130 error_msg("umoven: short read (%u < %u) @0x%lx",
1131 (unsigned int) r, len, addr);
1136 process_vm_readv_not_supported = 1;
1139 /* operation not permitted, try PTRACE_PEEKDATA */
1142 /* the process is gone */
1144 case EFAULT: case EIO:
1145 /* address space is inaccessible */
1148 /* all the rest is strange and should be reported */
1149 perror_msg("process_vm_readv");
1155 if (addr & (sizeof(long) - 1)) {
1156 /* addr not a multiple of sizeof(long) */
1157 n = addr & (sizeof(long) - 1); /* residue */
1158 addr &= -sizeof(long); /* aligned address */
1160 u.val = ptrace(PTRACE_PEEKDATA, pid, (void *) addr, 0);
1164 case ESRCH: case EINVAL:
1165 /* these could be seen if the process is gone */
1167 case EFAULT: case EIO: case EPERM:
1168 /* address space is inaccessible */
1171 /* all the rest is strange and should be reported */
1172 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
1176 m = MIN(sizeof(long) - n, len);
1177 memcpy(laddr, &u.x[n], m);
1178 addr += sizeof(long);
1185 u.val = ptrace(PTRACE_PEEKDATA, pid, (void *) addr, 0);
1189 case ESRCH: case EINVAL:
1190 /* these could be seen if the process is gone */
1192 case EFAULT: case EIO: case EPERM:
1193 /* address space is inaccessible */
1195 perror_msg("umoven: short read (%u < %u) @0x%lx",
1196 nread, nread + len, addr - nread);
1200 /* all the rest is strange and should be reported */
1201 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
1205 m = MIN(sizeof(long), len);
1206 memcpy(laddr, u.x, m);
1207 addr += sizeof(long);
1217 umoven_or_printaddr(struct tcb *const tcp, const kernel_ureg_t addr,
1218 const unsigned int len, void *const our_addr)
1220 if (!addr || !verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
1221 umoven(tcp, addr, len, our_addr) < 0) {
1229 umoven_or_printaddr_ignore_syserror(struct tcb *const tcp,
1230 const kernel_ureg_t addr,
1231 const unsigned int len,
1232 void *const our_addr)
1234 if (!addr || !verbose(tcp) || umoven(tcp, addr, len, our_addr) < 0) {
1242 * Like `umove' but make the additional effort of looking
1243 * for a terminating zero byte.
1245 * Returns < 0 on error, > 0 if NUL was seen,
1246 * (TODO if useful: return count of bytes including NUL),
1247 * else 0 if len bytes were read but no NUL byte seen.
1249 * Note: there is no guarantee we won't overwrite some bytes
1250 * in laddr[] _after_ terminating NUL (but, of course,
1251 * we never write past laddr[len-1]).
1254 umovestr(struct tcb *const tcp, kernel_ureg_t addr, unsigned int len, char *laddr)
1256 const unsigned long x01010101 = (unsigned long) 0x0101010101010101ULL;
1257 const unsigned long x80808080 = (unsigned long) 0x8080808080808080ULL;
1260 unsigned int n, m, nread;
1263 char x[sizeof(long)];
1266 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1267 if (current_wordsize < sizeof(addr))
1268 addr &= (1ul << 8 * current_wordsize) - 1;
1272 if (!process_vm_readv_not_supported) {
1273 const size_t page_size = get_pagesize();
1274 const size_t page_mask = page_size - 1;
1277 unsigned int chunk_len;
1278 unsigned int end_in_page;
1281 * Don't cross pages, otherwise we can get EFAULT
1282 * and fail to notice that terminating NUL lies
1283 * in the existing (first) page.
1285 chunk_len = len > page_size ? page_size : len;
1286 end_in_page = (addr + chunk_len) & page_mask;
1287 if (chunk_len > end_in_page) /* crosses to the next page */
1288 chunk_len -= end_in_page;
1290 int r = vm_read_mem(pid, laddr, addr, chunk_len);
1292 if (memchr(laddr, '\0', r))
1302 process_vm_readv_not_supported = 1;
1303 goto vm_readv_didnt_work;
1305 /* the process is gone */
1308 /* operation not permitted, try PTRACE_PEEKDATA */
1310 goto vm_readv_didnt_work;
1312 case EFAULT: case EIO:
1313 /* address space is inaccessible */
1315 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1316 nread, nread + len, addr - nread);
1320 /* all the rest is strange and should be reported */
1321 perror_msg("process_vm_readv");
1327 vm_readv_didnt_work:
1329 if (addr & (sizeof(long) - 1)) {
1330 /* addr not a multiple of sizeof(long) */
1331 n = addr & (sizeof(long) - 1); /* residue */
1332 addr &= -sizeof(long); /* aligned address */
1334 u.val = ptrace(PTRACE_PEEKDATA, pid, (void *) addr, 0);
1338 case ESRCH: case EINVAL:
1339 /* these could be seen if the process is gone */
1341 case EFAULT: case EIO: case EPERM:
1342 /* address space is inaccessible */
1345 /* all the rest is strange and should be reported */
1346 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1350 m = MIN(sizeof(long) - n, len);
1351 memcpy(laddr, &u.x[n], m);
1352 while (n & (sizeof(long) - 1))
1353 if (u.x[n++] == '\0')
1355 addr += sizeof(long);
1363 u.val = ptrace(PTRACE_PEEKDATA, pid, (void *) addr, 0);
1367 case ESRCH: case EINVAL:
1368 /* these could be seen if the process is gone */
1370 case EFAULT: case EIO: case EPERM:
1371 /* address space is inaccessible */
1373 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1374 nread, nread + len, addr - nread);
1378 /* all the rest is strange and should be reported */
1379 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1383 m = MIN(sizeof(long), len);
1384 memcpy(laddr, u.x, m);
1385 /* "If a NUL char exists in this word" */
1386 if ((u.val - x01010101) & ~u.val & x80808080)
1388 addr += sizeof(long);
1397 * Iteratively fetch and print up to nmemb elements of elem_size size
1398 * from the array that starts at tracee's address start_addr.
1400 * Array elements are being fetched to the address specified by elem_buf.
1402 * The fetcher callback function specified by umoven_func should follow
1403 * the same semantics as umoven_or_printaddr function.
1405 * The printer callback function specified by print_func is expected
1406 * to print something; if it returns false, no more iterations will be made.
1408 * The pointer specified by opaque_data is passed to each invocation
1409 * of print_func callback function.
1411 * This function prints:
1412 * - "NULL", if start_addr is NULL;
1413 * - "[]", if nmemb is 0;
1414 * - start_addr, if nmemb * elem_size overflows or wraps around;
1415 * - nothing, if the first element cannot be fetched
1416 * (if umoven_func returns non-zero), but it is assumed that
1417 * umoven_func has printed the address it failed to fetch data from;
1418 * - elements of the array, delimited by ", ", with the array itself
1419 * enclosed with [] brackets.
1421 * If abbrev(tcp) is true, then
1422 * - the maximum number of elements printed equals to max_strlen;
1423 * - "..." is printed instead of max_strlen+1 element
1424 * and no more iterations will be made.
1426 * This function returns true only if
1427 * - umoven_func has been called at least once AND
1428 * - umoven_func has not returned false.
1431 print_array(struct tcb *const tcp,
1432 const kernel_ureg_t start_addr,
1434 void *const elem_buf,
1435 const size_t elem_size,
1436 int (*const umoven_func)(struct tcb *,
1440 bool (*const print_func)(struct tcb *,
1444 void *const opaque_data)
1456 const size_t size = nmemb * elem_size;
1457 const kernel_ureg_t end_addr = start_addr + size;
1459 if (end_addr <= start_addr || size / elem_size != nmemb) {
1460 printaddr(start_addr);
1464 const kernel_ureg_t abbrev_end =
1465 (abbrev(tcp) && max_strlen < nmemb) ?
1466 start_addr + elem_size * max_strlen : end_addr;
1469 for (cur = start_addr; cur < end_addr; cur += elem_size) {
1470 if (cur != start_addr)
1473 if (umoven_func(tcp, cur, elem_size, elem_buf))
1476 if (cur == start_addr)
1479 if (cur >= abbrev_end) {
1485 if (!print_func(tcp, elem_buf, elem_size, opaque_data)) {
1490 if (cur != start_addr)
1493 return cur >= end_addr;
1497 getarg_klu(struct tcb *tcp, int argn)
1499 #if HAVE_STRUCT_TCB_EXT_ARG
1500 # ifndef current_klongsize
1501 if (current_klongsize < sizeof(*tcp->ext_arg)) {
1502 return tcp->u_arg[argn];
1504 # endif /* !current_klongsize */
1506 return tcp->ext_arg[argn];
1509 return tcp->u_arg[argn];
1514 printargs(struct tcb *tcp)
1516 const int n = tcp->s_ent->nargs;
1518 for (i = 0; i < n; ++i)
1519 tprintf("%s%#" PRI_klx, i ? ", " : "", getarg_klu(tcp, i));
1520 return RVAL_DECODED;
1524 printargs_u(struct tcb *tcp)
1526 const int n = tcp->s_ent->nargs;
1528 for (i = 0; i < n; ++i)
1529 tprintf("%s%u", i ? ", " : "",
1530 (unsigned int) tcp->u_arg[i]);
1531 return RVAL_DECODED;
1535 printargs_d(struct tcb *tcp)
1537 const int n = tcp->s_ent->nargs;
1539 for (i = 0; i < n; ++i)
1540 tprintf("%s%d", i ? ", " : "",
1541 (int) tcp->u_arg[i]);
1542 return RVAL_DECODED;
1545 #if defined _LARGEFILE64_SOURCE && defined HAVE_OPEN64
1546 # define open_file open64
1548 # define open_file open
1552 read_int_from_file(const char *const fname, int *const pvalue)
1554 const int fd = open_file(fname, O_RDONLY);
1559 char buf[sizeof(lval) * 3];
1560 int n = read(fd, buf, sizeof(buf) - 1);
1561 int saved_errno = errno;
1565 errno = saved_errno;
1572 lval = strtol(buf, &endptr, 10);
1573 if (!endptr || (*endptr && '\n' != *endptr)
1574 #if INT_MAX < LONG_MAX
1575 || lval > INT_MAX || lval < INT_MIN
1577 || ERANGE == errno) {
1583 *pvalue = (int) lval;