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 # if SUPPORTED_PERSONALITIES > 1
302 if (current_personality != 1) {
304 if (current_wordsize > 4) {
307 *val = tcp->u_arg[arg_no];
309 # if SUPPORTED_PERSONALITIES > 1
311 # if defined(AARCH64) || defined(POWERPC64)
312 /* Align arg_no to the next even number. */
313 arg_no = (arg_no + 1) & 0xe;
314 # endif /* AARCH64 || POWERPC64 */
315 *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
318 # endif /* SUPPORTED_PERSONALITIES > 1 */
319 #elif SIZEOF_LONG > 4
320 # error Unsupported configuration: SIZEOF_LONG > 4 && SIZEOF_LONG_LONG > SIZEOF_LONG
321 #elif HAVE_STRUCT_TCB_EXT_ARG
322 # if SUPPORTED_PERSONALITIES > 1
323 if (current_personality == 1) {
324 *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
329 *val = tcp->ext_arg[arg_no];
333 # if defined __ARM_EABI__ || \
334 defined LINUX_MIPSO32 || \
337 /* Align arg_no to the next even number. */
338 arg_no = (arg_no + 1) & 0xe;
341 * The SH4 ABI does allow long longs in odd-numbered registers, but
342 * does not allow them to be split between registers and memory - and
343 * there are only four argument registers for normal functions. As a
344 * result, pread, for example, takes an extra padding argument before
345 * the offset. This was changed late in the 2.4 series (around 2.4.20).
349 # endif /* __ARM_EABI__ || LINUX_MIPSO32 || POWERPC || XTENSA || SH */
350 *val = LONG_LONG(tcp->u_arg[arg_no], tcp->u_arg[arg_no + 1]);
358 * Print 64bit argument at position arg_no and
359 * return the index of the next argument.
362 printllval(struct tcb *tcp, const char *format, int arg_no)
364 unsigned long long val = 0;
366 arg_no = getllval(tcp, &val, arg_no);
367 tprintf(format, val);
372 * Interpret `xlat' as an array of flags
373 * print the entries whose bits are on in `flags'
374 * return # of flags printed.
377 addflags(const struct xlat *xlat, uint64_t flags)
379 for (; xlat->str; xlat++) {
380 if (xlat->val && (flags & xlat->val) == xlat->val) {
381 tprintf("|%s", xlat->str);
386 tprintf("|%#" PRIx64, flags);
391 * Interpret `xlat' as an array of flags.
392 * Print to static string the entries whose bits are on in `flags'
393 * Return static string.
396 sprintflags(const char *prefix, const struct xlat *xlat, uint64_t flags)
398 static char outstr[1024];
402 outptr = stpcpy(outstr, prefix);
404 if (flags == 0 && xlat->val == 0 && xlat->str) {
405 strcpy(outptr, xlat->str);
409 for (; xlat->str; xlat++) {
410 if (xlat->val && (flags & xlat->val) == xlat->val) {
413 outptr = stpcpy(outptr, xlat->str);
423 outptr += sprintf(outptr, "%#" PRIx64, flags);
430 printflags64(const struct xlat *xlat, uint64_t flags, const char *dflt)
435 if (flags == 0 && xlat->val == 0 && xlat->str) {
441 for (n = 0; xlat->str; xlat++) {
442 if (xlat->val && (flags & xlat->val) == xlat->val) {
443 tprintf("%s%s", sep, xlat->str);
452 tprintf("%s%#" PRIx64, sep, flags);
457 tprintf("%#" PRIx64, flags);
459 tprintf(" /* %s */", dflt);
470 printaddr_ull(const unsigned long long addr)
475 tprintf("%#llx", addr);
478 #define DEF_PRINTNUM(name, type) \
480 printnum_ ## name(struct tcb *tcp, const long addr, const char *fmt) \
483 if (umove_or_printaddr(tcp, addr, &num)) \
491 #define DEF_PRINTPAIR(name, type) \
493 printpair_ ## name(struct tcb *tcp, const long addr, const char *fmt) \
496 if (umove_or_printaddr(tcp, addr, &pair)) \
499 tprintf(fmt, pair[0]); \
501 tprintf(fmt, pair[1]); \
506 DEF_PRINTNUM(int, int)
507 DEF_PRINTPAIR(int, int)
508 DEF_PRINTNUM(short, short)
509 DEF_PRINTNUM(int64, uint64_t)
510 DEF_PRINTPAIR(int64, uint64_t)
512 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
514 printnum_long_int(struct tcb *tcp, const long addr,
515 const char *fmt_long, const char *fmt_int)
517 if (current_wordsize > sizeof(int)) {
518 return printnum_int64(tcp, addr, fmt_long);
520 return printnum_int(tcp, addr, fmt_int);
529 static char buf[sizeof(int) * 3 * 6 + sizeof("+0000")];
537 strftime(buf, sizeof(buf), "%FT%T%z", tmp);
539 snprintf(buf, sizeof(buf), "%lu", (unsigned long) t);
545 getfdproto(struct tcb *tcp, int fd)
547 #ifdef HAVE_SYS_XATTR_H
548 size_t bufsize = 256;
551 char path[sizeof("/proc/%u/fd/%u") + 2 * sizeof(int)*3];
554 return SOCK_PROTO_UNKNOWN;
556 sprintf(path, "/proc/%u/fd/%u", tcp->pid, fd);
557 r = getxattr(path, "system.sockprotoname", buf, bufsize - 1);
559 return SOCK_PROTO_UNKNOWN;
562 * This is a protection for the case when the kernel
563 * side does not append a null byte to the buffer.
567 return get_proto_by_name(buf);
570 return SOCK_PROTO_UNKNOWN;
575 printfd(struct tcb *tcp, int fd)
577 char path[PATH_MAX + 1];
578 if (show_fd_path && getfdpath(tcp, fd, path, sizeof(path)) >= 0) {
579 static const char socket_prefix[] = "socket:[";
580 const size_t socket_prefix_len = sizeof(socket_prefix) - 1;
581 const size_t path_len = strlen(path);
584 if (show_fd_path > 1 &&
585 strncmp(path, socket_prefix, socket_prefix_len) == 0 &&
586 path[path_len - 1] == ']') {
587 unsigned long inode =
588 strtoul(path + socket_prefix_len, NULL, 10);
590 if (!print_sockaddr_by_inode_cached(inode)) {
591 const enum sock_proto proto =
593 if (!print_sockaddr_by_inode(inode, proto))
597 print_quoted_string(path, path_len,
598 QUOTE_OMIT_LEADING_TRAILING_QUOTES);
606 * Quote string `instr' of length `size'
607 * Write up to (3 + `size' * 4) bytes to `outstr' buffer.
609 * If QUOTE_0_TERMINATED `style' flag is set,
610 * treat `instr' as a NUL-terminated string,
611 * checking up to (`size' + 1) bytes of `instr'.
613 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
614 * do not add leading and trailing quoting symbols.
616 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
617 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
620 string_quote(const char *instr, char *outstr, const unsigned int size,
621 const unsigned int style)
623 const unsigned char *ustr = (const unsigned char *) instr;
628 if (style & QUOTE_0_TERMINATED)
631 eol = 0x100; /* this can never match a char */
637 /* Check for presence of symbol which require
638 to hex-quote the whole string. */
639 for (i = 0; i < size; ++i) {
641 /* Check for NUL-terminated string. */
645 /* Force hex unless c is printable or whitespace */
650 /* In ASCII isspace is only these chars: "\t\n\v\f\r".
651 * They happen to have ASCII codes 9,10,11,12,13.
653 if (c < ' ' && (unsigned)(c - 9) >= 5) {
660 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
664 /* Hex-quote the whole string. */
665 for (i = 0; i < size; ++i) {
667 /* Check for NUL-terminated string. */
672 *s++ = "0123456789abcdef"[c >> 4];
673 *s++ = "0123456789abcdef"[c & 0xf];
676 for (i = 0; i < size; ++i) {
678 /* Check for NUL-terminated string. */
681 if ((i == (size - 1)) &&
682 (style & QUOTE_OMIT_TRAILING_0) && (c == '\0'))
685 case '\"': case '\\':
710 if (c >= ' ' && c <= 0x7e)
716 && ustr[i + 1] >= '0'
717 && ustr[i + 1] <= '9'
720 *s++ = '0' + (c >> 6);
721 *s++ = '0' + ((c >> 3) & 0x7);
726 *s++ = '0' + (c >> 6);
727 *s++ = '0' + ((c >> 3) & 0x7);
730 *s++ = '0' + (c & 0x7);
737 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
741 /* Return zero if we printed entire ASCIZ string (didn't truncate it) */
742 if (style & QUOTE_0_TERMINATED && ustr[i] == '\0') {
743 /* We didn't see NUL yet (otherwise we'd jump to 'asciz_ended')
744 * but next char is NUL.
752 if (!(style & QUOTE_OMIT_LEADING_TRAILING_QUOTES))
755 /* Return zero: we printed entire ASCIZ string (didn't truncate it) */
759 #ifndef ALLOCA_CUTOFF
760 # define ALLOCA_CUTOFF 4032
762 #define use_alloca(n) ((n) <= ALLOCA_CUTOFF)
765 * Quote string `str' of length `size' and print the result.
767 * If QUOTE_0_TERMINATED `style' flag is set,
768 * treat `str' as a NUL-terminated string and
769 * quote at most (`size' - 1) bytes.
771 * If QUOTE_OMIT_LEADING_TRAILING_QUOTES `style' flag is set,
772 * do not add leading and trailing quoting symbols.
774 * Returns 0 if QUOTE_0_TERMINATED is set and NUL was seen, 1 otherwise.
775 * Note that if QUOTE_0_TERMINATED is not set, always returns 1.
778 print_quoted_string(const char *str, unsigned int size,
779 const unsigned int style)
783 unsigned int alloc_size;
786 if (size && style & QUOTE_0_TERMINATED)
789 alloc_size = 4 * size;
790 if (alloc_size / 4 != size) {
791 error_msg("Out of memory");
795 alloc_size += 1 + (style & QUOTE_OMIT_LEADING_TRAILING_QUOTES ? 0 : 2);
797 if (use_alloca(alloc_size)) {
798 outstr = alloca(alloc_size);
801 outstr = buf = malloc(alloc_size);
803 error_msg("Out of memory");
809 rc = string_quote(str, outstr, size, style);
817 * Print path string specified by address `addr' and length `n'.
818 * If path length exceeds `n', append `...' to the output.
821 printpathn(struct tcb *tcp, long addr, unsigned int n)
823 char path[PATH_MAX + 1];
831 /* Cap path length to the path buffer size */
832 if (n > sizeof path - 1)
835 /* Fetch one byte more to find out whether path length > n. */
836 nul_seen = umovestr(tcp, addr, n + 1, path);
841 print_quoted_string(path, n, QUOTE_0_TERMINATED);
848 printpath(struct tcb *tcp, long addr)
850 /* Size must correspond to char path[] size in printpathn */
851 printpathn(tcp, addr, PATH_MAX);
855 * Print string specified by address `addr' and length `len'.
856 * If `len' == -1, set QUOTE_0_TERMINATED bit in `user_style'.
857 * If `user_style' has QUOTE_0_TERMINATED bit set, treat the string
858 * as a NUL-terminated string.
859 * Pass `user_style' on to `string_quote'.
860 * Append `...' to the output if either the string length exceeds `max_strlen',
861 * or `len' != -1 and the string length exceeds `len'.
864 printstr_ex(struct tcb *tcp, long addr, long len, unsigned int user_style)
866 static char *str = NULL;
869 unsigned int style = user_style;
877 /* Allocate static buffers if they are not allocated yet. */
879 unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
881 if (outstr_size / 4 != max_strlen)
883 str = xmalloc(max_strlen + 1);
884 outstr = xmalloc(outstr_size);
887 size = max_strlen + 1;
890 * Treat as a NUL-terminated string: fetch one byte more
891 * because string_quote may look one byte ahead.
893 style |= QUOTE_0_TERMINATED;
894 rc = umovestr(tcp, addr, size, str);
896 if (size > (unsigned long) len)
897 size = (unsigned long) len;
898 if (style & QUOTE_0_TERMINATED)
899 rc = umovestr(tcp, addr, size, str);
901 rc = umoven(tcp, addr, size, str);
908 if (size > max_strlen)
913 /* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
914 * or we were requested to print more than -s NUM chars)...
916 ellipsis = string_quote(str, outstr, size, style)
918 && ((style & QUOTE_0_TERMINATED)
919 || (unsigned long) len > max_strlen);
927 dumpiov_upto(struct tcb *tcp, int len, long addr, unsigned long data_size)
929 #if SUPPORTED_PERSONALITIES > 1
931 struct { uint32_t base; uint32_t len; } *iov32;
932 struct { uint64_t base; uint64_t len; } *iov64;
934 #define iov iovu.iov64
936 (current_wordsize == 4 ? sizeof(*iovu.iov32) : sizeof(*iovu.iov64))
937 #define iov_iov_base(i) \
938 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].base : iovu.iov64[i].base)
939 #define iov_iov_len(i) \
940 (current_wordsize == 4 ? (uint64_t) iovu.iov32[i].len : iovu.iov64[i].len)
943 #define sizeof_iov sizeof(*iov)
944 #define iov_iov_base(i) iov[i].iov_base
945 #define iov_iov_len(i) iov[i].iov_len
950 size = sizeof_iov * len;
951 /* Assuming no sane program has millions of iovs */
952 if ((unsigned)len > 1024*1024 /* insane or negative size? */
953 || (iov = malloc(size)) == NULL) {
954 error_msg("Out of memory");
957 if (umoven(tcp, addr, size, iov) >= 0) {
958 for (i = 0; i < len; i++) {
959 unsigned long iov_len = iov_iov_len(i);
960 if (iov_len > data_size)
964 data_size -= iov_len;
965 /* include the buffer number to make it easy to
966 * match up the trace with the source */
967 tprintf(" * %lu bytes in buffer %d\n", iov_len, i);
968 dumpstr(tcp, (long) iov_iov_base(i), iov_len);
979 dumpstr(struct tcb *tcp, long addr, int len)
981 static int strsize = -1;
982 static unsigned char *str;
987 "xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx xx "
988 "1234567890123456") + /*in case I'm off by few:*/ 4)
989 /*align to 8 to make memset easier:*/ + 7) & -8
991 const unsigned char *src;
994 memset(outbuf, ' ', sizeof(outbuf));
996 if (strsize < len + 16) {
998 str = malloc(len + 16);
1001 error_msg("Out of memory");
1007 if (umoven(tcp, addr, len, str) < 0)
1010 /* Space-pad to 16 bytes */
1022 *dst++ = "0123456789abcdef"[*src >> 4];
1023 *dst++ = "0123456789abcdef"[*src & 0xf];
1029 dst++; /* space is there by memset */
1032 dst++; /* space is there by memset */
1039 if (*src >= ' ' && *src < 0x7f)
1044 } while (++i & 0xf);
1046 tprintf(" | %05x %s |\n", i - 16, outbuf);
1050 #ifdef HAVE_PROCESS_VM_READV
1051 /* C library supports this, but the kernel might not. */
1052 static bool process_vm_readv_not_supported = 0;
1055 /* Need to do this since process_vm_readv() is not yet available in libc.
1056 * When libc is be updated, only "static bool process_vm_readv_not_supported"
1057 * line should remain.
1059 #if !defined(__NR_process_vm_readv)
1061 # define __NR_process_vm_readv 347
1062 # elif defined(X86_64)
1063 # define __NR_process_vm_readv 310
1064 # elif defined(POWERPC)
1065 # define __NR_process_vm_readv 351
1069 #if defined(__NR_process_vm_readv)
1070 static bool process_vm_readv_not_supported = 0;
1071 /* Have to avoid duplicating with the C library headers. */
1072 static ssize_t strace_process_vm_readv(pid_t pid,
1073 const struct iovec *lvec,
1074 unsigned long liovcnt,
1075 const struct iovec *rvec,
1076 unsigned long riovcnt,
1077 unsigned long flags)
1079 return syscall(__NR_process_vm_readv, (long)pid, lvec, liovcnt, rvec, riovcnt, flags);
1081 #define process_vm_readv strace_process_vm_readv
1083 static bool process_vm_readv_not_supported = 1;
1084 # define process_vm_readv(...) (errno = ENOSYS, -1)
1087 #endif /* end of hack */
1090 vm_read_mem(pid_t pid, void *laddr, long raddr, 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 *tcp, long addr, unsigned int len, void *our_addr)
1111 char *laddr = our_addr;
1113 unsigned int n, m, nread;
1116 char x[sizeof(long)];
1119 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1120 if (current_wordsize < sizeof(addr))
1121 addr &= (1ul << 8 * current_wordsize) - 1;
1124 if (!process_vm_readv_not_supported) {
1125 int r = vm_read_mem(pid, laddr, addr, len);
1126 if ((unsigned int) r == len)
1129 error_msg("umoven: short read (%u < %u) @0x%lx",
1130 (unsigned int) r, len, addr);
1135 process_vm_readv_not_supported = 1;
1138 /* operation not permitted, try PTRACE_PEEKDATA */
1141 /* the process is gone */
1143 case EFAULT: case EIO:
1144 /* address space is inaccessible */
1147 /* all the rest is strange and should be reported */
1148 perror_msg("process_vm_readv");
1154 if (addr & (sizeof(long) - 1)) {
1155 /* addr not a multiple of sizeof(long) */
1156 n = addr & (sizeof(long) - 1); /* residue */
1157 addr &= -sizeof(long); /* aligned address */
1159 u.val = ptrace(PTRACE_PEEKDATA, pid, (void *) addr, 0);
1163 case ESRCH: case EINVAL:
1164 /* these could be seen if the process is gone */
1166 case EFAULT: case EIO: case EPERM:
1167 /* address space is inaccessible */
1170 /* all the rest is strange and should be reported */
1171 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
1175 m = MIN(sizeof(long) - n, len);
1176 memcpy(laddr, &u.x[n], m);
1177 addr += sizeof(long);
1184 u.val = ptrace(PTRACE_PEEKDATA, pid, (void *) addr, 0);
1188 case ESRCH: case EINVAL:
1189 /* these could be seen if the process is gone */
1191 case EFAULT: case EIO: case EPERM:
1192 /* address space is inaccessible */
1194 perror_msg("umoven: short read (%u < %u) @0x%lx",
1195 nread, nread + len, addr - nread);
1199 /* all the rest is strange and should be reported */
1200 perror_msg("umoven: PTRACE_PEEKDATA pid:%d @0x%lx",
1204 m = MIN(sizeof(long), len);
1205 memcpy(laddr, u.x, m);
1206 addr += sizeof(long);
1216 umoven_or_printaddr(struct tcb *tcp, const long addr, const unsigned int len,
1219 if (!addr || !verbose(tcp) || (exiting(tcp) && syserror(tcp)) ||
1220 umoven(tcp, addr, len, our_addr) < 0) {
1228 umoven_or_printaddr_ignore_syserror(struct tcb *tcp, const long addr,
1229 const unsigned int len, void *our_addr)
1231 if (!addr || !verbose(tcp) || umoven(tcp, addr, len, our_addr) < 0) {
1239 * Like `umove' but make the additional effort of looking
1240 * for a terminating zero byte.
1242 * Returns < 0 on error, > 0 if NUL was seen,
1243 * (TODO if useful: return count of bytes including NUL),
1244 * else 0 if len bytes were read but no NUL byte seen.
1246 * Note: there is no guarantee we won't overwrite some bytes
1247 * in laddr[] _after_ terminating NUL (but, of course,
1248 * we never write past laddr[len-1]).
1251 umovestr(struct tcb *tcp, long addr, unsigned int len, char *laddr)
1253 #if SIZEOF_LONG == 4
1254 const unsigned long x01010101 = 0x01010101ul;
1255 const unsigned long x80808080 = 0x80808080ul;
1256 #elif SIZEOF_LONG == 8
1257 const unsigned long x01010101 = 0x0101010101010101ul;
1258 const unsigned long x80808080 = 0x8080808080808080ul;
1260 # error SIZEOF_LONG > 8
1264 unsigned int n, m, nread;
1267 char x[sizeof(long)];
1270 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1271 if (current_wordsize < sizeof(addr))
1272 addr &= (1ul << 8 * current_wordsize) - 1;
1276 if (!process_vm_readv_not_supported) {
1277 const size_t page_size = get_pagesize();
1278 const size_t page_mask = page_size - 1;
1281 unsigned int chunk_len;
1282 unsigned int end_in_page;
1285 * Don't cross pages, otherwise we can get EFAULT
1286 * and fail to notice that terminating NUL lies
1287 * in the existing (first) page.
1289 chunk_len = len > page_size ? page_size : len;
1290 end_in_page = (addr + chunk_len) & page_mask;
1291 if (chunk_len > end_in_page) /* crosses to the next page */
1292 chunk_len -= end_in_page;
1294 int r = vm_read_mem(pid, laddr, addr, chunk_len);
1296 if (memchr(laddr, '\0', r))
1306 process_vm_readv_not_supported = 1;
1307 goto vm_readv_didnt_work;
1309 /* the process is gone */
1312 /* operation not permitted, try PTRACE_PEEKDATA */
1314 goto vm_readv_didnt_work;
1316 case EFAULT: case EIO:
1317 /* address space is inaccessible */
1319 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1320 nread, nread + len, addr - nread);
1324 /* all the rest is strange and should be reported */
1325 perror_msg("process_vm_readv");
1331 vm_readv_didnt_work:
1333 if (addr & (sizeof(long) - 1)) {
1334 /* addr not a multiple of sizeof(long) */
1335 n = addr & (sizeof(long) - 1); /* residue */
1336 addr &= -sizeof(long); /* aligned address */
1338 u.val = ptrace(PTRACE_PEEKDATA, pid, (void *) addr, 0);
1342 case ESRCH: case EINVAL:
1343 /* these could be seen if the process is gone */
1345 case EFAULT: case EIO: case EPERM:
1346 /* address space is inaccessible */
1349 /* all the rest is strange and should be reported */
1350 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1354 m = MIN(sizeof(long) - n, len);
1355 memcpy(laddr, &u.x[n], m);
1356 while (n & (sizeof(long) - 1))
1357 if (u.x[n++] == '\0')
1359 addr += sizeof(long);
1367 u.val = ptrace(PTRACE_PEEKDATA, pid, (void *) addr, 0);
1371 case ESRCH: case EINVAL:
1372 /* these could be seen if the process is gone */
1374 case EFAULT: case EIO: case EPERM:
1375 /* address space is inaccessible */
1377 perror_msg("umovestr: short read (%d < %d) @0x%lx",
1378 nread, nread + len, addr - nread);
1382 /* all the rest is strange and should be reported */
1383 perror_msg("umovestr: PTRACE_PEEKDATA pid:%d @0x%lx",
1387 m = MIN(sizeof(long), len);
1388 memcpy(laddr, u.x, m);
1389 /* "If a NUL char exists in this word" */
1390 if ((u.val - x01010101) & ~u.val & x80808080)
1392 addr += sizeof(long);
1401 * Iteratively fetch and print up to nmemb elements of elem_size size
1402 * from the array that starts at tracee's address start_addr.
1404 * Array elements are being fetched to the address specified by elem_buf.
1406 * The fetcher callback function specified by umoven_func should follow
1407 * the same semantics as umoven_or_printaddr function.
1409 * The printer callback function specified by print_func is expected
1410 * to print something; if it returns false, no more iterations will be made.
1412 * The pointer specified by opaque_data is passed to each invocation
1413 * of print_func callback function.
1415 * This function prints:
1416 * - "NULL", if start_addr is NULL;
1417 * - "[]", if nmemb is 0;
1418 * - start_addr, if nmemb * elem_size overflows or wraps around;
1419 * - nothing, if the first element cannot be fetched
1420 * (if umoven_func returns non-zero), but it is assumed that
1421 * umoven_func has printed the address it failed to fetch data from;
1422 * - elements of the array, delimited by ", ", with the array itself
1423 * enclosed with [] brackets.
1425 * If abbrev(tcp) is true, then
1426 * - the maximum number of elements printed equals to max_strlen;
1427 * - "..." is printed instead of max_strlen+1 element
1428 * and no more iterations will be made.
1430 * This function returns true only if
1431 * - umoven_func has been called at least once AND
1432 * - umoven_func has not returned false.
1435 print_array(struct tcb *tcp,
1436 const unsigned long start_addr,
1438 void *const elem_buf,
1439 const size_t elem_size,
1440 int (*const umoven_func)(struct tcb *,
1444 bool (*const print_func)(struct tcb *,
1448 void *const opaque_data)
1460 const size_t size = nmemb * elem_size;
1461 const unsigned long end_addr = start_addr + size;
1463 if (end_addr <= start_addr || size / elem_size != nmemb) {
1464 printaddr(start_addr);
1468 const unsigned long abbrev_end =
1469 (abbrev(tcp) && max_strlen < nmemb) ?
1470 start_addr + elem_size * max_strlen : end_addr;
1473 for (cur = start_addr; cur < end_addr; cur += elem_size) {
1474 if (cur != start_addr)
1477 if (umoven_func(tcp, cur, elem_size, elem_buf))
1480 if (cur == start_addr)
1483 if (cur >= abbrev_end) {
1489 if (!print_func(tcp, elem_buf, elem_size, opaque_data)) {
1494 if (cur != start_addr)
1497 return cur >= end_addr;
1501 getarg_ll(struct tcb *tcp, int argn)
1503 #if HAVE_STRUCT_TCB_EXT_ARG
1504 # if SUPPORTED_PERSONALITIES > 1
1505 if (current_personality == 1)
1506 return (long) tcp->u_arg[argn];
1509 return (long long) tcp->ext_arg[argn];
1511 return (long) tcp->u_arg[argn];
1516 getarg_ull(struct tcb *tcp, int argn)
1518 #if HAVE_STRUCT_TCB_EXT_ARG
1519 # if SUPPORTED_PERSONALITIES > 1
1520 if (current_personality == 1)
1521 return (unsigned long) tcp->u_arg[argn];
1524 return (unsigned long long) tcp->ext_arg[argn];
1526 return (unsigned long) tcp->u_arg[argn];
1531 printargs(struct tcb *tcp)
1533 const int n = tcp->s_ent->nargs;
1535 for (i = 0; i < n; ++i)
1536 tprintf("%s%#llx", i ? ", " : "", getarg_ull(tcp, i));
1537 return RVAL_DECODED;
1541 printargs_u(struct tcb *tcp)
1543 const int n = tcp->s_ent->nargs;
1545 for (i = 0; i < n; ++i)
1546 tprintf("%s%u", i ? ", " : "",
1547 (unsigned int) tcp->u_arg[i]);
1548 return RVAL_DECODED;
1552 printargs_d(struct tcb *tcp)
1554 const int n = tcp->s_ent->nargs;
1556 for (i = 0; i < n; ++i)
1557 tprintf("%s%d", i ? ", " : "",
1558 (int) tcp->u_arg[i]);
1559 return RVAL_DECODED;
1562 #if defined _LARGEFILE64_SOURCE && defined HAVE_OPEN64
1563 # define open_file open64
1565 # define open_file open
1569 read_int_from_file(const char *const fname, int *const pvalue)
1571 const int fd = open_file(fname, O_RDONLY);
1576 char buf[sizeof(lval) * 3];
1577 int n = read(fd, buf, sizeof(buf) - 1);
1578 int saved_errno = errno;
1582 errno = saved_errno;
1589 lval = strtol(buf, &endptr, 10);
1590 if (!endptr || (*endptr && '\n' != *endptr)
1591 #if INT_MAX < LONG_MAX
1592 || lval > INT_MAX || lval < INT_MIN
1594 || ERANGE == errno) {
1600 *pvalue = (int) lval;